Skip to contentSkip to conversation

User controlled virtual machines

PATCH In review Carsten Otte 3 participants 24 messages Dec 1, 2011 – Dec 2, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover User controlled virtual machines

Hi Avi, Hi Marcelo,

this patch series introduces an interface to allow a privileged userspace program to control a KVM virtual machine. The interface is intended for use by a machine simulator called CECSIM that can simulate an entire mainframe machine with nested virtualization and I/O for the purpose of testing and debugging its firmware prior to availability of silicon. This patchset allows for concurrent use of the KVM device driver to drive both regular and user controlled virtual machines at the same time. I would kindly like to ask for review and inclusion of these patches.

with kind regards,
Carsten

#1 lore raw

PATCH 1/12 kvm-s390: ioctl to switch to user controlled virtual machines

This patch introduces a new config option for user controlled kernel virtual machines. It introduces a new ioctl named KVM_S390_ENABLE_UCONTROL on the kvm file descriptor which allows for a one way transition from a regular kernel virtual machine to a user controlled virtual machine. The virtual machine must not have any memory slots installed, and no virtual cpus defined. Note that the user controlled virtual machines require CAP_SYS_ADMIN privileges.

---

---
 arch/s390/kvm/Kconfig    |    9 +++++++++
 arch/s390/kvm/kvm-s390.c |   30 ++++++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.h |   10 ++++++++++
 include/linux/kvm.h      |    3 +++
 4 files changed, 52 insertions(+)
arch/s390/kvm/Kconfig +9 -0
@@ -34,6 +34,15 @@ config KVM
 
 	  If unsure, say N.
 
+config KVM_UCONTROL
+	bool "Userspace controlled virtual machines"
+	depends on KVM
+	---help---
+	  Allow CAP_SYS_ADMIN users to create KVM virtual machines that are
+	  controlled by userspace.
+
+	  If unsure, say N.
+
 # OK, it's a little counter-intuitive to do this, but it puts it neatly under
 # the virtualization menu.
 source drivers/vhost/Kconfig
arch/s390/kvm/kvm-s390.c +30 -0
@@ -147,6 +147,32 @@ int kvm_vm_ioctl_get_dirty_log(struct kv
 	return 0;
 }
 
+int kvm_s390_enable_ucontrol(struct kvm *kvm)
+{
+#ifdef CONFIG_KVM_UCONTROL
+	int i;
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	for (i = 0; i < KVM_MAX_VCPUS; i++)
+		if (kvm->vcpus[i])
+			return -EINVAL;
+
+	if (kvm->memslots->nmemslots)
+		return -EPERM;
+
+	if (kvm->arch.gmap)
+		gmap_free(kvm->arch.gmap);
+
+	kvm->arch.gmap = NULL;
+
+	return 0;
+#else
+	return -ENOTTY;
+#endif
+}
+
 long kvm_arch_vm_ioctl(struct file *filp,
 		       unsigned int ioctl, unsigned long arg)
 {
@@ -164,6 +190,10 @@ long kvm_arch_vm_ioctl(struct file *filp
 		r = kvm_s390_inject_vm(kvm, &s390int);
 		break;
 	}
+	case KVM_S390_ENABLE_UCONTROL: {
+		r = kvm_s390_enable_ucontrol(kvm);
+		break;
+	}
 	default:
 		r = -ENOTTY;
 	}
arch/s390/kvm/kvm-s390.h +10 -0
@@ -47,6 +47,16 @@ static inline int __cpu_is_stopped(struc
 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOP_INT;
 }
 
+static inline int kvm_is_ucontrol(struct kvm *kvm)
+{
+#ifdef CONFIG_KVM_UCONTROL
+	if (kvm->arch.gmap)
+		return 0;
+	return 1;
+#else
+	return 0;
+#endif
+}
 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu);
 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer);
 void kvm_s390_tasklet(unsigned long parm);
include/linux/kvm.h +3 -0
@@ -654,6 +654,9 @@ struct kvm_clock_data {
 					struct kvm_userspace_memory_region)
 #define KVM_SET_TSS_ADDR          _IO(KVMIO,   0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
+/* enable ucontrol for s390 */
+#define KVM_S390_ENABLE_UCONTROL  _IO(KVMIO,  0x49)
+
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP        _IO(KVMIO,   0x60)
 #define KVM_IRQ_LINE              _IOW(KVMIO,  0x61, struct kvm_irq_level)
#2 lore raw

Re: [patch 01/12] [PATCH] kvm-s390: ioctl to switch to user controlled virtual machines

Quoted 9 lines · Carsten Otte

This patch introduces a new config option for user controlled kernel virtual machines. It introduces a new ioctl named KVM_S390_ENABLE_UCONTROL on the kvm file descriptor which allows for a one way transition from a regular kernel virtual machine to a user controlled virtual machine. The virtual machine must not have any memory slots installed, and no virtual cpus defined. Note that the user controlled virtual machines require CAP_SYS_ADMIN privileges.

Quoted 8 lines

---

---
 arch/s390/kvm/Kconfig    |    9 +++++++++
 arch/s390/kvm/kvm-s390.c |   30 ++++++++++++++++++++++++++++++
 arch/s390/kvm/kvm-s390.h |   10 ++++++++++
 include/linux/kvm.h      |    3 +++
 4 files changed, 52 insertions(+)

Documentation/virtual/kvm/api.txt +++++++++++++++++++

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +4 -0
+int kvm_s390_enable_ucontrol(struct kvm *kvm)
+{
+#ifdef CONFIG_KVM_UCONTROL
+	int i;

arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_get_dirty_log(struct kv · line 163

Quoted 10 lines
arch/s390/kvm/kvm-s390.c +10 -0
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
+	for (i = 0; i < KVM_MAX_VCPUS; i++)
+		if (kvm->vcpus[i])
+			return -EINVAL;
+
+	if (kvm->memslots->nmemslots)
+		return -EPERM;

Why different errors?

arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_get_dirty_log(struct kv · line 168

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +5 -0
+
+	if (kvm->arch.gmap)
+		gmap_free(kvm->arch.gmap);
+
+	kvm->arch.gmap = NULL;

Locking?

What happens if a vcpu is created afterwards?

I guess you don't mind too much since this is a privileged interface for a single purpose.

include/linux/kvm.h struct kvm_clock_data { · line 662

Quoted 13 lines
include/linux/kvm.h +3 -0
@@ -654,6 +654,9 @@ struct kvm_clock_data {
 					struct kvm_userspace_memory_region)
 #define KVM_SET_TSS_ADDR          _IO(KVMIO,   0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
+/* enable ucontrol for s390 */
+#define KVM_S390_ENABLE_UCONTROL  _IO(KVMIO,  0x49)
+
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP        _IO(KVMIO,   0x60)
 #define KVM_IRQ_LINE              _IOW(KVMIO,  0x61, struct kvm_irq_level)

#define KVM_CAP_S390_UCONTROL

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

Re: [patch 01/12] [PATCH] kvm-s390: ioctl to switch to user controlled virtual machines

Quoted 11 lines · Avi Kivity

arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_get_dirty_log(struct kv · line 168

Quoted 6 lines
arch/s390/kvm/kvm-s390.c +5 -0
+
+	if (kvm->arch.gmap)
+		gmap_free(kvm->arch.gmap);
+
+	kvm->arch.gmap = NULL;

Locking?

What happens if a vcpu is created afterwards?

Having read the code, I think you can repurpose the argument of KVM_CREATE_VM to be a vm type; with 0 being a normal vm, and 1 being your new monster. The code already checks that the argument is zero, so we're safe there.

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw

Re: [patch 01/12] [PATCH] kvm-s390: ioctl to switch to user controlled virtual machines

In reply to Avi Kivity
Quoted 8 lines · On Thu, 01 Dec 2011 15:15:03 +0200 Avi Kivity <[email protected]> wrote:

arch/s390/kvm/kvm-s390.c int kvm_vm_ioctl_get_dirty_log(struct kv · line 168

Quoted 5 lines
arch/s390/kvm/kvm-s390.c +5 -0
+
+	if (kvm->arch.gmap)
+		gmap_free(kvm->arch.gmap);
+
+	kvm->arch.gmap = NULL;

Locking?

Quoted 4 lines

What happens if a vcpu is created afterwards?

I guess you don't mind too much since this is a privileged interface for a single purpose.

That is indeed a race. A malicious user space could create a new cpu with KVM_CREATE_VCPU on another thread after the for loop checked that there are no VCPUs. The new VCPU could then pick up the kvm->arch.gmap and use it while the caller of KVM_S390_ENABLE_UCONTROL frees the structure. The kvm_s390_enable_ucontrol function needs to lock with the kvm->lock mutex.

Signature · 4 lines
blue skies,
   Martin.

"Reality continues to ruin my life." - Calvin.
#5 lore raw

PATCH 2/12 kvm-s390-ucontrol: per vcpu address spaces

In reply to Carsten Otte

This patch introduces two ioctls for virtual cpus, that are only valid for kernel virtual machines that are controlled by userspace. Each virtual cpu has its individual address space in this mode of operation, and each address space is backed by the gmap implementation just like the address space for regular KVM guests. KVM_S390_UCAS_MAP allows to map a part of the user's virtual address space to the vcpu. Starting offset and length in both the user and the vcpu address space need to be aligned to 1M. KVM_S390_UCAS_UNMAP can be used to unmap a range of memory from a virtual cpu in a similar way.

---

---
 arch/s390/kvm/kvm-s390.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/kvm.h      |    7 ++++++
 2 files changed, 56 insertions(+), 1 deletion(-)
arch/s390/kvm/kvm-s390.c +49 -1
@@ -249,6 +249,10 @@ void kvm_arch_vcpu_destroy(struct kvm_vc
 		(__u64) vcpu->arch.sie_block)
 		vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
 	smp_mb();
+
+	if (kvm_is_ucontrol(vcpu->kvm))
+		gmap_free(vcpu->arch.gmap);
+
 	free_page((unsigned long)(vcpu->arch.sie_block));
 	kvm_vcpu_uninit(vcpu);
 	kfree(vcpu);
@@ -279,12 +283,20 @@ void kvm_arch_destroy_vm(struct kvm *kvm
 	kvm_free_vcpus(kvm);
 	free_page((unsigned long)(kvm->arch.sca));
 	debug_unregister(kvm->arch.dbf);
-	gmap_free(kvm->arch.gmap);
+	if (!kvm_is_ucontrol(kvm))
+		gmap_free(kvm->arch.gmap);
 }
 
 /* Section: vcpu related */
 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 {
+	if (kvm_is_ucontrol(vcpu->kvm)) {
+		vcpu->arch.gmap = gmap_alloc(current->mm);
+		if (!vcpu->arch.gmap)
+			return -ENOMEM;
+		return 0;
+	}
+
 	vcpu->arch.gmap = vcpu->kvm->arch.gmap;
 	return 0;
 }
@@ -703,6 +715,42 @@ long kvm_arch_vcpu_ioctl(struct file *fi
 	case KVM_S390_INITIAL_RESET:
 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
 		break;
+#ifdef CONFIG_KVM_UCONTROL
+	case KVM_S390_UCAS_MAP: {
+		struct kvm_s390_ucas_mapping ucasmap;
+
+		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
+			r = -EFAULT;
+			break;
+		}
+
+		if (!kvm_is_ucontrol(vcpu->kvm)) {
+			r = -EINVAL;
+			break;
+		}
+
+		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
+				     ucasmap.vcpu_addr, ucasmap.length);
+		break;
+	}
+	case KVM_S390_UCAS_UNMAP: {
+		struct kvm_s390_ucas_mapping ucasmap;
+
+		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
+			r = -EFAULT;
+			break;
+		}
+
+		if (!kvm_is_ucontrol(vcpu->kvm)) {
+			r = -EINVAL;
+			break;
+		}
+
+		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
+			ucasmap.length);
+		break;
+	}
+#endif
 	default:
 		r = -EINVAL;
 	}
include/linux/kvm.h +7 -0
@@ -655,7 +655,14 @@ struct kvm_clock_data {
 #define KVM_SET_TSS_ADDR          _IO(KVMIO,   0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
 /* enable ucontrol for s390 */
+struct kvm_s390_ucas_mapping {
+	unsigned long user_addr;
+	unsigned long vcpu_addr;
+	unsigned long length;
+};
 #define KVM_S390_ENABLE_UCONTROL  _IO(KVMIO,  0x49)
+#define KVM_S390_UCAS_MAP        _IOW(KVMIO, 0x50, struct kvm_s390_ucas_mapping)
+#define KVM_S390_UCAS_UNMAP      _IOW(KVMIO, 0x51, struct kvm_s390_ucas_mapping)
 
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP        _IO(KVMIO,   0x60)
#6 lore raw

Re: [patch 02/12] [PATCH] kvm-s390-ucontrol: per vcpu address spaces

Quoted 15 lines · Carsten Otte

This patch introduces two ioctls for virtual cpus, that are only valid for kernel virtual machines that are controlled by userspace. Each virtual cpu has its individual address space in this mode of operation, and each address space is backed by the gmap implementation just like the address space for regular KVM guests. KVM_S390_UCAS_MAP allows to map a part of the user's virtual address space to the vcpu. Starting offset and length in both the user and the vcpu address space need to be aligned to 1M. KVM_S390_UCAS_UNMAP can be used to unmap a range of memory from a virtual cpu in a similar way.

(quoted diff) +0 -0
 	}

include/linux/kvm.h struct kvm_clock_data { · line 662

Quoted 8 lines
include/linux/kvm.h +5 -0
@@ -655,7 +655,14 @@ struct kvm_clock_data {
 #define KVM_SET_TSS_ADDR          _IO(KVMIO,   0x47)
 #define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO,  0x48, __u64)
 /* enable ucontrol for s390 */
+struct kvm_s390_ucas_mapping {
+	unsigned long user_addr;
+	unsigned long vcpu_addr;
+	unsigned long length;
+};

Do you have 32/64 issues on s390? This struct changes size with host userspace bitness.

Quoted 5 lines
include/linux/kvm.h +2 -0
 #define KVM_S390_ENABLE_UCONTROL  _IO(KVMIO,  0x49)
+#define KVM_S390_UCAS_MAP        _IOW(KVMIO, 0x50, struct kvm_s390_ucas_mapping)
+#define KVM_S390_UCAS_UNMAP      _IOW(KVMIO, 0x51, struct kvm_s390_ucas_mapping)
 
 
Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw

PATCH 3/12 kvm-s390-ucontrol: export page faults to user

In reply to Carsten Otte

This patch introduces a new exit reason in the kvm_run structure named KVM_EXIT_UCONTROL. This exit indicates, that a virtual cpu has regognized a fault on the host page table. The idea is that userspace can handle this fault by mapping memory at the fault location into the cpu's address space and then continue to run the virtual cpu.

---

arch/s390/kvm/kvm-s390.c +27 -5
Index: linux-2.5-cecsim/arch/s390/kvm/kvm-s390.c
===================================================================
@@ -509,8 +509,10 @@ int kvm_arch_vcpu_ioctl_set_mpstate(stru
 	return -EINVAL; /* not implemented yet */
 }
 
-static void __vcpu_run(struct kvm_vcpu *vcpu)
+static int __vcpu_run(struct kvm_vcpu *vcpu)
 {
+	int rc;
+
 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14], 16);
 
 	if (need_resched())
@@ -527,9 +529,15 @@ static void __vcpu_run(struct kvm_vcpu *
 	local_irq_enable();
 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
-	if (sie64a(vcpu->arch.sie_block, vcpu->arch.guest_gprs)) {
-		VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
-		kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
+	rc = sie64a(vcpu->arch.sie_block, vcpu->arch.guest_gprs);
+	if (rc) {
+		if (kvm_is_ucontrol(vcpu->kvm)) {
+			rc = SIE_INTERCEPT_UCONTROL;
+		} else {
+			VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
+			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
+			rc = 0;
+		}
 	}
 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
 		   vcpu->arch.sie_block->icptcode);
@@ -538,6 +546,7 @@ static void __vcpu_run(struct kvm_vcpu *
 	local_irq_enable();
 
 	memcpy(&vcpu->arch.guest_gprs[14], &vcpu->arch.sie_block->gg14, 16);
+	return rc;
 }
 
 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
@@ -558,6 +567,7 @@ rerun_vcpu:
 	case KVM_EXIT_UNKNOWN:
 	case KVM_EXIT_INTR:
 	case KVM_EXIT_S390_RESET:
+	case KVM_EXIT_UCONTROL:
 		break;
 	default:
 		BUG();
@@ -569,7 +579,9 @@ rerun_vcpu:
 	might_fault();
 
 	do {
-		__vcpu_run(vcpu);
+		rc = __vcpu_run(vcpu);
+		if (rc)
+			break;
 		rc = kvm_handle_sie_intercept(vcpu);
 	} while (!signal_pending(current) && !rc);
 
@@ -581,6 +593,16 @@ rerun_vcpu:
 		rc = -EINTR;
 	}
 
+#ifdef CONFIG_KVM_UCONTROL
+	if (rc == SIE_INTERCEPT_UCONTROL) {
+		kvm_run->exit_reason = KVM_EXIT_UCONTROL;
+		kvm_run->s390_ucontrol.trans_exc_code =
+			current->thread.gmap_addr;
+		kvm_run->s390_ucontrol.pgm_code = 0x10;
+		rc = 0;
+	}
+#endif
+
 	if (rc == -EOPNOTSUPP) {
 		/* intercept cannot be handled in-kernel, prepare kvm-run */
 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
arch/s390/kvm/kvm-s390.h +1 -0
Index: linux-2.5-cecsim/arch/s390/kvm/kvm-s390.h
===================================================================
@@ -26,6 +26,7 @@ typedef int (*intercept_handler_t)(struc
 
 /* negativ values are error codes, positive values for internal conditions */
 #define SIE_INTERCEPT_RERUNVCPU		(1<<0)
+#define SIE_INTERCEPT_UCONTROL		(1<<1)
 int kvm_handle_sie_intercept(struct kvm_vcpu *vcpu);
 
 #define VM_EVENT(d_kvm, d_loglevel, d_string, d_args...)\
include/linux/kvm.h +6 -0
Index: linux-2.5-cecsim/include/linux/kvm.h
===================================================================
@@ -162,6 +162,7 @@ struct kvm_pit_config {
 #define KVM_EXIT_INTERNAL_ERROR   17
 #define KVM_EXIT_OSI              18
 #define KVM_EXIT_PAPR_HCALL	  19
+#define KVM_EXIT_UCONTROL	  20
 
 /* For KVM_EXIT_INTERNAL_ERROR */
 #define KVM_INTERNAL_ERROR_EMULATION 1
@@ -249,6 +250,11 @@ struct kvm_run {
 #define KVM_S390_RESET_CPU_INIT  8
 #define KVM_S390_RESET_IPL       16
 		__u64 s390_reset_flags;
+		/* KVM_EXIT_UCONTROL */
+		struct {
+			__u64 trans_exc_code;
+			__u32 pgm_code;
+		} s390_ucontrol;
 		/* KVM_EXIT_DCR */
 		struct {
 			__u32 dcrn;
#8 lore raw

PATCH 4/12 kvm-s390-ucontrol: export SIE control block to user

In reply to Carsten Otte

This patch exports the SIE hardware control block to userspace via the mapping of the vcpu file descriptor.

---

---
 arch/s390/include/asm/kvm_host.h |    2 ++
 virt/kvm/kvm_main.c              |    5 +++++
 2 files changed, 7 insertions(+)
arch/s390/include/asm/kvm_host.h +2 -0
@@ -24,6 +24,8 @@
 /* memory slots that does not exposed to userspace */
 #define KVM_PRIVATE_MEM_SLOTS 4
 
+#define KVM_SIE_PAGE_OFFSET 1
+
 struct sca_entry {
 	atomic_t scn;
 	__u32	reserved;
virt/kvm/kvm_main.c +5 -0
@@ -1656,6 +1656,11 @@ static int kvm_vcpu_fault(struct vm_area
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
+#if defined(CONFIG_S390) && defined(CONFIG_KVM_UCONTROL)
+	else if ((vmf->pgoff == KVM_SIE_PAGE_OFFSET)
+		 && (!(vcpu->kvm->arch.gmap)))
+		page = virt_to_page(vcpu->arch.sie_block);
+#endif
 	else
 		return VM_FAULT_SIGBUS;
 	get_page(page);
#9 lore raw

Re: [patch 04/12] [PATCH] kvm-s390-ucontrol: export SIE control block to user

Quoted 13 lines · Carsten Otte

This patch exports the SIE hardware control block to userspace via the mapping of the vcpu file descriptor.

---

---
 arch/s390/include/asm/kvm_host.h |    2 ++
 virt/kvm/kvm_main.c              |    5 +++++
 2 files changed, 7 insertions(+)

arch/s390/include/asm/kvm_host.h line 28

Quoted 5 lines
arch/s390/include/asm/kvm_host.h +2 -0
@@ -24,6 +24,8 @@
 /* memory slots that does not exposed to userspace */
 #define KVM_PRIVATE_MEM_SLOTS 4
 
+#define KVM_SIE_PAGE_OFFSET 1
+

How does userspace get to know what this value is?

Coalesced mmio does this with the return value of KVM_CAP_COALESCED_MMIO. You can hardcode it, but in a user visible header please.

Quoted 6 lines
arch/s390/include/asm/kvm_host.h +0 -0
 struct sca_entry {
 	atomic_t scn;
 	__u32	reserved;

virt/kvm/kvm_main.c static int kvm_vcpu_fault(struct vm_area · line 1663

Quoted 8 lines
virt/kvm/kvm_main.c +5 -0
@@ -1656,6 +1656,11 @@ static int kvm_vcpu_fault(struct vm_area
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
+#if defined(CONFIG_S390) && defined(CONFIG_KVM_UCONTROL)
+	else if ((vmf->pgoff == KVM_SIE_PAGE_OFFSET)
+		 && (!(vcpu->kvm->arch.gmap)))
+		page = virt_to_page(vcpu->arch.sie_block);
+#endif

kvm_arch_vcpu_fault()

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw

Re: [patch 04/12] [PATCH] kvm-s390-ucontrol: export SIE control block to user

In reply to Carsten Otte

virt/kvm/kvm_main.c static int kvm_vcpu_fault(struct vm_area · line 1661

Quoted 8 lines · Carsten Otte

This patch exports the SIE hardware control block to userspace via the mapping of the vcpu file descriptor.

virt/kvm/kvm_main.c +3 -0
 	else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
 		page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
 #endif
+#if defined(CONFIG_S390) && defined(CONFIG_KVM_UCONTROL)
+	else if ((vmf->pgoff == KVM_SIE_PAGE_OFFSET)
+		 && (!(vcpu->kvm->arch.gmap)))

Is the second test "kvm_is_ucontrol()"?

Quoted 6 lines
virt/kvm/kvm_main.c +2 -0
+		page = virt_to_page(vcpu->arch.sie_block);
+#endif
 	else
 		return VM_FAULT_SIGBUS;
 	get_page(page);
Signature · 1 line
error compiling committee.c: too many arguments to function
#11 lore raw

Re: [patch 04/12] [PATCH] kvm-s390-ucontrol: export SIE control block to user

Quoted 11 lines · Avi Kivity

virt/kvm/kvm_main.c static int kvm_vcpu_fault(struct vm_area · line 1661

Quoted 8 lines · Carsten Otte

This patch exports the SIE hardware control block to userspace via the mapping of the vcpu file descriptor.
else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
#endif

virt/kvm/kvm_main.c +3 -0
+#if defined(CONFIG_S390)&&  defined(CONFIG_KVM_UCONTROL)
+	else if ((vmf->pgoff == KVM_SIE_PAGE_OFFSET)
+		&&  (!(vcpu->kvm->arch.gmap)))

Is the second test "kvm_is_ucontrol()"?

Yes it is, but that is defined in arch/s390/kvm/kvm-s390.h which I do not dare to include here.

#12 lore raw

Re: [patch 04/12] [PATCH] kvm-s390-ucontrol: export SIE control block to user

Quoted 11 lines · Carsten Otte
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Carsten Otte

This patch exports the SIE hardware control block to userspace via the mapping of the vcpu file descriptor.

      else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
          page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);

#endif

virt/kvm/kvm_main.c +3 -0
+#if defined(CONFIG_S390)&&  defined(CONFIG_KVM_UCONTROL)
+    else if ((vmf->pgoff == KVM_SIE_PAGE_OFFSET)
+        &&  (!(vcpu->kvm->arch.gmap)))
Quoted 4 lines
Quoted 1 line

Is the second test "kvm_is_ucontrol()"?

Yes it is, but that is defined in arch/s390/kvm/kvm-s390.h which I do not dare to include here.

Oh, so kvm_arch_vcpu_fault() will help. The pio stuff sets a bad example, but we needn't follow it.

Signature · 1 line
error compiling committee.c: too many arguments to function
#13 lore raw

PATCH 5/12 kvm-s390-ucontrol: disable in-kernel handling of SIE intercepts

In reply to Carsten Otte

This patch disables in-kernel handling of SIE intercepts for user controlled virtual machines. All intercepts are passed to userspace via KVM_EXIT_SIE exit reason just like SIE intercepts that cannot be handled in-kernel for regular KVM guests.

---

arch/s390/kvm/kvm-s390.c +4 -1
Index: linux-2.5-cecsim/arch/s390/kvm/kvm-s390.c
===================================================================
@@ -582,7 +582,10 @@ rerun_vcpu:
 		rc = __vcpu_run(vcpu);
 		if (rc)
 			break;
-		rc = kvm_handle_sie_intercept(vcpu);
+		if (kvm_is_ucontrol(vcpu->kvm))
+			rc = -EOPNOTSUPP;
+		else
+			rc = kvm_handle_sie_intercept(vcpu);
 	} while (!signal_pending(current) && !rc);
 
 	if (rc == SIE_INTERCEPT_RERUNVCPU)
#14 lore raw

PATCH 6/12 kvm-s390-ucontrol: disable in-kernel irq stack

In reply to Carsten Otte

This patch disables the in-kernel interrupt stack for KVM virtual machines that are controlled by user. Userspace has to take care of handling interrupts on its own.

---

arch/s390/kvm/kvm-s390.c +2 -1
Index: linux-2.5-cecsim/arch/s390/kvm/kvm-s390.c
===================================================================
@@ -521,7 +521,8 @@ static int __vcpu_run(struct kvm_vcpu *v
 	if (test_thread_flag(TIF_MCCK_PENDING))
 		s390_handle_mcck();
 
-	kvm_s390_deliver_pending_interrupts(vcpu);
+	if (!kvm_is_ucontrol(vcpu->kvm))
+		kvm_s390_deliver_pending_interrupts(vcpu);
 
 	vcpu->arch.sie_block->icptcode = 0;
 	local_irq_disable();
#15 lore raw

PATCH 7/12 kvm-s390-ucontrol: interface to inject faults on a vcpu page table

In reply to Carsten Otte

This patch allows the user to fault in pages on a virtual cpus address space for user controlled virtual machines. Typically this is superfluous because userspace can just create a mapping and let the kernel's page fault logic take are of it. There is one
exception: SIE won't start if the lowcore is not present. Normally the kernel takes care of this [handle_validity() in arch/s390/kvm/intercept.c] but since the kernel does not handle intercepts for user controlled virtual machines, userspace needs to be able to handle this condition.

---

---
 arch/s390/kvm/kvm-s390.c |    6 ++++++
 include/linux/kvm.h      |    1 +
 2 files changed, 7 insertions(+)
arch/s390/kvm/kvm-s390.c +6 -0
@@ -777,6 +777,12 @@ long kvm_arch_vcpu_ioctl(struct file *fi
 		break;
 	}
 #endif
+	case KVM_S390_VCPU_FAULT: {
+		r = gmap_fault(arg, vcpu->arch.gmap);
+		if (!IS_ERR_VALUE(r))
+			r = 0;
+		break;
+	}
 	default:
 		r = -EINVAL;
 	}
include/linux/kvm.h +1 -0
@@ -669,6 +669,7 @@ struct kvm_s390_ucas_mapping {
 #define KVM_S390_ENABLE_UCONTROL  _IO(KVMIO,  0x49)
 #define KVM_S390_UCAS_MAP        _IOW(KVMIO, 0x50, struct kvm_s390_ucas_mapping)
 #define KVM_S390_UCAS_UNMAP      _IOW(KVMIO, 0x51, struct kvm_s390_ucas_mapping)
+#define KVM_S390_VCPU_FAULT	 _IOW(KVMIO, 0x52, unsigned long)
 
 /* Device model IOC */
 #define KVM_CREATE_IRQCHIP        _IO(KVMIO,   0x60)
#16 lore raw

PATCH 8/12 kvm-s390-ucontrol: disable sca

In reply to Carsten Otte

This patch makes sure user controlled virtual machines do not use a system control area (sca). This is needed in order to create virtual machines with more cpus than the size of the sca [64].

---

arch/s390/kvm/kvm-s390.c +20 -10
Index: linux-2.5-cecsim/arch/s390/kvm/kvm-s390.c
===================================================================
@@ -244,10 +244,13 @@ out_err:
 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
 {
 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
-	clear_bit(63 - vcpu->vcpu_id, (unsigned long *) &vcpu->kvm->arch.sca->mcn);
-	if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
-		(__u64) vcpu->arch.sie_block)
-		vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
+	if (!kvm_is_ucontrol(vcpu->kvm)) {
+		clear_bit(63 - vcpu->vcpu_id,
+			  (unsigned long *) &vcpu->kvm->arch.sca->mcn);
+		if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
+		    (__u64) vcpu->arch.sie_block)
+			vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
+	}
 	smp_mb();
 
 	if (kvm_is_ucontrol(vcpu->kvm))
@@ -384,12 +387,19 @@ struct kvm_vcpu *kvm_arch_vcpu_create(st
 		goto out_free_cpu;
 
 	vcpu->arch.sie_block->icpua = id;
-	BUG_ON(!kvm->arch.sca);
-	if (!kvm->arch.sca->cpu[id].sda)
-		kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block;
-	vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32);
-	vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
-	set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
+	if (!kvm_is_ucontrol(kvm)) {
+		if (!kvm->arch.sca) {
+			WARN_ON_ONCE(1);
+			goto out_free_cpu;
+		}
+		if (!kvm->arch.sca->cpu[id].sda)
+			kvm->arch.sca->cpu[id].sda =
+				(__u64) vcpu->arch.sie_block;
+		vcpu->arch.sie_block->scaoh =
+			(__u32)(((__u64)kvm->arch.sca) >> 32);
+		vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
+		set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
+	}
 
 	spin_lock_init(&vcpu->arch.local_int.lock);
 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
#17 lore raw

PATCH 9/12 kvm-s390: fix assumption for KVM_MAX_VCPUS

In reply to Carsten Otte

This patch fixes definition of the idle_mask and the local_int array in kvm_s390_float_interrupt. Previous definition had 64 cpus max hardcoded instead of using KVM_MAX_VCPUS.

---

arch/s390/include/asm/kvm_host.h +3 -2
Index: linux-2.5-cecsim/arch/s390/include/asm/kvm_host.h
===================================================================
@@ -222,8 +222,9 @@ struct kvm_s390_float_interrupt {
 	struct list_head list;
 	atomic_t active;
 	int next_rr_cpu;
-	unsigned long idle_mask [(64 + sizeof(long) - 1) / sizeof(long)];
-	struct kvm_s390_local_interrupt *local_int[64];
+	unsigned long idle_mask[(KVM_MAX_VCPUS + sizeof(long) - 1)
+				/ sizeof(long)];
+	struct kvm_s390_local_interrupt *local_int[KVM_MAX_VCPUS];
 };
 
 
#18 lore raw

PATCH 10/12 kvm-s390: storage key interface

In reply to Carsten Otte

This patch introduces an interface to access the guest visible storage keys. It supports three operations that model the behavior that SSKE/ISKE/RRBE instructions would have if they were issued by the guest. These instructions are all documented in the z architecture principles of operation book.

---

arch/s390/include/asm/kvm_host.h +3 -0
Index: linux-2.5-cecsim/arch/s390/include/asm/kvm_host.h
===================================================================
@@ -25,6 +25,9 @@
 #define KVM_PRIVATE_MEM_SLOTS 4
 
 #define KVM_SIE_PAGE_OFFSET 1
+#define KVM_S390_KEYOP_SSKE 0x01
+#define KVM_S390_KEYOP_ISKE 0x02
+#define KVM_S390_KEYOP_RRBE 0x03
 
 struct sca_entry {
 	atomic_t scn;
arch/s390/kvm/kvm-s390.c +134 -3

147 lines not on this page · raw message

include/linux/kvm.h +7 -0
Index: linux-2.5-cecsim/include/linux/kvm.h
===================================================================
@@ -445,6 +445,13 @@ struct kvm_ppc_pvinfo {
 #define KVM_GET_MSR_INDEX_LIST    _IOWR(KVMIO, 0x02, struct kvm_msr_list)
 
 #define KVM_S390_ENABLE_SIE       _IO(KVMIO,   0x06)
+
+struct kvm_s390_keyop {
+	__u64 user_addr;
+	__u8  key;
+	__u8  operation;
+};
+#define KVM_S390_KEYOP            _IOWR(KVMIO,   0x09, struct kvm_s390_keyop)
 /*
  * Check if a kvm extension is available.  Argument is extension number,
  * return is 1 (yes) or 0 (no, sorry).
#19 lore raw

PATCH 11/12 kvm-s390-ucontrol: announce capability for user controlled vms

In reply to Carsten Otte

This patch announces a new capability KVM_CAP_S390_UCONTROL that indicates that kvm can now support virtual machines that are controlled by userspace.

---

---
 arch/s390/kvm/kvm-s390.c |    3 +++
 include/linux/kvm.h      |    1 +
 2 files changed, 4 insertions(+)
arch/s390/kvm/kvm-s390.c +3 -0
@@ -260,6 +260,9 @@ int kvm_dev_ioctl_check_extension(long e
 	case KVM_CAP_S390_PSW:
 	case KVM_CAP_S390_GMAP:
 	case KVM_CAP_SYNC_MMU:
+#ifdef CONFIG_KVM_UCONTROL
+	case KVM_CAP_S390_UCONTROL:
+#endif
 		r = 1;
 		break;
 	default:
include/linux/kvm.h +1 -0
@@ -570,6 +570,7 @@ struct kvm_s390_keyop {
 #define KVM_CAP_MAX_VCPUS 66       /* returns max vcpus per vm */
 #define KVM_CAP_PPC_PAPR 68
 #define KVM_CAP_S390_GMAP 71
+#define KVM_CAP_S390_UCONTROL 72
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
#20 lore raw

PATCH 12/12 From: Carsten Otte <[email protected]>

In reply to Carsten Otte

This patch fixes the return code of kvm_arch_vcpu_ioctl in case of an unkown ioctl number.

---

arch/s390/kvm/kvm-s390.c +1 -1
Index: linux-2.5-cecsim/arch/s390/kvm/kvm-s390.c
===================================================================
@@ -928,7 +928,7 @@ long kvm_arch_vcpu_ioctl(struct file *fi
 		break;
 	}
 	default:
-		r = -EINVAL;
+		r = -ENOTTY;
 	}
 	return r;
 }
#21 lore raw
In reply to Carsten Otte
Quoted 10 lines · Carsten Otte

Hi Avi, Hi Marcelo,

this patch series introduces an interface to allow a privileged userspace program to control a KVM virtual machine. The interface is intended for use by a machine simulator called CECSIM that can simulate an entire mainframe machine with nested virtualization and I/O for the purpose of testing and debugging its firmware prior to availability of silicon. This patchset allows for concurrent use of the KVM device driver to drive both regular and user controlled virtual machines at the same time. I would kindly like to ask for review and inclusion of these patches.

So, this moves the responsibility of handling intercepts from the kernel to userspace, yes?

How much of the kvm code continues to be active after this? Perhaps it makes sense to have a separate interface for this.

Signature · 1 line
error compiling committee.c: too many arguments to function
#22 lore raw
Quoted 12 lines · Avi Kivity
Quoted 10 lines · Carsten Otte

Hi Avi, Hi Marcelo,

this patch series introduces an interface to allow a privileged userspace program to control a KVM virtual machine. The interface is intended for use by a machine simulator called CECSIM that can simulate an entire mainframe machine with nested virtualization and I/O for the purpose of testing and debugging its firmware prior to availability of silicon. This patchset allows for concurrent use of the KVM device driver to drive both regular and user controlled virtual machines at the same time. I would kindly like to ask for review and inclusion of these patches.

Quoted 5 lines

So, this moves the responsibility of handling intercepts from the kernel to userspace, yes?

How much of the kvm code continues to be active after this? Perhaps it makes sense to have a separate interface for this.

Okay, I read the code and I even think I understood a little bit of it. In general the patches look okay, I had only minor comments. But please do document all the new interfaces.

Signature · 1 line
error compiling committee.c: too many arguments to function
#23 lore raw
Quoted 3 lines · Avi Kivity

Okay, I read the code and I even think I understood a little bit of it. In general the patches look okay, I had only minor comments. But please do document all the new interfaces.

Since when do we have api documentation? Tssj, kvm has grown up since last I looked ;-). Thanks for your feedback, all makes sense to me. I'll implement it and repost the series.

so long,
Carsten