VirtualBox

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

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

RTR0MemObjEnterPhys/rtR0MemObjNativeEnterPhys: Validate the cache policy in the common code. Use uint32_t as parameter type. All native implementations must set the policy member.

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