VirtualBox

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

Last change on this file since 24125 was 23915, checked in by vboxsync, 15 years ago

VM,PDMDevHlp,PDMDrvHlp: VM[R3]TeleportedAndNotFullyResumedYet (horrible name).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.9 KB
Line 
1/* $Id: VMInternal.h 23915 2009-10-20 17:06:58Z 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
231 /** List of registered error callbacks. */
232 PVMATRUNTIMEERROR pAtRuntimeError;
233 /** List of registered error callbacks. */
234 PVMATRUNTIMEERROR *ppAtRuntimeErrorNext;
235
236 /** @name Generic Halt data
237 * @{
238 */
239 /** The current halt method.
240 * Can be selected by CFGM option 'VM/HaltMethod'. */
241 VMHALTMETHOD enmHaltMethod;
242 /** The index into g_aHaltMethods of the current halt method. */
243 uint32_t volatile iHaltMethod;
244 /** @} */
245
246 /** @todo Do NOT add new members here or resue the current, we need to store the config for
247 * each halt method seperately because we're racing on SMP guest rigs. */
248 union
249 {
250 /**
251 * Method 1 & 2 - Block whenever possible, and when lagging behind
252 * switch to spinning with regular blocking every 5-200ms (defaults)
253 * depending on the accumulated lag. The blocking interval is adjusted
254 * with the average oversleeping of the last 64 times.
255 *
256 * The difference between 1 and 2 is that we use native absolute
257 * time APIs for the blocking instead of the millisecond based IPRT
258 * interface.
259 */
260 struct
261 {
262 /** The max interval without blocking (when spinning). */
263 uint32_t u32MinBlockIntervalCfg;
264 /** The minimum interval between blocking (when spinning). */
265 uint32_t u32MaxBlockIntervalCfg;
266 /** The value to divide the current lag by to get the raw blocking interval (when spinning). */
267 uint32_t u32LagBlockIntervalDivisorCfg;
268 /** When to start spinning (lag / nano secs). */
269 uint32_t u32StartSpinningCfg;
270 /** When to stop spinning (lag / nano secs). */
271 uint32_t u32StopSpinningCfg;
272 } Method12;
273 } Halt;
274
275 /** Pointer to the DBGC instance data. */
276 void *pvDBGC;
277
278 /** TLS index for the VMINTUSERPERVMCPU pointer. */
279 RTTLS idxTLS;
280} VMINTUSERPERVM;
281
282/** Pointer to the VM internal data kept in the UVM. */
283typedef VMINTUSERPERVM *PVMINTUSERPERVM;
284
285
286/**
287 * VMCPU internal data kept in the UVM.
288 *
289 * Almost a copy of VMINTUSERPERVM. Separate data properly later on.
290 */
291typedef struct VMINTUSERPERVMCPU
292{
293 /** Head of the request queue. Atomic. */
294 volatile PVMREQ pReqs;
295
296 /** The handle to the EMT thread. */
297 RTTHREAD ThreadEMT;
298 /** The native of the EMT thread. */
299 RTNATIVETHREAD NativeThreadEMT;
300 /** Wait event semaphore. */
301 RTSEMEVENT EventSemWait;
302 /** Wait/Idle indicator. */
303 bool volatile fWait;
304 /** Force EMT to terminate. */
305 bool volatile fTerminateEMT;
306 /** If set the EMT does the final VM cleanup when it exits.
307 * If clear the VMR3Destroy() caller does so. */
308 bool fEMTDoesTheCleanup;
309 /** Align the next bit. */
310 bool afAlignment[5];
311
312 /** @name Generic Halt data
313 * @{
314 */
315 /** The average time (ns) between two halts in the last second. (updated once per second) */
316 uint32_t HaltInterval;
317 /** The average halt frequency for the last second. (updated once per second) */
318 uint32_t HaltFrequency;
319 /** The number of halts in the current period. */
320 uint32_t cHalts;
321 uint32_t padding; /**< alignment padding. */
322 /** When we started counting halts in cHalts (RTTimeNanoTS). */
323 uint64_t u64HaltsStartTS;
324 /** @} */
325
326 /** Union containing data and config for the different halt algorithms. */
327 union
328 {
329 /**
330 * Method 1 & 2 - Block whenever possible, and when lagging behind
331 * switch to spinning with regular blocking every 5-200ms (defaults)
332 * depending on the accumulated lag. The blocking interval is adjusted
333 * with the average oversleeping of the last 64 times.
334 *
335 * The difference between 1 and 2 is that we use native absolute
336 * time APIs for the blocking instead of the millisecond based IPRT
337 * interface.
338 */
339 struct
340 {
341 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
342 uint32_t cBlocks;
343 /** Align the next member. */
344 uint32_t u32Alignment;
345 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
346 uint64_t cNSBlockedTooLongAvg;
347 /** Total time spend oversleeping when blocking. */
348 uint64_t cNSBlockedTooLong;
349 /** Total time spent blocking. */
350 uint64_t cNSBlocked;
351 /** The timestamp (RTTimeNanoTS) of the last block. */
352 uint64_t u64LastBlockTS;
353
354 /** When we started spinning relentlessly in order to catch up some of the oversleeping.
355 * This is 0 when we're not spinning. */
356 uint64_t u64StartSpinTS;
357 } Method12;
358
359#if 0
360 /**
361 * Method 3 & 4 - Same as method 1 & 2 respectivly, except that we
362 * sprinkle it with yields.
363 */
364 struct
365 {
366 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
367 uint32_t cBlocks;
368 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
369 uint64_t cBlockedTooLongNSAvg;
370 /** Total time spend oversleeping when blocking. */
371 uint64_t cBlockedTooLongNS;
372 /** Total time spent blocking. */
373 uint64_t cBlockedNS;
374 /** The timestamp (RTTimeNanoTS) of the last block. */
375 uint64_t u64LastBlockTS;
376
377 /** How many times we've yielded while cBlockedNS and cBlockedTooLongNS has been accumulating. */
378 uint32_t cYields;
379 /** Avg. time spend oversleeping when yielding. */
380 uint32_t cYieldTooLongNSAvg;
381 /** Total time spend oversleeping when yielding. */
382 uint64_t cYieldTooLongNS;
383 /** Total time spent yielding. */
384 uint64_t cYieldedNS;
385 /** The timestamp (RTTimeNanoTS) of the last block. */
386 uint64_t u64LastYieldTS;
387
388 /** When we started spinning relentlessly in order to catch up some of the oversleeping. */
389 uint64_t u64StartSpinTS;
390 } Method34;
391#endif
392 } Halt;
393
394 /** Profiling the halted state; yielding vs blocking.
395 * @{ */
396 STAMPROFILE StatHaltYield;
397 STAMPROFILE StatHaltBlock;
398 STAMPROFILE StatHaltTimers;
399 STAMPROFILE StatHaltPoll;
400 /** @} */
401} VMINTUSERPERVMCPU;
402#ifdef IN_RING3
403AssertCompileMemberAlignment(VMINTUSERPERVMCPU, u64HaltsStartTS, 8);
404AssertCompileMemberAlignment(VMINTUSERPERVMCPU, Halt.Method12.cNSBlockedTooLongAvg, 8);
405AssertCompileMemberAlignment(VMINTUSERPERVMCPU, StatHaltYield, 8);
406#endif
407
408/** Pointer to the VM internal data kept in the UVM. */
409typedef VMINTUSERPERVMCPU *PVMINTUSERPERVMCPU;
410
411RT_C_DECLS_BEGIN
412
413DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD ThreadSelf, void *pvArg);
414int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod);
415DECLCALLBACK(int) vmR3Destroy(PVM pVM);
416DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *args);
417void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args);
418DECLCALLBACK(int) vmR3SetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, char *pszMessage);
419DECLCALLBACK(int) vmR3SetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa);
420void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va);
421void vmR3DestroyFinalBitFromEMT(PUVM pUVM, VMCPUID idCpu);
422void vmR3SetGuruMeditation(PVM pVM);
423
424RT_C_DECLS_END
425
426
427/** @} */
428
429#endif
430
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