peterdelevoryas / rpms / qemu

Forked from rpms/qemu 2 years ago
Clone

Blame qemu-notifier-event-notifier-implementation.patch

Justin M. Forbes 272dfe
event notifiers are slightly generalized eventfd descriptors. Current
Justin M. Forbes 272dfe
implementation depends on eventfd because vhost is the only user, and
Justin M. Forbes 272dfe
vhost depends on eventfd anyway, but a stub is provided for non-eventfd
Justin M. Forbes 272dfe
case.
Justin M. Forbes 272dfe
Justin M. Forbes 272dfe
We'll be able to further generalize this when another user comes along
Justin M. Forbes 272dfe
and we see how to best do this.
Justin M. Forbes 272dfe
Justin M. Forbes 272dfe
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Justin M. Forbes 272dfe
---
Justin M. Forbes 272dfe
 Makefile.target |    1 +
Justin M. Forbes 272dfe
 hw/notifier.c   |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
Justin M. Forbes 272dfe
 hw/notifier.h   |   16 ++++++++++++++++
Justin M. Forbes 272dfe
 qemu-common.h   |    1 +
Justin M. Forbes 272dfe
 4 files changed, 68 insertions(+), 0 deletions(-)
Justin M. Forbes 272dfe
 create mode 100644 hw/notifier.c
Justin M. Forbes 272dfe
 create mode 100644 hw/notifier.h
Justin M. Forbes 272dfe
Justin M. Forbes 272dfe
diff --git a/Makefile.target b/Makefile.target
Justin M. Forbes 272dfe
index 6037fed..0c844a9 100644
Justin M. Forbes 272dfe
--- a/Makefile.target
Justin M. Forbes 272dfe
+++ b/Makefile.target
Justin M. Forbes 272dfe
@@ -167,6 +167,7 @@ obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o
Justin M. Forbes 272dfe
 # virtio has to be here due to weird dependency between PCI and virtio-net.
Justin M. Forbes 272dfe
 # need to fix this properly
Justin M. Forbes 272dfe
 obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o virtio-serial-bus.o
Justin M. Forbes 272dfe
+obj-y += notifier.o
Justin M. Forbes 272dfe
 obj-$(CONFIG_KVM) += kvm.o kvm-all.o
Justin M. Forbes 272dfe
 # MSI-X depends on kvm for interrupt injection,
Justin M. Forbes 272dfe
 # so moved it from Makefile.hw to Makefile.target for now
Justin M. Forbes 272dfe
diff --git a/hw/notifier.c b/hw/notifier.c
Justin M. Forbes 272dfe
new file mode 100644
Justin M. Forbes 272dfe
index 0000000..dff38de
Justin M. Forbes 272dfe
--- /dev/null
Justin M. Forbes 272dfe
+++ b/hw/notifier.c
Justin M. Forbes 272dfe
@@ -0,0 +1,50 @@
Justin M. Forbes 272dfe
+#include "hw.h"
Justin M. Forbes 272dfe
+#include "notifier.h"
Justin M. Forbes 272dfe
+#ifdef CONFIG_EVENTFD
Justin M. Forbes 272dfe
+#include <sys/eventfd.h>
Justin M. Forbes 272dfe
+#endif
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+int event_notifier_init(EventNotifier *e, int active)
Justin M. Forbes 272dfe
+{
Justin M. Forbes 272dfe
+#ifdef CONFIG_EVENTFD
Justin M. Forbes 272dfe
+	int fd = eventfd(!!active, EFD_NONBLOCK | EFD_CLOEXEC);
Justin M. Forbes 272dfe
+	if (fd < 0)
Justin M. Forbes 272dfe
+		return -errno;
Justin M. Forbes 272dfe
+	e->fd = fd;
Justin M. Forbes 272dfe
+	return 0;
Justin M. Forbes 272dfe
+#else
Justin M. Forbes 272dfe
+	return -ENOSYS;
Justin M. Forbes 272dfe
+#endif
Justin M. Forbes 272dfe
+}
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+void event_notifier_cleanup(EventNotifier *e)
Justin M. Forbes 272dfe
+{
Justin M. Forbes 272dfe
+	close(e->fd);
Justin M. Forbes 272dfe
+}
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+int event_notifier_get_fd(EventNotifier *e)
Justin M. Forbes 272dfe
+{
Justin M. Forbes 272dfe
+	return e->fd;
Justin M. Forbes 272dfe
+}
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+int event_notifier_test_and_clear(EventNotifier *e)
Justin M. Forbes 272dfe
+{
Justin M. Forbes 272dfe
+	uint64_t value;
Justin M. Forbes 272dfe
+	int r = read(e->fd, &value, sizeof value);
Justin M. Forbes 272dfe
+	return r == sizeof value;
Justin M. Forbes 272dfe
+}
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+int event_notifier_test(EventNotifier *e)
Justin M. Forbes 272dfe
+{
Justin M. Forbes 272dfe
+	uint64_t value;
Justin M. Forbes 272dfe
+	int r = read(e->fd, &value, sizeof value);
Justin M. Forbes 272dfe
+	if (r == sizeof value) {
Justin M. Forbes 272dfe
+		/* restore previous value. */
Justin M. Forbes 272dfe
+		int s = write(e->fd, &value, sizeof value);
Justin M. Forbes 272dfe
+		/* never blocks because we use EFD_SEMAPHORE.
Justin M. Forbes 272dfe
+		 * If we didn't we'd get EAGAIN on overflow
Justin M. Forbes 272dfe
+		 * and we'd have to write code to ignore it. */
Justin M. Forbes 272dfe
+		assert(s == sizeof value);
Justin M. Forbes 272dfe
+	}
Justin M. Forbes 272dfe
+	return r == sizeof value;
Justin M. Forbes 272dfe
+}
Justin M. Forbes 272dfe
diff --git a/hw/notifier.h b/hw/notifier.h
Justin M. Forbes 272dfe
new file mode 100644
Justin M. Forbes 272dfe
index 0000000..24117ea
Justin M. Forbes 272dfe
--- /dev/null
Justin M. Forbes 272dfe
+++ b/hw/notifier.h
Justin M. Forbes 272dfe
@@ -0,0 +1,16 @@
Justin M. Forbes 272dfe
+#ifndef QEMU_EVENT_NOTIFIER_H
Justin M. Forbes 272dfe
+#define QEMU_EVENT_NOTIFIER_H
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+#include "qemu-common.h"
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+struct EventNotifier {
Justin M. Forbes 272dfe
+	int fd;
Justin M. Forbes 272dfe
+};
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+int event_notifier_init(EventNotifier *, int active);
Justin M. Forbes 272dfe
+void event_notifier_cleanup(EventNotifier *);
Justin M. Forbes 272dfe
+int event_notifier_get_fd(EventNotifier *);
Justin M. Forbes 272dfe
+int event_notifier_test_and_clear(EventNotifier *);
Justin M. Forbes 272dfe
+int event_notifier_test(EventNotifier *);
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+#endif
Justin M. Forbes 272dfe
diff --git a/qemu-common.h b/qemu-common.h
Justin M. Forbes 272dfe
index 5fbe0f9..cdead98 100644
Justin M. Forbes 272dfe
--- a/qemu-common.h
Justin M. Forbes 272dfe
+++ b/qemu-common.h
Justin M. Forbes 272dfe
@@ -217,6 +217,7 @@ typedef struct uWireSlave uWireSlave;
Justin M. Forbes 272dfe
 typedef struct I2SCodec I2SCodec;
Justin M. Forbes 272dfe
 typedef struct DeviceState DeviceState;
Justin M. Forbes 272dfe
 typedef struct SSIBus SSIBus;
Justin M. Forbes 272dfe
+typedef struct EventNotifier EventNotifier;
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
 /* CPU save/load.  */
Justin M. Forbes 272dfe
 void cpu_save(QEMUFile *f, void *opaque);
Justin M. Forbes 272dfe
-- 
Justin M. Forbes 272dfe
1.6.6.144.g5c3af