1 | /* $Id: memobj-r0drv-darwin.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Ring-0 Memory Objects, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include "the-darwin-kernel.h"
|
---|
32 |
|
---|
33 | #include <iprt/memobj.h>
|
---|
34 | #include <iprt/alloc.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/log.h>
|
---|
37 | #include <iprt/param.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/process.h>
|
---|
40 | #include "internal/memobj.h"
|
---|
41 |
|
---|
42 | #define USE_VM_MAP_WIRE
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *******************************************************************************/
|
---|
48 | /**
|
---|
49 | * The Darwin version of the memory object structure.
|
---|
50 | */
|
---|
51 | typedef struct RTR0MEMOBJDARWIN
|
---|
52 | {
|
---|
53 | /** The core structure. */
|
---|
54 | RTR0MEMOBJINTERNAL Core;
|
---|
55 | /** Pointer to the memory descriptor created for allocated and locked memory. */
|
---|
56 | IOMemoryDescriptor *pMemDesc;
|
---|
57 | /** Pointer to the memory mapping object for mapped memory. */
|
---|
58 | IOMemoryMap *pMemMap;
|
---|
59 | } RTR0MEMOBJDARWIN, *PRTR0MEMOBJDARWIN;
|
---|
60 |
|
---|
61 |
|
---|
62 | int rtR0MemObjNativeFree(RTR0MEMOBJ pMem)
|
---|
63 | {
|
---|
64 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Release the IOMemoryDescriptor/IOMemoryMap associated with the object.
|
---|
68 | */
|
---|
69 | if (pMemDarwin->pMemDesc)
|
---|
70 | {
|
---|
71 | if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
|
---|
72 | pMemDarwin->pMemDesc->complete(); /* paranoia */
|
---|
73 | pMemDarwin->pMemDesc->release();
|
---|
74 | pMemDarwin->pMemDesc = NULL;
|
---|
75 | Assert(!pMemDarwin->pMemMap);
|
---|
76 | }
|
---|
77 | else if (pMemDarwin->pMemMap)
|
---|
78 | {
|
---|
79 | pMemDarwin->pMemMap->release();
|
---|
80 | pMemDarwin->pMemMap = NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Release any memory that we've allocated or locked.
|
---|
85 | */
|
---|
86 | switch (pMemDarwin->Core.enmType)
|
---|
87 | {
|
---|
88 | case RTR0MEMOBJTYPE_LOW:
|
---|
89 | case RTR0MEMOBJTYPE_PAGE:
|
---|
90 | IOFreeAligned(pMemDarwin->Core.pv, pMemDarwin->Core.cb);
|
---|
91 | break;
|
---|
92 |
|
---|
93 | case RTR0MEMOBJTYPE_CONT:
|
---|
94 | IOFreeContiguous(pMemDarwin->Core.pv, pMemDarwin->Core.cb);
|
---|
95 | break;
|
---|
96 |
|
---|
97 | case RTR0MEMOBJTYPE_LOCK:
|
---|
98 | {
|
---|
99 | #ifdef USE_VM_MAP_WIRE
|
---|
100 | vm_map_t Map = pMemDarwin->Core.u.Lock.R0Process != NIL_RTR0PROCESS
|
---|
101 | ? get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process)
|
---|
102 | : kernel_map;
|
---|
103 | kern_return_t kr = vm_map_unwire(Map,
|
---|
104 | (vm_map_offset_t)pMemDarwin->Core.pv,
|
---|
105 | (vm_map_offset_t)pMemDarwin->Core.pv + pMemDarwin->Core.cb,
|
---|
106 | 0 /* not user */);
|
---|
107 | AssertRC(kr == KERN_SUCCESS); /** @todo don't ignore... */
|
---|
108 | #endif
|
---|
109 | break;
|
---|
110 | }
|
---|
111 |
|
---|
112 | case RTR0MEMOBJTYPE_PHYS:
|
---|
113 | /*if (pMemDarwin->Core.u.Phys.fAllocated)
|
---|
114 | IOFreePhysical(pMemDarwin->Core.u.Phys.PhysBase, pMemDarwin->Core.cb);*/
|
---|
115 | Assert(!pMemDarwin->Core.u.Phys.fAllocated);
|
---|
116 | break;
|
---|
117 |
|
---|
118 | case RTR0MEMOBJTYPE_PHYS_NC:
|
---|
119 | AssertMsgFailed(("RTR0MEMOBJTYPE_PHYS_NC\n"));
|
---|
120 | return VERR_INTERNAL_ERROR;
|
---|
121 | break;
|
---|
122 |
|
---|
123 | case RTR0MEMOBJTYPE_RES_VIRT:
|
---|
124 | AssertMsgFailed(("RTR0MEMOBJTYPE_RES_VIRT\n"));
|
---|
125 | return VERR_INTERNAL_ERROR;
|
---|
126 | break;
|
---|
127 |
|
---|
128 | case RTR0MEMOBJTYPE_MAPPING:
|
---|
129 | /* nothing to do here. */
|
---|
130 | break;
|
---|
131 |
|
---|
132 | default:
|
---|
133 | AssertMsgFailed(("enmType=%d\n", pMemDarwin->Core.enmType));
|
---|
134 | return VERR_INTERNAL_ERROR;
|
---|
135 | }
|
---|
136 |
|
---|
137 | return VINF_SUCCESS;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
142 | {
|
---|
143 | /*
|
---|
144 | * Try allocate the memory and create it's IOMemoryDescriptor first.
|
---|
145 | */
|
---|
146 | int rc = VERR_NO_PAGE_MEMORY;
|
---|
147 | AssertCompile(sizeof(IOPhysicalAddress) == 4);
|
---|
148 | void *pv = IOMallocAligned(cb, PAGE_SIZE);
|
---|
149 | if (pv)
|
---|
150 | {
|
---|
151 | IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
|
---|
152 | if (pMemDesc)
|
---|
153 | {
|
---|
154 | /*
|
---|
155 | * Create the IPRT memory object.
|
---|
156 | */
|
---|
157 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PAGE, pv, cb);
|
---|
158 | if (pMemDarwin)
|
---|
159 | {
|
---|
160 | pMemDarwin->pMemDesc = pMemDesc;
|
---|
161 | *ppMem = &pMemDarwin->Core;
|
---|
162 | return VINF_SUCCESS;
|
---|
163 | }
|
---|
164 |
|
---|
165 | rc = VERR_NO_MEMORY;
|
---|
166 | pMemDesc->release();
|
---|
167 | }
|
---|
168 | else
|
---|
169 | rc = VERR_MEMOBJ_INIT_FAILED;
|
---|
170 | IOFreeAligned(pv, cb);
|
---|
171 | }
|
---|
172 | return rc;
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
177 | {
|
---|
178 | #if 1
|
---|
179 | /*
|
---|
180 | * Allocating 128KB for the low page pool can bit a bit exhausting on the kernel,
|
---|
181 | * it frequnetly causes the entire box to lock up on startup.
|
---|
182 | *
|
---|
183 | * So, try allocate the memory using IOMallocAligned first and if we get any high
|
---|
184 | * physical memory we'll release it and fall back on IOMAllocContiguous.
|
---|
185 | */
|
---|
186 | int rc = VERR_NO_PAGE_MEMORY;
|
---|
187 | AssertCompile(sizeof(IOPhysicalAddress) == 4);
|
---|
188 | void *pv = IOMallocAligned(cb, PAGE_SIZE);
|
---|
189 | if (pv)
|
---|
190 | {
|
---|
191 | IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
|
---|
192 | if (pMemDesc)
|
---|
193 | {
|
---|
194 | /*
|
---|
195 | * Check if it's all below 4GB.
|
---|
196 | */
|
---|
197 | for (IOByteCount off = 0; off < cb; off += PAGE_SIZE)
|
---|
198 | {
|
---|
199 | addr64_t Addr = pMemDesc->getPhysicalSegment64(off, NULL);
|
---|
200 | if (Addr > (uint32_t)(_4G - PAGE_SIZE))
|
---|
201 | {
|
---|
202 | /* Ok, we failed, fall back on contiguous allocation. */
|
---|
203 | pMemDesc->release();
|
---|
204 | IOFreeAligned(pv, cb);
|
---|
205 | return rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * Create the IPRT memory object.
|
---|
211 | */
|
---|
212 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOW, pv, cb);
|
---|
213 | if (pMemDarwin)
|
---|
214 | {
|
---|
215 | pMemDarwin->pMemDesc = pMemDesc;
|
---|
216 | *ppMem = &pMemDarwin->Core;
|
---|
217 | return VINF_SUCCESS;
|
---|
218 | }
|
---|
219 |
|
---|
220 | rc = VERR_NO_MEMORY;
|
---|
221 | pMemDesc->release();
|
---|
222 | }
|
---|
223 | else
|
---|
224 | rc = VERR_MEMOBJ_INIT_FAILED;
|
---|
225 | IOFreeAligned(pv, cb);
|
---|
226 | }
|
---|
227 | return rc;
|
---|
228 |
|
---|
229 | #else
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * IOMallocContiguous is the most suitable API.
|
---|
233 | */
|
---|
234 | return rtR0MemObjNativeAllocCont(ppMem, cb, fExecutable);
|
---|
235 | #endif
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable)
|
---|
240 | {
|
---|
241 | /*
|
---|
242 | * Try allocate the memory and create it's IOMemoryDescriptor first.
|
---|
243 | */
|
---|
244 | int rc = VERR_NO_CONT_MEMORY;
|
---|
245 | AssertCompile(sizeof(IOPhysicalAddress) == 4);
|
---|
246 | void *pv = IOMallocContiguous(cb, PAGE_SIZE, NULL);
|
---|
247 | if (pv)
|
---|
248 | {
|
---|
249 | IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, kernel_task);
|
---|
250 | if (pMemDesc)
|
---|
251 | {
|
---|
252 | /* a bit of useful paranoia. */
|
---|
253 | addr64_t PhysAddr = pMemDesc->getPhysicalSegment64(0, NULL);
|
---|
254 | Assert(PhysAddr == pMemDesc->getPhysicalAddress());
|
---|
255 | if ( PhysAddr > 0
|
---|
256 | && PhysAddr <= _4G
|
---|
257 | && PhysAddr + cb <= _4G)
|
---|
258 | {
|
---|
259 | /*
|
---|
260 | * Create the IPRT memory object.
|
---|
261 | */
|
---|
262 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_CONT, pv, cb);
|
---|
263 | if (pMemDarwin)
|
---|
264 | {
|
---|
265 | pMemDarwin->Core.u.Cont.Phys = PhysAddr;
|
---|
266 | pMemDarwin->pMemDesc = pMemDesc;
|
---|
267 | *ppMem = &pMemDarwin->Core;
|
---|
268 | return VINF_SUCCESS;
|
---|
269 | }
|
---|
270 |
|
---|
271 | rc = VERR_NO_MEMORY;
|
---|
272 | }
|
---|
273 | else
|
---|
274 | {
|
---|
275 | AssertMsgFailed(("PhysAddr=%llx\n", (unsigned long long)PhysAddr));
|
---|
276 | rc = VERR_INTERNAL_ERROR;
|
---|
277 | }
|
---|
278 | pMemDesc->release();
|
---|
279 | }
|
---|
280 | else
|
---|
281 | rc = VERR_MEMOBJ_INIT_FAILED;
|
---|
282 | IOFreeContiguous(pv, cb);
|
---|
283 | }
|
---|
284 | return rc;
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
289 | {
|
---|
290 | #if 0 /* turned out IOMallocPhysical isn't exported yet. sigh. */
|
---|
291 | /*
|
---|
292 | * Try allocate the memory and create it's IOMemoryDescriptor first.
|
---|
293 | * Note that IOMallocPhysical is not working correctly (it's ignoring the mask).
|
---|
294 | */
|
---|
295 |
|
---|
296 | /* first calc the mask (in the hope that it'll be used) */
|
---|
297 | IOPhysicalAddress PhysMask = ~(IOPhysicalAddress)PAGE_OFFSET_MASK;
|
---|
298 | if (PhysHighest != NIL_RTHCPHYS)
|
---|
299 | {
|
---|
300 | PhysMask = ~(IOPhysicalAddress)0;
|
---|
301 | while (PhysMask > PhysHighest)
|
---|
302 | PhysMask >>= 1;
|
---|
303 | AssertReturn(PhysMask + 1 < cb, VERR_INVALID_PARAMETER);
|
---|
304 | PhysMask &= ~(IOPhysicalAddress)PAGE_OFFSET_MASK;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /* try allocate physical memory. */
|
---|
308 | int rc = VERR_NO_PHYS_MEMORY;
|
---|
309 | mach_vm_address_t PhysAddr64 = IOMallocPhysical(cb, PhysMask);
|
---|
310 | if (PhysAddr64)
|
---|
311 | {
|
---|
312 | IOPhysicalAddress PhysAddr = PhysAddr64;
|
---|
313 | if ( PhysAddr == PhysAddr64
|
---|
314 | && PhysAddr < PhysHighest
|
---|
315 | && PhysAddr + cb <= PhysHighest)
|
---|
316 | {
|
---|
317 | /* create a descriptor. */
|
---|
318 | IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withPhysicalAddress(PhysAddr, cb, kIODirectionInOut);
|
---|
319 | if (pMemDesc)
|
---|
320 | {
|
---|
321 | Assert(PhysAddr == pMemDesc->getPhysicalAddress());
|
---|
322 |
|
---|
323 | /*
|
---|
324 | * Create the IPRT memory object.
|
---|
325 | */
|
---|
326 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
327 | if (pMemDarwin)
|
---|
328 | {
|
---|
329 | pMemDarwin->Core.u.Phys.PhysBase = PhysAddr;
|
---|
330 | pMemDarwin->Core.u.Phys.fAllocated = true;
|
---|
331 | pMemDarwin->pMemDesc = pMemDesc;
|
---|
332 | *ppMem = &pMemDarwin->Core;
|
---|
333 | return VINF_SUCCESS;
|
---|
334 | }
|
---|
335 |
|
---|
336 | rc = VERR_NO_MEMORY;
|
---|
337 | pMemDesc->release();
|
---|
338 | }
|
---|
339 | else
|
---|
340 | rc = VERR_MEMOBJ_INIT_FAILED;
|
---|
341 | }
|
---|
342 | else
|
---|
343 | {
|
---|
344 | AssertMsgFailed(("PhysAddr=%#llx PhysAddr64=%#llx PhysHigest=%#llx\n", (unsigned long long)PhysAddr,
|
---|
345 | (unsigned long long)PhysAddr64, (unsigned long long)PhysHighest));
|
---|
346 | rc = VERR_INTERNAL_ERROR;
|
---|
347 | }
|
---|
348 |
|
---|
349 | IOFreePhysical(PhysAddr64, cb);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * Just in case IOMallocContiguous doesn't work right, we can try fall back
|
---|
354 | * on a contiguous allcation.
|
---|
355 | */
|
---|
356 | if (rc == VERR_INTERNAL_ERROR || rc == VERR_NO_PHYS_MEMORY)
|
---|
357 | {
|
---|
358 | int rc2 = rtR0MemObjNativeAllocCont(ppMem, cb, false);
|
---|
359 | if (RT_SUCCESS(rc2))
|
---|
360 | rc = rc2;
|
---|
361 | }
|
---|
362 |
|
---|
363 | return rc;
|
---|
364 |
|
---|
365 | #else
|
---|
366 |
|
---|
367 | return rtR0MemObjNativeAllocCont(ppMem, cb, false);
|
---|
368 | #endif
|
---|
369 | }
|
---|
370 |
|
---|
371 |
|
---|
372 | int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest)
|
---|
373 | {
|
---|
374 | /** @todo rtR0MemObjNativeAllocPhys / darwin. */
|
---|
375 | return rtR0MemObjNativeAllocPhys(ppMem, cb, PhysHighest);
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb)
|
---|
380 | {
|
---|
381 | /*
|
---|
382 | * Validate the address range and create a descriptor for it.
|
---|
383 | */
|
---|
384 | int rc = VERR_ADDRESS_TOO_BIG;
|
---|
385 | IOPhysicalAddress PhysAddr = Phys;
|
---|
386 | if (PhysAddr == Phys)
|
---|
387 | {
|
---|
388 | IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withPhysicalAddress(PhysAddr, cb, kIODirectionInOut);
|
---|
389 | if (pMemDesc)
|
---|
390 | {
|
---|
391 | Assert(PhysAddr == pMemDesc->getPhysicalAddress());
|
---|
392 |
|
---|
393 | /*
|
---|
394 | * Create the IPRT memory object.
|
---|
395 | */
|
---|
396 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_PHYS, NULL, cb);
|
---|
397 | if (pMemDarwin)
|
---|
398 | {
|
---|
399 | pMemDarwin->Core.u.Phys.PhysBase = PhysAddr;
|
---|
400 | pMemDarwin->Core.u.Phys.fAllocated = false;
|
---|
401 | pMemDarwin->pMemDesc = pMemDesc;
|
---|
402 | *ppMem = &pMemDarwin->Core;
|
---|
403 | return VINF_SUCCESS;
|
---|
404 | }
|
---|
405 |
|
---|
406 | rc = VERR_NO_MEMORY;
|
---|
407 | pMemDesc->release();
|
---|
408 | }
|
---|
409 | }
|
---|
410 | else
|
---|
411 | AssertMsgFailed(("%#llx\n", (unsigned long long)Phys));
|
---|
412 | return rc;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | /**
|
---|
417 | * Internal worker for locking down pages.
|
---|
418 | *
|
---|
419 | * @return IPRT status code.
|
---|
420 | *
|
---|
421 | * @param ppMem Where to store the memory object pointer.
|
---|
422 | * @param pv First page.
|
---|
423 | * @param cb Number of bytes.
|
---|
424 | * @param Task The task \a pv and \a cb refers to.
|
---|
425 | */
|
---|
426 | static int rtR0MemObjNativeLock(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, task_t Task)
|
---|
427 | {
|
---|
428 | #ifdef USE_VM_MAP_WIRE
|
---|
429 | vm_map_t Map = get_task_map(Task);
|
---|
430 | Assert(Map);
|
---|
431 |
|
---|
432 | /*
|
---|
433 | * First try lock the memory.
|
---|
434 | */
|
---|
435 | int rc = VERR_LOCK_FAILED;
|
---|
436 | kern_return_t kr = vm_map_wire(get_task_map(Task),
|
---|
437 | (vm_map_offset_t)pv,
|
---|
438 | (vm_map_offset_t)pv + cb,
|
---|
439 | VM_PROT_DEFAULT,
|
---|
440 | 0 /* not user */);
|
---|
441 | if (kr == KERN_SUCCESS)
|
---|
442 | {
|
---|
443 | /*
|
---|
444 | * Create the IPRT memory object.
|
---|
445 | */
|
---|
446 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
|
---|
447 | if (pMemDarwin)
|
---|
448 | {
|
---|
449 | pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
|
---|
450 | *ppMem = &pMemDarwin->Core;
|
---|
451 | return VINF_SUCCESS;
|
---|
452 | }
|
---|
453 |
|
---|
454 | kr = vm_map_unwire(get_task_map(Task), (vm_map_offset_t)pv, (vm_map_offset_t)pv + cb, 0 /* not user */);
|
---|
455 | Assert(kr == KERN_SUCCESS);
|
---|
456 | rc = VERR_NO_MEMORY;
|
---|
457 | }
|
---|
458 |
|
---|
459 | #else
|
---|
460 |
|
---|
461 | /*
|
---|
462 | * Create a descriptor and try lock it (prepare).
|
---|
463 | */
|
---|
464 | int rc = VERR_MEMOBJ_INIT_FAILED;
|
---|
465 | IOMemoryDescriptor *pMemDesc = IOMemoryDescriptor::withAddress((vm_address_t)pv, cb, kIODirectionInOut, Task);
|
---|
466 | if (pMemDesc)
|
---|
467 | {
|
---|
468 | IOReturn IORet = pMemDesc->prepare(kIODirectionInOut);
|
---|
469 | if (IORet == kIOReturnSuccess)
|
---|
470 | {
|
---|
471 | /*
|
---|
472 | * Create the IPRT memory object.
|
---|
473 | */
|
---|
474 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_LOCK, pv, cb);
|
---|
475 | if (pMemDarwin)
|
---|
476 | {
|
---|
477 | pMemDarwin->Core.u.Lock.R0Process = (RTR0PROCESS)Task;
|
---|
478 | pMemDarwin->pMemDesc = pMemDesc;
|
---|
479 | *ppMem = &pMemDarwin->Core;
|
---|
480 | return VINF_SUCCESS;
|
---|
481 | }
|
---|
482 |
|
---|
483 | pMemDesc->complete();
|
---|
484 | rc = VERR_NO_MEMORY;
|
---|
485 | }
|
---|
486 | else
|
---|
487 | rc = VERR_LOCK_FAILED;
|
---|
488 | pMemDesc->release();
|
---|
489 | }
|
---|
490 | #endif
|
---|
491 | return rc;
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process)
|
---|
496 | {
|
---|
497 | return rtR0MemObjNativeLock(ppMem, (void *)R3Ptr, cb, (task_t)R0Process);
|
---|
498 | }
|
---|
499 |
|
---|
500 |
|
---|
501 | int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb)
|
---|
502 | {
|
---|
503 | return rtR0MemObjNativeLock(ppMem, pv, cb, kernel_task);
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 | int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment)
|
---|
508 | {
|
---|
509 | return VERR_NOT_IMPLEMENTED;
|
---|
510 | }
|
---|
511 |
|
---|
512 |
|
---|
513 | int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
|
---|
514 | {
|
---|
515 | return VERR_NOT_IMPLEMENTED;
|
---|
516 | }
|
---|
517 |
|
---|
518 |
|
---|
519 | int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
|
---|
520 | {
|
---|
521 | /*
|
---|
522 | * Must have a memory descriptor.
|
---|
523 | */
|
---|
524 | int rc = VERR_INVALID_PARAMETER;
|
---|
525 | PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
|
---|
526 | if (pMemToMapDarwin->pMemDesc)
|
---|
527 | {
|
---|
528 | IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map(kernel_task, kIOMapAnywhere,
|
---|
529 | kIOMapAnywhere | kIOMapDefaultCache);
|
---|
530 | if (pMemMap)
|
---|
531 | {
|
---|
532 | IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
|
---|
533 | void *pv = (void *)(uintptr_t)VirtAddr;
|
---|
534 | if ((uintptr_t)pv == VirtAddr)
|
---|
535 | {
|
---|
536 | /*
|
---|
537 | * Create the IPRT memory object.
|
---|
538 | */
|
---|
539 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
|
---|
540 | pv, pMemToMapDarwin->Core.cb);
|
---|
541 | if (pMemDarwin)
|
---|
542 | {
|
---|
543 | pMemDarwin->Core.u.Mapping.R0Process = NIL_RTR0PROCESS;
|
---|
544 | pMemDarwin->pMemMap = pMemMap;
|
---|
545 | *ppMem = &pMemDarwin->Core;
|
---|
546 | return VINF_SUCCESS;
|
---|
547 | }
|
---|
548 |
|
---|
549 | rc = VERR_NO_MEMORY;
|
---|
550 | }
|
---|
551 | else
|
---|
552 | rc = VERR_ADDRESS_TOO_BIG;
|
---|
553 | pMemMap->release();
|
---|
554 | }
|
---|
555 | else
|
---|
556 | rc = VERR_MAP_FAILED;
|
---|
557 | }
|
---|
558 | return rc;
|
---|
559 | }
|
---|
560 |
|
---|
561 |
|
---|
562 | int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
|
---|
563 | {
|
---|
564 | /*
|
---|
565 | * Must have a memory descriptor.
|
---|
566 | */
|
---|
567 | int rc = VERR_INVALID_PARAMETER;
|
---|
568 | PRTR0MEMOBJDARWIN pMemToMapDarwin = (PRTR0MEMOBJDARWIN)pMemToMap;
|
---|
569 | if (pMemToMapDarwin->pMemDesc)
|
---|
570 | {
|
---|
571 | IOMemoryMap *pMemMap = pMemToMapDarwin->pMemDesc->map((task_t)R0Process, kIOMapAnywhere,
|
---|
572 | kIOMapAnywhere | kIOMapDefaultCache);
|
---|
573 | if (pMemMap)
|
---|
574 | {
|
---|
575 | IOVirtualAddress VirtAddr = pMemMap->getVirtualAddress();
|
---|
576 | void *pv = (void *)(uintptr_t)VirtAddr;
|
---|
577 | if ((uintptr_t)pv == VirtAddr)
|
---|
578 | {
|
---|
579 | /*
|
---|
580 | * Create the IPRT memory object.
|
---|
581 | */
|
---|
582 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)rtR0MemObjNew(sizeof(*pMemDarwin), RTR0MEMOBJTYPE_MAPPING,
|
---|
583 | pv, pMemToMapDarwin->Core.cb);
|
---|
584 | if (pMemDarwin)
|
---|
585 | {
|
---|
586 | pMemDarwin->Core.u.Mapping.R0Process = R0Process;
|
---|
587 | pMemDarwin->pMemMap = pMemMap;
|
---|
588 | *ppMem = &pMemDarwin->Core;
|
---|
589 | return VINF_SUCCESS;
|
---|
590 | }
|
---|
591 |
|
---|
592 | rc = VERR_NO_MEMORY;
|
---|
593 | }
|
---|
594 | else
|
---|
595 | rc = VERR_ADDRESS_TOO_BIG;
|
---|
596 | pMemMap->release();
|
---|
597 | }
|
---|
598 | else
|
---|
599 | rc = VERR_MAP_FAILED;
|
---|
600 | }
|
---|
601 | return rc;
|
---|
602 | }
|
---|
603 |
|
---|
604 |
|
---|
605 | RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage)
|
---|
606 | {
|
---|
607 | RTHCPHYS PhysAddr;
|
---|
608 | PRTR0MEMOBJDARWIN pMemDarwin = (PRTR0MEMOBJDARWIN)pMem;
|
---|
609 |
|
---|
610 | #ifdef USE_VM_MAP_WIRE
|
---|
611 | /*
|
---|
612 | * Locked memory doesn't have a memory descriptor and
|
---|
613 | * needs to be handled differently.
|
---|
614 | */
|
---|
615 | if (pMemDarwin->Core.enmType == RTR0MEMOBJTYPE_LOCK)
|
---|
616 | {
|
---|
617 | ppnum_t PgNo;
|
---|
618 | if (pMemDarwin->Core.u.Lock.R0Process == NIL_RTR0PROCESS)
|
---|
619 | PgNo = pmap_find_phys(kernel_pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
|
---|
620 | else
|
---|
621 | {
|
---|
622 | /*
|
---|
623 | * From what I can tell, Apple seems to have locked up the all the
|
---|
624 | * available interfaces that could help us obtain the pmap_t of a task
|
---|
625 | * or vm_map_t.
|
---|
626 |
|
---|
627 | * So, we'll have to figure out where in the vm_map_t structure it is
|
---|
628 | * and read it our selves. ASSUMING that kernel_pmap is pointed to by
|
---|
629 | * kernel_map->pmap, we scan kernel_map to locate the structure offset.
|
---|
630 | * Not nice, but it will hopefully do the job in a reliable manner...
|
---|
631 | *
|
---|
632 | * (get_task_pmap, get_map_pmap or vm_map_pmap is what we really need btw.)
|
---|
633 | */
|
---|
634 | static int s_offPmap = -1;
|
---|
635 | if (RT_UNLIKELY(s_offPmap == -1))
|
---|
636 | {
|
---|
637 | pmap_t const *p = (pmap_t *)kernel_map;
|
---|
638 | pmap_t const * const pEnd = p + 64;
|
---|
639 | for (; p < pEnd; p++)
|
---|
640 | if (*p == kernel_pmap)
|
---|
641 | {
|
---|
642 | s_offPmap = (uintptr_t)p - (uintptr_t)kernel_map;
|
---|
643 | break;
|
---|
644 | }
|
---|
645 | AssertReturn(s_offPmap >= 0, NIL_RTHCPHYS);
|
---|
646 | }
|
---|
647 | pmap_t Pmap = *(pmap_t *)((uintptr_t)get_task_map((task_t)pMemDarwin->Core.u.Lock.R0Process) + s_offPmap);
|
---|
648 | PgNo = pmap_find_phys(Pmap, (uintptr_t)pMemDarwin->Core.pv + iPage * PAGE_SIZE);
|
---|
649 | }
|
---|
650 |
|
---|
651 | AssertReturn(PgNo, NIL_RTHCPHYS);
|
---|
652 | PhysAddr = (RTHCPHYS)PgNo << PAGE_SHIFT;
|
---|
653 | Assert((PhysAddr >> PAGE_SHIFT) == PgNo);
|
---|
654 | }
|
---|
655 | else
|
---|
656 | #endif /* USE_VM_MAP_WIRE */
|
---|
657 | {
|
---|
658 | /*
|
---|
659 | * Get the memory descriptor.
|
---|
660 | */
|
---|
661 | IOMemoryDescriptor *pMemDesc = pMemDarwin->pMemDesc;
|
---|
662 | if (!pMemDesc)
|
---|
663 | pMemDesc = pMemDarwin->pMemMap->getMemoryDescriptor();
|
---|
664 | AssertReturn(pMemDesc, NIL_RTHCPHYS);
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * If we've got a memory descriptor, use getPhysicalSegment64().
|
---|
668 | */
|
---|
669 | addr64_t Addr = pMemDesc->getPhysicalSegment64(iPage * PAGE_SIZE, NULL);
|
---|
670 | AssertMsgReturn(Addr, ("iPage=%u\n", iPage), NIL_RTHCPHYS);
|
---|
671 | PhysAddr = Addr;
|
---|
672 | AssertMsgReturn(PhysAddr == Addr, ("PhysAddr=%VHp Addr=%RX64\n", PhysAddr, (uint64_t)Addr), NIL_RTHCPHYS);
|
---|
673 | }
|
---|
674 |
|
---|
675 | return PhysAddr;
|
---|
676 | }
|
---|
677 |
|
---|