VirtualBox

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

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

FreeBSD/MemObjR0: Bug fixes (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: 30.2 KB
Line 
1/* $Id: memobj-r0drv-freebsd.c 40916 2012-04-14 07:53:26Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, FreeBSD.
4 */
5
6/*
7 * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
8 * Copyright (c) 2011 Andriy Gapon <avg@FreeBSD.org>
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/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * The FreeBSD version of the memory object structure.
53 */
54typedef struct RTR0MEMOBJFREEBSD
55{
56 /** The core structure. */
57 RTR0MEMOBJINTERNAL Core;
58 /** The VM object associated with the allocation. */
59 vm_object_t pObject;
60} RTR0MEMOBJFREEBSD, *PRTR0MEMOBJFREEBSD;
61
62
63MALLOC_DEFINE(M_IPRTMOBJ, "iprtmobj", "IPRT - R0MemObj");
64
65
66/**
67 * Gets the virtual memory map the specified object is mapped into.
68 *
69 * @returns VM map handle on success, NULL if no map.
70 * @param pMem The memory object.
71 */
72static vm_map_t rtR0MemObjFreeBSDGetMap(PRTR0MEMOBJINTERNAL pMem)
73{
74 switch (pMem->enmType)
75 {
76 case RTR0MEMOBJTYPE_PAGE:
77 case RTR0MEMOBJTYPE_LOW:
78 case RTR0MEMOBJTYPE_CONT:
79 return kernel_map;
80
81 case RTR0MEMOBJTYPE_PHYS:
82 case RTR0MEMOBJTYPE_PHYS_NC:
83 return NULL; /* pretend these have no mapping atm. */
84
85 case RTR0MEMOBJTYPE_LOCK:
86 return pMem->u.Lock.R0Process == NIL_RTR0PROCESS
87 ? kernel_map
88 : &((struct proc *)pMem->u.Lock.R0Process)->p_vmspace->vm_map;
89
90 case RTR0MEMOBJTYPE_RES_VIRT:
91 return pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS
92 ? kernel_map
93 : &((struct proc *)pMem->u.ResVirt.R0Process)->p_vmspace->vm_map;
94
95 case RTR0MEMOBJTYPE_MAPPING:
96 return pMem->u.Mapping.R0Process == NIL_RTR0PROCESS
97 ? kernel_map
98 : &((struct proc *)pMem->u.Mapping.R0Process)->p_vmspace->vm_map;
99
100 default:
101 return NULL;
102 }
103}
104
105
106DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
107{
108 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
109 int rc;
110
111 switch (pMemFreeBSD->Core.enmType)
112 {
113 case RTR0MEMOBJTYPE_PAGE:
114 case RTR0MEMOBJTYPE_LOW:
115 case RTR0MEMOBJTYPE_CONT:
116 rc = vm_map_remove(kernel_map,
117 (vm_offset_t)pMemFreeBSD->Core.pv,
118 (vm_offset_t)pMemFreeBSD->Core.pv + pMemFreeBSD->Core.cb);
119 AssertMsg(rc == KERN_SUCCESS, ("%#x", rc));
120 break;
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 int cTries = 0;
195
196#if __FreeBSD_version > 1000000
197 int fFlags = VM_ALLOC_INTERRUPT | VM_ALLOC_NOBUSY;
198 if (fWire)
199 fFlags |= VM_ALLOC_WIRED;
200
201 while (cTries <= 1)
202 {
203 VM_OBJECT_LOCK(pObject);
204 pPages = vm_page_alloc_contig(pObject, iPIndex, fFlags, cPages, 0,
205 VmPhysAddrHigh, uAlignment, 0, VM_MEMATTR_DEFAULT);
206 VM_OBJECT_UNLOCK(pObject);
207 if (pPages)
208 break;
209 vm_contig_grow_cache(cTries, 0, VmPhysAddrHigh);
210 cTries++;
211 }
212
213 return pPages;
214#else
215 while (cTries <= 1)
216 {
217 pPages = vm_phys_alloc_contig(cPages, 0, VmPhysAddrHigh, uAlignment, 0);
218 if (pPages)
219 break;
220 vm_contig_grow_cache(cTries, 0, VmPhysAddrHigh);
221 cTries++;
222 }
223
224 if (!pPages)
225 return pPages;
226 VM_OBJECT_LOCK(pObject);
227 for (vm_pindex_t iPage = 0; iPage < cPages; iPage++)
228 {
229 vm_page_t pPage = pPages + iPage;
230 vm_page_insert(pPage, pObject, iPIndex + iPage);
231 pPage->valid = VM_PAGE_BITS_ALL;
232 if (fWire)
233 {
234 pPage->wire_count = 1;
235 atomic_add_int(&cnt.v_wire_count, 1);
236 }
237 }
238 VM_OBJECT_UNLOCK(pObject);
239 return pPages;
240#endif
241}
242
243static int rtR0MemObjFreeBSDPhysAllocHelper(vm_object_t pObject, u_long cPages,
244 vm_paddr_t VmPhysAddrHigh, u_long uAlignment,
245 bool fContiguous, bool fWire, int rcNoMem)
246{
247 if (fContiguous)
248 {
249 if (rtR0MemObjFreeBSDContigPhysAllocHelper(pObject, 0, cPages, VmPhysAddrHigh,
250 uAlignment, fWire) != NULL)
251 return VINF_SUCCESS;
252 return rcNoMem;
253 }
254
255 for (vm_pindex_t iPage = 0; iPage < cPages; iPage++)
256 {
257 vm_page_t pPage = rtR0MemObjFreeBSDContigPhysAllocHelper(pObject, iPage, 1, VmPhysAddrHigh,
258 uAlignment, fWire);
259 if (!pPage)
260 {
261 /* Free all allocated pages */
262 VM_OBJECT_LOCK(pObject);
263 while (iPage-- > 0)
264 {
265 pPage = vm_page_lookup(pObject, iPage);
266 vm_page_lock_queues();
267 if (fWire)
268 vm_page_unwire(pPage, 0);
269 vm_page_free(pPage);
270 vm_page_unlock_queues();
271 }
272 VM_OBJECT_UNLOCK(pObject);
273 return rcNoMem;
274 }
275 }
276 return VINF_SUCCESS;
277}
278
279static int rtR0MemObjFreeBSDAllocHelper(PRTR0MEMOBJFREEBSD pMemFreeBSD, bool fExecutable,
280 vm_paddr_t VmPhysAddrHigh, bool fContiguous, int rcNoMem)
281{
282 vm_offset_t MapAddress = vm_map_min(kernel_map);
283 size_t cPages = atop(pMemFreeBSD->Core.cb);
284 int rc;
285
286 pMemFreeBSD->pObject = vm_object_allocate(OBJT_PHYS, cPages);
287
288 /* No additional object reference for auto-deallocation upon unmapping. */
289 rc = vm_map_find(kernel_map, pMemFreeBSD->pObject, 0,
290 &MapAddress, pMemFreeBSD->Core.cb, VMFS_ANY_SPACE,
291 fExecutable ? VM_PROT_ALL : VM_PROT_RW, VM_PROT_ALL, 0);
292
293 if (rc == KERN_SUCCESS)
294 {
295 rc = rtR0MemObjFreeBSDPhysAllocHelper(pMemFreeBSD->pObject, cPages,
296 VmPhysAddrHigh, PAGE_SIZE, fContiguous,
297 false, rcNoMem);
298 if (RT_SUCCESS(rc))
299 {
300 vm_map_wire(kernel_map, MapAddress, MapAddress + pMemFreeBSD->Core.cb,
301 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
302
303 /* Store start address */
304 pMemFreeBSD->Core.pv = (void *)MapAddress;
305 return VINF_SUCCESS;
306 }
307
308 vm_map_remove(kernel_map, MapAddress, MapAddress + pMemFreeBSD->Core.cb);
309 }
310 else
311 {
312 rc = rcNoMem; /** @todo fix translation (borrow from darwin) */
313 vm_object_deallocate(pMemFreeBSD->pObject);
314 }
315
316 rtR0MemObjDelete(&pMemFreeBSD->Core);
317 return rc;
318}
319DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
320{
321 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
322 RTR0MEMOBJTYPE_PAGE, NULL, cb);
323 if (!pMemFreeBSD)
324 return VERR_NO_MEMORY;
325
326 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, ~(vm_paddr_t)0, false, VERR_NO_MEMORY);
327 if (RT_FAILURE(rc))
328 {
329 rtR0MemObjDelete(&pMemFreeBSD->Core);
330 return rc;
331 }
332
333 *ppMem = &pMemFreeBSD->Core;
334 return rc;
335}
336
337
338DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
339{
340 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
341 RTR0MEMOBJTYPE_LOW, NULL, cb);
342 if (!pMemFreeBSD)
343 return VERR_NO_MEMORY;
344
345 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, _4G - 1, false, VERR_NO_LOW_MEMORY);
346 if (RT_FAILURE(rc))
347 {
348 rtR0MemObjDelete(&pMemFreeBSD->Core);
349 return rc;
350 }
351
352 *ppMem = &pMemFreeBSD->Core;
353 return rc;
354}
355
356
357DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
358{
359 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
360 RTR0MEMOBJTYPE_CONT, NULL, cb);
361 if (!pMemFreeBSD)
362 return VERR_NO_MEMORY;
363
364 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, _4G - 1, true, VERR_NO_CONT_MEMORY);
365 if (RT_FAILURE(rc))
366 {
367 rtR0MemObjDelete(&pMemFreeBSD->Core);
368 return rc;
369 }
370
371 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
372 *ppMem = &pMemFreeBSD->Core;
373 return rc;
374}
375
376
377static int rtR0MemObjFreeBSDAllocPhysPages(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType,
378 size_t cb,
379 RTHCPHYS PhysHighest, size_t uAlignment,
380 bool fContiguous, int rcNoMem)
381{
382 uint32_t cPages = atop(cb);
383 vm_paddr_t VmPhysAddrHigh;
384
385 /* create the object. */
386 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD),
387 enmType, NULL, cb);
388 if (!pMemFreeBSD)
389 return VERR_NO_MEMORY;
390
391 pMemFreeBSD->pObject = vm_object_allocate(OBJT_PHYS, atop(cb));
392
393 if (PhysHighest != NIL_RTHCPHYS)
394 VmPhysAddrHigh = PhysHighest;
395 else
396 VmPhysAddrHigh = ~(vm_paddr_t)0;
397
398 int rc = rtR0MemObjFreeBSDPhysAllocHelper(pMemFreeBSD->pObject, cPages, VmPhysAddrHigh,
399 uAlignment, fContiguous, true, rcNoMem);
400 if (RT_SUCCESS(rc))
401 {
402 if (fContiguous)
403 {
404 Assert(enmType == RTR0MEMOBJTYPE_PHYS);
405 VM_OBJECT_LOCK(pMemFreeBSD->pObject);
406 pMemFreeBSD->Core.u.Phys.PhysBase = VM_PAGE_TO_PHYS(vm_page_find_least(pMemFreeBSD->pObject, 0));
407 VM_OBJECT_UNLOCK(pMemFreeBSD->pObject);
408 pMemFreeBSD->Core.u.Phys.fAllocated = true;
409 }
410
411 *ppMem = &pMemFreeBSD->Core;
412 }
413 else
414 {
415 vm_object_deallocate(pMemFreeBSD->pObject);
416 rtR0MemObjDelete(&pMemFreeBSD->Core);
417 }
418
419 return rc;
420}
421
422
423DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
424{
425 return rtR0MemObjFreeBSDAllocPhysPages(ppMem, RTR0MEMOBJTYPE_PHYS, cb, PhysHighest, uAlignment, true, VERR_NO_MEMORY);
426}
427
428
429DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
430{
431 return rtR0MemObjFreeBSDAllocPhysPages(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PhysHighest, PAGE_SIZE, false, VERR_NO_PHYS_MEMORY);
432}
433
434
435DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
436{
437 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
438
439 /* create the object. */
440 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS, NULL, cb);
441 if (!pMemFreeBSD)
442 return VERR_NO_MEMORY;
443
444 /* there is no allocation here, it needs to be mapped somewhere first. */
445 pMemFreeBSD->Core.u.Phys.fAllocated = false;
446 pMemFreeBSD->Core.u.Phys.PhysBase = Phys;
447 pMemFreeBSD->Core.u.Phys.uCachePolicy = uCachePolicy;
448 *ppMem = &pMemFreeBSD->Core;
449 return VINF_SUCCESS;
450}
451
452
453/**
454 * Worker locking the memory in either kernel or user maps.
455 */
456static int rtR0MemObjNativeLockInMap(PPRTR0MEMOBJINTERNAL ppMem, vm_map_t pVmMap,
457 vm_offset_t AddrStart, size_t cb, uint32_t fAccess,
458 RTR0PROCESS R0Process, int fFlags)
459{
460 int rc;
461 NOREF(fAccess);
462
463 /* create the object. */
464 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, (void *)AddrStart, cb);
465 if (!pMemFreeBSD)
466 return VERR_NO_MEMORY;
467
468 /*
469 * We could've used vslock here, but we don't wish to be subject to
470 * resource usage restrictions, so we'll call vm_map_wire directly.
471 */
472 rc = vm_map_wire(pVmMap, /* the map */
473 AddrStart, /* start */
474 AddrStart + cb, /* end */
475 fFlags); /* flags */
476 if (rc == KERN_SUCCESS)
477 {
478 pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
479 *ppMem = &pMemFreeBSD->Core;
480 return VINF_SUCCESS;
481 }
482 rtR0MemObjDelete(&pMemFreeBSD->Core);
483 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
484}
485
486
487DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
488{
489 return rtR0MemObjNativeLockInMap(ppMem,
490 &((struct proc *)R0Process)->p_vmspace->vm_map,
491 (vm_offset_t)R3Ptr,
492 cb,
493 fAccess,
494 R0Process,
495 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
496}
497
498
499DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
500{
501 return rtR0MemObjNativeLockInMap(ppMem,
502 kernel_map,
503 (vm_offset_t)pv,
504 cb,
505 fAccess,
506 NIL_RTR0PROCESS,
507 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
508}
509
510
511/**
512 * Worker for the two virtual address space reservers.
513 *
514 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
515 */
516static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process, vm_map_t pMap)
517{
518 int rc;
519
520 /*
521 * The pvFixed address range must be within the VM space when specified.
522 */
523 if ( pvFixed != (void *)-1
524 && ( (vm_offset_t)pvFixed < vm_map_min(pMap)
525 || (vm_offset_t)pvFixed + cb > vm_map_max(pMap)))
526 return VERR_INVALID_PARAMETER;
527
528 /*
529 * Check that the specified alignment is supported.
530 */
531 if (uAlignment > PAGE_SIZE)
532 return VERR_NOT_SUPPORTED;
533
534 /*
535 * Create the object.
536 */
537 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_RES_VIRT, NULL, cb);
538 if (!pMemFreeBSD)
539 return VERR_NO_MEMORY;
540
541 vm_offset_t MapAddress = pvFixed != (void *)-1
542 ? (vm_offset_t)pvFixed
543 : vm_map_min(pMap);
544 if (pvFixed != (void *)-1)
545 vm_map_remove(pMap,
546 MapAddress,
547 MapAddress + cb);
548
549 rc = vm_map_find(pMap, /* map */
550 NULL, /* object */
551 0, /* offset */
552 &MapAddress, /* addr (IN/OUT) */
553 cb, /* length */
554 pvFixed == (void *)-1 ? VMFS_ANY_SPACE : VMFS_NO_SPACE,
555 /* find_space */
556 VM_PROT_NONE, /* protection */
557 VM_PROT_ALL, /* max(_prot) ?? */
558 0); /* cow (copy-on-write) */
559 if (rc == KERN_SUCCESS)
560 {
561 if (R0Process != NIL_RTR0PROCESS)
562 {
563 rc = vm_map_inherit(pMap,
564 MapAddress,
565 MapAddress + cb,
566 VM_INHERIT_SHARE);
567 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
568 }
569 pMemFreeBSD->Core.pv = (void *)MapAddress;
570 pMemFreeBSD->Core.u.ResVirt.R0Process = R0Process;
571 *ppMem = &pMemFreeBSD->Core;
572 return VINF_SUCCESS;
573 }
574
575 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
576 rtR0MemObjDelete(&pMemFreeBSD->Core);
577 return rc;
578
579}
580
581
582DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
583{
584 return rtR0MemObjNativeReserveInMap(ppMem, pvFixed, cb, uAlignment, NIL_RTR0PROCESS, kernel_map);
585}
586
587
588DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
589{
590 return rtR0MemObjNativeReserveInMap(ppMem, (void *)R3PtrFixed, cb, uAlignment, R0Process,
591 &((struct proc *)R0Process)->p_vmspace->vm_map);
592}
593
594
595DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
596 unsigned fProt, size_t offSub, size_t cbSub)
597{
598// AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
599 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
600
601 /*
602 * Check that the specified alignment is supported.
603 */
604 if (uAlignment > PAGE_SIZE)
605 return VERR_NOT_SUPPORTED;
606
607 int rc;
608 PRTR0MEMOBJFREEBSD pMemToMapFreeBSD = (PRTR0MEMOBJFREEBSD)pMemToMap;
609
610 /* calc protection */
611 vm_prot_t ProtectionFlags = 0;
612 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
613 ProtectionFlags = VM_PROT_NONE;
614 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
615 ProtectionFlags |= VM_PROT_READ;
616 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
617 ProtectionFlags |= VM_PROT_WRITE;
618 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
619 ProtectionFlags |= VM_PROT_EXECUTE;
620
621 vm_offset_t Addr = vm_map_min(kernel_map);
622 if (cbSub == 0)
623 cbSub = pMemToMap->cb - offSub;
624
625 vm_object_reference(pMemToMapFreeBSD->pObject);
626 rc = vm_map_find(kernel_map, /* Map to insert the object in */
627 pMemToMapFreeBSD->pObject, /* Object to map */
628 offSub, /* Start offset in the object */
629 &Addr, /* Start address IN/OUT */
630 cbSub, /* Size of the mapping */
631 VMFS_ANY_SPACE, /* Whether a suitable address should be searched for first */
632 ProtectionFlags, /* protection flags */
633 VM_PROT_ALL, /* Maximum protection flags */
634 0); /* copy-on-write and similar flags */
635
636 if (rc == KERN_SUCCESS)
637 {
638 rc = vm_map_wire(kernel_map, Addr, Addr + cbSub, VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES);
639 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
640
641 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
642 RTR0MEMOBJTYPE_MAPPING,
643 (void *)Addr,
644 cbSub);
645 if (pMemFreeBSD)
646 {
647 Assert((vm_offset_t)pMemFreeBSD->Core.pv == Addr);
648 pMemFreeBSD->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
649 *ppMem = &pMemFreeBSD->Core;
650 return VINF_SUCCESS;
651 }
652 rc = vm_map_remove(kernel_map, Addr, Addr + cbSub);
653 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
654 }
655 else
656 vm_object_deallocate(pMemToMapFreeBSD->pObject);
657
658 return VERR_NO_MEMORY;
659}
660
661
662DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment,
663 unsigned fProt, RTR0PROCESS R0Process)
664{
665 /*
666 * Check for unsupported stuff.
667 */
668 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
669 if (uAlignment > PAGE_SIZE)
670 return VERR_NOT_SUPPORTED;
671
672 int rc;
673 PRTR0MEMOBJFREEBSD pMemToMapFreeBSD = (PRTR0MEMOBJFREEBSD)pMemToMap;
674 struct proc *pProc = (struct proc *)R0Process;
675 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
676
677 /* calc protection */
678 vm_prot_t ProtectionFlags = 0;
679 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
680 ProtectionFlags = VM_PROT_NONE;
681 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
682 ProtectionFlags |= VM_PROT_READ;
683 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
684 ProtectionFlags |= VM_PROT_WRITE;
685 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
686 ProtectionFlags |= VM_PROT_EXECUTE;
687
688 /* calc mapping address */
689 vm_offset_t AddrR3;
690 if (R3PtrFixed == (RTR3PTR)-1)
691 {
692 /** @todo: is this needed?. */
693 PROC_LOCK(pProc);
694 AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
695 PROC_UNLOCK(pProc);
696 }
697 else
698 AddrR3 = (vm_offset_t)R3PtrFixed;
699
700 /* Insert the pObject in the map. */
701 vm_object_reference(pMemToMapFreeBSD->pObject);
702 rc = vm_map_find(pProcMap, /* Map to insert the object in */
703 pMemToMapFreeBSD->pObject, /* Object to map */
704 0, /* Start offset in the object */
705 &AddrR3, /* Start address IN/OUT */
706 pMemToMap->cb, /* Size of the mapping */
707 R3PtrFixed == (RTR3PTR)-1 ? VMFS_ANY_SPACE : VMFS_NO_SPACE,
708 /* Whether a suitable address should be searched for first */
709 ProtectionFlags, /* protection flags */
710 VM_PROT_ALL, /* Maximum protection flags */
711 0); /* copy-on-write and similar flags */
712
713 if (rc == KERN_SUCCESS)
714 {
715 rc = vm_map_wire(pProcMap, AddrR3, AddrR3 + pMemToMap->cb, VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
716 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
717
718 rc = vm_map_inherit(pProcMap, AddrR3, AddrR3 + pMemToMap->cb, VM_INHERIT_SHARE);
719 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
720
721 /*
722 * Create a mapping object for it.
723 */
724 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
725 RTR0MEMOBJTYPE_MAPPING,
726 (void *)AddrR3,
727 pMemToMap->cb);
728 if (pMemFreeBSD)
729 {
730 Assert((vm_offset_t)pMemFreeBSD->Core.pv == AddrR3);
731 pMemFreeBSD->Core.u.Mapping.R0Process = R0Process;
732 *ppMem = &pMemFreeBSD->Core;
733 return VINF_SUCCESS;
734 }
735
736 rc = vm_map_remove(pProcMap, AddrR3, AddrR3 + pMemToMap->cb);
737 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
738 }
739 else
740 vm_object_deallocate(pMemToMapFreeBSD->pObject);
741
742 return VERR_NO_MEMORY;
743}
744
745
746DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
747{
748 vm_prot_t ProtectionFlags = 0;
749 vm_offset_t AddrStart = (uintptr_t)pMem->pv + offSub;
750 vm_offset_t AddrEnd = AddrStart + cbSub;
751 vm_map_t pVmMap = rtR0MemObjFreeBSDGetMap(pMem);
752
753 if (!pVmMap)
754 return VERR_NOT_SUPPORTED;
755
756 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
757 ProtectionFlags = VM_PROT_NONE;
758 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
759 ProtectionFlags |= VM_PROT_READ;
760 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
761 ProtectionFlags |= VM_PROT_WRITE;
762 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
763 ProtectionFlags |= VM_PROT_EXECUTE;
764
765 int krc = vm_map_protect(pVmMap, AddrStart, AddrEnd, ProtectionFlags, FALSE);
766 if (krc == KERN_SUCCESS)
767 return VINF_SUCCESS;
768
769 return VERR_NOT_SUPPORTED;
770}
771
772
773DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
774{
775 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
776
777 switch (pMemFreeBSD->Core.enmType)
778 {
779 case RTR0MEMOBJTYPE_LOCK:
780 {
781 if ( pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS
782 && pMemFreeBSD->Core.u.Lock.R0Process != (RTR0PROCESS)curproc)
783 {
784 /* later */
785 return NIL_RTHCPHYS;
786 }
787
788 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + ptoa(iPage);
789
790 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Lock.R0Process;
791 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
792 pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
793
794 return pmap_extract(pPhysicalMap, pb);
795 }
796
797 case RTR0MEMOBJTYPE_MAPPING:
798 {
799 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + ptoa(iPage);
800
801 if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
802 {
803 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Mapping.R0Process;
804 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
805 pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
806
807 return pmap_extract(pPhysicalMap, pb);
808 }
809 return vtophys(pb);
810 }
811
812 case RTR0MEMOBJTYPE_PAGE:
813 case RTR0MEMOBJTYPE_LOW:
814 case RTR0MEMOBJTYPE_PHYS_NC:
815 {
816 RTHCPHYS addr;
817 VM_OBJECT_LOCK(pMemFreeBSD->pObject);
818 addr = VM_PAGE_TO_PHYS(vm_page_lookup(pMemFreeBSD->pObject, iPage));
819 VM_OBJECT_UNLOCK(pMemFreeBSD->pObject);
820 return addr;
821 }
822
823 case RTR0MEMOBJTYPE_PHYS:
824 return pMemFreeBSD->Core.u.Cont.Phys + ptoa(iPage);
825
826 case RTR0MEMOBJTYPE_CONT:
827 return pMemFreeBSD->Core.u.Phys.PhysBase + ptoa(iPage);
828
829 case RTR0MEMOBJTYPE_RES_VIRT:
830 default:
831 return NIL_RTHCPHYS;
832 }
833}
834
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