VirtualBox

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

最後變更 在這個檔案從43304是 43304,由 vboxsync 提交於 13 年 前

Forward ported r78691 from 4.1: Extend r80723 / r78414 to Snow Leopard.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 43.1 KB
 
1/* $Id: memobj-r0drv-darwin.cpp 43304 2012-09-11 23:56:41Z 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.alldomusa.eu.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 /* Bug 6226: Ignore KERN_PROTECTION_FAILURE on Leopard and older. */
551 if ( rc == VERR_PERMISSION_DENIED
552 && version_major <= 10 /* 10 = 10.6.x = Snow Leopard. */)
553 rc = VINF_SUCCESS;
554 }
555 else
556#endif
557 rc = VINF_SUCCESS;
558 if (RT_SUCCESS(rc))
559 {
560 pMemDarwin->pMemDesc = pMemDesc;
561 *ppMem = &pMemDarwin->Core;
562 return VINF_SUCCESS;
563 }
564
565 rtR0MemObjDelete(&pMemDarwin->Core);
566 }
567
568 if (enmType == RTR0MEMOBJTYPE_PHYS_NC)
569 rc = VERR_NO_PHYS_MEMORY;
570 else if (enmType == RTR0MEMOBJTYPE_LOW)
571 rc = VERR_NO_LOW_MEMORY;
572 else if (enmType == RTR0MEMOBJTYPE_CONT)
573 rc = VERR_NO_CONT_MEMORY;
574 else
575 rc = VERR_NO_MEMORY;
576 }
577 else
578 rc = VERR_MEMOBJ_INIT_FAILED;
579 }
580 else
581 rc = RTErrConvertFromDarwinIO(IORet);
582 pMemDesc->release();
583 }
584 else
585 rc = VERR_MEMOBJ_INIT_FAILED;
586 Assert(rc != VERR_ADDRESS_TOO_BIG);
587 return rc;
588}
589
590
591DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
592{
593 return rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
594 0 /* PhysMask */, UINT64_MAX, RTR0MEMOBJTYPE_PAGE);
595}
596
597
598DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
599{
600 /*
601 * Try IOMallocPhysical/IOMallocAligned first.
602 * Then try optimistically without a physical address mask, which will always
603 * end up using IOMallocAligned.
604 *
605 * (See bug comment in the worker and IOBufferMemoryDescriptor::initWithPhysicalMask.)
606 */
607 int rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
608 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE, RTR0MEMOBJTYPE_LOW);
609 if (rc == VERR_ADDRESS_TOO_BIG)
610 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, false /* fContiguous */,
611 0 /* PhysMask */, _4G - PAGE_SIZE, RTR0MEMOBJTYPE_LOW);
612 return rc;
613}
614
615
616DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
617{
618 int rc = rtR0MemObjNativeAllocWorker(ppMem, cb, fExecutable, true /* fContiguous */,
619 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE,
620 RTR0MEMOBJTYPE_CONT);
621
622 /*
623 * Workaround for bogus IOKernelAllocateContiguous behavior, just in case.
624 * cb <= PAGE_SIZE allocations take a different path, using a different allocator.
625 */
626 if (RT_FAILURE(rc) && cb <= PAGE_SIZE)
627 rc = rtR0MemObjNativeAllocWorker(ppMem, cb + PAGE_SIZE, fExecutable, true /* fContiguous */,
628 ~(uint32_t)PAGE_OFFSET_MASK, _4G - PAGE_SIZE,
629 RTR0MEMOBJTYPE_CONT);
630 return rc;
631}
632
633
634DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment)
635{
636 /** @todo alignment */
637 if (uAlignment != PAGE_SIZE)
638 return VERR_NOT_SUPPORTED;
639
640 /*
641 * Translate the PhysHighest address into a mask.
642 */
643 int rc;
644 if (PhysHighest == NIL_RTHCPHYS)
645 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, true /* fExecutable */, true /* fContiguous */,
646 0 /* PhysMask*/, UINT64_MAX, RTR0MEMOBJTYPE_PHYS);
647 else
648 {
649 mach_vm_address_t PhysMask = 0;
650 PhysMask = ~(mach_vm_address_t)0;
651 while (PhysMask > (PhysHighest | PAGE_OFFSET_MASK))
652 PhysMask >>= 1;
653 AssertReturn(PhysMask + 1 <= cb, VERR_INVALID_PARAMETER);
654 PhysMask &= ~(mach_vm_address_t)PAGE_OFFSET_MASK;
655
656 rc = rtR0MemObjNativeAllocWorker(ppMem, cb, true /* fExecutable */, true /* fContiguous */,
657 PhysMask, PhysHighest, RTR0MEMOBJTYPE_PHYS);
658 }
659 return rc;
660}
661
662
663DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
664{
665 /** @todo rtR0MemObjNativeAllocPhys / darwin.
666 * This might be a bit problematic and may very well require having to create our own
667 * object which we populate with pages but without mapping it into any address space.
668 * Estimate is 2-3 days.
669 */
670 return VERR_NOT_SUPPORTED;
671}
672
673
674DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy)
675{
676 AssertReturn(uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE, VERR_NOT_SUPPORTED);
677
678 /*
679 * Create a descriptor for it (the validation is always true on intel macs, but
680 * as it doesn't harm us keep it in).
681 */
682 int rc = VERR_ADDRESS_TOO_BIG;
683 IOAddressRange aRanges[1] = { { Phys, cb } };
684 if ( aRanges[0].address == Phys
685 && aRanges[0].length == cb)
686 {
687 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddressRanges(&aRanges[0], RT_ELEMENTS(aRanges),
688 kIODirectionInOut, NULL /*task*/);
689 if (pMemDesc)
690 {
691#ifdef __LP64__ /* Grumble! */
692 Assert(Phys == pMemDesc->getPhysicalSegment(0, 0));
693#else
694 Assert(Phys == pMemDesc->getPhysicalSegment64(0, 0));
695#endif
696
697 /*
698 * Create the IPRT memory object.
699 */
700 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
701 if (pMemDarwin)
702 {
703 pMemDarwin->Core.u.Phys.PhysBase = Phys;
704 pMemDarwin->Core.u.Phys.fAllocated = false;
705 pMemDarwin->Core.u.Phys.uCachePolicy = uCachePolicy;
706 pMemDarwin->pMemDesc = pMemDesc;
707 *ppMem = &pMemDarwin->Core;
708 return VINF_SUCCESS;
709 }
710
711 rc = VERR_NO_MEMORY;
712 pMemDesc->release();
713 }
714 else
715 rc = VERR_MEMOBJ_INIT_FAILED;
716 }
717 else
718 AssertMsgFailed(("%#llx %llx\n", (unsigned long long)Phys, (unsigned long long)cb));
719 return rc;
720}
721
722
723/**
724 * Internal worker for locking down pages.
725 *
726 * @return IPRT status code.
727 *
728 * @param ppMem Where to store the memory object pointer.
729 * @param pv First page.
730 * @param cb Number of bytes.
731 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
732 * and RTMEM_PROT_WRITE.
733 * @param Task The task \a pv and \a cb refers to.
734 */
735static int rtR0MemObjNativeLock(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess, task_t Task)
736{
737 NOREF(fAccess);
738#ifdef USE_VM_MAP_WIRE
739 vm_map_t Map = get_task_map(Task);
740 Assert(Map);
741
742 /*
743 * First try lock the memory.
744 */
745 int rc = VERR_LOCK_FAILED;
746 kern_return_t kr = vm_map_wire(get_task_map(Task),
747 (vm_map_offset_t)pv,
748 (vm_map_offset_t)pv + cb,
749 VM_PROT_DEFAULT,
750 0 /* not user */);
751 if (kr == KERN_SUCCESS)
752 {
753 /*
754 * Create the IPRT memory object.
755 */
756 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
757 if (pMemDarwin)
758 {
759 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
760 *ppMem = &pMemDarwin->Core;
761 return VINF_SUCCESS;
762 }
763
764 kr = vm_map_unwire(get_task_map(Task), (vm_map_offset_t)pv, (vm_map_offset_t)pv + cb, 0 /* not user */);
765 Assert(kr == KERN_SUCCESS);
766 rc = VERR_NO_MEMORY;
767 }
768
769#else
770
771 /*
772 * Create a descriptor and try lock it (prepare).
773 */
774 int rc = VERR_MEMOBJ_INIT_FAILED;
775 IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddressRange((vm_address_t)pv, cb, kIODirectionInOut, Task);
776 if (pMemDesc)
777 {
778 IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
779 if (IORet == kIOReturnSuccess)
780 {
781 /*
782 * Create the IPRT memory object.
783 */
784 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
785 if (pMemDarwin)
786 {
787 pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
788 pMemDarwin->pMemDesc = pMemDesc;
789 *ppMem = &pMemDarwin->Core;
790 return VINF_SUCCESS;
791 }
792
793 pMemDesc->complete();
794 rc = VERR_NO_MEMORY;
795 }
796 else
797 rc = VERR_LOCK_FAILED;
798 pMemDesc->release();
799 }
800#endif
801 return rc;
802}
803
804
805DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
806{
807 return rtR0MemObjNativeLock(ppMem, (void *)R3Ptr, cb, fAccess, (task_t)R0Process);
808}
809
810
811DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess)
812{
813 return rtR0MemObjNativeLock(ppMem, pv, cb, fAccess, kernel_task);
814}
815
816
817DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
818{
819 return VERR_NOT_SUPPORTED;
820}
821
822
823DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
824{
825 return VERR_NOT_SUPPORTED;
826}
827
828
829DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
830 unsigned fProt, size_t offSub, size_t cbSub)
831{
832 AssertReturn(pvFixed == (void *)-1, VERR_NOT_SUPPORTED);
833
834 /*
835 * Check that the specified alignment is supported.
836 */
837 if (uAlignment > PAGE_SIZE)
838 return VERR_NOT_SUPPORTED;
839
840 /*
841 * Must have a memory descriptor that we can map.
842 */
843 int rc = VERR_INVALID_PARAMETER;
844 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
845 if (pMemToMapDarwin->pMemDesc)
846 {
847#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
848 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->createMappingInTask(kernel_task,
849 0,
850 kIOMapAnywhere | kIOMapDefaultCache,
851 offSub,
852 cbSub);
853#else
854 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map(kernel_task,
855 0,
856 kIOMapAnywhere | kIOMapDefaultCache,
857 offSub,
858 cbSub);
859#endif
860 if (pMemMap)
861 {
862 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
863 void *pv = (void *)(uintptr_t)VirtAddr;
864 if ((uintptr_t)pv == VirtAddr)
865 {
866 //addr64_t Addr = pMemToMapDarwin->pMemDesc->getPhysicalSegment64(offSub, NULL);
867 //printf("pv=%p: %8llx %8llx\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr);
868
869// /*
870// * Explicitly lock it so that we're sure it is present and that
871// * its PTEs cannot be recycled.
872// * Note! withAddressRange() doesn't work as it adds kIOMemoryTypeVirtual64
873// * to the options which causes prepare() to not wire the pages.
874// * This is probably a bug.
875// */
876// IOAddressRange Range = { (mach_vm_address_t)pv, cbSub };
877// IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withOptions(&Range,
878// 1 /* count */,
879// 0 /* offset */,
880// kernel_task,
881// kIODirectionInOut | kIOMemoryTypeVirtual,
882// kIOMapperSystem);
883// if (pMemDesc)
884// {
885// IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
886// if (IORet == kIOReturnSuccess)
887// {
888 /* HACK ALERT! */
889 rtR0MemObjDarwinTouchPages(pv, cbSub);
890 /** @todo First, the memory should've been mapped by now, and second, it
891 * should have the wired attribute in the PTE (bit 9). Neither
892 * seems to be the case. The disabled locking code doesn't make any
893 * difference, which is extremely odd, and breaks
894 * rtR0MemObjNativeGetPagePhysAddr (getPhysicalSegment64 -> 64 for the
895 * lock descriptor. */
896 //addr64_t Addr = pMemDesc->getPhysicalSegment64(0, NULL);
897 //printf("pv=%p: %8llx %8llx (%d)\n", pv, rtR0MemObjDarwinGetPTE(pv), Addr, 2);
898
899 /*
900 * Create the IPRT memory object.
901 */
902 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
903 pv, cbSub);
904 if (pMemDarwin)
905 {
906 pMemDarwin->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
907 pMemDarwin->pMemMap = pMemMap;
908// pMemDarwin->pMemDesc = pMemDesc;
909 *ppMem = &pMemDarwin->Core;
910 return VINF_SUCCESS;
911 }
912
913// pMemDesc->complete();
914// rc = VERR_NO_MEMORY;
915// }
916// else
917// rc = RTErrConvertFromDarwinIO(IORet);
918// pMemDesc->release();
919// }
920// else
921// rc = VERR_MEMOBJ_INIT_FAILED;
922 }
923 else
924 rc = VERR_ADDRESS_TOO_BIG;
925 pMemMap->release();
926 }
927 else
928 rc = VERR_MAP_FAILED;
929 }
930 return rc;
931}
932
933
934DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
935{
936 /*
937 * Check for unsupported things.
938 */
939 AssertReturn(R3PtrFixed == (RTR3PTR)-1, VERR_NOT_SUPPORTED);
940 if (uAlignment > PAGE_SIZE)
941 return VERR_NOT_SUPPORTED;
942
943 /*
944 * Must have a memory descriptor.
945 */
946 int rc = VERR_INVALID_PARAMETER;
947 PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
948 if (pMemToMapDarwin->pMemDesc)
949 {
950#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
951 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->createMappingInTask((task_t)R0Process,
952 0,
953 kIOMapAnywhere | kIOMapDefaultCache,
954 0 /* offset */,
955 0 /* length */);
956#else
957 IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map((task_t)R0Process,
958 0,
959 kIOMapAnywhere | kIOMapDefaultCache);
960#endif
961 if (pMemMap)
962 {
963 IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
964 void *pv = (void *)(uintptr_t)VirtAddr;
965 if ((uintptr_t)pv == VirtAddr)
966 {
967 /*
968 * Create the IPRT memory object.
969 */
970 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
971 pv, pMemToMapDarwin->Core.cb);
972 if (pMemDarwin)
973 {
974 pMemDarwin->Core.u.Mapping.R0Process = R0Process;
975 pMemDarwin->pMemMap = pMemMap;
976 *ppMem = &pMemDarwin->Core;
977 return VINF_SUCCESS;
978 }
979
980 rc = VERR_NO_MEMORY;
981 }
982 else
983 rc = VERR_ADDRESS_TOO_BIG;
984 pMemMap->release();
985 }
986 else
987 rc = VERR_MAP_FAILED;
988 }
989 return rc;
990}
991
992
993DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt)
994{
995 /* Get the map for the object. */
996 vm_map_t pVmMap = rtR0MemObjDarwinGetMap(pMem);
997 if (!pVmMap)
998 return VERR_NOT_SUPPORTED;
999
1000 /*
1001 * Convert the protection.
1002 */
1003 vm_prot_t fMachProt;
1004 switch (fProt)
1005 {
1006 case RTMEM_PROT_NONE:
1007 fMachProt = VM_PROT_NONE;
1008 break;
1009 case RTMEM_PROT_READ:
1010 fMachProt = VM_PROT_READ;
1011 break;
1012 case RTMEM_PROT_READ | RTMEM_PROT_WRITE:
1013 fMachProt = VM_PROT_READ | VM_PROT_WRITE;
1014 break;
1015 case RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
1016 fMachProt = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
1017 break;
1018 case RTMEM_PROT_WRITE:
1019 fMachProt = VM_PROT_WRITE | VM_PROT_READ; /* never write-only */
1020 break;
1021 case RTMEM_PROT_WRITE | RTMEM_PROT_EXEC:
1022 fMachProt = VM_PROT_WRITE | VM_PROT_EXECUTE | VM_PROT_READ; /* never write-only or execute-only */
1023 break;
1024 case RTMEM_PROT_EXEC:
1025 fMachProt = VM_PROT_EXECUTE | VM_PROT_READ; /* never execute-only */
1026 break;
1027 default:
1028 AssertFailedReturn(VERR_INVALID_PARAMETER);
1029 }
1030
1031 /*
1032 * Do the job.
1033 */
1034 vm_offset_t Start = (uintptr_t)pMem->pv + offSub;
1035 kern_return_t krc = vm_protect(pVmMap,
1036 Start,
1037 cbSub,
1038 false,
1039 fMachProt);
1040 if (krc != KERN_SUCCESS)
1041 {
1042 static int s_cComplaints = 0;
1043 if (s_cComplaints < 10)
1044 {
1045 s_cComplaints++;
1046 printf("rtR0MemObjNativeProtect: vm_protect(%p,%p,%p,false,%#x) -> %d\n",
1047 pVmMap, (void *)Start, (void *)cbSub, fMachProt, krc);
1048
1049 kern_return_t krc2;
1050 vm_offset_t pvReal = Start;
1051 vm_size_t cbReal = 0;
1052 mach_msg_type_number_t cInfo = VM_REGION_BASIC_INFO_COUNT;
1053 struct vm_region_basic_info Info;
1054 RT_ZERO(Info);
1055 krc2 = vm_region(pVmMap, &pvReal, &cbReal, VM_REGION_BASIC_INFO, (vm_region_info_t)&Info, &cInfo, NULL);
1056 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",
1057 krc2, (void *)pvReal, (void *)cbReal, Info.protection, Info.max_protection, Info.inheritance,
1058 Info.shared, Info.reserved, Info.offset, Info.behavior, Info.user_wired_count);
1059 }
1060 return RTErrConvertFromDarwinKern(krc);
1061 }
1062
1063 /*
1064 * Touch the pages if they should be writable afterwards and accessible
1065 * from code which should never fault. vm_protect() may leave pages
1066 * temporarily write protected, possibly due to pmap no-upgrade rules?
1067 *
1068 * This is the same trick (or HACK ALERT if you like) as applied in
1069 * rtR0MemObjNativeMapKernel.
1070 */
1071 if ( pMem->enmType != RTR0MEMOBJTYPE_MAPPING
1072 || pMem->u.Mapping.R0Process == NIL_RTR0PROCESS)
1073 {
1074 if (fProt & RTMEM_PROT_WRITE)
1075 rtR0MemObjDarwinTouchPages((void *)Start, cbSub);
1076 /*
1077 * Sniff (read) read-only pages too, just to be sure.
1078 */
1079 else if (fProt & (RTMEM_PROT_READ | RTMEM_PROT_EXEC))
1080 rtR0MemObjDarwinSniffPages((void const *)Start, cbSub);
1081 }
1082
1083 return VINF_SUCCESS;
1084}
1085
1086
1087DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
1088{
1089 RTHCPHYS PhysAddr;
1090 PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
1091
1092#ifdef USE_VM_MAP_WIRE
1093 /*
1094 * Locked memory doesn't have a memory descriptor and
1095 * needs to be handled differently.
1096 */
1097 if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
1098 {
1099 ppnum_t PgNo;
1100 if (pMemDarwin->Core.u.Lock.R0Process == NIL_RTR0PROCESS)
1101 PgNo = pmap_find_phys(kernel_pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
1102 else
1103 {
1104 /*
1105 * From what I can tell, Apple seems to have locked up the all the
1106 * available interfaces that could help us obtain the pmap_t of a task
1107 * or vm_map_t.
1108
1109 * So, we'll have to figure out where in the vm_map_t structure it is
1110 * and read it our selves. ASSUMING that kernel_pmap is pointed to by
1111 * kernel_map->pmap, we scan kernel_map to locate the structure offset.
1112 * Not nice, but it will hopefully do the job in a reliable manner...
1113 *
1114 * (get_task_pmap, get_map_pmap or vm_map_pmap is what we really need btw.)
1115 */
1116 static int s_offPmap = -1;
1117 if (RT_UNLIKELY(s_offPmap == -1))
1118 {
1119 pmap_t const *p = (pmap_t *)kernel_map;
1120 pmap_t const * const pEnd = p + 64;
1121 for (; p < pEnd; p++)
1122 if (*p == kernel_pmap)
1123 {
1124 s_offPmap = (uintptr_t)p - (uintptr_t)kernel_map;
1125 break;
1126 }
1127 AssertReturn(s_offPmap >= 0, NIL_RTHCPHYS);
1128 }
1129 pmap_t Pmap = *(pmap_t *)((uintptr_t)get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process) + s_offPmap);
1130 PgNo = pmap_find_phys(Pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
1131 }
1132
1133 AssertReturn(PgNo, NIL_RTHCPHYS);
1134 PhysAddr = (RTHCPHYS)PgNo << PAGE_SHIFT;
1135 Assert((PhysAddr >> PAGE_SHIFT) == PgNo);
1136 }
1137 else
1138#endif /* USE_VM_MAP_WIRE */
1139 {
1140 /*
1141 * Get the memory descriptor.
1142 */
1143 IOMemoryDescriptor *pMemDesc = pMemDarwin->pMemDesc;
1144 if (!pMemDesc)
1145 pMemDesc = pMemDarwin->pMemMap->getMemoryDescriptor();
1146 AssertReturn(pMemDesc, NIL_RTHCPHYS);
1147
1148 /*
1149 * If we've got a memory descriptor, use getPhysicalSegment64().
1150 */
1151#ifdef __LP64__ /* Grumble! */
1152 addr64_t Addr = pMemDesc->getPhysicalSegment(iPage * PAGE_SIZE, NULL);
1153#else
1154 addr64_t Addr = pMemDesc->getPhysicalSegment64(iPage * PAGE_SIZE, NULL);
1155#endif
1156 AssertMsgReturn(Addr, ("iPage=%u\n", iPage), NIL_RTHCPHYS);
1157 PhysAddr = Addr;
1158 AssertMsgReturn(PhysAddr == Addr, ("PhysAddr=%RHp Addr=%RX64\n", PhysAddr, (uint64_t)Addr), NIL_RTHCPHYS);
1159 }
1160
1161 return PhysAddr;
1162}
1163
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette