VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMSwitcher.cpp@ 26600

Last change on this file since 26600 was 26437, checked in by vboxsync, 15 years ago

VMM: VBOX_WITH_RAW_MODE fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 36.9 KB
Line 
1/* $Id: VMMSwitcher.cpp 26437 2010-02-11 15:06:15Z vboxsync $ */
2/** @file
3 * VMM - The Virtual Machine Monitor, World Switcher(s).
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* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_VMM
26#include <VBox/vmm.h>
27#include <VBox/pgm.h>
28#include <VBox/selm.h>
29#include <VBox/mm.h>
30#include <VBox/sup.h>
31#include "VMMInternal.h"
32#include "VMMSwitcher/VMMSwitcher.h"
33#include <VBox/vm.h>
34#include <VBox/dis.h>
35
36#include <VBox/err.h>
37#include <VBox/param.h>
38#include <iprt/assert.h>
39#include <iprt/alloc.h>
40#include <iprt/asm.h>
41#include <iprt/string.h>
42#include <iprt/ctype.h>
43
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48/** Array of switcher defininitions.
49 * The type and index shall match!
50 */
51static PVMMSWITCHERDEF s_apSwitchers[VMMSWITCHER_MAX] =
52{
53 NULL, /* invalid entry */
54#ifdef VBOX_WITH_RAW_MODE
55# ifndef RT_ARCH_AMD64
56 &vmmR3Switcher32BitTo32Bit_Def,
57 &vmmR3Switcher32BitToPAE_Def,
58 &vmmR3Switcher32BitToAMD64_Def,
59 &vmmR3SwitcherPAETo32Bit_Def,
60 &vmmR3SwitcherPAEToPAE_Def,
61 &vmmR3SwitcherPAEToAMD64_Def,
62 NULL, //&vmmR3SwitcherPAETo32Bit_Def,
63# ifdef VBOX_WITH_HYBRID_32BIT_KERNEL
64 &vmmR3SwitcherAMD64ToPAE_Def,
65# else
66 NULL, //&vmmR3SwitcherAMD64ToPAE_Def,
67# endif
68 NULL //&vmmR3SwitcherAMD64ToAMD64_Def,
69# else /* RT_ARCH_AMD64 */
70 NULL, //&vmmR3Switcher32BitTo32Bit_Def,
71 NULL, //&vmmR3Switcher32BitToPAE_Def,
72 NULL, //&vmmR3Switcher32BitToAMD64_Def,
73 NULL, //&vmmR3SwitcherPAETo32Bit_Def,
74 NULL, //&vmmR3SwitcherPAEToPAE_Def,
75 NULL, //&vmmR3SwitcherPAEToAMD64_Def,
76 &vmmR3SwitcherAMD64To32Bit_Def,
77 &vmmR3SwitcherAMD64ToPAE_Def,
78 NULL //&vmmR3SwitcherAMD64ToAMD64_Def,
79# endif /* RT_ARCH_AMD64 */
80#else /* !VBOX_WITH_RAW_MODE */
81 NULL,
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 NULL,
87 NULL,
88 NULL,
89 NULL
90#endif /* !VBOX_WITH_RAW_MODE */
91};
92
93
94/**
95 * VMMR3Init worker that initiates the switcher code (aka core code).
96 *
97 * This is core per VM code which might need fixups and/or for ease of use are
98 * put on linear contiguous backing.
99 *
100 * @returns VBox status code.
101 * @param pVM Pointer to the shared VM structure.
102 */
103int vmmR3SwitcherInit(PVM pVM)
104{
105#ifndef VBOX_WITH_RAW_MODE
106 return VINF_SUCCESS;
107#else
108 /*
109 * Calc the size.
110 */
111 unsigned cbCoreCode = 0;
112 for (unsigned iSwitcher = 0; iSwitcher < RT_ELEMENTS(s_apSwitchers); iSwitcher++)
113 {
114 pVM->vmm.s.aoffSwitchers[iSwitcher] = cbCoreCode;
115 PVMMSWITCHERDEF pSwitcher = s_apSwitchers[iSwitcher];
116 if (pSwitcher)
117 {
118 AssertRelease((unsigned)pSwitcher->enmType == iSwitcher);
119 cbCoreCode += RT_ALIGN_32(pSwitcher->cbCode + 1, 32);
120 }
121 }
122
123 /*
124 * Allocate continguous pages for switchers and deal with
125 * conflicts in the intermediate mapping of the code.
126 */
127 pVM->vmm.s.cbCoreCode = RT_ALIGN_32(cbCoreCode, PAGE_SIZE);
128 pVM->vmm.s.pvCoreCodeR3 = SUPR3ContAlloc(pVM->vmm.s.cbCoreCode >> PAGE_SHIFT, &pVM->vmm.s.pvCoreCodeR0, &pVM->vmm.s.HCPhysCoreCode);
129 int rc = VERR_NO_MEMORY;
130 if (pVM->vmm.s.pvCoreCodeR3)
131 {
132 rc = PGMR3MapIntermediate(pVM, pVM->vmm.s.pvCoreCodeR0, pVM->vmm.s.HCPhysCoreCode, cbCoreCode);
133 if (rc == VERR_PGM_INTERMEDIATE_PAGING_CONFLICT)
134 {
135 /* try more allocations - Solaris, Linux. */
136 const unsigned cTries = 8234;
137 struct VMMInitBadTry
138 {
139 RTR0PTR pvR0;
140 void *pvR3;
141 RTHCPHYS HCPhys;
142 RTUINT cb;
143 } *paBadTries = (struct VMMInitBadTry *)RTMemTmpAlloc(sizeof(*paBadTries) * cTries);
144 AssertReturn(paBadTries, VERR_NO_TMP_MEMORY);
145 unsigned i = 0;
146 do
147 {
148 paBadTries[i].pvR3 = pVM->vmm.s.pvCoreCodeR3;
149 paBadTries[i].pvR0 = pVM->vmm.s.pvCoreCodeR0;
150 paBadTries[i].HCPhys = pVM->vmm.s.HCPhysCoreCode;
151 i++;
152 pVM->vmm.s.pvCoreCodeR0 = NIL_RTR0PTR;
153 pVM->vmm.s.HCPhysCoreCode = NIL_RTHCPHYS;
154 pVM->vmm.s.pvCoreCodeR3 = SUPR3ContAlloc(pVM->vmm.s.cbCoreCode >> PAGE_SHIFT, &pVM->vmm.s.pvCoreCodeR0, &pVM->vmm.s.HCPhysCoreCode);
155 if (!pVM->vmm.s.pvCoreCodeR3)
156 break;
157 rc = PGMR3MapIntermediate(pVM, pVM->vmm.s.pvCoreCodeR0, pVM->vmm.s.HCPhysCoreCode, cbCoreCode);
158 } while ( rc == VERR_PGM_INTERMEDIATE_PAGING_CONFLICT
159 && i < cTries - 1);
160
161 /* cleanup */
162 if (RT_FAILURE(rc))
163 {
164 paBadTries[i].pvR3 = pVM->vmm.s.pvCoreCodeR3;
165 paBadTries[i].pvR0 = pVM->vmm.s.pvCoreCodeR0;
166 paBadTries[i].HCPhys = pVM->vmm.s.HCPhysCoreCode;
167 paBadTries[i].cb = pVM->vmm.s.cbCoreCode;
168 i++;
169 LogRel(("Failed to allocated and map core code: rc=%Rrc\n", rc));
170 }
171 while (i-- > 0)
172 {
173 LogRel(("Core code alloc attempt #%d: pvR3=%p pvR0=%p HCPhys=%RHp\n",
174 i, paBadTries[i].pvR3, paBadTries[i].pvR0, paBadTries[i].HCPhys));
175 SUPR3ContFree(paBadTries[i].pvR3, paBadTries[i].cb >> PAGE_SHIFT);
176 }
177 RTMemTmpFree(paBadTries);
178 }
179 }
180 if (RT_SUCCESS(rc))
181 {
182 /*
183 * copy the code.
184 */
185 for (unsigned iSwitcher = 0; iSwitcher < RT_ELEMENTS(s_apSwitchers); iSwitcher++)
186 {
187 PVMMSWITCHERDEF pSwitcher = s_apSwitchers[iSwitcher];
188 if (pSwitcher)
189 memcpy((uint8_t *)pVM->vmm.s.pvCoreCodeR3 + pVM->vmm.s.aoffSwitchers[iSwitcher],
190 pSwitcher->pvCode, pSwitcher->cbCode);
191 }
192
193 /*
194 * Map the code into the GC address space.
195 */
196 RTGCPTR GCPtr;
197 rc = MMR3HyperMapHCPhys(pVM, pVM->vmm.s.pvCoreCodeR3, pVM->vmm.s.pvCoreCodeR0, pVM->vmm.s.HCPhysCoreCode,
198 cbCoreCode, "Core Code", &GCPtr);
199 if (RT_SUCCESS(rc))
200 {
201 pVM->vmm.s.pvCoreCodeRC = GCPtr;
202 MMR3HyperReserve(pVM, PAGE_SIZE, "fence", NULL);
203 LogRel(("CoreCode: R3=%RHv R0=%RHv RC=%RRv Phys=%RHp cb=%#x\n",
204 pVM->vmm.s.pvCoreCodeR3, pVM->vmm.s.pvCoreCodeR0, pVM->vmm.s.pvCoreCodeRC, pVM->vmm.s.HCPhysCoreCode, pVM->vmm.s.cbCoreCode));
205
206 /*
207 * Finally, PGM probably has selected a switcher already but we need
208 * to get the routine addresses, so we'll reselect it.
209 * This may legally fail so, we're ignoring the rc.
210 */
211 VMMR3SelectSwitcher(pVM, pVM->vmm.s.enmSwitcher);
212 return rc;
213 }
214
215 /* shit */
216 AssertMsgFailed(("PGMR3Map(,%RRv, %RHp, %#x, 0) failed with rc=%Rrc\n", pVM->vmm.s.pvCoreCodeRC, pVM->vmm.s.HCPhysCoreCode, cbCoreCode, rc));
217 SUPR3ContFree(pVM->vmm.s.pvCoreCodeR3, pVM->vmm.s.cbCoreCode >> PAGE_SHIFT);
218 }
219 else
220 VMSetError(pVM, rc, RT_SRC_POS,
221 N_("Failed to allocate %d bytes of contiguous memory for the world switcher code"),
222 cbCoreCode);
223
224 pVM->vmm.s.pvCoreCodeR3 = NULL;
225 pVM->vmm.s.pvCoreCodeR0 = NIL_RTR0PTR;
226 pVM->vmm.s.pvCoreCodeRC = 0;
227 return rc;
228#endif
229}
230
231/**
232 * Relocate the switchers, called by VMMR#Relocate.
233 *
234 * @param pVM Pointer to the shared VM structure.
235 * @param offDelta The relocation delta.
236 */
237void vmmR3SwitcherRelocate(PVM pVM, RTGCINTPTR offDelta)
238{
239#ifdef VBOX_WITH_RAW_MODE
240 /*
241 * Relocate all the switchers.
242 */
243 for (unsigned iSwitcher = 0; iSwitcher < RT_ELEMENTS(s_apSwitchers); iSwitcher++)
244 {
245 PVMMSWITCHERDEF pSwitcher = s_apSwitchers[iSwitcher];
246 if (pSwitcher && pSwitcher->pfnRelocate)
247 {
248 unsigned off = pVM->vmm.s.aoffSwitchers[iSwitcher];
249 pSwitcher->pfnRelocate(pVM,
250 pSwitcher,
251 pVM->vmm.s.pvCoreCodeR0 + off,
252 (uint8_t *)pVM->vmm.s.pvCoreCodeR3 + off,
253 pVM->vmm.s.pvCoreCodeRC + off,
254 pVM->vmm.s.HCPhysCoreCode + off);
255 }
256 }
257
258 /*
259 * Recalc the RC address for the current switcher.
260 */
261 PVMMSWITCHERDEF pSwitcher = s_apSwitchers[pVM->vmm.s.enmSwitcher];
262 RTRCPTR RCPtr = pVM->vmm.s.pvCoreCodeRC + pVM->vmm.s.aoffSwitchers[pVM->vmm.s.enmSwitcher];
263 pVM->vmm.s.pfnGuestToHostRC = RCPtr + pSwitcher->offGCGuestToHost;
264 pVM->vmm.s.pfnCallTrampolineRC = RCPtr + pSwitcher->offGCCallTrampoline;
265 pVM->pfnVMMGCGuestToHostAsm = RCPtr + pSwitcher->offGCGuestToHostAsm;
266 pVM->pfnVMMGCGuestToHostAsmHyperCtx = RCPtr + pSwitcher->offGCGuestToHostAsmHyperCtx;
267 pVM->pfnVMMGCGuestToHostAsmGuestCtx = RCPtr + pSwitcher->offGCGuestToHostAsmGuestCtx;
268
269// AssertFailed();
270#endif
271}
272
273
274/**
275 * Generic switcher code relocator.
276 *
277 * @param pVM The VM handle.
278 * @param pSwitcher The switcher definition.
279 * @param pu8CodeR3 Pointer to the core code block for the switcher, ring-3 mapping.
280 * @param R0PtrCode Pointer to the core code block for the switcher, ring-0 mapping.
281 * @param GCPtrCode The guest context address corresponding to pu8Code.
282 * @param u32IDCode The identity mapped (ID) address corresponding to pu8Code.
283 * @param SelCS The hypervisor CS selector.
284 * @param SelDS The hypervisor DS selector.
285 * @param SelTSS The hypervisor TSS selector.
286 * @param GCPtrGDT The GC address of the hypervisor GDT.
287 * @param SelCS64 The 64-bit mode hypervisor CS selector.
288 */
289static void vmmR3SwitcherGenericRelocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode,
290 RTSEL SelCS, RTSEL SelDS, RTSEL SelTSS, RTGCPTR GCPtrGDT, RTSEL SelCS64)
291{
292 union
293 {
294 const uint8_t *pu8;
295 const uint16_t *pu16;
296 const uint32_t *pu32;
297 const uint64_t *pu64;
298 const void *pv;
299 uintptr_t u;
300 } u;
301 u.pv = pSwitcher->pvFixups;
302
303 /*
304 * Process fixups.
305 */
306 uint8_t u8;
307 while ((u8 = *u.pu8++) != FIX_THE_END)
308 {
309 /*
310 * Get the source (where to write the fixup).
311 */
312 uint32_t offSrc = *u.pu32++;
313 Assert(offSrc < pSwitcher->cbCode);
314 union
315 {
316 uint8_t *pu8;
317 uint16_t *pu16;
318 uint32_t *pu32;
319 uint64_t *pu64;
320 uintptr_t u;
321 } uSrc;
322 uSrc.pu8 = pu8CodeR3 + offSrc;
323
324 /* The fixup target and method depends on the type. */
325 switch (u8)
326 {
327 /*
328 * 32-bit relative, source in HC and target in GC.
329 */
330 case FIX_HC_2_GC_NEAR_REL:
331 {
332 Assert(offSrc - pSwitcher->offHCCode0 < pSwitcher->cbHCCode0 || offSrc - pSwitcher->offHCCode1 < pSwitcher->cbHCCode1);
333 uint32_t offTrg = *u.pu32++;
334 Assert(offTrg - pSwitcher->offGCCode < pSwitcher->cbGCCode);
335 *uSrc.pu32 = (uint32_t)((GCPtrCode + offTrg) - (uSrc.u + 4));
336 break;
337 }
338
339 /*
340 * 32-bit relative, source in HC and target in ID.
341 */
342 case FIX_HC_2_ID_NEAR_REL:
343 {
344 Assert(offSrc - pSwitcher->offHCCode0 < pSwitcher->cbHCCode0 || offSrc - pSwitcher->offHCCode1 < pSwitcher->cbHCCode1);
345 uint32_t offTrg = *u.pu32++;
346 Assert(offTrg - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0 || offTrg - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1);
347 *uSrc.pu32 = (uint32_t)((u32IDCode + offTrg) - (R0PtrCode + offSrc + 4));
348 break;
349 }
350
351 /*
352 * 32-bit relative, source in GC and target in HC.
353 */
354 case FIX_GC_2_HC_NEAR_REL:
355 {
356 Assert(offSrc - pSwitcher->offGCCode < pSwitcher->cbGCCode);
357 uint32_t offTrg = *u.pu32++;
358 Assert(offTrg - pSwitcher->offHCCode0 < pSwitcher->cbHCCode0 || offTrg - pSwitcher->offHCCode1 < pSwitcher->cbHCCode1);
359 *uSrc.pu32 = (uint32_t)((R0PtrCode + offTrg) - (GCPtrCode + offSrc + 4));
360 break;
361 }
362
363 /*
364 * 32-bit relative, source in GC and target in ID.
365 */
366 case FIX_GC_2_ID_NEAR_REL:
367 {
368 AssertMsg(offSrc - pSwitcher->offGCCode < pSwitcher->cbGCCode, ("%x - %x < %x\n", offSrc, pSwitcher->offGCCode, pSwitcher->cbGCCode));
369 uint32_t offTrg = *u.pu32++;
370 Assert(offTrg - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0 || offTrg - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1);
371 *uSrc.pu32 = (uint32_t)((u32IDCode + offTrg) - (GCPtrCode + offSrc + 4));
372 break;
373 }
374
375 /*
376 * 32-bit relative, source in ID and target in HC.
377 */
378 case FIX_ID_2_HC_NEAR_REL:
379 {
380 Assert(offSrc - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0 || offSrc - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1);
381 uint32_t offTrg = *u.pu32++;
382 Assert(offTrg - pSwitcher->offHCCode0 < pSwitcher->cbHCCode0 || offTrg - pSwitcher->offHCCode1 < pSwitcher->cbHCCode1);
383 *uSrc.pu32 = (uint32_t)((R0PtrCode + offTrg) - (u32IDCode + offSrc + 4));
384 break;
385 }
386
387 /*
388 * 32-bit relative, source in ID and target in HC.
389 */
390 case FIX_ID_2_GC_NEAR_REL:
391 {
392 Assert(offSrc - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0 || offSrc - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1);
393 uint32_t offTrg = *u.pu32++;
394 Assert(offTrg - pSwitcher->offGCCode < pSwitcher->cbGCCode);
395 *uSrc.pu32 = (uint32_t)((GCPtrCode + offTrg) - (u32IDCode + offSrc + 4));
396 break;
397 }
398
399 /*
400 * 16:32 far jump, target in GC.
401 */
402 case FIX_GC_FAR32:
403 {
404 uint32_t offTrg = *u.pu32++;
405 Assert(offTrg - pSwitcher->offGCCode < pSwitcher->cbGCCode);
406 *uSrc.pu32++ = (uint32_t)(GCPtrCode + offTrg);
407 *uSrc.pu16++ = SelCS;
408 break;
409 }
410
411 /*
412 * Make 32-bit GC pointer given CPUM offset.
413 */
414 case FIX_GC_CPUM_OFF:
415 {
416 uint32_t offCPUM = *u.pu32++;
417 Assert(offCPUM < sizeof(pVM->cpum));
418 *uSrc.pu32 = (uint32_t)(VM_RC_ADDR(pVM, &pVM->cpum) + offCPUM);
419 break;
420 }
421
422 /*
423 * Make 32-bit GC pointer given CPUMCPU offset.
424 */
425 case FIX_GC_CPUMCPU_OFF:
426 {
427 uint32_t offCPUM = *u.pu32++;
428 Assert(offCPUM < sizeof(pVM->aCpus[0].cpum));
429 *uSrc.pu32 = (uint32_t)(VM_RC_ADDR(pVM, &pVM->aCpus[0].cpum) + offCPUM);
430 break;
431 }
432
433 /*
434 * Make 32-bit GC pointer given VM offset.
435 */
436 case FIX_GC_VM_OFF:
437 {
438 uint32_t offVM = *u.pu32++;
439 Assert(offVM < sizeof(VM));
440 *uSrc.pu32 = (uint32_t)(VM_RC_ADDR(pVM, pVM) + offVM);
441 break;
442 }
443
444 /*
445 * Make 32-bit HC pointer given CPUM offset.
446 */
447 case FIX_HC_CPUM_OFF:
448 {
449 uint32_t offCPUM = *u.pu32++;
450 Assert(offCPUM < sizeof(pVM->cpum));
451 *uSrc.pu32 = (uint32_t)pVM->pVMR0 + RT_OFFSETOF(VM, cpum) + offCPUM;
452 break;
453 }
454
455 /*
456 * Make 32-bit R0 pointer given VM offset.
457 */
458 case FIX_HC_VM_OFF:
459 {
460 uint32_t offVM = *u.pu32++;
461 Assert(offVM < sizeof(VM));
462 *uSrc.pu32 = (uint32_t)pVM->pVMR0 + offVM;
463 break;
464 }
465
466 /*
467 * Store the 32-Bit CR3 (32-bit) for the intermediate memory context.
468 */
469 case FIX_INTER_32BIT_CR3:
470 {
471
472 *uSrc.pu32 = PGMGetInter32BitCR3(pVM);
473 break;
474 }
475
476 /*
477 * Store the PAE CR3 (32-bit) for the intermediate memory context.
478 */
479 case FIX_INTER_PAE_CR3:
480 {
481
482 *uSrc.pu32 = PGMGetInterPaeCR3(pVM);
483 break;
484 }
485
486 /*
487 * Store the AMD64 CR3 (32-bit) for the intermediate memory context.
488 */
489 case FIX_INTER_AMD64_CR3:
490 {
491
492 *uSrc.pu32 = PGMGetInterAmd64CR3(pVM);
493 break;
494 }
495
496 /*
497 * Store Hypervisor CS (16-bit).
498 */
499 case FIX_HYPER_CS:
500 {
501 *uSrc.pu16 = SelCS;
502 break;
503 }
504
505 /*
506 * Store Hypervisor DS (16-bit).
507 */
508 case FIX_HYPER_DS:
509 {
510 *uSrc.pu16 = SelDS;
511 break;
512 }
513
514 /*
515 * Store Hypervisor TSS (16-bit).
516 */
517 case FIX_HYPER_TSS:
518 {
519 *uSrc.pu16 = SelTSS;
520 break;
521 }
522
523 /*
524 * Store the 32-bit GC address of the 2nd dword of the TSS descriptor (in the GDT).
525 */
526 case FIX_GC_TSS_GDTE_DW2:
527 {
528 RTGCPTR GCPtr = GCPtrGDT + (SelTSS & ~7) + 4;
529 *uSrc.pu32 = (uint32_t)GCPtr;
530 break;
531 }
532
533
534 ///@todo case FIX_CR4_MASK:
535 ///@todo case FIX_CR4_OSFSXR:
536
537 /*
538 * Insert relative jump to specified target it FXSAVE/FXRSTOR isn't supported by the cpu.
539 */
540 case FIX_NO_FXSAVE_JMP:
541 {
542 uint32_t offTrg = *u.pu32++;
543 Assert(offTrg < pSwitcher->cbCode);
544 if (!CPUMSupportsFXSR(pVM))
545 {
546 *uSrc.pu8++ = 0xe9; /* jmp rel32 */
547 *uSrc.pu32++ = offTrg - (offSrc + 5);
548 }
549 else
550 {
551 *uSrc.pu8++ = *((uint8_t *)pSwitcher->pvCode + offSrc);
552 *uSrc.pu32++ = *(uint32_t *)((uint8_t *)pSwitcher->pvCode + offSrc + 1);
553 }
554 break;
555 }
556
557 /*
558 * Insert relative jump to specified target it SYSENTER isn't used by the host.
559 */
560 case FIX_NO_SYSENTER_JMP:
561 {
562 uint32_t offTrg = *u.pu32++;
563 Assert(offTrg < pSwitcher->cbCode);
564 if (!CPUMIsHostUsingSysEnter(pVM))
565 {
566 *uSrc.pu8++ = 0xe9; /* jmp rel32 */
567 *uSrc.pu32++ = offTrg - (offSrc + 5);
568 }
569 else
570 {
571 *uSrc.pu8++ = *((uint8_t *)pSwitcher->pvCode + offSrc);
572 *uSrc.pu32++ = *(uint32_t *)((uint8_t *)pSwitcher->pvCode + offSrc + 1);
573 }
574 break;
575 }
576
577 /*
578 * Insert relative jump to specified target it SYSCALL isn't used by the host.
579 */
580 case FIX_NO_SYSCALL_JMP:
581 {
582 uint32_t offTrg = *u.pu32++;
583 Assert(offTrg < pSwitcher->cbCode);
584 if (!CPUMIsHostUsingSysCall(pVM))
585 {
586 *uSrc.pu8++ = 0xe9; /* jmp rel32 */
587 *uSrc.pu32++ = offTrg - (offSrc + 5);
588 }
589 else
590 {
591 *uSrc.pu8++ = *((uint8_t *)pSwitcher->pvCode + offSrc);
592 *uSrc.pu32++ = *(uint32_t *)((uint8_t *)pSwitcher->pvCode + offSrc + 1);
593 }
594 break;
595 }
596
597 /*
598 * 32-bit HC pointer fixup to (HC) target within the code (32-bit offset).
599 */
600 case FIX_HC_32BIT:
601 {
602 uint32_t offTrg = *u.pu32++;
603 Assert(offSrc < pSwitcher->cbCode);
604 Assert(offTrg - pSwitcher->offHCCode0 < pSwitcher->cbHCCode0 || offTrg - pSwitcher->offHCCode1 < pSwitcher->cbHCCode1);
605 *uSrc.pu32 = R0PtrCode + offTrg;
606 break;
607 }
608
609#if defined(RT_ARCH_AMD64) || defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
610 /*
611 * 64-bit HC Code Selector (no argument).
612 */
613 case FIX_HC_64BIT_CS:
614 {
615 Assert(offSrc < pSwitcher->cbCode);
616# if defined(RT_OS_DARWIN) && defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
617 *uSrc.pu16 = 0x80; /* KERNEL64_CS from i386/seg.h */
618# else
619 AssertFatalMsgFailed(("FIX_HC_64BIT_CS not implemented for this host\n"));
620# endif
621 break;
622 }
623
624 /*
625 * 64-bit HC pointer to the CPUM instance data (no argument).
626 */
627 case FIX_HC_64BIT_CPUM:
628 {
629 Assert(offSrc < pSwitcher->cbCode);
630 *uSrc.pu64 = pVM->pVMR0 + RT_OFFSETOF(VM, cpum);
631 break;
632 }
633#endif
634 /*
635 * 64-bit HC pointer fixup to (HC) target within the code (32-bit offset).
636 */
637 case FIX_HC_64BIT:
638 {
639 uint32_t offTrg = *u.pu32++;
640 Assert(offSrc < pSwitcher->cbCode);
641 Assert(offTrg - pSwitcher->offHCCode0 < pSwitcher->cbHCCode0 || offTrg - pSwitcher->offHCCode1 < pSwitcher->cbHCCode1);
642 *uSrc.pu64 = R0PtrCode + offTrg;
643 break;
644 }
645
646#ifdef RT_ARCH_X86
647 case FIX_GC_64_BIT_CPUM_OFF:
648 {
649 uint32_t offCPUM = *u.pu32++;
650 Assert(offCPUM < sizeof(pVM->cpum));
651 *uSrc.pu64 = (uint32_t)(VM_RC_ADDR(pVM, &pVM->cpum) + offCPUM);
652 break;
653 }
654#endif
655
656 /*
657 * 32-bit ID pointer to (ID) target within the code (32-bit offset).
658 */
659 case FIX_ID_32BIT:
660 {
661 uint32_t offTrg = *u.pu32++;
662 Assert(offSrc < pSwitcher->cbCode);
663 Assert(offTrg - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0 || offTrg - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1);
664 *uSrc.pu32 = u32IDCode + offTrg;
665 break;
666 }
667
668 /*
669 * 64-bit ID pointer to (ID) target within the code (32-bit offset).
670 */
671 case FIX_ID_64BIT:
672 case FIX_HC_64BIT_NOCHECK:
673 {
674 uint32_t offTrg = *u.pu32++;
675 Assert(offSrc < pSwitcher->cbCode);
676 Assert(u8 == FIX_HC_64BIT_NOCHECK || offTrg - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0 || offTrg - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1);
677 *uSrc.pu64 = u32IDCode + offTrg;
678 break;
679 }
680
681 /*
682 * Far 16:32 ID pointer to 64-bit mode (ID) target within the code (32-bit offset).
683 */
684 case FIX_ID_FAR32_TO_64BIT_MODE:
685 {
686 uint32_t offTrg = *u.pu32++;
687 Assert(offSrc < pSwitcher->cbCode);
688 Assert(offTrg - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0 || offTrg - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1);
689 *uSrc.pu32++ = u32IDCode + offTrg;
690 *uSrc.pu16 = SelCS64;
691 AssertRelease(SelCS64);
692 break;
693 }
694
695#ifdef VBOX_WITH_NMI
696 /*
697 * 32-bit address to the APIC base.
698 */
699 case FIX_GC_APIC_BASE_32BIT:
700 {
701 *uSrc.pu32 = pVM->vmm.s.GCPtrApicBase;
702 break;
703 }
704#endif
705
706 default:
707 AssertReleaseMsgFailed(("Unknown fixup %d in switcher %s\n", u8, pSwitcher->pszDesc));
708 break;
709 }
710 }
711
712#ifdef LOG_ENABLED
713 /*
714 * If Log2 is enabled disassemble the switcher code.
715 *
716 * The switcher code have 1-2 HC parts, 1 GC part and 0-2 ID parts.
717 */
718 if (LogIs2Enabled())
719 {
720 RTLogPrintf("*** Disassembly of switcher %d '%s' %#x bytes ***\n"
721 " R0PtrCode = %p\n"
722 " pu8CodeR3 = %p\n"
723 " GCPtrCode = %RGv\n"
724 " u32IDCode = %08x\n"
725 " pVMRC = %RRv\n"
726 " pCPUMRC = %RRv\n"
727 " pVMR3 = %p\n"
728 " pCPUMR3 = %p\n"
729 " GCPtrGDT = %RGv\n"
730 " InterCR3s = %08RHp, %08RHp, %08RHp (32-Bit, PAE, AMD64)\n"
731 " HyperCR3s = %08RHp (32-Bit, PAE & AMD64)\n"
732 " SelCS = %04x\n"
733 " SelDS = %04x\n"
734 " SelCS64 = %04x\n"
735 " SelTSS = %04x\n",
736 pSwitcher->enmType, pSwitcher->pszDesc, pSwitcher->cbCode,
737 R0PtrCode,
738 pu8CodeR3,
739 GCPtrCode,
740 u32IDCode,
741 VM_RC_ADDR(pVM, pVM),
742 VM_RC_ADDR(pVM, &pVM->cpum),
743 pVM,
744 &pVM->cpum,
745 GCPtrGDT,
746 PGMGetInter32BitCR3(pVM), PGMGetInterPaeCR3(pVM), PGMGetInterAmd64CR3(pVM),
747 PGMGetHyperCR3(VMMGetCpu(pVM)),
748 SelCS, SelDS, SelCS64, SelTSS);
749
750 uint32_t offCode = 0;
751 while (offCode < pSwitcher->cbCode)
752 {
753 /*
754 * Figure out where this is.
755 */
756 const char *pszDesc = NULL;
757 RTUINTPTR uBase;
758 uint32_t cbCode;
759 if (offCode - pSwitcher->offHCCode0 < pSwitcher->cbHCCode0)
760 {
761 pszDesc = "HCCode0";
762 uBase = R0PtrCode;
763 offCode = pSwitcher->offHCCode0;
764 cbCode = pSwitcher->cbHCCode0;
765 }
766 else if (offCode - pSwitcher->offHCCode1 < pSwitcher->cbHCCode1)
767 {
768 pszDesc = "HCCode1";
769 uBase = R0PtrCode;
770 offCode = pSwitcher->offHCCode1;
771 cbCode = pSwitcher->cbHCCode1;
772 }
773 else if (offCode - pSwitcher->offGCCode < pSwitcher->cbGCCode)
774 {
775 pszDesc = "GCCode";
776 uBase = GCPtrCode;
777 offCode = pSwitcher->offGCCode;
778 cbCode = pSwitcher->cbGCCode;
779 }
780 else if (offCode - pSwitcher->offIDCode0 < pSwitcher->cbIDCode0)
781 {
782 pszDesc = "IDCode0";
783 uBase = u32IDCode;
784 offCode = pSwitcher->offIDCode0;
785 cbCode = pSwitcher->cbIDCode0;
786 }
787 else if (offCode - pSwitcher->offIDCode1 < pSwitcher->cbIDCode1)
788 {
789 pszDesc = "IDCode1";
790 uBase = u32IDCode;
791 offCode = pSwitcher->offIDCode1;
792 cbCode = pSwitcher->cbIDCode1;
793 }
794 else
795 {
796 RTLogPrintf(" %04x: %02x '%c' (nowhere)\n",
797 offCode, pu8CodeR3[offCode], RT_C_IS_PRINT(pu8CodeR3[offCode]) ? pu8CodeR3[offCode] : ' ');
798 offCode++;
799 continue;
800 }
801
802 /*
803 * Disassemble it.
804 */
805 RTLogPrintf(" %s: offCode=%#x cbCode=%#x\n", pszDesc, offCode, cbCode);
806 DISCPUSTATE Cpu;
807
808 memset(&Cpu, 0, sizeof(Cpu));
809 Cpu.mode = CPUMODE_32BIT;
810 while (cbCode > 0)
811 {
812 /* try label it */
813 if (pSwitcher->offR0HostToGuest == offCode)
814 RTLogPrintf(" *R0HostToGuest:\n");
815 if (pSwitcher->offGCGuestToHost == offCode)
816 RTLogPrintf(" *GCGuestToHost:\n");
817 if (pSwitcher->offGCCallTrampoline == offCode)
818 RTLogPrintf(" *GCCallTrampoline:\n");
819 if (pSwitcher->offGCGuestToHostAsm == offCode)
820 RTLogPrintf(" *GCGuestToHostAsm:\n");
821 if (pSwitcher->offGCGuestToHostAsmHyperCtx == offCode)
822 RTLogPrintf(" *GCGuestToHostAsmHyperCtx:\n");
823 if (pSwitcher->offGCGuestToHostAsmGuestCtx == offCode)
824 RTLogPrintf(" *GCGuestToHostAsmGuestCtx:\n");
825
826 /* disas */
827 uint32_t cbInstr = 0;
828 char szDisas[256];
829 if (RT_SUCCESS(DISInstr(&Cpu, (uintptr_t)pu8CodeR3 + offCode, uBase - (uintptr_t)pu8CodeR3, &cbInstr, szDisas)))
830 RTLogPrintf(" %04x: %s", offCode, szDisas); //for whatever reason szDisas includes '\n'.
831 else
832 {
833 RTLogPrintf(" %04x: %02x '%c'\n",
834 offCode, pu8CodeR3[offCode], RT_C_IS_PRINT(pu8CodeR3[offCode]) ? pu8CodeR3[offCode] : ' ');
835 cbInstr = 1;
836 }
837 offCode += cbInstr;
838 cbCode -= RT_MIN(cbInstr, cbCode);
839 }
840 }
841 }
842#endif
843}
844
845
846/**
847 * Relocator for the 32-Bit to 32-Bit world switcher.
848 */
849DECLCALLBACK(void) vmmR3Switcher32BitTo32Bit_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
850{
851 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
852 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), 0);
853}
854
855
856/**
857 * Relocator for the 32-Bit to PAE world switcher.
858 */
859DECLCALLBACK(void) vmmR3Switcher32BitToPAE_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
860{
861 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
862 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), 0);
863}
864
865
866/**
867 * Relocator for the 32-Bit to AMD64 world switcher.
868 */
869DECLCALLBACK(void) vmmR3Switcher32BitToAMD64_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
870{
871 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
872 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), SELMGetHyperCS64(pVM));
873}
874
875
876/**
877 * Relocator for the PAE to 32-Bit world switcher.
878 */
879DECLCALLBACK(void) vmmR3SwitcherPAETo32Bit_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
880{
881 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
882 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), 0);
883}
884
885
886/**
887 * Relocator for the PAE to PAE world switcher.
888 */
889DECLCALLBACK(void) vmmR3SwitcherPAEToPAE_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
890{
891 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
892 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), 0);
893}
894
895/**
896 * Relocator for the PAE to AMD64 world switcher.
897 */
898DECLCALLBACK(void) vmmR3SwitcherPAEToAMD64_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
899{
900 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
901 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), SELMGetHyperCS64(pVM));
902}
903
904
905/**
906 * Relocator for the AMD64 to 32-bit world switcher.
907 */
908DECLCALLBACK(void) vmmR3SwitcherAMD64To32Bit_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
909{
910 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
911 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), SELMGetHyperCS64(pVM));
912}
913
914
915/**
916 * Relocator for the AMD64 to PAE world switcher.
917 */
918DECLCALLBACK(void) vmmR3SwitcherAMD64ToPAE_Relocate(PVM pVM, PVMMSWITCHERDEF pSwitcher, RTR0PTR R0PtrCode, uint8_t *pu8CodeR3, RTGCPTR GCPtrCode, uint32_t u32IDCode)
919{
920 vmmR3SwitcherGenericRelocate(pVM, pSwitcher, R0PtrCode, pu8CodeR3, GCPtrCode, u32IDCode,
921 SELMGetHyperCS(pVM), SELMGetHyperDS(pVM), SELMGetHyperTSS(pVM), SELMGetHyperGDT(pVM), SELMGetHyperCS64(pVM));
922}
923
924
925/**
926 * Selects the switcher to be used for switching to GC.
927 *
928 * @returns VBox status code.
929 * @param pVM VM handle.
930 * @param enmSwitcher The new switcher.
931 * @remark This function may be called before the VMM is initialized.
932 */
933VMMR3DECL(int) VMMR3SelectSwitcher(PVM pVM, VMMSWITCHER enmSwitcher)
934{
935 /*
936 * Validate input.
937 */
938 if ( enmSwitcher < VMMSWITCHER_INVALID
939 || enmSwitcher >= VMMSWITCHER_MAX)
940 {
941 AssertMsgFailed(("Invalid input enmSwitcher=%d\n", enmSwitcher));
942 return VERR_INVALID_PARAMETER;
943 }
944
945 /* Do nothing if the switcher is disabled. */
946 if (pVM->vmm.s.fSwitcherDisabled)
947 return VINF_SUCCESS;
948
949 /*
950 * Select the new switcher.
951 */
952 PVMMSWITCHERDEF pSwitcher = s_apSwitchers[enmSwitcher];
953 if (pSwitcher)
954 {
955 Log(("VMMR3SelectSwitcher: enmSwitcher %d -> %d %s\n", pVM->vmm.s.enmSwitcher, enmSwitcher, pSwitcher->pszDesc));
956 pVM->vmm.s.enmSwitcher = enmSwitcher;
957
958 RTR0PTR pbCodeR0 = (RTR0PTR)pVM->vmm.s.pvCoreCodeR0 + pVM->vmm.s.aoffSwitchers[enmSwitcher]; /** @todo fix the pvCoreCodeR0 type */
959 pVM->vmm.s.pfnHostToGuestR0 = pbCodeR0 + pSwitcher->offR0HostToGuest;
960
961 RTGCPTR GCPtr = pVM->vmm.s.pvCoreCodeRC + pVM->vmm.s.aoffSwitchers[enmSwitcher];
962 pVM->vmm.s.pfnGuestToHostRC = GCPtr + pSwitcher->offGCGuestToHost;
963 pVM->vmm.s.pfnCallTrampolineRC = GCPtr + pSwitcher->offGCCallTrampoline;
964 pVM->pfnVMMGCGuestToHostAsm = GCPtr + pSwitcher->offGCGuestToHostAsm;
965 pVM->pfnVMMGCGuestToHostAsmHyperCtx = GCPtr + pSwitcher->offGCGuestToHostAsmHyperCtx;
966 pVM->pfnVMMGCGuestToHostAsmGuestCtx = GCPtr + pSwitcher->offGCGuestToHostAsmGuestCtx;
967 return VINF_SUCCESS;
968 }
969
970 return VERR_NOT_IMPLEMENTED;
971}
972
973
974/**
975 * Disable the switcher logic permanently.
976 *
977 * @returns VBox status code.
978 * @param pVM VM handle.
979 */
980VMMR3DECL(int) VMMR3DisableSwitcher(PVM pVM)
981{
982/** @todo r=bird: I would suggest that we create a dummy switcher which just does something like:
983 * @code
984 * mov eax, VERR_INTERNAL_ERROR
985 * ret
986 * @endcode
987 * And then check for fSwitcherDisabled in VMMR3SelectSwitcher() in order to prevent it from being removed.
988 */
989 pVM->vmm.s.fSwitcherDisabled = true;
990 return VINF_SUCCESS;
991}
992
993
994/**
995 * Gets the switcher to be used for switching to GC.
996 *
997 * @returns host to guest ring 0 switcher entrypoint
998 * @param pVM VM handle.
999 * @param enmSwitcher The new switcher.
1000 */
1001VMMR3DECL(RTR0PTR) VMMR3GetHostToGuestSwitcher(PVM pVM, VMMSWITCHER enmSwitcher)
1002{
1003 /*
1004 * Validate input.
1005 */
1006 if ( enmSwitcher < VMMSWITCHER_INVALID
1007 || enmSwitcher >= VMMSWITCHER_MAX)
1008 {
1009 AssertMsgFailed(("Invalid input enmSwitcher=%d\n", enmSwitcher));
1010 return NIL_RTR0PTR;
1011 }
1012
1013 /*
1014 * Select the new switcher.
1015 */
1016 PVMMSWITCHERDEF pSwitcher = s_apSwitchers[enmSwitcher];
1017 if (pSwitcher)
1018 {
1019 RTR0PTR pbCodeR0 = (RTR0PTR)pVM->vmm.s.pvCoreCodeR0 + pVM->vmm.s.aoffSwitchers[enmSwitcher]; /** @todo fix the pvCoreCodeR0 type */
1020 return pbCodeR0 + pSwitcher->offR0HostToGuest;
1021 }
1022 return NIL_RTR0PTR;
1023}
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