VirtualBox

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

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

Synced V86 changes.

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

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