VirtualBox

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

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

oops.

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