VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/freebsd/memobj-r0drv-freebsd.c@ 23454

Last change on this file since 23454 was 22574, checked in by vboxsync, 15 years ago

Runtime/memobj-r0drv-freebsd: rtR0MemObjNativeReserveInMap may remove arbitrary mappings if pvFixed is -1. Spotted by Alexander Kabaev

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 27.8 KB
Line 
1/* $Id: memobj-r0drv-freebsd.c 22574 2009-08-30 19:57:03Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
8 *
9 * Permission is hereby granted, free of charge, to any person
10 * obtaining a copy of this software and associated documentation
11 * files (the "Software"), to deal in the Software without
12 * restriction, including without limitation the rights to use,
13 * copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following
16 * conditions:
17 *
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include "the-freebsd-kernel.h"
36
37#include <iprt/memobj.h>
38#include <iprt/mem.h>
39#include <iprt/err.h>
40#include <iprt/assert.h>
41#include <iprt/log.h>
42#include <iprt/param.h>
43#include <iprt/process.h>
44#include "internal/memobj.h"
45
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * The FreeBSD version of the memory object structure.
52 */
53typedef struct RTR0MEMOBJFREEBSD
54{
55 /** The core structure. */
56 RTR0MEMOBJINTERNAL Core;
57 /** The VM object associated with the allocation. */
58 vm_object_t pObject;
59 /** the VM object associated with the mapping.
60 * In mapping mem object, this is the shadow object?
61 * In a allocation/enter mem object, this is the shared object we constructed (contig, perhaps alloc). */
62 vm_object_t pMappingObject;
63} RTR0MEMOBJFREEBSD, *PRTR0MEMOBJFREEBSD;
64
65
66MALLOC_DEFINE(M_IPRTMOBJ, "iprtmobj", "IPRT - R0MemObj");
67
68/*******************************************************************************
69* Internal Functions *
70*******************************************************************************/
71
72
73int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
74{
75 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
76 int rc;
77
78 switch (pMemFreeBSD->Core.enmType)
79 {
80 case RTR0MEMOBJTYPE_CONT:
81 contigfree(pMemFreeBSD->Core.pv, pMemFreeBSD->Core.cb, M_IPRTMOBJ);
82 if (pMemFreeBSD->pMappingObject)
83 {
84 rc = vm_map_remove(kernel_map,
85 (vm_offset_t)pMemFreeBSD->Core.pv,
86 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
87 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
88 }
89 break;
90
91 case RTR0MEMOBJTYPE_PAGE:
92 if (pMemFreeBSD->pObject)
93 {
94 rc = vm_map_remove(kernel_map,
95 (vm_offset_t)pMemFreeBSD->Core.pv,
96 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
97 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
98 }
99 else
100 {
101 contigfree(pMemFreeBSD->Core.pv, pMemFreeBSD->Core.cb, M_IPRTMOBJ);
102 if (pMemFreeBSD->pMappingObject)
103 {
104 rc = vm_map_remove(kernel_map,
105 (vm_offset_t)pMemFreeBSD->Core.pv,
106 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
107 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
108 }
109 }
110 break;
111
112 case RTR0MEMOBJTYPE_LOCK:
113 {
114 int fFlags = VM_MAP_WIRE_NOHOLES;
115 vm_map_t pMap = kernel_map;
116
117 if (pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
118 {
119 pMap = &((struct proc *)pMemFreeBSD->Core.u.Lock.R0Process)->p_vmspace->vm_map;
120 fFlags |= VM_MAP_WIRE_USER;
121 }
122 else
123 fFlags |= VM_MAP_WIRE_SYSTEM;
124
125 rc = vm_map_unwire(pMap,
126 (vm_offset_t)pMemFreeBSD->Core.pv,
127 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb,
128 fFlags);
129 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
130 break;
131 }
132
133 case RTR0MEMOBJTYPE_RES_VIRT:
134 {
135 vm_map_t pMap = kernel_map;
136 if (pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
137 pMap = &((struct proc *)pMemFreeBSD->Core.u.Lock.R0Process)->p_vmspace->vm_map;
138 rc = vm_map_remove(pMap,
139 (vm_offset_t)pMemFreeBSD->Core.pv,
140 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
141 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
142 break;
143 }
144
145 case RTR0MEMOBJTYPE_MAPPING:
146 {
147 vm_map_t pMap = kernel_map;
148
149 /* vm_map_remove will unmap the pages we inserted with pmap_enter */
150 AssertMsg(pMemFreeBSD->pMappingObject != NULL, ("MappingObject is NULL\n"));
151 if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
152 pMap = &((struct proc *)pMemFreeBSD->Core.u.Mapping.R0Process)->p_vmspace->vm_map;
153
154 rc = vm_map_remove(pMap,
155 (vm_offset_t)pMemFreeBSD->Core.pv,
156 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
157 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
158 break;
159 }
160
161 /* unused: */
162 case RTR0MEMOBJTYPE_LOW:
163 case RTR0MEMOBJTYPE_PHYS:
164 case RTR0MEMOBJTYPE_PHYS_NC:
165 default:
166 AssertMsgFailed(("enmType=%d\n", pMemFreeBSD->Core.enmType));
167 return VERR_INTERNAL_ERROR;
168 }
169
170 return VINF_SUCCESS;
171}
172
173
174int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
175{
176 int rc;
177 size_t cPages = cb >> PAGE_SHIFT;
178
179 /* create the object. */
180 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PAGE, NULL, cb);
181 if (!pMemFreeBSD)
182 return VERR_NO_MEMORY;
183
184 pMemFreeBSD->pObject = vm_object_allocate(OBJT_DEFAULT, cPages);
185 if (pMemFreeBSD->pObject)
186 {
187 vm_offset_t MapAddress = vm_map_min(kernel_map);
188 rc = vm_map_find(kernel_map, /* map */
189 pMemFreeBSD->pObject, /* object */
190 0, /* offset */
191 &MapAddress, /* addr (IN/OUT) */
192 cb, /* length */
193 TRUE, /* find_space */
194 fExecutable /* protection */
195 ? VM_PROT_ALL
196 : VM_PROT_RW,
197 VM_PROT_ALL, /* max(_prot) */
198 FALSE); /* cow (copy-on-write) */
199 if (rc == KERN_SUCCESS)
200 {
201 vm_offset_t AddressDst = MapAddress;
202
203 rc = VINF_SUCCESS;
204
205 VM_OBJECT_LOCK(pMemFreeBSD->pObject);
206 for (size_t iPage = 0; iPage < cPages; iPage++)
207 {
208 vm_pindex_t PageIndex = OFF_TO_IDX(AddressDst);
209 vm_page_t pPage;
210
211 pPage = vm_page_alloc(pMemFreeBSD->pObject, PageIndex,
212 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
213 VM_ALLOC_WIRED);
214
215#if __FreeBSD_version >= 800000 /** @todo Find exact version number */
216 /* Fixes crashes during VM termination on FreeBSD8-CURRENT amd64
217 * with kernel debugging enabled. */
218 vm_page_set_valid(pPage, 0, PAGE_SIZE);
219#endif
220
221 if (pPage)
222 {
223 vm_page_lock_queues();
224 vm_page_wire(pPage);
225 vm_page_unlock_queues();
226 /* Put the page into the page table now. */
227#if __FreeBSD_version >= 701105
228 pmap_enter(kernel_map->pmap, AddressDst, VM_PROT_NONE, pPage,
229 fExecutable
230 ? VM_PROT_ALL
231 : VM_PROT_RW,
232 TRUE);
233#else
234 pmap_enter(kernel_map->pmap, AddressDst, pPage,
235 fExecutable
236 ? VM_PROT_ALL
237 : VM_PROT_RW,
238 TRUE);
239#endif
240 }
241 else
242 {
243 /*
244 * Allocation failed. vm_map_remove will remove any
245 * page already alocated.
246 */
247 rc = VERR_NO_MEMORY;
248 break;
249 }
250 AddressDst += PAGE_SIZE;
251 }
252 VM_OBJECT_UNLOCK(pMemFreeBSD->pObject);
253
254 if (rc == VINF_SUCCESS)
255 {
256 pMemFreeBSD->Core.pv = (void *)MapAddress;
257 *ppMem = &pMemFreeBSD->Core;
258 return VINF_SUCCESS;
259 }
260
261 vm_map_remove(kernel_map,
262 MapAddress,
263 MapAddress + cb);
264 }
265 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
266 }
267 else
268 rc = VERR_NO_MEMORY;
269
270 rtR0MemObjDelete(&pMemFreeBSD->Core);
271 return rc;
272}
273
274
275int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
276{
277 /*
278 * Try a Alloc first and see if we get luck, if not try contigmalloc.
279 * Might wish to try find our own pages or something later if this
280 * turns into a problemspot on AMD64 boxes.
281 */
282 int rc = rtR0MemObjNativeAllocPage(ppMem, cb, fExecutable);
283 if (RT_SUCCESS(rc))
284 {
285 size_t iPage = cb >> PAGE_SHIFT;
286 while (iPage-- > 0)
287 if (rtR0MemObjNativeGetPagePhysAddr(*ppMem, iPage) > (_4G - PAGE_SIZE))
288 {
289 RTR0MemObjFree(*ppMem, false);
290 *ppMem = NULL;
291 rc = VERR_NO_MEMORY;
292 break;
293 }
294 }
295 if (RT_FAILURE(rc))
296 rc = rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
297 return rc;
298}
299
300
301int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
302{
303 /* create the object. */
304 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb);
305 if (!pMemFreeBSD)
306 return VERR_NO_MEMORY;
307
308 /* do the allocation. */
309 pMemFreeBSD->Core.pv = contigmalloc(cb, /* size */
310 M_IPRTMOBJ, /* type */
311 M_NOWAIT | M_ZERO, /* flags */
312 0, /* lowest physical address*/
313 _4G-1, /* highest physical address */
314 PAGE_SIZE, /* alignment. */
315 0); /* boundrary */
316 if (pMemFreeBSD->Core.pv)
317 {
318 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
319 *ppMem = &pMemFreeBSD->Core;
320 return VINF_SUCCESS;
321 }
322
323 NOREF(fExecutable);
324 rtR0MemObjDelete(&pMemFreeBSD->Core);
325 return VERR_NO_MEMORY;
326}
327
328
329int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
330{
331 /** @todo check if there is a more appropriate API somewhere.. */
332
333 /* create the object. */
334 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb);
335 if (!pMemFreeBSD)
336 return VERR_NO_MEMORY;
337
338 /* do the allocation. */
339 pMemFreeBSD->Core.pv = contigmalloc(cb, /* size */
340 M_IPRTMOBJ, /* type */
341 M_NOWAIT | M_ZERO, /* flags */
342 0, /* lowest physical address*/
343 PhysHighest, /* highest physical address */
344 PAGE_SIZE, /* alignment. */
345 0); /* boundrary */
346 if (pMemFreeBSD->Core.pv)
347 {
348 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
349 *ppMem = &pMemFreeBSD->Core;
350 return VINF_SUCCESS;
351 }
352
353 rtR0MemObjDelete(&pMemFreeBSD->Core);
354 return VERR_NO_MEMORY;
355}
356
357
358int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
359{
360 /** @todo rtR0MemObjNativeAllocPhys / freebsd */
361 return VERR_NOT_SUPPORTED;
362}
363
364
365int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
366{
367 /* create the object. */
368 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS, NULL, cb);
369 if (!pMemFreeBSD)
370 return VERR_NO_MEMORY;
371
372 /* there is no allocation here, it needs to be mapped somewhere first. */
373 pMemFreeBSD->Core.u.Phys.fAllocated = false;
374 pMemFreeBSD->Core.u.Phys.PhysBase = Phys;
375 *ppMem = &pMemFreeBSD->Core;
376 return VINF_SUCCESS;
377}
378
379
380int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
381{
382 int rc;
383
384 /* create the object. */
385 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, (void *)R3Ptr, cb);
386 if (!pMemFreeBSD)
387 return VERR_NO_MEMORY;
388
389 /*
390 * We could've used vslock here, but we don't wish to be subject to
391 * resource usage restrictions, so we'll call vm_map_wire directly.
392 */
393 rc = vm_map_wire(&((struct proc *)R0Process)->p_vmspace->vm_map, /* the map */
394 (vm_offset_t)R3Ptr, /* start */
395 (vm_offset_t)R3Ptr + cb, /* end */
396 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); /* flags */
397 if (rc == KERN_SUCCESS)
398 {
399 pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
400 *ppMem = &pMemFreeBSD->Core;
401 return VINF_SUCCESS;
402 }
403 rtR0MemObjDelete(&pMemFreeBSD->Core);
404 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
405}
406
407
408int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
409{
410 int rc;
411
412 /* create the object. */
413 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, pv, cb);
414 if (!pMemFreeBSD)
415 return VERR_NO_MEMORY;
416
417 /* lock the memory */
418 rc = vm_map_wire(kernel_map, /* the map */
419 (vm_offset_t)pv, /* start */
420 (vm_offset_t)pv + cb, /* end */
421 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); /* flags - SYSTEM? */
422 if (rc == KERN_SUCCESS)
423 {
424 pMemFreeBSD->Core.u.Lock.R0Process = NIL_RTR0PROCESS;
425 *ppMem = &pMemFreeBSD->Core;
426 return VINF_SUCCESS;
427 }
428 rtR0MemObjDelete(&pMemFreeBSD->Core);
429 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
430}
431
432
433/**
434 * Worker for the two virtual address space reservers.
435 *
436 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
437 */
438static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process, vm_map_t pMap)
439{
440 int rc;
441
442 /*
443 * The pvFixed address range must be within the VM space when specified.
444 */
445 if (pvFixed != (void *)-1
446 && ( (vm_offset_t)pvFixed < vm_map_min(pMap)
447 || (vm_offset_t)pvFixed + cb > vm_map_max(pMap)))
448 return VERR_INVALID_PARAMETER;
449
450 /*
451 * Check that the specified alignment is supported.
452 */
453 if (uAlignment > PAGE_SIZE)
454 return VERR_NOT_SUPPORTED;
455
456 /*
457 * Create the object.
458 */
459 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_RES_VIRT, NULL, cb);
460 if (!pMemFreeBSD)
461 return VERR_NO_MEMORY;
462
463 /*
464 * Allocate an empty VM object and map it into the requested map.
465 */
466 pMemFreeBSD->pObject = vm_object_allocate(OBJT_DEFAULT, cb >> PAGE_SHIFT);
467 if (pMemFreeBSD->pObject)
468 {
469 vm_offset_t MapAddress = pvFixed != (void *)-1
470 ? (vm_offset_t)pvFixed
471 : vm_map_min(pMap);
472 if (pvFixed != (void *)-1)
473 vm_map_remove(pMap,
474 MapAddress,
475 MapAddress + cb);
476
477 rc = vm_map_find(pMap, /* map */
478 pMemFreeBSD->pObject, /* object */
479 0, /* offset */
480 &MapAddress, /* addr (IN/OUT) */
481 cb, /* length */
482 pvFixed == (void *)-1, /* find_space */
483 VM_PROT_NONE, /* protection */
484 VM_PROT_ALL, /* max(_prot) ?? */
485 0); /* cow (copy-on-write) */
486 if (rc == KERN_SUCCESS)
487 {
488 if (R0Process != NIL_RTR0PROCESS)
489 {
490 rc = vm_map_inherit(pMap,
491 MapAddress,
492 MapAddress + cb,
493 VM_INHERIT_SHARE);
494 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
495 }
496 pMemFreeBSD->Core.pv = (void *)MapAddress;
497 pMemFreeBSD->Core.u.ResVirt.R0Process = R0Process;
498 *ppMem = &pMemFreeBSD->Core;
499 return VINF_SUCCESS;
500 }
501 vm_object_deallocate(pMemFreeBSD->pObject);
502 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
503 }
504 else
505 rc = VERR_NO_MEMORY;
506 rtR0MemObjDelete(&pMemFreeBSD->Core);
507 return rc;
508
509}
510
511int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
512{
513 return rtR0MemObjNativeReserveInMap(ppMem, pvFixed, cb, uAlignment, NIL_RTR0PROCESS, kernel_map);
514}
515
516
517int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
518{
519 return rtR0MemObjNativeReserveInMap(ppMem, (void *)R3PtrFixed, cb, uAlignment, R0Process,
520 &((struct proc *)R0Process)->p_vmspace->vm_map);
521}
522
523
524int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
525 unsigned fProt, size_t offSub, size_t cbSub)
526{
527 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
528 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
529
530 /*
531 * Check that the specified alignment is supported.
532 */
533 if (uAlignment > PAGE_SIZE)
534 return VERR_NOT_SUPPORTED;
535
536
537
538/* Phys: see pmap_mapdev in i386/i386/pmap.c (http://fxr.watson.org/fxr/source/i386/i386/pmap.c?v=RELENG62#L2860) */
539
540#if 0
541/** @todo finish the implementation. */
542
543 int rc;
544 void *pvR0 = NULL;
545 PRTR0MEMOBJFREEBSD pMemToMapOs2 = (PRTR0MEMOBJFREEBSD)pMemToMap;
546 switch (pMemToMapOs2->Core.enmType)
547 {
548 /*
549 * These has kernel mappings.
550 */
551 case RTR0MEMOBJTYPE_PAGE:
552 case RTR0MEMOBJTYPE_LOW:
553 case RTR0MEMOBJTYPE_CONT:
554 pvR0 = pMemToMapOs2->Core.pv;
555 break;
556
557 case RTR0MEMOBJTYPE_PHYS_NC:
558 case RTR0MEMOBJTYPE_PHYS:
559 pvR0 = pMemToMapOs2->Core.pv;
560 if (!pvR0)
561 {
562 /* no ring-0 mapping, so allocate a mapping in the process. */
563 AssertMsgReturn(uAlignment == PAGE_SIZE, ("%#zx\n", uAlignment), VERR_NOT_SUPPORTED);
564 AssertMsgReturn(fProt & RTMEM_PROT_WRITE, ("%#x\n", fProt), VERR_NOT_SUPPORTED);
565 Assert(!pMemToMapOs2->Core.u.Phys.fAllocated);
566 ULONG ulPhys = pMemToMapOs2->Core.u.Phys.PhysBase;
567 rc = KernVMAlloc(pMemToMapOs2->Core.cb, VMDHA_PHYS, &pvR0, (PPVOID)&ulPhys, NULL);
568 if (rc)
569 return RTErrConvertFromOS2(rc);
570 pMemToMapOs2->Core.pv = pvR0;
571 }
572 break;
573
574 case RTR0MEMOBJTYPE_LOCK:
575 if (pMemToMapOs2->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
576 return VERR_NOT_SUPPORTED; /** @todo implement this... */
577 pvR0 = pMemToMapOs2->Core.pv;
578 break;
579
580 case RTR0MEMOBJTYPE_RES_VIRT:
581 case RTR0MEMOBJTYPE_MAPPING:
582 default:
583 AssertMsgFailed(("enmType=%d\n", pMemToMapOs2->Core.enmType));
584 return VERR_INTERNAL_ERROR;
585 }
586
587 /*
588 * Create a dummy mapping object for it.
589 *
590 * All mappings are read/write/execute in OS/2 and there isn't
591 * any cache options, so sharing is ok. And the main memory object
592 * isn't actually freed until all the mappings have been freed up
593 * (reference counting).
594 */
595 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(RT_OFFSETOF(RTR0MEMOBJOS2, Lock), RTR0MEMOBJTYPE_MAPPING, pvR0, pMemToMapOs2->Core.cb);
596 if (pMemFreeBSD)
597 {
598 pMemFreeBSD->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
599 *ppMem = &pMemFreeBSD->Core;
600 return VINF_SUCCESS;
601 }
602 return VERR_NO_MEMORY;
603#endif
604 return VERR_NOT_IMPLEMENTED;
605}
606
607
608/* see http://markmail.org/message/udhq33tefgtyfozs */
609int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
610{
611 /*
612 * Check for unsupported stuff.
613 */
614 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
615 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
616 if (uAlignment > PAGE_SIZE)
617 return VERR_NOT_SUPPORTED;
618
619 int rc;
620 vm_object_t pObjectToMap = ((PRTR0MEMOBJFREEBSD)pMemToMap)->pObject;
621 struct proc *pProc = (struct proc *)R0Process;
622 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
623
624 /* calc protection */
625 vm_prot_t ProtectionFlags = 0;
626 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
627 ProtectionFlags = VM_PROT_NONE;
628 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
629 ProtectionFlags |= VM_PROT_READ;
630 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
631 ProtectionFlags |= VM_PROT_WRITE;
632 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
633 ProtectionFlags |= VM_PROT_EXECUTE;
634
635 /* calc mapping address */
636 PROC_LOCK(pProc);
637 vm_offset_t AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
638 PROC_UNLOCK(pProc);
639
640 vm_object_t pObjectNew = vm_object_allocate(OBJT_PHYS, pMemToMap->cb >> PAGE_SHIFT);
641 if (!RT_UNLIKELY(pObjectNew))
642 return VERR_NO_MEMORY;
643
644 /* Insert the object in the map. */
645 rc = vm_map_find(pProcMap, /* Map to insert the object in */
646 pObjectNew , /* Object to map */
647 0, /* Start offset in the object */
648 &AddrR3, /* Start address IN/OUT */
649 pMemToMap->cb, /* Size of the mapping */
650 TRUE, /* Whether a suitable address should be searched for first */
651 ProtectionFlags, /* protection flags */
652 VM_PROT_ALL, /* Maximum protection flags */
653 0); /* Copy on write */
654
655 /* Map the memory page by page into the destination map. */
656 if (rc == KERN_SUCCESS)
657 {
658 size_t cLeft = pMemToMap->cb >> PAGE_SHIFT;
659 vm_offset_t AddrToMap = (vm_offset_t)pMemToMap->pv;
660 pmap_t pPhysicalMap = pProcMap->pmap;
661 vm_offset_t AddrR3Dst = AddrR3;
662
663 /* Insert the memory page by page into the mapping. */
664 while (cLeft-- > 0)
665 {
666 vm_page_t Page = PHYS_TO_VM_PAGE(vtophys(AddrToMap));
667
668#if __FreeBSD_version >= 701105
669 pmap_enter(pPhysicalMap, AddrR3Dst, VM_PROT_NONE, Page, ProtectionFlags, TRUE);
670#else
671 pmap_enter(pPhysicalMap, AddrR3Dst, Page, ProtectionFlags, TRUE);
672#endif
673 AddrToMap += PAGE_SIZE;
674 AddrR3Dst += PAGE_SIZE;
675 }
676 pObjectToMap = pObjectNew;
677 }
678 else
679 vm_object_deallocate(pObjectNew);
680
681 if (rc == KERN_SUCCESS)
682 {
683 /*
684 * Create a mapping object for it.
685 */
686 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
687 RTR0MEMOBJTYPE_MAPPING,
688 (void *)AddrR3,
689 pMemToMap->cb);
690 if (pMemFreeBSD)
691 {
692 Assert((vm_offset_t)pMemFreeBSD->Core.pv == AddrR3);
693 pMemFreeBSD->Core.u.Mapping.R0Process = R0Process;
694 pMemFreeBSD->pMappingObject = pObjectToMap;
695 *ppMem = &pMemFreeBSD->Core;
696 return VINF_SUCCESS;
697 }
698
699 rc = vm_map_remove(pProcMap, ((vm_offset_t)AddrR3), ((vm_offset_t)AddrR3) + pMemToMap->cb);
700 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
701 }
702
703 if (pObjectToMap)
704 vm_object_deallocate(pObjectToMap);
705
706 return VERR_NO_MEMORY;
707}
708
709
710int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
711{
712 NOREF(pMem);
713 NOREF(offSub);
714 NOREF(cbSub);
715 NOREF(fProt);
716 return VERR_NOT_SUPPORTED;
717}
718
719
720RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
721{
722 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
723
724 switch (pMemFreeBSD->Core.enmType)
725 {
726 case RTR0MEMOBJTYPE_LOCK:
727 if ( pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS
728 && pMemFreeBSD->Core.u.Lock.R0Process != (RTR0PROCESS)curproc)
729 {
730 /* later */
731 return NIL_RTHCPHYS;
732 }
733 /* fall thru*/
734 case RTR0MEMOBJTYPE_PAGE:
735 case RTR0MEMOBJTYPE_MAPPING:
736 {
737 uint8_t *pb = (uint8_t *)pMemFreeBSD->Core.pv + (iPage << PAGE_SHIFT);
738 return vtophys(pb);
739 }
740
741 case RTR0MEMOBJTYPE_CONT:
742 return pMemFreeBSD->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
743
744 case RTR0MEMOBJTYPE_PHYS:
745 return pMemFreeBSD->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
746
747 case RTR0MEMOBJTYPE_PHYS_NC:
748 case RTR0MEMOBJTYPE_RES_VIRT:
749 case RTR0MEMOBJTYPE_LOW:
750 default:
751 return NIL_RTHCPHYS;
752 }
753}
754
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette