xref: /linux/arch/s390/kvm/kvm-s390.c (revision 25985edcedea6396277003854657b5f3cb31a628)
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) },
655288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
665288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
675288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
685288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
695288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
705288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
71e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
72b0c632dbSHeiko Carstens 	{ NULL }
73b0c632dbSHeiko Carstens };
74b0c632dbSHeiko Carstens 
75ef50f7acSChristian Borntraeger static unsigned long long *facilities;
76b0c632dbSHeiko Carstens 
77b0c632dbSHeiko Carstens /* Section: not file related */
7810474ae8SAlexander Graf int kvm_arch_hardware_enable(void *garbage)
79b0c632dbSHeiko Carstens {
80b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
8110474ae8SAlexander Graf 	return 0;
82b0c632dbSHeiko Carstens }
83b0c632dbSHeiko Carstens 
84b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage)
85b0c632dbSHeiko Carstens {
86b0c632dbSHeiko Carstens }
87b0c632dbSHeiko Carstens 
88b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
89b0c632dbSHeiko Carstens {
90b0c632dbSHeiko Carstens 	return 0;
91b0c632dbSHeiko Carstens }
92b0c632dbSHeiko Carstens 
93b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
94b0c632dbSHeiko Carstens {
95b0c632dbSHeiko Carstens }
96b0c632dbSHeiko Carstens 
97b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn)
98b0c632dbSHeiko Carstens {
99b0c632dbSHeiko Carstens }
100b0c632dbSHeiko Carstens 
101b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
102b0c632dbSHeiko Carstens {
103b0c632dbSHeiko Carstens 	return 0;
104b0c632dbSHeiko Carstens }
105b0c632dbSHeiko Carstens 
106b0c632dbSHeiko Carstens void kvm_arch_exit(void)
107b0c632dbSHeiko Carstens {
108b0c632dbSHeiko Carstens }
109b0c632dbSHeiko Carstens 
110b0c632dbSHeiko Carstens /* Section: device related */
111b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
112b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
113b0c632dbSHeiko Carstens {
114b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
115b0c632dbSHeiko Carstens 		return s390_enable_sie();
116b0c632dbSHeiko Carstens 	return -EINVAL;
117b0c632dbSHeiko Carstens }
118b0c632dbSHeiko Carstens 
119b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext)
120b0c632dbSHeiko Carstens {
121d7b0b5ebSCarsten Otte 	int r;
122d7b0b5ebSCarsten Otte 
1232bd0ac4eSCarsten Otte 	switch (ext) {
124d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
125d7b0b5ebSCarsten Otte 		r = 1;
126d7b0b5ebSCarsten Otte 		break;
1272bd0ac4eSCarsten Otte 	default:
128d7b0b5ebSCarsten Otte 		r = 0;
129b0c632dbSHeiko Carstens 	}
130d7b0b5ebSCarsten Otte 	return r;
1312bd0ac4eSCarsten Otte }
132b0c632dbSHeiko Carstens 
133b0c632dbSHeiko Carstens /* Section: vm related */
134b0c632dbSHeiko Carstens /*
135b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
136b0c632dbSHeiko Carstens  */
137b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
138b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
139b0c632dbSHeiko Carstens {
140b0c632dbSHeiko Carstens 	return 0;
141b0c632dbSHeiko Carstens }
142b0c632dbSHeiko Carstens 
143b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
144b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
145b0c632dbSHeiko Carstens {
146b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
147b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
148b0c632dbSHeiko Carstens 	int r;
149b0c632dbSHeiko Carstens 
150b0c632dbSHeiko Carstens 	switch (ioctl) {
151ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
152ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
153ba5c1e9bSCarsten Otte 
154ba5c1e9bSCarsten Otte 		r = -EFAULT;
155ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
156ba5c1e9bSCarsten Otte 			break;
157ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
158ba5c1e9bSCarsten Otte 		break;
159ba5c1e9bSCarsten Otte 	}
160b0c632dbSHeiko Carstens 	default:
161367e1319SAvi Kivity 		r = -ENOTTY;
162b0c632dbSHeiko Carstens 	}
163b0c632dbSHeiko Carstens 
164b0c632dbSHeiko Carstens 	return r;
165b0c632dbSHeiko Carstens }
166b0c632dbSHeiko Carstens 
167d89f5effSJan Kiszka int kvm_arch_init_vm(struct kvm *kvm)
168b0c632dbSHeiko Carstens {
169b0c632dbSHeiko Carstens 	int rc;
170b0c632dbSHeiko Carstens 	char debug_name[16];
171b0c632dbSHeiko Carstens 
172b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
173b0c632dbSHeiko Carstens 	if (rc)
174d89f5effSJan Kiszka 		goto out_err;
175b0c632dbSHeiko Carstens 
176b0c632dbSHeiko Carstens 	kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
177b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
178d89f5effSJan Kiszka 		goto out_err;
179b0c632dbSHeiko Carstens 
180b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
181b0c632dbSHeiko Carstens 
182b0c632dbSHeiko Carstens 	kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
183b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
184b0c632dbSHeiko Carstens 		goto out_nodbf;
185b0c632dbSHeiko Carstens 
186ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
187ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&kvm->arch.float_int.list);
188ba5c1e9bSCarsten Otte 
189b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
190b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "%s", "vm created");
191b0c632dbSHeiko Carstens 
192d89f5effSJan Kiszka 	return 0;
193b0c632dbSHeiko Carstens out_nodbf:
194b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
195d89f5effSJan Kiszka out_err:
196d89f5effSJan Kiszka 	return rc;
197b0c632dbSHeiko Carstens }
198b0c632dbSHeiko Carstens 
199d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
200d329c035SChristian Borntraeger {
201d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
202fc34531dSChristian Borntraeger 	clear_bit(63 - vcpu->vcpu_id, (unsigned long *) &vcpu->kvm->arch.sca->mcn);
203abf4a71eSCarsten Otte 	if (vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda ==
204abf4a71eSCarsten Otte 		(__u64) vcpu->arch.sie_block)
205abf4a71eSCarsten Otte 		vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sda = 0;
206abf4a71eSCarsten Otte 	smp_mb();
207d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2086692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
209d329c035SChristian Borntraeger 	kfree(vcpu);
210d329c035SChristian Borntraeger }
211d329c035SChristian Borntraeger 
212d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
213d329c035SChristian Borntraeger {
214d329c035SChristian Borntraeger 	unsigned int i;
215988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
216d329c035SChristian Borntraeger 
217988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
218988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
219988a2caeSGleb Natapov 
220988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
221988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
222d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
223988a2caeSGleb Natapov 
224988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
225988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
226d329c035SChristian Borntraeger }
227d329c035SChristian Borntraeger 
228ad8ba2cdSSheng Yang void kvm_arch_sync_events(struct kvm *kvm)
229ad8ba2cdSSheng Yang {
230ad8ba2cdSSheng Yang }
231ad8ba2cdSSheng Yang 
232b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
233b0c632dbSHeiko Carstens {
234d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
235b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
236d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
237b0c632dbSHeiko Carstens }
238b0c632dbSHeiko Carstens 
239b0c632dbSHeiko Carstens /* Section: vcpu related */
240b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
241b0c632dbSHeiko Carstens {
242b0c632dbSHeiko Carstens 	return 0;
243b0c632dbSHeiko Carstens }
244b0c632dbSHeiko Carstens 
245b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
246b0c632dbSHeiko Carstens {
2476692cef3SChristian Borntraeger 	/* Nothing todo */
248b0c632dbSHeiko Carstens }
249b0c632dbSHeiko Carstens 
250b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
251b0c632dbSHeiko Carstens {
252b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.host_fpregs);
253b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.host_acrs);
254b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK;
255b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.guest_fpregs);
256b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.guest_acrs);
257b0c632dbSHeiko Carstens }
258b0c632dbSHeiko Carstens 
259b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
260b0c632dbSHeiko Carstens {
261b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.guest_fpregs);
262b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.guest_acrs);
263b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.host_fpregs);
264b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
265b0c632dbSHeiko Carstens }
266b0c632dbSHeiko Carstens 
267b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
268b0c632dbSHeiko Carstens {
269b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
270b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
271b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
272b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->prefix    = 0UL;
273b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ihcpu     = 0xffff;
274b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
275b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
276b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
277b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
278b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
279b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
280b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
281b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
282b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
283b0c632dbSHeiko Carstens }
284b0c632dbSHeiko Carstens 
285b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
286b0c632dbSHeiko Carstens {
287b0c632dbSHeiko Carstens 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH);
288628eb9b8SChristian Ehrhardt 	set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests);
289fc34531dSChristian Borntraeger 	vcpu->arch.sie_block->ecb   = 6;
290b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->eca   = 0xC1002001U;
291ef50f7acSChristian Borntraeger 	vcpu->arch.sie_block->fac   = (int) (long) facilities;
292ca872302SChristian Borntraeger 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
293ca872302SChristian Borntraeger 	tasklet_init(&vcpu->arch.tasklet, kvm_s390_tasklet,
294ba5c1e9bSCarsten Otte 		     (unsigned long) vcpu);
295ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
296453423dcSChristian Borntraeger 	get_cpu_id(&vcpu->arch.cpu_id);
29792e6ecf3SChristian Borntraeger 	vcpu->arch.cpu_id.version = 0xff;
298b0c632dbSHeiko Carstens 	return 0;
299b0c632dbSHeiko Carstens }
300b0c632dbSHeiko Carstens 
301b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
302b0c632dbSHeiko Carstens 				      unsigned int id)
303b0c632dbSHeiko Carstens {
304b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
305b0c632dbSHeiko Carstens 	int rc = -ENOMEM;
306b0c632dbSHeiko Carstens 
307b0c632dbSHeiko Carstens 	if (!vcpu)
308b0c632dbSHeiko Carstens 		goto out_nomem;
309b0c632dbSHeiko Carstens 
310180c12fbSChristian Borntraeger 	vcpu->arch.sie_block = (struct kvm_s390_sie_block *)
311180c12fbSChristian Borntraeger 					get_zeroed_page(GFP_KERNEL);
312b0c632dbSHeiko Carstens 
313b0c632dbSHeiko Carstens 	if (!vcpu->arch.sie_block)
314b0c632dbSHeiko Carstens 		goto out_free_cpu;
315b0c632dbSHeiko Carstens 
316b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
317b0c632dbSHeiko Carstens 	BUG_ON(!kvm->arch.sca);
318abf4a71eSCarsten Otte 	if (!kvm->arch.sca->cpu[id].sda)
319b0c632dbSHeiko Carstens 		kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block;
320b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32);
321b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
322fc34531dSChristian Borntraeger 	set_bit(63 - id, (unsigned long *) &kvm->arch.sca->mcn);
323b0c632dbSHeiko Carstens 
324ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
325ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
326ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
327b037a4f3SChristian Borntraeger 	spin_lock(&kvm->arch.float_int.lock);
328ba5c1e9bSCarsten Otte 	kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int;
329ba5c1e9bSCarsten Otte 	init_waitqueue_head(&vcpu->arch.local_int.wq);
3305288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
331b037a4f3SChristian Borntraeger 	spin_unlock(&kvm->arch.float_int.lock);
332ba5c1e9bSCarsten Otte 
333b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
334b0c632dbSHeiko Carstens 	if (rc)
3357b06bf2fSWei Yongjun 		goto out_free_sie_block;
336b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
337b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
338b0c632dbSHeiko Carstens 
339b0c632dbSHeiko Carstens 	return vcpu;
3407b06bf2fSWei Yongjun out_free_sie_block:
3417b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
342b0c632dbSHeiko Carstens out_free_cpu:
343b0c632dbSHeiko Carstens 	kfree(vcpu);
344b0c632dbSHeiko Carstens out_nomem:
345b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
346b0c632dbSHeiko Carstens }
347b0c632dbSHeiko Carstens 
348b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
349b0c632dbSHeiko Carstens {
350b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but never calls it */
351b0c632dbSHeiko Carstens 	BUG();
352b0c632dbSHeiko Carstens 	return 0;
353b0c632dbSHeiko Carstens }
354b0c632dbSHeiko Carstens 
355b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
356b0c632dbSHeiko Carstens {
357b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
358b0c632dbSHeiko Carstens 	return 0;
359b0c632dbSHeiko Carstens }
360b0c632dbSHeiko Carstens 
361b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
362b0c632dbSHeiko Carstens {
363b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_gprs, &regs->gprs, sizeof(regs->gprs));
364b0c632dbSHeiko Carstens 	return 0;
365b0c632dbSHeiko Carstens }
366b0c632dbSHeiko Carstens 
367b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
368b0c632dbSHeiko Carstens {
369b0c632dbSHeiko Carstens 	memcpy(&regs->gprs, &vcpu->arch.guest_gprs, sizeof(regs->gprs));
370b0c632dbSHeiko Carstens 	return 0;
371b0c632dbSHeiko Carstens }
372b0c632dbSHeiko Carstens 
373b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
374b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
375b0c632dbSHeiko Carstens {
376b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_acrs, &sregs->acrs, sizeof(sregs->acrs));
377b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
378b0c632dbSHeiko Carstens 	return 0;
379b0c632dbSHeiko Carstens }
380b0c632dbSHeiko Carstens 
381b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
382b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
383b0c632dbSHeiko Carstens {
384b0c632dbSHeiko Carstens 	memcpy(&sregs->acrs, &vcpu->arch.guest_acrs, sizeof(sregs->acrs));
385b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
386b0c632dbSHeiko Carstens 	return 0;
387b0c632dbSHeiko Carstens }
388b0c632dbSHeiko Carstens 
389b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
390b0c632dbSHeiko Carstens {
391b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
392b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = fpu->fpc;
393b0c632dbSHeiko Carstens 	return 0;
394b0c632dbSHeiko Carstens }
395b0c632dbSHeiko Carstens 
396b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
397b0c632dbSHeiko Carstens {
398b0c632dbSHeiko Carstens 	memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
399b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
400b0c632dbSHeiko Carstens 	return 0;
401b0c632dbSHeiko Carstens }
402b0c632dbSHeiko Carstens 
403b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
404b0c632dbSHeiko Carstens {
405b0c632dbSHeiko Carstens 	int rc = 0;
406b0c632dbSHeiko Carstens 
407b0c632dbSHeiko Carstens 	if (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_RUNNING)
408b0c632dbSHeiko Carstens 		rc = -EBUSY;
409d7b0b5ebSCarsten Otte 	else {
410d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
411d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
412d7b0b5ebSCarsten Otte 	}
413b0c632dbSHeiko Carstens 	return rc;
414b0c632dbSHeiko Carstens }
415b0c632dbSHeiko Carstens 
416b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
417b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
418b0c632dbSHeiko Carstens {
419b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
420b0c632dbSHeiko Carstens }
421b0c632dbSHeiko Carstens 
422d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
423d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
424b0c632dbSHeiko Carstens {
425b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
426b0c632dbSHeiko Carstens }
427b0c632dbSHeiko Carstens 
42862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
42962d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
43062d9f0dbSMarcelo Tosatti {
43162d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
43262d9f0dbSMarcelo Tosatti }
43362d9f0dbSMarcelo Tosatti 
43462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
43562d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
43662d9f0dbSMarcelo Tosatti {
43762d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
43862d9f0dbSMarcelo Tosatti }
43962d9f0dbSMarcelo Tosatti 
440b0c632dbSHeiko Carstens static void __vcpu_run(struct kvm_vcpu *vcpu)
441b0c632dbSHeiko Carstens {
442b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14], 16);
443b0c632dbSHeiko Carstens 
444b0c632dbSHeiko Carstens 	if (need_resched())
445b0c632dbSHeiko Carstens 		schedule();
446b0c632dbSHeiko Carstens 
44771cde587SChristian Borntraeger 	if (test_thread_flag(TIF_MCCK_PENDING))
44871cde587SChristian Borntraeger 		s390_handle_mcck();
44971cde587SChristian Borntraeger 
4500ff31867SCarsten Otte 	kvm_s390_deliver_pending_interrupts(vcpu);
4510ff31867SCarsten Otte 
452b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
453b0c632dbSHeiko Carstens 	local_irq_disable();
454b0c632dbSHeiko Carstens 	kvm_guest_enter();
455b0c632dbSHeiko Carstens 	local_irq_enable();
456b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
457b0c632dbSHeiko Carstens 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
4581f0d0f09SCarsten Otte 	if (sie64a(vcpu->arch.sie_block, vcpu->arch.guest_gprs)) {
4591f0d0f09SCarsten Otte 		VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4601f0d0f09SCarsten Otte 		kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
4611f0d0f09SCarsten Otte 	}
462b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
463b0c632dbSHeiko Carstens 		   vcpu->arch.sie_block->icptcode);
464b0c632dbSHeiko Carstens 	local_irq_disable();
465b0c632dbSHeiko Carstens 	kvm_guest_exit();
466b0c632dbSHeiko Carstens 	local_irq_enable();
467b0c632dbSHeiko Carstens 
468b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_gprs[14], &vcpu->arch.sie_block->gg14, 16);
469b0c632dbSHeiko Carstens }
470b0c632dbSHeiko Carstens 
471b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
472b0c632dbSHeiko Carstens {
4738f2abe6aSChristian Borntraeger 	int rc;
474b0c632dbSHeiko Carstens 	sigset_t sigsaved;
475b0c632dbSHeiko Carstens 
4769ace903dSChristian Ehrhardt rerun_vcpu:
477628eb9b8SChristian Ehrhardt 	if (vcpu->requests)
478628eb9b8SChristian Ehrhardt 		if (test_and_clear_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
479628eb9b8SChristian Ehrhardt 			kvm_s390_vcpu_set_mem(vcpu);
480628eb9b8SChristian Ehrhardt 
48151e4d5abSCarsten Otte 	/* verify, that memory has been registered */
482628eb9b8SChristian Ehrhardt 	if (!vcpu->arch.sie_block->gmslm) {
48351e4d5abSCarsten Otte 		vcpu_put(vcpu);
484628eb9b8SChristian Ehrhardt 		VCPU_EVENT(vcpu, 3, "%s", "no memory registered to run vcpu");
48551e4d5abSCarsten Otte 		return -EINVAL;
48651e4d5abSCarsten Otte 	}
48751e4d5abSCarsten Otte 
488b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
489b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
490b0c632dbSHeiko Carstens 
491b0c632dbSHeiko Carstens 	atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
492b0c632dbSHeiko Carstens 
493ba5c1e9bSCarsten Otte 	BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL);
494ba5c1e9bSCarsten Otte 
4958f2abe6aSChristian Borntraeger 	switch (kvm_run->exit_reason) {
4968f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_SIEIC:
4978f2abe6aSChristian Borntraeger 	case KVM_EXIT_UNKNOWN:
4989ace903dSChristian Ehrhardt 	case KVM_EXIT_INTR:
4998f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_RESET:
5008f2abe6aSChristian Borntraeger 		break;
5018f2abe6aSChristian Borntraeger 	default:
5028f2abe6aSChristian Borntraeger 		BUG();
5038f2abe6aSChristian Borntraeger 	}
5048f2abe6aSChristian Borntraeger 
505d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
506d7b0b5ebSCarsten Otte 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
507d7b0b5ebSCarsten Otte 
508dab4079dSHeiko Carstens 	might_fault();
5098f2abe6aSChristian Borntraeger 
5108f2abe6aSChristian Borntraeger 	do {
511b0c632dbSHeiko Carstens 		__vcpu_run(vcpu);
5128f2abe6aSChristian Borntraeger 		rc = kvm_handle_sie_intercept(vcpu);
5138f2abe6aSChristian Borntraeger 	} while (!signal_pending(current) && !rc);
5148f2abe6aSChristian Borntraeger 
5159ace903dSChristian Ehrhardt 	if (rc == SIE_INTERCEPT_RERUNVCPU)
5169ace903dSChristian Ehrhardt 		goto rerun_vcpu;
5179ace903dSChristian Ehrhardt 
518b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
519b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
5208f2abe6aSChristian Borntraeger 		rc = -EINTR;
521b1d16c49SChristian Ehrhardt 	}
5228f2abe6aSChristian Borntraeger 
523b8e660b8SHeiko Carstens 	if (rc == -EOPNOTSUPP) {
5248f2abe6aSChristian Borntraeger 		/* intercept cannot be handled in-kernel, prepare kvm-run */
5258f2abe6aSChristian Borntraeger 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
5268f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
5278f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipa      = vcpu->arch.sie_block->ipa;
5288f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipb      = vcpu->arch.sie_block->ipb;
5298f2abe6aSChristian Borntraeger 		rc = 0;
5308f2abe6aSChristian Borntraeger 	}
5318f2abe6aSChristian Borntraeger 
5328f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
5338f2abe6aSChristian Borntraeger 		/* intercept was handled, but userspace support is needed
5348f2abe6aSChristian Borntraeger 		 * kvm_run has been prepared by the handler */
5358f2abe6aSChristian Borntraeger 		rc = 0;
5368f2abe6aSChristian Borntraeger 	}
5378f2abe6aSChristian Borntraeger 
538d7b0b5ebSCarsten Otte 	kvm_run->psw_mask     = vcpu->arch.sie_block->gpsw.mask;
539d7b0b5ebSCarsten Otte 	kvm_run->psw_addr     = vcpu->arch.sie_block->gpsw.addr;
540d7b0b5ebSCarsten Otte 
541b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
542b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
543b0c632dbSHeiko Carstens 
544b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
5457e8e6ab4SHeiko Carstens 	return rc;
546b0c632dbSHeiko Carstens }
547b0c632dbSHeiko Carstens 
548b0c632dbSHeiko Carstens static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, const void *from,
549b0c632dbSHeiko Carstens 		       unsigned long n, int prefix)
550b0c632dbSHeiko Carstens {
551b0c632dbSHeiko Carstens 	if (prefix)
552b0c632dbSHeiko Carstens 		return copy_to_guest(vcpu, guestdest, from, n);
553b0c632dbSHeiko Carstens 	else
554b0c632dbSHeiko Carstens 		return copy_to_guest_absolute(vcpu, guestdest, from, n);
555b0c632dbSHeiko Carstens }
556b0c632dbSHeiko Carstens 
557b0c632dbSHeiko Carstens /*
558b0c632dbSHeiko Carstens  * store status at address
559b0c632dbSHeiko Carstens  * we use have two special cases:
560b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
561b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
562b0c632dbSHeiko Carstens  */
563971eb77fSChristian Borntraeger int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
564b0c632dbSHeiko Carstens {
565b0c632dbSHeiko Carstens 	const unsigned char archmode = 1;
566b0c632dbSHeiko Carstens 	int prefix;
567b0c632dbSHeiko Carstens 
568b0c632dbSHeiko Carstens 	if (addr == KVM_S390_STORE_STATUS_NOADDR) {
569b0c632dbSHeiko Carstens 		if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
570b0c632dbSHeiko Carstens 			return -EFAULT;
571b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
572b0c632dbSHeiko Carstens 		prefix = 0;
573b0c632dbSHeiko Carstens 	} else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
574b0c632dbSHeiko Carstens 		if (copy_to_guest(vcpu, 163ul, &archmode, 1))
575b0c632dbSHeiko Carstens 			return -EFAULT;
576b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
577b0c632dbSHeiko Carstens 		prefix = 1;
578b0c632dbSHeiko Carstens 	} else
579b0c632dbSHeiko Carstens 		prefix = 0;
580b0c632dbSHeiko Carstens 
581f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, fp_regs),
582b0c632dbSHeiko Carstens 			vcpu->arch.guest_fpregs.fprs, 128, prefix))
583b0c632dbSHeiko Carstens 		return -EFAULT;
584b0c632dbSHeiko Carstens 
585f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, gp_regs),
586b0c632dbSHeiko Carstens 			vcpu->arch.guest_gprs, 128, prefix))
587b0c632dbSHeiko Carstens 		return -EFAULT;
588b0c632dbSHeiko Carstens 
589f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, psw),
590b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gpsw, 16, prefix))
591b0c632dbSHeiko Carstens 		return -EFAULT;
592b0c632dbSHeiko Carstens 
593f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, pref_reg),
594b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->prefix, 4, prefix))
595b0c632dbSHeiko Carstens 		return -EFAULT;
596b0c632dbSHeiko Carstens 
597b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
598f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, fp_ctrl_reg),
599b0c632dbSHeiko Carstens 			&vcpu->arch.guest_fpregs.fpc, 4, prefix))
600b0c632dbSHeiko Carstens 		return -EFAULT;
601b0c632dbSHeiko Carstens 
602f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, tod_reg),
603b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->todpr, 4, prefix))
604b0c632dbSHeiko Carstens 		return -EFAULT;
605b0c632dbSHeiko Carstens 
606f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, timer),
607b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->cputm, 8, prefix))
608b0c632dbSHeiko Carstens 		return -EFAULT;
609b0c632dbSHeiko Carstens 
610f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, clk_cmp),
611b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->ckc, 8, prefix))
612b0c632dbSHeiko Carstens 		return -EFAULT;
613b0c632dbSHeiko Carstens 
614f64ca217SHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area, acc_regs),
615b0c632dbSHeiko Carstens 			&vcpu->arch.guest_acrs, 64, prefix))
616b0c632dbSHeiko Carstens 		return -EFAULT;
617b0c632dbSHeiko Carstens 
618b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
619f64ca217SHeiko Carstens 			addr + offsetof(struct save_area, ctrl_regs),
620b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gcr, 128, prefix))
621b0c632dbSHeiko Carstens 		return -EFAULT;
622b0c632dbSHeiko Carstens 	return 0;
623b0c632dbSHeiko Carstens }
624b0c632dbSHeiko Carstens 
625b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
626b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
627b0c632dbSHeiko Carstens {
628b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
629b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
630bc923cc9SAvi Kivity 	long r;
631b0c632dbSHeiko Carstens 
63293736624SAvi Kivity 	switch (ioctl) {
63393736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
634ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
635ba5c1e9bSCarsten Otte 
63693736624SAvi Kivity 		r = -EFAULT;
637ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
63893736624SAvi Kivity 			break;
63993736624SAvi Kivity 		r = kvm_s390_inject_vcpu(vcpu, &s390int);
64093736624SAvi Kivity 		break;
641ba5c1e9bSCarsten Otte 	}
642b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
643bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
644bc923cc9SAvi Kivity 		break;
645b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
646b0c632dbSHeiko Carstens 		psw_t psw;
647b0c632dbSHeiko Carstens 
648bc923cc9SAvi Kivity 		r = -EFAULT;
649b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
650bc923cc9SAvi Kivity 			break;
651bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
652bc923cc9SAvi Kivity 		break;
653b0c632dbSHeiko Carstens 	}
654b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
655bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
656bc923cc9SAvi Kivity 		break;
657b0c632dbSHeiko Carstens 	default:
658bc923cc9SAvi Kivity 		r = -EINVAL;
659b0c632dbSHeiko Carstens 	}
660bc923cc9SAvi Kivity 	return r;
661b0c632dbSHeiko Carstens }
662b0c632dbSHeiko Carstens 
663b0c632dbSHeiko Carstens /* Section: memory related */
664f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
665f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
666b0c632dbSHeiko Carstens 				   struct kvm_memory_slot old,
667f7784b8eSMarcelo Tosatti 				   struct kvm_userspace_memory_region *mem,
668b0c632dbSHeiko Carstens 				   int user_alloc)
669b0c632dbSHeiko Carstens {
670b0c632dbSHeiko Carstens 	/* A few sanity checks. We can have exactly one memory slot which has
671b0c632dbSHeiko Carstens 	   to start at guest virtual zero and which has to be located at a
672b0c632dbSHeiko Carstens 	   page boundary in userland and which has to end at a page boundary.
673b0c632dbSHeiko Carstens 	   The memory in userland is ok to be fragmented into various different
674b0c632dbSHeiko Carstens 	   vmas. It is okay to mmap() and munmap() stuff in this slot after
675b0c632dbSHeiko Carstens 	   doing this call at any time */
676b0c632dbSHeiko Carstens 
677628eb9b8SChristian Ehrhardt 	if (mem->slot)
678b0c632dbSHeiko Carstens 		return -EINVAL;
679b0c632dbSHeiko Carstens 
680b0c632dbSHeiko Carstens 	if (mem->guest_phys_addr)
681b0c632dbSHeiko Carstens 		return -EINVAL;
682b0c632dbSHeiko Carstens 
683b0c632dbSHeiko Carstens 	if (mem->userspace_addr & (PAGE_SIZE - 1))
684b0c632dbSHeiko Carstens 		return -EINVAL;
685b0c632dbSHeiko Carstens 
686b0c632dbSHeiko Carstens 	if (mem->memory_size & (PAGE_SIZE - 1))
687b0c632dbSHeiko Carstens 		return -EINVAL;
688b0c632dbSHeiko Carstens 
6892668dab7SCarsten Otte 	if (!user_alloc)
6902668dab7SCarsten Otte 		return -EINVAL;
6912668dab7SCarsten Otte 
692f7784b8eSMarcelo Tosatti 	return 0;
693f7784b8eSMarcelo Tosatti }
694f7784b8eSMarcelo Tosatti 
695f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
696f7784b8eSMarcelo Tosatti 				struct kvm_userspace_memory_region *mem,
697f7784b8eSMarcelo Tosatti 				struct kvm_memory_slot old,
698f7784b8eSMarcelo Tosatti 				int user_alloc)
699f7784b8eSMarcelo Tosatti {
700f7784b8eSMarcelo Tosatti 	int i;
701f7784b8eSMarcelo Tosatti 	struct kvm_vcpu *vcpu;
702f7784b8eSMarcelo Tosatti 
703628eb9b8SChristian Ehrhardt 	/* request update of sie control block for all available vcpus */
704988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm) {
705988a2caeSGleb Natapov 		if (test_and_set_bit(KVM_REQ_MMU_RELOAD, &vcpu->requests))
706628eb9b8SChristian Ehrhardt 			continue;
707988a2caeSGleb Natapov 		kvm_s390_inject_sigp_stop(vcpu, ACTION_RELOADVCPU_ON_STOP);
7082668dab7SCarsten Otte 	}
709b0c632dbSHeiko Carstens }
710b0c632dbSHeiko Carstens 
71134d4cb8fSMarcelo Tosatti void kvm_arch_flush_shadow(struct kvm *kvm)
71234d4cb8fSMarcelo Tosatti {
71334d4cb8fSMarcelo Tosatti }
71434d4cb8fSMarcelo Tosatti 
715b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
716b0c632dbSHeiko Carstens {
717ef50f7acSChristian Borntraeger 	int ret;
7180ee75beaSAvi Kivity 	ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
719ef50f7acSChristian Borntraeger 	if (ret)
720ef50f7acSChristian Borntraeger 		return ret;
721ef50f7acSChristian Borntraeger 
722ef50f7acSChristian Borntraeger 	/*
723ef50f7acSChristian Borntraeger 	 * guests can ask for up to 255+1 double words, we need a full page
724*25985edcSLucas De Marchi 	 * to hold the maximum amount of facilities. On the other hand, we
725ef50f7acSChristian Borntraeger 	 * only set facilities that are known to work in KVM.
726ef50f7acSChristian Borntraeger 	 */
727c2f0e8c8SHeiko Carstens 	facilities = (unsigned long long *) get_zeroed_page(GFP_KERNEL|GFP_DMA);
728ef50f7acSChristian Borntraeger 	if (!facilities) {
729ef50f7acSChristian Borntraeger 		kvm_exit();
730ef50f7acSChristian Borntraeger 		return -ENOMEM;
731ef50f7acSChristian Borntraeger 	}
73214375bc4SMartin Schwidefsky 	memcpy(facilities, S390_lowcore.stfle_fac_list, 16);
7336d00d00bSChristian Borntraeger 	facilities[0] &= 0xff00fff3f47c0000ULL;
734ef50f7acSChristian Borntraeger 	return 0;
735b0c632dbSHeiko Carstens }
736b0c632dbSHeiko Carstens 
737b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
738b0c632dbSHeiko Carstens {
739ef50f7acSChristian Borntraeger 	free_page((unsigned long) facilities);
740b0c632dbSHeiko Carstens 	kvm_exit();
741b0c632dbSHeiko Carstens }
742b0c632dbSHeiko Carstens 
743b0c632dbSHeiko Carstens module_init(kvm_s390_init);
744b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
745