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