xref: /linux/arch/s390/kvm/kvm-s390.c (revision 9eed0735ca6a5cf386a4998ad4b6d52d1e29353f)
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>
30ef50f7acSChristian Borntraeger #include <asm/system.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) },
77b0c632dbSHeiko Carstens 	{ NULL }
78b0c632dbSHeiko Carstens };
79b0c632dbSHeiko Carstens 
80ef50f7acSChristian Borntraeger static unsigned long long *facilities;
81b0c632dbSHeiko Carstens 
82b0c632dbSHeiko Carstens /* Section: not file related */
8310474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage)
84b0c632dbSHeiko Carstens {
85b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
8610474ae8SAlexander Graf 	return 0;
87b0c632dbSHeiko Carstens }
88b0c632dbSHeiko Carstens 
89b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage)
90b0c632dbSHeiko Carstens {
91b0c632dbSHeiko Carstens }
92b0c632dbSHeiko Carstens 
93b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
94b0c632dbSHeiko Carstens {
95b0c632dbSHeiko Carstens 	return 0;
96b0c632dbSHeiko Carstens }
97b0c632dbSHeiko Carstens 
98b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
99b0c632dbSHeiko Carstens {
100b0c632dbSHeiko Carstens }
101b0c632dbSHeiko Carstens 
102b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn)
103b0c632dbSHeiko Carstens {
104b0c632dbSHeiko Carstens }
105b0c632dbSHeiko Carstens 
106b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
107b0c632dbSHeiko Carstens {
108b0c632dbSHeiko Carstens 	return 0;
109b0c632dbSHeiko Carstens }
110b0c632dbSHeiko Carstens 
111b0c632dbSHeiko Carstens void kvm_arch_exit(void)
112b0c632dbSHeiko Carstens {
113b0c632dbSHeiko Carstens }
114b0c632dbSHeiko Carstens 
115b0c632dbSHeiko Carstens /* Section: device related */
116b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
117b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
118b0c632dbSHeiko Carstens {
119b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
120b0c632dbSHeiko Carstens 		return s390_enable_sie();
121b0c632dbSHeiko Carstens 	return -EINVAL;
122b0c632dbSHeiko Carstens }
123b0c632dbSHeiko Carstens 
124b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext)
125b0c632dbSHeiko Carstens {
126d7b0b5ebSCarsten Otte 	int r;
127d7b0b5ebSCarsten Otte 
1282bd0ac4eSCarsten Otte 	switch (ext) {
129d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
130b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
13152e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
1321efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1331efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
1341efd0f59SCarsten Otte #endif
13560b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
136d7b0b5ebSCarsten Otte 		r = 1;
137d7b0b5ebSCarsten Otte 		break;
1382bd0ac4eSCarsten Otte 	default:
139d7b0b5ebSCarsten Otte 		r = 0;
140b0c632dbSHeiko Carstens 	}
141d7b0b5ebSCarsten Otte 	return r;
1422bd0ac4eSCarsten Otte }
143b0c632dbSHeiko Carstens 
144b0c632dbSHeiko Carstens /* Section: vm related */
145b0c632dbSHeiko Carstens /*
146b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
147b0c632dbSHeiko Carstens  */
148b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
149b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
150b0c632dbSHeiko Carstens {
151b0c632dbSHeiko Carstens 	return 0;
152b0c632dbSHeiko Carstens }
153b0c632dbSHeiko Carstens 
154b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
155b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
156b0c632dbSHeiko Carstens {
157b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
158b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
159b0c632dbSHeiko Carstens 	int r;
160b0c632dbSHeiko Carstens 
161b0c632dbSHeiko Carstens 	switch (ioctl) {
162ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
163ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
164ba5c1e9bSCarsten Otte 
165ba5c1e9bSCarsten Otte 		r = -EFAULT;
166ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
167ba5c1e9bSCarsten Otte 			break;
168ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
169ba5c1e9bSCarsten Otte 		break;
170ba5c1e9bSCarsten Otte 	}
171b0c632dbSHeiko Carstens 	default:
172367e1319SAvi Kivity 		r = -ENOTTY;
173b0c632dbSHeiko Carstens 	}
174b0c632dbSHeiko Carstens 
175b0c632dbSHeiko Carstens 	return r;
176b0c632dbSHeiko Carstens }
177b0c632dbSHeiko Carstens 
178e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
179b0c632dbSHeiko Carstens {
180b0c632dbSHeiko Carstens 	int rc;
181b0c632dbSHeiko Carstens 	char debug_name[16];
182b0c632dbSHeiko Carstens 
183e08b9637SCarsten Otte 	rc = -EINVAL;
184e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
185e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
186e08b9637SCarsten Otte 		goto out_err;
187e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
188e08b9637SCarsten Otte 		goto out_err;
189e08b9637SCarsten Otte #else
190e08b9637SCarsten Otte 	if (type)
191e08b9637SCarsten Otte 		goto out_err;
192e08b9637SCarsten Otte #endif
193e08b9637SCarsten Otte 
194b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
195b0c632dbSHeiko Carstens 	if (rc)
196d89f5effSJan Kiszka 		goto out_err;
197b0c632dbSHeiko Carstens 
198b290411aSCarsten Otte 	rc = -ENOMEM;
199b290411aSCarsten Otte 
200b0c632dbSHeiko Carstens 	kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
201b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
202d89f5effSJan Kiszka 		goto out_err;
203b0c632dbSHeiko Carstens 
204b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
205b0c632dbSHeiko Carstens 
206b0c632dbSHeiko Carstens 	kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
207b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
208b0c632dbSHeiko Carstens 		goto out_nodbf;
209b0c632dbSHeiko Carstens 
210ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
211ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&kvm->arch.float_int.list);
212ba5c1e9bSCarsten Otte 
213b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
214b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "%s", "vm created");
215b0c632dbSHeiko Carstens 
216e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
217e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
218e08b9637SCarsten Otte 	} else {
219598841caSCarsten Otte 		kvm->arch.gmap = gmap_alloc(current->mm);
220598841caSCarsten Otte 		if (!kvm->arch.gmap)
221598841caSCarsten Otte 			goto out_nogmap;
222e08b9637SCarsten Otte 	}
223d89f5effSJan Kiszka 	return 0;
224598841caSCarsten Otte out_nogmap:
225598841caSCarsten Otte 	debug_unregister(kvm->arch.dbf);
226b0c632dbSHeiko Carstens out_nodbf:
227b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
228d89f5effSJan Kiszka out_err:
229d89f5effSJan Kiszka 	return rc;
230b0c632dbSHeiko Carstens }
231b0c632dbSHeiko Carstens 
232d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
233d329c035SChristian Borntraeger {
234d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
23558f9460bSCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm)) {
23658f9460bSCarsten Otte 		clear_bit(63 - vcpu->vcpu_id,
23758f9460bSCarsten Otte 			  (unsigned long *) &vcpu->kvm->arch.sca->mcn);
238abf4a71eSCarsten Otte 		if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
239abf4a71eSCarsten Otte 		    (__u64) vcpu->arch.sie_block)
240abf4a71eSCarsten Otte 			vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
24158f9460bSCarsten Otte 	}
242abf4a71eSCarsten Otte 	smp_mb();
24327e0393fSCarsten Otte 
24427e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
24527e0393fSCarsten Otte 		gmap_free(vcpu->arch.gmap);
24627e0393fSCarsten Otte 
247d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2486692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
249d329c035SChristian Borntraeger 	kfree(vcpu);
250d329c035SChristian Borntraeger }
251d329c035SChristian Borntraeger 
252d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
253d329c035SChristian Borntraeger {
254d329c035SChristian Borntraeger 	unsigned int i;
255988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
256d329c035SChristian Borntraeger 
257988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
258988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
259988a2caeSGleb Natapov 
260988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
261988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
262d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
263988a2caeSGleb Natapov 
264988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
265988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
266d329c035SChristian Borntraeger }
267d329c035SChristian Borntraeger 
268ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm)
269ad8ba2cdSSheng Yang {
270ad8ba2cdSSheng Yang }
271ad8ba2cdSSheng Yang 
272b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
273b0c632dbSHeiko Carstens {
274d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
275b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
276d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
27727e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
278598841caSCarsten Otte 		gmap_free(kvm->arch.gmap);
279b0c632dbSHeiko Carstens }
280b0c632dbSHeiko Carstens 
281b0c632dbSHeiko Carstens /* Section: vcpu related */
282b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
283b0c632dbSHeiko Carstens {
28427e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm)) {
28527e0393fSCarsten Otte 		vcpu->arch.gmap = gmap_alloc(current->mm);
28627e0393fSCarsten Otte 		if (!vcpu->arch.gmap)
28727e0393fSCarsten Otte 			return -ENOMEM;
28827e0393fSCarsten Otte 		return 0;
28927e0393fSCarsten Otte 	}
29027e0393fSCarsten Otte 
291598841caSCarsten Otte 	vcpu->arch.gmap = vcpu->kvm->arch.gmap;
29259674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
29359674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
294*9eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
295*9eed0735SChristian Borntraeger 				    KVM_SYNC_CRS;
296b0c632dbSHeiko Carstens 	return 0;
297b0c632dbSHeiko Carstens }
298b0c632dbSHeiko Carstens 
299b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
300b0c632dbSHeiko Carstens {
3016692cef3SChristian Borntraeger 	/* Nothing todo */
302b0c632dbSHeiko Carstens }
303b0c632dbSHeiko Carstens 
304b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
305b0c632dbSHeiko Carstens {
306b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.host_fpregs);
307b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.host_acrs);
308b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK;
309b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.guest_fpregs);
31059674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
311480e5926SChristian Borntraeger 	gmap_enable(vcpu->arch.gmap);
3129e6dabefSCornelia Huck 	atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
313b0c632dbSHeiko Carstens }
314b0c632dbSHeiko Carstens 
315b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
316b0c632dbSHeiko Carstens {
3179e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
318480e5926SChristian Borntraeger 	gmap_disable(vcpu->arch.gmap);
319b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.guest_fpregs);
32059674c1aSChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
321b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.host_fpregs);
322b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
323b0c632dbSHeiko Carstens }
324b0c632dbSHeiko Carstens 
325b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
326b0c632dbSHeiko Carstens {
327b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
328b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
329b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
3308d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
331b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
332b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
333b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
334b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
335b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
336b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
337b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
338b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
339b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
340b0c632dbSHeiko Carstens }
341b0c632dbSHeiko Carstens 
342b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
343b0c632dbSHeiko Carstens {
3449e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3459e6dabefSCornelia Huck 						    CPUSTAT_SM |
3469e6dabefSCornelia Huck 						    CPUSTAT_STOPPED);
347fc34531dSChristian Borntraeger 	vcpu->arch.sie_block->ecb   = 6;
348b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->eca   = 0xC1002001U;
349ef50f7acSChristian Borntraeger 	vcpu->arch.sie_block->fac   = (int) (long) facilities;
350ca872302SChristian Borntraeger 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
351ca872302SChristian Borntraeger 	tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet,
352ba5c1e9bSCarsten Otte 		     (unsigned long) vcpu);
353ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
354453423dcSChristian Borntraeger 	get_cpu_id(&vcpu->arch.cpu_id);
35592e6ecf3SChristian Borntraeger 	vcpu->arch.cpu_id.version = 0xff;
356b0c632dbSHeiko Carstens 	return 0;
357b0c632dbSHeiko Carstens }
358b0c632dbSHeiko Carstens 
359b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
360b0c632dbSHeiko Carstens 				      unsigned int id)
361b0c632dbSHeiko Carstens {
3624d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
3634d47555aSCarsten Otte 	int rc = -EINVAL;
364b0c632dbSHeiko Carstens 
3654d47555aSCarsten Otte 	if (id >= KVM_MAX_VCPUS)
3664d47555aSCarsten Otte 		goto out;
3674d47555aSCarsten Otte 
3684d47555aSCarsten Otte 	rc = -ENOMEM;
3694d47555aSCarsten Otte 
3704d47555aSCarsten Otte 	vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
371b0c632dbSHeiko Carstens 	if (!vcpu)
3724d47555aSCarsten Otte 		goto out;
373b0c632dbSHeiko Carstens 
374180c12fbSChristian Borntraeger 	vcpu->arch.sie_block = (struct kvm_s390_sie_block *)
375180c12fbSChristian Borntraeger 					get_zeroed_page(GFP_KERNEL);
376b0c632dbSHeiko Carstens 
377b0c632dbSHeiko Carstens 	if (!vcpu->arch.sie_block)
378b0c632dbSHeiko Carstens 		goto out_free_cpu;
379b0c632dbSHeiko Carstens 
380b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
38158f9460bSCarsten Otte 	if (!kvm_is_ucontrol(kvm)) {
38258f9460bSCarsten Otte 		if (!kvm->arch.sca) {
38358f9460bSCarsten Otte 			WARN_ON_ONCE(1);
38458f9460bSCarsten Otte 			goto out_free_cpu;
38558f9460bSCarsten Otte 		}
386abf4a71eSCarsten Otte 		if (!kvm->arch.sca->cpu[id].sda)
38758f9460bSCarsten Otte 			kvm->arch.sca->cpu[id].sda =
38858f9460bSCarsten Otte 				(__u64) vcpu->arch.sie_block;
38958f9460bSCarsten Otte 		vcpu->arch.sie_block->scaoh =
39058f9460bSCarsten Otte 			(__u32)(((__u64)kvm->arch.sca) >> 32);
391b0c632dbSHeiko Carstens 		vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
392fc34531dSChristian Borntraeger 		set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
39358f9460bSCarsten Otte 	}
394b0c632dbSHeiko Carstens 
395ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
396ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
397ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
398b037a4f3SChristian Borntraeger 	spin_lock(&kvm->arch.float_int.lock);
399ba5c1e9bSCarsten Otte 	kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int;
400ba5c1e9bSCarsten Otte 	init_waitqueue_head(&vcpu->arch.local_int.wq);
4015288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
402b037a4f3SChristian Borntraeger 	spin_unlock(&kvm->arch.float_int.lock);
403ba5c1e9bSCarsten Otte 
404b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
405b0c632dbSHeiko Carstens 	if (rc)
4067b06bf2fSWei Yongjun 		goto out_free_sie_block;
407b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
408b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
409b0c632dbSHeiko Carstens 
410b0c632dbSHeiko Carstens 	return vcpu;
4117b06bf2fSWei Yongjun out_free_sie_block:
4127b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
413b0c632dbSHeiko Carstens out_free_cpu:
414b0c632dbSHeiko Carstens 	kfree(vcpu);
4154d47555aSCarsten Otte out:
416b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
417b0c632dbSHeiko Carstens }
418b0c632dbSHeiko Carstens 
419b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
420b0c632dbSHeiko Carstens {
421b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but never calls it */
422b0c632dbSHeiko Carstens 	BUG();
423b0c632dbSHeiko Carstens 	return 0;
424b0c632dbSHeiko Carstens }
425b0c632dbSHeiko Carstens 
426b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
427b0c632dbSHeiko Carstens {
428b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
429b0c632dbSHeiko Carstens 	return 0;
430b0c632dbSHeiko Carstens }
431b0c632dbSHeiko Carstens 
432b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
433b0c632dbSHeiko Carstens {
4345a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
435b0c632dbSHeiko Carstens 	return 0;
436b0c632dbSHeiko Carstens }
437b0c632dbSHeiko Carstens 
438b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
439b0c632dbSHeiko Carstens {
4405a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
441b0c632dbSHeiko Carstens 	return 0;
442b0c632dbSHeiko Carstens }
443b0c632dbSHeiko Carstens 
444b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
445b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
446b0c632dbSHeiko Carstens {
44759674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
448b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
44959674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
450b0c632dbSHeiko Carstens 	return 0;
451b0c632dbSHeiko Carstens }
452b0c632dbSHeiko Carstens 
453b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
454b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
455b0c632dbSHeiko Carstens {
45659674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
457b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
458b0c632dbSHeiko Carstens 	return 0;
459b0c632dbSHeiko Carstens }
460b0c632dbSHeiko Carstens 
461b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
462b0c632dbSHeiko Carstens {
463b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
46485175587SChristian Borntraeger 	vcpu->arch.guest_fpregs.fpc = fpu->fpc & FPC_VALID_MASK;
4657eef87dcSCarsten Otte 	restore_fp_regs(&vcpu->arch.guest_fpregs);
466b0c632dbSHeiko Carstens 	return 0;
467b0c632dbSHeiko Carstens }
468b0c632dbSHeiko Carstens 
469b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
470b0c632dbSHeiko Carstens {
471b0c632dbSHeiko Carstens 	memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
472b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
473b0c632dbSHeiko Carstens 	return 0;
474b0c632dbSHeiko Carstens }
475b0c632dbSHeiko Carstens 
476b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
477b0c632dbSHeiko Carstens {
478b0c632dbSHeiko Carstens 	int rc = 0;
479b0c632dbSHeiko Carstens 
4809e6dabefSCornelia Huck 	if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_STOPPED))
481b0c632dbSHeiko Carstens 		rc = -EBUSY;
482d7b0b5ebSCarsten Otte 	else {
483d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
484d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
485d7b0b5ebSCarsten Otte 	}
486b0c632dbSHeiko Carstens 	return rc;
487b0c632dbSHeiko Carstens }
488b0c632dbSHeiko Carstens 
489b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
490b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
491b0c632dbSHeiko Carstens {
492b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
493b0c632dbSHeiko Carstens }
494b0c632dbSHeiko Carstens 
495d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
496d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
497b0c632dbSHeiko Carstens {
498b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
499b0c632dbSHeiko Carstens }
500b0c632dbSHeiko Carstens 
50162d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
50262d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
50362d9f0dbSMarcelo Tosatti {
50462d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
50562d9f0dbSMarcelo Tosatti }
50662d9f0dbSMarcelo Tosatti 
50762d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
50862d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
50962d9f0dbSMarcelo Tosatti {
51062d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
51162d9f0dbSMarcelo Tosatti }
51262d9f0dbSMarcelo Tosatti 
513e168bf8dSCarsten Otte static int __vcpu_run(struct kvm_vcpu *vcpu)
514b0c632dbSHeiko Carstens {
515e168bf8dSCarsten Otte 	int rc;
516e168bf8dSCarsten Otte 
5175a32c1afSChristian Borntraeger 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->run->s.regs.gprs[14], 16);
518b0c632dbSHeiko Carstens 
519b0c632dbSHeiko Carstens 	if (need_resched())
520b0c632dbSHeiko Carstens 		schedule();
521b0c632dbSHeiko Carstens 
52271cde587SChristian Borntraeger 	if (test_thread_flag(TIF_MCCK_PENDING))
52371cde587SChristian Borntraeger 		s390_handle_mcck();
52471cde587SChristian Borntraeger 
525d6b6d166SCarsten Otte 	if (!kvm_is_ucontrol(vcpu->kvm))
5260ff31867SCarsten Otte 		kvm_s390_deliver_pending_interrupts(vcpu);
5270ff31867SCarsten Otte 
528b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
529b0c632dbSHeiko Carstens 	local_irq_disable();
530b0c632dbSHeiko Carstens 	kvm_guest_enter();
531b0c632dbSHeiko Carstens 	local_irq_enable();
532b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
533b0c632dbSHeiko Carstens 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
5345a32c1afSChristian Borntraeger 	rc = sie64a(vcpu->arch.sie_block, vcpu->run->s.regs.gprs);
535e168bf8dSCarsten Otte 	if (rc) {
536e168bf8dSCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm)) {
537e168bf8dSCarsten Otte 			rc = SIE_INTERCEPT_UCONTROL;
538e168bf8dSCarsten Otte 		} else {
5391f0d0f09SCarsten Otte 			VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
5401f0d0f09SCarsten Otte 			kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
541e168bf8dSCarsten Otte 			rc = 0;
542e168bf8dSCarsten Otte 		}
5431f0d0f09SCarsten Otte 	}
544b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
545b0c632dbSHeiko Carstens 		   vcpu->arch.sie_block->icptcode);
546b0c632dbSHeiko Carstens 	local_irq_disable();
547b0c632dbSHeiko Carstens 	kvm_guest_exit();
548b0c632dbSHeiko Carstens 	local_irq_enable();
549b0c632dbSHeiko Carstens 
5505a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs[14], &vcpu->arch.sie_block->gg14, 16);
551e168bf8dSCarsten Otte 	return rc;
552b0c632dbSHeiko Carstens }
553b0c632dbSHeiko Carstens 
554b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
555b0c632dbSHeiko Carstens {
5568f2abe6aSChristian Borntraeger 	int rc;
557b0c632dbSHeiko Carstens 	sigset_t sigsaved;
558b0c632dbSHeiko Carstens 
5599ace903dSChristian Ehrhardt rerun_vcpu:
560b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
561b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
562b0c632dbSHeiko Carstens 
5639e6dabefSCornelia Huck 	atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
564b0c632dbSHeiko Carstens 
565ba5c1e9bSCarsten Otte 	BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL);
566ba5c1e9bSCarsten Otte 
5678f2abe6aSChristian Borntraeger 	switch (kvm_run->exit_reason) {
5688f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_SIEIC:
5698f2abe6aSChristian Borntraeger 	case KVM_EXIT_UNKNOWN:
5709ace903dSChristian Ehrhardt 	case KVM_EXIT_INTR:
5718f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_RESET:
572e168bf8dSCarsten Otte 	case KVM_EXIT_S390_UCONTROL:
5738f2abe6aSChristian Borntraeger 		break;
5748f2abe6aSChristian Borntraeger 	default:
5758f2abe6aSChristian Borntraeger 		BUG();
5768f2abe6aSChristian Borntraeger 	}
5778f2abe6aSChristian Borntraeger 
578d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
579d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
58060b413c9SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX) {
58160b413c9SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_PREFIX;
58260b413c9SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
58360b413c9SChristian Borntraeger 	}
584*9eed0735SChristian Borntraeger 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
585*9eed0735SChristian Borntraeger 		kvm_run->kvm_dirty_regs &= ~KVM_SYNC_CRS;
586*9eed0735SChristian Borntraeger 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
587*9eed0735SChristian Borntraeger 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
588*9eed0735SChristian Borntraeger 	}
589d7b0b5ebSCarsten Otte 
590dab4079dSHeiko Carstens 	might_fault();
5918f2abe6aSChristian Borntraeger 
5928f2abe6aSChristian Borntraeger 	do {
593e168bf8dSCarsten Otte 		rc = __vcpu_run(vcpu);
594e168bf8dSCarsten Otte 		if (rc)
595e168bf8dSCarsten Otte 			break;
596c0d744a9SCarsten Otte 		if (kvm_is_ucontrol(vcpu->kvm))
597c0d744a9SCarsten Otte 			rc = -EOPNOTSUPP;
598c0d744a9SCarsten Otte 		else
5998f2abe6aSChristian Borntraeger 			rc = kvm_handle_sie_intercept(vcpu);
6008f2abe6aSChristian Borntraeger 	} while (!signal_pending(current) && !rc);
6018f2abe6aSChristian Borntraeger 
6029ace903dSChristian Ehrhardt 	if (rc == SIE_INTERCEPT_RERUNVCPU)
6039ace903dSChristian Ehrhardt 		goto rerun_vcpu;
6049ace903dSChristian Ehrhardt 
605b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
606b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
6078f2abe6aSChristian Borntraeger 		rc = -EINTR;
608b1d16c49SChristian Ehrhardt 	}
6098f2abe6aSChristian Borntraeger 
610e168bf8dSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
611e168bf8dSCarsten Otte 	if (rc == SIE_INTERCEPT_UCONTROL) {
612e168bf8dSCarsten Otte 		kvm_run->exit_reason = KVM_EXIT_S390_UCONTROL;
613e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.trans_exc_code =
614e168bf8dSCarsten Otte 			current->thread.gmap_addr;
615e168bf8dSCarsten Otte 		kvm_run->s390_ucontrol.pgm_code = 0x10;
616e168bf8dSCarsten Otte 		rc = 0;
617e168bf8dSCarsten Otte 	}
618e168bf8dSCarsten Otte #endif
619e168bf8dSCarsten Otte 
620b8e660b8SHeiko Carstens 	if (rc == -EOPNOTSUPP) {
6218f2abe6aSChristian Borntraeger 		/* intercept cannot be handled in-kernel, prepare kvm-run */
6228f2abe6aSChristian Borntraeger 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
6238f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
6248f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipa      = vcpu->arch.sie_block->ipa;
6258f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipb      = vcpu->arch.sie_block->ipb;
6268f2abe6aSChristian Borntraeger 		rc = 0;
6278f2abe6aSChristian Borntraeger 	}
6288f2abe6aSChristian Borntraeger 
6298f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
6308f2abe6aSChristian Borntraeger 		/* intercept was handled, but userspace support is needed
6318f2abe6aSChristian Borntraeger 		 * kvm_run has been prepared by the handler */
6328f2abe6aSChristian Borntraeger 		rc = 0;
6338f2abe6aSChristian Borntraeger 	}
6348f2abe6aSChristian Borntraeger 
635d7b0b5ebSCarsten Otte 	kvm_run->psw_mask     = vcpu->arch.sie_block->gpsw.mask;
636d7b0b5ebSCarsten Otte 	kvm_run->psw_addr     = vcpu->arch.sie_block->gpsw.addr;
63760b413c9SChristian Borntraeger 	kvm_run->s.regs.prefix = vcpu->arch.sie_block->prefix;
638*9eed0735SChristian Borntraeger 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
639d7b0b5ebSCarsten Otte 
640b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
641b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
642b0c632dbSHeiko Carstens 
643b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
6447e8e6ab4SHeiko Carstens 	return rc;
645b0c632dbSHeiko Carstens }
646b0c632dbSHeiko Carstens 
647092670cdSCarsten Otte static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, void *from,
648b0c632dbSHeiko Carstens 		       unsigned long n, int prefix)
649b0c632dbSHeiko Carstens {
650b0c632dbSHeiko Carstens 	if (prefix)
651b0c632dbSHeiko Carstens 		return copy_to_guest(vcpu, guestdest, from, n);
652b0c632dbSHeiko Carstens 	else
653b0c632dbSHeiko Carstens 		return copy_to_guest_absolute(vcpu, guestdest, from, n);
654b0c632dbSHeiko Carstens }
655b0c632dbSHeiko Carstens 
656b0c632dbSHeiko Carstens /*
657b0c632dbSHeiko Carstens  * store status at address
658b0c632dbSHeiko Carstens  * we use have two special cases:
659b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
660b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
661b0c632dbSHeiko Carstens  */
662971eb77fSChristian Borntraeger int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
663b0c632dbSHeiko Carstens {
664092670cdSCarsten Otte 	unsigned char archmode = 1;
665b0c632dbSHeiko Carstens 	int prefix;
666b0c632dbSHeiko Carstens 
667b0c632dbSHeiko Carstens 	if (addr == KVM_S390_STORE_STATUS_NOADDR) {
668b0c632dbSHeiko Carstens 		if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
669b0c632dbSHeiko Carstens 			return -EFAULT;
670b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
671b0c632dbSHeiko Carstens 		prefix = 0;
672b0c632dbSHeiko Carstens 	} else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
673b0c632dbSHeiko Carstens 		if (copy_to_guest(vcpu, 163ul, &archmode, 1))
674b0c632dbSHeiko Carstens 			return -EFAULT;
675b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
676b0c632dbSHeiko Carstens 		prefix = 1;
677b0c632dbSHeiko Carstens 	} else
678b0c632dbSHeiko Carstens 		prefix = 0;
679b0c632dbSHeiko Carstens 
680f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, fp_regs),
681b0c632dbSHeiko Carstens 			vcpu->arch.guest_fpregs.fprs, 128, prefix))
682b0c632dbSHeiko Carstens 		return -EFAULT;
683b0c632dbSHeiko Carstens 
684f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, gp_regs),
6855a32c1afSChristian Borntraeger 			vcpu->run->s.regs.gprs, 128, prefix))
686b0c632dbSHeiko Carstens 		return -EFAULT;
687b0c632dbSHeiko Carstens 
688f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, psw),
689b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gpsw, 16, prefix))
690b0c632dbSHeiko Carstens 		return -EFAULT;
691b0c632dbSHeiko Carstens 
692f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, pref_reg),
693b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->prefix, 4, prefix))
694b0c632dbSHeiko Carstens 		return -EFAULT;
695b0c632dbSHeiko Carstens 
696b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
697f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, fp_ctrl_reg),
698b0c632dbSHeiko Carstens 			&vcpu->arch.guest_fpregs.fpc, 4, prefix))
699b0c632dbSHeiko Carstens 		return -EFAULT;
700b0c632dbSHeiko Carstens 
701f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, tod_reg),
702b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->todpr, 4, prefix))
703b0c632dbSHeiko Carstens 		return -EFAULT;
704b0c632dbSHeiko Carstens 
705f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, timer),
706b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->cputm, 8, prefix))
707b0c632dbSHeiko Carstens 		return -EFAULT;
708b0c632dbSHeiko Carstens 
709f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, clk_cmp),
710b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->ckc, 8, prefix))
711b0c632dbSHeiko Carstens 		return -EFAULT;
712b0c632dbSHeiko Carstens 
713f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, acc_regs),
71459674c1aSChristian Borntraeger 			&vcpu->run->s.regs.acrs, 64, prefix))
715b0c632dbSHeiko Carstens 		return -EFAULT;
716b0c632dbSHeiko Carstens 
717b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
718f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, ctrl_regs),
719b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gcr, 128, prefix))
720b0c632dbSHeiko Carstens 		return -EFAULT;
721b0c632dbSHeiko Carstens 	return 0;
722b0c632dbSHeiko Carstens }
723b0c632dbSHeiko Carstens 
724b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
725b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
726b0c632dbSHeiko Carstens {
727b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
728b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
729bc923cc9SAvi Kivity 	long r;
730b0c632dbSHeiko Carstens 
73193736624SAvi Kivity 	switch (ioctl) {
73293736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
733ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
734ba5c1e9bSCarsten Otte 
73593736624SAvi Kivity 		r = -EFAULT;
736ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
73793736624SAvi Kivity 			break;
73893736624SAvi Kivity 		r = kvm_s390_inject_vcpu(vcpu, &s390int);
73993736624SAvi Kivity 		break;
740ba5c1e9bSCarsten Otte 	}
741b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
742bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
743bc923cc9SAvi Kivity 		break;
744b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
745b0c632dbSHeiko Carstens 		psw_t psw;
746b0c632dbSHeiko Carstens 
747bc923cc9SAvi Kivity 		r = -EFAULT;
748b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
749bc923cc9SAvi Kivity 			break;
750bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
751bc923cc9SAvi Kivity 		break;
752b0c632dbSHeiko Carstens 	}
753b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
754bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
755bc923cc9SAvi Kivity 		break;
75627e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
75727e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
75827e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
75927e0393fSCarsten Otte 
76027e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
76127e0393fSCarsten Otte 			r = -EFAULT;
76227e0393fSCarsten Otte 			break;
76327e0393fSCarsten Otte 		}
76427e0393fSCarsten Otte 
76527e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
76627e0393fSCarsten Otte 			r = -EINVAL;
76727e0393fSCarsten Otte 			break;
76827e0393fSCarsten Otte 		}
76927e0393fSCarsten Otte 
77027e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
77127e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
77227e0393fSCarsten Otte 		break;
77327e0393fSCarsten Otte 	}
77427e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
77527e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
77627e0393fSCarsten Otte 
77727e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
77827e0393fSCarsten Otte 			r = -EFAULT;
77927e0393fSCarsten Otte 			break;
78027e0393fSCarsten Otte 		}
78127e0393fSCarsten Otte 
78227e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
78327e0393fSCarsten Otte 			r = -EINVAL;
78427e0393fSCarsten Otte 			break;
78527e0393fSCarsten Otte 		}
78627e0393fSCarsten Otte 
78727e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
78827e0393fSCarsten Otte 			ucasmap.length);
78927e0393fSCarsten Otte 		break;
79027e0393fSCarsten Otte 	}
79127e0393fSCarsten Otte #endif
792ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
793ccc7910fSCarsten Otte 		r = gmap_fault(arg, vcpu->arch.gmap);
794ccc7910fSCarsten Otte 		if (!IS_ERR_VALUE(r))
795ccc7910fSCarsten Otte 			r = 0;
796ccc7910fSCarsten Otte 		break;
797ccc7910fSCarsten Otte 	}
798b0c632dbSHeiko Carstens 	default:
7993e6afcf1SCarsten Otte 		r = -ENOTTY;
800b0c632dbSHeiko Carstens 	}
801bc923cc9SAvi Kivity 	return r;
802b0c632dbSHeiko Carstens }
803b0c632dbSHeiko Carstens 
8045b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
8055b1c1493SCarsten Otte {
8065b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
8075b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
8085b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
8095b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
8105b1c1493SCarsten Otte 		get_page(vmf->page);
8115b1c1493SCarsten Otte 		return 0;
8125b1c1493SCarsten Otte 	}
8135b1c1493SCarsten Otte #endif
8145b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
8155b1c1493SCarsten Otte }
8165b1c1493SCarsten Otte 
817b0c632dbSHeiko Carstens /* Section: memory related */
818f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
819f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
820b0c632dbSHeiko Carstens 				   struct kvm_memory_slot old,
821f7784b8eSMarcelo Tosatti 				   struct kvm_userspace_memory_region *mem,
822b0c632dbSHeiko Carstens 				   int user_alloc)
823b0c632dbSHeiko Carstens {
824b0c632dbSHeiko Carstens 	/* A few sanity checks. We can have exactly one memory slot which has
825b0c632dbSHeiko Carstens 	   to start at guest virtual zero and which has to be located at a
826b0c632dbSHeiko Carstens 	   page boundary in userland and which has to end at a page boundary.
827b0c632dbSHeiko Carstens 	   The memory in userland is ok to be fragmented into various different
828b0c632dbSHeiko Carstens 	   vmas. It is okay to mmap() and munmap() stuff in this slot after
829b0c632dbSHeiko Carstens 	   doing this call at any time */
830b0c632dbSHeiko Carstens 
831628eb9b8SChristian Ehrhardt 	if (mem->slot)
832b0c632dbSHeiko Carstens 		return -EINVAL;
833b0c632dbSHeiko Carstens 
834b0c632dbSHeiko Carstens 	if (mem->guest_phys_addr)
835b0c632dbSHeiko Carstens 		return -EINVAL;
836b0c632dbSHeiko Carstens 
837598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
838b0c632dbSHeiko Carstens 		return -EINVAL;
839b0c632dbSHeiko Carstens 
840598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
841b0c632dbSHeiko Carstens 		return -EINVAL;
842b0c632dbSHeiko Carstens 
8432668dab7SCarsten Otte 	if (!user_alloc)
8442668dab7SCarsten Otte 		return -EINVAL;
8452668dab7SCarsten Otte 
846f7784b8eSMarcelo Tosatti 	return 0;
847f7784b8eSMarcelo Tosatti }
848f7784b8eSMarcelo Tosatti 
849f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
850f7784b8eSMarcelo Tosatti 				struct kvm_userspace_memory_region *mem,
851f7784b8eSMarcelo Tosatti 				struct kvm_memory_slot old,
852f7784b8eSMarcelo Tosatti 				int user_alloc)
853f7784b8eSMarcelo Tosatti {
854f7850c92SCarsten Otte 	int rc;
855f7784b8eSMarcelo Tosatti 
856598841caSCarsten Otte 
857598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
858598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
859598841caSCarsten Otte 	if (rc)
860f7850c92SCarsten Otte 		printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
861598841caSCarsten Otte 	return;
862b0c632dbSHeiko Carstens }
863b0c632dbSHeiko Carstens 
86434d4cb8fSMarcelo Tosatti void kvm_arch_flush_shadow(struct kvm *kvm)
86534d4cb8fSMarcelo Tosatti {
86634d4cb8fSMarcelo Tosatti }
86734d4cb8fSMarcelo Tosatti 
868b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
869b0c632dbSHeiko Carstens {
870ef50f7acSChristian Borntraeger 	int ret;
8710ee75beaSAvi Kivity 	ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
872ef50f7acSChristian Borntraeger 	if (ret)
873ef50f7acSChristian Borntraeger 		return ret;
874ef50f7acSChristian Borntraeger 
875ef50f7acSChristian Borntraeger 	/*
876ef50f7acSChristian Borntraeger 	 * guests can ask for up to 255+1 double words, we need a full page
87725985edcSLucas De Marchi 	 * to hold the maximum amount of facilities. On the other hand, we
878ef50f7acSChristian Borntraeger 	 * only set facilities that are known to work in KVM.
879ef50f7acSChristian Borntraeger 	 */
880c2f0e8c8SHeiko Carstens 	facilities = (unsigned long long *) get_zeroed_page(GFP_KERNEL|GFP_DMA);
881ef50f7acSChristian Borntraeger 	if (!facilities) {
882ef50f7acSChristian Borntraeger 		kvm_exit();
883ef50f7acSChristian Borntraeger 		return -ENOMEM;
884ef50f7acSChristian Borntraeger 	}
88514375bc4SMartin Schwidefsky 	memcpy(facilities, S390_lowcore.stfle_fac_list, 16);
8866d00d00bSChristian Borntraeger 	facilities[0] &= 0xff00fff3f47c0000ULL;
8879950f8beSChristian Borntraeger 	facilities[1] &= 0x201c000000000000ULL;
888ef50f7acSChristian Borntraeger 	return 0;
889b0c632dbSHeiko Carstens }
890b0c632dbSHeiko Carstens 
891b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
892b0c632dbSHeiko Carstens {
893ef50f7acSChristian Borntraeger 	free_page((unsigned long) facilities);
894b0c632dbSHeiko Carstens 	kvm_exit();
895b0c632dbSHeiko Carstens }
896b0c632dbSHeiko Carstens 
897b0c632dbSHeiko Carstens module_init(kvm_s390_init);
898b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
899