VirtualBox

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

Last change on this file since 21992 was 21927, checked in by vboxsync, 15 years ago

SSM,VBox/err.h: SSMR3SkipToEndOfUnit and more error codes.

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