VirtualBox

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

Last change on this file since 8170 was 8155, checked in by vboxsync, 17 years ago

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 102.9 KB
Line 
1/* $Id: SSM.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
2/** @file
3 * SSM - Saved State Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/** @page pg_ssm SSM - The Saved State Manager
24 *
25 * The Saved State Manager (SSM) implements facilities for saving and loading
26 * a VM state in a structural manner using callbacks for each collection of
27 * data which needs saving.
28 *
29 * At init time each of the VM components will register data entities which
30 * they need to save and restore. Each entity have a unique name (ascii) and
31 * a set of callbacks associated with it. The name will be used to identify
32 * the entity during restore. The callbacks are for the two operations, save
33 * and restore. There are three callbacks for each of the two - a prepare,
34 * a execute and a what-now.
35 *
36 * The SSM provides a number of APIs for encoding and decoding the data.
37 */
38
39
40/*******************************************************************************
41* Header Files *
42*******************************************************************************/
43#define LOG_GROUP LOG_GROUP_SSM
44#include <VBox/ssm.h>
45#include <VBox/dbgf.h>
46#include <VBox/mm.h>
47#include "SSMInternal.h"
48#include <VBox/vm.h>
49#include <VBox/err.h>
50#include <VBox/log.h>
51
52#include <iprt/assert.h>
53#include <iprt/file.h>
54#include <iprt/alloc.h>
55#include <iprt/uuid.h>
56#include <iprt/zip.h>
57#include <iprt/crc32.h>
58#include <iprt/thread.h>
59#include <iprt/string.h>
60
61
62
63/*******************************************************************************
64* Defined Constants And Macros *
65*******************************************************************************/
66/** Start structure magic. (Isacc Asimov) */
67#define SSMR3STRUCT_BEGIN 0x19200102
68/** End structure magic. (Isacc Asimov) */
69#define SSMR3STRUCT_END 0x19920406
70
71
72/*******************************************************************************
73* Structures and Typedefs *
74*******************************************************************************/
75
76typedef enum SSMSTATE
77{
78 SSMSTATE_SAVE_PREP = 1,
79 SSMSTATE_SAVE_EXEC,
80 SSMSTATE_SAVE_DONE,
81 SSMSTATE_LOAD_PREP,
82 SSMSTATE_LOAD_EXEC,
83 SSMSTATE_LOAD_DONE,
84 SSMSTATE_OPEN_READ
85} SSMSTATE;
86
87
88/**
89 * Handle structure.
90 */
91typedef struct SSMHANDLE
92{
93 /** The file handle. */
94 RTFILE File;
95 /** The VM handle. */
96 PVM pVM;
97 /** The size of the file header.
98 * Because the file header was incorrectly aligned there we've ended up with
99 * differences between the 64-bit and 32-bit file header. */
100 size_t cbFileHdr;
101 /** The current operation. */
102 SSMSTATE enmOp;
103 /** What to do after save completes. (move the enum) */
104 SSMAFTER enmAfter;
105 /** The current rc of the save operation. */
106 int rc;
107 /** The compressor of the current data unit. */
108 PRTZIPCOMP pZipComp;
109 /** The decompressor of the current data unit. */
110 PRTZIPDECOMP pZipDecomp;
111 /** Number of bytes left in the current data unit. */
112 uint64_t cbUnitLeft;
113
114 /** Pointer to the progress callback function. */
115 PFNVMPROGRESS pfnProgress;
116 /** User specified arguemnt to the callback function. */
117 void *pvUser;
118 /** Next completion percentage. (corresponds to offEstProgress) */
119 unsigned uPercent;
120 /** The position of the next progress callback in the estimated file. */
121 uint64_t offEstProgress;
122 /** The estimated total byte count.
123 * (Only valid after the prep.) */
124 uint64_t cbEstTotal;
125 /** Current position in the estimated file. */
126 uint64_t offEst;
127 /** End of current unit in the estimated file. */
128 uint64_t offEstUnitEnd;
129 /** the amount of % we reserve for the 'prepare' phase */
130 unsigned uPercentPrepare;
131 /** the amount of % we reserve for the 'done' stage */
132 unsigned uPercentDone;
133
134} SSMHANDLE;
135
136
137/**
138 * Header of the saved state file.
139 */
140typedef struct SSMFILEHDR
141{
142 /** Magic string which identifies this file as a version of VBox saved state file format. */
143 char achMagic[32];
144 /** The size of this file. Used to check
145 * whether the save completed and that things are fine otherwise. */
146 uint64_t cbFile;
147 /** File checksum. The actual calculation skips past the u32CRC field. */
148 uint32_t u32CRC;
149 /** Padding. */
150 uint32_t u32Reserved;
151 /** The machine UUID. (Ignored if NIL.) */
152 RTUUID MachineUuid;
153} SSMFILEHDR, *PSSMFILEHDR;
154AssertCompileSize(SSMFILEHDR, 64);
155
156
157/**
158 * The x86 edition of the 1.0 header.
159 */
160#pragma pack(1) /* darn, MachineUuid got missaligned! */
161typedef struct SSMFILEHDRV10X86
162{
163 /** Magic string which identifies this file as a version of VBox saved state file format. */
164 char achMagic[32];
165 /** The size of this file. Used to check
166 * whether the save completed and that things are fine otherwise. */
167 uint64_t cbFile;
168 /** File checksum. The actual calculation skips past the u32CRC field. */
169 uint32_t u32CRC;
170 /** The machine UUID. (Ignored if NIL.) */
171 RTUUID MachineUuid;
172} SSMFILEHDRV10X86, *PSSMFILEHDRV10X86;
173#pragma pack()
174
175/**
176 * The amd64 edition of the 1.0 header.
177 */
178typedef SSMFILEHDR SSMFILEHDRV10AMD64, *PSSMFILEHDRV10AMD64;
179
180/** Saved state file magic base string. */
181#define SSMFILEHDR_MAGIC_BASE "\177VirtualBox SavedState "
182/** Saved state file v1.0 magic. */
183#define SSMFILEHDR_MAGIC_V1_0 "\177VirtualBox SavedState V1.0\n"
184/** Saved state file v1.1 magic. */
185#define SSMFILEHDR_MAGIC_V1_1 "\177VirtualBox SavedState V1.1\n"
186
187
188
189
190/**
191 * Data unit header.
192 */
193typedef struct SSMFILEUNITHDR
194{
195 /** Magic. */
196 char achMagic[8];
197 /** Number of bytes in this data unit including the header. */
198 uint64_t cbUnit;
199 /** Data version. */
200 uint32_t u32Version;
201 /** Instance number. */
202 uint32_t u32Instance;
203 /** Size of the data unit name including the terminator. (bytes) */
204 uint32_t cchName;
205 /** Data unit name. */
206 char szName[1];
207} SSMFILEUNITHDR, *PSSMFILEUNITHDR;
208
209/** Data unit magic. */
210#define SSMFILEUNITHDR_MAGIC "\nUnit\n"
211/** Data end marker magic. */
212#define SSMFILEUNITHDR_END "\nTheEnd"
213
214
215
216/*******************************************************************************
217* Internal Functions *
218*******************************************************************************/
219static int smmr3Register(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess, PSSMUNIT *ppUnit);
220static int ssmr3CalcChecksum(RTFILE File, uint64_t cbFile, uint32_t *pu32CRC);
221static void ssmR3Progress(PSSMHANDLE pSSM, uint64_t cbAdvance);
222static int ssmr3Validate(RTFILE File, PSSMFILEHDR pHdr, size_t *pcbFileHdr);
223static PSSMUNIT ssmr3Find(PVM pVM, const char *pszName, uint32_t u32Instance);
224static int ssmr3WriteFinish(PSSMHANDLE pSSM);
225static int ssmr3Write(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf);
226static DECLCALLBACK(int) ssmr3WriteOut(void *pvSSM, const void *pvBuf, size_t cbBuf);
227static void ssmr3ReadFinish(PSSMHANDLE pSSM);
228static int ssmr3Read(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf);
229static DECLCALLBACK(int) ssmr3ReadIn(void *pvSSM, void *pvBuf, size_t cbBuf, size_t *pcbRead);
230
231
232/**
233 * Internal registration worker.
234 *
235 * @returns VBox status code.
236 * @param pVM The VM handle.
237 * @param pszName Data unit name.
238 * @param u32Instance The instance id.
239 * @param u32Version The data unit version.
240 * @param cbGuess The guessed data unit size.
241 * @param ppUnit Where to store the insterted unit node.
242 * Caller must fill in the missing details.
243 */
244static int smmr3Register(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess, PSSMUNIT *ppUnit)
245{
246 /*
247 * Walk to the end of the list checking for duplicates as we go.
248 */
249 size_t cchName = strlen(pszName);
250 PSSMUNIT pUnitPrev = NULL;
251 PSSMUNIT pUnit = pVM->ssm.s.pHead;
252 while (pUnit)
253 {
254 if ( pUnit->u32Instance == u32Instance
255 && pUnit->cchName == cchName
256 && !memcmp(pUnit->szName, pszName, cchName))
257 {
258 AssertMsgFailed(("Duplicate registration %s\n", pszName));
259 return VERR_SSM_UNIT_EXISTS;
260 }
261 /* next */
262 pUnitPrev = pUnit;
263 pUnit = pUnit->pNext;
264 }
265
266 /*
267 * Allocate new node.
268 */
269 pUnit = (PSSMUNIT)MMR3HeapAllocZ(pVM, MM_TAG_SSM, RT_OFFSETOF(SSMUNIT, szName[cchName + 1]));
270 if (!pUnit)
271 return VERR_NO_MEMORY;
272
273 /*
274 * Fill in (some) data. (Stuff is zero'ed.)
275 */
276 pUnit->u32Version = u32Version;
277 pUnit->u32Instance = u32Instance;
278 pUnit->cbGuess = cbGuess;
279 pUnit->cchName = cchName;
280 memcpy(pUnit->szName, pszName, cchName);
281
282 /*
283 * Insert
284 */
285 if (pUnitPrev)
286 pUnitPrev->pNext = pUnit;
287 else
288 pVM->ssm.s.pHead = pUnit;
289
290 *ppUnit = pUnit;
291 return VINF_SUCCESS;
292}
293
294
295/**
296 * Register a PDM Devices data unit.
297 *
298 * @returns VBox status.
299 * @param pVM The VM handle.
300 * @param pDevIns Device instance.
301 * @param pszName Data unit name.
302 * @param u32Instance The instance identifier of the data unit.
303 * This must together with the name be unique.
304 * @param u32Version Data layout version number.
305 * @param cbGuess The approximate amount of data in the unit.
306 * Only for progress indicators.
307 * @param pfnSavePrep Prepare save callback, optional.
308 * @param pfnSaveExec Execute save callback, optional.
309 * @param pfnSaveDone Done save callback, optional.
310 * @param pfnLoadPrep Prepare load callback, optional.
311 * @param pfnLoadExec Execute load callback, optional.
312 * @param pfnLoadDone Done load callback, optional.
313 */
314SSMR3DECL(int) SSMR3Register(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
315 PFNSSMDEVSAVEPREP pfnSavePrep, PFNSSMDEVSAVEEXEC pfnSaveExec, PFNSSMDEVSAVEDONE pfnSaveDone,
316 PFNSSMDEVLOADPREP pfnLoadPrep, PFNSSMDEVLOADEXEC pfnLoadExec, PFNSSMDEVLOADDONE pfnLoadDone)
317{
318 PSSMUNIT pUnit;
319 int rc = smmr3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
320 if (VBOX_SUCCESS(rc))
321 {
322 pUnit->enmType = SSMUNITTYPE_DEV;
323 pUnit->u.Dev.pfnSavePrep = pfnSavePrep;
324 pUnit->u.Dev.pfnSaveExec = pfnSaveExec;
325 pUnit->u.Dev.pfnSaveDone = pfnSaveDone;
326 pUnit->u.Dev.pfnLoadPrep = pfnLoadPrep;
327 pUnit->u.Dev.pfnLoadExec = pfnLoadExec;
328 pUnit->u.Dev.pfnLoadDone = pfnLoadDone;
329 pUnit->u.Dev.pDevIns = pDevIns;
330 }
331 return rc;
332}
333
334
335/**
336 * Register a PDM driver data unit.
337 *
338 * @returns VBox status.
339 * @param pVM The VM handle.
340 * @param pDrvIns Driver instance.
341 * @param pszName Data unit name.
342 * @param u32Instance The instance identifier of the data unit.
343 * This must together with the name be unique.
344 * @param u32Version Data layout version number.
345 * @param cbGuess The approximate amount of data in the unit.
346 * Only for progress indicators.
347 * @param pfnSavePrep Prepare save callback, optional.
348 * @param pfnSaveExec Execute save callback, optional.
349 * @param pfnSaveDone Done save callback, optional.
350 * @param pfnLoadPrep Prepare load callback, optional.
351 * @param pfnLoadExec Execute load callback, optional.
352 * @param pfnLoadDone Done load callback, optional.
353 */
354SSMR3DECL(int) SSMR3RegisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
355 PFNSSMDRVSAVEPREP pfnSavePrep, PFNSSMDRVSAVEEXEC pfnSaveExec, PFNSSMDRVSAVEDONE pfnSaveDone,
356 PFNSSMDRVLOADPREP pfnLoadPrep, PFNSSMDRVLOADEXEC pfnLoadExec, PFNSSMDRVLOADDONE pfnLoadDone)
357{
358 PSSMUNIT pUnit;
359 int rc = smmr3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
360 if (VBOX_SUCCESS(rc))
361 {
362 pUnit->enmType = SSMUNITTYPE_DRV;
363 pUnit->u.Drv.pfnSavePrep = pfnSavePrep;
364 pUnit->u.Drv.pfnSaveExec = pfnSaveExec;
365 pUnit->u.Drv.pfnSaveDone = pfnSaveDone;
366 pUnit->u.Drv.pfnLoadPrep = pfnLoadPrep;
367 pUnit->u.Drv.pfnLoadExec = pfnLoadExec;
368 pUnit->u.Drv.pfnLoadDone = pfnLoadDone;
369 pUnit->u.Drv.pDrvIns = pDrvIns;
370 }
371 return rc;
372}
373
374
375/**
376 * Register a internal data unit.
377 *
378 * @returns VBox status.
379 * @param pVM The VM handle.
380 * @param pszName Data unit name.
381 * @param u32Instance The instance identifier of the data unit.
382 * This must together with the name be unique.
383 * @param u32Version Data layout version number.
384 * @param cbGuess The approximate amount of data in the unit.
385 * Only for progress indicators.
386 * @param pfnSavePrep Prepare save callback, optional.
387 * @param pfnSaveExec Execute save callback, optional.
388 * @param pfnSaveDone Done save callback, optional.
389 * @param pfnLoadPrep Prepare load callback, optional.
390 * @param pfnLoadExec Execute load callback, optional.
391 * @param pfnLoadDone Done load callback, optional.
392 */
393SSMR3DECL(int) SSMR3RegisterInternal(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
394 PFNSSMINTSAVEPREP pfnSavePrep, PFNSSMINTSAVEEXEC pfnSaveExec, PFNSSMINTSAVEDONE pfnSaveDone,
395 PFNSSMINTLOADPREP pfnLoadPrep, PFNSSMINTLOADEXEC pfnLoadExec, PFNSSMINTLOADDONE pfnLoadDone)
396{
397 PSSMUNIT pUnit;
398 int rc = smmr3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
399 if (VBOX_SUCCESS(rc))
400 {
401 pUnit->enmType = SSMUNITTYPE_INTERNAL;
402 pUnit->u.Internal.pfnSavePrep = pfnSavePrep;
403 pUnit->u.Internal.pfnSaveExec = pfnSaveExec;
404 pUnit->u.Internal.pfnSaveDone = pfnSaveDone;
405 pUnit->u.Internal.pfnLoadPrep = pfnLoadPrep;
406 pUnit->u.Internal.pfnLoadExec = pfnLoadExec;
407 pUnit->u.Internal.pfnLoadDone = pfnLoadDone;
408 }
409 return rc;
410}
411
412
413/**
414 * Register an external data unit.
415 *
416 * @returns VBox status.
417 * @param pVM The VM handle.
418 * @param pszName Data unit name.
419 * @param u32Instance The instance identifier of the data unit.
420 * This must together with the name be unique.
421 * @param u32Version Data layout version number.
422 * @param cbGuess The approximate amount of data in the unit.
423 * Only for progress indicators.
424 * @param pfnSavePrep Prepare save callback, optional.
425 * @param pfnSaveExec Execute save callback, optional.
426 * @param pfnSaveDone Done save callback, optional.
427 * @param pfnLoadPrep Prepare load callback, optional.
428 * @param pfnLoadExec Execute load callback, optional.
429 * @param pfnLoadDone Done load callback, optional.
430 * @param pvUser User argument.
431 */
432SSMR3DECL(int) SSMR3RegisterExternal(PVM pVM, const char *pszName, uint32_t u32Instance, uint32_t u32Version, size_t cbGuess,
433 PFNSSMEXTSAVEPREP pfnSavePrep, PFNSSMEXTSAVEEXEC pfnSaveExec, PFNSSMEXTSAVEDONE pfnSaveDone,
434 PFNSSMEXTLOADPREP pfnLoadPrep, PFNSSMEXTLOADEXEC pfnLoadExec, PFNSSMEXTLOADDONE pfnLoadDone, void *pvUser)
435{
436 PSSMUNIT pUnit;
437 int rc = smmr3Register(pVM, pszName, u32Instance, u32Version, cbGuess, &pUnit);
438 if (VBOX_SUCCESS(rc))
439 {
440 pUnit->enmType = SSMUNITTYPE_EXTERNAL;
441 pUnit->u.External.pfnSavePrep = pfnSavePrep;
442 pUnit->u.External.pfnSaveExec = pfnSaveExec;
443 pUnit->u.External.pfnSaveDone = pfnSaveDone;
444 pUnit->u.External.pfnLoadPrep = pfnLoadPrep;
445 pUnit->u.External.pfnLoadExec = pfnLoadExec;
446 pUnit->u.External.pfnLoadDone = pfnLoadDone;
447 pUnit->u.External.pvUser = pvUser;
448 }
449 return rc;
450}
451
452
453/**
454 * Deregister one or more PDM Device data units.
455 *
456 * @returns VBox status.
457 * @param pVM The VM handle.
458 * @param pDevIns Device instance.
459 * @param pszName Data unit name.
460 * Use NULL to deregister all data units for that device instance.
461 * @param u32Instance The instance identifier of the data unit.
462 * This must together with the name be unique.
463 * @remark Only for dynmaic data units and dynamic unloaded modules.
464 */
465SSMR3DECL(int) SSMR3Deregister(PVM pVM, PPDMDEVINS pDevIns, const char *pszName, uint32_t u32Instance)
466{
467 /*
468 * Validate input.
469 */
470 if (!pDevIns)
471 {
472 AssertMsgFailed(("pDevIns is NULL!\n"));
473 return VERR_INVALID_PARAMETER;
474 }
475
476 /*
477 * Search the list.
478 */
479 size_t cchName = pszName ? strlen(pszName) : 0;
480 int rc = pszName ? VERR_SSM_UNIT_NOT_FOUND : VINF_SUCCESS;
481 PSSMUNIT pUnitPrev = NULL;
482 PSSMUNIT pUnit = pVM->ssm.s.pHead;
483 while (pUnit)
484 {
485 if ( pUnit->enmType == SSMUNITTYPE_DEV
486 && ( !pszName
487 || ( pUnit->cchName == cchName
488 && !memcmp(pUnit->szName, pszName, cchName)))
489 && pUnit->u32Instance == u32Instance
490 )
491 {
492 if (pUnit->u.Dev.pDevIns == pDevIns)
493 {
494 /*
495 * Unlink it, advance pointer, and free the node.
496 */
497 PSSMUNIT pFree = pUnit;
498 pUnit = pUnit->pNext;
499 if (pUnitPrev)
500 pUnitPrev->pNext = pUnit;
501 else
502 pVM->ssm.s.pHead = pUnit;
503 Log(("SSM: Removed data unit '%s' (pdm dev).\n", pFree->szName));
504 MMR3HeapFree(pFree);
505 if (pszName)
506 return VINF_SUCCESS;
507 rc = VINF_SUCCESS;
508 continue;
509 }
510 else if (pszName)
511 {
512 AssertMsgFailed(("Caller is not owner! Owner=%p Caller=%p %s\n",
513 pUnit->u.Dev.pDevIns, pDevIns, pszName));
514 return VERR_SSM_UNIT_NOT_OWNER;
515 }
516 }
517
518 /* next */
519 pUnitPrev = pUnit;
520 pUnit = pUnit->pNext;
521 }
522
523 return rc;
524}
525
526
527/**
528 * Deregister one ore more PDM Driver data units.
529 *
530 * @returns VBox status.
531 * @param pVM The VM handle.
532 * @param pDrvIns Driver instance.
533 * @param pszName Data unit name.
534 * Use NULL to deregister all data units for that driver instance.
535 * @param u32Instance The instance identifier of the data unit.
536 * This must together with the name be unique. Ignored if pszName is NULL.
537 * @remark Only for dynmaic data units and dynamic unloaded modules.
538 */
539SSMR3DECL(int) SSMR3DeregisterDriver(PVM pVM, PPDMDRVINS pDrvIns, const char *pszName, uint32_t u32Instance)
540{
541 /*
542 * Validate input.
543 */
544 if (!pDrvIns)
545 {
546 AssertMsgFailed(("pDrvIns is NULL!\n"));
547 return VERR_INVALID_PARAMETER;
548 }
549
550 /*
551 * Search the list.
552 */
553 size_t cchName = pszName ? strlen(pszName) : 0;
554 int rc = pszName ? VERR_SSM_UNIT_NOT_FOUND : VINF_SUCCESS;
555 PSSMUNIT pUnitPrev = NULL;
556 PSSMUNIT pUnit = pVM->ssm.s.pHead;
557 while (pUnit)
558 {
559 if ( pUnit->enmType == SSMUNITTYPE_DRV
560 && ( !pszName
561 || ( pUnit->cchName == cchName
562 && !memcmp(pUnit->szName, pszName, cchName)
563 && pUnit->u32Instance == u32Instance))
564 )
565 {
566 if (pUnit->u.Drv.pDrvIns == pDrvIns)
567 {
568 /*
569 * Unlink it, advance pointer, and free the node.
570 */
571 PSSMUNIT pFree = pUnit;
572 pUnit = pUnit->pNext;
573 if (pUnitPrev)
574 pUnitPrev->pNext = pUnit;
575 else
576 pVM->ssm.s.pHead = pUnit;
577 Log(("SSM: Removed data unit '%s' (pdm drv).\n", pFree->szName));
578 MMR3HeapFree(pFree);
579 if (pszName)
580 return VINF_SUCCESS;
581 rc = VINF_SUCCESS;
582 continue;
583 }
584 else if (pszName)
585 {
586 AssertMsgFailed(("Caller is not owner! Owner=%p Caller=%p %s\n",
587 pUnit->u.Drv.pDrvIns, pDrvIns, pszName));
588 return VERR_SSM_UNIT_NOT_OWNER;
589 }
590 }
591
592 /* next */
593 pUnitPrev = pUnit;
594 pUnit = pUnit->pNext;
595 }
596
597 return rc;
598}
599
600/**
601 * Deregister a data unit.
602 *
603 * @returns VBox status.
604 * @param pVM The VM handle.
605 * @param enmType Unit type
606 * @param pszName Data unit name.
607 * @remark Only for dynmaic data units.
608 */
609static int ssmR3DeregisterByNameAndType(PVM pVM, const char *pszName, SSMUNITTYPE enmType)
610{
611 /*
612 * Validate input.
613 */
614 if (!pszName)
615 {
616 AssertMsgFailed(("pszName is NULL!\n"));
617 return VERR_INVALID_PARAMETER;
618 }
619
620 /*
621 * Search the list.
622 */
623 size_t cchName = strlen(pszName);
624 int rc = VERR_SSM_UNIT_NOT_FOUND;
625 PSSMUNIT pUnitPrev = NULL;
626 PSSMUNIT pUnit = pVM->ssm.s.pHead;
627 while (pUnit)
628 {
629 if ( pUnit->enmType == enmType
630 && pUnit->cchName == cchName
631 && !memcmp(pUnit->szName, pszName, cchName))
632 {
633 /*
634 * Unlink it, advance pointer, and free the node.
635 */
636 PSSMUNIT pFree = pUnit;
637 pUnit = pUnit->pNext;
638 if (pUnitPrev)
639 pUnitPrev->pNext = pUnit;
640 else
641 pVM->ssm.s.pHead = pUnit;
642 Log(("SSM: Removed data unit '%s' (type=%d).\n", pFree->szName, enmType));
643 MMR3HeapFree(pFree);
644 return VINF_SUCCESS;
645 }
646
647 /* next */
648 pUnitPrev = pUnit;
649 pUnit = pUnit->pNext;
650 }
651
652 return rc;
653}
654
655
656/**
657 * Deregister an internal data unit.
658 *
659 * @returns VBox status.
660 * @param pVM The VM handle.
661 * @param pszName Data unit name.
662 * @remark Only for dynmaic data units.
663 */
664SSMR3DECL(int) SSMR3DeregisterInternal(PVM pVM, const char *pszName)
665{
666 return ssmR3DeregisterByNameAndType(pVM, pszName, SSMUNITTYPE_INTERNAL);
667}
668
669
670/**
671 * Deregister an external data unit.
672 *
673 * @returns VBox status.
674 * @param pVM The VM handle.
675 * @param pszName Data unit name.
676 * @remark Only for dynmaic data units.
677 */
678SSMR3DECL(int) SSMR3DeregisterExternal(PVM pVM, const char *pszName)
679{
680 return ssmR3DeregisterByNameAndType(pVM, pszName, SSMUNITTYPE_EXTERNAL);
681}
682
683
684/**
685 * Calculate the checksum of a file portion.
686 *
687 * The current implementation is a cut&past of the libkern/crc32.c file from FreeBSD.
688 *
689 * @returns VBox status.
690 * @param File Handle to the file.
691 * @param cbFile Size of the file.
692 * @param pu32CRC Where to store the calculated checksum.
693 */
694static int ssmr3CalcChecksum(RTFILE File, uint64_t cbFile, uint32_t *pu32CRC)
695{
696 /*
697 * Allocate a buffer.
698 */
699 void *pvBuf = RTMemTmpAlloc(32*1024);
700 if (!pvBuf)
701 return VERR_NO_TMP_MEMORY;
702
703 /*
704 * Loop reading and calculating CRC32.
705 */
706 int rc = VINF_SUCCESS;
707 uint32_t u32CRC = RTCrc32Start();
708 while (cbFile)
709 {
710 /* read chunk */
711 register unsigned cbToRead = 32*1024;
712 if (cbFile < 32*1024)
713 cbToRead = (unsigned)cbFile;
714 rc = RTFileRead(File, pvBuf, cbToRead, NULL);
715 if (VBOX_FAILURE(rc))
716 {
717 AssertMsgFailed(("Failed with rc=%Vrc while calculating crc.\n", rc));
718 RTMemTmpFree(pvBuf);
719 return rc;
720 }
721
722 /* update total */
723 cbFile -= cbToRead;
724
725 /* calc crc32. */
726 u32CRC = RTCrc32Process(u32CRC, pvBuf, cbToRead);
727 }
728 RTMemTmpFree(pvBuf);
729
730 /* store the calculated crc */
731 u32CRC = RTCrc32Finish(u32CRC);
732 Log(("SSM: u32CRC=0x%08x\n", u32CRC));
733 *pu32CRC = u32CRC;
734
735 return VINF_SUCCESS;
736}
737
738
739/**
740 * Works the progress calculation.
741 *
742 * @param pSSM The SSM handle.
743 * @param cbAdvance Number of bytes to advance
744 */
745static void ssmR3Progress(PSSMHANDLE pSSM, uint64_t cbAdvance)
746{
747 /* Can't advance it beyond the estimated end of the unit. */
748 uint64_t cbLeft = pSSM->offEstUnitEnd - pSSM->offEst;
749 if (cbAdvance > cbLeft)
750 cbAdvance = cbLeft;
751 pSSM->offEst += cbAdvance;
752
753 /* uPercentPrepare% prepare, xx% exec, uPercentDone% done+crc */
754 while (pSSM->offEst >= pSSM->offEstProgress && pSSM->uPercent <= 100-pSSM->uPercentDone)
755 {
756 if (pSSM->pfnProgress)
757 pSSM->pfnProgress(pSSM->pVM, pSSM->uPercent, pSSM->pvUser);
758 pSSM->uPercent++;
759 pSSM->offEstProgress = (pSSM->uPercent - pSSM->uPercentPrepare) * pSSM->cbEstTotal /
760 (100-pSSM->uPercentDone-pSSM->uPercentPrepare);
761 }
762}
763
764
765/**
766 * Start VM save operation.
767 * The caller must be the emulation thread!
768 *
769 * @returns VBox status.
770 * @param pVM The VM handle.
771 * @param pszFilename Name of the file to save the state in.
772 * @param enmAfter What is planned after a successful save operation.
773 * @param pfnProgress Progress callback. Optional.
774 * @param pvUser User argument for the progress callback.
775 */
776SSMR3DECL(int) SSMR3Save(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser)
777{
778 LogFlow(("SSMR3Save: pszFilename=%p:{%s} enmAfter=%d pfnProgress=%p pvUser=%p\n", pszFilename, pszFilename, enmAfter, pfnProgress, pvUser));
779
780 /*
781 * Validate input.
782 */
783 if ( enmAfter != SSMAFTER_DESTROY
784 && enmAfter != SSMAFTER_CONTINUE)
785 {
786 AssertMsgFailed(("Invalid enmAfter=%d!\n", enmAfter));
787 return VERR_INVALID_PARAMETER;
788 }
789
790 /*
791 * Try open the file.
792 */
793 SSMHANDLE Handle = {0};
794 Handle.enmAfter = enmAfter;
795 Handle.pVM = pVM;
796 Handle.cbFileHdr = sizeof(SSMFILEHDR);
797 Handle.pfnProgress = pfnProgress;
798 Handle.pvUser = pvUser;
799 /*
800 * The 'done' part might take much time:
801 * (1) Call the SaveDone function of each module
802 * (2) Calculate the Checksum
803 * (3) RTFileClose() will probably flush the write cache
804 */
805 Handle.uPercentPrepare = 2;
806 Handle.uPercentDone = 20; /* reserve substantial time for crc-checking the image */
807 int rc = RTFileOpen(&Handle.File, pszFilename, RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_WRITE);
808 if (VBOX_FAILURE(rc))
809 {
810 LogRel(("SSM: Failed to create save state file '%s', rc=%Vrc.\n", pszFilename, rc));
811 return rc;
812 }
813
814 Log(("SSM: Starting state save to file '%s'...\n", pszFilename));
815
816 /*
817 * Write header.
818 */
819 SSMFILEHDR Hdr = { SSMFILEHDR_MAGIC_V1_1, 0, 0, 0 };
820 rc = RTFileWrite(Handle.File, &Hdr, sizeof(Hdr), NULL);
821 if (VBOX_SUCCESS(rc))
822 {
823 /*
824 * Clear the per unit flags.
825 */
826 PSSMUNIT pUnit;
827 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
828 pUnit->fCalled = false;
829
830 /*
831 * Do the prepare run.
832 */
833 Handle.rc = VINF_SUCCESS;
834 Handle.enmOp = SSMSTATE_SAVE_PREP;
835 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
836 {
837 switch (pUnit->enmType)
838 {
839 case SSMUNITTYPE_DEV:
840 if (pUnit->u.Dev.pfnSavePrep)
841 {
842 rc = pUnit->u.Dev.pfnSavePrep(pUnit->u.Dev.pDevIns, &Handle);
843 pUnit->fCalled = true;
844 }
845 break;
846 case SSMUNITTYPE_DRV:
847 if (pUnit->u.Drv.pfnSavePrep)
848 {
849 rc = pUnit->u.Drv.pfnSavePrep(pUnit->u.Drv.pDrvIns, &Handle);
850 pUnit->fCalled = true;
851 }
852 break;
853 case SSMUNITTYPE_INTERNAL:
854 if (pUnit->u.Internal.pfnSavePrep)
855 {
856 rc = pUnit->u.Internal.pfnSavePrep(pVM, &Handle);
857 pUnit->fCalled = true;
858 }
859 break;
860 case SSMUNITTYPE_EXTERNAL:
861 if (pUnit->u.External.pfnSavePrep)
862 {
863 rc = pUnit->u.External.pfnSavePrep(&Handle, pUnit->u.External.pvUser);
864 pUnit->fCalled = true;
865 }
866 break;
867 }
868 if (VBOX_FAILURE(rc))
869 {
870 LogRel(("SSM: Prepare save failed with rc=%Vrc for data unit '%s.\n", rc, pUnit->szName));
871 break;
872 }
873
874 Handle.cbEstTotal += pUnit->cbGuess;
875 }
876
877 /* Progress. */
878 if (pfnProgress)
879 pfnProgress(pVM, Handle.uPercentPrepare-1, pvUser);
880 Handle.uPercent = Handle.uPercentPrepare;
881
882 /*
883 * Do the execute run.
884 */
885 if (VBOX_SUCCESS(rc))
886 {
887 Handle.enmOp = SSMSTATE_SAVE_EXEC;
888 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
889 {
890 /*
891 * Estimate.
892 */
893 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
894 Handle.offEstUnitEnd += pUnit->cbGuess;
895
896 /*
897 * Does this unit have a callback? If, not skip it.
898 */
899 bool fSkip;
900 switch (pUnit->enmType)
901 {
902 case SSMUNITTYPE_DEV: fSkip = pUnit->u.Dev.pfnSaveExec == NULL; break;
903 case SSMUNITTYPE_DRV: fSkip = pUnit->u.Drv.pfnSaveExec == NULL; break;
904 case SSMUNITTYPE_INTERNAL: fSkip = pUnit->u.Internal.pfnSaveExec == NULL; break;
905 case SSMUNITTYPE_EXTERNAL: fSkip = pUnit->u.External.pfnSaveExec == NULL; break;
906 default: fSkip = true; break;
907 }
908 if (fSkip)
909 {
910 pUnit->fCalled = true;
911 continue;
912 }
913
914 /*
915 * Write data unit header
916 */
917 uint64_t offHdr = RTFileTell(Handle.File);
918 SSMFILEUNITHDR UnitHdr = { SSMFILEUNITHDR_MAGIC, 0, pUnit->u32Version, pUnit->u32Instance, pUnit->cchName + 1, { '\0' } };
919 rc = RTFileWrite(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), NULL);
920 if (VBOX_SUCCESS(rc))
921 {
922 rc = RTFileWrite(Handle.File, &pUnit->szName[0], pUnit->cchName + 1, NULL);
923 if (VBOX_SUCCESS(rc))
924 {
925 /*
926 * Call the execute handler.
927 */
928 switch (pUnit->enmType)
929 {
930 case SSMUNITTYPE_DEV:
931 rc = pUnit->u.Dev.pfnSaveExec(pUnit->u.Dev.pDevIns, &Handle);
932 break;
933 case SSMUNITTYPE_DRV:
934 rc = pUnit->u.Drv.pfnSaveExec(pUnit->u.Drv.pDrvIns, &Handle);
935 break;
936 case SSMUNITTYPE_INTERNAL:
937 rc = pUnit->u.Internal.pfnSaveExec(pVM, &Handle);
938 break;
939 case SSMUNITTYPE_EXTERNAL:
940 pUnit->u.External.pfnSaveExec(&Handle, pUnit->u.External.pvUser);
941 rc = Handle.rc;
942 break;
943 }
944 pUnit->fCalled = true;
945 if (VBOX_FAILURE(Handle.rc) && VBOX_SUCCESS(rc))
946 rc = Handle.rc;
947 if (VBOX_SUCCESS(rc))
948 {
949 /*
950 * Flush buffer / end compression stream.
951 */
952 if (Handle.pZipComp)
953 rc = ssmr3WriteFinish(&Handle);
954 if (VBOX_SUCCESS(rc))
955 {
956 /*
957 * Update header with correct length.
958 */
959 uint64_t offEnd = RTFileTell(Handle.File);
960 rc = RTFileSeek(Handle.File, offHdr, RTFILE_SEEK_BEGIN, NULL);
961 if (VBOX_SUCCESS(rc))
962 {
963 UnitHdr.cbUnit = offEnd - offHdr;
964 rc = RTFileWrite(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), NULL);
965 if (VBOX_SUCCESS(rc))
966 {
967 rc = RTFileSeek(Handle.File, offEnd, RTFILE_SEEK_BEGIN, NULL);
968 if (VBOX_SUCCESS(rc))
969 Log(("SSM: Data unit: offset %#9llx size %9lld '%s'\n", offHdr, UnitHdr.cbUnit, pUnit->szName));
970 }
971 }
972 }
973 else
974 {
975 LogRel(("SSM: Failed ending compression stream. rc=%Vrc\n", rc));
976 break;
977 }
978 }
979 else
980 {
981 LogRel(("SSM: Execute save failed with rc=%Vrc for data unit '%s.\n", rc, pUnit->szName));
982 break;
983 }
984 }
985 }
986 if (VBOX_FAILURE(rc))
987 {
988 LogRel(("SSM: Failed to write unit header. rc=%Vrc\n", rc));
989 break;
990 }
991 } /* for each unit */
992
993 /* finish the progress. */
994 if (VBOX_SUCCESS(rc))
995 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
996 }
997 /* (progress should be pending 99% now) */
998 AssertMsg(VBOX_FAILURE(rc) || Handle.uPercent == (101-Handle.uPercentDone), ("%d\n", Handle.uPercent));
999
1000 /*
1001 * Do the done run.
1002 */
1003 Handle.rc = rc;
1004 Handle.enmOp = SSMSTATE_SAVE_DONE;
1005 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1006 {
1007 switch (pUnit->enmType)
1008 {
1009 case SSMUNITTYPE_DEV:
1010 if ( pUnit->u.Dev.pfnSaveDone
1011 && ( pUnit->fCalled
1012 || (!pUnit->u.Dev.pfnSavePrep && !pUnit->u.Dev.pfnSaveExec)))
1013 rc = pUnit->u.Dev.pfnSaveDone(pUnit->u.Dev.pDevIns, &Handle);
1014 break;
1015 case SSMUNITTYPE_DRV:
1016 if ( pUnit->u.Drv.pfnSaveDone
1017 && ( pUnit->fCalled
1018 || (!pUnit->u.Drv.pfnSavePrep && !pUnit->u.Drv.pfnSaveExec)))
1019 rc = pUnit->u.Drv.pfnSaveDone(pUnit->u.Drv.pDrvIns, &Handle);
1020 break;
1021 case SSMUNITTYPE_INTERNAL:
1022 if ( pUnit->u.Internal.pfnSaveDone
1023 && ( pUnit->fCalled
1024 || (!pUnit->u.Internal.pfnSavePrep && !pUnit->u.Internal.pfnSaveExec)))
1025 rc = pUnit->u.Internal.pfnSaveDone(pVM, &Handle);
1026 break;
1027 case SSMUNITTYPE_EXTERNAL:
1028 if ( pUnit->u.External.pfnSaveDone
1029 && ( pUnit->fCalled
1030 || (!pUnit->u.External.pfnSavePrep && !pUnit->u.External.pfnSaveExec)))
1031 rc = pUnit->u.External.pfnSaveDone(&Handle, pUnit->u.External.pvUser);
1032 break;
1033 }
1034 if (VBOX_FAILURE(rc))
1035 {
1036 LogRel(("SSM: Done save failed with rc=%Vrc for data unit '%s.\n", rc, pUnit->szName));
1037 if (VBOX_SUCCESS(Handle.rc))
1038 Handle.rc = rc;
1039 }
1040 }
1041 rc = Handle.rc;
1042
1043 /*
1044 * Finalize the file if successfully saved.
1045 */
1046 if (VBOX_SUCCESS(rc))
1047 {
1048 /* end record */
1049 SSMFILEUNITHDR UnitHdr = { SSMFILEUNITHDR_END, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), 0, '\0'};
1050 rc = RTFileWrite(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName[0]), NULL);
1051 if (VBOX_SUCCESS(rc))
1052 {
1053 /* get size */
1054 Hdr.cbFile = RTFileTell(Handle.File);
1055 /* calc checksum */
1056 rc = RTFileSeek(Handle.File, RT_OFFSETOF(SSMFILEHDR, u32CRC) + sizeof(Hdr.u32CRC), RTFILE_SEEK_BEGIN, NULL);
1057 if (VBOX_SUCCESS(rc))
1058 rc = ssmr3CalcChecksum(Handle.File, Hdr.cbFile - sizeof(Hdr), &Hdr.u32CRC);
1059 if (VBOX_SUCCESS(rc))
1060 {
1061 if (pfnProgress)
1062 pfnProgress(pVM, 90, pvUser);
1063
1064 /*
1065 * Write the update the header to the file.
1066 */
1067 rc = RTFileSeek(Handle.File, 0, RTFILE_SEEK_BEGIN, NULL);
1068 if (VBOX_SUCCESS(rc))
1069 rc = RTFileWrite(Handle.File, &Hdr, sizeof(Hdr), NULL);
1070 if (VBOX_SUCCESS(rc))
1071 {
1072 rc = RTFileClose(Handle.File);
1073 AssertRC(rc);
1074 if (pfnProgress)
1075 pfnProgress(pVM, 100, pvUser);
1076 Log(("SSM: Successfully saved the vm state to '%s'.\n", pszFilename));
1077 Log(("\n\n\n"));
1078 DBGFR3InfoLog(pVM, "cpum", "verbose");
1079 DBGFR3InfoLog(pVM, "timers", NULL);
1080 DBGFR3InfoLog(pVM, "activetimers", NULL);
1081 DBGFR3InfoLog(pVM, "ioport", NULL);
1082 DBGFR3InfoLog(pVM, "mmio", NULL);
1083 DBGFR3InfoLog(pVM, "phys", NULL);
1084 Log(("\n\n\n"));
1085 return VINF_SUCCESS;
1086 }
1087
1088 }
1089 }
1090 LogRel(("SSM: Failed to finalize state file! rc=%Vrc\n", pszFilename));
1091 }
1092 }
1093
1094 /*
1095 * Delete the file on failure and destroy any compressors.
1096 */
1097 int rc2 = RTFileClose(Handle.File);
1098 AssertRC(rc2);
1099 rc2 = RTFileDelete(pszFilename);
1100 AssertRC(rc2);
1101 if (Handle.pZipComp)
1102 RTZipCompDestroy(Handle.pZipComp);
1103
1104 return rc;
1105}
1106
1107
1108/**
1109 * Validates the integrity of a saved state file.
1110 *
1111 * @returns VBox status.
1112 * @param File File to validate.
1113 * The file position is undefined on return.
1114 * @param pHdr Where to store the file header.
1115 * @param pcbFileHdr Where to store the file header size.
1116 */
1117static int ssmr3Validate(RTFILE File, PSSMFILEHDR pHdr, size_t *pcbFileHdr)
1118{
1119 /*
1120 * Read the header.
1121 */
1122 int rc = RTFileRead(File, pHdr, sizeof(*pHdr), NULL);
1123 if (VBOX_FAILURE(rc))
1124 {
1125 Log(("SSM: Failed to read file header. rc=%Vrc\n", rc));
1126 return rc;
1127 }
1128
1129 /*
1130 * Verify the magic and make adjustments for versions differences.
1131 */
1132 if (memcmp(pHdr->achMagic, SSMFILEHDR_MAGIC_BASE, sizeof(SSMFILEHDR_MAGIC_BASE) - 1))
1133 {
1134 Log(("SSM: Not a saved state file. magic=%.*s\n", sizeof(pHdr->achMagic) - 1, pHdr->achMagic));
1135 return VERR_SSM_INTEGRITY_MAGIC;
1136 }
1137
1138 size_t offCrc32 = RT_OFFSETOF(SSMFILEHDR, u32CRC) + sizeof(pHdr->u32CRC);
1139 *pcbFileHdr = sizeof(*pHdr);
1140 if (!memcmp(pHdr->achMagic, SSMFILEHDR_MAGIC_V1_0, sizeof(SSMFILEHDR_MAGIC_V1_0)))
1141 {
1142 if (pHdr->MachineUuid.au32[3])
1143 {
1144 SSMFILEHDRV10X86 OldHdr;
1145 memcpy(&OldHdr, pHdr, sizeof(OldHdr));
1146 pHdr->cbFile = OldHdr.cbFile;
1147 pHdr->u32CRC = OldHdr.u32CRC;
1148 pHdr->u32Reserved = 0;
1149 pHdr->MachineUuid = OldHdr.MachineUuid;
1150
1151 offCrc32 = RT_OFFSETOF(SSMFILEHDRV10X86, u32CRC) + sizeof(pHdr->u32CRC);
1152 *pcbFileHdr = sizeof(OldHdr);
1153 }
1154 else
1155 {
1156 /* (It's identical, but this doesn't harm us and will continue working after future changes.) */
1157 SSMFILEHDRV10AMD64 OldHdr;
1158 memcpy(&OldHdr, pHdr, sizeof(OldHdr));
1159 pHdr->cbFile = OldHdr.cbFile;
1160 pHdr->u32CRC = OldHdr.u32CRC;
1161 pHdr->u32Reserved = 0;
1162 pHdr->MachineUuid = OldHdr.MachineUuid;
1163
1164 offCrc32 = RT_OFFSETOF(SSMFILEHDRV10AMD64, u32CRC) + sizeof(pHdr->u32CRC);
1165 *pcbFileHdr = sizeof(OldHdr);
1166 }
1167 }
1168 else if (memcmp(pHdr->achMagic, SSMFILEHDR_MAGIC_V1_1, sizeof(SSMFILEHDR_MAGIC_V1_1)))
1169 {
1170 Log(("SSM: Unknown file format version. magic=%.*s\n", sizeof(pHdr->achMagic) - 1, pHdr->achMagic));
1171 return VERR_SSM_INTEGRITY_VERSION;
1172 }
1173
1174 /*
1175 * Verify the file size.
1176 */
1177 uint64_t cbFile;
1178 rc = RTFileGetSize(File, &cbFile);
1179 if (VBOX_FAILURE(rc))
1180 {
1181 Log(("SSM: Failed to get file size. rc=%Vrc\n", rc));
1182 return rc;
1183 }
1184 if (cbFile != pHdr->cbFile)
1185 {
1186 Log(("SSM: File size mistmatch. hdr.cbFile=%lld actual %lld\n", pHdr->cbFile, cbFile));
1187 return VERR_SSM_INTEGRITY_SIZE;
1188 }
1189
1190 /*
1191 * Verify the checksum.
1192 */
1193 rc = RTFileSeek(File, offCrc32, RTFILE_SEEK_BEGIN, NULL);
1194 if (VBOX_FAILURE(rc))
1195 {
1196 Log(("SSM: Failed to seek to crc start. rc=%Vrc\n", rc));
1197 return rc;
1198 }
1199 uint32_t u32CRC;
1200 rc = ssmr3CalcChecksum(File, pHdr->cbFile - *pcbFileHdr, &u32CRC);
1201 if (VBOX_FAILURE(rc))
1202 return rc;
1203 if (u32CRC != pHdr->u32CRC)
1204 {
1205 Log(("SSM: Invalid CRC! Calculated %#08x, in header %#08x\n", u32CRC, pHdr->u32CRC));
1206 return VERR_SSM_INTEGRITY_CRC;
1207 }
1208
1209 /*
1210 * Verify Virtual Machine UUID.
1211 */
1212 RTUUID Uuid;
1213 memset(&Uuid, 0, sizeof(Uuid));
1214/** @todo get machine uuids CFGGetUuid(, &Uuid); */
1215 if ( RTUuidCompare(&pHdr->MachineUuid, &Uuid)
1216 && !RTUuidIsNull(&pHdr->MachineUuid)) /* temporary hack, allowing NULL uuids. */
1217 {
1218 Log(("SSM: The UUID of the saved state doesn't match the running VM.\n"));
1219 return VERR_SMM_INTEGRITY_MACHINE;
1220 }
1221
1222 return VINF_SUCCESS;
1223}
1224
1225
1226/**
1227 * Find a data unit by name.
1228 *
1229 * @returns Pointer to the unit.
1230 * @returns NULL if not found.
1231 * @param pVM VM handle.
1232 * @param pszName Data unit name.
1233 * @param u32Instance The data unit instance id.
1234 */
1235static PSSMUNIT ssmr3Find(PVM pVM, const char *pszName, uint32_t u32Instance)
1236{
1237 size_t cchName = strlen(pszName);
1238 PSSMUNIT pUnit = pVM->ssm.s.pHead;
1239 while ( pUnit
1240 && ( pUnit->u32Instance != u32Instance
1241 || pUnit->cchName != cchName
1242 || memcmp(pUnit->szName, pszName, cchName)))
1243 pUnit = pUnit->pNext;
1244 return pUnit;
1245}
1246
1247
1248/**
1249 * Load VM save operation.
1250 * The caller must be the emulation thread!
1251 *
1252 * @returns VBox status.
1253 * @param pVM The VM handle.
1254 * @param pszFilename Name of the file to save the state in.
1255 * @param enmAfter What is planned after a successful load operation.
1256 * Only acceptable values are SSMAFTER_RESUME and SSMAFTER_DEBUG_IT.
1257 * @param pfnProgress Progress callback. Optional.
1258 * @param pvUser User argument for the progress callback.
1259 */
1260SSMR3DECL(int) SSMR3Load(PVM pVM, const char *pszFilename, SSMAFTER enmAfter, PFNVMPROGRESS pfnProgress, void *pvUser)
1261{
1262 LogFlow(("SSMR3Load: pszFilename=%p:{%s} enmAfter=%d pfnProgress=%p pvUser=%p\n", pszFilename, pszFilename, enmAfter, pfnProgress, pvUser));
1263
1264 /*
1265 * Validate input.
1266 */
1267 if ( enmAfter != SSMAFTER_RESUME
1268 && enmAfter != SSMAFTER_DEBUG_IT)
1269 {
1270 AssertMsgFailed(("Invalid enmAfter=%d!\n", enmAfter));
1271 return VERR_INVALID_PARAMETER;
1272 }
1273
1274 /*
1275 * Open the file.
1276 */
1277 SSMHANDLE Handle = {0};
1278 Handle.enmAfter = enmAfter;
1279 Handle.pVM = pVM;
1280 Handle.cbFileHdr = sizeof(SSMFILEHDR);
1281 Handle.pfnProgress = pfnProgress;
1282 Handle.pvUser = pvUser;
1283 Handle.uPercentPrepare = 20; /* reserve substantial time for validating the image */
1284 Handle.uPercentDone = 2;
1285 int rc = RTFileOpen(&Handle.File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1286 if (VBOX_FAILURE(rc))
1287 {
1288 Log(("SSM: Failed to open save state file '%s', rc=%Vrc.\n", pszFilename, rc));
1289 return rc;
1290 }
1291
1292 /*
1293 * Read file header and validate it.
1294 */
1295 SSMFILEHDR Hdr;
1296 rc = ssmr3Validate(Handle.File, &Hdr, &Handle.cbFileHdr);
1297 if (VBOX_SUCCESS(rc))
1298 {
1299 /*
1300 * Clear the per unit flags.
1301 */
1302 PSSMUNIT pUnit;
1303 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1304 pUnit->fCalled = false;
1305
1306 /*
1307 * Do the prepare run.
1308 */
1309 Handle.rc = VINF_SUCCESS;
1310 Handle.enmOp = SSMSTATE_LOAD_PREP;
1311 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1312 {
1313 switch (pUnit->enmType)
1314 {
1315 case SSMUNITTYPE_DEV:
1316 if (pUnit->u.Dev.pfnLoadPrep)
1317 {
1318 rc = pUnit->u.Dev.pfnLoadPrep(pUnit->u.Dev.pDevIns, &Handle);
1319 pUnit->fCalled = true;
1320 }
1321 break;
1322 case SSMUNITTYPE_DRV:
1323 if (pUnit->u.Drv.pfnLoadPrep)
1324 {
1325 rc = pUnit->u.Drv.pfnLoadPrep(pUnit->u.Drv.pDrvIns, &Handle);
1326 pUnit->fCalled = true;
1327 }
1328 break;
1329 case SSMUNITTYPE_INTERNAL:
1330 if (pUnit->u.Internal.pfnLoadPrep)
1331 {
1332 rc = pUnit->u.Internal.pfnLoadPrep(pVM, &Handle);
1333 pUnit->fCalled = true;
1334 }
1335 break;
1336 case SSMUNITTYPE_EXTERNAL:
1337 if (pUnit->u.External.pfnLoadPrep)
1338 {
1339 rc = pUnit->u.External.pfnLoadPrep(&Handle, pUnit->u.External.pvUser);
1340 pUnit->fCalled = true;
1341 }
1342 break;
1343 }
1344 if (VBOX_FAILURE(rc))
1345 {
1346 LogRel(("SSM: Prepare load failed with rc=%Vrc for data unit '%s.\n", rc, pUnit->szName));
1347 break;
1348 }
1349 }
1350
1351 /* pending 2% */
1352 if (pfnProgress)
1353 pfnProgress(pVM, Handle.uPercentPrepare-1, pvUser);
1354 Handle.uPercent = Handle.uPercentPrepare;
1355 Handle.cbEstTotal = Hdr.cbFile;
1356
1357 /*
1358 * Do the execute run.
1359 */
1360 if (VBOX_SUCCESS(rc))
1361 rc = RTFileSeek(Handle.File, Handle.cbFileHdr, RTFILE_SEEK_BEGIN, NULL);
1362 if (VBOX_SUCCESS(rc))
1363 {
1364 char *pszName = NULL;
1365 size_t cchName = 0;
1366 Handle.enmOp = SSMSTATE_LOAD_EXEC;
1367 for (;;)
1368 {
1369 /*
1370 * Save the current file position and read the data unit header.
1371 */
1372 uint64_t offUnit = RTFileTell(Handle.File);
1373 SSMFILEUNITHDR UnitHdr;
1374 rc = RTFileRead(Handle.File, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName), NULL);
1375 if (VBOX_SUCCESS(rc))
1376 {
1377 /*
1378 * Check the magic and see if it's valid and whether it is a end header or not.
1379 */
1380 if (memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(SSMFILEUNITHDR_MAGIC)))
1381 {
1382 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_END, sizeof(SSMFILEUNITHDR_END)))
1383 {
1384 Log(("SSM: EndOfFile: offset %#9llx size %9d\n", offUnit, UnitHdr.cbUnit));
1385 /* Complete the progress bar (pending 99% afterwards). */
1386 Handle.offEstUnitEnd = Handle.cbEstTotal;
1387 ssmR3Progress(&Handle, Handle.cbEstTotal - Handle.offEst);
1388 break;
1389 }
1390 LogRel(("SSM: Invalid unit magic at offset %#llx (%lld), '%.*s'!\n",
1391 offUnit, offUnit, sizeof(UnitHdr.achMagic) - 1, &UnitHdr.achMagic[0]));
1392 rc = VERR_SSM_INTEGRITY_UNIT_MAGIC;
1393 break;
1394 }
1395
1396 /*
1397 * Read the name.
1398 * Adjust the name buffer first.
1399 */
1400 if (cchName < UnitHdr.cchName)
1401 {
1402 if (pszName)
1403 RTMemTmpFree(pszName);
1404 cchName = RT_ALIGN_Z(UnitHdr.cchName, 64);
1405 pszName = (char *)RTMemTmpAlloc(cchName);
1406 }
1407 if (pszName)
1408 {
1409 rc = RTFileRead(Handle.File, pszName, UnitHdr.cchName, NULL);
1410 if (VBOX_SUCCESS(rc))
1411 {
1412 if (!pszName[UnitHdr.cchName - 1])
1413 {
1414 Log(("SSM: Data unit: offset %#9llx size %9lld '%s'\n", offUnit, UnitHdr.cbUnit, pszName));
1415
1416 /*
1417 * Progress
1418 */
1419 Handle.offEstUnitEnd += UnitHdr.cbUnit;
1420
1421 /*
1422 * Find the data unit in our internal table.
1423 */
1424 pUnit = ssmr3Find(pVM, pszName, UnitHdr.u32Instance);
1425 if (pUnit)
1426 {
1427 /*
1428 * Call the execute handler.
1429 */
1430 Handle.cbUnitLeft = UnitHdr.cbUnit - RT_OFFSETOF(SSMFILEUNITHDR, szName[UnitHdr.cchName]);
1431 switch (pUnit->enmType)
1432 {
1433 case SSMUNITTYPE_DEV:
1434 if (pUnit->u.Dev.pfnLoadExec)
1435 rc = pUnit->u.Dev.pfnLoadExec(pUnit->u.Dev.pDevIns, &Handle, UnitHdr.u32Version);
1436 else
1437 rc = VERR_SSM_NO_LOAD_EXEC;
1438 break;
1439 case SSMUNITTYPE_DRV:
1440 if (pUnit->u.Drv.pfnLoadExec)
1441 rc = pUnit->u.Drv.pfnLoadExec(pUnit->u.Drv.pDrvIns, &Handle, UnitHdr.u32Version);
1442 else
1443 rc = VERR_SSM_NO_LOAD_EXEC;
1444 break;
1445 case SSMUNITTYPE_INTERNAL:
1446 if (pUnit->u.Internal.pfnLoadExec)
1447 rc = pUnit->u.Internal.pfnLoadExec(pVM, &Handle, UnitHdr.u32Version);
1448 else
1449 rc = VERR_SSM_NO_LOAD_EXEC;
1450 break;
1451 case SSMUNITTYPE_EXTERNAL:
1452 if (pUnit->u.External.pfnLoadExec)
1453 {
1454 rc = pUnit->u.External.pfnLoadExec(&Handle, pUnit->u.External.pvUser, UnitHdr.u32Version);
1455 if (!rc)
1456 rc = Handle.rc;
1457 }
1458 else
1459 rc = VERR_SSM_NO_LOAD_EXEC;
1460 break;
1461 }
1462 if (rc != VERR_SSM_NO_LOAD_EXEC)
1463 {
1464 /*
1465 * Close the reader stream.
1466 */
1467 if (Handle.pZipDecomp)
1468 ssmr3ReadFinish(&Handle);
1469
1470 pUnit->fCalled = true;
1471 if (VBOX_SUCCESS(rc))
1472 rc = Handle.rc;
1473 if (VBOX_SUCCESS(rc))
1474 {
1475 /*
1476 * Now, we'll check the current position to see if all, or
1477 * more than all, the data was read.
1478 *
1479 * Note! Because of buffering / compression we'll only see the
1480 * really bad ones here.
1481 */
1482 uint64_t off = RTFileTell(Handle.File);
1483 int64_t i64Diff = off - (offUnit + UnitHdr.cbUnit);
1484 if (i64Diff < 0)
1485 {
1486 Log(("SSM: Unit '%s' left %lld bytes unread!\n", pszName, -i64Diff));
1487 rc = RTFileSeek(Handle.File, offUnit + UnitHdr.cbUnit, RTFILE_SEEK_BEGIN, NULL);
1488 }
1489 else if (i64Diff > 0)
1490 {
1491 LogRel(("SSM: Unit '%s' read %lld bytes too much!\n", pszName, i64Diff));
1492 rc = VERR_SSM_INTEGRITY;
1493 break;
1494 }
1495
1496 /* Advance the progress bar to the end of the block. */
1497 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
1498 }
1499 else
1500 {
1501 /*
1502 * We failed, but if loading for the debugger ignore certain failures
1503 * just to get it all loaded (big hack).
1504 */
1505 LogRel(("SSM: LoadExec failed with rc=%Vrc for unit '%s'!\n", rc, pszName));
1506 if ( Handle.enmAfter != SSMAFTER_DEBUG_IT
1507 || rc != VERR_SSM_LOADED_TOO_MUCH)
1508 break;
1509 Handle.rc = rc = VINF_SUCCESS;
1510 ssmR3Progress(&Handle, Handle.offEstUnitEnd - Handle.offEst);
1511 }
1512 }
1513 else
1514 {
1515 LogRel(("SSM: No load exec callback for unit '%s'!\n", pszName));
1516 rc = VERR_SSM_INTEGRITY;
1517 break;
1518 }
1519 }
1520 else
1521 {
1522 /*
1523 * SSM unit wasn't found - ignore this when loading for the debugger.
1524 */
1525 LogRel(("SSM: Found no handler for unit '%s'!\n", pszName));
1526 rc = VERR_SSM_INTEGRITY_UNIT_NOT_FOUND;
1527 if (Handle.enmAfter != SSMAFTER_DEBUG_IT)
1528 break;
1529 rc = RTFileSeek(Handle.File, offUnit + UnitHdr.cbUnit, RTFILE_SEEK_BEGIN, NULL);
1530 }
1531 }
1532 else
1533 {
1534 LogRel(("SSM: Unit name '%.*s' was not properly terminated.\n", UnitHdr.cchName, pszName));
1535 rc = VERR_SSM_INTEGRITY;
1536 break;
1537 }
1538 }
1539 }
1540 else
1541 rc = VERR_NO_TMP_MEMORY;
1542 }
1543
1544 /*
1545 * I/O errors ends up here (yea, I know, very nice programming).
1546 */
1547 if (VBOX_FAILURE(rc))
1548 {
1549 LogRel(("SSM: I/O error. rc=%Vrc\n", rc));
1550 break;
1551 }
1552 }
1553 }
1554 /* (progress should be pending 99% now) */
1555 AssertMsg(VBOX_FAILURE(rc) || Handle.uPercent == (101-Handle.uPercentDone), ("%d\n", Handle.uPercent));
1556
1557 /*
1558 * Do the done run.
1559 */
1560 Handle.rc = rc;
1561 Handle.enmOp = SSMSTATE_LOAD_DONE;
1562 for (pUnit = pVM->ssm.s.pHead; pUnit; pUnit = pUnit->pNext)
1563 {
1564 rc = VINF_SUCCESS;
1565 switch (pUnit->enmType)
1566 {
1567 case SSMUNITTYPE_DEV:
1568 if ( pUnit->u.Dev.pfnLoadDone
1569 && ( pUnit->fCalled
1570 || (!pUnit->u.Dev.pfnLoadPrep && !pUnit->u.Dev.pfnLoadExec)))
1571 rc = pUnit->u.Dev.pfnLoadDone(pUnit->u.Dev.pDevIns, &Handle);
1572 break;
1573 case SSMUNITTYPE_DRV:
1574 if ( pUnit->u.Drv.pfnLoadDone
1575 && ( pUnit->fCalled
1576 || (!pUnit->u.Drv.pfnLoadPrep && !pUnit->u.Drv.pfnLoadExec)))
1577 rc = pUnit->u.Drv.pfnLoadDone(pUnit->u.Drv.pDrvIns, &Handle);
1578 break;
1579 case SSMUNITTYPE_INTERNAL:
1580 if (pUnit->u.Internal.pfnLoadDone
1581 && ( pUnit->fCalled
1582 || (!pUnit->u.Internal.pfnLoadPrep && !pUnit->u.Internal.pfnLoadExec)))
1583 rc = pUnit->u.Internal.pfnLoadDone(pVM, &Handle);
1584 break;
1585 case SSMUNITTYPE_EXTERNAL:
1586 if (pUnit->u.External.pfnLoadDone
1587 && ( pUnit->fCalled
1588 || (!pUnit->u.Internal.pfnLoadPrep && !pUnit->u.Internal.pfnLoadExec)))
1589 rc = pUnit->u.External.pfnLoadDone(&Handle, pUnit->u.External.pvUser);
1590 break;
1591 }
1592 if (VBOX_FAILURE(rc))
1593 {
1594 LogRel(("SSM: Done load failed with rc=%Vrc for data unit '%s'.\n", rc, pUnit->szName));
1595 if (VBOX_SUCCESS(Handle.rc))
1596 Handle.rc = rc;
1597 }
1598 }
1599 rc = Handle.rc;
1600
1601 /* progress */
1602 if (pfnProgress)
1603 pfnProgress(pVM, 99, pvUser);
1604 }
1605
1606 /*
1607 * Done
1608 */
1609 int rc2 = RTFileClose(Handle.File);
1610 AssertRC(rc2);
1611 if (VBOX_SUCCESS(rc))
1612 {
1613 /* progress */
1614 if (pfnProgress)
1615 pfnProgress(pVM, 100, pvUser);
1616 Log(("SSM: Load of '%s' completed!\n", pszFilename));
1617 Log(("\n\n\n"));
1618 DBGFR3InfoLog(pVM, "cpum", "verbose");
1619 DBGFR3InfoLog(pVM, "timers", NULL);
1620 DBGFR3InfoLog(pVM, "activetimers", NULL);
1621 DBGFR3InfoLog(pVM, "ioport", NULL);
1622 DBGFR3InfoLog(pVM, "mmio", NULL);
1623 DBGFR3InfoLog(pVM, "phys", NULL);
1624 Log(("\n\n\n"));
1625 }
1626 return rc;
1627}
1628
1629
1630/**
1631 * Validates a file as a validate SSM saved state.
1632 *
1633 * This will only verify the file format, the format and content of individual
1634 * data units are not inspected.
1635 *
1636 * @returns VINF_SUCCESS if valid.
1637 * @returns VBox status code on other failures.
1638 * @param pszFilename The path to the file to validate.
1639 */
1640SSMR3DECL(int) SSMR3ValidateFile(const char *pszFilename)
1641{
1642 LogFlow(("SSMR3ValidateFile: pszFilename=%p:{%s}\n", pszFilename, pszFilename));
1643
1644 /*
1645 * Try open the file and validate it.
1646 */
1647 RTFILE File;
1648 int rc = RTFileOpen(&File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1649 if (VBOX_SUCCESS(rc))
1650 {
1651 size_t cbFileHdr;
1652 SSMFILEHDR Hdr;
1653 rc = ssmr3Validate(File, &Hdr, &cbFileHdr);
1654 RTFileClose(File);
1655 }
1656 else
1657 Log(("SSM: Failed to open saved state file '%s', rc=%Vrc.\n", pszFilename, rc));
1658 return rc;
1659}
1660
1661
1662/**
1663 * Opens a saved state file for reading.
1664 *
1665 * @returns VBox status code.
1666 * @param pszFilename The path to the saved state file.
1667 * @param fFlags Open flags. Reserved, must be 0.
1668 * @param ppSSM Where to store the SSM handle.
1669 */
1670SSMR3DECL(int) SSMR3Open(const char *pszFilename, unsigned fFlags, PSSMHANDLE *ppSSM)
1671{
1672 LogFlow(("SSMR3Open: pszFilename=%p:{%s} fFlags=%#x ppSSM=%p\n", pszFilename, pszFilename, fFlags, ppSSM));
1673
1674 /*
1675 * Validate input.
1676 */
1677 AssertMsgReturn(VALID_PTR(pszFilename), ("%p\n", pszFilename), VERR_INVALID_PARAMETER);
1678 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
1679 AssertMsgReturn(VALID_PTR(ppSSM), ("%p\n", ppSSM), VERR_INVALID_PARAMETER);
1680
1681 /*
1682 * Allocate a handle.
1683 */
1684 PSSMHANDLE pSSM = (PSSMHANDLE)RTMemAllocZ(sizeof(*pSSM));
1685 AssertReturn(pSSM, VERR_NO_MEMORY);
1686
1687 /*
1688 * Try open the file and validate it.
1689 */
1690 int rc = RTFileOpen(&pSSM->File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
1691 if (VBOX_SUCCESS(rc))
1692 {
1693 SSMFILEHDR Hdr;
1694 size_t cbFileHdr;
1695 rc = ssmr3Validate(pSSM->File, &Hdr, &cbFileHdr);
1696 if (VBOX_SUCCESS(rc))
1697 {
1698 //pSSM->pVM = NULL;
1699 pSSM->cbFileHdr = cbFileHdr;
1700 pSSM->enmOp = SSMSTATE_OPEN_READ;
1701 pSSM->enmAfter = SSMAFTER_OPENED;
1702 pSSM->uPercentPrepare = 20; /* reserve substantial time for validating the image */
1703 pSSM->uPercentDone = 2;
1704 //pSSM->rc = VINF_SUCCESS;
1705 //pSSM->pZipComp = NULL;
1706 //pSSM->pZipDecomp = NULL;
1707 //pSSM->cbUnitLeft = 0;
1708 //pSSM->pfnProgress = NULL;
1709 //pSSM->pvUser = NULL;
1710 //pSSM->uPercent = 0;
1711 //pSSM->offEstProgress= 0;
1712 //pSSM->cbEstTotal = 0;
1713 //pSSM->offEst = 0;
1714 //pSSM->offEstUnitEnd = 0;
1715 *ppSSM = pSSM;
1716 LogFlow(("SSMR3Open: returns VINF_SUCCESS *ppSSM=%p\n", *ppSSM));
1717 return VINF_SUCCESS;
1718 }
1719 Log(("SSMR3Open: Validation of '%s' failed, rc=%Vrc.\n", pszFilename, rc));
1720 RTFileClose(pSSM->File);
1721 }
1722 else
1723 Log(("SSMR3Open: Failed to open saved state file '%s', rc=%Vrc.\n", pszFilename, rc));
1724 RTMemFree(pSSM);
1725 return rc;
1726
1727}
1728
1729
1730/**
1731 * Closes a saved state file opened by SSMR3Open().
1732 *
1733 * @returns VBox status code.
1734 * @param pSSM The SSM handle returned by SSMR3Open().
1735 */
1736SSMR3DECL(int) SSMR3Close(PSSMHANDLE pSSM)
1737{
1738 LogFlow(("SSMR3Close: pSSM=%p\n", pSSM));
1739
1740 /*
1741 * Validate input.
1742 */
1743 AssertMsgReturn(VALID_PTR(pSSM), ("%p\n", pSSM), VERR_INVALID_PARAMETER);
1744 AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
1745 AssertMsgReturn(pSSM->enmOp == SSMSTATE_OPEN_READ, ("%d\n", pSSM->enmOp), VERR_INVALID_PARAMETER);
1746
1747 /*
1748 * Close the file and free the handle.
1749 */
1750 int rc = RTFileClose(pSSM->File);
1751 AssertRC(rc);
1752 RTMemFree(pSSM);
1753 return rc;
1754}
1755
1756
1757/**
1758 * Seeks to a specific data unit.
1759 *
1760 * After seeking it's possible to use the getters to on
1761 * that data unit.
1762 *
1763 * @returns VBox status code.
1764 * @returns VERR_SSM_UNIT_NOT_FOUND if the unit+instance wasn't found.
1765 * @param pSSM The SSM handle returned by SSMR3Open().
1766 * @param pszUnit The name of the data unit.
1767 * @param iInstance The instance number.
1768 * @param piVersion Where to store the version number. (Optional)
1769 */
1770SSMR3DECL(int) SSMR3Seek(PSSMHANDLE pSSM, const char *pszUnit, uint32_t iInstance, uint32_t *piVersion)
1771{
1772 LogFlow(("SSMR3Seek: pSSM=%p pszUnit=%p:{%s} iInstance=%RU32 piVersion=%p\n",
1773 pSSM, pszUnit, pszUnit, iInstance, piVersion));
1774
1775 /*
1776 * Validate input.
1777 */
1778 AssertMsgReturn(VALID_PTR(pSSM), ("%p\n", pSSM), VERR_INVALID_PARAMETER);
1779 AssertMsgReturn(pSSM->enmAfter == SSMAFTER_OPENED, ("%d\n", pSSM->enmAfter),VERR_INVALID_PARAMETER);
1780 AssertMsgReturn(pSSM->enmOp == SSMSTATE_OPEN_READ, ("%d\n", pSSM->enmOp), VERR_INVALID_PARAMETER);
1781 AssertMsgReturn(VALID_PTR(pszUnit), ("%p\n", pszUnit), VERR_INVALID_POINTER);
1782 AssertMsgReturn(!piVersion || VALID_PTR(piVersion), ("%p\n", piVersion), VERR_INVALID_POINTER);
1783
1784 /*
1785 * Reset the state.
1786 */
1787 if (pSSM->pZipDecomp)
1788 {
1789 RTZipDecompDestroy(pSSM->pZipDecomp);
1790 pSSM->pZipDecomp = NULL;
1791 }
1792 pSSM->rc = VERR_SSM_UNIT_NOT_FOUND;
1793 pSSM->cbUnitLeft = 0;
1794
1795 /*
1796 * Walk the data units until we find EOF or a match.
1797 */
1798 size_t cchUnit = strlen(pszUnit) + 1;
1799 int rc = VINF_SUCCESS;
1800 char *pszName = NULL;
1801 size_t cchName = 0;
1802 SSMFILEUNITHDR UnitHdr;
1803 for (RTFOFF off = pSSM->cbFileHdr; ; off += UnitHdr.cbUnit)
1804 {
1805 /*
1806 * Read the unit header and verify it.
1807 */
1808 rc = RTFileReadAt(pSSM->File, off, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName), NULL);
1809 AssertRC(rc);
1810 if (VBOX_SUCCESS(rc))
1811 {
1812 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(SSMFILEUNITHDR_MAGIC)))
1813 {
1814 /*
1815 * Does it match thus far or should we just skip along?
1816 */
1817 if ( UnitHdr.u32Instance != iInstance
1818 && UnitHdr.cchName != cchUnit)
1819 continue;
1820
1821 /*
1822 * Read the name.
1823 * Adjust the name buffer first.
1824 */
1825 if (cchName < UnitHdr.cchName)
1826 {
1827 if (pszName)
1828 RTMemTmpFree(pszName);
1829 cchName = RT_ALIGN_Z(UnitHdr.cchName, 64);
1830 pszName = (char *)RTMemTmpAlloc(cchName);
1831 }
1832 rc = VERR_NO_MEMORY;
1833 if (pszName)
1834 {
1835 rc = RTFileRead(pSSM->File, pszName, UnitHdr.cchName, NULL);
1836 AssertRC(rc);
1837 if (VBOX_SUCCESS(rc))
1838 {
1839 if (!pszName[UnitHdr.cchName - 1])
1840 {
1841 /*
1842 * Does the name match? If not continue with the next item.
1843 */
1844 if (memcmp(pszName, pszUnit, cchUnit))
1845 continue;
1846
1847 pSSM->rc = rc = VINF_SUCCESS;
1848 pSSM->cbUnitLeft = UnitHdr.cbUnit - RT_OFFSETOF(SSMFILEUNITHDR, szName[UnitHdr.cchName]);
1849 if (piVersion)
1850 *piVersion = UnitHdr.u32Version;
1851 }
1852 else
1853 {
1854 AssertMsgFailed((" Unit name '%.*s' was not properly terminated.\n", UnitHdr.cchName, pszName));
1855 rc = VERR_SSM_INTEGRITY;
1856 }
1857 }
1858 }
1859 }
1860 else
1861 {
1862 if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_END, sizeof(SSMFILEUNITHDR_END)))
1863 rc = VERR_SSM_UNIT_NOT_FOUND;
1864 else
1865 {
1866 AssertMsgFailed(("Invalid unit magic at offset %RTfoff, '%.*s'!\n",
1867 off, sizeof(UnitHdr.achMagic) - 1, &UnitHdr.achMagic[0]));
1868 rc = VERR_SSM_INTEGRITY_UNIT_MAGIC;
1869 }
1870 }
1871 }
1872
1873 /* error or success, two continue statements cover the iterating */
1874 break;
1875 }
1876
1877 RTMemFree(pszName);
1878 return rc;
1879}
1880
1881
1882/**
1883 * Finishes a data unit.
1884 * All buffers and compressor instances are flushed and destroyed.
1885 *
1886 * @returns VBox status.
1887 * @param pSSM SSM operation handle.
1888 */
1889static int ssmr3WriteFinish(PSSMHANDLE pSSM)
1890{
1891 //Log2(("ssmr3WriteFinish: %#010llx start\n", RTFileTell(pSSM->File)));
1892 if (!pSSM->pZipComp)
1893 return VINF_SUCCESS;
1894
1895 int rc = RTZipCompFinish(pSSM->pZipComp);
1896 if (VBOX_SUCCESS(rc))
1897 {
1898 rc = RTZipCompDestroy(pSSM->pZipComp);
1899 if (VBOX_SUCCESS(rc))
1900 {
1901 pSSM->pZipComp = NULL;
1902 //Log2(("ssmr3WriteFinish: %#010llx done\n", RTFileTell(pSSM->File)));
1903 return VINF_SUCCESS;
1904 }
1905 }
1906 if (VBOX_SUCCESS(pSSM->rc))
1907 pSSM->rc = rc;
1908 Log2(("ssmr3WriteFinish: failure rc=%Vrc\n", rc));
1909 return rc;
1910}
1911
1912/**
1913 * Writes something to the current data item in the saved state file.
1914 *
1915 * @returns VBox status.
1916 * @param pSSM SSM operation handle.
1917 * @param pvBuf The bits to write.
1918 * @param cbBuf The number of bytes to write.
1919 */
1920static int ssmr3Write(PSSMHANDLE pSSM, const void *pvBuf, size_t cbBuf)
1921{
1922 Log2(("ssmr3Write: pvBuf=%p cbBuf=%#x %.*Vhxs%s\n", pvBuf, cbBuf, RT_MIN(cbBuf, 128), pvBuf, cbBuf > 128 ? "..." : ""));
1923
1924 /*
1925 * Check that everything is fine.
1926 */
1927 if (VBOX_SUCCESS(pSSM->rc))
1928 {
1929 /*
1930 * First call starts the compression.
1931 */
1932 if (!pSSM->pZipComp)
1933 {
1934 //int rc = RTZipCompCreate(&pSSM->pZipComp, pSSM, ssmr3WriteOut, RTZIPTYPE_ZLIB, RTZIPLEVEL_FAST);
1935 int rc = RTZipCompCreate(&pSSM->pZipComp, pSSM, ssmr3WriteOut, RTZIPTYPE_LZF, RTZIPLEVEL_FAST);
1936 if (VBOX_FAILURE(rc))
1937 return rc;
1938 }
1939
1940 /*
1941 * Write the data item in 128kb chunks for progress indicator reasons.
1942 */
1943 while (cbBuf > 0)
1944 {
1945 size_t cbChunk = RT_MIN(cbBuf, 128*1024);
1946 pSSM->rc = RTZipCompress(pSSM->pZipComp, pvBuf, cbChunk);
1947 if (VBOX_FAILURE(pSSM->rc))
1948 break;
1949 ssmR3Progress(pSSM, cbChunk);
1950 cbBuf -= cbChunk;
1951 pvBuf = (char *)pvBuf + cbChunk;
1952 }
1953 }
1954
1955 return pSSM->rc;
1956}
1957
1958
1959/**
1960 * Callback for flusing the output buffer of a compression stream.
1961 *
1962 * @returns VBox status.
1963 * @param pvSSM SSM operation handle.
1964 * @param pvBuf Compressed data.
1965 * @param cbBuf Size of the compressed data.
1966 */
1967static DECLCALLBACK(int) ssmr3WriteOut(void *pvSSM, const void *pvBuf, size_t cbBuf)
1968{
1969 //Log2(("ssmr3WriteOut: %#010llx cbBuf=%#x\n", RTFileTell(((PSSMHANDLE)pvSSM)->File), cbBuf));
1970 int rc = RTFileWrite(((PSSMHANDLE)pvSSM)->File, pvBuf, cbBuf, NULL);
1971 if (VBOX_SUCCESS(rc))
1972 return rc;
1973 Log(("ssmr3WriteOut: RTFileWrite(,,%d) -> %d\n", cbBuf, rc));
1974 return rc;
1975}
1976
1977
1978/**
1979 * Puts a structure.
1980 *
1981 * @returns VBox status code.
1982 * @param pSSM The saved state handle.
1983 * @param pvStruct The structure address.
1984 * @param paFields The array of structure fields descriptions.
1985 * The array must be terminated by a SSMFIELD_ENTRY_TERM().
1986 */
1987SSMR3DECL(int) SSMR3PutStruct(PSSMHANDLE pSSM, const void *pvStruct, PCSSMFIELD paFields)
1988{
1989 /* begin marker. */
1990 int rc = SSMR3PutU32(pSSM, SSMR3STRUCT_BEGIN);
1991 if (VBOX_FAILURE(rc))
1992 return rc;
1993
1994 /* put the fields */
1995 for (PCSSMFIELD pCur = paFields;
1996 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
1997 pCur++)
1998 {
1999 rc = ssmr3Write(pSSM, (uint8_t *)pvStruct + pCur->off, pCur->cb);
2000 if (VBOX_FAILURE(rc))
2001 return rc;
2002 }
2003
2004 /* end marker */
2005 return SSMR3PutU32(pSSM, SSMR3STRUCT_END);
2006}
2007
2008
2009/**
2010 * Saves a boolean item to the current data unit.
2011 *
2012 * @returns VBox status.
2013 * @param pSSM SSM operation handle.
2014 * @param fBool Item to save.
2015 */
2016SSMR3DECL(int) SSMR3PutBool(PSSMHANDLE pSSM, bool fBool)
2017{
2018 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2019 {
2020 uint8_t u8 = fBool; /* enforce 1 byte size */
2021 return ssmr3Write(pSSM, &u8, sizeof(u8));
2022 }
2023 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2024 return VERR_SSM_INVALID_STATE;
2025}
2026
2027/**
2028 * Saves a 8-bit unsigned integer item to the current data unit.
2029 *
2030 * @returns VBox status.
2031 * @param pSSM SSM operation handle.
2032 * @param u8 Item to save.
2033 */
2034SSMR3DECL(int) SSMR3PutU8(PSSMHANDLE pSSM, uint8_t u8)
2035{
2036 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2037 return ssmr3Write(pSSM, &u8, sizeof(u8));
2038 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2039 return VERR_SSM_INVALID_STATE;
2040}
2041
2042/**
2043 * Saves a 8-bit signed integer item to the current data unit.
2044 *
2045 * @returns VBox status.
2046 * @param pSSM SSM operation handle.
2047 * @param i8 Item to save.
2048 */
2049SSMR3DECL(int) SSMR3PutS8(PSSMHANDLE pSSM, int8_t i8)
2050{
2051 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2052 return ssmr3Write(pSSM, &i8, sizeof(i8));
2053 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2054 return VERR_SSM_INVALID_STATE;
2055}
2056
2057/**
2058 * Saves a 16-bit unsigned integer item to the current data unit.
2059 *
2060 * @returns VBox status.
2061 * @param pSSM SSM operation handle.
2062 * @param u16 Item to save.
2063 */
2064SSMR3DECL(int) SSMR3PutU16(PSSMHANDLE pSSM, uint16_t u16)
2065{
2066 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2067 return ssmr3Write(pSSM, &u16, sizeof(u16));
2068 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2069 return VERR_SSM_INVALID_STATE;
2070}
2071
2072/**
2073 * Saves a 16-bit signed integer item to the current data unit.
2074 *
2075 * @returns VBox status.
2076 * @param pSSM SSM operation handle.
2077 * @param i16 Item to save.
2078 */
2079SSMR3DECL(int) SSMR3PutS16(PSSMHANDLE pSSM, int16_t i16)
2080{
2081 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2082 return ssmr3Write(pSSM, &i16, sizeof(i16));
2083 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2084 return VERR_SSM_INVALID_STATE;
2085}
2086
2087/**
2088 * Saves a 32-bit unsigned integer item to the current data unit.
2089 *
2090 * @returns VBox status.
2091 * @param pSSM SSM operation handle.
2092 * @param u32 Item to save.
2093 */
2094SSMR3DECL(int) SSMR3PutU32(PSSMHANDLE pSSM, uint32_t u32)
2095{
2096 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2097 return ssmr3Write(pSSM, &u32, sizeof(u32));
2098 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2099 return VERR_SSM_INVALID_STATE;
2100}
2101
2102/**
2103 * Saves a 32-bit signed integer item to the current data unit.
2104 *
2105 * @returns VBox status.
2106 * @param pSSM SSM operation handle.
2107 * @param i32 Item to save.
2108 */
2109SSMR3DECL(int) SSMR3PutS32(PSSMHANDLE pSSM, int32_t i32)
2110{
2111 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2112 return ssmr3Write(pSSM, &i32, sizeof(i32));
2113 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2114 return VERR_SSM_INVALID_STATE;
2115}
2116
2117/**
2118 * Saves a 64-bit unsigned integer item to the current data unit.
2119 *
2120 * @returns VBox status.
2121 * @param pSSM SSM operation handle.
2122 * @param u64 Item to save.
2123 */
2124SSMR3DECL(int) SSMR3PutU64(PSSMHANDLE pSSM, uint64_t u64)
2125{
2126 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2127 return ssmr3Write(pSSM, &u64, sizeof(u64));
2128 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2129 return VERR_SSM_INVALID_STATE;
2130}
2131
2132/**
2133 * Saves a 64-bit signed integer item to the current data unit.
2134 *
2135 * @returns VBox status.
2136 * @param pSSM SSM operation handle.
2137 * @param i64 Item to save.
2138 */
2139SSMR3DECL(int) SSMR3PutS64(PSSMHANDLE pSSM, int64_t i64)
2140{
2141 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2142 return ssmr3Write(pSSM, &i64, sizeof(i64));
2143 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2144 return VERR_SSM_INVALID_STATE;
2145}
2146
2147/**
2148 * Saves a 128-bit unsigned integer item to the current data unit.
2149 *
2150 * @returns VBox status.
2151 * @param pSSM SSM operation handle.
2152 * @param u128 Item to save.
2153 */
2154SSMR3DECL(int) SSMR3PutU128(PSSMHANDLE pSSM, uint128_t u128)
2155{
2156 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2157 return ssmr3Write(pSSM, &u128, sizeof(u128));
2158 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2159 return VERR_SSM_INVALID_STATE;
2160}
2161
2162/**
2163 * Saves a 128-bit signed integer item to the current data unit.
2164 *
2165 * @returns VBox status.
2166 * @param pSSM SSM operation handle.
2167 * @param i128 Item to save.
2168 */
2169SSMR3DECL(int) SSMR3PutS128(PSSMHANDLE pSSM, int128_t i128)
2170{
2171 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2172 return ssmr3Write(pSSM, &i128, sizeof(i128));
2173 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2174 return VERR_SSM_INVALID_STATE;
2175}
2176
2177/**
2178 * Saves a VBox unsigned integer item to the current data unit.
2179 *
2180 * @returns VBox status.
2181 * @param pSSM SSM operation handle.
2182 * @param u Item to save.
2183 */
2184SSMR3DECL(int) SSMR3PutUInt(PSSMHANDLE pSSM, RTUINT u)
2185{
2186 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2187 return ssmr3Write(pSSM, &u, sizeof(u));
2188 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2189 return VERR_SSM_INVALID_STATE;
2190}
2191
2192
2193/**
2194 * Saves a VBox signed integer item to the current data unit.
2195 *
2196 * @returns VBox status.
2197 * @param pSSM SSM operation handle.
2198 * @param i Item to save.
2199 */
2200SSMR3DECL(int) SSMR3PutSInt(PSSMHANDLE pSSM, RTINT i)
2201{
2202 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2203 return ssmr3Write(pSSM, &i, sizeof(i));
2204 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2205 return VERR_SSM_INVALID_STATE;
2206}
2207
2208
2209/**
2210 * Saves a GC natural unsigned integer item to the current data unit.
2211 *
2212 * @returns VBox status.
2213 * @param pSSM SSM operation handle.
2214 * @param u Item to save.
2215 */
2216SSMR3DECL(int) SSMR3PutGCUInt(PSSMHANDLE pSSM, RTGCUINT u)
2217{
2218 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2219 return ssmr3Write(pSSM, &u, sizeof(u));
2220 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2221 return VERR_SSM_INVALID_STATE;
2222}
2223
2224
2225/**
2226 * Saves a GC natural signed integer item to the current data unit.
2227 *
2228 * @returns VBox status.
2229 * @param pSSM SSM operation handle.
2230 * @param i Item to save.
2231 */
2232SSMR3DECL(int) SSMR3PutGCSInt(PSSMHANDLE pSSM, RTGCINT i)
2233{
2234 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2235 return ssmr3Write(pSSM, &i, sizeof(i));
2236 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2237 return VERR_SSM_INVALID_STATE;
2238}
2239
2240
2241/**
2242 * Saves a 32 bits GC physical address item to the current data unit.
2243 *
2244 * @returns VBox status.
2245 * @param pSSM SSM operation handle.
2246 * @param GCPhys The item to save
2247 */
2248SSMR3DECL(int) SSMR3PutGCPhys32(PSSMHANDLE pSSM, RTGCPHYS32 GCPhys)
2249{
2250 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2251 return ssmr3Write(pSSM, &GCPhys, sizeof(GCPhys));
2252 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2253 return VERR_SSM_INVALID_STATE;
2254}
2255
2256
2257/**
2258 * Saves a 64 bits GC physical address item to the current data unit.
2259 *
2260 * @returns VBox status.
2261 * @param pSSM SSM operation handle.
2262 * @param GCPhys The item to save
2263 */
2264SSMR3DECL(int) SSMR3PutGCPhys64(PSSMHANDLE pSSM, RTGCPHYS64 GCPhys)
2265{
2266 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2267 return ssmr3Write(pSSM, &GCPhys, sizeof(GCPhys));
2268 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2269 return VERR_SSM_INVALID_STATE;
2270}
2271
2272
2273/**
2274 * Saves a GC physical address item to the current data unit.
2275 *
2276 * @returns VBox status.
2277 * @param pSSM SSM operation handle.
2278 * @param GCPhys The item to save
2279 */
2280SSMR3DECL(int) SSMR3PutGCPhys(PSSMHANDLE pSSM, RTGCPHYS GCPhys)
2281{
2282 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2283 return ssmr3Write(pSSM, &GCPhys, sizeof(GCPhys));
2284 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2285 return VERR_SSM_INVALID_STATE;
2286}
2287
2288
2289/**
2290 * Saves a GC virtual address item to the current data unit.
2291 *
2292 * @returns VBox status.
2293 * @param pSSM SSM operation handle.
2294 * @param GCPtr The item to save.
2295 */
2296SSMR3DECL(int) SSMR3PutGCPtr(PSSMHANDLE pSSM, RTGCPTR GCPtr)
2297{
2298 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2299 return ssmr3Write(pSSM, &GCPtr, sizeof(GCPtr));
2300 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2301 return VERR_SSM_INVALID_STATE;
2302}
2303
2304
2305/**
2306 * Saves a GC virtual address (represented as an unsigned integer) item to the current data unit.
2307 *
2308 * @returns VBox status.
2309 * @param pSSM SSM operation handle.
2310 * @param GCPtr The item to save.
2311 */
2312SSMR3DECL(int) SSMR3PutGCUIntPtr(PSSMHANDLE pSSM, RTGCUINTPTR GCPtr)
2313{
2314 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2315 return ssmr3Write(pSSM, &GCPtr, sizeof(GCPtr));
2316 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2317 return VERR_SSM_INVALID_STATE;
2318}
2319
2320
2321/**
2322 * Saves a HC natural unsigned integer item to the current data unit.
2323 *
2324 * @returns VBox status.
2325 * @param pSSM SSM operation handle.
2326 * @param u Item to save.
2327 */
2328SSMR3DECL(int) SSMR3PutHCUInt(PSSMHANDLE pSSM, RTHCUINT u)
2329{
2330 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2331 return ssmr3Write(pSSM, &u, sizeof(u));
2332 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2333 return VERR_SSM_INVALID_STATE;
2334}
2335
2336
2337/**
2338 * Saves a HC natural signed integer item to the current data unit.
2339 *
2340 * @returns VBox status.
2341 * @param pSSM SSM operation handle.
2342 * @param i Item to save.
2343 */
2344SSMR3DECL(int) SSMR3PutHCSInt(PSSMHANDLE pSSM, RTHCINT i)
2345{
2346 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2347 return ssmr3Write(pSSM, &i, sizeof(i));
2348 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2349 return VERR_SSM_INVALID_STATE;
2350}
2351
2352
2353/**
2354 * Saves a I/O port address item to the current data unit.
2355 *
2356 * @returns VBox status.
2357 * @param pSSM SSM operation handle.
2358 * @param IOPort The item to save.
2359 */
2360SSMR3DECL(int) SSMR3PutIOPort(PSSMHANDLE pSSM, RTIOPORT IOPort)
2361{
2362 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2363 return ssmr3Write(pSSM, &IOPort, sizeof(IOPort));
2364 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2365 return VERR_SSM_INVALID_STATE;
2366}
2367
2368
2369/**
2370 * Saves a selector item to the current data unit.
2371 *
2372 * @returns VBox status.
2373 * @param pSSM SSM operation handle.
2374 * @param Sel The item to save.
2375 */
2376SSMR3DECL(int) SSMR3PutSel(PSSMHANDLE pSSM, RTSEL Sel)
2377{
2378 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2379 return ssmr3Write(pSSM, &Sel, sizeof(Sel));
2380 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2381 return VERR_SSM_INVALID_STATE;
2382}
2383
2384
2385/**
2386 * Saves a memory item to the current data unit.
2387 *
2388 * @returns VBox status.
2389 * @param pSSM SSM operation handle.
2390 * @param pv Item to save.
2391 * @param cb Size of the item.
2392 */
2393SSMR3DECL(int) SSMR3PutMem(PSSMHANDLE pSSM, const void *pv, size_t cb)
2394{
2395 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2396 return ssmr3Write(pSSM, pv, cb);
2397 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2398 return VERR_SSM_INVALID_STATE;
2399}
2400
2401/**
2402 * Saves a zero terminated string item to the current data unit.
2403 *
2404 * @returns VBox status.
2405 * @param pSSM SSM operation handle.
2406 * @param psz Item to save.
2407 */
2408SSMR3DECL(int) SSMR3PutStrZ(PSSMHANDLE pSSM, const char *psz)
2409{
2410 if (pSSM->enmOp == SSMSTATE_SAVE_EXEC)
2411 {
2412 size_t cch = strlen(psz);
2413 if (cch > 1024*1024)
2414 {
2415 AssertMsgFailed(("a %d byte long string, what's this!?!\n"));
2416 return VERR_TOO_MUCH_DATA;
2417 }
2418 uint32_t u32 = (uint32_t)cch;
2419 int rc = ssmr3Write(pSSM, &u32, sizeof(u32));
2420 if (rc)
2421 return rc;
2422 return ssmr3Write(pSSM, psz, cch);
2423 }
2424 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2425 return VERR_SSM_INVALID_STATE;
2426}
2427
2428
2429
2430
2431
2432/**
2433 * Closes the decompressor of a data unit.
2434 *
2435 * @param pSSM SSM operation handle.
2436 */
2437static void ssmr3ReadFinish(PSSMHANDLE pSSM)
2438{
2439 int rc = RTZipDecompDestroy(pSSM->pZipDecomp);
2440 AssertRC(rc);
2441 pSSM->pZipDecomp = NULL;
2442}
2443
2444/**
2445 * Internal read worker.
2446 *
2447 * @param pSSM SSM operation handle.
2448 * @param pvBuf Where to store the read data.
2449 * @param cbBuf Number of bytes to read.
2450 */
2451static int ssmr3Read(PSSMHANDLE pSSM, void *pvBuf, size_t cbBuf)
2452{
2453 /*
2454 * Check that everything is fine.
2455 */
2456 if (VBOX_SUCCESS(pSSM->rc))
2457 {
2458 /*
2459 * Open the decompressor on the first read.
2460 */
2461 if (!pSSM->pZipDecomp)
2462 {
2463 pSSM->rc = RTZipDecompCreate(&pSSM->pZipDecomp, pSSM, ssmr3ReadIn);
2464 if (VBOX_FAILURE(pSSM->rc))
2465 return pSSM->rc;
2466 }
2467
2468 /*
2469 * Do the requested read.
2470 * Use 32kb chunks to work the progress indicator.
2471 */
2472 pSSM->rc = RTZipDecompress(pSSM->pZipDecomp, pvBuf, cbBuf, NULL);
2473 if (VBOX_SUCCESS(pSSM->rc))
2474 Log2(("ssmr3Read: pvBuf=%p cbBuf=%#x %.*Vhxs%s\n", pvBuf, cbBuf, RT_MIN(cbBuf, 128), pvBuf, cbBuf > 128 ? "..." : ""));
2475 else
2476 AssertMsgFailed(("rc=%Vrc cbBuf=%#x\n", pSSM->rc, cbBuf));
2477 }
2478
2479 return pSSM->rc;
2480}
2481
2482/**
2483 * Callback for reading compressed data into the input buffer of the
2484 * decompressor.
2485 *
2486 * @returns VBox status code.
2487 * @param pvSSM The SSM handle.
2488 * @param pvBuf Where to store the compressed data.
2489 * @param cbBuf Size of the buffer.
2490 * @param pcbRead Number of bytes actually stored in the buffer.
2491 */
2492static DECLCALLBACK(int) ssmr3ReadIn(void *pvSSM, void *pvBuf, size_t cbBuf, size_t *pcbRead)
2493{
2494 PSSMHANDLE pSSM = (PSSMHANDLE)pvSSM;
2495 size_t cbRead = cbBuf;
2496 if (pSSM->cbUnitLeft < cbBuf)
2497 cbRead = (size_t)pSSM->cbUnitLeft;
2498 if (cbRead)
2499 {
2500 //Log2(("ssmr3ReadIn: %#010llx cbBug=%#x cbRead=%#x\n", RTFileTell(pSSM->File), cbBuf, cbRead));
2501 int rc = RTFileRead(pSSM->File, pvBuf, cbRead, NULL);
2502 if (VBOX_SUCCESS(rc))
2503 {
2504 pSSM->cbUnitLeft -= cbRead;
2505 if (pcbRead)
2506 *pcbRead = cbRead;
2507 ssmR3Progress(pSSM, cbRead);
2508 return VINF_SUCCESS;
2509 }
2510 Log(("ssmr3ReadIn: RTFileRead(,,%d) -> %d\n", cbRead, rc));
2511 return rc;
2512 }
2513
2514 /** @todo weed out lazy saving */
2515 if (pSSM->enmAfter != SSMAFTER_DEBUG_IT)
2516 AssertMsgFailed(("SSM: attempted reading more than the unit!\n"));
2517 return VERR_SSM_LOADED_TOO_MUCH;
2518}
2519
2520
2521/**
2522 * Gets a structure.
2523 *
2524 * @returns VBox status code.
2525 * @param pSSM The saved state handle.
2526 * @param pvStruct The structure address.
2527 * @param paFields The array of structure fields descriptions.
2528 * The array must be terminated by a SSMFIELD_ENTRY_TERM().
2529 */
2530SSMR3DECL(int) SSMR3GetStruct(PSSMHANDLE pSSM, void *pvStruct, PCSSMFIELD paFields)
2531{
2532 /* begin marker. */
2533 uint32_t u32Magic;
2534 int rc = SSMR3GetU32(pSSM, &u32Magic);
2535 if (VBOX_FAILURE(rc))
2536 return rc;
2537 if (u32Magic != SSMR3STRUCT_BEGIN)
2538 AssertMsgFailedReturn(("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
2539
2540 /* put the fields */
2541 for (PCSSMFIELD pCur = paFields;
2542 pCur->cb != UINT32_MAX && pCur->off != UINT32_MAX;
2543 pCur++)
2544 {
2545 rc = ssmr3Read(pSSM, (uint8_t *)pvStruct + pCur->off, pCur->cb);
2546 if (VBOX_FAILURE(rc))
2547 return rc;
2548 }
2549
2550 /* end marker */
2551 rc = SSMR3GetU32(pSSM, &u32Magic);
2552 if (VBOX_FAILURE(rc))
2553 return rc;
2554 if (u32Magic != SSMR3STRUCT_END)
2555 AssertMsgFailedReturn(("u32Magic=%#RX32\n", u32Magic), VERR_SSM_STRUCTURE_MAGIC);
2556 return rc;
2557}
2558
2559
2560/**
2561 * Loads a boolean item from the current data unit.
2562 *
2563 * @returns VBox status.
2564 * @param pSSM SSM operation handle.
2565 * @param pfBool Where to store the item.
2566 */
2567SSMR3DECL(int) SSMR3GetBool(PSSMHANDLE pSSM, bool *pfBool)
2568{
2569 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2570 {
2571 uint8_t u8; /* see SSMR3PutBool */
2572 int rc = ssmr3Read(pSSM, &u8, sizeof(u8));
2573 if (VBOX_SUCCESS(rc))
2574 {
2575 Assert(u8 <= 1);
2576 *pfBool = !!u8;
2577 }
2578 return rc;
2579 }
2580 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2581 return VERR_SSM_INVALID_STATE;
2582}
2583
2584/**
2585 * Loads a 8-bit unsigned integer item from the current data unit.
2586 *
2587 * @returns VBox status.
2588 * @param pSSM SSM operation handle.
2589 * @param pu8 Where to store the item.
2590 */
2591SSMR3DECL(int) SSMR3GetU8(PSSMHANDLE pSSM, uint8_t *pu8)
2592{
2593 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2594 return ssmr3Read(pSSM, pu8, sizeof(*pu8));
2595 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2596 return VERR_SSM_INVALID_STATE;
2597}
2598
2599/**
2600 * Loads a 8-bit signed integer item from the current data unit.
2601 *
2602 * @returns VBox status.
2603 * @param pSSM SSM operation handle.
2604 * @param pi8 Where to store the item.
2605 */
2606SSMR3DECL(int) SSMR3GetS8(PSSMHANDLE pSSM, int8_t *pi8)
2607{
2608 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2609 return ssmr3Read(pSSM, pi8, sizeof(*pi8));
2610 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2611 return VERR_SSM_INVALID_STATE;
2612}
2613
2614/**
2615 * Loads a 16-bit unsigned integer item from the current data unit.
2616 *
2617 * @returns VBox status.
2618 * @param pSSM SSM operation handle.
2619 * @param pu16 Where to store the item.
2620 */
2621SSMR3DECL(int) SSMR3GetU16(PSSMHANDLE pSSM, uint16_t *pu16)
2622{
2623 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2624 return ssmr3Read(pSSM, pu16, sizeof(*pu16));
2625 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2626 return VERR_SSM_INVALID_STATE;
2627}
2628
2629/**
2630 * Loads a 16-bit signed integer item from the current data unit.
2631 *
2632 * @returns VBox status.
2633 * @param pSSM SSM operation handle.
2634 * @param pi16 Where to store the item.
2635 */
2636SSMR3DECL(int) SSMR3GetS16(PSSMHANDLE pSSM, int16_t *pi16)
2637{
2638 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2639 return ssmr3Read(pSSM, pi16, sizeof(*pi16));
2640 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2641 return VERR_SSM_INVALID_STATE;
2642}
2643
2644/**
2645 * Loads a 32-bit unsigned integer item from the current data unit.
2646 *
2647 * @returns VBox status.
2648 * @param pSSM SSM operation handle.
2649 * @param pu32 Where to store the item.
2650 */
2651SSMR3DECL(int) SSMR3GetU32(PSSMHANDLE pSSM, uint32_t *pu32)
2652{
2653 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2654 return ssmr3Read(pSSM, pu32, sizeof(*pu32));
2655 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2656 return VERR_SSM_INVALID_STATE;
2657}
2658
2659/**
2660 * Loads a 32-bit signed integer item from the current data unit.
2661 *
2662 * @returns VBox status.
2663 * @param pSSM SSM operation handle.
2664 * @param pi32 Where to store the item.
2665 */
2666SSMR3DECL(int) SSMR3GetS32(PSSMHANDLE pSSM, int32_t *pi32)
2667{
2668 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2669 return ssmr3Read(pSSM, pi32, sizeof(*pi32));
2670 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2671 return VERR_SSM_INVALID_STATE;
2672}
2673
2674/**
2675 * Loads a 64-bit unsigned integer item from the current data unit.
2676 *
2677 * @returns VBox status.
2678 * @param pSSM SSM operation handle.
2679 * @param pu64 Where to store the item.
2680 */
2681SSMR3DECL(int) SSMR3GetU64(PSSMHANDLE pSSM, uint64_t *pu64)
2682{
2683 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2684 return ssmr3Read(pSSM, pu64, sizeof(*pu64));
2685 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2686 return VERR_SSM_INVALID_STATE;
2687}
2688
2689/**
2690 * Loads a 64-bit signed integer item from the current data unit.
2691 *
2692 * @returns VBox status.
2693 * @param pSSM SSM operation handle.
2694 * @param pi64 Where to store the item.
2695 */
2696SSMR3DECL(int) SSMR3GetS64(PSSMHANDLE pSSM, int64_t *pi64)
2697{
2698 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2699 return ssmr3Read(pSSM, pi64, sizeof(*pi64));
2700 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2701 return VERR_SSM_INVALID_STATE;
2702}
2703
2704/**
2705 * Loads a 128-bit unsigned integer item from the current data unit.
2706 *
2707 * @returns VBox status.
2708 * @param pSSM SSM operation handle.
2709 * @param pu128 Where to store the item.
2710 */
2711SSMR3DECL(int) SSMR3GetU128(PSSMHANDLE pSSM, uint128_t *pu128)
2712{
2713 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2714 return ssmr3Read(pSSM, pu128, sizeof(*pu128));
2715 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2716 return VERR_SSM_INVALID_STATE;
2717}
2718
2719
2720/**
2721 * Loads a 128-bit signed integer item from the current data unit.
2722 *
2723 * @returns VBox status.
2724 * @param pSSM SSM operation handle.
2725 * @param pi128 Where to store the item.
2726 */
2727SSMR3DECL(int) SSMR3GetS128(PSSMHANDLE pSSM, int128_t *pi128)
2728{
2729 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2730 return ssmr3Read(pSSM, pi128, sizeof(*pi128));
2731 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2732 return VERR_SSM_INVALID_STATE;
2733}
2734
2735
2736/**
2737 * Loads a VBox unsigned integer item from the current data unit.
2738 *
2739 * @returns VBox status.
2740 * @param pSSM SSM operation handle.
2741 * @param pu Where to store the integer.
2742 */
2743SSMR3DECL(int) SSMR3GetUInt(PSSMHANDLE pSSM, PRTUINT pu)
2744{
2745 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2746 return ssmr3Read(pSSM, pu, sizeof(*pu));
2747 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2748 return VERR_SSM_INVALID_STATE;
2749}
2750
2751
2752/**
2753 * Loads a VBox signed integer item from the current data unit.
2754 *
2755 * @returns VBox status.
2756 * @param pSSM SSM operation handle.
2757 * @param pi Where to store the integer.
2758 */
2759SSMR3DECL(int) SSMR3GetSInt(PSSMHANDLE pSSM, PRTINT pi)
2760{
2761 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2762 return ssmr3Read(pSSM, pi, sizeof(*pi));
2763 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2764 return VERR_SSM_INVALID_STATE;
2765}
2766
2767
2768/**
2769 * Loads a GC natural unsigned integer item from the current data unit.
2770 *
2771 * @returns VBox status.
2772 * @param pSSM SSM operation handle.
2773 * @param pu Where to store the integer.
2774 */
2775SSMR3DECL(int) SSMR3GetGCUInt(PSSMHANDLE pSSM, PRTGCUINT pu)
2776{
2777 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2778 return ssmr3Read(pSSM, pu, sizeof(*pu));
2779 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2780 return VERR_SSM_INVALID_STATE;
2781}
2782
2783
2784/**
2785 * Loads a GC natural signed integer item from the current data unit.
2786 *
2787 * @returns VBox status.
2788 * @param pSSM SSM operation handle.
2789 * @param pi Where to store the integer.
2790 */
2791SSMR3DECL(int) SSMR3GetGCSInt(PSSMHANDLE pSSM, PRTGCINT pi)
2792{
2793 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2794 return ssmr3Read(pSSM, pi, sizeof(*pi));
2795 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2796 return VERR_SSM_INVALID_STATE;
2797}
2798
2799
2800/**
2801 * Loads a 32 bits GC physical address item from the current data unit.
2802 *
2803 * @returns VBox status.
2804 * @param pSSM SSM operation handle.
2805 * @param pGCPhys Where to store the GC physical address.
2806 */
2807SSMR3DECL(int) SSMR3GetGCPhys32(PSSMHANDLE pSSM, PRTGCPHYS32 pGCPhys)
2808{
2809 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2810 return ssmr3Read(pSSM, pGCPhys, sizeof(*pGCPhys));
2811 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2812 return VERR_SSM_INVALID_STATE;
2813}
2814
2815
2816/**
2817 * Loads a 64 bits GC physical address item from the current data unit.
2818 *
2819 * @returns VBox status.
2820 * @param pSSM SSM operation handle.
2821 * @param pGCPhys Where to store the GC physical address.
2822 */
2823SSMR3DECL(int) SSMR3GetGCPhys64(PSSMHANDLE pSSM, PRTGCPHYS64 pGCPhys)
2824{
2825 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2826 return ssmr3Read(pSSM, pGCPhys, sizeof(*pGCPhys));
2827 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2828 return VERR_SSM_INVALID_STATE;
2829}
2830
2831
2832/**
2833 * Loads a GC physical address item from the current data unit.
2834 *
2835 * @returns VBox status.
2836 * @param pSSM SSM operation handle.
2837 * @param pGCPhys Where to store the GC physical address.
2838 */
2839SSMR3DECL(int) SSMR3GetGCPhys(PSSMHANDLE pSSM, PRTGCPHYS pGCPhys)
2840{
2841 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2842 return ssmr3Read(pSSM, pGCPhys, sizeof(*pGCPhys));
2843 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2844 return VERR_SSM_INVALID_STATE;
2845}
2846
2847
2848/**
2849 * Loads a GC virtual address item from the current data unit.
2850 *
2851 * @returns VBox status.
2852 * @param pSSM SSM operation handle.
2853 * @param pGCPtr Where to store the GC virtual address.
2854 */
2855SSMR3DECL(int) SSMR3GetGCPtr(PSSMHANDLE pSSM, PRTGCPTR pGCPtr)
2856{
2857 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2858 return ssmr3Read(pSSM, pGCPtr, sizeof(*pGCPtr));
2859 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2860 return VERR_SSM_INVALID_STATE;
2861}
2862
2863
2864/**
2865 * Loads a GC virtual address (represented as unsigned integer) item from the current data unit.
2866 *
2867 * @returns VBox status.
2868 * @param pSSM SSM operation handle.
2869 * @param pGCPtr Where to store the GC virtual address.
2870 */
2871SSMR3DECL(int) SSMR3GetGCUIntPtr(PSSMHANDLE pSSM, PRTGCUINTPTR pGCPtr)
2872{
2873 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2874 return ssmr3Read(pSSM, pGCPtr, sizeof(*pGCPtr));
2875 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2876 return VERR_SSM_INVALID_STATE;
2877}
2878
2879
2880/**
2881 * Loads a I/O port address item from the current data unit.
2882 *
2883 * @returns VBox status.
2884 * @param pSSM SSM operation handle.
2885 * @param pIOPort Where to store the I/O port address.
2886 */
2887SSMR3DECL(int) SSMR3GetIOPort(PSSMHANDLE pSSM, PRTIOPORT pIOPort)
2888{
2889 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2890 return ssmr3Read(pSSM, pIOPort, sizeof(*pIOPort));
2891 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2892 return VERR_SSM_INVALID_STATE;
2893}
2894
2895
2896/**
2897 * Loads a selector item from the current data unit.
2898 *
2899 * @returns VBox status.
2900 * @param pSSM SSM operation handle.
2901 * @param pSel Where to store the selector.
2902 */
2903SSMR3DECL(int) SSMR3GetSel(PSSMHANDLE pSSM, PRTSEL pSel)
2904{
2905 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2906 return ssmr3Read(pSSM, pSel, sizeof(*pSel));
2907 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2908 return VERR_SSM_INVALID_STATE;
2909}
2910
2911
2912/**
2913 * Loads a memory item from the current data unit.
2914 *
2915 * @returns VBox status.
2916 * @param pSSM SSM operation handle.
2917 * @param pv Where to store the item.
2918 * @param cb Size of the item.
2919 */
2920SSMR3DECL(int) SSMR3GetMem(PSSMHANDLE pSSM, void *pv, size_t cb)
2921{
2922 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2923 return ssmr3Read(pSSM, pv, cb);
2924 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2925 return VERR_SSM_INVALID_STATE;
2926}
2927
2928
2929/**
2930 * Loads a string item from the current data unit.
2931 *
2932 * @returns VBox status.
2933 * @param pSSM SSM operation handle.
2934 * @param psz Where to store the item.
2935 * @param cbMax Max size of the item (including '\\0').
2936 */
2937SSMR3DECL(int) SSMR3GetStrZ(PSSMHANDLE pSSM, char *psz, size_t cbMax)
2938{
2939 return SSMR3GetStrZEx(pSSM, psz, cbMax, NULL);
2940}
2941
2942
2943/**
2944 * Loads a string item from the current data unit.
2945 *
2946 * @returns VBox status.
2947 * @param pSSM SSM operation handle.
2948 * @param psz Where to store the item.
2949 * @param cbMax Max size of the item (including '\\0').
2950 * @param pcbStr The length of the loaded string excluding the '\\0'. (optional)
2951 */
2952SSMR3DECL(int) SSMR3GetStrZEx(PSSMHANDLE pSSM, char *psz, size_t cbMax, size_t *pcbStr)
2953{
2954 if (pSSM->enmOp == SSMSTATE_LOAD_EXEC || pSSM->enmOp == SSMSTATE_OPEN_READ)
2955 {
2956 /* read size prefix. */
2957 uint32_t u32;
2958 int rc = SSMR3GetU32(pSSM, &u32);
2959 if (VBOX_SUCCESS(rc))
2960 {
2961 if (pcbStr)
2962 *pcbStr = u32;
2963 if (u32 < cbMax)
2964 {
2965 /* terminate and read string content. */
2966 psz[u32] = '\0';
2967 return ssmr3Read(pSSM, psz, u32);
2968 }
2969 return VERR_TOO_MUCH_DATA;
2970 }
2971 return rc;
2972 }
2973 AssertMsgFailed(("Invalid state %d\n", pSSM->enmOp));
2974 return VERR_SSM_INVALID_STATE;
2975}
2976
2977
2978
2979/**
2980 * Query what the VBox status code of the operation is.
2981 *
2982 * This can be used for putting and getting a batch of values
2983 * without bother checking the result till all the calls have
2984 * been made.
2985 *
2986 * @returns SSMAFTER enum value.
2987 * @param pSSM SSM operation handle.
2988 */
2989SSMR3DECL(int) SSMR3HandleGetStatus(PSSMHANDLE pSSM)
2990{
2991 return pSSM->rc;
2992}
2993
2994/**
2995 * Fail the load operation.
2996 *
2997 * This is mainly intended for sub item loaders (like timers) which
2998 * return code isn't necessarily heeded by the caller but is important
2999 * to SSM.
3000 *
3001 * @returns SSMAFTER enum value.
3002 * @param pSSM SSM operation handle.
3003 * @param iStatus Failure status code. This MUST be a VERR_*.
3004 */
3005SSMR3DECL(int) SSMR3HandleSetStatus(PSSMHANDLE pSSM, int iStatus)
3006{
3007 if (VBOX_FAILURE(iStatus))
3008 {
3009 if (VBOX_SUCCESS(pSSM->rc))
3010 pSSM->rc = iStatus;
3011 return pSSM->rc = iStatus;
3012 }
3013 AssertMsgFailed(("iStatus=%d %Vrc\n", iStatus, iStatus));
3014 return VERR_INVALID_PARAMETER;
3015}
3016
3017
3018/**
3019 * Query what to do after this operation.
3020 *
3021 * @returns SSMAFTER enum value.
3022 * @param pSSM SSM operation handle.
3023 */
3024SSMR3DECL(SSMAFTER) SSMR3HandleGetAfter(PSSMHANDLE pSSM)
3025{
3026 return pSSM->enmAfter;
3027}
3028
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