xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision 41628d334361670d825fb03c04568f5ef9f084dc)
1b0c632dbSHeiko Carstens /*
2b0c632dbSHeiko Carstens  * s390host.c --  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>
318f2abe6aSChristian Borntraeger #include "kvm-s390.h"
32b0c632dbSHeiko Carstens #include "gaccess.h"
33b0c632dbSHeiko Carstens 
34b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
35b0c632dbSHeiko Carstens 
36b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
37b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
380eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
398f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
408f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
418f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
428f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
43ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
44ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
45ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
46f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
47ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
48ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
497697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
50ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
51ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
52ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
53ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
54ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
55ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
56ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
57453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
58453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
59453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
60453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
61453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
62453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
63453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
64453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
65453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
66bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
675288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
68bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
697697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
705288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
715288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
725288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
735288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
745288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
75388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
76e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
77*41628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
78b0c632dbSHeiko Carstens 	{ NULL }
79b0c632dbSHeiko Carstens };
80b0c632dbSHeiko Carstens 
81ef50f7acSChristian Borntraeger static unsigned long long *facilities;
82b0c632dbSHeiko Carstens 
83b0c632dbSHeiko Carstens /* Section: not file related */
8410474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage)
85b0c632dbSHeiko Carstens {
86b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
8710474ae8SAlexander Graf 	return 0;
88b0c632dbSHeiko Carstens }
89b0c632dbSHeiko Carstens 
90b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage)
91b0c632dbSHeiko Carstens {
92b0c632dbSHeiko Carstens }
93b0c632dbSHeiko Carstens 
94b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
95b0c632dbSHeiko Carstens {
96b0c632dbSHeiko Carstens 	return 0;
97b0c632dbSHeiko Carstens }
98b0c632dbSHeiko Carstens 
99b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
100b0c632dbSHeiko Carstens {
101b0c632dbSHeiko Carstens }
102b0c632dbSHeiko Carstens 
103b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn)
104b0c632dbSHeiko Carstens {
105b0c632dbSHeiko Carstens }
106b0c632dbSHeiko Carstens 
107b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
108b0c632dbSHeiko Carstens {
109b0c632dbSHeiko Carstens 	return 0;
110b0c632dbSHeiko Carstens }
111b0c632dbSHeiko Carstens 
112b0c632dbSHeiko Carstens void kvm_arch_exit(void)
113b0c632dbSHeiko Carstens {
114b0c632dbSHeiko Carstens }
115b0c632dbSHeiko Carstens 
116b0c632dbSHeiko Carstens /* Section: device related */
117b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
118b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
119b0c632dbSHeiko Carstens {
120b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
121b0c632dbSHeiko Carstens 		return s390_enable_sie();
122b0c632dbSHeiko Carstens 	return -EINVAL;
123b0c632dbSHeiko Carstens }
124b0c632dbSHeiko Carstens 
125b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext)
126b0c632dbSHeiko Carstens {
127d7b0b5ebSCarsten Otte 	int r;
128d7b0b5ebSCarsten Otte 
1292bd0ac4eSCarsten Otte 	switch (ext) {
130d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
131b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
13252e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
1331efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1341efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
1351efd0f59SCarsten Otte #endif
13660b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
137d7b0b5ebSCarsten Otte 		r = 1;
138d7b0b5ebSCarsten Otte 		break;
1392bd0ac4eSCarsten Otte 	default:
140d7b0b5ebSCarsten Otte 		r = 0;
141b0c632dbSHeiko Carstens 	}
142d7b0b5ebSCarsten Otte 	return r;
1432bd0ac4eSCarsten Otte }
144b0c632dbSHeiko Carstens 
145b0c632dbSHeiko Carstens /* Section: vm related */
146b0c632dbSHeiko Carstens /*
147b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
148b0c632dbSHeiko Carstens  */
149b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
150b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
151b0c632dbSHeiko Carstens {
152b0c632dbSHeiko Carstens 	return 0;
153b0c632dbSHeiko Carstens }
154b0c632dbSHeiko Carstens 
155b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
156b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
157b0c632dbSHeiko Carstens {
158b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
159b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
160b0c632dbSHeiko Carstens 	int r;
161b0c632dbSHeiko Carstens 
162b0c632dbSHeiko Carstens 	switch (ioctl) {
163ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
164ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
165ba5c1e9bSCarsten Otte 
166ba5c1e9bSCarsten Otte 		r = -EFAULT;
167ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
168ba5c1e9bSCarsten Otte 			break;
169ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
170ba5c1e9bSCarsten Otte 		break;
171ba5c1e9bSCarsten Otte 	}
172b0c632dbSHeiko Carstens 	default:
173367e1319SAvi Kivity 		r = -ENOTTY;
174b0c632dbSHeiko Carstens 	}
175b0c632dbSHeiko Carstens 
176b0c632dbSHeiko Carstens 	return r;
177b0c632dbSHeiko Carstens }
178b0c632dbSHeiko Carstens 
179e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
180b0c632dbSHeiko Carstens {
181b0c632dbSHeiko Carstens 	int rc;
182b0c632dbSHeiko Carstens 	char debug_name[16];
183b0c632dbSHeiko Carstens 
184e08b9637SCarsten Otte 	rc = -EINVAL;
185e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
186e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
187e08b9637SCarsten Otte 		goto out_err;
188e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
189e08b9637SCarsten Otte 		goto out_err;
190e08b9637SCarsten Otte #else
191e08b9637SCarsten Otte 	if (type)
192e08b9637SCarsten Otte 		goto out_err;
193e08b9637SCarsten Otte #endif
194e08b9637SCarsten Otte 
195b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
196b0c632dbSHeiko Carstens 	if (rc)
197d89f5effSJan Kiszka 		goto out_err;
198b0c632dbSHeiko Carstens 
199b290411aSCarsten Otte 	rc = -ENOMEM;
200b290411aSCarsten Otte 
201b0c632dbSHeiko Carstens 	kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
202b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
203d89f5effSJan Kiszka 		goto out_err;
204b0c632dbSHeiko Carstens 
205b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
206b0c632dbSHeiko Carstens 
207b0c632dbSHeiko Carstens 	kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
208b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
209b0c632dbSHeiko Carstens 		goto out_nodbf;
210b0c632dbSHeiko Carstens 
211ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
212ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&kvm->arch.float_int.list);
213ba5c1e9bSCarsten Otte 
214b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
215b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "%s", "vm created");
216b0c632dbSHeiko Carstens 
217e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
218e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
219e08b9637SCarsten Otte 	} else {
220598841caSCarsten Otte 		kvm->arch.gmap = gmap_alloc(current->mm);
221598841caSCarsten Otte 		if (!kvm->arch.gmap)
222598841caSCarsten Otte 			goto out_nogmap;
223e08b9637SCarsten Otte 	}
224d89f5effSJan Kiszka 	return 0;
225598841caSCarsten Otte out_nogmap:
226598841caSCarsten Otte 	debug_unregister(kvm->arch.dbf);
227b0c632dbSHeiko Carstens out_nodbf:
228b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
229d89f5effSJan Kiszka out_err:
230d89f5effSJan Kiszka 	return rc;
231b0c632dbSHeiko Carstens }
232b0c632dbSHeiko Carstens 
233d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
234d329c035SChristian Borntraeger {
235d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
23658f9460bSCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm)) {
23758f9460bSCarsten Otte 		clear_bit(63 - vcpu->vcpu_id,
23858f9460bSCarsten Otte 			  (unsigned long *) &vcpu->kvm->arch.sca->mcn);
239abf4a71eSCarsten Otte 		if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
240abf4a71eSCarsten Otte 		    (__u64) vcpu->arch.sie_block)
241abf4a71eSCarsten Otte 			vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
24258f9460bSCarsten Otte 	}
243abf4a71eSCarsten Otte 	smp_mb();
24427e0393fSCarsten Otte 
24527e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
24627e0393fSCarsten Otte 		gmap_free(vcpu->arch.gmap);
24727e0393fSCarsten Otte 
248d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2496692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
250d329c035SChristian Borntraeger 	kfree(vcpu);
251d329c035SChristian Borntraeger }
252d329c035SChristian Borntraeger 
253d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
254d329c035SChristian Borntraeger {
255d329c035SChristian Borntraeger 	unsigned int i;
256988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
257d329c035SChristian Borntraeger 
258988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
259988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
260988a2caeSGleb Natapov 
261988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
262988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
263d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
264988a2caeSGleb Natapov 
265988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
266988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
267d329c035SChristian Borntraeger }
268d329c035SChristian Borntraeger 
269ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm)
270ad8ba2cdSSheng Yang {
271ad8ba2cdSSheng Yang }
272ad8ba2cdSSheng Yang 
273b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
274b0c632dbSHeiko Carstens {
275d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
276b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
277d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
27827e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
279598841caSCarsten Otte 		gmap_free(kvm->arch.gmap);
280b0c632dbSHeiko Carstens }
281b0c632dbSHeiko Carstens 
282b0c632dbSHeiko Carstens /* Section: vcpu related */
283b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
284b0c632dbSHeiko Carstens {
28527e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm)) {
28627e0393fSCarsten Otte 		vcpu->arch.gmap = gmap_alloc(current->mm);
28727e0393fSCarsten Otte 		if (!vcpu->arch.gmap)
28827e0393fSCarsten Otte 			return -ENOMEM;
28927e0393fSCarsten Otte 		return 0;
29027e0393fSCarsten Otte 	}
29127e0393fSCarsten Otte 
292598841caSCarsten Otte 	vcpu->arch.gmap = vcpu->kvm->arch.gmap;
29359674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
29459674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
2959eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
2969eed0735SChristian Borntraeger 				    KVM_SYNC_CRS;
297b0c632dbSHeiko Carstens 	return 0;
298b0c632dbSHeiko Carstens }
299b0c632dbSHeiko Carstens 
300b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
301b0c632dbSHeiko Carstens {
3026692cef3SChristian Borntraeger 	/* Nothing todo */
303b0c632dbSHeiko Carstens }
304b0c632dbSHeiko Carstens 
305b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
306b0c632dbSHeiko Carstens {
307b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.host_fpregs);
308b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.host_acrs);
309b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK;
310b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.guest_fpregs);
31159674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
312480e5926SChristian Borntraeger 	gmap_enable(vcpu->arch.gmap);
3139e6dabefSCornelia Huck 	atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
314b0c632dbSHeiko Carstens }
315b0c632dbSHeiko Carstens 
316b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
317b0c632dbSHeiko Carstens {
3189e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
319480e5926SChristian Borntraeger 	gmap_disable(vcpu->arch.gmap);
320b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.guest_fpregs);
32159674c1aSChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
322b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.host_fpregs);
323b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
324b0c632dbSHeiko Carstens }
325b0c632dbSHeiko Carstens 
326b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
327b0c632dbSHeiko Carstens {
328b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
329b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
330b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
3318d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
332b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
333b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
334b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
335b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
336b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
337b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
338b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
339b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
340b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
341b0c632dbSHeiko Carstens }
342b0c632dbSHeiko Carstens 
343b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
344b0c632dbSHeiko Carstens {
3459e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3469e6dabefSCornelia Huck 						    CPUSTAT_SM |
3479e6dabefSCornelia Huck 						    CPUSTAT_STOPPED);
348fc34531dSChristian Borntraeger 	vcpu->arch.sie_block->ecb   = 6;
349b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->eca   = 0xC1002001U;
350ef50f7acSChristian Borntraeger 	vcpu->arch.sie_block->fac   = (int) (long) facilities;
351ca872302SChristian Borntraeger 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
352ca872302SChristian Borntraeger 	tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet,
353ba5c1e9bSCarsten Otte 		     (unsigned long) vcpu);
354ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
355453423dcSChristian Borntraeger 	get_cpu_id(&vcpu->arch.cpu_id);
35692e6ecf3SChristian Borntraeger 	vcpu->arch.cpu_id.version = 0xff;
357b0c632dbSHeiko Carstens 	return 0;
358b0c632dbSHeiko Carstens }
359b0c632dbSHeiko Carstens 
360b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
361b0c632dbSHeiko Carstens 				      unsigned int id)
362b0c632dbSHeiko Carstens {
3634d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
3644d47555aSCarsten Otte 	int rc = -EINVAL;
365b0c632dbSHeiko Carstens 
3664d47555aSCarsten Otte 	if (id >= KVM_MAX_VCPUS)
3674d47555aSCarsten Otte 		goto out;
3684d47555aSCarsten Otte 
3694d47555aSCarsten Otte 	rc = -ENOMEM;
3704d47555aSCarsten Otte 
3714d47555aSCarsten Otte 	vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
372b0c632dbSHeiko Carstens 	if (!vcpu)
3734d47555aSCarsten Otte 		goto out;
374b0c632dbSHeiko Carstens 
375180c12fbSChristian Borntraeger 	vcpu->arch.sie_block = (struct kvm_s390_sie_block *)
376180c12fbSChristian Borntraeger 					get_zeroed_page(GFP_KERNEL);
377b0c632dbSHeiko Carstens 
378b0c632dbSHeiko Carstens 	if (!vcpu->arch.sie_block)
379b0c632dbSHeiko Carstens 		goto out_free_cpu;
380b0c632dbSHeiko Carstens 
381b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
38258f9460bSCarsten Otte 	if (!kvm_is_ucontrol(kvm)) {
38358f9460bSCarsten Otte 		if (!kvm->arch.sca) {
38458f9460bSCarsten Otte 			WARN_ON_ONCE(1);
38558f9460bSCarsten Otte 			goto out_free_cpu;
38658f9460bSCarsten Otte 		}
387abf4a71eSCarsten Otte 		if (!kvm->arch.sca->cpu[id].sda)
38858f9460bSCarsten Otte 			kvm->arch.sca->cpu[id].sda =
38958f9460bSCarsten Otte 				(__u64) vcpu->arch.sie_block;
39058f9460bSCarsten Otte 		vcpu->arch.sie_block->scaoh =
39158f9460bSCarsten Otte 			(__u32)(((__u64)kvm->arch.sca) >> 32);
392b0c632dbSHeiko Carstens 		vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
393fc34531dSChristian Borntraeger 		set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
39458f9460bSCarsten Otte 	}
395b0c632dbSHeiko Carstens 
396ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
397ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
398ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
399b037a4f3SChristian Borntraeger 	spin_lock(&kvm->arch.float_int.lock);
400ba5c1e9bSCarsten Otte 	kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int;
401ba5c1e9bSCarsten Otte 	init_waitqueue_head(&vcpu->arch.local_int.wq);
4025288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
403b037a4f3SChristian Borntraeger 	spin_unlock(&kvm->arch.float_int.lock);
404ba5c1e9bSCarsten Otte 
405b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
406b0c632dbSHeiko Carstens 	if (rc)
4077b06bf2fSWei Yongjun 		goto out_free_sie_block;
408b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
409b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
410b0c632dbSHeiko Carstens 
411b0c632dbSHeiko Carstens 	return vcpu;
4127b06bf2fSWei Yongjun out_free_sie_block:
4137b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
414b0c632dbSHeiko Carstens out_free_cpu:
415b0c632dbSHeiko Carstens 	kfree(vcpu);
4164d47555aSCarsten Otte out:
417b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
418b0c632dbSHeiko Carstens }
419b0c632dbSHeiko Carstens 
420b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
421b0c632dbSHeiko Carstens {
422b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but never calls it */
423b0c632dbSHeiko Carstens 	BUG();
424b0c632dbSHeiko Carstens 	return 0;
425b0c632dbSHeiko Carstens }
426b0c632dbSHeiko Carstens 
427b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
428b6d33834SChristoffer Dall {
429b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
430b6d33834SChristoffer Dall 	BUG();
431b6d33834SChristoffer Dall 	return 0;
432b6d33834SChristoffer Dall }
433b6d33834SChristoffer Dall 
434b6d33834SChristoffer Dall 
435b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
436b0c632dbSHeiko Carstens {
437b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
438b0c632dbSHeiko Carstens 	return 0;
439b0c632dbSHeiko Carstens }
440b0c632dbSHeiko Carstens 
441b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
442b0c632dbSHeiko Carstens {
4435a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
444b0c632dbSHeiko Carstens 	return 0;
445b0c632dbSHeiko Carstens }
446b0c632dbSHeiko Carstens 
447b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
448b0c632dbSHeiko Carstens {
4495a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
450b0c632dbSHeiko Carstens 	return 0;
451b0c632dbSHeiko Carstens }
452b0c632dbSHeiko Carstens 
453b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
454b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
455b0c632dbSHeiko Carstens {
45659674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
457b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
45859674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
459b0c632dbSHeiko Carstens 	return 0;
460b0c632dbSHeiko Carstens }
461b0c632dbSHeiko Carstens 
462b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
463b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
464b0c632dbSHeiko Carstens {
46559674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
466b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
467b0c632dbSHeiko Carstens 	return 0;
468b0c632dbSHeiko Carstens }
469b0c632dbSHeiko Carstens 
470b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
471b0c632dbSHeiko Carstens {
472b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
47385175587SChristian Borntraeger 	vcpu->arch.guest_fpregs.fpc = fpu->fpc & FPC_VALID_MASK;
4747eef87dcSCarsten Otte 	restore_fp_regs(&vcpu->arch.guest_fpregs);
475b0c632dbSHeiko Carstens 	return 0;
476b0c632dbSHeiko Carstens }
477b0c632dbSHeiko Carstens 
478b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
479b0c632dbSHeiko Carstens {
480b0c632dbSHeiko Carstens 	memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
481b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
482b0c632dbSHeiko Carstens 	return 0;
483b0c632dbSHeiko Carstens }
484b0c632dbSHeiko Carstens 
485b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
486b0c632dbSHeiko Carstens {
487b0c632dbSHeiko Carstens 	int rc = 0;
488b0c632dbSHeiko Carstens 
4899e6dabefSCornelia Huck 	if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED))
490b0c632dbSHeiko Carstens 		rc = -EBUSY;
491d7b0b5ebSCarsten Otte 	else {
492d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
493d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
494d7b0b5ebSCarsten Otte 	}
495b0c632dbSHeiko Carstens 	return rc;
496b0c632dbSHeiko Carstens }
497b0c632dbSHeiko Carstens 
498b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
499b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
500b0c632dbSHeiko Carstens {
501b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
502b0c632dbSHeiko Carstens }
503b0c632dbSHeiko Carstens 
504d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
505d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
506b0c632dbSHeiko Carstens {
507b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
508b0c632dbSHeiko Carstens }
509b0c632dbSHeiko Carstens 
51062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
51162d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
51262d9f0dbSMarcelo Tosatti {
51362d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
51462d9f0dbSMarcelo Tosatti }
51562d9f0dbSMarcelo Tosatti 
51662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
51762d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
51862d9f0dbSMarcelo Tosatti {
51962d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
52062d9f0dbSMarcelo Tosatti }
52162d9f0dbSMarcelo Tosatti 
522e168bf8dSCarsten Otte static int __vcpu_run(struct kvm_vcpu *vcpu)
523b0c632dbSHeiko Carstens {
524e168bf8dSCarsten Otte 	int rc;
525e168bf8dSCarsten Otte 
5265a32c1afSChristian Borntraeger 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
527b0c632dbSHeiko Carstens 
528b0c632dbSHeiko Carstens 	if (need_resched())
529b0c632dbSHeiko Carstens 		schedule();
530b0c632dbSHeiko Carstens 
53171cde587SChristian Borntraeger 	if (test_thread_flag(TIF_MCCK_PENDING))
53271cde587SChristian Borntraeger 		s390_handle_mcck();
53371cde587SChristian Borntraeger 
534d6b6d166SCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm))
5350ff31867SCarsten Otte 		kvm_s390_deliver_pending_interrupts(vcpu);
5360ff31867SCarsten Otte 
537b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
538b0c632dbSHeiko Carstens 	local_irq_disable();
539b0c632dbSHeiko Carstens 	kvm_guest_enter();
540b0c632dbSHeiko Carstens 	local_irq_enable();
541b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
542b0c632dbSHeiko Carstens 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
5435a32c1afSChristian Borntraeger 	rc = sie64a(vcpu->arch.sie_block, vcpu->run->s.regs.gprs);
544e168bf8dSCarsten Otte 	if (rc) {
545e168bf8dSCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm)) {
546e168bf8dSCarsten Otte 			rc = SIE_INTERCEPT_UCONTROL;
547e168bf8dSCarsten Otte 		} else {
5481f0d0f09SCarsten Otte 			VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
5491f0d0f09SCarsten Otte 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
550e168bf8dSCarsten Otte 			rc = 0;
551e168bf8dSCarsten Otte 		}
5521f0d0f09SCarsten Otte 	}
553b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
554b0c632dbSHeiko Carstens 		   vcpu->arch.sie_block->icptcode);
555b0c632dbSHeiko Carstens 	local_irq_disable();
556b0c632dbSHeiko Carstens 	kvm_guest_exit();
557b0c632dbSHeiko Carstens 	local_irq_enable();
558b0c632dbSHeiko Carstens 
5595a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
560e168bf8dSCarsten Otte 	return rc;
561b0c632dbSHeiko Carstens }
562b0c632dbSHeiko Carstens 
563b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
564b0c632dbSHeiko Carstens {
5658f2abe6aSChristian Borntraeger 	int rc;
566b0c632dbSHeiko Carstens 	sigset_t sigsaved;
567b0c632dbSHeiko Carstens 
5689ace903dSChristian Ehrhardt rerun_vcpu:
569b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
570b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
571b0c632dbSHeiko Carstens 
5729e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
573b0c632dbSHeiko Carstens 
574ba5c1e9bSCarsten Otte 	BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL);
575ba5c1e9bSCarsten Otte 
5768f2abe6aSChristian Borntraeger 	switch (kvm_run->exit_reason) {
5778f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_SIEIC:
5788f2abe6aSChristian Borntraeger 	case KVM_EXIT_UNKNOWN:
5799ace903dSChristian Ehrhardt 	case KVM_EXIT_INTR:
5808f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_RESET:
581e168bf8dSCarsten Otte 	case KVM_EXIT_S390_UCONTROL:
5828f2abe6aSChristian Borntraeger 		break;
5838f2abe6aSChristian Borntraeger 	default:
5848f2abe6aSChristian Borntraeger 		BUG();
5858f2abe6aSChristian Borntraeger 	}
5868f2abe6aSChristian Borntraeger 
587d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
588d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
58960b413c9SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) {
59060b413c9SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_PREFIX;
59160b413c9SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
59260b413c9SChristian Borntraeger 	}
5939eed0735SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
5949eed0735SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_CRS;
5959eed0735SChristian Borntraeger 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
5969eed0735SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
5979eed0735SChristian Borntraeger 	}
598d7b0b5ebSCarsten Otte 
599dab4079dSHeiko Carstens 	might_fault();
6008f2abe6aSChristian Borntraeger 
6018f2abe6aSChristian Borntraeger 	do {
602e168bf8dSCarsten Otte 		rc = __vcpu_run(vcpu);
603e168bf8dSCarsten Otte 		if (rc)
604e168bf8dSCarsten Otte 			break;
605c0d744a9SCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm))
606c0d744a9SCarsten Otte 			rc = -EOPNOTSUPP;
607c0d744a9SCarsten Otte 		else
6088f2abe6aSChristian Borntraeger 			rc = kvm_handle_sie_intercept(vcpu);
6098f2abe6aSChristian Borntraeger 	} while (!signal_pending(current) && !rc);
6108f2abe6aSChristian Borntraeger 
6119ace903dSChristian Ehrhardt 	if (rc == SIE_INTERCEPT_RERUNVCPU)
6129ace903dSChristian Ehrhardt 		goto rerun_vcpu;
6139ace903dSChristian Ehrhardt 
614b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
615b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
6168f2abe6aSChristian Borntraeger 		rc = -EINTR;
617b1d16c49SChristian Ehrhardt 	}
6188f2abe6aSChristian Borntraeger 
619e168bf8dSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
620e168bf8dSCarsten Otte 	if (rc == SIE_INTERCEPT_UCONTROL) {
621e168bf8dSCarsten Otte 		kvm_run->exit_reason = KVM_EXIT_S390_UCONTROL;
622e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.trans_exc_code =
623e168bf8dSCarsten Otte 			current->thread.gmap_addr;
624e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.pgm_code = 0x10;
625e168bf8dSCarsten Otte 		rc = 0;
626e168bf8dSCarsten Otte 	}
627e168bf8dSCarsten Otte #endif
628e168bf8dSCarsten Otte 
629b8e660b8SHeiko Carstens 	if (rc == -EOPNOTSUPP) {
6308f2abe6aSChristian Borntraeger 		/* intercept cannot be handled in-kernel, prepare kvm-run */
6318f2abe6aSChristian Borntraeger 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
6328f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
6338f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipa      = vcpu->arch.sie_block->ipa;
6348f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipb      = vcpu->arch.sie_block->ipb;
6358f2abe6aSChristian Borntraeger 		rc = 0;
6368f2abe6aSChristian Borntraeger 	}
6378f2abe6aSChristian Borntraeger 
6388f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
6398f2abe6aSChristian Borntraeger 		/* intercept was handled, but userspace support is needed
6408f2abe6aSChristian Borntraeger 		 * kvm_run has been prepared by the handler */
6418f2abe6aSChristian Borntraeger 		rc = 0;
6428f2abe6aSChristian Borntraeger 	}
6438f2abe6aSChristian Borntraeger 
644d7b0b5ebSCarsten Otte 	kvm_run->psw_mask     = vcpu->arch.sie_block->gpsw.mask;
645d7b0b5ebSCarsten Otte 	kvm_run->psw_addr     = vcpu->arch.sie_block->gpsw.addr;
64660b413c9SChristian Borntraeger 	kvm_run->s.regs.prefix = vcpu->arch.sie_block->prefix;
6479eed0735SChristian Borntraeger 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
648d7b0b5ebSCarsten Otte 
649b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
650b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
651b0c632dbSHeiko Carstens 
652b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
6537e8e6ab4SHeiko Carstens 	return rc;
654b0c632dbSHeiko Carstens }
655b0c632dbSHeiko Carstens 
656092670cdSCarsten Otte static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, void *from,
657b0c632dbSHeiko Carstens 		       unsigned long n, int prefix)
658b0c632dbSHeiko Carstens {
659b0c632dbSHeiko Carstens 	if (prefix)
660b0c632dbSHeiko Carstens 		return copy_to_guest(vcpu, guestdest, from, n);
661b0c632dbSHeiko Carstens 	else
662b0c632dbSHeiko Carstens 		return copy_to_guest_absolute(vcpu, guestdest, from, n);
663b0c632dbSHeiko Carstens }
664b0c632dbSHeiko Carstens 
665b0c632dbSHeiko Carstens /*
666b0c632dbSHeiko Carstens  * store status at address
667b0c632dbSHeiko Carstens  * we use have two special cases:
668b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
669b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
670b0c632dbSHeiko Carstens  */
671971eb77fSChristian Borntraeger int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
672b0c632dbSHeiko Carstens {
673092670cdSCarsten Otte 	unsigned char archmode = 1;
674b0c632dbSHeiko Carstens 	int prefix;
675b0c632dbSHeiko Carstens 
676b0c632dbSHeiko Carstens 	if (addr == KVM_S390_STORE_STATUS_NOADDR) {
677b0c632dbSHeiko Carstens 		if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
678b0c632dbSHeiko Carstens 			return -EFAULT;
679b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
680b0c632dbSHeiko Carstens 		prefix = 0;
681b0c632dbSHeiko Carstens 	} else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
682b0c632dbSHeiko Carstens 		if (copy_to_guest(vcpu, 163ul, &archmode, 1))
683b0c632dbSHeiko Carstens 			return -EFAULT;
684b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
685b0c632dbSHeiko Carstens 		prefix = 1;
686b0c632dbSHeiko Carstens 	} else
687b0c632dbSHeiko Carstens 		prefix = 0;
688b0c632dbSHeiko Carstens 
689f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, fp_regs),
690b0c632dbSHeiko Carstens 			vcpu->arch.guest_fpregs.fprs, 128, prefix))
691b0c632dbSHeiko Carstens 		return -EFAULT;
692b0c632dbSHeiko Carstens 
693f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, gp_regs),
6945a32c1afSChristian Borntraeger 			vcpu->run->s.regs.gprs, 128, prefix))
695b0c632dbSHeiko Carstens 		return -EFAULT;
696b0c632dbSHeiko Carstens 
697f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, psw),
698b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gpsw, 16, prefix))
699b0c632dbSHeiko Carstens 		return -EFAULT;
700b0c632dbSHeiko Carstens 
701f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, pref_reg),
702b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->prefix, 4, prefix))
703b0c632dbSHeiko Carstens 		return -EFAULT;
704b0c632dbSHeiko Carstens 
705b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
706f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, fp_ctrl_reg),
707b0c632dbSHeiko Carstens 			&vcpu->arch.guest_fpregs.fpc, 4, prefix))
708b0c632dbSHeiko Carstens 		return -EFAULT;
709b0c632dbSHeiko Carstens 
710f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, tod_reg),
711b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->todpr, 4, prefix))
712b0c632dbSHeiko Carstens 		return -EFAULT;
713b0c632dbSHeiko Carstens 
714f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, timer),
715b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->cputm, 8, prefix))
716b0c632dbSHeiko Carstens 		return -EFAULT;
717b0c632dbSHeiko Carstens 
718f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, clk_cmp),
719b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->ckc, 8, prefix))
720b0c632dbSHeiko Carstens 		return -EFAULT;
721b0c632dbSHeiko Carstens 
722f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, acc_regs),
72359674c1aSChristian Borntraeger 			&vcpu->run->s.regs.acrs, 64, prefix))
724b0c632dbSHeiko Carstens 		return -EFAULT;
725b0c632dbSHeiko Carstens 
726b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
727f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, ctrl_regs),
728b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gcr, 128, prefix))
729b0c632dbSHeiko Carstens 		return -EFAULT;
730b0c632dbSHeiko Carstens 	return 0;
731b0c632dbSHeiko Carstens }
732b0c632dbSHeiko Carstens 
733b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
734b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
735b0c632dbSHeiko Carstens {
736b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
737b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
738bc923cc9SAvi Kivity 	long r;
739b0c632dbSHeiko Carstens 
74093736624SAvi Kivity 	switch (ioctl) {
74193736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
742ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
743ba5c1e9bSCarsten Otte 
74493736624SAvi Kivity 		r = -EFAULT;
745ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
74693736624SAvi Kivity 			break;
74793736624SAvi Kivity 		r = kvm_s390_inject_vcpu(vcpu, &s390int);
74893736624SAvi Kivity 		break;
749ba5c1e9bSCarsten Otte 	}
750b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
751bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
752bc923cc9SAvi Kivity 		break;
753b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
754b0c632dbSHeiko Carstens 		psw_t psw;
755b0c632dbSHeiko Carstens 
756bc923cc9SAvi Kivity 		r = -EFAULT;
757b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
758bc923cc9SAvi Kivity 			break;
759bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
760bc923cc9SAvi Kivity 		break;
761b0c632dbSHeiko Carstens 	}
762b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
763bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
764bc923cc9SAvi Kivity 		break;
76527e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
76627e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
76727e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
76827e0393fSCarsten Otte 
76927e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
77027e0393fSCarsten Otte 			r = -EFAULT;
77127e0393fSCarsten Otte 			break;
77227e0393fSCarsten Otte 		}
77327e0393fSCarsten Otte 
77427e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
77527e0393fSCarsten Otte 			r = -EINVAL;
77627e0393fSCarsten Otte 			break;
77727e0393fSCarsten Otte 		}
77827e0393fSCarsten Otte 
77927e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
78027e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
78127e0393fSCarsten Otte 		break;
78227e0393fSCarsten Otte 	}
78327e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
78427e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
78527e0393fSCarsten Otte 
78627e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
78727e0393fSCarsten Otte 			r = -EFAULT;
78827e0393fSCarsten Otte 			break;
78927e0393fSCarsten Otte 		}
79027e0393fSCarsten Otte 
79127e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
79227e0393fSCarsten Otte 			r = -EINVAL;
79327e0393fSCarsten Otte 			break;
79427e0393fSCarsten Otte 		}
79527e0393fSCarsten Otte 
79627e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
79727e0393fSCarsten Otte 			ucasmap.length);
79827e0393fSCarsten Otte 		break;
79927e0393fSCarsten Otte 	}
80027e0393fSCarsten Otte #endif
801ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
802ccc7910fSCarsten Otte 		r = gmap_fault(arg, vcpu->arch.gmap);
803ccc7910fSCarsten Otte 		if (!IS_ERR_VALUE(r))
804ccc7910fSCarsten Otte 			r = 0;
805ccc7910fSCarsten Otte 		break;
806ccc7910fSCarsten Otte 	}
807b0c632dbSHeiko Carstens 	default:
8083e6afcf1SCarsten Otte 		r = -ENOTTY;
809b0c632dbSHeiko Carstens 	}
810bc923cc9SAvi Kivity 	return r;
811b0c632dbSHeiko Carstens }
812b0c632dbSHeiko Carstens 
8135b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
8145b1c1493SCarsten Otte {
8155b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
8165b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
8175b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
8185b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
8195b1c1493SCarsten Otte 		get_page(vmf->page);
8205b1c1493SCarsten Otte 		return 0;
8215b1c1493SCarsten Otte 	}
8225b1c1493SCarsten Otte #endif
8235b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
8245b1c1493SCarsten Otte }
8255b1c1493SCarsten Otte 
826db3fe4ebSTakuya Yoshikawa void kvm_arch_free_memslot(struct kvm_memory_slot *free,
827db3fe4ebSTakuya Yoshikawa 			   struct kvm_memory_slot *dont)
828db3fe4ebSTakuya Yoshikawa {
829db3fe4ebSTakuya Yoshikawa }
830db3fe4ebSTakuya Yoshikawa 
831db3fe4ebSTakuya Yoshikawa int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
832db3fe4ebSTakuya Yoshikawa {
833db3fe4ebSTakuya Yoshikawa 	return 0;
834db3fe4ebSTakuya Yoshikawa }
835db3fe4ebSTakuya Yoshikawa 
836b0c632dbSHeiko Carstens /* Section: memory related */
837f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
838f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
839b0c632dbSHeiko Carstens 				   struct kvm_memory_slot old,
840f7784b8eSMarcelo Tosatti 				   struct kvm_userspace_memory_region *mem,
841b0c632dbSHeiko Carstens 				   int user_alloc)
842b0c632dbSHeiko Carstens {
843b0c632dbSHeiko Carstens 	/* A few sanity checks. We can have exactly one memory slot which has
844b0c632dbSHeiko Carstens 	   to start at guest virtual zero and which has to be located at a
845b0c632dbSHeiko Carstens 	   page boundary in userland and which has to end at a page boundary.
846b0c632dbSHeiko Carstens 	   The memory in userland is ok to be fragmented into various different
847b0c632dbSHeiko Carstens 	   vmas. It is okay to mmap() and munmap() stuff in this slot after
848b0c632dbSHeiko Carstens 	   doing this call at any time */
849b0c632dbSHeiko Carstens 
850628eb9b8SChristian Ehrhardt 	if (mem->slot)
851b0c632dbSHeiko Carstens 		return -EINVAL;
852b0c632dbSHeiko Carstens 
853b0c632dbSHeiko Carstens 	if (mem->guest_phys_addr)
854b0c632dbSHeiko Carstens 		return -EINVAL;
855b0c632dbSHeiko Carstens 
856598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
857b0c632dbSHeiko Carstens 		return -EINVAL;
858b0c632dbSHeiko Carstens 
859598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
860b0c632dbSHeiko Carstens 		return -EINVAL;
861b0c632dbSHeiko Carstens 
8622668dab7SCarsten Otte 	if (!user_alloc)
8632668dab7SCarsten Otte 		return -EINVAL;
8642668dab7SCarsten Otte 
865f7784b8eSMarcelo Tosatti 	return 0;
866f7784b8eSMarcelo Tosatti }
867f7784b8eSMarcelo Tosatti 
868f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
869f7784b8eSMarcelo Tosatti 				struct kvm_userspace_memory_region *mem,
870f7784b8eSMarcelo Tosatti 				struct kvm_memory_slot old,
871f7784b8eSMarcelo Tosatti 				int user_alloc)
872f7784b8eSMarcelo Tosatti {
873f7850c92SCarsten Otte 	int rc;
874f7784b8eSMarcelo Tosatti 
875598841caSCarsten Otte 
876598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
877598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
878598841caSCarsten Otte 	if (rc)
879f7850c92SCarsten Otte 		printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
880598841caSCarsten Otte 	return;
881b0c632dbSHeiko Carstens }
882b0c632dbSHeiko Carstens 
88334d4cb8fSMarcelo Tosatti void kvm_arch_flush_shadow(struct kvm *kvm)
88434d4cb8fSMarcelo Tosatti {
88534d4cb8fSMarcelo Tosatti }
88634d4cb8fSMarcelo Tosatti 
887b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
888b0c632dbSHeiko Carstens {
889ef50f7acSChristian Borntraeger 	int ret;
8900ee75beaSAvi Kivity 	ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
891ef50f7acSChristian Borntraeger 	if (ret)
892ef50f7acSChristian Borntraeger 		return ret;
893ef50f7acSChristian Borntraeger 
894ef50f7acSChristian Borntraeger 	/*
895ef50f7acSChristian Borntraeger 	 * guests can ask for up to 255+1 double words, we need a full page
89625985edcSLucas De Marchi 	 * to hold the maximum amount of facilities. On the other hand, we
897ef50f7acSChristian Borntraeger 	 * only set facilities that are known to work in KVM.
898ef50f7acSChristian Borntraeger 	 */
899c2f0e8c8SHeiko Carstens 	facilities = (unsigned long long *) get_zeroed_page(GFP_KERNEL|GFP_DMA);
900ef50f7acSChristian Borntraeger 	if (!facilities) {
901ef50f7acSChristian Borntraeger 		kvm_exit();
902ef50f7acSChristian Borntraeger 		return -ENOMEM;
903ef50f7acSChristian Borntraeger 	}
90414375bc4SMartin Schwidefsky 	memcpy(facilities, S390_lowcore.stfle_fac_list, 16);
9056d00d00bSChristian Borntraeger 	facilities[0] &= 0xff00fff3f47c0000ULL;
9069950f8beSChristian Borntraeger 	facilities[1] &= 0x201c000000000000ULL;
907ef50f7acSChristian Borntraeger 	return 0;
908b0c632dbSHeiko Carstens }
909b0c632dbSHeiko Carstens 
910b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
911b0c632dbSHeiko Carstens {
912ef50f7acSChristian Borntraeger 	free_page((unsigned long) facilities);
913b0c632dbSHeiko Carstens 	kvm_exit();
914b0c632dbSHeiko Carstens }
915b0c632dbSHeiko Carstens 
916b0c632dbSHeiko Carstens module_init(kvm_s390_init);
917b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
918