VirtualBox

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

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

Corrected assertion

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