VirtualBox

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

Last change on this file since 21194 was 20525, checked in by vboxsync, 16 years ago

iprt/memobj.h: Added RTR0MemObjProtect, only implemented for darwin.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev
File size: 16.9 KB
Line 
1/* $Revision: 20525 $ */
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 R0Process The process to lock pages in.
338 */
339int rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, RTR0PROCESS R0Process);
340
341/**
342 * Locks a range of kernel virtual memory.
343 *
344 * @returns IPRT status code.
345 * @param ppMem Where to store the ring-0 memory object handle.
346 * @param pv Kernel virtual address, page aligned.
347 * @param cb Number of bytes to lock, page aligned.
348 */
349int rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb);
350
351/**
352 * Allocates contiguous page aligned physical memory without (necessarily) any kernel mapping.
353 *
354 * @returns IPRT status code.
355 * @param ppMem Where to store the ring-0 memory object handle.
356 * @param cb Number of bytes to allocate, page aligned.
357 * @param PhysHighest The highest permittable address (inclusive).
358 * NIL_RTHCPHYS if any address is acceptable.
359 */
360int rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest);
361
362/**
363 * Allocates non-contiguous page aligned physical memory without (necessarily) any kernel mapping.
364 *
365 * @returns IPRT status code.
366 * @retval VERR_NOT_SUPPORTED if it's not possible to allocated unmapped
367 * physical memory on this platform.
368 * @param ppMem Where to store the ring-0 memory object handle.
369 * @param cb Number of bytes to allocate, page aligned.
370 * @param PhysHighest The highest permittable address (inclusive).
371 * NIL_RTHCPHYS if any address is acceptable.
372 */
373int rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest);
374
375/**
376 * Creates a page aligned, contiguous, physical memory object.
377 *
378 * @returns IPRT status code.
379 * @param ppMem Where to store the ring-0 memory object handle.
380 * @param Phys The physical address to start at, page aligned.
381 * @param cb The size of the object in bytes, page aligned.
382 */
383int rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb);
384
385/**
386 * Reserves kernel virtual address space.
387 *
388 * @returns IPRT status code.
389 * Return VERR_NOT_SUPPORTED to indicate that the user should employ fallback strategies.
390 * @param ppMem Where to store the ring-0 memory object handle.
391 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
392 * @param cb The number of bytes to reserve, page aligned.
393 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
394 */
395int rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment);
396
397/**
398 * Reserves user virtual address space in the current process.
399 *
400 * @returns IPRT status code.
401 * @param ppMem Where to store the ring-0 memory object handle.
402 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This matches uAlignment if specified.
403 * @param cb The number of bytes to reserve, page aligned.
404 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
405 * @param R0Process The process to reserve the memory in.
406 */
407int rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process);
408
409/**
410 * Maps a memory object into user virtual address space in the current process.
411 *
412 * @returns IPRT status code.
413 * @retval VERR_NOT_SUPPORTED see RTR0MemObjMapKernelEx.
414 *
415 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
416 * @param pMemToMap The object to be map.
417 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
418 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
419 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
420 * @param offSub Where in the object to start mapping. If non-zero
421 * the value must be page aligned and cbSub must be
422 * non-zero as well.
423 * @param cbSub The size of the part of the object to be mapped. If
424 * zero the entire object is mapped. The value must be
425 * page aligned.
426 */
427int rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
428 unsigned fProt, size_t offSub, size_t cbSub);
429
430/**
431 * Maps a memory object into user virtual address space in the current process.
432 *
433 * @returns IPRT status code.
434 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
435 * @param pMemToMap The object to be map.
436 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This matches uAlignment if specified.
437 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
438 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
439 * @param R0Process The process to map the memory into.
440 */
441int rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
442
443/**
444 * Change the page level protection of one or more pages in a memory object.
445 *
446 * @returns IPRT status code.
447 * @retval VERR_NOT_SUPPORTED see RTR0MemObjProtect.
448 *
449 * @param pMem The memory object.
450 * @param offSub Offset into the memory object. Page aligned.
451 * @param cbSub Number of bytes to change the protection of. Page
452 * aligned.
453 * @param fProt Combination of RTMEM_PROT_* flags.
454 */
455int rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt);
456
457/**
458 * Get the physical address of an page in the memory object.
459 *
460 * @returns The physical address.
461 * @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
462 * @returns NIL_RTHCPHYS if the iPage is out of range.
463 * @returns NIL_RTHCPHYS if the object handle isn't valid.
464 * @param pMem The ring-0 memory object handle.
465 * @param iPage The page number within the object (valid).
466 */
467RTHCPHYS rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage);
468
469PRTR0MEMOBJINTERNAL rtR0MemObjNew(size_t cbSelf, RTR0MEMOBJTYPE enmType, void *pv, size_t cb);
470void rtR0MemObjDelete(PRTR0MEMOBJINTERNAL pMem);
471
472/** @} */
473
474RT_C_DECLS_END
475
476#endif
477
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