xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision fd5ada04030cb584251c381cb70daa41e984ae82)
1b0c632dbSHeiko Carstens /*
2a53c8fabSHeiko Carstens  * 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>
1415f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
15b0c632dbSHeiko Carstens  */
16b0c632dbSHeiko Carstens 
17b0c632dbSHeiko Carstens #include <linux/compiler.h>
18b0c632dbSHeiko Carstens #include <linux/err.h>
19b0c632dbSHeiko Carstens #include <linux/fs.h>
20ca872302SChristian Borntraeger #include <linux/hrtimer.h>
21b0c632dbSHeiko Carstens #include <linux/init.h>
22b0c632dbSHeiko Carstens #include <linux/kvm.h>
23b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
24b0c632dbSHeiko Carstens #include <linux/module.h>
25a374e892STony Krowiak #include <linux/random.h>
26b0c632dbSHeiko Carstens #include <linux/slab.h>
27ba5c1e9bSCarsten Otte #include <linux/timer.h>
2841408c28SThomas Huth #include <linux/vmalloc.h>
29cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
30b0c632dbSHeiko Carstens #include <asm/lowcore.h>
31*fd5ada04SMartin Schwidefsky #include <asm/stp.h>
32b0c632dbSHeiko Carstens #include <asm/pgtable.h>
331e133ab2SMartin Schwidefsky #include <asm/gmap.h>
34f5daba1dSHeiko Carstens #include <asm/nmi.h>
35a0616cdeSDavid Howells #include <asm/switch_to.h>
366d3da241SJens Freimann #include <asm/isc.h>
371526bf9cSChristian Borntraeger #include <asm/sclp.h>
388f2abe6aSChristian Borntraeger #include "kvm-s390.h"
39b0c632dbSHeiko Carstens #include "gaccess.h"
40b0c632dbSHeiko Carstens 
41ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
42ea2cdd27SDavid Hildenbrand #undef pr_fmt
43ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
44ea2cdd27SDavid Hildenbrand 
455786fffaSCornelia Huck #define CREATE_TRACE_POINTS
465786fffaSCornelia Huck #include "trace.h"
47ade38c31SCornelia Huck #include "trace-s390.h"
485786fffaSCornelia Huck 
4941408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
50816c7667SJens Freimann #define LOCAL_IRQS 32
51816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
52816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
5341408c28SThomas Huth 
54b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
55b0c632dbSHeiko Carstens 
56b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
57b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
580eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
598f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
608f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
618f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
628f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
63ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
64ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
65ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
66f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
6762bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
683491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
69ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
70f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
71ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
72aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
73aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
74ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
757697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
76ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
77ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
78ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
79ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
80ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
81ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
82ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
8369d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
84453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
85453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
86453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
87453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
88453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
898a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
90453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
91453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
92b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
93453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
94453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
95bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
965288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
97bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
987697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
995288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
10042cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
10142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1025288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
10342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
10442cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
105cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1065288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1075288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1085288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
10942cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
11042cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
11142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
112388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
113e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
11441628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
115175a5c9eSChristian Borntraeger 	{ "diagnose_258", VCPU_STAT(diagnose_258) },
116175a5c9eSChristian Borntraeger 	{ "diagnose_308", VCPU_STAT(diagnose_308) },
117175a5c9eSChristian Borntraeger 	{ "diagnose_500", VCPU_STAT(diagnose_500) },
118b0c632dbSHeiko Carstens 	{ NULL }
119b0c632dbSHeiko Carstens };
120b0c632dbSHeiko Carstens 
1219d8d5786SMichael Mueller /* upper facilities limit for kvm */
12260a37709SAlexander Yarygin unsigned long kvm_s390_fac_list_mask[16] = {
12360a37709SAlexander Yarygin 	0xffe6000000000000UL,
12460a37709SAlexander Yarygin 	0x005e000000000000UL,
1259d8d5786SMichael Mueller };
126b0c632dbSHeiko Carstens 
1279d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void)
12878c4b59fSMichael Mueller {
1299d8d5786SMichael Mueller 	BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
1309d8d5786SMichael Mueller 	return ARRAY_SIZE(kvm_s390_fac_list_mask);
13178c4b59fSMichael Mueller }
13278c4b59fSMichael Mueller 
1339d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
13478f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
1359d8d5786SMichael Mueller 
136b0c632dbSHeiko Carstens /* Section: not file related */
13713a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
138b0c632dbSHeiko Carstens {
139b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
14010474ae8SAlexander Graf 	return 0;
141b0c632dbSHeiko Carstens }
142b0c632dbSHeiko Carstens 
1432c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address);
1442c70fe44SChristian Borntraeger 
145fdf03650SFan Zhang /*
146fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
147fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
148fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
149fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
150fdf03650SFan Zhang  */
151fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
152fdf03650SFan Zhang 			  void *v)
153fdf03650SFan Zhang {
154fdf03650SFan Zhang 	struct kvm *kvm;
155fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
156fdf03650SFan Zhang 	int i;
157fdf03650SFan Zhang 	unsigned long long *delta = v;
158fdf03650SFan Zhang 
159fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
160fdf03650SFan Zhang 		kvm->arch.epoch -= *delta;
161fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
162fdf03650SFan Zhang 			vcpu->arch.sie_block->epoch -= *delta;
163db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
164db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
165fdf03650SFan Zhang 		}
166fdf03650SFan Zhang 	}
167fdf03650SFan Zhang 	return NOTIFY_OK;
168fdf03650SFan Zhang }
169fdf03650SFan Zhang 
170fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
171fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
172fdf03650SFan Zhang };
173fdf03650SFan Zhang 
174b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
175b0c632dbSHeiko Carstens {
1762c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
1772c70fe44SChristian Borntraeger 	gmap_register_ipte_notifier(&gmap_notifier);
178fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
179fdf03650SFan Zhang 				       &kvm_clock_notifier);
180b0c632dbSHeiko Carstens 	return 0;
181b0c632dbSHeiko Carstens }
182b0c632dbSHeiko Carstens 
183b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
184b0c632dbSHeiko Carstens {
1852c70fe44SChristian Borntraeger 	gmap_unregister_ipte_notifier(&gmap_notifier);
186fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
187fdf03650SFan Zhang 					 &kvm_clock_notifier);
188b0c632dbSHeiko Carstens }
189b0c632dbSHeiko Carstens 
190b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
191b0c632dbSHeiko Carstens {
19278f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
19378f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
19478f26131SChristian Borntraeger 		return -ENOMEM;
19578f26131SChristian Borntraeger 
19678f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
19778f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
19878f26131SChristian Borntraeger 		return -ENOMEM;
19978f26131SChristian Borntraeger 	}
20078f26131SChristian Borntraeger 
20184877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
20284877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
203b0c632dbSHeiko Carstens }
204b0c632dbSHeiko Carstens 
20578f26131SChristian Borntraeger void kvm_arch_exit(void)
20678f26131SChristian Borntraeger {
20778f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
20878f26131SChristian Borntraeger }
20978f26131SChristian Borntraeger 
210b0c632dbSHeiko Carstens /* Section: device related */
211b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
212b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
213b0c632dbSHeiko Carstens {
214b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
215b0c632dbSHeiko Carstens 		return s390_enable_sie();
216b0c632dbSHeiko Carstens 	return -EINVAL;
217b0c632dbSHeiko Carstens }
218b0c632dbSHeiko Carstens 
219784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
220b0c632dbSHeiko Carstens {
221d7b0b5ebSCarsten Otte 	int r;
222d7b0b5ebSCarsten Otte 
2232bd0ac4eSCarsten Otte 	switch (ext) {
224d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
225b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
22652e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
2271efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
2281efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
2291efd0f59SCarsten Otte #endif
2303c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
23160b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
23214eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
233d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
234fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
23510ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
236c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
237d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
23878599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
239f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
2406352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
24147b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
2422444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
243e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
24430ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
245816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
246d7b0b5ebSCarsten Otte 		r = 1;
247d7b0b5ebSCarsten Otte 		break;
24841408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
24941408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
25041408c28SThomas Huth 		break;
251e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
252e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
253fe0edcb7SEugene (jno) Dvurechenski 		r = sclp.has_esca ? KVM_S390_ESCA_CPU_SLOTS
254fe0edcb7SEugene (jno) Dvurechenski 				  : KVM_S390_BSCA_CPU_SLOTS;
255e726b1bdSChristian Borntraeger 		break;
256e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
257e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
258e1e2e605SNick Wang 		break;
2591526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
260abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
2611526bf9cSChristian Borntraeger 		break;
26268c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
26368c55750SEric Farman 		r = MACHINE_HAS_VX;
26468c55750SEric Farman 		break;
265c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
266c6e5f166SFan Zhang 		r = test_facility(64);
267c6e5f166SFan Zhang 		break;
2682bd0ac4eSCarsten Otte 	default:
269d7b0b5ebSCarsten Otte 		r = 0;
270b0c632dbSHeiko Carstens 	}
271d7b0b5ebSCarsten Otte 	return r;
2722bd0ac4eSCarsten Otte }
273b0c632dbSHeiko Carstens 
27415f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
27515f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
27615f36ebdSJason J. Herne {
27715f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
27815f36ebdSJason J. Herne 	unsigned long address;
27915f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
28015f36ebdSJason J. Herne 
28115f36ebdSJason J. Herne 	/* Loop over all guest pages */
28215f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
28315f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
28415f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
28515f36ebdSJason J. Herne 
2861e133ab2SMartin Schwidefsky 		if (test_and_clear_guest_dirty(gmap->mm, address))
28715f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
2881763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
2891763f8d0SChristian Borntraeger 			return;
29070c88a00SChristian Borntraeger 		cond_resched();
29115f36ebdSJason J. Herne 	}
29215f36ebdSJason J. Herne }
29315f36ebdSJason J. Herne 
294b0c632dbSHeiko Carstens /* Section: vm related */
295a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
296a6e2f683SEugene (jno) Dvurechenski 
297b0c632dbSHeiko Carstens /*
298b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
299b0c632dbSHeiko Carstens  */
300b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
301b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
302b0c632dbSHeiko Carstens {
30315f36ebdSJason J. Herne 	int r;
30415f36ebdSJason J. Herne 	unsigned long n;
3059f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
30615f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
30715f36ebdSJason J. Herne 	int is_dirty = 0;
30815f36ebdSJason J. Herne 
30915f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
31015f36ebdSJason J. Herne 
31115f36ebdSJason J. Herne 	r = -EINVAL;
31215f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
31315f36ebdSJason J. Herne 		goto out;
31415f36ebdSJason J. Herne 
3159f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
3169f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
31715f36ebdSJason J. Herne 	r = -ENOENT;
31815f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
31915f36ebdSJason J. Herne 		goto out;
32015f36ebdSJason J. Herne 
32115f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
32215f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
32315f36ebdSJason J. Herne 	if (r)
32415f36ebdSJason J. Herne 		goto out;
32515f36ebdSJason J. Herne 
32615f36ebdSJason J. Herne 	/* Clear the dirty log */
32715f36ebdSJason J. Herne 	if (is_dirty) {
32815f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
32915f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
33015f36ebdSJason J. Herne 	}
33115f36ebdSJason J. Herne 	r = 0;
33215f36ebdSJason J. Herne out:
33315f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
33415f36ebdSJason J. Herne 	return r;
335b0c632dbSHeiko Carstens }
336b0c632dbSHeiko Carstens 
337d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
338d938dc55SCornelia Huck {
339d938dc55SCornelia Huck 	int r;
340d938dc55SCornelia Huck 
341d938dc55SCornelia Huck 	if (cap->flags)
342d938dc55SCornelia Huck 		return -EINVAL;
343d938dc55SCornelia Huck 
344d938dc55SCornelia Huck 	switch (cap->cap) {
34584223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
346c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
34784223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
34884223598SCornelia Huck 		r = 0;
34984223598SCornelia Huck 		break;
3502444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
351c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
3522444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
3532444b352SDavid Hildenbrand 		r = 0;
3542444b352SDavid Hildenbrand 		break;
35568c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
3565967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
3575967c17bSDavid Hildenbrand 		if (atomic_read(&kvm->online_vcpus)) {
3585967c17bSDavid Hildenbrand 			r = -EBUSY;
3595967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
360c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
361c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
36218280d8bSMichael Mueller 			r = 0;
36318280d8bSMichael Mueller 		} else
36418280d8bSMichael Mueller 			r = -EINVAL;
3655967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
366c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
367c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
36868c55750SEric Farman 		break;
369c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
370c6e5f166SFan Zhang 		r = -EINVAL;
371c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
372c6e5f166SFan Zhang 		if (atomic_read(&kvm->online_vcpus)) {
373c6e5f166SFan Zhang 			r = -EBUSY;
374c6e5f166SFan Zhang 		} else if (test_facility(64)) {
375c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
376c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
377c6e5f166SFan Zhang 			r = 0;
378c6e5f166SFan Zhang 		}
379c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
380c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
381c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
382c6e5f166SFan Zhang 		break;
383e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
384c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
385e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
386e44fc8c9SEkaterina Tumanova 		r = 0;
387e44fc8c9SEkaterina Tumanova 		break;
388d938dc55SCornelia Huck 	default:
389d938dc55SCornelia Huck 		r = -EINVAL;
390d938dc55SCornelia Huck 		break;
391d938dc55SCornelia Huck 	}
392d938dc55SCornelia Huck 	return r;
393d938dc55SCornelia Huck }
394d938dc55SCornelia Huck 
3958c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
3968c0a7ce6SDominik Dingel {
3978c0a7ce6SDominik Dingel 	int ret;
3988c0a7ce6SDominik Dingel 
3998c0a7ce6SDominik Dingel 	switch (attr->attr) {
4008c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
4018c0a7ce6SDominik Dingel 		ret = 0;
402c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
403a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
404a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
4058c0a7ce6SDominik Dingel 			ret = -EFAULT;
4068c0a7ce6SDominik Dingel 		break;
4078c0a7ce6SDominik Dingel 	default:
4088c0a7ce6SDominik Dingel 		ret = -ENXIO;
4098c0a7ce6SDominik Dingel 		break;
4108c0a7ce6SDominik Dingel 	}
4118c0a7ce6SDominik Dingel 	return ret;
4128c0a7ce6SDominik Dingel }
4138c0a7ce6SDominik Dingel 
4148c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
4154f718eabSDominik Dingel {
4164f718eabSDominik Dingel 	int ret;
4174f718eabSDominik Dingel 	unsigned int idx;
4184f718eabSDominik Dingel 	switch (attr->attr) {
4194f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
420e6db1d61SDominik Dingel 		/* enable CMMA only for z10 and later (EDAT_1) */
421e6db1d61SDominik Dingel 		ret = -EINVAL;
422e6db1d61SDominik Dingel 		if (!MACHINE_IS_LPAR || !MACHINE_HAS_EDAT1)
423e6db1d61SDominik Dingel 			break;
424e6db1d61SDominik Dingel 
4254f718eabSDominik Dingel 		ret = -EBUSY;
426c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
4274f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
4284f718eabSDominik Dingel 		if (atomic_read(&kvm->online_vcpus) == 0) {
4294f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
4304f718eabSDominik Dingel 			ret = 0;
4314f718eabSDominik Dingel 		}
4324f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
4334f718eabSDominik Dingel 		break;
4344f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
435c3489155SDominik Dingel 		ret = -EINVAL;
436c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
437c3489155SDominik Dingel 			break;
438c3489155SDominik Dingel 
439c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
4404f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
4414f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
442a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
4434f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
4444f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
4454f718eabSDominik Dingel 		ret = 0;
4464f718eabSDominik Dingel 		break;
4478c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
4488c0a7ce6SDominik Dingel 		unsigned long new_limit;
4498c0a7ce6SDominik Dingel 
4508c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
4518c0a7ce6SDominik Dingel 			return -EINVAL;
4528c0a7ce6SDominik Dingel 
4538c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
4548c0a7ce6SDominik Dingel 			return -EFAULT;
4558c0a7ce6SDominik Dingel 
456a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
457a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
4588c0a7ce6SDominik Dingel 			return -E2BIG;
4598c0a7ce6SDominik Dingel 
460a3a92c31SDominik Dingel 		if (!new_limit)
461a3a92c31SDominik Dingel 			return -EINVAL;
462a3a92c31SDominik Dingel 
463a3a92c31SDominik Dingel 		/* gmap_alloc takes last usable address */
464a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
465a3a92c31SDominik Dingel 			new_limit -= 1;
466a3a92c31SDominik Dingel 
4678c0a7ce6SDominik Dingel 		ret = -EBUSY;
4688c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
4698c0a7ce6SDominik Dingel 		if (atomic_read(&kvm->online_vcpus) == 0) {
4708c0a7ce6SDominik Dingel 			/* gmap_alloc will round the limit up */
4718c0a7ce6SDominik Dingel 			struct gmap *new = gmap_alloc(current->mm, new_limit);
4728c0a7ce6SDominik Dingel 
4738c0a7ce6SDominik Dingel 			if (!new) {
4748c0a7ce6SDominik Dingel 				ret = -ENOMEM;
4758c0a7ce6SDominik Dingel 			} else {
4768c0a7ce6SDominik Dingel 				gmap_free(kvm->arch.gmap);
4778c0a7ce6SDominik Dingel 				new->private = kvm;
4788c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
4798c0a7ce6SDominik Dingel 				ret = 0;
4808c0a7ce6SDominik Dingel 			}
4818c0a7ce6SDominik Dingel 		}
4828c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
483a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
484a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
485a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
4868c0a7ce6SDominik Dingel 		break;
4878c0a7ce6SDominik Dingel 	}
4884f718eabSDominik Dingel 	default:
4894f718eabSDominik Dingel 		ret = -ENXIO;
4904f718eabSDominik Dingel 		break;
4914f718eabSDominik Dingel 	}
4924f718eabSDominik Dingel 	return ret;
4934f718eabSDominik Dingel }
4944f718eabSDominik Dingel 
495a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
496a374e892STony Krowiak 
497a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
498a374e892STony Krowiak {
499a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
500a374e892STony Krowiak 	int i;
501a374e892STony Krowiak 
5029d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
503a374e892STony Krowiak 		return -EINVAL;
504a374e892STony Krowiak 
505a374e892STony Krowiak 	mutex_lock(&kvm->lock);
506a374e892STony Krowiak 	switch (attr->attr) {
507a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
508a374e892STony Krowiak 		get_random_bytes(
509a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
510a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
511a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
512c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
513a374e892STony Krowiak 		break;
514a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
515a374e892STony Krowiak 		get_random_bytes(
516a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
517a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
518a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
519c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
520a374e892STony Krowiak 		break;
521a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
522a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
523a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
524a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
525c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
526a374e892STony Krowiak 		break;
527a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
528a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
529a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
530a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
531c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
532a374e892STony Krowiak 		break;
533a374e892STony Krowiak 	default:
534a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
535a374e892STony Krowiak 		return -ENXIO;
536a374e892STony Krowiak 	}
537a374e892STony Krowiak 
538a374e892STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm) {
539a374e892STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
540a374e892STony Krowiak 		exit_sie(vcpu);
541a374e892STony Krowiak 	}
542a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
543a374e892STony Krowiak 	return 0;
544a374e892STony Krowiak }
545a374e892STony Krowiak 
54672f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
54772f25020SJason J. Herne {
54872f25020SJason J. Herne 	u8 gtod_high;
54972f25020SJason J. Herne 
55072f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
55172f25020SJason J. Herne 					   sizeof(gtod_high)))
55272f25020SJason J. Herne 		return -EFAULT;
55372f25020SJason J. Herne 
55472f25020SJason J. Herne 	if (gtod_high != 0)
55572f25020SJason J. Herne 		return -EINVAL;
55658c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
55772f25020SJason J. Herne 
55872f25020SJason J. Herne 	return 0;
55972f25020SJason J. Herne }
56072f25020SJason J. Herne 
56172f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
56272f25020SJason J. Herne {
5635a3d883aSDavid Hildenbrand 	u64 gtod;
56472f25020SJason J. Herne 
56572f25020SJason J. Herne 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
56672f25020SJason J. Herne 		return -EFAULT;
56772f25020SJason J. Herne 
56825ed1675SDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, gtod);
56958c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
57072f25020SJason J. Herne 	return 0;
57172f25020SJason J. Herne }
57272f25020SJason J. Herne 
57372f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
57472f25020SJason J. Herne {
57572f25020SJason J. Herne 	int ret;
57672f25020SJason J. Herne 
57772f25020SJason J. Herne 	if (attr->flags)
57872f25020SJason J. Herne 		return -EINVAL;
57972f25020SJason J. Herne 
58072f25020SJason J. Herne 	switch (attr->attr) {
58172f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
58272f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
58372f25020SJason J. Herne 		break;
58472f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
58572f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
58672f25020SJason J. Herne 		break;
58772f25020SJason J. Herne 	default:
58872f25020SJason J. Herne 		ret = -ENXIO;
58972f25020SJason J. Herne 		break;
59072f25020SJason J. Herne 	}
59172f25020SJason J. Herne 	return ret;
59272f25020SJason J. Herne }
59372f25020SJason J. Herne 
59472f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
59572f25020SJason J. Herne {
59672f25020SJason J. Herne 	u8 gtod_high = 0;
59772f25020SJason J. Herne 
59872f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
59972f25020SJason J. Herne 					 sizeof(gtod_high)))
60072f25020SJason J. Herne 		return -EFAULT;
60158c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
60272f25020SJason J. Herne 
60372f25020SJason J. Herne 	return 0;
60472f25020SJason J. Herne }
60572f25020SJason J. Herne 
60672f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
60772f25020SJason J. Herne {
6085a3d883aSDavid Hildenbrand 	u64 gtod;
60972f25020SJason J. Herne 
61060417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
61172f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
61272f25020SJason J. Herne 		return -EFAULT;
61358c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
61472f25020SJason J. Herne 
61572f25020SJason J. Herne 	return 0;
61672f25020SJason J. Herne }
61772f25020SJason J. Herne 
61872f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
61972f25020SJason J. Herne {
62072f25020SJason J. Herne 	int ret;
62172f25020SJason J. Herne 
62272f25020SJason J. Herne 	if (attr->flags)
62372f25020SJason J. Herne 		return -EINVAL;
62472f25020SJason J. Herne 
62572f25020SJason J. Herne 	switch (attr->attr) {
62672f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
62772f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
62872f25020SJason J. Herne 		break;
62972f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
63072f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
63172f25020SJason J. Herne 		break;
63272f25020SJason J. Herne 	default:
63372f25020SJason J. Herne 		ret = -ENXIO;
63472f25020SJason J. Herne 		break;
63572f25020SJason J. Herne 	}
63672f25020SJason J. Herne 	return ret;
63772f25020SJason J. Herne }
63872f25020SJason J. Herne 
639658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
640658b6edaSMichael Mueller {
641658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
642053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
643658b6edaSMichael Mueller 	int ret = 0;
644658b6edaSMichael Mueller 
645658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
646658b6edaSMichael Mueller 	if (atomic_read(&kvm->online_vcpus)) {
647658b6edaSMichael Mueller 		ret = -EBUSY;
648658b6edaSMichael Mueller 		goto out;
649658b6edaSMichael Mueller 	}
650658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
651658b6edaSMichael Mueller 	if (!proc) {
652658b6edaSMichael Mueller 		ret = -ENOMEM;
653658b6edaSMichael Mueller 		goto out;
654658b6edaSMichael Mueller 	}
655658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
656658b6edaSMichael Mueller 			    sizeof(*proc))) {
6579bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
658053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
659053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
660053dd230SDavid Hildenbrand 		if (lowest_ibc) {
661053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
662053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
663053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
664053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
665053dd230SDavid Hildenbrand 			else
666658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
667053dd230SDavid Hildenbrand 		}
668c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
669658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
670658b6edaSMichael Mueller 	} else
671658b6edaSMichael Mueller 		ret = -EFAULT;
672658b6edaSMichael Mueller 	kfree(proc);
673658b6edaSMichael Mueller out:
674658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
675658b6edaSMichael Mueller 	return ret;
676658b6edaSMichael Mueller }
677658b6edaSMichael Mueller 
678658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
679658b6edaSMichael Mueller {
680658b6edaSMichael Mueller 	int ret = -ENXIO;
681658b6edaSMichael Mueller 
682658b6edaSMichael Mueller 	switch (attr->attr) {
683658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
684658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
685658b6edaSMichael Mueller 		break;
686658b6edaSMichael Mueller 	}
687658b6edaSMichael Mueller 	return ret;
688658b6edaSMichael Mueller }
689658b6edaSMichael Mueller 
690658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
691658b6edaSMichael Mueller {
692658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
693658b6edaSMichael Mueller 	int ret = 0;
694658b6edaSMichael Mueller 
695658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
696658b6edaSMichael Mueller 	if (!proc) {
697658b6edaSMichael Mueller 		ret = -ENOMEM;
698658b6edaSMichael Mueller 		goto out;
699658b6edaSMichael Mueller 	}
7009bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
701658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
702c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
703c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
704658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
705658b6edaSMichael Mueller 		ret = -EFAULT;
706658b6edaSMichael Mueller 	kfree(proc);
707658b6edaSMichael Mueller out:
708658b6edaSMichael Mueller 	return ret;
709658b6edaSMichael Mueller }
710658b6edaSMichael Mueller 
711658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
712658b6edaSMichael Mueller {
713658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
714658b6edaSMichael Mueller 	int ret = 0;
715658b6edaSMichael Mueller 
716658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
717658b6edaSMichael Mueller 	if (!mach) {
718658b6edaSMichael Mueller 		ret = -ENOMEM;
719658b6edaSMichael Mueller 		goto out;
720658b6edaSMichael Mueller 	}
721658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
72237c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
723c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
724981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
725658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
72694422ee8SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
727658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
728658b6edaSMichael Mueller 		ret = -EFAULT;
729658b6edaSMichael Mueller 	kfree(mach);
730658b6edaSMichael Mueller out:
731658b6edaSMichael Mueller 	return ret;
732658b6edaSMichael Mueller }
733658b6edaSMichael Mueller 
734658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
735658b6edaSMichael Mueller {
736658b6edaSMichael Mueller 	int ret = -ENXIO;
737658b6edaSMichael Mueller 
738658b6edaSMichael Mueller 	switch (attr->attr) {
739658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
740658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
741658b6edaSMichael Mueller 		break;
742658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
743658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
744658b6edaSMichael Mueller 		break;
745658b6edaSMichael Mueller 	}
746658b6edaSMichael Mueller 	return ret;
747658b6edaSMichael Mueller }
748658b6edaSMichael Mueller 
749f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
750f2061656SDominik Dingel {
751f2061656SDominik Dingel 	int ret;
752f2061656SDominik Dingel 
753f2061656SDominik Dingel 	switch (attr->group) {
7544f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
7558c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
7564f718eabSDominik Dingel 		break;
75772f25020SJason J. Herne 	case KVM_S390_VM_TOD:
75872f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
75972f25020SJason J. Herne 		break;
760658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
761658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
762658b6edaSMichael Mueller 		break;
763a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
764a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
765a374e892STony Krowiak 		break;
766f2061656SDominik Dingel 	default:
767f2061656SDominik Dingel 		ret = -ENXIO;
768f2061656SDominik Dingel 		break;
769f2061656SDominik Dingel 	}
770f2061656SDominik Dingel 
771f2061656SDominik Dingel 	return ret;
772f2061656SDominik Dingel }
773f2061656SDominik Dingel 
774f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
775f2061656SDominik Dingel {
7768c0a7ce6SDominik Dingel 	int ret;
7778c0a7ce6SDominik Dingel 
7788c0a7ce6SDominik Dingel 	switch (attr->group) {
7798c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
7808c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
7818c0a7ce6SDominik Dingel 		break;
78272f25020SJason J. Herne 	case KVM_S390_VM_TOD:
78372f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
78472f25020SJason J. Herne 		break;
785658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
786658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
787658b6edaSMichael Mueller 		break;
7888c0a7ce6SDominik Dingel 	default:
7898c0a7ce6SDominik Dingel 		ret = -ENXIO;
7908c0a7ce6SDominik Dingel 		break;
7918c0a7ce6SDominik Dingel 	}
7928c0a7ce6SDominik Dingel 
7938c0a7ce6SDominik Dingel 	return ret;
794f2061656SDominik Dingel }
795f2061656SDominik Dingel 
796f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
797f2061656SDominik Dingel {
798f2061656SDominik Dingel 	int ret;
799f2061656SDominik Dingel 
800f2061656SDominik Dingel 	switch (attr->group) {
8014f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
8024f718eabSDominik Dingel 		switch (attr->attr) {
8034f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
8044f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
8058c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
8064f718eabSDominik Dingel 			ret = 0;
8074f718eabSDominik Dingel 			break;
8084f718eabSDominik Dingel 		default:
8094f718eabSDominik Dingel 			ret = -ENXIO;
8104f718eabSDominik Dingel 			break;
8114f718eabSDominik Dingel 		}
8124f718eabSDominik Dingel 		break;
81372f25020SJason J. Herne 	case KVM_S390_VM_TOD:
81472f25020SJason J. Herne 		switch (attr->attr) {
81572f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
81672f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
81772f25020SJason J. Herne 			ret = 0;
81872f25020SJason J. Herne 			break;
81972f25020SJason J. Herne 		default:
82072f25020SJason J. Herne 			ret = -ENXIO;
82172f25020SJason J. Herne 			break;
82272f25020SJason J. Herne 		}
82372f25020SJason J. Herne 		break;
824658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
825658b6edaSMichael Mueller 		switch (attr->attr) {
826658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
827658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
828658b6edaSMichael Mueller 			ret = 0;
829658b6edaSMichael Mueller 			break;
830658b6edaSMichael Mueller 		default:
831658b6edaSMichael Mueller 			ret = -ENXIO;
832658b6edaSMichael Mueller 			break;
833658b6edaSMichael Mueller 		}
834658b6edaSMichael Mueller 		break;
835a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
836a374e892STony Krowiak 		switch (attr->attr) {
837a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
838a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
839a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
840a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
841a374e892STony Krowiak 			ret = 0;
842a374e892STony Krowiak 			break;
843a374e892STony Krowiak 		default:
844a374e892STony Krowiak 			ret = -ENXIO;
845a374e892STony Krowiak 			break;
846a374e892STony Krowiak 		}
847a374e892STony Krowiak 		break;
848f2061656SDominik Dingel 	default:
849f2061656SDominik Dingel 		ret = -ENXIO;
850f2061656SDominik Dingel 		break;
851f2061656SDominik Dingel 	}
852f2061656SDominik Dingel 
853f2061656SDominik Dingel 	return ret;
854f2061656SDominik Dingel }
855f2061656SDominik Dingel 
85630ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
85730ee2a98SJason J. Herne {
85830ee2a98SJason J. Herne 	uint8_t *keys;
85930ee2a98SJason J. Herne 	uint64_t hva;
86030ee2a98SJason J. Herne 	unsigned long curkey;
86130ee2a98SJason J. Herne 	int i, r = 0;
86230ee2a98SJason J. Herne 
86330ee2a98SJason J. Herne 	if (args->flags != 0)
86430ee2a98SJason J. Herne 		return -EINVAL;
86530ee2a98SJason J. Herne 
86630ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
86730ee2a98SJason J. Herne 	if (!mm_use_skey(current->mm))
86830ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
86930ee2a98SJason J. Herne 
87030ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
87130ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
87230ee2a98SJason J. Herne 		return -EINVAL;
87330ee2a98SJason J. Herne 
87430ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
87530ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
87630ee2a98SJason J. Herne 	if (!keys)
87730ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
87830ee2a98SJason J. Herne 	if (!keys)
87930ee2a98SJason J. Herne 		return -ENOMEM;
88030ee2a98SJason J. Herne 
88130ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
88230ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
88330ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
88430ee2a98SJason J. Herne 			r = -EFAULT;
88530ee2a98SJason J. Herne 			goto out;
88630ee2a98SJason J. Herne 		}
88730ee2a98SJason J. Herne 
88830ee2a98SJason J. Herne 		curkey = get_guest_storage_key(current->mm, hva);
88930ee2a98SJason J. Herne 		if (IS_ERR_VALUE(curkey)) {
89030ee2a98SJason J. Herne 			r = curkey;
89130ee2a98SJason J. Herne 			goto out;
89230ee2a98SJason J. Herne 		}
89330ee2a98SJason J. Herne 		keys[i] = curkey;
89430ee2a98SJason J. Herne 	}
89530ee2a98SJason J. Herne 
89630ee2a98SJason J. Herne 	r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
89730ee2a98SJason J. Herne 			 sizeof(uint8_t) * args->count);
89830ee2a98SJason J. Herne 	if (r)
89930ee2a98SJason J. Herne 		r = -EFAULT;
90030ee2a98SJason J. Herne out:
90130ee2a98SJason J. Herne 	kvfree(keys);
90230ee2a98SJason J. Herne 	return r;
90330ee2a98SJason J. Herne }
90430ee2a98SJason J. Herne 
90530ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
90630ee2a98SJason J. Herne {
90730ee2a98SJason J. Herne 	uint8_t *keys;
90830ee2a98SJason J. Herne 	uint64_t hva;
90930ee2a98SJason J. Herne 	int i, r = 0;
91030ee2a98SJason J. Herne 
91130ee2a98SJason J. Herne 	if (args->flags != 0)
91230ee2a98SJason J. Herne 		return -EINVAL;
91330ee2a98SJason J. Herne 
91430ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
91530ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
91630ee2a98SJason J. Herne 		return -EINVAL;
91730ee2a98SJason J. Herne 
91830ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
91930ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
92030ee2a98SJason J. Herne 	if (!keys)
92130ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
92230ee2a98SJason J. Herne 	if (!keys)
92330ee2a98SJason J. Herne 		return -ENOMEM;
92430ee2a98SJason J. Herne 
92530ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
92630ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
92730ee2a98SJason J. Herne 	if (r) {
92830ee2a98SJason J. Herne 		r = -EFAULT;
92930ee2a98SJason J. Herne 		goto out;
93030ee2a98SJason J. Herne 	}
93130ee2a98SJason J. Herne 
93230ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
93314d4a425SDominik Dingel 	r = s390_enable_skey();
93414d4a425SDominik Dingel 	if (r)
93514d4a425SDominik Dingel 		goto out;
93630ee2a98SJason J. Herne 
93730ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
93830ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
93930ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
94030ee2a98SJason J. Herne 			r = -EFAULT;
94130ee2a98SJason J. Herne 			goto out;
94230ee2a98SJason J. Herne 		}
94330ee2a98SJason J. Herne 
94430ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
94530ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
94630ee2a98SJason J. Herne 			r = -EINVAL;
94730ee2a98SJason J. Herne 			goto out;
94830ee2a98SJason J. Herne 		}
94930ee2a98SJason J. Herne 
95030ee2a98SJason J. Herne 		r = set_guest_storage_key(current->mm, hva,
95130ee2a98SJason J. Herne 					  (unsigned long)keys[i], 0);
95230ee2a98SJason J. Herne 		if (r)
95330ee2a98SJason J. Herne 			goto out;
95430ee2a98SJason J. Herne 	}
95530ee2a98SJason J. Herne out:
95630ee2a98SJason J. Herne 	kvfree(keys);
95730ee2a98SJason J. Herne 	return r;
95830ee2a98SJason J. Herne }
95930ee2a98SJason J. Herne 
960b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
961b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
962b0c632dbSHeiko Carstens {
963b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
964b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
965f2061656SDominik Dingel 	struct kvm_device_attr attr;
966b0c632dbSHeiko Carstens 	int r;
967b0c632dbSHeiko Carstens 
968b0c632dbSHeiko Carstens 	switch (ioctl) {
969ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
970ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
971ba5c1e9bSCarsten Otte 
972ba5c1e9bSCarsten Otte 		r = -EFAULT;
973ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
974ba5c1e9bSCarsten Otte 			break;
975ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
976ba5c1e9bSCarsten Otte 		break;
977ba5c1e9bSCarsten Otte 	}
978d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
979d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
980d938dc55SCornelia Huck 		r = -EFAULT;
981d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
982d938dc55SCornelia Huck 			break;
983d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
984d938dc55SCornelia Huck 		break;
985d938dc55SCornelia Huck 	}
98684223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
98784223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
98884223598SCornelia Huck 
98984223598SCornelia Huck 		r = -EINVAL;
99084223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
99184223598SCornelia Huck 			/* Set up dummy routing. */
99284223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
993152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
99484223598SCornelia Huck 		}
99584223598SCornelia Huck 		break;
99684223598SCornelia Huck 	}
997f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
998f2061656SDominik Dingel 		r = -EFAULT;
999f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1000f2061656SDominik Dingel 			break;
1001f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
1002f2061656SDominik Dingel 		break;
1003f2061656SDominik Dingel 	}
1004f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
1005f2061656SDominik Dingel 		r = -EFAULT;
1006f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1007f2061656SDominik Dingel 			break;
1008f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
1009f2061656SDominik Dingel 		break;
1010f2061656SDominik Dingel 	}
1011f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
1012f2061656SDominik Dingel 		r = -EFAULT;
1013f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1014f2061656SDominik Dingel 			break;
1015f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
1016f2061656SDominik Dingel 		break;
1017f2061656SDominik Dingel 	}
101830ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
101930ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
102030ee2a98SJason J. Herne 
102130ee2a98SJason J. Herne 		r = -EFAULT;
102230ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
102330ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
102430ee2a98SJason J. Herne 			break;
102530ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
102630ee2a98SJason J. Herne 		break;
102730ee2a98SJason J. Herne 	}
102830ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
102930ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
103030ee2a98SJason J. Herne 
103130ee2a98SJason J. Herne 		r = -EFAULT;
103230ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
103330ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
103430ee2a98SJason J. Herne 			break;
103530ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
103630ee2a98SJason J. Herne 		break;
103730ee2a98SJason J. Herne 	}
1038b0c632dbSHeiko Carstens 	default:
1039367e1319SAvi Kivity 		r = -ENOTTY;
1040b0c632dbSHeiko Carstens 	}
1041b0c632dbSHeiko Carstens 
1042b0c632dbSHeiko Carstens 	return r;
1043b0c632dbSHeiko Carstens }
1044b0c632dbSHeiko Carstens 
104545c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
104645c9b47cSTony Krowiak {
104745c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
104886044c8cSChristian Borntraeger 	u32 cc = 0;
104945c9b47cSTony Krowiak 
105086044c8cSChristian Borntraeger 	memset(config, 0, 128);
105145c9b47cSTony Krowiak 	asm volatile(
105245c9b47cSTony Krowiak 		"lgr 0,%1\n"
105345c9b47cSTony Krowiak 		"lgr 2,%2\n"
105445c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
105586044c8cSChristian Borntraeger 		"0: ipm %0\n"
105645c9b47cSTony Krowiak 		"srl %0,28\n"
105786044c8cSChristian Borntraeger 		"1:\n"
105886044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
105986044c8cSChristian Borntraeger 		: "+r" (cc)
106045c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
106145c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
106245c9b47cSTony Krowiak 	);
106345c9b47cSTony Krowiak 
106445c9b47cSTony Krowiak 	return cc;
106545c9b47cSTony Krowiak }
106645c9b47cSTony Krowiak 
106745c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
106845c9b47cSTony Krowiak {
106945c9b47cSTony Krowiak 	u8 config[128];
107045c9b47cSTony Krowiak 	int cc;
107145c9b47cSTony Krowiak 
1072a6aacc3fSHeiko Carstens 	if (test_facility(12)) {
107345c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
107445c9b47cSTony Krowiak 
107545c9b47cSTony Krowiak 		if (cc)
107645c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
107745c9b47cSTony Krowiak 		else
107845c9b47cSTony Krowiak 			return config[0] & 0x40;
107945c9b47cSTony Krowiak 	}
108045c9b47cSTony Krowiak 
108145c9b47cSTony Krowiak 	return 0;
108245c9b47cSTony Krowiak }
108345c9b47cSTony Krowiak 
108445c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
108545c9b47cSTony Krowiak {
108645c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
108745c9b47cSTony Krowiak 
108845c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
108945c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
109045c9b47cSTony Krowiak 	else
109145c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
109245c9b47cSTony Krowiak }
109345c9b47cSTony Krowiak 
10949bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
10959d8d5786SMichael Mueller {
10969bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
10979bb0ec09SDavid Hildenbrand 
10989bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
10999bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
11009bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
11019d8d5786SMichael Mueller }
11029d8d5786SMichael Mueller 
1103c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
11045102ee87STony Krowiak {
11059d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
1106c54f0d6aSDavid Hildenbrand 		return;
11075102ee87STony Krowiak 
1108c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
110945c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
11105102ee87STony Krowiak 
1111ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1112ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1113ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1114ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1115ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1116ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1117ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
11185102ee87STony Krowiak }
11195102ee87STony Krowiak 
11207d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
11217d43bafcSEugene (jno) Dvurechenski {
11227d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
11235e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
11247d43bafcSEugene (jno) Dvurechenski 	else
11257d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
11267d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
11277d43bafcSEugene (jno) Dvurechenski }
11287d43bafcSEugene (jno) Dvurechenski 
1129e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1130b0c632dbSHeiko Carstens {
11319d8d5786SMichael Mueller 	int i, rc;
1132b0c632dbSHeiko Carstens 	char debug_name[16];
1133f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1134b0c632dbSHeiko Carstens 
1135e08b9637SCarsten Otte 	rc = -EINVAL;
1136e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1137e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1138e08b9637SCarsten Otte 		goto out_err;
1139e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1140e08b9637SCarsten Otte 		goto out_err;
1141e08b9637SCarsten Otte #else
1142e08b9637SCarsten Otte 	if (type)
1143e08b9637SCarsten Otte 		goto out_err;
1144e08b9637SCarsten Otte #endif
1145e08b9637SCarsten Otte 
1146b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1147b0c632dbSHeiko Carstens 	if (rc)
1148d89f5effSJan Kiszka 		goto out_err;
1149b0c632dbSHeiko Carstens 
1150b290411aSCarsten Otte 	rc = -ENOMEM;
1151b290411aSCarsten Otte 
11527d43bafcSEugene (jno) Dvurechenski 	kvm->arch.use_esca = 0; /* start with basic SCA */
11535e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
1154bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(GFP_KERNEL);
1155b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1156d89f5effSJan Kiszka 		goto out_err;
1157f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
1158c5c2c393SDavid Hildenbrand 	sca_offset += 16;
1159bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
1160c5c2c393SDavid Hildenbrand 		sca_offset = 0;
1161bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
1162bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
1163f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
1164b0c632dbSHeiko Carstens 
1165b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
1166b0c632dbSHeiko Carstens 
11671cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
1168b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
116940f5b735SDominik Dingel 		goto out_err;
1170b0c632dbSHeiko Carstens 
1171c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
1172c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1173c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
117440f5b735SDominik Dingel 		goto out_err;
11759d8d5786SMichael Mueller 
1176fb5bf93fSMichael Mueller 	/* Populate the facility mask initially. */
1177c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
117894422ee8SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
11799d8d5786SMichael Mueller 	for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
11809d8d5786SMichael Mueller 		if (i < kvm_s390_fac_list_mask_size())
1181c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
11829d8d5786SMichael Mueller 		else
1183c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] = 0UL;
11849d8d5786SMichael Mueller 	}
11859d8d5786SMichael Mueller 
1186981467c9SMichael Mueller 	/* Populate the facility list initially. */
1187c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1188c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
1189981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1190981467c9SMichael Mueller 
11919bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
119237c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
11939d8d5786SMichael Mueller 
1194c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
11955102ee87STony Krowiak 
1196ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
11976d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
11986d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
11998a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
1200a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
1201ba5c1e9bSCarsten Otte 
1202b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
120378f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
1204b0c632dbSHeiko Carstens 
1205e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
1206e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
1207a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
1208e08b9637SCarsten Otte 	} else {
120932e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
1210a3a92c31SDominik Dingel 			kvm->arch.mem_limit = TASK_MAX_SIZE;
121132e6b236SGuenther Hutzl 		else
121232e6b236SGuenther Hutzl 			kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE,
121332e6b236SGuenther Hutzl 						    sclp.hamax + 1);
1214a3a92c31SDominik Dingel 		kvm->arch.gmap = gmap_alloc(current->mm, kvm->arch.mem_limit - 1);
1215598841caSCarsten Otte 		if (!kvm->arch.gmap)
121640f5b735SDominik Dingel 			goto out_err;
12172c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
121824eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
1219e08b9637SCarsten Otte 	}
1220fa6b7fe9SCornelia Huck 
1221fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
122284223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
122372f25020SJason J. Herne 	kvm->arch.epoch = 0;
1224fa6b7fe9SCornelia Huck 
12258ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
12268335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
12278ad35755SDavid Hildenbrand 
1228d89f5effSJan Kiszka 	return 0;
1229d89f5effSJan Kiszka out_err:
1230c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
123140f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
12327d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
123378f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
1234d89f5effSJan Kiszka 	return rc;
1235b0c632dbSHeiko Carstens }
1236b0c632dbSHeiko Carstens 
1237d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1238d329c035SChristian Borntraeger {
1239d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
1240ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
124167335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
12423c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
1243bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
1244a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
124527e0393fSCarsten Otte 
124627e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
124727e0393fSCarsten Otte 		gmap_free(vcpu->arch.gmap);
124827e0393fSCarsten Otte 
1249e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
1250b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
1251d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
1252b31288faSKonstantin Weitz 
12536692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
1254b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
1255d329c035SChristian Borntraeger }
1256d329c035SChristian Borntraeger 
1257d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
1258d329c035SChristian Borntraeger {
1259d329c035SChristian Borntraeger 	unsigned int i;
1260988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
1261d329c035SChristian Borntraeger 
1262988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
1263988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
1264988a2caeSGleb Natapov 
1265988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
1266988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1267d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
1268988a2caeSGleb Natapov 
1269988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
1270988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
1271d329c035SChristian Borntraeger }
1272d329c035SChristian Borntraeger 
1273b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
1274b0c632dbSHeiko Carstens {
1275d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
12767d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
1277d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
1278c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
127927e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
1280598841caSCarsten Otte 		gmap_free(kvm->arch.gmap);
1281841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
128267335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
12838335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
1284b0c632dbSHeiko Carstens }
1285b0c632dbSHeiko Carstens 
1286b0c632dbSHeiko Carstens /* Section: vcpu related */
1287dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1288b0c632dbSHeiko Carstens {
1289c6c956b8SMartin Schwidefsky 	vcpu->arch.gmap = gmap_alloc(current->mm, -1UL);
129027e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
129127e0393fSCarsten Otte 		return -ENOMEM;
12922c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
1293dafd032aSDominik Dingel 
129427e0393fSCarsten Otte 	return 0;
129527e0393fSCarsten Otte }
129627e0393fSCarsten Otte 
1297a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1298a6e2f683SEugene (jno) Dvurechenski {
12995e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
13007d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
13017d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
13027d43bafcSEugene (jno) Dvurechenski 
13037d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
13047d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
13057d43bafcSEugene (jno) Dvurechenski 	} else {
1306bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1307a6e2f683SEugene (jno) Dvurechenski 
1308a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1309a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
1310a6e2f683SEugene (jno) Dvurechenski 	}
13115e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
13127d43bafcSEugene (jno) Dvurechenski }
1313a6e2f683SEugene (jno) Dvurechenski 
1314eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
1315a6e2f683SEugene (jno) Dvurechenski {
1316eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
1317eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
1318eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
13197d43bafcSEugene (jno) Dvurechenski 
1320eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
13217d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
13227d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
132325508824SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= 0x04U;
1324eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
13257d43bafcSEugene (jno) Dvurechenski 	} else {
1326eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1327a6e2f683SEugene (jno) Dvurechenski 
1328eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
1329a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1330a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1331eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1332a6e2f683SEugene (jno) Dvurechenski 	}
1333eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
13345e044315SEugene (jno) Dvurechenski }
13355e044315SEugene (jno) Dvurechenski 
13365e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
13375e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
13385e044315SEugene (jno) Dvurechenski {
13395e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
13405e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
13415e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
13425e044315SEugene (jno) Dvurechenski }
13435e044315SEugene (jno) Dvurechenski 
13445e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
13455e044315SEugene (jno) Dvurechenski {
13465e044315SEugene (jno) Dvurechenski 	int i;
13475e044315SEugene (jno) Dvurechenski 
13485e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
13495e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
13505e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
13515e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
13525e044315SEugene (jno) Dvurechenski }
13535e044315SEugene (jno) Dvurechenski 
13545e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
13555e044315SEugene (jno) Dvurechenski {
13565e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
13575e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
13585e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
13595e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
13605e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
13615e044315SEugene (jno) Dvurechenski 
13625e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
13635e044315SEugene (jno) Dvurechenski 	if (!new_sca)
13645e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
13655e044315SEugene (jno) Dvurechenski 
13665e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
13675e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
13685e044315SEugene (jno) Dvurechenski 
13695e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
13705e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
13715e044315SEugene (jno) Dvurechenski 
13725e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
13735e044315SEugene (jno) Dvurechenski 
13745e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
13755e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
13765e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
13775e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->ecb2 |= 0x04U;
13785e044315SEugene (jno) Dvurechenski 	}
13795e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
13805e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
13815e044315SEugene (jno) Dvurechenski 
13825e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
13835e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
13845e044315SEugene (jno) Dvurechenski 
13855e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
13865e044315SEugene (jno) Dvurechenski 
13878335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
13888335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
13895e044315SEugene (jno) Dvurechenski 	return 0;
13907d43bafcSEugene (jno) Dvurechenski }
1391a6e2f683SEugene (jno) Dvurechenski 
1392a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1393a6e2f683SEugene (jno) Dvurechenski {
13945e044315SEugene (jno) Dvurechenski 	int rc;
13955e044315SEugene (jno) Dvurechenski 
13965e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
13975e044315SEugene (jno) Dvurechenski 		return true;
13985e044315SEugene (jno) Dvurechenski 	if (!sclp.has_esca)
13995e044315SEugene (jno) Dvurechenski 		return false;
14005e044315SEugene (jno) Dvurechenski 
14015e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
14025e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
14035e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
14045e044315SEugene (jno) Dvurechenski 
14055e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
1406a6e2f683SEugene (jno) Dvurechenski }
1407a6e2f683SEugene (jno) Dvurechenski 
1408dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1409dafd032aSDominik Dingel {
1410dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1411dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
141259674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
141359674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
14149eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
1415b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
1416b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
1417b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
1418c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
1419c6e5f166SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
1420f6aa6dc4SDavid Hildenbrand 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
1421f6aa6dc4SDavid Hildenbrand 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
1422f6aa6dc4SDavid Hildenbrand 	 */
1423f6aa6dc4SDavid Hildenbrand 	if (MACHINE_HAS_VX)
142468c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
14256fd8e67dSDavid Hildenbrand 	else
14266fd8e67dSDavid Hildenbrand 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
1427dafd032aSDominik Dingel 
1428dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
1429dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
1430dafd032aSDominik Dingel 
1431b0c632dbSHeiko Carstens 	return 0;
1432b0c632dbSHeiko Carstens }
1433b0c632dbSHeiko Carstens 
1434db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1435db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1436db0758b2SDavid Hildenbrand {
1437db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
14389c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1439db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
14409c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1441db0758b2SDavid Hildenbrand }
1442db0758b2SDavid Hildenbrand 
1443db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1444db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1445db0758b2SDavid Hildenbrand {
1446db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
14479c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1448db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1449db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
14509c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1451db0758b2SDavid Hildenbrand }
1452db0758b2SDavid Hildenbrand 
1453db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1454db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1455db0758b2SDavid Hildenbrand {
1456db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
1457db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
1458db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
1459db0758b2SDavid Hildenbrand }
1460db0758b2SDavid Hildenbrand 
1461db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1462db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1463db0758b2SDavid Hildenbrand {
1464db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
1465db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
1466db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
1467db0758b2SDavid Hildenbrand }
1468db0758b2SDavid Hildenbrand 
1469db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1470db0758b2SDavid Hildenbrand {
1471db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1472db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
1473db0758b2SDavid Hildenbrand 	preempt_enable();
1474db0758b2SDavid Hildenbrand }
1475db0758b2SDavid Hildenbrand 
1476db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1477db0758b2SDavid Hildenbrand {
1478db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1479db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
1480db0758b2SDavid Hildenbrand 	preempt_enable();
1481db0758b2SDavid Hildenbrand }
1482db0758b2SDavid Hildenbrand 
14834287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
14844287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
14854287f247SDavid Hildenbrand {
1486db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
14879c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1488db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
1489db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
14904287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
14919c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1492db0758b2SDavid Hildenbrand 	preempt_enable();
14934287f247SDavid Hildenbrand }
14944287f247SDavid Hildenbrand 
1495db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
14964287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
14974287f247SDavid Hildenbrand {
14989c23a131SDavid Hildenbrand 	unsigned int seq;
1499db0758b2SDavid Hildenbrand 	__u64 value;
1500db0758b2SDavid Hildenbrand 
1501db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
15024287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
1503db0758b2SDavid Hildenbrand 
15049c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
15059c23a131SDavid Hildenbrand 	do {
15069c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
15079c23a131SDavid Hildenbrand 		/*
15089c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
15099c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
15109c23a131SDavid Hildenbrand 		 */
15119c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
1512db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
15139c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
15149c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
1515db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
15169c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
15179c23a131SDavid Hildenbrand 	preempt_enable();
1518db0758b2SDavid Hildenbrand 	return value;
15194287f247SDavid Hildenbrand }
15204287f247SDavid Hildenbrand 
1521b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1522b0c632dbSHeiko Carstens {
15239977e886SHendrik Brueckner 	/* Save host register state */
1524d0164ee2SHendrik Brueckner 	save_fpu_regs();
15259abc2a08SDavid Hildenbrand 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
15269abc2a08SDavid Hildenbrand 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
152796b2d7a8SHendrik Brueckner 
15286fd8e67dSDavid Hildenbrand 	if (MACHINE_HAS_VX)
15299abc2a08SDavid Hildenbrand 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
15306fd8e67dSDavid Hildenbrand 	else
15316fd8e67dSDavid Hildenbrand 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
15329abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
15339977e886SHendrik Brueckner 	if (test_fp_ctl(current->thread.fpu.fpc))
153496b2d7a8SHendrik Brueckner 		/* User space provided an invalid FPC, let's clear it */
15359977e886SHendrik Brueckner 		current->thread.fpu.fpc = 0;
15369977e886SHendrik Brueckner 
15379977e886SHendrik Brueckner 	save_access_regs(vcpu->arch.host_acrs);
153859674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
1539480e5926SChristian Borntraeger 	gmap_enable(vcpu->arch.gmap);
1540805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
15415ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
1542db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
154301a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
1544b0c632dbSHeiko Carstens }
1545b0c632dbSHeiko Carstens 
1546b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1547b0c632dbSHeiko Carstens {
154801a745acSDavid Hildenbrand 	vcpu->cpu = -1;
15495ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
1550db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
1551805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
1552480e5926SChristian Borntraeger 	gmap_disable(vcpu->arch.gmap);
15539977e886SHendrik Brueckner 
15549abc2a08SDavid Hildenbrand 	/* Save guest register state */
1555d0164ee2SHendrik Brueckner 	save_fpu_regs();
15569977e886SHendrik Brueckner 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
15579abc2a08SDavid Hildenbrand 
15589abc2a08SDavid Hildenbrand 	/* Restore host register state */
15599abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
15609abc2a08SDavid Hildenbrand 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
15619977e886SHendrik Brueckner 
15629977e886SHendrik Brueckner 	save_access_regs(vcpu->run->s.regs.acrs);
1563b0c632dbSHeiko Carstens 	restore_access_regs(vcpu->arch.host_acrs);
1564b0c632dbSHeiko Carstens }
1565b0c632dbSHeiko Carstens 
1566b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1567b0c632dbSHeiko Carstens {
1568b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
1569b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
1570b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
15718d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
15724287f247SDavid Hildenbrand 	kvm_s390_set_cpu_timer(vcpu, 0);
1573b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
1574b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
1575b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1576b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
1577b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
15789abc2a08SDavid Hildenbrand 	/* make sure the new fpc will be lazily loaded */
15799abc2a08SDavid Hildenbrand 	save_fpu_regs();
15809abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = 0;
1581b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
1582672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
15833c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
15843c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
15856352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
15866852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
15872ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
1588b0c632dbSHeiko Carstens }
1589b0c632dbSHeiko Carstens 
159031928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
159142897d86SMarcelo Tosatti {
159272f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
1593fdf03650SFan Zhang 	preempt_disable();
159472f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1595fdf03650SFan Zhang 	preempt_enable();
159672f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
159725508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
1598dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
1599eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
160025508824SDavid Hildenbrand 	}
160125508824SDavid Hildenbrand 
160242897d86SMarcelo Tosatti }
160342897d86SMarcelo Tosatti 
16045102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
16055102ee87STony Krowiak {
16069d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
16075102ee87STony Krowiak 		return;
16085102ee87STony Krowiak 
1609a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1610a374e892STony Krowiak 
1611a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
1612a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1613a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
1614a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1615a374e892STony Krowiak 
16165102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
16175102ee87STony Krowiak }
16185102ee87STony Krowiak 
1619b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1620b31605c1SDominik Dingel {
1621b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
1622b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
1623b31605c1SDominik Dingel }
1624b31605c1SDominik Dingel 
1625b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1626b31605c1SDominik Dingel {
1627b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1628b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
1629b31605c1SDominik Dingel 		return -ENOMEM;
1630b31605c1SDominik Dingel 
1631b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 |= 0x80;
1632b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 &= ~0x08;
1633b31605c1SDominik Dingel 	return 0;
1634b31605c1SDominik Dingel }
1635b31605c1SDominik Dingel 
163691520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
163791520f1aSMichael Mueller {
163891520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
163991520f1aSMichael Mueller 
164091520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
164180bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
1642c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
164391520f1aSMichael Mueller }
164491520f1aSMichael Mueller 
1645b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1646b0c632dbSHeiko Carstens {
1647b31605c1SDominik Dingel 	int rc = 0;
1648b31288faSKonstantin Weitz 
16499e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
16509e6dabefSCornelia Huck 						    CPUSTAT_SM |
1651a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
1652a4a4f191SGuenther Hutzl 
165353df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
1654805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
165553df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
1656805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
1657a4a4f191SGuenther Hutzl 
165891520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
165991520f1aSMichael Mueller 
1660bd50e8ecSDavid Hildenbrand 	vcpu->arch.sie_block->ecb = 0x02;
1661bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
1662bd50e8ecSDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= 0x04;
16639d8d5786SMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 50) && test_kvm_facility(vcpu->kvm, 73))
16647feb6bb8SMichael Mueller 		vcpu->arch.sie_block->ecb |= 0x10;
16657feb6bb8SMichael Mueller 
1666d6af0b49SDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 8))
1667d6af0b49SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= 0x08;
1668ea5f4969SDavid Hildenbrand 	vcpu->arch.sie_block->eca   = 0xC1002000U;
166937c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
1670217a4406SHeiko Carstens 		vcpu->arch.sie_block->eca |= 1;
167137c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
1672ea5f4969SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x10000000U;
1673c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
1674c6e5f166SFan Zhang 		vcpu->arch.sie_block->ecb3 |= 0x01;
167518280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
167613211ea7SEric Farman 		vcpu->arch.sie_block->eca |= 0x00020000;
167713211ea7SEric Farman 		vcpu->arch.sie_block->ecd |= 0x20000000;
167813211ea7SEric Farman 	}
1679c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
1680492d8642SThomas Huth 	vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
16815a5e6536SMatthew Rosato 
1682e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
1683b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
1684b31605c1SDominik Dingel 		if (rc)
1685b31605c1SDominik Dingel 			return rc;
1686b31288faSKonstantin Weitz 	}
16870ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1688ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
16899d8d5786SMichael Mueller 
16905102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
16915102ee87STony Krowiak 
1692b31605c1SDominik Dingel 	return rc;
1693b0c632dbSHeiko Carstens }
1694b0c632dbSHeiko Carstens 
1695b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1696b0c632dbSHeiko Carstens 				      unsigned int id)
1697b0c632dbSHeiko Carstens {
16984d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
16997feb6bb8SMichael Mueller 	struct sie_page *sie_page;
17004d47555aSCarsten Otte 	int rc = -EINVAL;
1701b0c632dbSHeiko Carstens 
17024215825eSDavid Hildenbrand 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
17034d47555aSCarsten Otte 		goto out;
17044d47555aSCarsten Otte 
17054d47555aSCarsten Otte 	rc = -ENOMEM;
17064d47555aSCarsten Otte 
1707b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1708b0c632dbSHeiko Carstens 	if (!vcpu)
17094d47555aSCarsten Otte 		goto out;
1710b0c632dbSHeiko Carstens 
17117feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
17127feb6bb8SMichael Mueller 	if (!sie_page)
1713b0c632dbSHeiko Carstens 		goto out_free_cpu;
1714b0c632dbSHeiko Carstens 
17157feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
17167feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
17177feb6bb8SMichael Mueller 
1718b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
1719ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
1720ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
1721d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
17225288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
17239c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
1724ba5c1e9bSCarsten Otte 
1725b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
1726b0c632dbSHeiko Carstens 	if (rc)
17279abc2a08SDavid Hildenbrand 		goto out_free_sie_block;
17288335713aSChristian Borntraeger 	VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
1729b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
1730ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
1731b0c632dbSHeiko Carstens 
1732b0c632dbSHeiko Carstens 	return vcpu;
17337b06bf2fSWei Yongjun out_free_sie_block:
17347b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
1735b0c632dbSHeiko Carstens out_free_cpu:
1736b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
17374d47555aSCarsten Otte out:
1738b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
1739b0c632dbSHeiko Carstens }
1740b0c632dbSHeiko Carstens 
1741b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
1742b0c632dbSHeiko Carstens {
17439a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
1744b0c632dbSHeiko Carstens }
1745b0c632dbSHeiko Carstens 
174627406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
174749b99e1eSChristian Borntraeger {
1748805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
174961a6df54SDavid Hildenbrand 	exit_sie(vcpu);
175049b99e1eSChristian Borntraeger }
175149b99e1eSChristian Borntraeger 
175227406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
175349b99e1eSChristian Borntraeger {
1754805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
175549b99e1eSChristian Borntraeger }
175649b99e1eSChristian Borntraeger 
17578e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
17588e236546SChristian Borntraeger {
1759805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
176061a6df54SDavid Hildenbrand 	exit_sie(vcpu);
17618e236546SChristian Borntraeger }
17628e236546SChristian Borntraeger 
17638e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
17648e236546SChristian Borntraeger {
17659bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
17668e236546SChristian Borntraeger }
17678e236546SChristian Borntraeger 
176849b99e1eSChristian Borntraeger /*
176949b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
177049b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
177149b99e1eSChristian Borntraeger  * return immediately. */
177249b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
177349b99e1eSChristian Borntraeger {
1774805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
177549b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
177649b99e1eSChristian Borntraeger 		cpu_relax();
177749b99e1eSChristian Borntraeger }
177849b99e1eSChristian Borntraeger 
17798e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
17808e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
178149b99e1eSChristian Borntraeger {
17828e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
17838e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
178449b99e1eSChristian Borntraeger }
178549b99e1eSChristian Borntraeger 
17862c70fe44SChristian Borntraeger static void kvm_gmap_notifier(struct gmap *gmap, unsigned long address)
17872c70fe44SChristian Borntraeger {
17882c70fe44SChristian Borntraeger 	int i;
17892c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
17902c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
17912c70fe44SChristian Borntraeger 
17922c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
17932c70fe44SChristian Borntraeger 		/* match against both prefix pages */
1794fda902cbSMichael Mueller 		if (kvm_s390_get_prefix(vcpu) == (address & ~0x1000UL)) {
17952c70fe44SChristian Borntraeger 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx", address);
17968e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
17972c70fe44SChristian Borntraeger 		}
17982c70fe44SChristian Borntraeger 	}
17992c70fe44SChristian Borntraeger }
18002c70fe44SChristian Borntraeger 
1801b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
1802b6d33834SChristoffer Dall {
1803b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
1804b6d33834SChristoffer Dall 	BUG();
1805b6d33834SChristoffer Dall 	return 0;
1806b6d33834SChristoffer Dall }
1807b6d33834SChristoffer Dall 
180814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
180914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
181014eebd91SCarsten Otte {
181114eebd91SCarsten Otte 	int r = -EINVAL;
181214eebd91SCarsten Otte 
181314eebd91SCarsten Otte 	switch (reg->id) {
181429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
181529b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
181629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
181729b7c71bSCarsten Otte 		break;
181829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
181929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
182029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
182129b7c71bSCarsten Otte 		break;
182246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
18234287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
182446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
182546a6dd1cSJason J. herne 		break;
182646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
182746a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
182846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
182946a6dd1cSJason J. herne 		break;
1830536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
1831536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
1832536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1833536336c2SDominik Dingel 		break;
1834536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
1835536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
1836536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1837536336c2SDominik Dingel 		break;
1838536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
1839536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
1840536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1841536336c2SDominik Dingel 		break;
1842672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
1843672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
1844672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
1845672550fbSChristian Borntraeger 		break;
1846afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
1847afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
1848afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
1849afa45ff5SChristian Borntraeger 		break;
185014eebd91SCarsten Otte 	default:
185114eebd91SCarsten Otte 		break;
185214eebd91SCarsten Otte 	}
185314eebd91SCarsten Otte 
185414eebd91SCarsten Otte 	return r;
185514eebd91SCarsten Otte }
185614eebd91SCarsten Otte 
185714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
185814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
185914eebd91SCarsten Otte {
186014eebd91SCarsten Otte 	int r = -EINVAL;
18614287f247SDavid Hildenbrand 	__u64 val;
186214eebd91SCarsten Otte 
186314eebd91SCarsten Otte 	switch (reg->id) {
186429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
186529b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
186629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
186729b7c71bSCarsten Otte 		break;
186829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
186929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
187029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
187129b7c71bSCarsten Otte 		break;
187246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
18734287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
18744287f247SDavid Hildenbrand 		if (!r)
18754287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
187646a6dd1cSJason J. herne 		break;
187746a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
187846a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
187946a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
188046a6dd1cSJason J. herne 		break;
1881536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
1882536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
1883536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
18849fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
18859fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
1886536336c2SDominik Dingel 		break;
1887536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
1888536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
1889536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1890536336c2SDominik Dingel 		break;
1891536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
1892536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
1893536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
1894536336c2SDominik Dingel 		break;
1895672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
1896672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
1897672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
1898672550fbSChristian Borntraeger 		break;
1899afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
1900afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
1901afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
1902afa45ff5SChristian Borntraeger 		break;
190314eebd91SCarsten Otte 	default:
190414eebd91SCarsten Otte 		break;
190514eebd91SCarsten Otte 	}
190614eebd91SCarsten Otte 
190714eebd91SCarsten Otte 	return r;
190814eebd91SCarsten Otte }
1909b6d33834SChristoffer Dall 
1910b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
1911b0c632dbSHeiko Carstens {
1912b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
1913b0c632dbSHeiko Carstens 	return 0;
1914b0c632dbSHeiko Carstens }
1915b0c632dbSHeiko Carstens 
1916b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1917b0c632dbSHeiko Carstens {
19185a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
1919b0c632dbSHeiko Carstens 	return 0;
1920b0c632dbSHeiko Carstens }
1921b0c632dbSHeiko Carstens 
1922b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1923b0c632dbSHeiko Carstens {
19245a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
1925b0c632dbSHeiko Carstens 	return 0;
1926b0c632dbSHeiko Carstens }
1927b0c632dbSHeiko Carstens 
1928b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1929b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
1930b0c632dbSHeiko Carstens {
193159674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
1932b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
193359674c1aSChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
1934b0c632dbSHeiko Carstens 	return 0;
1935b0c632dbSHeiko Carstens }
1936b0c632dbSHeiko Carstens 
1937b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1938b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
1939b0c632dbSHeiko Carstens {
194059674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
1941b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
1942b0c632dbSHeiko Carstens 	return 0;
1943b0c632dbSHeiko Carstens }
1944b0c632dbSHeiko Carstens 
1945b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1946b0c632dbSHeiko Carstens {
19479abc2a08SDavid Hildenbrand 	/* make sure the new values will be lazily loaded */
19489abc2a08SDavid Hildenbrand 	save_fpu_regs();
19494725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
19504725c860SMartin Schwidefsky 		return -EINVAL;
19519abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = fpu->fpc;
19529abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
19539abc2a08SDavid Hildenbrand 		convert_fp_to_vx(current->thread.fpu.vxrs, (freg_t *)fpu->fprs);
19549abc2a08SDavid Hildenbrand 	else
19559abc2a08SDavid Hildenbrand 		memcpy(current->thread.fpu.fprs, &fpu->fprs, sizeof(fpu->fprs));
1956b0c632dbSHeiko Carstens 	return 0;
1957b0c632dbSHeiko Carstens }
1958b0c632dbSHeiko Carstens 
1959b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1960b0c632dbSHeiko Carstens {
19619abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
19629abc2a08SDavid Hildenbrand 	save_fpu_regs();
19639abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
19649abc2a08SDavid Hildenbrand 		convert_vx_to_fp((freg_t *)fpu->fprs, current->thread.fpu.vxrs);
19659abc2a08SDavid Hildenbrand 	else
19669abc2a08SDavid Hildenbrand 		memcpy(fpu->fprs, current->thread.fpu.fprs, sizeof(fpu->fprs));
19679abc2a08SDavid Hildenbrand 	fpu->fpc = current->thread.fpu.fpc;
1968b0c632dbSHeiko Carstens 	return 0;
1969b0c632dbSHeiko Carstens }
1970b0c632dbSHeiko Carstens 
1971b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
1972b0c632dbSHeiko Carstens {
1973b0c632dbSHeiko Carstens 	int rc = 0;
1974b0c632dbSHeiko Carstens 
19757a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
1976b0c632dbSHeiko Carstens 		rc = -EBUSY;
1977d7b0b5ebSCarsten Otte 	else {
1978d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
1979d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
1980d7b0b5ebSCarsten Otte 	}
1981b0c632dbSHeiko Carstens 	return rc;
1982b0c632dbSHeiko Carstens }
1983b0c632dbSHeiko Carstens 
1984b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1985b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
1986b0c632dbSHeiko Carstens {
1987b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
1988b0c632dbSHeiko Carstens }
1989b0c632dbSHeiko Carstens 
199027291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
199127291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
199227291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
199327291e21SDavid Hildenbrand 
1994d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1995d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
1996b0c632dbSHeiko Carstens {
199727291e21SDavid Hildenbrand 	int rc = 0;
199827291e21SDavid Hildenbrand 
199927291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
200027291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
200127291e21SDavid Hildenbrand 
20022de3bfc2SDavid Hildenbrand 	if (dbg->control & ~VALID_GUESTDBG_FLAGS)
200327291e21SDavid Hildenbrand 		return -EINVAL;
200427291e21SDavid Hildenbrand 
200527291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
200627291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
200727291e21SDavid Hildenbrand 		/* enforce guest PER */
2008805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
200927291e21SDavid Hildenbrand 
201027291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
201127291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
201227291e21SDavid Hildenbrand 	} else {
2013805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
201427291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
201527291e21SDavid Hildenbrand 	}
201627291e21SDavid Hildenbrand 
201727291e21SDavid Hildenbrand 	if (rc) {
201827291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
201927291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
2020805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
202127291e21SDavid Hildenbrand 	}
202227291e21SDavid Hildenbrand 
202327291e21SDavid Hildenbrand 	return rc;
2024b0c632dbSHeiko Carstens }
2025b0c632dbSHeiko Carstens 
202662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
202762d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
202862d9f0dbSMarcelo Tosatti {
20296352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
20306352e4d2SDavid Hildenbrand 	return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
20316352e4d2SDavid Hildenbrand 				       KVM_MP_STATE_OPERATING;
203262d9f0dbSMarcelo Tosatti }
203362d9f0dbSMarcelo Tosatti 
203462d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
203562d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
203662d9f0dbSMarcelo Tosatti {
20376352e4d2SDavid Hildenbrand 	int rc = 0;
20386352e4d2SDavid Hildenbrand 
20396352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
20406352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
20416352e4d2SDavid Hildenbrand 
20426352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
20436352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
20446352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
20456352e4d2SDavid Hildenbrand 		break;
20466352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
20476352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
20486352e4d2SDavid Hildenbrand 		break;
20496352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
20506352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
20516352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
20526352e4d2SDavid Hildenbrand 	default:
20536352e4d2SDavid Hildenbrand 		rc = -ENXIO;
20546352e4d2SDavid Hildenbrand 	}
20556352e4d2SDavid Hildenbrand 
20566352e4d2SDavid Hildenbrand 	return rc;
205762d9f0dbSMarcelo Tosatti }
205862d9f0dbSMarcelo Tosatti 
20598ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
20608ad35755SDavid Hildenbrand {
20618ad35755SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
20628ad35755SDavid Hildenbrand }
20638ad35755SDavid Hildenbrand 
20642c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
20652c70fe44SChristian Borntraeger {
20668ad35755SDavid Hildenbrand retry:
20678e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
2068586b7ccdSChristian Borntraeger 	if (!vcpu->requests)
2069586b7ccdSChristian Borntraeger 		return 0;
20702c70fe44SChristian Borntraeger 	/*
20712c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
20722c70fe44SChristian Borntraeger 	 * guest prefix page. gmap_ipte_notify will wait on the ptl lock.
20732c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
20742c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
20752c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
20762c70fe44SChristian Borntraeger 	 */
20778ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
20782c70fe44SChristian Borntraeger 		int rc;
20792c70fe44SChristian Borntraeger 		rc = gmap_ipte_notify(vcpu->arch.gmap,
2080fda902cbSMichael Mueller 				      kvm_s390_get_prefix(vcpu),
20812c70fe44SChristian Borntraeger 				      PAGE_SIZE * 2);
20822c70fe44SChristian Borntraeger 		if (rc)
20832c70fe44SChristian Borntraeger 			return rc;
20848ad35755SDavid Hildenbrand 		goto retry;
20852c70fe44SChristian Borntraeger 	}
20868ad35755SDavid Hildenbrand 
2087d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2088d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
2089d3d692c8SDavid Hildenbrand 		goto retry;
2090d3d692c8SDavid Hildenbrand 	}
2091d3d692c8SDavid Hildenbrand 
20928ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
20938ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
20948ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
2095805de8f4SPeter Zijlstra 			atomic_or(CPUSTAT_IBS,
20968ad35755SDavid Hildenbrand 					&vcpu->arch.sie_block->cpuflags);
20978ad35755SDavid Hildenbrand 		}
20988ad35755SDavid Hildenbrand 		goto retry;
20998ad35755SDavid Hildenbrand 	}
21008ad35755SDavid Hildenbrand 
21018ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
21028ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
21038ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
2104805de8f4SPeter Zijlstra 			atomic_andnot(CPUSTAT_IBS,
21058ad35755SDavid Hildenbrand 					  &vcpu->arch.sie_block->cpuflags);
21068ad35755SDavid Hildenbrand 		}
21078ad35755SDavid Hildenbrand 		goto retry;
21088ad35755SDavid Hildenbrand 	}
21098ad35755SDavid Hildenbrand 
21100759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
21110759d068SDavid Hildenbrand 	clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
21120759d068SDavid Hildenbrand 
21132c70fe44SChristian Borntraeger 	return 0;
21142c70fe44SChristian Borntraeger }
21152c70fe44SChristian Borntraeger 
211625ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
211725ed1675SDavid Hildenbrand {
211825ed1675SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
211925ed1675SDavid Hildenbrand 	int i;
212025ed1675SDavid Hildenbrand 
212125ed1675SDavid Hildenbrand 	mutex_lock(&kvm->lock);
212225ed1675SDavid Hildenbrand 	preempt_disable();
212325ed1675SDavid Hildenbrand 	kvm->arch.epoch = tod - get_tod_clock();
212425ed1675SDavid Hildenbrand 	kvm_s390_vcpu_block_all(kvm);
212525ed1675SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm)
212625ed1675SDavid Hildenbrand 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
212725ed1675SDavid Hildenbrand 	kvm_s390_vcpu_unblock_all(kvm);
212825ed1675SDavid Hildenbrand 	preempt_enable();
212925ed1675SDavid Hildenbrand 	mutex_unlock(&kvm->lock);
213025ed1675SDavid Hildenbrand }
213125ed1675SDavid Hildenbrand 
2132fa576c58SThomas Huth /**
2133fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
2134fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
2135fa576c58SThomas Huth  * @gpa: Guest physical address
2136fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
2137fa576c58SThomas Huth  *
2138fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
2139fa576c58SThomas Huth  *
2140fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
2141fa576c58SThomas Huth  */
2142fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
214324eb3a82SDominik Dingel {
2144527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
2145527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
214624eb3a82SDominik Dingel }
214724eb3a82SDominik Dingel 
21483c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
21493c038e6bSDominik Dingel 				      unsigned long token)
21503c038e6bSDominik Dingel {
21513c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
2152383d0b05SJens Freimann 	struct kvm_s390_irq irq;
21533c038e6bSDominik Dingel 
21543c038e6bSDominik Dingel 	if (start_token) {
2155383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
2156383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
2157383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
21583c038e6bSDominik Dingel 	} else {
21593c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
2160383d0b05SJens Freimann 		inti.parm64 = token;
21613c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
21623c038e6bSDominik Dingel 	}
21633c038e6bSDominik Dingel }
21643c038e6bSDominik Dingel 
21653c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
21663c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
21673c038e6bSDominik Dingel {
21683c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
21693c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
21703c038e6bSDominik Dingel }
21713c038e6bSDominik Dingel 
21723c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
21733c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
21743c038e6bSDominik Dingel {
21753c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
21763c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
21773c038e6bSDominik Dingel }
21783c038e6bSDominik Dingel 
21793c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
21803c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
21813c038e6bSDominik Dingel {
21823c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
21833c038e6bSDominik Dingel }
21843c038e6bSDominik Dingel 
21853c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
21863c038e6bSDominik Dingel {
21873c038e6bSDominik Dingel 	/*
21883c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
21893c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
21903c038e6bSDominik Dingel 	 */
21913c038e6bSDominik Dingel 	return true;
21923c038e6bSDominik Dingel }
21933c038e6bSDominik Dingel 
21943c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
21953c038e6bSDominik Dingel {
21963c038e6bSDominik Dingel 	hva_t hva;
21973c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
21983c038e6bSDominik Dingel 	int rc;
21993c038e6bSDominik Dingel 
22003c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
22013c038e6bSDominik Dingel 		return 0;
22023c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
22033c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
22043c038e6bSDominik Dingel 		return 0;
22053c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
22063c038e6bSDominik Dingel 		return 0;
22079a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
22083c038e6bSDominik Dingel 		return 0;
22093c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
22103c038e6bSDominik Dingel 		return 0;
22113c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
22123c038e6bSDominik Dingel 		return 0;
22133c038e6bSDominik Dingel 
221481480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
221581480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
221681480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
22173c038e6bSDominik Dingel 		return 0;
22183c038e6bSDominik Dingel 
22193c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
22203c038e6bSDominik Dingel 	return rc;
22213c038e6bSDominik Dingel }
22223c038e6bSDominik Dingel 
22233fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
2224b0c632dbSHeiko Carstens {
22253fb4c40fSThomas Huth 	int rc, cpuflags;
2226e168bf8dSCarsten Otte 
22273c038e6bSDominik Dingel 	/*
22283c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
22293c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
22303c038e6bSDominik Dingel 	 * handled outside the worker.
22313c038e6bSDominik Dingel 	 */
22323c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
22333c038e6bSDominik Dingel 
22347ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
22357ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
2236b0c632dbSHeiko Carstens 
2237b0c632dbSHeiko Carstens 	if (need_resched())
2238b0c632dbSHeiko Carstens 		schedule();
2239b0c632dbSHeiko Carstens 
2240d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
224171cde587SChristian Borntraeger 		s390_handle_mcck();
224271cde587SChristian Borntraeger 
224379395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
224479395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
224579395031SJens Freimann 		if (rc)
224679395031SJens Freimann 			return rc;
224779395031SJens Freimann 	}
22480ff31867SCarsten Otte 
22492c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
22502c70fe44SChristian Borntraeger 	if (rc)
22512c70fe44SChristian Borntraeger 		return rc;
22522c70fe44SChristian Borntraeger 
225327291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
225427291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
225527291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
225627291e21SDavid Hildenbrand 	}
225727291e21SDavid Hildenbrand 
2258b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
22593fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
22603fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
22613fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
22622b29a9fdSDominik Dingel 
22633fb4c40fSThomas Huth 	return 0;
22643fb4c40fSThomas Huth }
22653fb4c40fSThomas Huth 
2266492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2267492d8642SThomas Huth {
226856317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
226956317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
227056317920SDavid Hildenbrand 	};
227156317920SDavid Hildenbrand 	u8 opcode, ilen;
2272492d8642SThomas Huth 	int rc;
2273492d8642SThomas Huth 
2274492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2275492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
2276492d8642SThomas Huth 
2277492d8642SThomas Huth 	/*
2278492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
2279492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
2280492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
2281492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
2282492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
2283492d8642SThomas Huth 	 * to be able to forward the PSW.
2284492d8642SThomas Huth 	 */
228565977322SDavid Hildenbrand 	rc = read_guest_instr(vcpu, &opcode, 1);
228656317920SDavid Hildenbrand 	ilen = insn_length(opcode);
22879b0d721aSDavid Hildenbrand 	if (rc < 0) {
22889b0d721aSDavid Hildenbrand 		return rc;
22899b0d721aSDavid Hildenbrand 	} else if (rc) {
22909b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
22919b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
22929b0d721aSDavid Hildenbrand 		 * nullification if necessary.
22939b0d721aSDavid Hildenbrand 		 */
22949b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
22959b0d721aSDavid Hildenbrand 		ilen = 4;
22969b0d721aSDavid Hildenbrand 	}
229756317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
229856317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
229956317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
2300492d8642SThomas Huth }
2301492d8642SThomas Huth 
23023fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
23033fb4c40fSThomas Huth {
23042b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
23052b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
23062b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
23072b29a9fdSDominik Dingel 
230827291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
230927291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
231027291e21SDavid Hildenbrand 
23117ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
23127ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
231371f116bfSDavid Hildenbrand 
231471f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
231571f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
231671f116bfSDavid Hildenbrand 
231771f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
231871f116bfSDavid Hildenbrand 			return rc;
231971f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
232071f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
232171f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
232271f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
232371f116bfSDavid Hildenbrand 		return -EREMOTE;
232471f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
232571f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
232671f116bfSDavid Hildenbrand 		return 0;
2327210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
2328210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2329210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
2330210b1607SThomas Huth 						current->thread.gmap_addr;
2331210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
233271f116bfSDavid Hildenbrand 		return -EREMOTE;
233324eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
23343c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
233524eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
233671f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
233771f116bfSDavid Hildenbrand 			return 0;
233871f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
2339fa576c58SThomas Huth 	}
234071f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
23413fb4c40fSThomas Huth }
23423fb4c40fSThomas Huth 
23433fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
23443fb4c40fSThomas Huth {
23453fb4c40fSThomas Huth 	int rc, exit_reason;
23463fb4c40fSThomas Huth 
2347800c1065SThomas Huth 	/*
2348800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2349800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
2350800c1065SThomas Huth 	 */
2351800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2352800c1065SThomas Huth 
2353a76ccff6SThomas Huth 	do {
23543fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
23553fb4c40fSThomas Huth 		if (rc)
2356a76ccff6SThomas Huth 			break;
23573fb4c40fSThomas Huth 
2358800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
23593fb4c40fSThomas Huth 		/*
2360a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
2361a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
23623fb4c40fSThomas Huth 		 */
23630097d12eSChristian Borntraeger 		local_irq_disable();
23640097d12eSChristian Borntraeger 		__kvm_guest_enter();
2365db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
23660097d12eSChristian Borntraeger 		local_irq_enable();
2367a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
2368a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
23690097d12eSChristian Borntraeger 		local_irq_disable();
2370db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
23710097d12eSChristian Borntraeger 		__kvm_guest_exit();
23720097d12eSChristian Borntraeger 		local_irq_enable();
2373800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
23743fb4c40fSThomas Huth 
23753fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
237627291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
23773fb4c40fSThomas Huth 
2378800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2379e168bf8dSCarsten Otte 	return rc;
2380b0c632dbSHeiko Carstens }
2381b0c632dbSHeiko Carstens 
2382b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2383b028ee3eSDavid Hildenbrand {
2384b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2385b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2386b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2387b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2388b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2389b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
2390d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
2391d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2392b028ee3eSDavid Hildenbrand 	}
2393b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
23944287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
2395b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2396b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2397b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2398b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2399b028ee3eSDavid Hildenbrand 	}
2400b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2401b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2402b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2403b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
24049fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
24059fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2406b028ee3eSDavid Hildenbrand 	}
2407b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
2408b028ee3eSDavid Hildenbrand }
2409b028ee3eSDavid Hildenbrand 
2410b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2411b028ee3eSDavid Hildenbrand {
2412b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2413b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2414b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2415b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
24164287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
2417b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2418b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2419b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2420b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2421b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2422b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2423b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
2424b028ee3eSDavid Hildenbrand }
2425b028ee3eSDavid Hildenbrand 
2426b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2427b0c632dbSHeiko Carstens {
24288f2abe6aSChristian Borntraeger 	int rc;
2429b0c632dbSHeiko Carstens 	sigset_t sigsaved;
2430b0c632dbSHeiko Carstens 
243127291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
243227291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
243327291e21SDavid Hildenbrand 		return 0;
243427291e21SDavid Hildenbrand 	}
243527291e21SDavid Hildenbrand 
2436b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2437b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2438b0c632dbSHeiko Carstens 
24396352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
24406852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
24416352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
2442ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
24436352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
24446352e4d2SDavid Hildenbrand 		return -EINVAL;
24456352e4d2SDavid Hildenbrand 	}
2446b0c632dbSHeiko Carstens 
2447b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
2448db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
2449d7b0b5ebSCarsten Otte 
2450dab4079dSHeiko Carstens 	might_fault();
2451e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
24529ace903dSChristian Ehrhardt 
2453b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
2454b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
24558f2abe6aSChristian Borntraeger 		rc = -EINTR;
2456b1d16c49SChristian Ehrhardt 	}
24578f2abe6aSChristian Borntraeger 
245827291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
245927291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
246027291e21SDavid Hildenbrand 		rc = 0;
246127291e21SDavid Hildenbrand 	}
246227291e21SDavid Hildenbrand 
24638f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
246471f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
24658f2abe6aSChristian Borntraeger 		rc = 0;
24668f2abe6aSChristian Borntraeger 	}
24678f2abe6aSChristian Borntraeger 
2468db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
2469b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
2470d7b0b5ebSCarsten Otte 
2471b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2472b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2473b0c632dbSHeiko Carstens 
2474b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
24757e8e6ab4SHeiko Carstens 	return rc;
2476b0c632dbSHeiko Carstens }
2477b0c632dbSHeiko Carstens 
2478b0c632dbSHeiko Carstens /*
2479b0c632dbSHeiko Carstens  * store status at address
2480b0c632dbSHeiko Carstens  * we use have two special cases:
2481b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2482b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2483b0c632dbSHeiko Carstens  */
2484d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
2485b0c632dbSHeiko Carstens {
2486092670cdSCarsten Otte 	unsigned char archmode = 1;
24879abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
2488fda902cbSMichael Mueller 	unsigned int px;
24894287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
2490d0bce605SHeiko Carstens 	int rc;
2491b0c632dbSHeiko Carstens 
2492d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
2493d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2494d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
2495b0c632dbSHeiko Carstens 			return -EFAULT;
2496d9a3a09aSMartin Schwidefsky 		gpa = 0;
2497d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2498d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
2499b0c632dbSHeiko Carstens 			return -EFAULT;
2500d9a3a09aSMartin Schwidefsky 		gpa = px;
2501d9a3a09aSMartin Schwidefsky 	} else
2502d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
25039abc2a08SDavid Hildenbrand 
25049abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
25059abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
25069522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
2507d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
25089abc2a08SDavid Hildenbrand 				     fprs, 128);
25099abc2a08SDavid Hildenbrand 	} else {
25109abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
25116fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
25129abc2a08SDavid Hildenbrand 	}
2513d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
2514d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
2515d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
2516d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
2517d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
2518fda902cbSMichael Mueller 			      &px, 4);
2519d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
25209abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
2521d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
2522d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
25234287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
2524d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
25254287f247SDavid Hildenbrand 			      &cputm, 8);
2526178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
2527d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
2528d0bce605SHeiko Carstens 			      &clkcomp, 8);
2529d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
2530d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
2531d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
2532d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
2533d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
2534b0c632dbSHeiko Carstens }
2535b0c632dbSHeiko Carstens 
2536e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2537e879892cSThomas Huth {
2538e879892cSThomas Huth 	/*
2539e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
2540e879892cSThomas Huth 	 * copying in vcpu load/put. Lets update our copies before we save
2541e879892cSThomas Huth 	 * it into the save area
2542e879892cSThomas Huth 	 */
2543d0164ee2SHendrik Brueckner 	save_fpu_regs();
25449abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
2545e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
2546e879892cSThomas Huth 
2547e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
2548e879892cSThomas Huth }
2549e879892cSThomas Huth 
2550bc17de7cSEric Farman /*
2551bc17de7cSEric Farman  * store additional status at address
2552bc17de7cSEric Farman  */
2553bc17de7cSEric Farman int kvm_s390_store_adtl_status_unloaded(struct kvm_vcpu *vcpu,
2554bc17de7cSEric Farman 					unsigned long gpa)
2555bc17de7cSEric Farman {
2556bc17de7cSEric Farman 	/* Only bits 0-53 are used for address formation */
2557bc17de7cSEric Farman 	if (!(gpa & ~0x3ff))
2558bc17de7cSEric Farman 		return 0;
2559bc17de7cSEric Farman 
2560bc17de7cSEric Farman 	return write_guest_abs(vcpu, gpa & ~0x3ff,
2561bc17de7cSEric Farman 			       (void *)&vcpu->run->s.regs.vrs, 512);
2562bc17de7cSEric Farman }
2563bc17de7cSEric Farman 
2564bc17de7cSEric Farman int kvm_s390_vcpu_store_adtl_status(struct kvm_vcpu *vcpu, unsigned long addr)
2565bc17de7cSEric Farman {
2566bc17de7cSEric Farman 	if (!test_kvm_facility(vcpu->kvm, 129))
2567bc17de7cSEric Farman 		return 0;
2568bc17de7cSEric Farman 
2569bc17de7cSEric Farman 	/*
2570bc17de7cSEric Farman 	 * The guest VXRS are in the host VXRs due to the lazy
25719977e886SHendrik Brueckner 	 * copying in vcpu load/put. We can simply call save_fpu_regs()
25729977e886SHendrik Brueckner 	 * to save the current register state because we are in the
25739977e886SHendrik Brueckner 	 * middle of a load/put cycle.
25749977e886SHendrik Brueckner 	 *
25759977e886SHendrik Brueckner 	 * Let's update our copies before we save it into the save area.
2576bc17de7cSEric Farman 	 */
2577d0164ee2SHendrik Brueckner 	save_fpu_regs();
2578bc17de7cSEric Farman 
2579bc17de7cSEric Farman 	return kvm_s390_store_adtl_status_unloaded(vcpu, addr);
2580bc17de7cSEric Farman }
2581bc17de7cSEric Farman 
25828ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
25838ad35755SDavid Hildenbrand {
25848ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
25858e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
25868ad35755SDavid Hildenbrand }
25878ad35755SDavid Hildenbrand 
25888ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
25898ad35755SDavid Hildenbrand {
25908ad35755SDavid Hildenbrand 	unsigned int i;
25918ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
25928ad35755SDavid Hildenbrand 
25938ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
25948ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
25958ad35755SDavid Hildenbrand 	}
25968ad35755SDavid Hildenbrand }
25978ad35755SDavid Hildenbrand 
25988ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
25998ad35755SDavid Hildenbrand {
26008ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
26018e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
26028ad35755SDavid Hildenbrand }
26038ad35755SDavid Hildenbrand 
26046852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
26056852d7b6SDavid Hildenbrand {
26068ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
26078ad35755SDavid Hildenbrand 
26088ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
26098ad35755SDavid Hildenbrand 		return;
26108ad35755SDavid Hildenbrand 
26116852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
26128ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
2613433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
26148ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
26158ad35755SDavid Hildenbrand 
26168ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
26178ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
26188ad35755SDavid Hildenbrand 			started_vcpus++;
26198ad35755SDavid Hildenbrand 	}
26208ad35755SDavid Hildenbrand 
26218ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
26228ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
26238ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
26248ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
26258ad35755SDavid Hildenbrand 		/*
26268ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
26278ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
26288ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
26298ad35755SDavid Hildenbrand 		 */
26308ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
26318ad35755SDavid Hildenbrand 	}
26328ad35755SDavid Hildenbrand 
2633805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
26348ad35755SDavid Hildenbrand 	/*
26358ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
26368ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
26378ad35755SDavid Hildenbrand 	 */
2638d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2639433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
26408ad35755SDavid Hildenbrand 	return;
26416852d7b6SDavid Hildenbrand }
26426852d7b6SDavid Hildenbrand 
26436852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
26446852d7b6SDavid Hildenbrand {
26458ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
26468ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
26478ad35755SDavid Hildenbrand 
26488ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
26498ad35755SDavid Hildenbrand 		return;
26508ad35755SDavid Hildenbrand 
26516852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
26528ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
2653433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
26548ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
26558ad35755SDavid Hildenbrand 
265632f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
26576cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
265832f5ff63SDavid Hildenbrand 
2659805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
26608ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
26618ad35755SDavid Hildenbrand 
26628ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
26638ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
26648ad35755SDavid Hildenbrand 			started_vcpus++;
26658ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
26668ad35755SDavid Hildenbrand 		}
26678ad35755SDavid Hildenbrand 	}
26688ad35755SDavid Hildenbrand 
26698ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
26708ad35755SDavid Hildenbrand 		/*
26718ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
26728ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
26738ad35755SDavid Hildenbrand 		 */
26748ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
26758ad35755SDavid Hildenbrand 	}
26768ad35755SDavid Hildenbrand 
2677433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
26788ad35755SDavid Hildenbrand 	return;
26796852d7b6SDavid Hildenbrand }
26806852d7b6SDavid Hildenbrand 
2681d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2682d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
2683d6712df9SCornelia Huck {
2684d6712df9SCornelia Huck 	int r;
2685d6712df9SCornelia Huck 
2686d6712df9SCornelia Huck 	if (cap->flags)
2687d6712df9SCornelia Huck 		return -EINVAL;
2688d6712df9SCornelia Huck 
2689d6712df9SCornelia Huck 	switch (cap->cap) {
2690fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
2691fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
2692fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
2693c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
2694fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
2695fa6b7fe9SCornelia Huck 		}
2696fa6b7fe9SCornelia Huck 		r = 0;
2697fa6b7fe9SCornelia Huck 		break;
2698d6712df9SCornelia Huck 	default:
2699d6712df9SCornelia Huck 		r = -EINVAL;
2700d6712df9SCornelia Huck 		break;
2701d6712df9SCornelia Huck 	}
2702d6712df9SCornelia Huck 	return r;
2703d6712df9SCornelia Huck }
2704d6712df9SCornelia Huck 
270541408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
270641408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
270741408c28SThomas Huth {
270841408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
270941408c28SThomas Huth 	void *tmpbuf = NULL;
271041408c28SThomas Huth 	int r, srcu_idx;
271141408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
271241408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
271341408c28SThomas Huth 
271441408c28SThomas Huth 	if (mop->flags & ~supported_flags)
271541408c28SThomas Huth 		return -EINVAL;
271641408c28SThomas Huth 
271741408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
271841408c28SThomas Huth 		return -E2BIG;
271941408c28SThomas Huth 
272041408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
272141408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
272241408c28SThomas Huth 		if (!tmpbuf)
272341408c28SThomas Huth 			return -ENOMEM;
272441408c28SThomas Huth 	}
272541408c28SThomas Huth 
272641408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
272741408c28SThomas Huth 
272841408c28SThomas Huth 	switch (mop->op) {
272941408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
273041408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
273192c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
273292c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
273341408c28SThomas Huth 			break;
273441408c28SThomas Huth 		}
273541408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
273641408c28SThomas Huth 		if (r == 0) {
273741408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
273841408c28SThomas Huth 				r = -EFAULT;
273941408c28SThomas Huth 		}
274041408c28SThomas Huth 		break;
274141408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
274241408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
274392c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
274492c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
274541408c28SThomas Huth 			break;
274641408c28SThomas Huth 		}
274741408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
274841408c28SThomas Huth 			r = -EFAULT;
274941408c28SThomas Huth 			break;
275041408c28SThomas Huth 		}
275141408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
275241408c28SThomas Huth 		break;
275341408c28SThomas Huth 	default:
275441408c28SThomas Huth 		r = -EINVAL;
275541408c28SThomas Huth 	}
275641408c28SThomas Huth 
275741408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
275841408c28SThomas Huth 
275941408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
276041408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
276141408c28SThomas Huth 
276241408c28SThomas Huth 	vfree(tmpbuf);
276341408c28SThomas Huth 	return r;
276441408c28SThomas Huth }
276541408c28SThomas Huth 
2766b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
2767b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
2768b0c632dbSHeiko Carstens {
2769b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
2770b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
2771800c1065SThomas Huth 	int idx;
2772bc923cc9SAvi Kivity 	long r;
2773b0c632dbSHeiko Carstens 
277493736624SAvi Kivity 	switch (ioctl) {
277547b43c52SJens Freimann 	case KVM_S390_IRQ: {
277647b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
277747b43c52SJens Freimann 
277847b43c52SJens Freimann 		r = -EFAULT;
277947b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
278047b43c52SJens Freimann 			break;
278147b43c52SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
278247b43c52SJens Freimann 		break;
278347b43c52SJens Freimann 	}
278493736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
2785ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
2786383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
2787ba5c1e9bSCarsten Otte 
278893736624SAvi Kivity 		r = -EFAULT;
2789ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
279093736624SAvi Kivity 			break;
2791383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
2792383d0b05SJens Freimann 			return -EINVAL;
2793383d0b05SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
279493736624SAvi Kivity 		break;
2795ba5c1e9bSCarsten Otte 	}
2796b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
2797800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
2798bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
2799800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
2800bc923cc9SAvi Kivity 		break;
2801b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
2802b0c632dbSHeiko Carstens 		psw_t psw;
2803b0c632dbSHeiko Carstens 
2804bc923cc9SAvi Kivity 		r = -EFAULT;
2805b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
2806bc923cc9SAvi Kivity 			break;
2807bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
2808bc923cc9SAvi Kivity 		break;
2809b0c632dbSHeiko Carstens 	}
2810b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
2811bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
2812bc923cc9SAvi Kivity 		break;
281314eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
281414eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
281514eebd91SCarsten Otte 		struct kvm_one_reg reg;
281614eebd91SCarsten Otte 		r = -EFAULT;
281714eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
281814eebd91SCarsten Otte 			break;
281914eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
282014eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
282114eebd91SCarsten Otte 		else
282214eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
282314eebd91SCarsten Otte 		break;
282414eebd91SCarsten Otte 	}
282527e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
282627e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
282727e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
282827e0393fSCarsten Otte 
282927e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
283027e0393fSCarsten Otte 			r = -EFAULT;
283127e0393fSCarsten Otte 			break;
283227e0393fSCarsten Otte 		}
283327e0393fSCarsten Otte 
283427e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
283527e0393fSCarsten Otte 			r = -EINVAL;
283627e0393fSCarsten Otte 			break;
283727e0393fSCarsten Otte 		}
283827e0393fSCarsten Otte 
283927e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
284027e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
284127e0393fSCarsten Otte 		break;
284227e0393fSCarsten Otte 	}
284327e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
284427e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
284527e0393fSCarsten Otte 
284627e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
284727e0393fSCarsten Otte 			r = -EFAULT;
284827e0393fSCarsten Otte 			break;
284927e0393fSCarsten Otte 		}
285027e0393fSCarsten Otte 
285127e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
285227e0393fSCarsten Otte 			r = -EINVAL;
285327e0393fSCarsten Otte 			break;
285427e0393fSCarsten Otte 		}
285527e0393fSCarsten Otte 
285627e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
285727e0393fSCarsten Otte 			ucasmap.length);
285827e0393fSCarsten Otte 		break;
285927e0393fSCarsten Otte 	}
286027e0393fSCarsten Otte #endif
2861ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
2862527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
2863ccc7910fSCarsten Otte 		break;
2864ccc7910fSCarsten Otte 	}
2865d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
2866d6712df9SCornelia Huck 	{
2867d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
2868d6712df9SCornelia Huck 		r = -EFAULT;
2869d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
2870d6712df9SCornelia Huck 			break;
2871d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
2872d6712df9SCornelia Huck 		break;
2873d6712df9SCornelia Huck 	}
287441408c28SThomas Huth 	case KVM_S390_MEM_OP: {
287541408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
287641408c28SThomas Huth 
287741408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
287841408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
287941408c28SThomas Huth 		else
288041408c28SThomas Huth 			r = -EFAULT;
288141408c28SThomas Huth 		break;
288241408c28SThomas Huth 	}
2883816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
2884816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
2885816c7667SJens Freimann 
2886816c7667SJens Freimann 		r = -EFAULT;
2887816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2888816c7667SJens Freimann 			break;
2889816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
2890816c7667SJens Freimann 		    irq_state.len == 0 ||
2891816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
2892816c7667SJens Freimann 			r = -EINVAL;
2893816c7667SJens Freimann 			break;
2894816c7667SJens Freimann 		}
2895816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
2896816c7667SJens Freimann 					   (void __user *) irq_state.buf,
2897816c7667SJens Freimann 					   irq_state.len);
2898816c7667SJens Freimann 		break;
2899816c7667SJens Freimann 	}
2900816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
2901816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
2902816c7667SJens Freimann 
2903816c7667SJens Freimann 		r = -EFAULT;
2904816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
2905816c7667SJens Freimann 			break;
2906816c7667SJens Freimann 		if (irq_state.len == 0) {
2907816c7667SJens Freimann 			r = -EINVAL;
2908816c7667SJens Freimann 			break;
2909816c7667SJens Freimann 		}
2910816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
2911816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
2912816c7667SJens Freimann 					   irq_state.len);
2913816c7667SJens Freimann 		break;
2914816c7667SJens Freimann 	}
2915b0c632dbSHeiko Carstens 	default:
29163e6afcf1SCarsten Otte 		r = -ENOTTY;
2917b0c632dbSHeiko Carstens 	}
2918bc923cc9SAvi Kivity 	return r;
2919b0c632dbSHeiko Carstens }
2920b0c632dbSHeiko Carstens 
29215b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
29225b1c1493SCarsten Otte {
29235b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
29245b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
29255b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
29265b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
29275b1c1493SCarsten Otte 		get_page(vmf->page);
29285b1c1493SCarsten Otte 		return 0;
29295b1c1493SCarsten Otte 	}
29305b1c1493SCarsten Otte #endif
29315b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
29325b1c1493SCarsten Otte }
29335b1c1493SCarsten Otte 
29345587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
29355587027cSAneesh Kumar K.V 			    unsigned long npages)
2936db3fe4ebSTakuya Yoshikawa {
2937db3fe4ebSTakuya Yoshikawa 	return 0;
2938db3fe4ebSTakuya Yoshikawa }
2939db3fe4ebSTakuya Yoshikawa 
2940b0c632dbSHeiko Carstens /* Section: memory related */
2941f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
2942f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
294309170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
29447b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
2945b0c632dbSHeiko Carstens {
2946dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
2947dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
2948dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
2949dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
2950b0c632dbSHeiko Carstens 
2951598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
2952b0c632dbSHeiko Carstens 		return -EINVAL;
2953b0c632dbSHeiko Carstens 
2954598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
2955b0c632dbSHeiko Carstens 		return -EINVAL;
2956b0c632dbSHeiko Carstens 
2957a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
2958a3a92c31SDominik Dingel 		return -EINVAL;
2959a3a92c31SDominik Dingel 
2960f7784b8eSMarcelo Tosatti 	return 0;
2961f7784b8eSMarcelo Tosatti }
2962f7784b8eSMarcelo Tosatti 
2963f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
296409170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
29658482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
2966f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
29678482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
2968f7784b8eSMarcelo Tosatti {
2969f7850c92SCarsten Otte 	int rc;
2970f7784b8eSMarcelo Tosatti 
29712cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
29722cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
29732cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
29742cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
29752cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
29762cef4debSChristian Borntraeger 	 */
29772cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
29782cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
29792cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
29802cef4debSChristian Borntraeger 		return;
2981598841caSCarsten Otte 
2982598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
2983598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
2984598841caSCarsten Otte 	if (rc)
2985ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
2986598841caSCarsten Otte 	return;
2987b0c632dbSHeiko Carstens }
2988b0c632dbSHeiko Carstens 
298960a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
299060a37709SAlexander Yarygin {
299160a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
299260a37709SAlexander Yarygin 
299360a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
299460a37709SAlexander Yarygin }
299560a37709SAlexander Yarygin 
29963491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
29973491caf2SChristian Borntraeger {
29983491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
29993491caf2SChristian Borntraeger }
30003491caf2SChristian Borntraeger 
3001b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
3002b0c632dbSHeiko Carstens {
300360a37709SAlexander Yarygin 	int i;
300460a37709SAlexander Yarygin 
300507197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
300607197fd0SDavid Hildenbrand 		pr_info("SIE not available\n");
300707197fd0SDavid Hildenbrand 		return -ENODEV;
300807197fd0SDavid Hildenbrand 	}
300907197fd0SDavid Hildenbrand 
301060a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
301160a37709SAlexander Yarygin 		kvm_s390_fac_list_mask[i] |=
301260a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
301360a37709SAlexander Yarygin 
30149d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
3015b0c632dbSHeiko Carstens }
3016b0c632dbSHeiko Carstens 
3017b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
3018b0c632dbSHeiko Carstens {
3019b0c632dbSHeiko Carstens 	kvm_exit();
3020b0c632dbSHeiko Carstens }
3021b0c632dbSHeiko Carstens 
3022b0c632dbSHeiko Carstens module_init(kvm_s390_init);
3023b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
3024566af940SCornelia Huck 
3025566af940SCornelia Huck /*
3026566af940SCornelia Huck  * Enable autoloading of the kvm module.
3027566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3028566af940SCornelia Huck  * since x86 takes a different approach.
3029566af940SCornelia Huck  */
3030566af940SCornelia Huck #include <linux/miscdevice.h>
3031566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
3032566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
3033