VirtualBox

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

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

r0drv/darwin: memobj-r0drv-darwin; complete() and prepare() must be paired and called before release().

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

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