VirtualBox

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

Last change on this file since 53402 was 49718, checked in by vboxsync, 11 years ago

Various FreeBSD fixes submitted Bernhard Froehlich

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