VirtualBox

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

Last change on this file since 14599 was 14599, checked in by vboxsync, 17 years ago

MMHyperR0ToY: Lookup on the actual R0 address instead of hacked R3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 17.0 KB
Line 
1/* $Id: MMAll.cpp 14599 2008-11-25 20:48:16Z vboxsync $ */
2/** @file
3 * MM - Memory Manager - Any Context.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
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 <VBox/log.h>
31#include <iprt/assert.h>
32
33
34
35/**
36 * Lookup a host context ring-3 address.
37 *
38 * @returns Pointer to the corresponding lookup record.
39 * @returns NULL on failure.
40 * @param pVM The VM handle.
41 * @param R3Ptr The host context ring-3 address to lookup.
42 * @param poff Where to store the offset into the HMA memory chunk.
43 */
44DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupR3(PVM pVM, RTR3PTR R3Ptr, uint32_t *poff)
45{
46 /** @todo cache last lookup, this stuff ain't cheap! */
47 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
48 for (;;)
49 {
50 switch (pLookup->enmType)
51 {
52 case MMLOOKUPHYPERTYPE_LOCKED:
53 {
54 const uint32_t off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.Locked.pvR3;
55 if (off < pLookup->cb)
56 {
57 *poff = off;
58 return pLookup;
59 }
60 break;
61 }
62
63 case MMLOOKUPHYPERTYPE_HCPHYS:
64 {
65 const uint32_t off = (RTR3UINTPTR)R3Ptr - (RTR3UINTPTR)pLookup->u.HCPhys.pvR3;
66 if (off < pLookup->cb)
67 {
68 *poff = off;
69 return pLookup;
70 }
71 break;
72 }
73
74 case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
75 case MMLOOKUPHYPERTYPE_MMIO2:
76 case MMLOOKUPHYPERTYPE_DYNAMIC:
77 break;
78
79 default:
80 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
81 break;
82 }
83
84 /* next */
85 if (pLookup->offNext == (int32_t)NIL_OFFSET)
86 break;
87 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
88 }
89
90 AssertMsgFailed(("R3Ptr=%RHv is not inside the hypervisor memory area!\n", R3Ptr));
91 return NULL;
92}
93
94
95/**
96 * Lookup a host context ring-0 address.
97 *
98 * @returns Pointer to the corresponding lookup record.
99 * @returns NULL on failure.
100 * @param pVM The VM handle.
101 * @param R0Ptr The host context ring-0 address to lookup.
102 * @param poff Where to store the offset into the HMA memory chunk.
103 */
104DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupR0(PVM pVM, RTR0PTR R0Ptr, uint32_t *poff)
105{
106 AssertCompile(sizeof(RTR0PTR) == sizeof(RTR3PTR));
107
108 /** @todo cache last lookup, this stuff ain't cheap! */
109 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
110 for (;;)
111 {
112 switch (pLookup->enmType)
113 {
114 case MMLOOKUPHYPERTYPE_LOCKED:
115 {
116 const uint32_t off = (RTR3UINTPTR)R0Ptr - (RTR0UINTPTR)pLookup->u.Locked.pvR0;
117 if (off < pLookup->cb && pLookup->u.Locked.pvR0)
118 {
119 *poff = off;
120 return pLookup;
121 }
122 break;
123 }
124
125 case MMLOOKUPHYPERTYPE_HCPHYS:
126 {
127 const uint32_t off = (RTR0UINTPTR)R0Ptr - (RTR0UINTPTR)pLookup->u.HCPhys.pvR0;
128 if (off < pLookup->cb && pLookup->u.HCPhys.pvR0)
129 {
130 *poff = off;
131 return pLookup;
132 }
133 break;
134 }
135
136 case MMLOOKUPHYPERTYPE_GCPHYS: /* (for now we'll not allow these kind of conversions) */
137 case MMLOOKUPHYPERTYPE_MMIO2:
138 case MMLOOKUPHYPERTYPE_DYNAMIC:
139 break;
140
141 default:
142 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
143 break;
144 }
145
146 /* next */
147 if (pLookup->offNext == (int32_t)NIL_OFFSET)
148 break;
149 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
150 }
151
152 AssertMsgFailed(("R0Ptr=%RHv is not inside the hypervisor memory area!\n", R0Ptr));
153 return NULL;
154}
155
156
157/**
158 * Lookup a raw-mode context address.
159 *
160 * @returns Pointer to the corresponding lookup record.
161 * @returns NULL on failure.
162 * @param pVM The VM handle.
163 * @param RCPtr The raw-mode context address to lookup.
164 * @param poff Where to store the offset into the HMA memory chunk.
165 */
166DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupRC(PVM pVM, RTRCPTR RCPtr, uint32_t *poff)
167{
168 /** @todo cache last lookup this stuff ain't cheap! */
169 unsigned offRC = (RTRCUINTPTR)RCPtr - (RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC;
170 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
171 for (;;)
172 {
173 const uint32_t off = offRC - pLookup->off;
174 if (off < pLookup->cb)
175 {
176 switch (pLookup->enmType)
177 {
178 case MMLOOKUPHYPERTYPE_LOCKED:
179 case MMLOOKUPHYPERTYPE_HCPHYS:
180 *poff = off;
181 return pLookup;
182 default:
183 break;
184 }
185 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
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 return NULL;
197}
198
199
200/**
201 * Lookup a current context address.
202 *
203 * @returns Pointer to the corresponding lookup record.
204 * @returns NULL on failure.
205 * @param pVM The VM handle.
206 * @param pv The current context address to lookup.
207 * @param poff Where to store the offset into the HMA memory chunk.
208 */
209DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupCC(PVM pVM, void *pv, uint32_t *poff)
210{
211#ifdef IN_RC
212 return mmHyperLookupRC(pVM, (RTRCPTR)pv, poff);
213#elif defined(IN_RING0)
214 return mmHyperLookupR0(pVM, pv, poff);
215#else
216 return mmHyperLookupR3(pVM, pv, poff);
217#endif
218}
219
220
221/**
222 * Calculate the host context ring-3 address of an offset into the HMA memory chunk.
223 *
224 * @returns the host context ring-3 address.
225 * @param pLookup The HMA lookup record.
226 * @param off The offset into the HMA memory chunk.
227 */
228DECLINLINE(RTR3PTR) mmHyperLookupCalcR3(PMMLOOKUPHYPER pLookup, uint32_t off)
229{
230 switch (pLookup->enmType)
231 {
232 case MMLOOKUPHYPERTYPE_LOCKED:
233 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.Locked.pvR3 + off);
234 case MMLOOKUPHYPERTYPE_HCPHYS:
235 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.HCPhys.pvR3 + off);
236 default:
237 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
238 return NIL_RTR3PTR;
239 }
240}
241
242
243/**
244 * Calculate the host context ring-0 address of an offset into the HMA memory chunk.
245 *
246 * @returns the host context ring-0 address.
247 * @param pLookup The HMA lookup record.
248 * @param off The offset into the HMA memory chunk.
249 */
250DECLINLINE(RTR0PTR) mmHyperLookupCalcR0(PMMLOOKUPHYPER pLookup, uint32_t off)
251{
252 switch (pLookup->enmType)
253 {
254 case MMLOOKUPHYPERTYPE_LOCKED:
255 if (pLookup->u.Locked.pvR0)
256 return (RTR0PTR)((RTR0UINTPTR)pLookup->u.Locked.pvR0 + off);
257#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE /** @todo make NIL_RTR0PTR default! */
258 return NIL_RTR0PTR;
259#else
260 return (RTR0PTR)((RTR3UINTPTR)pLookup->u.Locked.pvR3 + off);
261#endif
262
263 case MMLOOKUPHYPERTYPE_HCPHYS:
264 if (pLookup->u.HCPhys.pvR0)
265 return (RTR0PTR)((RTR0UINTPTR)pLookup->u.HCPhys.pvR0 + off);
266 return NIL_RTR0PTR;
267 default:
268 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
269 return NIL_RTR0PTR;
270 }
271}
272
273
274/**
275 * Calculate the raw-mode context address of an offset into the HMA memory chunk.
276 *
277 * @returns the raw-mode context base address.
278 * @param pVM The the VM handle.
279 * @param pLookup The HMA lookup record.
280 * @param off The offset into the HMA memory chunk.
281 */
282DECLINLINE(RTRCPTR) mmHyperLookupCalcRC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
283{
284 return (RTRCPTR)((RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC + pLookup->off + off);
285}
286
287
288/**
289 * Calculate the guest context address of an offset into the HMA memory chunk.
290 *
291 * @returns the guest context base address.
292 * @param pVM The the VM handle.
293 * @param pLookup The HMA lookup record.
294 * @param off The offset into the HMA memory chunk.
295 */
296DECLINLINE(void *) mmHyperLookupCalcCC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
297{
298#ifdef IN_RC
299 return (void *)mmHyperLookupCalcRC(pVM, pLookup, off);
300#elif defined(IN_RING0)
301 return mmHyperLookupCalcR0(pLookup, off);
302#else
303 return mmHyperLookupCalcR3(pLookup, off);
304#endif
305}
306
307
308/**
309 * Converts a ring-0 host context address in the Hypervisor memory region to a ring-3 host context address.
310 *
311 * @returns ring-3 host context address.
312 * @param pVM The VM to operate on.
313 * @param R0Ptr The ring-0 host context address.
314 * You'll be damned if this is not in the HMA! :-)
315 * @thread The Emulation Thread.
316 */
317VMMDECL(RTR3PTR) MMHyperR0ToR3(PVM pVM, RTR0PTR R0Ptr)
318{
319 uint32_t off;
320 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
321 if (pLookup)
322 return mmHyperLookupCalcR3(pLookup, off);
323 return NIL_RTR3PTR;
324}
325
326
327/**
328 * Converts a ring-0 host context address in the Hypervisor memory region to a raw-mode context address.
329 *
330 * @returns raw-mode context address.
331 * @param pVM The VM to operate on.
332 * @param R0Ptr The ring-0 host context address.
333 * You'll be damned if this is not in the HMA! :-)
334 * @thread The Emulation Thread.
335 */
336VMMDECL(RTRCPTR) MMHyperR0ToRC(PVM pVM, RTR0PTR R0Ptr)
337{
338 uint32_t off;
339 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
340 if (pLookup)
341 return mmHyperLookupCalcRC(pVM, pLookup, off);
342 return NIL_RTRCPTR;
343}
344
345
346#ifndef IN_RING0
347/**
348 * Converts a ring-0 host context address in the Hypervisor memory region to a current context address.
349 *
350 * @returns current context address.
351 * @param pVM The VM to operate on.
352 * @param R0Ptr The ring-0 host context address.
353 * You'll be damned if this is not in the HMA! :-)
354 * @thread The Emulation Thread.
355 */
356VMMDECL(void *) MMHyperR0ToCC(PVM pVM, RTR0PTR R0Ptr)
357{
358 uint32_t off;
359 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
360 if (pLookup)
361 return mmHyperLookupCalcCC(pVM, pLookup, off);
362 return NULL;
363}
364#endif
365
366
367/**
368 * Converts a ring-3 host context address in the Hypervisor memory region to a ring-0 host context address.
369 *
370 * @returns ring-0 host context address.
371 * @param pVM The VM to operate on.
372 * @param R3Ptr The ring-3 host context address.
373 * You'll be damned if this is not in the HMA! :-)
374 * @thread The Emulation Thread.
375 */
376VMMDECL(RTR0PTR) MMHyperR3ToR0(PVM pVM, RTR3PTR R3Ptr)
377{
378 uint32_t off;
379 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
380 if (pLookup)
381 return mmHyperLookupCalcR0(pLookup, off);
382 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
383 return NIL_RTR0PTR;
384}
385
386
387/**
388 * Converts a ring-3 host context address in the Hypervisor memory region to a guest context address.
389 *
390 * @returns guest context address.
391 * @param pVM The VM to operate on.
392 * @param R3Ptr The ring-3 host context address.
393 * You'll be damned if this is not in the HMA! :-)
394 * @thread The Emulation Thread.
395 */
396VMMDECL(RTRCPTR) MMHyperR3ToRC(PVM pVM, RTR3PTR R3Ptr)
397{
398 uint32_t off;
399 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
400 if (pLookup)
401 return mmHyperLookupCalcRC(pVM, pLookup, off);
402 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
403 return NIL_RTRCPTR;
404}
405
406
407/**
408 * Converts a ring-3 host context address in the Hypervisor memory region to a current context address.
409 *
410 * @returns current context address.
411 * @param pVM The VM to operate on.
412 * @param R3Ptr The ring-3 host context address.
413 * You'll be damned if this is not in the HMA! :-)
414 * @thread The Emulation Thread.
415 */
416#ifndef IN_RING3
417VMMDECL(void *) MMHyperR3ToCC(PVM pVM, RTR3PTR R3Ptr)
418{
419 uint32_t off;
420 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
421 if (pLookup)
422 return mmHyperLookupCalcCC(pVM, pLookup, off);
423 return NULL;
424}
425#endif
426
427
428/**
429 * Converts a raw-mode context address in the Hypervisor memory region to a ring-3 context address.
430 *
431 * @returns ring-3 host context address.
432 * @param pVM The VM to operate on.
433 * @param GCPtr The raw-mode context address.
434 * You'll be damned if this is not in the HMA! :-)
435 * @thread The Emulation Thread.
436 */
437VMMDECL(RTR3PTR) MMHyperRCToR3(PVM pVM, RTRCPTR RCPtr)
438{
439 uint32_t off;
440 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
441 if (pLookup)
442 return mmHyperLookupCalcR3(pLookup, off);
443 return NIL_RTR3PTR;
444}
445
446
447/**
448 * Converts a raw-mode context address in the Hypervisor memory region to a ring-0 host context address.
449 *
450 * @returns ring-0 host context address.
451 * @param pVM The VM to operate on.
452 * @param RCPtr The raw-mode context address.
453 * You'll be damned if this is not in the HMA! :-)
454 * @thread The Emulation Thread.
455 */
456VMMDECL(RTR0PTR) MMHyperRCToR0(PVM pVM, RTRCPTR RCPtr)
457{
458 uint32_t off;
459 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
460 if (pLookup)
461 return mmHyperLookupCalcR0(pLookup, off);
462 return NIL_RTR0PTR;
463}
464
465
466/**
467 * Converts a raw-mode context address in the Hypervisor memory region to a current context address.
468 *
469 * @returns current context address.
470 * @param pVM The VM to operate on.
471 * @param RCPtr The raw-mode host context address.
472 * You'll be damned if this is not in the HMA! :-)
473 * @thread The Emulation Thread.
474 */
475#ifndef IN_RC
476VMMDECL(void *) MMHyperRCToCC(PVM pVM, RTRCPTR RCPtr)
477{
478 uint32_t off;
479 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
480 if (pLookup)
481 return mmHyperLookupCalcCC(pVM, pLookup, off);
482 return NULL;
483}
484#endif
485
486
487
488/**
489 * Converts a current context address in the Hypervisor memory region to a ring-3 host context address.
490 *
491 * @returns ring-3 host context address.
492 * @param pVM The VM to operate on.
493 * @param pv The current context address.
494 * You'll be damned if this is not in the HMA! :-)
495 * @thread The Emulation Thread.
496 */
497#ifndef IN_RING3
498VMMDECL(RTR3PTR) MMHyperCCToR3(PVM pVM, void *pv)
499{
500 uint32_t off;
501 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
502 if (pLookup)
503 return mmHyperLookupCalcR3(pLookup, off);
504 return NIL_RTR3PTR;
505}
506#endif
507
508/**
509 * Converts a current context address in the Hypervisor memory region to a ring-0 host context address.
510 *
511 * @returns ring-0 host context address.
512 * @param pVM The VM to operate on.
513 * @param pv The current context address.
514 * You'll be damned if this is not in the HMA! :-)
515 * @thread The Emulation Thread.
516 */
517#ifndef IN_RING0
518VMMDECL(RTR0PTR) MMHyperCCToR0(PVM pVM, void *pv)
519{
520 uint32_t off;
521 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
522 if (pLookup)
523 return mmHyperLookupCalcR0(pLookup, off);
524 return NIL_RTR0PTR;
525}
526#endif
527
528
529/**
530 * Converts a current context address in the Hypervisor memory region to a raw-mode context address.
531 *
532 * @returns guest context address.
533 * @param pVM The VM to operate on.
534 * @param pv The current context address.
535 * You'll be damned if this is not in the HMA! :-)
536 * @thread The Emulation Thread.
537 */
538#ifndef IN_RC
539VMMDECL(RTRCPTR) MMHyperCCToRC(PVM pVM, void *pv)
540{
541 uint32_t off;
542 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
543 if (pLookup)
544 return mmHyperLookupCalcRC(pVM, pLookup, off);
545 return NIL_RTRCPTR;
546}
547#endif
548
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