VirtualBox

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

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

Removed the old recompiler code.

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