VirtualBox

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

Last change on this file since 27146 was 27146, checked in by vboxsync, 15 years ago

memobj-r0drv-freebsd.c: Major rewrite again. Removed most of the vm_object_t usage and changed the Phys/PhysNC allocators again. Seems to work this time but only tested on FreeBSD 9-CURRENT amd64 so far, so be careful

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