VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PGMSavedState.cpp@ 85972

Last change on this file since 85972 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 127.3 KB
Line 
1/* $Id: PGMSavedState.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * PGM - Page Manager and Monitor, The Saved State Part.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_PGM
23#include <VBox/vmm/pgm.h>
24#include <VBox/vmm/stam.h>
25#include <VBox/vmm/ssm.h>
26#include <VBox/vmm/pdmdrv.h>
27#include <VBox/vmm/pdmdev.h>
28#include "PGMInternal.h"
29#include <VBox/vmm/vm.h>
30#include "PGMInline.h"
31
32#include <VBox/param.h>
33#include <VBox/err.h>
34
35#include <iprt/asm.h>
36#include <iprt/assert.h>
37#include <iprt/crc.h>
38#include <iprt/mem.h>
39#include <iprt/sha.h>
40#include <iprt/string.h>
41#include <iprt/thread.h>
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47/** Saved state data unit version. */
48#define PGM_SAVED_STATE_VERSION 14
49/** Saved state data unit version before the PAE PDPE registers. */
50#define PGM_SAVED_STATE_VERSION_PRE_PAE 13
51/** Saved state data unit version after this includes ballooned page flags in
52 * the state (see @bugref{5515}). */
53#define PGM_SAVED_STATE_VERSION_BALLOON_BROKEN 12
54/** Saved state before the balloon change. */
55#define PGM_SAVED_STATE_VERSION_PRE_BALLOON 11
56/** Saved state data unit version used during 3.1 development, misses the RAM
57 * config. */
58#define PGM_SAVED_STATE_VERSION_NO_RAM_CFG 10
59/** Saved state data unit version for 3.0 (pre teleportation). */
60#define PGM_SAVED_STATE_VERSION_3_0_0 9
61/** Saved state data unit version for 2.2.2 and later. */
62#define PGM_SAVED_STATE_VERSION_2_2_2 8
63/** Saved state data unit version for 2.2.0. */
64#define PGM_SAVED_STATE_VERSION_RR_DESC 7
65/** Saved state data unit version. */
66#define PGM_SAVED_STATE_VERSION_OLD_PHYS_CODE 6
67
68
69/** @name Sparse state record types
70 * @{ */
71/** Zero page. No data. */
72#define PGM_STATE_REC_RAM_ZERO UINT8_C(0x00)
73/** Raw page. */
74#define PGM_STATE_REC_RAM_RAW UINT8_C(0x01)
75/** Raw MMIO2 page. */
76#define PGM_STATE_REC_MMIO2_RAW UINT8_C(0x02)
77/** Zero MMIO2 page. */
78#define PGM_STATE_REC_MMIO2_ZERO UINT8_C(0x03)
79/** Virgin ROM page. Followed by protection (8-bit) and the raw bits. */
80#define PGM_STATE_REC_ROM_VIRGIN UINT8_C(0x04)
81/** Raw shadowed ROM page. The protection (8-bit) precedes the raw bits. */
82#define PGM_STATE_REC_ROM_SHW_RAW UINT8_C(0x05)
83/** Zero shadowed ROM page. The protection (8-bit) is the only payload. */
84#define PGM_STATE_REC_ROM_SHW_ZERO UINT8_C(0x06)
85/** ROM protection (8-bit). */
86#define PGM_STATE_REC_ROM_PROT UINT8_C(0x07)
87/** Ballooned page. No data. */
88#define PGM_STATE_REC_RAM_BALLOONED UINT8_C(0x08)
89/** The last record type. */
90#define PGM_STATE_REC_LAST PGM_STATE_REC_RAM_BALLOONED
91/** End marker. */
92#define PGM_STATE_REC_END UINT8_C(0xff)
93/** Flag indicating that the data is preceded by the page address.
94 * For RAW pages this is a RTGCPHYS. For MMIO2 and ROM pages this is a 8-bit
95 * range ID and a 32-bit page index.
96 */
97#define PGM_STATE_REC_FLAG_ADDR UINT8_C(0x80)
98/** @} */
99
100/** The CRC-32 for a zero page. */
101#define PGM_STATE_CRC32_ZERO_PAGE UINT32_C(0xc71c0011)
102/** The CRC-32 for a zero half page. */
103#define PGM_STATE_CRC32_ZERO_HALF_PAGE UINT32_C(0xf1e8ba9e)
104
105
106
107/** @name Old Page types used in older saved states.
108 * @{ */
109/** Old saved state: The usual invalid zero entry. */
110#define PGMPAGETYPE_OLD_INVALID 0
111/** Old saved state: RAM page. (RWX) */
112#define PGMPAGETYPE_OLD_RAM 1
113/** Old saved state: MMIO2 page. (RWX) */
114#define PGMPAGETYPE_OLD_MMIO2 1
115/** Old saved state: MMIO2 page aliased over an MMIO page. (RWX)
116 * See PGMHandlerPhysicalPageAlias(). */
117#define PGMPAGETYPE_OLD_MMIO2_ALIAS_MMIO 2
118/** Old saved state: Shadowed ROM. (RWX) */
119#define PGMPAGETYPE_OLD_ROM_SHADOW 3
120/** Old saved state: ROM page. (R-X) */
121#define PGMPAGETYPE_OLD_ROM 4
122/** Old saved state: MMIO page. (---) */
123#define PGMPAGETYPE_OLD_MMIO 5
124/** @} */
125
126
127/*********************************************************************************************************************************
128* Structures and Typedefs *
129*********************************************************************************************************************************/
130/** For loading old saved states. (pre-smp) */
131typedef struct
132{
133 /** If set no conflict checks are required. (boolean) */
134 bool fMappingsFixed;
135 /** Size of fixed mapping */
136 uint32_t cbMappingFixed;
137 /** Base address (GC) of fixed mapping */
138 RTGCPTR GCPtrMappingFixed;
139 /** A20 gate mask.
140 * Our current approach to A20 emulation is to let REM do it and don't bother
141 * anywhere else. The interesting guests will be operating with it enabled anyway.
142 * But should the need arise, we'll subject physical addresses to this mask. */
143 RTGCPHYS GCPhysA20Mask;
144 /** A20 gate state - boolean! */
145 bool fA20Enabled;
146 /** The guest paging mode. */
147 PGMMODE enmGuestMode;
148} PGMOLD;
149
150
151/*********************************************************************************************************************************
152* Global Variables *
153*********************************************************************************************************************************/
154/** PGM fields to save/load. */
155
156static const SSMFIELD s_aPGMFields[] =
157{
158 SSMFIELD_ENTRY( PGM, fMappingsFixed),
159 SSMFIELD_ENTRY_GCPTR( PGM, GCPtrMappingFixed),
160 SSMFIELD_ENTRY( PGM, cbMappingFixed),
161 SSMFIELD_ENTRY( PGM, cBalloonedPages),
162 SSMFIELD_ENTRY_TERM()
163};
164
165static const SSMFIELD s_aPGMFieldsPreBalloon[] =
166{
167 SSMFIELD_ENTRY( PGM, fMappingsFixed),
168 SSMFIELD_ENTRY_GCPTR( PGM, GCPtrMappingFixed),
169 SSMFIELD_ENTRY( PGM, cbMappingFixed),
170 SSMFIELD_ENTRY_TERM()
171};
172
173static const SSMFIELD s_aPGMCpuFields[] =
174{
175 SSMFIELD_ENTRY( PGMCPU, fA20Enabled),
176 SSMFIELD_ENTRY_GCPHYS( PGMCPU, GCPhysA20Mask),
177 SSMFIELD_ENTRY( PGMCPU, enmGuestMode),
178 SSMFIELD_ENTRY( PGMCPU, aGCPhysGstPaePDs[0]),
179 SSMFIELD_ENTRY( PGMCPU, aGCPhysGstPaePDs[1]),
180 SSMFIELD_ENTRY( PGMCPU, aGCPhysGstPaePDs[2]),
181 SSMFIELD_ENTRY( PGMCPU, aGCPhysGstPaePDs[3]),
182 SSMFIELD_ENTRY_TERM()
183};
184
185static const SSMFIELD s_aPGMCpuFieldsPrePae[] =
186{
187 SSMFIELD_ENTRY( PGMCPU, fA20Enabled),
188 SSMFIELD_ENTRY_GCPHYS( PGMCPU, GCPhysA20Mask),
189 SSMFIELD_ENTRY( PGMCPU, enmGuestMode),
190 SSMFIELD_ENTRY_TERM()
191};
192
193static const SSMFIELD s_aPGMFields_Old[] =
194{
195 SSMFIELD_ENTRY( PGMOLD, fMappingsFixed),
196 SSMFIELD_ENTRY_GCPTR( PGMOLD, GCPtrMappingFixed),
197 SSMFIELD_ENTRY( PGMOLD, cbMappingFixed),
198 SSMFIELD_ENTRY( PGMOLD, fA20Enabled),
199 SSMFIELD_ENTRY_GCPHYS( PGMOLD, GCPhysA20Mask),
200 SSMFIELD_ENTRY( PGMOLD, enmGuestMode),
201 SSMFIELD_ENTRY_TERM()
202};
203
204
205/**
206 * Find the ROM tracking structure for the given page.
207 *
208 * @returns Pointer to the ROM page structure. NULL if the caller didn't check
209 * that it's a ROM page.
210 * @param pVM The cross context VM structure.
211 * @param GCPhys The address of the ROM page.
212 */
213static PPGMROMPAGE pgmR3GetRomPage(PVM pVM, RTGCPHYS GCPhys) /** @todo change this to take a hint. */
214{
215 for (PPGMROMRANGE pRomRange = pVM->pgm.s.CTX_SUFF(pRomRanges);
216 pRomRange;
217 pRomRange = pRomRange->CTX_SUFF(pNext))
218 {
219 RTGCPHYS off = GCPhys - pRomRange->GCPhys;
220 if (GCPhys - pRomRange->GCPhys < pRomRange->cb)
221 return &pRomRange->aPages[off >> PAGE_SHIFT];
222 }
223 return NULL;
224}
225
226
227/**
228 * Prepares the ROM pages for a live save.
229 *
230 * @returns VBox status code.
231 * @param pVM The cross context VM structure.
232 */
233static int pgmR3PrepRomPages(PVM pVM)
234{
235 /*
236 * Initialize the live save tracking in the ROM page descriptors.
237 */
238 pgmLock(pVM);
239 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
240 {
241 PPGMRAMRANGE pRamHint = NULL;;
242 uint32_t const cPages = pRom->cb >> PAGE_SHIFT;
243
244 for (uint32_t iPage = 0; iPage < cPages; iPage++)
245 {
246 pRom->aPages[iPage].LiveSave.u8Prot = (uint8_t)PGMROMPROT_INVALID;
247 pRom->aPages[iPage].LiveSave.fWrittenTo = false;
248 pRom->aPages[iPage].LiveSave.fDirty = true;
249 pRom->aPages[iPage].LiveSave.fDirtiedRecently = true;
250 if (!(pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED))
251 {
252 if (PGMROMPROT_IS_ROM(pRom->aPages[iPage].enmProt))
253 pRom->aPages[iPage].LiveSave.fWrittenTo = !PGM_PAGE_IS_ZERO(&pRom->aPages[iPage].Shadow) && !PGM_PAGE_IS_BALLOONED(&pRom->aPages[iPage].Shadow);
254 else
255 {
256 RTGCPHYS GCPhys = pRom->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
257 PPGMPAGE pPage;
258 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
259 AssertLogRelMsgRC(rc, ("%Rrc GCPhys=%RGp\n", rc, GCPhys));
260 if (RT_SUCCESS(rc))
261 pRom->aPages[iPage].LiveSave.fWrittenTo = !PGM_PAGE_IS_ZERO(pPage) && !PGM_PAGE_IS_BALLOONED(pPage);
262 else
263 pRom->aPages[iPage].LiveSave.fWrittenTo = !PGM_PAGE_IS_ZERO(&pRom->aPages[iPage].Shadow) && !PGM_PAGE_IS_BALLOONED(&pRom->aPages[iPage].Shadow);
264 }
265 }
266 }
267
268 pVM->pgm.s.LiveSave.Rom.cDirtyPages += cPages;
269 if (pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
270 pVM->pgm.s.LiveSave.Rom.cDirtyPages += cPages;
271 }
272 pgmUnlock(pVM);
273
274 return VINF_SUCCESS;
275}
276
277
278/**
279 * Assigns IDs to the ROM ranges and saves them.
280 *
281 * @returns VBox status code.
282 * @param pVM The cross context VM structure.
283 * @param pSSM Saved state handle.
284 */
285static int pgmR3SaveRomRanges(PVM pVM, PSSMHANDLE pSSM)
286{
287 pgmLock(pVM);
288 uint8_t id = 1;
289 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3, id++)
290 {
291 pRom->idSavedState = id;
292 SSMR3PutU8(pSSM, id);
293 SSMR3PutStrZ(pSSM, ""); /* device name */
294 SSMR3PutU32(pSSM, 0); /* device instance */
295 SSMR3PutU8(pSSM, 0); /* region */
296 SSMR3PutStrZ(pSSM, pRom->pszDesc);
297 SSMR3PutGCPhys(pSSM, pRom->GCPhys);
298 int rc = SSMR3PutGCPhys(pSSM, pRom->cb);
299 if (RT_FAILURE(rc))
300 break;
301 }
302 pgmUnlock(pVM);
303 return SSMR3PutU8(pSSM, UINT8_MAX);
304}
305
306
307/**
308 * Loads the ROM range ID assignments.
309 *
310 * @returns VBox status code.
311 *
312 * @param pVM The cross context VM structure.
313 * @param pSSM The saved state handle.
314 */
315static int pgmR3LoadRomRanges(PVM pVM, PSSMHANDLE pSSM)
316{
317 PGM_LOCK_ASSERT_OWNER(pVM);
318
319 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
320 pRom->idSavedState = UINT8_MAX;
321
322 for (;;)
323 {
324 /*
325 * Read the data.
326 */
327 uint8_t id;
328 int rc = SSMR3GetU8(pSSM, &id);
329 if (RT_FAILURE(rc))
330 return rc;
331 if (id == UINT8_MAX)
332 {
333 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
334 if (pRom->idSavedState != UINT8_MAX)
335 { /* likely */ }
336 else if (pRom->fFlags & PGMPHYS_ROM_FLAGS_MAYBE_MISSING_FROM_STATE)
337 LogRel(("PGM: The '%s' ROM was not found in the saved state, but it is marked as maybe-missing, so that's probably okay.\n",
338 pRom->pszDesc));
339 else
340 AssertLogRelMsg(pRom->idSavedState != UINT8_MAX,
341 ("The '%s' ROM was not found in the saved state. Probably due to some misconfiguration\n",
342 pRom->pszDesc));
343 return VINF_SUCCESS; /* the end */
344 }
345 AssertLogRelReturn(id != 0, VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
346
347 char szDevName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
348 rc = SSMR3GetStrZ(pSSM, szDevName, sizeof(szDevName));
349 AssertLogRelRCReturn(rc, rc);
350
351 uint32_t uInstance;
352 SSMR3GetU32(pSSM, &uInstance);
353 uint8_t iRegion;
354 SSMR3GetU8(pSSM, &iRegion);
355
356 char szDesc[64];
357 rc = SSMR3GetStrZ(pSSM, szDesc, sizeof(szDesc));
358 AssertLogRelRCReturn(rc, rc);
359
360 RTGCPHYS GCPhys;
361 SSMR3GetGCPhys(pSSM, &GCPhys);
362 RTGCPHYS cb;
363 rc = SSMR3GetGCPhys(pSSM, &cb);
364 if (RT_FAILURE(rc))
365 return rc;
366 AssertLogRelMsgReturn(!(GCPhys & PAGE_OFFSET_MASK), ("GCPhys=%RGp %s\n", GCPhys, szDesc), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
367 AssertLogRelMsgReturn(!(cb & PAGE_OFFSET_MASK), ("cb=%RGp %s\n", cb, szDesc), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
368
369 /*
370 * Locate a matching ROM range.
371 */
372 AssertLogRelMsgReturn( uInstance == 0
373 && iRegion == 0
374 && szDevName[0] == '\0',
375 ("GCPhys=%RGp %s\n", GCPhys, szDesc),
376 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
377 PPGMROMRANGE pRom;
378 for (pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
379 {
380 if ( pRom->idSavedState == UINT8_MAX
381 && !strcmp(pRom->pszDesc, szDesc))
382 {
383 pRom->idSavedState = id;
384 break;
385 }
386 }
387 if (!pRom)
388 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("ROM at %RGp by the name '%s' was not found"), GCPhys, szDesc);
389 } /* forever */
390}
391
392
393/**
394 * Scan ROM pages.
395 *
396 * @param pVM The cross context VM structure.
397 */
398static void pgmR3ScanRomPages(PVM pVM)
399{
400 /*
401 * The shadow ROMs.
402 */
403 pgmLock(pVM);
404 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
405 {
406 if (pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
407 {
408 uint32_t const cPages = pRom->cb >> PAGE_SHIFT;
409 for (uint32_t iPage = 0; iPage < cPages; iPage++)
410 {
411 PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
412 if (pRomPage->LiveSave.fWrittenTo)
413 {
414 pRomPage->LiveSave.fWrittenTo = false;
415 if (!pRomPage->LiveSave.fDirty)
416 {
417 pRomPage->LiveSave.fDirty = true;
418 pVM->pgm.s.LiveSave.Rom.cReadyPages--;
419 pVM->pgm.s.LiveSave.Rom.cDirtyPages++;
420 }
421 pRomPage->LiveSave.fDirtiedRecently = true;
422 }
423 else
424 pRomPage->LiveSave.fDirtiedRecently = false;
425 }
426 }
427 }
428 pgmUnlock(pVM);
429}
430
431
432/**
433 * Takes care of the virgin ROM pages in the first pass.
434 *
435 * This is an attempt at simplifying the handling of ROM pages a little bit.
436 * This ASSUMES that no new ROM ranges will be added and that they won't be
437 * relinked in any way.
438 *
439 * @param pVM The cross context VM structure.
440 * @param pSSM The SSM handle.
441 * @param fLiveSave Whether we're in a live save or not.
442 */
443static int pgmR3SaveRomVirginPages(PVM pVM, PSSMHANDLE pSSM, bool fLiveSave)
444{
445 pgmLock(pVM);
446 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
447 {
448 uint32_t const cPages = pRom->cb >> PAGE_SHIFT;
449 for (uint32_t iPage = 0; iPage < cPages; iPage++)
450 {
451 RTGCPHYS GCPhys = pRom->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
452 PGMROMPROT enmProt = pRom->aPages[iPage].enmProt;
453
454 /* Get the virgin page descriptor. */
455 PPGMPAGE pPage;
456 if (PGMROMPROT_IS_ROM(enmProt))
457 pPage = pgmPhysGetPage(pVM, GCPhys);
458 else
459 pPage = &pRom->aPages[iPage].Virgin;
460
461 /* Get the page bits. (Cannot use pgmPhysGCPhys2CCPtrInternalReadOnly here!) */
462 int rc = VINF_SUCCESS;
463 char abPage[PAGE_SIZE];
464 if ( !PGM_PAGE_IS_ZERO(pPage)
465 && !PGM_PAGE_IS_BALLOONED(pPage))
466 {
467 void const *pvPage;
468 rc = pgmPhysPageMapReadOnly(pVM, pPage, GCPhys, &pvPage);
469 if (RT_SUCCESS(rc))
470 memcpy(abPage, pvPage, PAGE_SIZE);
471 }
472 else
473 ASMMemZeroPage(abPage);
474 pgmUnlock(pVM);
475 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys), rc);
476
477 /* Save it. */
478 if (iPage > 0)
479 SSMR3PutU8(pSSM, PGM_STATE_REC_ROM_VIRGIN);
480 else
481 {
482 SSMR3PutU8(pSSM, PGM_STATE_REC_ROM_VIRGIN | PGM_STATE_REC_FLAG_ADDR);
483 SSMR3PutU8(pSSM, pRom->idSavedState);
484 SSMR3PutU32(pSSM, iPage);
485 }
486 SSMR3PutU8(pSSM, (uint8_t)enmProt);
487 rc = SSMR3PutMem(pSSM, abPage, PAGE_SIZE);
488 if (RT_FAILURE(rc))
489 return rc;
490
491 /* Update state. */
492 pgmLock(pVM);
493 pRom->aPages[iPage].LiveSave.u8Prot = (uint8_t)enmProt;
494 if (fLiveSave)
495 {
496 pVM->pgm.s.LiveSave.Rom.cDirtyPages--;
497 pVM->pgm.s.LiveSave.Rom.cReadyPages++;
498 pVM->pgm.s.LiveSave.cSavedPages++;
499 }
500 }
501 }
502 pgmUnlock(pVM);
503 return VINF_SUCCESS;
504}
505
506
507/**
508 * Saves dirty pages in the shadowed ROM ranges.
509 *
510 * Used by pgmR3LiveExecPart2 and pgmR3SaveExecMemory.
511 *
512 * @returns VBox status code.
513 * @param pVM The cross context VM structure.
514 * @param pSSM The SSM handle.
515 * @param fLiveSave Whether it's a live save or not.
516 * @param fFinalPass Whether this is the final pass or not.
517 */
518static int pgmR3SaveShadowedRomPages(PVM pVM, PSSMHANDLE pSSM, bool fLiveSave, bool fFinalPass)
519{
520 /*
521 * The Shadowed ROMs.
522 *
523 * ASSUMES that the ROM ranges are fixed.
524 * ASSUMES that all the ROM ranges are mapped.
525 */
526 pgmLock(pVM);
527 for (PPGMROMRANGE pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
528 {
529 if (pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)
530 {
531 uint32_t const cPages = pRom->cb >> PAGE_SHIFT;
532 uint32_t iPrevPage = cPages;
533 for (uint32_t iPage = 0; iPage < cPages; iPage++)
534 {
535 PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
536 if ( !fLiveSave
537 || ( pRomPage->LiveSave.fDirty
538 && ( ( !pRomPage->LiveSave.fDirtiedRecently
539 && !pRomPage->LiveSave.fWrittenTo)
540 || fFinalPass
541 )
542 )
543 )
544 {
545 uint8_t abPage[PAGE_SIZE];
546 PGMROMPROT enmProt = pRomPage->enmProt;
547 RTGCPHYS GCPhys = pRom->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
548 PPGMPAGE pPage = PGMROMPROT_IS_ROM(enmProt) ? &pRomPage->Shadow : pgmPhysGetPage(pVM, GCPhys);
549 bool fZero = PGM_PAGE_IS_ZERO(pPage) || PGM_PAGE_IS_BALLOONED(pPage); Assert(!PGM_PAGE_IS_BALLOONED(pPage)); /* Shouldn't be ballooned. */
550 int rc = VINF_SUCCESS;
551 if (!fZero)
552 {
553 void const *pvPage;
554 rc = pgmPhysPageMapReadOnly(pVM, pPage, GCPhys, &pvPage);
555 if (RT_SUCCESS(rc))
556 memcpy(abPage, pvPage, PAGE_SIZE);
557 }
558 if (fLiveSave && RT_SUCCESS(rc))
559 {
560 pRomPage->LiveSave.u8Prot = (uint8_t)enmProt;
561 pRomPage->LiveSave.fDirty = false;
562 pVM->pgm.s.LiveSave.Rom.cReadyPages++;
563 pVM->pgm.s.LiveSave.Rom.cDirtyPages--;
564 pVM->pgm.s.LiveSave.cSavedPages++;
565 }
566 pgmUnlock(pVM);
567 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys), rc);
568
569 if (iPage - 1U == iPrevPage && iPage > 0)
570 SSMR3PutU8(pSSM, (fZero ? PGM_STATE_REC_ROM_SHW_ZERO : PGM_STATE_REC_ROM_SHW_RAW));
571 else
572 {
573 SSMR3PutU8(pSSM, (fZero ? PGM_STATE_REC_ROM_SHW_ZERO : PGM_STATE_REC_ROM_SHW_RAW) | PGM_STATE_REC_FLAG_ADDR);
574 SSMR3PutU8(pSSM, pRom->idSavedState);
575 SSMR3PutU32(pSSM, iPage);
576 }
577 rc = SSMR3PutU8(pSSM, (uint8_t)enmProt);
578 if (!fZero)
579 rc = SSMR3PutMem(pSSM, abPage, PAGE_SIZE);
580 if (RT_FAILURE(rc))
581 return rc;
582
583 pgmLock(pVM);
584 iPrevPage = iPage;
585 }
586 /*
587 * In the final pass, make sure the protection is in sync.
588 */
589 else if ( fFinalPass
590 && pRomPage->LiveSave.u8Prot != pRomPage->enmProt)
591 {
592 PGMROMPROT enmProt = pRomPage->enmProt;
593 pRomPage->LiveSave.u8Prot = (uint8_t)enmProt;
594 pgmUnlock(pVM);
595
596 if (iPage - 1U == iPrevPage && iPage > 0)
597 SSMR3PutU8(pSSM, PGM_STATE_REC_ROM_PROT);
598 else
599 {
600 SSMR3PutU8(pSSM, PGM_STATE_REC_ROM_PROT | PGM_STATE_REC_FLAG_ADDR);
601 SSMR3PutU8(pSSM, pRom->idSavedState);
602 SSMR3PutU32(pSSM, iPage);
603 }
604 int rc = SSMR3PutU8(pSSM, (uint8_t)enmProt);
605 if (RT_FAILURE(rc))
606 return rc;
607
608 pgmLock(pVM);
609 iPrevPage = iPage;
610 }
611 }
612 }
613 }
614 pgmUnlock(pVM);
615 return VINF_SUCCESS;
616}
617
618
619/**
620 * Cleans up ROM pages after a live save.
621 *
622 * @param pVM The cross context VM structure.
623 */
624static void pgmR3DoneRomPages(PVM pVM)
625{
626 NOREF(pVM);
627}
628
629
630/**
631 * Prepares the MMIO2 pages for a live save.
632 *
633 * @returns VBox status code.
634 * @param pVM The cross context VM structure.
635 */
636static int pgmR3PrepMmio2Pages(PVM pVM)
637{
638 /*
639 * Initialize the live save tracking in the MMIO2 ranges.
640 * ASSUME nothing changes here.
641 */
642 pgmLock(pVM);
643 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
644 {
645 if (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
646 {
647 uint32_t const cPages = pRegMmio->RamRange.cb >> PAGE_SHIFT;
648 pgmUnlock(pVM);
649
650 PPGMLIVESAVEMMIO2PAGE paLSPages = (PPGMLIVESAVEMMIO2PAGE)MMR3HeapAllocZ(pVM, MM_TAG_PGM, sizeof(PGMLIVESAVEMMIO2PAGE) * cPages);
651 if (!paLSPages)
652 return VERR_NO_MEMORY;
653 for (uint32_t iPage = 0; iPage < cPages; iPage++)
654 {
655 /* Initialize it as a dirty zero page. */
656 paLSPages[iPage].fDirty = true;
657 paLSPages[iPage].cUnchangedScans = 0;
658 paLSPages[iPage].fZero = true;
659 paLSPages[iPage].u32CrcH1 = PGM_STATE_CRC32_ZERO_HALF_PAGE;
660 paLSPages[iPage].u32CrcH2 = PGM_STATE_CRC32_ZERO_HALF_PAGE;
661 }
662
663 pgmLock(pVM);
664 pRegMmio->paLSPages = paLSPages;
665 pVM->pgm.s.LiveSave.Mmio2.cDirtyPages += cPages;
666 }
667 }
668 pgmUnlock(pVM);
669 return VINF_SUCCESS;
670}
671
672
673/**
674 * Assigns IDs to the MMIO2 ranges and saves them.
675 *
676 * @returns VBox status code.
677 * @param pVM The cross context VM structure.
678 * @param pSSM Saved state handle.
679 */
680static int pgmR3SaveMmio2Ranges(PVM pVM, PSSMHANDLE pSSM)
681{
682 pgmLock(pVM);
683 uint8_t id = 1;
684 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
685 {
686 if (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
687 {
688 pRegMmio->idSavedState = id;
689 SSMR3PutU8(pSSM, id);
690 SSMR3PutStrZ(pSSM, pRegMmio->pDevInsR3->pReg->szName);
691 SSMR3PutU32(pSSM, pRegMmio->pDevInsR3->iInstance);
692 SSMR3PutU8(pSSM, pRegMmio->iRegion);
693 SSMR3PutStrZ(pSSM, pRegMmio->RamRange.pszDesc);
694 int rc = SSMR3PutGCPhys(pSSM, pRegMmio->RamRange.cb);
695 if (RT_FAILURE(rc))
696 break;
697 id++;
698 }
699 }
700 pgmUnlock(pVM);
701 return SSMR3PutU8(pSSM, UINT8_MAX);
702}
703
704
705/**
706 * Loads the MMIO2 range ID assignments.
707 *
708 * @returns VBox status code.
709 *
710 * @param pVM The cross context VM structure.
711 * @param pSSM The saved state handle.
712 */
713static int pgmR3LoadMmio2Ranges(PVM pVM, PSSMHANDLE pSSM)
714{
715 PGM_LOCK_ASSERT_OWNER(pVM);
716
717 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
718 if (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
719 pRegMmio->idSavedState = UINT8_MAX;
720
721 for (;;)
722 {
723 /*
724 * Read the data.
725 */
726 uint8_t id;
727 int rc = SSMR3GetU8(pSSM, &id);
728 if (RT_FAILURE(rc))
729 return rc;
730 if (id == UINT8_MAX)
731 {
732 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
733 AssertLogRelMsg( pRegMmio->idSavedState != UINT8_MAX
734 || !(pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2),
735 ("%s\n", pRegMmio->RamRange.pszDesc));
736 return VINF_SUCCESS; /* the end */
737 }
738 AssertLogRelReturn(id != 0, VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
739
740 char szDevName[RT_SIZEOFMEMB(PDMDEVREG, szName)];
741 rc = SSMR3GetStrZ(pSSM, szDevName, sizeof(szDevName));
742 AssertLogRelRCReturn(rc, rc);
743
744 uint32_t uInstance;
745 SSMR3GetU32(pSSM, &uInstance);
746 uint8_t iRegion;
747 SSMR3GetU8(pSSM, &iRegion);
748
749 char szDesc[64];
750 rc = SSMR3GetStrZ(pSSM, szDesc, sizeof(szDesc));
751 AssertLogRelRCReturn(rc, rc);
752
753 RTGCPHYS cb;
754 rc = SSMR3GetGCPhys(pSSM, &cb);
755 AssertLogRelMsgReturn(!(cb & PAGE_OFFSET_MASK), ("cb=%RGp %s\n", cb, szDesc), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
756
757 /*
758 * Locate a matching MMIO2 range.
759 */
760 PPGMREGMMIO2RANGE pRegMmio;
761 for (pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
762 {
763 if ( pRegMmio->idSavedState == UINT8_MAX
764 && pRegMmio->iRegion == iRegion
765 && pRegMmio->pDevInsR3->iInstance == uInstance
766 && (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
767 && !strcmp(pRegMmio->pDevInsR3->pReg->szName, szDevName))
768 {
769 pRegMmio->idSavedState = id;
770 break;
771 }
772 }
773 if (!pRegMmio)
774 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to locate a MMIO2 range called '%s' owned by %s/%u, region %d"),
775 szDesc, szDevName, uInstance, iRegion);
776
777 /*
778 * Validate the configuration, the size of the MMIO2 region should be
779 * the same.
780 */
781 if (cb != pRegMmio->RamRange.cb)
782 {
783 LogRel(("PGM: MMIO2 region \"%s\" size mismatch: saved=%RGp config=%RGp\n",
784 pRegMmio->RamRange.pszDesc, cb, pRegMmio->RamRange.cb));
785 if (cb > pRegMmio->RamRange.cb) /* bad idea? */
786 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("MMIO2 region \"%s\" size mismatch: saved=%RGp config=%RGp"),
787 pRegMmio->RamRange.pszDesc, cb, pRegMmio->RamRange.cb);
788 }
789 } /* forever */
790}
791
792
793/**
794 * Scans one MMIO2 page.
795 *
796 * @returns True if changed, false if unchanged.
797 *
798 * @param pVM The cross context VM structure.
799 * @param pbPage The page bits.
800 * @param pLSPage The live save tracking structure for the page.
801 *
802 */
803DECLINLINE(bool) pgmR3ScanMmio2Page(PVM pVM, uint8_t const *pbPage, PPGMLIVESAVEMMIO2PAGE pLSPage)
804{
805 /*
806 * Special handling of zero pages.
807 */
808 bool const fZero = pLSPage->fZero;
809 if (fZero)
810 {
811 if (ASMMemIsZeroPage(pbPage))
812 {
813 /* Not modified. */
814 if (pLSPage->fDirty)
815 pLSPage->cUnchangedScans++;
816 return false;
817 }
818
819 pLSPage->fZero = false;
820 pLSPage->u32CrcH1 = RTCrc32(pbPage, PAGE_SIZE / 2);
821 }
822 else
823 {
824 /*
825 * CRC the first half, if it doesn't match the page is dirty and
826 * we won't check the 2nd half (we'll do that next time).
827 */
828 uint32_t u32CrcH1 = RTCrc32(pbPage, PAGE_SIZE / 2);
829 if (u32CrcH1 == pLSPage->u32CrcH1)
830 {
831 uint32_t u32CrcH2 = RTCrc32(pbPage + PAGE_SIZE / 2, PAGE_SIZE / 2);
832 if (u32CrcH2 == pLSPage->u32CrcH2)
833 {
834 /* Probably not modified. */
835 if (pLSPage->fDirty)
836 pLSPage->cUnchangedScans++;
837 return false;
838 }
839
840 pLSPage->u32CrcH2 = u32CrcH2;
841 }
842 else
843 {
844 pLSPage->u32CrcH1 = u32CrcH1;
845 if ( u32CrcH1 == PGM_STATE_CRC32_ZERO_HALF_PAGE
846 && ASMMemIsZeroPage(pbPage))
847 {
848 pLSPage->u32CrcH2 = PGM_STATE_CRC32_ZERO_HALF_PAGE;
849 pLSPage->fZero = true;
850 }
851 }
852 }
853
854 /* dirty page path */
855 pLSPage->cUnchangedScans = 0;
856 if (!pLSPage->fDirty)
857 {
858 pLSPage->fDirty = true;
859 pVM->pgm.s.LiveSave.Mmio2.cReadyPages--;
860 pVM->pgm.s.LiveSave.Mmio2.cDirtyPages++;
861 if (fZero)
862 pVM->pgm.s.LiveSave.Mmio2.cZeroPages--;
863 }
864 return true;
865}
866
867
868/**
869 * Scan for MMIO2 page modifications.
870 *
871 * @param pVM The cross context VM structure.
872 * @param uPass The pass number.
873 */
874static void pgmR3ScanMmio2Pages(PVM pVM, uint32_t uPass)
875{
876 /*
877 * Since this is a bit expensive we lower the scan rate after a little while.
878 */
879 if ( ( (uPass & 3) != 0
880 && uPass > 10)
881 || uPass == SSM_PASS_FINAL)
882 return;
883
884 pgmLock(pVM); /* paranoia */
885 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
886 if (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
887 {
888 PPGMLIVESAVEMMIO2PAGE paLSPages = pRegMmio->paLSPages;
889 uint32_t cPages = pRegMmio->RamRange.cb >> PAGE_SHIFT;
890 pgmUnlock(pVM);
891
892 for (uint32_t iPage = 0; iPage < cPages; iPage++)
893 {
894 uint8_t const *pbPage = (uint8_t const *)pRegMmio->pvR3 + iPage * PAGE_SIZE;
895 pgmR3ScanMmio2Page(pVM, pbPage, &paLSPages[iPage]);
896 }
897
898 pgmLock(pVM);
899 }
900 pgmUnlock(pVM);
901
902}
903
904
905/**
906 * Save quiescent MMIO2 pages.
907 *
908 * @returns VBox status code.
909 * @param pVM The cross context VM structure.
910 * @param pSSM The SSM handle.
911 * @param fLiveSave Whether it's a live save or not.
912 * @param uPass The pass number.
913 */
914static int pgmR3SaveMmio2Pages(PVM pVM, PSSMHANDLE pSSM, bool fLiveSave, uint32_t uPass)
915{
916 /** @todo implement live saving of MMIO2 pages. (Need some way of telling the
917 * device that we wish to know about changes.) */
918
919 int rc = VINF_SUCCESS;
920 if (uPass == SSM_PASS_FINAL)
921 {
922 /*
923 * The mop up round.
924 */
925 pgmLock(pVM);
926 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3;
927 pRegMmio && RT_SUCCESS(rc);
928 pRegMmio = pRegMmio->pNextR3)
929 if (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
930 {
931 PPGMLIVESAVEMMIO2PAGE paLSPages = pRegMmio->paLSPages;
932 uint8_t const *pbPage = (uint8_t const *)pRegMmio->RamRange.pvR3;
933 uint32_t cPages = pRegMmio->RamRange.cb >> PAGE_SHIFT;
934 uint32_t iPageLast = cPages;
935 for (uint32_t iPage = 0; iPage < cPages; iPage++, pbPage += PAGE_SIZE)
936 {
937 uint8_t u8Type;
938 if (!fLiveSave)
939 u8Type = ASMMemIsZeroPage(pbPage) ? PGM_STATE_REC_MMIO2_ZERO : PGM_STATE_REC_MMIO2_RAW;
940 else
941 {
942 /* Try figure if it's a clean page, compare the SHA-1 to be really sure. */
943 if ( !paLSPages[iPage].fDirty
944 && !pgmR3ScanMmio2Page(pVM, pbPage, &paLSPages[iPage]))
945 {
946 if (paLSPages[iPage].fZero)
947 continue;
948
949 uint8_t abSha1Hash[RTSHA1_HASH_SIZE];
950 RTSha1(pbPage, PAGE_SIZE, abSha1Hash);
951 if (!memcmp(abSha1Hash, paLSPages[iPage].abSha1Saved, sizeof(abSha1Hash)))
952 continue;
953 }
954 u8Type = paLSPages[iPage].fZero ? PGM_STATE_REC_MMIO2_ZERO : PGM_STATE_REC_MMIO2_RAW;
955 pVM->pgm.s.LiveSave.cSavedPages++;
956 }
957
958 if (iPage != 0 && iPage == iPageLast + 1)
959 rc = SSMR3PutU8(pSSM, u8Type);
960 else
961 {
962 SSMR3PutU8(pSSM, u8Type | PGM_STATE_REC_FLAG_ADDR);
963 SSMR3PutU8(pSSM, pRegMmio->idSavedState);
964 rc = SSMR3PutU32(pSSM, iPage);
965 }
966 if (u8Type == PGM_STATE_REC_MMIO2_RAW)
967 rc = SSMR3PutMem(pSSM, pbPage, PAGE_SIZE);
968 if (RT_FAILURE(rc))
969 break;
970 iPageLast = iPage;
971 }
972 }
973 pgmUnlock(pVM);
974 }
975 /*
976 * Reduce the rate after a little while since the current MMIO2 approach is
977 * a bit expensive.
978 * We position it two passes after the scan pass to avoid saving busy pages.
979 */
980 else if ( uPass <= 10
981 || (uPass & 3) == 2)
982 {
983 pgmLock(pVM);
984 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3;
985 pRegMmio && RT_SUCCESS(rc);
986 pRegMmio = pRegMmio->pNextR3)
987 if (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
988 {
989 PPGMLIVESAVEMMIO2PAGE paLSPages = pRegMmio->paLSPages;
990 uint8_t const *pbPage = (uint8_t const *)pRegMmio->RamRange.pvR3;
991 uint32_t cPages = pRegMmio->RamRange.cb >> PAGE_SHIFT;
992 uint32_t iPageLast = cPages;
993 pgmUnlock(pVM);
994
995 for (uint32_t iPage = 0; iPage < cPages; iPage++, pbPage += PAGE_SIZE)
996 {
997 /* Skip clean pages and pages which hasn't quiesced. */
998 if (!paLSPages[iPage].fDirty)
999 continue;
1000 if (paLSPages[iPage].cUnchangedScans < 3)
1001 continue;
1002 if (pgmR3ScanMmio2Page(pVM, pbPage, &paLSPages[iPage]))
1003 continue;
1004
1005 /* Save it. */
1006 bool const fZero = paLSPages[iPage].fZero;
1007 uint8_t abPage[PAGE_SIZE];
1008 if (!fZero)
1009 {
1010 memcpy(abPage, pbPage, PAGE_SIZE);
1011 RTSha1(abPage, PAGE_SIZE, paLSPages[iPage].abSha1Saved);
1012 }
1013
1014 uint8_t u8Type = paLSPages[iPage].fZero ? PGM_STATE_REC_MMIO2_ZERO : PGM_STATE_REC_MMIO2_RAW;
1015 if (iPage != 0 && iPage == iPageLast + 1)
1016 rc = SSMR3PutU8(pSSM, u8Type);
1017 else
1018 {
1019 SSMR3PutU8(pSSM, u8Type | PGM_STATE_REC_FLAG_ADDR);
1020 SSMR3PutU8(pSSM, pRegMmio->idSavedState);
1021 rc = SSMR3PutU32(pSSM, iPage);
1022 }
1023 if (u8Type == PGM_STATE_REC_MMIO2_RAW)
1024 rc = SSMR3PutMem(pSSM, abPage, PAGE_SIZE);
1025 if (RT_FAILURE(rc))
1026 break;
1027
1028 /* Housekeeping. */
1029 paLSPages[iPage].fDirty = false;
1030 pVM->pgm.s.LiveSave.Mmio2.cDirtyPages--;
1031 pVM->pgm.s.LiveSave.Mmio2.cReadyPages++;
1032 if (u8Type == PGM_STATE_REC_MMIO2_ZERO)
1033 pVM->pgm.s.LiveSave.Mmio2.cZeroPages++;
1034 pVM->pgm.s.LiveSave.cSavedPages++;
1035 iPageLast = iPage;
1036 }
1037
1038 pgmLock(pVM);
1039 }
1040 pgmUnlock(pVM);
1041 }
1042
1043 return rc;
1044}
1045
1046
1047/**
1048 * Cleans up MMIO2 pages after a live save.
1049 *
1050 * @param pVM The cross context VM structure.
1051 */
1052static void pgmR3DoneMmio2Pages(PVM pVM)
1053{
1054 /*
1055 * Free the tracking structures for the MMIO2 pages.
1056 * We do the freeing outside the lock in case the VM is running.
1057 */
1058 pgmLock(pVM);
1059 for (PPGMREGMMIO2RANGE pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
1060 if (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2)
1061 {
1062 void *pvMmio2ToFree = pRegMmio->paLSPages;
1063 if (pvMmio2ToFree)
1064 {
1065 pRegMmio->paLSPages = NULL;
1066 pgmUnlock(pVM);
1067 MMR3HeapFree(pvMmio2ToFree);
1068 pgmLock(pVM);
1069 }
1070 }
1071 pgmUnlock(pVM);
1072}
1073
1074
1075/**
1076 * Prepares the RAM pages for a live save.
1077 *
1078 * @returns VBox status code.
1079 * @param pVM The cross context VM structure.
1080 */
1081static int pgmR3PrepRamPages(PVM pVM)
1082{
1083
1084 /*
1085 * Try allocating tracking structures for the ram ranges.
1086 *
1087 * To avoid lock contention, we leave the lock every time we're allocating
1088 * a new array. This means we'll have to ditch the allocation and start
1089 * all over again if the RAM range list changes in-between.
1090 *
1091 * Note! pgmR3SaveDone will always be called and it is therefore responsible
1092 * for cleaning up.
1093 */
1094 PPGMRAMRANGE pCur;
1095 pgmLock(pVM);
1096 do
1097 {
1098 for (pCur = pVM->pgm.s.pRamRangesXR3; pCur; pCur = pCur->pNextR3)
1099 {
1100 if ( !pCur->paLSPages
1101 && !PGM_RAM_RANGE_IS_AD_HOC(pCur))
1102 {
1103 uint32_t const idRamRangesGen = pVM->pgm.s.idRamRangesGen;
1104 uint32_t const cPages = pCur->cb >> PAGE_SHIFT;
1105 pgmUnlock(pVM);
1106 PPGMLIVESAVERAMPAGE paLSPages = (PPGMLIVESAVERAMPAGE)MMR3HeapAllocZ(pVM, MM_TAG_PGM, cPages * sizeof(PGMLIVESAVERAMPAGE));
1107 if (!paLSPages)
1108 return VERR_NO_MEMORY;
1109 pgmLock(pVM);
1110 if (pVM->pgm.s.idRamRangesGen != idRamRangesGen)
1111 {
1112 pgmUnlock(pVM);
1113 MMR3HeapFree(paLSPages);
1114 pgmLock(pVM);
1115 break; /* try again */
1116 }
1117 pCur->paLSPages = paLSPages;
1118
1119 /*
1120 * Initialize the array.
1121 */
1122 uint32_t iPage = cPages;
1123 while (iPage-- > 0)
1124 {
1125 /** @todo yield critsect! (after moving this away from EMT0) */
1126 PCPGMPAGE pPage = &pCur->aPages[iPage];
1127 paLSPages[iPage].cDirtied = 0;
1128 paLSPages[iPage].fDirty = 1; /* everything is dirty at this time */
1129 paLSPages[iPage].fWriteMonitored = 0;
1130 paLSPages[iPage].fWriteMonitoredJustNow = 0;
1131 paLSPages[iPage].u2Reserved = 0;
1132 switch (PGM_PAGE_GET_TYPE(pPage))
1133 {
1134 case PGMPAGETYPE_RAM:
1135 if ( PGM_PAGE_IS_ZERO(pPage)
1136 || PGM_PAGE_IS_BALLOONED(pPage))
1137 {
1138 paLSPages[iPage].fZero = 1;
1139 paLSPages[iPage].fShared = 0;
1140#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1141 paLSPages[iPage].u32Crc = PGM_STATE_CRC32_ZERO_PAGE;
1142#endif
1143 }
1144 else if (PGM_PAGE_IS_SHARED(pPage))
1145 {
1146 paLSPages[iPage].fZero = 0;
1147 paLSPages[iPage].fShared = 1;
1148#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1149 paLSPages[iPage].u32Crc = UINT32_MAX;
1150#endif
1151 }
1152 else
1153 {
1154 paLSPages[iPage].fZero = 0;
1155 paLSPages[iPage].fShared = 0;
1156#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1157 paLSPages[iPage].u32Crc = UINT32_MAX;
1158#endif
1159 }
1160 paLSPages[iPage].fIgnore = 0;
1161 pVM->pgm.s.LiveSave.Ram.cDirtyPages++;
1162 break;
1163
1164 case PGMPAGETYPE_ROM_SHADOW:
1165 case PGMPAGETYPE_ROM:
1166 {
1167 paLSPages[iPage].fZero = 0;
1168 paLSPages[iPage].fShared = 0;
1169 paLSPages[iPage].fDirty = 0;
1170 paLSPages[iPage].fIgnore = 1;
1171#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1172 paLSPages[iPage].u32Crc = UINT32_MAX;
1173#endif
1174 pVM->pgm.s.LiveSave.cIgnoredPages++;
1175 break;
1176 }
1177
1178 default:
1179 AssertMsgFailed(("%R[pgmpage]", pPage));
1180 RT_FALL_THRU();
1181 case PGMPAGETYPE_MMIO2:
1182 case PGMPAGETYPE_MMIO2_ALIAS_MMIO:
1183 paLSPages[iPage].fZero = 0;
1184 paLSPages[iPage].fShared = 0;
1185 paLSPages[iPage].fDirty = 0;
1186 paLSPages[iPage].fIgnore = 1;
1187#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1188 paLSPages[iPage].u32Crc = UINT32_MAX;
1189#endif
1190 pVM->pgm.s.LiveSave.cIgnoredPages++;
1191 break;
1192
1193 case PGMPAGETYPE_MMIO:
1194 case PGMPAGETYPE_SPECIAL_ALIAS_MMIO:
1195 paLSPages[iPage].fZero = 0;
1196 paLSPages[iPage].fShared = 0;
1197 paLSPages[iPage].fDirty = 0;
1198 paLSPages[iPage].fIgnore = 1;
1199#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1200 paLSPages[iPage].u32Crc = UINT32_MAX;
1201#endif
1202 pVM->pgm.s.LiveSave.cIgnoredPages++;
1203 break;
1204 }
1205 }
1206 }
1207 }
1208 } while (pCur);
1209 pgmUnlock(pVM);
1210
1211 return VINF_SUCCESS;
1212}
1213
1214
1215/**
1216 * Saves the RAM configuration.
1217 *
1218 * @returns VBox status code.
1219 * @param pVM The cross context VM structure.
1220 * @param pSSM The saved state handle.
1221 */
1222static int pgmR3SaveRamConfig(PVM pVM, PSSMHANDLE pSSM)
1223{
1224 uint32_t cbRamHole = 0;
1225 int rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "RamHoleSize", &cbRamHole, MM_RAM_HOLE_SIZE_DEFAULT);
1226 AssertRCReturn(rc, rc);
1227
1228 uint64_t cbRam = 0;
1229 rc = CFGMR3QueryU64Def(CFGMR3GetRoot(pVM), "RamSize", &cbRam, 0);
1230 AssertRCReturn(rc, rc);
1231
1232 SSMR3PutU32(pSSM, cbRamHole);
1233 return SSMR3PutU64(pSSM, cbRam);
1234}
1235
1236
1237/**
1238 * Loads and verifies the RAM configuration.
1239 *
1240 * @returns VBox status code.
1241 * @param pVM The cross context VM structure.
1242 * @param pSSM The saved state handle.
1243 */
1244static int pgmR3LoadRamConfig(PVM pVM, PSSMHANDLE pSSM)
1245{
1246 uint32_t cbRamHoleCfg = 0;
1247 int rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "RamHoleSize", &cbRamHoleCfg, MM_RAM_HOLE_SIZE_DEFAULT);
1248 AssertRCReturn(rc, rc);
1249
1250 uint64_t cbRamCfg = 0;
1251 rc = CFGMR3QueryU64Def(CFGMR3GetRoot(pVM), "RamSize", &cbRamCfg, 0);
1252 AssertRCReturn(rc, rc);
1253
1254 uint32_t cbRamHoleSaved;
1255 SSMR3GetU32(pSSM, &cbRamHoleSaved);
1256
1257 uint64_t cbRamSaved;
1258 rc = SSMR3GetU64(pSSM, &cbRamSaved);
1259 AssertRCReturn(rc, rc);
1260
1261 if ( cbRamHoleCfg != cbRamHoleSaved
1262 || cbRamCfg != cbRamSaved)
1263 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Ram config mismatch: saved=%RX64/%RX32 config=%RX64/%RX32 (RAM/Hole)"),
1264 cbRamSaved, cbRamHoleSaved, cbRamCfg, cbRamHoleCfg);
1265 return VINF_SUCCESS;
1266}
1267
1268#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1269
1270/**
1271 * Calculates the CRC-32 for a RAM page and updates the live save page tracking
1272 * info with it.
1273 *
1274 * @param pVM The cross context VM structure.
1275 * @param pCur The current RAM range.
1276 * @param paLSPages The current array of live save page tracking
1277 * structures.
1278 * @param iPage The page index.
1279 */
1280static void pgmR3StateCalcCrc32ForRamPage(PVM pVM, PPGMRAMRANGE pCur, PPGMLIVESAVERAMPAGE paLSPages, uint32_t iPage)
1281{
1282 RTGCPHYS GCPhys = pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
1283 PGMPAGEMAPLOCK PgMpLck;
1284 void const *pvPage;
1285 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, &pCur->aPages[iPage], GCPhys, &pvPage, &PgMpLck);
1286 if (RT_SUCCESS(rc))
1287 {
1288 paLSPages[iPage].u32Crc = RTCrc32(pvPage, PAGE_SIZE);
1289 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
1290 }
1291 else
1292 paLSPages[iPage].u32Crc = UINT32_MAX; /* Invalid */
1293}
1294
1295
1296/**
1297 * Verifies the CRC-32 for a page given it's raw bits.
1298 *
1299 * @param pvPage The page bits.
1300 * @param pCur The current RAM range.
1301 * @param paLSPages The current array of live save page tracking
1302 * structures.
1303 * @param iPage The page index.
1304 */
1305static void pgmR3StateVerifyCrc32ForPage(void const *pvPage, PPGMRAMRANGE pCur, PPGMLIVESAVERAMPAGE paLSPages, uint32_t iPage, const char *pszWhere)
1306{
1307 if (paLSPages[iPage].u32Crc != UINT32_MAX)
1308 {
1309 uint32_t u32Crc = RTCrc32(pvPage, PAGE_SIZE);
1310 Assert( ( !PGM_PAGE_IS_ZERO(&pCur->aPages[iPage])
1311 && !PGM_PAGE_IS_BALLOONED(&pCur->aPages[iPage]))
1312 || u32Crc == PGM_STATE_CRC32_ZERO_PAGE);
1313 AssertMsg(paLSPages[iPage].u32Crc == u32Crc,
1314 ("%08x != %08x for %RGp %R[pgmpage] %s\n", paLSPages[iPage].u32Crc, u32Crc,
1315 pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pCur->aPages[iPage], pszWhere));
1316 }
1317}
1318
1319
1320/**
1321 * Verifies the CRC-32 for a RAM page.
1322 *
1323 * @param pVM The cross context VM structure.
1324 * @param pCur The current RAM range.
1325 * @param paLSPages The current array of live save page tracking
1326 * structures.
1327 * @param iPage The page index.
1328 */
1329static void pgmR3StateVerifyCrc32ForRamPage(PVM pVM, PPGMRAMRANGE pCur, PPGMLIVESAVERAMPAGE paLSPages, uint32_t iPage, const char *pszWhere)
1330{
1331 if (paLSPages[iPage].u32Crc != UINT32_MAX)
1332 {
1333 RTGCPHYS GCPhys = pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
1334 PGMPAGEMAPLOCK PgMpLck;
1335 void const *pvPage;
1336 int rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, &pCur->aPages[iPage], GCPhys, &pvPage, &PgMpLck);
1337 if (RT_SUCCESS(rc))
1338 {
1339 pgmR3StateVerifyCrc32ForPage(pvPage, pCur, paLSPages, iPage, pszWhere);
1340 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
1341 }
1342 }
1343}
1344
1345#endif /* PGMLIVESAVERAMPAGE_WITH_CRC32 */
1346
1347/**
1348 * Scan for RAM page modifications and reprotect them.
1349 *
1350 * @param pVM The cross context VM structure.
1351 * @param fFinalPass Whether this is the final pass or not.
1352 */
1353static void pgmR3ScanRamPages(PVM pVM, bool fFinalPass)
1354{
1355 /*
1356 * The RAM.
1357 */
1358 RTGCPHYS GCPhysCur = 0;
1359 PPGMRAMRANGE pCur;
1360 pgmLock(pVM);
1361 do
1362 {
1363 uint32_t const idRamRangesGen = pVM->pgm.s.idRamRangesGen;
1364 for (pCur = pVM->pgm.s.pRamRangesXR3; pCur; pCur = pCur->pNextR3)
1365 {
1366 if ( pCur->GCPhysLast > GCPhysCur
1367 && !PGM_RAM_RANGE_IS_AD_HOC(pCur))
1368 {
1369 PPGMLIVESAVERAMPAGE paLSPages = pCur->paLSPages;
1370 uint32_t cPages = pCur->cb >> PAGE_SHIFT;
1371 uint32_t iPage = GCPhysCur <= pCur->GCPhys ? 0 : (GCPhysCur - pCur->GCPhys) >> PAGE_SHIFT;
1372 GCPhysCur = 0;
1373 for (; iPage < cPages; iPage++)
1374 {
1375 /* Do yield first. */
1376 if ( !fFinalPass
1377#ifndef PGMLIVESAVERAMPAGE_WITH_CRC32
1378 && (iPage & 0x7ff) == 0x100
1379#endif
1380 && PDMR3CritSectYield(pVM, &pVM->pgm.s.CritSectX)
1381 && pVM->pgm.s.idRamRangesGen != idRamRangesGen)
1382 {
1383 GCPhysCur = pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
1384 break; /* restart */
1385 }
1386
1387 /* Skip already ignored pages. */
1388 if (paLSPages[iPage].fIgnore)
1389 continue;
1390
1391 if (RT_LIKELY(PGM_PAGE_GET_TYPE(&pCur->aPages[iPage]) == PGMPAGETYPE_RAM))
1392 {
1393 /*
1394 * A RAM page.
1395 */
1396 switch (PGM_PAGE_GET_STATE(&pCur->aPages[iPage]))
1397 {
1398 case PGM_PAGE_STATE_ALLOCATED:
1399 /** @todo Optimize this: Don't always re-enable write
1400 * monitoring if the page is known to be very busy. */
1401 if (PGM_PAGE_IS_WRITTEN_TO(&pCur->aPages[iPage]))
1402 {
1403 AssertMsg(paLSPages[iPage].fWriteMonitored,
1404 ("%RGp %R[pgmpage]\n", pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pCur->aPages[iPage]));
1405 PGM_PAGE_CLEAR_WRITTEN_TO(pVM, &pCur->aPages[iPage]);
1406 Assert(pVM->pgm.s.cWrittenToPages > 0);
1407 pVM->pgm.s.cWrittenToPages--;
1408 }
1409 else
1410 {
1411 AssertMsg(!paLSPages[iPage].fWriteMonitored,
1412 ("%RGp %R[pgmpage]\n", pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT), &pCur->aPages[iPage]));
1413 pVM->pgm.s.LiveSave.Ram.cMonitoredPages++;
1414 }
1415
1416 if (!paLSPages[iPage].fDirty)
1417 {
1418 pVM->pgm.s.LiveSave.Ram.cReadyPages--;
1419 if (paLSPages[iPage].fZero)
1420 pVM->pgm.s.LiveSave.Ram.cZeroPages--;
1421 pVM->pgm.s.LiveSave.Ram.cDirtyPages++;
1422 if (++paLSPages[iPage].cDirtied > PGMLIVSAVEPAGE_MAX_DIRTIED)
1423 paLSPages[iPage].cDirtied = PGMLIVSAVEPAGE_MAX_DIRTIED;
1424 }
1425
1426 pgmPhysPageWriteMonitor(pVM, &pCur->aPages[iPage],
1427 pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT));
1428 paLSPages[iPage].fWriteMonitored = 1;
1429 paLSPages[iPage].fWriteMonitoredJustNow = 1;
1430 paLSPages[iPage].fDirty = 1;
1431 paLSPages[iPage].fZero = 0;
1432 paLSPages[iPage].fShared = 0;
1433#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1434 paLSPages[iPage].u32Crc = UINT32_MAX; /* invalid */
1435#endif
1436 break;
1437
1438 case PGM_PAGE_STATE_WRITE_MONITORED:
1439 Assert(paLSPages[iPage].fWriteMonitored);
1440 if (PGM_PAGE_GET_WRITE_LOCKS(&pCur->aPages[iPage]) == 0)
1441 {
1442#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1443 if (paLSPages[iPage].fWriteMonitoredJustNow)
1444 pgmR3StateCalcCrc32ForRamPage(pVM, pCur, paLSPages, iPage);
1445 else
1446 pgmR3StateVerifyCrc32ForRamPage(pVM, pCur, paLSPages, iPage, "scan");
1447#endif
1448 paLSPages[iPage].fWriteMonitoredJustNow = 0;
1449 }
1450 else
1451 {
1452 paLSPages[iPage].fWriteMonitoredJustNow = 1;
1453#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1454 paLSPages[iPage].u32Crc = UINT32_MAX; /* invalid */
1455#endif
1456 if (!paLSPages[iPage].fDirty)
1457 {
1458 pVM->pgm.s.LiveSave.Ram.cReadyPages--;
1459 pVM->pgm.s.LiveSave.Ram.cDirtyPages++;
1460 if (++paLSPages[iPage].cDirtied > PGMLIVSAVEPAGE_MAX_DIRTIED)
1461 paLSPages[iPage].cDirtied = PGMLIVSAVEPAGE_MAX_DIRTIED;
1462 }
1463 }
1464 break;
1465
1466 case PGM_PAGE_STATE_ZERO:
1467 case PGM_PAGE_STATE_BALLOONED:
1468 if (!paLSPages[iPage].fZero)
1469 {
1470 if (!paLSPages[iPage].fDirty)
1471 {
1472 paLSPages[iPage].fDirty = 1;
1473 pVM->pgm.s.LiveSave.Ram.cReadyPages--;
1474 pVM->pgm.s.LiveSave.Ram.cDirtyPages++;
1475 }
1476 paLSPages[iPage].fZero = 1;
1477 paLSPages[iPage].fShared = 0;
1478#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1479 paLSPages[iPage].u32Crc = PGM_STATE_CRC32_ZERO_PAGE;
1480#endif
1481 }
1482 break;
1483
1484 case PGM_PAGE_STATE_SHARED:
1485 if (!paLSPages[iPage].fShared)
1486 {
1487 if (!paLSPages[iPage].fDirty)
1488 {
1489 paLSPages[iPage].fDirty = 1;
1490 pVM->pgm.s.LiveSave.Ram.cReadyPages--;
1491 if (paLSPages[iPage].fZero)
1492 pVM->pgm.s.LiveSave.Ram.cZeroPages--;
1493 pVM->pgm.s.LiveSave.Ram.cDirtyPages++;
1494 }
1495 paLSPages[iPage].fZero = 0;
1496 paLSPages[iPage].fShared = 1;
1497#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1498 pgmR3StateCalcCrc32ForRamPage(pVM, pCur, paLSPages, iPage);
1499#endif
1500 }
1501 break;
1502 }
1503 }
1504 else
1505 {
1506 /*
1507 * All other types => Ignore the page.
1508 */
1509 Assert(!paLSPages[iPage].fIgnore); /* skipped before switch */
1510 paLSPages[iPage].fIgnore = 1;
1511 if (paLSPages[iPage].fWriteMonitored)
1512 {
1513 /** @todo this doesn't hold water when we start monitoring MMIO2 and ROM shadow
1514 * pages! */
1515 if (RT_UNLIKELY(PGM_PAGE_GET_STATE(&pCur->aPages[iPage]) == PGM_PAGE_STATE_WRITE_MONITORED))
1516 {
1517 AssertMsgFailed(("%R[pgmpage]", &pCur->aPages[iPage])); /* shouldn't happen. */
1518 PGM_PAGE_SET_STATE(pVM, &pCur->aPages[iPage], PGM_PAGE_STATE_ALLOCATED);
1519 Assert(pVM->pgm.s.cMonitoredPages > 0);
1520 pVM->pgm.s.cMonitoredPages--;
1521 }
1522 if (PGM_PAGE_IS_WRITTEN_TO(&pCur->aPages[iPage]))
1523 {
1524 PGM_PAGE_CLEAR_WRITTEN_TO(pVM, &pCur->aPages[iPage]);
1525 Assert(pVM->pgm.s.cWrittenToPages > 0);
1526 pVM->pgm.s.cWrittenToPages--;
1527 }
1528 pVM->pgm.s.LiveSave.Ram.cMonitoredPages--;
1529 }
1530
1531 /** @todo the counting doesn't quite work out here. fix later? */
1532 if (paLSPages[iPage].fDirty)
1533 pVM->pgm.s.LiveSave.Ram.cDirtyPages--;
1534 else
1535 {
1536 pVM->pgm.s.LiveSave.Ram.cReadyPages--;
1537 if (paLSPages[iPage].fZero)
1538 pVM->pgm.s.LiveSave.Ram.cZeroPages--;
1539 }
1540 pVM->pgm.s.LiveSave.cIgnoredPages++;
1541 }
1542 } /* for each page in range */
1543
1544 if (GCPhysCur != 0)
1545 break; /* Yield + ramrange change */
1546 GCPhysCur = pCur->GCPhysLast;
1547 }
1548 } /* for each range */
1549 } while (pCur);
1550 pgmUnlock(pVM);
1551}
1552
1553
1554/**
1555 * Save quiescent RAM pages.
1556 *
1557 * @returns VBox status code.
1558 * @param pVM The cross context VM structure.
1559 * @param pSSM The SSM handle.
1560 * @param fLiveSave Whether it's a live save or not.
1561 * @param uPass The pass number.
1562 */
1563static int pgmR3SaveRamPages(PVM pVM, PSSMHANDLE pSSM, bool fLiveSave, uint32_t uPass)
1564{
1565 NOREF(fLiveSave);
1566
1567 /*
1568 * The RAM.
1569 */
1570 RTGCPHYS GCPhysLast = NIL_RTGCPHYS;
1571 RTGCPHYS GCPhysCur = 0;
1572 PPGMRAMRANGE pCur;
1573
1574 pgmLock(pVM);
1575 do
1576 {
1577 uint32_t const idRamRangesGen = pVM->pgm.s.idRamRangesGen;
1578 for (pCur = pVM->pgm.s.pRamRangesXR3; pCur; pCur = pCur->pNextR3)
1579 {
1580 if ( pCur->GCPhysLast > GCPhysCur
1581 && !PGM_RAM_RANGE_IS_AD_HOC(pCur))
1582 {
1583 PPGMLIVESAVERAMPAGE paLSPages = pCur->paLSPages;
1584 uint32_t cPages = pCur->cb >> PAGE_SHIFT;
1585 uint32_t iPage = GCPhysCur <= pCur->GCPhys ? 0 : (GCPhysCur - pCur->GCPhys) >> PAGE_SHIFT;
1586 GCPhysCur = 0;
1587 for (; iPage < cPages; iPage++)
1588 {
1589 /* Do yield first. */
1590 if ( uPass != SSM_PASS_FINAL
1591 && (iPage & 0x7ff) == 0x100
1592 && PDMR3CritSectYield(pVM, &pVM->pgm.s.CritSectX)
1593 && pVM->pgm.s.idRamRangesGen != idRamRangesGen)
1594 {
1595 GCPhysCur = pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
1596 break; /* restart */
1597 }
1598
1599 PPGMPAGE pCurPage = &pCur->aPages[iPage];
1600
1601 /*
1602 * Only save pages that haven't changed since last scan and are dirty.
1603 */
1604 if ( uPass != SSM_PASS_FINAL
1605 && paLSPages)
1606 {
1607 if (!paLSPages[iPage].fDirty)
1608 continue;
1609 if (paLSPages[iPage].fWriteMonitoredJustNow)
1610 continue;
1611 if (paLSPages[iPage].fIgnore)
1612 continue;
1613 if (PGM_PAGE_GET_TYPE(pCurPage) != PGMPAGETYPE_RAM) /* in case of recent remappings */
1614 continue;
1615 if ( PGM_PAGE_GET_STATE(pCurPage)
1616 != ( paLSPages[iPage].fZero
1617 ? PGM_PAGE_STATE_ZERO
1618 : paLSPages[iPage].fShared
1619 ? PGM_PAGE_STATE_SHARED
1620 : PGM_PAGE_STATE_WRITE_MONITORED))
1621 continue;
1622 if (PGM_PAGE_GET_WRITE_LOCKS(&pCur->aPages[iPage]) > 0)
1623 continue;
1624 }
1625 else
1626 {
1627 if ( paLSPages
1628 && !paLSPages[iPage].fDirty
1629 && !paLSPages[iPage].fIgnore)
1630 {
1631#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1632 if (PGM_PAGE_GET_TYPE(pCurPage) != PGMPAGETYPE_RAM)
1633 pgmR3StateVerifyCrc32ForRamPage(pVM, pCur, paLSPages, iPage, "save#1");
1634#endif
1635 continue;
1636 }
1637 if (PGM_PAGE_GET_TYPE(pCurPage) != PGMPAGETYPE_RAM)
1638 continue;
1639 }
1640
1641 /*
1642 * Do the saving outside the PGM critsect since SSM may block on I/O.
1643 */
1644 int rc;
1645 RTGCPHYS GCPhys = pCur->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
1646 bool fZero = PGM_PAGE_IS_ZERO(pCurPage);
1647 bool fBallooned = PGM_PAGE_IS_BALLOONED(pCurPage);
1648 bool fSkipped = false;
1649
1650 if (!fZero && !fBallooned)
1651 {
1652 /*
1653 * Copy the page and then save it outside the lock (since any
1654 * SSM call may block).
1655 */
1656 uint8_t abPage[PAGE_SIZE];
1657 PGMPAGEMAPLOCK PgMpLck;
1658 void const *pvPage;
1659 rc = pgmPhysGCPhys2CCPtrInternalReadOnly(pVM, pCurPage, GCPhys, &pvPage, &PgMpLck);
1660 if (RT_SUCCESS(rc))
1661 {
1662 memcpy(abPage, pvPage, PAGE_SIZE);
1663#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1664 if (paLSPages)
1665 pgmR3StateVerifyCrc32ForPage(abPage, pCur, paLSPages, iPage, "save#3");
1666#endif
1667 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
1668 }
1669 pgmUnlock(pVM);
1670 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc GCPhys=%RGp\n", rc, GCPhys), rc);
1671
1672 /* Try save some memory when restoring. */
1673 if (!ASMMemIsZeroPage(pvPage))
1674 {
1675 if (GCPhys == GCPhysLast + PAGE_SIZE)
1676 SSMR3PutU8(pSSM, PGM_STATE_REC_RAM_RAW);
1677 else
1678 {
1679 SSMR3PutU8(pSSM, PGM_STATE_REC_RAM_RAW | PGM_STATE_REC_FLAG_ADDR);
1680 SSMR3PutGCPhys(pSSM, GCPhys);
1681 }
1682 rc = SSMR3PutMem(pSSM, abPage, PAGE_SIZE);
1683 }
1684 else
1685 {
1686 if (GCPhys == GCPhysLast + PAGE_SIZE)
1687 rc = SSMR3PutU8(pSSM, PGM_STATE_REC_RAM_ZERO);
1688 else
1689 {
1690 SSMR3PutU8(pSSM, PGM_STATE_REC_RAM_ZERO | PGM_STATE_REC_FLAG_ADDR);
1691 rc = SSMR3PutGCPhys(pSSM, GCPhys);
1692 }
1693 }
1694 }
1695 else
1696 {
1697 /*
1698 * Dirty zero or ballooned page.
1699 */
1700#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1701 if (paLSPages)
1702 pgmR3StateVerifyCrc32ForRamPage(pVM, pCur, paLSPages, iPage, "save#2");
1703#endif
1704 pgmUnlock(pVM);
1705
1706 uint8_t u8RecType = fBallooned ? PGM_STATE_REC_RAM_BALLOONED : PGM_STATE_REC_RAM_ZERO;
1707 if (GCPhys == GCPhysLast + PAGE_SIZE)
1708 rc = SSMR3PutU8(pSSM, u8RecType);
1709 else
1710 {
1711 SSMR3PutU8(pSSM, u8RecType | PGM_STATE_REC_FLAG_ADDR);
1712 rc = SSMR3PutGCPhys(pSSM, GCPhys);
1713 }
1714 }
1715 if (RT_FAILURE(rc))
1716 return rc;
1717
1718 pgmLock(pVM);
1719 if (!fSkipped)
1720 GCPhysLast = GCPhys;
1721 if (paLSPages)
1722 {
1723 paLSPages[iPage].fDirty = 0;
1724 pVM->pgm.s.LiveSave.Ram.cReadyPages++;
1725 if (fZero)
1726 pVM->pgm.s.LiveSave.Ram.cZeroPages++;
1727 pVM->pgm.s.LiveSave.Ram.cDirtyPages--;
1728 pVM->pgm.s.LiveSave.cSavedPages++;
1729 }
1730 if (idRamRangesGen != pVM->pgm.s.idRamRangesGen)
1731 {
1732 GCPhysCur = GCPhys | PAGE_OFFSET_MASK;
1733 break; /* restart */
1734 }
1735
1736 } /* for each page in range */
1737
1738 if (GCPhysCur != 0)
1739 break; /* Yield + ramrange change */
1740 GCPhysCur = pCur->GCPhysLast;
1741 }
1742 } /* for each range */
1743 } while (pCur);
1744
1745 pgmUnlock(pVM);
1746
1747 return VINF_SUCCESS;
1748}
1749
1750
1751/**
1752 * Cleans up RAM pages after a live save.
1753 *
1754 * @param pVM The cross context VM structure.
1755 */
1756static void pgmR3DoneRamPages(PVM pVM)
1757{
1758 /*
1759 * Free the tracking arrays and disable write monitoring.
1760 *
1761 * Play nice with the PGM lock in case we're called while the VM is still
1762 * running. This means we have to delay the freeing since we wish to use
1763 * paLSPages as an indicator of which RAM ranges which we need to scan for
1764 * write monitored pages.
1765 */
1766 void *pvToFree = NULL;
1767 PPGMRAMRANGE pCur;
1768 uint32_t cMonitoredPages = 0;
1769 pgmLock(pVM);
1770 do
1771 {
1772 for (pCur = pVM->pgm.s.pRamRangesXR3; pCur; pCur = pCur->pNextR3)
1773 {
1774 if (pCur->paLSPages)
1775 {
1776 if (pvToFree)
1777 {
1778 uint32_t idRamRangesGen = pVM->pgm.s.idRamRangesGen;
1779 pgmUnlock(pVM);
1780 MMR3HeapFree(pvToFree);
1781 pvToFree = NULL;
1782 pgmLock(pVM);
1783 if (idRamRangesGen != pVM->pgm.s.idRamRangesGen)
1784 break; /* start over again. */
1785 }
1786
1787 pvToFree = pCur->paLSPages;
1788 pCur->paLSPages = NULL;
1789
1790 uint32_t iPage = pCur->cb >> PAGE_SHIFT;
1791 while (iPage--)
1792 {
1793 PPGMPAGE pPage = &pCur->aPages[iPage];
1794 PGM_PAGE_CLEAR_WRITTEN_TO(pVM, pPage);
1795 if (PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_WRITE_MONITORED)
1796 {
1797 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ALLOCATED);
1798 cMonitoredPages++;
1799 }
1800 }
1801 }
1802 }
1803 } while (pCur);
1804
1805 Assert(pVM->pgm.s.cMonitoredPages >= cMonitoredPages);
1806 if (pVM->pgm.s.cMonitoredPages < cMonitoredPages)
1807 pVM->pgm.s.cMonitoredPages = 0;
1808 else
1809 pVM->pgm.s.cMonitoredPages -= cMonitoredPages;
1810
1811 pgmUnlock(pVM);
1812
1813 MMR3HeapFree(pvToFree);
1814 pvToFree = NULL;
1815}
1816
1817
1818/**
1819 * @callback_method_impl{FNSSMINTLIVEEXEC}
1820 */
1821static DECLCALLBACK(int) pgmR3LiveExec(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
1822{
1823 int rc;
1824
1825 /*
1826 * Save the MMIO2 and ROM range IDs in pass 0.
1827 */
1828 if (uPass == 0)
1829 {
1830 rc = pgmR3SaveRamConfig(pVM, pSSM);
1831 if (RT_FAILURE(rc))
1832 return rc;
1833 rc = pgmR3SaveRomRanges(pVM, pSSM);
1834 if (RT_FAILURE(rc))
1835 return rc;
1836 rc = pgmR3SaveMmio2Ranges(pVM, pSSM);
1837 if (RT_FAILURE(rc))
1838 return rc;
1839 }
1840 /*
1841 * Reset the page-per-second estimate to avoid inflation by the initial
1842 * load of zero pages. pgmR3LiveVote ASSUMES this is done at pass 7.
1843 */
1844 else if (uPass == 7)
1845 {
1846 pVM->pgm.s.LiveSave.cSavedPages = 0;
1847 pVM->pgm.s.LiveSave.uSaveStartNS = RTTimeNanoTS();
1848 }
1849
1850 /*
1851 * Do the scanning.
1852 */
1853 pgmR3ScanRomPages(pVM);
1854 pgmR3ScanMmio2Pages(pVM, uPass);
1855 pgmR3ScanRamPages(pVM, false /*fFinalPass*/);
1856 pgmR3PoolClearAll(pVM, true /*fFlushRemTlb*/); /** @todo this could perhaps be optimized a bit. */
1857
1858 /*
1859 * Save the pages.
1860 */
1861 if (uPass == 0)
1862 rc = pgmR3SaveRomVirginPages( pVM, pSSM, true /*fLiveSave*/);
1863 else
1864 rc = VINF_SUCCESS;
1865 if (RT_SUCCESS(rc))
1866 rc = pgmR3SaveShadowedRomPages(pVM, pSSM, true /*fLiveSave*/, false /*fFinalPass*/);
1867 if (RT_SUCCESS(rc))
1868 rc = pgmR3SaveMmio2Pages( pVM, pSSM, true /*fLiveSave*/, uPass);
1869 if (RT_SUCCESS(rc))
1870 rc = pgmR3SaveRamPages( pVM, pSSM, true /*fLiveSave*/, uPass);
1871 SSMR3PutU8(pSSM, PGM_STATE_REC_END); /* (Ignore the rc, SSM takes care of it.) */
1872
1873 return rc;
1874}
1875
1876
1877/**
1878 * @callback_method_impl{FNSSMINTLIVEVOTE}
1879 */
1880static DECLCALLBACK(int) pgmR3LiveVote(PVM pVM, PSSMHANDLE pSSM, uint32_t uPass)
1881{
1882 /*
1883 * Update and calculate parameters used in the decision making.
1884 */
1885 const uint32_t cHistoryEntries = RT_ELEMENTS(pVM->pgm.s.LiveSave.acDirtyPagesHistory);
1886
1887 /* update history. */
1888 pgmLock(pVM);
1889 uint32_t const cWrittenToPages = pVM->pgm.s.cWrittenToPages;
1890 pgmUnlock(pVM);
1891 uint32_t const cDirtyNow = pVM->pgm.s.LiveSave.Rom.cDirtyPages
1892 + pVM->pgm.s.LiveSave.Mmio2.cDirtyPages
1893 + pVM->pgm.s.LiveSave.Ram.cDirtyPages
1894 + cWrittenToPages;
1895 uint32_t i = pVM->pgm.s.LiveSave.iDirtyPagesHistory;
1896 pVM->pgm.s.LiveSave.acDirtyPagesHistory[i] = cDirtyNow;
1897 pVM->pgm.s.LiveSave.iDirtyPagesHistory = (i + 1) % cHistoryEntries;
1898
1899 /* calc shortterm average (4 passes). */
1900 AssertCompile(RT_ELEMENTS(pVM->pgm.s.LiveSave.acDirtyPagesHistory) > 4);
1901 uint64_t cTotal = pVM->pgm.s.LiveSave.acDirtyPagesHistory[i];
1902 cTotal += pVM->pgm.s.LiveSave.acDirtyPagesHistory[(i + cHistoryEntries - 1) % cHistoryEntries];
1903 cTotal += pVM->pgm.s.LiveSave.acDirtyPagesHistory[(i + cHistoryEntries - 2) % cHistoryEntries];
1904 cTotal += pVM->pgm.s.LiveSave.acDirtyPagesHistory[(i + cHistoryEntries - 3) % cHistoryEntries];
1905 uint32_t const cDirtyPagesShort = cTotal / 4;
1906 pVM->pgm.s.LiveSave.cDirtyPagesShort = cDirtyPagesShort;
1907
1908 /* calc longterm average. */
1909 cTotal = 0;
1910 if (uPass < cHistoryEntries)
1911 for (i = 0; i < cHistoryEntries && i <= uPass; i++)
1912 cTotal += pVM->pgm.s.LiveSave.acDirtyPagesHistory[i];
1913 else
1914 for (i = 0; i < cHistoryEntries; i++)
1915 cTotal += pVM->pgm.s.LiveSave.acDirtyPagesHistory[i];
1916 uint32_t const cDirtyPagesLong = cTotal / cHistoryEntries;
1917 pVM->pgm.s.LiveSave.cDirtyPagesLong = cDirtyPagesLong;
1918
1919 /* estimate the speed */
1920 uint64_t cNsElapsed = RTTimeNanoTS() - pVM->pgm.s.LiveSave.uSaveStartNS;
1921 uint32_t cPagesPerSecond = (uint32_t)( pVM->pgm.s.LiveSave.cSavedPages
1922 / ((long double)cNsElapsed / 1000000000.0) );
1923 pVM->pgm.s.LiveSave.cPagesPerSecond = cPagesPerSecond;
1924
1925 /*
1926 * Try make a decision.
1927 */
1928 if ( cDirtyPagesShort <= cDirtyPagesLong
1929 && ( cDirtyNow <= cDirtyPagesShort
1930 || cDirtyNow - cDirtyPagesShort < RT_MIN(cDirtyPagesShort / 8, 16)
1931 )
1932 )
1933 {
1934 if (uPass > 10)
1935 {
1936 uint32_t cMsLeftShort = (uint32_t)(cDirtyPagesShort / (long double)cPagesPerSecond * 1000.0);
1937 uint32_t cMsLeftLong = (uint32_t)(cDirtyPagesLong / (long double)cPagesPerSecond * 1000.0);
1938 uint32_t cMsMaxDowntime = SSMR3HandleMaxDowntime(pSSM);
1939 if (cMsMaxDowntime < 32)
1940 cMsMaxDowntime = 32;
1941 if ( ( cMsLeftLong <= cMsMaxDowntime
1942 && cMsLeftShort < cMsMaxDowntime)
1943 || cMsLeftShort < cMsMaxDowntime / 2
1944 )
1945 {
1946 Log(("pgmR3LiveVote: VINF_SUCCESS - pass=%d cDirtyPagesShort=%u|%ums cDirtyPagesLong=%u|%ums cMsMaxDowntime=%u\n",
1947 uPass, cDirtyPagesShort, cMsLeftShort, cDirtyPagesLong, cMsLeftLong, cMsMaxDowntime));
1948 return VINF_SUCCESS;
1949 }
1950 }
1951 else
1952 {
1953 if ( ( cDirtyPagesShort <= 128
1954 && cDirtyPagesLong <= 1024)
1955 || cDirtyPagesLong <= 256
1956 )
1957 {
1958 Log(("pgmR3LiveVote: VINF_SUCCESS - pass=%d cDirtyPagesShort=%u cDirtyPagesLong=%u\n", uPass, cDirtyPagesShort, cDirtyPagesLong));
1959 return VINF_SUCCESS;
1960 }
1961 }
1962 }
1963
1964 /*
1965 * Come up with a completion percentage. Currently this is a simple
1966 * dirty page (long term) vs. total pages ratio + some pass trickery.
1967 */
1968 unsigned uPctDirty = (unsigned)( (long double)cDirtyPagesLong
1969 / (pVM->pgm.s.cAllPages - pVM->pgm.s.LiveSave.cIgnoredPages - pVM->pgm.s.cZeroPages) );
1970 if (uPctDirty <= 100)
1971 SSMR3HandleReportLivePercent(pSSM, RT_MIN(100 - uPctDirty, uPass * 2));
1972 else
1973 AssertMsgFailed(("uPctDirty=%u cDirtyPagesLong=%#x cAllPages=%#x cIgnoredPages=%#x cZeroPages=%#x\n",
1974 uPctDirty, cDirtyPagesLong, pVM->pgm.s.cAllPages, pVM->pgm.s.LiveSave.cIgnoredPages, pVM->pgm.s.cZeroPages));
1975
1976 return VINF_SSM_VOTE_FOR_ANOTHER_PASS;
1977}
1978
1979
1980/**
1981 * @callback_method_impl{FNSSMINTLIVEPREP}
1982 *
1983 * This will attempt to allocate and initialize the tracking structures. It
1984 * will also prepare for write monitoring of pages and initialize PGM::LiveSave.
1985 * pgmR3SaveDone will do the cleanups.
1986 */
1987static DECLCALLBACK(int) pgmR3LivePrep(PVM pVM, PSSMHANDLE pSSM)
1988{
1989 /*
1990 * Indicate that we will be using the write monitoring.
1991 */
1992 pgmLock(pVM);
1993 /** @todo find a way of mediating this when more users are added. */
1994 if (pVM->pgm.s.fPhysWriteMonitoringEngaged)
1995 {
1996 pgmUnlock(pVM);
1997 AssertLogRelFailedReturn(VERR_PGM_WRITE_MONITOR_ENGAGED);
1998 }
1999 pVM->pgm.s.fPhysWriteMonitoringEngaged = true;
2000 pgmUnlock(pVM);
2001
2002 /*
2003 * Initialize the statistics.
2004 */
2005 pVM->pgm.s.LiveSave.Rom.cReadyPages = 0;
2006 pVM->pgm.s.LiveSave.Rom.cDirtyPages = 0;
2007 pVM->pgm.s.LiveSave.Mmio2.cReadyPages = 0;
2008 pVM->pgm.s.LiveSave.Mmio2.cDirtyPages = 0;
2009 pVM->pgm.s.LiveSave.Ram.cReadyPages = 0;
2010 pVM->pgm.s.LiveSave.Ram.cDirtyPages = 0;
2011 pVM->pgm.s.LiveSave.cIgnoredPages = 0;
2012 pVM->pgm.s.LiveSave.fActive = true;
2013 for (unsigned i = 0; i < RT_ELEMENTS(pVM->pgm.s.LiveSave.acDirtyPagesHistory); i++)
2014 pVM->pgm.s.LiveSave.acDirtyPagesHistory[i] = UINT32_MAX / 2;
2015 pVM->pgm.s.LiveSave.iDirtyPagesHistory = 0;
2016 pVM->pgm.s.LiveSave.cSavedPages = 0;
2017 pVM->pgm.s.LiveSave.uSaveStartNS = RTTimeNanoTS();
2018 pVM->pgm.s.LiveSave.cPagesPerSecond = 8192;
2019
2020 /*
2021 * Per page type.
2022 */
2023 int rc = pgmR3PrepRomPages(pVM);
2024 if (RT_SUCCESS(rc))
2025 rc = pgmR3PrepMmio2Pages(pVM);
2026 if (RT_SUCCESS(rc))
2027 rc = pgmR3PrepRamPages(pVM);
2028
2029 NOREF(pSSM);
2030 return rc;
2031}
2032
2033
2034/**
2035 * @callback_method_impl{FNSSMINTSAVEEXEC}
2036 */
2037static DECLCALLBACK(int) pgmR3SaveExec(PVM pVM, PSSMHANDLE pSSM)
2038{
2039 int rc = VINF_SUCCESS;
2040 PPGM pPGM = &pVM->pgm.s;
2041
2042 /*
2043 * Lock PGM and set the no-more-writes indicator.
2044 */
2045 pgmLock(pVM);
2046 pVM->pgm.s.fNoMorePhysWrites = true;
2047
2048 /*
2049 * Save basic data (required / unaffected by relocation).
2050 */
2051 bool const fMappingsFixed = pVM->pgm.s.fMappingsFixed;
2052 pVM->pgm.s.fMappingsFixed |= pVM->pgm.s.fMappingsFixedRestored;
2053 SSMR3PutStruct(pSSM, pPGM, &s_aPGMFields[0]);
2054 pVM->pgm.s.fMappingsFixed = fMappingsFixed;
2055
2056 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
2057 rc = SSMR3PutStruct(pSSM, &pVM->apCpusR3[idCpu]->pgm.s, &s_aPGMCpuFields[0]);
2058
2059 /*
2060 * Save the (remainder of the) memory.
2061 */
2062 if (RT_SUCCESS(rc))
2063 {
2064 if (pVM->pgm.s.LiveSave.fActive)
2065 {
2066 pgmR3ScanRomPages(pVM);
2067 pgmR3ScanMmio2Pages(pVM, SSM_PASS_FINAL);
2068 pgmR3ScanRamPages(pVM, true /*fFinalPass*/);
2069
2070 rc = pgmR3SaveShadowedRomPages( pVM, pSSM, true /*fLiveSave*/, true /*fFinalPass*/);
2071 if (RT_SUCCESS(rc))
2072 rc = pgmR3SaveMmio2Pages( pVM, pSSM, true /*fLiveSave*/, SSM_PASS_FINAL);
2073 if (RT_SUCCESS(rc))
2074 rc = pgmR3SaveRamPages( pVM, pSSM, true /*fLiveSave*/, SSM_PASS_FINAL);
2075 }
2076 else
2077 {
2078 rc = pgmR3SaveRamConfig(pVM, pSSM);
2079 if (RT_SUCCESS(rc))
2080 rc = pgmR3SaveRomRanges(pVM, pSSM);
2081 if (RT_SUCCESS(rc))
2082 rc = pgmR3SaveMmio2Ranges(pVM, pSSM);
2083 if (RT_SUCCESS(rc))
2084 rc = pgmR3SaveRomVirginPages( pVM, pSSM, false /*fLiveSave*/);
2085 if (RT_SUCCESS(rc))
2086 rc = pgmR3SaveShadowedRomPages(pVM, pSSM, false /*fLiveSave*/, true /*fFinalPass*/);
2087 if (RT_SUCCESS(rc))
2088 rc = pgmR3SaveMmio2Pages( pVM, pSSM, false /*fLiveSave*/, SSM_PASS_FINAL);
2089 if (RT_SUCCESS(rc))
2090 rc = pgmR3SaveRamPages( pVM, pSSM, false /*fLiveSave*/, SSM_PASS_FINAL);
2091 }
2092 SSMR3PutU8(pSSM, PGM_STATE_REC_END); /* (Ignore the rc, SSM takes of it.) */
2093 }
2094
2095 pgmUnlock(pVM);
2096 return rc;
2097}
2098
2099
2100/**
2101 * @callback_method_impl{FNSSMINTSAVEDONE}
2102 */
2103static DECLCALLBACK(int) pgmR3SaveDone(PVM pVM, PSSMHANDLE pSSM)
2104{
2105 /*
2106 * Do per page type cleanups first.
2107 */
2108 if (pVM->pgm.s.LiveSave.fActive)
2109 {
2110 pgmR3DoneRomPages(pVM);
2111 pgmR3DoneMmio2Pages(pVM);
2112 pgmR3DoneRamPages(pVM);
2113 }
2114
2115 /*
2116 * Clear the live save indicator and disengage write monitoring.
2117 */
2118 pgmLock(pVM);
2119 pVM->pgm.s.LiveSave.fActive = false;
2120 /** @todo this is blindly assuming that we're the only user of write
2121 * monitoring. Fix this when more users are added. */
2122 pVM->pgm.s.fPhysWriteMonitoringEngaged = false;
2123 pgmUnlock(pVM);
2124
2125 NOREF(pSSM);
2126 return VINF_SUCCESS;
2127}
2128
2129
2130/**
2131 * @callback_method_impl{FNSSMINTLOADPREP}
2132 */
2133static DECLCALLBACK(int) pgmR3LoadPrep(PVM pVM, PSSMHANDLE pSSM)
2134{
2135 /*
2136 * Call the reset function to make sure all the memory is cleared.
2137 */
2138 PGMR3Reset(pVM);
2139 pVM->pgm.s.LiveSave.fActive = false;
2140 NOREF(pSSM);
2141 return VINF_SUCCESS;
2142}
2143
2144
2145/**
2146 * Load an ignored page.
2147 *
2148 * @returns VBox status code.
2149 * @param pSSM The saved state handle.
2150 */
2151static int pgmR3LoadPageToDevNullOld(PSSMHANDLE pSSM)
2152{
2153 uint8_t abPage[PAGE_SIZE];
2154 return SSMR3GetMem(pSSM, &abPage[0], sizeof(abPage));
2155}
2156
2157
2158/**
2159 * Compares a page with an old save type value.
2160 *
2161 * @returns true if equal, false if not.
2162 * @param pPage The page to compare.
2163 * @param uOldType The old type value from the saved state.
2164 */
2165DECLINLINE(bool) pgmR3CompareNewAndOldPageTypes(PPGMPAGE pPage, uint8_t uOldType)
2166{
2167 uint8_t uOldPageType;
2168 switch (PGM_PAGE_GET_TYPE(pPage))
2169 {
2170 case PGMPAGETYPE_INVALID: uOldPageType = PGMPAGETYPE_OLD_INVALID; break;
2171 case PGMPAGETYPE_RAM: uOldPageType = PGMPAGETYPE_OLD_RAM; break;
2172 case PGMPAGETYPE_MMIO2: uOldPageType = PGMPAGETYPE_OLD_MMIO2; break;
2173 case PGMPAGETYPE_MMIO2_ALIAS_MMIO: uOldPageType = PGMPAGETYPE_OLD_MMIO2_ALIAS_MMIO; break;
2174 case PGMPAGETYPE_ROM_SHADOW: uOldPageType = PGMPAGETYPE_OLD_ROM_SHADOW; break;
2175 case PGMPAGETYPE_ROM: uOldPageType = PGMPAGETYPE_OLD_ROM; break;
2176 case PGMPAGETYPE_SPECIAL_ALIAS_MMIO: RT_FALL_THRU();
2177 case PGMPAGETYPE_MMIO: uOldPageType = PGMPAGETYPE_OLD_MMIO; break;
2178 default:
2179 AssertFailed();
2180 uOldPageType = PGMPAGETYPE_OLD_INVALID;
2181 break;
2182 }
2183 return uOldPageType == uOldType;
2184}
2185
2186
2187/**
2188 * Loads a page without any bits in the saved state, i.e. making sure it's
2189 * really zero.
2190 *
2191 * @returns VBox status code.
2192 * @param pVM The cross context VM structure.
2193 * @param uOldType The page type or PGMPAGETYPE_OLD_INVALID (old saved
2194 * state).
2195 * @param pPage The guest page tracking structure.
2196 * @param GCPhys The page address.
2197 * @param pRam The ram range (logging).
2198 */
2199static int pgmR3LoadPageZeroOld(PVM pVM, uint8_t uOldType, PPGMPAGE pPage, RTGCPHYS GCPhys, PPGMRAMRANGE pRam)
2200{
2201 if ( uOldType != PGMPAGETYPE_OLD_INVALID
2202 && !pgmR3CompareNewAndOldPageTypes(pPage, uOldType))
2203 return VERR_SSM_UNEXPECTED_DATA;
2204
2205 /* I think this should be sufficient. */
2206 if ( !PGM_PAGE_IS_ZERO(pPage)
2207 && !PGM_PAGE_IS_BALLOONED(pPage))
2208 return VERR_SSM_UNEXPECTED_DATA;
2209
2210 NOREF(pVM);
2211 NOREF(GCPhys);
2212 NOREF(pRam);
2213 return VINF_SUCCESS;
2214}
2215
2216
2217/**
2218 * Loads a page from the saved state.
2219 *
2220 * @returns VBox status code.
2221 * @param pVM The cross context VM structure.
2222 * @param pSSM The SSM handle.
2223 * @param uOldType The page type or PGMPAGETYPE_OLD_INVALID (old saved
2224 * state).
2225 * @param pPage The guest page tracking structure.
2226 * @param GCPhys The page address.
2227 * @param pRam The ram range (logging).
2228 */
2229static int pgmR3LoadPageBitsOld(PVM pVM, PSSMHANDLE pSSM, uint8_t uOldType, PPGMPAGE pPage, RTGCPHYS GCPhys, PPGMRAMRANGE pRam)
2230{
2231 /*
2232 * Match up the type, dealing with MMIO2 aliases (dropped).
2233 */
2234 AssertLogRelMsgReturn( uOldType == PGMPAGETYPE_INVALID
2235 || pgmR3CompareNewAndOldPageTypes(pPage, uOldType)
2236 /* kudge for the expanded PXE bios (r67885) - @bugref{5687}: */
2237 || ( uOldType == PGMPAGETYPE_OLD_RAM
2238 && GCPhys >= 0xed000
2239 && GCPhys <= 0xeffff
2240 && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_ROM)
2241 ,
2242 ("pPage=%R[pgmpage] GCPhys=%#x %s\n", pPage, GCPhys, pRam->pszDesc),
2243 VERR_SSM_UNEXPECTED_DATA);
2244
2245 /*
2246 * Load the page.
2247 */
2248 PGMPAGEMAPLOCK PgMpLck;
2249 void *pvPage;
2250 int rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvPage, &PgMpLck);
2251 if (RT_SUCCESS(rc))
2252 {
2253 rc = SSMR3GetMem(pSSM, pvPage, PAGE_SIZE);
2254 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2255 }
2256
2257 return rc;
2258}
2259
2260
2261/**
2262 * Loads a page (counter part to pgmR3SavePage).
2263 *
2264 * @returns VBox status code, fully bitched errors.
2265 * @param pVM The cross context VM structure.
2266 * @param pSSM The SSM handle.
2267 * @param uOldType The page type.
2268 * @param pPage The page.
2269 * @param GCPhys The page address.
2270 * @param pRam The RAM range (for error messages).
2271 */
2272static int pgmR3LoadPageOld(PVM pVM, PSSMHANDLE pSSM, uint8_t uOldType, PPGMPAGE pPage, RTGCPHYS GCPhys, PPGMRAMRANGE pRam)
2273{
2274 uint8_t uState;
2275 int rc = SSMR3GetU8(pSSM, &uState);
2276 AssertLogRelMsgRCReturn(rc, ("pPage=%R[pgmpage] GCPhys=%#x %s rc=%Rrc\n", pPage, GCPhys, pRam->pszDesc, rc), rc);
2277 if (uState == 0 /* zero */)
2278 rc = pgmR3LoadPageZeroOld(pVM, uOldType, pPage, GCPhys, pRam);
2279 else if (uState == 1)
2280 rc = pgmR3LoadPageBitsOld(pVM, pSSM, uOldType, pPage, GCPhys, pRam);
2281 else
2282 rc = VERR_PGM_INVALID_SAVED_PAGE_STATE;
2283 AssertLogRelMsgRCReturn(rc, ("pPage=%R[pgmpage] uState=%d uOldType=%d GCPhys=%RGp %s rc=%Rrc\n",
2284 pPage, uState, uOldType, GCPhys, pRam->pszDesc, rc),
2285 rc);
2286 return VINF_SUCCESS;
2287}
2288
2289
2290/**
2291 * Loads a shadowed ROM page.
2292 *
2293 * @returns VBox status code, errors are fully bitched.
2294 * @param pVM The cross context VM structure.
2295 * @param pSSM The saved state handle.
2296 * @param pPage The page.
2297 * @param GCPhys The page address.
2298 * @param pRam The RAM range (for error messages).
2299 */
2300static int pgmR3LoadShadowedRomPageOld(PVM pVM, PSSMHANDLE pSSM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPGMRAMRANGE pRam)
2301{
2302 /*
2303 * Load and set the protection first, then load the two pages, the first
2304 * one is the active the other is the passive.
2305 */
2306 PPGMROMPAGE pRomPage = pgmR3GetRomPage(pVM, GCPhys);
2307 AssertLogRelMsgReturn(pRomPage, ("GCPhys=%RGp %s\n", GCPhys, pRam->pszDesc), VERR_PGM_SAVED_ROM_PAGE_NOT_FOUND);
2308
2309 uint8_t uProt;
2310 int rc = SSMR3GetU8(pSSM, &uProt);
2311 AssertLogRelMsgRCReturn(rc, ("pPage=%R[pgmpage] GCPhys=%#x %s\n", pPage, GCPhys, pRam->pszDesc), rc);
2312 PGMROMPROT enmProt = (PGMROMPROT)uProt;
2313 AssertLogRelMsgReturn( enmProt >= PGMROMPROT_INVALID
2314 && enmProt < PGMROMPROT_END,
2315 ("enmProt=%d pPage=%R[pgmpage] GCPhys=%#x %s\n", enmProt, pPage, GCPhys, pRam->pszDesc),
2316 VERR_SSM_UNEXPECTED_DATA);
2317
2318 if (pRomPage->enmProt != enmProt)
2319 {
2320 rc = PGMR3PhysRomProtect(pVM, GCPhys, PAGE_SIZE, enmProt);
2321 AssertLogRelRCReturn(rc, rc);
2322 AssertLogRelReturn(pRomPage->enmProt == enmProt, VERR_PGM_SAVED_ROM_PAGE_PROT);
2323 }
2324
2325 PPGMPAGE pPageActive = PGMROMPROT_IS_ROM(enmProt) ? &pRomPage->Virgin : &pRomPage->Shadow;
2326 PPGMPAGE pPagePassive = PGMROMPROT_IS_ROM(enmProt) ? &pRomPage->Shadow : &pRomPage->Virgin;
2327 uint8_t u8ActiveType = PGMROMPROT_IS_ROM(enmProt) ? PGMPAGETYPE_ROM : PGMPAGETYPE_ROM_SHADOW;
2328 uint8_t u8PassiveType= PGMROMPROT_IS_ROM(enmProt) ? PGMPAGETYPE_ROM_SHADOW : PGMPAGETYPE_ROM;
2329
2330 /** @todo this isn't entirely correct as long as pgmPhysGCPhys2CCPtrInternal is
2331 * used down the line (will the 2nd page will be written to the first
2332 * one because of a false TLB hit since the TLB is using GCPhys and
2333 * doesn't check the HCPhys of the desired page). */
2334 rc = pgmR3LoadPageOld(pVM, pSSM, u8ActiveType, pPage, GCPhys, pRam);
2335 if (RT_SUCCESS(rc))
2336 {
2337 *pPageActive = *pPage;
2338 rc = pgmR3LoadPageOld(pVM, pSSM, u8PassiveType, pPagePassive, GCPhys, pRam);
2339 }
2340 return rc;
2341}
2342
2343/**
2344 * Ram range flags and bits for older versions of the saved state.
2345 *
2346 * @returns VBox status code.
2347 *
2348 * @param pVM The cross context VM structure.
2349 * @param pSSM The SSM handle.
2350 * @param uVersion The saved state version.
2351 */
2352static int pgmR3LoadMemoryOld(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion)
2353{
2354 PPGM pPGM = &pVM->pgm.s;
2355
2356 /*
2357 * Ram range flags and bits.
2358 */
2359 uint32_t i = 0;
2360 for (PPGMRAMRANGE pRam = pPGM->pRamRangesXR3; ; pRam = pRam->pNextR3, i++)
2361 {
2362 /* Check the sequence number / separator. */
2363 uint32_t u32Sep;
2364 int rc = SSMR3GetU32(pSSM, &u32Sep);
2365 if (RT_FAILURE(rc))
2366 return rc;
2367 if (u32Sep == ~0U)
2368 break;
2369 if (u32Sep != i)
2370 {
2371 AssertMsgFailed(("u32Sep=%#x (last)\n", u32Sep));
2372 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
2373 }
2374 AssertLogRelReturn(pRam, VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2375
2376 /* Get the range details. */
2377 RTGCPHYS GCPhys;
2378 SSMR3GetGCPhys(pSSM, &GCPhys);
2379 RTGCPHYS GCPhysLast;
2380 SSMR3GetGCPhys(pSSM, &GCPhysLast);
2381 RTGCPHYS cb;
2382 SSMR3GetGCPhys(pSSM, &cb);
2383 uint8_t fHaveBits;
2384 rc = SSMR3GetU8(pSSM, &fHaveBits);
2385 if (RT_FAILURE(rc))
2386 return rc;
2387 if (fHaveBits & ~1)
2388 {
2389 AssertMsgFailed(("u32Sep=%#x (last)\n", u32Sep));
2390 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
2391 }
2392 size_t cchDesc = 0;
2393 char szDesc[256];
2394 szDesc[0] = '\0';
2395 if (uVersion >= PGM_SAVED_STATE_VERSION_RR_DESC)
2396 {
2397 rc = SSMR3GetStrZ(pSSM, szDesc, sizeof(szDesc));
2398 if (RT_FAILURE(rc))
2399 return rc;
2400 /* Since we've modified the description strings in r45878, only compare
2401 them if the saved state is more recent. */
2402 if (uVersion != PGM_SAVED_STATE_VERSION_RR_DESC)
2403 cchDesc = strlen(szDesc);
2404 }
2405
2406 /*
2407 * Match it up with the current range.
2408 *
2409 * Note there is a hack for dealing with the high BIOS mapping
2410 * in the old saved state format, this means we might not have
2411 * a 1:1 match on success.
2412 */
2413 if ( ( GCPhys != pRam->GCPhys
2414 || GCPhysLast != pRam->GCPhysLast
2415 || cb != pRam->cb
2416 || ( cchDesc
2417 && strcmp(szDesc, pRam->pszDesc)) )
2418 /* Hack for PDMDevHlpPhysReserve(pDevIns, 0xfff80000, 0x80000, "High ROM Region"); */
2419 && ( uVersion != PGM_SAVED_STATE_VERSION_OLD_PHYS_CODE
2420 || GCPhys != UINT32_C(0xfff80000)
2421 || GCPhysLast != UINT32_C(0xffffffff)
2422 || pRam->GCPhysLast != GCPhysLast
2423 || pRam->GCPhys < GCPhys
2424 || !fHaveBits)
2425 )
2426 {
2427 LogRel(("Ram range: %RGp-%RGp %RGp bytes %s %s\n"
2428 "State : %RGp-%RGp %RGp bytes %s %s\n",
2429 pRam->GCPhys, pRam->GCPhysLast, pRam->cb, pRam->pvR3 ? "bits" : "nobits", pRam->pszDesc,
2430 GCPhys, GCPhysLast, cb, fHaveBits ? "bits" : "nobits", szDesc));
2431 /*
2432 * If we're loading a state for debugging purpose, don't make a fuss if
2433 * the MMIO and ROM stuff isn't 100% right, just skip the mismatches.
2434 */
2435 if ( SSMR3HandleGetAfter(pSSM) != SSMAFTER_DEBUG_IT
2436 || GCPhys < 8 * _1M)
2437 return SSMR3SetCfgError(pSSM, RT_SRC_POS,
2438 N_("RAM range mismatch; saved={%RGp-%RGp %RGp bytes %s %s} config={%RGp-%RGp %RGp bytes %s %s}"),
2439 GCPhys, GCPhysLast, cb, fHaveBits ? "bits" : "nobits", szDesc,
2440 pRam->GCPhys, pRam->GCPhysLast, pRam->cb, pRam->pvR3 ? "bits" : "nobits", pRam->pszDesc);
2441
2442 AssertMsgFailed(("debug skipping not implemented, sorry\n"));
2443 continue;
2444 }
2445
2446 uint32_t cPages = (GCPhysLast - GCPhys + 1) >> PAGE_SHIFT;
2447 if (uVersion >= PGM_SAVED_STATE_VERSION_RR_DESC)
2448 {
2449 /*
2450 * Load the pages one by one.
2451 */
2452 for (uint32_t iPage = 0; iPage < cPages; iPage++)
2453 {
2454 RTGCPHYS const GCPhysPage = ((RTGCPHYS)iPage << PAGE_SHIFT) + pRam->GCPhys;
2455 PPGMPAGE pPage = &pRam->aPages[iPage];
2456 uint8_t uOldType;
2457 rc = SSMR3GetU8(pSSM, &uOldType);
2458 AssertLogRelMsgRCReturn(rc, ("pPage=%R[pgmpage] iPage=%#x GCPhysPage=%#x %s\n", pPage, iPage, GCPhysPage, pRam->pszDesc), rc);
2459 if (uOldType == PGMPAGETYPE_OLD_ROM_SHADOW)
2460 rc = pgmR3LoadShadowedRomPageOld(pVM, pSSM, pPage, GCPhysPage, pRam);
2461 else
2462 rc = pgmR3LoadPageOld(pVM, pSSM, uOldType, pPage, GCPhysPage, pRam);
2463 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc iPage=%#x GCPhysPage=%#x %s\n", rc, iPage, GCPhysPage, pRam->pszDesc), rc);
2464 }
2465 }
2466 else
2467 {
2468 /*
2469 * Old format.
2470 */
2471
2472 /* Of the page flags, pick up MMIO2 and ROM/RESERVED for the !fHaveBits case.
2473 The rest is generally irrelevant and wrong since the stuff have to match registrations. */
2474 uint32_t fFlags = 0;
2475 for (uint32_t iPage = 0; iPage < cPages; iPage++)
2476 {
2477 uint16_t u16Flags;
2478 rc = SSMR3GetU16(pSSM, &u16Flags);
2479 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc iPage=%#x GCPhys=%#x %s\n", rc, iPage, pRam->GCPhys, pRam->pszDesc), rc);
2480 fFlags |= u16Flags;
2481 }
2482
2483 /* Load the bits */
2484 if ( !fHaveBits
2485 && GCPhysLast < UINT32_C(0xe0000000))
2486 {
2487 /*
2488 * Dynamic chunks.
2489 */
2490 const uint32_t cPagesInChunk = (1*1024*1024) >> PAGE_SHIFT;
2491 AssertLogRelMsgReturn(cPages % cPagesInChunk == 0,
2492 ("cPages=%#x cPagesInChunk=%#x GCPhys=%RGp %s\n", cPages, cPagesInChunk, pRam->GCPhys, pRam->pszDesc),
2493 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2494
2495 for (uint32_t iPage = 0; iPage < cPages; /* incremented by inner loop */ )
2496 {
2497 uint8_t fPresent;
2498 rc = SSMR3GetU8(pSSM, &fPresent);
2499 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc iPage=%#x GCPhys=%#x %s\n", rc, iPage, pRam->GCPhys, pRam->pszDesc), rc);
2500 AssertLogRelMsgReturn(fPresent == (uint8_t)true || fPresent == (uint8_t)false,
2501 ("fPresent=%#x iPage=%#x GCPhys=%#x %s\n", fPresent, iPage, pRam->GCPhys, pRam->pszDesc),
2502 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2503
2504 for (uint32_t iChunkPage = 0; iChunkPage < cPagesInChunk; iChunkPage++, iPage++)
2505 {
2506 RTGCPHYS const GCPhysPage = ((RTGCPHYS)iPage << PAGE_SHIFT) + pRam->GCPhys;
2507 PPGMPAGE pPage = &pRam->aPages[iPage];
2508 if (fPresent)
2509 {
2510 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO
2511 || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO)
2512 rc = pgmR3LoadPageToDevNullOld(pSSM);
2513 else
2514 rc = pgmR3LoadPageBitsOld(pVM, pSSM, PGMPAGETYPE_INVALID, pPage, GCPhysPage, pRam);
2515 }
2516 else
2517 rc = pgmR3LoadPageZeroOld(pVM, PGMPAGETYPE_INVALID, pPage, GCPhysPage, pRam);
2518 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc iPage=%#x GCPhysPage=%#x %s\n", rc, iPage, GCPhysPage, pRam->pszDesc), rc);
2519 }
2520 }
2521 }
2522 else if (pRam->pvR3)
2523 {
2524 /*
2525 * MMIO2.
2526 */
2527 AssertLogRelMsgReturn((fFlags & 0x0f) == RT_BIT(3) /*MM_RAM_FLAGS_MMIO2*/,
2528 ("fFlags=%#x GCPhys=%#x %s\n", fFlags, pRam->GCPhys, pRam->pszDesc),
2529 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2530 AssertLogRelMsgReturn(pRam->pvR3,
2531 ("GCPhys=%#x %s\n", pRam->GCPhys, pRam->pszDesc),
2532 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2533
2534 rc = SSMR3GetMem(pSSM, pRam->pvR3, pRam->cb);
2535 AssertLogRelMsgRCReturn(rc, ("GCPhys=%#x %s\n", pRam->GCPhys, pRam->pszDesc), rc);
2536 }
2537 else if (GCPhysLast < UINT32_C(0xfff80000))
2538 {
2539 /*
2540 * PCI MMIO, no pages saved.
2541 */
2542 }
2543 else
2544 {
2545 /*
2546 * Load the 0xfff80000..0xffffffff BIOS range.
2547 * It starts with X reserved pages that we have to skip over since
2548 * the RAMRANGE create by the new code won't include those.
2549 */
2550 AssertLogRelMsgReturn( !(fFlags & RT_BIT(3) /*MM_RAM_FLAGS_MMIO2*/)
2551 && (fFlags & RT_BIT(0) /*MM_RAM_FLAGS_RESERVED*/),
2552 ("fFlags=%#x GCPhys=%#x %s\n", fFlags, pRam->GCPhys, pRam->pszDesc),
2553 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2554 AssertLogRelMsgReturn(GCPhys == UINT32_C(0xfff80000),
2555 ("GCPhys=%RGp pRamRange{GCPhys=%#x %s}\n", GCPhys, pRam->GCPhys, pRam->pszDesc),
2556 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2557
2558 /* Skip wasted reserved pages before the ROM. */
2559 while (GCPhys < pRam->GCPhys)
2560 {
2561 rc = pgmR3LoadPageToDevNullOld(pSSM);
2562 GCPhys += PAGE_SIZE;
2563 }
2564
2565 /* Load the bios pages. */
2566 cPages = pRam->cb >> PAGE_SHIFT;
2567 for (uint32_t iPage = 0; iPage < cPages; iPage++)
2568 {
2569 RTGCPHYS const GCPhysPage = ((RTGCPHYS)iPage << PAGE_SHIFT) + pRam->GCPhys;
2570 PPGMPAGE pPage = &pRam->aPages[iPage];
2571
2572 AssertLogRelMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_ROM,
2573 ("GCPhys=%RGp pPage=%R[pgmpage]\n", GCPhys, GCPhys),
2574 VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2575 rc = pgmR3LoadPageBitsOld(pVM, pSSM, PGMPAGETYPE_ROM, pPage, GCPhysPage, pRam);
2576 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc iPage=%#x GCPhys=%#x %s\n", rc, iPage, pRam->GCPhys, pRam->pszDesc), rc);
2577 }
2578 }
2579 }
2580 }
2581
2582 return VINF_SUCCESS;
2583}
2584
2585
2586/**
2587 * Worker for pgmR3Load and pgmR3LoadLocked.
2588 *
2589 * @returns VBox status code.
2590 *
2591 * @param pVM The cross context VM structure.
2592 * @param pSSM The SSM handle.
2593 * @param uVersion The PGM saved state unit version.
2594 * @param uPass The pass number.
2595 *
2596 * @todo This needs splitting up if more record types or code twists are
2597 * added...
2598 */
2599static int pgmR3LoadMemory(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2600{
2601 NOREF(uPass);
2602
2603 /*
2604 * Process page records until we hit the terminator.
2605 */
2606 RTGCPHYS GCPhys = NIL_RTGCPHYS;
2607 PPGMRAMRANGE pRamHint = NULL;
2608 uint8_t id = UINT8_MAX;
2609 uint32_t iPage = UINT32_MAX - 10;
2610 PPGMROMRANGE pRom = NULL;
2611 PPGMREGMMIO2RANGE pRegMmio = NULL;
2612
2613 /*
2614 * We batch up pages that should be freed instead of calling GMM for
2615 * each and every one of them. Note that we'll lose the pages in most
2616 * failure paths - this should probably be addressed one day.
2617 */
2618 uint32_t cPendingPages = 0;
2619 PGMMFREEPAGESREQ pReq;
2620 int rc = GMMR3FreePagesPrepare(pVM, &pReq, 128 /* batch size */, GMMACCOUNT_BASE);
2621 AssertLogRelRCReturn(rc, rc);
2622
2623 for (;;)
2624 {
2625 /*
2626 * Get the record type and flags.
2627 */
2628 uint8_t u8;
2629 rc = SSMR3GetU8(pSSM, &u8);
2630 if (RT_FAILURE(rc))
2631 return rc;
2632 if (u8 == PGM_STATE_REC_END)
2633 {
2634 /*
2635 * Finish off any pages pending freeing.
2636 */
2637 if (cPendingPages)
2638 {
2639 Log(("pgmR3LoadMemory: GMMR3FreePagesPerform pVM=%p cPendingPages=%u\n", pVM, cPendingPages));
2640 rc = GMMR3FreePagesPerform(pVM, pReq, cPendingPages);
2641 AssertLogRelRCReturn(rc, rc);
2642 }
2643 GMMR3FreePagesCleanup(pReq);
2644 return VINF_SUCCESS;
2645 }
2646 AssertLogRelMsgReturn((u8 & ~PGM_STATE_REC_FLAG_ADDR) <= PGM_STATE_REC_LAST, ("%#x\n", u8), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2647 switch (u8 & ~PGM_STATE_REC_FLAG_ADDR)
2648 {
2649 /*
2650 * RAM page.
2651 */
2652 case PGM_STATE_REC_RAM_ZERO:
2653 case PGM_STATE_REC_RAM_RAW:
2654 case PGM_STATE_REC_RAM_BALLOONED:
2655 {
2656 /*
2657 * Get the address and resolve it into a page descriptor.
2658 */
2659 if (!(u8 & PGM_STATE_REC_FLAG_ADDR))
2660 GCPhys += PAGE_SIZE;
2661 else
2662 {
2663 rc = SSMR3GetGCPhys(pSSM, &GCPhys);
2664 if (RT_FAILURE(rc))
2665 return rc;
2666 }
2667 AssertLogRelMsgReturn(!(GCPhys & PAGE_OFFSET_MASK), ("%RGp\n", GCPhys), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
2668
2669 PPGMPAGE pPage;
2670 rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
2671 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc %RGp\n", rc, GCPhys), rc);
2672
2673 /*
2674 * Take action according to the record type.
2675 */
2676 switch (u8 & ~PGM_STATE_REC_FLAG_ADDR)
2677 {
2678 case PGM_STATE_REC_RAM_ZERO:
2679 {
2680 if (PGM_PAGE_IS_ZERO(pPage))
2681 break;
2682
2683 /* Ballooned pages must be unmarked (live snapshot and
2684 teleportation scenarios). */
2685 if (PGM_PAGE_IS_BALLOONED(pPage))
2686 {
2687 Assert(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM);
2688 if (uVersion == PGM_SAVED_STATE_VERSION_BALLOON_BROKEN)
2689 break;
2690 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ZERO);
2691 break;
2692 }
2693
2694 AssertLogRelMsgReturn(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED, ("GCPhys=%RGp %R[pgmpage]\n", GCPhys, pPage), VERR_PGM_UNEXPECTED_PAGE_STATE);
2695
2696 /* If this is a ROM page, we must clear it and not try to
2697 * free it. Ditto if the VM is using RamPreAlloc (see
2698 * @bugref{6318}). */
2699 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_ROM
2700 || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_ROM_SHADOW
2701 || pVM->pgm.s.fRamPreAlloc)
2702 {
2703 PGMPAGEMAPLOCK PgMpLck;
2704 void *pvDstPage;
2705 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDstPage, &PgMpLck);
2706 AssertLogRelMsgRCReturn(rc, ("GCPhys=%RGp %R[pgmpage] rc=%Rrc\n", GCPhys, pPage, rc), rc);
2707
2708 ASMMemZeroPage(pvDstPage);
2709 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2710 }
2711 /* Free it only if it's not part of a previously
2712 allocated large page (no need to clear the page). */
2713 else if ( PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE
2714 && PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE_DISABLED)
2715 {
2716 rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, GCPhys, (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage));
2717 AssertRCReturn(rc, rc);
2718 }
2719 /** @todo handle large pages (see @bugref{5545}) */
2720 break;
2721 }
2722
2723 case PGM_STATE_REC_RAM_BALLOONED:
2724 {
2725 Assert(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM);
2726 if (PGM_PAGE_IS_BALLOONED(pPage))
2727 break;
2728
2729 /* We don't map ballooned pages in our shadow page tables, let's
2730 just free it if allocated and mark as ballooned. See @bugref{5515}. */
2731 if (PGM_PAGE_IS_ALLOCATED(pPage))
2732 {
2733 /** @todo handle large pages + ballooning when it works. (see @bugref{5515},
2734 * @bugref{5545}). */
2735 AssertLogRelMsgReturn( PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE
2736 && PGM_PAGE_GET_PDE_TYPE(pPage) != PGM_PAGE_PDE_TYPE_PDE_DISABLED,
2737 ("GCPhys=%RGp %R[pgmpage]\n", GCPhys, pPage), VERR_PGM_LOAD_UNEXPECTED_PAGE_TYPE);
2738
2739 rc = pgmPhysFreePage(pVM, pReq, &cPendingPages, pPage, GCPhys, (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage));
2740 AssertRCReturn(rc, rc);
2741 }
2742 Assert(PGM_PAGE_IS_ZERO(pPage));
2743 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_BALLOONED);
2744 break;
2745 }
2746
2747 case PGM_STATE_REC_RAM_RAW:
2748 {
2749 PGMPAGEMAPLOCK PgMpLck;
2750 void *pvDstPage;
2751 rc = pgmPhysGCPhys2CCPtrInternal(pVM, pPage, GCPhys, &pvDstPage, &PgMpLck);
2752 AssertLogRelMsgRCReturn(rc, ("GCPhys=%RGp %R[pgmpage] rc=%Rrc\n", GCPhys, pPage, rc), rc);
2753 rc = SSMR3GetMem(pSSM, pvDstPage, PAGE_SIZE);
2754 pgmPhysReleaseInternalPageMappingLock(pVM, &PgMpLck);
2755 if (RT_FAILURE(rc))
2756 return rc;
2757 break;
2758 }
2759
2760 default:
2761 AssertMsgFailedReturn(("%#x\n", u8), VERR_PGM_SAVED_REC_TYPE);
2762 }
2763 id = UINT8_MAX;
2764 break;
2765 }
2766
2767 /*
2768 * MMIO2 page.
2769 */
2770 case PGM_STATE_REC_MMIO2_RAW:
2771 case PGM_STATE_REC_MMIO2_ZERO:
2772 {
2773 /*
2774 * Get the ID + page number and resolved that into a MMIO2 page.
2775 */
2776 if (!(u8 & PGM_STATE_REC_FLAG_ADDR))
2777 iPage++;
2778 else
2779 {
2780 SSMR3GetU8(pSSM, &id);
2781 rc = SSMR3GetU32(pSSM, &iPage);
2782 if (RT_FAILURE(rc))
2783 return rc;
2784 }
2785 if ( !pRegMmio
2786 || pRegMmio->idSavedState != id)
2787 {
2788 for (pRegMmio = pVM->pgm.s.pRegMmioRangesR3; pRegMmio; pRegMmio = pRegMmio->pNextR3)
2789 if ( pRegMmio->idSavedState == id
2790 && (pRegMmio->fFlags & PGMREGMMIO2RANGE_F_MMIO2))
2791 break;
2792 AssertLogRelMsgReturn(pRegMmio, ("id=%#u iPage=%#x\n", id, iPage), VERR_PGM_SAVED_MMIO2_RANGE_NOT_FOUND);
2793 }
2794 AssertLogRelMsgReturn(iPage < (pRegMmio->RamRange.cb >> PAGE_SHIFT), ("iPage=%#x cb=%RGp %s\n", iPage, pRegMmio->RamRange.cb, pRegMmio->RamRange.pszDesc), VERR_PGM_SAVED_MMIO2_PAGE_NOT_FOUND);
2795 void *pvDstPage = (uint8_t *)pRegMmio->RamRange.pvR3 + ((size_t)iPage << PAGE_SHIFT);
2796
2797 /*
2798 * Load the page bits.
2799 */
2800 if ((u8 & ~PGM_STATE_REC_FLAG_ADDR) == PGM_STATE_REC_MMIO2_ZERO)
2801 ASMMemZeroPage(pvDstPage);
2802 else
2803 {
2804 rc = SSMR3GetMem(pSSM, pvDstPage, PAGE_SIZE);
2805 if (RT_FAILURE(rc))
2806 return rc;
2807 }
2808 GCPhys = NIL_RTGCPHYS;
2809 break;
2810 }
2811
2812 /*
2813 * ROM pages.
2814 */
2815 case PGM_STATE_REC_ROM_VIRGIN:
2816 case PGM_STATE_REC_ROM_SHW_RAW:
2817 case PGM_STATE_REC_ROM_SHW_ZERO:
2818 case PGM_STATE_REC_ROM_PROT:
2819 {
2820 /*
2821 * Get the ID + page number and resolved that into a ROM page descriptor.
2822 */
2823 if (!(u8 & PGM_STATE_REC_FLAG_ADDR))
2824 iPage++;
2825 else
2826 {
2827 SSMR3GetU8(pSSM, &id);
2828 rc = SSMR3GetU32(pSSM, &iPage);
2829 if (RT_FAILURE(rc))
2830 return rc;
2831 }
2832 if ( !pRom
2833 || pRom->idSavedState != id)
2834 {
2835 for (pRom = pVM->pgm.s.pRomRangesR3; pRom; pRom = pRom->pNextR3)
2836 if (pRom->idSavedState == id)
2837 break;
2838 AssertLogRelMsgReturn(pRom, ("id=%#u iPage=%#x\n", id, iPage), VERR_PGM_SAVED_ROM_RANGE_NOT_FOUND);
2839 }
2840 AssertLogRelMsgReturn(iPage < (pRom->cb >> PAGE_SHIFT), ("iPage=%#x cb=%RGp %s\n", iPage, pRom->cb, pRom->pszDesc), VERR_PGM_SAVED_ROM_PAGE_NOT_FOUND);
2841 PPGMROMPAGE pRomPage = &pRom->aPages[iPage];
2842 GCPhys = pRom->GCPhys + ((RTGCPHYS)iPage << PAGE_SHIFT);
2843
2844 /*
2845 * Get and set the protection.
2846 */
2847 uint8_t u8Prot;
2848 rc = SSMR3GetU8(pSSM, &u8Prot);
2849 if (RT_FAILURE(rc))
2850 return rc;
2851 PGMROMPROT enmProt = (PGMROMPROT)u8Prot;
2852 AssertLogRelMsgReturn(enmProt > PGMROMPROT_INVALID && enmProt < PGMROMPROT_END, ("GCPhys=%RGp enmProt=%d\n", GCPhys, enmProt), VERR_PGM_SAVED_ROM_PAGE_PROT);
2853
2854 if (enmProt != pRomPage->enmProt)
2855 {
2856 if (RT_UNLIKELY(!(pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)))
2857 return SSMR3SetCfgError(pSSM, RT_SRC_POS,
2858 N_("Protection change of unshadowed ROM page: GCPhys=%RGp enmProt=%d %s"),
2859 GCPhys, enmProt, pRom->pszDesc);
2860 rc = PGMR3PhysRomProtect(pVM, GCPhys, PAGE_SIZE, enmProt);
2861 AssertLogRelMsgRCReturn(rc, ("GCPhys=%RGp rc=%Rrc\n", GCPhys, rc), rc);
2862 AssertLogRelReturn(pRomPage->enmProt == enmProt, VERR_PGM_SAVED_ROM_PAGE_PROT);
2863 }
2864 if ((u8 & ~PGM_STATE_REC_FLAG_ADDR) == PGM_STATE_REC_ROM_PROT)
2865 break; /* done */
2866
2867 /*
2868 * Get the right page descriptor.
2869 */
2870 PPGMPAGE pRealPage;
2871 switch (u8 & ~PGM_STATE_REC_FLAG_ADDR)
2872 {
2873 case PGM_STATE_REC_ROM_VIRGIN:
2874 if (!PGMROMPROT_IS_ROM(enmProt))
2875 pRealPage = &pRomPage->Virgin;
2876 else
2877 pRealPage = NULL;
2878 break;
2879
2880 case PGM_STATE_REC_ROM_SHW_RAW:
2881 case PGM_STATE_REC_ROM_SHW_ZERO:
2882 if (RT_UNLIKELY(!(pRom->fFlags & PGMPHYS_ROM_FLAGS_SHADOWED)))
2883 return SSMR3SetCfgError(pSSM, RT_SRC_POS,
2884 N_("Shadowed / non-shadowed page type mismatch: GCPhys=%RGp enmProt=%d %s"),
2885 GCPhys, enmProt, pRom->pszDesc);
2886 if (PGMROMPROT_IS_ROM(enmProt))
2887 pRealPage = &pRomPage->Shadow;
2888 else
2889 pRealPage = NULL;
2890 break;
2891
2892 default: AssertLogRelFailedReturn(VERR_IPE_NOT_REACHED_DEFAULT_CASE); /* shut up gcc */
2893 }
2894 if (!pRealPage)
2895 {
2896 rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pRealPage, &pRamHint);
2897 AssertLogRelMsgRCReturn(rc, ("rc=%Rrc %RGp\n", rc, GCPhys), rc);
2898 }
2899
2900 /*
2901 * Make it writable and map it (if necessary).
2902 */
2903 void *pvDstPage = NULL;
2904 switch (u8 & ~PGM_STATE_REC_FLAG_ADDR)
2905 {
2906 case PGM_STATE_REC_ROM_SHW_ZERO:
2907 if ( PGM_PAGE_IS_ZERO(pRealPage)
2908 || PGM_PAGE_IS_BALLOONED(pRealPage))
2909 break;
2910 /** @todo implement zero page replacing. */
2911 RT_FALL_THRU();
2912 case PGM_STATE_REC_ROM_VIRGIN:
2913 case PGM_STATE_REC_ROM_SHW_RAW:
2914 {
2915 rc = pgmPhysPageMakeWritableAndMap(pVM, pRealPage, GCPhys, &pvDstPage);
2916 AssertLogRelMsgRCReturn(rc, ("GCPhys=%RGp rc=%Rrc\n", GCPhys, rc), rc);
2917 break;
2918 }
2919 }
2920
2921 /*
2922 * Load the bits.
2923 */
2924 switch (u8 & ~PGM_STATE_REC_FLAG_ADDR)
2925 {
2926 case PGM_STATE_REC_ROM_SHW_ZERO:
2927 if (pvDstPage)
2928 ASMMemZeroPage(pvDstPage);
2929 break;
2930
2931 case PGM_STATE_REC_ROM_VIRGIN:
2932 case PGM_STATE_REC_ROM_SHW_RAW:
2933 rc = SSMR3GetMem(pSSM, pvDstPage, PAGE_SIZE);
2934 if (RT_FAILURE(rc))
2935 return rc;
2936 break;
2937 }
2938 GCPhys = NIL_RTGCPHYS;
2939 break;
2940 }
2941
2942 /*
2943 * Unknown type.
2944 */
2945 default:
2946 AssertLogRelMsgFailedReturn(("%#x\n", u8), VERR_PGM_SAVED_REC_TYPE);
2947 }
2948 } /* forever */
2949}
2950
2951
2952/**
2953 * Worker for pgmR3Load.
2954 *
2955 * @returns VBox status code.
2956 *
2957 * @param pVM The cross context VM structure.
2958 * @param pSSM The SSM handle.
2959 * @param uVersion The saved state version.
2960 */
2961static int pgmR3LoadFinalLocked(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion)
2962{
2963 PPGM pPGM = &pVM->pgm.s;
2964 int rc;
2965 uint32_t u32Sep;
2966
2967 /*
2968 * Load basic data (required / unaffected by relocation).
2969 */
2970 if (uVersion >= PGM_SAVED_STATE_VERSION_3_0_0)
2971 {
2972 if (uVersion > PGM_SAVED_STATE_VERSION_PRE_BALLOON)
2973 rc = SSMR3GetStruct(pSSM, pPGM, &s_aPGMFields[0]);
2974 else
2975 rc = SSMR3GetStruct(pSSM, pPGM, &s_aPGMFieldsPreBalloon[0]);
2976
2977 AssertLogRelRCReturn(rc, rc);
2978
2979 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2980 {
2981 if (uVersion <= PGM_SAVED_STATE_VERSION_PRE_PAE)
2982 rc = SSMR3GetStruct(pSSM, &pVM->apCpusR3[i]->pgm.s, &s_aPGMCpuFieldsPrePae[0]);
2983 else
2984 rc = SSMR3GetStruct(pSSM, &pVM->apCpusR3[i]->pgm.s, &s_aPGMCpuFields[0]);
2985 AssertLogRelRCReturn(rc, rc);
2986 }
2987 }
2988 else if (uVersion >= PGM_SAVED_STATE_VERSION_RR_DESC)
2989 {
2990 AssertRelease(pVM->cCpus == 1);
2991
2992 PGMOLD pgmOld;
2993 rc = SSMR3GetStruct(pSSM, &pgmOld, &s_aPGMFields_Old[0]);
2994 AssertLogRelRCReturn(rc, rc);
2995
2996 pPGM->fMappingsFixed = pgmOld.fMappingsFixed;
2997 pPGM->GCPtrMappingFixed = pgmOld.GCPtrMappingFixed;
2998 pPGM->cbMappingFixed = pgmOld.cbMappingFixed;
2999
3000 PVMCPU pVCpu0 = pVM->apCpusR3[0];
3001 pVCpu0->pgm.s.fA20Enabled = pgmOld.fA20Enabled;
3002 pVCpu0->pgm.s.GCPhysA20Mask = pgmOld.GCPhysA20Mask;
3003 pVCpu0->pgm.s.enmGuestMode = pgmOld.enmGuestMode;
3004 }
3005 else
3006 {
3007 AssertRelease(pVM->cCpus == 1);
3008
3009 SSMR3GetBool(pSSM, &pPGM->fMappingsFixed);
3010 SSMR3GetGCPtr(pSSM, &pPGM->GCPtrMappingFixed);
3011 SSMR3GetU32(pSSM, &pPGM->cbMappingFixed);
3012
3013 uint32_t cbRamSizeIgnored;
3014 rc = SSMR3GetU32(pSSM, &cbRamSizeIgnored);
3015 if (RT_FAILURE(rc))
3016 return rc;
3017 PVMCPU pVCpu0 = pVM->apCpusR3[0];
3018 SSMR3GetGCPhys(pSSM, &pVCpu0->pgm.s.GCPhysA20Mask);
3019
3020 uint32_t u32 = 0;
3021 SSMR3GetUInt(pSSM, &u32);
3022 pVCpu0->pgm.s.fA20Enabled = !!u32;
3023 SSMR3GetUInt(pSSM, &pVCpu0->pgm.s.fSyncFlags);
3024 RTUINT uGuestMode;
3025 SSMR3GetUInt(pSSM, &uGuestMode);
3026 pVCpu0->pgm.s.enmGuestMode = (PGMMODE)uGuestMode;
3027
3028 /* check separator. */
3029 SSMR3GetU32(pSSM, &u32Sep);
3030 if (RT_FAILURE(rc))
3031 return rc;
3032 if (u32Sep != (uint32_t)~0)
3033 {
3034 AssertMsgFailed(("u32Sep=%#x (first)\n", u32Sep));
3035 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
3036 }
3037 }
3038
3039 /*
3040 * Fix the A20 mask.
3041 */
3042 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3043 {
3044 PVMCPU pVCpu = pVM->apCpusR3[i];
3045 pVCpu->pgm.s.GCPhysA20Mask = ~((RTGCPHYS)!pVCpu->pgm.s.fA20Enabled << 20);
3046 pgmR3RefreshShadowModeAfterA20Change(pVCpu);
3047 }
3048
3049 /*
3050 * The guest mappings - skipped now, see re-fixation in the caller.
3051 */
3052 if (uVersion <= PGM_SAVED_STATE_VERSION_PRE_PAE)
3053 {
3054 for (uint32_t i = 0; ; i++)
3055 {
3056 rc = SSMR3GetU32(pSSM, &u32Sep); /* sequence number */
3057 if (RT_FAILURE(rc))
3058 return rc;
3059 if (u32Sep == ~0U)
3060 break;
3061 AssertMsgReturn(u32Sep == i, ("u32Sep=%#x i=%#x\n", u32Sep, i), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
3062
3063 char szDesc[256];
3064 rc = SSMR3GetStrZ(pSSM, szDesc, sizeof(szDesc));
3065 if (RT_FAILURE(rc))
3066 return rc;
3067 RTGCPTR GCPtrIgnore;
3068 SSMR3GetGCPtr(pSSM, &GCPtrIgnore); /* GCPtr */
3069 rc = SSMR3GetGCPtr(pSSM, &GCPtrIgnore); /* cPTs */
3070 if (RT_FAILURE(rc))
3071 return rc;
3072 }
3073 }
3074
3075 /*
3076 * Load the RAM contents.
3077 */
3078 if (uVersion > PGM_SAVED_STATE_VERSION_3_0_0)
3079 {
3080 if (!pVM->pgm.s.LiveSave.fActive)
3081 {
3082 if (uVersion > PGM_SAVED_STATE_VERSION_NO_RAM_CFG)
3083 {
3084 rc = pgmR3LoadRamConfig(pVM, pSSM);
3085 if (RT_FAILURE(rc))
3086 return rc;
3087 }
3088 rc = pgmR3LoadRomRanges(pVM, pSSM);
3089 if (RT_FAILURE(rc))
3090 return rc;
3091 rc = pgmR3LoadMmio2Ranges(pVM, pSSM);
3092 if (RT_FAILURE(rc))
3093 return rc;
3094 }
3095
3096 rc = pgmR3LoadMemory(pVM, pSSM, uVersion, SSM_PASS_FINAL);
3097 }
3098 else
3099 rc = pgmR3LoadMemoryOld(pVM, pSSM, uVersion);
3100
3101 /* Refresh balloon accounting. */
3102 if (pVM->pgm.s.cBalloonedPages)
3103 {
3104 Log(("pgmR3LoadFinalLocked: pVM=%p cBalloonedPages=%#x\n", pVM, pVM->pgm.s.cBalloonedPages));
3105 rc = GMMR3BalloonedPages(pVM, GMMBALLOONACTION_INFLATE, pVM->pgm.s.cBalloonedPages);
3106 AssertRCReturn(rc, rc);
3107 }
3108 return rc;
3109}
3110
3111
3112/**
3113 * @callback_method_impl{FNSSMINTLOADEXEC}
3114 */
3115static DECLCALLBACK(int) pgmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3116{
3117 int rc;
3118
3119 /*
3120 * Validate version.
3121 */
3122 if ( ( uPass != SSM_PASS_FINAL
3123 && uVersion != PGM_SAVED_STATE_VERSION
3124 && uVersion != PGM_SAVED_STATE_VERSION_PRE_PAE
3125 && uVersion != PGM_SAVED_STATE_VERSION_BALLOON_BROKEN
3126 && uVersion != PGM_SAVED_STATE_VERSION_PRE_BALLOON
3127 && uVersion != PGM_SAVED_STATE_VERSION_NO_RAM_CFG)
3128 || ( uVersion != PGM_SAVED_STATE_VERSION
3129 && uVersion != PGM_SAVED_STATE_VERSION_PRE_PAE
3130 && uVersion != PGM_SAVED_STATE_VERSION_BALLOON_BROKEN
3131 && uVersion != PGM_SAVED_STATE_VERSION_PRE_BALLOON
3132 && uVersion != PGM_SAVED_STATE_VERSION_NO_RAM_CFG
3133 && uVersion != PGM_SAVED_STATE_VERSION_3_0_0
3134 && uVersion != PGM_SAVED_STATE_VERSION_2_2_2
3135 && uVersion != PGM_SAVED_STATE_VERSION_RR_DESC
3136 && uVersion != PGM_SAVED_STATE_VERSION_OLD_PHYS_CODE)
3137 )
3138 {
3139 AssertMsgFailed(("pgmR3Load: Invalid version uVersion=%d (current %d)!\n", uVersion, PGM_SAVED_STATE_VERSION));
3140 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3141 }
3142
3143 /*
3144 * Do the loading while owning the lock because a bunch of the functions
3145 * we're using requires this.
3146 */
3147 if (uPass != SSM_PASS_FINAL)
3148 {
3149 pgmLock(pVM);
3150 if (uPass != 0)
3151 rc = pgmR3LoadMemory(pVM, pSSM, uVersion, uPass);
3152 else
3153 {
3154 pVM->pgm.s.LiveSave.fActive = true;
3155 if (uVersion > PGM_SAVED_STATE_VERSION_NO_RAM_CFG)
3156 rc = pgmR3LoadRamConfig(pVM, pSSM);
3157 else
3158 rc = VINF_SUCCESS;
3159 if (RT_SUCCESS(rc))
3160 rc = pgmR3LoadRomRanges(pVM, pSSM);
3161 if (RT_SUCCESS(rc))
3162 rc = pgmR3LoadMmio2Ranges(pVM, pSSM);
3163 if (RT_SUCCESS(rc))
3164 rc = pgmR3LoadMemory(pVM, pSSM, uVersion, uPass);
3165 }
3166 pgmUnlock(pVM);
3167 }
3168 else
3169 {
3170 pgmLock(pVM);
3171 rc = pgmR3LoadFinalLocked(pVM, pSSM, uVersion);
3172 pVM->pgm.s.LiveSave.fActive = false;
3173 pgmUnlock(pVM);
3174 if (RT_SUCCESS(rc))
3175 {
3176 /*
3177 * We require a full resync now.
3178 */
3179 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3180 {
3181 PVMCPU pVCpu = pVM->apCpusR3[i];
3182 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
3183 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
3184 /** @todo For guest PAE, we might get the wrong
3185 * aGCPhysGstPaePDs values now. We should used the
3186 * saved ones... Postponing this since it nothing new
3187 * and PAE/PDPTR needs some general readjusting, see
3188 * @bugref{5880}. */
3189 }
3190
3191 pgmR3HandlerPhysicalUpdateAll(pVM);
3192
3193 /*
3194 * Change the paging mode (indirectly restores PGMCPU::GCPhysCR3).
3195 * (Requires the CPUM state to be restored already!)
3196 */
3197 if (CPUMR3IsStateRestorePending(pVM))
3198 return SSMR3SetLoadError(pSSM, VERR_WRONG_ORDER, RT_SRC_POS,
3199 N_("PGM was unexpectedly restored before CPUM"));
3200
3201 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3202 {
3203 PVMCPU pVCpu = pVM->apCpusR3[i];
3204
3205 rc = PGMHCChangeMode(pVM, pVCpu, pVCpu->pgm.s.enmGuestMode);
3206 AssertLogRelRCReturn(rc, rc);
3207
3208 /* Update the PSE, NX flags and validity masks. */
3209 pVCpu->pgm.s.fGst32BitPageSizeExtension = CPUMIsGuestPageSizeExtEnabled(pVCpu);
3210 PGMNotifyNxeChanged(pVCpu, CPUMIsGuestNXEnabled(pVCpu));
3211 }
3212
3213 /*
3214 * Try re-fixate the guest mappings.
3215 */
3216 pVM->pgm.s.fMappingsFixedRestored = false;
3217 if ( pVM->pgm.s.fMappingsFixed
3218 && pgmMapAreMappingsEnabled(pVM))
3219 {
3220#ifndef PGM_WITHOUT_MAPPINGS
3221 RTGCPTR GCPtrFixed = pVM->pgm.s.GCPtrMappingFixed;
3222 uint32_t cbFixed = pVM->pgm.s.cbMappingFixed;
3223 pVM->pgm.s.fMappingsFixed = false;
3224
3225 uint32_t cbRequired;
3226 int rc2 = PGMR3MappingsSize(pVM, &cbRequired); AssertRC(rc2);
3227 if ( RT_SUCCESS(rc2)
3228 && cbRequired > cbFixed)
3229 rc2 = VERR_OUT_OF_RANGE;
3230 if (RT_SUCCESS(rc2))
3231 rc2 = pgmR3MappingsFixInternal(pVM, GCPtrFixed, cbFixed);
3232 if (RT_FAILURE(rc2))
3233 {
3234 LogRel(("PGM: Unable to re-fixate the guest mappings at %RGv-%RGv: rc=%Rrc (cbRequired=%#x)\n",
3235 GCPtrFixed, GCPtrFixed + cbFixed, rc2, cbRequired));
3236 pVM->pgm.s.fMappingsFixed = false;
3237 pVM->pgm.s.fMappingsFixedRestored = true;
3238 pVM->pgm.s.GCPtrMappingFixed = GCPtrFixed;
3239 pVM->pgm.s.cbMappingFixed = cbFixed;
3240 }
3241#else
3242 AssertFailed();
3243#endif
3244 }
3245 else
3246 {
3247 /* We used to set fixed + disabled while we only use disabled now,
3248 so wipe the state to avoid any confusion. */
3249 pVM->pgm.s.fMappingsFixed = false;
3250 pVM->pgm.s.GCPtrMappingFixed = NIL_RTGCPTR;
3251 pVM->pgm.s.cbMappingFixed = 0;
3252 }
3253
3254 /*
3255 * If we have floating mappings, do a CR3 sync now to make sure the HMA
3256 * doesn't conflict with guest code / data and thereby cause trouble
3257 * when restoring other components like PATM.
3258 */
3259 if (pgmMapAreMappingsFloating(pVM))
3260 {
3261 PVMCPU pVCpu = pVM->apCpusR3[0];
3262 rc = PGMSyncCR3(pVCpu, CPUMGetGuestCR0(pVCpu), CPUMGetGuestCR3(pVCpu), CPUMGetGuestCR4(pVCpu), true);
3263 if (RT_FAILURE(rc))
3264 return SSMR3SetLoadError(pSSM, VERR_WRONG_ORDER, RT_SRC_POS,
3265 N_("PGMSyncCR3 failed unexpectedly with rc=%Rrc"), rc);
3266
3267 /* Make sure to re-sync before executing code. */
3268 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL);
3269 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
3270 }
3271 }
3272 }
3273
3274 return rc;
3275}
3276
3277
3278/**
3279 * @callback_method_impl{FNSSMINTLOADDONE}
3280 */
3281static DECLCALLBACK(int) pgmR3LoadDone(PVM pVM, PSSMHANDLE pSSM)
3282{
3283 pVM->pgm.s.fRestoreRomPagesOnReset = true;
3284 NOREF(pSSM);
3285 return VINF_SUCCESS;
3286}
3287
3288
3289/**
3290 * Registers the saved state callbacks with SSM.
3291 *
3292 * @returns VBox status code.
3293 * @param pVM The cross context VM structure.
3294 * @param cbRam The RAM size.
3295 */
3296int pgmR3InitSavedState(PVM pVM, uint64_t cbRam)
3297{
3298 return SSMR3RegisterInternal(pVM, "pgm", 1, PGM_SAVED_STATE_VERSION, (size_t)cbRam + sizeof(PGM),
3299 pgmR3LivePrep, pgmR3LiveExec, pgmR3LiveVote,
3300 NULL, pgmR3SaveExec, pgmR3SaveDone,
3301 pgmR3LoadPrep, pgmR3Load, pgmR3LoadDone);
3302}
3303
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