VirtualBox

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

Last change on this file since 86669 was 85124, checked in by vboxsync, 4 years ago

*: Use DECL_HIDDEN_DATA for data, DECLHIDDEN will soon be exclusively for functions. bugref:9794

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