VirtualBox

source: vbox/trunk/include/VBox/em.h@ 12065

Last change on this file since 12065 was 11508, checked in by vboxsync, 16 years ago

Added emulation of cmpxchg & lock cmpxchg (not yet used).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.5 KB
Line 
1/** @file
2 * EM - Execution Monitor.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 *
25 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___VBox_em_h
31#define ___VBox_em_h
32
33#include <VBox/cdefs.h>
34#include <VBox/types.h>
35#include <VBox/trpm.h>
36#include <VBox/dis.h>
37
38__BEGIN_DECLS
39
40/** @defgroup grp_em The Execution Monitor API
41 * @{
42 */
43
44/** Enable to allow V86 code to run in raw mode. */
45#define VBOX_RAW_V86
46
47/**
48 * The Execution Manager State.
49 */
50typedef enum EMSTATE
51{
52 /** Not yet started. */
53 EMSTATE_NONE = 1,
54 /** Raw-mode execution. */
55 EMSTATE_RAW,
56 /** Hardware accelerated raw-mode execution. */
57 EMSTATE_HWACC,
58 /** Recompiled mode execution. */
59 EMSTATE_REM,
60 /** Execution is halted. (waiting for interrupt) */
61 EMSTATE_HALTED,
62 /** Execution is suspended. */
63 EMSTATE_SUSPENDED,
64 /** The VM is terminating. */
65 EMSTATE_TERMINATING,
66 /** Guest debug event from raw-mode is being processed. */
67 EMSTATE_DEBUG_GUEST_RAW,
68 /** Guest debug event from hardware accelerated mode is being processed. */
69 EMSTATE_DEBUG_GUEST_HWACC,
70 /** Guest debug event from recompiled-mode is being processed. */
71 EMSTATE_DEBUG_GUEST_REM,
72 /** Hypervisor debug event being processed. */
73 EMSTATE_DEBUG_HYPER,
74 /** The VM has encountered a fatal error. (And everyone is panicing....) */
75 EMSTATE_GURU_MEDITATION,
76 /** Just a hack to ensure that we get a 32-bit integer. */
77 EMSTATE_MAKE_32BIT_HACK = 0x7fffffff
78} EMSTATE;
79
80/*
81 * Callback handlers for instruction emulation functions.
82 */
83typedef DECLCALLBACK(uint32_t) PFN_EMULATE_PARAM2_UINT32(void *pvParam1, uint64_t val2);
84typedef DECLCALLBACK(uint32_t) PFN_EMULATE_PARAM2(void *pvParam1, size_t val2);
85typedef DECLCALLBACK(uint32_t) PFN_EMULATE_PARAM3(void *pvParam1, uint64_t val2, size_t val3);
86typedef DECLCALLBACK(int) FNEMULATELOCKPARAM2(void *pvParam1, uint64_t val2, RTGCUINTREG32 *pf);
87typedef FNEMULATELOCKPARAM2 *PFNEMULATELOCKPARAM2;
88typedef DECLCALLBACK(int) FNEMULATELOCKPARAM3(void *pvParam1, uint64_t val2, size_t cb, RTGCUINTREG32 *pf);
89typedef FNEMULATELOCKPARAM3 *PFNEMULATELOCKPARAM3;
90
91/**
92 * Get the current execution manager status.
93 *
94 * @returns Current status.
95 */
96EMDECL(EMSTATE) EMGetState(PVM pVM);
97
98/**
99 * Checks if raw ring-3 execute mode is enabled.
100 *
101 * @returns true if enabled.
102 * @returns false if disabled.
103 * @param pVM The VM to operate on.
104 */
105#define EMIsRawRing3Enabled(pVM) ((pVM)->fRawR3Enabled)
106
107/**
108 * Checks if raw ring-0 execute mode is enabled.
109 *
110 * @returns true if enabled.
111 * @returns false if disabled.
112 * @param pVM The VM to operate on.
113 */
114#define EMIsRawRing0Enabled(pVM) ((pVM)->fRawR0Enabled)
115
116/**
117 * Sets the PC for which interrupts should be inhibited.
118 *
119 * @param pVM The VM handle.
120 * @param PC The PC.
121 */
122EMDECL(void) EMSetInhibitInterruptsPC(PVM pVM, RTGCUINTPTR PC);
123
124/**
125 * Gets the PC for which interrupts should be inhibited.
126 *
127 * There are a few instructions which inhibits or delays interrupts
128 * for the instruction following them. These instructions are:
129 * - STI
130 * - MOV SS, r/m16
131 * - POP SS
132 *
133 * @returns The PC for which interrupts should be inhibited.
134 * @param pVM VM handle.
135 *
136 */
137EMDECL(RTGCUINTPTR) EMGetInhibitInterruptsPC(PVM pVM);
138
139/**
140 * Disassembles one instruction.
141 *
142 * @param pVM The VM handle.
143 * @param pCtxCore The context core (used for both the mode and instruction).
144 * @param pCpu Where to return the parsed instruction info.
145 * @param pcbInstr Where to return the instruction size. (optional)
146 */
147EMDECL(int) EMInterpretDisasOne(PVM pVM, PCCPUMCTXCORE pCtxCore, PDISCPUSTATE pCpu, unsigned *pcbInstr);
148
149/**
150 * Disassembles one instruction.
151 *
152 * This is used by internally by the interpreter and by trap/access handlers.
153 *
154 * @param pVM The VM handle.
155 * @param GCPtrInstr The flat address of the instruction.
156 * @param pCtxCore The context core (used to determin the cpu mode).
157 * @param pCpu Where to return the parsed instruction info.
158 * @param pcbInstr Where to return the instruction size. (optional)
159 */
160EMDECL(int) EMInterpretDisasOneEx(PVM pVM, RTGCUINTPTR GCPtrInstr, PCCPUMCTXCORE pCtxCore,
161 PDISCPUSTATE pCpu, unsigned *pcbInstr);
162
163/**
164 * Interprets the current instruction.
165 *
166 * @returns VBox status code.
167 * @retval VINF_* Scheduling instructions.
168 * @retval VERR_EM_INTERPRETER Something we can't cope with.
169 * @retval VERR_* Fatal errors.
170 *
171 * @param pVM The VM handle.
172 * @param pRegFrame The register frame.
173 * Updates the EIP if an instruction was executed successfully.
174 * @param pvFault The fault address (CR2).
175 * @param pcbSize Size of the write (if applicable).
176 *
177 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
178 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
179 * to worry about e.g. invalid modrm combinations (!)
180 */
181EMDECL(int) EMInterpretInstruction(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize);
182
183/**
184 * Interprets the current instruction using the supplied DISCPUSTATE structure.
185 *
186 * EIP is *NOT* updated!
187 *
188 * @returns VBox status code.
189 * @retval VINF_* Scheduling instructions. When these are returned, it
190 * starts to get a bit tricky to know whether code was
191 * executed or not... We'll address this when it becomes a problem.
192 * @retval VERR_EM_INTERPRETER Something we can't cope with.
193 * @retval VERR_* Fatal errors.
194 *
195 * @param pVM The VM handle.
196 * @param pCpu The disassembler cpu state for the instruction to be interpreted.
197 * @param pRegFrame The register frame. EIP is *NOT* changed!
198 * @param pvFault The fault address (CR2).
199 * @param pcbSize Size of the write (if applicable).
200 *
201 * @remark Invalid opcode exceptions have a higher priority than GP (see Intel
202 * Architecture System Developers Manual, Vol 3, 5.5) so we don't need
203 * to worry about e.g. invalid modrm combinations (!)
204 */
205EMDECL(int) EMInterpretInstructionCPU(PVM pVM, PDISCPUSTATE pCpu, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, uint32_t *pcbSize);
206
207/**
208 * Interpret CPUID given the parameters in the CPU context
209 *
210 * @returns VBox status code.
211 * @param pVM The VM handle.
212 * @param pRegFrame The register frame.
213 *
214 */
215EMDECL(int) EMInterpretCpuId(PVM pVM, PCPUMCTXCORE pRegFrame);
216
217/**
218 * Interpret RDTSC
219 *
220 * @returns VBox status code.
221 * @param pVM The VM handle.
222 * @param pRegFrame The register frame.
223 *
224 */
225EMDECL(int) EMInterpretRdtsc(PVM pVM, PCPUMCTXCORE pRegFrame);
226
227/**
228 * Interpret INVLPG
229 *
230 * @returns VBox status code.
231 * @param pVM The VM handle.
232 * @param pRegFrame The register frame.
233 * @param pAddrGC Operand address
234 *
235 */
236EMDECL(int) EMInterpretInvlpg(PVM pVM, PCPUMCTXCORE pRegFrame, RTGCPTR pAddrGC);
237
238/**
239 * Interpret IRET (currently only to V86 code)
240 *
241 * @returns VBox status code.
242 * @param pVM The VM handle.
243 * @param pRegFrame The register frame.
244 *
245 */
246EMDECL(int) EMInterpretIret(PVM pVM, PCPUMCTXCORE pRegFrame);
247
248/**
249 * Interpret DRx write
250 *
251 * @returns VBox status code.
252 * @param pVM The VM handle.
253 * @param pRegFrame The register frame.
254 * @param DestRegDRx DRx register index (USE_REG_DR*)
255 * @param SrcRegGen General purpose register index (USE_REG_E**))
256 *
257 */
258EMDECL(int) EMInterpretDRxWrite(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegDrx, uint32_t SrcRegGen);
259
260/**
261 * Interpret DRx read
262 *
263 * @returns VBox status code.
264 * @param pVM The VM handle.
265 * @param pRegFrame The register frame.
266 * @param DestRegGen General purpose register index (USE_REG_E**))
267 * @param SrcRegDRx DRx register index (USE_REG_DR*)
268 *
269 */
270EMDECL(int) EMInterpretDRxRead(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegDrx);
271
272/**
273 * Interpret CRx write
274 *
275 * @returns VBox status code.
276 * @param pVM The VM handle.
277 * @param pRegFrame The register frame.
278 * @param DestRegCRx DRx register index (USE_REG_CR*)
279 * @param SrcRegGen General purpose register index (USE_REG_E**))
280 *
281 */
282EMDECL(int) EMInterpretCRxWrite(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegCrx, uint32_t SrcRegGen);
283
284/**
285 * Interpret CRx read
286 *
287 * @returns VBox status code.
288 * @param pVM The VM handle.
289 * @param pRegFrame The register frame.
290 * @param DestRegGen General purpose register index (USE_REG_E**))
291 * @param SrcRegCRx CRx register index (USE_REG_CR*)
292 *
293 */
294EMDECL(int) EMInterpretCRxRead(PVM pVM, PCPUMCTXCORE pRegFrame, uint32_t DestRegGen, uint32_t SrcRegCrx);
295
296/**
297 * Interpret LMSW
298 *
299 * @returns VBox status code.
300 * @param pVM The VM handle.
301 * @param u16Data LMSW source data.
302 */
303EMDECL(int) EMInterpretLMSW(PVM pVM, uint16_t u16Data);
304
305/**
306 * Interpret CLTS
307 *
308 * @returns VBox status code.
309 * @param pVM The VM handle.
310 *
311 */
312EMDECL(int) EMInterpretCLTS(PVM pVM);
313
314/**
315 * Interpret a port I/O instruction.
316 *
317 * @returns VBox status code suitable for scheduling.
318 * @param pVM The VM handle.
319 * @param pCtxCore The context core. This will be updated on successful return.
320 * @param pCpu The instruction to interpret.
321 * @param cbOp The size of the instruction.
322 * @remark This may raise exceptions.
323 */
324EMDECL(int) EMInterpretPortIO(PVM pVM, PCPUMCTXCORE pCtxCore, PDISCPUSTATE pCpu, uint32_t cbOp);
325
326EMDECL(uint32_t) EMEmulateCmp(uint32_t u32Param1, uint64_t u64Param2, size_t cb);
327EMDECL(uint32_t) EMEmulateAnd(void *pvParam1, uint64_t u64Param2, size_t cb);
328EMDECL(uint32_t) EMEmulateInc(void *pvParam1, size_t cb);
329EMDECL(uint32_t) EMEmulateDec(void *pvParam1, size_t cb);
330EMDECL(uint32_t) EMEmulateOr(void *pvParam1, uint64_t u64Param2, size_t cb);
331EMDECL(int) EMEmulateLockOr(void *pvParam1, uint64_t u64Param2, size_t cbSize, RTGCUINTREG32 *pf);
332EMDECL(uint32_t) EMEmulateXor(void *pvParam1, uint64_t u64Param2, size_t cb);
333EMDECL(uint32_t) EMEmulateAdd(void *pvParam1, uint64_t u64Param2, size_t cb);
334EMDECL(uint32_t) EMEmulateSub(void *pvParam1, uint64_t u64Param2, size_t cb);
335EMDECL(uint32_t) EMEmulateAdcWithCarrySet(void *pvParam1, uint64_t u64Param2, size_t cb);
336EMDECL(uint32_t) EMEmulateBtr(void *pvParam1, uint64_t u64Param2);
337EMDECL(int) EMEmulateLockBtr(void *pvParam1, uint64_t u64Param2, RTGCUINTREG32 *pf);
338EMDECL(uint32_t) EMEmulateBts(void *pvParam1, uint64_t u64Param2);
339EMDECL(uint32_t) EMEmulateBtc(void *pvParam1, uint64_t u64Param2);
340EMDECL(uint32_t) EMEmulateCmpXchg(void *pvParam1, uint64_t *pu32Param2, uint64_t u32Param3, size_t cbSize);
341EMDECL(uint32_t) EMEmulateLockCmpXchg(void *pvParam1, uint64_t *pu64Param2, uint64_t u64Param3, size_t cbSize);
342EMDECL(uint32_t) EMEmulateCmpXchg8b32(RTHCPTR pu32Param1, uint32_t *pEAX, uint32_t *pEDX, uint32_t uEBX, uint32_t uECX);
343EMDECL(uint32_t) EMEmulateLockCmpXchg8b(RTHCPTR pu32Param1, uint32_t *pEAX, uint32_t *pEDX, uint32_t uEBX, uint32_t uECX);
344EMDECL(int) EMInterpretRdmsr(PVM pVM, PCPUMCTXCORE pRegFrame);
345EMDECL(int) EMInterpretWrmsr(PVM pVM, PCPUMCTXCORE pRegFrame);
346
347#ifdef IN_RING3
348/** @defgroup grp_em_r3 The EM Host Context Ring-3 API
349 * @ingroup grp_em
350 * @{
351 */
352
353/**
354 * Initializes the EM.
355 *
356 * @returns VBox status code.
357 * @param pVM The VM to operate on.
358 */
359EMR3DECL(int) EMR3Init(PVM pVM);
360
361/**
362 * Applies relocations to data and code managed by this
363 * component. This function will be called at init and
364 * whenever the VMM need to relocate it self inside the GC.
365 *
366 * @param pVM The VM.
367 */
368EMR3DECL(void) EMR3Relocate(PVM pVM);
369
370/**
371 * Reset notification.
372 *
373 * @param pVM
374 */
375EMR3DECL(void) EMR3Reset(PVM pVM);
376
377/**
378 * Terminates the EM.
379 *
380 * Termination means cleaning up and freeing all resources,
381 * the VM it self is at this point powered off or suspended.
382 *
383 * @returns VBox status code.
384 * @param pVM The VM to operate on.
385 */
386EMR3DECL(int) EMR3Term(PVM pVM);
387
388
389/**
390 * Command argument for EMR3RawSetMode().
391 *
392 * It's possible to extend this interface to change several
393 * execution modes at once should the need arise.
394 */
395typedef enum EMRAWMODE
396{
397 /** No raw execution. */
398 EMRAW_NONE = 0,
399 /** Enable Only ring-3 raw execution. */
400 EMRAW_RING3_ENABLE,
401 /** Only ring-3 raw execution. */
402 EMRAW_RING3_DISABLE,
403 /** Enable raw ring-0 execution. */
404 EMRAW_RING0_ENABLE,
405 /** Disable raw ring-0 execution. */
406 EMRAW_RING0_DISABLE,
407 EMRAW_END
408} EMRAWMODE;
409
410/**
411 * Enables or disables a set of raw-mode execution modes.
412 *
413 * @returns VINF_SUCCESS on success.
414 * @returns VINF_RESCHEDULE if a rescheduling might be required.
415 * @returns VERR_INVALID_PARAMETER on an invalid enmMode value.
416 *
417 * @param pVM The VM to operate on.
418 * @param enmMode The execution mode change.
419 * @thread The emulation thread.
420 */
421EMR3DECL(int) EMR3RawSetMode(PVM pVM, EMRAWMODE enmMode);
422
423/**
424 * Raise a fatal error.
425 *
426 * Safely terminate the VM with full state report and stuff. This function
427 * will naturally never return.
428 *
429 * @param pVM VM handle.
430 * @param rc VBox status code.
431 */
432EMR3DECL(DECLNORETURN(void)) EMR3FatalError(PVM pVM, int rc);
433
434/**
435 * Execute VM
436 *
437 * This function is the main loop of the VM. The emulation thread
438 * calls this function when the VM has been successfully constructed
439 * and we're ready for executing the VM.
440 *
441 * Returning from this function means that the VM is turned off or
442 * suspended (state already saved) and deconstruction in next in line.
443 *
444 * @returns VBox status code.
445 * @param pVM The VM to operate on.
446 */
447EMR3DECL(int) EMR3ExecuteVM(PVM pVM);
448
449/**
450 * Check for pending raw actions
451 *
452 * @returns VBox status code.
453 * @param pVM The VM to operate on.
454 */
455EMR3DECL(int) EMR3CheckRawForcedActions(PVM pVM);
456
457/**
458 * Interpret instructions.
459 * This works directly on the Guest CPUM context.
460 * The interpretation will try execute at least one instruction. It will
461 * stop when a we're better off in a raw or recompiler mode.
462 *
463 * @returns Todo - status describing what to do next?
464 * @param pVM The VM to operate on.
465 */
466EMR3DECL(int) EMR3Interpret(PVM pVM);
467
468/** @} */
469#endif
470
471
472#ifdef IN_GC
473/** @defgroup grp_em_gc The EM Guest Context API
474 * @ingroup grp_em
475 * @{
476 */
477
478/**
479 * Decide what to do with a trap.
480 *
481 * @returns Next VMM state.
482 * @returns Might not return at all?
483 * @param pVM The VM to operate on.
484 * @param uTrap The trap number.
485 * @param pRegFrame Register frame to operate on.
486 */
487EMGCDECL(int) EMGCTrap(PVM pVM, unsigned uTrap, PCPUMCTXCORE pRegFrame);
488
489EMGCDECL(uint32_t) EMGCEmulateLockCmpXchg(RTRCPTR pu32Param1, uint32_t *pu32Param2, uint32_t u32Param3, size_t cbSize, uint32_t *pEflags);
490EMGCDECL(uint32_t) EMGCEmulateCmpXchg(RTRCPTR pu32Param1, uint32_t *pu32Param2, uint32_t u32Param3, size_t cbSize, uint32_t *pEflags);
491EMGCDECL(uint32_t) EMGCEmulateLockCmpXchg8b(RTRCPTR pu32Param1, uint32_t *pEAX, uint32_t *pEDX, uint32_t uEBX, uint32_t uECX, uint32_t *pEflags);
492EMGCDECL(uint32_t) EMGCEmulateCmpXchg8b(RTRCPTR pu32Param1, uint32_t *pEAX, uint32_t *pEDX, uint32_t uEBX, uint32_t uECX, uint32_t *pEflags);
493EMGCDECL(uint32_t) EMGCEmulateLockXAdd(RTRCPTR pu32Param1, uint32_t *pu32Param2, size_t cbSize, uint32_t *pEflags);
494EMGCDECL(uint32_t) EMGCEmulateXAdd(RTRCPTR pu32Param1, uint32_t *pu32Param2, size_t cbSize, uint32_t *pEflags);
495
496/** @} */
497#endif
498
499/** @} */
500
501__END_DECLS
502
503#endif
504
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