VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/darwin/memobj-r0drv-darwin.cpp@ 4135

Last change on this file since 4135 was 4135, checked in by vboxsync, 17 years ago

Use size_t for the page index. Added API for querying the ring-3 address.

  • Property svn:keywords set to Id
File size: 21.4 KB
Line 
1/* $Id: memobj-r0drv-darwin.cpp 4135 2007-08-14 01:29:43Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Ring-0 Memory Objects, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include "the-darwin-kernel.h"
23
24#include <iprt/memobj.h>
25#include <iprt/alloc.h>
26#include <iprt/assert.h>
27#include <iprt/log.h>
28#include <iprt/param.h>
29#include <iprt/string.h>
30#include <iprt/process.h>
31#include "internal/memobj.h"
32
33#define USE_VM_MAP_WIRE
34
35
36/*******************************************************************************
37* Structures and Typedefs *
38*******************************************************************************/
39/**
40 * The Darwin version of the memory object structure.
41 */
42typedef struct RTR0MEMOBJDARWIN
43{
44 /** The core structure. */
45 RTR0MEMOBJINTERNAL Core;
46 /** Pointer to the memory descriptor created for allocated and locked memory. */
47 IOMemoryDescriptor *pMemDesc;
48 /** Pointer to the memory mapping object for mapped memory. */
49 IOMemoryMap *pMemMap;
50} RTR0MEMOBJDARWIN, *PRTR0MEMOBJDARWIN;
51
52
53int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
54{
55 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
56
57 /*
58 * Release the IOMemoryDescriptor/IOMemoryMap associated with the object.
59 */
60 if (pMemDarwin->pMemDesc)
61 {
62 if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
63 pMemDarwin->pMemDesc->complete(); /* paranoia */
64 pMemDarwin->pMemDesc->release();
65 pMemDarwin->pMemDesc = NULL;
66 Assert(!pMemDarwin->pMemMap);
67 }
68 else if (pMemDarwin->pMemMap)
69 {
70 pMemDarwin->pMemMap->release();
71 pMemDarwin->pMemMap = NULL;
72 }
73
74 /*
75 * Release any memory that we've allocated or locked.
76 */
77 switch (pMemDarwin->Core.enmType)
78 {
79 case RTR0MEMOBJTYPE_LOW:
80 case RTR0MEMOBJTYPE_PAGE:
81 IOFreeAligned(pMemDarwin->Core.pv, pMemDarwin->Core.cb);
82 break;
83
84 case RTR0MEMOBJTYPE_CONT:
85 IOFreeContiguous(pMemDarwin->Core.pv, pMemDarwin->Core.cb);
86 break;
87
88 case RTR0MEMOBJTYPE_LOCK:
89 {
90#ifdef USE_VM_MAP_WIRE
91 vm_map_t Map = pMemDarwin->Core.u.Lock.R0Process != NIL_RTR0PROCESS
92 ? get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process)
93 : kernel_map;
94 kern_return_t kr = vm_map_unwire(Map,
95 (vm_map_offset_t)pMemDarwin->Core.pv,
96 (vm_map_offset_t)pMemDarwin->Core.pv + pMemDarwin->Core.cb,
97 0 /* not user */);
98 AssertRC(kr == KERN_SUCCESS); /** @todo don't ignore... */
99#endif
100 break;
101 }
102
103 case RTR0MEMOBJTYPE_PHYS:
104 /*if (pMemDarwin->Core.u.Phys.fAllocated)
105 IOFreePhysical(pMemDarwin->Core.u.Phys.PhysBase, pMemDarwin->Core.cb);*/
106 Assert(!pMemDarwin->Core.u.Phys.fAllocated);
107 break;
108
109 case RTR0MEMOBJTYPE_RES_VIRT:
110 AssertMsgFailed(("RTR0MEMOBJTYPE_RES_VIRT\n"));
111 return VERR_INTERNAL_ERROR;
112 break;
113
114 case RTR0MEMOBJTYPE_MAPPING:
115 /* nothing to do here. */
116 break;
117
118 default:
119 AssertMsgFailed(("enmType=%d\n", pMemDarwin->Core.enmType));
120 return VERR_INTERNAL_ERROR;
121 }
122
123 return VINF_SUCCESS;
124}
125
126
127int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
128{
129 /*
130 * Try allocate the memory and create it's IOMemoryDescriptor first.
131 */
132 int rc = VERR_NO_PAGE_MEMORY;
133 AssertCompile(sizeof(IOPhysicalAddress) == 4);
134 void *pv = IOMallocAligned(cb, PAGE_SIZE);
135 if (pv)
136 {
137 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
138 if (pMemDesc)
139 {
140 /*
141 * Create the IPRT memory object.
142 */
143 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PAGE, pv, cb);
144 if (pMemDarwin)
145 {
146 pMemDarwin->pMemDesc = pMemDesc;
147 *ppMem = &pMemDarwin->Core;
148 return VINF_SUCCESS;
149 }
150
151 rc = VERR_NO_MEMORY;
152 pMemDesc->release();
153 }
154 else
155 rc = VERR_MEMOBJ_INIT_FAILED;
156 IOFreeAligned(pv, cb);
157 }
158 return rc;
159}
160
161
162int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
163{
164#if 1
165 /*
166 * Allocating 128KB for the low page pool can bit a bit exhausting on the kernel,
167 * it frequnetly causes the entire box to lock up on startup.
168 *
169 * So, try allocate the memory using IOMallocAligned first and if we get any high
170 * physical memory we'll release it and fall back on IOMAllocContiguous.
171 */
172 int rc = VERR_NO_PAGE_MEMORY;
173 AssertCompile(sizeof(IOPhysicalAddress) == 4);
174 void *pv = IOMallocAligned(cb, PAGE_SIZE);
175 if (pv)
176 {
177 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
178 if (pMemDesc)
179 {
180 /*
181 * Check if it's all below 4GB.
182 */
183 for (IOByteCount off = 0; off < cb; off += PAGE_SIZE)
184 {
185 addr64_t Addr = pMemDesc->getPhysicalSegment64(off, NULL);
186 if (Addr > (uint32_t)(_4G - PAGE_SIZE))
187 {
188 /* Ok, we failed, fall back on contiguous allocation. */
189 pMemDesc->release();
190 IOFreeAligned(pv, cb);
191 return rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
192 }
193 }
194
195 /*
196 * Create the IPRT memory object.
197 */
198 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOW, pv, cb);
199 if (pMemDarwin)
200 {
201 pMemDarwin->pMemDesc = pMemDesc;
202 *ppMem = &pMemDarwin->Core;
203 return VINF_SUCCESS;
204 }
205
206 rc = VERR_NO_MEMORY;
207 pMemDesc->release();
208 }
209 else
210 rc = VERR_MEMOBJ_INIT_FAILED;
211 IOFreeAligned(pv, cb);
212 }
213 return rc;
214
215#else
216
217 /*
218 * IOMallocContiguous is the most suitable API.
219 */
220 return rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
221#endif
222}
223
224
225int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
226{
227 /*
228 * Try allocate the memory and create it's IOMemoryDescriptor first.
229 */
230 int rc = VERR_NO_CONT_MEMORY;
231 AssertCompile(sizeof(IOPhysicalAddress) == 4);
232 void *pv = IOMallocContiguous(cb, PAGE_SIZE, NULL);
233 if (pv)
234 {
235 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
236 if (pMemDesc)
237 {
238 /* a bit of useful paranoia. */
239 addr64_t PhysAddr = pMemDesc->getPhysicalSegment64(0, NULL);
240 Assert(PhysAddr == pMemDesc->getPhysicalAddress());
241 if ( PhysAddr > 0
242 && PhysAddr <= _4G
243 && PhysAddr + cb <= _4G)
244 {
245 /*
246 * Create the IPRT memory object.
247 */
248 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_CONT, pv, cb);
249 if (pMemDarwin)
250 {
251 pMemDarwin->Core.u.Cont.Phys = PhysAddr;
252 pMemDarwin->pMemDesc = pMemDesc;
253 *ppMem = &pMemDarwin->Core;
254 return VINF_SUCCESS;
255 }
256
257 rc = VERR_NO_MEMORY;
258 }
259 else
260 {
261 AssertMsgFailed(("PhysAddr=%llx\n", (unsigned long long)PhysAddr));
262 rc = VERR_INTERNAL_ERROR;
263 }
264 pMemDesc->release();
265 }
266 else
267 rc = VERR_MEMOBJ_INIT_FAILED;
268 IOFreeContiguous(pv, cb);
269 }
270 return rc;
271}
272
273
274int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
275{
276#if 0 /* turned out IOMallocPhysical isn't exported yet. sigh. */
277 /*
278 * Try allocate the memory and create it's IOMemoryDescriptor first.
279 * Note that IOMallocPhysical is not working correctly (it's ignoring the mask).
280 */
281
282 /* first calc the mask (in the hope that it'll be used) */
283 IOPhysicalAddress PhysMask = ~(IOPhysicalAddress)PAGE_OFFSET_MASK;
284 if (PhysHighest != NIL_RTHCPHYS)
285 {
286 PhysMask = ~(IOPhysicalAddress)0;
287 while (PhysMask > PhysHighest)
288 PhysMask >>= 1;
289 AssertReturn(PhysMask + 1 < cb, VERR_INVALID_PARAMETER);
290 PhysMask &= ~(IOPhysicalAddress)PAGE_OFFSET_MASK;
291 }
292
293 /* try allocate physical memory. */
294 int rc = VERR_NO_PHYS_MEMORY;
295 mach_vm_address_t PhysAddr64 = IOMallocPhysical(cb, PhysMask);
296 if (PhysAddr64)
297 {
298 IOPhysicalAddress PhysAddr = PhysAddr64;
299 if ( PhysAddr == PhysAddr64
300 && PhysAddr < PhysHighest
301 && PhysAddr + cb <= PhysHighest)
302 {
303 /* create a descriptor. */
304 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withPhysicalAddress(PhysAddr, cb, kIODirectionInOut);
305 if (pMemDesc)
306 {
307 Assert(PhysAddr == pMemDesc->getPhysicalAddress());
308
309 /*
310 * Create the IPRT memory object.
311 */
312 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
313 if (pMemDarwin)
314 {
315 pMemDarwin->Core.u.Phys.PhysBase = PhysAddr;
316 pMemDarwin->Core.u.Phys.fAllocated = true;
317 pMemDarwin->pMemDesc = pMemDesc;
318 *ppMem = &pMemDarwin->Core;
319 return VINF_SUCCESS;
320 }
321
322 rc = VERR_NO_MEMORY;
323 pMemDesc->release();
324 }
325 else
326 rc = VERR_MEMOBJ_INIT_FAILED;
327 }
328 else
329 {
330 AssertMsgFailed(("PhysAddr=%#llx PhysAddr64=%#llx PhysHigest=%#llx\n", (unsigned long long)PhysAddr,
331 (unsigned long long)PhysAddr64, (unsigned long long)PhysHighest));
332 rc = VERR_INTERNAL_ERROR;
333 }
334
335 IOFreePhysical(PhysAddr64, cb);
336 }
337
338 /*
339 * Just in case IOMallocContiguous doesn't work right, we can try fall back
340 * on a contiguous allcation.
341 */
342 if (rc == VERR_INTERNAL_ERROR || rc == VERR_NO_PHYS_MEMORY)
343 {
344 int rc2 = rtR0MemObjNativeAllocCont(ppMem, cb, false);
345 if (RT_SUCCESS(rc2))
346 rc = rc2;
347 }
348
349 return rc;
350
351#else
352
353 return rtR0MemObjNativeAllocCont(ppMem, cb, false);
354#endif
355}
356
357
358int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
359{
360 /*
361 * Validate the address range and create a descriptor for it.
362 */
363 int rc = VERR_ADDRESS_TOO_BIG;
364 IOPhysicalAddress PhysAddr = Phys;
365 if (PhysAddr == Phys)
366 {
367 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withPhysicalAddress(PhysAddr, cb, kIODirectionInOut);
368 if (pMemDesc)
369 {
370 Assert(PhysAddr == pMemDesc->getPhysicalAddress());
371
372 /*
373 * Create the IPRT memory object.
374 */
375 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
376 if (pMemDarwin)
377 {
378 pMemDarwin->Core.u.Phys.PhysBase = PhysAddr;
379 pMemDarwin->Core.u.Phys.fAllocated = false;
380 pMemDarwin->pMemDesc = pMemDesc;
381 *ppMem = &pMemDarwin->Core;
382 return VINF_SUCCESS;
383 }
384
385 rc = VERR_NO_MEMORY;
386 pMemDesc->release();
387 }
388 }
389 else
390 AssertMsgFailed(("%#llx\n", (unsigned long long)Phys));
391 return rc;
392}
393
394
395/**
396 * Internal worker for locking down pages.
397 *
398 * @return IPRT status code.
399 *
400 * @param ppMem Where to store the memory object pointer.
401 * @param pv First page.
402 * @param cb Number of bytes.
403 * @param Task The task \a pv and \a cb refers to.
404 */
405static int rtR0MemObjNativeLock(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, task_t Task)
406{
407#ifdef USE_VM_MAP_WIRE
408 vm_map_t Map = get_task_map(Task);
409 Assert(Map);
410
411 /*
412 * First try lock the memory.
413 */
414 int rc = VERR_LOCK_FAILED;
415 kern_return_t kr = vm_map_wire(get_task_map(Task),
416 (vm_map_offset_t)pv,
417 (vm_map_offset_t)pv + cb,
418 VM_PROT_DEFAULT,
419 0 /* not user */);
420 if (kr == KERN_SUCCESS)
421 {
422 /*
423 * Create the IPRT memory object.
424 */
425 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
426 if (pMemDarwin)
427 {
428 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
429 *ppMem = &pMemDarwin->Core;
430 return VINF_SUCCESS;
431 }
432
433 kr = vm_map_unwire(get_task_map(Task), (vm_map_offset_t)pv, (vm_map_offset_t)pv + cb, 0 /* not user */);
434 Assert(kr == KERN_SUCCESS);
435 rc = VERR_NO_MEMORY;
436 }
437
438#else
439
440 /*
441 * Create a descriptor and try lock it (prepare).
442 */
443 int rc = VERR_MEMOBJ_INIT_FAILED;
444 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, Task);
445 if (pMemDesc)
446 {
447 IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
448 if (IORet == kIOReturnSuccess)
449 {
450 /*
451 * Create the IPRT memory object.
452 */
453 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
454 if (pMemDarwin)
455 {
456 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
457 pMemDarwin->pMemDesc = pMemDesc;
458 *ppMem = &pMemDarwin->Core;
459 return VINF_SUCCESS;
460 }
461
462 pMemDesc->complete();
463 rc = VERR_NO_MEMORY;
464 }
465 else
466 rc = VERR_LOCK_FAILED;
467 pMemDesc->release();
468 }
469#endif
470 return rc;
471}
472
473
474int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, RTR0PROCESS R0Process)
475{
476 return rtR0MemObjNativeLock(ppMem, pv, cb, (task_t)R0Process);
477}
478
479
480int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
481{
482 return rtR0MemObjNativeLock(ppMem, pv, cb, kernel_task);
483}
484
485
486int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
487{
488 return VERR_NOT_IMPLEMENTED;
489}
490
491
492int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
493{
494 return VERR_NOT_IMPLEMENTED;
495}
496
497
498int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
499{
500 /*
501 * Must have a memory descriptor.
502 */
503 int rc = VERR_INVALID_PARAMETER;
504 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
505 if (pMemToMapDarwin->pMemDesc)
506 {
507 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map(kernel_task, kIOMapAnywhere,
508 kIOMapAnywhere | kIOMapDefaultCache);
509 if (pMemMap)
510 {
511 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
512 void *pv = (void *)(uintptr_t)VirtAddr;
513 if ((uintptr_t)pv == VirtAddr)
514 {
515 /*
516 * Create the IPRT memory object.
517 */
518 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
519 pv, pMemToMapDarwin->Core.cb);
520 if (pMemDarwin)
521 {
522 pMemDarwin->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
523 pMemDarwin->pMemMap = pMemMap;
524 *ppMem = &pMemDarwin->Core;
525 return VINF_SUCCESS;
526 }
527
528 rc = VERR_NO_MEMORY;
529 }
530 else
531 rc = VERR_ADDRESS_TOO_BIG;
532 pMemMap->release();
533 }
534 else
535 rc = VERR_MAP_FAILED;
536 }
537 return rc;
538}
539
540
541int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
542{
543 /*
544 * Must have a memory descriptor.
545 */
546 int rc = VERR_INVALID_PARAMETER;
547 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
548 if (pMemToMapDarwin->pMemDesc)
549 {
550 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map((task_t)R0Process, kIOMapAnywhere,
551 kIOMapAnywhere | kIOMapDefaultCache);
552 if (pMemMap)
553 {
554 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
555 void *pv = (void *)(uintptr_t)VirtAddr;
556 if ((uintptr_t)pv == VirtAddr)
557 {
558 /*
559 * Create the IPRT memory object.
560 */
561 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
562 pv, pMemToMapDarwin->Core.cb);
563 if (pMemDarwin)
564 {
565 pMemDarwin->Core.u.Mapping.R0Process = R0Process;
566 pMemDarwin->pMemMap = pMemMap;
567 *ppMem = &pMemDarwin->Core;
568 return VINF_SUCCESS;
569 }
570
571 rc = VERR_NO_MEMORY;
572 }
573 else
574 rc = VERR_ADDRESS_TOO_BIG;
575 pMemMap->release();
576 }
577 else
578 rc = VERR_MAP_FAILED;
579 }
580 return rc;
581}
582
583
584RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
585{
586 RTHCPHYS PhysAddr;
587 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
588
589#ifdef USE_VM_MAP_WIRE
590 /*
591 * Locked memory doesn't have a memory descriptor and
592 * needs to be handled differently.
593 */
594 if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
595 {
596 ppnum_t PgNo;
597 if (pMemDarwin->Core.u.Lock.R0Process == NIL_RTR0PROCESS)
598 PgNo = pmap_find_phys(kernel_pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
599 else
600 {
601 /*
602 * From what I can tell, Apple seems to have locked up the all the
603 * available interfaces that could help us obtain the pmap_t of a task
604 * or vm_map_t.
605
606 * So, we'll have to figure out where in the vm_map_t structure it is
607 * and read it our selves. ASSUMING that kernel_pmap is pointed to by
608 * kernel_map->pmap, we scan kernel_map to locate the structure offset.
609 * Not nice, but it will hopefully do the job in a reliable manner...
610 *
611 * (get_task_pmap, get_map_pmap or vm_map_pmap is what we really need btw.)
612 */
613 static int s_offPmap = -1;
614 if (RT_UNLIKELY(s_offPmap == -1))
615 {
616 pmap_t const *p = (pmap_t *)kernel_map;
617 pmap_t const * const pEnd = p + 64;
618 for (; p < pEnd; p++)
619 if (*p == kernel_pmap)
620 {
621 s_offPmap = (uintptr_t)p - (uintptr_t)kernel_map;
622 break;
623 }
624 AssertReturn(s_offPmap >= 0, NIL_RTHCPHYS);
625 }
626 pmap_t Pmap = *(pmap_t *)((uintptr_t)get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process) + s_offPmap);
627 PgNo = pmap_find_phys(Pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
628 }
629
630 AssertReturn(PgNo, NIL_RTHCPHYS);
631 PhysAddr = (RTHCPHYS)PgNo << PAGE_SHIFT;
632 Assert((PhysAddr >> PAGE_SHIFT) == PgNo);
633 }
634 else
635#endif /* USE_VM_MAP_WIRE */
636 {
637 /*
638 * Get the memory descriptor.
639 */
640 IOMemoryDescriptor *pMemDesc = pMemDarwin->pMemDesc;
641 if (!pMemDesc)
642 pMemDesc = pMemDarwin->pMemMap->getMemoryDescriptor();
643 AssertReturn(pMemDesc, NIL_RTHCPHYS);
644
645 /*
646 * If we've got a memory descriptor, use getPhysicalSegment64().
647 */
648 addr64_t Addr = pMemDesc->getPhysicalSegment64(iPage * PAGE_SIZE, NULL);
649 AssertMsgReturn(Addr, ("iPage=%u\n", iPage), NIL_RTHCPHYS);
650 PhysAddr = Addr;
651 AssertMsgReturn(PhysAddr == Addr, ("PhysAddr=%VHp Addr=%RX64\n", PhysAddr, (uint64_t)Addr), NIL_RTHCPHYS);
652 }
653
654 return PhysAddr;
655}
656
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