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