VirtualBox

source: vbox/trunk/src/recompiler_new/VBoxRecompiler.c@ 15034

Last change on this file since 15034 was 15015, checked in by vboxsync, 16 years ago

Unused variable

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