Skip to contentSkip to conversation

Xen: Use ioreq-server API

PATCH Superseded Paul Durrant 6 participants 46 messages Oct 15, 2014 – Oct 16, 2014

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Xen: Use ioreq-server API

This patch series is v3 of what was originally the single patch "Xen: Use the ioreq-server API when available".

v2 of the series moved the code that added the PCI bus listener to patch #1 and the remainder of the changes to patch #2. Patch #2 was then re-worked to constrain the #ifdefing to xen_common.h, as requested by Stefano.

v3 of the series modifies patch #1 to add the listener interface into the core qdev, rather than the PCI bus code. This change only requires trivial modification to patch #2, to only act on realize/ unrealize of PCI devices. Patch #2 was also modified at Stefano's request to remove an extra identity check of memory sections against the ram region.

#1 lore raw

PATCH v3 1/2 Add device listener interface

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54 +++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/qdev-core.h  |   10 +++++++++
 include/qemu/typedefs.h |    1 +
 3 files changed, 65 insertions(+)
hw/core/qdev.c +54 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL, qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev, Error **errp)
             return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +10 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
@@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler,
                              QDEV_HOTPLUG_HANDLER_PROPERTY, errp);
     bus->allow_hotplug = 1;
 }
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v3 1/2 Add device listener interface

In reply to Paul Durrant

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54 +++++++++++++++++++++++++++++++++++++++++++++++
 include/hw/qdev-core.h  |   10 +++++++++
 include/qemu/typedefs.h |    1 +
 3 files changed, 65 insertions(+)
hw/core/qdev.c +54 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL, qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev, Error **errp)
             return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +10 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass), TYPE_BUS)
@@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler,
                              QDEV_HOTPLUG_HANDLER_PROPERTY, errp);
     bus->allow_hotplug = 1;
 }
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
Signature · 1 line
1.7.10.4
#3 lore raw

Re: [PATCH v3 1/2] Add device listener interface

Quoted 5 lines · On Wed, 15 Oct 2014 10:16:38 +0100 Paul Durrant <[email protected]> wrote:

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

If you need only one listener handler for your case, why you couldn't use hotplug interface instead of listerners?
to me it looks like it should work for this case.
To make it work xen code would need to override default plug/unplug handlers on PCI bus and do register/unregister from there.

One thing is that unplug handler is not defined for PCI bus yet, to get it work one would need to refactor pcihp/bridge/pcie unplug path to call unplug handler before object_unparent().

Quoted 133 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54

+++++++++++++++++++++++++++++++++++++++++++++++
include/hw/qdev-core.h | 10 +++++++++ include/qemu/typedefs.h |
1 + 3 files changed, 65 insertions(+)

hw/core/qdev.c +50 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL,
 qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/core/qdev.c +4 -0
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev,
 Error **errp) return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +6 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),

TYPE_BUS) @@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, QDEV_HOTPLUG_HANDLER_PROPERTY, errp); bus->allow_hotplug = 1;
}

include/hw/qdev-core.h +4 -0
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
#4 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] Add device listener interface

In reply to Paul Durrant
Quoted 5 lines · On Wed, 15 Oct 2014 10:16:38 +0100 Paul Durrant <[email protected]> wrote:

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

If you need only one listener handler for your case, why you couldn't use hotplug interface instead of listerners?
to me it looks like it should work for this case.
To make it work xen code would need to override default plug/unplug handlers on PCI bus and do register/unregister from there.

One thing is that unplug handler is not defined for PCI bus yet, to get it work one would need to refactor pcihp/bridge/pcie unplug path to call unplug handler before object_unparent().

Quoted 133 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54

+++++++++++++++++++++++++++++++++++++++++++++++
include/hw/qdev-core.h | 10 +++++++++ include/qemu/typedefs.h |
1 + 3 files changed, 65 insertions(+)

hw/core/qdev.c +50 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL,
 qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/core/qdev.c +4 -0
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev,
 Error **errp) return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +6 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),

TYPE_BUS) @@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, QDEV_HOTPLUG_HANDLER_PROPERTY, errp); bus->allow_hotplug = 1;
}

include/hw/qdev-core.h +4 -0
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
#5 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] Add device listener interface

Quoted 20 lines

-----Original Message-----
From: Igor Mammedov [mailto:[email protected]]
Sent: 15 October 2014 10:54
To: Paul Durrant
Cc: [email protected]; [email protected]; Michael S. Tsirkin; Andreas Faerber"; Paolo Bonzini; Peter Crosthwaite; Igor Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
Subject: Re: [PATCH v3 1/2] Add device listener interface

Quoted 5 lines · On Wed, 15 Oct 2014 10:16:38 +0100 Paul Durrant <[email protected]> wrote:

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

Quoted 6 lines

If you need only one listener handler for your case, why you couldn't use hotplug interface instead of listerners?
to me it looks like it should work for this case.
To make it work xen code would need to override default plug/unplug handlers on PCI bus and do register/unregister from there.

That sounds a bit ugly. A pci or now qdev listener interface seems more elegant and generally useful.

Quoted 4 lines

One thing is that unplug handler is not defined for PCI bus yet, to get it work one would need to refactor pcihp/bridge/pcie unplug path to call unplug handler before object_unparent().

Yes - that makes it a much more intrusive modification.

Paul

Quoted 134 lines
Quoted 134 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54

+++++++++++++++++++++++++++++++++++++++++++++++
include/hw/qdev-core.h | 10 +++++++++ include/qemu/typedefs.h |
1 + 3 files changed, 65 insertions(+)

hw/core/qdev.c +50 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL,
 qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/core/qdev.c +4 -0
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev,
 Error **errp) return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +6 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)
 #define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),

TYPE_BUS) @@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, QDEV_HOTPLUG_HANDLER_PROPERTY, errp); bus->allow_hotplug = 1;
}

include/hw/qdev-core.h +4 -0
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
#6 lore raw

Re: [PATCH v3 1/2] Add device listener interface

Quoted 27 lines · On Wed, 15 Oct 2014 10:05:32 +0000 Paul Durrant <[email protected]> wrote:
Quoted 20 lines

-----Original Message-----
From: Igor Mammedov [mailto:[email protected]]
Sent: 15 October 2014 10:54
To: Paul Durrant
Cc: [email protected]; [email protected]; Michael S. Tsirkin; Andreas Faerber"; Paolo Bonzini; Peter Crosthwaite; Igor Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
Subject: Re: [PATCH v3 1/2] Add device listener interface

Quoted 5 lines · On Wed, 15 Oct 2014 10:16:38 +0100 Paul Durrant <[email protected]> wrote:

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

Quoted 6 lines

If you need only one listener handler for your case, why you couldn't use hotplug interface instead of listerners?
to me it looks like it should work for this case.
To make it work xen code would need to override default plug/unplug handlers on PCI bus and do register/unregister from there.

Quoted 2 lines

That sounds a bit ugly. A pci or now qdev listener interface seems more elegant and generally useful.

Even if suggested listener is usefull it could be better if handlers had error handling as well, so that errors in callback could be reported up to the caller.

Quoted 7 lines
Quoted 5 lines

One thing is that unplug handler is not defined for PCI bus yet, to get it work one would need to refactor pcihp/bridge/pcie unplug path to call unplug handler before object_unparent().

Yes - that makes it a much more intrusive modification.

It' isn't if you don't consolidate unplug handler from pcihp/bridge/pcie. It would be ~3-4 LOC per each.

Quoted 139 lines

Paul

Quoted 135 lines
Quoted 135 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54

+++++++++++++++++++++++++++++++++++++++++++++++
include/hw/qdev-core.h | 10 +++++++++ include/qemu/typedefs.h |
1 + 3 files changed, 65 insertions(+)

hw/core/qdev.c +50 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL,
 qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/core/qdev.c +4 -0
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev,
 Error **errp) return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +6 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState
 *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)

#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),
TYPE_BUS) @@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, QDEV_HOTPLUG_HANDLER_PROPERTY, errp); bus->allow_hotplug = 1;
}

include/hw/qdev-core.h +4 -0
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
#7 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] Add device listener interface

In reply to Paul Durrant
Quoted 27 lines · On Wed, 15 Oct 2014 10:05:32 +0000 Paul Durrant <[email protected]> wrote:
Quoted 20 lines

-----Original Message-----
From: Igor Mammedov [mailto:[email protected]]
Sent: 15 October 2014 10:54
To: Paul Durrant
Cc: [email protected]; [email protected]; Michael S. Tsirkin; Andreas Faerber"; Paolo Bonzini; Peter Crosthwaite; Igor Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
Subject: Re: [PATCH v3 1/2] Add device listener interface

Quoted 5 lines · On Wed, 15 Oct 2014 10:16:38 +0100 Paul Durrant <[email protected]> wrote:

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

Quoted 6 lines

If you need only one listener handler for your case, why you couldn't use hotplug interface instead of listerners?
to me it looks like it should work for this case.
To make it work xen code would need to override default plug/unplug handlers on PCI bus and do register/unregister from there.

Quoted 2 lines

That sounds a bit ugly. A pci or now qdev listener interface seems more elegant and generally useful.

Even if suggested listener is usefull it could be better if handlers had error handling as well, so that errors in callback could be reported up to the caller.

Quoted 7 lines
Quoted 5 lines

One thing is that unplug handler is not defined for PCI bus yet, to get it work one would need to refactor pcihp/bridge/pcie unplug path to call unplug handler before object_unparent().

Yes - that makes it a much more intrusive modification.

It' isn't if you don't consolidate unplug handler from pcihp/bridge/pcie. It would be ~3-4 LOC per each.

Quoted 139 lines

Paul

Quoted 135 lines
Quoted 135 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54

+++++++++++++++++++++++++++++++++++++++++++++++
include/hw/qdev-core.h | 10 +++++++++ include/qemu/typedefs.h |
1 + 3 files changed, 65 insertions(+)

hw/core/qdev.c +50 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL,
 qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/core/qdev.c +4 -0
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev,
 Error **errp) return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +6 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState
 *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)

#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),
TYPE_BUS) @@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, QDEV_HOTPLUG_HANDLER_PROPERTY, errp); bus->allow_hotplug = 1;
}

include/hw/qdev-core.h +4 -0
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
#8 lore raw

Re: [PATCH v3 1/2] Add device listener interface

Quoted 38 lines

-----Original Message-----
From: Igor Mammedov [mailto:[email protected]]
Sent: 16 October 2014 13:41
To: Paul Durrant
Cc: [email protected]; [email protected]; Michael S. Tsirkin; Andreas Faerber
Subject: Re: [PATCH v3 1/2] Add device listener interface

Quoted 27 lines · On Wed, 15 Oct 2014 10:05:32 +0000 Paul Durrant <[email protected]> wrote:
Quoted 26 lines

-----Original Message-----
From: Igor Mammedov [mailto:[email protected]]
Sent: 15 October 2014 10:54
To: Paul Durrant
Cc: [email protected]; [email protected]; Michael S. Tsirkin; Andreas Faerber"; Paolo Bonzini; Peter Crosthwaite; Igor Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
Subject: Re: [PATCH v3 1/2] Add device listener interface

Quoted 5 lines · On Wed, 15 Oct 2014 10:16:38 +0100 Paul Durrant <[email protected]> wrote:

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

If you need only one listener handler for your case, why you couldn't use hotplug interface instead of listerners?
to me it looks like it should work for this case.
To make it work xen code would need to override default plug/unplug handlers on PCI bus and do register/unregister from there.

Quoted 5 lines
Quoted 2 lines

That sounds a bit ugly. A pci or now qdev listener interface seems more elegant and generally useful.

Even if suggested listener is usefull it could be better if handlers had error handling as well, so that errors in callback could be reported up to the caller.

That's not necessary for current use and not consistent with the memory listener interface. What sort of thing would you envisage the caller doing with such an error?

Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

One thing is that unplug handler is not defined for PCI bus yet, to get it work one would need to refactor pcihp/bridge/pcie unplug path to call unplug handler before object_unparent().

Yes - that makes it a much more intrusive modification.

It' isn't if you don't consolidate unplug handler from pcihp/bridge/pcie. It would be ~3-4 LOC per each.

Ok, but doing specific modifications like that still seems ugly. Also Xen needs to know about all PCI devices, not just hotplugged ones. I know that all PCI devices in QEMU are hotplugged at the moment but if that ever changed it would then break on Xen if I were to choose this method of callback.

Paul

Quoted 139 lines
Quoted 139 lines

Paul

Quoted 135 lines
Quoted 135 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54

+++++++++++++++++++++++++++++++++++++++++++++++
include/hw/qdev-core.h | 10 +++++++++ include/qemu/typedefs.h |
1 + 3 files changed, 65 insertions(+)

hw/core/qdev.c +50 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL,
 qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/core/qdev.c +4 -0
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev,
 Error **errp) return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +6 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState
 *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)

#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),
TYPE_BUS) @@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, QDEV_HOTPLUG_HANDLER_PROPERTY, errp); bus->allow_hotplug = 1;
}

include/hw/qdev-core.h +4 -0
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
#9 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] Add device listener interface

In reply to Igor Mammedov
Quoted 38 lines

-----Original Message-----
From: Igor Mammedov [mailto:[email protected]]
Sent: 16 October 2014 13:41
To: Paul Durrant
Cc: [email protected]; [email protected]; Michael S. Tsirkin; Andreas Faerber
Subject: Re: [PATCH v3 1/2] Add device listener interface

Quoted 27 lines · On Wed, 15 Oct 2014 10:05:32 +0000 Paul Durrant <[email protected]> wrote:
Quoted 26 lines

-----Original Message-----
From: Igor Mammedov [mailto:[email protected]]
Sent: 15 October 2014 10:54
To: Paul Durrant
Cc: [email protected]; [email protected]; Michael S. Tsirkin; Andreas Faerber"; Paolo Bonzini; Peter Crosthwaite; Igor Mammedov; Markus Armbruster; Thomas Huth; Christian Borntraeger
Subject: Re: [PATCH v3 1/2] Add device listener interface

Quoted 5 lines · On Wed, 15 Oct 2014 10:16:38 +0100 Paul Durrant <[email protected]> wrote:

The Xen ioreq-server API, introduced in Xen 4.5, requires that PCI device models explicitly register with Xen for config space accesses. This patch adds a listener interface into qdev-core which can be used by the Xen interface code to monitor for arrival and departure of PCI devices.

If you need only one listener handler for your case, why you couldn't use hotplug interface instead of listerners?
to me it looks like it should work for this case.
To make it work xen code would need to override default plug/unplug handlers on PCI bus and do register/unregister from there.

Quoted 5 lines
Quoted 2 lines

That sounds a bit ugly. A pci or now qdev listener interface seems more elegant and generally useful.

Even if suggested listener is usefull it could be better if handlers had error handling as well, so that errors in callback could be reported up to the caller.

That's not necessary for current use and not consistent with the memory listener interface. What sort of thing would you envisage the caller doing with such an error?

Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

One thing is that unplug handler is not defined for PCI bus yet, to get it work one would need to refactor pcihp/bridge/pcie unplug path to call unplug handler before object_unparent().

Yes - that makes it a much more intrusive modification.

It' isn't if you don't consolidate unplug handler from pcihp/bridge/pcie. It would be ~3-4 LOC per each.

Ok, but doing specific modifications like that still seems ugly. Also Xen needs to know about all PCI devices, not just hotplugged ones. I know that all PCI devices in QEMU are hotplugged at the moment but if that ever changed it would then break on Xen if I were to choose this method of callback.

Paul

Quoted 139 lines
Quoted 139 lines

Paul

Quoted 135 lines
Quoted 135 lines
Cc Michael S. Tsirkin <[email protected]>
Cc Andreas Faerber" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Peter Crosthwaite <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Markus Armbruster <[email protected]>
Cc Thomas Huth <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 hw/core/qdev.c          |   54

+++++++++++++++++++++++++++++++++++++++++++++++
include/hw/qdev-core.h | 10 +++++++++ include/qemu/typedefs.h |
1 + 3 files changed, 65 insertions(+)

hw/core/qdev.c +50 -0
index fcb1638..4a9c1f6 100644
@@ -175,6 +175,56 @@ int qdev_init(DeviceState *dev)
     return 0;
 }
 
+static QTAILQ_HEAD(qdev_listeners, DeviceListener) qdev_listeners
+    = QTAILQ_HEAD_INITIALIZER(qdev_listeners);
+
+enum ListenerDirection { Forward, Reverse };
+
+#define QDEV_LISTENER_CALL(_callback, _direction, _args...)     \
+    do {                                                        \
+        DeviceListener *_listener;                              \
+                                                                \
+        switch (_direction) {                                   \
+        case Forward:                                           \
+            QTAILQ_FOREACH(_listener, &qdev_listeners, link) {  \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        case Reverse:                                           \
+            QTAILQ_FOREACH_REVERSE(_listener, &qdev_listeners,  \
+                                   qdev_listeners, link) {      \
+                if (_listener->_callback) {                     \
+                    _listener->_callback(_listener, ##_args);   \
+                }                                               \
+            }                                                   \
+            break;                                              \
+        default:                                                \
+            abort();                                            \
+        }                                                       \
+    } while (0)
+
+static int qdev_listener_add(DeviceState *dev, void *opaque)
+{
+    QDEV_LISTENER_CALL(realize, Forward, dev);
+
+    return 0;
+}
+
+void qdev_listener_register(DeviceListener *listener)
+{
+    QTAILQ_INSERT_TAIL(&qdev_listeners, listener, link);
+
+    qbus_walk_children(sysbus_get_default(), NULL, NULL,
 qdev_listener_add,
+                       NULL, NULL);
+}
+
+void qdev_listener_unregister(DeviceListener *listener)
+{
+    QTAILQ_REMOVE(&qdev_listeners, listener, link);
+}
+
 static void device_realize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
hw/core/qdev.c +4 -0
@@ -186,12 +236,16 @@ static void device_realize(DeviceState *dev,
 Error **errp) return;
         }
     }
+
+    QDEV_LISTENER_CALL(realize, Forward, dev);
 }
 
 static void device_unrealize(DeviceState *dev, Error **errp)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
+    QDEV_LISTENER_CALL(unrealize, Reverse, dev);
+
     if (dc->exit) {
         int rc = dc->exit(dev);
         if (rc < 0) {
include/hw/qdev-core.h +6 -0
index 178fee2..f2dc267 100644
@@ -167,6 +167,12 @@ struct DeviceState {
     int alias_required_for_version;
 };
 
+struct DeviceListener {
+    void (*realize)(DeviceListener *listener, DeviceState *dev);
+    void (*unrealize)(DeviceListener *listener, DeviceState
 *dev);
+    QTAILQ_ENTRY(DeviceListener) link;
+};
+
 #define TYPE_BUS "bus"
 #define BUS(obj) OBJECT_CHECK(BusState, (obj), TYPE_BUS)

#define BUS_CLASS(klass) OBJECT_CLASS_CHECK(BusClass, (klass),
TYPE_BUS) @@ -368,4 +374,8 @@ static inline void qbus_set_hotplug_handler(BusState *bus, DeviceState *handler, QDEV_HOTPLUG_HANDLER_PROPERTY, errp); bus->allow_hotplug = 1;
}

include/hw/qdev-core.h +4 -0
+
+void qdev_listener_register(DeviceListener *listener);
+void qdev_listener_unregister(DeviceListener *listener);
+
 #endif
include/qemu/typedefs.h +1 -0
index 04df51b..e32bca2 100644
@@ -20,6 +20,7 @@ typedef struct Property Property;
 typedef struct PropertyInfo PropertyInfo;
 typedef struct CompatProperty CompatProperty;
 typedef struct DeviceState DeviceState;
+typedef struct DeviceListener DeviceListener;
 typedef struct BusState BusState;
 typedef struct BusClass BusClass;
 
#10 lore raw

PATCH v3 2/2 Xen: Use the ioreq-server API when available

In reply to Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222 +++++++++++++++++++++++++++++++++++++++++++
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)
configure +29 -0
index 9ac2600..c2db574 100755
@@ -1876,6 +1876,32 @@ int main(void) {
   xc_gnttab_open(NULL, 0);
   xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
   xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
+  xc_hvm_create_ioreq_server(xc, 0, 0, NULL);
+  return 0;
+}
+EOF
+      compile_prog "" "$xen_libs"
+    then
+    xen_ctrl_version=450
+    xen=yes
+
+  elif
+      cat > $TMPC <<EOF &&
+#include <xenctrl.h>
+#include <xenstore.h>
+#include <stdint.h>
+#include <xen/hvm/hvm_info_table.h>
+#if !defined(HVM_MAX_VCPUS)
+# error HVM_MAX_VCPUS not defined
+#endif
+int main(void) {
+  xc_interface *xc;
+  xs_daemon_open();
+  xc = xc_interface_open(0, 0, 0);
+  xc_hvm_set_mem_type(0, 0, HVMMEM_ram_ro, 0, 0);
+  xc_gnttab_open(NULL, 0);
+  xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
+  xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
   return 0;
 }
 EOF
@@ -4282,6 +4308,9 @@ if test -n "$sparc_cpu"; then
     echo "Target Sparc Arch $sparc_cpu"
 fi
 echo "xen support       $xen"
+if test "$xen" = "yes" ; then
+  echo "xen ctrl version  $xen_ctrl_version"
+fi
 echo "brlapi support    $brlapi"
 echo "bluez  support    $bluez"
 echo "Documentation     $docs"
include/hw/xen/xen_common.h +222 -0

233 lines not on this page · raw message

trace-events +8 -0
index 011d105..3efcff7 100644
@@ -895,6 +895,14 @@ pvscsi_tx_rings_num_pages(const char* label, uint32_t num) "Number of %s pages:
 # xen-hvm.c
 xen_ram_alloc(unsigned long ram_addr, unsigned long size) "requested: %#lx, size %#lx"
 xen_client_set_memory(uint64_t start_addr, unsigned long size, bool log_dirty) "%#"PRIx64" size %#lx, log_dirty %i"
+xen_ioreq_server_create(uint32_t id) "id: %u"
+xen_ioreq_server_destroy(uint32_t id) "id: %u"
+xen_map_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_unmap_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_map_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_unmap_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_map_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x"
+xen_unmap_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x"
 
 # xen-mapcache.c
 xen_map_cache(uint64_t phys_addr) "want %#"PRIx64
xen-hvm.c +153 -21

288 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#11 lore raw

PATCH v3 2/2 Xen: Use the ioreq-server API when available

In reply to Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222 +++++++++++++++++++++++++++++++++++++++++++
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)
configure +29 -0
index 9ac2600..c2db574 100755
@@ -1876,6 +1876,32 @@ int main(void) {
   xc_gnttab_open(NULL, 0);
   xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
   xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
+  xc_hvm_create_ioreq_server(xc, 0, 0, NULL);
+  return 0;
+}
+EOF
+      compile_prog "" "$xen_libs"
+    then
+    xen_ctrl_version=450
+    xen=yes
+
+  elif
+      cat > $TMPC <<EOF &&
+#include <xenctrl.h>
+#include <xenstore.h>
+#include <stdint.h>
+#include <xen/hvm/hvm_info_table.h>
+#if !defined(HVM_MAX_VCPUS)
+# error HVM_MAX_VCPUS not defined
+#endif
+int main(void) {
+  xc_interface *xc;
+  xs_daemon_open();
+  xc = xc_interface_open(0, 0, 0);
+  xc_hvm_set_mem_type(0, 0, HVMMEM_ram_ro, 0, 0);
+  xc_gnttab_open(NULL, 0);
+  xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
+  xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
   return 0;
 }
 EOF
@@ -4282,6 +4308,9 @@ if test -n "$sparc_cpu"; then
     echo "Target Sparc Arch $sparc_cpu"
 fi
 echo "xen support       $xen"
+if test "$xen" = "yes" ; then
+  echo "xen ctrl version  $xen_ctrl_version"
+fi
 echo "brlapi support    $brlapi"
 echo "bluez  support    $bluez"
 echo "Documentation     $docs"
include/hw/xen/xen_common.h +222 -0

233 lines not on this page · raw message

trace-events +8 -0
index 011d105..3efcff7 100644
@@ -895,6 +895,14 @@ pvscsi_tx_rings_num_pages(const char* label, uint32_t num) "Number of %s pages:
 # xen-hvm.c
 xen_ram_alloc(unsigned long ram_addr, unsigned long size) "requested: %#lx, size %#lx"
 xen_client_set_memory(uint64_t start_addr, unsigned long size, bool log_dirty) "%#"PRIx64" size %#lx, log_dirty %i"
+xen_ioreq_server_create(uint32_t id) "id: %u"
+xen_ioreq_server_destroy(uint32_t id) "id: %u"
+xen_map_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_unmap_mmio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_map_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_unmap_portio_range(uint32_t id, uint64_t start_addr, uint64_t end_addr) "id: %u start: %#"PRIx64" end: %#"PRIx64
+xen_map_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x"
+xen_unmap_pcidev(uint32_t id, uint8_t bus, uint8_t dev, uint8_t func) "id: %u bdf: %02x.%02x.%02x"
 
 # xen-mapcache.c
 xen_map_cache(uint64_t phys_addr) "want %#"PRIx64
xen-hvm.c +153 -21

288 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#12 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 17 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222 +++++++++++++++++++++++++++++++++++++++++++
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

Quoted 44 lines
xen-hvm.c +12 -3
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct MemoryListener *listener,
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 461

Quoted 10 lines
xen-hvm.c +3 -2
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

Quoted 11 lines
xen-hvm.c +1 -0
     }
 
@@ -480,6 +491,7 @@ static void xen_region_add(MemoryListener *listener,
                            MemoryRegionSection *section)
 {
     memory_region_ref(section->mr);
+
     xen_set_memory(listener, section, true);
 }
 

xen-hvm.c static void xen_region_del(MemoryListener *listener, · line 504

Quoted 6 lines
xen-hvm.c +1 -0
@@ -487,9 +499,54 @@ static void xen_region_del(MemoryListener *listener,
                            MemoryRegionSection *section)
 {
     xen_set_memory(listener, section, false);
+
     memory_region_unref(section->mr);
 }

Useless changes?

#13 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paul Durrant
Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 17 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222 +++++++++++++++++++++++++++++++++++++++++++
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

Quoted 44 lines
xen-hvm.c +12 -3
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t *xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct MemoryListener *listener,
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 461

Quoted 10 lines
xen-hvm.c +3 -2
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

Quoted 11 lines
xen-hvm.c +1 -0
     }
 
@@ -480,6 +491,7 @@ static void xen_region_add(MemoryListener *listener,
                            MemoryRegionSection *section)
 {
     memory_region_ref(section->mr);
+
     xen_set_memory(listener, section, true);
 }
 

xen-hvm.c static void xen_region_del(MemoryListener *listener, · line 504

Quoted 6 lines
xen-hvm.c +1 -0
@@ -487,9 +499,54 @@ static void xen_region_del(MemoryListener *listener,
                            MemoryRegionSection *section)
 {
     xen_set_memory(listener, section, false);
+
     memory_region_unref(section->mr);
 }

Useless changes?

#14 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 101 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 18 lines
xen-hvm.c +11 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
Quoted 7 lines

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 4 lines
xen-hvm.c +1 -2
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

I'm not sure which is better TBH.

Quoted 15 lines

xen-hvm.c static void xen_region_add(MemoryListener

Quoted 5 lines
xen-hvm.c +0 -0
     }
 
@@ -480,6 +491,7 @@ static void xen_region_add(MemoryListener

*listener,

xen-hvm.c static void xen_region_del(MemoryListener

Quoted 8 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     memory_region_ref(section->mr);
+
     xen_set_memory(listener, section, true);
 }
 
@@ -487,9 +499,54 @@ static void xen_region_del(MemoryListener

*listener,

Quoted 8 lines

xen-hvm.c static void xen_region_del(MemoryListener *listener, · line 504

Quoted 6 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     xen_set_memory(listener, section, false);
+
     memory_region_unref(section->mr);
 }

Useless changes?

Yes. Unfortunately I only noticed after posting. I will definitely clean up if I re-post.

Paul

#15 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Stefano Stabellini
Quoted 101 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 18 lines
xen-hvm.c +11 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
Quoted 7 lines

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 4 lines
xen-hvm.c +1 -2
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

I'm not sure which is better TBH.

Quoted 15 lines

xen-hvm.c static void xen_region_add(MemoryListener

Quoted 5 lines
xen-hvm.c +0 -0
     }
 
@@ -480,6 +491,7 @@ static void xen_region_add(MemoryListener

*listener,

xen-hvm.c static void xen_region_del(MemoryListener

Quoted 8 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     memory_region_ref(section->mr);
+
     xen_set_memory(listener, section, true);
 }
 
@@ -487,9 +499,54 @@ static void xen_region_del(MemoryListener

*listener,

Quoted 8 lines

xen-hvm.c static void xen_region_del(MemoryListener *listener, · line 504

Quoted 6 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     xen_set_memory(listener, section, false);
+
     memory_region_unref(section->mr);
 }

Useless changes?

Yes. Unfortunately I only noticed after posting. I will definitely clean up if I re-post.

Paul

#16 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 99 lines · Paul Durrant
Quoted 99 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 18 lines
xen-hvm.c +11 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 4 lines
xen-hvm.c +1 -2
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

I'm not sure which is better TBH.

I set simplification problems like this to my Part 1a digital electronics supervisees.

This is "if (!(log_dirty ^ add))" as they are both booleans, and reads rather more easily that either of the above.

~Andrew

#17 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paul Durrant
Quoted 99 lines · Paul Durrant
Quoted 99 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 18 lines
xen-hvm.c +11 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 4 lines
xen-hvm.c +1 -2
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

I'm not sure which is better TBH.

I set simplification problems like this to my Part 1a digital electronics supervisees.

This is "if (!(log_dirty ^ add))" as they are both booleans, and reads rather more easily that either of the above.

~Andrew

#18 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 106 lines · Andrew Cooper
Quoted 105 lines · Paul Durrant
Quoted 105 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 12 lines
xen-hvm.c +9 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 10 lines
xen-hvm.c +3 -2
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

Quoted 7 lines
Quoted 1 line

I'm not sure which is better TBH.

I set simplification problems like this to my Part 1a digital electronics supervisees.

This is "if (!(log_dirty ^ add))" as they are both booleans, and reads rather more easily that either of the above.

you are funny

#19 lore raw

Re: [Qemu-devel] [Xen-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Andrew Cooper
Quoted 106 lines · Andrew Cooper
Quoted 105 lines · Paul Durrant
Quoted 105 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 12 lines
xen-hvm.c +9 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 10 lines
xen-hvm.c +3 -2
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

Quoted 7 lines
Quoted 1 line

I'm not sure which is better TBH.

I set simplification problems like this to my Part 1a digital electronics supervisees.

This is "if (!(log_dirty ^ add))" as they are both booleans, and reads rather more easily that either of the above.

you are funny

#20 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Stefano Stabellini
Quoted 101 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 18 lines
xen-hvm.c +11 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
Quoted 7 lines

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 4 lines
xen-hvm.c +1 -2
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

Thinking some more about what Andrew said, this is even more simply expressed as

if (add != log_dirty)

is it not?

Paul

Quoted 23 lines

xen-hvm.c static void xen_region_add(MemoryListener

Quoted 5 lines
xen-hvm.c +0 -0
     }
 
@@ -480,6 +491,7 @@ static void xen_region_add(MemoryListener

*listener,

xen-hvm.c static void xen_region_del(MemoryListener

Quoted 8 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     memory_region_ref(section->mr);
+
     xen_set_memory(listener, section, true);
 }
 
@@ -487,9 +499,54 @@ static void xen_region_del(MemoryListener

*listener,

xen-hvm.c static void xen_region_del(MemoryListener *listener, · line 504

Quoted 6 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     xen_set_memory(listener, section, false);
+
     memory_region_unref(section->mr);
 }

Useless changes?

#21 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Stefano Stabellini
Quoted 101 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 18 lines
xen-hvm.c +11 -0
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
Quoted 7 lines

xen-hvm.c static void xen_set_memory(struct MemoryListener *listener, · line 462

Quoted 4 lines
xen-hvm.c +1 -2
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;

if (!((log_dirty && add) || (!log_dirty && !add)))

Thinking some more about what Andrew said, this is even more simply expressed as

if (add != log_dirty)

is it not?

Paul

Quoted 23 lines

xen-hvm.c static void xen_region_add(MemoryListener

Quoted 5 lines
xen-hvm.c +0 -0
     }
 
@@ -480,6 +491,7 @@ static void xen_region_add(MemoryListener

*listener,

xen-hvm.c static void xen_region_del(MemoryListener

Quoted 8 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     memory_region_ref(section->mr);
+
     xen_set_memory(listener, section, true);
 }
 
@@ -487,9 +499,54 @@ static void xen_region_del(MemoryListener

*listener,

xen-hvm.c static void xen_region_del(MemoryListener *listener, · line 504

Quoted 6 lines
xen-hvm.c +1 -0
                            MemoryRegionSection *section)
 {
     xen_set_memory(listener, section, false);
+
     memory_region_unref(section->mr);
 }

Useless changes?

#22 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 106 lines · Paul Durrant
Quoted 106 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 22 lines
xen-hvm.c +12 -2
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;
Quoted 8 lines
Quoted 2 lines

if (!((log_dirty && add) || (!log_dirty && !add)))

Thinking some more about what Andrew said, this is even more simply expressed as

if (add != log_dirty)

is it not?

Yes, I think that should work.

#23 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paul Durrant
Quoted 106 lines · Paul Durrant
Quoted 106 lines

-----Original Message-----
From: Stefano Stabellini [mailto:[email protected]]
Sent: 15 October 2014 15:38
To: Paul Durrant
Cc: [email protected]; [email protected]; Stefano Stabellini; Peter Maydell; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 11 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

The patch is OK, so you can add my Acked-by.
I have a couple of minor comments below. If you need to repost it then would be nice if you could address them.

Quoted 10 lines
Cc Stefano Stabellini <[email protected]>
Cc Peter Maydell <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michael Tokarev <[email protected]>
Cc Stefan Hajnoczi <[email protected]>
Cc Stefan Weil <[email protected]>
Cc Olaf Hering <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Alexander Graf <[email protected]>
Quoted 3 lines
---
 configure                   |   29 ++++++
 include/hw/xen/xen_common.h |  222

+++++++++++++++++++++++++++++++++++++++++++

Quoted 4 lines
 trace-events                |    8 ++
 xen-hvm.c                   |  174 +++++++++++++++++++++++++++++----
 4 files changed, 412 insertions(+), 21 deletions(-)

[...]

xen-hvm.c static inline ioreq_t

Quoted 5 lines
xen-hvm.c +0 -0
index 05e522c..0bbbf2a 100644
@@ -62,9 +62,6 @@ static inline ioreq_t

*xen_vcpu_ioreq(shared_iopage_t *shared_page, int vcpu)

xen-hvm.c static void xen_set_memory(struct

Quoted 27 lines
xen-hvm.c +3 -3
 }
 #  define FMT_ioreq_size "u"
 #endif
-#ifndef HVM_PARAM_BUFIOREQ_EVTCHN
-#define HVM_PARAM_BUFIOREQ_EVTCHN 26
-#endif
 
 #define BUFFER_IO_MAX_DELAY  100
 
@@ -78,6 +75,7 @@ typedef struct XenPhysmap {
 } XenPhysmap;
 
 typedef struct XenIOState {
+    ioservid_t ioservid;
     shared_iopage_t *shared_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
@@ -92,6 +90,8 @@ typedef struct XenIOState {
 
     struct xs_handle *xenstore;
     MemoryListener memory_listener;
+    MemoryListener io_listener;
+    DeviceListener device_listener;
     QLIST_HEAD(, XenPhysmap) physmap;
     hwaddr free_phys_offset;
     const XenPhysmap *log_for_dirtybit;
@@ -442,12 +442,23 @@ static void xen_set_memory(struct

MemoryListener *listener,

Quoted 22 lines
xen-hvm.c +12 -2
     bool log_dirty = memory_region_is_logging(section->mr);
     hvmmem_type_t mem_type;
 
+    if (section->mr == &ram_memory) {
+        return;
+    } else {
+        if (add) {
+            xen_map_memory_section(xen_xc, xen_domid, state->ioservid,
+                                   section);
+        } else {
+            xen_unmap_memory_section(xen_xc, xen_domid, state->ioservid,
+                                     section);
+        }
+    }
     if (!memory_region_is_ram(section->mr)) {
         return;
     }
 
-    if (!(section->mr != &ram_memory
-          && ( (log_dirty && add) || (!log_dirty && !add)))) {
+    if (!(log_dirty && add) && !(!log_dirty && !add)) {
         return;
Quoted 8 lines
Quoted 2 lines

if (!((log_dirty && add) || (!log_dirty && !add)))

Thinking some more about what Andrew said, this is even more simply expressed as

if (add != log_dirty)

is it not?

Yes, I think that should work.

#24 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paul Durrant
Quoted 9 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },

A vmstate which doesn't actually save any state? This seems rather suspicious...

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1188

Quoted 5 lines
xen-hvm.c +1 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size, ram_memory);
 
     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
+    vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

thanks
-- PMM

#25 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paul Durrant
Quoted 9 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },

A vmstate which doesn't actually save any state? This seems rather suspicious...

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1188

Quoted 5 lines
xen-hvm.c +1 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size, ram_memory);
 
     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
+    vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

thanks
-- PMM

#26 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 15/10/2014 19:30, Peter Maydell ha scritto:

Quoted 42 lines · Paul Durrant
Quoted 9 lines

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },

A vmstate which doesn't actually save any state? This seems rather suspicious...

Quoted 3 lines
xen-hvm.c +0 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size, ram_memory);
 
Quoted 6 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1188

Quoted 2 lines
xen-hvm.c +1 -0
     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
+    vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

In this case, it seems like a job for a vmstate change handler.

Paolo

#27 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Peter Maydell

Il 15/10/2014 19:30, Peter Maydell ha scritto:

Quoted 42 lines · Paul Durrant
Quoted 9 lines

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },

A vmstate which doesn't actually save any state? This seems rather suspicious...

Quoted 3 lines
xen-hvm.c +0 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size, ram_memory);
 
Quoted 6 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1188

Quoted 2 lines
xen-hvm.c +1 -0
     qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);
+    vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

In this case, it seems like a job for a vmstate change handler.

Paolo

#28 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 57 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: 16 October 2014 08:37
To: Peter Maydell; Paul Durrant
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 15/10/2014 19:30, Peter Maydell ha scritto:

Quoted 36 lines · Paul Durrant
Quoted 9 lines

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },
Quoted 4 lines

A vmstate which doesn't actually save any state? This seems rather suspicious...

Review int xen_hvm_init(ram_addr_t

Quoted 1 line
(quoted diff) +0 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 1 line

Review int xen_hvm_init(ram_addr_t · line 1185

Quoted 1 line
(quoted diff) +0 -0
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size,

ram_memory);

Quoted 2 lines
Quoted 2 lines

qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

+ vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

Paul

Quoted 1 line

Paolo

#29 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paolo Bonzini
Quoted 57 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: 16 October 2014 08:37
To: Peter Maydell; Paul Durrant
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 15/10/2014 19:30, Peter Maydell ha scritto:

Quoted 36 lines · Paul Durrant
Quoted 9 lines

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },
Quoted 4 lines

A vmstate which doesn't actually save any state? This seems rather suspicious...

Review int xen_hvm_init(ram_addr_t

Quoted 1 line
(quoted diff) +0 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 1 line

Review int xen_hvm_init(ram_addr_t · line 1185

Quoted 1 line
(quoted diff) +0 -0
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size,

ram_memory);

Quoted 2 lines
Quoted 2 lines

qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

+ vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

Paul

Quoted 1 line

Paolo

#30 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 16/10/2014 10:25, Paul Durrant ha scritto:

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen-hvm.c +4 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
Quoted 8 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1078

Quoted 4 lines
xen-hvm.c +4 -0
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

Quoted 4 lines

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

Paolo

#31 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paul Durrant

Il 16/10/2014 10:25, Paul Durrant ha scritto:

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
xen-hvm.c +4 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
Quoted 8 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1078

Quoted 4 lines
xen-hvm.c +4 -0
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

Quoted 4 lines

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

Paolo

#32 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 24 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: 16 October 2014 11:10
To: Paul Durrant; Peter Maydell
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 16/10/2014 10:25, Paul Durrant ha scritto:

Quoted 6 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data)

Quoted 6 lines
xen-hvm.c +6 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid,

0);

Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1078

Quoted 2 lines
xen-hvm.c +2 -0
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

Quoted 9 lines
Quoted 4 lines

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

I need something that will be called prior to the VM memory image being saved, but if save can fail I will also need something to be called if that occurs too.

Paul

Quoted 1 line

Paolo

#33 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paolo Bonzini
Quoted 24 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: 16 October 2014 11:10
To: Paul Durrant; Peter Maydell
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 16/10/2014 10:25, Paul Durrant ha scritto:

Quoted 6 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data)

Quoted 6 lines
xen-hvm.c +6 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid,

0);

Quoted 7 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1078

Quoted 2 lines
xen-hvm.c +2 -0
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

Quoted 9 lines
Quoted 4 lines

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

I need something that will be called prior to the VM memory image being saved, but if save can fail I will also need something to be called if that occurs too.

Paul

Quoted 1 line

Paolo

#34 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 16/10/2014 12:16, Paul Durrant ha scritto:

Quoted 8 lines
Quoted 4 lines

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

I need something that will be called prior to the VM memory image being saved, but if save can fail I will also need something to be called if that occurs too.

Can you check the runstate in the vmstate change callback? The runstate to use is RUN_STATE_FINISH_MIGRATE (and then you revert if you get from there to anything but RUN_STATE_POSTMIGRATE).

...

oh wait, those are the runstate for migration, xen's save-devices-state command uses something else. Luckily, the command is synchronous, so management cannot "poll" the state as is the case for regular migration. So a patch like this could provide the right runstates:

savevm.c +5 -1
index 2d8eb96..f9a8e27 100644
@@ -1144,7 +1144,7 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     int ret;
 
     saved_vm_running = runstate_is_running();
-    vm_stop(RUN_STATE_SAVE_VM);
+    vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
 
     f = qemu_fopen(filename, "wb");
     if (!f) {
@@ -1155,8 +1155,12 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     qemu_fclose(f);
     if (ret < 0) {
         error_set(errp, QERR_IO_ERROR);
+        goto the_end;
     }
 
+    runstate_set(RUN_STATE_POSTMIGRATE);
+    return;
+
  the_end:
     if (saved_vm_running) {
         vm_start();

(feel free to include it in your patches with Signed-off-by: Paolo Bonzini <[email protected]>).

Alternatively, can you stop/restart emulation always (even on stop/cont monitor commands) rather than just on migration? That would make things even simpler and not need anything like the above savevm.c change.

Paolo

#35 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paul Durrant

Il 16/10/2014 12:16, Paul Durrant ha scritto:

Quoted 8 lines
Quoted 4 lines

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

I need something that will be called prior to the VM memory image being saved, but if save can fail I will also need something to be called if that occurs too.

Can you check the runstate in the vmstate change callback? The runstate to use is RUN_STATE_FINISH_MIGRATE (and then you revert if you get from there to anything but RUN_STATE_POSTMIGRATE).

...

oh wait, those are the runstate for migration, xen's save-devices-state command uses something else. Luckily, the command is synchronous, so management cannot "poll" the state as is the case for regular migration. So a patch like this could provide the right runstates:

savevm.c +5 -1
index 2d8eb96..f9a8e27 100644
@@ -1144,7 +1144,7 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     int ret;
 
     saved_vm_running = runstate_is_running();
-    vm_stop(RUN_STATE_SAVE_VM);
+    vm_stop_force_state(RUN_STATE_FINISH_MIGRATE);
 
     f = qemu_fopen(filename, "wb");
     if (!f) {
@@ -1155,8 +1155,12 @@ void qmp_xen_save_devices_state(const char *filename, Error **errp)
     qemu_fclose(f);
     if (ret < 0) {
         error_set(errp, QERR_IO_ERROR);
+        goto the_end;
     }
 
+    runstate_set(RUN_STATE_POSTMIGRATE);
+    return;
+
  the_end:
     if (saved_vm_running) {
         vm_start();

(feel free to include it in your patches with Signed-off-by: Paolo Bonzini <[email protected]>).

Alternatively, can you stop/restart emulation always (even on stop/cont monitor commands) rather than just on migration? That would make things even simpler and not need anything like the above savevm.c change.

Paolo

#36 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paolo Bonzini
Quoted 39 lines

-----Original Message-----
From: Paul Durrant
Sent: 16 October 2014 11:17
To: 'Paolo Bonzini'; Peter Maydell
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: RE: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 6 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: 16 October 2014 11:10
To: Paul Durrant; Peter Maydell
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd

Hoffmann;

Quoted 23 lines

Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 16/10/2014 10:25, Paul Durrant ha scritto:

Quoted 6 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data)

Quoted 6 lines
xen-hvm.c +6 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid,

0);

Quoted 11 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1078

Quoted 2 lines
xen-hvm.c +2 -0
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

Quoted 8 lines
Quoted 4 lines

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

I need something that will be called prior to the VM memory image being saved, but if save can fail I will also need something to be called if that occurs too.

Tracing this back through the many layers it looks like a state change notifier may work after all as qmp_xen_save_devices_state() does calls vm_stop() with RUN_STATE_SAVE_VM before calling qemu_save_device_state(). I'll check again.

Paul

Quoted 4 lines

Paul

Quoted 1 line

Paolo

#37 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Paolo Bonzini
Quoted 39 lines

-----Original Message-----
From: Paul Durrant
Sent: 16 October 2014 11:17
To: 'Paolo Bonzini'; Peter Maydell
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: RE: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 6 lines

-----Original Message-----
From: Paolo Bonzini [mailto:[email protected]]
Sent: 16 October 2014 11:10
To: Paul Durrant; Peter Maydell
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd

Hoffmann;

Quoted 23 lines

Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Il 16/10/2014 10:25, Paul Durrant ha scritto:

Quoted 6 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data)

Quoted 6 lines
xen-hvm.c +6 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid,

0);

Quoted 11 lines
Quoted 6 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1078

Quoted 2 lines
xen-hvm.c +2 -0
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

In this case, it seems like a job for a vmstate change handler.

I looked at that but it did not seem to give me the right semantic, whereas the pre-save callback gave me exactly the right semantic.

Quoted 8 lines
Quoted 4 lines

What exactly is the right semantics? Note that save _can_ fail, so you need the ability to roll back to the source machine. I think this is missing from your patch, and there is no post_save hook that you can use.

I need something that will be called prior to the VM memory image being saved, but if save can fail I will also need something to be called if that occurs too.

Tracing this back through the many layers it looks like a state change notifier may work after all as qmp_xen_save_devices_state() does calls vm_stop() with RUN_STATE_SAVE_VM before calling qemu_save_device_state(). I'll check again.

Paul

Quoted 4 lines

Paul

Quoted 1 line

Paolo

#38 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Peter Maydell
Quoted 24 lines

-----Original Message-----
From: Peter Maydell [mailto:[email protected]]
Sent: 15 October 2014 18:30
To: Paul Durrant
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 9 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

Quoted 16 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

Good point. I was just using the Xen major, but that doesn't make much sense.

Quoted 8 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },

A vmstate which doesn't actually save any state? This seems rather suspicious...

Not really. The state is actually in Xen and so is saved by the Xen toolstack. I need the pre-save hook here because the pages shared between QEMU and Xen need re-inserting into the guest before the Xen toolstack saves the memory image.

Quoted 6 lines

Review int xen_hvm_init(ram_addr_t

Quoted 1 line
(quoted diff) +0 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Review int xen_hvm_init(ram_addr_t · line 1185

Quoted 1 line
(quoted diff) +0 -0
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size,

ram_memory);

Quoted 2 lines
Quoted 8 lines

qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);

Quoted 1 line

+ vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

I don't think so. As I said, there is no state to save but there is need for a callback before state is saved. Is there another way to achieve that? I could not find any 'clean' way to do it.

Paul

Quoted 2 lines

thanks
-- PMM

#39 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Peter Maydell
Quoted 24 lines

-----Original Message-----
From: Peter Maydell [mailto:[email protected]]
Sent: 15 October 2014 18:30
To: Paul Durrant
Cc: QEMU Developers; [email protected]; Stefano Stabellini; Paolo Bonzini; Michael Tokarev; Stefan Hajnoczi; Stefan Weil; Olaf Hering; Gerd Hoffmann; Alexey Kardashevskiy; Alexander Graf
Subject: Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 9 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

Quoted 16 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1083

Quoted 13 lines
xen-hvm.c +13 -0
+static void xen_hvm_pre_save(void *opaque)
+{
+    XenIOState *state = opaque;
+
+    /* Stop servicing emulation requests */
+    xen_set_ioreq_server_state(xen_xc, xen_domid, state->ioservid, 0);
+    xen_destroy_ioreq_server(xen_xc, xen_domid, state->ioservid);
+}
+
+static const VMStateDescription vmstate_xen_hvm = {
+    .name = "xen-hvm",
+    .version_id = 4,
+    .minimum_version_id = 4,

This is new in upstream so why's it starting at version 4?

Good point. I was just using the Xen major, but that doesn't make much sense.

Quoted 8 lines

xen-hvm.c static void xen_wakeup_notifier(Notifier *notifier, void *data) · line 1087

Quoted 4 lines
xen-hvm.c +4 -0
+    .pre_save = xen_hvm_pre_save,
+    .fields = (VMStateField[]) {
+        VMSTATE_END_OF_LIST()
+    },

A vmstate which doesn't actually save any state? This seems rather suspicious...

Not really. The state is actually in Xen and so is saved by the Xen toolstack. I need the pre-save hook here because the pages shared between QEMU and Xen need re-inserting into the guest before the Xen toolstack saves the memory image.

Quoted 6 lines

Review int xen_hvm_init(ram_addr_t

Quoted 1 line
(quoted diff) +0 -0
@@ -1060,12 +1185,19 @@ int xen_hvm_init(ram_addr_t

*below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Review int xen_hvm_init(ram_addr_t · line 1185

Quoted 1 line
(quoted diff) +0 -0
     xen_ram_init(below_4g_mem_size, above_4g_mem_size, ram_size,

ram_memory);

Quoted 2 lines
Quoted 8 lines

qemu_add_vm_change_state_handler(xen_hvm_change_state_handler, state);

Quoted 1 line

+ vmstate_register(NULL, 0, &vmstate_xen_hvm, state);

Is the new use of vmstate_register() really necessary? Usually the state you're saving corresponds to some QOM device whose vmsd field you can use instead.

I don't think so. As I said, there is no state to save but there is need for a callback before state is saved. Is there another way to achieve that? I could not find any 'clean' way to do it.

Paul

Quoted 2 lines

thanks
-- PMM

#40 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell
Quoted 9 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

Quoted 3 lines

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

Unfortunately even though it is pretty x86 specific, it is still compiled on ARM, even though it is never actually used (it is used in i386 emulation with Xen acceleration support, while on ARM we only use the PV machine).

#41 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Peter Maydell
Quoted 11 lines · Peter Maydell
Quoted 9 lines · Paul Durrant

The ioreq-server API added to Xen 4.5 offers better security than the existing Xen/QEMU interface because the shared pages that are used to pass emulation request/results back and forth are removed from the guest's memory space before any requests are serviced. This prevents the guest from mapping these pages (they are in a well known location) and attempting to attack QEMU by synthesizing its own request structures. Hence, this patch modifies configure to detect whether the API is available, and adds the necessary code to use the API if it is.

Quoted 3 lines

This commit message doesn't mention it, but presumably this is all x86-specific given it's in a file which is only used for x86 Xen?

Unfortunately even though it is pretty x86 specific, it is still compiled on ARM, even though it is never actually used (it is used in i386 emulation with Xen acceleration support, while on ARM we only use the PV machine).

#42 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 4 lines · Stefano Stabellini

Unfortunately even though it is pretty x86 specific, it is still compiled on ARM, even though it is never actually used (it is used in i386 emulation with Xen acceleration support, while on ARM we only use the PV machine).

Really? CONFIG_XEN_I386 is only set in the i386 and x86_64 defconfigs...

-- PMM

#43 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Stefano Stabellini
Quoted 4 lines · Stefano Stabellini

Unfortunately even though it is pretty x86 specific, it is still compiled on ARM, even though it is never actually used (it is used in i386 emulation with Xen acceleration support, while on ARM we only use the PV machine).

Really? CONFIG_XEN_I386 is only set in the i386 and x86_64 defconfigs...

-- PMM

#44 lore raw

Re: [PATCH v3 2/2] Xen: Use the ioreq-server API when available

Quoted 8 lines · Peter Maydell
Quoted 4 lines · Stefano Stabellini

Unfortunately even though it is pretty x86 specific, it is still compiled on ARM, even though it is never actually used (it is used in i386 emulation with Xen acceleration support, while on ARM we only use the PV machine).

Really? CONFIG_XEN_I386 is only set in the i386 and x86_64 defconfigs...

We are still using --target-list=i386-softmmu on ARM :-(
Of course we don't need i386-softmmu emulation on ARM, but we didn't manage to disentangle the PV machine from i386 architecture emulation, see:

http://marc.info/?l=qemu-devel&m=139082410318316&w=2
http://marc.info/?l=qemu-devel&m=140491268731773&w=2

#45 lore raw

Re: [Qemu-devel] [PATCH v3 2/2] Xen: Use the ioreq-server API when available

In reply to Peter Maydell
Quoted 8 lines · Peter Maydell
Quoted 4 lines · Stefano Stabellini

Unfortunately even though it is pretty x86 specific, it is still compiled on ARM, even though it is never actually used (it is used in i386 emulation with Xen acceleration support, while on ARM we only use the PV machine).

Really? CONFIG_XEN_I386 is only set in the i386 and x86_64 defconfigs...

We are still using --target-list=i386-softmmu on ARM :-(
Of course we don't need i386-softmmu emulation on ARM, but we didn't manage to disentangle the PV machine from i386 architecture emulation, see:

http://marc.info/?l=qemu-devel&m=139082410318316&w=2
http://marc.info/?l=qemu-devel&m=140491268731773&w=2