acpi: RSDP: fix checksum calculations
cover acpi: RSDP: fix checksum calculations
* arm/virt is broken but it looks like linux doesn't care, lets fix it anyways
* x86, got lucky since we didn't use extended fields,
fix it so that it will calculate checksum using correct length so that it would be easier to unify arm/x86 into one impl.
Igor Mammedov (2):
pc: acpi: use correct RSDT length for checksum
arm/virt: acpi: fix incorrect checksums in RSDP
hw/arm/virt-acpi-build.c | 7 ++++++- hw/i386/acpi-build.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/2 pc: acpi: use correct RSDT length for checksum
AcpiRsdpDescriptor describes revision 2 RSDP table so using sizeof(*rsdp) for checksum calculation isn't correct since we are adding extra 16 bytes. But acpi_data_push() zeroes out table, so just by luck we are summing up exta zeros which still yelds correct checksum.
Fix it up by explicitly stating table size instead of using pointer arithmetics on stucture.
PS:
Extra 16 bytes are still wasted, but droping them will break migration for machines older than 2.3 due to size mismatch, for 2.3 and older it's not an issue since they are using resizable memory regions (a1666142d) for ACPI blobs. So keep wasting memory to avoid breaking old machines.
---
there is no changes to the current RSDP content caused by this patch
--- hw/i386/acpi-build.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/acpi-build.c +1 -1
@@ -2567,7 +2567,7 @@ build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned rsdt_tbl_offset)/* Checksum to be filled by Guest linker */ bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , - (char *)rsdp - rsdp_table->data,sizeof *rsdp, + (char *)rsdp - rsdp_table->data,20 /* ACPI rev 1.0 RSDP size */ , (char *)&rsdp->checksum - rsdp_table->data);return rsdp_table;
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 1/2] pc: acpi: use correct RSDT length for checksum
Quoted 5 lines · Igor Mammedov
AcpiRsdpDescriptor describes revision 2 RSDP table so using
sizeof(*rsdp)for checksum calculation isn't correct since we are adding extra 16 bytes. Butacpi_data_push()zeroes out table, so just by luck we are summing up exta zeros which still yelds correct checksum.
Quoted 8 lines
Fix it up by explicitly stating table size instead of using pointer arithmetics on stucture.
PS:
Extra 16 bytes are still wasted, but droping them will break migration for machines older than 2.3 due to size mismatch, for 2.3 and older it's not an issue since they are using resizable memory regions (a1666142d) for ACPI blobs. So keep wasting memory to avoid breaking old machines.
I'd like this explanation in code comments please.
Quoted 13 lines
Fixes 72c194f7e (i386: ACPI table generation code from seabios)Signed-off-by Igor Mammedov <[email protected]>---
there is no changes to the current RSDP content caused by this patch--- hw/i386/acpi-build.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i386/acpi-build.c +0
-0
hw/i386/acpi-build.c build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned rsdt_tbl_offset) · line 2573
Quoted 8 lines
hw/i386/acpi-build.c +1
-1@@ -2567,7 +2567,7 @@ build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned rsdt_tbl_offset)/* Checksum to be filled by Guest linker */ bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , - (char *)rsdp - rsdp_table->data,sizeof *rsdp, + (char *)rsdp - rsdp_table->data,20 /* ACPI rev 1.0 RSDP size */ , (char *)&rsdp->checksum - rsdp_table->data);return rsdp_table;
I dislike hard-coded size math like this.
How about a sub-structure for RSDPv1?
Quoted 2 lines
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 1/2] pc: acpi: use correct RSDT length for checksum
Quoted 9 lines · On Fri, 23 Nov 2018 11:10:01 -0500 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 8 lines · Igor Mammedov
AcpiRsdpDescriptor describes revision 2 RSDP table so using
sizeof(*rsdp)for checksum calculation isn't correct since we are adding extra 16 bytes. Butacpi_data_push()zeroes out table, so just by luck we are summing up exta zeros which still yelds correct checksum.Fix it up by explicitly stating table size instead of using pointer arithmetics on stucture.
Quoted 7 lines
Quoted 5 lines
PS:
Extra 16 bytes are still wasted, but droping them will break migration for machines older than 2.3 due to size mismatch, for 2.3 and older it's not an issue since they are using resizable memory regions (a1666142d) for ACPI blobs. So keep wasting memory to avoid breaking old machines.I'd like this explanation in code comments please.
sure, I'll move it there
Quoted 20 lines
Quoted 20 lines
Fixes 72c194f7e (i386: ACPI table generation code from seabios)Signed-off-by Igor Mammedov <[email protected]>---
there is no changes to the current RSDP content caused by this patch--- hw/i386/acpi-build.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/i386/acpi-build.c +1
-1@@ -2567,7 +2567,7 @@ build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned rsdt_tbl_offset)/* Checksum to be filled by Guest linker */ bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , - (char *)rsdp - rsdp_table->data,sizeof *rsdp, + (char *)rsdp - rsdp_table->data,20 /* ACPI rev 1.0 RSDP size */ , (char *)&rsdp->checksum - rsdp_table->data);
Quoted 3 lines
hw/i386/acpi-build.c build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned rsdt_tbl_offset) · line 2573
Quoted 1 line
hw/i386/acpi-build.c +0
-0return rsdp_table;I dislike hard-coded size math like this.
I thought we had a consensus on this point. i.e. replace all packed structures with build_append_int_noprefix() API and use spec defined values directly (with comments), so whenever we compare spec definition with qemu code it would be 1:1 match.
Quoted 1 line
How about a sub-structure for RSDPv1?
Structure won't be used for long, this patch fixes immediate error and Samuel is preparing series that's generalizing build_rsdp() switching to build_append_int_noprefix() API and dropping no longer used structure so it couldn't be misused.
Quoted 2 lines
Quoted 2 lines
Signature · 1 line
2.7.4
PATCH v2 1/2 pc: acpi: use correct RSDT length for checksum
In reply to Michael S. TsirkinAcpiRsdpDescriptor describes revision 2 RSDP table so using sizeof(*rsdp) for checksum calculation isn't correct since we are adding extra 16 bytes. But acpi_data_push() zeroes out table, so just by luck we are summing up exta zeros which still yelds correct checksum.
Fix it up by explicitly stating table size instead of using pointer arithmetics on stucture.
PS:
Extra 16 bytes are still wasted, but droping them will break migration for machines older than 2.3 due to size mismatch, for 2.3 and older it's not an issue since they are using resizable memory regions (a1666142d) for ACPI blobs. So keep wasting memory to avoid breaking old machines.
---
there is no changes to the current RSDP content caused by this patch
v2:
- add 16 byte leak comment to the code (Michael)
--- hw/i386/acpi-build.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/i386/acpi-build.c +6 -1
@@ -2550,6 +2550,11 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker)static GArray *build_rsdp (GArray *rsdp_table,BIOSLinker *linker,unsigned rsdt_tbl_offset) { +/* AcpiRsdpDescriptor describes revision 2 RSDP table and as result we +* allocate extra 16 bytes for pc/q35 RSDP rev1 as well. Keep extra 16 bytes +* wasted to make sure we won't breake migration for machine types older +* than 2.3 due to size mismatch. +*/ AcpiRsdpDescriptor *rsdp =acpi_data_push (rsdp_table,sizeof *rsdp);unsigned rsdt_pa_size =sizeof (rsdp->rsdt_physical_address); unsigned rsdt_pa_offset =
@@ -2567,7 +2572,7 @@ build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned rsdt_tbl_offset)/* Checksum to be filled by Guest linker */ bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , - (char *)rsdp - rsdp_table->data,sizeof *rsdp, + (char *)rsdp - rsdp_table->data,20 /* ACPI rev 1.0 RSDP size */ , (char *)&rsdp->checksum - rsdp_table->data);return rsdp_table;
Signature · 1 line
2.7.4
PATCH 2/2 arm/virt: acpi: fix incorrect checksums in RSDP
In reply to Igor MammedovWhen RSDP table was introduced (d4bec5d87), we calculated only legacy checksum, and that was incorrect as it
- specified rev=2 and forgot about extended checksum.
- legacy checksum calculated on full table instead of the 1st 20 bytes
Fix it by adding extended checksum calculation and using correct size for legacy checksum.
While at it use explicit constants to specify sub/full tables
sizes instead of relying on AcpiRsdpDescriptor size and fields offsets. The follow up commits will convert this table to build_append_int_noprefix() API, will use constants anyway and remove unused AcpiRsdpDescriptor structure.
Based on "[PATCH v5 05/24] hw: acpi: Implement XSDT support for RSDP" by Samuel Ortiz, who did it right in his impl.
--- hw/arm/virt-acpi-build.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/arm/virt-acpi-build.c +6 -1
@@ -390,9 +390,14 @@ build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned xsdt_tbl_offset)/* Checksum to be filled by Guest linker */ bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , - (char *)rsdp - rsdp_table->data,sizeof *rsdp, + (char *)rsdp - rsdp_table->data,20 /* ACPI rev 1.0 RSDP size */ , (char *)&rsdp->checksum - rsdp_table->data); +/* Extended checksum to be filled by Guest linker */ +bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , + (char *)rsdp - rsdp_table->data,36 /* ACPI rev 2.0 RSDP size */ , + (char *)&rsdp->extended_checksum - rsdp_table->data); +return rsdp_table; }
Signature · 1 line
2.7.4
Re: [Qemu-devel] [PATCH 2/2] arm/virt: acpi: fix incorrect checksums in RSDP
Quoted 48 lines · Igor Mammedov
When RSDP table was introduced (
d4bec5d87), we calculated only legacy checksum, and that was incorrect as it
- specified rev=2 and forgot about extended checksum.
- legacy checksum calculated on full table instead of the 1st 20 bytesFix it by adding extended checksum calculation and using correct size for legacy checksum.
While at it use explicit constants to specify sub/full tables
sizes instead of relying on AcpiRsdpDescriptor size and fields offsets. The follow up commits will convert this table tobuild_append_int_noprefix()API, will use constants anyway and remove unused AcpiRsdpDescriptor structure.Based on "[PATCH v5 05/24] hw: acpi: Implement XSDT support for RSDP" by Samuel Ortiz, who did it right in his impl.
Fixes d4bec5d87 (hw/arm/virt-acpi-build: Generate RSDP table)Signed-off-by Igor Mammedov <[email protected]>CC Ard Biesheuvel <[email protected]>CC Shannon Zhao <[email protected]>CC Samuel Ortiz <[email protected]>--- hw/arm/virt-acpi-build.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/arm/virt-acpi-build.c +6
-1@@ -390,9 +390,14 @@ build_rsdp(GArray *rsdp_table, BIOSLinker *linker, unsigned xsdt_tbl_offset)/* Checksum to be filled by Guest linker */ bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , - (char *)rsdp - rsdp_table->data,sizeof *rsdp, + (char *)rsdp - rsdp_table->data,20 /* ACPI rev 1.0 RSDP size */ , (char *)&rsdp->checksum - rsdp_table->data); +/* Extended checksum to be filled by Guest linker */ +bios_linker_loader_add_checksum (linker,ACPI_BUILD_RSDP_FILE , + (char *)rsdp - rsdp_table->data,36 /* ACPI rev 2.0 RSDP size */ , + (char *)&rsdp->extended_checksum - rsdp_table->data); +return rsdp_table; }Signature · 1 line
2.7.4
Indeed this slipped by because Linux doesn't seem to care. Linux doesn't even check the checksums by default. It can be forced to check them by adding the acpi_force_table_verification kernel command line parameter, but even then it didn't care that we got it wrong for this table, at it apparently doesn't bother checking it the RSDP, but rather just the XSDT it points to. In any case, a guest that was depending on that broken behavior is broken, so we can fix this without concern for compatibility, and with this change we now match the spec.
Quoted 5 lines · Igor Mammedov
* arm/virt is broken but it looks like linux doesn't care, lets fix it anyways
* x86, got lucky since we didn't use extended fields,
fix it so that it will calculate checksum using correct length so that it would be easier to unify arm/x86 into one impl.
I'd say patch 2 should go into this release.
What about patch 1? It's cosmetic and it isn't the best we can do.
Let me know whether you agree.
Quoted 10 lines
Igor Mammedov (2):
pc: acpi: use correct RSDT length for checksum
arm/virt: acpi: fix incorrect checksums in RSDPhw/arm/virt-acpi-build.c | 7 ++++++- hw/i386/acpi-build.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-)Signature · 1 line
2.7.4
Quoted 11 lines · On Fri, 23 Nov 2018 11:11:50 -0500 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 5 lines · Igor Mammedov
* arm/virt is broken but it looks like linux doesn't care, lets fix it anyways
* x86, got lucky since we didn't use extended fields,
fix it so that it will calculate checksum using correct length so that it would be easier to unify arm/x86 into one impl.I'd say patch 2 should go into this release.
What about patch 1? It's cosmetic and it isn't the best we can do.Let me know whether you agree.
I'd say they are both 'cosmetic' as they don't cause guest failure nor even warning (linux doesn't care (so far) and we don't have windows for ARM to verify extended checksum). So we don't have to delay release for both of them.
I assumed that Samuel, would include patches in his RSDP refactoring series and we would merge them as whole later on.
Quoted 11 lines
Quoted 11 lines
Igor Mammedov (2):
pc: acpi: use correct RSDT length for checksum
arm/virt: acpi: fix incorrect checksums in RSDPhw/arm/virt-acpi-build.c | 7 ++++++- hw/i386/acpi-build.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-)Signature · 1 line
2.7.4
Quoted 10 lines · Igor Mammedov
Quoted 7 lines · On Fri, 23 Nov 2018 11:11:50 -0500 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 5 lines · Igor Mammedov
* arm/virt is broken but it looks like linux doesn't care, lets fix it anyways
* x86, got lucky since we didn't use extended fields,
fix it so that it will calculate checksum using correct length so that it would be easier to unify arm/x86 into one impl.
Quoted 8 lines
Quoted 4 lines
I'd say patch 2 should go into this release.
What about patch 1? It's cosmetic and it isn't the best we can do.Let me know whether you agree.
I'd say they are both 'cosmetic' as they don't cause guest failure nor even warning (linux doesn't care (so far) and we don't have windows for ARM to verify extended checksum). So we don't have to delay release for both of them.
Agreed.
Quoted 2 lines
I assumed that Samuel, would include patches in his RSDP refactoring series and we would merge them as whole later on.
That's correct, yes.
Cheers,
Samuel.