VirtualBox

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

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

Removed obsolete and incorrect remR3SetPage.

  • 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 4376 2007-08-27 08:25:07Z 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 LogFlow(("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
2507 cpu_register_physical_memory(GCPhys, cb, GCPhys | (fFlags & MM_RAM_FLAGS_RESERVED ? IO_MEM_UNASSIGNED : 0));
2508
2509# ifndef REM_PHYS_ADDR_IN_TLB
2510 AssertRelease(pVM->rem.s.cPhysRegistrations < REM_MAX_PHYS_REGISTRATIONS);
2511 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2512 {
2513 if (pVM->rem.s.aPhysReg[i].GCPhys == GCPhys)
2514 {
2515 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvRam;
2516 pVM->rem.s.aPhysReg[i].cb = cb;
2517 break;
2518 }
2519 }
2520 if (i == pVM->rem.s.cPhysRegistrations)
2521 {
2522 pVM->rem.s.aPhysReg[i].GCPhys = GCPhys;
2523 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvRam;
2524 pVM->rem.s.aPhysReg[i].cb = cb;
2525 pVM->rem.s.cPhysRegistrations++;
2526 }
2527# endif /* !REM_PHYS_ADDR_IN_TLB */
2528 }
2529#elif defined(REM_PHYS_ADDR_IN_TLB)
2530 cpu_register_physical_memory(GCPhys, cb, GCPhys | (fFlags & MM_RAM_FLAGS_RESERVED ? IO_MEM_UNASSIGNED : 0));
2531#else
2532 AssertRelease(phys_ram_base);
2533 cpu_register_physical_memory(GCPhys, cb, ((uintptr_t)pvRam - (uintptr_t)phys_ram_base)
2534 | (fFlags & MM_RAM_FLAGS_RESERVED ? IO_MEM_UNASSIGNED : 0));
2535#endif
2536 Assert(pVM->rem.s.fIgnoreAll);
2537 pVM->rem.s.fIgnoreAll = false;
2538}
2539
2540
2541/**
2542 * Notification about a successful PGMR3PhysRegisterChunk() call.
2543 *
2544 * @param pVM VM handle.
2545 * @param GCPhys The physical address the RAM.
2546 * @param cb Size of the memory.
2547 * @param pvRam The HC address of the RAM.
2548 * @param fFlags Flags of the MM_RAM_FLAGS_* defines.
2549 */
2550REMR3DECL(void) REMR3NotifyPhysRamChunkRegister(PVM pVM, RTGCPHYS GCPhys, RTUINT cb, RTHCUINTPTR pvRam, unsigned fFlags)
2551{
2552#ifdef PGM_DYNAMIC_RAM_ALLOC
2553# ifndef REM_PHYS_ADDR_IN_TLB
2554 uint32_t idx;
2555#endif
2556
2557 Log(("REMR3NotifyPhysRamChunkRegister: GCPhys=%VGp cb=%d pvRam=%p fFlags=%d\n", GCPhys, cb, pvRam, fFlags));
2558 VM_ASSERT_EMT(pVM);
2559
2560 /*
2561 * Validate input - we trust the caller.
2562 */
2563 Assert(pvRam);
2564 Assert(RT_ALIGN(pvRam, PAGE_SIZE) == pvRam);
2565 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2566 Assert(cb == PGM_DYNAMIC_CHUNK_SIZE);
2567 Assert(fFlags == 0 /* normal RAM */);
2568
2569# ifndef REM_PHYS_ADDR_IN_TLB
2570 if (!pVM->rem.s.paHCVirtToGCPhys)
2571 {
2572 uint32_t size = (_4G >> PGM_DYNAMIC_CHUNK_SHIFT) * sizeof(REMCHUNKINFO);
2573
2574 Assert(phys_ram_size);
2575
2576 pVM->rem.s.paHCVirtToGCPhys = (PREMCHUNKINFO)MMR3HeapAllocZ(pVM, MM_TAG_REM, size);
2577 pVM->rem.s.paGCPhysToHCVirt = (RTHCPTR)MMR3HeapAllocZ(pVM, MM_TAG_REM, (phys_ram_size >> PGM_DYNAMIC_CHUNK_SHIFT)*sizeof(RTHCPTR));
2578 }
2579 pVM->rem.s.paGCPhysToHCVirt[GCPhys >> PGM_DYNAMIC_CHUNK_SHIFT] = pvRam;
2580
2581 idx = (pvRam >> PGM_DYNAMIC_CHUNK_SHIFT);
2582 if (!pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1)
2583 {
2584 pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1 = pvRam;
2585 pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys1 = GCPhys;
2586 }
2587 else
2588 {
2589 Assert(!pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2);
2590 pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2 = pvRam;
2591 pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys2 = GCPhys;
2592 }
2593 /* Does the region spawn two chunks? */
2594 if (pvRam & PGM_DYNAMIC_CHUNK_OFFSET_MASK)
2595 {
2596 if (!pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk1)
2597 {
2598 pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk1 = pvRam;
2599 pVM->rem.s.paHCVirtToGCPhys[idx+1].GCPhys1 = GCPhys;
2600 }
2601 else
2602 {
2603 Assert(!pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk2);
2604 pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk2 = pvRam;
2605 pVM->rem.s.paHCVirtToGCPhys[idx+1].GCPhys2 = GCPhys;
2606 }
2607 }
2608# endif /* !REM_PHYS_ADDR_IN_TLB */
2609
2610 Assert(!pVM->rem.s.fIgnoreAll);
2611 pVM->rem.s.fIgnoreAll = true;
2612
2613 cpu_register_physical_memory(GCPhys, cb, GCPhys);
2614
2615 Assert(pVM->rem.s.fIgnoreAll);
2616 pVM->rem.s.fIgnoreAll = false;
2617
2618#else
2619 AssertReleaseFailed();
2620#endif
2621}
2622
2623
2624#ifdef PGM_DYNAMIC_RAM_ALLOC
2625# ifndef REM_PHYS_ADDR_IN_TLB
2626#if 0
2627static const uint8_t gabZeroPage[PAGE_SIZE];
2628#endif
2629
2630/**
2631 * Convert GC physical address to HC virt
2632 *
2633 * @returns The HC virt address corresponding to addr.
2634 * @param env The cpu environment.
2635 * @param addr The physical address.
2636 */
2637DECLINLINE(void *) remR3GCPhys2HCVirtInlined(PVM pVM, target_ulong addr)
2638{
2639 uint32_t i;
2640 void *pv;
2641 STAM_PROFILE_START(&gStatGCPhys2HCVirt, a);
2642
2643#if 1
2644 /* lookup in pVM->rem.s.aPhysReg array first (for ROM range(s) inside the guest's RAM) */
2645 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2646 {
2647 RTGCPHYS off = addr - pVM->rem.s.aPhysReg[i].GCPhys;
2648 if (off < pVM->rem.s.aPhysReg[i].cb)
2649 {
2650 pv = (void *)(pVM->rem.s.aPhysReg[i].HCVirt + off);
2651 Log2(("remR3GCPhys2HCVirt: %x -> %x\n", addr, pv));
2652 STAM_PROFILE_STOP(&gStatGCPhys2HCVirt, a);
2653 return pv;
2654 }
2655 }
2656 AssertMsg(addr < phys_ram_size, ("remR3GCPhys2HCVirt: unknown physical address %x\n", addr));
2657 pv = (void *)(pVM->rem.s.paGCPhysToHCVirt[addr >> PGM_DYNAMIC_CHUNK_SHIFT] + (addr & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
2658 Log2(("remR3GCPhys2HCVirt: %x -> %x\n", addr, pv));
2659#else
2660 /** @todo figure out why this is faster than the above code. */
2661 int rc = PGMPhysGCPhys2HCPtr(pVM, addr & X86_PTE_PAE_PG_MASK, PAGE_SIZE, &pv);
2662 if (RT_FAILURE(rc))
2663 {
2664 AssertMsgFailed(("remR3GCPhys2HCVirt: unknown physical address %x\n", addr));
2665 pv = gabZeroPage;
2666 }
2667 pv = (void *)((uintptr_t)pv | (addr & PAGE_OFFSET_MASK));
2668#endif
2669 return pv;
2670}
2671
2672
2673/**
2674 * Convert GC physical address to HC virt
2675 *
2676 * @returns The HC virt address corresponding to addr.
2677 * @param env The cpu environment.
2678 * @param addr The physical address.
2679 */
2680DECLINLINE(target_ulong) remR3HCVirt2GCPhysInlined(PVM pVM, void *addr)
2681{
2682 RTHCUINTPTR HCVirt = (RTHCUINTPTR)addr;
2683 uint32_t idx = (HCVirt >> PGM_DYNAMIC_CHUNK_SHIFT);
2684 RTHCUINTPTR off;
2685 RTUINT i;
2686 target_ulong GCPhys;
2687
2688 off = HCVirt - pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1;
2689
2690 if ( pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1
2691 && off < PGM_DYNAMIC_CHUNK_SIZE)
2692 {
2693 GCPhys = pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys1 + off;
2694 Log2(("remR3HCVirt2GCPhys %x -> %x\n", addr, GCPhys));
2695 return GCPhys;
2696 }
2697
2698 off = HCVirt - pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2;
2699 if ( pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2
2700 && off < PGM_DYNAMIC_CHUNK_SIZE)
2701 {
2702 GCPhys = pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys2 + off;
2703 Log2(("remR3HCVirt2GCPhys %x -> %x\n", addr, GCPhys));
2704 return GCPhys;
2705 }
2706
2707 /* Must be externally registered RAM/ROM range */
2708 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2709 {
2710 uint32_t off = HCVirt - pVM->rem.s.aPhysReg[i].HCVirt;
2711 if (off < pVM->rem.s.aPhysReg[i].cb)
2712 {
2713 GCPhys = pVM->rem.s.aPhysReg[i].GCPhys + off;
2714 Log2(("remR3HCVirt2GCPhys %x -> %x\n", addr, GCPhys));
2715 return GCPhys;
2716 }
2717 }
2718 AssertReleaseMsgFailed(("No translation for physical address %VHv???\n", addr));
2719 return 0;
2720}
2721
2722/**
2723 * Convert GC physical address to HC virt
2724 *
2725 * @returns The HC virt address corresponding to addr.
2726 * @param env The cpu environment.
2727 * @param addr The physical address.
2728 */
2729void *remR3GCPhys2HCVirt(void *env, target_ulong addr)
2730{
2731 PVM pVM = ((CPUState *)env)->pVM;
2732 void *pv;
2733 STAM_PROFILE_START(&gStatGCPhys2HCVirt, a);
2734 pv = remR3GCPhys2HCVirtInlined(pVM, addr);
2735 STAM_PROFILE_STOP(&gStatGCPhys2HCVirt, a);
2736 return pv;
2737}
2738
2739
2740/**
2741 * Convert GC physical address to HC virt
2742 *
2743 * @returns The HC virt address corresponding to addr.
2744 * @param env The cpu environment.
2745 * @param addr The physical address.
2746 */
2747target_ulong remR3HCVirt2GCPhys(void *env, void *addr)
2748{
2749 PVM pVM = ((CPUState *)env)->pVM;
2750 target_ulong GCPhys;
2751 STAM_PROFILE_START(&gStatHCVirt2GCPhys, a);
2752 GCPhys = remR3HCVirt2GCPhysInlined(pVM, addr);
2753 STAM_PROFILE_STOP(&gStatHCVirt2GCPhys, a);
2754 return GCPhys;
2755}
2756
2757# endif /* !REM_PHYS_ADDR_IN_TLB */
2758
2759/**
2760 * Grows dynamically allocated guest RAM.
2761 * Will raise a fatal error if the operation fails.
2762 *
2763 * @param physaddr The physical address.
2764 */
2765void remR3GrowDynRange(unsigned long physaddr)
2766{
2767 int rc;
2768 PVM pVM = cpu_single_env->pVM;
2769
2770 Log(("remR3GrowDynRange %VGp\n", physaddr));
2771 rc = PGM3PhysGrowRange(pVM, (RTGCPHYS)physaddr);
2772 if (VBOX_SUCCESS(rc))
2773 return;
2774
2775 LogRel(("\nUnable to allocate guest RAM chunk at %VGp\n", physaddr));
2776 cpu_abort(cpu_single_env, "Unable to allocate guest RAM chunk at %VGp\n", physaddr);
2777 AssertFatalFailed();
2778}
2779
2780#endif /* PGM_DYNAMIC_RAM_ALLOC */
2781
2782
2783/**
2784 * Notification about a successful MMR3PhysRomRegister() call.
2785 *
2786 * @param pVM VM handle.
2787 * @param GCPhys The physical address of the ROM.
2788 * @param cb The size of the ROM.
2789 * @param pvCopy Pointer to the ROM copy.
2790 */
2791REMR3DECL(void) REMR3NotifyPhysRomRegister(PVM pVM, RTGCPHYS GCPhys, RTUINT cb, void *pvCopy)
2792{
2793#if defined(PGM_DYNAMIC_RAM_ALLOC) && !defined(REM_PHYS_ADDR_IN_TLB)
2794 uint32_t i;
2795#endif
2796 LogFlow(("REMR3NotifyPhysRomRegister: GCPhys=%VGp cb=%d pvCopy=%p\n", GCPhys, cb, pvCopy));
2797 VM_ASSERT_EMT(pVM);
2798
2799 /*
2800 * Validate input - we trust the caller.
2801 */
2802 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2803 Assert(cb);
2804 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
2805 Assert(pvCopy);
2806 Assert(RT_ALIGN_P(pvCopy, PAGE_SIZE) == pvCopy);
2807
2808 /*
2809 * Register the rom.
2810 */
2811 Assert(!pVM->rem.s.fIgnoreAll);
2812 pVM->rem.s.fIgnoreAll = true;
2813
2814#ifdef REM_PHYS_ADDR_IN_TLB
2815 cpu_register_physical_memory(GCPhys, cb, GCPhys | IO_MEM_ROM);
2816#elif defined(PGM_DYNAMIC_RAM_ALLOC)
2817 cpu_register_physical_memory(GCPhys, cb, GCPhys | IO_MEM_ROM);
2818 AssertRelease(pVM->rem.s.cPhysRegistrations < REM_MAX_PHYS_REGISTRATIONS);
2819 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2820 {
2821 if (pVM->rem.s.aPhysReg[i].GCPhys == GCPhys)
2822 {
2823 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvCopy;
2824 pVM->rem.s.aPhysReg[i].cb = cb;
2825 break;
2826 }
2827 }
2828 if (i == pVM->rem.s.cPhysRegistrations)
2829 {
2830 pVM->rem.s.aPhysReg[i].GCPhys = GCPhys;
2831 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvCopy;
2832 pVM->rem.s.aPhysReg[i].cb = cb;
2833 pVM->rem.s.cPhysRegistrations++;
2834 }
2835#else
2836 AssertRelease(phys_ram_base);
2837 cpu_register_physical_memory(GCPhys, cb, ((uintptr_t)pvCopy - (uintptr_t)phys_ram_base) | IO_MEM_ROM);
2838#endif
2839
2840 Log2(("%.64Vhxd\n", (char *)pvCopy + cb - 64));
2841
2842 Assert(pVM->rem.s.fIgnoreAll);
2843 pVM->rem.s.fIgnoreAll = false;
2844}
2845
2846
2847/**
2848 * Notification about a successful MMR3PhysRegister() call.
2849 *
2850 * @param pVM VM Handle.
2851 * @param GCPhys Start physical address.
2852 * @param cb The size of the range.
2853 */
2854REMR3DECL(void) REMR3NotifyPhysReserve(PVM pVM, RTGCPHYS GCPhys, RTUINT cb)
2855{
2856 LogFlow(("REMR3NotifyPhysReserve: GCPhys=%VGp cb=%d\n", GCPhys, cb));
2857 VM_ASSERT_EMT(pVM);
2858
2859 /*
2860 * Validate input - we trust the caller.
2861 */
2862 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2863 Assert(cb);
2864 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
2865
2866 /*
2867 * Unassigning the memory.
2868 */
2869 Assert(!pVM->rem.s.fIgnoreAll);
2870 pVM->rem.s.fIgnoreAll = true;
2871
2872 cpu_register_physical_memory(GCPhys, cb, IO_MEM_UNASSIGNED);
2873
2874 Assert(pVM->rem.s.fIgnoreAll);
2875 pVM->rem.s.fIgnoreAll = false;
2876}
2877
2878
2879/**
2880 * Notification about a successful PGMR3HandlerPhysicalRegister() call.
2881 *
2882 * @param pVM VM Handle.
2883 * @param enmType Handler type.
2884 * @param GCPhys Handler range address.
2885 * @param cb Size of the handler range.
2886 * @param fHasHCHandler Set if the handler has a HC callback function.
2887 *
2888 * @remark MMR3PhysRomRegister assumes that this function will not apply the
2889 * Handler memory type to memory which has no HC handler.
2890 */
2891REMR3DECL(void) REMR3NotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler)
2892{
2893 LogFlow(("REMR3NotifyHandlerPhysicalRegister: enmType=%d GCPhys=%VGp cb=%d fHasHCHandler=%d\n",
2894 enmType, GCPhys, cb, fHasHCHandler));
2895 VM_ASSERT_EMT(pVM);
2896 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2897 Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
2898
2899 if (pVM->rem.s.cHandlerNotifications)
2900 REMR3ReplayHandlerNotifications(pVM);
2901
2902 Assert(!pVM->rem.s.fIgnoreAll);
2903 pVM->rem.s.fIgnoreAll = true;
2904
2905 if (enmType == PGMPHYSHANDLERTYPE_MMIO)
2906 cpu_register_physical_memory(GCPhys, cb, pVM->rem.s.iMMIOMemType);
2907 else if (fHasHCHandler)
2908 cpu_register_physical_memory(GCPhys, cb, pVM->rem.s.iHandlerMemType);
2909
2910 Assert(pVM->rem.s.fIgnoreAll);
2911 pVM->rem.s.fIgnoreAll = false;
2912}
2913
2914
2915/**
2916 * Notification about a successful PGMR3HandlerPhysicalDeregister() operation.
2917 *
2918 * @param pVM VM Handle.
2919 * @param enmType Handler type.
2920 * @param GCPhys Handler range address.
2921 * @param cb Size of the handler range.
2922 * @param fHasHCHandler Set if the handler has a HC callback function.
2923 * @param pvHCPtr The HC virtual address corresponding to GCPhys if available.
2924 */
2925REMR3DECL(void) REMR3NotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler, void *pvHCPtr)
2926{
2927 LogFlow(("REMR3NotifyHandlerPhysicalDeregister: enmType=%d GCPhys=%VGp cb=%d fHasHCHandler=%d pvHCPtr=%p RAM=%08x\n",
2928 enmType, GCPhys, cb, fHasHCHandler, pvHCPtr, MMR3PhysGetRamSize(pVM)));
2929 VM_ASSERT_EMT(pVM);
2930
2931 if (pVM->rem.s.cHandlerNotifications)
2932 REMR3ReplayHandlerNotifications(pVM);
2933
2934 Assert(!pVM->rem.s.fIgnoreAll);
2935 pVM->rem.s.fIgnoreAll = true;
2936
2937 if (enmType == PGMPHYSHANDLERTYPE_MMIO)
2938 cpu_register_physical_memory(GCPhys, cb, IO_MEM_UNASSIGNED);
2939 else if (fHasHCHandler)
2940 {
2941 if (!pvHCPtr)
2942 {
2943 Assert(GCPhys > MMR3PhysGetRamSize(pVM));
2944 cpu_register_physical_memory(GCPhys, cb, IO_MEM_UNASSIGNED);
2945 }
2946 else
2947 {
2948 /* This is not perfect, but it'll do for PD monitoring... */
2949 Assert(cb == PAGE_SIZE);
2950 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2951#ifdef REM_PHYS_ADDR_IN_TLB
2952 cpu_register_physical_memory(GCPhys, cb, GCPhys);
2953#elif defined(PGM_DYNAMIC_RAM_ALLOC)
2954 Assert(remR3HCVirt2GCPhysInlined(pVM, pvHCPtr) < MMR3PhysGetRamSize(pVM));
2955 cpu_register_physical_memory(GCPhys, cb, GCPhys);
2956#else
2957 Assert((uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base < MMR3PhysGetRamSize(pVM));
2958 cpu_register_physical_memory(GCPhys, cb, (uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base);
2959#endif
2960 }
2961 }
2962
2963 Assert(pVM->rem.s.fIgnoreAll);
2964 pVM->rem.s.fIgnoreAll = false;
2965}
2966
2967
2968/**
2969 * Notification about a successful PGMR3HandlerPhysicalModify() call.
2970 *
2971 * @param pVM VM Handle.
2972 * @param enmType Handler type.
2973 * @param GCPhysOld Old handler range address.
2974 * @param GCPhysNew New handler range address.
2975 * @param cb Size of the handler range.
2976 * @param fHasHCHandler Set if the handler has a HC callback function.
2977 * @param pvHCPtr The HC virtual address corresponding to GCPhys if available.
2978 */
2979REMR3DECL(void) REMR3NotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, void *pvHCPtr)
2980{
2981 LogFlow(("REMR3NotifyHandlerPhysicalModify: enmType=%d GCPhysOld=%VGp GCPhysNew=%VGp cb=%d fHasHCHandler=%d pvHCPtr=%p\n",
2982 enmType, GCPhysOld, GCPhysNew, cb, fHasHCHandler, pvHCPtr));
2983 VM_ASSERT_EMT(pVM);
2984 AssertReleaseMsg(enmType != PGMPHYSHANDLERTYPE_MMIO, ("enmType=%d\n", enmType));
2985
2986 if (pVM->rem.s.cHandlerNotifications)
2987 REMR3ReplayHandlerNotifications(pVM);
2988
2989 if (fHasHCHandler)
2990 {
2991 Assert(!pVM->rem.s.fIgnoreAll);
2992 pVM->rem.s.fIgnoreAll = true;
2993
2994 /*
2995 * Reset the old page.
2996 */
2997 if (!pvHCPtr)
2998 cpu_register_physical_memory(GCPhysOld, cb, IO_MEM_UNASSIGNED);
2999 else
3000 {
3001 /* This is not perfect, but it'll do for PD monitoring... */
3002 Assert(cb == PAGE_SIZE);
3003 Assert(RT_ALIGN_T(GCPhysOld, PAGE_SIZE, RTGCPHYS) == GCPhysOld);
3004#ifdef REM_PHYS_ADDR_IN_TLB
3005 cpu_register_physical_memory(GCPhysOld, cb, GCPhysOld);
3006#elif defined(PGM_DYNAMIC_RAM_ALLOC)
3007 Assert(remR3HCVirt2GCPhysInlined(pVM, pvHCPtr) < MMR3PhysGetRamSize(pVM));
3008 cpu_register_physical_memory(GCPhysOld, cb, GCPhysOld);
3009#else
3010 AssertMsg((uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base < MMR3PhysGetRamSize(pVM),
3011 ("pvHCPtr=%p phys_ram_base=%p size=%RX64 cb=%RGp\n", pvHCPtr, phys_ram_base, MMR3PhysGetRamSize(pVM), cb));
3012 cpu_register_physical_memory(GCPhysOld, cb, (uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base);
3013#endif
3014 }
3015
3016 /*
3017 * Update the new page.
3018 */
3019 Assert(RT_ALIGN_T(GCPhysNew, PAGE_SIZE, RTGCPHYS) == GCPhysNew);
3020 Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
3021 cpu_register_physical_memory(GCPhysNew, cb, pVM->rem.s.iHandlerMemType);
3022
3023 Assert(pVM->rem.s.fIgnoreAll);
3024 pVM->rem.s.fIgnoreAll = false;
3025 }
3026}
3027
3028
3029/**
3030 * Checks if we're handling access to this page or not.
3031 *
3032 * @returns true if we're trapping access.
3033 * @returns false if we aren't.
3034 * @param pVM The VM handle.
3035 * @param GCPhys The physical address.
3036 *
3037 * @remark This function will only work correctly in VBOX_STRICT builds!
3038 */
3039REMDECL(bool) REMR3IsPageAccessHandled(PVM pVM, RTGCPHYS GCPhys)
3040{
3041#ifdef VBOX_STRICT
3042 if (pVM->rem.s.cHandlerNotifications)
3043 REMR3ReplayHandlerNotifications(pVM);
3044
3045 unsigned long off = get_phys_page_offset(GCPhys);
3046 return (off & PAGE_OFFSET_MASK) == pVM->rem.s.iHandlerMemType
3047 || (off & PAGE_OFFSET_MASK) == pVM->rem.s.iMMIOMemType
3048 || (off & PAGE_OFFSET_MASK) == IO_MEM_ROM;
3049#else
3050 return false;
3051#endif
3052}
3053
3054
3055/**
3056 * Deals with a rare case in get_phys_addr_code where the code
3057 * is being monitored.
3058 *
3059 * It could also be an MMIO page, in which case we will raise a fatal error.
3060 *
3061 * @returns The physical address corresponding to addr.
3062 * @param env The cpu environment.
3063 * @param addr The virtual address.
3064 * @param pTLBEntry The TLB entry.
3065 */
3066target_ulong remR3PhysGetPhysicalAddressCode(CPUState *env, target_ulong addr, CPUTLBEntry *pTLBEntry)
3067{
3068 PVM pVM = env->pVM;
3069 if ((pTLBEntry->addr_code & ~TARGET_PAGE_MASK) == pVM->rem.s.iHandlerMemType)
3070 {
3071 target_ulong ret = pTLBEntry->addend + addr;
3072 AssertMsg2("remR3PhysGetPhysicalAddressCode: addr=%VGv addr_code=%VGv addend=%VGp ret=%VGp\n",
3073 (RTGCPTR)addr, (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, ret);
3074 return ret;
3075 }
3076 LogRel(("\nTrying to execute code with memory type addr_code=%VGv addend=%VGp at %VGv! (iHandlerMemType=%#x iMMIOMemType=%#x)\n"
3077 "*** handlers\n",
3078 (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, (RTGCPTR)addr, pVM->rem.s.iHandlerMemType, pVM->rem.s.iMMIOMemType));
3079 DBGFR3Info(pVM, "handlers", NULL, DBGFR3InfoLogRelHlp());
3080 LogRel(("*** mmio\n"));
3081 DBGFR3Info(pVM, "mmio", NULL, DBGFR3InfoLogRelHlp());
3082 LogRel(("*** phys\n"));
3083 DBGFR3Info(pVM, "phys", NULL, DBGFR3InfoLogRelHlp());
3084 cpu_abort(env, "Trying to execute code with memory type addr_code=%VGv addend=%VGp at %VGv. (iHandlerMemType=%#x iMMIOMemType=%#x)\n",
3085 (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, (RTGCPTR)addr, pVM->rem.s.iHandlerMemType, pVM->rem.s.iMMIOMemType);
3086 AssertFatalFailed();
3087}
3088
3089
3090/** Validate the physical address passed to the read functions.
3091 * Useful for finding non-guest-ram reads/writes. */
3092#if 1 /* disable if it becomes bothersome... */
3093# define VBOX_CHECK_ADDR(GCPhys) AssertMsg(PGMPhysIsGCPhysValid(cpu_single_env->pVM, (GCPhys)), ("%VGp\n", (GCPhys)))
3094#else
3095# define VBOX_CHECK_ADDR(GCPhys) do { } while (0)
3096#endif
3097
3098/**
3099 * Read guest RAM and ROM.
3100 *
3101 * @param SrcGCPhys The source address (guest physical).
3102 * @param pvDst The destination address.
3103 * @param cb Number of bytes
3104 */
3105void remR3PhysRead(RTGCPHYS SrcGCPhys, void *pvDst, unsigned cb)
3106{
3107 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3108 VBOX_CHECK_ADDR(SrcGCPhys);
3109 PGMPhysRead(cpu_single_env->pVM, SrcGCPhys, pvDst, cb);
3110 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3111}
3112
3113
3114/**
3115 * Read guest RAM and ROM, unsigned 8-bit.
3116 *
3117 * @param SrcGCPhys The source address (guest physical).
3118 */
3119uint8_t remR3PhysReadU8(RTGCPHYS SrcGCPhys)
3120{
3121 uint8_t val;
3122 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3123 VBOX_CHECK_ADDR(SrcGCPhys);
3124 val = PGMR3PhysReadByte(cpu_single_env->pVM, SrcGCPhys);
3125 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3126 return val;
3127}
3128
3129
3130/**
3131 * Read guest RAM and ROM, signed 8-bit.
3132 *
3133 * @param SrcGCPhys The source address (guest physical).
3134 */
3135int8_t remR3PhysReadS8(RTGCPHYS SrcGCPhys)
3136{
3137 int8_t val;
3138 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3139 VBOX_CHECK_ADDR(SrcGCPhys);
3140 val = PGMR3PhysReadByte(cpu_single_env->pVM, SrcGCPhys);
3141 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3142 return val;
3143}
3144
3145
3146/**
3147 * Read guest RAM and ROM, unsigned 16-bit.
3148 *
3149 * @param SrcGCPhys The source address (guest physical).
3150 */
3151uint16_t remR3PhysReadU16(RTGCPHYS SrcGCPhys)
3152{
3153 uint16_t val;
3154 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3155 VBOX_CHECK_ADDR(SrcGCPhys);
3156 val = PGMR3PhysReadWord(cpu_single_env->pVM, SrcGCPhys);
3157 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3158 return val;
3159}
3160
3161
3162/**
3163 * Read guest RAM and ROM, signed 16-bit.
3164 *
3165 * @param SrcGCPhys The source address (guest physical).
3166 */
3167int16_t remR3PhysReadS16(RTGCPHYS SrcGCPhys)
3168{
3169 uint16_t val;
3170 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3171 VBOX_CHECK_ADDR(SrcGCPhys);
3172 val = PGMR3PhysReadWord(cpu_single_env->pVM, SrcGCPhys);
3173 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3174 return val;
3175}
3176
3177
3178/**
3179 * Read guest RAM and ROM, unsigned 32-bit.
3180 *
3181 * @param SrcGCPhys The source address (guest physical).
3182 */
3183uint32_t remR3PhysReadU32(RTGCPHYS SrcGCPhys)
3184{
3185 uint32_t val;
3186 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3187 VBOX_CHECK_ADDR(SrcGCPhys);
3188 val = PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys);
3189 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3190 return val;
3191}
3192
3193
3194/**
3195 * Read guest RAM and ROM, signed 32-bit.
3196 *
3197 * @param SrcGCPhys The source address (guest physical).
3198 */
3199int32_t remR3PhysReadS32(RTGCPHYS SrcGCPhys)
3200{
3201 int32_t val;
3202 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3203 VBOX_CHECK_ADDR(SrcGCPhys);
3204 val = PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys);
3205 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3206 return val;
3207}
3208
3209
3210/**
3211 * Read guest RAM and ROM, unsigned 64-bit.
3212 *
3213 * @param SrcGCPhys The source address (guest physical).
3214 */
3215uint64_t remR3PhysReadU64(RTGCPHYS SrcGCPhys)
3216{
3217 uint64_t val;
3218 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3219 VBOX_CHECK_ADDR(SrcGCPhys);
3220 val = PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys)
3221 | ((uint64_t)PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys + 4) << 32); /** @todo fix me! */
3222 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3223 return val;
3224}
3225
3226
3227/**
3228 * Write guest RAM.
3229 *
3230 * @param DstGCPhys The destination address (guest physical).
3231 * @param pvSrc The source address.
3232 * @param cb Number of bytes to write
3233 */
3234void remR3PhysWrite(RTGCPHYS DstGCPhys, const void *pvSrc, unsigned cb)
3235{
3236 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3237 VBOX_CHECK_ADDR(DstGCPhys);
3238 PGMPhysWrite(cpu_single_env->pVM, DstGCPhys, pvSrc, cb);
3239 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3240}
3241
3242
3243/**
3244 * Write guest RAM, unsigned 8-bit.
3245 *
3246 * @param DstGCPhys The destination address (guest physical).
3247 * @param val Value
3248 */
3249void remR3PhysWriteU8(RTGCPHYS DstGCPhys, uint8_t val)
3250{
3251 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3252 VBOX_CHECK_ADDR(DstGCPhys);
3253 PGMR3PhysWriteByte(cpu_single_env->pVM, DstGCPhys, val);
3254 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3255}
3256
3257
3258/**
3259 * Write guest RAM, unsigned 8-bit.
3260 *
3261 * @param DstGCPhys The destination address (guest physical).
3262 * @param val Value
3263 */
3264void remR3PhysWriteU16(RTGCPHYS DstGCPhys, uint16_t val)
3265{
3266 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3267 VBOX_CHECK_ADDR(DstGCPhys);
3268 PGMR3PhysWriteWord(cpu_single_env->pVM, DstGCPhys, val);
3269 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3270}
3271
3272
3273/**
3274 * Write guest RAM, unsigned 32-bit.
3275 *
3276 * @param DstGCPhys The destination address (guest physical).
3277 * @param val Value
3278 */
3279void remR3PhysWriteU32(RTGCPHYS DstGCPhys, uint32_t val)
3280{
3281 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3282 VBOX_CHECK_ADDR(DstGCPhys);
3283 PGMR3PhysWriteDword(cpu_single_env->pVM, DstGCPhys, val);
3284 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3285}
3286
3287
3288/**
3289 * Write guest RAM, unsigned 64-bit.
3290 *
3291 * @param DstGCPhys The destination address (guest physical).
3292 * @param val Value
3293 */
3294void remR3PhysWriteU64(RTGCPHYS DstGCPhys, uint64_t val)
3295{
3296 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3297 VBOX_CHECK_ADDR(DstGCPhys);
3298 PGMR3PhysWriteDword(cpu_single_env->pVM, DstGCPhys, (uint32_t)val); /** @todo add U64 interface. */
3299 PGMR3PhysWriteDword(cpu_single_env->pVM, DstGCPhys + 4, val >> 32);
3300 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3301}
3302
3303
3304#ifndef REM_PHYS_ADDR_IN_TLB
3305
3306/**
3307 * Read guest RAM and ROM.
3308 *
3309 * @param pbSrcPhys The source address. Relative to guest RAM.
3310 * @param pvDst The destination address.
3311 * @param cb Number of bytes
3312 */
3313void remR3PhysReadHCPtr(uint8_t *pbSrcPhys, void *pvDst, unsigned cb)
3314{
3315 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3316
3317 /*
3318 * Calc the physical address ('off') and check that it's within the RAM.
3319 * ROM is accessed this way, even if it's not part of the RAM.
3320 */
3321#ifdef PGM_DYNAMIC_RAM_ALLOC
3322 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3323#else
3324 uintptr_t off = pbSrcPhys - phys_ram_base;
3325#endif
3326 PGMPhysRead(cpu_single_env->pVM, (RTGCPHYS)off, pvDst, cb);
3327 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3328}
3329
3330
3331/**
3332 * Read guest RAM and ROM, unsigned 8-bit.
3333 *
3334 * @param pbSrcPhys The source address. Relative to guest RAM.
3335 */
3336uint8_t remR3PhysReadHCPtrU8(uint8_t *pbSrcPhys)
3337{
3338 uint8_t val;
3339
3340 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3341
3342 /*
3343 * Calc the physical address ('off') and check that it's within the RAM.
3344 * ROM is accessed this way, even if it's not part of the RAM.
3345 */
3346#ifdef PGM_DYNAMIC_RAM_ALLOC
3347 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3348#else
3349 uintptr_t off = pbSrcPhys - phys_ram_base;
3350#endif
3351 val = PGMR3PhysReadByte(cpu_single_env->pVM, (RTGCPHYS)off);
3352 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3353 return val;
3354}
3355
3356
3357/**
3358 * Read guest RAM and ROM, signed 8-bit.
3359 *
3360 * @param pbSrcPhys The source address. Relative to guest RAM.
3361 */
3362int8_t remR3PhysReadHCPtrS8(uint8_t *pbSrcPhys)
3363{
3364 int8_t val;
3365
3366 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3367
3368 /*
3369 * Calc the physical address ('off') and check that it's within the RAM.
3370 * ROM is accessed this way, even if it's not part of the RAM.
3371 */
3372#ifdef PGM_DYNAMIC_RAM_ALLOC
3373 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3374#else
3375 uintptr_t off = pbSrcPhys - phys_ram_base;
3376#endif
3377 val = PGMR3PhysReadByte(cpu_single_env->pVM, (RTGCPHYS)off);
3378 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3379 return val;
3380}
3381
3382
3383/**
3384 * Read guest RAM and ROM, unsigned 16-bit.
3385 *
3386 * @param pbSrcPhys The source address. Relative to guest RAM.
3387 */
3388uint16_t remR3PhysReadHCPtrU16(uint8_t *pbSrcPhys)
3389{
3390 uint16_t val;
3391
3392 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3393
3394 /*
3395 * Calc the physical address ('off') and check that it's within the RAM.
3396 * ROM is accessed this way, even if it's not part of the RAM.
3397 */
3398#ifdef PGM_DYNAMIC_RAM_ALLOC
3399 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3400#else
3401 uintptr_t off = pbSrcPhys - phys_ram_base;
3402#endif
3403 val = PGMR3PhysReadWord(cpu_single_env->pVM, (RTGCPHYS)off);
3404 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3405 return val;
3406}
3407
3408
3409/**
3410 * Read guest RAM and ROM, signed 16-bit.
3411 *
3412 * @param pbSrcPhys The source address. Relative to guest RAM.
3413 */
3414int16_t remR3PhysReadHCPtrS16(uint8_t *pbSrcPhys)
3415{
3416 int16_t val;
3417
3418 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3419
3420 /*
3421 * Calc the physical address ('off') and check that it's within the RAM.
3422 * ROM is accessed this way, even if it's not part of the RAM.
3423 */
3424 /** @todo This is rather ugly, but there's no other way when we don't wish to touch *many* other files. */
3425#ifdef PGM_DYNAMIC_RAM_ALLOC
3426 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3427#else
3428 uintptr_t off = pbSrcPhys - phys_ram_base;
3429#endif
3430 val = PGMR3PhysReadWord(cpu_single_env->pVM, (RTGCPHYS)off);
3431 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3432 return val;
3433}
3434
3435
3436/**
3437 * Read guest RAM and ROM, unsigned 32-bit.
3438 *
3439 * @param pbSrcPhys The source address. Relative to guest RAM.
3440 */
3441uint32_t remR3PhysReadHCPtrU32(uint8_t *pbSrcPhys)
3442{
3443 uint32_t val;
3444
3445 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3446
3447 /*
3448 * Calc the physical address ('off') and check that it's within the RAM.
3449 * ROM is accessed this way, even if it's not part of the RAM.
3450 */
3451#ifdef PGM_DYNAMIC_RAM_ALLOC
3452 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3453#else
3454 uintptr_t off = pbSrcPhys - phys_ram_base;
3455#endif
3456 val = PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off);
3457 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3458 return val;
3459}
3460
3461
3462/**
3463 * Read guest RAM and ROM, signed 32-bit.
3464 *
3465 * @param pbSrcPhys The source address. Relative to guest RAM.
3466 */
3467int32_t remR3PhysReadHCPtrS32(uint8_t *pbSrcPhys)
3468{
3469 int32_t val;
3470
3471 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3472
3473 /*
3474 * Calc the physical address ('off') and check that it's within the RAM.
3475 * ROM is accessed this way, even if it's not part of the RAM.
3476 */
3477#ifdef PGM_DYNAMIC_RAM_ALLOC
3478 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3479#else
3480 uintptr_t off = pbSrcPhys - phys_ram_base;
3481#endif
3482 val = PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off);
3483 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3484 return val;
3485}
3486
3487
3488/**
3489 * Read guest RAM and ROM, unsigned 64-bit.
3490 *
3491 * @param pbSrcPhys The source address. Relative to guest RAM.
3492 */
3493uint64_t remR3PhysReadHCPtrU64(uint8_t *pbSrcPhys)
3494{
3495 uint64_t val;
3496
3497 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3498
3499 /*
3500 * Calc the physical address ('off') and check that it's within the RAM.
3501 * ROM is accessed this way, even if it's not part of the RAM.
3502 */
3503#ifdef PGM_DYNAMIC_RAM_ALLOC
3504 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3505#else
3506 uintptr_t off = pbSrcPhys - phys_ram_base;
3507#endif
3508 val = PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off)
3509 | ((uint64_t)PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off + 4) << 32); /** @todo fix me! */
3510 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3511 return val;
3512}
3513
3514
3515/**
3516 * Write guest RAM.
3517 *
3518 * @param pbDstPhys The destination address. Relative to guest RAM.
3519 * @param pvSrc The source address.
3520 * @param cb Number of bytes to write
3521 */
3522void remR3PhysWriteHCPtr(uint8_t *pbDstPhys, const void *pvSrc, unsigned cb)
3523{
3524 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3525 /*
3526 * Calc the physical address ('off') and check that it's within the RAM.
3527 */
3528#ifdef PGM_DYNAMIC_RAM_ALLOC
3529 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3530#else
3531 uintptr_t off = pbDstPhys - phys_ram_base;
3532#endif
3533 PGMPhysWrite(cpu_single_env->pVM, (RTGCPHYS)off, pvSrc, cb);
3534 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3535}
3536
3537
3538/**
3539 * Write guest RAM, unsigned 8-bit.
3540 *
3541 * @param pbDstPhys The destination address. Relative to guest RAM.
3542 * @param val Value
3543 */
3544void remR3PhysWriteHCPtrU8(uint8_t *pbDstPhys, uint8_t val)
3545{
3546 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3547 /*
3548 * Calc the physical address ('off') and check that it's within the RAM.
3549 */
3550#ifdef PGM_DYNAMIC_RAM_ALLOC
3551 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3552#else
3553 uintptr_t off = pbDstPhys - phys_ram_base;
3554#endif
3555 PGMR3PhysWriteByte(cpu_single_env->pVM, (RTGCPHYS)off, val);
3556 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3557}
3558
3559
3560/**
3561 * Write guest RAM, unsigned 16-bit.
3562 *
3563 * @param pbDstPhys The destination address. Relative to guest RAM.
3564 * @param val Value
3565 */
3566void remR3PhysWriteHCPtrU16(uint8_t *pbDstPhys, uint16_t val)
3567{
3568 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3569 /*
3570 * Calc the physical address ('off') and check that it's within the RAM.
3571 */
3572#ifdef PGM_DYNAMIC_RAM_ALLOC
3573 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3574#else
3575 uintptr_t off = pbDstPhys - phys_ram_base;
3576#endif
3577 PGMR3PhysWriteWord(cpu_single_env->pVM, (RTGCPHYS)off, val);
3578 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3579}
3580
3581
3582/**
3583 * Write guest RAM, unsigned 32-bit.
3584 *
3585 * @param pbDstPhys The destination address. Relative to guest RAM.
3586 * @param val Value
3587 */
3588void remR3PhysWriteHCPtrU32(uint8_t *pbDstPhys, uint32_t val)
3589{
3590 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3591 /*
3592 * Calc the physical address ('off') and check that it's within the RAM.
3593 */
3594#ifdef PGM_DYNAMIC_RAM_ALLOC
3595 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3596#else
3597 uintptr_t off = pbDstPhys - phys_ram_base;
3598#endif
3599 PGMR3PhysWriteDword(cpu_single_env->pVM, (RTGCPHYS)off, val);
3600 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3601}
3602
3603
3604/**
3605 * Write guest RAM, unsigned 64-bit.
3606 *
3607 * @param pbDstPhys The destination address. Relative to guest RAM.
3608 * @param val Value
3609 */
3610void remR3PhysWriteHCPtrU64(uint8_t *pbDstPhys, uint64_t val)
3611{
3612 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3613 /*
3614 * Calc the physical address ('off') and check that it's within the RAM.
3615 */
3616#ifdef PGM_DYNAMIC_RAM_ALLOC
3617 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3618#else
3619 uintptr_t off = pbDstPhys - phys_ram_base;
3620#endif
3621 PGMR3PhysWriteDword(cpu_single_env->pVM, (RTGCPHYS)off, (uint32_t)val); /** @todo add U64 interface. */
3622 PGMR3PhysWriteDword(cpu_single_env->pVM, (RTGCPHYS)off + 4, val >> 32);
3623 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3624}
3625
3626#endif /* !REM_PHYS_ADDR_IN_TLB */
3627
3628
3629#undef LOG_GROUP
3630#define LOG_GROUP LOG_GROUP_REM_MMIO
3631
3632/** Read MMIO memory. */
3633static uint32_t remR3MMIOReadU8(void *pvVM, target_phys_addr_t GCPhys)
3634{
3635 uint32_t u32 = 0;
3636 int rc = IOMMMIORead((PVM)pvVM, GCPhys, &u32, 1);
3637 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3638 Log2(("remR3MMIOReadU8: GCPhys=%VGp -> %02x\n", GCPhys, u32));
3639 return u32;
3640}
3641
3642/** Read MMIO memory. */
3643static uint32_t remR3MMIOReadU16(void *pvVM, target_phys_addr_t GCPhys)
3644{
3645 uint32_t u32 = 0;
3646 int rc = IOMMMIORead((PVM)pvVM, GCPhys, &u32, 2);
3647 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3648 Log2(("remR3MMIOReadU16: GCPhys=%VGp -> %04x\n", GCPhys, u32));
3649 return u32;
3650}
3651
3652/** Read MMIO memory. */
3653static uint32_t remR3MMIOReadU32(void *pvVM, target_phys_addr_t GCPhys)
3654{
3655 uint32_t u32 = 0;
3656 int rc = IOMMMIORead((PVM)pvVM, GCPhys, &u32, 4);
3657 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3658 Log2(("remR3MMIOReadU32: GCPhys=%VGp -> %08x\n", GCPhys, u32));
3659 return u32;
3660}
3661
3662/** Write to MMIO memory. */
3663static void remR3MMIOWriteU8(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3664{
3665 Log2(("remR3MMIOWriteU8: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3666 int rc = IOMMMIOWrite((PVM)pvVM, GCPhys, u32, 1);
3667 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3668}
3669
3670/** Write to MMIO memory. */
3671static void remR3MMIOWriteU16(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3672{
3673 Log2(("remR3MMIOWriteU16: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3674 int rc = IOMMMIOWrite((PVM)pvVM, GCPhys, u32, 2);
3675 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3676}
3677
3678/** Write to MMIO memory. */
3679static void remR3MMIOWriteU32(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3680{
3681 Log2(("remR3MMIOWriteU32: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3682 int rc = IOMMMIOWrite((PVM)pvVM, GCPhys, u32, 4);
3683 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3684}
3685
3686
3687#undef LOG_GROUP
3688#define LOG_GROUP LOG_GROUP_REM_HANDLER
3689
3690/* !!!WARNING!!! This is extremely hackish right now, we assume it's only for LFB access! !!!WARNING!!! */
3691
3692static uint32_t remR3HandlerReadU8(void *pvVM, target_phys_addr_t GCPhys)
3693{
3694 Log2(("remR3HandlerReadU8: GCPhys=%VGp\n", GCPhys));
3695 uint8_t u8;
3696 PGMPhysRead((PVM)pvVM, GCPhys, &u8, sizeof(u8));
3697 return u8;
3698}
3699
3700static uint32_t remR3HandlerReadU16(void *pvVM, target_phys_addr_t GCPhys)
3701{
3702 Log2(("remR3HandlerReadU16: GCPhys=%VGp\n", GCPhys));
3703 uint16_t u16;
3704 PGMPhysRead((PVM)pvVM, GCPhys, &u16, sizeof(u16));
3705 return u16;
3706}
3707
3708static uint32_t remR3HandlerReadU32(void *pvVM, target_phys_addr_t GCPhys)
3709{
3710 Log2(("remR3HandlerReadU32: GCPhys=%VGp\n", GCPhys));
3711 uint32_t u32;
3712 PGMPhysRead((PVM)pvVM, GCPhys, &u32, sizeof(u32));
3713 return u32;
3714}
3715
3716static void remR3HandlerWriteU8(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3717{
3718 Log2(("remR3HandlerWriteU8: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3719 PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint8_t));
3720}
3721
3722static void remR3HandlerWriteU16(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3723{
3724 Log2(("remR3HandlerWriteU16: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3725 PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint16_t));
3726}
3727
3728static void remR3HandlerWriteU32(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3729{
3730 Log2(("remR3HandlerWriteU32: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3731 PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint32_t));
3732}
3733
3734/* -+- disassembly -+- */
3735
3736#undef LOG_GROUP
3737#define LOG_GROUP LOG_GROUP_REM_DISAS
3738
3739
3740/**
3741 * Enables or disables singled stepped disassembly.
3742 *
3743 * @returns VBox status code.
3744 * @param pVM VM handle.
3745 * @param fEnable To enable set this flag, to disable clear it.
3746 */
3747static DECLCALLBACK(int) remR3DisasEnableStepping(PVM pVM, bool fEnable)
3748{
3749 LogFlow(("remR3DisasEnableStepping: fEnable=%d\n", fEnable));
3750 VM_ASSERT_EMT(pVM);
3751
3752 if (fEnable)
3753 pVM->rem.s.Env.state |= CPU_EMULATE_SINGLE_STEP;
3754 else
3755 pVM->rem.s.Env.state &= ~CPU_EMULATE_SINGLE_STEP;
3756 return VINF_SUCCESS;
3757}
3758
3759
3760/**
3761 * Enables or disables singled stepped disassembly.
3762 *
3763 * @returns VBox status code.
3764 * @param pVM VM handle.
3765 * @param fEnable To enable set this flag, to disable clear it.
3766 */
3767REMR3DECL(int) REMR3DisasEnableStepping(PVM pVM, bool fEnable)
3768{
3769 PVMREQ pReq;
3770 int rc;
3771
3772 LogFlow(("REMR3DisasEnableStepping: fEnable=%d\n", fEnable));
3773 if (VM_IS_EMT(pVM))
3774 return remR3DisasEnableStepping(pVM, fEnable);
3775
3776 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)remR3DisasEnableStepping, 2, pVM, fEnable);
3777 AssertRC(rc);
3778 if (VBOX_SUCCESS(rc))
3779 rc = pReq->iStatus;
3780 VMR3ReqFree(pReq);
3781 return rc;
3782}
3783
3784
3785#if defined(VBOX_WITH_DEBUGGER) && !(defined(RT_OS_WINDOWS) && defined(RT_ARCH_AMD64))
3786/**
3787 * External Debugger Command: .remstep [on|off|1|0]
3788 */
3789static DECLCALLBACK(int) remR3CmdDisasEnableStepping(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3790{
3791 bool fEnable;
3792 int rc;
3793
3794 /* print status */
3795 if (cArgs == 0)
3796 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "DisasStepping is %s\n",
3797 pVM->rem.s.Env.state & CPU_EMULATE_SINGLE_STEP ? "enabled" : "disabled");
3798
3799 /* convert the argument and change the mode. */
3800 rc = pCmdHlp->pfnVarToBool(pCmdHlp, &paArgs[0], &fEnable);
3801 if (VBOX_FAILURE(rc))
3802 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "boolean conversion failed!\n");
3803 rc = REMR3DisasEnableStepping(pVM, fEnable);
3804 if (VBOX_FAILURE(rc))
3805 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "REMR3DisasEnableStepping failed!\n");
3806 return rc;
3807}
3808#endif
3809
3810
3811/**
3812 * Disassembles n instructions and prints them to the log.
3813 *
3814 * @returns Success indicator.
3815 * @param env Pointer to the recompiler CPU structure.
3816 * @param f32BitCode Indicates that whether or not the code should
3817 * be disassembled as 16 or 32 bit. If -1 the CS
3818 * selector will be inspected.
3819 * @param nrInstructions Nr of instructions to disassemble
3820 * @param pszPrefix
3821 * @remark not currently used for anything but ad-hoc debugging.
3822 */
3823bool remR3DisasBlock(CPUState *env, int f32BitCode, int nrInstructions, char *pszPrefix)
3824{
3825 int i;
3826
3827 /*
3828 * Determin 16/32 bit mode.
3829 */
3830 if (f32BitCode == -1)
3831 f32BitCode = !!(env->segs[R_CS].flags & X86_DESC_DB); /** @todo is this right?!!?!?!?!? */
3832
3833 /*
3834 * Convert cs:eip to host context address.
3835 * We don't care to much about cross page correctness presently.
3836 */
3837 RTGCPTR GCPtrPC = env->segs[R_CS].base + env->eip;
3838 void *pvPC;
3839 if (f32BitCode && (env->cr[0] & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG))
3840 {
3841 /* convert eip to physical address. */
3842 int rc = PGMPhysGCPtr2HCPtrByGstCR3(env->pVM,
3843 GCPtrPC,
3844 env->cr[3],
3845 env->cr[4] & (X86_CR4_PSE | X86_CR4_PAE), /** @todo add longmode flag */
3846 &pvPC);
3847 if (VBOX_FAILURE(rc))
3848 {
3849 if (!PATMIsPatchGCAddr(env->pVM, GCPtrPC))
3850 return false;
3851 pvPC = (char *)PATMR3QueryPatchMemHC(env->pVM, NULL)
3852 + (GCPtrPC - PATMR3QueryPatchMemGC(env->pVM, NULL));
3853 }
3854 }
3855 else
3856 {
3857 /* physical address */
3858 int rc = PGMPhysGCPhys2HCPtr(env->pVM, (RTGCPHYS)GCPtrPC, nrInstructions * 16, &pvPC);
3859 if (VBOX_FAILURE(rc))
3860 return false;
3861 }
3862
3863 /*
3864 * Disassemble.
3865 */
3866 RTINTPTR off = env->eip - (RTINTPTR)pvPC;
3867 DISCPUSTATE Cpu;
3868 Cpu.mode = f32BitCode ? CPUMODE_32BIT : CPUMODE_16BIT;
3869 Cpu.pfnReadBytes = NULL; /** @todo make cs:eip reader for the disassembler. */
3870 //Cpu.dwUserData[0] = (uintptr_t)pVM;
3871 //Cpu.dwUserData[1] = (uintptr_t)pvPC;
3872 //Cpu.dwUserData[2] = GCPtrPC;
3873
3874 for (i=0;i<nrInstructions;i++)
3875 {
3876 char szOutput[256];
3877 uint32_t cbOp;
3878 if (!DISInstr(&Cpu, (uintptr_t)pvPC, off, &cbOp, &szOutput[0]))
3879 return false;
3880 if (pszPrefix)
3881 Log(("%s: %s", pszPrefix, szOutput));
3882 else
3883 Log(("%s", szOutput));
3884
3885 pvPC += cbOp;
3886 }
3887 return true;
3888}
3889
3890
3891/** @todo need to test the new code, using the old code in the mean while. */
3892#define USE_OLD_DUMP_AND_DISASSEMBLY
3893
3894/**
3895 * Disassembles one instruction and prints it to the log.
3896 *
3897 * @returns Success indicator.
3898 * @param env Pointer to the recompiler CPU structure.
3899 * @param f32BitCode Indicates that whether or not the code should
3900 * be disassembled as 16 or 32 bit. If -1 the CS
3901 * selector will be inspected.
3902 * @param pszPrefix
3903 */
3904bool remR3DisasInstr(CPUState *env, int f32BitCode, char *pszPrefix)
3905{
3906#ifdef USE_OLD_DUMP_AND_DISASSEMBLY
3907 PVM pVM = env->pVM;
3908
3909 /*
3910 * Determin 16/32 bit mode.
3911 */
3912 if (f32BitCode == -1)
3913 f32BitCode = !!(env->segs[R_CS].flags & X86_DESC_DB); /** @todo is this right?!!?!?!?!? */
3914
3915 /*
3916 * Log registers
3917 */
3918 if (LogIs2Enabled())
3919 {
3920 remR3StateUpdate(pVM);
3921 DBGFR3InfoLog(pVM, "cpumguest", pszPrefix);
3922 }
3923
3924 /*
3925 * Convert cs:eip to host context address.
3926 * We don't care to much about cross page correctness presently.
3927 */
3928 RTGCPTR GCPtrPC = env->segs[R_CS].base + env->eip;
3929 void *pvPC;
3930 if ((env->cr[0] & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG))
3931 {
3932 /* convert eip to physical address. */
3933 int rc = PGMPhysGCPtr2HCPtrByGstCR3(pVM,
3934 GCPtrPC,
3935 env->cr[3],
3936 env->cr[4] & (X86_CR4_PSE | X86_CR4_PAE),
3937 &pvPC);
3938 if (VBOX_FAILURE(rc))
3939 {
3940 if (!PATMIsPatchGCAddr(pVM, GCPtrPC))
3941 return false;
3942 pvPC = (char *)PATMR3QueryPatchMemHC(pVM, NULL)
3943 + (GCPtrPC - PATMR3QueryPatchMemGC(pVM, NULL));
3944 }
3945 }
3946 else
3947 {
3948
3949 /* physical address */
3950 int rc = PGMPhysGCPhys2HCPtr(pVM, (RTGCPHYS)GCPtrPC, 16, &pvPC);
3951 if (VBOX_FAILURE(rc))
3952 return false;
3953 }
3954
3955 /*
3956 * Disassemble.
3957 */
3958 RTINTPTR off = env->eip - (RTINTPTR)pvPC;
3959 DISCPUSTATE Cpu;
3960 Cpu.mode = f32BitCode ? CPUMODE_32BIT : CPUMODE_16BIT;
3961 Cpu.pfnReadBytes = NULL; /** @todo make cs:eip reader for the disassembler. */
3962 //Cpu.dwUserData[0] = (uintptr_t)pVM;
3963 //Cpu.dwUserData[1] = (uintptr_t)pvPC;
3964 //Cpu.dwUserData[2] = GCPtrPC;
3965 char szOutput[256];
3966 uint32_t cbOp;
3967 if (!DISInstr(&Cpu, (uintptr_t)pvPC, off, &cbOp, &szOutput[0]))
3968 return false;
3969
3970 if (!f32BitCode)
3971 {
3972 if (pszPrefix)
3973 Log(("%s: %04X:%s", pszPrefix, env->segs[R_CS].selector, szOutput));
3974 else
3975 Log(("%04X:%s", env->segs[R_CS].selector, szOutput));
3976 }
3977 else
3978 {
3979 if (pszPrefix)
3980 Log(("%s: %s", pszPrefix, szOutput));
3981 else
3982 Log(("%s", szOutput));
3983 }
3984 return true;
3985
3986#else /* !USE_OLD_DUMP_AND_DISASSEMBLY */
3987 PVM pVM = env->pVM;
3988 const bool fLog = LogIsEnabled();
3989 const bool fLog2 = LogIs2Enabled();
3990 int rc = VINF_SUCCESS;
3991
3992 /*
3993 * Don't bother if there ain't any log output to do.
3994 */
3995 if (!fLog && !fLog2)
3996 return true;
3997
3998 /*
3999 * Update the state so DBGF reads the correct register values.
4000 */
4001 remR3StateUpdate(pVM);
4002
4003 /*
4004 * Log registers if requested.
4005 */
4006 if (!fLog2)
4007 DBGFR3InfoLog(pVM, "cpumguest", pszPrefix);
4008
4009 /*
4010 * Disassemble to log.
4011 */
4012 if (fLog)
4013 rc = DBGFR3DisasInstrCurrentLogInternal(pVM, pszPrefix);
4014
4015 return VBOX_SUCCESS(rc);
4016#endif
4017}
4018
4019
4020/**
4021 * Disassemble recompiled code.
4022 *
4023 * @param phFileIgnored Ignored, logfile usually.
4024 * @param pvCode Pointer to the code block.
4025 * @param cb Size of the code block.
4026 */
4027void disas(FILE *phFileIgnored, void *pvCode, unsigned long cb)
4028{
4029 if (LogIs2Enabled())
4030 {
4031 unsigned off = 0;
4032 char szOutput[256];
4033 DISCPUSTATE Cpu = {0};
4034 Cpu.mode = CPUMODE_32BIT;
4035
4036 RTLogPrintf("Recompiled Code: %p %#lx (%ld) bytes\n", pvCode, cb, cb);
4037 while (off < cb)
4038 {
4039 uint32_t cbInstr;
4040 if (DISInstr(&Cpu, (uintptr_t)pvCode + off, 0, &cbInstr, szOutput))
4041 RTLogPrintf("%s", szOutput);
4042 else
4043 {
4044 RTLogPrintf("disas error\n");
4045 cbInstr = 1;
4046 }
4047 off += cbInstr;
4048 }
4049 }
4050 NOREF(phFileIgnored);
4051}
4052
4053
4054/**
4055 * Disassemble guest code.
4056 *
4057 * @param phFileIgnored Ignored, logfile usually.
4058 * @param uCode The guest address of the code to disassemble. (flat?)
4059 * @param cb Number of bytes to disassemble.
4060 * @param fFlags Flags, probably something which tells if this is 16, 32 or 64 bit code.
4061 */
4062void target_disas(FILE *phFileIgnored, target_ulong uCode, target_ulong cb, int fFlags)
4063{
4064 if (LogIs2Enabled())
4065 {
4066 PVM pVM = cpu_single_env->pVM;
4067
4068 /*
4069 * Update the state so DBGF reads the correct register values (flags).
4070 */
4071 remR3StateUpdate(pVM);
4072
4073 /*
4074 * Do the disassembling.
4075 */
4076 RTLogPrintf("Guest Code: PC=%VGp #VGp (%VGp) bytes fFlags=%d\n", uCode, cb, cb, fFlags);
4077 RTSEL cs = cpu_single_env->segs[R_CS].selector;
4078 RTGCUINTPTR eip = uCode - cpu_single_env->segs[R_CS].base;
4079 for (;;)
4080 {
4081 char szBuf[256];
4082 uint32_t cbInstr;
4083 int rc = DBGFR3DisasInstrEx(pVM,
4084 cs,
4085 eip,
4086 0,
4087 szBuf, sizeof(szBuf),
4088 &cbInstr);
4089 if (VBOX_SUCCESS(rc))
4090 RTLogPrintf("%VGp %s\n", uCode, szBuf);
4091 else
4092 {
4093 RTLogPrintf("%VGp %04x:%VGp: %s\n", uCode, cs, eip, szBuf);
4094 cbInstr = 1;
4095 }
4096
4097 /* next */
4098 if (cb <= cbInstr)
4099 break;
4100 cb -= cbInstr;
4101 uCode += cbInstr;
4102 eip += cbInstr;
4103 }
4104 }
4105 NOREF(phFileIgnored);
4106}
4107
4108
4109/**
4110 * Looks up a guest symbol.
4111 *
4112 * @returns Pointer to symbol name. This is a static buffer.
4113 * @param orig_addr The address in question.
4114 */
4115const char *lookup_symbol(target_ulong orig_addr)
4116{
4117 RTGCINTPTR off = 0;
4118 DBGFSYMBOL Sym;
4119 PVM pVM = cpu_single_env->pVM;
4120 int rc = DBGFR3SymbolByAddr(pVM, orig_addr, &off, &Sym);
4121 if (VBOX_SUCCESS(rc))
4122 {
4123 static char szSym[sizeof(Sym.szName) + 48];
4124 if (!off)
4125 RTStrPrintf(szSym, sizeof(szSym), "%s\n", Sym.szName);
4126 else if (off > 0)
4127 RTStrPrintf(szSym, sizeof(szSym), "%s+%x\n", Sym.szName, off);
4128 else
4129 RTStrPrintf(szSym, sizeof(szSym), "%s-%x\n", Sym.szName, -off);
4130 return szSym;
4131 }
4132 return "<N/A>";
4133}
4134
4135
4136#undef LOG_GROUP
4137#define LOG_GROUP LOG_GROUP_REM
4138
4139
4140/* -+- FF notifications -+- */
4141
4142
4143/**
4144 * Notification about a pending interrupt.
4145 *
4146 * @param pVM VM Handle.
4147 * @param u8Interrupt Interrupt
4148 * @thread The emulation thread.
4149 */
4150REMR3DECL(void) REMR3NotifyPendingInterrupt(PVM pVM, uint8_t u8Interrupt)
4151{
4152 Assert(pVM->rem.s.u32PendingInterrupt == REM_NO_PENDING_IRQ);
4153 pVM->rem.s.u32PendingInterrupt = u8Interrupt;
4154}
4155
4156/**
4157 * Notification about a pending interrupt.
4158 *
4159 * @returns Pending interrupt or REM_NO_PENDING_IRQ
4160 * @param pVM VM Handle.
4161 * @thread The emulation thread.
4162 */
4163REMR3DECL(uint32_t) REMR3QueryPendingInterrupt(PVM pVM)
4164{
4165 return pVM->rem.s.u32PendingInterrupt;
4166}
4167
4168/**
4169 * Notification about the interrupt FF being set.
4170 *
4171 * @param pVM VM Handle.
4172 * @thread The emulation thread.
4173 */
4174REMR3DECL(void) REMR3NotifyInterruptSet(PVM pVM)
4175{
4176 LogFlow(("REMR3NotifyInterruptSet: fInRem=%d interrupts %s\n", pVM->rem.s.fInREM,
4177 (pVM->rem.s.Env.eflags & IF_MASK) && !(pVM->rem.s.Env.hflags & HF_INHIBIT_IRQ_MASK) ? "enabled" : "disabled"));
4178 if (pVM->rem.s.fInREM)
4179 {
4180 if (VM_IS_EMT(pVM))
4181 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
4182 else
4183 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_HARD);
4184 }
4185}
4186
4187
4188/**
4189 * Notification about the interrupt FF being set.
4190 *
4191 * @param pVM VM Handle.
4192 * @thread The emulation thread.
4193 */
4194REMR3DECL(void) REMR3NotifyInterruptClear(PVM pVM)
4195{
4196 LogFlow(("REMR3NotifyInterruptClear:\n"));
4197 VM_ASSERT_EMT(pVM);
4198 if (pVM->rem.s.fInREM)
4199 cpu_reset_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
4200}
4201
4202
4203/**
4204 * Notification about pending timer(s).
4205 *
4206 * @param pVM VM Handle.
4207 * @thread Any.
4208 */
4209REMR3DECL(void) REMR3NotifyTimerPending(PVM pVM)
4210{
4211#ifndef DEBUG_bird
4212 LogFlow(("REMR3NotifyTimerPending: fInRem=%d\n", pVM->rem.s.fInREM));
4213#endif
4214 if (pVM->rem.s.fInREM)
4215 {
4216 if (VM_IS_EMT(pVM))
4217 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4218 else
4219 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_TIMER);
4220 }
4221}
4222
4223
4224/**
4225 * Notification about pending DMA transfers.
4226 *
4227 * @param pVM VM Handle.
4228 * @thread Any.
4229 */
4230REMR3DECL(void) REMR3NotifyDmaPending(PVM pVM)
4231{
4232 LogFlow(("REMR3NotifyDmaPending: fInRem=%d\n", pVM->rem.s.fInREM));
4233 if (pVM->rem.s.fInREM)
4234 {
4235 if (VM_IS_EMT(pVM))
4236 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4237 else
4238 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_DMA);
4239 }
4240}
4241
4242
4243/**
4244 * Notification about pending timer(s).
4245 *
4246 * @param pVM VM Handle.
4247 * @thread Any.
4248 */
4249REMR3DECL(void) REMR3NotifyQueuePending(PVM pVM)
4250{
4251 LogFlow(("REMR3NotifyQueuePending: fInRem=%d\n", pVM->rem.s.fInREM));
4252 if (pVM->rem.s.fInREM)
4253 {
4254 if (VM_IS_EMT(pVM))
4255 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4256 else
4257 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_EXIT);
4258 }
4259}
4260
4261
4262/**
4263 * Notification about pending FF set by an external thread.
4264 *
4265 * @param pVM VM handle.
4266 * @thread Any.
4267 */
4268REMR3DECL(void) REMR3NotifyFF(PVM pVM)
4269{
4270 LogFlow(("REMR3NotifyFF: fInRem=%d\n", pVM->rem.s.fInREM));
4271 if (pVM->rem.s.fInREM)
4272 {
4273 if (VM_IS_EMT(pVM))
4274 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4275 else
4276 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_EXIT);
4277 }
4278}
4279
4280
4281#ifdef VBOX_WITH_STATISTICS
4282void remR3ProfileStart(int statcode)
4283{
4284 STAMPROFILEADV *pStat;
4285 switch(statcode)
4286 {
4287 case STATS_EMULATE_SINGLE_INSTR:
4288 pStat = &gStatExecuteSingleInstr;
4289 break;
4290 case STATS_QEMU_COMPILATION:
4291 pStat = &gStatCompilationQEmu;
4292 break;
4293 case STATS_QEMU_RUN_EMULATED_CODE:
4294 pStat = &gStatRunCodeQEmu;
4295 break;
4296 case STATS_QEMU_TOTAL:
4297 pStat = &gStatTotalTimeQEmu;
4298 break;
4299 case STATS_QEMU_RUN_TIMERS:
4300 pStat = &gStatTimers;
4301 break;
4302 case STATS_TLB_LOOKUP:
4303 pStat= &gStatTBLookup;
4304 break;
4305 case STATS_IRQ_HANDLING:
4306 pStat= &gStatIRQ;
4307 break;
4308 case STATS_RAW_CHECK:
4309 pStat = &gStatRawCheck;
4310 break;
4311
4312 default:
4313 AssertMsgFailed(("unknown stat %d\n", statcode));
4314 return;
4315 }
4316 STAM_PROFILE_ADV_START(pStat, a);
4317}
4318
4319
4320void remR3ProfileStop(int statcode)
4321{
4322 STAMPROFILEADV *pStat;
4323 switch(statcode)
4324 {
4325 case STATS_EMULATE_SINGLE_INSTR:
4326 pStat = &gStatExecuteSingleInstr;
4327 break;
4328 case STATS_QEMU_COMPILATION:
4329 pStat = &gStatCompilationQEmu;
4330 break;
4331 case STATS_QEMU_RUN_EMULATED_CODE:
4332 pStat = &gStatRunCodeQEmu;
4333 break;
4334 case STATS_QEMU_TOTAL:
4335 pStat = &gStatTotalTimeQEmu;
4336 break;
4337 case STATS_QEMU_RUN_TIMERS:
4338 pStat = &gStatTimers;
4339 break;
4340 case STATS_TLB_LOOKUP:
4341 pStat= &gStatTBLookup;
4342 break;
4343 case STATS_IRQ_HANDLING:
4344 pStat= &gStatIRQ;
4345 break;
4346 case STATS_RAW_CHECK:
4347 pStat = &gStatRawCheck;
4348 break;
4349 default:
4350 AssertMsgFailed(("unknown stat %d\n", statcode));
4351 return;
4352 }
4353 STAM_PROFILE_ADV_STOP(pStat, a);
4354}
4355#endif
4356
4357/**
4358 * Raise an RC, force rem exit.
4359 *
4360 * @param pVM VM handle.
4361 * @param rc The rc.
4362 */
4363void remR3RaiseRC(PVM pVM, int rc)
4364{
4365 Log(("remR3RaiseRC: rc=%Vrc\n", rc));
4366 Assert(pVM->rem.s.fInREM);
4367 VM_ASSERT_EMT(pVM);
4368 pVM->rem.s.rc = rc;
4369 cpu_interrupt(&pVM->rem.s.Env, CPU_INTERRUPT_RC);
4370}
4371
4372
4373/* -+- timers -+- */
4374
4375uint64_t cpu_get_tsc(CPUX86State *env)
4376{
4377 STAM_COUNTER_INC(&gStatCpuGetTSC);
4378 return TMCpuTickGet(env->pVM);
4379}
4380
4381
4382/* -+- interrupts -+- */
4383
4384void cpu_set_ferr(CPUX86State *env)
4385{
4386 int rc = PDMIsaSetIrq(env->pVM, 13, 1);
4387 LogFlow(("cpu_set_ferr: rc=%d\n", rc)); NOREF(rc);
4388}
4389
4390int cpu_get_pic_interrupt(CPUState *env)
4391{
4392 uint8_t u8Interrupt;
4393 int rc;
4394
4395 /* When we fail to forward interrupts directly in raw mode, we fall back to the recompiler.
4396 * In that case we can't call PDMGetInterrupt anymore, because it has already cleared the interrupt
4397 * with the (a)pic.
4398 */
4399 /** @note We assume we will go directly to the recompiler to handle the pending interrupt! */
4400 /** @todo r=bird: In the long run we should just do the interrupt handling in EM/CPUM/TRPM/somewhere and
4401 * if we cannot execute the interrupt handler in raw-mode just reschedule to REM. Once that is done we
4402 * remove this kludge. */
4403 if (env->pVM->rem.s.u32PendingInterrupt != REM_NO_PENDING_IRQ)
4404 {
4405 rc = VINF_SUCCESS;
4406 Assert(env->pVM->rem.s.u32PendingInterrupt >= 0 && env->pVM->rem.s.u32PendingInterrupt <= 255);
4407 u8Interrupt = env->pVM->rem.s.u32PendingInterrupt;
4408 env->pVM->rem.s.u32PendingInterrupt = REM_NO_PENDING_IRQ;
4409 }
4410 else
4411 rc = PDMGetInterrupt(env->pVM, &u8Interrupt);
4412
4413 LogFlow(("cpu_get_pic_interrupt: u8Interrupt=%d rc=%Vrc\n", u8Interrupt, rc));
4414 if (VBOX_SUCCESS(rc))
4415 {
4416 if (VM_FF_ISPENDING(env->pVM, VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC))
4417 env->interrupt_request |= CPU_INTERRUPT_HARD;
4418 return u8Interrupt;
4419 }
4420 return -1;
4421}
4422
4423
4424/* -+- local apic -+- */
4425
4426void cpu_set_apic_base(CPUX86State *env, uint64_t val)
4427{
4428 int rc = PDMApicSetBase(env->pVM, val);
4429 LogFlow(("cpu_set_apic_base: val=%#llx rc=%Vrc\n", val, rc)); NOREF(rc);
4430}
4431
4432uint64_t cpu_get_apic_base(CPUX86State *env)
4433{
4434 uint64_t u64;
4435 int rc = PDMApicGetBase(env->pVM, &u64);
4436 if (VBOX_SUCCESS(rc))
4437 {
4438 LogFlow(("cpu_get_apic_base: returns %#llx \n", u64));
4439 return u64;
4440 }
4441 LogFlow(("cpu_get_apic_base: returns 0 (rc=%Vrc)\n", rc));
4442 return 0;
4443}
4444
4445void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
4446{
4447 int rc = PDMApicSetTPR(env->pVM, val);
4448 LogFlow(("cpu_set_apic_tpr: val=%#x rc=%Vrc\n", val, rc)); NOREF(rc);
4449}
4450
4451uint8_t cpu_get_apic_tpr(CPUX86State *env)
4452{
4453 uint8_t u8;
4454 int rc = PDMApicGetTPR(env->pVM, &u8);
4455 if (VBOX_SUCCESS(rc))
4456 {
4457 LogFlow(("cpu_get_apic_tpr: returns %#x\n", u8));
4458 return u8;
4459 }
4460 LogFlow(("cpu_get_apic_tpr: returns 0 (rc=%Vrc)\n", rc));
4461 return 0;
4462}
4463
4464
4465/* -+- I/O Ports -+- */
4466
4467#undef LOG_GROUP
4468#define LOG_GROUP LOG_GROUP_REM_IOPORT
4469
4470void cpu_outb(CPUState *env, int addr, int val)
4471{
4472 if (addr != 0x80 && addr != 0x70 && addr != 0x61)
4473 Log2(("cpu_outb: addr=%#06x val=%#x\n", addr, val));
4474
4475 int rc = IOMIOPortWrite(env->pVM, (RTIOPORT)addr, val, 1);
4476 if (RT_LIKELY(rc == VINF_SUCCESS))
4477 return;
4478 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4479 {
4480 Log(("cpu_outb: addr=%#06x val=%#x -> %Vrc\n", addr, val, rc));
4481 remR3RaiseRC(env->pVM, rc);
4482 return;
4483 }
4484 remAbort(rc, __FUNCTION__);
4485}
4486
4487void cpu_outw(CPUState *env, int addr, int val)
4488{
4489 //Log2(("cpu_outw: addr=%#06x val=%#x\n", addr, val));
4490 int rc = IOMIOPortWrite(env->pVM, (RTIOPORT)addr, val, 2);
4491 if (RT_LIKELY(rc == VINF_SUCCESS))
4492 return;
4493 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4494 {
4495 Log(("cpu_outw: addr=%#06x val=%#x -> %Vrc\n", addr, val, rc));
4496 remR3RaiseRC(env->pVM, rc);
4497 return;
4498 }
4499 remAbort(rc, __FUNCTION__);
4500}
4501
4502void cpu_outl(CPUState *env, int addr, int val)
4503{
4504 Log2(("cpu_outl: addr=%#06x val=%#x\n", addr, val));
4505 int rc = IOMIOPortWrite(env->pVM, (RTIOPORT)addr, val, 4);
4506 if (RT_LIKELY(rc == VINF_SUCCESS))
4507 return;
4508 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4509 {
4510 Log(("cpu_outl: addr=%#06x val=%#x -> %Vrc\n", addr, val, rc));
4511 remR3RaiseRC(env->pVM, rc);
4512 return;
4513 }
4514 remAbort(rc, __FUNCTION__);
4515}
4516
4517int cpu_inb(CPUState *env, int addr)
4518{
4519 uint32_t u32 = 0;
4520 int rc = IOMIOPortRead(env->pVM, (RTIOPORT)addr, &u32, 1);
4521 if (RT_LIKELY(rc == VINF_SUCCESS))
4522 {
4523 if (/*addr != 0x61 && */addr != 0x71)
4524 Log2(("cpu_inb: addr=%#06x -> %#x\n", addr, u32));
4525 return (int)u32;
4526 }
4527 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4528 {
4529 Log(("cpu_inb: addr=%#06x -> %#x rc=%Vrc\n", addr, u32, rc));
4530 remR3RaiseRC(env->pVM, rc);
4531 return (int)u32;
4532 }
4533 remAbort(rc, __FUNCTION__);
4534 return 0xff;
4535}
4536
4537int cpu_inw(CPUState *env, int addr)
4538{
4539 uint32_t u32 = 0;
4540 int rc = IOMIOPortRead(env->pVM, (RTIOPORT)addr, &u32, 2);
4541 if (RT_LIKELY(rc == VINF_SUCCESS))
4542 {
4543 Log2(("cpu_inw: addr=%#06x -> %#x\n", addr, u32));
4544 return (int)u32;
4545 }
4546 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4547 {
4548 Log(("cpu_inw: addr=%#06x -> %#x rc=%Vrc\n", addr, u32, rc));
4549 remR3RaiseRC(env->pVM, rc);
4550 return (int)u32;
4551 }
4552 remAbort(rc, __FUNCTION__);
4553 return 0xffff;
4554}
4555
4556int cpu_inl(CPUState *env, int addr)
4557{
4558 uint32_t u32 = 0;
4559 int rc = IOMIOPortRead(env->pVM, (RTIOPORT)addr, &u32, 4);
4560 if (RT_LIKELY(rc == VINF_SUCCESS))
4561 {
4562//if (addr==0x01f0 && u32 == 0x6b6d)
4563// loglevel = ~0;
4564 Log2(("cpu_inl: addr=%#06x -> %#x\n", addr, u32));
4565 return (int)u32;
4566 }
4567 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4568 {
4569 Log(("cpu_inl: addr=%#06x -> %#x rc=%Vrc\n", addr, u32, rc));
4570 remR3RaiseRC(env->pVM, rc);
4571 return (int)u32;
4572 }
4573 remAbort(rc, __FUNCTION__);
4574 return 0xffffffff;
4575}
4576
4577#undef LOG_GROUP
4578#define LOG_GROUP LOG_GROUP_REM
4579
4580
4581/* -+- helpers and misc other interfaces -+- */
4582
4583/**
4584 * Perform the CPUID instruction.
4585 *
4586 * ASMCpuId cannot be invoked from some source files where this is used because of global
4587 * register allocations.
4588 *
4589 * @param env Pointer to the recompiler CPU structure.
4590 * @param uOperator CPUID operation (eax).
4591 * @param pvEAX Where to store eax.
4592 * @param pvEBX Where to store ebx.
4593 * @param pvECX Where to store ecx.
4594 * @param pvEDX Where to store edx.
4595 */
4596void remR3CpuId(CPUState *env, unsigned uOperator, void *pvEAX, void *pvEBX, void *pvECX, void *pvEDX)
4597{
4598 CPUMGetGuestCpuId(env->pVM, uOperator, (uint32_t *)pvEAX, (uint32_t *)pvEBX, (uint32_t *)pvECX, (uint32_t *)pvEDX);
4599}
4600
4601
4602#if 0 /* not used */
4603/**
4604 * Interface for qemu hardware to report back fatal errors.
4605 */
4606void hw_error(const char *pszFormat, ...)
4607{
4608 /*
4609 * Bitch about it.
4610 */
4611 /** @todo Add support for nested arg lists in the LogPrintfV routine! I've code for
4612 * this in my Odin32 tree at home! */
4613 va_list args;
4614 va_start(args, pszFormat);
4615 RTLogPrintf("fatal error in virtual hardware:");
4616 RTLogPrintfV(pszFormat, args);
4617 va_end(args);
4618 AssertReleaseMsgFailed(("fatal error in virtual hardware: %s\n", pszFormat));
4619
4620 /*
4621 * If we're in REM context we'll sync back the state before 'jumping' to
4622 * the EMs failure handling.
4623 */
4624 PVM pVM = cpu_single_env->pVM;
4625 if (pVM->rem.s.fInREM)
4626 REMR3StateBack(pVM);
4627 EMR3FatalError(pVM, VERR_REM_VIRTUAL_HARDWARE_ERROR);
4628 AssertMsgFailed(("EMR3FatalError returned!\n"));
4629}
4630#endif
4631
4632/**
4633 * Interface for the qemu cpu to report unhandled situation
4634 * raising a fatal VM error.
4635 */
4636void cpu_abort(CPUState *env, const char *pszFormat, ...)
4637{
4638 /*
4639 * Bitch about it.
4640 */
4641 RTLogFlags(NULL, "nodisabled nobuffered");
4642 va_list args;
4643 va_start(args, pszFormat);
4644 RTLogPrintf("fatal error in recompiler cpu: %N\n", pszFormat, &args);
4645 va_end(args);
4646 va_start(args, pszFormat);
4647 AssertReleaseMsgFailed(("fatal error in recompiler cpu: %N\n", pszFormat, &args));
4648 va_end(args);
4649
4650 /*
4651 * If we're in REM context we'll sync back the state before 'jumping' to
4652 * the EMs failure handling.
4653 */
4654 PVM pVM = cpu_single_env->pVM;
4655 if (pVM->rem.s.fInREM)
4656 REMR3StateBack(pVM);
4657 EMR3FatalError(pVM, VERR_REM_VIRTUAL_CPU_ERROR);
4658 AssertMsgFailed(("EMR3FatalError returned!\n"));
4659}
4660
4661
4662/**
4663 * Aborts the VM.
4664 *
4665 * @param rc VBox error code.
4666 * @param pszTip Hint about why/when this happend.
4667 */
4668static void remAbort(int rc, const char *pszTip)
4669{
4670 /*
4671 * Bitch about it.
4672 */
4673 RTLogPrintf("internal REM fatal error: rc=%Vrc %s\n", rc, pszTip);
4674 AssertReleaseMsgFailed(("internal REM fatal error: rc=%Vrc %s\n", rc, pszTip));
4675
4676 /*
4677 * Jump back to where we entered the recompiler.
4678 */
4679 PVM pVM = cpu_single_env->pVM;
4680 if (pVM->rem.s.fInREM)
4681 REMR3StateBack(pVM);
4682 EMR3FatalError(pVM, rc);
4683 AssertMsgFailed(("EMR3FatalError returned!\n"));
4684}
4685
4686
4687/**
4688 * Dumps a linux system call.
4689 * @param pVM VM handle.
4690 */
4691void remR3DumpLnxSyscall(PVM pVM)
4692{
4693 static const char *apsz[] =
4694 {
4695 "sys_restart_syscall", /* 0 - old "setup()" system call, used for restarting */
4696 "sys_exit",
4697 "sys_fork",
4698 "sys_read",
4699 "sys_write",
4700 "sys_open", /* 5 */
4701 "sys_close",
4702 "sys_waitpid",
4703 "sys_creat",
4704 "sys_link",
4705 "sys_unlink", /* 10 */
4706 "sys_execve",
4707 "sys_chdir",
4708 "sys_time",
4709 "sys_mknod",
4710 "sys_chmod", /* 15 */
4711 "sys_lchown16",
4712 "sys_ni_syscall", /* old break syscall holder */
4713 "sys_stat",
4714 "sys_lseek",
4715 "sys_getpid", /* 20 */
4716 "sys_mount",
4717 "sys_oldumount",
4718 "sys_setuid16",
4719 "sys_getuid16",
4720 "sys_stime", /* 25 */
4721 "sys_ptrace",
4722 "sys_alarm",
4723 "sys_fstat",
4724 "sys_pause",
4725 "sys_utime", /* 30 */
4726 "sys_ni_syscall", /* old stty syscall holder */
4727 "sys_ni_syscall", /* old gtty syscall holder */
4728 "sys_access",
4729 "sys_nice",
4730 "sys_ni_syscall", /* 35 - old ftime syscall holder */
4731 "sys_sync",
4732 "sys_kill",
4733 "sys_rename",
4734 "sys_mkdir",
4735 "sys_rmdir", /* 40 */
4736 "sys_dup",
4737 "sys_pipe",
4738 "sys_times",
4739 "sys_ni_syscall", /* old prof syscall holder */
4740 "sys_brk", /* 45 */
4741 "sys_setgid16",
4742 "sys_getgid16",
4743 "sys_signal",
4744 "sys_geteuid16",
4745 "sys_getegid16", /* 50 */
4746 "sys_acct",
4747 "sys_umount", /* recycled never used phys() */
4748 "sys_ni_syscall", /* old lock syscall holder */
4749 "sys_ioctl",
4750 "sys_fcntl", /* 55 */
4751 "sys_ni_syscall", /* old mpx syscall holder */
4752 "sys_setpgid",
4753 "sys_ni_syscall", /* old ulimit syscall holder */
4754 "sys_olduname",
4755 "sys_umask", /* 60 */
4756 "sys_chroot",
4757 "sys_ustat",
4758 "sys_dup2",
4759 "sys_getppid",
4760 "sys_getpgrp", /* 65 */
4761 "sys_setsid",
4762 "sys_sigaction",
4763 "sys_sgetmask",
4764 "sys_ssetmask",
4765 "sys_setreuid16", /* 70 */
4766 "sys_setregid16",
4767 "sys_sigsuspend",
4768 "sys_sigpending",
4769 "sys_sethostname",
4770 "sys_setrlimit", /* 75 */
4771 "sys_old_getrlimit",
4772 "sys_getrusage",
4773 "sys_gettimeofday",
4774 "sys_settimeofday",
4775 "sys_getgroups16", /* 80 */
4776 "sys_setgroups16",
4777 "old_select",
4778 "sys_symlink",
4779 "sys_lstat",
4780 "sys_readlink", /* 85 */
4781 "sys_uselib",
4782 "sys_swapon",
4783 "sys_reboot",
4784 "old_readdir",
4785 "old_mmap", /* 90 */
4786 "sys_munmap",
4787 "sys_truncate",
4788 "sys_ftruncate",
4789 "sys_fchmod",
4790 "sys_fchown16", /* 95 */
4791 "sys_getpriority",
4792 "sys_setpriority",
4793 "sys_ni_syscall", /* old profil syscall holder */
4794 "sys_statfs",
4795 "sys_fstatfs", /* 100 */
4796 "sys_ioperm",
4797 "sys_socketcall",
4798 "sys_syslog",
4799 "sys_setitimer",
4800 "sys_getitimer", /* 105 */
4801 "sys_newstat",
4802 "sys_newlstat",
4803 "sys_newfstat",
4804 "sys_uname",
4805 "sys_iopl", /* 110 */
4806 "sys_vhangup",
4807 "sys_ni_syscall", /* old "idle" system call */
4808 "sys_vm86old",
4809 "sys_wait4",
4810 "sys_swapoff", /* 115 */
4811 "sys_sysinfo",
4812 "sys_ipc",
4813 "sys_fsync",
4814 "sys_sigreturn",
4815 "sys_clone", /* 120 */
4816 "sys_setdomainname",
4817 "sys_newuname",
4818 "sys_modify_ldt",
4819 "sys_adjtimex",
4820 "sys_mprotect", /* 125 */
4821 "sys_sigprocmask",
4822 "sys_ni_syscall", /* old "create_module" */
4823 "sys_init_module",
4824 "sys_delete_module",
4825 "sys_ni_syscall", /* 130: old "get_kernel_syms" */
4826 "sys_quotactl",
4827 "sys_getpgid",
4828 "sys_fchdir",
4829 "sys_bdflush",
4830 "sys_sysfs", /* 135 */
4831 "sys_personality",
4832 "sys_ni_syscall", /* reserved for afs_syscall */
4833 "sys_setfsuid16",
4834 "sys_setfsgid16",
4835 "sys_llseek", /* 140 */
4836 "sys_getdents",
4837 "sys_select",
4838 "sys_flock",
4839 "sys_msync",
4840 "sys_readv", /* 145 */
4841 "sys_writev",
4842 "sys_getsid",
4843 "sys_fdatasync",
4844 "sys_sysctl",
4845 "sys_mlock", /* 150 */
4846 "sys_munlock",
4847 "sys_mlockall",
4848 "sys_munlockall",
4849 "sys_sched_setparam",
4850 "sys_sched_getparam", /* 155 */
4851 "sys_sched_setscheduler",
4852 "sys_sched_getscheduler",
4853 "sys_sched_yield",
4854 "sys_sched_get_priority_max",
4855 "sys_sched_get_priority_min", /* 160 */
4856 "sys_sched_rr_get_interval",
4857 "sys_nanosleep",
4858 "sys_mremap",
4859 "sys_setresuid16",
4860 "sys_getresuid16", /* 165 */
4861 "sys_vm86",
4862 "sys_ni_syscall", /* Old sys_query_module */
4863 "sys_poll",
4864 "sys_nfsservctl",
4865 "sys_setresgid16", /* 170 */
4866 "sys_getresgid16",
4867 "sys_prctl",
4868 "sys_rt_sigreturn",
4869 "sys_rt_sigaction",
4870 "sys_rt_sigprocmask", /* 175 */
4871 "sys_rt_sigpending",
4872 "sys_rt_sigtimedwait",
4873 "sys_rt_sigqueueinfo",
4874 "sys_rt_sigsuspend",
4875 "sys_pread64", /* 180 */
4876 "sys_pwrite64",
4877 "sys_chown16",
4878 "sys_getcwd",
4879 "sys_capget",
4880 "sys_capset", /* 185 */
4881 "sys_sigaltstack",
4882 "sys_sendfile",
4883 "sys_ni_syscall", /* reserved for streams1 */
4884 "sys_ni_syscall", /* reserved for streams2 */
4885 "sys_vfork", /* 190 */
4886 "sys_getrlimit",
4887 "sys_mmap2",
4888 "sys_truncate64",
4889 "sys_ftruncate64",
4890 "sys_stat64", /* 195 */
4891 "sys_lstat64",
4892 "sys_fstat64",
4893 "sys_lchown",
4894 "sys_getuid",
4895 "sys_getgid", /* 200 */
4896 "sys_geteuid",
4897 "sys_getegid",
4898 "sys_setreuid",
4899 "sys_setregid",
4900 "sys_getgroups", /* 205 */
4901 "sys_setgroups",
4902 "sys_fchown",
4903 "sys_setresuid",
4904 "sys_getresuid",
4905 "sys_setresgid", /* 210 */
4906 "sys_getresgid",
4907 "sys_chown",
4908 "sys_setuid",
4909 "sys_setgid",
4910 "sys_setfsuid", /* 215 */
4911 "sys_setfsgid",
4912 "sys_pivot_root",
4913 "sys_mincore",
4914 "sys_madvise",
4915 "sys_getdents64", /* 220 */
4916 "sys_fcntl64",
4917 "sys_ni_syscall", /* reserved for TUX */
4918 "sys_ni_syscall",
4919 "sys_gettid",
4920 "sys_readahead", /* 225 */
4921 "sys_setxattr",
4922 "sys_lsetxattr",
4923 "sys_fsetxattr",
4924 "sys_getxattr",
4925 "sys_lgetxattr", /* 230 */
4926 "sys_fgetxattr",
4927 "sys_listxattr",
4928 "sys_llistxattr",
4929 "sys_flistxattr",
4930 "sys_removexattr", /* 235 */
4931 "sys_lremovexattr",
4932 "sys_fremovexattr",
4933 "sys_tkill",
4934 "sys_sendfile64",
4935 "sys_futex", /* 240 */
4936 "sys_sched_setaffinity",
4937 "sys_sched_getaffinity",
4938 "sys_set_thread_area",
4939 "sys_get_thread_area",
4940 "sys_io_setup", /* 245 */
4941 "sys_io_destroy",
4942 "sys_io_getevents",
4943 "sys_io_submit",
4944 "sys_io_cancel",
4945 "sys_fadvise64", /* 250 */
4946 "sys_ni_syscall",
4947 "sys_exit_group",
4948 "sys_lookup_dcookie",
4949 "sys_epoll_create",
4950 "sys_epoll_ctl", /* 255 */
4951 "sys_epoll_wait",
4952 "sys_remap_file_pages",
4953 "sys_set_tid_address",
4954 "sys_timer_create",
4955 "sys_timer_settime", /* 260 */
4956 "sys_timer_gettime",
4957 "sys_timer_getoverrun",
4958 "sys_timer_delete",
4959 "sys_clock_settime",
4960 "sys_clock_gettime", /* 265 */
4961 "sys_clock_getres",
4962 "sys_clock_nanosleep",
4963 "sys_statfs64",
4964 "sys_fstatfs64",
4965 "sys_tgkill", /* 270 */
4966 "sys_utimes",
4967 "sys_fadvise64_64",
4968 "sys_ni_syscall" /* sys_vserver */
4969 };
4970
4971 uint32_t uEAX = CPUMGetGuestEAX(pVM);
4972 switch (uEAX)
4973 {
4974 default:
4975 if (uEAX < ELEMENTS(apsz))
4976 Log(("REM: linux syscall %3d: %s (eip=%VGv ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x ebp=%08x)\n",
4977 uEAX, apsz[uEAX], CPUMGetGuestEIP(pVM), CPUMGetGuestEBX(pVM), CPUMGetGuestECX(pVM),
4978 CPUMGetGuestEDX(pVM), CPUMGetGuestESI(pVM), CPUMGetGuestEDI(pVM), CPUMGetGuestEBP(pVM)));
4979 else
4980 Log(("eip=%08x: linux syscall %d (#%x) unknown\n", CPUMGetGuestEIP(pVM), uEAX, uEAX));
4981 break;
4982
4983 }
4984}
4985
4986
4987/**
4988 * Dumps an OpenBSD system call.
4989 * @param pVM VM handle.
4990 */
4991void remR3DumpOBsdSyscall(PVM pVM)
4992{
4993 static const char *apsz[] =
4994 {
4995 "SYS_syscall", //0
4996 "SYS_exit", //1
4997 "SYS_fork", //2
4998 "SYS_read", //3
4999 "SYS_write", //4
5000 "SYS_open", //5
5001 "SYS_close", //6
5002 "SYS_wait4", //7
5003 "SYS_8",
5004 "SYS_link", //9
5005 "SYS_unlink", //10
5006 "SYS_11",
5007 "SYS_chdir", //12
5008 "SYS_fchdir", //13
5009 "SYS_mknod", //14
5010 "SYS_chmod", //15
5011 "SYS_chown", //16
5012 "SYS_break", //17
5013 "SYS_18",
5014 "SYS_19",
5015 "SYS_getpid", //20
5016 "SYS_mount", //21
5017 "SYS_unmount", //22
5018 "SYS_setuid", //23
5019 "SYS_getuid", //24
5020 "SYS_geteuid", //25
5021 "SYS_ptrace", //26
5022 "SYS_recvmsg", //27
5023 "SYS_sendmsg", //28
5024 "SYS_recvfrom", //29
5025 "SYS_accept", //30
5026 "SYS_getpeername", //31
5027 "SYS_getsockname", //32
5028 "SYS_access", //33
5029 "SYS_chflags", //34
5030 "SYS_fchflags", //35
5031 "SYS_sync", //36
5032 "SYS_kill", //37
5033 "SYS_38",
5034 "SYS_getppid", //39
5035 "SYS_40",
5036 "SYS_dup", //41
5037 "SYS_opipe", //42
5038 "SYS_getegid", //43
5039 "SYS_profil", //44
5040 "SYS_ktrace", //45
5041 "SYS_sigaction", //46
5042 "SYS_getgid", //47
5043 "SYS_sigprocmask", //48
5044 "SYS_getlogin", //49
5045 "SYS_setlogin", //50
5046 "SYS_acct", //51
5047 "SYS_sigpending", //52
5048 "SYS_osigaltstack", //53
5049 "SYS_ioctl", //54
5050 "SYS_reboot", //55
5051 "SYS_revoke", //56
5052 "SYS_symlink", //57
5053 "SYS_readlink", //58
5054 "SYS_execve", //59
5055 "SYS_umask", //60
5056 "SYS_chroot", //61
5057 "SYS_62",
5058 "SYS_63",
5059 "SYS_64",
5060 "SYS_65",
5061 "SYS_vfork", //66
5062 "SYS_67",
5063 "SYS_68",
5064 "SYS_sbrk", //69
5065 "SYS_sstk", //70
5066 "SYS_61",
5067 "SYS_vadvise", //72
5068 "SYS_munmap", //73
5069 "SYS_mprotect", //74
5070 "SYS_madvise", //75
5071 "SYS_76",
5072 "SYS_77",
5073 "SYS_mincore", //78
5074 "SYS_getgroups", //79
5075 "SYS_setgroups", //80
5076 "SYS_getpgrp", //81
5077 "SYS_setpgid", //82
5078 "SYS_setitimer", //83
5079 "SYS_84",
5080 "SYS_85",
5081 "SYS_getitimer", //86
5082 "SYS_87",
5083 "SYS_88",
5084 "SYS_89",
5085 "SYS_dup2", //90
5086 "SYS_91",
5087 "SYS_fcntl", //92
5088 "SYS_select", //93
5089 "SYS_94",
5090 "SYS_fsync", //95
5091 "SYS_setpriority", //96
5092 "SYS_socket", //97
5093 "SYS_connect", //98
5094 "SYS_99",
5095 "SYS_getpriority", //100
5096 "SYS_101",
5097 "SYS_102",
5098 "SYS_sigreturn", //103
5099 "SYS_bind", //104
5100 "SYS_setsockopt", //105
5101 "SYS_listen", //106
5102 "SYS_107",
5103 "SYS_108",
5104 "SYS_109",
5105 "SYS_110",
5106 "SYS_sigsuspend", //111
5107 "SYS_112",
5108 "SYS_113",
5109 "SYS_114",
5110 "SYS_115",
5111 "SYS_gettimeofday", //116
5112 "SYS_getrusage", //117
5113 "SYS_getsockopt", //118
5114 "SYS_119",
5115 "SYS_readv", //120
5116 "SYS_writev", //121
5117 "SYS_settimeofday", //122
5118 "SYS_fchown", //123
5119 "SYS_fchmod", //124
5120 "SYS_125",
5121 "SYS_setreuid", //126
5122 "SYS_setregid", //127
5123 "SYS_rename", //128
5124 "SYS_129",
5125 "SYS_130",
5126 "SYS_flock", //131
5127 "SYS_mkfifo", //132
5128 "SYS_sendto", //133
5129 "SYS_shutdown", //134
5130 "SYS_socketpair", //135
5131 "SYS_mkdir", //136
5132 "SYS_rmdir", //137
5133 "SYS_utimes", //138
5134 "SYS_139",
5135 "SYS_adjtime", //140
5136 "SYS_141",
5137 "SYS_142",
5138 "SYS_143",
5139 "SYS_144",
5140 "SYS_145",
5141 "SYS_146",
5142 "SYS_setsid", //147
5143 "SYS_quotactl", //148
5144 "SYS_149",
5145 "SYS_150",
5146 "SYS_151",
5147 "SYS_152",
5148 "SYS_153",
5149 "SYS_154",
5150 "SYS_nfssvc", //155
5151 "SYS_156",
5152 "SYS_157",
5153 "SYS_158",
5154 "SYS_159",
5155 "SYS_160",
5156 "SYS_getfh", //161
5157 "SYS_162",
5158 "SYS_163",
5159 "SYS_164",
5160 "SYS_sysarch", //165
5161 "SYS_166",
5162 "SYS_167",
5163 "SYS_168",
5164 "SYS_169",
5165 "SYS_170",
5166 "SYS_171",
5167 "SYS_172",
5168 "SYS_pread", //173
5169 "SYS_pwrite", //174
5170 "SYS_175",
5171 "SYS_176",
5172 "SYS_177",
5173 "SYS_178",
5174 "SYS_179",
5175 "SYS_180",
5176 "SYS_setgid", //181
5177 "SYS_setegid", //182
5178 "SYS_seteuid", //183
5179 "SYS_lfs_bmapv", //184
5180 "SYS_lfs_markv", //185
5181 "SYS_lfs_segclean", //186
5182 "SYS_lfs_segwait", //187
5183 "SYS_188",
5184 "SYS_189",
5185 "SYS_190",
5186 "SYS_pathconf", //191
5187 "SYS_fpathconf", //192
5188 "SYS_swapctl", //193
5189 "SYS_getrlimit", //194
5190 "SYS_setrlimit", //195
5191 "SYS_getdirentries", //196
5192 "SYS_mmap", //197
5193 "SYS___syscall", //198
5194 "SYS_lseek", //199
5195 "SYS_truncate", //200
5196 "SYS_ftruncate", //201
5197 "SYS___sysctl", //202
5198 "SYS_mlock", //203
5199 "SYS_munlock", //204
5200 "SYS_205",
5201 "SYS_futimes", //206
5202 "SYS_getpgid", //207
5203 "SYS_xfspioctl", //208
5204 "SYS_209",
5205 "SYS_210",
5206 "SYS_211",
5207 "SYS_212",
5208 "SYS_213",
5209 "SYS_214",
5210 "SYS_215",
5211 "SYS_216",
5212 "SYS_217",
5213 "SYS_218",
5214 "SYS_219",
5215 "SYS_220",
5216 "SYS_semget", //221
5217 "SYS_222",
5218 "SYS_223",
5219 "SYS_224",
5220 "SYS_msgget", //225
5221 "SYS_msgsnd", //226
5222 "SYS_msgrcv", //227
5223 "SYS_shmat", //228
5224 "SYS_229",
5225 "SYS_shmdt", //230
5226 "SYS_231",
5227 "SYS_clock_gettime", //232
5228 "SYS_clock_settime", //233
5229 "SYS_clock_getres", //234
5230 "SYS_235",
5231 "SYS_236",
5232 "SYS_237",
5233 "SYS_238",
5234 "SYS_239",
5235 "SYS_nanosleep", //240
5236 "SYS_241",
5237 "SYS_242",
5238 "SYS_243",
5239 "SYS_244",
5240 "SYS_245",
5241 "SYS_246",
5242 "SYS_247",
5243 "SYS_248",
5244 "SYS_249",
5245 "SYS_minherit", //250
5246 "SYS_rfork", //251
5247 "SYS_poll", //252
5248 "SYS_issetugid", //253
5249 "SYS_lchown", //254
5250 "SYS_getsid", //255
5251 "SYS_msync", //256
5252 "SYS_257",
5253 "SYS_258",
5254 "SYS_259",
5255 "SYS_getfsstat", //260
5256 "SYS_statfs", //261
5257 "SYS_fstatfs", //262
5258 "SYS_pipe", //263
5259 "SYS_fhopen", //264
5260 "SYS_265",
5261 "SYS_fhstatfs", //266
5262 "SYS_preadv", //267
5263 "SYS_pwritev", //268
5264 "SYS_kqueue", //269
5265 "SYS_kevent", //270
5266 "SYS_mlockall", //271
5267 "SYS_munlockall", //272
5268 "SYS_getpeereid", //273
5269 "SYS_274",
5270 "SYS_275",
5271 "SYS_276",
5272 "SYS_277",
5273 "SYS_278",
5274 "SYS_279",
5275 "SYS_280",
5276 "SYS_getresuid", //281
5277 "SYS_setresuid", //282
5278 "SYS_getresgid", //283
5279 "SYS_setresgid", //284
5280 "SYS_285",
5281 "SYS_mquery", //286
5282 "SYS_closefrom", //287
5283 "SYS_sigaltstack", //288
5284 "SYS_shmget", //289
5285 "SYS_semop", //290
5286 "SYS_stat", //291
5287 "SYS_fstat", //292
5288 "SYS_lstat", //293
5289 "SYS_fhstat", //294
5290 "SYS___semctl", //295
5291 "SYS_shmctl", //296
5292 "SYS_msgctl", //297
5293 "SYS_MAXSYSCALL", //298
5294 //299
5295 //300
5296 };
5297 uint32_t uEAX;
5298 if (!LogIsEnabled())
5299 return;
5300 uEAX = CPUMGetGuestEAX(pVM);
5301 switch (uEAX)
5302 {
5303 default:
5304 if (uEAX < ELEMENTS(apsz))
5305 {
5306 uint32_t au32Args[8] = {0};
5307 PGMPhysReadGCPtr(pVM, au32Args, CPUMGetGuestESP(pVM), sizeof(au32Args));
5308 RTLogPrintf("REM: OpenBSD syscall %3d: %s (eip=%08x %08x %08x %08x %08x %08x %08x %08x %08x)\n",
5309 uEAX, apsz[uEAX], CPUMGetGuestEIP(pVM), au32Args[0], au32Args[1], au32Args[2], au32Args[3],
5310 au32Args[4], au32Args[5], au32Args[6], au32Args[7]);
5311 }
5312 else
5313 RTLogPrintf("eip=%08x: OpenBSD syscall %d (#%x) unknown!!\n", CPUMGetGuestEIP(pVM), uEAX, uEAX);
5314 break;
5315 }
5316}
5317
5318
5319#if defined(IPRT_NO_CRT) && defined(RT_OS_WINDOWS) && defined(RT_ARCH_X86)
5320/**
5321 * The Dll main entry point (stub).
5322 */
5323bool __stdcall _DllMainCRTStartup(void *hModule, uint32_t dwReason, void *pvReserved)
5324{
5325 return true;
5326}
5327
5328void *memcpy(void *dst, const void *src, size_t size)
5329{
5330 uint8_t*pbDst = dst, *pbSrc = src;
5331 while (size-- > 0)
5332 *pbDst++ = *pbSrc++;
5333 return dst;
5334}
5335
5336#endif
5337
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