pc: fix ACPI tables corruption after bridge hotplug
v2 cover pc: fix ACPI tables corruption after bridge hotplug
Patches [1,2] are fix itself suitable for stable and patch 4 is to make building procedure robust and not depend on table order, not suitable for stable.
Reproducer:
1: hotplug bridge using command:
device_add pci-bridge,chassis_nr=1
2: reset system from monitor:
system_reset
As result:
Windows doesn't boot and linux boots without ACPI due to not readble/corrupted ACPI tables
Igor Mammedov (4):
pc: acpi-build: make linker table dynamic
pc: acpi-build: place RSDT at fixed offset across reboots
pc: Add pc-[i440fx|q35]-2.3 machine type and pc_compat_2_2() function
pc: acpi-build: make RSDP table dynamic
hw/i386/acpi-build.c | 70 +++++++++++++++++++++++++++++++++++++++++----------- hw/i386/pc_piix.c | 33 ++++++++++++++++++++++--- hw/i386/pc_q35.c | 30 +++++++++++++++++++--- include/hw/i386/pc.h | 1 + 4 files changed, 112 insertions(+), 22 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/4 pc: acpi-build: make linker table dynamic
linker table is build only once, so if later during rebuild sizes of other ACPI tables change pointers will be patched incorrectly due to wrong offsets leading to tables corruption.
Fix is to rebuild linker table along with the rest of ACPI tables so that it would have correct offsets.
--- hw/i386/acpi-build.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
hw/i386/acpi-build.c +7 -1
@@ -1509,6 +1509,8 @@ struct AcpiBuildState {/* Copy of table in RAM (for patching). */ ram_addr_t table_ram;uint32_t table_size; +ram_addr_t linker_ram; +uint32_t linker_size;/* Is table patched? */ uint8_t patched;PcGuestInfo *guest_info;
@@ -1714,6 +1716,8 @@ static void acpi_build_update(void *build_opaque, uint32_t offset)assert (acpi_data_len (tables.table_data) == build_state->table_size);memcpy (qemu_get_ram_ptr (build_state->table_ram), tables.table_data->data, build_state->table_size); +memcpy (qemu_get_ram_ptr (build_state->linker_ram), tables.linker->data, + build_state->linker_size);cpu_physical_memory_set_dirty_range_nocode (build_state->table_ram, build_state->table_size);
@@ -1779,7 +1783,9 @@ void acpi_setup(PcGuestInfo *guest_info)assert (build_state->table_ram !=RAM_ADDR_MAX ); build_state->table_size =acpi_data_len (tables.table_data); -acpi_add_rom_blob (NULL , tables.linker,"etc/table-loader" ); + build_state->linker_ram =acpi_add_rom_blob (build_state, tables.linker, +"etc/table-loader" ); + build_state->linker_size =acpi_data_len (tables.linker);fw_cfg_add_file (guest_info->fw_cfg,ACPI_BUILD_TPMLOG_FILE , tables.tcpalog->data,acpi_data_len (tables.tcpalog));
Signature · 1 line
1.8.3.1
PATCH v2 2/4 pc: acpi-build: place RSDT at fixed offset across reboots
In reply to Igor MammedovFixing RSDT location allows pointer to RSDP be constant and we can keep RSDP file immutable for the whole instance lifetime.
Which makes this fix suitable for stable, since migration format doesn't change in addition it fixes old machine types as well without breaking compatibility.
--- hw/i386/acpi-build.c | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-)
hw/i386/acpi-build.c +32 -8
@@ -1431,16 +1431,23 @@ build_dsdt(GArray *table_data, GArray *linker, AcpiMiscInfo *misc) misc->dsdt_size, 1); } -/* Build final rsdt table */ -static void -build_rsdt (GArray *table_data,GArray *linker,GArray *table_offsets) +/* reserve space for RSDT */ +static void alloc_rsdt (GArray *table_data,int tables_count) { -AcpiRsdtDescriptorRev1 *rsdt;size_t rsdt_len; + + rsdt_len =sizeof (AcpiRsdtDescriptorRev1) +sizeof (uint32_t ) * tables_count; +acpi_data_push (table_data, rsdt_len); +} + +/* fill final rsdt table */ +static void fill_rsdt (GArray *table_data,GArray *linker, +unsigned rsdt_offset,GArray *table_offsets) +{int i; +AcpiRsdtDescriptorRev1 *rsdt = (void *)(table_data->data + rsdt_offset); +size_t rsdt_len =sizeof (*rsdt) +sizeof (uint32_t ) * table_offsets->len; - rsdt_len =sizeof (*rsdt) +sizeof (uint32_t ) * table_offsets->len; - rsdt =acpi_data_push (table_data, rsdt_len);memcpy (rsdt->table_offset_entry, table_offsets->data,sizeof (uint32_t ) * table_offsets->len);for (i =0 ; i < table_offsets->len; ++i) {
@@ -1562,6 +1569,7 @@ void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables)PcPciInfo pci;uint8_t *u;size_t aml_len =0 ; +int tables_count =0 ;acpi_get_cpu_info (&cpu);acpi_get_pm_info (&pm);
@@ -1594,6 +1602,23 @@ void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables)*/ aml_len += tables->table_data->len - dsdt; +/* preallocate RSDT at fixed position to keep its offset +* constant accorss reboots since FACS and DSDT are static. +* This way RSDP referencing it could be kept immutable. +*/ + tables_count =1 /* FADT */ +1 /* SSDT */ +1 /* MADT */ + + (misc.has_hpet ?1 :0 ) + + (misc.has_tpm ?2 :0 ) + + (guest_info->numa_nodes ?1 :0 ) + + (acpi_get_mcfg (&mcfg) ?1 :0 ) + + (acpi_has_iommu () ?1 :0 ); +for (u =acpi_table_first (); u; u =acpi_table_next (u)) { + tables_count++; + } + rsdt = tables->table_data->len; +alloc_rsdt (tables->table_data, tables_count); + +/* ACPI tables pointed to by RSDT */ acpi_add_table (table_offsets, tables->table_data);build_fadt (tables->table_data, tables->linker, &pm, facs, dsdt);
@@ -1640,8 +1665,7 @@ void acpi_build(PcGuestInfo *guest_info, AcpiBuildTables *tables) }/* RSDT is pointed to by RSDP */ - rsdt = tables->table_data->len; -build_rsdt (tables->table_data, tables->linker, table_offsets); +fill_rsdt (tables->table_data, tables->linker, rsdt, table_offsets);/* RSDP is in FSEG memory, so allocate it separately */ build_rsdp (tables->rsdp, tables->linker, rsdt);
Signature · 1 line
1.8.3.1
PATCH v2 3/4 pc: Add pc-[i440fx|q35]-2.3 machine type and pc_compat_2_2() function
In reply to Igor Mammedov--- hw/i386/pc_piix.c | 30 ++++++++++++++++++++++++++---- hw/i386/pc_q35.c | 27 ++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 7 deletions(-)
hw/i386/pc_piix.c +26 -4
@@ -308,9 +308,16 @@ static void pc_init_pci(MachineState *machine)pc_init1 (machine,1 ,1 ); } +static void pc_compat_2_2 (MachineState *machine) +{ +} +static void pc_compat_2_1 (MachineState *machine) {PCMachineState *pcms =PC_MACHINE (machine); + +pc_compat_2_2 (machine); + smbios_uuid_encoded = false;x86_cpu_compat_set_features ("coreduo" ,FEAT_1_ECX ,CPUID_EXT_VMX ,0 );x86_cpu_compat_set_features ("core2duo" ,FEAT_1_ECX ,CPUID_EXT_VMX ,0 );
@@ -385,6 +392,12 @@ static void pc_compat_1_2(MachineState *machine)x86_cpu_compat_kvm_no_autoenable (FEAT_KVM ,KVM_FEATURE_PV_EOI ); } +static void pc_init_pci_2_2 (MachineState *machine) +{ +pc_compat_2_2 (machine); +pc_init_pci (machine); +} +static void pc_init_pci_2_1 (MachineState *machine) {pc_compat_2_1 (machine);
@@ -478,19 +491,27 @@ static void pc_xen_hvm_init(MachineState *machine) .desc = "Standard PC (i440FX + PIIX, 1996)", \ .hot_add_cpu = pc_hot_add_cpu -#define PC_I440FX_2_2_MACHINE_OPTIONS \ +#define PC_I440FX_2_3_MACHINE_OPTIONS \ PC_I440FX_MACHINE_OPTIONS, \ .default_machine_opts = "firmware=bios-256k.bin", \ .default_display = "std" -static QEMUMachine pc_i440fx_machine_v2_2 = { -PC_I440FX_2_2_MACHINE_OPTIONS , - .name ="pc-i440fx-2.2" , +static QEMUMachine pc_i440fx_machine_v2_3 = { +PC_I440FX_2_3_MACHINE_OPTIONS , + .name ="pc-i440fx-2.3" , .alias ="pc" , .init = pc_init_pci, .is_default =1 , }; +#define PC_I440FX_2_2_MACHINE_OPTIONS PC_I440FX_2_3_MACHINE_OPTIONS + +static QEMUMachine pc_i440fx_machine_v2_2 = { +PC_I440FX_2_2_MACHINE_OPTIONS , + .name ="pc-i440fx-2.2" , + .init = pc_init_pci_2_2, +}; +#define PC_I440FX_2_1_MACHINE_OPTIONS \ PC_I440FX_MACHINE_OPTIONS, \ .default_machine_opts = "firmware=bios-256k.bin"
@@ -928,6 +949,7 @@ static QEMUMachine xenfv_machine = {static void pc_machine_init (void ) { +qemu_register_pc_machine (&pc_i440fx_machine_v2_3);qemu_register_pc_machine (&pc_i440fx_machine_v2_2);qemu_register_pc_machine (&pc_i440fx_machine_v2_1);qemu_register_pc_machine (&pc_i440fx_machine_v2_0);
hw/i386/pc_q35.c +24 -3
@@ -287,10 +287,16 @@ static void pc_q35_init(MachineState *machine) } } +static void pc_compat_2_2 (MachineState *machine) +{ +} +static void pc_compat_2_1 (MachineState *machine) {PCMachineState *pcms =PC_MACHINE (machine); +pc_compat_2_2 (machine); + pcms->enforce_aligned_dimm = false; smbios_uuid_encoded = false;x86_cpu_compat_set_features ("coreduo" ,FEAT_1_ECX ,CPUID_EXT_VMX ,0 );
@@ -334,6 +340,12 @@ static void pc_compat_1_4(MachineState *machine)x86_cpu_compat_set_features ("Westmere" ,FEAT_1_ECX ,0 ,CPUID_EXT_PCLMULQDQ ); } +static void pc_q35_init_2_2 (MachineState *machine) +{ +pc_compat_2_2 (machine); +pc_q35_init (machine); +} +static void pc_q35_init_2_1 (MachineState *machine) {pc_compat_2_1 (machine);
@@ -377,16 +389,24 @@ static void pc_q35_init_1_4(MachineState *machine) .hot_add_cpu = pc_hot_add_cpu, \ .units_per_default_bus = 1 -#define PC_Q35_2_2_MACHINE_OPTIONS \ +#define PC_Q35_2_3_MACHINE_OPTIONS \ PC_Q35_MACHINE_OPTIONS, \ .default_machine_opts = "firmware=bios-256k.bin", \ .default_display = "std" +static QEMUMachine pc_q35_machine_v2_3 = { +PC_Q35_2_3_MACHINE_OPTIONS , + .name ="pc-q35-2.3" , + .alias ="q35" , + .init = pc_q35_init, +}; + +#define PC_Q35_2_2_MACHINE_OPTIONS PC_Q35_2_3_MACHINE_OPTIONS +static QEMUMachine pc_q35_machine_v2_2 = {PC_Q35_2_2_MACHINE_OPTIONS , .name ="pc-q35-2.2" , - .alias ="q35" , - .init = pc_q35_init, + .init = pc_q35_init_2_2, };#define PC_Q35_2_1_MACHINE_OPTIONS \
@@ -465,6 +485,7 @@ static QEMUMachine pc_q35_machine_v1_4 = {static void pc_q35_machine_init (void ) { +qemu_register_pc_machine (&pc_q35_machine_v2_3);qemu_register_pc_machine (&pc_q35_machine_v2_2);qemu_register_pc_machine (&pc_q35_machine_v2_1);qemu_register_pc_machine (&pc_q35_machine_v2_0);
Signature · 1 line
1.8.3.1
PATCH v2 4/4 pc: acpi-build: make RSDP table dynamic
In reply to Igor MammedovRebuild RSDP along with other tables to make building ACPI blob more robust and not depend on particular tables order.
Use it only with new machine types to avoid breaking migration since it changes migration format.
--- hw/i386/acpi-build.c | 22 ++++++++++++++++------ hw/i386/pc_piix.c | 3 +++ hw/i386/pc_q35.c | 3 +++ include/hw/i386/pc.h | 1 + 4 files changed, 23 insertions(+), 6 deletions(-)
hw/i386/acpi-build.c +16 -6
@@ -1518,6 +1518,8 @@ struct AcpiBuildState {uint32_t table_size;ram_addr_t linker_ram;uint32_t linker_size; +ram_addr_t rsdp_ram; +uint32_t rsdp_size;/* Is table patched? */ uint8_t patched;PcGuestInfo *guest_info;
@@ -1742,6 +1744,8 @@ static void acpi_build_update(void *build_opaque, uint32_t offset) build_state->table_size);memcpy (qemu_get_ram_ptr (build_state->linker_ram), tables.linker->data, build_state->linker_size); +memcpy (qemu_get_ram_ptr (build_state->rsdp_ram), tables.rsdp->data, + build_state->rsdp_size);cpu_physical_memory_set_dirty_range_nocode (build_state->table_ram, build_state->table_size);
@@ -1814,12 +1818,18 @@ void acpi_setup(PcGuestInfo *guest_info)fw_cfg_add_file (guest_info->fw_cfg,ACPI_BUILD_TPMLOG_FILE , tables.tcpalog->data,acpi_data_len (tables.tcpalog)); -/* -* RSDP is small so it's easy to keep it immutable, no need to -* bother with ROM blobs. -*/ -fw_cfg_add_file (guest_info->fw_cfg,ACPI_BUILD_RSDP_FILE , - tables.rsdp->data,acpi_data_len (tables.rsdp)); +if (guest_info->has_immutable_rsdp) { +/* +* RSDP is small so it's easy to keep it immutable, no need to +* bother with ROM blobs. +*/ +fw_cfg_add_file (guest_info->fw_cfg,ACPI_BUILD_RSDP_FILE , + tables.rsdp->data,acpi_data_len (tables.rsdp)); + }else { + build_state->rsdp_ram =acpi_add_rom_blob (build_state, tables.rsdp, +ACPI_BUILD_RSDP_FILE ); + build_state->rsdp_size =acpi_data_len (tables.rsdp); + }qemu_register_reset (acpi_build_reset, build_state);acpi_build_reset (build_state);
hw/i386/pc_piix.c +3 -0
@@ -60,6 +60,7 @@ static const int ide_iobase2[MAX_IDE_BUS] = { 0x3f6, 0x376 };static const int ide_irq[MAX_IDE_BUS ] = {14 ,15 };static bool has_acpi_build = true; +static bool has_immutable_rsdp;static int legacy_acpi_table_size;static bool smbios_defaults = true;static bool smbios_legacy_mode;
@@ -168,6 +169,7 @@ static void pc_init1(MachineState *machine, guest_info->isapc_ram_fw = !pci_enabled; guest_info->has_reserved_memory = has_reserved_memory; + guest_info->has_immutable_rsdp = has_immutable_rsdp;if (smbios_defaults) {MachineClass *mc =MACHINE_GET_CLASS (machine);
@@ -310,6 +312,7 @@ static void pc_init_pci(MachineState *machine)static void pc_compat_2_2 (MachineState *machine) { + has_immutable_rsdp = true; }static void pc_compat_2_1 (MachineState *machine)
hw/i386/pc_q35.c +3 -0
@@ -50,6 +50,7 @@#define MAX_SATA_PORTS 6static bool has_acpi_build = true; +static bool has_immutable_rsdp;static bool smbios_defaults = true;static bool smbios_legacy_mode;static bool smbios_uuid_encoded = true;
@@ -154,6 +155,7 @@ static void pc_q35_init(MachineState *machine) guest_info->isapc_ram_fw = false; guest_info->has_acpi_build = has_acpi_build; guest_info->has_reserved_memory = has_reserved_memory; + guest_info->has_immutable_rsdp = has_imutable_rsdp;/* Migration was not supported in 2.0 for Q35, so do not bother * with this hack (see hw/i386/acpi-build.c).
@@ -289,6 +291,7 @@ static void pc_q35_init(MachineState *machine)static void pc_compat_2_2 (MachineState *machine) { + has_immutable_rsdp = true; }static void pc_compat_2_1 (MachineState *machine)
include/hw/i386/pc.h +1 -0
@@ -104,6 +104,7 @@ struct PcGuestInfo {int legacy_acpi_table_size;bool has_acpi_build;bool has_reserved_memory; +bool has_immutable_rsdp; };/* parallel.c */
Signature · 1 line
1.8.3.1