VirtualBox

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

Last change on this file since 1816 was 1816, checked in by vboxsync, 18 years ago

moved magics to a common header to avoid duplicating the same defines all over the place.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1/* $Id: memobj.h 1816 2007-03-29 18:59:35Z vboxsync $ */
2/** @file
3 * InnoTek Portable Runtime - Ring-0 Memory Objects.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef __internal_memobj_h__
23#define __internal_memobj_h__
24
25#include <iprt/memobj.h>
26#include <iprt/assert.h>
27#include "internal/magics.h"
28
29
30/** @defgroup grp_rt_memobj_int Internals.
31 * @ingroup grp_rt_memobj
32 * @internal
33 * @{
34 */
35
36/**
37 * Ring-0 memory object type.
38 */
39typedef enum RTR0MEMOBJTYPE
40{
41 /** The traditional invalid value. */
42 RTR0MEMOBJTYPE_INVALID = 0,
43
44 /** @name Primary types (parents)
45 * @{ */
46 /** RTR0MemObjAllocPage.
47 * This memory is page aligned and fixed. */
48 RTR0MEMOBJTYPE_PAGE,
49 /** RTR0MemObjAllocLow.
50 * This memory is page aligned, fixed and is backed by physical memory below 4GB. */
51 RTR0MEMOBJTYPE_LOW,
52 /** RTR0MemObjAllocCont.
53 * This memory is page aligned, fixed and is backed by contiguous physical memory below 4GB. */
54 RTR0MEMOBJTYPE_CONT,
55 /** RTR0MemObjLockKernel, RTR0MemObjLockUser.
56 * This memory is page aligned and fixed. It was locked/pinned/wired down by the API call. */
57 RTR0MEMOBJTYPE_LOCK,
58 /** RTR0MemObjAllocPhys, RTR0MemObjEnterPhys.
59 * This memory is physical memory, page aligned and doesn't need to have a mapping. */
60 RTR0MEMOBJTYPE_PHYS,
61 /** RTR0MemObjReserveKernel, RTR0MemObjReserveUser.
62 * This memory is page aligned and has no backing. */
63 RTR0MEMOBJTYPE_RES_VIRT,
64 /** @} */
65
66 /** @name Secondary types (children)
67 * @{
68 */
69 /** RTR0MemObjMapUser, RTR0MemObjMapKernel.
70 * This is a user or kernel context mapping of another ring-0 memory object. */
71 RTR0MEMOBJTYPE_MAPPING,
72 /** @} */
73
74 /** The end of the valid types. Used for sanity checking. */
75 RTR0MEMOBJTYPE_END
76} RTR0MEMOBJTYPE;
77
78
79typedef struct RTR0MEMOBJINTERNAL *PRTR0MEMOBJINTERNAL;
80typedef struct RTR0MEMOBJINTERNAL **PPRTR0MEMOBJINTERNAL;
81
82/**
83 * Ring-0 memory object.
84 *
85 * When using the PRTR0MEMOBJINTERNAL and PPRTR0MEMOBJINTERNAL types
86 * we get pMem and ppMem variable names.
87 *
88 * When using the RTR0MEMOBJ and PRTR0MEMOBJ types we get MemObj and
89 * pMemObj variable names. We never dereference variables of the RTR0MEMOBJ
90 * type, we always convert it to a PRTR0MEMOBJECTINTERNAL variable first.
91 */
92typedef struct RTR0MEMOBJINTERNAL
93{
94 /** Magic number (RTR0MEM_MAGIC). */
95 uint32_t u32Magic;
96 /** The size of this structure. */
97 uint32_t cbSelf;
98 /** The type of allocation. */
99 RTR0MEMOBJTYPE enmType;
100 /** The size of the memory allocated, pinned down, or mapped. */
101 size_t cb;
102 /** The memory address.
103 * What this really is varies with the type.
104 * For PAGE, CONT, LOW, RES_VIRT, LOCK/R0 and MAP/R0 it's the ring-0 mapping.
105 * For LOCK/R3 and MAP/R3 it is the ring-3 mapping.
106 * For PHYS this might actually be NULL if there isn't any mapping.
107 */
108 void *pv;
109
110 /** Object relations. */
111 union
112 {
113 /** This is for tracking child memory handles mapping the
114 * memory described by the primary handle. */
115 struct
116 {
117 /** Number of mappings. */
118 uint32_t cMappingsAllocated;
119 /** Number of mappings in the array. */
120 uint32_t cMappings;
121 /** Pointers to child handles mapping this memory. */
122 PPRTR0MEMOBJINTERNAL papMappings;
123 } Parent;
124
125 /** Pointer to the primary handle. */
126 struct
127 {
128 /** Pointer to the parent. */
129 PRTR0MEMOBJINTERNAL pParent;
130 } Child;
131 } uRel;
132
133 /** Type specific data for the memory types that requires that. */
134 union
135 {
136 /** RTR0MEMTYPE_PAGE. */
137 struct
138 {
139 unsigned iDummy;
140 } Page;
141
142 /** RTR0MEMTYPE_LOW. */
143 struct
144 {
145 unsigned iDummy;
146 } Low;
147
148 /** RTR0MEMTYPE_CONT. */
149 struct
150 {
151 /** The physical address of the first page. */
152 RTHCPHYS Phys;
153 } Cont;
154
155 /** RTR0MEMTYPE_LOCK_USER. */
156 struct
157 {
158 /** The process that owns the locked memory.
159 * This is NIL_RTR0PROCESS if it's kernel memory. */
160 RTR0PROCESS R0Process;
161 } Lock;
162
163 /** RTR0MEMTYPE_PHYS. */
164 struct
165 {
166 /** The base address of the physical memory that's being mapped. */
167 RTHCPHYS PhysBase;
168 /** If set this object was created by RTR0MemPhysAlloc, otherwise by RTR0MemPhysEnter. */
169 bool fAllocated;
170 } Phys;
171
172 /** RTR0MEMOBJTYPE_RES_VIRT */
173 struct
174 {
175 /** The process that owns the reserved memory.
176 * This is NIL_RTR0PROCESS if it's kernel memory. */
177 RTR0PROCESS R0Process;
178 } ResVirt;
179
180 /** RTR0MEMOBJTYPE_MAPPING */
181 struct
182 {
183 /** The process that owns the reserved memory.
184 * This is NIL_RTR0PROCESS if it's kernel memory. */
185 RTR0PROCESS R0Process;
186 } Mapping;
187 } u;
188
189} RTR0MEMOBJINTERNAL;
190
191
192/**
193 * Checks if this is mapping or not.
194 *
195 * @returns true if it's a mapping, otherwise false.
196 * @param MemObj The ring-0 memory object handle.
197 * @see RTR0MemObjIsMapping
198 */
199DECLINLINE(bool) rtR0MemObjIsMapping(PRTR0MEMOBJINTERNAL pMem)
200{
201 switch (pMem->enmType)
202 {
203 case RTR0MEMOBJTYPE_MAPPING:
204 return true;
205
206 default:
207 return false;
208 }
209}
210
211
212/**
213 * Frees the memory object (but not the handle).
214 * Any OS specific handle resources will be freed by this call.
215 *
216 * @returns IPRT status code. On failure it is assumed that the object remains valid.
217 * @param pMem The ring-0 memory object handle to the memory which should be freed.
218 */
219int rtR0MemObjNativeFree(PRTR0MEMOBJINTERNAL pMem);
220
221/**
222 * Allocates page aligned virtual kernel memory.
223 *
224 * The memory is taken from a non paged (= fixed physical memory backing) pool.
225 *
226 * @returns IPRT status code.
227 * @param ppMem Where to store the ring-0 memory object handle.
228 * @param cb Number of bytes to allocate, page aligned.
229 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
230 */
231int rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable);
232
233/**
234 * Allocates page aligned virtual kernel memory with physical backing below 4GB.
235 *
236 * The physical memory backing the allocation is fixed.
237 *
238 * @returns IPRT status code.
239 * @param ppMem Where to store the ring-0 memory object handle.
240 * @param cb Number of bytes to allocate, page aligned.
241 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
242 */
243int rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable);
244
245/**
246 * Allocates page aligned virtual kernel memory with contiguous physical backing below 4GB.
247 *
248 * The physical memory backing the allocation is fixed.
249 *
250 * @returns IPRT status code.
251 * @param ppMem Where to store the ring-0 memory object handle.
252 * @param cb Number of bytes to allocate, page aligned.
253 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
254 */
255int rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable);
256
257/**
258 * Locks a range of user virtual memory.
259 *
260 * @returns IPRT status code.
261 * @param ppMem Where to store the ring-0 memory object handle.
262 * @param pv User virtual address, page aligned.
263 * @param cb Number of bytes to lock, page aligned.
264 * @param R0Process The process to lock pages in.
265 */
266int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, RTR0PROCESS R0Process);
267
268/**
269 * Locks a range of kernel virtual memory.
270 *
271 * @returns IPRT status code.
272 * @param ppMem Where to store the ring-0 memory object handle.
273 * @param pv Kernel virtual address, page aligned.
274 * @param cb Number of bytes to lock, page aligned.
275 */
276int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb);
277
278/**
279 * Allocates page aligned physical memory without (necessarily) any kernel mapping.
280 *
281 * @returns IPRT status code.
282 * @param ppMem Where to store the ring-0 memory object handle.
283 * @param cb Number of bytes to allocate, page aligned.
284 * @param PhysHighest The highest permittable address (inclusive).
285 * NIL_RTHCPHYS if any address is acceptable.
286 */
287int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest);
288
289/**
290 * Creates a page aligned, contiguous, physical memory object.
291 *
292 * @returns IPRT status code.
293 * @param ppMem Where to store the ring-0 memory object handle.
294 * @param Phys The physical address to start at, page aligned.
295 * @param cb The size of the object in bytes, page aligned.
296 */
297int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb);
298
299/**
300 * Reserves kernel virtual address space.
301 *
302 * @returns IPRT status code.
303 * @param ppMem Where to store the ring-0 memory object handle.
304 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
305 * @param cb The number of bytes to reserve, page aligned.
306 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
307 */
308int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment);
309
310/**
311 * Reserves user virtual address space in the current process.
312 *
313 * @returns IPRT status code.
314 * @param ppMem Where to store the ring-0 memory object handle.
315 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
316 * @param cb The number of bytes to reserve, page aligned.
317 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
318 * @param R0Process The process to reserve the memory in.
319 */
320int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process);
321
322/**
323 * Maps a memory object into user virtual address space in the current process.
324 *
325 * @returns IPRT status code.
326 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
327 * @param pMemToMap The object to be map.
328 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
329 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
330 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
331 */
332int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt);
333
334/**
335 * Maps a memory object into user virtual address space in the current process.
336 *
337 * @returns IPRT status code.
338 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
339 * @param pMemToMap The object to be map.
340 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
341 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
342 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
343 * @param R0Process The process to map the memory into.
344 */
345int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, void *pvFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
346
347/**
348 * Get the physical address of an page in the memory object.
349 *
350 * @returns The physical address.
351 * @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
352 * @returns NIL_RTHCPHYS if the iPage is out of range.
353 * @returns NIL_RTHCPHYS if the object handle isn't valid.
354 * @param pMem The ring-0 memory object handle.
355 * @param iPage The page number within the object (valid).
356 */
357RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, unsigned iPage);
358
359PRTR0MEMOBJINTERNAL rtR0MemObjNew(size_t cbSelf, RTR0MEMOBJTYPE enmType, void *pv, size_t cb);
360void rtR0MemObjDelete(PRTR0MEMOBJINTERNAL pMem);
361
362/** @} */
363
364#endif
365
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