VirtualBox

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

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