VirtualBox

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

Last change on this file since 46852 was 46493, checked in by vboxsync, 12 years ago

STAMR3Deregister* optimizations. Relevant for both startup and shutdown times.

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