VirtualBox

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

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

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

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