VirtualBox

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

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

64-bit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 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.215389.xyz. 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#if HC_ARCH_BITS == 32
320 /* padding to make gnuc put the StatQemuToGC where msc does. */
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#if HC_ARCH_BITS == 32
350 char padding[16];
351#endif
352
353 /** CPUM part. */
354 union
355 {
356#ifdef __CPUMInternal_h__
357 struct CPUM s;
358#endif
359 char padding[3424]; /* multiple of 32 */
360 } cpum;
361
362 /** VMM part. */
363 union
364 {
365#ifdef __VMMInternal_h__
366 struct VMM s;
367#endif
368 char padding[1024]; /* multiple of 32 */
369 } vmm;
370
371 /** PGM part. */
372 union
373 {
374#ifdef __PGMInternal_h__
375 struct PGM s;
376#endif
377 char padding[50*1024]; /* multiple of 32 */
378 } pgm;
379
380 /** HWACCM part. */
381 union
382 {
383#ifdef __HWACCMInternal_h__
384 struct HWACCM s;
385#endif
386 char padding[1024]; /* multiple of 32 */
387 } hwaccm;
388
389 /** TRPM part. */
390 union
391 {
392#ifdef __TRPMInternal_h__
393 struct TRPM s;
394#endif
395 char padding[5344]; /* multiple of 32 */
396 } trpm;
397
398 /** SELM part. */
399 union
400 {
401#ifdef __SELMInternal_h__
402 struct SELM s;
403#endif
404 char padding[544]; /* multiple of 32 */
405 } selm;
406
407 /** MM part. */
408 union
409 {
410#ifdef __MMInternal_h__
411 struct MM s;
412#endif
413 char padding[128]; /* multiple of 32 */
414 } mm;
415
416 /** CFGM part. */
417 union
418 {
419#ifdef __CFGMInternal_h__
420 struct CFGM s;
421#endif
422 char padding[32]; /* multiple of 32 */
423 } cfgm;
424
425 /** PDM part. */
426 union
427 {
428#ifdef __PDMInternal_h__
429 struct PDM s;
430#endif
431 char padding[1024]; /* multiple of 32 */
432 } pdm;
433
434 /** IOM part. */
435 union
436 {
437#ifdef __IOMInternal_h__
438 struct IOM s;
439#endif
440 char padding[4544]; /* multiple of 32 */
441 } iom;
442
443 /** PATM part. */
444 union
445 {
446#ifdef __PATMInternal_h__
447 struct PATM s;
448#endif
449 char padding[768]; /* multiple of 32 */
450 } patm;
451
452 /** CSAM part. */
453 union
454 {
455#ifdef __CSAMInternal_h__
456 struct CSAM s;
457#endif
458 char padding[3328]; /* multiple of 32 */
459 } csam;
460
461 /** EM part. */
462 union
463 {
464#ifdef __EMInternal_h__
465 struct EM s;
466#endif
467 char padding[1344]; /* multiple of 32 */
468 } em;
469
470 /** TM part. */
471 union
472 {
473#ifdef __TMInternal_h__
474 struct TM s;
475#endif
476 char padding[768]; /* multiple of 32 */
477 } tm;
478
479 /** DBGF part. */
480 union
481 {
482#ifdef __DBGFInternal_h__
483 struct DBGF s;
484#endif
485 char padding[1888]; /* multiple of 32 */
486 } dbgf;
487
488 /** STAM part. */
489 union
490 {
491#ifdef __STAMInternal_h__
492 struct STAM s;
493#endif
494 char padding[32]; /* multiple of 32 */
495 } stam;
496
497 /** SSM part. */
498 union
499 {
500#ifdef __SSMInternal_h__
501 struct SSM s;
502#endif
503 char padding[32]; /* multiple of 32 */
504 } ssm;
505
506 /** VM part. */
507 union
508 {
509#ifdef __VMInternal_h__
510 struct VMINT s;
511#endif
512 char padding[512]; /* multiple of 32 */
513 } vm;
514
515 /** REM part. */
516 union
517 {
518#ifdef __REMInternal_h__
519 struct REM s;
520#endif
521 char padding[HC_ARCH_BITS == 32 ? 0x6b00 : 0xbf00]; /* multiple of 32 */
522 } rem;
523} VM;
524
525/** Pointer to a VM. */
526#ifndef __VBox_types_h__
527typedef struct VM *PVM;
528#endif
529
530
531#ifdef IN_GC
532__BEGIN_DECLS
533
534/** The VM structure.
535 * This is imported from the VMMGCBuiltin module, i.e. it's a one
536 * of those magic globals which we should avoid using.
537 */
538extern DECLIMPORT(VM) g_VM;
539
540__END_DECLS
541#endif
542
543/** @} */
544
545#endif
546
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette