xref: /linux/arch/s390/kvm/kvm-s390.c (revision 2bd0ac4eb469ef58c3b1746fccd15da871fc55c4)
1b0c632dbSHeiko Carstens /*
2b0c632dbSHeiko Carstens  * s390host.c --  hosting zSeries kernel virtual machines
3b0c632dbSHeiko Carstens  *
4b0c632dbSHeiko Carstens  * Copyright IBM Corp. 2008
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>
13b0c632dbSHeiko Carstens  */
14b0c632dbSHeiko Carstens 
15b0c632dbSHeiko Carstens #include <linux/compiler.h>
16b0c632dbSHeiko Carstens #include <linux/err.h>
17b0c632dbSHeiko Carstens #include <linux/fs.h>
18b0c632dbSHeiko Carstens #include <linux/init.h>
19b0c632dbSHeiko Carstens #include <linux/kvm.h>
20b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
21b0c632dbSHeiko Carstens #include <linux/module.h>
22b0c632dbSHeiko Carstens #include <linux/slab.h>
23ba5c1e9bSCarsten Otte #include <linux/timer.h>
24b0c632dbSHeiko Carstens #include <asm/lowcore.h>
25b0c632dbSHeiko Carstens #include <asm/pgtable.h>
26b0c632dbSHeiko Carstens 
278f2abe6aSChristian Borntraeger #include "kvm-s390.h"
28b0c632dbSHeiko Carstens #include "gaccess.h"
29b0c632dbSHeiko Carstens 
30b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
31b0c632dbSHeiko Carstens 
32b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
33b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
340eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
358f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
368f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
378f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
388f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
39ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
40ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
41ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
42ba5c1e9bSCarsten Otte 	{ "instruction_lctg", VCPU_STAT(instruction_lctg) },
43ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
44ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
45ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
46ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
47ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
48ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
49ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
50ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
51ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
52453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
53453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
54453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
55453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
56453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
57453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
58453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
59453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
60453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
615288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
625288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
635288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
645288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
655288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
665288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
67e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
68b0c632dbSHeiko Carstens 	{ NULL }
69b0c632dbSHeiko Carstens };
70b0c632dbSHeiko Carstens 
71b0c632dbSHeiko Carstens 
72b0c632dbSHeiko Carstens /* Section: not file related */
73b0c632dbSHeiko Carstens void kvm_arch_hardware_enable(void *garbage)
74b0c632dbSHeiko Carstens {
75b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
76b0c632dbSHeiko Carstens }
77b0c632dbSHeiko Carstens 
78b0c632dbSHeiko Carstens void kvm_arch_hardware_disable(void *garbage)
79b0c632dbSHeiko Carstens {
80b0c632dbSHeiko Carstens }
81b0c632dbSHeiko Carstens 
82b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
83b0c632dbSHeiko Carstens {
84b0c632dbSHeiko Carstens 	return 0;
85b0c632dbSHeiko Carstens }
86b0c632dbSHeiko Carstens 
87b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
88b0c632dbSHeiko Carstens {
89b0c632dbSHeiko Carstens }
90b0c632dbSHeiko Carstens 
91b0c632dbSHeiko Carstens void kvm_arch_check_processor_compat(void *rtn)
92b0c632dbSHeiko Carstens {
93b0c632dbSHeiko Carstens }
94b0c632dbSHeiko Carstens 
95b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
96b0c632dbSHeiko Carstens {
97b0c632dbSHeiko Carstens 	return 0;
98b0c632dbSHeiko Carstens }
99b0c632dbSHeiko Carstens 
100b0c632dbSHeiko Carstens void kvm_arch_exit(void)
101b0c632dbSHeiko Carstens {
102b0c632dbSHeiko Carstens }
103b0c632dbSHeiko Carstens 
104b0c632dbSHeiko Carstens /* Section: device related */
105b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
106b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
107b0c632dbSHeiko Carstens {
108b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
109b0c632dbSHeiko Carstens 		return s390_enable_sie();
110b0c632dbSHeiko Carstens 	return -EINVAL;
111b0c632dbSHeiko Carstens }
112b0c632dbSHeiko Carstens 
113b0c632dbSHeiko Carstens int kvm_dev_ioctl_check_extension(long ext)
114b0c632dbSHeiko Carstens {
115*2bd0ac4eSCarsten Otte 	switch (ext) {
116*2bd0ac4eSCarsten Otte 	case KVM_CAP_USER_MEMORY:
117*2bd0ac4eSCarsten Otte 		return 1;
118*2bd0ac4eSCarsten Otte 	default:
119b0c632dbSHeiko Carstens 		return 0;
120b0c632dbSHeiko Carstens 	}
121*2bd0ac4eSCarsten Otte }
122b0c632dbSHeiko Carstens 
123b0c632dbSHeiko Carstens /* Section: vm related */
124b0c632dbSHeiko Carstens /*
125b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
126b0c632dbSHeiko Carstens  */
127b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
128b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
129b0c632dbSHeiko Carstens {
130b0c632dbSHeiko Carstens 	return 0;
131b0c632dbSHeiko Carstens }
132b0c632dbSHeiko Carstens 
133b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
134b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
135b0c632dbSHeiko Carstens {
136b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
137b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
138b0c632dbSHeiko Carstens 	int r;
139b0c632dbSHeiko Carstens 
140b0c632dbSHeiko Carstens 	switch (ioctl) {
141ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
142ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
143ba5c1e9bSCarsten Otte 
144ba5c1e9bSCarsten Otte 		r = -EFAULT;
145ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
146ba5c1e9bSCarsten Otte 			break;
147ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
148ba5c1e9bSCarsten Otte 		break;
149ba5c1e9bSCarsten Otte 	}
150b0c632dbSHeiko Carstens 	default:
151b0c632dbSHeiko Carstens 		r = -EINVAL;
152b0c632dbSHeiko Carstens 	}
153b0c632dbSHeiko Carstens 
154b0c632dbSHeiko Carstens 	return r;
155b0c632dbSHeiko Carstens }
156b0c632dbSHeiko Carstens 
157b0c632dbSHeiko Carstens struct kvm *kvm_arch_create_vm(void)
158b0c632dbSHeiko Carstens {
159b0c632dbSHeiko Carstens 	struct kvm *kvm;
160b0c632dbSHeiko Carstens 	int rc;
161b0c632dbSHeiko Carstens 	char debug_name[16];
162b0c632dbSHeiko Carstens 
163b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
164b0c632dbSHeiko Carstens 	if (rc)
165b0c632dbSHeiko Carstens 		goto out_nokvm;
166b0c632dbSHeiko Carstens 
167b0c632dbSHeiko Carstens 	rc = -ENOMEM;
168b0c632dbSHeiko Carstens 	kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL);
169b0c632dbSHeiko Carstens 	if (!kvm)
170b0c632dbSHeiko Carstens 		goto out_nokvm;
171b0c632dbSHeiko Carstens 
172b0c632dbSHeiko Carstens 	kvm->arch.sca = (struct sca_block *) get_zeroed_page(GFP_KERNEL);
173b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
174b0c632dbSHeiko Carstens 		goto out_nosca;
175b0c632dbSHeiko Carstens 
176b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
177b0c632dbSHeiko Carstens 
178b0c632dbSHeiko Carstens 	kvm->arch.dbf = debug_register(debug_name, 8, 2, 8 * sizeof(long));
179b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
180b0c632dbSHeiko Carstens 		goto out_nodbf;
181b0c632dbSHeiko Carstens 
182ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
183ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&kvm->arch.float_int.list);
184ba5c1e9bSCarsten Otte 
185b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
186b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "%s", "vm created");
187b0c632dbSHeiko Carstens 
188b0c632dbSHeiko Carstens 	try_module_get(THIS_MODULE);
189b0c632dbSHeiko Carstens 
190b0c632dbSHeiko Carstens 	return kvm;
191b0c632dbSHeiko Carstens out_nodbf:
192b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
193b0c632dbSHeiko Carstens out_nosca:
194b0c632dbSHeiko Carstens 	kfree(kvm);
195b0c632dbSHeiko Carstens out_nokvm:
196b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
197b0c632dbSHeiko Carstens }
198b0c632dbSHeiko Carstens 
199b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
200b0c632dbSHeiko Carstens {
201b0c632dbSHeiko Carstens 	debug_unregister(kvm->arch.dbf);
202dfdded7cSCarsten Otte 	kvm_free_physmem(kvm);
203b0c632dbSHeiko Carstens 	free_page((unsigned long)(kvm->arch.sca));
204b0c632dbSHeiko Carstens 	kfree(kvm);
205b0c632dbSHeiko Carstens 	module_put(THIS_MODULE);
206b0c632dbSHeiko Carstens }
207b0c632dbSHeiko Carstens 
208b0c632dbSHeiko Carstens /* Section: vcpu related */
209b0c632dbSHeiko Carstens int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
210b0c632dbSHeiko Carstens {
211b0c632dbSHeiko Carstens 	return 0;
212b0c632dbSHeiko Carstens }
213b0c632dbSHeiko Carstens 
214b0c632dbSHeiko Carstens void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
215b0c632dbSHeiko Carstens {
216b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but does'nt call it */
217b0c632dbSHeiko Carstens 	BUG();
218b0c632dbSHeiko Carstens }
219b0c632dbSHeiko Carstens 
220b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
221b0c632dbSHeiko Carstens {
222b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.host_fpregs);
223b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.host_acrs);
224b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc &= FPC_VALID_MASK;
225b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.guest_fpregs);
226b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.guest_acrs);
227b0c632dbSHeiko Carstens }
228b0c632dbSHeiko Carstens 
229b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
230b0c632dbSHeiko Carstens {
231b0c632dbSHeiko Carstens 	save_fp_regs(&vcpu->arch.guest_fpregs);
232b0c632dbSHeiko Carstens 	save_access_regs(vcpu->arch.guest_acrs);
233b0c632dbSHeiko Carstens 	restore_fp_regs(&vcpu->arch.host_fpregs);
234b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
235b0c632dbSHeiko Carstens }
236b0c632dbSHeiko Carstens 
237b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
238b0c632dbSHeiko Carstens {
239b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
240b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
241b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
242b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->prefix    = 0UL;
243b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ihcpu     = 0xffff;
244b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->cputm     = 0UL;
245b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
246b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
247b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
248b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
249b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
250b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = 0;
251b0c632dbSHeiko Carstens 	asm volatile("lfpc %0" : : "Q" (vcpu->arch.guest_fpregs.fpc));
252b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
253b0c632dbSHeiko Carstens }
254b0c632dbSHeiko Carstens 
2554da29e90SChristian Borntraeger /* The current code can have up to 256 pages for virtio */
2564da29e90SChristian Borntraeger #define VIRTIODESCSPACE (256ul * 4096ul)
2574da29e90SChristian Borntraeger 
258b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
259b0c632dbSHeiko Carstens {
260b0c632dbSHeiko Carstens 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH);
2614da29e90SChristian Borntraeger 	vcpu->arch.sie_block->gmslm = vcpu->kvm->arch.guest_memsize +
2624da29e90SChristian Borntraeger 				      vcpu->kvm->arch.guest_origin +
2634da29e90SChristian Borntraeger 				      VIRTIODESCSPACE - 1ul;
2644da29e90SChristian Borntraeger 	vcpu->arch.sie_block->gmsor = vcpu->kvm->arch.guest_origin;
265b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ecb   = 2;
266b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->eca   = 0xC1002001U;
267ba5c1e9bSCarsten Otte 	setup_timer(&vcpu->arch.ckc_timer, kvm_s390_idle_wakeup,
268ba5c1e9bSCarsten Otte 		 (unsigned long) vcpu);
269453423dcSChristian Borntraeger 	get_cpu_id(&vcpu->arch.cpu_id);
270453423dcSChristian Borntraeger 	vcpu->arch.cpu_id.version = 0xfe;
271b0c632dbSHeiko Carstens 	return 0;
272b0c632dbSHeiko Carstens }
273b0c632dbSHeiko Carstens 
274b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
275b0c632dbSHeiko Carstens 				      unsigned int id)
276b0c632dbSHeiko Carstens {
277b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
278b0c632dbSHeiko Carstens 	int rc = -ENOMEM;
279b0c632dbSHeiko Carstens 
280b0c632dbSHeiko Carstens 	if (!vcpu)
281b0c632dbSHeiko Carstens 		goto out_nomem;
282b0c632dbSHeiko Carstens 
283180c12fbSChristian Borntraeger 	vcpu->arch.sie_block = (struct kvm_s390_sie_block *)
284180c12fbSChristian Borntraeger 					get_zeroed_page(GFP_KERNEL);
285b0c632dbSHeiko Carstens 
286b0c632dbSHeiko Carstens 	if (!vcpu->arch.sie_block)
287b0c632dbSHeiko Carstens 		goto out_free_cpu;
288b0c632dbSHeiko Carstens 
289b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
290b0c632dbSHeiko Carstens 	BUG_ON(!kvm->arch.sca);
291b0c632dbSHeiko Carstens 	BUG_ON(kvm->arch.sca->cpu[id].sda);
292b0c632dbSHeiko Carstens 	kvm->arch.sca->cpu[id].sda = (__u64) vcpu->arch.sie_block;
293b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->scaoh = (__u32)(((__u64)kvm->arch.sca) >> 32);
294b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->scaol = (__u32)(__u64)kvm->arch.sca;
295b0c632dbSHeiko Carstens 
296ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
297ba5c1e9bSCarsten Otte 	INIT_LIST_HEAD(&vcpu->arch.local_int.list);
298ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
299ba5c1e9bSCarsten Otte 	spin_lock_bh(&kvm->arch.float_int.lock);
300ba5c1e9bSCarsten Otte 	kvm->arch.float_int.local_int[id] = &vcpu->arch.local_int;
301ba5c1e9bSCarsten Otte 	init_waitqueue_head(&vcpu->arch.local_int.wq);
3025288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
303ba5c1e9bSCarsten Otte 	spin_unlock_bh(&kvm->arch.float_int.lock);
304ba5c1e9bSCarsten Otte 
305b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
306b0c632dbSHeiko Carstens 	if (rc)
307b0c632dbSHeiko Carstens 		goto out_free_cpu;
308b0c632dbSHeiko Carstens 	VM_EVENT(kvm, 3, "create cpu %d at %p, sie block at %p", id, vcpu,
309b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
310b0c632dbSHeiko Carstens 
311b0c632dbSHeiko Carstens 	try_module_get(THIS_MODULE);
312b0c632dbSHeiko Carstens 
313b0c632dbSHeiko Carstens 	return vcpu;
314b0c632dbSHeiko Carstens out_free_cpu:
315b0c632dbSHeiko Carstens 	kfree(vcpu);
316b0c632dbSHeiko Carstens out_nomem:
317b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
318b0c632dbSHeiko Carstens }
319b0c632dbSHeiko Carstens 
320b0c632dbSHeiko Carstens void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
321b0c632dbSHeiko Carstens {
322b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 3, "%s", "destroy cpu");
323b0c632dbSHeiko Carstens 	free_page((unsigned long)(vcpu->arch.sie_block));
324b0c632dbSHeiko Carstens 	kfree(vcpu);
325b0c632dbSHeiko Carstens 	module_put(THIS_MODULE);
326b0c632dbSHeiko Carstens }
327b0c632dbSHeiko Carstens 
328b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
329b0c632dbSHeiko Carstens {
330b0c632dbSHeiko Carstens 	/* kvm common code refers to this, but never calls it */
331b0c632dbSHeiko Carstens 	BUG();
332b0c632dbSHeiko Carstens 	return 0;
333b0c632dbSHeiko Carstens }
334b0c632dbSHeiko Carstens 
335b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
336b0c632dbSHeiko Carstens {
337b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
338b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
339b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
340b0c632dbSHeiko Carstens 	return 0;
341b0c632dbSHeiko Carstens }
342b0c632dbSHeiko Carstens 
343b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
344b0c632dbSHeiko Carstens {
345b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
346b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_gprs, &regs->gprs, sizeof(regs->gprs));
347b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
348b0c632dbSHeiko Carstens 	return 0;
349b0c632dbSHeiko Carstens }
350b0c632dbSHeiko Carstens 
351b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
352b0c632dbSHeiko Carstens {
353b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
354b0c632dbSHeiko Carstens 	memcpy(&regs->gprs, &vcpu->arch.guest_gprs, sizeof(regs->gprs));
355b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
356b0c632dbSHeiko Carstens 	return 0;
357b0c632dbSHeiko Carstens }
358b0c632dbSHeiko Carstens 
359b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
360b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
361b0c632dbSHeiko Carstens {
362b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
363b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_acrs, &sregs->acrs, sizeof(sregs->acrs));
364b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
365b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
366b0c632dbSHeiko Carstens 	return 0;
367b0c632dbSHeiko Carstens }
368b0c632dbSHeiko Carstens 
369b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
370b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
371b0c632dbSHeiko Carstens {
372b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
373b0c632dbSHeiko Carstens 	memcpy(&sregs->acrs, &vcpu->arch.guest_acrs, sizeof(sregs->acrs));
374b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
375b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
376b0c632dbSHeiko Carstens 	return 0;
377b0c632dbSHeiko Carstens }
378b0c632dbSHeiko Carstens 
379b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
380b0c632dbSHeiko Carstens {
381b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
382b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_fpregs.fprs, &fpu->fprs, sizeof(fpu->fprs));
383b0c632dbSHeiko Carstens 	vcpu->arch.guest_fpregs.fpc = fpu->fpc;
384b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
385b0c632dbSHeiko Carstens 	return 0;
386b0c632dbSHeiko Carstens }
387b0c632dbSHeiko Carstens 
388b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
389b0c632dbSHeiko Carstens {
390b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
391b0c632dbSHeiko Carstens 	memcpy(&fpu->fprs, &vcpu->arch.guest_fpregs.fprs, sizeof(fpu->fprs));
392b0c632dbSHeiko Carstens 	fpu->fpc = vcpu->arch.guest_fpregs.fpc;
393b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
394b0c632dbSHeiko Carstens 	return 0;
395b0c632dbSHeiko Carstens }
396b0c632dbSHeiko Carstens 
397b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
398b0c632dbSHeiko Carstens {
399b0c632dbSHeiko Carstens 	int rc = 0;
400b0c632dbSHeiko Carstens 
401b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
402b0c632dbSHeiko Carstens 	if (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_RUNNING)
403b0c632dbSHeiko Carstens 		rc = -EBUSY;
404b0c632dbSHeiko Carstens 	else
405b0c632dbSHeiko Carstens 		vcpu->arch.sie_block->gpsw = psw;
406b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
407b0c632dbSHeiko Carstens 	return rc;
408b0c632dbSHeiko Carstens }
409b0c632dbSHeiko Carstens 
410b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
411b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
412b0c632dbSHeiko Carstens {
413b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
414b0c632dbSHeiko Carstens }
415b0c632dbSHeiko Carstens 
416b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_debug_guest(struct kvm_vcpu *vcpu,
417b0c632dbSHeiko Carstens 				    struct kvm_debug_guest *dbg)
418b0c632dbSHeiko Carstens {
419b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
420b0c632dbSHeiko Carstens }
421b0c632dbSHeiko Carstens 
42262d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
42362d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
42462d9f0dbSMarcelo Tosatti {
42562d9f0dbSMarcelo Tosatti 	return -EINVAL; /* not implemented yet */
42662d9f0dbSMarcelo Tosatti }
42762d9f0dbSMarcelo Tosatti 
42862d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_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 
43471cde587SChristian Borntraeger extern void s390_handle_mcck(void);
43571cde587SChristian Borntraeger 
436b0c632dbSHeiko Carstens static void __vcpu_run(struct kvm_vcpu *vcpu)
437b0c632dbSHeiko Carstens {
438b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gg14, &vcpu->arch.guest_gprs[14], 16);
439b0c632dbSHeiko Carstens 
440b0c632dbSHeiko Carstens 	if (need_resched())
441b0c632dbSHeiko Carstens 		schedule();
442b0c632dbSHeiko Carstens 
44371cde587SChristian Borntraeger 	if (test_thread_flag(TIF_MCCK_PENDING))
44471cde587SChristian Borntraeger 		s390_handle_mcck();
44571cde587SChristian Borntraeger 
4460ff31867SCarsten Otte 	kvm_s390_deliver_pending_interrupts(vcpu);
4470ff31867SCarsten Otte 
448b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
449b0c632dbSHeiko Carstens 	local_irq_disable();
450b0c632dbSHeiko Carstens 	kvm_guest_enter();
451b0c632dbSHeiko Carstens 	local_irq_enable();
452b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "entering sie flags %x",
453b0c632dbSHeiko Carstens 		   atomic_read(&vcpu->arch.sie_block->cpuflags));
4541f0d0f09SCarsten Otte 	if (sie64a(vcpu->arch.sie_block, vcpu->arch.guest_gprs)) {
4551f0d0f09SCarsten Otte 		VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4561f0d0f09SCarsten Otte 		kvm_s390_inject_program_int(vcpu, PGM_ADDRESSING);
4571f0d0f09SCarsten Otte 	}
458b0c632dbSHeiko Carstens 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
459b0c632dbSHeiko Carstens 		   vcpu->arch.sie_block->icptcode);
460b0c632dbSHeiko Carstens 	local_irq_disable();
461b0c632dbSHeiko Carstens 	kvm_guest_exit();
462b0c632dbSHeiko Carstens 	local_irq_enable();
463b0c632dbSHeiko Carstens 
464b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.guest_gprs[14], &vcpu->arch.sie_block->gg14, 16);
465b0c632dbSHeiko Carstens }
466b0c632dbSHeiko Carstens 
467b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
468b0c632dbSHeiko Carstens {
4698f2abe6aSChristian Borntraeger 	int rc;
470b0c632dbSHeiko Carstens 	sigset_t sigsaved;
471b0c632dbSHeiko Carstens 
472b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
473b0c632dbSHeiko Carstens 
474b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
475b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
476b0c632dbSHeiko Carstens 
477b0c632dbSHeiko Carstens 	atomic_set_mask(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
478b0c632dbSHeiko Carstens 
479ba5c1e9bSCarsten Otte 	BUG_ON(vcpu->kvm->arch.float_int.local_int[vcpu->vcpu_id] == NULL);
480ba5c1e9bSCarsten Otte 
4818f2abe6aSChristian Borntraeger 	switch (kvm_run->exit_reason) {
4828f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_SIEIC:
4838f2abe6aSChristian Borntraeger 		vcpu->arch.sie_block->gpsw.mask = kvm_run->s390_sieic.mask;
4848f2abe6aSChristian Borntraeger 		vcpu->arch.sie_block->gpsw.addr = kvm_run->s390_sieic.addr;
4858f2abe6aSChristian Borntraeger 		break;
4868f2abe6aSChristian Borntraeger 	case KVM_EXIT_UNKNOWN:
4878f2abe6aSChristian Borntraeger 	case KVM_EXIT_S390_RESET:
4888f2abe6aSChristian Borntraeger 		break;
4898f2abe6aSChristian Borntraeger 	default:
4908f2abe6aSChristian Borntraeger 		BUG();
4918f2abe6aSChristian Borntraeger 	}
4928f2abe6aSChristian Borntraeger 
4938f2abe6aSChristian Borntraeger 	might_sleep();
4948f2abe6aSChristian Borntraeger 
4958f2abe6aSChristian Borntraeger 	do {
496b0c632dbSHeiko Carstens 		__vcpu_run(vcpu);
4978f2abe6aSChristian Borntraeger 		rc = kvm_handle_sie_intercept(vcpu);
4988f2abe6aSChristian Borntraeger 	} while (!signal_pending(current) && !rc);
4998f2abe6aSChristian Borntraeger 
5008f2abe6aSChristian Borntraeger 	if (signal_pending(current) && !rc)
5018f2abe6aSChristian Borntraeger 		rc = -EINTR;
5028f2abe6aSChristian Borntraeger 
5038f2abe6aSChristian Borntraeger 	if (rc == -ENOTSUPP) {
5048f2abe6aSChristian Borntraeger 		/* intercept cannot be handled in-kernel, prepare kvm-run */
5058f2abe6aSChristian Borntraeger 		kvm_run->exit_reason         = KVM_EXIT_S390_SIEIC;
5068f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
5078f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.mask     = vcpu->arch.sie_block->gpsw.mask;
5088f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.addr     = vcpu->arch.sie_block->gpsw.addr;
5098f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipa      = vcpu->arch.sie_block->ipa;
5108f2abe6aSChristian Borntraeger 		kvm_run->s390_sieic.ipb      = vcpu->arch.sie_block->ipb;
5118f2abe6aSChristian Borntraeger 		rc = 0;
5128f2abe6aSChristian Borntraeger 	}
5138f2abe6aSChristian Borntraeger 
5148f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
5158f2abe6aSChristian Borntraeger 		/* intercept was handled, but userspace support is needed
5168f2abe6aSChristian Borntraeger 		 * kvm_run has been prepared by the handler */
5178f2abe6aSChristian Borntraeger 		rc = 0;
5188f2abe6aSChristian Borntraeger 	}
5198f2abe6aSChristian Borntraeger 
520b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
521b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
522b0c632dbSHeiko Carstens 
523b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
524b0c632dbSHeiko Carstens 
525b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
5267e8e6ab4SHeiko Carstens 	return rc;
527b0c632dbSHeiko Carstens }
528b0c632dbSHeiko Carstens 
529b0c632dbSHeiko Carstens static int __guestcopy(struct kvm_vcpu *vcpu, u64 guestdest, const void *from,
530b0c632dbSHeiko Carstens 		       unsigned long n, int prefix)
531b0c632dbSHeiko Carstens {
532b0c632dbSHeiko Carstens 	if (prefix)
533b0c632dbSHeiko Carstens 		return copy_to_guest(vcpu, guestdest, from, n);
534b0c632dbSHeiko Carstens 	else
535b0c632dbSHeiko Carstens 		return copy_to_guest_absolute(vcpu, guestdest, from, n);
536b0c632dbSHeiko Carstens }
537b0c632dbSHeiko Carstens 
538b0c632dbSHeiko Carstens /*
539b0c632dbSHeiko Carstens  * store status at address
540b0c632dbSHeiko Carstens  * we use have two special cases:
541b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
542b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
543b0c632dbSHeiko Carstens  */
544b0c632dbSHeiko Carstens int __kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
545b0c632dbSHeiko Carstens {
546b0c632dbSHeiko Carstens 	const unsigned char archmode = 1;
547b0c632dbSHeiko Carstens 	int prefix;
548b0c632dbSHeiko Carstens 
549b0c632dbSHeiko Carstens 	if (addr == KVM_S390_STORE_STATUS_NOADDR) {
550b0c632dbSHeiko Carstens 		if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
551b0c632dbSHeiko Carstens 			return -EFAULT;
552b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
553b0c632dbSHeiko Carstens 		prefix = 0;
554b0c632dbSHeiko Carstens 	} else if (addr == KVM_S390_STORE_STATUS_PREFIXED) {
555b0c632dbSHeiko Carstens 		if (copy_to_guest(vcpu, 163ul, &archmode, 1))
556b0c632dbSHeiko Carstens 			return -EFAULT;
557b0c632dbSHeiko Carstens 		addr = SAVE_AREA_BASE;
558b0c632dbSHeiko Carstens 		prefix = 1;
559b0c632dbSHeiko Carstens 	} else
560b0c632dbSHeiko Carstens 		prefix = 0;
561b0c632dbSHeiko Carstens 
562b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, fp_regs),
563b0c632dbSHeiko Carstens 			vcpu->arch.guest_fpregs.fprs, 128, prefix))
564b0c632dbSHeiko Carstens 		return -EFAULT;
565b0c632dbSHeiko Carstens 
566b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, gp_regs),
567b0c632dbSHeiko Carstens 			vcpu->arch.guest_gprs, 128, prefix))
568b0c632dbSHeiko Carstens 		return -EFAULT;
569b0c632dbSHeiko Carstens 
570b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, psw),
571b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gpsw, 16, prefix))
572b0c632dbSHeiko Carstens 		return -EFAULT;
573b0c632dbSHeiko Carstens 
574b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, pref_reg),
575b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->prefix, 4, prefix))
576b0c632dbSHeiko Carstens 		return -EFAULT;
577b0c632dbSHeiko Carstens 
578b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
579b0c632dbSHeiko Carstens 			addr + offsetof(struct save_area_s390x, fp_ctrl_reg),
580b0c632dbSHeiko Carstens 			&vcpu->arch.guest_fpregs.fpc, 4, prefix))
581b0c632dbSHeiko Carstens 		return -EFAULT;
582b0c632dbSHeiko Carstens 
583b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, tod_reg),
584b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->todpr, 4, prefix))
585b0c632dbSHeiko Carstens 		return -EFAULT;
586b0c632dbSHeiko Carstens 
587b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, timer),
588b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->cputm, 8, prefix))
589b0c632dbSHeiko Carstens 		return -EFAULT;
590b0c632dbSHeiko Carstens 
591b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, clk_cmp),
592b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->ckc, 8, prefix))
593b0c632dbSHeiko Carstens 		return -EFAULT;
594b0c632dbSHeiko Carstens 
595b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu, addr + offsetof(struct save_area_s390x, acc_regs),
596b0c632dbSHeiko Carstens 			&vcpu->arch.guest_acrs, 64, prefix))
597b0c632dbSHeiko Carstens 		return -EFAULT;
598b0c632dbSHeiko Carstens 
599b0c632dbSHeiko Carstens 	if (__guestcopy(vcpu,
600b0c632dbSHeiko Carstens 			addr + offsetof(struct save_area_s390x, ctrl_regs),
601b0c632dbSHeiko Carstens 			&vcpu->arch.sie_block->gcr, 128, prefix))
602b0c632dbSHeiko Carstens 		return -EFAULT;
603b0c632dbSHeiko Carstens 	return 0;
604b0c632dbSHeiko Carstens }
605b0c632dbSHeiko Carstens 
606b0c632dbSHeiko Carstens static int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
607b0c632dbSHeiko Carstens {
608b0c632dbSHeiko Carstens 	int rc;
609b0c632dbSHeiko Carstens 
610b0c632dbSHeiko Carstens 	vcpu_load(vcpu);
611b0c632dbSHeiko Carstens 	rc = __kvm_s390_vcpu_store_status(vcpu, addr);
612b0c632dbSHeiko Carstens 	vcpu_put(vcpu);
613b0c632dbSHeiko Carstens 	return rc;
614b0c632dbSHeiko Carstens }
615b0c632dbSHeiko Carstens 
616b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
617b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
618b0c632dbSHeiko Carstens {
619b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
620b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
621b0c632dbSHeiko Carstens 
622b0c632dbSHeiko Carstens 	switch (ioctl) {
623ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
624ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
625ba5c1e9bSCarsten Otte 
626ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
627ba5c1e9bSCarsten Otte 			return -EFAULT;
628ba5c1e9bSCarsten Otte 		return kvm_s390_inject_vcpu(vcpu, &s390int);
629ba5c1e9bSCarsten Otte 	}
630b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
631b0c632dbSHeiko Carstens 		return kvm_s390_vcpu_store_status(vcpu, arg);
632b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
633b0c632dbSHeiko Carstens 		psw_t psw;
634b0c632dbSHeiko Carstens 
635b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
636b0c632dbSHeiko Carstens 			return -EFAULT;
637b0c632dbSHeiko Carstens 		return kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
638b0c632dbSHeiko Carstens 	}
639b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
640b0c632dbSHeiko Carstens 		return kvm_arch_vcpu_ioctl_initial_reset(vcpu);
641b0c632dbSHeiko Carstens 	default:
642b0c632dbSHeiko Carstens 		;
643b0c632dbSHeiko Carstens 	}
644b0c632dbSHeiko Carstens 	return -EINVAL;
645b0c632dbSHeiko Carstens }
646b0c632dbSHeiko Carstens 
647b0c632dbSHeiko Carstens /* Section: memory related */
648b0c632dbSHeiko Carstens int kvm_arch_set_memory_region(struct kvm *kvm,
649b0c632dbSHeiko Carstens 				struct kvm_userspace_memory_region *mem,
650b0c632dbSHeiko Carstens 				struct kvm_memory_slot old,
651b0c632dbSHeiko Carstens 				int user_alloc)
652b0c632dbSHeiko Carstens {
653b0c632dbSHeiko Carstens 	/* A few sanity checks. We can have exactly one memory slot which has
654b0c632dbSHeiko Carstens 	   to start at guest virtual zero and which has to be located at a
655b0c632dbSHeiko Carstens 	   page boundary in userland and which has to end at a page boundary.
656b0c632dbSHeiko Carstens 	   The memory in userland is ok to be fragmented into various different
657b0c632dbSHeiko Carstens 	   vmas. It is okay to mmap() and munmap() stuff in this slot after
658b0c632dbSHeiko Carstens 	   doing this call at any time */
659b0c632dbSHeiko Carstens 
660b0c632dbSHeiko Carstens 	if (mem->slot)
661b0c632dbSHeiko Carstens 		return -EINVAL;
662b0c632dbSHeiko Carstens 
663b0c632dbSHeiko Carstens 	if (mem->guest_phys_addr)
664b0c632dbSHeiko Carstens 		return -EINVAL;
665b0c632dbSHeiko Carstens 
666b0c632dbSHeiko Carstens 	if (mem->userspace_addr & (PAGE_SIZE - 1))
667b0c632dbSHeiko Carstens 		return -EINVAL;
668b0c632dbSHeiko Carstens 
669b0c632dbSHeiko Carstens 	if (mem->memory_size & (PAGE_SIZE - 1))
670b0c632dbSHeiko Carstens 		return -EINVAL;
671b0c632dbSHeiko Carstens 
672b0c632dbSHeiko Carstens 	kvm->arch.guest_origin = mem->userspace_addr;
673b0c632dbSHeiko Carstens 	kvm->arch.guest_memsize = mem->memory_size;
674b0c632dbSHeiko Carstens 
675b0c632dbSHeiko Carstens 	/* FIXME: we do want to interrupt running CPUs and update their memory
676b0c632dbSHeiko Carstens 	   configuration now to avoid race conditions. But hey, changing the
677b0c632dbSHeiko Carstens 	   memory layout while virtual CPUs are running is usually bad
678b0c632dbSHeiko Carstens 	   programming practice. */
679b0c632dbSHeiko Carstens 
680b0c632dbSHeiko Carstens 	return 0;
681b0c632dbSHeiko Carstens }
682b0c632dbSHeiko Carstens 
68334d4cb8fSMarcelo Tosatti void kvm_arch_flush_shadow(struct kvm *kvm)
68434d4cb8fSMarcelo Tosatti {
68534d4cb8fSMarcelo Tosatti }
68634d4cb8fSMarcelo Tosatti 
687b0c632dbSHeiko Carstens gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn)
688b0c632dbSHeiko Carstens {
689b0c632dbSHeiko Carstens 	return gfn;
690b0c632dbSHeiko Carstens }
691b0c632dbSHeiko Carstens 
692b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
693b0c632dbSHeiko Carstens {
694b0c632dbSHeiko Carstens 	return kvm_init(NULL, sizeof(struct kvm_vcpu), THIS_MODULE);
695b0c632dbSHeiko Carstens }
696b0c632dbSHeiko Carstens 
697b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
698b0c632dbSHeiko Carstens {
699b0c632dbSHeiko Carstens 	kvm_exit();
700b0c632dbSHeiko Carstens }
701b0c632dbSHeiko Carstens 
702b0c632dbSHeiko Carstens module_init(kvm_s390_init);
703b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
704