VirtualBox

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

Last change on this file since 41169 was 40938, checked in by vboxsync, 13 years ago

runtime: backed out r77481,r77482,r77483,r77484,r77485

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