VirtualBox

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

Last change on this file since 30111 was 30111, checked in by vboxsync, 14 years ago

iprt/asm.h,*: Revised the ASMAtomic*Ptr functions and macros. The new saves lots of unsafe (void * volatile *) casts as well as adding some type safety when using GCC (typeof rulez).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 299.1 KB
Line 
1/* $Id: SSM.cpp 30111 2010-06-09 12:14:59Z vboxsync $ */
2/** @file
3 * SSM - Saved State Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/** @page pg_ssm SSM - The Saved State Manager
20 *
21 * The Saved State Manager (SSM) implements facilities for saving and loading a
22 * VM state in a structural manner using callbacks for named data units.
23 *
24 * At init time each of the VMM components, Devices, Drivers and one or two
25 * other things will register data units which they need to save and restore.
26 * Each unit have a unique name (ascii), instance number, and a set of callbacks
27 * associated with it. The name will be used to identify the unit during
28 * restore. The callbacks are for the two operations, save and restore. There
29 * are three callbacks for each of the two - a prepare, a execute and a complete
30 * - giving each component ample opportunity to perform actions both before and
31 * afterwards.
32 *
33 * The SSM provides a number of APIs for encoding and decoding the data: @see
34 * grp_ssm
35 *
36 *
37 *
38 * @section sec_ssm_live_snapshots Live Snapshots
39 *
40 * The live snapshots feature (LS) is similar to teleportation (TP) and was a
41 * natural first step when implementing TP. The main differences between LS and
42 * TP are that after a live snapshot we will have a saved state file, disk image
43 * snapshots, and the VM will still be running.
44 *
45 * Compared to normal saved stated and snapshots, the difference is in that the
46 * VM is running while we do most of the saving. Prior to LS, there was only
47 * one round of callbacks during saving and the VM was paused during it. With
48 * LS there are 1 or more passes while the VM is still running and a final one
49 * after it has been paused. The runtime passes are executed on a dedicated
50 * thread running at at the same priority as the EMTs so that the saving doesn't
51 * starve or lose in scheduling questions (note: not implemented yet). The final
52 * pass is done on EMT(0).
53 *
54 * There are a couple of common reasons why LS and TP will fail:
55 * - Memory configuration changed (PCI memory mappings).
56 * - Takes too long (TP) / Too much output (LS).
57 *
58 *
59 * The live saving sequence is something like this:
60 *
61 * -# SSMR3LiveSave is called on EMT0. It returns a saved state
62 * handle.
63 * -# SSMR3LiveDoStep1 is called on a non-EMT. This will save the major
64 * parts of the state while the VM may still be running.
65 * -# The VM is suspended.
66 * -# SSMR3LiveDoStep2 is called on EMT0 to save the remainder of the state
67 * in the normal way.
68 * -# The client does any necessary reconfiguration of harddisks and
69 * similar.
70 * -# SSMR3LiveDone is called on EMT0 to close the handle.
71 * -# The VM is resumed or powered off and destroyed.
72 *
73 *
74 * @section sec_ssm_teleportation Teleportation
75 *
76 * As mentioned in the previous section, the main differences between this and
77 * live snapshots are in where the saved state is written and what state the
78 * local VM is in afterwards - at least from the VMM point of view. The
79 * necessary administrative work - establishing the connection to the remote
80 * machine, cloning the VM config on it and doing lowlevel saved state data
81 * transfer - is taken care of by layer above the VMM (i.e. Main).
82 *
83 * The SSM data format was made streamable for the purpose of teleportation
84 * (v1.2 was the last non-streamable version).
85 *
86 *
87 * @section sec_ssm_format Saved State Format
88 *
89 * The stream format starts with a header (SSMFILEHDR) that indicates the
90 * version and such things, it is followed by zero or more saved state units
91 * (name + instance + pass), and the stream concludes with a footer
92 * (SSMFILEFTR) that contains unit counts and optionally a checksum for the
93 * entire file. (In version 1.2 and earlier, the checksum was in the header and
94 * there was no footer. This meant that the header was updated after the entire
95 * file was written.)
96 *
97 * The saved state units each starts with a variable sized header
98 * (SSMFILEUNITHDRV2) that contains the name, instance and pass. The data
99 * follows the header and is encoded as records with a 2-8 byte record header
100 * indicating the type, flags and size. The first byte in the record header
101 * indicates the type and flags:
102 *
103 * - bits 0..3: Record type:
104 * - type 0: Invalid.
105 * - type 1: Terminator with CRC-32 and unit size.
106 * - type 2: Raw data record.
107 * - type 3: Raw data compressed by LZF. The data is prefixed by a 8-bit
108 * field countining the length of the uncompressed data given in
109 * 1KB units.
110 * - type 4: Zero data. The record header is followed by a 8-bit field
111 * counting the length of the zero data given in 1KB units.
112 * - type 5: Named data - length prefixed name followed by the data. This
113 * type is not implemented yet as we're missing the API part, so
114 * the type assignment is tentative.
115 * - types 6 thru 15 are current undefined.
116 * - bit 4: Important (set), can be skipped (clear).
117 * - bit 5: Undefined flag, must be zero.
118 * - bit 6: Undefined flag, must be zero.
119 * - bit 7: "magic" bit, always set.
120 *
121 * Record header byte 2 (optionally thru 7) is the size of the following data
122 * encoded in UTF-8 style. To make buffering simpler and more efficient during
123 * the save operation, the strict checks enforcing optimal encoding has been
124 * relaxed for the 2 and 3 byte encodings.
125 *
126 * (In version 1.2 and earlier the unit data was compressed and not record
127 * based. The unit header contained the compressed size of the data, i.e. it
128 * needed updating after the data was written.)
129 *
130 *
131 * @section sec_ssm_future Future Changes
132 *
133 * There are plans to extend SSM to make it easier to be both backwards and
134 * (somewhat) forwards compatible. One of the new features will be being able
135 * to classify units and data items as unimportant (added to the format in
136 * v2.0). Another suggested feature is naming data items (also added to the
137 * format in v2.0), perhaps by extending the SSMR3PutStruct API. Both features
138 * will require API changes, the naming may possibly require both buffering of
139 * the stream as well as some helper managing them.
140 */
141
142
143/*******************************************************************************
144* Header Files *
145*******************************************************************************/
146#define LOG_GROUP LOG_GROUP_SSM
147#include <VBox/ssm.h>
148#include <VBox/dbgf.h>
149#include <VBox/mm.h>
150#include "SSMInternal.h"
151#include <VBox/vm.h>
152#include <VBox/err.h>
153#include <VBox/log.h>
154#include <VBox/version.h>
155
156#include <iprt/asm.h>
157#include <iprt/assert.h>
158#include <iprt/crc32.h>
159#include <iprt/file.h>
160#include <iprt/mem.h>
161#include <iprt/param.h>
162#include <iprt/thread.h>
163#include <iprt/semaphore.h>
164#include <iprt/string.h>
165#include <iprt/uuid.h>
166#include <iprt/zip.h>
167
168
169/*******************************************************************************
170* Defined Constants And Macros *
171*******************************************************************************/
172/** The max length of a unit name. */
173#define SSM_MAX_NAME_SIZE 48
174
175/** Saved state file magic base string. */
176#define SSMFILEHDR_MAGIC_BASE "\177VirtualBox SavedState "
177/** Saved state file magic indicating version 1.x. */
178#define SSMFILEHDR_MAGIC_V1_X "\177VirtualBox SavedState V1."
179/** Saved state file v1.1 magic. */
180#define SSMFILEHDR_MAGIC_V1_1 "\177VirtualBox SavedState V1.1\n"
181/** Saved state file v1.2 magic. */
182#define SSMFILEHDR_MAGIC_V1_2 "\177VirtualBox SavedState V1.2\n\0\0\0"
183/** Saved state file v2.0 magic. */
184#define SSMFILEHDR_MAGIC_V2_0 "\177VirtualBox SavedState V2.0\n\0\0\0"
185
186/** @name SSMFILEHDR::fFlags
187 * @{ */
188/** The stream is checkesummed up to the footer using CRC-32. */
189#define SSMFILEHDR_FLAGS_STREAM_CRC32 RT_BIT_32(0)
190/** Indicates that the file was produced by a live save. */
191#define SSMFILEHDR_FLAGS_STREAM_LIVE_SAVE RT_BIT_32(1)
192/** @} */
193
194/** The directory magic. */
195#define SSMFILEDIR_MAGIC "\nDir\n\0\0"
196
197/** Saved state file v2.0 magic. */
198#define SSMFILEFTR_MAGIC "\nFooter"
199
200/** Data unit magic. */
201#define SSMFILEUNITHDR_MAGIC "\nUnit\n\0"
202/** Data end marker magic. */
203#define SSMFILEUNITHDR_END "\nTheEnd"
204
205
206/** @name Record Types (data unit)
207 * @{ */
208/** The record type mask. */
209#define SSM_REC_TYPE_MASK UINT8_C(0x0f)
210/** Invalid record. */
211#define SSM_REC_TYPE_INVALID 0
212/** Normal termination record, see SSMRECTERM. */
213#define SSM_REC_TYPE_TERM 1
214/** Raw data. The data follows the size field without further ado. */
215#define SSM_REC_TYPE_RAW 2
216/** Raw data compressed by LZF.
217 * The record header is followed by a 8-bit field containing the size of the
218 * uncompressed data in 1KB units. The compressed data is after it. */
219#define SSM_REC_TYPE_RAW_LZF 3
220/** Raw zero data.
221 * The record header is followed by a 8-bit field containing the size of the
222 * zero data in 1KB units. */
223#define SSM_REC_TYPE_RAW_ZERO 4
224/** Named data items.
225 * A length prefix zero terminated string (i.e. max 255) followed by the data. */
226#define SSM_REC_TYPE_NAMED 5
227/** Macro for validating the record type.
228 * This can be used with the flags+type byte, no need to mask out the type first. */
229#define SSM_REC_TYPE_IS_VALID(u8Type) ( ((u8Type) & SSM_REC_TYPE_MASK) > SSM_REC_TYPE_INVALID \
230 && ((u8Type) & SSM_REC_TYPE_MASK) <= SSM_REC_TYPE_NAMED )
231/** @} */
232
233/** The flag mask. */
234#define SSM_REC_FLAGS_MASK UINT8_C(0xf0)
235/** The record is important if this flag is set, if clear it can be omitted. */
236#define SSM_REC_FLAGS_IMPORTANT UINT8_C(0x10)
237/** This flag is always set. */
238#define SSM_REC_FLAGS_FIXED UINT8_C(0x80)
239/** Macro for validating the flags.
240 * No need to mask the flags out of the flags+type byte before invoking this macro. */
241#define SSM_REC_FLAGS_ARE_VALID(fFlags) ( ((fFlags) & UINT8_C(0xe0)) == UINT8_C(0x80) )
242
243/** Macro for validating the type and flags byte in a data record. */
244#define SSM_REC_ARE_TYPE_AND_FLAGS_VALID(u8) ( SSM_REC_FLAGS_ARE_VALID(u8) && SSM_REC_TYPE_IS_VALID(u8) )
245
246/** @name SSMRECTERM::fFlags
247 * @{ */
248/** There is a CRC-32 value for the stream. */
249#define SSMRECTERM_FLAGS_CRC32 UINT16_C(0x0001)
250/** @} */
251
252/** Start structure magic. (Isacc Asimov) */
253#define SSMR3STRUCT_BEGIN UINT32_C(0x19200102)
254/** End structure magic. (Isacc Asimov) */
255#define SSMR3STRUCT_END UINT32_C(0x19920406)
256
257
258/** Number of bytes to log in Log2 and Log4 statements. */
259#define SSM_LOG_BYTES 16
260
261/** SSMHANDLE::fCancelled value indicating that the operation has been
262 * cancelled. */
263#define SSMHANDLE_CANCELLED UINT32_C(0xdeadbeef)
264/** SSMHANDLE::fCancelled value indicating no cancellation. */
265#define SSMHANDLE_OK UINT32_C(0x77777777)
266
267
268/** Macro for checking the u32CRC field of a structure.
269 * The Msg can assume there are u32ActualCRC and u32CRC in the context. */
270#define SSM_CHECK_CRC32_RET(p, cb, Msg) \
271 do \
272 { \
273 uint32_t u32CRC = (p)->u32CRC; \
274 (p)->u32CRC = 0; \
275 uint32_t u32ActualCRC = RTCrc32((p), (cb)); \
276 (p)->u32CRC = u32CRC; \
277 AssertLogRelMsgReturn(u32ActualCRC == u32CRC, Msg, VERR_SSM_INTEGRITY_CRC); \
278 } while (0)
279
280/** The number of bytes to compress is one block.
281 * Must be a multiple of 1KB. */
282#define SSM_ZIP_BLOCK_SIZE _4K
283AssertCompile(SSM_ZIP_BLOCK_SIZE / _1K * _1K == SSM_ZIP_BLOCK_SIZE);
284
285
286/**
287 * Asserts that the handle is writable and returns with VERR_SSM_INVALID_STATE
288 * if it isn't.
289 */
290#define SSM_ASSERT_WRITEABLE_RET(pSSM) \
291 AssertMsgReturn( pSSM->enmOp == SSMSTATE_SAVE_EXEC \
292 || pSSM->enmOp == SSMSTATE_LIVE_EXEC,\
293 ("Invalid state %d\n", pSSM->enmOp), VERR_SSM_INVALID_STATE);
294
295/**
296 * Asserts that the handle is readable and returns with VERR_SSM_INVALID_STATE
297 * if it isn't.
298 */
299#define SSM_ASSERT_READABLE_RET(pSSM) \
300 AssertMsgReturn( pSSM->enmOp == SSMSTATE_LOAD_EXEC \
301 || pSSM->enmOp == SSMSTATE_OPEN_READ,\
302 ("Invalid state %d\n", pSSM->enmOp), VERR_SSM_INVALID_STATE);
303
304/** Checks for cancellation and returns if pending.
305 * Sets SSMHANDLE::rc to VERR_SSM_CANCELLED (if it still indicates success) and
306 * then returns SSMHANDLE::rc. (Debug logging only.) */
307#define SSM_CHECK_CANCELLED_RET(pSSM) \
308 do \
309 { \
310 if (RT_UNLIKELY(ASMAtomicUoReadU32(&(pSSM)->fCancelled) == SSMHANDLE_CANCELLED)) \
311 { \
312 LogFlow(("%Rfn: Cancelled -> VERR_SSM_CANCELLED\n", __PRETTY_FUNCTION__)); \
313 if (RT_SUCCESS((pSSM)->rc)) \
314 (pSSM)->rc = VERR_SSM_CANCELLED; \
315 return (pSSM)->rc; \
316 } \
317 } while (0)
318
319/**
320 * Asserts that the handle is somewhat valid. No returns as this is just a
321 * simple safeguard for catching bad API calls. */
322#define SSM_ASSERT_VALID_HANDLE(pSSM) \
323 do \
324 { \
325 AssertPtr(pSSM); \
326 Assert(pSSM->enmOp > SSMSTATE_INVALID && pSSM->enmOp < SSMSTATE_END); \
327 } while (0)
328
329
330/** @def SSM_HOST_IS_MSC_32
331 * Set to 1 if the host is 32-bit MSC, otherwise set to 0.
332 * */
333#if defined(_MSC_VER) && HC_ARCH_BITS == 32
334# define SSM_HOST_IS_MSC_32 1
335#else
336# define SSM_HOST_IS_MSC_32 0
337#endif
338
339
340
341/*******************************************************************************
342* Structures and Typedefs *
343*******************************************************************************/
344/** SSM state. */
345typedef enum SSMSTATE
346{
347 SSMSTATE_INVALID = 0,
348 SSMSTATE_LIVE_PREP,
349 SSMSTATE_LIVE_STEP1,
350 SSMSTATE_LIVE_EXEC,
351 SSMSTATE_LIVE_VOTE,
352 SSMSTATE_LIVE_STEP2,
353 SSMSTATE_SAVE_PREP,
354 SSMSTATE_SAVE_EXEC,
355 SSMSTATE_SAVE_DONE,
356 SSMSTATE_LOAD_PREP,
357 SSMSTATE_LOAD_EXEC,
358 SSMSTATE_LOAD_DONE,
359 SSMSTATE_OPEN_READ,
360 SSMSTATE_END
361} SSMSTATE;
362
363
364/** Pointer to a SSM stream buffer. */
365typedef struct SSMSTRMBUF *PSSMSTRMBUF;
366/**
367 * A SSM stream buffer.
368 */
369typedef struct SSMSTRMBUF
370{
371 /** The buffer data. */
372 uint8_t abData[_64K];
373
374 /** The stream position of this buffer. */
375 uint64_t offStream;
376 /** The amount of buffered data. */
377 uint32_t cb;
378 /** End of stream indicator (for read streams only). */
379 bool fEndOfStream;
380 /** The nano timestamp set by ssmR3StrmGetFreeBuf. */
381 uint64_t NanoTS;
382 /** Pointer to the next buffer in the chain. */
383 PSSMSTRMBUF volatile pNext;
384} SSMSTRMBUF;
385
386/**
387 * SSM stream.
388 *
389 * This is a typical producer / consumer setup with a dedicated I/O thread and
390 * fixed number of buffers for read ahead and write back.
391 */
392typedef struct SSMSTRM
393{
394 /** The stream method table. */
395 PCSSMSTRMOPS pOps;
396 /** The user argument for the stream methods.
397 * For file based streams, this is the file handle and not a pointer. */
398 void *pvUser;
399
400 /** Write (set) or read (clear) stream. */
401 bool fWrite;
402 /** Termination indicator. */
403 bool volatile fTerminating;
404 /** Indicates whether it is necessary to seek before the next buffer is
405 * read from the stream. This is used to avoid a seek in ssmR3StrmPeekAt. */
406 bool fNeedSeek;
407 /** Stream error status. */
408 int32_t volatile rc;
409 /** The handle of the I/O thread. This is set to nil when not active. */
410 RTTHREAD hIoThread;
411 /** Where to seek to. */
412 uint64_t offNeedSeekTo;
413
414 /** The head of the consumer queue.
415 * For save the consumer is the I/O thread. For load the I/O thread is the
416 * producer. */
417 PSSMSTRMBUF volatile pHead;
418 /** Chain of free buffers.
419 * The consumer/producer roles are the inverse of pHead. */
420 PSSMSTRMBUF volatile pFree;
421 /** Event that's signalled when pHead is updated. */
422 RTSEMEVENT hEvtHead;
423 /** Event that's signalled when pFree is updated. */
424 RTSEMEVENT hEvtFree;
425
426 /** List of pending buffers that has been dequeued from pHead and reversed. */
427 PSSMSTRMBUF pPending;
428 /** Pointer to the current buffer. */
429 PSSMSTRMBUF pCur;
430 /** The stream offset of the current buffer. */
431 uint64_t offCurStream;
432 /** The current buffer offset. */
433 uint32_t off;
434 /** Whether we're checksumming reads/writes. */
435 bool fChecksummed;
436 /** The stream CRC if fChecksummed is set. */
437 uint32_t u32StreamCRC;
438 /** How far into the buffer u32StreamCRC is up-to-date.
439 * This may lag behind off as it's desirable to checksum as large blocks as
440 * possible. */
441 uint32_t offStreamCRC;
442} SSMSTRM;
443/** Pointer to a SSM stream. */
444typedef SSMSTRM *PSSMSTRM;
445
446
447/**
448 * Handle structure.
449 */
450typedef struct SSMHANDLE
451{
452 /** Stream/buffer manager. */
453 SSMSTRM Strm;
454
455 /** The VM handle. */
456 PVM pVM;
457 /** The current operation. */
458 SSMSTATE enmOp;
459 /** What to do after save completes. (move the enum) */
460 SSMAFTER enmAfter;
461 /** Flag indicating that the operation has been cancelled. */
462 uint32_t volatile fCancelled;
463 /** The current rc of the save operation. */
464 int32_t rc;
465 /** Number of compressed bytes left in the current data unit (V1). */
466 uint64_t cbUnitLeftV1;
467 /** The current uncompressed offset into the data unit. */
468 uint64_t offUnit;
469 /** Indicates that this is a live save or restore operation. */
470 bool fLiveSave;
471
472 /** Pointer to the progress callback function. */
473 PFNVMPROGRESS pfnProgress;
474 /** User specified arguemnt to the callback function. */
475 void *pvUser;
476 /** Next completion percentage. (corresponds to offEstProgress) */
477 unsigned uPercent;
478 /** The position of the next progress callback in the estimated file. */
479 uint64_t offEstProgress;
480 /** The estimated total byte count.
481 * (Only valid after the prep.) */
482 uint64_t cbEstTotal;
483 /** Current position in the estimated file. */
484 uint64_t offEst;
485 /** End of current unit in the estimated file. */
486 uint64_t offEstUnitEnd;
487 /** the amount of % we reserve for the 'prepare' phase */
488 unsigned uPercentPrepare;
489 /** the amount of % we reserve for the 'done' stage */
490 unsigned uPercentDone;
491 /** The filename, NULL if remote stream. */
492 const char *pszFilename;
493
494 union
495 {
496 /** Write data. */
497 struct
498 {
499 /** Offset into the databuffer. */
500 uint32_t offDataBuffer;
501 /** Space for the record header. */
502 uint8_t abRecHdr[1+7];
503 /** Data buffer. */
504 uint8_t abDataBuffer[4096];
505 /** The maximum downtime given as milliseconds. */
506 uint32_t cMsMaxDowntime;
507 } Write;
508
509 /** Read data. */
510 struct
511 {
512 /** V1: The decompressor of the current data unit. */
513 PRTZIPDECOMP pZipDecompV1;
514 /** The major format version number. */
515 uint32_t uFmtVerMajor;
516 /** The minor format version number. */
517 uint32_t uFmtVerMinor;
518
519 /** V2: Unread bytes in the current record. */
520 uint32_t cbRecLeft;
521 /** V2: Bytes in the data buffer. */
522 uint32_t cbDataBuffer;
523 /** V2: Current buffer position. */
524 uint32_t offDataBuffer;
525 /** V2: End of data indicator. */
526 bool fEndOfData;
527 /** V2: The type and flags byte fo the current record. */
528 uint8_t u8TypeAndFlags;
529
530 /** @name Context info for SSMR3SetLoadError.
531 * @{ */
532 /** Pointer to the header for the current unit. */
533 PSSMUNIT pCurUnit;
534 /** The version of the current unit if in the load exec stage. */
535 uint32_t uCurUnitVer;
536 /** The pass number of the current unit if in the load exec stage. */
537 uint32_t uCurUnitPass;
538 /** Whether SSMR3SetLoadError[V] has been called.
539 * @note Using ASMAtomicXchgBool because I'm very lazy. */
540 bool volatile fHaveSetError;
541 /** @} */
542
543 /** RTGCPHYS size in bytes. (Only applicable when loading/reading.) */
544 unsigned cbGCPhys;
545 /** RTGCPTR size in bytes. (Only applicable when loading/reading.) */
546 unsigned cbGCPtr;
547 /** Whether cbGCPtr is fixed or settable. */
548 bool fFixedGCPtrSize;
549
550 /** 32-bit MSC saved this? */
551 bool fIsHostMsc32;
552 /** "Host OS" dot "architecture", picked up from recent SSM data units. */
553 char szHostOSAndArch[32];
554
555 /** @name Header info (set by ssmR3ValidateFile)
556 * @{ */
557 /** The size of the file header. */
558 uint32_t cbFileHdr;
559 /** The major version number. */
560 uint16_t u16VerMajor;
561 /** The minor version number. */
562 uint16_t u16VerMinor;
563 /** The build number. */
564 uint32_t u32VerBuild;
565 /** The SVN revision. */
566 uint32_t u32SvnRev;
567 /** 32 or 64 depending on the host. */
568 uint8_t cHostBits;
569 /** Whether the stream is checksummed (SSMFILEHDR_FLAGS_STREAM_CRC32). */
570 bool fStreamCrc32;
571 /** The CRC of the loaded file. */
572 uint32_t u32LoadCRC;
573 /** The size of the load file. */
574 uint64_t cbLoadFile;
575 /** @} */
576
577 /** V2: Data buffer.
578 * @remarks Be extremely careful when changing the size of this buffer! */
579 uint8_t abDataBuffer[4096];
580
581 /** V2: Decompression buffer for when we cannot use the stream buffer. */
582 uint8_t abComprBuffer[4096];
583 } Read;
584 } u;
585} SSMHANDLE;
586
587
588/**
589 * Header of the saved state file.
590 *
591 * Added in r5xxxx on 2009-07-2?, VirtualBox v3.0.51.
592 */
593typedef struct SSMFILEHDR
594{
595 /** Magic string which identifies this file as a version of VBox saved state
596 * file format (SSMFILEHDR_MAGIC_V2_0). */
597 char szMagic[32];
598 /** The major version number. */
599 uint16_t u16VerMajor;
600 /** The minor version number. */
601 uint16_t u16VerMinor;
602 /** The build number. */
603 uint32_t u32VerBuild;
604 /** The SVN revision. */
605 uint32_t u32SvnRev;
606 /** 32 or 64 depending on the host. */
607 uint8_t cHostBits;
608 /** The size of RTGCPHYS. */
609 uint8_t cbGCPhys;
610 /** The size of RTGCPTR. */
611 uint8_t cbGCPtr;
612 /** Reserved header space - must be zero. */
613 uint8_t u8Reserved;
614 /** The number of units that (may) have stored data in the file. */
615 uint32_t cUnits;
616 /** Flags, see SSMFILEHDR_FLAGS_XXX. */
617 uint32_t fFlags;
618 /** The maximum size of decompressed data. */
619 uint32_t cbMaxDecompr;
620 /** The checksum of this header.
621 * This field is set to zero when calculating the checksum. */
622 uint32_t u32CRC;
623} SSMFILEHDR;
624AssertCompileSize(SSMFILEHDR, 64);
625AssertCompileMemberOffset(SSMFILEHDR, u32CRC, 60);
626AssertCompileMemberSize(SSMFILEHDR, szMagic, sizeof(SSMFILEHDR_MAGIC_V2_0));
627/** Pointer to a saved state file header. */
628typedef SSMFILEHDR *PSSMFILEHDR;
629/** Pointer to a const saved state file header. */
630typedef SSMFILEHDR const *PCSSMFILEHDR;
631
632
633/**
634 * Header of the saved state file.
635 *
636 * Added in r40980 on 2008-12-15, VirtualBox v2.0.51.
637 *
638 * @remarks This is a superset of SSMFILEHDRV11.
639 */
640typedef struct SSMFILEHDRV12
641{
642 /** Magic string which identifies this file as a version of VBox saved state
643 * file format (SSMFILEHDR_MAGIC_V1_2). */
644 char achMagic[32];
645 /** The size of this file. Used to check
646 * whether the save completed and that things are fine otherwise. */
647 uint64_t cbFile;
648 /** File checksum. The actual calculation skips past the u32CRC field. */
649 uint32_t u32CRC;
650 /** Padding. */
651 uint32_t u32Reserved;
652 /** The machine UUID. (Ignored if NIL.) */
653 RTUUID MachineUuid;
654
655 /** The major version number. */
656 uint16_t u16VerMajor;
657 /** The minor version number. */
658 uint16_t u16VerMinor;
659 /** The build number. */
660 uint32_t u32VerBuild;
661 /** The SVN revision. */
662 uint32_t u32SvnRev;
663
664 /** 32 or 64 depending on the host. */
665 uint8_t cHostBits;
666 /** The size of RTGCPHYS. */
667 uint8_t cbGCPhys;
668 /** The size of RTGCPTR. */
669 uint8_t cbGCPtr;
670 /** Padding. */
671 uint8_t au8Reserved;
672} SSMFILEHDRV12;
673AssertCompileSize(SSMFILEHDRV12, 64+16);
674AssertCompileMemberOffset(SSMFILEHDRV12, u32CRC, 40);
675AssertCompileMemberSize(SSMFILEHDRV12, achMagic, sizeof(SSMFILEHDR_MAGIC_V1_2));
676/** Pointer to a saved state file header. */
677typedef SSMFILEHDRV12 *PSSMFILEHDRV12;
678
679
680/**
681 * Header of the saved state file, version 1.1.
682 *
683 * Added in r23677 on 2007-08-17, VirtualBox v1.4.1.
684 */
685typedef struct SSMFILEHDRV11
686{
687 /** Magic string which identifies this file as a version of VBox saved state
688 * file format (SSMFILEHDR_MAGIC_V1_1). */
689 char achMagic[32];
690 /** The size of this file. Used to check
691 * whether the save completed and that things are fine otherwise. */
692 uint64_t cbFile;
693 /** File checksum. The actual calculation skips past the u32CRC field. */
694 uint32_t u32CRC;
695 /** Padding. */
696 uint32_t u32Reserved;
697 /** The machine UUID. (Ignored if NIL.) */
698 RTUUID MachineUuid;
699} SSMFILEHDRV11;
700AssertCompileSize(SSMFILEHDRV11, 64);
701AssertCompileMemberOffset(SSMFILEHDRV11, u32CRC, 40);
702/** Pointer to a saved state file header. */
703typedef SSMFILEHDRV11 *PSSMFILEHDRV11;
704
705
706/**
707 * Data unit header.
708 */
709typedef struct SSMFILEUNITHDRV2
710{
711 /** Magic (SSMFILEUNITHDR_MAGIC or SSMFILEUNITHDR_END). */
712 char szMagic[8];
713 /** The offset in the saved state stream of the start of this unit.
714 * This is mainly intended for sanity checking. */
715 uint64_t offStream;
716 /** The CRC-in-progress value this unit starts at. */
717 uint32_t u32CurStreamCRC;
718 /** The checksum of this structure, including the whole name.
719 * Calculated with this field set to zero. */
720 uint32_t u32CRC;
721 /** Data version. */
722 uint32_t u32Version;
723 /** Instance number. */
724 uint32_t u32Instance;
725 /** Data pass number. */
726 uint32_t u32Pass;
727 /** Flags reserved for future extensions. Must be zero. */
728 uint32_t fFlags;
729 /** Size of the data unit name including the terminator. (bytes) */
730 uint32_t cbName;
731 /** Data unit name, variable size. */
732 char szName[SSM_MAX_NAME_SIZE];
733} SSMFILEUNITHDRV2;
734AssertCompileMemberOffset(SSMFILEUNITHDRV2, szName, 44);
735AssertCompileMemberSize(SSMFILEUNITHDRV2, szMagic, sizeof(SSMFILEUNITHDR_MAGIC));
736AssertCompileMemberSize(SSMFILEUNITHDRV2, szMagic, sizeof(SSMFILEUNITHDR_END));
737/** Pointer to SSMFILEUNITHDRV2. */
738typedef SSMFILEUNITHDRV2 *PSSMFILEUNITHDRV2;
739
740
741/**
742 * Data unit header.
743 *
744 * This is used by v1.0, v1.1 and v1.2 of the format.
745 */
746typedef struct SSMFILEUNITHDRV1
747{
748 /** Magic (SSMFILEUNITHDR_MAGIC or SSMFILEUNITHDR_END). */
749 char achMagic[8];
750 /** Number of bytes in this data unit including the header. */
751 uint64_t cbUnit;
752 /** Data version. */
753 uint32_t u32Version;
754 /** Instance number. */
755 uint32_t u32Instance;
756 /** Size of the data unit name including the terminator. (bytes) */
757 uint32_t cchName;
758 /** Data unit name. */
759 char szName[1];
760} SSMFILEUNITHDRV1;
761/** Pointer to SSMFILEUNITHDR. */
762typedef SSMFILEUNITHDRV1 *PSSMFILEUNITHDRV1;
763
764
765/**
766 * Termination data record.
767 */
768typedef struct SSMRECTERM
769{
770 uint8_t u8TypeAndFlags;
771 /** The record size (sizeof(SSMRECTERM) - 2). */
772 uint8_t cbRec;
773 /** Flags, see SSMRECTERM_FLAGS_CRC32. */
774 uint16_t fFlags;
775 /** The checksum of the stream up to fFlags (exclusive). */
776 uint32_t u32StreamCRC;
777 /** The length of this data unit in bytes (including this record). */
778 uint64_t cbUnit;
779} SSMRECTERM;
780AssertCompileSize(SSMRECTERM, 16);
781AssertCompileMemberAlignment(SSMRECTERM, cbUnit, 8);
782/** Pointer to a termination record. */
783typedef SSMRECTERM *PSSMRECTERM;
784/** Pointer to a const termination record. */
785typedef SSMRECTERM const *PCSSMRECTERM;
786
787
788/**
789 * Directory entry.
790 */
791typedef struct SSMFILEDIRENTRY
792{
793 /** The offset of the data unit. */
794 uint64_t off;
795 /** The instance number. */
796 uint32_t u32Instance;
797 /** The CRC-32 of the name excluding the terminator. (lazy bird) */
798 uint32_t u32NameCRC;
799} SSMFILEDIRENTRY;
800AssertCompileSize(SSMFILEDIRENTRY, 16);
801/** Pointer to a directory entry. */
802typedef SSMFILEDIRENTRY *PSSMFILEDIRENTRY;
803/** Pointer to a const directory entry. */
804typedef SSMFILEDIRENTRY const *PCSSMFILEDIRENTRY;
805
806/**
807 * Directory for the data units from the final pass.
808 *
809 * This is used to speed up SSMR3Seek (it would have to decompress and parse the
810 * whole stream otherwise).
811 */
812typedef struct SSMFILEDIR
813{
814 /** Magic string (SSMFILEDIR_MAGIC). */
815 char szMagic[8];
816 /** The CRC-32 for the whole directory.
817 * Calculated with this field set to zero. */
818 uint32_t u32CRC;
819 /** The number of directory entries. */
820 uint32_t cEntries;
821 /** The directory entries (variable size). */
822 SSMFILEDIRENTRY aEntries[1];
823} SSMFILEDIR;
824AssertCompileSize(SSMFILEDIR, 32);
825/** Pointer to a directory. */
826typedef SSMFILEDIR *PSSMFILEDIR;
827/** Pointer to a const directory. */
828typedef SSMFILEDIR *PSSMFILEDIR;
829
830
831/**
832 * Footer structure
833 */
834typedef struct SSMFILEFTR
835{
836 /** Magic string (SSMFILEFTR_MAGIC). */
837 char szMagic[8];
838 /** The offset of this record in the stream. */
839 uint64_t offStream;
840 /** The CRC for the stream.
841 * This is set to zero if SSMFILEHDR_FLAGS_STREAM_CRC32 is clear. */
842 uint32_t u32StreamCRC;
843 /** Number directory entries. */
844 uint32_t cDirEntries;
845 /** Reserved footer space - must be zero. */
846 uint32_t u32Reserved;
847 /** The CRC-32 for this structure.
848 * Calculated with this field set to zero. */
849 uint32_t u32CRC;
850} SSMFILEFTR;
851AssertCompileSize(SSMFILEFTR, 32);
852/** Pointer to a footer. */
853typedef SSMFILEFTR *PSSMFILEFTR;
854/** Pointer to a const footer. */
855typedef SSMFILEFTR const *PCSSMFILEFTR;
856
857
858/*******************************************************************************
859* Global Variables *
860*******************************************************************************/
861/** Zeros used by the struct putter.
862 * This must be at least 8 bytes or the code breaks. */
863static uint8_t const g_abZero[_1K] = {0};
864
865
866/*******************************************************************************
867* Internal Functions *
868*******************************************************************************/
869#ifndef SSM_STANDALONE
870static int ssmR3LazyInit(PVM pVM);
871static DECLCALLBACK(int) ssmR3SelfLiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass);
872static DECLCALLBACK(int) ssmR3SelfSaveExec(PVM pVM, PSSMHANDLE pSSM);
873static DECLCALLBACK(int) ssmR3SelfLoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
874static int ssmR3Register(PVM pVM, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess, const char *pszBefore, PSSMUNIT *ppUnit);
875#endif
876
877static int ssmR3StrmWriteBuffers(PSSMSTRM pStrm);
878static int ssmR3StrmReadMore(PSSMSTRM pStrm);
879
880static int ssmR3DataFlushBuffer(PSSMHANDLE pSSM);
881static int ssmR3DataReadRecHdrV2(PSSMHANDLE pSSM);
882
883
884#ifndef SSM_STANDALONE
885
886/**
887 * Cleans up resources allocated by SSM on VM termination.
888 *
889 * @param pVM The VM handle.
890 */
891VMMR3_INT_DECL(void) SSMR3Term(PVM pVM)
892{
893 if (pVM->ssm.s.fInitialized)
894 {
895 pVM->ssm.s.fInitialized = false;
896 RTCritSectDelete(&pVM->ssm.s.CancelCritSect);
897 }
898}
899
900
901/**
902 * Performs lazy initialization of the SSM.
903 *
904 * @returns VBox status code.
905 * @param pVM The VM.
906 */
907static int ssmR3LazyInit(PVM pVM)
908{
909 /*
910 * Register a saved state unit which we use to put the VirtualBox version,
911 * revision and similar stuff in.
912 */
913 pVM->ssm.s.fInitialized = true;
914 int rc = SSMR3RegisterInternal(pVM, "SSM", 0 /*uInstance*/, 1 /*uVersion*/, 64 /*cbGuess*/,
915 NULL /*pfnLivePrep*/, ssmR3SelfLiveExec, NULL /*pfnLiveVote*/,
916 NULL /*pfnSavePrep*/, ssmR3SelfSaveExec, NULL /*pfnSaveDone*/,
917 NULL /*pfnSavePrep*/, ssmR3SelfLoadExec, NULL /*pfnSaveDone*/);
918
919 /*
920 * Initialize the cancellation critsect now.
921 */
922 if (RT_SUCCESS(rc))
923 rc = RTCritSectInit(&pVM->ssm.s.CancelCritSect);
924 if (RT_SUCCESS(rc))
925 {
926 STAM_REL_REG_USED(pVM, &pVM->ssm.s.uPass, STAMTYPE_U32, "/SSM/uPass", STAMUNIT_COUNT, "Current pass");
927 }
928
929 pVM->ssm.s.fInitialized = RT_SUCCESS(rc);
930 return rc;
931}
932
933
934/**
935 * Do ssmR3SelfSaveExec in pass 0.
936 *
937 * @returns VBox status code.
938 * @param pVM Pointer to the shared VM structure.
939 * @param pSSM The SSM handle.
940 * @param uPass The data pass number.
941 */
942static DECLCALLBACK(int) ssmR3SelfLiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
943{
944 if (uPass == 0)
945 {
946 int rc = ssmR3SelfSaveExec(pVM, pSSM);
947 if (RT_SUCCESS(rc))
948 rc = VINF_SSM_DONT_CALL_AGAIN;
949 return rc;
950 }
951 AssertFailed();
952 return VERR_INTERNAL_ERROR_3;
953}
954
955
956/**
957 * For saving usful things without having to go thru the tedious process of
958 * adding it to the header.
959 *
960 * @returns VBox status code.
961 * @param pVM Pointer to the shared VM structure.
962 * @param pSSM The SSM handle.
963 */
964static DECLCALLBACK(int) ssmR3SelfSaveExec(PVM pVM, PSSMHANDLE pSSM)
965{
966 /*
967 * String table containg pairs of variable and value string.
968 * Terminated by two empty strings.
969 */
970 SSMR3PutStrZ(pSSM, "Build Type");
971 SSMR3PutStrZ(pSSM, KBUILD_TYPE);
972 SSMR3PutStrZ(pSSM, "Host OS");
973 SSMR3PutStrZ(pSSM, KBUILD_TARGET "." KBUILD_TARGET_ARCH);
974#ifdef VBOX_OSE
975 SSMR3PutStrZ(pSSM, "OSE");
976 SSMR3PutStrZ(pSSM, "true");
977#endif
978
979 /* terminator */
980 SSMR3PutStrZ(pSSM, "");
981 return SSMR3PutStrZ(pSSM, "");
982}
983
984
985/**
986 * For load the version + revision and stuff.
987 *
988 * @returns VBox status code.
989 * @param pVM Pointer to the shared VM structure.
990 * @param pSSM The SSM handle.
991 * @param uVersion The version (1).
992 * @param uPass The pass.
993 */
994static DECLCALLBACK(int) ssmR3SelfLoadExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
995{
996 AssertLogRelMsgReturn(uVersion == 1, ("%d", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
997
998 /*
999 * The first and last passes contains a {name, value} string table that is
1000 * terminated by two emptry strings. It contains useful informal build
1001 * info and can be very handy when something goes wrong after restore.
1002 */
1003 if ( uPass == 0
1004 || uPass == SSM_PASS_FINAL)
1005 {
1006 for (unsigned i = 0; ; i++)
1007 {
1008 char szVar[128];
1009 char szValue[1024];
1010 int rc = SSMR3GetStrZ(pSSM, szVar, sizeof(szVar));
1011 AssertRCReturn(rc, rc);
1012 rc = SSMR3GetStrZ(pSSM, szValue, sizeof(szValue));
1013 AssertRCReturn(rc, rc);
1014 if (!szVar[0] && !szValue[0])
1015 break;
1016 if (i == 0)
1017 LogRel(("SSM: Saved state info:\n"));
1018 LogRel(("SSM: %s: %s\n", szVar, szValue));
1019
1020 /*
1021 * Detect 32-bit MSC for handling SSMFIELD_ENTRY_PAD_MSC32_AUTO.
1022 * Save the Host OS for SSMR3HandleHostOSAndArch
1023 */
1024 if (!strcmp(szVar, "Host OS"))
1025 {
1026 bool fIsHostMsc32 = !strcmp(szValue, "win.x86");
1027 if (fIsHostMsc32 != pSSM->u.Read.fIsHostMsc32)
1028 {
1029 LogRel(("SSM: (fIsHostMsc32 %RTbool => %RTbool)\n", pSSM->u.Read.fIsHostMsc32, fIsHostMsc32));
1030 pSSM->u.Read.fIsHostMsc32 = fIsHostMsc32;
1031 }
1032
1033 size_t cchValue = strlen(szValue);
1034 size_t cchCopy = RT_MIN(cchValue, sizeof(pSSM->u.Read.szHostOSAndArch) - 1);
1035 Assert(cchValue == cchCopy);
1036 memcpy(pSSM->u.Read.szHostOSAndArch, szValue, cchCopy);
1037 pSSM->u.Read.szHostOSAndArch[cchCopy] = '\0';
1038 }
1039 }
1040 }
1041 return VINF_SUCCESS;
1042}
1043
1044
1045/**
1046 * Internal registration worker.
1047 *
1048 * @returns VBox status code.
1049 * @param pVM The VM handle.
1050 * @param pszName Data unit name.
1051 * @param uInstance The instance id.
1052 * @param uVersion The data unit version.
1053 * @param cbGuess The guessed data unit size.
1054 * @param pszBefore Name of data unit to be placed in front of.
1055 * Optional.
1056 * @param ppUnit Where to store the insterted unit node.
1057 * Caller must fill in the missing details.
1058 */
1059static int ssmR3Register(PVM pVM, const char *pszName, uint32_t uInstance,
1060 uint32_t uVersion, size_t cbGuess, const char *pszBefore, PSSMUNIT *ppUnit)
1061{
1062 /*
1063 * Validate input.
1064 */
1065 AssertPtr(pszName);
1066 AssertReturn(*pszName, VERR_INVALID_PARAMETER);
1067 size_t cchName = strlen(pszName);
1068 AssertMsgReturn(cchName < SSM_MAX_NAME_SIZE, ("%zu >= %u: %s\n", cchName, SSM_MAX_NAME_SIZE, pszName), VERR_OUT_OF_RANGE);
1069
1070 AssertReturn(!pszBefore || *pszBefore, VERR_INVALID_PARAMETER);
1071 size_t cchBefore = pszBefore ? strlen(pszBefore) : 0;
1072 AssertMsgReturn(cchBefore < SSM_MAX_NAME_SIZE, ("%zu >= %u: %s\n", cchBefore, SSM_MAX_NAME_SIZE, pszBefore), VERR_OUT_OF_RANGE);
1073
1074 /*
1075 * Lazy init.
1076 */
1077 if (!pVM->ssm.s.fInitialized)
1078 {
1079 int rc = ssmR3LazyInit(pVM);
1080 AssertRCReturn(rc, rc);
1081 }
1082
1083 /*
1084 * Walk to the end of the list checking for duplicates as we go.
1085 */
1086 PSSMUNIT pUnitBeforePrev = NULL;
1087 PSSMUNIT pUnitBefore = NULL;
1088 PSSMUNIT pUnitPrev = NULL;
1089 PSSMUNIT pUnit = pVM->ssm.s.pHead;
1090 while (pUnit)
1091 {
1092 if ( pUnit->u32Instance == uInstance
1093 && pUnit->cchName == cchName
1094 && !memcmp(pUnit->szName, pszName, cchName))
1095 {
1096 AssertMsgFailed(("Duplicate registration %s\n", pszName));
1097 return VERR_SSM_UNIT_EXISTS;
1098 }
1099 if ( pUnit->cchName == cchBefore
1100 && !pUnitBefore
1101 && !memcmp(pUnit->szName, pszBefore, cchBefore))
1102 {
1103 pUnitBeforePrev = pUnitPrev;
1104 pUnitBefore = pUnit;
1105 }
1106
1107 /* next */
1108 pUnitPrev = pUnit;
1109 pUnit = pUnit->pNext;
1110 }
1111
1112 /*
1113 * Allocate new node.
1114 */
1115 pUnit = (PSSMUNIT)MMR3HeapAllocZ(pVM, MM_TAG_SSM, RT_OFFSETOF(SSMUNIT, szName[cchName + 1]));
1116 if (!pUnit)
1117 return VERR_NO_MEMORY;
1118
1119 /*
1120 * Fill in (some) data. (Stuff is zero'ed.)
1121 */
1122 pUnit->u32Version = uVersion;
1123 pUnit->u32Instance = uInstance;
1124 pUnit->cbGuess = cbGuess;
1125 pUnit->cchName = cchName;
1126 memcpy(pUnit->szName, pszName, cchName);
1127
1128 /*
1129 * Insert
1130 */
1131 if (pUnitBefore)
1132 {
1133 pUnit->pNext = pUnitBefore;
1134 if (pUnitBeforePrev)
1135 pUnitBeforePrev->pNext = pUnit;
1136 else
1137 pVM->ssm.s.pHead = pUnit;
1138 }
1139 else if (pUnitPrev)
1140 pUnitPrev->pNext = pUnit;
1141 else
1142 pVM->ssm.s.pHead = pUnit;
1143 pVM->ssm.s.cUnits++;
1144
1145 *ppUnit = pUnit;
1146 return VINF_SUCCESS;
1147}
1148
1149
1150/**
1151 * Register a PDM Devices data unit.
1152 *
1153 * @returns VBox status.
1154 *
1155 * @param pVM The VM handle.
1156 * @param pDevIns Device instance.
1157 * @param pszName Data unit name.
1158 * @param uInstance The instance identifier of the data unit.
1159 * This must together with the name be unique.
1160 * @param uVersion Data layout version number.
1161 * @param cbGuess The approximate amount of data in the unit.
1162 * Only for progress indicators.
1163 * @param pszBefore Name of data unit which we should be put in front
1164 * of. Optional (NULL).
1165 *
1166 * @param pfnLivePrep Prepare live save callback, optional.
1167 * @param pfnLiveExec Execute live save callback, optional.
1168 * @param pfnLiveVote Vote live save callback, optional.
1169 *
1170 * @param pfnSavePrep Prepare save callback, optional.
1171 * @param pfnSaveExec Execute save callback, optional.
1172 * @param pfnSaveDone Done save callback, optional.
1173 *
1174 * @param pfnLoadPrep Prepare load callback, optional.
1175 * @param pfnLoadExec Execute load callback, optional.
1176 * @param pfnLoadDone Done load callback, optional.
1177 */
1178VMMR3DECL(int) SSMR3RegisterDevice(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess, const char *pszBefore,
1179 PFNSSMDEVLIVEPREP pfnLivePrep, PFNSSMDEVLIVEEXEC pfnLiveExec, PFNSSMDEVLIVEVOTE pfnLiveVote,
1180 PFNSSMDEVSAVEPREP pfnSavePrep, PFNSSMDEVSAVEEXEC pfnSaveExec, PFNSSMDEVSAVEDONE pfnSaveDone,
1181 PFNSSMDEVLOADPREP pfnLoadPrep, PFNSSMDEVLOADEXEC pfnLoadExec, PFNSSMDEVLOADDONE pfnLoadDone)
1182{
1183 PSSMUNIT pUnit;
1184 int rc = ssmR3Register(pVM, pszName, uInstance, uVersion, cbGuess, pszBefore, &pUnit);
1185 if (RT_SUCCESS(rc))
1186 {
1187 pUnit->enmType = SSMUNITTYPE_DEV;
1188 pUnit->u.Dev.pfnLivePrep = pfnLivePrep;
1189 pUnit->u.Dev.pfnLiveExec = pfnLiveExec;
1190 pUnit->u.Dev.pfnLiveVote = pfnLiveVote;
1191 pUnit->u.Dev.pfnSavePrep = pfnSavePrep;
1192 pUnit->u.Dev.pfnSaveExec = pfnSaveExec;
1193 pUnit->u.Dev.pfnSaveDone = pfnSaveDone;
1194 pUnit->u.Dev.pfnLoadPrep = pfnLoadPrep;
1195 pUnit->u.Dev.pfnLoadExec = pfnLoadExec;
1196 pUnit->u.Dev.pfnLoadDone = pfnLoadDone;
1197 pUnit->u.Dev.pDevIns = pDevIns;
1198 }
1199 return rc;
1200}
1201
1202
1203/**
1204 * Register a PDM driver data unit.
1205 *
1206 * @returns VBox status.
1207 *
1208 * @param pVM The VM handle.
1209 * @param pDrvIns Driver instance.
1210 * @param pszName Data unit name.
1211 * @param uInstance The instance identifier of the data unit.
1212 * This must together with the name be unique.
1213 * @param uVersion Data layout version number.
1214 * @param cbGuess The approximate amount of data in the unit.
1215 * Only for progress indicators.
1216 *
1217 * @param pfnLivePrep Prepare live save callback, optional.
1218 * @param pfnLiveExec Execute live save callback, optional.
1219 * @param pfnLiveVote Vote live save callback, optional.
1220 *
1221 * @param pfnSavePrep Prepare save callback, optional.
1222 * @param pfnSaveExec Execute save callback, optional.
1223 * @param pfnSaveDone Done save callback, optional.
1224 *
1225 * @param pfnLoadPrep Prepare load callback, optional.
1226 * @param pfnLoadExec Execute load callback, optional.
1227 * @param pfnLoadDone Done load callback, optional.
1228 */
1229VMMR3DECL(int) SSMR3RegisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess,
1230 PFNSSMDRVLIVEPREP pfnLivePrep, PFNSSMDRVLIVEEXEC pfnLiveExec, PFNSSMDRVLIVEVOTE pfnLiveVote,
1231 PFNSSMDRVSAVEPREP pfnSavePrep, PFNSSMDRVSAVEEXEC pfnSaveExec, PFNSSMDRVSAVEDONE pfnSaveDone,
1232 PFNSSMDRVLOADPREP pfnLoadPrep, PFNSSMDRVLOADEXEC pfnLoadExec, PFNSSMDRVLOADDONE pfnLoadDone)
1233{
1234 PSSMUNIT pUnit;
1235 int rc = ssmR3Register(pVM, pszName, uInstance, uVersion, cbGuess, NULL, &pUnit);
1236 if (RT_SUCCESS(rc))
1237 {
1238 pUnit->enmType = SSMUNITTYPE_DRV;
1239 pUnit->u.Drv.pfnSavePrep = pfnSavePrep;
1240 pUnit->u.Drv.pfnSaveExec = pfnSaveExec;
1241 pUnit->u.Drv.pfnSaveDone = pfnSaveDone;
1242 pUnit->u.Drv.pfnLoadPrep = pfnLoadPrep;
1243 pUnit->u.Drv.pfnLoadExec = pfnLoadExec;
1244 pUnit->u.Drv.pfnLoadDone = pfnLoadDone;
1245 pUnit->u.Drv.pDrvIns = pDrvIns;
1246 }
1247 return rc;
1248}
1249
1250
1251/**
1252 * Register a internal data unit.
1253 *
1254 * @returns VBox status.
1255 *
1256 * @param pVM The VM handle.
1257 * @param pszName Data unit name.
1258 * @param uInstance The instance identifier of the data unit.
1259 * This must together with the name be unique.
1260 * @param uVersion Data layout version number.
1261 * @param cbGuess The approximate amount of data in the unit.
1262 * Only for progress indicators.
1263 *
1264 * @param pfnLivePrep Prepare live save callback, optional.
1265 * @param pfnLiveExec Execute live save callback, optional.
1266 * @param pfnLiveVote Vote live save callback, optional.
1267 *
1268 * @param pfnSavePrep Prepare save callback, optional.
1269 * @param pfnSaveExec Execute save callback, optional.
1270 * @param pfnSaveDone Done save callback, optional.
1271 *
1272 * @param pfnLoadPrep Prepare load callback, optional.
1273 * @param pfnLoadExec Execute load callback, optional.
1274 * @param pfnLoadDone Done load callback, optional.
1275 */
1276VMMR3DECL(int) SSMR3RegisterInternal(PVM pVM, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess,
1277 PFNSSMINTLIVEPREP pfnLivePrep, PFNSSMINTLIVEEXEC pfnLiveExec, PFNSSMINTLIVEVOTE pfnLiveVote,
1278 PFNSSMINTSAVEPREP pfnSavePrep, PFNSSMINTSAVEEXEC pfnSaveExec, PFNSSMINTSAVEDONE pfnSaveDone,
1279 PFNSSMINTLOADPREP pfnLoadPrep, PFNSSMINTLOADEXEC pfnLoadExec, PFNSSMINTLOADDONE pfnLoadDone)
1280{
1281 PSSMUNIT pUnit;
1282 int rc = ssmR3Register(pVM, pszName, uInstance, uVersion, cbGuess, NULL, &pUnit);
1283 if (RT_SUCCESS(rc))
1284 {
1285 pUnit->enmType = SSMUNITTYPE_INTERNAL;
1286 pUnit->u.Internal.pfnLivePrep = pfnLivePrep;
1287 pUnit->u.Internal.pfnLiveExec = pfnLiveExec;
1288 pUnit->u.Internal.pfnLiveVote = pfnLiveVote;
1289 pUnit->u.Internal.pfnSavePrep = pfnSavePrep;
1290 pUnit->u.Internal.pfnSaveExec = pfnSaveExec;
1291 pUnit->u.Internal.pfnSaveDone = pfnSaveDone;
1292 pUnit->u.Internal.pfnLoadPrep = pfnLoadPrep;
1293 pUnit->u.Internal.pfnLoadExec = pfnLoadExec;
1294 pUnit->u.Internal.pfnLoadDone = pfnLoadDone;
1295 }
1296 return rc;
1297}
1298
1299
1300/**
1301 * Register an external data unit.
1302 *
1303 * @returns VBox status.
1304 *
1305 * @param pVM The VM handle.
1306 * @param pszName Data unit name.
1307 * @param uInstance The instance identifier of the data unit.
1308 * This must together with the name be unique.
1309 * @param uVersion Data layout version number.
1310 * @param cbGuess The approximate amount of data in the unit.
1311 * Only for progress indicators.
1312 *
1313 * @param pfnLivePrep Prepare live save callback, optional.
1314 * @param pfnLiveExec Execute live save callback, optional.
1315 * @param pfnLiveVote Vote live save callback, optional.
1316 *
1317 * @param pfnSavePrep Prepare save callback, optional.
1318 * @param pfnSaveExec Execute save callback, optional.
1319 * @param pfnSaveDone Done save callback, optional.
1320 *
1321 * @param pfnLoadPrep Prepare load callback, optional.
1322 * @param pfnLoadExec Execute load callback, optional.
1323 * @param pfnLoadDone Done load callback, optional.
1324 * @param pvUser User argument.
1325 */
1326VMMR3DECL(int) SSMR3RegisterExternal(PVM pVM, const char *pszName, uint32_t uInstance, uint32_t uVersion, size_t cbGuess,
1327 PFNSSMEXTLIVEPREP pfnLivePrep, PFNSSMEXTLIVEEXEC pfnLiveExec, PFNSSMEXTLIVEVOTE pfnLiveVote,
1328 PFNSSMEXTSAVEPREP pfnSavePrep, PFNSSMEXTSAVEEXEC pfnSaveExec, PFNSSMEXTSAVEDONE pfnSaveDone,
1329 PFNSSMEXTLOADPREP pfnLoadPrep, PFNSSMEXTLOADEXEC pfnLoadExec, PFNSSMEXTLOADDONE pfnLoadDone, void *pvUser)
1330{
1331 PSSMUNIT pUnit;
1332 int rc = ssmR3Register(pVM, pszName, uInstance, uVersion, cbGuess, NULL, &pUnit);
1333 if (RT_SUCCESS(rc))
1334 {
1335 pUnit->enmType = SSMUNITTYPE_EXTERNAL;
1336 pUnit->u.External.pfnLivePrep = pfnLivePrep;
1337 pUnit->u.External.pfnLiveExec = pfnLiveExec;
1338 pUnit->u.External.pfnLiveVote = pfnLiveVote;
1339 pUnit->u.External.pfnSavePrep = pfnSavePrep;
1340 pUnit->u.External.pfnSaveExec = pfnSaveExec;
1341 pUnit->u.External.pfnSaveDone = pfnSaveDone;
1342 pUnit->u.External.pfnLoadPrep = pfnLoadPrep;
1343 pUnit->u.External.pfnLoadExec = pfnLoadExec;
1344 pUnit->u.External.pfnLoadDone = pfnLoadDone;
1345 pUnit->u.External.pvUser = pvUser;
1346 }
1347 return rc;
1348}
1349
1350
1351/**
1352 * Deregister one or more PDM Device data units.
1353 *
1354 * @returns VBox status.
1355 *
1356 * @param pVM The VM handle.
1357 * @param pDevIns Device instance.
1358 * @param pszName Data unit name.
1359 * Use NULL to deregister all data units for that device instance.
1360 * @param uInstance The instance identifier of the data unit.
1361 * This must together with the name be unique.
1362 * @remark Only for dynmaic data units and dynamic unloaded modules.
1363 */
1364VMMR3_INT_DECL(int) SSMR3DeregisterDevice(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t uInstance)
1365{
1366 /*
1367 * Validate input.
1368 */
1369 if (!pDevIns)
1370 {
1371 AssertMsgFailed(("pDevIns is NULL!\n"));
1372 return VERR_INVALID_PARAMETER;
1373 }
1374
1375 /*
1376 * Search the list.
1377 */
1378 size_t cchName = pszName ? strlen(pszName) : 0;
1379 int rc = pszName ? VERR_SSM_UNIT_NOT_FOUND : VINF_SUCCESS;
1380 PSSMUNIT pUnitPrev = NULL;
1381 PSSMUNIT pUnit = pVM->ssm.s.pHead;
1382 while (pUnit)
1383 {
1384 if ( pUnit->enmType == SSMUNITTYPE_DEV
1385 && ( !pszName
1386 || ( pUnit->cchName == cchName
1387 && !memcmp(pUnit->szName, pszName, cchName)))
1388 && pUnit->u32Instance == uInstance
1389 )
1390 {
1391 if (pUnit->u.Dev.pDevIns == pDevIns)
1392 {
1393 /*
1394 * Unlink it, advance pointer, and free the node.
1395 */
1396 PSSMUNIT pFree = pUnit;
1397 pUnit = pUnit->pNext;
1398 if (pUnitPrev)
1399 pUnitPrev->pNext = pUnit;
1400 else
1401 pVM->ssm.s.pHead = pUnit;
1402 pVM->ssm.s.cUnits--;
1403 Log(("SSM: Removed data unit '%s' (pdm dev).\n", pFree->szName));
1404 MMR3HeapFree(pFree);
1405
1406 if (pszName)
1407 return VINF_SUCCESS;
1408 rc = VINF_SUCCESS;
1409 continue;
1410 }
1411 else if (pszName)
1412 {
1413 AssertMsgFailed(("Caller is not owner! Owner=%p Caller=%p %s\n",
1414 pUnit->u.Dev.pDevIns, pDevIns, pszName));
1415 return VERR_SSM_UNIT_NOT_OWNER;
1416 }
1417 }
1418
1419 /* next */
1420 pUnitPrev = pUnit;
1421 pUnit = pUnit->pNext;
1422 }
1423
1424 return rc;
1425}
1426
1427
1428/**
1429 * Deregister one ore more PDM Driver data units.
1430 *
1431 * @returns VBox status.
1432 * @param pVM The VM handle.
1433 * @param pDrvIns Driver instance.
1434 * @param pszName Data unit name.
1435 * Use NULL to deregister all data units for that driver instance.
1436 * @param uInstance The instance identifier of the data unit.
1437 * This must together with the name be unique. Ignored if pszName is NULL.
1438 * @remark Only for dynmaic data units and dynamic unloaded modules.
1439 */
1440VMMR3_INT_DECL(int) SSMR3DeregisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t uInstance)
1441{
1442 /*
1443 * Validate input.
1444 */
1445 if (!pDrvIns)
1446 {
1447 AssertMsgFailed(("pDrvIns is NULL!\n"));
1448 return VERR_INVALID_PARAMETER;
1449 }
1450
1451 /*
1452 * Search the list.
1453 */
1454 size_t cchName = pszName ? strlen(pszName) : 0;
1455 int rc = pszName ? VERR_SSM_UNIT_NOT_FOUND : VINF_SUCCESS;
1456 PSSMUNIT pUnitPrev = NULL;
1457 PSSMUNIT pUnit = pVM->ssm.s.pHead;
1458 while (pUnit)
1459 {
1460 if ( pUnit->enmType == SSMUNITTYPE_DRV
1461 && ( !pszName
1462 || ( pUnit->cchName == cchName
1463 && !memcmp(pUnit->szName, pszName, cchName)
1464 && pUnit->u32Instance == uInstance))
1465 )
1466 {
1467 if (pUnit->u.Drv.pDrvIns == pDrvIns)
1468 {
1469 /*
1470 * Unlink it, advance pointer, and free the node.
1471 */
1472 PSSMUNIT pFree = pUnit;
1473 pUnit = pUnit->pNext;
1474 if (pUnitPrev)
1475 pUnitPrev->pNext = pUnit;
1476 else
1477 pVM->ssm.s.pHead = pUnit;
1478 pVM->ssm.s.cUnits--;
1479 Log(("SSM: Removed data unit '%s' (pdm drv).\n", pFree->szName));
1480 MMR3HeapFree(pFree);
1481
1482 if (pszName)
1483 return VINF_SUCCESS;
1484 rc = VINF_SUCCESS;
1485 continue;
1486 }
1487
1488 AssertMsgReturn(!pszName,
1489 ("Caller is not owner! Owner=%p Caller=%p %s\n", pUnit->u.Drv.pDrvIns, pDrvIns, pszName),
1490 VERR_SSM_UNIT_NOT_OWNER);
1491 }
1492
1493 /* next */
1494 pUnitPrev = pUnit;
1495 pUnit = pUnit->pNext;
1496 }
1497
1498 return rc;
1499}
1500
1501
1502/**
1503 * Deregister a data unit.
1504 *
1505 * @returns VBox status.
1506 * @param pVM The VM handle.
1507 * @param enmType Unit type
1508 * @param pszName Data unit name.
1509 * @remark Only for dynmaic data units.
1510 */
1511static int ssmR3DeregisterByNameAndType(PVM pVM, const char *pszName, SSMUNITTYPE enmType)
1512{
1513 /*
1514 * Validate input.
1515 */
1516 if (!pszName)
1517 {
1518 AssertMsgFailed(("pszName is NULL!\n"));
1519 return VERR_INVALID_PARAMETER;
1520 }
1521
1522 /*
1523 * Search the list.
1524 */
1525 size_t cchName = strlen(pszName);
1526 int rc = VERR_SSM_UNIT_NOT_FOUND;
1527 PSSMUNIT pUnitPrev = NULL;
1528 PSSMUNIT pUnit = pVM->ssm.s.pHead;
1529 while (pUnit)
1530 {
1531 if ( pUnit->enmType == enmType
1532 && pUnit->cchName == cchName
1533 && !memcmp(pUnit->szName, pszName, cchName))
1534 {
1535 /*
1536 * Unlink it, advance pointer, and free the node.
1537 */
1538 PSSMUNIT pFree = pUnit;
1539 pUnit = pUnit->pNext;
1540 if (pUnitPrev)
1541 pUnitPrev->pNext = pUnit;
1542 else
1543 pVM->ssm.s.pHead = pUnit;
1544 pVM->ssm.s.cUnits--;
1545 Log(("SSM: Removed data unit '%s' (type=%d).\n", pFree->szName, enmType));
1546 MMR3HeapFree(pFree);
1547 return VINF_SUCCESS;
1548 }
1549
1550 /* next */
1551 pUnitPrev = pUnit;
1552 pUnit = pUnit->pNext;
1553 }
1554
1555 return rc;
1556}
1557
1558
1559/**
1560 * Deregister an internal data unit.
1561 *
1562 * @returns VBox status.
1563 * @param pVM The VM handle.
1564 * @param pszName Data unit name.
1565 * @remark Only for dynmaic data units.
1566 */
1567VMMR3DECL(int) SSMR3DeregisterInternal(PVM pVM, const char *pszName)
1568{
1569 return ssmR3DeregisterByNameAndType(pVM, pszName, SSMUNITTYPE_INTERNAL);
1570}
1571
1572
1573/**
1574 * Deregister an external data unit.
1575 *
1576 * @returns VBox status.
1577 * @param pVM The VM handle.
1578 * @param pszName Data unit name.
1579 * @remark Only for dynmaic data units.
1580 */
1581VMMR3DECL(int) SSMR3DeregisterExternal(PVM pVM, const char *pszName)
1582{
1583 return ssmR3DeregisterByNameAndType(pVM, pszName, SSMUNITTYPE_EXTERNAL);
1584}
1585
1586#endif /* !SSM_STANDALONE */
1587
1588
1589/**
1590 * Initializes the stream after/before opening the file/whatever.
1591 *
1592 * @returns VINF_SUCCESS or VERR_NO_MEMORY.
1593 * @param pStrm The stream handle.
1594 * @param fChecksummed Whether the stream is to be checksummed while
1595 * written/read.
1596 * @param cBuffers The number of buffers.
1597 */
1598static int ssmR3StrmInitInternal(PSSMSTRM pStrm, bool fChecksummed, uint32_t cBuffers)
1599{
1600 Assert(cBuffers > 0);
1601
1602 /*
1603 * Init the common data members.
1604 */
1605 pStrm->fTerminating = false;
1606 pStrm->fNeedSeek = false;
1607 pStrm->rc = VINF_SUCCESS;
1608 pStrm->hIoThread = NIL_RTTHREAD;
1609 pStrm->offNeedSeekTo= UINT64_MAX;
1610
1611 pStrm->pHead = NULL;
1612 pStrm->pFree = NULL;
1613 pStrm->hEvtHead = NIL_RTSEMEVENT;
1614 pStrm->hEvtFree = NIL_RTSEMEVENT;
1615
1616 pStrm->pPending = NULL;
1617 pStrm->pCur = NULL;
1618 pStrm->offCurStream = 0;
1619 pStrm->off = 0;
1620 pStrm->fChecksummed = fChecksummed;
1621 pStrm->u32StreamCRC = fChecksummed ? RTCrc32Start() : 0;
1622 pStrm->offStreamCRC = 0;
1623
1624 /*
1625 * Allocate the buffers. Page align them in case that makes the kernel
1626 * and/or cpu happier in some way.
1627 */
1628 int rc = VINF_SUCCESS;
1629 for (uint32_t i = 0; i < cBuffers; i++)
1630 {
1631 PSSMSTRMBUF pBuf = (PSSMSTRMBUF)RTMemPageAllocZ(sizeof(*pBuf));
1632 if (!pBuf)
1633 {
1634 if (i > 2)
1635 {
1636 LogRel(("ssmR3StrmAllocBuffer: WARNING: Could only get %d stream buffers.\n", i));
1637 break;
1638 }
1639 LogRel(("ssmR3StrmAllocBuffer: Failed to allocate stream buffers. (i=%d)\n", i));
1640 return VERR_NO_MEMORY;
1641 }
1642
1643 /* link it */
1644 pBuf->pNext = pStrm->pFree;
1645 pStrm->pFree = pBuf;
1646 }
1647
1648 /*
1649 * Create the event semaphores.
1650 */
1651 rc = RTSemEventCreate(&pStrm->hEvtHead);
1652 if (RT_FAILURE(rc))
1653 return rc;
1654 rc = RTSemEventCreate(&pStrm->hEvtFree);
1655 if (RT_FAILURE(rc))
1656 return rc;
1657
1658 return VINF_SUCCESS;
1659}
1660
1661
1662/**
1663 * Destroys a list of buffers.
1664 *
1665 * @param pHead Pointer to the head.
1666 */
1667static void ssmR3StrmDestroyBufList(PSSMSTRMBUF pHead)
1668{
1669 while (pHead)
1670 {
1671 PSSMSTRMBUF pCur = pHead;
1672 pHead = pCur->pNext;
1673 pCur->pNext = NULL;
1674 RTMemPageFree(pCur, sizeof(*pCur));
1675 }
1676}
1677
1678
1679/**
1680 * Cleans up a stream after ssmR3StrmInitInternal has been called (regardless of
1681 * it succeeded or not).
1682 *
1683 * @param pStrm The stream handle.
1684 */
1685static void ssmR3StrmDelete(PSSMSTRM pStrm)
1686{
1687 RTMemPageFree(pStrm->pCur, sizeof(*pStrm->pCur));
1688 pStrm->pCur = NULL;
1689 ssmR3StrmDestroyBufList(pStrm->pHead);
1690 pStrm->pHead = NULL;
1691 ssmR3StrmDestroyBufList(pStrm->pPending);
1692 pStrm->pPending = NULL;
1693 ssmR3StrmDestroyBufList(pStrm->pFree);
1694 pStrm->pFree = NULL;
1695
1696 RTSemEventDestroy(pStrm->hEvtHead);
1697 pStrm->hEvtHead = NIL_RTSEMEVENT;
1698
1699 RTSemEventDestroy(pStrm->hEvtFree);
1700 pStrm->hEvtFree = NIL_RTSEMEVENT;
1701}
1702
1703
1704/**
1705 * Initializes a stream that uses a method table.
1706 *
1707 * @returns VBox status code.
1708 * @param pStrm The stream manager structure.
1709 * @param pStreamOps The stream method table.
1710 * @param pvUser The user argument for the stream methods.
1711 * @param fWrite Whether to open for writing or reading.
1712 * @param fChecksummed Whether the stream is to be checksummed while
1713 * written/read.
1714 * @param cBuffers The number of buffers.
1715 */
1716static int ssmR3StrmInit(PSSMSTRM pStrm, PCSSMSTRMOPS pStreamOps, void *pvUser, bool fWrite, bool fChecksummed, uint32_t cBuffers)
1717{
1718 int rc = ssmR3StrmInitInternal(pStrm, fChecksummed, cBuffers);
1719 if (RT_SUCCESS(rc))
1720 {
1721 pStrm->pOps = pStreamOps;
1722 pStrm->pvUser = pvUser;
1723 pStrm->fWrite = fWrite;
1724 return VINF_SUCCESS;
1725 }
1726
1727 ssmR3StrmDelete(pStrm);
1728 pStrm->rc = rc;
1729 return rc;
1730}
1731
1732
1733/**
1734 * @copydoc SSMSTRMOPS::pfnWrite
1735 */
1736static DECLCALLBACK(int) ssmR3FileWrite(void *pvUser, uint64_t offStream, const void *pvBuf, size_t cbToWrite)
1737{
1738 Assert(RTFileTell((RTFILE)(uintptr_t)pvUser) == offStream); NOREF(offStream);
1739 return RTFileWriteAt((RTFILE)(uintptr_t)pvUser, offStream, pvBuf, cbToWrite, NULL); /** @todo use RTFileWrite */
1740}
1741
1742
1743/**
1744 * @copydoc SSMSTRMOPS::pfnRead
1745 */
1746static DECLCALLBACK(int) ssmR3FileRead(void *pvUser, uint64_t offStream, void *pvBuf, size_t cbToRead, size_t *pcbRead)
1747{
1748 Assert(RTFileTell((RTFILE)(uintptr_t)pvUser) == offStream); NOREF(offStream);
1749 return RTFileRead((RTFILE)(uintptr_t)pvUser, pvBuf, cbToRead, pcbRead);
1750}
1751
1752
1753/**
1754 * @copydoc SSMSTRMOPS::pfnSeek
1755 */
1756static DECLCALLBACK(int) ssmR3FileSeek(void *pvUser, int64_t offSeek, unsigned uMethod, uint64_t *poffActual)
1757{
1758 return RTFileSeek((RTFILE)(uintptr_t)pvUser, offSeek, uMethod, poffActual);
1759}
1760
1761
1762/**
1763 * @copydoc SSMSTRMOPS::pfnTell
1764 */
1765static DECLCALLBACK(uint64_t) ssmR3FileTell(void *pvUser)
1766{
1767 return RTFileTell((RTFILE)(uintptr_t)pvUser);
1768}
1769
1770
1771/**
1772 * @copydoc SSMSTRMOPS::pfnSize
1773 */
1774static DECLCALLBACK(int) ssmR3FileSize(void *pvUser, uint64_t *pcb)
1775{
1776 return RTFileGetSize((RTFILE)(uintptr_t)pvUser, pcb);
1777}
1778
1779
1780/**
1781 * @copydoc SSMSTRMOPS::pfnIsOk
1782 */
1783static DECLCALLBACK(int) ssmR3FileIsOk(void *pvUser)
1784{
1785 /*
1786 * Check that there is still some space left on the disk.
1787 */
1788 RTFOFF cbFree;
1789 int rc = RTFileQueryFsSizes((RTFILE)(uintptr_t)pvUser, NULL, &cbFree, NULL, NULL);
1790#define SSM_MIN_DISK_FREE ((RTFOFF)( 10 * _1M ))
1791 if (RT_SUCCESS(rc))
1792 {
1793 if (cbFree < SSM_MIN_DISK_FREE)
1794 {
1795 LogRel(("SSM: Giving up: Low on disk space. (cbFree=%RTfoff, SSM_MIN_DISK_FREE=%RTfoff).\n",
1796 cbFree, SSM_MIN_DISK_FREE));
1797 rc = VERR_SSM_LOW_ON_DISK_SPACE;
1798 }
1799 }
1800 else if (rc == VERR_NOT_SUPPORTED)
1801 rc = VINF_SUCCESS;
1802 else
1803 AssertLogRelRC(rc);
1804 return rc;
1805}
1806
1807
1808/**
1809 * @copydoc SSMSTRMOPS::pfnClose
1810 */
1811static DECLCALLBACK(int) ssmR3FileClose(void *pvUser, bool fCancelled)
1812{
1813 NOREF(fCancelled);
1814 return RTFileClose((RTFILE)(uintptr_t)pvUser);
1815}
1816
1817
1818/**
1819 * Method table for a file based stream.
1820 */
1821static SSMSTRMOPS const g_ssmR3FileOps =
1822{
1823 SSMSTRMOPS_VERSION,
1824 ssmR3FileWrite,
1825 ssmR3FileRead,
1826 ssmR3FileSeek,
1827 ssmR3FileTell,
1828 ssmR3FileSize,
1829 ssmR3FileIsOk,
1830 ssmR3FileClose,
1831 SSMSTRMOPS_VERSION
1832};
1833
1834
1835/**
1836 * Opens a file stream.
1837 *
1838 * @returns VBox status code.
1839 * @param pStrm The stream manager structure.
1840 * @param pszFilename The file to open or create.
1841 * @param fWrite Whether to open for writing or reading.
1842 * @param fChecksummed Whether the stream is to be checksummed while
1843 * written/read.
1844 * @param cBuffers The number of buffers.
1845 */
1846static int ssmR3StrmOpenFile(PSSMSTRM pStrm, const char *pszFilename, bool fWrite, bool fChecksummed, uint32_t cBuffers)
1847{
1848 int rc = ssmR3StrmInitInternal(pStrm, fChecksummed, cBuffers);
1849 if (RT_SUCCESS(rc))
1850 {
1851 uint32_t fFlags = fWrite
1852 ? RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE
1853 : RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE;
1854 RTFILE hFile;
1855 rc = RTFileOpen(&hFile, pszFilename, fFlags);
1856 if (RT_SUCCESS(rc))
1857 {
1858 pStrm->pOps = &g_ssmR3FileOps;
1859 pStrm->pvUser = (void *)(uintptr_t)hFile;
1860 pStrm->fWrite = fWrite;
1861 return VINF_SUCCESS;
1862 }
1863 }
1864
1865 ssmR3StrmDelete(pStrm);
1866 pStrm->rc = rc;
1867 return rc;
1868}
1869
1870
1871/**
1872 * Raise an error condition on the stream.
1873 *
1874 * @returns true if we raised the error condition, false if the stream already
1875 * had an error condition set.
1876 *
1877 * @param pStrm The stream handle.
1878 * @param rc The VBox error status code.
1879 *
1880 * @thread Any.
1881 */
1882DECLINLINE(bool) ssmR3StrmSetError(PSSMSTRM pStrm, int rc)
1883{
1884 Assert(RT_FAILURE_NP(rc));
1885 return ASMAtomicCmpXchgS32(&pStrm->rc, rc, VINF_SUCCESS);
1886}
1887
1888
1889/**
1890 * Puts a buffer into the free list.
1891 *
1892 * @param pStrm The stream handle.
1893 * @param pBuf The buffer.
1894 *
1895 * @thread The consumer.
1896 */
1897static void ssmR3StrmPutFreeBuf(PSSMSTRM pStrm, PSSMSTRMBUF pBuf)
1898{
1899 for (;;)
1900 {
1901 PSSMSTRMBUF pCurFreeHead = ASMAtomicUoReadPtrT(&pStrm->pFree, PSSMSTRMBUF);
1902 ASMAtomicUoWritePtr(&pBuf->pNext, pCurFreeHead);
1903 if (ASMAtomicCmpXchgPtr(&pStrm->pFree, pBuf, pCurFreeHead))
1904 {
1905 int rc = RTSemEventSignal(pStrm->hEvtFree);
1906 AssertRC(rc);
1907 return;
1908 }
1909 }
1910}
1911
1912
1913/**
1914 * Gets a free buffer, waits for one if necessary.
1915 *
1916 * @returns Pointer to the buffer on success. NULL if we're terminating.
1917 * @param pStrm The stream handle.
1918 *
1919 * @thread The producer.
1920 */
1921static PSSMSTRMBUF ssmR3StrmGetFreeBuf(PSSMSTRM pStrm)
1922{
1923 for (;;)
1924 {
1925 PSSMSTRMBUF pMine = ASMAtomicUoReadPtrT(&pStrm->pFree, PSSMSTRMBUF);
1926 if (!pMine)
1927 {
1928 if (pStrm->fTerminating)
1929 return NULL;
1930 if (RT_FAILURE(pStrm->rc))
1931 return NULL;
1932 if ( pStrm->fWrite
1933 && pStrm->hIoThread == NIL_RTTHREAD)
1934 {
1935 int rc = ssmR3StrmWriteBuffers(pStrm);
1936 if (RT_FAILURE(rc))
1937 return NULL;
1938 }
1939 int rc = RTSemEventWaitNoResume(pStrm->hEvtFree, 30000);
1940 if ( rc == VERR_SEM_DESTROYED
1941 || pStrm->fTerminating)
1942 return NULL;
1943 continue;
1944 }
1945
1946 if (ASMAtomicCmpXchgPtr(&pStrm->pFree, pMine->pNext, pMine))
1947 {
1948 pMine->offStream = UINT64_MAX;
1949 pMine->cb = 0;
1950 pMine->pNext = NULL;
1951 pMine->fEndOfStream = false;
1952 pMine->NanoTS = RTTimeNanoTS();
1953 return pMine;
1954 }
1955 }
1956}
1957
1958
1959/**
1960 * Puts a buffer onto the queue.
1961 *
1962 * @param pBuf The buffer.
1963 *
1964 * @thread The producer.
1965 */
1966static void ssmR3StrmPutBuf(PSSMSTRM pStrm, PSSMSTRMBUF pBuf)
1967{
1968 for (;;)
1969 {
1970 PSSMSTRMBUF pCurHead = ASMAtomicUoReadPtrT(&pStrm->pHead, PSSMSTRMBUF);
1971 ASMAtomicUoWritePtr(&pBuf->pNext, pCurHead);
1972 if (ASMAtomicCmpXchgPtr(&pStrm->pHead, pBuf, pCurHead))
1973 {
1974 int rc = RTSemEventSignal(pStrm->hEvtHead);
1975 AssertRC(rc);
1976 return;
1977 }
1978 }
1979}
1980
1981
1982/**
1983 * Reverses the list.
1984 *
1985 * @returns The head of the reversed list.
1986 * @param pHead The head of the list to reverse.
1987 */
1988static PSSMSTRMBUF ssmR3StrmReverseList(PSSMSTRMBUF pHead)
1989{
1990 PSSMSTRMBUF pRevHead = NULL;
1991 while (pHead)
1992 {
1993 PSSMSTRMBUF pCur = pHead;
1994 pHead = pCur->pNext;
1995 pCur->pNext = pRevHead;
1996 pRevHead = pCur;
1997 }
1998 return pRevHead;
1999}
2000
2001
2002/**
2003 * Gets one buffer from the queue, will wait for one to become ready if
2004 * necessary.
2005 *
2006 * @returns Pointer to the buffer on success. NULL if we're terminating.
2007 * @param pBuf The buffer.
2008 *
2009 * @thread The consumer.
2010 */
2011static PSSMSTRMBUF ssmR3StrmGetBuf(PSSMSTRM pStrm)
2012{
2013 for (;;)
2014 {
2015 PSSMSTRMBUF pMine = pStrm->pPending;
2016 if (pMine)
2017 {
2018 pStrm->pPending = pMine->pNext;
2019 pMine->pNext = NULL;
2020 return pMine;
2021 }
2022
2023 pMine = ASMAtomicXchgPtrT(&pStrm->pHead, NULL, PSSMSTRMBUF);
2024 if (pMine)
2025 pStrm->pPending = ssmR3StrmReverseList(pMine);
2026 else
2027 {
2028 if (pStrm->fTerminating)
2029 return NULL;
2030 if (RT_FAILURE(pStrm->rc))
2031 return NULL;
2032 if ( !pStrm->fWrite
2033 && pStrm->hIoThread == NIL_RTTHREAD)
2034 {
2035 int rc = ssmR3StrmReadMore(pStrm);
2036 if (RT_FAILURE(rc))
2037 return NULL;
2038 continue;
2039 }
2040
2041 int rc = RTSemEventWaitNoResume(pStrm->hEvtHead, 30000);
2042 if ( rc == VERR_SEM_DESTROYED
2043 || pStrm->fTerminating)
2044 return NULL;
2045 }
2046 }
2047}
2048
2049
2050/**
2051 * Flushes the current buffer (both write and read streams).
2052 *
2053 * @param pStrm The stream handle.
2054 */
2055static void ssmR3StrmFlushCurBuf(PSSMSTRM pStrm)
2056{
2057 if (pStrm->pCur)
2058 {
2059 PSSMSTRMBUF pBuf = pStrm->pCur;
2060 pStrm->pCur = NULL;
2061
2062 if (pStrm->fWrite)
2063 {
2064 uint32_t cb = pStrm->off;
2065 pBuf->cb = cb;
2066 pBuf->offStream = pStrm->offCurStream;
2067 if ( pStrm->fChecksummed
2068 && pStrm->offStreamCRC < cb)
2069 pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC,
2070 &pBuf->abData[pStrm->offStreamCRC],
2071 cb - pStrm->offStreamCRC);
2072 pStrm->offCurStream += cb;
2073 pStrm->off = 0;
2074 pStrm->offStreamCRC = 0;
2075
2076 ssmR3StrmPutBuf(pStrm, pBuf);
2077 }
2078 else
2079 {
2080 uint32_t cb = pBuf->cb;
2081 if ( pStrm->fChecksummed
2082 && pStrm->offStreamCRC < cb)
2083 pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC,
2084 &pBuf->abData[pStrm->offStreamCRC],
2085 cb - pStrm->offStreamCRC);
2086 pStrm->offCurStream += cb;
2087 pStrm->off = 0;
2088 pStrm->offStreamCRC = 0;
2089
2090 ssmR3StrmPutFreeBuf(pStrm, pBuf);
2091 }
2092 }
2093}
2094
2095
2096/**
2097 * Flush buffered data.
2098 *
2099 * @returns VBox status code. Returns VINF_EOF if we encounter a buffer with the
2100 * fEndOfStream indicator set.
2101 * @param pStrm The stream handle.
2102 *
2103 * @thread The producer thread.
2104 */
2105static int ssmR3StrmWriteBuffers(PSSMSTRM pStrm)
2106{
2107 Assert(pStrm->fWrite);
2108
2109 /*
2110 * Just return if the stream has a pending error condition.
2111 */
2112 int rc = pStrm->rc;
2113 if (RT_FAILURE(rc))
2114 return rc;
2115
2116 /*
2117 * Grab the pending list and write it out.
2118 */
2119 PSSMSTRMBUF pHead = ASMAtomicXchgPtrT(&pStrm->pHead, NULL, PSSMSTRMBUF);
2120 if (!pHead)
2121 return VINF_SUCCESS;
2122 pHead = ssmR3StrmReverseList(pHead);
2123
2124 while (pHead)
2125 {
2126 /* pop */
2127 PSSMSTRMBUF pCur = pHead;
2128 pHead = pCur->pNext;
2129
2130 /* flush */
2131 rc = pStrm->pOps->pfnIsOk(pStrm->pvUser);
2132 if (RT_SUCCESS(rc))
2133 rc = pStrm->pOps->pfnWrite(pStrm->pvUser, pCur->offStream, &pCur->abData[0], pCur->cb);
2134 if ( RT_FAILURE(rc)
2135 && ssmR3StrmSetError(pStrm, rc))
2136 LogRel(("ssmR3StrmWriteBuffers: Write failed with rc=%Rrc at offStream=%#llx\n", rc, pCur->offStream));
2137
2138 /* free */
2139 bool fEndOfStream = pCur->fEndOfStream;
2140 ssmR3StrmPutFreeBuf(pStrm, pCur);
2141 if (fEndOfStream)
2142 {
2143 Assert(!pHead);
2144 return VINF_EOF;
2145 }
2146 }
2147
2148 return pStrm->rc;
2149}
2150
2151
2152/**
2153 * Closes the stream after first flushing any pending write.
2154 *
2155 * @returns VBox status code.
2156 * @param pStrm The stream handle.
2157 * @param fCancelled Indicates whether the operation was cancelled or
2158 * not.
2159 */
2160static int ssmR3StrmClose(PSSMSTRM pStrm, bool fCancelled)
2161{
2162 /*
2163 * Flush, terminate the I/O thread, and close the stream.
2164 */
2165 if (pStrm->fWrite)
2166 {
2167 ssmR3StrmFlushCurBuf(pStrm);
2168 if (pStrm->hIoThread == NIL_RTTHREAD)
2169 ssmR3StrmWriteBuffers(pStrm);
2170 }
2171
2172 if (pStrm->hIoThread != NIL_RTTHREAD)
2173 ASMAtomicWriteBool(&pStrm->fTerminating, true);
2174
2175 int rc;
2176 if (pStrm->fWrite)
2177 {
2178 if (pStrm->hIoThread != NIL_RTTHREAD)
2179 {
2180 int rc2 = RTSemEventSignal(pStrm->hEvtHead);
2181 AssertLogRelRC(rc2);
2182 int rc3 = RTThreadWait(pStrm->hIoThread, RT_INDEFINITE_WAIT, NULL);
2183 AssertLogRelRC(rc3);
2184 pStrm->hIoThread = NIL_RTTHREAD;
2185 }
2186
2187 rc = pStrm->pOps->pfnClose(pStrm->pvUser, fCancelled);
2188 if (RT_FAILURE(rc))
2189 ssmR3StrmSetError(pStrm, rc);
2190 }
2191 else
2192 {
2193 rc = pStrm->pOps->pfnClose(pStrm->pvUser, fCancelled);
2194 if (RT_FAILURE(rc))
2195 ssmR3StrmSetError(pStrm, rc);
2196
2197 if (pStrm->hIoThread != NIL_RTTHREAD)
2198 {
2199 int rc2 = RTSemEventSignal(pStrm->hEvtFree);
2200 AssertLogRelRC(rc2);
2201 int rc3 = RTThreadWait(pStrm->hIoThread, RT_INDEFINITE_WAIT, NULL);
2202 AssertLogRelRC(rc3);
2203 pStrm->hIoThread = NIL_RTTHREAD;
2204 }
2205 }
2206
2207 pStrm->pOps = NULL;
2208 pStrm->pvUser = NULL;
2209
2210 rc = pStrm->rc;
2211 ssmR3StrmDelete(pStrm);
2212
2213 return rc;
2214}
2215
2216
2217/**
2218 * Stream output routine.
2219 *
2220 * @returns VBox status code.
2221 * @param pStrm The stream handle.
2222 * @param pvBuf What to write.
2223 * @param cbToWrite How much to write.
2224 *
2225 * @thread The producer in a write stream (never the I/O thread).
2226 */
2227static int ssmR3StrmWrite(PSSMSTRM pStrm, const void *pvBuf, size_t cbToWrite)
2228{
2229 AssertReturn(cbToWrite > 0, VINF_SUCCESS);
2230 Assert(pStrm->fWrite);
2231
2232 /*
2233 * Squeeze as much as possible into the current buffer.
2234 */
2235 PSSMSTRMBUF pBuf = pStrm->pCur;
2236 if (RT_LIKELY(pBuf))
2237 {
2238 uint32_t cbLeft = RT_SIZEOFMEMB(SSMSTRMBUF, abData) - pStrm->off;
2239 if (RT_LIKELY(cbLeft >= cbToWrite))
2240 {
2241 memcpy(&pBuf->abData[pStrm->off], pvBuf, cbToWrite);
2242 pStrm->off += (uint32_t)cbToWrite;
2243 return VINF_SUCCESS;
2244 }
2245
2246 if (cbLeft > 0)
2247 {
2248 memcpy(&pBuf->abData[pStrm->off], pvBuf, cbLeft);
2249 pStrm->off += cbLeft;
2250 cbToWrite -= cbLeft;
2251 pvBuf = (uint8_t const *)pvBuf + cbLeft;
2252 }
2253 Assert(pStrm->off == RT_SIZEOFMEMB(SSMSTRMBUF, abData));
2254 }
2255
2256 /*
2257 * Need one or more new buffers.
2258 */
2259 do
2260 {
2261 /*
2262 * Flush the current buffer and replace it with a new one.
2263 */
2264 ssmR3StrmFlushCurBuf(pStrm);
2265 pBuf = ssmR3StrmGetFreeBuf(pStrm);
2266 if (!pBuf)
2267 break;
2268 pStrm->pCur = pBuf;
2269 Assert(pStrm->off == 0);
2270
2271 /*
2272 * Copy data to the buffer.
2273 */
2274 uint32_t cbCopy = RT_SIZEOFMEMB(SSMSTRMBUF, abData);
2275 if (cbCopy > cbToWrite)
2276 cbCopy = (uint32_t)cbToWrite;
2277 memcpy(&pBuf->abData[0], pvBuf, cbCopy);
2278 pStrm->off = cbCopy;
2279 cbToWrite -= cbCopy;
2280 pvBuf = (uint8_t const *)pvBuf + cbCopy;
2281 } while (cbToWrite > 0);
2282
2283 return pStrm->rc;
2284}
2285
2286
2287/**
2288 * Reserves space in the current buffer so the caller can write directly to the
2289 * buffer instead of doing double buffering.
2290 *
2291 * @returns VBox status code
2292 * @param pStrm The stream handle.
2293 * @param cb The amount of buffer space to reserve.
2294 * @param ppb Where to return the pointer.
2295 */
2296static int ssmR3StrmReserveWriteBufferSpace(PSSMSTRM pStrm, size_t cb, uint8_t **ppb)
2297{
2298 Assert(pStrm->fWrite);
2299 Assert(RT_SIZEOFMEMB(SSMSTRMBUF, abData) / 4 >= cb);
2300
2301 /*
2302 * Check if there is room in the current buffer, it not flush it.
2303 */
2304 PSSMSTRMBUF pBuf = pStrm->pCur;
2305 if (pBuf)
2306 {
2307 uint32_t cbLeft = RT_SIZEOFMEMB(SSMSTRMBUF, abData) - pStrm->off;
2308 if (cbLeft >= cb)
2309 {
2310 *ppb = &pBuf->abData[pStrm->off];
2311 return VINF_SUCCESS;
2312 }
2313
2314 ssmR3StrmFlushCurBuf(pStrm);
2315 }
2316
2317 /*
2318 * Get a fresh buffer and return a pointer into it.
2319 */
2320 pBuf = ssmR3StrmGetFreeBuf(pStrm);
2321 if (pBuf)
2322 {
2323 pStrm->pCur = pBuf;
2324 Assert(pStrm->off == 0);
2325 *ppb = &pBuf->abData[0];
2326 }
2327 else
2328 *ppb = NULL; /* make gcc happy. */
2329 return pStrm->rc;
2330}
2331
2332
2333/**
2334 * Commits buffer space reserved by ssmR3StrmReserveWriteBufferSpace.
2335 *
2336 * @returns VBox status code.
2337 * @param pStrm The stream handle.
2338 * @param cb The amount of buffer space to commit. This can be less
2339 * that what was reserved initially.
2340 */
2341static int ssmR3StrmCommitWriteBufferSpace(PSSMSTRM pStrm, size_t cb)
2342{
2343 Assert(pStrm->pCur);
2344 Assert(pStrm->off + cb <= RT_SIZEOFMEMB(SSMSTRMBUF, abData));
2345 pStrm->off += (uint32_t)cb;
2346 return VINF_SUCCESS;
2347}
2348
2349
2350/**
2351 * Marks the end of the stream.
2352 *
2353 * This will cause the I/O thread to quit waiting for more buffers.
2354 *
2355 * @returns VBox status code.
2356 * @param pStrm The stream handle.
2357 */
2358static int ssmR3StrmSetEnd(PSSMSTRM pStrm)
2359{
2360 Assert(pStrm->fWrite);
2361 PSSMSTRMBUF pBuf = pStrm->pCur;
2362 if (RT_UNLIKELY(!pStrm->pCur))
2363 {
2364 pBuf = ssmR3StrmGetFreeBuf(pStrm);
2365 if (!pBuf)
2366 return pStrm->rc;
2367 pStrm->pCur = pBuf;
2368 Assert(pStrm->off == 0);
2369 }
2370 pBuf->fEndOfStream = true;
2371 ssmR3StrmFlushCurBuf(pStrm);
2372 return VINF_SUCCESS;
2373}
2374
2375
2376/**
2377 * Read more from the stream.
2378 *
2379 * @returns VBox status code. VERR_EOF gets translated into VINF_EOF.
2380 * @param pStrm The stream handle.
2381 *
2382 * @thread The I/O thread when we got one, otherwise the stream user.
2383 */
2384static int ssmR3StrmReadMore(PSSMSTRM pStrm)
2385{
2386 int rc;
2387 Log6(("ssmR3StrmReadMore:\n"));
2388
2389 /*
2390 * Undo seek done by ssmR3StrmPeekAt.
2391 */
2392 if (pStrm->fNeedSeek)
2393 {
2394 rc = pStrm->pOps->pfnSeek(pStrm->pvUser, pStrm->offNeedSeekTo, RTFILE_SEEK_BEGIN, NULL);
2395 if (RT_FAILURE(rc))
2396 {
2397 if (ssmR3StrmSetError(pStrm, rc))
2398 LogRel(("ssmR3StrmReadMore: RTFileSeek(,%#llx,) failed with rc=%Rrc\n", pStrm->offNeedSeekTo, rc));
2399 return rc;
2400 }
2401 pStrm->fNeedSeek = false;
2402 pStrm->offNeedSeekTo = UINT64_MAX;
2403 }
2404
2405 /*
2406 * Get a free buffer and try fill it up.
2407 */
2408 PSSMSTRMBUF pBuf = ssmR3StrmGetFreeBuf(pStrm);
2409 if (!pBuf)
2410 return pStrm->rc;
2411
2412 pBuf->offStream = pStrm->pOps->pfnTell(pStrm->pvUser);
2413 size_t cbRead = sizeof(pBuf->abData);
2414 rc = pStrm->pOps->pfnRead(pStrm->pvUser, pBuf->offStream, &pBuf->abData[0], cbRead, &cbRead);
2415 if ( RT_SUCCESS(rc)
2416 && cbRead > 0)
2417 {
2418 pBuf->cb = (uint32_t)cbRead;
2419 pBuf->fEndOfStream = false;
2420 Log6(("ssmR3StrmReadMore: %#010llx %#x\n", pBuf->offStream, pBuf->cb));
2421 ssmR3StrmPutBuf(pStrm, pBuf);
2422 }
2423 else if ( ( RT_SUCCESS_NP(rc)
2424 && cbRead == 0)
2425 || rc == VERR_EOF)
2426 {
2427 pBuf->cb = 0;
2428 pBuf->fEndOfStream = true;
2429 Log6(("ssmR3StrmReadMore: %#010llx 0 EOF!\n", pBuf->offStream));
2430 ssmR3StrmPutBuf(pStrm, pBuf);
2431 rc = VINF_EOF;
2432 }
2433 else
2434 {
2435 Log6(("ssmR3StrmReadMore: %#010llx rc=%Rrc!\n", pBuf->offStream, rc));
2436 if (ssmR3StrmSetError(pStrm, rc))
2437 LogRel(("ssmR3StrmReadMore: RTFileRead(,,%#x,) -> %Rrc at offset %#llx\n",
2438 sizeof(pBuf->abData), rc, pBuf->offStream));
2439 ssmR3StrmPutFreeBuf(pStrm, pBuf);
2440 }
2441 return rc;
2442}
2443
2444
2445/**
2446 * Stream input routine.
2447 *
2448 * @returns VBox status code.
2449 * @param pStrm The stream handle.
2450 * @param pvBuf Where to put what we read.
2451 * @param cbToRead How much to read.
2452 */
2453static int ssmR3StrmRead(PSSMSTRM pStrm, void *pvBuf, size_t cbToRead)
2454{
2455 AssertReturn(cbToRead > 0, VINF_SUCCESS);
2456 Assert(!pStrm->fWrite);
2457
2458 /*
2459 * Read from the current buffer if we got one.
2460 */
2461 PSSMSTRMBUF pBuf = pStrm->pCur;
2462 if (RT_LIKELY(pBuf))
2463 {
2464 Assert(pStrm->off <= pBuf->cb);
2465 uint32_t cbLeft = pBuf->cb - pStrm->off;
2466 if (cbLeft >= cbToRead)
2467 {
2468 memcpy(pvBuf, &pBuf->abData[pStrm->off], cbToRead);
2469 pStrm->off += (uint32_t)cbToRead;
2470 Assert(pStrm->off <= pBuf->cb);
2471 return VINF_SUCCESS;
2472 }
2473 if (cbLeft)
2474 {
2475 memcpy(pvBuf, &pBuf->abData[pStrm->off], cbLeft);
2476 pStrm->off += cbLeft;
2477 cbToRead -= cbLeft;
2478 pvBuf = (uint8_t *)pvBuf + cbLeft;
2479 }
2480 else if (pBuf->fEndOfStream)
2481 return VERR_EOF;
2482 Assert(pStrm->off == pBuf->cb);
2483 }
2484
2485 /*
2486 * Get more buffers from the stream.
2487 */
2488 int rc = VINF_SUCCESS;
2489 do
2490 {
2491 /*
2492 * Check for EOF first - never flush the EOF buffer.
2493 */
2494 if ( pBuf
2495 && pBuf->fEndOfStream)
2496 return VERR_EOF;
2497
2498 /*
2499 * Flush the current buffer and get the next one.
2500 */
2501 ssmR3StrmFlushCurBuf(pStrm);
2502 pBuf = ssmR3StrmGetBuf(pStrm);
2503 if (!pBuf)
2504 {
2505 rc = pStrm->rc;
2506 break;
2507 }
2508 pStrm->pCur = pBuf;
2509 Assert(pStrm->off == 0);
2510 Assert(pStrm->offCurStream == pBuf->offStream);
2511 if (!pBuf->cb)
2512 {
2513 Assert(pBuf->fEndOfStream);
2514 return VERR_EOF;
2515 }
2516
2517 /*
2518 * Read data from the buffer.
2519 */
2520 uint32_t cbCopy = pBuf->cb;
2521 if (cbCopy > cbToRead)
2522 cbCopy = (uint32_t)cbToRead;
2523 memcpy(pvBuf, &pBuf->abData[0], cbCopy);
2524 pStrm->off = cbCopy;
2525 cbToRead -= cbCopy;
2526 pvBuf = (uint8_t *)pvBuf + cbCopy;
2527 Assert(!pStrm->pCur || pStrm->off <= pStrm->pCur->cb);
2528 } while (cbToRead > 0);
2529
2530 return rc;
2531}
2532
2533
2534/**
2535 * Reads data from the stream but instead of copying it to some output buffer
2536 * the caller gets a pointer to into the current stream buffer.
2537 *
2538 * The returned pointer becomes invalid after the next stream operation!
2539 *
2540 * @returns Pointer to the read data residing in the stream buffer. NULL is
2541 * returned if the request amount of data isn't available in the
2542 * buffer. The caller must fall back on ssmR3StrmRead when this
2543 * happens.
2544 *
2545 * @param pStrm The stream handle.
2546 * @param cbToRead The number of bytes to tread.
2547 */
2548static uint8_t const *ssmR3StrmReadDirect(PSSMSTRM pStrm, size_t cbToRead)
2549{
2550 AssertReturn(cbToRead > 0, VINF_SUCCESS);
2551 Assert(!pStrm->fWrite);
2552
2553 /*
2554 * Too lazy to fetch more data for the odd case that we're
2555 * exactly at the boundrary between two buffers.
2556 */
2557 PSSMSTRMBUF pBuf = pStrm->pCur;
2558 if (RT_LIKELY(pBuf))
2559 {
2560 Assert(pStrm->off <= pBuf->cb);
2561 uint32_t cbLeft = pBuf->cb - pStrm->off;
2562 if (cbLeft >= cbToRead)
2563 {
2564 uint8_t const *pb = &pBuf->abData[pStrm->off];
2565 pStrm->off += (uint32_t)cbToRead;
2566 Assert(pStrm->off <= pBuf->cb);
2567 return pb;
2568 }
2569 }
2570 return NULL;
2571}
2572
2573
2574/**
2575 * Check that the stream is OK and flush data that is getting old
2576 *
2577 * The checking is mainly for testing for cancellation and out of space
2578 * conditions.
2579 *
2580 * @returns VBox status code.
2581 * @param pStrm The stream handle.
2582 */
2583static int ssmR3StrmCheckAndFlush(PSSMSTRM pStrm)
2584{
2585 int rc = pStrm->pOps->pfnIsOk(pStrm->pvUser);
2586 if (RT_FAILURE(rc))
2587 return rc;
2588
2589 if ( pStrm->fWrite
2590 && pStrm->hIoThread != NIL_RTTHREAD
2591 && !pStrm->pHead /* the worker is probably idle */
2592 && pStrm->pCur
2593 && RTTimeNanoTS() - pStrm->pCur->NanoTS > 500*1000*1000 /* 0.5s */
2594 )
2595 ssmR3StrmFlushCurBuf(pStrm);
2596 return VINF_SUCCESS;
2597}
2598
2599
2600/**
2601 * Tell current stream position.
2602 *
2603 * @returns stream position.
2604 * @param pStrm The stream handle.
2605 */
2606static uint64_t ssmR3StrmTell(PSSMSTRM pStrm)
2607{
2608 return pStrm->offCurStream + pStrm->off;
2609}
2610
2611
2612/**
2613 * Gets the intermediate stream CRC up to the current position.
2614 *
2615 * @returns CRC.
2616 * @param pStrm The stream handle.
2617 */
2618static uint32_t ssmR3StrmCurCRC(PSSMSTRM pStrm)
2619{
2620 if (!pStrm->fChecksummed)
2621 return 0;
2622 if (pStrm->offStreamCRC < pStrm->off)
2623 {
2624 PSSMSTRMBUF pBuf = pStrm->pCur; Assert(pBuf);
2625 pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC, &pBuf->abData[pStrm->offStreamCRC], pStrm->off - pStrm->offStreamCRC);
2626 pStrm->offStreamCRC = pStrm->off;
2627 }
2628 else
2629 Assert(pStrm->offStreamCRC == pStrm->off);
2630 return pStrm->u32StreamCRC;
2631}
2632
2633
2634/**
2635 * Gets the final stream CRC up to the current position.
2636 *
2637 * @returns CRC.
2638 * @param pStrm The stream handle.
2639 */
2640static uint32_t ssmR3StrmFinalCRC(PSSMSTRM pStrm)
2641{
2642 if (!pStrm->fChecksummed)
2643 return 0;
2644 return RTCrc32Finish(ssmR3StrmCurCRC(pStrm));
2645}
2646
2647
2648/**
2649 * Disables checksumming of the stream.
2650 *
2651 * @param pStrm The stream handle.
2652 */
2653static void ssmR3StrmDisableChecksumming(PSSMSTRM pStrm)
2654{
2655 pStrm->fChecksummed = false;
2656}
2657
2658
2659/**
2660 * Used by SSMR3Seek to position the stream at the new unit.
2661 *
2662 * @returns VBox stutus code.
2663 * @param pStrm The strem handle.
2664 * @param off The seek offset.
2665 * @param uMethod The seek method.
2666 * @param u32CurCRC The current CRC at the seek position.
2667 */
2668static int ssmR3StrmSeek(PSSMSTRM pStrm, int64_t off, uint32_t uMethod, uint32_t u32CurCRC)
2669{
2670 AssertReturn(!pStrm->fWrite, VERR_NOT_SUPPORTED);
2671 AssertReturn(pStrm->hIoThread == NIL_RTTHREAD, VERR_WRONG_ORDER);
2672
2673 uint64_t offStream;
2674 int rc = pStrm->pOps->pfnSeek(pStrm->pvUser, off, uMethod, &offStream);
2675 if (RT_SUCCESS(rc))
2676 {
2677 pStrm->fNeedSeek = false;
2678 pStrm->offNeedSeekTo= UINT64_MAX;
2679 pStrm->offCurStream = offStream;
2680 pStrm->off = 0;
2681 pStrm->offStreamCRC = 0;
2682 if (pStrm->fChecksummed)
2683 pStrm->u32StreamCRC = u32CurCRC;
2684 if (pStrm->pCur)
2685 {
2686 ssmR3StrmPutFreeBuf(pStrm, pStrm->pCur);
2687 pStrm->pCur = NULL;
2688 }
2689 }
2690 return rc;
2691}
2692
2693
2694/**
2695 * Skip some bytes in the stream.
2696 *
2697 * This is only used if someone didn't read all of their data in the V1 format,
2698 * so don't bother making this very efficient yet.
2699 *
2700 * @returns VBox status code.
2701 * @param pStrm The stream handle.
2702 * @param offDst The destination offset.
2703 */
2704static int ssmR3StrmSkipTo(PSSMSTRM pStrm, uint64_t offDst)
2705{
2706 /* dead simple - lazy bird! */
2707 for (;;)
2708 {
2709 uint64_t offCur = ssmR3StrmTell(pStrm);
2710 AssertReturn(offCur <= offDst, VERR_INTERNAL_ERROR_4);
2711 if (offCur == offDst)
2712 return VINF_SUCCESS;
2713
2714 uint8_t abBuf[4096];
2715 size_t cbToRead = RT_MIN(sizeof(abBuf), offDst - offCur);
2716 int rc = ssmR3StrmRead(pStrm, abBuf, cbToRead);
2717 if (RT_FAILURE(rc))
2718 return rc;
2719 }
2720}
2721
2722
2723/**
2724 * Get the size of the file.
2725 *
2726 * This does not work for non-file streams!
2727 *
2728 * @returns The file size, or UINT64_MAX if not a file stream.
2729 * @param pStrm The stream handle.
2730 */
2731static uint64_t ssmR3StrmGetSize(PSSMSTRM pStrm)
2732{
2733 uint64_t cbFile;
2734 int rc = pStrm->pOps->pfnSize(pStrm->pvUser, &cbFile);
2735 AssertLogRelRCReturn(rc, UINT64_MAX);
2736 return cbFile;
2737}
2738
2739
2740/***
2741 * Tests if the stream is a file stream or not.
2742 *
2743 * @returns true / false.
2744 * @param pStrm The stream handle.
2745 */
2746static bool ssmR3StrmIsFile(PSSMSTRM pStrm)
2747{
2748 return pStrm->pOps == &g_ssmR3FileOps;
2749}
2750
2751
2752/**
2753 * Peeks at data in a file stream without buffering anything (or upsetting
2754 * the buffering for that matter).
2755 *
2756 * @returns VBox status code.
2757 * @param pStrm The stream handle
2758 * @param off The offset to start peeking at. Use a negative offset to
2759 * peek at something relative to the end of the file.
2760 * @param pvBuf Output buffer.
2761 * @param cbToRead How much to read.
2762 * @param poff Where to optionally store the position. Useful when
2763 * using a negative off.
2764 *
2765 * @remarks Failures occuring while peeking will not be raised on the stream.
2766 */
2767static int ssmR3StrmPeekAt(PSSMSTRM pStrm, RTFOFF off, void *pvBuf, size_t cbToRead, uint64_t *poff)
2768{
2769 AssertReturn(!pStrm->fWrite, VERR_NOT_SUPPORTED);
2770 AssertReturn(pStrm->hIoThread == NIL_RTTHREAD, VERR_WRONG_ORDER);
2771
2772 if (!pStrm->fNeedSeek)
2773 {
2774 pStrm->fNeedSeek = true;
2775 pStrm->offNeedSeekTo = pStrm->offCurStream + (pStrm->pCur ? pStrm->pCur->cb : 0);
2776 }
2777 uint64_t offActual;
2778 int rc = pStrm->pOps->pfnSeek(pStrm->pvUser, off, off >= 0 ? RTFILE_SEEK_BEGIN : RTFILE_SEEK_END, &offActual);
2779 if (RT_SUCCESS(rc))
2780 {
2781 if (poff)
2782 *poff = offActual;
2783 rc = pStrm->pOps->pfnRead(pStrm->pvUser, offActual, pvBuf, cbToRead, NULL);
2784 }
2785
2786 return rc;
2787}
2788
2789
2790/**
2791 * The I/O thread.
2792 *
2793 * @returns VINF_SUCCESS (ignored).
2794 * @param hSelf The thread handle.
2795 * @param pvStrm The stream handle.
2796 */
2797static DECLCALLBACK(int) ssmR3StrmIoThread(RTTHREAD hSelf, void *pvStrm)
2798{
2799 PSSMSTRM pStrm = (PSSMSTRM)pvStrm;
2800 ASMAtomicWriteHandle(&pStrm->hIoThread, hSelf); /* paranoia */
2801
2802 Log(("ssmR3StrmIoThread: starts working\n"));
2803 if (pStrm->fWrite)
2804 {
2805 /*
2806 * Write until error or terminated.
2807 */
2808 for (;;)
2809 {
2810 int rc = ssmR3StrmWriteBuffers(pStrm);
2811 if ( RT_FAILURE(rc)
2812 || rc == VINF_EOF)
2813 {
2814 Log(("ssmR3StrmIoThread: quitting writing with rc=%Rrc.\n", rc));
2815 break;
2816 }
2817 if (RT_FAILURE(pStrm->rc))
2818 {
2819 Log(("ssmR3StrmIoThread: quitting writing with stream rc=%Rrc\n", pStrm->rc));
2820 break;
2821 }
2822
2823 if (ASMAtomicReadBool(&pStrm->fTerminating))
2824 {
2825 if (!ASMAtomicReadPtrT(&pStrm->pHead, PSSMSTRMBUF))
2826 {
2827 Log(("ssmR3StrmIoThread: quitting writing because of pending termination.\n"));
2828 break;
2829 }
2830 Log(("ssmR3StrmIoThread: postponing termination because of pending buffers.\n"));
2831 }
2832 else if (!ASMAtomicReadPtrT(&pStrm->pHead, PSSMSTRMBUF))
2833 {
2834 rc = RTSemEventWait(pStrm->hEvtHead, RT_INDEFINITE_WAIT);
2835 AssertLogRelRC(rc);
2836 }
2837 }
2838
2839 if (!ASMAtomicReadBool(&pStrm->fTerminating))
2840 RTSemEventSignal(pStrm->hEvtFree);
2841 }
2842 else
2843 {
2844 /*
2845 * Read until end of file, error or termination.
2846 */
2847 for (;;)
2848 {
2849 if (ASMAtomicReadBool(&pStrm->fTerminating))
2850 {
2851 Log(("ssmR3StrmIoThread: quitting reading because of pending termination.\n"));
2852 break;
2853 }
2854
2855 int rc = ssmR3StrmReadMore(pStrm);
2856 if ( RT_FAILURE(rc)
2857 || rc == VINF_EOF)
2858 {
2859 Log(("ssmR3StrmIoThread: quitting reading with rc=%Rrc\n", rc));
2860 break;
2861 }
2862 if (RT_FAILURE(pStrm->rc))
2863 {
2864 Log(("ssmR3StrmIoThread: quitting reading with stream rc=%Rrc\n", pStrm->rc));
2865 break;
2866 }
2867 }
2868
2869 if (!ASMAtomicReadBool(&pStrm->fTerminating))
2870 RTSemEventSignal(pStrm->hEvtHead);
2871 }
2872
2873 return VINF_SUCCESS;
2874}
2875
2876
2877/**
2878 * Starts the I/O thread for the specified stream.
2879 *
2880 * @param pStrm The stream handle.
2881 */
2882static void ssmR3StrmStartIoThread(PSSMSTRM pStrm)
2883{
2884 Assert(pStrm->hIoThread == NIL_RTTHREAD);
2885
2886 RTTHREAD hThread;
2887 int rc = RTThreadCreate(&hThread, ssmR3StrmIoThread, pStrm, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SSM-IO");
2888 AssertRCReturnVoid(rc);
2889 ASMAtomicWriteHandle(&pStrm->hIoThread, hThread); /* paranoia */
2890}
2891
2892
2893/**
2894 * Works the progress calculation.
2895 *
2896 * @param pSSM The SSM handle.
2897 * @param cbAdvance Number of bytes to advance
2898 */
2899static void ssmR3Progress(PSSMHANDLE pSSM, uint64_t cbAdvance)
2900{
2901 /* Can't advance it beyond the estimated end of the unit. */
2902 uint64_t cbLeft = pSSM->offEstUnitEnd - pSSM->offEst;
2903 if (cbAdvance > cbLeft)
2904 cbAdvance = cbLeft;
2905 pSSM->offEst += cbAdvance;
2906
2907 /* uPercentPrepare% prepare, xx% exec, uPercentDone% done+crc */
2908 while ( pSSM->offEst >= pSSM->offEstProgress
2909 && pSSM->uPercent <= 100-pSSM->uPercentDone)
2910 {
2911 if (pSSM->pfnProgress)
2912 pSSM->pfnProgress(pSSM->pVM, pSSM->uPercent, pSSM->pvUser);
2913 pSSM->uPercent++;
2914 pSSM->offEstProgress = (pSSM->uPercent - pSSM->uPercentPrepare) * pSSM->cbEstTotal
2915 / (100 - pSSM->uPercentDone - pSSM->uPercentPrepare);
2916 }
2917}
2918
2919
2920/**
2921 * Makes the SSM operation cancellable or not (via SSMR3Cancel).
2922 *
2923 * @param pVM The VM handle.
2924 * @param pSSM The saved state handle. (SSMHANDLE::rc may be set.)
2925 * @param fCancellable The new state.
2926 */
2927static void ssmR3SetCancellable(PVM pVM, PSSMHANDLE pSSM, bool fCancellable)
2928{
2929 RTCritSectEnter(&pVM->ssm.s.CancelCritSect);
2930 if (fCancellable)
2931 {
2932 Assert(!pVM->ssm.s.pSSM);
2933 pVM->ssm.s.pSSM = pSSM;
2934 }
2935 else
2936 {
2937 if (pVM->ssm.s.pSSM == pSSM)
2938 pVM->ssm.s.pSSM = NULL;
2939
2940 uint32_t fCancelled = ASMAtomicUoReadU32(&pSSM->fCancelled);
2941 if ( fCancelled == SSMHANDLE_CANCELLED
2942 && RT_SUCCESS(pSSM->rc))
2943 pSSM->rc = VERR_SSM_CANCELLED;
2944 }
2945
2946 RTCritSectLeave(&pVM->ssm.s.CancelCritSect);
2947}
2948
2949
2950/**
2951 * Gets the host bit count of the saved state.
2952 *
2953 * Works for on both save and load handles.
2954 *
2955 * @returns 32 or 64.
2956 * @param pSSM The saved state handle.
2957 */
2958DECLINLINE(uint32_t) ssmR3GetHostBits(PSSMHANDLE pSSM)
2959{
2960 if (pSSM->enmOp >= SSMSTATE_LOAD_PREP)
2961 {
2962 uint32_t cBits = pSSM->u.Read.cHostBits;
2963 if (cBits)
2964 return cBits;
2965 }
2966 return HC_ARCH_BITS;
2967}
2968
2969
2970/**
2971 * Saved state origins on a host using 32-bit MSC?
2972 *
2973 * Works for on both save and load handles.
2974 *
2975 * @returns true/false.
2976 * @param pSSM The saved state handle.
2977 */
2978DECLINLINE(bool) ssmR3IsHostMsc32(PSSMHANDLE pSSM)
2979{
2980 if (pSSM->enmOp >= SSMSTATE_LOAD_PREP)
2981 return pSSM->u.Read.fIsHostMsc32;
2982 return SSM_HOST_IS_MSC_32;
2983}
2984
2985#ifndef SSM_STANDALONE
2986
2987/**
2988 * Finishes a data unit.
2989 * All buffers and compressor instances are flushed and destroyed.
2990 *
2991 * @returns VBox status.
2992 * @param pSSM The saved state handle.
2993 */
2994static int ssmR3DataWriteFinish(PSSMHANDLE pSSM)
2995{
2996 //Log2(("ssmR3DataWriteFinish: %#010llx start\n", ssmR3StrmTell(&pSSM->Strm)));
2997 int rc = ssmR3DataFlushBuffer(pSSM);
2998 if (RT_SUCCESS(rc))
2999 {
3000 pSSM->offUnit = UINT64_MAX;
3001 return VINF_SUCCESS;
3002 }
3003
3004 if (RT_SUCCESS(pSSM->rc))
3005 pSSM->rc = rc;
3006 Log2(("ssmR3DataWriteFinish: failure rc=%Rrc\n", rc));
3007 return rc;
3008}
3009
3010
3011/**
3012 * Begins writing the data of a data unit.
3013 *
3014 * Errors are signalled via pSSM->rc.
3015 *
3016 * @param pSSM The saved state handle.
3017 */
3018static void ssmR3DataWriteBegin(PSSMHANDLE pSSM)
3019{
3020 pSSM->offUnit = 0;
3021}
3022
3023
3024/**
3025 * Writes a record to the current data item in the saved state file.
3026 *
3027 * @returns VBox status code. Sets pSSM->rc on failure.
3028 * @param pSSM The saved state handle.
3029 * @param pvBuf The bits to write.
3030 * @param cbBuf The number of bytes to write.
3031 */
3032static int ssmR3DataWriteRaw(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf)
3033{
3034 Log2(("ssmR3DataWriteRaw: %08llx|%08llx: pvBuf=%p cbBuf=%#x %.*Rhxs%s\n",
3035 ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pvBuf, cbBuf, RT_MIN(cbBuf, SSM_LOG_BYTES), pvBuf, cbBuf > SSM_LOG_BYTES ? "..." : ""));
3036
3037 /*
3038 * Check that everything is fine.
3039 */
3040 if (RT_FAILURE(pSSM->rc))
3041 return pSSM->rc;
3042
3043 /*
3044 * Write the data item in 1MB chunks for progress indicator reasons.
3045 */
3046 while (cbBuf > 0)
3047 {
3048 size_t cbChunk = RT_MIN(cbBuf, _1M);
3049 int rc = ssmR3StrmWrite(&pSSM->Strm, pvBuf, cbChunk);
3050 if (RT_FAILURE(rc))
3051 return rc;
3052 pSSM->offUnit += cbChunk;
3053 cbBuf -= cbChunk;
3054 pvBuf = (char *)pvBuf + cbChunk;
3055 }
3056
3057 return VINF_SUCCESS;
3058}
3059
3060
3061/**
3062 * Writes a record header for the specified amount of data.
3063 *
3064 * @returns VBox status code. Sets pSSM->rc on failure.
3065 * @param pSSM The saved state handle
3066 * @param cb The amount of data.
3067 * @param u8TypeAndFlags The record type and flags.
3068 */
3069static int ssmR3DataWriteRecHdr(PSSMHANDLE pSSM, size_t cb, uint8_t u8TypeAndFlags)
3070{
3071 size_t cbHdr;
3072 uint8_t abHdr[8];
3073 abHdr[0] = u8TypeAndFlags;
3074 if (cb < 0x80)
3075 {
3076 cbHdr = 2;
3077 abHdr[1] = (uint8_t)cb;
3078 }
3079 else if (cb < 0x00000800)
3080 {
3081 cbHdr = 3;
3082 abHdr[1] = (uint8_t)(0xc0 | (cb >> 6));
3083 abHdr[2] = (uint8_t)(0x80 | (cb & 0x3f));
3084 }
3085 else if (cb < 0x00010000)
3086 {
3087 cbHdr = 4;
3088 abHdr[1] = (uint8_t)(0xe0 | (cb >> 12));
3089 abHdr[2] = (uint8_t)(0x80 | ((cb >> 6) & 0x3f));
3090 abHdr[3] = (uint8_t)(0x80 | (cb & 0x3f));
3091 }
3092 else if (cb < 0x00200000)
3093 {
3094 cbHdr = 5;
3095 abHdr[1] = (uint8_t)(0xf0 | (cb >> 18));
3096 abHdr[2] = (uint8_t)(0x80 | ((cb >> 12) & 0x3f));
3097 abHdr[3] = (uint8_t)(0x80 | ((cb >> 6) & 0x3f));
3098 abHdr[4] = (uint8_t)(0x80 | (cb & 0x3f));
3099 }
3100 else if (cb < 0x04000000)
3101 {
3102 cbHdr = 6;
3103 abHdr[1] = (uint8_t)(0xf8 | (cb >> 24));
3104 abHdr[2] = (uint8_t)(0x80 | ((cb >> 18) & 0x3f));
3105 abHdr[3] = (uint8_t)(0x80 | ((cb >> 12) & 0x3f));
3106 abHdr[4] = (uint8_t)(0x80 | ((cb >> 6) & 0x3f));
3107 abHdr[5] = (uint8_t)(0x80 | (cb & 0x3f));
3108 }
3109 else if (cb <= 0x7fffffff)
3110 {
3111 cbHdr = 7;
3112 abHdr[1] = (uint8_t)(0xfc | (cb >> 30));
3113 abHdr[2] = (uint8_t)(0x80 | ((cb >> 24) & 0x3f));
3114 abHdr[3] = (uint8_t)(0x80 | ((cb >> 18) & 0x3f));
3115 abHdr[4] = (uint8_t)(0x80 | ((cb >> 12) & 0x3f));
3116 abHdr[5] = (uint8_t)(0x80 | ((cb >> 6) & 0x3f));
3117 abHdr[6] = (uint8_t)(0x80 | (cb & 0x3f));
3118 }
3119 else
3120 AssertLogRelMsgFailedReturn(("cb=%#x\n", cb), pSSM->rc = VERR_INTERNAL_ERROR);
3121
3122 Log3(("ssmR3DataWriteRecHdr: %08llx|%08llx/%08x: Type=%02x fImportant=%RTbool cbHdr=%u\n",
3123 ssmR3StrmTell(&pSSM->Strm) + cbHdr, pSSM->offUnit + cbHdr, cb, u8TypeAndFlags & SSM_REC_TYPE_MASK, !!(u8TypeAndFlags & SSM_REC_FLAGS_IMPORTANT), cbHdr));
3124
3125 return ssmR3DataWriteRaw(pSSM, &abHdr[0], cbHdr);
3126}
3127
3128
3129/**
3130 * Worker that flushes the buffered data.
3131 *
3132 * @returns VBox status code. Will set pSSM->rc on error.
3133 * @param pSSM The saved state handle.
3134 */
3135static int ssmR3DataFlushBuffer(PSSMHANDLE pSSM)
3136{
3137 /*
3138 * Check how much there current is in the buffer.
3139 */
3140 uint32_t cb = pSSM->u.Write.offDataBuffer;
3141 if (!cb)
3142 return pSSM->rc;
3143 pSSM->u.Write.offDataBuffer = 0;
3144
3145 /*
3146 * Write a record header and then the data.
3147 * (No need for fancy optimizations here any longer since the stream is
3148 * fully buffered.)
3149 */
3150 int rc = ssmR3DataWriteRecHdr(pSSM, cb, SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_RAW);
3151 if (RT_SUCCESS(rc))
3152 rc = ssmR3DataWriteRaw(pSSM, pSSM->u.Write.abDataBuffer, cb);
3153 ssmR3Progress(pSSM, cb);
3154 return rc;
3155}
3156
3157
3158/**
3159 * ssmR3DataWrite worker that writes big stuff.
3160 *
3161 * @returns VBox status code
3162 * @param pSSM The saved state handle.
3163 * @param pvBuf The bits to write.
3164 * @param cbBuf The number of bytes to write.
3165 */
3166static int ssmR3DataWriteBig(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf)
3167{
3168 int rc = ssmR3DataFlushBuffer(pSSM);
3169 if (RT_SUCCESS(rc))
3170 {
3171 /*
3172 * Split it up into compression blocks.
3173 */
3174 for (;;)
3175 {
3176 AssertCompile(SSM_ZIP_BLOCK_SIZE == PAGE_SIZE);
3177 if ( cbBuf >= SSM_ZIP_BLOCK_SIZE
3178 && ( ((uintptr_t)pvBuf & 0xf)
3179 || !ASMMemIsZeroPage(pvBuf))
3180 )
3181 {
3182 /*
3183 * Compress it.
3184 */
3185 AssertCompile(1 + 3 + 1 + SSM_ZIP_BLOCK_SIZE < 0x00010000);
3186 uint8_t *pb;
3187 rc = ssmR3StrmReserveWriteBufferSpace(&pSSM->Strm, 1 + 3 + 1 + SSM_ZIP_BLOCK_SIZE, &pb);
3188 if (RT_FAILURE(rc))
3189 break;
3190 size_t cbRec = SSM_ZIP_BLOCK_SIZE - (SSM_ZIP_BLOCK_SIZE / 16);
3191 rc = RTZipBlockCompress(RTZIPTYPE_LZF, RTZIPLEVEL_FAST, 0 /*fFlags*/,
3192 pvBuf, SSM_ZIP_BLOCK_SIZE,
3193 pb + 1 + 3 + 1, cbRec, &cbRec);
3194 if (RT_SUCCESS(rc))
3195 {
3196 pb[0] = SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_RAW_LZF;
3197 pb[4] = SSM_ZIP_BLOCK_SIZE / _1K;
3198 cbRec += 1;
3199 }
3200 else
3201 {
3202 pb[0] = SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_RAW;
3203 memcpy(&pb[4], pvBuf, SSM_ZIP_BLOCK_SIZE);
3204 cbRec = SSM_ZIP_BLOCK_SIZE;
3205 }
3206 pb[1] = (uint8_t)(0xe0 | ( cbRec >> 12));
3207 pb[2] = (uint8_t)(0x80 | ((cbRec >> 6) & 0x3f));
3208 pb[3] = (uint8_t)(0x80 | ( cbRec & 0x3f));
3209 cbRec += 1 + 3;
3210 rc = ssmR3StrmCommitWriteBufferSpace(&pSSM->Strm, cbRec);
3211 if (RT_FAILURE(rc))
3212 break;
3213
3214 pSSM->offUnit += cbRec;
3215 ssmR3Progress(pSSM, SSM_ZIP_BLOCK_SIZE);
3216
3217 /* advance */
3218 if (cbBuf == SSM_ZIP_BLOCK_SIZE)
3219 return VINF_SUCCESS;
3220 cbBuf -= SSM_ZIP_BLOCK_SIZE;
3221 pvBuf = (uint8_t const*)pvBuf + SSM_ZIP_BLOCK_SIZE;
3222 }
3223 else if (cbBuf >= SSM_ZIP_BLOCK_SIZE)
3224 {
3225 /*
3226 * Zero block.
3227 */
3228 uint8_t abRec[3];
3229 abRec[0] = SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_RAW_ZERO;
3230 abRec[1] = 1;
3231 abRec[2] = SSM_ZIP_BLOCK_SIZE / _1K;
3232 Log3(("ssmR3DataWriteBig: %08llx|%08llx/%08x: ZERO\n", ssmR3StrmTell(&pSSM->Strm) + 2, pSSM->offUnit + 2, 1));
3233 rc = ssmR3DataWriteRaw(pSSM, &abRec[0], sizeof(abRec));
3234 if (RT_FAILURE(rc))
3235 break;
3236
3237 /* advance */
3238 ssmR3Progress(pSSM, SSM_ZIP_BLOCK_SIZE);
3239 if (cbBuf == SSM_ZIP_BLOCK_SIZE)
3240 return VINF_SUCCESS;
3241 cbBuf -= SSM_ZIP_BLOCK_SIZE;
3242 pvBuf = (uint8_t const*)pvBuf + SSM_ZIP_BLOCK_SIZE;
3243 }
3244 else
3245 {
3246 /*
3247 * Less than one block left, store it the simple way.
3248 */
3249 rc = ssmR3DataWriteRecHdr(pSSM, cbBuf, SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_RAW);
3250 if (RT_SUCCESS(rc))
3251 rc = ssmR3DataWriteRaw(pSSM, pvBuf, cbBuf);
3252 ssmR3Progress(pSSM, cbBuf);
3253 break;
3254 }
3255 }
3256 }
3257 return rc;
3258}
3259
3260
3261/**
3262 * ssmR3DataWrite worker that is called when there isn't enough room in the
3263 * buffer for the current chunk of data.
3264 *
3265 * This will first flush the buffer and then add the new bits to it.
3266 *
3267 * @returns VBox status code
3268 * @param pSSM The saved state handle.
3269 * @param pvBuf The bits to write.
3270 * @param cbBuf The number of bytes to write.
3271 */
3272static int ssmR3DataWriteFlushAndBuffer(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf)
3273{
3274 int rc = ssmR3DataFlushBuffer(pSSM);
3275 if (RT_SUCCESS(rc))
3276 {
3277 memcpy(&pSSM->u.Write.abDataBuffer[0], pvBuf, cbBuf);
3278 pSSM->u.Write.offDataBuffer = (uint32_t)cbBuf;
3279 }
3280 return rc;
3281}
3282
3283
3284/**
3285 * Writes data to the current data unit.
3286 *
3287 * This is an inlined wrapper that optimizes the small writes that so many of
3288 * the APIs make.
3289 *
3290 * @returns VBox status code
3291 * @param pSSM The saved state handle.
3292 * @param pvBuf The bits to write.
3293 * @param cbBuf The number of bytes to write.
3294 */
3295DECLINLINE(int) ssmR3DataWrite(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf)
3296{
3297 if (cbBuf > sizeof(pSSM->u.Write.abDataBuffer) / 8)
3298 return ssmR3DataWriteBig(pSSM, pvBuf, cbBuf);
3299 if (!cbBuf)
3300 return VINF_SUCCESS;
3301
3302 uint32_t off = pSSM->u.Write.offDataBuffer;
3303 if (RT_UNLIKELY(cbBuf + off > sizeof(pSSM->u.Write.abDataBuffer)))
3304 return ssmR3DataWriteFlushAndBuffer(pSSM, pvBuf, cbBuf);
3305
3306 memcpy(&pSSM->u.Write.abDataBuffer[off], pvBuf, cbBuf);
3307 pSSM->u.Write.offDataBuffer = off + (uint32_t)cbBuf;
3308 return VINF_SUCCESS;
3309}
3310
3311
3312/**
3313 * Puts a structure.
3314 *
3315 * @returns VBox status code.
3316 * @param pSSM The saved state handle.
3317 * @param pvStruct The structure address.
3318 * @param paFields The array of structure fields descriptions.
3319 * The array must be terminated by a SSMFIELD_ENTRY_TERM().
3320 */
3321VMMR3DECL(int) SSMR3PutStruct(PSSMHANDLE pSSM, const void *pvStruct, PCSSMFIELD paFields)
3322{
3323 SSM_ASSERT_WRITEABLE_RET(pSSM);
3324 SSM_CHECK_CANCELLED_RET(pSSM);
3325 AssertPtr(pvStruct);
3326 AssertPtr(paFields);
3327
3328 /* begin marker. */
3329 int rc = SSMR3PutU32(pSSM, SSMR3STRUCT_BEGIN);
3330 if (RT_FAILURE(rc))
3331 return rc;
3332
3333 /* put the fields */
3334 for (PCSSMFIELD pCur = paFields;
3335 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
3336 pCur++)
3337 {
3338 uint8_t const *pbField = (uint8_t const *)pvStruct + pCur->off;
3339 switch ((uintptr_t)pCur->pfnGetPutOrTransformer)
3340 {
3341 case SSMFIELDTRANS_NO_TRANSFORMATION:
3342 rc = ssmR3DataWrite(pSSM, pbField, pCur->cb);
3343 break;
3344
3345 case SSMFIELDTRANS_GCPTR:
3346 AssertMsgReturn(pCur->cb == sizeof(RTGCPTR), ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3347 rc = SSMR3PutGCPtr(pSSM, *(PRTGCPTR)pbField);
3348 break;
3349
3350 case SSMFIELDTRANS_GCPHYS:
3351 AssertMsgReturn(pCur->cb == sizeof(RTGCPHYS), ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3352 rc = SSMR3PutGCPhys(pSSM, *(PRTGCPHYS)pbField);
3353 break;
3354
3355 case SSMFIELDTRANS_RCPTR:
3356 AssertMsgReturn(pCur->cb == sizeof(RTRCPTR), ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3357 rc = SSMR3PutRCPtr(pSSM, *(PRTRCPTR)pbField);
3358 break;
3359
3360 case SSMFIELDTRANS_RCPTR_ARRAY:
3361 {
3362 uint32_t const cEntries = pCur->cb / sizeof(RTRCPTR);
3363 AssertMsgReturn(pCur->cb == cEntries * sizeof(RTRCPTR) && cEntries, ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3364 rc = VINF_SUCCESS;
3365 for (uint32_t i = 0; i < cEntries && RT_SUCCESS(rc); i++)
3366 rc = SSMR3PutRCPtr(pSSM, ((PRTRCPTR)pbField)[i]);
3367 break;
3368 }
3369
3370 default:
3371 AssertMsgFailedReturn(("%#x\n", pCur->pfnGetPutOrTransformer), VERR_SSM_FIELD_COMPLEX);
3372 }
3373 if (RT_FAILURE(rc))
3374 return rc;
3375 }
3376
3377 /* end marker */
3378 return SSMR3PutU32(pSSM, SSMR3STRUCT_END);
3379}
3380
3381
3382/**
3383 * SSMR3PutStructEx helper that puts a HCPTR that is used as a NULL indicator.
3384 *
3385 * @returns VBox status code.
3386 *
3387 * @param pSSM The saved state handle.
3388 * @param pv The value to put.
3389 * @param fFlags SSMSTRUCT_FLAGS_XXX.
3390 */
3391DECLINLINE(int) ssmR3PutHCPtrNI(PSSMHANDLE pSSM, void *pv, uint32_t fFlags)
3392{
3393 int rc;
3394 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
3395 rc = ssmR3DataWrite(pSSM, &pv, sizeof(void *));
3396 else
3397 rc = SSMR3PutBool(pSSM, pv != NULL);
3398 return rc;
3399}
3400
3401
3402/**
3403 * SSMR3PutStructEx helper that puts an arbitrary number of zeros.
3404 *
3405 * @returns VBox status code.
3406 * @param pSSM The saved state handle.
3407 * @param cbToFill The number of zeros to stuff into the state.
3408 */
3409static int ssmR3PutZeros(PSSMHANDLE pSSM, uint32_t cbToFill)
3410{
3411 while (cbToFill > 0)
3412 {
3413 uint32_t cb = RT_MIN(sizeof(g_abZero), cbToFill);
3414 int rc = ssmR3DataWrite(pSSM, g_abZero, cb);
3415 if (RT_FAILURE(rc))
3416 return rc;
3417 cbToFill -= cb;
3418 }
3419 return VINF_SUCCESS;
3420}
3421
3422
3423/**
3424 * Puts a structure, extended API.
3425 *
3426 * @returns VBox status code.
3427 * @param pSSM The saved state handle.
3428 * @param pvStruct The structure address.
3429 * @param cbStruct The size of the struct (use for validation only).
3430 * @param fFlags Combination of SSMSTRUCT_FLAGS_XXX defines.
3431 * @param paFields The array of structure fields descriptions. The
3432 * array must be terminated by a SSMFIELD_ENTRY_TERM().
3433 * @param pvUser User argument for any callbacks that paFields might
3434 * contain.
3435 */
3436VMMR3DECL(int) SSMR3PutStructEx(PSSMHANDLE pSSM, const void *pvStruct, size_t cbStruct,
3437 uint32_t fFlags, PCSSMFIELD paFields, void *pvUser)
3438{
3439 int rc;
3440
3441 /*
3442 * Validation.
3443 */
3444 SSM_ASSERT_WRITEABLE_RET(pSSM);
3445 SSM_CHECK_CANCELLED_RET(pSSM);
3446 AssertMsgReturn(!(fFlags & ~SSMSTRUCT_FLAGS_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
3447 AssertPtr(pvStruct);
3448 AssertPtr(paFields);
3449
3450
3451 /*
3452 * Begin marker.
3453 */
3454 if (!(fFlags & SSMSTRUCT_FLAGS_NO_MARKERS))
3455 {
3456 rc = SSMR3PutU32(pSSM, SSMR3STRUCT_BEGIN);
3457 if (RT_FAILURE(rc))
3458 return rc;
3459 }
3460
3461 /*
3462 * Put the fields
3463 */
3464 uint32_t off = 0;
3465 for (PCSSMFIELD pCur = paFields;
3466 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
3467 pCur++)
3468 {
3469 uint32_t const offField = (!SSMFIELDTRANS_IS_PADDING(pCur->pfnGetPutOrTransformer) || pCur->off != UINT32_MAX / 2)
3470 && !SSMFIELDTRANS_IS_OLD(pCur->pfnGetPutOrTransformer)
3471 ? pCur->off
3472 : off;
3473 uint32_t const cbField = SSMFIELDTRANS_IS_OLD(pCur->pfnGetPutOrTransformer)
3474 ? 0
3475 : SSMFIELDTRANS_IS_PADDING(pCur->pfnGetPutOrTransformer)
3476 ? RT_HIWORD(pCur->cb)
3477 : pCur->cb;
3478 AssertMsgReturn( cbField <= cbStruct
3479 && offField + cbField <= cbStruct
3480 && offField + cbField >= offField,
3481 ("off=%#x cb=%#x cbStruct=%#x (%s)\n", cbField, offField, cbStruct, pCur->pszName),
3482 VERR_SSM_FIELD_OUT_OF_BOUNDS);
3483 AssertMsgReturn( !(fFlags & SSMSTRUCT_FLAGS_FULL_STRUCT)
3484 || off == offField,
3485 ("off=%#x offField=%#x (%s)\n", off, offField, pCur->pszName),
3486 VERR_SSM_FIELD_NOT_CONSECUTIVE);
3487
3488 rc = VINF_SUCCESS;
3489 uint8_t const *pbField = (uint8_t const *)pvStruct + offField;
3490 switch ((uintptr_t)pCur->pfnGetPutOrTransformer)
3491 {
3492 case SSMFIELDTRANS_NO_TRANSFORMATION:
3493 rc = ssmR3DataWrite(pSSM, pbField, cbField);
3494 break;
3495
3496 case SSMFIELDTRANS_GCPHYS:
3497 AssertMsgReturn(cbField == sizeof(RTGCPHYS), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3498 rc = SSMR3PutGCPhys(pSSM, *(PRTGCPHYS)pbField);
3499 break;
3500
3501 case SSMFIELDTRANS_GCPTR:
3502 AssertMsgReturn(cbField == sizeof(RTGCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3503 rc = SSMR3PutGCPtr(pSSM, *(PRTGCPTR)pbField);
3504 break;
3505
3506 case SSMFIELDTRANS_RCPTR:
3507 AssertMsgReturn(cbField == sizeof(RTRCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3508 rc = SSMR3PutRCPtr(pSSM, *(PRTRCPTR)pbField);
3509 break;
3510
3511 case SSMFIELDTRANS_RCPTR_ARRAY:
3512 {
3513 uint32_t const cEntries = cbField / sizeof(RTRCPTR);
3514 AssertMsgReturn(cbField == cEntries * sizeof(RTRCPTR) && cEntries, ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3515 for (uint32_t i = 0; i < cEntries && RT_SUCCESS(rc); i++)
3516 rc = SSMR3PutRCPtr(pSSM, ((PRTRCPTR)pbField)[i]);
3517 break;
3518 }
3519
3520 case SSMFIELDTRANS_HCPTR_NI:
3521 AssertMsgReturn(cbField == sizeof(void *), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3522 rc = ssmR3PutHCPtrNI(pSSM, *(void * const *)pbField, fFlags);
3523 break;
3524
3525 case SSMFIELDTRANS_HCPTR_NI_ARRAY:
3526 {
3527 uint32_t const cEntries = cbField / sizeof(void *);
3528 AssertMsgReturn(cbField == cEntries * sizeof(void *) && cEntries, ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3529 for (uint32_t i = 0; i < cEntries && RT_SUCCESS(rc); i++)
3530 rc = ssmR3PutHCPtrNI(pSSM, ((void * const *)pbField)[i], fFlags);
3531 break;
3532 }
3533
3534 case SSMFIELDTRANS_HCPTR_HACK_U32:
3535 AssertMsgReturn(cbField == sizeof(void *), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3536 AssertMsgReturn(*(uintptr_t *)pbField <= UINT32_MAX, ("%p (%s)\n", *(uintptr_t *)pbField, pCur->pszName), VERR_SSM_FIELD_INVALID_VALUE);
3537 rc = ssmR3DataWrite(pSSM, pbField, sizeof(uint32_t));
3538 if ((fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE) && sizeof(void *) != sizeof(uint32_t))
3539 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(uint32_t));
3540 break;
3541
3542
3543 case SSMFIELDTRANS_IGNORE:
3544 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
3545 rc = ssmR3PutZeros(pSSM, cbField);
3546 break;
3547
3548 case SSMFIELDTRANS_IGN_GCPHYS:
3549 AssertMsgReturn(cbField == sizeof(RTGCPHYS), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3550 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
3551 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(RTGCPHYS));
3552 break;
3553
3554 case SSMFIELDTRANS_IGN_GCPTR:
3555 AssertMsgReturn(cbField == sizeof(RTGCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3556 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
3557 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(RTGCPTR));
3558 break;
3559
3560 case SSMFIELDTRANS_IGN_RCPTR:
3561 AssertMsgReturn(cbField == sizeof(RTRCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3562 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
3563 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(RTRCPTR));
3564 break;
3565
3566 case SSMFIELDTRANS_IGN_HCPTR:
3567 AssertMsgReturn(cbField == sizeof(void *), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3568 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
3569 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(void *));
3570 break;
3571
3572
3573 case SSMFIELDTRANS_OLD:
3574 AssertMsgReturn(pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3575 rc = ssmR3PutZeros(pSSM, pCur->cb);
3576 break;
3577
3578 case SSMFIELDTRANS_OLD_GCPHYS:
3579 AssertMsgReturn(pCur->cb == sizeof(RTGCPHYS) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3580 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(RTGCPHYS));
3581 break;
3582
3583 case SSMFIELDTRANS_OLD_GCPTR:
3584 AssertMsgReturn(pCur->cb == sizeof(RTGCPTR) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3585 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(RTGCPTR));
3586 break;
3587
3588 case SSMFIELDTRANS_OLD_RCPTR:
3589 AssertMsgReturn(pCur->cb == sizeof(RTRCPTR) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3590 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(RTRCPTR));
3591 break;
3592
3593 case SSMFIELDTRANS_OLD_HCPTR:
3594 AssertMsgReturn(pCur->cb == sizeof(void *) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3595 rc = ssmR3DataWrite(pSSM, g_abZero, sizeof(void *));
3596 break;
3597
3598 case SSMFIELDTRANS_OLD_PAD_HC:
3599 AssertMsgReturn(pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3600 rc = ssmR3PutZeros(pSSM, HC_ARCH_BITS == 64 ? RT_HIWORD(pCur->cb) : RT_LOWORD(pCur->cb));
3601 break;
3602
3603 case SSMFIELDTRANS_OLD_PAD_MSC32:
3604 AssertMsgReturn(pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
3605 if (SSM_HOST_IS_MSC_32)
3606 rc = ssmR3PutZeros(pSSM, pCur->cb);
3607 break;
3608
3609
3610 case SSMFIELDTRANS_PAD_HC:
3611 case SSMFIELDTRANS_PAD_HC32:
3612 case SSMFIELDTRANS_PAD_HC64:
3613 case SSMFIELDTRANS_PAD_HC_AUTO:
3614 case SSMFIELDTRANS_PAD_MSC32_AUTO:
3615 {
3616 uint32_t cb32 = RT_BYTE1(pCur->cb);
3617 uint32_t cb64 = RT_BYTE2(pCur->cb);
3618 uint32_t cbCtx = HC_ARCH_BITS == 64
3619 || ( (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_MSC32_AUTO
3620 && !SSM_HOST_IS_MSC_32)
3621 ? cb64 : cb32;
3622 uint32_t cbSaved = ssmR3GetHostBits(pSSM) == 64
3623 || ( (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_MSC32_AUTO
3624 && !ssmR3IsHostMsc32(pSSM))
3625 ? cb64 : cb32;
3626 AssertMsgReturn( cbField == cbCtx
3627 && ( ( pCur->off == UINT32_MAX / 2
3628 && ( cbField == 0
3629 || (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_HC_AUTO
3630 || (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_MSC32_AUTO
3631 )
3632 )
3633 || (pCur->off != UINT32_MAX / 2 && cbField != 0)
3634 )
3635 , ("cbField=%#x cb32=%#x cb64=%#x HC_ARCH_BITS=%u cbCtx=%#x cbSaved=%#x off=%#x\n",
3636 cbField, cb32, cb64, HC_ARCH_BITS, cbCtx, cbSaved, pCur->off),
3637 VERR_SSM_FIELD_INVALID_PADDING_SIZE);
3638 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
3639 rc = ssmR3PutZeros(pSSM, cbSaved);
3640 break;
3641 }
3642
3643 default:
3644 AssertPtrReturn(pCur->pfnGetPutOrTransformer, VERR_SSM_FIELD_INVALID_CALLBACK);
3645 rc = pCur->pfnGetPutOrTransformer(pSSM, pCur, (void *)pvStruct, fFlags, false /*fGetOrPut*/, pvUser);
3646 break;
3647 }
3648 if (RT_FAILURE(rc))
3649 return rc;
3650
3651 off = offField + cbField;
3652 }
3653 AssertMsgReturn( !(fFlags & SSMSTRUCT_FLAGS_FULL_STRUCT)
3654 || off == cbStruct,
3655 ("off=%#x cbStruct=%#x\n", off, cbStruct),
3656 VERR_SSM_FIELD_NOT_CONSECUTIVE);
3657
3658 /*
3659 * End marker
3660 */
3661 if (!(fFlags & SSMSTRUCT_FLAGS_NO_MARKERS))
3662 {
3663 rc = SSMR3PutU32(pSSM, SSMR3STRUCT_END);
3664 if (RT_FAILURE(rc))
3665 return rc;
3666 }
3667
3668 return VINF_SUCCESS;
3669}
3670
3671
3672/**
3673 * Saves a boolean item to the current data unit.
3674 *
3675 * @returns VBox status.
3676 * @param pSSM The saved state handle.
3677 * @param fBool Item to save.
3678 */
3679VMMR3DECL(int) SSMR3PutBool(PSSMHANDLE pSSM, bool fBool)
3680{
3681 SSM_ASSERT_WRITEABLE_RET(pSSM);
3682 SSM_CHECK_CANCELLED_RET(pSSM);
3683 uint8_t u8 = fBool; /* enforce 1 byte size */
3684 return ssmR3DataWrite(pSSM, &u8, sizeof(u8));
3685}
3686
3687
3688/**
3689 * Saves a 8-bit unsigned integer item to the current data unit.
3690 *
3691 * @returns VBox status.
3692 * @param pSSM The saved state handle.
3693 * @param u8 Item to save.
3694 */
3695VMMR3DECL(int) SSMR3PutU8(PSSMHANDLE pSSM, uint8_t u8)
3696{
3697 SSM_ASSERT_WRITEABLE_RET(pSSM);
3698 SSM_CHECK_CANCELLED_RET(pSSM);
3699 return ssmR3DataWrite(pSSM, &u8, sizeof(u8));
3700}
3701
3702
3703/**
3704 * Saves a 8-bit signed integer item to the current data unit.
3705 *
3706 * @returns VBox status.
3707 * @param pSSM The saved state handle.
3708 * @param i8 Item to save.
3709 */
3710VMMR3DECL(int) SSMR3PutS8(PSSMHANDLE pSSM, int8_t i8)
3711{
3712 SSM_ASSERT_WRITEABLE_RET(pSSM);
3713 SSM_CHECK_CANCELLED_RET(pSSM);
3714 return ssmR3DataWrite(pSSM, &i8, sizeof(i8));
3715}
3716
3717
3718/**
3719 * Saves a 16-bit unsigned integer item to the current data unit.
3720 *
3721 * @returns VBox status.
3722 * @param pSSM The saved state handle.
3723 * @param u16 Item to save.
3724 */
3725VMMR3DECL(int) SSMR3PutU16(PSSMHANDLE pSSM, uint16_t u16)
3726{
3727 SSM_ASSERT_WRITEABLE_RET(pSSM);
3728 SSM_CHECK_CANCELLED_RET(pSSM);
3729 return ssmR3DataWrite(pSSM, &u16, sizeof(u16));
3730}
3731
3732
3733/**
3734 * Saves a 16-bit signed integer item to the current data unit.
3735 *
3736 * @returns VBox status.
3737 * @param pSSM The saved state handle.
3738 * @param i16 Item to save.
3739 */
3740VMMR3DECL(int) SSMR3PutS16(PSSMHANDLE pSSM, int16_t i16)
3741{
3742 SSM_ASSERT_WRITEABLE_RET(pSSM);
3743 SSM_CHECK_CANCELLED_RET(pSSM);
3744 return ssmR3DataWrite(pSSM, &i16, sizeof(i16));
3745}
3746
3747
3748/**
3749 * Saves a 32-bit unsigned integer item to the current data unit.
3750 *
3751 * @returns VBox status.
3752 * @param pSSM The saved state handle.
3753 * @param u32 Item to save.
3754 */
3755VMMR3DECL(int) SSMR3PutU32(PSSMHANDLE pSSM, uint32_t u32)
3756{
3757 SSM_ASSERT_WRITEABLE_RET(pSSM);
3758 SSM_CHECK_CANCELLED_RET(pSSM);
3759 return ssmR3DataWrite(pSSM, &u32, sizeof(u32));
3760}
3761
3762
3763/**
3764 * Saves a 32-bit signed integer item to the current data unit.
3765 *
3766 * @returns VBox status.
3767 * @param pSSM The saved state handle.
3768 * @param i32 Item to save.
3769 */
3770VMMR3DECL(int) SSMR3PutS32(PSSMHANDLE pSSM, int32_t i32)
3771{
3772 SSM_ASSERT_WRITEABLE_RET(pSSM);
3773 SSM_CHECK_CANCELLED_RET(pSSM);
3774 return ssmR3DataWrite(pSSM, &i32, sizeof(i32));
3775}
3776
3777
3778/**
3779 * Saves a 64-bit unsigned integer item to the current data unit.
3780 *
3781 * @returns VBox status.
3782 * @param pSSM The saved state handle.
3783 * @param u64 Item to save.
3784 */
3785VMMR3DECL(int) SSMR3PutU64(PSSMHANDLE pSSM, uint64_t u64)
3786{
3787 SSM_ASSERT_WRITEABLE_RET(pSSM);
3788 SSM_CHECK_CANCELLED_RET(pSSM);
3789 return ssmR3DataWrite(pSSM, &u64, sizeof(u64));
3790}
3791
3792
3793/**
3794 * Saves a 64-bit signed integer item to the current data unit.
3795 *
3796 * @returns VBox status.
3797 * @param pSSM The saved state handle.
3798 * @param i64 Item to save.
3799 */
3800VMMR3DECL(int) SSMR3PutS64(PSSMHANDLE pSSM, int64_t i64)
3801{
3802 SSM_ASSERT_WRITEABLE_RET(pSSM);
3803 SSM_CHECK_CANCELLED_RET(pSSM);
3804 return ssmR3DataWrite(pSSM, &i64, sizeof(i64));
3805}
3806
3807
3808/**
3809 * Saves a 128-bit unsigned integer item to the current data unit.
3810 *
3811 * @returns VBox status.
3812 * @param pSSM The saved state handle.
3813 * @param u128 Item to save.
3814 */
3815VMMR3DECL(int) SSMR3PutU128(PSSMHANDLE pSSM, uint128_t u128)
3816{
3817 SSM_ASSERT_WRITEABLE_RET(pSSM);
3818 SSM_CHECK_CANCELLED_RET(pSSM);
3819 return ssmR3DataWrite(pSSM, &u128, sizeof(u128));
3820}
3821
3822
3823/**
3824 * Saves a 128-bit signed integer item to the current data unit.
3825 *
3826 * @returns VBox status.
3827 * @param pSSM The saved state handle.
3828 * @param i128 Item to save.
3829 */
3830VMMR3DECL(int) SSMR3PutS128(PSSMHANDLE pSSM, int128_t i128)
3831{
3832 SSM_ASSERT_WRITEABLE_RET(pSSM);
3833 SSM_CHECK_CANCELLED_RET(pSSM);
3834 return ssmR3DataWrite(pSSM, &i128, sizeof(i128));
3835}
3836
3837
3838/**
3839 * Saves a VBox unsigned integer item to the current data unit.
3840 *
3841 * @returns VBox status.
3842 * @param pSSM The saved state handle.
3843 * @param u Item to save.
3844 */
3845VMMR3DECL(int) SSMR3PutUInt(PSSMHANDLE pSSM, RTUINT u)
3846{
3847 SSM_ASSERT_WRITEABLE_RET(pSSM);
3848 SSM_CHECK_CANCELLED_RET(pSSM);
3849 return ssmR3DataWrite(pSSM, &u, sizeof(u));
3850}
3851
3852
3853/**
3854 * Saves a VBox signed integer item to the current data unit.
3855 *
3856 * @returns VBox status.
3857 * @param pSSM The saved state handle.
3858 * @param i Item to save.
3859 */
3860VMMR3DECL(int) SSMR3PutSInt(PSSMHANDLE pSSM, RTINT i)
3861{
3862 SSM_ASSERT_WRITEABLE_RET(pSSM);
3863 SSM_CHECK_CANCELLED_RET(pSSM);
3864 return ssmR3DataWrite(pSSM, &i, sizeof(i));
3865}
3866
3867
3868/**
3869 * Saves a GC natural unsigned integer item to the current data unit.
3870 *
3871 * @returns VBox status.
3872 * @param pSSM The saved state handle.
3873 * @param u Item to save.
3874 *
3875 * @deprecated Silly type, don't use it.
3876 */
3877VMMR3DECL(int) SSMR3PutGCUInt(PSSMHANDLE pSSM, RTGCUINT u)
3878{
3879 SSM_ASSERT_WRITEABLE_RET(pSSM);
3880 SSM_CHECK_CANCELLED_RET(pSSM);
3881 return ssmR3DataWrite(pSSM, &u, sizeof(u));
3882}
3883
3884
3885/**
3886 * Saves a GC unsigned integer register item to the current data unit.
3887 *
3888 * @returns VBox status.
3889 * @param pSSM The saved state handle.
3890 * @param u Item to save.
3891 */
3892VMMR3DECL(int) SSMR3PutGCUIntReg(PSSMHANDLE pSSM, RTGCUINTREG u)
3893{
3894 SSM_ASSERT_WRITEABLE_RET(pSSM);
3895 SSM_CHECK_CANCELLED_RET(pSSM);
3896 return ssmR3DataWrite(pSSM, &u, sizeof(u));
3897}
3898
3899
3900/**
3901 * Saves a 32 bits GC physical address item to the current data unit.
3902 *
3903 * @returns VBox status.
3904 * @param pSSM The saved state handle.
3905 * @param GCPhys The item to save
3906 */
3907VMMR3DECL(int) SSMR3PutGCPhys32(PSSMHANDLE pSSM, RTGCPHYS32 GCPhys)
3908{
3909 SSM_ASSERT_WRITEABLE_RET(pSSM);
3910 SSM_CHECK_CANCELLED_RET(pSSM);
3911 return ssmR3DataWrite(pSSM, &GCPhys, sizeof(GCPhys));
3912}
3913
3914
3915/**
3916 * Saves a 64 bits GC physical address item to the current data unit.
3917 *
3918 * @returns VBox status.
3919 * @param pSSM The saved state handle.
3920 * @param GCPhys The item to save
3921 */
3922VMMR3DECL(int) SSMR3PutGCPhys64(PSSMHANDLE pSSM, RTGCPHYS64 GCPhys)
3923{
3924 SSM_ASSERT_WRITEABLE_RET(pSSM);
3925 SSM_CHECK_CANCELLED_RET(pSSM);
3926 return ssmR3DataWrite(pSSM, &GCPhys, sizeof(GCPhys));
3927}
3928
3929
3930/**
3931 * Saves a GC physical address item to the current data unit.
3932 *
3933 * @returns VBox status.
3934 * @param pSSM The saved state handle.
3935 * @param GCPhys The item to save
3936 */
3937VMMR3DECL(int) SSMR3PutGCPhys(PSSMHANDLE pSSM, RTGCPHYS GCPhys)
3938{
3939 SSM_ASSERT_WRITEABLE_RET(pSSM);
3940 SSM_CHECK_CANCELLED_RET(pSSM);
3941 return ssmR3DataWrite(pSSM, &GCPhys, sizeof(GCPhys));
3942}
3943
3944
3945/**
3946 * Saves a GC virtual address item to the current data unit.
3947 *
3948 * @returns VBox status.
3949 * @param pSSM The saved state handle.
3950 * @param GCPtr The item to save.
3951 */
3952VMMR3DECL(int) SSMR3PutGCPtr(PSSMHANDLE pSSM, RTGCPTR GCPtr)
3953{
3954 SSM_ASSERT_WRITEABLE_RET(pSSM);
3955 SSM_CHECK_CANCELLED_RET(pSSM);
3956 return ssmR3DataWrite(pSSM, &GCPtr, sizeof(GCPtr));
3957}
3958
3959
3960/**
3961 * Saves an RC virtual address item to the current data unit.
3962 *
3963 * @returns VBox status.
3964 * @param pSSM The saved state handle.
3965 * @param RCPtr The item to save.
3966 */
3967VMMR3DECL(int) SSMR3PutRCPtr(PSSMHANDLE pSSM, RTRCPTR RCPtr)
3968{
3969 SSM_ASSERT_WRITEABLE_RET(pSSM);
3970 SSM_CHECK_CANCELLED_RET(pSSM);
3971 return ssmR3DataWrite(pSSM, &RCPtr, sizeof(RCPtr));
3972}
3973
3974
3975/**
3976 * Saves a GC virtual address (represented as an unsigned integer) item to the current data unit.
3977 *
3978 * @returns VBox status.
3979 * @param pSSM The saved state handle.
3980 * @param GCPtr The item to save.
3981 */
3982VMMR3DECL(int) SSMR3PutGCUIntPtr(PSSMHANDLE pSSM, RTGCUINTPTR GCPtr)
3983{
3984 SSM_ASSERT_WRITEABLE_RET(pSSM);
3985 SSM_CHECK_CANCELLED_RET(pSSM);
3986 return ssmR3DataWrite(pSSM, &GCPtr, sizeof(GCPtr));
3987}
3988
3989
3990/**
3991 * Saves a I/O port address item to the current data unit.
3992 *
3993 * @returns VBox status.
3994 * @param pSSM The saved state handle.
3995 * @param IOPort The item to save.
3996 */
3997VMMR3DECL(int) SSMR3PutIOPort(PSSMHANDLE pSSM, RTIOPORT IOPort)
3998{
3999 SSM_ASSERT_WRITEABLE_RET(pSSM);
4000 SSM_CHECK_CANCELLED_RET(pSSM);
4001 return ssmR3DataWrite(pSSM, &IOPort, sizeof(IOPort));
4002}
4003
4004
4005/**
4006 * Saves a selector item to the current data unit.
4007 *
4008 * @returns VBox status.
4009 * @param pSSM The saved state handle.
4010 * @param Sel The item to save.
4011 */
4012VMMR3DECL(int) SSMR3PutSel(PSSMHANDLE pSSM, RTSEL Sel)
4013{
4014 SSM_ASSERT_WRITEABLE_RET(pSSM);
4015 SSM_CHECK_CANCELLED_RET(pSSM);
4016 return ssmR3DataWrite(pSSM, &Sel, sizeof(Sel));
4017}
4018
4019
4020/**
4021 * Saves a memory item to the current data unit.
4022 *
4023 * @returns VBox status.
4024 * @param pSSM The saved state handle.
4025 * @param pv Item to save.
4026 * @param cb Size of the item.
4027 */
4028VMMR3DECL(int) SSMR3PutMem(PSSMHANDLE pSSM, const void *pv, size_t cb)
4029{
4030 SSM_ASSERT_WRITEABLE_RET(pSSM);
4031 SSM_CHECK_CANCELLED_RET(pSSM);
4032 return ssmR3DataWrite(pSSM, pv, cb);
4033}
4034
4035
4036/**
4037 * Saves a zero terminated string item to the current data unit.
4038 *
4039 * @returns VBox status.
4040 * @param pSSM The saved state handle.
4041 * @param psz Item to save.
4042 */
4043VMMR3DECL(int) SSMR3PutStrZ(PSSMHANDLE pSSM, const char *psz)
4044{
4045 SSM_ASSERT_WRITEABLE_RET(pSSM);
4046 SSM_CHECK_CANCELLED_RET(pSSM);
4047
4048 size_t cch = strlen(psz);
4049 if (cch > _1M)
4050 {
4051 AssertMsgFailed(("a %zu byte long string, what's this!?!\n", cch));
4052 return VERR_TOO_MUCH_DATA;
4053 }
4054 uint32_t u32 = (uint32_t)cch;
4055 int rc = ssmR3DataWrite(pSSM, &u32, sizeof(u32));
4056 if (rc)
4057 return rc;
4058 return ssmR3DataWrite(pSSM, psz, cch);
4059}
4060
4061
4062/**
4063 * Do the pfnSaveDone run.
4064 *
4065 * @returns VBox status code (pSSM->rc).
4066 * @param pVM The VM handle.
4067 * @param pSSM The saved state handle.
4068 */
4069static int ssmR3SaveDoDoneRun(PVM pVM, PSSMHANDLE pSSM)
4070{
4071 VM_ASSERT_EMT0(pVM);
4072
4073 /*
4074 * Do the done run.
4075 */
4076 pSSM->enmOp = SSMSTATE_SAVE_DONE;
4077 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4078 {
4079 if ( pUnit->u.Common.pfnSaveDone
4080 && ( pUnit->fCalled
4081 || (!pUnit->u.Common.pfnSavePrep && !pUnit->u.Common.pfnSaveExec)))
4082 {
4083 int rcOld = pSSM->rc;
4084 int rc;
4085 switch (pUnit->enmType)
4086 {
4087 case SSMUNITTYPE_DEV:
4088 rc = pUnit->u.Dev.pfnSaveDone(pUnit->u.Dev.pDevIns, pSSM);
4089 break;
4090 case SSMUNITTYPE_DRV:
4091 rc = pUnit->u.Drv.pfnSaveDone(pUnit->u.Drv.pDrvIns, pSSM);
4092 break;
4093 case SSMUNITTYPE_INTERNAL:
4094 rc = pUnit->u.Internal.pfnSaveDone(pVM, pSSM);
4095 break;
4096 case SSMUNITTYPE_EXTERNAL:
4097 rc = pUnit->u.External.pfnSaveDone(pSSM, pUnit->u.External.pvUser);
4098 break;
4099 default:
4100 rc = VERR_INTERNAL_ERROR;
4101 break;
4102 }
4103 if (RT_SUCCESS(rc) && pSSM->rc != rcOld)
4104 rc = pSSM->rc;
4105 if (RT_FAILURE(rc))
4106 {
4107 LogRel(("SSM: Done save failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
4108 if (RT_SUCCESS_NP(pSSM->rc))
4109 pSSM->rc = rc;
4110 }
4111 }
4112 }
4113 return pSSM->rc;
4114}
4115
4116
4117/**
4118 * Worker for SSMR3LiveDone and SSMR3Save that closes the handle and deletes the
4119 * saved state file on failure.
4120 *
4121 * @returns VBox status code (pSSM->rc).
4122 * @param pVM The VM handle.
4123 * @param pSSM The saved state handle.
4124 */
4125static int ssmR3SaveDoClose(PVM pVM, PSSMHANDLE pSSM)
4126{
4127 VM_ASSERT_EMT0(pVM);
4128 pVM->ssm.s.uPass = 0;
4129
4130 /*
4131 * Make it non-cancellable, close the stream and delete the file on failure.
4132 */
4133 ssmR3SetCancellable(pVM, pSSM, false);
4134 int rc = ssmR3StrmClose(&pSSM->Strm, pSSM->rc == VERR_SSM_CANCELLED);
4135 if (RT_SUCCESS(rc))
4136 rc = pSSM->rc;
4137 if (RT_SUCCESS(rc))
4138 {
4139 Assert(pSSM->enmOp == SSMSTATE_SAVE_DONE);
4140 if (pSSM->pfnProgress)
4141 pSSM->pfnProgress(pVM, 100, pSSM->pvUser);
4142 LogRel(("SSM: Successfully saved the VM state to '%s'\n",
4143 pSSM->pszFilename ? pSSM->pszFilename : "<remote-machine>"));
4144 }
4145 else
4146 {
4147 if (pSSM->pszFilename)
4148 {
4149 int rc2 = RTFileDelete(pSSM->pszFilename);
4150 AssertRC(rc2);
4151 if (RT_SUCCESS(rc2))
4152 LogRel(("SSM: Failed to save the VM state to '%s' (file deleted): %Rrc\n",
4153 pSSM->pszFilename, rc));
4154 else
4155 LogRel(("SSM: Failed to save the VM state to '%s' (file deletion failed, rc2=%Rrc): %Rrc\n",
4156 pSSM->pszFilename, rc2, rc));
4157 }
4158 else
4159 LogRel(("SSM: Failed to save the VM state.\n"));
4160
4161 Assert(pSSM->enmOp <= SSMSTATE_SAVE_DONE);
4162 if (pSSM->enmOp != SSMSTATE_SAVE_DONE)
4163 ssmR3SaveDoDoneRun(pVM, pSSM);
4164 }
4165
4166 /*
4167 * Trash the handle before freeing it.
4168 */
4169 ASMAtomicWriteU32(&pSSM->fCancelled, 0);
4170 pSSM->pVM = NULL;
4171 pSSM->enmAfter = SSMAFTER_INVALID;
4172 pSSM->enmOp = SSMSTATE_INVALID;
4173 RTMemFree(pSSM);
4174
4175 return rc;
4176}
4177
4178
4179/**
4180 * Closes the SSM handle.
4181 *
4182 * This must always be called on a handled returned by SSMR3LiveSave.
4183 *
4184 * @returns VBox status.
4185 *
4186 * @param pSSM The SSM handle returned by SSMR3LiveSave.
4187 *
4188 * @thread EMT(0).
4189 */
4190VMMR3_INT_DECL(int) SSMR3LiveDone(PSSMHANDLE pSSM)
4191{
4192 LogFlow(("SSMR3LiveDone: pSSM=%p\n", pSSM));
4193
4194 /*
4195 * Validate input.
4196 */
4197 AssertPtrReturn(pSSM, VERR_INVALID_POINTER);
4198 PVM pVM = pSSM->pVM;
4199 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4200 VM_ASSERT_EMT0(pVM);
4201 AssertMsgReturn( pSSM->enmAfter == SSMAFTER_DESTROY
4202 || pSSM->enmAfter == SSMAFTER_CONTINUE
4203 || pSSM->enmAfter == SSMAFTER_TELEPORT,
4204 ("%d\n", pSSM->enmAfter),
4205 VERR_INVALID_PARAMETER);
4206 AssertMsgReturn( pSSM->enmOp >= SSMSTATE_LIVE_PREP
4207 && pSSM->enmOp <= SSMSTATE_SAVE_DONE,
4208 ("%d\n", pSSM->enmOp), VERR_INVALID_STATE);
4209
4210 /*
4211 * Join paths with SSMR3Save again.
4212 */
4213 return ssmR3SaveDoClose(pVM, pSSM);
4214}
4215
4216
4217/**
4218 * Writes the directory.
4219 *
4220 * @returns VBox status code.
4221 * @param pVM The VM handle.
4222 * @param pSSM The SSM handle.
4223 * @param pcEntries Where to return the number of directory entries.
4224 */
4225static int ssmR3WriteDirectory(PVM pVM, PSSMHANDLE pSSM, uint32_t *pcEntries)
4226{
4227 VM_ASSERT_EMT0(pVM);
4228
4229 /*
4230 * Grab some temporary memory for the dictionary.
4231 */
4232 size_t cbDir = RT_OFFSETOF(SSMFILEDIR, aEntries[pVM->ssm.s.cUnits]);
4233 PSSMFILEDIR pDir = (PSSMFILEDIR)RTMemTmpAlloc(cbDir);
4234 if (!pDir)
4235 {
4236 LogRel(("ssmR3WriteDirectory: failed to allocate %zu bytes!\n", cbDir));
4237 return VERR_NO_TMP_MEMORY;
4238 }
4239
4240 /*
4241 * Initialize it.
4242 */
4243 memcpy(pDir->szMagic, SSMFILEDIR_MAGIC, sizeof(pDir->szMagic));
4244 pDir->u32CRC = 0;
4245 pDir->cEntries = 0;
4246
4247 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4248 if (pUnit->offStream != RTFOFF_MIN)
4249 {
4250 PSSMFILEDIRENTRY pEntry = &pDir->aEntries[pDir->cEntries++];
4251 Assert(pDir->cEntries <= pVM->ssm.s.cUnits);
4252 Assert(pUnit->offStream >= (RTFOFF)sizeof(SSMFILEHDR));
4253 pEntry->off = pUnit->offStream;
4254 pEntry->u32Instance = pUnit->u32Instance;
4255 pEntry->u32NameCRC = RTCrc32(pUnit->szName, pUnit->cchName);
4256 }
4257
4258 /*
4259 * Calculate the actual size and CRC-32, then write the directory
4260 * out to the stream.
4261 */
4262 *pcEntries = pDir->cEntries;
4263 cbDir = RT_OFFSETOF(SSMFILEDIR, aEntries[pDir->cEntries]);
4264 pDir->u32CRC = RTCrc32(pDir, cbDir);
4265 int rc = ssmR3StrmWrite(&pSSM->Strm, pDir, cbDir);
4266 RTMemTmpFree(pDir);
4267 return rc;
4268}
4269
4270
4271/**
4272 * Finalize the saved state stream, i.e. add the end unit, directory
4273 * and footer.
4274 *
4275 * @returns VBox status code (pSSM->rc).
4276 * @param pVM The VM handle.
4277 * @param pSSM The saved state handle.
4278 */
4279static int ssmR3SaveDoFinalization(PVM pVM, PSSMHANDLE pSSM)
4280{
4281 VM_ASSERT_EMT0(pVM);
4282 Assert(RT_SUCCESS(pSSM->rc));
4283
4284 /*
4285 * Write the end unit.
4286 */
4287 SSMFILEUNITHDRV2 UnitHdr;
4288 memcpy(&UnitHdr.szMagic[0], SSMFILEUNITHDR_END, sizeof(UnitHdr.szMagic));
4289 UnitHdr.offStream = ssmR3StrmTell(&pSSM->Strm);
4290 UnitHdr.u32CurStreamCRC = ssmR3StrmCurCRC(&pSSM->Strm);
4291 UnitHdr.u32CRC = 0;
4292 UnitHdr.u32Version = 0;
4293 UnitHdr.u32Instance = 0;
4294 UnitHdr.u32Pass = SSM_PASS_FINAL;
4295 UnitHdr.fFlags = 0;
4296 UnitHdr.cbName = 0;
4297 UnitHdr.u32CRC = RTCrc32(&UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[0]));
4298 Log(("SSM: Unit at %#9llx: END UNIT\n", UnitHdr.offStream));
4299 int rc = ssmR3StrmWrite(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[0]));
4300 if (RT_FAILURE(rc))
4301 {
4302 LogRel(("SSM: Failed writing the end unit: %Rrc\n", rc));
4303 return pSSM->rc = rc;
4304 }
4305
4306 /*
4307 * Write the directory for the final units and then the footer.
4308 */
4309 SSMFILEFTR Footer;
4310 rc = ssmR3WriteDirectory(pVM, pSSM, &Footer.cDirEntries);
4311 if (RT_FAILURE(rc))
4312 {
4313 LogRel(("SSM: Failed writing the directory: %Rrc\n", rc));
4314 return pSSM->rc = rc;
4315 }
4316
4317 memcpy(Footer.szMagic, SSMFILEFTR_MAGIC, sizeof(Footer.szMagic));
4318 Footer.offStream = ssmR3StrmTell(&pSSM->Strm);
4319 Footer.u32StreamCRC = ssmR3StrmFinalCRC(&pSSM->Strm);
4320 Footer.u32Reserved = 0;
4321 Footer.u32CRC = 0;
4322 Footer.u32CRC = RTCrc32(&Footer, sizeof(Footer));
4323 Log(("SSM: Footer at %#9llx: \n", Footer.offStream));
4324 rc = ssmR3StrmWrite(&pSSM->Strm, &Footer, sizeof(Footer));
4325 if (RT_SUCCESS(rc))
4326 rc = ssmR3StrmSetEnd(&pSSM->Strm);
4327 if (RT_FAILURE(rc))
4328 {
4329 LogRel(("SSM: Failed writing the footer: %Rrc\n", rc));
4330 return pSSM->rc = rc;
4331 }
4332
4333 LogRel(("SSM: Footer at %#llx (%lld), %u directory entries.\n",
4334 Footer.offStream, Footer.offStream, Footer.cDirEntries));
4335 return VINF_SUCCESS;
4336}
4337
4338
4339/**
4340 * Do the pfnSaveExec run.
4341 *
4342 * @returns VBox status code (pSSM->rc).
4343 * @param pVM The VM handle.
4344 * @param pSSM The saved state handle.
4345 */
4346static int ssmR3SaveDoExecRun(PVM pVM, PSSMHANDLE pSSM)
4347{
4348 VM_ASSERT_EMT0(pVM);
4349 AssertRC(pSSM->rc);
4350 pSSM->rc = VINF_SUCCESS;
4351 pSSM->enmOp = SSMSTATE_SAVE_EXEC;
4352 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4353 {
4354 /*
4355 * Not all unit have a callback. Skip those which don't and
4356 * make sure to keep the progress indicator up to date.
4357 */
4358 pSSM->offEstUnitEnd += pUnit->cbGuess;
4359 if (!pUnit->u.Common.pfnSaveExec)
4360 {
4361 pUnit->fCalled = true;
4362 if (pUnit->cbGuess)
4363 ssmR3Progress(pSSM, pSSM->offEstUnitEnd - pSSM->offEst);
4364 continue;
4365 }
4366 pUnit->offStream = ssmR3StrmTell(&pSSM->Strm);
4367
4368 /*
4369 * Check for cancellation.
4370 */
4371 if (RT_UNLIKELY(ASMAtomicUoReadU32(&(pSSM)->fCancelled) == SSMHANDLE_CANCELLED))
4372 {
4373 LogRel(("SSM: Cancelled!\n"));
4374 AssertRC(pSSM->rc);
4375 return pSSM->rc = VERR_SSM_CANCELLED;
4376 }
4377
4378 /*
4379 * Write data unit header
4380 */
4381 SSMFILEUNITHDRV2 UnitHdr;
4382 memcpy(&UnitHdr.szMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(UnitHdr.szMagic));
4383 UnitHdr.offStream = pUnit->offStream;
4384 UnitHdr.u32CurStreamCRC = ssmR3StrmCurCRC(&pSSM->Strm);
4385 UnitHdr.u32CRC = 0;
4386 UnitHdr.u32Version = pUnit->u32Version;
4387 UnitHdr.u32Instance = pUnit->u32Instance;
4388 UnitHdr.u32Pass = SSM_PASS_FINAL;
4389 UnitHdr.fFlags = 0;
4390 UnitHdr.cbName = (uint32_t)pUnit->cchName + 1;
4391 memcpy(&UnitHdr.szName[0], &pUnit->szName[0], UnitHdr.cbName);
4392 UnitHdr.u32CRC = RTCrc32(&UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]));
4393 Log(("SSM: Unit at %#9llx: '%s', instance %u, pass %#x, version %u\n",
4394 UnitHdr.offStream, UnitHdr.szName, UnitHdr.u32Instance, UnitHdr.u32Pass, UnitHdr.u32Version));
4395 int rc = ssmR3StrmWrite(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]));
4396 if (RT_FAILURE(rc))
4397 {
4398 LogRel(("SSM: Failed to write unit header. rc=%Rrc\n", rc));
4399 return pSSM->rc = rc;
4400 }
4401
4402 /*
4403 * Call the execute handler.
4404 */
4405 ssmR3DataWriteBegin(pSSM);
4406 switch (pUnit->enmType)
4407 {
4408 case SSMUNITTYPE_DEV:
4409 rc = pUnit->u.Dev.pfnSaveExec(pUnit->u.Dev.pDevIns, pSSM);
4410 break;
4411 case SSMUNITTYPE_DRV:
4412 rc = pUnit->u.Drv.pfnSaveExec(pUnit->u.Drv.pDrvIns, pSSM);
4413 break;
4414 case SSMUNITTYPE_INTERNAL:
4415 rc = pUnit->u.Internal.pfnSaveExec(pVM, pSSM);
4416 break;
4417 case SSMUNITTYPE_EXTERNAL:
4418 pUnit->u.External.pfnSaveExec(pSSM, pUnit->u.External.pvUser);
4419 rc = pSSM->rc;
4420 break;
4421 default:
4422 rc = VERR_INTERNAL_ERROR;
4423 break;
4424 }
4425 pUnit->fCalled = true;
4426 if (RT_FAILURE(rc) && RT_SUCCESS_NP(pSSM->rc))
4427 pSSM->rc = rc;
4428 else
4429 rc = ssmR3DataFlushBuffer(pSSM); /* will return SSMHANDLE::rc if it is set */
4430 if (RT_FAILURE(rc))
4431 {
4432 LogRel(("SSM: Execute save failed with rc=%Rrc for data unit '%s'/#%u.\n", rc, pUnit->szName, pUnit->u32Instance));
4433 return rc;
4434 }
4435
4436 /*
4437 * Write the termination record and flush the compression stream.
4438 */
4439 SSMRECTERM TermRec;
4440 TermRec.u8TypeAndFlags = SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_TERM;
4441 TermRec.cbRec = sizeof(TermRec) - 2;
4442 if (pSSM->Strm.fChecksummed)
4443 {
4444 TermRec.fFlags = SSMRECTERM_FLAGS_CRC32;
4445 TermRec.u32StreamCRC = RTCrc32Finish(RTCrc32Process(ssmR3StrmCurCRC(&pSSM->Strm), &TermRec, 2));
4446 }
4447 else
4448 {
4449 TermRec.fFlags = 0;
4450 TermRec.u32StreamCRC = 0;
4451 }
4452 TermRec.cbUnit = pSSM->offUnit + sizeof(TermRec);
4453 rc = ssmR3DataWriteRaw(pSSM, &TermRec, sizeof(TermRec));
4454 if (RT_SUCCESS(rc))
4455 rc = ssmR3DataWriteFinish(pSSM);
4456 if (RT_FAILURE(rc))
4457 {
4458 LogRel(("SSM: Failed terminating unit: %Rrc\n", rc));
4459 return pSSM->rc = rc;
4460 }
4461
4462 /*
4463 * Advance the progress indicator to the end of the current unit.
4464 */
4465 ssmR3Progress(pSSM, pSSM->offEstUnitEnd - pSSM->offEst);
4466 } /* for each unit */
4467
4468
4469 /* (progress should be pending 99% now) */
4470 AssertMsg( pSSM->uPercent == (101 - pSSM->uPercentDone)
4471 || pSSM->fLiveSave, ("%d\n", pSSM->uPercent));
4472 return VINF_SUCCESS;
4473}
4474
4475
4476/**
4477 * Do the pfnSavePrep run.
4478 *
4479 * @returns VBox status code (pSSM->rc).
4480 * @param pVM The VM handle.
4481 * @param pSSM The saved state handle.
4482 */
4483static int ssmR3SaveDoPrepRun(PVM pVM, PSSMHANDLE pSSM)
4484{
4485 VM_ASSERT_EMT0(pVM);
4486 Assert(RT_SUCCESS(pSSM->rc));
4487 pSSM->enmOp = SSMSTATE_SAVE_PREP;
4488 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4489 {
4490 if (pUnit->u.Common.pfnSavePrep)
4491 {
4492 int rc;
4493 switch (pUnit->enmType)
4494 {
4495 case SSMUNITTYPE_DEV:
4496 rc = pUnit->u.Dev.pfnSavePrep(pUnit->u.Dev.pDevIns, pSSM);
4497 break;
4498 case SSMUNITTYPE_DRV:
4499 rc = pUnit->u.Drv.pfnSavePrep(pUnit->u.Drv.pDrvIns, pSSM);
4500 break;
4501 case SSMUNITTYPE_INTERNAL:
4502 rc = pUnit->u.Internal.pfnSavePrep(pVM, pSSM);
4503 break;
4504 case SSMUNITTYPE_EXTERNAL:
4505 rc = pUnit->u.External.pfnSavePrep(pSSM, pUnit->u.External.pvUser);
4506 break;
4507 default:
4508 rc = VERR_INTERNAL_ERROR;
4509 break;
4510 }
4511 pUnit->fCalled = true;
4512 if (RT_FAILURE(rc) && RT_SUCCESS_NP(pSSM->rc))
4513 pSSM->rc = rc;
4514 else
4515 rc = pSSM->rc;
4516 if (RT_FAILURE(rc))
4517 {
4518 LogRel(("SSM: Prepare save failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
4519 return rc;
4520 }
4521 }
4522
4523 pSSM->cbEstTotal += pUnit->cbGuess;
4524 }
4525
4526 /*
4527 * Work the progress indicator if we got one.
4528 */
4529 if (pSSM->pfnProgress)
4530 pSSM->pfnProgress(pVM, pSSM->uPercentPrepare-1, pSSM->pvUser);
4531 pSSM->uPercent = pSSM->uPercentPrepare;
4532
4533 return VINF_SUCCESS;
4534}
4535
4536
4537/**
4538 * Common worker for SSMR3Save and SSMR3LiveSave.
4539 *
4540 * @returns VBox status code (no need to check pSSM->rc).
4541 * @param pVM The VM handle.
4542 * @param pSSM The state handle.
4543 *
4544 * @thread EMT(0)
4545 */
4546static int ssmR3SaveDoCommon(PVM pVM, PSSMHANDLE pSSM)
4547{
4548 VM_ASSERT_EMT0(pVM);
4549
4550 /*
4551 * Do the work.
4552 */
4553 int rc = ssmR3SaveDoPrepRun(pVM, pSSM);
4554 if (RT_SUCCESS(rc))
4555 {
4556 rc = ssmR3SaveDoExecRun(pVM, pSSM);
4557 if (RT_SUCCESS(rc))
4558 rc = ssmR3SaveDoFinalization(pVM, pSSM);
4559 }
4560 Assert(pSSM->rc == rc);
4561 int rc2 = ssmR3SaveDoDoneRun(pVM, pSSM);
4562 if (RT_SUCCESS(rc))
4563 rc = rc2;
4564
4565 return rc;
4566}
4567
4568
4569/**
4570 * Saves the rest of the state on EMT0.
4571 *
4572 * @returns VBox status.
4573 *
4574 * @param pSSM The SSM handle returned by SSMR3LiveSave.
4575 *
4576 * @thread Non-EMT thread. Will involve the EMT at the end of the operation.
4577 */
4578VMMR3_INT_DECL(int) SSMR3LiveDoStep2(PSSMHANDLE pSSM)
4579{
4580 LogFlow(("SSMR3LiveDoStep2: pSSM=%p\n", pSSM));
4581
4582 /*
4583 * Validate input.
4584 */
4585 AssertPtrReturn(pSSM, VERR_INVALID_POINTER);
4586 PVM pVM = pSSM->pVM;
4587 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
4588 VM_ASSERT_EMT0(pVM);
4589 AssertMsgReturn( pSSM->enmAfter == SSMAFTER_DESTROY
4590 || pSSM->enmAfter == SSMAFTER_CONTINUE
4591 || pSSM->enmAfter == SSMAFTER_TELEPORT,
4592 ("%d\n", pSSM->enmAfter),
4593 VERR_INVALID_PARAMETER);
4594 AssertMsgReturn(pSSM->enmOp == SSMSTATE_LIVE_STEP2, ("%d\n", pSSM->enmOp), VERR_INVALID_STATE);
4595 AssertRCReturn(pSSM->rc, pSSM->rc);
4596
4597 /*
4598 * Join paths with VMMR3Save.
4599 */
4600 return ssmR3SaveDoCommon(pVM, pSSM);
4601}
4602
4603
4604/**
4605 * Writes the file header and clear the per-unit data.
4606 *
4607 * @returns VBox status code.
4608 * @param pVM The VM handle.
4609 * @param pSSM The SSM handle.
4610 */
4611static int ssmR3WriteHeaderAndClearPerUnitData(PVM pVM, PSSMHANDLE pSSM)
4612{
4613 /*
4614 * Write the header.
4615 */
4616 SSMFILEHDR FileHdr;
4617 memcpy(&FileHdr.szMagic, SSMFILEHDR_MAGIC_V2_0, sizeof(FileHdr.szMagic));
4618 FileHdr.u16VerMajor = VBOX_VERSION_MAJOR;
4619 FileHdr.u16VerMinor = VBOX_VERSION_MINOR;
4620 FileHdr.u32VerBuild = VBOX_VERSION_BUILD;
4621 FileHdr.u32SvnRev = VMMGetSvnRev(),
4622 FileHdr.cHostBits = HC_ARCH_BITS;
4623 FileHdr.cbGCPhys = sizeof(RTGCPHYS);
4624 FileHdr.cbGCPtr = sizeof(RTGCPTR);
4625 FileHdr.u8Reserved = 0;
4626 FileHdr.cUnits = pVM->ssm.s.cUnits;
4627 FileHdr.fFlags = SSMFILEHDR_FLAGS_STREAM_CRC32;
4628 if (pSSM->fLiveSave)
4629 FileHdr.fFlags |= SSMFILEHDR_FLAGS_STREAM_LIVE_SAVE;
4630 FileHdr.cbMaxDecompr = RT_SIZEOFMEMB(SSMHANDLE, u.Read.abDataBuffer);
4631 FileHdr.u32CRC = 0;
4632 FileHdr.u32CRC = RTCrc32(&FileHdr, sizeof(FileHdr));
4633 int rc = ssmR3StrmWrite(&pSSM->Strm, &FileHdr, sizeof(FileHdr));
4634 if (RT_FAILURE(rc))
4635 return rc;
4636
4637 /*
4638 * Clear the per unit flags and offsets.
4639 */
4640 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4641 {
4642 pUnit->fCalled = false;
4643 pUnit->offStream = RTFOFF_MIN;
4644 }
4645
4646 return VINF_SUCCESS;
4647}
4648
4649
4650/**
4651 * Creates a new saved state file.
4652 *
4653 * @returns VBox status code.
4654 * @param pVM The VM handle.
4655 * @param pszFilename The name of the file. NULL if pStreamOps is
4656 * used.
4657 * @param pStreamOps The stream methods. NULL if pszFilename is
4658 * used.
4659 * @param pvStreamOpsUser The user argument to the stream methods.
4660 * @param enmAfter What to do afterwards.
4661 * @param pfnProgress The progress callback.
4662 * @param pvProgressUser The progress callback user argument.
4663 * @param ppSSM Where to return the pointer to the saved state
4664 * handle upon successful return. Free it using
4665 * RTMemFree after closing the stream.
4666 */
4667static int ssmR3SaveDoCreateFile(PVM pVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
4668 SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser, PSSMHANDLE *ppSSM)
4669{
4670 PSSMHANDLE pSSM = (PSSMHANDLE)RTMemAllocZ(sizeof(*pSSM));
4671 if (!pSSM)
4672 return VERR_NO_MEMORY;
4673
4674 pSSM->pVM = pVM;
4675 pSSM->enmOp = SSMSTATE_INVALID;
4676 pSSM->enmAfter = enmAfter;
4677 pSSM->fCancelled = SSMHANDLE_OK;
4678 pSSM->rc = VINF_SUCCESS;
4679 pSSM->cbUnitLeftV1 = 0;
4680 pSSM->offUnit = UINT64_MAX;
4681 pSSM->fLiveSave = false;
4682 pSSM->pfnProgress = pfnProgress;
4683 pSSM->pvUser = pvProgressUser;
4684 pSSM->uPercent = 0;
4685 pSSM->offEstProgress = 0;
4686 pSSM->cbEstTotal = 0;
4687 pSSM->offEst = 0;
4688 pSSM->offEstUnitEnd = 0;
4689 pSSM->uPercentPrepare = 0;
4690 pSSM->uPercentDone = 0;
4691 pSSM->pszFilename = pszFilename;
4692 pSSM->u.Write.offDataBuffer = 0;
4693 pSSM->u.Write.cMsMaxDowntime = UINT32_MAX;
4694
4695 int rc;
4696 if (pStreamOps)
4697 rc = ssmR3StrmInit(&pSSM->Strm, pStreamOps, pvStreamOpsUser, true /*fWrite*/, true /*fChecksummed*/, 8 /*cBuffers*/);
4698 else
4699 rc = ssmR3StrmOpenFile(&pSSM->Strm, pszFilename, true /*fWrite*/, true /*fChecksummed*/, 8 /*cBuffers*/);
4700 if (RT_FAILURE(rc))
4701 {
4702 LogRel(("SSM: Failed to create save state file '%s', rc=%Rrc.\n", pszFilename, rc));
4703 RTMemFree(pSSM);
4704 return rc;
4705 }
4706
4707 *ppSSM = pSSM;
4708 return VINF_SUCCESS;
4709}
4710
4711
4712/**
4713 * Start VM save operation.
4714 *
4715 * @returns VBox status.
4716 *
4717 * @param pVM The VM handle.
4718 * @param pszFilename Name of the file to save the state in.
4719 * @param enmAfter What is planned after a successful save operation.
4720 * @param pfnProgress Progress callback. Optional.
4721 * @param pvUser User argument for the progress callback.
4722 *
4723 * @thread EMT
4724 */
4725VMMR3DECL(int) SSMR3Save(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser)
4726{
4727 LogFlow(("SSMR3Save: pszFilename=%p:{%s} enmAfter=%d pfnProgress=%p pvUser=%p\n", pszFilename, pszFilename, enmAfter, pfnProgress, pvUser));
4728 VM_ASSERT_EMT0(pVM);
4729
4730 /*
4731 * Validate input.
4732 */
4733 AssertMsgReturn( enmAfter == SSMAFTER_DESTROY
4734 || enmAfter == SSMAFTER_CONTINUE,
4735 ("%d\n", enmAfter),
4736 VERR_INVALID_PARAMETER);
4737
4738 /*
4739 * Create the saved state file and handle.
4740 *
4741 * Note that there might be quite some work to do after executing the saving,
4742 * so we reserve 20% for the 'Done' period.
4743 */
4744 PSSMHANDLE pSSM;
4745 int rc = ssmR3SaveDoCreateFile(pVM, pszFilename, NULL /*pStreamOps*/, NULL /*pvStreamOpsUser*/,
4746 enmAfter, pfnProgress, pvUser, &pSSM);
4747 if (RT_FAILURE(rc))
4748 return rc;
4749 pSSM->uPercentPrepare = 20;
4750 pSSM->uPercentDone = 2;
4751
4752 /*
4753 * Write the saved state stream header and join paths with
4754 * the other save methods for the rest of the job.
4755 */
4756 Log(("SSM: Starting state save to file '%s'...\n", pszFilename));
4757 ssmR3StrmStartIoThread(&pSSM->Strm);
4758 rc = ssmR3WriteHeaderAndClearPerUnitData(pVM, pSSM);
4759 if (RT_SUCCESS(rc))
4760 {
4761 ssmR3SetCancellable(pVM, pSSM, true);
4762 ssmR3SaveDoCommon(pVM, pSSM);
4763 }
4764
4765 return ssmR3SaveDoClose(pVM, pSSM);
4766}
4767
4768
4769/**
4770 * Calls pfnLiveVote for all units.
4771 *
4772 * @returns VBox status code (no need to check pSSM->rc).
4773 * @retval VINF_SUCCESS if we can pass on to step 2.
4774 * @retval VINF_SSM_VOTE_FOR_ANOTHER_PASS if we need another pass.
4775 *
4776 * @param pVM The VM handle.
4777 * @param pSSM The saved state handle.
4778 * @param uPass The current pass.
4779 */
4780static int ssmR3LiveDoVoteRun(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
4781{
4782 int rcRet = VINF_SUCCESS;
4783 AssertRC(pSSM->rc);
4784 pSSM->rc = VINF_SUCCESS;
4785 pSSM->enmOp = SSMSTATE_LIVE_VOTE;
4786 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4787 {
4788 if ( pUnit->u.Common.pfnLiveVote
4789 && !pUnit->fDoneLive)
4790 {
4791 int rc;
4792 switch (pUnit->enmType)
4793 {
4794 case SSMUNITTYPE_DEV:
4795 rc = pUnit->u.Dev.pfnLiveVote(pUnit->u.Dev.pDevIns, pSSM, uPass);
4796 break;
4797 case SSMUNITTYPE_DRV:
4798 rc = pUnit->u.Drv.pfnLiveVote(pUnit->u.Drv.pDrvIns, pSSM, uPass);
4799 break;
4800 case SSMUNITTYPE_INTERNAL:
4801 rc = pUnit->u.Internal.pfnLiveVote(pVM, pSSM, uPass);
4802 break;
4803 case SSMUNITTYPE_EXTERNAL:
4804 rc = pUnit->u.External.pfnLiveVote(pSSM, pUnit->u.External.pvUser, uPass);
4805 break;
4806 default:
4807 rc = VERR_INTERNAL_ERROR;
4808 break;
4809 }
4810 pUnit->fCalled = true;
4811 Assert(pSSM->rc == VINF_SUCCESS);
4812 if (rc != VINF_SUCCESS)
4813 {
4814 if (rc == VINF_SSM_VOTE_FOR_ANOTHER_PASS)
4815 {
4816 Log(("ssmR3DoLiveVoteRun: '%s'/#%u -> VINF_SSM_VOTE_FOR_ANOTHER_PASS (pass=%u)\n", pUnit->szName, pUnit->u32Instance, uPass));
4817 rcRet = VINF_SSM_VOTE_FOR_ANOTHER_PASS;
4818 }
4819 else if (rc == VINF_SSM_VOTE_DONE_DONT_CALL_AGAIN)
4820 {
4821 pUnit->fDoneLive = true;
4822 Log(("ssmR3DoLiveVoteRun: '%s'/#%u -> VINF_SSM_VOTE_DONE_DONT_CALL_AGAIN (pass=%u)\n", pUnit->szName, pUnit->u32Instance, uPass));
4823 }
4824 else
4825 {
4826 /*
4827 * rc is usually VERR_SSM_VOTE_FOR_GIVING_UP here, but we allow
4828 * other status codes for better user feed back. However, no
4829 * other non-error status is allowed.
4830 */
4831 LogRel(("SSM: Error - '%s'/#%u voted %Rrc! (pass=%u)\n", pUnit->szName, pUnit->u32Instance, rc, uPass));
4832 AssertMsgReturn(RT_FAILURE(rc), ("%Rrc; '%s'\n", rc, pUnit->szName), pSSM->rc = VERR_IPE_UNEXPECTED_INFO_STATUS);
4833 return pSSM->rc = rc;
4834 }
4835 }
4836 }
4837 }
4838 if (rcRet == VINF_SUCCESS)
4839 LogRel(("SSM: Step 1 completed after pass %u.\n", uPass));
4840 return rcRet;
4841}
4842
4843
4844/**
4845 * Calls pfnLiveExec for all units.
4846 *
4847 * @returns VBox status code (no need to check pSSM->rc).
4848 *
4849 * @param pVM The VM handle.
4850 * @param pSSM The saved state handle.
4851 * @param uPass The current pass.
4852 */
4853static int ssmR3LiveDoExecRun(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
4854{
4855 AssertRC(pSSM->rc);
4856 pSSM->rc = VINF_SUCCESS;
4857 pSSM->enmOp = SSMSTATE_LIVE_EXEC;
4858 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4859 {
4860 /*
4861 * Skip units without a callback (this is most).
4862 */
4863 if ( !pUnit->u.Common.pfnLiveExec
4864 || pUnit->fDoneLive)
4865 continue;
4866 pUnit->offStream = ssmR3StrmTell(&pSSM->Strm);
4867
4868 /*
4869 * Check for cancellation.
4870 */
4871 if (RT_UNLIKELY(ASMAtomicUoReadU32(&(pSSM)->fCancelled) == SSMHANDLE_CANCELLED))
4872 {
4873 LogRel(("SSM: Cancelled!\n"));
4874 AssertRC(pSSM->rc);
4875 return pSSM->rc = VERR_SSM_CANCELLED;
4876 }
4877
4878 /*
4879 * Write data unit header.
4880 */
4881 SSMFILEUNITHDRV2 UnitHdr;
4882 memcpy(&UnitHdr.szMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(UnitHdr.szMagic));
4883 UnitHdr.offStream = pUnit->offStream;
4884 UnitHdr.u32CurStreamCRC = ssmR3StrmCurCRC(&pSSM->Strm);
4885 UnitHdr.u32CRC = 0;
4886 UnitHdr.u32Version = pUnit->u32Version;
4887 UnitHdr.u32Instance = pUnit->u32Instance;
4888 UnitHdr.u32Pass = uPass;
4889 UnitHdr.fFlags = 0;
4890 UnitHdr.cbName = (uint32_t)pUnit->cchName + 1;
4891 memcpy(&UnitHdr.szName[0], &pUnit->szName[0], UnitHdr.cbName);
4892 UnitHdr.u32CRC = RTCrc32(&UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]));
4893 Log(("SSM: Unit at %#9llx: '%s', instance %u, pass %#x, version %u\n",
4894 UnitHdr.offStream, UnitHdr.szName, UnitHdr.u32Instance, UnitHdr.u32Pass, UnitHdr.u32Version));
4895 int rc = ssmR3StrmWrite(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]));
4896 if (RT_FAILURE(rc))
4897 {
4898 LogRel(("SSM: Failed to write unit header. rc=%Rrc\n", rc));
4899 return pSSM->rc = rc;
4900 }
4901
4902 /*
4903 * Call the execute handler.
4904 */
4905 ssmR3DataWriteBegin(pSSM);
4906 switch (pUnit->enmType)
4907 {
4908 case SSMUNITTYPE_DEV:
4909 rc = pUnit->u.Dev.pfnLiveExec(pUnit->u.Dev.pDevIns, pSSM, uPass);
4910 break;
4911 case SSMUNITTYPE_DRV:
4912 rc = pUnit->u.Drv.pfnLiveExec(pUnit->u.Drv.pDrvIns, pSSM, uPass);
4913 break;
4914 case SSMUNITTYPE_INTERNAL:
4915 rc = pUnit->u.Internal.pfnLiveExec(pVM, pSSM, uPass);
4916 break;
4917 case SSMUNITTYPE_EXTERNAL:
4918 rc = pUnit->u.External.pfnLiveExec(pSSM, pUnit->u.External.pvUser, uPass);
4919 break;
4920 default:
4921 rc = VERR_INTERNAL_ERROR;
4922 break;
4923 }
4924 pUnit->fCalled = true;
4925 if (RT_FAILURE(rc) && RT_SUCCESS_NP(pSSM->rc))
4926 pSSM->rc = rc;
4927 else
4928 {
4929 if (rc == VINF_SSM_DONT_CALL_AGAIN)
4930 pUnit->fDoneLive = true;
4931 rc = ssmR3DataFlushBuffer(pSSM); /* will return SSMHANDLE::rc if it is set */
4932 }
4933 if (RT_FAILURE(rc))
4934 {
4935 LogRel(("SSM: Execute save failed with rc=%Rrc for data unit '%s'/#%u.\n", rc, pUnit->szName, pUnit->u32Instance));
4936 if (RT_SUCCESS(pSSM->rc))
4937 pSSM->rc = rc;
4938 return rc;
4939 }
4940
4941 /*
4942 * Write the termination record and flush the compression stream.
4943 */
4944 SSMRECTERM TermRec;
4945 TermRec.u8TypeAndFlags = SSM_REC_FLAGS_FIXED | SSM_REC_FLAGS_IMPORTANT | SSM_REC_TYPE_TERM;
4946 TermRec.cbRec = sizeof(TermRec) - 2;
4947 if (pSSM->Strm.fChecksummed)
4948 {
4949 TermRec.fFlags = SSMRECTERM_FLAGS_CRC32;
4950 TermRec.u32StreamCRC = RTCrc32Finish(RTCrc32Process(ssmR3StrmCurCRC(&pSSM->Strm), &TermRec, 2));
4951 }
4952 else
4953 {
4954 TermRec.fFlags = 0;
4955 TermRec.u32StreamCRC = 0;
4956 }
4957 TermRec.cbUnit = pSSM->offUnit + sizeof(TermRec);
4958 rc = ssmR3DataWriteRaw(pSSM, &TermRec, sizeof(TermRec));
4959 if (RT_SUCCESS(rc))
4960 rc = ssmR3DataWriteFinish(pSSM);
4961 if (RT_FAILURE(rc))
4962 {
4963 LogRel(("SSM: Failed terminating unit: %Rrc (pass=%u)\n", rc, uPass));
4964 return pSSM->rc = rc;
4965 }
4966 } /* for each unit */
4967
4968 return VINF_SUCCESS;
4969}
4970
4971
4972/**
4973 * Implements the live exec+vote loop.
4974 *
4975 * @returns VBox status code (no need to check pSSM->rc).
4976 * @param pVM The VM handle.
4977 * @param pSSM The saved state handle.
4978 */
4979static int ssmR3DoLiveExecVoteLoop(PVM pVM, PSSMHANDLE pSSM)
4980{
4981 /*
4982 * Calc the max saved state size before we should give up because of insane
4983 * amounts of data.
4984 */
4985#define SSM_MAX_GROWTH_FILE 10000
4986#define SSM_MAX_GROWTH_REMOTE 100000
4987 uint64_t cbSum = 0;
4988 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
4989 cbSum += pUnit->cbGuess;
4990 uint64_t cbMax = cbSum * (pSSM->pszFilename ? SSM_MAX_GROWTH_FILE : SSM_MAX_GROWTH_REMOTE);
4991 AssertLogRelMsgReturn(cbMax > cbSum, ("cbMax=%#RX64, cbSum=%#RX64\n", cbMax, cbSum), pSSM->rc = VERR_OUT_OF_RANGE);
4992 if (cbMax < _1G)
4993 cbMax = _1G;
4994
4995 /*
4996 * The pass loop.
4997 *
4998 * The number of interations is restricted for two reasons, first
4999 * to make sure
5000 */
5001#define SSM_MAX_PASSES _1M
5002 for (uint32_t uPass = 0; uPass < SSM_MAX_PASSES; uPass++)
5003 {
5004 pVM->ssm.s.uPass = uPass;
5005
5006 /*
5007 * Save state and vote on whether we need more passes or not.
5008 */
5009 int rc = ssmR3LiveDoExecRun(pVM, pSSM, uPass);
5010 if (RT_FAILURE(rc))
5011 return rc;
5012 rc = ssmR3LiveDoVoteRun(pVM, pSSM, uPass);
5013 if (rc == VINF_SUCCESS)
5014 {
5015 pSSM->enmOp = SSMSTATE_LIVE_STEP2;
5016 return VINF_SUCCESS;
5017 }
5018 if (RT_FAILURE(rc))
5019 return rc;
5020
5021 /*
5022 * Check that we're still within sane data amounts.
5023 */
5024 uint64_t cbSaved = ssmR3StrmTell(&pSSM->Strm);
5025 if (cbSaved > cbMax)
5026 {
5027 LogRel(("SSM: Giving up: Exceeded max state size. (cbSaved=%#RX64, cbMax=%#RX64)\n", cbSaved, cbMax));
5028 return pSSM->rc = VERR_SSM_STATE_GREW_TOO_BIG;
5029 }
5030
5031 /*
5032 * Check that the stream is still OK.
5033 */
5034 rc = ssmR3StrmCheckAndFlush(&pSSM->Strm);
5035 if (RT_FAILURE(rc))
5036 return pSSM->rc = rc;
5037 }
5038
5039 LogRel(("SSM: Giving up: Too many passes! (%u)\n", SSM_MAX_PASSES));
5040 return pSSM->rc = VERR_SSM_TOO_MANY_PASSES;
5041}
5042
5043
5044/**
5045 * Calls pfnLivePrep for all units.
5046 *
5047 * @returns VBox status code (no need to check pSSM->rc).
5048 * @param pVM The VM handle.
5049 * @param pSSM The saved state handle.
5050 */
5051static int ssmR3DoLivePrepRun(PVM pVM, PSSMHANDLE pSSM)
5052{
5053 /*
5054 * Do the prepare run.
5055 */
5056 pSSM->rc = VINF_SUCCESS;
5057 pSSM->enmOp = SSMSTATE_SAVE_PREP;
5058 for (PSSMUNIT pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
5059 {
5060 if (pUnit->u.Common.pfnLivePrep)
5061 {
5062 int rc;
5063 switch (pUnit->enmType)
5064 {
5065 case SSMUNITTYPE_DEV:
5066 rc = pUnit->u.Dev.pfnLivePrep(pUnit->u.Dev.pDevIns, pSSM);
5067 break;
5068 case SSMUNITTYPE_DRV:
5069 rc = pUnit->u.Drv.pfnLivePrep(pUnit->u.Drv.pDrvIns, pSSM);
5070 break;
5071 case SSMUNITTYPE_INTERNAL:
5072 rc = pUnit->u.Internal.pfnLivePrep(pVM, pSSM);
5073 break;
5074 case SSMUNITTYPE_EXTERNAL:
5075 rc = pUnit->u.External.pfnLivePrep(pSSM, pUnit->u.External.pvUser);
5076 break;
5077 default:
5078 rc = VERR_INTERNAL_ERROR;
5079 break;
5080 }
5081 pUnit->fCalled = true;
5082 if (RT_FAILURE(rc) && RT_SUCCESS_NP(pSSM->rc))
5083 pSSM->rc = rc;
5084 else
5085 rc = pSSM->rc;
5086 if (RT_FAILURE(rc))
5087 {
5088 LogRel(("SSM: Prepare save failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
5089 return rc;
5090 }
5091 }
5092
5093 pSSM->cbEstTotal += pUnit->cbGuess;
5094 }
5095
5096 /*
5097 * Work the progress indicator if we got one.
5098 */
5099 if (pSSM->pfnProgress)
5100 pSSM->pfnProgress(pVM, 2, pSSM->pvUser);
5101 pSSM->uPercent = 2;
5102
5103 return VINF_SUCCESS;
5104}
5105
5106
5107/**
5108 * Continue a live state saving operation on the worker thread.
5109 *
5110 * @returns VBox status.
5111 *
5112 * @param pSSM The SSM handle returned by SSMR3LiveSave.
5113 *
5114 * @thread Non-EMT thread. Will involve the EMT at the end of the operation.
5115 */
5116VMMR3_INT_DECL(int) SSMR3LiveDoStep1(PSSMHANDLE pSSM)
5117{
5118 LogFlow(("SSMR3LiveDoStep1: pSSM=%p\n", pSSM));
5119
5120 /*
5121 * Validate input.
5122 */
5123 AssertPtrReturn(pSSM, VERR_INVALID_POINTER);
5124 PVM pVM = pSSM->pVM;
5125 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
5126 VM_ASSERT_OTHER_THREAD(pVM);
5127 AssertMsgReturn( pSSM->enmAfter == SSMAFTER_DESTROY
5128 || pSSM->enmAfter == SSMAFTER_CONTINUE
5129 || pSSM->enmAfter == SSMAFTER_TELEPORT,
5130 ("%d\n", pSSM->enmAfter),
5131 VERR_INVALID_PARAMETER);
5132 AssertMsgReturn(pSSM->enmOp == SSMSTATE_LIVE_STEP1, ("%d\n", pSSM->enmOp), VERR_INVALID_STATE);
5133 AssertRCReturn(pSSM->rc, pSSM->rc);
5134
5135 /*
5136 * Do the prep run, then the exec+vote cycle.
5137 */
5138 int rc = ssmR3DoLivePrepRun(pVM, pSSM);
5139 if (RT_SUCCESS(rc))
5140 rc = ssmR3DoLiveExecVoteLoop(pVM, pSSM);
5141 return rc;
5142}
5143
5144
5145/**
5146 * Start saving the live state.
5147 *
5148 * Call SSMR3LiveDoStep1, SSMR3LiveDoStep2 and finally SSMR3LiveDone on success.
5149 * SSMR3LiveDone should be called even if SSMR3LiveDoStep1 or SSMR3LiveDoStep2
5150 * fails.
5151 *
5152 * @returns VBox status.
5153 *
5154 * @param pVM The VM handle.
5155 * @param cMsMaxDowntime The maximum downtime given as milliseconds.
5156 * @param pszFilename Name of the file to save the state in. This string
5157 * must remain valid until SSMR3LiveDone is called.
5158 * Must be NULL if pStreamOps is used.
5159 * @param pStreamOps The stream method table. NULL if pszFilename is
5160 * used.
5161 * @param pvStreamOpsUser The user argument to the stream methods.
5162 * @param enmAfter What is planned after a successful save operation.
5163 * @param pfnProgress Progress callback. Optional.
5164 * @param pvProgressUser User argument for the progress callback.
5165 *
5166 * @thread EMT0
5167 */
5168VMMR3_INT_DECL(int) SSMR3LiveSave(PVM pVM, uint32_t cMsMaxDowntime,
5169 const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
5170 SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser,
5171 PSSMHANDLE *ppSSM)
5172{
5173 LogFlow(("SSMR3LiveSave: cMsMaxDowntime=%u pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p enmAfter=%d pfnProgress=%p pvProgressUser=%p\n",
5174 cMsMaxDowntime, pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, enmAfter, pfnProgress, pvProgressUser));
5175 VM_ASSERT_EMT0(pVM);
5176
5177 /*
5178 * Validate input.
5179 */
5180 AssertMsgReturn( enmAfter == SSMAFTER_DESTROY
5181 || enmAfter == SSMAFTER_CONTINUE
5182 || enmAfter == SSMAFTER_TELEPORT,
5183 ("%d\n", enmAfter),
5184 VERR_INVALID_PARAMETER);
5185 AssertReturn(!pszFilename != !pStreamOps, VERR_INVALID_PARAMETER);
5186 if (pStreamOps)
5187 {
5188 AssertReturn(pStreamOps->u32Version == SSMSTRMOPS_VERSION, VERR_INVALID_MAGIC);
5189 AssertReturn(pStreamOps->u32EndVersion == SSMSTRMOPS_VERSION, VERR_INVALID_MAGIC);
5190 AssertReturn(pStreamOps->pfnWrite, VERR_INVALID_PARAMETER);
5191 AssertReturn(pStreamOps->pfnRead, VERR_INVALID_PARAMETER);
5192 AssertReturn(pStreamOps->pfnSeek, VERR_INVALID_PARAMETER);
5193 AssertReturn(pStreamOps->pfnTell, VERR_INVALID_PARAMETER);
5194 AssertReturn(pStreamOps->pfnSize, VERR_INVALID_PARAMETER);
5195 AssertReturn(pStreamOps->pfnClose, VERR_INVALID_PARAMETER);
5196 }
5197
5198 /*
5199 * Create the saved state file and handle.
5200 *
5201 * Note that there might be quite some work to do after executing the saving,
5202 * so we reserve 20% for the 'Done' period.
5203 */
5204 PSSMHANDLE pSSM;
5205 int rc = ssmR3SaveDoCreateFile(pVM, pszFilename, pStreamOps, pvStreamOpsUser,
5206 enmAfter, pfnProgress, pvProgressUser, &pSSM);
5207 if (RT_FAILURE(rc))
5208 return rc;
5209 pSSM->uPercentPrepare = 20; /** @todo fix these. */
5210 pSSM->uPercentDone = 2;
5211 pSSM->fLiveSave = true;
5212 pSSM->u.Write.cMsMaxDowntime = cMsMaxDowntime;
5213
5214 /*
5215 * Write the saved state stream header and do the prep run for live saving.
5216 */
5217 Log(("SSM: Starting state save to file '%s'...\n", pszFilename));
5218 ssmR3StrmStartIoThread(&pSSM->Strm);
5219 rc = ssmR3WriteHeaderAndClearPerUnitData(pVM, pSSM);
5220 if (RT_SUCCESS(rc))
5221 {
5222 /*
5223 * Return and let the requstor thread do the pfnLiveExec/Vote part
5224 * via SSMR3SaveFinishLive
5225 */
5226 pSSM->enmOp = SSMSTATE_LIVE_STEP1;
5227 ssmR3SetCancellable(pVM, pSSM, true);
5228 *ppSSM = pSSM;
5229 return VINF_SUCCESS;
5230 }
5231 /* bail out. */
5232 int rc2 = ssmR3StrmClose(&pSSM->Strm, pSSM->rc == VERR_SSM_CANCELLED);
5233 RTMemFree(pSSM);
5234 rc2 = RTFileDelete(pszFilename);
5235 AssertRC(rc2);
5236 return rc;
5237}
5238
5239#endif /* !SSM_STANDALONE */
5240
5241
5242/* ... Loading and reading starts here ... */
5243/* ... Loading and reading starts here ... */
5244/* ... Loading and reading starts here ... */
5245/* ... Loading and reading starts here ... */
5246/* ... Loading and reading starts here ... */
5247/* ... Loading and reading starts here ... */
5248/* ... Loading and reading starts here ... */
5249/* ... Loading and reading starts here ... */
5250/* ... Loading and reading starts here ... */
5251/* ... Loading and reading starts here ... */
5252/* ... Loading and reading starts here ... */
5253/* ... Loading and reading starts here ... */
5254/* ... Loading and reading starts here ... */
5255/* ... Loading and reading starts here ... */
5256/* ... Loading and reading starts here ... */
5257/* ... Loading and reading starts here ... */
5258/* ... Loading and reading starts here ... */
5259
5260
5261/**
5262 * Closes the decompressor of a data unit.
5263 *
5264 * @returns pSSM->rc.
5265 * @param pSSM The saved state handle.
5266 */
5267static int ssmR3DataReadFinishV1(PSSMHANDLE pSSM)
5268{
5269 if (pSSM->u.Read.pZipDecompV1)
5270 {
5271 int rc = RTZipDecompDestroy(pSSM->u.Read.pZipDecompV1);
5272 AssertRC(rc);
5273 pSSM->u.Read.pZipDecompV1 = NULL;
5274 }
5275 return pSSM->rc;
5276}
5277
5278
5279/**
5280 * Callback for reading compressed data into the input buffer of the
5281 * decompressor, for saved file format version 1.
5282 *
5283 * @returns VBox status code. Set pSSM->rc on error.
5284 * @param pvSSM The SSM handle.
5285 * @param pvBuf Where to store the compressed data.
5286 * @param cbBuf Size of the buffer.
5287 * @param pcbRead Number of bytes actually stored in the buffer.
5288 */
5289static DECLCALLBACK(int) ssmR3ReadInV1(void *pvSSM, void *pvBuf, size_t cbBuf, size_t *pcbRead)
5290{
5291 PSSMHANDLE pSSM = (PSSMHANDLE)pvSSM;
5292 size_t cbRead = cbBuf;
5293 if (pSSM->cbUnitLeftV1 < cbBuf)
5294 cbRead = (size_t)pSSM->cbUnitLeftV1;
5295 if (cbRead)
5296 {
5297 //Log2(("ssmR3ReadInV1: %#010llx cbBug=%#x cbRead=%#x\n", ssmR3StrmTell(&pSSM->Strm), cbBuf, cbRead));
5298 int rc = ssmR3StrmRead(&pSSM->Strm, pvBuf, cbRead);
5299 if (RT_SUCCESS(rc))
5300 {
5301 pSSM->cbUnitLeftV1 -= cbRead;
5302 if (pcbRead)
5303 *pcbRead = cbRead;
5304 ssmR3Progress(pSSM, cbRead);
5305 return VINF_SUCCESS;
5306 }
5307 return pSSM->rc = rc;
5308 }
5309
5310 if (pSSM->enmAfter != SSMAFTER_DEBUG_IT)
5311 AssertMsgFailed(("SSM: attempted reading more than the unit!\n"));
5312 return pSSM->rc = VERR_SSM_LOADED_TOO_MUCH;
5313}
5314
5315
5316/**
5317 * Internal read worker for reading data from a version 1 unit.
5318 *
5319 * @returns VBox status code, pSSM->rc is set on error.
5320 *
5321 * @param pSSM The saved state handle.
5322 * @param pvBuf Where to store the read data.
5323 * @param cbBuf Number of bytes to read.
5324 */
5325static int ssmR3DataReadV1(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf)
5326{
5327 /*
5328 * Open the decompressor on the first read.
5329 */
5330 if (!pSSM->u.Read.pZipDecompV1)
5331 {
5332 pSSM->rc = RTZipDecompCreate(&pSSM->u.Read.pZipDecompV1, pSSM, ssmR3ReadInV1);
5333 if (RT_FAILURE(pSSM->rc))
5334 return pSSM->rc;
5335 }
5336
5337 /*
5338 * Do the requested read.
5339 */
5340 int rc = pSSM->rc = RTZipDecompress(pSSM->u.Read.pZipDecompV1, pvBuf, cbBuf, NULL);
5341 if (RT_SUCCESS(rc))
5342 {
5343 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 ? "..." : ""));
5344 pSSM->offUnit += cbBuf;
5345 return VINF_SUCCESS;
5346 }
5347 AssertMsgFailed(("rc=%Rrc cbBuf=%#x\n", rc, cbBuf));
5348 return rc;
5349}
5350
5351
5352/**
5353 * Creates the decompressor for the data unit.
5354 *
5355 * pSSM->rc will be set on error.
5356 *
5357 * @param pSSM The saved state handle.
5358 */
5359static void ssmR3DataReadBeginV2(PSSMHANDLE pSSM)
5360{
5361 Assert(!pSSM->u.Read.cbDataBuffer || pSSM->u.Read.cbDataBuffer == pSSM->u.Read.offDataBuffer);
5362 Assert(!pSSM->u.Read.cbRecLeft);
5363
5364 pSSM->offUnit = 0;
5365 pSSM->u.Read.cbRecLeft = 0;
5366 pSSM->u.Read.cbDataBuffer = 0;
5367 pSSM->u.Read.offDataBuffer = 0;
5368 pSSM->u.Read.fEndOfData = false;
5369 pSSM->u.Read.u8TypeAndFlags = 0;
5370}
5371
5372
5373/**
5374 * Checks for the termination record and closes the decompressor.
5375 *
5376 * pSSM->rc will be set on error.
5377 *
5378 * @returns pSSM->rc.
5379 * @param pSSM The saved state handle.
5380 */
5381static int ssmR3DataReadFinishV2(PSSMHANDLE pSSM)
5382{
5383 /*
5384 * If we haven't encountered the end of the record, it must be the next one.
5385 */
5386 int rc = pSSM->rc;
5387 if ( !pSSM->u.Read.fEndOfData
5388 && RT_SUCCESS(rc))
5389 {
5390 rc = ssmR3DataReadRecHdrV2(pSSM);
5391 if ( RT_SUCCESS(rc)
5392 && !pSSM->u.Read.fEndOfData)
5393 {
5394 rc = VERR_SSM_LOADED_TOO_LITTLE;
5395 AssertFailed();
5396 }
5397 pSSM->rc = rc;
5398 }
5399 return rc;
5400}
5401
5402
5403/**
5404 * Read reader that keep works the progress indicator and unit offset.
5405 *
5406 * @returns VBox status code. Does NOT set pSSM->rc.
5407 * @param pSSM The saved state handle.
5408 * @param pvBuf Where to put the bits
5409 * @param cbBuf How many bytes to read.
5410 */
5411DECLINLINE(int) ssmR3DataReadV2Raw(PSSMHANDLE pSSM, void *pvBuf, size_t cbToRead)
5412{
5413 int rc = ssmR3StrmRead(&pSSM->Strm, pvBuf, cbToRead);
5414 if (RT_SUCCESS(rc))
5415 {
5416 pSSM->offUnit += cbToRead;
5417 ssmR3Progress(pSSM, cbToRead);
5418 return VINF_SUCCESS;
5419 }
5420
5421 if (rc == VERR_SSM_CANCELLED)
5422 return rc;
5423
5424 if (pSSM->enmAfter != SSMAFTER_DEBUG_IT && rc == VERR_EOF)
5425 AssertMsgFailedReturn(("SSM: attempted reading more than the unit! rc=%Rrc\n", rc), VERR_SSM_LOADED_TOO_MUCH);
5426 return VERR_SSM_STREAM_ERROR;
5427}
5428
5429
5430/**
5431 * Reads and checks the LZF "header".
5432 *
5433 * @returns VBox status code. Sets pSSM->rc on error.
5434 * @param pSSM The saved state handle..
5435 * @param pcbDecompr Where to store the size of the decompressed data.
5436 */
5437DECLINLINE(int) ssmR3DataReadV2RawLzfHdr(PSSMHANDLE pSSM, uint32_t *pcbDecompr)
5438{
5439 *pcbDecompr = 0; /* shuts up gcc. */
5440 AssertLogRelMsgReturn( pSSM->u.Read.cbRecLeft > 1
5441 && pSSM->u.Read.cbRecLeft <= RT_SIZEOFMEMB(SSMHANDLE, u.Read.abComprBuffer) + 2,
5442 ("%#x\n", pSSM->u.Read.cbRecLeft),
5443 VERR_SSM_INTEGRITY_DECOMPRESSION);
5444
5445 uint8_t cKB;
5446 int rc = ssmR3DataReadV2Raw(pSSM, &cKB, 1);
5447 if (RT_FAILURE(rc))
5448 return pSSM->rc = rc;
5449 pSSM->u.Read.cbRecLeft -= sizeof(cKB);
5450
5451 uint32_t cbDecompr = (uint32_t)cKB * _1K;
5452 AssertLogRelMsgReturn( cbDecompr >= pSSM->u.Read.cbRecLeft
5453 && cbDecompr <= RT_SIZEOFMEMB(SSMHANDLE, u.Read.abDataBuffer),
5454 ("%#x\n", cbDecompr),
5455 VERR_SSM_INTEGRITY_DECOMPRESSION);
5456
5457 *pcbDecompr = cbDecompr;
5458 return VINF_SUCCESS;
5459}
5460
5461
5462/**
5463 * Reads an LZF block from the stream and decompresses into the specified
5464 * buffer.
5465 *
5466 * @returns VBox status code. Sets pSSM->rc on error.
5467 * @param SSM The saved state handle.
5468 * @param pvDst Pointer to the output buffer.
5469 * @param cbDecompr The size of the decompressed data.
5470 */
5471static int ssmR3DataReadV2RawLzf(PSSMHANDLE pSSM, void *pvDst, size_t cbDecompr)
5472{
5473 int rc;
5474 uint32_t cbCompr = pSSM->u.Read.cbRecLeft;
5475 pSSM->u.Read.cbRecLeft = 0;
5476
5477 /*
5478 * Try use the stream buffer directly to avoid copying things around.
5479 */
5480 uint8_t const *pb = ssmR3StrmReadDirect(&pSSM->Strm, cbCompr);
5481 if (pb)
5482 {
5483 pSSM->offUnit += cbCompr;
5484 ssmR3Progress(pSSM, cbCompr);
5485 }
5486 else
5487 {
5488 rc = ssmR3DataReadV2Raw(pSSM, &pSSM->u.Read.abComprBuffer[0], cbCompr);
5489 if (RT_FAILURE(rc))
5490 return pSSM->rc = rc;
5491 pb = &pSSM->u.Read.abComprBuffer[0];
5492 }
5493
5494 /*
5495 * Decompress it.
5496 */
5497 size_t cbDstActual;
5498 rc = RTZipBlockDecompress(RTZIPTYPE_LZF, 0 /*fFlags*/,
5499 pb, cbCompr, NULL /*pcbSrcActual*/,
5500 pvDst, cbDecompr, &cbDstActual);
5501 if (RT_SUCCESS(rc))
5502 {
5503 AssertLogRelMsgReturn(cbDstActual == cbDecompr, ("%#x %#x\n", cbDstActual, cbDecompr), VERR_SSM_INTEGRITY_DECOMPRESSION);
5504 return VINF_SUCCESS;
5505 }
5506
5507 AssertLogRelMsgFailed(("cbCompr=%#x cbDecompr=%#x rc=%Rrc\n", cbCompr, cbDecompr, rc));
5508 return pSSM->rc = VERR_SSM_INTEGRITY_DECOMPRESSION;
5509}
5510
5511
5512/**
5513 * Reads and checks the raw zero "header".
5514 *
5515 * @returns VBox status code. Sets pSSM->rc on error.
5516 * @param pSSM The saved state handle..
5517 * @param pcbDecompr Where to store the size of the zero data.
5518 */
5519DECLINLINE(int) ssmR3DataReadV2RawZeroHdr(PSSMHANDLE pSSM, uint32_t *pcbZero)
5520{
5521 *pcbZero = 0; /* shuts up gcc. */
5522 AssertLogRelMsgReturn(pSSM->u.Read.cbRecLeft == 1, ("%#x\n", pSSM->u.Read.cbRecLeft), VERR_SSM_INTEGRITY_DECOMPRESSION);
5523
5524 uint8_t cKB;
5525 int rc = ssmR3DataReadV2Raw(pSSM, &cKB, 1);
5526 if (RT_FAILURE(rc))
5527 return pSSM->rc = rc;
5528 pSSM->u.Read.cbRecLeft = 0;
5529
5530 uint32_t cbZero = (uint32_t)cKB * _1K;
5531 AssertLogRelMsgReturn(cbZero <= RT_SIZEOFMEMB(SSMHANDLE, u.Read.abDataBuffer),
5532 ("%#x\n", cbZero), VERR_SSM_INTEGRITY_DECOMPRESSION);
5533
5534 *pcbZero = cbZero;
5535 return VINF_SUCCESS;
5536}
5537
5538
5539/**
5540 * Worker for reading the record header.
5541 *
5542 * It sets pSSM->u.Read.cbRecLeft, pSSM->u.Read.u8TypeAndFlags and
5543 * pSSM->u.Read.fEndOfData. When a termination record is encounter, it will be
5544 * read in full and validated, the fEndOfData indicator is set, and VINF_SUCCESS
5545 * is returned.
5546 *
5547 * @returns VBox status code. Does not set pSSM->rc.
5548 * @param pSSM The saved state handle.
5549 */
5550static int ssmR3DataReadRecHdrV2(PSSMHANDLE pSSM)
5551{
5552 AssertLogRelReturn(!pSSM->u.Read.fEndOfData, VERR_SSM_LOADED_TOO_MUCH);
5553
5554 /*
5555 * Read the two mandatory bytes.
5556 */
5557 uint8_t abHdr[8];
5558 int rc = ssmR3DataReadV2Raw(pSSM, abHdr, 2);
5559 if (RT_FAILURE(rc))
5560 return rc;
5561
5562 /*
5563 * Validate the first byte and check for the termination records.
5564 */
5565 pSSM->u.Read.u8TypeAndFlags = abHdr[0];
5566 AssertLogRelMsgReturn(SSM_REC_ARE_TYPE_AND_FLAGS_VALID(abHdr[0]), ("%#x %#x\n", abHdr[0], abHdr[1]), VERR_SSM_INTEGRITY_REC_HDR);
5567 if ((abHdr[0] & SSM_REC_TYPE_MASK) == SSM_REC_TYPE_TERM)
5568 {
5569 pSSM->u.Read.cbRecLeft = 0;
5570 pSSM->u.Read.fEndOfData = true;
5571 AssertLogRelMsgReturn(abHdr[1] == sizeof(SSMRECTERM) - 2, ("%#x\n", abHdr[1]), VERR_SSM_INTEGRITY_REC_TERM);
5572 AssertLogRelMsgReturn(abHdr[0] & SSM_REC_FLAGS_IMPORTANT, ("%#x\n", abHdr[0]), VERR_SSM_INTEGRITY_REC_TERM);
5573
5574 /* get the rest */
5575 uint32_t u32StreamCRC = ssmR3StrmFinalCRC(&pSSM->Strm);
5576 SSMRECTERM TermRec;
5577 rc = ssmR3DataReadV2Raw(pSSM, (uint8_t *)&TermRec + 2, sizeof(SSMRECTERM) - 2);
5578 if (RT_FAILURE(rc))
5579 return rc;
5580
5581 /* validate integrity */
5582 AssertLogRelMsgReturn(TermRec.cbUnit == pSSM->offUnit,
5583 ("cbUnit=%#llx offUnit=%#llx\n", TermRec.cbUnit, pSSM->offUnit),
5584 VERR_SSM_INTEGRITY_REC_TERM);
5585 AssertLogRelMsgReturn(!(TermRec.fFlags & ~SSMRECTERM_FLAGS_CRC32), ("%#x\n", TermRec.fFlags), VERR_SSM_INTEGRITY_REC_TERM);
5586 if (!(TermRec.fFlags & SSMRECTERM_FLAGS_CRC32))
5587 AssertLogRelMsgReturn(TermRec.u32StreamCRC == 0, ("%#x\n", TermRec.u32StreamCRC), VERR_SSM_INTEGRITY_REC_TERM);
5588 else if (pSSM->Strm.fChecksummed)
5589 AssertLogRelMsgReturn(TermRec.u32StreamCRC == u32StreamCRC, ("%#x, %#x\n", TermRec.u32StreamCRC, u32StreamCRC),
5590 VERR_SSM_INTEGRITY_REC_TERM_CRC);
5591
5592 Log3(("ssmR3DataReadRecHdrV2: %08llx|%08llx: TERM\n", ssmR3StrmTell(&pSSM->Strm) - sizeof(SSMRECTERM), pSSM->offUnit));
5593 return VINF_SUCCESS;
5594 }
5595
5596 /*
5597 * Figure the size. The 2nd byte is encoded in UTF-8 fashion, so this
5598 * is can be highly enjoyable.
5599 */
5600 uint32_t cbHdr = 2;
5601 uint32_t cb = abHdr[1];
5602 if (!(cb & 0x80))
5603 pSSM->u.Read.cbRecLeft = cb;
5604 else
5605 {
5606 /*
5607 * Need more data. Figure how much and read it.
5608 */
5609 if (!(cb & RT_BIT(5)))
5610 cb = 2;
5611 else if (!(cb & RT_BIT(4)))
5612 cb = 3;
5613 else if (!(cb & RT_BIT(3)))
5614 cb = 4;
5615 else if (!(cb & RT_BIT(2)))
5616 cb = 5;
5617 else if (!(cb & RT_BIT(1)))
5618 cb = 6;
5619 else
5620 AssertLogRelMsgFailedReturn(("Invalid record size byte: %#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
5621 cbHdr = cb + 1;
5622
5623 rc = ssmR3DataReadV2Raw(pSSM, &abHdr[2], cb - 1);
5624 if (RT_FAILURE(rc))
5625 return rc;
5626
5627 /*
5628 * Validate what we've read.
5629 */
5630 switch (cb)
5631 {
5632 case 6:
5633 AssertLogRelMsgReturn((abHdr[6] & 0xc0) == 0x80, ("6/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
5634 case 5:
5635 AssertLogRelMsgReturn((abHdr[5] & 0xc0) == 0x80, ("5/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
5636 case 4:
5637 AssertLogRelMsgReturn((abHdr[4] & 0xc0) == 0x80, ("4/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
5638 case 3:
5639 AssertLogRelMsgReturn((abHdr[3] & 0xc0) == 0x80, ("3/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
5640 case 2:
5641 AssertLogRelMsgReturn((abHdr[2] & 0xc0) == 0x80, ("2/%u: %.*Rhxs\n", cb, cb + 1, &abHdr[0]), VERR_SSM_INTEGRITY_REC_HDR);
5642 break;
5643 default:
5644 return VERR_INTERNAL_ERROR;
5645 }
5646
5647 /*
5648 * Decode it and validate the range.
5649 */
5650 switch (cb)
5651 {
5652 case 6:
5653 cb = (abHdr[6] & 0x3f)
5654 | ((uint32_t)(abHdr[5] & 0x3f) << 6)
5655 | ((uint32_t)(abHdr[4] & 0x3f) << 12)
5656 | ((uint32_t)(abHdr[3] & 0x3f) << 18)
5657 | ((uint32_t)(abHdr[2] & 0x3f) << 24)
5658 | ((uint32_t)(abHdr[1] & 0x01) << 30);
5659 AssertLogRelMsgReturn(cb >= 0x04000000 && cb <= 0x7fffffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
5660 break;
5661 case 5:
5662 cb = (abHdr[5] & 0x3f)
5663 | ((uint32_t)(abHdr[4] & 0x3f) << 6)
5664 | ((uint32_t)(abHdr[3] & 0x3f) << 12)
5665 | ((uint32_t)(abHdr[2] & 0x3f) << 18)
5666 | ((uint32_t)(abHdr[1] & 0x03) << 24);
5667 AssertLogRelMsgReturn(cb >= 0x00200000 && cb <= 0x03ffffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
5668 break;
5669 case 4:
5670 cb = (abHdr[4] & 0x3f)
5671 | ((uint32_t)(abHdr[3] & 0x3f) << 6)
5672 | ((uint32_t)(abHdr[2] & 0x3f) << 12)
5673 | ((uint32_t)(abHdr[1] & 0x07) << 18);
5674 AssertLogRelMsgReturn(cb >= 0x00010000 && cb <= 0x001fffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
5675 break;
5676 case 3:
5677 cb = (abHdr[3] & 0x3f)
5678 | ((uint32_t)(abHdr[2] & 0x3f) << 6)
5679 | ((uint32_t)(abHdr[1] & 0x0f) << 12);
5680#if 0 /* disabled to optimize buffering */
5681 AssertLogRelMsgReturn(cb >= 0x00000800 && cb <= 0x0000ffff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
5682#endif
5683 break;
5684 case 2:
5685 cb = (abHdr[2] & 0x3f)
5686 | ((uint32_t)(abHdr[1] & 0x1f) << 6);
5687#if 0 /* disabled to optimize buffering */
5688 AssertLogRelMsgReturn(cb >= 0x00000080 && cb <= 0x000007ff, ("cb=%#x\n", cb), VERR_SSM_INTEGRITY_REC_HDR);
5689#endif
5690 break;
5691 default:
5692 return VERR_INTERNAL_ERROR;
5693 }
5694
5695 pSSM->u.Read.cbRecLeft = cb;
5696 }
5697
5698 Log3(("ssmR3DataReadRecHdrV2: %08llx|%08llx/%08x: Type=%02x fImportant=%RTbool cbHdr=%u\n",
5699 ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft,
5700 pSSM->u.Read.u8TypeAndFlags & SSM_REC_TYPE_MASK,
5701 !!(pSSM->u.Read.u8TypeAndFlags & SSM_REC_FLAGS_IMPORTANT),
5702 cbHdr
5703 )); NOREF(cbHdr);
5704 return VINF_SUCCESS;
5705}
5706
5707
5708/**
5709 * Buffer miss, do an unbuffered read.
5710 *
5711 * @param pSSM The saved state handle.
5712 * @param pvBuf Where to store the read data.
5713 * @param cbBuf Number of bytes to read.
5714 */
5715static int ssmR3DataReadUnbufferedV2(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf)
5716{
5717 void const *pvBufOrg = pvBuf; NOREF(pvBufOrg);
5718 size_t const cbBufOrg = cbBuf; NOREF(cbBufOrg);
5719
5720 /*
5721 * Copy out what we've got in the buffer.
5722 */
5723 uint32_t off = pSSM->u.Read.offDataBuffer;
5724 int32_t cbInBuffer = pSSM->u.Read.cbDataBuffer - off;
5725 Log4(("ssmR3DataReadUnbufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x\n", ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, cbInBuffer, cbBufOrg));
5726 if (cbInBuffer > 0)
5727 {
5728 uint32_t const cbToCopy = (uint32_t)cbInBuffer;
5729 Assert(cbBuf > cbToCopy);
5730 memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[off], cbToCopy);
5731 pvBuf = (uint8_t *)pvBuf + cbToCopy;
5732 cbBuf -= cbToCopy;
5733 pSSM->u.Read.cbDataBuffer = 0;
5734 pSSM->u.Read.offDataBuffer = 0;
5735 }
5736
5737 /*
5738 * Read data.
5739 */
5740 do
5741 {
5742 /*
5743 * Read the next record header if no more data.
5744 */
5745 if (!pSSM->u.Read.cbRecLeft)
5746 {
5747 int rc = ssmR3DataReadRecHdrV2(pSSM);
5748 if (RT_FAILURE(rc))
5749 return pSSM->rc = rc;
5750 }
5751 AssertLogRelMsgReturn(!pSSM->u.Read.fEndOfData, ("cbBuf=%zu", cbBuf), pSSM->rc = VERR_SSM_LOADED_TOO_MUCH);
5752
5753 /*
5754 * Read data from the current record.
5755 */
5756 uint32_t cbToRead;
5757 switch (pSSM->u.Read.u8TypeAndFlags & SSM_REC_TYPE_MASK)
5758 {
5759 case SSM_REC_TYPE_RAW:
5760 {
5761 cbToRead = (uint32_t)RT_MIN(cbBuf, pSSM->u.Read.cbRecLeft);
5762 int rc = ssmR3DataReadV2Raw(pSSM, pvBuf, cbToRead);
5763 if (RT_FAILURE(rc))
5764 return pSSM->rc = rc;
5765 pSSM->u.Read.cbRecLeft -= cbToRead;
5766 break;
5767 }
5768
5769 case SSM_REC_TYPE_RAW_LZF:
5770 {
5771 int rc = ssmR3DataReadV2RawLzfHdr(pSSM, &cbToRead);
5772 if (RT_FAILURE(rc))
5773 return rc;
5774 if (cbToRead <= cbBuf)
5775 {
5776 rc = ssmR3DataReadV2RawLzf(pSSM, pvBuf, cbToRead);
5777 if (RT_FAILURE(rc))
5778 return rc;
5779 }
5780 else
5781 {
5782 /* The output buffer is too small, use the data buffer. */
5783 rc = ssmR3DataReadV2RawLzf(pSSM, &pSSM->u.Read.abDataBuffer[0], cbToRead);
5784 if (RT_FAILURE(rc))
5785 return rc;
5786 pSSM->u.Read.cbDataBuffer = cbToRead;
5787 cbToRead = (uint32_t)cbBuf;
5788 pSSM->u.Read.offDataBuffer = cbToRead;
5789 memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[0], cbToRead);
5790 }
5791 break;
5792 }
5793
5794 case SSM_REC_TYPE_RAW_ZERO:
5795 {
5796 int rc = ssmR3DataReadV2RawZeroHdr(pSSM, &cbToRead);
5797 if (RT_FAILURE(rc))
5798 return rc;
5799 if (cbToRead > cbBuf)
5800 {
5801 /* Spill the remainer into the data buffer. */
5802 memset(&pSSM->u.Read.abDataBuffer[0], 0, cbToRead - cbBuf);
5803 pSSM->u.Read.cbDataBuffer = cbToRead - (uint32_t)cbBuf;
5804 pSSM->u.Read.offDataBuffer = 0;
5805 cbToRead = (uint32_t)cbBuf;
5806 }
5807 memset(pvBuf, 0, cbToRead);
5808 break;
5809 }
5810
5811 default:
5812 AssertMsgFailedReturn(("%x\n", pSSM->u.Read.u8TypeAndFlags), VERR_INTERNAL_ERROR_5);
5813 }
5814
5815 cbBuf -= cbToRead;
5816 pvBuf = (uint8_t *)pvBuf + cbToRead;
5817 } while (cbBuf > 0);
5818
5819 Log4(("ssmR3DataReadUnBufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x %.*Rhxs%s\n",
5820 ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, 0, cbBufOrg, RT_MIN(SSM_LOG_BYTES, cbBufOrg), pvBufOrg, cbBufOrg > SSM_LOG_BYTES ? "..." : ""));
5821 return VINF_SUCCESS;
5822}
5823
5824
5825/**
5826 * Buffer miss, do a buffered read.
5827 *
5828 * @returns VBox status code. Sets pSSM->rc on error.
5829 *
5830 * @param pSSM The saved state handle.
5831 * @param pvBuf Where to store the read data.
5832 * @param cbBuf Number of bytes to read.
5833 */
5834static int ssmR3DataReadBufferedV2(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf)
5835{
5836 void const *pvBufOrg = pvBuf; NOREF(pvBufOrg);
5837 size_t const cbBufOrg = cbBuf; NOREF(cbBufOrg);
5838
5839 /*
5840 * Copy out what we've got in the buffer.
5841 */
5842 uint32_t off = pSSM->u.Read.offDataBuffer;
5843 int32_t cbInBuffer = pSSM->u.Read.cbDataBuffer - off;
5844 Log4(("ssmR3DataReadBufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x\n", ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, cbInBuffer, cbBufOrg));
5845 if (cbInBuffer > 0)
5846 {
5847 uint32_t const cbToCopy = (uint32_t)cbInBuffer;
5848 Assert(cbBuf > cbToCopy);
5849 memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[off], cbToCopy);
5850 pvBuf = (uint8_t *)pvBuf + cbToCopy;
5851 cbBuf -= cbToCopy;
5852 pSSM->u.Read.cbDataBuffer = 0;
5853 pSSM->u.Read.offDataBuffer = 0;
5854 }
5855
5856 /*
5857 * Buffer more data.
5858 */
5859 do
5860 {
5861 /*
5862 * Read the next record header if no more data.
5863 */
5864 if (!pSSM->u.Read.cbRecLeft)
5865 {
5866 int rc = ssmR3DataReadRecHdrV2(pSSM);
5867 if (RT_FAILURE(rc))
5868 return pSSM->rc = rc;
5869 }
5870 AssertLogRelMsgReturn(!pSSM->u.Read.fEndOfData, ("cbBuf=%zu", cbBuf), pSSM->rc = VERR_SSM_LOADED_TOO_MUCH);
5871
5872 /*
5873 * Read data from the current record.
5874 * LATER: optimize by reading directly into the output buffer for some cases.
5875 */
5876 uint32_t cbToRead;
5877 switch (pSSM->u.Read.u8TypeAndFlags & SSM_REC_TYPE_MASK)
5878 {
5879 case SSM_REC_TYPE_RAW:
5880 {
5881 cbToRead = RT_MIN(sizeof(pSSM->u.Read.abDataBuffer), pSSM->u.Read.cbRecLeft);
5882 int rc = ssmR3DataReadV2Raw(pSSM, &pSSM->u.Read.abDataBuffer[0], cbToRead);
5883 if (RT_FAILURE(rc))
5884 return pSSM->rc = rc;
5885 pSSM->u.Read.cbRecLeft -= cbToRead;
5886 pSSM->u.Read.cbDataBuffer = cbToRead;
5887 break;
5888 }
5889
5890 case SSM_REC_TYPE_RAW_LZF:
5891 {
5892 int rc = ssmR3DataReadV2RawLzfHdr(pSSM, &cbToRead);
5893 if (RT_FAILURE(rc))
5894 return rc;
5895 rc = ssmR3DataReadV2RawLzf(pSSM, &pSSM->u.Read.abDataBuffer[0], cbToRead);
5896 if (RT_FAILURE(rc))
5897 return rc;
5898 pSSM->u.Read.cbDataBuffer = cbToRead;
5899 break;
5900 }
5901
5902 case SSM_REC_TYPE_RAW_ZERO:
5903 {
5904 int rc = ssmR3DataReadV2RawZeroHdr(pSSM, &cbToRead);
5905 if (RT_FAILURE(rc))
5906 return rc;
5907 memset(&pSSM->u.Read.abDataBuffer[0], 0, cbToRead);
5908 pSSM->u.Read.cbDataBuffer = cbToRead;
5909 break;
5910 }
5911
5912 default:
5913 AssertMsgFailedReturn(("%x\n", pSSM->u.Read.u8TypeAndFlags), VERR_INTERNAL_ERROR_5);
5914 }
5915 /*pSSM->u.Read.offDataBuffer = 0;*/
5916
5917 /*
5918 * Copy data from the buffer.
5919 */
5920 uint32_t cbToCopy = (uint32_t)RT_MIN(cbBuf, cbToRead);
5921 memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[0], cbToCopy);
5922 cbBuf -= cbToCopy;
5923 pvBuf = (uint8_t *)pvBuf + cbToCopy;
5924 pSSM->u.Read.offDataBuffer = cbToCopy;
5925 } while (cbBuf > 0);
5926
5927 Log4(("ssmR3DataReadBufferedV2: %08llx|%08llx/%08x/%08x: cbBuf=%#x %.*Rhxs%s\n",
5928 ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, pSSM->u.Read.cbDataBuffer - pSSM->u.Read.offDataBuffer,
5929 cbBufOrg, RT_MIN(SSM_LOG_BYTES, cbBufOrg), pvBufOrg, cbBufOrg > SSM_LOG_BYTES ? "..." : ""));
5930 return VINF_SUCCESS;
5931}
5932
5933
5934/**
5935 * Inlined worker that handles format checks and buffered reads.
5936 *
5937 * @param pSSM The saved state handle.
5938 * @param pvBuf Where to store the read data.
5939 * @param cbBuf Number of bytes to read.
5940 */
5941DECLINLINE(int) ssmR3DataRead(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf)
5942{
5943 /*
5944 * Fend off previous errors and V1 data units.
5945 */
5946 if (RT_FAILURE(pSSM->rc))
5947 return pSSM->rc;
5948 if (RT_UNLIKELY(pSSM->u.Read.uFmtVerMajor == 1))
5949 return ssmR3DataReadV1(pSSM, pvBuf, cbBuf);
5950
5951 /*
5952 * Check if the requested data is buffered.
5953 */
5954 uint32_t off = pSSM->u.Read.offDataBuffer;
5955 if ( off + cbBuf > pSSM->u.Read.cbDataBuffer
5956 || cbBuf > sizeof(pSSM->u.Read.abDataBuffer))
5957 {
5958 if (cbBuf <= sizeof(pSSM->u.Read.abDataBuffer) / 8)
5959 return ssmR3DataReadBufferedV2(pSSM, pvBuf, cbBuf);
5960 return ssmR3DataReadUnbufferedV2(pSSM, pvBuf, cbBuf);
5961 }
5962
5963 memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[off], cbBuf);
5964 pSSM->u.Read.offDataBuffer = off + (uint32_t)cbBuf;
5965 Log4((cbBuf
5966 ? "ssmR3DataRead: %08llx|%08llx/%08x/%08x: cbBuf=%#x %.*Rhxs%s\n"
5967 : "ssmR3DataRead: %08llx|%08llx/%08x/%08x: cbBuf=%#x\n",
5968 ssmR3StrmTell(&pSSM->Strm), pSSM->offUnit, pSSM->u.Read.cbRecLeft, pSSM->u.Read.cbDataBuffer - pSSM->u.Read.offDataBuffer,
5969 cbBuf, RT_MIN(SSM_LOG_BYTES, cbBuf), pvBuf, cbBuf > SSM_LOG_BYTES ? "..." : ""));
5970
5971 return VINF_SUCCESS;
5972}
5973
5974
5975/**
5976 * Gets a structure.
5977 *
5978 * @returns VBox status code.
5979 * @param pSSM The saved state handle.
5980 * @param pvStruct The structure address.
5981 * @param paFields The array of structure fields descriptions.
5982 * The array must be terminated by a SSMFIELD_ENTRY_TERM().
5983 */
5984VMMR3DECL(int) SSMR3GetStruct(PSSMHANDLE pSSM, void *pvStruct, PCSSMFIELD paFields)
5985{
5986 SSM_ASSERT_READABLE_RET(pSSM);
5987 SSM_CHECK_CANCELLED_RET(pSSM);
5988 AssertPtr(pvStruct);
5989 AssertPtr(paFields);
5990
5991 /* begin marker. */
5992 uint32_t u32Magic;
5993 int rc = SSMR3GetU32(pSSM, &u32Magic);
5994 if (RT_FAILURE(rc))
5995 return rc;
5996 AssertMsgReturn(u32Magic == SSMR3STRUCT_BEGIN, ("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
5997
5998 /* get the fields */
5999 for (PCSSMFIELD pCur = paFields;
6000 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
6001 pCur++)
6002 {
6003 uint8_t *pbField = (uint8_t *)pvStruct + pCur->off;
6004 switch ((uintptr_t)pCur->pfnGetPutOrTransformer)
6005 {
6006 case SSMFIELDTRANS_NO_TRANSFORMATION:
6007 rc = ssmR3DataRead(pSSM, pbField, pCur->cb);
6008 break;
6009
6010 case SSMFIELDTRANS_GCPTR:
6011 AssertMsgReturn(pCur->cb == sizeof(RTGCPTR), ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6012 rc = SSMR3GetGCPtr(pSSM, (PRTGCPTR)pbField);
6013 break;
6014
6015 case SSMFIELDTRANS_GCPHYS:
6016 AssertMsgReturn(pCur->cb == sizeof(RTGCPHYS), ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6017 rc = SSMR3GetGCPhys(pSSM, (PRTGCPHYS)pbField);
6018 break;
6019
6020 case SSMFIELDTRANS_RCPTR:
6021 AssertMsgReturn(pCur->cb == sizeof(RTRCPTR), ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6022 rc = SSMR3GetRCPtr(pSSM, (PRTRCPTR)pbField);
6023 break;
6024
6025 case SSMFIELDTRANS_RCPTR_ARRAY:
6026 {
6027 uint32_t const cEntries = pCur->cb / sizeof(RTRCPTR);
6028 AssertMsgReturn(pCur->cb == cEntries * sizeof(RTRCPTR) && cEntries, ("%#x (%s)\n", pCur->cb, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6029 rc = VINF_SUCCESS;
6030 for (uint32_t i = 0; i < cEntries && RT_SUCCESS(rc); i++)
6031 rc = SSMR3GetRCPtr(pSSM, &((PRTRCPTR)pbField)[i]);
6032 break;
6033 }
6034
6035 default:
6036 AssertMsgFailedReturn(("%#x\n", pCur->pfnGetPutOrTransformer), VERR_SSM_FIELD_COMPLEX);
6037 }
6038 if (RT_FAILURE(rc))
6039 return rc;
6040 }
6041
6042 /* end marker */
6043 rc = SSMR3GetU32(pSSM, &u32Magic);
6044 if (RT_FAILURE(rc))
6045 return rc;
6046 AssertMsgReturn(u32Magic == SSMR3STRUCT_END, ("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
6047 return rc;
6048}
6049
6050
6051/**
6052 * SSMR3GetStructEx helper that gets a HCPTR that is used as a NULL indicator.
6053 *
6054 * @returns VBox status code.
6055 *
6056 * @param pSSM The saved state handle.
6057 * @param ppv Where to return the value (0/1).
6058 * @param fFlags SSMSTRUCT_FLAGS_XXX.
6059 */
6060DECLINLINE(int) ssmR3GetHCPtrNI(PSSMHANDLE pSSM, void **ppv, uint32_t fFlags)
6061{
6062 int rc;
6063 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
6064 {
6065 if (ssmR3GetHostBits(pSSM) == 64)
6066 {
6067 uint64_t u;
6068 rc = ssmR3DataRead(pSSM, &u, sizeof(u));
6069 if (RT_SUCCESS(rc))
6070 *ppv = (void *)(u ? 1 : 0);
6071 }
6072 else
6073 {
6074 uint32_t u;
6075 rc = ssmR3DataRead(pSSM, &u, sizeof(u));
6076 if (RT_SUCCESS(rc))
6077 *ppv = (void *)(u ? 1 : 0);
6078 }
6079 }
6080 else
6081 {
6082 bool f;
6083 rc = SSMR3GetBool(pSSM, &f);
6084 if (RT_SUCCESS(rc))
6085 *ppv = (void *)(f ? 1 : 0);
6086 }
6087 return rc;
6088}
6089
6090
6091/**
6092 * Guts a structure, extended API.
6093 *
6094 * @returns VBox status code.
6095 * @param pSSM The saved state handle.
6096 * @param pvStruct The structure address.
6097 * @param cbStruct The size of the struct (use for validation only).
6098 * @param fFlags Combination of SSMSTRUCT_FLAGS_XXX defines.
6099 * @param paFields The array of structure fields descriptions. The
6100 * array must be terminated by a SSMFIELD_ENTRY_TERM().
6101 * @param pvUser User argument for any callbacks that paFields might
6102 * contain.
6103 */
6104VMMR3DECL(int) SSMR3GetStructEx(PSSMHANDLE pSSM, void *pvStruct, size_t cbStruct,
6105 uint32_t fFlags, PCSSMFIELD paFields, void *pvUser)
6106{
6107 int rc;
6108 uint32_t u32Magic;
6109
6110 /*
6111 * Validation.
6112 */
6113 SSM_ASSERT_READABLE_RET(pSSM);
6114 SSM_CHECK_CANCELLED_RET(pSSM);
6115 AssertMsgReturn(!(fFlags & ~SSMSTRUCT_FLAGS_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
6116 AssertPtr(pvStruct);
6117 AssertPtr(paFields);
6118
6119 /*
6120 * Begin marker.
6121 */
6122 if (!(fFlags & SSMSTRUCT_FLAGS_NO_MARKERS))
6123 {
6124 rc = SSMR3GetU32(pSSM, &u32Magic);
6125 if (RT_FAILURE(rc))
6126 return rc;
6127 AssertMsgReturn(u32Magic == SSMR3STRUCT_BEGIN, ("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
6128 }
6129
6130 /*
6131 * Put the fields
6132 */
6133 uint32_t off = 0;
6134 for (PCSSMFIELD pCur = paFields;
6135 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
6136 pCur++)
6137 {
6138 uint32_t const offField = (!SSMFIELDTRANS_IS_PADDING(pCur->pfnGetPutOrTransformer) || pCur->off != UINT32_MAX / 2)
6139 && !SSMFIELDTRANS_IS_OLD(pCur->pfnGetPutOrTransformer)
6140 ? pCur->off
6141 : off;
6142 uint32_t const cbField = SSMFIELDTRANS_IS_OLD(pCur->pfnGetPutOrTransformer)
6143 ? 0
6144 : SSMFIELDTRANS_IS_PADDING(pCur->pfnGetPutOrTransformer)
6145 ? RT_HIWORD(pCur->cb)
6146 : pCur->cb;
6147 AssertMsgReturn( cbField <= cbStruct
6148 && offField + cbField <= cbStruct
6149 && offField + cbField >= offField,
6150 ("off=%#x cb=%#x cbStruct=%#x (%s)\n", cbField, offField, cbStruct, pCur->pszName),
6151 VERR_SSM_FIELD_OUT_OF_BOUNDS);
6152 AssertMsgReturn( !(fFlags & SSMSTRUCT_FLAGS_FULL_STRUCT)
6153 || off == offField,
6154 ("off=%#x offField=%#x (%s)\n", off, offField, pCur->pszName),
6155 VERR_SSM_FIELD_NOT_CONSECUTIVE);
6156
6157 rc = VINF_SUCCESS;
6158 uint8_t *pbField = (uint8_t *)pvStruct + offField;
6159 switch ((uintptr_t)pCur->pfnGetPutOrTransformer)
6160 {
6161 case SSMFIELDTRANS_NO_TRANSFORMATION:
6162 rc = ssmR3DataRead(pSSM, pbField, cbField);
6163 break;
6164
6165 case SSMFIELDTRANS_GCPHYS:
6166 AssertMsgReturn(cbField == sizeof(RTGCPHYS), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6167 rc = SSMR3GetGCPhys(pSSM, (PRTGCPHYS)pbField);
6168 break;
6169
6170 case SSMFIELDTRANS_GCPTR:
6171 AssertMsgReturn(cbField == sizeof(RTGCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6172 rc = SSMR3GetGCPtr(pSSM, (PRTGCPTR)pbField);
6173 break;
6174
6175 case SSMFIELDTRANS_RCPTR:
6176 AssertMsgReturn(cbField == sizeof(RTRCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6177 rc = SSMR3GetRCPtr(pSSM, (PRTRCPTR)pbField);
6178 break;
6179
6180 case SSMFIELDTRANS_RCPTR_ARRAY:
6181 {
6182 uint32_t const cEntries = cbField / sizeof(RTRCPTR);
6183 AssertMsgReturn(cbField == cEntries * sizeof(RTRCPTR) && cEntries, ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6184 rc = VINF_SUCCESS;
6185 for (uint32_t i = 0; i < cEntries && RT_SUCCESS(rc); i++)
6186 rc = SSMR3GetRCPtr(pSSM, &((PRTRCPTR)pbField)[i]);
6187 break;
6188 }
6189
6190 case SSMFIELDTRANS_HCPTR_NI:
6191 AssertMsgReturn(cbField == sizeof(void *), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6192 rc = ssmR3GetHCPtrNI(pSSM, (void **)pbField, fFlags);
6193 break;
6194
6195 case SSMFIELDTRANS_HCPTR_NI_ARRAY:
6196 {
6197 uint32_t const cEntries = cbField / sizeof(void *);
6198 AssertMsgReturn(cbField == cEntries * sizeof(void *) && cEntries, ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6199 rc = VINF_SUCCESS;
6200 for (uint32_t i = 0; i < cEntries && RT_SUCCESS(rc); i++)
6201 rc = ssmR3GetHCPtrNI(pSSM, &((void **)pbField)[i], fFlags);
6202 break;
6203 }
6204
6205 case SSMFIELDTRANS_HCPTR_HACK_U32:
6206 AssertMsgReturn(cbField == sizeof(void *), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6207 *(uintptr_t *)pbField = 0;
6208 rc = ssmR3DataRead(pSSM, pbField, sizeof(uint32_t));
6209 if ((fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE) && ssmR3GetHostBits(pSSM) == 64)
6210 {
6211 uint32_t u32;
6212 rc = ssmR3DataRead(pSSM, &u32, sizeof(uint32_t));
6213 AssertMsgReturn(RT_FAILURE(rc) || u32 == 0 || (fFlags & SSMSTRUCT_FLAGS_SAVED_AS_MEM),
6214 ("high=%#x low=%#x (%s)\n", u32, *(uint32_t *)pbField, pCur->pszName),
6215 VERR_SSM_FIELD_INVALID_VALUE);
6216 }
6217 break;
6218
6219
6220 case SSMFIELDTRANS_IGNORE:
6221 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
6222 rc = SSMR3Skip(pSSM, cbField);
6223 break;
6224
6225 case SSMFIELDTRANS_IGN_GCPHYS:
6226 AssertMsgReturn(cbField == sizeof(RTGCPHYS), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6227 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
6228 rc = SSMR3Skip(pSSM, pSSM->u.Read.cbGCPhys);
6229 break;
6230
6231 case SSMFIELDTRANS_IGN_GCPTR:
6232 AssertMsgReturn(cbField == sizeof(RTGCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6233 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
6234 rc = SSMR3Skip(pSSM, pSSM->u.Read.cbGCPtr);
6235 break;
6236
6237 case SSMFIELDTRANS_IGN_RCPTR:
6238 AssertMsgReturn(cbField == sizeof(RTRCPTR), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6239 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
6240 rc = SSMR3Skip(pSSM, sizeof(RTRCPTR));
6241 break;
6242
6243 case SSMFIELDTRANS_IGN_HCPTR:
6244 AssertMsgReturn(cbField == sizeof(void *), ("%#x (%s)\n", cbField, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6245 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
6246 rc = SSMR3Skip(pSSM, ssmR3GetHostBits(pSSM) / 8);
6247 break;
6248
6249
6250 case SSMFIELDTRANS_OLD:
6251 AssertMsgReturn(pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6252 rc = SSMR3Skip(pSSM, pCur->cb);
6253 break;
6254
6255 case SSMFIELDTRANS_OLD_GCPHYS:
6256 AssertMsgReturn(pCur->cb == sizeof(RTGCPHYS) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6257 rc = SSMR3Skip(pSSM, pSSM->u.Read.cbGCPhys);
6258 break;
6259
6260 case SSMFIELDTRANS_OLD_GCPTR:
6261 AssertMsgReturn(pCur->cb == sizeof(RTGCPTR) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6262 rc = SSMR3Skip(pSSM, pSSM->u.Read.cbGCPtr);
6263 break;
6264
6265 case SSMFIELDTRANS_OLD_RCPTR:
6266 AssertMsgReturn(pCur->cb == sizeof(RTRCPTR) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6267 rc = SSMR3Skip(pSSM, sizeof(RTRCPTR));
6268 break;
6269
6270 case SSMFIELDTRANS_OLD_HCPTR:
6271 AssertMsgReturn(pCur->cb == sizeof(void *) && pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6272 rc = SSMR3Skip(pSSM, ssmR3GetHostBits(pSSM) / 8);
6273 break;
6274
6275 case SSMFIELDTRANS_OLD_PAD_HC:
6276 AssertMsgReturn(pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6277 rc = SSMR3Skip(pSSM, ssmR3GetHostBits(pSSM) == 64 ? RT_HIWORD(pCur->cb) : RT_LOWORD(pCur->cb));
6278 break;
6279
6280 case SSMFIELDTRANS_OLD_PAD_MSC32:
6281 AssertMsgReturn(pCur->off == UINT32_MAX / 2, ("%#x %#x (%s)\n", pCur->cb, pCur->off, pCur->pszName), VERR_SSM_FIELD_INVALID_SIZE);
6282 if (ssmR3IsHostMsc32(pSSM))
6283 rc = SSMR3Skip(pSSM, pCur->cb);
6284 break;
6285
6286
6287 case SSMFIELDTRANS_PAD_HC:
6288 case SSMFIELDTRANS_PAD_HC32:
6289 case SSMFIELDTRANS_PAD_HC64:
6290 case SSMFIELDTRANS_PAD_HC_AUTO:
6291 case SSMFIELDTRANS_PAD_MSC32_AUTO:
6292 {
6293 uint32_t cb32 = RT_BYTE1(pCur->cb);
6294 uint32_t cb64 = RT_BYTE2(pCur->cb);
6295 uint32_t cbCtx = HC_ARCH_BITS == 64
6296 || ( (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_MSC32_AUTO
6297 && !SSM_HOST_IS_MSC_32)
6298 ? cb64 : cb32;
6299 uint32_t cbSaved = ssmR3GetHostBits(pSSM) == 64
6300 || ( (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_MSC32_AUTO
6301 && !ssmR3IsHostMsc32(pSSM))
6302 ? cb64 : cb32;
6303 AssertMsgReturn( cbField == cbCtx
6304 && ( ( pCur->off == UINT32_MAX / 2
6305 && ( cbField == 0
6306 || (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_HC_AUTO
6307 || (uintptr_t)pCur->pfnGetPutOrTransformer == SSMFIELDTRANS_PAD_MSC32_AUTO
6308 )
6309 )
6310 || (pCur->off != UINT32_MAX / 2 && cbField != 0)
6311 )
6312 , ("cbField=%#x cb32=%#x cb64=%#x HC_ARCH_BITS=%u cbCtx=%#x cbSaved=%#x off=%#x\n",
6313 cbField, cb32, cb64, HC_ARCH_BITS, cbCtx, cbSaved, pCur->off),
6314 VERR_SSM_FIELD_INVALID_PADDING_SIZE);
6315 if (fFlags & SSMSTRUCT_FLAGS_DONT_IGNORE)
6316 rc = SSMR3Skip(pSSM, cbSaved);
6317 break;
6318 }
6319
6320 default:
6321 AssertPtrReturn(pCur->pfnGetPutOrTransformer, VERR_SSM_FIELD_INVALID_CALLBACK);
6322 rc = pCur->pfnGetPutOrTransformer(pSSM, pCur, pvStruct, fFlags, true /*fGetOrPut*/, pvUser);
6323 break;
6324 }
6325 if (RT_FAILURE(rc))
6326 return rc;
6327
6328 off = offField + cbField;
6329 }
6330 AssertMsgReturn( !(fFlags & SSMSTRUCT_FLAGS_FULL_STRUCT)
6331 || off == cbStruct,
6332 ("off=%#x cbStruct=%#x\n", off, cbStruct),
6333 VERR_SSM_FIELD_NOT_CONSECUTIVE);
6334
6335 /*
6336 * End marker
6337 */
6338 if (!(fFlags & SSMSTRUCT_FLAGS_NO_MARKERS))
6339 {
6340 rc = SSMR3GetU32(pSSM, &u32Magic);
6341 if (RT_FAILURE(rc))
6342 return rc;
6343 AssertMsgReturn(u32Magic == SSMR3STRUCT_END, ("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
6344 }
6345
6346 return VINF_SUCCESS;
6347}
6348
6349
6350/**
6351 * Loads a boolean item from the current data unit.
6352 *
6353 * @returns VBox status.
6354 * @param pSSM The saved state handle.
6355 * @param pfBool Where to store the item.
6356 */
6357VMMR3DECL(int) SSMR3GetBool(PSSMHANDLE pSSM, bool *pfBool)
6358{
6359 SSM_ASSERT_READABLE_RET(pSSM);
6360 SSM_CHECK_CANCELLED_RET(pSSM);
6361 uint8_t u8; /* see SSMR3PutBool */
6362 int rc = ssmR3DataRead(pSSM, &u8, sizeof(u8));
6363 if (RT_SUCCESS(rc))
6364 {
6365 Assert(u8 <= 1);
6366 *pfBool = !!u8;
6367 }
6368 return rc;
6369}
6370
6371
6372/**
6373 * Loads a 8-bit unsigned integer item from the current data unit.
6374 *
6375 * @returns VBox status.
6376 * @param pSSM The saved state handle.
6377 * @param pu8 Where to store the item.
6378 */
6379VMMR3DECL(int) SSMR3GetU8(PSSMHANDLE pSSM, uint8_t *pu8)
6380{
6381 SSM_ASSERT_READABLE_RET(pSSM);
6382 SSM_CHECK_CANCELLED_RET(pSSM);
6383 return ssmR3DataRead(pSSM, pu8, sizeof(*pu8));
6384}
6385
6386
6387/**
6388 * Loads a 8-bit signed integer item from the current data unit.
6389 *
6390 * @returns VBox status.
6391 * @param pSSM The saved state handle.
6392 * @param pi8 Where to store the item.
6393 */
6394VMMR3DECL(int) SSMR3GetS8(PSSMHANDLE pSSM, int8_t *pi8)
6395{
6396 SSM_ASSERT_READABLE_RET(pSSM);
6397 SSM_CHECK_CANCELLED_RET(pSSM);
6398 return ssmR3DataRead(pSSM, pi8, sizeof(*pi8));
6399}
6400
6401
6402/**
6403 * Loads a 16-bit unsigned integer item from the current data unit.
6404 *
6405 * @returns VBox status.
6406 * @param pSSM The saved state handle.
6407 * @param pu16 Where to store the item.
6408 */
6409VMMR3DECL(int) SSMR3GetU16(PSSMHANDLE pSSM, uint16_t *pu16)
6410{
6411 SSM_ASSERT_READABLE_RET(pSSM);
6412 SSM_CHECK_CANCELLED_RET(pSSM);
6413 return ssmR3DataRead(pSSM, pu16, sizeof(*pu16));
6414}
6415
6416
6417/**
6418 * Loads a 16-bit signed integer item from the current data unit.
6419 *
6420 * @returns VBox status.
6421 * @param pSSM The saved state handle.
6422 * @param pi16 Where to store the item.
6423 */
6424VMMR3DECL(int) SSMR3GetS16(PSSMHANDLE pSSM, int16_t *pi16)
6425{
6426 SSM_ASSERT_READABLE_RET(pSSM);
6427 SSM_CHECK_CANCELLED_RET(pSSM);
6428 return ssmR3DataRead(pSSM, pi16, sizeof(*pi16));
6429}
6430
6431
6432/**
6433 * Loads a 32-bit unsigned integer item from the current data unit.
6434 *
6435 * @returns VBox status.
6436 * @param pSSM The saved state handle.
6437 * @param pu32 Where to store the item.
6438 */
6439VMMR3DECL(int) SSMR3GetU32(PSSMHANDLE pSSM, uint32_t *pu32)
6440{
6441 SSM_ASSERT_READABLE_RET(pSSM);
6442 SSM_CHECK_CANCELLED_RET(pSSM);
6443 return ssmR3DataRead(pSSM, pu32, sizeof(*pu32));
6444}
6445
6446
6447/**
6448 * Loads a 32-bit signed integer item from the current data unit.
6449 *
6450 * @returns VBox status.
6451 * @param pSSM The saved state handle.
6452 * @param pi32 Where to store the item.
6453 */
6454VMMR3DECL(int) SSMR3GetS32(PSSMHANDLE pSSM, int32_t *pi32)
6455{
6456 SSM_ASSERT_READABLE_RET(pSSM);
6457 SSM_CHECK_CANCELLED_RET(pSSM);
6458 return ssmR3DataRead(pSSM, pi32, sizeof(*pi32));
6459}
6460
6461
6462/**
6463 * Loads a 64-bit unsigned integer item from the current data unit.
6464 *
6465 * @returns VBox status.
6466 * @param pSSM The saved state handle.
6467 * @param pu64 Where to store the item.
6468 */
6469VMMR3DECL(int) SSMR3GetU64(PSSMHANDLE pSSM, uint64_t *pu64)
6470{
6471 SSM_ASSERT_READABLE_RET(pSSM);
6472 SSM_CHECK_CANCELLED_RET(pSSM);
6473 return ssmR3DataRead(pSSM, pu64, sizeof(*pu64));
6474}
6475
6476
6477/**
6478 * Loads a 64-bit signed integer item from the current data unit.
6479 *
6480 * @returns VBox status.
6481 * @param pSSM The saved state handle.
6482 * @param pi64 Where to store the item.
6483 */
6484VMMR3DECL(int) SSMR3GetS64(PSSMHANDLE pSSM, int64_t *pi64)
6485{
6486 SSM_ASSERT_READABLE_RET(pSSM);
6487 SSM_CHECK_CANCELLED_RET(pSSM);
6488 return ssmR3DataRead(pSSM, pi64, sizeof(*pi64));
6489}
6490
6491
6492/**
6493 * Loads a 128-bit unsigned integer item from the current data unit.
6494 *
6495 * @returns VBox status.
6496 * @param pSSM The saved state handle.
6497 * @param pu128 Where to store the item.
6498 */
6499VMMR3DECL(int) SSMR3GetU128(PSSMHANDLE pSSM, uint128_t *pu128)
6500{
6501 SSM_ASSERT_READABLE_RET(pSSM);
6502 SSM_CHECK_CANCELLED_RET(pSSM);
6503 return ssmR3DataRead(pSSM, pu128, sizeof(*pu128));
6504}
6505
6506
6507/**
6508 * Loads a 128-bit signed integer item from the current data unit.
6509 *
6510 * @returns VBox status.
6511 * @param pSSM The saved state handle.
6512 * @param pi128 Where to store the item.
6513 */
6514VMMR3DECL(int) SSMR3GetS128(PSSMHANDLE pSSM, int128_t *pi128)
6515{
6516 SSM_ASSERT_READABLE_RET(pSSM);
6517 SSM_CHECK_CANCELLED_RET(pSSM);
6518 return ssmR3DataRead(pSSM, pi128, sizeof(*pi128));
6519}
6520
6521
6522/**
6523 * Loads a VBox unsigned integer item from the current data unit.
6524 *
6525 * @returns VBox status.
6526 * @param pSSM The saved state handle.
6527 * @param pu Where to store the integer.
6528 */
6529VMMR3DECL(int) SSMR3GetUInt(PSSMHANDLE pSSM, PRTUINT pu)
6530{
6531 SSM_ASSERT_READABLE_RET(pSSM);
6532 SSM_CHECK_CANCELLED_RET(pSSM);
6533 return ssmR3DataRead(pSSM, pu, sizeof(*pu));
6534}
6535
6536
6537/**
6538 * Loads a VBox signed integer item from the current data unit.
6539 *
6540 * @returns VBox status.
6541 * @param pSSM The saved state handle.
6542 * @param pi Where to store the integer.
6543 */
6544VMMR3DECL(int) SSMR3GetSInt(PSSMHANDLE pSSM, PRTINT pi)
6545{
6546 SSM_ASSERT_READABLE_RET(pSSM);
6547 SSM_CHECK_CANCELLED_RET(pSSM);
6548 return ssmR3DataRead(pSSM, pi, sizeof(*pi));
6549}
6550
6551
6552/**
6553 * Loads a GC natural unsigned integer item from the current data unit.
6554 *
6555 * @returns VBox status.
6556 * @param pSSM The saved state handle.
6557 * @param pu Where to store the integer.
6558 *
6559 * @deprecated Silly type with an incorrect size, don't use it.
6560 */
6561VMMR3DECL(int) SSMR3GetGCUInt(PSSMHANDLE pSSM, PRTGCUINT pu)
6562{
6563 AssertCompile(sizeof(RTGCPTR) == sizeof(*pu));
6564 return SSMR3GetGCPtr(pSSM, (PRTGCPTR)pu);
6565}
6566
6567
6568/**
6569 * Loads a GC unsigned integer register item from the current data unit.
6570 *
6571 * @returns VBox status.
6572 * @param pSSM The saved state handle.
6573 * @param pu Where to store the integer.
6574 */
6575VMMR3DECL(int) SSMR3GetGCUIntReg(PSSMHANDLE pSSM, PRTGCUINTREG pu)
6576{
6577 AssertCompile(sizeof(RTGCPTR) == sizeof(*pu));
6578 return SSMR3GetGCPtr(pSSM, (PRTGCPTR)pu);
6579}
6580
6581
6582/**
6583 * Loads a 32 bits GC physical address item from the current data unit.
6584 *
6585 * @returns VBox status.
6586 * @param pSSM The saved state handle.
6587 * @param pGCPhys Where to store the GC physical address.
6588 */
6589VMMR3DECL(int) SSMR3GetGCPhys32(PSSMHANDLE pSSM, PRTGCPHYS32 pGCPhys)
6590{
6591 SSM_ASSERT_READABLE_RET(pSSM);
6592 SSM_CHECK_CANCELLED_RET(pSSM);
6593 return ssmR3DataRead(pSSM, pGCPhys, sizeof(*pGCPhys));
6594}
6595
6596
6597/**
6598 * Loads a 64 bits GC physical address item from the current data unit.
6599 *
6600 * @returns VBox status.
6601 * @param pSSM The saved state handle.
6602 * @param pGCPhys Where to store the GC physical address.
6603 */
6604VMMR3DECL(int) SSMR3GetGCPhys64(PSSMHANDLE pSSM, PRTGCPHYS64 pGCPhys)
6605{
6606 SSM_ASSERT_READABLE_RET(pSSM);
6607 SSM_CHECK_CANCELLED_RET(pSSM);
6608 return ssmR3DataRead(pSSM, pGCPhys, sizeof(*pGCPhys));
6609}
6610
6611
6612/**
6613 * Loads a GC physical address item from the current data unit.
6614 *
6615 * @returns VBox status.
6616 * @param pSSM The saved state handle.
6617 * @param pGCPhys Where to store the GC physical address.
6618 */
6619VMMR3DECL(int) SSMR3GetGCPhys(PSSMHANDLE pSSM, PRTGCPHYS pGCPhys)
6620{
6621 SSM_ASSERT_READABLE_RET(pSSM);
6622 SSM_CHECK_CANCELLED_RET(pSSM);
6623
6624 /*
6625 * Default size?
6626 */
6627 if (RT_LIKELY(sizeof(*pGCPhys) == pSSM->u.Read.cbGCPhys))
6628 return ssmR3DataRead(pSSM, pGCPhys, sizeof(*pGCPhys));
6629
6630 /*
6631 * Fiddly.
6632 */
6633 Assert(sizeof(*pGCPhys) == sizeof(uint64_t) || sizeof(*pGCPhys) == sizeof(uint32_t));
6634 Assert(pSSM->u.Read.cbGCPhys == sizeof(uint64_t) || pSSM->u.Read.cbGCPhys == sizeof(uint32_t));
6635 if (pSSM->u.Read.cbGCPhys == sizeof(uint64_t))
6636 {
6637 /* 64-bit saved, 32-bit load: try truncate it. */
6638 uint64_t u64;
6639 int rc = ssmR3DataRead(pSSM, &u64, sizeof(uint64_t));
6640 if (RT_FAILURE(rc))
6641 return rc;
6642 if (u64 >= _4G)
6643 return VERR_SSM_GCPHYS_OVERFLOW;
6644 *pGCPhys = (RTGCPHYS)u64;
6645 return rc;
6646 }
6647
6648 /* 32-bit saved, 64-bit load: clear the high part. */
6649 *pGCPhys = 0;
6650 return ssmR3DataRead(pSSM, pGCPhys, sizeof(uint32_t));
6651}
6652
6653
6654/**
6655 * Loads a GC virtual address item from the current data unit.
6656 *
6657 * Only applies to in the 1.1 format:
6658 * - SSMR3GetGCPtr
6659 * - SSMR3GetGCUIntPtr
6660 * - SSMR3GetGCUInt
6661 * - SSMR3GetGCUIntReg
6662 *
6663 * Put functions are not affected.
6664 *
6665 * @returns VBox status.
6666 * @param pSSM The saved state handle.
6667 * @param cbGCPtr Size of RTGCPTR
6668 *
6669 * @remarks This interface only works with saved state version 1.1, if the
6670 * format isn't 1.1 the call will be ignored.
6671 */
6672VMMR3_INT_DECL(int) SSMR3SetGCPtrSize(PSSMHANDLE pSSM, unsigned cbGCPtr)
6673{
6674 Assert(cbGCPtr == sizeof(RTGCPTR32) || cbGCPtr == sizeof(RTGCPTR64));
6675 if (!pSSM->u.Read.fFixedGCPtrSize)
6676 {
6677 Log(("SSMR3SetGCPtrSize: %u -> %u bytes\n", pSSM->u.Read.cbGCPtr, cbGCPtr));
6678 pSSM->u.Read.cbGCPtr = cbGCPtr;
6679 pSSM->u.Read.fFixedGCPtrSize = true;
6680 }
6681 else if ( pSSM->u.Read.cbGCPtr != cbGCPtr
6682 && pSSM->u.Read.uFmtVerMajor == 1
6683 && pSSM->u.Read.uFmtVerMinor == 1)
6684 AssertMsgFailed(("SSMR3SetGCPtrSize: already fixed at %u bytes; requested %u bytes\n", pSSM->u.Read.cbGCPtr, cbGCPtr));
6685
6686 return VINF_SUCCESS;
6687}
6688
6689
6690/**
6691 * Loads a GC virtual address item from the current data unit.
6692 *
6693 * @returns VBox status.
6694 * @param pSSM The saved state handle.
6695 * @param pGCPtr Where to store the GC virtual address.
6696 */
6697VMMR3DECL(int) SSMR3GetGCPtr(PSSMHANDLE pSSM, PRTGCPTR pGCPtr)
6698{
6699 SSM_ASSERT_READABLE_RET(pSSM);
6700 SSM_CHECK_CANCELLED_RET(pSSM);
6701
6702 /*
6703 * Default size?
6704 */
6705 if (RT_LIKELY(sizeof(*pGCPtr) == pSSM->u.Read.cbGCPtr))
6706 return ssmR3DataRead(pSSM, pGCPtr, sizeof(*pGCPtr));
6707
6708 /*
6709 * Fiddly.
6710 */
6711 Assert(sizeof(*pGCPtr) == sizeof(uint64_t) || sizeof(*pGCPtr) == sizeof(uint32_t));
6712 Assert(pSSM->u.Read.cbGCPtr == sizeof(uint64_t) || pSSM->u.Read.cbGCPtr == sizeof(uint32_t));
6713 if (pSSM->u.Read.cbGCPtr == sizeof(uint64_t))
6714 {
6715 /* 64-bit saved, 32-bit load: try truncate it. */
6716 uint64_t u64;
6717 int rc = ssmR3DataRead(pSSM, &u64, sizeof(uint64_t));
6718 if (RT_FAILURE(rc))
6719 return rc;
6720 if (u64 >= _4G)
6721 return VERR_SSM_GCPTR_OVERFLOW;
6722 *pGCPtr = (RTGCPTR)u64;
6723 return rc;
6724 }
6725
6726 /* 32-bit saved, 64-bit load: clear the high part. */
6727 *pGCPtr = 0;
6728 return ssmR3DataRead(pSSM, pGCPtr, sizeof(uint32_t));
6729}
6730
6731
6732/**
6733 * Loads a GC virtual address (represented as unsigned integer) item from the current data unit.
6734 *
6735 * @returns VBox status.
6736 * @param pSSM The saved state handle.
6737 * @param pGCPtr Where to store the GC virtual address.
6738 */
6739VMMR3DECL(int) SSMR3GetGCUIntPtr(PSSMHANDLE pSSM, PRTGCUINTPTR pGCPtr)
6740{
6741 AssertCompile(sizeof(RTGCPTR) == sizeof(*pGCPtr));
6742 return SSMR3GetGCPtr(pSSM, (PRTGCPTR)pGCPtr);
6743}
6744
6745
6746/**
6747 * Loads an RC virtual address item from the current data unit.
6748 *
6749 * @returns VBox status.
6750 * @param pSSM The saved state handle.
6751 * @param pRCPtr Where to store the RC virtual address.
6752 */
6753VMMR3DECL(int) SSMR3GetRCPtr(PSSMHANDLE pSSM, PRTRCPTR pRCPtr)
6754{
6755 SSM_ASSERT_READABLE_RET(pSSM);
6756 SSM_CHECK_CANCELLED_RET(pSSM);
6757 return ssmR3DataRead(pSSM, pRCPtr, sizeof(*pRCPtr));
6758}
6759
6760
6761/**
6762 * Loads a I/O port address item from the current data unit.
6763 *
6764 * @returns VBox status.
6765 * @param pSSM The saved state handle.
6766 * @param pIOPort Where to store the I/O port address.
6767 */
6768VMMR3DECL(int) SSMR3GetIOPort(PSSMHANDLE pSSM, PRTIOPORT pIOPort)
6769{
6770 SSM_ASSERT_READABLE_RET(pSSM);
6771 SSM_CHECK_CANCELLED_RET(pSSM);
6772 return ssmR3DataRead(pSSM, pIOPort, sizeof(*pIOPort));
6773}
6774
6775
6776/**
6777 * Loads a selector item from the current data unit.
6778 *
6779 * @returns VBox status.
6780 * @param pSSM The saved state handle.
6781 * @param pSel Where to store the selector.
6782 */
6783VMMR3DECL(int) SSMR3GetSel(PSSMHANDLE pSSM, PRTSEL pSel)
6784{
6785 SSM_ASSERT_READABLE_RET(pSSM);
6786 SSM_CHECK_CANCELLED_RET(pSSM);
6787 return ssmR3DataRead(pSSM, pSel, sizeof(*pSel));
6788}
6789
6790
6791/**
6792 * Loads a memory item from the current data unit.
6793 *
6794 * @returns VBox status.
6795 * @param pSSM The saved state handle.
6796 * @param pv Where to store the item.
6797 * @param cb Size of the item.
6798 */
6799VMMR3DECL(int) SSMR3GetMem(PSSMHANDLE pSSM, void *pv, size_t cb)
6800{
6801 SSM_ASSERT_READABLE_RET(pSSM);
6802 SSM_CHECK_CANCELLED_RET(pSSM);
6803 return ssmR3DataRead(pSSM, pv, cb);
6804}
6805
6806
6807/**
6808 * Loads a string item from the current data unit.
6809 *
6810 * @returns VBox status.
6811 * @param pSSM The saved state handle.
6812 * @param psz Where to store the item.
6813 * @param cbMax Max size of the item (including '\\0').
6814 */
6815VMMR3DECL(int) SSMR3GetStrZ(PSSMHANDLE pSSM, char *psz, size_t cbMax)
6816{
6817 return SSMR3GetStrZEx(pSSM, psz, cbMax, NULL);
6818}
6819
6820
6821/**
6822 * Loads a string item from the current data unit.
6823 *
6824 * @returns VBox status.
6825 * @param pSSM The saved state handle.
6826 * @param psz Where to store the item.
6827 * @param cbMax Max size of the item (including '\\0').
6828 * @param pcbStr The length of the loaded string excluding the '\\0'. (optional)
6829 */
6830VMMR3DECL(int) SSMR3GetStrZEx(PSSMHANDLE pSSM, char *psz, size_t cbMax, size_t *pcbStr)
6831{
6832 SSM_ASSERT_READABLE_RET(pSSM);
6833 SSM_CHECK_CANCELLED_RET(pSSM);
6834
6835 /* read size prefix. */
6836 uint32_t u32;
6837 int rc = SSMR3GetU32(pSSM, &u32);
6838 if (RT_SUCCESS(rc))
6839 {
6840 if (pcbStr)
6841 *pcbStr = u32;
6842 if (u32 < cbMax)
6843 {
6844 /* terminate and read string content. */
6845 psz[u32] = '\0';
6846 return ssmR3DataRead(pSSM, psz, u32);
6847 }
6848 return VERR_TOO_MUCH_DATA;
6849 }
6850 return rc;
6851}
6852
6853
6854/**
6855 * Skips a number of bytes in the current data unit.
6856 *
6857 * @returns VBox status code.
6858 * @param pSSM The SSM handle.
6859 * @param cb The number of bytes to skip.
6860 */
6861VMMR3DECL(int) SSMR3Skip(PSSMHANDLE pSSM, size_t cb)
6862{
6863 SSM_ASSERT_READABLE_RET(pSSM);
6864 SSM_CHECK_CANCELLED_RET(pSSM);
6865 while (cb > 0)
6866 {
6867 uint8_t abBuf[8192];
6868 size_t cbCur = RT_MIN(sizeof(abBuf), cb);
6869 cb -= cbCur;
6870 int rc = ssmR3DataRead(pSSM, abBuf, cbCur);
6871 if (RT_FAILURE(rc))
6872 return rc;
6873 }
6874
6875 return VINF_SUCCESS;
6876}
6877
6878
6879/**
6880 * Skips to the end of the current data unit.
6881 *
6882 * Since version 2 of the format, the load exec callback have to explicitly call
6883 * this API if it wish to be lazy for some reason. This is because there seldom
6884 * is a good reason to not read your entire data unit and it was hiding bugs.
6885 *
6886 * @returns VBox status code.
6887 * @param pSSM The saved state handle.
6888 */
6889VMMR3DECL(int) SSMR3SkipToEndOfUnit(PSSMHANDLE pSSM)
6890{
6891 SSM_ASSERT_READABLE_RET(pSSM);
6892 SSM_CHECK_CANCELLED_RET(pSSM);
6893 if (pSSM->u.Read.uFmtVerMajor >= 2)
6894 {
6895 /*
6896 * Read until we the end of data condition is raised.
6897 */
6898 pSSM->u.Read.cbDataBuffer = 0;
6899 pSSM->u.Read.offDataBuffer = 0;
6900 if (!pSSM->u.Read.fEndOfData)
6901 {
6902 do
6903 {
6904 /* read the rest of the current record */
6905 while (pSSM->u.Read.cbRecLeft)
6906 {
6907 uint8_t abBuf[8192];
6908 uint32_t cbToRead = RT_MIN(pSSM->u.Read.cbRecLeft, sizeof(abBuf));
6909 int rc = ssmR3DataReadV2Raw(pSSM, abBuf, cbToRead);
6910 if (RT_FAILURE(rc))
6911 return pSSM->rc = rc;
6912 pSSM->u.Read.cbRecLeft -= cbToRead;
6913 }
6914
6915 /* read the next header. */
6916 int rc = ssmR3DataReadRecHdrV2(pSSM);
6917 if (RT_FAILURE(rc))
6918 return pSSM->rc = rc;
6919 } while (!pSSM->u.Read.fEndOfData);
6920 }
6921 }
6922 /* else: Doesn't matter for the version 1 loading. */
6923
6924 return VINF_SUCCESS;
6925}
6926
6927
6928/**
6929 * Calculate the checksum of a file portion.
6930 *
6931 * @returns VBox status.
6932 * @param pStrm The stream handle
6933 * @param off Where to start checksumming.
6934 * @param cb How much to checksum.
6935 * @param pu32CRC Where to store the calculated checksum.
6936 */
6937static int ssmR3CalcChecksum(PSSMSTRM pStrm, uint64_t off, uint64_t cb, uint32_t *pu32CRC)
6938{
6939 /*
6940 * Allocate a buffer.
6941 */
6942 const size_t cbBuf = _32K;
6943 void *pvBuf = RTMemTmpAlloc(cbBuf);
6944 if (!pvBuf)
6945 return VERR_NO_TMP_MEMORY;
6946
6947 /*
6948 * Loop reading and calculating CRC32.
6949 */
6950 int rc = VINF_SUCCESS;
6951 uint32_t u32CRC = RTCrc32Start();
6952 while (cb > 0)
6953 {
6954 /* read chunk */
6955 size_t cbToRead = cbBuf;
6956 if (cb < cbBuf)
6957 cbToRead = cb;
6958 rc = ssmR3StrmPeekAt(pStrm, off, pvBuf, cbToRead, NULL);
6959 if (RT_FAILURE(rc))
6960 {
6961 AssertMsgFailed(("Failed with rc=%Rrc while calculating crc.\n", rc));
6962 RTMemTmpFree(pvBuf);
6963 return rc;
6964 }
6965
6966 /* advance */
6967 cb -= cbToRead;
6968 off += cbToRead;
6969
6970 /* calc crc32. */
6971 u32CRC = RTCrc32Process(u32CRC, pvBuf, cbToRead);
6972 }
6973 RTMemTmpFree(pvBuf);
6974
6975 /* store the calculated crc */
6976 u32CRC = RTCrc32Finish(u32CRC);
6977 Log(("SSM: u32CRC=0x%08x\n", u32CRC));
6978 *pu32CRC = u32CRC;
6979
6980 return VINF_SUCCESS;
6981}
6982
6983
6984/**
6985 * Validates a version 2 footer.
6986 *
6987 * @returns VBox status code.
6988 *
6989 * @param pFooter The footer.
6990 * @param offFooter The stream offset of the footer.
6991 * @param cDirEntries The number of directory entries. UINT32_MAX if
6992 * unknown.
6993 * @param fStreamCrc32 Whether the stream is checksummed using CRC-32.
6994 * @param u32StreamCRC The stream checksum.
6995 */
6996static int ssmR3ValidateFooter(PSSMFILEFTR pFooter, uint64_t offFooter, uint32_t cDirEntries, bool fStreamCrc32, uint32_t u32StreamCRC)
6997{
6998 if (memcmp(pFooter->szMagic, SSMFILEFTR_MAGIC, sizeof(pFooter->szMagic)))
6999 {
7000 LogRel(("SSM: Bad footer magic: %.*Rhxs\n", sizeof(pFooter->szMagic), &pFooter->szMagic[0]));
7001 return VERR_SSM_INTEGRITY_FOOTER;
7002 }
7003 SSM_CHECK_CRC32_RET(pFooter, sizeof(*pFooter), ("Footer CRC mismatch: %08x, correct is %08x\n", u32CRC, u32ActualCRC));
7004 if (pFooter->offStream != offFooter)
7005 {
7006 LogRel(("SSM: SSMFILEFTR::offStream is wrong: %llx, expected %llx\n", pFooter->offStream, offFooter));
7007 return VERR_SSM_INTEGRITY_FOOTER;
7008 }
7009 if (pFooter->u32Reserved)
7010 {
7011 LogRel(("SSM: Reserved footer field isn't zero: %08x\n", pFooter->u32Reserved));
7012 return VERR_SSM_INTEGRITY_FOOTER;
7013 }
7014 if (cDirEntries != UINT32_MAX)
7015 AssertLogRelMsgReturn(pFooter->cDirEntries == cDirEntries,
7016 ("Footer: cDirEntries=%#x, expected %#x\n", pFooter->cDirEntries, cDirEntries),
7017 VERR_SSM_INTEGRITY_FOOTER);
7018 else
7019 AssertLogRelMsgReturn(pFooter->cDirEntries < _64K,
7020 ("Footer: cDirEntries=%#x\n", pFooter->cDirEntries),
7021 VERR_SSM_INTEGRITY_FOOTER);
7022 if ( !fStreamCrc32
7023 && pFooter->u32StreamCRC)
7024 {
7025 LogRel(("SSM: u32StreamCRC field isn't zero, but header says stream checksumming is disabled.\n"));
7026 return VERR_SSM_INTEGRITY_FOOTER;
7027 }
7028 if ( fStreamCrc32
7029 && pFooter->u32StreamCRC != u32StreamCRC)
7030 {
7031 LogRel(("SSM: Bad stream CRC: %#x, expected %#x.\n", pFooter->u32StreamCRC, u32StreamCRC));
7032 return VERR_SSM_INTEGRITY_CRC;
7033 }
7034 return VINF_SUCCESS;
7035}
7036
7037
7038/**
7039 * Validates the header information stored in the handle.
7040 *
7041 * @returns VBox status code.
7042 *
7043 * @param pSSM The handle.
7044 * @param fHaveHostBits Set if the host bits field is valid.
7045 * @param fHaveVersion Set if we have a version.
7046 */
7047static int ssmR3ValidateHeaderInfo(PSSMHANDLE pSSM, bool fHaveHostBits, bool fHaveVersion)
7048{
7049 Assert(pSSM->u.Read.cbFileHdr < 256 && pSSM->u.Read.cbFileHdr > 32);
7050 Assert(pSSM->u.Read.uFmtVerMajor == 1 || pSSM->u.Read.uFmtVerMajor == 2);
7051 Assert(pSSM->u.Read.uFmtVerMinor <= 2);
7052
7053 if (fHaveVersion)
7054 {
7055 if ( pSSM->u.Read.u16VerMajor == 0
7056 || pSSM->u.Read.u16VerMajor > 1000
7057 || pSSM->u.Read.u16VerMinor > 1000
7058 || pSSM->u.Read.u32VerBuild > _1M
7059 || pSSM->u.Read.u32SvnRev == 0
7060 || pSSM->u.Read.u32SvnRev > 10000000 /*100M*/)
7061 {
7062 LogRel(("SSM: Incorrect version values: %u.%u.%u.r%u\n",
7063 pSSM->u.Read.u16VerMajor, pSSM->u.Read.u16VerMinor, pSSM->u.Read.u32VerBuild, pSSM->u.Read.u32SvnRev));
7064 return VERR_SSM_INTEGRITY_VBOX_VERSION;
7065 }
7066 }
7067 else
7068 AssertLogRelReturn( pSSM->u.Read.u16VerMajor == 0
7069 && pSSM->u.Read.u16VerMinor == 0
7070 && pSSM->u.Read.u32VerBuild == 0
7071 && pSSM->u.Read.u32SvnRev == 0,
7072 VERR_SSM_INTEGRITY_VBOX_VERSION);
7073
7074 if (fHaveHostBits)
7075 {
7076 if ( pSSM->u.Read.cHostBits != 32
7077 && pSSM->u.Read.cHostBits != 64)
7078 {
7079 LogRel(("SSM: Incorrect cHostBits value: %u\n", pSSM->u.Read.cHostBits));
7080 return VERR_SSM_INTEGRITY_HEADER;
7081 }
7082 }
7083 else
7084 AssertLogRelReturn(pSSM->u.Read.cHostBits == 0, VERR_SSM_INTEGRITY_HEADER);
7085
7086 if ( pSSM->u.Read.cbGCPhys != sizeof(uint32_t)
7087 && pSSM->u.Read.cbGCPhys != sizeof(uint64_t))
7088 {
7089 LogRel(("SSM: Incorrect cbGCPhys value: %d\n", pSSM->u.Read.cbGCPhys));
7090 return VERR_SSM_INTEGRITY_HEADER;
7091 }
7092 if ( pSSM->u.Read.cbGCPtr != sizeof(uint32_t)
7093 && pSSM->u.Read.cbGCPtr != sizeof(uint64_t))
7094 {
7095 LogRel(("SSM: Incorrect cbGCPtr value: %d\n", pSSM->u.Read.cbGCPtr));
7096 return VERR_SSM_INTEGRITY_HEADER;
7097 }
7098
7099 return VINF_SUCCESS;
7100}
7101
7102
7103/**
7104 * Reads the header, detects the format version and performs integrity
7105 * validations.
7106 *
7107 * @returns VBox status.
7108 * @param pSSM The saved state handle. A number of field will
7109 * be updated, mostly header related information.
7110 * fLiveSave is also set if appropriate.
7111 * @param fChecksumIt Whether to checksum the file or not. This will
7112 * be ignored if it the stream isn't a file.
7113 * @param fChecksumOnRead Whether to validate the checksum while reading
7114 * the stream instead of up front. If not possible,
7115 * verify the checksum up front.
7116 * @param pHdr Where to store the file header.
7117 */
7118static int ssmR3HeaderAndValidate(PSSMHANDLE pSSM, bool fChecksumIt, bool fChecksumOnRead)
7119{
7120 /*
7121 * Read and check the header magic.
7122 */
7123 union
7124 {
7125 SSMFILEHDR v2_0;
7126 SSMFILEHDRV12 v1_2;
7127 SSMFILEHDRV11 v1_1;
7128 } uHdr;
7129 int rc = ssmR3StrmRead(&pSSM->Strm, &uHdr, sizeof(uHdr.v2_0.szMagic));
7130 if (RT_FAILURE(rc))
7131 {
7132 LogRel(("SSM: Failed to read file magic header. rc=%Rrc\n", rc));
7133 return rc;
7134 }
7135 if (memcmp(uHdr.v2_0.szMagic, SSMFILEHDR_MAGIC_BASE, sizeof(SSMFILEHDR_MAGIC_BASE) - 1))
7136 {
7137 Log(("SSM: Not a saved state file. magic=%.*s\n", sizeof(uHdr.v2_0.szMagic) - 1, uHdr.v2_0.szMagic));
7138 return VERR_SSM_INTEGRITY_MAGIC;
7139 }
7140
7141 /*
7142 * Find the header size and read the rest.
7143 */
7144 static const struct
7145 {
7146 char szMagic[sizeof(SSMFILEHDR_MAGIC_V2_0)];
7147 uint32_t cbHdr;
7148 unsigned uFmtVerMajor;
7149 unsigned uFmtVerMinor;
7150 } s_aVers[] =
7151 {
7152 { SSMFILEHDR_MAGIC_V2_0, sizeof(SSMFILEHDR), 2, 0 },
7153 { SSMFILEHDR_MAGIC_V1_2, sizeof(SSMFILEHDRV12), 1, 2 },
7154 { SSMFILEHDR_MAGIC_V1_1, sizeof(SSMFILEHDRV11), 1, 1 },
7155 };
7156 int iVer = RT_ELEMENTS(s_aVers);
7157 while (iVer-- > 0)
7158 if (!memcmp(uHdr.v2_0.szMagic, s_aVers[iVer].szMagic, sizeof(uHdr.v2_0.szMagic)))
7159 break;
7160 if (iVer < 0)
7161 {
7162 Log(("SSM: Unknown file format version. magic=%.*s\n", sizeof(uHdr.v2_0.szMagic) - 1, uHdr.v2_0.szMagic));
7163 return VERR_SSM_INTEGRITY_VERSION;
7164 }
7165 pSSM->u.Read.uFmtVerMajor = s_aVers[iVer].uFmtVerMajor;
7166 pSSM->u.Read.uFmtVerMinor = s_aVers[iVer].uFmtVerMinor;
7167 pSSM->u.Read.cbFileHdr = s_aVers[iVer].cbHdr;
7168
7169 rc = ssmR3StrmRead(&pSSM->Strm, (uint8_t *)&uHdr + sizeof(uHdr.v2_0.szMagic), pSSM->u.Read.cbFileHdr - sizeof(uHdr.v2_0.szMagic));
7170 if (RT_FAILURE(rc))
7171 {
7172 LogRel(("SSM: Failed to read the file header. rc=%Rrc\n", rc));
7173 return rc;
7174 }
7175
7176 /*
7177 * Make version specific adjustments.
7178 */
7179 if (pSSM->u.Read.uFmtVerMajor >= 2)
7180 {
7181 /*
7182 * Version 2.0 and later.
7183 */
7184 if (pSSM->u.Read.uFmtVerMinor == 0)
7185 {
7186 /* validate the header. */
7187 SSM_CHECK_CRC32_RET(&uHdr.v2_0, sizeof(uHdr.v2_0), ("Header CRC mismatch: %08x, correct is %08x\n", u32CRC, u32ActualCRC));
7188 if (uHdr.v2_0.u8Reserved)
7189 {
7190 LogRel(("SSM: Reserved header field isn't zero: %02x\n", uHdr.v2_0.u8Reserved));
7191 return VERR_SSM_INTEGRITY;
7192 }
7193 if (uHdr.v2_0.fFlags & ~(SSMFILEHDR_FLAGS_STREAM_CRC32 | SSMFILEHDR_FLAGS_STREAM_LIVE_SAVE))
7194 {
7195 LogRel(("SSM: Unknown header flags: %08x\n", uHdr.v2_0.fFlags));
7196 return VERR_SSM_INTEGRITY;
7197 }
7198 if ( uHdr.v2_0.cbMaxDecompr > sizeof(pSSM->u.Read.abDataBuffer)
7199 || uHdr.v2_0.cbMaxDecompr < _1K
7200 || (uHdr.v2_0.cbMaxDecompr & 0xff) != 0)
7201 {
7202 LogRel(("SSM: The cbMaxDecompr header field is out of range: %#x\n", uHdr.v2_0.cbMaxDecompr));
7203 return VERR_SSM_INTEGRITY;
7204 }
7205
7206 /* set the header info. */
7207 pSSM->u.Read.cHostBits = uHdr.v2_0.cHostBits;
7208 pSSM->u.Read.u16VerMajor = uHdr.v2_0.u16VerMajor;
7209 pSSM->u.Read.u16VerMinor = uHdr.v2_0.u16VerMinor;
7210 pSSM->u.Read.u32VerBuild = uHdr.v2_0.u32VerBuild;
7211 pSSM->u.Read.u32SvnRev = uHdr.v2_0.u32SvnRev;
7212 pSSM->u.Read.cbGCPhys = uHdr.v2_0.cbGCPhys;
7213 pSSM->u.Read.cbGCPtr = uHdr.v2_0.cbGCPtr;
7214 pSSM->u.Read.fFixedGCPtrSize= true;
7215 pSSM->u.Read.fStreamCrc32 = !!(uHdr.v2_0.fFlags & SSMFILEHDR_FLAGS_STREAM_CRC32);
7216 pSSM->fLiveSave = !!(uHdr.v2_0.fFlags & SSMFILEHDR_FLAGS_STREAM_LIVE_SAVE);
7217 }
7218 else
7219 AssertFailedReturn(VERR_INTERNAL_ERROR);
7220 if (!pSSM->u.Read.fStreamCrc32)
7221 ssmR3StrmDisableChecksumming(&pSSM->Strm);
7222
7223 /*
7224 * Read and validate the footer if it's a file.
7225 */
7226 if (ssmR3StrmIsFile(&pSSM->Strm))
7227 {
7228 SSMFILEFTR Footer;
7229 uint64_t offFooter;
7230 rc = ssmR3StrmPeekAt(&pSSM->Strm, -(RTFOFF)sizeof(SSMFILEFTR), &Footer, sizeof(Footer), &offFooter);
7231 AssertLogRelRCReturn(rc, rc);
7232
7233 rc = ssmR3ValidateFooter(&Footer, offFooter, UINT32_MAX, pSSM->u.Read.fStreamCrc32, Footer.u32StreamCRC);
7234 if (RT_FAILURE(rc))
7235 return rc;
7236
7237 pSSM->u.Read.cbLoadFile = offFooter + sizeof(Footer);
7238 pSSM->u.Read.u32LoadCRC = Footer.u32StreamCRC;
7239 }
7240 else
7241 {
7242 pSSM->u.Read.cbLoadFile = UINT64_MAX;
7243 pSSM->u.Read.u32LoadCRC = 0;
7244 }
7245
7246 /*
7247 * Validate the header info we've set in the handle.
7248 */
7249 rc = ssmR3ValidateHeaderInfo(pSSM, true /*fHaveHostBits*/, true /*fHaveVersion*/);
7250 if (RT_FAILURE(rc))
7251 return rc;
7252
7253 /*
7254 * Check the checksum if that's called for and possible.
7255 */
7256 if ( pSSM->u.Read.fStreamCrc32
7257 && fChecksumIt
7258 && !fChecksumOnRead
7259 && ssmR3StrmIsFile(&pSSM->Strm))
7260 {
7261 uint32_t u32CRC;
7262 rc = ssmR3CalcChecksum(&pSSM->Strm, 0, pSSM->u.Read.cbLoadFile - sizeof(SSMFILEFTR), &u32CRC);
7263 if (RT_FAILURE(rc))
7264 return rc;
7265 if (u32CRC != pSSM->u.Read.u32LoadCRC)
7266 {
7267 LogRel(("SSM: Invalid CRC! Calculated %#010x, in footer %#010x\n", u32CRC, pSSM->u.Read.u32LoadCRC));
7268 return VERR_SSM_INTEGRITY_CRC;
7269 }
7270 }
7271 }
7272 else
7273 {
7274 /*
7275 * Version 1.x of the format.
7276 */
7277 bool fHaveHostBits = true;
7278 bool fHaveVersion = false;
7279 RTUUID MachineUuidFromHdr;
7280
7281 ssmR3StrmDisableChecksumming(&pSSM->Strm);
7282 if (pSSM->u.Read.uFmtVerMinor == 1)
7283 {
7284 pSSM->u.Read.cHostBits = 0; /* unknown */
7285 pSSM->u.Read.u16VerMajor = 0;
7286 pSSM->u.Read.u16VerMinor = 0;
7287 pSSM->u.Read.u32VerBuild = 0;
7288 pSSM->u.Read.u32SvnRev = 0;
7289 pSSM->u.Read.cbLoadFile = uHdr.v1_1.cbFile;
7290 pSSM->u.Read.u32LoadCRC = uHdr.v1_1.u32CRC;
7291 pSSM->u.Read.cbGCPhys = sizeof(RTGCPHYS);
7292 pSSM->u.Read.cbGCPtr = sizeof(RTGCPTR);
7293 pSSM->u.Read.fFixedGCPtrSize = false; /* settable */
7294 pSSM->u.Read.fStreamCrc32 = false;
7295
7296 MachineUuidFromHdr = uHdr.v1_1.MachineUuid;
7297 fHaveHostBits = false;
7298 }
7299 else if (pSSM->u.Read.uFmtVerMinor == 2)
7300 {
7301 pSSM->u.Read.cHostBits = uHdr.v1_2.cHostBits;
7302 pSSM->u.Read.u16VerMajor = uHdr.v1_2.u16VerMajor;
7303 pSSM->u.Read.u16VerMinor = uHdr.v1_2.u16VerMinor;
7304 pSSM->u.Read.u32VerBuild = uHdr.v1_2.u32VerBuild;
7305 pSSM->u.Read.u32SvnRev = uHdr.v1_2.u32SvnRev;
7306 pSSM->u.Read.cbLoadFile = uHdr.v1_2.cbFile;
7307 pSSM->u.Read.u32LoadCRC = uHdr.v1_2.u32CRC;
7308 pSSM->u.Read.cbGCPhys = uHdr.v1_2.cbGCPhys;
7309 pSSM->u.Read.cbGCPtr = uHdr.v1_2.cbGCPtr;
7310 pSSM->u.Read.fFixedGCPtrSize = true;
7311 pSSM->u.Read.fStreamCrc32 = false;
7312
7313 MachineUuidFromHdr = uHdr.v1_2.MachineUuid;
7314 fHaveVersion = true;
7315 }
7316 else
7317 AssertFailedReturn(VERR_INTERNAL_ERROR);
7318
7319 /*
7320 * The MachineUuid must be NULL (was never used).
7321 */
7322 if (!RTUuidIsNull(&MachineUuidFromHdr))
7323 {
7324 LogRel(("SSM: The UUID of the saved state doesn't match the running VM.\n"));
7325 return VERR_SMM_INTEGRITY_MACHINE;
7326 }
7327
7328 /*
7329 * Verify the file size.
7330 */
7331 uint64_t cbFile = ssmR3StrmGetSize(&pSSM->Strm);
7332 if (cbFile != pSSM->u.Read.cbLoadFile)
7333 {
7334 LogRel(("SSM: File size mismatch. hdr.cbFile=%lld actual %lld\n", pSSM->u.Read.cbLoadFile, cbFile));
7335 return VERR_SSM_INTEGRITY_SIZE;
7336 }
7337
7338 /*
7339 * Validate the header info we've set in the handle.
7340 */
7341 rc = ssmR3ValidateHeaderInfo(pSSM, fHaveHostBits, fHaveVersion);
7342 if (RT_FAILURE(rc))
7343 return rc;
7344
7345 /*
7346 * Verify the checksum if requested.
7347 *
7348 * Note! The checksum is not actually generated for the whole file,
7349 * this is of course a bug in the v1.x code that we cannot do
7350 * anything about.
7351 */
7352 if ( fChecksumIt
7353 || fChecksumOnRead)
7354 {
7355 uint32_t u32CRC;
7356 rc = ssmR3CalcChecksum(&pSSM->Strm,
7357 RT_OFFSETOF(SSMFILEHDRV11, u32CRC) + sizeof(uHdr.v1_1.u32CRC),
7358 cbFile - pSSM->u.Read.cbFileHdr,
7359 &u32CRC);
7360 if (RT_FAILURE(rc))
7361 return rc;
7362 if (u32CRC != pSSM->u.Read.u32LoadCRC)
7363 {
7364 LogRel(("SSM: Invalid CRC! Calculated %#010x, in header %#010x\n", u32CRC, pSSM->u.Read.u32LoadCRC));
7365 return VERR_SSM_INTEGRITY_CRC;
7366 }
7367 }
7368 }
7369
7370 return VINF_SUCCESS;
7371}
7372
7373
7374/**
7375 * Open a saved state for reading.
7376 *
7377 * The file will be positioned at the first data unit upon successful return.
7378 *
7379 * @returns VBox status code.
7380 *
7381 * @param pVM The VM handle.
7382 * @param pszFilename The filename. NULL if pStreamOps is used.
7383 * @param pStreamOps The stream method table. NULL if pszFilename is
7384 * used.
7385 * @param pvUser The user argument to the stream methods.
7386 * @param fChecksumIt Check the checksum for the entire file.
7387 * @param fChecksumOnRead Whether to validate the checksum while reading
7388 * the stream instead of up front. If not possible,
7389 * verify the checksum up front.
7390 * @param pSSM Pointer to the handle structure. This will be
7391 * completely initialized on success.
7392 * @param cBuffers The number of stream buffers.
7393 */
7394static int ssmR3OpenFile(PVM pVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvUser,
7395 bool fChecksumIt, bool fChecksumOnRead, uint32_t cBuffers, PSSMHANDLE pSSM)
7396{
7397 /*
7398 * Initialize the handle.
7399 */
7400 pSSM->pVM = pVM;
7401 pSSM->enmOp = SSMSTATE_INVALID;
7402 pSSM->enmAfter = SSMAFTER_INVALID;
7403 pSSM->fCancelled = SSMHANDLE_OK;
7404 pSSM->rc = VINF_SUCCESS;
7405 pSSM->cbUnitLeftV1 = 0;
7406 pSSM->offUnit = UINT64_MAX;
7407 pSSM->fLiveSave = false;
7408 pSSM->pfnProgress = NULL;
7409 pSSM->pvUser = NULL;
7410 pSSM->uPercent = 0;
7411 pSSM->offEstProgress = 0;
7412 pSSM->cbEstTotal = 0;
7413 pSSM->offEst = 0;
7414 pSSM->offEstUnitEnd = 0;
7415 pSSM->uPercentPrepare = 5;
7416 pSSM->uPercentDone = 2;
7417 pSSM->pszFilename = pszFilename;
7418
7419 pSSM->u.Read.pZipDecompV1 = NULL;
7420 pSSM->u.Read.uFmtVerMajor = UINT32_MAX;
7421 pSSM->u.Read.uFmtVerMinor = UINT32_MAX;
7422 pSSM->u.Read.cbFileHdr = UINT32_MAX;
7423 pSSM->u.Read.cbGCPhys = UINT8_MAX;
7424 pSSM->u.Read.cbGCPtr = UINT8_MAX;
7425 pSSM->u.Read.fFixedGCPtrSize= false;
7426 pSSM->u.Read.fIsHostMsc32 = SSM_HOST_IS_MSC_32;
7427 RT_ZERO(pSSM->u.Read.szHostOSAndArch);
7428 pSSM->u.Read.u16VerMajor = UINT16_MAX;
7429 pSSM->u.Read.u16VerMinor = UINT16_MAX;
7430 pSSM->u.Read.u32VerBuild = UINT32_MAX;
7431 pSSM->u.Read.u32SvnRev = UINT32_MAX;
7432 pSSM->u.Read.cHostBits = UINT8_MAX;
7433 pSSM->u.Read.cbLoadFile = UINT64_MAX;
7434
7435 pSSM->u.Read.cbRecLeft = 0;
7436 pSSM->u.Read.cbDataBuffer = 0;
7437 pSSM->u.Read.offDataBuffer = 0;
7438 pSSM->u.Read.fEndOfData = 0;
7439 pSSM->u.Read.u8TypeAndFlags = 0;
7440
7441 pSSM->u.Read.pCurUnit = NULL;
7442 pSSM->u.Read.uCurUnitVer = UINT32_MAX;
7443 pSSM->u.Read.uCurUnitPass = 0;
7444 pSSM->u.Read.fHaveSetError = false;
7445
7446 /*
7447 * Try open and validate the file.
7448 */
7449 int rc;
7450 if (pStreamOps)
7451 rc = ssmR3StrmInit(&pSSM->Strm, pStreamOps, pvUser, false /*fWrite*/, fChecksumOnRead, cBuffers);
7452 else
7453 rc = ssmR3StrmOpenFile(&pSSM->Strm, pszFilename, false /*fWrite*/, fChecksumOnRead, cBuffers);
7454 if (RT_SUCCESS(rc))
7455 {
7456 rc = ssmR3HeaderAndValidate(pSSM, fChecksumIt, fChecksumOnRead);
7457 if (RT_SUCCESS(rc))
7458 return rc;
7459
7460 /* failure path */
7461 ssmR3StrmClose(&pSSM->Strm, pSSM->rc == VERR_SSM_CANCELLED);
7462 }
7463 else
7464 Log(("SSM: Failed to open save state file '%s', rc=%Rrc.\n", pszFilename, rc));
7465 return rc;
7466}
7467
7468
7469/**
7470 * Verifies the directory.
7471 *
7472 * @returns VBox status code.
7473 *
7474 * @param pDir The full directory.
7475 * @param cbDir The size of the directory.
7476 * @param offDir The directory stream offset.
7477 * @param cDirEntries The directory entry count from the footer.
7478 * @param cbHdr The header size.
7479 * @param uSvnRev The SVN revision that saved the state. Bug detection.
7480 */
7481static int ssmR3ValidateDirectory(PSSMFILEDIR pDir, size_t cbDir, uint64_t offDir, uint32_t cDirEntries,
7482 uint32_t cbHdr, uint32_t uSvnRev)
7483{
7484 AssertLogRelReturn(!memcmp(pDir->szMagic, SSMFILEDIR_MAGIC, sizeof(pDir->szMagic)), VERR_SSM_INTEGRITY_DIR_MAGIC);
7485 SSM_CHECK_CRC32_RET(pDir, cbDir, ("Bad directory CRC: %08x, actual %08x\n", u32CRC, u32ActualCRC));
7486 AssertLogRelMsgReturn(pDir->cEntries == cDirEntries,
7487 ("Bad directory entry count: %#x, expected %#x (from the footer)\n", pDir->cEntries, cDirEntries),
7488 VERR_SSM_INTEGRITY_DIR);
7489 AssertLogRelReturn(RT_UOFFSETOF(SSMFILEDIR, aEntries[pDir->cEntries]) == cbDir, VERR_SSM_INTEGRITY_DIR);
7490
7491 for (uint32_t i = 0; i < pDir->cEntries; i++)
7492 {
7493 AssertLogRelMsgReturn( ( pDir->aEntries[i].off >= cbHdr
7494 && pDir->aEntries[i].off < offDir)
7495 || ( pDir->aEntries[i].off == 0 /* bug in unreleased code */
7496 && uSvnRev < 53365),
7497 ("off=%#llx cbHdr=%#x offDir=%#llx\n", pDir->aEntries[i].off, cbHdr, offDir),
7498 VERR_SSM_INTEGRITY_DIR);
7499 }
7500 return VINF_SUCCESS;
7501}
7502
7503#ifndef SSM_STANDALONE
7504
7505/**
7506 * Find a data unit by name.
7507 *
7508 * @returns Pointer to the unit.
7509 * @returns NULL if not found.
7510 *
7511 * @param pVM VM handle.
7512 * @param pszName Data unit name.
7513 * @param uInstance The data unit instance id.
7514 */
7515static PSSMUNIT ssmR3Find(PVM pVM, const char *pszName, uint32_t uInstance)
7516{
7517 size_t cchName = strlen(pszName);
7518 PSSMUNIT pUnit = pVM->ssm.s.pHead;
7519 while ( pUnit
7520 && ( pUnit->u32Instance != uInstance
7521 || pUnit->cchName != cchName
7522 || memcmp(pUnit->szName, pszName, cchName)))
7523 pUnit = pUnit->pNext;
7524 return pUnit;
7525}
7526
7527
7528/**
7529 * Executes the loading of a V1.X file.
7530 *
7531 * @returns VBox status code.
7532 * @param pVM The VM handle.
7533 * @param pSSM The saved state handle.
7534 */
7535static int ssmR3LoadExecV1(PVM pVM, PSSMHANDLE pSSM)
7536{
7537 int rc;
7538 char *pszName = NULL;
7539 size_t cchName = 0;
7540 pSSM->enmOp = SSMSTATE_LOAD_EXEC;
7541 for (;;)
7542 {
7543 /*
7544 * Save the current file position and read the data unit header.
7545 */
7546 uint64_t offUnit = ssmR3StrmTell(&pSSM->Strm);
7547 SSMFILEUNITHDRV1 UnitHdr;
7548 rc = ssmR3StrmRead(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV1, szName));
7549 if (RT_SUCCESS(rc))
7550 {
7551 /*
7552 * Check the magic and see if it's valid and whether it is a end header or not.
7553 */
7554 if (memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(SSMFILEUNITHDR_MAGIC)))
7555 {
7556 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_END, sizeof(SSMFILEUNITHDR_END)))
7557 {
7558 Log(("SSM: EndOfFile: offset %#9llx size %9d\n", offUnit, UnitHdr.cbUnit));
7559 /* Complete the progress bar (pending 99% afterwards). */
7560 ssmR3Progress(pSSM, pSSM->cbEstTotal - pSSM->offEst);
7561 break;
7562 }
7563 LogRel(("SSM: Invalid unit magic at offset %#llx (%lld), '%.*s'!\n",
7564 offUnit, offUnit, sizeof(UnitHdr.achMagic) - 1, &UnitHdr.achMagic[0]));
7565 rc = VERR_SSM_INTEGRITY_UNIT_MAGIC;
7566 break;
7567 }
7568
7569 /*
7570 * Read the name.
7571 * Adjust the name buffer first.
7572 */
7573 if (cchName < UnitHdr.cchName)
7574 {
7575 if (pszName)
7576 RTMemTmpFree(pszName);
7577 cchName = RT_ALIGN_Z(UnitHdr.cchName, 64);
7578 pszName = (char *)RTMemTmpAlloc(cchName);
7579 }
7580 if (pszName)
7581 {
7582 rc = ssmR3StrmRead(&pSSM->Strm, pszName, UnitHdr.cchName);
7583 if (RT_SUCCESS(rc))
7584 {
7585 if (pszName[UnitHdr.cchName - 1])
7586 {
7587 LogRel(("SSM: Unit name '%.*s' was not properly terminated.\n", UnitHdr.cchName, pszName));
7588 rc = VERR_SSM_INTEGRITY_UNIT;
7589 break;
7590 }
7591 Log(("SSM: Data unit: offset %#9llx size %9lld '%s'\n", offUnit, UnitHdr.cbUnit, pszName));
7592
7593 /*
7594 * Find the data unit in our internal table.
7595 */
7596 PSSMUNIT pUnit = ssmR3Find(pVM, pszName, UnitHdr.u32Instance);
7597 if (pUnit)
7598 {
7599 /*
7600 * Call the execute handler.
7601 */
7602 pSSM->cbUnitLeftV1 = UnitHdr.cbUnit - RT_OFFSETOF(SSMFILEUNITHDRV1, szName[UnitHdr.cchName]);
7603 pSSM->offUnit = 0;
7604 pSSM->u.Read.uCurUnitVer = UnitHdr.u32Version;
7605 pSSM->u.Read.uCurUnitPass = SSM_PASS_FINAL;
7606 pSSM->u.Read.pCurUnit = pUnit;
7607 if (!pUnit->u.Common.pfnLoadExec)
7608 {
7609 LogRel(("SSM: No load exec callback for unit '%s'!\n", pszName));
7610 pSSM->rc = rc = VERR_SSM_NO_LOAD_EXEC;
7611 break;
7612 }
7613 switch (pUnit->enmType)
7614 {
7615 case SSMUNITTYPE_DEV:
7616 rc = pUnit->u.Dev.pfnLoadExec(pUnit->u.Dev.pDevIns, pSSM, UnitHdr.u32Version, SSM_PASS_FINAL);
7617 break;
7618 case SSMUNITTYPE_DRV:
7619 rc = pUnit->u.Drv.pfnLoadExec(pUnit->u.Drv.pDrvIns, pSSM, UnitHdr.u32Version, SSM_PASS_FINAL);
7620 break;
7621 case SSMUNITTYPE_INTERNAL:
7622 rc = pUnit->u.Internal.pfnLoadExec(pVM, pSSM, UnitHdr.u32Version, SSM_PASS_FINAL);
7623 break;
7624 case SSMUNITTYPE_EXTERNAL:
7625 rc = pUnit->u.External.pfnLoadExec(pSSM, pUnit->u.External.pvUser, UnitHdr.u32Version, SSM_PASS_FINAL);
7626 break;
7627 default:
7628 rc = VERR_INTERNAL_ERROR;
7629 break;
7630 }
7631 pUnit->fCalled = true;
7632 if (RT_FAILURE(rc) && RT_SUCCESS_NP(pSSM->rc))
7633 pSSM->rc = rc;
7634
7635 /*
7636 * Close the reader stream.
7637 */
7638 rc = ssmR3DataReadFinishV1(pSSM);
7639 if (RT_SUCCESS(rc))
7640 {
7641 /*
7642 * Now, we'll check the current position to see if all, or
7643 * more than all, the data was read.
7644 *
7645 * Note! Because of buffering / compression we'll only see the
7646 * really bad ones here.
7647 */
7648 uint64_t off = ssmR3StrmTell(&pSSM->Strm);
7649 int64_t i64Diff = off - (offUnit + UnitHdr.cbUnit);
7650 if (i64Diff < 0)
7651 {
7652 Log(("SSM: Unit '%s' left %lld bytes unread!\n", pszName, -i64Diff));
7653 rc = ssmR3StrmSkipTo(&pSSM->Strm, offUnit + UnitHdr.cbUnit);
7654 ssmR3Progress(pSSM, offUnit + UnitHdr.cbUnit - pSSM->offEst);
7655 }
7656 else if (i64Diff > 0)
7657 {
7658 LogRel(("SSM: Unit '%s' read %lld bytes too much!\n", pszName, i64Diff));
7659 if (!ASMAtomicXchgBool(&pSSM->u.Read.fHaveSetError, true))
7660 rc = VMSetError(pVM, VERR_SSM_LOADED_TOO_MUCH, RT_SRC_POS,
7661 N_("Unit '%s' read %lld bytes too much"), pszName, i64Diff);
7662 break;
7663 }
7664
7665 pSSM->offUnit = UINT64_MAX;
7666 }
7667 else
7668 {
7669 LogRel(("SSM: Load exec failed for '%s' instance #%u ! (version %u)\n",
7670 pszName, UnitHdr.u32Instance, UnitHdr.u32Version));
7671 if (!ASMAtomicXchgBool(&pSSM->u.Read.fHaveSetError, true))
7672 {
7673 if (rc == VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION)
7674 VMSetError(pVM, rc, RT_SRC_POS, N_("Unsupported version %u of data unit '%s' (instance #%u)"),
7675 UnitHdr.u32Version, UnitHdr.szName, UnitHdr.u32Instance);
7676 else
7677 VMSetError(pVM, rc, RT_SRC_POS, N_("Load exec failed for '%s' instance #%u (version %u)"),
7678 pszName, UnitHdr.u32Instance, UnitHdr.u32Version);
7679 }
7680 break;
7681 }
7682
7683 pSSM->u.Read.pCurUnit = NULL;
7684 pSSM->u.Read.uCurUnitVer = UINT32_MAX;
7685 pSSM->u.Read.uCurUnitPass = 0;
7686 }
7687 else
7688 {
7689 /*
7690 * SSM unit wasn't found - ignore this when loading for the debugger.
7691 */
7692 LogRel(("SSM: Found no handler for unit '%s'!\n", pszName));
7693 rc = VERR_SSM_INTEGRITY_UNIT_NOT_FOUND;
7694 if (pSSM->enmAfter != SSMAFTER_DEBUG_IT)
7695 break;
7696 rc = ssmR3StrmSkipTo(&pSSM->Strm, offUnit + UnitHdr.cbUnit);
7697 }
7698 }
7699 }
7700 else
7701 rc = VERR_NO_TMP_MEMORY;
7702 }
7703
7704 /*
7705 * I/O errors ends up here (yea, I know, very nice programming).
7706 */
7707 if (RT_FAILURE(rc))
7708 {
7709 LogRel(("SSM: I/O error. rc=%Rrc\n", rc));
7710 break;
7711 }
7712
7713 /*
7714 * Check for cancellation.
7715 */
7716 if (RT_UNLIKELY(ASMAtomicUoReadU32(&(pSSM)->fCancelled) == SSMHANDLE_CANCELLED))
7717 {
7718 LogRel(("SSM: Cancelled!n"));
7719 rc = pSSM->rc;
7720 if (RT_SUCCESS(pSSM->rc))
7721 pSSM->rc = rc = VERR_SSM_CANCELLED;
7722 break;
7723 }
7724 }
7725
7726 RTMemTmpFree(pszName);
7727 return rc;
7728}
7729
7730
7731/**
7732 * Reads and verifies the directory and footer.
7733 *
7734 * @returns VBox status code.
7735 * @param pSSM The saved state handle.
7736 */
7737static int ssmR3LoadDirectoryAndFooter(PSSMHANDLE pSSM)
7738{
7739 /*
7740 * The directory.
7741 *
7742 * Get the header containing the number of entries first. Then read the
7743 * entries and pass the combined block to the validation function.
7744 */
7745 uint64_t off = ssmR3StrmTell(&pSSM->Strm);
7746 size_t const cbDirHdr = RT_OFFSETOF(SSMFILEDIR, aEntries);
7747 SSMFILEDIR DirHdr;
7748 int rc = ssmR3StrmRead(&pSSM->Strm, &DirHdr, cbDirHdr);
7749 if (RT_FAILURE(rc))
7750 return rc;
7751 AssertLogRelMsgReturn(!memcmp(DirHdr.szMagic, SSMFILEDIR_MAGIC, sizeof(DirHdr.szMagic)),
7752 ("Invalid directory magic at %#llx (%lld): %.*Rhxs\n", off, off, sizeof(DirHdr.szMagic), DirHdr.szMagic),
7753 VERR_SSM_INTEGRITY_DIR_MAGIC);
7754 AssertLogRelMsgReturn(DirHdr.cEntries < _64K,
7755 ("Too many directory entries at %#llx (%lld): %#x\n", off, off, DirHdr.cEntries),
7756 VERR_SSM_INTEGRITY_DIR);
7757
7758 size_t cbDir = RT_OFFSETOF(SSMFILEDIR, aEntries[DirHdr.cEntries]);
7759 PSSMFILEDIR pDir = (PSSMFILEDIR)RTMemTmpAlloc(cbDir);
7760 if (!pDir)
7761 return VERR_NO_TMP_MEMORY;
7762 memcpy(pDir, &DirHdr, cbDirHdr);
7763 rc = ssmR3StrmRead(&pSSM->Strm, (uint8_t *)pDir + cbDirHdr, cbDir - cbDirHdr);
7764 if (RT_SUCCESS(rc))
7765 rc = ssmR3ValidateDirectory(pDir, cbDir, off, DirHdr.cEntries, pSSM->u.Read.cbFileHdr, pSSM->u.Read.u32SvnRev);
7766 RTMemTmpFree(pDir);
7767 if (RT_FAILURE(rc))
7768 return rc;
7769
7770 /*
7771 * Read and validate the footer.
7772 */
7773 off = ssmR3StrmTell(&pSSM->Strm);
7774 uint32_t u32StreamCRC = ssmR3StrmFinalCRC(&pSSM->Strm);
7775 SSMFILEFTR Footer;
7776 rc = ssmR3StrmRead(&pSSM->Strm, &Footer, sizeof(Footer));
7777 if (RT_FAILURE(rc))
7778 return rc;
7779 return ssmR3ValidateFooter(&Footer, off, DirHdr.cEntries, pSSM->u.Read.fStreamCrc32, u32StreamCRC);
7780}
7781
7782
7783/**
7784 * Executes the loading of a V2.X file.
7785 *
7786 * @returns VBox status code. May or may not set pSSM->rc, the returned
7787 * status code is ALWAYS the more accurate of the two.
7788 * @param pVM The VM handle.
7789 * @param pSSM The saved state handle.
7790 */
7791static int ssmR3LoadExecV2(PVM pVM, PSSMHANDLE pSSM)
7792{
7793 pSSM->enmOp = SSMSTATE_LOAD_EXEC;
7794 for (;;)
7795 {
7796 /*
7797 * Read the unit header and check its integrity.
7798 */
7799 uint64_t offUnit = ssmR3StrmTell(&pSSM->Strm);
7800 uint32_t u32CurStreamCRC = ssmR3StrmCurCRC(&pSSM->Strm);
7801 SSMFILEUNITHDRV2 UnitHdr;
7802 int rc = ssmR3StrmRead(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName));
7803 if (RT_FAILURE(rc))
7804 return rc;
7805 if (RT_UNLIKELY( memcmp(&UnitHdr.szMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(UnitHdr.szMagic))
7806 && memcmp(&UnitHdr.szMagic[0], SSMFILEUNITHDR_END, sizeof(UnitHdr.szMagic))))
7807 {
7808 LogRel(("SSM: Unit at %#llx (%lld): Invalid unit magic: %.*Rhxs!\n",
7809 offUnit, offUnit, sizeof(UnitHdr.szMagic) - 1, &UnitHdr.szMagic[0]));
7810 pSSM->u.Read.fHaveSetError = true;
7811 return VMSetError(pVM, VERR_SSM_INTEGRITY_UNIT_MAGIC, RT_SRC_POS,
7812 N_("Unit at %#llx (%lld): Invalid unit magic"), offUnit, offUnit);
7813 }
7814 if (UnitHdr.cbName)
7815 {
7816 AssertLogRelMsgReturn(UnitHdr.cbName <= sizeof(UnitHdr.szName),
7817 ("Unit at %#llx (%lld): UnitHdr.cbName=%u > %u\n",
7818 offUnit, offUnit, UnitHdr.cbName, sizeof(UnitHdr.szName)),
7819 VERR_SSM_INTEGRITY_UNIT);
7820 rc = ssmR3StrmRead(&pSSM->Strm, &UnitHdr.szName[0], UnitHdr.cbName);
7821 if (RT_FAILURE(rc))
7822 return rc;
7823 AssertLogRelMsgReturn(!UnitHdr.szName[UnitHdr.cbName - 1],
7824 ("Unit at %#llx (%lld): Name %.*Rhxs was not properly terminated.\n",
7825 offUnit, offUnit, UnitHdr.cbName, UnitHdr.szName),
7826 VERR_SSM_INTEGRITY_UNIT);
7827 }
7828 SSM_CHECK_CRC32_RET(&UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]),
7829 ("Unit at %#llx (%lld): CRC mismatch: %08x, correct is %08x\n", offUnit, offUnit, u32CRC, u32ActualCRC));
7830 AssertLogRelMsgReturn(UnitHdr.offStream == offUnit,
7831 ("Unit at %#llx (%lld): offStream=%#llx, expected %#llx\n", offUnit, offUnit, UnitHdr.offStream, offUnit),
7832 VERR_SSM_INTEGRITY_UNIT);
7833 AssertLogRelMsgReturn(UnitHdr.u32CurStreamCRC == u32CurStreamCRC || !pSSM->Strm.fChecksummed,
7834 ("Unit at %#llx (%lld): Stream CRC mismatch: %08x, correct is %08x\n", offUnit, offUnit, UnitHdr.u32CurStreamCRC, u32CurStreamCRC),
7835 VERR_SSM_INTEGRITY_UNIT);
7836 AssertLogRelMsgReturn(!UnitHdr.fFlags, ("Unit at %#llx (%lld): fFlags=%08x\n", offUnit, offUnit, UnitHdr.fFlags),
7837 VERR_SSM_INTEGRITY_UNIT);
7838 if (!memcmp(&UnitHdr.szMagic[0], SSMFILEUNITHDR_END, sizeof(UnitHdr.szMagic)))
7839 {
7840 AssertLogRelMsgReturn( UnitHdr.cbName == 0
7841 && UnitHdr.u32Instance == 0
7842 && UnitHdr.u32Version == 0
7843 && UnitHdr.u32Pass == SSM_PASS_FINAL,
7844 ("Unit at %#llx (%lld): Malformed END unit\n", offUnit, offUnit),
7845 VERR_SSM_INTEGRITY_UNIT);
7846
7847 /*
7848 * Complete the progress bar (pending 99% afterwards) and RETURN.
7849 */
7850 Log(("SSM: Unit at %#9llx: END UNIT\n", offUnit));
7851 ssmR3Progress(pSSM, pSSM->cbEstTotal - pSSM->offEst);
7852
7853 return ssmR3LoadDirectoryAndFooter(pSSM);
7854 }
7855 AssertLogRelMsgReturn(UnitHdr.cbName > 1, ("Unit at %#llx (%lld): No name\n", offUnit, offUnit), VERR_SSM_INTEGRITY);
7856
7857 Log(("SSM: Unit at %#9llx: '%s', instance %u, pass %#x, version %u\n",
7858 offUnit, UnitHdr.szName, UnitHdr.u32Instance, UnitHdr.u32Pass, UnitHdr.u32Version));
7859
7860 /*
7861 * Find the data unit in our internal table.
7862 */
7863 PSSMUNIT pUnit = ssmR3Find(pVM, UnitHdr.szName, UnitHdr.u32Instance);
7864 if (pUnit)
7865 {
7866 /*
7867 * Call the execute handler.
7868 */
7869 AssertLogRelMsgReturn(pUnit->u.Common.pfnLoadExec,
7870 ("SSM: No load exec callback for unit '%s'!\n", UnitHdr.szName),
7871 VERR_SSM_NO_LOAD_EXEC);
7872 pSSM->u.Read.uCurUnitVer = UnitHdr.u32Version;
7873 pSSM->u.Read.uCurUnitPass = UnitHdr.u32Pass;
7874 pSSM->u.Read.pCurUnit = pUnit;
7875 ssmR3DataReadBeginV2(pSSM);
7876 switch (pUnit->enmType)
7877 {
7878 case SSMUNITTYPE_DEV:
7879 rc = pUnit->u.Dev.pfnLoadExec(pUnit->u.Dev.pDevIns, pSSM, UnitHdr.u32Version, UnitHdr.u32Pass);
7880 break;
7881 case SSMUNITTYPE_DRV:
7882 rc = pUnit->u.Drv.pfnLoadExec(pUnit->u.Drv.pDrvIns, pSSM, UnitHdr.u32Version, UnitHdr.u32Pass);
7883 break;
7884 case SSMUNITTYPE_INTERNAL:
7885 rc = pUnit->u.Internal.pfnLoadExec(pVM, pSSM, UnitHdr.u32Version, UnitHdr.u32Pass);
7886 break;
7887 case SSMUNITTYPE_EXTERNAL:
7888 rc = pUnit->u.External.pfnLoadExec(pSSM, pUnit->u.External.pvUser, UnitHdr.u32Version, UnitHdr.u32Pass);
7889 break;
7890 default:
7891 rc = VERR_INTERNAL_ERROR;
7892 break;
7893 }
7894 pUnit->fCalled = true;
7895 if (RT_FAILURE(rc) && RT_SUCCESS_NP(pSSM->rc))
7896 pSSM->rc = rc;
7897 rc = ssmR3DataReadFinishV2(pSSM);
7898 if (RT_SUCCESS(rc))
7899 pSSM->offUnit = UINT64_MAX;
7900 else
7901 {
7902 LogRel(("SSM: LoadExec failed for '%s' instance #%u (version %u, pass %#x): %Rrc\n",
7903 UnitHdr.szName, UnitHdr.u32Instance, UnitHdr.u32Version, UnitHdr.u32Pass, rc));
7904 if (!ASMAtomicXchgBool(&pSSM->u.Read.fHaveSetError, true))
7905 {
7906 if (rc == VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION)
7907 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Unsupported version %u of data unit '%s' (instance #%u, pass %#x)"),
7908 UnitHdr.u32Version, UnitHdr.szName, UnitHdr.u32Instance, UnitHdr.u32Pass);
7909 else
7910 rc = VMSetError(pVM, rc, RT_SRC_POS, N_("Failed to load unit '%s'"), UnitHdr.szName);
7911 }
7912 return rc;
7913 }
7914 }
7915 else
7916 {
7917 /*
7918 * SSM unit wasn't found - ignore this when loading for the debugger.
7919 */
7920 LogRel(("SSM: Found no handler for unit '%s' instance #%u!\n", UnitHdr.szName, UnitHdr.u32Instance));
7921 if (pSSM->enmAfter != SSMAFTER_DEBUG_IT)
7922 {
7923 pSSM->u.Read.fHaveSetError = true;
7924 return VMSetError(pVM, VERR_SSM_INTEGRITY_UNIT_NOT_FOUND, RT_SRC_POS,
7925 N_("Found no handler for unit '%s' instance #%u"), UnitHdr.szName, UnitHdr.u32Instance);
7926 }
7927 SSMR3SkipToEndOfUnit(pSSM);
7928 ssmR3DataReadFinishV2(pSSM);
7929 }
7930
7931 /*
7932 * Check for cancellation.
7933 */
7934 if (RT_UNLIKELY(ASMAtomicUoReadU32(&(pSSM)->fCancelled) == SSMHANDLE_CANCELLED))
7935 {
7936 LogRel(("SSM: Cancelled!\n"));
7937 if (RT_SUCCESS(pSSM->rc))
7938 pSSM->rc = VERR_SSM_CANCELLED;
7939 return pSSM->rc;
7940 }
7941 }
7942 /* won't get here */
7943}
7944
7945
7946
7947
7948/**
7949 * Load VM save operation.
7950 *
7951 * @returns VBox status.
7952 *
7953 * @param pVM The VM handle.
7954 * @param pszFilename The name of the saved state file. NULL if pStreamOps
7955 * is used.
7956 * @param pStreamOps The stream method table. NULL if pszFilename is
7957 * used.
7958 * @param pvStreamOpsUser The user argument for the stream methods.
7959 * @param enmAfter What is planned after a successful load operation.
7960 * Only acceptable values are SSMAFTER_RESUME and SSMAFTER_DEBUG_IT.
7961 * @param pfnProgress Progress callback. Optional.
7962 * @param pvProgressUser User argument for the progress callback.
7963 *
7964 * @thread EMT
7965 */
7966VMMR3DECL(int) SSMR3Load(PVM pVM, const char *pszFilename, PCSSMSTRMOPS pStreamOps, void *pvStreamOpsUser,
7967 SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvProgressUser)
7968{
7969 LogFlow(("SSMR3Load: pszFilename=%p:{%s} pStreamOps=%p pvStreamOpsUser=%p enmAfter=%d pfnProgress=%p pvProgressUser=%p\n",
7970 pszFilename, pszFilename, pStreamOps, pvStreamOpsUser, enmAfter, pfnProgress, pvProgressUser));
7971 VM_ASSERT_EMT0(pVM);
7972
7973 /*
7974 * Validate input.
7975 */
7976 AssertMsgReturn( enmAfter == SSMAFTER_RESUME
7977 || enmAfter == SSMAFTER_TELEPORT
7978 || enmAfter == SSMAFTER_DEBUG_IT,
7979 ("%d\n", enmAfter),
7980 VERR_INVALID_PARAMETER);
7981 AssertReturn(!pszFilename != !pStreamOps, VERR_INVALID_PARAMETER);
7982 if (pStreamOps)
7983 {
7984 AssertReturn(pStreamOps->u32Version == SSMSTRMOPS_VERSION, VERR_INVALID_MAGIC);
7985 AssertReturn(pStreamOps->u32EndVersion == SSMSTRMOPS_VERSION, VERR_INVALID_MAGIC);
7986 AssertReturn(pStreamOps->pfnWrite, VERR_INVALID_PARAMETER);
7987 AssertReturn(pStreamOps->pfnRead, VERR_INVALID_PARAMETER);
7988 AssertReturn(pStreamOps->pfnSeek, VERR_INVALID_PARAMETER);
7989 AssertReturn(pStreamOps->pfnTell, VERR_INVALID_PARAMETER);
7990 AssertReturn(pStreamOps->pfnSize, VERR_INVALID_PARAMETER);
7991 AssertReturn(pStreamOps->pfnClose, VERR_INVALID_PARAMETER);
7992 }
7993
7994 /*
7995 * Create the handle and open the file.
7996 */
7997 SSMHANDLE Handle;
7998 int rc = ssmR3OpenFile(pVM, pszFilename, pStreamOps, pvStreamOpsUser, false /* fChecksumIt */,
7999 true /* fChecksumOnRead */, 8 /*cBuffers*/, &Handle);
8000 if (RT_SUCCESS(rc))
8001 {
8002 ssmR3StrmStartIoThread(&Handle.Strm);
8003 ssmR3SetCancellable(pVM, &Handle, true);
8004
8005 Handle.enmAfter = enmAfter;
8006 Handle.pfnProgress = pfnProgress;
8007 Handle.pvUser = pvProgressUser;
8008
8009 if (Handle.u.Read.u16VerMajor)
8010 LogRel(("SSM: File header: Format %u.%u, VirtualBox Version %u.%u.%u r%u, %u-bit host, cbGCPhys=%u, cbGCPtr=%u\n",
8011 Handle.u.Read.uFmtVerMajor, Handle.u.Read.uFmtVerMinor,
8012 Handle.u.Read.u16VerMajor, Handle.u.Read.u16VerMinor, Handle.u.Read.u32VerBuild, Handle.u.Read.u32SvnRev,
8013 Handle.u.Read.cHostBits, Handle.u.Read.cbGCPhys, Handle.u.Read.cbGCPtr));
8014 else
8015 LogRel(("SSM: File header: Format %u.%u, %u-bit host, cbGCPhys=%u, cbGCPtr=%u\n" ,
8016 Handle.u.Read.uFmtVerMajor, Handle.u.Read.uFmtVerMinor,
8017 Handle.u.Read.cHostBits, Handle.u.Read.cbGCPhys, Handle.u.Read.cbGCPtr));
8018
8019 if (pfnProgress)
8020 pfnProgress(pVM, Handle.uPercent, pvProgressUser);
8021
8022 /*
8023 * Clear the per unit flags.
8024 */
8025 PSSMUNIT pUnit;
8026 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
8027 pUnit->fCalled = false;
8028
8029 /*
8030 * Do the prepare run.
8031 */
8032 Handle.rc = VINF_SUCCESS;
8033 Handle.enmOp = SSMSTATE_LOAD_PREP;
8034 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
8035 {
8036 if (pUnit->u.Common.pfnLoadPrep)
8037 {
8038 Handle.u.Read.pCurUnit = pUnit;
8039 pUnit->fCalled = true;
8040 switch (pUnit->enmType)
8041 {
8042 case SSMUNITTYPE_DEV:
8043 rc = pUnit->u.Dev.pfnLoadPrep(pUnit->u.Dev.pDevIns, &Handle);
8044 break;
8045 case SSMUNITTYPE_DRV:
8046 rc = pUnit->u.Drv.pfnLoadPrep(pUnit->u.Drv.pDrvIns, &Handle);
8047 break;
8048 case SSMUNITTYPE_INTERNAL:
8049 rc = pUnit->u.Internal.pfnLoadPrep(pVM, &Handle);
8050 break;
8051 case SSMUNITTYPE_EXTERNAL:
8052 rc = pUnit->u.External.pfnLoadPrep(&Handle, pUnit->u.External.pvUser);
8053 break;
8054 default:
8055 rc = VERR_INTERNAL_ERROR;
8056 break;
8057 }
8058 Handle.u.Read.pCurUnit = NULL;
8059 if (RT_FAILURE(rc) && RT_SUCCESS_NP(Handle.rc))
8060 Handle.rc = rc;
8061 else
8062 rc = Handle.rc;
8063 if (RT_FAILURE(rc))
8064 {
8065 LogRel(("SSM: Prepare load failed with rc=%Rrc for data unit '%s.\n", rc, pUnit->szName));
8066 break;
8067 }
8068 }
8069 }
8070
8071 /* pending 2% */
8072 if (pfnProgress)
8073 pfnProgress(pVM, Handle.uPercentPrepare-1, pvProgressUser);
8074 Handle.uPercent = Handle.uPercentPrepare;
8075 Handle.cbEstTotal = Handle.u.Read.cbLoadFile;
8076 Handle.offEstUnitEnd = Handle.u.Read.cbLoadFile;
8077
8078 /*
8079 * Do the execute run.
8080 */
8081 if (RT_SUCCESS(rc))
8082 {
8083 if (Handle.u.Read.uFmtVerMajor >= 2)
8084 rc = ssmR3LoadExecV2(pVM, &Handle);
8085 else
8086 rc = ssmR3LoadExecV1(pVM, &Handle);
8087 Handle.u.Read.pCurUnit = NULL;
8088 Handle.u.Read.uCurUnitVer = UINT32_MAX;
8089 Handle.u.Read.uCurUnitPass = 0;
8090
8091 /* (progress should be pending 99% now) */
8092 AssertMsg( Handle.fLiveSave
8093 || RT_FAILURE(rc)
8094 || Handle.uPercent == (101-Handle.uPercentDone), ("%d\n", Handle.uPercent));
8095 }
8096
8097 /*
8098 * Do the done run.
8099 */
8100 Handle.rc = rc;
8101 Handle.enmOp = SSMSTATE_LOAD_DONE;
8102 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
8103 {
8104 if ( pUnit->u.Common.pfnLoadDone
8105 && ( pUnit->fCalled
8106 || (!pUnit->u.Common.pfnLoadPrep && !pUnit->u.Common.pfnLoadExec)))
8107 {
8108 Handle.u.Read.pCurUnit = pUnit;
8109 int const rcOld = Handle.rc;
8110 rc = VINF_SUCCESS;
8111 switch (pUnit->enmType)
8112 {
8113 case SSMUNITTYPE_DEV:
8114 rc = pUnit->u.Dev.pfnLoadDone(pUnit->u.Dev.pDevIns, &Handle);
8115 break;
8116 case SSMUNITTYPE_DRV:
8117 rc = pUnit->u.Drv.pfnLoadDone(pUnit->u.Drv.pDrvIns, &Handle);
8118 break;
8119 case SSMUNITTYPE_INTERNAL:
8120 rc = pUnit->u.Internal.pfnLoadDone(pVM, &Handle);
8121 break;
8122 case SSMUNITTYPE_EXTERNAL:
8123 rc = pUnit->u.External.pfnLoadDone(&Handle, pUnit->u.External.pvUser);
8124 break;
8125 default:
8126 rc = VERR_INTERNAL_ERROR;
8127 break;
8128 }
8129 Handle.u.Read.pCurUnit = NULL;
8130 if (RT_SUCCESS(rc) && Handle.rc != rcOld)
8131 rc = Handle.rc;
8132 if (RT_FAILURE(rc))
8133 {
8134 LogRel(("SSM: LoadDone failed with rc=%Rrc for data unit '%s' instance #%u.\n",
8135 rc, pUnit->szName, pUnit->u32Instance));
8136 if (!ASMAtomicXchgBool(&Handle.u.Read.fHaveSetError, true))
8137 VMSetError(pVM, rc, RT_SRC_POS, N_("LoadDone failed with rc=%Rrc for data unit '%s' instance #%u."),
8138 rc, pUnit->szName, pUnit->u32Instance);
8139 if (RT_SUCCESS_NP(Handle.rc))
8140 Handle.rc = rc;
8141 }
8142 }
8143 }
8144
8145 /* progress */
8146 if (pfnProgress)
8147 pfnProgress(pVM, 99, pvProgressUser);
8148
8149 ssmR3SetCancellable(pVM, &Handle, false);
8150 ssmR3StrmClose(&Handle.Strm, Handle.rc == VERR_SSM_CANCELLED);
8151 rc = Handle.rc;
8152 }
8153
8154 /*
8155 * Done
8156 */
8157 if (RT_SUCCESS(rc))
8158 {
8159 /* progress */
8160 if (pfnProgress)
8161 pfnProgress(pVM, 100, pvProgressUser);
8162 Log(("SSM: Load of '%s' completed!\n", pszFilename));
8163 }
8164 return rc;
8165}
8166
8167
8168/**
8169 * VMSetError wrapper for load errors that inserts the saved state details.
8170 *
8171 * @returns rc.
8172 * @param pSSM The saved state handle.
8173 * @param rc The status code of the error. Use RT_SRC_POS.
8174 * @param RT_SRC_POS_DECL The source location.
8175 * @param pszFormat The message format string.
8176 * @param ... Variable argument list.
8177 */
8178VMMR3DECL(int) SSMR3SetLoadError(PSSMHANDLE pSSM, int rc, RT_SRC_POS_DECL, const char *pszFormat, ...)
8179{
8180 va_list va;
8181 va_start(va, pszFormat);
8182 rc = SSMR3SetLoadErrorV(pSSM, rc, RT_SRC_POS_ARGS, pszFormat, va);
8183 va_end(va);
8184 return rc;
8185}
8186
8187
8188/**
8189 * VMSetError wrapper for load errors that inserts the saved state details.
8190 *
8191 * @returns rc.
8192 * @param pSSM The saved state handle.
8193 * @param rc The status code of the error.
8194 * @param RT_SRC_POS_DECL The error location, use RT_SRC_POS.
8195 * @param pszFormat The message format string.
8196 * @param va Variable argument list.
8197 */
8198VMMR3DECL(int) SSMR3SetLoadErrorV(PSSMHANDLE pSSM, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
8199{
8200 /*
8201 * Input validations.
8202 */
8203 SSM_ASSERT_READABLE_RET(pSSM);
8204 AssertPtr(pszFormat);
8205 Assert(RT_FAILURE_NP(rc));
8206
8207 /*
8208 * Format the incoming error.
8209 */
8210 char *pszMsg;
8211 RTStrAPrintfV(&pszMsg, pszFormat, va);
8212 if (!pszMsg)
8213 {
8214 VMSetError(pSSM->pVM, VERR_NO_MEMORY, RT_SRC_POS,
8215 N_("SSMR3SetLoadErrorV ran out of memory formatting: %s\n"), pszFormat);
8216 return rc;
8217 }
8218
8219 /*
8220 * Forward to VMSetError with the additional info.
8221 */
8222 PSSMUNIT pUnit = pSSM->u.Read.pCurUnit;
8223 const char *pszName = pUnit ? pUnit->szName : "unknown";
8224 uint32_t uInstance = pUnit ? pUnit->u32Instance : 0;
8225 if ( pSSM->enmOp == SSMSTATE_LOAD_EXEC
8226 && pSSM->u.Read.uCurUnitPass == SSM_PASS_FINAL)
8227 rc = VMSetError(pSSM->pVM, rc, RT_SRC_POS_ARGS, N_("%s#%u: %s [ver=%u pass=final]"),
8228 pszName, uInstance, pszMsg, pSSM->u.Read.uCurUnitVer);
8229 else if (pSSM->enmOp == SSMSTATE_LOAD_EXEC)
8230 rc = VMSetError(pSSM->pVM, rc, RT_SRC_POS_ARGS, N_("%s#%u: %s [ver=%u pass=#%u]"),
8231 pszName, uInstance, pszMsg, pSSM->u.Read.uCurUnitVer, pSSM->u.Read.uCurUnitPass);
8232 else if (pSSM->enmOp == SSMSTATE_LOAD_PREP)
8233 rc = VMSetError(pSSM->pVM, rc, RT_SRC_POS_ARGS, N_("%s#%u: %s [prep]"),
8234 pszName, uInstance, pszMsg);
8235 else if (pSSM->enmOp == SSMSTATE_LOAD_DONE)
8236 rc = VMSetError(pSSM->pVM, rc, RT_SRC_POS_ARGS, N_("%s#%u: %s [done]"),
8237 pszName, uInstance, pszMsg);
8238 else if (pSSM->enmOp == SSMSTATE_OPEN_READ)
8239 rc = VMSetError(pSSM->pVM, rc, RT_SRC_POS_ARGS, N_("%s#%u: %s [read]"),
8240 pszName, uInstance, pszMsg);
8241 else
8242 AssertFailed();
8243 pSSM->u.Read.fHaveSetError = true;
8244 RTStrFree(pszMsg);
8245 return rc;
8246}
8247
8248
8249/**
8250 * SSMR3SetLoadError wrapper that returns VERR_SSM_LOAD_CONFIG_MISMATCH.
8251 *
8252 * @returns VERR_SSM_LOAD_CONFIG_MISMATCH.
8253 * @param pSSM The saved state handle.
8254 * @param RT_SRC_POS_DECL The error location, use RT_SRC_POS.
8255 * @param pszFormat The message format string.
8256 * @param va Variable argument list.
8257 */
8258VMMR3DECL(int) SSMR3SetCfgError(PSSMHANDLE pSSM, RT_SRC_POS_DECL, const char *pszFormat, ...)
8259{
8260 va_list va;
8261 va_start(va, pszFormat);
8262 int rc = SSMR3SetLoadErrorV(pSSM, VERR_SSM_LOAD_CONFIG_MISMATCH, RT_SRC_POS_ARGS, pszFormat, va);
8263 va_end(va);
8264 return rc;
8265}
8266
8267#endif /* !SSM_STANDALONE */
8268
8269/**
8270 * Validates a file as a validate SSM saved state.
8271 *
8272 * This will only verify the file format, the format and content of individual
8273 * data units are not inspected.
8274 *
8275 * @returns VINF_SUCCESS if valid.
8276 * @returns VBox status code on other failures.
8277 *
8278 * @param pszFilename The path to the file to validate.
8279 * @param fChecksumIt Whether to checksum the file or not.
8280 *
8281 * @thread Any.
8282 */
8283VMMR3DECL(int) SSMR3ValidateFile(const char *pszFilename, bool fChecksumIt)
8284{
8285 LogFlow(("SSMR3ValidateFile: pszFilename=%p:{%s} fChecksumIt=%RTbool\n", pszFilename, pszFilename, fChecksumIt));
8286
8287 /*
8288 * Try open the file and validate it.
8289 */
8290 SSMHANDLE Handle;
8291 int rc = ssmR3OpenFile(NULL, pszFilename, NULL /*pStreamOps*/, NULL /*pvUser*/, fChecksumIt,
8292 false /*fChecksumOnRead*/, 1 /*cBuffers*/, &Handle);
8293 if (RT_SUCCESS(rc))
8294 ssmR3StrmClose(&Handle.Strm, false /*fCancelled*/);
8295 else
8296 Log(("SSM: Failed to open saved state file '%s', rc=%Rrc.\n", pszFilename, rc));
8297 return rc;
8298}
8299
8300
8301/**
8302 * Opens a saved state file for reading.
8303 *
8304 * @returns VBox status code.
8305 *
8306 * @param pszFilename The path to the saved state file.
8307 * @param fFlags Open flags. Reserved, must be 0.
8308 * @param ppSSM Where to store the SSM handle.
8309 *
8310 * @thread Any.
8311 */
8312VMMR3DECL(int) SSMR3Open(const char *pszFilename, unsigned fFlags, PSSMHANDLE *ppSSM)
8313{
8314 LogFlow(("SSMR3Open: pszFilename=%p:{%s} fFlags=%#x ppSSM=%p\n", pszFilename, pszFilename, fFlags, ppSSM));
8315
8316 /*
8317 * Validate input.
8318 */
8319 AssertMsgReturn(VALID_PTR(pszFilename), ("%p\n", pszFilename), VERR_INVALID_PARAMETER);
8320 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
8321 AssertMsgReturn(VALID_PTR(ppSSM), ("%p\n", ppSSM), VERR_INVALID_PARAMETER);
8322
8323 /*
8324 * Allocate a handle.
8325 */
8326 PSSMHANDLE pSSM = (PSSMHANDLE)RTMemAllocZ(sizeof(*pSSM));
8327 AssertReturn(pSSM, VERR_NO_MEMORY);
8328
8329 /*
8330 * Try open the file and validate it.
8331 */
8332 int rc = ssmR3OpenFile(NULL, pszFilename, NULL /*pStreamOps*/, NULL /*pvUser*/, false /*fChecksumIt*/,
8333 true /*fChecksumOnRead*/, 1 /*cBuffers*/, pSSM);
8334 if (RT_SUCCESS(rc))
8335 {
8336 pSSM->enmAfter = SSMAFTER_OPENED;
8337 pSSM->enmOp = SSMSTATE_OPEN_READ;
8338 *ppSSM = pSSM;
8339 LogFlow(("SSMR3Open: returns VINF_SUCCESS *ppSSM=%p\n", *ppSSM));
8340 return VINF_SUCCESS;
8341 }
8342
8343 Log(("SSMR3Open: Failed to open saved state file '%s', rc=%Rrc.\n", pszFilename, rc));
8344 RTMemFree(pSSM);
8345 return rc;
8346
8347}
8348
8349
8350/**
8351 * Closes a saved state file opened by SSMR3Open().
8352 *
8353 * @returns VBox status code.
8354 *
8355 * @param pSSM The SSM handle returned by SSMR3Open().
8356 *
8357 * @thread Any, but the caller is responsible for serializing calls per handle.
8358 */
8359VMMR3DECL(int) SSMR3Close(PSSMHANDLE pSSM)
8360{
8361 LogFlow(("SSMR3Close: pSSM=%p\n", pSSM));
8362
8363 /*
8364 * Validate input.
8365 */
8366 AssertMsgReturn(VALID_PTR(pSSM), ("%p\n", pSSM), VERR_INVALID_PARAMETER);
8367 AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
8368 AssertMsgReturn(pSSM->enmOp == SSMSTATE_OPEN_READ, ("%d\n", pSSM->enmOp), VERR_INVALID_PARAMETER);
8369 Assert(pSSM->fCancelled == SSMHANDLE_OK);
8370
8371 /*
8372 * Close the stream and free the handle.
8373 */
8374 int rc = ssmR3StrmClose(&pSSM->Strm, pSSM->rc == VERR_SSM_CANCELLED);
8375 if (pSSM->u.Read.pZipDecompV1)
8376 {
8377 RTZipDecompDestroy(pSSM->u.Read.pZipDecompV1);
8378 pSSM->u.Read.pZipDecompV1 = NULL;
8379 }
8380 RTMemFree(pSSM);
8381 return rc;
8382}
8383
8384
8385/**
8386 * Worker for SSMR3Seek that seeks version 1 saved state files.
8387 *
8388 * @returns VBox status code.
8389 * @param pSSM The SSM handle.
8390 * @param pszUnit The unit to seek to.
8391 * @param iInstance The particulart insance we seek.
8392 * @param piVersion Where to store the unit version number.
8393 */
8394static int ssmR3FileSeekV1(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
8395{
8396 /*
8397 * Walk the data units until we find EOF or a match.
8398 */
8399 size_t cbUnitNm = strlen(pszUnit) + 1;
8400 AssertLogRelReturn(cbUnitNm <= SSM_MAX_NAME_SIZE, VERR_SSM_UNIT_NOT_FOUND);
8401 char szName[SSM_MAX_NAME_SIZE];
8402 SSMFILEUNITHDRV1 UnitHdr;
8403 for (RTFOFF off = pSSM->u.Read.cbFileHdr; ; off += UnitHdr.cbUnit)
8404 {
8405 /*
8406 * Read the unit header and verify it.
8407 */
8408 int rc = ssmR3StrmPeekAt(&pSSM->Strm, off, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV1, szName), NULL);
8409 AssertRCReturn(rc, rc);
8410 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(SSMFILEUNITHDR_MAGIC)))
8411 {
8412 /*
8413 * Does what we've got match, if so read the name.
8414 */
8415 if ( UnitHdr.u32Instance == iInstance
8416 && UnitHdr.cchName == cbUnitNm)
8417 {
8418 rc = ssmR3StrmPeekAt(&pSSM->Strm, off + RT_OFFSETOF(SSMFILEUNITHDRV1, szName), szName, cbUnitNm, NULL);
8419 AssertRCReturn(rc, rc);
8420 AssertLogRelMsgReturn(!szName[UnitHdr.cchName - 1],
8421 (" Unit name '%.*s' was not properly terminated.\n", cbUnitNm, szName),
8422 VERR_SSM_INTEGRITY_UNIT);
8423
8424 /*
8425 * Does the name match?
8426 */
8427 if (!memcmp(szName, pszUnit, cbUnitNm))
8428 {
8429 rc = ssmR3StrmSeek(&pSSM->Strm, off + RT_OFFSETOF(SSMFILEUNITHDRV1, szName) + cbUnitNm, RTFILE_SEEK_BEGIN, 0);
8430 pSSM->cbUnitLeftV1 = UnitHdr.cbUnit - RT_OFFSETOF(SSMFILEUNITHDRV1, szName[cbUnitNm]);
8431 pSSM->offUnit = 0;
8432 if (piVersion)
8433 *piVersion = UnitHdr.u32Version;
8434 return VINF_SUCCESS;
8435 }
8436 }
8437 }
8438 else if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_END, sizeof(SSMFILEUNITHDR_END)))
8439 return VERR_SSM_UNIT_NOT_FOUND;
8440 else
8441 AssertLogRelMsgFailedReturn(("Invalid unit magic at offset %RTfoff, '%.*s'!\n",
8442 off, sizeof(UnitHdr.achMagic) - 1, &UnitHdr.achMagic[0]),
8443 VERR_SSM_INTEGRITY_UNIT_MAGIC);
8444 }
8445 /* won't get here. */
8446}
8447
8448
8449/**
8450 * Worker for ssmR3FileSeekV2 for simplifying memory cleanup.
8451 *
8452 * @returns VBox status code.
8453 * @param pSSM The SSM handle.
8454 * @param pDir The directory buffer.
8455 * @param cbDir The size of the directory.
8456 * @param cDirEntries The number of directory entries.
8457 * @param offDir The directory offset in the file.
8458 * @param pszUnit The unit to seek to.
8459 * @param iInstance The particulart insance we seek.
8460 * @param piVersion Where to store the unit version number.
8461 */
8462static int ssmR3FileSeekSubV2(PSSMHANDLE pSSM, PSSMFILEDIR pDir, size_t cbDir, uint32_t cDirEntries, uint64_t offDir,
8463 const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
8464{
8465 /*
8466 * Read it.
8467 */
8468 int rc = ssmR3StrmPeekAt(&pSSM->Strm, offDir, pDir, cbDir, NULL);
8469 AssertLogRelRCReturn(rc, rc);
8470 rc = ssmR3ValidateDirectory(pDir, (uint32_t)cbDir, offDir, cDirEntries, pSSM->u.Read.cbFileHdr, pSSM->u.Read.u32SvnRev);
8471 if (RT_FAILURE(rc))
8472 return rc;
8473
8474 /*
8475 * Search the directory.
8476 */
8477 size_t cbUnitNm = strlen(pszUnit) + 1;
8478 uint32_t const u32NameCRC = RTCrc32(pszUnit, cbUnitNm - 1);
8479 for (uint32_t i = 0; i < cDirEntries; i++)
8480 {
8481 if ( pDir->aEntries[i].u32NameCRC == u32NameCRC
8482 && pDir->aEntries[i].u32Instance == iInstance
8483 && pDir->aEntries[i].off != 0 /* bug in unreleased code */
8484 )
8485 {
8486 /*
8487 * Read and validate the unit header.
8488 */
8489 SSMFILEUNITHDRV2 UnitHdr;
8490 size_t cbToRead = sizeof(UnitHdr);
8491 if (pDir->aEntries[i].off + cbToRead > offDir)
8492 {
8493 cbToRead = offDir - pDir->aEntries[i].off;
8494 RT_ZERO(UnitHdr);
8495 }
8496 rc = ssmR3StrmPeekAt(&pSSM->Strm, pDir->aEntries[i].off, &UnitHdr, cbToRead, NULL);
8497 AssertLogRelRCReturn(rc, rc);
8498
8499 AssertLogRelMsgReturn(!memcmp(UnitHdr.szMagic, SSMFILEUNITHDR_MAGIC, sizeof(UnitHdr.szMagic)),
8500 ("Bad unit header or dictionary offset: i=%u off=%lld\n", i, pDir->aEntries[i].off),
8501 VERR_SSM_INTEGRITY_UNIT);
8502 AssertLogRelMsgReturn(UnitHdr.offStream == pDir->aEntries[i].off,
8503 ("Bad unit header: i=%d off=%lld offStream=%lld\n", i, pDir->aEntries[i].off, UnitHdr.offStream),
8504 VERR_SSM_INTEGRITY_UNIT);
8505 AssertLogRelMsgReturn(UnitHdr.u32Instance == pDir->aEntries[i].u32Instance,
8506 ("Bad unit header: i=%d off=%lld u32Instance=%u Dir.u32Instance=%u\n",
8507 i, pDir->aEntries[i].off, UnitHdr.u32Instance, pDir->aEntries[i].u32Instance),
8508 VERR_SSM_INTEGRITY_UNIT);
8509 uint32_t cbUnitHdr = RT_UOFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]);
8510 AssertLogRelMsgReturn( UnitHdr.cbName > 0
8511 && UnitHdr.cbName < sizeof(UnitHdr)
8512 && cbUnitHdr <= cbToRead,
8513 ("Bad unit header: i=%u off=%lld cbName=%#x cbToRead=%#x\n", i, pDir->aEntries[i].off, UnitHdr.cbName, cbToRead),
8514 VERR_SSM_INTEGRITY_UNIT);
8515 SSM_CHECK_CRC32_RET(&UnitHdr, RT_OFFSETOF(SSMFILEUNITHDRV2, szName[UnitHdr.cbName]),
8516 ("Bad unit header CRC: i=%u off=%lld u32CRC=%#x u32ActualCRC=%#x\n",
8517 i, pDir->aEntries[i].off, u32CRC, u32ActualCRC));
8518
8519 /*
8520 * Ok, it is valid, get on with the comparing now.
8521 */
8522 if ( UnitHdr.cbName == cbUnitNm
8523 && !memcmp(UnitHdr.szName, pszUnit, cbUnitNm))
8524 {
8525 if (piVersion)
8526 *piVersion = UnitHdr.u32Version;
8527 rc = ssmR3StrmSeek(&pSSM->Strm, pDir->aEntries[i].off + cbUnitHdr, RTFILE_SEEK_BEGIN,
8528 RTCrc32Process(UnitHdr.u32CurStreamCRC, &UnitHdr, cbUnitHdr));
8529 AssertLogRelRCReturn(rc, rc);
8530 ssmR3DataReadBeginV2(pSSM);
8531 return VINF_SUCCESS;
8532 }
8533 }
8534 }
8535
8536 return VERR_SSM_UNIT_NOT_FOUND;
8537}
8538
8539
8540/**
8541 * Worker for SSMR3Seek that seeks version 2 saved state files.
8542 *
8543 * @returns VBox status code.
8544 * @param pSSM The SSM handle.
8545 * @param pszUnit The unit to seek to.
8546 * @param iInstance The particulart insance we seek.
8547 * @param piVersion Where to store the unit version number.
8548 */
8549static int ssmR3FileSeekV2(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
8550{
8551 /*
8552 * Read the footer, allocate a temporary buffer for the dictionary and
8553 * pass it down to a worker to simplify cleanup.
8554 */
8555 uint64_t offFooter;
8556 SSMFILEFTR Footer;
8557 int rc = ssmR3StrmPeekAt(&pSSM->Strm, -(RTFOFF)sizeof(Footer), &Footer, sizeof(Footer), &offFooter);
8558 AssertLogRelRCReturn(rc, rc);
8559 AssertLogRelReturn(!memcmp(Footer.szMagic, SSMFILEFTR_MAGIC, sizeof(Footer.szMagic)), VERR_SSM_INTEGRITY);
8560 SSM_CHECK_CRC32_RET(&Footer, sizeof(Footer), ("Bad footer CRC: %08x, actual %08x\n", u32CRC, u32ActualCRC));
8561
8562 size_t const cbDir = RT_OFFSETOF(SSMFILEDIR, aEntries[Footer.cDirEntries]);
8563 PSSMFILEDIR pDir = (PSSMFILEDIR)RTMemTmpAlloc(cbDir);
8564 if (RT_UNLIKELY(!pDir))
8565 return VERR_NO_TMP_MEMORY;
8566 rc = ssmR3FileSeekSubV2(pSSM, pDir, cbDir, Footer.cDirEntries, offFooter - cbDir,
8567 pszUnit, iInstance, piVersion);
8568 RTMemTmpFree(pDir);
8569
8570 return rc;
8571}
8572
8573
8574/**
8575 * Seeks to a specific data unit.
8576 *
8577 * After seeking it's possible to use the getters to on
8578 * that data unit.
8579 *
8580 * @returns VBox status code.
8581 * @returns VERR_SSM_UNIT_NOT_FOUND if the unit+instance wasn't found.
8582 *
8583 * @param pSSM The SSM handle returned by SSMR3Open().
8584 * @param pszUnit The name of the data unit.
8585 * @param iInstance The instance number.
8586 * @param piVersion Where to store the version number. (Optional)
8587 *
8588 * @thread Any, but the caller is responsible for serializing calls per handle.
8589 */
8590VMMR3DECL(int) SSMR3Seek(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
8591{
8592 LogFlow(("SSMR3Seek: pSSM=%p pszUnit=%p:{%s} iInstance=%RU32 piVersion=%p\n",
8593 pSSM, pszUnit, pszUnit, iInstance, piVersion));
8594
8595 /*
8596 * Validate input.
8597 */
8598 AssertPtrReturn(pSSM, VERR_INVALID_PARAMETER);
8599 AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
8600 AssertMsgReturn(pSSM->enmOp == SSMSTATE_OPEN_READ, ("%d\n", pSSM->enmOp), VERR_INVALID_PARAMETER);
8601 AssertPtrReturn(pszUnit, VERR_INVALID_POINTER);
8602 AssertMsgReturn(!piVersion || VALID_PTR(piVersion), ("%p\n", piVersion), VERR_INVALID_POINTER);
8603
8604 /*
8605 * Reset the state.
8606 */
8607 if (pSSM->u.Read.pZipDecompV1)
8608 {
8609 RTZipDecompDestroy(pSSM->u.Read.pZipDecompV1);
8610 pSSM->u.Read.pZipDecompV1 = NULL;
8611 }
8612 pSSM->cbUnitLeftV1 = 0;
8613 pSSM->offUnit = UINT64_MAX;
8614
8615 /*
8616 * Call the version specific workers.
8617 */
8618 if (pSSM->u.Read.uFmtVerMajor >= 2)
8619 pSSM->rc = ssmR3FileSeekV2(pSSM, pszUnit, iInstance, piVersion);
8620 else
8621 pSSM->rc = ssmR3FileSeekV1(pSSM, pszUnit, iInstance, piVersion);
8622 return pSSM->rc;
8623}
8624
8625
8626
8627/* ... Misc APIs ... */
8628/* ... Misc APIs ... */
8629/* ... Misc APIs ... */
8630/* ... Misc APIs ... */
8631/* ... Misc APIs ... */
8632/* ... Misc APIs ... */
8633/* ... Misc APIs ... */
8634/* ... Misc APIs ... */
8635/* ... Misc APIs ... */
8636/* ... Misc APIs ... */
8637/* ... Misc APIs ... */
8638
8639
8640
8641/**
8642 * Query what the VBox status code of the operation is.
8643 *
8644 * This can be used for putting and getting a batch of values
8645 * without bother checking the result till all the calls have
8646 * been made.
8647 *
8648 * @returns SSMAFTER enum value.
8649 * @param pSSM The saved state handle.
8650 */
8651VMMR3DECL(int) SSMR3HandleGetStatus(PSSMHANDLE pSSM)
8652{
8653 SSM_ASSERT_VALID_HANDLE(pSSM);
8654 return pSSM->rc;
8655}
8656
8657
8658/**
8659 * Fail the load operation.
8660 *
8661 * This is mainly intended for sub item loaders (like timers) which
8662 * return code isn't necessarily heeded by the caller but is important
8663 * to SSM.
8664 *
8665 * @returns VBox status code of the handle, or VERR_INVALID_PARAMETER.
8666 * @param pSSM The saved state handle.
8667 * @param iStatus Failure status code. This MUST be a VERR_*.
8668 */
8669VMMR3DECL(int) SSMR3HandleSetStatus(PSSMHANDLE pSSM, int iStatus)
8670{
8671 SSM_ASSERT_VALID_HANDLE(pSSM);
8672 Assert(pSSM->enmOp != SSMSTATE_LIVE_VOTE);
8673 if (RT_FAILURE(iStatus))
8674 {
8675 int rc = pSSM->rc;
8676 if (RT_SUCCESS(rc))
8677 pSSM->rc = rc = iStatus;
8678 return rc;
8679 }
8680 AssertMsgFailed(("iStatus=%d %Rrc\n", iStatus, iStatus));
8681 return VERR_INVALID_PARAMETER;
8682}
8683
8684
8685/**
8686 * Get what to do after this operation.
8687 *
8688 * @returns SSMAFTER enum value.
8689 * @param pSSM The saved state handle.
8690 */
8691VMMR3DECL(SSMAFTER) SSMR3HandleGetAfter(PSSMHANDLE pSSM)
8692{
8693 SSM_ASSERT_VALID_HANDLE(pSSM);
8694 return pSSM->enmAfter;
8695}
8696
8697
8698/**
8699 * Checks if it is a live save operation or not.
8700 *
8701 * @returns True if it is, false if it isn't.
8702 * @param pSSM The saved state handle.
8703 */
8704VMMR3DECL(bool) SSMR3HandleIsLiveSave(PSSMHANDLE pSSM)
8705{
8706 SSM_ASSERT_VALID_HANDLE(pSSM);
8707 return pSSM->fLiveSave;
8708}
8709
8710
8711/**
8712 * Gets the maximum downtime for a live operation.
8713 *
8714 * @returns The max downtime in milliseconds. Can be anything from 0 thru
8715 * UINT32_MAX.
8716 *
8717 * @param pSSM The saved state handle.
8718 */
8719VMMR3DECL(uint32_t) SSMR3HandleMaxDowntime(PSSMHANDLE pSSM)
8720{
8721 SSM_ASSERT_VALID_HANDLE(pSSM);
8722 if (pSSM->enmOp <= SSMSTATE_SAVE_DONE)
8723 return pSSM->u.Write.cMsMaxDowntime;
8724 return UINT32_MAX;
8725}
8726
8727
8728/**
8729 * Gets the host bit count of a saved state.
8730 *
8731 * @returns 32 or 64. If pSSM is invalid, 0 is returned.
8732 * @param pSSM The saved state handle.
8733 *
8734 * @remarks This method should ONLY be used for hacks when loading OLDER saved
8735 * state that have data layout or semantical changes without the
8736 * compulsory version number change.
8737 */
8738VMMR3DECL(uint32_t) SSMR3HandleHostBits(PSSMHANDLE pSSM)
8739{
8740 SSM_ASSERT_VALID_HANDLE(pSSM);
8741 return ssmR3GetHostBits(pSSM);
8742}
8743
8744
8745/**
8746 * Get the VirtualBox SVN revision that created the saved state.
8747 *
8748 * @returns The revision number on success.
8749 * form. If we don't know, it's 0.
8750 * @param pSSM The saved state handle.
8751 *
8752 * @remarks This method should ONLY be used for hacks when loading OLDER saved
8753 * state that have data layout or semantical changes without the
8754 * compulsory version number change. Be VERY careful with this
8755 * function since it will return different values for OSE builds!
8756 */
8757VMMR3DECL(uint32_t) SSMR3HandleRevision(PSSMHANDLE pSSM)
8758{
8759 if (pSSM->enmOp >= SSMSTATE_LOAD_PREP)
8760 return pSSM->u.Read.u32SvnRev;
8761#ifdef SSM_STANDALONE
8762 return 0;
8763#else
8764 return VMMGetSvnRev();
8765#endif
8766}
8767
8768
8769/**
8770 * Gets the VirtualBox version that created the saved state.
8771 *
8772 * @returns VBOX_FULL_VERSION style version number.
8773 * Returns UINT32_MAX if unknown or somehow out of range.
8774 *
8775 * @param pSSM The saved state handle.
8776 *
8777 * @remarks This method should ONLY be used for hacks when loading OLDER saved
8778 * state that have data layout or semantical changes without the
8779 * compulsory version number change.
8780 */
8781VMMR3DECL(uint32_t) SSMR3HandleVersion(PSSMHANDLE pSSM)
8782{
8783 if (pSSM->enmOp >= SSMSTATE_LOAD_PREP)
8784 {
8785 if ( !pSSM->u.Read.u16VerMajor
8786 && !pSSM->u.Read.u16VerMinor
8787 && !pSSM->u.Read.u32VerBuild)
8788 return UINT32_MAX;
8789 AssertReturn(pSSM->u.Read.u16VerMajor <= 0xff, UINT32_MAX);
8790 AssertReturn(pSSM->u.Read.u16VerMinor <= 0xff, UINT32_MAX);
8791 AssertReturn(pSSM->u.Read.u32VerBuild <= 0xffff, UINT32_MAX);
8792 return VBOX_FULL_VERSION_MAKE(pSSM->u.Read.u16VerMajor, pSSM->u.Read.u16VerMinor, pSSM->u.Read.u32VerBuild);
8793 }
8794 return VBOX_FULL_VERSION;
8795}
8796
8797
8798/**
8799 * Get the host OS and architecture where the saved state was created.
8800 *
8801 * @returns Pointer to a read only string. When known, this is on the os.arch
8802 * form. If we don't know, it's an empty string.
8803 * @param pSSM The saved state handle.
8804 *
8805 * @remarks This method should ONLY be used for hacks when loading OLDER saved
8806 * state that have data layout or semantical changes without the
8807 * compulsory version number change.
8808 */
8809VMMR3DECL(const char *) SSMR3HandleHostOSAndArch(PSSMHANDLE pSSM)
8810{
8811 if (pSSM->enmOp >= SSMSTATE_LOAD_PREP)
8812 return pSSM->u.Read.szHostOSAndArch;
8813 return KBUILD_TARGET "." KBUILD_TARGET_ARCH;
8814}
8815
8816
8817#ifndef SSM_STANDALONE
8818/**
8819 * Asynchronously cancels the current SSM operation ASAP.
8820 *
8821 * @returns VBox status code.
8822 * @retval VINF_SUCCESS on success.
8823 * @retval VERR_SSM_NO_PENDING_OPERATION if nothing around that can be
8824 * cancelled.
8825 * @retval VERR_SSM_ALREADY_CANCELLED if the operation as already been
8826 * cancelled.
8827 *
8828 * @param pVM The VM handle.
8829 *
8830 * @thread Any.
8831 */
8832VMMR3DECL(int) SSMR3Cancel(PVM pVM)
8833{
8834 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
8835
8836 int rc = RTCritSectEnter(&pVM->ssm.s.CancelCritSect);
8837 AssertRCReturn(rc, rc);
8838
8839 PSSMHANDLE pSSM = pVM->ssm.s.pSSM;
8840 if (pSSM)
8841 {
8842 uint32_t u32Old;
8843 if (ASMAtomicCmpXchgExU32(&pSSM->fCancelled, SSMHANDLE_CANCELLED, SSMHANDLE_OK, &u32Old))
8844 {
8845 LogRel(("SSM: Cancelled pending operation\n"));
8846 rc = VINF_SUCCESS;
8847 }
8848 else if (u32Old == SSMHANDLE_CANCELLED)
8849 rc = VERR_SSM_ALREADY_CANCELLED;
8850 else
8851 {
8852 AssertLogRelMsgFailed(("fCancelled=%RX32 enmOp=%d\n", u32Old, pSSM->enmOp));
8853 rc = VERR_INTERNAL_ERROR_2;
8854 }
8855 }
8856 else
8857 rc = VERR_SSM_NO_PENDING_OPERATION;
8858
8859 RTCritSectLeave(&pVM->ssm.s.CancelCritSect);
8860 return rc;
8861}
8862#endif /* !SSM_STANDALONE */
8863
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette