VirtualBox

source: vbox/trunk/src/VBox/VMM/SSM.cpp@ 16281

Last change on this file since 16281 was 16165, checked in by vboxsync, 16 years ago

SSM: nc.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 116.2 KB
Line 
1/* $Id: SSM.cpp 16165 2009-01-22 13:22:06Z vboxsync $ */
2/** @file
3 * SSM - Saved State Manager.
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
23/** @page pg_ssm SSM - The Saved State Manager
24 *
25 * The Saved State Manager (SSM) implements facilities for saving and loading a
26 * VM state in a structural manner using callbacks for each collection of data
27 * which needs saving.
28 *
29 * At init time each of the VMM components, Devices, Drivers and one or two
30 * other things will register data entities which they need to save and restore.
31 * Each entity have a unique name (ascii), instance number, and a set of
32 * callbacks associated with it. The name will be used to identify the entity
33 * during restore. The callbacks are for the two operations, save and restore.
34 * There are three callbacks for each of the two - a prepare, a execute and a
35 * complete - giving each component ample opportunity to perform actions both
36 * before and afterwards.
37 *
38 * The SSM provides a number of APIs for encoding and decoding the data.
39 *
40 * @see grp_ssm
41 *
42 *
43 * @section sec_ssm_future Future Changes
44 *
45 * There are plans to extend SSM to make it easier to be both backwards and
46 * (somewhat) forwards compatible. One of the new features will be being able
47 * to classify units and data items as unimportant, one example where this would
48 * be nice can be seen in with the SSM data unit. Another potentail feature is
49 * naming data items, perhaps by extending the SSMR3PutStruct API.
50 *
51 */
52
53
54/*******************************************************************************
55* Header Files *
56*******************************************************************************/
57#define LOG_GROUP LOG_GROUP_SSM
58#include <VBox/ssm.h>
59#include <VBox/dbgf.h>
60#include <VBox/mm.h>
61#include "SSMInternal.h"
62#include <VBox/vm.h>
63#include <VBox/err.h>
64#include <VBox/log.h>
65#include <VBox/version.h>
66
67#include <iprt/assert.h>
68#include <iprt/file.h>
69#include <iprt/alloc.h>
70#include <iprt/uuid.h>
71#include <iprt/zip.h>
72#include <iprt/crc32.h>
73#include <iprt/thread.h>
74#include <iprt/string.h>
75
76
77/*******************************************************************************
78* Defined Constants And Macros *
79*******************************************************************************/
80/** Saved state file magic base string. */
81#define SSMFILEHDR_MAGIC_BASE "\177VirtualBox SavedState "
82/** Saved state file v1.0 magic. */
83#define SSMFILEHDR_MAGIC_V1_0 "\177VirtualBox SavedState V1.0\n"
84/** Saved state file v1.1 magic. */
85#define SSMFILEHDR_MAGIC_V1_1 "\177VirtualBox SavedState V1.1\n"
86/** Saved state file v1.2 magic. */
87#define SSMFILEHDR_MAGIC_V1_2 "\177VirtualBox SavedState V1.2\n\0\0\0"
88
89/** Data unit magic. */
90#define SSMFILEUNITHDR_MAGIC "\nUnit\n"
91/** Data end marker magic. */
92#define SSMFILEUNITHDR_END "\nTheEnd"
93
94/** Start structure magic. (Isacc Asimov) */
95#define SSMR3STRUCT_BEGIN 0x19200102
96/** End structure magic. (Isacc Asimov) */
97#define SSMR3STRUCT_END 0x19920406
98
99
100/*******************************************************************************
101* Structures and Typedefs *
102*******************************************************************************/
103/** SSM state. */
104typedef enum SSMSTATE
105{
106 SSMSTATE_INVALID = 0,
107 SSMSTATE_SAVE_PREP,
108 SSMSTATE_SAVE_EXEC,
109 SSMSTATE_SAVE_DONE,
110 SSMSTATE_LOAD_PREP,
111 SSMSTATE_LOAD_EXEC,
112 SSMSTATE_LOAD_DONE,
113 SSMSTATE_OPEN_READ
114} SSMSTATE;
115
116
117/**
118 * Handle structure.
119 */
120typedef struct SSMHANDLE
121{
122 /** The file handle. */
123 RTFILE File;
124 /** The VM handle. */
125 PVM pVM;
126 /** The size of the file header.
127 * Because the file header was incorrectly aligned there we've ended up with
128 * differences between the 64-bit and 32-bit file header. */
129 size_t cbFileHdr;
130 /** The current operation. */
131 SSMSTATE enmOp;
132 /** What to do after save completes. (move the enum) */
133 SSMAFTER enmAfter;
134 /** The current rc of the save operation. */
135 int rc;
136 /** The compressor of the current data unit. */
137 PRTZIPCOMP pZipComp;
138 /** The decompressor of the current data unit. */
139 PRTZIPDECOMP pZipDecomp;
140 /** Number of bytes left in the current data unit. */
141 uint64_t cbUnitLeft;
142
143 /** Pointer to the progress callback function. */
144 PFNVMPROGRESS pfnProgress;
145 /** User specified arguemnt to the callback function. */
146 void *pvUser;
147 /** Next completion percentage. (corresponds to offEstProgress) */
148 unsigned uPercent;
149 /** The position of the next progress callback in the estimated file. */
150 uint64_t offEstProgress;
151 /** The estimated total byte count.
152 * (Only valid after the prep.) */
153 uint64_t cbEstTotal;
154 /** Current position in the estimated file. */
155 uint64_t offEst;
156 /** End of current unit in the estimated file. */
157 uint64_t offEstUnitEnd;
158 /** the amount of % we reserve for the 'prepare' phase */
159 unsigned uPercentPrepare;
160 /** the amount of % we reserve for the 'done' stage */
161 unsigned uPercentDone;
162
163 /** RTGCPHYS size in bytes. (Only applicable when loading/reading.) */
164 unsigned cbGCPhys;
165 /** RTGCPTR size in bytes. (Only applicable when loading/reading.) */
166 unsigned cbGCPtr;
167 /** Whether cbGCPtr is fixed or settable. */
168 bool fFixedGCPtrSize;
169} SSMHANDLE;
170
171
172/**
173 * Header of the saved state file.
174 *
175 * @remarks This is a superset of SSMFILEHDRV11.
176 */
177typedef struct SSMFILEHDR
178{
179 /** Magic string which identifies this file as a version of VBox saved state
180 * file format (SSMFILEHDR_MAGIC_V1_2). */
181 char achMagic[32];
182 /** The size of this file. Used to check
183 * whether the save completed and that things are fine otherwise. */
184 uint64_t cbFile;
185 /** File checksum. The actual calculation skips past the u32CRC field. */
186 uint32_t u32CRC;
187 /** Padding. */
188 uint32_t u32Reserved;
189 /** The machine UUID. (Ignored if NIL.) */
190 RTUUID MachineUuid;
191
192 /** The major version number. */
193 uint16_t u16VerMajor;
194 /** The minor version number. */
195 uint16_t u16VerMinor;
196 /** The build number. */
197 uint32_t u32VerBuild;
198 /** The SVN revision. */
199 uint32_t u32SvnRev;
200
201 /** 32 or 64 depending on the host. */
202 uint8_t cHostBits;
203 /** The size of RTGCPHYS. */
204 uint8_t cbGCPhys;
205 /** The size of RTGCPTR. */
206 uint8_t cbGCPtr;
207 /** Padding. */
208 uint8_t au8Reserved;
209} SSMFILEHDR;
210AssertCompileSize(SSMFILEHDR, 64+16);
211AssertCompileMemberSize(SSMFILEHDR, achMagic, sizeof(SSMFILEHDR_MAGIC_V1_2));
212/** Pointer to a saved state file header. */
213typedef SSMFILEHDR *PSSMFILEHDR;
214
215
216/**
217 * Header of the saved state file, version 1.1.
218 */
219typedef struct SSMFILEHDRV11
220{
221 /** Magic string which identifies this file as a version of VBox saved state
222 * file format (SSMFILEHDR_MAGIC_V1_1). */
223 char achMagic[32];
224 /** The size of this file. Used to check
225 * whether the save completed and that things are fine otherwise. */
226 uint64_t cbFile;
227 /** File checksum. The actual calculation skips past the u32CRC field. */
228 uint32_t u32CRC;
229 /** Padding. */
230 uint32_t u32Reserved;
231 /** The machine UUID. (Ignored if NIL.) */
232 RTUUID MachineUuid;
233} SSMFILEHDRV11;
234AssertCompileSize(SSMFILEHDRV11, 64);
235/** Pointer to a saved state file header. */
236typedef SSMFILEHDRV11 *PSSMFILEHDRV11;
237
238
239/**
240 * The x86 edition of the 1.0 header.
241 */
242#pragma pack(1) /* darn, MachineUuid got missaligned! */
243typedef struct SSMFILEHDRV10X86
244{
245 /** Magic string which identifies this file as a version of VBox saved state
246 * file format (SSMFILEHDR_MAGIC_V1_0). */
247 char achMagic[32];
248 /** The size of this file. Used to check
249 * whether the save completed and that things are fine otherwise. */
250 uint64_t cbFile;
251 /** File checksum. The actual calculation skips past the u32CRC field. */
252 uint32_t u32CRC;
253 /** The machine UUID. (Ignored if NIL.) */
254 RTUUID MachineUuid;
255} SSMFILEHDRV10X86;
256#pragma pack()
257/** Pointer to a SSMFILEHDRV10X86. */
258typedef SSMFILEHDRV10X86 *PSSMFILEHDRV10X86;
259
260/**
261 * The amd64 edition of the 1.0 header.
262 */
263typedef SSMFILEHDR SSMFILEHDRV10AMD64;
264/** Pointer to SSMFILEHDRV10AMD64. */
265typedef SSMFILEHDRV10AMD64 *PSSMFILEHDRV10AMD64;
266
267
268/**
269 * Data unit header.
270 */
271typedef struct SSMFILEUNITHDR
272{
273 /** Magic (SSMFILEUNITHDR_MAGIC or SSMFILEUNITHDR_END). */
274 char achMagic[8];
275 /** Number of bytes in this data unit including the header. */
276 uint64_t cbUnit;
277 /** Data version. */
278 uint32_t u32Version;
279 /** Instance number. */
280 uint32_t u32Instance;
281 /** Size of the data unit name including the terminator. (bytes) */
282 uint32_t cchName;
283 /** Data unit name. */
284 char szName[1];
285} SSMFILEUNITHDR;
286/** Pointer to SSMFILEUNITHDR. */
287typedef SSMFILEUNITHDR *PSSMFILEUNITHDR;
288
289
290/*******************************************************************************
291* Internal Functions *
292*******************************************************************************/
293static int ssmR3LazyInit(PVM pVM);
294static DECLCALLBACK(int) ssmR3SelfSaveExec(PVM pVM, PSSMHANDLE pSSM);
295static DECLCALLBACK(int) ssmR3SelfLoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
296static int ssmR3Register(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess, PSSMUNIT *ppUnit);
297static int ssmR3CalcChecksum(RTFILE File, uint64_t cbFile, uint32_t *pu32CRC);
298static void ssmR3Progress(PSSMHANDLE pSSM, uint64_t cbAdvance);
299static int ssmR3Validate(RTFILE File, PSSMFILEHDR pHdr, size_t *pcbFileHdr);
300static PSSMUNIT ssmR3Find(PVM pVM, const char *pszName, uint32_t u32Instance);
301static int ssmR3WriteFinish(PSSMHANDLE pSSM);
302static int ssmR3Write(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf);
303static DECLCALLBACK(int) ssmR3WriteOut(void *pvSSM, const void *pvBuf, size_t cbBuf);
304static void ssmR3ReadFinish(PSSMHANDLE pSSM);
305static int ssmR3Read(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf);
306static DECLCALLBACK(int) ssmR3ReadIn(void *pvSSM, void *pvBuf, size_t cbBuf, size_t *pcbRead);
307
308
309/**
310 * Performs lazy initialization of the SSM.
311 *
312 * @returns VBox status code.
313 * @param pVM The VM.
314 */
315static int ssmR3LazyInit(PVM pVM)
316{
317 /*
318 * Register a saved state unit which we use to put the VirtualBox version,
319 * revision and similar stuff in.
320 */
321 pVM->ssm.s.fInitialized = true;
322 int rc = SSMR3RegisterInternal(pVM, "SSM", 0 /*u32Instance*/, 1/*u32Version*/, 64 /*cbGuess*/,
323 NULL /*pfnSavePrep*/, ssmR3SelfSaveExec, NULL /*pfnSaveDone*/,
324 NULL /*pfnSavePrep*/, ssmR3SelfLoadExec, NULL /*pfnSaveDone*/);
325 pVM->ssm.s.fInitialized = RT_SUCCESS(rc);
326 return rc;
327}
328
329
330/**
331 * For saving usful things without having to go thru the tedious process of
332 * adding it to the header.
333 *
334 * @returns VBox status code.
335 * @param pVM Pointer to the shared VM structure.
336 * @param pSSM The SSM handle.
337 */
338static DECLCALLBACK(int) ssmR3SelfSaveExec(PVM pVM, PSSMHANDLE pSSM)
339{
340 /*
341 * String table containg pairs of variable and value string.
342 * Terminated by two empty strings.
343 */
344#ifdef VBOX_OSE
345 SSMR3PutStrZ(pSSM, "OSE");
346 SSMR3PutStrZ(pSSM, "true");
347#endif
348
349 /* terminator */
350 SSMR3PutStrZ(pSSM, "");
351 return SSMR3PutStrZ(pSSM, "");
352}
353
354
355/**
356 * For load the version + revision and stuff.
357 *
358 * @returns VBox status code.
359 * @param pVM Pointer to the shared VM structure.
360 * @param pSSM The SSM handle.
361 * @param u32Version The version (1).
362 */
363static DECLCALLBACK(int) ssmR3SelfLoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
364{
365 AssertLogRelMsgReturn(u32Version == 1, ("%d", u32Version), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
366
367 /*
368 * String table containg pairs of variable and value string.
369 * Terminated by two empty strings.
370 */
371 for (unsigned i = 0; ; i++)
372 {
373 char szVar[128];
374 char szValue[1024];
375 int rc = SSMR3GetStrZ(pSSM, szVar, sizeof(szVar));
376 AssertRCReturn(rc, rc);
377 rc = SSMR3GetStrZ(pSSM, szValue, sizeof(szValue));
378 AssertRCReturn(rc, rc);
379 if (!szVar[0] && !szValue[0])
380 break;
381 if (i == 0)
382 LogRel(("SSM: Saved state info:\n"));
383 LogRel(("SSM: %s: %s\n", szVar, szValue));
384 }
385 return VINF_SUCCESS;
386}
387
388
389/**
390 * Internal registration worker.
391 *
392 * @returns VBox status code.
393 * @param pVM The VM handle.
394 * @param pszName Data unit name.
395 * @param u32Instance The instance id.
396 * @param u32Version The data unit version.
397 * @param cbGuess The guessed data unit size.
398 * @param ppUnit Where to store the insterted unit node.
399 * Caller must fill in the missing details.
400 */
401static int ssmR3Register(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess, PSSMUNIT *ppUnit)
402{
403 /*
404 * Lazy init.
405 */
406 if (!pVM->ssm.s.fInitialized)
407 {
408 int rc = ssmR3LazyInit(pVM);
409 AssertRCReturn(rc, rc);
410 }
411
412 /*
413 * Walk to the end of the list checking for duplicates as we go.
414 */
415 size_t cchName = strlen(pszName);
416 PSSMUNIT pUnitPrev = NULL;
417 PSSMUNIT pUnit = pVM->ssm.s.pHead;
418 while (pUnit)
419 {
420 if ( pUnit->u32Instance == u32Instance
421 && pUnit->cchName == cchName
422 && !memcmp(pUnit->szName, pszName, cchName))
423 {
424 AssertMsgFailed(("Duplicate registration %s\n", pszName));
425 return VERR_SSM_UNIT_EXISTS;
426 }
427 /* next */
428 pUnitPrev = pUnit;
429 pUnit = pUnit->pNext;
430 }
431
432 /*
433 * Allocate new node.
434 */
435 pUnit = (PSSMUNIT)MMR3HeapAllocZ(pVM, MM_TAG_SSM, RT_OFFSETOF(SSMUNIT, szName[cchName + 1]));
436 if (!pUnit)
437 return VERR_NO_MEMORY;
438
439 /*
440 * Fill in (some) data. (Stuff is zero'ed.)
441 */
442 pUnit->u32Version = u32Version;
443 pUnit->u32Instance = u32Instance;
444 pUnit->cbGuess = cbGuess;
445 pUnit->cchName = cchName;
446 memcpy(pUnit->szName, pszName, cchName);
447
448 /*
449 * Insert
450 */
451 if (pUnitPrev)
452 pUnitPrev->pNext = pUnit;
453 else
454 pVM->ssm.s.pHead = pUnit;
455
456 *ppUnit = pUnit;
457 return VINF_SUCCESS;
458}
459
460
461/**
462 * Register a PDM Devices data unit.
463 *
464 * @returns VBox status.
465 *
466 * @param pVM The VM handle.
467 * @param pDevIns Device instance.
468 * @param pszName Data unit name.
469 * @param u32Instance The instance identifier of the data unit.
470 * This must together with the name be unique.
471 * @param u32Version Data layout version number.
472 * @param cbGuess The approximate amount of data in the unit.
473 * Only for progress indicators.
474 * @param pfnSavePrep Prepare save callback, optional.
475 * @param pfnSaveExec Execute save callback, optional.
476 * @param pfnSaveDone Done save callback, optional.
477 * @param pfnLoadPrep Prepare load callback, optional.
478 * @param pfnLoadExec Execute load callback, optional.
479 * @param pfnLoadDone Done load callback, optional.
480 */
481VMMR3DECL(int) SSMR3RegisterDevice(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
482 PFNSSMDEVSAVEPREP pfnSavePrep, PFNSSMDEVSAVEEXEC pfnSaveExec, PFNSSMDEVSAVEDONE pfnSaveDone,
483 PFNSSMDEVLOADPREP pfnLoadPrep, PFNSSMDEVLOADEXEC pfnLoadExec, PFNSSMDEVLOADDONE pfnLoadDone)
484{
485 PSSMUNIT pUnit;
486 int rc = ssmR3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
487 if (RT_SUCCESS(rc))
488 {
489 pUnit->enmType = SSMUNITTYPE_DEV;
490 pUnit->u.Dev.pfnSavePrep = pfnSavePrep;
491 pUnit->u.Dev.pfnSaveExec = pfnSaveExec;
492 pUnit->u.Dev.pfnSaveDone = pfnSaveDone;
493 pUnit->u.Dev.pfnLoadPrep = pfnLoadPrep;
494 pUnit->u.Dev.pfnLoadExec = pfnLoadExec;
495 pUnit->u.Dev.pfnLoadDone = pfnLoadDone;
496 pUnit->u.Dev.pDevIns = pDevIns;
497 }
498 return rc;
499}
500
501
502/**
503 * Register a PDM driver data unit.
504 *
505 * @returns VBox status.
506 *
507 * @param pVM The VM handle.
508 * @param pDrvIns Driver instance.
509 * @param pszName Data unit name.
510 * @param u32Instance The instance identifier of the data unit.
511 * This must together with the name be unique.
512 * @param u32Version Data layout version number.
513 * @param cbGuess The approximate amount of data in the unit.
514 * Only for progress indicators.
515 * @param pfnSavePrep Prepare save callback, optional.
516 * @param pfnSaveExec Execute save callback, optional.
517 * @param pfnSaveDone Done save callback, optional.
518 * @param pfnLoadPrep Prepare load callback, optional.
519 * @param pfnLoadExec Execute load callback, optional.
520 * @param pfnLoadDone Done load callback, optional.
521 */
522VMMR3DECL(int) SSMR3RegisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
523 PFNSSMDRVSAVEPREP pfnSavePrep, PFNSSMDRVSAVEEXEC pfnSaveExec, PFNSSMDRVSAVEDONE pfnSaveDone,
524 PFNSSMDRVLOADPREP pfnLoadPrep, PFNSSMDRVLOADEXEC pfnLoadExec, PFNSSMDRVLOADDONE pfnLoadDone)
525{
526 PSSMUNIT pUnit;
527 int rc = ssmR3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
528 if (RT_SUCCESS(rc))
529 {
530 pUnit->enmType = SSMUNITTYPE_DRV;
531 pUnit->u.Drv.pfnSavePrep = pfnSavePrep;
532 pUnit->u.Drv.pfnSaveExec = pfnSaveExec;
533 pUnit->u.Drv.pfnSaveDone = pfnSaveDone;
534 pUnit->u.Drv.pfnLoadPrep = pfnLoadPrep;
535 pUnit->u.Drv.pfnLoadExec = pfnLoadExec;
536 pUnit->u.Drv.pfnLoadDone = pfnLoadDone;
537 pUnit->u.Drv.pDrvIns = pDrvIns;
538 }
539 return rc;
540}
541
542
543/**
544 * Register a internal data unit.
545 *
546 * @returns VBox status.
547 *
548 * @param pVM The VM handle.
549 * @param pszName Data unit name.
550 * @param u32Instance The instance identifier of the data unit.
551 * This must together with the name be unique.
552 * @param u32Version Data layout version number.
553 * @param cbGuess The approximate amount of data in the unit.
554 * Only for progress indicators.
555 * @param pfnSavePrep Prepare save callback, optional.
556 * @param pfnSaveExec Execute save callback, optional.
557 * @param pfnSaveDone Done save callback, optional.
558 * @param pfnLoadPrep Prepare load callback, optional.
559 * @param pfnLoadExec Execute load callback, optional.
560 * @param pfnLoadDone Done load callback, optional.
561 */
562VMMR3DECL(int) SSMR3RegisterInternal(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
563 PFNSSMINTSAVEPREP pfnSavePrep, PFNSSMINTSAVEEXEC pfnSaveExec, PFNSSMINTSAVEDONE pfnSaveDone,
564 PFNSSMINTLOADPREP pfnLoadPrep, PFNSSMINTLOADEXEC pfnLoadExec, PFNSSMINTLOADDONE pfnLoadDone)
565{
566 PSSMUNIT pUnit;
567 int rc = ssmR3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
568 if (RT_SUCCESS(rc))
569 {
570 pUnit->enmType = SSMUNITTYPE_INTERNAL;
571 pUnit->u.Internal.pfnSavePrep = pfnSavePrep;
572 pUnit->u.Internal.pfnSaveExec = pfnSaveExec;
573 pUnit->u.Internal.pfnSaveDone = pfnSaveDone;
574 pUnit->u.Internal.pfnLoadPrep = pfnLoadPrep;
575 pUnit->u.Internal.pfnLoadExec = pfnLoadExec;
576 pUnit->u.Internal.pfnLoadDone = pfnLoadDone;
577 }
578 return rc;
579}
580
581
582/**
583 * Register an external data unit.
584 *
585 * @returns VBox status.
586 *
587 * @param pVM The VM handle.
588 * @param pszName Data unit name.
589 * @param u32Instance The instance identifier of the data unit.
590 * This must together with the name be unique.
591 * @param u32Version Data layout version number.
592 * @param cbGuess The approximate amount of data in the unit.
593 * Only for progress indicators.
594 * @param pfnSavePrep Prepare save callback, optional.
595 * @param pfnSaveExec Execute save callback, optional.
596 * @param pfnSaveDone Done save callback, optional.
597 * @param pfnLoadPrep Prepare load callback, optional.
598 * @param pfnLoadExec Execute load callback, optional.
599 * @param pfnLoadDone Done load callback, optional.
600 * @param pvUser User argument.
601 */
602VMMR3DECL(int) SSMR3RegisterExternal(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
603 PFNSSMEXTSAVEPREP pfnSavePrep, PFNSSMEXTSAVEEXEC pfnSaveExec, PFNSSMEXTSAVEDONE pfnSaveDone,
604 PFNSSMEXTLOADPREP pfnLoadPrep, PFNSSMEXTLOADEXEC pfnLoadExec, PFNSSMEXTLOADDONE pfnLoadDone, void *pvUser)
605{
606 PSSMUNIT pUnit;
607 int rc = ssmR3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
608 if (RT_SUCCESS(rc))
609 {
610 pUnit->enmType = SSMUNITTYPE_EXTERNAL;
611 pUnit->u.External.pfnSavePrep = pfnSavePrep;
612 pUnit->u.External.pfnSaveExec = pfnSaveExec;
613 pUnit->u.External.pfnSaveDone = pfnSaveDone;
614 pUnit->u.External.pfnLoadPrep = pfnLoadPrep;
615 pUnit->u.External.pfnLoadExec = pfnLoadExec;
616 pUnit->u.External.pfnLoadDone = pfnLoadDone;
617 pUnit->u.External.pvUser = pvUser;
618 }
619 return rc;
620}
621
622
623/**
624 * Deregister one or more PDM Device data units.
625 *
626 * @returns VBox status.
627 *
628 * @param pVM The VM handle.
629 * @param pDevIns Device instance.
630 * @param pszName Data unit name.
631 * Use NULL to deregister all data units for that device instance.
632 * @param u32Instance The instance identifier of the data unit.
633 * This must together with the name be unique.
634 * @remark Only for dynmaic data units and dynamic unloaded modules.
635 */
636VMMR3DECL(int) SSMR3DeregisterDevice(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t u32Instance)
637{
638 /*
639 * Validate input.
640 */
641 if (!pDevIns)
642 {
643 AssertMsgFailed(("pDevIns is NULL!\n"));
644 return VERR_INVALID_PARAMETER;
645 }
646
647 /*
648 * Search the list.
649 */
650 size_t cchName = pszName ? strlen(pszName) : 0;
651 int rc = pszName ? VERR_SSM_UNIT_NOT_FOUND : VINF_SUCCESS;
652 PSSMUNIT pUnitPrev = NULL;
653 PSSMUNIT pUnit = pVM->ssm.s.pHead;
654 while (pUnit)
655 {
656 if ( pUnit->enmType == SSMUNITTYPE_DEV
657 && ( !pszName
658 || ( pUnit->cchName == cchName
659 && !memcmp(pUnit->szName, pszName, cchName)))
660 && pUnit->u32Instance == u32Instance
661 )
662 {
663 if (pUnit->u.Dev.pDevIns == pDevIns)
664 {
665 /*
666 * Unlink it, advance pointer, and free the node.
667 */
668 PSSMUNIT pFree = pUnit;
669 pUnit = pUnit->pNext;
670 if (pUnitPrev)
671 pUnitPrev->pNext = pUnit;
672 else
673 pVM->ssm.s.pHead = pUnit;
674 Log(("SSM: Removed data unit '%s' (pdm dev).\n", pFree->szName));
675 MMR3HeapFree(pFree);
676 if (pszName)
677 return VINF_SUCCESS;
678 rc = VINF_SUCCESS;
679 continue;
680 }
681 else if (pszName)
682 {
683 AssertMsgFailed(("Caller is not owner! Owner=%p Caller=%p %s\n",
684 pUnit->u.Dev.pDevIns, pDevIns, pszName));
685 return VERR_SSM_UNIT_NOT_OWNER;
686 }
687 }
688
689 /* next */
690 pUnitPrev = pUnit;
691 pUnit = pUnit->pNext;
692 }
693
694 return rc;
695}
696
697
698/**
699 * Deregister one ore more PDM Driver data units.
700 *
701 * @returns VBox status.
702 * @param pVM The VM handle.
703 * @param pDrvIns Driver instance.
704 * @param pszName Data unit name.
705 * Use NULL to deregister all data units for that driver instance.
706 * @param u32Instance The instance identifier of the data unit.
707 * This must together with the name be unique. Ignored if pszName is NULL.
708 * @remark Only for dynmaic data units and dynamic unloaded modules.
709 */
710VMMR3DECL(int) SSMR3DeregisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t u32Instance)
711{
712 /*
713 * Validate input.
714 */
715 if (!pDrvIns)
716 {
717 AssertMsgFailed(("pDrvIns is NULL!\n"));
718 return VERR_INVALID_PARAMETER;
719 }
720
721 /*
722 * Search the list.
723 */
724 size_t cchName = pszName ? strlen(pszName) : 0;
725 int rc = pszName ? VERR_SSM_UNIT_NOT_FOUND : VINF_SUCCESS;
726 PSSMUNIT pUnitPrev = NULL;
727 PSSMUNIT pUnit = pVM->ssm.s.pHead;
728 while (pUnit)
729 {
730 if ( pUnit->enmType == SSMUNITTYPE_DRV
731 && ( !pszName
732 || ( pUnit->cchName == cchName
733 && !memcmp(pUnit->szName, pszName, cchName)
734 && pUnit->u32Instance == u32Instance))
735 )
736 {
737 if (pUnit->u.Drv.pDrvIns == pDrvIns)
738 {
739 /*
740 * Unlink it, advance pointer, and free the node.
741 */
742 PSSMUNIT pFree = pUnit;
743 pUnit = pUnit->pNext;
744 if (pUnitPrev)
745 pUnitPrev->pNext = pUnit;
746 else
747 pVM->ssm.s.pHead = pUnit;
748 Log(("SSM: Removed data unit '%s' (pdm drv).\n", pFree->szName));
749 MMR3HeapFree(pFree);
750 if (pszName)
751 return VINF_SUCCESS;
752 rc = VINF_SUCCESS;
753 continue;
754 }
755 else if (pszName)
756 {
757 AssertMsgFailed(("Caller is not owner! Owner=%p Caller=%p %s\n",
758 pUnit->u.Drv.pDrvIns, pDrvIns, pszName));
759 return VERR_SSM_UNIT_NOT_OWNER;
760 }
761 }
762
763 /* next */
764 pUnitPrev = pUnit;
765 pUnit = pUnit->pNext;
766 }
767
768 return rc;
769}
770
771
772/**
773 * Deregister a data unit.
774 *
775 * @returns VBox status.
776 * @param pVM The VM handle.
777 * @param enmType Unit type
778 * @param pszName Data unit name.
779 * @remark Only for dynmaic data units.
780 */
781static int ssmR3DeregisterByNameAndType(PVM pVM, const char *pszName, SSMUNITTYPE enmType)
782{
783 /*
784 * Validate input.
785 */
786 if (!pszName)
787 {
788 AssertMsgFailed(("pszName is NULL!\n"));
789 return VERR_INVALID_PARAMETER;
790 }
791
792 /*
793 * Search the list.
794 */
795 size_t cchName = strlen(pszName);
796 int rc = VERR_SSM_UNIT_NOT_FOUND;
797 PSSMUNIT pUnitPrev = NULL;
798 PSSMUNIT pUnit = pVM->ssm.s.pHead;
799 while (pUnit)
800 {
801 if ( pUnit->enmType == enmType
802 && pUnit->cchName == cchName
803 && !memcmp(pUnit->szName, pszName, cchName))
804 {
805 /*
806 * Unlink it, advance pointer, and free the node.
807 */
808 PSSMUNIT pFree = pUnit;
809 pUnit = pUnit->pNext;
810 if (pUnitPrev)
811 pUnitPrev->pNext = pUnit;
812 else
813 pVM->ssm.s.pHead = pUnit;
814 Log(("SSM: Removed data unit '%s' (type=%d).\n", pFree->szName, enmType));
815 MMR3HeapFree(pFree);
816 return VINF_SUCCESS;
817 }
818
819 /* next */
820 pUnitPrev = pUnit;
821 pUnit = pUnit->pNext;
822 }
823
824 return rc;
825}
826
827
828/**
829 * Deregister an internal data unit.
830 *
831 * @returns VBox status.
832 * @param pVM The VM handle.
833 * @param pszName Data unit name.
834 * @remark Only for dynmaic data units.
835 */
836VMMR3DECL(int) SSMR3DeregisterInternal(PVM pVM, const char *pszName)
837{
838 return ssmR3DeregisterByNameAndType(pVM, pszName, SSMUNITTYPE_INTERNAL);
839}
840
841
842/**
843 * Deregister an external data unit.
844 *
845 * @returns VBox status.
846 * @param pVM The VM handle.
847 * @param pszName Data unit name.
848 * @remark Only for dynmaic data units.
849 */
850VMMR3DECL(int) SSMR3DeregisterExternal(PVM pVM, const char *pszName)
851{
852 return ssmR3DeregisterByNameAndType(pVM, pszName, SSMUNITTYPE_EXTERNAL);
853}
854
855
856/**
857 * Calculate the checksum of a file portion.
858 *
859 * The current implementation is a cut&past of the libkern/crc32.c file from FreeBSD.
860 *
861 * @returns VBox status.
862 * @param File Handle to the file.
863 * @param cbFile Size of the file.
864 * @param pu32CRC Where to store the calculated checksum.
865 */
866static int ssmR3CalcChecksum(RTFILE File, uint64_t cbFile, uint32_t *pu32CRC)
867{
868 /*
869 * Allocate a buffer.
870 */
871 void *pvBuf = RTMemTmpAlloc(32*1024);
872 if (!pvBuf)
873 return VERR_NO_TMP_MEMORY;
874
875 /*
876 * Loop reading and calculating CRC32.
877 */
878 int rc = VINF_SUCCESS;
879 uint32_t u32CRC = RTCrc32Start();
880 while (cbFile)
881 {
882 /* read chunk */
883 register unsigned cbToRead = 32*1024;
884 if (cbFile < 32*1024)
885 cbToRead = (unsigned)cbFile;
886 rc = RTFileRead(File, pvBuf, cbToRead, NULL);
887 if (RT_FAILURE(rc))
888 {
889 AssertMsgFailed(("Failed with rc=%Rrc while calculating crc.\n", rc));
890 RTMemTmpFree(pvBuf);
891 return rc;
892 }
893
894 /* update total */
895 cbFile -= cbToRead;
896
897 /* calc crc32. */
898 u32CRC = RTCrc32Process(u32CRC, pvBuf, cbToRead);
899 }
900 RTMemTmpFree(pvBuf);
901
902 /* store the calculated crc */
903 u32CRC = RTCrc32Finish(u32CRC);
904 Log(("SSM: u32CRC=0x%08x\n", u32CRC));
905 *pu32CRC = u32CRC;
906
907 return VINF_SUCCESS;
908}
909
910
911/**
912 * Works the progress calculation.
913 *
914 * @param pSSM The SSM handle.
915 * @param cbAdvance Number of bytes to advance
916 */
917static void ssmR3Progress(PSSMHANDLE pSSM, uint64_t cbAdvance)
918{
919 /* Can't advance it beyond the estimated end of the unit. */
920 uint64_t cbLeft = pSSM->offEstUnitEnd - pSSM->offEst;
921 if (cbAdvance > cbLeft)
922 cbAdvance = cbLeft;
923 pSSM->offEst += cbAdvance;
924
925 /* uPercentPrepare% prepare, xx% exec, uPercentDone% done+crc */
926 while (pSSM->offEst >= pSSM->offEstProgress && pSSM->uPercent <= 100-pSSM->uPercentDone)
927 {
928 if (pSSM->pfnProgress)
929 pSSM->pfnProgress(pSSM->pVM, pSSM->uPercent, pSSM->pvUser);
930 pSSM->uPercent++;
931 pSSM->offEstProgress = (pSSM->uPercent - pSSM->uPercentPrepare) * pSSM->cbEstTotal
932 / (100 - pSSM->uPercentDone - pSSM->uPercentPrepare);
933 }
934}
935
936
937/**
938 * Start VM save operation.
939 *
940 * @returns VBox status.
941 *
942 * @param pVM The VM handle.
943 * @param pszFilename Name of the file to save the state in.
944 * @param enmAfter What is planned after a successful save operation.
945 * @param pfnProgress Progress callback. Optional.
946 * @param pvUser User argument for the progress callback.
947 *
948 * @thread EMT
949 */
950VMMR3DECL(int) SSMR3Save(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser)
951{
952 LogFlow(("SSMR3Save: pszFilename=%p:{%s} enmAfter=%d pfnProgress=%p pvUser=%p\n", pszFilename, pszFilename, enmAfter, pfnProgress, pvUser));
953 VM_ASSERT_EMT(pVM);
954
955 /*
956 * Validate input.
957 */
958 if ( enmAfter != SSMAFTER_DESTROY
959 && enmAfter != SSMAFTER_CONTINUE)
960 {
961 AssertMsgFailed(("Invalid enmAfter=%d!\n", enmAfter));
962 return VERR_INVALID_PARAMETER;
963 }
964
965 /*
966 * Create the handle and try open the file.
967 *
968 * Note that there might be quite some work to do after executing the saving,
969 * so we reserve 20% for the 'Done' period. The checksumming and closing of
970 * the saved state file might take a long time.
971 */
972 SSMHANDLE Handle = {0};
973 Handle.File = NIL_RTFILE;
974 Handle.pVM = pVM;
975 Handle.cbFileHdr = sizeof(SSMFILEHDR);
976 Handle.enmOp = SSMSTATE_INVALID;
977 Handle.enmAfter = enmAfter;
978 Handle.rc = VINF_SUCCESS;
979 Handle.pZipComp = NULL;
980 Handle.pZipDecomp = NULL;
981 Handle.pfnProgress = pfnProgress;
982 Handle.pvUser = pvUser;
983 Handle.uPercent = 0;
984 Handle.offEstProgress = 0;
985 Handle.cbEstTotal = 0;
986 Handle.offEst = 0;
987 Handle.offEstUnitEnd = 0;
988 Handle.uPercentPrepare = 20;
989 Handle.uPercentDone = 2;
990 Handle.cbGCPhys = sizeof(RTGCPHYS);
991 Handle.cbGCPtr = sizeof(RTGCPTR);
992 Handle.fFixedGCPtrSize = true;
993
994 int rc = RTFileOpen(&Handle.File, pszFilename, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE);
995 if (RT_FAILURE(rc))
996 {
997 LogRel(("SSM: Failed to create save state file '%s', rc=%Rrc.\n", pszFilename, rc));
998 return rc;
999 }
1000
1001 Log(("SSM: Starting state save to file '%s'...\n", pszFilename));
1002
1003 /*
1004 * Write header.
1005 */
1006 SSMFILEHDR Hdr =
1007 {
1008 /* .achMagic[32] = */ SSMFILEHDR_MAGIC_V1_2,
1009 /* .cbFile = */ 0,
1010 /* .u32CRC = */ 0,
1011 /* .u32Reserved = */ 0,
1012 /* .MachineUuid = */ {{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}},
1013 /* .u16VerMajor = */ VBOX_VERSION_MAJOR,
1014 /* .u16VerMinor = */ VBOX_VERSION_MINOR,
1015 /* .u32VerBuild = */ VBOX_VERSION_BUILD,
1016 /* .u32SvnRev = */ VMMGetSvnRev(),
1017 /* .cHostBits = */ HC_ARCH_BITS,
1018 /* .cbGCPhys = */ sizeof(RTGCPHYS),
1019 /* .cbGCPtr = */ sizeof(RTGCPTR),
1020 /* .au8Reserved = */ 0
1021 };
1022 rc = RTFileWrite(Handle.File, &Hdr, sizeof(Hdr), NULL);
1023 if (RT_SUCCESS(rc))
1024 {
1025 /*
1026 * Clear the per unit flags.
1027 */
1028 PSSMUNIT pUnit;
1029 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1030 pUnit->fCalled = false;
1031
1032 /*
1033 * Do the prepare run.
1034 */
1035 Handle.rc = VINF_SUCCESS;
1036 Handle.enmOp = SSMSTATE_SAVE_PREP;
1037 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1038 {
1039 switch (pUnit->enmType)
1040 {
1041 case SSMUNITTYPE_DEV:
1042 if (pUnit->u.Dev.pfnSavePrep)
1043 {
1044 rc = pUnit->u.Dev.pfnSavePrep(pUnit->u.Dev.pDevIns, &Handle);
1045 pUnit->fCalled = true;
1046 }
1047 break;
1048 case SSMUNITTYPE_DRV:
1049 if (pUnit->u.Drv.pfnSavePrep)
1050 {
1051 rc = pUnit->u.Drv.pfnSavePrep(pUnit->u.Drv.pDrvIns, &Handle);
1052 pUnit->fCalled = true;
1053 }
1054 break;
1055 case SSMUNITTYPE_INTERNAL:
1056 if (pUnit->u.Internal.pfnSavePrep)
1057 {
1058 rc = pUnit->u.Internal.pfnSavePrep(pVM, &Handle);
1059 pUnit->fCalled = true;
1060 }
1061 break;
1062 case SSMUNITTYPE_EXTERNAL:
1063 if (pUnit->u.External.pfnSavePrep)
1064 {
1065 rc = pUnit->u.External.pfnSavePrep(&Handle, pUnit->u.External.pvUser);
1066 pUnit->fCalled = true;
1067 }
1068 break;
1069 }
1070 if (RT_FAILURE(rc))
1071 {
1072 LogRel(("SSM: Prepare save failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
1073 break;
1074 }
1075
1076 Handle.cbEstTotal += pUnit->cbGuess;
1077 }
1078
1079 /* Progress. */
1080 if (pfnProgress)
1081 pfnProgress(pVM, Handle.uPercentPrepare-1, pvUser);
1082 Handle.uPercent = Handle.uPercentPrepare;
1083
1084 /*
1085 * Do the execute run.
1086 */
1087 if (RT_SUCCESS(rc))
1088 {
1089 Handle.enmOp = SSMSTATE_SAVE_EXEC;
1090 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1091 {
1092 /*
1093 * Estimate.
1094 */
1095 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
1096 Handle.offEstUnitEnd += pUnit->cbGuess;
1097
1098 /*
1099 * Does this unit have a callback? If, not skip it.
1100 */
1101 bool fSkip;
1102 switch (pUnit->enmType)
1103 {
1104 case SSMUNITTYPE_DEV: fSkip = pUnit->u.Dev.pfnSaveExec == NULL; break;
1105 case SSMUNITTYPE_DRV: fSkip = pUnit->u.Drv.pfnSaveExec == NULL; break;
1106 case SSMUNITTYPE_INTERNAL: fSkip = pUnit->u.Internal.pfnSaveExec == NULL; break;
1107 case SSMUNITTYPE_EXTERNAL: fSkip = pUnit->u.External.pfnSaveExec == NULL; break;
1108 default: fSkip = true; break;
1109 }
1110 if (fSkip)
1111 {
1112 pUnit->fCalled = true;
1113 continue;
1114 }
1115
1116 /*
1117 * Write data unit header
1118 */
1119 uint64_t offHdr = RTFileTell(Handle.File);
1120 SSMFILEUNITHDR UnitHdr = { SSMFILEUNITHDR_MAGIC, 0, pUnit->u32Version, pUnit->u32Instance, (uint32_t)pUnit->cchName + 1, { '\0' } };
1121 rc = RTFileWrite(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), NULL);
1122 if (RT_SUCCESS(rc))
1123 {
1124 rc = RTFileWrite(Handle.File, &pUnit->szName[0], pUnit->cchName + 1, NULL);
1125 if (RT_SUCCESS(rc))
1126 {
1127 /*
1128 * Call the execute handler.
1129 */
1130 switch (pUnit->enmType)
1131 {
1132 case SSMUNITTYPE_DEV:
1133 rc = pUnit->u.Dev.pfnSaveExec(pUnit->u.Dev.pDevIns, &Handle);
1134 break;
1135 case SSMUNITTYPE_DRV:
1136 rc = pUnit->u.Drv.pfnSaveExec(pUnit->u.Drv.pDrvIns, &Handle);
1137 break;
1138 case SSMUNITTYPE_INTERNAL:
1139 rc = pUnit->u.Internal.pfnSaveExec(pVM, &Handle);
1140 break;
1141 case SSMUNITTYPE_EXTERNAL:
1142 pUnit->u.External.pfnSaveExec(&Handle, pUnit->u.External.pvUser);
1143 rc = Handle.rc;
1144 break;
1145 }
1146 pUnit->fCalled = true;
1147 if (RT_FAILURE(Handle.rc) && RT_SUCCESS(rc))
1148 rc = Handle.rc;
1149 if (RT_SUCCESS(rc))
1150 {
1151 /*
1152 * Flush buffer / end compression stream.
1153 */
1154 if (Handle.pZipComp)
1155 rc = ssmR3WriteFinish(&Handle);
1156 if (RT_SUCCESS(rc))
1157 {
1158 /*
1159 * Update header with correct length.
1160 */
1161 uint64_t offEnd = RTFileTell(Handle.File);
1162 rc = RTFileSeek(Handle.File, offHdr, RTFILE_SEEK_BEGIN, NULL);
1163 if (RT_SUCCESS(rc))
1164 {
1165 UnitHdr.cbUnit = offEnd - offHdr;
1166 rc = RTFileWrite(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), NULL);
1167 if (RT_SUCCESS(rc))
1168 {
1169 rc = RTFileSeek(Handle.File, offEnd, RTFILE_SEEK_BEGIN, NULL);
1170 if (RT_SUCCESS(rc))
1171 Log(("SSM: Data unit: offset %#9llx size %9lld '%s'\n", offHdr, UnitHdr.cbUnit, pUnit->szName));
1172 }
1173 }
1174 }
1175 else
1176 {
1177 LogRel(("SSM: Failed ending compression stream. rc=%Rrc\n", rc));
1178 break;
1179 }
1180 }
1181 else
1182 {
1183 LogRel(("SSM: Execute save failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
1184 break;
1185 }
1186 }
1187 }
1188 if (RT_FAILURE(rc))
1189 {
1190 LogRel(("SSM: Failed to write unit header. rc=%Rrc\n", rc));
1191 break;
1192 }
1193 } /* for each unit */
1194
1195 /* finish the progress. */
1196 if (RT_SUCCESS(rc))
1197 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
1198 }
1199 /* (progress should be pending 99% now) */
1200 AssertMsg(RT_FAILURE(rc) || Handle.uPercent == (101-Handle.uPercentDone), ("%d\n", Handle.uPercent));
1201
1202 /*
1203 * Do the done run.
1204 */
1205 Handle.rc = rc;
1206 Handle.enmOp = SSMSTATE_SAVE_DONE;
1207 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1208 {
1209 switch (pUnit->enmType)
1210 {
1211 case SSMUNITTYPE_DEV:
1212 if ( pUnit->u.Dev.pfnSaveDone
1213 && ( pUnit->fCalled
1214 || (!pUnit->u.Dev.pfnSavePrep && !pUnit->u.Dev.pfnSaveExec)))
1215 rc = pUnit->u.Dev.pfnSaveDone(pUnit->u.Dev.pDevIns, &Handle);
1216 break;
1217 case SSMUNITTYPE_DRV:
1218 if ( pUnit->u.Drv.pfnSaveDone
1219 && ( pUnit->fCalled
1220 || (!pUnit->u.Drv.pfnSavePrep && !pUnit->u.Drv.pfnSaveExec)))
1221 rc = pUnit->u.Drv.pfnSaveDone(pUnit->u.Drv.pDrvIns, &Handle);
1222 break;
1223 case SSMUNITTYPE_INTERNAL:
1224 if ( pUnit->u.Internal.pfnSaveDone
1225 && ( pUnit->fCalled
1226 || (!pUnit->u.Internal.pfnSavePrep && !pUnit->u.Internal.pfnSaveExec)))
1227 rc = pUnit->u.Internal.pfnSaveDone(pVM, &Handle);
1228 break;
1229 case SSMUNITTYPE_EXTERNAL:
1230 if ( pUnit->u.External.pfnSaveDone
1231 && ( pUnit->fCalled
1232 || (!pUnit->u.External.pfnSavePrep && !pUnit->u.External.pfnSaveExec)))
1233 rc = pUnit->u.External.pfnSaveDone(&Handle, pUnit->u.External.pvUser);
1234 break;
1235 }
1236 if (RT_FAILURE(rc))
1237 {
1238 LogRel(("SSM: Done save failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
1239 if (RT_SUCCESS(Handle.rc))
1240 Handle.rc = rc;
1241 }
1242 }
1243 rc = Handle.rc;
1244
1245 /*
1246 * Finalize the file if successfully saved.
1247 */
1248 if (RT_SUCCESS(rc))
1249 {
1250 /* end record */
1251 SSMFILEUNITHDR UnitHdr = { SSMFILEUNITHDR_END, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), 0, '\0'};
1252 rc = RTFileWrite(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), NULL);
1253 if (RT_SUCCESS(rc))
1254 {
1255 /* get size */
1256 Hdr.cbFile = RTFileTell(Handle.File);
1257 /* calc checksum */
1258 rc = RTFileSeek(Handle.File, RT_OFFSETOF(SSMFILEHDR, u32CRC) + sizeof(Hdr.u32CRC), RTFILE_SEEK_BEGIN, NULL);
1259 if (RT_SUCCESS(rc))
1260 rc = ssmR3CalcChecksum(Handle.File, Hdr.cbFile - sizeof(Hdr), &Hdr.u32CRC);
1261 if (RT_SUCCESS(rc))
1262 {
1263 if (pfnProgress)
1264 pfnProgress(pVM, 90, pvUser);
1265
1266 /*
1267 * Write the update the header to the file.
1268 */
1269 rc = RTFileSeek(Handle.File, 0, RTFILE_SEEK_BEGIN, NULL);
1270 if (RT_SUCCESS(rc))
1271 rc = RTFileWrite(Handle.File, &Hdr, sizeof(Hdr), NULL);
1272 if (RT_SUCCESS(rc))
1273 {
1274 rc = RTFileClose(Handle.File);
1275 AssertRC(rc);
1276 if (pfnProgress)
1277 pfnProgress(pVM, 100, pvUser);
1278 Log(("SSM: Successfully saved the vm state to '%s'.\n", pszFilename));
1279 Log(("\n\n\n"));
1280 DBGFR3InfoLog(pVM, "cpum", "verbose");
1281 DBGFR3InfoLog(pVM, "timers", NULL);
1282 DBGFR3InfoLog(pVM, "activetimers", NULL);
1283 DBGFR3InfoLog(pVM, "ioport", NULL);
1284 DBGFR3InfoLog(pVM, "mmio", NULL);
1285 DBGFR3InfoLog(pVM, "phys", NULL);
1286 Log(("\n\n\n"));
1287 return VINF_SUCCESS;
1288 }
1289
1290 }
1291 }
1292 LogRel(("SSM: Failed to finalize state file! rc=%Rrc\n", pszFilename));
1293 }
1294 }
1295
1296 /*
1297 * Delete the file on failure and destroy any compressors.
1298 */
1299 int rc2 = RTFileClose(Handle.File);
1300 AssertRC(rc2);
1301 rc2 = RTFileDelete(pszFilename);
1302 AssertRC(rc2);
1303 if (Handle.pZipComp)
1304 RTZipCompDestroy(Handle.pZipComp);
1305
1306 return rc;
1307}
1308
1309
1310/**
1311 * Validates the integrity of a saved state file.
1312 *
1313 * @returns VBox status.
1314 * @param File File to validate.
1315 * The file position is undefined on return.
1316 * @param pHdr Where to store the file header.
1317 * @param pcbFileHdr Where to store the file header size.
1318 */
1319static int ssmR3Validate(RTFILE File, PSSMFILEHDR pHdr, size_t *pcbFileHdr)
1320{
1321 /*
1322 * Read the header.
1323 */
1324 int rc = RTFileRead(File, pHdr, sizeof(*pHdr), NULL);
1325 if (RT_FAILURE(rc))
1326 {
1327 Log(("SSM: Failed to read file header. rc=%Rrc\n", rc));
1328 return rc;
1329 }
1330
1331 /*
1332 * Verify the magic and make adjustments for versions differences.
1333 */
1334 if (memcmp(pHdr->achMagic, SSMFILEHDR_MAGIC_BASE, sizeof(SSMFILEHDR_MAGIC_BASE) - 1))
1335 {
1336 Log(("SSM: Not a saved state file. magic=%.*s\n", sizeof(pHdr->achMagic) - 1, pHdr->achMagic));
1337 return VERR_SSM_INTEGRITY_MAGIC;
1338 }
1339
1340 size_t offCrc32 = RT_OFFSETOF(SSMFILEHDR, u32CRC) + sizeof(pHdr->u32CRC);
1341 *pcbFileHdr = sizeof(*pHdr);
1342 if (!memcmp(pHdr->achMagic, SSMFILEHDR_MAGIC_V1_0, sizeof(SSMFILEHDR_MAGIC_V1_0)))
1343 {
1344 if (pHdr->MachineUuid.au32[3])
1345 {
1346 SSMFILEHDRV10X86 OldHdr;
1347 memcpy(&OldHdr, pHdr, sizeof(OldHdr));
1348 pHdr->cbFile = OldHdr.cbFile;
1349 pHdr->u32CRC = OldHdr.u32CRC;
1350 pHdr->u32Reserved = 0;
1351 pHdr->MachineUuid = OldHdr.MachineUuid;
1352 pHdr->cHostBits = 32;
1353
1354 offCrc32 = RT_OFFSETOF(SSMFILEHDRV10X86, u32CRC) + sizeof(pHdr->u32CRC);
1355 *pcbFileHdr = sizeof(OldHdr);
1356 }
1357 else
1358 {
1359 SSMFILEHDRV10AMD64 OldHdr;
1360 memcpy(&OldHdr, pHdr, sizeof(OldHdr));
1361 pHdr->cbFile = OldHdr.cbFile;
1362 pHdr->u32CRC = OldHdr.u32CRC;
1363 pHdr->u32Reserved = 0;
1364 pHdr->MachineUuid = OldHdr.MachineUuid;
1365 pHdr->cHostBits = 64;
1366
1367 offCrc32 = RT_OFFSETOF(SSMFILEHDRV10AMD64, u32CRC) + sizeof(pHdr->u32CRC);
1368 *pcbFileHdr = sizeof(OldHdr);
1369 }
1370 pHdr->u16VerMajor = 0;
1371 pHdr->u16VerMinor = 0;
1372 pHdr->u32VerBuild = 0;
1373 pHdr->u32SvnRev = 0;
1374 pHdr->cbGCPhys = sizeof(uint32_t);
1375 pHdr->cbGCPtr = sizeof(uint32_t);
1376 pHdr->au8Reserved = 0;
1377 }
1378 else if (!memcmp(pHdr->achMagic, SSMFILEHDR_MAGIC_V1_1, sizeof(SSMFILEHDR_MAGIC_V1_1)))
1379 {
1380 *pcbFileHdr = sizeof(SSMFILEHDRV11);
1381 pHdr->u16VerMajor = 0;
1382 pHdr->u16VerMinor = 0;
1383 pHdr->u32VerBuild = 0;
1384 pHdr->u32SvnRev = 0;
1385 pHdr->cHostBits = 0; /* unknown */
1386 pHdr->cbGCPhys = sizeof(RTGCPHYS);
1387 pHdr->cbGCPtr = 0; /* settable. */
1388 pHdr->au8Reserved = 0;
1389 }
1390 else if (!memcmp(pHdr->achMagic, SSMFILEHDR_MAGIC_V1_2, sizeof(pHdr->achMagic)))
1391 {
1392 if ( pHdr->u16VerMajor == 0
1393 || pHdr->u16VerMajor > 1000
1394 || pHdr->u32SvnRev == 0
1395 || pHdr->u32SvnRev > 10000000 /*100M*/)
1396 {
1397 LogRel(("SSM: Incorrect version values: %d.%d.%d.r%d\n",
1398 pHdr->u16VerMajor, pHdr->u16VerMinor, pHdr->u32VerBuild, pHdr->u32SvnRev));
1399 return VERR_SSM_INTEGRITY_VBOX_VERSION;
1400 }
1401 if ( pHdr->cHostBits != 32
1402 && pHdr->cHostBits != 64)
1403 {
1404 LogRel(("SSM: Incorrect cHostBits value: %d\n", pHdr->cHostBits));
1405 return VERR_SSM_INTEGRITY_SIZES;
1406 }
1407 if ( pHdr->cbGCPhys != sizeof(uint32_t)
1408 && pHdr->cbGCPhys != sizeof(uint64_t))
1409 {
1410 LogRel(("SSM: Incorrect cbGCPhys value: %d\n", pHdr->cbGCPhys));
1411 return VERR_SSM_INTEGRITY_SIZES;
1412 }
1413 if ( pHdr->cbGCPtr != sizeof(uint32_t)
1414 && pHdr->cbGCPtr != sizeof(uint64_t))
1415 {
1416 LogRel(("SSM: Incorrect cbGCPtr value: %d\n", pHdr->cbGCPtr));
1417 return VERR_SSM_INTEGRITY_SIZES;
1418 }
1419 }
1420 else
1421 {
1422 Log(("SSM: Unknown file format version. magic=%.*s\n", sizeof(pHdr->achMagic) - 1, pHdr->achMagic));
1423 return VERR_SSM_INTEGRITY_VERSION;
1424 }
1425
1426 /*
1427 * Verify the file size.
1428 */
1429 uint64_t cbFile;
1430 rc = RTFileGetSize(File, &cbFile);
1431 if (RT_FAILURE(rc))
1432 {
1433 Log(("SSM: Failed to get file size. rc=%Rrc\n", rc));
1434 return rc;
1435 }
1436 if (cbFile != pHdr->cbFile)
1437 {
1438 Log(("SSM: File size mismatch. hdr.cbFile=%lld actual %lld\n", pHdr->cbFile, cbFile));
1439 return VERR_SSM_INTEGRITY_SIZE;
1440 }
1441
1442 /*
1443 * Verify the checksum.
1444 */
1445 rc = RTFileSeek(File, offCrc32, RTFILE_SEEK_BEGIN, NULL);
1446 if (RT_FAILURE(rc))
1447 {
1448 Log(("SSM: Failed to seek to crc start. rc=%Rrc\n", rc));
1449 return rc;
1450 }
1451 uint32_t u32CRC;
1452 rc = ssmR3CalcChecksum(File, pHdr->cbFile - *pcbFileHdr, &u32CRC);
1453 if (RT_FAILURE(rc))
1454 return rc;
1455 if (u32CRC != pHdr->u32CRC)
1456 {
1457 Log(("SSM: Invalid CRC! Calculated %#08x, in header %#08x\n", u32CRC, pHdr->u32CRC));
1458 return VERR_SSM_INTEGRITY_CRC;
1459 }
1460
1461 /*
1462 * Verify Virtual Machine UUID.
1463 */
1464 RTUUID Uuid;
1465 memset(&Uuid, 0, sizeof(Uuid));
1466/** @todo get machine uuids CFGGetUuid(, &Uuid); */
1467 if ( RTUuidCompare(&pHdr->MachineUuid, &Uuid)
1468 && !RTUuidIsNull(&pHdr->MachineUuid)) /* temporary hack, allowing NULL uuids. */
1469 {
1470 Log(("SSM: The UUID of the saved state doesn't match the running VM.\n"));
1471 return VERR_SMM_INTEGRITY_MACHINE;
1472 }
1473
1474 return VINF_SUCCESS;
1475}
1476
1477
1478/**
1479 * Find a data unit by name.
1480 *
1481 * @returns Pointer to the unit.
1482 * @returns NULL if not found.
1483 *
1484 * @param pVM VM handle.
1485 * @param pszName Data unit name.
1486 * @param u32Instance The data unit instance id.
1487 */
1488static PSSMUNIT ssmR3Find(PVM pVM, const char *pszName, uint32_t u32Instance)
1489{
1490 size_t cchName = strlen(pszName);
1491 PSSMUNIT pUnit = pVM->ssm.s.pHead;
1492 while ( pUnit
1493 && ( pUnit->u32Instance != u32Instance
1494 || pUnit->cchName != cchName
1495 || memcmp(pUnit->szName, pszName, cchName)))
1496 pUnit = pUnit->pNext;
1497 return pUnit;
1498}
1499
1500
1501/**
1502 * Load VM save operation.
1503 *
1504 * @returns VBox status.
1505 *
1506 * @param pVM The VM handle.
1507 * @param pszFilename Name of the file to save the state in.
1508 * @param enmAfter What is planned after a successful load operation.
1509 * Only acceptable values are SSMAFTER_RESUME and SSMAFTER_DEBUG_IT.
1510 * @param pfnProgress Progress callback. Optional.
1511 * @param pvUser User argument for the progress callback.
1512 *
1513 * @thread EMT
1514 */
1515VMMR3DECL(int) SSMR3Load(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser)
1516{
1517 LogFlow(("SSMR3Load: pszFilename=%p:{%s} enmAfter=%d pfnProgress=%p pvUser=%p\n", pszFilename, pszFilename, enmAfter, pfnProgress, pvUser));
1518 VM_ASSERT_EMT(pVM);
1519
1520 /*
1521 * Validate input.
1522 */
1523 if ( enmAfter != SSMAFTER_RESUME
1524 && enmAfter != SSMAFTER_DEBUG_IT)
1525 {
1526 AssertMsgFailed(("Invalid enmAfter=%d!\n", enmAfter));
1527 return VERR_INVALID_PARAMETER;
1528 }
1529
1530 /*
1531 * Create the handle and open the file.
1532 * Note that we reserve 20% of the time on validating the image since this might
1533 * take a long time.
1534 */
1535 SSMHANDLE Handle = {0};
1536 Handle.File = NIL_RTFILE;
1537 Handle.pVM = pVM;
1538 Handle.cbFileHdr = sizeof(SSMFILEHDR);
1539 Handle.enmOp = SSMSTATE_INVALID;
1540 Handle.enmAfter = enmAfter;
1541 Handle.rc = VINF_SUCCESS;
1542 Handle.pZipComp = NULL;
1543 Handle.pZipDecomp = NULL;
1544 Handle.pfnProgress = pfnProgress;
1545 Handle.pvUser = pvUser;
1546 Handle.uPercent = 0;
1547 Handle.offEstProgress = 0;
1548 Handle.cbEstTotal = 0;
1549 Handle.offEst = 0;
1550 Handle.offEstUnitEnd = 0;
1551 Handle.uPercentPrepare = 20;
1552 Handle.uPercentDone = 2;
1553 Handle.cbGCPhys = sizeof(RTGCPHYS);
1554 Handle.cbGCPtr = sizeof(RTGCPTR);
1555 Handle.fFixedGCPtrSize = false;
1556
1557 int rc = RTFileOpen(&Handle.File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1558 if (RT_FAILURE(rc))
1559 {
1560 Log(("SSM: Failed to open save state file '%s', rc=%Rrc.\n", pszFilename, rc));
1561 return rc;
1562 }
1563
1564 /*
1565 * Read file header and validate it.
1566 */
1567 SSMFILEHDR Hdr;
1568 rc = ssmR3Validate(Handle.File, &Hdr, &Handle.cbFileHdr);
1569 if (RT_SUCCESS(rc))
1570 {
1571 if (Hdr.cbGCPhys)
1572 Handle.cbGCPhys = Hdr.cbGCPhys;
1573 if (Hdr.cbGCPtr)
1574 {
1575 Handle.cbGCPtr = Hdr.cbGCPtr;
1576 Handle.fFixedGCPtrSize = true;
1577 }
1578
1579 if (Handle.cbFileHdr == sizeof(Hdr))
1580 LogRel(("SSM: File header: Format %.4s, VirtualBox Version %u.%u.%u r%u, %u-bit host, cbGCPhys=%u, cbGCPtr=%u\n",
1581 &Hdr.achMagic[sizeof(SSMFILEHDR_MAGIC_BASE) - 1],
1582 Hdr.u16VerMajor, Hdr.u16VerMinor, Hdr.u32VerBuild, Hdr.u32SvnRev,
1583 Hdr.cHostBits, Hdr.cbGCPhys, Hdr.cbGCPtr));
1584 else
1585 LogRel(("SSM: File header: Format %.4s, %u-bit host, cbGCPhys=%u, cbGCPtr=%u\n" ,
1586 &Hdr.achMagic[sizeof(SSMFILEHDR_MAGIC_BASE)-1], Hdr.cHostBits, Hdr.cbGCPhys, Hdr.cbGCPtr));
1587
1588
1589 /*
1590 * Clear the per unit flags.
1591 */
1592 PSSMUNIT pUnit;
1593 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1594 pUnit->fCalled = false;
1595
1596 /*
1597 * Do the prepare run.
1598 */
1599 Handle.rc = VINF_SUCCESS;
1600 Handle.enmOp = SSMSTATE_LOAD_PREP;
1601 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1602 {
1603 switch (pUnit->enmType)
1604 {
1605 case SSMUNITTYPE_DEV:
1606 if (pUnit->u.Dev.pfnLoadPrep)
1607 {
1608 rc = pUnit->u.Dev.pfnLoadPrep(pUnit->u.Dev.pDevIns, &Handle);
1609 pUnit->fCalled = true;
1610 }
1611 break;
1612 case SSMUNITTYPE_DRV:
1613 if (pUnit->u.Drv.pfnLoadPrep)
1614 {
1615 rc = pUnit->u.Drv.pfnLoadPrep(pUnit->u.Drv.pDrvIns, &Handle);
1616 pUnit->fCalled = true;
1617 }
1618 break;
1619 case SSMUNITTYPE_INTERNAL:
1620 if (pUnit->u.Internal.pfnLoadPrep)
1621 {
1622 rc = pUnit->u.Internal.pfnLoadPrep(pVM, &Handle);
1623 pUnit->fCalled = true;
1624 }
1625 break;
1626 case SSMUNITTYPE_EXTERNAL:
1627 if (pUnit->u.External.pfnLoadPrep)
1628 {
1629 rc = pUnit->u.External.pfnLoadPrep(&Handle, pUnit->u.External.pvUser);
1630 pUnit->fCalled = true;
1631 }
1632 break;
1633 }
1634 if (RT_FAILURE(rc))
1635 {
1636 LogRel(("SSM: Prepare load failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
1637 break;
1638 }
1639 }
1640
1641 /* pending 2% */
1642 if (pfnProgress)
1643 pfnProgress(pVM, Handle.uPercentPrepare-1, pvUser);
1644 Handle.uPercent = Handle.uPercentPrepare;
1645 Handle.cbEstTotal = Hdr.cbFile;
1646
1647 /*
1648 * Do the execute run.
1649 */
1650 if (RT_SUCCESS(rc))
1651 rc = RTFileSeek(Handle.File, Handle.cbFileHdr, RTFILE_SEEK_BEGIN, NULL);
1652 if (RT_SUCCESS(rc))
1653 {
1654 char *pszName = NULL;
1655 size_t cchName = 0;
1656 Handle.enmOp = SSMSTATE_LOAD_EXEC;
1657 for (;;)
1658 {
1659 /*
1660 * Save the current file position and read the data unit header.
1661 */
1662 uint64_t offUnit = RTFileTell(Handle.File);
1663 SSMFILEUNITHDR UnitHdr;
1664 rc = RTFileRead(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName), NULL);
1665 if (RT_SUCCESS(rc))
1666 {
1667 /*
1668 * Check the magic and see if it's valid and whether it is a end header or not.
1669 */
1670 if (memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(SSMFILEUNITHDR_MAGIC)))
1671 {
1672 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_END, sizeof(SSMFILEUNITHDR_END)))
1673 {
1674 Log(("SSM: EndOfFile: offset %#9llx size %9d\n", offUnit, UnitHdr.cbUnit));
1675 /* Complete the progress bar (pending 99% afterwards). */
1676 Handle.offEstUnitEnd = Handle.cbEstTotal;
1677 ssmR3Progress(&Handle, Handle.cbEstTotal - Handle.offEst);
1678 break;
1679 }
1680 LogRel(("SSM: Invalid unit magic at offset %#llx (%lld), '%.*s'!\n",
1681 offUnit, offUnit, sizeof(UnitHdr.achMagic) - 1, &UnitHdr.achMagic[0]));
1682 rc = VERR_SSM_INTEGRITY_UNIT_MAGIC;
1683 break;
1684 }
1685
1686 /*
1687 * Read the name.
1688 * Adjust the name buffer first.
1689 */
1690 if (cchName < UnitHdr.cchName)
1691 {
1692 if (pszName)
1693 RTMemTmpFree(pszName);
1694 cchName = RT_ALIGN_Z(UnitHdr.cchName, 64);
1695 pszName = (char *)RTMemTmpAlloc(cchName);
1696 }
1697 if (pszName)
1698 {
1699 rc = RTFileRead(Handle.File, pszName, UnitHdr.cchName, NULL);
1700 if (RT_SUCCESS(rc))
1701 {
1702 if (!pszName[UnitHdr.cchName - 1])
1703 {
1704 Log(("SSM: Data unit: offset %#9llx size %9lld '%s'\n", offUnit, UnitHdr.cbUnit, pszName));
1705
1706 /*
1707 * Progress
1708 */
1709 Handle.offEstUnitEnd += UnitHdr.cbUnit;
1710
1711 /*
1712 * Find the data unit in our internal table.
1713 */
1714 pUnit = ssmR3Find(pVM, pszName, UnitHdr.u32Instance);
1715 if (pUnit)
1716 {
1717 /*
1718 * Call the execute handler.
1719 */
1720 Handle.cbUnitLeft = UnitHdr.cbUnit - RT_OFFSETOF(SSMFILEUNITHDR, szName[UnitHdr.cchName]);
1721 switch (pUnit->enmType)
1722 {
1723 case SSMUNITTYPE_DEV:
1724 if (pUnit->u.Dev.pfnLoadExec)
1725 {
1726 rc = pUnit->u.Dev.pfnLoadExec(pUnit->u.Dev.pDevIns, &Handle, UnitHdr.u32Version);
1727 AssertRC(rc);
1728 }
1729 else
1730 rc = VERR_SSM_NO_LOAD_EXEC;
1731 break;
1732 case SSMUNITTYPE_DRV:
1733 if (pUnit->u.Drv.pfnLoadExec)
1734 {
1735 rc = pUnit->u.Drv.pfnLoadExec(pUnit->u.Drv.pDrvIns, &Handle, UnitHdr.u32Version);
1736 AssertRC(rc);
1737 }
1738 else
1739 rc = VERR_SSM_NO_LOAD_EXEC;
1740 break;
1741 case SSMUNITTYPE_INTERNAL:
1742 if (pUnit->u.Internal.pfnLoadExec)
1743 {
1744 rc = pUnit->u.Internal.pfnLoadExec(pVM, &Handle, UnitHdr.u32Version);
1745 AssertRC(rc);
1746 }
1747 else
1748 rc = VERR_SSM_NO_LOAD_EXEC;
1749 break;
1750 case SSMUNITTYPE_EXTERNAL:
1751 if (pUnit->u.External.pfnLoadExec)
1752 {
1753 rc = pUnit->u.External.pfnLoadExec(&Handle, pUnit->u.External.pvUser, UnitHdr.u32Version);
1754 if (!rc)
1755 rc = Handle.rc;
1756 }
1757 else
1758 rc = VERR_SSM_NO_LOAD_EXEC;
1759 break;
1760 }
1761 if (rc != VERR_SSM_NO_LOAD_EXEC)
1762 {
1763 /*
1764 * Close the reader stream.
1765 */
1766 if (Handle.pZipDecomp)
1767 ssmR3ReadFinish(&Handle);
1768
1769 pUnit->fCalled = true;
1770 if (RT_SUCCESS(rc))
1771 rc = Handle.rc;
1772 if (RT_SUCCESS(rc))
1773 {
1774 /*
1775 * Now, we'll check the current position to see if all, or
1776 * more than all, the data was read.
1777 *
1778 * Note! Because of buffering / compression we'll only see the
1779 * really bad ones here.
1780 */
1781 uint64_t off = RTFileTell(Handle.File);
1782 int64_t i64Diff = off - (offUnit + UnitHdr.cbUnit);
1783 if (i64Diff < 0)
1784 {
1785 Log(("SSM: Unit '%s' left %lld bytes unread!\n", pszName, -i64Diff));
1786 rc = RTFileSeek(Handle.File, offUnit + UnitHdr.cbUnit, RTFILE_SEEK_BEGIN, NULL);
1787 }
1788 else if (i64Diff > 0)
1789 {
1790 LogRel(("SSM: Unit '%s' read %lld bytes too much!\n", pszName, i64Diff));
1791 rc = VERR_SSM_INTEGRITY;
1792 break;
1793 }
1794
1795 /* Advance the progress bar to the end of the block. */
1796 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
1797 }
1798 else
1799 {
1800 /*
1801 * We failed, but if loading for the debugger ignore certain failures
1802 * just to get it all loaded (big hack).
1803 */
1804 LogRel(("SSM: LoadExec failed with rc=%Rrc for unit '%s'!\n", rc, pszName));
1805 if ( Handle.enmAfter != SSMAFTER_DEBUG_IT
1806 || rc != VERR_SSM_LOADED_TOO_MUCH)
1807 break;
1808 Handle.rc = rc = VINF_SUCCESS;
1809 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
1810 }
1811 }
1812 else
1813 {
1814 LogRel(("SSM: No load exec callback for unit '%s'!\n", pszName));
1815 rc = VERR_SSM_INTEGRITY;
1816 break;
1817 }
1818 }
1819 else
1820 {
1821 /*
1822 * SSM unit wasn't found - ignore this when loading for the debugger.
1823 */
1824 LogRel(("SSM: Found no handler for unit '%s'!\n", pszName));
1825 rc = VERR_SSM_INTEGRITY_UNIT_NOT_FOUND;
1826 if (Handle.enmAfter != SSMAFTER_DEBUG_IT)
1827 break;
1828 rc = RTFileSeek(Handle.File, offUnit + UnitHdr.cbUnit, RTFILE_SEEK_BEGIN, NULL);
1829 }
1830 }
1831 else
1832 {
1833 LogRel(("SSM: Unit name '%.*s' was not properly terminated.\n", UnitHdr.cchName, pszName));
1834 rc = VERR_SSM_INTEGRITY;
1835 break;
1836 }
1837 }
1838 }
1839 else
1840 rc = VERR_NO_TMP_MEMORY;
1841 }
1842
1843 /*
1844 * I/O errors ends up here (yea, I know, very nice programming).
1845 */
1846 if (RT_FAILURE(rc))
1847 {
1848 LogRel(("SSM: I/O error. rc=%Rrc\n", rc));
1849 break;
1850 }
1851 }
1852 }
1853 /* (progress should be pending 99% now) */
1854 AssertMsg(RT_FAILURE(rc) || Handle.uPercent == (101-Handle.uPercentDone), ("%d\n", Handle.uPercent));
1855
1856 /*
1857 * Do the done run.
1858 */
1859 Handle.rc = rc;
1860 Handle.enmOp = SSMSTATE_LOAD_DONE;
1861 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1862 {
1863 rc = VINF_SUCCESS;
1864 switch (pUnit->enmType)
1865 {
1866 case SSMUNITTYPE_DEV:
1867 if ( pUnit->u.Dev.pfnLoadDone
1868 && ( pUnit->fCalled
1869 || (!pUnit->u.Dev.pfnLoadPrep && !pUnit->u.Dev.pfnLoadExec)))
1870 rc = pUnit->u.Dev.pfnLoadDone(pUnit->u.Dev.pDevIns, &Handle);
1871 break;
1872 case SSMUNITTYPE_DRV:
1873 if ( pUnit->u.Drv.pfnLoadDone
1874 && ( pUnit->fCalled
1875 || (!pUnit->u.Drv.pfnLoadPrep && !pUnit->u.Drv.pfnLoadExec)))
1876 rc = pUnit->u.Drv.pfnLoadDone(pUnit->u.Drv.pDrvIns, &Handle);
1877 break;
1878 case SSMUNITTYPE_INTERNAL:
1879 if (pUnit->u.Internal.pfnLoadDone
1880 && ( pUnit->fCalled
1881 || (!pUnit->u.Internal.pfnLoadPrep && !pUnit->u.Internal.pfnLoadExec)))
1882 rc = pUnit->u.Internal.pfnLoadDone(pVM, &Handle);
1883 break;
1884 case SSMUNITTYPE_EXTERNAL:
1885 if (pUnit->u.External.pfnLoadDone
1886 && ( pUnit->fCalled
1887 || (!pUnit->u.Internal.pfnLoadPrep && !pUnit->u.Internal.pfnLoadExec)))
1888 rc = pUnit->u.External.pfnLoadDone(&Handle, pUnit->u.External.pvUser);
1889 break;
1890 }
1891 if (RT_FAILURE(rc))
1892 {
1893 LogRel(("SSM: Done load failed with rc=%Rrc for data unit '%s'.\n", rc, pUnit->szName));
1894 if (RT_SUCCESS(Handle.rc))
1895 Handle.rc = rc;
1896 }
1897 }
1898 rc = Handle.rc;
1899
1900 /* progress */
1901 if (pfnProgress)
1902 pfnProgress(pVM, 99, pvUser);
1903 }
1904
1905 /*
1906 * Done
1907 */
1908 int rc2 = RTFileClose(Handle.File);
1909 AssertRC(rc2);
1910 if (RT_SUCCESS(rc))
1911 {
1912 /* progress */
1913 if (pfnProgress)
1914 pfnProgress(pVM, 100, pvUser);
1915 Log(("SSM: Load of '%s' completed!\n", pszFilename));
1916 Log(("\n\n\n"));
1917 DBGFR3InfoLog(pVM, "cpum", "verbose");
1918 DBGFR3InfoLog(pVM, "timers", NULL);
1919 DBGFR3InfoLog(pVM, "activetimers", NULL);
1920 DBGFR3InfoLog(pVM, "ioport", NULL);
1921 DBGFR3InfoLog(pVM, "mmio", NULL);
1922 DBGFR3InfoLog(pVM, "phys", NULL);
1923 Log(("\n\n\n"));
1924 }
1925 return rc;
1926}
1927
1928
1929/**
1930 * Validates a file as a validate SSM saved state.
1931 *
1932 * This will only verify the file format, the format and content of individual
1933 * data units are not inspected.
1934 *
1935 * @returns VINF_SUCCESS if valid.
1936 * @returns VBox status code on other failures.
1937 *
1938 * @param pszFilename The path to the file to validate.
1939 *
1940 * @thread Any.
1941 */
1942VMMR3DECL(int) SSMR3ValidateFile(const char *pszFilename)
1943{
1944 LogFlow(("SSMR3ValidateFile: pszFilename=%p:{%s}\n", pszFilename, pszFilename));
1945
1946 /*
1947 * Try open the file and validate it.
1948 */
1949 RTFILE File;
1950 int rc = RTFileOpen(&File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1951 if (RT_SUCCESS(rc))
1952 {
1953 size_t cbFileHdr;
1954 SSMFILEHDR Hdr;
1955 rc = ssmR3Validate(File, &Hdr, &cbFileHdr);
1956 RTFileClose(File);
1957 }
1958 else
1959 Log(("SSM: Failed to open saved state file '%s', rc=%Rrc.\n", pszFilename, rc));
1960 return rc;
1961}
1962
1963
1964/**
1965 * Opens a saved state file for reading.
1966 *
1967 * @returns VBox status code.
1968 *
1969 * @param pszFilename The path to the saved state file.
1970 * @param fFlags Open flags. Reserved, must be 0.
1971 * @param ppSSM Where to store the SSM handle.
1972 *
1973 * @thread Any.
1974 */
1975VMMR3DECL(int) SSMR3Open(const char *pszFilename, unsigned fFlags, PSSMHANDLE *ppSSM)
1976{
1977 LogFlow(("SSMR3Open: pszFilename=%p:{%s} fFlags=%#x ppSSM=%p\n", pszFilename, pszFilename, fFlags, ppSSM));
1978
1979 /*
1980 * Validate input.
1981 */
1982 AssertMsgReturn(VALID_PTR(pszFilename), ("%p\n", pszFilename), VERR_INVALID_PARAMETER);
1983 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
1984 AssertMsgReturn(VALID_PTR(ppSSM), ("%p\n", ppSSM), VERR_INVALID_PARAMETER);
1985
1986 /*
1987 * Allocate a handle.
1988 */
1989 PSSMHANDLE pSSM = (PSSMHANDLE)RTMemAllocZ(sizeof(*pSSM));
1990 AssertReturn(pSSM, VERR_NO_MEMORY);
1991
1992 /*
1993 * Try open the file and validate it.
1994 */
1995 int rc = RTFileOpen(&pSSM->File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1996 if (RT_SUCCESS(rc))
1997 {
1998 SSMFILEHDR Hdr;
1999 size_t cbFileHdr;
2000 rc = ssmR3Validate(pSSM->File, &Hdr, &cbFileHdr);
2001 if (RT_SUCCESS(rc))
2002 {
2003 //pSSM->pVM = NULL;
2004 pSSM->cbFileHdr = cbFileHdr;
2005 pSSM->enmOp = SSMSTATE_OPEN_READ;
2006 pSSM->enmAfter = SSMAFTER_OPENED;
2007 //pSSM->rc = VINF_SUCCESS;
2008 //pSSM->pZipComp = NULL;
2009 //pSSM->pZipDecomp = NULL;
2010 //pSSM->cbUnitLeft = 0;
2011 //pSSM->pfnProgress = NULL;
2012 //pSSM->pvUser = NULL;
2013 //pSSM->uPercent = 0;
2014 //pSSM->offEstProgress= 0;
2015 //pSSM->cbEstTotal = 0;
2016 //pSSM->offEst = 0;
2017 //pSSM->offEstUnitEnd = 0;
2018 pSSM->uPercentPrepare = 20;
2019 pSSM->uPercentDone = 2;
2020 pSSM->cbGCPhys = Hdr.cbGCPhys ? Hdr.cbGCPhys : sizeof(RTGCPHYS);
2021 pSSM->cbGCPtr = sizeof(RTGCPTR);
2022 pSSM->fFixedGCPtrSize = false;
2023 if (Hdr.cbGCPtr)
2024 {
2025 pSSM->cbGCPtr = Hdr.cbGCPtr;
2026 pSSM->fFixedGCPtrSize = true;
2027 }
2028
2029 *ppSSM = pSSM;
2030 LogFlow(("SSMR3Open: returns VINF_SUCCESS *ppSSM=%p\n", *ppSSM));
2031 return VINF_SUCCESS;
2032 }
2033
2034 Log(("SSMR3Open: Validation of '%s' failed, rc=%Rrc.\n", pszFilename, rc));
2035 RTFileClose(pSSM->File);
2036 }
2037 else
2038 Log(("SSMR3Open: Failed to open saved state file '%s', rc=%Rrc.\n", pszFilename, rc));
2039 RTMemFree(pSSM);
2040 return rc;
2041
2042}
2043
2044
2045/**
2046 * Closes a saved state file opened by SSMR3Open().
2047 *
2048 * @returns VBox status code.
2049 *
2050 * @param pSSM The SSM handle returned by SSMR3Open().
2051 *
2052 * @thread Any, but the caller is responsible for serializing calls per handle.
2053 */
2054VMMR3DECL(int) SSMR3Close(PSSMHANDLE pSSM)
2055{
2056 LogFlow(("SSMR3Close: pSSM=%p\n", pSSM));
2057
2058 /*
2059 * Validate input.
2060 */
2061 AssertMsgReturn(VALID_PTR(pSSM), ("%p\n", pSSM), VERR_INVALID_PARAMETER);
2062 AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
2063 AssertMsgReturn(pSSM->enmOp == SSMSTATE_OPEN_READ, ("%d\n", pSSM->enmOp), VERR_INVALID_PARAMETER);
2064
2065 /*
2066 * Close the file and free the handle.
2067 */
2068 int rc = RTFileClose(pSSM->File);
2069 AssertRC(rc);
2070 RTMemFree(pSSM);
2071 return rc;
2072}
2073
2074
2075/**
2076 * Seeks to a specific data unit.
2077 *
2078 * After seeking it's possible to use the getters to on
2079 * that data unit.
2080 *
2081 * @returns VBox status code.
2082 * @returns VERR_SSM_UNIT_NOT_FOUND if the unit+instance wasn't found.
2083 *
2084 * @param pSSM The SSM handle returned by SSMR3Open().
2085 * @param pszUnit The name of the data unit.
2086 * @param iInstance The instance number.
2087 * @param piVersion Where to store the version number. (Optional)
2088 *
2089 * @thread Any, but the caller is responsible for serializing calls per handle.
2090 */
2091VMMR3DECL(int) SSMR3Seek(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
2092{
2093 LogFlow(("SSMR3Seek: pSSM=%p pszUnit=%p:{%s} iInstance=%RU32 piVersion=%p\n",
2094 pSSM, pszUnit, pszUnit, iInstance, piVersion));
2095
2096 /*
2097 * Validate input.
2098 */
2099 AssertMsgReturn(VALID_PTR(pSSM), ("%p\n", pSSM), VERR_INVALID_PARAMETER);
2100 AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
2101 AssertMsgReturn(pSSM->enmOp == SSMSTATE_OPEN_READ, ("%d\n", pSSM->enmOp), VERR_INVALID_PARAMETER);
2102 AssertMsgReturn(VALID_PTR(pszUnit), ("%p\n", pszUnit), VERR_INVALID_POINTER);
2103 AssertMsgReturn(!piVersion || VALID_PTR(piVersion), ("%p\n", piVersion), VERR_INVALID_POINTER);
2104
2105 /*
2106 * Reset the state.
2107 */
2108 if (pSSM->pZipDecomp)
2109 {
2110 RTZipDecompDestroy(pSSM->pZipDecomp);
2111 pSSM->pZipDecomp = NULL;
2112 }
2113 pSSM->rc = VERR_SSM_UNIT_NOT_FOUND;
2114 pSSM->cbUnitLeft = 0;
2115
2116 /*
2117 * Walk the data units until we find EOF or a match.
2118 */
2119 size_t cchUnit = strlen(pszUnit) + 1;
2120 int rc = VINF_SUCCESS;
2121 char *pszName = NULL;
2122 size_t cchName = 0;
2123 SSMFILEUNITHDR UnitHdr;
2124 for (RTFOFF off = pSSM->cbFileHdr; ; off += UnitHdr.cbUnit)
2125 {
2126 /*
2127 * Read the unit header and verify it.
2128 */
2129 rc = RTFileReadAt(pSSM->File, off, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName), NULL);
2130 AssertRC(rc);
2131 if (RT_SUCCESS(rc))
2132 {
2133 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(SSMFILEUNITHDR_MAGIC)))
2134 {
2135 /*
2136 * Does it match thus far or should we just skip along?
2137 */
2138 if ( UnitHdr.u32Instance != iInstance
2139 && UnitHdr.cchName != cchUnit)
2140 continue;
2141
2142 /*
2143 * Read the name.
2144 * Adjust the name buffer first.
2145 */
2146 if (cchName < UnitHdr.cchName)
2147 {
2148 if (pszName)
2149 RTMemTmpFree(pszName);
2150 cchName = RT_ALIGN_Z(UnitHdr.cchName, 64);
2151 pszName = (char *)RTMemTmpAlloc(cchName);
2152 }
2153 rc = VERR_NO_MEMORY;
2154 if (pszName)
2155 {
2156 rc = RTFileRead(pSSM->File, pszName, UnitHdr.cchName, NULL);
2157 AssertRC(rc);
2158 if (RT_SUCCESS(rc))
2159 {
2160 if (!pszName[UnitHdr.cchName - 1])
2161 {
2162 /*
2163 * Does the name match? If not continue with the next item.
2164 */
2165 if (memcmp(pszName, pszUnit, cchUnit))
2166 continue;
2167
2168 pSSM->rc = rc = VINF_SUCCESS;
2169 pSSM->cbUnitLeft = UnitHdr.cbUnit - RT_OFFSETOF(SSMFILEUNITHDR, szName[UnitHdr.cchName]);
2170 if (piVersion)
2171 *piVersion = UnitHdr.u32Version;
2172 }
2173 else
2174 {
2175 AssertMsgFailed((" Unit name '%.*s' was not properly terminated.\n", UnitHdr.cchName, pszName));
2176 rc = VERR_SSM_INTEGRITY;
2177 }
2178 }
2179 }
2180 }
2181 else
2182 {
2183 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_END, sizeof(SSMFILEUNITHDR_END)))
2184 rc = VERR_SSM_UNIT_NOT_FOUND;
2185 else
2186 {
2187 AssertMsgFailed(("Invalid unit magic at offset %RTfoff, '%.*s'!\n",
2188 off, sizeof(UnitHdr.achMagic) - 1, &UnitHdr.achMagic[0]));
2189 rc = VERR_SSM_INTEGRITY_UNIT_MAGIC;
2190 }
2191 }
2192 }
2193
2194 /* error or success, two continue statements cover the iterating */
2195 break;
2196 }
2197
2198 RTMemFree(pszName);
2199 return rc;
2200}
2201
2202
2203/**
2204 * Finishes a data unit.
2205 * All buffers and compressor instances are flushed and destroyed.
2206 *
2207 * @returns VBox status.
2208 * @param pSSM SSM operation handle.
2209 */
2210static int ssmR3WriteFinish(PSSMHANDLE pSSM)
2211{
2212 //Log2(("ssmR3WriteFinish: %#010llx start\n", RTFileTell(pSSM->File)));
2213 if (!pSSM->pZipComp)
2214 return VINF_SUCCESS;
2215
2216 int rc = RTZipCompFinish(pSSM->pZipComp);
2217 if (RT_SUCCESS(rc))
2218 {
2219 rc = RTZipCompDestroy(pSSM->pZipComp);
2220 if (RT_SUCCESS(rc))
2221 {
2222 pSSM->pZipComp = NULL;
2223 //Log2(("ssmR3WriteFinish: %#010llx done\n", RTFileTell(pSSM->File)));
2224 return VINF_SUCCESS;
2225 }
2226 }
2227 if (RT_SUCCESS(pSSM->rc))
2228 pSSM->rc = rc;
2229 Log2(("ssmR3WriteFinish: failure rc=%Rrc\n", rc));
2230 return rc;
2231}
2232
2233
2234/**
2235 * Writes something to the current data item in the saved state file.
2236 *
2237 * @returns VBox status.
2238 * @param pSSM SSM operation handle.
2239 * @param pvBuf The bits to write.
2240 * @param cbBuf The number of bytes to write.
2241 */
2242static int ssmR3Write(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf)
2243{
2244 Log2(("ssmR3Write: pvBuf=%p cbBuf=%#x %.*Rhxs%s\n", pvBuf, cbBuf, RT_MIN(cbBuf, 128), pvBuf, cbBuf > 128 ? "..." : ""));
2245
2246 /*
2247 * Check that everything is fine.
2248 */
2249 if (RT_SUCCESS(pSSM->rc))
2250 {
2251 /*
2252 * First call starts the compression.
2253 */
2254 if (!pSSM->pZipComp)
2255 {
2256 //int rc = RTZipCompCreate(&pSSM->pZipComp, pSSM, ssmR3WriteOut, RTZIPTYPE_ZLIB, RTZIPLEVEL_FAST);
2257 int rc = RTZipCompCreate(&pSSM->pZipComp, pSSM, ssmR3WriteOut, RTZIPTYPE_LZF, RTZIPLEVEL_FAST);
2258 //int rc = RTZipCompCreate(&pSSM->pZipComp, pSSM, ssmR3WriteOut, RTZIPTYPE_STORE, RTZIPLEVEL_FAST);
2259 if (RT_FAILURE(rc))
2260 return rc;
2261 }
2262
2263 /*
2264 * Write the data item in 128kb chunks for progress indicator reasons.
2265 */
2266 while (cbBuf > 0)
2267 {
2268 size_t cbChunk = RT_MIN(cbBuf, 128*1024);
2269 pSSM->rc = RTZipCompress(pSSM->pZipComp, pvBuf, cbChunk);
2270 if (RT_FAILURE(pSSM->rc))
2271 break;
2272 ssmR3Progress(pSSM, cbChunk);
2273 cbBuf -= cbChunk;
2274 pvBuf = (char *)pvBuf + cbChunk;
2275 }
2276 }
2277
2278 return pSSM->rc;
2279}
2280
2281
2282/**
2283 * Callback for flusing the output buffer of a compression stream.
2284 *
2285 * @returns VBox status.
2286 * @param pvSSM SSM operation handle.
2287 * @param pvBuf Compressed data.
2288 * @param cbBuf Size of the compressed data.
2289 */
2290static DECLCALLBACK(int) ssmR3WriteOut(void *pvSSM, const void *pvBuf, size_t cbBuf)
2291{
2292 //Log2(("ssmR3WriteOut: %#010llx cbBuf=%#x\n", RTFileTell(((PSSMHANDLE)pvSSM)->File), cbBuf));
2293 int rc = RTFileWrite(((PSSMHANDLE)pvSSM)->File, pvBuf, cbBuf, NULL);
2294 if (RT_SUCCESS(rc))
2295 return rc;
2296 Log(("ssmR3WriteOut: RTFileWrite(,,%d) -> %d\n", cbBuf, rc));
2297 return rc;
2298}
2299
2300
2301/**
2302 * Puts a structure.
2303 *
2304 * @returns VBox status code.
2305 * @param pSSM The saved state handle.
2306 * @param pvStruct The structure address.
2307 * @param paFields The array of structure fields descriptions.
2308 * The array must be terminated by a SSMFIELD_ENTRY_TERM().
2309 */
2310VMMR3DECL(int) SSMR3PutStruct(PSSMHANDLE pSSM, const void *pvStruct, PCSSMFIELD paFields)
2311{
2312 /* begin marker. */
2313 int rc = SSMR3PutU32(pSSM, SSMR3STRUCT_BEGIN);
2314 if (RT_FAILURE(rc))
2315 return rc;
2316
2317 /* put the fields */
2318 for (PCSSMFIELD pCur = paFields;
2319 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
2320 pCur++)
2321 {
2322 rc = ssmR3Write(pSSM, (uint8_t *)pvStruct + pCur->off, pCur->cb);
2323 if (RT_FAILURE(rc))
2324 return rc;
2325 }
2326
2327 /* end marker */
2328 return SSMR3PutU32(pSSM, SSMR3STRUCT_END);
2329}
2330
2331
2332/**
2333 * Saves a boolean item to the current data unit.
2334 *
2335 * @returns VBox status.
2336 * @param pSSM SSM operation handle.
2337 * @param fBool Item to save.
2338 */
2339VMMR3DECL(int) SSMR3PutBool(PSSMHANDLE pSSM, bool fBool)
2340{
2341 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2342 {
2343 uint8_t u8 = fBool; /* enforce 1 byte size */
2344 return ssmR3Write(pSSM, &u8, sizeof(u8));
2345 }
2346 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2347 return VERR_SSM_INVALID_STATE;
2348}
2349
2350
2351/**
2352 * Saves a 8-bit unsigned integer item to the current data unit.
2353 *
2354 * @returns VBox status.
2355 * @param pSSM SSM operation handle.
2356 * @param u8 Item to save.
2357 */
2358VMMR3DECL(int) SSMR3PutU8(PSSMHANDLE pSSM, uint8_t u8)
2359{
2360 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2361 return ssmR3Write(pSSM, &u8, sizeof(u8));
2362 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2363 return VERR_SSM_INVALID_STATE;
2364}
2365
2366
2367/**
2368 * Saves a 8-bit signed integer item to the current data unit.
2369 *
2370 * @returns VBox status.
2371 * @param pSSM SSM operation handle.
2372 * @param i8 Item to save.
2373 */
2374VMMR3DECL(int) SSMR3PutS8(PSSMHANDLE pSSM, int8_t i8)
2375{
2376 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2377 return ssmR3Write(pSSM, &i8, sizeof(i8));
2378 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2379 return VERR_SSM_INVALID_STATE;
2380}
2381
2382
2383/**
2384 * Saves a 16-bit unsigned integer item to the current data unit.
2385 *
2386 * @returns VBox status.
2387 * @param pSSM SSM operation handle.
2388 * @param u16 Item to save.
2389 */
2390VMMR3DECL(int) SSMR3PutU16(PSSMHANDLE pSSM, uint16_t u16)
2391{
2392 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2393 return ssmR3Write(pSSM, &u16, sizeof(u16));
2394 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2395 return VERR_SSM_INVALID_STATE;
2396}
2397
2398
2399/**
2400 * Saves a 16-bit signed integer item to the current data unit.
2401 *
2402 * @returns VBox status.
2403 * @param pSSM SSM operation handle.
2404 * @param i16 Item to save.
2405 */
2406VMMR3DECL(int) SSMR3PutS16(PSSMHANDLE pSSM, int16_t i16)
2407{
2408 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2409 return ssmR3Write(pSSM, &i16, sizeof(i16));
2410 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2411 return VERR_SSM_INVALID_STATE;
2412}
2413
2414
2415/**
2416 * Saves a 32-bit unsigned integer item to the current data unit.
2417 *
2418 * @returns VBox status.
2419 * @param pSSM SSM operation handle.
2420 * @param u32 Item to save.
2421 */
2422VMMR3DECL(int) SSMR3PutU32(PSSMHANDLE pSSM, uint32_t u32)
2423{
2424 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2425 return ssmR3Write(pSSM, &u32, sizeof(u32));
2426 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2427 return VERR_SSM_INVALID_STATE;
2428}
2429
2430
2431/**
2432 * Saves a 32-bit signed integer item to the current data unit.
2433 *
2434 * @returns VBox status.
2435 * @param pSSM SSM operation handle.
2436 * @param i32 Item to save.
2437 */
2438VMMR3DECL(int) SSMR3PutS32(PSSMHANDLE pSSM, int32_t i32)
2439{
2440 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2441 return ssmR3Write(pSSM, &i32, sizeof(i32));
2442 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2443 return VERR_SSM_INVALID_STATE;
2444}
2445
2446
2447/**
2448 * Saves a 64-bit unsigned integer item to the current data unit.
2449 *
2450 * @returns VBox status.
2451 * @param pSSM SSM operation handle.
2452 * @param u64 Item to save.
2453 */
2454VMMR3DECL(int) SSMR3PutU64(PSSMHANDLE pSSM, uint64_t u64)
2455{
2456 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2457 return ssmR3Write(pSSM, &u64, sizeof(u64));
2458 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2459 return VERR_SSM_INVALID_STATE;
2460}
2461
2462
2463/**
2464 * Saves a 64-bit signed integer item to the current data unit.
2465 *
2466 * @returns VBox status.
2467 * @param pSSM SSM operation handle.
2468 * @param i64 Item to save.
2469 */
2470VMMR3DECL(int) SSMR3PutS64(PSSMHANDLE pSSM, int64_t i64)
2471{
2472 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2473 return ssmR3Write(pSSM, &i64, sizeof(i64));
2474 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2475 return VERR_SSM_INVALID_STATE;
2476}
2477
2478
2479/**
2480 * Saves a 128-bit unsigned integer item to the current data unit.
2481 *
2482 * @returns VBox status.
2483 * @param pSSM SSM operation handle.
2484 * @param u128 Item to save.
2485 */
2486VMMR3DECL(int) SSMR3PutU128(PSSMHANDLE pSSM, uint128_t u128)
2487{
2488 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2489 return ssmR3Write(pSSM, &u128, sizeof(u128));
2490 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2491 return VERR_SSM_INVALID_STATE;
2492}
2493
2494
2495/**
2496 * Saves a 128-bit signed integer item to the current data unit.
2497 *
2498 * @returns VBox status.
2499 * @param pSSM SSM operation handle.
2500 * @param i128 Item to save.
2501 */
2502VMMR3DECL(int) SSMR3PutS128(PSSMHANDLE pSSM, int128_t i128)
2503{
2504 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2505 return ssmR3Write(pSSM, &i128, sizeof(i128));
2506 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2507 return VERR_SSM_INVALID_STATE;
2508}
2509
2510
2511/**
2512 * Saves a VBox unsigned integer item to the current data unit.
2513 *
2514 * @returns VBox status.
2515 * @param pSSM SSM operation handle.
2516 * @param u Item to save.
2517 */
2518VMMR3DECL(int) SSMR3PutUInt(PSSMHANDLE pSSM, RTUINT u)
2519{
2520 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2521 return ssmR3Write(pSSM, &u, sizeof(u));
2522 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2523 return VERR_SSM_INVALID_STATE;
2524}
2525
2526
2527/**
2528 * Saves a VBox signed integer item to the current data unit.
2529 *
2530 * @returns VBox status.
2531 * @param pSSM SSM operation handle.
2532 * @param i Item to save.
2533 */
2534VMMR3DECL(int) SSMR3PutSInt(PSSMHANDLE pSSM, RTINT i)
2535{
2536 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2537 return ssmR3Write(pSSM, &i, sizeof(i));
2538 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2539 return VERR_SSM_INVALID_STATE;
2540}
2541
2542
2543/**
2544 * Saves a GC natural unsigned integer item to the current data unit.
2545 *
2546 * @returns VBox status.
2547 * @param pSSM SSM operation handle.
2548 * @param u Item to save.
2549 *
2550 * @deprecated Silly type, don't use it.
2551 */
2552VMMR3DECL(int) SSMR3PutGCUInt(PSSMHANDLE pSSM, RTGCUINT u)
2553{
2554 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2555 return ssmR3Write(pSSM, &u, sizeof(u));
2556 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2557 return VERR_SSM_INVALID_STATE;
2558}
2559
2560
2561/**
2562 * Saves a GC unsigned integer register item to the current data unit.
2563 *
2564 * @returns VBox status.
2565 * @param pSSM SSM operation handle.
2566 * @param u Item to save.
2567 */
2568VMMR3DECL(int) SSMR3PutGCUIntReg(PSSMHANDLE pSSM, RTGCUINTREG u)
2569{
2570 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2571 return ssmR3Write(pSSM, &u, sizeof(u));
2572 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2573 return VERR_SSM_INVALID_STATE;
2574}
2575
2576
2577/**
2578 * Saves a 32 bits GC physical address item to the current data unit.
2579 *
2580 * @returns VBox status.
2581 * @param pSSM SSM operation handle.
2582 * @param GCPhys The item to save
2583 */
2584VMMR3DECL(int) SSMR3PutGCPhys32(PSSMHANDLE pSSM, RTGCPHYS32 GCPhys)
2585{
2586 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2587 return ssmR3Write(pSSM, &GCPhys, sizeof(GCPhys));
2588 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2589 return VERR_SSM_INVALID_STATE;
2590}
2591
2592
2593/**
2594 * Saves a 64 bits GC physical address item to the current data unit.
2595 *
2596 * @returns VBox status.
2597 * @param pSSM SSM operation handle.
2598 * @param GCPhys The item to save
2599 */
2600VMMR3DECL(int) SSMR3PutGCPhys64(PSSMHANDLE pSSM, RTGCPHYS64 GCPhys)
2601{
2602 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2603 return ssmR3Write(pSSM, &GCPhys, sizeof(GCPhys));
2604 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2605 return VERR_SSM_INVALID_STATE;
2606}
2607
2608
2609/**
2610 * Saves a GC physical address item to the current data unit.
2611 *
2612 * @returns VBox status.
2613 * @param pSSM SSM operation handle.
2614 * @param GCPhys The item to save
2615 */
2616VMMR3DECL(int) SSMR3PutGCPhys(PSSMHANDLE pSSM, RTGCPHYS GCPhys)
2617{
2618 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2619 return ssmR3Write(pSSM, &GCPhys, sizeof(GCPhys));
2620 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2621 return VERR_SSM_INVALID_STATE;
2622}
2623
2624
2625/**
2626 * Saves a GC virtual address item to the current data unit.
2627 *
2628 * @returns VBox status.
2629 * @param pSSM SSM operation handle.
2630 * @param GCPtr The item to save.
2631 */
2632VMMR3DECL(int) SSMR3PutGCPtr(PSSMHANDLE pSSM, RTGCPTR GCPtr)
2633{
2634 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2635 return ssmR3Write(pSSM, &GCPtr, sizeof(GCPtr));
2636 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2637 return VERR_SSM_INVALID_STATE;
2638}
2639
2640
2641/**
2642 * Saves an RC virtual address item to the current data unit.
2643 *
2644 * @returns VBox status.
2645 * @param pSSM SSM operation handle.
2646 * @param RCPtr The item to save.
2647 */
2648VMMR3DECL(int) SSMR3PutRCPtr(PSSMHANDLE pSSM, RTRCPTR RCPtr)
2649{
2650 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2651 return ssmR3Write(pSSM, &RCPtr, sizeof(RCPtr));
2652 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2653 return VERR_SSM_INVALID_STATE;
2654}
2655
2656
2657/**
2658 * Saves a GC virtual address (represented as an unsigned integer) item to the current data unit.
2659 *
2660 * @returns VBox status.
2661 * @param pSSM SSM operation handle.
2662 * @param GCPtr The item to save.
2663 */
2664VMMR3DECL(int) SSMR3PutGCUIntPtr(PSSMHANDLE pSSM, RTGCUINTPTR GCPtr)
2665{
2666 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2667 return ssmR3Write(pSSM, &GCPtr, sizeof(GCPtr));
2668 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2669 return VERR_SSM_INVALID_STATE;
2670}
2671
2672
2673/**
2674 * Saves a I/O port address item to the current data unit.
2675 *
2676 * @returns VBox status.
2677 * @param pSSM SSM operation handle.
2678 * @param IOPort The item to save.
2679 */
2680VMMR3DECL(int) SSMR3PutIOPort(PSSMHANDLE pSSM, RTIOPORT IOPort)
2681{
2682 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2683 return ssmR3Write(pSSM, &IOPort, sizeof(IOPort));
2684 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2685 return VERR_SSM_INVALID_STATE;
2686}
2687
2688
2689/**
2690 * Saves a selector item to the current data unit.
2691 *
2692 * @returns VBox status.
2693 * @param pSSM SSM operation handle.
2694 * @param Sel The item to save.
2695 */
2696VMMR3DECL(int) SSMR3PutSel(PSSMHANDLE pSSM, RTSEL Sel)
2697{
2698 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2699 return ssmR3Write(pSSM, &Sel, sizeof(Sel));
2700 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2701 return VERR_SSM_INVALID_STATE;
2702}
2703
2704
2705/**
2706 * Saves a memory item to the current data unit.
2707 *
2708 * @returns VBox status.
2709 * @param pSSM SSM operation handle.
2710 * @param pv Item to save.
2711 * @param cb Size of the item.
2712 */
2713VMMR3DECL(int) SSMR3PutMem(PSSMHANDLE pSSM, const void *pv, size_t cb)
2714{
2715 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2716 return ssmR3Write(pSSM, pv, cb);
2717 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2718 return VERR_SSM_INVALID_STATE;
2719}
2720
2721
2722/**
2723 * Saves a zero terminated string item to the current data unit.
2724 *
2725 * @returns VBox status.
2726 * @param pSSM SSM operation handle.
2727 * @param psz Item to save.
2728 */
2729VMMR3DECL(int) SSMR3PutStrZ(PSSMHANDLE pSSM, const char *psz)
2730{
2731 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2732 {
2733 size_t cch = strlen(psz);
2734 if (cch > 1024*1024)
2735 {
2736 AssertMsgFailed(("a %d byte long string, what's this!?!\n"));
2737 return VERR_TOO_MUCH_DATA;
2738 }
2739 uint32_t u32 = (uint32_t)cch;
2740 int rc = ssmR3Write(pSSM, &u32, sizeof(u32));
2741 if (rc)
2742 return rc;
2743 return ssmR3Write(pSSM, psz, cch);
2744 }
2745 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2746 return VERR_SSM_INVALID_STATE;
2747}
2748
2749
2750
2751
2752
2753/**
2754 * Closes the decompressor of a data unit.
2755 *
2756 * @param pSSM SSM operation handle.
2757 */
2758static void ssmR3ReadFinish(PSSMHANDLE pSSM)
2759{
2760 int rc = RTZipDecompDestroy(pSSM->pZipDecomp);
2761 AssertRC(rc);
2762 pSSM->pZipDecomp = NULL;
2763}
2764
2765
2766/**
2767 * Internal read worker.
2768 *
2769 * @param pSSM SSM operation handle.
2770 * @param pvBuf Where to store the read data.
2771 * @param cbBuf Number of bytes to read.
2772 */
2773static int ssmR3Read(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf)
2774{
2775 /*
2776 * Check that everything is fine.
2777 */
2778 if (RT_SUCCESS(pSSM->rc))
2779 {
2780 /*
2781 * Open the decompressor on the first read.
2782 */
2783 if (!pSSM->pZipDecomp)
2784 {
2785 pSSM->rc = RTZipDecompCreate(&pSSM->pZipDecomp, pSSM, ssmR3ReadIn);
2786 if (RT_FAILURE(pSSM->rc))
2787 return pSSM->rc;
2788 }
2789
2790 /*
2791 * Do the requested read.
2792 * Use 32kb chunks to work the progress indicator.
2793 */
2794 pSSM->rc = RTZipDecompress(pSSM->pZipDecomp, pvBuf, cbBuf, NULL);
2795 if (RT_SUCCESS(pSSM->rc))
2796 Log2(("ssmR3Read: pvBuf=%p cbBuf=%#x %.*Rhxs%s\n", pvBuf, cbBuf, RT_MIN(cbBuf, 128), pvBuf, cbBuf > 128 ? "..." : ""));
2797 else
2798 AssertMsgFailed(("rc=%Rrc cbBuf=%#x\n", pSSM->rc, cbBuf));
2799 }
2800
2801 return pSSM->rc;
2802}
2803
2804
2805/**
2806 * Callback for reading compressed data into the input buffer of the
2807 * decompressor.
2808 *
2809 * @returns VBox status code.
2810 * @param pvSSM The SSM handle.
2811 * @param pvBuf Where to store the compressed data.
2812 * @param cbBuf Size of the buffer.
2813 * @param pcbRead Number of bytes actually stored in the buffer.
2814 */
2815static DECLCALLBACK(int) ssmR3ReadIn(void *pvSSM, void *pvBuf, size_t cbBuf, size_t *pcbRead)
2816{
2817 PSSMHANDLE pSSM = (PSSMHANDLE)pvSSM;
2818 size_t cbRead = cbBuf;
2819 if (pSSM->cbUnitLeft < cbBuf)
2820 cbRead = (size_t)pSSM->cbUnitLeft;
2821 if (cbRead)
2822 {
2823 //Log2(("ssmR3ReadIn: %#010llx cbBug=%#x cbRead=%#x\n", RTFileTell(pSSM->File), cbBuf, cbRead));
2824 int rc = RTFileRead(pSSM->File, pvBuf, cbRead, NULL);
2825 if (RT_SUCCESS(rc))
2826 {
2827 pSSM->cbUnitLeft -= cbRead;
2828 if (pcbRead)
2829 *pcbRead = cbRead;
2830 ssmR3Progress(pSSM, cbRead);
2831 return VINF_SUCCESS;
2832 }
2833 Log(("ssmR3ReadIn: RTFileRead(,,%d) -> %d\n", cbRead, rc));
2834 return rc;
2835 }
2836
2837 /** @todo weed out lazy saving */
2838 if (pSSM->enmAfter != SSMAFTER_DEBUG_IT)
2839 AssertMsgFailed(("SSM: attempted reading more than the unit!\n"));
2840 return VERR_SSM_LOADED_TOO_MUCH;
2841}
2842
2843
2844/**
2845 * Gets a structure.
2846 *
2847 * @returns VBox status code.
2848 * @param pSSM The saved state handle.
2849 * @param pvStruct The structure address.
2850 * @param paFields The array of structure fields descriptions.
2851 * The array must be terminated by a SSMFIELD_ENTRY_TERM().
2852 */
2853VMMR3DECL(int) SSMR3GetStruct(PSSMHANDLE pSSM, void *pvStruct, PCSSMFIELD paFields)
2854{
2855 /* begin marker. */
2856 uint32_t u32Magic;
2857 int rc = SSMR3GetU32(pSSM, &u32Magic);
2858 if (RT_FAILURE(rc))
2859 return rc;
2860 if (u32Magic != SSMR3STRUCT_BEGIN)
2861 AssertMsgFailedReturn(("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
2862
2863 /* put the fields */
2864 for (PCSSMFIELD pCur = paFields;
2865 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
2866 pCur++)
2867 {
2868 rc = ssmR3Read(pSSM, (uint8_t *)pvStruct + pCur->off, pCur->cb);
2869 if (RT_FAILURE(rc))
2870 return rc;
2871 }
2872
2873 /* end marker */
2874 rc = SSMR3GetU32(pSSM, &u32Magic);
2875 if (RT_FAILURE(rc))
2876 return rc;
2877 if (u32Magic != SSMR3STRUCT_END)
2878 AssertMsgFailedReturn(("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
2879 return rc;
2880}
2881
2882
2883/**
2884 * Loads a boolean item from the current data unit.
2885 *
2886 * @returns VBox status.
2887 * @param pSSM SSM operation handle.
2888 * @param pfBool Where to store the item.
2889 */
2890VMMR3DECL(int) SSMR3GetBool(PSSMHANDLE pSSM, bool *pfBool)
2891{
2892 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2893 {
2894 uint8_t u8; /* see SSMR3PutBool */
2895 int rc = ssmR3Read(pSSM, &u8, sizeof(u8));
2896 if (RT_SUCCESS(rc))
2897 {
2898 Assert(u8 <= 1);
2899 *pfBool = !!u8;
2900 }
2901 return rc;
2902 }
2903 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2904 return VERR_SSM_INVALID_STATE;
2905}
2906
2907
2908/**
2909 * Loads a 8-bit unsigned integer item from the current data unit.
2910 *
2911 * @returns VBox status.
2912 * @param pSSM SSM operation handle.
2913 * @param pu8 Where to store the item.
2914 */
2915VMMR3DECL(int) SSMR3GetU8(PSSMHANDLE pSSM, uint8_t *pu8)
2916{
2917 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2918 return ssmR3Read(pSSM, pu8, sizeof(*pu8));
2919 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2920 return VERR_SSM_INVALID_STATE;
2921}
2922
2923
2924/**
2925 * Loads a 8-bit signed integer item from the current data unit.
2926 *
2927 * @returns VBox status.
2928 * @param pSSM SSM operation handle.
2929 * @param pi8 Where to store the item.
2930 */
2931VMMR3DECL(int) SSMR3GetS8(PSSMHANDLE pSSM, int8_t *pi8)
2932{
2933 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2934 return ssmR3Read(pSSM, pi8, sizeof(*pi8));
2935 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2936 return VERR_SSM_INVALID_STATE;
2937}
2938
2939
2940/**
2941 * Loads a 16-bit unsigned integer item from the current data unit.
2942 *
2943 * @returns VBox status.
2944 * @param pSSM SSM operation handle.
2945 * @param pu16 Where to store the item.
2946 */
2947VMMR3DECL(int) SSMR3GetU16(PSSMHANDLE pSSM, uint16_t *pu16)
2948{
2949 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2950 return ssmR3Read(pSSM, pu16, sizeof(*pu16));
2951 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2952 return VERR_SSM_INVALID_STATE;
2953}
2954
2955
2956/**
2957 * Loads a 16-bit signed integer item from the current data unit.
2958 *
2959 * @returns VBox status.
2960 * @param pSSM SSM operation handle.
2961 * @param pi16 Where to store the item.
2962 */
2963VMMR3DECL(int) SSMR3GetS16(PSSMHANDLE pSSM, int16_t *pi16)
2964{
2965 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2966 return ssmR3Read(pSSM, pi16, sizeof(*pi16));
2967 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2968 return VERR_SSM_INVALID_STATE;
2969}
2970
2971
2972/**
2973 * Loads a 32-bit unsigned integer item from the current data unit.
2974 *
2975 * @returns VBox status.
2976 * @param pSSM SSM operation handle.
2977 * @param pu32 Where to store the item.
2978 */
2979VMMR3DECL(int) SSMR3GetU32(PSSMHANDLE pSSM, uint32_t *pu32)
2980{
2981 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2982 return ssmR3Read(pSSM, pu32, sizeof(*pu32));
2983 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2984 return VERR_SSM_INVALID_STATE;
2985}
2986
2987
2988/**
2989 * Loads a 32-bit signed integer item from the current data unit.
2990 *
2991 * @returns VBox status.
2992 * @param pSSM SSM operation handle.
2993 * @param pi32 Where to store the item.
2994 */
2995VMMR3DECL(int) SSMR3GetS32(PSSMHANDLE pSSM, int32_t *pi32)
2996{
2997 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2998 return ssmR3Read(pSSM, pi32, sizeof(*pi32));
2999 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3000 return VERR_SSM_INVALID_STATE;
3001}
3002
3003
3004/**
3005 * Loads a 64-bit unsigned integer item from the current data unit.
3006 *
3007 * @returns VBox status.
3008 * @param pSSM SSM operation handle.
3009 * @param pu64 Where to store the item.
3010 */
3011VMMR3DECL(int) SSMR3GetU64(PSSMHANDLE pSSM, uint64_t *pu64)
3012{
3013 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3014 return ssmR3Read(pSSM, pu64, sizeof(*pu64));
3015 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3016 return VERR_SSM_INVALID_STATE;
3017}
3018
3019
3020/**
3021 * Loads a 64-bit signed integer item from the current data unit.
3022 *
3023 * @returns VBox status.
3024 * @param pSSM SSM operation handle.
3025 * @param pi64 Where to store the item.
3026 */
3027VMMR3DECL(int) SSMR3GetS64(PSSMHANDLE pSSM, int64_t *pi64)
3028{
3029 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3030 return ssmR3Read(pSSM, pi64, sizeof(*pi64));
3031 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3032 return VERR_SSM_INVALID_STATE;
3033}
3034
3035
3036/**
3037 * Loads a 128-bit unsigned integer item from the current data unit.
3038 *
3039 * @returns VBox status.
3040 * @param pSSM SSM operation handle.
3041 * @param pu128 Where to store the item.
3042 */
3043VMMR3DECL(int) SSMR3GetU128(PSSMHANDLE pSSM, uint128_t *pu128)
3044{
3045 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3046 return ssmR3Read(pSSM, pu128, sizeof(*pu128));
3047 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3048 return VERR_SSM_INVALID_STATE;
3049}
3050
3051
3052/**
3053 * Loads a 128-bit signed integer item from the current data unit.
3054 *
3055 * @returns VBox status.
3056 * @param pSSM SSM operation handle.
3057 * @param pi128 Where to store the item.
3058 */
3059VMMR3DECL(int) SSMR3GetS128(PSSMHANDLE pSSM, int128_t *pi128)
3060{
3061 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3062 return ssmR3Read(pSSM, pi128, sizeof(*pi128));
3063 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3064 return VERR_SSM_INVALID_STATE;
3065}
3066
3067
3068/**
3069 * Loads a VBox unsigned integer item from the current data unit.
3070 *
3071 * @returns VBox status.
3072 * @param pSSM SSM operation handle.
3073 * @param pu Where to store the integer.
3074 */
3075VMMR3DECL(int) SSMR3GetUInt(PSSMHANDLE pSSM, PRTUINT pu)
3076{
3077 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3078 return ssmR3Read(pSSM, pu, sizeof(*pu));
3079 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3080 return VERR_SSM_INVALID_STATE;
3081}
3082
3083
3084/**
3085 * Loads a VBox signed integer item from the current data unit.
3086 *
3087 * @returns VBox status.
3088 * @param pSSM SSM operation handle.
3089 * @param pi Where to store the integer.
3090 */
3091VMMR3DECL(int) SSMR3GetSInt(PSSMHANDLE pSSM, PRTINT pi)
3092{
3093 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3094 return ssmR3Read(pSSM, pi, sizeof(*pi));
3095 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3096 return VERR_SSM_INVALID_STATE;
3097}
3098
3099
3100/**
3101 * Loads a GC natural unsigned integer item from the current data unit.
3102 *
3103 * @returns VBox status.
3104 * @param pSSM SSM operation handle.
3105 * @param pu Where to store the integer.
3106 *
3107 * @deprecated Silly type with an incorrect size, don't use it.
3108 */
3109VMMR3DECL(int) SSMR3GetGCUInt(PSSMHANDLE pSSM, PRTGCUINT pu)
3110{
3111 AssertCompile(sizeof(RTGCPTR) == sizeof(*pu));
3112 return SSMR3GetGCPtr(pSSM, (PRTGCPTR)pu);
3113}
3114
3115
3116/**
3117 * Loads a GC unsigned integer register item from the current data unit.
3118 *
3119 * @returns VBox status.
3120 * @param pSSM SSM operation handle.
3121 * @param pu Where to store the integer.
3122 */
3123VMMR3DECL(int) SSMR3GetGCUIntReg(PSSMHANDLE pSSM, PRTGCUINTREG pu)
3124{
3125 AssertCompile(sizeof(RTGCPTR) == sizeof(*pu));
3126 return SSMR3GetGCPtr(pSSM, (PRTGCPTR)pu);
3127}
3128
3129
3130/**
3131 * Loads a 32 bits GC physical address item from the current data unit.
3132 *
3133 * @returns VBox status.
3134 * @param pSSM SSM operation handle.
3135 * @param pGCPhys Where to store the GC physical address.
3136 */
3137VMMR3DECL(int) SSMR3GetGCPhys32(PSSMHANDLE pSSM, PRTGCPHYS32 pGCPhys)
3138{
3139 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3140 return ssmR3Read(pSSM, pGCPhys, sizeof(*pGCPhys));
3141 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3142 return VERR_SSM_INVALID_STATE;
3143}
3144
3145
3146/**
3147 * Loads a 64 bits GC physical address item from the current data unit.
3148 *
3149 * @returns VBox status.
3150 * @param pSSM SSM operation handle.
3151 * @param pGCPhys Where to store the GC physical address.
3152 */
3153VMMR3DECL(int) SSMR3GetGCPhys64(PSSMHANDLE pSSM, PRTGCPHYS64 pGCPhys)
3154{
3155 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3156 return ssmR3Read(pSSM, pGCPhys, sizeof(*pGCPhys));
3157 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3158 return VERR_SSM_INVALID_STATE;
3159}
3160
3161
3162/**
3163 * Loads a GC physical address item from the current data unit.
3164 *
3165 * @returns VBox status.
3166 * @param pSSM SSM operation handle.
3167 * @param pGCPhys Where to store the GC physical address.
3168 */
3169VMMR3DECL(int) SSMR3GetGCPhys(PSSMHANDLE pSSM, PRTGCPHYS pGCPhys)
3170{
3171 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3172 {
3173 if (sizeof(*pGCPhys) != pSSM->cbGCPhys)
3174 {
3175 Assert(sizeof(*pGCPhys) == sizeof(uint64_t) || sizeof(*pGCPhys) == sizeof(uint32_t));
3176 Assert(pSSM->cbGCPhys == sizeof(uint64_t) || pSSM->cbGCPhys == sizeof(uint32_t));
3177 if (pSSM->cbGCPhys == sizeof(uint64_t))
3178 {
3179 /* 64-bit saved, 32-bit load: try truncate it. */
3180 uint64_t u64;
3181 int rc = ssmR3Read(pSSM, &u64, pSSM->cbGCPhys);
3182 if (RT_FAILURE(rc))
3183 return rc;
3184 if (u64 >= _4G)
3185 return VERR_SSM_GCPHYS_OVERFLOW;
3186 *pGCPhys = (RTGCPHYS)u64;
3187 return rc;
3188 }
3189 /* 32-bit saved, 64-bit load: clear the high part. */
3190 *pGCPhys = 0;
3191 }
3192 return ssmR3Read(pSSM, pGCPhys, pSSM->cbGCPhys);
3193 }
3194 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3195 return VERR_SSM_INVALID_STATE;
3196}
3197
3198
3199/**
3200 * Loads a GC virtual address item from the current data unit.
3201 *
3202 * Only applies to in the 1.1 format:
3203 * - SSMR3GetGCPtr
3204 * - SSMR3GetGCUIntPtr
3205 * - SSMR3GetGCUInt
3206 * - SSMR3GetGCUIntReg
3207 *
3208 * Put functions are not affected.
3209 *
3210 * @returns VBox status.
3211 * @param pSSM SSM operation handle.
3212 * @param cbGCPtr Size of RTGCPTR
3213 *
3214 * @remarks This interface only works with saved state version 1.1, if the
3215 * format isn't 1.1 the call will be ignored.
3216 */
3217VMMR3DECL(int) SSMR3SetGCPtrSize(PSSMHANDLE pSSM, unsigned cbGCPtr)
3218{
3219 Assert(cbGCPtr == sizeof(RTGCPTR32) || cbGCPtr == sizeof(RTGCPTR64));
3220 if (!pSSM->fFixedGCPtrSize)
3221 {
3222 Log(("SSMR3SetGCPtrSize: %d -> %d bytes\n", pSSM->cbGCPtr, cbGCPtr));
3223 pSSM->cbGCPtr = cbGCPtr;
3224 pSSM->fFixedGCPtrSize = true;
3225 }
3226 else if ( pSSM->cbGCPtr != cbGCPtr
3227 && pSSM->cbFileHdr == sizeof(SSMFILEHDRV11))
3228 AssertMsgFailed(("SSMR3SetGCPtrSize: already fixed at %d bytes; requested %d bytes\n", pSSM->cbGCPtr, cbGCPtr));
3229
3230 return VINF_SUCCESS;
3231}
3232
3233
3234/**
3235 * Loads a GC virtual address item from the current data unit.
3236 *
3237 * @returns VBox status.
3238 * @param pSSM SSM operation handle.
3239 * @param pGCPtr Where to store the GC virtual address.
3240 */
3241VMMR3DECL(int) SSMR3GetGCPtr(PSSMHANDLE pSSM, PRTGCPTR pGCPtr)
3242{
3243 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3244 {
3245 if (sizeof(*pGCPtr) != pSSM->cbGCPtr)
3246 {
3247 Assert(sizeof(*pGCPtr) == sizeof(uint64_t) || sizeof(*pGCPtr) == sizeof(uint32_t));
3248 Assert(pSSM->cbGCPtr == sizeof(uint64_t) || pSSM->cbGCPtr == sizeof(uint32_t));
3249 if (pSSM->cbGCPtr == sizeof(uint64_t))
3250 {
3251 /* 64-bit saved, 32-bit load: try truncate it. */
3252 uint64_t u64;
3253 int rc = ssmR3Read(pSSM, &u64, pSSM->cbGCPhys);
3254 if (RT_FAILURE(rc))
3255 return rc;
3256 if (u64 >= _4G)
3257 return VERR_SSM_GCPTR_OVERFLOW;
3258 *pGCPtr = (RTGCPTR)u64;
3259 return rc;
3260 }
3261 /* 32-bit saved, 64-bit load: clear the high part. */
3262 *pGCPtr = 0;
3263 }
3264 return ssmR3Read(pSSM, pGCPtr, pSSM->cbGCPtr);
3265 }
3266 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3267 return VERR_SSM_INVALID_STATE;
3268}
3269
3270
3271/**
3272 * Loads a GC virtual address (represented as unsigned integer) item from the current data unit.
3273 *
3274 * @returns VBox status.
3275 * @param pSSM SSM operation handle.
3276 * @param pGCPtr Where to store the GC virtual address.
3277 */
3278VMMR3DECL(int) SSMR3GetGCUIntPtr(PSSMHANDLE pSSM, PRTGCUINTPTR pGCPtr)
3279{
3280 AssertCompile(sizeof(RTGCPTR) == sizeof(*pGCPtr));
3281 return SSMR3GetGCPtr(pSSM, (PRTGCPTR)pGCPtr);
3282}
3283
3284
3285/**
3286 * Loads an RC virtual address item from the current data unit.
3287 *
3288 * @returns VBox status.
3289 * @param pSSM SSM operation handle.
3290 * @param pRCPtr Where to store the RC virtual address.
3291 */
3292VMMR3DECL(int) SSMR3GetRCPtr(PSSMHANDLE pSSM, PRTRCPTR pRCPtr)
3293{
3294 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3295 return ssmR3Read(pSSM, pRCPtr, sizeof(*pRCPtr));
3296
3297 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3298 return VERR_SSM_INVALID_STATE;
3299}
3300
3301
3302/**
3303 * Loads a I/O port address item from the current data unit.
3304 *
3305 * @returns VBox status.
3306 * @param pSSM SSM operation handle.
3307 * @param pIOPort Where to store the I/O port address.
3308 */
3309VMMR3DECL(int) SSMR3GetIOPort(PSSMHANDLE pSSM, PRTIOPORT pIOPort)
3310{
3311 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3312 return ssmR3Read(pSSM, pIOPort, sizeof(*pIOPort));
3313 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3314 return VERR_SSM_INVALID_STATE;
3315}
3316
3317
3318/**
3319 * Loads a selector item from the current data unit.
3320 *
3321 * @returns VBox status.
3322 * @param pSSM SSM operation handle.
3323 * @param pSel Where to store the selector.
3324 */
3325VMMR3DECL(int) SSMR3GetSel(PSSMHANDLE pSSM, PRTSEL pSel)
3326{
3327 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3328 return ssmR3Read(pSSM, pSel, sizeof(*pSel));
3329 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3330 return VERR_SSM_INVALID_STATE;
3331}
3332
3333
3334/**
3335 * Loads a memory item from the current data unit.
3336 *
3337 * @returns VBox status.
3338 * @param pSSM SSM operation handle.
3339 * @param pv Where to store the item.
3340 * @param cb Size of the item.
3341 */
3342VMMR3DECL(int) SSMR3GetMem(PSSMHANDLE pSSM, void *pv, size_t cb)
3343{
3344 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3345 return ssmR3Read(pSSM, pv, cb);
3346 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3347 return VERR_SSM_INVALID_STATE;
3348}
3349
3350
3351/**
3352 * Loads a string item from the current data unit.
3353 *
3354 * @returns VBox status.
3355 * @param pSSM SSM operation handle.
3356 * @param psz Where to store the item.
3357 * @param cbMax Max size of the item (including '\\0').
3358 */
3359VMMR3DECL(int) SSMR3GetStrZ(PSSMHANDLE pSSM, char *psz, size_t cbMax)
3360{
3361 return SSMR3GetStrZEx(pSSM, psz, cbMax, NULL);
3362}
3363
3364
3365/**
3366 * Loads a string item from the current data unit.
3367 *
3368 * @returns VBox status.
3369 * @param pSSM SSM operation handle.
3370 * @param psz Where to store the item.
3371 * @param cbMax Max size of the item (including '\\0').
3372 * @param pcbStr The length of the loaded string excluding the '\\0'. (optional)
3373 */
3374VMMR3DECL(int) SSMR3GetStrZEx(PSSMHANDLE pSSM, char *psz, size_t cbMax, size_t *pcbStr)
3375{
3376 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
3377 {
3378 /* read size prefix. */
3379 uint32_t u32;
3380 int rc = SSMR3GetU32(pSSM, &u32);
3381 if (RT_SUCCESS(rc))
3382 {
3383 if (pcbStr)
3384 *pcbStr = u32;
3385 if (u32 < cbMax)
3386 {
3387 /* terminate and read string content. */
3388 psz[u32] = '\0';
3389 return ssmR3Read(pSSM, psz, u32);
3390 }
3391 return VERR_TOO_MUCH_DATA;
3392 }
3393 return rc;
3394 }
3395 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
3396 return VERR_SSM_INVALID_STATE;
3397}
3398
3399
3400
3401/**
3402 * Query what the VBox status code of the operation is.
3403 *
3404 * This can be used for putting and getting a batch of values
3405 * without bother checking the result till all the calls have
3406 * been made.
3407 *
3408 * @returns SSMAFTER enum value.
3409 * @param pSSM SSM operation handle.
3410 */
3411VMMR3DECL(int) SSMR3HandleGetStatus(PSSMHANDLE pSSM)
3412{
3413 return pSSM->rc;
3414}
3415
3416
3417/**
3418 * Fail the load operation.
3419 *
3420 * This is mainly intended for sub item loaders (like timers) which
3421 * return code isn't necessarily heeded by the caller but is important
3422 * to SSM.
3423 *
3424 * @returns SSMAFTER enum value.
3425 * @param pSSM SSM operation handle.
3426 * @param iStatus Failure status code. This MUST be a VERR_*.
3427 */
3428VMMR3DECL(int) SSMR3HandleSetStatus(PSSMHANDLE pSSM, int iStatus)
3429{
3430 if (RT_FAILURE(iStatus))
3431 {
3432 if (RT_SUCCESS(pSSM->rc))
3433 pSSM->rc = iStatus;
3434 return pSSM->rc = iStatus;
3435 }
3436 AssertMsgFailed(("iStatus=%d %Rrc\n", iStatus, iStatus));
3437 return VERR_INVALID_PARAMETER;
3438}
3439
3440
3441/**
3442 * Query what to do after this operation.
3443 *
3444 * @returns SSMAFTER enum value.
3445 * @param pSSM SSM operation handle.
3446 */
3447VMMR3DECL(SSMAFTER) SSMR3HandleGetAfter(PSSMHANDLE pSSM)
3448{
3449 return pSSM->enmAfter;
3450}
3451
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