VirtualBox

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

Last change on this file since 82968 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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