SSM.cpp revision e834dcee341693bbb99ac8673d522513043fe82e
/* $Id$ */
/** @file
* SSM - Saved State Manager.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/** @page pg_ssm SSM - The Saved State Manager
*
* The Saved State Manager (SSM) implements facilities for saving and loading a
* VM state in a structural manner using callbacks for named data units.
*
* At init time each of the VMM components, Devices, Drivers and one or two
* other things will register data units which they need to save and restore.
* Each unit have a unique name (ascii), instance number, and a set of callbacks
* associated with it. The name will be used to identify the unit during
* restore. The callbacks are for the two operations, save and restore. There
* are three callbacks for each of the two - a prepare, a execute and a complete
* - giving each component ample opportunity to perform actions both before and
* afterwards.
*
* The SSM provides a number of APIs for encoding and decoding the data: @see
* grp_ssm
*
*
*
* @section sec_ssm_live_snapshots Live Snapshots
*
* The live snapshots feature (LS) is similar to live migration (LM) and was a
* natural first step when implementing LM. The main differences between LS and
* LM are that after a live snapshot we will have a saved state file, disk image
* snapshots, and the VM will still be running.
*
* Compared to normal saved stated and snapshots, the difference is in that the
* VM is running while we do most of the saving. Prior to LS, there was only
* one round of callbacks during saving and the VM was paused during it. With
* LS there are 1 or more passes while the VM is still running and a final one
* after it has been paused. The runtime passes are executed on a dedicated
* thread running at at the same priority as the EMTs so that the saving doesn't
* starve or lose in scheduling questions (note: not implemented yet). The final
* pass is done on EMT(0).
*
* There are a couple of common reasons why LS and LM will fail:
* - Memory configuration changed (PCI memory mappings).
* - Takes too long (LM) / Too much output (LS).
*
*
* The live saving sequence is something like this:
*
* -# SSMR3LiveSave is called on EMT0. It returns a saved state
* handle.
* -# SSMR3LiveDoStep1 is called on a non-EMT. This will save the major
* parts of the state while the VM may still be running.
* -# The VM is suspended.
* -# SSMR3LiveDoStep2 is called on EMT0 to save the remainder of the state
* in the normal way.
* -# The client does any necessary reconfiguration of harddisks and
* similar.
* -# SSMR3LiveDone is called on EMT0 to close the handle.
* -# The VM is resumed or powered off and destroyed.
*
*
* @section sec_ssm_live_migration Live Migration
*
* As mentioned in the previous section, the main differences between this and
* live snapshots are in where the saved state is written and what state the
* local VM is in afterwards - at least from the VMM point of view. The
* necessary administrative work - establishing the connection to the remote
* machine, cloning the VM config on it and doing lowlevel saved state data
* transfer - is taken care of by layer above the VMM (i.e. Main).
*
* The SSM data format was made streamable for the purpose of live migration
* (v1.2 was the last non-streamable version).
*
*
* @section sec_ssm_format Saved State Format
*
* The stream format starts with a header (SSMFILEHDR) that indicates the
* version and such things, it is followed by zero or more saved state units
* (name + instance + pass), and the stream concludes with a footer
* (SSMFILEFTR) that contains unit counts and optionally a checksum for the
* entire file. (In version 1.2 and earlier, the checksum was in the header and
* there was no footer. This meant that the header was updated after the entire
* file was written.)
*
* The saved state units each starts with a variable sized header
* (SSMFILEUNITHDRV2) that contains the name, instance and pass. The data
* follows the header and is encoded as records with a 2-8 byte record header
* indicating the type, flags and size. The first byte in the record header
* indicates the type and flags:
*
* - bits 0..3: Record type:
* - type 0: Invalid.
* - type 1: Terminator with CRC-32 and unit size.
* - type 2: Raw data record.
* - type 3: Raw data compressed by LZF. The data is prefixed by a 8-bit
* field countining the length of the uncompressed data given in
* 1KB units.
* - type 4: Zero data. The record header is followed by a 8-bit field
* counting the length of the zero data given in 1KB units.
* - type 5: Named data - length prefixed name followed by the data. This
* type is not implemented yet as we're missing the API part, so
* the type assignment is tentative.
* - types 6 thru 15 are current undefined.
* - bit 4: Important (set), can be skipped (clear).
* - bit 5: Undefined flag, must be zero.
* - bit 6: Undefined flag, must be zero.
* - bit 7: "magic" bit, always set.
*
* Record header byte 2 (optionally thru 7) is the size of the following data
* encoded in UTF-8 style. To make buffering simpler and more efficient during
* the save operation, the strict checks enforcing optimal encoding has been
* relaxed for the 2 and 3 byte encodings.
*
* (In version 1.2 and earlier the unit data was compressed and not record
* based. The unit header contained the compressed size of the data, i.e. it
* needed updating after the data was written.)
*
*
* @section sec_ssm_future Future Changes
*
* There are plans to extend SSM to make it easier to be both backwards and
* (somewhat) forwards compatible. One of the new features will be being able
* to classify units and data items as unimportant (added to the format in
* v2.0). Another suggested feature is naming data items (also added to the
* format in v2.0), perhaps by extending the SSMR3PutStruct API. Both features
* will require API changes, the naming may possibly require both buffering of
* the stream as well as some helper managing them.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SSM
#include "SSMInternal.h"
#include <iprt/semaphore.h>
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** The max length of a unit name. */
#define SSM_MAX_NAME_SIZE 48
/** Saved state file magic base string. */
#define SSMFILEHDR_MAGIC_BASE "\177VirtualBox SavedState "
/** Saved state file magic indicating version 1.x. */
#define SSMFILEHDR_MAGIC_V1_X "\177VirtualBox SavedState V1."
/** Saved state file v1.1 magic. */
#define SSMFILEHDR_MAGIC_V1_1 "\177VirtualBox SavedState V1.1\n"
/** Saved state file v1.2 magic. */
#define SSMFILEHDR_MAGIC_V1_2 "\177VirtualBox SavedState V1.2\n\0\0\0"
/** Saved state file v2.0 magic. */
#define SSMFILEHDR_MAGIC_V2_0 "\177VirtualBox SavedState V2.0\n\0\0\0"
/** @name SSMFILEHDR::fFlags
* @{ */
/** The stream is checkesummed up to the footer using CRC-32. */
#define SSMFILEHDR_FLAGS_STREAM_CRC32 RT_BIT_32(0)
/** Indicates that the file was produced by a live save. */
/** @} */
/** The directory magic. */
#define SSMFILEDIR_MAGIC "\nDir\n\0\0"
/** Saved state file v2.0 magic. */
#define SSMFILEFTR_MAGIC "\nFooter"
/** Data unit magic. */
#define SSMFILEUNITHDR_MAGIC "\nUnit\n\0"
/** Data end marker magic. */
#define SSMFILEUNITHDR_END "\nTheEnd"
/** @name Record Types (data unit)
* @{ */
/** The record type mask. */
/** Invalid record. */
#define SSM_REC_TYPE_INVALID 0
/** Normal termination record, see SSMRECTERM. */
#define SSM_REC_TYPE_TERM 1
/** Raw data. The data follows the size field without further ado. */
#define SSM_REC_TYPE_RAW 2
/** Raw data compressed by LZF.
* The record header is followed by a 8-bit field containing the size of the
* uncompressed data in 1KB units. The compressed data is after it. */
#define SSM_REC_TYPE_RAW_LZF 3
/** Raw zero data.
* The record header is followed by a 8-bit field containing the size of the
* zero data in 1KB units. */
#define SSM_REC_TYPE_RAW_ZERO 4
/** Named data items.
* A length prefix zero terminated string (i.e. max 255) followed by the data. */
#define SSM_REC_TYPE_NAMED 5
/** Macro for validating the record type.
* This can be used with the flags+type byte, no need to mask out the type first. */
/** @} */
/** The flag mask. */
/** The record is important if this flag is set, if clear it can be omitted. */
/** This flag is always set. */
/** Macro for validating the flags.
* No need to mask the flags out of the flags+type byte before invoking this macro. */
/** Macro for validating the type and flags byte in a data record. */
#define SSM_REC_ARE_TYPE_AND_FLAGS_VALID(u8) ( SSM_REC_FLAGS_ARE_VALID(u8) && SSM_REC_TYPE_IS_VALID(u8) )
/** @name SSMRECTERM::fFlags
* @{ */
/** There is a CRC-32 value for the stream. */
/** @} */
/** Start structure magic. (Isacc Asimov) */
/** End structure magic. (Isacc Asimov) */
/** Number of bytes to log in Log2 and Log4 statements. */
#define SSM_LOG_BYTES 16
/** SSMHANDLE::fCancelled value indicating that the operation has been
* cancelled. */
/** SSMHANDLE::fCancelled value indicating no cancellation. */
/** Macro for checking the u32CRC field of a structure.
* The Msg can assume there are u32ActualCRC and u32CRC in the context. */
do \
{ \
(p)->u32CRC = 0; \
} while (0)
/** The number of bytes to compress is one block.
* Must be a multiple of 1KB. */
#define SSM_ZIP_BLOCK_SIZE _4K
/**
* Asserts that the handle is writable and returns with VERR_SSM_INVALID_STATE
* if it isn't.
*/
#define SSM_ASSERT_WRITEABLE_RET(pSSM) \
/**
* Asserts that the handle is readable and returns with VERR_SSM_INVALID_STATE
* if it isn't.
*/
#define SSM_ASSERT_READABLE_RET(pSSM) \
/** Checks for cancellation and returns if pending.
* Sets SSMHANDLE::rc to VERR_SSM_CANCELLED (if it still indicates success) and
* then returns SSMHANDLE::rc. (Debug logging only.) */
#define SSM_CHECK_CANCELLED_RET(pSSM) \
do \
{ \
{ \
} \
} while (0)
/**
* Asserts that the handle is somewhat valid. No returns as this is just a
* simple safeguard for catching bad API calls. */
#define SSM_ASSERT_VALID_HANDLE(pSSM) \
do \
{ \
} while (0)
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** SSM state. */
typedef enum SSMSTATE
{
SSMSTATE_INVALID = 0,
} SSMSTATE;
/** Pointer to a SSM stream buffer. */
typedef struct SSMSTRMBUF *PSSMSTRMBUF;
/**
* A SSM stream buffer.
*/
typedef struct SSMSTRMBUF
{
/** The buffer data. */
/** The stream position of this buffer. */
/** The amount of buffered data. */
/** End of stream indicator (for read streams only). */
bool fEndOfStream;
/** Pointer to the next buffer in the chain. */
PSSMSTRMBUF volatile pNext;
} SSMSTRMBUF;
/**
* SSM stream.
*
* This is a typical producer / consumer setup with a dedicated I/O thread and
* fixed number of buffers for read ahead and write back.
*/
typedef struct SSMSTRM
{
/** The stream method table. */
/** The user argument for the stream methods.
* For file based streams, this is the file handle and not a pointer. */
void *pvUser;
/** Write (set) or read (clear) stream. */
bool fWrite;
/** Termination indicator. */
bool volatile fTerminating;
/** Indicates whether it is necessary to seek before the next buffer is
* read from the stream. This is used to avoid a seek in ssmR3StrmPeekAt. */
bool fNeedSeek;
/** Stream error status. */
/** The handle of the I/O thread. This is set to nil when not active. */
/** Where to seek to. */
/** The head of the consumer queue.
* For save the consumer is the I/O thread. For load the I/O thread is the
* producer. */
PSSMSTRMBUF volatile pHead;
/** Chain of free buffers.
PSSMSTRMBUF volatile pFree;
/** Event that's signalled when pHead is updated. */
/** Event that's signalled when pFree is updated. */
/** List of pending buffers that has been dequeued from pHead and reversed. */
/** Pointer to the current buffer. */
/** The stream offset of the current buffer. */
/** The current buffer offset. */
bool fChecksummed;
/** The stream CRC if fChecksummed is set. */
/** How far into the buffer u32StreamCRC is up-to-date.
* This may lag behind off as it's desirable to checksum as large blocks as
* possible. */
} SSMSTRM;
/** Pointer to a SSM stream. */
/**
* Handle structure.
*/
typedef struct SSMHANDLE
{
/** The VM handle. */
/** The current operation. */
/** What to do after save completes. (move the enum) */
/** Flag indicating that the operation has been cancelled. */
uint32_t volatile fCancelled;
/** The current rc of the save operation. */
/** Number of compressed bytes left in the current data unit (V1). */
/** The current uncompressed offset into the data unit. */
/** Indicates that this is a live save or restore operation. */
bool fLiveSave;
/** Pointer to the progress callback function. */
/** User specified arguemnt to the callback function. */
void *pvUser;
/** Next completion percentage. (corresponds to offEstProgress) */
unsigned uPercent;
/** The position of the next progress callback in the estimated file. */
/** The estimated total byte count.
* (Only valid after the prep.) */
/** Current position in the estimated file. */
/** End of current unit in the estimated file. */
/** the amount of % we reserve for the 'prepare' phase */
unsigned uPercentPrepare;
/** the amount of % we reserve for the 'done' stage */
unsigned uPercentDone;
/** The filename, NULL if remote stream. */
const char *pszFilename;
union
{
/** Write data. */
struct
{
/** Offset into the databuffer. */
/** Space for the record header. */
/** Data buffer. */
} Write;
/** Read data. */
struct
{
/** V1: The decompressor of the current data unit. */
/** The major format version number. */
/** The minor format version number. */
/** V2: Unread bytes in the current record. */
/** V2: Bytes in the data buffer. */
/** V2: Current buffer position. */
/** V2: End of data indicator. */
bool fEndOfData;
/** V2: The type and flags byte fo the current record. */
unsigned cbGCPhys;
unsigned cbGCPtr;
/** Whether cbGCPtr is fixed or settable. */
bool fFixedGCPtrSize;
/** @name Header info (set by ssmR3ValidateFile)
* @{ */
/** The size of the file header. */
/** The major version number. */
/** The minor version number. */
/** The build number. */
/** The SVN revision. */
/** 32 or 64 depending on the host. */
/** Whether the stream is checksummed (SSMFILEHDR_FLAGS_STREAM_CRC32). */
bool fStreamCrc32;
/** The CRC of the loaded file. */
/** The size of the load file. */
/** @} */
/** V2: Data buffer.
* @remarks Be extremely careful when changing the size of this buffer! */
/** V2: Decompression buffer for when we cannot use the stream buffer. */
} Read;
} u;
} SSMHANDLE;
/**
* Header of the saved state file.
*
* Added in r5xxxx on 2009-07-2?, VirtualBox v3.0.51.
*/
typedef struct SSMFILEHDR
{
/** Magic string which identifies this file as a version of VBox saved state
* file format (SSMFILEHDR_MAGIC_V2_0). */
char szMagic[32];
/** The major version number. */
/** The minor version number. */
/** The build number. */
/** The SVN revision. */
/** 32 or 64 depending on the host. */
/** The size of RTGCPHYS. */
/** The size of RTGCPTR. */
/** Reserved header space - must be zero. */
/** The number of units that (may) have stored data in the file. */
/** Flags, see SSMFILEHDR_FLAGS_XXX. */
/** The maximum size of decompressed data. */
/** The checksum of this header.
* This field is set to zero when calculating the checksum. */
} SSMFILEHDR;
/** Pointer to a saved state file header. */
typedef SSMFILEHDR *PSSMFILEHDR;
/** Pointer to a const saved state file header. */
typedef SSMFILEHDR const *PCSSMFILEHDR;
/**
* Header of the saved state file.
*
* Added in r40980 on 2008-12-15, VirtualBox v2.0.51.
*
* @remarks This is a superset of SSMFILEHDRV11.
*/
typedef struct SSMFILEHDRV12
{
/** Magic string which identifies this file as a version of VBox saved state
* file format (SSMFILEHDR_MAGIC_V1_2). */
char achMagic[32];
/** The size of this file. Used to check
* whether the save completed and that things are fine otherwise. */
/** File checksum. The actual calculation skips past the u32CRC field. */
/** Padding. */
/** The machine UUID. (Ignored if NIL.) */
/** The major version number. */
/** The minor version number. */
/** The build number. */
/** The SVN revision. */
/** 32 or 64 depending on the host. */
/** The size of RTGCPHYS. */
/** The size of RTGCPTR. */
/** Padding. */
/** Pointer to a saved state file header. */
typedef SSMFILEHDRV12 *PSSMFILEHDRV12;
/**
* Header of the saved state file, version 1.1.
*
* Added in r23677 on 2007-08-17, VirtualBox v1.4.1.
*/
typedef struct SSMFILEHDRV11
{
/** Magic string which identifies this file as a version of VBox saved state
* file format (SSMFILEHDR_MAGIC_V1_1). */
char achMagic[32];
/** The size of this file. Used to check
* whether the save completed and that things are fine otherwise. */
/** File checksum. The actual calculation skips past the u32CRC field. */
/** Padding. */
/** The machine UUID. (Ignored if NIL.) */
/** Pointer to a saved state file header. */
typedef SSMFILEHDRV11 *PSSMFILEHDRV11;
/**
* Data unit header.
*/
typedef struct SSMFILEUNITHDRV2
{
/** Magic (SSMFILEUNITHDR_MAGIC or SSMFILEUNITHDR_END). */
char szMagic[8];
/** The offset in the saved state stream of the start of this unit.
* This is mainly intended for sanity checking. */
/** The CRC-in-progress value this unit starts at. */
/** The checksum of this structure, including the whole name.
* Calculated with this field set to zero. */
/** Data version. */
/** Instance number. */
/** Data pass number. */
/** Flags reserved for future extensions. Must be zero. */
/** Size of the data unit name including the terminator. (bytes) */
/** Data unit name, variable size. */
char szName[SSM_MAX_NAME_SIZE];
/** Pointer to SSMFILEUNITHDRV2. */
typedef SSMFILEUNITHDRV2 *PSSMFILEUNITHDRV2;
/**
* Data unit header.
*
* This is used by v1.0, v1.1 and v1.2 of the format.
*/
typedef struct SSMFILEUNITHDRV1
{
/** Magic (SSMFILEUNITHDR_MAGIC or SSMFILEUNITHDR_END). */
char achMagic[8];
/** Number of bytes in this data unit including the header. */
/** Data version. */
/** Instance number. */
/** Size of the data unit name including the terminator. (bytes) */
/** Data unit name. */
char szName[1];
/** Pointer to SSMFILEUNITHDR. */
typedef SSMFILEUNITHDRV1 *PSSMFILEUNITHDRV1;
/**
* Termination data record.
*/
typedef struct SSMRECTERM
{
/** The record size (sizeof(SSMRECTERM) - 2). */
/** Flags, see SSMRECTERM_FLAGS_CRC32. */
/** The checksum of the stream up to fFlags (exclusive). */
/** The length of this data unit in bytes (including this record). */
} SSMRECTERM;
/** Pointer to a termination record. */
typedef SSMRECTERM *PSSMRECTERM;
/** Pointer to a const termination record. */
typedef SSMRECTERM const *PCSSMRECTERM;
/**
* Directory entry.
*/
typedef struct SSMFILEDIRENTRY
{
/** The offset of the data unit. */
/** The instance number. */
/** The CRC-32 of the name excluding the terminator. (lazy bird) */
/** Pointer to a directory entry. */
typedef SSMFILEDIRENTRY *PSSMFILEDIRENTRY;
/** Pointer to a const directory entry. */
typedef SSMFILEDIRENTRY const *PCSSMFILEDIRENTRY;
/**
* Directory for the data units from the final pass.
*
* This is used to speed up SSMR3Seek (it would have to decompress and parse the
* whole stream otherwise).
*/
typedef struct SSMFILEDIR
{
/** Magic string (SSMFILEDIR_MAGIC). */
char szMagic[8];
/** The CRC-32 for the whole directory.
* Calculated with this field set to zero. */
/** The number of directory entries. */
/** The directory entries (variable size). */
} SSMFILEDIR;
/** Pointer to a directory. */
typedef SSMFILEDIR *PSSMFILEDIR;
/** Pointer to a const directory. */
typedef SSMFILEDIR *PSSMFILEDIR;
/**
* Footer structure
*/
typedef struct SSMFILEFTR
{
/** Magic string (SSMFILEFTR_MAGIC). */
char szMagic[8];
/** The offset of this record in the stream. */
/** The CRC for the stream.
* This is set to zero if SSMFILEHDR_FLAGS_STREAM_CRC32 is clear. */
/** Number directory entries. */
/** Reserved footer space - must be zero. */
/** The CRC-32 for this structure.
* Calculated with this field set to zero. */
} SSMFILEFTR;
/** Pointer to a footer. */
typedef SSMFILEFTR *PSSMFILEFTR;
/** Pointer to a const footer. */
typedef SSMFILEFTR const *PCSSMFILEFTR;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static DECLCALLBACK(int) ssmR3SelfLoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
static int ssmR3Register(PVM pVM, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess, const char *pszBefore, PSSMUNIT *ppUnit);
/**
* Cleans up resources allocated by SSM on VM termination.
*
* @param pVM The VM handle.
*/
{
{
}
}
/**
* Performs lazy initialization of the SSM.
*
* @returns VBox status code.
* @param pVM The VM.
*/
{
/*
* Register a saved state unit which we use to put the VirtualBox version,
* revision and similar stuff in.
*/
/*
* Initialize the cancellation critsect now.
*/
if (RT_SUCCESS(rc))
return rc;
}
/**
* Do ssmR3SelfSaveExec in pass 0.
*
* @returns VBox status code.
* @param pVM Pointer to the shared VM structure.
* @param pSSM The SSM handle.
* @param uPass The data pass number.
*/
{
if (uPass == 0)
{
if (RT_SUCCESS(rc))
return rc;
}
AssertFailed();
return VERR_INTERNAL_ERROR_3;
}
/**
* For saving usful things without having to go thru the tedious process of
* adding it to the header.
*
* @returns VBox status code.
* @param pVM Pointer to the shared VM structure.
* @param pSSM The SSM handle.
*/
{
/*
* String table containg pairs of variable and value string.
* Terminated by two empty strings.
*/
#ifdef VBOX_OSE
#endif
/* terminator */
}
/**
* For load the version + revision and stuff.
*
* @returns VBox status code.
* @param pVM Pointer to the shared VM structure.
* @param pSSM The SSM handle.
* @param uVersion The version (1).
* @param uPass The pass.
*/
static DECLCALLBACK(int) ssmR3SelfLoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
/*
* The first and last passes contains a {name, value} string table that is
* terminated by two emptry strings. It contains useful informal build
* info and can be very handy when something goes wrong after restore.
*/
if ( uPass == 0
|| uPass == SSM_PASS_FINAL)
{
for (unsigned i = 0; ; i++)
{
char szVar[128];
char szValue[1024];
break;
if (i == 0)
LogRel(("SSM: Saved state info:\n"));
}
}
return VINF_SUCCESS;
}
/**
* Internal registration worker.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pszName Data unit name.
* @param uInstance The instance id.
* @param uVersion The data unit version.
* @param cbGuess The guessed data unit size.
* @param pszBefore Name of data unit to be placed in front of.
* Optional.
* @param ppUnit Where to store the insterted unit node.
* Caller must fill in the missing details.
*/
{
/*
* Validate input.
*/
AssertMsgReturn(cchName < SSM_MAX_NAME_SIZE, ("%zu >= %u: %s\n", cchName, SSM_MAX_NAME_SIZE, pszName), VERR_OUT_OF_RANGE);
AssertMsgReturn(cchBefore < SSM_MAX_NAME_SIZE, ("%zu >= %u: %s\n", cchBefore, SSM_MAX_NAME_SIZE, pszBefore), VERR_OUT_OF_RANGE);
/*
* Lazy init.
*/
{
}
/*
* Walk to the end of the list checking for duplicates as we go.
*/
while (pUnit)
{
{
return VERR_SSM_UNIT_EXISTS;
}
&& !pUnitBefore
{
pUnitBefore = pUnit;
}
/* next */
}
/*
* Allocate new node.
*/
if (!pUnit)
return VERR_NO_MEMORY;
/*
* Fill in (some) data. (Stuff is zero'ed.)
*/
/*
* Insert
*/
if (pUnitBefore)
{
if (pUnitBeforePrev)
else
}
else if (pUnitPrev)
else
return VINF_SUCCESS;
}
/**
* Register a PDM Devices data unit.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pDevIns Device instance.
* @param pszName Data unit name.
* @param uInstance The instance identifier of the data unit.
* This must together with the name be unique.
* @param uVersion Data layout version number.
* @param cbGuess The approximate amount of data in the unit.
* Only for progress indicators.
* @param pszBefore Name of data unit which we should be put in front
* of. Optional (NULL).
*
* @param pfnLivePrep Prepare live save callback, optional.
* @param pfnLiveExec Execute live save callback, optional.
* @param pfnLiveVote Vote live save callback, optional.
*
* @param pfnSavePrep Prepare save callback, optional.
* @param pfnSaveExec Execute save callback, optional.
* @param pfnSaveDone Done save callback, optional.
*
* @param pfnLoadPrep Prepare load callback, optional.
* @param pfnLoadExec Execute load callback, optional.
* @param pfnLoadDone Done load callback, optional.
*/
VMMR3DECL(int) SSMR3RegisterDevice(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess, const char *pszBefore,
{
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Register a PDM driver data unit.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pDrvIns Driver instance.
* @param pszName Data unit name.
* @param uInstance The instance identifier of the data unit.
* This must together with the name be unique.
* @param uVersion Data layout version number.
* @param cbGuess The approximate amount of data in the unit.
* Only for progress indicators.
*
* @param pfnLivePrep Prepare live save callback, optional.
* @param pfnLiveExec Execute live save callback, optional.
* @param pfnLiveVote Vote live save callback, optional.
*
* @param pfnSavePrep Prepare save callback, optional.
* @param pfnSaveExec Execute save callback, optional.
* @param pfnSaveDone Done save callback, optional.
*
* @param pfnLoadPrep Prepare load callback, optional.
* @param pfnLoadExec Execute load callback, optional.
* @param pfnLoadDone Done load callback, optional.
*/
VMMR3DECL(int) SSMR3RegisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess,
{
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Register a internal data unit.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pszName Data unit name.
* @param uInstance The instance identifier of the data unit.
* This must together with the name be unique.
* @param uVersion Data layout version number.
* @param cbGuess The approximate amount of data in the unit.
* Only for progress indicators.
*
* @param pfnLivePrep Prepare live save callback, optional.
* @param pfnLiveExec Execute live save callback, optional.
* @param pfnLiveVote Vote live save callback, optional.
*
* @param pfnSavePrep Prepare save callback, optional.
* @param pfnSaveExec Execute save callback, optional.
* @param pfnSaveDone Done save callback, optional.
*
* @param pfnLoadPrep Prepare load callback, optional.
* @param pfnLoadExec Execute load callback, optional.
* @param pfnLoadDone Done load callback, optional.
*/
VMMR3DECL(int) SSMR3RegisterInternal(PVM pVM, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess,
{
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Register an external data unit.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pszName Data unit name.
* @param uInstance The instance identifier of the data unit.
* This must together with the name be unique.
* @param uVersion Data layout version number.
* @param cbGuess The approximate amount of data in the unit.
* Only for progress indicators.
*
* @param pfnLivePrep Prepare live save callback, optional.
* @param pfnLiveExec Execute live save callback, optional.
* @param pfnLiveVote Vote live save callback, optional.
*
* @param pfnSavePrep Prepare save callback, optional.
* @param pfnSaveExec Execute save callback, optional.
* @param pfnSaveDone Done save callback, optional.
*
* @param pfnLoadPrep Prepare load callback, optional.
* @param pfnLoadExec Execute load callback, optional.
* @param pfnLoadDone Done load callback, optional.
* @param pvUser User argument.
*/
VMMR3DECL(int) SSMR3RegisterExternal(PVM pVM, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess,
PFNSSMEXTLOADPREP pfnLoadPrep, PFNSSMEXTLOADEXEC pfnLoadExec, PFNSSMEXTLOADDONE pfnLoadDone, void *pvUser)
{
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Deregister one or more PDM Device data units.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pDevIns Device instance.
* @param pszName Data unit name.
* Use NULL to deregister all data units for that device instance.
* @param uInstance The instance identifier of the data unit.
* This must together with the name be unique.
* @remark Only for dynmaic data units and dynamic unloaded modules.
*/
VMMR3_INT_DECL(int) SSMR3DeregisterDevice(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t uInstance)
{
/*
* Validate input.
*/
if (!pDevIns)
{
AssertMsgFailed(("pDevIns is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Search the list.
*/
while (pUnit)
{
&& ( !pszName
)
{
{
/*
* Unlink it, advance pointer, and free the node.
*/
if (pUnitPrev)
else
if (pszName)
return VINF_SUCCESS;
rc = VINF_SUCCESS;
continue;
}
else if (pszName)
{
AssertMsgFailed(("Caller is not owner! Owner=%p Caller=%p %s\n",
return VERR_SSM_UNIT_NOT_OWNER;
}
}
/* next */
}
return rc;
}
/**
* Deregister one ore more PDM Driver data units.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pDrvIns Driver instance.
* @param pszName Data unit name.
* Use NULL to deregister all data units for that driver instance.
* @param uInstance The instance identifier of the data unit.
* This must together with the name be unique. Ignored if pszName is NULL.
* @remark Only for dynmaic data units and dynamic unloaded modules.
*/
VMMR3_INT_DECL(int) SSMR3DeregisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t uInstance)
{
/*
* Validate input.
*/
if (!pDrvIns)
{
AssertMsgFailed(("pDrvIns is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Search the list.
*/
while (pUnit)
{
&& ( !pszName
)
{
{
/*
* Unlink it, advance pointer, and free the node.
*/
if (pUnitPrev)
else
if (pszName)
return VINF_SUCCESS;
rc = VINF_SUCCESS;
continue;
}
else if (pszName)
{
AssertMsgFailed(("Caller is not owner! Owner=%p Caller=%p %s\n",
return VERR_SSM_UNIT_NOT_OWNER;
}
}
/* next */
}
return rc;
}
/**
* Deregister a data unit.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param enmType Unit type
* @param pszName Data unit name.
* @remark Only for dynmaic data units.
*/
{
/*
* Validate input.
*/
if (!pszName)
{
AssertMsgFailed(("pszName is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Search the list.
*/
int rc = VERR_SSM_UNIT_NOT_FOUND;
while (pUnit)
{
{
/*
* Unlink it, advance pointer, and free the node.
*/
if (pUnitPrev)
else
return VINF_SUCCESS;
}
/* next */
}
return rc;
}
/**
* Deregister an internal data unit.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pszName Data unit name.
* @remark Only for dynmaic data units.
*/
{
}
/**
* Deregister an external data unit.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pszName Data unit name.
* @remark Only for dynmaic data units.
*/
{
}
/**
*
* @returns VINF_SUCCESS or VERR_NO_MEMORY.
* @param pStrm The stream handle.
* @param fChecksummed Whether the stream is to be checksummed while
* @param cBuffers The number of buffers.
*/
{
/*
* Init the common data members.
*/
pStrm->fTerminating = false;
pStrm->offCurStream = 0;
pStrm->offStreamCRC = 0;
/*
* Allocate the buffers. Page align them in case that makes the kernel
*/
int rc = VINF_SUCCESS;
{
if (!pBuf)
{
if (i > 2)
{
LogRel(("ssmR3StrmAllocBuffer: WARNING: Could only get %d stream buffers.\n", i));
break;
}
LogRel(("ssmR3StrmAllocBuffer: Failed to allocate stream buffers. (i=%d)\n", i));
return VERR_NO_MEMORY;
}
/* link it */
}
/*
* Create the event semaphores.
*/
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
/**
* Destroys a list of buffers.
*
* @param pHead Pointer to the head.
*/
{
while (pHead)
{
}
}
/**
* Cleans up a stream after ssmR3StrmInitInternal has been called (regardless of
* it succeeded or not).
*
* @param pStrm The stream handle.
*/
{
}
/**
* Initializes a stream that uses a method table.
*
* @returns VBox status code.
* @param pStrm The stream manager structure.
* @param pStreamOps The stream method table.
* @param pvUser The user argument for the stream methods.
* @param fWrite Whether to open for writing or reading.
* @param fChecksummed Whether the stream is to be checksummed while
* @param cBuffers The number of buffers.
*/
static int ssmR3StrmInit(PSSMSTRM pStrm, PCSSMSTRMOPS pStreamOps, void *pvUser, bool fWrite, bool fChecksummed, uint32_t cBuffers)
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
/**
* @copydoc SSMSTRMOPS::pfnWrite
*/
static DECLCALLBACK(int) ssmR3FileWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
{
return RTFileWriteAt((RTFILE)(uintptr_t)pvUser, offStream, pvBuf, cbToWrite, NULL); /** @todo use RTFileWrite */
}
/**
* @copydoc SSMSTRMOPS::pfnRead
*/
static DECLCALLBACK(int) ssmR3FileRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
{
}
/**
* @copydoc SSMSTRMOPS::pfnSeek
*/
static DECLCALLBACK(int) ssmR3FileSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
{
}
/**
* @copydoc SSMSTRMOPS::pfnTell
*/
{
}
/**
* @copydoc SSMSTRMOPS::pfnSize
*/
{
}
/**
* @copydoc SSMSTRMOPS::pfnClose
*/
{
}
/**
* Method table for a file based stream.
*/
static SSMSTRMOPS const g_ssmR3FileOps =
{
};
/**
* Opens a file stream.
*
* @returns VBox status code.
* @param pStrm The stream manager structure.
* @param pszFilename The file to open or create.
* @param fWrite Whether to open for writing or reading.
* @param fChecksummed Whether the stream is to be checksummed while
* @param cBuffers The number of buffers.
*/
static int ssmR3StrmOpenFile(PSSMSTRM pStrm, const char *pszFilename, bool fWrite, bool fChecksummed, uint32_t cBuffers)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
return rc;
}
/**
* Raise an error condition on the stream.
*
* @returns true if we raised the error condition, false if the stream already
* had an error condition set.
*
* @param pStrm The stream handle.
* @param rc The VBox error status code.
*
* @thread Any.
*/
{
}
/**
* Puts a buffer into the free list.
*
* @param pStrm The stream handle.
* @param pBuf The buffer.
*
* @thread The consumer.
*/
{
for (;;)
{
{
return;
}
}
}
/**
* Gets a free buffer, waits for one if necessary.
*
* @returns Pointer to the buffer on success. NULL if we're terminating.
* @param pStrm The stream handle.
*
* @thread The producer.
*/
{
for (;;)
{
if (!pMine)
{
if (pStrm->fTerminating)
return NULL;
return NULL;
{
if (RT_FAILURE(rc))
return NULL;
}
if ( rc == VERR_SEM_DESTROYED
|| pStrm->fTerminating)
return NULL;
continue;
}
{
pMine->fEndOfStream = false;
return pMine;
}
}
}
/**
* Puts a buffer onto the queue.
*
* @param pBuf The buffer.
*
* @thread The producer.
*/
{
for (;;)
{
{
return;
}
}
}
/**
* Reverses the list.
*
* @returns The head of the reversed list.
* @param pHead The head of the list to reverse.
*/
{
while (pHead)
{
}
return pRevHead;
}
/**
* Gets one buffer from the queue, will wait for one to become ready if
* necessary.
*
* @returns Pointer to the buffer on success. NULL if we're terminating.
* @param pBuf The buffer.
*
* @thread The consumer.
*/
{
for (;;)
{
if (pMine)
{
return pMine;
}
if (pMine)
else
{
if (pStrm->fTerminating)
return NULL;
return NULL;
{
if (RT_FAILURE(rc))
return NULL;
continue;
}
if ( rc == VERR_SEM_DESTROYED
|| pStrm->fTerminating)
return NULL;
}
}
}
/**
* Flushes the current buffer (both write and read streams).
*
* @param pStrm The stream handle.
*/
{
{
{
if ( pStrm->fChecksummed
pStrm->offStreamCRC = 0;
}
else
{
if ( pStrm->fChecksummed
pStrm->offStreamCRC = 0;
}
}
}
/**
* Flush buffered data.
*
* @returns VBox status code. Returns VINF_EOF if we encounter a buffer with the
* fEndOfStream indicator set.
* @param pStrm The stream handle.
*
* @thread The producer thread.
*/
{
/*
* Just return if the stream has a pending error condition.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Grab the pending list and write it out.
*/
if (!pHead)
return VINF_SUCCESS;
while (pHead)
{
/* pop */
/* flush */
if ( RT_FAILURE(rc)
LogRel(("ssmR3StrmWriteBuffers: RTFileWriteAt failed with rc=%Rrc at offStream=%#llx\n", rc, pCur->offStream));
/* free */
if (fEndOfStream)
{
return VINF_EOF;
}
}
}
/**
* Closes the stream after first flushing any pending write.
*
* @returns VBox status code.
* @param pStrm The stream handle.
*/
{
/*
* Flush, terminate the I/O thread, and close the stream.
*/
{
}
int rc;
{
{
}
if (RT_FAILURE(rc))
}
else
{
if (RT_FAILURE(rc))
{
}
}
return rc;
}
/**
* Stream output routine.
*
* @returns VBox status code.
* @param pStrm The stream handle.
* @param pvBuf What to write.
* @param cbToWrite How much to write.
*
* @thread The producer in a write stream (never the I/O thread).
*/
{
/*
* Squeeze as much as possible into the current buffer.
*/
{
{
return VINF_SUCCESS;
}
if (cbLeft > 0)
{
}
}
/*
* Need one or more new buffers.
*/
do
{
/*
* Flush the current buffer and replace it with a new one.
*/
if (!pBuf)
break;
/*
* Copy data to the buffer.
*/
} while (cbToWrite > 0);
}
/**
* Reserves space in the current buffer so the caller can write directly to the
* buffer instead of doing double buffering.
*
* @returns VBox status code
* @param pStrm The stream handle.
* @param cb The amount of buffer space to reserve.
* @param ppb Where to return the pointer.
*/
{
/*
* Check if there is room in the current buffer, it not flush it.
*/
if (pBuf)
{
{
return VINF_SUCCESS;
}
}
/*
* Get a fresh buffer and return a pointer into it.
*/
if (pBuf)
{
}
else
}
/**
* Commits buffer space reserved by ssmR3StrmReserveWriteBufferSpace.
*
* @returns VBox status code.
* @param pStrm The stream handle.
* @param cb The amount of buffer space to commit. This can be less
* that what was reserved initially.
*/
{
return VINF_SUCCESS;
}
/**
* Marks the end of the stream.
*
* This will cause the I/O thread to quit waiting for more buffers.
*
* @returns VBox status code.
* @param pStrm The stream handle.
*/
{
{
if (!pBuf)
}
pBuf->fEndOfStream = true;
return VINF_SUCCESS;
}
/**
* Read more from the stream.
*
* @returns VBox status code. VERR_EOF gets translated into VINF_EOF.
* @param pStrm The stream handle.
*
* @thread The I/O thread when we got one, otherwise the stream user.
*/
{
int rc;
Log6(("ssmR3StrmReadMore:\n"));
/*
* Undo seek done by ssmR3StrmPeekAt.
*/
{
if (RT_FAILURE(rc))
{
return rc;
}
}
/*
* Get a free buffer and try fill it up.
*/
if (!pBuf)
if ( RT_SUCCESS(rc)
&& cbRead > 0)
{
pBuf->fEndOfStream = false;
}
else if ( ( RT_SUCCESS_NP(rc)
&& cbRead == 0)
{
pBuf->fEndOfStream = true;
}
else
{
LogRel(("ssmR3StrmReadMore: RTFileRead(,,%#x,) -> %Rrc at offset %#llx\n",
}
return rc;
}
/**
* Stream input routine.
*
* @returns VBox status code.
* @param pStrm The stream handle.
* @param pvBuf Where to put what we read.
* @param cbToRead How much to read.
*/
{
/*
* Read from the current buffer if we got one.
*/
{
{
return VINF_SUCCESS;
}
if (cbLeft)
{
}
else if (pBuf->fEndOfStream)
return VERR_EOF;
}
/*
* Get more buffers from the stream.
*/
int rc = VINF_SUCCESS;
do
{
/*
* Check for EOF first - never flush the EOF buffer.
*/
if ( pBuf
&& pBuf->fEndOfStream)
return VERR_EOF;
/*
* Flush the current buffer and get the next one.
*/
if (!pBuf)
{
break;
}
{
return VERR_EOF;
}
/*
* Read data from the buffer.
*/
} while (cbToRead > 0);
return rc;
}
/**
* Reads data from the stream but instead of copying it to some output buffer
* the caller gets a pointer to into the current stream buffer.
*
* The returned pointer becomes invalid after the next stream operation!
*
* @returns Pointer to the read data residing in the stream buffer. NULL is
* returned if the request amount of data isn't available in the
* buffer. The caller must fall back on ssmR3StrmRead when this
* happens.
*
* @param pStrm The stream handle.
* @param cbToRead The number of bytes to tread.
*/
{
/*
* Too lazy to fetch more data for the odd case that we're
* exactly at the boundrary between two buffers.
*/
{
{
return pb;
}
}
return NULL;
}
/**
* Tell current stream position.
*
* @returns stream position.
* @param pStrm The stream handle.
*/
{
}
/**
* Gets the intermediate stream CRC up to the current position.
*
* @returns CRC.
* @param pStrm The stream handle.
*/
{
if (!pStrm->fChecksummed)
return 0;
{
pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC, &pBuf->abData[pStrm->offStreamCRC], pStrm->off - pStrm->offStreamCRC);
}
else
return pStrm->u32StreamCRC;
}
/**
* Gets the final stream CRC up to the current position.
*
* @returns CRC.
* @param pStrm The stream handle.
*/
{
if (!pStrm->fChecksummed)
return 0;
}
/**
* Disables checksumming of the stream.
*
* @param pStrm The stream handle.
*/
{
pStrm->fChecksummed = false;
}
/**
* Used by SSMR3Seek to position the stream at the new unit.
*
* @returns VBox stutus code.
* @param pStrm The strem handle.
* @param off The seek offset.
* @param uMethod The seek method.
* @param u32CurCRC The current CRC at the seek position.
*/
{
if (RT_SUCCESS(rc))
{
pStrm->offStreamCRC = 0;
if (pStrm->fChecksummed)
{
}
}
return rc;
}
/**
* Skip some bytes in the stream.
*
* This is only used if someone didn't read all of their data in the V1 format,
* so don't bother making this very efficient yet.
*
* @returns VBox status code.
* @param pStrm The stream handle.
* @param offDst The destination offset.
*/
{
/* dead simple - lazy bird! */
for (;;)
{
return VINF_SUCCESS;
if (RT_FAILURE(rc))
return rc;
}
}
/**
* Get the size of the file.
*
* This does not work for non-file streams!
*
* @returns The file size, or UINT64_MAX if not a file stream.
* @param pStrm The stream handle.
*/
{
return cbFile;
}
/***
* Tests if the stream is a file stream or not.
*
* @returns true / false.
* @param pStrm The stream handle.
*/
{
}
/**
* Peeks at data in a file stream without buffering anything (or upsetting
* the buffering for that matter).
*
* @returns VBox status code.
* @param pStrm The stream handle
* @param off The offset to start peeking at. Use a negative offset to
* peek at something relative to the end of the file.
* @param pvBuf Output buffer.
* @param cbToRead How much to read.
* @param poff Where to optionally store the position. Useful when
* using a negative off.
*
* @remarks Failures occuring while peeking will not be raised on the stream.
*/
static int ssmR3StrmPeekAt(PSSMSTRM pStrm, RTFOFF off, void *pvBuf, size_t cbToRead, uint64_t *poff)
{
{
}
int rc = pStrm->pOps->pfnSeek(pStrm->pvUser, off, off >= 0 ? RTFILE_SEEK_BEGIN : RTFILE_SEEK_END, &offActual);
if (RT_SUCCESS(rc))
{
if (poff)
}
return rc;
}
/**
* The I/O thread.
*
* @returns VINF_SUCCESS (ignored).
* @param hSelf The thread handle.
* @param pvStrm The stream handle.
*/
{
Log(("ssmR3StrmIoThread: starts working\n"));
{
/*
* Write until error or terminated.
*/
for (;;)
{
if ( RT_FAILURE(rc)
{
break;
}
{
break;
}
{
{
Log(("ssmR3StrmIoThread: quitting writing because of pending termination.\n"));
break;
}
Log(("ssmR3StrmIoThread: postponing termination because of pending buffers.\n"));
}
{
}
}
}
else
{
/*
* Read until end of file, error or termination.
*/
for (;;)
{
{
Log(("ssmR3StrmIoThread: quitting reading because of pending termination.\n"));
break;
}
if ( RT_FAILURE(rc)
{
break;
}
{
break;
}
}
}
return VINF_SUCCESS;
}
/**
* Starts the I/O thread for the specified stream.
*
* @param pStrm The stream handle.
*/
{
int rc = RTThreadCreate(&hThread, ssmR3StrmIoThread, pStrm, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SSM-IO");
}
/**
* Works the progress calculation.
*
* @param pSSM The SSM handle.
* @param cbAdvance Number of bytes to advance
*/
{
/* Can't advance it beyond the estimated end of the unit. */
/* uPercentPrepare% prepare, xx% exec, uPercentDone% done+crc */
{
if (pSSM->pfnProgress)
}
}
/**
* Makes the SSM operation cancellable or not (via SSMR3Cancel).
*
* @param pVM The VM handle.
* @param pSSM The saved state handle. (SSMHANDLE::rc may be set.)
* @param fCancellable The new state.
*/
{
if (fCancellable)
{
}
else
{
if ( fCancelled == SSMHANDLE_CANCELLED
}
}
/**
* Finishes a data unit.
* All buffers and compressor instances are flushed and destroyed.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
*/
{
//Log2(("ssmR3DataWriteFinish: %#010llx start\n", ssmR3StrmTell(&pSSM->Strm)));
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
/**
* Begins writing the data of a data unit.
*
* Errors are signalled via pSSM->rc.
*
* @param pSSM The saved state handle.
*/
{
}
/**
* Writes a record to the current data item in the saved state file.
*
* @returns VBox status code. Sets pSSM->rc on failure.
* @param pSSM The saved state handle.
* @param pvBuf The bits to write.
* @param cbBuf The number of bytes to write.
*/
{
Log2(("ssmR3DataWriteRaw: %08llx|%08llx: pvBuf=%p cbBuf=%#x %.*Rhxs%s\n",
ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pvBuf, cbBuf, RT_MIN(cbBuf, SSM_LOG_BYTES), pvBuf, cbBuf > SSM_LOG_BYTES ? "..." : ""));
/*
* Check that everything is fine.
*/
/*
* Write the data item in 1MB chunks for progress indicator reasons.
*/
while (cbBuf > 0)
{
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/**
* Writes a record header for the specified amount of data.
*
* @returns VBox status code. Sets pSSM->rc on failure.
* @param pSSM The saved state handle
* @param cb The amount of data.
* @param u8TypeAndFlags The record type and flags.
*/
{
abHdr[0] = u8TypeAndFlags;
if (cb < 0x80)
{
cbHdr = 2;
}
else if (cb < 0x00000800)
{
cbHdr = 3;
}
else if (cb < 0x00010000)
{
cbHdr = 4;
}
else if (cb < 0x00200000)
{
cbHdr = 5;
}
else if (cb < 0x04000000)
{
cbHdr = 6;
}
else if (cb <= 0x7fffffff)
{
cbHdr = 7;
}
else
Log3(("ssmR3DataWriteRecHdr: %08llx|%08llx/%08x: Type=%02x fImportant=%RTbool cbHdr=%u\n",
ssmR3StrmTell(&pSSM->Strm) + cbHdr, pSSM->offUnit + cbHdr, cb, u8TypeAndFlags & SSM_REC_TYPE_MASK, !!(u8TypeAndFlags & SSM_REC_FLAGS_IMPORTANT), cbHdr));
}
/**
* Worker that flushes the buffered data.
*
* @returns VBox status code. Will set pSSM->rc on error.
* @param pSSM The saved state handle.
*/
{
/*
* Check how much there current is in the buffer.
*/
if (!cb)
/*
* Write a record header and then the data.
* (No need for fancy optimizations here any longer since the stream is
* fully buffered.)
*/
int rc = ssmR3DataWriteRecHdr(pSSM, cb, SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_RAW);
if (RT_SUCCESS(rc))
return rc;
}
/**
* ssmR3DataWrite worker that writes big stuff.
*
* @returns VBox status code
* @param pSSM The saved state handle.
* @param pvBuf The bits to write.
* @param cbBuf The number of bytes to write.
*/
{
if (RT_SUCCESS(rc))
{
/*
* Split it up into compression blocks.
*/
for (;;)
{
if ( cbBuf >= SSM_ZIP_BLOCK_SIZE
|| !ASMMemIsZeroPage(pvBuf))
)
{
/*
* Compress it.
*/
if (RT_FAILURE(rc))
break;
if (RT_SUCCESS(rc))
{
cbRec += 1;
}
else
{
}
if (RT_FAILURE(rc))
break;
/* advance */
if (cbBuf == SSM_ZIP_BLOCK_SIZE)
return VINF_SUCCESS;
}
else if (cbBuf >= SSM_ZIP_BLOCK_SIZE)
{
/*
* Zero block.
*/
Log3(("ssmR3DataWriteBig: %08llx|%08llx/%08x: ZERO\n", ssmR3StrmTell(&pSSM->Strm) + 2, pSSM->offUnit + 2, 1));
if (RT_FAILURE(rc))
break;
/* advance */
if (cbBuf == SSM_ZIP_BLOCK_SIZE)
return VINF_SUCCESS;
}
else
{
/*
* Less than one block left, store it the simple way.
*/
rc = ssmR3DataWriteRecHdr(pSSM, cbBuf, SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_RAW);
if (RT_SUCCESS(rc))
break;
}
}
}
return rc;
}
/**
* ssmR3DataWrite worker that is called when there isn't enough room in the
* buffer for the current chunk of data.
*
* This will first flush the buffer and then add the new bits to it.
*
* @returns VBox status code
* @param pSSM The saved state handle.
* @param pvBuf The bits to write.
* @param cbBuf The number of bytes to write.
*/
{
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Writes data to the current data unit.
*
* This is an inlined wrapper that optimizes the small writes that so many of
* the APIs make.
*
* @returns VBox status code
* @param pSSM The saved state handle.
* @param pvBuf The bits to write.
* @param cbBuf The number of bytes to write.
*/
{
if (!cbBuf)
return VINF_SUCCESS;
return VINF_SUCCESS;
}
/**
* Puts a structure.
*
* @returns VBox status code.
* @param pSSM The saved state handle.
* @param pvStruct The structure address.
* @param paFields The array of structure fields descriptions.
* The array must be terminated by a SSMFIELD_ENTRY_TERM().
*/
{
/* begin marker. */
if (RT_FAILURE(rc))
return rc;
/* put the fields */
pCur++)
{
{
break;
case SSMFIELDTRANS_GCPTR:
break;
case SSMFIELDTRANS_GCPHYS:
break;
default:
}
}
/* end marker */
}
/**
* Saves a boolean item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param fBool Item to save.
*/
{
}
/**
* Saves a 8-bit unsigned integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u8 Item to save.
*/
{
}
/**
* Saves a 8-bit signed integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param i8 Item to save.
*/
{
}
/**
* Saves a 16-bit unsigned integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u16 Item to save.
*/
{
}
/**
* Saves a 16-bit signed integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param i16 Item to save.
*/
{
}
/**
* Saves a 32-bit unsigned integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u32 Item to save.
*/
{
}
/**
* Saves a 32-bit signed integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param i32 Item to save.
*/
{
}
/**
* Saves a 64-bit unsigned integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u64 Item to save.
*/
{
}
/**
* Saves a 64-bit signed integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param i64 Item to save.
*/
{
}
/**
* Saves a 128-bit unsigned integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u128 Item to save.
*/
{
}
/**
* Saves a 128-bit signed integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param i128 Item to save.
*/
{
}
/**
* Saves a VBox unsigned integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u Item to save.
*/
{
return ssmR3DataWrite(pSSM, &u, sizeof(u));
}
/**
* Saves a VBox signed integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param i Item to save.
*/
{
return ssmR3DataWrite(pSSM, &i, sizeof(i));
}
/**
* Saves a GC natural unsigned integer item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u Item to save.
*
* @deprecated Silly type, don't use it.
*/
{
return ssmR3DataWrite(pSSM, &u, sizeof(u));
}
/**
* Saves a GC unsigned integer register item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param u Item to save.
*/
{
return ssmR3DataWrite(pSSM, &u, sizeof(u));
}
/**
* Saves a 32 bits GC physical address item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param GCPhys The item to save
*/
{
}
/**
* Saves a 64 bits GC physical address item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param GCPhys The item to save
*/
{
}
/**
* Saves a GC physical address item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param GCPhys The item to save
*/
{
}
/**
* Saves a GC virtual address item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param GCPtr The item to save.
*/
{
}
/**
* Saves an RC virtual address item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param RCPtr The item to save.
*/
{
}
/**
* Saves a GC virtual address (represented as an unsigned integer) item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param GCPtr The item to save.
*/
{
}
/**
* Saves a I/O port address item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param IOPort The item to save.
*/
{
}
/**
* Saves a selector item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param Sel The item to save.
*/
{
}
/**
* Saves a memory item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pv Item to save.
* @param cb Size of the item.
*/
{
}
/**
* Saves a zero terminated string item to the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param psz Item to save.
*/
{
{
AssertMsgFailed(("a %d byte long string, what's this!?!\n"));
return VERR_TOO_MUCH_DATA;
}
if (rc)
return rc;
}
/**
* Worker for SSMR3LiveDone and SSMR3Save that closes the handle and deletes the
* saved state file on failure.
*
* @returns VBox status code (pSSM->rc).
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
/*
* Make it non-cancellable, close the stream and delete the file on failure.
*/
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
if (pSSM->pfnProgress)
LogRel(("SSM: Successfully saved the VM state to '%s'\n",
}
else
{
if (pSSM->pszFilename)
{
if (RT_SUCCESS(rc2))
LogRel(("SSM: Failed to save the VM state to '%s' (file deleted): %Rrc\n",
else
LogRel(("SSM: Failed to save the VM state to '%s' (file deletion failed, rc2=%Rrc): %Rrc\n",
}
else
LogRel(("SSM: Failed to save the VM state.\n"));
}
/*
* Trash the handle before freeing it.
*/
return rc;
}
/**
* Closes the SSM handle.
*
* This must always be called on a handled returned by SSMR3LiveSave.
*
* @returns VBox status.
*
* @param pSSM The SSM handle returned by SSMR3LiveSave.
*
* @thread EMT(0).
*/
{
/*
* Validate input.
*/
/*
* Join paths with SSMR3Save again.
*/
}
/**
* Do the pfnSaveDone run.
*
* @returns VBox status code (pSSM->rc).
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
/*
* Do the done run.
*/
{
{
int rc;
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
if (RT_FAILURE(rc))
{
}
}
}
}
/**
* Writes the directory.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pSSM The SSM handle.
* @param pcEntries Where to return the number of directory entries.
*/
{
/*
* Grab some temporary memory for the dictionary.
*/
if (!pDir)
{
return VERR_NO_TMP_MEMORY;
}
/*
* Initialize it.
*/
{
}
/*
* Calculate the actual size and CRC-32, then write the directory
* out to the stream.
*/
return rc;
}
/**
* Finalize the saved state stream, i.e. add the end unit, directory
* and footer.
*
* @returns VBox status code (pSSM->rc).
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
/*
* Write the end unit.
*/
UnitHdr.u32Version = 0;
UnitHdr.u32Instance = 0;
if (RT_FAILURE(rc))
{
}
/*
* Write the directory for the final units and then the footer.
*/
if (RT_FAILURE(rc))
{
}
Footer.u32Reserved = 0;
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
}
LogRel(("SSM: Footer at %#llx (%lld), %u directory entries.\n",
return VINF_SUCCESS;
}
/**
* Do the pfnSaveExec run.
*
* @returns VBox status code (pSSM->rc).
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
{
/*
* Not all unit have a callback. Skip those which don't and
* make sure to keep the progress indicator up to date.
*/
{
continue;
}
/*
* Check for cancellation.
*/
{
LogRel(("SSM: Cancelled!\n"));
}
/*
* Write data unit header
*/
Log(("SSM: Unit at %#9llx: '%s', instance %u, pass %#x, version %u\n",
int rc = ssmR3StrmWrite(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]));
if (RT_FAILURE(rc))
{
}
/*
* Call the execute handler.
*/
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
else
if (RT_FAILURE(rc))
{
LogRel(("SSM: Execute save failed with rc=%Rrc for data unit '%s'/#%u.\n", rc, pUnit->szName, pUnit->u32Instance));
return rc;
}
/*
* Write the termination record and flush the compression stream.
*/
{
}
else
{
TermRec.u32StreamCRC = 0;
}
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
}
/*
* Advance the progress indicator to the end of the current unit.
*/
} /* for each unit */
/* (progress should be pending 99% now) */
return VINF_SUCCESS;
}
/**
* Do the pfnSavePrep run.
*
* @returns VBox status code (pSSM->rc).
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
{
{
int rc;
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
else
if (RT_FAILURE(rc))
{
return rc;
}
}
}
/*
* Work the progress indicator if we got one.
*/
if (pSSM->pfnProgress)
return VINF_SUCCESS;
}
/**
* Common worker for SSMR3Save and SSMR3LiveSave.
*
* @returns VBox status code (no need to check pSSM->rc).
* @param pVM The VM handle.
* @param pSSM The state handle.
*
* @thread EMT(0)
*/
{
/*
* Do the work.
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
return rc;
}
/**
* Saves the rest of the state on EMT0.
*
* @returns VBox status.
*
* @param pSSM The SSM handle returned by SSMR3LiveSave.
*
* @thread Non-EMT thread. Will involve the EMT at the end of the operation.
*/
{
/*
* Validate input.
*/
/*
* Join paths with VMMR3Save.
*/
}
/**
* Writes the file header and clear the per-unit data.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pSSM The SSM handle.
*/
{
/*
* Write the header.
*/
FileHdr.u8Reserved = 0;
if (RT_FAILURE(rc))
return rc;
/*
* Clear the per unit flags and offsets.
*/
{
}
return VINF_SUCCESS;
}
/**
* Creates a new saved state file.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pszFilename The name of the file. NULL if pStreamOps is
* used.
* @param pStreamOps The stream methods. NULL if pszFilename is
* used.
* @param pvStreamOpsUser The user argument to the stream methods.
* @param enmAfter What to do afterwards.
* @param pfnProgress The progress callback.
* @param pvProgressUser The progress callback user argument.
* @param ppSSM Where to return the pointer to the saved state
* handle upon successful return. Free it using
* RTMemFree after closing the stream.
*/
static int ssmR3SaveDoCreateFile(PVM pVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
{
if (!pSSM)
return VERR_NO_MEMORY;
pSSM->cbUnitLeftV1 = 0;
pSSM->offEstProgress = 0;
pSSM->cbEstTotal = 0;
pSSM->offEstUnitEnd = 0;
pSSM->uPercentPrepare = 0;
pSSM->uPercentDone = 0;
int rc;
if (pStreamOps)
rc = ssmR3StrmInit(&pSSM->Strm, pStreamOps, pvStreamOpsUser, true /*fWrite*/, true /*fChecksummed*/, 8 /*cBuffers*/);
else
rc = ssmR3StrmOpenFile(&pSSM->Strm, pszFilename, true /*fWrite*/, true /*fChecksummed*/, 8 /*cBuffers*/);
if (RT_FAILURE(rc))
{
return rc;
}
return VINF_SUCCESS;
}
/**
* Start VM save operation.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pszFilename Name of the file to save the state in.
* @param enmAfter What is planned after a successful save operation.
* @param pfnProgress Progress callback. Optional.
* @param pvUser User argument for the progress callback.
*
* @thread EMT
*/
VMMR3DECL(int) SSMR3Save(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser)
{
LogFlow(("SSMR3Save: pszFilename=%p:{%s} enmAfter=%d pfnProgress=%p pvUser=%p\n", pszFilename, pszFilename, enmAfter, pfnProgress, pvUser));
/*
* Validate input.
*/
|| enmAfter == SSMAFTER_CONTINUE,
("%d\n", enmAfter),
/*
* Create the saved state file and handle.
*
* Note that there might be quite some work to do after executing the saving,
* so we reserve 20% for the 'Done' period.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Write the saved state stream header and join paths with
* the other save methods for the rest of the job.
*/
if (RT_SUCCESS(rc))
{
}
}
/**
* Calls pfnLiveVote for all units.
*
* @returns VBox status code (no need to check pSSM->rc).
* @retval VINF_SUCCESS if we can pass on to step 2.
* @retval VINF_SSM_VOTE_FOR_ANOTHER_PASS if we need another pass.
*
* @param pVM The VM handle.
* @param pSSM The saved state handle.
* @param uPass The current pass.
*/
{
int rcRet = VINF_SUCCESS;
{
{
int rc;
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
if (rc != VINF_SUCCESS)
{
if (rc == VINF_SSM_VOTE_FOR_ANOTHER_PASS)
{
Log(("ssmR3DoLiveVoteRun: '%s'/#%u -> VINF_SSM_VOTE_FOR_ANOTHER_PASS (pass=%u)\n", pUnit->szName, pUnit->u32Instance, uPass));
}
else if (rc == VINF_SSM_VOTE_DONE_DONT_CALL_AGAIN)
{
Log(("ssmR3DoLiveVoteRun: '%s'/#%u -> VINF_SSM_VOTE_DONE_DONT_CALL_AGAIN (pass=%u)\n", pUnit->szName, pUnit->u32Instance, uPass));
}
else
{
/*
* rc is usually VERR_SSM_VOTE_FOR_GIVING_UP here, but we allow
* other status codes for better user feed back. However, no
* other non-error status is allowed.
*/
LogRel(("SSM: Error - '%s'/#%u voted %Rrc! (pass=%u)\n", pUnit->szName, pUnit->u32Instance, rc, uPass));
AssertMsgReturn(RT_FAILURE(rc), ("%Rrc; '%s'\n", rc, pUnit->szName), pSSM->rc = VERR_IPE_UNEXPECTED_INFO_STATUS);
}
}
}
}
if (rcRet == VINF_SUCCESS)
return rcRet;
}
/**
* Calls pfnLiveExec for all units.
*
* @returns VBox status code (no need to check pSSM->rc).
*
* @param pVM The VM handle.
* @param pSSM The saved state handle.
* @param uPass The current pass.
*/
{
/** @todo VMR3GetState(pVM) == VMSTATE_LIVE_SAVING &&*/ pUnit;
{
/*
* Skip units without a callback (this is most).
*/
continue;
/*
* Check for cancellation.
*/
{
LogRel(("SSM: Cancelled!\n"));
}
/*
* Write data unit header.
*/
Log(("SSM: Unit at %#9llx: '%s', instance %u, pass %#x, version %u\n",
int rc = ssmR3StrmWrite(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]));
if (RT_FAILURE(rc))
{
}
/*
* Call the execute handler.
*/
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
else
{
if (rc == VINF_SSM_DONT_CALL_AGAIN)
}
if (RT_FAILURE(rc))
{
LogRel(("SSM: Execute save failed with rc=%Rrc for data unit '%s'/#%u.\n", rc, pUnit->szName, pUnit->u32Instance));
return rc;
}
/*
* Write the termination record and flush the compression stream.
*/
{
}
else
{
TermRec.u32StreamCRC = 0;
}
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
{
}
} /* for each unit */
return VINF_SUCCESS;
}
/**
* Continue a live state saving operation on the worker thread.
*
* @returns VBox status.
*
* @param pSSM The SSM handle returned by SSMR3LiveSave.
*
* @thread Non-EMT thread. Will involve the EMT at the end of the operation.
*/
{
/*
* Validate input.
*/
/*
* Calc the max saved state size before we should give up because of insane
* amounts of data.
*/
#define SSM_MAX_GROWTH_FILE 10000
#define SSM_MAX_GROWTH_REMOTE 100000
AssertLogRelMsgReturn(cbMax > cbSum, ("cbMax=%#RX64, cbSum=%#RX64\n", cbMax, cbSum), pSSM->rc = VERR_OUT_OF_RANGE);
/*
* The pass loop.
*
* The number of interations is restricted for two reasons, first
* to make sure
*/
#define SSM_MAX_PASSES _1M
{
/*
* Save state and vote on whether we need more passes or not.
*/
if (RT_FAILURE(rc))
return rc;
if (rc == VINF_SUCCESS)
{
return VINF_SUCCESS;
}
if (RT_FAILURE(rc))
return rc;
/*
* Check that we're still within sane data amounts.
*/
{
LogRel(("SSM: Giving up: Exceeded max state size. (cbSaved=%#RX64, cbMax=%#RX64)\n", cbSaved, cbMax));
}
/*
* Check that there is still some space left on the disk.
*/
/** @todo move this to the stream flushing code? It's not perfect when done
* here, it could be way better if we did it there. */
if (pSSM->pszFilename)
{
if ( RT_SUCCESS(rc)
&& cbFree < SSM_MIN_DISK_FREE)
{
LogRel(("SSM: Giving up: Low on disk space. (cbFree=%RTfoff, SSM_MIN_DISK_FREE=%RTfoff).\n",
}
}
#if 0 /** @todo check this out... */
/*
* Check the VM state to see if it has changed.
*/
if (enmState != VMSTATE_LIVE_SAVING)
{
switch (enmState)
{
case VMSTATE_LIVE_CANCELLED:
LogRel(("SSM: Cancelled\n"));
case VMSTATE_LIVE_POWERED_OFF:
LogRel(("SSM: Powered off, no state to save, aborting.\n"));
case VMSTATE_GURU_MEDITATION:
LogRel(("SSM: Guru meditation, aborting.\n"));
default:
}
}
#endif
}
}
/**
* Calls pfnLivePrep for all units.
*
* @returns VBox status code (no need to check pSSM->rc).
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
/*
* Do the prepare run.
*/
{
{
int rc;
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
else
if (RT_FAILURE(rc))
{
return rc;
}
}
}
/*
* Work the progress indicator if we got one.
*/
if (pSSM->pfnProgress)
return VINF_SUCCESS;
}
/**
* Start saving the live state.
*
* Call SSMR3LiveDoStep1, SSMR3LiveDoStep2 and finally SSMR3LiveDone on success.
* SSMR3LiveDone should be called even if SSMR3LiveDoStep1 or SSMR3LiveDoStep2
* fails.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pszFilename Name of the file to save the state in. This string
* must remain valid until SSMR3LiveDone is called.
* Must be NULL if pStreamOps is used.
* @param pStreamOps The stream method table. NULL if pszFilename is
* used.
* @param pvStreamOpsUser The user argument to the stream methods.
* @param enmAfter What is planned after a successful save operation.
* @param pfnProgress Progress callback. Optional.
* @param pvProgressUser User argument for the progress callback.
*
* @thread EMT0
*/
VMMR3_INT_DECL(int) SSMR3LiveSave(PVM pVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
{
LogFlow(("SSMR3LiveSave: pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p enmAfter=%d pfnProgress=%p pvProgressUser=%p\n",
/*
* Validate input.
*/
|| enmAfter == SSMAFTER_CONTINUE
|| enmAfter == SSMAFTER_MIGRATE,
("%d\n", enmAfter),
if (pStreamOps)
{
}
/*
* Create the saved state file and handle.
*
* Note that there might be quite some work to do after executing the saving,
* so we reserve 20% for the 'Done' period.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Write the saved state stream header and do the prep run for live saving.
*/
if (RT_SUCCESS(rc))
{
/** @todo If it turns out we don't need to do ssmR3DoLivePrepRun on EMT0,
* simply move the code to SSMR3LiveDoStep1.
* Update: This is certinaly the case, move it. */
if (RT_SUCCESS(rc))
{
/*
* Return and let the requstor thread do the pfnLiveExec/Vote part
* via SSMR3SaveFinishLive
*/
return VINF_SUCCESS;
}
}
/* bail out. */
return rc;
}
invent stream interface and stuff */)
{
return VERR_NOT_IMPLEMENTED;
}
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/* ... Loading and reading starts here ... */
/**
* Closes the decompressor of a data unit.
*
* @returns pSSM->rc.
* @param pSSM SSM operation handle.
*/
{
{
}
}
/**
* Callback for reading compressed data into the input buffer of the
* decompressor, for saved file format version 1.
*
* @returns VBox status code.
* @param pvSSM The SSM handle.
* @param pvBuf Where to store the compressed data.
* @param cbBuf Size of the buffer.
* @param pcbRead Number of bytes actually stored in the buffer.
*/
{
if (cbRead)
{
//Log2(("ssmR3ReadInV1: %#010llx cbBug=%#x cbRead=%#x\n", ssmR3StrmTell(&pSSM->Strm), cbBuf, cbRead));
if (RT_SUCCESS(rc))
{
if (pcbRead)
return VINF_SUCCESS;
}
return rc;
}
AssertMsgFailed(("SSM: attempted reading more than the unit!\n"));
return VERR_SSM_LOADED_TOO_MUCH;
}
/**
* Internal read worker for reading data from a version 1 unit.
*
* @param pSSM SSM operation handle.
* @param pvBuf Where to store the read data.
* @param cbBuf Number of bytes to read.
*/
{
/*
* Open the decompressor on the first read.
*/
{
}
/*
* Do the requested read.
*/
if (RT_SUCCESS(rc))
{
Log2(("ssmR3DataRead: pvBuf=%p cbBuf=%#x offUnit=%#llx %.*Rhxs%s\n", pvBuf, cbBuf, pSSM->offUnit, RT_MIN(cbBuf, SSM_LOG_BYTES), pvBuf, cbBuf > SSM_LOG_BYTES ? "..." : ""));
return VINF_SUCCESS;
}
return rc;
}
/**
* Creates the decompressor for the data unit.
*
* pSSM->rc will be set on error.
*
* @param pSSM SSM operation handle.
*/
{
}
/**
* Checks for the termination record and closes the decompressor.
*
* pSSM->rc will be set on error.
*
* @returns pSSM->rc.
* @param pSSM SSM operation handle.
*/
{
/*
* If we haven't encountered the end of the record, it must be the next one.
*/
&& RT_SUCCESS(rc))
{
if ( RT_SUCCESS(rc)
{
AssertFailed();
}
}
return rc;
}
/**
* Read reader that keep works the progress indicator and unit offset.
*
* Does not set SSM::rc.
*
* @returns VBox status code.
* @param pSSM The saved state handle.
* @param pvBuf Where to put the bits
* @param cbBuf How many bytes to read.
*/
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
/** @todo weed out lazy saving */
AssertMsgFailed(("SSM: attempted reading more than the unit!\n"));
return VERR_SSM_LOADED_TOO_MUCH;
}
/**
* Reads and checks the LZF "header".
*
* @returns VBox status code.
* @param pSSM The saved state handle..
* @param pcbDecompr Where to store the size of the decompressed data.
*/
{
*pcbDecompr = 0; /* shuts up gcc. */
if (RT_FAILURE(rc))
return rc;
("%#x\n", cbDecompr),
*pcbDecompr = cbDecompr;
return VINF_SUCCESS;
}
/**
* Reads an LZF block from the stream and decompresses into the specified
* buffer.
*
* @returns VBox status code.
* @param SSM The saved state handle.
* @param pvDst Pointer to the output buffer.
* @param cbDecompr The size of the decompressed data.
*/
{
int rc;
/*
* Try use the stream buffer directly to avoid copying things around.
*/
if (pb)
{
}
else
{
if (RT_FAILURE(rc))
return rc;
}
/*
* Decompress it.
*/
if (RT_SUCCESS(rc))
{
AssertLogRelMsgReturn(cbDstActual == cbDecompr, ("%#x %#x\n", cbDstActual, cbDecompr), VERR_SSM_INTEGRITY_DECOMPRESSION);
return VINF_SUCCESS;
}
return VERR_SSM_INTEGRITY_DECOMPRESSION;
}
/**
* Reads and checks the raw zero "header".
*
* @returns VBox status code.
* @param pSSM The saved state handle..
* @param pcbDecompr Where to store the size of the zero data.
*/
{
*pcbZero = 0; /* shuts up gcc. */
AssertLogRelMsgReturn(pSSM->u.Read.cbRecLeft == 1, ("%#x\n", pSSM->u.Read.cbRecLeft), VERR_SSM_INTEGRITY_DECOMPRESSION);
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
/**
* Worker for reading the record header.
*
* It sets pSSM->u.Read.cbRecLeft, pSSM->u.Read.u8TypeAndFlags and
* pSSM->u.Read.fEndOfData. When a termination record is encounter, it will be
* read in full and validated, the fEndOfData indicator is set, and VINF_SUCCESS
* is returned.
*
* @returns VBox status code.
* @param pSSM The saved state handle.
*/
{
/*
* Read the two mandatory bytes.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Validate the first byte and check for the termination records.
*/
AssertLogRelMsgReturn(SSM_REC_ARE_TYPE_AND_FLAGS_VALID(abHdr[0]), ("%#x %#x\n", abHdr[0], abHdr[1]), VERR_SSM_INTEGRITY_REC_HDR);
{
AssertLogRelMsgReturn(abHdr[1] == sizeof(SSMRECTERM) - 2, ("%#x\n", abHdr[1]), VERR_SSM_INTEGRITY_REC_TERM);
AssertLogRelMsgReturn(abHdr[0] & SSM_REC_FLAGS_IMPORTANT, ("%#x\n", abHdr[0]), VERR_SSM_INTEGRITY_REC_TERM);
/* get the rest */
if (RT_FAILURE(rc))
return rc;
/* validate integrity */
AssertLogRelMsgReturn(!(TermRec.fFlags & ~SSMRECTERM_FLAGS_CRC32), ("%#x\n", TermRec.fFlags), VERR_SSM_INTEGRITY_REC_TERM);
AssertLogRelMsgReturn(TermRec.u32StreamCRC == 0, ("%#x\n", TermRec.u32StreamCRC), VERR_SSM_INTEGRITY_REC_TERM);
AssertLogRelMsgReturn(TermRec.u32StreamCRC == u32StreamCRC, ("%#x, %#x\n", TermRec.u32StreamCRC, u32StreamCRC),
Log3(("ssmR3DataReadRecHdrV2: %08llx|%08llx: TERM\n", ssmR3StrmTell(&pSSM->Strm) - sizeof(SSMRECTERM), pSSM->offUnit));
return VINF_SUCCESS;
}
/*
* Figure the size. The 2nd byte is encoded in UTF-8 fashion, so this
* is can be highly enjoyable.
*/
if (!(cb & 0x80))
else
{
/*
* Need more data. Figure how much and read it.
*/
cb = 2;
cb = 3;
cb = 4;
cb = 5;
cb = 6;
else
if (RT_FAILURE(rc))
return rc;
/*
* Validate what we've read.
*/
switch (cb)
{
case 6:
AssertLogRelMsgReturn((abHdr[6] & 0xc0) == 0x80, ("6/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
case 5:
AssertLogRelMsgReturn((abHdr[5] & 0xc0) == 0x80, ("5/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
case 4:
AssertLogRelMsgReturn((abHdr[4] & 0xc0) == 0x80, ("4/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
case 3:
AssertLogRelMsgReturn((abHdr[3] & 0xc0) == 0x80, ("3/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
case 2:
AssertLogRelMsgReturn((abHdr[2] & 0xc0) == 0x80, ("2/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
break;
default:
return VERR_INTERNAL_ERROR;
}
/*
* Decode it and validate the range.
*/
switch (cb)
{
case 6:
AssertLogRelMsgReturn(cb >= 0x04000000 && cb <= 0x7fffffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
break;
case 5:
AssertLogRelMsgReturn(cb >= 0x00200000 && cb <= 0x03ffffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
break;
case 4:
AssertLogRelMsgReturn(cb >= 0x00010000 && cb <= 0x001fffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
break;
case 3:
#if 0 /* disabled to optimize buffering */
AssertLogRelMsgReturn(cb >= 0x00000800 && cb <= 0x0000ffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
#endif
break;
case 2:
#if 0 /* disabled to optimize buffering */
AssertLogRelMsgReturn(cb >= 0x00000080 && cb <= 0x000007ff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
#endif
break;
default:
return VERR_INTERNAL_ERROR;
}
}
Log3(("ssmR3DataReadRecHdrV2: %08llx|%08llx/%08x: Type=%02x fImportant=%RTbool cbHdr=%u\n",
return VINF_SUCCESS;
}
/**
* Buffer miss, do an unbuffered read.
*
* @param pSSM SSM operation handle.
* @param pvBuf Where to store the read data.
* @param cbBuf Number of bytes to read.
*/
{
/*
* Copy out what we've got in the buffer.
*/
Log4(("ssmR3DataReadUnbufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x\n", ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, cbInBuffer, cbBufOrg));
if (cbInBuffer > 0)
{
}
/*
* Read data.
*/
do
{
/*
* Read the next record header if no more data.
*/
{
if (RT_FAILURE(rc))
}
AssertLogRelMsgReturn(!pSSM->u.Read.fEndOfData, ("cbBuf=%zu", cbBuf), pSSM->rc = VERR_SSM_LOADED_TOO_MUCH);
/*
* Read data from the current record.
*/
{
case SSM_REC_TYPE_RAW:
{
if (RT_FAILURE(rc))
break;
}
case SSM_REC_TYPE_RAW_LZF:
{
if (RT_FAILURE(rc))
return rc;
{
if (RT_FAILURE(rc))
return rc;
}
else
{
/* The output buffer is too small, use the data buffer. */
if (RT_FAILURE(rc))
return rc;
}
break;
}
case SSM_REC_TYPE_RAW_ZERO:
{
if (RT_FAILURE(rc))
return rc;
{
/* Spill the remainer into the data buffer. */
}
break;
}
default:
}
} while (cbBuf > 0);
Log4(("ssmR3DataReadUnBufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x %.*Rhxs%s\n",
ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, 0, cbBufOrg, RT_MIN(SSM_LOG_BYTES, cbBufOrg), pvBufOrg, cbBufOrg > SSM_LOG_BYTES ? "..." : ""));
return VINF_SUCCESS;
}
/**
* Buffer miss, do a buffered read.
*
* @param pSSM SSM operation handle.
* @param pvBuf Where to store the read data.
* @param cbBuf Number of bytes to read.
*/
{
/*
* Copy out what we've got in the buffer.
*/
Log4(("ssmR3DataReadBufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x\n", ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, cbInBuffer, cbBufOrg));
if (cbInBuffer > 0)
{
}
/*
* Buffer more data.
*/
do
{
/*
* Read the next record header if no more data.
*/
{
if (RT_FAILURE(rc))
}
AssertLogRelMsgReturn(!pSSM->u.Read.fEndOfData, ("cbBuf=%zu", cbBuf), pSSM->rc = VERR_SSM_LOADED_TOO_MUCH);
/*
* Read data from the current record.
* LATER: optimize by reading directly into the output buffer for some cases.
*/
{
case SSM_REC_TYPE_RAW:
{
if (RT_FAILURE(rc))
break;
}
case SSM_REC_TYPE_RAW_LZF:
{
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
break;
}
case SSM_REC_TYPE_RAW_ZERO:
{
if (RT_FAILURE(rc))
return rc;
break;
}
default:
}
/*pSSM->u.Read.offDataBuffer = 0;*/
/*
* Copy data from the buffer.
*/
} while (cbBuf > 0);
Log4(("ssmR3DataReadBufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x %.*Rhxs%s\n",
ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, pSSM->u.Read.cbDataBuffer - pSSM->u.Read.offDataBuffer,
return VINF_SUCCESS;
}
/**
* Inlined worker that handles format checks and buffered reads.
*
* @param pSSM SSM operation handle.
* @param pvBuf Where to store the read data.
* @param cbBuf Number of bytes to read.
*/
{
/*
* Fend off previous errors and V1 data units.
*/
/*
* Check if the requested data is buffered.
*/
{
}
? "ssmR3DataRead: %08llx|%08llx/%08x/%08x: cbBuf=%#x %.*Rhxs%s\n"
: "ssmR3DataRead: %08llx|%08llx/%08x/%08x: cbBuf=%#x\n",
ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, pSSM->u.Read.cbDataBuffer - pSSM->u.Read.offDataBuffer,
return VINF_SUCCESS;
}
/**
* Gets a structure.
*
* @returns VBox status code.
* @param pSSM The saved state handle.
* @param pvStruct The structure address.
* @param paFields The array of structure fields descriptions.
* The array must be terminated by a SSMFIELD_ENTRY_TERM().
*/
{
/* begin marker. */
if (RT_FAILURE(rc))
return rc;
if (u32Magic != SSMR3STRUCT_BEGIN)
/* get the fields */
pCur++)
{
{
break;
case SSMFIELDTRANS_GCPTR:
break;
case SSMFIELDTRANS_GCPHYS:
break;
default:
}
}
/* end marker */
if (RT_FAILURE(rc))
return rc;
if (u32Magic != SSMR3STRUCT_END)
return rc;
}
/**
* Loads a boolean item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pfBool Where to store the item.
*/
{
if (RT_SUCCESS(rc))
{
}
return rc;
}
/**
* Loads a 8-bit unsigned integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu8 Where to store the item.
*/
{
}
/**
* Loads a 8-bit signed integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pi8 Where to store the item.
*/
{
}
/**
* Loads a 16-bit unsigned integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu16 Where to store the item.
*/
{
}
/**
* Loads a 16-bit signed integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pi16 Where to store the item.
*/
{
}
/**
* Loads a 32-bit unsigned integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu32 Where to store the item.
*/
{
}
/**
* Loads a 32-bit signed integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pi32 Where to store the item.
*/
{
}
/**
* Loads a 64-bit unsigned integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu64 Where to store the item.
*/
{
}
/**
* Loads a 64-bit signed integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pi64 Where to store the item.
*/
{
}
/**
* Loads a 128-bit unsigned integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu128 Where to store the item.
*/
{
}
/**
* Loads a 128-bit signed integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pi128 Where to store the item.
*/
{
}
/**
* Loads a VBox unsigned integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu Where to store the integer.
*/
{
}
/**
* Loads a VBox signed integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pi Where to store the integer.
*/
{
}
/**
* Loads a GC natural unsigned integer item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu Where to store the integer.
*
* @deprecated Silly type with an incorrect size, don't use it.
*/
{
}
/**
* Loads a GC unsigned integer register item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pu Where to store the integer.
*/
{
}
/**
* Loads a 32 bits GC physical address item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pGCPhys Where to store the GC physical address.
*/
{
}
/**
* Loads a 64 bits GC physical address item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pGCPhys Where to store the GC physical address.
*/
{
}
/**
* Loads a GC physical address item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pGCPhys Where to store the GC physical address.
*/
{
/*
* Default size?
*/
/*
* Fiddly.
*/
{
/* 64-bit saved, 32-bit load: try truncate it. */
if (RT_FAILURE(rc))
return rc;
return VERR_SSM_GCPHYS_OVERFLOW;
return rc;
}
/* 32-bit saved, 64-bit load: clear the high part. */
*pGCPhys = 0;
}
/**
* Loads a GC virtual address item from the current data unit.
*
* Only applies to in the 1.1 format:
* - SSMR3GetGCPtr
* - SSMR3GetGCUIntPtr
* - SSMR3GetGCUInt
* - SSMR3GetGCUIntReg
*
* Put functions are not affected.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param cbGCPtr Size of RTGCPTR
*
* @remarks This interface only works with saved state version 1.1, if the
* format isn't 1.1 the call will be ignored.
*/
{
{
}
AssertMsgFailed(("SSMR3SetGCPtrSize: already fixed at %u bytes; requested %u bytes\n", pSSM->u.Read.cbGCPtr, cbGCPtr));
return VINF_SUCCESS;
}
/**
* Loads a GC virtual address item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pGCPtr Where to store the GC virtual address.
*/
{
/*
* Default size?
*/
/*
* Fiddly.
*/
{
/* 64-bit saved, 32-bit load: try truncate it. */
if (RT_FAILURE(rc))
return rc;
return VERR_SSM_GCPTR_OVERFLOW;
return rc;
}
/* 32-bit saved, 64-bit load: clear the high part. */
*pGCPtr = 0;
}
/**
* Loads a GC virtual address (represented as unsigned integer) item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pGCPtr Where to store the GC virtual address.
*/
{
}
/**
* Loads an RC virtual address item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pRCPtr Where to store the RC virtual address.
*/
{
}
/**
* Loads a I/O port address item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pIOPort Where to store the I/O port address.
*/
{
}
/**
* Loads a selector item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pSel Where to store the selector.
*/
{
}
/**
* Loads a memory item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param pv Where to store the item.
* @param cb Size of the item.
*/
{
}
/**
* Loads a string item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param psz Where to store the item.
* @param cbMax Max size of the item (including '\\0').
*/
{
}
/**
* Loads a string item from the current data unit.
*
* @returns VBox status.
* @param pSSM SSM operation handle.
* @param psz Where to store the item.
* @param cbMax Max size of the item (including '\\0').
* @param pcbStr The length of the loaded string excluding the '\\0'. (optional)
*/
{
/* read size prefix. */
if (RT_SUCCESS(rc))
{
if (pcbStr)
{
/* terminate and read string content. */
}
return VERR_TOO_MUCH_DATA;
}
return rc;
}
/**
* Skips a number of bytes in the current data unit.
*
* @returns VBox status code.
* @param pSSM The SSM handle.
* @param cb The number of bytes to skip.
*/
{
while (cb > 0)
{
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/**
* Skips to the end of the current data unit.
*
* Since version 2 of the format, the load exec callback have to explicitly call
* this API if it wish to be lazy for some reason. This is because there seldom
* is a good reason to not read your entire data unit and it was hiding bugs.
*
* @returns VBox status code.
* @param pSSM The saved state handle.
*/
{
{
/*
* Read until we the end of data condition is raised.
*/
{
do
{
/* read the rest of the current record */
{
if (RT_FAILURE(rc))
}
/* read the next header. */
if (RT_FAILURE(rc))
}
}
/* else: Doesn't matter for the version 1 loading. */
return VINF_SUCCESS;
}
/**
* Calculate the checksum of a file portion.
*
* @returns VBox status.
* @param pStrm The stream handle
* @param off Where to start checksumming.
* @param cb How much to checksum.
* @param pu32CRC Where to store the calculated checksum.
*/
{
/*
* Allocate a buffer.
*/
if (!pvBuf)
return VERR_NO_TMP_MEMORY;
/*
* Loop reading and calculating CRC32.
*/
int rc = VINF_SUCCESS;
while (cb > 0)
{
/* read chunk */
if (RT_FAILURE(rc))
{
return rc;
}
/* advance */
/* calc crc32. */
}
/* store the calculated crc */
return VINF_SUCCESS;
}
/**
* Validates a version 2 footer.
*
* @returns VBox status code.
*
* @param pFooter The footer.
* @param offFooter The stream offset of the footer.
* @param cDirEntries The number of directory entries. UINT32_MAX if
* unknown.
* @param fStreamCrc32 Whether the stream is checksummed using CRC-32.
* @param u32StreamCRC The stream checksum.
*/
static int ssmR3ValidateFooter(PSSMFILEFTR pFooter, uint64_t offFooter, uint32_t cDirEntries, bool fStreamCrc32, uint32_t u32StreamCRC)
{
{
return VERR_SSM_INTEGRITY_FOOTER;
}
SSM_CHECK_CRC32_RET(pFooter, sizeof(*pFooter), ("Footer CRC mismatch: %08x, correct is %08x\n", u32CRC, u32ActualCRC));
{
LogRel(("SSM: SSMFILEFTR::offStream is wrong: %llx, expected %llx\n", pFooter->offStream, offFooter));
return VERR_SSM_INTEGRITY_FOOTER;
}
if (pFooter->u32Reserved)
{
return VERR_SSM_INTEGRITY_FOOTER;
}
if (cDirEntries != UINT32_MAX)
else
if ( !fStreamCrc32
&& pFooter->u32StreamCRC)
{
LogRel(("SSM: u32StreamCRC field isn't zero, but header says stream checksumming is disabled.\n"));
return VERR_SSM_INTEGRITY_FOOTER;
}
if ( fStreamCrc32
{
return VERR_SSM_INTEGRITY_CRC;
}
return VINF_SUCCESS;
}
/**
* Validates the header information stored in the handle.
*
* @returns VBox status code.
*
* @param pSSM The handle.
* @param fHaveHostBits Set if the host bits field is valid.
* @param fHaveVersion Set if we have a version.
*/
{
if (fHaveVersion)
{
{
LogRel(("SSM: Incorrect version values: %u.%u.%u.r%u\n",
pSSM->u.Read.u16VerMajor, pSSM->u.Read.u16VerMinor, pSSM->u.Read.u32VerBuild, pSSM->u.Read.u32SvnRev));
return VERR_SSM_INTEGRITY_VBOX_VERSION;
}
}
else
if (fHaveHostBits)
{
{
return VERR_SSM_INTEGRITY_HEADER;
}
}
else
{
return VERR_SSM_INTEGRITY_HEADER;
}
{
return VERR_SSM_INTEGRITY_HEADER;
}
return VINF_SUCCESS;
}
/**
* Reads the header, detects the format version and performs integrity
* validations.
*
* @returns VBox status.
* @param pSSM The saved state handle. A number of field will
* be updated, mostly header related information.
* fLiveSave is also set if appropriate.
* @param fChecksumIt Whether to checksum the file or not. This will
* be ignored if it the stream isn't a file.
* @param fChecksumOnRead Whether to validate the checksum while reading
* the stream instead of up front. If not possible,
* verify the checksum up front.
* @param pHdr Where to store the file header.
*/
{
/*
* Read and check the header magic.
*/
union
{
} uHdr;
if (RT_FAILURE(rc))
{
return rc;
}
{
Log(("SSM: Not a saved state file. magic=%.*s\n", sizeof(uHdr.v2_0.szMagic) - 1, uHdr.v2_0.szMagic));
return VERR_SSM_INTEGRITY_MAGIC;
}
/*
* Find the header size and read the rest.
*/
static const struct
{
char szMagic[sizeof(SSMFILEHDR_MAGIC_V2_0)];
unsigned uFmtVerMajor;
unsigned uFmtVerMinor;
} s_aVers[] =
{
};
while (iVer-- > 0)
break;
if (iVer < 0)
{
Log(("SSM: Unknown file format version. magic=%.*s\n", sizeof(uHdr.v2_0.szMagic) - 1, uHdr.v2_0.szMagic));
return VERR_SSM_INTEGRITY_VERSION;
}
rc = ssmR3StrmRead(&pSSM->Strm, (uint8_t *)&uHdr + sizeof(uHdr.v2_0.szMagic), pSSM->u.Read.cbFileHdr - sizeof(uHdr.v2_0.szMagic));
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Make version specific adjustments.
*/
{
/*
* Version 2.0 and later.
*/
{
/* validate the header. */
SSM_CHECK_CRC32_RET(&uHdr.v2_0, sizeof(uHdr.v2_0), ("Header CRC mismatch: %08x, correct is %08x\n", u32CRC, u32ActualCRC));
{
return VERR_SSM_INTEGRITY;
}
{
return VERR_SSM_INTEGRITY;
}
{
return VERR_SSM_INTEGRITY;
}
/* set the header info. */
}
else
/*
* Read and validate the footer if it's a file.
*/
{
rc = ssmR3StrmPeekAt(&pSSM->Strm, -(RTFOFF)sizeof(SSMFILEFTR), &Footer, sizeof(Footer), &offFooter);
rc = ssmR3ValidateFooter(&Footer, offFooter, UINT32_MAX, pSSM->u.Read.fStreamCrc32, Footer.u32StreamCRC);
if (RT_FAILURE(rc))
return rc;
}
else
{
}
/*
* Validate the header info we've set in the handle.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Check the checksum if that's called for and possible.
*/
&& fChecksumIt
&& !fChecksumOnRead
{
if (RT_FAILURE(rc))
return rc;
{
LogRel(("SSM: Invalid CRC! Calculated %#010x, in footer %#010x\n", u32CRC, pSSM->u.Read.u32LoadCRC));
return VERR_SSM_INTEGRITY_CRC;
}
}
}
else
{
/*
* Version 1.x of the format.
*/
bool fHaveHostBits = true;
bool fHaveVersion = false;
{
fHaveHostBits = false;
}
{
fHaveVersion = true;
}
else
/*
* The MachineUuid must be NULL (was never used).
*/
if (!RTUuidIsNull(&MachineUuidFromHdr))
{
LogRel(("SSM: The UUID of the saved state doesn't match the running VM.\n"));
return VERR_SMM_INTEGRITY_MACHINE;
}
/*
* Verify the file size.
*/
{
LogRel(("SSM: File size mismatch. hdr.cbFile=%lld actual %lld\n", pSSM->u.Read.cbLoadFile, cbFile));
return VERR_SSM_INTEGRITY_SIZE;
}
/*
* Validate the header info we've set in the handle.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Verify the checksum if requested.
*
* Note! The checksum is not actually generated for the whole file,
* this is of course a bug in the v1.x code that we cannot do
* anything about.
*/
if ( fChecksumIt
|| fChecksumOnRead)
{
&u32CRC);
if (RT_FAILURE(rc))
return rc;
{
LogRel(("SSM: Invalid CRC! Calculated %#010x, in header %#010x\n", u32CRC, pSSM->u.Read.u32LoadCRC));
return VERR_SSM_INTEGRITY_CRC;
}
}
}
return VINF_SUCCESS;
}
/**
* Open a saved state for reading.
*
* The file will be positioned at the first data unit upon successful return.
*
* @returns VBox status code.
*
* @param pVM The VM handle.
* @param pszFilename The filename. NULL if pStreamOps is used.
* @param pStreamOps The stream method table. NULL if pszFilename is
* used.
* @param pvUser The user argument to the stream methods.
* @param fChecksumIt Check the checksum for the entire file.
* @param fChecksumOnRead Whether to validate the checksum while reading
* the stream instead of up front. If not possible,
* verify the checksum up front.
* @param pSSM Pointer to the handle structure. This will be
* completely initialized on success.
* @param cBuffers The number of stream buffers.
*/
{
/*
* Initialize the handle.
*/
pSSM->cbUnitLeftV1 = 0;
pSSM->offEstProgress = 0;
pSSM->cbEstTotal = 0;
pSSM->offEstUnitEnd = 0;
/*
* Try open and validate the file.
*/
int rc;
if (pStreamOps)
else
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
return rc;
/* failure path */
}
else
return rc;
}
/**
* Find a data unit by name.
*
* @returns Pointer to the unit.
* @returns NULL if not found.
*
* @param pVM VM handle.
* @param pszName Data unit name.
* @param uInstance The data unit instance id.
*/
{
while ( pUnit
return pUnit;
}
/**
* Executes the loading of a V1.X file.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
int rc;
for (;;)
{
/*
* Save the current file position and read the data unit header.
*/
if (RT_SUCCESS(rc))
{
/*
* Check the magic and see if it's valid and whether it is a end header or not.
*/
{
{
/* Complete the progress bar (pending 99% afterwards). */
break;
}
LogRel(("SSM: Invalid unit magic at offset %#llx (%lld), '%.*s'!\n",
break;
}
/*
* Read the name.
* Adjust the name buffer first.
*/
{
if (pszName)
}
if (pszName)
{
if (RT_SUCCESS(rc))
{
{
break;
}
/*
* Find the data unit in our internal table.
*/
if (pUnit)
{
/*
* Call the execute handler.
*/
{
break;
}
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
rc = pUnit->u.External.pfnLoadExec(pSSM, pUnit->u.External.pvUser, UnitHdr.u32Version, SSM_PASS_FINAL);
break;
default:
break;
}
/*
* Close the reader stream.
*/
if (RT_SUCCESS(rc))
{
/*
* Now, we'll check the current position to see if all, or
* more than all, the data was read.
*
* Note! Because of buffering / compression we'll only see the
* really bad ones here.
*/
if (i64Diff < 0)
{
}
else if (i64Diff > 0)
{
break;
}
}
else
{
LogRel(("SSM: Load exec failed for '%s' instance #%u ! (version %u)\n",
break;
}
}
else
{
/*
* SSM unit wasn't found - ignore this when loading for the debugger.
*/
break;
}
}
}
else
}
/*
* I/O errors ends up here (yea, I know, very nice programming).
*/
if (RT_FAILURE(rc))
{
break;
}
/*
* Check for cancellation.
*/
{
LogRel(("SSM: Cancelled!n"));
break;
}
}
return rc;
}
/**
* Verifies the directory.
*
* @returns VBox status code.
*
* @param pDir The full directory.
* @param cbDir The size of the directory.
* @param offDir The directory stream offset.
* @param cDirEntries The directory entry count from the footer.
* @param cbHdr The header size.
* @param uSvnRev The SVN revision that saved the state. Bug detection.
*/
static int ssmR3ValidateDirectory(PSSMFILEDIR pDir, size_t cbDir, uint64_t offDir, uint32_t cDirEntries,
{
AssertLogRelReturn(!memcmp(pDir->szMagic, SSMFILEDIR_MAGIC, sizeof(pDir->szMagic)), VERR_SSM_INTEGRITY_DIR_MAGIC);
AssertLogRelReturn(RT_UOFFSETOF(SSMFILEDIR, aEntries[pDir->cEntries]) == cbDir, VERR_SSM_INTEGRITY_DIR);
{
&& uSvnRev < 53365),
}
return VINF_SUCCESS;
}
/**
* Reads and verifies the directory and footer.
*
* @returns VBox status code.
* @param pSSM The saved state handle.
*/
{
/*
* The directory.
*
* Get the header containing the number of entries first. Then read the
* entries and pass the combined block to the validation function.
*/
if (RT_FAILURE(rc))
return rc;
("Invalid directory magic at %#llx (%lld): %.*Rhxs\n", off, off, sizeof(DirHdr.szMagic), DirHdr.szMagic),
if (!pDir)
return VERR_NO_TMP_MEMORY;
if (RT_SUCCESS(rc))
rc = ssmR3ValidateDirectory(pDir, cbDir, off, DirHdr.cEntries, pSSM->u.Read.cbFileHdr, pSSM->u.Read.u32SvnRev);
if (RT_FAILURE(rc))
return rc;
/*
* Read and validate the footer.
*/
if (RT_FAILURE(rc))
return rc;
}
/**
* Executes the loading of a V2.X file.
*
* @returns VBox status code.
* @param pVM The VM handle.
* @param pSSM The saved state handle.
*/
{
for (;;)
{
/*
* Read the unit header and check its integrity.
*/
if (RT_FAILURE(rc))
return rc;
{
LogRel(("SSM: Unit at %#llx (%lld): Invalid unit magic: %.*Rhxs!\n",
}
{
("Unit at %#llx (%lld): UnitHdr.cbName=%u > %u\n",
if (RT_FAILURE(rc))
return rc;
("Unit at %#llx (%lld): Name %.*Rhxs was not properly terminated.\n",
}
("Unit at %#llx (%lld): CRC mismatch: %08x, correct is %08x\n", offUnit, offUnit, u32CRC, u32ActualCRC));
("Unit at %#llx (%lld): offStream=%#llx, expected %#llx\n", offUnit, offUnit, UnitHdr.offStream, offUnit),
("Unit at %#llx (%lld): Stream CRC mismatch: %08x, correct is %08x\n", offUnit, offUnit, UnitHdr.u32CurStreamCRC, u32CurStreamCRC),
AssertLogRelMsgReturn(!UnitHdr.fFlags, ("Unit at %#llx (%lld): fFlags=%08x\n", offUnit, offUnit, UnitHdr.fFlags),
{
&& UnitHdr.u32Instance == 0
&& UnitHdr.u32Version == 0
/*
* Complete the progress bar (pending 99% afterwards) and RETURN.
*/
return ssmR3LoadDirectoryAndFooter(pSSM);
}
AssertLogRelMsgReturn(UnitHdr.cbName > 1, ("Unit at %#llx (%lld): No name\n", offUnit, offUnit), VERR_SSM_INTEGRITY);
Log(("SSM: Unit at %#9llx: '%s', instance %u, pass %#x, version %u\n",
/*
* Find the data unit in our internal table.
*/
if (pUnit)
{
/*
* Call the execute handler.
*/
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
rc = pUnit->u.External.pfnLoadExec(pSSM, pUnit->u.External.pvUser, UnitHdr.u32Version, UnitHdr.u32Pass);
break;
default:
break;
}
if (RT_SUCCESS(rc))
else
{
LogRel(("SSM: LoadExec failed for '%s' instance #%u (version %u, pass %#x): %Rrc\n",
}
}
else
{
/*
* SSM unit wasn't found - ignore this when loading for the debugger.
*/
LogRel(("SSM: Found no handler for unit '%s' instance #%u!\n", UnitHdr.szName, UnitHdr.u32Instance));
}
/*
* Check for cancellation.
*/
{
LogRel(("SSM: Cancelled!\n"));
}
}
/* won't get here */
}
/**
* Load VM save operation.
*
* @returns VBox status.
*
* @param pVM The VM handle.
* @param pszFilename The name of the saved state file. NULL if pStreamOps
* is used.
* @param pStreamOps The stream method table. NULL if pszFilename is
* used.
* @param pvStreamOpsUser The user argument for the stream methods.
* @param enmAfter What is planned after a successful load operation.
* Only acceptable values are SSMAFTER_RESUME and SSMAFTER_DEBUG_IT.
* @param pfnProgress Progress callback. Optional.
* @param pvProgressUser User argument for the progress callback.
*
* @thread EMT
*/
VMMR3DECL(int) SSMR3Load(PVM pVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
{
LogFlow(("SSMR3Load: pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p enmAfter=%d pfnProgress=%p pvProgressUser=%p\n",
/*
* Validate input.
*/
|| enmAfter == SSMAFTER_MIGRATE
|| enmAfter == SSMAFTER_DEBUG_IT,
("%d\n", enmAfter),
if (pStreamOps)
{
}
/*
* Create the handle and open the file.
*/
if (RT_SUCCESS(rc))
{
LogRel(("SSM: File header: Format %u.%u, VirtualBox Version %u.%u.%u r%u, %u-bit host, cbGCPhys=%u, cbGCPtr=%u\n",
Handle.u.Read.u16VerMajor, Handle.u.Read.u16VerMinor, Handle.u.Read.u32VerBuild, Handle.u.Read.u32SvnRev,
else
LogRel(("SSM: File header: Format %u.%u, %u-bit host, cbGCPhys=%u, cbGCPtr=%u\n" ,
if (pfnProgress)
/*
* Clear the per unit flags.
*/
/*
* Do the prepare run.
*/
{
{
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
else
if (RT_FAILURE(rc))
{
break;
}
}
}
/* pending 2% */
if (pfnProgress)
/*
* Do the execute run.
*/
if (RT_SUCCESS(rc))
{
else
/* (progress should be pending 99% now) */
|| RT_FAILURE(rc)
}
/*
* Do the done run.
*/
{
{
rc = VINF_SUCCESS;
{
case SSMUNITTYPE_DEV:
break;
case SSMUNITTYPE_DRV:
break;
case SSMUNITTYPE_INTERNAL:
break;
case SSMUNITTYPE_EXTERNAL:
break;
default:
break;
}
if (RT_FAILURE(rc))
{
LogRel(("SSM: LoadDone failed with rc=%Rrc for data unit '%s' instance #%u.\n",
}
}
}
/* progress */
if (pfnProgress)
}
/*
* Done
*/
if (RT_SUCCESS(rc))
{
/* progress */
if (pfnProgress)
}
return rc;
}
/**
* Validates a file as a validate SSM saved state.
*
* This will only verify the file format, the format and content of individual
* data units are not inspected.
*
* @returns VINF_SUCCESS if valid.
* @returns VBox status code on other failures.
*
* @param pszFilename The path to the file to validate.
* @param fChecksumIt Whether to checksum the file or not.
*
* @thread Any.
*/
{
LogFlow(("SSMR3ValidateFile: pszFilename=%p:{%s} fChecksumIt=%RTbool\n", pszFilename, pszFilename, fChecksumIt));
/*
* Try open the file and validate it.
*/
if (RT_SUCCESS(rc))
else
return rc;
}
/**
* Opens a saved state file for reading.
*
* @returns VBox status code.
*
* @param pszFilename The path to the saved state file.
* @param fFlags Open flags. Reserved, must be 0.
* @param ppSSM Where to store the SSM handle.
*
* @thread Any.
*/
{
LogFlow(("SSMR3Open: pszFilename=%p:{%s} fFlags=%#x ppSSM=%p\n", pszFilename, pszFilename, fFlags, ppSSM));
/*
* Validate input.
*/
/*
* Allocate a handle.
*/
/*
* Try open the file and validate it.
*/
int rc = ssmR3OpenFile(NULL, pszFilename, NULL /*pStreamOps*/, NULL /*pvUser*/, false /*fChecksumIt*/,
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
/**
* Closes a saved state file opened by SSMR3Open().
*
* @returns VBox status code.
*
* @param pSSM The SSM handle returned by SSMR3Open().
*
* @thread Any, but the caller is responsible for serializing calls per handle.
*/
{
/*
* Validate input.
*/
AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
/*
* Close the stream and free the handle.
*/
{
}
return rc;
}
/**
* Worker for SSMR3Seek that seeks version 1 saved state files.
*
* @returns VBox status code.
* @param pSSM The SSM handle.
* @param pszUnit The unit to seek to.
* @param iInstance The particulart insance we seek.
* @param piVersion Where to store the unit version number.
*/
static int ssmR3FileSeekV1(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
{
/*
* Walk the data units until we find EOF or a match.
*/
char szName[SSM_MAX_NAME_SIZE];
{
/*
* Read the unit header and verify it.
*/
{
/*
* Does what we've got match, if so read the name.
*/
{
rc = ssmR3StrmPeekAt(&pSSM->Strm, off + RT_OFFSETOF(SSMFILEUNITHDRV1, szName), szName, cbUnitNm, NULL);
/*
* Does the name match?
*/
{
rc = ssmR3StrmSeek(&pSSM->Strm, off + RT_OFFSETOF(SSMFILEUNITHDRV1, szName) + cbUnitNm, RTFILE_SEEK_BEGIN, 0);
if (piVersion)
return VINF_SUCCESS;
}
}
}
return VERR_SSM_UNIT_NOT_FOUND;
else
AssertLogRelMsgFailedReturn(("Invalid unit magic at offset %RTfoff, '%.*s'!\n",
}
/* won't get here. */
}
/**
* Worker for ssmR3FileSeekV2 for simplifying memory cleanup.
*
* @returns VBox status code.
* @param pSSM The SSM handle.
* @param pDir The directory buffer.
* @param cbDir The size of the directory.
* @param cDirEntries The number of directory entries.
* @param offDir The directory offset in the file.
* @param pszUnit The unit to seek to.
* @param iInstance The particulart insance we seek.
* @param piVersion Where to store the unit version number.
*/
static int ssmR3FileSeekSubV2(PSSMHANDLE pSSM, PSSMFILEDIR pDir, size_t cbDir, uint32_t cDirEntries, uint64_t offDir,
{
/*
* Read it.
*/
rc = ssmR3ValidateDirectory(pDir, cbDir, offDir, cDirEntries, pSSM->u.Read.cbFileHdr, pSSM->u.Read.u32SvnRev);
if (RT_FAILURE(rc))
return rc;
/*
* Search the directory.
*/
for (uint32_t i = 0; i < cDirEntries; i++)
{
)
{
/*
* Read and validate the unit header.
*/
{
}
("Bad unit header: i=%d off=%lld u32Instance=%u Dir.u32Instance=%u\n",
("Bad unit header: i=%u off=%lld cbName=%#x cbToRead=%#x\n", i, pDir->aEntries[i].off, UnitHdr.cbName, cbToRead),
("Bad unit header CRC: i=%u off=%lld u32CRC=%#x u32ActualCRC=%#x\n",
/*
* Ok, it is valid, get on with the comparing now.
*/
{
if (piVersion)
return VINF_SUCCESS;
}
}
}
return VERR_SSM_UNIT_NOT_FOUND;
}
/**
* Worker for SSMR3Seek that seeks version 2 saved state files.
*
* @returns VBox status code.
* @param pSSM The SSM handle.
* @param pszUnit The unit to seek to.
* @param iInstance The particulart insance we seek.
* @param piVersion Where to store the unit version number.
*/
static int ssmR3FileSeekV2(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
{
/*
* Read the footer, allocate a temporary buffer for the dictionary and
* pass it down to a worker to simplify cleanup.
*/
int rc = ssmR3StrmPeekAt(&pSSM->Strm, -(RTFOFF)sizeof(Footer), &Footer, sizeof(Footer), &offFooter);
AssertLogRelReturn(!memcmp(Footer.szMagic, SSMFILEFTR_MAGIC, sizeof(Footer.szMagic)), VERR_SSM_INTEGRITY);
SSM_CHECK_CRC32_RET(&Footer, sizeof(Footer), ("Bad footer CRC: %08x, actual %08x\n", u32CRC, u32ActualCRC));
if (RT_UNLIKELY(!pDir))
return VERR_NO_TMP_MEMORY;
return rc;
}
/**
* Seeks to a specific data unit.
*
* After seeking it's possible to use the getters to on
* that data unit.
*
* @returns VBox status code.
* @returns VERR_SSM_UNIT_NOT_FOUND if the unit+instance wasn't found.
*
* @param pSSM The SSM handle returned by SSMR3Open().
* @param pszUnit The name of the data unit.
* @param iInstance The instance number.
* @param piVersion Where to store the version number. (Optional)
*
* @thread Any, but the caller is responsible for serializing calls per handle.
*/
VMMR3DECL(int) SSMR3Seek(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
{
LogFlow(("SSMR3Seek: pSSM=%p pszUnit=%p:{%s} iInstance=%RU32 piVersion=%p\n",
/*
* Validate input.
*/
AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
/*
* Reset the state.
*/
{
}
pSSM->cbUnitLeftV1 = 0;
/*
* Call the version specific workers.
*/
else
}
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/* ... Misc APIs ... */
/**
* Query what the VBox status code of the operation is.
*
* This can be used for putting and getting a batch of values
* without bother checking the result till all the calls have
* been made.
*
* @returns SSMAFTER enum value.
* @param pSSM SSM operation handle.
*/
{
}
/**
* Fail the load operation.
*
* This is mainly intended for sub item loaders (like timers) which
* return code isn't necessarily heeded by the caller but is important
* to SSM.
*
* @returns VBox status code of the handle, or VERR_INVALID_PARAMETER.
* @param pSSM SSM operation handle.
* @param iStatus Failure status code. This MUST be a VERR_*.
*/
{
if (RT_FAILURE(iStatus))
{
if (RT_SUCCESS(rc))
return rc;
}
return VERR_INVALID_PARAMETER;
}
/**
* Get what to do after this operation.
*
* @returns SSMAFTER enum value.
* @param pSSM SSM operation handle.
*/
{
}
/**
* Checks if it is a live save operation or not.
*
* @returns True if it is, false if it isn't.
* @param pSSM SSM operation handle.
*/
{
}
/**
* Asynchronously cancels the current SSM operation ASAP.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @retval VERR_SSM_NO_PENDING_OPERATION if nothing around that can be
* cancelled.
* @retval VERR_SSM_ALREADY_CANCELLED if the operation as already been
* cancelled.
*
* @param pVM The VM handle.
*
* @thread Any.
*/
{
if (pSSM)
{
{
LogRel(("SSM: Cancelled pending operation\n"));
rc = VINF_SUCCESS;
}
else if (u32Old == SSMHANDLE_CANCELLED)
else
{
}
}
else
return rc;
}