VirtualBox

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

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

IPRT/memobj: Passing pszTag around...

  • 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 91481 2021-09-30 00:06:31Z 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), RTR0MEMOBJTYPE_PAGE, NULL, cb, NULL);
367 if (!pMemFreeBSD)
368 return VERR_NO_MEMORY;
369
370 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, ~(vm_paddr_t)0, false, VERR_NO_MEMORY);
371 if (RT_FAILURE(rc))
372 {
373 rtR0MemObjDelete(&pMemFreeBSD->Core);
374 return rc;
375 }
376
377 *ppMem = &pMemFreeBSD->Core;
378 return rc;
379}
380
381
382DECLHIDDEN(int) rtR0MemObjNativeAllocLarge(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, size_t cbLargePage, uint32_t fFlags,
383 const char *pszTag)
384{
385 return rtR0MemObjFallbackAllocLarge(ppMem, cb, cbLargePage, fFlags, pszTag);
386}
387
388
389DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
390{
391 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOW, NULL, cb, NULL);
392 if (!pMemFreeBSD)
393 return VERR_NO_MEMORY;
394
395 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, _4G - 1, false, VERR_NO_LOW_MEMORY);
396 if (RT_FAILURE(rc))
397 {
398 rtR0MemObjDelete(&pMemFreeBSD->Core);
399 return rc;
400 }
401
402 *ppMem = &pMemFreeBSD->Core;
403 return rc;
404}
405
406
407DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
408{
409 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_CONT, NULL, cb, NULL);
410 if (!pMemFreeBSD)
411 return VERR_NO_MEMORY;
412
413 int rc = rtR0MemObjFreeBSDAllocHelper(pMemFreeBSD, fExecutable, _4G - 1, true, VERR_NO_CONT_MEMORY);
414 if (RT_FAILURE(rc))
415 {
416 rtR0MemObjDelete(&pMemFreeBSD->Core);
417 return rc;
418 }
419
420 pMemFreeBSD->Core.u.Cont.Phys = vtophys(pMemFreeBSD->Core.pv);
421 *ppMem = &pMemFreeBSD->Core;
422 return rc;
423}
424
425
426static int rtR0MemObjFreeBSDAllocPhysPages(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJTYPE enmType, size_t cb, RTHCPHYS PhysHighest,
427 size_t uAlignment, bool fContiguous, int rcNoMem, const char *pszTag)
428{
429 uint32_t cPages = atop(cb);
430 vm_paddr_t VmPhysAddrHigh;
431
432 /* create the object. */
433 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), enmType, NULL, cb, pszTag);
434 if (!pMemFreeBSD)
435 return VERR_NO_MEMORY;
436
437 pMemFreeBSD->pObject = vm_object_allocate(OBJT_PHYS, atop(cb));
438
439 if (PhysHighest != NIL_RTHCPHYS)
440 VmPhysAddrHigh = PhysHighest;
441 else
442 VmPhysAddrHigh = ~(vm_paddr_t)0;
443
444 int rc = rtR0MemObjFreeBSDPhysAllocHelper(pMemFreeBSD->pObject, cPages, VmPhysAddrHigh,
445 uAlignment, fContiguous, true, rcNoMem);
446 if (RT_SUCCESS(rc))
447 {
448 if (fContiguous)
449 {
450 Assert(enmType == RTR0MEMOBJTYPE_PHYS);
451 VM_OBJECT_WLOCK(pMemFreeBSD->pObject);
452 pMemFreeBSD->Core.u.Phys.PhysBase = VM_PAGE_TO_PHYS(vm_page_find_least(pMemFreeBSD->pObject, 0));
453 VM_OBJECT_WUNLOCK(pMemFreeBSD->pObject);
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 const char *pszTag)
471{
472 return rtR0MemObjFreeBSDAllocPhysPages(ppMem, RTR0MEMOBJTYPE_PHYS, cb, PhysHighest, uAlignment, true, VERR_NO_MEMORY, pszTag);
473}
474
475
476DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, const char *pszTag)
477{
478 return rtR0MemObjFreeBSDAllocPhysPages(ppMem, RTR0MEMOBJTYPE_PHYS_NC, cb, PhysHighest, PAGE_SIZE, false,
479 VERR_NO_PHYS_MEMORY, pszTag);
480}
481
482
483DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy,
484 const char *pszTag)
485{
486 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
487
488 /* create the object. */
489 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS,
490 NULL, cb, pszTag);
491 if (!pMemFreeBSD)
492 return VERR_NO_MEMORY;
493
494 /* there is no allocation here, it needs to be mapped somewhere first. */
495 pMemFreeBSD->Core.u.Phys.fAllocated = false;
496 pMemFreeBSD->Core.u.Phys.PhysBase = Phys;
497 pMemFreeBSD->Core.u.Phys.uCachePolicy = uCachePolicy;
498 *ppMem = &pMemFreeBSD->Core;
499 return VINF_SUCCESS;
500}
501
502
503/**
504 * Worker locking the memory in either kernel or user maps.
505 */
506static int rtR0MemObjNativeLockInMap(PPRTR0MEMOBJINTERNAL ppMem, vm_map_t pVmMap,
507 vm_offset_t AddrStart, size_t cb, uint32_t fAccess,
508 RTR0PROCESS R0Process, int fFlags)
509{
510 int rc;
511 NOREF(fAccess);
512
513 /* create the object. */
514 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK,
515 (void *)AddrStart, cb, NULL);
516 if (!pMemFreeBSD)
517 return VERR_NO_MEMORY;
518
519 /*
520 * We could've used vslock here, but we don't wish to be subject to
521 * resource usage restrictions, so we'll call vm_map_wire directly.
522 */
523 rc = vm_map_wire(pVmMap, /* the map */
524 AddrStart, /* start */
525 AddrStart + cb, /* end */
526 fFlags); /* flags */
527 if (rc == KERN_SUCCESS)
528 {
529 pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
530 *ppMem = &pMemFreeBSD->Core;
531 return VINF_SUCCESS;
532 }
533 rtR0MemObjDelete(&pMemFreeBSD->Core);
534 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
535}
536
537
538DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
539{
540 return rtR0MemObjNativeLockInMap(ppMem,
541 &((struct proc *)R0Process)->p_vmspace->vm_map,
542 (vm_offset_t)R3Ptr,
543 cb,
544 fAccess,
545 R0Process,
546 VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
547}
548
549
550DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
551{
552 return rtR0MemObjNativeLockInMap(ppMem,
553 kernel_map,
554 (vm_offset_t)pv,
555 cb,
556 fAccess,
557 NIL_RTR0PROCESS,
558 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
559}
560
561
562/**
563 * Worker for the two virtual address space reservers.
564 *
565 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
566 */
567static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment,
568 RTR0PROCESS R0Process, vm_map_t pMap, const char *pszTag)
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,
590 NULL, cb, pszTag);
591 if (!pMemFreeBSD)
592 return VERR_NO_MEMORY;
593
594 vm_offset_t MapAddress = pvFixed != (void *)-1
595 ? (vm_offset_t)pvFixed
596 : vm_map_min(pMap);
597 if (pvFixed != (void *)-1)
598 vm_map_remove(pMap,
599 MapAddress,
600 MapAddress + cb);
601
602 rc = vm_map_find(pMap, /* map */
603 NULL, /* object */
604 0, /* offset */
605 &MapAddress, /* addr (IN/OUT) */
606 cb, /* length */
607#if __FreeBSD_version >= 1000055
608 0, /* max addr */
609#endif
610 pvFixed == (void *)-1 ? VMFS_ANY_SPACE : VMFS_NO_SPACE,
611 /* find_space */
612 VM_PROT_NONE, /* protection */
613 VM_PROT_ALL, /* max(_prot) ?? */
614 0); /* cow (copy-on-write) */
615 if (rc == KERN_SUCCESS)
616 {
617 if (R0Process != NIL_RTR0PROCESS)
618 {
619 rc = vm_map_inherit(pMap,
620 MapAddress,
621 MapAddress + cb,
622 VM_INHERIT_SHARE);
623 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
624 }
625 pMemFreeBSD->Core.pv = (void *)MapAddress;
626 pMemFreeBSD->Core.u.ResVirt.R0Process = R0Process;
627 *ppMem = &pMemFreeBSD->Core;
628 return VINF_SUCCESS;
629 }
630
631 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
632 rtR0MemObjDelete(&pMemFreeBSD->Core);
633 return rc;
634
635}
636
637
638DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment,
639 const char *pszTag)
640{
641 return rtR0MemObjNativeReserveInMap(ppMem, pvFixed, cb, uAlignment, NIL_RTR0PROCESS, kernel_map, pszTag);
642}
643
644
645DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment,
646 RTR0PROCESS R0Process, const char *pszTag)
647{
648 return rtR0MemObjNativeReserveInMap(ppMem, (void *)R3PtrFixed, cb, uAlignment, R0Process,
649 &((struct proc *)R0Process)->p_vmspace->vm_map, pszTag);
650}
651
652
653DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
654 unsigned fProt, size_t offSub, size_t cbSub, const char *pszTag)
655{
656// AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
657 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
658
659 /*
660 * Check that the specified alignment is supported.
661 */
662 if (uAlignment > PAGE_SIZE)
663 return VERR_NOT_SUPPORTED;
664 Assert(!offSub || cbSub);
665
666 int rc;
667 PRTR0MEMOBJFREEBSD pMemToMapFreeBSD = (PRTR0MEMOBJFREEBSD)pMemToMap;
668
669 /* calc protection */
670 vm_prot_t ProtectionFlags = 0;
671 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
672 ProtectionFlags = VM_PROT_NONE;
673 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
674 ProtectionFlags |= VM_PROT_READ;
675 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
676 ProtectionFlags |= VM_PROT_WRITE;
677 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
678 ProtectionFlags |= VM_PROT_EXECUTE;
679
680 vm_offset_t Addr = vm_map_min(kernel_map);
681 if (cbSub == 0)
682 cbSub = pMemToMap->cb - offSub;
683
684 vm_object_reference(pMemToMapFreeBSD->pObject);
685 rc = vm_map_find(kernel_map, /* Map to insert the object in */
686 pMemToMapFreeBSD->pObject, /* Object to map */
687 offSub, /* Start offset in the object */
688 &Addr, /* Start address IN/OUT */
689 cbSub, /* Size of the mapping */
690#if __FreeBSD_version >= 1000055
691 0, /* Upper bound of mapping */
692#endif
693 VMFS_ANY_SPACE, /* Whether a suitable address should be searched for first */
694 ProtectionFlags, /* protection flags */
695 VM_PROT_ALL, /* Maximum protection flags */
696 0); /* copy-on-write and similar flags */
697
698 if (rc == KERN_SUCCESS)
699 {
700 rc = vm_map_wire(kernel_map, Addr, Addr + cbSub, VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
701 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
702
703 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD), RTR0MEMOBJTYPE_MAPPING,
704 (void *)Addr, cbSub, pszTag);
705 if (pMemFreeBSD)
706 {
707 Assert((vm_offset_t)pMemFreeBSD->Core.pv == Addr);
708 pMemFreeBSD->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
709 *ppMem = &pMemFreeBSD->Core;
710 return VINF_SUCCESS;
711 }
712 rc = vm_map_remove(kernel_map, Addr, Addr + cbSub);
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) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment,
723 unsigned fProt, RTR0PROCESS R0Process, size_t offSub, size_t cbSub, const char *pszTag)
724{
725 /*
726 * Check for unsupported stuff.
727 */
728 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
729 if (uAlignment > PAGE_SIZE)
730 return VERR_NOT_SUPPORTED;
731 Assert(!offSub || cbSub);
732
733 int rc;
734 PRTR0MEMOBJFREEBSD pMemToMapFreeBSD = (PRTR0MEMOBJFREEBSD)pMemToMap;
735 struct proc *pProc = (struct proc *)R0Process;
736 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
737
738 /* calc protection */
739 vm_prot_t ProtectionFlags = 0;
740 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
741 ProtectionFlags = VM_PROT_NONE;
742 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
743 ProtectionFlags |= VM_PROT_READ;
744 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
745 ProtectionFlags |= VM_PROT_WRITE;
746 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
747 ProtectionFlags |= VM_PROT_EXECUTE;
748
749 /* calc mapping address */
750 vm_offset_t AddrR3;
751 if (R3PtrFixed == (RTR3PTR)-1)
752 {
753 /** @todo is this needed?. */
754 PROC_LOCK(pProc);
755 AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + MY_LIM_MAX_PROC(pProc, RLIMIT_DATA));
756 PROC_UNLOCK(pProc);
757 }
758 else
759 AddrR3 = (vm_offset_t)R3PtrFixed;
760
761 if (cbSub == 0)
762 cbSub = pMemToMap->cb - offSub;
763
764 /* Insert the pObject in the map. */
765 vm_object_reference(pMemToMapFreeBSD->pObject);
766 rc = vm_map_find(pProcMap, /* Map to insert the object in */
767 pMemToMapFreeBSD->pObject, /* Object to map */
768 offSub, /* Start offset in the object */
769 &AddrR3, /* Start address IN/OUT */
770 cbSub, /* Size of the mapping */
771#if __FreeBSD_version >= 1000055
772 0, /* Upper bound of the mapping */
773#endif
774 R3PtrFixed == (RTR3PTR)-1 ? VMFS_ANY_SPACE : VMFS_NO_SPACE,
775 /* Whether a suitable address should be searched for first */
776 ProtectionFlags, /* protection flags */
777 VM_PROT_ALL, /* Maximum protection flags */
778 0); /* copy-on-write and similar flags */
779
780 if (rc == KERN_SUCCESS)
781 {
782 rc = vm_map_wire(pProcMap, AddrR3, AddrR3 + pMemToMap->cb, VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
783 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
784
785 rc = vm_map_inherit(pProcMap, AddrR3, AddrR3 + pMemToMap->cb, VM_INHERIT_SHARE);
786 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
787
788 /*
789 * Create a mapping object for it.
790 */
791 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD), RTR0MEMOBJTYPE_MAPPING,
792 (void *)AddrR3, pMemToMap->cb, pszTag);
793 if (pMemFreeBSD)
794 {
795 Assert((vm_offset_t)pMemFreeBSD->Core.pv == AddrR3);
796 pMemFreeBSD->Core.u.Mapping.R0Process = R0Process;
797 *ppMem = &pMemFreeBSD->Core;
798 return VINF_SUCCESS;
799 }
800
801 rc = vm_map_remove(pProcMap, AddrR3, AddrR3 + pMemToMap->cb);
802 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
803 }
804 else
805 vm_object_deallocate(pMemToMapFreeBSD->pObject);
806
807 return VERR_NO_MEMORY;
808}
809
810
811DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
812{
813 vm_prot_t ProtectionFlags = 0;
814 vm_offset_t AddrStart = (uintptr_t)pMem->pv + offSub;
815 vm_offset_t AddrEnd = AddrStart + cbSub;
816 vm_map_t pVmMap = rtR0MemObjFreeBSDGetMap(pMem);
817
818 if (!pVmMap)
819 return VERR_NOT_SUPPORTED;
820
821 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
822 ProtectionFlags = VM_PROT_NONE;
823 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
824 ProtectionFlags |= VM_PROT_READ;
825 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
826 ProtectionFlags |= VM_PROT_WRITE;
827 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
828 ProtectionFlags |= VM_PROT_EXECUTE;
829
830 int krc = vm_map_protect(pVmMap, AddrStart, AddrEnd, ProtectionFlags, FALSE);
831 if (krc == KERN_SUCCESS)
832 return VINF_SUCCESS;
833
834 return VERR_NOT_SUPPORTED;
835}
836
837
838DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
839{
840 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
841
842 switch (pMemFreeBSD->Core.enmType)
843 {
844 case RTR0MEMOBJTYPE_LOCK:
845 {
846 if ( pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS
847 && pMemFreeBSD->Core.u.Lock.R0Process != (RTR0PROCESS)curproc)
848 {
849 /* later */
850 return NIL_RTHCPHYS;
851 }
852
853 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + ptoa(iPage);
854
855 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Lock.R0Process;
856 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
857 pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
858
859 return pmap_extract(pPhysicalMap, pb);
860 }
861
862 case RTR0MEMOBJTYPE_MAPPING:
863 {
864 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + ptoa(iPage);
865
866 if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
867 {
868 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Mapping.R0Process;
869 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
870 pmap_t pPhysicalMap = vm_map_pmap(pProcMap);
871
872 return pmap_extract(pPhysicalMap, pb);
873 }
874 return vtophys(pb);
875 }
876
877 case RTR0MEMOBJTYPE_PAGE:
878 case RTR0MEMOBJTYPE_LOW:
879 case RTR0MEMOBJTYPE_PHYS_NC:
880 {
881 RTHCPHYS addr;
882
883 VM_OBJECT_WLOCK(pMemFreeBSD->pObject);
884 addr = VM_PAGE_TO_PHYS(vm_page_lookup(pMemFreeBSD->pObject, iPage));
885 VM_OBJECT_WUNLOCK(pMemFreeBSD->pObject);
886 return addr;
887 }
888
889 case RTR0MEMOBJTYPE_PHYS:
890 return pMemFreeBSD->Core.u.Cont.Phys + ptoa(iPage);
891
892 case RTR0MEMOBJTYPE_CONT:
893 return pMemFreeBSD->Core.u.Phys.PhysBase + ptoa(iPage);
894
895 case RTR0MEMOBJTYPE_RES_VIRT:
896 default:
897 return NIL_RTHCPHYS;
898 }
899}
900
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