VirtualBox

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

Last change on this file since 43974 was 43375, checked in by vboxsync, 12 years ago

memobj-r0drv-darwin.cpp: 10.8.2 fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 43.8 KB
Line 
1/* $Id: memobj-r0drv-darwin.cpp 43375 2012-09-20 17:15:53Z 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 pMemDarwin->pMemDesc->complete();
364 pMemDarwin->pMemDesc->release();
365 pMemDarwin->pMemDesc = NULL;
366 }
367
368 if (pMemDarwin->pMemMap)
369 {
370 pMemDarwin->pMemMap->release();
371 pMemDarwin->pMemMap = NULL;
372 }
373
374 /*
375 * Release any memory that we've allocated or locked.
376 */
377 switch (pMemDarwin->Core.enmType)
378 {
379 case RTR0MEMOBJTYPE_LOW:
380 case RTR0MEMOBJTYPE_PAGE:
381 case RTR0MEMOBJTYPE_CONT:
382 break;
383
384 case RTR0MEMOBJTYPE_LOCK:
385 {
386#ifdef USE_VM_MAP_WIRE
387 vm_map_t Map = pMemDarwin->Core.u.Lock.R0Process != NIL_RTR0PROCESS
388 ? get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process)
389 : kernel_map;
390 kern_return_t kr = vm_map_unwire(Map,
391 (vm_map_offset_t)pMemDarwin->Core.pv,
392 (vm_map_offset_t)pMemDarwin->Core.pv + pMemDarwin->Core.cb,
393 0 /* not user */);
394 AssertRC(kr == KERN_SUCCESS); /** @todo don't ignore... */
395#endif
396 break;
397 }
398
399 case RTR0MEMOBJTYPE_PHYS:
400 /*if (pMemDarwin->Core.u.Phys.fAllocated)
401 IOFreePhysical(pMemDarwin->Core.u.Phys.PhysBase, pMemDarwin->Core.cb);*/
402 Assert(!pMemDarwin->Core.u.Phys.fAllocated);
403 break;
404
405 case RTR0MEMOBJTYPE_PHYS_NC:
406 AssertMsgFailed(("RTR0MEMOBJTYPE_PHYS_NC\n"));
407 return VERR_INTERNAL_ERROR;
408
409 case RTR0MEMOBJTYPE_RES_VIRT:
410 AssertMsgFailed(("RTR0MEMOBJTYPE_RES_VIRT\n"));
411 return VERR_INTERNAL_ERROR;
412
413 case RTR0MEMOBJTYPE_MAPPING:
414 /* nothing to do here. */
415 break;
416
417 default:
418 AssertMsgFailed(("enmType=%d\n", pMemDarwin->Core.enmType));
419 return VERR_INTERNAL_ERROR;
420 }
421
422 return VINF_SUCCESS;
423}
424
425
426
427/**
428 * Kernel memory alloc worker that uses inTaskWithPhysicalMask.
429 *
430 * @returns IPRT status code.
431 * @retval VERR_ADDRESS_TOO_BIG try another way.
432 *
433 * @param ppMem Where to return the memory object.
434 * @param cb The page aligned memory size.
435 * @param fExecutable Whether the mapping needs to be executable.
436 * @param fContiguous Whether the backing memory needs to be contiguous.
437 * @param PhysMask The mask for the backing memory (i.e. range). Use 0 if
438 * you don't care that much or is speculating.
439 * @param MaxPhysAddr The max address to verify the result against. Use
440 * UINT64_MAX if it doesn't matter.
441 * @param enmType The object type.
442 */
443static int rtR0MemObjNativeAllocWorker(PPRTR0MEMOBJINTERNAL ppMem, size_t cb,
444 bool fExecutable, bool fContiguous,
445 mach_vm_address_t PhysMask, uint64_t MaxPhysAddr,
446 RTR0MEMOBJTYPE enmType)
447{
448 /*
449 * Try inTaskWithPhysicalMask first, but since we don't quite trust that it
450 * actually respects the physical memory mask (10.5.x is certainly busted),
451 * we'll use rtR0MemObjNativeAllocCont as a fallback for dealing with that.
452 *
453 * The kIOMemoryKernelUserShared flag just forces the result to be page aligned.
454 *
455 * The kIOMemoryMapperNone flag is required since 10.8.2 (IOMMU changes?).
456 */
457 int rc;
458 size_t cbFudged = cb;
459 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. */
460 cbFudged += PAGE_SIZE;
461#if 1
462 IOOptionBits fOptions = kIOMemoryKernelUserShared | kIODirectionInOut;
463 if (fContiguous)
464 fOptions |= kIOMemoryPhysicallyContiguous;
465 if (version_major >= 12 /* 12 = 10.8.x = Mountain Kitten */)
466 fOptions |= kIOMemoryMapperNone;
467 IOBufferMemoryDescriptor *pMemDesc = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task, fOptions,
468 cbFudged, PhysMask);
469#else /* Requires 10.7 SDK, but allows alignment to be specified: */
470 uint64_t uAlignment = PAGE_SIZE;
471 IOOptionBits fOptions = kIODirectionInOut | kIOMemoryMapperNone;
472 if (fContiguous || MaxPhysAddr < UINT64_MAX)
473 {
474 fOptions |= kIOMemoryPhysicallyContiguous;
475 uAlignment = 1; /* PhysMask isn't respected if higher. */
476 }
477
478 IOBufferMemoryDescriptor *pMemDesc = new IOBufferMemoryDescriptor;
479 if (pMemDesc && !pMemDesc->initWithPhysicalMask(kernel_task, fOptions, cbFudged, uAlignment, PhysMask))
480 {
481 pMemDesc->release();
482 pMemDesc = NULL;
483 }
484#endif
485 if (pMemDesc)
486 {
487 IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
488 if (IORet == kIOReturnSuccess)
489 {
490 void *pv = pMemDesc->getBytesNoCopy(0, cbFudged);
491 if (pv)
492 {
493 /*
494 * Check if it's all below 4GB.
495 */
496 addr64_t AddrPrev = 0;
497 MaxPhysAddr &= ~(uint64_t)PAGE_OFFSET_MASK;
498 for (IOByteCount off = 0; off < cb; off += PAGE_SIZE)
499 {
500#ifdef __LP64__
501 addr64_t Addr = pMemDesc->getPhysicalSegment(off, NULL, kIOMemoryMapperNone);
502#else
503 addr64_t Addr = pMemDesc->getPhysicalSegment64(off, NULL);
504#endif
505 if ( Addr > MaxPhysAddr
506 || !Addr
507 || (Addr & PAGE_OFFSET_MASK)
508 || ( fContiguous
509 && !off
510 && Addr == AddrPrev + PAGE_SIZE))
511 {
512 /* Buggy API, try allocate the memory another way. */
513 pMemDesc->complete();
514 pMemDesc->release();
515 if (PhysMask)
516 LogRel(("rtR0MemObjNativeAllocWorker: off=%x Addr=%llx AddrPrev=%llx MaxPhysAddr=%llx PhysMas=%llx fContiguous=%RTbool fOptions=%#x - buggy API!\n",
517 off, Addr, AddrPrev, MaxPhysAddr, PhysMask, fContiguous, fOptions));
518 return VERR_ADDRESS_TOO_BIG;
519 }
520 AddrPrev = Addr;
521 }
522
523#ifdef RT_STRICT
524 /* check that the memory is actually mapped. */
525 //addr64_t Addr = pMemDesc->getPhysicalSegment64(0, NULL);
526 //printf("rtR0MemObjNativeAllocWorker: pv=%p %8llx %8llx\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr);
527 RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
528 RTThreadPreemptDisable(&State);
529 rtR0MemObjDarwinTouchPages(pv, cb);
530 RTThreadPreemptRestore(&State);
531#endif
532
533 /*
534 * Create the IPRT memory object.
535 */
536 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), enmType, pv, cb);
537 if (pMemDarwin)
538 {
539 if (fContiguous)
540 {
541#ifdef __LP64__
542 addr64_t PhysBase64 = pMemDesc->getPhysicalSegment(0, NULL, kIOMemoryMapperNone);
543#else
544 addr64_t PhysBase64 = pMemDesc->getPhysicalSegment64(0, NULL);
545#endif
546 RTHCPHYS PhysBase = PhysBase64; Assert(PhysBase == PhysBase64);
547 if (enmType == RTR0MEMOBJTYPE_CONT)
548 pMemDarwin->Core.u.Cont.Phys = PhysBase;
549 else if (enmType == RTR0MEMOBJTYPE_PHYS)
550 pMemDarwin->Core.u.Phys.PhysBase = PhysBase;
551 else
552 AssertMsgFailed(("enmType=%d\n", enmType));
553 }
554
555#if 1 /* Experimental code. */
556 if (fExecutable)
557 {
558 rc = rtR0MemObjNativeProtect(&pMemDarwin->Core, 0, cb, RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
559# ifdef RT_STRICT
560 /* check that the memory is actually mapped. */
561 RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
562 RTThreadPreemptDisable(&State);
563 rtR0MemObjDarwinTouchPages(pv, cb);
564 RTThreadPreemptRestore(&State);
565# endif
566
567 /* Bug 6226: Ignore KERN_PROTECTION_FAILURE on Leopard and older. */
568 if ( rc == VERR_PERMISSION_DENIED
569 && version_major <= 10 /* 10 = 10.6.x = Snow Leopard. */)
570 rc = VINF_SUCCESS;
571 }
572 else
573#endif
574 rc = VINF_SUCCESS;
575 if (RT_SUCCESS(rc))
576 {
577 pMemDarwin->pMemDesc = pMemDesc;
578 *ppMem = &pMemDarwin->Core;
579 return VINF_SUCCESS;
580 }
581
582 rtR0MemObjDelete(&pMemDarwin->Core);
583 }
584
585 if (enmType == RTR0MEMOBJTYPE_PHYS_NC)
586 rc = VERR_NO_PHYS_MEMORY;
587 else if (enmType == RTR0MEMOBJTYPE_LOW)
588 rc = VERR_NO_LOW_MEMORY;
589 else if (enmType == RTR0MEMOBJTYPE_CONT)
590 rc = VERR_NO_CONT_MEMORY;
591 else
592 rc = VERR_NO_MEMORY;
593 }
594 else
595 rc = VERR_MEMOBJ_INIT_FAILED;
596
597 pMemDesc->complete();
598 }
599 else
600 rc = RTErrConvertFromDarwinIO(IORet);
601 pMemDesc->release();
602 }
603 else
604 rc = VERR_MEMOBJ_INIT_FAILED;
605 Assert(rc != VERR_ADDRESS_TOO_BIG);
606 return rc;
607}
608
609
610DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
611{
612 return rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
613 0 /* PhysMask */, UINT64_MAX, RTR0MEMOBJTYPE_PAGE);
614}
615
616
617DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
618{
619 /*
620 * Try IOMallocPhysical/IOMallocAligned first.
621 * Then try optimistically without a physical address mask, which will always
622 * end up using IOMallocAligned.
623 *
624 * (See bug comment in the worker and IOBufferMemoryDescriptor::initWithPhysicalMask.)
625 */
626 int rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
627 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE, RTR0MEMOBJTYPE_LOW);
628 if (rc == VERR_ADDRESS_TOO_BIG)
629 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
630 0 /* PhysMask */, _4G - PAGE_SIZE, RTR0MEMOBJTYPE_LOW);
631 return rc;
632}
633
634
635DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
636{
637 int rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, true /* fContiguous */,
638 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE,
639 RTR0MEMOBJTYPE_CONT);
640
641 /*
642 * Workaround for bogus IOKernelAllocateContiguous behavior, just in case.
643 * cb <= PAGE_SIZE allocations take a different path, using a different allocator.
644 */
645 if (RT_FAILURE(rc) && cb <= PAGE_SIZE)
646 rc = rtR0MemObjNativeAllocWorker(ppMem, cb + PAGE_SIZE, fExecutable, true /* fContiguous */,
647 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE,
648 RTR0MEMOBJTYPE_CONT);
649 return rc;
650}
651
652
653DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
654{
655 /** @todo alignment */
656 if (uAlignment != PAGE_SIZE)
657 return VERR_NOT_SUPPORTED;
658
659 /*
660 * Translate the PhysHighest address into a mask.
661 */
662 int rc;
663 if (PhysHighest == NIL_RTHCPHYS)
664 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, true /* fExecutable */, true /* fContiguous */,
665 0 /* PhysMask*/, UINT64_MAX, RTR0MEMOBJTYPE_PHYS);
666 else
667 {
668 mach_vm_address_t PhysMask = 0;
669 PhysMask = ~(mach_vm_address_t)0;
670 while (PhysMask > (PhysHighest | PAGE_OFFSET_MASK))
671 PhysMask >>= 1;
672 AssertReturn(PhysMask + 1 <= cb, VERR_INVALID_PARAMETER);
673 PhysMask &= ~(mach_vm_address_t)PAGE_OFFSET_MASK;
674
675 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, true /* fExecutable */, true /* fContiguous */,
676 PhysMask, PhysHighest, RTR0MEMOBJTYPE_PHYS);
677 }
678 return rc;
679}
680
681
682DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
683{
684 /** @todo rtR0MemObjNativeAllocPhys / darwin.
685 * This might be a bit problematic and may very well require having to create our own
686 * object which we populate with pages but without mapping it into any address space.
687 * Estimate is 2-3 days.
688 */
689 return VERR_NOT_SUPPORTED;
690}
691
692
693DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
694{
695 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
696
697 /*
698 * Create a descriptor for it (the validation is always true on intel macs, but
699 * as it doesn't harm us keep it in).
700 */
701 int rc = VERR_ADDRESS_TOO_BIG;
702 IOAddressRange aRanges[1] = { { Phys, cb } };
703 if ( aRanges[0].address == Phys
704 && aRanges[0].length == cb)
705 {
706 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddressRanges(&aRanges[0], RT_ELEMENTS(aRanges),
707 kIODirectionInOut, NULL /*task*/);
708 if (pMemDesc)
709 {
710#ifdef __LP64__
711 Assert(Phys == pMemDesc->getPhysicalSegment(0, NULL, kIOMemoryMapperNone));
712#else
713 Assert(Phys == pMemDesc->getPhysicalSegment64(0, NULL));
714#endif
715
716 /*
717 * Create the IPRT memory object.
718 */
719 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
720 if (pMemDarwin)
721 {
722 pMemDarwin->Core.u.Phys.PhysBase = Phys;
723 pMemDarwin->Core.u.Phys.fAllocated = false;
724 pMemDarwin->Core.u.Phys.uCachePolicy = uCachePolicy;
725 pMemDarwin->pMemDesc = pMemDesc;
726 *ppMem = &pMemDarwin->Core;
727 return VINF_SUCCESS;
728 }
729
730 rc = VERR_NO_MEMORY;
731 pMemDesc->release();
732 }
733 else
734 rc = VERR_MEMOBJ_INIT_FAILED;
735 }
736 else
737 AssertMsgFailed(("%#llx %llx\n", (unsigned long long)Phys, (unsigned long long)cb));
738 return rc;
739}
740
741
742/**
743 * Internal worker for locking down pages.
744 *
745 * @return IPRT status code.
746 *
747 * @param ppMem Where to store the memory object pointer.
748 * @param pv First page.
749 * @param cb Number of bytes.
750 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
751 * and RTMEM_PROT_WRITE.
752 * @param Task The task \a pv and \a cb refers to.
753 */
754static int rtR0MemObjNativeLock(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess, task_t Task)
755{
756 NOREF(fAccess);
757#ifdef USE_VM_MAP_WIRE
758 vm_map_t Map = get_task_map(Task);
759 Assert(Map);
760
761 /*
762 * First try lock the memory.
763 */
764 int rc = VERR_LOCK_FAILED;
765 kern_return_t kr = vm_map_wire(get_task_map(Task),
766 (vm_map_offset_t)pv,
767 (vm_map_offset_t)pv + cb,
768 VM_PROT_DEFAULT,
769 0 /* not user */);
770 if (kr == KERN_SUCCESS)
771 {
772 /*
773 * Create the IPRT memory object.
774 */
775 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
776 if (pMemDarwin)
777 {
778 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
779 *ppMem = &pMemDarwin->Core;
780 return VINF_SUCCESS;
781 }
782
783 kr = vm_map_unwire(get_task_map(Task), (vm_map_offset_t)pv, (vm_map_offset_t)pv + cb, 0 /* not user */);
784 Assert(kr == KERN_SUCCESS);
785 rc = VERR_NO_MEMORY;
786 }
787
788#else
789
790 /*
791 * Create a descriptor and try lock it (prepare).
792 */
793 int rc = VERR_MEMOBJ_INIT_FAILED;
794 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddressRange((vm_address_t)pv, cb, kIODirectionInOut, Task);
795 if (pMemDesc)
796 {
797 IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
798 if (IORet == kIOReturnSuccess)
799 {
800 /*
801 * Create the IPRT memory object.
802 */
803 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
804 if (pMemDarwin)
805 {
806 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
807 pMemDarwin->pMemDesc = pMemDesc;
808 *ppMem = &pMemDarwin->Core;
809 return VINF_SUCCESS;
810 }
811
812 pMemDesc->complete();
813 rc = VERR_NO_MEMORY;
814 }
815 else
816 rc = VERR_LOCK_FAILED;
817 pMemDesc->release();
818 }
819#endif
820 return rc;
821}
822
823
824DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
825{
826 return rtR0MemObjNativeLock(ppMem, (void *)R3Ptr, cb, fAccess, (task_t)R0Process);
827}
828
829
830DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
831{
832 return rtR0MemObjNativeLock(ppMem, pv, cb, fAccess, kernel_task);
833}
834
835
836DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
837{
838 return VERR_NOT_SUPPORTED;
839}
840
841
842DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
843{
844 return VERR_NOT_SUPPORTED;
845}
846
847
848DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
849 unsigned fProt, size_t offSub, size_t cbSub)
850{
851 AssertReturn(pvFixed == (void *)-1, VERR_NOT_SUPPORTED);
852
853 /*
854 * Check that the specified alignment is supported.
855 */
856 if (uAlignment > PAGE_SIZE)
857 return VERR_NOT_SUPPORTED;
858
859 /*
860 * Must have a memory descriptor that we can map.
861 */
862 int rc = VERR_INVALID_PARAMETER;
863 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
864 if (pMemToMapDarwin->pMemDesc)
865 {
866#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
867 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->createMappingInTask(kernel_task,
868 0,
869 kIOMapAnywhere | kIOMapDefaultCache,
870 offSub,
871 cbSub);
872#else
873 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map(kernel_task,
874 0,
875 kIOMapAnywhere | kIOMapDefaultCache,
876 offSub,
877 cbSub);
878#endif
879 if (pMemMap)
880 {
881 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
882 void *pv = (void *)(uintptr_t)VirtAddr;
883 if ((uintptr_t)pv == VirtAddr)
884 {
885 //addr64_t Addr = pMemToMapDarwin->pMemDesc->getPhysicalSegment64(offSub, NULL);
886 //printf("pv=%p: %8llx %8llx\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr);
887
888// /*
889// * Explicitly lock it so that we're sure it is present and that
890// * its PTEs cannot be recycled.
891// * Note! withAddressRange() doesn't work as it adds kIOMemoryTypeVirtual64
892// * to the options which causes prepare() to not wire the pages.
893// * This is probably a bug.
894// */
895// IOAddressRange Range = { (mach_vm_address_t)pv, cbSub };
896// IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withOptions(&Range,
897// 1 /* count */,
898// 0 /* offset */,
899// kernel_task,
900// kIODirectionInOut | kIOMemoryTypeVirtual,
901// kIOMapperSystem);
902// if (pMemDesc)
903// {
904// IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
905// if (IORet == kIOReturnSuccess)
906// {
907 /* HACK ALERT! */
908 rtR0MemObjDarwinTouchPages(pv, cbSub);
909 /** @todo First, the memory should've been mapped by now, and second, it
910 * should have the wired attribute in the PTE (bit 9). Neither
911 * seems to be the case. The disabled locking code doesn't make any
912 * difference, which is extremely odd, and breaks
913 * rtR0MemObjNativeGetPagePhysAddr (getPhysicalSegment64 -> 64 for the
914 * lock descriptor. */
915 //addr64_t Addr = pMemDesc->getPhysicalSegment64(0, NULL);
916 //printf("pv=%p: %8llx %8llx (%d)\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr, 2);
917
918 /*
919 * Create the IPRT memory object.
920 */
921 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
922 pv, cbSub);
923 if (pMemDarwin)
924 {
925 pMemDarwin->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
926 pMemDarwin->pMemMap = pMemMap;
927// pMemDarwin->pMemDesc = pMemDesc;
928 *ppMem = &pMemDarwin->Core;
929 return VINF_SUCCESS;
930 }
931
932// pMemDesc->complete();
933// rc = VERR_NO_MEMORY;
934// }
935// else
936// rc = RTErrConvertFromDarwinIO(IORet);
937// pMemDesc->release();
938// }
939// else
940// rc = VERR_MEMOBJ_INIT_FAILED;
941 }
942 else
943 rc = VERR_ADDRESS_TOO_BIG;
944 pMemMap->release();
945 }
946 else
947 rc = VERR_MAP_FAILED;
948 }
949 return rc;
950}
951
952
953DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
954{
955 /*
956 * Check for unsupported things.
957 */
958 AssertReturn(R3PtrFixed == (RTR3PTR)-1, VERR_NOT_SUPPORTED);
959 if (uAlignment > PAGE_SIZE)
960 return VERR_NOT_SUPPORTED;
961
962 /*
963 * Must have a memory descriptor.
964 */
965 int rc = VERR_INVALID_PARAMETER;
966 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
967 if (pMemToMapDarwin->pMemDesc)
968 {
969#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
970 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->createMappingInTask((task_t)R0Process,
971 0,
972 kIOMapAnywhere | kIOMapDefaultCache,
973 0 /* offset */,
974 0 /* length */);
975#else
976 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map((task_t)R0Process,
977 0,
978 kIOMapAnywhere | kIOMapDefaultCache);
979#endif
980 if (pMemMap)
981 {
982 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
983 void *pv = (void *)(uintptr_t)VirtAddr;
984 if ((uintptr_t)pv == VirtAddr)
985 {
986 /*
987 * Create the IPRT memory object.
988 */
989 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
990 pv, pMemToMapDarwin->Core.cb);
991 if (pMemDarwin)
992 {
993 pMemDarwin->Core.u.Mapping.R0Process = R0Process;
994 pMemDarwin->pMemMap = pMemMap;
995 *ppMem = &pMemDarwin->Core;
996 return VINF_SUCCESS;
997 }
998
999 rc = VERR_NO_MEMORY;
1000 }
1001 else
1002 rc = VERR_ADDRESS_TOO_BIG;
1003 pMemMap->release();
1004 }
1005 else
1006 rc = VERR_MAP_FAILED;
1007 }
1008 return rc;
1009}
1010
1011
1012DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
1013{
1014 /* Get the map for the object. */
1015 vm_map_t pVmMap = rtR0MemObjDarwinGetMap(pMem);
1016 if (!pVmMap)
1017 return VERR_NOT_SUPPORTED;
1018
1019 /*
1020 * Convert the protection.
1021 */
1022 vm_prot_t fMachProt;
1023 switch (fProt)
1024 {
1025 case RTMEM_PROT_NONE:
1026 fMachProt = VM_PROT_NONE;
1027 break;
1028 case RTMEM_PROT_READ:
1029 fMachProt = VM_PROT_READ;
1030 break;
1031 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
1032 fMachProt = VM_PROT_READ | VM_PROT_WRITE;
1033 break;
1034 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
1035 fMachProt = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
1036 break;
1037 case RTMEM_PROT_WRITE:
1038 fMachProt = VM_PROT_WRITE | VM_PROT_READ; /* never write-only */
1039 break;
1040 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
1041 fMachProt = VM_PROT_WRITE | VM_PROT_EXECUTE | VM_PROT_READ; /* never write-only or execute-only */
1042 break;
1043 case RTMEM_PROT_EXEC:
1044 fMachProt = VM_PROT_EXECUTE | VM_PROT_READ; /* never execute-only */
1045 break;
1046 default:
1047 AssertFailedReturn(VERR_INVALID_PARAMETER);
1048 }
1049
1050 /*
1051 * Do the job.
1052 */
1053 vm_offset_t Start = (uintptr_t)pMem->pv + offSub;
1054 kern_return_t krc = vm_protect(pVmMap,
1055 Start,
1056 cbSub,
1057 false,
1058 fMachProt);
1059 if (krc != KERN_SUCCESS)
1060 {
1061 static int s_cComplaints = 0;
1062 if (s_cComplaints < 10)
1063 {
1064 s_cComplaints++;
1065 printf("rtR0MemObjNativeProtect: vm_protect(%p,%p,%p,false,%#x) -> %d\n",
1066 pVmMap, (void *)Start, (void *)cbSub, fMachProt, krc);
1067
1068 kern_return_t krc2;
1069 vm_offset_t pvReal = Start;
1070 vm_size_t cbReal = 0;
1071 mach_msg_type_number_t cInfo = VM_REGION_BASIC_INFO_COUNT;
1072 struct vm_region_basic_info Info;
1073 RT_ZERO(Info);
1074 krc2 = vm_region(pVmMap, &pvReal, &cbReal, VM_REGION_BASIC_INFO, (vm_region_info_t)&Info, &cInfo, NULL);
1075 printf("rtR0MemObjNativeProtect: basic info - krc2=%d pv=%p cb=%p prot=%#x max=%#x inh=%#x shr=%d rvd=%d off=%#x behavior=%#x wired=%#x\n",
1076 krc2, (void *)pvReal, (void *)cbReal, Info.protection, Info.max_protection, Info.inheritance,
1077 Info.shared, Info.reserved, Info.offset, Info.behavior, Info.user_wired_count);
1078 }
1079 return RTErrConvertFromDarwinKern(krc);
1080 }
1081
1082 /*
1083 * Touch the pages if they should be writable afterwards and accessible
1084 * from code which should never fault. vm_protect() may leave pages
1085 * temporarily write protected, possibly due to pmap no-upgrade rules?
1086 *
1087 * This is the same trick (or HACK ALERT if you like) as applied in
1088 * rtR0MemObjNativeMapKernel.
1089 */
1090 if ( pMem->enmType != RTR0MEMOBJTYPE_MAPPING
1091 || pMem->u.Mapping.R0Process == NIL_RTR0PROCESS)
1092 {
1093 if (fProt & RTMEM_PROT_WRITE)
1094 rtR0MemObjDarwinTouchPages((void *)Start, cbSub);
1095 /*
1096 * Sniff (read) read-only pages too, just to be sure.
1097 */
1098 else if (fProt & (RTMEM_PROT_READ | RTMEM_PROT_EXEC))
1099 rtR0MemObjDarwinSniffPages((void const *)Start, cbSub);
1100 }
1101
1102 return VINF_SUCCESS;
1103}
1104
1105
1106DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1107{
1108 RTHCPHYS PhysAddr;
1109 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
1110
1111#ifdef USE_VM_MAP_WIRE
1112 /*
1113 * Locked memory doesn't have a memory descriptor and
1114 * needs to be handled differently.
1115 */
1116 if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
1117 {
1118 ppnum_t PgNo;
1119 if (pMemDarwin->Core.u.Lock.R0Process == NIL_RTR0PROCESS)
1120 PgNo = pmap_find_phys(kernel_pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
1121 else
1122 {
1123 /*
1124 * From what I can tell, Apple seems to have locked up the all the
1125 * available interfaces that could help us obtain the pmap_t of a task
1126 * or vm_map_t.
1127
1128 * So, we'll have to figure out where in the vm_map_t structure it is
1129 * and read it our selves. ASSUMING that kernel_pmap is pointed to by
1130 * kernel_map->pmap, we scan kernel_map to locate the structure offset.
1131 * Not nice, but it will hopefully do the job in a reliable manner...
1132 *
1133 * (get_task_pmap, get_map_pmap or vm_map_pmap is what we really need btw.)
1134 */
1135 static int s_offPmap = -1;
1136 if (RT_UNLIKELY(s_offPmap == -1))
1137 {
1138 pmap_t const *p = (pmap_t *)kernel_map;
1139 pmap_t const * const pEnd = p + 64;
1140 for (; p < pEnd; p++)
1141 if (*p == kernel_pmap)
1142 {
1143 s_offPmap = (uintptr_t)p - (uintptr_t)kernel_map;
1144 break;
1145 }
1146 AssertReturn(s_offPmap >= 0, NIL_RTHCPHYS);
1147 }
1148 pmap_t Pmap = *(pmap_t *)((uintptr_t)get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process) + s_offPmap);
1149 PgNo = pmap_find_phys(Pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
1150 }
1151
1152 AssertReturn(PgNo, NIL_RTHCPHYS);
1153 PhysAddr = (RTHCPHYS)PgNo << PAGE_SHIFT;
1154 Assert((PhysAddr >> PAGE_SHIFT) == PgNo);
1155 }
1156 else
1157#endif /* USE_VM_MAP_WIRE */
1158 {
1159 /*
1160 * Get the memory descriptor.
1161 */
1162 IOMemoryDescriptor *pMemDesc = pMemDarwin->pMemDesc;
1163 if (!pMemDesc)
1164 pMemDesc = pMemDarwin->pMemMap->getMemoryDescriptor();
1165 AssertReturn(pMemDesc, NIL_RTHCPHYS);
1166
1167 /*
1168 * If we've got a memory descriptor, use getPhysicalSegment64().
1169 */
1170#ifdef __LP64__
1171 addr64_t Addr = pMemDesc->getPhysicalSegment(iPage * PAGE_SIZE, NULL, kIOMemoryMapperNone);
1172#else
1173 addr64_t Addr = pMemDesc->getPhysicalSegment64(iPage * PAGE_SIZE, NULL);
1174#endif
1175 AssertMsgReturn(Addr, ("iPage=%u\n", iPage), NIL_RTHCPHYS);
1176 PhysAddr = Addr;
1177 AssertMsgReturn(PhysAddr == Addr, ("PhysAddr=%RHp Addr=%RX64\n", PhysAddr, (uint64_t)Addr), NIL_RTHCPHYS);
1178 }
1179
1180 return PhysAddr;
1181}
1182
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