VirtualBox

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

Last change on this file since 91482 was 91482, checked in by vboxsync, 3 years ago

IPRT/memobj: Passing pszTag around...

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