VirtualBox

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

Last change on this file since 24646 was 24508, checked in by vboxsync, 15 years ago

SSM,VM: VMSetError handling.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.2 KB
Line 
1/* $Id: VMInternal.h 24508 2009-11-09 14:44:12Z 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 <iprt/assert.h>
28#include <iprt/critsect.h>
29#include <setjmp.h>
30
31
32
33/** @defgroup grp_vm_int Internals
34 * @ingroup grp_vm
35 * @internal
36 * @{
37 */
38
39
40/**
41 * VM state change callback.
42 */
43typedef struct VMATSTATE
44{
45 /** Pointer to the next one. */
46 struct VMATSTATE *pNext;
47 /** Pointer to the callback. */
48 PFNVMATSTATE pfnAtState;
49 /** The user argument. */
50 void *pvUser;
51} VMATSTATE;
52/** Pointer to a VM state change callback. */
53typedef VMATSTATE *PVMATSTATE;
54
55
56/**
57 * VM error callback.
58 */
59typedef struct VMATERROR
60{
61 /** Pointer to the next one. */
62 struct VMATERROR *pNext;
63 /** Pointer to the callback. */
64 PFNVMATERROR pfnAtError;
65 /** The user argument. */
66 void *pvUser;
67} VMATERROR;
68/** Pointer to a VM error callback. */
69typedef VMATERROR *PVMATERROR;
70
71
72/**
73 * Chunk of memory allocated off the hypervisor heap in which
74 * we copy the error details.
75 */
76typedef struct VMERROR
77{
78 /** The size of the chunk. */
79 uint32_t cbAllocated;
80 /** The current offset into the chunk.
81 * We start by putting the filename and function immediatly
82 * after the end of the buffer. */
83 uint32_t off;
84 /** Offset from the start of this structure to the file name. */
85 uint32_t offFile;
86 /** The line number. */
87 uint32_t iLine;
88 /** Offset from the start of this structure to the function name. */
89 uint32_t offFunction;
90 /** Offset from the start of this structure to the formatted message text. */
91 uint32_t offMessage;
92 /** The VBox status code. */
93 int32_t rc;
94} VMERROR, *PVMERROR;
95
96
97/**
98 * VM runtime error callback.
99 */
100typedef struct VMATRUNTIMEERROR
101{
102 /** Pointer to the next one. */
103 struct VMATRUNTIMEERROR *pNext;
104 /** Pointer to the callback. */
105 PFNVMATRUNTIMEERROR pfnAtRuntimeError;
106 /** The user argument. */
107 void *pvUser;
108} VMATRUNTIMEERROR;
109/** Pointer to a VM error callback. */
110typedef VMATRUNTIMEERROR *PVMATRUNTIMEERROR;
111
112
113/**
114 * Chunk of memory allocated off the hypervisor heap in which
115 * we copy the runtime error details.
116 */
117typedef struct VMRUNTIMEERROR
118{
119 /** The size of the chunk. */
120 uint32_t cbAllocated;
121 /** The current offset into the chunk.
122 * We start by putting the error ID immediatly
123 * after the end of the buffer. */
124 uint32_t off;
125 /** Offset from the start of this structure to the error ID. */
126 uint32_t offErrorId;
127 /** Offset from the start of this structure to the formatted message text. */
128 uint32_t offMessage;
129 /** Error flags. */
130 uint32_t fFlags;
131} VMRUNTIMEERROR, *PVMRUNTIMEERROR;
132
133/** The halt method. */
134typedef enum
135{
136 /** The usual invalid value. */
137 VMHALTMETHOD_INVALID = 0,
138 /** Use the method used during bootstrapping. */
139 VMHALTMETHOD_BOOTSTRAP,
140 /** Use the default method. */
141 VMHALTMETHOD_DEFAULT,
142 /** The old spin/yield/block method. */
143 VMHALTMETHOD_OLD,
144 /** The first go at a block/spin method. */
145 VMHALTMETHOD_1,
146 /** The first go at a more global approach. */
147 VMHALTMETHOD_GLOBAL_1,
148 /** The end of valid methods. (not inclusive of course) */
149 VMHALTMETHOD_END,
150 /** The usual 32-bit max value. */
151 VMHALTMETHOD_32BIT_HACK = 0x7fffffff
152} VMHALTMETHOD;
153
154
155/**
156 * VM Internal Data (part of the VM structure).
157 *
158 * @todo Move this and all related things to VMM. The VM component was, to some
159 * extent at least, a bad ad hoc design which should all have been put in
160 * VMM. @see pg_vm.
161 */
162typedef struct VMINT
163{
164 /** VM Error Message. */
165 R3PTRTYPE(PVMERROR) pErrorR3;
166 /** VM Runtime Error Message. */
167 R3PTRTYPE(PVMRUNTIMEERROR) pRuntimeErrorR3;
168 /** The VM was/is-being teleported and has not yet been fully resumed. */
169 bool fTeleportedAndNotFullyResumedYet;
170} VMINT;
171/** Pointer to the VM Internal Data (part of the VM structure). */
172typedef VMINT *PVMINT;
173
174
175/**
176 * VM internal data kept in the UVM.
177 */
178typedef struct VMINTUSERPERVM
179{
180 /** Head of the request queue. Atomic. */
181 volatile PVMREQ pReqs;
182 /** The last index used during alloc/free. */
183 volatile uint32_t iReqFree;
184 /** Number of free request packets. */
185 volatile uint32_t cReqFree;
186 /** Array of pointers to lists of free request packets. Atomic. */
187 volatile PVMREQ apReqFree[9];
188
189#ifdef VBOX_WITH_STATISTICS
190 /** Number of VMR3ReqAlloc returning a new packet. */
191 STAMCOUNTER StatReqAllocNew;
192 /** Number of VMR3ReqAlloc causing races. */
193 STAMCOUNTER StatReqAllocRaces;
194 /** Number of VMR3ReqAlloc returning a recycled packet. */
195 STAMCOUNTER StatReqAllocRecycled;
196 /** Number of VMR3ReqFree calls. */
197 STAMCOUNTER StatReqFree;
198 /** Number of times the request was actually freed. */
199 STAMCOUNTER StatReqFreeOverflow;
200#endif
201
202 /** Pointer to the support library session.
203 * Mainly for creation and destruction. */
204 PSUPDRVSESSION pSession;
205
206 /** Force EMT to terminate. */
207 bool volatile fTerminateEMT;
208 /** If set the EMT(0) does the final VM cleanup when it exits.
209 * If clear the VMR3Destroy() caller does so. */
210 bool fEMTDoesTheCleanup;
211
212 /** Critical section for pAtState and enmPrevVMState. */
213 RTCRITSECT AtStateCritSect;
214 /** List of registered state change callbacks. */
215 PVMATSTATE pAtState;
216 /** List of registered state change callbacks. */
217 PVMATSTATE *ppAtStateNext;
218 /** The previous VM state.
219 * This is mainly used for the 'Resetting' state, but may come in handy later
220 * and when debugging. */
221 VMSTATE enmPrevVMState;
222
223 /** Critical section for pAtError and pAtRuntimeError. */
224 RTCRITSECT AtErrorCritSect;
225
226 /** List of registered error callbacks. */
227 PVMATERROR pAtError;
228 /** List of registered error callbacks. */
229 PVMATERROR *ppAtErrorNext;
230 /** The error message count.
231 * This is incremented every time an error is raised. */
232 uint32_t volatile cErrors;
233
234 /** The runtime error message count.
235 * This is incremented every time a runtime error is raised. */
236 uint32_t volatile cRuntimeErrors;
237 /** List of registered error callbacks. */
238 PVMATRUNTIMEERROR pAtRuntimeError;
239 /** List of registered error callbacks. */
240 PVMATRUNTIMEERROR *ppAtRuntimeErrorNext;
241
242 /** @name Generic Halt data
243 * @{
244 */
245 /** The current halt method.
246 * Can be selected by CFGM option 'VM/HaltMethod'. */
247 VMHALTMETHOD enmHaltMethod;
248 /** The index into g_aHaltMethods of the current halt method. */
249 uint32_t volatile iHaltMethod;
250 /** @} */
251
252 /** @todo Do NOT add new members here or resue the current, we need to store the config for
253 * each halt method seperately because we're racing on SMP guest rigs. */
254 union
255 {
256 /**
257 * Method 1 & 2 - Block whenever possible, and when lagging behind
258 * switch to spinning with regular blocking every 5-200ms (defaults)
259 * depending on the accumulated lag. The blocking interval is adjusted
260 * with the average oversleeping of the last 64 times.
261 *
262 * The difference between 1 and 2 is that we use native absolute
263 * time APIs for the blocking instead of the millisecond based IPRT
264 * interface.
265 */
266 struct
267 {
268 /** The max interval without blocking (when spinning). */
269 uint32_t u32MinBlockIntervalCfg;
270 /** The minimum interval between blocking (when spinning). */
271 uint32_t u32MaxBlockIntervalCfg;
272 /** The value to divide the current lag by to get the raw blocking interval (when spinning). */
273 uint32_t u32LagBlockIntervalDivisorCfg;
274 /** When to start spinning (lag / nano secs). */
275 uint32_t u32StartSpinningCfg;
276 /** When to stop spinning (lag / nano secs). */
277 uint32_t u32StopSpinningCfg;
278 } Method12;
279 } Halt;
280
281 /** Pointer to the DBGC instance data. */
282 void *pvDBGC;
283
284 /** TLS index for the VMINTUSERPERVMCPU pointer. */
285 RTTLS idxTLS;
286} VMINTUSERPERVM;
287
288/** Pointer to the VM internal data kept in the UVM. */
289typedef VMINTUSERPERVM *PVMINTUSERPERVM;
290
291
292/**
293 * VMCPU internal data kept in the UVM.
294 *
295 * Almost a copy of VMINTUSERPERVM. Separate data properly later on.
296 */
297typedef struct VMINTUSERPERVMCPU
298{
299 /** Head of the request queue. Atomic. */
300 volatile PVMREQ pReqs;
301
302 /** The handle to the EMT thread. */
303 RTTHREAD ThreadEMT;
304 /** The native of the EMT thread. */
305 RTNATIVETHREAD NativeThreadEMT;
306 /** Wait event semaphore. */
307 RTSEMEVENT EventSemWait;
308 /** Wait/Idle indicator. */
309 bool volatile fWait;
310 /** Force EMT to terminate. */
311 bool volatile fTerminateEMT;
312 /** If set the EMT does the final VM cleanup when it exits.
313 * If clear the VMR3Destroy() caller does so. */
314 bool fEMTDoesTheCleanup;
315 /** Align the next bit. */
316 bool afAlignment[5];
317
318 /** @name Generic Halt data
319 * @{
320 */
321 /** The average time (ns) between two halts in the last second. (updated once per second) */
322 uint32_t HaltInterval;
323 /** The average halt frequency for the last second. (updated once per second) */
324 uint32_t HaltFrequency;
325 /** The number of halts in the current period. */
326 uint32_t cHalts;
327 uint32_t padding; /**< alignment padding. */
328 /** When we started counting halts in cHalts (RTTimeNanoTS). */
329 uint64_t u64HaltsStartTS;
330 /** @} */
331
332 /** Union containing data and config for the different halt algorithms. */
333 union
334 {
335 /**
336 * Method 1 & 2 - Block whenever possible, and when lagging behind
337 * switch to spinning with regular blocking every 5-200ms (defaults)
338 * depending on the accumulated lag. The blocking interval is adjusted
339 * with the average oversleeping of the last 64 times.
340 *
341 * The difference between 1 and 2 is that we use native absolute
342 * time APIs for the blocking instead of the millisecond based IPRT
343 * interface.
344 */
345 struct
346 {
347 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
348 uint32_t cBlocks;
349 /** Align the next member. */
350 uint32_t u32Alignment;
351 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
352 uint64_t cNSBlockedTooLongAvg;
353 /** Total time spend oversleeping when blocking. */
354 uint64_t cNSBlockedTooLong;
355 /** Total time spent blocking. */
356 uint64_t cNSBlocked;
357 /** The timestamp (RTTimeNanoTS) of the last block. */
358 uint64_t u64LastBlockTS;
359
360 /** When we started spinning relentlessly in order to catch up some of the oversleeping.
361 * This is 0 when we're not spinning. */
362 uint64_t u64StartSpinTS;
363 } Method12;
364
365#if 0
366 /**
367 * Method 3 & 4 - Same as method 1 & 2 respectivly, except that we
368 * sprinkle it with yields.
369 */
370 struct
371 {
372 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
373 uint32_t cBlocks;
374 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
375 uint64_t cBlockedTooLongNSAvg;
376 /** Total time spend oversleeping when blocking. */
377 uint64_t cBlockedTooLongNS;
378 /** Total time spent blocking. */
379 uint64_t cBlockedNS;
380 /** The timestamp (RTTimeNanoTS) of the last block. */
381 uint64_t u64LastBlockTS;
382
383 /** How many times we've yielded while cBlockedNS and cBlockedTooLongNS has been accumulating. */
384 uint32_t cYields;
385 /** Avg. time spend oversleeping when yielding. */
386 uint32_t cYieldTooLongNSAvg;
387 /** Total time spend oversleeping when yielding. */
388 uint64_t cYieldTooLongNS;
389 /** Total time spent yielding. */
390 uint64_t cYieldedNS;
391 /** The timestamp (RTTimeNanoTS) of the last block. */
392 uint64_t u64LastYieldTS;
393
394 /** When we started spinning relentlessly in order to catch up some of the oversleeping. */
395 uint64_t u64StartSpinTS;
396 } Method34;
397#endif
398 } Halt;
399
400 /** Profiling the halted state; yielding vs blocking.
401 * @{ */
402 STAMPROFILE StatHaltYield;
403 STAMPROFILE StatHaltBlock;
404 STAMPROFILE StatHaltTimers;
405 STAMPROFILE StatHaltPoll;
406 /** @} */
407} VMINTUSERPERVMCPU;
408#ifdef IN_RING3
409AssertCompileMemberAlignment(VMINTUSERPERVMCPU, u64HaltsStartTS, 8);
410AssertCompileMemberAlignment(VMINTUSERPERVMCPU, Halt.Method12.cNSBlockedTooLongAvg, 8);
411AssertCompileMemberAlignment(VMINTUSERPERVMCPU, StatHaltYield, 8);
412#endif
413
414/** Pointer to the VM internal data kept in the UVM. */
415typedef VMINTUSERPERVMCPU *PVMINTUSERPERVMCPU;
416
417RT_C_DECLS_BEGIN
418
419DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD ThreadSelf, void *pvArg);
420int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod);
421DECLCALLBACK(int) vmR3Destroy(PVM pVM);
422DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *args);
423void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args);
424DECLCALLBACK(int) vmR3SetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, char *pszMessage);
425DECLCALLBACK(int) vmR3SetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa);
426void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va);
427void vmR3DestroyFinalBitFromEMT(PUVM pUVM, VMCPUID idCpu);
428void vmR3SetGuruMeditation(PVM pVM);
429
430RT_C_DECLS_END
431
432
433/** @} */
434
435#endif
436
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