VirtualBox

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

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

some profiling.

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