VirtualBox

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

Last change on this file since 1122 was 23, checked in by vboxsync, 18 years ago

string.h & stdio.h + header cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 18.7 KB
Line 
1/* $Id: MMAll.cpp 23 2007-01-15 14:08:28Z vboxsync $ */
2/** @file
3 * MM - Memory Monitor(/Manager) - Any Context.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_MM_HYPER
27#include <VBox/mm.h>
28#include "MMInternal.h"
29#include <VBox/vm.h>
30#include <iprt/assert.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 VM handle.
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)((char*)CTXSUFF(pVM->mm.s.pHyperHeap) + pVM->mm.s.offLookupHyper);
47 for (;;)
48 {
49 switch (pLookup->enmType)
50 {
51 case MMLOOKUPHYPERTYPE_LOCKED:
52 {
53 const uint32_t off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.Locked.pvHC;
54 if (off < pLookup->cb)
55 {
56 *poff = off;
57 return pLookup;
58 }
59 break;
60 }
61
62 case MMLOOKUPHYPERTYPE_HCPHYS:
63 {
64 const uint32_t off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.HCPhys.pvHC;
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_DYNAMIC:
75 break;
76
77 default:
78 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
79 break;
80 }
81
82 /* next */
83 if (pLookup->offNext == (int32_t)NIL_OFFSET)
84 break;
85 pLookup = (PMMLOOKUPHYPER)((char *)pLookup + pLookup->offNext);
86 }
87
88 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
89 return NULL;
90}
91
92
93/**
94 * Lookup a host context ring-0 address.
95 *
96 * @returns Pointer to the corresponding lookup record.
97 * @returns NULL on failure.
98 * @param pVM The VM handle.
99 * @param R0Ptr The host context ring-0 address to lookup.
100 * @param poff Where to store the offset into the HMA memory chunk.
101 */
102DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupR0(PVM pVM, RTR0PTR R0Ptr, uint32_t *poff)
103{
104 AssertCompile(sizeof(RTR0PTR) == sizeof(RTR3PTR));
105 return mmHyperLookupR3(pVM, (RTR3PTR)R0Ptr, poff);
106}
107
108
109/**
110 * Lookup a guest context address.
111 *
112 * @returns Pointer to the corresponding lookup record.
113 * @returns NULL on failure.
114 * @param pVM The VM handle.
115 * @param GCPtr The guest context address to lookup.
116 * @param poff Where to store the offset into the HMA memory chunk.
117 */
118DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupGC(PVM pVM, RTGCPTR GCPtr, uint32_t *poff)
119{
120 /** @todo cache last lookup this stuff ain't cheap! */
121 unsigned offGC = (RTGCUINTPTR)GCPtr - (RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC;
122 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((char*)CTXSUFF(pVM->mm.s.pHyperHeap) + pVM->mm.s.offLookupHyper);
123 for (;;)
124 {
125 const uint32_t off = offGC - pLookup->off;
126 if (off < pLookup->cb)
127 {
128 switch (pLookup->enmType)
129 {
130 case MMLOOKUPHYPERTYPE_LOCKED:
131 case MMLOOKUPHYPERTYPE_HCPHYS:
132 *poff = off;
133 return pLookup;
134 default:
135 break;
136 }
137 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
138 return NULL;
139 }
140
141 /* next */
142 if (pLookup->offNext == (int32_t)NIL_OFFSET)
143 break;
144 pLookup = (PMMLOOKUPHYPER)((char *)pLookup + pLookup->offNext);
145 }
146
147 AssertMsgFailed(("GCPtr=%p is not inside the hypervisor memory area!\n", GCPtr));
148 return NULL;
149}
150
151
152/**
153 * Lookup a current context address.
154 *
155 * @returns Pointer to the corresponding lookup record.
156 * @returns NULL on failure.
157 * @param pVM The VM handle.
158 * @param pv The current context address to lookup.
159 * @param poff Where to store the offset into the HMA memory chunk.
160 */
161DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupCC(PVM pVM, void *pv, uint32_t *poff)
162{
163#ifdef IN_GC
164 return mmHyperLookupGC(pVM, pv, poff);
165#elif defined(IN_RING0)
166 return mmHyperLookupR0(pVM, pv, poff);
167#else
168 return mmHyperLookupR3(pVM, pv, poff);
169#endif
170}
171
172
173/**
174 * Calculate the host context ring-3 address of an offset into the HMA memory chunk.
175 *
176 * @returns the host context ring-3 address.
177 * @param pLookup The HMA lookup record.
178 * @param off The offset into the HMA memory chunk.
179 */
180DECLINLINE(RTR3PTR) mmHyperLookupCalcR3(PMMLOOKUPHYPER pLookup, uint32_t off)
181{
182 switch (pLookup->enmType)
183 {
184 case MMLOOKUPHYPERTYPE_LOCKED:
185 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.Locked.pvHC + off);
186 case MMLOOKUPHYPERTYPE_HCPHYS:
187 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.HCPhys.pvHC + off);
188 default:
189 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
190 return NIL_RTR3PTR;
191 }
192}
193
194
195/**
196 * Calculate the host context ring-0 address of an offset into the HMA memory chunk.
197 *
198 * @returns the host context ring-0 address.
199 * @param pLookup The HMA lookup record.
200 * @param off The offset into the HMA memory chunk.
201 */
202DECLINLINE(RTR0PTR) mmHyperLookupCalcR0(PMMLOOKUPHYPER pLookup, uint32_t off)
203{
204 return (RTR0PTR)mmHyperLookupCalcR3(pLookup, off);
205}
206
207
208/**
209 * Calculate the guest context address of an offset into the HMA memory chunk.
210 *
211 * @returns the guest context base address.
212 * @param pVM The the VM handle.
213 * @param pLookup The HMA lookup record.
214 * @param off The offset into the HMA memory chunk.
215 */
216DECLINLINE(RTGCPTR) mmHyperLookupCalcGC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
217{
218 return (RTGCPTR)((RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC + pLookup->off + off);
219}
220
221
222/**
223 * Calculate the guest context address of an offset into the HMA memory chunk.
224 *
225 * @returns the guest context base address.
226 * @param pVM The the VM handle.
227 * @param pLookup The HMA lookup record.
228 * @param off The offset into the HMA memory chunk.
229 */
230DECLINLINE(void *) mmHyperLookupCalcCC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
231{
232#ifdef IN_GC
233 return mmHyperLookupCalcGC(pVM, pLookup, off);
234#elif defined(IN_RING0)
235 return mmHyperLookupCalcR0(pLookup, off);
236#else
237 return mmHyperLookupCalcR3(pLookup, off);
238#endif
239}
240
241
242/**
243 * Converts a ring-0 host context address in the Hypervisor memory region to a ring-3 host context address.
244 *
245 * @returns ring-3 host context address.
246 * @param pVM The VM to operate on.
247 * @param R0Ptr The ring-0 host context address.
248 * You'll be damned if this is not in the HMA! :-)
249 * @thread The Emulation Thread.
250 */
251MMDECL(RTR3PTR) MMHyperR0ToR3(PVM pVM, RTR0PTR R0Ptr)
252{
253 uint32_t off;
254 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
255 if (pLookup)
256 return mmHyperLookupCalcR3(pLookup, off);
257 return NIL_RTR3PTR;
258}
259
260
261/**
262 * Converts a ring-0 host context address in the Hypervisor memory region to a guest context address.
263 *
264 * @returns guest context address.
265 * @param pVM The VM to operate on.
266 * @param R0Ptr The ring-0 host context address.
267 * You'll be damned if this is not in the HMA! :-)
268 * @thread The Emulation Thread.
269 */
270MMDECL(RTGCPTR) MMHyperR0ToGC(PVM pVM, RTR0PTR R0Ptr)
271{
272 uint32_t off;
273 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
274 if (pLookup)
275 return mmHyperLookupCalcGC(pVM, pLookup, off);
276 return NIL_RTGCPTR;
277}
278
279
280/**
281 * Converts a ring-0 host context address in the Hypervisor memory region to a current context address.
282 *
283 * @returns current context address.
284 * @param pVM The VM to operate on.
285 * @param R0Ptr The ring-0 host context address.
286 * You'll be damned if this is not in the HMA! :-)
287 * @thread The Emulation Thread.
288 */
289#ifndef IN_RING0
290MMDECL(void *) MMHyperR0ToCC(PVM pVM, RTR0PTR R0Ptr)
291{
292 uint32_t off;
293 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
294 if (pLookup)
295 return mmHyperLookupCalcCC(pVM, pLookup, off);
296 return NULL;
297}
298#endif
299
300
301/**
302 * Converts a ring-3 host context address in the Hypervisor memory region to a ring-0 host context address.
303 *
304 * @returns ring-0 host context address.
305 * @param pVM The VM to operate on.
306 * @param R3Ptr The ring-3 host context address.
307 * You'll be damned if this is not in the HMA! :-)
308 * @thread The Emulation Thread.
309 */
310MMDECL(RTR0PTR) MMHyperR3ToR0(PVM pVM, RTR3PTR R3Ptr)
311{
312 uint32_t off;
313 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
314 if (pLookup)
315 return mmHyperLookupCalcR0(pLookup, off);
316 return NIL_RTR0PTR;
317}
318
319
320/**
321 * Converts a ring-3 host context address in the Hypervisor memory region to a guest context address.
322 *
323 * @returns guest context address.
324 * @param pVM The VM to operate on.
325 * @param R3Ptr The ring-3 host context address.
326 * You'll be damned if this is not in the HMA! :-)
327 * @thread The Emulation Thread.
328 */
329MMDECL(RTGCPTR) MMHyperR3ToGC(PVM pVM, RTR3PTR R3Ptr)
330{
331 uint32_t off;
332 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
333 if (pLookup)
334 return mmHyperLookupCalcGC(pVM, pLookup, off);
335 return NIL_RTGCPTR;
336}
337
338
339/**
340 * Converts a ring-3 host context address in the Hypervisor memory region to a current context address.
341 *
342 * @returns current context address.
343 * @param pVM The VM to operate on.
344 * @param R3Ptr The ring-3 host context address.
345 * You'll be damned if this is not in the HMA! :-)
346 * @thread The Emulation Thread.
347 */
348#ifndef IN_RING3
349MMDECL(void *) MMHyperR3ToCC(PVM pVM, RTR3PTR R3Ptr)
350{
351 uint32_t off;
352 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
353 if (pLookup)
354 return mmHyperLookupCalcCC(pVM, pLookup, off);
355 return NULL;
356}
357#endif
358
359
360/**
361 * Converts a guest context address in the Hypervisor memory region to a ring-3 context address.
362 *
363 * @returns ring-3 host context address.
364 * @param pVM The VM to operate on.
365 * @param GCPtr The guest context address.
366 * You'll be damned if this is not in the HMA! :-)
367 * @thread The Emulation Thread.
368 */
369MMDECL(RTR3PTR) MMHyperGCToR3(PVM pVM, RTGCPTR GCPtr)
370{
371 uint32_t off;
372 PMMLOOKUPHYPER pLookup = mmHyperLookupGC(pVM, GCPtr, &off);
373 if (pLookup)
374 return mmHyperLookupCalcR3(pLookup, off);
375 return NIL_RTR3PTR;
376}
377
378
379/**
380 * Converts a guest context address in the Hypervisor memory region to a ring-0 host context address.
381 *
382 * @returns ring-0 host context address.
383 * @param pVM The VM to operate on.
384 * @param GCPtr The guest context address.
385 * You'll be damned if this is not in the HMA! :-)
386 * @thread The Emulation Thread.
387 */
388MMDECL(RTR0PTR) MMHyperGCToR0(PVM pVM, RTGCPTR GCPtr)
389{
390 uint32_t off;
391 PMMLOOKUPHYPER pLookup = mmHyperLookupGC(pVM, GCPtr, &off);
392 if (pLookup)
393 return mmHyperLookupCalcR0(pLookup, off);
394 return NIL_RTR0PTR;
395}
396
397
398/**
399 * Converts a guest context address in the Hypervisor memory region to a current context address.
400 *
401 * @returns current context address.
402 * @param pVM The VM to operate on.
403 * @param GCPtr The guest host context address.
404 * You'll be damned if this is not in the HMA! :-)
405 * @thread The Emulation Thread.
406 */
407#ifndef IN_GC
408MMDECL(void *) MMHyperGCToCC(PVM pVM, RTGCPTR GCPtr)
409{
410 uint32_t off;
411 PMMLOOKUPHYPER pLookup = mmHyperLookupGC(pVM, GCPtr, &off);
412 if (pLookup)
413 return mmHyperLookupCalcCC(pVM, pLookup, off);
414 return NULL;
415}
416#endif
417
418
419
420/**
421 * Converts a current context address in the Hypervisor memory region to a ring-3 host context address.
422 *
423 * @returns ring-3 host context address.
424 * @param pVM The VM to operate on.
425 * @param pv The current context address.
426 * You'll be damned if this is not in the HMA! :-)
427 * @thread The Emulation Thread.
428 */
429#ifndef IN_RING3
430MMDECL(RTR3PTR) MMHyperCCToR3(PVM pVM, void *pv)
431{
432 uint32_t off;
433 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
434 if (pLookup)
435 return mmHyperLookupCalcR3(pLookup, off);
436 return NIL_RTR3PTR;
437}
438#endif
439
440/**
441 * Converts a current context address in the Hypervisor memory region to a ring-0 host context address.
442 *
443 * @returns ring-0 host context address.
444 * @param pVM The VM to operate on.
445 * @param pv The current context address.
446 * You'll be damned if this is not in the HMA! :-)
447 * @thread The Emulation Thread.
448 */
449#ifndef IN_RING0
450MMDECL(RTR0PTR) MMHyperCCToR0(PVM pVM, void *pv)
451{
452 uint32_t off;
453 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
454 if (pLookup)
455 return mmHyperLookupCalcR0(pLookup, off);
456 return NIL_RTR0PTR;
457}
458#endif
459
460
461/**
462 * Converts a current context address in the Hypervisor memory region to a guest context address.
463 *
464 * @returns guest context address.
465 * @param pVM The VM to operate on.
466 * @param pv The current context address.
467 * You'll be damned if this is not in the HMA! :-)
468 * @thread The Emulation Thread.
469 */
470#ifndef IN_GC
471MMDECL(RTGCPTR) MMHyperCCToGC(PVM pVM, void *pv)
472{
473 uint32_t off;
474 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
475 if (pLookup)
476 return mmHyperLookupCalcGC(pVM, pLookup, off);
477 return NIL_RTGCPTR;
478}
479#endif
480
481
482
483/**
484 * Converts a HC address in the Hypervisor memory region to a GC address.
485 * The memory must have been allocated with MMGCHyperAlloc() or MMR3HyperAlloc().
486 *
487 * @returns GC address.
488 * @param pVM The VM to operate on.
489 * @param HCPtr The host context address.
490 * You'll be damed if this is not in the hypervisor region! :-)
491 * @deprecated
492 */
493MMDECL(RTGCPTR) MMHyperHC2GC(PVM pVM, RTHCPTR HCPtr)
494{
495 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((char*)CTXSUFF(pVM->mm.s.pHyperHeap) + pVM->mm.s.offLookupHyper);
496 for (;;)
497 {
498 switch (pLookup->enmType)
499 {
500 case MMLOOKUPHYPERTYPE_LOCKED:
501 {
502 unsigned off = (RTHCUINTPTR)HCPtr - (RTHCUINTPTR)pLookup->u.Locked.pvHC;
503 if (off < pLookup->cb)
504 return (RTGCPTR)((RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC + pLookup->off + off);
505 break;
506 }
507
508 case MMLOOKUPHYPERTYPE_HCPHYS:
509 {
510 unsigned off = (RTHCUINTPTR)HCPtr - (RTHCUINTPTR)pLookup->u.HCPhys.pvHC;
511 if (off < pLookup->cb)
512 return (RTGCPTR)((RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC + pLookup->off + off);
513 break;
514 }
515
516 case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
517 case MMLOOKUPHYPERTYPE_DYNAMIC:
518 break;
519
520 default:
521 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
522 break;
523 }
524
525 /* next */
526 if ((unsigned)pLookup->offNext == NIL_OFFSET)
527 break;
528 pLookup = (PMMLOOKUPHYPER)((char *)pLookup + pLookup->offNext);
529 }
530
531 AssertMsgFailed(("HCPtr=%p is not inside the hypervisor memory area!\n", HCPtr));
532 return (RTGCPTR)0;
533}
534
535
536/**
537 * Converts a GC address in the Hypervisor memory region to a HC address.
538 * The memory must have been allocated with MMHyperAlloc().
539 *
540 * @returns HC address.
541 * @param pVM The VM to operate on.
542 * @param GCPtr The guest context address.
543 * You'll be damed if this is not in the hypervisor region! :-)
544 * @deprecated
545 */
546MMDECL(RTHCPTR) MMHyperGC2HC(PVM pVM, RTGCPTR GCPtr)
547{
548 unsigned offGC = (RTGCUINTPTR)GCPtr - (RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC;
549 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((char*)CTXSUFF(pVM->mm.s.pHyperHeap) + pVM->mm.s.offLookupHyper);
550 for (;;)
551 {
552 unsigned off = offGC - pLookup->off;
553 if (off < pLookup->cb)
554 {
555 switch (pLookup->enmType)
556 {
557 case MMLOOKUPHYPERTYPE_LOCKED:
558 return (RTHCPTR)((RTHCUINTPTR)pLookup->u.Locked.pvHC + off);
559 case MMLOOKUPHYPERTYPE_HCPHYS:
560 return (RTHCPTR)((RTHCUINTPTR)pLookup->u.HCPhys.pvHC + off);
561 default:
562 break;
563 }
564 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
565 return (RTHCPTR)0;
566 }
567
568 /* next */
569 if ((unsigned)pLookup->offNext == NIL_OFFSET)
570 break;
571 pLookup = (PMMLOOKUPHYPER)((char *)pLookup + pLookup->offNext);
572 }
573
574 AssertMsgFailed(("GCPtr=%p is not inside the hypervisor memory area!\n", GCPtr));
575 return (RTHCPTR)0;
576}
577
578
579#ifdef IN_GC
580/**
581 * Converts a current context address in the Hypervisor memory region to a HC address.
582 * The memory must have been allocated with MMGCHyperAlloc() or MMR3HyperAlloc().
583 *
584 * @returns HC address.
585 * @param pVM The VM to operate on.
586 * @param Ptr The current context address.
587 * @deprecated
588 */
589MMDECL(RTHCPTR) MMHyper2HC(PVM pVM, uintptr_t Ptr)
590{
591 return MMHyperGC2HC(pVM, (RTGCPTR)Ptr);
592}
593
594#else /* !IN_GC */
595
596/**
597 * Converts a current context address in the Hypervisor memory region to a GC address.
598 * The memory must have been allocated with MMHyperAlloc().
599 *
600 * @returns HC address.
601 * @param pVM The VM to operate on.
602 * @param Ptr The current context address.
603 * @thread The Emulation Thread.
604 * @deprecated
605 */
606MMDECL(RTGCPTR) MMHyper2GC(PVM pVM, uintptr_t Ptr)
607{
608 return MMHyperHC2GC(pVM, (RTHCPTR)Ptr);
609}
610#endif /* !IN_GC */
611
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