VirtualBox

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

Last change on this file since 78398 was 78120, checked in by vboxsync, 6 years ago

IPRT: Started adding a RTR0MemObjMapUserEx function that takes offSub and cbSub. bugref:9217

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