Skip to contentSkip to conversation

vpci: PCI config space emulation

PATCH Superseded Roger Pau Monne 6 participants 51 messages Sep 19, 2017 – Jan 6, 2018

v6 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

v6 cover vpci: PCI config space emulation

Hello,

The following series contain an implementation of handlers for the PCI configuration space inside of Xen. This allows Xen to detect accesses to the PCI configuration space and react accordingly.

Why is this needed? IMHO, there are two main points of doing all this emulation inside of Xen, the first one is to prevent adding a bunch of duplicated Xen PV specific code to each OS we want to support in PVH mode. This just promotes Xen code duplication amongst OSes, which leads to a higher maintainership burden.

The second reason would be that this code (or it's functionality to be more precise) already exists in QEMU (and pciback to a degree), and it's code that we already support and maintain. By moving it into the hypervisor itself every guest type can make use of it, and should be shared between them all. I know that the code in this series is not yet suitable for DomU HVM guests in it's current state, but it should be in due time.

As usual, each patch contains a changeset summary between versions, I'm not going to copy the list of changes here.

Patch 1 modifies a function to decode a PCI IO port access into pci_sbdf_t and register (which is shared with the ioreq code). Patch 2 implements the generic handlers for accesses to the PCI configuration space together with a minimal user-space test harness that I've used during development. Currently a per-device linked list is used in order to store the list of handlers, and they are sorted based on their offset inside of the configuration space. Patch 2 also adds the x86 port IO traps and wires them into the newly introduced vPCI dispatchers. Patch 3 and 4 adds handlers for the MMCFG areas (as found on the MMCFG ACPI table). Patches 5, 6 and 7 are mostly code moment/refactoring in order to implement support for BAR mapping in patch 8. Finally patches 9 and 11 add support for trapping accesses to the MSI and MSI-X capabilities respectively, so that interrupts are properly setup on behalf of Dom0.

The most noticeable functional difference from previous versions is that this version supports preemptive BAR mapping and unmapping.

The branch containing the patches can be found at:

git://xenbits.xen.org/people/royger/xen.git vpci_v6

Note that this is only safe to use for the hardware domain (that's trusted), any non-trusted domain will need a lot more of traps before it can freely access the PCI configuration space.

Thanks, Roger.

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

#1 lore raw

PATCH v6 1/11 pci: introduce a type to store a SBDF

That provides direct access to all the members that constitute a SBDF. The only function switched to use it is hvm_pci_decode_addr, because it makes following patches simpler.

---
Cc: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v5:
- New in this version.

---
 xen/arch/x86/hvm/io.c        | 10 ++--------
 xen/arch/x86/hvm/ioreq.c     | 12 +++++-------
 xen/include/asm-x86/hvm/io.h |  4 ++--
 xen/include/xen/pci.h        | 20 ++++++++++++++++++++
 4 files changed, 29 insertions(+), 17 deletions(-)
xen/arch/x86/hvm/io.c +2 -8
index bf41954f59..4e49e59012 100644
@@ -257,17 +257,11 @@ void register_g2m_portio_handler(struct domain *d)
 }
 
 unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
-                                 unsigned int *bus, unsigned int *slot,
-                                 unsigned int *func)
+                                 pci_sbdf_t *bdf)
 {
-    unsigned int bdf;
-
     ASSERT(CF8_ENABLED(cf8));
 
-    bdf = CF8_BDF(cf8);
-    *bus = PCI_BUS(bdf);
-    *slot = PCI_SLOT(bdf);
-    *func = PCI_FUNC(bdf);
+    bdf->sbdf = CF8_BDF(cf8);
     /*
      * NB: the lower 2 bits of the register address are fetched from the
      * offset into the 0xcfc register when reading/writing to it.
xen/arch/x86/hvm/ioreq.c +5 -7
index 752976d16d..3e7a88e053 100644
@@ -1177,17 +1177,15 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
          (p->addr & ~3) == 0xcfc &&
          CF8_ENABLED(cf8) )
     {
-        uint32_t sbdf, x86_fam;
-        unsigned int bus, slot, func, reg;
+        uint32_t x86_fam;
+        pci_sbdf_t bdf;
+        unsigned int reg;
 
-        reg = hvm_pci_decode_addr(cf8, p->addr, &bus, &slot, &func);
+        reg = hvm_pci_decode_addr(cf8, p->addr, &bdf);
 
         /* PCI config data cycle */
-
-        sbdf = XEN_DMOP_PCI_SBDF(0, bus, slot, func);
-
         type = XEN_DMOP_IO_RANGE_PCI;
-        addr = ((uint64_t)sbdf << 32) | reg;
+        addr = ((uint64_t)bdf.bdf << 32) | reg;
         /* AMD extended configuration space access? */
         if ( CF8_ADDR_HI(cf8) &&
              d->arch.cpuid->x86_vendor == X86_VENDOR_AMD &&
xen/include/asm-x86/hvm/io.h +2 -2
index 51659b6c7f..2ff1c96883 100644
@@ -20,6 +20,7 @@
 #define __ASM_X86_HVM_IO_H__
 
 #include <xen/mm.h>
+#include <xen/pci.h>
 #include <asm/hvm/vpic.h>
 #include <asm/hvm/vioapic.h>
 #include <public/hvm/ioreq.h>
@@ -151,8 +152,7 @@ extern void hvm_dpci_msi_eoi(struct domain *d, int vector);
 
 /* Decode a PCI port IO access into a bus/slot/func/reg. */
 unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
-                                 unsigned int *bus, unsigned int *slot,
-                                 unsigned int *func);
+                                 pci_sbdf_t *bdf);
 
 /*
  * HVM port IO handler that performs forwarding of guest IO ports into machine
xen/include/xen/pci.h +20 -0
index 43f21251a5..dd5ec43a70 100644
@@ -38,6 +38,26 @@
 #define PCI_SBDF2(s,bdf) ((((s) & 0xffff) << 16) | ((bdf) & 0xffff))
 #define PCI_SBDF3(s,b,df) ((((s) & 0xffff) << 16) | PCI_BDF2(b, df))
 
+typedef union {
+    uint32_t sbdf;
+    struct {
+        union {
+            uint16_t bdf;
+            struct {
+                union {
+                    struct {
+                        uint8_t func : 3,
+                                dev  : 5;
+                    };
+                    uint8_t     extfunc;
+                };
+                uint8_t         bus;
+            };
+        };
+        uint16_t                seg;
+    };
+} pci_sbdf_t;
+
 struct pci_dev_info {
     /*
      * VF's 'is_extfn' field is used to indicate whether its PF is an extended
Signature · 7 lines
2.11.0 (Apple Git-81)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v6 1/11
#2 lore raw

Re: [PATCH v6 01/11] pci: introduce a type to store a SBDF

Quoted 44 lines

-----Original Message-----
From: Roger Pau Monne [mailto:[email protected]]
Sent: 19 September 2017 16:29
To: [email protected]
Cc: [email protected]; [email protected]; Roger Pau Monne <[email protected]>; Paul Durrant <[email protected]>; Jan Beulich <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; Tim (Xen.org) <[email protected]>; Wei Liu <[email protected]>
Subject: [PATCH v6 01/11] pci: introduce a type to store a SBDF

That provides direct access to all the members that constitute a SBDF. The only function switched to use it is hvm_pci_decode_addr, because it makes following patches simpler.

---
Cc: Paul Durrant <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v5:
- New in this version.

---
 xen/arch/x86/hvm/io.c        | 10 ++--------
 xen/arch/x86/hvm/ioreq.c     | 12 +++++-------
 xen/include/asm-x86/hvm/io.h |  4 ++--
 xen/include/xen/pci.h        | 20 ++++++++++++++++++++
 4 files changed, 29 insertions(+), 17 deletions(-)
xen/arch/x86/hvm/io.c +0 -0
index bf41954f59..4e49e59012 100644

xen/arch/x86/hvm/io.c void register_g2m_portio_handler(struct domain · line 261

Quoted 7 lines
xen/arch/x86/hvm/io.c +1 -2
@@ -257,17 +257,11 @@ void register_g2m_portio_handler(struct domain
 *d)
 }
 
 unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
-                                 unsigned int *bus, unsigned int *slot,
-                                 unsigned int *func)
+                                 pci_sbdf_t *bdf)

I'd prefer the pointer name to be 'sbdf' rather than 'bdf', but otherwise...

Quoted 97 lines
xen/arch/x86/hvm/io.c +1 -6
 {
-    unsigned int bdf;
-
     ASSERT(CF8_ENABLED(cf8));
 
-    bdf = CF8_BDF(cf8);
-    *bus = PCI_BUS(bdf);
-    *slot = PCI_SLOT(bdf);
-    *func = PCI_FUNC(bdf);
+    bdf->sbdf = CF8_BDF(cf8);
     /*
      * NB: the lower 2 bits of the register address are fetched from the

* offset into the 0xcfc register when reading/writing to it.

xen/arch/x86/hvm/ioreq.c +5 -7
index 752976d16d..3e7a88e053 100644
@@ -1177,17 +1177,15 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
          (p->addr & ~3) == 0xcfc &&
          CF8_ENABLED(cf8) )
     {
-        uint32_t sbdf, x86_fam;
-        unsigned int bus, slot, func, reg;
+        uint32_t x86_fam;
+        pci_sbdf_t bdf;
+        unsigned int reg;
 
-        reg = hvm_pci_decode_addr(cf8, p->addr, &bus, &slot, &func);
+        reg = hvm_pci_decode_addr(cf8, p->addr, &bdf);
 
         /* PCI config data cycle */
-
-        sbdf = XEN_DMOP_PCI_SBDF(0, bus, slot, func);
-
         type = XEN_DMOP_IO_RANGE_PCI;
-        addr = ((uint64_t)sbdf << 32) | reg;
+        addr = ((uint64_t)bdf.bdf << 32) | reg;
         /* AMD extended configuration space access? */
         if ( CF8_ADDR_HI(cf8) &&
              d->arch.cpuid->x86_vendor == X86_VENDOR_AMD &&
xen/include/asm-x86/hvm/io.h +2 -2
index 51659b6c7f..2ff1c96883 100644
@@ -20,6 +20,7 @@
 #define __ASM_X86_HVM_IO_H__
 
 #include <xen/mm.h>
+#include <xen/pci.h>
 #include <asm/hvm/vpic.h>
 #include <asm/hvm/vioapic.h>
 #include <public/hvm/ioreq.h>
@@ -151,8 +152,7 @@ extern void hvm_dpci_msi_eoi(struct domain *d, int
 vector);
 
 /* Decode a PCI port IO access into a bus/slot/func/reg. */
 unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
-                                 unsigned int *bus, unsigned int *slot,
-                                 unsigned int *func);
+                                 pci_sbdf_t *bdf);
 
 /*

* HVM port IO handler that performs forwarding of guest IO ports into
machine

xen/include/xen/pci.h +20 -1
index 43f21251a5..dd5ec43a70 100644
@@ -38,6 +38,26 @@
 #define PCI_SBDF2(s,bdf) ((((s) & 0xffff) << 16) | ((bdf) & 0xffff))
 #define PCI_SBDF3(s,b,df) ((((s) & 0xffff) << 16) | PCI_BDF2(b, df))
 
+typedef union {
+    uint32_t sbdf;
+    struct {
+        union {
+            uint16_t bdf;
+            struct {
+                union {
+                    struct {
+                        uint8_t func : 3,
+                                dev  : 5;
+                    };
+                    uint8_t     extfunc;
+                };
+                uint8_t         bus;
+            };
+        };
+        uint16_t                seg;
+    };
+} pci_sbdf_t;
+
 struct pci_dev_info {
     /*
      * VF's 'is_extfn' field is used to indicate whether its PF is an extended
--
Quoted 1 line

2.11.0 (Apple Git-81)

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

#3 lore raw

Re: [PATCH v6 01/11] pci: introduce a type to store a SBDF

Quoted 6 lines
Quoted 6 lines
Quoted 1 line · On 19.09.17 at 17:40, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +0 -0
@@ -257,17 +257,11 @@ void register_g2m_portio_handler(struct domain *d)
 }
 
Quoted 6 lines

xen/arch/x86/hvm/io.c void register_g2m_portio_handler(struct domain *d) · line 260

Quoted 4 lines
xen/arch/x86/hvm/io.c +1 -2
 unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
-                                 unsigned int *bus, unsigned int *slot,
-                                 unsigned int *func)
+                                 pci_sbdf_t *bdf)

I'd prefer the pointer name to be 'sbdf' rather than 'bdf', but otherwise...

Indeed. Or have a sub-type "struct pci_bdf_t", as the segment (sadly) isn't relevant yet.

Quoted 10 lines

xen/arch/x86/hvm/io.c void register_g2m_portio_handler(struct domain *d) · line 264

Quoted 10 lines
xen/arch/x86/hvm/io.c +1 -6
 {
-    unsigned int bdf;
-
     ASSERT(CF8_ENABLED(cf8));
 
-    bdf = CF8_BDF(cf8);
-    *bus = PCI_BUS(bdf);
-    *slot = PCI_SLOT(bdf);
-    *func = PCI_FUNC(bdf);
+    bdf->sbdf = CF8_BDF(cf8);

Filling ->bdf here and setting ->seg explicitly with zero may also make the current limitation more obvious.

Quoted 16 lines
Quoted 16 lines
xen/arch/x86/hvm/ioreq.c +4 -3
@@ -1177,17 +1177,15 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
          (p->addr & ~3) == 0xcfc &&
          CF8_ENABLED(cf8) )
     {
-        uint32_t sbdf, x86_fam;
-        unsigned int bus, slot, func, reg;
+        uint32_t x86_fam;
+        pci_sbdf_t bdf;
+        unsigned int reg;
 
-        reg = hvm_pci_decode_addr(cf8, p->addr, &bus, &slot, &func);
+        reg = hvm_pci_decode_addr(cf8, p->addr, &bdf);
 
Quoted 7 lines

xen/arch/x86/hvm/ioreq.c struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, · line 1188

Quoted 7 lines
xen/arch/x86/hvm/ioreq.c +1 -4
         /* PCI config data cycle */
-
-        sbdf = XEN_DMOP_PCI_SBDF(0, bus, slot, func);
-
         type = XEN_DMOP_IO_RANGE_PCI;
-        addr = ((uint64_t)sbdf << 32) | reg;
+        addr = ((uint64_t)bdf.bdf << 32) | reg;

I also wonder why the field used here is bdf instead of sbdf. It would make for less future changes if you used .sbdf here right away.

Jan

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

#4 lore raw

PATCH v6 2/11 vpci: introduce basic handlers to trap accesses to the PCI config space

In reply to Roger Pau Monne

This functionality is going to reside in vpci.c (and the corresponding vpci.h header), and should be arch-agnostic. The handlers introduced in this patch setup the basic functionality required in order to trap accesses to the PCI config space, and allow decoding the address and finding the corresponding handler that should handle the access (although no handlers are implemented).

Note that the traps to the PCI IO ports registers (0xcf8/0xcfc) are setup inside of a x86 HVM file, since that's not shared with other arches.

A new XEN_X86_EMU_VPCI x86 domain flag is added in order to signal Xen whether a domain should use the newly introduced vPCI handlers, this is only enabled for PVH Dom0 at the moment.

A very simple user-space test is also provided, so that the basic functionality of the vPCI traps can be asserted. This has been proven quite helpful during development, since the logic to handle partial accesses or accesses that expand across multiple registers is not trivial.

The handlers for the registers are added to a linked list that's keep sorted at all times. Both the read and write handlers support accesses that expand across multiple emulated registers and contain gaps not emulated.

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v5:
- Use a spinlock per pci device.
- Use the recently introduced pci_sbdf_t type.
- Fix test harness to use the right handler type and the newly introduced lock.
- Move the position of the vpci sections in the linker scripts.
- Constify domain and pci_dev in vpci_{read/write}.
- Fix typos in comments.
- Use _XEN_VPCI_H_ as header guard.

Changes since v4:
* User-space test harness:
- Do not redirect the output of the test.
- Add main.c and emul.h as dependencies of the Makefile target.
- Use the same rule to modify the vpci and list headers.
- Remove underscores from local macro variables.
- Add _check suffix to the test harness multiread function.
- Change the value written by every different size in the multiwrite test.
- Use { } to initialize the r16 and r20 arrays (instead of { 0 }).
- Perform some of the read checks with the local variable directly.
- Expand some comments.
- Implement a dummy rwlock.
* Hypervisor code:
- Guard the linker script changes with CONFIG_HAS_PCI.
- Rename vpci_access_check to vpci_access_allowed and make it return bool.
- Make hvm_pci_decode_addr return the register as return value.
- Use ~3 instead of 0xfffc to remove the register offset when checking accesses to IO ports.
- s/head/prev in vpci_add_register.
- Add parentheses around & in vpci_add_register.
- Fix register removal.
- Change the BUGs in vpci_{read/write}_hw helpers to ASSERT_UNREACHABLE.
- Make merge_result static and change the computation of the mask to avoid using a uint64_t.
- Modify vpci_read to only read from hardware the not-emulated gaps.
- Remove the vpci_val union and use a uint32_t instead.
- Change handler read type to return a uint32_t instead of modifying a variable passed by reference.
- Constify the data opaque parameter of read handlers.
- Change the size parameter of the vpci_{read/write} functions to unsigned int.
- Place the array of initialization handlers in init.rodata or .rodata depending on whether late-hwdom is enabled.
- Remove the pci_devs lock, assume the Dom0 is well behaved and won't remove the device while trying to access it.
- Change the recursive spinlock into a rw lock for performance reasons.

Changes since v3:
* User-space test harness:
- Fix spaces in container_of macro.
- Implement a dummy locking functions.
- Remove 'current' macro make current a pointer to the statically allocated vpcu.
- Remove unneeded parentheses in the pci_conf_readX macros.
- Fix the name of the write test macro.
- Remove the dummy EXPORT_SYMBOL macro (this was needed by the RB code only).
- Import the max macro.
- Test all possible read/write size combinations with all possible emulated register sizes.
- Introduce a test for register removal.
* Hypervisor code:
- Use a sorted list in order to store the config space handlers.
- Remove some unneeded 'else' branches.
- Make the IO port handlers always return X86EMUL_OKAY, and set the data to all 1's in case of read failure (write are simply ignored).
- In hvm_select_ioreq_server reuse local variables when calling XEN_DMOP_PCI_SBDF.
- Store the pointers to the initialization functions in the .rodata section.
- Do not ignore the return value of xen_vpci_add_handlers in setup_one_hwdom_device.
- Remove the vpci_init macro.
- Do not hide the pointers inside of the vpci_{read/write}_t typedefs.
- Rename priv_data to private in vpci_register.
- Simplify checking for register overlap in vpci_register_cmp.
- Check that the offset and the length match before removing a register in xen_vpci_remove_register.
- Make vpci_read_hw return a value rather than storing it in a pointer passed by parameter.
- Handler dispatcher functions vpci_{read/write} no longer return an error code, errors on reads/writes should be treated like hardware (writes ignored, reads return all 1's or garbage).
- Make sure pcidevs is locked before calling pci_get_pdev_by_domain.
- Use a recursive spinlock for the vpci lock, so that spin_is_locked checks that the current CPU is holding the lock.
- Make the code less error-chatty by removing some of the printk's.
- Pass the slot and the function as separate parameters to the handler dispatchers (instead of passing devfn).
- Allow handlers to be registered with either a read or write function only, the missing handler will be replaced by a dummy handler (writes ignored, reads return 1's).
- Introduce PCI_CFG_SPACE_* defines from Linux.
- Simplify the handler dispatchers by removing the recursion, now the dispatchers iterate over the list of sorted handlers and call them in order.
- Remove the GENMASK_BYTES, SHIFT_RIGHT_BYTES and ADD_RESULT macros, and instead provide a merge_result function in order to merge a register output into a partial result.
- Rename the fields of the vpci_val union to u8/u16/u32.
- Remove the return values from the read/write handlers, errors should be handled internally and signaled as would be done on native hardware.
- Remove the usage of the GENMASK macro.

Changes since v2:
- Generalize the PCI address decoding and use it for IOREQ code also.

Changes since v1:
- Allow access to cross a word-boundary.
- Add locking.
- Add cleanup to xen_vpci_add_handlers in case of failure.

---
 .gitignore                        |   3 +
 tools/libxl/libxl_x86.c           |   2 +-
 tools/tests/Makefile              |   1 +
 tools/tests/vpci/Makefile         |  37 ++++
 tools/tests/vpci/emul.h           | 133 +++++++++++
 tools/tests/vpci/main.c           | 308 ++++++++++++++++++++++++++
 xen/arch/arm/xen.lds.S            |  12 +
 xen/arch/x86/domain.c             |  18 +-
 xen/arch/x86/hvm/hvm.c            |   2 +
 xen/arch/x86/hvm/io.c             | 103 +++++++++
 xen/arch/x86/setup.c              |   3 +-
 xen/arch/x86/xen.lds.S            |  12 +
 xen/drivers/Makefile              |   2 +-
 xen/drivers/passthrough/pci.c     |   9 +-
 xen/drivers/vpci/Makefile         |   1 +
 xen/drivers/vpci/vpci.c           | 450 ++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h      |   1 +
 xen/include/asm-x86/hvm/io.h      |   3 +
 xen/include/public/arch-x86/xen.h |   5 +-
 xen/include/xen/pci.h             |   3 +
 xen/include/xen/pci_regs.h        |   8 +
 xen/include/xen/vpci.h            |  54 +++++
 22 files changed, 1161 insertions(+), 9 deletions(-)
 create mode 100644 tools/tests/vpci/Makefile
 create mode 100644 tools/tests/vpci/emul.h
 create mode 100644 tools/tests/vpci/main.c
 create mode 100644 xen/drivers/vpci/Makefile
 create mode 100644 xen/drivers/vpci/vpci.c
 create mode 100644 xen/include/xen/vpci.h
.gitignore +3 -0
index cc16649457..1c670b27d7 100644
@@ -240,6 +240,9 @@ tools/tests/regression/build/*
 tools/tests/regression/downloads/*
 tools/tests/mem-sharing/memshrtool
 tools/tests/mce-test/tools/xen-mceinj
+tools/tests/vpci/list.h
+tools/tests/vpci/vpci.[hc]
+tools/tests/vpci/test_vpci
 tools/xcutils/lsevtchn
 tools/xcutils/readnotes
 tools/xenbackendd/_paths.h
tools/libxl/libxl_x86.c +1 -1
index 455f6f0bed..dd7fc78a99 100644
@@ -11,7 +11,7 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
     if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM) {
         if (d_config->b_info.device_model_version !=
             LIBXL_DEVICE_MODEL_VERSION_NONE) {
-            xc_config->emulation_flags = XEN_X86_EMU_ALL;
+            xc_config->emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);
         } else if (libxl_defbool_val(d_config->b_info.u.hvm.apic)) {
             /*
              * HVM guests without device model may want
tools/tests/Makefile +1 -0
index 7162945121..f6942a93fb 100644
@@ -13,6 +13,7 @@ endif
 SUBDIRS-$(CONFIG_X86) += x86_emulator
 SUBDIRS-y += xen-access
 SUBDIRS-y += xenstore
+SUBDIRS-$(CONFIG_HAS_PCI) += vpci
 
 .PHONY: all clean install distclean uninstall
 all clean distclean: %: subdirs-%
tools/tests/vpci/Makefile new file +37 -0
new file mode 100644
index 0000000000..e45fcb5cd9
@@ -0,0 +1,37 @@
+XEN_ROOT=$(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGET := test_vpci
+
+.PHONY: all
+all: $(TARGET)
+
+.PHONY: run
+run: $(TARGET)
+	./$(TARGET)
+
+$(TARGET): vpci.c vpci.h list.h main.c emul.h
+	$(HOSTCC) -g -o $@ vpci.c main.c
+
+.PHONY: clean
+clean:
+	rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h
+
+.PHONY: distclean
+distclean: clean
+
+.PHONY: install
+install:
+
+vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c
+	# Trick the compiler so it doesn't complain about missing symbols
+	sed -e '/#include/d' \
+	    -e '1s;^;#include "emul.h"\
+	             vpci_register_init_t *const __start_vpci_array[1]\;\
+	             vpci_register_init_t *const __end_vpci_array[1]\;\
+	             ;' <$< >$@
+
+list.h: $(XEN_ROOT)/xen/include/xen/list.h
+vpci.h: $(XEN_ROOT)/xen/include/xen/vpci.h
+list.h vpci.h:
+	sed -e '/#include/d' <$< >$@
tools/tests/vpci/emul.h new file +133 -0

133 lines not on this page · raw message

tools/tests/vpci/main.c new file +308 -0

308 lines not on this page · raw message

xen/arch/arm/xen.lds.S +12 -0
index c9b9546435..eb14909645 100644
@@ -65,6 +65,12 @@ SECTIONS
        __param_start = .;
        *(.data.param)
        __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
 #if defined(BUILD_ID)
@@ -173,6 +179,12 @@ SECTIONS
        *(.init_array)
        *(SORT(.init_array.*))
        __ctors_end = .;
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
   __init_end_efi = .;
   . = ALIGN(STACK_SIZE);
xen/arch/x86/domain.c +14 -4
index dbddc536d3..898bb746ac 100644
@@ -382,11 +382,21 @@ static bool emulation_flags_ok(const struct domain *d, uint32_t emflags)
     if ( is_hvm_domain(d) )
     {
         if ( is_hardware_domain(d) &&
-             emflags != (XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC) )
-            return false;
-        if ( !is_hardware_domain(d) && emflags &&
-             emflags != XEN_X86_EMU_ALL && emflags != XEN_X86_EMU_LAPIC )
+             emflags != (XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC|
+                         XEN_X86_EMU_VPCI) )
             return false;
+        if ( !is_hardware_domain(d) )
+        {
+            switch ( emflags )
+            {
+            case XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI:
+            case XEN_X86_EMU_LAPIC:
+            case 0:
+                break;
+            default:
+                return false;
+            }
+        }
     }
     else if ( emflags != 0 && emflags != XEN_X86_EMU_PIT )
     {
xen/arch/x86/hvm/hvm.c +2 -0
index 6cb903def5..cc73df8dc7 100644
@@ -36,6 +36,7 @@
 #include <xen/rangeset.h>
 #include <xen/monitor.h>
 #include <xen/warning.h>
+#include <xen/vpci.h>
 #include <asm/shadow.h>
 #include <asm/hap.h>
 #include <asm/current.h>
@@ -629,6 +630,7 @@ int hvm_domain_initialise(struct domain *d, unsigned long domcr_flags,
         d->arch.hvm_domain.io_bitmap = hvm_io_bitmap;
 
     register_g2m_portio_handler(d);
+    register_vpci_portio_handler(d);
 
     hvm_ioreq_init(d);
 
xen/arch/x86/hvm/io.c +103 -0

115 lines not on this page · raw message

xen/arch/x86/setup.c +2 -1
index 3cbe305202..1bb2dbd31c 100644
@@ -1581,7 +1581,8 @@ void __init noreturn __start_xen(unsigned long mbi_p)
         domcr_flags |= DOMCRF_hvm |
                        ((hvm_funcs.hap_supported && !opt_dom0_shadow) ?
                          DOMCRF_hap : 0);
-        config.emulation_flags = XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC;
+        config.emulation_flags = XEN_X86_EMU_LAPIC|XEN_X86_EMU_IOAPIC|
+                                 XEN_X86_EMU_VPCI;
     }
 
     /* Create initial domain 0. */
xen/arch/x86/xen.lds.S +12 -0
index d5e8821d41..61775953d6 100644
@@ -124,6 +124,12 @@ SECTIONS
        __param_start = .;
        *(.data.param)
        __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
 #if defined(BUILD_ID)
@@ -213,6 +219,12 @@ SECTIONS
        *(.init_array)
        *(SORT(.init_array.*))
        __ctors_end = .;
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
 #ifdef EFI
xen/drivers/Makefile +1 -1
index 19391802a8..d51c766453 100644
@@ -1,6 +1,6 @@
 subdir-y += char
 subdir-$(CONFIG_HAS_CPUFREQ) += cpufreq
-subdir-$(CONFIG_HAS_PCI) += pci
+subdir-$(CONFIG_HAS_PCI) += pci vpci
 subdir-$(CONFIG_HAS_PASSTHROUGH) += passthrough
 subdir-$(CONFIG_ACPI) += acpi
 subdir-$(CONFIG_VIDEO) += video
xen/drivers/passthrough/pci.c +8 -1
index 469dfc6c3d..975485fe05 100644
@@ -31,6 +31,7 @@
 #include <xen/radix-tree.h>
 #include <xen/softirq.h>
 #include <xen/tasklet.h>
+#include <xen/vpci.h>
 #include <xsm/xsm.h>
 #include <asm/msi.h>
 #include "ats.h"
@@ -1052,9 +1053,10 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
                                                 struct pci_dev *pdev)
 {
     u8 devfn = pdev->devfn;
+    int err;
 
     do {
-        int err = ctxt->handler(devfn, pdev);
+        err = ctxt->handler(devfn, pdev);
 
         if ( err )
         {
@@ -1067,6 +1069,11 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
         devfn += pdev->phantom_stride;
     } while ( devfn != pdev->devfn &&
               PCI_SLOT(devfn) == PCI_SLOT(pdev->devfn) );
+
+    err = vpci_add_handlers(pdev);
+    if ( err )
+        printk(XENLOG_ERR "setup of vPCI for d%d failed: %d\n",
+               ctxt->d->domain_id, err);
 }
 
 static int __hwdom_init _setup_hwdom_pci_devices(struct pci_seg *pseg, void *arg)
xen/drivers/vpci/Makefile new file +1 -0
new file mode 100644
index 0000000000..840a906470
@@ -0,0 +1 @@
+obj-y += vpci.o
xen/drivers/vpci/vpci.c new file +450 -0

450 lines not on this page · raw message

xen/include/asm-x86/domain.h +1 -0
index fb8bf17458..695418bd74 100644
@@ -428,6 +428,7 @@ struct arch_domain
 #define has_vpit(d)        (!!((d)->arch.emulation_flags & XEN_X86_EMU_PIT))
 #define has_pirq(d)        (!!((d)->arch.emulation_flags & \
                             XEN_X86_EMU_USE_PIRQ))
+#define has_vpci(d)        (!!((d)->arch.emulation_flags & XEN_X86_EMU_VPCI))
 
 #define has_arch_pdevs(d)    (!list_empty(&(d)->arch.pdev_list))
 
xen/include/asm-x86/hvm/io.h +3 -0
index 2ff1c96883..f68aed9323 100644
@@ -160,6 +160,9 @@ unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
  */
 void register_g2m_portio_handler(struct domain *d);
 
+/* HVM port IO handler for vPCI accesses. */
+void register_vpci_portio_handler(struct domain *d);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/include/public/arch-x86/xen.h +4 -1
index ff918310f6..06ef4772cd 100644
@@ -293,12 +293,15 @@ struct xen_arch_domainconfig {
 #define XEN_X86_EMU_PIT             (1U<<_XEN_X86_EMU_PIT)
 #define _XEN_X86_EMU_USE_PIRQ       9
 #define XEN_X86_EMU_USE_PIRQ        (1U<<_XEN_X86_EMU_USE_PIRQ)
+#define _XEN_X86_EMU_VPCI           10
+#define XEN_X86_EMU_VPCI            (1U<<_XEN_X86_EMU_VPCI)
 
 #define XEN_X86_EMU_ALL             (XEN_X86_EMU_LAPIC | XEN_X86_EMU_HPET |  \
                                      XEN_X86_EMU_PM | XEN_X86_EMU_RTC |      \
                                      XEN_X86_EMU_IOAPIC | XEN_X86_EMU_PIC |  \
                                      XEN_X86_EMU_VGA | XEN_X86_EMU_IOMMU |   \
-                                     XEN_X86_EMU_PIT | XEN_X86_EMU_USE_PIRQ)
+                                     XEN_X86_EMU_PIT | XEN_X86_EMU_USE_PIRQ |\
+                                     XEN_X86_EMU_VPCI)
     uint32_t emulation_flags;
 };
 
xen/include/xen/pci.h +3 -0
index dd5ec43a70..b7a6abfc53 100644
@@ -112,6 +112,9 @@ struct pci_dev {
 #define PT_FAULT_THRESHOLD 10
     } fault;
     u64 vf_rlen[6];
+
+    /* Data for vPCI. */
+    struct vpci *vpci;
 };
 
 #define for_each_pdev(domain, pdev) \
xen/include/xen/pci_regs.h +8 -0
index ecd6124d91..cc4ee3b83e 100644
@@ -23,6 +23,14 @@
 #define LINUX_PCI_REGS_H
 
 /*
+ * Conventional PCI and PCI-X Mode 1 devices have 256 bytes of
+ * configuration space.  PCI-X Mode 2 and PCIe devices have 4096 bytes of
+ * configuration space.
+ */
+#define PCI_CFG_SPACE_SIZE	256
+#define PCI_CFG_SPACE_EXP_SIZE	4096
+
+/*
  * Under PCI, each device has 256 bytes of configuration address space,
  * of which the first 64 bytes are standardized as follows:
  */
xen/include/xen/vpci.h new file +54 -0

54 lines not on this page · raw message

Signature · 7 lines
2.11.0 (Apple Git-81)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v6 2/11
#5 lore raw

Re: [PATCH v6 02/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 21 lines · Roger Pau Monne

This functionality is going to reside in vpci.c (and the corresponding vpci.h header), and should be arch-agnostic. The handlers introduced in this patch setup the basic functionality required in order to trap accesses to the PCI config space, and allow decoding the address and finding the corresponding handler that should handle the access (although no handlers are implemented).

Note that the traps to the PCI IO ports registers (0xcf8/0xcfc) are setup inside of a x86 HVM file, since that's not shared with other arches.

A new XEN_X86_EMU_VPCI x86 domain flag is added in order to signal Xen whether a domain should use the newly introduced vPCI handlers, this is only enabled for PVH Dom0 at the moment.

A very simple user-space test is also provided, so that the basic functionality of the vPCI traps can be asserted. This has been proven quite helpful during development, since the logic to handle partial accesses or accesses that expand across multiple registers is not trivial.

Quoted 6 lines

The handlers for the registers are added to a linked list that's keep sorted at all times. Both the read and write handlers support accesses that expand across multiple emulated registers and contain gaps not emulated.

I am afraid I don't know much about PCI so I can't do meaningful review of this patch.

The change to libxl looks good to me so for that bit:

Quoted 201 lines

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v5:
- Use a spinlock per pci device.
- Use the recently introduced pci_sbdf_t type.
- Fix test harness to use the right handler type and the newly introduced lock.
- Move the position of the vpci sections in the linker scripts.
- Constify domain and pci_dev in vpci_{read/write}.
- Fix typos in comments.
- Use _XEN_VPCI_H_ as header guard.

Changes since v4:
* User-space test harness:
- Do not redirect the output of the test.
- Add main.c and emul.h as dependencies of the Makefile target.
- Use the same rule to modify the vpci and list headers.
- Remove underscores from local macro variables.
- Add _check suffix to the test harness multiread function.
- Change the value written by every different size in the multiwrite test.
- Use { } to initialize the r16 and r20 arrays (instead of { 0 }).
- Perform some of the read checks with the local variable directly.
- Expand some comments.
- Implement a dummy rwlock.
* Hypervisor code:
- Guard the linker script changes with CONFIG_HAS_PCI.
- Rename vpci_access_check to vpci_access_allowed and make it return bool.
- Make hvm_pci_decode_addr return the register as return value.
- Use ~3 instead of 0xfffc to remove the register offset when checking accesses to IO ports.
- s/head/prev in vpci_add_register.
- Add parentheses around & in vpci_add_register.
- Fix register removal.
- Change the BUGs in vpci_{read/write}_hw helpers to ASSERT_UNREACHABLE.
- Make merge_result static and change the computation of the mask to avoid using a uint64_t.
- Modify vpci_read to only read from hardware the not-emulated gaps.
- Remove the vpci_val union and use a uint32_t instead.
- Change handler read type to return a uint32_t instead of modifying a variable passed by reference.
- Constify the data opaque parameter of read handlers.
- Change the size parameter of the vpci_{read/write} functions to unsigned int.
- Place the array of initialization handlers in init.rodata or .rodata depending on whether late-hwdom is enabled.
- Remove the pci_devs lock, assume the Dom0 is well behaved and won't remove the device while trying to access it.
- Change the recursive spinlock into a rw lock for performance reasons.

Changes since v3:
* User-space test harness:
- Fix spaces in container_of macro.
- Implement a dummy locking functions.
- Remove 'current' macro make current a pointer to the statically allocated vpcu.
- Remove unneeded parentheses in the pci_conf_readX macros.
- Fix the name of the write test macro.
- Remove the dummy EXPORT_SYMBOL macro (this was needed by the RB code only).
- Import the max macro.
- Test all possible read/write size combinations with all possible emulated register sizes.
- Introduce a test for register removal.
* Hypervisor code:
- Use a sorted list in order to store the config space handlers.
- Remove some unneeded 'else' branches.
- Make the IO port handlers always return X86EMUL_OKAY, and set the data to all 1's in case of read failure (write are simply ignored).
- In hvm_select_ioreq_server reuse local variables when calling XEN_DMOP_PCI_SBDF.
- Store the pointers to the initialization functions in the .rodata section.
- Do not ignore the return value of xen_vpci_add_handlers in setup_one_hwdom_device.
- Remove the vpci_init macro.
- Do not hide the pointers inside of the vpci_{read/write}_t typedefs.
- Rename priv_data to private in vpci_register.
- Simplify checking for register overlap in vpci_register_cmp.
- Check that the offset and the length match before removing a register in xen_vpci_remove_register.
- Make vpci_read_hw return a value rather than storing it in a pointer passed by parameter.
- Handler dispatcher functions vpci_{read/write} no longer return an error code, errors on reads/writes should be treated like hardware (writes ignored, reads return all 1's or garbage).
- Make sure pcidevs is locked before calling pci_get_pdev_by_domain.
- Use a recursive spinlock for the vpci lock, so that spin_is_locked checks that the current CPU is holding the lock.
- Make the code less error-chatty by removing some of the printk's.
- Pass the slot and the function as separate parameters to the handler dispatchers (instead of passing devfn).
- Allow handlers to be registered with either a read or write function only, the missing handler will be replaced by a dummy handler (writes ignored, reads return 1's).
- Introduce PCI_CFG_SPACE_* defines from Linux.
- Simplify the handler dispatchers by removing the recursion, now the dispatchers iterate over the list of sorted handlers and call them in order.
- Remove the GENMASK_BYTES, SHIFT_RIGHT_BYTES and ADD_RESULT macros, and instead provide a merge_result function in order to merge a register output into a partial result.
- Rename the fields of the vpci_val union to u8/u16/u32.
- Remove the return values from the read/write handlers, errors should be handled internally and signaled as would be done on native hardware.
- Remove the usage of the GENMASK macro.

Changes since v2:
- Generalize the PCI address decoding and use it for IOREQ code also.

Changes since v1:
- Allow access to cross a word-boundary.
- Add locking.
- Add cleanup to xen_vpci_add_handlers in case of failure.

---
 .gitignore                        |   3 +
 tools/libxl/libxl_x86.c           |   2 +-
 tools/tests/Makefile              |   1 +
 tools/tests/vpci/Makefile         |  37 ++++
 tools/tests/vpci/emul.h           | 133 +++++++++++
 tools/tests/vpci/main.c           | 308 ++++++++++++++++++++++++++
 xen/arch/arm/xen.lds.S            |  12 +
 xen/arch/x86/domain.c             |  18 +-
 xen/arch/x86/hvm/hvm.c            |   2 +
 xen/arch/x86/hvm/io.c             | 103 +++++++++
 xen/arch/x86/setup.c              |   3 +-
 xen/arch/x86/xen.lds.S            |  12 +
 xen/drivers/Makefile              |   2 +-
 xen/drivers/passthrough/pci.c     |   9 +-
 xen/drivers/vpci/Makefile         |   1 +
 xen/drivers/vpci/vpci.c           | 450 ++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h      |   1 +
 xen/include/asm-x86/hvm/io.h      |   3 +
 xen/include/public/arch-x86/xen.h |   5 +-
 xen/include/xen/pci.h             |   3 +
 xen/include/xen/pci_regs.h        |   8 +
 xen/include/xen/vpci.h            |  54 +++++
 22 files changed, 1161 insertions(+), 9 deletions(-)
 create mode 100644 tools/tests/vpci/Makefile
 create mode 100644 tools/tests/vpci/emul.h
 create mode 100644 tools/tests/vpci/main.c
 create mode 100644 xen/drivers/vpci/Makefile
 create mode 100644 xen/drivers/vpci/vpci.c
 create mode 100644 xen/include/xen/vpci.h
.gitignore +3 -0
index cc16649457..1c670b27d7 100644
@@ -240,6 +240,9 @@ tools/tests/regression/build/*
 tools/tests/regression/downloads/*
 tools/tests/mem-sharing/memshrtool
 tools/tests/mce-test/tools/xen-mceinj
+tools/tests/vpci/list.h
+tools/tests/vpci/vpci.[hc]
+tools/tests/vpci/test_vpci
 tools/xcutils/lsevtchn
 tools/xcutils/readnotes
 tools/xenbackendd/_paths.h
tools/libxl/libxl_x86.c +1 -1
index 455f6f0bed..dd7fc78a99 100644
@@ -11,7 +11,7 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
     if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM) {
         if (d_config->b_info.device_model_version !=
             LIBXL_DEVICE_MODEL_VERSION_NONE) {
-            xc_config->emulation_flags = XEN_X86_EMU_ALL;
+            xc_config->emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);
         } else if (libxl_defbool_val(d_config->b_info.u.hvm.apic)) {
             /*
              * HVM guests without device model may want
tools/tests/Makefile +1 -0
index 7162945121..f6942a93fb 100644
@@ -13,6 +13,7 @@ endif
 SUBDIRS-$(CONFIG_X86) += x86_emulator
 SUBDIRS-y += xen-access
 SUBDIRS-y += xenstore
+SUBDIRS-$(CONFIG_HAS_PCI) += vpci
 
 .PHONY: all clean install distclean uninstall
 all clean distclean: %: subdirs-%
tools/tests/vpci/Makefile new file +1 -0
new file mode 100644
index 0000000000..e45fcb5cd9
@@ -0,0 +1,37 @@
+XEN_ROOT=$(CURDIR)/../../..

tools/tests/vpci/Makefile line 11

Quoted 10 lines
tools/tests/vpci/Makefile new file +10 -0
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGET := test_vpci
+
+.PHONY: all
+all: $(TARGET)
+
+.PHONY: run
+run: $(TARGET)
+	./$(TARGET)

Is such target useful? It will possibly break if you invoke it outside of this directory.

Quoted 11 lines
tools/tests/vpci/Makefile new file +11 -0
+
+$(TARGET): vpci.c vpci.h list.h main.c emul.h
+	$(HOSTCC) -g -o $@ vpci.c main.c
+
+.PHONY: clean
+clean:
+	rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h
+
+.PHONY: distclean
+distclean: clean
+

tools/tests/vpci/Makefile line 32

Quoted 10 lines
tools/tests/vpci/Makefile new file +10 -0
+.PHONY: install
+install:
+
+vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c
+	# Trick the compiler so it doesn't complain about missing symbols
+	sed -e '/#include/d' \
+	    -e '1s;^;#include "emul.h"\
+	             vpci_register_init_t *const __start_vpci_array[1]\;\
+	             vpci_register_init_t *const __end_vpci_array[1]\;\
+	             ;' <$< >$@

This is prone to breakage. I think it would be better to just have a local stub file. Or maybe define it in your main.c?

tools/tests/vpci/Makefile

Quoted 12 lines
tools/tests/vpci/Makefile new file +12 -0
+
+int
+main(int argc, char **argv)
+{
+    /* Index storage by offset. */
+    uint32_t r0 = 0xdeadbeef;
+    uint8_t r5 = 0xef;
+    uint8_t r6 = 0xbe;
+    uint8_t r7 = 0xef;
+    uint16_t r12 = 0x8696;
+    uint8_t r16[4] = { };
+    uint16_t r20[2] = { };

Need to put 0 in the brackets.

The code seems rather self-contained. Maybe we can write a fuzzer for it?

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

#6 lore raw

Re: [PATCH v6 02/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 27 lines · Wei Liu
Quoted 26 lines · Roger Pau Monne

This functionality is going to reside in vpci.c (and the corresponding vpci.h header), and should be arch-agnostic. The handlers introduced in this patch setup the basic functionality required in order to trap accesses to the PCI config space, and allow decoding the address and finding the corresponding handler that should handle the access (although no handlers are implemented).

Note that the traps to the PCI IO ports registers (0xcf8/0xcfc) are setup inside of a x86 HVM file, since that's not shared with other arches.

A new XEN_X86_EMU_VPCI x86 domain flag is added in order to signal Xen whether a domain should use the newly introduced vPCI handlers, this is only enabled for PVH Dom0 at the moment.

A very simple user-space test is also provided, so that the basic functionality of the vPCI traps can be asserted. This has been proven quite helpful during development, since the logic to handle partial accesses or accesses that expand across multiple registers is not trivial.

The handlers for the registers are added to a linked list that's keep sorted at all times. Both the read and write handlers support accesses that expand across multiple emulated registers and contain gaps not emulated.

Quoted 8 lines
Quoted 1 line

I am afraid I don't know much about PCI so I can't do meaningful review of this patch.

The change to libxl looks good to me so for that bit:

Thanks.

Quoted 206 lines
Quoted 206 lines

---
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v5:
- Use a spinlock per pci device.
- Use the recently introduced pci_sbdf_t type.
- Fix test harness to use the right handler type and the newly introduced lock.
- Move the position of the vpci sections in the linker scripts.
- Constify domain and pci_dev in vpci_{read/write}.
- Fix typos in comments.
- Use _XEN_VPCI_H_ as header guard.

Changes since v4:
* User-space test harness:
- Do not redirect the output of the test.
- Add main.c and emul.h as dependencies of the Makefile target.
- Use the same rule to modify the vpci and list headers.
- Remove underscores from local macro variables.
- Add _check suffix to the test harness multiread function.
- Change the value written by every different size in the multiwrite test.
- Use { } to initialize the r16 and r20 arrays (instead of { 0 }).
- Perform some of the read checks with the local variable directly.
- Expand some comments.
- Implement a dummy rwlock.
* Hypervisor code:
- Guard the linker script changes with CONFIG_HAS_PCI.
- Rename vpci_access_check to vpci_access_allowed and make it return bool.
- Make hvm_pci_decode_addr return the register as return value.
- Use ~3 instead of 0xfffc to remove the register offset when checking accesses to IO ports.
- s/head/prev in vpci_add_register.
- Add parentheses around & in vpci_add_register.
- Fix register removal.
- Change the BUGs in vpci_{read/write}_hw helpers to ASSERT_UNREACHABLE.
- Make merge_result static and change the computation of the mask to avoid using a uint64_t.
- Modify vpci_read to only read from hardware the not-emulated gaps.
- Remove the vpci_val union and use a uint32_t instead.
- Change handler read type to return a uint32_t instead of modifying a variable passed by reference.
- Constify the data opaque parameter of read handlers.
- Change the size parameter of the vpci_{read/write} functions to unsigned int.
- Place the array of initialization handlers in init.rodata or .rodata depending on whether late-hwdom is enabled.
- Remove the pci_devs lock, assume the Dom0 is well behaved and won't remove the device while trying to access it.
- Change the recursive spinlock into a rw lock for performance reasons.

Changes since v3:
* User-space test harness:
- Fix spaces in container_of macro.
- Implement a dummy locking functions.
- Remove 'current' macro make current a pointer to the statically allocated vpcu.
- Remove unneeded parentheses in the pci_conf_readX macros.
- Fix the name of the write test macro.
- Remove the dummy EXPORT_SYMBOL macro (this was needed by the RB code only).
- Import the max macro.
- Test all possible read/write size combinations with all possible emulated register sizes.
- Introduce a test for register removal.
* Hypervisor code:
- Use a sorted list in order to store the config space handlers.
- Remove some unneeded 'else' branches.
- Make the IO port handlers always return X86EMUL_OKAY, and set the data to all 1's in case of read failure (write are simply ignored).
- In hvm_select_ioreq_server reuse local variables when calling XEN_DMOP_PCI_SBDF.
- Store the pointers to the initialization functions in the .rodata section.
- Do not ignore the return value of xen_vpci_add_handlers in setup_one_hwdom_device.
- Remove the vpci_init macro.
- Do not hide the pointers inside of the vpci_{read/write}_t typedefs.
- Rename priv_data to private in vpci_register.
- Simplify checking for register overlap in vpci_register_cmp.
- Check that the offset and the length match before removing a register in xen_vpci_remove_register.
- Make vpci_read_hw return a value rather than storing it in a pointer passed by parameter.
- Handler dispatcher functions vpci_{read/write} no longer return an error code, errors on reads/writes should be treated like hardware (writes ignored, reads return all 1's or garbage).
- Make sure pcidevs is locked before calling pci_get_pdev_by_domain.
- Use a recursive spinlock for the vpci lock, so that spin_is_locked checks that the current CPU is holding the lock.
- Make the code less error-chatty by removing some of the printk's.
- Pass the slot and the function as separate parameters to the handler dispatchers (instead of passing devfn).
- Allow handlers to be registered with either a read or write function only, the missing handler will be replaced by a dummy handler (writes ignored, reads return 1's).
- Introduce PCI_CFG_SPACE_* defines from Linux.
- Simplify the handler dispatchers by removing the recursion, now the dispatchers iterate over the list of sorted handlers and call them in order.
- Remove the GENMASK_BYTES, SHIFT_RIGHT_BYTES and ADD_RESULT macros, and instead provide a merge_result function in order to merge a register output into a partial result.
- Rename the fields of the vpci_val union to u8/u16/u32.
- Remove the return values from the read/write handlers, errors should be handled internally and signaled as would be done on native hardware.
- Remove the usage of the GENMASK macro.

Changes since v2:
- Generalize the PCI address decoding and use it for IOREQ code also.

Changes since v1:
- Allow access to cross a word-boundary.
- Add locking.
- Add cleanup to xen_vpci_add_handlers in case of failure.

---
 .gitignore                        |   3 +
 tools/libxl/libxl_x86.c           |   2 +-
 tools/tests/Makefile              |   1 +
 tools/tests/vpci/Makefile         |  37 ++++
 tools/tests/vpci/emul.h           | 133 +++++++++++
 tools/tests/vpci/main.c           | 308 ++++++++++++++++++++++++++
 xen/arch/arm/xen.lds.S            |  12 +
 xen/arch/x86/domain.c             |  18 +-
 xen/arch/x86/hvm/hvm.c            |   2 +
 xen/arch/x86/hvm/io.c             | 103 +++++++++
 xen/arch/x86/setup.c              |   3 +-
 xen/arch/x86/xen.lds.S            |  12 +
 xen/drivers/Makefile              |   2 +-
 xen/drivers/passthrough/pci.c     |   9 +-
 xen/drivers/vpci/Makefile         |   1 +
 xen/drivers/vpci/vpci.c           | 450 ++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/domain.h      |   1 +
 xen/include/asm-x86/hvm/io.h      |   3 +
 xen/include/public/arch-x86/xen.h |   5 +-
 xen/include/xen/pci.h             |   3 +
 xen/include/xen/pci_regs.h        |   8 +
 xen/include/xen/vpci.h            |  54 +++++
 22 files changed, 1161 insertions(+), 9 deletions(-)
 create mode 100644 tools/tests/vpci/Makefile
 create mode 100644 tools/tests/vpci/emul.h
 create mode 100644 tools/tests/vpci/main.c
 create mode 100644 xen/drivers/vpci/Makefile
 create mode 100644 xen/drivers/vpci/vpci.c
 create mode 100644 xen/include/xen/vpci.h
.gitignore +3 -0
index cc16649457..1c670b27d7 100644
@@ -240,6 +240,9 @@ tools/tests/regression/build/*
 tools/tests/regression/downloads/*
 tools/tests/mem-sharing/memshrtool
 tools/tests/mce-test/tools/xen-mceinj
+tools/tests/vpci/list.h
+tools/tests/vpci/vpci.[hc]
+tools/tests/vpci/test_vpci
 tools/xcutils/lsevtchn
 tools/xcutils/readnotes
 tools/xenbackendd/_paths.h
tools/libxl/libxl_x86.c +1 -1
index 455f6f0bed..dd7fc78a99 100644
@@ -11,7 +11,7 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
     if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_HVM) {
         if (d_config->b_info.device_model_version !=
             LIBXL_DEVICE_MODEL_VERSION_NONE) {
-            xc_config->emulation_flags = XEN_X86_EMU_ALL;
+            xc_config->emulation_flags = (XEN_X86_EMU_ALL & ~XEN_X86_EMU_VPCI);
         } else if (libxl_defbool_val(d_config->b_info.u.hvm.apic)) {
             /*
              * HVM guests without device model may want
tools/tests/Makefile +1 -0
index 7162945121..f6942a93fb 100644
@@ -13,6 +13,7 @@ endif
 SUBDIRS-$(CONFIG_X86) += x86_emulator
 SUBDIRS-y += xen-access
 SUBDIRS-y += xenstore
+SUBDIRS-$(CONFIG_HAS_PCI) += vpci
 
 .PHONY: all clean install distclean uninstall
 all clean distclean: %: subdirs-%
tools/tests/vpci/Makefile new file +6 -0
new file mode 100644
index 0000000000..e45fcb5cd9
@@ -0,0 +1,37 @@
+XEN_ROOT=$(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGET := test_vpci
+
+.PHONY: all
Quoted 8 lines

tools/tests/vpci/Makefile line 11

Quoted 5 lines
tools/tests/vpci/Makefile new file +5 -0
+all: $(TARGET)
+
+.PHONY: run
+run: $(TARGET)
+	./$(TARGET)

Is such target useful? It will possibly break if you invoke it outside of this directory.

Seems to work fine if I do:

gmake -C tools/tests/vpci run

Quoted 16 lines
Quoted 16 lines
tools/tests/vpci/Makefile new file +16 -0
+
+$(TARGET): vpci.c vpci.h list.h main.c emul.h
+	$(HOSTCC) -g -o $@ vpci.c main.c
+
+.PHONY: clean
+clean:
+	rm -rf $(TARGET) *.o *~ vpci.h vpci.c list.h
+
+.PHONY: distclean
+distclean: clean
+
+.PHONY: install
+install:
+
+vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c
+	# Trick the compiler so it doesn't complain about missing symbols
Quoted 8 lines

tools/tests/vpci/Makefile line 32

Quoted 5 lines
tools/tests/vpci/Makefile new file +5 -0
+	sed -e '/#include/d' \
+	    -e '1s;^;#include "emul.h"\
+	             vpci_register_init_t *const __start_vpci_array[1]\;\
+	             vpci_register_init_t *const __end_vpci_array[1]\;\
+	             ;' <$< >$@

This is prone to breakage. I think it would be better to just have a local stub file. Or maybe define it in your main.c?

I could add this to main.c, I haven't done so because the above it's done for the linking to succeed, but it's not used in any way by the test harness itself, or any of the functions called by it.

Quoted 6 lines
Quoted 6 lines
tools/tests/vpci/Makefile new file +6 -0
+
+int
+main(int argc, char **argv)
+{
+    /* Index storage by offset. */
+    uint32_t r0 = 0xdeadbeef;
Quoted 8 lines

tools/tests/vpci/Makefile

Quoted 6 lines
tools/tests/vpci/Makefile new file +6 -0
+    uint8_t r5 = 0xef;
+    uint8_t r6 = 0xbe;
+    uint8_t r7 = 0xef;
+    uint16_t r12 = 0x8696;
+    uint8_t r16[4] = { };
+    uint16_t r20[2] = { };

Need to put 0 in the brackets.

Jan requested to remove explicitly setting it to 0:

https://marc.info/?l=xen-devel&m=149995672426469

The end result is the same, when doing initialization non-set fields get set to 0.

Quoted 2 lines

The code seems rather self-contained. Maybe we can write a fuzzer for it?

Probably, but I don't think we can expect the fuzzer to set the handlers, in which case we would have to pre-populate the PCI space with some handlers in order to perform the fuzzing, which kinds of limits it's usefulness.

Roger.

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

#7 lore raw

Re: [PATCH v6 02/11] vpci: introduce basic handlers to trap accesses to the PCI config space

In reply to Roger Pau Monne
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +10 -0
+static int vpci_portio_read(const struct hvm_io_handler *handler,
+                            uint64_t addr, uint32_t size, uint64_t *data)
+{
+    struct domain *d = current->domain;
+    unsigned int reg;
+    pci_sbdf_t sbdf;
+    uint32_t cf8;
+
+    *data = ~(uint64_t)0;
+

xen/arch/x86/hvm/io.c unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr, · line 313

Quoted 10 lines
xen/arch/x86/hvm/io.c +10 -0
+    if ( addr == 0xcf8 )
+    {
+        ASSERT(size == 4);
+        *data = d->arch.hvm_domain.pci_cf8;
+        return X86EMUL_OKAY;
+    }
+
+    cf8 = ACCESS_ONCE(d->arch.hvm_domain.pci_cf8);
+    if ( !CF8_ENABLED(cf8) )
+        return X86EMUL_OKAY;

Why is this OKAY instead of UNHANDLEABLE? The access is supposed to be forwarded to qemu if it's not a config space one. Same in the write path then.

Quoted 16 lines
xen/arch/x86/xen.lds.S +6 -0
@@ -124,6 +124,12 @@ SECTIONS
        __param_start = .;
        *(.data.param)
        __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
 #if defined(BUILD_ID)

xen/arch/x86/xen.lds.S SECTIONS · line 228

Quoted 10 lines
xen/arch/x86/xen.lds.S +6 -0
@@ -213,6 +219,12 @@ SECTIONS
        *(.init_array)
        *(SORT(.init_array.*))
        __ctors_end = .;
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text

Suitable alignment needs to be enforced in both cases, or else we risk someone adding something immediately ahead of one of your insertions, making __start_vpci_array no longer point to the first entry.

xen/arch/x86/xen.lds.S static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt, · line 1061

Quoted 11 lines
xen/arch/x86/xen.lds.S +2 -1
@@ -1052,9 +1053,10 @@ static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,
                                                 struct pci_dev *pdev)
 {
     u8 devfn = pdev->devfn;
+    int err;
 
     do {
-        int err = ctxt->handler(devfn, pdev);
+        err = ctxt->handler(devfn, pdev);
 
         if ( err )

Please also remove the now stray blank line.

Quoted 37 lines
xen/arch/x86/xen.lds.S +37 -0
+int vpci_add_register(const struct pci_dev *pdev, vpci_read_t *read_handler,
+                      vpci_write_t *write_handler, unsigned int offset,
+                      unsigned int size, void *data)
+{
+    struct list_head *prev;
+    struct vpci_register *r;
+
+    /* Some sanity checks. */
+    if ( (size != 1 && size != 2 && size != 4) ||
+         offset >= PCI_CFG_SPACE_EXP_SIZE || (offset & (size - 1)) ||
+         (!read_handler && !write_handler) )
+        return -EINVAL;
+
+    r = xmalloc(struct vpci_register);
+    if ( !r )
+        return -ENOMEM;
+
+    r->read = read_handler ?: vpci_ignored_read;
+    r->write = write_handler ?: vpci_ignored_write;
+    r->size = size;
+    r->offset = offset;
+    r->private = data;
+
+    spin_lock(&pdev->vpci->lock);
+
+    /* The list of handlers must be kept sorted at all times. */
+    list_for_each ( prev, &pdev->vpci->handlers )
+    {
+        const struct vpci_register *this =
+            list_entry(prev, const struct vpci_register, node);
+        int cmp = vpci_register_cmp(r, this);
+
+        if ( cmp < 0 )
+            break;
+        if ( cmp == 0 )
+        {
+            spin_unlock(&pdev->vpci->lock);

xen/arch/x86/xen.lds.S static void __hwdom_init setup_one_hwdom_device(const struct setup_hwdom *ctxt,

Quoted 10 lines
xen/arch/x86/xen.lds.S +10 -0
+            xfree(r);
+            return -EEXIST;
+        }
+    }
+
+    list_add_tail(&r->node, prev);
+    spin_unlock(&pdev->vpci->lock);
+
+    return 0;
+}

Looking at this and its remove counterpart it is not (no longer?) clear why they both take a struct pci_dev * as parameter - struct vpci * would fully suffice, and would eliminate the question on whether functions like these should have the respective parameters const-qualified.

xen/arch/x86/xen.lds.S

Quoted 13 lines
xen/arch/x86/xen.lds.S +13 -0
+/*
+ * Merge new data into a partial result.
+ *
+ * Copy the value found in 'new' from [0, size) left shifted by
+ * 'offset' into 'data'.
+ */
+static uint32_t merge_result(uint32_t data, uint32_t new, unsigned int size,
+                             unsigned int offset)
+{
+    uint32_t mask = 0xffffffff >> (32 - 8 * size);
+
+    return (data & ~(mask << (offset * 8))) | ((new & mask) << (offset * 8));
+}

If a function like this one has a relatively long comment, I think that comment should clarify that both size and offset are byte-granular. Especially for offset (used for shifting) bit otherwise would seem more natural to me.

Jan

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

#8 lore raw

Re: [PATCH v6 02/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +16 -0
+static int vpci_portio_read(const struct hvm_io_handler *handler,
+                            uint64_t addr, uint32_t size, uint64_t *data)
+{
+    struct domain *d = current->domain;
+    unsigned int reg;
+    pci_sbdf_t sbdf;
+    uint32_t cf8;
+
+    *data = ~(uint64_t)0;
+
+    if ( addr == 0xcf8 )
+    {
+        ASSERT(size == 4);
+        *data = d->arch.hvm_domain.pci_cf8;
+        return X86EMUL_OKAY;
+    }
Quoted 8 lines

xen/arch/x86/hvm/io.c unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr, · line 313

Quoted 4 lines
xen/arch/x86/hvm/io.c +4 -0
+
+    cf8 = ACCESS_ONCE(d->arch.hvm_domain.pci_cf8);
+    if ( !CF8_ENABLED(cf8) )
+        return X86EMUL_OKAY;

Why is this OKAY instead of UNHANDLEABLE? The access is supposed to be forwarded to qemu if it's not a config space one. Same in the write path then.

No, I don't think this should be forwarded to QEMU. It is a config space access (because vpci_portio_accept returned true). But the value in CF8 doesn't have the enabled bit set, hence the access is discarded.

Quoted 14 lines
Quoted 14 lines
xen/arch/x86/xen.lds.S +6 -0
@@ -124,6 +124,12 @@ SECTIONS
        __param_start = .;
        *(.data.param)
        __param_end = .;
+
+#if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text
 
Quoted 16 lines

xen/arch/x86/xen.lds.S SECTIONS · line 228

Quoted 12 lines
xen/arch/x86/xen.lds.S +6 -0
 #if defined(BUILD_ID)
@@ -213,6 +219,12 @@ SECTIONS
        *(.init_array)
        *(SORT(.init_array.*))
        __ctors_end = .;
+
+#if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
+       __start_vpci_array = .;
+       *(.data.vpci)
+       __end_vpci_array = .;
+#endif
   } :text

Suitable alignment needs to be enforced in both cases, or else we risk someone adding something immediately ahead of one of your insertions, making __start_vpci_array no longer point to the first entry.

OK, I've used . = ALIGN(POINTER_ALIGN); for both x86 and ARM.

Thanks, Roger.

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

#9 lore raw

Re: [PATCH v6 02/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 04.10.17 at 11:24, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +16 -0
+static int vpci_portio_read(const struct hvm_io_handler *handler,
+                            uint64_t addr, uint32_t size, uint64_t *data)
+{
+    struct domain *d = current->domain;
+    unsigned int reg;
+    pci_sbdf_t sbdf;
+    uint32_t cf8;
+
+    *data = ~(uint64_t)0;
+
+    if ( addr == 0xcf8 )
+    {
+        ASSERT(size == 4);
+        *data = d->arch.hvm_domain.pci_cf8;
+        return X86EMUL_OKAY;
+    }
Quoted 8 lines

xen/arch/x86/hvm/io.c unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr, · line 313

Quoted 4 lines
xen/arch/x86/hvm/io.c +4 -0
+
+    cf8 = ACCESS_ONCE(d->arch.hvm_domain.pci_cf8);
+    if ( !CF8_ENABLED(cf8) )
+        return X86EMUL_OKAY;

Why is this OKAY instead of UNHANDLEABLE? The access is supposed to be forwarded to qemu if it's not a config space one. Same in the write path then.

Quoted 4 lines

No, I don't think this should be forwarded to QEMU. It is a config space access (because vpci_portio_accept returned true). But the value in CF8 doesn't have the enabled bit set, hence the access is discarded.

With the enable bit clear it is my understanding that this is then _not_ a config space access. vpci_portio_accept() simply doesn't have enough information to tell.

Jan

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

#10 lore raw

Re: [PATCH v6 02/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 04.10.17 at 11:24, <[email protected]> wrote:
Quoted 25 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +10 -0
+static int vpci_portio_read(const struct hvm_io_handler *handler,
+                            uint64_t addr, uint32_t size, uint64_t *data)
+{
+    struct domain *d = current->domain;
+    unsigned int reg;
+    pci_sbdf_t sbdf;
+    uint32_t cf8;
+
+    *data = ~(uint64_t)0;
+

xen/arch/x86/hvm/io.c unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr, · line 313

Quoted 10 lines
xen/arch/x86/hvm/io.c +10 -0
+    if ( addr == 0xcf8 )
+    {
+        ASSERT(size == 4);
+        *data = d->arch.hvm_domain.pci_cf8;
+        return X86EMUL_OKAY;
+    }
+
+    cf8 = ACCESS_ONCE(d->arch.hvm_domain.pci_cf8);
+    if ( !CF8_ENABLED(cf8) )
+        return X86EMUL_OKAY;

Why is this OKAY instead of UNHANDLEABLE? The access is supposed to be forwarded to qemu if it's not a config space one. Same in the write path then.

Quoted 8 lines
Quoted 4 lines

No, I don't think this should be forwarded to QEMU. It is a config space access (because vpci_portio_accept returned true). But the value in CF8 doesn't have the enabled bit set, hence the access is discarded.

With the enable bit clear it is my understanding that this is then _not_ a config space access. vpci_portio_accept() simply doesn't have enough information to tell.

OK, it was my understanding that accesses to cf8/cfc where only used by the PCI config space.

Roger.

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

#11 lore raw

Re: [PATCH v6 02/11] vpci: introduce basic handlers to trap accesses to the PCI config space

Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 04.10.17 at 12:32, <[email protected]> wrote:
Quoted 33 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · On 04.10.17 at 11:24, <[email protected]> wrote:
Quoted 17 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +16 -0
+static int vpci_portio_read(const struct hvm_io_handler *handler,
+                            uint64_t addr, uint32_t size, uint64_t *data)
+{
+    struct domain *d = current->domain;
+    unsigned int reg;
+    pci_sbdf_t sbdf;
+    uint32_t cf8;
+
+    *data = ~(uint64_t)0;
+
+    if ( addr == 0xcf8 )
+    {
+        ASSERT(size == 4);
+        *data = d->arch.hvm_domain.pci_cf8;
+        return X86EMUL_OKAY;
+    }
Quoted 8 lines

xen/arch/x86/hvm/io.c unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr, · line 313

Quoted 4 lines
xen/arch/x86/hvm/io.c +4 -0
+
+    cf8 = ACCESS_ONCE(d->arch.hvm_domain.pci_cf8);
+    if ( !CF8_ENABLED(cf8) )
+        return X86EMUL_OKAY;

Why is this OKAY instead of UNHANDLEABLE? The access is supposed to be forwarded to qemu if it's not a config space one. Same in the write path then.

No, I don't think this should be forwarded to QEMU. It is a config space access (because vpci_portio_accept returned true). But the value in CF8 doesn't have the enabled bit set, hence the access is discarded.

Quoted 6 lines
Quoted 3 lines

With the enable bit clear it is my understanding that this is then _not_ a config space access. vpci_portio_accept() simply doesn't have enough information to tell.

OK, it was my understanding that accesses to cf8/cfc where only used by the PCI config space.

Just like with the overlaid byte accesses to port cf9, other such overlays could exist too; iirc back when PCI was introduced the enable bit was used to make sure other uses of this port range remained reasonably unaffected.

Jan

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

#12 lore raw

PATCH v6 3/11 x86/mmcfg: add handlers for the PVH Dom0 MMCFG areas

In reply to Roger Pau Monne

Introduce a set of handlers for the accesses to the MMCFG areas. Those areas are setup based on the contents of the hardware MMCFG tables, and the list of handled MMCFG areas is stored inside of the hvm_domain struct.

The read/writes are forwarded to the generic vpci handlers once the address is decoded in order to obtain the device and register the guest is trying to access.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v5:
- Switch to use pci_sbdf_t.
- Switch to the new per vpci locks.
- Move the mmcfg related external definitions to asm-x86/pci.h

Changes since v4:
- Change the attribute of pvh_setup_mmcfg to __hwdom_init.
- Try to add as many MMCFG regions as possible, even if one fails to add.
- Change some fields of the hvm_mmcfg struct: turn size into a unsigned int, segment into uint16_t and bus into uint8_t.
- Convert some address parameters from unsigned long to paddr_t for consistency.
- Make vpci_mmcfg_decode_addr return the decoded register in the return of the function.
- Introduce a new macro to convert a MMCFG address into a BDF, and use it in vpci_mmcfg_decode_addr to clarify the logic.
- In vpci_mmcfg_{read/write} unify the logic for 8B accesses and smaller ones.
- Add the __hwdom_init attribute to register_vpci_mmcfg_handler.
- Test that reg + size doesn't cross a device boundary.

Changes since v3:
- Propagate changes from previous patches: drop xen_ prefix for vpci functions, pass slot and func instead of devfn and fix the error paths of the MMCFG handlers.
- s/ecam/mmcfg/.
- Move the destroy code to a separate function, so the hvm_mmcfg struct can be private to hvm/io.c.
- Constify the return of vpci_mmcfg_find.
- Use d instead of v->domain in vpci_mmcfg_accept.
- Allow 8byte accesses to the mmcfg.

Changes since v1:
- Added locking.

---
 xen/arch/x86/hvm/dom0_build.c    |  21 +++++
 xen/arch/x86/hvm/hvm.c           |   4 +
 xen/arch/x86/hvm/io.c            | 174 ++++++++++++++++++++++++++++++++++++++-
 xen/arch/x86/x86_64/mmconfig.h   |   4 -
 xen/include/asm-x86/hvm/domain.h |   4 +
 xen/include/asm-x86/hvm/io.h     |   7 ++
 xen/include/asm-x86/pci.h        |   6 ++
 7 files changed, 215 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/dom0_build.c +21 -0
index 020c355faf..17d77137d6 100644
@@ -22,6 +22,7 @@
 #include <xen/init.h>
 #include <xen/libelf.h>
 #include <xen/multiboot.h>
+#include <xen/pci.h>
 #include <xen/softirq.h>
 
 #include <acpi/actables.h>
@@ -1048,6 +1049,24 @@ static int __init pvh_setup_acpi(struct domain *d, paddr_t start_info)
     return 0;
 }
 
+static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
+{
+    unsigned int i;
+    int rc;
+
+    for ( i = 0; i < pci_mmcfg_config_num; i++ )
+    {
+        rc = register_vpci_mmcfg_handler(d, pci_mmcfg_config[i].address,
+                                         pci_mmcfg_config[i].start_bus_number,
+                                         pci_mmcfg_config[i].end_bus_number,
+                                         pci_mmcfg_config[i].pci_segment);
+        if ( rc )
+            printk("Unable to setup MMCFG handler at %#lx for segment %u\n",
+                   pci_mmcfg_config[i].address,
+                   pci_mmcfg_config[i].pci_segment);
+    }
+}
+
 int __init dom0_construct_pvh(struct domain *d, const module_t *image,
                               unsigned long image_headroom,
                               module_t *initrd,
@@ -1090,6 +1109,8 @@ int __init dom0_construct_pvh(struct domain *d, const module_t *image,
         return rc;
     }
 
+    pvh_setup_mmcfg(d);
+
     panic("Building a PVHv2 Dom0 is not yet supported.");
     return 0;
 }
xen/arch/x86/hvm/hvm.c +4 -0
index cc73df8dc7..b1064413fc 100644
@@ -581,8 +581,10 @@ int hvm_domain_initialise(struct domain *d, unsigned long domcr_flags,
     spin_lock_init(&d->arch.hvm_domain.irq_lock);
     spin_lock_init(&d->arch.hvm_domain.uc_lock);
     spin_lock_init(&d->arch.hvm_domain.write_map.lock);
+    rwlock_init(&d->arch.hvm_domain.mmcfg_lock);
     INIT_LIST_HEAD(&d->arch.hvm_domain.write_map.list);
     INIT_LIST_HEAD(&d->arch.hvm_domain.g2m_ioport_list);
+    INIT_LIST_HEAD(&d->arch.hvm_domain.mmcfg_regions);
 
     rc = create_perdomain_mapping(d, PERDOMAIN_VIRT_START, 0, NULL, NULL);
     if ( rc )
@@ -728,6 +730,8 @@ void hvm_domain_destroy(struct domain *d)
         list_del(&ioport->list);
         xfree(ioport);
     }
+
+    destroy_vpci_mmcfg(&d->arch.hvm_domain.mmcfg_regions);
 }
 
 static int hvm_save_tsc_adjust(struct domain *d, hvm_domain_context_t *h)
xen/arch/x86/hvm/io.c +173 -1

186 lines not on this page · raw message

xen/arch/x86/x86_64/mmconfig.h +0 -4
index 7537519414..2e836848ad 100644
@@ -74,10 +74,6 @@ static inline void mmio_config_writel(void __iomem *pos, u32 val)
     asm volatile("movl %%eax,(%1)" :: "a" (val), "r" (pos) : "memory");
 }
 
-/* external variable defines */
-extern int pci_mmcfg_config_num;
-extern struct acpi_mcfg_allocation *pci_mmcfg_config;
-
 /* function prototypes */
 int acpi_parse_mcfg(struct acpi_table_header *header);
 int pci_mmcfg_reserved(uint64_t address, unsigned int segment,
xen/include/asm-x86/hvm/domain.h +4 -0
index d2899c9bb2..cd19ee11e9 100644
@@ -184,6 +184,10 @@ struct hvm_domain {
     /* List of guest to machine IO ports mapping. */
     struct list_head g2m_ioport_list;
 
+    /* List of MMCFG regions trapped by Xen. */
+    struct list_head mmcfg_regions;
+    rwlock_t mmcfg_lock;
+
     /* List of permanently write-mapped pages. */
     struct {
         spinlock_t lock;
xen/include/asm-x86/hvm/io.h +7 -0
index f68aed9323..ba66e22e04 100644
@@ -163,6 +163,13 @@ void register_g2m_portio_handler(struct domain *d);
 /* HVM port IO handler for vPCI accesses. */
 void register_vpci_portio_handler(struct domain *d);
 
+/* HVM MMIO handler for PCI MMCFG accesses. */
+int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
+                                unsigned int start_bus, unsigned int end_bus,
+                                unsigned int seg);
+/* Destroy tracked MMCFG areas. */
+void destroy_vpci_mmcfg(struct list_head *domain_mmcfg);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/include/asm-x86/pci.h +6 -0
index 36801d317b..cc05045e9c 100644
@@ -6,6 +6,8 @@
 #define CF8_ADDR_HI(cf8) (  ((cf8) & 0x0f000000) >> 16)
 #define CF8_ENABLED(cf8) (!!((cf8) & 0x80000000))
 
+#define MMCFG_BDF(addr)  ( ((addr) & 0x0ffff000) >> 12)
+
 #define IS_SNB_GFX(id) (id == 0x01068086 || id == 0x01168086 \
                         || id == 0x01268086 || id == 0x01028086 \
                         || id == 0x01128086 || id == 0x01228086 \
@@ -26,4 +28,8 @@ bool_t pci_mmcfg_decode(unsigned long mfn, unsigned int *seg,
 bool_t pci_ro_mmcfg_decode(unsigned long mfn, unsigned int *seg,
                            unsigned int *bdf);
 
+/* MMCFG external variable defines */
+extern int pci_mmcfg_config_num;
+extern struct acpi_mcfg_allocation *pci_mmcfg_config;
+
 #endif /* __X86_PCI_H__ */
Signature · 7 lines
2.11.0 (Apple Git-81)


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

Re: [PATCH v6 03/11] x86/mmcfg: add handlers for the PVH Dom0 MMCFG areas

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +7 -0
+static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
+                           unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;
+    const struct hvm_mmcfg *mmcfg;
+    unsigned int reg;
+    pci_sbdf_t sbdf;

xen/arch/x86/hvm/io.c void register_vpci_portio_handler(struct domain *d) · line 435

Quoted 10 lines
xen/arch/x86/hvm/io.c +10 -0
+
+    *data = ~0ul;
+
+    read_lock(&d->arch.hvm_domain.mmcfg_lock);
+    mmcfg = vpci_mmcfg_find(d, addr);
+    if ( !mmcfg )
+    {
+        read_unlock(&d->arch.hvm_domain.mmcfg_lock);
+        return X86EMUL_OKAY;
+    }

With the lock dropped between accept() and read() (or write() below), is it really appropriate to return OKAY here? The access again should be forwarded to qemu, I would think.

Quoted 5 lines
xen/arch/x86/hvm/io.c +5 -0
+int __hwdom_init register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
+                                             unsigned int start_bus,
+                                             unsigned int end_bus,
+                                             unsigned int seg)
+{

xen/arch/x86/hvm/io.c void register_vpci_portio_handler(struct domain *d) · line 513

Quoted 10 lines
xen/arch/x86/hvm/io.c +10 -0
+    struct hvm_mmcfg *mmcfg;
+
+    ASSERT(is_hardware_domain(d));
+
+    write_lock(&d->arch.hvm_domain.mmcfg_lock);
+    if ( vpci_mmcfg_find(d, addr) )
+    {
+        write_unlock(&d->arch.hvm_domain.mmcfg_lock);
+        return -EEXIST;
+    }

You check here for an exact match in starting address. Is it really to reject just this special case, rather than doing a proper overlap check?

Quoted 1 line

+ mmcfg = xmalloc(struct hvm_mmcfg);

Whenever possible without too much trouble allocations should be done with no lock held.

Jan

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

#14 lore raw

Re: [PATCH v6 03/11] x86/mmcfg: add handlers for the PVH Dom0 MMCFG areas

Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +13 -0
+static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
+                           unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;
+    const struct hvm_mmcfg *mmcfg;
+    unsigned int reg;
+    pci_sbdf_t sbdf;
+
+    *data = ~0ul;
+
+    read_lock(&d->arch.hvm_domain.mmcfg_lock);
+    mmcfg = vpci_mmcfg_find(d, addr);
+    if ( !mmcfg )
Quoted 8 lines

xen/arch/x86/hvm/io.c void register_vpci_portio_handler(struct domain *d) · line 435

Quoted 4 lines
xen/arch/x86/hvm/io.c +4 -0
+    {
+        read_unlock(&d->arch.hvm_domain.mmcfg_lock);
+        return X86EMUL_OKAY;
+    }

With the lock dropped between accept() and read() (or write() below), is it really appropriate to return OKAY here? The access again should be forwarded to qemu, I would think.

That's right, the MCFG area could have been removed in the meantime. I guess it is indeed more appropriate to return X86EMUL_UNHANDLEABLE or X86EMUL_RETRY.

It would seem like RETRY is better, since a new call to _accept should return false now.

Roger.

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

#15 lore raw

Re: [PATCH v6 03/11] x86/mmcfg: add handlers for the PVH Dom0 MMCFG areas

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 04.10.17 at 12:21, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/io.c +13 -0
+static int vpci_mmcfg_read(struct vcpu *v, unsigned long addr,
+                           unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;
+    const struct hvm_mmcfg *mmcfg;
+    unsigned int reg;
+    pci_sbdf_t sbdf;
+
+    *data = ~0ul;
+
+    read_lock(&d->arch.hvm_domain.mmcfg_lock);
+    mmcfg = vpci_mmcfg_find(d, addr);
+    if ( !mmcfg )
Quoted 8 lines

xen/arch/x86/hvm/io.c void register_vpci_portio_handler(struct domain *d) · line 435

Quoted 4 lines
xen/arch/x86/hvm/io.c +4 -0
+    {
+        read_unlock(&d->arch.hvm_domain.mmcfg_lock);
+        return X86EMUL_OKAY;
+    }

With the lock dropped between accept() and read() (or write() below), is it really appropriate to return OKAY here? The access again should be forwarded to qemu, I would think.

Quoted 6 lines

That's right, the MCFG area could have been removed in the meantime. I guess it is indeed more appropriate to return X86EMUL_UNHANDLEABLE or X86EMUL_RETRY.

It would seem like RETRY is better, since a new call to _accept should return false now.

Ah, yes, I'm fine with RETRY.

Jan

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

#16 lore raw

PATCH v6 4/11 x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0

In reply to Roger Pau Monne

So that MMCFG regions not present in the MCFG ACPI table can be added at run time by the hardware domain.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v5:
- Check for has_vpci before calling register_vpci_mmcfg_handler instead of checking for is_hvm_domain.

Changes since v4:
- Change the hardware_domain check in hvm_physdev_op to a vpci check.
- Only register the MMCFG area, but don't scan it.

Changes since v3:
- New in this version.

---
 xen/arch/x86/hvm/hypercall.c |  4 ++++
 xen/arch/x86/hvm/io.c        |  7 +++----
 xen/arch/x86/physdev.c       | 11 +++++++++++
 3 files changed, 18 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hypercall.c +4 -0
index 5742dd1797..d81160c1f7 100644
@@ -89,6 +89,10 @@ static long hvm_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( !has_pirq(curr->domain) )
             return -ENOSYS;
         break;
+    case PHYSDEVOP_pci_mmcfg_reserved:
+        if ( !has_vpci(curr->domain) )
+            return -ENOSYS;
+        break;
     }
 
     if ( !curr->hcall_compat )
xen/arch/x86/hvm/io.c +3 -4
index 7ee20eb5d4..ff167bdfc7 100644
@@ -496,10 +496,9 @@ static const struct hvm_mmio_ops vpci_mmcfg_ops = {
     .write = vpci_mmcfg_write,
 };
 
-int __hwdom_init register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
-                                             unsigned int start_bus,
-                                             unsigned int end_bus,
-                                             unsigned int seg)
+int register_vpci_mmcfg_handler(struct domain *d, paddr_t addr,
+                                unsigned int start_bus, unsigned int end_bus,
+                                unsigned int seg)
 {
     struct hvm_mmcfg *mmcfg;
 
xen/arch/x86/physdev.c +11 -0
index 0eb409758f..b36add32f1 100644
@@ -559,6 +559,17 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         ret = pci_mmcfg_reserved(info.address, info.segment,
                                  info.start_bus, info.end_bus, info.flags);
+        if ( !ret && has_vpci(currd) )
+        {
+            /*
+             * For HVM (PVH) domains try to add the newly found MMCFG to the
+             * domain.
+             */
+            ret = register_vpci_mmcfg_handler(currd, info.address,
+                                              info.start_bus, info.end_bus,
+                                              info.segment);
+        }
+
         break;
     }
 
Signature · 7 lines
2.11.0 (Apple Git-81)


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

Re: [PATCH v6 04/11] x86/physdev: enable PHYSDEVOP_pci_mmcfg_reserved for PVH Dom0

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/physdev.c +3 -0
@@ -559,6 +559,17 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
 
         ret = pci_mmcfg_reserved(info.address, info.segment,
                                  info.start_bus, info.end_bus, info.flags);
+        if ( !ret && has_vpci(currd) )
+        {
+            /*

xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 574

Quoted 10 lines
xen/arch/x86/physdev.c +8 -0
+             * For HVM (PVH) domains try to add the newly found MMCFG to the
+             * domain.
+             */
+            ret = register_vpci_mmcfg_handler(currd, info.address,
+                                              info.start_bus, info.end_bus,
+                                              info.segment);
+        }
+
         break;
     }

I think it is wrong to report back -EEXIST here for an exact match region which we have on record already.

Jan

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

#18 lore raw

PATCH v6 5/11 pci: split code to size BARs from pci_add_device

In reply to Roger Pau Monne

So that it can be called from outside in order to get the size of regular PCI BARs. This will be required in order to map the BARs from PCI devices into PVH Dom0 p2m.

---
Cc: Jan Beulich <[email protected]>
---
Changes since v5:
- Introduce a flags field for pci_size_mem_bar.
- Use pci_sbdf_t.

Changes since v4:
- Restore printing whether the BAR is from a vf.
- Make the psize pointer parameter not optional.
- s/u64/uint64_t.
- Remove some unneeded parentheses.
- Assert the return value is never 0.
- Use the newly introduced pci_sbdf_t type.

Changes since v3:
- Rename function to size BARs to pci_size_mem_bar.
- Change the parameters passed to the function. Pass the position and whether the BAR is the last one, instead of the (base, max_bars, *index) tuple.
- Make the function return the number of BARs consumed (1 for 32b, 2 for 64b BARs).
- Change the dprintk back to printk.
- Do not log another error message in pci_add_device in case pci_size_mem_bar fails.

---
 xen/drivers/passthrough/pci.c | 98 ++++++++++++++++++++++++++++---------------
 xen/include/xen/pci.h         |  4 ++
 2 files changed, 68 insertions(+), 34 deletions(-)
xen/drivers/passthrough/pci.c +64 -34
index 975485fe05..ba58b4d0cc 100644
@@ -603,6 +603,56 @@ static int iommu_add_device(struct pci_dev *pdev);
 static int iommu_enable_device(struct pci_dev *pdev);
 static int iommu_remove_device(struct pci_dev *pdev);
 
+int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
+                     uint64_t *paddr, uint64_t *psize, unsigned int flags)
+{
+    uint32_t hi = 0, bar = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
+                                           sbdf.func, pos);
+    uint64_t addr, size;
+    bool vf = flags & PCI_BAR_VF;
+
+    ASSERT((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
+    pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, ~0);
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    {
+        if ( last )
+        {
+            printk(XENLOG_WARNING
+                   "%sdevice %04x:%02x:%02x.%u with 64-bit %sBAR in last slot\n",
+                   vf ? "SR-IOV " : "", sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func,
+                   vf ? "vf " : "");
+            return -EINVAL;
+        }
+        hi = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4);
+        pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, ~0);
+    }
+    size = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos) &
+           PCI_BASE_ADDRESS_MEM_MASK;
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    {
+        size |= (uint64_t)pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
+                                          sbdf.func, pos + 4) << 32;
+        pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, hi);
+    }
+    else if ( size )
+        size |= (uint64_t)~0 << 32;
+    pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, bar);
+    size = -size;
+    addr = (bar & PCI_BASE_ADDRESS_MEM_MASK) | ((uint64_t)hi << 32);
+
+    if ( paddr )
+        *paddr = addr;
+    *psize = size;
+
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+        return 2;
+
+    return 1;
+}
+
 int pci_add_device(u16 seg, u8 bus, u8 devfn,
                    const struct pci_dev_info *info, nodeid_t node)
 {
@@ -674,11 +724,16 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             unsigned int i;
 
             BUILD_BUG_ON(ARRAY_SIZE(pdev->vf_rlen) != PCI_SRIOV_NUM_BARS);
-            for ( i = 0; i < PCI_SRIOV_NUM_BARS; ++i )
+            for ( i = 0; i < PCI_SRIOV_NUM_BARS; )
             {
                 unsigned int idx = pos + PCI_SRIOV_BAR + i * 4;
                 u32 bar = pci_conf_read32(seg, bus, slot, func, idx);
-                u32 hi = 0;
+                pci_sbdf_t sbdf = {
+                    .seg = seg,
+                    .bus = bus,
+                    .dev = slot,
+                    .func = func,
+                };
 
                 if ( (bar & PCI_BASE_ADDRESS_SPACE) ==
                      PCI_BASE_ADDRESS_SPACE_IO )
@@ -689,38 +744,13 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
                            seg, bus, slot, func, i);
                     continue;
                 }
-                pci_conf_write32(seg, bus, slot, func, idx, ~0);
-                if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-                     PCI_BASE_ADDRESS_MEM_TYPE_64 )
-                {
-                    if ( i >= PCI_SRIOV_NUM_BARS )
-                    {
-                        printk(XENLOG_WARNING
-                               "SR-IOV device %04x:%02x:%02x.%u with 64-bit"
-                               " vf BAR in last slot\n",
-                               seg, bus, slot, func);
-                        break;
-                    }
-                    hi = pci_conf_read32(seg, bus, slot, func, idx + 4);
-                    pci_conf_write32(seg, bus, slot, func, idx + 4, ~0);
-                }
-                pdev->vf_rlen[i] = pci_conf_read32(seg, bus, slot, func, idx) &
-                                   PCI_BASE_ADDRESS_MEM_MASK;
-                if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-                     PCI_BASE_ADDRESS_MEM_TYPE_64 )
-                {
-                    pdev->vf_rlen[i] |= (u64)pci_conf_read32(seg, bus,
-                                                             slot, func,
-                                                             idx + 4) << 32;
-                    pci_conf_write32(seg, bus, slot, func, idx + 4, hi);
-                }
-                else if ( pdev->vf_rlen[i] )
-                    pdev->vf_rlen[i] |= (u64)~0 << 32;
-                pci_conf_write32(seg, bus, slot, func, idx, bar);
-                pdev->vf_rlen[i] = -pdev->vf_rlen[i];
-                if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-                     PCI_BASE_ADDRESS_MEM_TYPE_64 )
-                    ++i;
+                ret = pci_size_mem_bar(sbdf, idx, i == PCI_SRIOV_NUM_BARS - 1,
+                                       NULL, &pdev->vf_rlen[i], PCI_BAR_VF);
+                if ( ret < 0 )
+                    break;
+
+                ASSERT(ret);
+                i += ret;
             }
         }
         else
xen/include/xen/pci.h +4 -0
index b7a6abfc53..2bee6a3247 100644
@@ -189,6 +189,10 @@ const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
 const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
                           unsigned int *dev, unsigned int *func, bool *def_seg);
 
+#define _PCI_BAR_VF     0
+#define PCI_BAR_VF      (1u << _PCI_BAR_VF)
+int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
+                     uint64_t *addr, uint64_t *size, unsigned int flags);
 
 bool_t pcie_aer_get_firmware_first(const struct pci_dev *);
 
Signature · 7 lines
2.11.0 (Apple Git-81)


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

Re: [PATCH v6 05/11] pci: split code to size BARs from pci_add_device

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:

xen/drivers/passthrough/pci.c static int iommu_add_device(struct pci_dev *pdev); · line 612

Quoted 10 lines
xen/drivers/passthrough/pci.c +7 -0
@@ -603,6 +603,56 @@ static int iommu_add_device(struct pci_dev *pdev);
 static int iommu_enable_device(struct pci_dev *pdev);
 static int iommu_remove_device(struct pci_dev *pdev);
 
+int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
+                     uint64_t *paddr, uint64_t *psize, unsigned int flags)
+{
+    uint32_t hi = 0, bar = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
+                                           sbdf.func, pos);
+    uint64_t addr, size;
+    bool vf = flags & PCI_BAR_VF;

Honestly I'm not convinced of the utility of this variable; same for the "rom" one in the next patch.

Quoted 23 lines
xen/drivers/passthrough/pci.c +23 -0
+    ASSERT((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
+    pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, ~0);
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    {
+        if ( last )
+        {
+            printk(XENLOG_WARNING
+                   "%sdevice %04x:%02x:%02x.%u with 64-bit %sBAR in last slot\n",
+                   vf ? "SR-IOV " : "", sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func,
+                   vf ? "vf " : "");
+            return -EINVAL;
+        }
+        hi = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4);
+        pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, ~0);
+    }
+    size = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos) &
+           PCI_BASE_ADDRESS_MEM_MASK;
+    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    {
+        size |= (uint64_t)pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
+                                          sbdf.func, pos + 4) << 32;

xen/drivers/passthrough/pci.c static int iommu_add_device(struct pci_dev *pdev); · line 646

Quoted 10 lines
xen/drivers/passthrough/pci.c +10 -0
+        pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, hi);
+    }
+    else if ( size )
+        size |= (uint64_t)~0 << 32;
+    pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, bar);
+    size = -size;
+    addr = (bar & PCI_BASE_ADDRESS_MEM_MASK) | ((uint64_t)hi << 32);
+
+    if ( paddr )
+        *paddr = addr;

You need addr only inside the if() - no need for the local variable, and no need to calculate it unconditionally.

xen/include/xen/pci.h const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus, · line 193

Quoted 8 lines
xen/include/xen/pci.h +2 -0
@@ -189,6 +189,10 @@ const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
 const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
                           unsigned int *dev, unsigned int *func, bool *def_seg);
 
+#define _PCI_BAR_VF     0
+#define PCI_BAR_VF      (1u << _PCI_BAR_VF)

Do you really need both? I know we have quite a few cases where flags are being defined this way, but that's usually when bit operations (test_bit() and alike) are intended on the flags fields.

Jan

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

#20 lore raw

Re: [PATCH v6 05/11] pci: split code to size BARs from pci_add_device

Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/include/xen/pci.h +0 -0
@@ -189,6 +189,10 @@ const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
 const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
                           unsigned int *dev, unsigned int *func, bool *def_seg);
 
Quoted 6 lines

xen/include/xen/pci.h const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus, · line 193

Quoted 2 lines
xen/include/xen/pci.h +2 -0
+#define _PCI_BAR_VF     0
+#define PCI_BAR_VF      (1u << _PCI_BAR_VF)

Do you really need both? I know we have quite a few cases where flags are being defined this way, but that's usually when bit operations (test_bit() and alike) are intended on the flags fields.

Ack, would you then rather prefer to have 1, or (1u << 0)? (to keep it in line with the other flag that will be added later).

Thanks, Roger.

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

#21 lore raw

Re: [PATCH v6 05/11] pci: split code to size BARs from pci_add_device

Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 04.10.17 at 15:31, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/include/xen/pci.h +0 -0
@@ -189,6 +189,10 @@ const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
 const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
                           unsigned int *dev, unsigned int *func, bool *def_seg);
 
Quoted 6 lines

xen/include/xen/pci.h const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus, · line 193

Quoted 2 lines
xen/include/xen/pci.h +2 -0
+#define _PCI_BAR_VF     0
+#define PCI_BAR_VF      (1u << _PCI_BAR_VF)

Do you really need both? I know we have quite a few cases where flags are being defined this way, but that's usually when bit operations (test_bit() and alike) are intended on the flags fields.

Quoted 2 lines

Ack, would you then rather prefer to have 1, or (1u << 0)? (to keep it in line with the other flag that will be added later).

1u please, as that's going to be mandatory once someone adds a definition for the 32nd flag bit. The other option would be to use a plain hex constant without involving the shift operator.

Jan

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

#22 lore raw

PATCH v6 6/11 pci: add support to size ROM BARs to pci_size_mem_bar

In reply to Roger Pau Monne

---
Cc: Jan Beulich <[email protected]>
---
Changes since v5:
- Use the flags field.
- Introduce a mask local variable.
- Simplify return.

Changes since v4:
- New in this version.

---
 xen/drivers/passthrough/pci.c | 29 +++++++++++++++--------------
 xen/include/xen/pci.h         |  2 ++
 2 files changed, 17 insertions(+), 14 deletions(-)
xen/drivers/passthrough/pci.c +15 -14
index ba58b4d0cc..92c1f9354a 100644
@@ -610,11 +610,17 @@ int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
                                            sbdf.func, pos);
     uint64_t addr, size;
     bool vf = flags & PCI_BAR_VF;
-
-    ASSERT((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
+    bool rom = flags & PCI_BAR_ROM;
+    bool is64bits = !rom && (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+                    PCI_BASE_ADDRESS_MEM_TYPE_64;
+    uint32_t mask = rom ? (uint32_t)PCI_ROM_ADDRESS_MASK
+                        : (uint32_t)PCI_BASE_ADDRESS_MEM_MASK;
+
+    ASSERT(!(rom && vf));
+    ASSERT(rom ||
+           (bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
     pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, ~0);
-    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    if ( is64bits )
     {
         if ( last )
         {
@@ -627,10 +633,9 @@ int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
         hi = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4);
         pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos + 4, ~0);
     }
-    size = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos) &
-           PCI_BASE_ADDRESS_MEM_MASK;
-    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-         PCI_BASE_ADDRESS_MEM_TYPE_64 )
+    size = pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func,
+                           pos) & mask;
+    if ( is64bits )
     {
         size |= (uint64_t)pci_conf_read32(sbdf.seg, sbdf.bus, sbdf.dev,
                                           sbdf.func, pos + 4) << 32;
@@ -640,17 +645,13 @@ int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
         size |= (uint64_t)~0 << 32;
     pci_conf_write32(sbdf.seg, sbdf.bus, sbdf.dev, sbdf.func, pos, bar);
     size = -size;
-    addr = (bar & PCI_BASE_ADDRESS_MEM_MASK) | ((uint64_t)hi << 32);
+    addr = (bar & mask) | ((uint64_t)hi << 32);
 
     if ( paddr )
         *paddr = addr;
     *psize = size;
 
-    if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
-         PCI_BASE_ADDRESS_MEM_TYPE_64 )
-        return 2;
-
-    return 1;
+    return is64bits ? 2 : 1;
 }
 
 int pci_add_device(u16 seg, u8 bus, u8 devfn,
xen/include/xen/pci.h +2 -0
index 2bee6a3247..4489edf9b5 100644
@@ -191,6 +191,8 @@ const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
 
 #define _PCI_BAR_VF     0
 #define PCI_BAR_VF      (1u << _PCI_BAR_VF)
+#define _PCI_BAR_ROM    1
+#define PCI_BAR_ROM     (1u << _PCI_BAR_ROM)
 int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
                      uint64_t *addr, uint64_t *size, unsigned int flags);
 
Signature · 7 lines
2.11.0 (Apple Git-81)


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v6 6/11
#23 lore raw

Re: [PATCH v6 06/11] pci: add support to size ROM BARs to pci_size_mem_bar

xen/drivers/passthrough/pci.c int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last, · line 613

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/passthrough/pci.c +1 -2
@@ -610,11 +610,17 @@ int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, bool last,
                                            sbdf.func, pos);
     uint64_t addr, size;
     bool vf = flags & PCI_BAR_VF;
-
-    ASSERT((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY);
+    bool rom = flags & PCI_BAR_ROM;

Ideally with this local variable and ...

xen/include/xen/pci.h const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus, · line 195

Quoted 8 lines
xen/include/xen/pci.h +2 -0
@@ -191,6 +191,8 @@ const char *parse_pci_seg(const char *, unsigned int *seg, unsigned int *bus,
 
 #define _PCI_BAR_VF     0
 #define PCI_BAR_VF      (1u << _PCI_BAR_VF)
+#define _PCI_BAR_ROM    1
+#define PCI_BAR_ROM     (1u << _PCI_BAR_ROM)

... the first of these two dropped

Jan

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

#24 lore raw

PATCH v6 7/11 xen: introduce rangeset_consume_ranges

In reply to Roger Pau Monne

This function allows to iterate over a rangeset while removing the processed regions.

It will be used by the following patches in order to store memory regions in rangesets, and remove them while iterating.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v5:
- New in this version.

---
 xen/common/rangeset.c      | 28 ++++++++++++++++++++++++++++
 xen/include/xen/rangeset.h |  4 ++++
 2 files changed, 32 insertions(+)
xen/common/rangeset.c +28 -0
index 6c6293c15c..fd4a6b3384 100644
@@ -298,6 +298,34 @@ int rangeset_report_ranges(
     return rc;
 }
 
+int rangeset_consume_ranges(
+    struct rangeset *r,
+    int (*cb)(unsigned long s, unsigned long e, void *, unsigned long *c),
+    void *ctxt)
+{
+    int rc = 0;
+
+    write_lock(&r->lock);
+    while ( !rangeset_is_empty(r) )
+    {
+        unsigned long consumed = 0;
+        struct range *x = first_range(r);
+
+        rc = cb(x->s, x->e, ctxt, &consumed);
+
+        ASSERT(consumed <= x->e - x->s + 1);
+        x->s += consumed;
+        if ( x->s > x->e )
+            destroy_range(r, x);
+
+        if ( rc )
+            break;
+    }
+    write_unlock(&r->lock);
+
+    return rc;
+}
+
 int rangeset_add_singleton(
     struct rangeset *r, unsigned long s)
 {
xen/include/xen/rangeset.h +4 -0
index aa6408248b..dfdb193800 100644
@@ -67,6 +67,10 @@ bool_t __must_check rangeset_overlaps_range(
 int rangeset_report_ranges(
     struct rangeset *r, unsigned long s, unsigned long e,
     int (*cb)(unsigned long s, unsigned long e, void *), void *ctxt);
+int rangeset_consume_ranges(
+    struct rangeset *r,
+    int (*cb)(unsigned long s, unsigned long e, void *, unsigned long *c),
+    void *ctxt);
 
 /* Add/remove/query a single number. */
 int __must_check rangeset_add_singleton(
Signature · 7 lines
2.11.0 (Apple Git-81)


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

Re: [PATCH v6 07/11] xen: introduce rangeset_consume_ranges

Quoted 28 lines · Roger Pau Monne

This function allows to iterate over a rangeset while removing the processed regions.

It will be used by the following patches in order to store memory regions in rangesets, and remove them while iterating.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v5:
- New in this version.

---
 xen/common/rangeset.c      | 28 ++++++++++++++++++++++++++++
 xen/include/xen/rangeset.h |  4 ++++
 2 files changed, 32 insertions(+)
xen/common/rangeset.c +0 -0
index 6c6293c15c..fd4a6b3384 100644

xen/common/rangeset.c int rangeset_report_ranges( · line 299

Quoted 2 lines
xen/common/rangeset.c +0 -0
@@ -298,6 +298,34 @@ int rangeset_report_ranges(
     return rc;
 }

I think you need to document the behaviour of this new function due to its destructive nature.

Something like:

Iterate through the range within a range set. Call cb on each range provided. Bail on first error. Destroy the range processed when cb has consumed the whole range.

Though without reading further I don't know why cb will only consume part of the range but not all of it all the time.

Quoted 50 lines
xen/common/rangeset.c +28 -0
+int rangeset_consume_ranges(
+    struct rangeset *r,
+    int (*cb)(unsigned long s, unsigned long e, void *, unsigned long *c),
+    void *ctxt)
+{
+    int rc = 0;
+
+    write_lock(&r->lock);
+    while ( !rangeset_is_empty(r) )
+    {
+        unsigned long consumed = 0;
+        struct range *x = first_range(r);
+
+        rc = cb(x->s, x->e, ctxt, &consumed);
+
+        ASSERT(consumed <= x->e - x->s + 1);
+        x->s += consumed;
+        if ( x->s > x->e )
+            destroy_range(r, x);
+
+        if ( rc )
+            break;
+    }
+    write_unlock(&r->lock);
+
+    return rc;
+}
+
 int rangeset_add_singleton(
     struct rangeset *r, unsigned long s)
 {
xen/include/xen/rangeset.h +4 -0
index aa6408248b..dfdb193800 100644
@@ -67,6 +67,10 @@ bool_t __must_check rangeset_overlaps_range(
 int rangeset_report_ranges(
     struct rangeset *r, unsigned long s, unsigned long e,
     int (*cb)(unsigned long s, unsigned long e, void *), void *ctxt);
+int rangeset_consume_ranges(
+    struct rangeset *r,
+    int (*cb)(unsigned long s, unsigned long e, void *, unsigned long *c),
+    void *ctxt);
 
 /* Add/remove/query a single number. */
 int __must_check rangeset_add_singleton(
Signature · 1 line
2.11.0 (Apple Git-81)

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

#26 lore raw

Re: [PATCH v6 07/11] xen: introduce rangeset_consume_ranges

Quoted 35 lines · Wei Liu
Quoted 28 lines · Roger Pau Monne

This function allows to iterate over a rangeset while removing the processed regions.

It will be used by the following patches in order to store memory regions in rangesets, and remove them while iterating.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v5:
- New in this version.

---
 xen/common/rangeset.c      | 28 ++++++++++++++++++++++++++++
 xen/include/xen/rangeset.h |  4 ++++
 2 files changed, 32 insertions(+)
xen/common/rangeset.c +0 -0
index 6c6293c15c..fd4a6b3384 100644

xen/common/rangeset.c int rangeset_report_ranges( · line 299

Quoted 2 lines
xen/common/rangeset.c +0 -0
@@ -298,6 +298,34 @@ int rangeset_report_ranges(
     return rc;
 }

I think you need to document the behaviour of this new function due to its destructive nature.

Quoted 5 lines

Something like:

Iterate through the range within a range set. Call cb on each range provided. Bail on first error. Destroy the range processed when cb has consumed the whole range.

OK, I thought that the 'consume' in the name was enough, but now that you have written the comment I certainly don't mind adding it ;).

Quoted 2 lines

Though without reading further I don't know why cb will only consume part of the range but not all of it all the time.

I guess you have to look at the next patch and it's usage. This will be used to store all the MMIO areas that need to be mapped into a domain p2m.

Some of the ranges might be very big (BARs from gfx cards for example), and might require preemption in order to map them, hence the emulated PCI code needs a way to store it's progress, and that's done by partially consuming a range.

Thanks, Roger.

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

#27 lore raw

Re: [PATCH v6 07/11] xen: introduce rangeset_consume_ranges

Quoted 48 lines · Roger Pau Monné
Quoted 35 lines · Wei Liu
Quoted 28 lines · Roger Pau Monne

This function allows to iterate over a rangeset while removing the processed regions.

It will be used by the following patches in order to store memory regions in rangesets, and remove them while iterating.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v5:
- New in this version.

---
 xen/common/rangeset.c      | 28 ++++++++++++++++++++++++++++
 xen/include/xen/rangeset.h |  4 ++++
 2 files changed, 32 insertions(+)
xen/common/rangeset.c +0 -0
index 6c6293c15c..fd4a6b3384 100644

xen/common/rangeset.c int rangeset_report_ranges( · line 299

Quoted 2 lines
xen/common/rangeset.c +0 -0
@@ -298,6 +298,34 @@ int rangeset_report_ranges(
     return rc;
 }

I think you need to document the behaviour of this new function due to its destructive nature.

Quoted 5 lines

Something like:

Iterate through the range within a range set. Call cb on each range provided. Bail on first error. Destroy the range processed when cb has consumed the whole range.

OK, I thought that the 'consume' in the name was enough, but now that you have written the comment I certainly don't mind adding it ;).

Quoted 2 lines

Though without reading further I don't know why cb will only consume part of the range but not all of it all the time.

Quoted 9 lines

I guess you have to look at the next patch and it's usage. This will be used to store all the MMIO areas that need to be mapped into a domain p2m.

Some of the ranges might be very big (BARs from gfx cards for example), and might require preemption in order to map them, hence the emulated PCI code needs a way to store it's progress, and that's done by partially consuming a range.

Ah, so the goal is indeed to consume all the ranges within the rangeset. This makes more sense now.

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

#28 lore raw

Re: [PATCH v6 07/11] xen: introduce rangeset_consume_ranges

In reply to Roger Pau Monne
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:

This function allows to iterate over a rangeset while removing the processed regions.

It will be used by the following patches in order to store memory regions in rangesets, and remove them while iterating.

This really only repeats what the first paragraph already says. Instead you want to state why this is actually needed (to be able to split processing aiui).

Quoted 23 lines
xen/common/rangeset.c +17 -0
@@ -298,6 +298,34 @@ int rangeset_report_ranges(
     return rc;
 }
 
+int rangeset_consume_ranges(
+    struct rangeset *r,
+    int (*cb)(unsigned long s, unsigned long e, void *, unsigned long *c),
+    void *ctxt)
+{
+    int rc = 0;
+
+    write_lock(&r->lock);
+    while ( !rangeset_is_empty(r) )
+    {
+        unsigned long consumed = 0;
+        struct range *x = first_range(r);
+
+        rc = cb(x->s, x->e, ctxt, &consumed);
+
+        ASSERT(consumed <= x->e - x->s + 1);
+        x->s += consumed;

xen/common/rangeset.c int rangeset_report_ranges( · line 327

Quoted 10 lines
xen/common/rangeset.c +10 -0
+        if ( x->s > x->e )
+            destroy_range(r, x);
+
+        if ( rc )
+            break;
+    }
+    write_unlock(&r->lock);
+
+    return rc;
+}

Leaving the rangeset populated in case of error (other than -ERESTART) looks to be potentially problematic/unexpected. Please at least add a comment in the header stating this. Perhaps negative vs positive rc from the callback could be used to direct intended behavior.

xen/include/xen/rangeset.h bool_t __must_check rangeset_overlaps_range( · line 73

Quoted 10 lines
xen/include/xen/rangeset.h +4 -0
@@ -67,6 +67,10 @@ bool_t __must_check rangeset_overlaps_range(
 int rangeset_report_ranges(
     struct rangeset *r, unsigned long s, unsigned long e,
     int (*cb)(unsigned long s, unsigned long e, void *), void *ctxt);
+int rangeset_consume_ranges(
+    struct rangeset *r,
+    int (*cb)(unsigned long s, unsigned long e, void *, unsigned long *c),
+    void *ctxt);

Indentation.

Jan

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

#29 lore raw

PATCH v6 8/11 vpci/bars: add handlers to map the BARs

In reply to Roger Pau Monne

Introduce a set of handlers that trap accesses to the PCI BARs and the command register, in order to snoop BAR sizing and BAR relocation.

The command handler is used to detect changes to bit 2 (response to memory space accesses), and maps/unmaps the BARs of the device into the guest p2m. A rangeset is used in order to figure out which memory to map/unmap. This makes it easier to keep track of the possible overlaps with other BARs, and will also simplify MSI-X support, where certain regions of a BAR might be used for the MSI-X table or PBA.

The BAR register handlers are used to detect attempts by the guest to size or relocate the BARs.

Note that the long running BAR mapping and unmapping operations are deferred to be performed by hvm_io_pending, so that they can be safely preempted.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v5:
- Switch to the new handler type.
- Use pci_sbdf_t to size the BARs.
- Use a single return for vpci_modify_bar.
- Do not return an error code from vpci_modify_bars, just log the failure.
- Remove the 'sizing' parameter. Instead just let the guest write directly to the BAR, and read the value back. This simplifies the BAR register handlers, specially the read one.
- Ignore ROM BAR writes with memory decoding enabled and ROM enabled.
- Do not propagate failures to setup the ROM BAR in vpci_init_bars.
- Add preemption support to the BAR mapping/unmapping operations.

Changes since v4:
- Expand commit message to mention the reason behind the usage of rangesets.
- Fix comment related to the inclusiveness of rangesets.
- Fix off-by-one error in the calculation of the end of memory regions.
- Store the state of the BAR (mapped/unmapped) in the vpci_bar enabled field, previously was only used by ROMs.
- Fix double negation of return code.
- Modify vpci_cmd_write so it has a single call to pci_conf_write16.
- Print a warning when trying to write to the BAR with memory decoding enabled (and ignore the write).
- Remove header_type local variable, it's used only once.
- Move the read of the command register.
- Restore previous command register value in the exit paths.
- Only set address to INVALID_PADDR if the initial BAR value matches

    ~0 & PCI_BASE_ADDRESS_MEM_MASK.

- Don't disable the enabled bit in the expansion ROM register, memory decoding is already disabled and takes precedence.
- Don't use INVALID_PADDR, just set the initial BAR address to the value found in the hardware.
- Introduce rom_enabled to store the status of the PCI_ROM_ADDRESS_ENABLE bit.
- Reorder fields of the structure to prevent holes.

Changes since v3:
- Propagate previous changes: drop xen_ prefix and use u8/u16/u32 instead of the previous half_word/word/double_word.
- Constify some of the paramerters.
- s/VPCI_BAR_MEM/VPCI_BAR_MEM32/.
- Simplify the number of fields stored for each BAR, a single address field is stored and contains the address of the BAR both on Xen and in the guest.
- Allow the guest to move the BARs around in the physical memory map.
- Add support for expansion ROM BARs.
- Do not cache the value of the command register.
- Remove a label used in vpci_cmd_write.
- Fix the calculation of the sizing mask in vpci_bar_write.
- Check the memory decode bit in order to decide if a BAR is positioned or not.
- Disable memory decoding before sizing the BARs in Xen.
- When mapping/unmapping BARs check if there's overlap between BARs, in order to avoid unmapping memory required by another BAR.
- Introduce a macro to check whether a BAR is mappable or not.
- Add a comment regarding the lack of support for SR-IOV.
- Remove the usage of the GENMASK macro.

Changes since v2:
- Detect unset BARs and allow the hardware domain to position them.

---
 xen/arch/x86/hvm/ioreq.c  |   4 +
 xen/drivers/vpci/Makefile |   2 +-
 xen/drivers/vpci/header.c | 478 ++++++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/sched.h   |   8 +
 xen/include/xen/vpci.h    |  41 ++++
 5 files changed, 532 insertions(+), 1 deletion(-)
 create mode 100644 xen/drivers/vpci/header.c
xen/arch/x86/hvm/ioreq.c +4 -0
index 3e7a88e053..f6588ceab4 100644
@@ -26,6 +26,7 @@
 #include <xen/domain.h>
 #include <xen/event.h>
 #include <xen/paging.h>
+#include <xen/vpci.h>
 
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/ioreq.h>
@@ -48,6 +49,9 @@ bool_t hvm_io_pending(struct vcpu *v)
     struct domain *d = v->domain;
     struct hvm_ioreq_server *s;
 
+    if ( has_vpci(v->domain) && vpci_check_pending(v) )
+      return 1;
+
     list_for_each_entry ( s,
                           &d->arch.hvm_domain.ioreq_server.list,
                           list_entry )
xen/drivers/vpci/Makefile +1 -1
index 840a906470..241467212f 100644
@@ -1 +1 @@
-obj-y += vpci.o
+obj-y += vpci.o header.o
xen/drivers/vpci/header.c new file +478 -0

478 lines not on this page · raw message

xen/include/xen/sched.h +8 -0
index b03afb450d..39a330ffca 100644
@@ -20,6 +20,9 @@
 #include <xen/smp.h>
 #include <xen/perfc.h>
 #include <asm/atomic.h>
+#ifdef CONFIG_HAS_PCI
+#include <xen/vpci.h>
+#endif
 #include <xen/wait.h>
 #include <public/xen.h>
 #include <public/domctl.h>
@@ -264,6 +267,11 @@ struct vcpu
 
     struct evtchn_fifo_vcpu *evtchn_fifo;
 
+#ifdef CONFIG_HAS_PCI
+    /* vPCI per-vCPU area, used to store data for long running operations. */
+    struct vpci_vcpu vpci;
+#endif
+
     struct arch_vcpu arch;
 };
 
xen/include/xen/vpci.h +41 -0

52 lines not on this page · raw message

Signature · 7 lines
2.11.0 (Apple Git-81)


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

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

xen/arch/x86/hvm/ioreq.c bool_t hvm_io_pending(struct vcpu *v) · line 52

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/arch/x86/hvm/ioreq.c +1 -0
@@ -48,6 +49,9 @@ bool_t hvm_io_pending(struct vcpu *v)
     struct domain *d = v->domain;
     struct hvm_ioreq_server *s;
 
+    if ( has_vpci(v->domain) && vpci_check_pending(v) )

has_vpci(d)

xen/arch/x86/hvm/ioreq.c bool_t hvm_io_pending(struct vcpu *v) · line 53

Quoted 1 line
xen/arch/x86/hvm/ioreq.c +1 -0
+      return 1;

Indentation.

Quoted 14 lines
xen/drivers/vpci/header.c new file +14 -0
+static int vpci_map_range(unsigned long s, unsigned long e, void *data,
+                          unsigned long *c)
+{
+    const struct map_data *map = data;
+    int rc;
+
+    for ( ; ; )
+    {
+        unsigned long size = e - s + 1;
+
+        rc = (map->map ? map_mmio_regions : unmap_mmio_regions)
+             (map->d, _gfn(s), size, _mfn(s));
+        if ( rc == 0 )
+        {

xen/drivers/vpci/header.c line 59

Quoted 10 lines
xen/drivers/vpci/header.c new file +10 -0
+            *c += size;
+            break;
+        }
+        if ( rc < 0 )
+        {
+            printk(XENLOG_G_WARNING
+                   "Failed to identity %smap [%" PRI_gfn ", %" PRI_gfn ") for d%d: %d\n",
+                   map ? "" : "un", s, e, map->d->domain_id, rc);
+            break;
+        }

ASSERT(rc < size) ?

Quoted 1 line

+bool vpci_check_pending(struct vcpu *v)

"check" in the function name generally suggests (to me at least) that the parameter ought to be const. Perhaps vpci_process_pending()?

xen/drivers/vpci/header.c line 88

Quoted 7 lines
xen/drivers/vpci/header.c new file +7 -0
+{
+    if ( v->vpci.mem )
+    {
+        int rc = vpci_map_memory(v->domain, v->vpci.mem, v->vpci.map);
+
+        if ( rc == -ERESTART )
+            return true;

There's no real need for the local variable if all other return values are simply discarded here. However, ...

xen/drivers/vpci/header.c line 95

Quoted 6 lines
xen/drivers/vpci/header.c new file +6 -0
+        rangeset_destroy(v->vpci.mem);
+        v->vpci.mem = NULL;
+    }
+
+    return false;
+}

... I'm not convinced this is a good error handling model. I don't recall how previous versions dealt with this, but iirc we agreed to generally make all such Dom0 handling best effort (here: don't skip the remaining ranges if mapping of one failed). An exception may want/need to be -ENOMEM.

xen/drivers/vpci/header.c line 130

Quoted 13 lines
xen/drivers/vpci/header.c new file +13 -0
+static int vpci_check_bar_overlap(const struct pci_dev *pdev,
+                                  const struct vpci_bar *rom,
+                                  struct rangeset *mem)
+{
+    const struct pci_dev *cmp;
+
+    /* Check for overlaps with other device's BARs. */
+    list_for_each_entry(cmp, &pdev->domain->arch.pdev_list, domain_list)
+    {
+        unsigned int i;
+
+        if ( rom == NULL && pdev == cmp )
+            continue;

This check looks rather unmotivated (or even bogus) without a comment. The other special casing of ROM BARs further down also isn't all that obvious (and right now I can't even convince myself it's correct).

Quoted 24 lines
xen/drivers/vpci/header.c new file +24 -0
+static void vpci_modify_bars(const struct pci_dev *pdev, bool map)
+{
+    struct vpci_header *header = &pdev->vpci->header;
+    struct rangeset *mem = rangeset_new(NULL, NULL, 0);
+    unsigned int i;
+    int rc;
+
+    if ( !mem )
+        return;
+
+    /*
+     * Create a rangeset that represents the current device BARs memory region
+     * and compare it against all the currently active BAR memory regions. If
+     * an overlap is found, subtract it from the region to be
+     * mapped/unmapped.
+     *
+     * NB: the rangeset uses inclusive frame numbers.
+     */
+
+    /* First fill the rangeset with all the BARs of this device. */
+    for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
+    {
+        const struct vpci_bar *bar = &header->bars[i];
+

xen/drivers/vpci/header.c line 185

Quoted 10 lines
xen/drivers/vpci/header.c new file +10 -0
+        if ( !MAPPABLE_BAR(bar) ||
+             (bar->type == VPCI_BAR_ROM && !bar->rom_enabled) )
+            continue;
+
+        rc = rangeset_add_range(mem, PFN_DOWN(bar->addr),
+                                PFN_DOWN(bar->addr + bar->size - 1));
+        if ( rc )
+        {
+            rangeset_destroy(mem);
+            return;

I'm afraid -ENOMEM here (which sadly is possible, as we don't maintain any reserves) would produce a very hard to diagnose misbehavior. I think you want to log a message here.

xen/drivers/vpci/header.c line 190

Quoted 5 lines
xen/drivers/vpci/header.c new file +5 -0
+        }
+    }
+
+    /* Check for overlaps with other device's BARs. */
+    rc = vpci_check_bar_overlap(pdev, NULL, mem);

Why is this not symmetrical with vpci_modify_rom() (which also checks overlaps inside the current device)?

xen/drivers/vpci/header.c line 185

Quoted 4 lines
xen/drivers/vpci/header.c new file +4 -0
+    if ( rc )
+    {
+        rangeset_destroy(mem);
+        return;

Same error handling comment as above, despite failure here being less likely (hopefully at least). Perhaps worth joining the two paths.

xen/drivers/vpci/header.c line 202

Quoted 8 lines
xen/drivers/vpci/header.c new file +8 -0
+    }
+
+    rc = vpci_maybe_defer_map(pdev->domain, mem, map);
+    if ( !rc )
+        for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
+            if ( header->bars[i].type != VPCI_BAR_ROM ||
+                 header->bars[i].rom_enabled )
+            header->bars[i].enabled = map;

Hmm, you're updating state here regardless of possible failure in the deferred operation (see the discarded error code in vpci_check_pending()).

xen/drivers/vpci/header.c line 246

Quoted 6 lines
xen/drivers/vpci/header.c new file +6 -0
+static uint32_t vpci_cmd_read(const struct pci_dev *pdev, unsigned int reg,
+                              void *data)
+{
+    return pci_conf_read16(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                           PCI_FUNC(pdev->devfn), reg);
+}

Wouldn't it be worthwhile having generic read functions dealing with simple cases like this (and the BAR) one?

xen/drivers/vpci/header.c line 258

Quoted 11 lines
xen/drivers/vpci/header.c new file +11 -0
+static void vpci_cmd_write(const struct pci_dev *pdev, unsigned int reg,
+                           uint32_t cmd, void *data)
+{
+    uint8_t seg = pdev->seg, bus = pdev->bus;
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    uint16_t current_cmd = pci_conf_read16(seg, bus, slot, func, reg);
+
+    /*
+     * Let the guest play with all the bits directly except for the
+     * memory decoding one.
+     */

Please could you make clear it's only Dom0 we apply this lax model to? Perhaps simply s/the guest/Dom0/.

Quoted 4 lines
xen/drivers/vpci/header.c new file +4 -0
+static void vpci_bar_write(const struct pci_dev *pdev, unsigned int reg,
+                           uint32_t val, void *data)
+{
+    struct vpci_bar *bar = data;

xen/drivers/vpci/header.c line 285

Quoted 10 lines
xen/drivers/vpci/header.c new file +10 -0
+    uint8_t seg = pdev->seg, bus = pdev->bus;
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    bool hi = false;
+
+    if ( pci_conf_read16(seg, bus, slot, func, PCI_COMMAND) &
+         PCI_COMMAND_MEMORY )
+    {
+         gprintk(XENLOG_WARNING,
+                 "%04x:%02x:%02x.%u: ignored BAR write with memory decoding enabled\n",
+                 seg, bus, slot, func);

Indentation. Also any chance to log which BAR it was?

xen/drivers/vpci/header.c line 325

Quoted 9 lines
xen/drivers/vpci/header.c new file +9 -0
+static void vpci_rom_write(const struct pci_dev *pdev, unsigned int reg,
+                           uint32_t val, void *data)
+{
+    struct vpci_bar *rom = data;
+    uint8_t seg = pdev->seg, bus = pdev->bus;
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    uint16_t cmd = pci_conf_read16(seg, bus, slot, func, PCI_COMMAND);
+
+    if ( (pci_conf_read16(seg, bus, slot, func, PCI_COMMAND) &

Please use cmd here.

xen/drivers/vpci/header.c line 330

Quoted 5 lines
xen/drivers/vpci/header.c new file +5 -0
+          PCI_COMMAND_MEMORY) && rom->rom_enabled )
+    {
+         gprintk(XENLOG_WARNING,
+                 "%04x:%02x:%02x.%u: ignored ROM BAR write with memory decoding enabled\n",
+                 seg, bus, slot, func);

Indentation again.

Quoted 51 lines
xen/drivers/vpci/header.c new file +51 -0
+static int vpci_init_bars(struct pci_dev *pdev)
+{
+    uint8_t seg = pdev->seg, bus = pdev->bus;
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    uint16_t cmd;
+    uint64_t addr, size;
+    unsigned int i, num_bars, rom_reg;
+    struct vpci_header *header = &pdev->vpci->header;
+    struct vpci_bar *bars = header->bars;
+    pci_sbdf_t sbdf = {
+        .seg = seg,
+        .bus = bus,
+        .dev = slot,
+        .func = func,
+    };
+    int rc;
+
+    switch ( pci_conf_read8(seg, bus, slot, func, PCI_HEADER_TYPE) & 0x7f )
+    {
+    case PCI_HEADER_TYPE_NORMAL:
+        num_bars = 6;
+        rom_reg = PCI_ROM_ADDRESS;
+        break;
+    case PCI_HEADER_TYPE_BRIDGE:
+        num_bars = 2;
+        rom_reg = PCI_ROM_ADDRESS1;
+        break;
+    default:
+        return -EOPNOTSUPP;
+    }
+
+    /* Setup a handler for the command register. */
+    rc = vpci_add_register(pdev, vpci_cmd_read, vpci_cmd_write, PCI_COMMAND,
+                           2, header);
+    if ( rc )
+        return rc;
+
+    /* Disable memory decoding before sizing. */
+    cmd = pci_conf_read16(seg, bus, slot, func, PCI_COMMAND);
+    if ( cmd & PCI_COMMAND_MEMORY )
+        pci_conf_write16(seg, bus, slot, func, PCI_COMMAND,
+                         cmd & ~PCI_COMMAND_MEMORY);
+
+    for ( i = 0; i < num_bars; i++ )
+    {
+        uint8_t reg = PCI_BASE_ADDRESS_0 + i * 4;
+        uint32_t val = pci_conf_read32(seg, bus, slot, func, reg);
+
+        if ( i && bars[i - 1].type == VPCI_BAR_MEM64_LO )
+        {
+            bars[i].type = VPCI_BAR_MEM64_HI;

xen/drivers/vpci/header.c line 405

Quoted 10 lines
xen/drivers/vpci/header.c new file +10 -0
+            rc = vpci_add_register(pdev, vpci_bar_read, vpci_bar_write, reg, 4,
+                                   &bars[i]);
+            if ( rc )
+            {
+                pci_conf_write16(seg, bus, slot, func, PCI_COMMAND, cmd);
+                return rc;
+            }
+
+            continue;
+        }

You don't need val up to here - please defer the read.

xen/drivers/vpci/header.c line 417

Quoted 12 lines
xen/drivers/vpci/header.c new file +12 -0
+        if ( (val & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO )
+        {
+            bars[i].type = VPCI_BAR_IO;
+            continue;
+        }
+        if ( (val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
+             PCI_BASE_ADDRESS_MEM_TYPE_64 )
+            bars[i].type = VPCI_BAR_MEM64_LO;
+        else
+            bars[i].type = VPCI_BAR_MEM32;
+
+        /* Size the BAR and map it. */

Isn't the map part of this comment stale now? And without it, considering the function name called, it is perhaps no longer worth having it.

xen/include/xen/sched.h line 25

Quoted 9 lines
xen/include/xen/sched.h +3 -0
@@ -20,6 +20,9 @@
 #include <xen/smp.h>
 #include <xen/perfc.h>
 #include <asm/atomic.h>
+#ifdef CONFIG_HAS_PCI
+#include <xen/vpci.h>
+#endif

Perhaps the conditional would better live in that header.

xen/include/xen/sched.h struct vcpu · line 273

Quoted 8 lines
xen/include/xen/sched.h +4 -0
@@ -264,6 +267,11 @@ struct vcpu
 
     struct evtchn_fifo_vcpu *evtchn_fifo;
 
+#ifdef CONFIG_HAS_PCI
+    /* vPCI per-vCPU area, used to store data for long running operations. */
+    struct vpci_vcpu vpci;
+#endif

And perhaps the header would better provide an empty structure for the "else" case. Another option would be to include the fields ...

xen/include/xen/sched.h struct vcpu · line 275

Quoted 1 line
xen/include/xen/sched.h +0 -0
     struct arch_vcpu arch;

... in this structure.

Quoted 30 lines
xen/include/xen/vpci.h +20 -0
@@ -35,11 +35,52 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
 void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
                 uint32_t data);
 
+/*
+ * Check for pending vPCI operations on this vcpu. Returns true if the vcpu
+ * should not run.
+ */
+bool vpci_check_pending(struct vcpu *v);
+
 struct vpci {
     /* List of vPCI handlers for a device. */
     struct list_head handlers;
     spinlock_t lock;
+
+#ifdef __XEN__
+    /* Hide the rest of the vpci struct from the user-space test harness. */
+    struct vpci_header {
+        /* Information about the PCI BARs of this device. */
+        struct vpci_bar {
+            paddr_t addr;
+            uint64_t size;
+            enum {
+                VPCI_BAR_EMPTY,
+                VPCI_BAR_IO,
+                VPCI_BAR_MEM32,
+                VPCI_BAR_MEM64_LO,
+                VPCI_BAR_MEM64_HI,

xen/include/xen/vpci.h uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size); · line 71

Quoted 10 lines
xen/include/xen/vpci.h +10 -0
+                VPCI_BAR_ROM,
+            } type;
+            bool prefetchable;
+            /* Store whether the BAR is mapped into guest p2m. */
+            bool enabled;
+            /*
+             * Store whether the ROM enable bit is set (doesn't imply ROM BAR
+             * is mapped into guest p2m). Only used for type VPCI_BAR_ROM.
+             */
+            bool rom_enabled;

Especially with the error handling issue in mind that I've mentioned earlier, I wonder whether this field shouldn't be dropped, along the lines of you also no longer caching the memory decode enable bit in the command register.

Jan

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

#31 lore raw

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/header.c new file +9 -0
+static int vpci_check_bar_overlap(const struct pci_dev *pdev,
+                                  const struct vpci_bar *rom,
+                                  struct rangeset *mem)
+{
+    const struct pci_dev *cmp;
+
+    /* Check for overlaps with other device's BARs. */
+    list_for_each_entry(cmp, &pdev->domain->arch.pdev_list, domain_list)
+    {
Quoted 8 lines

xen/drivers/vpci/header.c line 130

Quoted 4 lines
xen/drivers/vpci/header.c new file +4 -0
+        unsigned int i;
+
+        if ( rom == NULL && pdev == cmp )
+            continue;

This check looks rather unmotivated (or even bogus) without a comment. The other special casing of ROM BARs further down also isn't all that obvious (and right now I can't even convince myself it's correct).

I've added the following comment before this check, which I think explains the logic for this check, and the one below:

Since ROM BARs can be enabled independently of the memory decoding bit we need to check for overlapping in slightly different ways depending on the case.

If !rom it means the memory decoding bit has been toggled, and all BARs belonging to the device will be {un}mapped, hence the rangeset will contain the mappings for the whole device. In this case there's no need to check for overlaps with BARs that belong to the same device because the rangeset is able to deal with overlapping areas.

OTOH, if rom is set if means a single ROM BAR is being {un}mapped, and hence the check for overlaps should be performed against all the possible BARs, even the ones that belong to the device being modified.

Quoted 8 lines

xen/drivers/vpci/header.c line 190

Quoted 5 lines
xen/drivers/vpci/header.c new file +5 -0
+        }
+    }
+
+    /* Check for overlaps with other device's BARs. */
+    rc = vpci_check_bar_overlap(pdev, NULL, mem);

Why is this not symmetrical with vpci_modify_rom() (which also checks overlaps inside the current device)?

I think the comment above should answer the question here, the difference is because in this case Xen is mapping a whole device, so vpci_check_bar_overlap should not check for overlap with BARs that belong to the same device. OTOH, when mapping a ROM BAR Xen should check for such overlap, because the regular BARs will already be mapped.

Quoted 9 lines

xen/drivers/vpci/header.c line 202

Quoted 8 lines
xen/drivers/vpci/header.c new file +8 -0
+    }
+
+    rc = vpci_maybe_defer_map(pdev->domain, mem, map);
+    if ( !rc )
+        for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
+            if ( header->bars[i].type != VPCI_BAR_ROM ||
+                 header->bars[i].rom_enabled )
+            header->bars[i].enabled = map;
Quoted 3 lines

Hmm, you're updating state here regardless of possible failure in the deferred operation (see the discarded error code in vpci_check_pending()).

Yes, I've fixed the code above to try to map/unmap as much as possible, even when a failure happens.

I agree that enabling/disabling here with the operation being deferred is not ideal, but I also think we would end up doing the same regardless of the outcome of the deferred operation. If some mapping/unmapping of BARs failed, the memory decoding should be enabled anyway. I can add a comment along this lines if you think that's OK.

Quoted 24 lines

xen/include/xen/sched.h line 25

Quoted 9 lines
xen/include/xen/sched.h +3 -0
@@ -20,6 +20,9 @@
 #include <xen/smp.h>
 #include <xen/perfc.h>
 #include <asm/atomic.h>
+#ifdef CONFIG_HAS_PCI
+#include <xen/vpci.h>
+#endif

Perhaps the conditional would better live in that header.

xen/include/xen/sched.h struct vcpu · line 273

Quoted 8 lines
xen/include/xen/sched.h +4 -0
@@ -264,6 +267,11 @@ struct vcpu
 
     struct evtchn_fifo_vcpu *evtchn_fifo;
 
+#ifdef CONFIG_HAS_PCI
+    /* vPCI per-vCPU area, used to store data for long running operations. */
+    struct vpci_vcpu vpci;
+#endif

And perhaps the header would better provide an empty structure for the "else" case. Another option would be to include the fields ...

Quoted 3 lines

xen/include/xen/sched.h struct vcpu · line 275

Quoted 1 line
xen/include/xen/sched.h +0 -0
     struct arch_vcpu arch;

... in this structure.

I've thought about placing the vpci data inside of arch vpcu, but it felt a little bit weird because the vpci code should be arch-agnostic, so placing some of it's data inside of an arch specific structure seemed wrong. I will do as you suggest and provide an empty vpci_vpcu structure in the !CONFIG_HAS_PCI case, together with hiding the CONFIG_HAS_PCI macros inside of the header itself.

Quoted 37 lines
Quoted 37 lines
xen/include/xen/vpci.h +27 -0
@@ -35,11 +35,52 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
 void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
                 uint32_t data);
 
+/*
+ * Check for pending vPCI operations on this vcpu. Returns true if the vcpu
+ * should not run.
+ */
+bool vpci_check_pending(struct vcpu *v);
+
 struct vpci {
     /* List of vPCI handlers for a device. */
     struct list_head handlers;
     spinlock_t lock;
+
+#ifdef __XEN__
+    /* Hide the rest of the vpci struct from the user-space test harness. */
+    struct vpci_header {
+        /* Information about the PCI BARs of this device. */
+        struct vpci_bar {
+            paddr_t addr;
+            uint64_t size;
+            enum {
+                VPCI_BAR_EMPTY,
+                VPCI_BAR_IO,
+                VPCI_BAR_MEM32,
+                VPCI_BAR_MEM64_LO,
+                VPCI_BAR_MEM64_HI,
+                VPCI_BAR_ROM,
+            } type;
+            bool prefetchable;
+            /* Store whether the BAR is mapped into guest p2m. */
+            bool enabled;
+            /*
+             * Store whether the ROM enable bit is set (doesn't imply ROM BAR
Quoted 8 lines

xen/include/xen/vpci.h uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size); · line 71

Quoted 3 lines
xen/include/xen/vpci.h +3 -0
+             * is mapped into guest p2m). Only used for type VPCI_BAR_ROM.
+             */
+            bool rom_enabled;

Especially with the error handling issue in mind that I've mentioned earlier, I wonder whether this field shouldn't be dropped, along the lines of you also no longer caching the memory decode enable bit in the command register.

Removing rom_enabled would imply doing a register read in vpci_modify_bars in order to know whether the ROM BAR is enabled or not, which is not trivial because depending on the header type the position of the ROM BAR is different.

Another option would be to store the prefetch/enable bits inside of the addr field, but that would also require more masking/unmasking of the fields when the values are used or updated.

Roger.

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

#32 lore raw

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

Quoted 24 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 11:20, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/header.c new file +9 -0
+static int vpci_check_bar_overlap(const struct pci_dev *pdev,
+                                  const struct vpci_bar *rom,
+                                  struct rangeset *mem)
+{
+    const struct pci_dev *cmp;
+
+    /* Check for overlaps with other device's BARs. */
+    list_for_each_entry(cmp, &pdev->domain->arch.pdev_list, domain_list)
+    {
Quoted 8 lines

xen/drivers/vpci/header.c line 130

Quoted 4 lines
xen/drivers/vpci/header.c new file +4 -0
+        unsigned int i;
+
+        if ( rom == NULL && pdev == cmp )
+            continue;

This check looks rather unmotivated (or even bogus) without a comment. The other special casing of ROM BARs further down also isn't all that obvious (and right now I can't even convince myself it's correct).

I've added the following comment before this check, which I think explains the logic for this check, and the one below:

Quoted 6 lines

Since ROM BARs can be enabled independently of the memory decoding bit we need to check for overlapping in slightly different ways depending on the case.

If !rom it means the memory decoding bit has been toggled, and all BARs belonging to the device will be {un}mapped,

That's not precise: When mapping, you may still skip the ROM one if its enable bit is clear. Whether the difference matters for unmapping when the ROM is already unmapped I can't tell right away. Nevertheless I think ...

Quoted 4 lines

hence the rangeset
will contain the mappings for the whole device. In this case there's no need to check for overlaps with BARs that belong to the same device because the rangeset is able to deal with overlapping areas.

... the conclusion is correct, as I would expect the ROM range to simply not be part of the rangeset then.

Quoted 9 lines
Quoted 8 lines

xen/drivers/vpci/header.c line 190

Quoted 5 lines
xen/drivers/vpci/header.c new file +5 -0
+        }
+    }
+
+    /* Check for overlaps with other device's BARs. */
+    rc = vpci_check_bar_overlap(pdev, NULL, mem);

Why is this not symmetrical with vpci_modify_rom() (which also checks overlaps inside the current device)?

Quoted 6 lines

I think the comment above should answer the question here, the difference is because in this case Xen is mapping a whole device, so vpci_check_bar_overlap should not check for overlap with BARs that belong to the same device. OTOH, when mapping a ROM BAR Xen should check for such overlap, because the regular BARs will already be mapped.

Right. Part of my confusion results from the naming of these two functions (pretty similar despite their different call sites) as well as their placement (modify_bars() sitting ahead of the CMD write is fine, as it's a helper of that function, but modify_rom() would better be moved down to make clear whose helper it is; it's questionable whether this being a separate helper function is actually useful).

Quoted 16 lines
Quoted 9 lines

xen/drivers/vpci/header.c line 202

Quoted 8 lines
xen/drivers/vpci/header.c new file +8 -0
+    }
+
+    rc = vpci_maybe_defer_map(pdev->domain, mem, map);
+    if ( !rc )
+        for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
+            if ( header->bars[i].type != VPCI_BAR_ROM ||
+                 header->bars[i].rom_enabled )
+            header->bars[i].enabled = map;
Quoted 3 lines

Hmm, you're updating state here regardless of possible failure in the deferred operation (see the discarded error code in vpci_check_pending()).

Yes, I've fixed the code above to try to map/unmap as much as possible, even when a failure happens.

Quoted 6 lines

I agree that enabling/disabling here with the operation being deferred is not ideal, but I also think we would end up doing the same regardless of the outcome of the deferred operation. If some mapping/unmapping of BARs failed, the memory decoding should be enabled anyway. I can add a comment along this lines if you think that's OK.

Yes, at least explaining why things are the (not fully correct) way they are would help (also to tell anyone wanting to improve this what it actually is that would need changing). Of course even better would be if maintained state would match the state hardware is in.

Quoted 46 lines
Quoted 37 lines
Quoted 37 lines
xen/include/xen/vpci.h +27 -0
@@ -35,11 +35,52 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
 void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
                 uint32_t data);
 
+/*
+ * Check for pending vPCI operations on this vcpu. Returns true if the vcpu
+ * should not run.
+ */
+bool vpci_check_pending(struct vcpu *v);
+
 struct vpci {
     /* List of vPCI handlers for a device. */
     struct list_head handlers;
     spinlock_t lock;
+
+#ifdef __XEN__
+    /* Hide the rest of the vpci struct from the user-space test harness. */
+    struct vpci_header {
+        /* Information about the PCI BARs of this device. */
+        struct vpci_bar {
+            paddr_t addr;
+            uint64_t size;
+            enum {
+                VPCI_BAR_EMPTY,
+                VPCI_BAR_IO,
+                VPCI_BAR_MEM32,
+                VPCI_BAR_MEM64_LO,
+                VPCI_BAR_MEM64_HI,
+                VPCI_BAR_ROM,
+            } type;
+            bool prefetchable;
+            /* Store whether the BAR is mapped into guest p2m. */
+            bool enabled;
+            /*
+             * Store whether the ROM enable bit is set (doesn't imply ROM BAR
Quoted 8 lines

xen/include/xen/vpci.h uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size); · line 71

Quoted 3 lines
xen/include/xen/vpci.h +3 -0
+             * is mapped into guest p2m). Only used for type VPCI_BAR_ROM.
+             */
+            bool rom_enabled;

Especially with the error handling issue in mind that I've mentioned earlier, I wonder whether this field shouldn't be dropped, along the lines of you also no longer caching the memory decode enable bit in the command register.

Quoted 4 lines

Removing rom_enabled would imply doing a register read in vpci_modify_bars in order to know whether the ROM BAR is enabled or not, which is not trivial because depending on the header type the position of the ROM BAR is different.

As said - I wouldn't mind the field if it was always in sync with the hardware one. And it was for a reason that I mentioned the memory decode bit, which you no longer cache. I think both should be treated the same.

Quoted 3 lines

Another option would be to store the prefetch/enable bits inside of the addr field, but that would also require more masking/unmasking of the fields when the values are used or updated.

I didn't ask for these two to be eliminated.

Jan

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

#33 lore raw

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

Quoted 26 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 11:20, <[email protected]> wrote:
Quoted 10 lines · Jan Beulich

xen/drivers/vpci/header.c line 202

Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/header.c new file +8 -0
+    }
+
+    rc = vpci_maybe_defer_map(pdev->domain, mem, map);
+    if ( !rc )
+        for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
+            if ( header->bars[i].type != VPCI_BAR_ROM ||
+                 header->bars[i].rom_enabled )
+            header->bars[i].enabled = map;
Quoted 3 lines

Hmm, you're updating state here regardless of possible failure in the deferred operation (see the discarded error code in vpci_check_pending()).

Yes, I've fixed the code above to try to map/unmap as much as possible, even when a failure happens.

Quoted 6 lines

I agree that enabling/disabling here with the operation being deferred is not ideal, but I also think we would end up doing the same regardless of the outcome of the deferred operation. If some mapping/unmapping of BARs failed, the memory decoding should be enabled anyway. I can add a comment along this lines if you think that's OK.

Quoted 5 lines

Yes, at least explaining why things are the (not fully correct) way they are would help (also to tell anyone wanting to improve this what it actually is that would need changing). Of course even better would be if maintained state would match the state hardware is in.

I see the current code in this version is slightly confusing regarding the usage of the 'enabled' field. I've fixed the code so that the 'enabled' field matches the following conditions:

- For non-ROM BARs: the 'enabled' field matches the value of the memory decoding bit, but it doesn't guarantee that the range is fully mapped/unmapped in the guest p2m.
- For ROM BARs: the 'enabled' bit matches the value of the memory decoding bit & the ROM enable bit, but again it doesn't guarantee that the memory is fully mapped/unmapped in the guest p2m.

Quoted 51 lines
Quoted 46 lines
Quoted 37 lines
Quoted 37 lines
xen/include/xen/vpci.h +27 -0
@@ -35,11 +35,52 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
 void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
                 uint32_t data);
 
+/*
+ * Check for pending vPCI operations on this vcpu. Returns true if the vcpu
+ * should not run.
+ */
+bool vpci_check_pending(struct vcpu *v);
+
 struct vpci {
     /* List of vPCI handlers for a device. */
     struct list_head handlers;
     spinlock_t lock;
+
+#ifdef __XEN__
+    /* Hide the rest of the vpci struct from the user-space test harness. */
+    struct vpci_header {
+        /* Information about the PCI BARs of this device. */
+        struct vpci_bar {
+            paddr_t addr;
+            uint64_t size;
+            enum {
+                VPCI_BAR_EMPTY,
+                VPCI_BAR_IO,
+                VPCI_BAR_MEM32,
+                VPCI_BAR_MEM64_LO,
+                VPCI_BAR_MEM64_HI,
+                VPCI_BAR_ROM,
+            } type;
+            bool prefetchable;
+            /* Store whether the BAR is mapped into guest p2m. */
+            bool enabled;
+            /*
+             * Store whether the ROM enable bit is set (doesn't imply ROM BAR
Quoted 8 lines

xen/include/xen/vpci.h uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size); · line 71

Quoted 3 lines
xen/include/xen/vpci.h +3 -0
+             * is mapped into guest p2m). Only used for type VPCI_BAR_ROM.
+             */
+            bool rom_enabled;

Especially with the error handling issue in mind that I've mentioned earlier, I wonder whether this field shouldn't be dropped, along the lines of you also no longer caching the memory decode enable bit in the command register.

Quoted 4 lines

Removing rom_enabled would imply doing a register read in vpci_modify_bars in order to know whether the ROM BAR is enabled or not, which is not trivial because depending on the header type the position of the ROM BAR is different.

Quoted 4 lines

As said - I wouldn't mind the field if it was always in sync with the hardware one. And it was for a reason that I mentioned the memory decode bit, which you no longer cache. I think both should be treated the same.

I think I'm missing something, rom_enabled matches exactly the state of the ROM enable bit. There's no way rom_enabled will get updated without the BAR ROM also being updated in vpci_rom_write.

In line with my comments above regarding the 'enabled' field, what about adding:

- rom_enabled matches the state of the ROM BAR enable bit. It doesn't take into account the state of the memory decoding bit. As such, it cannot be used to detect if the ROM BAR memory is active or not, the 'enabled' bit should be used in that case.

Roger.

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

#34 lore raw

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

Quoted 56 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 13:09, <[email protected]> wrote:
Quoted 54 lines · Jan Beulich
Quoted 53 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 11:20, <[email protected]> wrote:
Quoted 38 lines · Jan Beulich
Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/include/xen/vpci.h +27 -0
@@ -35,11 +35,52 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
 void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
                 uint32_t data);
 
+/*
+ * Check for pending vPCI operations on this vcpu. Returns true if the vcpu
+ * should not run.
+ */
+bool vpci_check_pending(struct vcpu *v);
+
 struct vpci {
     /* List of vPCI handlers for a device. */
     struct list_head handlers;
     spinlock_t lock;
+
+#ifdef __XEN__
+    /* Hide the rest of the vpci struct from the user-space test harness. */
+    struct vpci_header {
+        /* Information about the PCI BARs of this device. */
+        struct vpci_bar {
+            paddr_t addr;
+            uint64_t size;
+            enum {
+                VPCI_BAR_EMPTY,
+                VPCI_BAR_IO,
+                VPCI_BAR_MEM32,
+                VPCI_BAR_MEM64_LO,
+                VPCI_BAR_MEM64_HI,
+                VPCI_BAR_ROM,
+            } type;
+            bool prefetchable;
+            /* Store whether the BAR is mapped into guest p2m. */
+            bool enabled;
+            /*
+             * Store whether the ROM enable bit is set (doesn't imply ROM BAR
Quoted 8 lines

xen/include/xen/vpci.h uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size); · line 71

Quoted 3 lines
xen/include/xen/vpci.h +3 -0
+             * is mapped into guest p2m). Only used for type VPCI_BAR_ROM.
+             */
+            bool rom_enabled;

Especially with the error handling issue in mind that I've mentioned earlier, I wonder whether this field shouldn't be dropped, along the lines of you also no longer caching the memory decode enable bit in the command register.

Removing rom_enabled would imply doing a register read in vpci_modify_bars in order to know whether the ROM BAR is enabled or not, which is not trivial because depending on the header type the position of the ROM BAR is different.

Quoted 8 lines
Quoted 4 lines

As said - I wouldn't mind the field if it was always in sync with the hardware one. And it was for a reason that I mentioned the memory decode bit, which you no longer cache. I think both should be treated the same.

I think I'm missing something, rom_enabled matches exactly the state of the ROM enable bit. There's no way rom_enabled will get updated without the BAR ROM also being updated in vpci_rom_write.

Oh, I'm sorry for not being precise here: I think the hardware bit should only be set once the mapping is complete. That's not how the code currently behaves, so yes, right now the cached bit apparently properly reflects the actual one. With the possibly deferred mapping, that wouldn't be the case.

Jan

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

#35 lore raw

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

Quoted 65 lines · Jan Beulich
Quoted 56 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 13:09, <[email protected]> wrote:
Quoted 54 lines · Jan Beulich
Quoted 53 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 11:20, <[email protected]> wrote:
Quoted 38 lines · Jan Beulich
Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/include/xen/vpci.h +27 -0
@@ -35,11 +35,52 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);
 void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
                 uint32_t data);
 
+/*
+ * Check for pending vPCI operations on this vcpu. Returns true if the vcpu
+ * should not run.
+ */
+bool vpci_check_pending(struct vcpu *v);
+
 struct vpci {
     /* List of vPCI handlers for a device. */
     struct list_head handlers;
     spinlock_t lock;
+
+#ifdef __XEN__
+    /* Hide the rest of the vpci struct from the user-space test harness. */
+    struct vpci_header {
+        /* Information about the PCI BARs of this device. */
+        struct vpci_bar {
+            paddr_t addr;
+            uint64_t size;
+            enum {
+                VPCI_BAR_EMPTY,
+                VPCI_BAR_IO,
+                VPCI_BAR_MEM32,
+                VPCI_BAR_MEM64_LO,
+                VPCI_BAR_MEM64_HI,
+                VPCI_BAR_ROM,
+            } type;
+            bool prefetchable;
+            /* Store whether the BAR is mapped into guest p2m. */
+            bool enabled;
+            /*
+             * Store whether the ROM enable bit is set (doesn't imply ROM BAR
Quoted 8 lines

xen/include/xen/vpci.h uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size); · line 71

Quoted 3 lines
xen/include/xen/vpci.h +3 -0
+             * is mapped into guest p2m). Only used for type VPCI_BAR_ROM.
+             */
+            bool rom_enabled;

Especially with the error handling issue in mind that I've mentioned earlier, I wonder whether this field shouldn't be dropped, along the lines of you also no longer caching the memory decode enable bit in the command register.

Removing rom_enabled would imply doing a register read in vpci_modify_bars in order to know whether the ROM BAR is enabled or not, which is not trivial because depending on the header type the position of the ROM BAR is different.

Quoted 8 lines
Quoted 4 lines

As said - I wouldn't mind the field if it was always in sync with the hardware one. And it was for a reason that I mentioned the memory decode bit, which you no longer cache. I think both should be treated the same.

I think I'm missing something, rom_enabled matches exactly the state of the ROM enable bit. There's no way rom_enabled will get updated without the BAR ROM also being updated in vpci_rom_write.

Quoted 5 lines

Oh, I'm sorry for not being precise here: I think the hardware bit should only be set once the mapping is complete. That's not how the code currently behaves, so yes, right now the cached bit apparently properly reflects the actual one. With the possibly deferred mapping, that wouldn't be the case.

I could add some tail code to vpci_process_pending that sets the memory decoding or ROM BAR enable bit together with the rom_enable and enabled fields in the header struct. Would you agree to this?

Thanks, Roger.

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

#36 lore raw

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

Quoted 34 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 14:02, <[email protected]> wrote:
Quoted 26 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 13:09, <[email protected]> wrote:
Quoted 15 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · On 05.10.17 at 11:20, <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:

+ bool rom_enabled;

Especially with the error handling issue in mind that I've mentioned earlier, I wonder whether this field shouldn't be dropped, along the lines of you also no longer caching the memory decode enable bit in the command register.

Removing rom_enabled would imply doing a register read in vpci_modify_bars in order to know whether the ROM BAR is enabled or not, which is not trivial because depending on the header type the position of the ROM BAR is different.

Quoted 8 lines
Quoted 4 lines

As said - I wouldn't mind the field if it was always in sync with the hardware one. And it was for a reason that I mentioned the memory decode bit, which you no longer cache. I think both should be treated the same.

I think I'm missing something, rom_enabled matches exactly the state of the ROM enable bit. There's no way rom_enabled will get updated without the BAR ROM also being updated in vpci_rom_write.

Quoted 5 lines

Oh, I'm sorry for not being precise here: I think the hardware bit should only be set once the mapping is complete. That's not how the code currently behaves, so yes, right now the cached bit apparently properly reflects the actual one. With the possibly deferred mapping, that wouldn't be the case.

Quoted 3 lines

I could add some tail code to vpci_process_pending that sets the memory decoding or ROM BAR enable bit together with the rom_enable and enabled fields in the header struct. Would you agree to this?

If that's cleanly doable, sure. I had assumed you didn't do it because you couldn't reasonably update state at that later point.

Jan

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

#37 lore raw

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

In reply to Roger Pau Monne

Hi Roger,

xen/drivers/vpci/header.c line 47

Quoted 12 lines · Roger Pau Monne
xen/drivers/vpci/header.c new file +12 -0
+static int vpci_map_range(unsigned long s, unsigned long e, void *data,
+                          unsigned long *c)
+{
+    const struct map_data *map = data;
+    int rc;
+
+    for ( ; ; )
+    {
+        unsigned long size = e - s + 1;
+
+        rc = (map->map ? map_mmio_regions : unmap_mmio_regions)
+             (map->d, _gfn(s), size, _mfn(s));

Again, this is relying on *map_mmio_regions to support preemption. This is not the case on ARM.

I am not asking to add preemption in the ARM code. But we should at least add a check similar to XEN_DOMCTL_memory_mapping ( if (size > 64) ) to remind us that *map_mmio_regions have to be fixed.

Similarly, on IRC said you will add a TODO regarding the lack of passing the type of the BAR.

Cheers,

Signature · 6 lines
Julien Grall

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

Re: [PATCH v6 08/11] vpci/bars: add handlers to map the BARs

Quoted 19 lines · Julien Grall

Hi Roger,

xen/drivers/vpci/header.c line 47

Quoted 12 lines · Roger Pau Monne
xen/drivers/vpci/header.c new file +12 -0
+static int vpci_map_range(unsigned long s, unsigned long e, void *data,
+                          unsigned long *c)
+{
+    const struct map_data *map = data;
+    int rc;
+
+    for ( ; ; )
+    {
+        unsigned long size = e - s + 1;
+
+        rc = (map->map ? map_mmio_regions : unmap_mmio_regions)
+             (map->d, _gfn(s), size, _mfn(s));

Again, this is relying on *map_mmio_regions to support preemption. This is not the case on ARM.

Quoted 3 lines

I am not asking to add preemption in the ARM code. But we should at least add a check similar to XEN_DOMCTL_memory_mapping ( if (size > 64) ) to remind us that *map_mmio_regions have to be fixed.

I've added a bodge for ARM in order to limit the mappings to 64 for each call to {un}map_mmio_regions.

Quoted 3 lines

Similarly, on IRC said you will add a TODO regarding the lack of passing the type of the BAR.

Sorry, not sure if we spoke about this before or after sending this series, but in any case I've added it now.

Thanks, Roger.

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

#39 lore raw

PATCH v6 9/11 vpci/msi: add MSI handlers

In reply to Roger Pau Monne

Add handlers for the MSI control, address, data and mask fields in order to detect accesses to them and setup the interrupts as requested by the guest.

Note that the pending register is not trapped, and the guest can freely read/write to it.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Paul Durrant <[email protected]>
---
Changes since v5:
- Update to new lock usage.
- Change handlers to match the new type.
- s/msi_flags/msi_gflags/, remove the local variables and use the new DOMCTL_VMSI_* defines.
- Change the MSI arch function to take a vpci_msi instead of a vpci_arch_msi as parameter.
- Fix the calculation of the guest vector for MSI injection to take into account the number of bits that can be modified.
- Use INVALID_PIRQ everywhere.
- Simplify exit path of vpci_msi_disable.
- Remove the conditional when setting address64 and masking fields.
- Add a process_pending_softirqs to the MSI dump loop.
- Place the prototypes for the MSI arch-specific functions in xen/vpci.h.
- Add parentheses around the INVALID_PIRQ definition.

Changes since v4:
- Fix commit message.
- Change the ASSERTs in vpci_msi_arch_mask into ifs.
- Introduce INVALID_PIRQ.
- Destroy the partially created bindings in case of failure in vpci_msi_arch_enable.
- Just take the pcidevs lock once in vpci_msi_arch_disable.
- Print an error message in case of failure of pt_irq_destroy_bind.
- Make vpci_msi_arch_init return void.
- Constify the arch parameter of vpci_msi_arch_print.
- Use fixed instead of cpu for msi redirection.
- Separate the header includes in vpci/msi.c between xen and asm.
- Store the number of configured vectors even if MSI is not enabled and always return it in vpci_msi_control_read.
- Fix/add comments in vpci_msi_control_write to clarify intended behavior.
- Simplify usage of masks in vpci_msi_address_{upper_}write.
- Add comment to vpci_msi_mask_{read/write}.
- Don't use MASK_EXTR in vpci_msi_mask_write.
- s/msi_offset/pos/ in vpci_init_msi.
- Move control variable setup closer to it's usage.
- Use d%d in vpci_dump_msi.
- Fix printing of bitfield mask in vpci_dump_msi.
- Fix definition of MSI_ADDR_REDIRECTION_MASK.
- Shuffle the layout of vpci_msi to minimize gaps.
- Remove the error label in vpci_init_msi.

Changes since v3:
- Propagate changes from previous versions: drop xen_ prefix, drop return value from handlers, use the new vpci_val fields.
- Use MASK_EXTR.
- Remove the usage of GENMASK.
- Add GFLAGS_SHIFT_DEST_ID and use it in msi_flags.
- Add "arch" to the MSI arch specific functions.
- Move the dumping of vPCI MSI information to dump_msi (key 'M').
- Remove the guest_vectors field.
- Allow the guest to change the number of active vectors without having to disable and enable MSI.
- Check the number of active vectors when parsing the disable mask.
- Remove the debug messages from vpci_init_msi.
- Move the arch-specific part of the dump handler to x86/hvm/vmsi.c.
- Use trylock in the dump handler to get the vpci lock.

Changes since v2:
- Add an arch-specific abstraction layer. Note that this is only implemented for x86 currently.
- Add a wrapper to detect MSI enabling for vPCI.

NB: I've only been able to test this with devices using a single MSI interrupt and no mask register. I will try to find hardware that supports the mask register and more than one vector, but I cannot make any promises.

If there are doubts about the untested parts we could always force Xen to report no per-vector masking support and only 1 available vector, but I would rather avoid doing it.

---
 xen/arch/x86/hvm/vmsi.c      | 153 ++++++++++++++++++
 xen/arch/x86/msi.c           |   3 +
 xen/drivers/vpci/Makefile    |   2 +-
 xen/drivers/vpci/msi.c       | 366 +++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/io.h |   5 +
 xen/include/asm-x86/msi.h    |   1 +
 xen/include/xen/irq.h        |   1 +
 xen/include/xen/vpci.h       |  35 +++++
 8 files changed, 565 insertions(+), 1 deletion(-)
 create mode 100644 xen/drivers/vpci/msi.c
xen/arch/x86/hvm/vmsi.c +153 -0
index 9b35e9b696..3dcde3d882 100644
@@ -31,6 +31,7 @@
 #include <xen/errno.h>
 #include <xen/sched.h>
 #include <xen/irq.h>
+#include <xen/vpci.h>
 #include <public/hvm/ioreq.h>
 #include <asm/hvm/io.h>
 #include <asm/hvm/vpic.h>
@@ -621,3 +622,155 @@ void msix_write_completion(struct vcpu *v)
     if ( msixtbl_write(v, ctrl_address, 4, 0) != X86EMUL_OKAY )
         gdprintk(XENLOG_WARNING, "MSI-X write completion failure\n");
 }
+
+static unsigned int msi_gflags(uint16_t data, uint64_t addr)
+{
+    /*
+     * We need to use the DOMCTL constants here because the output of this
+     * function is used as input to pt_irq_create_bind, which also takes the
+     * input from the DOMCTL itself.
+     */
+    return MASK_INSR(MASK_EXTR(addr, MSI_ADDR_DEST_ID_MASK),
+                     XEN_DOMCTL_VMSI_X86_DEST_ID_MASK) |
+           MASK_INSR(MASK_EXTR(addr, MSI_ADDR_REDIRECTION_MASK),
+                     XEN_DOMCTL_VMSI_X86_RH_MASK) |
+           MASK_INSR(MASK_EXTR(addr, MSI_ADDR_DESTMODE_MASK),
+                     XEN_DOMCTL_VMSI_X86_DM_MASK) |
+           MASK_INSR(MASK_EXTR(data, MSI_DATA_DELIVERY_MODE_MASK),
+                     XEN_DOMCTL_VMSI_X86_DELIV_MASK) |
+           MASK_INSR(MASK_EXTR(data, MSI_DATA_TRIGGER_MASK),
+                     XEN_DOMCTL_VMSI_X86_TRIG_MASK);
+}
+
+void vpci_msi_arch_mask(struct vpci_msi *msi, const struct pci_dev *pdev,
+                        unsigned int entry, bool mask)
+{
+    const struct pirq *pinfo;
+    struct irq_desc *desc;
+    unsigned long flags;
+    int irq;
+
+    ASSERT(msi->arch.pirq >= 0 && entry < msi->vectors);
+    pinfo = pirq_info(pdev->domain, msi->arch.pirq + entry);
+    if ( !pinfo )
+        return;
+
+    irq = pinfo->arch.irq;
+    if ( irq >= nr_irqs || irq < 0)
+        return;
+
+    desc = irq_to_desc(irq);
+    if ( !desc )
+        return;
+
+    spin_lock_irqsave(&desc->lock, flags);
+    guest_mask_msi_irq(desc, mask);
+    spin_unlock_irqrestore(&desc->lock, flags);
+}
+
+int vpci_msi_arch_enable(struct vpci_msi *msi, const struct pci_dev *pdev,
+                         unsigned int vectors)
+{
+    struct msi_info msi_info = {
+        .seg = pdev->seg,
+        .bus = pdev->bus,
+        .devfn = pdev->devfn,
+        .entry_nr = vectors,
+    };
+    unsigned int i;
+    int rc;
+
+    ASSERT(msi->arch.pirq == INVALID_PIRQ);
+
+    /* Get a PIRQ. */
+    rc = allocate_and_map_msi_pirq(pdev->domain, -1, &msi->arch.pirq,
+                                   MAP_PIRQ_TYPE_MULTI_MSI, &msi_info);
+    if ( rc )
+    {
+        gdprintk(XENLOG_ERR, "%04x:%02x:%02x.%u: failed to map PIRQ: %d\n",
+                 pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                 PCI_FUNC(pdev->devfn), rc);
+        return rc;
+    }
+
+    for ( i = 0; i < vectors; i++ )
+    {
+        uint8_t vector = MASK_EXTR(msi->data, MSI_DATA_VECTOR_MASK);
+        uint8_t vector_mask = 0xff >> (8 - fls(msi->vectors) + 1);
+        xen_domctl_bind_pt_irq_t bind = {
+            .machine_irq = msi->arch.pirq + i,
+            .irq_type = PT_IRQ_TYPE_MSI,
+            .u.msi.gvec = (vector & ~vector_mask) |
+                          ((vector + i) & vector_mask),
+            .u.msi.gflags = msi_gflags(msi->data, msi->address),
+        };
+
+        pcidevs_lock();
+        rc = pt_irq_create_bind(pdev->domain, &bind);
+        if ( rc )
+        {
+            gdprintk(XENLOG_ERR,
+                     "%04x:%02x:%02x.%u: failed to bind PIRQ %u: %d\n",
+                     pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                     PCI_FUNC(pdev->devfn), msi->arch.pirq + i, rc);
+            while ( bind.machine_irq-- )
+                pt_irq_destroy_bind(pdev->domain, &bind);
+            spin_lock(&pdev->domain->event_lock);
+            unmap_domain_pirq(pdev->domain, msi->arch.pirq);
+            spin_unlock(&pdev->domain->event_lock);
+            pcidevs_unlock();
+            msi->arch.pirq = INVALID_PIRQ;
+            return rc;
+        }
+        pcidevs_unlock();
+    }
+
+    return 0;
+}
+
+int vpci_msi_arch_disable(struct vpci_msi *msi, const struct pci_dev *pdev)
+{
+    unsigned int i;
+
+    ASSERT(msi->arch.pirq != INVALID_PIRQ);
+
+    pcidevs_lock();
+    for ( i = 0; i < msi->vectors; i++ )
+    {
+        xen_domctl_bind_pt_irq_t bind = {
+            .machine_irq = msi->arch.pirq + i,
+            .irq_type = PT_IRQ_TYPE_MSI,
+        };
+        int rc;
+
+        rc = pt_irq_destroy_bind(pdev->domain, &bind);
+        ASSERT(!rc);
+    }
+
+    spin_lock(&pdev->domain->event_lock);
+    unmap_domain_pirq(pdev->domain, msi->arch.pirq);
+    spin_unlock(&pdev->domain->event_lock);
+    pcidevs_unlock();
+
+    msi->arch.pirq = INVALID_PIRQ;
+
+    return 0;
+}
+
+void vpci_msi_arch_init(struct vpci_msi *msi)
+{
+    msi->arch.pirq = INVALID_PIRQ;
+}
+
+void vpci_msi_arch_print(const struct vpci_msi *msi)
+{
+    printk("vec=%#02x%7s%6s%3sassert%5s%7s dest_id=%lu pirq: %d\n",
+           MASK_EXTR(msi->data, MSI_DATA_VECTOR_MASK),
+           msi->data & MSI_DATA_DELIVERY_LOWPRI ? "lowest" : "fixed",
+           msi->data & MSI_DATA_TRIGGER_LEVEL ? "level" : "edge",
+           msi->data & MSI_DATA_LEVEL_ASSERT ? "" : "de",
+           msi->address & MSI_ADDR_DESTMODE_LOGIC ? "log" : "phys",
+           msi->address & MSI_ADDR_REDIRECTION_LOWPRI ? "lowest" : "fixed",
+           MASK_EXTR(msi->address, MSI_ADDR_DEST_ID_MASK),
+           msi->arch.pirq);
+}
xen/arch/x86/msi.c +3 -0
index 77998f4fb3..63769153f1 100644
@@ -30,6 +30,7 @@
 #include <public/physdev.h>
 #include <xen/iommu.h>
 #include <xsm/xsm.h>
+#include <xen/vpci.h>
 
 static s8 __read_mostly use_msi = -1;
 boolean_param("msi", use_msi);
@@ -1536,6 +1537,8 @@ static void dump_msi(unsigned char key)
                attr.guest_masked ? 'G' : ' ',
                mask);
     }
+
+    vpci_dump_msi();
 }
 
 static int __init msi_setup_keyhandler(void)
xen/drivers/vpci/Makefile +1 -1
index 241467212f..62cec9e82b 100644
@@ -1 +1 @@
-obj-y += vpci.o header.o
+obj-y += vpci.o header.o msi.o
xen/drivers/vpci/msi.c new file +366 -0

366 lines not on this page · raw message

xen/include/asm-x86/hvm/io.h +5 -0
index ba66e22e04..c47cc971d3 100644
@@ -127,6 +127,11 @@ void hvm_dpci_eoi(struct domain *d, unsigned int guest_irq,
 void msix_write_completion(struct vcpu *);
 void msixtbl_init(struct domain *d);
 
+/* Arch-specific MSI data for vPCI. */
+struct vpci_arch_msi {
+    int pirq;
+};
+
 enum stdvga_cache_state {
     STDVGA_CACHE_UNINITIALIZED,
     STDVGA_CACHE_ENABLED,
xen/include/asm-x86/msi.h +1 -0
index 37d37b820e..43ab5c6bc6 100644
@@ -48,6 +48,7 @@
 #define MSI_ADDR_REDIRECTION_SHIFT  3
 #define MSI_ADDR_REDIRECTION_CPU    (0 << MSI_ADDR_REDIRECTION_SHIFT)
 #define MSI_ADDR_REDIRECTION_LOWPRI (1 << MSI_ADDR_REDIRECTION_SHIFT)
+#define MSI_ADDR_REDIRECTION_MASK   (1 << MSI_ADDR_REDIRECTION_SHIFT)
 
 #define MSI_ADDR_DEST_ID_SHIFT		12
 #define	 MSI_ADDR_DEST_ID_MASK		0x00ff000
xen/include/xen/irq.h +1 -0
index 0aa817e266..586b78393a 100644
@@ -133,6 +133,7 @@ struct pirq {
     struct arch_pirq arch;
 };
 
+#define INVALID_PIRQ (-1)
 #define pirq_info(d, p) ((struct pirq *)radix_tree_lookup(&(d)->pirq_tree, p))
 
 /* Use this instead of pirq_info() if the structure may need allocating. */
xen/include/xen/vpci.h +35 -0
index 4e0b67c2f1..5b582b8012 100644
@@ -72,6 +72,30 @@ struct vpci {
         } bars[7]; /* At most 6 BARS + 1 expansion ROM BAR. */
         /* FIXME: currently there's no support for SR-IOV. */
     } header;
+
+    /* MSI data. */
+    struct vpci_msi {
+        /* Arch-specific data. */
+        struct vpci_arch_msi arch;
+        /* Address. */
+        uint64_t address;
+        /* Offset of the capability in the config space. */
+        unsigned int pos;
+        /* Maximum number of vectors supported by the device. */
+        unsigned int max_vectors;
+        /* Number of vectors configured. */
+        unsigned int vectors;
+        /* Mask bitfield. */
+        uint32_t mask;
+        /* Data. */
+        uint16_t data;
+        /* Enabled? */
+        bool enabled;
+        /* Supports per-vector masking? */
+        bool masking;
+        /* 64-bit address capable? */
+        bool address64;
+    } *msi;
 #endif
 };
 
@@ -80,6 +104,17 @@ struct vpci_vcpu {
     struct rangeset *mem;
     bool map;
 };
+
+void vpci_dump_msi(void);
+
+/* Arch-specific vPCI MSI helpers. */
+void vpci_msi_arch_mask(struct vpci_msi *msi, const struct pci_dev *pdev,
+                        unsigned int entry, bool mask);
+int vpci_msi_arch_enable(struct vpci_msi *msi, const struct pci_dev *pdev,
+                         unsigned int vectors);
+int vpci_msi_arch_disable(struct vpci_msi *msi, const struct pci_dev *pdev);
+void vpci_msi_arch_init(struct vpci_msi *msi);
+void vpci_msi_arch_print(const struct vpci_msi *msi);
 #endif
 
 #endif
Signature · 7 lines
2.11.0 (Apple Git-81)


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

Re: [PATCH v6 09/11] vpci/msi: add MSI handlers

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:

Add handlers for the MSI control, address, data and mask fields in order to detect accesses to them and setup the interrupts as requested by the guest.

Note that the pending register is not trapped, and the guest can freely read/write to it.

I wonder how valid this can be with ...

Quoted 8 lines

Changes since v5:
- Update to new lock usage.
- Change handlers to match the new type.
- s/msi_flags/msi_gflags/, remove the local variables and use the new DOMCTL_VMSI_* defines.
- Change the MSI arch function to take a vpci_msi instead of a vpci_arch_msi as parameter.
- Fix the calculation of the guest vector for MSI injection to take

Quoted 8 lines

into account the number of bits that can be modified.
- Use INVALID_PIRQ everywhere.
- Simplify exit path of vpci_msi_disable.
- Remove the conditional when setting address64 and masking fields.
- Add a process_pending_softirqs to the MSI dump loop.
- Place the prototypes for the MSI arch-specific functions in xen/vpci.h.
- Add parentheses around the INVALID_PIRQ definition.

... this set of changes.

Quoted 5 lines
xen/arch/x86/hvm/vmsi.c +5 -0
+void vpci_msi_arch_mask(struct vpci_msi *msi, const struct pci_dev *pdev,
+                        unsigned int entry, bool mask)
+{
+    const struct pirq *pinfo;
+    struct irq_desc *desc;

xen/arch/x86/hvm/vmsi.c void msix_write_completion(struct vcpu *v) · line 659

Quoted 10 lines
xen/arch/x86/hvm/vmsi.c +10 -0
+    unsigned long flags;
+    int irq;
+
+    ASSERT(msi->arch.pirq >= 0 && entry < msi->vectors);
+    pinfo = pirq_info(pdev->domain, msi->arch.pirq + entry);
+    if ( !pinfo )
+        return;
+
+    irq = pinfo->arch.irq;
+    if ( irq >= nr_irqs || irq < 0)

Style. However, ...

xen/arch/x86/hvm/vmsi.c void msix_write_completion(struct vcpu *v) · line 666

Quoted 7 lines
xen/arch/x86/hvm/vmsi.c +7 -0
+        return;
+
+    desc = irq_to_desc(irq);
+    if ( !desc )
+        return;
+
+    spin_lock_irqsave(&desc->lock, flags);

... didn't I comment on this already suggesting to use domain_spin_lock_irq_desc() instead of open coding it?

Quoted 15 lines
xen/drivers/vpci/msi.c new file +15 -0
+static void vpci_msi_enable(const struct pci_dev *pdev, struct vpci_msi *msi,
+                            unsigned int vectors)
+{
+    int ret;
+
+    ASSERT(!msi->enabled);
+    ret = vpci_msi_arch_enable(msi, pdev, vectors);
+    if ( ret )
+        return;
+
+    /* Apply the mask bits. */
+    if ( msi->masking )
+    {
+        unsigned int i;
+        uint32_t mask = msi->mask;

xen/drivers/vpci/msi.c line 66

Quoted 10 lines
xen/drivers/vpci/msi.c new file +10 -0
+
+        for ( i = ffs(mask) - 1; mask && i < vectors; i = ffs(mask) - 1 )
+        {
+            vpci_msi_arch_mask(msi, pdev, i, true);
+            __clear_bit(i, &mask);
+        }
+    }
+
+    __msi_set_enable(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                     PCI_FUNC(pdev->devfn), msi->pos, 1);

This is very unlikely to be a function that arch-independent code is permitted to call.

xen/drivers/vpci/msi.c line 314

Quoted 3 lines
xen/drivers/vpci/msi.c new file +3 -0
+void vpci_dump_msi(void)
+{
+    struct domain *d;

const?

Quoted 1 line

+ for_each_domain ( d )

You need to rcu_read_lock(&domlist_read_lock) in order to validly use this construct.

Quoted 16 lines
xen/drivers/vpci/msi.c new file +16 -0
+    {
+        const struct pci_dev *pdev;
+
+        if ( !has_vpci(d) )
+            continue;
+
+        printk("vPCI MSI information for d%d\n", d->domain_id);
+
+        list_for_each_entry ( pdev, &d->arch.pdev_list, domain_list )
+        {
+            uint8_t seg = pdev->seg, bus = pdev->bus;
+            uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+            const struct vpci_msi *msi = pdev->vpci->msi;
+
+            if ( !spin_trylock(&pdev->vpci->lock) )
+            {

xen/drivers/vpci/msi.c line 342

Quoted 10 lines
xen/drivers/vpci/msi.c new file +10 -0
+                printk("Unable to get vPCI lock, skipping\n");
+                continue;
+            }
+
+            if ( msi )
+            {
+                printk("Device %04x:%02x:%02x.%u\n", seg, bus, slot, func);
+
+                printk("  Enabled: %u Supports masking: %u 64-bit addresses: %u\n",
+                       msi->enabled, msi->masking, msi->address64);

bool wants to be printed with %d, I think.

xen/drivers/vpci/msi.c line 349

Quoted 7 lines
xen/drivers/vpci/msi.c new file +7 -0
+                printk("  Max vectors: %u enabled vectors: %u\n",
+                       msi->max_vectors, msi->vectors);
+
+                vpci_msi_arch_print(msi);
+
+                if ( msi->masking )
+                    printk("  mask=%08x\n", msi->mask);

Is this really worth a separate line? Also please don't separately print ->making as its value will be known from the presence of the field here.

Overall please try to shorten messages such that they're still meaningful but don't cause unnecesary load on the serial line or extra wasted space in the ring buffer.

Quoted 22 lines
xen/include/xen/vpci.h +16 -0
@@ -72,6 +72,30 @@ struct vpci {
         } bars[7]; /* At most 6 BARS + 1 expansion ROM BAR. */
         /* FIXME: currently there's no support for SR-IOV. */
     } header;
+
+    /* MSI data. */
+    struct vpci_msi {
+        /* Arch-specific data. */
+        struct vpci_arch_msi arch;
+        /* Address. */
+        uint64_t address;
+        /* Offset of the capability in the config space. */
+        unsigned int pos;
+        /* Maximum number of vectors supported by the device. */
+        unsigned int max_vectors;
+        /* Number of vectors configured. */
+        unsigned int vectors;
+        /* Mask bitfield. */
+        uint32_t mask;
+        /* Data. */

xen/include/xen/vpci.h struct vpci { · line 100

Quoted 10 lines
xen/include/xen/vpci.h +8 -0
+        uint16_t data;
+        /* Enabled? */
+        bool enabled;
+        /* Supports per-vector masking? */
+        bool masking;
+        /* 64-bit address capable? */
+        bool address64;
+    } *msi;
 #endif
 };

As there may be quite a few instance of this structure, please strive to keep its size down. Many of the fields above have a pretty limited valid value range and hence would benefit from using more narrow types and/or bitfields.

Jan

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

#41 lore raw

Re: [PATCH v6 09/11] vpci/msi: add MSI handlers

Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/msi.c new file +20 -0
+static void vpci_msi_enable(const struct pci_dev *pdev, struct vpci_msi *msi,
+                            unsigned int vectors)
+{
+    int ret;
+
+    ASSERT(!msi->enabled);
+    ret = vpci_msi_arch_enable(msi, pdev, vectors);
+    if ( ret )
+        return;
+
+    /* Apply the mask bits. */
+    if ( msi->masking )
+    {
+        unsigned int i;
+        uint32_t mask = msi->mask;
+
+        for ( i = ffs(mask) - 1; mask && i < vectors; i = ffs(mask) - 1 )
+        {
+            vpci_msi_arch_mask(msi, pdev, i, true);
+            __clear_bit(i, &mask);
Quoted 8 lines

xen/drivers/vpci/msi.c line 66

Quoted 5 lines
xen/drivers/vpci/msi.c new file +5 -0
+        }
+    }
+
+    __msi_set_enable(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                     PCI_FUNC(pdev->devfn), msi->pos, 1);

This is very unlikely to be a function that arch-independent code is permitted to call.

Right, I could remove the '__' prefix, or introduce a vpci_msi_arch_dev_enable helper that calls this function.

Quoted 29 lines
Quoted 29 lines
xen/include/xen/vpci.h +23 -0
@@ -72,6 +72,30 @@ struct vpci {
         } bars[7]; /* At most 6 BARS + 1 expansion ROM BAR. */
         /* FIXME: currently there's no support for SR-IOV. */
     } header;
+
+    /* MSI data. */
+    struct vpci_msi {
+        /* Arch-specific data. */
+        struct vpci_arch_msi arch;
+        /* Address. */
+        uint64_t address;
+        /* Offset of the capability in the config space. */
+        unsigned int pos;
+        /* Maximum number of vectors supported by the device. */
+        unsigned int max_vectors;
+        /* Number of vectors configured. */
+        unsigned int vectors;
+        /* Mask bitfield. */
+        uint32_t mask;
+        /* Data. */
+        uint16_t data;
+        /* Enabled? */
+        bool enabled;
+        /* Supports per-vector masking? */
+        bool masking;
+        /* 64-bit address capable? */
+        bool address64;
Quoted 8 lines

xen/include/xen/vpci.h struct vpci { · line 100

Quoted 3 lines
xen/include/xen/vpci.h +1 -0
+    } *msi;
 #endif
 };

As there may be quite a few instance of this structure, please strive to keep its size down. Many of the fields above have a pretty limited valid value range and hence would benefit from using more narrow types and/or bitfields.

max_vectors/vectors can be uint8_t, the rest I'm not sure how to reduce. I could turn the bools into a bitfield, but isn't a bool already limited to 1 bit?

Thanks, Roger.

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

#42 lore raw

Re: [PATCH v6 09/11] vpci/msi: add MSI handlers

Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 10.10.17 at 13:35, <[email protected]> wrote:
Quoted 30 lines · Jan Beulich
Quoted 30 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/include/xen/vpci.h +23 -0
@@ -72,6 +72,30 @@ struct vpci {
         } bars[7]; /* At most 6 BARS + 1 expansion ROM BAR. */
         /* FIXME: currently there's no support for SR-IOV. */
     } header;
+
+    /* MSI data. */
+    struct vpci_msi {
+        /* Arch-specific data. */
+        struct vpci_arch_msi arch;
+        /* Address. */
+        uint64_t address;
+        /* Offset of the capability in the config space. */
+        unsigned int pos;
+        /* Maximum number of vectors supported by the device. */
+        unsigned int max_vectors;
+        /* Number of vectors configured. */
+        unsigned int vectors;
+        /* Mask bitfield. */
+        uint32_t mask;
+        /* Data. */
+        uint16_t data;
+        /* Enabled? */
+        bool enabled;
+        /* Supports per-vector masking? */
+        bool masking;
+        /* 64-bit address capable? */
+        bool address64;
Quoted 8 lines

xen/include/xen/vpci.h struct vpci { · line 100

Quoted 3 lines
xen/include/xen/vpci.h +1 -0
+    } *msi;
 #endif
 };

As there may be quite a few instance of this structure, please strive to keep its size down. Many of the fields above have a pretty limited valid value range and hence would benefit from using more narrow types and/or bitfields.

Quoted 2 lines

max_vectors/vectors can be uint8_t, the rest I'm not sure how to reduce.

"pos" can be uint8_t too afaict.

Quoted 2 lines

I could turn the bools into a bitfield, but isn't a bool already limited to 1 bit?

Plus 7 bits of unused data. The minimum addressable unit is a byte after all, and non-bitfield members need to allow their address being taken.

Jan

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

#43 lore raw

Re: [PATCH v6 09/11] vpci/msi: add MSI handlers

In reply to Roger Pau Monné
Quoted 31 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/msi.c new file +20 -0
+static void vpci_msi_enable(const struct pci_dev *pdev, struct vpci_msi *msi,
+                            unsigned int vectors)
+{
+    int ret;
+
+    ASSERT(!msi->enabled);
+    ret = vpci_msi_arch_enable(msi, pdev, vectors);
+    if ( ret )
+        return;
+
+    /* Apply the mask bits. */
+    if ( msi->masking )
+    {
+        unsigned int i;
+        uint32_t mask = msi->mask;
+
+        for ( i = ffs(mask) - 1; mask && i < vectors; i = ffs(mask) - 1 )
+        {
+            vpci_msi_arch_mask(msi, pdev, i, true);
+            __clear_bit(i, &mask);
Quoted 8 lines

xen/drivers/vpci/msi.c line 66

Quoted 5 lines
xen/drivers/vpci/msi.c new file +5 -0
+        }
+    }
+
+    __msi_set_enable(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                     PCI_FUNC(pdev->devfn), msi->pos, 1);

This is very unlikely to be a function that arch-independent code is permitted to call.

Quoted 2 lines

Right, I could remove the '__' prefix, or introduce a vpci_msi_arch_dev_enable helper that calls this function.

So would using msi_set_enable instead be acceptable?

Thanks, Roger.

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

#44 lore raw

Re: [PATCH v6 09/11] vpci/msi: add MSI handlers

Quoted 33 lines
Quoted 1 line
Quoted 1 line · On 13.10.17 at 13:17, <[email protected]> wrote:
Quoted 31 lines · Roger Pau Monné
Quoted 29 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/msi.c new file +15 -0
+static void vpci_msi_enable(const struct pci_dev *pdev, struct vpci_msi *msi,
+                            unsigned int vectors)
+{
+    int ret;
+
+    ASSERT(!msi->enabled);
+    ret = vpci_msi_arch_enable(msi, pdev, vectors);
+    if ( ret )
+        return;
+
+    /* Apply the mask bits. */
+    if ( msi->masking )
+    {
+        unsigned int i;
+        uint32_t mask = msi->mask;

xen/drivers/vpci/msi.c line 66

Quoted 10 lines
xen/drivers/vpci/msi.c new file +10 -0
+
+        for ( i = ffs(mask) - 1; mask && i < vectors; i = ffs(mask) - 1 )
+        {
+            vpci_msi_arch_mask(msi, pdev, i, true);
+            __clear_bit(i, &mask);
+        }
+    }
+
+    __msi_set_enable(pdev->seg, pdev->bus, PCI_SLOT(pdev->devfn),
+                     PCI_FUNC(pdev->devfn), msi->pos, 1);

This is very unlikely to be a function that arch-independent code is permitted to call.

Quoted 4 lines
Quoted 2 lines

Right, I could remove the '__' prefix, or introduce a vpci_msi_arch_dev_enable helper that calls this function.

So would using msi_set_enable instead be acceptable?

Not really, no, the more that it's static (and should remain so); __msi_set_enable() not being static is also just because of an AMD IOMMU oddity. These are low level functions that higher layers aren't supposed to call directly.

Jan

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

#45 lore raw

PATCH v6 10/11 vpci: add a priority parameter to the vPCI register initializer

In reply to Roger Pau Monne

This is needed for MSI-X, since MSI-X will need to be initialized before parsing the BARs, so that the header BAR handlers are aware of the MSI-X related holes and make sure they are not mapped in order for the trap handlers to work properly.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v4:
- Add a middle priority and add the PCI header to it.

Changes since v3:
- Add a numerial suffix to the section used to store the pointer to each initializer function, and sort them at link time.

---
 xen/arch/arm/xen.lds.S    | 4 ++--
 xen/arch/x86/xen.lds.S    | 4 ++--
 xen/drivers/vpci/header.c | 2 +-
 xen/drivers/vpci/msi.c    | 2 +-
 xen/include/xen/vpci.h    | 8 ++++++--
 5 files changed, 12 insertions(+), 8 deletions(-)
xen/arch/arm/xen.lds.S +2 -2
index eb14909645..4a08435f7e 100644
@@ -68,7 +68,7 @@ SECTIONS
 
 #if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
@@ -182,7 +182,7 @@ SECTIONS
 
 #if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
xen/arch/x86/xen.lds.S +2 -2
index 61775953d6..3c44fb410e 100644
@@ -127,7 +127,7 @@ SECTIONS
 
 #if defined(CONFIG_HAS_PCI) && defined(CONFIG_LATE_HWDOM)
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
@@ -222,7 +222,7 @@ SECTIONS
 
 #if defined(CONFIG_HAS_PCI) && !defined(CONFIG_LATE_HWDOM)
        __start_vpci_array = .;
-       *(.data.vpci)
+       *(SORT(.data.vpci.*))
        __end_vpci_array = .;
 #endif
   } :text
xen/drivers/vpci/header.c +1 -1
index c0d38c8b91..07a6bbf0be 100644
@@ -465,7 +465,7 @@ static int vpci_init_bars(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(vpci_init_bars);
+REGISTER_VPCI_INIT(vpci_init_bars, VPCI_PRIORITY_MIDDLE);
 
 /*
  * Local variables:
xen/drivers/vpci/msi.c +1 -1
index 933adba0ff..7a0b0521c5 100644
@@ -307,7 +307,7 @@ static int vpci_init_msi(struct pci_dev *pdev)
 
     return 0;
 }
-REGISTER_VPCI_INIT(vpci_init_msi);
+REGISTER_VPCI_INIT(vpci_init_msi, VPCI_PRIORITY_LOW);
 
 void vpci_dump_msi(void)
 {
xen/include/xen/vpci.h +6 -2
index 5b582b8012..c6913631c0 100644
@@ -13,9 +13,13 @@ typedef void vpci_write_t(const struct pci_dev *pdev, unsigned int reg,
 
 typedef int vpci_register_init_t(struct pci_dev *dev);
 
-#define REGISTER_VPCI_INIT(x)                   \
+#define VPCI_PRIORITY_HIGH      "1"
+#define VPCI_PRIORITY_MIDDLE    "5"
+#define VPCI_PRIORITY_LOW       "9"
+
+#define REGISTER_VPCI_INIT(x, p)                \
   static vpci_register_init_t *const x##_entry  \
-               __used_section(".data.vpci") = x
+               __used_section(".data.vpci." p) = x
 
 /* Add vPCI handlers to device. */
 int __must_check vpci_add_handlers(struct pci_dev *dev);
Signature · 7 lines
2.11.0 (Apple Git-81)


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

PATCH v6 11/11 vpci/msix: add MSI-X handlers

In reply to Roger Pau Monne

Add handlers for accesses to the MSI-X message control field on the PCI configuration space, and traps for accesses to the memory region that contains the MSI-X table and PBA. This traps detect attempts from the guest to configure MSI-X interrupts and properly sets them up.

Note that accesses to the Table Offset, Table BIR, PBA Offset and PBA BIR are not trapped by Xen at the moment.

Finally, turn the panic in the Dom0 PVH builder into a warning.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v5:
- Update lock usage.
- Unbind/unmap PIRQs when MSIX is disabled.
- Share the arch-specific MSIX code with the MSI functions.
- Do not reference the MSIX memory areas from the PCI BARs fields, instead fetch the BIR and offset each time needed.
- Add the '_entry' suffix to the MSIX arch functions.
- Prefix the vMSIX macros with 'V'.
- s/gdprintk/gprintk/ in msix.c
- Make vpci_msix_access_check return bool, and change it's name to vpci_msix_access_allowed.
- Join the first two ifs in vpci_msix_{read/write} into a single one.
- Allow Dom0 to write to the PBA area.
- Add a note that reads from the PBA area will need to be translated if the PBA it's not identity mapped.

Changes since v4:
- Remove parentheses around offsetof.
- Add "being" to MSI-X enabling comment.
- Use INVALID_PIRQ.
- Add a simple sanity check to vpci_msix_arch_enable in order to detect wrong MSI-X entries more quickly.
- Constify vpci_msix_arch_print entry argument.
- s/cpu/fixed/ in vpci_msix_arch_print.
- Dump the MSI-X info together with the MSI info.
- Fix vpci_msix_control_write to take into account changes to the address and data fields when switching the function mask bit.
- Only disable/enable the entries if the address or data fields have been updated.
- Usew the BAR enable field to check if a BAR is mapped or not (instead of reading the command register for each device).
- Fix error path in vpci_msix_read to set the return data to ~0.
- Simplify mask usage in vpci_msix_write.
- Cast data to uint64_t when shifting it 32 bits.
- Fix writes to the table entry control register to take into account if the mask-all bit is set.
- Add some comments to clarify the intended behavior of the code.
- Align the PBA size to 64-bits.
- Remove the error label in vpci_init_msix.
- Try to compact the layout of the vpci_msix structure.
- Remove the local table_bar and pba_bar variables from vpci_init_msix, they are used only once.

Changes since v3:
- Propagate changes from previous versions: remove xen_ prefix, use the new fields in vpci_val and remove the return value from handlers.
- Remove the usage of GENMASK.
- Mave the arch-specific parts of the dump routine to the x86/hvm/vmsi.c dump handler.
- Chain the MSI-X dump handler to the 'M' debug key.
- Fix the header BAR mappings so that the MSI-X regions inside of BARs are unmapped from the domain p2m in order for the handlers to work properly.
- Unconditionally trap and forward accesses to the PBA MSI-X area.
- Simplify the conditionals in vpci_msix_control_write.
- Fix vpci_msix_accept to use a bool type.
- Allow all supported accesses as described in the spec to the MSI-X table.
- Truncate the returned address when the access is a 32b read.
- Always return X86EMUL_OKAY from the handlers, returning ~0 in the read case if the access is not supported, or ignoring writes.
- Do not check that max_entries is != 0 in the init handler.
- Use trylock in the dump handler.

Changes since v2:
- Split out arch-specific code.

This patch has been tested with devices using both a single MSI-X entry and multiple ones.

---
 xen/arch/x86/hvm/dom0_build.c    |   2 +-
 xen/arch/x86/hvm/hvm.c           |   1 +
 xen/arch/x86/hvm/vmsi.c          | 133 ++++++++--
 xen/drivers/vpci/Makefile        |   2 +-
 xen/drivers/vpci/header.c        |  16 ++
 xen/drivers/vpci/msi.c           |  22 +-
 xen/drivers/vpci/msix.c          | 506 +++++++++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/domain.h |   3 +
 xen/include/asm-x86/hvm/io.h     |   5 +
 xen/include/xen/vpci.h           |  45 ++++
 10 files changed, 705 insertions(+), 30 deletions(-)
 create mode 100644 xen/drivers/vpci/msix.c
xen/arch/x86/hvm/dom0_build.c +1 -1
index 17d77137d6..8fa92bc5b6 100644
@@ -1111,7 +1111,7 @@ int __init dom0_construct_pvh(struct domain *d, const module_t *image,
 
     pvh_setup_mmcfg(d);
 
-    panic("Building a PVHv2 Dom0 is not yet supported.");
+    printk("WARNING: PVH is an experimental mode with limited functionality\n");
     return 0;
 }
 
xen/arch/x86/hvm/hvm.c +1 -0
index b1064413fc..042b7c6a31 100644
@@ -585,6 +585,7 @@ int hvm_domain_initialise(struct domain *d, unsigned long domcr_flags,
     INIT_LIST_HEAD(&d->arch.hvm_domain.write_map.list);
     INIT_LIST_HEAD(&d->arch.hvm_domain.g2m_ioport_list);
     INIT_LIST_HEAD(&d->arch.hvm_domain.mmcfg_regions);
+    INIT_LIST_HEAD(&d->arch.hvm_domain.msix_tables);
 
     rc = create_perdomain_mapping(d, PERDOMAIN_VIRT_START, 0, NULL, NULL);
     if ( rc )
xen/arch/x86/hvm/vmsi.c +107 -26

206 lines not on this page · raw message

xen/drivers/vpci/Makefile +1 -1
index 62cec9e82b..55d1bdfda0 100644
@@ -1 +1 @@
-obj-y += vpci.o header.o msi.o
+obj-y += vpci.o header.o msi.o msix.o
xen/drivers/vpci/header.c +16 -0
index 07a6bbf0be..02b9776ea9 100644
@@ -152,6 +152,7 @@ static int vpci_check_bar_overlap(const struct pci_dev *pdev,
 static void vpci_modify_bars(const struct pci_dev *pdev, bool map)
 {
     struct vpci_header *header = &pdev->vpci->header;
+    struct vpci_msix *msix = pdev->vpci->msix;
     struct rangeset *mem = rangeset_new(NULL, NULL, 0);
     unsigned int i;
     int rc;
@@ -186,6 +187,21 @@ static void vpci_modify_bars(const struct pci_dev *pdev, bool map)
         }
     }
 
+    /* Remove any MSIX regions if present. */
+    for ( i = 0; msix && i < ARRAY_SIZE(msix->mem); i++ )
+    {
+        paddr_t start =
+            header->bars[msix->mem[i].bir].addr + msix->mem[i].offset;
+
+        rc = rangeset_remove_range(mem, PFN_DOWN(start),
+                                   PFN_DOWN(start + msix->mem[i].size - 1));
+        if ( rc )
+        {
+            rangeset_destroy(mem);
+            return;
+        }
+    }
+
     /* Check for overlaps with other device's BARs. */
     rc = vpci_check_bar_overlap(pdev, NULL, mem);
     if ( rc )
xen/drivers/vpci/msi.c +20 -2
index 7a0b0521c5..5c10a0d9c9 100644
@@ -320,13 +320,17 @@ void vpci_dump_msi(void)
         if ( !has_vpci(d) )
             continue;
 
-        printk("vPCI MSI information for d%d\n", d->domain_id);
+        printk("vPCI MSI/MSI-X information for d%d\n", d->domain_id);
 
         list_for_each_entry ( pdev, &d->arch.pdev_list, domain_list )
         {
             uint8_t seg = pdev->seg, bus = pdev->bus;
             uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
             const struct vpci_msi *msi = pdev->vpci->msi;
+            const struct vpci_msix *msix = pdev->vpci->msix;
+
+            if ( msi || msix )
+                printk("Device %04x:%02x:%02x.%u\n", seg, bus, slot, func);
 
             if ( !spin_trylock(&pdev->vpci->lock) )
             {
@@ -336,7 +340,7 @@ void vpci_dump_msi(void)
 
             if ( msi )
             {
-                printk("Device %04x:%02x:%02x.%u\n", seg, bus, slot, func);
+                printk(" MSI\n");
 
                 printk("  Enabled: %u Supports masking: %u 64-bit addresses: %u\n",
                        msi->enabled, msi->masking, msi->address64);
@@ -349,6 +353,20 @@ void vpci_dump_msi(void)
                     printk("  mask=%08x\n", msi->mask);
             }
 
+            if ( msix )
+            {
+                unsigned int i;
+
+                printk(" MSI-X\n");
+
+                printk("  Max entries: %u maskall: %u enabled: %u\n",
+                       msix->max_entries, msix->masked, msix->enabled);
+
+                printk("  Table entries:\n");
+                for ( i = 0; i < msix->max_entries; i++ )
+                    vpci_msix_arch_print_entry(&msix->entries[i]);
+            }
+
             spin_unlock(&pdev->vpci->lock);
             process_pending_softirqs();
         }
xen/drivers/vpci/msix.c new file +506 -0

506 lines not on this page · raw message

xen/include/asm-x86/hvm/domain.h +3 -0
index cd19ee11e9..5e3139e61c 100644
@@ -188,6 +188,9 @@ struct hvm_domain {
     struct list_head mmcfg_regions;
     rwlock_t mmcfg_lock;
 
+    /* List of MSI-X tables. */
+    struct list_head msix_tables;
+
     /* List of permanently write-mapped pages. */
     struct {
         spinlock_t lock;
xen/include/asm-x86/hvm/io.h +5 -0
index c47cc971d3..e7367071ce 100644
@@ -132,6 +132,11 @@ struct vpci_arch_msi {
     int pirq;
 };
 
+/* Arch-specific MSI-X entry data for vPCI. */
+struct vpci_arch_msix_entry {
+    int pirq;
+};
+
 enum stdvga_cache_state {
     STDVGA_CACHE_UNINITIALIZED,
     STDVGA_CACHE_ENABLED,
xen/include/xen/vpci.h +45 -0

57 lines not on this page · raw message

Signature · 7 lines
2.11.0 (Apple Git-81)


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

Re: [PATCH v6 11/11] vpci/msix: add MSI-X handlers

xen/drivers/vpci/header.c static int vpci_check_bar_overlap(const struct pci_dev *pdev, · line 155

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/header.c +1 -0
@@ -152,6 +152,7 @@ static int vpci_check_bar_overlap(const struct pci_dev *pdev,
 static void vpci_modify_bars(const struct pci_dev *pdev, bool map)
 {
     struct vpci_header *header = &pdev->vpci->header;
+    struct vpci_msix *msix = pdev->vpci->msix;

const and please fetch the value only right before you first need it.

Quoted 38 lines
xen/drivers/vpci/msi.c +10 -2
@@ -320,13 +320,17 @@ void vpci_dump_msi(void)
         if ( !has_vpci(d) )
             continue;
 
-        printk("vPCI MSI information for d%d\n", d->domain_id);
+        printk("vPCI MSI/MSI-X information for d%d\n", d->domain_id);
 
         list_for_each_entry ( pdev, &d->arch.pdev_list, domain_list )
         {
             uint8_t seg = pdev->seg, bus = pdev->bus;
             uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
             const struct vpci_msi *msi = pdev->vpci->msi;
+            const struct vpci_msix *msix = pdev->vpci->msix;
+
+            if ( msi || msix )
+                printk("Device %04x:%02x:%02x.%u\n", seg, bus, slot, func);
 
             if ( !spin_trylock(&pdev->vpci->lock) )
             {
@@ -336,7 +340,7 @@ void vpci_dump_msi(void)
 
             if ( msi )
             {
-                printk("Device %04x:%02x:%02x.%u\n", seg, bus, slot, func);
+                printk(" MSI\n");
 
                 printk("  Enabled: %u Supports masking: %u 64-bit addresses: %u\n",
                        msi->enabled, msi->masking, msi->address64);
@@ -349,6 +353,20 @@ void vpci_dump_msi(void)
                     printk("  mask=%08x\n", msi->mask);
             }
 
+            if ( msix )
+            {
+                unsigned int i;
+

xen/drivers/vpci/msi.c void vpci_dump_msi(void) · line 369

Quoted 10 lines
xen/drivers/vpci/msi.c +10 -0
+                printk(" MSI-X\n");
+
+                printk("  Max entries: %u maskall: %u enabled: %u\n",
+                       msix->max_entries, msix->masked, msix->enabled);
+
+                printk("  Table entries:\n");
+                for ( i = 0; i < msix->max_entries; i++ )
+                    vpci_msix_arch_print_entry(&msix->entries[i]);
+            }
+

Again, please try to reduce the amount of overall output.

Quoted 23 lines
xen/drivers/vpci/msi.c +23 -0
+static void vpci_msix_control_write(const struct pci_dev *pdev,
+                                    unsigned int reg, uint32_t val, void *data)
+{
+    uint8_t seg = pdev->seg, bus = pdev->bus;
+    uint8_t slot = PCI_SLOT(pdev->devfn), func = PCI_FUNC(pdev->devfn);
+    struct vpci_msix *msix = data;
+    bool new_masked, new_enabled;
+    unsigned int i;
+    int rc;
+
+    new_masked = val & PCI_MSIX_FLAGS_MASKALL;
+    new_enabled = val & PCI_MSIX_FLAGS_ENABLE;
+
+    /*
+     * According to the PCI 3.0 specification, switching the enable bit
+     * to 1 or the function mask bit to 0 should cause all the cached
+     * addresses and data fields to be recalculated. Xen implements this
+     * as disabling and enabling the entries.
+     *
+     * Note that the disable/enable sequence is only performed when the
+     * guest has written to the entry (ie: updated field set) or MSIX is
+     * enabled.
+     */

xen/drivers/vpci/msi.c void vpci_dump_msi(void)

Quoted 10 lines
xen/drivers/vpci/msi.c +10 -0
+    if ( new_enabled && !new_masked && (!msix->enabled || msix->masked) )
+    {
+        paddr_t table_base =
+            pdev->vpci->header.bars[msix->mem[VPCI_MSIX_TABLE].bir].addr;
+
+        for ( i = 0; i < msix->max_entries; i++ )
+        {
+            if ( msix->entries[i].masked ||
+                 (new_enabled && msix->enabled && !msix->entries[i].updated) )
+                continue;

This doesn't look to match up with the earlier comment.

xen/drivers/vpci/msi.c

Quoted 4 lines
xen/drivers/vpci/msi.c +4 -0
+static int vpci_msix_read(struct vcpu *v, unsigned long addr,
+                          unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;

const?

xen/drivers/vpci/msi.c

Quoted 10 lines
xen/drivers/vpci/msi.c +10 -0
+    const struct vpci_bar *bars;
+    struct vpci_msix *msix;
+    const struct vpci_msix_entry *entry;
+    unsigned int offset;
+
+    *data = ~0ul;
+
+    msix = vpci_msix_find(d, addr);
+    if ( !msix || !vpci_msix_access_allowed(msix->pdev, addr, len) )
+        return X86EMUL_OKAY;

In the !msix case I'm once again not convinced returning OKAY is correct here.

Quoted 30 lines
xen/include/xen/vpci.h +24 -0
@@ -100,6 +100,40 @@ struct vpci {
         /* 64-bit address capable? */
         bool address64;
     } *msi;
+
+    /* MSI-X data. */
+    struct vpci_msix {
+        struct pci_dev *pdev;
+        /* List link. */
+        struct list_head next;
+        /* Table information. */
+        struct vpci_msix_mem {
+            /* MSI-X table offset. */
+            unsigned int offset;
+            /* MSI-X table BIR. */
+            unsigned int bir;
+            /* Table size. */
+            unsigned int size;
+#define VPCI_MSIX_TABLE     0
+#define VPCI_MSIX_PBA       1
+#define VPCI_MSIX_MEM_NUM   2
+        } mem[VPCI_MSIX_MEM_NUM];
+        /* Maximum number of vectors supported by the device. */
+        unsigned int max_entries;
+        /* MSI-X enabled? */
+        bool enabled;
+        /* Masked? */
+        bool masked;

xen/include/xen/vpci.h struct vpci { · line 136

Quoted 10 lines
xen/include/xen/vpci.h +10 -0
+        /* Entries. */
+        struct vpci_msix_entry {
+            uint64_t addr;
+            uint32_t data;
+            unsigned int nr;
+            struct vpci_arch_msix_entry arch;
+            bool masked;
+            bool updated;
+        } entries[];
+    } *msix;

Same remark as for MSI regarding optimizing structure size.

Jan

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

#48 lore raw

Re: [PATCH v6 11/11] vpci/msix: add MSI-X handlers

Quoted 14 lines · Jan Beulich

xen/drivers/vpci/msix.c line 229

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/drivers/vpci/msix.c new file +10 -0
+    const struct vpci_bar *bars;
+    struct vpci_msix *msix;
+    const struct vpci_msix_entry *entry;
+    unsigned int offset;
+
+    *data = ~0ul;
+
+    msix = vpci_msix_find(d, addr);
+    if ( !msix || !vpci_msix_access_allowed(msix->pdev, addr, len) )
+        return X86EMUL_OKAY;

In the !msix case I'm once again not convinced returning OKAY is correct here.

From what we have spoken in the mmcfg case, for the !msix case Xen should return _RETRY. This error can only happen when the msix table is unmapped in between a accept and read/write call, so calling the accept handler again will return the correct value.

Quoted 34 lines
Quoted 34 lines
xen/include/xen/vpci.h +28 -0
@@ -100,6 +100,40 @@ struct vpci {
         /* 64-bit address capable? */
         bool address64;
     } *msi;
+
+    /* MSI-X data. */
+    struct vpci_msix {
+        struct pci_dev *pdev;
+        /* List link. */
+        struct list_head next;
+        /* Table information. */
+        struct vpci_msix_mem {
+            /* MSI-X table offset. */
+            unsigned int offset;
+            /* MSI-X table BIR. */
+            unsigned int bir;
+            /* Table size. */
+            unsigned int size;
+#define VPCI_MSIX_TABLE     0
+#define VPCI_MSIX_PBA       1
+#define VPCI_MSIX_MEM_NUM   2
+        } mem[VPCI_MSIX_MEM_NUM];
+        /* Maximum number of vectors supported by the device. */
+        unsigned int max_entries;
+        /* MSI-X enabled? */
+        bool enabled;
+        /* Masked? */
+        bool masked;
+        /* Entries. */
+        struct vpci_msix_entry {
+            uint64_t addr;
+            uint32_t data;
Quoted 8 lines

xen/include/xen/vpci.h struct vpci { · line 136

Quoted 6 lines
xen/include/xen/vpci.h +6 -0
+            unsigned int nr;
+            struct vpci_arch_msix_entry arch;
+            bool masked;
+            bool updated;
+        } entries[];
+    } *msix;

Same remark as for MSI regarding optimizing structure size.

Going over the fields, bir can be turned into a uint8_t, and size into a uint16_t. max_entries can also be converted to a uint16_t together with nr.

Apart from that I don't see much more optimization, unless we start packaging fields (ie: offset and bir could reside in a uint32_t), but IMHO that's going to make the code harder to parse for little gain, and will involve more calculations in the handlers.

Thanks, Roger.

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

#49 lore raw

Re: [PATCH v6 11/11] vpci/msix: add MSI-X handlers

Quoted 46 lines
Quoted 1 line
Quoted 1 line · On 10.10.17 at 17:04, <[email protected]> wrote:
Quoted 35 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · On 19.09.17 at 17:29, <[email protected]> wrote:
xen/include/xen/vpci.h +28 -0
@@ -100,6 +100,40 @@ struct vpci {
         /* 64-bit address capable? */
         bool address64;
     } *msi;
+
+    /* MSI-X data. */
+    struct vpci_msix {
+        struct pci_dev *pdev;
+        /* List link. */
+        struct list_head next;
+        /* Table information. */
+        struct vpci_msix_mem {
+            /* MSI-X table offset. */
+            unsigned int offset;
+            /* MSI-X table BIR. */
+            unsigned int bir;
+            /* Table size. */
+            unsigned int size;
+#define VPCI_MSIX_TABLE     0
+#define VPCI_MSIX_PBA       1
+#define VPCI_MSIX_MEM_NUM   2
+        } mem[VPCI_MSIX_MEM_NUM];
+        /* Maximum number of vectors supported by the device. */
+        unsigned int max_entries;
+        /* MSI-X enabled? */
+        bool enabled;
+        /* Masked? */
+        bool masked;
+        /* Entries. */
+        struct vpci_msix_entry {
+            uint64_t addr;
+            uint32_t data;
Quoted 8 lines

xen/include/xen/vpci.h struct vpci { · line 136

Quoted 6 lines
xen/include/xen/vpci.h +6 -0
+            unsigned int nr;
+            struct vpci_arch_msix_entry arch;
+            bool masked;
+            bool updated;
+        } entries[];
+    } *msix;

Same remark as for MSI regarding optimizing structure size.

Quoted 8 lines

Going over the fields, bir can be turned into a uint8_t, and size into a uint16_t. max_entries can also be converted to a uint16_t together with nr.

Apart from that I don't see much more optimization, unless we start packaging fields (ie: offset and bir could reside in a uint32_t), but IMHO that's going to make the code harder to parse for little gain, and will involve more calculations in the handlers.

The more instances of a structure there may be, the more relevant it is to pack them tightly. I.e. primary focus needs to be on struct vpci_msix_entry, but since - as indicated - there may be many devices supporting MSI-X, struct vpci_msix as a whole should be reasonably well packed as well. I don't think more calculation in the handlers is an argument - the compiler will do it for you, and the affected code shouldn't really be performance critical (it's involved in setting up interrupts, not delivering them).

Jan

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

#50 lore raw
In reply to Roger Pau Monne

Hello,
________________________________________

Quoted 11 lines

From: Xen-devel [[email protected]] on behalf of Roger Pau Monne [[email protected]]
Sent: Tuesday, September 19, 2017 8:59 PM
To: [email protected]
Cc: [email protected]
Subject: [Xen-devel] [PATCH v6 00/11] vpci: PCI config space emulation

Hello,

The following series contain an implementation of handlers for the PCI configuration space inside of Xen. This allows Xen to detect accesses to the PCI configuration space and react accordingly.

Is someone working on arm port of this series?
I would be happy to test, and help in developing remaining parts if any...

-manish

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