Skip to contentSkip to conversation

xen-hvm: stop faking I/O to access PCI config space

PATCH Superseded Paul Durrant 2 participants 5 messages May 18, 2018

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen-hvm: stop faking I/O to access PCI config space

This patch removes the current hackery where IOREQ_TYPE_PCI_CONFIG requests are handled by faking PIO to 0xcf8 and 0xcfc and replaces it with direct calls to pci_host_config_read/write_common(). Doing so necessitates mapping BDFs to PCIDevices but maintaining a simple QLIST in xen_device_realize/unrealize() will suffice.

NOTE: whilst config space accesses are currently limited to
PCI_CONFIG_SPACE_SIZE, this patch paves the way to increasing the limit to PCIE_CONFIG_SPACE_SIZE when Xen gains the ability to emulate MCFG table accesses.

--
Cc: Stefano Stabellini <[email protected]>
Cc: Anthony Perard <[email protected]>
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Marcel Apfelbaum <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Richard Henderson <[email protected]>
Cc: Eduardo Habkost <[email protected]>
Cc: Roger Pau Monne <[email protected]>

v2:
- Introduce a helper function rw_config_req_item() to handle config register offset calculation
- Handle req->count > 1 more like MMIO rather than PIO
- Added Roger to cc list (not taking R-b because of significant change)

---
 hw/i386/xen/trace-events |   2 +
 hw/i386/xen/xen-hvm.c    | 120 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 102 insertions(+), 20 deletions(-)
hw/i386/xen/trace-events +2 -0
index 8dab7bc..f576f1b 100644
@@ -15,6 +15,8 @@ cpu_ioreq_pio(void *req, uint32_t dir, uint32_t df, uint32_t data_is_ptr, uint64
 cpu_ioreq_pio_read_reg(void *req, uint64_t data, uint64_t addr, uint32_t size) "I/O=%p pio read reg data=0x%"PRIx64" port=0x%"PRIx64" size=%d"
 cpu_ioreq_pio_write_reg(void *req, uint64_t data, uint64_t addr, uint32_t size) "I/O=%p pio write reg data=0x%"PRIx64" port=0x%"PRIx64" size=%d"
 cpu_ioreq_move(void *req, uint32_t dir, uint32_t df, uint32_t data_is_ptr, uint64_t addr, uint64_t data, uint32_t count, uint32_t size) "I/O=%p copy dir=%d df=%d ptr=%d port=0x%"PRIx64" data=0x%"PRIx64" count=%d size=%d"
+cpu_ioreq_config_read(void *req, uint32_t sbdf, uint32_t reg, uint32_t size, uint32_t data) "I/O=%p sbdf=0x%x reg=%u size=%u data=0x%x"
+cpu_ioreq_config_write(void *req, uint32_t sbdf, uint32_t reg, uint32_t size, uint32_t data) "I/O=%p sbdf=0x%x reg=%u size=%u data=0x%x"
 
 # xen-mapcache.c
 xen_map_cache(uint64_t phys_addr) "want 0x%"PRIx64
hw/i386/xen/xen-hvm.c +100 -20

171 lines not on this page · raw message

Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 929

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.05.18 at 15:00, <[email protected]> wrote:
hw/i386/xen/xen-hvm.c +1 -0
@@ -903,6 +926,80 @@ static void cpu_ioreq_move(ioreq_t *req)
     }
 }
 
+static void rw_config_req_item(XenPciDevice *xendev, ioreq_t *req,

It looks to me as if both parameters could be constified.

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 936

Quoted 7 lines
hw/i386/xen/xen-hvm.c +7 -0
+                               uint32_t i, uint32_t *val)
+{
+    int32_t reg = req->addr;
+    uint32_t offset = req->size * i;
+
+    reg += (req->df ? -1 : 1) * offset;
+    if (reg < 0 || reg > PCI_CONFIG_SPACE_SIZE) {

Having fought a number of issues in this area in the hypervisor a couple of years back I wonder
- why reg is of signed type,
- whether overflow of the first multiplication really doesn't matter,
- whether wrapping when adding in the offset is not an issue.

I take it that the rather lax upper bound check (should imo really be reg + size > PCI_CONFIG_SPACE_SIZE [implying reg + size doesn't itself wrap], or at least reg >= PCI_CONFIG_SPACE_SIZE) is not a problem because ...

Quoted 9 lines
hw/i386/xen/xen-hvm.c +9 -0
+        if (req->dir == IOREQ_READ) {
+            *val = ~0u;
+        }
+        return;
+    }
+
+    if (req->dir == IOREQ_READ) {
+        *val = pci_host_config_read_common(xendev->pci_dev, reg,
+                                           PCI_CONFIG_SPACE_SIZE,

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 955

Quoted 10 lines
hw/i386/xen/xen-hvm.c +10 -0
+                                           req->size);
+        trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
+                                    req->size, *val);
+    } else {
+        trace_cpu_ioreq_config_write(req, xendev->sbdf, reg, req->size,
+                                     *val);
+        pci_host_config_write_common(xendev->pci_dev, reg,
+                                     PCI_CONFIG_SPACE_SIZE, *val,
+                                     req->size);
+    }

... these called functions do full checking anyway?

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 964

Quoted 7 lines
hw/i386/xen/xen-hvm.c +7 -0
+static void cpu_ioreq_config(XenIOState *state, ioreq_t *req)
+{
+    uint32_t sbdf = req->addr >> 32;
+    XenPciDevice *xendev;
+
+    if (req->size > sizeof(uint32_t)) {
+        hw_error("PCI config access: bad size (%u)", req->size);

What about size 0 or 3?

Quoted 12 lines
hw/i386/xen/xen-hvm.c +12 -0
+    }
+
+    QLIST_FOREACH(xendev, &state->dev_list, entry) {
+        unsigned int i;
+        uint32_t tmp;
+
+        if (xendev->sbdf != sbdf) {
+            continue;
+        }
+
+        if (!req->data_is_ptr) {
+            if (req->dir == IOREQ_READ) {

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 986

Quoted 10 lines
hw/i386/xen/xen-hvm.c +10 -0
+                for (i = 0; i < req->count; i++) {
+                    rw_config_req_item(xendev, req, i, &tmp);
+                    req->data = tmp;
+                }
+            } else if (req->dir == IOREQ_WRITE) {
+                for (i = 0; i < req->count; i++) {
+                    tmp = req->data;
+                    rw_config_req_item(xendev, req, i, &tmp);
+                }
+            }

Wouldn't it be more sensible to fail req->count != 1 requests here?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 12 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 May 2018 14:34
To: Paul Durrant <[email protected]>
Cc: Anthony Perard <[email protected]>; Roger Pau Monne <[email protected]>; Stefano Stabellini <[email protected]>; xen- devel <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Paolo Bonzini <[email protected]>; Richard Henderson <[email protected]>
Subject: Re: [Xen-devel] [PATCH v2] xen-hvm: stop faking I/O to access PCI config space

Quoted 9 lines

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 929

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 18.05.18 at 15:00, <[email protected]> wrote:
hw/i386/xen/xen-hvm.c +1 -0
@@ -903,6 +926,80 @@ static void cpu_ioreq_move(ioreq_t *req)
     }
 }
 
+static void rw_config_req_item(XenPciDevice *xendev, ioreq_t *req,

It looks to me as if both parameters could be constified.

They could for this function, yes.

Quoted 11 lines

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 936

Quoted 7 lines
hw/i386/xen/xen-hvm.c +7 -0
+                               uint32_t i, uint32_t *val)
+{
+    int32_t reg = req->addr;
+    uint32_t offset = req->size * i;
+
+    reg += (req->df ? -1 : 1) * offset;
+    if (reg < 0 || reg > PCI_CONFIG_SPACE_SIZE) {

Having fought a number of issues in this area in the hypervisor a couple of years back I wonder
- why reg is of signed type,

I did that so I could do a < 0 check.

Quoted 1 line

- whether overflow of the first multiplication really doesn't matter,

It would be better to check it.

Quoted 2 lines

- whether wrapping when adding in the offset is not an issue.

I'll do limits check on offset then... should be able to make reg unsigned then I guess.

Quoted 25 lines

I take it that the rather lax upper bound check (should imo really be reg + size > PCI_CONFIG_SPACE_SIZE [implying reg + size doesn't itself wrap], or at least reg >= PCI_CONFIG_SPACE_SIZE) is not a problem because ...

Quoted 9 lines
hw/i386/xen/xen-hvm.c +9 -0
+        if (req->dir == IOREQ_READ) {
+            *val = ~0u;
+        }
+        return;
+    }
+
+    if (req->dir == IOREQ_READ) {
+        *val = pci_host_config_read_common(xendev->pci_dev, reg,
+                                           PCI_CONFIG_SPACE_SIZE,

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 955

Quoted 10 lines
hw/i386/xen/xen-hvm.c +10 -0
+                                           req->size);
+        trace_cpu_ioreq_config_read(req, xendev->sbdf, reg,
+                                    req->size, *val);
+    } else {
+        trace_cpu_ioreq_config_write(req, xendev->sbdf, reg, req->size,
+                                     *val);
+        pci_host_config_write_common(xendev->pci_dev, reg,
+                                     PCI_CONFIG_SPACE_SIZE, *val,
+                                     req->size);
+    }
Quoted 1 line

... these called functions do full checking anyway?

Yes, I'm deferring further checking to these common functions. I'm only intending to avoid passing junk into them here.

Quoted 11 lines

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 964

Quoted 8 lines
hw/i386/xen/xen-hvm.c +7 -0
+static void cpu_ioreq_config(XenIOState *state, ioreq_t *req)
+{
+    uint32_t sbdf = req->addr >> 32;
+    XenPciDevice *xendev;
+
+    if (req->size > sizeof(uint32_t)) {
+        hw_error("PCI config access: bad size (%u)", req->size);

What about size 0 or 3?

Yes, I can reject those here also.

Quoted 16 lines
Quoted 16 lines
hw/i386/xen/xen-hvm.c +16 -0
+    }
+
+    QLIST_FOREACH(xendev, &state->dev_list, entry) {
+        unsigned int i;
+        uint32_t tmp;
+
+        if (xendev->sbdf != sbdf) {
+            continue;
+        }
+
+        if (!req->data_is_ptr) {
+            if (req->dir == IOREQ_READ) {
+                for (i = 0; i < req->count; i++) {
+                    rw_config_req_item(xendev, req, i, &tmp);
+                    req->data = tmp;
+                }
Quoted 9 lines

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 986

Quoted 6 lines
hw/i386/xen/xen-hvm.c +6 -0
+            } else if (req->dir == IOREQ_WRITE) {
+                for (i = 0; i < req->count; i++) {
+                    tmp = req->data;
+                    rw_config_req_item(xendev, req, i, &tmp);
+                }
+            }

Wouldn't it be more sensible to fail req->count != 1 requests here?

I'm wondering whether we'd want to handle count > 1 once we allow MMCONFIG accesses though. I guess it would be easier just to defer that.

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 28 lines
Quoted 18 lines
Quoted 1 line · On 18.05.18 at 15:51, <[email protected]> wrote:

Sent: 18 May 2018 14:34
To: Paul Durrant <[email protected]>

Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 18.05.18 at 15:00, <[email protected]> wrote:
hw/i386/xen/xen-hvm.c +14 -0
+    QLIST_FOREACH(xendev, &state->dev_list, entry) {
+        unsigned int i;
+        uint32_t tmp;
+
+        if (xendev->sbdf != sbdf) {
+            continue;
+        }
+
+        if (!req->data_is_ptr) {
+            if (req->dir == IOREQ_READ) {
+                for (i = 0; i < req->count; i++) {
+                    rw_config_req_item(xendev, req, i, &tmp);
+                    req->data = tmp;
+                }
Quoted 9 lines

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 986

Quoted 6 lines
hw/i386/xen/xen-hvm.c +6 -0
+            } else if (req->dir == IOREQ_WRITE) {
+                for (i = 0; i < req->count; i++) {
+                    tmp = req->data;
+                    rw_config_req_item(xendev, req, i, &tmp);
+                }
+            }

Wouldn't it be more sensible to fail req->count != 1 requests here?

Quoted 2 lines

I'm wondering whether we'd want to handle count > 1 once we allow MMCONFIG accesses though. I guess it would be easier just to defer that.

For the data_is_ptr case - sure. But here? Or wait - are you thinking about REP STOS (and the relatively useless REP LODS)?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 40 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 18 May 2018 15:16
To: Paul Durrant <[email protected]>
Cc: Anthony Perard <[email protected]>; Roger Pau Monne <[email protected]>; Stefano Stabellini <[email protected]>; xen- devel <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Paolo Bonzini <[email protected]>; Richard Henderson <[email protected]>
Subject: RE: [Xen-devel] [PATCH v2] xen-hvm: stop faking I/O to access PCI config space

Quoted 28 lines
Quoted 27 lines
Quoted 1 line · On 18.05.18 at 15:51, <[email protected]> wrote:

Sent: 18 May 2018 14:34
To: Paul Durrant <[email protected]>

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 18.05.18 at 15:00, <[email protected]> wrote:
hw/i386/xen/xen-hvm.c +10 -0
+    QLIST_FOREACH(xendev, &state->dev_list, entry) {
+        unsigned int i;
+        uint32_t tmp;
+
+        if (xendev->sbdf != sbdf) {
+            continue;
+        }
+
+        if (!req->data_is_ptr) {
+            if (req->dir == IOREQ_READ) {

hw/i386/xen/xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 986

Quoted 10 lines
hw/i386/xen/xen-hvm.c +10 -0
+                for (i = 0; i < req->count; i++) {
+                    rw_config_req_item(xendev, req, i, &tmp);
+                    req->data = tmp;
+                }
+            } else if (req->dir == IOREQ_WRITE) {
+                for (i = 0; i < req->count; i++) {
+                    tmp = req->data;
+                    rw_config_req_item(xendev, req, i, &tmp);
+                }
+            }

Wouldn't it be more sensible to fail req->count != 1 requests here?

Quoted 7 lines
Quoted 1 line

I'm wondering whether we'd want to handle count > 1 once we allow

MMCONFIG

Quoted 1 line

accesses though. I guess it would be easier just to defer that.

For the data_is_ptr case - sure. But here? Or wait - are you thinking about REP STOS (and the relatively useless REP LODS)?

Yes. We'd need to cope with a rep stos if we had memory mapped access, but we don't need to worry about it until then I think.

In the meantime I doubt any well behaved OS is going to do rep ins or rep outs to cfc so just aborting on count > 1 is probably fine.

Paul

Quoted 2 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel