VirtualBox

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

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

warnings.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette