xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision 7eef87dc99e419b1cc051e4417c37e4744d7b661)
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) },
49ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
50ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
51ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
52ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
53ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
54ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
55ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
56453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
57453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
58453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
59453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
60453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
61453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
62453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
63453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
64453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
65bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
665288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
675288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
685288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
695288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
705288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
715288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
72e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
73b0c632dbSHeiko Carstens 	{ NULL }
74b0c632dbSHeiko Carstens };
75b0c632dbSHeiko Carstens 
76ef50f7acSChristian Borntraeger static unsigned long long *facilities;
77b0c632dbSHeiko Carstens 
78b0c632dbSHeiko Carstens /* Section: not file related */
7910474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage)
80b0c632dbSHeiko Carstens {
81b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
8210474ae8SAlexander Graf 	return 0;
83b0c632dbSHeiko Carstens }
84b0c632dbSHeiko Carstens 
85b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage)
86b0c632dbSHeiko Carstens {
87b0c632dbSHeiko Carstens }
88b0c632dbSHeiko Carstens 
89b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
90b0c632dbSHeiko Carstens {
91b0c632dbSHeiko Carstens 	return 0;
92b0c632dbSHeiko Carstens }
93b0c632dbSHeiko Carstens 
94b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
95b0c632dbSHeiko Carstens {
96b0c632dbSHeiko Carstens }
97b0c632dbSHeiko Carstens 
98b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn)
99b0c632dbSHeiko Carstens {
100b0c632dbSHeiko Carstens }
101b0c632dbSHeiko Carstens 
102b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
103b0c632dbSHeiko Carstens {
104b0c632dbSHeiko Carstens 	return 0;
105b0c632dbSHeiko Carstens }
106b0c632dbSHeiko Carstens 
107b0c632dbSHeiko Carstens void kvm_arch_exit(void)
108b0c632dbSHeiko Carstens {
109b0c632dbSHeiko Carstens }
110b0c632dbSHeiko Carstens 
111b0c632dbSHeiko Carstens /* Section: device related */
112b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
113b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
114b0c632dbSHeiko Carstens {
115b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
116b0c632dbSHeiko Carstens 		return s390_enable_sie();
117b0c632dbSHeiko Carstens 	return -EINVAL;
118b0c632dbSHeiko Carstens }
119b0c632dbSHeiko Carstens 
120b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext)
121b0c632dbSHeiko Carstens {
122d7b0b5ebSCarsten Otte 	int r;
123d7b0b5ebSCarsten Otte 
1242bd0ac4eSCarsten Otte 	switch (ext) {
125d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
126b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
127d7b0b5ebSCarsten Otte 		r = 1;
128d7b0b5ebSCarsten Otte 		break;
1292bd0ac4eSCarsten Otte 	default:
130d7b0b5ebSCarsten Otte 		r = 0;
131b0c632dbSHeiko Carstens 	}
132d7b0b5ebSCarsten Otte 	return r;
1332bd0ac4eSCarsten Otte }
134b0c632dbSHeiko Carstens 
135b0c632dbSHeiko Carstens /* Section: vm related */
136b0c632dbSHeiko Carstens /*
137b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
138b0c632dbSHeiko Carstens  */
139b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
140b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
141b0c632dbSHeiko Carstens {
142b0c632dbSHeiko Carstens 	return 0;
143b0c632dbSHeiko Carstens }
144b0c632dbSHeiko Carstens 
145b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
146b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
147b0c632dbSHeiko Carstens {
148b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
149b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
150b0c632dbSHeiko Carstens 	int r;
151b0c632dbSHeiko Carstens 
152b0c632dbSHeiko Carstens 	switch (ioctl) {
153ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
154ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
155ba5c1e9bSCarsten Otte 
156ba5c1e9bSCarsten Otte 		r = -EFAULT;
157ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
158ba5c1e9bSCarsten Otte 			break;
159ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
160ba5c1e9bSCarsten Otte 		break;
161ba5c1e9bSCarsten Otte 	}
162b0c632dbSHeiko Carstens 	default:
163367e1319SAvi Kivity 		r = -ENOTTY;
164b0c632dbSHeiko Carstens 	}
165b0c632dbSHeiko Carstens 
166b0c632dbSHeiko Carstens 	return r;
167b0c632dbSHeiko Carstens }
168b0c632dbSHeiko Carstens 
169d89f5effSJan Kiszka int kvm_arch_init_vm(struct kvm *kvm)
170b0c632dbSHeiko Carstens {
171b0c632dbSHeiko Carstens 	int rc;
172b0c632dbSHeiko Carstens 	char debug_name[16];
173b0c632dbSHeiko Carstens 
174b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
175b0c632dbSHeiko Carstens 	if (rc)
176d89f5effSJan Kiszka 		goto out_err;
177b0c632dbSHeiko Carstens 
178b290411aSCarsten Otte 	rc = -ENOMEM;
179b290411aSCarsten Otte 
180b0c632dbSHeiko Carstens 	kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
181b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
182d89f5effSJan Kiszka 		goto out_err;
183b0c632dbSHeiko Carstens 
184b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
185b0c632dbSHeiko Carstens 
186b0c632dbSHeiko Carstens 	kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
187b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
188b0c632dbSHeiko Carstens 		goto out_nodbf;
189b0c632dbSHeiko Carstens 
190ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
191ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&kvm->arch.float_int.list);
192ba5c1e9bSCarsten Otte 
193b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
194b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "%s", "vm created");
195b0c632dbSHeiko Carstens 
196598841caSCarsten Otte 	kvm->arch.gmap = gmap_alloc(current->mm);
197598841caSCarsten Otte 	if (!kvm->arch.gmap)
198598841caSCarsten Otte 		goto out_nogmap;
199598841caSCarsten Otte 
200d89f5effSJan Kiszka 	return 0;
201598841caSCarsten Otte out_nogmap:
202598841caSCarsten Otte 	debug_unregister(kvm->arch.dbf);
203b0c632dbSHeiko Carstens out_nodbf:
204b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
205d89f5effSJan Kiszka out_err:
206d89f5effSJan Kiszka 	return rc;
207b0c632dbSHeiko Carstens }
208b0c632dbSHeiko Carstens 
209d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
210d329c035SChristian Borntraeger {
211d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
212fc34531dSChristian Borntraeger 	clear_bit(63 - vcpu->vcpu_id, (unsigned long *) &vcpu->kvm->arch.sca->mcn);
213abf4a71eSCarsten Otte 	if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
214abf4a71eSCarsten Otte 		(__u64) vcpu->arch.sie_block)
215abf4a71eSCarsten Otte 		vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
216abf4a71eSCarsten Otte 	smp_mb();
217d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2186692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
219d329c035SChristian Borntraeger 	kfree(vcpu);
220d329c035SChristian Borntraeger }
221d329c035SChristian Borntraeger 
222d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
223d329c035SChristian Borntraeger {
224d329c035SChristian Borntraeger 	unsigned int i;
225988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
226d329c035SChristian Borntraeger 
227988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
228988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
229988a2caeSGleb Natapov 
230988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
231988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
232d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
233988a2caeSGleb Natapov 
234988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
235988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
236d329c035SChristian Borntraeger }
237d329c035SChristian Borntraeger 
238ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm)
239ad8ba2cdSSheng Yang {
240ad8ba2cdSSheng Yang }
241ad8ba2cdSSheng Yang 
242b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
243b0c632dbSHeiko Carstens {
244d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
245b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
246d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
247598841caSCarsten Otte 	gmap_free(kvm->arch.gmap);
248b0c632dbSHeiko Carstens }
249b0c632dbSHeiko Carstens 
250b0c632dbSHeiko Carstens /* Section: vcpu related */
251b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
252b0c632dbSHeiko Carstens {
253598841caSCarsten Otte 	vcpu->arch.gmap = vcpu->kvm->arch.gmap;
254b0c632dbSHeiko Carstens 	return 0;
255b0c632dbSHeiko Carstens }
256b0c632dbSHeiko Carstens 
257b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
258b0c632dbSHeiko Carstens {
2596692cef3SChristian Borntraeger 	/* Nothing todo */
260b0c632dbSHeiko Carstens }
261b0c632dbSHeiko Carstens 
262b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
263b0c632dbSHeiko Carstens {
264b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.host_fpregs);
265b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.host_acrs);
266b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK;
267b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.guest_fpregs);
268b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.guest_acrs);
269480e5926SChristian Borntraeger 	gmap_enable(vcpu->arch.gmap);
270b0c632dbSHeiko Carstens }
271b0c632dbSHeiko Carstens 
272b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
273b0c632dbSHeiko Carstens {
274480e5926SChristian Borntraeger 	gmap_disable(vcpu->arch.gmap);
275b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.guest_fpregs);
276b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.guest_acrs);
277b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.host_fpregs);
278b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
279b0c632dbSHeiko Carstens }
280b0c632dbSHeiko Carstens 
281b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
282b0c632dbSHeiko Carstens {
283b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
284b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
285b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
286b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->prefix    = 0UL;
287b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ihcpu     = 0xffff;
288b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
289b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
290b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
291b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
292b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
293b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
294b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
295b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
296b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
297b0c632dbSHeiko Carstens }
298b0c632dbSHeiko Carstens 
299b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
300b0c632dbSHeiko Carstens {
301598841caSCarsten Otte 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH | CPUSTAT_SM);
302fc34531dSChristian Borntraeger 	vcpu->arch.sie_block->ecb   = 6;
303b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->eca   = 0xC1002001U;
304ef50f7acSChristian Borntraeger 	vcpu->arch.sie_block->fac   = (int) (long) facilities;
305ca872302SChristian Borntraeger 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
306ca872302SChristian Borntraeger 	tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet,
307ba5c1e9bSCarsten Otte 		     (unsigned long) vcpu);
308ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
309453423dcSChristian Borntraeger 	get_cpu_id(&vcpu->arch.cpu_id);
31092e6ecf3SChristian Borntraeger 	vcpu->arch.cpu_id.version = 0xff;
311b0c632dbSHeiko Carstens 	return 0;
312b0c632dbSHeiko Carstens }
313b0c632dbSHeiko Carstens 
314b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
315b0c632dbSHeiko Carstens 				      unsigned int id)
316b0c632dbSHeiko Carstens {
3174d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
3184d47555aSCarsten Otte 	int rc = -EINVAL;
319b0c632dbSHeiko Carstens 
3204d47555aSCarsten Otte 	if (id >= KVM_MAX_VCPUS)
3214d47555aSCarsten Otte 		goto out;
3224d47555aSCarsten Otte 
3234d47555aSCarsten Otte 	rc = -ENOMEM;
3244d47555aSCarsten Otte 
3254d47555aSCarsten Otte 	vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
326b0c632dbSHeiko Carstens 	if (!vcpu)
3274d47555aSCarsten Otte 		goto out;
328b0c632dbSHeiko Carstens 
329180c12fbSChristian Borntraeger 	vcpu->arch.sie_block = (struct kvm_s390_sie_block *)
330180c12fbSChristian Borntraeger 					get_zeroed_page(GFP_KERNEL);
331b0c632dbSHeiko Carstens 
332b0c632dbSHeiko Carstens 	if (!vcpu->arch.sie_block)
333b0c632dbSHeiko Carstens 		goto out_free_cpu;
334b0c632dbSHeiko Carstens 
335b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
336b0c632dbSHeiko Carstens 	BUG_ON(!kvm->arch.sca);
337abf4a71eSCarsten Otte 	if (!kvm->arch.sca->cpu[id].sda)
338b0c632dbSHeiko Carstens 		kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block;
339b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32);
340b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
341fc34531dSChristian Borntraeger 	set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
342b0c632dbSHeiko Carstens 
343ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
344ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
345ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
346b037a4f3SChristian Borntraeger 	spin_lock(&kvm->arch.float_int.lock);
347ba5c1e9bSCarsten Otte 	kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int;
348ba5c1e9bSCarsten Otte 	init_waitqueue_head(&vcpu->arch.local_int.wq);
3495288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
350b037a4f3SChristian Borntraeger 	spin_unlock(&kvm->arch.float_int.lock);
351ba5c1e9bSCarsten Otte 
352b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
353b0c632dbSHeiko Carstens 	if (rc)
3547b06bf2fSWei Yongjun 		goto out_free_sie_block;
355b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
356b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
357b0c632dbSHeiko Carstens 
358b0c632dbSHeiko Carstens 	return vcpu;
3597b06bf2fSWei Yongjun out_free_sie_block:
3607b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
361b0c632dbSHeiko Carstens out_free_cpu:
362b0c632dbSHeiko Carstens 	kfree(vcpu);
3634d47555aSCarsten Otte out:
364b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
365b0c632dbSHeiko Carstens }
366b0c632dbSHeiko Carstens 
367b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
368b0c632dbSHeiko Carstens {
369b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but never calls it */
370b0c632dbSHeiko Carstens 	BUG();
371b0c632dbSHeiko Carstens 	return 0;
372b0c632dbSHeiko Carstens }
373b0c632dbSHeiko Carstens 
374b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
375b0c632dbSHeiko Carstens {
376b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
377b0c632dbSHeiko Carstens 	return 0;
378b0c632dbSHeiko Carstens }
379b0c632dbSHeiko Carstens 
380b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
381b0c632dbSHeiko Carstens {
382b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_gprs, &regs->gprs, sizeof(regs->gprs));
383b0c632dbSHeiko Carstens 	return 0;
384b0c632dbSHeiko Carstens }
385b0c632dbSHeiko Carstens 
386b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
387b0c632dbSHeiko Carstens {
388b0c632dbSHeiko Carstens 	memcpy(&regs->gprs, &vcpu->arch.guest_gprs, sizeof(regs->gprs));
389b0c632dbSHeiko Carstens 	return 0;
390b0c632dbSHeiko Carstens }
391b0c632dbSHeiko Carstens 
392b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
393b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
394b0c632dbSHeiko Carstens {
395b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_acrs, &sregs->acrs, sizeof(sregs->acrs));
396b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
397*7eef87dcSCarsten Otte 	restore_access_regs(vcpu->arch.guest_acrs);
398b0c632dbSHeiko Carstens 	return 0;
399b0c632dbSHeiko Carstens }
400b0c632dbSHeiko Carstens 
401b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
402b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
403b0c632dbSHeiko Carstens {
404b0c632dbSHeiko Carstens 	memcpy(&sregs->acrs, &vcpu->arch.guest_acrs, sizeof(sregs->acrs));
405b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
406b0c632dbSHeiko Carstens 	return 0;
407b0c632dbSHeiko Carstens }
408b0c632dbSHeiko Carstens 
409b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
410b0c632dbSHeiko Carstens {
411b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
412b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = fpu->fpc;
413*7eef87dcSCarsten Otte 	restore_fp_regs(&vcpu->arch.guest_fpregs);
414b0c632dbSHeiko Carstens 	return 0;
415b0c632dbSHeiko Carstens }
416b0c632dbSHeiko Carstens 
417b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
418b0c632dbSHeiko Carstens {
419b0c632dbSHeiko Carstens 	memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
420b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
421b0c632dbSHeiko Carstens 	return 0;
422b0c632dbSHeiko Carstens }
423b0c632dbSHeiko Carstens 
424b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
425b0c632dbSHeiko Carstens {
426b0c632dbSHeiko Carstens 	int rc = 0;
427b0c632dbSHeiko Carstens 
428b0c632dbSHeiko Carstens 	if (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_RUNNING)
429b0c632dbSHeiko Carstens 		rc = -EBUSY;
430d7b0b5ebSCarsten Otte 	else {
431d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
432d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
433d7b0b5ebSCarsten Otte 	}
434b0c632dbSHeiko Carstens 	return rc;
435b0c632dbSHeiko Carstens }
436b0c632dbSHeiko Carstens 
437b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
438b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
439b0c632dbSHeiko Carstens {
440b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
441b0c632dbSHeiko Carstens }
442b0c632dbSHeiko Carstens 
443d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
444d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
445b0c632dbSHeiko Carstens {
446b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
447b0c632dbSHeiko Carstens }
448b0c632dbSHeiko Carstens 
44962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
45062d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
45162d9f0dbSMarcelo Tosatti {
45262d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
45362d9f0dbSMarcelo Tosatti }
45462d9f0dbSMarcelo Tosatti 
45562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
45662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
45762d9f0dbSMarcelo Tosatti {
45862d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
45962d9f0dbSMarcelo Tosatti }
46062d9f0dbSMarcelo Tosatti 
461b0c632dbSHeiko Carstens static void __vcpu_run(struct kvm_vcpu *vcpu)
462b0c632dbSHeiko Carstens {
463b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14], 16);
464b0c632dbSHeiko Carstens 
465b0c632dbSHeiko Carstens 	if (need_resched())
466b0c632dbSHeiko Carstens 		schedule();
467b0c632dbSHeiko Carstens 
46871cde587SChristian Borntraeger 	if (test_thread_flag(TIF_MCCK_PENDING))
46971cde587SChristian Borntraeger 		s390_handle_mcck();
47071cde587SChristian Borntraeger 
4710ff31867SCarsten Otte 	kvm_s390_deliver_pending_interrupts(vcpu);
4720ff31867SCarsten Otte 
473b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
474b0c632dbSHeiko Carstens 	local_irq_disable();
475b0c632dbSHeiko Carstens 	kvm_guest_enter();
476b0c632dbSHeiko Carstens 	local_irq_enable();
477b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
478b0c632dbSHeiko Carstens 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
4791f0d0f09SCarsten Otte 	if (sie64a(vcpu->arch.sie_block, vcpu->arch.guest_gprs)) {
4801f0d0f09SCarsten Otte 		VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4811f0d0f09SCarsten Otte 		kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
4821f0d0f09SCarsten Otte 	}
483b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
484b0c632dbSHeiko Carstens 		   vcpu->arch.sie_block->icptcode);
485b0c632dbSHeiko Carstens 	local_irq_disable();
486b0c632dbSHeiko Carstens 	kvm_guest_exit();
487b0c632dbSHeiko Carstens 	local_irq_enable();
488b0c632dbSHeiko Carstens 
489b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_gprs[14], &vcpu->arch.sie_block->gg14, 16);
490b0c632dbSHeiko Carstens }
491b0c632dbSHeiko Carstens 
492b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
493b0c632dbSHeiko Carstens {
4948f2abe6aSChristian Borntraeger 	int rc;
495b0c632dbSHeiko Carstens 	sigset_t sigsaved;
496b0c632dbSHeiko Carstens 
4979ace903dSChristian Ehrhardt rerun_vcpu:
498b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
499b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
500b0c632dbSHeiko Carstens 
501b0c632dbSHeiko Carstens 	atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
502b0c632dbSHeiko Carstens 
503ba5c1e9bSCarsten Otte 	BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL);
504ba5c1e9bSCarsten Otte 
5058f2abe6aSChristian Borntraeger 	switch (kvm_run->exit_reason) {
5068f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_SIEIC:
5078f2abe6aSChristian Borntraeger 	case KVM_EXIT_UNKNOWN:
5089ace903dSChristian Ehrhardt 	case KVM_EXIT_INTR:
5098f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_RESET:
5108f2abe6aSChristian Borntraeger 		break;
5118f2abe6aSChristian Borntraeger 	default:
5128f2abe6aSChristian Borntraeger 		BUG();
5138f2abe6aSChristian Borntraeger 	}
5148f2abe6aSChristian Borntraeger 
515d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
516d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
517d7b0b5ebSCarsten Otte 
518dab4079dSHeiko Carstens 	might_fault();
5198f2abe6aSChristian Borntraeger 
5208f2abe6aSChristian Borntraeger 	do {
521b0c632dbSHeiko Carstens 		__vcpu_run(vcpu);
5228f2abe6aSChristian Borntraeger 		rc = kvm_handle_sie_intercept(vcpu);
5238f2abe6aSChristian Borntraeger 	} while (!signal_pending(current) && !rc);
5248f2abe6aSChristian Borntraeger 
5259ace903dSChristian Ehrhardt 	if (rc == SIE_INTERCEPT_RERUNVCPU)
5269ace903dSChristian Ehrhardt 		goto rerun_vcpu;
5279ace903dSChristian Ehrhardt 
528b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
529b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
5308f2abe6aSChristian Borntraeger 		rc = -EINTR;
531b1d16c49SChristian Ehrhardt 	}
5328f2abe6aSChristian Borntraeger 
533b8e660b8SHeiko Carstens 	if (rc == -EOPNOTSUPP) {
5348f2abe6aSChristian Borntraeger 		/* intercept cannot be handled in-kernel, prepare kvm-run */
5358f2abe6aSChristian Borntraeger 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
5368f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
5378f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipa      = vcpu->arch.sie_block->ipa;
5388f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipb      = vcpu->arch.sie_block->ipb;
5398f2abe6aSChristian Borntraeger 		rc = 0;
5408f2abe6aSChristian Borntraeger 	}
5418f2abe6aSChristian Borntraeger 
5428f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
5438f2abe6aSChristian Borntraeger 		/* intercept was handled, but userspace support is needed
5448f2abe6aSChristian Borntraeger 		 * kvm_run has been prepared by the handler */
5458f2abe6aSChristian Borntraeger 		rc = 0;
5468f2abe6aSChristian Borntraeger 	}
5478f2abe6aSChristian Borntraeger 
548d7b0b5ebSCarsten Otte 	kvm_run->psw_mask     = vcpu->arch.sie_block->gpsw.mask;
549d7b0b5ebSCarsten Otte 	kvm_run->psw_addr     = vcpu->arch.sie_block->gpsw.addr;
550d7b0b5ebSCarsten Otte 
551b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
552b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
553b0c632dbSHeiko Carstens 
554b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
5557e8e6ab4SHeiko Carstens 	return rc;
556b0c632dbSHeiko Carstens }
557b0c632dbSHeiko Carstens 
558092670cdSCarsten Otte static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, void *from,
559b0c632dbSHeiko Carstens 		       unsigned long n, int prefix)
560b0c632dbSHeiko Carstens {
561b0c632dbSHeiko Carstens 	if (prefix)
562b0c632dbSHeiko Carstens 		return copy_to_guest(vcpu, guestdest, from, n);
563b0c632dbSHeiko Carstens 	else
564b0c632dbSHeiko Carstens 		return copy_to_guest_absolute(vcpu, guestdest, from, n);
565b0c632dbSHeiko Carstens }
566b0c632dbSHeiko Carstens 
567b0c632dbSHeiko Carstens /*
568b0c632dbSHeiko Carstens  * store status at address
569b0c632dbSHeiko Carstens  * we use have two special cases:
570b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
571b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
572b0c632dbSHeiko Carstens  */
573971eb77fSChristian Borntraeger int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
574b0c632dbSHeiko Carstens {
575092670cdSCarsten Otte 	unsigned char archmode = 1;
576b0c632dbSHeiko Carstens 	int prefix;
577b0c632dbSHeiko Carstens 
578b0c632dbSHeiko Carstens 	if (addr == KVM_S390_STORE_STATUS_NOADDR) {
579b0c632dbSHeiko Carstens 		if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
580b0c632dbSHeiko Carstens 			return -EFAULT;
581b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
582b0c632dbSHeiko Carstens 		prefix = 0;
583b0c632dbSHeiko Carstens 	} else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
584b0c632dbSHeiko Carstens 		if (copy_to_guest(vcpu, 163ul, &archmode, 1))
585b0c632dbSHeiko Carstens 			return -EFAULT;
586b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
587b0c632dbSHeiko Carstens 		prefix = 1;
588b0c632dbSHeiko Carstens 	} else
589b0c632dbSHeiko Carstens 		prefix = 0;
590b0c632dbSHeiko Carstens 
591f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, fp_regs),
592b0c632dbSHeiko Carstens 			vcpu->arch.guest_fpregs.fprs, 128, prefix))
593b0c632dbSHeiko Carstens 		return -EFAULT;
594b0c632dbSHeiko Carstens 
595f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, gp_regs),
596b0c632dbSHeiko Carstens 			vcpu->arch.guest_gprs, 128, prefix))
597b0c632dbSHeiko Carstens 		return -EFAULT;
598b0c632dbSHeiko Carstens 
599f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, psw),
600b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gpsw, 16, prefix))
601b0c632dbSHeiko Carstens 		return -EFAULT;
602b0c632dbSHeiko Carstens 
603f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, pref_reg),
604b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->prefix, 4, prefix))
605b0c632dbSHeiko Carstens 		return -EFAULT;
606b0c632dbSHeiko Carstens 
607b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
608f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, fp_ctrl_reg),
609b0c632dbSHeiko Carstens 			&vcpu->arch.guest_fpregs.fpc, 4, prefix))
610b0c632dbSHeiko Carstens 		return -EFAULT;
611b0c632dbSHeiko Carstens 
612f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, tod_reg),
613b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->todpr, 4, prefix))
614b0c632dbSHeiko Carstens 		return -EFAULT;
615b0c632dbSHeiko Carstens 
616f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, timer),
617b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->cputm, 8, prefix))
618b0c632dbSHeiko Carstens 		return -EFAULT;
619b0c632dbSHeiko Carstens 
620f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, clk_cmp),
621b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->ckc, 8, prefix))
622b0c632dbSHeiko Carstens 		return -EFAULT;
623b0c632dbSHeiko Carstens 
624f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, acc_regs),
625b0c632dbSHeiko Carstens 			&vcpu->arch.guest_acrs, 64, prefix))
626b0c632dbSHeiko Carstens 		return -EFAULT;
627b0c632dbSHeiko Carstens 
628b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
629f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, ctrl_regs),
630b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gcr, 128, prefix))
631b0c632dbSHeiko Carstens 		return -EFAULT;
632b0c632dbSHeiko Carstens 	return 0;
633b0c632dbSHeiko Carstens }
634b0c632dbSHeiko Carstens 
635b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
636b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
637b0c632dbSHeiko Carstens {
638b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
639b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
640bc923cc9SAvi Kivity 	long r;
641b0c632dbSHeiko Carstens 
64293736624SAvi Kivity 	switch (ioctl) {
64393736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
644ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
645ba5c1e9bSCarsten Otte 
64693736624SAvi Kivity 		r = -EFAULT;
647ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
64893736624SAvi Kivity 			break;
64993736624SAvi Kivity 		r = kvm_s390_inject_vcpu(vcpu, &s390int);
65093736624SAvi Kivity 		break;
651ba5c1e9bSCarsten Otte 	}
652b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
653bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
654bc923cc9SAvi Kivity 		break;
655b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
656b0c632dbSHeiko Carstens 		psw_t psw;
657b0c632dbSHeiko Carstens 
658bc923cc9SAvi Kivity 		r = -EFAULT;
659b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
660bc923cc9SAvi Kivity 			break;
661bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
662bc923cc9SAvi Kivity 		break;
663b0c632dbSHeiko Carstens 	}
664b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
665bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
666bc923cc9SAvi Kivity 		break;
667b0c632dbSHeiko Carstens 	default:
668bc923cc9SAvi Kivity 		r = -EINVAL;
669b0c632dbSHeiko Carstens 	}
670bc923cc9SAvi Kivity 	return r;
671b0c632dbSHeiko Carstens }
672b0c632dbSHeiko Carstens 
673b0c632dbSHeiko Carstens /* Section: memory related */
674f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
675f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
676b0c632dbSHeiko Carstens 				   struct kvm_memory_slot old,
677f7784b8eSMarcelo Tosatti 				   struct kvm_userspace_memory_region *mem,
678b0c632dbSHeiko Carstens 				   int user_alloc)
679b0c632dbSHeiko Carstens {
680b0c632dbSHeiko Carstens 	/* A few sanity checks. We can have exactly one memory slot which has
681b0c632dbSHeiko Carstens 	   to start at guest virtual zero and which has to be located at a
682b0c632dbSHeiko Carstens 	   page boundary in userland and which has to end at a page boundary.
683b0c632dbSHeiko Carstens 	   The memory in userland is ok to be fragmented into various different
684b0c632dbSHeiko Carstens 	   vmas. It is okay to mmap() and munmap() stuff in this slot after
685b0c632dbSHeiko Carstens 	   doing this call at any time */
686b0c632dbSHeiko Carstens 
687628eb9b8SChristian Ehrhardt 	if (mem->slot)
688b0c632dbSHeiko Carstens 		return -EINVAL;
689b0c632dbSHeiko Carstens 
690b0c632dbSHeiko Carstens 	if (mem->guest_phys_addr)
691b0c632dbSHeiko Carstens 		return -EINVAL;
692b0c632dbSHeiko Carstens 
693598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
694b0c632dbSHeiko Carstens 		return -EINVAL;
695b0c632dbSHeiko Carstens 
696598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
697b0c632dbSHeiko Carstens 		return -EINVAL;
698b0c632dbSHeiko Carstens 
6992668dab7SCarsten Otte 	if (!user_alloc)
7002668dab7SCarsten Otte 		return -EINVAL;
7012668dab7SCarsten Otte 
702f7784b8eSMarcelo Tosatti 	return 0;
703f7784b8eSMarcelo Tosatti }
704f7784b8eSMarcelo Tosatti 
705f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
706f7784b8eSMarcelo Tosatti 				struct kvm_userspace_memory_region *mem,
707f7784b8eSMarcelo Tosatti 				struct kvm_memory_slot old,
708f7784b8eSMarcelo Tosatti 				int user_alloc)
709f7784b8eSMarcelo Tosatti {
710f7850c92SCarsten Otte 	int rc;
711f7784b8eSMarcelo Tosatti 
712598841caSCarsten Otte 
713598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
714598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
715598841caSCarsten Otte 	if (rc)
716f7850c92SCarsten Otte 		printk(KERN_WARNING "kvm-s390: failed to commit memory region\n");
717598841caSCarsten Otte 	return;
718b0c632dbSHeiko Carstens }
719b0c632dbSHeiko Carstens 
72034d4cb8fSMarcelo Tosatti void kvm_arch_flush_shadow(struct kvm *kvm)
72134d4cb8fSMarcelo Tosatti {
72234d4cb8fSMarcelo Tosatti }
72334d4cb8fSMarcelo Tosatti 
724b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
725b0c632dbSHeiko Carstens {
726ef50f7acSChristian Borntraeger 	int ret;
7270ee75beaSAvi Kivity 	ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
728ef50f7acSChristian Borntraeger 	if (ret)
729ef50f7acSChristian Borntraeger 		return ret;
730ef50f7acSChristian Borntraeger 
731ef50f7acSChristian Borntraeger 	/*
732ef50f7acSChristian Borntraeger 	 * guests can ask for up to 255+1 double words, we need a full page
73325985edcSLucas De Marchi 	 * to hold the maximum amount of facilities. On the other hand, we
734ef50f7acSChristian Borntraeger 	 * only set facilities that are known to work in KVM.
735ef50f7acSChristian Borntraeger 	 */
736c2f0e8c8SHeiko Carstens 	facilities = (unsigned long long *) get_zeroed_page(GFP_KERNEL|GFP_DMA);
737ef50f7acSChristian Borntraeger 	if (!facilities) {
738ef50f7acSChristian Borntraeger 		kvm_exit();
739ef50f7acSChristian Borntraeger 		return -ENOMEM;
740ef50f7acSChristian Borntraeger 	}
74114375bc4SMartin Schwidefsky 	memcpy(facilities, S390_lowcore.stfle_fac_list, 16);
7426d00d00bSChristian Borntraeger 	facilities[0] &= 0xff00fff3f47c0000ULL;
7439950f8beSChristian Borntraeger 	facilities[1] &= 0x201c000000000000ULL;
744ef50f7acSChristian Borntraeger 	return 0;
745b0c632dbSHeiko Carstens }
746b0c632dbSHeiko Carstens 
747b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
748b0c632dbSHeiko Carstens {
749ef50f7acSChristian Borntraeger 	free_page((unsigned long) facilities);
750b0c632dbSHeiko Carstens 	kvm_exit();
751b0c632dbSHeiko Carstens }
752b0c632dbSHeiko Carstens 
753b0c632dbSHeiko Carstens module_init(kvm_s390_init);
754b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
755