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