1 | /* $Id: EMRaw.cpp 61634 2016-06-09 18:23:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EM - Execution Monitor / Manager - software virtualization
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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_EM
|
---|
23 | #include <VBox/vmm/em.h>
|
---|
24 | #include <VBox/vmm/vmm.h>
|
---|
25 | #include <VBox/vmm/patm.h>
|
---|
26 | #include <VBox/vmm/csam.h>
|
---|
27 | #include <VBox/vmm/selm.h>
|
---|
28 | #include <VBox/vmm/trpm.h>
|
---|
29 | #include <VBox/vmm/iem.h>
|
---|
30 | #include <VBox/vmm/iom.h>
|
---|
31 | #include <VBox/vmm/dbgf.h>
|
---|
32 | #include <VBox/vmm/pgm.h>
|
---|
33 | #ifdef VBOX_WITH_REM
|
---|
34 | # include <VBox/vmm/rem.h>
|
---|
35 | #endif
|
---|
36 | #include <VBox/vmm/tm.h>
|
---|
37 | #include <VBox/vmm/mm.h>
|
---|
38 | #include <VBox/vmm/ssm.h>
|
---|
39 | #include <VBox/vmm/pdmapi.h>
|
---|
40 | #include <VBox/vmm/pdmcritsect.h>
|
---|
41 | #include <VBox/vmm/pdmqueue.h>
|
---|
42 | #include <VBox/vmm/patm.h>
|
---|
43 | #include "EMInternal.h"
|
---|
44 | #include <VBox/vmm/vm.h>
|
---|
45 | #include <VBox/vmm/gim.h>
|
---|
46 | #include <VBox/vmm/cpumdis.h>
|
---|
47 | #include <VBox/dis.h>
|
---|
48 | #include <VBox/disopcode.h>
|
---|
49 | #include <VBox/vmm/dbgf.h>
|
---|
50 | #include "VMMTracing.h"
|
---|
51 |
|
---|
52 | #include <VBox/log.h>
|
---|
53 | #include <iprt/asm.h>
|
---|
54 | #include <iprt/string.h>
|
---|
55 | #include <iprt/stream.h>
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | /*********************************************************************************************************************************
|
---|
60 | * Internal Functions *
|
---|
61 | *********************************************************************************************************************************/
|
---|
62 | static int emR3RawForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
|
---|
63 | DECLINLINE(int) emR3RawExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC = VINF_SUCCESS);
|
---|
64 | static int emR3RawGuestTrap(PVM pVM, PVMCPU pVCpu);
|
---|
65 | static int emR3RawPatchTrap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int gcret);
|
---|
66 | static int emR3RawPrivileged(PVM pVM, PVMCPU pVCpu);
|
---|
67 | static int emR3RawExecuteIOInstruction(PVM pVM, PVMCPU pVCpu);
|
---|
68 | static int emR3RawRingSwitch(PVM pVM, PVMCPU pVCpu);
|
---|
69 |
|
---|
70 | #define EMHANDLERC_WITH_PATM
|
---|
71 | #define emR3ExecuteInstruction emR3RawExecuteInstruction
|
---|
72 | #define emR3ExecuteIOInstruction emR3RawExecuteIOInstruction
|
---|
73 | #include "EMHandleRCTmpl.h"
|
---|
74 |
|
---|
75 |
|
---|
76 |
|
---|
77 | #ifdef VBOX_WITH_STATISTICS
|
---|
78 | /**
|
---|
79 | * Just a braindead function to keep track of cli addresses.
|
---|
80 | * @param pVM The cross context VM structure.
|
---|
81 | * @param pVCpu The cross context virtual CPU structure.
|
---|
82 | * @param GCPtrInstr The EIP of the cli instruction.
|
---|
83 | */
|
---|
84 | static void emR3RecordCli(PVM pVM, PVMCPU pVCpu, RTGCPTR GCPtrInstr)
|
---|
85 | {
|
---|
86 | PCLISTAT pRec;
|
---|
87 |
|
---|
88 | pRec = (PCLISTAT)RTAvlGCPtrGet(&pVCpu->em.s.pCliStatTree, GCPtrInstr);
|
---|
89 | if (!pRec)
|
---|
90 | {
|
---|
91 | /* New cli instruction; insert into the tree. */
|
---|
92 | pRec = (PCLISTAT)MMR3HeapAllocZ(pVM, MM_TAG_EM, sizeof(*pRec));
|
---|
93 | Assert(pRec);
|
---|
94 | if (!pRec)
|
---|
95 | return;
|
---|
96 | pRec->Core.Key = GCPtrInstr;
|
---|
97 |
|
---|
98 | char szCliStatName[32];
|
---|
99 | RTStrPrintf(szCliStatName, sizeof(szCliStatName), "/EM/Cli/0x%RGv", GCPtrInstr);
|
---|
100 | STAM_REG(pVM, &pRec->Counter, STAMTYPE_COUNTER, szCliStatName, STAMUNIT_OCCURENCES, "Number of times cli was executed.");
|
---|
101 |
|
---|
102 | bool fRc = RTAvlGCPtrInsert(&pVCpu->em.s.pCliStatTree, &pRec->Core);
|
---|
103 | Assert(fRc); NOREF(fRc);
|
---|
104 | }
|
---|
105 | STAM_COUNTER_INC(&pRec->Counter);
|
---|
106 | STAM_COUNTER_INC(&pVCpu->em.s.StatTotalClis);
|
---|
107 | }
|
---|
108 | #endif /* VBOX_WITH_STATISTICS */
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Resumes executing hypervisor after a debug event.
|
---|
114 | *
|
---|
115 | * This is kind of special since our current guest state is
|
---|
116 | * potentially out of sync.
|
---|
117 | *
|
---|
118 | * @returns VBox status code.
|
---|
119 | * @param pVM The cross context VM structure.
|
---|
120 | * @param pVCpu The cross context virtual CPU structure.
|
---|
121 | */
|
---|
122 | int emR3RawResumeHyper(PVM pVM, PVMCPU pVCpu)
|
---|
123 | {
|
---|
124 | int rc;
|
---|
125 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
126 | Assert(pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER);
|
---|
127 | Log(("emR3RawResumeHyper: cs:eip=%RTsel:%RGr efl=%RGr\n", pCtx->cs.Sel, pCtx->eip, pCtx->eflags));
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Resume execution.
|
---|
131 | */
|
---|
132 | CPUMRawEnter(pVCpu);
|
---|
133 | CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) | X86_EFL_RF);
|
---|
134 | rc = VMMR3ResumeHyper(pVM, pVCpu);
|
---|
135 | Log(("emR3RawResumeHyper: cs:eip=%RTsel:%RGr efl=%RGr - returned from GC with rc=%Rrc\n", pCtx->cs.Sel, pCtx->eip, pCtx->eflags, rc));
|
---|
136 | rc = CPUMRawLeave(pVCpu, rc);
|
---|
137 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Deal with the return code.
|
---|
141 | */
|
---|
142 | rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
|
---|
143 | rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
|
---|
144 | rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Steps rawmode.
|
---|
151 | *
|
---|
152 | * @returns VBox status code.
|
---|
153 | * @param pVM The cross context VM structure.
|
---|
154 | * @param pVCpu The cross context virtual CPU structure.
|
---|
155 | */
|
---|
156 | int emR3RawStep(PVM pVM, PVMCPU pVCpu)
|
---|
157 | {
|
---|
158 | Assert( pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER
|
---|
159 | || pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_RAW
|
---|
160 | || pVCpu->em.s.enmState == EMSTATE_DEBUG_GUEST_REM);
|
---|
161 | int rc;
|
---|
162 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
163 | bool fGuest = pVCpu->em.s.enmState != EMSTATE_DEBUG_HYPER;
|
---|
164 | #ifndef DEBUG_sander
|
---|
165 | Log(("emR3RawStep: cs:eip=%RTsel:%RGr efl=%RGr\n", fGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu),
|
---|
166 | fGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu), fGuest ? CPUMGetGuestEFlags(pVCpu) : CPUMGetHyperEFlags(pVCpu)));
|
---|
167 | #endif
|
---|
168 | if (fGuest)
|
---|
169 | {
|
---|
170 | /*
|
---|
171 | * Check vital forced actions, but ignore pending interrupts and timers.
|
---|
172 | */
|
---|
173 | if ( VM_FF_IS_PENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
|
---|
174 | || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
|
---|
175 | {
|
---|
176 | rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
|
---|
177 | VBOXVMM_EM_FF_RAW_RET(pVCpu, rc);
|
---|
178 | if (rc != VINF_SUCCESS)
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /*
|
---|
183 | * Set flags for single stepping.
|
---|
184 | */
|
---|
185 | CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
|
---|
186 | }
|
---|
187 | else
|
---|
188 | CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) | X86_EFL_TF | X86_EFL_RF);
|
---|
189 |
|
---|
190 | /*
|
---|
191 | * Single step.
|
---|
192 | * We do not start time or anything, if anything we should just do a few nanoseconds.
|
---|
193 | */
|
---|
194 | CPUMRawEnter(pVCpu);
|
---|
195 | do
|
---|
196 | {
|
---|
197 | if (pVCpu->em.s.enmState == EMSTATE_DEBUG_HYPER)
|
---|
198 | rc = VMMR3ResumeHyper(pVM, pVCpu);
|
---|
199 | else
|
---|
200 | rc = VMMR3RawRunGC(pVM, pVCpu);
|
---|
201 | #ifndef DEBUG_sander
|
---|
202 | Log(("emR3RawStep: cs:eip=%RTsel:%RGr efl=%RGr - GC rc %Rrc\n", fGuest ? CPUMGetGuestCS(pVCpu) : CPUMGetHyperCS(pVCpu),
|
---|
203 | fGuest ? CPUMGetGuestEIP(pVCpu) : CPUMGetHyperEIP(pVCpu), fGuest ? CPUMGetGuestEFlags(pVCpu) : CPUMGetHyperEFlags(pVCpu), rc));
|
---|
204 | #endif
|
---|
205 | } while ( rc == VINF_SUCCESS
|
---|
206 | || rc == VINF_EM_RAW_INTERRUPT);
|
---|
207 | rc = CPUMRawLeave(pVCpu, rc);
|
---|
208 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Make sure the trap flag is cleared.
|
---|
212 | * (Too bad if the guest is trying to single step too.)
|
---|
213 | */
|
---|
214 | if (fGuest)
|
---|
215 | CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
|
---|
216 | else
|
---|
217 | CPUMSetHyperEFlags(pVCpu, CPUMGetHyperEFlags(pVCpu) & ~X86_EFL_TF);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * Deal with the return codes.
|
---|
221 | */
|
---|
222 | rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
|
---|
223 | rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
|
---|
224 | rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
|
---|
225 | return rc;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | #ifdef DEBUG
|
---|
230 |
|
---|
231 |
|
---|
232 | int emR3SingleStepExecRaw(PVM pVM, PVMCPU pVCpu, uint32_t cIterations)
|
---|
233 | {
|
---|
234 | int rc = VINF_SUCCESS;
|
---|
235 | EMSTATE enmOldState = pVCpu->em.s.enmState;
|
---|
236 | pVCpu->em.s.enmState = EMSTATE_DEBUG_GUEST_RAW;
|
---|
237 |
|
---|
238 | Log(("Single step BEGIN:\n"));
|
---|
239 | for (uint32_t i = 0; i < cIterations; i++)
|
---|
240 | {
|
---|
241 | DBGFR3PrgStep(pVCpu);
|
---|
242 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "RSS");
|
---|
243 | rc = emR3RawStep(pVM, pVCpu);
|
---|
244 | if ( rc != VINF_SUCCESS
|
---|
245 | && rc != VINF_EM_DBG_STEPPED)
|
---|
246 | break;
|
---|
247 | }
|
---|
248 | Log(("Single step END: rc=%Rrc\n", rc));
|
---|
249 | CPUMSetGuestEFlags(pVCpu, CPUMGetGuestEFlags(pVCpu) & ~X86_EFL_TF);
|
---|
250 | pVCpu->em.s.enmState = enmOldState;
|
---|
251 | return rc;
|
---|
252 | }
|
---|
253 |
|
---|
254 | #endif /* DEBUG */
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Executes one (or perhaps a few more) instruction(s).
|
---|
259 | *
|
---|
260 | * @returns VBox status code suitable for EM.
|
---|
261 | *
|
---|
262 | * @param pVM The cross context VM structure.
|
---|
263 | * @param pVCpu The cross context virtual CPU structure.
|
---|
264 | * @param rcGC GC return code
|
---|
265 | * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
|
---|
266 | * instruction and prefix the log output with this text.
|
---|
267 | */
|
---|
268 | #if defined(LOG_ENABLED) || defined(DOXYGEN_RUNNING)
|
---|
269 | static int emR3RawExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcGC, const char *pszPrefix)
|
---|
270 | #else
|
---|
271 | static int emR3RawExecuteInstructionWorker(PVM pVM, PVMCPU pVCpu, int rcGC)
|
---|
272 | #endif
|
---|
273 | {
|
---|
274 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
275 | int rc;
|
---|
276 |
|
---|
277 | #ifdef LOG_ENABLED
|
---|
278 | /*
|
---|
279 | * Disassemble the instruction if requested.
|
---|
280 | */
|
---|
281 | if (pszPrefix)
|
---|
282 | {
|
---|
283 | DBGFR3_INFO_LOG(pVM, pVCpu, "cpumguest", pszPrefix);
|
---|
284 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, pszPrefix);
|
---|
285 | }
|
---|
286 | #endif /* LOG_ENABLED */
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * PATM is making life more interesting.
|
---|
290 | * We cannot hand anything to REM which has an EIP inside patch code. So, we'll
|
---|
291 | * tell PATM there is a trap in this code and have it take the appropriate actions
|
---|
292 | * to allow us execute the code in REM.
|
---|
293 | */
|
---|
294 | if (PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
295 | {
|
---|
296 | Log(("emR3RawExecuteInstruction: In patch block. eip=%RRv\n", (RTRCPTR)pCtx->eip));
|
---|
297 |
|
---|
298 | RTGCPTR uNewEip;
|
---|
299 | rc = PATMR3HandleTrap(pVM, pCtx, pCtx->eip, &uNewEip);
|
---|
300 | switch (rc)
|
---|
301 | {
|
---|
302 | /*
|
---|
303 | * It's not very useful to emulate a single instruction and then go back to raw
|
---|
304 | * mode; just execute the whole block until IF is set again.
|
---|
305 | */
|
---|
306 | case VINF_SUCCESS:
|
---|
307 | Log(("emR3RawExecuteInstruction: Executing instruction starting at new address %RGv IF=%d VMIF=%x\n",
|
---|
308 | uNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
|
---|
309 | pCtx->eip = uNewEip;
|
---|
310 | Assert(pCtx->eip);
|
---|
311 |
|
---|
312 | if (pCtx->eflags.Bits.u1IF)
|
---|
313 | {
|
---|
314 | /*
|
---|
315 | * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
|
---|
316 | */
|
---|
317 | Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
|
---|
318 | return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
|
---|
319 | }
|
---|
320 | else if (rcGC == VINF_PATM_PENDING_IRQ_AFTER_IRET)
|
---|
321 | {
|
---|
322 | /* special case: iret, that sets IF, detected a pending irq/event */
|
---|
323 | return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIRET");
|
---|
324 | }
|
---|
325 | return VINF_EM_RESCHEDULE_REM;
|
---|
326 |
|
---|
327 | /*
|
---|
328 | * One instruction.
|
---|
329 | */
|
---|
330 | case VINF_PATCH_EMULATE_INSTR:
|
---|
331 | Log(("emR3RawExecuteInstruction: Emulate patched instruction at %RGv IF=%d VMIF=%x\n",
|
---|
332 | uNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
|
---|
333 | pCtx->eip = uNewEip;
|
---|
334 | return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * The patch was disabled, hand it to the REM.
|
---|
338 | */
|
---|
339 | case VERR_PATCH_DISABLED:
|
---|
340 | Log(("emR3RawExecuteInstruction: Disabled patch -> new eip %RGv IF=%d VMIF=%x\n",
|
---|
341 | uNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
|
---|
342 | pCtx->eip = uNewEip;
|
---|
343 | if (pCtx->eflags.Bits.u1IF)
|
---|
344 | {
|
---|
345 | /*
|
---|
346 | * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
|
---|
347 | */
|
---|
348 | Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
|
---|
349 | return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
|
---|
350 | }
|
---|
351 | return VINF_EM_RESCHEDULE_REM;
|
---|
352 |
|
---|
353 | /* Force continued patch exection; usually due to write monitored stack. */
|
---|
354 | case VINF_PATCH_CONTINUE:
|
---|
355 | return VINF_SUCCESS;
|
---|
356 |
|
---|
357 | default:
|
---|
358 | AssertReleaseMsgFailed(("Unknown return code %Rrc from PATMR3HandleTrap\n", rc));
|
---|
359 | return VERR_IPE_UNEXPECTED_STATUS;
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | /*
|
---|
365 | * Use IEM and fallback on REM if the functionality is missing.
|
---|
366 | * Once IEM gets mature enough, nothing should ever fall back.
|
---|
367 | */
|
---|
368 | #define VBOX_WITH_FIRST_IEM_STEP_B
|
---|
369 | #if defined(VBOX_WITH_FIRST_IEM_STEP_B) || !defined(VBOX_WITH_REM)
|
---|
370 | Log(("EMINS: %04x:%RGv RSP=%RGv\n", pCtx->cs.Sel, (RTGCPTR)pCtx->rip, (RTGCPTR)pCtx->rsp));
|
---|
371 | STAM_PROFILE_START(&pVCpu->em.s.StatIEMEmu, a);
|
---|
372 | rc = VBOXSTRICTRC_TODO(IEMExecOne(pVCpu));
|
---|
373 | STAM_PROFILE_STOP(&pVCpu->em.s.StatIEMEmu, a);
|
---|
374 | if (RT_SUCCESS(rc))
|
---|
375 | {
|
---|
376 | if (rc == VINF_SUCCESS || rc == VINF_EM_RESCHEDULE)
|
---|
377 | rc = VINF_EM_RESCHEDULE;
|
---|
378 | }
|
---|
379 | else if ( rc == VERR_IEM_ASPECT_NOT_IMPLEMENTED
|
---|
380 | || rc == VERR_IEM_INSTR_NOT_IMPLEMENTED)
|
---|
381 | #endif
|
---|
382 | {
|
---|
383 | #ifdef VBOX_WITH_REM
|
---|
384 | STAM_PROFILE_START(&pVCpu->em.s.StatREMEmu, b);
|
---|
385 | # ifndef VBOX_WITH_FIRST_IEM_STEP_B
|
---|
386 | Log(("EMINS[rem]: %04x:%RGv RSP=%RGv\n", pCtx->cs.Sel, (RTGCPTR)pCtx->rip, (RTGCPTR)pCtx->rsp));
|
---|
387 | //# elif defined(DEBUG_bird)
|
---|
388 | // AssertFailed();
|
---|
389 | # endif
|
---|
390 | EMRemLock(pVM);
|
---|
391 | /* Flush the recompiler TLB if the VCPU has changed. */
|
---|
392 | if (pVM->em.s.idLastRemCpu != pVCpu->idCpu)
|
---|
393 | CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
|
---|
394 | pVM->em.s.idLastRemCpu = pVCpu->idCpu;
|
---|
395 |
|
---|
396 | rc = REMR3EmulateInstruction(pVM, pVCpu);
|
---|
397 | EMRemUnlock(pVM);
|
---|
398 | STAM_PROFILE_STOP(&pVCpu->em.s.StatREMEmu, b);
|
---|
399 | #else /* !VBOX_WITH_REM */
|
---|
400 | NOREF(pVM);
|
---|
401 | #endif /* !VBOX_WITH_REM */
|
---|
402 | }
|
---|
403 | return rc;
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * Executes one (or perhaps a few more) instruction(s).
|
---|
409 | * This is just a wrapper for discarding pszPrefix in non-logging builds.
|
---|
410 | *
|
---|
411 | * @returns VBox status code suitable for EM.
|
---|
412 | * @param pVM The cross context VM structure.
|
---|
413 | * @param pVCpu The cross context virtual CPU structure.
|
---|
414 | * @param pszPrefix Disassembly prefix. If not NULL we'll disassemble the
|
---|
415 | * instruction and prefix the log output with this text.
|
---|
416 | * @param rcGC GC return code
|
---|
417 | */
|
---|
418 | DECLINLINE(int) emR3RawExecuteInstruction(PVM pVM, PVMCPU pVCpu, const char *pszPrefix, int rcGC)
|
---|
419 | {
|
---|
420 | #ifdef LOG_ENABLED
|
---|
421 | return emR3RawExecuteInstructionWorker(pVM, pVCpu, rcGC, pszPrefix);
|
---|
422 | #else
|
---|
423 | return emR3RawExecuteInstructionWorker(pVM, pVCpu, rcGC);
|
---|
424 | #endif
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Executes one (or perhaps a few more) IO instruction(s).
|
---|
429 | *
|
---|
430 | * @returns VBox status code suitable for EM.
|
---|
431 | * @param pVM The cross context VM structure.
|
---|
432 | * @param pVCpu The cross context virtual CPU structure.
|
---|
433 | */
|
---|
434 | static int emR3RawExecuteIOInstruction(PVM pVM, PVMCPU pVCpu)
|
---|
435 | {
|
---|
436 | STAM_PROFILE_START(&pVCpu->em.s.StatIOEmu, a);
|
---|
437 |
|
---|
438 | /* Hand it over to the interpreter. */
|
---|
439 | VBOXSTRICTRC rcStrict = IEMExecOne(pVCpu);
|
---|
440 | LogFlow(("emR3RawExecuteIOInstruction: %Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
|
---|
441 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatIoIem);
|
---|
442 | STAM_PROFILE_STOP(&pVCpu->em.s.StatIOEmu, a);
|
---|
443 | return VBOXSTRICTRC_TODO(rcStrict);
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Handle a guest context trap.
|
---|
449 | *
|
---|
450 | * @returns VBox status code suitable for EM.
|
---|
451 | * @param pVM The cross context VM structure.
|
---|
452 | * @param pVCpu The cross context virtual CPU structure.
|
---|
453 | */
|
---|
454 | static int emR3RawGuestTrap(PVM pVM, PVMCPU pVCpu)
|
---|
455 | {
|
---|
456 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
457 |
|
---|
458 | /*
|
---|
459 | * Get the trap info.
|
---|
460 | */
|
---|
461 | uint8_t u8TrapNo;
|
---|
462 | TRPMEVENT enmType;
|
---|
463 | RTGCUINT uErrorCode;
|
---|
464 | RTGCUINTPTR uCR2;
|
---|
465 | int rc = TRPMQueryTrapAll(pVCpu, &u8TrapNo, &enmType, &uErrorCode, &uCR2, NULL /* pu8InstrLen */);
|
---|
466 | if (RT_FAILURE(rc))
|
---|
467 | {
|
---|
468 | AssertReleaseMsgFailed(("No trap! (rc=%Rrc)\n", rc));
|
---|
469 | return rc;
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 | #if 1 /* Experimental: Review, disable if it causes trouble. */
|
---|
474 | /*
|
---|
475 | * Handle traps in patch code first.
|
---|
476 | *
|
---|
477 | * We catch a few of these cases in RC before returning to R3 (#PF, #GP, #BP)
|
---|
478 | * but several traps isn't handled specially by TRPM in RC and we end up here
|
---|
479 | * instead. One example is #DE.
|
---|
480 | */
|
---|
481 | uint32_t uCpl = CPUMGetGuestCPL(pVCpu);
|
---|
482 | if ( uCpl == 0
|
---|
483 | && PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
484 | {
|
---|
485 | LogFlow(("emR3RawGuestTrap: trap %#x in patch code; eip=%08x\n", u8TrapNo, pCtx->eip));
|
---|
486 | return emR3RawPatchTrap(pVM, pVCpu, pCtx, rc);
|
---|
487 | }
|
---|
488 | #endif
|
---|
489 |
|
---|
490 | /*
|
---|
491 | * If the guest gate is marked unpatched, then we will check again if we can patch it.
|
---|
492 | * (This assumes that we've already tried and failed to dispatch the trap in
|
---|
493 | * RC for the gates that already has been patched. Which is true for most high
|
---|
494 | * volume traps, because these are handled specially, but not for odd ones like #DE.)
|
---|
495 | */
|
---|
496 | if (TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) == TRPM_INVALID_HANDLER)
|
---|
497 | {
|
---|
498 | CSAMR3CheckGates(pVM, u8TrapNo, 1);
|
---|
499 | Log(("emR3RawHandleRC: recheck gate %x -> valid=%d\n", u8TrapNo, TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) != TRPM_INVALID_HANDLER));
|
---|
500 |
|
---|
501 | /* If it was successful, then we could go back to raw mode. */
|
---|
502 | if (TRPMR3GetGuestTrapHandler(pVM, u8TrapNo) != TRPM_INVALID_HANDLER)
|
---|
503 | {
|
---|
504 | /* Must check pending forced actions as our IDT or GDT might be out of sync. */
|
---|
505 | rc = EMR3CheckRawForcedActions(pVM, pVCpu);
|
---|
506 | AssertRCReturn(rc, rc);
|
---|
507 |
|
---|
508 | TRPMERRORCODE enmError = uErrorCode != ~0U
|
---|
509 | ? TRPM_TRAP_HAS_ERRORCODE
|
---|
510 | : TRPM_TRAP_NO_ERRORCODE;
|
---|
511 | rc = TRPMForwardTrap(pVCpu, CPUMCTX2CORE(pCtx), u8TrapNo, uErrorCode, enmError, TRPM_TRAP, -1);
|
---|
512 | if (rc == VINF_SUCCESS /* Don't use RT_SUCCESS */)
|
---|
513 | {
|
---|
514 | TRPMResetTrap(pVCpu);
|
---|
515 | return VINF_EM_RESCHEDULE_RAW;
|
---|
516 | }
|
---|
517 | AssertMsg(rc == VINF_EM_RAW_GUEST_TRAP, ("%Rrc\n", rc));
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | /*
|
---|
522 | * Scan kernel code that traps; we might not get another chance.
|
---|
523 | */
|
---|
524 | /** @todo move this up before the dispatching? */
|
---|
525 | if ( (pCtx->ss.Sel & X86_SEL_RPL) <= 1
|
---|
526 | && !pCtx->eflags.Bits.u1VM)
|
---|
527 | {
|
---|
528 | Assert(!PATMIsPatchGCAddr(pVM, pCtx->eip));
|
---|
529 | CSAMR3CheckCodeEx(pVM, pCtx, pCtx->eip);
|
---|
530 | }
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Trap specific handling.
|
---|
534 | */
|
---|
535 | if (u8TrapNo == 6) /* (#UD) Invalid opcode. */
|
---|
536 | {
|
---|
537 | /*
|
---|
538 | * If MONITOR & MWAIT are supported, then interpret them here.
|
---|
539 | */
|
---|
540 | DISCPUSTATE cpu;
|
---|
541 | rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &cpu, "Guest Trap (#UD): ");
|
---|
542 | if ( RT_SUCCESS(rc)
|
---|
543 | && (cpu.pCurInstr->uOpcode == OP_MONITOR || cpu.pCurInstr->uOpcode == OP_MWAIT))
|
---|
544 | {
|
---|
545 | uint32_t u32Dummy, u32Features, u32ExtFeatures;
|
---|
546 | CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &u32ExtFeatures, &u32Features);
|
---|
547 | if (u32ExtFeatures & X86_CPUID_FEATURE_ECX_MONITOR)
|
---|
548 | {
|
---|
549 | rc = TRPMResetTrap(pVCpu);
|
---|
550 | AssertRC(rc);
|
---|
551 |
|
---|
552 | rc = VBOXSTRICTRC_TODO(EMInterpretInstructionDisasState(pVCpu, &cpu, CPUMCTX2CORE(pCtx), 0, EMCODETYPE_SUPERVISOR));
|
---|
553 | if (RT_SUCCESS(rc))
|
---|
554 | return rc;
|
---|
555 | return emR3RawExecuteInstruction(pVM, pVCpu, "Monitor: ");
|
---|
556 | }
|
---|
557 | }
|
---|
558 | }
|
---|
559 | else if (u8TrapNo == 13) /* (#GP) Privileged exception */
|
---|
560 | {
|
---|
561 | /*
|
---|
562 | * Handle I/O bitmap?
|
---|
563 | */
|
---|
564 | /** @todo We're not supposed to be here with a false guest trap concerning
|
---|
565 | * I/O access. We can easily handle those in RC. */
|
---|
566 | DISCPUSTATE cpu;
|
---|
567 | rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &cpu, "Guest Trap: ");
|
---|
568 | if ( RT_SUCCESS(rc)
|
---|
569 | && (cpu.pCurInstr->fOpType & DISOPTYPE_PORTIO))
|
---|
570 | {
|
---|
571 | /*
|
---|
572 | * We should really check the TSS for the IO bitmap, but it's not like this
|
---|
573 | * lazy approach really makes things worse.
|
---|
574 | */
|
---|
575 | rc = TRPMResetTrap(pVCpu);
|
---|
576 | AssertRC(rc);
|
---|
577 | return emR3RawExecuteInstruction(pVM, pVCpu, "IO Guest Trap: ");
|
---|
578 | }
|
---|
579 | }
|
---|
580 |
|
---|
581 | #ifdef LOG_ENABLED
|
---|
582 | DBGFR3_INFO_LOG(pVM, pVCpu, "cpumguest", "Guest trap");
|
---|
583 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Guest trap");
|
---|
584 |
|
---|
585 | /* Get guest page information. */
|
---|
586 | uint64_t fFlags = 0;
|
---|
587 | RTGCPHYS GCPhys = 0;
|
---|
588 | int rc2 = PGMGstGetPage(pVCpu, uCR2, &fFlags, &GCPhys);
|
---|
589 | Log(("emR3RawGuestTrap: cs:eip=%04x:%08x: trap=%02x err=%08x cr2=%08x cr0=%08x%s: Phys=%RGp fFlags=%08llx %s %s %s%s rc2=%d\n",
|
---|
590 | pCtx->cs.Sel, pCtx->eip, u8TrapNo, uErrorCode, uCR2, (uint32_t)pCtx->cr0,
|
---|
591 | (enmType == TRPM_SOFTWARE_INT) ? " software" : "", GCPhys, fFlags,
|
---|
592 | fFlags & X86_PTE_P ? "P " : "NP", fFlags & X86_PTE_US ? "U" : "S",
|
---|
593 | fFlags & X86_PTE_RW ? "RW" : "R0", fFlags & X86_PTE_G ? " G" : "", rc2));
|
---|
594 | #endif
|
---|
595 |
|
---|
596 | /*
|
---|
597 | * #PG has CR2.
|
---|
598 | * (Because of stuff like above we must set CR2 in a delayed fashion.)
|
---|
599 | */
|
---|
600 | if (u8TrapNo == 14 /* #PG */)
|
---|
601 | pCtx->cr2 = uCR2;
|
---|
602 |
|
---|
603 | return VINF_EM_RESCHEDULE_REM;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | /**
|
---|
608 | * Handle a ring switch trap.
|
---|
609 | * Need to do statistics and to install patches. The result is going to REM.
|
---|
610 | *
|
---|
611 | * @returns VBox status code suitable for EM.
|
---|
612 | * @param pVM The cross context VM structure.
|
---|
613 | * @param pVCpu The cross context virtual CPU structure.
|
---|
614 | */
|
---|
615 | static int emR3RawRingSwitch(PVM pVM, PVMCPU pVCpu)
|
---|
616 | {
|
---|
617 | int rc;
|
---|
618 | DISCPUSTATE Cpu;
|
---|
619 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
620 |
|
---|
621 | /*
|
---|
622 | * sysenter, syscall & callgate
|
---|
623 | */
|
---|
624 | rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "RSWITCH: ");
|
---|
625 | if (RT_SUCCESS(rc))
|
---|
626 | {
|
---|
627 | if (Cpu.pCurInstr->uOpcode == OP_SYSENTER)
|
---|
628 | {
|
---|
629 | if (pCtx->SysEnter.cs != 0)
|
---|
630 | {
|
---|
631 | rc = PATMR3InstallPatch(pVM, SELMToFlat(pVM, DISSELREG_CS, CPUMCTX2CORE(pCtx), pCtx->eip),
|
---|
632 | CPUMGetGuestCodeBits(pVCpu) == 32 ? PATMFL_CODE32 : 0);
|
---|
633 | if (RT_SUCCESS(rc))
|
---|
634 | {
|
---|
635 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Patched sysenter instruction");
|
---|
636 | return VINF_EM_RESCHEDULE_RAW;
|
---|
637 | }
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 | #ifdef VBOX_WITH_STATISTICS
|
---|
642 | switch (Cpu.pCurInstr->uOpcode)
|
---|
643 | {
|
---|
644 | case OP_SYSENTER:
|
---|
645 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysEnter);
|
---|
646 | break;
|
---|
647 | case OP_SYSEXIT:
|
---|
648 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysExit);
|
---|
649 | break;
|
---|
650 | case OP_SYSCALL:
|
---|
651 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysCall);
|
---|
652 | break;
|
---|
653 | case OP_SYSRET:
|
---|
654 | STAM_COUNTER_INC(&pVCpu->em.s.CTX_SUFF(pStats)->StatSysRet);
|
---|
655 | break;
|
---|
656 | }
|
---|
657 | #endif
|
---|
658 | }
|
---|
659 | else
|
---|
660 | AssertRC(rc);
|
---|
661 |
|
---|
662 | /* go to the REM to emulate a single instruction */
|
---|
663 | return emR3RawExecuteInstruction(pVM, pVCpu, "RSWITCH: ");
|
---|
664 | }
|
---|
665 |
|
---|
666 |
|
---|
667 | /**
|
---|
668 | * Handle a trap (\#PF or \#GP) in patch code
|
---|
669 | *
|
---|
670 | * @returns VBox status code suitable for EM.
|
---|
671 | * @param pVM The cross context VM structure.
|
---|
672 | * @param pVCpu The cross context virtual CPU structure.
|
---|
673 | * @param pCtx Pointer to the guest CPU context.
|
---|
674 | * @param gcret GC return code.
|
---|
675 | */
|
---|
676 | static int emR3RawPatchTrap(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int gcret)
|
---|
677 | {
|
---|
678 | uint8_t u8TrapNo;
|
---|
679 | int rc;
|
---|
680 | TRPMEVENT enmType;
|
---|
681 | RTGCUINT uErrorCode;
|
---|
682 | RTGCUINTPTR uCR2;
|
---|
683 |
|
---|
684 | Assert(PATMIsPatchGCAddr(pVM, pCtx->eip));
|
---|
685 |
|
---|
686 | if (gcret == VINF_PATM_PATCH_INT3)
|
---|
687 | {
|
---|
688 | u8TrapNo = 3;
|
---|
689 | uCR2 = 0;
|
---|
690 | uErrorCode = 0;
|
---|
691 | }
|
---|
692 | else if (gcret == VINF_PATM_PATCH_TRAP_GP)
|
---|
693 | {
|
---|
694 | /* No active trap in this case. Kind of ugly. */
|
---|
695 | u8TrapNo = X86_XCPT_GP;
|
---|
696 | uCR2 = 0;
|
---|
697 | uErrorCode = 0;
|
---|
698 | }
|
---|
699 | else
|
---|
700 | {
|
---|
701 | rc = TRPMQueryTrapAll(pVCpu, &u8TrapNo, &enmType, &uErrorCode, &uCR2, NULL /* pu8InstrLen */);
|
---|
702 | if (RT_FAILURE(rc))
|
---|
703 | {
|
---|
704 | AssertReleaseMsgFailed(("emR3RawPatchTrap: no trap! (rc=%Rrc) gcret=%Rrc\n", rc, gcret));
|
---|
705 | return rc;
|
---|
706 | }
|
---|
707 | /* Reset the trap as we'll execute the original instruction again. */
|
---|
708 | TRPMResetTrap(pVCpu);
|
---|
709 | }
|
---|
710 |
|
---|
711 | /*
|
---|
712 | * Deal with traps inside patch code.
|
---|
713 | * (This code won't run outside GC.)
|
---|
714 | */
|
---|
715 | if (u8TrapNo != 1)
|
---|
716 | {
|
---|
717 | #ifdef LOG_ENABLED
|
---|
718 | DBGFR3_INFO_LOG(pVM, pVCpu, "cpumguest", "Trap in patch code");
|
---|
719 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Patch code");
|
---|
720 |
|
---|
721 | DISCPUSTATE Cpu;
|
---|
722 | rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->eip, &Cpu, "Patch code: ");
|
---|
723 | if ( RT_SUCCESS(rc)
|
---|
724 | && Cpu.pCurInstr->uOpcode == OP_IRET)
|
---|
725 | {
|
---|
726 | uint32_t eip, selCS, uEFlags;
|
---|
727 |
|
---|
728 | /* Iret crashes are bad as we have already changed the flags on the stack */
|
---|
729 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &eip, pCtx->esp, 4);
|
---|
730 | rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selCS, pCtx->esp+4, 4);
|
---|
731 | rc |= PGMPhysSimpleReadGCPtr(pVCpu, &uEFlags, pCtx->esp+8, 4);
|
---|
732 | if (rc == VINF_SUCCESS)
|
---|
733 | {
|
---|
734 | if ( (uEFlags & X86_EFL_VM)
|
---|
735 | || (selCS & X86_SEL_RPL) == 3)
|
---|
736 | {
|
---|
737 | uint32_t selSS, esp;
|
---|
738 |
|
---|
739 | rc |= PGMPhysSimpleReadGCPtr(pVCpu, &esp, pCtx->esp + 12, 4);
|
---|
740 | rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selSS, pCtx->esp + 16, 4);
|
---|
741 |
|
---|
742 | if (uEFlags & X86_EFL_VM)
|
---|
743 | {
|
---|
744 | uint32_t selDS, selES, selFS, selGS;
|
---|
745 | rc = PGMPhysSimpleReadGCPtr(pVCpu, &selES, pCtx->esp + 20, 4);
|
---|
746 | rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selDS, pCtx->esp + 24, 4);
|
---|
747 | rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selFS, pCtx->esp + 28, 4);
|
---|
748 | rc |= PGMPhysSimpleReadGCPtr(pVCpu, &selGS, pCtx->esp + 32, 4);
|
---|
749 | if (rc == VINF_SUCCESS)
|
---|
750 | {
|
---|
751 | Log(("Patch code: IRET->VM stack frame: return address %04X:%08RX32 eflags=%08x ss:esp=%04X:%08RX32\n", selCS, eip, uEFlags, selSS, esp));
|
---|
752 | Log(("Patch code: IRET->VM stack frame: DS=%04X ES=%04X FS=%04X GS=%04X\n", selDS, selES, selFS, selGS));
|
---|
753 | }
|
---|
754 | }
|
---|
755 | else
|
---|
756 | Log(("Patch code: IRET stack frame: return address %04X:%08RX32 eflags=%08x ss:esp=%04X:%08RX32\n", selCS, eip, uEFlags, selSS, esp));
|
---|
757 | }
|
---|
758 | else
|
---|
759 | Log(("Patch code: IRET stack frame: return address %04X:%08RX32 eflags=%08x\n", selCS, eip, uEFlags));
|
---|
760 | }
|
---|
761 | }
|
---|
762 | #endif /* LOG_ENABLED */
|
---|
763 | Log(("emR3RawPatchTrap: in patch: eip=%08x: trap=%02x err=%08x cr2=%08x cr0=%08x\n",
|
---|
764 | pCtx->eip, u8TrapNo, uErrorCode, uCR2, (uint32_t)pCtx->cr0));
|
---|
765 |
|
---|
766 | RTGCPTR uNewEip;
|
---|
767 | rc = PATMR3HandleTrap(pVM, pCtx, pCtx->eip, &uNewEip);
|
---|
768 | switch (rc)
|
---|
769 | {
|
---|
770 | /*
|
---|
771 | * Execute the faulting instruction.
|
---|
772 | */
|
---|
773 | case VINF_SUCCESS:
|
---|
774 | {
|
---|
775 | /** @todo execute a whole block */
|
---|
776 | Log(("emR3RawPatchTrap: Executing faulting instruction at new address %RGv\n", uNewEip));
|
---|
777 | if (!(pVCpu->em.s.pPatmGCState->uVMFlags & X86_EFL_IF))
|
---|
778 | Log(("emR3RawPatchTrap: Virtual IF flag disabled!!\n"));
|
---|
779 |
|
---|
780 | pCtx->eip = uNewEip;
|
---|
781 | AssertRelease(pCtx->eip);
|
---|
782 |
|
---|
783 | if (pCtx->eflags.Bits.u1IF)
|
---|
784 | {
|
---|
785 | /* Windows XP lets irets fault intentionally and then takes action based on the opcode; an
|
---|
786 | * int3 patch overwrites it and leads to blue screens. Remove the patch in this case.
|
---|
787 | */
|
---|
788 | if ( u8TrapNo == X86_XCPT_GP
|
---|
789 | && PATMIsInt3Patch(pVM, pCtx->eip, NULL, NULL))
|
---|
790 | {
|
---|
791 | /** @todo move to PATMR3HandleTrap */
|
---|
792 | Log(("Possible Windows XP iret fault at %08RX32\n", pCtx->eip));
|
---|
793 | PATMR3RemovePatch(pVM, pCtx->eip);
|
---|
794 | }
|
---|
795 |
|
---|
796 | /** @todo Knoppix 5 regression when returning VINF_SUCCESS here and going back to raw mode. */
|
---|
797 | /* Note: possibly because a reschedule is required (e.g. iret to V86 code) */
|
---|
798 |
|
---|
799 | return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
|
---|
800 | /* Interrupts are enabled; just go back to the original instruction.
|
---|
801 | return VINF_SUCCESS; */
|
---|
802 | }
|
---|
803 | return VINF_EM_RESCHEDULE_REM;
|
---|
804 | }
|
---|
805 |
|
---|
806 | /*
|
---|
807 | * One instruction.
|
---|
808 | */
|
---|
809 | case VINF_PATCH_EMULATE_INSTR:
|
---|
810 | Log(("emR3RawPatchTrap: Emulate patched instruction at %RGv IF=%d VMIF=%x\n",
|
---|
811 | uNewEip, pCtx->eflags.Bits.u1IF, pVCpu->em.s.pPatmGCState->uVMFlags));
|
---|
812 | pCtx->eip = uNewEip;
|
---|
813 | AssertRelease(pCtx->eip);
|
---|
814 | return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHEMUL: ");
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * The patch was disabled, hand it to the REM.
|
---|
818 | */
|
---|
819 | case VERR_PATCH_DISABLED:
|
---|
820 | if (!(pVCpu->em.s.pPatmGCState->uVMFlags & X86_EFL_IF))
|
---|
821 | Log(("emR3RawPatchTrap: Virtual IF flag disabled!!\n"));
|
---|
822 | pCtx->eip = uNewEip;
|
---|
823 | AssertRelease(pCtx->eip);
|
---|
824 |
|
---|
825 | if (pCtx->eflags.Bits.u1IF)
|
---|
826 | {
|
---|
827 | /*
|
---|
828 | * The last instruction in the patch block needs to be executed!! (sti/sysexit for example)
|
---|
829 | */
|
---|
830 | Log(("PATCH: IF=1 -> emulate last instruction as it can't be interrupted!!\n"));
|
---|
831 | return emR3RawExecuteInstruction(pVM, pVCpu, "PATCHIR");
|
---|
832 | }
|
---|
833 | return VINF_EM_RESCHEDULE_REM;
|
---|
834 |
|
---|
835 | /* Force continued patch exection; usually due to write monitored stack. */
|
---|
836 | case VINF_PATCH_CONTINUE:
|
---|
837 | return VINF_SUCCESS;
|
---|
838 |
|
---|
839 | /*
|
---|
840 | * Anything else is *fatal*.
|
---|
841 | */
|
---|
842 | default:
|
---|
843 | AssertReleaseMsgFailed(("Unknown return code %Rrc from PATMR3HandleTrap!\n", rc));
|
---|
844 | return VERR_IPE_UNEXPECTED_STATUS;
|
---|
845 | }
|
---|
846 | }
|
---|
847 | return VINF_SUCCESS;
|
---|
848 | }
|
---|
849 |
|
---|
850 |
|
---|
851 | /**
|
---|
852 | * Handle a privileged instruction.
|
---|
853 | *
|
---|
854 | * @returns VBox status code suitable for EM.
|
---|
855 | * @param pVM The cross context VM structure.
|
---|
856 | * @param pVCpu The cross context virtual CPU structure.
|
---|
857 | */
|
---|
858 | static int emR3RawPrivileged(PVM pVM, PVMCPU pVCpu)
|
---|
859 | {
|
---|
860 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
861 |
|
---|
862 | Assert(!pCtx->eflags.Bits.u1VM);
|
---|
863 |
|
---|
864 | if (PATMIsEnabled(pVM))
|
---|
865 | {
|
---|
866 | /*
|
---|
867 | * Check if in patch code.
|
---|
868 | */
|
---|
869 | if (PATMR3IsInsidePatchJump(pVM, pCtx->eip, NULL))
|
---|
870 | {
|
---|
871 | #ifdef LOG_ENABLED
|
---|
872 | DBGFR3_INFO_LOG(pVM, pVCpu, "cpumguest", "PRIV");
|
---|
873 | #endif
|
---|
874 | AssertMsgFailed(("FATAL ERROR: executing random instruction inside generated patch jump %08x\n", pCtx->eip));
|
---|
875 | return VERR_EM_RAW_PATCH_CONFLICT;
|
---|
876 | }
|
---|
877 | if ( (pCtx->ss.Sel & X86_SEL_RPL) == 0
|
---|
878 | && !pCtx->eflags.Bits.u1VM
|
---|
879 | && !PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
880 | {
|
---|
881 | int rc = PATMR3InstallPatch(pVM, SELMToFlat(pVM, DISSELREG_CS, CPUMCTX2CORE(pCtx), pCtx->eip),
|
---|
882 | CPUMGetGuestCodeBits(pVCpu) == 32 ? PATMFL_CODE32 : 0);
|
---|
883 | if (RT_SUCCESS(rc))
|
---|
884 | {
|
---|
885 | #ifdef LOG_ENABLED
|
---|
886 | DBGFR3_INFO_LOG(pVM, pVCpu, "cpumguest", "PRIV");
|
---|
887 | #endif
|
---|
888 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Patched privileged instruction");
|
---|
889 | return VINF_SUCCESS;
|
---|
890 | }
|
---|
891 | }
|
---|
892 | }
|
---|
893 |
|
---|
894 | #ifdef LOG_ENABLED
|
---|
895 | if (!PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
896 | {
|
---|
897 | DBGFR3_INFO_LOG(pVM, pVCpu, "cpumguest", "PRIV");
|
---|
898 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Privileged instr");
|
---|
899 | }
|
---|
900 | #endif
|
---|
901 |
|
---|
902 | /*
|
---|
903 | * Instruction statistics and logging.
|
---|
904 | */
|
---|
905 | DISCPUSTATE Cpu;
|
---|
906 | int rc;
|
---|
907 |
|
---|
908 | rc = CPUMR3DisasmInstrCPU(pVM, pVCpu, pCtx, pCtx->rip, &Cpu, "PRIV: ");
|
---|
909 | if (RT_SUCCESS(rc))
|
---|
910 | {
|
---|
911 | #ifdef VBOX_WITH_STATISTICS
|
---|
912 | PEMSTATS pStats = pVCpu->em.s.CTX_SUFF(pStats);
|
---|
913 | switch (Cpu.pCurInstr->uOpcode)
|
---|
914 | {
|
---|
915 | case OP_INVLPG:
|
---|
916 | STAM_COUNTER_INC(&pStats->StatInvlpg);
|
---|
917 | break;
|
---|
918 | case OP_IRET:
|
---|
919 | STAM_COUNTER_INC(&pStats->StatIret);
|
---|
920 | break;
|
---|
921 | case OP_CLI:
|
---|
922 | STAM_COUNTER_INC(&pStats->StatCli);
|
---|
923 | emR3RecordCli(pVM, pVCpu, pCtx->rip);
|
---|
924 | break;
|
---|
925 | case OP_STI:
|
---|
926 | STAM_COUNTER_INC(&pStats->StatSti);
|
---|
927 | break;
|
---|
928 | case OP_INSB:
|
---|
929 | case OP_INSWD:
|
---|
930 | case OP_IN:
|
---|
931 | case OP_OUTSB:
|
---|
932 | case OP_OUTSWD:
|
---|
933 | case OP_OUT:
|
---|
934 | AssertMsgFailed(("Unexpected privileged exception due to port IO\n"));
|
---|
935 | break;
|
---|
936 |
|
---|
937 | case OP_MOV_CR:
|
---|
938 | if (Cpu.Param1.fUse & DISUSE_REG_GEN32)
|
---|
939 | {
|
---|
940 | //read
|
---|
941 | Assert(Cpu.Param2.fUse & DISUSE_REG_CR);
|
---|
942 | Assert(Cpu.Param2.Base.idxCtrlReg <= DISCREG_CR4);
|
---|
943 | STAM_COUNTER_INC(&pStats->StatMovReadCR[Cpu.Param2.Base.idxCtrlReg]);
|
---|
944 | }
|
---|
945 | else
|
---|
946 | {
|
---|
947 | //write
|
---|
948 | Assert(Cpu.Param1.fUse & DISUSE_REG_CR);
|
---|
949 | Assert(Cpu.Param1.Base.idxCtrlReg <= DISCREG_CR4);
|
---|
950 | STAM_COUNTER_INC(&pStats->StatMovWriteCR[Cpu.Param1.Base.idxCtrlReg]);
|
---|
951 | }
|
---|
952 | break;
|
---|
953 |
|
---|
954 | case OP_MOV_DR:
|
---|
955 | STAM_COUNTER_INC(&pStats->StatMovDRx);
|
---|
956 | break;
|
---|
957 | case OP_LLDT:
|
---|
958 | STAM_COUNTER_INC(&pStats->StatMovLldt);
|
---|
959 | break;
|
---|
960 | case OP_LIDT:
|
---|
961 | STAM_COUNTER_INC(&pStats->StatMovLidt);
|
---|
962 | break;
|
---|
963 | case OP_LGDT:
|
---|
964 | STAM_COUNTER_INC(&pStats->StatMovLgdt);
|
---|
965 | break;
|
---|
966 | case OP_SYSENTER:
|
---|
967 | STAM_COUNTER_INC(&pStats->StatSysEnter);
|
---|
968 | break;
|
---|
969 | case OP_SYSEXIT:
|
---|
970 | STAM_COUNTER_INC(&pStats->StatSysExit);
|
---|
971 | break;
|
---|
972 | case OP_SYSCALL:
|
---|
973 | STAM_COUNTER_INC(&pStats->StatSysCall);
|
---|
974 | break;
|
---|
975 | case OP_SYSRET:
|
---|
976 | STAM_COUNTER_INC(&pStats->StatSysRet);
|
---|
977 | break;
|
---|
978 | case OP_HLT:
|
---|
979 | STAM_COUNTER_INC(&pStats->StatHlt);
|
---|
980 | break;
|
---|
981 | default:
|
---|
982 | STAM_COUNTER_INC(&pStats->StatMisc);
|
---|
983 | Log4(("emR3RawPrivileged: opcode=%d\n", Cpu.pCurInstr->uOpcode));
|
---|
984 | break;
|
---|
985 | }
|
---|
986 | #endif /* VBOX_WITH_STATISTICS */
|
---|
987 | if ( (pCtx->ss.Sel & X86_SEL_RPL) == 0
|
---|
988 | && !pCtx->eflags.Bits.u1VM
|
---|
989 | && CPUMGetGuestCodeBits(pVCpu) == 32)
|
---|
990 | {
|
---|
991 | STAM_PROFILE_START(&pVCpu->em.s.StatPrivEmu, a);
|
---|
992 | switch (Cpu.pCurInstr->uOpcode)
|
---|
993 | {
|
---|
994 | case OP_CLI:
|
---|
995 | pCtx->eflags.u32 &= ~X86_EFL_IF;
|
---|
996 | Assert(Cpu.cbInstr == 1);
|
---|
997 | pCtx->rip += Cpu.cbInstr;
|
---|
998 | STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
|
---|
999 | return VINF_EM_RESCHEDULE_REM; /* must go to the recompiler now! */
|
---|
1000 |
|
---|
1001 | case OP_STI:
|
---|
1002 | pCtx->eflags.u32 |= X86_EFL_IF;
|
---|
1003 | EMSetInhibitInterruptsPC(pVCpu, pCtx->rip + Cpu.cbInstr);
|
---|
1004 | Assert(Cpu.cbInstr == 1);
|
---|
1005 | pCtx->rip += Cpu.cbInstr;
|
---|
1006 | STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
|
---|
1007 | return VINF_SUCCESS;
|
---|
1008 |
|
---|
1009 | case OP_HLT:
|
---|
1010 | if (PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
1011 | {
|
---|
1012 | PATMTRANSSTATE enmState;
|
---|
1013 | RTGCPTR pOrgInstrGC = PATMR3PatchToGCPtr(pVM, pCtx->eip, &enmState);
|
---|
1014 |
|
---|
1015 | if (enmState == PATMTRANS_OVERWRITTEN)
|
---|
1016 | {
|
---|
1017 | rc = PATMR3DetectConflict(pVM, pOrgInstrGC, pOrgInstrGC);
|
---|
1018 | Assert(rc == VERR_PATCH_DISABLED);
|
---|
1019 | /* Conflict detected, patch disabled */
|
---|
1020 | Log(("emR3RawPrivileged: detected conflict -> disabled patch at %08RX32\n", pCtx->eip));
|
---|
1021 |
|
---|
1022 | enmState = PATMTRANS_SAFE;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | /* The translation had better be successful. Otherwise we can't recover. */
|
---|
1026 | AssertReleaseMsg(pOrgInstrGC && enmState != PATMTRANS_OVERWRITTEN, ("Unable to translate instruction address at %08RX32\n", pCtx->eip));
|
---|
1027 | if (enmState != PATMTRANS_OVERWRITTEN)
|
---|
1028 | pCtx->eip = pOrgInstrGC;
|
---|
1029 | }
|
---|
1030 | /* no break; we could just return VINF_EM_HALT here */
|
---|
1031 |
|
---|
1032 | case OP_MOV_CR:
|
---|
1033 | case OP_MOV_DR:
|
---|
1034 | #ifdef LOG_ENABLED
|
---|
1035 | if (PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
1036 | {
|
---|
1037 | DBGFR3_INFO_LOG(pVM, pVCpu, "cpumguest", "PRIV");
|
---|
1038 | DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Privileged instr");
|
---|
1039 | }
|
---|
1040 | #endif
|
---|
1041 |
|
---|
1042 | rc = VBOXSTRICTRC_TODO(EMInterpretInstructionDisasState(pVCpu, &Cpu, CPUMCTX2CORE(pCtx), 0, EMCODETYPE_SUPERVISOR));
|
---|
1043 | if (RT_SUCCESS(rc))
|
---|
1044 | {
|
---|
1045 | STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
|
---|
1046 |
|
---|
1047 | if ( Cpu.pCurInstr->uOpcode == OP_MOV_CR
|
---|
1048 | && Cpu.Param1.fUse == DISUSE_REG_CR /* write */
|
---|
1049 | )
|
---|
1050 | {
|
---|
1051 | /* Deal with CR0 updates inside patch code that force
|
---|
1052 | * us to go to the recompiler.
|
---|
1053 | */
|
---|
1054 | if ( PATMIsPatchGCAddr(pVM, pCtx->rip)
|
---|
1055 | && (pCtx->cr0 & (X86_CR0_WP|X86_CR0_PG|X86_CR0_PE)) != (X86_CR0_WP|X86_CR0_PG|X86_CR0_PE))
|
---|
1056 | {
|
---|
1057 | PATMTRANSSTATE enmState;
|
---|
1058 | RTGCPTR pOrgInstrGC = PATMR3PatchToGCPtr(pVM, pCtx->rip, &enmState);
|
---|
1059 |
|
---|
1060 | Log(("Force recompiler switch due to cr0 (%RGp) update rip=%RGv -> %RGv (enmState=%d)\n", pCtx->cr0, pCtx->rip, pOrgInstrGC, enmState));
|
---|
1061 | if (enmState == PATMTRANS_OVERWRITTEN)
|
---|
1062 | {
|
---|
1063 | rc = PATMR3DetectConflict(pVM, pOrgInstrGC, pOrgInstrGC);
|
---|
1064 | Assert(rc == VERR_PATCH_DISABLED);
|
---|
1065 | /* Conflict detected, patch disabled */
|
---|
1066 | Log(("emR3RawPrivileged: detected conflict -> disabled patch at %RGv\n", (RTGCPTR)pCtx->rip));
|
---|
1067 | enmState = PATMTRANS_SAFE;
|
---|
1068 | }
|
---|
1069 | /* The translation had better be successful. Otherwise we can't recover. */
|
---|
1070 | AssertReleaseMsg(pOrgInstrGC && enmState != PATMTRANS_OVERWRITTEN, ("Unable to translate instruction address at %RGv\n", (RTGCPTR)pCtx->rip));
|
---|
1071 | if (enmState != PATMTRANS_OVERWRITTEN)
|
---|
1072 | pCtx->rip = pOrgInstrGC;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | /* Reschedule is necessary as the execution/paging mode might have changed. */
|
---|
1076 | return VINF_EM_RESCHEDULE;
|
---|
1077 | }
|
---|
1078 | return rc; /* can return VINF_EM_HALT as well. */
|
---|
1079 | }
|
---|
1080 | AssertMsgReturn(rc == VERR_EM_INTERPRETER, ("%Rrc\n", rc), rc);
|
---|
1081 | break; /* fall back to the recompiler */
|
---|
1082 | }
|
---|
1083 | STAM_PROFILE_STOP(&pVCpu->em.s.StatPrivEmu, a);
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | if (PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
1088 | return emR3RawPatchTrap(pVM, pVCpu, pCtx, VINF_PATM_PATCH_TRAP_GP);
|
---|
1089 |
|
---|
1090 | return emR3RawExecuteInstruction(pVM, pVCpu, "PRIV");
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 |
|
---|
1094 | /**
|
---|
1095 | * Update the forced rawmode execution modifier.
|
---|
1096 | *
|
---|
1097 | * This function is called when we're returning from the raw-mode loop(s). If we're
|
---|
1098 | * in patch code, it will set a flag forcing execution to be resumed in raw-mode,
|
---|
1099 | * if not in patch code, the flag will be cleared.
|
---|
1100 | *
|
---|
1101 | * We should never interrupt patch code while it's being executed. Cli patches can
|
---|
1102 | * contain big code blocks, but they are always executed with IF=0. Other patches
|
---|
1103 | * replace single instructions and should be atomic.
|
---|
1104 | *
|
---|
1105 | * @returns Updated rc.
|
---|
1106 | *
|
---|
1107 | * @param pVM The cross context VM structure.
|
---|
1108 | * @param pVCpu The cross context virtual CPU structure.
|
---|
1109 | * @param pCtx Pointer to the guest CPU context.
|
---|
1110 | * @param rc The result code.
|
---|
1111 | */
|
---|
1112 | int emR3RawUpdateForceFlag(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int rc)
|
---|
1113 | {
|
---|
1114 | if (PATMIsPatchGCAddr(pVM, pCtx->eip)) /** @todo check cs selector base/type */
|
---|
1115 | {
|
---|
1116 | /* ignore reschedule attempts. */
|
---|
1117 | switch (rc)
|
---|
1118 | {
|
---|
1119 | case VINF_EM_RESCHEDULE:
|
---|
1120 | case VINF_EM_RESCHEDULE_REM:
|
---|
1121 | LogFlow(("emR3RawUpdateForceFlag: patch address -> force raw reschedule\n"));
|
---|
1122 | rc = VINF_SUCCESS;
|
---|
1123 | break;
|
---|
1124 | }
|
---|
1125 | pVCpu->em.s.fForceRAW = true;
|
---|
1126 | }
|
---|
1127 | else
|
---|
1128 | pVCpu->em.s.fForceRAW = false;
|
---|
1129 | return rc;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 |
|
---|
1133 | /**
|
---|
1134 | * Check for pending raw actions
|
---|
1135 | *
|
---|
1136 | * @returns VBox status code. May return VINF_EM_NO_MEMORY but none of the other
|
---|
1137 | * EM statuses.
|
---|
1138 | * @param pVM The cross context VM structure.
|
---|
1139 | * @param pVCpu The cross context virtual CPU structure.
|
---|
1140 | */
|
---|
1141 | VMMR3_INT_DECL(int) EMR3CheckRawForcedActions(PVM pVM, PVMCPU pVCpu)
|
---|
1142 | {
|
---|
1143 | int rc = emR3RawForcedActions(pVM, pVCpu, pVCpu->em.s.pCtx);
|
---|
1144 | VBOXVMM_EM_FF_RAW_RET(pVCpu, rc);
|
---|
1145 | return rc;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 |
|
---|
1149 | /**
|
---|
1150 | * Process raw-mode specific forced actions.
|
---|
1151 | *
|
---|
1152 | * This function is called when any FFs in the VM_FF_HIGH_PRIORITY_PRE_RAW_MASK is pending.
|
---|
1153 | *
|
---|
1154 | * @returns VBox status code. May return VINF_EM_NO_MEMORY but none of the other
|
---|
1155 | * EM statuses.
|
---|
1156 | * @param pVM The cross context VM structure.
|
---|
1157 | * @param pVCpu The cross context virtual CPU structure.
|
---|
1158 | * @param pCtx Pointer to the guest CPU context.
|
---|
1159 | */
|
---|
1160 | static int emR3RawForcedActions(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
|
---|
1161 | {
|
---|
1162 | /*
|
---|
1163 | * Note that the order is *vitally* important!
|
---|
1164 | * Also note that SELMR3UpdateFromCPUM may trigger VM_FF_SELM_SYNC_TSS.
|
---|
1165 | */
|
---|
1166 | VBOXVMM_EM_FF_RAW(pVCpu, pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions);
|
---|
1167 |
|
---|
1168 | /*
|
---|
1169 | * Sync selector tables.
|
---|
1170 | */
|
---|
1171 | if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT))
|
---|
1172 | {
|
---|
1173 | VBOXSTRICTRC rcStrict = SELMR3UpdateFromCPUM(pVM, pVCpu);
|
---|
1174 | if (rcStrict != VINF_SUCCESS)
|
---|
1175 | return VBOXSTRICTRC_TODO(rcStrict);
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | /*
|
---|
1179 | * Sync IDT.
|
---|
1180 | *
|
---|
1181 | * The CSAMR3CheckGates call in TRPMR3SyncIDT may call PGMPrefetchPage
|
---|
1182 | * and PGMShwModifyPage, so we're in for trouble if for instance a
|
---|
1183 | * PGMSyncCR3+pgmR3PoolClearAll is pending.
|
---|
1184 | */
|
---|
1185 | if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TRPM_SYNC_IDT))
|
---|
1186 | {
|
---|
1187 | if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3)
|
---|
1188 | && EMIsRawRing0Enabled(pVM)
|
---|
1189 | && CSAMIsEnabled(pVM))
|
---|
1190 | {
|
---|
1191 | int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
|
---|
1192 | if (RT_FAILURE(rc))
|
---|
1193 | return rc;
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | int rc = TRPMR3SyncIDT(pVM, pVCpu);
|
---|
1197 | if (RT_FAILURE(rc))
|
---|
1198 | return rc;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | /*
|
---|
1202 | * Sync TSS.
|
---|
1203 | */
|
---|
1204 | if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_SELM_SYNC_TSS))
|
---|
1205 | {
|
---|
1206 | int rc = SELMR3SyncTSS(pVM, pVCpu);
|
---|
1207 | if (RT_FAILURE(rc))
|
---|
1208 | return rc;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | /*
|
---|
1212 | * Sync page directory.
|
---|
1213 | */
|
---|
1214 | if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL))
|
---|
1215 | {
|
---|
1216 | Assert(pVCpu->em.s.enmState != EMSTATE_WAIT_SIPI);
|
---|
1217 | int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
|
---|
1218 | if (RT_FAILURE(rc))
|
---|
1219 | return rc == VERR_PGM_NO_HYPERVISOR_ADDRESS ? VINF_EM_RESCHEDULE_REM : rc;
|
---|
1220 |
|
---|
1221 | Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
|
---|
1222 |
|
---|
1223 | /* Prefetch pages for EIP and ESP. */
|
---|
1224 | /** @todo This is rather expensive. Should investigate if it really helps at all. */
|
---|
1225 | rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DISSELREG_CS, CPUMCTX2CORE(pCtx), pCtx->rip));
|
---|
1226 | if (rc == VINF_SUCCESS)
|
---|
1227 | rc = PGMPrefetchPage(pVCpu, SELMToFlat(pVM, DISSELREG_SS, CPUMCTX2CORE(pCtx), pCtx->rsp));
|
---|
1228 | if (rc != VINF_SUCCESS)
|
---|
1229 | {
|
---|
1230 | if (rc != VINF_PGM_SYNC_CR3)
|
---|
1231 | {
|
---|
1232 | AssertLogRelMsgReturn(RT_FAILURE(rc), ("%Rrc\n", rc), VERR_IPE_UNEXPECTED_INFO_STATUS);
|
---|
1233 | return rc;
|
---|
1234 | }
|
---|
1235 | rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
|
---|
1236 | if (RT_FAILURE(rc))
|
---|
1237 | return rc;
|
---|
1238 | }
|
---|
1239 | /** @todo maybe prefetch the supervisor stack page as well */
|
---|
1240 | Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_SELM_SYNC_GDT | VMCPU_FF_SELM_SYNC_LDT));
|
---|
1241 | }
|
---|
1242 |
|
---|
1243 | /*
|
---|
1244 | * Allocate handy pages (just in case the above actions have consumed some pages).
|
---|
1245 | */
|
---|
1246 | if (VM_FF_IS_PENDING_EXCEPT(pVM, VM_FF_PGM_NEED_HANDY_PAGES, VM_FF_PGM_NO_MEMORY))
|
---|
1247 | {
|
---|
1248 | int rc = PGMR3PhysAllocateHandyPages(pVM);
|
---|
1249 | if (RT_FAILURE(rc))
|
---|
1250 | return rc;
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | /*
|
---|
1254 | * Check whether we're out of memory now.
|
---|
1255 | *
|
---|
1256 | * This may stem from some of the above actions or operations that has been executed
|
---|
1257 | * since we ran FFs. The allocate handy pages must for instance always be followed by
|
---|
1258 | * this check.
|
---|
1259 | */
|
---|
1260 | if (VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY))
|
---|
1261 | return VINF_EM_NO_MEMORY;
|
---|
1262 |
|
---|
1263 | return VINF_SUCCESS;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 |
|
---|
1267 | /**
|
---|
1268 | * Executes raw code.
|
---|
1269 | *
|
---|
1270 | * This function contains the raw-mode version of the inner
|
---|
1271 | * execution loop (the outer loop being in EMR3ExecuteVM()).
|
---|
1272 | *
|
---|
1273 | * @returns VBox status code. The most important ones are: VINF_EM_RESCHEDULE,
|
---|
1274 | * VINF_EM_RESCHEDULE_REM, VINF_EM_SUSPEND, VINF_EM_RESET and VINF_EM_TERMINATE.
|
---|
1275 | *
|
---|
1276 | * @param pVM The cross context VM structure.
|
---|
1277 | * @param pVCpu The cross context virtual CPU structure.
|
---|
1278 | * @param pfFFDone Where to store an indicator telling whether or not
|
---|
1279 | * FFs were done before returning.
|
---|
1280 | */
|
---|
1281 | int emR3RawExecute(PVM pVM, PVMCPU pVCpu, bool *pfFFDone)
|
---|
1282 | {
|
---|
1283 | STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatRAWTotal, a);
|
---|
1284 |
|
---|
1285 | int rc = VERR_IPE_UNINITIALIZED_STATUS;
|
---|
1286 | PCPUMCTX pCtx = pVCpu->em.s.pCtx;
|
---|
1287 | LogFlow(("emR3RawExecute: (cs:eip=%04x:%08x)\n", pCtx->cs.Sel, pCtx->eip));
|
---|
1288 | pVCpu->em.s.fForceRAW = false;
|
---|
1289 | *pfFFDone = false;
|
---|
1290 |
|
---|
1291 |
|
---|
1292 | /*
|
---|
1293 | *
|
---|
1294 | * Spin till we get a forced action or raw mode status code resulting in
|
---|
1295 | * in anything but VINF_SUCCESS or VINF_EM_RESCHEDULE_RAW.
|
---|
1296 | *
|
---|
1297 | */
|
---|
1298 | for (;;)
|
---|
1299 | {
|
---|
1300 | STAM_PROFILE_ADV_START(&pVCpu->em.s.StatRAWEntry, b);
|
---|
1301 |
|
---|
1302 | /*
|
---|
1303 | * Check various preconditions.
|
---|
1304 | */
|
---|
1305 | #ifdef VBOX_STRICT
|
---|
1306 | # ifdef VBOX_WITH_REM
|
---|
1307 | Assert(REMR3QueryPendingInterrupt(pVM, pVCpu) == REM_NO_PENDING_IRQ);
|
---|
1308 | # endif
|
---|
1309 | Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss.Sel & X86_SEL_RPL) == 3 || (pCtx->ss.Sel & X86_SEL_RPL) == 0
|
---|
1310 | || (EMIsRawRing1Enabled(pVM) && (pCtx->ss.Sel & X86_SEL_RPL) == 1));
|
---|
1311 | AssertMsg( (pCtx->eflags.u32 & X86_EFL_IF)
|
---|
1312 | || PATMShouldUseRawMode(pVM, (RTGCPTR)pCtx->eip),
|
---|
1313 | ("Tried to execute code with IF at EIP=%08x!\n", pCtx->eip));
|
---|
1314 | if ( !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
|
---|
1315 | && PGMMapHasConflicts(pVM))
|
---|
1316 | {
|
---|
1317 | PGMMapCheck(pVM);
|
---|
1318 | AssertMsgFailed(("We should not get conflicts any longer!!!\n"));
|
---|
1319 | return VERR_EM_UNEXPECTED_MAPPING_CONFLICT;
|
---|
1320 | }
|
---|
1321 | #endif /* VBOX_STRICT */
|
---|
1322 |
|
---|
1323 | /*
|
---|
1324 | * Process high priority pre-execution raw-mode FFs.
|
---|
1325 | */
|
---|
1326 | if ( VM_FF_IS_PENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
|
---|
1327 | || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
|
---|
1328 | {
|
---|
1329 | rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
|
---|
1330 | VBOXVMM_EM_FF_RAW_RET(pVCpu, rc);
|
---|
1331 | if (rc != VINF_SUCCESS)
|
---|
1332 | break;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | /*
|
---|
1336 | * If we're going to execute ring-0 code, the guest state needs to
|
---|
1337 | * be modified a bit and some of the state components (IF, SS/CS RPL,
|
---|
1338 | * and perhaps EIP) needs to be stored with PATM.
|
---|
1339 | */
|
---|
1340 | rc = CPUMRawEnter(pVCpu);
|
---|
1341 | if (rc != VINF_SUCCESS)
|
---|
1342 | {
|
---|
1343 | STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWEntry, b);
|
---|
1344 | break;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | /*
|
---|
1348 | * Scan code before executing it. Don't bother with user mode or V86 code
|
---|
1349 | */
|
---|
1350 | if ( (pCtx->ss.Sel & X86_SEL_RPL) <= 1
|
---|
1351 | && !pCtx->eflags.Bits.u1VM
|
---|
1352 | && !PATMIsPatchGCAddr(pVM, pCtx->eip))
|
---|
1353 | {
|
---|
1354 | STAM_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatRAWEntry, b);
|
---|
1355 | CSAMR3CheckCodeEx(pVM, pCtx, pCtx->eip);
|
---|
1356 | STAM_PROFILE_ADV_RESUME(&pVCpu->em.s.StatRAWEntry, b);
|
---|
1357 | if ( VM_FF_IS_PENDING(pVM, VM_FF_HIGH_PRIORITY_PRE_RAW_MASK)
|
---|
1358 | || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
|
---|
1359 | {
|
---|
1360 | rc = emR3RawForcedActions(pVM, pVCpu, pCtx);
|
---|
1361 | VBOXVMM_EM_FF_RAW_RET(pVCpu, rc);
|
---|
1362 | if (rc != VINF_SUCCESS)
|
---|
1363 | {
|
---|
1364 | rc = CPUMRawLeave(pVCpu, rc);
|
---|
1365 | break;
|
---|
1366 | }
|
---|
1367 | }
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | #ifdef LOG_ENABLED
|
---|
1371 | /*
|
---|
1372 | * Log important stuff before entering GC.
|
---|
1373 | */
|
---|
1374 | PPATMGCSTATE pGCState = PATMR3QueryGCStateHC(pVM);
|
---|
1375 | if (pCtx->eflags.Bits.u1VM)
|
---|
1376 | Log(("RV86: %04x:%08x IF=%d VMFlags=%x\n", pCtx->cs.Sel, pCtx->eip, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
|
---|
1377 | else if ((pCtx->ss.Sel & X86_SEL_RPL) == 1)
|
---|
1378 | Log(("RR0: %x:%08x ESP=%x:%08x EFL=%x IF=%d/%d VMFlags=%x PIF=%d CPL=%d (Scanned=%d)\n",
|
---|
1379 | pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, CPUMRawGetEFlags(pVCpu), !!(pGCState->uVMFlags & X86_EFL_IF), pCtx->eflags.Bits.u1IF,
|
---|
1380 | pGCState->uVMFlags, pGCState->fPIF, (pCtx->ss.Sel & X86_SEL_RPL), CSAMIsPageScanned(pVM, (RTGCPTR)pCtx->eip)));
|
---|
1381 | # ifdef VBOX_WITH_RAW_RING1
|
---|
1382 | else if ((pCtx->ss.Sel & X86_SEL_RPL) == 2)
|
---|
1383 | Log(("RR1: %x:%08x ESP=%x:%08x IF=%d VMFlags=%x CPL=%x\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags, (pCtx->ss.Sel & X86_SEL_RPL)));
|
---|
1384 | # endif
|
---|
1385 | else if ((pCtx->ss.Sel & X86_SEL_RPL) == 3)
|
---|
1386 | Log(("RR3: %x:%08x ESP=%x:%08x IF=%d VMFlags=%x\n", pCtx->cs.Sel, pCtx->eip, pCtx->ss.Sel, pCtx->esp, pCtx->eflags.Bits.u1IF, pGCState->uVMFlags));
|
---|
1387 | #endif /* LOG_ENABLED */
|
---|
1388 |
|
---|
1389 |
|
---|
1390 |
|
---|
1391 | /*
|
---|
1392 | * Execute the code.
|
---|
1393 | */
|
---|
1394 | STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWEntry, b);
|
---|
1395 | if (RT_LIKELY(emR3IsExecutionAllowed(pVM, pVCpu)))
|
---|
1396 | {
|
---|
1397 | STAM_PROFILE_START(&pVCpu->em.s.StatRAWExec, c);
|
---|
1398 | VBOXVMM_EM_RAW_RUN_PRE(pVCpu, pCtx);
|
---|
1399 | rc = VMMR3RawRunGC(pVM, pVCpu);
|
---|
1400 | VBOXVMM_EM_RAW_RUN_RET(pVCpu, pCtx, rc);
|
---|
1401 | STAM_PROFILE_STOP(&pVCpu->em.s.StatRAWExec, c);
|
---|
1402 | }
|
---|
1403 | else
|
---|
1404 | {
|
---|
1405 | /* Give up this time slice; virtual time continues */
|
---|
1406 | STAM_REL_PROFILE_ADV_START(&pVCpu->em.s.StatCapped, u);
|
---|
1407 | RTThreadSleep(5);
|
---|
1408 | STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatCapped, u);
|
---|
1409 | rc = VINF_SUCCESS;
|
---|
1410 | }
|
---|
1411 | STAM_PROFILE_ADV_START(&pVCpu->em.s.StatRAWTail, d);
|
---|
1412 |
|
---|
1413 | LogFlow(("RR%u-E: %08x ESP=%08x EFL=%x IF=%d/%d VMFlags=%x PIF=%d\n",
|
---|
1414 | (pCtx->ss.Sel & X86_SEL_RPL), pCtx->eip, pCtx->esp, CPUMRawGetEFlags(pVCpu),
|
---|
1415 | !!(pGCState->uVMFlags & X86_EFL_IF), pCtx->eflags.Bits.u1IF, pGCState->uVMFlags, pGCState->fPIF));
|
---|
1416 | LogFlow(("VMMR3RawRunGC returned %Rrc\n", rc));
|
---|
1417 |
|
---|
1418 |
|
---|
1419 |
|
---|
1420 | /*
|
---|
1421 | * Restore the real CPU state and deal with high priority post
|
---|
1422 | * execution FFs before doing anything else.
|
---|
1423 | */
|
---|
1424 | rc = CPUMRawLeave(pVCpu, rc);
|
---|
1425 | VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
|
---|
1426 | if ( VM_FF_IS_PENDING(pVM, VM_FF_HIGH_PRIORITY_POST_MASK)
|
---|
1427 | || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HIGH_PRIORITY_POST_MASK))
|
---|
1428 | rc = emR3HighPriorityPostForcedActions(pVM, pVCpu, rc);
|
---|
1429 |
|
---|
1430 | #ifdef VBOX_STRICT
|
---|
1431 | /*
|
---|
1432 | * Assert TSS consistency & rc vs patch code.
|
---|
1433 | */
|
---|
1434 | if ( !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_SELM_SYNC_TSS | VMCPU_FF_SELM_SYNC_GDT) /* GDT implies TSS at the moment. */
|
---|
1435 | && EMIsRawRing0Enabled(pVM))
|
---|
1436 | SELMR3CheckTSS(pVM);
|
---|
1437 | switch (rc)
|
---|
1438 | {
|
---|
1439 | case VINF_SUCCESS:
|
---|
1440 | case VINF_EM_RAW_INTERRUPT:
|
---|
1441 | case VINF_PATM_PATCH_TRAP_PF:
|
---|
1442 | case VINF_PATM_PATCH_TRAP_GP:
|
---|
1443 | case VINF_PATM_PATCH_INT3:
|
---|
1444 | case VINF_PATM_CHECK_PATCH_PAGE:
|
---|
1445 | case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
|
---|
1446 | case VINF_EM_RAW_GUEST_TRAP:
|
---|
1447 | case VINF_EM_RESCHEDULE_RAW:
|
---|
1448 | break;
|
---|
1449 |
|
---|
1450 | default:
|
---|
1451 | if (PATMIsPatchGCAddr(pVM, pCtx->eip) && !(pCtx->eflags.u32 & X86_EFL_TF))
|
---|
1452 | LogIt(0, LOG_GROUP_PATM, ("Patch code interrupted at %RRv for reason %Rrc\n", (RTRCPTR)CPUMGetGuestEIP(pVCpu), rc));
|
---|
1453 | break;
|
---|
1454 | }
|
---|
1455 | /*
|
---|
1456 | * Let's go paranoid!
|
---|
1457 | */
|
---|
1458 | if ( !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL)
|
---|
1459 | && PGMMapHasConflicts(pVM))
|
---|
1460 | {
|
---|
1461 | PGMMapCheck(pVM);
|
---|
1462 | AssertMsgFailed(("We should not get conflicts any longer!!! rc=%Rrc\n", rc));
|
---|
1463 | return VERR_EM_UNEXPECTED_MAPPING_CONFLICT;
|
---|
1464 | }
|
---|
1465 | #endif /* VBOX_STRICT */
|
---|
1466 |
|
---|
1467 | /*
|
---|
1468 | * Process the returned status code.
|
---|
1469 | */
|
---|
1470 | if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
|
---|
1471 | {
|
---|
1472 | STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
|
---|
1473 | break;
|
---|
1474 | }
|
---|
1475 | rc = emR3RawHandleRC(pVM, pVCpu, pCtx, rc);
|
---|
1476 | if (rc != VINF_SUCCESS)
|
---|
1477 | {
|
---|
1478 | rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
|
---|
1479 | if (rc != VINF_SUCCESS)
|
---|
1480 | {
|
---|
1481 | STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
|
---|
1482 | break;
|
---|
1483 | }
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | /*
|
---|
1487 | * Check and execute forced actions.
|
---|
1488 | */
|
---|
1489 | #ifdef VBOX_HIGH_RES_TIMERS_HACK
|
---|
1490 | TMTimerPollVoid(pVM, pVCpu);
|
---|
1491 | #endif
|
---|
1492 | STAM_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTail, d);
|
---|
1493 | if ( VM_FF_IS_PENDING(pVM, ~VM_FF_HIGH_PRIORITY_PRE_RAW_MASK | VM_FF_PGM_NO_MEMORY)
|
---|
1494 | || VMCPU_FF_IS_PENDING(pVCpu, ~VMCPU_FF_HIGH_PRIORITY_PRE_RAW_MASK))
|
---|
1495 | {
|
---|
1496 | Assert(pCtx->eflags.Bits.u1VM || (pCtx->ss.Sel & X86_SEL_RPL) != (EMIsRawRing1Enabled(pVM) ? 2 : 1));
|
---|
1497 |
|
---|
1498 | STAM_REL_PROFILE_ADV_SUSPEND(&pVCpu->em.s.StatRAWTotal, a);
|
---|
1499 | rc = emR3ForcedActions(pVM, pVCpu, rc);
|
---|
1500 | VBOXVMM_EM_FF_ALL_RET(pVCpu, rc);
|
---|
1501 | STAM_REL_PROFILE_ADV_RESUME(&pVCpu->em.s.StatRAWTotal, a);
|
---|
1502 | if ( rc != VINF_SUCCESS
|
---|
1503 | && rc != VINF_EM_RESCHEDULE_RAW)
|
---|
1504 | {
|
---|
1505 | rc = emR3RawUpdateForceFlag(pVM, pVCpu, pCtx, rc);
|
---|
1506 | if (rc != VINF_SUCCESS)
|
---|
1507 | {
|
---|
1508 | *pfFFDone = true;
|
---|
1509 | break;
|
---|
1510 | }
|
---|
1511 | }
|
---|
1512 | }
|
---|
1513 | }
|
---|
1514 |
|
---|
1515 | /*
|
---|
1516 | * Return to outer loop.
|
---|
1517 | */
|
---|
1518 | #if defined(LOG_ENABLED) && defined(DEBUG)
|
---|
1519 | RTLogFlush(NULL);
|
---|
1520 | #endif
|
---|
1521 | STAM_REL_PROFILE_ADV_STOP(&pVCpu->em.s.StatRAWTotal, a);
|
---|
1522 | return rc;
|
---|
1523 | }
|
---|
1524 |
|
---|