VirtualBox

source: vbox/trunk/src/VBox/Runtime/include/internal/memobj.h@ 25690

Last change on this file since 25690 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: 17.2 KB
Line 
1/* $Revision: 23610 $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects.
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#ifndef ___internal_memobj_h
32#define ___internal_memobj_h
33
34#include <iprt/memobj.h>
35#include <iprt/assert.h>
36#include "internal/magics.h"
37
38RT_C_DECLS_BEGIN
39
40/** @defgroup grp_rt_memobj_int Internals.
41 * @ingroup grp_rt_memobj
42 * @internal
43 * @{
44 */
45
46/**
47 * Ring-0 memory object type.
48 */
49typedef enum RTR0MEMOBJTYPE
50{
51 /** The traditional invalid value. */
52 RTR0MEMOBJTYPE_INVALID = 0,
53
54 /** @name Primary types (parents)
55 * @{ */
56 /** RTR0MemObjAllocPage.
57 * This memory is page aligned and fixed. */
58 RTR0MEMOBJTYPE_PAGE,
59 /** RTR0MemObjAllocLow.
60 * This memory is page aligned, fixed and is backed by physical memory below 4GB. */
61 RTR0MEMOBJTYPE_LOW,
62 /** RTR0MemObjAllocCont.
63 * This memory is page aligned, fixed and is backed by contiguous physical memory below 4GB. */
64 RTR0MEMOBJTYPE_CONT,
65 /** RTR0MemObjLockKernel, RTR0MemObjLockUser.
66 * This memory is page aligned and fixed. It was locked/pinned/wired down by the API call. */
67 RTR0MEMOBJTYPE_LOCK,
68 /** RTR0MemObjAllocPhys, RTR0MemObjEnterPhys.
69 * This memory is physical memory, page aligned, contiguous and doesn't need to have a mapping. */
70 RTR0MEMOBJTYPE_PHYS,
71 /** RTR0MemObjAllocPhysNC.
72 * This memory is physical memory, page aligned and doesn't need to have a mapping. */
73 RTR0MEMOBJTYPE_PHYS_NC,
74 /** RTR0MemObjReserveKernel, RTR0MemObjReserveUser.
75 * This memory is page aligned and has no backing. */
76 RTR0MEMOBJTYPE_RES_VIRT,
77 /** @} */
78
79 /** @name Secondary types (children)
80 * @{
81 */
82 /** RTR0MemObjMapUser, RTR0MemObjMapKernel.
83 * This is a user or kernel context mapping of another ring-0 memory object. */
84 RTR0MEMOBJTYPE_MAPPING,
85 /** @} */
86
87 /** The end of the valid types. Used for sanity checking. */
88 RTR0MEMOBJTYPE_END
89} RTR0MEMOBJTYPE;
90
91
92/** @name RTR0MEMOBJINTERNAL::fFlags
93 * @{ */
94/** Page level protection was changed. */
95#define RTR0MEMOBJ_FLAGS_PROT_CHANGED RT_BIT_32(0)
96/** @} */
97
98
99typedef struct RTR0MEMOBJINTERNAL *PRTR0MEMOBJINTERNAL;
100typedef struct RTR0MEMOBJINTERNAL **PPRTR0MEMOBJINTERNAL;
101
102/**
103 * Ring-0 memory object.
104 *
105 * When using the PRTR0MEMOBJINTERNAL and PPRTR0MEMOBJINTERNAL types
106 * we get pMem and ppMem variable names.
107 *
108 * When using the RTR0MEMOBJ and PRTR0MEMOBJ types we get MemObj and
109 * pMemObj variable names. We never dereference variables of the RTR0MEMOBJ
110 * type, we always convert it to a PRTR0MEMOBJECTINTERNAL variable first.
111 */
112typedef struct RTR0MEMOBJINTERNAL
113{
114 /** Magic number (RTR0MEM_MAGIC). */
115 uint32_t u32Magic;
116 /** The size of this structure. */
117 uint32_t cbSelf;
118 /** The type of allocation. */
119 RTR0MEMOBJTYPE enmType;
120 /** Flags, RTR0MEMOBJ_FLAGS_*. */
121 uint32_t fFlags;
122 /** The size of the memory allocated, pinned down, or mapped. */
123 size_t cb;
124 /** The memory address.
125 * What this really is varies with the type.
126 * For PAGE, CONT, LOW, RES_VIRT/R0, LOCK/R0 and MAP/R0 it's the ring-0 mapping.
127 * For LOCK/R3, RES_VIRT/R3 and MAP/R3 it is the ring-3 mapping.
128 * For PHYS this might actually be NULL if there isn't any mapping.
129 */
130 void *pv;
131
132 /** Object relations. */
133 union
134 {
135 /** This is for tracking child memory handles mapping the
136 * memory described by the primary handle. */
137 struct
138 {
139 /** Number of mappings. */
140 uint32_t cMappingsAllocated;
141 /** Number of mappings in the array. */
142 uint32_t cMappings;
143 /** Pointers to child handles mapping this memory. */
144 PPRTR0MEMOBJINTERNAL papMappings;
145 } Parent;
146
147 /** Pointer to the primary handle. */
148 struct
149 {
150 /** Pointer to the parent. */
151 PRTR0MEMOBJINTERNAL pParent;
152 } Child;
153 } uRel;
154
155 /** Type specific data for the memory types that requires that. */
156 union
157 {
158 /** RTR0MEMTYPE_PAGE. */
159 struct
160 {
161 unsigned iDummy;
162 } Page;
163
164 /** RTR0MEMTYPE_LOW. */
165 struct
166 {
167 unsigned iDummy;
168 } Low;
169
170 /** RTR0MEMTYPE_CONT. */
171 struct
172 {
173 /** The physical address of the first page. */
174 RTHCPHYS Phys;
175 } Cont;
176
177 /** RTR0MEMTYPE_LOCK_USER. */
178 struct
179 {
180 /** The process that owns the locked memory.
181 * This is NIL_RTR0PROCESS if it's kernel memory. */
182 RTR0PROCESS R0Process;
183 } Lock;
184
185 /** RTR0MEMTYPE_PHYS. */
186 struct
187 {
188 /** The base address of the physical memory. */
189 RTHCPHYS PhysBase;
190 /** If set this object was created by RTR0MemPhysAlloc, otherwise it was
191 * created by RTR0MemPhysEnter. */
192 bool fAllocated;
193 } Phys;
194
195 /** RTR0MEMTYPE_PHYS_NC. */
196 struct
197 {
198 unsigned iDummy;
199 } PhysNC;
200
201 /** RTR0MEMOBJTYPE_RES_VIRT */
202 struct
203 {
204 /** The process that owns the reserved memory.
205 * This is NIL_RTR0PROCESS if it's kernel memory. */
206 RTR0PROCESS R0Process;
207 } ResVirt;
208
209 /** RTR0MEMOBJTYPE_MAPPING */
210 struct
211 {
212 /** The process that owns the reserved memory.
213 * This is NIL_RTR0PROCESS if it's kernel memory. */
214 RTR0PROCESS R0Process;
215 } Mapping;
216 } u;
217
218} RTR0MEMOBJINTERNAL;
219
220
221/**
222 * Checks if this is mapping or not.
223 *
224 * @returns true if it's a mapping, otherwise false.
225 * @param pMem The ring-0 memory object handle.
226 * @see RTR0MemObjIsMapping
227 */
228DECLINLINE(bool) rtR0MemObjIsMapping(PRTR0MEMOBJINTERNAL pMem)
229{
230 switch (pMem->enmType)
231 {
232 case RTR0MEMOBJTYPE_MAPPING:
233 return true;
234
235 default:
236 return false;
237 }
238}
239
240
241/**
242 * Checks page level protection can be changed on this object.
243 *
244 * @returns true / false.
245 * @param pMem The ring-0 memory object handle.
246 */
247DECLINLINE(bool) rtR0MemObjIsProtectable(PRTR0MEMOBJINTERNAL pMem)
248{
249 switch (pMem->enmType)
250 {
251 case RTR0MEMOBJTYPE_MAPPING:
252 case RTR0MEMOBJTYPE_PAGE:
253 case RTR0MEMOBJTYPE_LOW:
254 case RTR0MEMOBJTYPE_CONT:
255 return true;
256
257 default:
258 return false;
259 }
260}
261
262
263/**
264 * Checks if RTR0MEMOBJ::pv is a ring-3 pointer or not.
265 *
266 * @returns true if it's a object with a ring-3 address, otherwise false.
267 * @param pMem The ring-0 memory object handle.
268 */
269DECLINLINE(bool) rtR0MemObjIsRing3(PRTR0MEMOBJINTERNAL pMem)
270{
271 switch (pMem->enmType)
272 {
273 case RTR0MEMOBJTYPE_RES_VIRT:
274 return pMem->u.ResVirt.R0Process != NIL_RTR0PROCESS;
275 case RTR0MEMOBJTYPE_LOCK:
276 return pMem->u.Lock.R0Process != NIL_RTR0PROCESS;
277 case RTR0MEMOBJTYPE_MAPPING:
278 return pMem->u.Mapping.R0Process != NIL_RTR0PROCESS;
279 default:
280 return false;
281 }
282}
283
284
285/**
286 * Frees the memory object (but not the handle).
287 * Any OS specific handle resources will be freed by this call.
288 *
289 * @returns IPRT status code. On failure it is assumed that the object remains valid.
290 * @param pMem The ring-0 memory object handle to the memory which should be freed.
291 */
292int rtR0MemObjNativeFree(PRTR0MEMOBJINTERNAL pMem);
293
294/**
295 * Allocates page aligned virtual kernel memory.
296 *
297 * The memory is taken from a non paged (= fixed physical memory backing) pool.
298 *
299 * @returns IPRT status code.
300 * @param ppMem Where to store the ring-0 memory object handle.
301 * @param cb Number of bytes to allocate, page aligned.
302 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
303 */
304int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable);
305
306/**
307 * Allocates page aligned virtual kernel memory with physical backing below 4GB.
308 *
309 * The physical memory backing the allocation is fixed.
310 *
311 * @returns IPRT status code.
312 * @param ppMem Where to store the ring-0 memory object handle.
313 * @param cb Number of bytes to allocate, page aligned.
314 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
315 */
316int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable);
317
318/**
319 * Allocates page aligned virtual kernel memory with contiguous physical backing below 4GB.
320 *
321 * The physical memory backing the allocation is fixed.
322 *
323 * @returns IPRT status code.
324 * @param ppMem Where to store the ring-0 memory object handle.
325 * @param cb Number of bytes to allocate, page aligned.
326 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
327 */
328int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable);
329
330/**
331 * Locks a range of user virtual memory.
332 *
333 * @returns IPRT status code.
334 * @param ppMem Where to store the ring-0 memory object handle.
335 * @param R3Ptr User virtual address, page aligned.
336 * @param cb Number of bytes to lock, page aligned.
337 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
338 * and RTMEM_PROT_WRITE.
339 * @param R0Process The process to lock pages in.
340 */
341int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process);
342
343/**
344 * Locks a range of kernel virtual memory.
345 *
346 * @returns IPRT status code.
347 * @param ppMem Where to store the ring-0 memory object handle.
348 * @param pv Kernel virtual address, page aligned.
349 * @param cb Number of bytes to lock, page aligned.
350 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
351 * and RTMEM_PROT_WRITE.
352 */
353int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess);
354
355/**
356 * Allocates contiguous page aligned physical memory without (necessarily) any kernel mapping.
357 *
358 * @returns IPRT status code.
359 * @param ppMem Where to store the ring-0 memory object handle.
360 * @param cb Number of bytes to allocate, page aligned.
361 * @param PhysHighest The highest permittable address (inclusive).
362 * NIL_RTHCPHYS if any address is acceptable.
363 */
364int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest);
365
366/**
367 * Allocates non-contiguous page aligned physical memory without (necessarily) any kernel mapping.
368 *
369 * @returns IPRT status code.
370 * @retval VERR_NOT_SUPPORTED if it's not possible to allocated unmapped
371 * physical memory on this platform.
372 * @param ppMem Where to store the ring-0 memory object handle.
373 * @param cb Number of bytes to allocate, page aligned.
374 * @param PhysHighest The highest permittable address (inclusive).
375 * NIL_RTHCPHYS if any address is acceptable.
376 */
377int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest);
378
379/**
380 * Creates a page aligned, contiguous, physical memory object.
381 *
382 * @returns IPRT status code.
383 * @param ppMem Where to store the ring-0 memory object handle.
384 * @param Phys The physical address to start at, page aligned.
385 * @param cb The size of the object in bytes, page aligned.
386 */
387int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb);
388
389/**
390 * Reserves kernel virtual address space.
391 *
392 * @returns IPRT status code.
393 * Return VERR_NOT_SUPPORTED to indicate that the user should employ fallback strategies.
394 * @param ppMem Where to store the ring-0 memory object handle.
395 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
396 * @param cb The number of bytes to reserve, page aligned.
397 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
398 */
399int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment);
400
401/**
402 * Reserves user virtual address space in the current process.
403 *
404 * @returns IPRT status code.
405 * @param ppMem Where to store the ring-0 memory object handle.
406 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This matches uAlignment if specified.
407 * @param cb The number of bytes to reserve, page aligned.
408 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
409 * @param R0Process The process to reserve the memory in.
410 */
411int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process);
412
413/**
414 * Maps a memory object into user virtual address space in the current process.
415 *
416 * @returns IPRT status code.
417 * @retval VERR_NOT_SUPPORTED see RTR0MemObjMapKernelEx.
418 *
419 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
420 * @param pMemToMap The object to be map.
421 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
422 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
423 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
424 * @param offSub Where in the object to start mapping. If non-zero
425 * the value must be page aligned and cbSub must be
426 * non-zero as well.
427 * @param cbSub The size of the part of the object to be mapped. If
428 * zero the entire object is mapped. The value must be
429 * page aligned.
430 */
431int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
432 unsigned fProt, size_t offSub, size_t cbSub);
433
434/**
435 * Maps a memory object into user virtual address space in the current process.
436 *
437 * @returns IPRT status code.
438 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
439 * @param pMemToMap The object to be map.
440 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This matches uAlignment if specified.
441 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
442 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
443 * @param R0Process The process to map the memory into.
444 */
445int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
446
447/**
448 * Change the page level protection of one or more pages in a memory object.
449 *
450 * @returns IPRT status code.
451 * @retval VERR_NOT_SUPPORTED see RTR0MemObjProtect.
452 *
453 * @param pMem The memory object.
454 * @param offSub Offset into the memory object. Page aligned.
455 * @param cbSub Number of bytes to change the protection of. Page
456 * aligned.
457 * @param fProt Combination of RTMEM_PROT_* flags.
458 */
459int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt);
460
461/**
462 * Get the physical address of an page in the memory object.
463 *
464 * @returns The physical address.
465 * @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
466 * @returns NIL_RTHCPHYS if the iPage is out of range.
467 * @returns NIL_RTHCPHYS if the object handle isn't valid.
468 * @param pMem The ring-0 memory object handle.
469 * @param iPage The page number within the object (valid).
470 */
471RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage);
472
473PRTR0MEMOBJINTERNAL rtR0MemObjNew(size_t cbSelf, RTR0MEMOBJTYPE enmType, void *pv, size_t cb);
474void rtR0MemObjDelete(PRTR0MEMOBJINTERNAL pMem);
475
476/** @} */
477
478RT_C_DECLS_END
479
480#endif
481
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