VirtualBox

source: vbox/trunk/include/iprt/memobj.h@ 25559

Last change on this file since 25559 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 eol-style set to native
File size: 14.7 KB
Line 
1/** @file
2 * IPRT - Memory Objects (Ring-0).
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 *
25 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_memobj_h
31#define ___iprt_memobj_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35
36RT_C_DECLS_BEGIN
37
38/** @defgroup grp_rt_memobj RTMemObj - Memory Object Manipulation (Ring-0)
39 * @ingroup grp_rt
40 * @{
41 */
42
43#ifdef IN_RING0
44
45/**
46 * Checks if this is mapping or not.
47 *
48 * @returns true if it's a mapping, otherwise false.
49 * @param MemObj The ring-0 memory object handle.
50 */
51RTR0DECL(bool) RTR0MemObjIsMapping(RTR0MEMOBJ MemObj);
52
53/**
54 * Gets the address of a ring-0 memory object.
55 *
56 * @returns The address of the memory object.
57 * @returns NULL if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
58 * @param MemObj The ring-0 memory object handle.
59 */
60RTR0DECL(void *) RTR0MemObjAddress(RTR0MEMOBJ MemObj);
61
62/**
63 * Gets the ring-3 address of a ring-0 memory object.
64 *
65 * This only applies to ring-0 memory object with ring-3 mappings of some kind, i.e.
66 * locked user memory, reserved user address space and user mappings. This API should
67 * not be used on any other objects.
68 *
69 * @returns The address of the memory object.
70 * @returns NIL_RTR3PTR if the handle is invalid or if it's not an object with a ring-3 mapping.
71 * Strict builds will assert in both cases.
72 * @param MemObj The ring-0 memory object handle.
73 */
74RTR0DECL(RTR3PTR) RTR0MemObjAddressR3(RTR0MEMOBJ MemObj);
75
76/**
77 * Gets the size of a ring-0 memory object.
78 *
79 * @returns The address of the memory object.
80 * @returns NULL if the handle is invalid (asserts in strict builds) or if there isn't any mapping.
81 * @param MemObj The ring-0 memory object handle.
82 */
83RTR0DECL(size_t) RTR0MemObjSize(RTR0MEMOBJ MemObj);
84
85/**
86 * Get the physical address of an page in the memory object.
87 *
88 * @returns The physical address.
89 * @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
90 * @returns NIL_RTHCPHYS if the iPage is out of range.
91 * @returns NIL_RTHCPHYS if the object handle isn't valid.
92 * @param MemObj The ring-0 memory object handle.
93 * @param iPage The page number within the object.
94 */
95RTR0DECL(RTHCPHYS) RTR0MemObjGetPagePhysAddr(RTR0MEMOBJ MemObj, size_t iPage);
96
97/**
98 * Frees a ring-0 memory object.
99 *
100 * @returns IPRT status code.
101 * @retval VERR_INVALID_HANDLE if
102 * @param MemObj The ring-0 memory object to be freed. NULL is accepted.
103 * @param fFreeMappings Whether or not to free mappings of the object.
104 */
105RTR0DECL(int) RTR0MemObjFree(RTR0MEMOBJ MemObj, bool fFreeMappings);
106
107/**
108 * Allocates page aligned virtual kernel memory.
109 *
110 * The memory is taken from a non paged (= fixed physical memory backing) pool.
111 *
112 * @returns IPRT status code.
113 * @param pMemObj Where to store the ring-0 memory object handle.
114 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
115 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
116 */
117RTR0DECL(int) RTR0MemObjAllocPage(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable);
118
119/**
120 * Allocates page aligned virtual kernel memory with physical backing below 4GB.
121 *
122 * The physical memory backing the allocation is fixed.
123 *
124 * @returns IPRT status code.
125 * @param pMemObj Where to store the ring-0 memory object handle.
126 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
127 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
128 */
129RTR0DECL(int) RTR0MemObjAllocLow(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable);
130
131/**
132 * Allocates page aligned virtual kernel memory with contiguous physical backing below 4GB.
133 *
134 * The physical memory backing the allocation is fixed.
135 *
136 * @returns IPRT status code.
137 * @param pMemObj Where to store the ring-0 memory object handle.
138 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
139 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
140 */
141RTR0DECL(int) RTR0MemObjAllocCont(PRTR0MEMOBJ pMemObj, size_t cb, bool fExecutable);
142
143/**
144 * Locks a range of user virtual memory.
145 *
146 * @returns IPRT status code.
147 * @param pMemObj Where to store the ring-0 memory object handle.
148 * @param R3Ptr User virtual address. This is rounded down to a page
149 * boundrary.
150 * @param cb Number of bytes to lock. This is rounded up to
151 * nearest page boundrary.
152 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
153 * and RTMEM_PROT_WRITE.
154 * @param R0Process The process to lock pages in. NIL_R0PROCESS is an
155 * alias for the current one.
156 *
157 * @remarks RTR0MemGetAddressR3() and RTR0MemGetAddress() will return therounded
158 * down address.
159 *
160 * @remarks Linux: This API requires that the memory begin locked is in a memory
161 * mapping that is not required in any forked off child process. This
162 * is not intented as permanent restriction, feel free to help out
163 * lifting it.
164 */
165RTR0DECL(int) RTR0MemObjLockUser(PRTR0MEMOBJ pMemObj, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess, RTR0PROCESS R0Process);
166
167/**
168 * Locks a range of kernel virtual memory.
169 *
170 * @returns IPRT status code.
171 * @param pMemObj Where to store the ring-0 memory object handle.
172 * @param pv Kernel virtual address. This is rounded down to a page boundrary.
173 * @param cb Number of bytes to lock. This is rounded up to nearest page boundrary.
174 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
175 * and RTMEM_PROT_WRITE.
176 *
177 * @remark RTR0MemGetAddress() will return the rounded down address.
178 */
179RTR0DECL(int) RTR0MemObjLockKernel(PRTR0MEMOBJ pMemObj, void *pv, size_t cb, uint32_t fAccess);
180
181/**
182 * Allocates contiguous page aligned physical memory without (necessarily) any kernel mapping.
183 *
184 * @returns IPRT status code.
185 * @param pMemObj Where to store the ring-0 memory object handle.
186 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
187 * @param PhysHighest The highest permittable address (inclusive).
188 * Pass NIL_RTHCPHYS if any address is acceptable.
189 */
190RTR0DECL(int) RTR0MemObjAllocPhys(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest);
191
192/**
193 * Allocates non-contiguous page aligned physical memory without (necessarily) any kernel mapping.
194 *
195 * This API is for allocating huge amounts of pages and will return
196 * VERR_NOT_SUPPORTED if this cannot be implemented in a satisfactory
197 * manner.
198 *
199 * @returns IPRT status code.
200 * @retval VERR_NOT_SUPPORTED if it's not possible to allocated unmapped
201 * physical memory on this platform. The caller should expect
202 * this error and have a fallback strategy for it.
203 *
204 * @param pMemObj Where to store the ring-0 memory object handle.
205 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
206 * @param PhysHighest The highest permittable address (inclusive).
207 * Pass NIL_RTHCPHYS if any address is acceptable.
208 */
209RTR0DECL(int) RTR0MemObjAllocPhysNC(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest);
210
211/**
212 * Creates a page aligned, contiguous, physical memory object.
213 *
214 * No physical memory is allocated, we trust you do know what you're doing.
215 *
216 * @returns IPRT status code.
217 * @param pMemObj Where to store the ring-0 memory object handle.
218 * @param Phys The physical address to start at. This is rounded down to the
219 * nearest page boundrary.
220 * @param cb The size of the object in bytes. This is rounded up to nearest page boundrary.
221 */
222RTR0DECL(int) RTR0MemObjEnterPhys(PRTR0MEMOBJ pMemObj, RTHCPHYS Phys, size_t cb);
223
224/**
225 * Reserves kernel virtual address space.
226 *
227 * If this function fails with VERR_NOT_SUPPORTED, the idea is that you
228 * can use RTR0MemObjEnterPhys() + RTR0MemObjMapKernel() as a fallback if
229 * you have a safe physical address range to make use of...
230 *
231 * @returns IPRT status code.
232 * @param pMemObj Where to store the ring-0 memory object handle.
233 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
234 * @param cb The number of bytes to reserve. This is rounded up to nearest page.
235 * @param uAlignment The alignment of the reserved memory.
236 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
237 */
238RTR0DECL(int) RTR0MemObjReserveKernel(PRTR0MEMOBJ pMemObj, void *pvFixed, size_t cb, size_t uAlignment);
239
240/**
241 * Reserves user virtual address space in the current process.
242 *
243 * @returns IPRT status code.
244 * @param pMemObj Where to store the ring-0 memory object handle.
245 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
246 * @param cb The number of bytes to reserve. This is rounded up to nearest PAGE_SIZE.
247 * @param uAlignment The alignment of the reserved memory.
248 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
249 * @param R0Process The process to reserve the memory in. NIL_R0PROCESS is an alias for the current one.
250 */
251RTR0DECL(int) RTR0MemObjReserveUser(PRTR0MEMOBJ pMemObj, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process);
252
253/**
254 * Maps a memory object into kernel virtual address space.
255 *
256 * This is the same as calling RTR0MemObjMapKernelEx with cbSub and offSub set
257 * to zero.
258 *
259 * @returns IPRT status code.
260 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
261 * @param MemObjToMap The object to be map.
262 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
263 * @param uAlignment The alignment of the reserved memory.
264 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
265 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
266 */
267RTR0DECL(int) RTR0MemObjMapKernel(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt);
268
269/**
270 * Maps a memory object into kernel virtual address space.
271 *
272 * The ability to map subsections of the object into kernel space is currently
273 * not implemented on all platforms. All/Most of platforms supports mapping the
274 * whole object into kernel space.
275 *
276 * @returns IPRT status code.
277 * @retval VERR_NOT_SUPPORTED if it's not possible to map a subsection of a
278 * memory object on this platform. When you hit this, try implement it.
279 *
280 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
281 * @param MemObjToMap The object to be map.
282 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
283 * @param uAlignment The alignment of the reserved memory.
284 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
285 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
286 * @param offSub Where in the object to start mapping. If non-zero
287 * the value must be page aligned and cbSub must be
288 * non-zero as well.
289 * @param cbSub The size of the part of the object to be mapped. If
290 * zero the entire object is mapped. The value must be
291 * page aligned.
292 */
293RTR0DECL(int) RTR0MemObjMapKernelEx(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment,
294 unsigned fProt, size_t offSub, size_t cbSub);
295
296/**
297 * Maps a memory object into user virtual address space in the current process.
298 *
299 * @returns IPRT status code.
300 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
301 * @param MemObjToMap The object to be map.
302 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
303 * @param uAlignment The alignment of the reserved memory.
304 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
305 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
306 * @param R0Process The process to map the memory into. NIL_R0PROCESS is an alias for the current one.
307 */
308RTR0DECL(int) RTR0MemObjMapUser(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
309
310/**
311 * Change the page level protection of one or more pages in a memory object.
312 *
313 * @returns IPRT status code.
314 * @retval VERR_NOT_SUPPORTED if the OS doesn't provide any way to manipulate
315 * page level protection. The caller must handle this status code
316 * gracefully. (Note that it may also occur if the implementation is
317 * missing, in which case just go ahead and implement it.)
318 *
319 * @param hMemObj Memory object handle.
320 * @param offSub Offset into the memory object. Must be page aligned.
321 * @param cbSub Number of bytes to change the protection of. Must be
322 * page aligned.
323 * @param fProt Combination of RTMEM_PROT_* flags.
324 */
325RTR0DECL(int) RTR0MemObjProtect(RTR0MEMOBJ hMemObj, size_t offSub, size_t cbSub, uint32_t fProt);
326
327#endif /* IN_RING0 */
328
329/** @} */
330
331RT_C_DECLS_END
332
333#endif
334
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