VirtualBox

source: vbox/trunk/src/VBox/VMM/VMInternal.h@ 16560

Last change on this file since 16560 was 16311, checked in by vboxsync, 15 years ago

IPRT: Changed the RTTLS type and corrected RTTLSAlloc defintions.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 20.8 KB
Line 
1/* $Id: VMInternal.h 16311 2009-01-28 13:46:41Z vboxsync $ */
2/** @file
3 * VM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#ifndef ___VMInternal_h
23#define ___VMInternal_h
24
25#include <VBox/cdefs.h>
26#include <VBox/vmapi.h>
27#include <setjmp.h>
28
29
30
31/** @defgroup grp_vm_int Internals
32 * @ingroup grp_vm
33 * @internal
34 * @{
35 */
36
37
38/**
39 * At-reset callback type.
40 */
41typedef enum VMATRESETTYPE
42{
43 /** Device callback. */
44 VMATRESETTYPE_DEV = 1,
45 /** Internal callback . */
46 VMATRESETTYPE_INTERNAL,
47 /** External callback. */
48 VMATRESETTYPE_EXTERNAL
49} VMATRESETTYPE;
50
51
52/** Pointer to at-reset callback. */
53typedef struct VMATRESET *PVMATRESET;
54
55/**
56 * At reset callback.
57 */
58typedef struct VMATRESET
59{
60 /** Pointer to the next one in the list. */
61 PVMATRESET pNext;
62 /** Callback type. */
63 VMATRESETTYPE enmType;
64 /** User argument for the callback. */
65 void *pvUser;
66 /** Description. */
67 const char *pszDesc;
68 /** Type specific data. */
69 union
70 {
71 /** VMATRESETTYPE_DEV. */
72 struct
73 {
74 /** Callback. */
75 PFNVMATRESET pfnCallback;
76 /** Device instance. */
77 PPDMDEVINS pDevIns;
78 } Dev;
79
80 /** VMATRESETTYPE_INTERNAL. */
81 struct
82 {
83 /** Callback. */
84 PFNVMATRESETINT pfnCallback;
85 } Internal;
86
87 /** VMATRESETTYPE_EXTERNAL. */
88 struct
89 {
90 /** Callback. */
91 PFNVMATRESETEXT pfnCallback;
92 } External;
93 } u;
94} VMATRESET;
95
96
97/**
98 * VM state change callback.
99 */
100typedef struct VMATSTATE
101{
102 /** Pointer to the next one. */
103 struct VMATSTATE *pNext;
104 /** Pointer to the callback. */
105 PFNVMATSTATE pfnAtState;
106 /** The user argument. */
107 void *pvUser;
108} VMATSTATE;
109/** Pointer to a VM state change callback. */
110typedef VMATSTATE *PVMATSTATE;
111
112
113/**
114 * VM error callback.
115 */
116typedef struct VMATERROR
117{
118 /** Pointer to the next one. */
119 struct VMATERROR *pNext;
120 /** Pointer to the callback. */
121 PFNVMATERROR pfnAtError;
122 /** The user argument. */
123 void *pvUser;
124} VMATERROR;
125/** Pointer to a VM error callback. */
126typedef VMATERROR *PVMATERROR;
127
128
129/**
130 * Chunk of memory allocated off the hypervisor heap in which
131 * we copy the error details.
132 */
133typedef struct VMERROR
134{
135 /** The size of the chunk. */
136 uint32_t cbAllocated;
137 /** The current offset into the chunk.
138 * We start by putting the filename and function immediatly
139 * after the end of the buffer. */
140 uint32_t off;
141 /** Offset from the start of this structure to the file name. */
142 uint32_t offFile;
143 /** The line number. */
144 uint32_t iLine;
145 /** Offset from the start of this structure to the function name. */
146 uint32_t offFunction;
147 /** Offset from the start of this structure to the formatted message text. */
148 uint32_t offMessage;
149 /** The VBox status code. */
150 int32_t rc;
151} VMERROR, *PVMERROR;
152
153
154/**
155 * VM runtime error callback.
156 */
157typedef struct VMATRUNTIMEERROR
158{
159 /** Pointer to the next one. */
160 struct VMATRUNTIMEERROR *pNext;
161 /** Pointer to the callback. */
162 PFNVMATRUNTIMEERROR pfnAtRuntimeError;
163 /** The user argument. */
164 void *pvUser;
165} VMATRUNTIMEERROR;
166/** Pointer to a VM error callback. */
167typedef VMATRUNTIMEERROR *PVMATRUNTIMEERROR;
168
169
170/**
171 * Chunk of memory allocated off the hypervisor heap in which
172 * we copy the runtime error details.
173 */
174typedef struct VMRUNTIMEERROR
175{
176 /** The size of the chunk. */
177 uint32_t cbAllocated;
178 /** The current offset into the chunk.
179 * We start by putting the error ID immediatly
180 * after the end of the buffer. */
181 uint32_t off;
182 /** Offset from the start of this structure to the error ID. */
183 uint32_t offErrorID;
184 /** Offset from the start of this structure to the formatted message text. */
185 uint32_t offMessage;
186 /** Whether the error is fatal or not */
187 bool fFatal;
188} VMRUNTIMEERROR, *PVMRUNTIMEERROR;
189
190/** The halt method. */
191typedef enum
192{
193 /** The usual invalid value. */
194 VMHALTMETHOD_INVALID = 0,
195 /** Use the method used during bootstrapping. */
196 VMHALTMETHOD_BOOTSTRAP,
197 /** Use the default method. */
198 VMHALTMETHOD_DEFAULT,
199 /** The old spin/yield/block method. */
200 VMHALTMETHOD_OLD,
201 /** The first go at a block/spin method. */
202 VMHALTMETHOD_1,
203 /** The first go at a more global approach. */
204 VMHALTMETHOD_GLOBAL_1,
205 /** The end of valid methods. (not inclusive of course) */
206 VMHALTMETHOD_END,
207 /** The usual 32-bit max value. */
208 VMHALTMETHOD_32BIT_HACK = 0x7fffffff
209} VMHALTMETHOD;
210
211
212/**
213 * VM Internal Data (part of the VM structure).
214 *
215 * @todo Move this and all related things to VMM. The VM component was, to some
216 * extent at least, a bad ad hoc design which should all have been put in
217 * VMM. @see pg_vm.
218 */
219typedef struct VMINT
220{
221 /** VM Error Message. */
222 R3PTRTYPE(PVMERROR) pErrorR3;
223 /** VM Runtime Error Message. */
224 R3PTRTYPE(PVMRUNTIMEERROR) pRuntimeErrorR3;
225 /** Set by VMR3SuspendNoSave; cleared by VMR3Resume; signals the VM is in an
226 * inconsistent state and saving is not allowed. */
227 bool fPreventSaveState;
228} VMINT;
229/** Pointer to the VM Internal Data (part of the VM structure). */
230typedef VMINT *PVMINT;
231
232
233/**
234 * VM internal data kept in the UVM.
235 */
236typedef struct VMINTUSERPERVM
237{
238 /** Head of the request queue. Atomic. */
239 volatile PVMREQ pReqs;
240 /** The last index used during alloc/free. */
241 volatile uint32_t iReqFree;
242 /** Number of free request packets. */
243 volatile uint32_t cReqFree;
244 /** Array of pointers to lists of free request packets. Atomic. */
245 volatile PVMREQ apReqFree[9];
246
247#ifdef VBOX_WITH_STATISTICS
248 /** Number of VMR3ReqAlloc returning a new packet. */
249 STAMCOUNTER StatReqAllocNew;
250 /** Number of VMR3ReqAlloc causing races. */
251 STAMCOUNTER StatReqAllocRaces;
252 /** Number of VMR3ReqAlloc returning a recycled packet. */
253 STAMCOUNTER StatReqAllocRecycled;
254 /** Number of VMR3ReqFree calls. */
255 STAMCOUNTER StatReqFree;
256 /** Number of times the request was actually freed. */
257 STAMCOUNTER StatReqFreeOverflow;
258#endif
259
260 /** Pointer to the support library session.
261 * Mainly for creation and destruction.. */
262 PSUPDRVSESSION pSession;
263
264 /** Wait event semaphore. */
265 RTSEMEVENT EventSemWait;
266 /** Wait/Idle indicator. */
267 bool volatile fWait;
268 /** Force EMT to terminate. */
269 bool volatile fTerminateEMT;
270 /** If set the EMT does the final VM cleanup when it exits.
271 * If clear the VMR3Destroy() caller does so. */
272 bool fEMTDoesTheCleanup;
273
274 /** @name Generic Halt data
275 * @{
276 */
277 /** The current halt method.
278 * Can be selected by CFGM option 'VM/HaltMethod'. */
279 VMHALTMETHOD enmHaltMethod;
280 /** The index into g_aHaltMethods of the current halt method. */
281 uint32_t volatile iHaltMethod;
282 /** The average time (ns) between two halts in the last second. (updated once per second) */
283 uint32_t HaltInterval;
284 /** The average halt frequency for the last second. (updated once per second) */
285 uint32_t HaltFrequency;
286 /** The number of halts in the current period. */
287 uint32_t cHalts;
288 uint32_t padding; /**< alignment padding. */
289 /** When we started counting halts in cHalts (RTTimeNanoTS). */
290 uint64_t u64HaltsStartTS;
291 /** @} */
292
293 /** Union containing data and config for the different halt algorithms. */
294 union
295 {
296 /**
297 * Method 1 & 2 - Block whenever possible, and when lagging behind
298 * switch to spinning with regular blocking every 5-200ms (defaults)
299 * depending on the accumulated lag. The blocking interval is adjusted
300 * with the average oversleeping of the last 64 times.
301 *
302 * The difference between 1 and 2 is that we use native absolute
303 * time APIs for the blocking instead of the millisecond based IPRT
304 * interface.
305 */
306 struct
307 {
308 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
309 uint32_t cBlocks;
310 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
311 uint64_t cNSBlockedTooLongAvg;
312 /** Total time spend oversleeping when blocking. */
313 uint64_t cNSBlockedTooLong;
314 /** Total time spent blocking. */
315 uint64_t cNSBlocked;
316 /** The timestamp (RTTimeNanoTS) of the last block. */
317 uint64_t u64LastBlockTS;
318
319 /** When we started spinning relentlessly in order to catch up some of the oversleeping.
320 * This is 0 when we're not spinning. */
321 uint64_t u64StartSpinTS;
322
323 /** The max interval without blocking (when spinning). */
324 uint32_t u32MinBlockIntervalCfg;
325 /** The minimum interval between blocking (when spinning). */
326 uint32_t u32MaxBlockIntervalCfg;
327 /** The value to divide the current lag by to get the raw blocking interval (when spinning). */
328 uint32_t u32LagBlockIntervalDivisorCfg;
329 /** When to start spinning (lag / nano secs). */
330 uint32_t u32StartSpinningCfg;
331 /** When to stop spinning (lag / nano secs). */
332 uint32_t u32StopSpinningCfg;
333 } Method12;
334
335#if 0
336 /**
337 * Method 3 & 4 - Same as method 1 & 2 respectivly, except that we
338 * sprinkle it with yields.
339 */
340 struct
341 {
342 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
343 uint32_t cBlocks;
344 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
345 uint64_t cBlockedTooLongNSAvg;
346 /** Total time spend oversleeping when blocking. */
347 uint64_t cBlockedTooLongNS;
348 /** Total time spent blocking. */
349 uint64_t cBlockedNS;
350 /** The timestamp (RTTimeNanoTS) of the last block. */
351 uint64_t u64LastBlockTS;
352
353 /** How many times we've yielded while cBlockedNS and cBlockedTooLongNS has been accumulating. */
354 uint32_t cYields;
355 /** Avg. time spend oversleeping when yielding. */
356 uint32_t cYieldTooLongNSAvg;
357 /** Total time spend oversleeping when yielding. */
358 uint64_t cYieldTooLongNS;
359 /** Total time spent yielding. */
360 uint64_t cYieldedNS;
361 /** The timestamp (RTTimeNanoTS) of the last block. */
362 uint64_t u64LastYieldTS;
363
364 /** When we started spinning relentlessly in order to catch up some of the oversleeping. */
365 uint64_t u64StartSpinTS;
366 } Method34;
367#endif
368 } Halt;
369
370 /** Profiling the halted state; yielding vs blocking.
371 * @{ */
372 STAMPROFILE StatHaltYield;
373 STAMPROFILE StatHaltBlock;
374 STAMPROFILE StatHaltTimers;
375 STAMPROFILE StatHaltPoll;
376 /** @} */
377
378
379 /** List of registered reset callbacks. */
380 PVMATRESET pAtReset;
381 /** List of registered reset callbacks. */
382 PVMATRESET *ppAtResetNext;
383
384 /** List of registered state change callbacks. */
385 PVMATSTATE pAtState;
386 /** List of registered state change callbacks. */
387 PVMATSTATE *ppAtStateNext;
388
389 /** List of registered error callbacks. */
390 PVMATERROR pAtError;
391 /** List of registered error callbacks. */
392 PVMATERROR *ppAtErrorNext;
393
394 /** List of registered error callbacks. */
395 PVMATRUNTIMEERROR pAtRuntimeError;
396 /** List of registered error callbacks. */
397 PVMATRUNTIMEERROR *ppAtRuntimeErrorNext;
398
399 /** Pointer to the DBGC instance data. */
400 void *pvDBGC;
401
402 /** TLS index for the VMINTUSERPERVMCPU pointer. */
403 RTTLS idxTLS;
404} VMINTUSERPERVM;
405
406/** Pointer to the VM internal data kept in the UVM. */
407typedef VMINTUSERPERVM *PVMINTUSERPERVM;
408
409
410/**
411 * VMCPU internal data kept in the UVM.
412 *
413 * Almost a copy of VMINTUSERPERVM. Separate data properly later on.
414 */
415typedef struct VMINTUSERPERVMCPU
416{
417 /** Head of the request queue. Atomic. */
418 volatile PVMREQ pReqs;
419
420 /** The handle to the EMT thread. */
421 RTTHREAD ThreadEMT;
422 /** The native of the EMT thread. */
423 RTNATIVETHREAD NativeThreadEMT;
424 /** Wait event semaphore. */
425 RTSEMEVENT EventSemWait;
426 /** Wait/Idle indicator. */
427 bool volatile fWait;
428 /** Force EMT to terminate. */
429 bool volatile fTerminateEMT;
430 /** If set the EMT does the final VM cleanup when it exits.
431 * If clear the VMR3Destroy() caller does so. */
432 bool fEMTDoesTheCleanup;
433
434 /** @name Generic Halt data
435 * @{
436 */
437 /** The current halt method.
438 * Can be selected by CFGM option 'VM/HaltMethod'. */
439 VMHALTMETHOD enmHaltMethod;
440 /** The index into g_aHaltMethods of the current halt method. */
441 uint32_t volatile iHaltMethod;
442 /** The average time (ns) between two halts in the last second. (updated once per second) */
443 uint32_t HaltInterval;
444 /** The average halt frequency for the last second. (updated once per second) */
445 uint32_t HaltFrequency;
446 /** The number of halts in the current period. */
447 uint32_t cHalts;
448 uint32_t padding; /**< alignment padding. */
449 /** When we started counting halts in cHalts (RTTimeNanoTS). */
450 uint64_t u64HaltsStartTS;
451 /** @} */
452
453 /** Union containing data and config for the different halt algorithms. */
454 union
455 {
456 /**
457 * Method 1 & 2 - Block whenever possible, and when lagging behind
458 * switch to spinning with regular blocking every 5-200ms (defaults)
459 * depending on the accumulated lag. The blocking interval is adjusted
460 * with the average oversleeping of the last 64 times.
461 *
462 * The difference between 1 and 2 is that we use native absolute
463 * time APIs for the blocking instead of the millisecond based IPRT
464 * interface.
465 */
466 struct
467 {
468 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
469 uint32_t cBlocks;
470 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
471 uint64_t cNSBlockedTooLongAvg;
472 /** Total time spend oversleeping when blocking. */
473 uint64_t cNSBlockedTooLong;
474 /** Total time spent blocking. */
475 uint64_t cNSBlocked;
476 /** The timestamp (RTTimeNanoTS) of the last block. */
477 uint64_t u64LastBlockTS;
478
479 /** When we started spinning relentlessly in order to catch up some of the oversleeping.
480 * This is 0 when we're not spinning. */
481 uint64_t u64StartSpinTS;
482
483 /** The max interval without blocking (when spinning). */
484 uint32_t u32MinBlockIntervalCfg;
485 /** The minimum interval between blocking (when spinning). */
486 uint32_t u32MaxBlockIntervalCfg;
487 /** The value to divide the current lag by to get the raw blocking interval (when spinning). */
488 uint32_t u32LagBlockIntervalDivisorCfg;
489 /** When to start spinning (lag / nano secs). */
490 uint32_t u32StartSpinningCfg;
491 /** When to stop spinning (lag / nano secs). */
492 uint32_t u32StopSpinningCfg;
493 } Method12;
494
495#if 0
496 /**
497 * Method 3 & 4 - Same as method 1 & 2 respectivly, except that we
498 * sprinkle it with yields.
499 */
500 struct
501 {
502 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
503 uint32_t cBlocks;
504 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
505 uint64_t cBlockedTooLongNSAvg;
506 /** Total time spend oversleeping when blocking. */
507 uint64_t cBlockedTooLongNS;
508 /** Total time spent blocking. */
509 uint64_t cBlockedNS;
510 /** The timestamp (RTTimeNanoTS) of the last block. */
511 uint64_t u64LastBlockTS;
512
513 /** How many times we've yielded while cBlockedNS and cBlockedTooLongNS has been accumulating. */
514 uint32_t cYields;
515 /** Avg. time spend oversleeping when yielding. */
516 uint32_t cYieldTooLongNSAvg;
517 /** Total time spend oversleeping when yielding. */
518 uint64_t cYieldTooLongNS;
519 /** Total time spent yielding. */
520 uint64_t cYieldedNS;
521 /** The timestamp (RTTimeNanoTS) of the last block. */
522 uint64_t u64LastYieldTS;
523
524 /** When we started spinning relentlessly in order to catch up some of the oversleeping. */
525 uint64_t u64StartSpinTS;
526 } Method34;
527#endif
528 } Halt;
529
530 /** Profiling the halted state; yielding vs blocking.
531 * @{ */
532 STAMPROFILE StatHaltYield;
533 STAMPROFILE StatHaltBlock;
534 STAMPROFILE StatHaltTimers;
535 STAMPROFILE StatHaltPoll;
536 /** @} */
537
538 /** Pointer to the DBGC instance data. */
539 void *pvDBGC;
540
541 /** vmR3EmulationThread longjmp buffer. Must be last in the structure. */
542 jmp_buf emtJumpEnv;
543} VMINTUSERPERVMCPU;
544
545/** Pointer to the VM internal data kept in the UVM. */
546typedef VMINTUSERPERVMCPU *PVMINTUSERPERVMCPU;
547
548DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD ThreadSelf, void *pvArg);
549int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod);
550DECLCALLBACK(int) vmR3Destroy(PVM pVM);
551DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *args);
552void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args);
553DECLCALLBACK(void) vmR3SetRuntimeErrorV(PVM pVM, bool fFatal, const char *pszErrorID, const char *pszFormat, va_list *args);
554void vmSetRuntimeErrorCopy(PVM pVM, bool fFatal, const char *pszErrorID, const char *pszFormat, va_list args);
555void vmR3DestroyFinalBitFromEMT(PUVM pUVM);
556void vmR3SetState(PVM pVM, VMSTATE enmStateNew);
557
558
559/** @} */
560
561#endif
562
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use