VirtualBox

source: vbox/trunk/src/recompiler/new/VBoxRecompiler.c@ 841

Last change on this file since 841 was 841, checked in by vboxsync, 18 years ago

PGMPhysGCPhys2HCPtr fix.

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