VirtualBox

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

Last change on this file since 70905 was 69474, checked in by vboxsync, 7 years ago

*: scm updates - header files should have 'svn:keywords=Id Revision' too (doesn't mean they have to use them).

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