1 | /* $Id: memobj-r0drv.cpp 104848 2024-06-05 09:38:20Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Ring-0 Memory Objects, Common Code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_DEFAULT /// @todo RTLOGGROUP_MEM
|
---|
42 | #define RTMEM_NO_WRAP_TO_EF_APIS /* circular dependency otherwise. */
|
---|
43 | #include <iprt/memobj.h>
|
---|
44 | #include "internal/iprt.h"
|
---|
45 |
|
---|
46 | #include <iprt/alloc.h>
|
---|
47 | #include <iprt/asm.h>
|
---|
48 | #include <iprt/assert.h>
|
---|
49 | #include <iprt/err.h>
|
---|
50 | #include <iprt/log.h>
|
---|
51 | #include <iprt/mp.h>
|
---|
52 | #include <iprt/param.h>
|
---|
53 | #include <iprt/process.h>
|
---|
54 | #include <iprt/string.h>
|
---|
55 | #include <iprt/thread.h>
|
---|
56 |
|
---|
57 | #include "internal/memobj.h"
|
---|
58 |
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Internal function for allocating a new memory object.
|
---|
62 | *
|
---|
63 | * @returns The allocated and initialized handle.
|
---|
64 | * @param cbSelf The size of the memory object handle. 0 mean default size.
|
---|
65 | * @param enmType The memory object type.
|
---|
66 | * @param pv The memory object mapping.
|
---|
67 | * @param cb The size of the memory object.
|
---|
68 | * @param pszTag The tag string.
|
---|
69 | */
|
---|
70 | DECLHIDDEN(PRTR0MEMOBJINTERNAL) rtR0MemObjNew(size_t cbSelf, RTR0MEMOBJTYPE enmType, void *pv, size_t cb, const char *pszTag)
|
---|
71 | {
|
---|
72 | PRTR0MEMOBJINTERNAL pNew;
|
---|
73 |
|
---|
74 | /* validate the size */
|
---|
75 | if (!cbSelf)
|
---|
76 | cbSelf = sizeof(*pNew);
|
---|
77 | Assert(cbSelf >= sizeof(*pNew));
|
---|
78 | Assert(cbSelf == (uint32_t)cbSelf);
|
---|
79 | AssertMsg(RT_ALIGN_Z(cb, PAGE_SIZE) == cb, ("%#zx\n", cb));
|
---|
80 |
|
---|
81 | /*
|
---|
82 | * Allocate and initialize the object.
|
---|
83 | */
|
---|
84 | pNew = (PRTR0MEMOBJINTERNAL)RTMemAllocZ(cbSelf);
|
---|
85 | if (pNew)
|
---|
86 | {
|
---|
87 | pNew->u32Magic = RTR0MEMOBJ_MAGIC;
|
---|
88 | pNew->cbSelf = (uint32_t)cbSelf;
|
---|
89 | pNew->enmType = enmType;
|
---|
90 | pNew->fFlags = 0;
|
---|
91 | pNew->cb = cb;
|
---|
92 | pNew->pv = pv;
|
---|
93 | #ifdef DEBUG
|
---|
94 | pNew->pszTag = pszTag;
|
---|
95 | #else
|
---|
96 | RT_NOREF_PV(pszTag);
|
---|
97 | #endif
|
---|
98 | }
|
---|
99 | return pNew;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Deletes an incomplete memory object.
|
---|
105 | *
|
---|
106 | * This is for cleaning up after failures during object creation.
|
---|
107 | *
|
---|
108 | * @param pMem The incomplete memory object to delete.
|
---|
109 | */
|
---|
110 | DECLHIDDEN(void) rtR0MemObjDelete(PRTR0MEMOBJINTERNAL pMem)
|
---|
111 | {
|
---|
112 | if (pMem)
|
---|
113 | {
|
---|
114 | ASMAtomicUoWriteU32(&pMem->u32Magic, ~RTR0MEMOBJ_MAGIC);
|
---|
115 | pMem->enmType = RTR0MEMOBJTYPE_END;
|
---|
116 | RTMemFree(pMem);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Links a mapping object to a primary object.
|
---|
123 | *
|
---|
124 | * @returns IPRT status code.
|
---|
125 | * @retval VINF_SUCCESS on success.
|
---|
126 | * @retval VINF_NO_MEMORY if we couldn't expand the mapping array of the parent.
|
---|
127 | * @param pParent The parent (primary) memory object.
|
---|
128 | * @param pChild The child (mapping) memory object.
|
---|
129 | */
|
---|
130 | static int rtR0MemObjLink(PRTR0MEMOBJINTERNAL pParent, PRTR0MEMOBJINTERNAL pChild)
|
---|
131 | {
|
---|
132 | uint32_t i;
|
---|
133 |
|
---|
134 | /* sanity */
|
---|
135 | Assert(rtR0MemObjIsMapping(pChild));
|
---|
136 | Assert(!rtR0MemObjIsMapping(pParent));
|
---|
137 |
|
---|
138 | /* expand the array? */
|
---|
139 | i = pParent->uRel.Parent.cMappings;
|
---|
140 | if (i >= pParent->uRel.Parent.cMappingsAllocated)
|
---|
141 | {
|
---|
142 | void *pv = RTMemRealloc(pParent->uRel.Parent.papMappings,
|
---|
143 | (i + 32) * sizeof(pParent->uRel.Parent.papMappings[0]));
|
---|
144 | if (!pv)
|
---|
145 | return VERR_NO_MEMORY;
|
---|
146 | pParent->uRel.Parent.papMappings = (PPRTR0MEMOBJINTERNAL)pv;
|
---|
147 | pParent->uRel.Parent.cMappingsAllocated = i + 32;
|
---|
148 | Assert(i == pParent->uRel.Parent.cMappings);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* do the linking. */
|
---|
152 | pParent->uRel.Parent.papMappings[i] = pChild;
|
---|
153 | pParent->uRel.Parent.cMappings++;
|
---|
154 | pChild->uRel.Child.pParent = pParent;
|
---|
155 |
|
---|
156 | return VINF_SUCCESS;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | RTR0DECL(bool) RTR0MemObjIsMapping(RTR0MEMOBJ MemObj)
|
---|
161 | {
|
---|
162 | /* Validate the object handle. */
|
---|
163 | PRTR0MEMOBJINTERNAL pMem;
|
---|
164 | AssertPtrReturn(MemObj, false);
|
---|
165 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
166 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), false);
|
---|
167 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), false);
|
---|
168 |
|
---|
169 | /* hand it on to the inlined worker. */
|
---|
170 | return rtR0MemObjIsMapping(pMem);
|
---|
171 | }
|
---|
172 | RT_EXPORT_SYMBOL(RTR0MemObjIsMapping);
|
---|
173 |
|
---|
174 |
|
---|
175 | RTR0DECL(void *) RTR0MemObjAddress(RTR0MEMOBJ MemObj)
|
---|
176 | {
|
---|
177 | /* Validate the object handle. */
|
---|
178 | PRTR0MEMOBJINTERNAL pMem;
|
---|
179 | if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
|
---|
180 | return NULL;
|
---|
181 | AssertPtrReturn(MemObj, NULL);
|
---|
182 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
183 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NULL);
|
---|
184 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NULL);
|
---|
185 |
|
---|
186 | /* return the mapping address. */
|
---|
187 | return pMem->pv;
|
---|
188 | }
|
---|
189 | RT_EXPORT_SYMBOL(RTR0MemObjAddress);
|
---|
190 |
|
---|
191 |
|
---|
192 | RTR0DECL(RTR3PTR) RTR0MemObjAddressR3(RTR0MEMOBJ MemObj)
|
---|
193 | {
|
---|
194 | PRTR0MEMOBJINTERNAL pMem;
|
---|
195 |
|
---|
196 | /* Validate the object handle. */
|
---|
197 | if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
|
---|
198 | return NIL_RTR3PTR;
|
---|
199 | AssertPtrReturn(MemObj, NIL_RTR3PTR);
|
---|
200 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
201 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NIL_RTR3PTR);
|
---|
202 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NIL_RTR3PTR);
|
---|
203 | if (RT_UNLIKELY( ( pMem->enmType != RTR0MEMOBJTYPE_MAPPING
|
---|
204 | || pMem->u.Mapping.R0Process == NIL_RTR0PROCESS)
|
---|
205 | && ( pMem->enmType != RTR0MEMOBJTYPE_LOCK
|
---|
206 | || pMem->u.Lock.R0Process == NIL_RTR0PROCESS)
|
---|
207 | && ( pMem->enmType != RTR0MEMOBJTYPE_PHYS_NC
|
---|
208 | || pMem->u.Lock.R0Process == NIL_RTR0PROCESS)
|
---|
209 | && ( pMem->enmType != RTR0MEMOBJTYPE_RES_VIRT
|
---|
210 | || pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS)))
|
---|
211 | return NIL_RTR3PTR;
|
---|
212 |
|
---|
213 | /* return the mapping address. */
|
---|
214 | return (RTR3PTR)pMem->pv;
|
---|
215 | }
|
---|
216 | RT_EXPORT_SYMBOL(RTR0MemObjAddressR3);
|
---|
217 |
|
---|
218 |
|
---|
219 | RTR0DECL(size_t) RTR0MemObjSize(RTR0MEMOBJ MemObj)
|
---|
220 | {
|
---|
221 | PRTR0MEMOBJINTERNAL pMem;
|
---|
222 |
|
---|
223 | /* Validate the object handle. */
|
---|
224 | if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
|
---|
225 | return 0;
|
---|
226 | AssertPtrReturn(MemObj, 0);
|
---|
227 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
228 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), 0);
|
---|
229 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), 0);
|
---|
230 | AssertMsg(RT_ALIGN_Z(pMem->cb, PAGE_SIZE) == pMem->cb, ("%#zx\n", pMem->cb));
|
---|
231 |
|
---|
232 | /* return the size. */
|
---|
233 | return pMem->cb;
|
---|
234 | }
|
---|
235 | RT_EXPORT_SYMBOL(RTR0MemObjSize);
|
---|
236 |
|
---|
237 |
|
---|
238 | /* Work around gcc bug 55940 */
|
---|
239 | #if defined(__GNUC__) && defined(RT_ARCH_X86) && (__GNUC__ * 100 + __GNUC_MINOR__) == 407
|
---|
240 | __attribute__((__optimize__ ("no-shrink-wrap")))
|
---|
241 | #endif
|
---|
242 | RTR0DECL(RTHCPHYS) RTR0MemObjGetPagePhysAddr(RTR0MEMOBJ MemObj, size_t iPage)
|
---|
243 | {
|
---|
244 | /* Validate the object handle. */
|
---|
245 | PRTR0MEMOBJINTERNAL pMem;
|
---|
246 | size_t cPages;
|
---|
247 | AssertPtrReturn(MemObj, NIL_RTHCPHYS);
|
---|
248 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
249 | AssertReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, NIL_RTHCPHYS);
|
---|
250 | AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, NIL_RTHCPHYS);
|
---|
251 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NIL_RTHCPHYS);
|
---|
252 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NIL_RTHCPHYS);
|
---|
253 | cPages = (pMem->cb >> PAGE_SHIFT);
|
---|
254 | if (iPage >= cPages)
|
---|
255 | {
|
---|
256 | /* permit: while (RTR0MemObjGetPagePhysAddr(pMem, iPage++) != NIL_RTHCPHYS) {} */
|
---|
257 | if (iPage == cPages)
|
---|
258 | return NIL_RTHCPHYS;
|
---|
259 | AssertReturn(iPage < (pMem->cb >> PAGE_SHIFT), NIL_RTHCPHYS);
|
---|
260 | }
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * We know the address of physically contiguous allocations and mappings.
|
---|
264 | */
|
---|
265 | if (pMem->enmType == RTR0MEMOBJTYPE_CONT)
|
---|
266 | return pMem->u.Cont.Phys + iPage * PAGE_SIZE;
|
---|
267 | if (pMem->enmType == RTR0MEMOBJTYPE_PHYS)
|
---|
268 | return pMem->u.Phys.PhysBase + iPage * PAGE_SIZE;
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Do the job.
|
---|
272 | */
|
---|
273 | return rtR0MemObjNativeGetPagePhysAddr(pMem, iPage);
|
---|
274 | }
|
---|
275 | RT_EXPORT_SYMBOL(RTR0MemObjGetPagePhysAddr);
|
---|
276 |
|
---|
277 |
|
---|
278 | RTR0DECL(bool) RTR0MemObjWasZeroInitialized(RTR0MEMOBJ hMemObj)
|
---|
279 | {
|
---|
280 | PRTR0MEMOBJINTERNAL pMem;
|
---|
281 |
|
---|
282 | /* Validate the object handle. */
|
---|
283 | if (RT_UNLIKELY(hMemObj == NIL_RTR0MEMOBJ))
|
---|
284 | return false;
|
---|
285 | AssertPtrReturn(hMemObj, false);
|
---|
286 | pMem = (PRTR0MEMOBJINTERNAL)hMemObj;
|
---|
287 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), false);
|
---|
288 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), false);
|
---|
289 | Assert( (pMem->fFlags & (RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC | RTR0MEMOBJ_FLAGS_UNINITIALIZED_AT_ALLOC))
|
---|
290 | != (RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC | RTR0MEMOBJ_FLAGS_UNINITIALIZED_AT_ALLOC));
|
---|
291 |
|
---|
292 | /* return the alloc init state. */
|
---|
293 | return (pMem->fFlags & (RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC | RTR0MEMOBJ_FLAGS_UNINITIALIZED_AT_ALLOC))
|
---|
294 | == RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC;
|
---|
295 | }
|
---|
296 | RT_EXPORT_SYMBOL(RTR0MemObjWasZeroInitialized);
|
---|
297 |
|
---|
298 |
|
---|
299 | RTR0DECL(int) RTR0MemObjZeroInitialize(RTR0MEMOBJ hMemObj, bool fForce)
|
---|
300 | {
|
---|
301 | PRTR0MEMOBJINTERNAL pMem;
|
---|
302 |
|
---|
303 | /* Validate the object handle. */
|
---|
304 | AssertReturn(hMemObj != NIL_RTR0MEMOBJ, VERR_INVALID_HANDLE);
|
---|
305 | AssertPtrReturn(hMemObj, VERR_INVALID_HANDLE);
|
---|
306 | pMem = (PRTR0MEMOBJINTERNAL)hMemObj;
|
---|
307 | AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), VERR_INVALID_HANDLE);
|
---|
308 | AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), VERR_INVALID_HANDLE);
|
---|
309 | AssertReturn( (pMem->enmType != RTR0MEMOBJTYPE_MAPPING || pMem->u.Mapping.R0Process == NIL_RTR0PROCESS)
|
---|
310 | && (pMem->enmType != RTR0MEMOBJTYPE_LOCK || pMem->u.Lock.R0Process == NIL_RTR0PROCESS)
|
---|
311 | && pMem->enmType != RTR0MEMOBJTYPE_RES_VIRT
|
---|
312 | , VERR_WRONG_TYPE);
|
---|
313 | Assert( (pMem->fFlags & (RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC | RTR0MEMOBJ_FLAGS_UNINITIALIZED_AT_ALLOC))
|
---|
314 | != (RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC | RTR0MEMOBJ_FLAGS_UNINITIALIZED_AT_ALLOC));
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Do we need to do anything?
|
---|
318 | */
|
---|
319 | if ( fForce
|
---|
320 | || (pMem->fFlags & (RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC | RTR0MEMOBJ_FLAGS_UNINITIALIZED_AT_ALLOC))
|
---|
321 | != RTR0MEMOBJ_FLAGS_ZERO_AT_ALLOC)
|
---|
322 | {
|
---|
323 | /* This is easy if there is a ring-0 mapping: */
|
---|
324 | if (pMem->pv)
|
---|
325 | RT_BZERO(pMem->pv, pMem->cb);
|
---|
326 | else
|
---|
327 | return rtR0MemObjNativeZeroInitWithoutMapping(pMem);
|
---|
328 | }
|
---|
329 | return VINF_SUCCESS;
|
---|
330 | }
|
---|
331 | RT_EXPORT_SYMBOL(RTR0MemObjWasZeroInitialized);
|
---|
332 |
|
---|
333 |
|
---|
334 | RTR0DECL(int) RTR0MemObjFree(RTR0MEMOBJ MemObj, bool fFreeMappings)
|
---|
335 | {
|
---|
336 | /*
|
---|
337 | * Validate the object handle.
|
---|
338 | */
|
---|
339 | PRTR0MEMOBJINTERNAL pMem;
|
---|
340 | int rc;
|
---|
341 |
|
---|
342 | if (MemObj == NIL_RTR0MEMOBJ)
|
---|
343 | return VINF_SUCCESS;
|
---|
344 | AssertPtrReturn(MemObj, VERR_INVALID_HANDLE);
|
---|
345 | pMem = (PRTR0MEMOBJINTERNAL)MemObj;
|
---|
346 | AssertReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
347 | AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
348 | RT_ASSERT_PREEMPTIBLE();
|
---|
349 |
|
---|
350 | /*
|
---|
351 | * Deal with mappings according to fFreeMappings.
|
---|
352 | */
|
---|
353 | if ( !rtR0MemObjIsMapping(pMem)
|
---|
354 | && pMem->uRel.Parent.cMappings > 0)
|
---|
355 | {
|
---|
356 | /* fail if not requested to free mappings. */
|
---|
357 | if (!fFreeMappings)
|
---|
358 | return VERR_MEMORY_BUSY;
|
---|
359 |
|
---|
360 | while (pMem->uRel.Parent.cMappings > 0)
|
---|
361 | {
|
---|
362 | PRTR0MEMOBJINTERNAL pChild = pMem->uRel.Parent.papMappings[--pMem->uRel.Parent.cMappings];
|
---|
363 | pMem->uRel.Parent.papMappings[pMem->uRel.Parent.cMappings] = NULL;
|
---|
364 |
|
---|
365 | /* sanity checks. */
|
---|
366 | AssertPtr(pChild);
|
---|
367 | AssertFatal(pChild->u32Magic == RTR0MEMOBJ_MAGIC);
|
---|
368 | AssertFatal(pChild->enmType > RTR0MEMOBJTYPE_INVALID && pChild->enmType < RTR0MEMOBJTYPE_END);
|
---|
369 | AssertFatal(rtR0MemObjIsMapping(pChild));
|
---|
370 |
|
---|
371 | /* free the mapping. */
|
---|
372 | rc = rtR0MemObjNativeFree(pChild);
|
---|
373 | if (RT_FAILURE(rc))
|
---|
374 | {
|
---|
375 | Log(("RTR0MemObjFree: failed to free mapping %p: %p %#zx; rc=%Rrc\n", pChild, pChild->pv, pChild->cb, rc));
|
---|
376 | pMem->uRel.Parent.papMappings[pMem->uRel.Parent.cMappings++] = pChild;
|
---|
377 | return rc;
|
---|
378 | }
|
---|
379 |
|
---|
380 | pChild->u32Magic++;
|
---|
381 | pChild->enmType = RTR0MEMOBJTYPE_END;
|
---|
382 | RTMemFree(pChild);
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Free this object.
|
---|
388 | */
|
---|
389 | rc = rtR0MemObjNativeFree(pMem);
|
---|
390 | if (RT_SUCCESS(rc))
|
---|
391 | {
|
---|
392 | /*
|
---|
393 | * Ok, it was freed just fine. Now, if it's a mapping we'll have to remove it from the parent.
|
---|
394 | */
|
---|
395 | if (rtR0MemObjIsMapping(pMem))
|
---|
396 | {
|
---|
397 | PRTR0MEMOBJINTERNAL pParent = pMem->uRel.Child.pParent;
|
---|
398 | uint32_t i;
|
---|
399 |
|
---|
400 | /* sanity checks */
|
---|
401 | AssertPtr(pParent);
|
---|
402 | AssertFatal(pParent->u32Magic == RTR0MEMOBJ_MAGIC);
|
---|
403 | AssertFatal(pParent->enmType > RTR0MEMOBJTYPE_INVALID && pParent->enmType < RTR0MEMOBJTYPE_END);
|
---|
404 | AssertFatal(!rtR0MemObjIsMapping(pParent));
|
---|
405 | AssertFatal(pParent->uRel.Parent.cMappings > 0);
|
---|
406 | AssertPtr(pParent->uRel.Parent.papMappings);
|
---|
407 |
|
---|
408 | /* locate and remove from the array of mappings. */
|
---|
409 | i = pParent->uRel.Parent.cMappings;
|
---|
410 | while (i-- > 0)
|
---|
411 | {
|
---|
412 | if (pParent->uRel.Parent.papMappings[i] == pMem)
|
---|
413 | {
|
---|
414 | pParent->uRel.Parent.papMappings[i] = pParent->uRel.Parent.papMappings[--pParent->uRel.Parent.cMappings];
|
---|
415 | break;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | Assert(i != UINT32_MAX);
|
---|
419 | }
|
---|
420 | else
|
---|
421 | Assert(pMem->uRel.Parent.cMappings == 0);
|
---|
422 |
|
---|
423 | /*
|
---|
424 | * Finally, destroy the handle.
|
---|
425 | */
|
---|
426 | pMem->u32Magic++;
|
---|
427 | pMem->enmType = RTR0MEMOBJTYPE_END;
|
---|
428 | if (!rtR0MemObjIsMapping(pMem))
|
---|
429 | RTMemFree(pMem->uRel.Parent.papMappings);
|
---|
430 | RTMemFree(pMem);
|
---|
431 | }
|
---|
432 | else
|
---|
433 | Log(("RTR0MemObjFree: failed to free %p: %d %p %#zx; rc=%Rrc\n",
|
---|
434 | pMem, pMem->enmType, pMem->pv, pMem->cb, rc));
|
---|
435 | return rc;
|
---|
436 | }
|
---|
437 | RT_EXPORT_SYMBOL(RTR0MemObjFree);
|
---|
438 |
|
---|
439 |
|
---|
440 |
|
---|
441 | RTR0DECL(int) RTR0MemObjAllocPageTag(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable, const char *pszTag)
|
---|
442 | {
|
---|
443 | /* sanity checks. */
|
---|
444 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
445 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
446 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
447 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
448 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
449 | RT_ASSERT_PREEMPTIBLE();
|
---|
450 |
|
---|
451 | /* do the allocation. */
|
---|
452 | return rtR0MemObjNativeAllocPage(pMemObj, cbAligned, fExecutable, pszTag);
|
---|
453 | }
|
---|
454 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPageTag);
|
---|
455 |
|
---|
456 |
|
---|
457 | RTR0DECL(int) RTR0MemObjAllocLargeTag(PRTR0MEMOBJ pMemObj, size_t cb, size_t cbLargePage, uint32_t fFlags, const char *pszTag)
|
---|
458 | {
|
---|
459 | /* sanity checks. */
|
---|
460 | const size_t cbAligned = RT_ALIGN_Z(cb, cbLargePage);
|
---|
461 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
462 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
463 | #ifdef RT_ARCH_AMD64
|
---|
464 | AssertReturn(cbLargePage == _2M || cbLargePage == _1G, VERR_OUT_OF_RANGE);
|
---|
465 | #elif defined(RT_ARCH_X86)
|
---|
466 | AssertReturn(cbLargePage == _2M || cbLargePage == _4M, VERR_OUT_OF_RANGE);
|
---|
467 | #else
|
---|
468 | AssertReturn(RT_IS_POWER_OF_TWO(cbLargePage), VERR_NOT_POWER_OF_TWO);
|
---|
469 | AssertReturn(cbLargePage > PAGE_SIZE, VERR_OUT_OF_RANGE);
|
---|
470 | #endif
|
---|
471 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
472 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
473 | AssertReturn(!(fFlags & ~RTMEMOBJ_ALLOC_LARGE_F_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
474 | RT_ASSERT_PREEMPTIBLE();
|
---|
475 |
|
---|
476 | /* do the allocation. */
|
---|
477 | return rtR0MemObjNativeAllocLarge(pMemObj, cbAligned, cbLargePage, fFlags, pszTag);
|
---|
478 | }
|
---|
479 | RT_EXPORT_SYMBOL(RTR0MemObjAllocLargeTag);
|
---|
480 |
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Fallback implementation of rtR0MemObjNativeAllocLarge and implements single
|
---|
484 | * page allocation using rtR0MemObjNativeAllocPhys.
|
---|
485 | */
|
---|
486 | DECLHIDDEN(int) rtR0MemObjFallbackAllocLarge(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, size_t cbLargePage, uint32_t fFlags,
|
---|
487 | const char *pszTag)
|
---|
488 | {
|
---|
489 | RT_NOREF(pszTag, fFlags);
|
---|
490 | if (cb == cbLargePage)
|
---|
491 | return rtR0MemObjNativeAllocPhys(ppMem, cb, NIL_RTHCPHYS, cbLargePage, pszTag);
|
---|
492 | return VERR_NOT_SUPPORTED;
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | RTR0DECL(int) RTR0MemObjAllocLowTag(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable, const char *pszTag)
|
---|
497 | {
|
---|
498 | /* sanity checks. */
|
---|
499 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
500 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
501 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
502 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
503 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
504 | RT_ASSERT_PREEMPTIBLE();
|
---|
505 |
|
---|
506 | /* do the allocation. */
|
---|
507 | return rtR0MemObjNativeAllocLow(pMemObj, cbAligned, fExecutable, pszTag);
|
---|
508 | }
|
---|
509 | RT_EXPORT_SYMBOL(RTR0MemObjAllocLowTag);
|
---|
510 |
|
---|
511 |
|
---|
512 | RTR0DECL(int) RTR0MemObjAllocContTag(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest, bool fExecutable, const char *pszTag)
|
---|
513 | {
|
---|
514 | /* sanity checks. */
|
---|
515 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
516 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
517 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
518 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
519 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
520 | AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
|
---|
521 | RT_ASSERT_PREEMPTIBLE();
|
---|
522 |
|
---|
523 | /* do the allocation. */
|
---|
524 | return rtR0MemObjNativeAllocCont(pMemObj, cbAligned, PhysHighest, fExecutable, pszTag);
|
---|
525 | }
|
---|
526 | RT_EXPORT_SYMBOL(RTR0MemObjAllocContTag);
|
---|
527 |
|
---|
528 |
|
---|
529 | RTR0DECL(int) RTR0MemObjLockUserTag(PRTR0MEMOBJ pMemObj, RTR3PTR R3Ptr, size_t cb,
|
---|
530 | uint32_t fAccess, RTR0PROCESS R0Process, const char *pszTag)
|
---|
531 | {
|
---|
532 | /* sanity checks. */
|
---|
533 | const size_t cbAligned = RT_ALIGN_Z(cb + (R3Ptr & PAGE_OFFSET_MASK), PAGE_SIZE);
|
---|
534 | RTR3PTR const R3PtrAligned = (R3Ptr & ~(RTR3PTR)PAGE_OFFSET_MASK);
|
---|
535 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
536 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
537 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
538 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
539 | if (R0Process == NIL_RTR0PROCESS)
|
---|
540 | R0Process = RTR0ProcHandleSelf();
|
---|
541 | AssertReturn(!(fAccess & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE)), VERR_INVALID_PARAMETER);
|
---|
542 | AssertReturn(fAccess, VERR_INVALID_PARAMETER);
|
---|
543 | RT_ASSERT_PREEMPTIBLE();
|
---|
544 |
|
---|
545 | /* do the locking. */
|
---|
546 | return rtR0MemObjNativeLockUser(pMemObj, R3PtrAligned, cbAligned, fAccess, R0Process, pszTag);
|
---|
547 | }
|
---|
548 | RT_EXPORT_SYMBOL(RTR0MemObjLockUserTag);
|
---|
549 |
|
---|
550 |
|
---|
551 | RTR0DECL(int) RTR0MemObjLockKernelTag(PRTR0MEMOBJ pMemObj, void *pv, size_t cb, uint32_t fAccess, const char *pszTag)
|
---|
552 | {
|
---|
553 | /* sanity checks. */
|
---|
554 | const size_t cbAligned = RT_ALIGN_Z(cb + ((uintptr_t)pv & PAGE_OFFSET_MASK), PAGE_SIZE);
|
---|
555 | void * const pvAligned = (void *)((uintptr_t)pv & ~(uintptr_t)PAGE_OFFSET_MASK);
|
---|
556 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
557 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
558 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
559 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
560 | AssertPtrReturn(pvAligned, VERR_INVALID_POINTER);
|
---|
561 | AssertReturn(!(fAccess & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE)), VERR_INVALID_PARAMETER);
|
---|
562 | AssertReturn(fAccess, VERR_INVALID_PARAMETER);
|
---|
563 | RT_ASSERT_PREEMPTIBLE();
|
---|
564 |
|
---|
565 | /* do the allocation. */
|
---|
566 | return rtR0MemObjNativeLockKernel(pMemObj, pvAligned, cbAligned, fAccess, pszTag);
|
---|
567 | }
|
---|
568 | RT_EXPORT_SYMBOL(RTR0MemObjLockKernelTag);
|
---|
569 |
|
---|
570 |
|
---|
571 | RTR0DECL(int) RTR0MemObjAllocPhysTag(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest, const char *pszTag)
|
---|
572 | {
|
---|
573 | /* sanity checks. */
|
---|
574 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
575 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
576 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
577 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
578 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
579 | AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
|
---|
580 | RT_ASSERT_PREEMPTIBLE();
|
---|
581 |
|
---|
582 | /* do the allocation. */
|
---|
583 | return rtR0MemObjNativeAllocPhys(pMemObj, cbAligned, PhysHighest, PAGE_SIZE /* page aligned */, pszTag);
|
---|
584 | }
|
---|
585 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPhysTag);
|
---|
586 |
|
---|
587 |
|
---|
588 | RTR0DECL(int) RTR0MemObjAllocPhysExTag(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment, const char *pszTag)
|
---|
589 | {
|
---|
590 | /* sanity checks. */
|
---|
591 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
592 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
593 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
594 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
595 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
596 | AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
|
---|
597 | if (uAlignment == 0)
|
---|
598 | uAlignment = PAGE_SIZE;
|
---|
599 | AssertReturn( uAlignment == PAGE_SIZE
|
---|
600 | || uAlignment == _2M
|
---|
601 | || uAlignment == _4M
|
---|
602 | || uAlignment == _1G,
|
---|
603 | VERR_INVALID_PARAMETER);
|
---|
604 | #if HC_ARCH_BITS == 32
|
---|
605 | /* Memory allocated in this way is typically mapped into kernel space as well; simply
|
---|
606 | don't allow this on 32 bits hosts as the kernel space is too crowded already. */
|
---|
607 | if (uAlignment != PAGE_SIZE)
|
---|
608 | return VERR_NOT_SUPPORTED;
|
---|
609 | #endif
|
---|
610 | RT_ASSERT_PREEMPTIBLE();
|
---|
611 |
|
---|
612 | /* do the allocation. */
|
---|
613 | return rtR0MemObjNativeAllocPhys(pMemObj, cbAligned, PhysHighest, uAlignment, pszTag);
|
---|
614 | }
|
---|
615 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPhysExTag);
|
---|
616 |
|
---|
617 |
|
---|
618 | RTR0DECL(int) RTR0MemObjAllocPhysNCTag(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest, const char *pszTag)
|
---|
619 | {
|
---|
620 | /* sanity checks. */
|
---|
621 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
622 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
623 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
624 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
625 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
626 | AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
|
---|
627 | RT_ASSERT_PREEMPTIBLE();
|
---|
628 |
|
---|
629 | /* do the allocation. */
|
---|
630 | return rtR0MemObjNativeAllocPhysNC(pMemObj, cbAligned, PhysHighest, pszTag);
|
---|
631 | }
|
---|
632 | RT_EXPORT_SYMBOL(RTR0MemObjAllocPhysNCTag);
|
---|
633 |
|
---|
634 |
|
---|
635 | RTR0DECL(int) RTR0MemObjEnterPhysTag(PRTR0MEMOBJ pMemObj, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy, const char *pszTag)
|
---|
636 | {
|
---|
637 | /* sanity checks. */
|
---|
638 | const size_t cbAligned = RT_ALIGN_Z(cb + (Phys & PAGE_OFFSET_MASK), PAGE_SIZE);
|
---|
639 | const RTHCPHYS PhysAligned = Phys & ~(RTHCPHYS)PAGE_OFFSET_MASK;
|
---|
640 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
641 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
642 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
643 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
644 | AssertReturn(Phys != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
|
---|
645 | AssertReturn( uCachePolicy == RTMEM_CACHE_POLICY_DONT_CARE
|
---|
646 | || uCachePolicy == RTMEM_CACHE_POLICY_MMIO,
|
---|
647 | VERR_INVALID_PARAMETER);
|
---|
648 | RT_ASSERT_PREEMPTIBLE();
|
---|
649 |
|
---|
650 | /* do the allocation. */
|
---|
651 | return rtR0MemObjNativeEnterPhys(pMemObj, PhysAligned, cbAligned, uCachePolicy, pszTag);
|
---|
652 | }
|
---|
653 | RT_EXPORT_SYMBOL(RTR0MemObjEnterPhysTag);
|
---|
654 |
|
---|
655 |
|
---|
656 | RTR0DECL(int) RTR0MemObjReserveKernelTag(PRTR0MEMOBJ pMemObj, void *pvFixed, size_t cb, size_t uAlignment, const char *pszTag)
|
---|
657 | {
|
---|
658 | /* sanity checks. */
|
---|
659 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
660 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
661 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
662 | if (uAlignment == 0)
|
---|
663 | uAlignment = PAGE_SIZE;
|
---|
664 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
665 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
666 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
667 | if (pvFixed != (void *)-1)
|
---|
668 | AssertReturn(!((uintptr_t)pvFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
669 | RT_ASSERT_PREEMPTIBLE();
|
---|
670 |
|
---|
671 | /* do the reservation. */
|
---|
672 | return rtR0MemObjNativeReserveKernel(pMemObj, pvFixed, cbAligned, uAlignment, pszTag);
|
---|
673 | }
|
---|
674 | RT_EXPORT_SYMBOL(RTR0MemObjReserveKernelTag);
|
---|
675 |
|
---|
676 |
|
---|
677 | RTR0DECL(int) RTR0MemObjReserveUserTag(PRTR0MEMOBJ pMemObj, RTR3PTR R3PtrFixed, size_t cb,
|
---|
678 | size_t uAlignment, RTR0PROCESS R0Process, const char *pszTag)
|
---|
679 | {
|
---|
680 | /* sanity checks. */
|
---|
681 | const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
|
---|
682 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
683 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
684 | if (uAlignment == 0)
|
---|
685 | uAlignment = PAGE_SIZE;
|
---|
686 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
687 | AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
|
---|
688 | AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
|
---|
689 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
690 | AssertReturn(!(R3PtrFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
691 | if (R0Process == NIL_RTR0PROCESS)
|
---|
692 | R0Process = RTR0ProcHandleSelf();
|
---|
693 | RT_ASSERT_PREEMPTIBLE();
|
---|
694 |
|
---|
695 | /* do the reservation. */
|
---|
696 | return rtR0MemObjNativeReserveUser(pMemObj, R3PtrFixed, cbAligned, uAlignment, R0Process, pszTag);
|
---|
697 | }
|
---|
698 | RT_EXPORT_SYMBOL(RTR0MemObjReserveUserTag);
|
---|
699 |
|
---|
700 |
|
---|
701 | RTR0DECL(int) RTR0MemObjMapKernelTag(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed,
|
---|
702 | size_t uAlignment, unsigned fProt, const char *pszTag)
|
---|
703 | {
|
---|
704 | return RTR0MemObjMapKernelExTag(pMemObj, MemObjToMap, pvFixed, uAlignment, fProt, 0, 0, pszTag);
|
---|
705 | }
|
---|
706 | RT_EXPORT_SYMBOL(RTR0MemObjMapKernelTag);
|
---|
707 |
|
---|
708 |
|
---|
709 | RTR0DECL(int) RTR0MemObjMapKernelExTag(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment,
|
---|
710 | unsigned fProt, size_t offSub, size_t cbSub, const char *pszTag)
|
---|
711 | {
|
---|
712 | PRTR0MEMOBJINTERNAL pMemToMap;
|
---|
713 | PRTR0MEMOBJINTERNAL pNew;
|
---|
714 | int rc;
|
---|
715 |
|
---|
716 | /* sanity checks. */
|
---|
717 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
718 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
719 | AssertPtrReturn(MemObjToMap, VERR_INVALID_HANDLE);
|
---|
720 | pMemToMap = (PRTR0MEMOBJINTERNAL)MemObjToMap;
|
---|
721 | AssertReturn(pMemToMap->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
722 | AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
723 | AssertReturn(!rtR0MemObjIsMapping(pMemToMap), VERR_INVALID_PARAMETER);
|
---|
724 | AssertReturn(pMemToMap->enmType != RTR0MEMOBJTYPE_RES_VIRT, VERR_INVALID_PARAMETER);
|
---|
725 | if (uAlignment == 0)
|
---|
726 | uAlignment = PAGE_SIZE;
|
---|
727 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
728 | if (pvFixed != (void *)-1)
|
---|
729 | AssertReturn(!((uintptr_t)pvFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
730 | AssertReturn(fProt != RTMEM_PROT_NONE, VERR_INVALID_PARAMETER);
|
---|
731 | AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
|
---|
732 | AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
733 | AssertReturn(offSub < pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
734 | AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
735 | AssertReturn(cbSub <= pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
736 | AssertReturn((!offSub && !cbSub) || (offSub + cbSub) <= pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
737 | RT_ASSERT_PREEMPTIBLE();
|
---|
738 |
|
---|
739 | /* adjust the request to simplify the native code. */
|
---|
740 | if (offSub == 0 && cbSub == pMemToMap->cb)
|
---|
741 | cbSub = 0;
|
---|
742 |
|
---|
743 | /* do the mapping. */
|
---|
744 | rc = rtR0MemObjNativeMapKernel(&pNew, pMemToMap, pvFixed, uAlignment, fProt, offSub, cbSub, pszTag);
|
---|
745 | if (RT_SUCCESS(rc))
|
---|
746 | {
|
---|
747 | /* link it. */
|
---|
748 | rc = rtR0MemObjLink(pMemToMap, pNew);
|
---|
749 | if (RT_SUCCESS(rc))
|
---|
750 | *pMemObj = pNew;
|
---|
751 | else
|
---|
752 | {
|
---|
753 | /* damn, out of memory. bail out. */
|
---|
754 | int rc2 = rtR0MemObjNativeFree(pNew);
|
---|
755 | AssertRC(rc2);
|
---|
756 | pNew->u32Magic++;
|
---|
757 | pNew->enmType = RTR0MEMOBJTYPE_END;
|
---|
758 | RTMemFree(pNew);
|
---|
759 | }
|
---|
760 | }
|
---|
761 |
|
---|
762 | return rc;
|
---|
763 | }
|
---|
764 | RT_EXPORT_SYMBOL(RTR0MemObjMapKernelExTag);
|
---|
765 |
|
---|
766 |
|
---|
767 | RTR0DECL(int) RTR0MemObjMapUserTag(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed,
|
---|
768 | size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process, const char *pszTag)
|
---|
769 | {
|
---|
770 | return RTR0MemObjMapUserExTag(pMemObj, MemObjToMap, R3PtrFixed, uAlignment, fProt, R0Process, 0, 0, pszTag);
|
---|
771 | }
|
---|
772 | RT_EXPORT_SYMBOL(RTR0MemObjMapUserTag);
|
---|
773 |
|
---|
774 |
|
---|
775 | RTR0DECL(int) RTR0MemObjMapUserExTag(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment,
|
---|
776 | unsigned fProt, RTR0PROCESS R0Process, size_t offSub, size_t cbSub, const char *pszTag)
|
---|
777 | {
|
---|
778 | /* sanity checks. */
|
---|
779 | PRTR0MEMOBJINTERNAL pMemToMap;
|
---|
780 | PRTR0MEMOBJINTERNAL pNew;
|
---|
781 | int rc;
|
---|
782 | AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
|
---|
783 | pMemToMap = (PRTR0MEMOBJINTERNAL)MemObjToMap;
|
---|
784 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
785 | AssertPtrReturn(MemObjToMap, VERR_INVALID_HANDLE);
|
---|
786 | AssertReturn(pMemToMap->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
787 | AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
788 | AssertReturn(!rtR0MemObjIsMapping(pMemToMap), VERR_INVALID_PARAMETER);
|
---|
789 | AssertReturn(pMemToMap->enmType != RTR0MEMOBJTYPE_RES_VIRT, VERR_INVALID_PARAMETER);
|
---|
790 | if (uAlignment == 0)
|
---|
791 | uAlignment = PAGE_SIZE;
|
---|
792 | AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
|
---|
793 | if (R3PtrFixed != (RTR3PTR)-1)
|
---|
794 | AssertReturn(!(R3PtrFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
|
---|
795 | AssertReturn(fProt != RTMEM_PROT_NONE, VERR_INVALID_PARAMETER);
|
---|
796 | AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
|
---|
797 | AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
798 | AssertReturn(offSub < pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
799 | AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
800 | AssertReturn(cbSub <= pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
801 | AssertReturn((!offSub && !cbSub) || (offSub + cbSub) <= pMemToMap->cb, VERR_INVALID_PARAMETER);
|
---|
802 | if (R0Process == NIL_RTR0PROCESS)
|
---|
803 | R0Process = RTR0ProcHandleSelf();
|
---|
804 | RT_ASSERT_PREEMPTIBLE();
|
---|
805 |
|
---|
806 | /* adjust the request to simplify the native code. */
|
---|
807 | if (offSub == 0 && cbSub == pMemToMap->cb)
|
---|
808 | cbSub = 0;
|
---|
809 |
|
---|
810 | /* do the mapping. */
|
---|
811 | rc = rtR0MemObjNativeMapUser(&pNew, pMemToMap, R3PtrFixed, uAlignment, fProt, R0Process, offSub, cbSub, pszTag);
|
---|
812 | if (RT_SUCCESS(rc))
|
---|
813 | {
|
---|
814 | /* link it. */
|
---|
815 | rc = rtR0MemObjLink(pMemToMap, pNew);
|
---|
816 | if (RT_SUCCESS(rc))
|
---|
817 | *pMemObj = pNew;
|
---|
818 | else
|
---|
819 | {
|
---|
820 | /* damn, out of memory. bail out. */
|
---|
821 | int rc2 = rtR0MemObjNativeFree(pNew);
|
---|
822 | AssertRC(rc2);
|
---|
823 | pNew->u32Magic++;
|
---|
824 | pNew->enmType = RTR0MEMOBJTYPE_END;
|
---|
825 | RTMemFree(pNew);
|
---|
826 | }
|
---|
827 | }
|
---|
828 |
|
---|
829 | return rc;
|
---|
830 | }
|
---|
831 | RT_EXPORT_SYMBOL(RTR0MemObjMapUserExTag);
|
---|
832 |
|
---|
833 |
|
---|
834 | RTR0DECL(int) RTR0MemObjProtect(RTR0MEMOBJ hMemObj, size_t offSub, size_t cbSub, uint32_t fProt)
|
---|
835 | {
|
---|
836 | PRTR0MEMOBJINTERNAL pMemObj;
|
---|
837 | int rc;
|
---|
838 |
|
---|
839 | /* sanity checks. */
|
---|
840 | pMemObj = (PRTR0MEMOBJINTERNAL)hMemObj;
|
---|
841 | AssertPtrReturn(pMemObj, VERR_INVALID_HANDLE);
|
---|
842 | AssertReturn(pMemObj->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
|
---|
843 | AssertReturn(pMemObj->enmType > RTR0MEMOBJTYPE_INVALID && pMemObj->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
|
---|
844 | AssertReturn(rtR0MemObjIsProtectable(pMemObj), VERR_INVALID_PARAMETER);
|
---|
845 | AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
846 | AssertReturn(offSub < pMemObj->cb, VERR_INVALID_PARAMETER);
|
---|
847 | AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
|
---|
848 | AssertReturn(cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
|
---|
849 | AssertReturn(offSub + cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
|
---|
850 | AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
|
---|
851 | RT_ASSERT_PREEMPTIBLE();
|
---|
852 |
|
---|
853 | /* do the job */
|
---|
854 | rc = rtR0MemObjNativeProtect(pMemObj, offSub, cbSub, fProt);
|
---|
855 | if (RT_SUCCESS(rc))
|
---|
856 | pMemObj->fFlags |= RTR0MEMOBJ_FLAGS_PROT_CHANGED; /* record it */
|
---|
857 |
|
---|
858 | return rc;
|
---|
859 | }
|
---|
860 | RT_EXPORT_SYMBOL(RTR0MemObjProtect);
|
---|
861 |
|
---|