VirtualBox

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

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

More corrections.

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