VirtualBox

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

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

Shadow ROM emulation. Clear the RESERVED flag for ROM.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 167.3 KB
Line 
1/* $Id: VBoxRecompiler.c 4388 2007-08-27 14:26:05Z 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 || pVM->rem.s.fInStateSync);
1346
1347 //RAWEx_ProfileStop(env, STATS_QEMU_TOTAL);
1348
1349 /*
1350 * Update the control registers before calling PGMFlushPage.
1351 */
1352 PCPUMCTX pCtx = (PCPUMCTX)pVM->rem.s.pCtx;
1353 pCtx->cr0 = env->cr[0];
1354 pCtx->cr3 = env->cr[3];
1355 pCtx->cr4 = env->cr[4];
1356
1357 /*
1358 * Let PGM do the rest.
1359 */
1360 int rc = PGMInvalidatePage(pVM, GCPtr);
1361 if (VBOX_FAILURE(rc))
1362 {
1363 AssertMsgFailed(("remR3FlushPage %x %x %x %d failed!!\n", GCPtr));
1364 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
1365 }
1366 //RAWEx_ProfileStart(env, STATS_QEMU_TOTAL);
1367}
1368
1369/**
1370 * Called from tlb_protect_code in order to write monitor a code page.
1371 *
1372 * @param env Pointer to the CPU environment.
1373 * @param GCPtr Code page to monitor
1374 */
1375void remR3ProtectCode(CPUState *env, RTGCPTR GCPtr)
1376{
1377 Assert(env->pVM->rem.s.fInREM);
1378 if ( (env->cr[0] & X86_CR0_PG) /* paging must be enabled */
1379 && !(env->state & CPU_EMULATE_SINGLE_INSTR) /* ignore during single instruction execution */
1380 && (((env->hflags >> HF_CPL_SHIFT) & 3) == 0) /* supervisor mode only */
1381 && !(env->eflags & VM_MASK) /* no V86 mode */
1382 && !HWACCMIsEnabled(env->pVM))
1383 CSAMR3MonitorPage(env->pVM, GCPtr, CSAM_TAG_REM);
1384}
1385
1386/**
1387 * Called when the CPU is initialized, any of the CRx registers are changed or
1388 * when the A20 line is modified.
1389 *
1390 * @param env Pointer to the CPU environment.
1391 * @param fGlobal Set if the flush is global.
1392 */
1393void remR3FlushTLB(CPUState *env, bool fGlobal)
1394{
1395 PVM pVM = env->pVM;
1396
1397 /*
1398 * When we're replaying invlpg instructions or restoring a saved
1399 * state we disable this path.
1400 */
1401 if (pVM->rem.s.fIgnoreCR3Load || pVM->rem.s.fIgnoreAll)
1402 return;
1403 Assert(pVM->rem.s.fInREM);
1404
1405 /*
1406 * The caller doesn't check cr4, so we have to do that for ourselves.
1407 */
1408 if (!fGlobal && !(env->cr[4] & X86_CR4_PGE))
1409 fGlobal = true;
1410 Log(("remR3FlushTLB: CR0=%VGp CR3=%VGp CR4=%VGp %s\n", env->cr[0], env->cr[3], env->cr[4], fGlobal ? " global" : ""));
1411
1412 /*
1413 * Update the control registers before calling PGMR3FlushTLB.
1414 */
1415 PCPUMCTX pCtx = (PCPUMCTX)pVM->rem.s.pCtx;
1416 pCtx->cr0 = env->cr[0];
1417 pCtx->cr3 = env->cr[3];
1418 pCtx->cr4 = env->cr[4];
1419
1420 /*
1421 * Let PGM do the rest.
1422 */
1423 PGMFlushTLB(pVM, env->cr[3], fGlobal);
1424}
1425
1426
1427/**
1428 * Called when any of the cr0, cr4 or efer registers is updated.
1429 *
1430 * @param env Pointer to the CPU environment.
1431 */
1432void remR3ChangeCpuMode(CPUState *env)
1433{
1434 int rc;
1435 PVM pVM = env->pVM;
1436
1437 /*
1438 * When we're replaying loads or restoring a saved
1439 * state this path is disabled.
1440 */
1441 if (pVM->rem.s.fIgnoreCpuMode || pVM->rem.s.fIgnoreAll)
1442 return;
1443 Assert(pVM->rem.s.fInREM);
1444
1445 /*
1446 * Update the control registers before calling PGMR3ChangeMode()
1447 * as it may need to map whatever cr3 is pointing to.
1448 */
1449 PCPUMCTX pCtx = (PCPUMCTX)pVM->rem.s.pCtx;
1450 pCtx->cr0 = env->cr[0];
1451 pCtx->cr3 = env->cr[3];
1452 pCtx->cr4 = env->cr[4];
1453
1454#ifdef TARGET_X86_64
1455 rc = PGMChangeMode(pVM, env->cr[0], env->cr[4], env->efer);
1456 if (rc != VINF_SUCCESS)
1457 cpu_abort(env, "PGMChangeMode(, %08x, %08x, %016llx) -> %Vrc\n", env->cr[0], env->cr[4], env->efer, rc);
1458#else
1459 rc = PGMChangeMode(pVM, env->cr[0], env->cr[4], 0);
1460 if (rc != VINF_SUCCESS)
1461 cpu_abort(env, "PGMChangeMode(, %08x, %08x, %016llx) -> %Vrc\n", env->cr[0], env->cr[4], 0LL, rc);
1462#endif
1463}
1464
1465
1466/**
1467 * Called from compiled code to run dma.
1468 *
1469 * @param env Pointer to the CPU environment.
1470 */
1471void remR3DmaRun(CPUState *env)
1472{
1473 remR3ProfileStop(STATS_QEMU_RUN_EMULATED_CODE);
1474 PDMR3DmaRun(env->pVM);
1475 remR3ProfileStart(STATS_QEMU_RUN_EMULATED_CODE);
1476}
1477
1478/**
1479 * Called from compiled code to schedule pending timers in VMM
1480 *
1481 * @param env Pointer to the CPU environment.
1482 */
1483void remR3TimersRun(CPUState *env)
1484{
1485 remR3ProfileStop(STATS_QEMU_RUN_EMULATED_CODE);
1486 remR3ProfileStart(STATS_QEMU_RUN_TIMERS);
1487 TMR3TimerQueuesDo(env->pVM);
1488 remR3ProfileStop(STATS_QEMU_RUN_TIMERS);
1489 remR3ProfileStart(STATS_QEMU_RUN_EMULATED_CODE);
1490}
1491
1492/**
1493 * Record trap occurance
1494 *
1495 * @returns VBox status code
1496 * @param env Pointer to the CPU environment.
1497 * @param uTrap Trap nr
1498 * @param uErrorCode Error code
1499 * @param pvNextEIP Next EIP
1500 */
1501int remR3NotifyTrap(CPUState *env, uint32_t uTrap, uint32_t uErrorCode, uint32_t pvNextEIP)
1502{
1503 PVM pVM = (PVM)env->pVM;
1504#ifdef VBOX_WITH_STATISTICS
1505 static STAMCOUNTER aStatTrap[255];
1506 static bool aRegisters[ELEMENTS(aStatTrap)];
1507#endif
1508
1509#ifdef VBOX_WITH_STATISTICS
1510 if (uTrap < 255)
1511 {
1512 if (!aRegisters[uTrap])
1513 {
1514 aRegisters[uTrap] = true;
1515 char szStatName[64];
1516 RTStrPrintf(szStatName, sizeof(szStatName), "/REM/Trap/0x%02X", uTrap);
1517 STAM_REG(env->pVM, &aStatTrap[uTrap], STAMTYPE_COUNTER, szStatName, STAMUNIT_OCCURENCES, "Trap stats.");
1518 }
1519 STAM_COUNTER_INC(&aStatTrap[uTrap]);
1520 }
1521#endif
1522 Log(("remR3NotifyTrap: uTrap=%x error=%x next_eip=%VGv eip=%VGv cr2=%08x\n", uTrap, uErrorCode, pvNextEIP, env->eip, env->cr[2]));
1523 if( uTrap < 0x20
1524 && (env->cr[0] & X86_CR0_PE)
1525 && !(env->eflags & X86_EFL_VM))
1526 {
1527#ifdef DEBUG
1528 remR3DisasInstr(env, 1, "remR3NotifyTrap: ");
1529#endif
1530 if(pVM->rem.s.uPendingException == uTrap && ++pVM->rem.s.cPendingExceptions > 512)
1531 {
1532 LogRel(("VERR_REM_TOO_MANY_TRAPS -> uTrap=%x error=%x next_eip=%VGv eip=%VGv cr2=%08x\n", uTrap, uErrorCode, pvNextEIP, env->eip, env->cr[2]));
1533 remR3RaiseRC(env->pVM, VERR_REM_TOO_MANY_TRAPS);
1534 return VERR_REM_TOO_MANY_TRAPS;
1535 }
1536 if(pVM->rem.s.uPendingException != uTrap || pVM->rem.s.uPendingExcptEIP != env->eip || pVM->rem.s.uPendingExcptCR2 != env->cr[2])
1537 pVM->rem.s.cPendingExceptions = 1;
1538 pVM->rem.s.uPendingException = uTrap;
1539 pVM->rem.s.uPendingExcptEIP = env->eip;
1540 pVM->rem.s.uPendingExcptCR2 = env->cr[2];
1541 }
1542 else
1543 {
1544 pVM->rem.s.cPendingExceptions = 0;
1545 pVM->rem.s.uPendingException = uTrap;
1546 pVM->rem.s.uPendingExcptEIP = env->eip;
1547 pVM->rem.s.uPendingExcptCR2 = env->cr[2];
1548 }
1549 return VINF_SUCCESS;
1550}
1551
1552/*
1553 * Clear current active trap
1554 *
1555 * @param pVM VM Handle.
1556 */
1557void remR3TrapClear(PVM pVM)
1558{
1559 pVM->rem.s.cPendingExceptions = 0;
1560 pVM->rem.s.uPendingException = 0;
1561 pVM->rem.s.uPendingExcptEIP = 0;
1562 pVM->rem.s.uPendingExcptCR2 = 0;
1563}
1564
1565
1566/**
1567 * Syncs the internal REM state with the VM.
1568 *
1569 * This must be called before REMR3Run() is invoked whenever when the REM
1570 * state is not up to date. Calling it several times in a row is not
1571 * permitted.
1572 *
1573 * @returns VBox status code.
1574 *
1575 * @param pVM VM Handle.
1576 *
1577 * @remark The caller has to check for important FFs before calling REMR3Run. REMR3State will
1578 * no do this since the majority of the callers don't want any unnecessary of events
1579 * pending that would immediatly interrupt execution.
1580 */
1581REMR3DECL(int) REMR3State(PVM pVM)
1582{
1583 Log2(("REMR3State:\n"));
1584 STAM_PROFILE_START(&pVM->rem.s.StatsState, a);
1585 register const CPUMCTX *pCtx = pVM->rem.s.pCtx;
1586 register unsigned fFlags;
1587 bool fHiddenSelRegsValid = CPUMAreHiddenSelRegsValid(pVM);
1588
1589 Assert(!pVM->rem.s.fInREM);
1590 pVM->rem.s.fInStateSync = true;
1591
1592 /*
1593 * Copy the registers which requires no special handling.
1594 */
1595 Assert(R_EAX == 0);
1596 pVM->rem.s.Env.regs[R_EAX] = pCtx->eax;
1597 Assert(R_ECX == 1);
1598 pVM->rem.s.Env.regs[R_ECX] = pCtx->ecx;
1599 Assert(R_EDX == 2);
1600 pVM->rem.s.Env.regs[R_EDX] = pCtx->edx;
1601 Assert(R_EBX == 3);
1602 pVM->rem.s.Env.regs[R_EBX] = pCtx->ebx;
1603 Assert(R_ESP == 4);
1604 pVM->rem.s.Env.regs[R_ESP] = pCtx->esp;
1605 Assert(R_EBP == 5);
1606 pVM->rem.s.Env.regs[R_EBP] = pCtx->ebp;
1607 Assert(R_ESI == 6);
1608 pVM->rem.s.Env.regs[R_ESI] = pCtx->esi;
1609 Assert(R_EDI == 7);
1610 pVM->rem.s.Env.regs[R_EDI] = pCtx->edi;
1611 pVM->rem.s.Env.eip = pCtx->eip;
1612
1613 pVM->rem.s.Env.eflags = pCtx->eflags.u32;
1614
1615 pVM->rem.s.Env.cr[2] = pCtx->cr2;
1616
1617 /** @todo we could probably benefit from using a CPUM_CHANGED_DRx flag too! */
1618 pVM->rem.s.Env.dr[0] = pCtx->dr0;
1619 pVM->rem.s.Env.dr[1] = pCtx->dr1;
1620 pVM->rem.s.Env.dr[2] = pCtx->dr2;
1621 pVM->rem.s.Env.dr[3] = pCtx->dr3;
1622 pVM->rem.s.Env.dr[4] = pCtx->dr4;
1623 pVM->rem.s.Env.dr[5] = pCtx->dr5;
1624 pVM->rem.s.Env.dr[6] = pCtx->dr6;
1625 pVM->rem.s.Env.dr[7] = pCtx->dr7;
1626
1627 /*
1628 * Clear the halted hidden flag (the interrupt waking up the CPU can
1629 * have been dispatched in raw mode).
1630 */
1631 pVM->rem.s.Env.hflags &= ~HF_HALTED_MASK;
1632
1633 /*
1634 * Replay invlpg?
1635 */
1636 if (pVM->rem.s.cInvalidatedPages)
1637 {
1638 pVM->rem.s.fIgnoreInvlPg = true;
1639 RTUINT i;
1640 for (i = 0; i < pVM->rem.s.cInvalidatedPages; i++)
1641 {
1642 Log2(("REMR3State: invlpg %VGv\n", pVM->rem.s.aGCPtrInvalidatedPages[i]));
1643 tlb_flush_page(&pVM->rem.s.Env, pVM->rem.s.aGCPtrInvalidatedPages[i]);
1644 }
1645 pVM->rem.s.fIgnoreInvlPg = false;
1646 pVM->rem.s.cInvalidatedPages = 0;
1647 }
1648
1649 /*
1650 * Registers which are rarely changed and require special handling / order when changed.
1651 */
1652 fFlags = CPUMGetAndClearChangedFlagsREM(pVM);
1653 if (fFlags & ( CPUM_CHANGED_CR4 | CPUM_CHANGED_CR3 | CPUM_CHANGED_CR0
1654 | CPUM_CHANGED_GDTR | CPUM_CHANGED_IDTR | CPUM_CHANGED_LDTR | CPUM_CHANGED_TR
1655 | CPUM_CHANGED_FPU_REM | CPUM_CHANGED_SYSENTER_MSR))
1656 {
1657 if (fFlags & CPUM_CHANGED_FPU_REM)
1658 save_raw_fp_state(&pVM->rem.s.Env, (uint8_t *)&pCtx->fpu); /* 'save' is an excellent name. */
1659
1660 if (fFlags & CPUM_CHANGED_GLOBAL_TLB_FLUSH)
1661 {
1662 pVM->rem.s.fIgnoreCR3Load = true;
1663 tlb_flush(&pVM->rem.s.Env, true);
1664 pVM->rem.s.fIgnoreCR3Load = false;
1665 }
1666
1667 if (fFlags & CPUM_CHANGED_CR4)
1668 {
1669 pVM->rem.s.fIgnoreCR3Load = true;
1670 pVM->rem.s.fIgnoreCpuMode = true;
1671 cpu_x86_update_cr4(&pVM->rem.s.Env, pCtx->cr4);
1672 pVM->rem.s.fIgnoreCpuMode = false;
1673 pVM->rem.s.fIgnoreCR3Load = false;
1674 }
1675
1676 if (fFlags & CPUM_CHANGED_CR0)
1677 {
1678 pVM->rem.s.fIgnoreCR3Load = true;
1679 pVM->rem.s.fIgnoreCpuMode = true;
1680 cpu_x86_update_cr0(&pVM->rem.s.Env, pCtx->cr0);
1681 pVM->rem.s.fIgnoreCpuMode = false;
1682 pVM->rem.s.fIgnoreCR3Load = false;
1683 }
1684
1685 if (fFlags & CPUM_CHANGED_CR3)
1686 {
1687 pVM->rem.s.fIgnoreCR3Load = true;
1688 cpu_x86_update_cr3(&pVM->rem.s.Env, pCtx->cr3);
1689 pVM->rem.s.fIgnoreCR3Load = false;
1690 }
1691
1692 if (fFlags & CPUM_CHANGED_GDTR)
1693 {
1694 pVM->rem.s.Env.gdt.base = pCtx->gdtr.pGdt;
1695 pVM->rem.s.Env.gdt.limit = pCtx->gdtr.cbGdt;
1696 }
1697
1698 if (fFlags & CPUM_CHANGED_IDTR)
1699 {
1700 pVM->rem.s.Env.idt.base = pCtx->idtr.pIdt;
1701 pVM->rem.s.Env.idt.limit = pCtx->idtr.cbIdt;
1702 }
1703
1704 if (fFlags & CPUM_CHANGED_SYSENTER_MSR)
1705 {
1706 pVM->rem.s.Env.sysenter_cs = pCtx->SysEnter.cs;
1707 pVM->rem.s.Env.sysenter_eip = pCtx->SysEnter.eip;
1708 pVM->rem.s.Env.sysenter_esp = pCtx->SysEnter.esp;
1709 }
1710
1711 if (fFlags & CPUM_CHANGED_LDTR)
1712 {
1713 if (fHiddenSelRegsValid)
1714 {
1715 pVM->rem.s.Env.ldt.selector = pCtx->ldtr;
1716 pVM->rem.s.Env.ldt.base = pCtx->ldtrHid.u32Base;
1717 pVM->rem.s.Env.ldt.limit = pCtx->ldtrHid.u32Limit;
1718 pVM->rem.s.Env.ldt.flags = (pCtx->ldtrHid.Attr.u << 8) & 0xFFFFFF;;
1719 }
1720 else
1721 sync_ldtr(&pVM->rem.s.Env, pCtx->ldtr);
1722 }
1723
1724 if (fFlags & CPUM_CHANGED_TR)
1725 {
1726 if (fHiddenSelRegsValid)
1727 {
1728 pVM->rem.s.Env.tr.selector = pCtx->tr;
1729 pVM->rem.s.Env.tr.base = pCtx->trHid.u32Base;
1730 pVM->rem.s.Env.tr.limit = pCtx->trHid.u32Limit;
1731 pVM->rem.s.Env.tr.flags = (pCtx->trHid.Attr.u << 8) & 0xFFFFFF;;
1732 }
1733 else
1734 sync_tr(&pVM->rem.s.Env, pCtx->tr);
1735
1736 /** @note do_interrupt will fault if the busy flag is still set.... */
1737 pVM->rem.s.Env.tr.flags &= ~DESC_TSS_BUSY_MASK;
1738 }
1739 }
1740
1741 /*
1742 * Update selector registers.
1743 * This must be done *after* we've synced gdt, ldt and crX registers
1744 * since we're reading the GDT/LDT om sync_seg. This will happen with
1745 * saved state which takes a quick dip into rawmode for instance.
1746 */
1747 /*
1748 * Stack; Note first check this one as the CPL might have changed. The
1749 * wrong CPL can cause QEmu to raise an exception in sync_seg!!
1750 */
1751
1752 if (fHiddenSelRegsValid)
1753 {
1754 /* The hidden selector registers are valid in the CPU context. */
1755 /** @note QEmu saves the 2nd dword of the descriptor; we should convert the attribute word back! */
1756
1757 /* Set current CPL */
1758 cpu_x86_set_cpl(&pVM->rem.s.Env, CPUMGetGuestCPL(pVM, CPUMCTX2CORE(pCtx)));
1759
1760 cpu_x86_load_seg_cache(&pVM->rem.s.Env, R_CS, pCtx->cs, pCtx->csHid.u32Base, pCtx->csHid.u32Limit, (pCtx->csHid.Attr.u << 8) & 0xFFFFFF);
1761 cpu_x86_load_seg_cache(&pVM->rem.s.Env, R_SS, pCtx->ss, pCtx->ssHid.u32Base, pCtx->ssHid.u32Limit, (pCtx->ssHid.Attr.u << 8) & 0xFFFFFF);
1762 cpu_x86_load_seg_cache(&pVM->rem.s.Env, R_DS, pCtx->ds, pCtx->dsHid.u32Base, pCtx->dsHid.u32Limit, (pCtx->dsHid.Attr.u << 8) & 0xFFFFFF);
1763 cpu_x86_load_seg_cache(&pVM->rem.s.Env, R_ES, pCtx->es, pCtx->esHid.u32Base, pCtx->esHid.u32Limit, (pCtx->esHid.Attr.u << 8) & 0xFFFFFF);
1764 cpu_x86_load_seg_cache(&pVM->rem.s.Env, R_FS, pCtx->fs, pCtx->fsHid.u32Base, pCtx->fsHid.u32Limit, (pCtx->fsHid.Attr.u << 8) & 0xFFFFFF);
1765 cpu_x86_load_seg_cache(&pVM->rem.s.Env, R_GS, pCtx->gs, pCtx->gsHid.u32Base, pCtx->gsHid.u32Limit, (pCtx->gsHid.Attr.u << 8) & 0xFFFFFF);
1766 }
1767 else
1768 {
1769 /* In 'normal' raw mode we don't have access to the hidden selector registers. */
1770 if (pVM->rem.s.Env.segs[R_SS].selector != (uint16_t)pCtx->ss)
1771 {
1772 Log2(("REMR3State: SS changed from %04x to %04x!\n", pVM->rem.s.Env.segs[R_SS].selector, pCtx->ss));
1773
1774 cpu_x86_set_cpl(&pVM->rem.s.Env, (pCtx->eflags.Bits.u1VM) ? 3 : (pCtx->ss & 3));
1775 sync_seg(&pVM->rem.s.Env, R_SS, pCtx->ss);
1776#ifdef VBOX_WITH_STATISTICS
1777 if (pVM->rem.s.Env.segs[R_SS].newselector)
1778 {
1779 STAM_COUNTER_INC(&gStatSelOutOfSync[R_SS]);
1780 }
1781#endif
1782 }
1783 else
1784 pVM->rem.s.Env.segs[R_SS].newselector = 0;
1785
1786 if (pVM->rem.s.Env.segs[R_ES].selector != pCtx->es)
1787 {
1788 Log2(("REMR3State: ES changed from %04x to %04x!\n", pVM->rem.s.Env.segs[R_ES].selector, pCtx->es));
1789 sync_seg(&pVM->rem.s.Env, R_ES, pCtx->es);
1790#ifdef VBOX_WITH_STATISTICS
1791 if (pVM->rem.s.Env.segs[R_ES].newselector)
1792 {
1793 STAM_COUNTER_INC(&gStatSelOutOfSync[R_ES]);
1794 }
1795#endif
1796 }
1797 else
1798 pVM->rem.s.Env.segs[R_ES].newselector = 0;
1799
1800 if (pVM->rem.s.Env.segs[R_CS].selector != pCtx->cs)
1801 {
1802 Log2(("REMR3State: CS changed from %04x to %04x!\n", pVM->rem.s.Env.segs[R_CS].selector, pCtx->cs));
1803 sync_seg(&pVM->rem.s.Env, R_CS, pCtx->cs);
1804#ifdef VBOX_WITH_STATISTICS
1805 if (pVM->rem.s.Env.segs[R_CS].newselector)
1806 {
1807 STAM_COUNTER_INC(&gStatSelOutOfSync[R_CS]);
1808 }
1809#endif
1810 }
1811 else
1812 pVM->rem.s.Env.segs[R_CS].newselector = 0;
1813
1814 if (pVM->rem.s.Env.segs[R_DS].selector != pCtx->ds)
1815 {
1816 Log2(("REMR3State: DS changed from %04x to %04x!\n", pVM->rem.s.Env.segs[R_DS].selector, pCtx->ds));
1817 sync_seg(&pVM->rem.s.Env, R_DS, pCtx->ds);
1818#ifdef VBOX_WITH_STATISTICS
1819 if (pVM->rem.s.Env.segs[R_DS].newselector)
1820 {
1821 STAM_COUNTER_INC(&gStatSelOutOfSync[R_DS]);
1822 }
1823#endif
1824 }
1825 else
1826 pVM->rem.s.Env.segs[R_DS].newselector = 0;
1827
1828 /** @todo need to find a way to communicate potential GDT/LDT changes and thread switches. The selector might
1829 * be the same but not the base/limit. */
1830 if (pVM->rem.s.Env.segs[R_FS].selector != pCtx->fs)
1831 {
1832 Log2(("REMR3State: FS changed from %04x to %04x!\n", pVM->rem.s.Env.segs[R_FS].selector, pCtx->fs));
1833 sync_seg(&pVM->rem.s.Env, R_FS, pCtx->fs);
1834#ifdef VBOX_WITH_STATISTICS
1835 if (pVM->rem.s.Env.segs[R_FS].newselector)
1836 {
1837 STAM_COUNTER_INC(&gStatSelOutOfSync[R_FS]);
1838 }
1839#endif
1840 }
1841 else
1842 pVM->rem.s.Env.segs[R_FS].newselector = 0;
1843
1844 if (pVM->rem.s.Env.segs[R_GS].selector != pCtx->gs)
1845 {
1846 Log2(("REMR3State: GS changed from %04x to %04x!\n", pVM->rem.s.Env.segs[R_GS].selector, pCtx->gs));
1847 sync_seg(&pVM->rem.s.Env, R_GS, pCtx->gs);
1848#ifdef VBOX_WITH_STATISTICS
1849 if (pVM->rem.s.Env.segs[R_GS].newselector)
1850 {
1851 STAM_COUNTER_INC(&gStatSelOutOfSync[R_GS]);
1852 }
1853#endif
1854 }
1855 else
1856 pVM->rem.s.Env.segs[R_GS].newselector = 0;
1857 }
1858
1859 /*
1860 * Check for traps.
1861 */
1862 pVM->rem.s.Env.exception_index = -1; /** @todo this won't work :/ */
1863 TRPMEVENT enmType;
1864 uint8_t u8TrapNo;
1865 int rc = TRPMQueryTrap(pVM, &u8TrapNo, &enmType);
1866 if (VBOX_SUCCESS(rc))
1867 {
1868 #ifdef DEBUG
1869 if (u8TrapNo == 0x80)
1870 {
1871 remR3DumpLnxSyscall(pVM);
1872 remR3DumpOBsdSyscall(pVM);
1873 }
1874 #endif
1875
1876 pVM->rem.s.Env.exception_index = u8TrapNo;
1877 if (enmType != TRPM_SOFTWARE_INT)
1878 {
1879 pVM->rem.s.Env.exception_is_int = 0;
1880 pVM->rem.s.Env.exception_next_eip = pVM->rem.s.Env.eip;
1881 }
1882 else
1883 {
1884 /*
1885 * The there are two 1 byte opcodes and one 2 byte opcode for software interrupts.
1886 * We ASSUME that there are no prefixes and sets the default to 2 byte, and checks
1887 * for int03 and into.
1888 */
1889 pVM->rem.s.Env.exception_is_int = 1;
1890 pVM->rem.s.Env.exception_next_eip = pCtx->eip + 2;
1891 /* int 3 may be generated by one-byte 0xcc */
1892 if (u8TrapNo == 3)
1893 {
1894 if (read_byte(&pVM->rem.s.Env, pVM->rem.s.Env.segs[R_CS].base + pCtx->eip) == 0xcc)
1895 pVM->rem.s.Env.exception_next_eip = pCtx->eip + 1;
1896 }
1897 /* int 4 may be generated by one-byte 0xce */
1898 else if (u8TrapNo == 4)
1899 {
1900 if (read_byte(&pVM->rem.s.Env, pVM->rem.s.Env.segs[R_CS].base + pCtx->eip) == 0xce)
1901 pVM->rem.s.Env.exception_next_eip = pCtx->eip + 1;
1902 }
1903 }
1904
1905 /* get error code and cr2 if needed. */
1906 switch (u8TrapNo)
1907 {
1908 case 0x0e:
1909 pVM->rem.s.Env.cr[2] = TRPMGetFaultAddress(pVM);
1910 /* fallthru */
1911 case 0x0a: case 0x0b: case 0x0c: case 0x0d:
1912 pVM->rem.s.Env.error_code = TRPMGetErrorCode(pVM);
1913 break;
1914
1915 case 0x11: case 0x08:
1916 default:
1917 pVM->rem.s.Env.error_code = 0;
1918 break;
1919 }
1920
1921 /*
1922 * We can now reset the active trap since the recompiler is gonna have a go at it.
1923 */
1924 rc = TRPMResetTrap(pVM);
1925 AssertRC(rc);
1926 Log2(("REMR3State: trap=%02x errcd=%VGv cr2=%VGv nexteip=%VGv%s\n", pVM->rem.s.Env.exception_index, pVM->rem.s.Env.error_code,
1927 pVM->rem.s.Env.cr[2], pVM->rem.s.Env.exception_next_eip, pVM->rem.s.Env.exception_is_int ? " software" : ""));
1928 }
1929
1930 /*
1931 * Clear old interrupt request flags; Check for pending hardware interrupts.
1932 * (See @remark for why we don't check for other FFs.)
1933 */
1934 pVM->rem.s.Env.interrupt_request &= ~(CPU_INTERRUPT_HARD | CPU_INTERRUPT_EXIT | CPU_INTERRUPT_EXITTB | CPU_INTERRUPT_TIMER);
1935 if ( pVM->rem.s.u32PendingInterrupt != REM_NO_PENDING_IRQ
1936 || VM_FF_ISPENDING(pVM, VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC))
1937 pVM->rem.s.Env.interrupt_request |= CPU_INTERRUPT_HARD;
1938
1939 /*
1940 * We're now in REM mode.
1941 */
1942 pVM->rem.s.fInREM = true;
1943 pVM->rem.s.fInStateSync = false;
1944 pVM->rem.s.cCanExecuteRaw = 0;
1945 STAM_PROFILE_STOP(&pVM->rem.s.StatsState, a);
1946 Log2(("REMR3State: returns VINF_SUCCESS\n"));
1947 return VINF_SUCCESS;
1948}
1949
1950
1951/**
1952 * Syncs back changes in the REM state to the the VM state.
1953 *
1954 * This must be called after invoking REMR3Run().
1955 * Calling it several times in a row is not permitted.
1956 *
1957 * @returns VBox status code.
1958 *
1959 * @param pVM VM Handle.
1960 */
1961REMR3DECL(int) REMR3StateBack(PVM pVM)
1962{
1963 Log2(("REMR3StateBack:\n"));
1964 Assert(pVM->rem.s.fInREM);
1965 STAM_PROFILE_START(&pVM->rem.s.StatsStateBack, a);
1966 register PCPUMCTX pCtx = pVM->rem.s.pCtx;
1967
1968 /*
1969 * Copy back the registers.
1970 * This is done in the order they are declared in the CPUMCTX structure.
1971 */
1972
1973 /** @todo FOP */
1974 /** @todo FPUIP */
1975 /** @todo CS */
1976 /** @todo FPUDP */
1977 /** @todo DS */
1978 /** @todo Fix MXCSR support in QEMU so we don't overwrite MXCSR with 0 when we shouldn't! */
1979 pCtx->fpu.MXCSR = 0;
1980 pCtx->fpu.MXCSR_MASK = 0;
1981
1982 /** @todo check if FPU/XMM was actually used in the recompiler */
1983 restore_raw_fp_state(&pVM->rem.s.Env, (uint8_t *)&pCtx->fpu);
1984//// dprintf2(("FPU state CW=%04X TT=%04X SW=%04X (%04X)\n", env->fpuc, env->fpstt, env->fpus, pVMCtx->fpu.FSW));
1985
1986 pCtx->edi = pVM->rem.s.Env.regs[R_EDI];
1987 pCtx->esi = pVM->rem.s.Env.regs[R_ESI];
1988 pCtx->ebp = pVM->rem.s.Env.regs[R_EBP];
1989 pCtx->eax = pVM->rem.s.Env.regs[R_EAX];
1990 pCtx->ebx = pVM->rem.s.Env.regs[R_EBX];
1991 pCtx->edx = pVM->rem.s.Env.regs[R_EDX];
1992 pCtx->ecx = pVM->rem.s.Env.regs[R_ECX];
1993
1994 pCtx->esp = pVM->rem.s.Env.regs[R_ESP];
1995 pCtx->ss = pVM->rem.s.Env.segs[R_SS].selector;
1996
1997#ifdef VBOX_WITH_STATISTICS
1998 if (pVM->rem.s.Env.segs[R_SS].newselector)
1999 {
2000 STAM_COUNTER_INC(&gStatSelOutOfSyncStateBack[R_SS]);
2001 }
2002 if (pVM->rem.s.Env.segs[R_GS].newselector)
2003 {
2004 STAM_COUNTER_INC(&gStatSelOutOfSyncStateBack[R_GS]);
2005 }
2006 if (pVM->rem.s.Env.segs[R_FS].newselector)
2007 {
2008 STAM_COUNTER_INC(&gStatSelOutOfSyncStateBack[R_FS]);
2009 }
2010 if (pVM->rem.s.Env.segs[R_ES].newselector)
2011 {
2012 STAM_COUNTER_INC(&gStatSelOutOfSyncStateBack[R_ES]);
2013 }
2014 if (pVM->rem.s.Env.segs[R_DS].newselector)
2015 {
2016 STAM_COUNTER_INC(&gStatSelOutOfSyncStateBack[R_DS]);
2017 }
2018 if (pVM->rem.s.Env.segs[R_CS].newselector)
2019 {
2020 STAM_COUNTER_INC(&gStatSelOutOfSyncStateBack[R_CS]);
2021 }
2022#endif
2023 pCtx->gs = pVM->rem.s.Env.segs[R_GS].selector;
2024 pCtx->fs = pVM->rem.s.Env.segs[R_FS].selector;
2025 pCtx->es = pVM->rem.s.Env.segs[R_ES].selector;
2026 pCtx->ds = pVM->rem.s.Env.segs[R_DS].selector;
2027 pCtx->cs = pVM->rem.s.Env.segs[R_CS].selector;
2028
2029 pCtx->eip = pVM->rem.s.Env.eip;
2030 pCtx->eflags.u32 = pVM->rem.s.Env.eflags;
2031
2032 pCtx->cr0 = pVM->rem.s.Env.cr[0];
2033 pCtx->cr2 = pVM->rem.s.Env.cr[2];
2034 pCtx->cr3 = pVM->rem.s.Env.cr[3];
2035 pCtx->cr4 = pVM->rem.s.Env.cr[4];
2036
2037 pCtx->dr0 = pVM->rem.s.Env.dr[0];
2038 pCtx->dr1 = pVM->rem.s.Env.dr[1];
2039 pCtx->dr2 = pVM->rem.s.Env.dr[2];
2040 pCtx->dr3 = pVM->rem.s.Env.dr[3];
2041 pCtx->dr4 = pVM->rem.s.Env.dr[4];
2042 pCtx->dr5 = pVM->rem.s.Env.dr[5];
2043 pCtx->dr6 = pVM->rem.s.Env.dr[6];
2044 pCtx->dr7 = pVM->rem.s.Env.dr[7];
2045
2046 pCtx->gdtr.cbGdt = pVM->rem.s.Env.gdt.limit;
2047 if (pCtx->gdtr.pGdt != (uint32_t)pVM->rem.s.Env.gdt.base)
2048 {
2049 pCtx->gdtr.pGdt = (uint32_t)pVM->rem.s.Env.gdt.base;
2050 STAM_COUNTER_INC(&gStatREMGDTChange);
2051 VM_FF_SET(pVM, VM_FF_SELM_SYNC_GDT);
2052 }
2053
2054 pCtx->idtr.cbIdt = pVM->rem.s.Env.idt.limit;
2055 if (pCtx->idtr.pIdt != (uint32_t)pVM->rem.s.Env.idt.base)
2056 {
2057 pCtx->idtr.pIdt = (uint32_t)pVM->rem.s.Env.idt.base;
2058 STAM_COUNTER_INC(&gStatREMIDTChange);
2059 VM_FF_SET(pVM, VM_FF_TRPM_SYNC_IDT);
2060 }
2061
2062 if (pCtx->ldtr != pVM->rem.s.Env.ldt.selector)
2063 {
2064 pCtx->ldtr = pVM->rem.s.Env.ldt.selector;
2065 STAM_COUNTER_INC(&gStatREMLDTRChange);
2066 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
2067 }
2068 if (pCtx->tr != pVM->rem.s.Env.tr.selector)
2069 {
2070 pCtx->tr = pVM->rem.s.Env.tr.selector;
2071 STAM_COUNTER_INC(&gStatREMTRChange);
2072 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
2073 }
2074
2075 /** @todo These values could still be out of sync! */
2076 pCtx->csHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_CS].base;
2077 pCtx->csHid.u32Limit = pVM->rem.s.Env.segs[R_CS].limit;
2078 /** @note QEmu saves the 2nd dword of the descriptor; we should store the attribute word only! */
2079 pCtx->csHid.Attr.u = (pVM->rem.s.Env.segs[R_CS].flags >> 8) & 0xF0FF;
2080
2081 pCtx->dsHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_DS].base;
2082 pCtx->dsHid.u32Limit = pVM->rem.s.Env.segs[R_DS].limit;
2083 pCtx->dsHid.Attr.u = (pVM->rem.s.Env.segs[R_DS].flags >> 8) & 0xF0FF;
2084
2085 pCtx->esHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_ES].base;
2086 pCtx->esHid.u32Limit = pVM->rem.s.Env.segs[R_ES].limit;
2087 pCtx->esHid.Attr.u = (pVM->rem.s.Env.segs[R_ES].flags >> 8) & 0xF0FF;
2088
2089 pCtx->fsHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_FS].base;
2090 pCtx->fsHid.u32Limit = pVM->rem.s.Env.segs[R_FS].limit;
2091 pCtx->fsHid.Attr.u = (pVM->rem.s.Env.segs[R_FS].flags >> 8) & 0xF0FF;
2092
2093 pCtx->gsHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_GS].base;
2094 pCtx->gsHid.u32Limit = pVM->rem.s.Env.segs[R_GS].limit;
2095 pCtx->gsHid.Attr.u = (pVM->rem.s.Env.segs[R_GS].flags >> 8) & 0xF0FF;
2096
2097 pCtx->ssHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_SS].base;
2098 pCtx->ssHid.u32Limit = pVM->rem.s.Env.segs[R_SS].limit;
2099 pCtx->ssHid.Attr.u = (pVM->rem.s.Env.segs[R_SS].flags >> 8) & 0xF0FF;
2100
2101 pCtx->ldtrHid.u32Base = (uint32_t)pVM->rem.s.Env.ldt.base;
2102 pCtx->ldtrHid.u32Limit = pVM->rem.s.Env.ldt.limit;
2103 pCtx->ldtrHid.Attr.u = (pVM->rem.s.Env.ldt.flags >> 8) & 0xF0FF;
2104
2105 pCtx->trHid.u32Base = (uint32_t)pVM->rem.s.Env.tr.base;
2106 pCtx->trHid.u32Limit = pVM->rem.s.Env.tr.limit;
2107 pCtx->trHid.Attr.u = (pVM->rem.s.Env.tr.flags >> 8) & 0xF0FF;
2108
2109 /* Sysenter MSR */
2110 pCtx->SysEnter.cs = pVM->rem.s.Env.sysenter_cs;
2111 pCtx->SysEnter.eip = pVM->rem.s.Env.sysenter_eip;
2112 pCtx->SysEnter.esp = pVM->rem.s.Env.sysenter_esp;
2113
2114 remR3TrapClear(pVM);
2115
2116 /*
2117 * Check for traps.
2118 */
2119 if ( pVM->rem.s.Env.exception_index >= 0
2120 && pVM->rem.s.Env.exception_index < 256)
2121 {
2122 Log(("REMR3StateBack: Pending trap %x %d\n", pVM->rem.s.Env.exception_index, pVM->rem.s.Env.exception_is_int));
2123 int rc = TRPMAssertTrap(pVM, pVM->rem.s.Env.exception_index, (pVM->rem.s.Env.exception_is_int) ? TRPM_SOFTWARE_INT : TRPM_HARDWARE_INT);
2124 AssertRC(rc);
2125 switch (pVM->rem.s.Env.exception_index)
2126 {
2127 case 0x0e:
2128 TRPMSetFaultAddress(pVM, pCtx->cr2);
2129 /* fallthru */
2130 case 0x0a: case 0x0b: case 0x0c: case 0x0d:
2131 case 0x11: case 0x08: /* 0 */
2132 TRPMSetErrorCode(pVM, pVM->rem.s.Env.error_code);
2133 break;
2134 }
2135
2136 }
2137
2138 /*
2139 * We're not longer in REM mode.
2140 */
2141 pVM->rem.s.fInREM = false;
2142 STAM_PROFILE_STOP(&pVM->rem.s.StatsStateBack, a);
2143 Log2(("REMR3StateBack: returns VINF_SUCCESS\n"));
2144 return VINF_SUCCESS;
2145}
2146
2147
2148/**
2149 * This is called by the disassembler when it wants to update the cpu state
2150 * before for instance doing a register dump.
2151 */
2152static void remR3StateUpdate(PVM pVM)
2153{
2154 Assert(pVM->rem.s.fInREM);
2155 register PCPUMCTX pCtx = pVM->rem.s.pCtx;
2156
2157 /*
2158 * Copy back the registers.
2159 * This is done in the order they are declared in the CPUMCTX structure.
2160 */
2161
2162 /** @todo FOP */
2163 /** @todo FPUIP */
2164 /** @todo CS */
2165 /** @todo FPUDP */
2166 /** @todo DS */
2167 /** @todo Fix MXCSR support in QEMU so we don't overwrite MXCSR with 0 when we shouldn't! */
2168 pCtx->fpu.MXCSR = 0;
2169 pCtx->fpu.MXCSR_MASK = 0;
2170
2171 /** @todo check if FPU/XMM was actually used in the recompiler */
2172 restore_raw_fp_state(&pVM->rem.s.Env, (uint8_t *)&pCtx->fpu);
2173//// dprintf2(("FPU state CW=%04X TT=%04X SW=%04X (%04X)\n", env->fpuc, env->fpstt, env->fpus, pVMCtx->fpu.FSW));
2174
2175 pCtx->edi = pVM->rem.s.Env.regs[R_EDI];
2176 pCtx->esi = pVM->rem.s.Env.regs[R_ESI];
2177 pCtx->ebp = pVM->rem.s.Env.regs[R_EBP];
2178 pCtx->eax = pVM->rem.s.Env.regs[R_EAX];
2179 pCtx->ebx = pVM->rem.s.Env.regs[R_EBX];
2180 pCtx->edx = pVM->rem.s.Env.regs[R_EDX];
2181 pCtx->ecx = pVM->rem.s.Env.regs[R_ECX];
2182
2183 pCtx->esp = pVM->rem.s.Env.regs[R_ESP];
2184 pCtx->ss = pVM->rem.s.Env.segs[R_SS].selector;
2185
2186 pCtx->gs = pVM->rem.s.Env.segs[R_GS].selector;
2187 pCtx->fs = pVM->rem.s.Env.segs[R_FS].selector;
2188 pCtx->es = pVM->rem.s.Env.segs[R_ES].selector;
2189 pCtx->ds = pVM->rem.s.Env.segs[R_DS].selector;
2190 pCtx->cs = pVM->rem.s.Env.segs[R_CS].selector;
2191
2192 pCtx->eip = pVM->rem.s.Env.eip;
2193 pCtx->eflags.u32 = pVM->rem.s.Env.eflags;
2194
2195 pCtx->cr0 = pVM->rem.s.Env.cr[0];
2196 pCtx->cr2 = pVM->rem.s.Env.cr[2];
2197 pCtx->cr3 = pVM->rem.s.Env.cr[3];
2198 pCtx->cr4 = pVM->rem.s.Env.cr[4];
2199
2200 pCtx->dr0 = pVM->rem.s.Env.dr[0];
2201 pCtx->dr1 = pVM->rem.s.Env.dr[1];
2202 pCtx->dr2 = pVM->rem.s.Env.dr[2];
2203 pCtx->dr3 = pVM->rem.s.Env.dr[3];
2204 pCtx->dr4 = pVM->rem.s.Env.dr[4];
2205 pCtx->dr5 = pVM->rem.s.Env.dr[5];
2206 pCtx->dr6 = pVM->rem.s.Env.dr[6];
2207 pCtx->dr7 = pVM->rem.s.Env.dr[7];
2208
2209 pCtx->gdtr.cbGdt = pVM->rem.s.Env.gdt.limit;
2210 if (pCtx->gdtr.pGdt != (uint32_t)pVM->rem.s.Env.gdt.base)
2211 {
2212 pCtx->gdtr.pGdt = (uint32_t)pVM->rem.s.Env.gdt.base;
2213 STAM_COUNTER_INC(&gStatREMGDTChange);
2214 VM_FF_SET(pVM, VM_FF_SELM_SYNC_GDT);
2215 }
2216
2217 pCtx->idtr.cbIdt = pVM->rem.s.Env.idt.limit;
2218 if (pCtx->idtr.pIdt != (uint32_t)pVM->rem.s.Env.idt.base)
2219 {
2220 pCtx->idtr.pIdt = (uint32_t)pVM->rem.s.Env.idt.base;
2221 STAM_COUNTER_INC(&gStatREMIDTChange);
2222 VM_FF_SET(pVM, VM_FF_TRPM_SYNC_IDT);
2223 }
2224
2225 if (pCtx->ldtr != pVM->rem.s.Env.ldt.selector)
2226 {
2227 pCtx->ldtr = pVM->rem.s.Env.ldt.selector;
2228 STAM_COUNTER_INC(&gStatREMLDTRChange);
2229 VM_FF_SET(pVM, VM_FF_SELM_SYNC_LDT);
2230 }
2231 if (pCtx->tr != pVM->rem.s.Env.tr.selector)
2232 {
2233 pCtx->tr = pVM->rem.s.Env.tr.selector;
2234 STAM_COUNTER_INC(&gStatREMTRChange);
2235 VM_FF_SET(pVM, VM_FF_SELM_SYNC_TSS);
2236 }
2237
2238 /** @todo These values could still be out of sync! */
2239 pCtx->csHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_CS].base;
2240 pCtx->csHid.u32Limit = pVM->rem.s.Env.segs[R_CS].limit;
2241 /** @note QEmu saves the 2nd dword of the descriptor; we should store the attribute word only! */
2242 pCtx->csHid.Attr.u = (pVM->rem.s.Env.segs[R_CS].flags >> 8) & 0xFFFF;
2243
2244 pCtx->dsHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_DS].base;
2245 pCtx->dsHid.u32Limit = pVM->rem.s.Env.segs[R_DS].limit;
2246 pCtx->dsHid.Attr.u = (pVM->rem.s.Env.segs[R_DS].flags >> 8) & 0xFFFF;
2247
2248 pCtx->esHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_ES].base;
2249 pCtx->esHid.u32Limit = pVM->rem.s.Env.segs[R_ES].limit;
2250 pCtx->esHid.Attr.u = (pVM->rem.s.Env.segs[R_ES].flags >> 8) & 0xFFFF;
2251
2252 pCtx->fsHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_FS].base;
2253 pCtx->fsHid.u32Limit = pVM->rem.s.Env.segs[R_FS].limit;
2254 pCtx->fsHid.Attr.u = (pVM->rem.s.Env.segs[R_FS].flags >> 8) & 0xFFFF;
2255
2256 pCtx->gsHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_GS].base;
2257 pCtx->gsHid.u32Limit = pVM->rem.s.Env.segs[R_GS].limit;
2258 pCtx->gsHid.Attr.u = (pVM->rem.s.Env.segs[R_GS].flags >> 8) & 0xFFFF;
2259
2260 pCtx->ssHid.u32Base = (uint32_t)pVM->rem.s.Env.segs[R_SS].base;
2261 pCtx->ssHid.u32Limit = pVM->rem.s.Env.segs[R_SS].limit;
2262 pCtx->ssHid.Attr.u = (pVM->rem.s.Env.segs[R_SS].flags >> 8) & 0xFFFF;
2263
2264 pCtx->ldtrHid.u32Base = (uint32_t)pVM->rem.s.Env.ldt.base;
2265 pCtx->ldtrHid.u32Limit = pVM->rem.s.Env.ldt.limit;
2266 pCtx->ldtrHid.Attr.u = (pVM->rem.s.Env.ldt.flags >> 8) & 0xFFFF;
2267
2268 pCtx->trHid.u32Base = (uint32_t)pVM->rem.s.Env.tr.base;
2269 pCtx->trHid.u32Limit = pVM->rem.s.Env.tr.limit;
2270 pCtx->trHid.Attr.u = (pVM->rem.s.Env.tr.flags >> 8) & 0xFFFF;
2271
2272 /* Sysenter MSR */
2273 pCtx->SysEnter.cs = pVM->rem.s.Env.sysenter_cs;
2274 pCtx->SysEnter.eip = pVM->rem.s.Env.sysenter_eip;
2275 pCtx->SysEnter.esp = pVM->rem.s.Env.sysenter_esp;
2276}
2277
2278
2279/**
2280 * Update the VMM state information if we're currently in REM.
2281 *
2282 * This method is used by the DBGF and PDMDevice when there is any uncertainty of whether
2283 * we're currently executing in REM and the VMM state is invalid. This method will of
2284 * course check that we're executing in REM before syncing any data over to the VMM.
2285 *
2286 * @param pVM The VM handle.
2287 */
2288REMR3DECL(void) REMR3StateUpdate(PVM pVM)
2289{
2290 if (pVM->rem.s.fInREM)
2291 remR3StateUpdate(pVM);
2292}
2293
2294
2295#undef LOG_GROUP
2296#define LOG_GROUP LOG_GROUP_REM
2297
2298
2299/**
2300 * Notify the recompiler about Address Gate 20 state change.
2301 *
2302 * This notification is required since A20 gate changes are
2303 * initialized from a device driver and the VM might just as
2304 * well be in REM mode as in RAW mode.
2305 *
2306 * @param pVM VM handle.
2307 * @param fEnable True if the gate should be enabled.
2308 * False if the gate should be disabled.
2309 */
2310REMR3DECL(void) REMR3A20Set(PVM pVM, bool fEnable)
2311{
2312 LogFlow(("REMR3A20Set: fEnable=%d\n", fEnable));
2313 VM_ASSERT_EMT(pVM);
2314 cpu_x86_set_a20(&pVM->rem.s.Env, fEnable);
2315}
2316
2317
2318/**
2319 * Replays the invalidated recorded pages.
2320 * Called in response to VERR_REM_FLUSHED_PAGES_OVERFLOW from the RAW execution loop.
2321 *
2322 * @param pVM VM handle.
2323 */
2324REMR3DECL(void) REMR3ReplayInvalidatedPages(PVM pVM)
2325{
2326 VM_ASSERT_EMT(pVM);
2327
2328 /*
2329 * Sync the required registers.
2330 */
2331 pVM->rem.s.Env.cr[0] = pVM->rem.s.pCtx->cr0;
2332 pVM->rem.s.Env.cr[2] = pVM->rem.s.pCtx->cr2;
2333 pVM->rem.s.Env.cr[3] = pVM->rem.s.pCtx->cr3;
2334 pVM->rem.s.Env.cr[4] = pVM->rem.s.pCtx->cr4;
2335
2336 /*
2337 * Replay the flushes.
2338 */
2339 pVM->rem.s.fIgnoreInvlPg = true;
2340 RTUINT i;
2341 for (i = 0; i < pVM->rem.s.cInvalidatedPages; i++)
2342 {
2343 Log2(("REMR3ReplayInvalidatedPages: invlpg %VGv\n", pVM->rem.s.aGCPtrInvalidatedPages[i]));
2344 tlb_flush_page(&pVM->rem.s.Env, pVM->rem.s.aGCPtrInvalidatedPages[i]);
2345 }
2346 pVM->rem.s.fIgnoreInvlPg = false;
2347 pVM->rem.s.cInvalidatedPages = 0;
2348}
2349
2350
2351/**
2352 * Replays the invalidated recorded pages.
2353 * Called in response to VERR_REM_FLUSHED_PAGES_OVERFLOW from the RAW execution loop.
2354 *
2355 * @param pVM VM handle.
2356 */
2357REMR3DECL(void) REMR3ReplayHandlerNotifications(PVM pVM)
2358{
2359 LogFlow(("REMR3ReplayInvalidatedPages:\n"));
2360 VM_ASSERT_EMT(pVM);
2361
2362 /*
2363 * Replay the flushes.
2364 */
2365 RTUINT i;
2366 const RTUINT c = pVM->rem.s.cHandlerNotifications;
2367 pVM->rem.s.cHandlerNotifications = 0;
2368 for (i = 0; i < c; i++)
2369 {
2370 PREMHANDLERNOTIFICATION pRec = &pVM->rem.s.aHandlerNotifications[i];
2371 switch (pRec->enmKind)
2372 {
2373 case REMHANDLERNOTIFICATIONKIND_PHYSICAL_REGISTER:
2374 REMR3NotifyHandlerPhysicalRegister(pVM,
2375 pRec->u.PhysicalRegister.enmType,
2376 pRec->u.PhysicalRegister.GCPhys,
2377 pRec->u.PhysicalRegister.cb,
2378 pRec->u.PhysicalRegister.fHasHCHandler);
2379 break;
2380
2381 case REMHANDLERNOTIFICATIONKIND_PHYSICAL_DEREGISTER:
2382 REMR3NotifyHandlerPhysicalDeregister(pVM,
2383 pRec->u.PhysicalDeregister.enmType,
2384 pRec->u.PhysicalDeregister.GCPhys,
2385 pRec->u.PhysicalDeregister.cb,
2386 pRec->u.PhysicalDeregister.fHasHCHandler,
2387 pRec->u.PhysicalDeregister.pvHCPtr);
2388 break;
2389
2390 case REMHANDLERNOTIFICATIONKIND_PHYSICAL_MODIFY:
2391 REMR3NotifyHandlerPhysicalModify(pVM,
2392 pRec->u.PhysicalModify.enmType,
2393 pRec->u.PhysicalModify.GCPhysOld,
2394 pRec->u.PhysicalModify.GCPhysNew,
2395 pRec->u.PhysicalModify.cb,
2396 pRec->u.PhysicalModify.fHasHCHandler,
2397 pRec->u.PhysicalModify.pvHCPtr);
2398 break;
2399
2400 default:
2401 AssertReleaseMsgFailed(("enmKind=%d\n", pRec->enmKind));
2402 break;
2403 }
2404 }
2405}
2406
2407
2408/**
2409 * Notify REM about changed code page.
2410 *
2411 * @returns VBox status code.
2412 * @param pVM VM handle.
2413 * @param pvCodePage Code page address
2414 */
2415REMR3DECL(int) REMR3NotifyCodePageChanged(PVM pVM, RTGCPTR pvCodePage)
2416{
2417 int rc;
2418 RTGCPHYS PhysGC;
2419 uint64_t flags;
2420
2421 VM_ASSERT_EMT(pVM);
2422
2423 /*
2424 * Get the physical page address.
2425 */
2426 rc = PGMGstGetPage(pVM, pvCodePage, &flags, &PhysGC);
2427 if (rc == VINF_SUCCESS)
2428 {
2429 /*
2430 * Sync the required registers and flush the whole page.
2431 * (Easier to do the whole page than notifying it about each physical
2432 * byte that was changed.
2433 */
2434 pVM->rem.s.Env.cr[0] = pVM->rem.s.pCtx->cr0;
2435 pVM->rem.s.Env.cr[2] = pVM->rem.s.pCtx->cr2;
2436 pVM->rem.s.Env.cr[3] = pVM->rem.s.pCtx->cr3;
2437 pVM->rem.s.Env.cr[4] = pVM->rem.s.pCtx->cr4;
2438
2439 tb_invalidate_phys_page_range(PhysGC, PhysGC + PAGE_SIZE - 1, 0);
2440 }
2441 return VINF_SUCCESS;
2442}
2443
2444/**
2445 * Notification about a successful MMR3PhysRegister() call.
2446 *
2447 * @param pVM VM handle.
2448 * @param GCPhys The physical address the RAM.
2449 * @param cb Size of the memory.
2450 * @param pvRam The HC address of the RAM.
2451 * @param fFlags Flags of the MM_RAM_FLAGS_* defines.
2452 */
2453REMR3DECL(void) REMR3NotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTUINT cb, void *pvRam, unsigned fFlags)
2454{
2455 Log(("REMR3NotifyPhysRamRegister: GCPhys=%VGp cb=%d pvRam=%p fFlags=%d\n", GCPhys, cb, pvRam, fFlags));
2456 VM_ASSERT_EMT(pVM);
2457
2458 /*
2459 * Validate input - we trust the caller.
2460 */
2461 Assert(!GCPhys || pvRam);
2462 Assert(RT_ALIGN_P(pvRam, PAGE_SIZE) == pvRam);
2463 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2464 Assert(cb);
2465 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
2466
2467 /*
2468 * Base ram?
2469 */
2470 if (!GCPhys)
2471 {
2472#if !defined(PGM_DYNAMIC_RAM_ALLOC) && !defined(REM_PHYS_ADDR_IN_TLB)
2473 AssertRelease(!phys_ram_base);
2474 phys_ram_base = pvRam;
2475#endif
2476 phys_ram_size = cb;
2477 phys_ram_dirty_size = cb >> PAGE_SHIFT;
2478#ifndef VBOX_STRICT
2479 phys_ram_dirty = MMR3HeapAlloc(pVM, MM_TAG_REM, phys_ram_dirty_size);
2480 AssertReleaseMsg(phys_ram_dirty, ("failed to allocate %d bytes of dirty bytes\n", phys_ram_dirty_size));
2481#else /* VBOX_STRICT: allocate a full map and make the out of bounds pages invalid. */
2482 phys_ram_dirty = RTMemPageAlloc(_4G >> PAGE_SHIFT);
2483 AssertReleaseMsg(phys_ram_dirty, ("failed to allocate %d bytes of dirty bytes\n", _4G >> PAGE_SHIFT));
2484 uint32_t cbBitmap = RT_ALIGN_32(phys_ram_dirty_size, PAGE_SIZE);
2485 int rc = RTMemProtect(phys_ram_dirty + cbBitmap, (_4G >> PAGE_SHIFT) - cbBitmap, RTMEM_PROT_NONE);
2486 AssertRC(rc);
2487 phys_ram_dirty += cbBitmap - phys_ram_dirty_size;
2488#endif
2489 memset(phys_ram_dirty, 0xff, phys_ram_dirty_size);
2490 }
2491
2492 /*
2493 * Register the ram.
2494 */
2495 Assert(!pVM->rem.s.fIgnoreAll);
2496 pVM->rem.s.fIgnoreAll = true;
2497
2498#ifdef PGM_DYNAMIC_RAM_ALLOC
2499 if (!GCPhys)
2500 cpu_register_physical_memory(GCPhys, cb, GCPhys | IO_MEM_RAM_MISSING);
2501 else
2502 {
2503# ifndef REM_PHYS_ADDR_IN_TLB
2504 uint32_t i;
2505# endif
2506 if (fFlags & MM_RAM_FLAGS_RESERVED)
2507 cpu_register_physical_memory(GCPhys, cb, IO_MEM_UNASSIGNED);
2508 else
2509 cpu_register_physical_memory(GCPhys, cb, GCPhys);
2510
2511# ifndef REM_PHYS_ADDR_IN_TLB
2512 AssertRelease(pVM->rem.s.cPhysRegistrations < REM_MAX_PHYS_REGISTRATIONS);
2513 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2514 {
2515 if (pVM->rem.s.aPhysReg[i].GCPhys == GCPhys)
2516 {
2517 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvRam;
2518 pVM->rem.s.aPhysReg[i].cb = cb;
2519 break;
2520 }
2521 }
2522 if (i == pVM->rem.s.cPhysRegistrations)
2523 {
2524 pVM->rem.s.aPhysReg[i].GCPhys = GCPhys;
2525 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvRam;
2526 pVM->rem.s.aPhysReg[i].cb = cb;
2527 pVM->rem.s.cPhysRegistrations++;
2528 }
2529# endif /* !REM_PHYS_ADDR_IN_TLB */
2530 }
2531#elif defined(REM_PHYS_ADDR_IN_TLB)
2532 cpu_register_physical_memory(GCPhys, cb, GCPhys | (fFlags & MM_RAM_FLAGS_RESERVED ? IO_MEM_UNASSIGNED : 0));
2533#else
2534 AssertRelease(phys_ram_base);
2535 cpu_register_physical_memory(GCPhys, cb, ((uintptr_t)pvRam - (uintptr_t)phys_ram_base)
2536 | (fFlags & MM_RAM_FLAGS_RESERVED ? IO_MEM_UNASSIGNED : 0));
2537#endif
2538 Assert(pVM->rem.s.fIgnoreAll);
2539 pVM->rem.s.fIgnoreAll = false;
2540}
2541
2542
2543/**
2544 * Notification about a successful PGMR3PhysRegisterChunk() call.
2545 *
2546 * @param pVM VM handle.
2547 * @param GCPhys The physical address the RAM.
2548 * @param cb Size of the memory.
2549 * @param pvRam The HC address of the RAM.
2550 * @param fFlags Flags of the MM_RAM_FLAGS_* defines.
2551 */
2552REMR3DECL(void) REMR3NotifyPhysRamChunkRegister(PVM pVM, RTGCPHYS GCPhys, RTUINT cb, RTHCUINTPTR pvRam, unsigned fFlags)
2553{
2554#ifdef PGM_DYNAMIC_RAM_ALLOC
2555# ifndef REM_PHYS_ADDR_IN_TLB
2556 uint32_t idx;
2557#endif
2558
2559 Log(("REMR3NotifyPhysRamChunkRegister: GCPhys=%VGp cb=%d pvRam=%p fFlags=%d\n", GCPhys, cb, pvRam, fFlags));
2560 VM_ASSERT_EMT(pVM);
2561
2562 /*
2563 * Validate input - we trust the caller.
2564 */
2565 Assert(pvRam);
2566 Assert(RT_ALIGN(pvRam, PAGE_SIZE) == pvRam);
2567 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2568 Assert(cb == PGM_DYNAMIC_CHUNK_SIZE);
2569 Assert(fFlags == 0 /* normal RAM */);
2570
2571# ifndef REM_PHYS_ADDR_IN_TLB
2572 if (!pVM->rem.s.paHCVirtToGCPhys)
2573 {
2574 uint32_t size = (_4G >> PGM_DYNAMIC_CHUNK_SHIFT) * sizeof(REMCHUNKINFO);
2575
2576 Assert(phys_ram_size);
2577
2578 pVM->rem.s.paHCVirtToGCPhys = (PREMCHUNKINFO)MMR3HeapAllocZ(pVM, MM_TAG_REM, size);
2579 pVM->rem.s.paGCPhysToHCVirt = (RTHCPTR)MMR3HeapAllocZ(pVM, MM_TAG_REM, (phys_ram_size >> PGM_DYNAMIC_CHUNK_SHIFT)*sizeof(RTHCPTR));
2580 }
2581 pVM->rem.s.paGCPhysToHCVirt[GCPhys >> PGM_DYNAMIC_CHUNK_SHIFT] = pvRam;
2582
2583 idx = (pvRam >> PGM_DYNAMIC_CHUNK_SHIFT);
2584 if (!pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1)
2585 {
2586 pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1 = pvRam;
2587 pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys1 = GCPhys;
2588 }
2589 else
2590 {
2591 Assert(!pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2);
2592 pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2 = pvRam;
2593 pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys2 = GCPhys;
2594 }
2595 /* Does the region spawn two chunks? */
2596 if (pvRam & PGM_DYNAMIC_CHUNK_OFFSET_MASK)
2597 {
2598 if (!pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk1)
2599 {
2600 pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk1 = pvRam;
2601 pVM->rem.s.paHCVirtToGCPhys[idx+1].GCPhys1 = GCPhys;
2602 }
2603 else
2604 {
2605 Assert(!pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk2);
2606 pVM->rem.s.paHCVirtToGCPhys[idx+1].pChunk2 = pvRam;
2607 pVM->rem.s.paHCVirtToGCPhys[idx+1].GCPhys2 = GCPhys;
2608 }
2609 }
2610# endif /* !REM_PHYS_ADDR_IN_TLB */
2611
2612 Assert(!pVM->rem.s.fIgnoreAll);
2613 pVM->rem.s.fIgnoreAll = true;
2614
2615 cpu_register_physical_memory(GCPhys, cb, GCPhys);
2616
2617 Assert(pVM->rem.s.fIgnoreAll);
2618 pVM->rem.s.fIgnoreAll = false;
2619
2620#else
2621 AssertReleaseFailed();
2622#endif
2623}
2624
2625
2626#ifdef PGM_DYNAMIC_RAM_ALLOC
2627# ifndef REM_PHYS_ADDR_IN_TLB
2628#if 0
2629static const uint8_t gabZeroPage[PAGE_SIZE];
2630#endif
2631
2632/**
2633 * Convert GC physical address to HC virt
2634 *
2635 * @returns The HC virt address corresponding to addr.
2636 * @param env The cpu environment.
2637 * @param addr The physical address.
2638 */
2639DECLINLINE(void *) remR3GCPhys2HCVirtInlined(PVM pVM, target_ulong addr)
2640{
2641 uint32_t i;
2642 void *pv;
2643 STAM_PROFILE_START(&gStatGCPhys2HCVirt, a);
2644
2645#if 1
2646 /* lookup in pVM->rem.s.aPhysReg array first (for ROM range(s) inside the guest's RAM) */
2647 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2648 {
2649 RTGCPHYS off = addr - pVM->rem.s.aPhysReg[i].GCPhys;
2650 if (off < pVM->rem.s.aPhysReg[i].cb)
2651 {
2652 pv = (void *)(pVM->rem.s.aPhysReg[i].HCVirt + off);
2653 Log2(("remR3GCPhys2HCVirt: %x -> %x\n", addr, pv));
2654 STAM_PROFILE_STOP(&gStatGCPhys2HCVirt, a);
2655 return pv;
2656 }
2657 }
2658 AssertMsg(addr < phys_ram_size, ("remR3GCPhys2HCVirt: unknown physical address %x\n", addr));
2659 pv = (void *)(pVM->rem.s.paGCPhysToHCVirt[addr >> PGM_DYNAMIC_CHUNK_SHIFT] + (addr & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
2660 Log2(("remR3GCPhys2HCVirt: %x -> %x\n", addr, pv));
2661#else
2662 /** @todo figure out why this is faster than the above code. */
2663 int rc = PGMPhysGCPhys2HCPtr(pVM, addr & X86_PTE_PAE_PG_MASK, PAGE_SIZE, &pv);
2664 if (RT_FAILURE(rc))
2665 {
2666 AssertMsgFailed(("remR3GCPhys2HCVirt: unknown physical address %x\n", addr));
2667 pv = gabZeroPage;
2668 }
2669 pv = (void *)((uintptr_t)pv | (addr & PAGE_OFFSET_MASK));
2670#endif
2671 return pv;
2672}
2673
2674
2675/**
2676 * Convert GC physical address to HC virt
2677 *
2678 * @returns The HC virt address corresponding to addr.
2679 * @param env The cpu environment.
2680 * @param addr The physical address.
2681 */
2682DECLINLINE(target_ulong) remR3HCVirt2GCPhysInlined(PVM pVM, void *addr)
2683{
2684 RTHCUINTPTR HCVirt = (RTHCUINTPTR)addr;
2685 uint32_t idx = (HCVirt >> PGM_DYNAMIC_CHUNK_SHIFT);
2686 RTHCUINTPTR off;
2687 RTUINT i;
2688 target_ulong GCPhys;
2689
2690 off = HCVirt - pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1;
2691
2692 if ( pVM->rem.s.paHCVirtToGCPhys[idx].pChunk1
2693 && off < PGM_DYNAMIC_CHUNK_SIZE)
2694 {
2695 GCPhys = pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys1 + off;
2696 Log2(("remR3HCVirt2GCPhys %x -> %x\n", addr, GCPhys));
2697 return GCPhys;
2698 }
2699
2700 off = HCVirt - pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2;
2701 if ( pVM->rem.s.paHCVirtToGCPhys[idx].pChunk2
2702 && off < PGM_DYNAMIC_CHUNK_SIZE)
2703 {
2704 GCPhys = pVM->rem.s.paHCVirtToGCPhys[idx].GCPhys2 + off;
2705 Log2(("remR3HCVirt2GCPhys %x -> %x\n", addr, GCPhys));
2706 return GCPhys;
2707 }
2708
2709 /* Must be externally registered RAM/ROM range */
2710 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2711 {
2712 uint32_t off = HCVirt - pVM->rem.s.aPhysReg[i].HCVirt;
2713 if (off < pVM->rem.s.aPhysReg[i].cb)
2714 {
2715 GCPhys = pVM->rem.s.aPhysReg[i].GCPhys + off;
2716 Log2(("remR3HCVirt2GCPhys %x -> %x\n", addr, GCPhys));
2717 return GCPhys;
2718 }
2719 }
2720 AssertReleaseMsgFailed(("No translation for physical address %VHv???\n", addr));
2721 return 0;
2722}
2723
2724/**
2725 * Convert GC physical address to HC virt
2726 *
2727 * @returns The HC virt address corresponding to addr.
2728 * @param env The cpu environment.
2729 * @param addr The physical address.
2730 */
2731void *remR3GCPhys2HCVirt(void *env, target_ulong addr)
2732{
2733 PVM pVM = ((CPUState *)env)->pVM;
2734 void *pv;
2735 STAM_PROFILE_START(&gStatGCPhys2HCVirt, a);
2736 pv = remR3GCPhys2HCVirtInlined(pVM, addr);
2737 STAM_PROFILE_STOP(&gStatGCPhys2HCVirt, a);
2738 return pv;
2739}
2740
2741
2742/**
2743 * Convert GC physical address to HC virt
2744 *
2745 * @returns The HC virt address corresponding to addr.
2746 * @param env The cpu environment.
2747 * @param addr The physical address.
2748 */
2749target_ulong remR3HCVirt2GCPhys(void *env, void *addr)
2750{
2751 PVM pVM = ((CPUState *)env)->pVM;
2752 target_ulong GCPhys;
2753 STAM_PROFILE_START(&gStatHCVirt2GCPhys, a);
2754 GCPhys = remR3HCVirt2GCPhysInlined(pVM, addr);
2755 STAM_PROFILE_STOP(&gStatHCVirt2GCPhys, a);
2756 return GCPhys;
2757}
2758
2759# endif /* !REM_PHYS_ADDR_IN_TLB */
2760
2761/**
2762 * Grows dynamically allocated guest RAM.
2763 * Will raise a fatal error if the operation fails.
2764 *
2765 * @param physaddr The physical address.
2766 */
2767void remR3GrowDynRange(unsigned long physaddr)
2768{
2769 int rc;
2770 PVM pVM = cpu_single_env->pVM;
2771
2772 Log(("remR3GrowDynRange %VGp\n", physaddr));
2773 rc = PGM3PhysGrowRange(pVM, (RTGCPHYS)physaddr);
2774 if (VBOX_SUCCESS(rc))
2775 return;
2776
2777 LogRel(("\nUnable to allocate guest RAM chunk at %VGp\n", physaddr));
2778 cpu_abort(cpu_single_env, "Unable to allocate guest RAM chunk at %VGp\n", physaddr);
2779 AssertFatalFailed();
2780}
2781
2782#endif /* PGM_DYNAMIC_RAM_ALLOC */
2783
2784
2785/**
2786 * Notification about a successful MMR3PhysRomRegister() call.
2787 *
2788 * @param pVM VM handle.
2789 * @param GCPhys The physical address of the ROM.
2790 * @param cb The size of the ROM.
2791 * @param pvCopy Pointer to the ROM copy.
2792 * @param fShadow Whether it's currently writable shadow ROM or normal readonly ROM.
2793 * This function will be called when ever the protection of the
2794 * shadow ROM changes (at reset and end of POST).
2795 */
2796REMR3DECL(void) REMR3NotifyPhysRomRegister(PVM pVM, RTGCPHYS GCPhys, RTUINT cb, void *pvCopy, bool fShadow)
2797{
2798#if defined(PGM_DYNAMIC_RAM_ALLOC) && !defined(REM_PHYS_ADDR_IN_TLB)
2799 uint32_t i;
2800#endif
2801 Log(("REMR3NotifyPhysRomRegister: GCPhys=%VGp cb=%d pvCopy=%p fShadow=%RTbool\n", GCPhys, cb, pvCopy, fShadow));
2802 VM_ASSERT_EMT(pVM);
2803
2804 /*
2805 * Validate input - we trust the caller.
2806 */
2807 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2808 Assert(cb);
2809 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
2810 Assert(pvCopy);
2811 Assert(RT_ALIGN_P(pvCopy, PAGE_SIZE) == pvCopy);
2812
2813 /*
2814 * Register the rom.
2815 */
2816 Assert(!pVM->rem.s.fIgnoreAll);
2817 pVM->rem.s.fIgnoreAll = true;
2818
2819#ifdef REM_PHYS_ADDR_IN_TLB
2820 cpu_register_physical_memory(GCPhys, cb, GCPhys | (fShadow ? 0 : IO_MEM_ROM));
2821#elif defined(PGM_DYNAMIC_RAM_ALLOC)
2822 cpu_register_physical_memory(GCPhys, cb, GCPhys | (fShadow ? 0 : IO_MEM_ROM));
2823 AssertRelease(pVM->rem.s.cPhysRegistrations < REM_MAX_PHYS_REGISTRATIONS);
2824 for (i = 0; i < pVM->rem.s.cPhysRegistrations; i++)
2825 {
2826 if (pVM->rem.s.aPhysReg[i].GCPhys == GCPhys)
2827 {
2828 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvCopy;
2829 pVM->rem.s.aPhysReg[i].cb = cb;
2830 break;
2831 }
2832 }
2833 if (i == pVM->rem.s.cPhysRegistrations)
2834 {
2835 pVM->rem.s.aPhysReg[i].GCPhys = GCPhys;
2836 pVM->rem.s.aPhysReg[i].HCVirt = (RTHCUINTPTR)pvCopy;
2837 pVM->rem.s.aPhysReg[i].cb = cb;
2838 pVM->rem.s.cPhysRegistrations++;
2839 }
2840#else
2841 AssertRelease(phys_ram_base);
2842 cpu_register_physical_memory(GCPhys, cb, ((uintptr_t)pvCopy - (uintptr_t)phys_ram_base) | (fShadow ? 0 : IO_MEM_ROM));
2843#endif
2844
2845 Log2(("%.64Vhxd\n", (char *)pvCopy + cb - 64));
2846
2847 Assert(pVM->rem.s.fIgnoreAll);
2848 pVM->rem.s.fIgnoreAll = false;
2849}
2850
2851
2852/**
2853 * Notification about a successful MMR3PhysRegister() call.
2854 *
2855 * @param pVM VM Handle.
2856 * @param GCPhys Start physical address.
2857 * @param cb The size of the range.
2858 */
2859REMR3DECL(void) REMR3NotifyPhysReserve(PVM pVM, RTGCPHYS GCPhys, RTUINT cb)
2860{
2861 Log(("REMR3NotifyPhysReserve: GCPhys=%VGp cb=%d\n", GCPhys, cb));
2862 VM_ASSERT_EMT(pVM);
2863
2864 /*
2865 * Validate input - we trust the caller.
2866 */
2867 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2868 Assert(cb);
2869 Assert(RT_ALIGN_Z(cb, PAGE_SIZE) == cb);
2870
2871 /*
2872 * Unassigning the memory.
2873 */
2874 Assert(!pVM->rem.s.fIgnoreAll);
2875 pVM->rem.s.fIgnoreAll = true;
2876
2877 cpu_register_physical_memory(GCPhys, cb, IO_MEM_UNASSIGNED);
2878
2879 Assert(pVM->rem.s.fIgnoreAll);
2880 pVM->rem.s.fIgnoreAll = false;
2881}
2882
2883
2884/**
2885 * Notification about a successful PGMR3HandlerPhysicalRegister() call.
2886 *
2887 * @param pVM VM Handle.
2888 * @param enmType Handler type.
2889 * @param GCPhys Handler range address.
2890 * @param cb Size of the handler range.
2891 * @param fHasHCHandler Set if the handler has a HC callback function.
2892 *
2893 * @remark MMR3PhysRomRegister assumes that this function will not apply the
2894 * Handler memory type to memory which has no HC handler.
2895 */
2896REMR3DECL(void) REMR3NotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler)
2897{
2898 Log(("REMR3NotifyHandlerPhysicalRegister: enmType=%d GCPhys=%VGp cb=%d fHasHCHandler=%d\n",
2899 enmType, GCPhys, cb, fHasHCHandler));
2900 VM_ASSERT_EMT(pVM);
2901 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2902 Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
2903
2904 if (pVM->rem.s.cHandlerNotifications)
2905 REMR3ReplayHandlerNotifications(pVM);
2906
2907 Assert(!pVM->rem.s.fIgnoreAll);
2908 pVM->rem.s.fIgnoreAll = true;
2909
2910 if (enmType == PGMPHYSHANDLERTYPE_MMIO)
2911 cpu_register_physical_memory(GCPhys, cb, pVM->rem.s.iMMIOMemType);
2912 else if (fHasHCHandler)
2913 cpu_register_physical_memory(GCPhys, cb, pVM->rem.s.iHandlerMemType);
2914
2915 Assert(pVM->rem.s.fIgnoreAll);
2916 pVM->rem.s.fIgnoreAll = false;
2917}
2918
2919
2920/**
2921 * Notification about a successful PGMR3HandlerPhysicalDeregister() operation.
2922 *
2923 * @param pVM VM Handle.
2924 * @param enmType Handler type.
2925 * @param GCPhys Handler range address.
2926 * @param cb Size of the handler range.
2927 * @param fHasHCHandler Set if the handler has a HC callback function.
2928 * @param pvHCPtr The HC virtual address corresponding to GCPhys if available.
2929 */
2930REMR3DECL(void) REMR3NotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS cb, bool fHasHCHandler, void *pvHCPtr)
2931{
2932 Log(("REMR3NotifyHandlerPhysicalDeregister: enmType=%d GCPhys=%VGp cb=%d fHasHCHandler=%d pvHCPtr=%p RAM=%08x\n",
2933 enmType, GCPhys, cb, fHasHCHandler, pvHCPtr, MMR3PhysGetRamSize(pVM)));
2934 VM_ASSERT_EMT(pVM);
2935
2936 if (pVM->rem.s.cHandlerNotifications)
2937 REMR3ReplayHandlerNotifications(pVM);
2938
2939 Assert(!pVM->rem.s.fIgnoreAll);
2940 pVM->rem.s.fIgnoreAll = true;
2941
2942 if (enmType == PGMPHYSHANDLERTYPE_MMIO)
2943 cpu_register_physical_memory(GCPhys, cb, IO_MEM_UNASSIGNED);
2944 else if (fHasHCHandler)
2945 {
2946 if (!pvHCPtr)
2947 {
2948 Assert(GCPhys > MMR3PhysGetRamSize(pVM));
2949 cpu_register_physical_memory(GCPhys, cb, IO_MEM_UNASSIGNED);
2950 }
2951 else
2952 {
2953 /* This is not perfect, but it'll do for PD monitoring... */
2954 Assert(cb == PAGE_SIZE);
2955 Assert(RT_ALIGN_T(GCPhys, PAGE_SIZE, RTGCPHYS) == GCPhys);
2956#ifdef REM_PHYS_ADDR_IN_TLB
2957 cpu_register_physical_memory(GCPhys, cb, GCPhys);
2958#elif defined(PGM_DYNAMIC_RAM_ALLOC)
2959 Assert(remR3HCVirt2GCPhysInlined(pVM, pvHCPtr) < MMR3PhysGetRamSize(pVM));
2960 cpu_register_physical_memory(GCPhys, cb, GCPhys);
2961#else
2962 Assert((uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base < MMR3PhysGetRamSize(pVM));
2963 cpu_register_physical_memory(GCPhys, cb, (uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base);
2964#endif
2965 }
2966 }
2967
2968 Assert(pVM->rem.s.fIgnoreAll);
2969 pVM->rem.s.fIgnoreAll = false;
2970}
2971
2972
2973/**
2974 * Notification about a successful PGMR3HandlerPhysicalModify() call.
2975 *
2976 * @param pVM VM Handle.
2977 * @param enmType Handler type.
2978 * @param GCPhysOld Old handler range address.
2979 * @param GCPhysNew New handler range address.
2980 * @param cb Size of the handler range.
2981 * @param fHasHCHandler Set if the handler has a HC callback function.
2982 * @param pvHCPtr The HC virtual address corresponding to GCPhys if available.
2983 */
2984REMR3DECL(void) REMR3NotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhysOld, RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fHasHCHandler, void *pvHCPtr)
2985{
2986 Log(("REMR3NotifyHandlerPhysicalModify: enmType=%d GCPhysOld=%VGp GCPhysNew=%VGp cb=%d fHasHCHandler=%d pvHCPtr=%p\n",
2987 enmType, GCPhysOld, GCPhysNew, cb, fHasHCHandler, pvHCPtr));
2988 VM_ASSERT_EMT(pVM);
2989 AssertReleaseMsg(enmType != PGMPHYSHANDLERTYPE_MMIO, ("enmType=%d\n", enmType));
2990
2991 if (pVM->rem.s.cHandlerNotifications)
2992 REMR3ReplayHandlerNotifications(pVM);
2993
2994 if (fHasHCHandler)
2995 {
2996 Assert(!pVM->rem.s.fIgnoreAll);
2997 pVM->rem.s.fIgnoreAll = true;
2998
2999 /*
3000 * Reset the old page.
3001 */
3002 if (!pvHCPtr)
3003 cpu_register_physical_memory(GCPhysOld, cb, IO_MEM_UNASSIGNED);
3004 else
3005 {
3006 /* This is not perfect, but it'll do for PD monitoring... */
3007 Assert(cb == PAGE_SIZE);
3008 Assert(RT_ALIGN_T(GCPhysOld, PAGE_SIZE, RTGCPHYS) == GCPhysOld);
3009#ifdef REM_PHYS_ADDR_IN_TLB
3010 cpu_register_physical_memory(GCPhysOld, cb, GCPhysOld);
3011#elif defined(PGM_DYNAMIC_RAM_ALLOC)
3012 Assert(remR3HCVirt2GCPhysInlined(pVM, pvHCPtr) < MMR3PhysGetRamSize(pVM));
3013 cpu_register_physical_memory(GCPhysOld, cb, GCPhysOld);
3014#else
3015 AssertMsg((uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base < MMR3PhysGetRamSize(pVM),
3016 ("pvHCPtr=%p phys_ram_base=%p size=%RX64 cb=%RGp\n", pvHCPtr, phys_ram_base, MMR3PhysGetRamSize(pVM), cb));
3017 cpu_register_physical_memory(GCPhysOld, cb, (uintptr_t)pvHCPtr - (uintptr_t)phys_ram_base);
3018#endif
3019 }
3020
3021 /*
3022 * Update the new page.
3023 */
3024 Assert(RT_ALIGN_T(GCPhysNew, PAGE_SIZE, RTGCPHYS) == GCPhysNew);
3025 Assert(RT_ALIGN_T(cb, PAGE_SIZE, RTGCPHYS) == cb);
3026 cpu_register_physical_memory(GCPhysNew, cb, pVM->rem.s.iHandlerMemType);
3027
3028 Assert(pVM->rem.s.fIgnoreAll);
3029 pVM->rem.s.fIgnoreAll = false;
3030 }
3031}
3032
3033
3034/**
3035 * Checks if we're handling access to this page or not.
3036 *
3037 * @returns true if we're trapping access.
3038 * @returns false if we aren't.
3039 * @param pVM The VM handle.
3040 * @param GCPhys The physical address.
3041 *
3042 * @remark This function will only work correctly in VBOX_STRICT builds!
3043 */
3044REMDECL(bool) REMR3IsPageAccessHandled(PVM pVM, RTGCPHYS GCPhys)
3045{
3046#ifdef VBOX_STRICT
3047 if (pVM->rem.s.cHandlerNotifications)
3048 REMR3ReplayHandlerNotifications(pVM);
3049
3050 unsigned long off = get_phys_page_offset(GCPhys);
3051 return (off & PAGE_OFFSET_MASK) == pVM->rem.s.iHandlerMemType
3052 || (off & PAGE_OFFSET_MASK) == pVM->rem.s.iMMIOMemType
3053 || (off & PAGE_OFFSET_MASK) == IO_MEM_ROM;
3054#else
3055 return false;
3056#endif
3057}
3058
3059
3060/**
3061 * Deals with a rare case in get_phys_addr_code where the code
3062 * is being monitored.
3063 *
3064 * It could also be an MMIO page, in which case we will raise a fatal error.
3065 *
3066 * @returns The physical address corresponding to addr.
3067 * @param env The cpu environment.
3068 * @param addr The virtual address.
3069 * @param pTLBEntry The TLB entry.
3070 */
3071target_ulong remR3PhysGetPhysicalAddressCode(CPUState *env, target_ulong addr, CPUTLBEntry *pTLBEntry)
3072{
3073 PVM pVM = env->pVM;
3074 if ((pTLBEntry->addr_code & ~TARGET_PAGE_MASK) == pVM->rem.s.iHandlerMemType)
3075 {
3076 target_ulong ret = pTLBEntry->addend + addr;
3077 AssertMsg2("remR3PhysGetPhysicalAddressCode: addr=%VGv addr_code=%VGv addend=%VGp ret=%VGp\n",
3078 (RTGCPTR)addr, (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, ret);
3079 return ret;
3080 }
3081 LogRel(("\nTrying to execute code with memory type addr_code=%VGv addend=%VGp at %VGv! (iHandlerMemType=%#x iMMIOMemType=%#x)\n"
3082 "*** handlers\n",
3083 (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, (RTGCPTR)addr, pVM->rem.s.iHandlerMemType, pVM->rem.s.iMMIOMemType));
3084 DBGFR3Info(pVM, "handlers", NULL, DBGFR3InfoLogRelHlp());
3085 LogRel(("*** mmio\n"));
3086 DBGFR3Info(pVM, "mmio", NULL, DBGFR3InfoLogRelHlp());
3087 LogRel(("*** phys\n"));
3088 DBGFR3Info(pVM, "phys", NULL, DBGFR3InfoLogRelHlp());
3089 cpu_abort(env, "Trying to execute code with memory type addr_code=%VGv addend=%VGp at %VGv. (iHandlerMemType=%#x iMMIOMemType=%#x)\n",
3090 (RTGCPTR)pTLBEntry->addr_code, (RTGCPHYS)pTLBEntry->addend, (RTGCPTR)addr, pVM->rem.s.iHandlerMemType, pVM->rem.s.iMMIOMemType);
3091 AssertFatalFailed();
3092}
3093
3094
3095/** Validate the physical address passed to the read functions.
3096 * Useful for finding non-guest-ram reads/writes. */
3097#if 1 /* disable if it becomes bothersome... */
3098# define VBOX_CHECK_ADDR(GCPhys) AssertMsg(PGMPhysIsGCPhysValid(cpu_single_env->pVM, (GCPhys)), ("%VGp\n", (GCPhys)))
3099#else
3100# define VBOX_CHECK_ADDR(GCPhys) do { } while (0)
3101#endif
3102
3103/**
3104 * Read guest RAM and ROM.
3105 *
3106 * @param SrcGCPhys The source address (guest physical).
3107 * @param pvDst The destination address.
3108 * @param cb Number of bytes
3109 */
3110void remR3PhysRead(RTGCPHYS SrcGCPhys, void *pvDst, unsigned cb)
3111{
3112 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3113 VBOX_CHECK_ADDR(SrcGCPhys);
3114 PGMPhysRead(cpu_single_env->pVM, SrcGCPhys, pvDst, cb);
3115 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3116}
3117
3118
3119/**
3120 * Read guest RAM and ROM, unsigned 8-bit.
3121 *
3122 * @param SrcGCPhys The source address (guest physical).
3123 */
3124uint8_t remR3PhysReadU8(RTGCPHYS SrcGCPhys)
3125{
3126 uint8_t val;
3127 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3128 VBOX_CHECK_ADDR(SrcGCPhys);
3129 val = PGMR3PhysReadByte(cpu_single_env->pVM, SrcGCPhys);
3130 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3131 return val;
3132}
3133
3134
3135/**
3136 * Read guest RAM and ROM, signed 8-bit.
3137 *
3138 * @param SrcGCPhys The source address (guest physical).
3139 */
3140int8_t remR3PhysReadS8(RTGCPHYS SrcGCPhys)
3141{
3142 int8_t val;
3143 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3144 VBOX_CHECK_ADDR(SrcGCPhys);
3145 val = PGMR3PhysReadByte(cpu_single_env->pVM, SrcGCPhys);
3146 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3147 return val;
3148}
3149
3150
3151/**
3152 * Read guest RAM and ROM, unsigned 16-bit.
3153 *
3154 * @param SrcGCPhys The source address (guest physical).
3155 */
3156uint16_t remR3PhysReadU16(RTGCPHYS SrcGCPhys)
3157{
3158 uint16_t val;
3159 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3160 VBOX_CHECK_ADDR(SrcGCPhys);
3161 val = PGMR3PhysReadWord(cpu_single_env->pVM, SrcGCPhys);
3162 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3163 return val;
3164}
3165
3166
3167/**
3168 * Read guest RAM and ROM, signed 16-bit.
3169 *
3170 * @param SrcGCPhys The source address (guest physical).
3171 */
3172int16_t remR3PhysReadS16(RTGCPHYS SrcGCPhys)
3173{
3174 uint16_t val;
3175 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3176 VBOX_CHECK_ADDR(SrcGCPhys);
3177 val = PGMR3PhysReadWord(cpu_single_env->pVM, SrcGCPhys);
3178 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3179 return val;
3180}
3181
3182
3183/**
3184 * Read guest RAM and ROM, unsigned 32-bit.
3185 *
3186 * @param SrcGCPhys The source address (guest physical).
3187 */
3188uint32_t remR3PhysReadU32(RTGCPHYS SrcGCPhys)
3189{
3190 uint32_t val;
3191 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3192 VBOX_CHECK_ADDR(SrcGCPhys);
3193 val = PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys);
3194 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3195 return val;
3196}
3197
3198
3199/**
3200 * Read guest RAM and ROM, signed 32-bit.
3201 *
3202 * @param SrcGCPhys The source address (guest physical).
3203 */
3204int32_t remR3PhysReadS32(RTGCPHYS SrcGCPhys)
3205{
3206 int32_t val;
3207 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3208 VBOX_CHECK_ADDR(SrcGCPhys);
3209 val = PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys);
3210 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3211 return val;
3212}
3213
3214
3215/**
3216 * Read guest RAM and ROM, unsigned 64-bit.
3217 *
3218 * @param SrcGCPhys The source address (guest physical).
3219 */
3220uint64_t remR3PhysReadU64(RTGCPHYS SrcGCPhys)
3221{
3222 uint64_t val;
3223 STAM_PROFILE_ADV_START(&gStatMemRead, a);
3224 VBOX_CHECK_ADDR(SrcGCPhys);
3225 val = PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys)
3226 | ((uint64_t)PGMR3PhysReadDword(cpu_single_env->pVM, SrcGCPhys + 4) << 32); /** @todo fix me! */
3227 STAM_PROFILE_ADV_STOP(&gStatMemRead, a);
3228 return val;
3229}
3230
3231
3232/**
3233 * Write guest RAM.
3234 *
3235 * @param DstGCPhys The destination address (guest physical).
3236 * @param pvSrc The source address.
3237 * @param cb Number of bytes to write
3238 */
3239void remR3PhysWrite(RTGCPHYS DstGCPhys, const void *pvSrc, unsigned cb)
3240{
3241 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3242 VBOX_CHECK_ADDR(DstGCPhys);
3243 PGMPhysWrite(cpu_single_env->pVM, DstGCPhys, pvSrc, cb);
3244 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3245}
3246
3247
3248/**
3249 * Write guest RAM, unsigned 8-bit.
3250 *
3251 * @param DstGCPhys The destination address (guest physical).
3252 * @param val Value
3253 */
3254void remR3PhysWriteU8(RTGCPHYS DstGCPhys, uint8_t val)
3255{
3256 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3257 VBOX_CHECK_ADDR(DstGCPhys);
3258 PGMR3PhysWriteByte(cpu_single_env->pVM, DstGCPhys, val);
3259 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3260}
3261
3262
3263/**
3264 * Write guest RAM, unsigned 8-bit.
3265 *
3266 * @param DstGCPhys The destination address (guest physical).
3267 * @param val Value
3268 */
3269void remR3PhysWriteU16(RTGCPHYS DstGCPhys, uint16_t val)
3270{
3271 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3272 VBOX_CHECK_ADDR(DstGCPhys);
3273 PGMR3PhysWriteWord(cpu_single_env->pVM, DstGCPhys, val);
3274 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3275}
3276
3277
3278/**
3279 * Write guest RAM, unsigned 32-bit.
3280 *
3281 * @param DstGCPhys The destination address (guest physical).
3282 * @param val Value
3283 */
3284void remR3PhysWriteU32(RTGCPHYS DstGCPhys, uint32_t val)
3285{
3286 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3287 VBOX_CHECK_ADDR(DstGCPhys);
3288 PGMR3PhysWriteDword(cpu_single_env->pVM, DstGCPhys, val);
3289 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3290}
3291
3292
3293/**
3294 * Write guest RAM, unsigned 64-bit.
3295 *
3296 * @param DstGCPhys The destination address (guest physical).
3297 * @param val Value
3298 */
3299void remR3PhysWriteU64(RTGCPHYS DstGCPhys, uint64_t val)
3300{
3301 STAM_PROFILE_ADV_START(&gStatMemWrite, a);
3302 VBOX_CHECK_ADDR(DstGCPhys);
3303 PGMR3PhysWriteDword(cpu_single_env->pVM, DstGCPhys, (uint32_t)val); /** @todo add U64 interface. */
3304 PGMR3PhysWriteDword(cpu_single_env->pVM, DstGCPhys + 4, val >> 32);
3305 STAM_PROFILE_ADV_STOP(&gStatMemWrite, a);
3306}
3307
3308
3309#ifndef REM_PHYS_ADDR_IN_TLB
3310
3311/**
3312 * Read guest RAM and ROM.
3313 *
3314 * @param pbSrcPhys The source address. Relative to guest RAM.
3315 * @param pvDst The destination address.
3316 * @param cb Number of bytes
3317 */
3318void remR3PhysReadHCPtr(uint8_t *pbSrcPhys, void *pvDst, unsigned cb)
3319{
3320 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3321
3322 /*
3323 * Calc the physical address ('off') and check that it's within the RAM.
3324 * ROM is accessed this way, even if it's not part of the RAM.
3325 */
3326#ifdef PGM_DYNAMIC_RAM_ALLOC
3327 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3328#else
3329 uintptr_t off = pbSrcPhys - phys_ram_base;
3330#endif
3331 PGMPhysRead(cpu_single_env->pVM, (RTGCPHYS)off, pvDst, cb);
3332 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3333}
3334
3335
3336/**
3337 * Read guest RAM and ROM, unsigned 8-bit.
3338 *
3339 * @param pbSrcPhys The source address. Relative to guest RAM.
3340 */
3341uint8_t remR3PhysReadHCPtrU8(uint8_t *pbSrcPhys)
3342{
3343 uint8_t val;
3344
3345 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3346
3347 /*
3348 * Calc the physical address ('off') and check that it's within the RAM.
3349 * ROM is accessed this way, even if it's not part of the RAM.
3350 */
3351#ifdef PGM_DYNAMIC_RAM_ALLOC
3352 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3353#else
3354 uintptr_t off = pbSrcPhys - phys_ram_base;
3355#endif
3356 val = PGMR3PhysReadByte(cpu_single_env->pVM, (RTGCPHYS)off);
3357 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3358 return val;
3359}
3360
3361
3362/**
3363 * Read guest RAM and ROM, signed 8-bit.
3364 *
3365 * @param pbSrcPhys The source address. Relative to guest RAM.
3366 */
3367int8_t remR3PhysReadHCPtrS8(uint8_t *pbSrcPhys)
3368{
3369 int8_t val;
3370
3371 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3372
3373 /*
3374 * Calc the physical address ('off') and check that it's within the RAM.
3375 * ROM is accessed this way, even if it's not part of the RAM.
3376 */
3377#ifdef PGM_DYNAMIC_RAM_ALLOC
3378 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3379#else
3380 uintptr_t off = pbSrcPhys - phys_ram_base;
3381#endif
3382 val = PGMR3PhysReadByte(cpu_single_env->pVM, (RTGCPHYS)off);
3383 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3384 return val;
3385}
3386
3387
3388/**
3389 * Read guest RAM and ROM, unsigned 16-bit.
3390 *
3391 * @param pbSrcPhys The source address. Relative to guest RAM.
3392 */
3393uint16_t remR3PhysReadHCPtrU16(uint8_t *pbSrcPhys)
3394{
3395 uint16_t val;
3396
3397 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3398
3399 /*
3400 * Calc the physical address ('off') and check that it's within the RAM.
3401 * ROM is accessed this way, even if it's not part of the RAM.
3402 */
3403#ifdef PGM_DYNAMIC_RAM_ALLOC
3404 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3405#else
3406 uintptr_t off = pbSrcPhys - phys_ram_base;
3407#endif
3408 val = PGMR3PhysReadWord(cpu_single_env->pVM, (RTGCPHYS)off);
3409 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3410 return val;
3411}
3412
3413
3414/**
3415 * Read guest RAM and ROM, signed 16-bit.
3416 *
3417 * @param pbSrcPhys The source address. Relative to guest RAM.
3418 */
3419int16_t remR3PhysReadHCPtrS16(uint8_t *pbSrcPhys)
3420{
3421 int16_t val;
3422
3423 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3424
3425 /*
3426 * Calc the physical address ('off') and check that it's within the RAM.
3427 * ROM is accessed this way, even if it's not part of the RAM.
3428 */
3429 /** @todo This is rather ugly, but there's no other way when we don't wish to touch *many* other files. */
3430#ifdef PGM_DYNAMIC_RAM_ALLOC
3431 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3432#else
3433 uintptr_t off = pbSrcPhys - phys_ram_base;
3434#endif
3435 val = PGMR3PhysReadWord(cpu_single_env->pVM, (RTGCPHYS)off);
3436 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3437 return val;
3438}
3439
3440
3441/**
3442 * Read guest RAM and ROM, unsigned 32-bit.
3443 *
3444 * @param pbSrcPhys The source address. Relative to guest RAM.
3445 */
3446uint32_t remR3PhysReadHCPtrU32(uint8_t *pbSrcPhys)
3447{
3448 uint32_t val;
3449
3450 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3451
3452 /*
3453 * Calc the physical address ('off') and check that it's within the RAM.
3454 * ROM is accessed this way, even if it's not part of the RAM.
3455 */
3456#ifdef PGM_DYNAMIC_RAM_ALLOC
3457 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3458#else
3459 uintptr_t off = pbSrcPhys - phys_ram_base;
3460#endif
3461 val = PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off);
3462 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3463 return val;
3464}
3465
3466
3467/**
3468 * Read guest RAM and ROM, signed 32-bit.
3469 *
3470 * @param pbSrcPhys The source address. Relative to guest RAM.
3471 */
3472int32_t remR3PhysReadHCPtrS32(uint8_t *pbSrcPhys)
3473{
3474 int32_t val;
3475
3476 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3477
3478 /*
3479 * Calc the physical address ('off') and check that it's within the RAM.
3480 * ROM is accessed this way, even if it's not part of the RAM.
3481 */
3482#ifdef PGM_DYNAMIC_RAM_ALLOC
3483 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3484#else
3485 uintptr_t off = pbSrcPhys - phys_ram_base;
3486#endif
3487 val = PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off);
3488 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3489 return val;
3490}
3491
3492
3493/**
3494 * Read guest RAM and ROM, unsigned 64-bit.
3495 *
3496 * @param pbSrcPhys The source address. Relative to guest RAM.
3497 */
3498uint64_t remR3PhysReadHCPtrU64(uint8_t *pbSrcPhys)
3499{
3500 uint64_t val;
3501
3502 STAM_PROFILE_ADV_START(&gStatMemReadHCPtr, a);
3503
3504 /*
3505 * Calc the physical address ('off') and check that it's within the RAM.
3506 * ROM is accessed this way, even if it's not part of the RAM.
3507 */
3508#ifdef PGM_DYNAMIC_RAM_ALLOC
3509 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbSrcPhys);
3510#else
3511 uintptr_t off = pbSrcPhys - phys_ram_base;
3512#endif
3513 val = PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off)
3514 | ((uint64_t)PGMR3PhysReadDword(cpu_single_env->pVM, (RTGCPHYS)off + 4) << 32); /** @todo fix me! */
3515 STAM_PROFILE_ADV_STOP(&gStatMemReadHCPtr, a);
3516 return val;
3517}
3518
3519
3520/**
3521 * Write guest RAM.
3522 *
3523 * @param pbDstPhys The destination address. Relative to guest RAM.
3524 * @param pvSrc The source address.
3525 * @param cb Number of bytes to write
3526 */
3527void remR3PhysWriteHCPtr(uint8_t *pbDstPhys, const void *pvSrc, unsigned cb)
3528{
3529 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3530 /*
3531 * Calc the physical address ('off') and check that it's within the RAM.
3532 */
3533#ifdef PGM_DYNAMIC_RAM_ALLOC
3534 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3535#else
3536 uintptr_t off = pbDstPhys - phys_ram_base;
3537#endif
3538 PGMPhysWrite(cpu_single_env->pVM, (RTGCPHYS)off, pvSrc, cb);
3539 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3540}
3541
3542
3543/**
3544 * Write guest RAM, unsigned 8-bit.
3545 *
3546 * @param pbDstPhys The destination address. Relative to guest RAM.
3547 * @param val Value
3548 */
3549void remR3PhysWriteHCPtrU8(uint8_t *pbDstPhys, uint8_t val)
3550{
3551 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3552 /*
3553 * Calc the physical address ('off') and check that it's within the RAM.
3554 */
3555#ifdef PGM_DYNAMIC_RAM_ALLOC
3556 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3557#else
3558 uintptr_t off = pbDstPhys - phys_ram_base;
3559#endif
3560 PGMR3PhysWriteByte(cpu_single_env->pVM, (RTGCPHYS)off, val);
3561 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3562}
3563
3564
3565/**
3566 * Write guest RAM, unsigned 16-bit.
3567 *
3568 * @param pbDstPhys The destination address. Relative to guest RAM.
3569 * @param val Value
3570 */
3571void remR3PhysWriteHCPtrU16(uint8_t *pbDstPhys, uint16_t val)
3572{
3573 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3574 /*
3575 * Calc the physical address ('off') and check that it's within the RAM.
3576 */
3577#ifdef PGM_DYNAMIC_RAM_ALLOC
3578 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3579#else
3580 uintptr_t off = pbDstPhys - phys_ram_base;
3581#endif
3582 PGMR3PhysWriteWord(cpu_single_env->pVM, (RTGCPHYS)off, val);
3583 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3584}
3585
3586
3587/**
3588 * Write guest RAM, unsigned 32-bit.
3589 *
3590 * @param pbDstPhys The destination address. Relative to guest RAM.
3591 * @param val Value
3592 */
3593void remR3PhysWriteHCPtrU32(uint8_t *pbDstPhys, uint32_t val)
3594{
3595 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3596 /*
3597 * Calc the physical address ('off') and check that it's within the RAM.
3598 */
3599#ifdef PGM_DYNAMIC_RAM_ALLOC
3600 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3601#else
3602 uintptr_t off = pbDstPhys - phys_ram_base;
3603#endif
3604 PGMR3PhysWriteDword(cpu_single_env->pVM, (RTGCPHYS)off, val);
3605 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3606}
3607
3608
3609/**
3610 * Write guest RAM, unsigned 64-bit.
3611 *
3612 * @param pbDstPhys The destination address. Relative to guest RAM.
3613 * @param val Value
3614 */
3615void remR3PhysWriteHCPtrU64(uint8_t *pbDstPhys, uint64_t val)
3616{
3617 STAM_PROFILE_ADV_START(&gStatMemWriteHCPtr, a);
3618 /*
3619 * Calc the physical address ('off') and check that it's within the RAM.
3620 */
3621#ifdef PGM_DYNAMIC_RAM_ALLOC
3622 uintptr_t off = remR3HCVirt2GCPhysInlined(cpu_single_env->pVM, pbDstPhys);
3623#else
3624 uintptr_t off = pbDstPhys - phys_ram_base;
3625#endif
3626 PGMR3PhysWriteDword(cpu_single_env->pVM, (RTGCPHYS)off, (uint32_t)val); /** @todo add U64 interface. */
3627 PGMR3PhysWriteDword(cpu_single_env->pVM, (RTGCPHYS)off + 4, val >> 32);
3628 STAM_PROFILE_ADV_STOP(&gStatMemWriteHCPtr, a);
3629}
3630
3631#endif /* !REM_PHYS_ADDR_IN_TLB */
3632
3633
3634#undef LOG_GROUP
3635#define LOG_GROUP LOG_GROUP_REM_MMIO
3636
3637/** Read MMIO memory. */
3638static uint32_t remR3MMIOReadU8(void *pvVM, target_phys_addr_t GCPhys)
3639{
3640 uint32_t u32 = 0;
3641 int rc = IOMMMIORead((PVM)pvVM, GCPhys, &u32, 1);
3642 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3643 Log2(("remR3MMIOReadU8: GCPhys=%VGp -> %02x\n", GCPhys, u32));
3644 return u32;
3645}
3646
3647/** Read MMIO memory. */
3648static uint32_t remR3MMIOReadU16(void *pvVM, target_phys_addr_t GCPhys)
3649{
3650 uint32_t u32 = 0;
3651 int rc = IOMMMIORead((PVM)pvVM, GCPhys, &u32, 2);
3652 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3653 Log2(("remR3MMIOReadU16: GCPhys=%VGp -> %04x\n", GCPhys, u32));
3654 return u32;
3655}
3656
3657/** Read MMIO memory. */
3658static uint32_t remR3MMIOReadU32(void *pvVM, target_phys_addr_t GCPhys)
3659{
3660 uint32_t u32 = 0;
3661 int rc = IOMMMIORead((PVM)pvVM, GCPhys, &u32, 4);
3662 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3663 Log2(("remR3MMIOReadU32: GCPhys=%VGp -> %08x\n", GCPhys, u32));
3664 return u32;
3665}
3666
3667/** Write to MMIO memory. */
3668static void remR3MMIOWriteU8(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3669{
3670 Log2(("remR3MMIOWriteU8: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3671 int rc = IOMMMIOWrite((PVM)pvVM, GCPhys, u32, 1);
3672 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3673}
3674
3675/** Write to MMIO memory. */
3676static void remR3MMIOWriteU16(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3677{
3678 Log2(("remR3MMIOWriteU16: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3679 int rc = IOMMMIOWrite((PVM)pvVM, GCPhys, u32, 2);
3680 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3681}
3682
3683/** Write to MMIO memory. */
3684static void remR3MMIOWriteU32(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3685{
3686 Log2(("remR3MMIOWriteU32: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3687 int rc = IOMMMIOWrite((PVM)pvVM, GCPhys, u32, 4);
3688 AssertMsg(rc == VINF_SUCCESS, ("rc=%Vrc\n", rc)); NOREF(rc);
3689}
3690
3691
3692#undef LOG_GROUP
3693#define LOG_GROUP LOG_GROUP_REM_HANDLER
3694
3695/* !!!WARNING!!! This is extremely hackish right now, we assume it's only for LFB access! !!!WARNING!!! */
3696
3697static uint32_t remR3HandlerReadU8(void *pvVM, target_phys_addr_t GCPhys)
3698{
3699 Log2(("remR3HandlerReadU8: GCPhys=%VGp\n", GCPhys));
3700 uint8_t u8;
3701 PGMPhysRead((PVM)pvVM, GCPhys, &u8, sizeof(u8));
3702 return u8;
3703}
3704
3705static uint32_t remR3HandlerReadU16(void *pvVM, target_phys_addr_t GCPhys)
3706{
3707 Log2(("remR3HandlerReadU16: GCPhys=%VGp\n", GCPhys));
3708 uint16_t u16;
3709 PGMPhysRead((PVM)pvVM, GCPhys, &u16, sizeof(u16));
3710 return u16;
3711}
3712
3713static uint32_t remR3HandlerReadU32(void *pvVM, target_phys_addr_t GCPhys)
3714{
3715 Log2(("remR3HandlerReadU32: GCPhys=%VGp\n", GCPhys));
3716 uint32_t u32;
3717 PGMPhysRead((PVM)pvVM, GCPhys, &u32, sizeof(u32));
3718 return u32;
3719}
3720
3721static void remR3HandlerWriteU8(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3722{
3723 Log2(("remR3HandlerWriteU8: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3724 PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint8_t));
3725}
3726
3727static void remR3HandlerWriteU16(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3728{
3729 Log2(("remR3HandlerWriteU16: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3730 PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint16_t));
3731}
3732
3733static void remR3HandlerWriteU32(void *pvVM, target_phys_addr_t GCPhys, uint32_t u32)
3734{
3735 Log2(("remR3HandlerWriteU32: GCPhys=%VGp u32=%#x\n", GCPhys, u32));
3736 PGMPhysWrite((PVM)pvVM, GCPhys, &u32, sizeof(uint32_t));
3737}
3738
3739/* -+- disassembly -+- */
3740
3741#undef LOG_GROUP
3742#define LOG_GROUP LOG_GROUP_REM_DISAS
3743
3744
3745/**
3746 * Enables or disables singled stepped disassembly.
3747 *
3748 * @returns VBox status code.
3749 * @param pVM VM handle.
3750 * @param fEnable To enable set this flag, to disable clear it.
3751 */
3752static DECLCALLBACK(int) remR3DisasEnableStepping(PVM pVM, bool fEnable)
3753{
3754 LogFlow(("remR3DisasEnableStepping: fEnable=%d\n", fEnable));
3755 VM_ASSERT_EMT(pVM);
3756
3757 if (fEnable)
3758 pVM->rem.s.Env.state |= CPU_EMULATE_SINGLE_STEP;
3759 else
3760 pVM->rem.s.Env.state &= ~CPU_EMULATE_SINGLE_STEP;
3761 return VINF_SUCCESS;
3762}
3763
3764
3765/**
3766 * Enables or disables singled stepped disassembly.
3767 *
3768 * @returns VBox status code.
3769 * @param pVM VM handle.
3770 * @param fEnable To enable set this flag, to disable clear it.
3771 */
3772REMR3DECL(int) REMR3DisasEnableStepping(PVM pVM, bool fEnable)
3773{
3774 PVMREQ pReq;
3775 int rc;
3776
3777 LogFlow(("REMR3DisasEnableStepping: fEnable=%d\n", fEnable));
3778 if (VM_IS_EMT(pVM))
3779 return remR3DisasEnableStepping(pVM, fEnable);
3780
3781 rc = VMR3ReqCall(pVM, &pReq, RT_INDEFINITE_WAIT, (PFNRT)remR3DisasEnableStepping, 2, pVM, fEnable);
3782 AssertRC(rc);
3783 if (VBOX_SUCCESS(rc))
3784 rc = pReq->iStatus;
3785 VMR3ReqFree(pReq);
3786 return rc;
3787}
3788
3789
3790#if defined(VBOX_WITH_DEBUGGER) && !(defined(RT_OS_WINDOWS) && defined(RT_ARCH_AMD64))
3791/**
3792 * External Debugger Command: .remstep [on|off|1|0]
3793 */
3794static DECLCALLBACK(int) remR3CmdDisasEnableStepping(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
3795{
3796 bool fEnable;
3797 int rc;
3798
3799 /* print status */
3800 if (cArgs == 0)
3801 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "DisasStepping is %s\n",
3802 pVM->rem.s.Env.state & CPU_EMULATE_SINGLE_STEP ? "enabled" : "disabled");
3803
3804 /* convert the argument and change the mode. */
3805 rc = pCmdHlp->pfnVarToBool(pCmdHlp, &paArgs[0], &fEnable);
3806 if (VBOX_FAILURE(rc))
3807 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "boolean conversion failed!\n");
3808 rc = REMR3DisasEnableStepping(pVM, fEnable);
3809 if (VBOX_FAILURE(rc))
3810 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "REMR3DisasEnableStepping failed!\n");
3811 return rc;
3812}
3813#endif
3814
3815
3816/**
3817 * Disassembles n instructions and prints them to the log.
3818 *
3819 * @returns Success indicator.
3820 * @param env Pointer to the recompiler CPU structure.
3821 * @param f32BitCode Indicates that whether or not the code should
3822 * be disassembled as 16 or 32 bit. If -1 the CS
3823 * selector will be inspected.
3824 * @param nrInstructions Nr of instructions to disassemble
3825 * @param pszPrefix
3826 * @remark not currently used for anything but ad-hoc debugging.
3827 */
3828bool remR3DisasBlock(CPUState *env, int f32BitCode, int nrInstructions, char *pszPrefix)
3829{
3830 int i;
3831
3832 /*
3833 * Determin 16/32 bit mode.
3834 */
3835 if (f32BitCode == -1)
3836 f32BitCode = !!(env->segs[R_CS].flags & X86_DESC_DB); /** @todo is this right?!!?!?!?!? */
3837
3838 /*
3839 * Convert cs:eip to host context address.
3840 * We don't care to much about cross page correctness presently.
3841 */
3842 RTGCPTR GCPtrPC = env->segs[R_CS].base + env->eip;
3843 void *pvPC;
3844 if (f32BitCode && (env->cr[0] & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG))
3845 {
3846 /* convert eip to physical address. */
3847 int rc = PGMPhysGCPtr2HCPtrByGstCR3(env->pVM,
3848 GCPtrPC,
3849 env->cr[3],
3850 env->cr[4] & (X86_CR4_PSE | X86_CR4_PAE), /** @todo add longmode flag */
3851 &pvPC);
3852 if (VBOX_FAILURE(rc))
3853 {
3854 if (!PATMIsPatchGCAddr(env->pVM, GCPtrPC))
3855 return false;
3856 pvPC = (char *)PATMR3QueryPatchMemHC(env->pVM, NULL)
3857 + (GCPtrPC - PATMR3QueryPatchMemGC(env->pVM, NULL));
3858 }
3859 }
3860 else
3861 {
3862 /* physical address */
3863 int rc = PGMPhysGCPhys2HCPtr(env->pVM, (RTGCPHYS)GCPtrPC, nrInstructions * 16, &pvPC);
3864 if (VBOX_FAILURE(rc))
3865 return false;
3866 }
3867
3868 /*
3869 * Disassemble.
3870 */
3871 RTINTPTR off = env->eip - (RTINTPTR)pvPC;
3872 DISCPUSTATE Cpu;
3873 Cpu.mode = f32BitCode ? CPUMODE_32BIT : CPUMODE_16BIT;
3874 Cpu.pfnReadBytes = NULL; /** @todo make cs:eip reader for the disassembler. */
3875 //Cpu.dwUserData[0] = (uintptr_t)pVM;
3876 //Cpu.dwUserData[1] = (uintptr_t)pvPC;
3877 //Cpu.dwUserData[2] = GCPtrPC;
3878
3879 for (i=0;i<nrInstructions;i++)
3880 {
3881 char szOutput[256];
3882 uint32_t cbOp;
3883 if (!DISInstr(&Cpu, (uintptr_t)pvPC, off, &cbOp, &szOutput[0]))
3884 return false;
3885 if (pszPrefix)
3886 Log(("%s: %s", pszPrefix, szOutput));
3887 else
3888 Log(("%s", szOutput));
3889
3890 pvPC += cbOp;
3891 }
3892 return true;
3893}
3894
3895
3896/** @todo need to test the new code, using the old code in the mean while. */
3897#define USE_OLD_DUMP_AND_DISASSEMBLY
3898
3899/**
3900 * Disassembles one instruction and prints it to the log.
3901 *
3902 * @returns Success indicator.
3903 * @param env Pointer to the recompiler CPU structure.
3904 * @param f32BitCode Indicates that whether or not the code should
3905 * be disassembled as 16 or 32 bit. If -1 the CS
3906 * selector will be inspected.
3907 * @param pszPrefix
3908 */
3909bool remR3DisasInstr(CPUState *env, int f32BitCode, char *pszPrefix)
3910{
3911#ifdef USE_OLD_DUMP_AND_DISASSEMBLY
3912 PVM pVM = env->pVM;
3913
3914 /*
3915 * Determin 16/32 bit mode.
3916 */
3917 if (f32BitCode == -1)
3918 f32BitCode = !!(env->segs[R_CS].flags & X86_DESC_DB); /** @todo is this right?!!?!?!?!? */
3919
3920 /*
3921 * Log registers
3922 */
3923 if (LogIs2Enabled())
3924 {
3925 remR3StateUpdate(pVM);
3926 DBGFR3InfoLog(pVM, "cpumguest", pszPrefix);
3927 }
3928
3929 /*
3930 * Convert cs:eip to host context address.
3931 * We don't care to much about cross page correctness presently.
3932 */
3933 RTGCPTR GCPtrPC = env->segs[R_CS].base + env->eip;
3934 void *pvPC;
3935 if ((env->cr[0] & (X86_CR0_PE | X86_CR0_PG)) == (X86_CR0_PE | X86_CR0_PG))
3936 {
3937 /* convert eip to physical address. */
3938 int rc = PGMPhysGCPtr2HCPtrByGstCR3(pVM,
3939 GCPtrPC,
3940 env->cr[3],
3941 env->cr[4] & (X86_CR4_PSE | X86_CR4_PAE),
3942 &pvPC);
3943 if (VBOX_FAILURE(rc))
3944 {
3945 if (!PATMIsPatchGCAddr(pVM, GCPtrPC))
3946 return false;
3947 pvPC = (char *)PATMR3QueryPatchMemHC(pVM, NULL)
3948 + (GCPtrPC - PATMR3QueryPatchMemGC(pVM, NULL));
3949 }
3950 }
3951 else
3952 {
3953
3954 /* physical address */
3955 int rc = PGMPhysGCPhys2HCPtr(pVM, (RTGCPHYS)GCPtrPC, 16, &pvPC);
3956 if (VBOX_FAILURE(rc))
3957 return false;
3958 }
3959
3960 /*
3961 * Disassemble.
3962 */
3963 RTINTPTR off = env->eip - (RTINTPTR)pvPC;
3964 DISCPUSTATE Cpu;
3965 Cpu.mode = f32BitCode ? CPUMODE_32BIT : CPUMODE_16BIT;
3966 Cpu.pfnReadBytes = NULL; /** @todo make cs:eip reader for the disassembler. */
3967 //Cpu.dwUserData[0] = (uintptr_t)pVM;
3968 //Cpu.dwUserData[1] = (uintptr_t)pvPC;
3969 //Cpu.dwUserData[2] = GCPtrPC;
3970 char szOutput[256];
3971 uint32_t cbOp;
3972 if (!DISInstr(&Cpu, (uintptr_t)pvPC, off, &cbOp, &szOutput[0]))
3973 return false;
3974
3975 if (!f32BitCode)
3976 {
3977 if (pszPrefix)
3978 Log(("%s: %04X:%s", pszPrefix, env->segs[R_CS].selector, szOutput));
3979 else
3980 Log(("%04X:%s", env->segs[R_CS].selector, szOutput));
3981 }
3982 else
3983 {
3984 if (pszPrefix)
3985 Log(("%s: %s", pszPrefix, szOutput));
3986 else
3987 Log(("%s", szOutput));
3988 }
3989 return true;
3990
3991#else /* !USE_OLD_DUMP_AND_DISASSEMBLY */
3992 PVM pVM = env->pVM;
3993 const bool fLog = LogIsEnabled();
3994 const bool fLog2 = LogIs2Enabled();
3995 int rc = VINF_SUCCESS;
3996
3997 /*
3998 * Don't bother if there ain't any log output to do.
3999 */
4000 if (!fLog && !fLog2)
4001 return true;
4002
4003 /*
4004 * Update the state so DBGF reads the correct register values.
4005 */
4006 remR3StateUpdate(pVM);
4007
4008 /*
4009 * Log registers if requested.
4010 */
4011 if (!fLog2)
4012 DBGFR3InfoLog(pVM, "cpumguest", pszPrefix);
4013
4014 /*
4015 * Disassemble to log.
4016 */
4017 if (fLog)
4018 rc = DBGFR3DisasInstrCurrentLogInternal(pVM, pszPrefix);
4019
4020 return VBOX_SUCCESS(rc);
4021#endif
4022}
4023
4024
4025/**
4026 * Disassemble recompiled code.
4027 *
4028 * @param phFileIgnored Ignored, logfile usually.
4029 * @param pvCode Pointer to the code block.
4030 * @param cb Size of the code block.
4031 */
4032void disas(FILE *phFileIgnored, void *pvCode, unsigned long cb)
4033{
4034 if (LogIs2Enabled())
4035 {
4036 unsigned off = 0;
4037 char szOutput[256];
4038 DISCPUSTATE Cpu = {0};
4039 Cpu.mode = CPUMODE_32BIT;
4040
4041 RTLogPrintf("Recompiled Code: %p %#lx (%ld) bytes\n", pvCode, cb, cb);
4042 while (off < cb)
4043 {
4044 uint32_t cbInstr;
4045 if (DISInstr(&Cpu, (uintptr_t)pvCode + off, 0, &cbInstr, szOutput))
4046 RTLogPrintf("%s", szOutput);
4047 else
4048 {
4049 RTLogPrintf("disas error\n");
4050 cbInstr = 1;
4051 }
4052 off += cbInstr;
4053 }
4054 }
4055 NOREF(phFileIgnored);
4056}
4057
4058
4059/**
4060 * Disassemble guest code.
4061 *
4062 * @param phFileIgnored Ignored, logfile usually.
4063 * @param uCode The guest address of the code to disassemble. (flat?)
4064 * @param cb Number of bytes to disassemble.
4065 * @param fFlags Flags, probably something which tells if this is 16, 32 or 64 bit code.
4066 */
4067void target_disas(FILE *phFileIgnored, target_ulong uCode, target_ulong cb, int fFlags)
4068{
4069 if (LogIs2Enabled())
4070 {
4071 PVM pVM = cpu_single_env->pVM;
4072
4073 /*
4074 * Update the state so DBGF reads the correct register values (flags).
4075 */
4076 remR3StateUpdate(pVM);
4077
4078 /*
4079 * Do the disassembling.
4080 */
4081 RTLogPrintf("Guest Code: PC=%VGp #VGp (%VGp) bytes fFlags=%d\n", uCode, cb, cb, fFlags);
4082 RTSEL cs = cpu_single_env->segs[R_CS].selector;
4083 RTGCUINTPTR eip = uCode - cpu_single_env->segs[R_CS].base;
4084 for (;;)
4085 {
4086 char szBuf[256];
4087 uint32_t cbInstr;
4088 int rc = DBGFR3DisasInstrEx(pVM,
4089 cs,
4090 eip,
4091 0,
4092 szBuf, sizeof(szBuf),
4093 &cbInstr);
4094 if (VBOX_SUCCESS(rc))
4095 RTLogPrintf("%VGp %s\n", uCode, szBuf);
4096 else
4097 {
4098 RTLogPrintf("%VGp %04x:%VGp: %s\n", uCode, cs, eip, szBuf);
4099 cbInstr = 1;
4100 }
4101
4102 /* next */
4103 if (cb <= cbInstr)
4104 break;
4105 cb -= cbInstr;
4106 uCode += cbInstr;
4107 eip += cbInstr;
4108 }
4109 }
4110 NOREF(phFileIgnored);
4111}
4112
4113
4114/**
4115 * Looks up a guest symbol.
4116 *
4117 * @returns Pointer to symbol name. This is a static buffer.
4118 * @param orig_addr The address in question.
4119 */
4120const char *lookup_symbol(target_ulong orig_addr)
4121{
4122 RTGCINTPTR off = 0;
4123 DBGFSYMBOL Sym;
4124 PVM pVM = cpu_single_env->pVM;
4125 int rc = DBGFR3SymbolByAddr(pVM, orig_addr, &off, &Sym);
4126 if (VBOX_SUCCESS(rc))
4127 {
4128 static char szSym[sizeof(Sym.szName) + 48];
4129 if (!off)
4130 RTStrPrintf(szSym, sizeof(szSym), "%s\n", Sym.szName);
4131 else if (off > 0)
4132 RTStrPrintf(szSym, sizeof(szSym), "%s+%x\n", Sym.szName, off);
4133 else
4134 RTStrPrintf(szSym, sizeof(szSym), "%s-%x\n", Sym.szName, -off);
4135 return szSym;
4136 }
4137 return "<N/A>";
4138}
4139
4140
4141#undef LOG_GROUP
4142#define LOG_GROUP LOG_GROUP_REM
4143
4144
4145/* -+- FF notifications -+- */
4146
4147
4148/**
4149 * Notification about a pending interrupt.
4150 *
4151 * @param pVM VM Handle.
4152 * @param u8Interrupt Interrupt
4153 * @thread The emulation thread.
4154 */
4155REMR3DECL(void) REMR3NotifyPendingInterrupt(PVM pVM, uint8_t u8Interrupt)
4156{
4157 Assert(pVM->rem.s.u32PendingInterrupt == REM_NO_PENDING_IRQ);
4158 pVM->rem.s.u32PendingInterrupt = u8Interrupt;
4159}
4160
4161/**
4162 * Notification about a pending interrupt.
4163 *
4164 * @returns Pending interrupt or REM_NO_PENDING_IRQ
4165 * @param pVM VM Handle.
4166 * @thread The emulation thread.
4167 */
4168REMR3DECL(uint32_t) REMR3QueryPendingInterrupt(PVM pVM)
4169{
4170 return pVM->rem.s.u32PendingInterrupt;
4171}
4172
4173/**
4174 * Notification about the interrupt FF being set.
4175 *
4176 * @param pVM VM Handle.
4177 * @thread The emulation thread.
4178 */
4179REMR3DECL(void) REMR3NotifyInterruptSet(PVM pVM)
4180{
4181 LogFlow(("REMR3NotifyInterruptSet: fInRem=%d interrupts %s\n", pVM->rem.s.fInREM,
4182 (pVM->rem.s.Env.eflags & IF_MASK) && !(pVM->rem.s.Env.hflags & HF_INHIBIT_IRQ_MASK) ? "enabled" : "disabled"));
4183 if (pVM->rem.s.fInREM)
4184 {
4185 if (VM_IS_EMT(pVM))
4186 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
4187 else
4188 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_HARD);
4189 }
4190}
4191
4192
4193/**
4194 * Notification about the interrupt FF being set.
4195 *
4196 * @param pVM VM Handle.
4197 * @thread The emulation thread.
4198 */
4199REMR3DECL(void) REMR3NotifyInterruptClear(PVM pVM)
4200{
4201 LogFlow(("REMR3NotifyInterruptClear:\n"));
4202 VM_ASSERT_EMT(pVM);
4203 if (pVM->rem.s.fInREM)
4204 cpu_reset_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
4205}
4206
4207
4208/**
4209 * Notification about pending timer(s).
4210 *
4211 * @param pVM VM Handle.
4212 * @thread Any.
4213 */
4214REMR3DECL(void) REMR3NotifyTimerPending(PVM pVM)
4215{
4216#ifndef DEBUG_bird
4217 LogFlow(("REMR3NotifyTimerPending: fInRem=%d\n", pVM->rem.s.fInREM));
4218#endif
4219 if (pVM->rem.s.fInREM)
4220 {
4221 if (VM_IS_EMT(pVM))
4222 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4223 else
4224 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_TIMER);
4225 }
4226}
4227
4228
4229/**
4230 * Notification about pending DMA transfers.
4231 *
4232 * @param pVM VM Handle.
4233 * @thread Any.
4234 */
4235REMR3DECL(void) REMR3NotifyDmaPending(PVM pVM)
4236{
4237 LogFlow(("REMR3NotifyDmaPending: fInRem=%d\n", pVM->rem.s.fInREM));
4238 if (pVM->rem.s.fInREM)
4239 {
4240 if (VM_IS_EMT(pVM))
4241 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4242 else
4243 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_DMA);
4244 }
4245}
4246
4247
4248/**
4249 * Notification about pending timer(s).
4250 *
4251 * @param pVM VM Handle.
4252 * @thread Any.
4253 */
4254REMR3DECL(void) REMR3NotifyQueuePending(PVM pVM)
4255{
4256 LogFlow(("REMR3NotifyQueuePending: fInRem=%d\n", pVM->rem.s.fInREM));
4257 if (pVM->rem.s.fInREM)
4258 {
4259 if (VM_IS_EMT(pVM))
4260 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4261 else
4262 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_EXIT);
4263 }
4264}
4265
4266
4267/**
4268 * Notification about pending FF set by an external thread.
4269 *
4270 * @param pVM VM handle.
4271 * @thread Any.
4272 */
4273REMR3DECL(void) REMR3NotifyFF(PVM pVM)
4274{
4275 LogFlow(("REMR3NotifyFF: fInRem=%d\n", pVM->rem.s.fInREM));
4276 if (pVM->rem.s.fInREM)
4277 {
4278 if (VM_IS_EMT(pVM))
4279 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4280 else
4281 ASMAtomicOrS32(&cpu_single_env->interrupt_request, CPU_INTERRUPT_EXTERNAL_EXIT);
4282 }
4283}
4284
4285
4286#ifdef VBOX_WITH_STATISTICS
4287void remR3ProfileStart(int statcode)
4288{
4289 STAMPROFILEADV *pStat;
4290 switch(statcode)
4291 {
4292 case STATS_EMULATE_SINGLE_INSTR:
4293 pStat = &gStatExecuteSingleInstr;
4294 break;
4295 case STATS_QEMU_COMPILATION:
4296 pStat = &gStatCompilationQEmu;
4297 break;
4298 case STATS_QEMU_RUN_EMULATED_CODE:
4299 pStat = &gStatRunCodeQEmu;
4300 break;
4301 case STATS_QEMU_TOTAL:
4302 pStat = &gStatTotalTimeQEmu;
4303 break;
4304 case STATS_QEMU_RUN_TIMERS:
4305 pStat = &gStatTimers;
4306 break;
4307 case STATS_TLB_LOOKUP:
4308 pStat= &gStatTBLookup;
4309 break;
4310 case STATS_IRQ_HANDLING:
4311 pStat= &gStatIRQ;
4312 break;
4313 case STATS_RAW_CHECK:
4314 pStat = &gStatRawCheck;
4315 break;
4316
4317 default:
4318 AssertMsgFailed(("unknown stat %d\n", statcode));
4319 return;
4320 }
4321 STAM_PROFILE_ADV_START(pStat, a);
4322}
4323
4324
4325void remR3ProfileStop(int statcode)
4326{
4327 STAMPROFILEADV *pStat;
4328 switch(statcode)
4329 {
4330 case STATS_EMULATE_SINGLE_INSTR:
4331 pStat = &gStatExecuteSingleInstr;
4332 break;
4333 case STATS_QEMU_COMPILATION:
4334 pStat = &gStatCompilationQEmu;
4335 break;
4336 case STATS_QEMU_RUN_EMULATED_CODE:
4337 pStat = &gStatRunCodeQEmu;
4338 break;
4339 case STATS_QEMU_TOTAL:
4340 pStat = &gStatTotalTimeQEmu;
4341 break;
4342 case STATS_QEMU_RUN_TIMERS:
4343 pStat = &gStatTimers;
4344 break;
4345 case STATS_TLB_LOOKUP:
4346 pStat= &gStatTBLookup;
4347 break;
4348 case STATS_IRQ_HANDLING:
4349 pStat= &gStatIRQ;
4350 break;
4351 case STATS_RAW_CHECK:
4352 pStat = &gStatRawCheck;
4353 break;
4354 default:
4355 AssertMsgFailed(("unknown stat %d\n", statcode));
4356 return;
4357 }
4358 STAM_PROFILE_ADV_STOP(pStat, a);
4359}
4360#endif
4361
4362/**
4363 * Raise an RC, force rem exit.
4364 *
4365 * @param pVM VM handle.
4366 * @param rc The rc.
4367 */
4368void remR3RaiseRC(PVM pVM, int rc)
4369{
4370 Log(("remR3RaiseRC: rc=%Vrc\n", rc));
4371 Assert(pVM->rem.s.fInREM);
4372 VM_ASSERT_EMT(pVM);
4373 pVM->rem.s.rc = rc;
4374 cpu_interrupt(&pVM->rem.s.Env, CPU_INTERRUPT_RC);
4375}
4376
4377
4378/* -+- timers -+- */
4379
4380uint64_t cpu_get_tsc(CPUX86State *env)
4381{
4382 STAM_COUNTER_INC(&gStatCpuGetTSC);
4383 return TMCpuTickGet(env->pVM);
4384}
4385
4386
4387/* -+- interrupts -+- */
4388
4389void cpu_set_ferr(CPUX86State *env)
4390{
4391 int rc = PDMIsaSetIrq(env->pVM, 13, 1);
4392 LogFlow(("cpu_set_ferr: rc=%d\n", rc)); NOREF(rc);
4393}
4394
4395int cpu_get_pic_interrupt(CPUState *env)
4396{
4397 uint8_t u8Interrupt;
4398 int rc;
4399
4400 /* When we fail to forward interrupts directly in raw mode, we fall back to the recompiler.
4401 * In that case we can't call PDMGetInterrupt anymore, because it has already cleared the interrupt
4402 * with the (a)pic.
4403 */
4404 /** @note We assume we will go directly to the recompiler to handle the pending interrupt! */
4405 /** @todo r=bird: In the long run we should just do the interrupt handling in EM/CPUM/TRPM/somewhere and
4406 * if we cannot execute the interrupt handler in raw-mode just reschedule to REM. Once that is done we
4407 * remove this kludge. */
4408 if (env->pVM->rem.s.u32PendingInterrupt != REM_NO_PENDING_IRQ)
4409 {
4410 rc = VINF_SUCCESS;
4411 Assert(env->pVM->rem.s.u32PendingInterrupt >= 0 && env->pVM->rem.s.u32PendingInterrupt <= 255);
4412 u8Interrupt = env->pVM->rem.s.u32PendingInterrupt;
4413 env->pVM->rem.s.u32PendingInterrupt = REM_NO_PENDING_IRQ;
4414 }
4415 else
4416 rc = PDMGetInterrupt(env->pVM, &u8Interrupt);
4417
4418 LogFlow(("cpu_get_pic_interrupt: u8Interrupt=%d rc=%Vrc\n", u8Interrupt, rc));
4419 if (VBOX_SUCCESS(rc))
4420 {
4421 if (VM_FF_ISPENDING(env->pVM, VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC))
4422 env->interrupt_request |= CPU_INTERRUPT_HARD;
4423 return u8Interrupt;
4424 }
4425 return -1;
4426}
4427
4428
4429/* -+- local apic -+- */
4430
4431void cpu_set_apic_base(CPUX86State *env, uint64_t val)
4432{
4433 int rc = PDMApicSetBase(env->pVM, val);
4434 LogFlow(("cpu_set_apic_base: val=%#llx rc=%Vrc\n", val, rc)); NOREF(rc);
4435}
4436
4437uint64_t cpu_get_apic_base(CPUX86State *env)
4438{
4439 uint64_t u64;
4440 int rc = PDMApicGetBase(env->pVM, &u64);
4441 if (VBOX_SUCCESS(rc))
4442 {
4443 LogFlow(("cpu_get_apic_base: returns %#llx \n", u64));
4444 return u64;
4445 }
4446 LogFlow(("cpu_get_apic_base: returns 0 (rc=%Vrc)\n", rc));
4447 return 0;
4448}
4449
4450void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
4451{
4452 int rc = PDMApicSetTPR(env->pVM, val);
4453 LogFlow(("cpu_set_apic_tpr: val=%#x rc=%Vrc\n", val, rc)); NOREF(rc);
4454}
4455
4456uint8_t cpu_get_apic_tpr(CPUX86State *env)
4457{
4458 uint8_t u8;
4459 int rc = PDMApicGetTPR(env->pVM, &u8);
4460 if (VBOX_SUCCESS(rc))
4461 {
4462 LogFlow(("cpu_get_apic_tpr: returns %#x\n", u8));
4463 return u8;
4464 }
4465 LogFlow(("cpu_get_apic_tpr: returns 0 (rc=%Vrc)\n", rc));
4466 return 0;
4467}
4468
4469
4470/* -+- I/O Ports -+- */
4471
4472#undef LOG_GROUP
4473#define LOG_GROUP LOG_GROUP_REM_IOPORT
4474
4475void cpu_outb(CPUState *env, int addr, int val)
4476{
4477 if (addr != 0x80 && addr != 0x70 && addr != 0x61)
4478 Log2(("cpu_outb: addr=%#06x val=%#x\n", addr, val));
4479
4480 int rc = IOMIOPortWrite(env->pVM, (RTIOPORT)addr, val, 1);
4481 if (RT_LIKELY(rc == VINF_SUCCESS))
4482 return;
4483 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4484 {
4485 Log(("cpu_outb: addr=%#06x val=%#x -> %Vrc\n", addr, val, rc));
4486 remR3RaiseRC(env->pVM, rc);
4487 return;
4488 }
4489 remAbort(rc, __FUNCTION__);
4490}
4491
4492void cpu_outw(CPUState *env, int addr, int val)
4493{
4494 //Log2(("cpu_outw: addr=%#06x val=%#x\n", addr, val));
4495 int rc = IOMIOPortWrite(env->pVM, (RTIOPORT)addr, val, 2);
4496 if (RT_LIKELY(rc == VINF_SUCCESS))
4497 return;
4498 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4499 {
4500 Log(("cpu_outw: addr=%#06x val=%#x -> %Vrc\n", addr, val, rc));
4501 remR3RaiseRC(env->pVM, rc);
4502 return;
4503 }
4504 remAbort(rc, __FUNCTION__);
4505}
4506
4507void cpu_outl(CPUState *env, int addr, int val)
4508{
4509 Log2(("cpu_outl: addr=%#06x val=%#x\n", addr, val));
4510 int rc = IOMIOPortWrite(env->pVM, (RTIOPORT)addr, val, 4);
4511 if (RT_LIKELY(rc == VINF_SUCCESS))
4512 return;
4513 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4514 {
4515 Log(("cpu_outl: addr=%#06x val=%#x -> %Vrc\n", addr, val, rc));
4516 remR3RaiseRC(env->pVM, rc);
4517 return;
4518 }
4519 remAbort(rc, __FUNCTION__);
4520}
4521
4522int cpu_inb(CPUState *env, int addr)
4523{
4524 uint32_t u32 = 0;
4525 int rc = IOMIOPortRead(env->pVM, (RTIOPORT)addr, &u32, 1);
4526 if (RT_LIKELY(rc == VINF_SUCCESS))
4527 {
4528 if (/*addr != 0x61 && */addr != 0x71)
4529 Log2(("cpu_inb: addr=%#06x -> %#x\n", addr, u32));
4530 return (int)u32;
4531 }
4532 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4533 {
4534 Log(("cpu_inb: addr=%#06x -> %#x rc=%Vrc\n", addr, u32, rc));
4535 remR3RaiseRC(env->pVM, rc);
4536 return (int)u32;
4537 }
4538 remAbort(rc, __FUNCTION__);
4539 return 0xff;
4540}
4541
4542int cpu_inw(CPUState *env, int addr)
4543{
4544 uint32_t u32 = 0;
4545 int rc = IOMIOPortRead(env->pVM, (RTIOPORT)addr, &u32, 2);
4546 if (RT_LIKELY(rc == VINF_SUCCESS))
4547 {
4548 Log2(("cpu_inw: addr=%#06x -> %#x\n", addr, u32));
4549 return (int)u32;
4550 }
4551 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4552 {
4553 Log(("cpu_inw: addr=%#06x -> %#x rc=%Vrc\n", addr, u32, rc));
4554 remR3RaiseRC(env->pVM, rc);
4555 return (int)u32;
4556 }
4557 remAbort(rc, __FUNCTION__);
4558 return 0xffff;
4559}
4560
4561int cpu_inl(CPUState *env, int addr)
4562{
4563 uint32_t u32 = 0;
4564 int rc = IOMIOPortRead(env->pVM, (RTIOPORT)addr, &u32, 4);
4565 if (RT_LIKELY(rc == VINF_SUCCESS))
4566 {
4567//if (addr==0x01f0 && u32 == 0x6b6d)
4568// loglevel = ~0;
4569 Log2(("cpu_inl: addr=%#06x -> %#x\n", addr, u32));
4570 return (int)u32;
4571 }
4572 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
4573 {
4574 Log(("cpu_inl: addr=%#06x -> %#x rc=%Vrc\n", addr, u32, rc));
4575 remR3RaiseRC(env->pVM, rc);
4576 return (int)u32;
4577 }
4578 remAbort(rc, __FUNCTION__);
4579 return 0xffffffff;
4580}
4581
4582#undef LOG_GROUP
4583#define LOG_GROUP LOG_GROUP_REM
4584
4585
4586/* -+- helpers and misc other interfaces -+- */
4587
4588/**
4589 * Perform the CPUID instruction.
4590 *
4591 * ASMCpuId cannot be invoked from some source files where this is used because of global
4592 * register allocations.
4593 *
4594 * @param env Pointer to the recompiler CPU structure.
4595 * @param uOperator CPUID operation (eax).
4596 * @param pvEAX Where to store eax.
4597 * @param pvEBX Where to store ebx.
4598 * @param pvECX Where to store ecx.
4599 * @param pvEDX Where to store edx.
4600 */
4601void remR3CpuId(CPUState *env, unsigned uOperator, void *pvEAX, void *pvEBX, void *pvECX, void *pvEDX)
4602{
4603 CPUMGetGuestCpuId(env->pVM, uOperator, (uint32_t *)pvEAX, (uint32_t *)pvEBX, (uint32_t *)pvECX, (uint32_t *)pvEDX);
4604}
4605
4606
4607#if 0 /* not used */
4608/**
4609 * Interface for qemu hardware to report back fatal errors.
4610 */
4611void hw_error(const char *pszFormat, ...)
4612{
4613 /*
4614 * Bitch about it.
4615 */
4616 /** @todo Add support for nested arg lists in the LogPrintfV routine! I've code for
4617 * this in my Odin32 tree at home! */
4618 va_list args;
4619 va_start(args, pszFormat);
4620 RTLogPrintf("fatal error in virtual hardware:");
4621 RTLogPrintfV(pszFormat, args);
4622 va_end(args);
4623 AssertReleaseMsgFailed(("fatal error in virtual hardware: %s\n", pszFormat));
4624
4625 /*
4626 * If we're in REM context we'll sync back the state before 'jumping' to
4627 * the EMs failure handling.
4628 */
4629 PVM pVM = cpu_single_env->pVM;
4630 if (pVM->rem.s.fInREM)
4631 REMR3StateBack(pVM);
4632 EMR3FatalError(pVM, VERR_REM_VIRTUAL_HARDWARE_ERROR);
4633 AssertMsgFailed(("EMR3FatalError returned!\n"));
4634}
4635#endif
4636
4637/**
4638 * Interface for the qemu cpu to report unhandled situation
4639 * raising a fatal VM error.
4640 */
4641void cpu_abort(CPUState *env, const char *pszFormat, ...)
4642{
4643 /*
4644 * Bitch about it.
4645 */
4646 RTLogFlags(NULL, "nodisabled nobuffered");
4647 va_list args;
4648 va_start(args, pszFormat);
4649 RTLogPrintf("fatal error in recompiler cpu: %N\n", pszFormat, &args);
4650 va_end(args);
4651 va_start(args, pszFormat);
4652 AssertReleaseMsgFailed(("fatal error in recompiler cpu: %N\n", pszFormat, &args));
4653 va_end(args);
4654
4655 /*
4656 * If we're in REM context we'll sync back the state before 'jumping' to
4657 * the EMs failure handling.
4658 */
4659 PVM pVM = cpu_single_env->pVM;
4660 if (pVM->rem.s.fInREM)
4661 REMR3StateBack(pVM);
4662 EMR3FatalError(pVM, VERR_REM_VIRTUAL_CPU_ERROR);
4663 AssertMsgFailed(("EMR3FatalError returned!\n"));
4664}
4665
4666
4667/**
4668 * Aborts the VM.
4669 *
4670 * @param rc VBox error code.
4671 * @param pszTip Hint about why/when this happend.
4672 */
4673static void remAbort(int rc, const char *pszTip)
4674{
4675 /*
4676 * Bitch about it.
4677 */
4678 RTLogPrintf("internal REM fatal error: rc=%Vrc %s\n", rc, pszTip);
4679 AssertReleaseMsgFailed(("internal REM fatal error: rc=%Vrc %s\n", rc, pszTip));
4680
4681 /*
4682 * Jump back to where we entered the recompiler.
4683 */
4684 PVM pVM = cpu_single_env->pVM;
4685 if (pVM->rem.s.fInREM)
4686 REMR3StateBack(pVM);
4687 EMR3FatalError(pVM, rc);
4688 AssertMsgFailed(("EMR3FatalError returned!\n"));
4689}
4690
4691
4692/**
4693 * Dumps a linux system call.
4694 * @param pVM VM handle.
4695 */
4696void remR3DumpLnxSyscall(PVM pVM)
4697{
4698 static const char *apsz[] =
4699 {
4700 "sys_restart_syscall", /* 0 - old "setup()" system call, used for restarting */
4701 "sys_exit",
4702 "sys_fork",
4703 "sys_read",
4704 "sys_write",
4705 "sys_open", /* 5 */
4706 "sys_close",
4707 "sys_waitpid",
4708 "sys_creat",
4709 "sys_link",
4710 "sys_unlink", /* 10 */
4711 "sys_execve",
4712 "sys_chdir",
4713 "sys_time",
4714 "sys_mknod",
4715 "sys_chmod", /* 15 */
4716 "sys_lchown16",
4717 "sys_ni_syscall", /* old break syscall holder */
4718 "sys_stat",
4719 "sys_lseek",
4720 "sys_getpid", /* 20 */
4721 "sys_mount",
4722 "sys_oldumount",
4723 "sys_setuid16",
4724 "sys_getuid16",
4725 "sys_stime", /* 25 */
4726 "sys_ptrace",
4727 "sys_alarm",
4728 "sys_fstat",
4729 "sys_pause",
4730 "sys_utime", /* 30 */
4731 "sys_ni_syscall", /* old stty syscall holder */
4732 "sys_ni_syscall", /* old gtty syscall holder */
4733 "sys_access",
4734 "sys_nice",
4735 "sys_ni_syscall", /* 35 - old ftime syscall holder */
4736 "sys_sync",
4737 "sys_kill",
4738 "sys_rename",
4739 "sys_mkdir",
4740 "sys_rmdir", /* 40 */
4741 "sys_dup",
4742 "sys_pipe",
4743 "sys_times",
4744 "sys_ni_syscall", /* old prof syscall holder */
4745 "sys_brk", /* 45 */
4746 "sys_setgid16",
4747 "sys_getgid16",
4748 "sys_signal",
4749 "sys_geteuid16",
4750 "sys_getegid16", /* 50 */
4751 "sys_acct",
4752 "sys_umount", /* recycled never used phys() */
4753 "sys_ni_syscall", /* old lock syscall holder */
4754 "sys_ioctl",
4755 "sys_fcntl", /* 55 */
4756 "sys_ni_syscall", /* old mpx syscall holder */
4757 "sys_setpgid",
4758 "sys_ni_syscall", /* old ulimit syscall holder */
4759 "sys_olduname",
4760 "sys_umask", /* 60 */
4761 "sys_chroot",
4762 "sys_ustat",
4763 "sys_dup2",
4764 "sys_getppid",
4765 "sys_getpgrp", /* 65 */
4766 "sys_setsid",
4767 "sys_sigaction",
4768 "sys_sgetmask",
4769 "sys_ssetmask",
4770 "sys_setreuid16", /* 70 */
4771 "sys_setregid16",
4772 "sys_sigsuspend",
4773 "sys_sigpending",
4774 "sys_sethostname",
4775 "sys_setrlimit", /* 75 */
4776 "sys_old_getrlimit",
4777 "sys_getrusage",
4778 "sys_gettimeofday",
4779 "sys_settimeofday",
4780 "sys_getgroups16", /* 80 */
4781 "sys_setgroups16",
4782 "old_select",
4783 "sys_symlink",
4784 "sys_lstat",
4785 "sys_readlink", /* 85 */
4786 "sys_uselib",
4787 "sys_swapon",
4788 "sys_reboot",
4789 "old_readdir",
4790 "old_mmap", /* 90 */
4791 "sys_munmap",
4792 "sys_truncate",
4793 "sys_ftruncate",
4794 "sys_fchmod",
4795 "sys_fchown16", /* 95 */
4796 "sys_getpriority",
4797 "sys_setpriority",
4798 "sys_ni_syscall", /* old profil syscall holder */
4799 "sys_statfs",
4800 "sys_fstatfs", /* 100 */
4801 "sys_ioperm",
4802 "sys_socketcall",
4803 "sys_syslog",
4804 "sys_setitimer",
4805 "sys_getitimer", /* 105 */
4806 "sys_newstat",
4807 "sys_newlstat",
4808 "sys_newfstat",
4809 "sys_uname",
4810 "sys_iopl", /* 110 */
4811 "sys_vhangup",
4812 "sys_ni_syscall", /* old "idle" system call */
4813 "sys_vm86old",
4814 "sys_wait4",
4815 "sys_swapoff", /* 115 */
4816 "sys_sysinfo",
4817 "sys_ipc",
4818 "sys_fsync",
4819 "sys_sigreturn",
4820 "sys_clone", /* 120 */
4821 "sys_setdomainname",
4822 "sys_newuname",
4823 "sys_modify_ldt",
4824 "sys_adjtimex",
4825 "sys_mprotect", /* 125 */
4826 "sys_sigprocmask",
4827 "sys_ni_syscall", /* old "create_module" */
4828 "sys_init_module",
4829 "sys_delete_module",
4830 "sys_ni_syscall", /* 130: old "get_kernel_syms" */
4831 "sys_quotactl",
4832 "sys_getpgid",
4833 "sys_fchdir",
4834 "sys_bdflush",
4835 "sys_sysfs", /* 135 */
4836 "sys_personality",
4837 "sys_ni_syscall", /* reserved for afs_syscall */
4838 "sys_setfsuid16",
4839 "sys_setfsgid16",
4840 "sys_llseek", /* 140 */
4841 "sys_getdents",
4842 "sys_select",
4843 "sys_flock",
4844 "sys_msync",
4845 "sys_readv", /* 145 */
4846 "sys_writev",
4847 "sys_getsid",
4848 "sys_fdatasync",
4849 "sys_sysctl",
4850 "sys_mlock", /* 150 */
4851 "sys_munlock",
4852 "sys_mlockall",
4853 "sys_munlockall",
4854 "sys_sched_setparam",
4855 "sys_sched_getparam", /* 155 */
4856 "sys_sched_setscheduler",
4857 "sys_sched_getscheduler",
4858 "sys_sched_yield",
4859 "sys_sched_get_priority_max",
4860 "sys_sched_get_priority_min", /* 160 */
4861 "sys_sched_rr_get_interval",
4862 "sys_nanosleep",
4863 "sys_mremap",
4864 "sys_setresuid16",
4865 "sys_getresuid16", /* 165 */
4866 "sys_vm86",
4867 "sys_ni_syscall", /* Old sys_query_module */
4868 "sys_poll",
4869 "sys_nfsservctl",
4870 "sys_setresgid16", /* 170 */
4871 "sys_getresgid16",
4872 "sys_prctl",
4873 "sys_rt_sigreturn",
4874 "sys_rt_sigaction",
4875 "sys_rt_sigprocmask", /* 175 */
4876 "sys_rt_sigpending",
4877 "sys_rt_sigtimedwait",
4878 "sys_rt_sigqueueinfo",
4879 "sys_rt_sigsuspend",
4880 "sys_pread64", /* 180 */
4881 "sys_pwrite64",
4882 "sys_chown16",
4883 "sys_getcwd",
4884 "sys_capget",
4885 "sys_capset", /* 185 */
4886 "sys_sigaltstack",
4887 "sys_sendfile",
4888 "sys_ni_syscall", /* reserved for streams1 */
4889 "sys_ni_syscall", /* reserved for streams2 */
4890 "sys_vfork", /* 190 */
4891 "sys_getrlimit",
4892 "sys_mmap2",
4893 "sys_truncate64",
4894 "sys_ftruncate64",
4895 "sys_stat64", /* 195 */
4896 "sys_lstat64",
4897 "sys_fstat64",
4898 "sys_lchown",
4899 "sys_getuid",
4900 "sys_getgid", /* 200 */
4901 "sys_geteuid",
4902 "sys_getegid",
4903 "sys_setreuid",
4904 "sys_setregid",
4905 "sys_getgroups", /* 205 */
4906 "sys_setgroups",
4907 "sys_fchown",
4908 "sys_setresuid",
4909 "sys_getresuid",
4910 "sys_setresgid", /* 210 */
4911 "sys_getresgid",
4912 "sys_chown",
4913 "sys_setuid",
4914 "sys_setgid",
4915 "sys_setfsuid", /* 215 */
4916 "sys_setfsgid",
4917 "sys_pivot_root",
4918 "sys_mincore",
4919 "sys_madvise",
4920 "sys_getdents64", /* 220 */
4921 "sys_fcntl64",
4922 "sys_ni_syscall", /* reserved for TUX */
4923 "sys_ni_syscall",
4924 "sys_gettid",
4925 "sys_readahead", /* 225 */
4926 "sys_setxattr",
4927 "sys_lsetxattr",
4928 "sys_fsetxattr",
4929 "sys_getxattr",
4930 "sys_lgetxattr", /* 230 */
4931 "sys_fgetxattr",
4932 "sys_listxattr",
4933 "sys_llistxattr",
4934 "sys_flistxattr",
4935 "sys_removexattr", /* 235 */
4936 "sys_lremovexattr",
4937 "sys_fremovexattr",
4938 "sys_tkill",
4939 "sys_sendfile64",
4940 "sys_futex", /* 240 */
4941 "sys_sched_setaffinity",
4942 "sys_sched_getaffinity",
4943 "sys_set_thread_area",
4944 "sys_get_thread_area",
4945 "sys_io_setup", /* 245 */
4946 "sys_io_destroy",
4947 "sys_io_getevents",
4948 "sys_io_submit",
4949 "sys_io_cancel",
4950 "sys_fadvise64", /* 250 */
4951 "sys_ni_syscall",
4952 "sys_exit_group",
4953 "sys_lookup_dcookie",
4954 "sys_epoll_create",
4955 "sys_epoll_ctl", /* 255 */
4956 "sys_epoll_wait",
4957 "sys_remap_file_pages",
4958 "sys_set_tid_address",
4959 "sys_timer_create",
4960 "sys_timer_settime", /* 260 */
4961 "sys_timer_gettime",
4962 "sys_timer_getoverrun",
4963 "sys_timer_delete",
4964 "sys_clock_settime",
4965 "sys_clock_gettime", /* 265 */
4966 "sys_clock_getres",
4967 "sys_clock_nanosleep",
4968 "sys_statfs64",
4969 "sys_fstatfs64",
4970 "sys_tgkill", /* 270 */
4971 "sys_utimes",
4972 "sys_fadvise64_64",
4973 "sys_ni_syscall" /* sys_vserver */
4974 };
4975
4976 uint32_t uEAX = CPUMGetGuestEAX(pVM);
4977 switch (uEAX)
4978 {
4979 default:
4980 if (uEAX < ELEMENTS(apsz))
4981 Log(("REM: linux syscall %3d: %s (eip=%VGv ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x ebp=%08x)\n",
4982 uEAX, apsz[uEAX], CPUMGetGuestEIP(pVM), CPUMGetGuestEBX(pVM), CPUMGetGuestECX(pVM),
4983 CPUMGetGuestEDX(pVM), CPUMGetGuestESI(pVM), CPUMGetGuestEDI(pVM), CPUMGetGuestEBP(pVM)));
4984 else
4985 Log(("eip=%08x: linux syscall %d (#%x) unknown\n", CPUMGetGuestEIP(pVM), uEAX, uEAX));
4986 break;
4987
4988 }
4989}
4990
4991
4992/**
4993 * Dumps an OpenBSD system call.
4994 * @param pVM VM handle.
4995 */
4996void remR3DumpOBsdSyscall(PVM pVM)
4997{
4998 static const char *apsz[] =
4999 {
5000 "SYS_syscall", //0
5001 "SYS_exit", //1
5002 "SYS_fork", //2
5003 "SYS_read", //3
5004 "SYS_write", //4
5005 "SYS_open", //5
5006 "SYS_close", //6
5007 "SYS_wait4", //7
5008 "SYS_8",
5009 "SYS_link", //9
5010 "SYS_unlink", //10
5011 "SYS_11",
5012 "SYS_chdir", //12
5013 "SYS_fchdir", //13
5014 "SYS_mknod", //14
5015 "SYS_chmod", //15
5016 "SYS_chown", //16
5017 "SYS_break", //17
5018 "SYS_18",
5019 "SYS_19",
5020 "SYS_getpid", //20
5021 "SYS_mount", //21
5022 "SYS_unmount", //22
5023 "SYS_setuid", //23
5024 "SYS_getuid", //24
5025 "SYS_geteuid", //25
5026 "SYS_ptrace", //26
5027 "SYS_recvmsg", //27
5028 "SYS_sendmsg", //28
5029 "SYS_recvfrom", //29
5030 "SYS_accept", //30
5031 "SYS_getpeername", //31
5032 "SYS_getsockname", //32
5033 "SYS_access", //33
5034 "SYS_chflags", //34
5035 "SYS_fchflags", //35
5036 "SYS_sync", //36
5037 "SYS_kill", //37
5038 "SYS_38",
5039 "SYS_getppid", //39
5040 "SYS_40",
5041 "SYS_dup", //41
5042 "SYS_opipe", //42
5043 "SYS_getegid", //43
5044 "SYS_profil", //44
5045 "SYS_ktrace", //45
5046 "SYS_sigaction", //46
5047 "SYS_getgid", //47
5048 "SYS_sigprocmask", //48
5049 "SYS_getlogin", //49
5050 "SYS_setlogin", //50
5051 "SYS_acct", //51
5052 "SYS_sigpending", //52
5053 "SYS_osigaltstack", //53
5054 "SYS_ioctl", //54
5055 "SYS_reboot", //55
5056 "SYS_revoke", //56
5057 "SYS_symlink", //57
5058 "SYS_readlink", //58
5059 "SYS_execve", //59
5060 "SYS_umask", //60
5061 "SYS_chroot", //61
5062 "SYS_62",
5063 "SYS_63",
5064 "SYS_64",
5065 "SYS_65",
5066 "SYS_vfork", //66
5067 "SYS_67",
5068 "SYS_68",
5069 "SYS_sbrk", //69
5070 "SYS_sstk", //70
5071 "SYS_61",
5072 "SYS_vadvise", //72
5073 "SYS_munmap", //73
5074 "SYS_mprotect", //74
5075 "SYS_madvise", //75
5076 "SYS_76",
5077 "SYS_77",
5078 "SYS_mincore", //78
5079 "SYS_getgroups", //79
5080 "SYS_setgroups", //80
5081 "SYS_getpgrp", //81
5082 "SYS_setpgid", //82
5083 "SYS_setitimer", //83
5084 "SYS_84",
5085 "SYS_85",
5086 "SYS_getitimer", //86
5087 "SYS_87",
5088 "SYS_88",
5089 "SYS_89",
5090 "SYS_dup2", //90
5091 "SYS_91",
5092 "SYS_fcntl", //92
5093 "SYS_select", //93
5094 "SYS_94",
5095 "SYS_fsync", //95
5096 "SYS_setpriority", //96
5097 "SYS_socket", //97
5098 "SYS_connect", //98
5099 "SYS_99",
5100 "SYS_getpriority", //100
5101 "SYS_101",
5102 "SYS_102",
5103 "SYS_sigreturn", //103
5104 "SYS_bind", //104
5105 "SYS_setsockopt", //105
5106 "SYS_listen", //106
5107 "SYS_107",
5108 "SYS_108",
5109 "SYS_109",
5110 "SYS_110",
5111 "SYS_sigsuspend", //111
5112 "SYS_112",
5113 "SYS_113",
5114 "SYS_114",
5115 "SYS_115",
5116 "SYS_gettimeofday", //116
5117 "SYS_getrusage", //117
5118 "SYS_getsockopt", //118
5119 "SYS_119",
5120 "SYS_readv", //120
5121 "SYS_writev", //121
5122 "SYS_settimeofday", //122
5123 "SYS_fchown", //123
5124 "SYS_fchmod", //124
5125 "SYS_125",
5126 "SYS_setreuid", //126
5127 "SYS_setregid", //127
5128 "SYS_rename", //128
5129 "SYS_129",
5130 "SYS_130",
5131 "SYS_flock", //131
5132 "SYS_mkfifo", //132
5133 "SYS_sendto", //133
5134 "SYS_shutdown", //134
5135 "SYS_socketpair", //135
5136 "SYS_mkdir", //136
5137 "SYS_rmdir", //137
5138 "SYS_utimes", //138
5139 "SYS_139",
5140 "SYS_adjtime", //140
5141 "SYS_141",
5142 "SYS_142",
5143 "SYS_143",
5144 "SYS_144",
5145 "SYS_145",
5146 "SYS_146",
5147 "SYS_setsid", //147
5148 "SYS_quotactl", //148
5149 "SYS_149",
5150 "SYS_150",
5151 "SYS_151",
5152 "SYS_152",
5153 "SYS_153",
5154 "SYS_154",
5155 "SYS_nfssvc", //155
5156 "SYS_156",
5157 "SYS_157",
5158 "SYS_158",
5159 "SYS_159",
5160 "SYS_160",
5161 "SYS_getfh", //161
5162 "SYS_162",
5163 "SYS_163",
5164 "SYS_164",
5165 "SYS_sysarch", //165
5166 "SYS_166",
5167 "SYS_167",
5168 "SYS_168",
5169 "SYS_169",
5170 "SYS_170",
5171 "SYS_171",
5172 "SYS_172",
5173 "SYS_pread", //173
5174 "SYS_pwrite", //174
5175 "SYS_175",
5176 "SYS_176",
5177 "SYS_177",
5178 "SYS_178",
5179 "SYS_179",
5180 "SYS_180",
5181 "SYS_setgid", //181
5182 "SYS_setegid", //182
5183 "SYS_seteuid", //183
5184 "SYS_lfs_bmapv", //184
5185 "SYS_lfs_markv", //185
5186 "SYS_lfs_segclean", //186
5187 "SYS_lfs_segwait", //187
5188 "SYS_188",
5189 "SYS_189",
5190 "SYS_190",
5191 "SYS_pathconf", //191
5192 "SYS_fpathconf", //192
5193 "SYS_swapctl", //193
5194 "SYS_getrlimit", //194
5195 "SYS_setrlimit", //195
5196 "SYS_getdirentries", //196
5197 "SYS_mmap", //197
5198 "SYS___syscall", //198
5199 "SYS_lseek", //199
5200 "SYS_truncate", //200
5201 "SYS_ftruncate", //201
5202 "SYS___sysctl", //202
5203 "SYS_mlock", //203
5204 "SYS_munlock", //204
5205 "SYS_205",
5206 "SYS_futimes", //206
5207 "SYS_getpgid", //207
5208 "SYS_xfspioctl", //208
5209 "SYS_209",
5210 "SYS_210",
5211 "SYS_211",
5212 "SYS_212",
5213 "SYS_213",
5214 "SYS_214",
5215 "SYS_215",
5216 "SYS_216",
5217 "SYS_217",
5218 "SYS_218",
5219 "SYS_219",
5220 "SYS_220",
5221 "SYS_semget", //221
5222 "SYS_222",
5223 "SYS_223",
5224 "SYS_224",
5225 "SYS_msgget", //225
5226 "SYS_msgsnd", //226
5227 "SYS_msgrcv", //227
5228 "SYS_shmat", //228
5229 "SYS_229",
5230 "SYS_shmdt", //230
5231 "SYS_231",
5232 "SYS_clock_gettime", //232
5233 "SYS_clock_settime", //233
5234 "SYS_clock_getres", //234
5235 "SYS_235",
5236 "SYS_236",
5237 "SYS_237",
5238 "SYS_238",
5239 "SYS_239",
5240 "SYS_nanosleep", //240
5241 "SYS_241",
5242 "SYS_242",
5243 "SYS_243",
5244 "SYS_244",
5245 "SYS_245",
5246 "SYS_246",
5247 "SYS_247",
5248 "SYS_248",
5249 "SYS_249",
5250 "SYS_minherit", //250
5251 "SYS_rfork", //251
5252 "SYS_poll", //252
5253 "SYS_issetugid", //253
5254 "SYS_lchown", //254
5255 "SYS_getsid", //255
5256 "SYS_msync", //256
5257 "SYS_257",
5258 "SYS_258",
5259 "SYS_259",
5260 "SYS_getfsstat", //260
5261 "SYS_statfs", //261
5262 "SYS_fstatfs", //262
5263 "SYS_pipe", //263
5264 "SYS_fhopen", //264
5265 "SYS_265",
5266 "SYS_fhstatfs", //266
5267 "SYS_preadv", //267
5268 "SYS_pwritev", //268
5269 "SYS_kqueue", //269
5270 "SYS_kevent", //270
5271 "SYS_mlockall", //271
5272 "SYS_munlockall", //272
5273 "SYS_getpeereid", //273
5274 "SYS_274",
5275 "SYS_275",
5276 "SYS_276",
5277 "SYS_277",
5278 "SYS_278",
5279 "SYS_279",
5280 "SYS_280",
5281 "SYS_getresuid", //281
5282 "SYS_setresuid", //282
5283 "SYS_getresgid", //283
5284 "SYS_setresgid", //284
5285 "SYS_285",
5286 "SYS_mquery", //286
5287 "SYS_closefrom", //287
5288 "SYS_sigaltstack", //288
5289 "SYS_shmget", //289
5290 "SYS_semop", //290
5291 "SYS_stat", //291
5292 "SYS_fstat", //292
5293 "SYS_lstat", //293
5294 "SYS_fhstat", //294
5295 "SYS___semctl", //295
5296 "SYS_shmctl", //296
5297 "SYS_msgctl", //297
5298 "SYS_MAXSYSCALL", //298
5299 //299
5300 //300
5301 };
5302 uint32_t uEAX;
5303 if (!LogIsEnabled())
5304 return;
5305 uEAX = CPUMGetGuestEAX(pVM);
5306 switch (uEAX)
5307 {
5308 default:
5309 if (uEAX < ELEMENTS(apsz))
5310 {
5311 uint32_t au32Args[8] = {0};
5312 PGMPhysReadGCPtr(pVM, au32Args, CPUMGetGuestESP(pVM), sizeof(au32Args));
5313 RTLogPrintf("REM: OpenBSD syscall %3d: %s (eip=%08x %08x %08x %08x %08x %08x %08x %08x %08x)\n",
5314 uEAX, apsz[uEAX], CPUMGetGuestEIP(pVM), au32Args[0], au32Args[1], au32Args[2], au32Args[3],
5315 au32Args[4], au32Args[5], au32Args[6], au32Args[7]);
5316 }
5317 else
5318 RTLogPrintf("eip=%08x: OpenBSD syscall %d (#%x) unknown!!\n", CPUMGetGuestEIP(pVM), uEAX, uEAX);
5319 break;
5320 }
5321}
5322
5323
5324#if defined(IPRT_NO_CRT) && defined(RT_OS_WINDOWS) && defined(RT_ARCH_X86)
5325/**
5326 * The Dll main entry point (stub).
5327 */
5328bool __stdcall _DllMainCRTStartup(void *hModule, uint32_t dwReason, void *pvReserved)
5329{
5330 return true;
5331}
5332
5333void *memcpy(void *dst, const void *src, size_t size)
5334{
5335 uint8_t*pbDst = dst, *pbSrc = src;
5336 while (size-- > 0)
5337 *pbDst++ = *pbSrc++;
5338 return dst;
5339}
5340
5341#endif
5342
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