1 | /* $Id: VMMGC.cpp 29250 2010-05-09 17:53:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM - Raw-mode Context.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_VMM
|
---|
23 | #include <iprt/asm-amd64-x86.h> /* for SUPGetCpuHzFromGIP */
|
---|
24 | #include <VBox/vmm.h>
|
---|
25 | #include <VBox/trpm.h>
|
---|
26 | #include <VBox/pgm.h>
|
---|
27 | #include "VMMInternal.h"
|
---|
28 | #include <VBox/vm.h>
|
---|
29 | #include <VBox/sup.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/log.h>
|
---|
32 | #include <iprt/assert.h>
|
---|
33 | #include <iprt/initterm.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Global Variables *
|
---|
38 | *******************************************************************************/
|
---|
39 | /** Default logger instance. */
|
---|
40 | extern "C" DECLIMPORT(RTLOGGERRC) g_Logger;
|
---|
41 | extern "C" DECLIMPORT(RTLOGGERRC) g_RelLogger;
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Internal Functions *
|
---|
46 | *******************************************************************************/
|
---|
47 | static int vmmGCTest(PVM pVM, unsigned uOperation, unsigned uArg);
|
---|
48 | static DECLCALLBACK(int) vmmGCTestTmpPFHandler(PVM pVM, PCPUMCTXCORE pRegFrame);
|
---|
49 | static DECLCALLBACK(int) vmmGCTestTmpPFHandlerCorruptFS(PVM pVM, PCPUMCTXCORE pRegFrame);
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * The GC entry point.
|
---|
55 | *
|
---|
56 | * @returns VBox status code.
|
---|
57 | * @param pVM The VM to operate on.
|
---|
58 | * @param uOperation Which operation to execute (VMMGCOPERATION).
|
---|
59 | * @param uArg Argument to that operation.
|
---|
60 | */
|
---|
61 | VMMRCDECL(int) VMMGCEntry(PVM pVM, unsigned uOperation, unsigned uArg, ...)
|
---|
62 | {
|
---|
63 | /* todo */
|
---|
64 | switch (uOperation)
|
---|
65 | {
|
---|
66 | /*
|
---|
67 | * Init RC modules.
|
---|
68 | */
|
---|
69 | case VMMGC_DO_VMMGC_INIT:
|
---|
70 | {
|
---|
71 | /*
|
---|
72 | * Validate the svn revision (uArg).
|
---|
73 | */
|
---|
74 | if (uArg != VMMGetSvnRev())
|
---|
75 | return VERR_VERSION_MISMATCH;
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Initialize the runtime.
|
---|
79 | * (The program timestamp is found in the elipsis.)
|
---|
80 | */
|
---|
81 | va_list va;
|
---|
82 | va_start(va, uArg);
|
---|
83 | uint64_t u64TS = va_arg(va, uint64_t);
|
---|
84 | va_end(va);
|
---|
85 |
|
---|
86 | int rc = RTRCInit(u64TS);
|
---|
87 | Log(("VMMGCEntry: VMMGC_DO_VMMGC_INIT - uArg=%u (svn revision) u64TS=%RX64; rc=%Rrc\n", uArg, u64TS, rc));
|
---|
88 | AssertRCReturn(rc, rc);
|
---|
89 |
|
---|
90 | rc = PGMRegisterStringFormatTypes();
|
---|
91 | AssertRCReturn(rc, rc);
|
---|
92 |
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * Testcase which is used to test interrupt forwarding.
|
---|
98 | * It spins for a while with interrupts enabled.
|
---|
99 | */
|
---|
100 | case VMMGC_DO_TESTCASE_HYPER_INTERRUPT:
|
---|
101 | {
|
---|
102 | uint32_t volatile i = 0;
|
---|
103 | ASMIntEnable();
|
---|
104 | while (i < _2G32)
|
---|
105 | i++;
|
---|
106 | ASMIntDisable();
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Testcase which simply returns, this is used for
|
---|
112 | * profiling of the switcher.
|
---|
113 | */
|
---|
114 | case VMMGC_DO_TESTCASE_NOP:
|
---|
115 | return 0;
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Testcase executes a privileged instruction to force a world switch. (in both SVM & VMX)
|
---|
119 | */
|
---|
120 | case VMMGC_DO_TESTCASE_HWACCM_NOP:
|
---|
121 | ASMRdMsr_Low(MSR_IA32_SYSENTER_CS);
|
---|
122 | return 0;
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Delay for ~100us.
|
---|
126 | */
|
---|
127 | case VMMGC_DO_TESTCASE_INTERRUPT_MASKING:
|
---|
128 | {
|
---|
129 | uint64_t u64MaxTicks = (SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage) != ~(uint64_t)0
|
---|
130 | ? SUPGetCpuHzFromGIP(g_pSUPGlobalInfoPage)
|
---|
131 | : _2G)
|
---|
132 | / 10000;
|
---|
133 | uint64_t u64StartTSC = ASMReadTSC();
|
---|
134 | uint64_t u64TicksNow;
|
---|
135 | uint32_t volatile i = 0;
|
---|
136 |
|
---|
137 | do
|
---|
138 | {
|
---|
139 | /* waste some time and protect against getting stuck. */
|
---|
140 | for (uint32_t volatile j = 0; j < 1000; j++, i++)
|
---|
141 | if (i > _2G32)
|
---|
142 | return VERR_GENERAL_FAILURE;
|
---|
143 |
|
---|
144 | /* check if we're done.*/
|
---|
145 | u64TicksNow = ASMReadTSC() - u64StartTSC;
|
---|
146 | } while (u64TicksNow < u64MaxTicks);
|
---|
147 |
|
---|
148 | return VINF_SUCCESS;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * Trap testcases and unknown operations.
|
---|
153 | */
|
---|
154 | default:
|
---|
155 | if ( uOperation >= VMMGC_DO_TESTCASE_TRAP_FIRST
|
---|
156 | && uOperation < VMMGC_DO_TESTCASE_TRAP_LAST)
|
---|
157 | return vmmGCTest(pVM, uOperation, uArg);
|
---|
158 | return VERR_INVALID_PARAMETER;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Internal RC logger worker: Flush logger.
|
---|
165 | *
|
---|
166 | * @returns VINF_SUCCESS.
|
---|
167 | * @param pLogger The logger instance to flush.
|
---|
168 | * @remark This function must be exported!
|
---|
169 | */
|
---|
170 | VMMRCDECL(int) vmmGCLoggerFlush(PRTLOGGERRC pLogger)
|
---|
171 | {
|
---|
172 | PVM pVM = &g_VM;
|
---|
173 | NOREF(pLogger);
|
---|
174 | if (pVM->vmm.s.fRCLoggerFlushingDisabled)
|
---|
175 | return VINF_SUCCESS; /* fail quietly. */
|
---|
176 | return VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Flush logger if almost full.
|
---|
182 | *
|
---|
183 | * @param pVM The VM handle.
|
---|
184 | */
|
---|
185 | VMMRCDECL(void) VMMGCLogFlushIfFull(PVM pVM)
|
---|
186 | {
|
---|
187 | if ( pVM->vmm.s.pRCLoggerRC
|
---|
188 | && pVM->vmm.s.pRCLoggerRC->offScratch >= (sizeof(pVM->vmm.s.pRCLoggerRC->achScratch)*3/4))
|
---|
189 | {
|
---|
190 | if (pVM->vmm.s.fRCLoggerFlushingDisabled)
|
---|
191 | return; /* fail quietly. */
|
---|
192 | VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Switches from guest context to host context.
|
---|
199 | *
|
---|
200 | * @param pVM The VM handle.
|
---|
201 | * @param rc The status code.
|
---|
202 | */
|
---|
203 | VMMRCDECL(void) VMMGCGuestToHost(PVM pVM, int rc)
|
---|
204 | {
|
---|
205 | pVM->vmm.s.pfnGuestToHostRC(rc);
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Execute the trap testcase.
|
---|
211 | *
|
---|
212 | * There is some common code here, that's why we're collecting them
|
---|
213 | * like this. Odd numbered variation (uArg) are executed with write
|
---|
214 | * protection (WP) enabled.
|
---|
215 | *
|
---|
216 | * @returns VINF_SUCCESS if it was a testcase setup up to continue and did so successfully.
|
---|
217 | * @returns VERR_NOT_IMPLEMENTED if the testcase wasn't implemented.
|
---|
218 | * @returns VERR_GENERAL_FAILURE if the testcase continued when it shouldn't.
|
---|
219 | *
|
---|
220 | * @param pVM The VM handle.
|
---|
221 | * @param uOperation The testcase.
|
---|
222 | * @param uArg The variation. See function description for odd / even details.
|
---|
223 | *
|
---|
224 | * @remark Careful with the trap 08 testcase and WP, it will tripple
|
---|
225 | * fault the box if the TSS, the Trap8 TSS and the fault TSS
|
---|
226 | * GDTE are in pages which are read-only.
|
---|
227 | * See bottom of SELMR3Init().
|
---|
228 | */
|
---|
229 | static int vmmGCTest(PVM pVM, unsigned uOperation, unsigned uArg)
|
---|
230 | {
|
---|
231 | /*
|
---|
232 | * Set up the testcase.
|
---|
233 | */
|
---|
234 | #if 0
|
---|
235 | switch (uOperation)
|
---|
236 | {
|
---|
237 | default:
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | #endif
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Enable WP if odd variation.
|
---|
244 | */
|
---|
245 | if (uArg & 1)
|
---|
246 | vmmGCEnableWP();
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Execute the testcase.
|
---|
250 | */
|
---|
251 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
252 | switch (uOperation)
|
---|
253 | {
|
---|
254 | //case VMMGC_DO_TESTCASE_TRAP_0:
|
---|
255 | //case VMMGC_DO_TESTCASE_TRAP_1:
|
---|
256 | //case VMMGC_DO_TESTCASE_TRAP_2:
|
---|
257 |
|
---|
258 | case VMMGC_DO_TESTCASE_TRAP_3:
|
---|
259 | {
|
---|
260 | if (uArg <= 1)
|
---|
261 | rc = vmmGCTestTrap3();
|
---|
262 | break;
|
---|
263 | }
|
---|
264 |
|
---|
265 | //case VMMGC_DO_TESTCASE_TRAP_4:
|
---|
266 | //case VMMGC_DO_TESTCASE_TRAP_5:
|
---|
267 | //case VMMGC_DO_TESTCASE_TRAP_6:
|
---|
268 | //case VMMGC_DO_TESTCASE_TRAP_7:
|
---|
269 |
|
---|
270 | case VMMGC_DO_TESTCASE_TRAP_8:
|
---|
271 | {
|
---|
272 | #ifndef DEBUG_bird /** @todo dynamic check that this won't tripple fault... */
|
---|
273 | if (uArg & 1)
|
---|
274 | break;
|
---|
275 | #endif
|
---|
276 | if (uArg <= 1)
|
---|
277 | rc = vmmGCTestTrap8();
|
---|
278 | break;
|
---|
279 | }
|
---|
280 |
|
---|
281 | //VMMGC_DO_TESTCASE_TRAP_9,
|
---|
282 | //VMMGC_DO_TESTCASE_TRAP_0A,
|
---|
283 | //VMMGC_DO_TESTCASE_TRAP_0B,
|
---|
284 | //VMMGC_DO_TESTCASE_TRAP_0C,
|
---|
285 |
|
---|
286 | case VMMGC_DO_TESTCASE_TRAP_0D:
|
---|
287 | {
|
---|
288 | if (uArg <= 1)
|
---|
289 | rc = vmmGCTestTrap0d();
|
---|
290 | break;
|
---|
291 | }
|
---|
292 |
|
---|
293 | case VMMGC_DO_TESTCASE_TRAP_0E:
|
---|
294 | {
|
---|
295 | if (uArg <= 1)
|
---|
296 | rc = vmmGCTestTrap0e();
|
---|
297 | else if (uArg == 2 || uArg == 4)
|
---|
298 | {
|
---|
299 | /*
|
---|
300 | * Test the use of a temporary #PF handler.
|
---|
301 | */
|
---|
302 | rc = TRPMGCSetTempHandler(pVM, X86_XCPT_PF, uArg != 4 ? vmmGCTestTmpPFHandler : vmmGCTestTmpPFHandlerCorruptFS);
|
---|
303 | if (RT_SUCCESS(rc))
|
---|
304 | {
|
---|
305 | rc = vmmGCTestTrap0e();
|
---|
306 |
|
---|
307 | /* in case it didn't fire. */
|
---|
308 | int rc2 = TRPMGCSetTempHandler(pVM, X86_XCPT_PF, NULL);
|
---|
309 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
310 | rc = rc2;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | break;
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Re-enable WP.
|
---|
319 | */
|
---|
320 | if (uArg & 1)
|
---|
321 | vmmGCDisableWP();
|
---|
322 |
|
---|
323 | return rc;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Temporary \#PF trap handler for the \#PF test case.
|
---|
329 | *
|
---|
330 | * @returns VBox status code (appropriate for GC return).
|
---|
331 | * In this context RT_SUCCESS means to restart the instruction.
|
---|
332 | * @param pVM VM handle.
|
---|
333 | * @param pRegFrame Trap register frame.
|
---|
334 | */
|
---|
335 | static DECLCALLBACK(int) vmmGCTestTmpPFHandler(PVM pVM, PCPUMCTXCORE pRegFrame)
|
---|
336 | {
|
---|
337 | if (pRegFrame->eip == (uintptr_t)vmmGCTestTrap0e_FaultEIP)
|
---|
338 | {
|
---|
339 | pRegFrame->eip = (uintptr_t)vmmGCTestTrap0e_ResumeEIP;
|
---|
340 | return VINF_SUCCESS;
|
---|
341 | }
|
---|
342 | return VERR_INTERNAL_ERROR;
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Temporary \#PF trap handler for the \#PF test case, this one messes up the fs
|
---|
348 | * selector.
|
---|
349 | *
|
---|
350 | * @returns VBox status code (appropriate for GC return).
|
---|
351 | * In this context RT_SUCCESS means to restart the instruction.
|
---|
352 | * @param pVM VM handle.
|
---|
353 | * @param pRegFrame Trap register frame.
|
---|
354 | */
|
---|
355 | static DECLCALLBACK(int) vmmGCTestTmpPFHandlerCorruptFS(PVM pVM, PCPUMCTXCORE pRegFrame)
|
---|
356 | {
|
---|
357 | int rc = vmmGCTestTmpPFHandler(pVM, pRegFrame);
|
---|
358 | pRegFrame->fs = 0x30;
|
---|
359 | return rc;
|
---|
360 | }
|
---|
361 |
|
---|