VirtualBox

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

Last change on this file since 62657 was 62657, checked in by vboxsync, 8 years ago

ssmR3StrmLogUnitContent: Restored the wrong rc, oops.

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