VirtualBox

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

Last change on this file since 39657 was 39657, checked in by vboxsync, 13 years ago

IPRT/FreeBSD: Rewrite of R0 memory objects handling, thanks to Andriy Gapon and Bernhard Froehlich

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.6 KB
Line 
1/* $Id: memobj-r0drv-freebsd.c 39657 2011-12-19 18:34:13Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <[email protected]>
8 * Copyright (c) 2011 Andriy Gapon <[email protected]>
9 *
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated documentation
12 * files (the "Software"), to deal in the Software without
13 * restriction, including without limitation the rights to use,
14 * copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following
17 * conditions:
18 *
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
24 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
27 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
30 */
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#include "the-freebsd-kernel.h"
37
38#include <iprt/memobj.h>
39#include <iprt/mem.h>
40#include <iprt/err.h>
41#include <iprt/assert.h>
42#include <iprt/log.h>
43#include <iprt/param.h>
44#include <iprt/process.h>
45#include "internal/memobj.h"
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} RTR0MEMOBJFREEBSD, *PRTR0MEMOBJFREEBSD;
60
61
62MALLOC_DEFINE(M_IPRTMOBJ, "iprtmobj", "IPRT - R0MemObj");
63
64/**
65 * Gets the virtual memory map the specified object is mapped into.
66 *
67 * @returns VM map handle on success, NULL if no map.
68 * @param pMem The memory object.
69 */
70static vm_map_t rtR0MemObjFreeBSDGetMap(PRTR0MEMOBJINTERNAL pMem)
71{
72 switch (pMem->enmType)
73 {
74 case RTR0MEMOBJTYPE_PAGE:
75 case RTR0MEMOBJTYPE_LOW:
76 case RTR0MEMOBJTYPE_CONT:
77 return kernel_map;
78
79 case RTR0MEMOBJTYPE_PHYS:
80 case RTR0MEMOBJTYPE_PHYS_NC:
81 return NULL; /* pretend these have no mapping atm. */
82
83 case RTR0MEMOBJTYPE_LOCK:
84 return pMem->u.Lock.R0Process == NIL_RTR0PROCESS
85 ? kernel_map
86 : &((struct proc *)pMem->u.Lock.R0Process)->p_vmspace->vm_map;
87
88 case RTR0MEMOBJTYPE_RES_VIRT:
89 return pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS
90 ? kernel_map
91 : &((struct proc *)pMem->u.ResVirt.R0Process)->p_vmspace->vm_map;
92
93 case RTR0MEMOBJTYPE_MAPPING:
94 return pMem->u.Mapping.R0Process == NIL_RTR0PROCESS
95 ? kernel_map
96 : &((struct proc *)pMem->u.Mapping.R0Process)->p_vmspace->vm_map;
97
98 default:
99 return NULL;
100 }
101}
102
103
104DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
105{
106 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
107 int rc;
108
109 switch (pMemFreeBSD->Core.enmType)
110 {
111 case RTR0MEMOBJTYPE_PAGE:
112 case RTR0MEMOBJTYPE_LOW:
113 case RTR0MEMOBJTYPE_CONT:
114 {
115 rc = vm_map_remove(kernel_map,
116 (vm_offset_t)pMemFreeBSD->Core.pv,
117 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
118 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
119 break;
120 }
121
122 case RTR0MEMOBJTYPE_LOCK:
123 {
124 vm_map_t pMap = kernel_map;
125
126 if (pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS)
127 pMap = &((struct proc *)pMemFreeBSD->Core.u.Lock.R0Process)->p_vmspace->vm_map;
128
129 rc = vm_map_unwire(pMap,
130 (vm_offset_t)pMemFreeBSD->Core.pv,
131 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb,
132 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
133 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
134 break;
135 }
136
137 case RTR0MEMOBJTYPE_RES_VIRT:
138 {
139 vm_map_t pMap = kernel_map;
140 if (pMemFreeBSD->Core.u.ResVirt.R0Process != NIL_RTR0PROCESS)
141 pMap = &((struct proc *)pMemFreeBSD->Core.u.ResVirt.R0Process)->p_vmspace->vm_map;
142 rc = vm_map_remove(pMap,
143 (vm_offset_t)pMemFreeBSD->Core.pv,
144 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
145 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
146 break;
147 }
148
149 case RTR0MEMOBJTYPE_MAPPING:
150 {
151 vm_map_t pMap = kernel_map;
152
153 if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
154 pMap = &((struct proc *)pMemFreeBSD->Core.u.Mapping.R0Process)->p_vmspace->vm_map;
155 rc = vm_map_remove(pMap,
156 (vm_offset_t)pMemFreeBSD->Core.pv,
157 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
158 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
159 break;
160 }
161
162 case RTR0MEMOBJTYPE_PHYS:
163 case RTR0MEMOBJTYPE_PHYS_NC:
164 {
165 VM_OBJECT_LOCK(pMemFreeBSD->pObject);
166 vm_page_t pPage = vm_page_find_least(pMemFreeBSD->pObject, 0);
167 vm_page_lock_queues();
168 for (vm_page_t pPage = vm_page_find_least(pMemFreeBSD->pObject, 0);
169 pPage != NULL;
170 pPage = vm_page_next(pPage))
171 {
172 vm_page_unwire(pPage, 0);
173 }
174 vm_page_unlock_queues();
175 VM_OBJECT_UNLOCK(pMemFreeBSD->pObject);
176 vm_object_deallocate(pMemFreeBSD->pObject);
177 break;
178 }
179
180 default:
181 AssertMsgFailed(("enmType=%d\n", pMemFreeBSD->Core.enmType));
182 return VERR_INTERNAL_ERROR;
183 }
184
185 return VINF_SUCCESS;
186}
187
188
189static vm_page_t rtR0MemObjFreeBSDContigPhysAllocHelper(vm_object_t pObject, vm_pindex_t iPIndex,
190 u_long cPages, vm_paddr_t VmPhysAddrHigh,
191 u_long uAlignment, bool fWire)
192{
193 vm_page_t pPages;
194#if __FreeBSD_version > 1000000
195 int fFlags = VM_ALLOC_INTERRUPT | VM_ALLOC_NOBUSY;
196 if (fWire)
197 fFlags |= VM_ALLOC_WIRED;
198 VM_OBJECT_LOCK(pObject);
199 pPages = vm_page_alloc_contig(pObject, iPIndex, fFlags, cPages, 0, VmPhysAddrHigh, uAlignment, 0, VM_MEMATTR_DEFAULT);
200 VM_OBJECT_UNLOCK(pObject);
201 return pPages;
202#else
203 pPages = vm_phys_alloc_contig(cPages, 0, VmPhysAddrHigh, uAlignment, 0);
204 if (!pPages)
205 return pPages;
206 VM_OBJECT_LOCK(pObject);
207 for (vm_pindex_t iPage = 0; iPage < cPages; iPage++)
208 {
209 vm_page_t pPage = pPages + iPage;
210 vm_page_insert(pPage, pObject, iPIndex + iPage);
211 pPage->valid = VM_PAGE_BITS_ALL;
212 if (fWire)
213 {
214 pPage->wire_count = 1;
215 atomic_add_int(&cnt.v_wire_count, 1);
216 }
217 }
218 VM_OBJECT_UNLOCK(pObject);
219 return pPages;
220#endif
221}
222
223static int rtR0MemObjFreeBSDPhysAllocHelper(vm_object_t pObject, u_long cPages,
224 vm_paddr_t VmPhysAddrHigh, u_long uAlignment,
225 bool fContiguous, bool fWire)
226{
227 if (fContiguous)
228 {
229 if (rtR0MemObjFreeBSDContigPhysAllocHelper(pObject, 0, cPages, VmPhysAddrHigh,
230 uAlignment, fWire) != NULL)
231 return VINF_SUCCESS;
232 else
233 return VERR_NO_MEMORY;
234 }
235
236 for (vm_pindex_t iPage = 0; iPage < cPages; iPage++)
237 {
238 vm_page_t pPage = rtR0MemObjFreeBSDContigPhysAllocHelper(pObject, iPage, 1, VmPhysAddrHigh,
239 uAlignment, fWire);
240 if (!pPage)
241 {
242 /* Free all allocated pages */
243 VM_OBJECT_LOCK(pObject);
244 while (iPage-- > 0)
245 {
246 pPage = vm_page_lookup(pObject, iPage);
247 vm_page_lock_queues();
248 if (fWire)
249 vm_page_unwire(pPage, 0);
250 vm_page_free(pPage);
251 vm_page_unlock_queues();
252 }
253 VM_OBJECT_UNLOCK(pObject);
254 return VERR_NO_MEMORY;
255 }
256 }
257 return VINF_SUCCESS;
258}
259
260static int rtR0MemObjFreeBSDAllocHelper(PRTR0MEMOBJFREEBSD pMemFreeBSD, bool fExecutable,
261 vm_paddr_t VmPhysAddrHigh, bool fContiguous)
262{
263 int rc;
264 size_t cPages = atop(pMemFreeBSD->Core.cb);
265
266 pMemFreeBSD->pObject = vm_object_allocate(OBJT_PHYS, cPages);
267 vm_offset_t MapAddress;
268
269 /* No additional object reference for auto-deallocation upon unmapping. */
270 rc = vm_map_find(kernel_map, pMemFreeBSD->pObject, 0,
271 &MapAddress, pMemFreeBSD->Core.cb, VMFS_ANY_SPACE,
272 fExecutable ? VM_PROT_ALL : VM_PROT_RW, VM_PROT_ALL, 0);
273
274 if (rc == KERN_SUCCESS)
275 {
276 rc = rtR0MemObjFreeBSDPhysAllocHelper(pMemFreeBSD->pObject, cPages,
277 VmPhysAddrHigh, PAGE_SIZE, fContiguous, false);
278 if (RT_SUCCESS(rc)) {
279 vm_map_wire(kernel_map, MapAddress, MapAddress + pMemFreeBSD->Core.cb,
280 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
281
282 /* Store start address */
283 pMemFreeBSD->Core.pv = (void *)MapAddress;
284 return VINF_SUCCESS;
285 }
286
287 vm_map_remove(kernel_map, MapAddress, MapAddress + pMemFreeBSD->Core.cb);
288 }
289 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
290
291 vm_object_deallocate(pMemFreeBSD->pObject);
292 rtR0MemObjDelete(&pMemFreeBSD->Core);
293 return rc;
294}
295DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
296{
297 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
298 RTR0MEMOBJTYPE_PAGE, NULL, cb);
299 if (!pMemFreeBSD)
300 return VERR_NO_MEMORY;
301
302 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, ~(vm_paddr_t)0, false);
303 if (RT_FAILURE(rc))
304 {
305 rtR0MemObjDelete(&pMemFreeBSD->Core);
306 return rc;
307 }
308
309 *ppMem = &pMemFreeBSD->Core;
310 return rc;
311}
312
313
314DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
315{
316 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
317 RTR0MEMOBJTYPE_LOW, NULL, cb);
318 if (!pMemFreeBSD)
319 return VERR_NO_MEMORY;
320
321 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, _4G, false);
322 if (RT_FAILURE(rc))
323 {
324 rtR0MemObjDelete(&pMemFreeBSD->Core);
325 return rc;
326 }
327
328 *ppMem = &pMemFreeBSD->Core;
329 return rc;
330}
331
332
333DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
334{
335 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
336 RTR0MEMOBJTYPE_CONT, NULL, cb);
337 if (!pMemFreeBSD)
338 return VERR_NO_MEMORY;
339
340 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, _4G, true);
341 if (RT_FAILURE(rc))
342 {
343 rtR0MemObjDelete(&pMemFreeBSD->Core);
344 return rc;
345 }
346
347 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
348 *ppMem = &pMemFreeBSD->Core;
349 return rc;
350}
351
352
353static int rtR0MemObjFreeBSDAllocPhysPages(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
354 size_t cb,
355 RTHCPHYS PhysHighest, size_t uAlignment,
356 bool fContiguous)
357{
358 uint32_t cPages = atop(cb);
359 vm_paddr_t VmPhysAddrHigh;
360
361 /* create the object. */
362 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
363 enmType, NULL, cb);
364 if (!pMemFreeBSD)
365 return VERR_NO_MEMORY;
366
367 pMemFreeBSD->pObject = vm_object_allocate(OBJT_PHYS, atop(cb));
368
369 if (PhysHighest != NIL_RTHCPHYS)
370 VmPhysAddrHigh = PhysHighest;
371 else
372 VmPhysAddrHigh = ~(vm_paddr_t)0;
373
374 int rc = rtR0MemObjFreeBSDPhysAllocHelper(pMemFreeBSD->pObject, cPages, VmPhysAddrHigh,
375 uAlignment, fContiguous, true);
376
377 if (RT_FAILURE(rc)) {
378 vm_object_deallocate(pMemFreeBSD->pObject);
379 rtR0MemObjDelete(&pMemFreeBSD->Core);
380 }
381 else
382 {
383 if (fContiguous)
384 {
385 Assert(enmType == RTR0MEMOBJTYPE_PHYS);
386 VM_OBJECT_LOCK(pMemFreeBSD->pObject);
387 pMemFreeBSD->Core.u.Phys.PhysBase = VM_PAGE_TO_PHYS(vm_page_find_least(pMemFreeBSD->pObject, 0));
388 VM_OBJECT_UNLOCK(pMemFreeBSD->pObject);
389 pMemFreeBSD->Core.u.Phys.fAllocated = true;
390 }
391
392 *ppMem = &pMemFreeBSD->Core;
393 }
394
395 return rc;
396}
397
398
399DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
400{
401 return rtR0MemObjFreeBSDAllocPhysPages(ppMem, RTR0MEMOBJTYPE_PHYS, cb, PhysHighest, uAlignment, true);
402}
403
404
405DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
406{
407 return rtR0MemObjFreeBSDAllocPhysPages(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PhysHighest, PAGE_SIZE, false);
408}
409
410
411DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
412{
413 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
414
415 /* create the object. */
416 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS, NULL, cb);
417 if (!pMemFreeBSD)
418 return VERR_NO_MEMORY;
419
420 /* there is no allocation here, it needs to be mapped somewhere first. */
421 pMemFreeBSD->Core.u.Phys.fAllocated = false;
422 pMemFreeBSD->Core.u.Phys.PhysBase = Phys;
423 pMemFreeBSD->Core.u.Phys.uCachePolicy = uCachePolicy;
424 *ppMem = &pMemFreeBSD->Core;
425 return VINF_SUCCESS;
426}
427
428
429/**
430 * Worker locking the memory in either kernel or user maps.
431 */
432static int rtR0MemObjNativeLockInMap(PPRTR0MEMOBJINTERNAL ppMem, vm_map_t pVmMap,
433 vm_offset_t AddrStart, size_t cb, uint32_t fAccess,
434 RTR0PROCESS R0Process, int fFlags)
435{
436 int rc;
437 NOREF(fAccess);
438
439 /* create the object. */
440 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, (void *)AddrStart, cb);
441 if (!pMemFreeBSD)
442 return VERR_NO_MEMORY;
443
444 /*
445 * We could've used vslock here, but we don't wish to be subject to
446 * resource usage restrictions, so we'll call vm_map_wire directly.
447 */
448 rc = vm_map_wire(pVmMap, /* the map */
449 AddrStart, /* start */
450 AddrStart + cb, /* end */
451 fFlags); /* flags */
452 if (rc == KERN_SUCCESS)
453 {
454 pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
455 *ppMem = &pMemFreeBSD->Core;
456 return VINF_SUCCESS;
457 }
458 rtR0MemObjDelete(&pMemFreeBSD->Core);
459 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
460}
461
462
463DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
464{
465 return rtR0MemObjNativeLockInMap(ppMem,
466 &((struct proc *)R0Process)->p_vmspace->vm_map,
467 (vm_offset_t)R3Ptr,
468 cb,
469 fAccess,
470 R0Process,
471 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
472}
473
474
475DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
476{
477 return rtR0MemObjNativeLockInMap(ppMem,
478 kernel_map,
479 (vm_offset_t)pv,
480 cb,
481 fAccess,
482 NIL_RTR0PROCESS,
483 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
484}
485
486
487/**
488 * Worker for the two virtual address space reservers.
489 *
490 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
491 */
492static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process, vm_map_t pMap)
493{
494 int rc;
495
496 /*
497 * The pvFixed address range must be within the VM space when specified.
498 */
499 if (pvFixed != (void *)-1
500 && ( (vm_offset_t)pvFixed < vm_map_min(pMap)
501 || (vm_offset_t)pvFixed + cb > vm_map_max(pMap)))
502 return VERR_INVALID_PARAMETER;
503
504 /*
505 * Check that the specified alignment is supported.
506 */
507 if (uAlignment > PAGE_SIZE)
508 return VERR_NOT_SUPPORTED;
509
510 /*
511 * Create the object.
512 */
513 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_RES_VIRT, NULL, cb);
514 if (!pMemFreeBSD)
515 return VERR_NO_MEMORY;
516
517 vm_offset_t MapAddress = pvFixed != (void *)-1
518 ? (vm_offset_t)pvFixed
519 : vm_map_min(pMap);
520 if (pvFixed != (void *)-1)
521 vm_map_remove(pMap,
522 MapAddress,
523 MapAddress + cb);
524
525 rc = vm_map_find(pMap, /* map */
526 NULL, /* object */
527 0, /* offset */
528 &MapAddress, /* addr (IN/OUT) */
529 cb, /* length */
530 pvFixed == (void *)-1 ? VMFS_ANY_SPACE : VMFS_NO_SPACE,
531 /* find_space */
532 VM_PROT_NONE, /* protection */
533 VM_PROT_ALL, /* max(_prot) ?? */
534 0); /* cow (copy-on-write) */
535 if (rc == KERN_SUCCESS)
536 {
537 if (R0Process != NIL_RTR0PROCESS)
538 {
539 rc = vm_map_inherit(pMap,
540 MapAddress,
541 MapAddress + cb,
542 VM_INHERIT_SHARE);
543 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
544 }
545 pMemFreeBSD->Core.pv = (void *)MapAddress;
546 pMemFreeBSD->Core.u.ResVirt.R0Process = R0Process;
547 *ppMem = &pMemFreeBSD->Core;
548 return VINF_SUCCESS;
549 }
550
551 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
552 rtR0MemObjDelete(&pMemFreeBSD->Core);
553 return rc;
554
555}
556
557
558DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
559{
560 return rtR0MemObjNativeReserveInMap(ppMem, pvFixed, cb, uAlignment, NIL_RTR0PROCESS, kernel_map);
561}
562
563
564DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
565{
566 return rtR0MemObjNativeReserveInMap(ppMem, (void *)R3PtrFixed, cb, uAlignment, R0Process,
567 &((struct proc *)R0Process)->p_vmspace->vm_map);
568}
569
570
571DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
572 unsigned fProt, size_t offSub, size_t cbSub)
573{
574// AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
575 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
576
577 /*
578 * Check that the specified alignment is supported.
579 */
580 if (uAlignment > PAGE_SIZE)
581 return VERR_NOT_SUPPORTED;
582
583 int rc;
584 PRTR0MEMOBJFREEBSD pMemToMapFreeBSD = (PRTR0MEMOBJFREEBSD)pMemToMap;
585
586 /* calc protection */
587 vm_prot_t ProtectionFlags = 0;
588 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
589 ProtectionFlags = VM_PROT_NONE;
590 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
591 ProtectionFlags |= VM_PROT_READ;
592 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
593 ProtectionFlags |= VM_PROT_WRITE;
594 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
595 ProtectionFlags |= VM_PROT_EXECUTE;
596
597 vm_offset_t Addr = vm_map_min(kernel_map);
598 if (cbSub == 0)
599 cbSub = pMemToMap->cb - offSub;
600
601 vm_object_reference(pMemToMapFreeBSD->pObject);
602 rc = vm_map_find(kernel_map, /* Map to insert the object in */
603 pMemToMapFreeBSD->pObject, /* Object to map */
604 offSub, /* Start offset in the object */
605 &Addr, /* Start address IN/OUT */
606 cbSub, /* Size of the mapping */
607 VMFS_ANY_SPACE, /* Whether a suitable address should be searched for first */
608 ProtectionFlags, /* protection flags */
609 VM_PROT_ALL, /* Maximum protection flags */
610 0); /* copy-on-write and similar flags */
611
612 if (rc == KERN_SUCCESS)
613 {
614 rc = vm_map_wire(kernel_map, Addr, Addr + cbSub, VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
615 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
616
617 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
618 RTR0MEMOBJTYPE_MAPPING,
619 (void *)Addr,
620 cbSub);
621 if (pMemFreeBSD)
622 {
623 Assert((vm_offset_t)pMemFreeBSD->Core.pv == Addr);
624 pMemFreeBSD->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
625 *ppMem = &pMemFreeBSD->Core;
626 return VINF_SUCCESS;
627 }
628 rc = vm_map_remove(kernel_map, Addr, Addr + cbSub);
629 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
630 }
631 else
632 vm_object_deallocate(pMemToMapFreeBSD->pObject);
633
634 return VERR_NO_MEMORY;
635}
636
637
638DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment,
639 unsigned fProt, RTR0PROCESS R0Process)
640{
641 /*
642 * Check for unsupported stuff.
643 */
644 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
645 if (uAlignment > PAGE_SIZE)
646 return VERR_NOT_SUPPORTED;
647
648 int rc;
649 PRTR0MEMOBJFREEBSD pMemToMapFreeBSD = (PRTR0MEMOBJFREEBSD)pMemToMap;
650 struct proc *pProc = (struct proc *)R0Process;
651 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
652
653 /* calc protection */
654 vm_prot_t ProtectionFlags = 0;
655 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
656 ProtectionFlags = VM_PROT_NONE;
657 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
658 ProtectionFlags |= VM_PROT_READ;
659 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
660 ProtectionFlags |= VM_PROT_WRITE;
661 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
662 ProtectionFlags |= VM_PROT_EXECUTE;
663
664 /* calc mapping address */
665 vm_offset_t AddrR3;
666 if (R3PtrFixed == (RTR3PTR)-1)
667 {
668 /** @todo: is this needed?. */
669 PROC_LOCK(pProc);
670 AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
671 PROC_UNLOCK(pProc);
672 }
673 else
674 AddrR3 = (vm_offset_t)R3PtrFixed;
675
676 /* Insert the pObject in the map. */
677 vm_object_reference(pMemToMapFreeBSD->pObject);
678 rc = vm_map_find(pProcMap, /* Map to insert the object in */
679 pMemToMapFreeBSD->pObject, /* Object to map */
680 0, /* Start offset in the object */
681 &AddrR3, /* Start address IN/OUT */
682 pMemToMap->cb, /* Size of the mapping */
683 R3PtrFixed == (RTR3PTR)-1 ? VMFS_ANY_SPACE : VMFS_NO_SPACE,
684 /* Whether a suitable address should be searched for first */
685 ProtectionFlags, /* protection flags */
686 VM_PROT_ALL, /* Maximum protection flags */
687 0); /* copy-on-write and similar flags */
688
689 if (rc == KERN_SUCCESS)
690 {
691 rc = vm_map_wire(pProcMap, AddrR3, AddrR3 + pMemToMap->cb, VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
692 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
693
694 rc = vm_map_inherit(pProcMap, AddrR3, AddrR3 + pMemToMap->cb, VM_INHERIT_SHARE);
695 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
696
697 /*
698 * Create a mapping object for it.
699 */
700 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
701 RTR0MEMOBJTYPE_MAPPING,
702 (void *)AddrR3,
703 pMemToMap->cb);
704 if (pMemFreeBSD)
705 {
706 Assert((vm_offset_t)pMemFreeBSD->Core.pv == AddrR3);
707 pMemFreeBSD->Core.u.Mapping.R0Process = R0Process;
708 *ppMem = &pMemFreeBSD->Core;
709 return VINF_SUCCESS;
710 }
711
712 rc = vm_map_remove(pProcMap, AddrR3, AddrR3 + pMemToMap->cb);
713 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
714 }
715 else
716 vm_object_deallocate(pMemToMapFreeBSD->pObject);
717
718 return VERR_NO_MEMORY;
719}
720
721
722DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
723{
724 vm_prot_t ProtectionFlags = 0;
725 vm_offset_t AddrStart = (uintptr_t)pMem->pv + offSub;
726 vm_offset_t AddrEnd = AddrStart + cbSub;
727 vm_map_t pVmMap = rtR0MemObjFreeBSDGetMap(pMem);
728
729 if (!pVmMap)
730 return VERR_NOT_SUPPORTED;
731
732 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
733 ProtectionFlags = VM_PROT_NONE;
734 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
735 ProtectionFlags |= VM_PROT_READ;
736 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
737 ProtectionFlags |= VM_PROT_WRITE;
738 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
739 ProtectionFlags |= VM_PROT_EXECUTE;
740
741 int krc = vm_map_protect(pVmMap, AddrStart, AddrEnd, ProtectionFlags, FALSE);
742 if (krc == KERN_SUCCESS)
743 return VINF_SUCCESS;
744
745 return VERR_NOT_SUPPORTED;
746}
747
748
749DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
750{
751 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
752
753 switch (pMemFreeBSD->Core.enmType)
754 {
755 case RTR0MEMOBJTYPE_LOCK:
756 {
757 if ( pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS
758 && pMemFreeBSD->Core.u.Lock.R0Process != (RTR0PROCESS)curproc)
759 {
760 /* later */
761 return NIL_RTHCPHYS;
762 }
763
764 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + ptoa(iPage);
765
766 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Lock.R0Process;
767 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
768 pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
769
770 return pmap_extract(pPhysicalMap, pb);
771 }
772
773 case RTR0MEMOBJTYPE_MAPPING:
774 {
775 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + ptoa(iPage);
776
777 if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
778 {
779 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Mapping.R0Process;
780 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
781 pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
782
783 return pmap_extract(pPhysicalMap, pb);
784 }
785 return vtophys(pb);
786 }
787
788 case RTR0MEMOBJTYPE_PAGE:
789 case RTR0MEMOBJTYPE_LOW:
790 case RTR0MEMOBJTYPE_PHYS_NC:
791 {
792 RTHCPHYS addr;
793 VM_OBJECT_LOCK(pMemFreeBSD->pObject);
794 addr = VM_PAGE_TO_PHYS(vm_page_lookup(pMemFreeBSD->pObject, iPage));
795 VM_OBJECT_UNLOCK(pMemFreeBSD->pObject);
796 return addr;
797 }
798
799 case RTR0MEMOBJTYPE_PHYS:
800 return pMemFreeBSD->Core.u.Cont.Phys + ptoa(iPage);
801
802 case RTR0MEMOBJTYPE_CONT:
803 return pMemFreeBSD->Core.u.Phys.PhysBase + ptoa(iPage);
804
805 case RTR0MEMOBJTYPE_RES_VIRT:
806 default:
807 return NIL_RTHCPHYS;
808 }
809}
810
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