1 | /* $Id: alloc-r0drv-darwin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "the-darwin-kernel.h"
|
---|
32 | #include "internal/iprt.h"
|
---|
33 | #include <iprt/mem.h>
|
---|
34 | #include <iprt/memobj.h>
|
---|
35 |
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/thread.h>
|
---|
39 | #include "r0drv/alloc-r0drv.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | /**
|
---|
46 | * Extended header used for headers marked with RTMEMHDR_FLAG_EXEC.
|
---|
47 | *
|
---|
48 | * This is used with allocating executable memory, for things like generated
|
---|
49 | * code and loaded modules.
|
---|
50 | */
|
---|
51 | typedef struct RTMEMDARWINHDREX
|
---|
52 | {
|
---|
53 | /** The associated memory object. */
|
---|
54 | RTR0MEMOBJ hMemObj;
|
---|
55 | /** Alignment padding. */
|
---|
56 | uint8_t abPadding[ARCH_BITS == 32 ? 12 : 8];
|
---|
57 | /** The header we present to the generic API. */
|
---|
58 | RTMEMHDR Hdr;
|
---|
59 | } RTMEMDARWINHDREX;
|
---|
60 | AssertCompileSize(RTMEMDARWINHDREX, 32);
|
---|
61 | /** Pointer to an extended memory header. */
|
---|
62 | typedef RTMEMDARWINHDREX *PRTMEMDARWINHDREX;
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * OS specific allocation function.
|
---|
67 | */
|
---|
68 | DECLHIDDEN(int) rtR0MemAllocEx(size_t cb, uint32_t fFlags, PRTMEMHDR *ppHdr)
|
---|
69 | {
|
---|
70 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
71 |
|
---|
72 | if (RT_UNLIKELY(fFlags & RTMEMHDR_FLAG_ANY_CTX))
|
---|
73 | return VERR_NOT_SUPPORTED;
|
---|
74 |
|
---|
75 | PRTMEMHDR pHdr;
|
---|
76 | if (fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
77 | {
|
---|
78 | RTR0MEMOBJ hMemObj;
|
---|
79 | int rc = RTR0MemObjAllocPage(&hMemObj, cb + sizeof(RTMEMDARWINHDREX), true /*fExecutable*/);
|
---|
80 | if (RT_FAILURE(rc))
|
---|
81 | {
|
---|
82 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
83 | return rc;
|
---|
84 | }
|
---|
85 | PRTMEMDARWINHDREX pExHdr = (PRTMEMDARWINHDREX)RTR0MemObjAddress(hMemObj);
|
---|
86 | pExHdr->hMemObj = hMemObj;
|
---|
87 | pHdr = &pExHdr->Hdr;
|
---|
88 | #if 1 /*fExecutable isn't currently honored above. */
|
---|
89 | rc = RTR0MemObjProtect(hMemObj, 0, RTR0MemObjSize(hMemObj), RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC);
|
---|
90 | AssertRC(rc);
|
---|
91 | #endif
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | pHdr = (PRTMEMHDR)IOMalloc(cb + sizeof(*pHdr));
|
---|
96 | if (RT_UNLIKELY(!pHdr))
|
---|
97 | {
|
---|
98 | printf("rtR0MemAllocEx(%#zx, %#x) failed\n", cb + sizeof(*pHdr), fFlags);
|
---|
99 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
100 | return VERR_NO_MEMORY;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | pHdr->u32Magic = RTMEMHDR_MAGIC;
|
---|
105 | pHdr->fFlags = fFlags;
|
---|
106 | pHdr->cb = cb;
|
---|
107 | pHdr->cbReq = cb;
|
---|
108 | *ppHdr = pHdr;
|
---|
109 |
|
---|
110 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * OS specific free function.
|
---|
117 | */
|
---|
118 | DECLHIDDEN(void) rtR0MemFree(PRTMEMHDR pHdr)
|
---|
119 | {
|
---|
120 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
121 |
|
---|
122 | pHdr->u32Magic += 1;
|
---|
123 | if (pHdr->fFlags & RTMEMHDR_FLAG_EXEC)
|
---|
124 | {
|
---|
125 | PRTMEMDARWINHDREX pExHdr = RT_FROM_MEMBER(pHdr, RTMEMDARWINHDREX, Hdr);
|
---|
126 | int rc = RTR0MemObjFree(pExHdr->hMemObj, false /*fFreeMappings*/);
|
---|
127 | AssertRC(rc);
|
---|
128 | }
|
---|
129 | else
|
---|
130 | IOFree(pHdr, pHdr->cb + sizeof(*pHdr));
|
---|
131 |
|
---|
132 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | RTR0DECL(void *) RTMemContAlloc(PRTCCPHYS pPhys, size_t cb)
|
---|
137 | {
|
---|
138 | /*
|
---|
139 | * validate input.
|
---|
140 | */
|
---|
141 | AssertPtr(pPhys);
|
---|
142 | Assert(cb > 0);
|
---|
143 | RT_ASSERT_PREEMPTIBLE();
|
---|
144 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
145 |
|
---|
146 | /*
|
---|
147 | * Allocate the memory and ensure that the API is still providing
|
---|
148 | * memory that's always below 4GB.
|
---|
149 | */
|
---|
150 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
151 | IOPhysicalAddress PhysAddr;
|
---|
152 | void *pv = IOMallocContiguous(cb, PAGE_SIZE, &PhysAddr);
|
---|
153 | if (pv)
|
---|
154 | {
|
---|
155 | if (PhysAddr + (cb - 1) <= (IOPhysicalAddress)0xffffffff)
|
---|
156 | {
|
---|
157 | if (!((uintptr_t)pv & PAGE_OFFSET_MASK))
|
---|
158 | {
|
---|
159 | *pPhys = PhysAddr;
|
---|
160 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
161 | return pv;
|
---|
162 | }
|
---|
163 | AssertMsgFailed(("IOMallocContiguous didn't return a page aligned address - %p!\n", pv));
|
---|
164 | }
|
---|
165 | else
|
---|
166 | AssertMsgFailed(("IOMallocContiguous returned high address! PhysAddr=%RX64 cb=%#zx\n", (uint64_t)PhysAddr, cb));
|
---|
167 | IOFreeContiguous(pv, cb);
|
---|
168 | }
|
---|
169 |
|
---|
170 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
171 | return NULL;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | RTR0DECL(void) RTMemContFree(void *pv, size_t cb)
|
---|
176 | {
|
---|
177 | RT_ASSERT_PREEMPTIBLE();
|
---|
178 | if (pv)
|
---|
179 | {
|
---|
180 | Assert(cb > 0);
|
---|
181 | AssertMsg(!((uintptr_t)pv & PAGE_OFFSET_MASK), ("pv=%p\n", pv));
|
---|
182 | IPRT_DARWIN_SAVE_EFL_AC();
|
---|
183 |
|
---|
184 | cb = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
185 | IOFreeContiguous(pv, cb);
|
---|
186 |
|
---|
187 | IPRT_DARWIN_RESTORE_EFL_AC();
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|