xref: /linux/arch/s390/kvm/kvm-s390.c (revision 4725c86055f5bbdcdfe47199c0715881893a2c79)
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>
3178c4b59fSMichael Mueller #include <asm/facility.h>
321526bf9cSChristian Borntraeger #include <asm/sclp.h>
338f2abe6aSChristian Borntraeger #include "kvm-s390.h"
34b0c632dbSHeiko Carstens #include "gaccess.h"
35b0c632dbSHeiko Carstens 
365786fffaSCornelia Huck #define CREATE_TRACE_POINTS
375786fffaSCornelia Huck #include "trace.h"
38ade38c31SCornelia Huck #include "trace-s390.h"
395786fffaSCornelia Huck 
40b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
41b0c632dbSHeiko Carstens 
42b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
43b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
440eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
458f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
468f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
478f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
488f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
49ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
50ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
51ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
52f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
53ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
54ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
557697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
56ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
57ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
58ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
59ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
60ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
61ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
62ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
6369d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
64453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
65453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
66453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
67453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
68453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
69453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
70453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
71453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
72453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
73bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
745288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
75bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
767697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
775288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
785288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
795288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
805288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
815288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
82388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
83e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
8441628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
85b0c632dbSHeiko Carstens 	{ NULL }
86b0c632dbSHeiko Carstens };
87b0c632dbSHeiko Carstens 
8878c4b59fSMichael Mueller unsigned long *vfacilities;
892c70fe44SChristian Borntraeger static struct gmap_notifier gmap_notifier;
90b0c632dbSHeiko Carstens 
9178c4b59fSMichael Mueller /* test availability of vfacility */
9278c4b59fSMichael Mueller static inline int test_vfacility(unsigned long nr)
9378c4b59fSMichael Mueller {
9478c4b59fSMichael Mueller 	return __test_facility(nr, (void *) vfacilities);
9578c4b59fSMichael Mueller }
9678c4b59fSMichael Mueller 
97b0c632dbSHeiko Carstens /* Section: not file related */
9810474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage)
99b0c632dbSHeiko Carstens {
100b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
10110474ae8SAlexander Graf 	return 0;
102b0c632dbSHeiko Carstens }
103b0c632dbSHeiko Carstens 
104b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage)
105b0c632dbSHeiko Carstens {
106b0c632dbSHeiko Carstens }
107b0c632dbSHeiko Carstens 
1082c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
1092c70fe44SChristian Borntraeger 
110b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
111b0c632dbSHeiko Carstens {
1122c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
1132c70fe44SChristian Borntraeger 	gmap_register_ipte_notifier(&gmap_notifier);
114b0c632dbSHeiko Carstens 	return 0;
115b0c632dbSHeiko Carstens }
116b0c632dbSHeiko Carstens 
117b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
118b0c632dbSHeiko Carstens {
1192c70fe44SChristian Borntraeger 	gmap_unregister_ipte_notifier(&gmap_notifier);
120b0c632dbSHeiko Carstens }
121b0c632dbSHeiko Carstens 
122b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn)
123b0c632dbSHeiko Carstens {
124b0c632dbSHeiko Carstens }
125b0c632dbSHeiko Carstens 
126b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
127b0c632dbSHeiko Carstens {
128b0c632dbSHeiko Carstens 	return 0;
129b0c632dbSHeiko Carstens }
130b0c632dbSHeiko Carstens 
131b0c632dbSHeiko Carstens void kvm_arch_exit(void)
132b0c632dbSHeiko Carstens {
133b0c632dbSHeiko Carstens }
134b0c632dbSHeiko Carstens 
135b0c632dbSHeiko Carstens /* Section: device related */
136b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
137b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
138b0c632dbSHeiko Carstens {
139b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
140b0c632dbSHeiko Carstens 		return s390_enable_sie();
141b0c632dbSHeiko Carstens 	return -EINVAL;
142b0c632dbSHeiko Carstens }
143b0c632dbSHeiko Carstens 
144b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext)
145b0c632dbSHeiko Carstens {
146d7b0b5ebSCarsten Otte 	int r;
147d7b0b5ebSCarsten Otte 
1482bd0ac4eSCarsten Otte 	switch (ext) {
149d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
150b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
15152e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
1521efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1531efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
1541efd0f59SCarsten Otte #endif
15560b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
15614eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
157d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
158fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
15910ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
160d7b0b5ebSCarsten Otte 		r = 1;
161d7b0b5ebSCarsten Otte 		break;
162e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
163e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
164e726b1bdSChristian Borntraeger 		r = KVM_MAX_VCPUS;
165e726b1bdSChristian Borntraeger 		break;
166e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
167e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
168e1e2e605SNick Wang 		break;
1691526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
170abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
1711526bf9cSChristian Borntraeger 		break;
1722bd0ac4eSCarsten Otte 	default:
173d7b0b5ebSCarsten Otte 		r = 0;
174b0c632dbSHeiko Carstens 	}
175d7b0b5ebSCarsten Otte 	return r;
1762bd0ac4eSCarsten Otte }
177b0c632dbSHeiko Carstens 
178b0c632dbSHeiko Carstens /* Section: vm related */
179b0c632dbSHeiko Carstens /*
180b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
181b0c632dbSHeiko Carstens  */
182b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
183b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
184b0c632dbSHeiko Carstens {
185b0c632dbSHeiko Carstens 	return 0;
186b0c632dbSHeiko Carstens }
187b0c632dbSHeiko Carstens 
188b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
189b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
190b0c632dbSHeiko Carstens {
191b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
192b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
193b0c632dbSHeiko Carstens 	int r;
194b0c632dbSHeiko Carstens 
195b0c632dbSHeiko Carstens 	switch (ioctl) {
196ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
197ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
198ba5c1e9bSCarsten Otte 
199ba5c1e9bSCarsten Otte 		r = -EFAULT;
200ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
201ba5c1e9bSCarsten Otte 			break;
202ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
203ba5c1e9bSCarsten Otte 		break;
204ba5c1e9bSCarsten Otte 	}
205b0c632dbSHeiko Carstens 	default:
206367e1319SAvi Kivity 		r = -ENOTTY;
207b0c632dbSHeiko Carstens 	}
208b0c632dbSHeiko Carstens 
209b0c632dbSHeiko Carstens 	return r;
210b0c632dbSHeiko Carstens }
211b0c632dbSHeiko Carstens 
212e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
213b0c632dbSHeiko Carstens {
214b0c632dbSHeiko Carstens 	int rc;
215b0c632dbSHeiko Carstens 	char debug_name[16];
216b0c632dbSHeiko Carstens 
217e08b9637SCarsten Otte 	rc = -EINVAL;
218e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
219e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
220e08b9637SCarsten Otte 		goto out_err;
221e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
222e08b9637SCarsten Otte 		goto out_err;
223e08b9637SCarsten Otte #else
224e08b9637SCarsten Otte 	if (type)
225e08b9637SCarsten Otte 		goto out_err;
226e08b9637SCarsten Otte #endif
227e08b9637SCarsten Otte 
228b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
229b0c632dbSHeiko Carstens 	if (rc)
230d89f5effSJan Kiszka 		goto out_err;
231b0c632dbSHeiko Carstens 
232b290411aSCarsten Otte 	rc = -ENOMEM;
233b290411aSCarsten Otte 
234b0c632dbSHeiko Carstens 	kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
235b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
236d89f5effSJan Kiszka 		goto out_err;
237b0c632dbSHeiko Carstens 
238b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
239b0c632dbSHeiko Carstens 
240b0c632dbSHeiko Carstens 	kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
241b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
242b0c632dbSHeiko Carstens 		goto out_nodbf;
243b0c632dbSHeiko Carstens 
244ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
245ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&kvm->arch.float_int.list);
246ba5c1e9bSCarsten Otte 
247b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
248b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "%s", "vm created");
249b0c632dbSHeiko Carstens 
250e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
251e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
252e08b9637SCarsten Otte 	} else {
253598841caSCarsten Otte 		kvm->arch.gmap = gmap_alloc(current->mm);
254598841caSCarsten Otte 		if (!kvm->arch.gmap)
255598841caSCarsten Otte 			goto out_nogmap;
2562c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
257e08b9637SCarsten Otte 	}
258fa6b7fe9SCornelia Huck 
259fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
260fa6b7fe9SCornelia Huck 
261d89f5effSJan Kiszka 	return 0;
262598841caSCarsten Otte out_nogmap:
263598841caSCarsten Otte 	debug_unregister(kvm->arch.dbf);
264b0c632dbSHeiko Carstens out_nodbf:
265b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
266d89f5effSJan Kiszka out_err:
267d89f5effSJan Kiszka 	return rc;
268b0c632dbSHeiko Carstens }
269b0c632dbSHeiko Carstens 
270d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
271d329c035SChristian Borntraeger {
272d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
273ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
27458f9460bSCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm)) {
27558f9460bSCarsten Otte 		clear_bit(63 - vcpu->vcpu_id,
27658f9460bSCarsten Otte 			  (unsigned long *) &vcpu->kvm->arch.sca->mcn);
277abf4a71eSCarsten Otte 		if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
278abf4a71eSCarsten Otte 		    (__u64) vcpu->arch.sie_block)
279abf4a71eSCarsten Otte 			vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
28058f9460bSCarsten Otte 	}
281abf4a71eSCarsten Otte 	smp_mb();
28227e0393fSCarsten Otte 
28327e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
28427e0393fSCarsten Otte 		gmap_free(vcpu->arch.gmap);
28527e0393fSCarsten Otte 
286d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2876692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
288b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
289d329c035SChristian Borntraeger }
290d329c035SChristian Borntraeger 
291d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
292d329c035SChristian Borntraeger {
293d329c035SChristian Borntraeger 	unsigned int i;
294988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
295d329c035SChristian Borntraeger 
296988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
297988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
298988a2caeSGleb Natapov 
299988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
300988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
301d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
302988a2caeSGleb Natapov 
303988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
304988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
305d329c035SChristian Borntraeger }
306d329c035SChristian Borntraeger 
307ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm)
308ad8ba2cdSSheng Yang {
309ad8ba2cdSSheng Yang }
310ad8ba2cdSSheng Yang 
311b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
312b0c632dbSHeiko Carstens {
313d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
314b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
315d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
31627e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
317598841caSCarsten Otte 		gmap_free(kvm->arch.gmap);
318b0c632dbSHeiko Carstens }
319b0c632dbSHeiko Carstens 
320b0c632dbSHeiko Carstens /* Section: vcpu related */
321b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
322b0c632dbSHeiko Carstens {
32327e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm)) {
32427e0393fSCarsten Otte 		vcpu->arch.gmap = gmap_alloc(current->mm);
32527e0393fSCarsten Otte 		if (!vcpu->arch.gmap)
32627e0393fSCarsten Otte 			return -ENOMEM;
3272c70fe44SChristian Borntraeger 		vcpu->arch.gmap->private = vcpu->kvm;
32827e0393fSCarsten Otte 		return 0;
32927e0393fSCarsten Otte 	}
33027e0393fSCarsten Otte 
331598841caSCarsten Otte 	vcpu->arch.gmap = vcpu->kvm->arch.gmap;
33259674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
33359674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
3349eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
3359eed0735SChristian Borntraeger 				    KVM_SYNC_CRS;
336b0c632dbSHeiko Carstens 	return 0;
337b0c632dbSHeiko Carstens }
338b0c632dbSHeiko Carstens 
339b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
340b0c632dbSHeiko Carstens {
3416692cef3SChristian Borntraeger 	/* Nothing todo */
342b0c632dbSHeiko Carstens }
343b0c632dbSHeiko Carstens 
344b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
345b0c632dbSHeiko Carstens {
346*4725c860SMartin Schwidefsky 	save_fp_ctl(&vcpu->arch.host_fpregs.fpc);
347*4725c860SMartin Schwidefsky 	save_fp_regs(vcpu->arch.host_fpregs.fprs);
348b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.host_acrs);
349*4725c860SMartin Schwidefsky 	restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
350*4725c860SMartin Schwidefsky 	restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
35159674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
352480e5926SChristian Borntraeger 	gmap_enable(vcpu->arch.gmap);
3539e6dabefSCornelia Huck 	atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
354b0c632dbSHeiko Carstens }
355b0c632dbSHeiko Carstens 
356b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
357b0c632dbSHeiko Carstens {
3589e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
359480e5926SChristian Borntraeger 	gmap_disable(vcpu->arch.gmap);
360*4725c860SMartin Schwidefsky 	save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
361*4725c860SMartin Schwidefsky 	save_fp_regs(vcpu->arch.guest_fpregs.fprs);
36259674c1aSChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
363*4725c860SMartin Schwidefsky 	restore_fp_ctl(&vcpu->arch.host_fpregs.fpc);
364*4725c860SMartin Schwidefsky 	restore_fp_regs(vcpu->arch.host_fpregs.fprs);
365b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
366b0c632dbSHeiko Carstens }
367b0c632dbSHeiko Carstens 
368b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
369b0c632dbSHeiko Carstens {
370b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
371b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
372b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
3738d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
374b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
375b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
376b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
377b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
378b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
379b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
380b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
381b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
382b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
38361bde82cSChristian Borntraeger 	atomic_set_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
384b0c632dbSHeiko Carstens }
385b0c632dbSHeiko Carstens 
38642897d86SMarcelo Tosatti int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
38742897d86SMarcelo Tosatti {
38842897d86SMarcelo Tosatti 	return 0;
38942897d86SMarcelo Tosatti }
39042897d86SMarcelo Tosatti 
391b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
392b0c632dbSHeiko Carstens {
3939e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3949e6dabefSCornelia Huck 						    CPUSTAT_SM |
39569d0d3a3SChristian Borntraeger 						    CPUSTAT_STOPPED |
39669d0d3a3SChristian Borntraeger 						    CPUSTAT_GED);
397fc34531dSChristian Borntraeger 	vcpu->arch.sie_block->ecb   = 6;
39869d0d3a3SChristian Borntraeger 	vcpu->arch.sie_block->ecb2  = 8;
399b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->eca   = 0xC1002001U;
40078c4b59fSMichael Mueller 	vcpu->arch.sie_block->fac   = (int) (long) vfacilities;
401ca872302SChristian Borntraeger 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
402ca872302SChristian Borntraeger 	tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet,
403ba5c1e9bSCarsten Otte 		     (unsigned long) vcpu);
404ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
405453423dcSChristian Borntraeger 	get_cpu_id(&vcpu->arch.cpu_id);
40692e6ecf3SChristian Borntraeger 	vcpu->arch.cpu_id.version = 0xff;
407b0c632dbSHeiko Carstens 	return 0;
408b0c632dbSHeiko Carstens }
409b0c632dbSHeiko Carstens 
410b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
411b0c632dbSHeiko Carstens 				      unsigned int id)
412b0c632dbSHeiko Carstens {
4134d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
4144d47555aSCarsten Otte 	int rc = -EINVAL;
415b0c632dbSHeiko Carstens 
4164d47555aSCarsten Otte 	if (id >= KVM_MAX_VCPUS)
4174d47555aSCarsten Otte 		goto out;
4184d47555aSCarsten Otte 
4194d47555aSCarsten Otte 	rc = -ENOMEM;
4204d47555aSCarsten Otte 
421b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
422b0c632dbSHeiko Carstens 	if (!vcpu)
4234d47555aSCarsten Otte 		goto out;
424b0c632dbSHeiko Carstens 
425180c12fbSChristian Borntraeger 	vcpu->arch.sie_block = (struct kvm_s390_sie_block *)
426180c12fbSChristian Borntraeger 					get_zeroed_page(GFP_KERNEL);
427b0c632dbSHeiko Carstens 
428b0c632dbSHeiko Carstens 	if (!vcpu->arch.sie_block)
429b0c632dbSHeiko Carstens 		goto out_free_cpu;
430b0c632dbSHeiko Carstens 
431b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
43258f9460bSCarsten Otte 	if (!kvm_is_ucontrol(kvm)) {
43358f9460bSCarsten Otte 		if (!kvm->arch.sca) {
43458f9460bSCarsten Otte 			WARN_ON_ONCE(1);
43558f9460bSCarsten Otte 			goto out_free_cpu;
43658f9460bSCarsten Otte 		}
437abf4a71eSCarsten Otte 		if (!kvm->arch.sca->cpu[id].sda)
43858f9460bSCarsten Otte 			kvm->arch.sca->cpu[id].sda =
43958f9460bSCarsten Otte 				(__u64) vcpu->arch.sie_block;
44058f9460bSCarsten Otte 		vcpu->arch.sie_block->scaoh =
44158f9460bSCarsten Otte 			(__u32)(((__u64)kvm->arch.sca) >> 32);
442b0c632dbSHeiko Carstens 		vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
443fc34531dSChristian Borntraeger 		set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
44458f9460bSCarsten Otte 	}
445b0c632dbSHeiko Carstens 
446ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
447ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
448ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
449b037a4f3SChristian Borntraeger 	spin_lock(&kvm->arch.float_int.lock);
450ba5c1e9bSCarsten Otte 	kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int;
451d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
4525288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
453b037a4f3SChristian Borntraeger 	spin_unlock(&kvm->arch.float_int.lock);
454ba5c1e9bSCarsten Otte 
455b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
456b0c632dbSHeiko Carstens 	if (rc)
4577b06bf2fSWei Yongjun 		goto out_free_sie_block;
458b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
459b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
460ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
461b0c632dbSHeiko Carstens 
462b0c632dbSHeiko Carstens 	return vcpu;
4637b06bf2fSWei Yongjun out_free_sie_block:
4647b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
465b0c632dbSHeiko Carstens out_free_cpu:
466b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
4674d47555aSCarsten Otte out:
468b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
469b0c632dbSHeiko Carstens }
470b0c632dbSHeiko Carstens 
471b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
472b0c632dbSHeiko Carstens {
473b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but never calls it */
474b0c632dbSHeiko Carstens 	BUG();
475b0c632dbSHeiko Carstens 	return 0;
476b0c632dbSHeiko Carstens }
477b0c632dbSHeiko Carstens 
47849b99e1eSChristian Borntraeger void s390_vcpu_block(struct kvm_vcpu *vcpu)
47949b99e1eSChristian Borntraeger {
48049b99e1eSChristian Borntraeger 	atomic_set_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
48149b99e1eSChristian Borntraeger }
48249b99e1eSChristian Borntraeger 
48349b99e1eSChristian Borntraeger void s390_vcpu_unblock(struct kvm_vcpu *vcpu)
48449b99e1eSChristian Borntraeger {
48549b99e1eSChristian Borntraeger 	atomic_clear_mask(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
48649b99e1eSChristian Borntraeger }
48749b99e1eSChristian Borntraeger 
48849b99e1eSChristian Borntraeger /*
48949b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
49049b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
49149b99e1eSChristian Borntraeger  * return immediately. */
49249b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
49349b99e1eSChristian Borntraeger {
49449b99e1eSChristian Borntraeger 	atomic_set_mask(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
49549b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
49649b99e1eSChristian Borntraeger 		cpu_relax();
49749b99e1eSChristian Borntraeger }
49849b99e1eSChristian Borntraeger 
49949b99e1eSChristian Borntraeger /* Kick a guest cpu out of SIE and prevent SIE-reentry */
50049b99e1eSChristian Borntraeger void exit_sie_sync(struct kvm_vcpu *vcpu)
50149b99e1eSChristian Borntraeger {
50249b99e1eSChristian Borntraeger 	s390_vcpu_block(vcpu);
50349b99e1eSChristian Borntraeger 	exit_sie(vcpu);
50449b99e1eSChristian Borntraeger }
50549b99e1eSChristian Borntraeger 
5062c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
5072c70fe44SChristian Borntraeger {
5082c70fe44SChristian Borntraeger 	int i;
5092c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
5102c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
5112c70fe44SChristian Borntraeger 
5122c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
5132c70fe44SChristian Borntraeger 		/* match against both prefix pages */
5142c70fe44SChristian Borntraeger 		if (vcpu->arch.sie_block->prefix == (address & ~0x1000UL)) {
5152c70fe44SChristian Borntraeger 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
5162c70fe44SChristian Borntraeger 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
5172c70fe44SChristian Borntraeger 			exit_sie_sync(vcpu);
5182c70fe44SChristian Borntraeger 		}
5192c70fe44SChristian Borntraeger 	}
5202c70fe44SChristian Borntraeger }
5212c70fe44SChristian Borntraeger 
522b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
523b6d33834SChristoffer Dall {
524b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
525b6d33834SChristoffer Dall 	BUG();
526b6d33834SChristoffer Dall 	return 0;
527b6d33834SChristoffer Dall }
528b6d33834SChristoffer Dall 
52914eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
53014eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
53114eebd91SCarsten Otte {
53214eebd91SCarsten Otte 	int r = -EINVAL;
53314eebd91SCarsten Otte 
53414eebd91SCarsten Otte 	switch (reg->id) {
53529b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
53629b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
53729b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
53829b7c71bSCarsten Otte 		break;
53929b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
54029b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
54129b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
54229b7c71bSCarsten Otte 		break;
54346a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
54446a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->cputm,
54546a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
54646a6dd1cSJason J. herne 		break;
54746a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
54846a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
54946a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
55046a6dd1cSJason J. herne 		break;
55114eebd91SCarsten Otte 	default:
55214eebd91SCarsten Otte 		break;
55314eebd91SCarsten Otte 	}
55414eebd91SCarsten Otte 
55514eebd91SCarsten Otte 	return r;
55614eebd91SCarsten Otte }
55714eebd91SCarsten Otte 
55814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
55914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
56014eebd91SCarsten Otte {
56114eebd91SCarsten Otte 	int r = -EINVAL;
56214eebd91SCarsten Otte 
56314eebd91SCarsten Otte 	switch (reg->id) {
56429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
56529b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
56629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
56729b7c71bSCarsten Otte 		break;
56829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
56929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
57029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
57129b7c71bSCarsten Otte 		break;
57246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
57346a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->cputm,
57446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
57546a6dd1cSJason J. herne 		break;
57646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
57746a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
57846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
57946a6dd1cSJason J. herne 		break;
58014eebd91SCarsten Otte 	default:
58114eebd91SCarsten Otte 		break;
58214eebd91SCarsten Otte 	}
58314eebd91SCarsten Otte 
58414eebd91SCarsten Otte 	return r;
58514eebd91SCarsten Otte }
586b6d33834SChristoffer Dall 
587b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
588b0c632dbSHeiko Carstens {
589b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
590b0c632dbSHeiko Carstens 	return 0;
591b0c632dbSHeiko Carstens }
592b0c632dbSHeiko Carstens 
593b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
594b0c632dbSHeiko Carstens {
5955a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
596b0c632dbSHeiko Carstens 	return 0;
597b0c632dbSHeiko Carstens }
598b0c632dbSHeiko Carstens 
599b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
600b0c632dbSHeiko Carstens {
6015a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
602b0c632dbSHeiko Carstens 	return 0;
603b0c632dbSHeiko Carstens }
604b0c632dbSHeiko Carstens 
605b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
606b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
607b0c632dbSHeiko Carstens {
60859674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
609b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
61059674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
611b0c632dbSHeiko Carstens 	return 0;
612b0c632dbSHeiko Carstens }
613b0c632dbSHeiko Carstens 
614b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
615b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
616b0c632dbSHeiko Carstens {
61759674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
618b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
619b0c632dbSHeiko Carstens 	return 0;
620b0c632dbSHeiko Carstens }
621b0c632dbSHeiko Carstens 
622b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
623b0c632dbSHeiko Carstens {
624*4725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
625*4725c860SMartin Schwidefsky 		return -EINVAL;
626b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
627*4725c860SMartin Schwidefsky 	vcpu->arch.guest_fpregs.fpc = fpu->fpc;
628*4725c860SMartin Schwidefsky 	restore_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
629*4725c860SMartin Schwidefsky 	restore_fp_regs(vcpu->arch.guest_fpregs.fprs);
630b0c632dbSHeiko Carstens 	return 0;
631b0c632dbSHeiko Carstens }
632b0c632dbSHeiko Carstens 
633b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
634b0c632dbSHeiko Carstens {
635b0c632dbSHeiko Carstens 	memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
636b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
637b0c632dbSHeiko Carstens 	return 0;
638b0c632dbSHeiko Carstens }
639b0c632dbSHeiko Carstens 
640b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
641b0c632dbSHeiko Carstens {
642b0c632dbSHeiko Carstens 	int rc = 0;
643b0c632dbSHeiko Carstens 
6449e6dabefSCornelia Huck 	if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED))
645b0c632dbSHeiko Carstens 		rc = -EBUSY;
646d7b0b5ebSCarsten Otte 	else {
647d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
648d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
649d7b0b5ebSCarsten Otte 	}
650b0c632dbSHeiko Carstens 	return rc;
651b0c632dbSHeiko Carstens }
652b0c632dbSHeiko Carstens 
653b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
654b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
655b0c632dbSHeiko Carstens {
656b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
657b0c632dbSHeiko Carstens }
658b0c632dbSHeiko Carstens 
659d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
660d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
661b0c632dbSHeiko Carstens {
662b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
663b0c632dbSHeiko Carstens }
664b0c632dbSHeiko Carstens 
66562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
66662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
66762d9f0dbSMarcelo Tosatti {
66862d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
66962d9f0dbSMarcelo Tosatti }
67062d9f0dbSMarcelo Tosatti 
67162d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
67262d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
67362d9f0dbSMarcelo Tosatti {
67462d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
67562d9f0dbSMarcelo Tosatti }
67662d9f0dbSMarcelo Tosatti 
6772c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
6782c70fe44SChristian Borntraeger {
6792c70fe44SChristian Borntraeger 	/*
6802c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
6812c70fe44SChristian Borntraeger 	 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
6822c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
6832c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
6842c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
6852c70fe44SChristian Borntraeger 	 */
6862c70fe44SChristian Borntraeger 	while (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
6872c70fe44SChristian Borntraeger 		int rc;
6882c70fe44SChristian Borntraeger 		rc = gmap_ipte_notify(vcpu->arch.gmap,
6892c70fe44SChristian Borntraeger 				      vcpu->arch.sie_block->prefix,
6902c70fe44SChristian Borntraeger 				      PAGE_SIZE * 2);
6912c70fe44SChristian Borntraeger 		if (rc)
6922c70fe44SChristian Borntraeger 			return rc;
6932c70fe44SChristian Borntraeger 		s390_vcpu_unblock(vcpu);
6942c70fe44SChristian Borntraeger 	}
6952c70fe44SChristian Borntraeger 	return 0;
6962c70fe44SChristian Borntraeger }
6972c70fe44SChristian Borntraeger 
698e168bf8dSCarsten Otte static int __vcpu_run(struct kvm_vcpu *vcpu)
699b0c632dbSHeiko Carstens {
700e168bf8dSCarsten Otte 	int rc;
701e168bf8dSCarsten Otte 
7025a32c1afSChristian Borntraeger 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
703b0c632dbSHeiko Carstens 
704b0c632dbSHeiko Carstens 	if (need_resched())
705b0c632dbSHeiko Carstens 		schedule();
706b0c632dbSHeiko Carstens 
70771cde587SChristian Borntraeger 	if (test_thread_flag(TIF_MCCK_PENDING))
70871cde587SChristian Borntraeger 		s390_handle_mcck();
70971cde587SChristian Borntraeger 
710d6b6d166SCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm))
7110ff31867SCarsten Otte 		kvm_s390_deliver_pending_interrupts(vcpu);
7120ff31867SCarsten Otte 
7132c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
7142c70fe44SChristian Borntraeger 	if (rc)
7152c70fe44SChristian Borntraeger 		return rc;
7162c70fe44SChristian Borntraeger 
717b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
718b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
719b0c632dbSHeiko Carstens 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
7205786fffaSCornelia Huck 	trace_kvm_s390_sie_enter(vcpu,
7215786fffaSCornelia Huck 				 atomic_read(&vcpu->arch.sie_block->cpuflags));
7222b29a9fdSDominik Dingel 
7232b29a9fdSDominik Dingel 	/*
7242b29a9fdSDominik Dingel 	 * As PF_VCPU will be used in fault handler, between guest_enter
7252b29a9fdSDominik Dingel 	 * and guest_exit should be no uaccess.
7262b29a9fdSDominik Dingel 	 */
7272b29a9fdSDominik Dingel 	preempt_disable();
7282b29a9fdSDominik Dingel 	kvm_guest_enter();
7292b29a9fdSDominik Dingel 	preempt_enable();
7305a32c1afSChristian Borntraeger 	rc = sie64a(vcpu->arch.sie_block, vcpu->run->s.regs.gprs);
7312b29a9fdSDominik Dingel 	kvm_guest_exit();
7322b29a9fdSDominik Dingel 
7332b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
7342b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
7352b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
7362b29a9fdSDominik Dingel 
7377c470539SMartin Schwidefsky 	if (rc > 0)
7387c470539SMartin Schwidefsky 		rc = 0;
7397c470539SMartin Schwidefsky 	if (rc < 0) {
740e168bf8dSCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm)) {
741e168bf8dSCarsten Otte 			rc = SIE_INTERCEPT_UCONTROL;
742e168bf8dSCarsten Otte 		} else {
7431f0d0f09SCarsten Otte 			VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
7445786fffaSCornelia Huck 			trace_kvm_s390_sie_fault(vcpu);
745db4a29cbSHeiko Carstens 			rc = kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
746e168bf8dSCarsten Otte 		}
7471f0d0f09SCarsten Otte 	}
748b0c632dbSHeiko Carstens 
7495a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
750e168bf8dSCarsten Otte 	return rc;
751b0c632dbSHeiko Carstens }
752b0c632dbSHeiko Carstens 
753b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
754b0c632dbSHeiko Carstens {
7558f2abe6aSChristian Borntraeger 	int rc;
756b0c632dbSHeiko Carstens 	sigset_t sigsaved;
757b0c632dbSHeiko Carstens 
7589ace903dSChristian Ehrhardt rerun_vcpu:
759b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
760b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
761b0c632dbSHeiko Carstens 
7629e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
763b0c632dbSHeiko Carstens 
764ba5c1e9bSCarsten Otte 	BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL);
765ba5c1e9bSCarsten Otte 
7668f2abe6aSChristian Borntraeger 	switch (kvm_run->exit_reason) {
7678f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_SIEIC:
7688f2abe6aSChristian Borntraeger 	case KVM_EXIT_UNKNOWN:
7699ace903dSChristian Ehrhardt 	case KVM_EXIT_INTR:
7708f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_RESET:
771e168bf8dSCarsten Otte 	case KVM_EXIT_S390_UCONTROL:
772fa6b7fe9SCornelia Huck 	case KVM_EXIT_S390_TSCH:
7738f2abe6aSChristian Borntraeger 		break;
7748f2abe6aSChristian Borntraeger 	default:
7758f2abe6aSChristian Borntraeger 		BUG();
7768f2abe6aSChristian Borntraeger 	}
7778f2abe6aSChristian Borntraeger 
778d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
779d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
78060b413c9SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) {
78160b413c9SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_PREFIX;
78260b413c9SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
78360b413c9SChristian Borntraeger 	}
7849eed0735SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
7859eed0735SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_CRS;
7869eed0735SChristian Borntraeger 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
7879eed0735SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
7889eed0735SChristian Borntraeger 	}
789d7b0b5ebSCarsten Otte 
790dab4079dSHeiko Carstens 	might_fault();
7918f2abe6aSChristian Borntraeger 
7928f2abe6aSChristian Borntraeger 	do {
793e168bf8dSCarsten Otte 		rc = __vcpu_run(vcpu);
794e168bf8dSCarsten Otte 		if (rc)
795e168bf8dSCarsten Otte 			break;
796c0d744a9SCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm))
797c0d744a9SCarsten Otte 			rc = -EOPNOTSUPP;
798c0d744a9SCarsten Otte 		else
7998f2abe6aSChristian Borntraeger 			rc = kvm_handle_sie_intercept(vcpu);
8008f2abe6aSChristian Borntraeger 	} while (!signal_pending(current) && !rc);
8018f2abe6aSChristian Borntraeger 
8029ace903dSChristian Ehrhardt 	if (rc == SIE_INTERCEPT_RERUNVCPU)
8039ace903dSChristian Ehrhardt 		goto rerun_vcpu;
8049ace903dSChristian Ehrhardt 
805b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
806b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
8078f2abe6aSChristian Borntraeger 		rc = -EINTR;
808b1d16c49SChristian Ehrhardt 	}
8098f2abe6aSChristian Borntraeger 
810e168bf8dSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
811e168bf8dSCarsten Otte 	if (rc == SIE_INTERCEPT_UCONTROL) {
812e168bf8dSCarsten Otte 		kvm_run->exit_reason = KVM_EXIT_S390_UCONTROL;
813e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.trans_exc_code =
814e168bf8dSCarsten Otte 			current->thread.gmap_addr;
815e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.pgm_code = 0x10;
816e168bf8dSCarsten Otte 		rc = 0;
817e168bf8dSCarsten Otte 	}
818e168bf8dSCarsten Otte #endif
819e168bf8dSCarsten Otte 
820b8e660b8SHeiko Carstens 	if (rc == -EOPNOTSUPP) {
8218f2abe6aSChristian Borntraeger 		/* intercept cannot be handled in-kernel, prepare kvm-run */
8228f2abe6aSChristian Borntraeger 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
8238f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
8248f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipa      = vcpu->arch.sie_block->ipa;
8258f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipb      = vcpu->arch.sie_block->ipb;
8268f2abe6aSChristian Borntraeger 		rc = 0;
8278f2abe6aSChristian Borntraeger 	}
8288f2abe6aSChristian Borntraeger 
8298f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
8308f2abe6aSChristian Borntraeger 		/* intercept was handled, but userspace support is needed
8318f2abe6aSChristian Borntraeger 		 * kvm_run has been prepared by the handler */
8328f2abe6aSChristian Borntraeger 		rc = 0;
8338f2abe6aSChristian Borntraeger 	}
8348f2abe6aSChristian Borntraeger 
835d7b0b5ebSCarsten Otte 	kvm_run->psw_mask     = vcpu->arch.sie_block->gpsw.mask;
836d7b0b5ebSCarsten Otte 	kvm_run->psw_addr     = vcpu->arch.sie_block->gpsw.addr;
83760b413c9SChristian Borntraeger 	kvm_run->s.regs.prefix = vcpu->arch.sie_block->prefix;
8389eed0735SChristian Borntraeger 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
839d7b0b5ebSCarsten Otte 
840b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
841b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
842b0c632dbSHeiko Carstens 
843b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
8447e8e6ab4SHeiko Carstens 	return rc;
845b0c632dbSHeiko Carstens }
846b0c632dbSHeiko Carstens 
847092670cdSCarsten Otte static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, void *from,
848b0c632dbSHeiko Carstens 		       unsigned long n, int prefix)
849b0c632dbSHeiko Carstens {
850b0c632dbSHeiko Carstens 	if (prefix)
851b0c632dbSHeiko Carstens 		return copy_to_guest(vcpu, guestdest, from, n);
852b0c632dbSHeiko Carstens 	else
853b0c632dbSHeiko Carstens 		return copy_to_guest_absolute(vcpu, guestdest, from, n);
854b0c632dbSHeiko Carstens }
855b0c632dbSHeiko Carstens 
856b0c632dbSHeiko Carstens /*
857b0c632dbSHeiko Carstens  * store status at address
858b0c632dbSHeiko Carstens  * we use have two special cases:
859b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
860b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
861b0c632dbSHeiko Carstens  */
862971eb77fSChristian Borntraeger int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
863b0c632dbSHeiko Carstens {
864092670cdSCarsten Otte 	unsigned char archmode = 1;
865b0c632dbSHeiko Carstens 	int prefix;
866b0c632dbSHeiko Carstens 
867b0c632dbSHeiko Carstens 	if (addr == KVM_S390_STORE_STATUS_NOADDR) {
868b0c632dbSHeiko Carstens 		if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
869b0c632dbSHeiko Carstens 			return -EFAULT;
870b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
871b0c632dbSHeiko Carstens 		prefix = 0;
872b0c632dbSHeiko Carstens 	} else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
873b0c632dbSHeiko Carstens 		if (copy_to_guest(vcpu, 163ul, &archmode, 1))
874b0c632dbSHeiko Carstens 			return -EFAULT;
875b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
876b0c632dbSHeiko Carstens 		prefix = 1;
877b0c632dbSHeiko Carstens 	} else
878b0c632dbSHeiko Carstens 		prefix = 0;
879b0c632dbSHeiko Carstens 
88015bc8d84SChristian Borntraeger 	/*
88115bc8d84SChristian Borntraeger 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
88215bc8d84SChristian Borntraeger 	 * copying in vcpu load/put. Lets update our copies before we save
88315bc8d84SChristian Borntraeger 	 * it into the save area
88415bc8d84SChristian Borntraeger 	 */
885*4725c860SMartin Schwidefsky 	save_fp_ctl(&vcpu->arch.guest_fpregs.fpc);
886*4725c860SMartin Schwidefsky 	save_fp_regs(vcpu->arch.guest_fpregs.fprs);
88715bc8d84SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
88815bc8d84SChristian Borntraeger 
889f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, fp_regs),
890b0c632dbSHeiko Carstens 			vcpu->arch.guest_fpregs.fprs, 128, prefix))
891b0c632dbSHeiko Carstens 		return -EFAULT;
892b0c632dbSHeiko Carstens 
893f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, gp_regs),
8945a32c1afSChristian Borntraeger 			vcpu->run->s.regs.gprs, 128, prefix))
895b0c632dbSHeiko Carstens 		return -EFAULT;
896b0c632dbSHeiko Carstens 
897f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, psw),
898b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gpsw, 16, prefix))
899b0c632dbSHeiko Carstens 		return -EFAULT;
900b0c632dbSHeiko Carstens 
901f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, pref_reg),
902b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->prefix, 4, prefix))
903b0c632dbSHeiko Carstens 		return -EFAULT;
904b0c632dbSHeiko Carstens 
905b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
906f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, fp_ctrl_reg),
907b0c632dbSHeiko Carstens 			&vcpu->arch.guest_fpregs.fpc, 4, prefix))
908b0c632dbSHeiko Carstens 		return -EFAULT;
909b0c632dbSHeiko Carstens 
910f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, tod_reg),
911b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->todpr, 4, prefix))
912b0c632dbSHeiko Carstens 		return -EFAULT;
913b0c632dbSHeiko Carstens 
914f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, timer),
915b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->cputm, 8, prefix))
916b0c632dbSHeiko Carstens 		return -EFAULT;
917b0c632dbSHeiko Carstens 
918f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, clk_cmp),
919b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->ckc, 8, prefix))
920b0c632dbSHeiko Carstens 		return -EFAULT;
921b0c632dbSHeiko Carstens 
922f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, acc_regs),
92359674c1aSChristian Borntraeger 			&vcpu->run->s.regs.acrs, 64, prefix))
924b0c632dbSHeiko Carstens 		return -EFAULT;
925b0c632dbSHeiko Carstens 
926b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
927f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, ctrl_regs),
928b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gcr, 128, prefix))
929b0c632dbSHeiko Carstens 		return -EFAULT;
930b0c632dbSHeiko Carstens 	return 0;
931b0c632dbSHeiko Carstens }
932b0c632dbSHeiko Carstens 
933d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
934d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
935d6712df9SCornelia Huck {
936d6712df9SCornelia Huck 	int r;
937d6712df9SCornelia Huck 
938d6712df9SCornelia Huck 	if (cap->flags)
939d6712df9SCornelia Huck 		return -EINVAL;
940d6712df9SCornelia Huck 
941d6712df9SCornelia Huck 	switch (cap->cap) {
942fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
943fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
944fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
945fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
946fa6b7fe9SCornelia Huck 		}
947fa6b7fe9SCornelia Huck 		r = 0;
948fa6b7fe9SCornelia Huck 		break;
949d6712df9SCornelia Huck 	default:
950d6712df9SCornelia Huck 		r = -EINVAL;
951d6712df9SCornelia Huck 		break;
952d6712df9SCornelia Huck 	}
953d6712df9SCornelia Huck 	return r;
954d6712df9SCornelia Huck }
955d6712df9SCornelia Huck 
956b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
957b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
958b0c632dbSHeiko Carstens {
959b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
960b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
961bc923cc9SAvi Kivity 	long r;
962b0c632dbSHeiko Carstens 
96393736624SAvi Kivity 	switch (ioctl) {
96493736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
965ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
966ba5c1e9bSCarsten Otte 
96793736624SAvi Kivity 		r = -EFAULT;
968ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
96993736624SAvi Kivity 			break;
97093736624SAvi Kivity 		r = kvm_s390_inject_vcpu(vcpu, &s390int);
97193736624SAvi Kivity 		break;
972ba5c1e9bSCarsten Otte 	}
973b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
974bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
975bc923cc9SAvi Kivity 		break;
976b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
977b0c632dbSHeiko Carstens 		psw_t psw;
978b0c632dbSHeiko Carstens 
979bc923cc9SAvi Kivity 		r = -EFAULT;
980b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
981bc923cc9SAvi Kivity 			break;
982bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
983bc923cc9SAvi Kivity 		break;
984b0c632dbSHeiko Carstens 	}
985b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
986bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
987bc923cc9SAvi Kivity 		break;
98814eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
98914eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
99014eebd91SCarsten Otte 		struct kvm_one_reg reg;
99114eebd91SCarsten Otte 		r = -EFAULT;
99214eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
99314eebd91SCarsten Otte 			break;
99414eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
99514eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
99614eebd91SCarsten Otte 		else
99714eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
99814eebd91SCarsten Otte 		break;
99914eebd91SCarsten Otte 	}
100027e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
100127e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
100227e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
100327e0393fSCarsten Otte 
100427e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
100527e0393fSCarsten Otte 			r = -EFAULT;
100627e0393fSCarsten Otte 			break;
100727e0393fSCarsten Otte 		}
100827e0393fSCarsten Otte 
100927e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
101027e0393fSCarsten Otte 			r = -EINVAL;
101127e0393fSCarsten Otte 			break;
101227e0393fSCarsten Otte 		}
101327e0393fSCarsten Otte 
101427e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
101527e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
101627e0393fSCarsten Otte 		break;
101727e0393fSCarsten Otte 	}
101827e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
101927e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
102027e0393fSCarsten Otte 
102127e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
102227e0393fSCarsten Otte 			r = -EFAULT;
102327e0393fSCarsten Otte 			break;
102427e0393fSCarsten Otte 		}
102527e0393fSCarsten Otte 
102627e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
102727e0393fSCarsten Otte 			r = -EINVAL;
102827e0393fSCarsten Otte 			break;
102927e0393fSCarsten Otte 		}
103027e0393fSCarsten Otte 
103127e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
103227e0393fSCarsten Otte 			ucasmap.length);
103327e0393fSCarsten Otte 		break;
103427e0393fSCarsten Otte 	}
103527e0393fSCarsten Otte #endif
1036ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
1037ccc7910fSCarsten Otte 		r = gmap_fault(arg, vcpu->arch.gmap);
1038ccc7910fSCarsten Otte 		if (!IS_ERR_VALUE(r))
1039ccc7910fSCarsten Otte 			r = 0;
1040ccc7910fSCarsten Otte 		break;
1041ccc7910fSCarsten Otte 	}
1042d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
1043d6712df9SCornelia Huck 	{
1044d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
1045d6712df9SCornelia Huck 		r = -EFAULT;
1046d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1047d6712df9SCornelia Huck 			break;
1048d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
1049d6712df9SCornelia Huck 		break;
1050d6712df9SCornelia Huck 	}
1051b0c632dbSHeiko Carstens 	default:
10523e6afcf1SCarsten Otte 		r = -ENOTTY;
1053b0c632dbSHeiko Carstens 	}
1054bc923cc9SAvi Kivity 	return r;
1055b0c632dbSHeiko Carstens }
1056b0c632dbSHeiko Carstens 
10575b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
10585b1c1493SCarsten Otte {
10595b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
10605b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
10615b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
10625b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
10635b1c1493SCarsten Otte 		get_page(vmf->page);
10645b1c1493SCarsten Otte 		return 0;
10655b1c1493SCarsten Otte 	}
10665b1c1493SCarsten Otte #endif
10675b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
10685b1c1493SCarsten Otte }
10695b1c1493SCarsten Otte 
1070db3fe4ebSTakuya Yoshikawa void kvm_arch_free_memslot(struct kvm_memory_slot *free,
1071db3fe4ebSTakuya Yoshikawa 			   struct kvm_memory_slot *dont)
1072db3fe4ebSTakuya Yoshikawa {
1073db3fe4ebSTakuya Yoshikawa }
1074db3fe4ebSTakuya Yoshikawa 
1075db3fe4ebSTakuya Yoshikawa int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
1076db3fe4ebSTakuya Yoshikawa {
1077db3fe4ebSTakuya Yoshikawa 	return 0;
1078db3fe4ebSTakuya Yoshikawa }
1079db3fe4ebSTakuya Yoshikawa 
1080e59dbe09STakuya Yoshikawa void kvm_arch_memslots_updated(struct kvm *kvm)
1081e59dbe09STakuya Yoshikawa {
1082e59dbe09STakuya Yoshikawa }
1083e59dbe09STakuya Yoshikawa 
1084b0c632dbSHeiko Carstens /* Section: memory related */
1085f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
1086f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
10877b6195a9STakuya Yoshikawa 				   struct kvm_userspace_memory_region *mem,
10887b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
1089b0c632dbSHeiko Carstens {
1090dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
1091dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
1092dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
1093dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
1094b0c632dbSHeiko Carstens 
1095598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
1096b0c632dbSHeiko Carstens 		return -EINVAL;
1097b0c632dbSHeiko Carstens 
1098598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
1099b0c632dbSHeiko Carstens 		return -EINVAL;
1100b0c632dbSHeiko Carstens 
1101f7784b8eSMarcelo Tosatti 	return 0;
1102f7784b8eSMarcelo Tosatti }
1103f7784b8eSMarcelo Tosatti 
1104f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
1105f7784b8eSMarcelo Tosatti 				struct kvm_userspace_memory_region *mem,
11068482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
11078482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
1108f7784b8eSMarcelo Tosatti {
1109f7850c92SCarsten Otte 	int rc;
1110f7784b8eSMarcelo Tosatti 
11112cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
11122cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
11132cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
11142cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
11152cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
11162cef4debSChristian Borntraeger 	 */
11172cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
11182cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
11192cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
11202cef4debSChristian Borntraeger 		return;
1121598841caSCarsten Otte 
1122598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
1123598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
1124598841caSCarsten Otte 	if (rc)
1125f7850c92SCarsten Otte 		printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
1126598841caSCarsten Otte 	return;
1127b0c632dbSHeiko Carstens }
1128b0c632dbSHeiko Carstens 
11292df72e9bSMarcelo Tosatti void kvm_arch_flush_shadow_all(struct kvm *kvm)
11302df72e9bSMarcelo Tosatti {
11312df72e9bSMarcelo Tosatti }
11322df72e9bSMarcelo Tosatti 
11332df72e9bSMarcelo Tosatti void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
11342df72e9bSMarcelo Tosatti 				   struct kvm_memory_slot *slot)
113534d4cb8fSMarcelo Tosatti {
113634d4cb8fSMarcelo Tosatti }
113734d4cb8fSMarcelo Tosatti 
1138b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
1139b0c632dbSHeiko Carstens {
1140ef50f7acSChristian Borntraeger 	int ret;
11410ee75beaSAvi Kivity 	ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
1142ef50f7acSChristian Borntraeger 	if (ret)
1143ef50f7acSChristian Borntraeger 		return ret;
1144ef50f7acSChristian Borntraeger 
1145ef50f7acSChristian Borntraeger 	/*
1146ef50f7acSChristian Borntraeger 	 * guests can ask for up to 255+1 double words, we need a full page
114725985edcSLucas De Marchi 	 * to hold the maximum amount of facilities. On the other hand, we
1148ef50f7acSChristian Borntraeger 	 * only set facilities that are known to work in KVM.
1149ef50f7acSChristian Borntraeger 	 */
115078c4b59fSMichael Mueller 	vfacilities = (unsigned long *) get_zeroed_page(GFP_KERNEL|GFP_DMA);
115178c4b59fSMichael Mueller 	if (!vfacilities) {
1152ef50f7acSChristian Borntraeger 		kvm_exit();
1153ef50f7acSChristian Borntraeger 		return -ENOMEM;
1154ef50f7acSChristian Borntraeger 	}
115578c4b59fSMichael Mueller 	memcpy(vfacilities, S390_lowcore.stfle_fac_list, 16);
115678c4b59fSMichael Mueller 	vfacilities[0] &= 0xff82fff3f47c0000UL;
115778c4b59fSMichael Mueller 	vfacilities[1] &= 0x001c000000000000UL;
1158ef50f7acSChristian Borntraeger 	return 0;
1159b0c632dbSHeiko Carstens }
1160b0c632dbSHeiko Carstens 
1161b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
1162b0c632dbSHeiko Carstens {
116378c4b59fSMichael Mueller 	free_page((unsigned long) vfacilities);
1164b0c632dbSHeiko Carstens 	kvm_exit();
1165b0c632dbSHeiko Carstens }
1166b0c632dbSHeiko Carstens 
1167b0c632dbSHeiko Carstens module_init(kvm_s390_init);
1168b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
1169566af940SCornelia Huck 
1170566af940SCornelia Huck /*
1171566af940SCornelia Huck  * Enable autoloading of the kvm module.
1172566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
1173566af940SCornelia Huck  * since x86 takes a different approach.
1174566af940SCornelia Huck  */
1175566af940SCornelia Huck #include <linux/miscdevice.h>
1176566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
1177566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
1178