VirtualBox

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

Last change on this file since 4379 was 4379, checked in by vboxsync, 17 years ago

Remove assertion for now

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