VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/MMAll.cpp@ 93338

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

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.7 KB
Line 
1/* $Id: MMAll.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * MM - Memory Manager - Any Context.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_MM_HYPER
23#include <VBox/vmm/mm.h>
24#include <VBox/vmm/vmm.h>
25#include "MMInternal.h"
26#include <VBox/vmm/vmcc.h>
27#include <VBox/vmm/hm.h>
28#include <VBox/log.h>
29#include <iprt/assert.h>
30#include <iprt/string.h>
31
32
33
34/**
35 * Lookup a host context ring-3 address.
36 *
37 * @returns Pointer to the corresponding lookup record.
38 * @returns NULL on failure.
39 * @param pVM The cross context VM structure.
40 * @param R3Ptr The host context ring-3 address to lookup.
41 * @param poff Where to store the offset into the HMA memory chunk.
42 */
43DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupR3(PVM pVM, RTR3PTR R3Ptr, uint32_t *poff)
44{
45 /** @todo cache last lookup, this stuff ain't cheap! */
46 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
47 for (;;)
48 {
49 switch (pLookup->enmType)
50 {
51 case MMLOOKUPHYPERTYPE_LOCKED:
52 {
53 const RTR3UINTPTR off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.Locked.pvR3;
54 if (off < pLookup->cb)
55 {
56 *poff = off;
57 return pLookup;
58 }
59 break;
60 }
61
62 case MMLOOKUPHYPERTYPE_HCPHYS:
63 {
64 const RTR3UINTPTR off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.HCPhys.pvR3;
65 if (off < pLookup->cb)
66 {
67 *poff = off;
68 return pLookup;
69 }
70 break;
71 }
72
73 case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
74 case MMLOOKUPHYPERTYPE_MMIO2:
75 case MMLOOKUPHYPERTYPE_DYNAMIC:
76 break;
77
78 default:
79 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
80 break;
81 }
82
83 /* next */
84 if (pLookup->offNext == (int32_t)NIL_OFFSET)
85 break;
86 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
87 }
88
89 AssertMsgFailed(("R3Ptr=%RHv is not inside the hypervisor memory area!\n", R3Ptr));
90 return NULL;
91}
92
93
94/**
95 * Lookup a host context ring-0 address.
96 *
97 * @returns Pointer to the corresponding lookup record.
98 * @returns NULL on failure.
99 * @param pVM The cross context VM structure.
100 * @param R0Ptr The host context ring-0 address to lookup.
101 * @param poff Where to store the offset into the HMA memory chunk.
102 */
103DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupR0(PVM pVM, RTR0PTR R0Ptr, uint32_t *poff)
104{
105 AssertCompile(sizeof(RTR0PTR) == sizeof(RTR3PTR));
106
107 /** @todo cache last lookup, this stuff ain't cheap! */
108 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
109 for (;;)
110 {
111 switch (pLookup->enmType)
112 {
113 case MMLOOKUPHYPERTYPE_LOCKED:
114 {
115 const RTR0UINTPTR off = (RTR0UINTPTR)R0Ptr - (RTR0UINTPTR)pLookup->u.Locked.pvR0;
116 if (off < pLookup->cb && pLookup->u.Locked.pvR0)
117 {
118 *poff = off;
119 return pLookup;
120 }
121 break;
122 }
123
124 case MMLOOKUPHYPERTYPE_HCPHYS:
125 {
126 const RTR0UINTPTR off = (RTR0UINTPTR)R0Ptr - (RTR0UINTPTR)pLookup->u.HCPhys.pvR0;
127 if (off < pLookup->cb && pLookup->u.HCPhys.pvR0)
128 {
129 *poff = off;
130 return pLookup;
131 }
132 break;
133 }
134
135 case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
136 case MMLOOKUPHYPERTYPE_MMIO2:
137 case MMLOOKUPHYPERTYPE_DYNAMIC:
138 break;
139
140 default:
141 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
142 break;
143 }
144
145 /* next */
146 if (pLookup->offNext == (int32_t)NIL_OFFSET)
147 break;
148 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
149 }
150
151 AssertMsgFailed(("R0Ptr=%RHv is not inside the hypervisor memory area!\n", R0Ptr));
152 return NULL;
153}
154
155
156/**
157 * Lookup a raw-mode context address.
158 *
159 * @returns Pointer to the corresponding lookup record.
160 * @returns NULL on failure.
161 * @param pVM The cross context VM structure.
162 * @param RCPtr The raw-mode context address to lookup.
163 * @param poff Where to store the offset into the HMA memory chunk.
164 */
165DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupRC(PVM pVM, RTRCPTR RCPtr, uint32_t *poff)
166{
167 /** @todo cache last lookup this stuff ain't cheap! */
168 unsigned offRC = (RTRCUINTPTR)RCPtr - (RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC;
169 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
170 for (;;)
171 {
172 const uint32_t off = offRC - pLookup->off;
173 if (off < pLookup->cb)
174 {
175 switch (pLookup->enmType)
176 {
177 case MMLOOKUPHYPERTYPE_LOCKED:
178 case MMLOOKUPHYPERTYPE_HCPHYS:
179 *poff = off;
180 return pLookup;
181 default:
182 break;
183 }
184 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
185 *poff = 0; /* shut up gcc */
186 return NULL;
187 }
188
189 /* next */
190 if (pLookup->offNext == (int32_t)NIL_OFFSET)
191 break;
192 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
193 }
194
195 AssertMsgFailed(("RCPtr=%RRv is not inside the hypervisor memory area!\n", RCPtr));
196 *poff = 0; /* shut up gcc */
197 return NULL;
198}
199
200
201/**
202 * Lookup a current context address.
203 *
204 * @returns Pointer to the corresponding lookup record.
205 * @returns NULL on failure.
206 * @param pVM The cross context VM structure.
207 * @param pv The current context address to lookup.
208 * @param poff Where to store the offset into the HMA memory chunk.
209 */
210DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupCC(PVM pVM, void *pv, uint32_t *poff)
211{
212#ifdef IN_RING0
213 return mmHyperLookupR0(pVM, pv, poff);
214#elif defined(IN_RING3)
215 return mmHyperLookupR3(pVM, pv, poff);
216#else
217# error "Neither IN_RING0 nor IN_RING3!"
218#endif
219}
220
221
222/**
223 * Calculate the host context ring-3 address of an offset into the HMA memory chunk.
224 *
225 * @returns the host context ring-3 address.
226 * @param pLookup The HMA lookup record.
227 * @param off The offset into the HMA memory chunk.
228 */
229DECLINLINE(RTR3PTR) mmHyperLookupCalcR3(PMMLOOKUPHYPER pLookup, uint32_t off)
230{
231 switch (pLookup->enmType)
232 {
233 case MMLOOKUPHYPERTYPE_LOCKED:
234 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.Locked.pvR3 + off);
235 case MMLOOKUPHYPERTYPE_HCPHYS:
236 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.HCPhys.pvR3 + off);
237 default:
238 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
239 return NIL_RTR3PTR;
240 }
241}
242
243
244/**
245 * Calculate the host context ring-0 address of an offset into the HMA memory chunk.
246 *
247 * @returns the host context ring-0 address.
248 * @param pVM The cross context VM structure.
249 * @param pLookup The HMA lookup record.
250 * @param off The offset into the HMA memory chunk.
251 */
252DECLINLINE(RTR0PTR) mmHyperLookupCalcR0(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
253{
254 switch (pLookup->enmType)
255 {
256 case MMLOOKUPHYPERTYPE_LOCKED:
257 if (pLookup->u.Locked.pvR0)
258 return (RTR0PTR)((RTR0UINTPTR)pLookup->u.Locked.pvR0 + off);
259#ifdef IN_RING3
260 AssertMsg(SUPR3IsDriverless(), ("%s\n", R3STRING(pLookup->pszDesc)));
261#else
262 AssertMsgFailed(("%s\n", R3STRING(pLookup->pszDesc)));
263#endif
264 NOREF(pVM);
265 return NIL_RTR0PTR;
266
267 case MMLOOKUPHYPERTYPE_HCPHYS:
268 if (pLookup->u.HCPhys.pvR0)
269 return (RTR0PTR)((RTR0UINTPTR)pLookup->u.HCPhys.pvR0 + off);
270#ifdef IN_RING3
271 AssertMsg(SUPR3IsDriverless(), ("%s\n", R3STRING(pLookup->pszDesc)));
272#else
273 AssertMsgFailed(("%s\n", R3STRING(pLookup->pszDesc)));
274#endif
275 return NIL_RTR0PTR;
276
277 default:
278 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
279 return NIL_RTR0PTR;
280 }
281}
282
283
284/**
285 * Calculate the guest context address of an offset into the HMA memory chunk.
286 *
287 * @returns the guest context base address.
288 * @param pVM The cross context VM structure.
289 * @param pLookup The HMA lookup record.
290 * @param off The offset into the HMA memory chunk.
291 */
292DECLINLINE(void *) mmHyperLookupCalcCC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
293{
294#ifdef IN_RING0
295 return mmHyperLookupCalcR0(pVM, pLookup, off);
296#elif defined(IN_RING3)
297 NOREF(pVM);
298 return mmHyperLookupCalcR3(pLookup, off);
299#else
300# error "Neither IN_RING0 nor IN_RING3!"
301#endif
302}
303
304
305/**
306 * Converts a ring-0 host context address in the Hypervisor memory region to a ring-3 host context address.
307 *
308 * @returns ring-3 host context address.
309 * @param pVM The cross context VM structure.
310 * @param R0Ptr The ring-0 host context address.
311 * You'll be damned if this is not in the HMA! :-)
312 * @thread The Emulation Thread.
313 */
314VMMDECL(RTR3PTR) MMHyperR0ToR3(PVM pVM, RTR0PTR R0Ptr)
315{
316 uint32_t off;
317 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
318 if (pLookup)
319 return mmHyperLookupCalcR3(pLookup, off);
320 return NIL_RTR3PTR;
321}
322
323
324#ifndef IN_RING0
325/**
326 * Converts a ring-0 host context address in the Hypervisor memory region to a current context address.
327 *
328 * @returns current context address.
329 * @param pVM The cross context VM structure.
330 * @param R0Ptr The ring-0 host context address.
331 * You'll be damned if this is not in the HMA! :-)
332 * @thread The Emulation Thread.
333 */
334VMMDECL(void *) MMHyperR0ToCC(PVM pVM, RTR0PTR R0Ptr)
335{
336 uint32_t off;
337 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
338 if (pLookup)
339 return mmHyperLookupCalcCC(pVM, pLookup, off);
340 return NULL;
341}
342#endif
343
344
345/**
346 * Converts a ring-3 host context address in the Hypervisor memory region to a ring-0 host context address.
347 *
348 * @returns ring-0 host context address.
349 * @param pVM The cross context VM structure.
350 * @param R3Ptr The ring-3 host context address.
351 * You'll be damned if this is not in the HMA! :-)
352 * @thread The Emulation Thread.
353 */
354VMMDECL(RTR0PTR) MMHyperR3ToR0(PVM pVM, RTR3PTR R3Ptr)
355{
356 uint32_t off;
357 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
358 if (pLookup)
359 return mmHyperLookupCalcR0(pVM, pLookup, off);
360 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
361 return NIL_RTR0PTR;
362}
363
364
365#ifndef IN_RING3
366/**
367 * Converts a ring-3 host context address in the Hypervisor memory region to a current context address.
368 *
369 * @returns current context address.
370 * @param pVM The cross context VM structure.
371 * @param R3Ptr The ring-3 host context address.
372 * You'll be damned if this is not in the HMA! :-)
373 * @thread The Emulation Thread.
374 */
375VMMDECL(void *) MMHyperR3ToCC(PVM pVM, RTR3PTR R3Ptr)
376{
377 uint32_t off;
378 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
379 if (pLookup)
380 return mmHyperLookupCalcCC(pVM, pLookup, off);
381 return NULL;
382}
383#endif
384
385
386/**
387 * Converts a raw-mode context address in the Hypervisor memory region to a ring-3 context address.
388 *
389 * @returns ring-3 host context address.
390 * @param pVM The cross context VM structure.
391 * @param RCPtr The raw-mode context address.
392 * You'll be damned if this is not in the HMA! :-)
393 * @thread The Emulation Thread.
394 */
395VMMDECL(RTR3PTR) MMHyperRCToR3(PVM pVM, RTRCPTR RCPtr)
396{
397 uint32_t off;
398 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
399 if (pLookup)
400 return mmHyperLookupCalcR3(pLookup, off);
401 return NIL_RTR3PTR;
402}
403
404
405/**
406 * Converts a raw-mode context address in the Hypervisor memory region to a ring-0 host context address.
407 *
408 * @returns ring-0 host context address.
409 * @param pVM The cross context VM structure.
410 * @param RCPtr The raw-mode context address.
411 * You'll be damned if this is not in the HMA! :-)
412 * @thread The Emulation Thread.
413 */
414VMMDECL(RTR0PTR) MMHyperRCToR0(PVM pVM, RTRCPTR RCPtr)
415{
416 uint32_t off;
417 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
418 if (pLookup)
419 return mmHyperLookupCalcR0(pVM, pLookup, off);
420 return NIL_RTR0PTR;
421}
422
423
424/**
425 * Converts a raw-mode context address in the Hypervisor memory region to a current context address.
426 *
427 * @returns current context address.
428 * @param pVM The cross context VM structure.
429 * @param RCPtr The raw-mode host context address.
430 * You'll be damned if this is not in the HMA! :-)
431 * @thread The Emulation Thread.
432 */
433VMMDECL(void *) MMHyperRCToCC(PVM pVM, RTRCPTR RCPtr)
434{
435 uint32_t off;
436 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
437 if (pLookup)
438 return mmHyperLookupCalcCC(pVM, pLookup, off);
439 return NULL;
440}
441
442
443#ifndef IN_RING3
444/**
445 * Converts a current context address in the Hypervisor memory region to a ring-3 host context address.
446 *
447 * @returns ring-3 host context address.
448 * @param pVM The cross context VM structure.
449 * @param pv The current context address.
450 * You'll be damned if this is not in the HMA! :-)
451 * @thread The Emulation Thread.
452 */
453VMMDECL(RTR3PTR) MMHyperCCToR3(PVM pVM, void *pv)
454{
455 uint32_t off;
456 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
457 if (pLookup)
458 return mmHyperLookupCalcR3(pLookup, off);
459 return NIL_RTR3PTR;
460}
461#endif
462
463#ifndef IN_RING0
464/**
465 * Converts a current context address in the Hypervisor memory region to a ring-0 host context address.
466 *
467 * @returns ring-0 host context address.
468 * @param pVM The cross context VM structure.
469 * @param pv The current context address.
470 * You'll be damned if this is not in the HMA! :-)
471 * @thread The Emulation Thread.
472 */
473VMMDECL(RTR0PTR) MMHyperCCToR0(PVM pVM, void *pv)
474{
475 uint32_t off;
476 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
477 if (pLookup)
478 return mmHyperLookupCalcR0(pVM, pLookup, off);
479 return NIL_RTR0PTR;
480}
481#endif
482
483
484/**
485 * Gets the string name of a memory tag.
486 *
487 * @returns name of enmTag.
488 * @param enmTag The tag.
489 */
490const char *mmGetTagName(MMTAG enmTag)
491{
492 switch (enmTag)
493 {
494 #define TAG2STR(tag) case MM_TAG_##tag: return #tag
495
496 TAG2STR(CFGM);
497 TAG2STR(CFGM_BYTES);
498 TAG2STR(CFGM_STRING);
499 TAG2STR(CFGM_USER);
500
501 TAG2STR(CPUM_CTX);
502 TAG2STR(CPUM_CPUID);
503 TAG2STR(CPUM_MSRS);
504
505 TAG2STR(CSAM);
506 TAG2STR(CSAM_PATCH);
507
508 TAG2STR(DBGF);
509 TAG2STR(DBGF_AS);
510 TAG2STR(DBGF_FLOWTRACE);
511 TAG2STR(DBGF_INFO);
512 TAG2STR(DBGF_LINE);
513 TAG2STR(DBGF_LINE_DUP);
514 TAG2STR(DBGF_MODULE);
515 TAG2STR(DBGF_OS);
516 TAG2STR(DBGF_REG);
517 TAG2STR(DBGF_STACK);
518 TAG2STR(DBGF_SYMBOL);
519 TAG2STR(DBGF_SYMBOL_DUP);
520 TAG2STR(DBGF_TYPE);
521 TAG2STR(DBGF_TRACER);
522
523 TAG2STR(EM);
524
525 TAG2STR(IEM);
526
527 TAG2STR(IOM);
528 TAG2STR(IOM_STATS);
529
530 TAG2STR(MM);
531 TAG2STR(MM_LOOKUP_GUEST);
532 TAG2STR(MM_LOOKUP_PHYS);
533 TAG2STR(MM_LOOKUP_VIRT);
534 TAG2STR(MM_PAGE);
535
536 TAG2STR(PARAV);
537
538 TAG2STR(PATM);
539 TAG2STR(PATM_PATCH);
540
541 TAG2STR(PDM);
542 TAG2STR(PDM_DEVICE);
543 TAG2STR(PDM_DEVICE_DESC);
544 TAG2STR(PDM_DEVICE_USER);
545 TAG2STR(PDM_DRIVER);
546 TAG2STR(PDM_DRIVER_DESC);
547 TAG2STR(PDM_DRIVER_USER);
548 TAG2STR(PDM_USB);
549 TAG2STR(PDM_USB_DESC);
550 TAG2STR(PDM_USB_USER);
551 TAG2STR(PDM_LUN);
552 TAG2STR(PDM_QUEUE);
553 TAG2STR(PDM_THREAD);
554 TAG2STR(PDM_ASYNC_COMPLETION);
555#ifdef VBOX_WITH_NETSHAPER
556 TAG2STR(PDM_NET_SHAPER);
557#endif /* VBOX_WITH_NETSHAPER */
558
559 TAG2STR(PGM);
560 TAG2STR(PGM_CHUNK_MAPPING);
561 TAG2STR(PGM_HANDLERS);
562 TAG2STR(PGM_HANDLER_TYPES);
563 TAG2STR(PGM_MAPPINGS);
564 TAG2STR(PGM_PHYS);
565 TAG2STR(PGM_POOL);
566
567 TAG2STR(REM);
568
569 TAG2STR(SELM);
570
571 TAG2STR(SSM);
572
573 TAG2STR(STAM);
574
575 TAG2STR(TM);
576
577 TAG2STR(TRPM);
578
579 TAG2STR(VM);
580 TAG2STR(VM_REQ);
581
582 TAG2STR(VMM);
583
584 TAG2STR(HM);
585
586 #undef TAG2STR
587
588 default:
589 {
590 AssertMsgFailed(("Unknown tag %d! forgot to add it to the switch?\n", enmTag));
591#ifdef IN_RING3
592 static char sz[48];
593 RTStrPrintf(sz, sizeof(sz), "%d", enmTag);
594 return sz;
595#else
596 return "unknown tag!";
597#endif
598 }
599 }
600}
601
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