VirtualBox

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

Last change on this file since 78365 was 78120, checked in by vboxsync, 6 years ago

IPRT: Started adding a RTR0MemObjMapUserEx function that takes offSub and cbSub. bugref:9217

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