VirtualBox

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

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

Main,VMM,VBoxManage: Added a parameter to IConsole::Teleport for specifying a max downtime and made PGM compare this against a rough estimate of the time it would take to deal with the rest of the pages.

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