xref: /linux/arch/s390/kvm/kvm-s390.c (revision 7c470539c95630c1f2a10f109e96f249730b75eb)
1b0c632dbSHeiko Carstens /*
2a53c8fabSHeiko Carstens  * hosting zSeries kernel virtual machines
3b0c632dbSHeiko Carstens  *
4628eb9b8SChristian Ehrhardt  * Copyright IBM Corp. 2008, 2009
5b0c632dbSHeiko Carstens  *
6b0c632dbSHeiko Carstens  * This program is free software; you can redistribute it and/or modify
7b0c632dbSHeiko Carstens  * it under the terms of the GNU General Public License (version 2 only)
8b0c632dbSHeiko Carstens  * as published by the Free Software Foundation.
9b0c632dbSHeiko Carstens  *
10b0c632dbSHeiko Carstens  *    Author(s): Carsten Otte <cotte@de.ibm.com>
11b0c632dbSHeiko Carstens  *               Christian Borntraeger <borntraeger@de.ibm.com>
12b0c632dbSHeiko Carstens  *               Heiko Carstens <heiko.carstens@de.ibm.com>
13628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
14b0c632dbSHeiko Carstens  */
15b0c632dbSHeiko Carstens 
16b0c632dbSHeiko Carstens #include <linux/compiler.h>
17b0c632dbSHeiko Carstens #include <linux/err.h>
18b0c632dbSHeiko Carstens #include <linux/fs.h>
19ca872302SChristian Borntraeger #include <linux/hrtimer.h>
20b0c632dbSHeiko Carstens #include <linux/init.h>
21b0c632dbSHeiko Carstens #include <linux/kvm.h>
22b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
23b0c632dbSHeiko Carstens #include <linux/module.h>
24b0c632dbSHeiko Carstens #include <linux/slab.h>
25ba5c1e9bSCarsten Otte #include <linux/timer.h>
26cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
27b0c632dbSHeiko Carstens #include <asm/lowcore.h>
28b0c632dbSHeiko Carstens #include <asm/pgtable.h>
29f5daba1dSHeiko Carstens #include <asm/nmi.h>
30a0616cdeSDavid Howells #include <asm/switch_to.h>
311526bf9cSChristian Borntraeger #include <asm/sclp.h>
328f2abe6aSChristian Borntraeger #include "kvm-s390.h"
33b0c632dbSHeiko Carstens #include "gaccess.h"
34b0c632dbSHeiko Carstens 
355786fffaSCornelia Huck #define CREATE_TRACE_POINTS
365786fffaSCornelia Huck #include "trace.h"
37ade38c31SCornelia Huck #include "trace-s390.h"
385786fffaSCornelia Huck 
39b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
40b0c632dbSHeiko Carstens 
41b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
42b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
430eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
448f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
458f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
468f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
478f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
48ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
49ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
50ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
51f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
52ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
53ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
547697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
55ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
56ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
57ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
58ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
59ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
60ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
61ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
62453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
63453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
64453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
65453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
66453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
67453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
68453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
69453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
70453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
71bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
725288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
73bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
747697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
755288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
765288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
775288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
785288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
795288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
80388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
81e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
8241628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
83b0c632dbSHeiko Carstens 	{ NULL }
84b0c632dbSHeiko Carstens };
85b0c632dbSHeiko Carstens 
86ef50f7acSChristian Borntraeger static unsigned long long *facilities;
872c70fe44SChristian Borntraeger static struct gmap_notifier gmap_notifier;
88b0c632dbSHeiko Carstens 
89b0c632dbSHeiko Carstens /* Section: not file related */
9010474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage)
91b0c632dbSHeiko Carstens {
92b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
9310474ae8SAlexander Graf 	return 0;
94b0c632dbSHeiko Carstens }
95b0c632dbSHeiko Carstens 
96b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage)
97b0c632dbSHeiko Carstens {
98b0c632dbSHeiko Carstens }
99b0c632dbSHeiko Carstens 
1002c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
1012c70fe44SChristian Borntraeger 
102b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
103b0c632dbSHeiko Carstens {
1042c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
1052c70fe44SChristian Borntraeger 	gmap_register_ipte_notifier(&gmap_notifier);
106b0c632dbSHeiko Carstens 	return 0;
107b0c632dbSHeiko Carstens }
108b0c632dbSHeiko Carstens 
109b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
110b0c632dbSHeiko Carstens {
1112c70fe44SChristian Borntraeger 	gmap_unregister_ipte_notifier(&gmap_notifier);
112b0c632dbSHeiko Carstens }
113b0c632dbSHeiko Carstens 
114b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn)
115b0c632dbSHeiko Carstens {
116b0c632dbSHeiko Carstens }
117b0c632dbSHeiko Carstens 
118b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
119b0c632dbSHeiko Carstens {
120b0c632dbSHeiko Carstens 	return 0;
121b0c632dbSHeiko Carstens }
122b0c632dbSHeiko Carstens 
123b0c632dbSHeiko Carstens void kvm_arch_exit(void)
124b0c632dbSHeiko Carstens {
125b0c632dbSHeiko Carstens }
126b0c632dbSHeiko Carstens 
127b0c632dbSHeiko Carstens /* Section: device related */
128b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
129b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
130b0c632dbSHeiko Carstens {
131b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
132b0c632dbSHeiko Carstens 		return s390_enable_sie();
133b0c632dbSHeiko Carstens 	return -EINVAL;
134b0c632dbSHeiko Carstens }
135b0c632dbSHeiko Carstens 
136b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext)
137b0c632dbSHeiko Carstens {
138d7b0b5ebSCarsten Otte 	int r;
139d7b0b5ebSCarsten Otte 
1402bd0ac4eSCarsten Otte 	switch (ext) {
141d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
142b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
14352e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
1441efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1451efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
1461efd0f59SCarsten Otte #endif
14760b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
14814eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
149d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
150fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
15110ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
152d7b0b5ebSCarsten Otte 		r = 1;
153d7b0b5ebSCarsten Otte 		break;
154e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
155e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
156e726b1bdSChristian Borntraeger 		r = KVM_MAX_VCPUS;
157e726b1bdSChristian Borntraeger 		break;
158e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
159e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
160e1e2e605SNick Wang 		break;
1611526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
162abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
1631526bf9cSChristian Borntraeger 		break;
1642bd0ac4eSCarsten Otte 	default:
165d7b0b5ebSCarsten Otte 		r = 0;
166b0c632dbSHeiko Carstens 	}
167d7b0b5ebSCarsten Otte 	return r;
1682bd0ac4eSCarsten Otte }
169b0c632dbSHeiko Carstens 
170b0c632dbSHeiko Carstens /* Section: vm related */
171b0c632dbSHeiko Carstens /*
172b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
173b0c632dbSHeiko Carstens  */
174b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
175b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
176b0c632dbSHeiko Carstens {
177b0c632dbSHeiko Carstens 	return 0;
178b0c632dbSHeiko Carstens }
179b0c632dbSHeiko Carstens 
180b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
181b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
182b0c632dbSHeiko Carstens {
183b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
184b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
185b0c632dbSHeiko Carstens 	int r;
186b0c632dbSHeiko Carstens 
187b0c632dbSHeiko Carstens 	switch (ioctl) {
188ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
189ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
190ba5c1e9bSCarsten Otte 
191ba5c1e9bSCarsten Otte 		r = -EFAULT;
192ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
193ba5c1e9bSCarsten Otte 			break;
194ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
195ba5c1e9bSCarsten Otte 		break;
196ba5c1e9bSCarsten Otte 	}
197b0c632dbSHeiko Carstens 	default:
198367e1319SAvi Kivity 		r = -ENOTTY;
199b0c632dbSHeiko Carstens 	}
200b0c632dbSHeiko Carstens 
201b0c632dbSHeiko Carstens 	return r;
202b0c632dbSHeiko Carstens }
203b0c632dbSHeiko Carstens 
204e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
205b0c632dbSHeiko Carstens {
206b0c632dbSHeiko Carstens 	int rc;
207b0c632dbSHeiko Carstens 	char debug_name[16];
208b0c632dbSHeiko Carstens 
209e08b9637SCarsten Otte 	rc = -EINVAL;
210e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
211e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
212e08b9637SCarsten Otte 		goto out_err;
213e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
214e08b9637SCarsten Otte 		goto out_err;
215e08b9637SCarsten Otte #else
216e08b9637SCarsten Otte 	if (type)
217e08b9637SCarsten Otte 		goto out_err;
218e08b9637SCarsten Otte #endif
219e08b9637SCarsten Otte 
220b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
221b0c632dbSHeiko Carstens 	if (rc)
222d89f5effSJan Kiszka 		goto out_err;
223b0c632dbSHeiko Carstens 
224b290411aSCarsten Otte 	rc = -ENOMEM;
225b290411aSCarsten Otte 
226b0c632dbSHeiko Carstens 	kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
227b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
228d89f5effSJan Kiszka 		goto out_err;
229b0c632dbSHeiko Carstens 
230b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
231b0c632dbSHeiko Carstens 
232b0c632dbSHeiko Carstens 	kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
233b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
234b0c632dbSHeiko Carstens 		goto out_nodbf;
235b0c632dbSHeiko Carstens 
236ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
237ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&kvm->arch.float_int.list);
238ba5c1e9bSCarsten Otte 
239b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
240b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "%s", "vm created");
241b0c632dbSHeiko Carstens 
242e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
243e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
244e08b9637SCarsten Otte 	} else {
245598841caSCarsten Otte 		kvm->arch.gmap = gmap_alloc(current->mm);
246598841caSCarsten Otte 		if (!kvm->arch.gmap)
247598841caSCarsten Otte 			goto out_nogmap;
2482c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
249e08b9637SCarsten Otte 	}
250fa6b7fe9SCornelia Huck 
251fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
252fa6b7fe9SCornelia Huck 
253d89f5effSJan Kiszka 	return 0;
254598841caSCarsten Otte out_nogmap:
255598841caSCarsten Otte 	debug_unregister(kvm->arch.dbf);
256b0c632dbSHeiko Carstens out_nodbf:
257b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
258d89f5effSJan Kiszka out_err:
259d89f5effSJan Kiszka 	return rc;
260b0c632dbSHeiko Carstens }
261b0c632dbSHeiko Carstens 
262d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
263d329c035SChristian Borntraeger {
264d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
265ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
26658f9460bSCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm)) {
26758f9460bSCarsten Otte 		clear_bit(63 - vcpu->vcpu_id,
26858f9460bSCarsten Otte 			  (unsigned long *) &vcpu->kvm->arch.sca->mcn);
269abf4a71eSCarsten Otte 		if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
270abf4a71eSCarsten Otte 		    (__u64) vcpu->arch.sie_block)
271abf4a71eSCarsten Otte 			vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
27258f9460bSCarsten Otte 	}
273abf4a71eSCarsten Otte 	smp_mb();
27427e0393fSCarsten Otte 
27527e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
27627e0393fSCarsten Otte 		gmap_free(vcpu->arch.gmap);
27727e0393fSCarsten Otte 
278d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2796692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
280d329c035SChristian Borntraeger 	kfree(vcpu);
281d329c035SChristian Borntraeger }
282d329c035SChristian Borntraeger 
283d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
284d329c035SChristian Borntraeger {
285d329c035SChristian Borntraeger 	unsigned int i;
286988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
287d329c035SChristian Borntraeger 
288988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
289988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
290988a2caeSGleb Natapov 
291988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
292988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
293d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
294988a2caeSGleb Natapov 
295988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
296988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
297d329c035SChristian Borntraeger }
298d329c035SChristian Borntraeger 
299ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm)
300ad8ba2cdSSheng Yang {
301ad8ba2cdSSheng Yang }
302ad8ba2cdSSheng Yang 
303b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
304b0c632dbSHeiko Carstens {
305d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
306b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
307d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
30827e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
309598841caSCarsten Otte 		gmap_free(kvm->arch.gmap);
310b0c632dbSHeiko Carstens }
311b0c632dbSHeiko Carstens 
312b0c632dbSHeiko Carstens /* Section: vcpu related */
313b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
314b0c632dbSHeiko Carstens {
31527e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm)) {
31627e0393fSCarsten Otte 		vcpu->arch.gmap = gmap_alloc(current->mm);
31727e0393fSCarsten Otte 		if (!vcpu->arch.gmap)
31827e0393fSCarsten Otte 			return -ENOMEM;
3192c70fe44SChristian Borntraeger 		vcpu->arch.gmap->private = vcpu->kvm;
32027e0393fSCarsten Otte 		return 0;
32127e0393fSCarsten Otte 	}
32227e0393fSCarsten Otte 
323598841caSCarsten Otte 	vcpu->arch.gmap = vcpu->kvm->arch.gmap;
32459674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
32559674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
3269eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
3279eed0735SChristian Borntraeger 				    KVM_SYNC_CRS;
328b0c632dbSHeiko Carstens 	return 0;
329b0c632dbSHeiko Carstens }
330b0c632dbSHeiko Carstens 
331b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
332b0c632dbSHeiko Carstens {
3336692cef3SChristian Borntraeger 	/* Nothing todo */
334b0c632dbSHeiko Carstens }
335b0c632dbSHeiko Carstens 
336b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
337b0c632dbSHeiko Carstens {
338b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.host_fpregs);
339b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.host_acrs);
340b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK;
341b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.guest_fpregs);
34259674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
343480e5926SChristian Borntraeger 	gmap_enable(vcpu->arch.gmap);
3449e6dabefSCornelia Huck 	atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
345b0c632dbSHeiko Carstens }
346b0c632dbSHeiko Carstens 
347b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
348b0c632dbSHeiko Carstens {
3499e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
350480e5926SChristian Borntraeger 	gmap_disable(vcpu->arch.gmap);
351b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.guest_fpregs);
35259674c1aSChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
353b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.host_fpregs);
354b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
355b0c632dbSHeiko Carstens }
356b0c632dbSHeiko Carstens 
357b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
358b0c632dbSHeiko Carstens {
359b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
360b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
361b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
3628d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
363b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
364b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
365b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
366b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
367b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
368b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
369b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
370b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
371b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
37261bde82cSChristian Borntraeger 	atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
373b0c632dbSHeiko Carstens }
374b0c632dbSHeiko Carstens 
37542897d86SMarcelo Tosatti int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
37642897d86SMarcelo Tosatti {
37742897d86SMarcelo Tosatti 	return 0;
37842897d86SMarcelo Tosatti }
37942897d86SMarcelo Tosatti 
380b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
381b0c632dbSHeiko Carstens {
3829e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3839e6dabefSCornelia Huck 						    CPUSTAT_SM |
3849e6dabefSCornelia Huck 						    CPUSTAT_STOPPED);
385fc34531dSChristian Borntraeger 	vcpu->arch.sie_block->ecb   = 6;
386b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->eca   = 0xC1002001U;
387ef50f7acSChristian Borntraeger 	vcpu->arch.sie_block->fac   = (int) (long) facilities;
388ca872302SChristian Borntraeger 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
389ca872302SChristian Borntraeger 	tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet,
390ba5c1e9bSCarsten Otte 		     (unsigned long) vcpu);
391ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
392453423dcSChristian Borntraeger 	get_cpu_id(&vcpu->arch.cpu_id);
39392e6ecf3SChristian Borntraeger 	vcpu->arch.cpu_id.version = 0xff;
394b0c632dbSHeiko Carstens 	return 0;
395b0c632dbSHeiko Carstens }
396b0c632dbSHeiko Carstens 
397b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
398b0c632dbSHeiko Carstens 				      unsigned int id)
399b0c632dbSHeiko Carstens {
4004d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
4014d47555aSCarsten Otte 	int rc = -EINVAL;
402b0c632dbSHeiko Carstens 
4034d47555aSCarsten Otte 	if (id >= KVM_MAX_VCPUS)
4044d47555aSCarsten Otte 		goto out;
4054d47555aSCarsten Otte 
4064d47555aSCarsten Otte 	rc = -ENOMEM;
4074d47555aSCarsten Otte 
4084d47555aSCarsten Otte 	vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
409b0c632dbSHeiko Carstens 	if (!vcpu)
4104d47555aSCarsten Otte 		goto out;
411b0c632dbSHeiko Carstens 
412180c12fbSChristian Borntraeger 	vcpu->arch.sie_block = (struct kvm_s390_sie_block *)
413180c12fbSChristian Borntraeger 					get_zeroed_page(GFP_KERNEL);
414b0c632dbSHeiko Carstens 
415b0c632dbSHeiko Carstens 	if (!vcpu->arch.sie_block)
416b0c632dbSHeiko Carstens 		goto out_free_cpu;
417b0c632dbSHeiko Carstens 
418b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
41958f9460bSCarsten Otte 	if (!kvm_is_ucontrol(kvm)) {
42058f9460bSCarsten Otte 		if (!kvm->arch.sca) {
42158f9460bSCarsten Otte 			WARN_ON_ONCE(1);
42258f9460bSCarsten Otte 			goto out_free_cpu;
42358f9460bSCarsten Otte 		}
424abf4a71eSCarsten Otte 		if (!kvm->arch.sca->cpu[id].sda)
42558f9460bSCarsten Otte 			kvm->arch.sca->cpu[id].sda =
42658f9460bSCarsten Otte 				(__u64) vcpu->arch.sie_block;
42758f9460bSCarsten Otte 		vcpu->arch.sie_block->scaoh =
42858f9460bSCarsten Otte 			(__u32)(((__u64)kvm->arch.sca) >> 32);
429b0c632dbSHeiko Carstens 		vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
430fc34531dSChristian Borntraeger 		set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
43158f9460bSCarsten Otte 	}
432b0c632dbSHeiko Carstens 
433ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
434ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
435ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
436b037a4f3SChristian Borntraeger 	spin_lock(&kvm->arch.float_int.lock);
437ba5c1e9bSCarsten Otte 	kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int;
438ba5c1e9bSCarsten Otte 	init_waitqueue_head(&vcpu->arch.local_int.wq);
4395288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
440b037a4f3SChristian Borntraeger 	spin_unlock(&kvm->arch.float_int.lock);
441ba5c1e9bSCarsten Otte 
442b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
443b0c632dbSHeiko Carstens 	if (rc)
4447b06bf2fSWei Yongjun 		goto out_free_sie_block;
445b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
446b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
447ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
448b0c632dbSHeiko Carstens 
449b0c632dbSHeiko Carstens 	return vcpu;
4507b06bf2fSWei Yongjun out_free_sie_block:
4517b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
452b0c632dbSHeiko Carstens out_free_cpu:
453b0c632dbSHeiko Carstens 	kfree(vcpu);
4544d47555aSCarsten Otte out:
455b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
456b0c632dbSHeiko Carstens }
457b0c632dbSHeiko Carstens 
458b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
459b0c632dbSHeiko Carstens {
460b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but never calls it */
461b0c632dbSHeiko Carstens 	BUG();
462b0c632dbSHeiko Carstens 	return 0;
463b0c632dbSHeiko Carstens }
464b0c632dbSHeiko Carstens 
46549b99e1eSChristian Borntraeger void s390_vcpu_block(struct kvm_vcpu *vcpu)
46649b99e1eSChristian Borntraeger {
46749b99e1eSChristian Borntraeger 	atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
46849b99e1eSChristian Borntraeger }
46949b99e1eSChristian Borntraeger 
47049b99e1eSChristian Borntraeger void s390_vcpu_unblock(struct kvm_vcpu *vcpu)
47149b99e1eSChristian Borntraeger {
47249b99e1eSChristian Borntraeger 	atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
47349b99e1eSChristian Borntraeger }
47449b99e1eSChristian Borntraeger 
47549b99e1eSChristian Borntraeger /*
47649b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
47749b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
47849b99e1eSChristian Borntraeger  * return immediately. */
47949b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
48049b99e1eSChristian Borntraeger {
48149b99e1eSChristian Borntraeger 	atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
48249b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
48349b99e1eSChristian Borntraeger 		cpu_relax();
48449b99e1eSChristian Borntraeger }
48549b99e1eSChristian Borntraeger 
48649b99e1eSChristian Borntraeger /* Kick a guest cpu out of SIE and prevent SIE-reentry */
48749b99e1eSChristian Borntraeger void exit_sie_sync(struct kvm_vcpu *vcpu)
48849b99e1eSChristian Borntraeger {
48949b99e1eSChristian Borntraeger 	s390_vcpu_block(vcpu);
49049b99e1eSChristian Borntraeger 	exit_sie(vcpu);
49149b99e1eSChristian Borntraeger }
49249b99e1eSChristian Borntraeger 
4932c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
4942c70fe44SChristian Borntraeger {
4952c70fe44SChristian Borntraeger 	int i;
4962c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
4972c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
4982c70fe44SChristian Borntraeger 
4992c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
5002c70fe44SChristian Borntraeger 		/* match against both prefix pages */
5012c70fe44SChristian Borntraeger 		if (vcpu->arch.sie_block->prefix == (address & ~0x1000UL)) {
5022c70fe44SChristian Borntraeger 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
5032c70fe44SChristian Borntraeger 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
5042c70fe44SChristian Borntraeger 			exit_sie_sync(vcpu);
5052c70fe44SChristian Borntraeger 		}
5062c70fe44SChristian Borntraeger 	}
5072c70fe44SChristian Borntraeger }
5082c70fe44SChristian Borntraeger 
509b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
510b6d33834SChristoffer Dall {
511b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
512b6d33834SChristoffer Dall 	BUG();
513b6d33834SChristoffer Dall 	return 0;
514b6d33834SChristoffer Dall }
515b6d33834SChristoffer Dall 
51614eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
51714eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
51814eebd91SCarsten Otte {
51914eebd91SCarsten Otte 	int r = -EINVAL;
52014eebd91SCarsten Otte 
52114eebd91SCarsten Otte 	switch (reg->id) {
52229b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
52329b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
52429b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
52529b7c71bSCarsten Otte 		break;
52629b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
52729b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
52829b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
52929b7c71bSCarsten Otte 		break;
53046a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
53146a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->cputm,
53246a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
53346a6dd1cSJason J. herne 		break;
53446a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
53546a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
53646a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
53746a6dd1cSJason J. herne 		break;
53814eebd91SCarsten Otte 	default:
53914eebd91SCarsten Otte 		break;
54014eebd91SCarsten Otte 	}
54114eebd91SCarsten Otte 
54214eebd91SCarsten Otte 	return r;
54314eebd91SCarsten Otte }
54414eebd91SCarsten Otte 
54514eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
54614eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
54714eebd91SCarsten Otte {
54814eebd91SCarsten Otte 	int r = -EINVAL;
54914eebd91SCarsten Otte 
55014eebd91SCarsten Otte 	switch (reg->id) {
55129b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
55229b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
55329b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
55429b7c71bSCarsten Otte 		break;
55529b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
55629b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
55729b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
55829b7c71bSCarsten Otte 		break;
55946a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
56046a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->cputm,
56146a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
56246a6dd1cSJason J. herne 		break;
56346a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
56446a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
56546a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
56646a6dd1cSJason J. herne 		break;
56714eebd91SCarsten Otte 	default:
56814eebd91SCarsten Otte 		break;
56914eebd91SCarsten Otte 	}
57014eebd91SCarsten Otte 
57114eebd91SCarsten Otte 	return r;
57214eebd91SCarsten Otte }
573b6d33834SChristoffer Dall 
574b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
575b0c632dbSHeiko Carstens {
576b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
577b0c632dbSHeiko Carstens 	return 0;
578b0c632dbSHeiko Carstens }
579b0c632dbSHeiko Carstens 
580b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
581b0c632dbSHeiko Carstens {
5825a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
583b0c632dbSHeiko Carstens 	return 0;
584b0c632dbSHeiko Carstens }
585b0c632dbSHeiko Carstens 
586b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
587b0c632dbSHeiko Carstens {
5885a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
589b0c632dbSHeiko Carstens 	return 0;
590b0c632dbSHeiko Carstens }
591b0c632dbSHeiko Carstens 
592b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
593b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
594b0c632dbSHeiko Carstens {
59559674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
596b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
59759674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
598b0c632dbSHeiko Carstens 	return 0;
599b0c632dbSHeiko Carstens }
600b0c632dbSHeiko Carstens 
601b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
602b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
603b0c632dbSHeiko Carstens {
60459674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
605b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
606b0c632dbSHeiko Carstens 	return 0;
607b0c632dbSHeiko Carstens }
608b0c632dbSHeiko Carstens 
609b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
610b0c632dbSHeiko Carstens {
611b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
61285175587SChristian Borntraeger 	vcpu->arch.guest_fpregs.fpc = fpu->fpc & FPC_VALID_MASK;
6137eef87dcSCarsten Otte 	restore_fp_regs(&vcpu->arch.guest_fpregs);
614b0c632dbSHeiko Carstens 	return 0;
615b0c632dbSHeiko Carstens }
616b0c632dbSHeiko Carstens 
617b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
618b0c632dbSHeiko Carstens {
619b0c632dbSHeiko Carstens 	memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
620b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
621b0c632dbSHeiko Carstens 	return 0;
622b0c632dbSHeiko Carstens }
623b0c632dbSHeiko Carstens 
624b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
625b0c632dbSHeiko Carstens {
626b0c632dbSHeiko Carstens 	int rc = 0;
627b0c632dbSHeiko Carstens 
6289e6dabefSCornelia Huck 	if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED))
629b0c632dbSHeiko Carstens 		rc = -EBUSY;
630d7b0b5ebSCarsten Otte 	else {
631d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
632d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
633d7b0b5ebSCarsten Otte 	}
634b0c632dbSHeiko Carstens 	return rc;
635b0c632dbSHeiko Carstens }
636b0c632dbSHeiko Carstens 
637b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
638b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
639b0c632dbSHeiko Carstens {
640b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
641b0c632dbSHeiko Carstens }
642b0c632dbSHeiko Carstens 
643d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
644d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
645b0c632dbSHeiko Carstens {
646b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
647b0c632dbSHeiko Carstens }
648b0c632dbSHeiko Carstens 
64962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
65062d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
65162d9f0dbSMarcelo Tosatti {
65262d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
65362d9f0dbSMarcelo Tosatti }
65462d9f0dbSMarcelo Tosatti 
65562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
65662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
65762d9f0dbSMarcelo Tosatti {
65862d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
65962d9f0dbSMarcelo Tosatti }
66062d9f0dbSMarcelo Tosatti 
6612c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
6622c70fe44SChristian Borntraeger {
6632c70fe44SChristian Borntraeger 	/*
6642c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
6652c70fe44SChristian Borntraeger 	 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
6662c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
6672c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
6682c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
6692c70fe44SChristian Borntraeger 	 */
6702c70fe44SChristian Borntraeger 	while (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
6712c70fe44SChristian Borntraeger 		int rc;
6722c70fe44SChristian Borntraeger 		rc = gmap_ipte_notify(vcpu->arch.gmap,
6732c70fe44SChristian Borntraeger 				      vcpu->arch.sie_block->prefix,
6742c70fe44SChristian Borntraeger 				      PAGE_SIZE * 2);
6752c70fe44SChristian Borntraeger 		if (rc)
6762c70fe44SChristian Borntraeger 			return rc;
6772c70fe44SChristian Borntraeger 		s390_vcpu_unblock(vcpu);
6782c70fe44SChristian Borntraeger 	}
6792c70fe44SChristian Borntraeger 	return 0;
6802c70fe44SChristian Borntraeger }
6812c70fe44SChristian Borntraeger 
682e168bf8dSCarsten Otte static int __vcpu_run(struct kvm_vcpu *vcpu)
683b0c632dbSHeiko Carstens {
684e168bf8dSCarsten Otte 	int rc;
685e168bf8dSCarsten Otte 
6865a32c1afSChristian Borntraeger 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
687b0c632dbSHeiko Carstens 
688b0c632dbSHeiko Carstens 	if (need_resched())
689b0c632dbSHeiko Carstens 		schedule();
690b0c632dbSHeiko Carstens 
69171cde587SChristian Borntraeger 	if (test_thread_flag(TIF_MCCK_PENDING))
69271cde587SChristian Borntraeger 		s390_handle_mcck();
69371cde587SChristian Borntraeger 
694d6b6d166SCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm))
6950ff31867SCarsten Otte 		kvm_s390_deliver_pending_interrupts(vcpu);
6960ff31867SCarsten Otte 
6972c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
6982c70fe44SChristian Borntraeger 	if (rc)
6992c70fe44SChristian Borntraeger 		return rc;
7002c70fe44SChristian Borntraeger 
701b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
70283987aceSChristian Borntraeger 	preempt_disable();
703b0c632dbSHeiko Carstens 	kvm_guest_enter();
70483987aceSChristian Borntraeger 	preempt_enable();
705b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
706b0c632dbSHeiko Carstens 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
7075786fffaSCornelia Huck 	trace_kvm_s390_sie_enter(vcpu,
7085786fffaSCornelia Huck 				 atomic_read(&vcpu->arch.sie_block->cpuflags));
7095a32c1afSChristian Borntraeger 	rc = sie64a(vcpu->arch.sie_block, vcpu->run->s.regs.gprs);
710*7c470539SMartin Schwidefsky 	if (rc > 0)
711*7c470539SMartin Schwidefsky 		rc = 0;
712*7c470539SMartin Schwidefsky 	if (rc < 0) {
713e168bf8dSCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm)) {
714e168bf8dSCarsten Otte 			rc = SIE_INTERCEPT_UCONTROL;
715e168bf8dSCarsten Otte 		} else {
7161f0d0f09SCarsten Otte 			VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
7175786fffaSCornelia Huck 			trace_kvm_s390_sie_fault(vcpu);
718db4a29cbSHeiko Carstens 			rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
719e168bf8dSCarsten Otte 		}
7201f0d0f09SCarsten Otte 	}
721b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
722b0c632dbSHeiko Carstens 		   vcpu->arch.sie_block->icptcode);
7235786fffaSCornelia Huck 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
724b0c632dbSHeiko Carstens 	kvm_guest_exit();
725b0c632dbSHeiko Carstens 
7265a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
727e168bf8dSCarsten Otte 	return rc;
728b0c632dbSHeiko Carstens }
729b0c632dbSHeiko Carstens 
730b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
731b0c632dbSHeiko Carstens {
7328f2abe6aSChristian Borntraeger 	int rc;
733b0c632dbSHeiko Carstens 	sigset_t sigsaved;
734b0c632dbSHeiko Carstens 
7359ace903dSChristian Ehrhardt rerun_vcpu:
736b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
737b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
738b0c632dbSHeiko Carstens 
7399e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
740b0c632dbSHeiko Carstens 
741ba5c1e9bSCarsten Otte 	BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL);
742ba5c1e9bSCarsten Otte 
7438f2abe6aSChristian Borntraeger 	switch (kvm_run->exit_reason) {
7448f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_SIEIC:
7458f2abe6aSChristian Borntraeger 	case KVM_EXIT_UNKNOWN:
7469ace903dSChristian Ehrhardt 	case KVM_EXIT_INTR:
7478f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_RESET:
748e168bf8dSCarsten Otte 	case KVM_EXIT_S390_UCONTROL:
749fa6b7fe9SCornelia Huck 	case KVM_EXIT_S390_TSCH:
7508f2abe6aSChristian Borntraeger 		break;
7518f2abe6aSChristian Borntraeger 	default:
7528f2abe6aSChristian Borntraeger 		BUG();
7538f2abe6aSChristian Borntraeger 	}
7548f2abe6aSChristian Borntraeger 
755d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
756d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
75760b413c9SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) {
75860b413c9SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_PREFIX;
75960b413c9SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
76060b413c9SChristian Borntraeger 	}
7619eed0735SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
7629eed0735SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_CRS;
7639eed0735SChristian Borntraeger 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
7649eed0735SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
7659eed0735SChristian Borntraeger 	}
766d7b0b5ebSCarsten Otte 
767dab4079dSHeiko Carstens 	might_fault();
7688f2abe6aSChristian Borntraeger 
7698f2abe6aSChristian Borntraeger 	do {
770e168bf8dSCarsten Otte 		rc = __vcpu_run(vcpu);
771e168bf8dSCarsten Otte 		if (rc)
772e168bf8dSCarsten Otte 			break;
773c0d744a9SCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm))
774c0d744a9SCarsten Otte 			rc = -EOPNOTSUPP;
775c0d744a9SCarsten Otte 		else
7768f2abe6aSChristian Borntraeger 			rc = kvm_handle_sie_intercept(vcpu);
7778f2abe6aSChristian Borntraeger 	} while (!signal_pending(current) && !rc);
7788f2abe6aSChristian Borntraeger 
7799ace903dSChristian Ehrhardt 	if (rc == SIE_INTERCEPT_RERUNVCPU)
7809ace903dSChristian Ehrhardt 		goto rerun_vcpu;
7819ace903dSChristian Ehrhardt 
782b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
783b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
7848f2abe6aSChristian Borntraeger 		rc = -EINTR;
785b1d16c49SChristian Ehrhardt 	}
7868f2abe6aSChristian Borntraeger 
787e168bf8dSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
788e168bf8dSCarsten Otte 	if (rc == SIE_INTERCEPT_UCONTROL) {
789e168bf8dSCarsten Otte 		kvm_run->exit_reason = KVM_EXIT_S390_UCONTROL;
790e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.trans_exc_code =
791e168bf8dSCarsten Otte 			current->thread.gmap_addr;
792e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.pgm_code = 0x10;
793e168bf8dSCarsten Otte 		rc = 0;
794e168bf8dSCarsten Otte 	}
795e168bf8dSCarsten Otte #endif
796e168bf8dSCarsten Otte 
797b8e660b8SHeiko Carstens 	if (rc == -EOPNOTSUPP) {
7988f2abe6aSChristian Borntraeger 		/* intercept cannot be handled in-kernel, prepare kvm-run */
7998f2abe6aSChristian Borntraeger 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
8008f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
8018f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipa      = vcpu->arch.sie_block->ipa;
8028f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipb      = vcpu->arch.sie_block->ipb;
8038f2abe6aSChristian Borntraeger 		rc = 0;
8048f2abe6aSChristian Borntraeger 	}
8058f2abe6aSChristian Borntraeger 
8068f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
8078f2abe6aSChristian Borntraeger 		/* intercept was handled, but userspace support is needed
8088f2abe6aSChristian Borntraeger 		 * kvm_run has been prepared by the handler */
8098f2abe6aSChristian Borntraeger 		rc = 0;
8108f2abe6aSChristian Borntraeger 	}
8118f2abe6aSChristian Borntraeger 
812d7b0b5ebSCarsten Otte 	kvm_run->psw_mask     = vcpu->arch.sie_block->gpsw.mask;
813d7b0b5ebSCarsten Otte 	kvm_run->psw_addr     = vcpu->arch.sie_block->gpsw.addr;
81460b413c9SChristian Borntraeger 	kvm_run->s.regs.prefix = vcpu->arch.sie_block->prefix;
8159eed0735SChristian Borntraeger 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
816d7b0b5ebSCarsten Otte 
817b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
818b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
819b0c632dbSHeiko Carstens 
820b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
8217e8e6ab4SHeiko Carstens 	return rc;
822b0c632dbSHeiko Carstens }
823b0c632dbSHeiko Carstens 
824092670cdSCarsten Otte static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, void *from,
825b0c632dbSHeiko Carstens 		       unsigned long n, int prefix)
826b0c632dbSHeiko Carstens {
827b0c632dbSHeiko Carstens 	if (prefix)
828b0c632dbSHeiko Carstens 		return copy_to_guest(vcpu, guestdest, from, n);
829b0c632dbSHeiko Carstens 	else
830b0c632dbSHeiko Carstens 		return copy_to_guest_absolute(vcpu, guestdest, from, n);
831b0c632dbSHeiko Carstens }
832b0c632dbSHeiko Carstens 
833b0c632dbSHeiko Carstens /*
834b0c632dbSHeiko Carstens  * store status at address
835b0c632dbSHeiko Carstens  * we use have two special cases:
836b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
837b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
838b0c632dbSHeiko Carstens  */
839971eb77fSChristian Borntraeger int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
840b0c632dbSHeiko Carstens {
841092670cdSCarsten Otte 	unsigned char archmode = 1;
842b0c632dbSHeiko Carstens 	int prefix;
843b0c632dbSHeiko Carstens 
844b0c632dbSHeiko Carstens 	if (addr == KVM_S390_STORE_STATUS_NOADDR) {
845b0c632dbSHeiko Carstens 		if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
846b0c632dbSHeiko Carstens 			return -EFAULT;
847b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
848b0c632dbSHeiko Carstens 		prefix = 0;
849b0c632dbSHeiko Carstens 	} else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
850b0c632dbSHeiko Carstens 		if (copy_to_guest(vcpu, 163ul, &archmode, 1))
851b0c632dbSHeiko Carstens 			return -EFAULT;
852b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
853b0c632dbSHeiko Carstens 		prefix = 1;
854b0c632dbSHeiko Carstens 	} else
855b0c632dbSHeiko Carstens 		prefix = 0;
856b0c632dbSHeiko Carstens 
85715bc8d84SChristian Borntraeger 	/*
85815bc8d84SChristian Borntraeger 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
85915bc8d84SChristian Borntraeger 	 * copying in vcpu load/put. Lets update our copies before we save
86015bc8d84SChristian Borntraeger 	 * it into the save area
86115bc8d84SChristian Borntraeger 	 */
86215bc8d84SChristian Borntraeger 	save_fp_regs(&vcpu->arch.guest_fpregs);
86315bc8d84SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
86415bc8d84SChristian Borntraeger 
865f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, fp_regs),
866b0c632dbSHeiko Carstens 			vcpu->arch.guest_fpregs.fprs, 128, prefix))
867b0c632dbSHeiko Carstens 		return -EFAULT;
868b0c632dbSHeiko Carstens 
869f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, gp_regs),
8705a32c1afSChristian Borntraeger 			vcpu->run->s.regs.gprs, 128, prefix))
871b0c632dbSHeiko Carstens 		return -EFAULT;
872b0c632dbSHeiko Carstens 
873f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, psw),
874b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gpsw, 16, prefix))
875b0c632dbSHeiko Carstens 		return -EFAULT;
876b0c632dbSHeiko Carstens 
877f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, pref_reg),
878b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->prefix, 4, prefix))
879b0c632dbSHeiko Carstens 		return -EFAULT;
880b0c632dbSHeiko Carstens 
881b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
882f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, fp_ctrl_reg),
883b0c632dbSHeiko Carstens 			&vcpu->arch.guest_fpregs.fpc, 4, prefix))
884b0c632dbSHeiko Carstens 		return -EFAULT;
885b0c632dbSHeiko Carstens 
886f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, tod_reg),
887b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->todpr, 4, prefix))
888b0c632dbSHeiko Carstens 		return -EFAULT;
889b0c632dbSHeiko Carstens 
890f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, timer),
891b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->cputm, 8, prefix))
892b0c632dbSHeiko Carstens 		return -EFAULT;
893b0c632dbSHeiko Carstens 
894f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, clk_cmp),
895b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->ckc, 8, prefix))
896b0c632dbSHeiko Carstens 		return -EFAULT;
897b0c632dbSHeiko Carstens 
898f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, acc_regs),
89959674c1aSChristian Borntraeger 			&vcpu->run->s.regs.acrs, 64, prefix))
900b0c632dbSHeiko Carstens 		return -EFAULT;
901b0c632dbSHeiko Carstens 
902b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
903f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, ctrl_regs),
904b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gcr, 128, prefix))
905b0c632dbSHeiko Carstens 		return -EFAULT;
906b0c632dbSHeiko Carstens 	return 0;
907b0c632dbSHeiko Carstens }
908b0c632dbSHeiko Carstens 
909d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
910d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
911d6712df9SCornelia Huck {
912d6712df9SCornelia Huck 	int r;
913d6712df9SCornelia Huck 
914d6712df9SCornelia Huck 	if (cap->flags)
915d6712df9SCornelia Huck 		return -EINVAL;
916d6712df9SCornelia Huck 
917d6712df9SCornelia Huck 	switch (cap->cap) {
918fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
919fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
920fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
921fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
922fa6b7fe9SCornelia Huck 		}
923fa6b7fe9SCornelia Huck 		r = 0;
924fa6b7fe9SCornelia Huck 		break;
925d6712df9SCornelia Huck 	default:
926d6712df9SCornelia Huck 		r = -EINVAL;
927d6712df9SCornelia Huck 		break;
928d6712df9SCornelia Huck 	}
929d6712df9SCornelia Huck 	return r;
930d6712df9SCornelia Huck }
931d6712df9SCornelia Huck 
932b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
933b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
934b0c632dbSHeiko Carstens {
935b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
936b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
937bc923cc9SAvi Kivity 	long r;
938b0c632dbSHeiko Carstens 
93993736624SAvi Kivity 	switch (ioctl) {
94093736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
941ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
942ba5c1e9bSCarsten Otte 
94393736624SAvi Kivity 		r = -EFAULT;
944ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
94593736624SAvi Kivity 			break;
94693736624SAvi Kivity 		r = kvm_s390_inject_vcpu(vcpu, &s390int);
94793736624SAvi Kivity 		break;
948ba5c1e9bSCarsten Otte 	}
949b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
950bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
951bc923cc9SAvi Kivity 		break;
952b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
953b0c632dbSHeiko Carstens 		psw_t psw;
954b0c632dbSHeiko Carstens 
955bc923cc9SAvi Kivity 		r = -EFAULT;
956b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
957bc923cc9SAvi Kivity 			break;
958bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
959bc923cc9SAvi Kivity 		break;
960b0c632dbSHeiko Carstens 	}
961b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
962bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
963bc923cc9SAvi Kivity 		break;
96414eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
96514eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
96614eebd91SCarsten Otte 		struct kvm_one_reg reg;
96714eebd91SCarsten Otte 		r = -EFAULT;
96814eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
96914eebd91SCarsten Otte 			break;
97014eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
97114eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
97214eebd91SCarsten Otte 		else
97314eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
97414eebd91SCarsten Otte 		break;
97514eebd91SCarsten Otte 	}
97627e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
97727e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
97827e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
97927e0393fSCarsten Otte 
98027e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
98127e0393fSCarsten Otte 			r = -EFAULT;
98227e0393fSCarsten Otte 			break;
98327e0393fSCarsten Otte 		}
98427e0393fSCarsten Otte 
98527e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
98627e0393fSCarsten Otte 			r = -EINVAL;
98727e0393fSCarsten Otte 			break;
98827e0393fSCarsten Otte 		}
98927e0393fSCarsten Otte 
99027e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
99127e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
99227e0393fSCarsten Otte 		break;
99327e0393fSCarsten Otte 	}
99427e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
99527e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
99627e0393fSCarsten Otte 
99727e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
99827e0393fSCarsten Otte 			r = -EFAULT;
99927e0393fSCarsten Otte 			break;
100027e0393fSCarsten Otte 		}
100127e0393fSCarsten Otte 
100227e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
100327e0393fSCarsten Otte 			r = -EINVAL;
100427e0393fSCarsten Otte 			break;
100527e0393fSCarsten Otte 		}
100627e0393fSCarsten Otte 
100727e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
100827e0393fSCarsten Otte 			ucasmap.length);
100927e0393fSCarsten Otte 		break;
101027e0393fSCarsten Otte 	}
101127e0393fSCarsten Otte #endif
1012ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
1013ccc7910fSCarsten Otte 		r = gmap_fault(arg, vcpu->arch.gmap);
1014ccc7910fSCarsten Otte 		if (!IS_ERR_VALUE(r))
1015ccc7910fSCarsten Otte 			r = 0;
1016ccc7910fSCarsten Otte 		break;
1017ccc7910fSCarsten Otte 	}
1018d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
1019d6712df9SCornelia Huck 	{
1020d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
1021d6712df9SCornelia Huck 		r = -EFAULT;
1022d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1023d6712df9SCornelia Huck 			break;
1024d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1025d6712df9SCornelia Huck 		break;
1026d6712df9SCornelia Huck 	}
1027b0c632dbSHeiko Carstens 	default:
10283e6afcf1SCarsten Otte 		r = -ENOTTY;
1029b0c632dbSHeiko Carstens 	}
1030bc923cc9SAvi Kivity 	return r;
1031b0c632dbSHeiko Carstens }
1032b0c632dbSHeiko Carstens 
10335b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
10345b1c1493SCarsten Otte {
10355b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
10365b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
10375b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
10385b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
10395b1c1493SCarsten Otte 		get_page(vmf->page);
10405b1c1493SCarsten Otte 		return 0;
10415b1c1493SCarsten Otte 	}
10425b1c1493SCarsten Otte #endif
10435b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
10445b1c1493SCarsten Otte }
10455b1c1493SCarsten Otte 
1046db3fe4ebSTakuya Yoshikawa void kvm_arch_free_memslot(struct kvm_memory_slot *free,
1047db3fe4ebSTakuya Yoshikawa 			   struct kvm_memory_slot *dont)
1048db3fe4ebSTakuya Yoshikawa {
1049db3fe4ebSTakuya Yoshikawa }
1050db3fe4ebSTakuya Yoshikawa 
1051db3fe4ebSTakuya Yoshikawa int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
1052db3fe4ebSTakuya Yoshikawa {
1053db3fe4ebSTakuya Yoshikawa 	return 0;
1054db3fe4ebSTakuya Yoshikawa }
1055db3fe4ebSTakuya Yoshikawa 
1056b0c632dbSHeiko Carstens /* Section: memory related */
1057f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
1058f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
10597b6195a9STakuya Yoshikawa 				   struct kvm_userspace_memory_region *mem,
10607b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
1061b0c632dbSHeiko Carstens {
1062dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
1063dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
1064dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
1065dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
1066b0c632dbSHeiko Carstens 
1067598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
1068b0c632dbSHeiko Carstens 		return -EINVAL;
1069b0c632dbSHeiko Carstens 
1070598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
1071b0c632dbSHeiko Carstens 		return -EINVAL;
1072b0c632dbSHeiko Carstens 
1073f7784b8eSMarcelo Tosatti 	return 0;
1074f7784b8eSMarcelo Tosatti }
1075f7784b8eSMarcelo Tosatti 
1076f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
1077f7784b8eSMarcelo Tosatti 				struct kvm_userspace_memory_region *mem,
10788482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
10798482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
1080f7784b8eSMarcelo Tosatti {
1081f7850c92SCarsten Otte 	int rc;
1082f7784b8eSMarcelo Tosatti 
10832cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
10842cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
10852cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
10862cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
10872cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
10882cef4debSChristian Borntraeger 	 */
10892cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
10902cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
10912cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
10922cef4debSChristian Borntraeger 		return;
1093598841caSCarsten Otte 
1094598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
1095598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
1096598841caSCarsten Otte 	if (rc)
1097f7850c92SCarsten Otte 		printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
1098598841caSCarsten Otte 	return;
1099b0c632dbSHeiko Carstens }
1100b0c632dbSHeiko Carstens 
11012df72e9bSMarcelo Tosatti void kvm_arch_flush_shadow_all(struct kvm *kvm)
11022df72e9bSMarcelo Tosatti {
11032df72e9bSMarcelo Tosatti }
11042df72e9bSMarcelo Tosatti 
11052df72e9bSMarcelo Tosatti void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
11062df72e9bSMarcelo Tosatti 				   struct kvm_memory_slot *slot)
110734d4cb8fSMarcelo Tosatti {
110834d4cb8fSMarcelo Tosatti }
110934d4cb8fSMarcelo Tosatti 
1110b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
1111b0c632dbSHeiko Carstens {
1112ef50f7acSChristian Borntraeger 	int ret;
11130ee75beaSAvi Kivity 	ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1114ef50f7acSChristian Borntraeger 	if (ret)
1115ef50f7acSChristian Borntraeger 		return ret;
1116ef50f7acSChristian Borntraeger 
1117ef50f7acSChristian Borntraeger 	/*
1118ef50f7acSChristian Borntraeger 	 * guests can ask for up to 255+1 double words, we need a full page
111925985edcSLucas De Marchi 	 * to hold the maximum amount of facilities. On the other hand, we
1120ef50f7acSChristian Borntraeger 	 * only set facilities that are known to work in KVM.
1121ef50f7acSChristian Borntraeger 	 */
1122c2f0e8c8SHeiko Carstens 	facilities = (unsigned long long *) get_zeroed_page(GFP_KERNEL|GFP_DMA);
1123ef50f7acSChristian Borntraeger 	if (!facilities) {
1124ef50f7acSChristian Borntraeger 		kvm_exit();
1125ef50f7acSChristian Borntraeger 		return -ENOMEM;
1126ef50f7acSChristian Borntraeger 	}
112714375bc4SMartin Schwidefsky 	memcpy(facilities, S390_lowcore.stfle_fac_list, 16);
11286d00d00bSChristian Borntraeger 	facilities[0] &= 0xff00fff3f47c0000ULL;
112987cac8f8SChristian Borntraeger 	facilities[1] &= 0x001c000000000000ULL;
1130ef50f7acSChristian Borntraeger 	return 0;
1131b0c632dbSHeiko Carstens }
1132b0c632dbSHeiko Carstens 
1133b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
1134b0c632dbSHeiko Carstens {
1135ef50f7acSChristian Borntraeger 	free_page((unsigned long) facilities);
1136b0c632dbSHeiko Carstens 	kvm_exit();
1137b0c632dbSHeiko Carstens }
1138b0c632dbSHeiko Carstens 
1139b0c632dbSHeiko Carstens module_init(kvm_s390_init);
1140b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
1141