VirtualBox

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

Last change on this file since 68853 was 68853, checked in by vboxsync, 8 years ago

VMMR3EmtRendezvous: Pretend we're a single CPU config when called during vmR3Destroy after the EMTs have started terminating.

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

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