VirtualBox

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

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

Added cpu_get_tsc stats.

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