Skip to contentSkip to conversation

pci: correct pci config size default for cap version 2 endpoints

PATCH In review Donald Dutile 4 participants 13 messages Jul 22, 2011 – Aug 8, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 pci: correct pci config size default for cap version 2 endpoints

v3: remove all boundary tests. just fix the obvious bug.
: boundary test is not necessary; get this fix in &
post boundary test in another separate patch.

v2: do local boundary check with respect to legacy PCI header length,
and don't depend on it in pci_add_capability().
: fix compilation, and change else>2 to simple else for all other cases.

v1: first patch: boundary check in pci_add_capability().

Doing device assignement using a PCIe device with it's PCI Cap structure at offset 0xcc showed a problem in the default size mapped for this cap-id.

The failure caused a corruption which might have gone unnoticed otherwise.

Fix assigned_device_pci_cap_init() to set the default size of PCIe Cap structure (cap-id 0x10) to 0x34 instead of 0x3c. 0x34 is default, min, for endpoint device with a cap version of 2.

cc Alex Williamson <[email protected]>
cc Michael S. Tsirkin <[email protected]>

---

 hw/device-assignment.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
hw/device-assignment.c +5 -3
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;
-        int size = 0x3c; /* version 2 size */
 
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;
+        } else {
             fprintf(stderr, "Unsupported PCI express capability version %d\n",
                     version);
             return -EINVAL;
Acked-by on v3 1/1
#1 lore raw
Quoted 49 lines · Donald Dutile

v3: remove all boundary tests. just fix the obvious bug.
: boundary test is not necessary; get this fix in &
post boundary test in another separate patch.

v2: do local boundary check with respect to legacy PCI header length,
and don't depend on it in pci_add_capability().
: fix compilation, and change else>2 to simple else for all other cases.

v1: first patch: boundary check in pci_add_capability().

Doing device assignement using a PCIe device with it's PCI Cap structure at offset 0xcc showed a problem in the default size mapped for this cap-id.

The failure caused a corruption which might have gone unnoticed otherwise.

Fix assigned_device_pci_cap_init() to set the default size of PCIe Cap structure (cap-id 0x10) to 0x34 instead of 0x3c. 0x34 is default, min, for endpoint device with a cap version of 2.

cc Alex Williamson <[email protected]>
cc Michael S. Tsirkin <[email protected]>

---

 hw/device-assignment.c |    8 +++++---
 1 files changed, 5 insertions(+), 3 deletions(-)
hw/device-assignment.c +1 -2
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;
-        int size = 0x3c; /* version 2 size */
 
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1436

Quoted 10 lines
hw/device-assignment.c +4 -1
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;
+        } else {
             fprintf(stderr, "Unsupported PCI express capability version %d\n",
                     version);
             return -EINVAL;
#2 lore raw
In reply to Donald Dutile
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

#3 lore raw
Quoted 15 lines · Chris Wright
Quoted 14 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -2
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;
-        int size = 0x3c; /* version 2 size */
 
Quoted 11 lines

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 8 lines
hw/device-assignment.c +3 -1
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Alex

#4 lore raw
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 15 lines · Chris Wright
Quoted 14 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -2
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;
-        int size = 0x3c; /* version 2 size */
 
Quoted 11 lines

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 8 lines
hw/device-assignment.c +3 -1
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

#5 lore raw
Quoted 29 lines · Chris Wright
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 26 lines · Chris Wright
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 5 lines
Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

How about we check for overflow and only do the hacks if it happens?

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

Signature · 1 line
MST
#6 lore raw
Quoted 43 lines · Michael S. Tsirkin
Quoted 29 lines · Chris Wright
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 26 lines · Chris Wright
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 5 lines
Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

Quoted 6 lines

How about we check for overflow and only do the hacks if it happens?

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

I take this last bit back: registers we touch are at offset < 0x34. Sorry about the noise. But the question about read-only registers still stands.

Quoted 3 lines
Signature · 2 lines
-- 
MST
#7 lore raw
Quoted 47 lines · Michael S. Tsirkin
Quoted 46 lines · Michael S. Tsirkin
Quoted 29 lines · Chris Wright
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 26 lines · Chris Wright
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
     }
 
     if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
         uint16_t type, devctl, lnkcap, lnksta;
         uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
         version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
         version &= PCI_EXP_FLAGS_VERS;
         if (version == 1) {
             size = 0x14;
-        } else if (version > 2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 5 lines
Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

How about we check for overflow and only do the hacks if it happens?

Quoted 7 lines
Quoted 3 lines

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

I take this last bit back: registers we touch are at offset < 0x34. Sorry about the noise. But the question about read-only registers still stands.

Also, where does the magic 0x34 come from? I'm guessing this is simply what's left till the end of the config space. So let's be conservative specific as possible with this hack:

/* A version 2 device was observed to only have a partial
* implementation for the capability structure. Apparently, it doesn't
* implement the registers from slot capability 2 and on (offset 0x34),
* with the capability at offset 0xCC = 256 - 0x34. This is out of spec,
* but let's try to support this. */

if (version == 2 && pos == 0xCC) {
	size = 0x34;
}
Quoted 3 lines
Quoted 3 lines
Signature · 2 lines
-- 
MST
author
#8 lore raw
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Michael S. Tsirkin
Quoted 49 lines · Michael S. Tsirkin
Quoted 29 lines · Chris Wright
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 26 lines · Chris Wright
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
      }
 
      if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
          uint16_t type, devctl, lnkcap, lnksta;
          uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
          version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
          version&= PCI_EXP_FLAGS_VERS;
          if (version == 1) {
              size = 0x14;
-        } else if (version>  2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 5 lines
Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

How about we check for overflow and only do the hacks if it happens?

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

Quoted 9 lines
Quoted 3 lines

I take this last bit back: registers we touch are at offset< 0x34. Sorry about the noise. But the question about read-only registers still stands.

Also, where does the magic 0x34 come from? I'm guessing this is simply what's left till the end of the config space. So let's be conservative specific as possible with this hack:

I believe the spec leaves room for interpretation, and thus the resulting 'broken' device. As I read the spec, the size of the struct can be:

-- 0x2c for all devices, min., that are cap version 2 or higher.
-- 0x34 for devices with links, i.e., not a root-port-based device,
, a device not integrated into the root port , or if it is, it uses a serial link anyhow
(doesn't strip out 8b/10b serial link btwn device
& internal root port)
-- 0x3c for devices with slots, i.e., a bridge with downstream slots,
i.e., not an endpoint, i.e., a PCI(e) bridge.

Thus, 0x34 was chosen, since we don't support device assigning PCI bridges, (not until MRIOV shows up, at least), and 0x34 fits the bug at hand, and device cap/stat/control may be used/modified.

So, a 'hack' is not needed. In fact, the 0x34 size is a bit of a hack, since the case to use 0x2c could be ascertained by checking if the device is a root port device, _and_ it's not using a serial link, but a perusal of root port devices on a number of systems I looked at always had this structure greater than 0x2c, so I figured the simple heuristic of 0x34 was sufficient.

Quoted 13 lines

/* A version 2 device was observed to only have a partial
* implementation for the capability structure. Apparently, it doesn't
* implement the registers from slot capability 2 and on (offset 0x34),
* with the capability at offset 0xCC = 256 - 0x34. This is out of spec,
* but let's try to support this. */

if (version == 2&&  pos == 0xCC) {
	size = 0x34;
}
Quoted 3 lines
Quoted 3 lines

--
MST

#9 lore raw
Quoted 79 lines · Don Dutile
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Michael S. Tsirkin
Quoted 49 lines · Michael S. Tsirkin
Quoted 29 lines · Chris Wright
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 26 lines · Chris Wright
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
      }
 
      if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
          uint16_t type, devctl, lnkcap, lnksta;
          uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
          version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
          version&= PCI_EXP_FLAGS_VERS;
          if (version == 1) {
              size = 0x14;
-        } else if (version>  2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 5 lines
Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

How about we check for overflow and only do the hacks if it happens?

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

Quoted 9 lines
Quoted 3 lines

I take this last bit back: registers we touch are at offset< 0x34. Sorry about the noise. But the question about read-only registers still stands.

Also, where does the magic 0x34 come from? I'm guessing this is simply what's left till the end of the config space. So let's be conservative specific as possible with this hack:

I believe the spec leaves room for interpretation, and thus the resulting 'broken' device. As I read the spec, the size of the struct can be:

-- 0x2c for all devices, min., that are cap version 2 or higher.
-- 0x34 for devices with links, i.e., not a root-port-based device,
, a device not integrated into the root port , or if it is, it uses a serial link anyhow
(doesn't strip out 8b/10b serial link btwn device
& internal root port)
-- 0x3c for devices with slots, i.e., a bridge with downstream slots,
i.e., not an endpoint, i.e., a PCI(e) bridge.

Thus, 0x34 was chosen, since we don't support device assigning PCI bridges, (not until MRIOV shows up, at least), and 0x34 fits the bug at hand, and device cap/stat/control may be used/modified.

Quoted 14 lines

So, a 'hack' is not needed. In fact, the 0x34 size is a bit of a hack, since the case to use 0x2c could be ascertained by checking if the device is a root port device, _and_ it's not using a serial link, but a perusal of root port devices on a number of systems I looked at always had this structure greater than 0x2c, so I figured the simple heuristic of 0x34 was sufficient.

Quoted 8 lines

/* A version 2 device was observed to only have a partial
* implementation for the capability structure. Apparently, it doesn't
* implement the registers from slot capability 2 and on (offset 0x34),
* with the capability at offset 0xCC = 256 - 0x34. This is out of spec,
* but let's try to support this. */

if (version == 2&&  pos == 0xCC) {
	size = 0x34;
}

The more I look at it, the more I think that maybe this is an especially broken device and we shouldn't change the default for it. BTW, the programming reference for this device is here:

http://www.broadcom.com/collateral/pg/5761-PG100-R.pdf

They've burned up most of the capability area for vendor specific registers, so there's not enough room for the full pci-e capability structure. I'd be fine with adding a test specifically for this. As you suggested on IRC, print some warning and cut pci-e back to 0x34 if it extends past the legacy config space, and reject the device if it's still too small. Leave the default 0x3c since this is the only device we've found with this problem. Thanks,

Alex

author
#10 lore raw
Quoted 109 lines · Alex Williamson
Quoted 79 lines · Don Dutile
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Michael S. Tsirkin
Quoted 49 lines · Michael S. Tsirkin
Quoted 29 lines · Chris Wright
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 26 lines · Chris Wright
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
       }
 
       if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
           uint16_t type, devctl, lnkcap, lnksta;
           uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
           version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
           version&= PCI_EXP_FLAGS_VERS;
           if (version == 1) {
               size = 0x14;
-        } else if (version>   2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 5 lines
Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

How about we check for overflow and only do the hacks if it happens?

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

Quoted 9 lines
Quoted 3 lines

I take this last bit back: registers we touch are at offset< 0x34.
Sorry about the noise. But the question about read-only registers still stands.

Also, where does the magic 0x34 come from? I'm guessing this is simply what's left till the end of the config space. So let's be conservative specific as possible with this hack:

I believe the spec leaves room for interpretation, and thus the resulting 'broken' device. As I read the spec, the size of the struct can be:

-- 0x2c for all devices, min., that are cap version 2 or higher.
-- 0x34 for devices with links, i.e., not a root-port-based device,
, a device not integrated into the root port , or if it is, it uses a serial link anyhow
(doesn't strip out 8b/10b serial link btwn device
& internal root port)
-- 0x3c for devices with slots, i.e., a bridge with downstream slots,
i.e., not an endpoint, i.e., a PCI(e) bridge.

Thus, 0x34 was chosen, since we don't support device assigning PCI bridges, (not until MRIOV shows up, at least), and 0x34 fits the bug at hand, and device cap/stat/control may be used/modified.

Quoted 14 lines

So, a 'hack' is not needed. In fact, the 0x34 size is a bit of a hack, since the case to use 0x2c could be ascertained by checking if the device is a root port device, _and_ it's not using a serial link, but a perusal of root port devices on a number of systems I looked at always had this structure greater than 0x2c, so I figured the simple heuristic of 0x34 was sufficient.

Quoted 8 lines

/* A version 2 device was observed to only have a partial
* implementation for the capability structure. Apparently, it doesn't
* implement the registers from slot capability 2 and on (offset 0x34),
* with the capability at offset 0xCC = 256 - 0x34. This is out of spec,
* but let's try to support this. */

if (version == 2&&   pos == 0xCC) {
	size = 0x34;
}

The more I look at it, the more I think that maybe this is an especially broken device and we shouldn't change the default for it. BTW, the programming reference for this device is here:

http://www.broadcom.com/collateral/pg/5761-PG100-R.pdf

They've burned up most of the capability area for vendor specific registers, so there's not enough room for the full pci-e capability structure. I'd be fine with adding a test specifically for this. As you suggested on IRC, print some warning and cut pci-e back to 0x34 if it extends past the legacy config space, and reject the device if it's still too small. Leave the default 0x3c since this is the only device we've found with this problem. Thanks,

Quoted 4 lines

Alex

I have to admit, broken devices wrt specs isn't uncommon, and this device seems to fit the bill. Even though the diagram for PCIe Capability structure implies optional regs, the verbal section in 7.8 says the registers should read-as-0 if they aren't supported, implying, the space for them should be reserved and read as 0's.

So, a check & workaround seems in order....

so something like
max_cap_size = 256-base;
if (size > max_cap_size) then size=max_cap_size, fprintf warning about structure mapping-size reduction?

#11 lore raw
Quoted 121 lines · Don Dutile
Quoted 109 lines · Alex Williamson
Quoted 79 lines · Don Dutile
Quoted 52 lines · Michael S. Tsirkin
Quoted 51 lines · Michael S. Tsirkin
Quoted 49 lines · Michael S. Tsirkin
Quoted 29 lines · Chris Wright
Quoted 28 lines · * Alex Williamson ([email protected]) wrote:
Quoted 26 lines · Chris Wright
Quoted 12 lines · * Donald Dutile ([email protected]) wrote:
hw/device-assignment.c +1 -1
index 36ad6b0..34db52e 100644
@@ -1419,16 +1419,18 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev)
      }
 
      if ((pos = pci_find_cap_offset(pci_dev, PCI_CAP_ID_EXP, 0))) {
-        uint8_t version;
+        uint8_t version, size;
          uint16_t type, devctl, lnkcap, lnksta;
          uint32_t devcap;

hw/device-assignment.c static int assigned_device_pci_cap_init(PCIDevice *pci_dev) · line 1432

Quoted 10 lines
hw/device-assignment.c +3 -2
-        int size = 0x3c; /* version 2 size */
 
          version = pci_get_byte(pci_dev->config + pos + PCI_EXP_FLAGS);
          version&= PCI_EXP_FLAGS_VERS;
          if (version == 1) {
              size = 0x14;
-        } else if (version>   2) {
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Quoted 5 lines
Quoted 3 lines

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

How about we check for overflow and only do the hacks if it happens?

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

Quoted 9 lines
Quoted 3 lines

I take this last bit back: registers we touch are at offset< 0x34.
Sorry about the noise. But the question about read-only registers still stands.

Also, where does the magic 0x34 come from? I'm guessing this is simply what's left till the end of the config space. So let's be conservative specific as possible with this hack:

I believe the spec leaves room for interpretation, and thus the resulting 'broken' device. As I read the spec, the size of the struct can be:

-- 0x2c for all devices, min., that are cap version 2 or higher.
-- 0x34 for devices with links, i.e., not a root-port-based device,
, a device not integrated into the root port , or if it is, it uses a serial link anyhow
(doesn't strip out 8b/10b serial link btwn device
& internal root port)
-- 0x3c for devices with slots, i.e., a bridge with downstream slots,
i.e., not an endpoint, i.e., a PCI(e) bridge.

Thus, 0x34 was chosen, since we don't support device assigning PCI bridges, (not until MRIOV shows up, at least), and 0x34 fits the bug at hand, and device cap/stat/control may be used/modified.

Quoted 14 lines

So, a 'hack' is not needed. In fact, the 0x34 size is a bit of a hack, since the case to use 0x2c could be ascertained by checking if the device is a root port device, _and_ it's not using a serial link, but a perusal of root port devices on a number of systems I looked at always had this structure greater than 0x2c, so I figured the simple heuristic of 0x34 was sufficient.

Quoted 8 lines

/* A version 2 device was observed to only have a partial
* implementation for the capability structure. Apparently, it doesn't
* implement the registers from slot capability 2 and on (offset 0x34),
* with the capability at offset 0xCC = 256 - 0x34. This is out of spec,
* but let's try to support this. */

if (version == 2&&   pos == 0xCC) {
	size = 0x34;
}

The more I look at it, the more I think that maybe this is an especially broken device and we shouldn't change the default for it. BTW, the programming reference for this device is here:

http://www.broadcom.com/collateral/pg/5761-PG100-R.pdf

They've burned up most of the capability area for vendor specific registers, so there's not enough room for the full pci-e capability structure. I'd be fine with adding a test specifically for this. As you suggested on IRC, print some warning and cut pci-e back to 0x34 if it extends past the legacy config space, and reject the device if it's still too small. Leave the default 0x3c since this is the only device we've found with this problem. Thanks,

Quoted 4 lines

Alex

I have to admit, broken devices wrt specs isn't uncommon, and this device seems to fit the bill. Even though the diagram for PCIe Capability structure implies optional regs, the verbal section in 7.8 says the registers should read-as-0 if they aren't supported, implying, the space for them should be reserved and read as 0's.

Quoted 6 lines

So, a check & workaround seems in order....

so something like
max_cap_size = 256-base;
if (size > max_cap_size) then size=max_cap_size, fprintf warning about structure mapping-size reduction?

s/fprintf/error_report/

Also need to make sure we don't access the config array outside the allocated size anywhere. That's why I thought special-casing just the specific offset is a better idea.

Signature · 1 line
MST
#12 lore raw
In reply to Don Dutile
Quoted 38 lines · * Don Dutile ([email protected]) wrote:
Quoted 37 lines · Michael S. Tsirkin
Quoted 35 lines · Michael S. Tsirkin
Quoted 24 lines · Michael S. Tsirkin
Quoted 7 lines · Chris Wright
Quoted 6 lines · * Alex Williamson ([email protected]) wrote:
Quoted 5 lines · Chris Wright
Quoted 3 lines · * Donald Dutile ([email protected]) wrote:
(quoted diff) +3 -0
+        } else if (version == 2) {
+            /* don't include slot cap/stat/ctrl 2 regs; only support endpoints */
+            size = 0x34;
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

That doesn't look correct to me. The size is fixed, just that some registers are Reserved Zero when they do not apply (e.g. endpoint only).

Apparently it can be interpreted differently. In this case, we've seen a tg3 device expose a v2 PCI express capability at offset 0xcc. Using 0x3c bytes, we extend 8 bytes past the legacy config space area :(

Wow, that device sounds broken to me. The spec is pretty clear.

Yes, I agree it's broken. Looks like something that happens when a device is designed in parallel with the spec.

What bothers me is this patch seems to make devices that do behave correctly out of spec (registers will be writeable by default) - correct?

Quoted 6 lines

How about we check for overflow and only do the hacks if it happens?

Also, the code to initialize slot and root control registers is still
there: it would seem that running it will corrupt memmory beyond the config array?

I take this last bit back: registers we touch are at offset< 0x34. Sorry about the noise. But the question about read-only registers still stands.

Quoted 7 lines
Quoted 4 lines

Also, where does the magic 0x34 come from? I'm guessing this is simply what's left till the end of the config space. So let's be conservative specific as possible with this hack:

I believe the spec leaves room for interpretation, and thus the resulting 'broken' device. As I read the spec, the size of the struct can be:

Yeah, I can see how it might be misinterpreted, however, it's made really clear in the config space test spec. This strucuture is meant to be full size. Perhaps something like Michael suggested (and if really paranoid + pci vendor/device id to quirk it). I haven't come across many devices have this wrong.

thanks,
-chris