VirtualBox

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

Last change on this file since 13813 was 12989, checked in by vboxsync, 16 years ago

VMM + VBox/cdefs.h: consolidated all the XYZ*DECLS of the VMM into VMM*DECL. Removed dead DECL and IN_XYZ* macros.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.8 KB
Line 
1/* $Id: MMAll.cpp 12989 2008-10-06 02:15:39Z 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.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 * 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=%p 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 /*
109 * Translate Ring-0 VM addresses into Ring-3 VM addresses before feeding it to mmHyperLookupR3.
110 */
111 /** @todo fix this properly; the ring 0 pVM address differs from the R3 one. (#1865) */
112 RTR0UINTPTR offVM = (RTR0UINTPTR)R0Ptr - (RTR0UINTPTR)pVM->pVMR0;
113 RTR3PTR R3Ptr = offVM < sizeof(*pVM)
114 ? (RTR3PTR)((RTR3UINTPTR)pVM->pVMR3 + offVM)
115 : (RTR3PTR)R0Ptr;
116
117 return mmHyperLookupR3(pVM, R3Ptr, poff);
118}
119
120
121/**
122 * Lookup a raw-mode context address.
123 *
124 * @returns Pointer to the corresponding lookup record.
125 * @returns NULL on failure.
126 * @param pVM The VM handle.
127 * @param RCPtr The raw-mode context address to lookup.
128 * @param poff Where to store the offset into the HMA memory chunk.
129 */
130DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupRC(PVM pVM, RTRCPTR RCPtr, uint32_t *poff)
131{
132 /** @todo cache last lookup this stuff ain't cheap! */
133 unsigned offRC = (RTRCUINTPTR)RCPtr - (RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC;
134 PMMLOOKUPHYPER pLookup = (PMMLOOKUPHYPER)((uint8_t *)pVM->mm.s.CTX_SUFF(pHyperHeap) + pVM->mm.s.offLookupHyper);
135 for (;;)
136 {
137 const uint32_t off = offRC - pLookup->off;
138 if (off < pLookup->cb)
139 {
140 switch (pLookup->enmType)
141 {
142 case MMLOOKUPHYPERTYPE_LOCKED:
143 case MMLOOKUPHYPERTYPE_HCPHYS:
144 *poff = off;
145 return pLookup;
146 default:
147 break;
148 }
149 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
150 return NULL;
151 }
152
153 /* next */
154 if (pLookup->offNext == (int32_t)NIL_OFFSET)
155 break;
156 pLookup = (PMMLOOKUPHYPER)((uint8_t *)pLookup + pLookup->offNext);
157 }
158
159 AssertMsgFailed(("GCPtr=%p is not inside the hypervisor memory area!\n", RCPtr));
160 return NULL;
161}
162
163
164/**
165 * Lookup a current context address.
166 *
167 * @returns Pointer to the corresponding lookup record.
168 * @returns NULL on failure.
169 * @param pVM The VM handle.
170 * @param pv The current context address to lookup.
171 * @param poff Where to store the offset into the HMA memory chunk.
172 */
173DECLINLINE(PMMLOOKUPHYPER) mmHyperLookupCC(PVM pVM, void *pv, uint32_t *poff)
174{
175#ifdef IN_GC
176 return mmHyperLookupRC(pVM, (RTRCPTR)pv, poff);
177#elif defined(IN_RING0)
178 return mmHyperLookupR0(pVM, pv, poff);
179#else
180 return mmHyperLookupR3(pVM, pv, poff);
181#endif
182}
183
184
185/**
186 * Calculate the host context ring-3 address of an offset into the HMA memory chunk.
187 *
188 * @returns the host context ring-3 address.
189 * @param pLookup The HMA lookup record.
190 * @param off The offset into the HMA memory chunk.
191 */
192DECLINLINE(RTR3PTR) mmHyperLookupCalcR3(PMMLOOKUPHYPER pLookup, uint32_t off)
193{
194 switch (pLookup->enmType)
195 {
196 case MMLOOKUPHYPERTYPE_LOCKED:
197 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.Locked.pvR3 + off);
198 case MMLOOKUPHYPERTYPE_HCPHYS:
199 return (RTR3PTR)((RTR3UINTPTR)pLookup->u.HCPhys.pvR3 + off);
200 default:
201 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
202 return NIL_RTR3PTR;
203 }
204}
205
206
207/**
208 * Calculate the host context ring-0 address of an offset into the HMA memory chunk.
209 *
210 * @returns the host context ring-0 address.
211 * @param pLookup The HMA lookup record.
212 * @param off The offset into the HMA memory chunk.
213 */
214DECLINLINE(RTR0PTR) mmHyperLookupCalcR0(PMMLOOKUPHYPER pLookup, uint32_t off)
215{
216 switch (pLookup->enmType)
217 {
218 case MMLOOKUPHYPERTYPE_LOCKED:
219 if (pLookup->u.Locked.pvR0)
220 return (RTR0PTR)((RTR0UINTPTR)pLookup->u.Locked.pvR0 + off);
221 /** @todo #1865: accessing ring-3 memory (LOCKED)! */
222 return (RTR0PTR)((RTR3UINTPTR)pLookup->u.Locked.pvR3 + off);
223 case MMLOOKUPHYPERTYPE_HCPHYS:
224 /** @todo #1865: accessing ring-3 memory (HCPHYS)! */
225 return (RTR0PTR)((RTR3UINTPTR)pLookup->u.HCPhys.pvR3 + off);
226 default:
227 AssertMsgFailed(("enmType=%d\n", pLookup->enmType));
228 return NIL_RTR0PTR;
229 }
230}
231
232
233/**
234 * Calculate the raw-mode context address of an offset into the HMA memory chunk.
235 *
236 * @returns the raw-mode context base address.
237 * @param pVM The the VM handle.
238 * @param pLookup The HMA lookup record.
239 * @param off The offset into the HMA memory chunk.
240 */
241DECLINLINE(RTRCPTR) mmHyperLookupCalcRC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
242{
243 return (RTRCPTR)((RTGCUINTPTR)pVM->mm.s.pvHyperAreaGC + pLookup->off + off);
244}
245
246
247/**
248 * Calculate the guest context address of an offset into the HMA memory chunk.
249 *
250 * @returns the guest context base address.
251 * @param pVM The the VM handle.
252 * @param pLookup The HMA lookup record.
253 * @param off The offset into the HMA memory chunk.
254 */
255DECLINLINE(void *) mmHyperLookupCalcCC(PVM pVM, PMMLOOKUPHYPER pLookup, uint32_t off)
256{
257#ifdef IN_GC
258 return (void *)mmHyperLookupCalcRC(pVM, pLookup, off);
259#elif defined(IN_RING0)
260 return mmHyperLookupCalcR0(pLookup, off);
261#else
262 return mmHyperLookupCalcR3(pLookup, off);
263#endif
264}
265
266
267/**
268 * Converts a ring-0 host context address in the Hypervisor memory region to a ring-3 host context address.
269 *
270 * @returns ring-3 host context address.
271 * @param pVM The VM to operate on.
272 * @param R0Ptr The ring-0 host context address.
273 * You'll be damned if this is not in the HMA! :-)
274 * @thread The Emulation Thread.
275 */
276VMMDECL(RTR3PTR) MMHyperR0ToR3(PVM pVM, RTR0PTR R0Ptr)
277{
278 uint32_t off;
279 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
280 if (pLookup)
281 return mmHyperLookupCalcR3(pLookup, off);
282 return NIL_RTR3PTR;
283}
284
285
286/**
287 * Converts a ring-0 host context address in the Hypervisor memory region to a raw-mode context address.
288 *
289 * @returns raw-mode context address.
290 * @param pVM The VM to operate on.
291 * @param R0Ptr The ring-0 host context address.
292 * You'll be damned if this is not in the HMA! :-)
293 * @thread The Emulation Thread.
294 */
295VMMDECL(RTRCPTR) MMHyperR0ToRC(PVM pVM, RTR0PTR R0Ptr)
296{
297 uint32_t off;
298 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
299 if (pLookup)
300 return mmHyperLookupCalcRC(pVM, pLookup, off);
301 return NIL_RTRCPTR;
302}
303
304
305#ifndef IN_RING0
306/**
307 * Converts a ring-0 host context address in the Hypervisor memory region to a current context address.
308 *
309 * @returns current context address.
310 * @param pVM The VM to operate on.
311 * @param R0Ptr The ring-0 host context address.
312 * You'll be damned if this is not in the HMA! :-)
313 * @thread The Emulation Thread.
314 */
315VMMDECL(void *) MMHyperR0ToCC(PVM pVM, RTR0PTR R0Ptr)
316{
317 uint32_t off;
318 PMMLOOKUPHYPER pLookup = mmHyperLookupR0(pVM, R0Ptr, &off);
319 if (pLookup)
320 return mmHyperLookupCalcCC(pVM, pLookup, off);
321 return NULL;
322}
323#endif
324
325
326/**
327 * Converts a ring-3 host context address in the Hypervisor memory region to a ring-0 host context address.
328 *
329 * @returns ring-0 host context address.
330 * @param pVM The VM to operate on.
331 * @param R3Ptr The ring-3 host context address.
332 * You'll be damned if this is not in the HMA! :-)
333 * @thread The Emulation Thread.
334 */
335VMMDECL(RTR0PTR) MMHyperR3ToR0(PVM pVM, RTR3PTR R3Ptr)
336{
337 uint32_t off;
338 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
339 if (pLookup)
340 return mmHyperLookupCalcR0(pLookup, off);
341 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
342 return NIL_RTR0PTR;
343}
344
345
346/**
347 * Converts a ring-3 host context address in the Hypervisor memory region to a guest context address.
348 *
349 * @returns guest context address.
350 * @param pVM The VM to operate on.
351 * @param R3Ptr The ring-3 host context address.
352 * You'll be damned if this is not in the HMA! :-)
353 * @thread The Emulation Thread.
354 */
355VMMDECL(RTRCPTR) MMHyperR3ToRC(PVM pVM, RTR3PTR R3Ptr)
356{
357 uint32_t off;
358 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
359 if (pLookup)
360 return mmHyperLookupCalcRC(pVM, pLookup, off);
361 AssertMsgFailed(("R3Ptr=%p is not inside the hypervisor memory area!\n", R3Ptr));
362 return NIL_RTRCPTR;
363}
364
365
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 VM to operate on.
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 */
375#ifndef IN_RING3
376VMMDECL(void *) MMHyperR3ToCC(PVM pVM, RTR3PTR R3Ptr)
377{
378 uint32_t off;
379 PMMLOOKUPHYPER pLookup = mmHyperLookupR3(pVM, R3Ptr, &off);
380 if (pLookup)
381 return mmHyperLookupCalcCC(pVM, pLookup, off);
382 return NULL;
383}
384#endif
385
386
387/**
388 * Converts a raw-mode context address in the Hypervisor memory region to a ring-3 context address.
389 *
390 * @returns ring-3 host context address.
391 * @param pVM The VM to operate on.
392 * @param GCPtr The raw-mode context address.
393 * You'll be damned if this is not in the HMA! :-)
394 * @thread The Emulation Thread.
395 */
396VMMDECL(RTR3PTR) MMHyperRCToR3(PVM pVM, RTRCPTR RCPtr)
397{
398 uint32_t off;
399 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
400 if (pLookup)
401 return mmHyperLookupCalcR3(pLookup, off);
402 return NIL_RTR3PTR;
403}
404
405
406/**
407 * Converts a raw-mode context address in the Hypervisor memory region to a ring-0 host context address.
408 *
409 * @returns ring-0 host context address.
410 * @param pVM The VM to operate on.
411 * @param RCPtr The raw-mode context address.
412 * You'll be damned if this is not in the HMA! :-)
413 * @thread The Emulation Thread.
414 */
415VMMDECL(RTR0PTR) MMHyperRCToR0(PVM pVM, RTRCPTR RCPtr)
416{
417 uint32_t off;
418 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
419 if (pLookup)
420 return mmHyperLookupCalcR0(pLookup, off);
421 return NIL_RTR0PTR;
422}
423
424
425/**
426 * Converts a raw-mode context address in the Hypervisor memory region to a current context address.
427 *
428 * @returns current context address.
429 * @param pVM The VM to operate on.
430 * @param RCPtr The raw-mode host context address.
431 * You'll be damned if this is not in the HMA! :-)
432 * @thread The Emulation Thread.
433 */
434#ifndef IN_GC
435VMMDECL(void *) MMHyperRCToCC(PVM pVM, RTRCPTR RCPtr)
436{
437 uint32_t off;
438 PMMLOOKUPHYPER pLookup = mmHyperLookupRC(pVM, RCPtr, &off);
439 if (pLookup)
440 return mmHyperLookupCalcCC(pVM, pLookup, off);
441 return NULL;
442}
443#endif
444
445
446
447/**
448 * Converts a current context address in the Hypervisor memory region to a ring-3 host context address.
449 *
450 * @returns ring-3 host context address.
451 * @param pVM The VM to operate on.
452 * @param pv The current context address.
453 * You'll be damned if this is not in the HMA! :-)
454 * @thread The Emulation Thread.
455 */
456#ifndef IN_RING3
457VMMDECL(RTR3PTR) MMHyperCCToR3(PVM pVM, void *pv)
458{
459 uint32_t off;
460 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
461 if (pLookup)
462 return mmHyperLookupCalcR3(pLookup, off);
463 return NIL_RTR3PTR;
464}
465#endif
466
467/**
468 * Converts a current context address in the Hypervisor memory region to a ring-0 host context address.
469 *
470 * @returns ring-0 host context address.
471 * @param pVM The VM to operate on.
472 * @param pv The current context address.
473 * You'll be damned if this is not in the HMA! :-)
474 * @thread The Emulation Thread.
475 */
476#ifndef IN_RING0
477VMMDECL(RTR0PTR) MMHyperCCToR0(PVM pVM, void *pv)
478{
479 uint32_t off;
480 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
481 if (pLookup)
482 return mmHyperLookupCalcR0(pLookup, off);
483 return NIL_RTR0PTR;
484}
485#endif
486
487
488/**
489 * Converts a current context address in the Hypervisor memory region to a raw-mode context address.
490 *
491 * @returns guest 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_GC
498VMMDECL(RTRCPTR) MMHyperCCToRC(PVM pVM, void *pv)
499{
500 uint32_t off;
501 PMMLOOKUPHYPER pLookup = mmHyperLookupCC(pVM, pv, &off);
502 if (pLookup)
503 return mmHyperLookupCalcRC(pVM, pLookup, off);
504 return NIL_RTRCPTR;
505}
506#endif
507
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