VirtualBox

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

Last change on this file since 26430 was 26430, checked in by vboxsync, 15 years ago

Introducing RTR0MemObjAllocPhysEx

  • Property eol-style set to native
File size: 15.4 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 contiguous page aligned physical memory without (necessarily) any kernel mapping.
194 *
195 * @returns IPRT status code.
196 * @param pMemObj Where to store the ring-0 memory object handle.
197 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
198 * @param PhysHighest The highest permittable address (inclusive).
199 * Pass NIL_RTHCPHYS if any address is acceptable.
200 * @param uAlignment The alignment of the reserved memory.
201 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M, _4M and _1G.
202 */
203RTR0DECL(int) RTR0MemObjAllocPhysEx(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment);
204
205/**
206 * Allocates non-contiguous page aligned physical memory without (necessarily) any kernel mapping.
207 *
208 * This API is for allocating huge amounts of pages and will return
209 * VERR_NOT_SUPPORTED if this cannot be implemented in a satisfactory
210 * manner.
211 *
212 * @returns IPRT status code.
213 * @retval VERR_NOT_SUPPORTED if it's not possible to allocated unmapped
214 * physical memory on this platform. The caller should expect
215 * this error and have a fallback strategy for it.
216 *
217 * @param pMemObj Where to store the ring-0 memory object handle.
218 * @param cb Number of bytes to allocate. This is rounded up to nearest page.
219 * @param PhysHighest The highest permittable address (inclusive).
220 * Pass NIL_RTHCPHYS if any address is acceptable.
221 */
222RTR0DECL(int) RTR0MemObjAllocPhysNC(PRTR0MEMOBJ pMemObj, size_t cb, RTHCPHYS PhysHighest);
223
224/**
225 * Creates a page aligned, contiguous, physical memory object.
226 *
227 * No physical memory is allocated, we trust you do know what you're doing.
228 *
229 * @returns IPRT status code.
230 * @param pMemObj Where to store the ring-0 memory object handle.
231 * @param Phys The physical address to start at. This is rounded down to the
232 * nearest page boundrary.
233 * @param cb The size of the object in bytes. This is rounded up to nearest page boundrary.
234 */
235RTR0DECL(int) RTR0MemObjEnterPhys(PRTR0MEMOBJ pMemObj, RTHCPHYS Phys, size_t cb);
236
237/**
238 * Reserves kernel virtual address space.
239 *
240 * If this function fails with VERR_NOT_SUPPORTED, the idea is that you
241 * can use RTR0MemObjEnterPhys() + RTR0MemObjMapKernel() as a fallback if
242 * you have a safe physical address range to make use of...
243 *
244 * @returns IPRT status code.
245 * @param pMemObj Where to store the ring-0 memory object handle.
246 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
247 * @param cb The number of bytes to reserve. This is rounded up to nearest page.
248 * @param uAlignment The alignment of the reserved memory.
249 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
250 */
251RTR0DECL(int) RTR0MemObjReserveKernel(PRTR0MEMOBJ pMemObj, void *pvFixed, size_t cb, size_t uAlignment);
252
253/**
254 * Reserves user virtual address space in the current process.
255 *
256 * @returns IPRT status code.
257 * @param pMemObj Where to store the ring-0 memory object handle.
258 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
259 * @param cb The number of bytes to reserve. This is rounded up to nearest PAGE_SIZE.
260 * @param uAlignment The alignment of the reserved memory.
261 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
262 * @param R0Process The process to reserve the memory in. NIL_R0PROCESS is an alias for the current one.
263 */
264RTR0DECL(int) RTR0MemObjReserveUser(PRTR0MEMOBJ pMemObj, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment, RTR0PROCESS R0Process);
265
266/**
267 * Maps a memory object into kernel virtual address space.
268 *
269 * This is the same as calling RTR0MemObjMapKernelEx with cbSub and offSub set
270 * to zero.
271 *
272 * @returns IPRT status code.
273 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
274 * @param MemObjToMap The object to be map.
275 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
276 * @param uAlignment The alignment of the reserved memory.
277 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
278 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
279 */
280RTR0DECL(int) RTR0MemObjMapKernel(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment, unsigned fProt);
281
282/**
283 * Maps a memory object into kernel virtual address space.
284 *
285 * The ability to map subsections of the object into kernel space is currently
286 * not implemented on all platforms. All/Most of platforms supports mapping the
287 * whole object into kernel space.
288 *
289 * @returns IPRT status code.
290 * @retval VERR_NOT_SUPPORTED if it's not possible to map a subsection of a
291 * memory object on this platform. When you hit this, try implement it.
292 *
293 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
294 * @param MemObjToMap The object to be map.
295 * @param pvFixed Requested address. (void *)-1 means any address. This must match the alignment.
296 * @param uAlignment The alignment of the reserved memory.
297 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
298 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
299 * @param offSub Where in the object to start mapping. If non-zero
300 * the value must be page aligned and cbSub must be
301 * non-zero as well.
302 * @param cbSub The size of the part of the object to be mapped. If
303 * zero the entire object is mapped. The value must be
304 * page aligned.
305 */
306RTR0DECL(int) RTR0MemObjMapKernelEx(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, void *pvFixed, size_t uAlignment,
307 unsigned fProt, size_t offSub, size_t cbSub);
308
309/**
310 * Maps a memory object into user virtual address space in the current process.
311 *
312 * @returns IPRT status code.
313 * @param pMemObj Where to store the ring-0 memory object handle of the mapping object.
314 * @param MemObjToMap The object to be map.
315 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This must match the alignment.
316 * @param uAlignment The alignment of the reserved memory.
317 * Supported values are 0 (alias for PAGE_SIZE), PAGE_SIZE, _2M and _4M.
318 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
319 * @param R0Process The process to map the memory into. NIL_R0PROCESS is an alias for the current one.
320 */
321RTR0DECL(int) RTR0MemObjMapUser(PRTR0MEMOBJ pMemObj, RTR0MEMOBJ MemObjToMap, RTR3PTR R3PtrFixed, size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process);
322
323/**
324 * Change the page level protection of one or more pages in a memory object.
325 *
326 * @returns IPRT status code.
327 * @retval VERR_NOT_SUPPORTED if the OS doesn't provide any way to manipulate
328 * page level protection. The caller must handle this status code
329 * gracefully. (Note that it may also occur if the implementation is
330 * missing, in which case just go ahead and implement it.)
331 *
332 * @param hMemObj Memory object handle.
333 * @param offSub Offset into the memory object. Must be page aligned.
334 * @param cbSub Number of bytes to change the protection of. Must be
335 * page aligned.
336 * @param fProt Combination of RTMEM_PROT_* flags.
337 */
338RTR0DECL(int) RTR0MemObjProtect(RTR0MEMOBJ hMemObj, size_t offSub, size_t cbSub, uint32_t fProt);
339
340#endif /* IN_RING0 */
341
342/** @} */
343
344RT_C_DECLS_END
345
346#endif
347
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