Skip to contentSkip to conversation

ioreq: add support for internal servers

PATCH In review Roger Pau Monne 4 participants 24 messages Sep 30, 2019 – Oct 25, 2019

lore
Share & tools
lore
#0 lore raw

v3 cover ioreq: add support for internal servers

Such internal servers are implemented by a single function that handles ioreqs inside the hypervisor.

The motivation behind this change is to switch vPCI to become an internal ioreq server, so that accesses to the PCI config space can be multiplexed between devices handled by vPCI and devices handled by other ioreq servers.

The implementation is fairly simple and limited to what's needed by vPCI, but can be expanded in the future if other more complex users appear.

The series can also be found at:

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

Thanks, Roger.

Roger Pau Monne (10):
ioreq: terminate cf8 handling at hypervisor level
ioreq: switch selection and forwarding to use ioservid_t
ioreq: add fields to allow internal ioreq servers
ioreq: add internal ioreq initialization support
ioreq: allow dispatching ioreqs to internal servers
ioreq: allow registering internal ioreq server handler
ioreq: allow decoding accesses to MMCFG regions
vpci: register as an internal ioreq server
ioreq: split the code to detect PCI config space accesses
ioreq: provide support for long-running operations...

 tools/tests/vpci/Makefile           |   5 +-
 tools/tests/vpci/emul.h             |   4 +
 xen/arch/x86/hvm/dm.c               |  19 +-
 xen/arch/x86/hvm/dom0_build.c       |   9 +-
 xen/arch/x86/hvm/emulate.c          |  14 +-
 xen/arch/x86/hvm/hvm.c              |   7 +-
 xen/arch/x86/hvm/io.c               | 248 ++------------------
 xen/arch/x86/hvm/ioreq.c            | 348 ++++++++++++++++++++--------
 xen/arch/x86/hvm/stdvga.c           |   8 +-
 xen/arch/x86/mm/p2m.c               |  20 +-
 xen/arch/x86/physdev.c              |   5 +-
 xen/drivers/passthrough/x86/iommu.c |   2 +-
 xen/drivers/vpci/header.c           |  60 ++---
 xen/drivers/vpci/vpci.c             |  70 +++++-
 xen/include/asm-x86/hvm/domain.h    |  35 ++-
 xen/include/asm-x86/hvm/io.h        |  29 ++-
 xen/include/asm-x86/hvm/ioreq.h     |  17 +-
 xen/include/asm-x86/hvm/vcpu.h      |   3 +-
 xen/include/asm-x86/p2m.h           |   9 +-
 xen/include/public/hvm/dm_op.h      |   1 +
 xen/include/xen/vpci.h              |  28 +--
 21 files changed, 495 insertions(+), 446 deletions(-)
Signature · 7 lines
2.23.0


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

PATCH v3 1/10 ioreq: terminate cf8 handling at hypervisor level

Do not forward accesses to cf8 to external emulators, decoding of PCI accesses is handled by Xen, and emulators can request handling of config space accesses of devices using the provided ioreq interface.

Fully terminate cf8 accesses at the hypervisor level, by improving the existing hvm_access_cf8 helper to also handle register reads, and always return X86EMUL_OKAY in order to terminate the emulation.

Note that without this change in the absence of some external emulator that catches accesses to cf8 read requests to the register would misbehave, as the ioreq internal handler did not handle those.

---
Changes since v2:
- Allow ioreq servers to map 0xcf8 and 0xcfc, even if those are handled by the hypervisor.

Changes since v1:
- New in this version.

---
 xen/arch/x86/hvm/ioreq.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/ioreq.c +7 -3
index d347144096..5e503ce498 100644
@@ -1518,11 +1518,15 @@ static int hvm_access_cf8(
 {
     struct domain *d = current->domain;
 
-    if ( dir == IOREQ_WRITE && bytes == 4 )
+    if ( bytes != 4 )
+        return X86EMUL_OKAY;
+
+    if ( dir == IOREQ_WRITE )
         d->arch.hvm.pci_cf8 = *val;
+    else
+        *val = d->arch.hvm.pci_cf8;
 
-    /* We always need to fall through to the catch all emulator */
-    return X86EMUL_UNHANDLEABLE;
+    return X86EMUL_OKAY;
 }
 
 void hvm_ioreq_init(struct domain *d)
Signature · 7 lines
2.23.0


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

Re: [Xen-devel] [PATCH v3 01/10] ioreq: terminate cf8 handling at hypervisor level

Quoted 16 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Roger Pau Monne
Sent: 30 September 2019 14:32
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Paul Durrant <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Roger Pau Monne <[email protected]>
Subject: [Xen-devel] [PATCH v3 01/10] ioreq: terminate cf8 handling at hypervisor level

Do not forward accesses to cf8 to external emulators, decoding of PCI accesses is handled by Xen, and emulators can request handling of config space accesses of devices using the provided ioreq interface.

Fully terminate cf8 accesses at the hypervisor level, by improving the existing hvm_access_cf8 helper to also handle register reads, and always return X86EMUL_OKAY in order to terminate the emulation.

Quoted 5 lines

Note that without this change in the absence of some external emulator that catches accesses to cf8 read requests to the register would misbehave, as the ioreq internal handler did not handle those.

Quoted 38 lines

---
Changes since v2:
- Allow ioreq servers to map 0xcf8 and 0xcfc, even if those are handled by the hypervisor.

Changes since v1:
- New in this version.

---
 xen/arch/x86/hvm/ioreq.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/ioreq.c +7 -4
index d347144096..5e503ce498 100644
@@ -1518,11 +1518,15 @@ static int hvm_access_cf8(
 {
     struct domain *d = current->domain;
 
-    if ( dir == IOREQ_WRITE && bytes == 4 )
+    if ( bytes != 4 )
+        return X86EMUL_OKAY;
+
+    if ( dir == IOREQ_WRITE )
         d->arch.hvm.pci_cf8 = *val;
+    else
+        *val = d->arch.hvm.pci_cf8;
 
-    /* We always need to fall through to the catch all emulator */
-    return X86EMUL_UNHANDLEABLE;
+    return X86EMUL_OKAY;
 }
 
 void hvm_ioreq_init(struct domain *d)
--

2.23.0

Quoted 4 lines

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

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

#3 lore raw

Re: [Xen-devel] [PATCH v3 01/10] ioreq: terminate cf8 handling at hypervisor level

In reply to Roger Pau Monne

xen/arch/x86/hvm/ioreq.c static int hvm_access_cf8( · line 1522

Quoted 9 lines · Roger Pau Monne
xen/arch/x86/hvm/ioreq.c +2 -1
@@ -1518,11 +1518,15 @@ static int hvm_access_cf8(
 {
     struct domain *d = current->domain;
 
-    if ( dir == IOREQ_WRITE && bytes == 4 )
+    if ( bytes != 4 )
+        return X86EMUL_OKAY;

I think it was already on v1 that Andrew had pointed out that e.g. a 1-bye access to CF9 should still be forwarded. I guess you mean to use X86EMUL_UNHANDLEABLE here, just like was done ...

xen/arch/x86/hvm/ioreq.c static int hvm_access_cf8( · line 1530

Quoted 9 lines
xen/arch/x86/hvm/ioreq.c +4 -2
+    if ( dir == IOREQ_WRITE )
         d->arch.hvm.pci_cf8 = *val;
+    else
+        *val = d->arch.hvm.pci_cf8;
 
-    /* We always need to fall through to the catch all emulator */
-    return X86EMUL_UNHANDLEABLE;
+    return X86EMUL_OKAY;
 }

... universally before. The comment (suitably adjusted) may then also want to move up.

Jan

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

#4 lore raw

PATCH v3 2/10 ioreq: switch selection and forwarding to use ioservid_t

In reply to Roger Pau Monne

hvm_select_ioreq_server and hvm_send_ioreq where both using hvm_ioreq_server directly, switch to use ioservid_t in order to select and forward ioreqs.

This is a preparatory change, since future patches will use the ioreq server id in order to differentiate between internal and external ioreq servers.

---
Changes since v2:
- Don't hardcode 0xffff for XEN_INVALID_IOSERVID.

Changes since v1:
- New in this version.

---
 xen/arch/x86/hvm/dm.c           |  2 +-
 xen/arch/x86/hvm/emulate.c      | 14 +++++++-------
 xen/arch/x86/hvm/ioreq.c        | 24 ++++++++++++------------
 xen/arch/x86/hvm/stdvga.c       |  8 ++++----
 xen/arch/x86/mm/p2m.c           | 20 ++++++++++----------
 xen/include/asm-x86/hvm/ioreq.h |  5 ++---
 xen/include/asm-x86/p2m.h       |  9 ++++-----
 xen/include/public/hvm/dm_op.h  |  1 +
 8 files changed, 41 insertions(+), 42 deletions(-)
xen/arch/x86/hvm/dm.c +1 -1
index d6d0e8be89..c2fca9f729 100644
@@ -263,7 +263,7 @@ static int set_mem_type(struct domain *d,
             return -EOPNOTSUPP;
 
         /* Do not change to HVMMEM_ioreq_server if no ioreq server mapped. */
-        if ( !p2m_get_ioreq_server(d, &flags) )
+        if ( p2m_get_ioreq_server(d, &flags) == XEN_INVALID_IOSERVID )
             return -EINVAL;
     }
 
xen/arch/x86/hvm/emulate.c +7 -7
index 637034b6a1..c37bd020c8 100644
@@ -255,7 +255,7 @@ static int hvmemul_do_io(
          * However, there's no cheap approach to avoid above situations in xen,
          * so the device model side needs to check the incoming ioreq event.
          */
-        struct hvm_ioreq_server *s = NULL;
+        ioservid_t id = XEN_INVALID_IOSERVID;
         p2m_type_t p2mt = p2m_invalid;
 
         if ( is_mmio )
@@ -268,9 +268,9 @@ static int hvmemul_do_io(
             {
                 unsigned int flags;
 
-                s = p2m_get_ioreq_server(currd, &flags);
+                id = p2m_get_ioreq_server(currd, &flags);
 
-                if ( s == NULL )
+                if ( id == XEN_INVALID_IOSERVID )
                 {
                     rc = X86EMUL_RETRY;
                     vio->io_req.state = STATE_IOREQ_NONE;
@@ -290,18 +290,18 @@ static int hvmemul_do_io(
             }
         }
 
-        if ( !s )
-            s = hvm_select_ioreq_server(currd, &p);
+        if ( id == XEN_INVALID_IOSERVID )
+            id = hvm_select_ioreq_server(currd, &p);
 
         /* If there is no suitable backing DM, just ignore accesses */
-        if ( !s )
+        if ( id == XEN_INVALID_IOSERVID )
         {
             rc = hvm_process_io_intercept(&null_handler, &p);
             vio->io_req.state = STATE_IOREQ_NONE;
         }
         else
         {
-            rc = hvm_send_ioreq(s, &p, 0);
+            rc = hvm_send_ioreq(id, &p, 0);
             if ( rc != X86EMUL_RETRY || currd->is_shutting_down )
                 vio->io_req.state = STATE_IOREQ_NONE;
             else if ( !hvm_ioreq_needs_completion(&vio->io_req) )
xen/arch/x86/hvm/ioreq.c +12 -12
index 5e503ce498..ed0142c4e1 100644
@@ -39,6 +39,7 @@ static void set_ioreq_server(struct domain *d, unsigned int id,
 {
     ASSERT(id < MAX_NR_IOREQ_SERVERS);
     ASSERT(!s || !d->arch.hvm.ioreq_server.server[id]);
+    BUILD_BUG_ON(MAX_NR_IOREQ_SERVERS >= XEN_INVALID_IOSERVID);
 
     d->arch.hvm.ioreq_server.server[id] = s;
 }
@@ -868,7 +869,7 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id)
 
     domain_pause(d);
 
-    p2m_set_ioreq_server(d, 0, s);
+    p2m_set_ioreq_server(d, 0, id);
 
     hvm_ioreq_server_disable(s);
 
@@ -1125,7 +1126,7 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,
     if ( s->emulator != current->domain )
         goto out;
 
-    rc = p2m_set_ioreq_server(d, flags, s);
+    rc = p2m_set_ioreq_server(d, flags, id);
 
  out:
     spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock);
@@ -1249,8 +1250,7 @@ void hvm_destroy_all_ioreq_servers(struct domain *d)
     spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock);
 }
 
-struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
-                                                 ioreq_t *p)
+ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p)
 {
     struct hvm_ioreq_server *s;
     uint32_t cf8;
@@ -1259,7 +1259,7 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
     unsigned int id;
 
     if ( p->type != IOREQ_TYPE_COPY && p->type != IOREQ_TYPE_PIO )
-        return NULL;
+        return XEN_INVALID_IOSERVID;
 
     cf8 = d->arch.hvm.pci_cf8;
 
@@ -1314,7 +1314,7 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
             start = addr;
             end = start + p->size - 1;
             if ( rangeset_contains_range(r, start, end) )
-                return s;
+                return id;
 
             break;
 
@@ -1323,7 +1323,7 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
             end = hvm_mmio_last_byte(p);
 
             if ( rangeset_contains_range(r, start, end) )
-                return s;
+                return id;
 
             break;
 
@@ -1332,14 +1332,14 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
             {
                 p->type = IOREQ_TYPE_PCI_CONFIG;
                 p->addr = addr;
-                return s;
+                return id;
             }
 
             break;
         }
     }
 
-    return NULL;
+    return XEN_INVALID_IOSERVID;
 }
 
 static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p)
@@ -1435,12 +1435,12 @@ static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p)
     return X86EMUL_OKAY;
 }
 
-int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p,
-                   bool buffered)
+int hvm_send_ioreq(ioservid_t id, ioreq_t *proto_p, bool buffered)
 {
     struct vcpu *curr = current;
     struct domain *d = curr->domain;
     struct hvm_ioreq_vcpu *sv;
+    struct hvm_ioreq_server *s = get_ioreq_server(d, id);
 
     ASSERT(s);
 
@@ -1506,7 +1506,7 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered)
         if ( !s->enabled )
             continue;
 
-        if ( hvm_send_ioreq(s, p, buffered) == X86EMUL_UNHANDLEABLE )
+        if ( hvm_send_ioreq(id, p, buffered) == X86EMUL_UNHANDLEABLE )
             failed++;
     }
 
xen/arch/x86/hvm/stdvga.c +4 -4
index bd398dbb1b..a689269712 100644
@@ -466,7 +466,7 @@ static int stdvga_mem_write(const struct hvm_io_handler *handler,
         .dir = IOREQ_WRITE,
         .data = data,
     };
-    struct hvm_ioreq_server *srv;
+    ioservid_t id;
 
     if ( !stdvga_cache_is_enabled(s) || !s->stdvga )
         goto done;
@@ -507,11 +507,11 @@ static int stdvga_mem_write(const struct hvm_io_handler *handler,
     }
 
  done:
-    srv = hvm_select_ioreq_server(current->domain, &p);
-    if ( !srv )
+    id = hvm_select_ioreq_server(current->domain, &p);
+    if ( id == XEN_INVALID_IOSERVID )
         return X86EMUL_UNHANDLEABLE;
 
-    return hvm_send_ioreq(srv, &p, 1);
+    return hvm_send_ioreq(id, &p, 1);
 }
 
 static bool_t stdvga_mem_accept(const struct hvm_io_handler *handler,
xen/arch/x86/mm/p2m.c +10 -10
index e5e4349dea..c0edb9a319 100644
@@ -102,6 +102,7 @@ static int p2m_initialise(struct domain *d, struct p2m_domain *p2m)
         p2m_pt_init(p2m);
 
     spin_lock_init(&p2m->ioreq.lock);
+    p2m->ioreq.server = XEN_INVALID_IOSERVID;
 
     return ret;
 }
@@ -361,7 +362,7 @@ void p2m_memory_type_changed(struct domain *d)
 
 int p2m_set_ioreq_server(struct domain *d,
                          unsigned int flags,
-                         struct hvm_ioreq_server *s)
+                         ioservid_t id)
 {
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
     int rc;
@@ -376,16 +377,16 @@ int p2m_set_ioreq_server(struct domain *d,
     if ( flags == 0 )
     {
         rc = -EINVAL;
-        if ( p2m->ioreq.server != s )
+        if ( p2m->ioreq.server != id )
             goto out;
 
-        p2m->ioreq.server = NULL;
+        p2m->ioreq.server = XEN_INVALID_IOSERVID;
         p2m->ioreq.flags = 0;
     }
     else
     {
         rc = -EBUSY;
-        if ( p2m->ioreq.server != NULL )
+        if ( p2m->ioreq.server != XEN_INVALID_IOSERVID )
             goto out;
 
         /*
@@ -397,7 +398,7 @@ int p2m_set_ioreq_server(struct domain *d,
         if ( read_atomic(&p2m->ioreq.entry_count) )
             goto out;
 
-        p2m->ioreq.server = s;
+        p2m->ioreq.server = id;
         p2m->ioreq.flags = flags;
     }
 
@@ -409,19 +410,18 @@ int p2m_set_ioreq_server(struct domain *d,
     return rc;
 }
 
-struct hvm_ioreq_server *p2m_get_ioreq_server(struct domain *d,
-                                              unsigned int *flags)
+ioservid_t p2m_get_ioreq_server(struct domain *d, unsigned int *flags)
 {
     struct p2m_domain *p2m = p2m_get_hostp2m(d);
-    struct hvm_ioreq_server *s;
+    ioservid_t id;
 
     spin_lock(&p2m->ioreq.lock);
 
-    s = p2m->ioreq.server;
+    id = p2m->ioreq.server;
     *flags = p2m->ioreq.flags;
 
     spin_unlock(&p2m->ioreq.lock);
-    return s;
+    return id;
 }
 
 void p2m_enable_hardware_log_dirty(struct domain *d)
xen/include/asm-x86/hvm/ioreq.h +2 -3
index e2588e912f..65491c48d2 100644
@@ -47,9 +47,8 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v);
 void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v);
 void hvm_destroy_all_ioreq_servers(struct domain *d);
 
-struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
-                                                 ioreq_t *p);
-int hvm_send_ioreq(struct hvm_ioreq_server *s, ioreq_t *proto_p,
+ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p);
+int hvm_send_ioreq(ioservid_t id, ioreq_t *proto_p,
                    bool buffered);
 unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);
 
xen/include/asm-x86/p2m.h +4 -5
index 94285db1b4..99a1dab311 100644
@@ -354,7 +354,7 @@ struct p2m_domain {
           * ioreq server who's responsible for the emulation of
           * gfns with specific p2m type(for now, p2m_ioreq_server).
           */
-         struct hvm_ioreq_server *server;
+         ioservid_t server;
          /*
           * flags specifies whether read, write or both operations
           * are to be emulated by an ioreq server.
@@ -819,7 +819,7 @@ static inline p2m_type_t p2m_recalc_type_range(bool recalc, p2m_type_t t,
     if ( !recalc || !p2m_is_changeable(t) )
         return t;
 
-    if ( t == p2m_ioreq_server && p2m->ioreq.server != NULL )
+    if ( t == p2m_ioreq_server && p2m->ioreq.server != XEN_INVALID_IOSERVID )
         return t;
 
     return p2m_is_logdirty_range(p2m, gfn_start, gfn_end) ? p2m_ram_logdirty
@@ -938,9 +938,8 @@ static inline unsigned int p2m_get_iommu_flags(p2m_type_t p2mt, mfn_t mfn)
 }
 
 int p2m_set_ioreq_server(struct domain *d, unsigned int flags,
-                         struct hvm_ioreq_server *s);
-struct hvm_ioreq_server *p2m_get_ioreq_server(struct domain *d,
-                                              unsigned int *flags);
+                         ioservid_t id);
+ioservid_t p2m_get_ioreq_server(struct domain *d, unsigned int *flags);
 
 static inline int p2m_entry_modify(struct p2m_domain *p2m, p2m_type_t nt,
                                    p2m_type_t ot, mfn_t nfn, mfn_t ofn,
xen/include/public/hvm/dm_op.h +1 -0
index d3b554d019..ee3963e54f 100644
@@ -54,6 +54,7 @@
  */
 
 typedef uint16_t ioservid_t;
+#define XEN_INVALID_IOSERVID ((ioservid_t)~0)
 
 /*
  * XEN_DMOP_create_ioreq_server: Instantiate a new IOREQ Server for a
Signature · 7 lines
2.23.0


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

PATCH v3 3/10 ioreq: add fields to allow internal ioreq servers

In reply to Roger Pau Monne

Internal ioreq servers are plain function handlers implemented inside of the hypervisor. Note that most fields used by current (external) ioreq servers are not needed for internal ones, and hence have been placed inside of a struct and packed in an union together with the only internal specific field, a function pointer to a handler.

This is required in order to have PCI config accesses forwarded to external ioreq servers or to internal ones (ie: QEMU emulated devices vs vPCI passthrough), and is the first step in order to allow unprivileged domains to use vPCI.

---
Changes since v2:
- Drop the vcpu parameter from the handler.

Changes since v1:
- Do not add an internal field to the ioreq server struct, whether a server is internal or external can already be inferred from the id.
- Add an extra parameter to the internal handler in order to pass user-provided opaque data to the handler.

---
 xen/include/asm-x86/hvm/domain.h | 30 +++++++++++++++++++-----------
 1 file changed, 19 insertions(+), 11 deletions(-)
xen/include/asm-x86/hvm/domain.h +19 -11
index bcc5621797..56a32e3e35 100644
@@ -52,21 +52,29 @@ struct hvm_ioreq_vcpu {
 #define MAX_NR_IO_RANGES  256
 
 struct hvm_ioreq_server {
-    struct domain          *target, *emulator;
-
+    struct domain          *target;
     /* Lock to serialize toolstack modifications */
     spinlock_t             lock;
-
-    struct hvm_ioreq_page  ioreq;
-    struct list_head       ioreq_vcpu_list;
-    struct hvm_ioreq_page  bufioreq;
-
-    /* Lock to serialize access to buffered ioreq ring */
-    spinlock_t             bufioreq_lock;
-    evtchn_port_t          bufioreq_evtchn;
     struct rangeset        *range[NR_IO_RANGE_TYPES];
     bool                   enabled;
-    uint8_t                bufioreq_handling;
+
+    union {
+        struct {
+            struct domain          *emulator;
+            struct hvm_ioreq_page  ioreq;
+            struct list_head       ioreq_vcpu_list;
+            struct hvm_ioreq_page  bufioreq;
+
+            /* Lock to serialize access to buffered ioreq ring */
+            spinlock_t             bufioreq_lock;
+            evtchn_port_t          bufioreq_evtchn;
+            uint8_t                bufioreq_handling;
+        };
+        struct {
+            void                   *data;
+            int (*handler)(ioreq_t *, void *);
+        };
+    };
 };
 
 /*
Signature · 7 lines
2.23.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v3 3/10
#6 lore raw

Re: [Xen-devel] [PATCH v3 03/10] ioreq: add fields to allow internal ioreq servers

Quoted 6 lines · Roger Pau Monne

Internal ioreq servers are plain function handlers implemented inside of the hypervisor. Note that most fields used by current (external) ioreq servers are not needed for internal ones, and hence have been placed inside of a struct and packed in an union together with the only internal specific field, a function pointer to a handler.

Quoted 7 lines

This is required in order to have PCI config accesses forwarded to external ioreq servers or to internal ones (ie: QEMU emulated devices vs vPCI passthrough), and is the first step in order to allow unprivileged domains to use vPCI.

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

#7 lore raw

PATCH v3 4/10 ioreq: add internal ioreq initialization support

In reply to Roger Pau Monne

Add support for internal ioreq servers to initialization and deinitialization routines, prevent some functions from being executed against internal ioreq servers and add guards to only allow internal callers to modify internal ioreq servers. External callers (ie: from hypercalls) are only allowed to deal with external ioreq servers.

---
Changes since v2:
- Return early from hvm_ioreq_server_init and hvm_ioreq_server_deinit if server is internal.
- hvm_destroy_ioreq_server, hvm_get_ioreq_server_info and hvm_map_mem_type_to_ioreq_server can only be used against external servers, hence add an assert to that effect.
- Simplify ASSERT in hvm_create_ioreq_server.

Changes since v1:
- Do not pass an 'internal' parameter to most functions, and instead use the id to key whether an ioreq server is internal or external.
- Prevent enabling an internal server without a handler.

---
 xen/arch/x86/hvm/dm.c            |  17 ++++-
 xen/arch/x86/hvm/ioreq.c         | 119 ++++++++++++++++++++-----------
 xen/include/asm-x86/hvm/domain.h |   5 +-
 xen/include/asm-x86/hvm/ioreq.h  |   8 ++-
 4 files changed, 105 insertions(+), 44 deletions(-)
xen/arch/x86/hvm/dm.c +16 -1
index c2fca9f729..6a3682e58c 100644
@@ -417,7 +417,7 @@ static int dm_op(const struct dmop_args *op_args)
             break;
 
         rc = hvm_create_ioreq_server(d, data->handle_bufioreq,
-                                     &data->id);
+                                     &data->id, false);
         break;
     }
 
@@ -450,6 +450,9 @@ static int dm_op(const struct dmop_args *op_args)
         rc = -EINVAL;
         if ( data->pad )
             break;
+        rc = -EPERM;
+        if ( hvm_ioreq_is_internal(data->id) )
+            break;
 
         rc = hvm_map_io_range_to_ioreq_server(d, data->id, data->type,
                                               data->start, data->end);
@@ -464,6 +467,9 @@ static int dm_op(const struct dmop_args *op_args)
         rc = -EINVAL;
         if ( data->pad )
             break;
+        rc = -EPERM;
+        if ( hvm_ioreq_is_internal(data->id) )
+            break;
 
         rc = hvm_unmap_io_range_from_ioreq_server(d, data->id, data->type,
                                                   data->start, data->end);
@@ -481,6 +487,9 @@ static int dm_op(const struct dmop_args *op_args)
         rc = -EOPNOTSUPP;
         if ( !hap_enabled(d) )
             break;
+        rc = -EPERM;
+        if ( hvm_ioreq_is_internal(data->id) )
+            break;
 
         if ( first_gfn == 0 )
             rc = hvm_map_mem_type_to_ioreq_server(d, data->id,
@@ -528,6 +537,9 @@ static int dm_op(const struct dmop_args *op_args)
         rc = -EINVAL;
         if ( data->pad )
             break;
+        rc = -EPERM;
+        if ( hvm_ioreq_is_internal(data->id) )
+            break;
 
         rc = hvm_set_ioreq_server_state(d, data->id, !!data->enabled);
         break;
@@ -541,6 +553,9 @@ static int dm_op(const struct dmop_args *op_args)
         rc = -EINVAL;
         if ( data->pad )
             break;
+        rc = -EPERM;
+        if ( hvm_ioreq_is_internal(data->id) )
+            break;
 
         rc = hvm_destroy_ioreq_server(d, data->id);
         break;
xen/arch/x86/hvm/ioreq.c +78 -41

319 lines not on this page · raw message

xen/include/asm-x86/hvm/domain.h +4 -1
index 56a32e3e35..f09ce9b417 100644
@@ -97,7 +97,10 @@ struct hvm_pi_ops {
     void (*vcpu_block)(struct vcpu *);
 };
 
-#define MAX_NR_IOREQ_SERVERS 8
+#define MAX_NR_EXTERNAL_IOREQ_SERVERS 8
+#define MAX_NR_INTERNAL_IOREQ_SERVERS 1
+#define MAX_NR_IOREQ_SERVERS \
+    (MAX_NR_EXTERNAL_IOREQ_SERVERS + MAX_NR_INTERNAL_IOREQ_SERVERS)
 
 struct hvm_domain {
     /* Guest page range used for non-default ioreq servers */
xen/include/asm-x86/hvm/ioreq.h +7 -1
index 65491c48d2..c3917aa74d 100644
@@ -24,7 +24,7 @@ bool handle_hvm_io_completion(struct vcpu *v);
 bool is_ioreq_server_page(struct domain *d, const struct page_info *page);
 
 int hvm_create_ioreq_server(struct domain *d, int bufioreq_handling,
-                            ioservid_t *id);
+                            ioservid_t *id, bool internal);
 int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id);
 int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,
                               unsigned long *ioreq_gfn,
@@ -54,6 +54,12 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);
 
 void hvm_ioreq_init(struct domain *d);
 
+static inline bool hvm_ioreq_is_internal(unsigned int id)
+{
+    ASSERT(id < MAX_NR_IOREQ_SERVERS);
+    return id >= MAX_NR_EXTERNAL_IOREQ_SERVERS;
+}
+
 #endif /* __ASM_X86_HVM_IOREQ_H__ */
 
 /*
Signature · 7 lines
2.23.0


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

Re: [Xen-devel] [PATCH v3 04/10] ioreq: add internal ioreq initialization support

xen/include/asm-x86/hvm/ioreq.h unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); · line 60

Quoted 12 lines · Roger Pau Monne
xen/include/asm-x86/hvm/ioreq.h +4 -0
index 65491c48d2..c3917aa74d 100644
@@ -54,6 +54,12 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);
 
 void hvm_ioreq_init(struct domain *d);
 
+static inline bool hvm_ioreq_is_internal(unsigned int id)
+{
+    ASSERT(id < MAX_NR_IOREQ_SERVERS);
+    return id >= MAX_NR_EXTERNAL_IOREQ_SERVERS;

You cannot ASSERT() here.  id is guest-controlled data in the dm_op() path.

~Andrew

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

#9 lore raw

Re: [Xen-devel] [PATCH v3 04/10] ioreq: add internal ioreq initialization support

Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Roger Pau Monne
xen/include/asm-x86/hvm/ioreq.h +0 -0
index 65491c48d2..c3917aa74d 100644
@@ -54,6 +54,12 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);
 
Quoted 8 lines

xen/include/asm-x86/hvm/ioreq.h unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered); · line 60

Quoted 6 lines
xen/include/asm-x86/hvm/ioreq.h +4 -0
 void hvm_ioreq_init(struct domain *d);
 
+static inline bool hvm_ioreq_is_internal(unsigned int id)
+{
+    ASSERT(id < MAX_NR_IOREQ_SERVERS);
+    return id >= MAX_NR_EXTERNAL_IOREQ_SERVERS;

You cannot ASSERT() here.  id is guest-controlled data in the dm_op() path.

Urg, right, thanks for noticing. There's no check prior to calling hvm_ioreq_is_internal on the dm_op path.

I guess just returning true if id >= MAX_NR_EXTERNAL_IOREQ_SERVERS would be OK, get_ioreq_server already copes with overflowing ids.

Thanks, Roger.

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

#10 lore raw

Re: [Xen-devel] [PATCH v3 04/10] ioreq: add internal ioreq initialization support

In reply to Roger Pau Monne

xen/arch/x86/hvm/ioreq.c int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) · line 887

Quoted 5 lines · Roger Pau Monne
xen/arch/x86/hvm/ioreq.c +1 -0
@@ -855,6 +884,8 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id)
     struct hvm_ioreq_server *s;
     int rc;
 
+    ASSERT(!hvm_ioreq_is_internal(id));

With this, ...

xen/arch/x86/hvm/ioreq.c int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) · line 906

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -1
@@ -871,13 +903,13 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id)
 
     p2m_set_ioreq_server(d, 0, id);
 
-    hvm_ioreq_server_disable(s);
+    hvm_ioreq_server_disable(s, hvm_ioreq_is_internal(id));

... why not simply "false" here?

xen/arch/x86/hvm/ioreq.c int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) · line 912

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -1
     /*
      * It is safe to call hvm_ioreq_server_deinit() prior to
      * set_ioreq_server() since the target domain is paused.
      */
-    hvm_ioreq_server_deinit(s);
+    hvm_ioreq_server_deinit(s, false);

The more that here you do so.

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +2 -0
@@ -900,6 +932,8 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,
     struct hvm_ioreq_server *s;
     int rc;
 
+    ASSERT(!hvm_ioreq_is_internal(id));
+
     spin_lock_recursive(&d->arch.hvm.ioreq_server.lock);
 
     s = get_ioreq_server(d, id);

xen/arch/x86/hvm/ioreq.c int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id, · line 948

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -0
@@ -909,6 +943,7 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,
         goto out;
 
     rc = -EPERM;
+    /* NB: don't allow fetching information from internal ioreq servers. */
     if ( s->emulator != current->domain )
         goto out;

The comment doesn't really seem to be applicable to the code here:
->emulator lives in the "external" part of the union, and hence if anywhere I think the comment should go next to the ASSERT() above.

Quoted 19 lines
xen/arch/x86/hvm/ioreq.c +2 -2
@@ -1010,7 +1045,7 @@ int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
         goto out;
 
     rc = -EPERM;
-    if ( s->emulator != current->domain )
+    if ( !hvm_ioreq_is_internal(id) && s->emulator != current->domain )
         goto out;
 
     switch ( type )
@@ -1062,7 +1097,7 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
         goto out;
 
     rc = -EPERM;
-    if ( s->emulator != current->domain )
+    if ( !hvm_ioreq_is_internal(id) && s->emulator != current->domain )
         goto out;
 
     switch ( type )

xen/arch/x86/hvm/ioreq.c int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id, · line 1149

Quoted 7 lines
xen/arch/x86/hvm/ioreq.c +2 -0
@@ -1108,6 +1143,8 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,
     struct hvm_ioreq_server *s;
     int rc;
 
+    ASSERT(!hvm_ioreq_is_internal(id));
+
     if ( type != HVMMEM_ioreq_server )
         return -EINVAL;

Taking just these three, things seem pretty inconsistent: Why ASSERT() here but if() above? I think it would be better if dm.c was left unchanged (not sure if I'm in opposition with this to prior review comments by someone else), in particular making it unnecessary (as it seems) to expose hvm_ioreq_is_internal() outside of this CU.

xen/arch/x86/hvm/ioreq.c int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v) · line 1224

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -1
@@ -1184,7 +1221,7 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v)
 
     spin_lock_recursive(&d->arch.hvm.ioreq_server.lock);
 
-    FOR_EACH_IOREQ_SERVER(d, id, s)
+    FOR_EACH_EXTERNAL_IOREQ_SERVER(d, id, s)

Still remembering the error path fix you likely spotted as necessary while doing this work (commit 215f2576b0): Don't you need to again adjust this same error path here (MAX_NR_IOREQ_SERVERS -> MAX_NR_EXTERNAL_IOREQ_SERVERS)?

Jan

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

#11 lore raw

PATCH v3 5/10 ioreq: allow dispatching ioreqs to internal servers

In reply to Roger Pau Monne

Internal ioreq servers will be processed first due to the implementation of FOR_EACH_IOREQ_SERVER, and ioreqs are dispatched simply by calling the handler function.

---
Changes since v2:
- Have a single condition for buffered ioreqs.

Changes since v1:
- Avoid having to iterate twice over the list of ioreq servers since now internal servers are always processed first by FOR_EACH_IOREQ_SERVER.
- Obtain ioreq server id using pointer arithmetic.

---
 xen/arch/x86/hvm/ioreq.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/ioreq.c +10 -1
index cdbd4244a4..0649b7e02d 100644
@@ -1482,7 +1482,16 @@ int hvm_send_ioreq(ioservid_t id, ioreq_t *proto_p, bool buffered)
     ASSERT(s);
 
     if ( buffered )
-        return hvm_send_buffered_ioreq(s, proto_p);
+    {
+        if ( likely(!hvm_ioreq_is_internal(id)) )
+            return hvm_send_buffered_ioreq(s, proto_p);
+
+        ASSERT_UNREACHABLE();
+        return X86EMUL_UNHANDLEABLE;
+    }
+
+    if ( hvm_ioreq_is_internal(id) )
+        return s->handler(proto_p, s->data);
 
     if ( unlikely(!vcpu_start_shutdown_deferral(curr)) )
         return X86EMUL_RETRY;
Signature · 7 lines
2.23.0


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

Re: [Xen-devel] [PATCH v3 05/10] ioreq: allow dispatching ioreqs to internal servers

Quoted 10 lines · Roger Pau Monne
xen/arch/x86/hvm/ioreq.c +3 -1
@@ -1482,7 +1482,16 @@ int hvm_send_ioreq(ioservid_t id, ioreq_t *proto_p, bool buffered)
     ASSERT(s);
 
     if ( buffered )
-        return hvm_send_buffered_ioreq(s, proto_p);
+    {
+        if ( likely(!hvm_ioreq_is_internal(id)) )
+            return hvm_send_buffered_ioreq(s, proto_p);

xen/arch/x86/hvm/ioreq.c int hvm_send_ioreq(ioservid_t id, ioreq_t *proto_p, bool buffered) · line 1497

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +7 -0
+
+        ASSERT_UNREACHABLE();
+        return X86EMUL_UNHANDLEABLE;
+    }
+
+    if ( hvm_ioreq_is_internal(id) )
+        return s->handler(proto_p, s->data);
 
     if ( unlikely(!vcpu_start_shutdown_deferral(curr)) )
         return X86EMUL_RETRY;

Wouldn't it be better to handle internal ones first, before even looking at the "buffered" input? The difference between buffered and non-buffered for external ones is whether to wait for a reply iirc; such a difference simply doesn't exist for internal ones.

Jan

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

#13 lore raw

PATCH v3 6/10 ioreq: allow registering internal ioreq server handler

In reply to Roger Pau Monne

Provide a routine to register the handler for an internal ioreq server.

---
Changes since v2:
- s/hvm_add_ioreq_handler/hvm_set_ioreq_handler.
- Do not goto the out label if ioreq is not internal.

Changes since v1:
- Allow to provide an opaque data parameter to pass to the handler.
- Allow changing the handler as long as the server is not enabled.

---
 xen/arch/x86/hvm/ioreq.c        | 32 ++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/ioreq.h |  4 ++++
 2 files changed, 36 insertions(+)
xen/arch/x86/hvm/ioreq.c +32 -0
index 0649b7e02d..57719c607c 100644
@@ -485,6 +485,38 @@ static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     return rc;
 }
 
+int hvm_set_ioreq_handler(struct domain *d, ioservid_t id,
+                          int (*handler)(ioreq_t *, void *),
+                          void *data)
+{
+    struct hvm_ioreq_server *s;
+    int rc = 0;
+
+    if ( !hvm_ioreq_is_internal(id) )
+        return -EINVAL;
+
+    spin_lock_recursive(&d->arch.hvm.ioreq_server.lock);
+    s = get_ioreq_server(d, id);
+    if ( !s )
+    {
+        rc = -ENOENT;
+        goto out;
+    }
+    if ( s->enabled )
+    {
+        rc = -EBUSY;
+        goto out;
+    }
+
+    s->handler = handler;
+    s->data = data;
+
+ out:
+    spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock);
+
+    return rc;
+}
+
 static void hvm_update_ioreq_evtchn(struct hvm_ioreq_server *s,
                                     struct hvm_ioreq_vcpu *sv)
 {
xen/include/asm-x86/hvm/ioreq.h +4 -0
index c3917aa74d..bfd2b9925e 100644
@@ -54,6 +54,10 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);
 
 void hvm_ioreq_init(struct domain *d);
 
+int hvm_set_ioreq_handler(struct domain *d, ioservid_t id,
+                          int (*handler)(ioreq_t *, void *),
+                          void *data);
+
 static inline bool hvm_ioreq_is_internal(unsigned int id)
 {
     ASSERT(id < MAX_NR_IOREQ_SERVERS);
Signature · 7 lines
2.23.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v3 6/10
#14 lore raw

Re: [Xen-devel] [PATCH v3 06/10] ioreq: allow registering internal ioreq server handler

Quoted 8 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Roger Pau Monne
Sent: 30 September 2019 14:33
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Paul Durrant <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Roger Pau Monne <[email protected]>
Subject: [Xen-devel] [PATCH v3 06/10] ioreq: allow registering internal ioreq server handler

Quoted 4 lines

Provide a routine to register the handler for an internal ioreq server.

Quoted 75 lines

---
Changes since v2:
- s/hvm_add_ioreq_handler/hvm_set_ioreq_handler.
- Do not goto the out label if ioreq is not internal.

Changes since v1:
- Allow to provide an opaque data parameter to pass to the handler.
- Allow changing the handler as long as the server is not enabled.

---
 xen/arch/x86/hvm/ioreq.c        | 32 ++++++++++++++++++++++++++++++++
 xen/include/asm-x86/hvm/ioreq.h |  4 ++++
 2 files changed, 36 insertions(+)
xen/arch/x86/hvm/ioreq.c +32 -0
index 0649b7e02d..57719c607c 100644
@@ -485,6 +485,38 @@ static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     return rc;
 }
 
+int hvm_set_ioreq_handler(struct domain *d, ioservid_t id,
+                          int (*handler)(ioreq_t *, void *),
+                          void *data)
+{
+    struct hvm_ioreq_server *s;
+    int rc = 0;
+
+    if ( !hvm_ioreq_is_internal(id) )
+        return -EINVAL;
+
+    spin_lock_recursive(&d->arch.hvm.ioreq_server.lock);
+    s = get_ioreq_server(d, id);
+    if ( !s )
+    {
+        rc = -ENOENT;
+        goto out;
+    }
+    if ( s->enabled )
+    {
+        rc = -EBUSY;
+        goto out;
+    }
+
+    s->handler = handler;
+    s->data = data;
+
+ out:
+    spin_unlock_recursive(&d->arch.hvm.ioreq_server.lock);
+
+    return rc;
+}
+
 static void hvm_update_ioreq_evtchn(struct hvm_ioreq_server *s,
                                     struct hvm_ioreq_vcpu *sv)
 {
xen/include/asm-x86/hvm/ioreq.h +4 -1
index c3917aa74d..bfd2b9925e 100644
@@ -54,6 +54,10 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered);
 
 void hvm_ioreq_init(struct domain *d);
 
+int hvm_set_ioreq_handler(struct domain *d, ioservid_t id,
+                          int (*handler)(ioreq_t *, void *),
+                          void *data);
+
 static inline bool hvm_ioreq_is_internal(unsigned int id)
 {
     ASSERT(id < MAX_NR_IOREQ_SERVERS);
--

2.23.0

Quoted 4 lines

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

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

#15 lore raw

Re: [Xen-devel] [PATCH v3 06/10] ioreq: allow registering internal ioreq server handler

In reply to Roger Pau Monne
Quoted 5 lines · Roger Pau Monne
xen/arch/x86/hvm/ioreq.c +0 -0
@@ -485,6 +485,38 @@ static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     return rc;
 }

xen/arch/x86/hvm/ioreq.c static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) · line 496

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +9 -0
 
+int hvm_set_ioreq_handler(struct domain *d, ioservid_t id,
+                          int (*handler)(ioreq_t *, void *),
+                          void *data)
+{
+    struct hvm_ioreq_server *s;
+    int rc = 0;
+
+    if ( !hvm_ioreq_is_internal(id) )
+        return -EINVAL;

Isn't BUG_ON() more applicable here?

Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +5 -0
+    spin_lock_recursive(&d->arch.hvm.ioreq_server.lock);
+    s = get_ioreq_server(d, id);
+    if ( !s )
+    {
+        rc = -ENOENT;

xen/arch/x86/hvm/ioreq.c static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) · line 512

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+        goto out;
+    }
+    if ( s->enabled )
+    {
+        rc = -EBUSY;
+        goto out;
+    }
+
+    s->handler = handler;
+    s->data = data;

Is it really intended to blindly replace a disabled handler? Wouldn't the s->enabled check better be evaluating s->handler?

Jan

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

#16 lore raw

PATCH v3 7/10 ioreq: allow decoding accesses to MMCFG regions

In reply to Roger Pau Monne

Pick up on the infrastructure already added for vPCI and allow ioreq to decode accesses to MMCFG regions registered for a domain. This infrastructure is still only accessible from internal callers, so MMCFG regions can only be registered from the internal domain builder used by PVH dom0.

Note that the vPCI infrastructure to decode and handle accesses to MMCFG regions will be removed in following patches when vPCI is switched to become an internal ioreq server.

---
Changes since v2:
- Don't prevent mapping MCFG ranges by ioreq servers.

Changes since v1:
- Remove prototype for destroy_vpci_mmcfg.
- Keep the code in io.c so PCI accesses to MMCFG regions can be decoded before ioreq processing.

---
 xen/arch/x86/hvm/dom0_build.c       |  8 +--
 xen/arch/x86/hvm/hvm.c              |  2 +-
 xen/arch/x86/hvm/io.c               | 79 ++++++++++++-----------------
 xen/arch/x86/hvm/ioreq.c            | 18 +++++--
 xen/arch/x86/physdev.c              |  5 +-
 xen/drivers/passthrough/x86/iommu.c |  2 +-
 xen/include/asm-x86/hvm/io.h        | 29 ++++++++---
 7 files changed, 75 insertions(+), 68 deletions(-)
xen/arch/x86/hvm/dom0_build.c +4 -4
index 831325150b..b30042d8f3 100644
@@ -1108,10 +1108,10 @@ static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
 
     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);
+        rc = hvm_register_mmcfg(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,
xen/arch/x86/hvm/hvm.c +1 -1
index c22cb39cf3..5348186c0c 100644
@@ -753,7 +753,7 @@ void hvm_domain_destroy(struct domain *d)
         xfree(ioport);
     }
 
-    destroy_vpci_mmcfg(d);
+    hvm_free_mmcfg(d);
 }
 
 static int hvm_save_tsc_adjust(struct vcpu *v, hvm_domain_context_t *h)
xen/arch/x86/hvm/io.c +33 -46

146 lines not on this page · raw message

xen/arch/x86/hvm/ioreq.c +13 -5
index 57719c607c..6b87a55db5 100644
@@ -1326,27 +1326,34 @@ ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p)
     uint8_t type;
     uint64_t addr;
     unsigned int id;
+    const struct hvm_mmcfg *mmcfg;
 
     if ( p->type != IOREQ_TYPE_COPY && p->type != IOREQ_TYPE_PIO )
         return XEN_INVALID_IOSERVID;
 
     cf8 = d->arch.hvm.pci_cf8;
 
-    if ( p->type == IOREQ_TYPE_PIO &&
-         (p->addr & ~3) == 0xcfc &&
-         CF8_ENABLED(cf8) )
+    read_lock(&d->arch.hvm.mmcfg_lock);
+    if ( (p->type == IOREQ_TYPE_PIO &&
+          (p->addr & ~3) == 0xcfc &&
+          CF8_ENABLED(cf8)) ||
+         (p->type == IOREQ_TYPE_COPY &&
+          (mmcfg = hvm_mmcfg_find(d, p->addr)) != NULL) )
     {
         uint32_t x86_fam;
         pci_sbdf_t sbdf;
         unsigned int reg;
 
-        reg = hvm_pci_decode_addr(cf8, p->addr, &sbdf);
+        reg = p->type == IOREQ_TYPE_PIO ? hvm_pci_decode_addr(cf8, p->addr,
+                                                              &sbdf)
+                                        : hvm_mmcfg_decode_addr(mmcfg, p->addr,
+                                                                &sbdf);
 
         /* PCI config data cycle */
         type = XEN_DMOP_IO_RANGE_PCI;
         addr = ((uint64_t)sbdf.sbdf << 32) | reg;
         /* AMD extended configuration space access? */
-        if ( CF8_ADDR_HI(cf8) &&
+        if ( p->type == IOREQ_TYPE_PIO && CF8_ADDR_HI(cf8) &&
              d->arch.cpuid->x86_vendor == X86_VENDOR_AMD &&
              (x86_fam = get_cpu_family(
                  d->arch.cpuid->basic.raw_fms, NULL, NULL)) > 0x10 &&
@@ -1365,6 +1372,7 @@ ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p)
                 XEN_DMOP_IO_RANGE_PORT : XEN_DMOP_IO_RANGE_MEMORY;
         addr = p->addr;
     }
+    read_unlock(&d->arch.hvm.mmcfg_lock);
 
     FOR_EACH_IOREQ_SERVER(d, id, s)
     {
xen/arch/x86/physdev.c +2 -3
index 3a3c15890b..f61f66df5f 100644
@@ -562,9 +562,8 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
              * 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);
+            ret = hvm_register_mmcfg(currd, info.address, info.start_bus,
+                                     info.end_bus, info.segment);
         }
 
         break;
xen/drivers/passthrough/x86/iommu.c +1 -1
index 59905629e1..53cdbb45f0 100644
@@ -152,7 +152,7 @@ static bool __hwdom_init hwdom_iommu_map(const struct domain *d,
      * TODO: runtime added MMCFG regions are not checked to make sure they
      * don't overlap with already mapped regions, thus preventing trapping.
      */
-    if ( has_vpci(d) && vpci_is_mmcfg_address(d, pfn_to_paddr(pfn)) )
+    if ( has_vpci(d) && hvm_is_mmcfg_address(d, pfn_to_paddr(pfn)) )
         return false;
 
     return true;
xen/include/asm-x86/hvm/io.h +21 -8
index 7ceb119b64..86ebbd1e7e 100644
@@ -165,9 +165,19 @@ void stdvga_deinit(struct domain *d);
 
 extern void hvm_dpci_msi_eoi(struct domain *d, int vector);
 
-/* Decode a PCI port IO access into a bus/slot/func/reg. */
+struct hvm_mmcfg {
+    struct list_head next;
+    paddr_t addr;
+    unsigned int size;
+    uint16_t segment;
+    uint8_t start_bus;
+};
+
+/* Decode a PCI port IO or MMCFG access into a bus/slot/func/reg. */
 unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
                                  pci_sbdf_t *sbdf);
+unsigned int hvm_mmcfg_decode_addr(const struct hvm_mmcfg *mmcfg,
+                                   paddr_t addr, pci_sbdf_t *sbdf);
 
 /*
  * HVM port IO handler that performs forwarding of guest IO ports into machine
@@ -178,15 +188,18 @@ 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 domain *d);
+/* HVM PCI MMCFG regions registration. */
+int hvm_register_mmcfg(struct domain *d, paddr_t addr,
+                       unsigned int start_bus, unsigned int end_bus,
+                       unsigned int seg);
+void hvm_free_mmcfg(struct domain *d);
+const struct hvm_mmcfg *hvm_mmcfg_find(const struct domain *d, paddr_t addr);
 
 /* Check if an address is between a MMCFG region for a domain. */
-bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr);
+static inline bool hvm_is_mmcfg_address(const struct domain *d, paddr_t addr)
+{
+    return hvm_mmcfg_find(d, addr);
+}
 
 #endif /* __ASM_X86_HVM_IO_H__ */
 
Signature · 7 lines
2.23.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v3 7/10
#17 lore raw

Re: [Xen-devel] [PATCH v3 07/10] ioreq: allow decoding accesses to MMCFG regions

Quoted 14 lines

-----Original Message-----
From: Xen-devel <[email protected]> On Behalf Of Roger Pau Monne
Sent: 30 September 2019 14:33
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Paul Durrant <[email protected]>; Wei Liu <[email protected]>; Jan Beulich <[email protected]>; Roger Pau Monne <[email protected]>
Subject: [Xen-devel] [PATCH v3 07/10] ioreq: allow decoding accesses to MMCFG regions

Pick up on the infrastructure already added for vPCI and allow ioreq to decode accesses to MMCFG regions registered for a domain. This infrastructure is still only accessible from internal callers, so MMCFG regions can only be registered from the internal domain builder used by PVH dom0.

Quoted 5 lines

Note that the vPCI infrastructure to decode and handle accesses to MMCFG regions will be removed in following patches when vPCI is switched to become an internal ioreq server.

...with one nit below...

Quoted 60 lines

---
Changes since v2:
- Don't prevent mapping MCFG ranges by ioreq servers.

Changes since v1:
- Remove prototype for destroy_vpci_mmcfg.
- Keep the code in io.c so PCI accesses to MMCFG regions can be decoded before ioreq processing.

---
 xen/arch/x86/hvm/dom0_build.c       |  8 +--
 xen/arch/x86/hvm/hvm.c              |  2 +-
 xen/arch/x86/hvm/io.c               | 79 ++++++++++++-----------------
 xen/arch/x86/hvm/ioreq.c            | 18 +++++--
 xen/arch/x86/physdev.c              |  5 +-
 xen/drivers/passthrough/x86/iommu.c |  2 +-
 xen/include/asm-x86/hvm/io.h        | 29 ++++++++---
 7 files changed, 75 insertions(+), 68 deletions(-)
xen/arch/x86/hvm/dom0_build.c +4 -4
index 831325150b..b30042d8f3 100644
@@ -1108,10 +1108,10 @@ static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
 
     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);
+        rc = hvm_register_mmcfg(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,
xen/arch/x86/hvm/hvm.c +1 -1
index c22cb39cf3..5348186c0c 100644
@@ -753,7 +753,7 @@ void hvm_domain_destroy(struct domain *d)
         xfree(ioport);
     }
 
-    destroy_vpci_mmcfg(d);
+    hvm_free_mmcfg(d);
 }
 
 static int hvm_save_tsc_adjust(struct vcpu *v, hvm_domain_context_t *h)
xen/arch/x86/hvm/io.c +2 -0
index a5b0a23f06..3334888136 100644
@@ -279,6 +279,18 @@ unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
     return CF8_ADDR_LO(cf8) | (addr & 3);
 }
 
+unsigned int hvm_mmcfg_decode_addr(const struct hvm_mmcfg *mmcfg,
+                                   paddr_t addr, pci_sbdf_t *sbdf)

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

Quoted 10 lines
xen/arch/x86/hvm/io.c +10 -0
+{
+    addr -= mmcfg->addr;
+    sbdf->bdf = MMCFG_BDF(addr);
+    sbdf->bus += mmcfg->start_bus;
+    sbdf->seg = mmcfg->segment;
+
+    return addr & (PCI_CFG_SPACE_EXP_SIZE - 1);
+}
+
+

Extraneous blank line here.

Paul

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

#18 lore raw

Re: [Xen-devel] [PATCH v3 07/10] ioreq: allow decoding accesses to MMCFG regions

In reply to Roger Pau Monne

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

Quoted 10 lines · Roger Pau Monne
xen/arch/x86/hvm/io.c +4 -0
@@ -279,6 +279,18 @@ unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
     return CF8_ADDR_LO(cf8) | (addr & 3);
 }
 
+unsigned int hvm_mmcfg_decode_addr(const struct hvm_mmcfg *mmcfg,
+                                   paddr_t addr, pci_sbdf_t *sbdf)
+{
+    addr -= mmcfg->addr;

As the function isn't even static, wouldn't it be helpful to at least assert that addr >= mmcfg->addr ahead of this, and addr < mmcfg->size afterwards?

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

Quoted 10 lines
xen/arch/x86/hvm/io.c +6 -0
@@ -574,6 +550,17 @@ void destroy_vpci_mmcfg(struct domain *d)
     write_unlock(&d->arch.hvm.mmcfg_lock);
 }
 
+const struct hvm_mmcfg *hvm_mmcfg_find(const struct domain *d, paddr_t addr)
+{
+    const struct hvm_mmcfg *mmcfg;
+
+    list_for_each_entry ( mmcfg, &d->arch.hvm.mmcfg_regions, next )
+        if ( addr >= mmcfg->addr && addr < mmcfg->addr + mmcfg->size )

As a minor remark, this could be coded without even a theoretical risk of overflow as

        if ( addr >= mmcfg->addr && addr - mmcfg->addr < mmcfg->size )
Quoted 22 lines
xen/arch/x86/hvm/ioreq.c +7 -3
@@ -1326,27 +1326,34 @@ ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p)
     uint8_t type;
     uint64_t addr;
     unsigned int id;
+    const struct hvm_mmcfg *mmcfg;
 
     if ( p->type != IOREQ_TYPE_COPY && p->type != IOREQ_TYPE_PIO )
         return XEN_INVALID_IOSERVID;
 
     cf8 = d->arch.hvm.pci_cf8;
 
-    if ( p->type == IOREQ_TYPE_PIO &&
-         (p->addr & ~3) == 0xcfc &&
-         CF8_ENABLED(cf8) )
+    read_lock(&d->arch.hvm.mmcfg_lock);
+    if ( (p->type == IOREQ_TYPE_PIO &&
+          (p->addr & ~3) == 0xcfc &&
+          CF8_ENABLED(cf8)) ||
+         (p->type == IOREQ_TYPE_COPY &&
+          (mmcfg = hvm_mmcfg_find(d, p->addr)) != NULL) )

xen/arch/x86/hvm/ioreq.c ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p) · line 1350

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +4 -1
     {
         uint32_t x86_fam;
         pci_sbdf_t sbdf;
         unsigned int reg;
 
-        reg = hvm_pci_decode_addr(cf8, p->addr, &sbdf);
+        reg = p->type == IOREQ_TYPE_PIO ? hvm_pci_decode_addr(cf8, p->addr,
+                                                              &sbdf)
+                                        : hvm_mmcfg_decode_addr(mmcfg, p->addr,
+                                                                &sbdf);

I'm afraid old gcc will consider this use of mmcfg "potentially uninitialized". I.e. I think the variable wants to gain a NULL initializer, at which point you can slightly shorten the above to

        reg = mmcfg ? hvm_mmcfg_decode_addr(mmcfg, p->addr, &sbdf)
                    : hvm_pci_decode_addr(cf8, p->addr, &sbdf);

also eliminating a pointer deref (i.e. likely producing minimally more efficient code).

xen/arch/x86/hvm/ioreq.c ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p) · line 1356

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -1
         /* PCI config data cycle */
         type = XEN_DMOP_IO_RANGE_PCI;
         addr = ((uint64_t)sbdf.sbdf << 32) | reg;
         /* AMD extended configuration space access? */
-        if ( CF8_ADDR_HI(cf8) &&
+        if ( p->type == IOREQ_TYPE_PIO && CF8_ADDR_HI(cf8) &&

Would be "!mmcfg && ..." then here.

Quoted 10 lines
xen/include/asm-x86/hvm/io.h +3 -1
@@ -165,9 +165,19 @@ void stdvga_deinit(struct domain *d);
 
 extern void hvm_dpci_msi_eoi(struct domain *d, int vector);
 
-/* Decode a PCI port IO access into a bus/slot/func/reg. */
+struct hvm_mmcfg {
+    struct list_head next;
+    paddr_t addr;

xen/include/asm-x86/hvm/io.h void stdvga_deinit(struct domain *d); · line 180

Quoted 10 lines
xen/include/asm-x86/hvm/io.h +8 -0
+    unsigned int size;
+    uint16_t segment;
+    uint8_t start_bus;
+};
+
+/* Decode a PCI port IO or MMCFG access into a bus/slot/func/reg. */
 unsigned int hvm_pci_decode_addr(unsigned int cf8, unsigned int addr,
                                  pci_sbdf_t *sbdf);
+unsigned int hvm_mmcfg_decode_addr(const struct hvm_mmcfg *mmcfg,
+                                   paddr_t addr, pci_sbdf_t *sbdf);

The latest now the comment also wants to include "seg/", I think.

Quoted 4 lines
xen/include/asm-x86/hvm/io.h +0 -0
@@ -178,15 +188,18 @@ void register_g2m_portio_handler(struct domain *d);
 /* HVM port IO handler for vPCI accesses. */
 void register_vpci_portio_handler(struct domain *d);
 

xen/include/asm-x86/hvm/io.h void register_g2m_portio_handler(struct domain *d); · line 194

Quoted 10 lines
xen/include/asm-x86/hvm/io.h +4 -6
-/* 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 domain *d);
+/* HVM PCI MMCFG regions registration. */
+int hvm_register_mmcfg(struct domain *d, paddr_t addr,
+                       unsigned int start_bus, unsigned int end_bus,
+                       unsigned int seg);

As - from a hierarchy perspective - the segment is more significant, can you put it ahead of the start/end bus numbers please?

xen/include/asm-x86/hvm/io.h void register_g2m_portio_handler(struct domain *d); · line 202

Quoted 9 lines
xen/include/asm-x86/hvm/io.h +6 -1
+void hvm_free_mmcfg(struct domain *d);
+const struct hvm_mmcfg *hvm_mmcfg_find(const struct domain *d, paddr_t addr);
 
 /* Check if an address is between a MMCFG region for a domain. */
-bool vpci_is_mmcfg_address(const struct domain *d, paddr_t addr);
+static inline bool hvm_is_mmcfg_address(const struct domain *d, paddr_t addr)
+{
+    return hvm_mmcfg_find(d, addr);
+}

Hmm, yet another instance where the size of an access doesn't matter in such a predicate. Is it a good idea (or is there a fair reason) to extend this bad practice? But yes, I do see that vpci_mmcfg_accept() has no way to pass a size here, and that you you effectively only move the code here.

Jan

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

#19 lore raw

PATCH v3 8/10 vpci: register as an internal ioreq server

In reply to Roger Pau Monne

Switch vPCI to become an internal ioreq server, and hence drop all the vPCI specific decoding and trapping to PCI IO ports and MMCFG regions.

This allows to unify the vPCI code with the ioreq infrastructure, opening the door for domains having PCI accesses handled by vPCI and other ioreq servers at the same time.

---
Changes since v2:
- Remove stray addition of ioreq header to physdev.c.

Changes since v1:
- Remove prototypes for register_vpci_portio_handler and register_vpci_mmcfg_handler.
- Re-add vpci check in hwdom_iommu_map.
- Fix test harness.
- Remove vpci_{read/write} prototypes and make the functions static.

---
 tools/tests/vpci/Makefile     |   5 +-
 tools/tests/vpci/emul.h       |   4 +
 xen/arch/x86/hvm/dom0_build.c |   1 +
 xen/arch/x86/hvm/hvm.c        |   5 +-
 xen/arch/x86/hvm/io.c         | 201 ----------------------------------
 xen/drivers/vpci/vpci.c       |  63 ++++++++++-
 xen/include/xen/vpci.h        |  22 +---
 7 files changed, 79 insertions(+), 222 deletions(-)
tools/tests/vpci/Makefile +4 -1
index 5075bc2be2..c365c4522a 100644
@@ -25,7 +25,10 @@ install:
 
 vpci.c: $(XEN_ROOT)/xen/drivers/vpci/vpci.c
 	# Remove includes and add the test harness header
-	sed -e '/#include/d' -e '1s/^/#include "emul.h"/' <$< >$@
+	sed -e '/#include/d' -e '1s/^/#include "emul.h"/' \
+	    -e 's/^static uint32_t read/uint32_t vpci_read/' \
+	    -e 's/^static void write/void vpci_write/' <$< >$@
+
 
 list.h: $(XEN_ROOT)/xen/include/xen/list.h
 vpci.h: $(XEN_ROOT)/xen/include/xen/vpci.h
tools/tests/vpci/emul.h +4 -0
index 2e1d3057c9..5a6494a797 100644
@@ -125,6 +125,10 @@ typedef union {
         tx > ty ? tx : ty;              \
 })
 
+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);
+
 #endif
 
 /*
xen/arch/x86/hvm/dom0_build.c +1 -0
index b30042d8f3..dff4d6663c 100644
@@ -29,6 +29,7 @@
 
 #include <asm/bzimage.h>
 #include <asm/dom0_build.h>
+#include <asm/hvm/ioreq.h>
 #include <asm/hvm/support.h>
 #include <asm/io_apic.h>
 #include <asm/p2m.h>
xen/arch/x86/hvm/hvm.c +4 -1
index 5348186c0c..c5c0e3fa2c 100644
@@ -656,10 +656,13 @@ int hvm_domain_initialise(struct domain *d)
         d->arch.hvm.io_bitmap = hvm_io_bitmap;
 
     register_g2m_portio_handler(d);
-    register_vpci_portio_handler(d);
 
     hvm_ioreq_init(d);
 
+    rc = vpci_register_ioreq(d);
+    if ( rc )
+        goto fail1;
+
     hvm_init_guest_time(d);
 
     d->arch.hvm.params[HVM_PARAM_TRIPLE_FAULT_REASON] = SHUTDOWN_reboot;
xen/arch/x86/hvm/io.c +0 -201

213 lines not on this page · raw message

xen/drivers/vpci/vpci.c +60 -3

87 lines not on this page · raw message

xen/include/xen/vpci.h +6 -16
index 5295d4c990..4e9591c020 100644
@@ -23,6 +23,9 @@ typedef int vpci_register_init_t(struct pci_dev *dev);
   static vpci_register_init_t *const x##_entry  \
                __used_section(".data.vpci." p) = x
 
+/* Register vPCI handler with ioreq. */
+int vpci_register_ioreq(struct domain *d);
+
 /* Add vPCI handlers to device. */
 int __must_check vpci_add_handlers(struct pci_dev *dev);
 
@@ -38,11 +41,6 @@ int __must_check vpci_add_register(struct vpci *vpci,
 int __must_check vpci_remove_register(struct vpci *vpci, unsigned int offset,
                                       unsigned int size);
 
-/* Generic read/write handlers for the PCI config space. */
-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);
-
 /* Passthrough handlers. */
 uint32_t vpci_hw_read16(const struct pci_dev *pdev, unsigned int reg,
                         void *data);
@@ -219,20 +217,12 @@ static inline int vpci_add_handlers(struct pci_dev *pdev)
     return 0;
 }
 
-static inline void vpci_dump_msi(void) { }
-
-static inline uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg,
-                                 unsigned int size)
+static inline int vpci_register_ioreq(struct domain *d)
 {
-    ASSERT_UNREACHABLE();
-    return ~(uint32_t)0;
+    return 0;
 }
 
-static inline void vpci_write(pci_sbdf_t sbdf, unsigned int reg,
-                              unsigned int size, uint32_t data)
-{
-    ASSERT_UNREACHABLE();
-}
+static inline void vpci_dump_msi(void) { }
 
 static inline bool vpci_process_pending(struct vcpu *v)
 {
Signature · 7 lines
2.23.0


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

Re: [Xen-devel] [PATCH v3 08/10] vpci: register as an internal ioreq server

xen/arch/x86/hvm/dom0_build.c line 35

Quoted 10 lines · Roger Pau Monne
xen/arch/x86/hvm/dom0_build.c +1 -0
@@ -29,6 +29,7 @@
 
 #include <asm/bzimage.h>
 #include <asm/dom0_build.h>
+#include <asm/hvm/ioreq.h>
 #include <asm/hvm/support.h>
 #include <asm/io_apic.h>
 #include <asm/p2m.h>

This is the only change to this file, and there's no addition to asm/hvm/ioreq.h - how come this #include is needed?

xen/drivers/vpci/vpci.c line 23

Quoted 7 lines
xen/drivers/vpci/vpci.c +1 -0
@@ -20,6 +20,8 @@
 #include <xen/sched.h>
 #include <xen/vpci.h>
 
+#include <asm/hvm/ioreq.h>

This of course means a step away from the code here being generic enough such that Arm could eventually use it. Independent of this aspect perhaps the #include would better move ...

xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 483

Quoted 5 lines
xen/drivers/vpci/vpci.c +1 -0
@@ -478,6 +480,61 @@ void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
     spin_unlock(&pdev->vpci->lock);
 }
 
+#ifdef __XEN__

... here?

xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 493

Quoted 10 lines
xen/drivers/vpci/vpci.c +10 -0
+static int ioreq_handler(ioreq_t *req, void *data)
+{
+    pci_sbdf_t sbdf;
+
+    /*
+     * NB: certain requests of type different than PCI are broadcasted to all
+     * registered ioreq servers, ignored those.
+     */
+    if ( req->type != IOREQ_TYPE_PCI_CONFIG || req->data_is_ptr )
+        return X86EMUL_UNHANDLEABLE;

I understand the left side of the || , but why the right side? There shouldn't be any IOREQ_TYPE_PCI_CONFIG requests with data_is_ptr set, should there? I also didn't think requests with data_is_ptr set would get broadcast?

Quoted 20 lines
xen/drivers/vpci/vpci.c +20 -0
+int vpci_register_ioreq(struct domain *d)
+{
+    ioservid_t id;
+    int rc;
+
+    if ( !has_vpci(d) )
+        return 0;
+
+    rc = hvm_create_ioreq_server(d, HVM_IOREQSRV_BUFIOREQ_OFF, &id, true);
+    if ( rc )
+        return rc;
+
+    rc = hvm_set_ioreq_handler(d, id, ioreq_handler, NULL);
+    if ( rc )
+        return rc;
+
+    if ( is_hardware_domain(d) )
+    {
+        /* Handle all devices in vpci. */
+        rc = hvm_map_io_range_to_ioreq_server(d, id, XEN_DMOP_IO_RANGE_PCI,

xen/drivers/vpci/vpci.c void vpci_write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size, · line 534

Quoted 10 lines
xen/drivers/vpci/vpci.c +10 -0
+                                              0, ~(uint64_t)0);
+        if ( rc )
+            return rc;
+    }
+
+    rc = hvm_set_ioreq_server_state(d, id, true);
+    if ( rc )
+        return rc;
+
+    return rc;

This last sequence of statements looks a little odd - is this in anticipation of further additions to the function?

Furthermore the only caller expects the function to clean up after itself (i.e. partially completed setup be undone), which doesn't look to be the case here. I can't seem to be able to convince myself that all of this gets cleaned up.

Jan

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

#21 lore raw

PATCH v3 9/10 ioreq: split the code to detect PCI config space accesses

In reply to Roger Pau Monne

Place the code that converts a PIO/COPY ioreq into a PCI_CONFIG one into a separate function, and adjust the code to make use of this newly introduced function.

No functional change intended.

---
Changes since v1:
- New in this version.

---
 xen/arch/x86/hvm/ioreq.c | 111 +++++++++++++++++++++++----------------
 1 file changed, 67 insertions(+), 44 deletions(-)
xen/arch/x86/hvm/ioreq.c +67 -44
index 6b87a55db5..f3684fc648 100644
@@ -183,6 +183,54 @@ static bool hvm_wait_for_io(struct hvm_ioreq_vcpu *sv, ioreq_t *p)
     return true;
 }
 
+static void convert_pci_ioreq(struct domain *d, ioreq_t *p)
+{
+    const struct hvm_mmcfg *mmcfg;
+    uint32_t cf8 = d->arch.hvm.pci_cf8;
+
+    if ( p->type != IOREQ_TYPE_PIO && p->type != IOREQ_TYPE_COPY )
+    {
+        ASSERT_UNREACHABLE();
+        return;
+    }
+
+    read_lock(&d->arch.hvm.mmcfg_lock);
+    if ( (p->type == IOREQ_TYPE_PIO &&
+          (p->addr & ~3) == 0xcfc &&
+          CF8_ENABLED(cf8)) ||
+         (p->type == IOREQ_TYPE_COPY &&
+          (mmcfg = hvm_mmcfg_find(d, p->addr)) != NULL) )
+    {
+        uint32_t x86_fam;
+        pci_sbdf_t sbdf;
+        unsigned int reg;
+
+        reg = p->type == IOREQ_TYPE_PIO ? hvm_pci_decode_addr(cf8, p->addr,
+                                                              &sbdf)
+                                        : hvm_mmcfg_decode_addr(mmcfg, p->addr,
+                                                                &sbdf);
+
+        /* PCI config data cycle */
+        p->addr = ((uint64_t)sbdf.sbdf << 32) | reg;
+        /* AMD extended configuration space access? */
+        if ( p->type == IOREQ_TYPE_PIO && CF8_ADDR_HI(cf8) &&
+             d->arch.cpuid->x86_vendor == X86_VENDOR_AMD &&
+             (x86_fam = get_cpu_family(
+                 d->arch.cpuid->basic.raw_fms, NULL, NULL)) > 0x10 &&
+             x86_fam < 0x17 )
+        {
+            uint64_t msr_val;
+
+            if ( !rdmsr_safe(MSR_AMD64_NB_CFG, msr_val) &&
+                 (msr_val & (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT)) )
+                p->addr |= CF8_ADDR_HI(cf8);
+        }
+        p->type = IOREQ_TYPE_PCI_CONFIG;
+
+    }
+    read_unlock(&d->arch.hvm.mmcfg_lock);
+}
+
 bool handle_hvm_io_completion(struct vcpu *v)
 {
     struct domain *d = v->domain;
@@ -1322,57 +1370,36 @@ void hvm_destroy_all_ioreq_servers(struct domain *d)
 ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p)
 {
     struct hvm_ioreq_server *s;
-    uint32_t cf8;
     uint8_t type;
-    uint64_t addr;
     unsigned int id;
-    const struct hvm_mmcfg *mmcfg;
 
     if ( p->type != IOREQ_TYPE_COPY && p->type != IOREQ_TYPE_PIO )
         return XEN_INVALID_IOSERVID;
 
-    cf8 = d->arch.hvm.pci_cf8;
+    /*
+     * Check and convert the PIO/MMIO ioreq to a PCI config space
+     * access.
+     */
+    convert_pci_ioreq(d, p);
 
-    read_lock(&d->arch.hvm.mmcfg_lock);
-    if ( (p->type == IOREQ_TYPE_PIO &&
-          (p->addr & ~3) == 0xcfc &&
-          CF8_ENABLED(cf8)) ||
-         (p->type == IOREQ_TYPE_COPY &&
-          (mmcfg = hvm_mmcfg_find(d, p->addr)) != NULL) )
+    switch ( p->type )
     {
-        uint32_t x86_fam;
-        pci_sbdf_t sbdf;
-        unsigned int reg;
+    case IOREQ_TYPE_PIO:
+        type = XEN_DMOP_IO_RANGE_PORT;
+        break;
 
-        reg = p->type == IOREQ_TYPE_PIO ? hvm_pci_decode_addr(cf8, p->addr,
-                                                              &sbdf)
-                                        : hvm_mmcfg_decode_addr(mmcfg, p->addr,
-                                                                &sbdf);
+    case IOREQ_TYPE_COPY:
+        type = XEN_DMOP_IO_RANGE_MEMORY;
+        break;
 
-        /* PCI config data cycle */
+    case IOREQ_TYPE_PCI_CONFIG:
         type = XEN_DMOP_IO_RANGE_PCI;
-        addr = ((uint64_t)sbdf.sbdf << 32) | reg;
-        /* AMD extended configuration space access? */
-        if ( p->type == IOREQ_TYPE_PIO && CF8_ADDR_HI(cf8) &&
-             d->arch.cpuid->x86_vendor == X86_VENDOR_AMD &&
-             (x86_fam = get_cpu_family(
-                 d->arch.cpuid->basic.raw_fms, NULL, NULL)) > 0x10 &&
-             x86_fam < 0x17 )
-        {
-            uint64_t msr_val;
+        break;
 
-            if ( !rdmsr_safe(MSR_AMD64_NB_CFG, msr_val) &&
-                 (msr_val & (1ULL << AMD64_NB_CFG_CF8_EXT_ENABLE_BIT)) )
-                addr |= CF8_ADDR_HI(cf8);
-        }
-    }
-    else
-    {
-        type = (p->type == IOREQ_TYPE_PIO) ?
-                XEN_DMOP_IO_RANGE_PORT : XEN_DMOP_IO_RANGE_MEMORY;
-        addr = p->addr;
+    default:
+        ASSERT_UNREACHABLE();
+        return XEN_INVALID_IOSERVID;
     }
-    read_unlock(&d->arch.hvm.mmcfg_lock);
 
     FOR_EACH_IOREQ_SERVER(d, id, s)
     {
@@ -1388,7 +1415,7 @@ ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p)
             unsigned long start, end;
 
         case XEN_DMOP_IO_RANGE_PORT:
-            start = addr;
+            start = p->addr;
             end = start + p->size - 1;
             if ( rangeset_contains_range(r, start, end) )
                 return id;
@@ -1405,12 +1432,8 @@ ioservid_t hvm_select_ioreq_server(struct domain *d, ioreq_t *p)
             break;
 
         case XEN_DMOP_IO_RANGE_PCI:
-            if ( rangeset_contains_singleton(r, addr >> 32) )
-            {
-                p->type = IOREQ_TYPE_PCI_CONFIG;
-                p->addr = addr;
+            if ( rangeset_contains_singleton(r, p->addr >> 32) )
                 return id;
-            }
 
             break;
         }
Signature · 7 lines
2.23.0


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

PATCH v3 10/10 ioreq: provide support for long-running operations...

In reply to Roger Pau Monne

...and switch vPCI to use this infrastructure for long running physmap modification operations.

This allows to get rid of the vPCI specific modifications done to handle_hvm_io_completion and allows generalizing the support for long-running operations to other internal ioreq servers. Such support is implemented as a specific handler that can be registers by internal ioreq servers and that will be called to check for pending work. Returning true from this handler will prevent the vcpu from running until the handler returns false.

---
Changes since v2:
- Remove extra newline in vpci_process_pending.
- Continue early in handle_hvm_io_completion in order to avoid one extra level of indentation.
- Switch setting the ioreq state to a ternary conditional operator.

---
 xen/arch/x86/hvm/ioreq.c       | 55 ++++++++++++++++++++++++++-----
 xen/drivers/vpci/header.c      | 60 ++++++++++++++++++----------------
 xen/drivers/vpci/vpci.c        |  9 ++++-
 xen/include/asm-x86/hvm/vcpu.h |  3 +-
 xen/include/xen/vpci.h         |  6 ----
 5 files changed, 89 insertions(+), 44 deletions(-)
xen/arch/x86/hvm/ioreq.c +47 -8
index f3684fc648..78322dfa67 100644
@@ -239,16 +239,48 @@ bool handle_hvm_io_completion(struct vcpu *v)
     enum hvm_io_completion io_completion;
     unsigned int id;
 
-    if ( has_vpci(d) && vpci_process_pending(v) )
-    {
-        raise_softirq(SCHEDULE_SOFTIRQ);
-        return false;
-    }
-
-    FOR_EACH_EXTERNAL_IOREQ_SERVER(d, id, s)
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct hvm_ioreq_vcpu *sv;
 
+        if ( hvm_ioreq_is_internal(id) )
+        {
+            ioreq_t req = vio->io_req;
+
+            if ( vio->io_req.state != STATE_IOREQ_INPROCESS )
+                continue;
+
+            /*
+             * Check and convert the PIO/MMIO ioreq to a PCI config space
+             * access.
+             */
+            convert_pci_ioreq(d, &req);
+
+            if ( s->handler(&req, s->data) == X86EMUL_RETRY )
+            {
+                /*
+                 * Need to raise a scheduler irq in order to prevent the
+                 * guest vcpu from resuming execution.
+                 *
+                 * Note this is not required for external ioreq operations
+                 * because in that case the vcpu is marked as blocked, but
+                 * this cannot be done for long-running internal
+                 * operations, since it would prevent the vcpu from being
+                 * scheduled and thus the long running operation from
+                 * finishing.
+                 */
+                raise_softirq(SCHEDULE_SOFTIRQ);
+                return false;
+            }
+
+            /* Finished processing the ioreq. */
+            vio->io_req.state = hvm_ioreq_needs_completion(&vio->io_req)
+                                ? STATE_IORESP_READY
+                                : STATE_IOREQ_NONE;
+
+            continue;
+        }
+
         list_for_each_entry ( sv,
                               &s->ioreq_vcpu_list,
                               list_entry )
@@ -1554,7 +1586,14 @@ int hvm_send_ioreq(ioservid_t id, ioreq_t *proto_p, bool buffered)
     }
 
     if ( hvm_ioreq_is_internal(id) )
-        return s->handler(proto_p, s->data);
+    {
+        int rc = s->handler(proto_p, s->data);
+
+        if ( rc == X86EMUL_RETRY )
+            curr->arch.hvm.hvm_io.io_req.state = STATE_IOREQ_INPROCESS;
+
+        return rc;
+    }
 
     if ( unlikely(!vcpu_start_shutdown_deferral(curr)) )
         return X86EMUL_RETRY;
xen/drivers/vpci/header.c +32 -28

69 lines not on this page · raw message

xen/drivers/vpci/vpci.c +8 -1
index 206fcadbc6..0cc8543eb8 100644
@@ -484,6 +484,7 @@ static void write(pci_sbdf_t sbdf, unsigned int reg, unsigned int size,
 static int ioreq_handler(ioreq_t *req, void *data)
 {
     pci_sbdf_t sbdf;
+    struct vcpu *curr = current;
 
     /*
      * NB: certain requests of type different than PCI are broadcasted to all
@@ -492,6 +493,12 @@ static int ioreq_handler(ioreq_t *req, void *data)
     if ( req->type != IOREQ_TYPE_PCI_CONFIG || req->data_is_ptr )
         return X86EMUL_UNHANDLEABLE;
 
+    if ( curr->vpci.mem )
+    {
+        ASSERT(req->state == STATE_IOREQ_INPROCESS);
+        return vpci_process_pending(curr) ? X86EMUL_RETRY : X86EMUL_OKAY;
+    }
+
     sbdf.sbdf = req->addr >> 32;
 
     if ( req->dir )
@@ -499,7 +506,7 @@ static int ioreq_handler(ioreq_t *req, void *data)
     else
         write(sbdf, req->addr, req->size, req->data);
 
-    return X86EMUL_OKAY;
+    return curr->vpci.mem ? X86EMUL_RETRY : X86EMUL_OKAY;
 }
 
 int vpci_register_ioreq(struct domain *d)
xen/include/asm-x86/hvm/vcpu.h +2 -1
index 38f5c2bb9b..4563746466 100644
@@ -92,7 +92,8 @@ struct hvm_vcpu_io {
 
 static inline bool hvm_ioreq_needs_completion(const ioreq_t *ioreq)
 {
-    return ioreq->state == STATE_IOREQ_READY &&
+    return (ioreq->state == STATE_IOREQ_READY ||
+            ioreq->state == STATE_IOREQ_INPROCESS) &&
            !ioreq->data_is_ptr &&
            (ioreq->type != IOREQ_TYPE_PIO || ioreq->dir != IOREQ_WRITE);
 }
xen/include/xen/vpci.h +0 -6
index 4e9591c020..bad406b21d 100644
@@ -223,12 +223,6 @@ static inline int vpci_register_ioreq(struct domain *d)
 }
 
 static inline void vpci_dump_msi(void) { }
-
-static inline bool vpci_process_pending(struct vcpu *v)
-{
-    ASSERT_UNREACHABLE();
-    return false;
-}
 #endif
 
 #endif
Signature · 7 lines
2.23.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v3 10/10
#23 lore raw

Re: [Xen-devel] [PATCH v3 10/10] ioreq: provide support for long-running operations...

Quoted 10 lines · Roger Pau Monne

...and switch vPCI to use this infrastructure for long running physmap modification operations.

This allows to get rid of the vPCI specific modifications done to handle_hvm_io_completion and allows generalizing the support for long-running operations to other internal ioreq servers. Such support is implemented as a specific handler that can be registers by internal ioreq servers and that will be called to check for pending work. Returning true from this handler will prevent the vcpu from running until the handler returns false.

Is the mentioning of a special handler stale from perhaps a different earlier implementation? Unless I'm overlooking / misunderstanding something here, with this adjusted ...

Quoted 1 line

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