VirtualBox

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

Last change on this file since 91909 was 91907, checked in by vboxsync, 4 years ago

VMM/MM: Eliminated MMHyperCCToRC and MMHyperR0ToRC. bugref:9517

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 18.6 KB
Line 
1/* $Id: MMAll.cpp 91907 2021-10-20 19:00:05Z vboxsync $ */
2/** @file
3 * MM - Memory Manager - Any Context.
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.215389.xyz. 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 AssertMsgFailed(("%s\n", R3STRING(pLookup->pszDesc))); NOREF(pVM);
260 return NIL_RTR0PTR;
261
262 case MMLOOKUPHYPERTYPE_HCPHYS:
263 if (pLookup->u.HCPhys.pvR0)
264 return (RTR0PTR)((RTR0UINTPTR)pLookup->u.HCPhys.pvR0 + off);
265 AssertMsgFailed(("%s\n", R3STRING(pLookup->pszDesc)));
266 return NIL_RTR0PTR;
267
268 default:
269 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
270 return NIL_RTR0PTR;
271 }
272}
273
274
275/**
276 * Calculate the raw-mode context address of an offset into the HMA memory chunk.
277 *
278 * @returns the raw-mode context base address.
279 * @param pVM The cross context VM structure.
280 * @param pLookup The HMA lookup record.
281 * @param off The offset into the HMA memory chunk.
282 */
283DECLINLINE(RTRCPTR) mmHyperLookupCalcRC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
284{
285 return (RTRCPTR)((RTRCUINTPTR)pVM->mm.s.pvHyperAreaGC + pLookup->off + off);
286}
287
288
289/**
290 * Calculate the guest context address of an offset into the HMA memory chunk.
291 *
292 * @returns the guest context base address.
293 * @param pVM The cross context VM structure.
294 * @param pLookup The HMA lookup record.
295 * @param off The offset into the HMA memory chunk.
296 */
297DECLINLINE(void *) mmHyperLookupCalcCC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
298{
299#ifdef IN_RING0
300 return mmHyperLookupCalcR0(pVM, pLookup, off);
301#elif defined(IN_RING3)
302 NOREF(pVM);
303 return mmHyperLookupCalcR3(pLookup, off);
304#else
305# error "Neither IN_RING0 nor IN_RING3!"
306#endif
307}
308
309
310/**
311 * Converts a ring-0 host context address in the Hypervisor memory region to a ring-3 host context address.
312 *
313 * @returns ring-3 host context address.
314 * @param pVM The cross context VM structure.
315 * @param R0Ptr The ring-0 host context address.
316 * You'll be damned if this is not in the HMA! :-)
317 * @thread The Emulation Thread.
318 */
319VMMDECL(RTR3PTR) MMHyperR0ToR3(PVM pVM, RTR0PTR R0Ptr)
320{
321 uint32_t off;
322 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
323 if (pLookup)
324 return mmHyperLookupCalcR3(pLookup, off);
325 return NIL_RTR3PTR;
326}
327
328
329#ifndef IN_RING0
330/**
331 * Converts a ring-0 host context address in the Hypervisor memory region to a current context address.
332 *
333 * @returns current context address.
334 * @param pVM The cross context VM structure.
335 * @param R0Ptr The ring-0 host context address.
336 * You'll be damned if this is not in the HMA! :-)
337 * @thread The Emulation Thread.
338 */
339VMMDECL(void *) MMHyperR0ToCC(PVM pVM, RTR0PTR R0Ptr)
340{
341 uint32_t off;
342 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
343 if (pLookup)
344 return mmHyperLookupCalcCC(pVM, pLookup, off);
345 return NULL;
346}
347#endif
348
349
350/**
351 * Converts a ring-3 host context address in the Hypervisor memory region to a ring-0 host context address.
352 *
353 * @returns ring-0 host context address.
354 * @param pVM The cross context VM structure.
355 * @param R3Ptr The ring-3 host context address.
356 * You'll be damned if this is not in the HMA! :-)
357 * @thread The Emulation Thread.
358 */
359VMMDECL(RTR0PTR) MMHyperR3ToR0(PVM pVM, RTR3PTR R3Ptr)
360{
361 uint32_t off;
362 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
363 if (pLookup)
364 return mmHyperLookupCalcR0(pVM, pLookup, off);
365 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
366 return NIL_RTR0PTR;
367}
368
369
370/**
371 * Converts a ring-3 host context address in the Hypervisor memory region to a guest context address.
372 *
373 * @returns guest context address.
374 * @param pVM The cross context VM structure.
375 * @param R3Ptr The ring-3 host context address.
376 * You'll be damned if this is not in the HMA! :-)
377 * @thread The Emulation Thread.
378 */
379VMMDECL(RTRCPTR) MMHyperR3ToRC(PVM pVM, RTR3PTR R3Ptr)
380{
381 uint32_t off;
382 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
383 if (pLookup)
384 return mmHyperLookupCalcRC(pVM, pLookup, off);
385 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
386 return NIL_RTRCPTR;
387}
388
389
390#ifndef IN_RING3
391/**
392 * Converts a ring-3 host context address in the Hypervisor memory region to a current context address.
393 *
394 * @returns current context address.
395 * @param pVM The cross context VM structure.
396 * @param R3Ptr The ring-3 host context address.
397 * You'll be damned if this is not in the HMA! :-)
398 * @thread The Emulation Thread.
399 */
400VMMDECL(void *) MMHyperR3ToCC(PVM pVM, RTR3PTR R3Ptr)
401{
402 uint32_t off;
403 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
404 if (pLookup)
405 return mmHyperLookupCalcCC(pVM, pLookup, off);
406 return NULL;
407}
408#endif
409
410
411/**
412 * Converts a raw-mode context address in the Hypervisor memory region to a ring-3 context address.
413 *
414 * @returns ring-3 host context address.
415 * @param pVM The cross context VM structure.
416 * @param RCPtr The raw-mode context address.
417 * You'll be damned if this is not in the HMA! :-)
418 * @thread The Emulation Thread.
419 */
420VMMDECL(RTR3PTR) MMHyperRCToR3(PVM pVM, RTRCPTR RCPtr)
421{
422 uint32_t off;
423 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
424 if (pLookup)
425 return mmHyperLookupCalcR3(pLookup, off);
426 return NIL_RTR3PTR;
427}
428
429
430/**
431 * Converts a raw-mode context address in the Hypervisor memory region to a ring-0 host context address.
432 *
433 * @returns ring-0 host context address.
434 * @param pVM The cross context VM structure.
435 * @param RCPtr The raw-mode context address.
436 * You'll be damned if this is not in the HMA! :-)
437 * @thread The Emulation Thread.
438 */
439VMMDECL(RTR0PTR) MMHyperRCToR0(PVM pVM, RTRCPTR RCPtr)
440{
441 uint32_t off;
442 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
443 if (pLookup)
444 return mmHyperLookupCalcR0(pVM, pLookup, off);
445 return NIL_RTR0PTR;
446}
447
448
449/**
450 * Converts a raw-mode context address in the Hypervisor memory region to a current context address.
451 *
452 * @returns current context address.
453 * @param pVM The cross context VM structure.
454 * @param RCPtr The raw-mode host context address.
455 * You'll be damned if this is not in the HMA! :-)
456 * @thread The Emulation Thread.
457 */
458VMMDECL(void *) MMHyperRCToCC(PVM pVM, RTRCPTR RCPtr)
459{
460 uint32_t off;
461 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
462 if (pLookup)
463 return mmHyperLookupCalcCC(pVM, pLookup, off);
464 return NULL;
465}
466
467
468#ifndef IN_RING3
469/**
470 * Converts a current context address in the Hypervisor memory region to a ring-3 host context address.
471 *
472 * @returns ring-3 host context address.
473 * @param pVM The cross context VM structure.
474 * @param pv The current context address.
475 * You'll be damned if this is not in the HMA! :-)
476 * @thread The Emulation Thread.
477 */
478VMMDECL(RTR3PTR) MMHyperCCToR3(PVM pVM, void *pv)
479{
480 uint32_t off;
481 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
482 if (pLookup)
483 return mmHyperLookupCalcR3(pLookup, off);
484 return NIL_RTR3PTR;
485}
486#endif
487
488#ifndef IN_RING0
489/**
490 * Converts a current context address in the Hypervisor memory region to a ring-0 host context address.
491 *
492 * @returns ring-0 host context address.
493 * @param pVM The cross context VM structure.
494 * @param pv The current context address.
495 * You'll be damned if this is not in the HMA! :-)
496 * @thread The Emulation Thread.
497 */
498VMMDECL(RTR0PTR) MMHyperCCToR0(PVM pVM, void *pv)
499{
500 uint32_t off;
501 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
502 if (pLookup)
503 return mmHyperLookupCalcR0(pVM, pLookup, off);
504 return NIL_RTR0PTR;
505}
506#endif
507
508
509/**
510 * Gets the string name of a memory tag.
511 *
512 * @returns name of enmTag.
513 * @param enmTag The tag.
514 */
515const char *mmGetTagName(MMTAG enmTag)
516{
517 switch (enmTag)
518 {
519 #define TAG2STR(tag) case MM_TAG_##tag: return #tag
520
521 TAG2STR(CFGM);
522 TAG2STR(CFGM_BYTES);
523 TAG2STR(CFGM_STRING);
524 TAG2STR(CFGM_USER);
525
526 TAG2STR(CPUM_CTX);
527 TAG2STR(CPUM_CPUID);
528 TAG2STR(CPUM_MSRS);
529
530 TAG2STR(CSAM);
531 TAG2STR(CSAM_PATCH);
532
533 TAG2STR(DBGF);
534 TAG2STR(DBGF_AS);
535 TAG2STR(DBGF_FLOWTRACE);
536 TAG2STR(DBGF_INFO);
537 TAG2STR(DBGF_LINE);
538 TAG2STR(DBGF_LINE_DUP);
539 TAG2STR(DBGF_MODULE);
540 TAG2STR(DBGF_OS);
541 TAG2STR(DBGF_REG);
542 TAG2STR(DBGF_STACK);
543 TAG2STR(DBGF_SYMBOL);
544 TAG2STR(DBGF_SYMBOL_DUP);
545 TAG2STR(DBGF_TYPE);
546 TAG2STR(DBGF_TRACER);
547
548 TAG2STR(EM);
549
550 TAG2STR(IEM);
551
552 TAG2STR(IOM);
553 TAG2STR(IOM_STATS);
554
555 TAG2STR(MM);
556 TAG2STR(MM_LOOKUP_GUEST);
557 TAG2STR(MM_LOOKUP_PHYS);
558 TAG2STR(MM_LOOKUP_VIRT);
559 TAG2STR(MM_PAGE);
560
561 TAG2STR(PARAV);
562
563 TAG2STR(PATM);
564 TAG2STR(PATM_PATCH);
565
566 TAG2STR(PDM);
567 TAG2STR(PDM_DEVICE);
568 TAG2STR(PDM_DEVICE_DESC);
569 TAG2STR(PDM_DEVICE_USER);
570 TAG2STR(PDM_DRIVER);
571 TAG2STR(PDM_DRIVER_DESC);
572 TAG2STR(PDM_DRIVER_USER);
573 TAG2STR(PDM_USB);
574 TAG2STR(PDM_USB_DESC);
575 TAG2STR(PDM_USB_USER);
576 TAG2STR(PDM_LUN);
577 TAG2STR(PDM_QUEUE);
578 TAG2STR(PDM_THREAD);
579 TAG2STR(PDM_ASYNC_COMPLETION);
580#ifdef VBOX_WITH_NETSHAPER
581 TAG2STR(PDM_NET_SHAPER);
582#endif /* VBOX_WITH_NETSHAPER */
583
584 TAG2STR(PGM);
585 TAG2STR(PGM_CHUNK_MAPPING);
586 TAG2STR(PGM_HANDLERS);
587 TAG2STR(PGM_HANDLER_TYPES);
588 TAG2STR(PGM_MAPPINGS);
589 TAG2STR(PGM_PHYS);
590 TAG2STR(PGM_POOL);
591
592 TAG2STR(REM);
593
594 TAG2STR(SELM);
595
596 TAG2STR(SSM);
597
598 TAG2STR(STAM);
599
600 TAG2STR(TM);
601
602 TAG2STR(TRPM);
603
604 TAG2STR(VM);
605 TAG2STR(VM_REQ);
606
607 TAG2STR(VMM);
608
609 TAG2STR(HM);
610
611 #undef TAG2STR
612
613 default:
614 {
615 AssertMsgFailed(("Unknown tag %d! forgot to add it to the switch?\n", enmTag));
616#ifdef IN_RING3
617 static char sz[48];
618 RTStrPrintf(sz, sizeof(sz), "%d", enmTag);
619 return sz;
620#else
621 return "unknown tag!";
622#endif
623 }
624 }
625}
626
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette