VirtualBox

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

Last change on this file since 25046 was 23610, checked in by vboxsync, 15 years ago

IPRT,VMM,SUPDrv,VBGLR0: Added a parameter to RTR0MemObjLockUser/Kernel that indicates read/write intent so we can correctly lock readonly memory on Windows and OS/2. (Guest property strings, see #4238.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev
File size: 34.9 KB
Line 
1/* $Revision: 23610 $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects, Common Code.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_DEFAULT ///@todo RTLOGGROUP_MEM
36#include <iprt/memobj.h>
37#include "internal/iprt.h"
38
39#include <iprt/alloc.h>
40#include <iprt/asm.h>
41#include <iprt/assert.h>
42#include <iprt/err.h>
43#include <iprt/log.h>
44#include <iprt/mp.h>
45#include <iprt/param.h>
46#include <iprt/process.h>
47#include <iprt/thread.h>
48
49#include "internal/memobj.h"
50
51
52/**
53 * Internal function for allocating a new memory object.
54 *
55 * @returns The allocated and initialized handle.
56 * @param cbSelf The size of the memory object handle. 0 mean default size.
57 * @param enmType The memory object type.
58 * @param pv The memory object mapping.
59 * @param cb The size of the memory object.
60 */
61PRTR0MEMOBJINTERNAL rtR0MemObjNew(size_t cbSelf, RTR0MEMOBJTYPE enmType, void *pv, size_t cb)
62{
63 PRTR0MEMOBJINTERNAL pNew;
64
65 /* validate the size */
66 if (!cbSelf)
67 cbSelf = sizeof(*pNew);
68 Assert(cbSelf >= sizeof(*pNew));
69 Assert(cbSelf == (uint32_t)cbSelf);
70
71 /*
72 * Allocate and initialize the object.
73 */
74 pNew = (PRTR0MEMOBJINTERNAL)RTMemAllocZ(cbSelf);
75 if (pNew)
76 {
77 pNew->u32Magic = RTR0MEMOBJ_MAGIC;
78 pNew->cbSelf = (uint32_t)cbSelf;
79 pNew->enmType = enmType;
80 pNew->fFlags = 0;
81 pNew->cb = cb;
82 pNew->pv = pv;
83 }
84 return pNew;
85}
86
87
88/**
89 * Deletes an incomplete memory object.
90 *
91 * This is for cleaning up after failures during object creation.
92 *
93 * @param pMem The incomplete memory object to delete.
94 */
95void rtR0MemObjDelete(PRTR0MEMOBJINTERNAL pMem)
96{
97 if (pMem)
98 {
99 ASMAtomicUoWriteU32(&pMem->u32Magic, ~RTR0MEMOBJ_MAGIC);
100 pMem->enmType = RTR0MEMOBJTYPE_END;
101 RTMemFree(pMem);
102 }
103}
104
105
106/**
107 * Links a mapping object to a primary object.
108 *
109 * @returns IPRT status code.
110 * @retval VINF_SUCCESS on success.
111 * @retval VINF_NO_MEMORY if we couldn't expand the mapping array of the parent.
112 * @param pParent The parent (primary) memory object.
113 * @param pChild The child (mapping) memory object.
114 */
115static int rtR0MemObjLink(PRTR0MEMOBJINTERNAL pParent, PRTR0MEMOBJINTERNAL pChild)
116{
117 uint32_t i;
118
119 /* sanity */
120 Assert(rtR0MemObjIsMapping(pChild));
121 Assert(!rtR0MemObjIsMapping(pParent));
122
123 /* expand the array? */
124 i = pParent->uRel.Parent.cMappings;
125 if (i >= pParent->uRel.Parent.cMappingsAllocated)
126 {
127 void *pv = RTMemRealloc(pParent->uRel.Parent.papMappings,
128 (i + 32) * sizeof(pParent->uRel.Parent.papMappings[0]));
129 if (!pv)
130 return VERR_NO_MEMORY;
131 pParent->uRel.Parent.papMappings = (PPRTR0MEMOBJINTERNAL)pv;
132 pParent->uRel.Parent.cMappingsAllocated = i + 32;
133 Assert(i == pParent->uRel.Parent.cMappings);
134 }
135
136 /* do the linking. */
137 pParent->uRel.Parent.papMappings[i] = pChild;
138 pParent->uRel.Parent.cMappings++;
139 pChild->uRel.Child.pParent = pParent;
140
141 return VINF_SUCCESS;
142}
143
144
145/**
146 * Checks if this is mapping or not.
147 *
148 * @returns true if it's a mapping, otherwise false.
149 * @param MemObj The ring-0 memory object handle.
150 */
151RTR0DECL(bool) RTR0MemObjIsMapping(RTR0MEMOBJ MemObj)
152{
153 /* Validate the object handle. */
154 PRTR0MEMOBJINTERNAL pMem;
155 AssertPtrReturn(MemObj, false);
156 pMem = (PRTR0MEMOBJINTERNAL)MemObj;
157 AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), false);
158 AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), false);
159
160 /* hand it on to the inlined worker. */
161 return rtR0MemObjIsMapping(pMem);
162}
163RT_EXPORT_SYMBOL(RTR0MemObjIsMapping);
164
165
166/**
167 * Gets the address of a ring-0 memory object.
168 *
169 * @returns The address of the memory object.
170 * @returns NULL if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
171 * @param MemObj The ring-0 memory object handle.
172 */
173RTR0DECL(void *) RTR0MemObjAddress(RTR0MEMOBJ MemObj)
174{
175 /* Validate the object handle. */
176 PRTR0MEMOBJINTERNAL pMem;
177 if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
178 return NULL;
179 AssertPtrReturn(MemObj, NULL);
180 pMem = (PRTR0MEMOBJINTERNAL)MemObj;
181 AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NULL);
182 AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NULL);
183
184 /* return the mapping address. */
185 return pMem->pv;
186}
187RT_EXPORT_SYMBOL(RTR0MemObjAddress);
188
189
190/**
191 * Gets the ring-3 address of a ring-0 memory object.
192 *
193 * This only applies to ring-0 memory object with ring-3 mappings of some kind, i.e.
194 * locked user memory, reserved user address space and user mappings. This API should
195 * not be used on any other objects.
196 *
197 * @returns The address of the memory object.
198 * @returns NIL_RTR3PTR if the handle is invalid or if it's not an object with a ring-3 mapping.
199 * Strict builds will assert in both cases.
200 * @param MemObj The ring-0 memory object handle.
201 */
202RTR0DECL(RTR3PTR) RTR0MemObjAddressR3(RTR0MEMOBJ MemObj)
203{
204 PRTR0MEMOBJINTERNAL pMem;
205
206 /* Validate the object handle. */
207 if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
208 return NIL_RTR3PTR;
209 AssertPtrReturn(MemObj, NIL_RTR3PTR);
210 pMem = (PRTR0MEMOBJINTERNAL)MemObj;
211 AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NIL_RTR3PTR);
212 AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NIL_RTR3PTR);
213 if (RT_UNLIKELY( ( pMem->enmType != RTR0MEMOBJTYPE_MAPPING
214 || pMem->u.Mapping.R0Process == NIL_RTR0PROCESS)
215 && ( pMem->enmType != RTR0MEMOBJTYPE_LOCK
216 || pMem->u.Lock.R0Process == NIL_RTR0PROCESS)
217 && ( pMem->enmType != RTR0MEMOBJTYPE_PHYS_NC
218 || pMem->u.Lock.R0Process == NIL_RTR0PROCESS)
219 && ( pMem->enmType != RTR0MEMOBJTYPE_RES_VIRT
220 || pMem->u.ResVirt.R0Process == NIL_RTR0PROCESS)))
221 return NIL_RTR3PTR;
222
223 /* return the mapping address. */
224 return (RTR3PTR)pMem->pv;
225}
226RT_EXPORT_SYMBOL(RTR0MemObjAddressR3);
227
228
229/**
230 * Gets the size of a ring-0 memory object.
231 *
232 * @returns The address of the memory object.
233 * @returns 0 if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
234 * @param MemObj The ring-0 memory object handle.
235 */
236RTR0DECL(size_t) RTR0MemObjSize(RTR0MEMOBJ MemObj)
237{
238 PRTR0MEMOBJINTERNAL pMem;
239
240 /* Validate the object handle. */
241 if (RT_UNLIKELY(MemObj == NIL_RTR0MEMOBJ))
242 return 0;
243 AssertPtrReturn(MemObj, 0);
244 pMem = (PRTR0MEMOBJINTERNAL)MemObj;
245 AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), 0);
246 AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), 0);
247
248 /* return the size. */
249 return pMem->cb;
250}
251RT_EXPORT_SYMBOL(RTR0MemObjSize);
252
253
254/**
255 * Get the physical address of an page in the memory object.
256 *
257 * @returns The physical address.
258 * @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
259 * @returns NIL_RTHCPHYS if the iPage is out of range.
260 * @returns NIL_RTHCPHYS if the object handle isn't valid.
261 * @param MemObj The ring-0 memory object handle.
262 * @param iPage The page number within the object.
263 */
264RTR0DECL(RTHCPHYS) RTR0MemObjGetPagePhysAddr(RTR0MEMOBJ MemObj, size_t iPage)
265{
266 /* Validate the object handle. */
267 PRTR0MEMOBJINTERNAL pMem;
268 size_t cPages;
269 AssertPtrReturn(MemObj, NIL_RTHCPHYS);
270 pMem = (PRTR0MEMOBJINTERNAL)MemObj;
271 AssertReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, NIL_RTHCPHYS);
272 AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, NIL_RTHCPHYS);
273 AssertMsgReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, ("%p: %#x\n", pMem, pMem->u32Magic), NIL_RTHCPHYS);
274 AssertMsgReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, ("%p: %d\n", pMem, pMem->enmType), NIL_RTHCPHYS);
275 cPages = (pMem->cb >> PAGE_SHIFT);
276 if (iPage >= cPages)
277 {
278 /* permit: while (RTR0MemObjGetPagePhysAddr(pMem, iPage++) != NIL_RTHCPHYS) {} */
279 if (iPage == cPages)
280 return NIL_RTHCPHYS;
281 AssertReturn(iPage < (pMem->cb >> PAGE_SHIFT), NIL_RTHCPHYS);
282 }
283
284 /*
285 * We know the address of physically contiguous allocations and mappings.
286 */
287 if (pMem->enmType == RTR0MEMOBJTYPE_CONT)
288 return pMem->u.Cont.Phys + iPage * PAGE_SIZE;
289 if (pMem->enmType == RTR0MEMOBJTYPE_PHYS)
290 return pMem->u.Phys.PhysBase + iPage * PAGE_SIZE;
291
292 /*
293 * Do the job.
294 */
295 return rtR0MemObjNativeGetPagePhysAddr(pMem, iPage);
296}
297RT_EXPORT_SYMBOL(RTR0MemObjGetPagePhysAddr);
298
299
300/**
301 * Frees a ring-0 memory object.
302 *
303 * @returns IPRT status code.
304 * @retval VERR_INVALID_HANDLE if
305 * @param MemObj The ring-0 memory object to be freed. NULL is accepted.
306 * @param fFreeMappings Whether or not to free mappings of the object.
307 */
308RTR0DECL(int) RTR0MemObjFree(RTR0MEMOBJ MemObj, bool fFreeMappings)
309{
310 /*
311 * Validate the object handle.
312 */
313 PRTR0MEMOBJINTERNAL pMem;
314 int rc;
315
316 if (MemObj == NIL_RTR0MEMOBJ)
317 return VINF_SUCCESS;
318 AssertPtrReturn(MemObj, VERR_INVALID_HANDLE);
319 pMem = (PRTR0MEMOBJINTERNAL)MemObj;
320 AssertReturn(pMem->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
321 AssertReturn(pMem->enmType > RTR0MEMOBJTYPE_INVALID && pMem->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
322 RT_ASSERT_PREEMPTIBLE();
323
324 /*
325 * Deal with mapings according to fFreeMappings.
326 */
327 if ( !rtR0MemObjIsMapping(pMem)
328 && pMem->uRel.Parent.cMappings > 0)
329 {
330 /* fail if not requested to free mappings. */
331 if (!fFreeMappings)
332 return VERR_MEMORY_BUSY;
333
334 while (pMem->uRel.Parent.cMappings > 0)
335 {
336 PRTR0MEMOBJINTERNAL pChild = pMem->uRel.Parent.papMappings[--pMem->uRel.Parent.cMappings];
337 pMem->uRel.Parent.papMappings[pMem->uRel.Parent.cMappings] = NULL;
338
339 /* sanity checks. */
340 AssertPtr(pChild);
341 AssertFatal(pChild->u32Magic == RTR0MEMOBJ_MAGIC);
342 AssertFatal(pChild->enmType > RTR0MEMOBJTYPE_INVALID && pChild->enmType < RTR0MEMOBJTYPE_END);
343 AssertFatal(rtR0MemObjIsMapping(pChild));
344
345 /* free the mapping. */
346 rc = rtR0MemObjNativeFree(pChild);
347 if (RT_FAILURE(rc))
348 {
349 Log(("RTR0MemObjFree: failed to free mapping %p: %p %#zx; rc=%Rrc\n", pChild, pChild->pv, pChild->cb, rc));
350 pMem->uRel.Parent.papMappings[pMem->uRel.Parent.cMappings++] = pChild;
351 return rc;
352 }
353 }
354 }
355
356 /*
357 * Free this object.
358 */
359 rc = rtR0MemObjNativeFree(pMem);
360 if (RT_SUCCESS(rc))
361 {
362 /*
363 * Ok, it was freed just fine. Now, if it's a mapping we'll have to remove it from the parent.
364 */
365 if (rtR0MemObjIsMapping(pMem))
366 {
367 PRTR0MEMOBJINTERNAL pParent = pMem->uRel.Child.pParent;
368 uint32_t i;
369
370 /* sanity checks */
371 AssertPtr(pParent);
372 AssertFatal(pParent->u32Magic == RTR0MEMOBJ_MAGIC);
373 AssertFatal(pParent->enmType > RTR0MEMOBJTYPE_INVALID && pParent->enmType < RTR0MEMOBJTYPE_END);
374 AssertFatal(!rtR0MemObjIsMapping(pParent));
375 AssertFatal(pParent->uRel.Parent.cMappings > 0);
376 AssertPtr(pParent->uRel.Parent.papMappings);
377
378 /* locate and remove from the array of mappings. */
379 i = pParent->uRel.Parent.cMappings;
380 while (i-- > 0)
381 {
382 if (pParent->uRel.Parent.papMappings[i] == pMem)
383 {
384 pParent->uRel.Parent.papMappings[i] = pParent->uRel.Parent.papMappings[--pParent->uRel.Parent.cMappings];
385 break;
386 }
387 }
388 Assert(i != UINT32_MAX);
389 }
390 else
391 Assert(pMem->uRel.Parent.cMappings == 0);
392
393 /*
394 * Finally, destroy the handle.
395 */
396 pMem->u32Magic++;
397 pMem->enmType = RTR0MEMOBJTYPE_END;
398 if (!rtR0MemObjIsMapping(pMem))
399 RTMemFree(pMem->uRel.Parent.papMappings);
400 RTMemFree(pMem);
401 }
402 else
403 Log(("RTR0MemObjFree: failed to free %p: %d %p %#zx; rc=%Rrc\n",
404 pMem, pMem->enmType, pMem->pv, pMem->cb, rc));
405 return rc;
406}
407RT_EXPORT_SYMBOL(RTR0MemObjFree);
408
409
410
411/**
412 * Allocates page aligned virtual kernel memory.
413 *
414 * The memory is taken from a non paged (= fixed physical memory backing) pool.
415 *
416 * @returns IPRT status code.
417 * @param pMemObj Where to store the ring-0 memory object handle.
418 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
419 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
420 */
421RTR0DECL(int) RTR0MemObjAllocPage(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable)
422{
423 /* sanity checks. */
424 const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
425 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
426 *pMemObj = NIL_RTR0MEMOBJ;
427 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
428 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
429 RT_ASSERT_PREEMPTIBLE();
430
431 /* do the allocation. */
432 return rtR0MemObjNativeAllocPage(pMemObj, cbAligned, fExecutable);
433}
434RT_EXPORT_SYMBOL(RTR0MemObjAllocPage);
435
436
437/**
438 * Allocates page aligned virtual kernel memory with physical backing below 4GB.
439 *
440 * The physical memory backing the allocation is fixed.
441 *
442 * @returns IPRT status code.
443 * @param pMemObj Where to store the ring-0 memory object handle.
444 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
445 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
446 */
447RTR0DECL(int) RTR0MemObjAllocLow(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable)
448{
449 /* sanity checks. */
450 const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
451 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
452 *pMemObj = NIL_RTR0MEMOBJ;
453 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
454 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
455 RT_ASSERT_PREEMPTIBLE();
456
457 /* do the allocation. */
458 return rtR0MemObjNativeAllocLow(pMemObj, cbAligned, fExecutable);
459}
460RT_EXPORT_SYMBOL(RTR0MemObjAllocLow);
461
462
463/**
464 * Allocates page aligned virtual kernel memory with contiguous physical backing below 4GB.
465 *
466 * The physical memory backing the allocation is fixed.
467 *
468 * @returns IPRT status code.
469 * @param pMemObj Where to store the ring-0 memory object handle.
470 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
471 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
472 */
473RTR0DECL(int) RTR0MemObjAllocCont(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable)
474{
475 /* sanity checks. */
476 const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
477 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
478 *pMemObj = NIL_RTR0MEMOBJ;
479 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
480 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
481 RT_ASSERT_PREEMPTIBLE();
482
483 /* do the allocation. */
484 return rtR0MemObjNativeAllocCont(pMemObj, cbAligned, fExecutable);
485}
486RT_EXPORT_SYMBOL(RTR0MemObjAllocCont);
487
488
489/**
490 * Locks a range of user virtual memory.
491 *
492 * @returns IPRT status code.
493 * @param pMemObj Where to store the ring-0 memory object handle.
494 * @param R3Ptr User virtual address. This is rounded down to a page
495 * boundrary.
496 * @param cb Number of bytes to lock. This is rounded up to
497 * nearest page boundrary.
498 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
499 * and RTMEM_PROT_WRITE.
500 * @param R0Process The process to lock pages in. NIL_R0PROCESS is an
501 * alias for the current one.
502 *
503 * @remarks RTR0MemGetAddressR3() and RTR0MemGetAddress() will return therounded
504 * down address.
505 *
506 * @remarks Linux: This API requires that the memory begin locked is in a memory
507 * mapping that is not required in any forked off child process. This
508 * is not intented as permanent restriction, feel free to help out
509 * lifting it.
510 */
511RTR0DECL(int) RTR0MemObjLockUser(PRTR0MEMOBJ pMemObj, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process)
512{
513 /* sanity checks. */
514 const size_t cbAligned = RT_ALIGN_Z(cb + (R3Ptr & PAGE_OFFSET_MASK), PAGE_SIZE);
515 RTR3PTR const R3PtrAligned = (R3Ptr & ~(RTR3PTR)PAGE_OFFSET_MASK);
516 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
517 *pMemObj = NIL_RTR0MEMOBJ;
518 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
519 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
520 if (R0Process == NIL_RTR0PROCESS)
521 R0Process = RTR0ProcHandleSelf();
522 AssertReturn(!(fAccess & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE)), VERR_INVALID_PARAMETER);
523 AssertReturn(fAccess, VERR_INVALID_PARAMETER);
524 RT_ASSERT_PREEMPTIBLE();
525
526 /* do the locking. */
527 return rtR0MemObjNativeLockUser(pMemObj, R3PtrAligned, cbAligned, fAccess, R0Process);
528}
529RT_EXPORT_SYMBOL(RTR0MemObjLockUser);
530
531
532/**
533 * Locks a range of kernel virtual memory.
534 *
535 * @returns IPRT status code.
536 * @param pMemObj Where to store the ring-0 memory object handle.
537 * @param pv Kernel virtual address. This is rounded down to a page boundrary.
538 * @param cb Number of bytes to lock. This is rounded up to nearest page boundrary.
539 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
540 * and RTMEM_PROT_WRITE.
541 *
542 * @remark RTR0MemGetAddress() will return the rounded down address.
543 */
544RTR0DECL(int) RTR0MemObjLockKernel(PRTR0MEMOBJ pMemObj, void *pv, size_t cb, uint32_t fAccess)
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);
560}
561RT_EXPORT_SYMBOL(RTR0MemObjLockKernel);
562
563
564/**
565 * Allocates contiguous page aligned physical memory without (necessarily) any kernel mapping.
566 *
567 * @returns IPRT status code.
568 * @param pMemObj Where to store the ring-0 memory object handle.
569 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
570 * @param PhysHighest The highest permittable address (inclusive).
571 * Pass NIL_RTHCPHYS if any address is acceptable.
572 */
573RTR0DECL(int) RTR0MemObjAllocPhys(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest)
574{
575 /* sanity checks. */
576 const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
577 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
578 *pMemObj = NIL_RTR0MEMOBJ;
579 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
580 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
581 AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
582 RT_ASSERT_PREEMPTIBLE();
583
584 /* do the allocation. */
585 return rtR0MemObjNativeAllocPhys(pMemObj, cbAligned, PhysHighest);
586}
587RT_EXPORT_SYMBOL(RTR0MemObjAllocPhys);
588
589
590/**
591 * Allocates non-contiguous page aligned physical memory without (necessarily) any kernel mapping.
592 *
593 * @returns IPRT status code.
594 * @param pMemObj Where to store the ring-0 memory object handle.
595 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
596 * @param PhysHighest The highest permittable address (inclusive).
597 * Pass NIL_RTHCPHYS if any address is acceptable.
598 */
599RTR0DECL(int) RTR0MemObjAllocPhysNC(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest)
600{
601 /* sanity checks. */
602 const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
603 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
604 *pMemObj = NIL_RTR0MEMOBJ;
605 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
606 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
607 AssertReturn(PhysHighest >= cb, VERR_INVALID_PARAMETER);
608 RT_ASSERT_PREEMPTIBLE();
609
610 /* do the allocation. */
611 return rtR0MemObjNativeAllocPhysNC(pMemObj, cbAligned, PhysHighest);
612}
613RT_EXPORT_SYMBOL(RTR0MemObjAllocPhysNC);
614
615
616/**
617 * Creates a page aligned, contiguous, physical memory object.
618 *
619 * No physical memory is allocated, we trust you do know what you're doing.
620 *
621 * @returns IPRT status code.
622 * @param pMemObj Where to store the ring-0 memory object handle.
623 * @param Phys The physical address to start at. This is rounded down to the
624 * nearest page boundrary.
625 * @param cb The size of the object in bytes. This is rounded up to nearest page boundrary.
626 */
627RTR0DECL(int) RTR0MemObjEnterPhys(PRTR0MEMOBJ pMemObj, RTHCPHYS Phys, size_t cb)
628{
629 /* sanity checks. */
630 const size_t cbAligned = RT_ALIGN_Z(cb + (Phys & PAGE_OFFSET_MASK), PAGE_SIZE);
631 const RTHCPHYS PhysAligned = Phys & ~(RTHCPHYS)PAGE_OFFSET_MASK;
632 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
633 *pMemObj = NIL_RTR0MEMOBJ;
634 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
635 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
636 AssertReturn(Phys != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
637 RT_ASSERT_PREEMPTIBLE();
638
639 /* do the allocation. */
640 return rtR0MemObjNativeEnterPhys(pMemObj, PhysAligned, cbAligned);
641}
642RT_EXPORT_SYMBOL(RTR0MemObjEnterPhys);
643
644
645/**
646 * Reserves kernel virtual address space.
647 *
648 * @returns IPRT status code.
649 * @param pMemObj Where to store the ring-0 memory object handle.
650 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
651 * @param cb The number of bytes to reserve. This is rounded up to nearest page.
652 * @param uAlignment The alignment of the reserved memory.
653 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
654 */
655RTR0DECL(int) RTR0MemObjReserveKernel(PRTR0MEMOBJ pMemObj, void *pvFixed, size_t cb, size_t uAlignment)
656{
657 /* sanity checks. */
658 const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
659 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
660 *pMemObj = NIL_RTR0MEMOBJ;
661 if (uAlignment == 0)
662 uAlignment = PAGE_SIZE;
663 AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
664 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
665 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
666 if (pvFixed != (void *)-1)
667 AssertReturn(!((uintptr_t)pvFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
668 RT_ASSERT_PREEMPTIBLE();
669
670 /* do the reservation. */
671 return rtR0MemObjNativeReserveKernel(pMemObj, pvFixed, cbAligned, uAlignment);
672}
673RT_EXPORT_SYMBOL(RTR0MemObjReserveKernel);
674
675
676/**
677 * Reserves user virtual address space in the current process.
678 *
679 * @returns IPRT status code.
680 * @param pMemObj Where to store the ring-0 memory object handle.
681 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
682 * @param cb The number of bytes to reserve. This is rounded up to nearest PAGE_SIZE.
683 * @param uAlignment The alignment of the reserved memory.
684 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
685 * @param R0Process The process to reserve the memory in. NIL_R0PROCESS is an alias for the current one.
686 */
687RTR0DECL(int) RTR0MemObjReserveUser(PRTR0MEMOBJ pMemObj, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process)
688{
689 /* sanity checks. */
690 const size_t cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
691 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
692 *pMemObj = NIL_RTR0MEMOBJ;
693 if (uAlignment == 0)
694 uAlignment = PAGE_SIZE;
695 AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
696 AssertReturn(cb > 0, VERR_INVALID_PARAMETER);
697 AssertReturn(cb <= cbAligned, VERR_INVALID_PARAMETER);
698 if (R3PtrFixed != (RTR3PTR)-1)
699 AssertReturn(!(R3PtrFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
700 if (R0Process == NIL_RTR0PROCESS)
701 R0Process = RTR0ProcHandleSelf();
702 RT_ASSERT_PREEMPTIBLE();
703
704 /* do the reservation. */
705 return rtR0MemObjNativeReserveUser(pMemObj, R3PtrFixed, cbAligned, uAlignment, R0Process);
706}
707RT_EXPORT_SYMBOL(RTR0MemObjReserveUser);
708
709
710/**
711 * Maps a memory object into kernel virtual address space.
712 *
713 * @returns IPRT status code.
714 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
715 * @param MemObjToMap The object to be map.
716 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
717 * @param uAlignment The alignment of the reserved memory.
718 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
719 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
720 */
721RTR0DECL(int) RTR0MemObjMapKernel(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt)
722{
723 return RTR0MemObjMapKernelEx(pMemObj, MemObjToMap, pvFixed, uAlignment, fProt, 0, 0);
724}
725RT_EXPORT_SYMBOL(RTR0MemObjMapKernel);
726
727
728/**
729 * Maps a memory object into kernel virtual address space.
730 *
731 * The ability to map subsections of the object into kernel space is currently
732 * not implemented on all platforms. All/Most of platforms supports mapping the
733 * whole object into kernel space.
734 *
735 * @returns IPRT status code.
736 * @retval VERR_NOT_SUPPORTED if it's not possible to map a subsection of a
737 * memory object on this platform. When you hit this, try implement it.
738 *
739 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
740 * @param MemObjToMap The object to be map.
741 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
742 * @param uAlignment The alignment of the reserved memory.
743 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
744 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
745 * @param offSub Where in the object to start mapping. If non-zero
746 * the value must be page aligned and cbSub must be
747 * non-zero as well.
748 * @param cbSub The size of the part of the object to be mapped. If
749 * zero the entire object is mapped. The value must be
750 * page aligned.
751 */
752RTR0DECL(int) RTR0MemObjMapKernelEx(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment,
753 unsigned fProt, size_t offSub, size_t cbSub)
754{
755 PRTR0MEMOBJINTERNAL pMemToMap;
756 PRTR0MEMOBJINTERNAL pNew;
757 int rc;
758
759 /* sanity checks. */
760 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
761 *pMemObj = NIL_RTR0MEMOBJ;
762 AssertPtrReturn(MemObjToMap, VERR_INVALID_HANDLE);
763 pMemToMap = (PRTR0MEMOBJINTERNAL)MemObjToMap;
764 AssertReturn(pMemToMap->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
765 AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
766 AssertReturn(!rtR0MemObjIsMapping(pMemToMap), VERR_INVALID_PARAMETER);
767 AssertReturn(pMemToMap->enmType != RTR0MEMOBJTYPE_RES_VIRT, VERR_INVALID_PARAMETER);
768 if (uAlignment == 0)
769 uAlignment = PAGE_SIZE;
770 AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
771 if (pvFixed != (void *)-1)
772 AssertReturn(!((uintptr_t)pvFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
773 AssertReturn(fProt != RTMEM_PROT_NONE, VERR_INVALID_PARAMETER);
774 AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
775 AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
776 AssertReturn(offSub < pMemToMap->cb, VERR_INVALID_PARAMETER);
777 AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
778 AssertReturn(cbSub <= pMemToMap->cb, VERR_INVALID_PARAMETER);
779 AssertReturn((!offSub && !cbSub) || (offSub + cbSub) <= pMemToMap->cb, VERR_INVALID_PARAMETER);
780 RT_ASSERT_PREEMPTIBLE();
781
782 /* adjust the request to simplify the native code. */
783 if (offSub == 0 && cbSub == pMemToMap->cb)
784 cbSub = 0;
785
786 /* do the mapping. */
787 rc = rtR0MemObjNativeMapKernel(&pNew, pMemToMap, pvFixed, uAlignment, fProt, offSub, cbSub);
788 if (RT_SUCCESS(rc))
789 {
790 /* link it. */
791 rc = rtR0MemObjLink(pMemToMap, pNew);
792 if (RT_SUCCESS(rc))
793 *pMemObj = pNew;
794 else
795 {
796 /* damn, out of memory. bail out. */
797 int rc2 = rtR0MemObjNativeFree(pNew);
798 AssertRC(rc2);
799 pNew->u32Magic++;
800 pNew->enmType = RTR0MEMOBJTYPE_END;
801 RTMemFree(pNew);
802 }
803 }
804
805 return rc;
806}
807RT_EXPORT_SYMBOL(RTR0MemObjMapKernelEx);
808
809
810/**
811 * Maps a memory object into user virtual address space in the current process.
812 *
813 * @returns IPRT status code.
814 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
815 * @param MemObjToMap The object to be map.
816 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
817 * @param uAlignment The alignment of the reserved memory.
818 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
819 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
820 * @param R0Process The process to map the memory into. NIL_R0PROCESS is an alias for the current one.
821 */
822RTR0DECL(int) RTR0MemObjMapUser(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process)
823{
824 /* sanity checks. */
825 PRTR0MEMOBJINTERNAL pMemToMap;
826 PRTR0MEMOBJINTERNAL pNew;
827 int rc;
828 AssertPtrReturn(pMemObj, VERR_INVALID_POINTER);
829 pMemToMap = (PRTR0MEMOBJINTERNAL)MemObjToMap;
830 *pMemObj = NIL_RTR0MEMOBJ;
831 AssertPtrReturn(MemObjToMap, VERR_INVALID_HANDLE);
832 AssertReturn(pMemToMap->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
833 AssertReturn(pMemToMap->enmType > RTR0MEMOBJTYPE_INVALID && pMemToMap->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
834 AssertReturn(!rtR0MemObjIsMapping(pMemToMap), VERR_INVALID_PARAMETER);
835 AssertReturn(pMemToMap->enmType != RTR0MEMOBJTYPE_RES_VIRT, VERR_INVALID_PARAMETER);
836 if (uAlignment == 0)
837 uAlignment = PAGE_SIZE;
838 AssertReturn(uAlignment == PAGE_SIZE || uAlignment == _2M || uAlignment == _4M, VERR_INVALID_PARAMETER);
839 if (R3PtrFixed != (RTR3PTR)-1)
840 AssertReturn(!(R3PtrFixed & (uAlignment - 1)), VERR_INVALID_PARAMETER);
841 AssertReturn(fProt != RTMEM_PROT_NONE, VERR_INVALID_PARAMETER);
842 AssertReturn(!(fProt & ~(RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
843 if (R0Process == NIL_RTR0PROCESS)
844 R0Process = RTR0ProcHandleSelf();
845 RT_ASSERT_PREEMPTIBLE();
846
847 /* do the mapping. */
848 rc = rtR0MemObjNativeMapUser(&pNew, pMemToMap, R3PtrFixed, uAlignment, fProt, R0Process);
849 if (RT_SUCCESS(rc))
850 {
851 /* link it. */
852 rc = rtR0MemObjLink(pMemToMap, pNew);
853 if (RT_SUCCESS(rc))
854 *pMemObj = pNew;
855 else
856 {
857 /* damn, out of memory. bail out. */
858 int rc2 = rtR0MemObjNativeFree(pNew);
859 AssertRC(rc2);
860 pNew->u32Magic++;
861 pNew->enmType = RTR0MEMOBJTYPE_END;
862 RTMemFree(pNew);
863 }
864 }
865
866 return rc;
867}
868RT_EXPORT_SYMBOL(RTR0MemObjMapUser);
869
870
871RTR0DECL(int) RTR0MemObjProtect(RTR0MEMOBJ hMemObj, size_t offSub, size_t cbSub, uint32_t fProt)
872{
873 PRTR0MEMOBJINTERNAL pMemObj;
874 int rc;
875
876 /* sanity checks. */
877 pMemObj = (PRTR0MEMOBJINTERNAL)hMemObj;
878 AssertPtrReturn(pMemObj, VERR_INVALID_HANDLE);
879 AssertReturn(pMemObj->u32Magic == RTR0MEMOBJ_MAGIC, VERR_INVALID_HANDLE);
880 AssertReturn(pMemObj->enmType > RTR0MEMOBJTYPE_INVALID && pMemObj->enmType < RTR0MEMOBJTYPE_END, VERR_INVALID_HANDLE);
881 AssertReturn(rtR0MemObjIsProtectable(pMemObj), VERR_INVALID_PARAMETER);
882 AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
883 AssertReturn(offSub < pMemObj->cb, VERR_INVALID_PARAMETER);
884 AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_INVALID_PARAMETER);
885 AssertReturn(cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
886 AssertReturn(offSub + cbSub <= pMemObj->cb, VERR_INVALID_PARAMETER);
887 AssertReturn(!(fProt & ~(RTMEM_PROT_NONE | RTMEM_PROT_READ | RTMEM_PROT_WRITE | RTMEM_PROT_EXEC)), VERR_INVALID_PARAMETER);
888 RT_ASSERT_PREEMPTIBLE();
889
890 /* do the job */
891 rc = rtR0MemObjNativeProtect(pMemObj, offSub, cbSub, fProt);
892 if (RT_SUCCESS(rc))
893 pMemObj->fFlags |= RTR0MEMOBJ_FLAGS_PROT_CHANGED; /* record it */
894
895 return rc;
896}
897RT_EXPORT_SYMBOL(RTR0MemObjProtect);
898
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