VirtualBox

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

Last change on this file since 30373 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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