All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Graf <agraf@suse.de>
To: kvm-ppc@vger.kernel.org
Cc: "kvm@vger.kernel.org mailing list" <kvm@vger.kernel.org>,
	Scott Wood <scottwood@freescale.com>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	Gleb Natapov <gleb@redhat.com>
Subject: [PATCH 09/20] kvm: add device control API
Date: Fri, 26 Apr 2013 10:24:38 +0200	[thread overview]
Message-ID: <1366964689-27567-10-git-send-email-agraf@suse.de> (raw)
In-Reply-To: <1366964689-27567-1-git-send-email-agraf@suse.de>

From: Scott Wood <scottwood@freescale.com>

Currently, devices that are emulated inside KVM are configured in a
hardcoded manner based on an assumption that any given architecture
only has one way to do it.  If there's any need to access device state,
it is done through inflexible one-purpose-only IOCTLs (e.g.
KVM_GET/SET_LAPIC).  Defining new IOCTLs for every little thing is
cumbersome and depletes a limited numberspace.

This API provides a mechanism to instantiate a device of a certain
type, returning an ID that can be used to set/get attributes of the
device.  Attributes may include configuration parameters (e.g.
register base address), device state, operational commands, etc.  It
is similar to the ONE_REG API, except that it acts on devices rather
than vcpus.

Both device types and individual attributes can be tested without having
to create the device or get/set the attribute, without the need for
separately managing enumerated capabilities.

Signed-off-by: Scott Wood <scottwood@freescale.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

---

v3 -> v4:

  - fix header
---
 Documentation/virtual/kvm/api.txt        |   70 ++++++++++++++++
 Documentation/virtual/kvm/devices/README |    1 +
 include/linux/kvm_host.h                 |   35 ++++++++
 include/uapi/linux/kvm.h                 |   27 ++++++
 virt/kvm/kvm_main.c                      |  129 ++++++++++++++++++++++++++++++
 5 files changed, 262 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/virtual/kvm/devices/README

diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt
index 976eb65..d52f3f9 100644
--- a/Documentation/virtual/kvm/api.txt
+++ b/Documentation/virtual/kvm/api.txt
@@ -2173,6 +2173,76 @@ header; first `n_valid' valid entries with contents from the data
 written, then `n_invalid' invalid entries, invalidating any previously
 valid entries found.
 
+4.79 KVM_CREATE_DEVICE
+
+Capability: KVM_CAP_DEVICE_CTRL
+Type: vm ioctl
+Parameters: struct kvm_create_device (in/out)
+Returns: 0 on success, -1 on error
+Errors:
+  ENODEV: The device type is unknown or unsupported
+  EEXIST: Device already created, and this type of device may not
+          be instantiated multiple times
+
+  Other error conditions may be defined by individual device types or
+  have their standard meanings.
+
+Creates an emulated device in the kernel.  The file descriptor returned
+in fd can be used with KVM_SET/GET/HAS_DEVICE_ATTR.
+
+If the KVM_CREATE_DEVICE_TEST flag is set, only test whether the
+device type is supported (not necessarily whether it can be created
+in the current vm).
+
+Individual devices should not define flags.  Attributes should be used
+for specifying any behavior that is not implied by the device type
+number.
+
+struct kvm_create_device {
+	__u32	type;	/* in: KVM_DEV_TYPE_xxx */
+	__u32	fd;	/* out: device handle */
+	__u32	flags;	/* in: KVM_CREATE_DEVICE_xxx */
+};
+
+4.80 KVM_SET_DEVICE_ATTR/KVM_GET_DEVICE_ATTR
+
+Capability: KVM_CAP_DEVICE_CTRL
+Type: device ioctl
+Parameters: struct kvm_device_attr
+Returns: 0 on success, -1 on error
+Errors:
+  ENXIO:  The group or attribute is unknown/unsupported for this device
+  EPERM:  The attribute cannot (currently) be accessed this way
+          (e.g. read-only attribute, or attribute that only makes
+          sense when the device is in a different state)
+
+  Other error conditions may be defined by individual device types.
+
+Gets/sets a specified piece of device configuration and/or state.  The
+semantics are device-specific.  See individual device documentation in
+the "devices" directory.  As with ONE_REG, the size of the data
+transferred is defined by the particular attribute.
+
+struct kvm_device_attr {
+	__u32	flags;		/* no flags currently defined */
+	__u32	group;		/* device-defined */
+	__u64	attr;		/* group-defined */
+	__u64	addr;		/* userspace address of attr data */
+};
+
+4.81 KVM_HAS_DEVICE_ATTR
+
+Capability: KVM_CAP_DEVICE_CTRL
+Type: device ioctl
+Parameters: struct kvm_device_attr
+Returns: 0 on success, -1 on error
+Errors:
+  ENXIO:  The group or attribute is unknown/unsupported for this device
+
+Tests whether a device supports a particular attribute.  A successful
+return indicates the attribute is implemented.  It does not necessarily
+indicate that the attribute can be read or written in the device's
+current state.  "addr" is ignored.
 
 4.77 KVM_ARM_VCPU_INIT
 
diff --git a/Documentation/virtual/kvm/devices/README b/Documentation/virtual/kvm/devices/README
new file mode 100644
index 0000000..34a6983
--- /dev/null
+++ b/Documentation/virtual/kvm/devices/README
@@ -0,0 +1 @@
+This directory contains specific device bindings for KVM_CAP_DEVICE_CTRL.
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index dcef724..6dab6b5 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1064,6 +1064,41 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
 
 extern bool kvm_rebooting;
 
+struct kvm_device_ops;
+
+struct kvm_device {
+	struct kvm_device_ops *ops;
+	struct kvm *kvm;
+	atomic_t users;
+	void *private;
+};
+
+/* create, destroy, and name are mandatory */
+struct kvm_device_ops {
+	const char *name;
+	int (*create)(struct kvm_device *dev, u32 type);
+
+	/*
+	 * Destroy is responsible for freeing dev.
+	 *
+	 * Destroy may be called before or after destructors are called
+	 * on emulated I/O regions, depending on whether a reference is
+	 * held by a vcpu or other kvm component that gets destroyed
+	 * after the emulated I/O.
+	 */
+	void (*destroy)(struct kvm_device *dev);
+
+	int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
+	int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
+	int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
+	long (*ioctl)(struct kvm_device *dev, unsigned int ioctl,
+		      unsigned long arg);
+};
+
+void kvm_device_get(struct kvm_device *dev);
+void kvm_device_put(struct kvm_device *dev);
+struct kvm_device *kvm_device_from_filp(struct file *filp);
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index c741902..38a0be0 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -666,6 +666,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_EPR 86
 #define KVM_CAP_ARM_PSCI 87
 #define KVM_CAP_ARM_SET_DEVICE_ADDR 88
+#define KVM_CAP_DEVICE_CTRL 89
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -819,6 +820,24 @@ struct kvm_arm_device_addr {
 };
 
 /*
+ * Device control API, available with KVM_CAP_DEVICE_CTRL
+ */
+#define KVM_CREATE_DEVICE_TEST		1
+
+struct kvm_create_device {
+	__u32	type;	/* in: KVM_DEV_TYPE_xxx */
+	__u32	fd;	/* out: device handle */
+	__u32	flags;	/* in: KVM_CREATE_DEVICE_xxx */
+};
+
+struct kvm_device_attr {
+	__u32	flags;		/* no flags currently defined */
+	__u32	group;		/* device-defined */
+	__u64	attr;		/* group-defined */
+	__u64	addr;		/* userspace address of attr data */
+};
+
+/*
  * ioctls for VM fds
  */
 #define KVM_SET_MEMORY_REGION     _IOW(KVMIO,  0x40, struct kvm_memory_region)
@@ -906,6 +925,14 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_ARM_SET_DEVICE_ADDR */
 #define KVM_ARM_SET_DEVICE_ADDR	  _IOW(KVMIO,  0xab, struct kvm_arm_device_addr)
 
+/* ioctl for vm fd */
+#define KVM_CREATE_DEVICE	  _IOWR(KVMIO,  0xe0, struct kvm_create_device)
+
+/* ioctls for fds returned by KVM_CREATE_DEVICE */
+#define KVM_SET_DEVICE_ATTR	  _IOW(KVMIO,  0xe1, struct kvm_device_attr)
+#define KVM_GET_DEVICE_ATTR	  _IOW(KVMIO,  0xe2, struct kvm_device_attr)
+#define KVM_HAS_DEVICE_ATTR	  _IOW(KVMIO,  0xe3, struct kvm_device_attr)
+
 /*
  * ioctls for vcpu fds
  */
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index f9492f3..5f0d78c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2159,6 +2159,117 @@ out:
 }
 #endif
 
+static int kvm_device_ioctl_attr(struct kvm_device *dev,
+				 int (*accessor)(struct kvm_device *dev,
+						 struct kvm_device_attr *attr),
+				 unsigned long arg)
+{
+	struct kvm_device_attr attr;
+
+	if (!accessor)
+		return -EPERM;
+
+	if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
+		return -EFAULT;
+
+	return accessor(dev, &attr);
+}
+
+static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
+			     unsigned long arg)
+{
+	struct kvm_device *dev = filp->private_data;
+
+	switch (ioctl) {
+	case KVM_SET_DEVICE_ATTR:
+		return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
+	case KVM_GET_DEVICE_ATTR:
+		return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
+	case KVM_HAS_DEVICE_ATTR:
+		return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
+	default:
+		if (dev->ops->ioctl)
+			return dev->ops->ioctl(dev, ioctl, arg);
+
+		return -ENOTTY;
+	}
+}
+
+void kvm_device_get(struct kvm_device *dev)
+{
+	atomic_inc(&dev->users);
+}
+
+void kvm_device_put(struct kvm_device *dev)
+{
+	if (atomic_dec_and_test(&dev->users))
+		dev->ops->destroy(dev);
+}
+
+static int kvm_device_release(struct inode *inode, struct file *filp)
+{
+	struct kvm_device *dev = filp->private_data;
+	struct kvm *kvm = dev->kvm;
+
+	kvm_device_put(dev);
+	kvm_put_kvm(kvm);
+	return 0;
+}
+
+static const struct file_operations kvm_device_fops = {
+	.unlocked_ioctl = kvm_device_ioctl,
+	.release = kvm_device_release,
+};
+
+struct kvm_device *kvm_device_from_filp(struct file *filp)
+{
+	if (filp->f_op != &kvm_device_fops)
+		return NULL;
+
+	return filp->private_data;
+}
+
+static int kvm_ioctl_create_device(struct kvm *kvm,
+				   struct kvm_create_device *cd)
+{
+	struct kvm_device_ops *ops = NULL;
+	struct kvm_device *dev;
+	bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
+	int ret;
+
+	switch (cd->type) {
+	default:
+		return -ENODEV;
+	}
+
+	if (test)
+		return 0;
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	dev->ops = ops;
+	dev->kvm = kvm;
+	atomic_set(&dev->users, 1);
+
+	ret = ops->create(dev, cd->type);
+	if (ret < 0) {
+		kfree(dev);
+		return ret;
+	}
+
+	ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR);
+	if (ret < 0) {
+		ops->destroy(dev);
+		return ret;
+	}
+
+	kvm_get_kvm(kvm);
+	cd->fd = ret;
+	return 0;
+}
+
 static long kvm_vm_ioctl(struct file *filp,
 			   unsigned int ioctl, unsigned long arg)
 {
@@ -2304,6 +2415,24 @@ static long kvm_vm_ioctl(struct file *filp,
 		break;
 	}
 #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
+	case KVM_CREATE_DEVICE: {
+		struct kvm_create_device cd;
+
+		r = -EFAULT;
+		if (copy_from_user(&cd, argp, sizeof(cd)))
+			goto out;
+
+		r = kvm_ioctl_create_device(kvm, &cd);
+		if (r)
+			goto out;
+
+		r = -EFAULT;
+		if (copy_to_user(argp, &cd, sizeof(cd)))
+			goto out;
+
+		r = 0;
+		break;
+	}
 	default:
 		r = kvm_arch_vm_ioctl(filp, ioctl, arg);
 		if (r == -ENOTTY)
-- 
1.6.0.2


WARNING: multiple messages have this Message-ID (diff)
From: Alexander Graf <agraf@suse.de>
To: kvm-ppc@vger.kernel.org
Cc: "kvm@vger.kernel.org mailing list" <kvm@vger.kernel.org>,
	Scott Wood <scottwood@freescale.com>,
	Marcelo Tosatti <mtosatti@redhat.com>,
	Gleb Natapov <gleb@redhat.com>
Subject: [PATCH 09/20] kvm: add device control API
Date: Fri, 26 Apr 2013 08:24:38 +0000	[thread overview]
Message-ID: <1366964689-27567-10-git-send-email-agraf@suse.de> (raw)
In-Reply-To: <1366964689-27567-1-git-send-email-agraf@suse.de>

From: Scott Wood <scottwood@freescale.com>

Currently, devices that are emulated inside KVM are configured in a
hardcoded manner based on an assumption that any given architecture
only has one way to do it.  If there's any need to access device state,
it is done through inflexible one-purpose-only IOCTLs (e.g.
KVM_GET/SET_LAPIC).  Defining new IOCTLs for every little thing is
cumbersome and depletes a limited numberspace.

This API provides a mechanism to instantiate a device of a certain
type, returning an ID that can be used to set/get attributes of the
device.  Attributes may include configuration parameters (e.g.
register base address), device state, operational commands, etc.  It
is similar to the ONE_REG API, except that it acts on devices rather
than vcpus.

Both device types and individual attributes can be tested without having
to create the device or get/set the attribute, without the need for
separately managing enumerated capabilities.

Signed-off-by: Scott Wood <scottwood@freescale.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

---

v3 -> v4:

  - fix header
---
 Documentation/virtual/kvm/api.txt        |   70 ++++++++++++++++
 Documentation/virtual/kvm/devices/README |    1 +
 include/linux/kvm_host.h                 |   35 ++++++++
 include/uapi/linux/kvm.h                 |   27 ++++++
 virt/kvm/kvm_main.c                      |  129 ++++++++++++++++++++++++++++++
 5 files changed, 262 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/virtual/kvm/devices/README

diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt
index 976eb65..d52f3f9 100644
--- a/Documentation/virtual/kvm/api.txt
+++ b/Documentation/virtual/kvm/api.txt
@@ -2173,6 +2173,76 @@ header; first `n_valid' valid entries with contents from the data
 written, then `n_invalid' invalid entries, invalidating any previously
 valid entries found.
 
+4.79 KVM_CREATE_DEVICE
+
+Capability: KVM_CAP_DEVICE_CTRL
+Type: vm ioctl
+Parameters: struct kvm_create_device (in/out)
+Returns: 0 on success, -1 on error
+Errors:
+  ENODEV: The device type is unknown or unsupported
+  EEXIST: Device already created, and this type of device may not
+          be instantiated multiple times
+
+  Other error conditions may be defined by individual device types or
+  have their standard meanings.
+
+Creates an emulated device in the kernel.  The file descriptor returned
+in fd can be used with KVM_SET/GET/HAS_DEVICE_ATTR.
+
+If the KVM_CREATE_DEVICE_TEST flag is set, only test whether the
+device type is supported (not necessarily whether it can be created
+in the current vm).
+
+Individual devices should not define flags.  Attributes should be used
+for specifying any behavior that is not implied by the device type
+number.
+
+struct kvm_create_device {
+	__u32	type;	/* in: KVM_DEV_TYPE_xxx */
+	__u32	fd;	/* out: device handle */
+	__u32	flags;	/* in: KVM_CREATE_DEVICE_xxx */
+};
+
+4.80 KVM_SET_DEVICE_ATTR/KVM_GET_DEVICE_ATTR
+
+Capability: KVM_CAP_DEVICE_CTRL
+Type: device ioctl
+Parameters: struct kvm_device_attr
+Returns: 0 on success, -1 on error
+Errors:
+  ENXIO:  The group or attribute is unknown/unsupported for this device
+  EPERM:  The attribute cannot (currently) be accessed this way
+          (e.g. read-only attribute, or attribute that only makes
+          sense when the device is in a different state)
+
+  Other error conditions may be defined by individual device types.
+
+Gets/sets a specified piece of device configuration and/or state.  The
+semantics are device-specific.  See individual device documentation in
+the "devices" directory.  As with ONE_REG, the size of the data
+transferred is defined by the particular attribute.
+
+struct kvm_device_attr {
+	__u32	flags;		/* no flags currently defined */
+	__u32	group;		/* device-defined */
+	__u64	attr;		/* group-defined */
+	__u64	addr;		/* userspace address of attr data */
+};
+
+4.81 KVM_HAS_DEVICE_ATTR
+
+Capability: KVM_CAP_DEVICE_CTRL
+Type: device ioctl
+Parameters: struct kvm_device_attr
+Returns: 0 on success, -1 on error
+Errors:
+  ENXIO:  The group or attribute is unknown/unsupported for this device
+
+Tests whether a device supports a particular attribute.  A successful
+return indicates the attribute is implemented.  It does not necessarily
+indicate that the attribute can be read or written in the device's
+current state.  "addr" is ignored.
 
 4.77 KVM_ARM_VCPU_INIT
 
diff --git a/Documentation/virtual/kvm/devices/README b/Documentation/virtual/kvm/devices/README
new file mode 100644
index 0000000..34a6983
--- /dev/null
+++ b/Documentation/virtual/kvm/devices/README
@@ -0,0 +1 @@
+This directory contains specific device bindings for KVM_CAP_DEVICE_CTRL.
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index dcef724..6dab6b5 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1064,6 +1064,41 @@ static inline bool kvm_check_request(int req, struct kvm_vcpu *vcpu)
 
 extern bool kvm_rebooting;
 
+struct kvm_device_ops;
+
+struct kvm_device {
+	struct kvm_device_ops *ops;
+	struct kvm *kvm;
+	atomic_t users;
+	void *private;
+};
+
+/* create, destroy, and name are mandatory */
+struct kvm_device_ops {
+	const char *name;
+	int (*create)(struct kvm_device *dev, u32 type);
+
+	/*
+	 * Destroy is responsible for freeing dev.
+	 *
+	 * Destroy may be called before or after destructors are called
+	 * on emulated I/O regions, depending on whether a reference is
+	 * held by a vcpu or other kvm component that gets destroyed
+	 * after the emulated I/O.
+	 */
+	void (*destroy)(struct kvm_device *dev);
+
+	int (*set_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
+	int (*get_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
+	int (*has_attr)(struct kvm_device *dev, struct kvm_device_attr *attr);
+	long (*ioctl)(struct kvm_device *dev, unsigned int ioctl,
+		      unsigned long arg);
+};
+
+void kvm_device_get(struct kvm_device *dev);
+void kvm_device_put(struct kvm_device *dev);
+struct kvm_device *kvm_device_from_filp(struct file *filp);
+
 #ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
 
 static inline void kvm_vcpu_set_in_spin_loop(struct kvm_vcpu *vcpu, bool val)
diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h
index c741902..38a0be0 100644
--- a/include/uapi/linux/kvm.h
+++ b/include/uapi/linux/kvm.h
@@ -666,6 +666,7 @@ struct kvm_ppc_smmu_info {
 #define KVM_CAP_PPC_EPR 86
 #define KVM_CAP_ARM_PSCI 87
 #define KVM_CAP_ARM_SET_DEVICE_ADDR 88
+#define KVM_CAP_DEVICE_CTRL 89
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -819,6 +820,24 @@ struct kvm_arm_device_addr {
 };
 
 /*
+ * Device control API, available with KVM_CAP_DEVICE_CTRL
+ */
+#define KVM_CREATE_DEVICE_TEST		1
+
+struct kvm_create_device {
+	__u32	type;	/* in: KVM_DEV_TYPE_xxx */
+	__u32	fd;	/* out: device handle */
+	__u32	flags;	/* in: KVM_CREATE_DEVICE_xxx */
+};
+
+struct kvm_device_attr {
+	__u32	flags;		/* no flags currently defined */
+	__u32	group;		/* device-defined */
+	__u64	attr;		/* group-defined */
+	__u64	addr;		/* userspace address of attr data */
+};
+
+/*
  * ioctls for VM fds
  */
 #define KVM_SET_MEMORY_REGION     _IOW(KVMIO,  0x40, struct kvm_memory_region)
@@ -906,6 +925,14 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_ARM_SET_DEVICE_ADDR */
 #define KVM_ARM_SET_DEVICE_ADDR	  _IOW(KVMIO,  0xab, struct kvm_arm_device_addr)
 
+/* ioctl for vm fd */
+#define KVM_CREATE_DEVICE	  _IOWR(KVMIO,  0xe0, struct kvm_create_device)
+
+/* ioctls for fds returned by KVM_CREATE_DEVICE */
+#define KVM_SET_DEVICE_ATTR	  _IOW(KVMIO,  0xe1, struct kvm_device_attr)
+#define KVM_GET_DEVICE_ATTR	  _IOW(KVMIO,  0xe2, struct kvm_device_attr)
+#define KVM_HAS_DEVICE_ATTR	  _IOW(KVMIO,  0xe3, struct kvm_device_attr)
+
 /*
  * ioctls for vcpu fds
  */
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index f9492f3..5f0d78c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2159,6 +2159,117 @@ out:
 }
 #endif
 
+static int kvm_device_ioctl_attr(struct kvm_device *dev,
+				 int (*accessor)(struct kvm_device *dev,
+						 struct kvm_device_attr *attr),
+				 unsigned long arg)
+{
+	struct kvm_device_attr attr;
+
+	if (!accessor)
+		return -EPERM;
+
+	if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
+		return -EFAULT;
+
+	return accessor(dev, &attr);
+}
+
+static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
+			     unsigned long arg)
+{
+	struct kvm_device *dev = filp->private_data;
+
+	switch (ioctl) {
+	case KVM_SET_DEVICE_ATTR:
+		return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
+	case KVM_GET_DEVICE_ATTR:
+		return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
+	case KVM_HAS_DEVICE_ATTR:
+		return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
+	default:
+		if (dev->ops->ioctl)
+			return dev->ops->ioctl(dev, ioctl, arg);
+
+		return -ENOTTY;
+	}
+}
+
+void kvm_device_get(struct kvm_device *dev)
+{
+	atomic_inc(&dev->users);
+}
+
+void kvm_device_put(struct kvm_device *dev)
+{
+	if (atomic_dec_and_test(&dev->users))
+		dev->ops->destroy(dev);
+}
+
+static int kvm_device_release(struct inode *inode, struct file *filp)
+{
+	struct kvm_device *dev = filp->private_data;
+	struct kvm *kvm = dev->kvm;
+
+	kvm_device_put(dev);
+	kvm_put_kvm(kvm);
+	return 0;
+}
+
+static const struct file_operations kvm_device_fops = {
+	.unlocked_ioctl = kvm_device_ioctl,
+	.release = kvm_device_release,
+};
+
+struct kvm_device *kvm_device_from_filp(struct file *filp)
+{
+	if (filp->f_op != &kvm_device_fops)
+		return NULL;
+
+	return filp->private_data;
+}
+
+static int kvm_ioctl_create_device(struct kvm *kvm,
+				   struct kvm_create_device *cd)
+{
+	struct kvm_device_ops *ops = NULL;
+	struct kvm_device *dev;
+	bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
+	int ret;
+
+	switch (cd->type) {
+	default:
+		return -ENODEV;
+	}
+
+	if (test)
+		return 0;
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+
+	dev->ops = ops;
+	dev->kvm = kvm;
+	atomic_set(&dev->users, 1);
+
+	ret = ops->create(dev, cd->type);
+	if (ret < 0) {
+		kfree(dev);
+		return ret;
+	}
+
+	ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR);
+	if (ret < 0) {
+		ops->destroy(dev);
+		return ret;
+	}
+
+	kvm_get_kvm(kvm);
+	cd->fd = ret;
+	return 0;
+}
+
 static long kvm_vm_ioctl(struct file *filp,
 			   unsigned int ioctl, unsigned long arg)
 {
@@ -2304,6 +2415,24 @@ static long kvm_vm_ioctl(struct file *filp,
 		break;
 	}
 #endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
+	case KVM_CREATE_DEVICE: {
+		struct kvm_create_device cd;
+
+		r = -EFAULT;
+		if (copy_from_user(&cd, argp, sizeof(cd)))
+			goto out;
+
+		r = kvm_ioctl_create_device(kvm, &cd);
+		if (r)
+			goto out;
+
+		r = -EFAULT;
+		if (copy_to_user(argp, &cd, sizeof(cd)))
+			goto out;
+
+		r = 0;
+		break;
+	}
 	default:
 		r = kvm_arch_vm_ioctl(filp, ioctl, arg);
 		if (r = -ENOTTY)
-- 
1.6.0.2


  parent reply	other threads:[~2013-04-26  8:24 UTC|newest]

Thread overview: 180+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-26  8:24 [PATCH 00/20] KVM: PPC: In-kernel MPIC support with irqfd v4 Alexander Graf
2013-04-26  8:24 ` Alexander Graf
2013-04-26  8:24 ` [PATCH 01/20] KVM: Add KVM_IRQCHIP_NUM_PINS in addition to KVM_IOAPIC_NUM_PINS Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 02/20] KVM: Introduce CONFIG_HAVE_KVM_IRQ_ROUTING Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 03/20] KVM: Drop __KVM_HAVE_IOAPIC condition on irq routing Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 04/20] KVM: Remove kvm_get_intr_delivery_bitmask Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 05/20] KVM: Move irq routing to generic code Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 06/20] KVM: Extract generic irqchip logic into irqchip.c Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 07/20] KVM: Move irq routing setup to irqchip.c Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 08/20] KVM: Move irqfd resample cap handling to generic code Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` Alexander Graf [this message]
2013-04-26  8:24   ` [PATCH 09/20] kvm: add device control API Alexander Graf
2013-04-26  8:24 ` [PATCH 10/20] kvm/ppc/mpic: import hw/openpic.c from QEMU Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 11/20] kvm/ppc/mpic: remove some obviously unneeded code Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 12/20] kvm/ppc/mpic: adapt to kernel style and environment Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 13/20] kvm/ppc/mpic: in-kernel MPIC emulation Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 14/20] kvm/ppc/mpic: add KVM_CAP_IRQ_MPIC Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 15/20] KVM: PPC: Support irq routing and irqfd for in-kernel MPIC Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 16/20] KVM: PPC: MPIC: Add support for KVM_IRQ_LINE Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 17/20] KVM: PPC: MPIC: Restrict to e500 platforms Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 18/20] KVM: IA64: Carry non-ia64 changes into ia64 Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 19/20] kvm: destroy emulated devices on VM exit Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26  8:24 ` [PATCH 20/20] kvm/ppc/mpic: Eliminate mmio_mapped Alexander Graf
2013-04-26  8:24   ` Alexander Graf
2013-04-26 18:29 [PATCH 00/20] KVM: PPC: In-kernel MPIC support with irqfd v4 Alexander Graf
2013-04-26 18:29 ` Alexander Graf
2013-04-26 18:29 ` [PATCH 01/20] KVM: Add KVM_IRQCHIP_NUM_PINS in addition to KVM_IOAPIC_NUM_PINS Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 02/20] KVM: Introduce CONFIG_HAVE_KVM_IRQ_ROUTING Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 03/20] KVM: Drop __KVM_HAVE_IOAPIC condition on irq routing Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 04/20] KVM: Remove kvm_get_intr_delivery_bitmask Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 05/20] KVM: Move irq routing to generic code Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 06/20] KVM: Extract generic irqchip logic into irqchip.c Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 07/20] KVM: Move irq routing setup to irqchip.c Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 08/20] KVM: Move irqfd resample cap handling to generic code Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 09/20] kvm: add device control API Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 10/20] kvm/ppc/mpic: import hw/openpic.c from QEMU Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 11/20] kvm/ppc/mpic: remove some obviously unneeded code Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 12/20] kvm/ppc/mpic: adapt to kernel style and environment Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 13/20] kvm/ppc/mpic: in-kernel MPIC emulation Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 14/20] kvm/ppc/mpic: add KVM_CAP_IRQ_MPIC Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 15/20] KVM: PPC: Support irq routing and irqfd for in-kernel MPIC Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 16/20] KVM: PPC: MPIC: Add support for KVM_IRQ_LINE Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 17/20] KVM: PPC: MPIC: Restrict to e500 platforms Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 18/20] KVM: IA64: Carry non-ia64 changes into ia64 Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 19/20] kvm: destroy emulated devices on VM exit Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 20/20] kvm/ppc/mpic: Eliminate mmio_mapped Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PULL 00/42] ppc patch queue 2013-04-26 for 3.10 Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-28 10:05   ` Gleb Natapov
2013-04-28 10:05     ` Gleb Natapov
2013-04-26 18:29 ` [PATCH 01/42] KVM: PPC: cache flush for kernel managed pages Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 02/42] KVM: PPC: debug stub interface parameter defined Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 03/42] Rename EMULATE_DO_PAPR to EMULATE_EXIT_USER Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:29 ` [PATCH 04/42] KVM: extend EMULATE_EXIT_USER to support different exit reasons Alexander Graf
2013-04-26 18:29   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 05/42] booke: exit to user space if emulator request Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 06/42] KVM: PPC: Book3E: Refactor ONE_REG ioctl implementation Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 07/42] KVM: PPC: e500: Expose MMU registers via ONE_REG Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 08/42] KVM: PPC: e500: Move vcpu's MMU configuration to dedicated functions Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 09/42] KVM: PPC: e500: Add support for TLBnPS registers Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 10/42] KVM: PPC: e500: Add support for EPTCFG register Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 11/42] KVM: PPC: e500: Remove E.PT and E.HV.LRAT categories from VCPUs Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 12/42] KVM: PPC: e500mc: Enable e6500 cores Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 13/42] KVM: PPC: e500: Add e6500 core to Kconfig description Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 14/42] KVM: PPC: Book3S HV: Make HPT reading code notice R/C bit changes Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 15/42] KVM: PPC: Book3S HV: Report VPA and DTL modifications in dirty map Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 16/42] KVM: Add KVM_IRQCHIP_NUM_PINS in addition to KVM_IOAPIC_NUM_PINS Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-28  5:41   ` Gleb Natapov
2013-04-28  5:41     ` Gleb Natapov
2013-04-28  9:46     ` Alexander Graf
2013-04-28  9:46       ` Alexander Graf
2013-04-26 18:30 ` [PATCH 17/42] KVM: Introduce CONFIG_HAVE_KVM_IRQ_ROUTING Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 18/42] KVM: Drop __KVM_HAVE_IOAPIC condition on irq routing Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 19/42] KVM: Remove kvm_get_intr_delivery_bitmask Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 20/42] KVM: Move irq routing to generic code Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 21/42] KVM: Extract generic irqchip logic into irqchip.c Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 22/42] KVM: Move irq routing setup to irqchip.c Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 23/42] KVM: Move irqfd resample cap handling to generic code Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 24/42] kvm: add device control API Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 25/42] kvm/ppc/mpic: import hw/openpic.c from QEMU Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 26/42] kvm/ppc/mpic: remove some obviously unneeded code Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 27/42] kvm/ppc/mpic: adapt to kernel style and environment Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 28/42] kvm/ppc/mpic: in-kernel MPIC emulation Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 29/42] kvm/ppc/mpic: add KVM_CAP_IRQ_MPIC Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 30/42] KVM: PPC: Support irq routing and irqfd for in-kernel MPIC Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 31/42] KVM: PPC: MPIC: Add support for KVM_IRQ_LINE Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 32/42] KVM: PPC: MPIC: Restrict to e500 platforms Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 33/42] KVM: IA64: Carry non-ia64 changes into ia64 Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 34/42] kvm: destroy emulated devices on VM exit Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-28  5:43   ` Gleb Natapov
2013-04-28  5:43     ` Gleb Natapov
2013-04-28  9:47     ` Alexander Graf
2013-04-28  9:47       ` Alexander Graf
2013-04-26 18:30 ` [PATCH 35/42] kvm/ppc/mpic: Eliminate mmio_mapped Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 36/42] KVM: PPC: Book3S: Add infrastructure to implement kernel-side RTAS calls Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 37/42] KVM: PPC: Book3S: Add kernel emulation for the XICS interrupt controller Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 38/42] KVM: PPC: Book3S HV: Speed up wakeups of CPUs on HV KVM Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 39/42] KVM: PPC: Book3S HV: Add support for real mode ICP in XICS emulation Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 40/42] KVM: PPC: Book3S HV: Improve real-mode handling of external interrupts Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 41/42] KVM: PPC: Book3S: Add support for ibm,int-on/off RTAS calls Alexander Graf
2013-04-26 18:30   ` Alexander Graf
2013-04-26 18:30 ` [PATCH 42/42] KVM: PPC: Book3S: Facilities to save/restore XICS presentation ctrler state Alexander Graf
2013-04-26 18:30   ` Alexander Graf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1366964689-27567-10-git-send-email-agraf@suse.de \
    --to=agraf@suse.de \
    --cc=gleb@redhat.com \
    --cc=kvm-ppc@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=scottwood@freescale.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.