VirtualBox

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

Last change on this file since 26718 was 26526, checked in by vboxsync, 15 years ago

VMM,asm.h,Config.kmk: win.amd64 warnings; fixed the ASMAtomicSubU32 signature.

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