VirtualBox

source: vbox/trunk/src/recompiler/VBoxRecompiler.c@ 63562

Last change on this file since 63562 was 62472, checked in by vboxsync, 8 years ago

Misc: scm

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 186.0 KB
Line 
1/* $Id: VBoxRecompiler.c 62472 2016-07-22 18:07:07Z vboxsync $ */
2/** @file
3 * VBox Recompiler - QEMU.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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/** @page pg_rem REM - Recompiled Execution Manager.
19 *
20 * The recompiled exeuction manager (REM) serves the final fallback for guest
21 * execution, after HM / raw-mode and IEM have given up.
22 *
23 * The REM is qemu with a whole bunch of VBox specific customization for
24 * interfacing with PATM, CSAM, PGM and other components.
25 *
26 * @sa @ref grp_rem
27 */
28
29
30/*********************************************************************************************************************************
31* Header Files *
32*********************************************************************************************************************************/
33#define LOG_GROUP LOG_GROUP_REM
34#include <stdio.h> /* FILE */
35#include "osdep.h"
36#include "config.h"
37#include "cpu.h"
38#include "exec-all.h"
39#include "ioport.h"
40
41#include <VBox/vmm/rem.h>
42#include <VBox/vmm/vmapi.h>
43#include <VBox/vmm/tm.h>
44#include <VBox/vmm/ssm.h>
45#include <VBox/vmm/em.h>
46#include <VBox/vmm/trpm.h>
47#include <VBox/vmm/iom.h>
48#include <VBox/vmm/mm.h>
49#include <VBox/vmm/pgm.h>
50#include <VBox/vmm/pdm.h>
51#include <VBox/vmm/dbgf.h>
52#include <VBox/dbg.h>
53#ifdef VBOX_WITH_NEW_APIC
54# include <VBox/vmm/apic.h>
55#endif
56#include <VBox/vmm/hm.h>
57#include <VBox/vmm/patm.h>
58#include <VBox/vmm/csam.h>
59#include "REMInternal.h"
60#include <VBox/vmm/vm.h>
61#include <VBox/vmm/uvm.h>
62#include <VBox/param.h>
63#include <VBox/err.h>
64
65#include <VBox/log.h>
66#include <iprt/alloca.h>
67#include <iprt/semaphore.h>
68#include <iprt/asm.h>
69#include <iprt/assert.h>
70#include <iprt/thread.h>
71#include <iprt/string.h>
72
73/* Don't wanna include everything. */
74extern void cpu_exec_init_all(uintptr_t tb_size);
75extern void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3);
76extern void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0);
77extern void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4);
78extern void tlb_flush_page(CPUX86State *env, target_ulong addr);
79extern void tlb_flush(CPUX86State *env, int flush_global);
80extern void sync_seg(CPUX86State *env1, int seg_reg, int selector);
81extern void sync_ldtr(CPUX86State *env1, int selector);
82
83#ifdef VBOX_STRICT
84ram_addr_t get_phys_page_offset(target_ulong addr);
85#endif
86
87
88/*********************************************************************************************************************************
89* Defined Constants And Macros *
90*********************************************************************************************************************************/
91
92/** Copy 80-bit fpu register at pSrc to pDst.
93 * This is probably faster than *calling* memcpy.
94 */
95#define REM_COPY_FPU_REG(pDst, pSrc) \
96 do { *(PX86FPUMMX)(pDst) = *(const X86FPUMMX *)(pSrc); } while (0)
97
98/** How remR3RunLoggingStep operates. */
99#define REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
100
101
102/** Selector flag shift between qemu and VBox.
103 * VBox shifts the qemu bits to the right. */
104#define SEL_FLAGS_SHIFT (8)
105/** Mask applied to the shifted qemu selector flags to get the attributes VBox
106 * (VT-x) needs. */
107#define SEL_FLAGS_SMASK UINT32_C(0x1F0FF)
108
109
110/*********************************************************************************************************************************
111* Internal Functions *
112*********************************************************************************************************************************/
113static DECLCALLBACK(int) remR3Save(PVM pVM, PSSMHANDLE pSSM);
114static DECLCALLBACK(int) remR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
115static DECLCALLBACK(int) remR3LoadDone(PVM pVM, PSSMHANDLE pSSM);
116static void remR3StateUpdate(PVM pVM, PVMCPU pVCpu);
117static int remR3InitPhysRamSizeAndDirtyMap(PVM pVM, bool fGuarded);
118
119static uint32_t remR3MMIOReadU8(void *pvEnv, target_phys_addr_t GCPhys);
120static uint32_t remR3MMIOReadU16(void *pvEnv, target_phys_addr_t GCPhys);
121static uint32_t remR3MMIOReadU32(void *pvEnv, target_phys_addr_t GCPhys);
122static void remR3MMIOWriteU8(void *pvEnv, target_phys_addr_t GCPhys, uint32_t u32);
123static void remR3MMIOWriteU16(void *pvEnv, target_phys_addr_t GCPhys, uint32_t u32);
124static void remR3MMIOWriteU32(void *pvEnv, target_phys_addr_t GCPhys, uint32_t u32);
125
126static uint32_t remR3HandlerReadU8(void *pvVM, target_phys_addr_t GCPhys);
127static uint32_t remR3HandlerReadU16(void *pvVM, target_phys_addr_t GCPhys);
128static uint32_t remR3HandlerReadU32(void *pvVM, target_phys_addr_t GCPhys);
129static void remR3HandlerWriteU8(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32);
130static void remR3HandlerWriteU16(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32);
131static void remR3HandlerWriteU32(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32);
132
133static void remR3NotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM);
134static void remR3NotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler);
135static void remR3NotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM);
136
137
138/*********************************************************************************************************************************
139* Global Variables *
140*********************************************************************************************************************************/
141
142/** @todo Move stats to REM::s some rainy day we have nothing do to. */
143#ifdef VBOX_WITH_STATISTICS
144static STAMPROFILEADV gStatExecuteSingleInstr;
145static STAMPROFILEADV gStatCompilationQEmu;
146static STAMPROFILEADV gStatRunCodeQEmu;
147static STAMPROFILEADV gStatTotalTimeQEmu;
148static STAMPROFILEADV gStatTimers;
149static STAMPROFILEADV gStatTBLookup;
150static STAMPROFILEADV gStatIRQ;
151static STAMPROFILEADV gStatRawCheck;
152static STAMPROFILEADV gStatMemRead;
153static STAMPROFILEADV gStatMemWrite;
154static STAMPROFILE gStatGCPhys2HCVirt;
155static STAMCOUNTER gStatCpuGetTSC;
156static STAMCOUNTER gStatRefuseTFInhibit;
157static STAMCOUNTER gStatRefuseVM86;
158static STAMCOUNTER gStatRefusePaging;
159static STAMCOUNTER gStatRefusePAE;
160static STAMCOUNTER gStatRefuseIOPLNot0;
161static STAMCOUNTER gStatRefuseIF0;
162static STAMCOUNTER gStatRefuseCode16;
163static STAMCOUNTER gStatRefuseWP0;
164static STAMCOUNTER gStatRefuseRing1or2;
165static STAMCOUNTER gStatRefuseCanExecute;
166static STAMCOUNTER gaStatRefuseStale[6];
167static STAMCOUNTER gStatREMGDTChange;
168static STAMCOUNTER gStatREMIDTChange;
169static STAMCOUNTER gStatREMLDTRChange;
170static STAMCOUNTER gStatREMTRChange;
171static STAMCOUNTER gStatSelOutOfSync[6];
172static STAMCOUNTER gStatSelOutOfSyncStateBack[6];
173static STAMCOUNTER gStatFlushTBs;
174#endif
175/* in exec.c */
176extern uint32_t tlb_flush_count;
177extern uint32_t tb_flush_count;
178extern uint32_t tb_phys_invalidate_count;
179
180/*
181 * Global stuff.
182 */
183
184/** MMIO read callbacks. */
185CPUReadMemoryFunc *g_apfnMMIORead[3] =
186{
187 remR3MMIOReadU8,
188 remR3MMIOReadU16,
189 remR3MMIOReadU32
190};
191
192/** MMIO write callbacks. */
193CPUWriteMemoryFunc *g_apfnMMIOWrite[3] =
194{
195 remR3MMIOWriteU8,
196 remR3MMIOWriteU16,
197 remR3MMIOWriteU32
198};
199
200/** Handler read callbacks. */
201CPUReadMemoryFunc *g_apfnHandlerRead[3] =
202{
203 remR3HandlerReadU8,
204 remR3HandlerReadU16,
205 remR3HandlerReadU32
206};
207
208/** Handler write callbacks. */
209CPUWriteMemoryFunc *g_apfnHandlerWrite[3] =
210{
211 remR3HandlerWriteU8,
212 remR3HandlerWriteU16,
213 remR3HandlerWriteU32
214};
215
216
217#ifdef VBOX_WITH_DEBUGGER
218/*
219 * Debugger commands.
220 */
221static FNDBGCCMD remR3CmdDisasEnableStepping;;
222
223/** '.remstep' arguments. */
224static const DBGCVARDESC g_aArgRemStep[] =
225{
226 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
227 { 0, ~0U, DBGCVAR_CAT_NUMBER, 0, "on/off", "Boolean value/mnemonic indicating the new state." },
228};
229
230/** Command descriptors. */
231static const DBGCCMD g_aCmds[] =
232{
233 {
234 .pszCmd ="remstep",
235 .cArgsMin = 0,
236 .cArgsMax = 1,
237 .paArgDescs = &g_aArgRemStep[0],
238 .cArgDescs = RT_ELEMENTS(g_aArgRemStep),
239 .fFlags = 0,
240 .pfnHandler = remR3CmdDisasEnableStepping,
241 .pszSyntax = "[on/off]",
242 .pszDescription = "Enable or disable the single stepping with logged disassembly. "
243 "If no arguments show the current state."
244 }
245};
246#endif
247
248/** Prologue code, must be in lower 4G to simplify jumps to/from generated code.
249 * @todo huh??? That cannot be the case on the mac... So, this
250 * point is probably not valid any longer. */
251uint8_t *code_gen_prologue;
252
253
254/*********************************************************************************************************************************
255* Internal Functions *
256*********************************************************************************************************************************/
257void remAbort(int rc, const char *pszTip);
258extern int testmath(void);
259
260/* Put them here to avoid unused variable warning. */
261AssertCompile(RT_SIZEOFMEMB(VM, rem.padding) >= RT_SIZEOFMEMB(VM, rem.s));
262#if !defined(IPRT_NO_CRT) && (defined(RT_OS_LINUX) || defined(RT_OS_DARWIN) || defined(RT_OS_WINDOWS))
263//AssertCompileMemberSize(REM, Env, REM_ENV_SIZE);
264/* Why did this have to be identical?? */
265AssertCompile(RT_SIZEOFMEMB(REM, Env) <= REM_ENV_SIZE);
266#else
267AssertCompile(RT_SIZEOFMEMB(REM, Env) <= REM_ENV_SIZE);
268#endif
269
270
271/**
272 * Initializes the REM.
273 *
274 * @returns VBox status code.
275 * @param pVM The VM to operate on.
276 */
277REMR3DECL(int) REMR3Init(PVM pVM)
278{
279 PREMHANDLERNOTIFICATION pCur;
280 uint32_t u32Dummy;
281 int rc;
282 unsigned i;
283
284#ifdef VBOX_ENABLE_VBOXREM64
285 LogRel(("Using 64-bit aware REM\n"));
286#endif
287
288 /*
289 * Assert sanity.
290 */
291 AssertReleaseMsg(sizeof(pVM->rem.padding) >= sizeof(pVM->rem.s), ("%#x >= %#x; sizeof(Env)=%#x\n", sizeof(pVM->rem.padding), sizeof(pVM->rem.s), sizeof(pVM->rem.s.Env)));
292 AssertReleaseMsg(sizeof(pVM->rem.s.Env) <= REM_ENV_SIZE, ("%#x == %#x\n", sizeof(pVM->rem.s.Env), REM_ENV_SIZE));
293 AssertReleaseMsg(!(RT_OFFSETOF(VM, rem) & 31), ("off=%#x\n", RT_OFFSETOF(VM, rem)));
294#if 0 /* just an annoyance at the moment. */
295#if defined(DEBUG) && !defined(RT_OS_SOLARIS) && !defined(RT_OS_FREEBSD) /// @todo fix the solaris and freebsd math stuff.
296 Assert(!testmath());
297#endif
298#endif
299
300 /*
301 * Init some internal data members.
302 */
303 pVM->rem.s.offVM = RT_OFFSETOF(VM, rem.s);
304 pVM->rem.s.Env.pVM = pVM;
305#ifdef CPU_RAW_MODE_INIT
306 pVM->rem.s.state |= CPU_RAW_MODE_INIT;
307#endif
308
309 /*
310 * Initialize the REM critical section.
311 *
312 * Note: This is not a 100% safe solution as updating the internal memory state while another VCPU
313 * is executing code could be dangerous. Taking the REM lock is not an option due to the danger of
314 * deadlocks. (mostly pgm vs rem locking)
315 */
316 rc = PDMR3CritSectInit(pVM, &pVM->rem.s.CritSectRegister, RT_SRC_POS, "REM-Register");
317 AssertRCReturn(rc, rc);
318
319 /* ctx. */
320 pVM->rem.s.pCtx = NULL; /* set when executing code. */
321 AssertMsg(MMR3PhysGetRamSize(pVM) == 0, ("Init order has changed! REM depends on notification about ALL physical memory registrations\n"));
322
323 /* ignore all notifications */
324 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
325
326 code_gen_prologue = RTMemExecAlloc(_1K);
327 AssertLogRelReturn(code_gen_prologue, VERR_NO_MEMORY);
328
329 cpu_exec_init_all(0);
330
331 /*
332 * Init the recompiler.
333 */
334 if (!cpu_x86_init(&pVM->rem.s.Env, "vbox"))
335 {
336 AssertMsgFailed(("cpu_x86_init failed - impossible!\n"));
337 return VERR_GENERAL_FAILURE;
338 }
339 PVMCPU pVCpu = VMMGetCpu(pVM);
340 CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &pVM->rem.s.Env.cpuid_ext_features, &pVM->rem.s.Env.cpuid_features);
341 CPUMGetGuestCpuId(pVCpu, 0x80000001, 0, &u32Dummy, &u32Dummy, &pVM->rem.s.Env.cpuid_ext3_features, &pVM->rem.s.Env.cpuid_ext2_features);
342
343 EMRemLock(pVM);
344 cpu_reset(&pVM->rem.s.Env);
345 EMRemUnlock(pVM);
346
347 /* allocate code buffer for single instruction emulation. */
348 pVM->rem.s.Env.cbCodeBuffer = 4096;
349 pVM->rem.s.Env.pvCodeBuffer = RTMemExecAlloc(pVM->rem.s.Env.cbCodeBuffer);
350 AssertMsgReturn(pVM->rem.s.Env.pvCodeBuffer, ("Failed to allocate code buffer!\n"), VERR_NO_MEMORY);
351
352 /* Finally, set the cpu_single_env global. */
353 cpu_single_env = &pVM->rem.s.Env;
354
355 /* Nothing is pending by default */
356 pVM->rem.s.uStateLoadPendingInterrupt = REM_NO_PENDING_IRQ;
357
358 /*
359 * Register ram types.
360 */
361 pVM->rem.s.iMMIOMemType = cpu_register_io_memory(g_apfnMMIORead, g_apfnMMIOWrite, &pVM->rem.s.Env);
362 AssertReleaseMsg(pVM->rem.s.iMMIOMemType >= 0, ("pVM->rem.s.iMMIOMemType=%d\n", pVM->rem.s.iMMIOMemType));
363 pVM->rem.s.iHandlerMemType = cpu_register_io_memory(g_apfnHandlerRead, g_apfnHandlerWrite, pVM);
364 AssertReleaseMsg(pVM->rem.s.iHandlerMemType >= 0, ("pVM->rem.s.iHandlerMemType=%d\n", pVM->rem.s.iHandlerMemType));
365 Log2(("REM: iMMIOMemType=%d iHandlerMemType=%d\n", pVM->rem.s.iMMIOMemType, pVM->rem.s.iHandlerMemType));
366
367 /* stop ignoring. */
368 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
369
370 /*
371 * Register the saved state data unit.
372 */
373 rc = SSMR3RegisterInternal(pVM, "rem", 1, REM_SAVED_STATE_VERSION, sizeof(uint32_t) * 10,
374 NULL, NULL, NULL,
375 NULL, remR3Save, NULL,
376 NULL, remR3Load, remR3LoadDone);
377 if (RT_FAILURE(rc))
378 return rc;
379
380#ifdef VBOX_WITH_DEBUGGER
381 /*
382 * Debugger commands.
383 */
384 static bool fRegisteredCmds = false;
385 if (!fRegisteredCmds)
386 {
387 int rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
388 if (RT_SUCCESS(rc))
389 fRegisteredCmds = true;
390 }
391#endif
392
393#ifdef VBOX_WITH_STATISTICS
394 /*
395 * Statistics.
396 */
397 STAM_REG(pVM, &gStatExecuteSingleInstr, STAMTYPE_PROFILE, "/PROF/REM/SingleInstr",STAMUNIT_TICKS_PER_CALL, "Profiling single instruction emulation.");
398 STAM_REG(pVM, &gStatCompilationQEmu, STAMTYPE_PROFILE, "/PROF/REM/Compile", STAMUNIT_TICKS_PER_CALL, "Profiling QEmu compilation.");
399 STAM_REG(pVM, &gStatRunCodeQEmu, STAMTYPE_PROFILE, "/PROF/REM/Runcode", STAMUNIT_TICKS_PER_CALL, "Profiling QEmu code execution.");
400 STAM_REG(pVM, &gStatTotalTimeQEmu, STAMTYPE_PROFILE, "/PROF/REM/Emulate", STAMUNIT_TICKS_PER_CALL, "Profiling code emulation.");
401 STAM_REG(pVM, &gStatTimers, STAMTYPE_PROFILE, "/PROF/REM/Timers", STAMUNIT_TICKS_PER_CALL, "Profiling timer queue processing.");
402 STAM_REG(pVM, &gStatTBLookup, STAMTYPE_PROFILE, "/PROF/REM/TBLookup", STAMUNIT_TICKS_PER_CALL, "Profiling translation block lookup.");
403 STAM_REG(pVM, &gStatIRQ, STAMTYPE_PROFILE, "/PROF/REM/IRQ", STAMUNIT_TICKS_PER_CALL, "Profiling IRQ delivery.");
404 STAM_REG(pVM, &gStatRawCheck, STAMTYPE_PROFILE, "/PROF/REM/RawCheck", STAMUNIT_TICKS_PER_CALL, "Profiling remR3CanExecuteRaw calls.");
405 STAM_REG(pVM, &gStatMemRead, STAMTYPE_PROFILE, "/PROF/REM/MemRead", STAMUNIT_TICKS_PER_CALL, "Profiling memory access.");
406 STAM_REG(pVM, &gStatMemWrite, STAMTYPE_PROFILE, "/PROF/REM/MemWrite", STAMUNIT_TICKS_PER_CALL, "Profiling memory access.");
407 STAM_REG(pVM, &gStatGCPhys2HCVirt, STAMTYPE_PROFILE, "/PROF/REM/GCPhys2HCVirt", STAMUNIT_TICKS_PER_CALL, "Profiling memory conversion (PGMR3PhysTlbGCPhys2Ptr).");
408
409 STAM_REG(pVM, &gStatCpuGetTSC, STAMTYPE_COUNTER, "/REM/CpuGetTSC", STAMUNIT_OCCURENCES, "cpu_get_tsc calls");
410
411 STAM_REG(pVM, &gStatRefuseTFInhibit, STAMTYPE_COUNTER, "/REM/Refuse/TFInibit", STAMUNIT_OCCURENCES, "Raw mode refused because of TF or irq inhibit");
412 STAM_REG(pVM, &gStatRefuseVM86, STAMTYPE_COUNTER, "/REM/Refuse/VM86", STAMUNIT_OCCURENCES, "Raw mode refused because of VM86");
413 STAM_REG(pVM, &gStatRefusePaging, STAMTYPE_COUNTER, "/REM/Refuse/Paging", STAMUNIT_OCCURENCES, "Raw mode refused because of disabled paging/pm");
414 STAM_REG(pVM, &gStatRefusePAE, STAMTYPE_COUNTER, "/REM/Refuse/PAE", STAMUNIT_OCCURENCES, "Raw mode refused because of PAE");
415 STAM_REG(pVM, &gStatRefuseIOPLNot0, STAMTYPE_COUNTER, "/REM/Refuse/IOPLNot0", STAMUNIT_OCCURENCES, "Raw mode refused because of IOPL != 0");
416 STAM_REG(pVM, &gStatRefuseIF0, STAMTYPE_COUNTER, "/REM/Refuse/IF0", STAMUNIT_OCCURENCES, "Raw mode refused because of IF=0");
417 STAM_REG(pVM, &gStatRefuseCode16, STAMTYPE_COUNTER, "/REM/Refuse/Code16", STAMUNIT_OCCURENCES, "Raw mode refused because of 16 bit code");
418 STAM_REG(pVM, &gStatRefuseWP0, STAMTYPE_COUNTER, "/REM/Refuse/WP0", STAMUNIT_OCCURENCES, "Raw mode refused because of WP=0");
419 STAM_REG(pVM, &gStatRefuseRing1or2, STAMTYPE_COUNTER, "/REM/Refuse/Ring1or2", STAMUNIT_OCCURENCES, "Raw mode refused because of ring 1/2 execution");
420 STAM_REG(pVM, &gStatRefuseCanExecute, STAMTYPE_COUNTER, "/REM/Refuse/CanExecuteRaw", STAMUNIT_OCCURENCES, "Raw mode refused because of cCanExecuteRaw");
421 STAM_REG(pVM, &gaStatRefuseStale[R_ES], STAMTYPE_COUNTER, "/REM/Refuse/StaleES", STAMUNIT_OCCURENCES, "Raw mode refused because of stale ES");
422 STAM_REG(pVM, &gaStatRefuseStale[R_CS], STAMTYPE_COUNTER, "/REM/Refuse/StaleCS", STAMUNIT_OCCURENCES, "Raw mode refused because of stale CS");
423 STAM_REG(pVM, &gaStatRefuseStale[R_SS], STAMTYPE_COUNTER, "/REM/Refuse/StaleSS", STAMUNIT_OCCURENCES, "Raw mode refused because of stale SS");
424 STAM_REG(pVM, &gaStatRefuseStale[R_DS], STAMTYPE_COUNTER, "/REM/Refuse/StaleDS", STAMUNIT_OCCURENCES, "Raw mode refused because of stale DS");
425 STAM_REG(pVM, &gaStatRefuseStale[R_FS], STAMTYPE_COUNTER, "/REM/Refuse/StaleFS", STAMUNIT_OCCURENCES, "Raw mode refused because of stale FS");
426 STAM_REG(pVM, &gaStatRefuseStale[R_GS], STAMTYPE_COUNTER, "/REM/Refuse/StaleGS", STAMUNIT_OCCURENCES, "Raw mode refused because of stale GS");
427 STAM_REG(pVM, &gStatFlushTBs, STAMTYPE_COUNTER, "/REM/FlushTB", STAMUNIT_OCCURENCES, "Number of TB flushes");
428
429 STAM_REG(pVM, &gStatREMGDTChange, STAMTYPE_COUNTER, "/REM/Change/GDTBase", STAMUNIT_OCCURENCES, "GDT base changes");
430 STAM_REG(pVM, &gStatREMLDTRChange, STAMTYPE_COUNTER, "/REM/Change/LDTR", STAMUNIT_OCCURENCES, "LDTR changes");
431 STAM_REG(pVM, &gStatREMIDTChange, STAMTYPE_COUNTER, "/REM/Change/IDTBase", STAMUNIT_OCCURENCES, "IDT base changes");
432 STAM_REG(pVM, &gStatREMTRChange, STAMTYPE_COUNTER, "/REM/Change/TR", STAMUNIT_OCCURENCES, "TR selector changes");
433
434 STAM_REG(pVM, &gStatSelOutOfSync[0], STAMTYPE_COUNTER, "/REM/State/SelOutOfSync/ES", STAMUNIT_OCCURENCES, "ES out of sync");
435 STAM_REG(pVM, &gStatSelOutOfSync[1], STAMTYPE_COUNTER, "/REM/State/SelOutOfSync/CS", STAMUNIT_OCCURENCES, "CS out of sync");
436 STAM_REG(pVM, &gStatSelOutOfSync[2], STAMTYPE_COUNTER, "/REM/State/SelOutOfSync/SS", STAMUNIT_OCCURENCES, "SS out of sync");
437 STAM_REG(pVM, &gStatSelOutOfSync[3], STAMTYPE_COUNTER, "/REM/State/SelOutOfSync/DS", STAMUNIT_OCCURENCES, "DS out of sync");
438 STAM_REG(pVM, &gStatSelOutOfSync[4], STAMTYPE_COUNTER, "/REM/State/SelOutOfSync/FS", STAMUNIT_OCCURENCES, "FS out of sync");
439 STAM_REG(pVM, &gStatSelOutOfSync[5], STAMTYPE_COUNTER, "/REM/State/SelOutOfSync/GS", STAMUNIT_OCCURENCES, "GS out of sync");
440
441 STAM_REG(pVM, &gStatSelOutOfSyncStateBack[0], STAMTYPE_COUNTER, "/REM/StateBack/SelOutOfSync/ES", STAMUNIT_OCCURENCES, "ES out of sync");
442 STAM_REG(pVM, &gStatSelOutOfSyncStateBack[1], STAMTYPE_COUNTER, "/REM/StateBack/SelOutOfSync/CS", STAMUNIT_OCCURENCES, "CS out of sync");
443 STAM_REG(pVM, &gStatSelOutOfSyncStateBack[2], STAMTYPE_COUNTER, "/REM/StateBack/SelOutOfSync/SS", STAMUNIT_OCCURENCES, "SS out of sync");
444 STAM_REG(pVM, &gStatSelOutOfSyncStateBack[3], STAMTYPE_COUNTER, "/REM/StateBack/SelOutOfSync/DS", STAMUNIT_OCCURENCES, "DS out of sync");
445 STAM_REG(pVM, &gStatSelOutOfSyncStateBack[4], STAMTYPE_COUNTER, "/REM/StateBack/SelOutOfSync/FS", STAMUNIT_OCCURENCES, "FS out of sync");
446 STAM_REG(pVM, &gStatSelOutOfSyncStateBack[5], STAMTYPE_COUNTER, "/REM/StateBack/SelOutOfSync/GS", STAMUNIT_OCCURENCES, "GS out of sync");
447
448 STAM_REG(pVM, &pVM->rem.s.Env.StatTbFlush, STAMTYPE_PROFILE, "/REM/TbFlush", STAMUNIT_TICKS_PER_CALL, "profiling tb_flush().");
449#endif /* VBOX_WITH_STATISTICS */
450 AssertCompileMemberAlignment(CPUX86State, StatTbFlush, 4);
451 AssertCompileMemberAlignment(CPUX86State, StatTbFlush, 8);
452
453 STAM_REL_REG(pVM, &tb_flush_count, STAMTYPE_U32_RESET, "/REM/TbFlushCount", STAMUNIT_OCCURENCES, "tb_flush() calls");
454 STAM_REL_REG(pVM, &tb_phys_invalidate_count, STAMTYPE_U32_RESET, "/REM/TbPhysInvldCount", STAMUNIT_OCCURENCES, "tb_phys_invalidate() calls");
455 STAM_REL_REG(pVM, &tlb_flush_count, STAMTYPE_U32_RESET, "/REM/TlbFlushCount", STAMUNIT_OCCURENCES, "tlb_flush() calls");
456
457
458#ifdef DEBUG_ALL_LOGGING
459 loglevel = ~0;
460#endif
461
462 /*
463 * Init the handler notification lists.
464 */
465 pVM->rem.s.idxPendingList = UINT32_MAX;
466 pVM->rem.s.idxFreeList = 0;
467
468 for (i = 0 ; i < RT_ELEMENTS(pVM->rem.s.aHandlerNotifications); i++)
469 {
470 pCur = &pVM->rem.s.aHandlerNotifications[i];
471 pCur->idxNext = i + 1;
472 pCur->idxSelf = i;
473 }
474 pCur->idxNext = UINT32_MAX; /* the last record. */
475
476 return rc;
477}
478
479
480/**
481 * Finalizes the REM initialization.
482 *
483 * This is called after all components, devices and drivers has
484 * been initialized. Its main purpose it to finish the RAM related
485 * initialization.
486 *
487 * @returns VBox status code.
488 *
489 * @param pVM The VM handle.
490 */
491REMR3DECL(int) REMR3InitFinalize(PVM pVM)
492{
493 int rc;
494
495 /*
496 * Ram size & dirty bit map.
497 */
498 Assert(!pVM->rem.s.fGCPhysLastRamFixed);
499 pVM->rem.s.fGCPhysLastRamFixed = true;
500#ifdef RT_STRICT
501 rc = remR3InitPhysRamSizeAndDirtyMap(pVM, true /* fGuarded */);
502#else
503 rc = remR3InitPhysRamSizeAndDirtyMap(pVM, false /* fGuarded */);
504#endif
505 return rc;
506}
507
508/**
509 * Initializes ram_list.phys_dirty and ram_list.phys_dirty_size.
510 *
511 * @returns VBox status code.
512 * @param pVM The VM handle.
513 * @param fGuarded Whether to guard the map.
514 */
515static int remR3InitPhysRamSizeAndDirtyMap(PVM pVM, bool fGuarded)
516{
517 int rc = VINF_SUCCESS;
518 RTGCPHYS cb;
519
520 AssertLogRelReturn(QLIST_EMPTY(&ram_list.blocks), VERR_INTERNAL_ERROR_2);
521
522 cb = pVM->rem.s.GCPhysLastRam + 1;
523 AssertLogRelMsgReturn(cb > pVM->rem.s.GCPhysLastRam,
524 ("GCPhysLastRam=%RGp - out of range\n", pVM->rem.s.GCPhysLastRam),
525 VERR_OUT_OF_RANGE);
526
527 ram_list.phys_dirty_size = cb >> PAGE_SHIFT;
528 AssertMsg(((RTGCPHYS)ram_list.phys_dirty_size << PAGE_SHIFT) == cb, ("%RGp\n", cb));
529
530 if (!fGuarded)
531 {
532 ram_list.phys_dirty = MMR3HeapAlloc(pVM, MM_TAG_REM, ram_list.phys_dirty_size);
533 AssertLogRelMsgReturn(ram_list.phys_dirty, ("Failed to allocate %u bytes of dirty page map bytes\n", ram_list.phys_dirty_size), VERR_NO_MEMORY);
534 }
535 else
536 {
537 /*
538 * Fill it up the nearest 4GB RAM and leave at least _64KB of guard after it.
539 */
540 uint32_t cbBitmapAligned = RT_ALIGN_32(ram_list.phys_dirty_size, PAGE_SIZE);
541 uint32_t cbBitmapFull = RT_ALIGN_32(ram_list.phys_dirty_size, (_4G >> PAGE_SHIFT));
542 if (cbBitmapFull == cbBitmapAligned)
543 cbBitmapFull += _4G >> PAGE_SHIFT;
544 else if (cbBitmapFull - cbBitmapAligned < _64K)
545 cbBitmapFull += _64K;
546
547 ram_list.phys_dirty = RTMemPageAlloc(cbBitmapFull);
548 AssertLogRelMsgReturn(ram_list.phys_dirty, ("Failed to allocate %u bytes of dirty page map bytes\n", cbBitmapFull), VERR_NO_MEMORY);
549
550 rc = RTMemProtect(ram_list.phys_dirty + cbBitmapAligned, cbBitmapFull - cbBitmapAligned, RTMEM_PROT_NONE);
551 if (RT_FAILURE(rc))
552 {
553 RTMemPageFree(ram_list.phys_dirty, cbBitmapFull);
554 AssertLogRelRCReturn(rc, rc);
555 }
556
557 ram_list.phys_dirty += cbBitmapAligned - ram_list.phys_dirty_size;
558 }
559
560 /* initialize it. */
561 memset(ram_list.phys_dirty, 0xff, ram_list.phys_dirty_size);
562 return rc;
563}
564
565
566/**
567 * Terminates the REM.
568 *
569 * Termination means cleaning up and freeing all resources,
570 * the VM it self is at this point powered off or suspended.
571 *
572 * @returns VBox status code.
573 * @param pVM The VM to operate on.
574 */
575REMR3DECL(int) REMR3Term(PVM pVM)
576{
577 /*
578 * Statistics.
579 */
580 STAMR3Deregister(pVM->pUVM, "/PROF/REM/*");
581 STAMR3Deregister(pVM->pUVM, "/REM/*");
582
583 return VINF_SUCCESS;
584}
585
586
587/**
588 * The VM is being reset.
589 *
590 * For the REM component this means to call the cpu_reset() and
591 * reinitialize some state variables.
592 *
593 * @param pVM VM handle.
594 */
595REMR3DECL(void) REMR3Reset(PVM pVM)
596{
597 EMRemLock(pVM); /* Only pro forma, we're in a rendezvous. */
598
599 /*
600 * Reset the REM cpu.
601 */
602 Assert(pVM->rem.s.cIgnoreAll == 0);
603 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
604 cpu_reset(&pVM->rem.s.Env);
605 pVM->rem.s.cInvalidatedPages = 0;
606 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
607 Assert(pVM->rem.s.cIgnoreAll == 0);
608
609 /* Clear raw ring 0 init state */
610 pVM->rem.s.Env.state &= ~CPU_RAW_RING0;
611
612 /* Flush the TBs the next time we execute code here. */
613 pVM->rem.s.fFlushTBs = true;
614
615 EMRemUnlock(pVM);
616}
617
618
619/**
620 * Execute state save operation.
621 *
622 * @returns VBox status code.
623 * @param pVM VM Handle.
624 * @param pSSM SSM operation handle.
625 */
626static DECLCALLBACK(int) remR3Save(PVM pVM, PSSMHANDLE pSSM)
627{
628 PREM pRem = &pVM->rem.s;
629
630 /*
631 * Save the required CPU Env bits.
632 * (Not much because we're never in REM when doing the save.)
633 */
634 LogFlow(("remR3Save:\n"));
635 Assert(!pRem->fInREM);
636 SSMR3PutU32(pSSM, pRem->Env.hflags);
637 SSMR3PutU32(pSSM, ~0); /* separator */
638
639 /* Remember if we've entered raw mode (vital for ring 1 checks in e.g. iret emulation). */
640 SSMR3PutU32(pSSM, !!(pRem->Env.state & CPU_RAW_RING0));
641 SSMR3PutU32(pSSM, REM_NO_PENDING_IRQ);
642
643 return SSMR3PutU32(pSSM, ~0); /* terminator */
644}
645
646
647/**
648 * Execute state load operation.
649 *
650 * @returns VBox status code.
651 * @param pVM VM Handle.
652 * @param pSSM SSM operation handle.
653 * @param uVersion Data layout version.
654 * @param uPass The data pass.
655 */
656static DECLCALLBACK(int) remR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
657{
658 uint32_t u32Dummy;
659 uint32_t fRawRing0 = false;
660 uint32_t u32Sep;
661 uint32_t i;
662 int rc;
663 PREM pRem;
664
665 LogFlow(("remR3Load:\n"));
666 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
667
668 /*
669 * Validate version.
670 */
671 if ( uVersion != REM_SAVED_STATE_VERSION
672 && uVersion != REM_SAVED_STATE_VERSION_VER1_6)
673 {
674 AssertMsgFailed(("remR3Load: Invalid version uVersion=%d!\n", uVersion));
675 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
676 }
677
678 /*
679 * Do a reset to be on the safe side...
680 */
681 REMR3Reset(pVM);
682
683 /*
684 * Ignore all ignorable notifications.
685 * (Not doing this will cause serious trouble.)
686 */
687 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
688
689 /*
690 * Load the required CPU Env bits.
691 * (Not much because we're never in REM when doing the save.)
692 */
693 pRem = &pVM->rem.s;
694 Assert(!pRem->fInREM);
695 SSMR3GetU32(pSSM, &pRem->Env.hflags);
696 if (uVersion == REM_SAVED_STATE_VERSION_VER1_6)
697 {
698 /* Redundant REM CPU state has to be loaded, but can be ignored. */
699 CPUX86State_Ver16 temp;
700 SSMR3GetMem(pSSM, &temp, RT_OFFSETOF(CPUX86State_Ver16, jmp_env));
701 }
702
703 rc = SSMR3GetU32(pSSM, &u32Sep); /* separator */
704 if (RT_FAILURE(rc))
705 return rc;
706 if (u32Sep != ~0U)
707 {
708 AssertMsgFailed(("u32Sep=%#x\n", u32Sep));
709 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
710 }
711
712 /* Remember if we've entered raw mode (vital for ring 1 checks in e.g. iret emulation). */
713 SSMR3GetUInt(pSSM, &fRawRing0);
714 if (fRawRing0)
715 pRem->Env.state |= CPU_RAW_RING0;
716
717 if (uVersion == REM_SAVED_STATE_VERSION_VER1_6)
718 {
719 /*
720 * Load the REM stuff.
721 */
722 /** @todo r=bird: We should just drop all these items, restoring doesn't make
723 * sense. */
724 rc = SSMR3GetU32(pSSM, (uint32_t *)&pRem->cInvalidatedPages);
725 if (RT_FAILURE(rc))
726 return rc;
727 if (pRem->cInvalidatedPages > RT_ELEMENTS(pRem->aGCPtrInvalidatedPages))
728 {
729 AssertMsgFailed(("cInvalidatedPages=%#x\n", pRem->cInvalidatedPages));
730 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
731 }
732 for (i = 0; i < pRem->cInvalidatedPages; i++)
733 SSMR3GetGCPtr(pSSM, &pRem->aGCPtrInvalidatedPages[i]);
734 }
735
736 rc = SSMR3GetUInt(pSSM, &pVM->rem.s.uStateLoadPendingInterrupt);
737 AssertRCReturn(rc, rc);
738 AssertLogRelMsgReturn( pVM->rem.s.uStateLoadPendingInterrupt == REM_NO_PENDING_IRQ
739 || pVM->rem.s.uStateLoadPendingInterrupt < 256,
740 ("uStateLoadPendingInterrupt=%#x\n", pVM->rem.s.uStateLoadPendingInterrupt),
741 VERR_SSM_UNEXPECTED_DATA);
742
743 /* check the terminator. */
744 rc = SSMR3GetU32(pSSM, &u32Sep);
745 if (RT_FAILURE(rc))
746 return rc;
747 if (u32Sep != ~0U)
748 {
749 AssertMsgFailed(("u32Sep=%#x (term)\n", u32Sep));
750 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
751 }
752
753 /*
754 * Get the CPUID features.
755 */
756 PVMCPU pVCpu = VMMGetCpu(pVM);
757 CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &pVM->rem.s.Env.cpuid_ext_features, &pVM->rem.s.Env.cpuid_features);
758 CPUMGetGuestCpuId(pVCpu, 0x80000001, 0, &u32Dummy, &u32Dummy, &u32Dummy, &pVM->rem.s.Env.cpuid_ext2_features);
759
760 /*
761 * Stop ignoring ignorable notifications.
762 */
763 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
764
765 /*
766 * Sync the whole CPU state when executing code in the recompiler.
767 */
768 for (i = 0; i < pVM->cCpus; i++)
769 {
770 PVMCPU pVCpu = &pVM->aCpus[i];
771 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
772 }
773 return VINF_SUCCESS;
774}
775
776
777/**
778 * @callback_method_impl{FNSSMINTLOADDONE,
779 * For pushing misdesigned pending-interrupt mess to TRPM where it belongs. }
780 */
781static DECLCALLBACK(int) remR3LoadDone(PVM pVM, PSSMHANDLE pSSM)
782{
783 if (pVM->rem.s.uStateLoadPendingInterrupt != REM_NO_PENDING_IRQ)
784 {
785 int rc = TRPMAssertTrap(&pVM->aCpus[0], pVM->rem.s.uStateLoadPendingInterrupt, TRPM_HARDWARE_INT);
786 AssertLogRelMsgReturn(rc, ("uStateLoadPendingInterrupt=%#x rc=%Rrc\n", pVM->rem.s.uStateLoadPendingInterrupt, rc), rc);
787 pVM->rem.s.uStateLoadPendingInterrupt = REM_NO_PENDING_IRQ;
788 }
789 return VINF_SUCCESS;
790}
791
792
793#undef LOG_GROUP
794#define LOG_GROUP LOG_GROUP_REM_RUN
795
796/**
797 * Single steps an instruction in recompiled mode.
798 *
799 * Before calling this function the REM state needs to be in sync with
800 * the VM. Call REMR3State() to perform the sync. It's only necessary
801 * (and permitted) to sync at the first call to REMR3Step()/REMR3Run()
802 * and after calling REMR3StateBack().
803 *
804 * @returns VBox status code.
805 *
806 * @param pVM VM Handle.
807 * @param pVCpu VMCPU Handle.
808 */
809REMR3DECL(int) REMR3Step(PVM pVM, PVMCPU pVCpu)
810{
811 int rc, interrupt_request;
812 RTGCPTR GCPtrPC;
813 bool fBp;
814
815 /*
816 * Lock the REM - we don't wanna have anyone interrupting us
817 * while stepping - and enabled single stepping. We also ignore
818 * pending interrupts and suchlike.
819 */
820 interrupt_request = pVM->rem.s.Env.interrupt_request;
821 Assert(!(interrupt_request & ~(CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB | CPU_INTERRUPT_TIMER | CPU_INTERRUPT_EXTERNAL_HARD | CPU_INTERRUPT_EXTERNAL_EXIT | CPU_INTERRUPT_EXTERNAL_FLUSH_TLB | CPU_INTERRUPT_EXTERNAL_TIMER)));
822 pVM->rem.s.Env.interrupt_request = 0;
823 cpu_single_step(&pVM->rem.s.Env, 1);
824
825 /*
826 * If we're standing at a breakpoint, that have to be disabled before we start stepping.
827 */
828 GCPtrPC = pVM->rem.s.Env.eip + pVM->rem.s.Env.segs[R_CS].base;
829 fBp = !cpu_breakpoint_remove(&pVM->rem.s.Env, GCPtrPC, BP_GDB);
830
831 /*
832 * Execute and handle the return code.
833 * We execute without enabling the cpu tick, so on success we'll
834 * just flip it on and off to make sure it moves
835 */
836 rc = cpu_exec(&pVM->rem.s.Env);
837 if (rc == EXCP_DEBUG)
838 {
839 TMR3NotifyResume(pVM, pVCpu);
840 TMR3NotifySuspend(pVM, pVCpu);
841 rc = VINF_EM_DBG_STEPPED;
842 }
843 else
844 {
845 switch (rc)
846 {
847 case EXCP_INTERRUPT: rc = VINF_SUCCESS; break;
848 case EXCP_HLT:
849 case EXCP_HALTED: rc = VINF_EM_HALT; break;
850 case EXCP_RC:
851 rc = pVM->rem.s.rc;
852 pVM->rem.s.rc = VERR_INTERNAL_ERROR;
853 break;
854 case EXCP_EXECUTE_RAW:
855 case EXCP_EXECUTE_HM:
856 /** @todo: is it correct? No! */
857 rc = VINF_SUCCESS;
858 break;
859 default:
860 AssertReleaseMsgFailed(("This really shouldn't happen, rc=%d!\n", rc));
861 rc = VERR_INTERNAL_ERROR;
862 break;
863 }
864 }
865
866 /*
867 * Restore the stuff we changed to prevent interruption.
868 * Unlock the REM.
869 */
870 if (fBp)
871 {
872 int rc2 = cpu_breakpoint_insert(&pVM->rem.s.Env, GCPtrPC, BP_GDB, NULL);
873 Assert(rc2 == 0); NOREF(rc2);
874 }
875 cpu_single_step(&pVM->rem.s.Env, 0);
876 pVM->rem.s.Env.interrupt_request = interrupt_request;
877
878 return rc;
879}
880
881
882/**
883 * Set a breakpoint using the REM facilities.
884 *
885 * @returns VBox status code.
886 * @param pVM The VM handle.
887 * @param Address The breakpoint address.
888 * @thread The emulation thread.
889 */
890REMR3DECL(int) REMR3BreakpointSet(PVM pVM, RTGCUINTPTR Address)
891{
892 VM_ASSERT_EMT(pVM);
893 if (!cpu_breakpoint_insert(&pVM->rem.s.Env, Address, BP_GDB, NULL))
894 {
895 LogFlow(("REMR3BreakpointSet: Address=%RGv\n", Address));
896 return VINF_SUCCESS;
897 }
898 LogFlow(("REMR3BreakpointSet: Address=%RGv - failed!\n", Address));
899 return VERR_REM_NO_MORE_BP_SLOTS;
900}
901
902
903/**
904 * Clears a breakpoint set by REMR3BreakpointSet().
905 *
906 * @returns VBox status code.
907 * @param pVM The VM handle.
908 * @param Address The breakpoint address.
909 * @thread The emulation thread.
910 */
911REMR3DECL(int) REMR3BreakpointClear(PVM pVM, RTGCUINTPTR Address)
912{
913 VM_ASSERT_EMT(pVM);
914 if (!cpu_breakpoint_remove(&pVM->rem.s.Env, Address, BP_GDB))
915 {
916 LogFlow(("REMR3BreakpointClear: Address=%RGv\n", Address));
917 return VINF_SUCCESS;
918 }
919 LogFlow(("REMR3BreakpointClear: Address=%RGv - not found!\n", Address));
920 return VERR_REM_BP_NOT_FOUND;
921}
922
923
924/**
925 * Emulate an instruction.
926 *
927 * This function executes one instruction without letting anyone
928 * interrupt it. This is intended for being called while being in
929 * raw mode and thus will take care of all the state syncing between
930 * REM and the rest.
931 *
932 * @returns VBox status code.
933 * @param pVM VM handle.
934 * @param pVCpu VMCPU Handle.
935 */
936REMR3DECL(int) REMR3EmulateInstruction(PVM pVM, PVMCPU pVCpu)
937{
938 bool fFlushTBs;
939
940 int rc, rc2;
941 Log2(("REMR3EmulateInstruction: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
942
943 /* Make sure this flag is set; we might never execute remR3CanExecuteRaw in the AMD-V case.
944 * CPU_RAW_HM makes sure we never execute interrupt handlers in the recompiler.
945 */
946 if (HMIsEnabled(pVM))
947 pVM->rem.s.Env.state |= CPU_RAW_HM;
948
949 /* Skip the TB flush as that's rather expensive and not necessary for single instruction emulation. */
950 fFlushTBs = pVM->rem.s.fFlushTBs;
951 pVM->rem.s.fFlushTBs = false;
952
953 /*
954 * Sync the state and enable single instruction / single stepping.
955 */
956 rc = REMR3State(pVM, pVCpu);
957 pVM->rem.s.fFlushTBs = fFlushTBs;
958 if (RT_SUCCESS(rc))
959 {
960 int interrupt_request = pVM->rem.s.Env.interrupt_request;
961 Assert(!( interrupt_request
962 & ~(CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB | CPU_INTERRUPT_TIMER | CPU_INTERRUPT_EXTERNAL_HARD
963 | CPU_INTERRUPT_EXTERNAL_EXIT | CPU_INTERRUPT_EXTERNAL_FLUSH_TLB | CPU_INTERRUPT_EXTERNAL_TIMER
964 | CPU_INTERRUPT_EXTERNAL_DMA)));
965#ifdef REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
966 cpu_single_step(&pVM->rem.s.Env, 0);
967#endif
968 Assert(!pVM->rem.s.Env.singlestep_enabled);
969
970 /*
971 * Now we set the execute single instruction flag and enter the cpu_exec loop.
972 */
973 TMNotifyStartOfExecution(pVCpu);
974 pVM->rem.s.Env.interrupt_request = CPU_INTERRUPT_SINGLE_INSTR;
975 rc = cpu_exec(&pVM->rem.s.Env);
976 TMNotifyEndOfExecution(pVCpu);
977 switch (rc)
978 {
979 /*
980 * Executed without anything out of the way happening.
981 */
982 case EXCP_SINGLE_INSTR:
983 rc = VINF_EM_RESCHEDULE;
984 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_SINGLE_INSTR\n"));
985 break;
986
987 /*
988 * If we take a trap or start servicing a pending interrupt, we might end up here.
989 * (Timer thread or some other thread wishing EMT's attention.)
990 */
991 case EXCP_INTERRUPT:
992 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_INTERRUPT\n"));
993 rc = VINF_EM_RESCHEDULE;
994 break;
995
996 /*
997 * Single step, we assume!
998 * If there was a breakpoint there we're fucked now.
999 */
1000 case EXCP_DEBUG:
1001 if (pVM->rem.s.Env.watchpoint_hit)
1002 {
1003 /** @todo deal with watchpoints */
1004 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_DEBUG rc=%Rrc !watchpoint_hit!\n", rc));
1005 rc = VINF_EM_DBG_BREAKPOINT;
1006 }
1007 else
1008 {
1009 CPUBreakpoint *pBP;
1010 RTGCPTR GCPtrPC = pVM->rem.s.Env.eip + pVM->rem.s.Env.segs[R_CS].base;
1011 QTAILQ_FOREACH(pBP, &pVM->rem.s.Env.breakpoints, entry)
1012 if (pBP->pc == GCPtrPC)
1013 break;
1014 rc = pBP ? VINF_EM_DBG_BREAKPOINT : VINF_EM_DBG_STEPPED;
1015 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_DEBUG rc=%Rrc pBP=%p GCPtrPC=%RGv\n", rc, pBP, GCPtrPC));
1016 }
1017 break;
1018
1019 /*
1020 * hlt instruction.
1021 */
1022 case EXCP_HLT:
1023 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_HLT\n"));
1024 rc = VINF_EM_HALT;
1025 break;
1026
1027 /*
1028 * The VM has halted.
1029 */
1030 case EXCP_HALTED:
1031 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_HALTED\n"));
1032 rc = VINF_EM_HALT;
1033 break;
1034
1035 /*
1036 * Switch to RAW-mode.
1037 */
1038 case EXCP_EXECUTE_RAW:
1039 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_EXECUTE_RAW\n"));
1040 rc = VINF_EM_RESCHEDULE_RAW;
1041 break;
1042
1043 /*
1044 * Switch to hardware accelerated RAW-mode.
1045 */
1046 case EXCP_EXECUTE_HM:
1047 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_EXECUTE_HM\n"));
1048 rc = VINF_EM_RESCHEDULE_HM;
1049 break;
1050
1051 /*
1052 * An EM RC was raised (VMR3Reset/Suspend/PowerOff/some-fatal-error).
1053 */
1054 case EXCP_RC:
1055 Log2(("REMR3EmulateInstruction: cpu_exec -> EXCP_RC\n"));
1056 rc = pVM->rem.s.rc;
1057 pVM->rem.s.rc = VERR_INTERNAL_ERROR;
1058 break;
1059
1060 /*
1061 * Figure out the rest when they arrive....
1062 */
1063 default:
1064 AssertMsgFailed(("rc=%d\n", rc));
1065 Log2(("REMR3EmulateInstruction: cpu_exec -> %d\n", rc));
1066 rc = VINF_EM_RESCHEDULE;
1067 break;
1068 }
1069
1070 /*
1071 * Switch back the state.
1072 */
1073 pVM->rem.s.Env.interrupt_request = interrupt_request;
1074 rc2 = REMR3StateBack(pVM, pVCpu);
1075 AssertRC(rc2);
1076 }
1077
1078 Log2(("REMR3EmulateInstruction: returns %Rrc (cs:eip=%04x:%RGv)\n",
1079 rc, pVM->rem.s.Env.segs[R_CS].selector, (RTGCPTR)pVM->rem.s.Env.eip));
1080 return rc;
1081}
1082
1083
1084/**
1085 * Used by REMR3Run to handle the case where CPU_EMULATE_SINGLE_STEP is set.
1086 *
1087 * @returns VBox status code.
1088 *
1089 * @param pVM The VM handle.
1090 * @param pVCpu The Virtual CPU handle.
1091 */
1092static int remR3RunLoggingStep(PVM pVM, PVMCPU pVCpu)
1093{
1094 int rc;
1095
1096 Assert(pVM->rem.s.fInREM);
1097#ifdef REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
1098 cpu_single_step(&pVM->rem.s.Env, 1);
1099#else
1100 Assert(!pVM->rem.s.Env.singlestep_enabled);
1101#endif
1102
1103 /*
1104 * Now we set the execute single instruction flag and enter the cpu_exec loop.
1105 */
1106 for (;;)
1107 {
1108 char szBuf[256];
1109
1110 /*
1111 * Log the current registers state and instruction.
1112 */
1113 remR3StateUpdate(pVM, pVCpu);
1114 DBGFR3Info(pVM->pUVM, "cpumguest", NULL, NULL);
1115 szBuf[0] = '\0';
1116 rc = DBGFR3DisasInstrEx(pVM->pUVM,
1117 pVCpu->idCpu,
1118 0, /* Sel */ 0, /* GCPtr */
1119 DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_DEFAULT_MODE,
1120 szBuf,
1121 sizeof(szBuf),
1122 NULL);
1123 if (RT_FAILURE(rc))
1124 RTStrPrintf(szBuf, sizeof(szBuf), "DBGFR3DisasInstrEx failed with rc=%Rrc\n", rc);
1125 RTLogPrintf("CPU%d: %s\n", pVCpu->idCpu, szBuf);
1126
1127 /*
1128 * Execute the instruction.
1129 */
1130 TMNotifyStartOfExecution(pVCpu);
1131
1132 if ( pVM->rem.s.Env.exception_index < 0
1133 || pVM->rem.s.Env.exception_index > 256)
1134 pVM->rem.s.Env.exception_index = -1; /** @todo We need to do similar stuff elsewhere, I think. */
1135
1136#ifdef REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
1137 pVM->rem.s.Env.interrupt_request = 0;
1138#else
1139 pVM->rem.s.Env.interrupt_request = CPU_INTERRUPT_SINGLE_INSTR;
1140#endif
1141 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC))
1142 pVM->rem.s.Env.interrupt_request |= CPU_INTERRUPT_HARD;
1143 RTLogPrintf("remR3RunLoggingStep: interrupt_request=%#x halted=%d exception_index=%#x\n",
1144 pVM->rem.s.Env.interrupt_request,
1145 pVM->rem.s.Env.halted,
1146 pVM->rem.s.Env.exception_index
1147 );
1148
1149 rc = cpu_exec(&pVM->rem.s.Env);
1150
1151 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> %#x interrupt_request=%#x halted=%d exception_index=%#x\n", rc,
1152 pVM->rem.s.Env.interrupt_request,
1153 pVM->rem.s.Env.halted,
1154 pVM->rem.s.Env.exception_index
1155 );
1156
1157 TMNotifyEndOfExecution(pVCpu);
1158
1159 switch (rc)
1160 {
1161#ifndef REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
1162 /*
1163 * The normal exit.
1164 */
1165 case EXCP_SINGLE_INSTR:
1166 if ( !VM_FF_IS_PENDING(pVM, VM_FF_ALL_REM_MASK)
1167 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_ALL_REM_MASK))
1168 continue;
1169 RTLogPrintf("remR3RunLoggingStep: rc=VINF_SUCCESS w/ FFs (%#x/%#x)\n",
1170 pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions);
1171 rc = VINF_SUCCESS;
1172 break;
1173
1174#else
1175 /*
1176 * The normal exit, check for breakpoints at PC just to be sure.
1177 */
1178#endif
1179 case EXCP_DEBUG:
1180 if (pVM->rem.s.Env.watchpoint_hit)
1181 {
1182 /** @todo deal with watchpoints */
1183 Log2(("remR3RunLoggingStep: cpu_exec -> EXCP_DEBUG rc=%Rrc !watchpoint_hit!\n", rc));
1184 rc = VINF_EM_DBG_BREAKPOINT;
1185 }
1186 else
1187 {
1188 CPUBreakpoint *pBP;
1189 RTGCPTR GCPtrPC = pVM->rem.s.Env.eip + pVM->rem.s.Env.segs[R_CS].base;
1190 QTAILQ_FOREACH(pBP, &pVM->rem.s.Env.breakpoints, entry)
1191 if (pBP->pc == GCPtrPC)
1192 break;
1193 rc = pBP ? VINF_EM_DBG_BREAKPOINT : VINF_EM_DBG_STEPPED;
1194 Log2(("remR3RunLoggingStep: cpu_exec -> EXCP_DEBUG rc=%Rrc pBP=%p GCPtrPC=%RGv\n", rc, pBP, GCPtrPC));
1195 }
1196#ifdef REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
1197 if (rc == VINF_EM_DBG_STEPPED)
1198 {
1199 if ( !VM_FF_IS_PENDING(pVM, VM_FF_ALL_REM_MASK)
1200 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_ALL_REM_MASK))
1201 continue;
1202
1203 RTLogPrintf("remR3RunLoggingStep: rc=VINF_SUCCESS w/ FFs (%#x/%#x)\n",
1204 pVM->fGlobalForcedActions, pVCpu->fLocalForcedActions);
1205 rc = VINF_SUCCESS;
1206 }
1207#endif
1208 break;
1209
1210 /*
1211 * If we take a trap or start servicing a pending interrupt, we might end up here.
1212 * (Timer thread or some other thread wishing EMT's attention.)
1213 */
1214 case EXCP_INTERRUPT:
1215 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> EXCP_INTERRUPT rc=VINF_SUCCESS\n");
1216 rc = VINF_SUCCESS;
1217 break;
1218
1219 /*
1220 * hlt instruction.
1221 */
1222 case EXCP_HLT:
1223 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> EXCP_HLT rc=VINF_EM_HALT\n");
1224 rc = VINF_EM_HALT;
1225 break;
1226
1227 /*
1228 * The VM has halted.
1229 */
1230 case EXCP_HALTED:
1231 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> EXCP_HALTED rc=VINF_EM_HALT\n");
1232 rc = VINF_EM_HALT;
1233 break;
1234
1235 /*
1236 * Switch to RAW-mode.
1237 */
1238 case EXCP_EXECUTE_RAW:
1239 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> EXCP_EXECUTE_RAW rc=VINF_EM_RESCHEDULE_RAW\n");
1240 rc = VINF_EM_RESCHEDULE_RAW;
1241 break;
1242
1243 /*
1244 * Switch to hardware accelerated RAW-mode.
1245 */
1246 case EXCP_EXECUTE_HM:
1247 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> EXCP_EXECUTE_HM rc=VINF_EM_RESCHEDULE_HM\n");
1248 rc = VINF_EM_RESCHEDULE_HM;
1249 break;
1250
1251 /*
1252 * An EM RC was raised (VMR3Reset/Suspend/PowerOff/some-fatal-error).
1253 */
1254 case EXCP_RC:
1255 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> EXCP_RC rc=%Rrc\n", pVM->rem.s.rc);
1256 rc = pVM->rem.s.rc;
1257 pVM->rem.s.rc = VERR_INTERNAL_ERROR;
1258 break;
1259
1260 /*
1261 * Figure out the rest when they arrive....
1262 */
1263 default:
1264 AssertMsgFailed(("rc=%d\n", rc));
1265 RTLogPrintf("remR3RunLoggingStep: cpu_exec -> %d rc=VINF_EM_RESCHEDULE\n", rc);
1266 rc = VINF_EM_RESCHEDULE;
1267 break;
1268 }
1269 break;
1270 }
1271
1272#ifdef REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
1273// cpu_single_step(&pVM->rem.s.Env, 0);
1274#else
1275 pVM->rem.s.Env.interrupt_request &= ~(CPU_INTERRUPT_SINGLE_INSTR | CPU_INTERRUPT_SINGLE_INSTR_IN_FLIGHT);
1276#endif
1277 return rc;
1278}
1279
1280
1281/**
1282 * Runs code in recompiled mode.
1283 *
1284 * Before calling this function the REM state needs to be in sync with
1285 * the VM. Call REMR3State() to perform the sync. It's only necessary
1286 * (and permitted) to sync at the first call to REMR3Step()/REMR3Run()
1287 * and after calling REMR3StateBack().
1288 *
1289 * @returns VBox status code.
1290 *
1291 * @param pVM VM Handle.
1292 * @param pVCpu VMCPU Handle.
1293 */
1294REMR3DECL(int) REMR3Run(PVM pVM, PVMCPU pVCpu)
1295{
1296 int rc;
1297
1298 if (RT_UNLIKELY(pVM->rem.s.Env.state & CPU_EMULATE_SINGLE_STEP))
1299 return remR3RunLoggingStep(pVM, pVCpu);
1300
1301 Assert(pVM->rem.s.fInREM);
1302 Log2(("REMR3Run: (cs:eip=%04x:%RGv)\n", pVM->rem.s.Env.segs[R_CS].selector, (RTGCPTR)pVM->rem.s.Env.eip));
1303
1304 TMNotifyStartOfExecution(pVCpu);
1305 rc = cpu_exec(&pVM->rem.s.Env);
1306 TMNotifyEndOfExecution(pVCpu);
1307 switch (rc)
1308 {
1309 /*
1310 * This happens when the execution was interrupted
1311 * by an external event, like pending timers.
1312 */
1313 case EXCP_INTERRUPT:
1314 Log2(("REMR3Run: cpu_exec -> EXCP_INTERRUPT\n"));
1315 rc = VINF_SUCCESS;
1316 break;
1317
1318 /*
1319 * hlt instruction.
1320 */
1321 case EXCP_HLT:
1322 Log2(("REMR3Run: cpu_exec -> EXCP_HLT\n"));
1323 rc = VINF_EM_HALT;
1324 break;
1325
1326 /*
1327 * The VM has halted.
1328 */
1329 case EXCP_HALTED:
1330 Log2(("REMR3Run: cpu_exec -> EXCP_HALTED\n"));
1331 rc = VINF_EM_HALT;
1332 break;
1333
1334 /*
1335 * Breakpoint/single step.
1336 */
1337 case EXCP_DEBUG:
1338 if (pVM->rem.s.Env.watchpoint_hit)
1339 {
1340 /** @todo deal with watchpoints */
1341 Log2(("REMR3Run: cpu_exec -> EXCP_DEBUG rc=%Rrc !watchpoint_hit!\n", rc));
1342 rc = VINF_EM_DBG_BREAKPOINT;
1343 }
1344 else
1345 {
1346 CPUBreakpoint *pBP;
1347 RTGCPTR GCPtrPC = pVM->rem.s.Env.eip + pVM->rem.s.Env.segs[R_CS].base;
1348 QTAILQ_FOREACH(pBP, &pVM->rem.s.Env.breakpoints, entry)
1349 if (pBP->pc == GCPtrPC)
1350 break;
1351 rc = pBP ? VINF_EM_DBG_BREAKPOINT : VINF_EM_DBG_STEPPED;
1352 Log2(("REMR3Run: cpu_exec -> EXCP_DEBUG rc=%Rrc pBP=%p GCPtrPC=%RGv\n", rc, pBP, GCPtrPC));
1353 }
1354 break;
1355
1356 /*
1357 * Switch to RAW-mode.
1358 */
1359 case EXCP_EXECUTE_RAW:
1360 Log2(("REMR3Run: cpu_exec -> EXCP_EXECUTE_RAW pc=%RGv\n", pVM->rem.s.Env.eip));
1361 rc = VINF_EM_RESCHEDULE_RAW;
1362 break;
1363
1364 /*
1365 * Switch to hardware accelerated RAW-mode.
1366 */
1367 case EXCP_EXECUTE_HM:
1368 Log2(("REMR3Run: cpu_exec -> EXCP_EXECUTE_HM\n"));
1369 rc = VINF_EM_RESCHEDULE_HM;
1370 break;
1371
1372 /*
1373 * An EM RC was raised (VMR3Reset/Suspend/PowerOff/some-fatal-error).
1374 */
1375 case EXCP_RC:
1376 Log2(("REMR3Run: cpu_exec -> EXCP_RC rc=%Rrc\n", pVM->rem.s.rc));
1377 rc = pVM->rem.s.rc;
1378 pVM->rem.s.rc = VERR_INTERNAL_ERROR;
1379 break;
1380
1381 /*
1382 * Figure out the rest when they arrive....
1383 */
1384 default:
1385 AssertMsgFailed(("rc=%d\n", rc));
1386 Log2(("REMR3Run: cpu_exec -> %d\n", rc));
1387 rc = VINF_SUCCESS;
1388 break;
1389 }
1390
1391 Log2(("REMR3Run: returns %Rrc (cs:eip=%04x:%RGv)\n", rc, pVM->rem.s.Env.segs[R_CS].selector, (RTGCPTR)pVM->rem.s.Env.eip));
1392 return rc;
1393}
1394
1395
1396/**
1397 * Check if the cpu state is suitable for Raw execution.
1398 *
1399 * @returns true if RAW/HWACC mode is ok, false if we should stay in REM.
1400 *
1401 * @param env The CPU env struct.
1402 * @param eip The EIP to check this for (might differ from env->eip).
1403 * @param fFlags hflags OR'ed with IOPL, TF and VM from eflags.
1404 * @param piException Stores EXCP_EXECUTE_RAW/HWACC in case raw mode is supported in this context
1405 *
1406 * @remark This function must be kept in perfect sync with the scheduler in EM.cpp!
1407 */
1408bool remR3CanExecuteRaw(CPUX86State *env, RTGCPTR eip, unsigned fFlags, int *piException)
1409{
1410 /* !!! THIS MUST BE IN SYNC WITH emR3Reschedule !!! */
1411 /* !!! THIS MUST BE IN SYNC WITH emR3Reschedule !!! */
1412 /* !!! THIS MUST BE IN SYNC WITH emR3Reschedule !!! */
1413 uint32_t u32CR0;
1414
1415#ifdef IEM_VERIFICATION_MODE
1416 return false;
1417#endif
1418
1419 /* Update counter. */
1420 env->pVM->rem.s.cCanExecuteRaw++;
1421
1422 /* Never when single stepping+logging guest code. */
1423 if (env->state & CPU_EMULATE_SINGLE_STEP)
1424 return false;
1425
1426 if (HMIsEnabled(env->pVM))
1427 {
1428#ifdef RT_OS_WINDOWS
1429 PCPUMCTX pCtx = alloca(sizeof(*pCtx));
1430#else
1431 CPUMCTX Ctx;
1432 PCPUMCTX pCtx = &Ctx;
1433#endif
1434
1435 env->state |= CPU_RAW_HM;
1436
1437 /*
1438 * The simple check first...
1439 */
1440 if (!EMIsHwVirtExecutionEnabled(env->pVM))
1441 return false;
1442
1443 /*
1444 * Create partial context for HMR3CanExecuteGuest
1445 */
1446 pCtx->cr0 = env->cr[0];
1447 pCtx->cr3 = env->cr[3];
1448 pCtx->cr4 = env->cr[4];
1449
1450 pCtx->tr.Sel = env->tr.selector;
1451 pCtx->tr.ValidSel = env->tr.selector;
1452 pCtx->tr.fFlags = CPUMSELREG_FLAGS_VALID;
1453 pCtx->tr.u64Base = env->tr.base;
1454 pCtx->tr.u32Limit = env->tr.limit;
1455 pCtx->tr.Attr.u = (env->tr.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1456
1457 pCtx->ldtr.Sel = env->ldt.selector;
1458 pCtx->ldtr.ValidSel = env->ldt.selector;
1459 pCtx->ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
1460 pCtx->ldtr.u64Base = env->ldt.base;
1461 pCtx->ldtr.u32Limit = env->ldt.limit;
1462 pCtx->ldtr.Attr.u = (env->ldt.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1463
1464 pCtx->idtr.cbIdt = env->idt.limit;
1465 pCtx->idtr.pIdt = env->idt.base;
1466
1467 pCtx->gdtr.cbGdt = env->gdt.limit;
1468 pCtx->gdtr.pGdt = env->gdt.base;
1469
1470 pCtx->rsp = env->regs[R_ESP];
1471 pCtx->rip = env->eip;
1472
1473 pCtx->eflags.u32 = env->eflags;
1474
1475 pCtx->cs.Sel = env->segs[R_CS].selector;
1476 pCtx->cs.ValidSel = env->segs[R_CS].selector;
1477 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1478 pCtx->cs.u64Base = env->segs[R_CS].base;
1479 pCtx->cs.u32Limit = env->segs[R_CS].limit;
1480 pCtx->cs.Attr.u = (env->segs[R_CS].flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1481
1482 pCtx->ds.Sel = env->segs[R_DS].selector;
1483 pCtx->ds.ValidSel = env->segs[R_DS].selector;
1484 pCtx->ds.fFlags = CPUMSELREG_FLAGS_VALID;
1485 pCtx->ds.u64Base = env->segs[R_DS].base;
1486 pCtx->ds.u32Limit = env->segs[R_DS].limit;
1487 pCtx->ds.Attr.u = (env->segs[R_DS].flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1488
1489 pCtx->es.Sel = env->segs[R_ES].selector;
1490 pCtx->es.ValidSel = env->segs[R_ES].selector;
1491 pCtx->es.fFlags = CPUMSELREG_FLAGS_VALID;
1492 pCtx->es.u64Base = env->segs[R_ES].base;
1493 pCtx->es.u32Limit = env->segs[R_ES].limit;
1494 pCtx->es.Attr.u = (env->segs[R_ES].flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1495
1496 pCtx->fs.Sel = env->segs[R_FS].selector;
1497 pCtx->fs.ValidSel = env->segs[R_FS].selector;
1498 pCtx->fs.fFlags = CPUMSELREG_FLAGS_VALID;
1499 pCtx->fs.u64Base = env->segs[R_FS].base;
1500 pCtx->fs.u32Limit = env->segs[R_FS].limit;
1501 pCtx->fs.Attr.u = (env->segs[R_FS].flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1502
1503 pCtx->gs.Sel = env->segs[R_GS].selector;
1504 pCtx->gs.ValidSel = env->segs[R_GS].selector;
1505 pCtx->gs.fFlags = CPUMSELREG_FLAGS_VALID;
1506 pCtx->gs.u64Base = env->segs[R_GS].base;
1507 pCtx->gs.u32Limit = env->segs[R_GS].limit;
1508 pCtx->gs.Attr.u = (env->segs[R_GS].flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1509
1510 pCtx->ss.Sel = env->segs[R_SS].selector;
1511 pCtx->ss.ValidSel = env->segs[R_SS].selector;
1512 pCtx->ss.fFlags = CPUMSELREG_FLAGS_VALID;
1513 pCtx->ss.u64Base = env->segs[R_SS].base;
1514 pCtx->ss.u32Limit = env->segs[R_SS].limit;
1515 pCtx->ss.Attr.u = (env->segs[R_SS].flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
1516
1517 pCtx->msrEFER = env->efer;
1518
1519 /* Hardware accelerated raw-mode:
1520 *
1521 * Typically only 32-bits protected mode, with paging enabled, code is allowed here.
1522 */
1523 if (HMR3CanExecuteGuest(env->pVM, pCtx) == true)
1524 {
1525 *piException = EXCP_EXECUTE_HM;
1526 return true;
1527 }
1528 return false;
1529 }
1530
1531 /*
1532 * Here we only support 16 & 32 bits protected mode ring 3 code that has no IO privileges
1533 * or 32 bits protected mode ring 0 code
1534 *
1535 * The tests are ordered by the likelihood of being true during normal execution.
1536 */
1537 if (fFlags & (HF_TF_MASK | HF_INHIBIT_IRQ_MASK))
1538 {
1539 STAM_COUNTER_INC(&gStatRefuseTFInhibit);
1540 Log2(("raw mode refused: fFlags=%#x\n", fFlags));
1541 return false;
1542 }
1543
1544#ifndef VBOX_RAW_V86
1545 if (fFlags & VM_MASK) {
1546 STAM_COUNTER_INC(&gStatRefuseVM86);
1547 Log2(("raw mode refused: VM_MASK\n"));
1548 return false;
1549 }
1550#endif
1551
1552 if (env->state & CPU_EMULATE_SINGLE_INSTR)
1553 {
1554#ifndef DEBUG_bird
1555 Log2(("raw mode refused: CPU_EMULATE_SINGLE_INSTR\n"));
1556#endif
1557 return false;
1558 }
1559
1560 if (env->singlestep_enabled)
1561 {
1562 //Log2(("raw mode refused: Single step\n"));
1563 return false;
1564 }
1565
1566 if (!QTAILQ_EMPTY(&env->breakpoints))
1567 {
1568 //Log2(("raw mode refused: Breakpoints\n"));
1569 return false;
1570 }
1571
1572 if (!QTAILQ_EMPTY(&env->watchpoints))
1573 {
1574 //Log2(("raw mode refused: Watchpoints\n"));
1575 return false;
1576 }
1577
1578 u32CR0 = env->cr[0];
1579 if ((u32CR0 & (X86_CR0_PG | X86_CR0_PE)) != (X86_CR0_PG | X86_CR0_PE))
1580 {
1581 STAM_COUNTER_INC(&gStatRefusePaging);
1582 //Log2(("raw mode refused: %s%s%s\n", (u32CR0 & X86_CR0_PG) ? "" : " !PG", (u32CR0 & X86_CR0_PE) ? "" : " !PE", (u32CR0 & X86_CR0_AM) ? "" : " !AM"));
1583 return false;
1584 }
1585
1586 if (env->cr[4] & CR4_PAE_MASK)
1587 {
1588 if (!(env->cpuid_features & X86_CPUID_FEATURE_EDX_PAE))
1589 {
1590 STAM_COUNTER_INC(&gStatRefusePAE);
1591 return false;
1592 }
1593 }
1594
1595 if (((fFlags >> HF_CPL_SHIFT) & 3) == 3)
1596 {
1597 if (!EMIsRawRing3Enabled(env->pVM))
1598 return false;
1599
1600 if (!(env->eflags & IF_MASK))
1601 {
1602 STAM_COUNTER_INC(&gStatRefuseIF0);
1603 Log2(("raw mode refused: IF (RawR3)\n"));
1604 return false;
1605 }
1606
1607 if (!(u32CR0 & CR0_WP_MASK) && EMIsRawRing0Enabled(env->pVM))
1608 {
1609 STAM_COUNTER_INC(&gStatRefuseWP0);
1610 Log2(("raw mode refused: CR0.WP + RawR0\n"));
1611 return false;
1612 }
1613 }
1614 else
1615 {
1616 if (!EMIsRawRing0Enabled(env->pVM))
1617 return false;
1618
1619 // Let's start with pure 32 bits ring 0 code first
1620 if ((fFlags & (HF_SS32_MASK | HF_CS32_MASK)) != (HF_SS32_MASK | HF_CS32_MASK))
1621 {
1622 STAM_COUNTER_INC(&gStatRefuseCode16);
1623 Log2(("raw r0 mode refused: HF_[S|C]S32_MASK fFlags=%#x\n", fFlags));
1624 return false;
1625 }
1626
1627 if (EMIsRawRing1Enabled(env->pVM))
1628 {
1629 /* Only ring 0 and 1 supervisor code. */
1630 if (((fFlags >> HF_CPL_SHIFT) & 3) == 2) /* ring 1 code is moved into ring 2, so we can't support ring-2 in that case. */
1631 {
1632 Log2(("raw r0 mode refused: CPL %d\n", (fFlags >> HF_CPL_SHIFT) & 3));
1633 return false;
1634 }
1635 }
1636 /* Only R0. */
1637 else if (((fFlags >> HF_CPL_SHIFT) & 3) != 0)
1638 {
1639 STAM_COUNTER_INC(&gStatRefuseRing1or2);
1640 Log2(("raw r0 mode refused: CPL %d\n", ((fFlags >> HF_CPL_SHIFT) & 3) ));
1641 return false;
1642 }
1643
1644 if (!(u32CR0 & CR0_WP_MASK))
1645 {
1646 STAM_COUNTER_INC(&gStatRefuseWP0);
1647 Log2(("raw r0 mode refused: CR0.WP=0!\n"));
1648 return false;
1649 }
1650
1651#ifdef VBOX_WITH_RAW_MODE
1652 if (PATMIsPatchGCAddr(env->pVM, eip))
1653 {
1654 Log2(("raw r0 mode forced: patch code\n"));
1655 *piException = EXCP_EXECUTE_RAW;
1656 return true;
1657 }
1658#endif
1659
1660#if !defined(VBOX_ALLOW_IF0) && !defined(VBOX_RUN_INTERRUPT_GATE_HANDLERS)
1661 if (!(env->eflags & IF_MASK))
1662 {
1663 STAM_COUNTER_INC(&gStatRefuseIF0);
1664 ////Log2(("R0: IF=0 VIF=%d %08X\n", eip, *env->pVMeflags));
1665 //Log2(("RR0: Interrupts turned off; fall back to emulation\n"));
1666 return false;
1667 }
1668#endif
1669
1670#ifndef VBOX_WITH_RAW_RING1
1671 if (((env->eflags >> IOPL_SHIFT) & 3) != 0)
1672 {
1673 Log2(("raw r0 mode refused: IOPL %d\n", ((env->eflags >> IOPL_SHIFT) & 3)));
1674 return false;
1675 }
1676#endif
1677 env->state |= CPU_RAW_RING0;
1678 }
1679
1680 /*
1681 * Don't reschedule the first time we're called, because there might be
1682 * special reasons why we're here that is not covered by the above checks.
1683 */
1684 if (env->pVM->rem.s.cCanExecuteRaw == 1)
1685 {
1686 Log2(("raw mode refused: first scheduling\n"));
1687 STAM_COUNTER_INC(&gStatRefuseCanExecute);
1688 return false;
1689 }
1690
1691 /*
1692 * Stale hidden selectors means raw-mode is unsafe (being very careful).
1693 */
1694 if (env->segs[R_CS].fVBoxFlags & CPUMSELREG_FLAGS_STALE)
1695 {
1696 Log2(("raw mode refused: stale CS (%#x)\n", env->segs[R_CS].selector));
1697 STAM_COUNTER_INC(&gaStatRefuseStale[R_CS]);
1698 return false;
1699 }
1700 if (env->segs[R_SS].fVBoxFlags & CPUMSELREG_FLAGS_STALE)
1701 {
1702 Log2(("raw mode refused: stale SS (%#x)\n", env->segs[R_SS].selector));
1703 STAM_COUNTER_INC(&gaStatRefuseStale[R_SS]);
1704 return false;
1705 }
1706 if (env->segs[R_DS].fVBoxFlags & CPUMSELREG_FLAGS_STALE)
1707 {
1708 Log2(("raw mode refused: stale DS (%#x)\n", env->segs[R_DS].selector));
1709 STAM_COUNTER_INC(&gaStatRefuseStale[R_DS]);
1710 return false;
1711 }
1712 if (env->segs[R_ES].fVBoxFlags & CPUMSELREG_FLAGS_STALE)
1713 {
1714 Log2(("raw mode refused: stale ES (%#x)\n", env->segs[R_ES].selector));
1715 STAM_COUNTER_INC(&gaStatRefuseStale[R_ES]);
1716 return false;
1717 }
1718 if (env->segs[R_FS].fVBoxFlags & CPUMSELREG_FLAGS_STALE)
1719 {
1720 Log2(("raw mode refused: stale FS (%#x)\n", env->segs[R_FS].selector));
1721 STAM_COUNTER_INC(&gaStatRefuseStale[R_FS]);
1722 return false;
1723 }
1724 if (env->segs[R_GS].fVBoxFlags & CPUMSELREG_FLAGS_STALE)
1725 {
1726 Log2(("raw mode refused: stale GS (%#x)\n", env->segs[R_GS].selector));
1727 STAM_COUNTER_INC(&gaStatRefuseStale[R_GS]);
1728 return false;
1729 }
1730
1731/* Assert(env->pVCpu && PGMPhysIsA20Enabled(env->pVCpu));*/
1732 *piException = EXCP_EXECUTE_RAW;
1733 return true;
1734}
1735
1736
1737#ifdef VBOX_WITH_RAW_MODE
1738/**
1739 * Fetches a code byte.
1740 *
1741 * @returns Success indicator (bool) for ease of use.
1742 * @param env The CPU environment structure.
1743 * @param GCPtrInstr Where to fetch code.
1744 * @param pu8Byte Where to store the byte on success
1745 */
1746bool remR3GetOpcode(CPUX86State *env, RTGCPTR GCPtrInstr, uint8_t *pu8Byte)
1747{
1748 int rc = PATMR3QueryOpcode(env->pVM, GCPtrInstr, pu8Byte);
1749 if (RT_SUCCESS(rc))
1750 return true;
1751 return false;
1752}
1753#endif /* VBOX_WITH_RAW_MODE */
1754
1755
1756/**
1757 * Flush (or invalidate if you like) page table/dir entry.
1758 *
1759 * (invlpg instruction; tlb_flush_page)
1760 *
1761 * @param env Pointer to cpu environment.
1762 * @param GCPtr The virtual address which page table/dir entry should be invalidated.
1763 */
1764void remR3FlushPage(CPUX86State *env, RTGCPTR GCPtr)
1765{
1766 PVM pVM = env->pVM;
1767 PCPUMCTX pCtx;
1768 int rc;
1769
1770 Assert(EMRemIsLockOwner(env->pVM));
1771
1772 /*
1773 * When we're replaying invlpg instructions or restoring a saved
1774 * state we disable this path.
1775 */
1776 if (pVM->rem.s.fIgnoreInvlPg || pVM->rem.s.cIgnoreAll)
1777 return;
1778 LogFlow(("remR3FlushPage: GCPtr=%RGv\n", GCPtr));
1779 Assert(pVM->rem.s.fInREM || pVM->rem.s.fInStateSync);
1780
1781 //RAWEx_ProfileStop(env, STATS_QEMU_TOTAL);
1782
1783 /*
1784 * Update the control registers before calling PGMFlushPage.
1785 */
1786 pCtx = (PCPUMCTX)pVM->rem.s.pCtx;
1787 Assert(pCtx);
1788 pCtx->cr0 = env->cr[0];
1789 pCtx->cr3 = env->cr[3];
1790#ifdef VBOX_WITH_RAW_MODE
1791 if (((env->cr[4] ^ pCtx->cr4) & X86_CR4_VME) && !HMIsEnabled(pVM))
1792 VMCPU_FF_SET(env->pVCpu, VMCPU_FF_SELM_SYNC_TSS);
1793#endif
1794 pCtx->cr4 = env->cr[4];
1795
1796 /*
1797 * Let PGM do the rest.
1798 */
1799 Assert(env->pVCpu);
1800 rc = PGMInvalidatePage(env->pVCpu, GCPtr);
1801 if (RT_FAILURE(rc))
1802 {
1803 AssertMsgFailed(("remR3FlushPage %RGv failed with %d!!\n", GCPtr, rc));
1804 VMCPU_FF_SET(env->pVCpu, VMCPU_FF_PGM_SYNC_CR3);
1805 }
1806 //RAWEx_ProfileStart(env, STATS_QEMU_TOTAL);
1807}
1808
1809
1810#ifndef REM_PHYS_ADDR_IN_TLB
1811/** Wrapper for PGMR3PhysTlbGCPhys2Ptr. */
1812void *remR3TlbGCPhys2Ptr(CPUX86State *env1, target_ulong physAddr, int fWritable)
1813{
1814 void *pv;
1815 int rc;
1816
1817
1818 /* Address must be aligned enough to fiddle with lower bits */
1819 Assert((physAddr & 0x3) == 0);
1820 /*AssertMsg((env1->a20_mask & physAddr) == physAddr, ("%llx\n", (uint64_t)physAddr));*/
1821
1822 STAM_PROFILE_START(&gStatGCPhys2HCVirt, a);
1823 rc = PGMR3PhysTlbGCPhys2Ptr(env1->pVM, physAddr, true /*fWritable*/, &pv);
1824 STAM_PROFILE_STOP(&gStatGCPhys2HCVirt, a);
1825 Assert( rc == VINF_SUCCESS
1826 || rc == VINF_PGM_PHYS_TLB_CATCH_WRITE
1827 || rc == VERR_PGM_PHYS_TLB_CATCH_ALL
1828 || rc == VERR_PGM_PHYS_TLB_UNASSIGNED);
1829 if (RT_FAILURE(rc))
1830 return (void *)1;
1831 if (rc == VINF_PGM_PHYS_TLB_CATCH_WRITE)
1832 return (void *)((uintptr_t)pv | 2);
1833 return pv;
1834}
1835#endif /* REM_PHYS_ADDR_IN_TLB */
1836
1837
1838/**
1839 * Called from tlb_protect_code in order to write monitor a code page.
1840 *
1841 * @param env Pointer to the CPU environment.
1842 * @param GCPtr Code page to monitor
1843 */
1844void remR3ProtectCode(CPUX86State *env, RTGCPTR GCPtr)
1845{
1846#ifdef VBOX_REM_PROTECT_PAGES_FROM_SMC
1847 Assert(env->pVM->rem.s.fInREM);
1848 if ( (env->cr[0] & X86_CR0_PG) /* paging must be enabled */
1849 && !(env->state & CPU_EMULATE_SINGLE_INSTR) /* ignore during single instruction execution */
1850 && (((env->hflags >> HF_CPL_SHIFT) & 3) == 0) /* supervisor mode only */
1851 && !(env->eflags & VM_MASK) /* no V86 mode */
1852 && !HMIsEnabled(env->pVM))
1853 CSAMR3MonitorPage(env->pVM, GCPtr, CSAM_TAG_REM);
1854#endif
1855}
1856
1857
1858/**
1859 * Called from tlb_unprotect_code in order to clear write monitoring for a code page.
1860 *
1861 * @param env Pointer to the CPU environment.
1862 * @param GCPtr Code page to monitor
1863 */
1864void remR3UnprotectCode(CPUX86State *env, RTGCPTR GCPtr)
1865{
1866 Assert(env->pVM->rem.s.fInREM);
1867#ifdef VBOX_REM_PROTECT_PAGES_FROM_SMC
1868 if ( (env->cr[0] & X86_CR0_PG) /* paging must be enabled */
1869 && !(env->state & CPU_EMULATE_SINGLE_INSTR) /* ignore during single instruction execution */
1870 && (((env->hflags >> HF_CPL_SHIFT) & 3) == 0) /* supervisor mode only */
1871 && !(env->eflags & VM_MASK) /* no V86 mode */
1872 && !HMIsEnabled(env->pVM))
1873 CSAMR3UnmonitorPage(env->pVM, GCPtr, CSAM_TAG_REM);
1874#endif
1875}
1876
1877
1878/**
1879 * Called when the CPU is initialized, any of the CRx registers are changed or
1880 * when the A20 line is modified.
1881 *
1882 * @param env Pointer to the CPU environment.
1883 * @param fGlobal Set if the flush is global.
1884 */
1885void remR3FlushTLB(CPUX86State *env, bool fGlobal)
1886{
1887 PVM pVM = env->pVM;
1888 PCPUMCTX pCtx;
1889 Assert(EMRemIsLockOwner(pVM));
1890
1891 /*
1892 * When we're replaying invlpg instructions or restoring a saved
1893 * state we disable this path.
1894 */
1895 if (pVM->rem.s.fIgnoreCR3Load || pVM->rem.s.cIgnoreAll)
1896 return;
1897 Assert(pVM->rem.s.fInREM);
1898
1899 /*
1900 * The caller doesn't check cr4, so we have to do that for ourselves.
1901 */
1902 if (!fGlobal && !(env->cr[4] & X86_CR4_PGE))
1903 fGlobal = true;
1904 Log(("remR3FlushTLB: CR0=%08RX64 CR3=%08RX64 CR4=%08RX64 %s\n", (uint64_t)env->cr[0], (uint64_t)env->cr[3], (uint64_t)env->cr[4], fGlobal ? " global" : ""));
1905
1906 /*
1907 * Update the control registers before calling PGMR3FlushTLB.
1908 */
1909 pCtx = (PCPUMCTX)pVM->rem.s.pCtx;
1910 Assert(pCtx);
1911 pCtx->cr0 = env->cr[0];
1912 pCtx->cr3 = env->cr[3];
1913#ifdef VBOX_WITH_RAW_MODE
1914 if (((env->cr[4] ^ pCtx->cr4) & X86_CR4_VME) && !HMIsEnabled(pVM))
1915 VMCPU_FF_SET(env->pVCpu, VMCPU_FF_SELM_SYNC_TSS);
1916#endif
1917 pCtx->cr4 = env->cr[4];
1918
1919 /*
1920 * Let PGM do the rest.
1921 */
1922 Assert(env->pVCpu);
1923 PGMFlushTLB(env->pVCpu, env->cr[3], fGlobal);
1924}
1925
1926
1927/**
1928 * Called when any of the cr0, cr4 or efer registers is updated.
1929 *
1930 * @param env Pointer to the CPU environment.
1931 */
1932void remR3ChangeCpuMode(CPUX86State *env)
1933{
1934 PVM pVM = env->pVM;
1935 uint64_t efer;
1936 PCPUMCTX pCtx;
1937 int rc;
1938
1939 /*
1940 * When we're replaying loads or restoring a saved
1941 * state this path is disabled.
1942 */
1943 if (pVM->rem.s.fIgnoreCpuMode || pVM->rem.s.cIgnoreAll)
1944 return;
1945 Assert(pVM->rem.s.fInREM);
1946
1947 pCtx = (PCPUMCTX)pVM->rem.s.pCtx;
1948 Assert(pCtx);
1949
1950 /*
1951 * Notify PGM about WP0 being enabled (like CPUSetGuestCR0 does).
1952 */
1953 if (((env->cr[0] ^ pCtx->cr0) & X86_CR0_WP) && (env->cr[0] & X86_CR0_WP))
1954 PGMCr0WpEnabled(env->pVCpu);
1955
1956 /*
1957 * Update the control registers before calling PGMChangeMode()
1958 * as it may need to map whatever cr3 is pointing to.
1959 */
1960 pCtx->cr0 = env->cr[0];
1961 pCtx->cr3 = env->cr[3];
1962#ifdef VBOX_WITH_RAW_MODE
1963 if (((env->cr[4] ^ pCtx->cr4) & X86_CR4_VME) && !HMIsEnabled(pVM))
1964 VMCPU_FF_SET(env->pVCpu, VMCPU_FF_SELM_SYNC_TSS);
1965#endif
1966 pCtx->cr4 = env->cr[4];
1967#ifdef TARGET_X86_64
1968 efer = env->efer;
1969 pCtx->msrEFER = efer;
1970#else
1971 efer = 0;
1972#endif
1973 Assert(env->pVCpu);
1974 rc = PGMChangeMode(env->pVCpu, env->cr[0], env->cr[4], efer);
1975 if (rc != VINF_SUCCESS)
1976 {
1977 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
1978 {
1979 Log(("PGMChangeMode(, %RX64, %RX64, %RX64) -> %Rrc -> remR3RaiseRC\n", env->cr[0], env->cr[4], efer, rc));
1980 remR3RaiseRC(env->pVM, rc);
1981 }
1982 else
1983 cpu_abort(env, "PGMChangeMode(, %RX64, %RX64, %RX64) -> %Rrc\n", env->cr[0], env->cr[4], efer, rc);
1984 }
1985}
1986
1987
1988/**
1989 * Called from compiled code to run dma.
1990 *
1991 * @param env Pointer to the CPU environment.
1992 */
1993void remR3DmaRun(CPUX86State *env)
1994{
1995 remR3ProfileStop(STATS_QEMU_RUN_EMULATED_CODE);
1996 PDMR3DmaRun(env->pVM);
1997 remR3ProfileStart(STATS_QEMU_RUN_EMULATED_CODE);
1998}
1999
2000
2001/**
2002 * Called from compiled code to schedule pending timers in VMM
2003 *
2004 * @param env Pointer to the CPU environment.
2005 */
2006void remR3TimersRun(CPUX86State *env)
2007{
2008 LogFlow(("remR3TimersRun:\n"));
2009 LogIt(RTLOGGRPFLAGS_LEVEL_5, LOG_GROUP_TM, ("remR3TimersRun\n"));
2010 remR3ProfileStop(STATS_QEMU_RUN_EMULATED_CODE);
2011 remR3ProfileStart(STATS_QEMU_RUN_TIMERS);
2012 TMR3TimerQueuesDo(env->pVM);
2013 remR3ProfileStop(STATS_QEMU_RUN_TIMERS);
2014 remR3ProfileStart(STATS_QEMU_RUN_EMULATED_CODE);
2015}
2016
2017
2018/**
2019 * Record trap occurrence
2020 *
2021 * @returns VBox status code
2022 * @param env Pointer to the CPU environment.
2023 * @param uTrap Trap nr
2024 * @param uErrorCode Error code
2025 * @param pvNextEIP Next EIP
2026 */
2027int remR3NotifyTrap(CPUX86State *env, uint32_t uTrap, uint32_t uErrorCode, RTGCPTR pvNextEIP)
2028{
2029 PVM pVM = env->pVM;
2030#ifdef VBOX_WITH_STATISTICS
2031 static STAMCOUNTER s_aStatTrap[255];
2032 static bool s_aRegisters[RT_ELEMENTS(s_aStatTrap)];
2033#endif
2034
2035#ifdef VBOX_WITH_STATISTICS
2036 if (uTrap < 255)
2037 {
2038 if (!s_aRegisters[uTrap])
2039 {
2040 char szStatName[64];
2041 s_aRegisters[uTrap] = true;
2042 RTStrPrintf(szStatName, sizeof(szStatName), "/REM/Trap/0x%02X", uTrap);
2043 STAM_REG(env->pVM, &s_aStatTrap[uTrap], STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Trap stats.");
2044 }
2045 STAM_COUNTER_INC(&s_aStatTrap[uTrap]);
2046 }
2047#endif
2048 Log(("remR3NotifyTrap: uTrap=%x error=%x next_eip=%RGv eip=%RGv cr2=%RGv\n", uTrap, uErrorCode, pvNextEIP, (RTGCPTR)env->eip, (RTGCPTR)env->cr[2]));
2049 if( uTrap < 0x20
2050 && (env->cr[0] & X86_CR0_PE)
2051 && !(env->eflags & X86_EFL_VM))
2052 {
2053#ifdef DEBUG
2054 remR3DisasInstr(env, 1, "remR3NotifyTrap: ");
2055#endif
2056 if(pVM->rem.s.uPendingException == uTrap && ++pVM->rem.s.cPendingExceptions > 512)
2057 {
2058 LogRel(("VERR_REM_TOO_MANY_TRAPS -> uTrap=%x error=%x next_eip=%RGv eip=%RGv cr2=%RGv\n", uTrap, uErrorCode, pvNextEIP, (RTGCPTR)env->eip, (RTGCPTR)env->cr[2]));
2059 remR3RaiseRC(env->pVM, VERR_REM_TOO_MANY_TRAPS);
2060 return VERR_REM_TOO_MANY_TRAPS;
2061 }
2062 if(pVM->rem.s.uPendingException != uTrap || pVM->rem.s.uPendingExcptEIP != env->eip || pVM->rem.s.uPendingExcptCR2 != env->cr[2])
2063 {
2064 Log(("remR3NotifyTrap: uTrap=%#x set as pending\n", uTrap));
2065 pVM->rem.s.cPendingExceptions = 1;
2066 }
2067 pVM->rem.s.uPendingException = uTrap;
2068 pVM->rem.s.uPendingExcptEIP = env->eip;
2069 pVM->rem.s.uPendingExcptCR2 = env->cr[2];
2070 }
2071 else
2072 {
2073 pVM->rem.s.cPendingExceptions = 0;
2074 pVM->rem.s.uPendingException = uTrap;
2075 pVM->rem.s.uPendingExcptEIP = env->eip;
2076 pVM->rem.s.uPendingExcptCR2 = env->cr[2];
2077 }
2078 return VINF_SUCCESS;
2079}
2080
2081
2082/*
2083 * Clear current active trap
2084 *
2085 * @param pVM VM Handle.
2086 */
2087void remR3TrapClear(PVM pVM)
2088{
2089 pVM->rem.s.cPendingExceptions = 0;
2090 pVM->rem.s.uPendingException = 0;
2091 pVM->rem.s.uPendingExcptEIP = 0;
2092 pVM->rem.s.uPendingExcptCR2 = 0;
2093}
2094
2095
2096/*
2097 * Record previous call instruction addresses
2098 *
2099 * @param env Pointer to the CPU environment.
2100 */
2101void remR3RecordCall(CPUX86State *env)
2102{
2103#ifdef VBOX_WITH_RAW_MODE
2104 CSAMR3RecordCallAddress(env->pVM, env->eip);
2105#endif
2106}
2107
2108
2109/**
2110 * Syncs the internal REM state with the VM.
2111 *
2112 * This must be called before REMR3Run() is invoked whenever when the REM
2113 * state is not up to date. Calling it several times in a row is not
2114 * permitted.
2115 *
2116 * @returns VBox status code.
2117 *
2118 * @param pVM VM Handle.
2119 * @param pVCpu VMCPU Handle.
2120 *
2121 * @remark The caller has to check for important FFs before calling REMR3Run. REMR3State will
2122 * no do this since the majority of the callers don't want any unnecessary of events
2123 * pending that would immediately interrupt execution.
2124 */
2125REMR3DECL(int) REMR3State(PVM pVM, PVMCPU pVCpu)
2126{
2127 register const CPUMCTX *pCtx;
2128 register unsigned fFlags;
2129 unsigned i;
2130 TRPMEVENT enmType;
2131 uint8_t u8TrapNo;
2132 uint32_t uCpl;
2133 int rc;
2134
2135 STAM_PROFILE_START(&pVM->rem.s.StatsState, a);
2136 Log2(("REMR3State:\n"));
2137
2138 pVM->rem.s.Env.pVCpu = pVCpu;
2139 pCtx = pVM->rem.s.pCtx = CPUMQueryGuestCtxPtr(pVCpu);
2140
2141 Assert(!pVM->rem.s.fInREM);
2142 pVM->rem.s.fInStateSync = true;
2143
2144 /*
2145 * If we have to flush TBs, do that immediately.
2146 */
2147 if (pVM->rem.s.fFlushTBs)
2148 {
2149 STAM_COUNTER_INC(&gStatFlushTBs);
2150 tb_flush(&pVM->rem.s.Env);
2151 pVM->rem.s.fFlushTBs = false;
2152 }
2153
2154 /*
2155 * Copy the registers which require no special handling.
2156 */
2157#ifdef TARGET_X86_64
2158 /* Note that the high dwords of 64 bits registers are undefined in 32 bits mode and are undefined after a mode change. */
2159 Assert(R_EAX == 0);
2160 pVM->rem.s.Env.regs[R_EAX] = pCtx->rax;
2161 Assert(R_ECX == 1);
2162 pVM->rem.s.Env.regs[R_ECX] = pCtx->rcx;
2163 Assert(R_EDX == 2);
2164 pVM->rem.s.Env.regs[R_EDX] = pCtx->rdx;
2165 Assert(R_EBX == 3);
2166 pVM->rem.s.Env.regs[R_EBX] = pCtx->rbx;
2167 Assert(R_ESP == 4);
2168 pVM->rem.s.Env.regs[R_ESP] = pCtx->rsp;
2169 Assert(R_EBP == 5);
2170 pVM->rem.s.Env.regs[R_EBP] = pCtx->rbp;
2171 Assert(R_ESI == 6);
2172 pVM->rem.s.Env.regs[R_ESI] = pCtx->rsi;
2173 Assert(R_EDI == 7);
2174 pVM->rem.s.Env.regs[R_EDI] = pCtx->rdi;
2175 pVM->rem.s.Env.regs[8] = pCtx->r8;
2176 pVM->rem.s.Env.regs[9] = pCtx->r9;
2177 pVM->rem.s.Env.regs[10] = pCtx->r10;
2178 pVM->rem.s.Env.regs[11] = pCtx->r11;
2179 pVM->rem.s.Env.regs[12] = pCtx->r12;
2180 pVM->rem.s.Env.regs[13] = pCtx->r13;
2181 pVM->rem.s.Env.regs[14] = pCtx->r14;
2182 pVM->rem.s.Env.regs[15] = pCtx->r15;
2183
2184 pVM->rem.s.Env.eip = pCtx->rip;
2185
2186 pVM->rem.s.Env.eflags = pCtx->rflags.u64;
2187#else
2188 Assert(R_EAX == 0);
2189 pVM->rem.s.Env.regs[R_EAX] = pCtx->eax;
2190 Assert(R_ECX == 1);
2191 pVM->rem.s.Env.regs[R_ECX] = pCtx->ecx;
2192 Assert(R_EDX == 2);
2193 pVM->rem.s.Env.regs[R_EDX] = pCtx->edx;
2194 Assert(R_EBX == 3);
2195 pVM->rem.s.Env.regs[R_EBX] = pCtx->ebx;
2196 Assert(R_ESP == 4);
2197 pVM->rem.s.Env.regs[R_ESP] = pCtx->esp;
2198 Assert(R_EBP == 5);
2199 pVM->rem.s.Env.regs[R_EBP] = pCtx->ebp;
2200 Assert(R_ESI == 6);
2201 pVM->rem.s.Env.regs[R_ESI] = pCtx->esi;
2202 Assert(R_EDI == 7);
2203 pVM->rem.s.Env.regs[R_EDI] = pCtx->edi;
2204 pVM->rem.s.Env.eip = pCtx->eip;
2205
2206 pVM->rem.s.Env.eflags = pCtx->eflags.u32;
2207#endif
2208
2209 pVM->rem.s.Env.cr[2] = pCtx->cr2;
2210
2211 /** @todo we could probably benefit from using a CPUM_CHANGED_DRx flag too! */
2212 for (i=0;i<8;i++)
2213 pVM->rem.s.Env.dr[i] = pCtx->dr[i];
2214
2215#ifdef HF_HALTED_MASK /** @todo remove me when we're up to date again. */
2216 /*
2217 * Clear the halted hidden flag (the interrupt waking up the CPU can
2218 * have been dispatched in raw mode).
2219 */
2220 pVM->rem.s.Env.hflags &= ~HF_HALTED_MASK;
2221#endif
2222
2223 /*
2224 * Replay invlpg? Only if we're not flushing the TLB.
2225 */
2226 fFlags = CPUMR3RemEnter(pVCpu, &uCpl);
2227 LogFlow(("CPUMR3RemEnter %x %x\n", fFlags, uCpl));
2228 if (pVM->rem.s.cInvalidatedPages)
2229 {
2230 if (!(fFlags & CPUM_CHANGED_GLOBAL_TLB_FLUSH))
2231 {
2232 RTUINT i;
2233
2234 pVM->rem.s.fIgnoreCR3Load = true;
2235 pVM->rem.s.fIgnoreInvlPg = true;
2236 for (i = 0; i < pVM->rem.s.cInvalidatedPages; i++)
2237 {
2238 Log2(("REMR3State: invlpg %RGv\n", pVM->rem.s.aGCPtrInvalidatedPages[i]));
2239 tlb_flush_page(&pVM->rem.s.Env, pVM->rem.s.aGCPtrInvalidatedPages[i]);
2240 }
2241 pVM->rem.s.fIgnoreInvlPg = false;
2242 pVM->rem.s.fIgnoreCR3Load = false;
2243 }
2244 pVM->rem.s.cInvalidatedPages = 0;
2245 }
2246
2247 /* Replay notification changes. */
2248 REMR3ReplayHandlerNotifications(pVM);
2249
2250 /* Update MSRs; before CRx registers! */
2251 pVM->rem.s.Env.efer = pCtx->msrEFER;
2252 pVM->rem.s.Env.star = pCtx->msrSTAR;
2253 pVM->rem.s.Env.pat = pCtx->msrPAT;
2254#ifdef TARGET_X86_64
2255 pVM->rem.s.Env.lstar = pCtx->msrLSTAR;
2256 pVM->rem.s.Env.cstar = pCtx->msrCSTAR;
2257 pVM->rem.s.Env.fmask = pCtx->msrSFMASK;
2258 pVM->rem.s.Env.kernelgsbase = pCtx->msrKERNELGSBASE;
2259
2260 /* Update the internal long mode activate flag according to the new EFER value. */
2261 if (pCtx->msrEFER & MSR_K6_EFER_LMA)
2262 pVM->rem.s.Env.hflags |= HF_LMA_MASK;
2263 else
2264 pVM->rem.s.Env.hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
2265#endif
2266
2267 /* Update the inhibit IRQ mask. */
2268 pVM->rem.s.Env.hflags &= ~HF_INHIBIT_IRQ_MASK;
2269 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
2270 {
2271 RTGCPTR InhibitPC = EMGetInhibitInterruptsPC(pVCpu);
2272 if (InhibitPC == pCtx->rip)
2273 pVM->rem.s.Env.hflags |= HF_INHIBIT_IRQ_MASK;
2274 else
2275 {
2276 Log(("Clearing VMCPU_FF_INHIBIT_INTERRUPTS at %RGv - successor %RGv (REM#1)\n", (RTGCPTR)pCtx->rip, InhibitPC));
2277 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2278 }
2279 }
2280
2281 /* Update the inhibit NMI mask. */
2282 pVM->rem.s.Env.hflags2 &= ~HF2_NMI_MASK;
2283 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS))
2284 pVM->rem.s.Env.hflags2 |= HF2_NMI_MASK;
2285
2286 /*
2287 * Sync the A20 gate.
2288 */
2289 bool fA20State = PGMPhysIsA20Enabled(pVCpu);
2290 if (fA20State != RT_BOOL(pVM->rem.s.Env.a20_mask & RT_BIT(20)))
2291 {
2292 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
2293 cpu_x86_set_a20(&pVM->rem.s.Env, fA20State);
2294 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
2295 }
2296
2297 /*
2298 * Registers which are rarely changed and require special handling / order when changed.
2299 */
2300 if (fFlags & ( CPUM_CHANGED_GLOBAL_TLB_FLUSH
2301 | CPUM_CHANGED_CR4
2302 | CPUM_CHANGED_CR0
2303 | CPUM_CHANGED_CR3
2304 | CPUM_CHANGED_GDTR
2305 | CPUM_CHANGED_IDTR
2306 | CPUM_CHANGED_SYSENTER_MSR
2307 | CPUM_CHANGED_LDTR
2308 | CPUM_CHANGED_CPUID
2309 | CPUM_CHANGED_FPU_REM
2310 )
2311 )
2312 {
2313 if (fFlags & CPUM_CHANGED_GLOBAL_TLB_FLUSH)
2314 {
2315 pVM->rem.s.fIgnoreCR3Load = true;
2316 tlb_flush(&pVM->rem.s.Env, true);
2317 pVM->rem.s.fIgnoreCR3Load = false;
2318 }
2319
2320 /* CR4 before CR0! */
2321 if (fFlags & CPUM_CHANGED_CR4)
2322 {
2323 pVM->rem.s.fIgnoreCR3Load = true;
2324 pVM->rem.s.fIgnoreCpuMode = true;
2325 cpu_x86_update_cr4(&pVM->rem.s.Env, pCtx->cr4);
2326 pVM->rem.s.fIgnoreCpuMode = false;
2327 pVM->rem.s.fIgnoreCR3Load = false;
2328 }
2329
2330 if (fFlags & CPUM_CHANGED_CR0)
2331 {
2332 pVM->rem.s.fIgnoreCR3Load = true;
2333 pVM->rem.s.fIgnoreCpuMode = true;
2334 cpu_x86_update_cr0(&pVM->rem.s.Env, pCtx->cr0);
2335 pVM->rem.s.fIgnoreCpuMode = false;
2336 pVM->rem.s.fIgnoreCR3Load = false;
2337 }
2338
2339 if (fFlags & CPUM_CHANGED_CR3)
2340 {
2341 pVM->rem.s.fIgnoreCR3Load = true;
2342 cpu_x86_update_cr3(&pVM->rem.s.Env, pCtx->cr3);
2343 pVM->rem.s.fIgnoreCR3Load = false;
2344 }
2345
2346 if (fFlags & CPUM_CHANGED_GDTR)
2347 {
2348 pVM->rem.s.Env.gdt.base = pCtx->gdtr.pGdt;
2349 pVM->rem.s.Env.gdt.limit = pCtx->gdtr.cbGdt;
2350 }
2351
2352 if (fFlags & CPUM_CHANGED_IDTR)
2353 {
2354 pVM->rem.s.Env.idt.base = pCtx->idtr.pIdt;
2355 pVM->rem.s.Env.idt.limit = pCtx->idtr.cbIdt;
2356 }
2357
2358 if (fFlags & CPUM_CHANGED_SYSENTER_MSR)
2359 {
2360 pVM->rem.s.Env.sysenter_cs = pCtx->SysEnter.cs;
2361 pVM->rem.s.Env.sysenter_eip = pCtx->SysEnter.eip;
2362 pVM->rem.s.Env.sysenter_esp = pCtx->SysEnter.esp;
2363 }
2364
2365 if (fFlags & CPUM_CHANGED_LDTR)
2366 {
2367 if (pCtx->ldtr.fFlags & CPUMSELREG_FLAGS_VALID)
2368 {
2369 pVM->rem.s.Env.ldt.selector = pCtx->ldtr.Sel;
2370 pVM->rem.s.Env.ldt.newselector = 0;
2371 pVM->rem.s.Env.ldt.fVBoxFlags = pCtx->ldtr.fFlags;
2372 pVM->rem.s.Env.ldt.base = pCtx->ldtr.u64Base;
2373 pVM->rem.s.Env.ldt.limit = pCtx->ldtr.u32Limit;
2374 pVM->rem.s.Env.ldt.flags = (pCtx->ldtr.Attr.u & SEL_FLAGS_SMASK) << SEL_FLAGS_SHIFT;
2375 }
2376 else
2377 {
2378 AssertFailed(); /* Shouldn't happen, see cpumR3LoadExec. */
2379 sync_ldtr(&pVM->rem.s.Env, pCtx->ldtr.Sel);
2380 }
2381 }
2382
2383 if (fFlags & CPUM_CHANGED_CPUID)
2384 {
2385 uint32_t u32Dummy;
2386
2387 /*
2388 * Get the CPUID features.
2389 */
2390 CPUMGetGuestCpuId(pVCpu, 1, 0, &u32Dummy, &u32Dummy, &pVM->rem.s.Env.cpuid_ext_features, &pVM->rem.s.Env.cpuid_features);
2391 CPUMGetGuestCpuId(pVCpu, 0x80000001, 0, &u32Dummy, &u32Dummy, &u32Dummy, &pVM->rem.s.Env.cpuid_ext2_features);
2392 }
2393
2394 /* Sync FPU state after CR4, CPUID and EFER (!). */
2395 if (fFlags & CPUM_CHANGED_FPU_REM)
2396 save_raw_fp_state(&pVM->rem.s.Env, (uint8_t *)&pCtx->pXStateR3->x87); /* 'save' is an excellent name. */
2397 }
2398
2399 /*
2400 * Sync TR unconditionally to make life simpler.
2401 */
2402 pVM->rem.s.Env.tr.selector = pCtx->tr.Sel;
2403 pVM->rem.s.Env.tr.newselector = 0;
2404 pVM->rem.s.Env.tr.fVBoxFlags = pCtx->tr.fFlags;
2405 pVM->rem.s.Env.tr.base = pCtx->tr.u64Base;
2406 pVM->rem.s.Env.tr.limit = pCtx->tr.u32Limit;
2407 pVM->rem.s.Env.tr.flags = (pCtx->tr.Attr.u & SEL_FLAGS_SMASK) << SEL_FLAGS_SHIFT;
2408 /* Note! do_interrupt will fault if the busy flag is still set... */ /** @todo so fix do_interrupt then! */
2409 pVM->rem.s.Env.tr.flags &= ~DESC_TSS_BUSY_MASK;
2410
2411 /*
2412 * Update selector registers.
2413 *
2414 * This must be done *after* we've synced gdt, ldt and crX registers
2415 * since we're reading the GDT/LDT om sync_seg. This will happen with
2416 * saved state which takes a quick dip into rawmode for instance.
2417 *
2418 * CPL/Stack; Note first check this one as the CPL might have changed.
2419 * The wrong CPL can cause QEmu to raise an exception in sync_seg!!
2420 */
2421 cpu_x86_set_cpl(&pVM->rem.s.Env, uCpl);
2422 /* Note! QEmu saves the 2nd dword of the descriptor; we should convert the attribute word back! */
2423#define SYNC_IN_SREG(a_pEnv, a_SReg, a_pRemSReg, a_pVBoxSReg) \
2424 do \
2425 { \
2426 if (CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, a_pVBoxSReg)) \
2427 { \
2428 cpu_x86_load_seg_cache(a_pEnv, R_##a_SReg, \
2429 (a_pVBoxSReg)->Sel, \
2430 (a_pVBoxSReg)->u64Base, \
2431 (a_pVBoxSReg)->u32Limit, \
2432 ((a_pVBoxSReg)->Attr.u & SEL_FLAGS_SMASK) << SEL_FLAGS_SHIFT); \
2433 (a_pRemSReg)->fVBoxFlags = (a_pVBoxSReg)->fFlags; \
2434 } \
2435 /* This only-reload-if-changed stuff is the old approach, we should ditch it. */ \
2436 else if ((a_pRemSReg)->selector != (a_pVBoxSReg)->Sel) \
2437 { \
2438 Log2(("REMR3State: " #a_SReg " changed from %04x to %04x!\n", \
2439 (a_pRemSReg)->selector, (a_pVBoxSReg)->Sel)); \
2440 sync_seg(a_pEnv, R_##a_SReg, (a_pVBoxSReg)->Sel); \
2441 if ((a_pRemSReg)->newselector) \
2442 STAM_COUNTER_INC(&gStatSelOutOfSync[R_##a_SReg]); \
2443 } \
2444 else \
2445 (a_pRemSReg)->newselector = 0; \
2446 } while (0)
2447
2448 SYNC_IN_SREG(&pVM->rem.s.Env, CS, &pVM->rem.s.Env.segs[R_CS], &pCtx->cs);
2449 SYNC_IN_SREG(&pVM->rem.s.Env, SS, &pVM->rem.s.Env.segs[R_SS], &pCtx->ss);
2450 SYNC_IN_SREG(&pVM->rem.s.Env, DS, &pVM->rem.s.Env.segs[R_DS], &pCtx->ds);
2451 SYNC_IN_SREG(&pVM->rem.s.Env, ES, &pVM->rem.s.Env.segs[R_ES], &pCtx->es);
2452 SYNC_IN_SREG(&pVM->rem.s.Env, FS, &pVM->rem.s.Env.segs[R_FS], &pCtx->fs);
2453 SYNC_IN_SREG(&pVM->rem.s.Env, GS, &pVM->rem.s.Env.segs[R_GS], &pCtx->gs);
2454 /** @todo need to find a way to communicate potential GDT/LDT changes and thread switches. The selector might
2455 * be the same but not the base/limit. */
2456
2457 /*
2458 * Check for traps.
2459 */
2460 pVM->rem.s.Env.exception_index = -1; /** @todo this won't work :/ */
2461 rc = TRPMQueryTrap(pVCpu, &u8TrapNo, &enmType);
2462 if (RT_SUCCESS(rc))
2463 {
2464#ifdef DEBUG
2465 if (u8TrapNo == 0x80)
2466 {
2467 remR3DumpLnxSyscall(pVCpu);
2468 remR3DumpOBsdSyscall(pVCpu);
2469 }
2470#endif
2471
2472 pVM->rem.s.Env.exception_index = u8TrapNo;
2473 if (enmType != TRPM_SOFTWARE_INT)
2474 {
2475 pVM->rem.s.Env.exception_is_int = enmType == TRPM_HARDWARE_INT
2476 ? EXCEPTION_IS_INT_VALUE_HARDWARE_IRQ : 0; /* HACK ALERT! */
2477 pVM->rem.s.Env.exception_next_eip = pVM->rem.s.Env.eip;
2478 }
2479 else
2480 {
2481 /*
2482 * The there are two 1 byte opcodes and one 2 byte opcode for software interrupts.
2483 * We ASSUME that there are no prefixes and sets the default to 2 byte, and checks
2484 * for int03 and into.
2485 */
2486 pVM->rem.s.Env.exception_is_int = 1;
2487 pVM->rem.s.Env.exception_next_eip = pCtx->rip + 2;
2488 /* int 3 may be generated by one-byte 0xcc */
2489 if (u8TrapNo == 3)
2490 {
2491 if (read_byte(&pVM->rem.s.Env, pVM->rem.s.Env.segs[R_CS].base + pCtx->rip) == 0xcc)
2492 pVM->rem.s.Env.exception_next_eip = pCtx->rip + 1;
2493 }
2494 /* int 4 may be generated by one-byte 0xce */
2495 else if (u8TrapNo == 4)
2496 {
2497 if (read_byte(&pVM->rem.s.Env, pVM->rem.s.Env.segs[R_CS].base + pCtx->rip) == 0xce)
2498 pVM->rem.s.Env.exception_next_eip = pCtx->rip + 1;
2499 }
2500 }
2501
2502 /* get error code and cr2 if needed. */
2503 if (enmType == TRPM_TRAP)
2504 {
2505 switch (u8TrapNo)
2506 {
2507 case X86_XCPT_PF:
2508 pVM->rem.s.Env.cr[2] = TRPMGetFaultAddress(pVCpu);
2509 /* fallthru */
2510 case X86_XCPT_TS: case X86_XCPT_NP: case X86_XCPT_SS: case X86_XCPT_GP:
2511 pVM->rem.s.Env.error_code = TRPMGetErrorCode(pVCpu);
2512 break;
2513
2514 case X86_XCPT_AC: case X86_XCPT_DF:
2515 default:
2516 pVM->rem.s.Env.error_code = 0;
2517 break;
2518 }
2519 }
2520 else
2521 pVM->rem.s.Env.error_code = 0;
2522
2523 /*
2524 * We can now reset the active trap since the recompiler is gonna have a go at it.
2525 */
2526 rc = TRPMResetTrap(pVCpu);
2527 AssertRC(rc);
2528 Log2(("REMR3State: trap=%02x errcd=%RGv cr2=%RGv nexteip=%RGv%s\n", pVM->rem.s.Env.exception_index, (RTGCPTR)pVM->rem.s.Env.error_code,
2529 (RTGCPTR)pVM->rem.s.Env.cr[2], (RTGCPTR)pVM->rem.s.Env.exception_next_eip, pVM->rem.s.Env.exception_is_int ? " software" : ""));
2530 }
2531
2532 /*
2533 * Clear old interrupt request flags; Check for pending hardware interrupts.
2534 * (See @remark for why we don't check for other FFs.)
2535 */
2536 pVM->rem.s.Env.interrupt_request &= ~(CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXITTB | CPU_INTERRUPT_TIMER);
2537#ifdef VBOX_WITH_NEW_APIC
2538 if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_UPDATE_APIC))
2539 APICUpdatePendingInterrupts(pVCpu);
2540#endif
2541 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC))
2542 pVM->rem.s.Env.interrupt_request |= CPU_INTERRUPT_HARD;
2543
2544 /*
2545 * We're now in REM mode.
2546 */
2547 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_REM);
2548 pVM->rem.s.fInREM = true;
2549 pVM->rem.s.fInStateSync = false;
2550 pVM->rem.s.cCanExecuteRaw = 0;
2551 STAM_PROFILE_STOP(&pVM->rem.s.StatsState, a);
2552 Log2(("REMR3State: returns VINF_SUCCESS\n"));
2553 return VINF_SUCCESS;
2554}
2555
2556
2557/**
2558 * Syncs back changes in the REM state to the the VM state.
2559 *
2560 * This must be called after invoking REMR3Run().
2561 * Calling it several times in a row is not permitted.
2562 *
2563 * @returns VBox status code.
2564 *
2565 * @param pVM VM Handle.
2566 * @param pVCpu VMCPU Handle.
2567 */
2568REMR3DECL(int) REMR3StateBack(PVM pVM, PVMCPU pVCpu)
2569{
2570 register PCPUMCTX pCtx = pVM->rem.s.pCtx;
2571 Assert(pCtx);
2572 unsigned i;
2573
2574 STAM_PROFILE_START(&pVM->rem.s.StatsStateBack, a);
2575 Log2(("REMR3StateBack:\n"));
2576 Assert(pVM->rem.s.fInREM);
2577
2578 /*
2579 * Copy back the registers.
2580 * This is done in the order they are declared in the CPUMCTX structure.
2581 */
2582
2583 /** @todo FOP */
2584 /** @todo FPUIP */
2585 /** @todo CS */
2586 /** @todo FPUDP */
2587 /** @todo DS */
2588
2589 /** @todo check if FPU/XMM was actually used in the recompiler */
2590 restore_raw_fp_state(&pVM->rem.s.Env, (uint8_t *)&pCtx->pXStateR3->x87);
2591//// dprintf2(("FPU state CW=%04X TT=%04X SW=%04X (%04X)\n", env->fpuc, env->fpstt, env->fpus, pVMCtx->fpu.FSW));
2592
2593#ifdef TARGET_X86_64
2594 /* Note that the high dwords of 64 bits registers are undefined in 32 bits mode and are undefined after a mode change. */
2595 pCtx->rdi = pVM->rem.s.Env.regs[R_EDI];
2596 pCtx->rsi = pVM->rem.s.Env.regs[R_ESI];
2597 pCtx->rbp = pVM->rem.s.Env.regs[R_EBP];
2598 pCtx->rax = pVM->rem.s.Env.regs[R_EAX];
2599 pCtx->rbx = pVM->rem.s.Env.regs[R_EBX];
2600 pCtx->rdx = pVM->rem.s.Env.regs[R_EDX];
2601 pCtx->rcx = pVM->rem.s.Env.regs[R_ECX];
2602 pCtx->r8 = pVM->rem.s.Env.regs[8];
2603 pCtx->r9 = pVM->rem.s.Env.regs[9];
2604 pCtx->r10 = pVM->rem.s.Env.regs[10];
2605 pCtx->r11 = pVM->rem.s.Env.regs[11];
2606 pCtx->r12 = pVM->rem.s.Env.regs[12];
2607 pCtx->r13 = pVM->rem.s.Env.regs[13];
2608 pCtx->r14 = pVM->rem.s.Env.regs[14];
2609 pCtx->r15 = pVM->rem.s.Env.regs[15];
2610
2611 pCtx->rsp = pVM->rem.s.Env.regs[R_ESP];
2612
2613#else
2614 pCtx->edi = pVM->rem.s.Env.regs[R_EDI];
2615 pCtx->esi = pVM->rem.s.Env.regs[R_ESI];
2616 pCtx->ebp = pVM->rem.s.Env.regs[R_EBP];
2617 pCtx->eax = pVM->rem.s.Env.regs[R_EAX];
2618 pCtx->ebx = pVM->rem.s.Env.regs[R_EBX];
2619 pCtx->edx = pVM->rem.s.Env.regs[R_EDX];
2620 pCtx->ecx = pVM->rem.s.Env.regs[R_ECX];
2621
2622 pCtx->esp = pVM->rem.s.Env.regs[R_ESP];
2623#endif
2624
2625#define SYNC_BACK_SREG(a_sreg, a_SREG) \
2626 do \
2627 { \
2628 pCtx->a_sreg.Sel = pVM->rem.s.Env.segs[R_##a_SREG].selector; \
2629 if (!pVM->rem.s.Env.segs[R_SS].newselector) \
2630 { \
2631 pCtx->a_sreg.ValidSel = pVM->rem.s.Env.segs[R_##a_SREG].selector; \
2632 pCtx->a_sreg.fFlags = CPUMSELREG_FLAGS_VALID; \
2633 pCtx->a_sreg.u64Base = pVM->rem.s.Env.segs[R_##a_SREG].base; \
2634 pCtx->a_sreg.u32Limit = pVM->rem.s.Env.segs[R_##a_SREG].limit; \
2635 /* Note! QEmu saves the 2nd dword of the descriptor; we (VT-x/AMD-V) keep only the attributes! */ \
2636 pCtx->a_sreg.Attr.u = (pVM->rem.s.Env.segs[R_##a_SREG].flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK; \
2637 } \
2638 else \
2639 { \
2640 pCtx->a_sreg.fFlags = 0; \
2641 STAM_COUNTER_INC(&gStatSelOutOfSyncStateBack[R_##a_SREG]); \
2642 } \
2643 } while (0)
2644
2645 SYNC_BACK_SREG(es, ES);
2646 SYNC_BACK_SREG(cs, CS);
2647 SYNC_BACK_SREG(ss, SS);
2648 SYNC_BACK_SREG(ds, DS);
2649 SYNC_BACK_SREG(fs, FS);
2650 SYNC_BACK_SREG(gs, GS);
2651
2652#ifdef TARGET_X86_64
2653 pCtx->rip = pVM->rem.s.Env.eip;
2654 pCtx->rflags.u64 = pVM->rem.s.Env.eflags;
2655#else
2656 pCtx->eip = pVM->rem.s.Env.eip;
2657 pCtx->eflags.u32 = pVM->rem.s.Env.eflags;
2658#endif
2659
2660 pCtx->cr0 = pVM->rem.s.Env.cr[0];
2661 pCtx->cr2 = pVM->rem.s.Env.cr[2];
2662 pCtx->cr3 = pVM->rem.s.Env.cr[3];
2663#ifdef VBOX_WITH_RAW_MODE
2664 if (((pVM->rem.s.Env.cr[4] ^ pCtx->cr4) & X86_CR4_VME) && !HMIsEnabled(pVM))
2665 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
2666#endif
2667 pCtx->cr4 = pVM->rem.s.Env.cr[4];
2668
2669 for (i = 0; i < 8; i++)
2670 pCtx->dr[i] = pVM->rem.s.Env.dr[i];
2671
2672 pCtx->gdtr.cbGdt = pVM->rem.s.Env.gdt.limit;
2673 if (pCtx->gdtr.pGdt != pVM->rem.s.Env.gdt.base)
2674 {
2675 pCtx->gdtr.pGdt = pVM->rem.s.Env.gdt.base;
2676 STAM_COUNTER_INC(&gStatREMGDTChange);
2677#ifdef VBOX_WITH_RAW_MODE
2678 if (!HMIsEnabled(pVM))
2679 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
2680#endif
2681 }
2682
2683 pCtx->idtr.cbIdt = pVM->rem.s.Env.idt.limit;
2684 if (pCtx->idtr.pIdt != pVM->rem.s.Env.idt.base)
2685 {
2686 pCtx->idtr.pIdt = pVM->rem.s.Env.idt.base;
2687 STAM_COUNTER_INC(&gStatREMIDTChange);
2688#ifdef VBOX_WITH_RAW_MODE
2689 if (!HMIsEnabled(pVM))
2690 VMCPU_FF_SET(pVCpu, VMCPU_FF_TRPM_SYNC_IDT);
2691#endif
2692 }
2693
2694 if ( pCtx->ldtr.Sel != pVM->rem.s.Env.ldt.selector
2695 || pCtx->ldtr.ValidSel != pVM->rem.s.Env.ldt.selector
2696 || pCtx->ldtr.u64Base != pVM->rem.s.Env.ldt.base
2697 || pCtx->ldtr.u32Limit != pVM->rem.s.Env.ldt.limit
2698 || pCtx->ldtr.Attr.u != ((pVM->rem.s.Env.ldt.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK)
2699 || !(pCtx->ldtr.fFlags & CPUMSELREG_FLAGS_VALID)
2700 )
2701 {
2702 pCtx->ldtr.Sel = pVM->rem.s.Env.ldt.selector;
2703 pCtx->ldtr.ValidSel = pVM->rem.s.Env.ldt.selector;
2704 pCtx->ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
2705 pCtx->ldtr.u64Base = pVM->rem.s.Env.ldt.base;
2706 pCtx->ldtr.u32Limit = pVM->rem.s.Env.ldt.limit;
2707 pCtx->ldtr.Attr.u = (pVM->rem.s.Env.ldt.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
2708 STAM_COUNTER_INC(&gStatREMLDTRChange);
2709#ifdef VBOX_WITH_RAW_MODE
2710 if (!HMIsEnabled(pVM))
2711 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
2712#endif
2713 }
2714
2715 if ( pCtx->tr.Sel != pVM->rem.s.Env.tr.selector
2716 || pCtx->tr.ValidSel != pVM->rem.s.Env.tr.selector
2717 || pCtx->tr.u64Base != pVM->rem.s.Env.tr.base
2718 || pCtx->tr.u32Limit != pVM->rem.s.Env.tr.limit
2719 /* Qemu and AMD/Intel have different ideas about the busy flag ... */ /** @todo just fix qemu! */
2720 || pCtx->tr.Attr.u != ( (pVM->rem.s.Env.tr.flags >> SEL_FLAGS_SHIFT) & (SEL_FLAGS_SMASK & ~DESC_INTEL_UNUSABLE)
2721 ? (pVM->rem.s.Env.tr.flags | DESC_TSS_BUSY_MASK) >> SEL_FLAGS_SHIFT
2722 : 0)
2723 || !(pCtx->tr.fFlags & CPUMSELREG_FLAGS_VALID)
2724 )
2725 {
2726 Log(("REM: TR changed! %#x{%#llx,%#x,%#x} -> %#x{%llx,%#x,%#x}\n",
2727 pCtx->tr.Sel, pCtx->tr.u64Base, pCtx->tr.u32Limit, pCtx->tr.Attr.u,
2728 pVM->rem.s.Env.tr.selector, (uint64_t)pVM->rem.s.Env.tr.base, pVM->rem.s.Env.tr.limit,
2729 (pVM->rem.s.Env.tr.flags >> SEL_FLAGS_SHIFT) & (SEL_FLAGS_SMASK & ~DESC_INTEL_UNUSABLE)
2730 ? (pVM->rem.s.Env.tr.flags | DESC_TSS_BUSY_MASK) >> SEL_FLAGS_SHIFT : 0));
2731 pCtx->tr.Sel = pVM->rem.s.Env.tr.selector;
2732 pCtx->tr.ValidSel = pVM->rem.s.Env.tr.selector;
2733 pCtx->tr.fFlags = CPUMSELREG_FLAGS_VALID;
2734 pCtx->tr.u64Base = pVM->rem.s.Env.tr.base;
2735 pCtx->tr.u32Limit = pVM->rem.s.Env.tr.limit;
2736 pCtx->tr.Attr.u = (pVM->rem.s.Env.tr.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
2737 if (pCtx->tr.Attr.u & ~DESC_INTEL_UNUSABLE)
2738 pCtx->tr.Attr.u |= DESC_TSS_BUSY_MASK >> SEL_FLAGS_SHIFT;
2739 STAM_COUNTER_INC(&gStatREMTRChange);
2740#ifdef VBOX_WITH_RAW_MODE
2741 if (!HMIsEnabled(pVM))
2742 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
2743#endif
2744 }
2745
2746 /* Sysenter MSR */
2747 pCtx->SysEnter.cs = pVM->rem.s.Env.sysenter_cs;
2748 pCtx->SysEnter.eip = pVM->rem.s.Env.sysenter_eip;
2749 pCtx->SysEnter.esp = pVM->rem.s.Env.sysenter_esp;
2750
2751 /* System MSRs. */
2752 pCtx->msrEFER = pVM->rem.s.Env.efer;
2753 pCtx->msrSTAR = pVM->rem.s.Env.star;
2754 pCtx->msrPAT = pVM->rem.s.Env.pat;
2755#ifdef TARGET_X86_64
2756 pCtx->msrLSTAR = pVM->rem.s.Env.lstar;
2757 pCtx->msrCSTAR = pVM->rem.s.Env.cstar;
2758 pCtx->msrSFMASK = pVM->rem.s.Env.fmask;
2759 pCtx->msrKERNELGSBASE = pVM->rem.s.Env.kernelgsbase;
2760#endif
2761
2762 /* Inhibit interrupt flag. */
2763 if (pVM->rem.s.Env.hflags & HF_INHIBIT_IRQ_MASK)
2764 {
2765 Log(("Settings VMCPU_FF_INHIBIT_INTERRUPTS at %RGv (REM)\n", (RTGCPTR)pCtx->rip));
2766 EMSetInhibitInterruptsPC(pVCpu, pCtx->rip);
2767 VMCPU_FF_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2768 }
2769 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
2770 {
2771 Log(("Clearing VMCPU_FF_INHIBIT_INTERRUPTS at %RGv - successor %RGv (REM#2)\n", (RTGCPTR)pCtx->rip, EMGetInhibitInterruptsPC(pVCpu)));
2772 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2773 }
2774
2775 /* Inhibit NMI flag. */
2776 if (pVM->rem.s.Env.hflags2 & HF2_NMI_MASK)
2777 {
2778 Log(("Settings VMCPU_FF_BLOCK_NMIS at %RGv (REM)\n", (RTGCPTR)pCtx->rip));
2779 VMCPU_FF_SET(pVCpu, VMCPU_FF_BLOCK_NMIS);
2780 }
2781 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS))
2782 {
2783 Log(("Clearing VMCPU_FF_BLOCK_NMIS at %RGv (REM)\n", (RTGCPTR)pCtx->rip));
2784 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
2785 }
2786
2787 remR3TrapClear(pVM);
2788
2789 /*
2790 * Check for traps.
2791 */
2792 if ( pVM->rem.s.Env.exception_index >= 0
2793 && pVM->rem.s.Env.exception_index < 256)
2794 {
2795 /* This cannot be a hardware-interrupt because exception_index < EXCP_INTERRUPT. */
2796 int rc;
2797
2798 Log(("REMR3StateBack: Pending trap %x %d\n", pVM->rem.s.Env.exception_index, pVM->rem.s.Env.exception_is_int));
2799 TRPMEVENT enmType = pVM->rem.s.Env.exception_is_int == 0 ? TRPM_TRAP
2800 : pVM->rem.s.Env.exception_is_int == EXCEPTION_IS_INT_VALUE_HARDWARE_IRQ ? TRPM_HARDWARE_INT
2801 : TRPM_SOFTWARE_INT;
2802 rc = TRPMAssertTrap(pVCpu, pVM->rem.s.Env.exception_index, enmType);
2803 AssertRC(rc);
2804 if (enmType == TRPM_TRAP)
2805 {
2806 switch (pVM->rem.s.Env.exception_index)
2807 {
2808 case X86_XCPT_PF:
2809 TRPMSetFaultAddress(pVCpu, pCtx->cr2);
2810 /* fallthru */
2811 case X86_XCPT_TS: case X86_XCPT_NP: case X86_XCPT_SS: case X86_XCPT_GP:
2812 case X86_XCPT_AC: case X86_XCPT_DF: /* 0 */
2813 TRPMSetErrorCode(pVCpu, pVM->rem.s.Env.error_code);
2814 break;
2815 }
2816 }
2817 }
2818
2819 /*
2820 * We're not longer in REM mode.
2821 */
2822 CPUMR3RemLeave(pVCpu,
2823 HMIsEnabled(pVM)
2824 || ( pVM->rem.s.Env.segs[R_SS].newselector
2825 | pVM->rem.s.Env.segs[R_GS].newselector
2826 | pVM->rem.s.Env.segs[R_FS].newselector
2827 | pVM->rem.s.Env.segs[R_ES].newselector
2828 | pVM->rem.s.Env.segs[R_DS].newselector
2829 | pVM->rem.s.Env.segs[R_CS].newselector) == 0
2830 );
2831 VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_REM);
2832 pVM->rem.s.fInREM = false;
2833 pVM->rem.s.pCtx = NULL;
2834 pVM->rem.s.Env.pVCpu = NULL;
2835 STAM_PROFILE_STOP(&pVM->rem.s.StatsStateBack, a);
2836 Log2(("REMR3StateBack: returns VINF_SUCCESS\n"));
2837 return VINF_SUCCESS;
2838}
2839
2840
2841/**
2842 * This is called by the disassembler when it wants to update the cpu state
2843 * before for instance doing a register dump.
2844 */
2845static void remR3StateUpdate(PVM pVM, PVMCPU pVCpu)
2846{
2847 register PCPUMCTX pCtx = pVM->rem.s.pCtx;
2848 unsigned i;
2849
2850 Assert(pVM->rem.s.fInREM);
2851
2852 /*
2853 * Copy back the registers.
2854 * This is done in the order they are declared in the CPUMCTX structure.
2855 */
2856
2857 PX86FXSTATE pFpuCtx = &pCtx->pXStateR3->x87;
2858 /** @todo FOP */
2859 /** @todo FPUIP */
2860 /** @todo CS */
2861 /** @todo FPUDP */
2862 /** @todo DS */
2863 /** @todo Fix MXCSR support in QEMU so we don't overwrite MXCSR with 0 when we shouldn't! */
2864 pFpuCtx->MXCSR = 0;
2865 pFpuCtx->MXCSR_MASK = 0;
2866
2867 /** @todo check if FPU/XMM was actually used in the recompiler */
2868 restore_raw_fp_state(&pVM->rem.s.Env, (uint8_t *)pFpuCtx);
2869//// dprintf2(("FPU state CW=%04X TT=%04X SW=%04X (%04X)\n", env->fpuc, env->fpstt, env->fpus, pVMCtx->fpu.FSW));
2870
2871#ifdef TARGET_X86_64
2872 pCtx->rdi = pVM->rem.s.Env.regs[R_EDI];
2873 pCtx->rsi = pVM->rem.s.Env.regs[R_ESI];
2874 pCtx->rbp = pVM->rem.s.Env.regs[R_EBP];
2875 pCtx->rax = pVM->rem.s.Env.regs[R_EAX];
2876 pCtx->rbx = pVM->rem.s.Env.regs[R_EBX];
2877 pCtx->rdx = pVM->rem.s.Env.regs[R_EDX];
2878 pCtx->rcx = pVM->rem.s.Env.regs[R_ECX];
2879 pCtx->r8 = pVM->rem.s.Env.regs[8];
2880 pCtx->r9 = pVM->rem.s.Env.regs[9];
2881 pCtx->r10 = pVM->rem.s.Env.regs[10];
2882 pCtx->r11 = pVM->rem.s.Env.regs[11];
2883 pCtx->r12 = pVM->rem.s.Env.regs[12];
2884 pCtx->r13 = pVM->rem.s.Env.regs[13];
2885 pCtx->r14 = pVM->rem.s.Env.regs[14];
2886 pCtx->r15 = pVM->rem.s.Env.regs[15];
2887
2888 pCtx->rsp = pVM->rem.s.Env.regs[R_ESP];
2889#else
2890 pCtx->edi = pVM->rem.s.Env.regs[R_EDI];
2891 pCtx->esi = pVM->rem.s.Env.regs[R_ESI];
2892 pCtx->ebp = pVM->rem.s.Env.regs[R_EBP];
2893 pCtx->eax = pVM->rem.s.Env.regs[R_EAX];
2894 pCtx->ebx = pVM->rem.s.Env.regs[R_EBX];
2895 pCtx->edx = pVM->rem.s.Env.regs[R_EDX];
2896 pCtx->ecx = pVM->rem.s.Env.regs[R_ECX];
2897
2898 pCtx->esp = pVM->rem.s.Env.regs[R_ESP];
2899#endif
2900
2901 SYNC_BACK_SREG(es, ES);
2902 SYNC_BACK_SREG(cs, CS);
2903 SYNC_BACK_SREG(ss, SS);
2904 SYNC_BACK_SREG(ds, DS);
2905 SYNC_BACK_SREG(fs, FS);
2906 SYNC_BACK_SREG(gs, GS);
2907
2908#ifdef TARGET_X86_64
2909 pCtx->rip = pVM->rem.s.Env.eip;
2910 pCtx->rflags.u64 = pVM->rem.s.Env.eflags;
2911#else
2912 pCtx->eip = pVM->rem.s.Env.eip;
2913 pCtx->eflags.u32 = pVM->rem.s.Env.eflags;
2914#endif
2915
2916 pCtx->cr0 = pVM->rem.s.Env.cr[0];
2917 pCtx->cr2 = pVM->rem.s.Env.cr[2];
2918 pCtx->cr3 = pVM->rem.s.Env.cr[3];
2919#ifdef VBOX_WITH_RAW_MODE
2920 if (((pVM->rem.s.Env.cr[4] ^ pCtx->cr4) & X86_CR4_VME) && !HMIsEnabled(pVM))
2921 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
2922#endif
2923 pCtx->cr4 = pVM->rem.s.Env.cr[4];
2924
2925 for (i = 0; i < 8; i++)
2926 pCtx->dr[i] = pVM->rem.s.Env.dr[i];
2927
2928 pCtx->gdtr.cbGdt = pVM->rem.s.Env.gdt.limit;
2929 if (pCtx->gdtr.pGdt != (RTGCPTR)pVM->rem.s.Env.gdt.base)
2930 {
2931 pCtx->gdtr.pGdt = (RTGCPTR)pVM->rem.s.Env.gdt.base;
2932 STAM_COUNTER_INC(&gStatREMGDTChange);
2933#ifdef VBOX_WITH_RAW_MODE
2934 if (!HMIsEnabled(pVM))
2935 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_GDT);
2936#endif
2937 }
2938
2939 pCtx->idtr.cbIdt = pVM->rem.s.Env.idt.limit;
2940 if (pCtx->idtr.pIdt != (RTGCPTR)pVM->rem.s.Env.idt.base)
2941 {
2942 pCtx->idtr.pIdt = (RTGCPTR)pVM->rem.s.Env.idt.base;
2943 STAM_COUNTER_INC(&gStatREMIDTChange);
2944#ifdef VBOX_WITH_RAW_MODE
2945 if (!HMIsEnabled(pVM))
2946 VMCPU_FF_SET(pVCpu, VMCPU_FF_TRPM_SYNC_IDT);
2947#endif
2948 }
2949
2950 if ( pCtx->ldtr.Sel != pVM->rem.s.Env.ldt.selector
2951 || pCtx->ldtr.ValidSel != pVM->rem.s.Env.ldt.selector
2952 || pCtx->ldtr.u64Base != pVM->rem.s.Env.ldt.base
2953 || pCtx->ldtr.u32Limit != pVM->rem.s.Env.ldt.limit
2954 || pCtx->ldtr.Attr.u != ((pVM->rem.s.Env.ldt.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK)
2955 || !(pCtx->ldtr.fFlags & CPUMSELREG_FLAGS_VALID)
2956 )
2957 {
2958 pCtx->ldtr.Sel = pVM->rem.s.Env.ldt.selector;
2959 pCtx->ldtr.ValidSel = pVM->rem.s.Env.ldt.selector;
2960 pCtx->ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
2961 pCtx->ldtr.u64Base = pVM->rem.s.Env.ldt.base;
2962 pCtx->ldtr.u32Limit = pVM->rem.s.Env.ldt.limit;
2963 pCtx->ldtr.Attr.u = (pVM->rem.s.Env.ldt.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
2964 STAM_COUNTER_INC(&gStatREMLDTRChange);
2965#ifdef VBOX_WITH_RAW_MODE
2966 if (!HMIsEnabled(pVM))
2967 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_LDT);
2968#endif
2969 }
2970
2971 if ( pCtx->tr.Sel != pVM->rem.s.Env.tr.selector
2972 || pCtx->tr.ValidSel != pVM->rem.s.Env.tr.selector
2973 || pCtx->tr.u64Base != pVM->rem.s.Env.tr.base
2974 || pCtx->tr.u32Limit != pVM->rem.s.Env.tr.limit
2975 /* Qemu and AMD/Intel have different ideas about the busy flag ... */
2976 || pCtx->tr.Attr.u != ( (pVM->rem.s.Env.tr.flags >> SEL_FLAGS_SHIFT) & (SEL_FLAGS_SMASK & ~DESC_INTEL_UNUSABLE)
2977 ? (pVM->rem.s.Env.tr.flags | DESC_TSS_BUSY_MASK) >> SEL_FLAGS_SHIFT
2978 : 0)
2979 || !(pCtx->tr.fFlags & CPUMSELREG_FLAGS_VALID)
2980 )
2981 {
2982 Log(("REM: TR changed! %#x{%#llx,%#x,%#x} -> %#x{%llx,%#x,%#x}\n",
2983 pCtx->tr.Sel, pCtx->tr.u64Base, pCtx->tr.u32Limit, pCtx->tr.Attr.u,
2984 pVM->rem.s.Env.tr.selector, (uint64_t)pVM->rem.s.Env.tr.base, pVM->rem.s.Env.tr.limit,
2985 (pVM->rem.s.Env.tr.flags >> SEL_FLAGS_SHIFT) & (SEL_FLAGS_SMASK & ~DESC_INTEL_UNUSABLE)
2986 ? (pVM->rem.s.Env.tr.flags | DESC_TSS_BUSY_MASK) >> SEL_FLAGS_SHIFT : 0));
2987 pCtx->tr.Sel = pVM->rem.s.Env.tr.selector;
2988 pCtx->tr.ValidSel = pVM->rem.s.Env.tr.selector;
2989 pCtx->tr.fFlags = CPUMSELREG_FLAGS_VALID;
2990 pCtx->tr.u64Base = pVM->rem.s.Env.tr.base;
2991 pCtx->tr.u32Limit = pVM->rem.s.Env.tr.limit;
2992 pCtx->tr.Attr.u = (pVM->rem.s.Env.tr.flags >> SEL_FLAGS_SHIFT) & SEL_FLAGS_SMASK;
2993 if (pCtx->tr.Attr.u & ~DESC_INTEL_UNUSABLE)
2994 pCtx->tr.Attr.u |= DESC_TSS_BUSY_MASK >> SEL_FLAGS_SHIFT;
2995 STAM_COUNTER_INC(&gStatREMTRChange);
2996#ifdef VBOX_WITH_RAW_MODE
2997 if (!HMIsEnabled(pVM))
2998 VMCPU_FF_SET(pVCpu, VMCPU_FF_SELM_SYNC_TSS);
2999#endif
3000 }
3001
3002 /* Sysenter MSR */
3003 pCtx->SysEnter.cs = pVM->rem.s.Env.sysenter_cs;
3004 pCtx->SysEnter.eip = pVM->rem.s.Env.sysenter_eip;
3005 pCtx->SysEnter.esp = pVM->rem.s.Env.sysenter_esp;
3006
3007 /* System MSRs. */
3008 pCtx->msrEFER = pVM->rem.s.Env.efer;
3009 pCtx->msrSTAR = pVM->rem.s.Env.star;
3010 pCtx->msrPAT = pVM->rem.s.Env.pat;
3011#ifdef TARGET_X86_64
3012 pCtx->msrLSTAR = pVM->rem.s.Env.lstar;
3013 pCtx->msrCSTAR = pVM->rem.s.Env.cstar;
3014 pCtx->msrSFMASK = pVM->rem.s.Env.fmask;
3015 pCtx->msrKERNELGSBASE = pVM->rem.s.Env.kernelgsbase;
3016#endif
3017
3018}
3019
3020
3021/**
3022 * Update the VMM state information if we're currently in REM.
3023 *
3024 * This method is used by the DBGF and PDMDevice when there is any uncertainty of whether
3025 * we're currently executing in REM and the VMM state is invalid. This method will of
3026 * course check that we're executing in REM before syncing any data over to the VMM.
3027 *
3028 * @param pVM The VM handle.
3029 * @param pVCpu The VMCPU handle.
3030 */
3031REMR3DECL(void) REMR3StateUpdate(PVM pVM, PVMCPU pVCpu)
3032{
3033 if (pVM->rem.s.fInREM)
3034 remR3StateUpdate(pVM, pVCpu);
3035}
3036
3037
3038#undef LOG_GROUP
3039#define LOG_GROUP LOG_GROUP_REM
3040
3041
3042/**
3043 * Notify the recompiler about Address Gate 20 state change.
3044 *
3045 * This notification is required since A20 gate changes are
3046 * initialized from a device driver and the VM might just as
3047 * well be in REM mode as in RAW mode.
3048 *
3049 * @param pVM VM handle.
3050 * @param pVCpu VMCPU handle.
3051 * @param fEnable True if the gate should be enabled.
3052 * False if the gate should be disabled.
3053 */
3054REMR3DECL(void) REMR3A20Set(PVM pVM, PVMCPU pVCpu, bool fEnable)
3055{
3056 LogFlow(("REMR3A20Set: fEnable=%d\n", fEnable));
3057 VM_ASSERT_EMT(pVM);
3058
3059 /** @todo SMP and the A20 gate... */
3060 if (pVM->rem.s.Env.pVCpu == pVCpu)
3061 {
3062 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
3063 cpu_x86_set_a20(&pVM->rem.s.Env, fEnable);
3064 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
3065 }
3066}
3067
3068
3069/**
3070 * Replays the handler notification changes
3071 * Called in response to VM_FF_REM_HANDLER_NOTIFY from the RAW execution loop.
3072 *
3073 * @param pVM VM handle.
3074 */
3075REMR3DECL(void) REMR3ReplayHandlerNotifications(PVM pVM)
3076{
3077 /*
3078 * Replay the flushes.
3079 */
3080 LogFlow(("REMR3ReplayHandlerNotifications:\n"));
3081 VM_ASSERT_EMT(pVM);
3082
3083 /** @todo this isn't ensuring correct replay order. */
3084 if (VM_FF_TEST_AND_CLEAR(pVM, VM_FF_REM_HANDLER_NOTIFY))
3085 {
3086 uint32_t idxNext;
3087 uint32_t idxRevHead;
3088 uint32_t idxHead;
3089#ifdef VBOX_STRICT
3090 int32_t c = 0;
3091#endif
3092
3093 /* Lockless purging of pending notifications. */
3094 idxHead = ASMAtomicXchgU32(&pVM->rem.s.idxPendingList, UINT32_MAX);
3095 if (idxHead == UINT32_MAX)
3096 return;
3097 Assert(idxHead < RT_ELEMENTS(pVM->rem.s.aHandlerNotifications));
3098
3099 /*
3100 * Reverse the list to process it in FIFO order.
3101 */
3102 idxRevHead = UINT32_MAX;
3103 do
3104 {
3105 /* Save the index of the next rec. */
3106 idxNext = pVM->rem.s.aHandlerNotifications[idxHead].idxNext;
3107 Assert(idxNext < RT_ELEMENTS(pVM->rem.s.aHandlerNotifications) || idxNext == UINT32_MAX);
3108 /* Push the record onto the reversed list. */
3109 pVM->rem.s.aHandlerNotifications[idxHead].idxNext = idxRevHead;
3110 idxRevHead = idxHead;
3111 Assert(++c <= RT_ELEMENTS(pVM->rem.s.aHandlerNotifications));
3112 /* Advance. */
3113 idxHead = idxNext;
3114 } while (idxHead != UINT32_MAX);
3115
3116 /*
3117 * Loop thru the list, reinserting the record into the free list as they are
3118 * processed to avoid having other EMTs running out of entries while we're flushing.
3119 */
3120 idxHead = idxRevHead;
3121 do
3122 {
3123 PREMHANDLERNOTIFICATION pCur = &pVM->rem.s.aHandlerNotifications[idxHead];
3124 uint32_t idxCur;
3125 Assert(--c >= 0);
3126
3127 switch (pCur->enmKind)
3128 {
3129 case REMHANDLERNOTIFICATIONKIND_PHYSICAL_REGISTER:
3130 remR3NotifyHandlerPhysicalRegister(pVM,
3131 pCur->u.PhysicalRegister.enmKind,
3132 pCur->u.PhysicalRegister.GCPhys,
3133 pCur->u.PhysicalRegister.cb,
3134 pCur->u.PhysicalRegister.fHasHCHandler);
3135 break;
3136
3137 case REMHANDLERNOTIFICATIONKIND_PHYSICAL_DEREGISTER:
3138 remR3NotifyHandlerPhysicalDeregister(pVM,
3139 pCur->u.PhysicalDeregister.enmKind,
3140 pCur->u.PhysicalDeregister.GCPhys,
3141 pCur->u.PhysicalDeregister.cb,
3142 pCur->u.PhysicalDeregister.fHasHCHandler,
3143 pCur->u.PhysicalDeregister.fRestoreAsRAM);
3144 break;
3145
3146 case REMHANDLERNOTIFICATIONKIND_PHYSICAL_MODIFY:
3147 remR3NotifyHandlerPhysicalModify(pVM,
3148 pCur->u.PhysicalModify.enmKind,
3149 pCur->u.PhysicalModify.GCPhysOld,
3150 pCur->u.PhysicalModify.GCPhysNew,
3151 pCur->u.PhysicalModify.cb,
3152 pCur->u.PhysicalModify.fHasHCHandler,
3153 pCur->u.PhysicalModify.fRestoreAsRAM);
3154 break;
3155
3156 default:
3157 AssertReleaseMsgFailed(("enmKind=%d\n", pCur->enmKind));
3158 break;
3159 }
3160
3161 /*
3162 * Advance idxHead.
3163 */
3164 idxCur = idxHead;
3165 idxHead = pCur->idxNext;
3166 Assert(idxHead < RT_ELEMENTS(pVM->rem.s.aHandlerNotifications) || (idxHead == UINT32_MAX && c == 0));
3167
3168 /*
3169 * Put the record back into the free list.
3170 */
3171 do
3172 {
3173 idxNext = ASMAtomicUoReadU32(&pVM->rem.s.idxFreeList);
3174 ASMAtomicWriteU32(&pCur->idxNext, idxNext);
3175 ASMCompilerBarrier();
3176 } while (!ASMAtomicCmpXchgU32(&pVM->rem.s.idxFreeList, idxCur, idxNext));
3177 } while (idxHead != UINT32_MAX);
3178
3179#ifdef VBOX_STRICT
3180 if (pVM->cCpus == 1)
3181 {
3182 unsigned c;
3183 /* Check that all records are now on the free list. */
3184 for (c = 0, idxNext = pVM->rem.s.idxFreeList; idxNext != UINT32_MAX;
3185 idxNext = pVM->rem.s.aHandlerNotifications[idxNext].idxNext)
3186 c++;
3187 AssertReleaseMsg(c == RT_ELEMENTS(pVM->rem.s.aHandlerNotifications), ("%#x != %#x, idxFreeList=%#x\n", c, RT_ELEMENTS(pVM->rem.s.aHandlerNotifications), pVM->rem.s.idxFreeList));
3188 }
3189#endif
3190 }
3191}
3192
3193
3194/**
3195 * Notify REM about changed code page.
3196 *
3197 * @returns VBox status code.
3198 * @param pVM VM handle.
3199 * @param pVCpu VMCPU handle.
3200 * @param pvCodePage Code page address
3201 */
3202REMR3DECL(int) REMR3NotifyCodePageChanged(PVM pVM, PVMCPU pVCpu, RTGCPTR pvCodePage)
3203{
3204#ifdef VBOX_REM_PROTECT_PAGES_FROM_SMC
3205 int rc;
3206 RTGCPHYS PhysGC;
3207 uint64_t flags;
3208
3209 VM_ASSERT_EMT(pVM);
3210
3211 /*
3212 * Get the physical page address.
3213 */
3214 rc = PGMGstGetPage(pVM, pvCodePage, &flags, &PhysGC);
3215 if (rc == VINF_SUCCESS)
3216 {
3217 /*
3218 * Sync the required registers and flush the whole page.
3219 * (Easier to do the whole page than notifying it about each physical
3220 * byte that was changed.
3221 */
3222 pVM->rem.s.Env.cr[0] = pVM->rem.s.pCtx->cr0;
3223 pVM->rem.s.Env.cr[2] = pVM->rem.s.pCtx->cr2;
3224 pVM->rem.s.Env.cr[3] = pVM->rem.s.pCtx->cr3;
3225 pVM->rem.s.Env.cr[4] = pVM->rem.s.pCtx->cr4;
3226
3227 tb_invalidate_phys_page_range(PhysGC, PhysGC + PAGE_SIZE - 1, 0);
3228 }
3229#endif
3230 return VINF_SUCCESS;
3231}
3232
3233
3234/**
3235 * Notification about a successful MMR3PhysRegister() call.
3236 *
3237 * @param pVM VM handle.
3238 * @param GCPhys The physical address the RAM.
3239 * @param cb Size of the memory.
3240 * @param fFlags Flags of the REM_NOTIFY_PHYS_RAM_FLAGS_* defines.
3241 */
3242REMR3DECL(void) REMR3NotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, unsigned fFlags)
3243{
3244 Log(("REMR3NotifyPhysRamRegister: GCPhys=%RGp cb=%RGp fFlags=%#x\n", GCPhys, cb, fFlags));
3245 VM_ASSERT_EMT(pVM);
3246
3247 /*
3248 * Validate input - we trust the caller.
3249 */
3250 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
3251 Assert(cb);
3252 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
3253 AssertMsg(fFlags == REM_NOTIFY_PHYS_RAM_FLAGS_RAM || fFlags == REM_NOTIFY_PHYS_RAM_FLAGS_MMIO2, ("%#x\n", fFlags));
3254
3255 /*
3256 * Base ram? Update GCPhysLastRam.
3257 */
3258 if (fFlags & REM_NOTIFY_PHYS_RAM_FLAGS_RAM)
3259 {
3260 if (GCPhys + (cb - 1) > pVM->rem.s.GCPhysLastRam)
3261 {
3262 AssertReleaseMsg(!pVM->rem.s.fGCPhysLastRamFixed, ("GCPhys=%RGp cb=%RGp\n", GCPhys, cb));
3263 pVM->rem.s.GCPhysLastRam = GCPhys + (cb - 1);
3264 }
3265 }
3266
3267 /*
3268 * Register the ram.
3269 */
3270 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
3271
3272 PDMCritSectEnter(&pVM->rem.s.CritSectRegister, VERR_SEM_BUSY);
3273 cpu_register_physical_memory_offset(GCPhys, cb, GCPhys, GCPhys);
3274 PDMCritSectLeave(&pVM->rem.s.CritSectRegister);
3275
3276 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
3277}
3278
3279
3280/**
3281 * Notification about a successful MMR3PhysRomRegister() call.
3282 *
3283 * @param pVM VM handle.
3284 * @param GCPhys The physical address of the ROM.
3285 * @param cb The size of the ROM.
3286 * @param pvCopy Pointer to the ROM copy.
3287 * @param fShadow Whether it's currently writable shadow ROM or normal readonly ROM.
3288 * This function will be called when ever the protection of the
3289 * shadow ROM changes (at reset and end of POST).
3290 */
3291REMR3DECL(void) REMR3NotifyPhysRomRegister(PVM pVM, RTGCPHYS GCPhys, RTUINT cb, void *pvCopy, bool fShadow)
3292{
3293 Log(("REMR3NotifyPhysRomRegister: GCPhys=%RGp cb=%d fShadow=%RTbool\n", GCPhys, cb, fShadow));
3294 VM_ASSERT_EMT(pVM);
3295
3296 /*
3297 * Validate input - we trust the caller.
3298 */
3299 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
3300 Assert(cb);
3301 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
3302
3303 /*
3304 * Register the rom.
3305 */
3306 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
3307
3308 PDMCritSectEnter(&pVM->rem.s.CritSectRegister, VERR_SEM_BUSY);
3309 cpu_register_physical_memory_offset(GCPhys, cb, GCPhys | (fShadow ? 0 : IO_MEM_ROM), GCPhys);
3310 PDMCritSectLeave(&pVM->rem.s.CritSectRegister);
3311
3312 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
3313}
3314
3315
3316/**
3317 * Notification about a successful memory deregistration or reservation.
3318 *
3319 * @param pVM VM Handle.
3320 * @param GCPhys Start physical address.
3321 * @param cb The size of the range.
3322 */
3323REMR3DECL(void) REMR3NotifyPhysRamDeregister(PVM pVM, RTGCPHYS GCPhys, RTUINT cb)
3324{
3325 Log(("REMR3NotifyPhysRamDeregister: GCPhys=%RGp cb=%d\n", GCPhys, cb));
3326 VM_ASSERT_EMT(pVM);
3327
3328 /*
3329 * Validate input - we trust the caller.
3330 */
3331 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
3332 Assert(cb);
3333 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
3334
3335 /*
3336 * Unassigning the memory.
3337 */
3338 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
3339
3340 PDMCritSectEnter(&pVM->rem.s.CritSectRegister, VERR_SEM_BUSY);
3341 cpu_register_physical_memory_offset(GCPhys, cb, IO_MEM_UNASSIGNED, GCPhys);
3342 PDMCritSectLeave(&pVM->rem.s.CritSectRegister);
3343
3344 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
3345}
3346
3347
3348/**
3349 * Notification about a successful PGMR3HandlerPhysicalRegister() call.
3350 *
3351 * @param pVM VM Handle.
3352 * @param enmKind Kind of access handler.
3353 * @param GCPhys Handler range address.
3354 * @param cb Size of the handler range.
3355 * @param fHasHCHandler Set if the handler has a HC callback function.
3356 *
3357 * @remark MMR3PhysRomRegister assumes that this function will not apply the
3358 * Handler memory type to memory which has no HC handler.
3359 */
3360static void remR3NotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
3361 bool fHasHCHandler)
3362{
3363 Log(("REMR3NotifyHandlerPhysicalRegister: enmKind=%d GCPhys=%RGp cb=%RGp fHasHCHandler=%d\n",
3364 enmKind, GCPhys, cb, fHasHCHandler));
3365
3366 VM_ASSERT_EMT(pVM);
3367 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
3368 Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
3369
3370
3371 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
3372
3373 PDMCritSectEnter(&pVM->rem.s.CritSectRegister, VERR_SEM_BUSY);
3374 if (enmKind == PGMPHYSHANDLERKIND_MMIO)
3375 cpu_register_physical_memory_offset(GCPhys, cb, pVM->rem.s.iMMIOMemType, GCPhys);
3376 else if (fHasHCHandler)
3377 cpu_register_physical_memory_offset(GCPhys, cb, pVM->rem.s.iHandlerMemType, GCPhys);
3378 PDMCritSectLeave(&pVM->rem.s.CritSectRegister);
3379
3380 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
3381}
3382
3383/**
3384 * Notification about a successful PGMR3HandlerPhysicalRegister() call.
3385 *
3386 * @param pVM VM Handle.
3387 * @param enmKind Kind of access handler.
3388 * @param GCPhys Handler range address.
3389 * @param cb Size of the handler range.
3390 * @param fHasHCHandler Set if the handler has a HC callback function.
3391 *
3392 * @remark MMR3PhysRomRegister assumes that this function will not apply the
3393 * Handler memory type to memory which has no HC handler.
3394 */
3395REMR3DECL(void) REMR3NotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
3396 bool fHasHCHandler)
3397{
3398 REMR3ReplayHandlerNotifications(pVM);
3399
3400 remR3NotifyHandlerPhysicalRegister(pVM, enmKind, GCPhys, cb, fHasHCHandler);
3401}
3402
3403/**
3404 * Notification about a successful PGMR3HandlerPhysicalDeregister() operation.
3405 *
3406 * @param pVM VM Handle.
3407 * @param enmKind Kind of access handler.
3408 * @param GCPhys Handler range address.
3409 * @param cb Size of the handler range.
3410 * @param fHasHCHandler Set if the handler has a HC callback function.
3411 * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
3412 */
3413static void remR3NotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
3414 bool fHasHCHandler, bool fRestoreAsRAM)
3415{
3416 Log(("REMR3NotifyHandlerPhysicalDeregister: enmKind=%d GCPhys=%RGp cb=%RGp fHasHCHandler=%RTbool fRestoreAsRAM=%RTbool RAM=%08x\n",
3417 enmKind, GCPhys, cb, fHasHCHandler, fRestoreAsRAM, MMR3PhysGetRamSize(pVM)));
3418 VM_ASSERT_EMT(pVM);
3419
3420
3421 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
3422
3423 PDMCritSectEnter(&pVM->rem.s.CritSectRegister, VERR_SEM_BUSY);
3424 /** @todo this isn't right, MMIO can (in theory) be restored as RAM. */
3425 if (enmKind == PGMPHYSHANDLERKIND_MMIO)
3426 cpu_register_physical_memory_offset(GCPhys, cb, IO_MEM_UNASSIGNED, GCPhys);
3427 else if (fHasHCHandler)
3428 {
3429 if (!fRestoreAsRAM)
3430 {
3431 Assert(GCPhys > MMR3PhysGetRamSize(pVM));
3432 cpu_register_physical_memory_offset(GCPhys, cb, IO_MEM_UNASSIGNED, GCPhys);
3433 }
3434 else
3435 {
3436 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
3437 Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
3438 cpu_register_physical_memory_offset(GCPhys, cb, GCPhys, GCPhys);
3439 }
3440 }
3441 PDMCritSectLeave(&pVM->rem.s.CritSectRegister);
3442
3443 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
3444}
3445
3446/**
3447 * Notification about a successful PGMR3HandlerPhysicalDeregister() operation.
3448 *
3449 * @param pVM VM Handle.
3450 * @param enmKind Kind of access handler.
3451 * @param GCPhys Handler range address.
3452 * @param cb Size of the handler range.
3453 * @param fHasHCHandler Set if the handler has a HC callback function.
3454 * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
3455 */
3456REMR3DECL(void) REMR3NotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
3457{
3458 REMR3ReplayHandlerNotifications(pVM);
3459 remR3NotifyHandlerPhysicalDeregister(pVM, enmKind, GCPhys, cb, fHasHCHandler, fRestoreAsRAM);
3460}
3461
3462
3463/**
3464 * Notification about a successful PGMR3HandlerPhysicalModify() call.
3465 *
3466 * @param pVM VM Handle.
3467 * @param enmKind Kind of access handler.
3468 * @param GCPhysOld Old handler range address.
3469 * @param GCPhysNew New handler range address.
3470 * @param cb Size of the handler range.
3471 * @param fHasHCHandler Set if the handler has a HC callback function.
3472 * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
3473 */
3474static void remR3NotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
3475{
3476 Log(("REMR3NotifyHandlerPhysicalModify: enmKind=%d GCPhysOld=%RGp GCPhysNew=%RGp cb=%RGp fHasHCHandler=%RTbool fRestoreAsRAM=%RTbool\n",
3477 enmKind, GCPhysOld, GCPhysNew, cb, fHasHCHandler, fRestoreAsRAM));
3478 VM_ASSERT_EMT(pVM);
3479 AssertReleaseMsg(enmKind != PGMPHYSHANDLERKIND_MMIO, ("enmKind=%d\n", enmKind));
3480
3481 if (fHasHCHandler)
3482 {
3483 ASMAtomicIncU32(&pVM->rem.s.cIgnoreAll);
3484
3485 /*
3486 * Reset the old page.
3487 */
3488 PDMCritSectEnter(&pVM->rem.s.CritSectRegister, VERR_SEM_BUSY);
3489 if (!fRestoreAsRAM)
3490 cpu_register_physical_memory_offset(GCPhysOld, cb, IO_MEM_UNASSIGNED, GCPhysOld);
3491 else
3492 {
3493 /* This is not perfect, but it'll do for PD monitoring... */
3494 Assert(cb == PAGE_SIZE);
3495 Assert(RT_ALIGN_T(GCPhysOld, PAGE_SIZE, RTGCPHYS) == GCPhysOld);
3496 cpu_register_physical_memory_offset(GCPhysOld, cb, GCPhysOld, GCPhysOld);
3497 }
3498
3499 /*
3500 * Update the new page.
3501 */
3502 Assert(RT_ALIGN_T(GCPhysNew, PAGE_SIZE, RTGCPHYS) == GCPhysNew);
3503 Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
3504 cpu_register_physical_memory_offset(GCPhysNew, cb, pVM->rem.s.iHandlerMemType, GCPhysNew);
3505 PDMCritSectLeave(&pVM->rem.s.CritSectRegister);
3506
3507 ASMAtomicDecU32(&pVM->rem.s.cIgnoreAll);
3508 }
3509}
3510
3511/**
3512 * Notification about a successful PGMR3HandlerPhysicalModify() call.
3513 *
3514 * @param pVM VM Handle.
3515 * @param enmKind Kind of access handler.
3516 * @param GCPhysOld Old handler range address.
3517 * @param GCPhysNew New handler range address.
3518 * @param cb Size of the handler range.
3519 * @param fHasHCHandler Set if the handler has a HC callback function.
3520 * @param fRestoreAsRAM Whether the to restore it as normal RAM or as unassigned memory.
3521 */
3522REMR3DECL(void) REMR3NotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, bool fRestoreAsRAM)
3523{
3524 REMR3ReplayHandlerNotifications(pVM);
3525
3526 remR3NotifyHandlerPhysicalModify(pVM, enmKind, GCPhysOld, GCPhysNew, cb, fHasHCHandler, fRestoreAsRAM);
3527}
3528
3529/**
3530 * Checks if we're handling access to this page or not.
3531 *
3532 * @returns true if we're trapping access.
3533 * @returns false if we aren't.
3534 * @param pVM The VM handle.
3535 * @param GCPhys The physical address.
3536 *
3537 * @remark This function will only work correctly in VBOX_STRICT builds!
3538 */
3539REMR3DECL(bool) REMR3IsPageAccessHandled(PVM pVM, RTGCPHYS GCPhys)
3540{
3541#ifdef VBOX_STRICT
3542 ram_addr_t off;
3543 REMR3ReplayHandlerNotifications(pVM);
3544
3545 off = get_phys_page_offset(GCPhys);
3546 return (off & PAGE_OFFSET_MASK) == pVM->rem.s.iHandlerMemType
3547 || (off & PAGE_OFFSET_MASK) == pVM->rem.s.iMMIOMemType
3548 || (off & PAGE_OFFSET_MASK) == IO_MEM_ROM;
3549#else
3550 return false;
3551#endif
3552}
3553
3554
3555/**
3556 * Deals with a rare case in get_phys_addr_code where the code
3557 * is being monitored.
3558 *
3559 * It could also be an MMIO page, in which case we will raise a fatal error.
3560 *
3561 * @returns The physical address corresponding to addr.
3562 * @param env The cpu environment.
3563 * @param addr The virtual address.
3564 * @param pTLBEntry The TLB entry.
3565 * @param IoTlbEntry The I/O TLB entry address.
3566 */
3567target_ulong remR3PhysGetPhysicalAddressCode(CPUX86State *env,
3568 target_ulong addr,
3569 CPUTLBEntry *pTLBEntry,
3570 target_phys_addr_t IoTlbEntry)
3571{
3572 PVM pVM = env->pVM;
3573
3574 if ((IoTlbEntry & ~TARGET_PAGE_MASK) == pVM->rem.s.iHandlerMemType)
3575 {
3576 /* If code memory is being monitored, appropriate IOTLB entry will have
3577 handler IO type, and addend will provide real physical address, no
3578 matter if we store VA in TLB or not, as handlers are always passed PA */
3579 target_ulong ret = (IoTlbEntry & TARGET_PAGE_MASK) + addr;
3580 return ret;
3581 }
3582 LogRel(("\nTrying to execute code with memory type addr_code=%RGv addend=%RGp at %RGv! (iHandlerMemType=%#x iMMIOMemType=%#x IOTLB=%RGp)\n"
3583 "*** handlers\n",
3584 (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, (RTGCPTR)addr, pVM->rem.s.iHandlerMemType, pVM->rem.s.iMMIOMemType, (RTGCPHYS)IoTlbEntry));
3585 DBGFR3Info(pVM->pUVM, "handlers", NULL, DBGFR3InfoLogRelHlp());
3586 LogRel(("*** mmio\n"));
3587 DBGFR3Info(pVM->pUVM, "mmio", NULL, DBGFR3InfoLogRelHlp());
3588 LogRel(("*** phys\n"));
3589 DBGFR3Info(pVM->pUVM, "phys", NULL, DBGFR3InfoLogRelHlp());
3590 cpu_abort(env, "Trying to execute code with memory type addr_code=%RGv addend=%RGp at %RGv. (iHandlerMemType=%#x iMMIOMemType=%#x)\n",
3591 (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, (RTGCPTR)addr, pVM->rem.s.iHandlerMemType, pVM->rem.s.iMMIOMemType);
3592 AssertFatalFailed();
3593}
3594
3595/**
3596 * Read guest RAM and ROM.
3597 *
3598 * @param SrcGCPhys The source address (guest physical).
3599 * @param pvDst The destination address.
3600 * @param cb Number of bytes
3601 */
3602void remR3PhysRead(RTGCPHYS SrcGCPhys, void *pvDst, unsigned cb)
3603{
3604 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3605 VBOX_CHECK_ADDR(SrcGCPhys);
3606 VBOXSTRICTRC rcStrict = PGMPhysRead(cpu_single_env->pVM, SrcGCPhys, pvDst, cb, PGMACCESSORIGIN_REM);
3607 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3608#ifdef VBOX_DEBUG_PHYS
3609 LogRel(("read(%d): %08x\n", cb, (uint32_t)SrcGCPhys));
3610#endif
3611 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3612}
3613
3614
3615/**
3616 * Read guest RAM and ROM, unsigned 8-bit.
3617 *
3618 * @param SrcGCPhys The source address (guest physical).
3619 */
3620RTCCUINTREG remR3PhysReadU8(RTGCPHYS SrcGCPhys)
3621{
3622 uint8_t val;
3623 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3624 VBOX_CHECK_ADDR(SrcGCPhys);
3625 val = PGMR3PhysReadU8(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3626 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3627#ifdef VBOX_DEBUG_PHYS
3628 LogRel(("readu8: %x <- %08x\n", val, (uint32_t)SrcGCPhys));
3629#endif
3630 return val;
3631}
3632
3633
3634/**
3635 * Read guest RAM and ROM, signed 8-bit.
3636 *
3637 * @param SrcGCPhys The source address (guest physical).
3638 */
3639RTCCINTREG remR3PhysReadS8(RTGCPHYS SrcGCPhys)
3640{
3641 int8_t val;
3642 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3643 VBOX_CHECK_ADDR(SrcGCPhys);
3644 val = PGMR3PhysReadU8(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3645 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3646#ifdef VBOX_DEBUG_PHYS
3647 LogRel(("reads8: %x <- %08x\n", val, (uint32_t)SrcGCPhys));
3648#endif
3649 return val;
3650}
3651
3652
3653/**
3654 * Read guest RAM and ROM, unsigned 16-bit.
3655 *
3656 * @param SrcGCPhys The source address (guest physical).
3657 */
3658RTCCUINTREG remR3PhysReadU16(RTGCPHYS SrcGCPhys)
3659{
3660 uint16_t val;
3661 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3662 VBOX_CHECK_ADDR(SrcGCPhys);
3663 val = PGMR3PhysReadU16(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3664 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3665#ifdef VBOX_DEBUG_PHYS
3666 LogRel(("readu16: %x <- %08x\n", val, (uint32_t)SrcGCPhys));
3667#endif
3668 return val;
3669}
3670
3671
3672/**
3673 * Read guest RAM and ROM, signed 16-bit.
3674 *
3675 * @param SrcGCPhys The source address (guest physical).
3676 */
3677RTCCINTREG remR3PhysReadS16(RTGCPHYS SrcGCPhys)
3678{
3679 int16_t val;
3680 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3681 VBOX_CHECK_ADDR(SrcGCPhys);
3682 val = PGMR3PhysReadU16(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3683 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3684#ifdef VBOX_DEBUG_PHYS
3685 LogRel(("reads16: %x <- %08x\n", (uint16_t)val, (uint32_t)SrcGCPhys));
3686#endif
3687 return val;
3688}
3689
3690
3691/**
3692 * Read guest RAM and ROM, unsigned 32-bit.
3693 *
3694 * @param SrcGCPhys The source address (guest physical).
3695 */
3696RTCCUINTREG remR3PhysReadU32(RTGCPHYS SrcGCPhys)
3697{
3698 uint32_t val;
3699 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3700 VBOX_CHECK_ADDR(SrcGCPhys);
3701 val = PGMR3PhysReadU32(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3702 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3703#ifdef VBOX_DEBUG_PHYS
3704 LogRel(("readu32: %x <- %08x\n", val, (uint32_t)SrcGCPhys));
3705#endif
3706 return val;
3707}
3708
3709
3710/**
3711 * Read guest RAM and ROM, signed 32-bit.
3712 *
3713 * @param SrcGCPhys The source address (guest physical).
3714 */
3715RTCCINTREG remR3PhysReadS32(RTGCPHYS SrcGCPhys)
3716{
3717 int32_t val;
3718 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3719 VBOX_CHECK_ADDR(SrcGCPhys);
3720 val = PGMR3PhysReadU32(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3721 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3722#ifdef VBOX_DEBUG_PHYS
3723 LogRel(("reads32: %x <- %08x\n", val, (uint32_t)SrcGCPhys));
3724#endif
3725 return val;
3726}
3727
3728
3729/**
3730 * Read guest RAM and ROM, unsigned 64-bit.
3731 *
3732 * @param SrcGCPhys The source address (guest physical).
3733 */
3734uint64_t remR3PhysReadU64(RTGCPHYS SrcGCPhys)
3735{
3736 uint64_t val;
3737 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3738 VBOX_CHECK_ADDR(SrcGCPhys);
3739 val = PGMR3PhysReadU64(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3740 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3741#ifdef VBOX_DEBUG_PHYS
3742 LogRel(("readu64: %llx <- %08x\n", val, (uint32_t)SrcGCPhys));
3743#endif
3744 return val;
3745}
3746
3747
3748/**
3749 * Read guest RAM and ROM, signed 64-bit.
3750 *
3751 * @param SrcGCPhys The source address (guest physical).
3752 */
3753int64_t remR3PhysReadS64(RTGCPHYS SrcGCPhys)
3754{
3755 int64_t val;
3756 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3757 VBOX_CHECK_ADDR(SrcGCPhys);
3758 val = PGMR3PhysReadU64(cpu_single_env->pVM, SrcGCPhys, PGMACCESSORIGIN_REM);
3759 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3760#ifdef VBOX_DEBUG_PHYS
3761 LogRel(("reads64: %llx <- %08x\n", val, (uint32_t)SrcGCPhys));
3762#endif
3763 return val;
3764}
3765
3766
3767/**
3768 * Write guest RAM.
3769 *
3770 * @param DstGCPhys The destination address (guest physical).
3771 * @param pvSrc The source address.
3772 * @param cb Number of bytes to write
3773 */
3774void remR3PhysWrite(RTGCPHYS DstGCPhys, const void *pvSrc, unsigned cb)
3775{
3776 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3777 VBOX_CHECK_ADDR(DstGCPhys);
3778 VBOXSTRICTRC rcStrict = PGMPhysWrite(cpu_single_env->pVM, DstGCPhys, pvSrc, cb, PGMACCESSORIGIN_REM);
3779 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3780 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3781#ifdef VBOX_DEBUG_PHYS
3782 LogRel(("write(%d): %08x\n", cb, (uint32_t)DstGCPhys));
3783#endif
3784}
3785
3786
3787/**
3788 * Write guest RAM, unsigned 8-bit.
3789 *
3790 * @param DstGCPhys The destination address (guest physical).
3791 * @param val Value
3792 */
3793void remR3PhysWriteU8(RTGCPHYS DstGCPhys, uint8_t val)
3794{
3795 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3796 VBOX_CHECK_ADDR(DstGCPhys);
3797 PGMR3PhysWriteU8(cpu_single_env->pVM, DstGCPhys, val, PGMACCESSORIGIN_REM);
3798 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3799#ifdef VBOX_DEBUG_PHYS
3800 LogRel(("writeu8: %x -> %08x\n", val, (uint32_t)DstGCPhys));
3801#endif
3802}
3803
3804
3805/**
3806 * Write guest RAM, unsigned 8-bit.
3807 *
3808 * @param DstGCPhys The destination address (guest physical).
3809 * @param val Value
3810 */
3811void remR3PhysWriteU16(RTGCPHYS DstGCPhys, uint16_t val)
3812{
3813 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3814 VBOX_CHECK_ADDR(DstGCPhys);
3815 PGMR3PhysWriteU16(cpu_single_env->pVM, DstGCPhys, val, PGMACCESSORIGIN_REM);
3816 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3817#ifdef VBOX_DEBUG_PHYS
3818 LogRel(("writeu16: %x -> %08x\n", val, (uint32_t)DstGCPhys));
3819#endif
3820}
3821
3822
3823/**
3824 * Write guest RAM, unsigned 32-bit.
3825 *
3826 * @param DstGCPhys The destination address (guest physical).
3827 * @param val Value
3828 */
3829void remR3PhysWriteU32(RTGCPHYS DstGCPhys, uint32_t val)
3830{
3831 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3832 VBOX_CHECK_ADDR(DstGCPhys);
3833 PGMR3PhysWriteU32(cpu_single_env->pVM, DstGCPhys, val, PGMACCESSORIGIN_REM);
3834 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3835#ifdef VBOX_DEBUG_PHYS
3836 LogRel(("writeu32: %x -> %08x\n", val, (uint32_t)DstGCPhys));
3837#endif
3838}
3839
3840
3841/**
3842 * Write guest RAM, unsigned 64-bit.
3843 *
3844 * @param DstGCPhys The destination address (guest physical).
3845 * @param val Value
3846 */
3847void remR3PhysWriteU64(RTGCPHYS DstGCPhys, uint64_t val)
3848{
3849 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3850 VBOX_CHECK_ADDR(DstGCPhys);
3851 PGMR3PhysWriteU64(cpu_single_env->pVM, DstGCPhys, val, PGMACCESSORIGIN_REM);
3852 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3853#ifdef VBOX_DEBUG_PHYS
3854 LogRel(("writeu64: %llx -> %08x\n", val, (uint32_t)DstGCPhys));
3855#endif
3856}
3857
3858#undef LOG_GROUP
3859#define LOG_GROUP LOG_GROUP_REM_MMIO
3860
3861/** Read MMIO memory. */
3862static uint32_t remR3MMIOReadU8(void *pvEnv, target_phys_addr_t GCPhys)
3863{
3864 CPUX86State *env = (CPUX86State *)pvEnv;
3865 uint32_t u32 = 0;
3866 int rc = IOMMMIORead(env->pVM, env->pVCpu, GCPhys, &u32, 1);
3867 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc)); NOREF(rc);
3868 Log2(("remR3MMIOReadU8: GCPhys=%RGp -> %02x\n", (RTGCPHYS)GCPhys, u32));
3869 return u32;
3870}
3871
3872/** Read MMIO memory. */
3873static uint32_t remR3MMIOReadU16(void *pvEnv, target_phys_addr_t GCPhys)
3874{
3875 CPUX86State *env = (CPUX86State *)pvEnv;
3876 uint32_t u32 = 0;
3877 int rc = IOMMMIORead(env->pVM, env->pVCpu, GCPhys, &u32, 2);
3878 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc)); NOREF(rc);
3879 Log2(("remR3MMIOReadU16: GCPhys=%RGp -> %04x\n", (RTGCPHYS)GCPhys, u32));
3880 return u32;
3881}
3882
3883/** Read MMIO memory. */
3884static uint32_t remR3MMIOReadU32(void *pvEnv, target_phys_addr_t GCPhys)
3885{
3886 CPUX86State *env = (CPUX86State *)pvEnv;
3887 uint32_t u32 = 0;
3888 int rc = IOMMMIORead(env->pVM, env->pVCpu, GCPhys, &u32, 4);
3889 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc)); NOREF(rc);
3890 Log2(("remR3MMIOReadU32: GCPhys=%RGp -> %08x\n", (RTGCPHYS)GCPhys, u32));
3891 return u32;
3892}
3893
3894/** Write to MMIO memory. */
3895static void remR3MMIOWriteU8(void *pvEnv, target_phys_addr_t GCPhys, uint32_t u32)
3896{
3897 CPUX86State *env = (CPUX86State *)pvEnv;
3898 int rc;
3899 Log2(("remR3MMIOWriteU8: GCPhys=%RGp u32=%#x\n", (RTGCPHYS)GCPhys, u32));
3900 rc = IOMMMIOWrite(env->pVM, env->pVCpu, GCPhys, u32, 1);
3901 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc)); NOREF(rc);
3902}
3903
3904/** Write to MMIO memory. */
3905static void remR3MMIOWriteU16(void *pvEnv, target_phys_addr_t GCPhys, uint32_t u32)
3906{
3907 CPUX86State *env = (CPUX86State *)pvEnv;
3908 int rc;
3909 Log2(("remR3MMIOWriteU16: GCPhys=%RGp u32=%#x\n", (RTGCPHYS)GCPhys, u32));
3910 rc = IOMMMIOWrite(env->pVM, env->pVCpu, GCPhys, u32, 2);
3911 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc)); NOREF(rc);
3912}
3913
3914/** Write to MMIO memory. */
3915static void remR3MMIOWriteU32(void *pvEnv, target_phys_addr_t GCPhys, uint32_t u32)
3916{
3917 CPUX86State *env = (CPUX86State *)pvEnv;
3918 int rc;
3919 Log2(("remR3MMIOWriteU32: GCPhys=%RGp u32=%#x\n", (RTGCPHYS)GCPhys, u32));
3920 rc = IOMMMIOWrite(env->pVM, env->pVCpu, GCPhys, u32, 4);
3921 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc)); NOREF(rc);
3922}
3923
3924
3925#undef LOG_GROUP
3926#define LOG_GROUP LOG_GROUP_REM_HANDLER
3927
3928/* !!!WARNING!!! This is extremely hackish right now, we assume it's only for LFB access! !!!WARNING!!! */
3929
3930static uint32_t remR3HandlerReadU8(void *pvVM, target_phys_addr_t GCPhys)
3931{
3932 uint8_t u8;
3933 Log2(("remR3HandlerReadU8: GCPhys=%RGp\n", (RTGCPHYS)GCPhys));
3934 VBOXSTRICTRC rcStrict = PGMPhysRead((PVM)pvVM, GCPhys, &u8, sizeof(u8), PGMACCESSORIGIN_REM);
3935 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3936 return u8;
3937}
3938
3939static uint32_t remR3HandlerReadU16(void *pvVM, target_phys_addr_t GCPhys)
3940{
3941 uint16_t u16;
3942 Log2(("remR3HandlerReadU16: GCPhys=%RGp\n", (RTGCPHYS)GCPhys));
3943 VBOXSTRICTRC rcStrict = PGMPhysRead((PVM)pvVM, GCPhys, &u16, sizeof(u16), PGMACCESSORIGIN_REM);
3944 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3945 return u16;
3946}
3947
3948static uint32_t remR3HandlerReadU32(void *pvVM, target_phys_addr_t GCPhys)
3949{
3950 uint32_t u32;
3951 Log2(("remR3HandlerReadU32: GCPhys=%RGp\n", (RTGCPHYS)GCPhys));
3952 VBOXSTRICTRC rcStrict = PGMPhysRead((PVM)pvVM, GCPhys, &u32, sizeof(u32), PGMACCESSORIGIN_REM);
3953 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3954 return u32;
3955}
3956
3957static void remR3HandlerWriteU8(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3958{
3959 Log2(("remR3HandlerWriteU8: GCPhys=%RGp u32=%#x\n", (RTGCPHYS)GCPhys, u32));
3960 VBOXSTRICTRC rcStrict = PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint8_t), PGMACCESSORIGIN_REM);
3961 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3962}
3963
3964static void remR3HandlerWriteU16(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3965{
3966 Log2(("remR3HandlerWriteU16: GCPhys=%RGp u32=%#x\n", (RTGCPHYS)GCPhys, u32));
3967 VBOXSTRICTRC rcStrict = PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint16_t), PGMACCESSORIGIN_REM);
3968 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3969}
3970
3971static void remR3HandlerWriteU32(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3972{
3973 Log2(("remR3HandlerWriteU32: GCPhys=%RGp u32=%#x\n", (RTGCPHYS)GCPhys, u32));
3974 VBOXSTRICTRC rcStrict = PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint32_t), PGMACCESSORIGIN_REM);
3975 AssertMsg(rcStrict == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict))); NOREF(rcStrict);
3976}
3977
3978/* -+- disassembly -+- */
3979
3980#undef LOG_GROUP
3981#define LOG_GROUP LOG_GROUP_REM_DISAS
3982
3983
3984/**
3985 * Enables or disables singled stepped disassembly.
3986 *
3987 * @returns VBox status code.
3988 * @param pVM VM handle.
3989 * @param fEnable To enable set this flag, to disable clear it.
3990 */
3991static DECLCALLBACK(int) remR3DisasEnableStepping(PVM pVM, bool fEnable)
3992{
3993 LogFlow(("remR3DisasEnableStepping: fEnable=%d\n", fEnable));
3994 VM_ASSERT_EMT(pVM);
3995
3996 if (fEnable)
3997 pVM->rem.s.Env.state |= CPU_EMULATE_SINGLE_STEP;
3998 else
3999 pVM->rem.s.Env.state &= ~CPU_EMULATE_SINGLE_STEP;
4000#ifdef REM_USE_QEMU_SINGLE_STEP_FOR_LOGGING
4001 cpu_single_step(&pVM->rem.s.Env, fEnable);
4002#endif
4003 return VINF_SUCCESS;
4004}
4005
4006
4007/**
4008 * Enables or disables singled stepped disassembly.
4009 *
4010 * @returns VBox status code.
4011 * @param pVM VM handle.
4012 * @param fEnable To enable set this flag, to disable clear it.
4013 */
4014REMR3DECL(int) REMR3DisasEnableStepping(PVM pVM, bool fEnable)
4015{
4016 int rc;
4017
4018 LogFlow(("REMR3DisasEnableStepping: fEnable=%d\n", fEnable));
4019 if (VM_IS_EMT(pVM))
4020 return remR3DisasEnableStepping(pVM, fEnable);
4021
4022 rc = VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)remR3DisasEnableStepping, 2, pVM, fEnable);
4023 AssertRC(rc);
4024 return rc;
4025}
4026
4027
4028#ifdef VBOX_WITH_DEBUGGER
4029/**
4030 * External Debugger Command: .remstep [on|off|1|0]
4031 */
4032static DECLCALLBACK(int) remR3CmdDisasEnableStepping(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM,
4033 PCDBGCVAR paArgs, unsigned cArgs)
4034{
4035 int rc;
4036 PVM pVM = pUVM->pVM;
4037
4038 if (cArgs == 0)
4039 /*
4040 * Print the current status.
4041 */
4042 rc = DBGCCmdHlpPrintf(pCmdHlp, "DisasStepping is %s\n",
4043 pVM->rem.s.Env.state & CPU_EMULATE_SINGLE_STEP ? "enabled" : "disabled");
4044 else
4045 {
4046 /*
4047 * Convert the argument and change the mode.
4048 */
4049 bool fEnable;
4050 rc = DBGCCmdHlpVarToBool(pCmdHlp, &paArgs[0], &fEnable);
4051 if (RT_SUCCESS(rc))
4052 {
4053 rc = REMR3DisasEnableStepping(pVM, fEnable);
4054 if (RT_SUCCESS(rc))
4055 rc = DBGCCmdHlpPrintf(pCmdHlp, "DisasStepping was %s\n", fEnable ? "enabled" : "disabled");
4056 else
4057 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "REMR3DisasEnableStepping");
4058 }
4059 else
4060 rc = DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "DBGCCmdHlpVarToBool");
4061 }
4062 return rc;
4063}
4064#endif /* VBOX_WITH_DEBUGGER */
4065
4066
4067/**
4068 * Disassembles one instruction and prints it to the log.
4069 *
4070 * @returns Success indicator.
4071 * @param env Pointer to the recompiler CPU structure.
4072 * @param f32BitCode Indicates that whether or not the code should
4073 * be disassembled as 16 or 32 bit. If -1 the CS
4074 * selector will be inspected.
4075 * @param pszPrefix
4076 */
4077bool remR3DisasInstr(CPUX86State *env, int f32BitCode, char *pszPrefix)
4078{
4079 PVM pVM = env->pVM;
4080 const bool fLog = LogIsEnabled();
4081 const bool fLog2 = LogIs2Enabled();
4082 int rc = VINF_SUCCESS;
4083
4084 /*
4085 * Don't bother if there ain't any log output to do.
4086 */
4087 if (!fLog && !fLog2)
4088 return true;
4089
4090 /*
4091 * Update the state so DBGF reads the correct register values.
4092 */
4093 remR3StateUpdate(pVM, env->pVCpu);
4094
4095 /*
4096 * Log registers if requested.
4097 */
4098 if (fLog2)
4099 DBGFR3_INFO_LOG(pVM, env->pVCpu, "cpumguest", pszPrefix);
4100
4101 /*
4102 * Disassemble to log.
4103 */
4104 if (fLog)
4105 {
4106 PVMCPU pVCpu = VMMGetCpu(pVM);
4107 char szBuf[256];
4108 szBuf[0] = '\0';
4109 int rc = DBGFR3DisasInstrEx(pVCpu->pVMR3->pUVM,
4110 pVCpu->idCpu,
4111 0, /* Sel */ 0, /* GCPtr */
4112 DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_DEFAULT_MODE,
4113 szBuf,
4114 sizeof(szBuf),
4115 NULL);
4116 if (RT_FAILURE(rc))
4117 RTStrPrintf(szBuf, sizeof(szBuf), "DBGFR3DisasInstrEx failed with rc=%Rrc\n", rc);
4118 if (pszPrefix && *pszPrefix)
4119 RTLogPrintf("%s-CPU%d: %s\n", pszPrefix, pVCpu->idCpu, szBuf);
4120 else
4121 RTLogPrintf("CPU%d: %s\n", pVCpu->idCpu, szBuf);
4122 }
4123
4124 return RT_SUCCESS(rc);
4125}
4126
4127
4128/**
4129 * Disassemble recompiled code.
4130 *
4131 * @param phFileIgnored Ignored, logfile usually.
4132 * @param pvCode Pointer to the code block.
4133 * @param cb Size of the code block.
4134 */
4135void disas(FILE *phFileIgnored, void *pvCode, unsigned long cb)
4136{
4137 if (LogIs2Enabled())
4138 {
4139 unsigned off = 0;
4140 char szOutput[256];
4141 DISCPUSTATE Cpu;
4142#ifdef RT_ARCH_X86
4143 DISCPUMODE enmCpuMode = DISCPUMODE_32BIT;
4144#else
4145 DISCPUMODE enmCpuMode = DISCPUMODE_64BIT;
4146#endif
4147
4148 RTLogPrintf("Recompiled Code: %p %#lx (%ld) bytes\n", pvCode, cb, cb);
4149 while (off < cb)
4150 {
4151 uint32_t cbInstr;
4152 int rc = DISInstrToStr((uint8_t const *)pvCode + off, enmCpuMode,
4153 &Cpu, &cbInstr, szOutput, sizeof(szOutput));
4154 if (RT_SUCCESS(rc))
4155 RTLogPrintf("%s", szOutput);
4156 else
4157 {
4158 RTLogPrintf("disas error %Rrc\n", rc);
4159 cbInstr = 1;
4160 }
4161 off += cbInstr;
4162 }
4163 }
4164}
4165
4166
4167/**
4168 * Disassemble guest code.
4169 *
4170 * @param phFileIgnored Ignored, logfile usually.
4171 * @param uCode The guest address of the code to disassemble. (flat?)
4172 * @param cb Number of bytes to disassemble.
4173 * @param fFlags Flags, probably something which tells if this is 16, 32 or 64 bit code.
4174 */
4175void target_disas(FILE *phFileIgnored, target_ulong uCode, target_ulong cb, int fFlags)
4176{
4177 if (LogIs2Enabled())
4178 {
4179 PVM pVM = cpu_single_env->pVM;
4180 PVMCPU pVCpu = cpu_single_env->pVCpu;
4181 RTSEL cs;
4182 RTGCUINTPTR eip;
4183
4184 Assert(pVCpu);
4185
4186 /*
4187 * Update the state so DBGF reads the correct register values (flags).
4188 */
4189 remR3StateUpdate(pVM, pVCpu);
4190
4191 /*
4192 * Do the disassembling.
4193 */
4194 RTLogPrintf("Guest Code: PC=%llx %llx bytes fFlags=%d\n", (uint64_t)uCode, (uint64_t)cb, fFlags);
4195 cs = cpu_single_env->segs[R_CS].selector;
4196 eip = uCode - cpu_single_env->segs[R_CS].base;
4197 for (;;)
4198 {
4199 char szBuf[256];
4200 uint32_t cbInstr;
4201 int rc = DBGFR3DisasInstrEx(pVM->pUVM,
4202 pVCpu->idCpu,
4203 cs,
4204 eip,
4205 DBGF_DISAS_FLAGS_DEFAULT_MODE,
4206 szBuf, sizeof(szBuf),
4207 &cbInstr);
4208 if (RT_SUCCESS(rc))
4209 RTLogPrintf("%llx %s\n", (uint64_t)uCode, szBuf);
4210 else
4211 {
4212 RTLogPrintf("%llx %04x:%llx: %s\n", (uint64_t)uCode, cs, (uint64_t)eip, szBuf);
4213 cbInstr = 1;
4214 }
4215
4216 /* next */
4217 if (cb <= cbInstr)
4218 break;
4219 cb -= cbInstr;
4220 uCode += cbInstr;
4221 eip += cbInstr;
4222 }
4223 }
4224}
4225
4226
4227/**
4228 * Looks up a guest symbol.
4229 *
4230 * @returns Pointer to symbol name. This is a static buffer.
4231 * @param orig_addr The address in question.
4232 */
4233const char *lookup_symbol(target_ulong orig_addr)
4234{
4235 PVM pVM = cpu_single_env->pVM;
4236 RTGCINTPTR off = 0;
4237 RTDBGSYMBOL Sym;
4238 DBGFADDRESS Addr;
4239
4240 int rc = DBGFR3AsSymbolByAddr(pVM->pUVM, DBGF_AS_GLOBAL, DBGFR3AddrFromFlat(pVM->pUVM, &Addr, orig_addr),
4241 RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &off, &Sym, NULL /*phMod*/);
4242 if (RT_SUCCESS(rc))
4243 {
4244 static char szSym[sizeof(Sym.szName) + 48];
4245 if (!off)
4246 RTStrPrintf(szSym, sizeof(szSym), "%s\n", Sym.szName);
4247 else if (off > 0)
4248 RTStrPrintf(szSym, sizeof(szSym), "%s+%x\n", Sym.szName, off);
4249 else
4250 RTStrPrintf(szSym, sizeof(szSym), "%s-%x\n", Sym.szName, -off);
4251 return szSym;
4252 }
4253 return "<N/A>";
4254}
4255
4256
4257#undef LOG_GROUP
4258#define LOG_GROUP LOG_GROUP_REM
4259
4260
4261/* -+- FF notifications -+- */
4262
4263/**
4264 * Notification about the interrupt FF being set.
4265 *
4266 * @param pVM VM Handle.
4267 * @param pVCpu VMCPU Handle.
4268 * @thread The emulation thread.
4269 */
4270REMR3DECL(void) REMR3NotifyInterruptSet(PVM pVM, PVMCPU pVCpu)
4271{
4272#ifndef IEM_VERIFICATION_MODE
4273 LogFlow(("REMR3NotifyInterruptSet: fInRem=%d interrupts %s\n", pVM->rem.s.fInREM,
4274 (pVM->rem.s.Env.eflags & IF_MASK) && !(pVM->rem.s.Env.hflags & HF_INHIBIT_IRQ_MASK) ? "enabled" : "disabled"));
4275 if (pVM->rem.s.fInREM)
4276 {
4277 ASMAtomicOrS32((int32_t volatile *)&cpu_single_env->interrupt_request,
4278 CPU_INTERRUPT_EXTERNAL_HARD);
4279 }
4280#endif
4281}
4282
4283
4284/**
4285 * Notification about the interrupt FF being set.
4286 *
4287 * @param pVM VM Handle.
4288 * @param pVCpu VMCPU Handle.
4289 * @thread Any.
4290 */
4291REMR3DECL(void) REMR3NotifyInterruptClear(PVM pVM, PVMCPU pVCpu)
4292{
4293 LogFlow(("REMR3NotifyInterruptClear:\n"));
4294 if (pVM->rem.s.fInREM)
4295 cpu_reset_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
4296}
4297
4298
4299/**
4300 * Notification about pending timer(s).
4301 *
4302 * @param pVM VM Handle.
4303 * @param pVCpuDst The target cpu for this notification.
4304 * TM will not broadcast pending timer events, but use
4305 * a dedicated EMT for them. So, only interrupt REM
4306 * execution if the given CPU is executing in REM.
4307 * @thread Any.
4308 */
4309REMR3DECL(void) REMR3NotifyTimerPending(PVM pVM, PVMCPU pVCpuDst)
4310{
4311#ifndef IEM_VERIFICATION_MODE
4312#ifndef DEBUG_bird
4313 LogFlow(("REMR3NotifyTimerPending: fInRem=%d\n", pVM->rem.s.fInREM));
4314#endif
4315 if (pVM->rem.s.fInREM)
4316 {
4317 if (pVM->rem.s.Env.pVCpu == pVCpuDst)
4318 {
4319 LogIt(RTLOGGRPFLAGS_LEVEL_5, LOG_GROUP_TM, ("REMR3NotifyTimerPending: setting\n"));
4320 ASMAtomicOrS32((int32_t volatile *)&pVM->rem.s.Env.interrupt_request,
4321 CPU_INTERRUPT_EXTERNAL_TIMER);
4322 }
4323 else
4324 LogIt(RTLOGGRPFLAGS_LEVEL_5, LOG_GROUP_TM, ("REMR3NotifyTimerPending: pVCpu:%p != pVCpuDst:%p\n", pVM->rem.s.Env.pVCpu, pVCpuDst));
4325 }
4326 else
4327 LogIt(RTLOGGRPFLAGS_LEVEL_5, LOG_GROUP_TM, ("REMR3NotifyTimerPending: !fInREM; cpu state=%d\n", VMCPU_GET_STATE(pVCpuDst)));
4328#endif
4329}
4330
4331
4332/**
4333 * Notification about pending DMA transfers.
4334 *
4335 * @param pVM VM Handle.
4336 * @thread Any.
4337 */
4338REMR3DECL(void) REMR3NotifyDmaPending(PVM pVM)
4339{
4340#ifndef IEM_VERIFICATION_MODE
4341 LogFlow(("REMR3NotifyDmaPending: fInRem=%d\n", pVM->rem.s.fInREM));
4342 if (pVM->rem.s.fInREM)
4343 {
4344 ASMAtomicOrS32((int32_t volatile *)&cpu_single_env->interrupt_request,
4345 CPU_INTERRUPT_EXTERNAL_DMA);
4346 }
4347#endif
4348}
4349
4350
4351/**
4352 * Notification about pending timer(s).
4353 *
4354 * @param pVM VM Handle.
4355 * @thread Any.
4356 */
4357REMR3DECL(void) REMR3NotifyQueuePending(PVM pVM)
4358{
4359#ifndef IEM_VERIFICATION_MODE
4360 LogFlow(("REMR3NotifyQueuePending: fInRem=%d\n", pVM->rem.s.fInREM));
4361 if (pVM->rem.s.fInREM)
4362 {
4363 ASMAtomicOrS32((int32_t volatile *)&cpu_single_env->interrupt_request,
4364 CPU_INTERRUPT_EXTERNAL_EXIT);
4365 }
4366#endif
4367}
4368
4369
4370/**
4371 * Notification about pending FF set by an external thread.
4372 *
4373 * @param pVM VM handle.
4374 * @thread Any.
4375 */
4376REMR3DECL(void) REMR3NotifyFF(PVM pVM)
4377{
4378#ifndef IEM_VERIFICATION_MODE
4379 LogFlow(("REMR3NotifyFF: fInRem=%d\n", pVM->rem.s.fInREM));
4380 if (pVM->rem.s.fInREM)
4381 {
4382 ASMAtomicOrS32((int32_t volatile *)&cpu_single_env->interrupt_request,
4383 CPU_INTERRUPT_EXTERNAL_EXIT);
4384 }
4385#endif
4386}
4387
4388
4389#ifdef VBOX_WITH_STATISTICS
4390void remR3ProfileStart(int statcode)
4391{
4392 STAMPROFILEADV *pStat;
4393 switch(statcode)
4394 {
4395 case STATS_EMULATE_SINGLE_INSTR:
4396 pStat = &gStatExecuteSingleInstr;
4397 break;
4398 case STATS_QEMU_COMPILATION:
4399 pStat = &gStatCompilationQEmu;
4400 break;
4401 case STATS_QEMU_RUN_EMULATED_CODE:
4402 pStat = &gStatRunCodeQEmu;
4403 break;
4404 case STATS_QEMU_TOTAL:
4405 pStat = &gStatTotalTimeQEmu;
4406 break;
4407 case STATS_QEMU_RUN_TIMERS:
4408 pStat = &gStatTimers;
4409 break;
4410 case STATS_TLB_LOOKUP:
4411 pStat= &gStatTBLookup;
4412 break;
4413 case STATS_IRQ_HANDLING:
4414 pStat= &gStatIRQ;
4415 break;
4416 case STATS_RAW_CHECK:
4417 pStat = &gStatRawCheck;
4418 break;
4419
4420 default:
4421 AssertMsgFailed(("unknown stat %d\n", statcode));
4422 return;
4423 }
4424 STAM_PROFILE_ADV_START(pStat, a);
4425}
4426
4427
4428void remR3ProfileStop(int statcode)
4429{
4430 STAMPROFILEADV *pStat;
4431 switch(statcode)
4432 {
4433 case STATS_EMULATE_SINGLE_INSTR:
4434 pStat = &gStatExecuteSingleInstr;
4435 break;
4436 case STATS_QEMU_COMPILATION:
4437 pStat = &gStatCompilationQEmu;
4438 break;
4439 case STATS_QEMU_RUN_EMULATED_CODE:
4440 pStat = &gStatRunCodeQEmu;
4441 break;
4442 case STATS_QEMU_TOTAL:
4443 pStat = &gStatTotalTimeQEmu;
4444 break;
4445 case STATS_QEMU_RUN_TIMERS:
4446 pStat = &gStatTimers;
4447 break;
4448 case STATS_TLB_LOOKUP:
4449 pStat= &gStatTBLookup;
4450 break;
4451 case STATS_IRQ_HANDLING:
4452 pStat= &gStatIRQ;
4453 break;
4454 case STATS_RAW_CHECK:
4455 pStat = &gStatRawCheck;
4456 break;
4457 default:
4458 AssertMsgFailed(("unknown stat %d\n", statcode));
4459 return;
4460 }
4461 STAM_PROFILE_ADV_STOP(pStat, a);
4462}
4463#endif
4464
4465/**
4466 * Raise an RC, force rem exit.
4467 *
4468 * @param pVM VM handle.
4469 * @param rc The rc.
4470 */
4471void remR3RaiseRC(PVM pVM, int rc)
4472{
4473 Log(("remR3RaiseRC: rc=%Rrc\n", rc));
4474 Assert(pVM->rem.s.fInREM);
4475 VM_ASSERT_EMT(pVM);
4476 pVM->rem.s.rc = rc;
4477 cpu_interrupt(&pVM->rem.s.Env, CPU_INTERRUPT_RC);
4478}
4479
4480
4481/* -+- timers -+- */
4482
4483uint64_t cpu_get_tsc(CPUX86State *env)
4484{
4485 STAM_COUNTER_INC(&gStatCpuGetTSC);
4486 return TMCpuTickGet(env->pVCpu);
4487}
4488
4489
4490/* -+- interrupts -+- */
4491
4492void cpu_set_ferr(CPUX86State *env)
4493{
4494 int rc = PDMIsaSetIrq(env->pVM, 13, 1, 0 /*uTagSrc*/);
4495 LogFlow(("cpu_set_ferr: rc=%d\n", rc)); NOREF(rc);
4496}
4497
4498int cpu_get_pic_interrupt(CPUX86State *env)
4499{
4500 uint8_t u8Interrupt;
4501 int rc;
4502
4503#ifdef VBOX_WITH_NEW_APIC
4504 if (VMCPU_FF_TEST_AND_CLEAR(env->pVCpu, VMCPU_FF_UPDATE_APIC))
4505 APICUpdatePendingInterrupts(env->pVCpu);
4506#endif
4507
4508 /* When we fail to forward interrupts directly in raw mode, we fall back to the recompiler.
4509 * In that case we can't call PDMGetInterrupt anymore, because it has already cleared the interrupt
4510 * with the (a)pic.
4511 */
4512 /* Note! We assume we will go directly to the recompiler to handle the pending interrupt! */
4513 rc = PDMGetInterrupt(env->pVCpu, &u8Interrupt);
4514 LogFlow(("cpu_get_pic_interrupt: u8Interrupt=%d rc=%Rrc pc=%04x:%08llx ~flags=%08llx\n",
4515 u8Interrupt, rc, env->segs[R_CS].selector, (uint64_t)env->eip, (uint64_t)env->eflags));
4516 if (RT_SUCCESS(rc))
4517 {
4518 if (VMCPU_FF_IS_PENDING(env->pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC))
4519 env->interrupt_request |= CPU_INTERRUPT_HARD;
4520 return u8Interrupt;
4521 }
4522 return -1;
4523}
4524
4525
4526/* -+- local apic -+- */
4527
4528#if 0 /* CPUMSetGuestMsr does this now. */
4529void cpu_set_apic_base(CPUX86State *env, uint64_t val)
4530{
4531 int rc = PDMApicSetBase(env->pVM, val);
4532 LogFlow(("cpu_set_apic_base: val=%#llx rc=%Rrc\n", val, rc)); NOREF(rc);
4533}
4534#endif
4535
4536uint64_t cpu_get_apic_base(CPUX86State *env)
4537{
4538 uint64_t u64;
4539 VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(env->pVCpu, MSR_IA32_APICBASE, &u64);
4540 if (RT_SUCCESS(rcStrict))
4541 {
4542 LogFlow(("cpu_get_apic_base: returns %#llx \n", u64));
4543 return u64;
4544 }
4545 LogFlow(("cpu_get_apic_base: returns 0 (rc=%Rrc)\n", VBOXSTRICTRC_VAL(rcStrict)));
4546 return 0;
4547}
4548
4549void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
4550{
4551 int rc = PDMApicSetTPR(env->pVCpu, val << 4); /* cr8 bits 3-0 correspond to bits 7-4 of the task priority mmio register. */
4552 LogFlow(("cpu_set_apic_tpr: val=%#x rc=%Rrc\n", val, rc)); NOREF(rc);
4553}
4554
4555uint8_t cpu_get_apic_tpr(CPUX86State *env)
4556{
4557 uint8_t u8;
4558 int rc = PDMApicGetTPR(env->pVCpu, &u8, NULL, NULL);
4559 if (RT_SUCCESS(rc))
4560 {
4561 LogFlow(("cpu_get_apic_tpr: returns %#x\n", u8));
4562 return u8 >> 4; /* cr8 bits 3-0 correspond to bits 7-4 of the task priority mmio register. */
4563 }
4564 LogFlow(("cpu_get_apic_tpr: returns 0 (rc=%Rrc)\n", rc));
4565 return 0;
4566}
4567
4568/**
4569 * Read an MSR.
4570 *
4571 * @retval 0 success.
4572 * @retval -1 failure, raise \#GP(0).
4573 * @param env The cpu state.
4574 * @param idMsr The MSR to read.
4575 * @param puValue Where to return the value.
4576 */
4577int cpu_rdmsr(CPUX86State *env, uint32_t idMsr, uint64_t *puValue)
4578{
4579 Assert(env->pVCpu);
4580 return CPUMQueryGuestMsr(env->pVCpu, idMsr, puValue) == VINF_SUCCESS ? 0 : -1;
4581}
4582
4583/**
4584 * Write to an MSR.
4585 *
4586 * @retval 0 success.
4587 * @retval -1 failure, raise \#GP(0).
4588 * @param env The cpu state.
4589 * @param idMsr The MSR to write to.
4590 * @param uValue The value to write.
4591 */
4592int cpu_wrmsr(CPUX86State *env, uint32_t idMsr, uint64_t uValue)
4593{
4594 Assert(env->pVCpu);
4595 return CPUMSetGuestMsr(env->pVCpu, idMsr, uValue) == VINF_SUCCESS ? 0 : -1;
4596}
4597
4598/* -+- I/O Ports -+- */
4599
4600#undef LOG_GROUP
4601#define LOG_GROUP LOG_GROUP_REM_IOPORT
4602
4603void cpu_outb(CPUX86State *env, pio_addr_t addr, uint8_t val)
4604{
4605 int rc;
4606
4607 if (addr != 0x80 && addr != 0x70 && addr != 0x61)
4608 Log2(("cpu_outb: addr=%#06x val=%#x\n", addr, val));
4609
4610 rc = IOMIOPortWrite(env->pVM, env->pVCpu, (RTIOPORT)addr, val, 1);
4611 if (RT_LIKELY(rc == VINF_SUCCESS))
4612 return;
4613 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4614 {
4615 Log(("cpu_outb: addr=%#06x val=%#x -> %Rrc\n", addr, val, rc));
4616 remR3RaiseRC(env->pVM, rc);
4617 return;
4618 }
4619 remAbort(rc, __FUNCTION__);
4620}
4621
4622void cpu_outw(CPUX86State *env, pio_addr_t addr, uint16_t val)
4623{
4624 //Log2(("cpu_outw: addr=%#06x val=%#x\n", addr, val));
4625 int rc = IOMIOPortWrite(env->pVM, env->pVCpu, (RTIOPORT)addr, val, 2);
4626 if (RT_LIKELY(rc == VINF_SUCCESS))
4627 return;
4628 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4629 {
4630 Log(("cpu_outw: addr=%#06x val=%#x -> %Rrc\n", addr, val, rc));
4631 remR3RaiseRC(env->pVM, rc);
4632 return;
4633 }
4634 remAbort(rc, __FUNCTION__);
4635}
4636
4637void cpu_outl(CPUX86State *env, pio_addr_t addr, uint32_t val)
4638{
4639 int rc;
4640 Log2(("cpu_outl: addr=%#06x val=%#x\n", addr, val));
4641 rc = IOMIOPortWrite(env->pVM, env->pVCpu, (RTIOPORT)addr, val, 4);
4642 if (RT_LIKELY(rc == VINF_SUCCESS))
4643 return;
4644 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4645 {
4646 Log(("cpu_outl: addr=%#06x val=%#x -> %Rrc\n", addr, val, rc));
4647 remR3RaiseRC(env->pVM, rc);
4648 return;
4649 }
4650 remAbort(rc, __FUNCTION__);
4651}
4652
4653uint8_t cpu_inb(CPUX86State *env, pio_addr_t addr)
4654{
4655 uint32_t u32 = 0;
4656 int rc = IOMIOPortRead(env->pVM, env->pVCpu, (RTIOPORT)addr, &u32, 1);
4657 if (RT_LIKELY(rc == VINF_SUCCESS))
4658 {
4659 if (/*addr != 0x61 && */addr != 0x71)
4660 Log2(("cpu_inb: addr=%#06x -> %#x\n", addr, u32));
4661 return (uint8_t)u32;
4662 }
4663 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4664 {
4665 Log(("cpu_inb: addr=%#06x -> %#x rc=%Rrc\n", addr, u32, rc));
4666 remR3RaiseRC(env->pVM, rc);
4667 return (uint8_t)u32;
4668 }
4669 remAbort(rc, __FUNCTION__);
4670 return UINT8_C(0xff);
4671}
4672
4673uint16_t cpu_inw(CPUX86State *env, pio_addr_t addr)
4674{
4675 uint32_t u32 = 0;
4676 int rc = IOMIOPortRead(env->pVM, env->pVCpu, (RTIOPORT)addr, &u32, 2);
4677 if (RT_LIKELY(rc == VINF_SUCCESS))
4678 {
4679 Log2(("cpu_inw: addr=%#06x -> %#x\n", addr, u32));
4680 return (uint16_t)u32;
4681 }
4682 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4683 {
4684 Log(("cpu_inw: addr=%#06x -> %#x rc=%Rrc\n", addr, u32, rc));
4685 remR3RaiseRC(env->pVM, rc);
4686 return (uint16_t)u32;
4687 }
4688 remAbort(rc, __FUNCTION__);
4689 return UINT16_C(0xffff);
4690}
4691
4692uint32_t cpu_inl(CPUX86State *env, pio_addr_t addr)
4693{
4694 uint32_t u32 = 0;
4695 int rc = IOMIOPortRead(env->pVM, env->pVCpu, (RTIOPORT)addr, &u32, 4);
4696 if (RT_LIKELY(rc == VINF_SUCCESS))
4697 {
4698 Log2(("cpu_inl: addr=%#06x -> %#x\n", addr, u32));
4699 return u32;
4700 }
4701 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4702 {
4703 Log(("cpu_inl: addr=%#06x -> %#x rc=%Rrc\n", addr, u32, rc));
4704 remR3RaiseRC(env->pVM, rc);
4705 return u32;
4706 }
4707 remAbort(rc, __FUNCTION__);
4708 return UINT32_C(0xffffffff);
4709}
4710
4711#undef LOG_GROUP
4712#define LOG_GROUP LOG_GROUP_REM
4713
4714
4715/* -+- helpers and misc other interfaces -+- */
4716
4717/**
4718 * Perform the CPUID instruction.
4719 *
4720 * @param env Pointer to the recompiler CPU structure.
4721 * @param idx The CPUID leaf (eax).
4722 * @param idxSub The CPUID sub-leaf (ecx) where applicable.
4723 * @param pEAX Where to store eax.
4724 * @param pEBX Where to store ebx.
4725 * @param pECX Where to store ecx.
4726 * @param pEDX Where to store edx.
4727 */
4728void cpu_x86_cpuid(CPUX86State *env, uint32_t idx, uint32_t idxSub,
4729 uint32_t *pEAX, uint32_t *pEBX, uint32_t *pECX, uint32_t *pEDX)
4730{
4731 NOREF(idxSub);
4732 CPUMGetGuestCpuId(env->pVCpu, idx, idxSub, pEAX, pEBX, pECX, pEDX);
4733}
4734
4735
4736#if 0 /* not used */
4737/**
4738 * Interface for qemu hardware to report back fatal errors.
4739 */
4740void hw_error(const char *pszFormat, ...)
4741{
4742 /*
4743 * Bitch about it.
4744 */
4745 /** @todo Add support for nested arg lists in the LogPrintfV routine! I've code for
4746 * this in my Odin32 tree at home! */
4747 va_list args;
4748 va_start(args, pszFormat);
4749 RTLogPrintf("fatal error in virtual hardware:");
4750 RTLogPrintfV(pszFormat, args);
4751 va_end(args);
4752 AssertReleaseMsgFailed(("fatal error in virtual hardware: %s\n", pszFormat));
4753
4754 /*
4755 * If we're in REM context we'll sync back the state before 'jumping' to
4756 * the EMs failure handling.
4757 */
4758 PVM pVM = cpu_single_env->pVM;
4759 if (pVM->rem.s.fInREM)
4760 REMR3StateBack(pVM);
4761 EMR3FatalError(pVM, VERR_REM_VIRTUAL_HARDWARE_ERROR);
4762 AssertMsgFailed(("EMR3FatalError returned!\n"));
4763}
4764#endif
4765
4766/**
4767 * Interface for the qemu cpu to report unhandled situation
4768 * raising a fatal VM error.
4769 */
4770void cpu_abort(CPUX86State *env, const char *pszFormat, ...)
4771{
4772 va_list va;
4773 PVM pVM;
4774 PVMCPU pVCpu;
4775 char szMsg[256];
4776
4777 /*
4778 * Bitch about it.
4779 */
4780 RTLogFlags(NULL, "nodisabled nobuffered");
4781 RTLogFlush(NULL);
4782
4783 va_start(va, pszFormat);
4784#if defined(RT_OS_WINDOWS) && ARCH_BITS == 64
4785 /* It's a bit complicated when mixing MSC and GCC on AMD64. This is a bit ugly, but it works. */
4786 unsigned cArgs = 0;
4787 uintptr_t auArgs[6] = {0,0,0,0,0,0};
4788 const char *psz = strchr(pszFormat, '%');
4789 while (psz && cArgs < 6)
4790 {
4791 auArgs[cArgs++] = va_arg(va, uintptr_t);
4792 psz = strchr(psz + 1, '%');
4793 }
4794 switch (cArgs)
4795 {
4796 case 1: RTStrPrintf(szMsg, sizeof(szMsg), pszFormat, auArgs[0]); break;
4797 case 2: RTStrPrintf(szMsg, sizeof(szMsg), pszFormat, auArgs[0], auArgs[1]); break;
4798 case 3: RTStrPrintf(szMsg, sizeof(szMsg), pszFormat, auArgs[0], auArgs[1], auArgs[2]); break;
4799 case 4: RTStrPrintf(szMsg, sizeof(szMsg), pszFormat, auArgs[0], auArgs[1], auArgs[2], auArgs[3]); break;
4800 case 5: RTStrPrintf(szMsg, sizeof(szMsg), pszFormat, auArgs[0], auArgs[1], auArgs[2], auArgs[3], auArgs[4]); break;
4801 case 6: RTStrPrintf(szMsg, sizeof(szMsg), pszFormat, auArgs[0], auArgs[1], auArgs[2], auArgs[3], auArgs[4], auArgs[5]); break;
4802 default:
4803 case 0: RTStrPrintf(szMsg, sizeof(szMsg), "%s", pszFormat); break;
4804 }
4805#else
4806 RTStrPrintfV(szMsg, sizeof(szMsg), pszFormat, va);
4807#endif
4808 va_end(va);
4809
4810 RTLogPrintf("fatal error in recompiler cpu: %s\n", szMsg);
4811 RTLogRelPrintf("fatal error in recompiler cpu: %s\n", szMsg);
4812
4813 /*
4814 * If we're in REM context we'll sync back the state before 'jumping' to
4815 * the EMs failure handling.
4816 */
4817 pVM = cpu_single_env->pVM;
4818 pVCpu = cpu_single_env->pVCpu;
4819 Assert(pVCpu);
4820
4821 if (pVM->rem.s.fInREM)
4822 REMR3StateBack(pVM, pVCpu);
4823 EMR3FatalError(pVCpu, VERR_REM_VIRTUAL_CPU_ERROR);
4824 AssertMsgFailed(("EMR3FatalError returned!\n"));
4825}
4826
4827
4828/**
4829 * Aborts the VM.
4830 *
4831 * @param rc VBox error code.
4832 * @param pszTip Hint about why/when this happened.
4833 */
4834void remAbort(int rc, const char *pszTip)
4835{
4836 PVM pVM;
4837 PVMCPU pVCpu;
4838
4839 /*
4840 * Bitch about it.
4841 */
4842 RTLogPrintf("internal REM fatal error: rc=%Rrc %s\n", rc, pszTip);
4843 AssertReleaseMsgFailed(("internal REM fatal error: rc=%Rrc %s\n", rc, pszTip));
4844
4845 /*
4846 * Jump back to where we entered the recompiler.
4847 */
4848 pVM = cpu_single_env->pVM;
4849 pVCpu = cpu_single_env->pVCpu;
4850 Assert(pVCpu);
4851
4852 if (pVM->rem.s.fInREM)
4853 REMR3StateBack(pVM, pVCpu);
4854
4855 EMR3FatalError(pVCpu, rc);
4856 AssertMsgFailed(("EMR3FatalError returned!\n"));
4857}
4858
4859
4860/**
4861 * Dumps a linux system call.
4862 * @param pVCpu VMCPU handle.
4863 */
4864void remR3DumpLnxSyscall(PVMCPU pVCpu)
4865{
4866 static const char *apsz[] =
4867 {
4868 "sys_restart_syscall", /* 0 - old "setup()" system call, used for restarting */
4869 "sys_exit",
4870 "sys_fork",
4871 "sys_read",
4872 "sys_write",
4873 "sys_open", /* 5 */
4874 "sys_close",
4875 "sys_waitpid",
4876 "sys_creat",
4877 "sys_link",
4878 "sys_unlink", /* 10 */
4879 "sys_execve",
4880 "sys_chdir",
4881 "sys_time",
4882 "sys_mknod",
4883 "sys_chmod", /* 15 */
4884 "sys_lchown16",
4885 "sys_ni_syscall", /* old break syscall holder */
4886 "sys_stat",
4887 "sys_lseek",
4888 "sys_getpid", /* 20 */
4889 "sys_mount",
4890 "sys_oldumount",
4891 "sys_setuid16",
4892 "sys_getuid16",
4893 "sys_stime", /* 25 */
4894 "sys_ptrace",
4895 "sys_alarm",
4896 "sys_fstat",
4897 "sys_pause",
4898 "sys_utime", /* 30 */
4899 "sys_ni_syscall", /* old stty syscall holder */
4900 "sys_ni_syscall", /* old gtty syscall holder */
4901 "sys_access",
4902 "sys_nice",
4903 "sys_ni_syscall", /* 35 - old ftime syscall holder */
4904 "sys_sync",
4905 "sys_kill",
4906 "sys_rename",
4907 "sys_mkdir",
4908 "sys_rmdir", /* 40 */
4909 "sys_dup",
4910 "sys_pipe",
4911 "sys_times",
4912 "sys_ni_syscall", /* old prof syscall holder */
4913 "sys_brk", /* 45 */
4914 "sys_setgid16",
4915 "sys_getgid16",
4916 "sys_signal",
4917 "sys_geteuid16",
4918 "sys_getegid16", /* 50 */
4919 "sys_acct",
4920 "sys_umount", /* recycled never used phys() */
4921 "sys_ni_syscall", /* old lock syscall holder */
4922 "sys_ioctl",
4923 "sys_fcntl", /* 55 */
4924 "sys_ni_syscall", /* old mpx syscall holder */
4925 "sys_setpgid",
4926 "sys_ni_syscall", /* old ulimit syscall holder */
4927 "sys_olduname",
4928 "sys_umask", /* 60 */
4929 "sys_chroot",
4930 "sys_ustat",
4931 "sys_dup2",
4932 "sys_getppid",
4933 "sys_getpgrp", /* 65 */
4934 "sys_setsid",
4935 "sys_sigaction",
4936 "sys_sgetmask",
4937 "sys_ssetmask",
4938 "sys_setreuid16", /* 70 */
4939 "sys_setregid16",
4940 "sys_sigsuspend",
4941 "sys_sigpending",
4942 "sys_sethostname",
4943 "sys_setrlimit", /* 75 */
4944 "sys_old_getrlimit",
4945 "sys_getrusage",
4946 "sys_gettimeofday",
4947 "sys_settimeofday",
4948 "sys_getgroups16", /* 80 */
4949 "sys_setgroups16",
4950 "old_select",
4951 "sys_symlink",
4952 "sys_lstat",
4953 "sys_readlink", /* 85 */
4954 "sys_uselib",
4955 "sys_swapon",
4956 "sys_reboot",
4957 "old_readdir",
4958 "old_mmap", /* 90 */
4959 "sys_munmap",
4960 "sys_truncate",
4961 "sys_ftruncate",
4962 "sys_fchmod",
4963 "sys_fchown16", /* 95 */
4964 "sys_getpriority",
4965 "sys_setpriority",
4966 "sys_ni_syscall", /* old profil syscall holder */
4967 "sys_statfs",
4968 "sys_fstatfs", /* 100 */
4969 "sys_ioperm",
4970 "sys_socketcall",
4971 "sys_syslog",
4972 "sys_setitimer",
4973 "sys_getitimer", /* 105 */
4974 "sys_newstat",
4975 "sys_newlstat",
4976 "sys_newfstat",
4977 "sys_uname",
4978 "sys_iopl", /* 110 */
4979 "sys_vhangup",
4980 "sys_ni_syscall", /* old "idle" system call */
4981 "sys_vm86old",
4982 "sys_wait4",
4983 "sys_swapoff", /* 115 */
4984 "sys_sysinfo",
4985 "sys_ipc",
4986 "sys_fsync",
4987 "sys_sigreturn",
4988 "sys_clone", /* 120 */
4989 "sys_setdomainname",
4990 "sys_newuname",
4991 "sys_modify_ldt",
4992 "sys_adjtimex",
4993 "sys_mprotect", /* 125 */
4994 "sys_sigprocmask",
4995 "sys_ni_syscall", /* old "create_module" */
4996 "sys_init_module",
4997 "sys_delete_module",
4998 "sys_ni_syscall", /* 130: old "get_kernel_syms" */
4999 "sys_quotactl",
5000 "sys_getpgid",
5001 "sys_fchdir",
5002 "sys_bdflush",
5003 "sys_sysfs", /* 135 */
5004 "sys_personality",
5005 "sys_ni_syscall", /* reserved for afs_syscall */
5006 "sys_setfsuid16",
5007 "sys_setfsgid16",
5008 "sys_llseek", /* 140 */
5009 "sys_getdents",
5010 "sys_select",
5011 "sys_flock",
5012 "sys_msync",
5013 "sys_readv", /* 145 */
5014 "sys_writev",
5015 "sys_getsid",
5016 "sys_fdatasync",
5017 "sys_sysctl",
5018 "sys_mlock", /* 150 */
5019 "sys_munlock",
5020 "sys_mlockall",
5021 "sys_munlockall",
5022 "sys_sched_setparam",
5023 "sys_sched_getparam", /* 155 */
5024 "sys_sched_setscheduler",
5025 "sys_sched_getscheduler",
5026 "sys_sched_yield",
5027 "sys_sched_get_priority_max",
5028 "sys_sched_get_priority_min", /* 160 */
5029 "sys_sched_rr_get_interval",
5030 "sys_nanosleep",
5031 "sys_mremap",
5032 "sys_setresuid16",
5033 "sys_getresuid16", /* 165 */
5034 "sys_vm86",
5035 "sys_ni_syscall", /* Old sys_query_module */
5036 "sys_poll",
5037 "sys_nfsservctl",
5038 "sys_setresgid16", /* 170 */
5039 "sys_getresgid16",
5040 "sys_prctl",
5041 "sys_rt_sigreturn",
5042 "sys_rt_sigaction",
5043 "sys_rt_sigprocmask", /* 175 */
5044 "sys_rt_sigpending",
5045 "sys_rt_sigtimedwait",
5046 "sys_rt_sigqueueinfo",
5047 "sys_rt_sigsuspend",
5048 "sys_pread64", /* 180 */
5049 "sys_pwrite64",
5050 "sys_chown16",
5051 "sys_getcwd",
5052 "sys_capget",
5053 "sys_capset", /* 185 */
5054 "sys_sigaltstack",
5055 "sys_sendfile",
5056 "sys_ni_syscall", /* reserved for streams1 */
5057 "sys_ni_syscall", /* reserved for streams2 */
5058 "sys_vfork", /* 190 */
5059 "sys_getrlimit",
5060 "sys_mmap2",
5061 "sys_truncate64",
5062 "sys_ftruncate64",
5063 "sys_stat64", /* 195 */
5064 "sys_lstat64",
5065 "sys_fstat64",
5066 "sys_lchown",
5067 "sys_getuid",
5068 "sys_getgid", /* 200 */
5069 "sys_geteuid",
5070 "sys_getegid",
5071 "sys_setreuid",
5072 "sys_setregid",
5073 "sys_getgroups", /* 205 */
5074 "sys_setgroups",
5075 "sys_fchown",
5076 "sys_setresuid",
5077 "sys_getresuid",
5078 "sys_setresgid", /* 210 */
5079 "sys_getresgid",
5080 "sys_chown",
5081 "sys_setuid",
5082 "sys_setgid",
5083 "sys_setfsuid", /* 215 */
5084 "sys_setfsgid",
5085 "sys_pivot_root",
5086 "sys_mincore",
5087 "sys_madvise",
5088 "sys_getdents64", /* 220 */
5089 "sys_fcntl64",
5090 "sys_ni_syscall", /* reserved for TUX */
5091 "sys_ni_syscall",
5092 "sys_gettid",
5093 "sys_readahead", /* 225 */
5094 "sys_setxattr",
5095 "sys_lsetxattr",
5096 "sys_fsetxattr",
5097 "sys_getxattr",
5098 "sys_lgetxattr", /* 230 */
5099 "sys_fgetxattr",
5100 "sys_listxattr",
5101 "sys_llistxattr",
5102 "sys_flistxattr",
5103 "sys_removexattr", /* 235 */
5104 "sys_lremovexattr",
5105 "sys_fremovexattr",
5106 "sys_tkill",
5107 "sys_sendfile64",
5108 "sys_futex", /* 240 */
5109 "sys_sched_setaffinity",
5110 "sys_sched_getaffinity",
5111 "sys_set_thread_area",
5112 "sys_get_thread_area",
5113 "sys_io_setup", /* 245 */
5114 "sys_io_destroy",
5115 "sys_io_getevents",
5116 "sys_io_submit",
5117 "sys_io_cancel",
5118 "sys_fadvise64", /* 250 */
5119 "sys_ni_syscall",
5120 "sys_exit_group",
5121 "sys_lookup_dcookie",
5122 "sys_epoll_create",
5123 "sys_epoll_ctl", /* 255 */
5124 "sys_epoll_wait",
5125 "sys_remap_file_pages",
5126 "sys_set_tid_address",
5127 "sys_timer_create",
5128 "sys_timer_settime", /* 260 */
5129 "sys_timer_gettime",
5130 "sys_timer_getoverrun",
5131 "sys_timer_delete",
5132 "sys_clock_settime",
5133 "sys_clock_gettime", /* 265 */
5134 "sys_clock_getres",
5135 "sys_clock_nanosleep",
5136 "sys_statfs64",
5137 "sys_fstatfs64",
5138 "sys_tgkill", /* 270 */
5139 "sys_utimes",
5140 "sys_fadvise64_64",
5141 "sys_ni_syscall" /* sys_vserver */
5142 };
5143
5144 uint32_t uEAX = CPUMGetGuestEAX(pVCpu);
5145 switch (uEAX)
5146 {
5147 default:
5148 if (uEAX < RT_ELEMENTS(apsz))
5149 Log(("REM: linux syscall %3d: %s (eip=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x ebp=%08x)\n",
5150 uEAX, apsz[uEAX], CPUMGetGuestEIP(pVCpu), CPUMGetGuestEBX(pVCpu), CPUMGetGuestECX(pVCpu),
5151 CPUMGetGuestEDX(pVCpu), CPUMGetGuestESI(pVCpu), CPUMGetGuestEDI(pVCpu), CPUMGetGuestEBP(pVCpu)));
5152 else
5153 Log(("eip=%08x: linux syscall %d (#%x) unknown\n", CPUMGetGuestEIP(pVCpu), uEAX, uEAX));
5154 break;
5155
5156 }
5157}
5158
5159
5160/**
5161 * Dumps an OpenBSD system call.
5162 * @param pVCpu VMCPU handle.
5163 */
5164void remR3DumpOBsdSyscall(PVMCPU pVCpu)
5165{
5166 static const char *apsz[] =
5167 {
5168 "SYS_syscall", //0
5169 "SYS_exit", //1
5170 "SYS_fork", //2
5171 "SYS_read", //3
5172 "SYS_write", //4
5173 "SYS_open", //5
5174 "SYS_close", //6
5175 "SYS_wait4", //7
5176 "SYS_8",
5177 "SYS_link", //9
5178 "SYS_unlink", //10
5179 "SYS_11",
5180 "SYS_chdir", //12
5181 "SYS_fchdir", //13
5182 "SYS_mknod", //14
5183 "SYS_chmod", //15
5184 "SYS_chown", //16
5185 "SYS_break", //17
5186 "SYS_18",
5187 "SYS_19",
5188 "SYS_getpid", //20
5189 "SYS_mount", //21
5190 "SYS_unmount", //22
5191 "SYS_setuid", //23
5192 "SYS_getuid", //24
5193 "SYS_geteuid", //25
5194 "SYS_ptrace", //26
5195 "SYS_recvmsg", //27
5196 "SYS_sendmsg", //28
5197 "SYS_recvfrom", //29
5198 "SYS_accept", //30
5199 "SYS_getpeername", //31
5200 "SYS_getsockname", //32
5201 "SYS_access", //33
5202 "SYS_chflags", //34
5203 "SYS_fchflags", //35
5204 "SYS_sync", //36
5205 "SYS_kill", //37
5206 "SYS_38",
5207 "SYS_getppid", //39
5208 "SYS_40",
5209 "SYS_dup", //41
5210 "SYS_opipe", //42
5211 "SYS_getegid", //43
5212 "SYS_profil", //44
5213 "SYS_ktrace", //45
5214 "SYS_sigaction", //46
5215 "SYS_getgid", //47
5216 "SYS_sigprocmask", //48
5217 "SYS_getlogin", //49
5218 "SYS_setlogin", //50
5219 "SYS_acct", //51
5220 "SYS_sigpending", //52
5221 "SYS_osigaltstack", //53
5222 "SYS_ioctl", //54
5223 "SYS_reboot", //55
5224 "SYS_revoke", //56
5225 "SYS_symlink", //57
5226 "SYS_readlink", //58
5227 "SYS_execve", //59
5228 "SYS_umask", //60
5229 "SYS_chroot", //61
5230 "SYS_62",
5231 "SYS_63",
5232 "SYS_64",
5233 "SYS_65",
5234 "SYS_vfork", //66
5235 "SYS_67",
5236 "SYS_68",
5237 "SYS_sbrk", //69
5238 "SYS_sstk", //70
5239 "SYS_61",
5240 "SYS_vadvise", //72
5241 "SYS_munmap", //73
5242 "SYS_mprotect", //74
5243 "SYS_madvise", //75
5244 "SYS_76",
5245 "SYS_77",
5246 "SYS_mincore", //78
5247 "SYS_getgroups", //79
5248 "SYS_setgroups", //80
5249 "SYS_getpgrp", //81
5250 "SYS_setpgid", //82
5251 "SYS_setitimer", //83
5252 "SYS_84",
5253 "SYS_85",
5254 "SYS_getitimer", //86
5255 "SYS_87",
5256 "SYS_88",
5257 "SYS_89",
5258 "SYS_dup2", //90
5259 "SYS_91",
5260 "SYS_fcntl", //92
5261 "SYS_select", //93
5262 "SYS_94",
5263 "SYS_fsync", //95
5264 "SYS_setpriority", //96
5265 "SYS_socket", //97
5266 "SYS_connect", //98
5267 "SYS_99",
5268 "SYS_getpriority", //100
5269 "SYS_101",
5270 "SYS_102",
5271 "SYS_sigreturn", //103
5272 "SYS_bind", //104
5273 "SYS_setsockopt", //105
5274 "SYS_listen", //106
5275 "SYS_107",
5276 "SYS_108",
5277 "SYS_109",
5278 "SYS_110",
5279 "SYS_sigsuspend", //111
5280 "SYS_112",
5281 "SYS_113",
5282 "SYS_114",
5283 "SYS_115",
5284 "SYS_gettimeofday", //116
5285 "SYS_getrusage", //117
5286 "SYS_getsockopt", //118
5287 "SYS_119",
5288 "SYS_readv", //120
5289 "SYS_writev", //121
5290 "SYS_settimeofday", //122
5291 "SYS_fchown", //123
5292 "SYS_fchmod", //124
5293 "SYS_125",
5294 "SYS_setreuid", //126
5295 "SYS_setregid", //127
5296 "SYS_rename", //128
5297 "SYS_129",
5298 "SYS_130",
5299 "SYS_flock", //131
5300 "SYS_mkfifo", //132
5301 "SYS_sendto", //133
5302 "SYS_shutdown", //134
5303 "SYS_socketpair", //135
5304 "SYS_mkdir", //136
5305 "SYS_rmdir", //137
5306 "SYS_utimes", //138
5307 "SYS_139",
5308 "SYS_adjtime", //140
5309 "SYS_141",
5310 "SYS_142",
5311 "SYS_143",
5312 "SYS_144",
5313 "SYS_145",
5314 "SYS_146",
5315 "SYS_setsid", //147
5316 "SYS_quotactl", //148
5317 "SYS_149",
5318 "SYS_150",
5319 "SYS_151",
5320 "SYS_152",
5321 "SYS_153",
5322 "SYS_154",
5323 "SYS_nfssvc", //155
5324 "SYS_156",
5325 "SYS_157",
5326 "SYS_158",
5327 "SYS_159",
5328 "SYS_160",
5329 "SYS_getfh", //161
5330 "SYS_162",
5331 "SYS_163",
5332 "SYS_164",
5333 "SYS_sysarch", //165
5334 "SYS_166",
5335 "SYS_167",
5336 "SYS_168",
5337 "SYS_169",
5338 "SYS_170",
5339 "SYS_171",
5340 "SYS_172",
5341 "SYS_pread", //173
5342 "SYS_pwrite", //174
5343 "SYS_175",
5344 "SYS_176",
5345 "SYS_177",
5346 "SYS_178",
5347 "SYS_179",
5348 "SYS_180",
5349 "SYS_setgid", //181
5350 "SYS_setegid", //182
5351 "SYS_seteuid", //183
5352 "SYS_lfs_bmapv", //184
5353 "SYS_lfs_markv", //185
5354 "SYS_lfs_segclean", //186
5355 "SYS_lfs_segwait", //187
5356 "SYS_188",
5357 "SYS_189",
5358 "SYS_190",
5359 "SYS_pathconf", //191
5360 "SYS_fpathconf", //192
5361 "SYS_swapctl", //193
5362 "SYS_getrlimit", //194
5363 "SYS_setrlimit", //195
5364 "SYS_getdirentries", //196
5365 "SYS_mmap", //197
5366 "SYS___syscall", //198
5367 "SYS_lseek", //199
5368 "SYS_truncate", //200
5369 "SYS_ftruncate", //201
5370 "SYS___sysctl", //202
5371 "SYS_mlock", //203
5372 "SYS_munlock", //204
5373 "SYS_205",
5374 "SYS_futimes", //206
5375 "SYS_getpgid", //207
5376 "SYS_xfspioctl", //208
5377 "SYS_209",
5378 "SYS_210",
5379 "SYS_211",
5380 "SYS_212",
5381 "SYS_213",
5382 "SYS_214",
5383 "SYS_215",
5384 "SYS_216",
5385 "SYS_217",
5386 "SYS_218",
5387 "SYS_219",
5388 "SYS_220",
5389 "SYS_semget", //221
5390 "SYS_222",
5391 "SYS_223",
5392 "SYS_224",
5393 "SYS_msgget", //225
5394 "SYS_msgsnd", //226
5395 "SYS_msgrcv", //227
5396 "SYS_shmat", //228
5397 "SYS_229",
5398 "SYS_shmdt", //230
5399 "SYS_231",
5400 "SYS_clock_gettime", //232
5401 "SYS_clock_settime", //233
5402 "SYS_clock_getres", //234
5403 "SYS_235",
5404 "SYS_236",
5405 "SYS_237",
5406 "SYS_238",
5407 "SYS_239",
5408 "SYS_nanosleep", //240
5409 "SYS_241",
5410 "SYS_242",
5411 "SYS_243",
5412 "SYS_244",
5413 "SYS_245",
5414 "SYS_246",
5415 "SYS_247",
5416 "SYS_248",
5417 "SYS_249",
5418 "SYS_minherit", //250
5419 "SYS_rfork", //251
5420 "SYS_poll", //252
5421 "SYS_issetugid", //253
5422 "SYS_lchown", //254
5423 "SYS_getsid", //255
5424 "SYS_msync", //256
5425 "SYS_257",
5426 "SYS_258",
5427 "SYS_259",
5428 "SYS_getfsstat", //260
5429 "SYS_statfs", //261
5430 "SYS_fstatfs", //262
5431 "SYS_pipe", //263
5432 "SYS_fhopen", //264
5433 "SYS_265",
5434 "SYS_fhstatfs", //266
5435 "SYS_preadv", //267
5436 "SYS_pwritev", //268
5437 "SYS_kqueue", //269
5438 "SYS_kevent", //270
5439 "SYS_mlockall", //271
5440 "SYS_munlockall", //272
5441 "SYS_getpeereid", //273
5442 "SYS_274",
5443 "SYS_275",
5444 "SYS_276",
5445 "SYS_277",
5446 "SYS_278",
5447 "SYS_279",
5448 "SYS_280",
5449 "SYS_getresuid", //281
5450 "SYS_setresuid", //282
5451 "SYS_getresgid", //283
5452 "SYS_setresgid", //284
5453 "SYS_285",
5454 "SYS_mquery", //286
5455 "SYS_closefrom", //287
5456 "SYS_sigaltstack", //288
5457 "SYS_shmget", //289
5458 "SYS_semop", //290
5459 "SYS_stat", //291
5460 "SYS_fstat", //292
5461 "SYS_lstat", //293
5462 "SYS_fhstat", //294
5463 "SYS___semctl", //295
5464 "SYS_shmctl", //296
5465 "SYS_msgctl", //297
5466 "SYS_MAXSYSCALL", //298
5467 //299
5468 //300
5469 };
5470 uint32_t uEAX;
5471 if (!LogIsEnabled())
5472 return;
5473 uEAX = CPUMGetGuestEAX(pVCpu);
5474 switch (uEAX)
5475 {
5476 default:
5477 if (uEAX < RT_ELEMENTS(apsz))
5478 {
5479 uint32_t au32Args[8] = {0};
5480 PGMPhysSimpleReadGCPtr(pVCpu, au32Args, CPUMGetGuestESP(pVCpu), sizeof(au32Args));
5481 RTLogPrintf("REM: OpenBSD syscall %3d: %s (eip=%08x %08x %08x %08x %08x %08x %08x %08x %08x)\n",
5482 uEAX, apsz[uEAX], CPUMGetGuestEIP(pVCpu), au32Args[0], au32Args[1], au32Args[2], au32Args[3],
5483 au32Args[4], au32Args[5], au32Args[6], au32Args[7]);
5484 }
5485 else
5486 RTLogPrintf("eip=%08x: OpenBSD syscall %d (#%x) unknown!!\n", CPUMGetGuestEIP(pVCpu), uEAX, uEAX);
5487 break;
5488 }
5489}
5490
5491
5492#if defined(IPRT_NO_CRT) && defined(RT_OS_WINDOWS) && defined(RT_ARCH_X86)
5493/**
5494 * The Dll main entry point (stub).
5495 */
5496bool __stdcall _DllMainCRTStartup(void *hModule, uint32_t dwReason, void *pvReserved)
5497{
5498 return true;
5499}
5500
5501void *memcpy(void *dst, const void *src, size_t size)
5502{
5503 uint8_t*pbDst = dst, *pbSrc = src;
5504 while (size-- > 0)
5505 *pbDst++ = *pbSrc++;
5506 return dst;
5507}
5508
5509#endif
5510
5511void cpu_smm_update(CPUX86State *env)
5512{
5513}
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