Skip to contentSkip to conversation

acpi/crs: Prevent bad ranges for host bridges

PATCH In review Ben Widawsky 3 participants 6 messages Oct 26, 2020 – Oct 29, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 acpi/crs: Prevent bad ranges for host bridges

Prevent _CRS resources being quietly chopped off and instead throw an assertion. _CRS is used by host bridges to declare regions of io and/or memory that they consume. On some (all?) platforms the host bridge doesn't have PCI header space and so they need some way to convey the information.

---

1. I'm not aware of this being a real issue on platforms today as I think many platforms that use ACPI and actually have regions, constrain to 32b to be complaint with legacy.
2. Since host bridges aren't usually hot plugged, it can't be invoked by a user, so assert() seems like the right way to handle this.

---
 hw/i386/acpi-build.c | 2 ++
 1 file changed, 2 insertions(+)
hw/i386/acpi-build.c +2 -0
index eee7b57c37..df13abecf4 100644
@@ -869,6 +869,8 @@ static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set)
     crs_range_merge(temp_range_set.mem_ranges);
     for (i = 0; i < temp_range_set.mem_ranges->len; i++) {
         entry = g_ptr_array_index(temp_range_set.mem_ranges, i);
+        assert(entry->limit <= UINT32_MAX &&
+               (entry->limit - entry->base + 1) <= UINT32_MAX);
         aml_append(crs,
                    aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
                                     AML_MAX_FIXED, AML_NON_CACHEABLE,
Signature · 1 line
2.29.1
#1 lore raw

PATCH 2/2 acpi/crs: Support ranges > 32b for hosts

According to PCIe spec 5.0 Type 1 header space Base Address Registers are defined by 7.5.1.2.1 Base Address Registers (same as Type 0). The _CRS region should allow for the same range (up to 64b). Prior to this change, any host bridge utilizing more than 32b for the BAR would have the address truncated and likely lead to conflicts when the operating systems reads the _CRS object.

---
I don't think this effects any code currently in QEMU. You'd need to have a host bridge which has a BAR, and that BAR wants to be > 32b. I've hit this because I have a modified PXB device that does advertise a 64b BAR. Also, you'd need a platform that cares about ACPI, which, many do not.

---
 hw/i386/acpi-build.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/i386/acpi-build.c +8 -2
index df13abecf4..75604bdc74 100644
@@ -789,8 +789,14 @@ static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set)
                 crs_range_insert(temp_range_set.io_ranges,
                                  range_base, range_limit);
             } else { /* "memory" */
-                crs_range_insert(temp_range_set.mem_ranges,
-                                 range_base, range_limit);
+                uint64_t length = range_limit - range_base + 1;
+                if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
+                    crs_range_insert(temp_range_set.mem_ranges, range_base,
+                                     range_limit);
+                } else {
+                    crs_range_insert(temp_range_set.mem_64bit_ranges,
+                                     range_base, range_limit);
+                }
             }
         }
 
Signature · 1 line
2.29.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] acpi/crs: Support ranges > 32b for hosts

Quoted 8 lines · On Mon, 26 Oct 2020 12:39:24 -0700 Ben Widawsky <[email protected]> wrote:

According to PCIe spec 5.0 Type 1 header space Base Address Registers are defined by 7.5.1.2.1 Base Address Registers (same as Type 0). The _CRS region should allow for the same range (up to 64b). Prior to this change, any host bridge utilizing more than 32b for the BAR would have the address truncated and likely lead to conflicts when the operating systems reads the _CRS object.

Looks good to me, so

CCing,
Michael to have a send pair of eyes on it

but I wonder how/why ivshm (which might have quite large BAR) works.

PS:
please use git's --cover-letter option to create multi-patch series, in the future

Quoted 32 lines

---
I don't think this effects any code currently in QEMU. You'd need to have a host bridge which has a BAR, and that BAR wants to be > 32b. I've hit this because I have a modified PXB device that does advertise a 64b BAR. Also, you'd need a platform that cares about ACPI, which, many do not.

---
 hw/i386/acpi-build.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/i386/acpi-build.c +8 -2
index df13abecf4..75604bdc74 100644
@@ -789,8 +789,14 @@ static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set)
                 crs_range_insert(temp_range_set.io_ranges,
                                  range_base, range_limit);
             } else { /* "memory" */
-                crs_range_insert(temp_range_set.mem_ranges,
-                                 range_base, range_limit);
+                uint64_t length = range_limit - range_base + 1;
+                if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
+                    crs_range_insert(temp_range_set.mem_ranges, range_base,
+                                     range_limit);
+                } else {
+                    crs_range_insert(temp_range_set.mem_64bit_ranges,
+                                     range_base, range_limit);
+                }
             }
         }
 
#3 lore raw

Re: [PATCH 2/2] acpi/crs: Support ranges > 32b for hosts

Quoted 14 lines · Igor Mammedov
Quoted 8 lines · On Mon, 26 Oct 2020 12:39:24 -0700 Ben Widawsky <[email protected]> wrote:

According to PCIe spec 5.0 Type 1 header space Base Address Registers are defined by 7.5.1.2.1 Base Address Registers (same as Type 0). The _CRS region should allow for the same range (up to 64b). Prior to this change, any host bridge utilizing more than 32b for the BAR would have the address truncated and likely lead to conflicts when the operating systems reads the _CRS object.

Looks good to me, so

Quoted 7 lines

CCing,
Michael to have a send pair of eyes on it

but I wonder how/why ivshm (which might have quite large BAR) works.

I think this will only hit things that subclass TYPE_PCI_HOST_BRIDGE. AFAICT, ivshm is a regular TYPE_PCI_DEVICE. Is there a _CRS created for ivshm?

Quoted 4 lines

PS:
please use git's --cover-letter option to create multi-patch series, in the future

Will do. I wasn't sure what the cutoff was, but the wiki is pretty clear that "multiple" is the cutoff and it's important to CI.

Quoted 35 lines
Quoted 33 lines

---
I don't think this effects any code currently in QEMU. You'd need to have a host bridge which has a BAR, and that BAR wants to be > 32b. I've hit this because I have a modified PXB device that does advertise a 64b BAR. Also, you'd need a platform that cares about ACPI, which, many do not.

---
 hw/i386/acpi-build.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
hw/i386/acpi-build.c +8 -2
index df13abecf4..75604bdc74 100644
@@ -789,8 +789,14 @@ static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set)
                 crs_range_insert(temp_range_set.io_ranges,
                                  range_base, range_limit);
             } else { /* "memory" */
-                crs_range_insert(temp_range_set.mem_ranges,
-                                 range_base, range_limit);
+                uint64_t length = range_limit - range_base + 1;
+                if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
+                    crs_range_insert(temp_range_set.mem_ranges, range_base,
+                                     range_limit);
+                } else {
+                    crs_range_insert(temp_range_set.mem_64bit_ranges,
+                                     range_base, range_limit);
+                }
             }
         }
 
#4 lore raw

Re: [PATCH 2/2] acpi/crs: Support ranges > 32b for hosts

Quoted 18 lines · On Tue, 27 Oct 2020 08:45:05 -0700 Ben Widawsky <[email protected]> wrote:
Quoted 17 lines · Igor Mammedov
Quoted 8 lines · On Mon, 26 Oct 2020 12:39:24 -0700 Ben Widawsky <[email protected]> wrote:

According to PCIe spec 5.0 Type 1 header space Base Address Registers are defined by 7.5.1.2.1 Base Address Registers (same as Type 0). The _CRS region should allow for the same range (up to 64b). Prior to this change, any host bridge utilizing more than 32b for the BAR would have the address truncated and likely lead to conflicts when the operating systems reads the _CRS object.

Looks good to me, so

Quoted 7 lines
Quoted 4 lines

CCing,
Michael to have a send pair of eyes on it

but I wonder how/why ivshm (which might have quite large BAR) works.

I think this will only hit things that subclass TYPE_PCI_HOST_BRIDGE. AFAICT, ivshm is a regular TYPE_PCI_DEVICE. Is there a _CRS created for ivshm?

no, but device uses _CRS provided by bus, so I'd expect it would fail on guest side if its BAR is bigger than window provided by host bridge.

[...]

Acked-by on v1 1/2
#5 lore raw
In reply to Ben Widawsky
Quoted 7 lines · On Mon, 26 Oct 2020 12:39:23 -0700 Ben Widawsky <[email protected]> wrote:

Prevent _CRS resources being quietly chopped off and instead throw an assertion. _CRS is used by host bridges to declare regions of io and/or memory that they consume. On some (all?) platforms the host bridge doesn't have PCI header space and so they need some way to convey the information.

Quoted 24 lines

---

1. I'm not aware of this being a real issue on platforms today as I think many platforms that use ACPI and actually have regions, constrain to 32b to be complaint with legacy.
2. Since host bridges aren't usually hot plugged, it can't be invoked by a user, so assert() seems like the right way to handle this.

---
 hw/i386/acpi-build.c | 2 ++
 1 file changed, 2 insertions(+)
hw/i386/acpi-build.c +2 -0
index eee7b57c37..df13abecf4 100644
@@ -869,6 +869,8 @@ static Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set)
     crs_range_merge(temp_range_set.mem_ranges);
     for (i = 0; i < temp_range_set.mem_ranges->len; i++) {
         entry = g_ptr_array_index(temp_range_set.mem_ranges, i);
+        assert(entry->limit <= UINT32_MAX &&
+               (entry->limit - entry->base + 1) <= UINT32_MAX);
         aml_append(crs,
                    aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
                                     AML_MAX_FIXED, AML_NON_CACHEABLE,