VirtualBox

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

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

Restore the PGM_DYNAMIC_RAM_ALLOC tests and #include <VBox/pgm.h> to make sure it's defined.

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