VirtualBox

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

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

iprt: added CachePolicy parameter to RTR0MemObjEnterPhys()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.8 KB
Line 
1/* $Id: memobj-r0drv-freebsd.c 28777 2010-04-26 19:45:16Z 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, unsigned CachePolicy)
499{
500 AssertReturn(CachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_IMPLEMENTED);
501
502 /* create the object. */
503 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_PHYS, NULL, cb);
504 if (!pMemFreeBSD)
505 return VERR_NO_MEMORY;
506
507 /* there is no allocation here, it needs to be mapped somewhere first. */
508 pMemFreeBSD->Core.u.Phys.fAllocated = false;
509 pMemFreeBSD->Core.u.Phys.PhysBase = Phys;
510 *ppMem = &pMemFreeBSD->Core;
511 return VINF_SUCCESS;
512}
513
514
515/**
516 * Worker locking the memory in either kernel or user maps.
517 */
518static int rtR0MemObjNativeLockInMap(PPRTR0MEMOBJINTERNAL ppMem, vm_map_t pVmMap,
519 vm_offset_t AddrStart, size_t cb, uint32_t fAccess,
520 RTR0PROCESS R0Process)
521{
522 int rc;
523 NOREF(fAccess);
524
525 /* create the object. */
526 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_LOCK, (void *)AddrStart, cb);
527 if (!pMemFreeBSD)
528 return VERR_NO_MEMORY;
529
530 /*
531 * We could've used vslock here, but we don't wish to be subject to
532 * resource usage restrictions, so we'll call vm_map_wire directly.
533 */
534 rc = vm_map_wire(pVmMap, /* the map */
535 AddrStart, /* start */
536 AddrStart + cb, /* end */
537 VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES); /* flags */
538 if (rc == KERN_SUCCESS)
539 {
540 pMemFreeBSD->Core.u.Lock.R0Process = R0Process;
541 *ppMem = &pMemFreeBSD->Core;
542 return VINF_SUCCESS;
543 }
544 rtR0MemObjDelete(&pMemFreeBSD->Core);
545 return VERR_NO_MEMORY;/** @todo fix mach -> vbox error conversion for freebsd. */
546}
547
548
549int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
550{
551 return rtR0MemObjNativeLockInMap(ppMem,
552 &((struct proc *)R0Process)->p_vmspace->vm_map,
553 (vm_offset_t)R3Ptr,
554 cb,
555 fAccess,
556 R0Process);
557}
558
559
560int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
561{
562 return rtR0MemObjNativeLockInMap(ppMem,
563 kernel_map,
564 (vm_offset_t)pv,
565 cb,
566 fAccess,
567 NIL_RTR0PROCESS);
568}
569
570
571/**
572 * Worker for the two virtual address space reservers.
573 *
574 * We're leaning on the examples provided by mmap and vm_mmap in vm_mmap.c here.
575 */
576static int rtR0MemObjNativeReserveInMap(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process, vm_map_t pMap)
577{
578 int rc;
579
580 /*
581 * The pvFixed address range must be within the VM space when specified.
582 */
583 if (pvFixed != (void *)-1
584 && ( (vm_offset_t)pvFixed < vm_map_min(pMap)
585 || (vm_offset_t)pvFixed + cb > vm_map_max(pMap)))
586 return VERR_INVALID_PARAMETER;
587
588 /*
589 * Check that the specified alignment is supported.
590 */
591 if (uAlignment > PAGE_SIZE)
592 return VERR_NOT_SUPPORTED;
593
594 /*
595 * Create the object.
596 */
597 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(*pMemFreeBSD), RTR0MEMOBJTYPE_RES_VIRT, NULL, cb);
598 if (!pMemFreeBSD)
599 return VERR_NO_MEMORY;
600
601 /*
602 * Allocate an empty VM object and map it into the requested map.
603 */
604 pMemFreeBSD->u.NonPhys.pObject = vm_object_allocate(OBJT_DEFAULT, cb >> PAGE_SHIFT);
605 if (pMemFreeBSD->u.NonPhys.pObject)
606 {
607 vm_offset_t MapAddress = pvFixed != (void *)-1
608 ? (vm_offset_t)pvFixed
609 : vm_map_min(pMap);
610 if (pvFixed != (void *)-1)
611 vm_map_remove(pMap,
612 MapAddress,
613 MapAddress + cb);
614
615 rc = vm_map_find(pMap, /* map */
616 pMemFreeBSD->u.NonPhys.pObject, /* object */
617 0, /* offset */
618 &MapAddress, /* addr (IN/OUT) */
619 cb, /* length */
620 pvFixed == (void *)-1, /* find_space */
621 VM_PROT_NONE, /* protection */
622 VM_PROT_ALL, /* max(_prot) ?? */
623 0); /* cow (copy-on-write) */
624 if (rc == KERN_SUCCESS)
625 {
626 if (R0Process != NIL_RTR0PROCESS)
627 {
628 rc = vm_map_inherit(pMap,
629 MapAddress,
630 MapAddress + cb,
631 VM_INHERIT_SHARE);
632 AssertMsg(rc == KERN_SUCCESS, ("%#x\n", rc));
633 }
634 pMemFreeBSD->Core.pv = (void *)MapAddress;
635 pMemFreeBSD->Core.u.ResVirt.R0Process = R0Process;
636 *ppMem = &pMemFreeBSD->Core;
637 return VINF_SUCCESS;
638 }
639 vm_object_deallocate(pMemFreeBSD->u.NonPhys.pObject);
640 rc = VERR_NO_MEMORY; /** @todo fix translation (borrow from darwin) */
641 }
642 else
643 rc = VERR_NO_MEMORY;
644 rtR0MemObjDelete(&pMemFreeBSD->Core);
645 return rc;
646
647}
648
649int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
650{
651 return rtR0MemObjNativeReserveInMap(ppMem, pvFixed, cb, uAlignment, NIL_RTR0PROCESS, kernel_map);
652}
653
654
655int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
656{
657 return rtR0MemObjNativeReserveInMap(ppMem, (void *)R3PtrFixed, cb, uAlignment, R0Process,
658 &((struct proc *)R0Process)->p_vmspace->vm_map);
659}
660
661
662int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
663 unsigned fProt, size_t offSub, size_t cbSub)
664{
665 AssertMsgReturn(!offSub && !cbSub, ("%#x %#x\n", offSub, cbSub), VERR_NOT_SUPPORTED);
666 AssertMsgReturn(pvFixed == (void *)-1, ("%p\n", pvFixed), VERR_NOT_SUPPORTED);
667
668 /*
669 * Check that the specified alignment is supported.
670 */
671 if (uAlignment > PAGE_SIZE)
672 return VERR_NOT_SUPPORTED;
673
674/* Phys: see pmap_mapdev in i386/i386/pmap.c (http://fxr.watson.org/fxr/source/i386/i386/pmap.c?v=RELENG62#L2860) */
675/** @todo finish the implementation. */
676
677 return VERR_NOT_IMPLEMENTED;
678}
679
680
681/* see http://markmail.org/message/udhq33tefgtyfozs */
682int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
683{
684 /*
685 * Check for unsupported stuff.
686 */
687 AssertMsgReturn(R0Process == RTR0ProcHandleSelf(), ("%p != %p\n", R0Process, RTR0ProcHandleSelf()), VERR_NOT_SUPPORTED);
688 AssertMsgReturn(R3PtrFixed == (RTR3PTR)-1, ("%p\n", R3PtrFixed), VERR_NOT_SUPPORTED);
689 if (uAlignment > PAGE_SIZE)
690 return VERR_NOT_SUPPORTED;
691
692 int rc;
693 PRTR0MEMOBJFREEBSD pMemToMapFreeBSD = (PRTR0MEMOBJFREEBSD)pMemToMap;
694 struct proc *pProc = (struct proc *)R0Process;
695 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
696
697 /* calc protection */
698 vm_prot_t ProtectionFlags = 0;
699 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
700 ProtectionFlags = VM_PROT_NONE;
701 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
702 ProtectionFlags |= VM_PROT_READ;
703 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
704 ProtectionFlags |= VM_PROT_WRITE;
705 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
706 ProtectionFlags |= VM_PROT_EXECUTE;
707
708 /* calc mapping address */
709 PROC_LOCK(pProc);
710 vm_offset_t AddrR3 = round_page((vm_offset_t)pProc->p_vmspace->vm_daddr + lim_max(pProc, RLIMIT_DATA));
711 PROC_UNLOCK(pProc);
712
713 /* Insert the object in the map. */
714 rc = vm_map_find(pProcMap, /* Map to insert the object in */
715 NULL, /* Object to map */
716 0, /* Start offset in the object */
717 &AddrR3, /* Start address IN/OUT */
718 pMemToMap->cb, /* Size of the mapping */
719 TRUE, /* Whether a suitable address should be searched for first */
720 ProtectionFlags, /* protection flags */
721 VM_PROT_ALL, /* Maximum protection flags */
722 0); /* Copy on write */
723
724 /* Map the memory page by page into the destination map. */
725 if (rc == KERN_SUCCESS)
726 {
727 size_t cPages = pMemToMap->cb >> PAGE_SHIFT;;
728 pmap_t pPhysicalMap = pProcMap->pmap;
729 vm_offset_t AddrR3Dst = AddrR3;
730
731 if ( pMemToMap->enmType == RTR0MEMOBJTYPE_PHYS
732 || pMemToMap->enmType == RTR0MEMOBJTYPE_PHYS_NC
733 || pMemToMap->enmType == RTR0MEMOBJTYPE_PAGE)
734 {
735 /* Mapping physical allocations */
736 Assert(cPages == pMemToMapFreeBSD->u.Phys.cPages);
737
738 /* Insert the memory page by page into the mapping. */
739 for (uint32_t iPage = 0; iPage < cPages; iPage++)
740 {
741 vm_page_t pPage = pMemToMapFreeBSD->u.Phys.apPages[iPage];
742
743 MY_PMAP_ENTER(pPhysicalMap, AddrR3Dst, pPage, ProtectionFlags, TRUE);
744 AddrR3Dst += PAGE_SIZE;
745 }
746 }
747 else
748 {
749 /* Mapping cont or low memory types */
750 vm_offset_t AddrToMap = (vm_offset_t)pMemToMap->pv;
751
752 for (uint32_t iPage = 0; iPage < cPages; iPage++)
753 {
754 vm_page_t pPage = PHYS_TO_VM_PAGE(vtophys(AddrToMap));
755
756 MY_PMAP_ENTER(pPhysicalMap, AddrR3Dst, pPage, ProtectionFlags, TRUE);
757 AddrR3Dst += PAGE_SIZE;
758 AddrToMap += PAGE_SIZE;
759 }
760 }
761 }
762
763 if (RT_SUCCESS(rc))
764 {
765 /*
766 * Create a mapping object for it.
767 */
768 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)rtR0MemObjNew(sizeof(RTR0MEMOBJFREEBSD),
769 RTR0MEMOBJTYPE_MAPPING,
770 (void *)AddrR3,
771 pMemToMap->cb);
772 if (pMemFreeBSD)
773 {
774 Assert((vm_offset_t)pMemFreeBSD->Core.pv == AddrR3);
775 pMemFreeBSD->Core.u.Mapping.R0Process = R0Process;
776 *ppMem = &pMemFreeBSD->Core;
777 return VINF_SUCCESS;
778 }
779
780 rc = vm_map_remove(pProcMap, ((vm_offset_t)AddrR3), ((vm_offset_t)AddrR3) + pMemToMap->cb);
781 AssertMsg(rc == KERN_SUCCESS, ("Deleting mapping failed\n"));
782 }
783
784 return VERR_NO_MEMORY;
785}
786
787
788int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
789{
790 vm_prot_t ProtectionFlags = 0;
791 vm_offset_t AddrStart = (uintptr_t)pMem->pv + offSub;
792 vm_offset_t AddrEnd = AddrStart + cbSub;
793 vm_map_t pVmMap = rtR0MemObjFreeBSDGetMap(pMem);
794
795 if (!pVmMap)
796 return VERR_NOT_SUPPORTED;
797
798 if ((fProt & RTMEM_PROT_NONE) == RTMEM_PROT_NONE)
799 ProtectionFlags = VM_PROT_NONE;
800 if ((fProt & RTMEM_PROT_READ) == RTMEM_PROT_READ)
801 ProtectionFlags |= VM_PROT_READ;
802 if ((fProt & RTMEM_PROT_WRITE) == RTMEM_PROT_WRITE)
803 ProtectionFlags |= VM_PROT_WRITE;
804 if ((fProt & RTMEM_PROT_EXEC) == RTMEM_PROT_EXEC)
805 ProtectionFlags |= VM_PROT_EXECUTE;
806
807 int krc = vm_map_protect(pVmMap, AddrStart, AddrEnd, ProtectionFlags, FALSE);
808 if (krc == KERN_SUCCESS)
809 return VINF_SUCCESS;
810
811 return VERR_NOT_SUPPORTED;
812}
813
814
815RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
816{
817 PRTR0MEMOBJFREEBSD pMemFreeBSD = (PRTR0MEMOBJFREEBSD)pMem;
818
819 switch (pMemFreeBSD->Core.enmType)
820 {
821 case RTR0MEMOBJTYPE_LOCK:
822 {
823 if ( pMemFreeBSD->Core.u.Lock.R0Process != NIL_RTR0PROCESS
824 && pMemFreeBSD->Core.u.Lock.R0Process != (RTR0PROCESS)curproc)
825 {
826 /* later */
827 return NIL_RTHCPHYS;
828 }
829
830 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + (iPage << PAGE_SHIFT);
831
832 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Lock.R0Process;
833 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
834 pmap_t pPhysicalMap = pProcMap->pmap;
835
836 return pmap_extract(pPhysicalMap, pb);
837 }
838
839 case RTR0MEMOBJTYPE_MAPPING:
840 {
841 vm_offset_t pb = (vm_offset_t)pMemFreeBSD->Core.pv + (iPage << PAGE_SHIFT);
842
843 if (pMemFreeBSD->Core.u.Mapping.R0Process != NIL_RTR0PROCESS)
844 {
845 struct proc *pProc = (struct proc *)pMemFreeBSD->Core.u.Mapping.R0Process;
846 struct vm_map *pProcMap = &pProc->p_vmspace->vm_map;
847 pmap_t pPhysicalMap = pProcMap->pmap;
848
849 return pmap_extract(pPhysicalMap, pb);
850 }
851 return vtophys(pb);
852 }
853
854 case RTR0MEMOBJTYPE_CONT:
855 return pMemFreeBSD->Core.u.Cont.Phys + (iPage << PAGE_SHIFT);
856
857 case RTR0MEMOBJTYPE_PHYS:
858 return pMemFreeBSD->Core.u.Phys.PhysBase + (iPage << PAGE_SHIFT);
859
860 case RTR0MEMOBJTYPE_PAGE:
861 case RTR0MEMOBJTYPE_PHYS_NC:
862 return VM_PAGE_TO_PHYS(pMemFreeBSD->u.Phys.apPages[iPage]);
863
864 case RTR0MEMOBJTYPE_RES_VIRT:
865 case RTR0MEMOBJTYPE_LOW:
866 default:
867 return NIL_RTHCPHYS;
868 }
869}
870
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