loader: store FW CFG ROM files in RAM

ROM files that are put in FW CFG are copied to guest ram, by BIOS, but
they are not backed by RAM so they don't get migrated.

Each time we change two bytes in such a ROM this breaks cross-version
migration: since we can migrate after BIOS has read the first byte but
before it has read the second one, getting an inconsistent state.

Future-proof this by creating, for each such ROM,
an MR serving as the backing store.
This MR is never mapped into guest memory, but it's registered
as RAM so it's migrated with the guest.

Naturally, this only helps for -M 1.7 and up, older machine types
will still have the cross-version migration bug.
Luckily the race window for the problem to trigger is very small,
which is also likely why we didn't notice the cross-version
migration bug in testing yet.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
This commit is contained in:
Michael S. Tsirkin 2013-08-19 17:26:55 +03:00
parent 0851c9f75c
commit 04920fc0fa
4 changed files with 51 additions and 3 deletions

View File

@ -54,6 +54,8 @@
#include <zlib.h> #include <zlib.h>
bool rom_file_in_ram = true;
static int roms_loaded; static int roms_loaded;
/* return the size or -1 if error */ /* return the size or -1 if error */
@ -576,6 +578,7 @@ struct Rom {
size_t datasize; size_t datasize;
uint8_t *data; uint8_t *data;
MemoryRegion *mr;
int isrom; int isrom;
char *fw_dir; char *fw_dir;
char *fw_file; char *fw_file;
@ -605,6 +608,21 @@ static void rom_insert(Rom *rom)
QTAILQ_INSERT_TAIL(&roms, rom, next); QTAILQ_INSERT_TAIL(&roms, rom, next);
} }
static void *rom_set_mr(Rom *rom, Object *owner, const char *name)
{
void *data;
rom->mr = g_malloc(sizeof(*rom->mr));
memory_region_init_ram(rom->mr, owner, name, rom->datasize);
memory_region_set_readonly(rom->mr, true);
vmstate_register_ram_global(rom->mr);
data = memory_region_get_ram_ptr(rom->mr);
memcpy(data, rom->data, rom->datasize);
return data;
}
int rom_add_file(const char *file, const char *fw_dir, int rom_add_file(const char *file, const char *fw_dir,
hwaddr addr, int32_t bootindex) hwaddr addr, int32_t bootindex)
{ {
@ -646,6 +664,7 @@ int rom_add_file(const char *file, const char *fw_dir,
if (rom->fw_file && fw_cfg) { if (rom->fw_file && fw_cfg) {
const char *basename; const char *basename;
char fw_file_name[56]; char fw_file_name[56];
void *data;
basename = strrchr(rom->fw_file, '/'); basename = strrchr(rom->fw_file, '/');
if (basename) { if (basename) {
@ -655,8 +674,15 @@ int rom_add_file(const char *file, const char *fw_dir,
} }
snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir, snprintf(fw_file_name, sizeof(fw_file_name), "%s/%s", rom->fw_dir,
basename); basename);
fw_cfg_add_file(fw_cfg, fw_file_name, rom->data, rom->romsize);
snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name); snprintf(devpath, sizeof(devpath), "/rom@%s", fw_file_name);
if (rom_file_in_ram) {
data = rom_set_mr(rom, OBJECT(fw_cfg), devpath);
} else {
data = rom->data;
}
fw_cfg_add_file(fw_cfg, fw_file_name, data, rom->romsize);
} else { } else {
snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr); snprintf(devpath, sizeof(devpath), "/rom@" TARGET_FMT_plx, addr);
} }
@ -731,7 +757,12 @@ static void rom_reset(void *unused)
if (rom->data == NULL) { if (rom->data == NULL) {
continue; continue;
} }
cpu_physical_memory_write_rom(rom->addr, rom->data, rom->datasize); if (rom->mr) {
void *host = memory_region_get_ram_ptr(rom->mr);
memcpy(host, rom->data, rom->datasize);
} else {
cpu_physical_memory_write_rom(rom->addr, rom->data, rom->datasize);
}
if (rom->isrom) { if (rom->isrom) {
/* rom needs to be written only once */ /* rom needs to be written only once */
g_free(rom->data); g_free(rom->data);
@ -781,6 +812,9 @@ static Rom *find_rom(hwaddr addr)
if (rom->fw_file) { if (rom->fw_file) {
continue; continue;
} }
if (rom->mr) {
continue;
}
if (rom->addr > addr) { if (rom->addr > addr) {
continue; continue;
} }
@ -808,6 +842,9 @@ int rom_copy(uint8_t *dest, hwaddr addr, size_t size)
if (rom->fw_file) { if (rom->fw_file) {
continue; continue;
} }
if (rom->mr) {
continue;
}
if (rom->addr + rom->romsize < addr) { if (rom->addr + rom->romsize < addr) {
continue; continue;
} }
@ -866,7 +903,13 @@ void do_info_roms(Monitor *mon, const QDict *qdict)
Rom *rom; Rom *rom;
QTAILQ_FOREACH(rom, &roms, next) { QTAILQ_FOREACH(rom, &roms, next) {
if (!rom->fw_file) { if (rom->mr) {
monitor_printf(mon, "%s"
" size=0x%06zx name=\"%s\"\n",
rom->mr->name,
rom->romsize,
rom->name);
} else if (!rom->fw_file) {
monitor_printf(mon, "addr=" TARGET_FMT_plx monitor_printf(mon, "addr=" TARGET_FMT_plx
" size=0x%06zx mem=%s name=\"%s\"\n", " size=0x%06zx mem=%s name=\"%s\"\n",
rom->addr, rom->romsize, rom->addr, rom->romsize,

View File

@ -25,6 +25,7 @@
#include <glib.h> #include <glib.h>
#include "hw/hw.h" #include "hw/hw.h"
#include "hw/loader.h"
#include "hw/i386/pc.h" #include "hw/i386/pc.h"
#include "hw/i386/apic.h" #include "hw/i386/apic.h"
#include "hw/pci/pci.h" #include "hw/pci/pci.h"
@ -252,6 +253,7 @@ static void pc_init_pci(QEMUMachineInitArgs *args)
static void pc_init_pci_1_6(QEMUMachineInitArgs *args) static void pc_init_pci_1_6(QEMUMachineInitArgs *args)
{ {
has_pci_info = false; has_pci_info = false;
rom_file_in_ram = false;
pc_init_pci(args); pc_init_pci(args);
} }

View File

@ -28,6 +28,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "hw/hw.h" #include "hw/hw.h"
#include "hw/loader.h"
#include "sysemu/arch_init.h" #include "sysemu/arch_init.h"
#include "hw/i2c/smbus.h" #include "hw/i2c/smbus.h"
#include "hw/boards.h" #include "hw/boards.h"
@ -221,6 +222,7 @@ static void pc_q35_init(QEMUMachineInitArgs *args)
static void pc_q35_init_1_6(QEMUMachineInitArgs *args) static void pc_q35_init_1_6(QEMUMachineInitArgs *args)
{ {
has_pci_info = false; has_pci_info = false;
rom_file_in_ram = false;
pc_q35_init(args); pc_q35_init(args);
} }

View File

@ -36,6 +36,7 @@ void pstrcpy_targphys(const char *name,
hwaddr dest, int buf_size, hwaddr dest, int buf_size,
const char *source); const char *source);
extern bool rom_file_in_ram;
int rom_add_file(const char *file, const char *fw_dir, int rom_add_file(const char *file, const char *fw_dir,
hwaddr addr, int32_t bootindex); hwaddr addr, int32_t bootindex);