VirtualBox

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

最後變更 在這個檔案從38941是 38941,由 vboxsync 提交於 13 年 前

duh

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 17.1 KB
 
1/* $Id: VMInternal.h 38941 2011-10-05 13:09:59Z 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.alldomusa.eu.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/vmm/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 immediately
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 immediately
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 standard request queue. Atomic. */
177 volatile PVMREQ pNormalReqs;
178 /** Head of the priority request queue. Atomic. */
179 volatile PVMREQ pPriorityReqs;
180 /** The last index used during alloc/free. */
181 volatile uint32_t iReqFree;
182 /** Number of free request packets. */
183 volatile uint32_t cReqFree;
184 /** Array of pointers to lists of free request packets. Atomic. */
185 volatile PVMREQ apReqFree[16 - (HC_ARCH_BITS == 32 ? 5 : 4)];
186
187 /** The reference count of the UVM handle. */
188 volatile uint32_t cUvmRefs;
189
190#ifdef VBOX_WITH_STATISTICS
191 /** Number of VMR3ReqAlloc returning a new packet. */
192 STAMCOUNTER StatReqAllocNew;
193 /** Number of VMR3ReqAlloc causing races. */
194 STAMCOUNTER StatReqAllocRaces;
195 /** Number of VMR3ReqAlloc returning a recycled packet. */
196 STAMCOUNTER StatReqAllocRecycled;
197 /** Number of VMR3ReqFree calls. */
198 STAMCOUNTER StatReqFree;
199 /** Number of times the request was actually freed. */
200 STAMCOUNTER StatReqFreeOverflow;
201 /** Number of requests served. */
202 STAMCOUNTER StatReqProcessed;
203 /** Number of times there are more than one request and the others needed to be
204 * pushed back onto the list. */
205 STAMCOUNTER StatReqMoreThan1;
206 /** Number of times we've raced someone when pushing the other requests back
207 * onto the list. */
208 STAMCOUNTER StatReqPushBackRaces;
209#endif
210
211 /** Pointer to the support library session.
212 * Mainly for creation and destruction. */
213 PSUPDRVSESSION pSession;
214
215 /** Force EMT to terminate. */
216 bool volatile fTerminateEMT;
217
218 /** Critical section for pAtState and enmPrevVMState. */
219 RTCRITSECT AtStateCritSect;
220 /** List of registered state change callbacks. */
221 PVMATSTATE pAtState;
222 /** List of registered state change callbacks. */
223 PVMATSTATE *ppAtStateNext;
224 /** The previous VM state.
225 * This is mainly used for the 'Resetting' state, but may come in handy later
226 * and when debugging. */
227 VMSTATE enmPrevVMState;
228
229 /** Critical section for pAtError and pAtRuntimeError. */
230 RTCRITSECT AtErrorCritSect;
231
232 /** List of registered error callbacks. */
233 PVMATERROR pAtError;
234 /** List of registered error callbacks. */
235 PVMATERROR *ppAtErrorNext;
236 /** The error message count.
237 * This is incremented every time an error is raised. */
238 uint32_t volatile cErrors;
239
240 /** The runtime error message count.
241 * This is incremented every time a runtime error is raised. */
242 uint32_t volatile cRuntimeErrors;
243 /** List of registered error callbacks. */
244 PVMATRUNTIMEERROR pAtRuntimeError;
245 /** List of registered error callbacks. */
246 PVMATRUNTIMEERROR *ppAtRuntimeErrorNext;
247
248 /** @name Generic Halt data
249 * @{
250 */
251 /** The current halt method.
252 * Can be selected by CFGM option 'VM/HaltMethod'. */
253 VMHALTMETHOD enmHaltMethod;
254 /** The index into g_aHaltMethods of the current halt method. */
255 uint32_t volatile iHaltMethod;
256 /** @} */
257
258 /** @todo Do NOT add new members here or reuse the current, we need to store the config for
259 * each halt method separately because we're racing on SMP guest rigs. */
260 union
261 {
262 /**
263 * Method 1 & 2 - Block whenever possible, and when lagging behind
264 * switch to spinning with regular blocking every 5-200ms (defaults)
265 * depending on the accumulated lag. The blocking interval is adjusted
266 * with the average oversleeping of the last 64 times.
267 *
268 * The difference between 1 and 2 is that we use native absolute
269 * time APIs for the blocking instead of the millisecond based IPRT
270 * interface.
271 */
272 struct
273 {
274 /** The max interval without blocking (when spinning). */
275 uint32_t u32MinBlockIntervalCfg;
276 /** The minimum interval between blocking (when spinning). */
277 uint32_t u32MaxBlockIntervalCfg;
278 /** The value to divide the current lag by to get the raw blocking interval (when spinning). */
279 uint32_t u32LagBlockIntervalDivisorCfg;
280 /** When to start spinning (lag / nano secs). */
281 uint32_t u32StartSpinningCfg;
282 /** When to stop spinning (lag / nano secs). */
283 uint32_t u32StopSpinningCfg;
284 } Method12;
285
286 /**
287 * The GVMM manages halted and waiting EMTs.
288 */
289 struct
290 {
291 /** The threshold between spinning and blocking. */
292 uint32_t cNsSpinBlockThresholdCfg;
293 } Global1;
294 } Halt;
295
296 /** Pointer to the DBGC instance data. */
297 void *pvDBGC;
298
299 /** TLS index for the VMINTUSERPERVMCPU pointer. */
300 RTTLS idxTLS;
301
302 /** The VM name. (Set after the config constructure has been called.) */
303 char *pszName;
304 /** The VM UUID. (Set after the config constructure has been called.) */
305 RTUUID Uuid;
306} VMINTUSERPERVM;
307#ifdef VBOX_WITH_STATISTICS
308AssertCompileMemberAlignment(VMINTUSERPERVM, StatReqAllocNew, 8);
309#endif
310
311/** Pointer to the VM internal data kept in the UVM. */
312typedef VMINTUSERPERVM *PVMINTUSERPERVM;
313
314
315/**
316 * VMCPU internal data kept in the UVM.
317 *
318 * Almost a copy of VMINTUSERPERVM. Separate data properly later on.
319 */
320typedef struct VMINTUSERPERVMCPU
321{
322 /** Head of the normal request queue. Atomic. */
323 volatile PVMREQ pNormalReqs;
324 /** Head of the priority request queue. Atomic. */
325 volatile PVMREQ pPriorityReqs;
326
327 /** The handle to the EMT thread. */
328 RTTHREAD ThreadEMT;
329 /** The native of the EMT thread. */
330 RTNATIVETHREAD NativeThreadEMT;
331 /** Wait event semaphore. */
332 RTSEMEVENT EventSemWait;
333 /** Wait/Idle indicator. */
334 bool volatile fWait;
335 /** Align the next bit. */
336 bool afAlignment[HC_ARCH_BITS == 32 ? 3 : 7];
337
338 /** @name Generic Halt data
339 * @{
340 */
341 /** The average time (ns) between two halts in the last second. (updated once per second) */
342 uint32_t HaltInterval;
343 /** The average halt frequency for the last second. (updated once per second) */
344 uint32_t HaltFrequency;
345 /** The number of halts in the current period. */
346 uint32_t cHalts;
347 uint32_t padding; /**< alignment padding. */
348 /** When we started counting halts in cHalts (RTTimeNanoTS). */
349 uint64_t u64HaltsStartTS;
350 /** @} */
351
352 /** Union containing data and config for the different halt algorithms. */
353 union
354 {
355 /**
356 * Method 1 & 2 - Block whenever possible, and when lagging behind
357 * switch to spinning with regular blocking every 5-200ms (defaults)
358 * depending on the accumulated lag. The blocking interval is adjusted
359 * with the average oversleeping of the last 64 times.
360 *
361 * The difference between 1 and 2 is that we use native absolute
362 * time APIs for the blocking instead of the millisecond based IPRT
363 * interface.
364 */
365 struct
366 {
367 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
368 uint32_t cBlocks;
369 /** Align the next member. */
370 uint32_t u32Alignment;
371 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
372 uint64_t cNSBlockedTooLongAvg;
373 /** Total time spend oversleeping when blocking. */
374 uint64_t cNSBlockedTooLong;
375 /** Total time spent blocking. */
376 uint64_t cNSBlocked;
377 /** The timestamp (RTTimeNanoTS) of the last block. */
378 uint64_t u64LastBlockTS;
379
380 /** When we started spinning relentlessly in order to catch up some of the oversleeping.
381 * This is 0 when we're not spinning. */
382 uint64_t u64StartSpinTS;
383 } Method12;
384
385#if 0
386 /**
387 * Method 3 & 4 - Same as method 1 & 2 respectivly, except that we
388 * sprinkle it with yields.
389 */
390 struct
391 {
392 /** How many times we've blocked while cBlockedNS and cBlockedTooLongNS has been accumulating. */
393 uint32_t cBlocks;
394 /** Avg. time spend oversleeping when blocking. (Re-calculated every so often.) */
395 uint64_t cBlockedTooLongNSAvg;
396 /** Total time spend oversleeping when blocking. */
397 uint64_t cBlockedTooLongNS;
398 /** Total time spent blocking. */
399 uint64_t cBlockedNS;
400 /** The timestamp (RTTimeNanoTS) of the last block. */
401 uint64_t u64LastBlockTS;
402
403 /** How many times we've yielded while cBlockedNS and cBlockedTooLongNS has been accumulating. */
404 uint32_t cYields;
405 /** Avg. time spend oversleeping when yielding. */
406 uint32_t cYieldTooLongNSAvg;
407 /** Total time spend oversleeping when yielding. */
408 uint64_t cYieldTooLongNS;
409 /** Total time spent yielding. */
410 uint64_t cYieldedNS;
411 /** The timestamp (RTTimeNanoTS) of the last block. */
412 uint64_t u64LastYieldTS;
413
414 /** When we started spinning relentlessly in order to catch up some of the oversleeping. */
415 uint64_t u64StartSpinTS;
416 } Method34;
417#endif
418 } Halt;
419
420 /** Profiling the halted state; yielding vs blocking.
421 * @{ */
422 STAMPROFILE StatHaltYield;
423 STAMPROFILE StatHaltBlock;
424 STAMPROFILE StatHaltBlockOverslept;
425 STAMPROFILE StatHaltBlockInsomnia;
426 STAMPROFILE StatHaltBlockOnTime;
427 STAMPROFILE StatHaltTimers;
428 STAMPROFILE StatHaltPoll;
429 /** @} */
430} VMINTUSERPERVMCPU;
431#ifdef IN_RING3
432AssertCompileMemberAlignment(VMINTUSERPERVMCPU, u64HaltsStartTS, 8);
433AssertCompileMemberAlignment(VMINTUSERPERVMCPU, Halt.Method12.cNSBlockedTooLongAvg, 8);
434AssertCompileMemberAlignment(VMINTUSERPERVMCPU, StatHaltYield, 8);
435#endif
436
437/** Pointer to the VM internal data kept in the UVM. */
438typedef VMINTUSERPERVMCPU *PVMINTUSERPERVMCPU;
439
440RT_C_DECLS_BEGIN
441
442DECLCALLBACK(int) vmR3EmulationThread(RTTHREAD ThreadSelf, void *pvArg);
443int vmR3SetHaltMethodU(PUVM pUVM, VMHALTMETHOD enmHaltMethod);
444DECLCALLBACK(int) vmR3Destroy(PVM pVM);
445DECLCALLBACK(void) vmR3SetErrorUV(PUVM pUVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list *args);
446void vmSetErrorCopy(PVM pVM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list args);
447DECLCALLBACK(int) vmR3SetRuntimeError(PVM pVM, uint32_t fFlags, const char *pszErrorId, char *pszMessage);
448DECLCALLBACK(int) vmR3SetRuntimeErrorV(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list *pVa);
449void vmSetRuntimeErrorCopy(PVM pVM, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va);
450void vmR3SetGuruMeditation(PVM pVM);
451void vmR3SetTerminated(PVM pVM);
452
453RT_C_DECLS_END
454
455
456/** @} */
457
458#endif
459
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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