VirtualBox

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

Last change on this file since 42935 was 42037, checked in by vboxsync, 13 years ago

Runtime/Darwin: RT_BIT -> iprt/x86.h constants (please review)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 41.7 KB
Line 
1/* $Id: memobj-r0drv-darwin.cpp 42037 2012-07-06 05:58:07Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2012 Oracle Corporation
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include "the-darwin-kernel.h"
32#include "internal/iprt.h"
33#include <iprt/memobj.h>
34
35#include <iprt/asm.h>
36#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
37# include <iprt/x86.h>
38# include <iprt/asm-amd64-x86.h>
39#endif
40#include <iprt/assert.h>
41#include <iprt/log.h>
42#include <iprt/mem.h>
43#include <iprt/param.h>
44#include <iprt/process.h>
45#include <iprt/string.h>
46#include <iprt/thread.h>
47#include "internal/memobj.h"
48
49/*#define USE_VM_MAP_WIRE - may re-enable later when non-mapped allocations are added. */
50
51
52/*******************************************************************************
53* Structures and Typedefs *
54*******************************************************************************/
55/**
56 * The Darwin version of the memory object structure.
57 */
58typedef struct RTR0MEMOBJDARWIN
59{
60 /** The core structure. */
61 RTR0MEMOBJINTERNAL Core;
62 /** Pointer to the memory descriptor created for allocated and locked memory. */
63 IOMemoryDescriptor *pMemDesc;
64 /** Pointer to the memory mapping object for mapped memory. */
65 IOMemoryMap *pMemMap;
66} RTR0MEMOBJDARWIN, *PRTR0MEMOBJDARWIN;
67
68
69/**
70 * Touch the pages to force the kernel to create or write-enable the page table
71 * entries.
72 *
73 * This is necessary since the kernel gets upset if we take a page fault when
74 * preemption is disabled and/or we own a simple lock (same thing). It has no
75 * problems with us disabling interrupts when taking the traps, weird stuff.
76 *
77 * (This is basically a way of invoking vm_fault on a range of pages.)
78 *
79 * @param pv Pointer to the first page.
80 * @param cb The number of bytes.
81 */
82static void rtR0MemObjDarwinTouchPages(void *pv, size_t cb)
83{
84 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
85 for (;;)
86 {
87 ASMAtomicCmpXchgU32(pu32, 0xdeadbeef, 0xdeadbeef);
88 if (cb <= PAGE_SIZE)
89 break;
90 cb -= PAGE_SIZE;
91 pu32 += PAGE_SIZE / sizeof(uint32_t);
92 }
93}
94
95
96/**
97 * Read (sniff) every page in the range to make sure there are some page tables
98 * entries backing it.
99 *
100 * This is just to be sure vm_protect didn't remove stuff without re-adding it
101 * if someone should try write-protect something.
102 *
103 * @param pv Pointer to the first page.
104 * @param cb The number of bytes.
105 */
106static void rtR0MemObjDarwinSniffPages(void const *pv, size_t cb)
107{
108 uint32_t volatile *pu32 = (uint32_t volatile *)pv;
109 uint32_t volatile u32Counter = 0;
110 for (;;)
111 {
112 u32Counter += *pu32;
113
114 if (cb <= PAGE_SIZE)
115 break;
116 cb -= PAGE_SIZE;
117 pu32 += PAGE_SIZE / sizeof(uint32_t);
118 }
119}
120
121
122/**
123 * Gets the virtual memory map the specified object is mapped into.
124 *
125 * @returns VM map handle on success, NULL if no map.
126 * @param pMem The memory object.
127 */
128DECLINLINE(vm_map_t) rtR0MemObjDarwinGetMap(PRTR0MEMOBJINTERNAL pMem)
129{
130 switch (pMem->enmType)
131 {
132 case RTR0MEMOBJTYPE_PAGE:
133 case RTR0MEMOBJTYPE_LOW:
134 case RTR0MEMOBJTYPE_CONT:
135 return kernel_map;
136
137 case RTR0MEMOBJTYPE_PHYS:
138 case RTR0MEMOBJTYPE_PHYS_NC:
139 return NULL; /* pretend these have no mapping atm. */
140
141 case RTR0MEMOBJTYPE_LOCK:
142 return pMem->u.Lock.R0Process == NIL_RTR0PROCESS
143 ? kernel_map
144 : get_task_map((task_t)pMem->u.Lock.R0Process);
145
146 case RTR0MEMOBJTYPE_RES_VIRT:
147 return pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS
148 ? kernel_map
149 : get_task_map((task_t)pMem->u.ResVirt.R0Process);
150
151 case RTR0MEMOBJTYPE_MAPPING:
152 return pMem->u.Mapping.R0Process == NIL_RTR0PROCESS
153 ? kernel_map
154 : get_task_map((task_t)pMem->u.Mapping.R0Process);
155
156 default:
157 return NULL;
158 }
159}
160
161#if 0 /* not necessary after all*/
162/* My vm_map mockup. */
163struct my_vm_map
164{
165 struct { char pad[8]; } lock;
166 struct my_vm_map_header
167 {
168 struct vm_map_links
169 {
170 void *prev;
171 void *next;
172 vm_map_offset_t start;
173 vm_map_offset_t end;
174 } links;
175 int nentries;
176 boolean_t entries_pageable;
177 } hdr;
178 pmap_t pmap;
179 vm_map_size_t size;
180};
181
182
183/**
184 * Gets the minimum map address, this is similar to get_map_min.
185 *
186 * @returns The start address of the map.
187 * @param pMap The map.
188 */
189static vm_map_offset_t rtR0MemObjDarwinGetMapMin(vm_map_t pMap)
190{
191 /* lazy discovery of the correct offset. The apple guys is a wonderfully secretive bunch. */
192 static int32_t volatile s_offAdjust = INT32_MAX;
193 int32_t off = s_offAdjust;
194 if (off == INT32_MAX)
195 {
196 for (off = 0; ; off += sizeof(pmap_t))
197 {
198 if (*(pmap_t *)((uint8_t *)kernel_map + off) == kernel_pmap)
199 break;
200 AssertReturn(off <= RT_MAX(RT_OFFSETOF(struct my_vm_map, pmap) * 4, 1024), 0x1000);
201 }
202 ASMAtomicWriteS32(&s_offAdjust, off - RT_OFFSETOF(struct my_vm_map, pmap));
203 }
204
205 /* calculate it. */
206 struct my_vm_map *pMyMap = (struct my_vm_map *)((uint8_t *)pMap + off);
207 return pMyMap->hdr.links.start;
208}
209#endif /* unused */
210
211#ifdef RT_STRICT
212
213/**
214 * Read from a physical page.
215 *
216 * @param HCPhys The address to start reading at.
217 * @param cb How many bytes to read.
218 * @param pvDst Where to put the bytes. This is zero'd on failure.
219 */
220static void rtR0MemObjDarwinReadPhys(RTHCPHYS HCPhys, size_t cb, void *pvDst)
221{
222 memset(pvDst, '\0', cb);
223
224 IOAddressRange aRanges[1] = { { (mach_vm_address_t)HCPhys, RT_ALIGN_Z(cb, PAGE_SIZE) } };
225 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddressRanges(&aRanges[0], RT_ELEMENTS(aRanges),
226 kIODirectionIn, NULL /*task*/);
227 if (pMemDesc)
228 {
229#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
230 IOMemoryMap *pMemMap = pMemDesc->createMappingInTask(kernel_task, 0, kIOMapAnywhere | kIOMapDefaultCache);
231#else
232 IOMemoryMap *pMemMap = pMemDesc->map(kernel_task, 0, kIOMapAnywhere | kIOMapDefaultCache);
233#endif
234 if (pMemMap)
235 {
236 void const *pvSrc = (void const *)(uintptr_t)pMemMap->getVirtualAddress();
237 memcpy(pvDst, pvSrc, cb);
238 pMemMap->release();
239 }
240 else
241 printf("rtR0MemObjDarwinReadPhys: createMappingInTask failed; HCPhys=%llx\n", HCPhys);
242
243 pMemDesc->release();
244 }
245 else
246 printf("rtR0MemObjDarwinReadPhys: withAddressRanges failed; HCPhys=%llx\n", HCPhys);
247}
248
249
250/**
251 * Gets the PTE for a page.
252 *
253 * @returns the PTE.
254 * @param pvPage The virtual address to get the PTE for.
255 */
256static uint64_t rtR0MemObjDarwinGetPTE(void *pvPage)
257{
258 RTUINT64U u64;
259 RTCCUINTREG cr3 = ASMGetCR3();
260 RTCCUINTREG cr4 = ASMGetCR4();
261 bool fPAE = false;
262 bool fLMA = false;
263 if (cr4 & X86_CR4_PAE)
264 {
265 fPAE = true;
266 uint32_t fExtFeatures = ASMCpuId_EDX(0x80000001);
267 if (fExtFeatures & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE)
268 {
269 uint64_t efer = ASMRdMsr(MSR_K6_EFER);
270 if (efer & MSR_K6_EFER_LMA)
271 fLMA = true;
272 }
273 }
274
275 if (fLMA)
276 {
277 /* PML4 */
278 rtR0MemObjDarwinReadPhys((cr3 & ~(RTCCUINTREG)PAGE_OFFSET_MASK) | (((uint64_t)(uintptr_t)pvPage >> X86_PML4_SHIFT) & X86_PML4_MASK) * 8, 8, &u64);
279 if (!(u64.u & X86_PML4E_P))
280 {
281 printf("rtR0MemObjDarwinGetPTE: %p -> PML4E !p\n", pvPage);
282 return 0;
283 }
284
285 /* PDPTR */
286 rtR0MemObjDarwinReadPhys((u64.u & ~(uint64_t)PAGE_OFFSET_MASK) | (((uintptr_t)pvPage >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64) * 8, 8, &u64);
287 if (!(u64.u & X86_PDPE_P))
288 {
289 printf("rtR0MemObjDarwinGetPTE: %p -> PDPTE !p\n", pvPage);
290 return 0;
291 }
292 if (u64.u & X86_PDPE_LM_PS)
293 return (u64.u & ~(uint64_t)(_1G -1)) | ((uintptr_t)pvPage & (_1G -1));
294
295 /* PD */
296 rtR0MemObjDarwinReadPhys((u64.u & ~(uint64_t)PAGE_OFFSET_MASK) | (((uintptr_t)pvPage >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * 8, 8, &u64);
297 if (!(u64.u & X86_PDE_P))
298 {
299 printf("rtR0MemObjDarwinGetPTE: %p -> PDE !p\n", pvPage);
300 return 0;
301 }
302 if (u64.u & X86_PDE_PS)
303 return (u64.u & ~(uint64_t)(_2M -1)) | ((uintptr_t)pvPage & (_2M -1));
304
305 /* PT */
306 rtR0MemObjDarwinReadPhys((u64.u & ~(uint64_t)PAGE_OFFSET_MASK) | (((uintptr_t)pvPage >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK) * 8, 8, &u64);
307 if (!(u64.u & X86_PTE_P))
308 {
309 printf("rtR0MemObjDarwinGetPTE: %p -> PTE !p\n", pvPage);
310 return 0;
311 }
312 return u64.u;
313 }
314
315 if (fPAE)
316 {
317 /* PDPTR */
318 rtR0MemObjDarwinReadPhys((u64.u & X86_CR3_PAE_PAGE_MASK) | (((uintptr_t)pvPage >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE) * 8, 8, &u64);
319 if (!(u64.u & X86_PDE_P))
320 return 0;
321
322 /* PD */
323 rtR0MemObjDarwinReadPhys((u64.u & ~(uint64_t)PAGE_OFFSET_MASK) | (((uintptr_t)pvPage >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK) * 8, 8, &u64);
324 if (!(u64.u & X86_PDE_P))
325 return 0;
326 if (u64.u & X86_PDE_PS)
327 return (u64.u & ~(uint64_t)(_2M -1)) | ((uintptr_t)pvPage & (_2M -1));
328
329 /* PT */
330 rtR0MemObjDarwinReadPhys((u64.u & ~(uint64_t)PAGE_OFFSET_MASK) | (((uintptr_t)pvPage >> X86_PT_PAE_SHIFT) & X86_PT_PAE_MASK) * 8, 8, &u64);
331 if (!(u64.u & X86_PTE_P))
332 return 0;
333 return u64.u;
334 }
335
336 /* PD */
337 rtR0MemObjDarwinReadPhys((u64.au32[0] & ~(uint32_t)PAGE_OFFSET_MASK) | (((uintptr_t)pvPage >> X86_PD_SHIFT) & X86_PD_MASK) * 4, 4, &u64);
338 if (!(u64.au32[0] & X86_PDE_P))
339 return 0;
340 if (u64.au32[0] & X86_PDE_PS)
341 return (u64.u & ~(uint64_t)(_2M -1)) | ((uintptr_t)pvPage & (_2M -1));
342
343 /* PT */
344 rtR0MemObjDarwinReadPhys((u64.au32[0] & ~(uint32_t)PAGE_OFFSET_MASK) | (((uintptr_t)pvPage >> X86_PT_SHIFT) & X86_PT_MASK) * 4, 4, &u64);
345 if (!(u64.au32[0] & X86_PTE_P))
346 return 0;
347 return u64.au32[0];
348
349 return 0;
350}
351
352#endif /* RT_STRICT */
353
354DECLHIDDEN(int) rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
355{
356 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
357
358 /*
359 * Release the IOMemoryDescriptor or/and IOMemoryMap associated with the object.
360 */
361 if (pMemDarwin->pMemDesc)
362 {
363 if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
364 pMemDarwin->pMemDesc->complete(); /* paranoia */
365 pMemDarwin->pMemDesc->release();
366 pMemDarwin->pMemDesc = NULL;
367 }
368
369 if (pMemDarwin->pMemMap)
370 {
371 pMemDarwin->pMemMap->release();
372 pMemDarwin->pMemMap = NULL;
373 }
374
375 /*
376 * Release any memory that we've allocated or locked.
377 */
378 switch (pMemDarwin->Core.enmType)
379 {
380 case RTR0MEMOBJTYPE_LOW:
381 case RTR0MEMOBJTYPE_PAGE:
382 case RTR0MEMOBJTYPE_CONT:
383 break;
384
385 case RTR0MEMOBJTYPE_LOCK:
386 {
387#ifdef USE_VM_MAP_WIRE
388 vm_map_t Map = pMemDarwin->Core.u.Lock.R0Process != NIL_RTR0PROCESS
389 ? get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process)
390 : kernel_map;
391 kern_return_t kr = vm_map_unwire(Map,
392 (vm_map_offset_t)pMemDarwin->Core.pv,
393 (vm_map_offset_t)pMemDarwin->Core.pv + pMemDarwin->Core.cb,
394 0 /* not user */);
395 AssertRC(kr == KERN_SUCCESS); /** @todo don't ignore... */
396#endif
397 break;
398 }
399
400 case RTR0MEMOBJTYPE_PHYS:
401 /*if (pMemDarwin->Core.u.Phys.fAllocated)
402 IOFreePhysical(pMemDarwin->Core.u.Phys.PhysBase, pMemDarwin->Core.cb);*/
403 Assert(!pMemDarwin->Core.u.Phys.fAllocated);
404 break;
405
406 case RTR0MEMOBJTYPE_PHYS_NC:
407 AssertMsgFailed(("RTR0MEMOBJTYPE_PHYS_NC\n"));
408 return VERR_INTERNAL_ERROR;
409
410 case RTR0MEMOBJTYPE_RES_VIRT:
411 AssertMsgFailed(("RTR0MEMOBJTYPE_RES_VIRT\n"));
412 return VERR_INTERNAL_ERROR;
413
414 case RTR0MEMOBJTYPE_MAPPING:
415 /* nothing to do here. */
416 break;
417
418 default:
419 AssertMsgFailed(("enmType=%d\n", pMemDarwin->Core.enmType));
420 return VERR_INTERNAL_ERROR;
421 }
422
423 return VINF_SUCCESS;
424}
425
426
427
428/**
429 * Kernel memory alloc worker that uses inTaskWithPhysicalMask.
430 *
431 * @returns IPRT status code.
432 * @retval VERR_ADDRESS_TOO_BIG try another way.
433 *
434 * @param ppMem Where to return the memory object.
435 * @param cb The page aligned memory size.
436 * @param fExecutable Whether the mapping needs to be executable.
437 * @param fContiguous Whether the backing memory needs to be contiguous.
438 * @param PhysMask The mask for the backing memory (i.e. range). Use 0 if
439 * you don't care that much or is speculating.
440 * @param MaxPhysAddr The max address to verify the result against. Use
441 * UINT64_MAX if it doesn't matter.
442 * @param enmType The object type.
443 */
444static int rtR0MemObjNativeAllocWorker(PPRTR0MEMOBJINTERNAL ppMem, size_t cb,
445 bool fExecutable, bool fContiguous,
446 mach_vm_address_t PhysMask, uint64_t MaxPhysAddr,
447 RTR0MEMOBJTYPE enmType)
448{
449 /*
450 * Try inTaskWithPhysicalMask first, but since we don't quite trust that it
451 * actually respects the physical memory mask (10.5.x is certainly busted),
452 * we'll use rtR0MemObjNativeAllocCont as a fallback for dealing with that.
453 *
454 * The kIOMemoryKernelUserShared flag just forces the result to be page aligned.
455 */
456#if 1 /** @todo Figure out why this is broken. Is it only on snow leopard? Seen allocating memory for the VM structure, last page corrupted or inaccessible. */
457 size_t const cbFudged = cb + PAGE_SIZE;
458#else
459 size_t const cbFudged = cb;
460#endif
461 int rc;
462 IOBufferMemoryDescriptor *pMemDesc =
463 IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task,
464 kIOMemoryKernelUserShared
465 | kIODirectionInOut
466 | (fContiguous ? kIOMemoryPhysicallyContiguous : 0),
467 cbFudged,
468 PhysMask);
469 if (pMemDesc)
470 {
471 IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
472 if (IORet == kIOReturnSuccess)
473 {
474 void *pv = pMemDesc->getBytesNoCopy(0, cbFudged);
475 if (pv)
476 {
477 /*
478 * Check if it's all below 4GB.
479 */
480 addr64_t AddrPrev = 0;
481 MaxPhysAddr &= ~(uint64_t)PAGE_OFFSET_MASK;
482 for (IOByteCount off = 0; off < cb; off += PAGE_SIZE)
483 {
484#ifdef __LP64__ /* Grumble! */
485 addr64_t Addr = pMemDesc->getPhysicalSegment(off, NULL);
486#else
487 addr64_t Addr = pMemDesc->getPhysicalSegment64(off, NULL);
488#endif
489 if ( Addr > MaxPhysAddr
490 || !Addr
491 || (Addr & PAGE_OFFSET_MASK)
492 || ( fContiguous
493 && !off
494 && Addr == AddrPrev + PAGE_SIZE))
495 {
496 /* Buggy API, try allocate the memory another way. */
497 pMemDesc->release();
498 if (PhysMask)
499 LogAlways(("rtR0MemObjNativeAllocWorker: off=%x Addr=%llx AddrPrev=%llx MaxPhysAddr=%llx PhysMas=%llx - buggy API!\n",
500 off, Addr, AddrPrev, MaxPhysAddr, PhysMask));
501 return VERR_ADDRESS_TOO_BIG;
502 }
503 AddrPrev = Addr;
504 }
505
506#ifdef RT_STRICT
507 /* check that the memory is actually mapped. */
508 //addr64_t Addr = pMemDesc->getPhysicalSegment64(0, NULL);
509 //printf("rtR0MemObjNativeAllocWorker: pv=%p %8llx %8llx\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr);
510 RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
511 RTThreadPreemptDisable(&State);
512 rtR0MemObjDarwinTouchPages(pv, cb);
513 RTThreadPreemptRestore(&State);
514#endif
515
516 /*
517 * Create the IPRT memory object.
518 */
519 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), enmType, pv, cb);
520 if (pMemDarwin)
521 {
522 if (fContiguous)
523 {
524#ifdef __LP64__ /* Grumble! */
525 addr64_t PhysBase64 = pMemDesc->getPhysicalSegment(0, NULL);
526#else
527 addr64_t PhysBase64 = pMemDesc->getPhysicalSegment64(0, NULL);
528#endif
529 RTHCPHYS PhysBase = PhysBase64; Assert(PhysBase == PhysBase64);
530 if (enmType == RTR0MEMOBJTYPE_CONT)
531 pMemDarwin->Core.u.Cont.Phys = PhysBase;
532 else if (enmType == RTR0MEMOBJTYPE_PHYS)
533 pMemDarwin->Core.u.Phys.PhysBase = PhysBase;
534 else
535 AssertMsgFailed(("enmType=%d\n", enmType));
536 }
537
538#if 1 /* Experimental code. */
539 if (fExecutable)
540 {
541 rc = rtR0MemObjNativeProtect(&pMemDarwin->Core, 0, cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
542# ifdef RT_STRICT
543 /* check that the memory is actually mapped. */
544 RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
545 RTThreadPreemptDisable(&State);
546 rtR0MemObjDarwinTouchPages(pv, cb);
547 RTThreadPreemptRestore(&State);
548# endif
549 }
550 else
551#endif
552 rc = VINF_SUCCESS;
553 if (RT_SUCCESS(rc))
554 {
555 pMemDarwin->pMemDesc = pMemDesc;
556 *ppMem = &pMemDarwin->Core;
557 return VINF_SUCCESS;
558 }
559
560 rtR0MemObjDelete(&pMemDarwin->Core);
561 }
562
563 if (enmType == RTR0MEMOBJTYPE_PHYS_NC)
564 rc = VERR_NO_PHYS_MEMORY;
565 else if (enmType == RTR0MEMOBJTYPE_LOW)
566 rc = VERR_NO_LOW_MEMORY;
567 else if (enmType == RTR0MEMOBJTYPE_CONT)
568 rc = VERR_NO_CONT_MEMORY;
569 else
570 rc = VERR_NO_MEMORY;
571 }
572 else
573 rc = VERR_MEMOBJ_INIT_FAILED;
574 }
575 else
576 rc = RTErrConvertFromDarwinIO(IORet);
577 pMemDesc->release();
578 }
579 else
580 rc = VERR_MEMOBJ_INIT_FAILED;
581 Assert(rc != VERR_ADDRESS_TOO_BIG);
582 return rc;
583}
584
585
586DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
587{
588 return rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
589 0 /* PhysMask */, UINT64_MAX, RTR0MEMOBJTYPE_PAGE);
590}
591
592
593DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
594{
595 /*
596 * Try IOMallocPhysical/IOMallocAligned first.
597 * Then try optimistically without a physical address mask, which will always
598 * end up using IOMallocAligned.
599 *
600 * (See bug comment in the worker and IOBufferMemoryDescriptor::initWithPhysicalMask.)
601 */
602 int rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
603 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE, RTR0MEMOBJTYPE_LOW);
604 if (rc == VERR_ADDRESS_TOO_BIG)
605 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
606 0 /* PhysMask */, _4G - PAGE_SIZE, RTR0MEMOBJTYPE_LOW);
607 return rc;
608}
609
610
611DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
612{
613 int rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, true /* fContiguous */,
614 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE,
615 RTR0MEMOBJTYPE_CONT);
616
617 /*
618 * Workaround for bogus IOKernelAllocateContiguous behavior, just in case.
619 * cb <= PAGE_SIZE allocations take a different path, using a different allocator.
620 */
621 if (RT_FAILURE(rc) && cb <= PAGE_SIZE)
622 rc = rtR0MemObjNativeAllocWorker(ppMem, cb + PAGE_SIZE, fExecutable, true /* fContiguous */,
623 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE,
624 RTR0MEMOBJTYPE_CONT);
625 return rc;
626}
627
628
629DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
630{
631 /** @todo alignment */
632 if (uAlignment != PAGE_SIZE)
633 return VERR_NOT_SUPPORTED;
634
635 /*
636 * Translate the PhysHighest address into a mask.
637 */
638 int rc;
639 if (PhysHighest == NIL_RTHCPHYS)
640 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, true /* fExecutable */, true /* fContiguous */,
641 0 /* PhysMask*/, UINT64_MAX, RTR0MEMOBJTYPE_PHYS);
642 else
643 {
644 mach_vm_address_t PhysMask = 0;
645 PhysMask = ~(mach_vm_address_t)0;
646 while (PhysMask > (PhysHighest | PAGE_OFFSET_MASK))
647 PhysMask >>= 1;
648 AssertReturn(PhysMask + 1 <= cb, VERR_INVALID_PARAMETER);
649 PhysMask &= ~(mach_vm_address_t)PAGE_OFFSET_MASK;
650
651 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, true /* fExecutable */, true /* fContiguous */,
652 PhysMask, PhysHighest, RTR0MEMOBJTYPE_PHYS);
653 }
654 return rc;
655}
656
657
658DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
659{
660 /** @todo rtR0MemObjNativeAllocPhys / darwin.
661 * This might be a bit problematic and may very well require having to create our own
662 * object which we populate with pages but without mapping it into any address space.
663 * Estimate is 2-3 days.
664 */
665 return VERR_NOT_SUPPORTED;
666}
667
668
669DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
670{
671 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
672
673 /*
674 * Create a descriptor for it (the validation is always true on intel macs, but
675 * as it doesn't harm us keep it in).
676 */
677 int rc = VERR_ADDRESS_TOO_BIG;
678 IOAddressRange aRanges[1] = { { Phys, cb } };
679 if ( aRanges[0].address == Phys
680 && aRanges[0].length == cb)
681 {
682 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddressRanges(&aRanges[0], RT_ELEMENTS(aRanges),
683 kIODirectionInOut, NULL /*task*/);
684 if (pMemDesc)
685 {
686#ifdef __LP64__ /* Grumble! */
687 Assert(Phys == pMemDesc->getPhysicalSegment(0, 0));
688#else
689 Assert(Phys == pMemDesc->getPhysicalSegment64(0, 0));
690#endif
691
692 /*
693 * Create the IPRT memory object.
694 */
695 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
696 if (pMemDarwin)
697 {
698 pMemDarwin->Core.u.Phys.PhysBase = Phys;
699 pMemDarwin->Core.u.Phys.fAllocated = false;
700 pMemDarwin->Core.u.Phys.uCachePolicy = uCachePolicy;
701 pMemDarwin->pMemDesc = pMemDesc;
702 *ppMem = &pMemDarwin->Core;
703 return VINF_SUCCESS;
704 }
705
706 rc = VERR_NO_MEMORY;
707 pMemDesc->release();
708 }
709 else
710 rc = VERR_MEMOBJ_INIT_FAILED;
711 }
712 else
713 AssertMsgFailed(("%#llx %llx\n", (unsigned long long)Phys, (unsigned long long)cb));
714 return rc;
715}
716
717
718/**
719 * Internal worker for locking down pages.
720 *
721 * @return IPRT status code.
722 *
723 * @param ppMem Where to store the memory object pointer.
724 * @param pv First page.
725 * @param cb Number of bytes.
726 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
727 * and RTMEM_PROT_WRITE.
728 * @param Task The task \a pv and \a cb refers to.
729 */
730static int rtR0MemObjNativeLock(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess, task_t Task)
731{
732 NOREF(fAccess);
733#ifdef USE_VM_MAP_WIRE
734 vm_map_t Map = get_task_map(Task);
735 Assert(Map);
736
737 /*
738 * First try lock the memory.
739 */
740 int rc = VERR_LOCK_FAILED;
741 kern_return_t kr = vm_map_wire(get_task_map(Task),
742 (vm_map_offset_t)pv,
743 (vm_map_offset_t)pv + cb,
744 VM_PROT_DEFAULT,
745 0 /* not user */);
746 if (kr == KERN_SUCCESS)
747 {
748 /*
749 * Create the IPRT memory object.
750 */
751 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
752 if (pMemDarwin)
753 {
754 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
755 *ppMem = &pMemDarwin->Core;
756 return VINF_SUCCESS;
757 }
758
759 kr = vm_map_unwire(get_task_map(Task), (vm_map_offset_t)pv, (vm_map_offset_t)pv + cb, 0 /* not user */);
760 Assert(kr == KERN_SUCCESS);
761 rc = VERR_NO_MEMORY;
762 }
763
764#else
765
766 /*
767 * Create a descriptor and try lock it (prepare).
768 */
769 int rc = VERR_MEMOBJ_INIT_FAILED;
770 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddressRange((vm_address_t)pv, cb, kIODirectionInOut, Task);
771 if (pMemDesc)
772 {
773 IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
774 if (IORet == kIOReturnSuccess)
775 {
776 /*
777 * Create the IPRT memory object.
778 */
779 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
780 if (pMemDarwin)
781 {
782 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
783 pMemDarwin->pMemDesc = pMemDesc;
784 *ppMem = &pMemDarwin->Core;
785 return VINF_SUCCESS;
786 }
787
788 pMemDesc->complete();
789 rc = VERR_NO_MEMORY;
790 }
791 else
792 rc = VERR_LOCK_FAILED;
793 pMemDesc->release();
794 }
795#endif
796 return rc;
797}
798
799
800DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
801{
802 return rtR0MemObjNativeLock(ppMem, (void *)R3Ptr, cb, fAccess, (task_t)R0Process);
803}
804
805
806DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
807{
808 return rtR0MemObjNativeLock(ppMem, pv, cb, fAccess, kernel_task);
809}
810
811
812DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
813{
814 return VERR_NOT_SUPPORTED;
815}
816
817
818DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
819{
820 return VERR_NOT_SUPPORTED;
821}
822
823
824DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
825 unsigned fProt, size_t offSub, size_t cbSub)
826{
827 AssertReturn(pvFixed == (void *)-1, VERR_NOT_SUPPORTED);
828
829 /*
830 * Check that the specified alignment is supported.
831 */
832 if (uAlignment > PAGE_SIZE)
833 return VERR_NOT_SUPPORTED;
834
835 /*
836 * Must have a memory descriptor that we can map.
837 */
838 int rc = VERR_INVALID_PARAMETER;
839 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
840 if (pMemToMapDarwin->pMemDesc)
841 {
842#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
843 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->createMappingInTask(kernel_task,
844 0,
845 kIOMapAnywhere | kIOMapDefaultCache,
846 offSub,
847 cbSub);
848#else
849 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map(kernel_task,
850 0,
851 kIOMapAnywhere | kIOMapDefaultCache,
852 offSub,
853 cbSub);
854#endif
855 if (pMemMap)
856 {
857 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
858 void *pv = (void *)(uintptr_t)VirtAddr;
859 if ((uintptr_t)pv == VirtAddr)
860 {
861 //addr64_t Addr = pMemToMapDarwin->pMemDesc->getPhysicalSegment64(offSub, NULL);
862 //printf("pv=%p: %8llx %8llx\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr);
863
864// /*
865// * Explicitly lock it so that we're sure it is present and that
866// * its PTEs cannot be recycled.
867// * Note! withAddressRange() doesn't work as it adds kIOMemoryTypeVirtual64
868// * to the options which causes prepare() to not wire the pages.
869// * This is probably a bug.
870// */
871// IOAddressRange Range = { (mach_vm_address_t)pv, cbSub };
872// IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withOptions(&Range,
873// 1 /* count */,
874// 0 /* offset */,
875// kernel_task,
876// kIODirectionInOut | kIOMemoryTypeVirtual,
877// kIOMapperSystem);
878// if (pMemDesc)
879// {
880// IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
881// if (IORet == kIOReturnSuccess)
882// {
883 /* HACK ALERT! */
884 rtR0MemObjDarwinTouchPages(pv, cbSub);
885 /** @todo First, the memory should've been mapped by now, and second, it
886 * should have the wired attribute in the PTE (bit 9). Neither
887 * seems to be the case. The disabled locking code doesn't make any
888 * difference, which is extremely odd, and breaks
889 * rtR0MemObjNativeGetPagePhysAddr (getPhysicalSegment64 -> 64 for the
890 * lock descriptor. */
891 //addr64_t Addr = pMemDesc->getPhysicalSegment64(0, NULL);
892 //printf("pv=%p: %8llx %8llx (%d)\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr, 2);
893
894 /*
895 * Create the IPRT memory object.
896 */
897 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
898 pv, cbSub);
899 if (pMemDarwin)
900 {
901 pMemDarwin->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
902 pMemDarwin->pMemMap = pMemMap;
903// pMemDarwin->pMemDesc = pMemDesc;
904 *ppMem = &pMemDarwin->Core;
905 return VINF_SUCCESS;
906 }
907
908// pMemDesc->complete();
909// rc = VERR_NO_MEMORY;
910// }
911// else
912// rc = RTErrConvertFromDarwinIO(IORet);
913// pMemDesc->release();
914// }
915// else
916// rc = VERR_MEMOBJ_INIT_FAILED;
917 }
918 else
919 rc = VERR_ADDRESS_TOO_BIG;
920 pMemMap->release();
921 }
922 else
923 rc = VERR_MAP_FAILED;
924 }
925 return rc;
926}
927
928
929DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
930{
931 /*
932 * Check for unsupported things.
933 */
934 AssertReturn(R3PtrFixed == (RTR3PTR)-1, VERR_NOT_SUPPORTED);
935 if (uAlignment > PAGE_SIZE)
936 return VERR_NOT_SUPPORTED;
937
938 /*
939 * Must have a memory descriptor.
940 */
941 int rc = VERR_INVALID_PARAMETER;
942 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
943 if (pMemToMapDarwin->pMemDesc)
944 {
945#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
946 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->createMappingInTask((task_t)R0Process,
947 0,
948 kIOMapAnywhere | kIOMapDefaultCache,
949 0 /* offset */,
950 0 /* length */);
951#else
952 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map((task_t)R0Process,
953 0,
954 kIOMapAnywhere | kIOMapDefaultCache);
955#endif
956 if (pMemMap)
957 {
958 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
959 void *pv = (void *)(uintptr_t)VirtAddr;
960 if ((uintptr_t)pv == VirtAddr)
961 {
962 /*
963 * Create the IPRT memory object.
964 */
965 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
966 pv, pMemToMapDarwin->Core.cb);
967 if (pMemDarwin)
968 {
969 pMemDarwin->Core.u.Mapping.R0Process = R0Process;
970 pMemDarwin->pMemMap = pMemMap;
971 *ppMem = &pMemDarwin->Core;
972 return VINF_SUCCESS;
973 }
974
975 rc = VERR_NO_MEMORY;
976 }
977 else
978 rc = VERR_ADDRESS_TOO_BIG;
979 pMemMap->release();
980 }
981 else
982 rc = VERR_MAP_FAILED;
983 }
984 return rc;
985}
986
987
988DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
989{
990 /* Get the map for the object. */
991 vm_map_t pVmMap = rtR0MemObjDarwinGetMap(pMem);
992 if (!pVmMap)
993 return VERR_NOT_SUPPORTED;
994
995 /*
996 * Convert the protection.
997 */
998 vm_prot_t fMachProt;
999 switch (fProt)
1000 {
1001 case RTMEM_PROT_NONE:
1002 fMachProt = VM_PROT_NONE;
1003 break;
1004 case RTMEM_PROT_READ:
1005 fMachProt = VM_PROT_READ;
1006 break;
1007 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
1008 fMachProt = VM_PROT_READ | VM_PROT_WRITE;
1009 break;
1010 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
1011 fMachProt = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
1012 break;
1013 case RTMEM_PROT_WRITE:
1014 fMachProt = VM_PROT_WRITE | VM_PROT_READ; /* never write-only */
1015 break;
1016 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
1017 fMachProt = VM_PROT_WRITE | VM_PROT_EXECUTE | VM_PROT_READ; /* never write-only or execute-only */
1018 break;
1019 case RTMEM_PROT_EXEC:
1020 fMachProt = VM_PROT_EXECUTE | VM_PROT_READ; /* never execute-only */
1021 break;
1022 default:
1023 AssertFailedReturn(VERR_INVALID_PARAMETER);
1024 }
1025
1026 /*
1027 * Do the job.
1028 */
1029 vm_offset_t Start = (uintptr_t)pMem->pv + offSub;
1030 kern_return_t krc = vm_protect(pVmMap,
1031 Start,
1032 cbSub,
1033 false,
1034 fMachProt);
1035 if (krc != KERN_SUCCESS)
1036 return RTErrConvertFromDarwinKern(krc);
1037
1038 /*
1039 * Touch the pages if they should be writable afterwards and accessible
1040 * from code which should never fault. vm_protect() may leave pages
1041 * temporarily write protected, possibly due to pmap no-upgrade rules?
1042 *
1043 * This is the same trick (or HACK ALERT if you like) as applied in
1044 * rtR0MemObjNativeMapKernel.
1045 */
1046 if ( pMem->enmType != RTR0MEMOBJTYPE_MAPPING
1047 || pMem->u.Mapping.R0Process == NIL_RTR0PROCESS)
1048 {
1049 if (fProt & RTMEM_PROT_WRITE)
1050 rtR0MemObjDarwinTouchPages((void *)Start, cbSub);
1051 /*
1052 * Sniff (read) read-only pages too, just to be sure.
1053 */
1054 else if (fProt & (RTMEM_PROT_READ | RTMEM_PROT_EXEC))
1055 rtR0MemObjDarwinSniffPages((void const *)Start, cbSub);
1056 }
1057
1058 return VINF_SUCCESS;
1059}
1060
1061
1062DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1063{
1064 RTHCPHYS PhysAddr;
1065 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
1066
1067#ifdef USE_VM_MAP_WIRE
1068 /*
1069 * Locked memory doesn't have a memory descriptor and
1070 * needs to be handled differently.
1071 */
1072 if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
1073 {
1074 ppnum_t PgNo;
1075 if (pMemDarwin->Core.u.Lock.R0Process == NIL_RTR0PROCESS)
1076 PgNo = pmap_find_phys(kernel_pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
1077 else
1078 {
1079 /*
1080 * From what I can tell, Apple seems to have locked up the all the
1081 * available interfaces that could help us obtain the pmap_t of a task
1082 * or vm_map_t.
1083
1084 * So, we'll have to figure out where in the vm_map_t structure it is
1085 * and read it our selves. ASSUMING that kernel_pmap is pointed to by
1086 * kernel_map->pmap, we scan kernel_map to locate the structure offset.
1087 * Not nice, but it will hopefully do the job in a reliable manner...
1088 *
1089 * (get_task_pmap, get_map_pmap or vm_map_pmap is what we really need btw.)
1090 */
1091 static int s_offPmap = -1;
1092 if (RT_UNLIKELY(s_offPmap == -1))
1093 {
1094 pmap_t const *p = (pmap_t *)kernel_map;
1095 pmap_t const * const pEnd = p + 64;
1096 for (; p < pEnd; p++)
1097 if (*p == kernel_pmap)
1098 {
1099 s_offPmap = (uintptr_t)p - (uintptr_t)kernel_map;
1100 break;
1101 }
1102 AssertReturn(s_offPmap >= 0, NIL_RTHCPHYS);
1103 }
1104 pmap_t Pmap = *(pmap_t *)((uintptr_t)get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process) + s_offPmap);
1105 PgNo = pmap_find_phys(Pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
1106 }
1107
1108 AssertReturn(PgNo, NIL_RTHCPHYS);
1109 PhysAddr = (RTHCPHYS)PgNo << PAGE_SHIFT;
1110 Assert((PhysAddr >> PAGE_SHIFT) == PgNo);
1111 }
1112 else
1113#endif /* USE_VM_MAP_WIRE */
1114 {
1115 /*
1116 * Get the memory descriptor.
1117 */
1118 IOMemoryDescriptor *pMemDesc = pMemDarwin->pMemDesc;
1119 if (!pMemDesc)
1120 pMemDesc = pMemDarwin->pMemMap->getMemoryDescriptor();
1121 AssertReturn(pMemDesc, NIL_RTHCPHYS);
1122
1123 /*
1124 * If we've got a memory descriptor, use getPhysicalSegment64().
1125 */
1126#ifdef __LP64__ /* Grumble! */
1127 addr64_t Addr = pMemDesc->getPhysicalSegment(iPage * PAGE_SIZE, NULL);
1128#else
1129 addr64_t Addr = pMemDesc->getPhysicalSegment64(iPage * PAGE_SIZE, NULL);
1130#endif
1131 AssertMsgReturn(Addr, ("iPage=%u\n", iPage), NIL_RTHCPHYS);
1132 PhysAddr = Addr;
1133 AssertMsgReturn(PhysAddr == Addr, ("PhysAddr=%RHp Addr=%RX64\n", PhysAddr, (uint64_t)Addr), NIL_RTHCPHYS);
1134 }
1135
1136 return PhysAddr;
1137}
1138
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