VirtualBox

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

Last change on this file since 91483 was 91483, checked in by vboxsync, 3 years ago

IPRT/memobj: Passing pszTag around...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Revision
File size: 20.3 KB
Line 
1/* $Id: memobj.h 91483 2021-09-30 00:19:19Z vboxsync $ */
2/** @file
3 * IPRT - Ring-0 Memory Objects.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
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
27#ifndef IPRT_INCLUDED_INTERNAL_memobj_h
28#define IPRT_INCLUDED_INTERNAL_memobj_h
29#ifndef RT_WITHOUT_PRAGMA_ONCE
30# pragma once
31#endif
32
33#include <iprt/memobj.h>
34#include <iprt/assert.h>
35#include "internal/magics.h"
36
37RT_C_DECLS_BEGIN
38
39/** @defgroup grp_rt_memobj_int Internals.
40 * @ingroup grp_rt_memobj
41 * @internal
42 * @{
43 */
44
45/**
46 * Ring-0 memory object type.
47 */
48typedef enum RTR0MEMOBJTYPE
49{
50 /** The traditional invalid value. */
51 RTR0MEMOBJTYPE_INVALID = 0,
52
53 /** @name Primary types (parents)
54 * @{ */
55 /** RTR0MemObjAllocPage.
56 * This memory is page aligned and fixed. */
57 RTR0MEMOBJTYPE_PAGE,
58 /** RTR0MemObjAllocLow.
59 * This memory is page aligned, fixed and is backed by physical memory below 4GB. */
60 RTR0MEMOBJTYPE_LOW,
61 /** RTR0MemObjAllocCont.
62 * This memory is page aligned, fixed and is backed by contiguous physical memory below 4GB. */
63 RTR0MEMOBJTYPE_CONT,
64 /** RTR0MemObjLockKernel, RTR0MemObjLockUser.
65 * This memory is page aligned and fixed. It was locked/pinned/wired down by the API call. */
66 RTR0MEMOBJTYPE_LOCK,
67 /** RTR0MemObjAllocPhys, RTR0MemObjEnterPhys.
68 * This memory is physical memory, page aligned, contiguous and doesn't need to have a mapping. */
69 RTR0MEMOBJTYPE_PHYS,
70 /** RTR0MemObjAllocPhysNC.
71 * This memory is physical memory, page aligned and doesn't need to have a mapping. */
72 RTR0MEMOBJTYPE_PHYS_NC,
73 /** RTR0MemObjReserveKernel, RTR0MemObjReserveUser.
74 * This memory is page aligned and has no backing. */
75 RTR0MEMOBJTYPE_RES_VIRT,
76 /** @} */
77
78 /** @name Secondary types (children)
79 * @{
80 */
81 /** RTR0MemObjMapUser, RTR0MemObjMapKernel.
82 * This is a user or kernel context mapping of another ring-0 memory object. */
83 RTR0MEMOBJTYPE_MAPPING,
84 /** @} */
85
86 /** The end of the valid types. Used for sanity checking. */
87 RTR0MEMOBJTYPE_END
88} RTR0MEMOBJTYPE;
89
90
91/** @name RTR0MEMOBJINTERNAL::fFlags
92 * @{ */
93/** Page level protection was changed. */
94#define RTR0MEMOBJ_FLAGS_PROT_CHANGED RT_BIT_32(0)
95/** @} */
96
97
98typedef struct RTR0MEMOBJINTERNAL *PRTR0MEMOBJINTERNAL;
99typedef struct RTR0MEMOBJINTERNAL **PPRTR0MEMOBJINTERNAL;
100
101/**
102 * Ring-0 memory object.
103 *
104 * When using the PRTR0MEMOBJINTERNAL and PPRTR0MEMOBJINTERNAL types
105 * we get pMem and ppMem variable names.
106 *
107 * When using the RTR0MEMOBJ and PRTR0MEMOBJ types we get MemObj and
108 * pMemObj variable names. We never dereference variables of the RTR0MEMOBJ
109 * type, we always convert it to a PRTR0MEMOBJECTINTERNAL variable first.
110 */
111typedef struct RTR0MEMOBJINTERNAL
112{
113 /** Magic number (RTR0MEMOBJ_MAGIC). */
114 uint32_t u32Magic;
115 /** The size of this structure. */
116 uint32_t cbSelf;
117 /** The type of allocation. */
118 RTR0MEMOBJTYPE enmType;
119 /** Flags, RTR0MEMOBJ_FLAGS_*. */
120 uint32_t fFlags;
121 /** The size of the memory allocated, pinned down, or mapped. */
122 size_t cb;
123 /** The memory address.
124 * What this really is varies with the type.
125 * For PAGE, CONT, LOW, RES_VIRT/R0, LOCK/R0 and MAP/R0 it's the ring-0 mapping.
126 * For LOCK/R3, RES_VIRT/R3 and MAP/R3 it is the ring-3 mapping.
127 * For PHYS this might actually be NULL if there isn't any mapping.
128 */
129 void *pv;
130
131 /** Object relations. */
132 union
133 {
134 /** This is for tracking child memory handles mapping the
135 * memory described by the primary handle. */
136 struct
137 {
138 /** Number of mappings. */
139 uint32_t cMappingsAllocated;
140 /** Number of mappings in the array. */
141 uint32_t cMappings;
142 /** Pointers to child handles mapping this memory. */
143 PPRTR0MEMOBJINTERNAL papMappings;
144 } Parent;
145
146 /** Pointer to the primary handle. */
147 struct
148 {
149 /** Pointer to the parent. */
150 PRTR0MEMOBJINTERNAL pParent;
151 } Child;
152 } uRel;
153
154 /** Type specific data for the memory types that requires that. */
155 union
156 {
157 /** RTR0MEMTYPE_PAGE. */
158 struct
159 {
160 unsigned iDummy;
161 } Page;
162
163 /** RTR0MEMTYPE_LOW. */
164 struct
165 {
166 unsigned iDummy;
167 } Low;
168
169 /** RTR0MEMTYPE_CONT. */
170 struct
171 {
172 /** The physical address of the first page. */
173 RTHCPHYS Phys;
174 } Cont;
175
176 /** RTR0MEMTYPE_LOCK_USER. */
177 struct
178 {
179 /** The process that owns the locked memory.
180 * This is NIL_RTR0PROCESS if it's kernel memory. */
181 RTR0PROCESS R0Process;
182 } Lock;
183
184 /** RTR0MEMTYPE_PHYS. */
185 struct
186 {
187 /** The base address of the physical memory. */
188 RTHCPHYS PhysBase;
189 /** If set this object was created by RTR0MemPhysAlloc, otherwise it was
190 * created by RTR0MemPhysEnter. */
191 bool fAllocated;
192 /** See RTMEM_CACHE_POLICY_XXX constants */
193 uint32_t uCachePolicy;
194 } Phys;
195
196 /** RTR0MEMTYPE_PHYS_NC. */
197 struct
198 {
199 unsigned iDummy;
200 } PhysNC;
201
202 /** RTR0MEMOBJTYPE_RES_VIRT */
203 struct
204 {
205 /** The process that owns the reserved memory.
206 * This is NIL_RTR0PROCESS if it's kernel memory. */
207 RTR0PROCESS R0Process;
208 } ResVirt;
209
210 /** RTR0MEMOBJTYPE_MAPPING */
211 struct
212 {
213 /** The process that owns the reserved memory.
214 * This is NIL_RTR0PROCESS if it's kernel memory. */
215 RTR0PROCESS R0Process;
216 } Mapping;
217 } u;
218
219#if defined(DEBUG)
220 /** Allocation tag string. */
221 const char *pszTag;
222#endif
223} RTR0MEMOBJINTERNAL;
224
225
226/**
227 * Checks if this is mapping or not.
228 *
229 * @returns true if it's a mapping, otherwise false.
230 * @param pMem The ring-0 memory object handle.
231 * @see RTR0MemObjIsMapping
232 */
233DECLINLINE(bool) rtR0MemObjIsMapping(PRTR0MEMOBJINTERNAL pMem)
234{
235 switch (pMem->enmType)
236 {
237 case RTR0MEMOBJTYPE_MAPPING:
238 return true;
239
240 default:
241 return false;
242 }
243}
244
245
246/**
247 * Checks page level protection can be changed on this object.
248 *
249 * @returns true / false.
250 * @param pMem The ring-0 memory object handle.
251 */
252DECLINLINE(bool) rtR0MemObjIsProtectable(PRTR0MEMOBJINTERNAL pMem)
253{
254 switch (pMem->enmType)
255 {
256 case RTR0MEMOBJTYPE_MAPPING:
257 case RTR0MEMOBJTYPE_PAGE:
258 case RTR0MEMOBJTYPE_LOW:
259 case RTR0MEMOBJTYPE_CONT:
260 return true;
261
262 default:
263 return false;
264 }
265}
266
267
268/**
269 * Checks if RTR0MEMOBJ::pv is a ring-3 pointer or not.
270 *
271 * @returns true if it's a object with a ring-3 address, otherwise false.
272 * @param pMem The ring-0 memory object handle.
273 */
274DECLINLINE(bool) rtR0MemObjIsRing3(PRTR0MEMOBJINTERNAL pMem)
275{
276 switch (pMem->enmType)
277 {
278 case RTR0MEMOBJTYPE_RES_VIRT:
279 return pMem->u.ResVirt.R0Process != NIL_RTR0PROCESS;
280 case RTR0MEMOBJTYPE_LOCK:
281 return pMem->u.Lock.R0Process != NIL_RTR0PROCESS;
282 case RTR0MEMOBJTYPE_MAPPING:
283 return pMem->u.Mapping.R0Process != NIL_RTR0PROCESS;
284 default:
285 return false;
286 }
287}
288
289
290/**
291 * Frees the memory object (but not the handle).
292 * Any OS specific handle resources will be freed by this call.
293 *
294 * @returns IPRT status code. On failure it is assumed that the object remains valid.
295 * @param pMem The ring-0 memory object handle to the memory which should be freed.
296 */
297DECLHIDDEN(int) rtR0MemObjNativeFree(PRTR0MEMOBJINTERNAL pMem);
298
299/**
300 * Allocates page aligned virtual kernel memory.
301 *
302 * The memory is taken from a non paged (= fixed physical memory backing) pool.
303 *
304 * @returns IPRT status code.
305 * @param ppMem Where to store the ring-0 memory object handle.
306 * @param cb Number of bytes to allocate, page aligned.
307 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
308 * @param pszTag Allocation tag used for statistics and such.
309 */
310DECLHIDDEN(int) rtR0MemObjNativeAllocPage(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable, const char *pszTag);
311
312/**
313 * Worker for RTR0MemObjAllocLargeTag.
314 *
315 * @returns IPRT status code.
316 * @param ppMem Where to store the ring-0 memory object handle.
317 * @param cb Number of bytes to allocate, aligned to @a
318 * cbLargePage.
319 * @param cbLargePage The large page size.
320 * @param fFlags RTMEMOBJ_ALLOC_LARGE_F_XXX, validated.
321 * @param pszTag The allocation tag.
322 */
323DECLHIDDEN(int) rtR0MemObjNativeAllocLarge(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, size_t cbLargePage, uint32_t fFlags,
324 const char *pszTag);
325
326/**
327 * Allocates page aligned virtual kernel memory with physical backing below 4GB.
328 *
329 * The physical memory backing the allocation is fixed.
330 *
331 * @returns IPRT status code.
332 * @param ppMem Where to store the ring-0 memory object handle.
333 * @param cb Number of bytes to allocate, page aligned.
334 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
335 * @param pszTag Allocation tag used for statistics and such.
336 */
337DECLHIDDEN(int) rtR0MemObjNativeAllocLow(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable, const char *pszTag);
338
339/**
340 * Allocates page aligned virtual kernel memory with contiguous physical backing below 4GB.
341 *
342 * The physical memory backing the allocation is fixed.
343 *
344 * @returns IPRT status code.
345 * @param ppMem Where to store the ring-0 memory object handle.
346 * @param cb Number of bytes to allocate, page aligned.
347 * @param fExecutable Flag indicating whether it should be permitted to executed code in the memory object.
348 * @param pszTag Allocation tag used for statistics and such.
349 */
350DECLHIDDEN(int) rtR0MemObjNativeAllocCont(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, bool fExecutable, const char *pszTag);
351
352/**
353 * Locks a range of user virtual memory.
354 *
355 * @returns IPRT status code.
356 * @param ppMem Where to store the ring-0 memory object handle.
357 * @param R3Ptr User virtual address, page aligned.
358 * @param cb Number of bytes to lock, page aligned.
359 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
360 * and RTMEM_PROT_WRITE.
361 * @param R0Process The process to lock pages in.
362 * @param pszTag Allocation tag used for statistics and such.
363 */
364DECLHIDDEN(int) rtR0MemObjNativeLockUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3Ptr, size_t cb, uint32_t fAccess,
365 RTR0PROCESS R0Process, const char *pszTag);
366
367/**
368 * Locks a range of kernel virtual memory.
369 *
370 * @returns IPRT status code.
371 * @param ppMem Where to store the ring-0 memory object handle.
372 * @param pv Kernel virtual address, page aligned.
373 * @param cb Number of bytes to lock, page aligned.
374 * @param fAccess The desired access, a combination of RTMEM_PROT_READ
375 * and RTMEM_PROT_WRITE.
376 * @param pszTag Allocation tag used for statistics and such.
377 */
378DECLHIDDEN(int) rtR0MemObjNativeLockKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pv, size_t cb, uint32_t fAccess, const char *pszTag);
379
380/**
381 * Allocates contiguous page aligned physical memory without (necessarily) any
382 * kernel mapping.
383 *
384 * @returns IPRT status code.
385 * @param ppMem Where to store the ring-0 memory object handle.
386 * @param cb Number of bytes to allocate, page aligned.
387 * @param PhysHighest The highest permitable address (inclusive).
388 * NIL_RTHCPHYS if any address is acceptable.
389 * @param uAlignment The alignment of the reserved memory.
390 * Supported values are PAGE_SIZE, _2M, _4M and _1G.
391 * @param pszTag Allocation tag used for statistics and such.
392 */
393DECLHIDDEN(int) rtR0MemObjNativeAllocPhys(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, size_t uAlignment,
394 const char *pszTag);
395
396/**
397 * Allocates non-contiguous page aligned physical memory without (necessarily) any kernel mapping.
398 *
399 * @returns IPRT status code.
400 * @retval VERR_NOT_SUPPORTED if it's not possible to allocated unmapped
401 * physical memory on this platform.
402 * @param ppMem Where to store the ring-0 memory object handle.
403 * @param cb Number of bytes to allocate, page aligned.
404 * @param PhysHighest The highest permitable address (inclusive).
405 * NIL_RTHCPHYS if any address is acceptable.
406 * @param pszTag Allocation tag used for statistics and such.
407 */
408DECLHIDDEN(int) rtR0MemObjNativeAllocPhysNC(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, RTHCPHYS PhysHighest, const char *pszTag);
409
410/**
411 * Creates a page aligned, contiguous, physical memory object.
412 *
413 * @returns IPRT status code.
414 * @param ppMem Where to store the ring-0 memory object handle.
415 * @param Phys The physical address to start at, page aligned.
416 * @param cb The size of the object in bytes, page aligned.
417 * @param uCachePolicy One of the RTMEM_CACHE_XXX modes.
418 * @param pszTag Allocation tag used for statistics and such.
419 */
420DECLHIDDEN(int) rtR0MemObjNativeEnterPhys(PPRTR0MEMOBJINTERNAL ppMem, RTHCPHYS Phys, size_t cb, uint32_t uCachePolicy,
421 const char *pszTag);
422
423/**
424 * Reserves kernel virtual address space.
425 *
426 * @returns IPRT status code.
427 * Return VERR_NOT_SUPPORTED to indicate that the user should employ fallback strategies.
428 * @param ppMem Where to store the ring-0 memory object handle.
429 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
430 * @param cb The number of bytes to reserve, page aligned.
431 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
432 * @param pszTag Allocation tag used for statistics and such.
433 */
434DECLHIDDEN(int) rtR0MemObjNativeReserveKernel(PPRTR0MEMOBJINTERNAL ppMem, void *pvFixed, size_t cb, size_t uAlignment,
435 const char *pszTag);
436
437/**
438 * Reserves user virtual address space in the current process.
439 *
440 * @returns IPRT status code.
441 * @param ppMem Where to store the ring-0 memory object handle.
442 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This matches uAlignment if specified.
443 * @param cb The number of bytes to reserve, page aligned.
444 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
445 * @param R0Process The process to reserve the memory in.
446 * @param pszTag Allocation tag used for statistics and such.
447 */
448DECLHIDDEN(int) rtR0MemObjNativeReserveUser(PPRTR0MEMOBJINTERNAL ppMem, RTR3PTR R3PtrFixed, size_t cb, size_t uAlignment,
449 RTR0PROCESS R0Process, const char *pszTag);
450
451/**
452 * Maps a memory object into user virtual address space in the current process.
453 *
454 * @returns IPRT status code.
455 * @retval VERR_NOT_SUPPORTED see RTR0MemObjMapKernelEx.
456 *
457 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
458 * @param pMemToMap The object to be map.
459 * @param pvFixed Requested address. (void *)-1 means any address. This matches uAlignment if specified.
460 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
461 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
462 * @param offSub Where in the object to start mapping. If non-zero
463 * the value must be page aligned and cbSub must be
464 * non-zero as well.
465 * @param cbSub The size of the part of the object to be mapped. If
466 * zero the entire object is mapped. The value must be
467 * page aligned.
468 * @param pszTag Allocation tag used for statistics and such.
469 */
470DECLHIDDEN(int) rtR0MemObjNativeMapKernel(PPRTR0MEMOBJINTERNAL ppMem, RTR0MEMOBJ pMemToMap, void *pvFixed, size_t uAlignment,
471 unsigned fProt, size_t offSub, size_t cbSub, const char *pszTag);
472
473/**
474 * Maps a memory object into user virtual address space in the current process.
475 *
476 * @returns IPRT status code.
477 * @param ppMem Where to store the ring-0 memory object handle of the mapping object.
478 * @param pMemToMap The object to be map.
479 * @param R3PtrFixed Requested address. (RTR3PTR)-1 means any address. This matches uAlignment if specified.
480 * @param uAlignment The alignment of the reserved memory; PAGE_SIZE, _2M or _4M.
481 * @param fProt Combination of RTMEM_PROT_* flags (except RTMEM_PROT_NONE).
482 * @param R0Process The process to map the memory into.
483 * @param offSub Where in the object to start mapping. If non-zero
484 * the value must be page aligned and cbSub must be
485 * non-zero as well.
486 * @param cbSub The size of the part of the object to be mapped. If
487 * zero the entire object is mapped. The value must be
488 * page aligned.
489 * @param pszTag Allocation tag used for statistics and such.
490 */
491DECLHIDDEN(int) rtR0MemObjNativeMapUser(PPRTR0MEMOBJINTERNAL ppMem, PRTR0MEMOBJINTERNAL pMemToMap, RTR3PTR R3PtrFixed,
492 size_t uAlignment, unsigned fProt, RTR0PROCESS R0Process, size_t offSub, size_t cbSub,
493 const char *pszTag);
494
495/**
496 * Change the page level protection of one or more pages in a memory object.
497 *
498 * @returns IPRT status code.
499 * @retval VERR_NOT_SUPPORTED see RTR0MemObjProtect.
500 *
501 * @param pMem The memory object.
502 * @param offSub Offset into the memory object. Page aligned.
503 * @param cbSub Number of bytes to change the protection of. Page
504 * aligned.
505 * @param fProt Combination of RTMEM_PROT_* flags.
506 */
507DECLHIDDEN(int) rtR0MemObjNativeProtect(PRTR0MEMOBJINTERNAL pMem, size_t offSub, size_t cbSub, uint32_t fProt);
508
509/**
510 * Get the physical address of an page in the memory object.
511 *
512 * @returns The physical address.
513 * @returns NIL_RTHCPHYS if the object doesn't contain fixed physical pages.
514 * @returns NIL_RTHCPHYS if the iPage is out of range.
515 * @returns NIL_RTHCPHYS if the object handle isn't valid.
516 * @param pMem The ring-0 memory object handle.
517 * @param iPage The page number within the object (valid).
518 */
519DECLHIDDEN(RTHCPHYS) rtR0MemObjNativeGetPagePhysAddr(PRTR0MEMOBJINTERNAL pMem, size_t iPage);
520
521DECLHIDDEN(PRTR0MEMOBJINTERNAL) rtR0MemObjNew(size_t cbSelf, RTR0MEMOBJTYPE enmType, void *pv, size_t cb, const char *pszTag);
522DECLHIDDEN(void) rtR0MemObjDelete(PRTR0MEMOBJINTERNAL pMem);
523DECLHIDDEN(int) rtR0MemObjFallbackAllocLarge(PPRTR0MEMOBJINTERNAL ppMem, size_t cb, size_t cbLargePage, uint32_t fFlags,
524 const char *pszTag);
525
526/** @} */
527
528RT_C_DECLS_END
529
530#endif /* !IPRT_INCLUDED_INTERNAL_memobj_h */
531
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