VirtualBox

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

Last change on this file since 91446 was 91446, checked in by vboxsync, 3 years ago

IPRT/memobj: Adding RTR0MemObjAllocLarge for speeding up large page allocations. bugref:5324

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