VirtualBox

source: vbox/trunk/include/VBox/vm.h@ 659

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

More room

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.8 KB
Line 
1/** @file
2 * VM - The Virtual Machine, data.
3 */
4
5/*
6 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21
22#ifndef __VBox_vm_h__
23#define __VBox_vm_h__
24
25#include <VBox/cdefs.h>
26#include <VBox/types.h>
27#include <VBox/cpum.h>
28#include <VBox/stam.h>
29#include <VBox/vmapi.h>
30
31
32/** @defgroup grp_vm The Virtual Machine
33 * @{
34 */
35
36/** The name of the Guest Context VMM Core module. */
37#define VMMGC_MAIN_MODULE_NAME "VMMGC.gc"
38/** The name of the Ring 0 Context VMM Core module. */
39#define VMMR0_MAIN_MODULE_NAME "VMMR0.r0"
40
41/** VM Forced Action Flags.
42 *
43 * Use the VM_FF_SET() and VM_FF_CLEAR() macros to change the force
44 * action mask of a VM.
45 *
46 * @{
47 */
48/** This action forces the VM to service check and pending interrups on the APIC. */
49#define VM_FF_INTERRUPT_APIC BIT(0)
50/** This action forces the VM to service check and pending interrups on the PIC. */
51#define VM_FF_INTERRUPT_PIC BIT(1)
52/** This action forces the VM to schedule and run pending timer (TM). */
53#define VM_FF_TIMER BIT(2)
54/** PDM Queues are pending. */
55#define VM_FF_PDM_QUEUES BIT(3)
56/** PDM DMA transfers are pending. */
57#define VM_FF_PDM_DMA BIT(4)
58/** PDM critical section unlocking is pending, process promptly upon return to R3. */
59#define VM_FF_PDM_CRITSECT BIT(5)
60
61/** This action forces the VM to call DBGF so DBGF can service debugger
62 * requests in the emulation thread.
63 * This action flag stays asserted till DBGF clears it.*/
64#define VM_FF_DBGF BIT(8)
65/** This action forces the VM to service pending requests from other
66 * thread or requests which must be executed in another context. */
67#define VM_FF_REQUEST BIT(9)
68/** Terminate the VM immediately. */
69#define VM_FF_TERMINATE BIT(10)
70/** Reset the VM. (postponed) */
71#define VM_FF_RESET BIT(11)
72
73/** This action forces the VM to resync the page tables before going
74 * back to execute guest code. (GLOBAL FLUSH) */
75#define VM_FF_PGM_SYNC_CR3 BIT(16)
76/** Same as VM_FF_PGM_SYNC_CR3 except that global pages can be skipped.
77 * (NON-GLOBAL FLUSH) */
78#define VM_FF_PGM_SYNC_CR3_NON_GLOBAL BIT(17)
79/** Check the interupt and trap gates */
80#define VM_FF_TRPM_SYNC_IDT BIT(18)
81/** Check Guest's TSS ring 0 stack */
82#define VM_FF_SELM_SYNC_TSS BIT(19)
83/** Check Guest's GDT table */
84#define VM_FF_SELM_SYNC_GDT BIT(20)
85/** Check Guest's LDT table */
86#define VM_FF_SELM_SYNC_LDT BIT(21)
87/** Inhibit interrupts pending. See EMGetInhibitInterruptsPC(). */
88#define VM_FF_INHIBIT_INTERRUPTS BIT(22)
89
90/** CSAM needs to scan the page that's being executed */
91#define VM_FF_CSAM_SCAN_PAGE BIT(24)
92/** CSAM needs to flush a code page that has been modified. */
93#define VM_FF_CSAM_FLUSH_DIRTY_PAGE BIT(25)
94
95/** Force return to Ring-3. */
96#define VM_FF_TO_R3 BIT(28)
97
98/** Suspend the VM - debug only. */
99#define VM_FF_DEBUG_SUSPEND BIT(31)
100
101/** Externally forced actions. Used to quit the idle/wait loop. */
102#define VM_FF_EXTERNAL_SUSPENDED_MASK (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_REQUEST)
103/** Externally forced actions. Used to quit the idle/wait loop. */
104#define VM_FF_EXTERNAL_HALTED_MASK (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_TIMER | VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC | VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA)
105/** High priority pre-execution actions. */
106#define VM_FF_HIGH_PRIORITY_PRE_MASK (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_INTERRUPT_APIC | VM_FF_INTERRUPT_PIC | VM_FF_TIMER | VM_FF_DEBUG_SUSPEND \
107 | VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL | VM_FF_SELM_SYNC_TSS | VM_FF_TRPM_SYNC_IDT | VM_FF_SELM_SYNC_GDT | VM_FF_SELM_SYNC_LDT)
108/** High priority pre raw-mode execution mask. */
109#define VM_FF_HIGH_PRIORITY_PRE_RAW_MASK (VM_FF_PGM_SYNC_CR3 | VM_FF_PGM_SYNC_CR3_NON_GLOBAL | VM_FF_SELM_SYNC_TSS | VM_FF_TRPM_SYNC_IDT | VM_FF_SELM_SYNC_GDT | VM_FF_SELM_SYNC_LDT | VM_FF_INHIBIT_INTERRUPTS)
110/** High priority post-execution actions. */
111#define VM_FF_HIGH_PRIORITY_POST_MASK (VM_FF_PDM_CRITSECT|VM_FF_CSAM_FLUSH_DIRTY_PAGE)
112/** Normal priority post-execution actions. */
113#define VM_FF_NORMAL_PRIORITY_POST_MASK (VM_FF_TERMINATE | VM_FF_DBGF | VM_FF_RESET | VM_FF_CSAM_SCAN_PAGE)
114/** Normal priority actions. */
115#define VM_FF_NORMAL_PRIORITY_MASK (VM_FF_REQUEST | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA)
116/** Flags to check before resuming guest execution. */
117#define VM_FF_RESUME_GUEST_MASK (VM_FF_TO_R3)
118/** All the forced flags. */
119#define VM_FF_ALL_MASK (~0U)
120/** All the forced flags. */
121#define VM_FF_ALL_BUT_RAW_MASK (~(VM_FF_HIGH_PRIORITY_PRE_RAW_MASK | VM_FF_CSAM_FLUSH_DIRTY_PAGE | VM_FF_PDM_CRITSECT))
122
123/** @} */
124
125/** @def VM_FF_SET
126 * Sets a force action flag.
127 *
128 * @param pVM VM Handle.
129 * @param fFlag The flag to set.
130 */
131#if 1
132# define VM_FF_SET(pVM, fFlag) ASMAtomicOrU32(&(pVM)->fForcedActions, (fFlag))
133#else
134# define VM_FF_SET(pVM, fFlag) \
135 do { ASMAtomicOrU32(&(pVM)->fForcedActions, (fFlag)); \
136 RTLogPrintf("VM_FF_SET : %08x %s - %s(%d) %s\n", (pVM)->fForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
137 } while (0)
138#endif
139
140/** @def VM_FF_CLEAR
141 * Clears a force action flag.
142 *
143 * @param pVM VM Handle.
144 * @param fFlag The flag to clear.
145 */
146#if 1
147# define VM_FF_CLEAR(pVM, fFlag) ASMAtomicAndU32(&(pVM)->fForcedActions, ~(fFlag))
148#else
149# define VM_FF_CLEAR(pVM, fFlag) \
150 do { ASMAtomicAndU32(&(pVM)->fForcedActions, ~(fFlag)); \
151 RTLogPrintf("VM_FF_CLEAR: %08x %s - %s(%d) %s\n", (pVM)->fForcedActions, #fFlag, __FILE__, __LINE__, __FUNCTION__); \
152 } while (0)
153#endif
154
155/** @def VM_FF_ISSET
156 * Checks if a force action flag is set.
157 *
158 * @param pVM VM Handle.
159 * @param fFlag The flag to check.
160 */
161#define VM_FF_ISSET(pVM, fFlag) (((pVM)->fForcedActions & (fFlag)) == (fFlag))
162
163/** @def VM_FF_ISPENDING
164 * Checks if one or more force action in the specified set is pending.
165 *
166 * @param pVM VM Handle.
167 * @param fFlags The flags to check for.
168 */
169#define VM_FF_ISPENDING(pVM, fFlags) ((pVM)->fForcedActions & (fFlags))
170
171
172/** @def VM_IS_EMT
173 * Checks if the current thread is the emulation thread (EMT).
174 *
175 * @remark The ring-0 variation will need attention if we expand the ring-0
176 * code to let threads other than EMT mess around with the VM.
177 */
178#ifdef IN_GC
179# define VM_IS_EMT(pVM) true
180#elif defined(IN_RING0)
181# define VM_IS_EMT(pVM) true
182#else
183# define VM_IS_EMT(pVM) ((pVM)->NativeThreadEMT == RTThreadNativeSelf())
184#endif
185
186/** @def VM_ASSERT_EMT
187 * Asserts that the current thread IS the emulation thread (EMT).
188 */
189#ifdef IN_GC
190# define VM_ASSERT_EMT(pVM) Assert(VM_IS_EMT(pVM))
191#elif defined(IN_RING0)
192# define VM_ASSERT_EMT(pVM) Assert(VM_IS_EMT(pVM))
193#else
194# define VM_ASSERT_EMT(pVM) \
195 AssertMsg(VM_IS_EMT(pVM), \
196 ("Not emulation thread! Thread=%RTnthrd ThreadEMT=%RTnthrd\n", RTThreadNativeSelf(), pVM->NativeThreadEMT))
197#endif
198
199
200/**
201 * Asserts that the current thread is NOT the emulation thread.
202 */
203#define VM_ASSERT_OTHER_THREAD(pVM) \
204 AssertMsg(!VM_IS_EMT(pVM), ("Not other thread!!\n"))
205
206
207
208/** This is the VM structure.
209 *
210 * It contains (nearly?) all the VM data which have to be available in all
211 * contexts. Even if it contains all the data the idea is to use APIs not
212 * to modify all the members all around the place. Therefore we make use of
213 * unions to hide everything which isn't local to the current source module.
214 * This means we'll have to pay a little bit of attention when adding new
215 * members to structures in the unions and make sure to keep the padding sizes
216 * up to date.
217 *
218 * Run tstVMStructSize after update!
219 */
220typedef struct VM
221{
222 /** The state of the VM.
223 * This field is read only to everyone except the VM and EM. */
224 VMSTATE enmVMState;
225 /** Forced action flags.
226 * See the VM_FF_* \#defines. Updated atomically.
227 */
228 volatile uint32_t fForcedActions;
229 /** Physical address (real) of this structure. */
230 RTHCPHYS HCPhysVM;
231 /** Session handle. For use when calling SUPR0 APIs. */
232 HCPTRTYPE(PSUPDRVSESSION) pSession;
233 /** Pointer to the next VM.
234 * We keep a per process list of VM for the event that a process could
235 * contain more than one VM.
236 */
237 HCPTRTYPE(struct VM *) pNext;
238 /** Host Context VM Pointer.
239 * @obsolete don't use in new code! */
240 HCPTRTYPE(struct VM *) pVMHC;
241 /** Ring-3 Host Context VM Pointer. */
242 R3PTRTYPE(struct VM *) pVMR3;
243 /** Ring-0 Host Context VM Pointer. */
244 R0PTRTYPE(struct VM *) pVMR0;
245 /** Guest Context VM Pointer. */
246 GCPTRTYPE(struct VM *) pVMGC;
247
248 /** @name Public VMM Switcher APIs
249 * @{ */
250 /**
251 * Assembly switch entry point for returning to host context.
252 * This function will clean up the stack frame.
253 *
254 * @param eax The return code, register.
255 * @param Ctx The guest core context.
256 * @remark Assume interrupts disabled.
257 */
258 RTGCPTR pfnVMMGCGuestToHostAsmGuestCtx/*(int32_t eax, CPUMCTXCORE Ctx)*/;
259
260 /**
261 * Assembly switch entry point for returning to host context.
262 *
263 * This is an alternative entry point which we'll be using when the we have the
264 * hypervisor context and need to save that before going to the host.
265 *
266 * This is typically useful when abandoning the hypervisor because of a trap
267 * and want the trap state to be saved.
268 *
269 * @param eax The return code, register.
270 * @param ecx Pointer to the hypervisor core context, register.
271 * @remark Assume interrupts disabled.
272 */
273 RTGCPTR pfnVMMGCGuestToHostAsmHyperCtx/*(int32_t eax, PCPUMCTXCORE ecx)*/;
274
275 /**
276 * Assembly switch entry point for returning to host context.
277 *
278 * This is an alternative to the two *Ctx APIs and implies that the context has already
279 * been saved, or that it's just a brief return to HC and that the caller intends to resume
280 * whatever it is doing upon 'return' from this call.
281 *
282 * @param eax The return code, register.
283 * @remark Assume interrupts disabled.
284 */
285 RTGCPTR pfnVMMGCGuestToHostAsm/*(int32_t eax)*/;
286 /** @} */
287
288
289 /** @name Various VM data owned by VM.
290 * @{ */
291 /** The thread handle of the emulation thread.
292 * Use the VM_IS_EMT() macro to check if executing in EMT. */
293 RTTHREAD ThreadEMT;
294 /** The native handle of ThreadEMT. Getting the native handle
295 * is generally faster than getting the IPRT one (except on OS/2 :-). */
296 RTNATIVETHREAD NativeThreadEMT;
297 /** @} */
298
299
300 /** @name Various items that are frequently accessed.
301 * @{ */
302 /** Raw ring-3 indicator. */
303 bool fRawR3Enabled;
304 /** Raw ring-0 indicator. */
305 bool fRawR0Enabled;
306 /** PATM enabled flag.
307 * This is placed here for performance reasons. */
308 bool fPATMEnabled;
309 /** CSAM enabled flag.
310 * This is placed here for performance reasons. */
311 bool fCSAMEnabled;
312
313 /** Hardware VM support is available and enabled.
314 * This is placed here for performance reasons. */
315 bool fHWACCMEnabled;
316 /** @} */
317
318
319 /* padding to make gnuc put the StatQemuToGC where msc does. */
320#if HC_ARCH_BITS == 32
321 uint32_t padding0;
322#endif
323
324 /** Profiling the total time from Qemu to GC. */
325 STAMPROFILEADV StatTotalQemuToGC;
326 /** Profiling the total time from GC to Qemu. */
327 STAMPROFILEADV StatTotalGCToQemu;
328 /** Profiling the total time spent in GC. */
329 STAMPROFILEADV StatTotalInGC;
330 /** Profiling the total time spent not in Qemu. */
331 STAMPROFILEADV StatTotalInQemu;
332 /** Profiling the VMMSwitcher code for going to GC. */
333 STAMPROFILEADV StatSwitcherToGC;
334 /** Profiling the VMMSwitcher code for going to HC. */
335 STAMPROFILEADV StatSwitcherToHC;
336 STAMPROFILEADV StatSwitcherSaveRegs;
337 STAMPROFILEADV StatSwitcherSysEnter;
338 STAMPROFILEADV StatSwitcherDebug;
339 STAMPROFILEADV StatSwitcherCR0;
340 STAMPROFILEADV StatSwitcherCR4;
341 STAMPROFILEADV StatSwitcherJmpCR3;
342 STAMPROFILEADV StatSwitcherRstrRegs;
343 STAMPROFILEADV StatSwitcherLgdt;
344 STAMPROFILEADV StatSwitcherLidt;
345 STAMPROFILEADV StatSwitcherLldt;
346 STAMPROFILEADV StatSwitcherTSS;
347
348 /* padding - the unions must be aligned on 32 bytes boundraries. */
349 uint32_t padding[HC_ARCH_BITS == 32 ? 4 : 6];
350
351 /** CPUM part. */
352 union
353 {
354#ifdef __CPUMInternal_h__
355 struct CPUM s;
356#endif
357 char padding[HC_ARCH_BITS == 32 ? 3424 : 3552]; /* multiple of 32 */
358 } cpum;
359
360 /** VMM part. */
361 union
362 {
363#ifdef __VMMInternal_h__
364 struct VMM s;
365#endif
366 char padding[1024]; /* multiple of 32 */
367 } vmm;
368
369 /** PGM part. */
370 union
371 {
372#ifdef __PGMInternal_h__
373 struct PGM s;
374#endif
375 char padding[50*1024]; /* multiple of 32 */
376 } pgm;
377
378 /** HWACCM part. */
379 union
380 {
381#ifdef __HWACCMInternal_h__
382 struct HWACCM s;
383#endif
384 char padding[1024]; /* multiple of 32 */
385 } hwaccm;
386
387 /** TRPM part. */
388 union
389 {
390#ifdef __TRPMInternal_h__
391 struct TRPM s;
392#endif
393 char padding[5344]; /* multiple of 32 */
394 } trpm;
395
396 /** SELM part. */
397 union
398 {
399#ifdef __SELMInternal_h__
400 struct SELM s;
401#endif
402 char padding[544]; /* multiple of 32 */
403 } selm;
404
405 /** MM part. */
406 union
407 {
408#ifdef __MMInternal_h__
409 struct MM s;
410#endif
411 char padding[128]; /* multiple of 32 */
412 } mm;
413
414 /** CFGM part. */
415 union
416 {
417#ifdef __CFGMInternal_h__
418 struct CFGM s;
419#endif
420 char padding[32]; /* multiple of 32 */
421 } cfgm;
422
423 /** PDM part. */
424 union
425 {
426#ifdef __PDMInternal_h__
427 struct PDM s;
428#endif
429 char padding[1024]; /* multiple of 32 */
430 } pdm;
431
432 /** IOM part. */
433 union
434 {
435#ifdef __IOMInternal_h__
436 struct IOM s;
437#endif
438 char padding[4544]; /* multiple of 32 */
439 } iom;
440
441 /** PATM part. */
442 union
443 {
444#ifdef __PATMInternal_h__
445 struct PATM s;
446#endif
447 char padding[768]; /* multiple of 32 */
448 } patm;
449
450 /** CSAM part. */
451 union
452 {
453#ifdef __CSAMInternal_h__
454 struct CSAM s;
455#endif
456 char padding[3328]; /* multiple of 32 */
457 } csam;
458
459 /** EM part. */
460 union
461 {
462#ifdef __EMInternal_h__
463 struct EM s;
464#endif
465 char padding[1344]; /* multiple of 32 */
466 } em;
467
468 /** TM part. */
469 union
470 {
471#ifdef __TMInternal_h__
472 struct TM s;
473#endif
474 char padding[768]; /* multiple of 32 */
475 } tm;
476
477 /** DBGF part. */
478 union
479 {
480#ifdef __DBGFInternal_h__
481 struct DBGF s;
482#endif
483 char padding[HC_ARCH_BITS == 32 ? 1888 : 1920]; /* multiple of 32 */
484 } dbgf;
485
486 /** STAM part. */
487 union
488 {
489#ifdef __STAMInternal_h__
490 struct STAM s;
491#endif
492 char padding[32]; /* multiple of 32 */
493 } stam;
494
495 /** SSM part. */
496 union
497 {
498#ifdef __SSMInternal_h__
499 struct SSM s;
500#endif
501 char padding[32]; /* multiple of 32 */
502 } ssm;
503
504 /** VM part. */
505 union
506 {
507#ifdef __VMInternal_h__
508 struct VMINT s;
509#endif
510 char padding[640]; /* multiple of 32 */
511 } vm;
512
513 /** REM part. */
514 union
515 {
516#ifdef __REMInternal_h__
517 struct REM s;
518#endif
519 char padding[HC_ARCH_BITS == 32 ? 0x6b00 : 0xbf00]; /* multiple of 32 */
520 } rem;
521} VM;
522
523/** Pointer to a VM. */
524#ifndef __VBox_types_h__
525typedef struct VM *PVM;
526#endif
527
528
529#ifdef IN_GC
530__BEGIN_DECLS
531
532/** The VM structure.
533 * This is imported from the VMMGCBuiltin module, i.e. it's a one
534 * of those magic globals which we should avoid using.
535 */
536extern DECLIMPORT(VM) g_VM;
537
538__END_DECLS
539#endif
540
541/** @} */
542
543#endif
544
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