VirtualBox

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

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

memobj-r0drv-freebsd.cpp: Hopefully correct range check now...

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