xref: /linux/arch/s390/kvm/kvm-s390.c (revision bb59c2da3fb4ecc83258ca6b2ecde70d80c33465)
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>
24b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
25b0c632dbSHeiko Carstens #include <linux/module.h>
26d3217967SPaul Gortmaker #include <linux/moduleparam.h>
27a374e892STony Krowiak #include <linux/random.h>
28b0c632dbSHeiko Carstens #include <linux/slab.h>
29ba5c1e9bSCarsten Otte #include <linux/timer.h>
3041408c28SThomas Huth #include <linux/vmalloc.h>
3115c9705fSDavid Hildenbrand #include <linux/bitmap.h>
32174cd4b1SIngo Molnar #include <linux/sched/signal.h>
33190df4a2SClaudio Imbrenda #include <linux/string.h>
34174cd4b1SIngo Molnar 
35cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
36b0c632dbSHeiko Carstens #include <asm/lowcore.h>
37fd5ada04SMartin Schwidefsky #include <asm/stp.h>
38b0c632dbSHeiko Carstens #include <asm/pgtable.h>
391e133ab2SMartin Schwidefsky #include <asm/gmap.h>
40f5daba1dSHeiko Carstens #include <asm/nmi.h>
41a0616cdeSDavid Howells #include <asm/switch_to.h>
426d3da241SJens Freimann #include <asm/isc.h>
431526bf9cSChristian Borntraeger #include <asm/sclp.h>
440a763c78SDavid Hildenbrand #include <asm/cpacf.h>
45221bb8a4SLinus Torvalds #include <asm/timex.h>
468f2abe6aSChristian Borntraeger #include "kvm-s390.h"
47b0c632dbSHeiko Carstens #include "gaccess.h"
48b0c632dbSHeiko Carstens 
49ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
50ea2cdd27SDavid Hildenbrand #undef pr_fmt
51ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
52ea2cdd27SDavid Hildenbrand 
535786fffaSCornelia Huck #define CREATE_TRACE_POINTS
545786fffaSCornelia Huck #include "trace.h"
55ade38c31SCornelia Huck #include "trace-s390.h"
565786fffaSCornelia Huck 
5741408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
58816c7667SJens Freimann #define LOCAL_IRQS 32
59816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
60816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
6141408c28SThomas Huth 
62b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
63b0c632dbSHeiko Carstens 
64b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
65b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
660eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
678f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
688f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
698f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
708f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
71ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
729ec6de19SAlexander Yarygin 	{ "exit_pei", VCPU_STAT(exit_pei) },
73ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
74ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
75a011eeb2SJanosch Frank 	{ "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
76f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
7762bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
783491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
79ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
80f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
81ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
82aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
83aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
84ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
857697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
86ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
87ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
88ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
89ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
90ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
91ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
92ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
9369d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
94453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
95453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
96453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
97453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
98453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
998a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
100453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
101453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
102b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
103453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
104453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
105bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
10695ca2cb5SJanosch Frank 	{ "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
107a3508fbeSDavid Hildenbrand 	{ "instruction_sie", VCPU_STAT(instruction_sie) },
1085288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
109bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
1107697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
1115288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
11242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
11342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1145288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
11542cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
11642cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
117cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1185288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1195288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1205288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
12142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
12242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
12342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
124388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
125e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
12641628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
127175a5c9eSChristian Borntraeger 	{ "diagnose_258", VCPU_STAT(diagnose_258) },
128175a5c9eSChristian Borntraeger 	{ "diagnose_308", VCPU_STAT(diagnose_308) },
129175a5c9eSChristian Borntraeger 	{ "diagnose_500", VCPU_STAT(diagnose_500) },
130b0c632dbSHeiko Carstens 	{ NULL }
131b0c632dbSHeiko Carstens };
132b0c632dbSHeiko Carstens 
1338fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext {
1348fa1696eSCollin L. Walling 	__u8 epoch_idx;
1358fa1696eSCollin L. Walling 	__u64 tod;
1368fa1696eSCollin L. Walling 	__u8 reserved[7];
1378fa1696eSCollin L. Walling } __packed;
1388fa1696eSCollin L. Walling 
139a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
140a411edf1SDavid Hildenbrand static int nested;
141a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
142a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
143a411edf1SDavid Hildenbrand 
1449d8d5786SMichael Mueller /* upper facilities limit for kvm */
145f6c1d359SHeiko Carstens unsigned long kvm_s390_fac_list_mask[16] = { FACILITIES_KVM };
146b0c632dbSHeiko Carstens 
1479d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void)
14878c4b59fSMichael Mueller {
1499d8d5786SMichael Mueller 	BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
1509d8d5786SMichael Mueller 	return ARRAY_SIZE(kvm_s390_fac_list_mask);
15178c4b59fSMichael Mueller }
15278c4b59fSMichael Mueller 
15315c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
15415c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1550a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
1560a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
15715c9705fSDavid Hildenbrand 
1589d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
159a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
16078f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
1619d8d5786SMichael Mueller 
162b0c632dbSHeiko Carstens /* Section: not file related */
16313a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
164b0c632dbSHeiko Carstens {
165b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
16610474ae8SAlexander Graf 	return 0;
167b0c632dbSHeiko Carstens }
168b0c632dbSHeiko Carstens 
169414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
170414d3b07SMartin Schwidefsky 			      unsigned long end);
1712c70fe44SChristian Borntraeger 
172fdf03650SFan Zhang /*
173fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
174fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
175fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
176fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
177fdf03650SFan Zhang  */
178fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
179fdf03650SFan Zhang 			  void *v)
180fdf03650SFan Zhang {
181fdf03650SFan Zhang 	struct kvm *kvm;
182fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
183fdf03650SFan Zhang 	int i;
184fdf03650SFan Zhang 	unsigned long long *delta = v;
185fdf03650SFan Zhang 
186fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
187fdf03650SFan Zhang 		kvm->arch.epoch -= *delta;
188fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
189fdf03650SFan Zhang 			vcpu->arch.sie_block->epoch -= *delta;
190db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
191db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
19291473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
19391473b48SDavid Hildenbrand 				vcpu->arch.vsie_block->epoch -= *delta;
194fdf03650SFan Zhang 		}
195fdf03650SFan Zhang 	}
196fdf03650SFan Zhang 	return NOTIFY_OK;
197fdf03650SFan Zhang }
198fdf03650SFan Zhang 
199fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
200fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
201fdf03650SFan Zhang };
202fdf03650SFan Zhang 
203b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
204b0c632dbSHeiko Carstens {
2052c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
206b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
207a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
208a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
209fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
210fdf03650SFan Zhang 				       &kvm_clock_notifier);
211b0c632dbSHeiko Carstens 	return 0;
212b0c632dbSHeiko Carstens }
213b0c632dbSHeiko Carstens 
214b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
215b0c632dbSHeiko Carstens {
216b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
217a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
218fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
219fdf03650SFan Zhang 					 &kvm_clock_notifier);
220b0c632dbSHeiko Carstens }
221b0c632dbSHeiko Carstens 
22222be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
22322be5a13SDavid Hildenbrand {
22422be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
22522be5a13SDavid Hildenbrand }
22622be5a13SDavid Hildenbrand 
2270a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
2280a763c78SDavid Hildenbrand {
2290a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
230d051ae53SHeiko Carstens 	int cc;
2310a763c78SDavid Hildenbrand 
2320a763c78SDavid Hildenbrand 	asm volatile(
2330a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
2340a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
2350a763c78SDavid Hildenbrand 		"	ipm	%0\n"
2360a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
2370a763c78SDavid Hildenbrand 		: "=d" (cc)
2380a763c78SDavid Hildenbrand 		: "d" (r0)
2390a763c78SDavid Hildenbrand 		: "cc");
2400a763c78SDavid Hildenbrand 	return cc == 0;
2410a763c78SDavid Hildenbrand }
2420a763c78SDavid Hildenbrand 
24322be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
24422be5a13SDavid Hildenbrand {
2450a763c78SDavid Hildenbrand 	int i;
2460a763c78SDavid Hildenbrand 
2470a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
2480a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
2490a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
2500a763c78SDavid Hildenbrand 	}
2510a763c78SDavid Hildenbrand 
2520a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
253221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
254221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
255221bb8a4SLinus Torvalds 		     PTFF_QAF);
2560a763c78SDavid Hildenbrand 
2570a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
25869c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
25969c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
26069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
26169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
26269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
26369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
26469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
26569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
26669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
26769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
2680a763c78SDavid Hildenbrand 	}
2690a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
27069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
27169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
2720a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
27369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
27469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
27569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
27669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
27769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
27869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
27969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
28069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
2810a763c78SDavid Hildenbrand 	}
2820a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
283985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
28469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
2850a763c78SDavid Hildenbrand 
286e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
287e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
288e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
289e000b8e0SJason J. Herne 
29022be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
29122be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
292a3508fbeSDavid Hildenbrand 	/*
293a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
294a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
295a3508fbeSDavid Hildenbrand 	 */
296a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
297a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
298a3508fbeSDavid Hildenbrand 		return;
299a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
30019c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
30119c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
3020615a326SDavid Hildenbrand 	if (sclp.has_siif)
3030615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
30477d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
30577d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
306a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
307a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
3085630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
3095630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
31013ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
31113ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
3127fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
3137fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
314730cd632SFarhan Ali 	if (sclp.has_kss)
315730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
3165d3876a8SDavid Hildenbrand 	/*
3175d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
3185d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
3195d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
3205d3876a8SDavid Hildenbrand 	 *
3215d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
3225d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
3235d3876a8SDavid Hildenbrand 	 *
3245d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
3255d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
3265d3876a8SDavid Hildenbrand 	 *
3275d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
3285d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
3295d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
3305d3876a8SDavid Hildenbrand 	 *
3315d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
3325d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
3335d3876a8SDavid Hildenbrand 	 */
33422be5a13SDavid Hildenbrand }
33522be5a13SDavid Hildenbrand 
336b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
337b0c632dbSHeiko Carstens {
33878f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
33978f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
34078f26131SChristian Borntraeger 		return -ENOMEM;
34178f26131SChristian Borntraeger 
34278f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
34378f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
34478f26131SChristian Borntraeger 		return -ENOMEM;
34578f26131SChristian Borntraeger 	}
34678f26131SChristian Borntraeger 
34722be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
34822be5a13SDavid Hildenbrand 
34984877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
35084877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
351b0c632dbSHeiko Carstens }
352b0c632dbSHeiko Carstens 
35378f26131SChristian Borntraeger void kvm_arch_exit(void)
35478f26131SChristian Borntraeger {
35578f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
35678f26131SChristian Borntraeger }
35778f26131SChristian Borntraeger 
358b0c632dbSHeiko Carstens /* Section: device related */
359b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
360b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
361b0c632dbSHeiko Carstens {
362b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
363b0c632dbSHeiko Carstens 		return s390_enable_sie();
364b0c632dbSHeiko Carstens 	return -EINVAL;
365b0c632dbSHeiko Carstens }
366b0c632dbSHeiko Carstens 
367784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
368b0c632dbSHeiko Carstens {
369d7b0b5ebSCarsten Otte 	int r;
370d7b0b5ebSCarsten Otte 
3712bd0ac4eSCarsten Otte 	switch (ext) {
372d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
373b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
37452e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
3751efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
3761efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
3771efd0f59SCarsten Otte #endif
3783c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
37960b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
38014eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
381d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
382fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
38310ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
384c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
385d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
38678599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
387f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
3886352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
389460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
39047b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
3912444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
392e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
39330ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
394816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
3956502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
3964036e387SClaudio Imbrenda 	case KVM_CAP_S390_CMMA_MIGRATION:
39747a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
398d7b0b5ebSCarsten Otte 		r = 1;
399d7b0b5ebSCarsten Otte 		break;
40041408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
40141408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
40241408c28SThomas Huth 		break;
403e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
404e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
40576a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
406a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
407a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
408a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
40976a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
410e726b1bdSChristian Borntraeger 		break;
411e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
412e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
413e1e2e605SNick Wang 		break;
4141526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
415abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
4161526bf9cSChristian Borntraeger 		break;
41768c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
41868c55750SEric Farman 		r = MACHINE_HAS_VX;
41968c55750SEric Farman 		break;
420c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
421c6e5f166SFan Zhang 		r = test_facility(64);
422c6e5f166SFan Zhang 		break;
4234e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
4244e0b1ab7SFan Zhang 		r = test_facility(133);
4254e0b1ab7SFan Zhang 		break;
4262bd0ac4eSCarsten Otte 	default:
427d7b0b5ebSCarsten Otte 		r = 0;
428b0c632dbSHeiko Carstens 	}
429d7b0b5ebSCarsten Otte 	return r;
4302bd0ac4eSCarsten Otte }
431b0c632dbSHeiko Carstens 
43215f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
43315f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
43415f36ebdSJason J. Herne {
43515f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
43615f36ebdSJason J. Herne 	unsigned long address;
43715f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
43815f36ebdSJason J. Herne 
43915f36ebdSJason J. Herne 	/* Loop over all guest pages */
44015f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
44115f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
44215f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
44315f36ebdSJason J. Herne 
4441e133ab2SMartin Schwidefsky 		if (test_and_clear_guest_dirty(gmap->mm, address))
44515f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
4461763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
4471763f8d0SChristian Borntraeger 			return;
44870c88a00SChristian Borntraeger 		cond_resched();
44915f36ebdSJason J. Herne 	}
45015f36ebdSJason J. Herne }
45115f36ebdSJason J. Herne 
452b0c632dbSHeiko Carstens /* Section: vm related */
453a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
454a6e2f683SEugene (jno) Dvurechenski 
455b0c632dbSHeiko Carstens /*
456b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
457b0c632dbSHeiko Carstens  */
458b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
459b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
460b0c632dbSHeiko Carstens {
46115f36ebdSJason J. Herne 	int r;
46215f36ebdSJason J. Herne 	unsigned long n;
4639f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
46415f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
46515f36ebdSJason J. Herne 	int is_dirty = 0;
46615f36ebdSJason J. Herne 
467e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
468e1e8a962SJanosch Frank 		return -EINVAL;
469e1e8a962SJanosch Frank 
47015f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
47115f36ebdSJason J. Herne 
47215f36ebdSJason J. Herne 	r = -EINVAL;
47315f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
47415f36ebdSJason J. Herne 		goto out;
47515f36ebdSJason J. Herne 
4769f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
4779f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
47815f36ebdSJason J. Herne 	r = -ENOENT;
47915f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
48015f36ebdSJason J. Herne 		goto out;
48115f36ebdSJason J. Herne 
48215f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
48315f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
48415f36ebdSJason J. Herne 	if (r)
48515f36ebdSJason J. Herne 		goto out;
48615f36ebdSJason J. Herne 
48715f36ebdSJason J. Herne 	/* Clear the dirty log */
48815f36ebdSJason J. Herne 	if (is_dirty) {
48915f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
49015f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
49115f36ebdSJason J. Herne 	}
49215f36ebdSJason J. Herne 	r = 0;
49315f36ebdSJason J. Herne out:
49415f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
49515f36ebdSJason J. Herne 	return r;
496b0c632dbSHeiko Carstens }
497b0c632dbSHeiko Carstens 
4986502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
4996502a34cSDavid Hildenbrand {
5006502a34cSDavid Hildenbrand 	unsigned int i;
5016502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
5026502a34cSDavid Hildenbrand 
5036502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
5046502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
5056502a34cSDavid Hildenbrand 	}
5066502a34cSDavid Hildenbrand }
5076502a34cSDavid Hildenbrand 
508d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
509d938dc55SCornelia Huck {
510d938dc55SCornelia Huck 	int r;
511d938dc55SCornelia Huck 
512d938dc55SCornelia Huck 	if (cap->flags)
513d938dc55SCornelia Huck 		return -EINVAL;
514d938dc55SCornelia Huck 
515d938dc55SCornelia Huck 	switch (cap->cap) {
51684223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
517c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
51884223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
51984223598SCornelia Huck 		r = 0;
52084223598SCornelia Huck 		break;
5212444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
522c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
5232444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
5242444b352SDavid Hildenbrand 		r = 0;
5252444b352SDavid Hildenbrand 		break;
52668c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
5275967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
528a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
5295967c17bSDavid Hildenbrand 			r = -EBUSY;
5305967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
531c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
532c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
5332f87d942SGuenther Hutzl 			if (test_facility(134)) {
5342f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
5352f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
5362f87d942SGuenther Hutzl 			}
53753743aa7SMaxim Samoylov 			if (test_facility(135)) {
53853743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
53953743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
54053743aa7SMaxim Samoylov 			}
54118280d8bSMichael Mueller 			r = 0;
54218280d8bSMichael Mueller 		} else
54318280d8bSMichael Mueller 			r = -EINVAL;
5445967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
545c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
546c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
54768c55750SEric Farman 		break;
548c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
549c6e5f166SFan Zhang 		r = -EINVAL;
550c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
551a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
552c6e5f166SFan Zhang 			r = -EBUSY;
553c6e5f166SFan Zhang 		} else if (test_facility(64)) {
554c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
555c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
556c6e5f166SFan Zhang 			r = 0;
557c6e5f166SFan Zhang 		}
558c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
559c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
560c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
561c6e5f166SFan Zhang 		break;
56247a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
56347a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
56447a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
56547a4693eSYi Min Zhao 			r = -EBUSY;
56647a4693eSYi Min Zhao 		} else {
56747a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
56847a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
56947a4693eSYi Min Zhao 			r = 0;
57047a4693eSYi Min Zhao 		}
57147a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
57247a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
57347a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
57447a4693eSYi Min Zhao 		break;
5754e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
5764e0b1ab7SFan Zhang 		r = -EINVAL;
5774e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
5784e0b1ab7SFan Zhang 		if (atomic_read(&kvm->online_vcpus)) {
5794e0b1ab7SFan Zhang 			r = -EBUSY;
5804e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
5814e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
5824e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
5834e0b1ab7SFan Zhang 			r = 0;
5844e0b1ab7SFan Zhang 		}
5854e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
5864e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
5874e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
5884e0b1ab7SFan Zhang 		break;
589e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
590c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
591e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
592e44fc8c9SEkaterina Tumanova 		r = 0;
593e44fc8c9SEkaterina Tumanova 		break;
5946502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5956502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
5966502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
5976502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
5986502a34cSDavid Hildenbrand 		r = 0;
5996502a34cSDavid Hildenbrand 		break;
600d938dc55SCornelia Huck 	default:
601d938dc55SCornelia Huck 		r = -EINVAL;
602d938dc55SCornelia Huck 		break;
603d938dc55SCornelia Huck 	}
604d938dc55SCornelia Huck 	return r;
605d938dc55SCornelia Huck }
606d938dc55SCornelia Huck 
6078c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
6088c0a7ce6SDominik Dingel {
6098c0a7ce6SDominik Dingel 	int ret;
6108c0a7ce6SDominik Dingel 
6118c0a7ce6SDominik Dingel 	switch (attr->attr) {
6128c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
6138c0a7ce6SDominik Dingel 		ret = 0;
614c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
615a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
616a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
6178c0a7ce6SDominik Dingel 			ret = -EFAULT;
6188c0a7ce6SDominik Dingel 		break;
6198c0a7ce6SDominik Dingel 	default:
6208c0a7ce6SDominik Dingel 		ret = -ENXIO;
6218c0a7ce6SDominik Dingel 		break;
6228c0a7ce6SDominik Dingel 	}
6238c0a7ce6SDominik Dingel 	return ret;
6248c0a7ce6SDominik Dingel }
6258c0a7ce6SDominik Dingel 
6268c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
6274f718eabSDominik Dingel {
6284f718eabSDominik Dingel 	int ret;
6294f718eabSDominik Dingel 	unsigned int idx;
6304f718eabSDominik Dingel 	switch (attr->attr) {
6314f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
632f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
633c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
634e6db1d61SDominik Dingel 			break;
635e6db1d61SDominik Dingel 
6364f718eabSDominik Dingel 		ret = -EBUSY;
637c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
6384f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
639a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6404f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
6414f718eabSDominik Dingel 			ret = 0;
6424f718eabSDominik Dingel 		}
6434f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
6444f718eabSDominik Dingel 		break;
6454f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
646f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
647f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
648f9cbd9b0SDavid Hildenbrand 			break;
649c3489155SDominik Dingel 		ret = -EINVAL;
650c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
651c3489155SDominik Dingel 			break;
652c3489155SDominik Dingel 
653c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
6544f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
6554f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
656a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
6574f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
6584f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
6594f718eabSDominik Dingel 		ret = 0;
6604f718eabSDominik Dingel 		break;
6618c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
6628c0a7ce6SDominik Dingel 		unsigned long new_limit;
6638c0a7ce6SDominik Dingel 
6648c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
6658c0a7ce6SDominik Dingel 			return -EINVAL;
6668c0a7ce6SDominik Dingel 
6678c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
6688c0a7ce6SDominik Dingel 			return -EFAULT;
6698c0a7ce6SDominik Dingel 
670a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
671a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
6728c0a7ce6SDominik Dingel 			return -E2BIG;
6738c0a7ce6SDominik Dingel 
674a3a92c31SDominik Dingel 		if (!new_limit)
675a3a92c31SDominik Dingel 			return -EINVAL;
676a3a92c31SDominik Dingel 
6776ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
678a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
679a3a92c31SDominik Dingel 			new_limit -= 1;
680a3a92c31SDominik Dingel 
6818c0a7ce6SDominik Dingel 		ret = -EBUSY;
6828c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
683a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6846ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
6856ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
6868c0a7ce6SDominik Dingel 
6878c0a7ce6SDominik Dingel 			if (!new) {
6888c0a7ce6SDominik Dingel 				ret = -ENOMEM;
6898c0a7ce6SDominik Dingel 			} else {
6906ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
6918c0a7ce6SDominik Dingel 				new->private = kvm;
6928c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
6938c0a7ce6SDominik Dingel 				ret = 0;
6948c0a7ce6SDominik Dingel 			}
6958c0a7ce6SDominik Dingel 		}
6968c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
697a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
698a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
699a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
7008c0a7ce6SDominik Dingel 		break;
7018c0a7ce6SDominik Dingel 	}
7024f718eabSDominik Dingel 	default:
7034f718eabSDominik Dingel 		ret = -ENXIO;
7044f718eabSDominik Dingel 		break;
7054f718eabSDominik Dingel 	}
7064f718eabSDominik Dingel 	return ret;
7074f718eabSDominik Dingel }
7084f718eabSDominik Dingel 
709a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
710a374e892STony Krowiak 
711a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
712a374e892STony Krowiak {
713a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
714a374e892STony Krowiak 	int i;
715a374e892STony Krowiak 
7169d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
717a374e892STony Krowiak 		return -EINVAL;
718a374e892STony Krowiak 
719a374e892STony Krowiak 	mutex_lock(&kvm->lock);
720a374e892STony Krowiak 	switch (attr->attr) {
721a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
722a374e892STony Krowiak 		get_random_bytes(
723a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
724a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
725a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
726c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
727a374e892STony Krowiak 		break;
728a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
729a374e892STony Krowiak 		get_random_bytes(
730a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
731a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
732a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
733c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
734a374e892STony Krowiak 		break;
735a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
736a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
737a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
738a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
739c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
740a374e892STony Krowiak 		break;
741a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
742a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
743a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
744a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
745c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
746a374e892STony Krowiak 		break;
747a374e892STony Krowiak 	default:
748a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
749a374e892STony Krowiak 		return -ENXIO;
750a374e892STony Krowiak 	}
751a374e892STony Krowiak 
752a374e892STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm) {
753a374e892STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
754a374e892STony Krowiak 		exit_sie(vcpu);
755a374e892STony Krowiak 	}
756a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
757a374e892STony Krowiak 	return 0;
758a374e892STony Krowiak }
759a374e892STony Krowiak 
760190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
761190df4a2SClaudio Imbrenda {
762190df4a2SClaudio Imbrenda 	int cx;
763190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
764190df4a2SClaudio Imbrenda 
765190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
766190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
767190df4a2SClaudio Imbrenda }
768190df4a2SClaudio Imbrenda 
769190df4a2SClaudio Imbrenda /*
770190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
771190df4a2SClaudio Imbrenda  * kvm->lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
772190df4a2SClaudio Imbrenda  */
773190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
774190df4a2SClaudio Imbrenda {
775190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
776190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
777190df4a2SClaudio Imbrenda 	/* should be the only one */
778190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
779190df4a2SClaudio Imbrenda 	unsigned long ram_pages;
780190df4a2SClaudio Imbrenda 	int slotnr;
781190df4a2SClaudio Imbrenda 
782190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
783190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state)
784190df4a2SClaudio Imbrenda 		return 0;
785190df4a2SClaudio Imbrenda 
786190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
787190df4a2SClaudio Imbrenda 	if (!slots || !slots->used_slots)
788190df4a2SClaudio Imbrenda 		return -EINVAL;
789190df4a2SClaudio Imbrenda 
790190df4a2SClaudio Imbrenda 	mgs = kzalloc(sizeof(*mgs), GFP_KERNEL);
791190df4a2SClaudio Imbrenda 	if (!mgs)
792190df4a2SClaudio Imbrenda 		return -ENOMEM;
793190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = mgs;
794190df4a2SClaudio Imbrenda 
795190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
796190df4a2SClaudio Imbrenda 		/*
797190df4a2SClaudio Imbrenda 		 * Get the last slot. They should be sorted by base_gfn, so the
798190df4a2SClaudio Imbrenda 		 * last slot is also the one at the end of the address space.
799190df4a2SClaudio Imbrenda 		 * We have verified above that at least one slot is present.
800190df4a2SClaudio Imbrenda 		 */
801190df4a2SClaudio Imbrenda 		ms = slots->memslots + slots->used_slots - 1;
802190df4a2SClaudio Imbrenda 		/* round up so we only use full longs */
803190df4a2SClaudio Imbrenda 		ram_pages = roundup(ms->base_gfn + ms->npages, BITS_PER_LONG);
804190df4a2SClaudio Imbrenda 		/* allocate enough bytes to store all the bits */
805190df4a2SClaudio Imbrenda 		mgs->pgste_bitmap = vmalloc(ram_pages / 8);
806190df4a2SClaudio Imbrenda 		if (!mgs->pgste_bitmap) {
807190df4a2SClaudio Imbrenda 			kfree(mgs);
808190df4a2SClaudio Imbrenda 			kvm->arch.migration_state = NULL;
809190df4a2SClaudio Imbrenda 			return -ENOMEM;
810190df4a2SClaudio Imbrenda 		}
811190df4a2SClaudio Imbrenda 
812190df4a2SClaudio Imbrenda 		mgs->bitmap_size = ram_pages;
813190df4a2SClaudio Imbrenda 		atomic64_set(&mgs->dirty_pages, ram_pages);
814190df4a2SClaudio Imbrenda 		/* mark all the pages in active slots as dirty */
815190df4a2SClaudio Imbrenda 		for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
816190df4a2SClaudio Imbrenda 			ms = slots->memslots + slotnr;
817190df4a2SClaudio Imbrenda 			bitmap_set(mgs->pgste_bitmap, ms->base_gfn, ms->npages);
818190df4a2SClaudio Imbrenda 		}
819190df4a2SClaudio Imbrenda 
820190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
821190df4a2SClaudio Imbrenda 	}
822190df4a2SClaudio Imbrenda 	return 0;
823190df4a2SClaudio Imbrenda }
824190df4a2SClaudio Imbrenda 
825190df4a2SClaudio Imbrenda /*
826190df4a2SClaudio Imbrenda  * Must be called with kvm->lock to avoid races with ourselves and
827190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
828190df4a2SClaudio Imbrenda  */
829190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
830190df4a2SClaudio Imbrenda {
831190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
832190df4a2SClaudio Imbrenda 
833190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
834190df4a2SClaudio Imbrenda 	if (!kvm->arch.migration_state)
835190df4a2SClaudio Imbrenda 		return 0;
836190df4a2SClaudio Imbrenda 	mgs = kvm->arch.migration_state;
837190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = NULL;
838190df4a2SClaudio Imbrenda 
839190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
840190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
841190df4a2SClaudio Imbrenda 		vfree(mgs->pgste_bitmap);
842190df4a2SClaudio Imbrenda 	}
843190df4a2SClaudio Imbrenda 	kfree(mgs);
844190df4a2SClaudio Imbrenda 	return 0;
845190df4a2SClaudio Imbrenda }
846190df4a2SClaudio Imbrenda 
847190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
848190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
849190df4a2SClaudio Imbrenda {
850190df4a2SClaudio Imbrenda 	int idx, res = -ENXIO;
851190df4a2SClaudio Imbrenda 
852190df4a2SClaudio Imbrenda 	mutex_lock(&kvm->lock);
853190df4a2SClaudio Imbrenda 	switch (attr->attr) {
854190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
855190df4a2SClaudio Imbrenda 		idx = srcu_read_lock(&kvm->srcu);
856190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
857190df4a2SClaudio Imbrenda 		srcu_read_unlock(&kvm->srcu, idx);
858190df4a2SClaudio Imbrenda 		break;
859190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
860190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
861190df4a2SClaudio Imbrenda 		break;
862190df4a2SClaudio Imbrenda 	default:
863190df4a2SClaudio Imbrenda 		break;
864190df4a2SClaudio Imbrenda 	}
865190df4a2SClaudio Imbrenda 	mutex_unlock(&kvm->lock);
866190df4a2SClaudio Imbrenda 
867190df4a2SClaudio Imbrenda 	return res;
868190df4a2SClaudio Imbrenda }
869190df4a2SClaudio Imbrenda 
870190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
871190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
872190df4a2SClaudio Imbrenda {
873190df4a2SClaudio Imbrenda 	u64 mig = (kvm->arch.migration_state != NULL);
874190df4a2SClaudio Imbrenda 
875190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
876190df4a2SClaudio Imbrenda 		return -ENXIO;
877190df4a2SClaudio Imbrenda 
878190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
879190df4a2SClaudio Imbrenda 		return -EFAULT;
880190df4a2SClaudio Imbrenda 	return 0;
881190df4a2SClaudio Imbrenda }
882190df4a2SClaudio Imbrenda 
8838fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
8848fa1696eSCollin L. Walling {
8858fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
8868fa1696eSCollin L. Walling 
8878fa1696eSCollin L. Walling 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
8888fa1696eSCollin L. Walling 		return -EFAULT;
8898fa1696eSCollin L. Walling 
8908fa1696eSCollin L. Walling 	if (test_kvm_facility(kvm, 139))
8918fa1696eSCollin L. Walling 		kvm_s390_set_tod_clock_ext(kvm, &gtod);
8928fa1696eSCollin L. Walling 	else if (gtod.epoch_idx == 0)
8938fa1696eSCollin L. Walling 		kvm_s390_set_tod_clock(kvm, gtod.tod);
8948fa1696eSCollin L. Walling 	else
8958fa1696eSCollin L. Walling 		return -EINVAL;
8968fa1696eSCollin L. Walling 
8978fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
8988fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
8998fa1696eSCollin L. Walling 
9008fa1696eSCollin L. Walling 	return 0;
9018fa1696eSCollin L. Walling }
9028fa1696eSCollin L. Walling 
90372f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
90472f25020SJason J. Herne {
90572f25020SJason J. Herne 	u8 gtod_high;
90672f25020SJason J. Herne 
90772f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
90872f25020SJason J. Herne 					   sizeof(gtod_high)))
90972f25020SJason J. Herne 		return -EFAULT;
91072f25020SJason J. Herne 
91172f25020SJason J. Herne 	if (gtod_high != 0)
91272f25020SJason J. Herne 		return -EINVAL;
91358c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
91472f25020SJason J. Herne 
91572f25020SJason J. Herne 	return 0;
91672f25020SJason J. Herne }
91772f25020SJason J. Herne 
91872f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
91972f25020SJason J. Herne {
9205a3d883aSDavid Hildenbrand 	u64 gtod;
92172f25020SJason J. Herne 
92272f25020SJason J. Herne 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
92372f25020SJason J. Herne 		return -EFAULT;
92472f25020SJason J. Herne 
92525ed1675SDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, gtod);
92658c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
92772f25020SJason J. Herne 	return 0;
92872f25020SJason J. Herne }
92972f25020SJason J. Herne 
93072f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
93172f25020SJason J. Herne {
93272f25020SJason J. Herne 	int ret;
93372f25020SJason J. Herne 
93472f25020SJason J. Herne 	if (attr->flags)
93572f25020SJason J. Herne 		return -EINVAL;
93672f25020SJason J. Herne 
93772f25020SJason J. Herne 	switch (attr->attr) {
9388fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
9398fa1696eSCollin L. Walling 		ret = kvm_s390_set_tod_ext(kvm, attr);
9408fa1696eSCollin L. Walling 		break;
94172f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
94272f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
94372f25020SJason J. Herne 		break;
94472f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
94572f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
94672f25020SJason J. Herne 		break;
94772f25020SJason J. Herne 	default:
94872f25020SJason J. Herne 		ret = -ENXIO;
94972f25020SJason J. Herne 		break;
95072f25020SJason J. Herne 	}
95172f25020SJason J. Herne 	return ret;
95272f25020SJason J. Herne }
95372f25020SJason J. Herne 
9548fa1696eSCollin L. Walling static void kvm_s390_get_tod_clock_ext(struct kvm *kvm,
9558fa1696eSCollin L. Walling 					struct kvm_s390_vm_tod_clock *gtod)
9568fa1696eSCollin L. Walling {
9578fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
9588fa1696eSCollin L. Walling 
9598fa1696eSCollin L. Walling 	preempt_disable();
9608fa1696eSCollin L. Walling 
9618fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
9628fa1696eSCollin L. Walling 
9638fa1696eSCollin L. Walling 	gtod->tod = htod.tod + kvm->arch.epoch;
9648fa1696eSCollin L. Walling 	gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx;
9658fa1696eSCollin L. Walling 
9668fa1696eSCollin L. Walling 	if (gtod->tod < htod.tod)
9678fa1696eSCollin L. Walling 		gtod->epoch_idx += 1;
9688fa1696eSCollin L. Walling 
9698fa1696eSCollin L. Walling 	preempt_enable();
9708fa1696eSCollin L. Walling }
9718fa1696eSCollin L. Walling 
9728fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
9738fa1696eSCollin L. Walling {
9748fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
9758fa1696eSCollin L. Walling 
9768fa1696eSCollin L. Walling 	memset(&gtod, 0, sizeof(gtod));
9778fa1696eSCollin L. Walling 
9788fa1696eSCollin L. Walling 	if (test_kvm_facility(kvm, 139))
9798fa1696eSCollin L. Walling 		kvm_s390_get_tod_clock_ext(kvm, &gtod);
9808fa1696eSCollin L. Walling 	else
9818fa1696eSCollin L. Walling 		gtod.tod = kvm_s390_get_tod_clock_fast(kvm);
9828fa1696eSCollin L. Walling 
9838fa1696eSCollin L. Walling 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
9848fa1696eSCollin L. Walling 		return -EFAULT;
9858fa1696eSCollin L. Walling 
9868fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
9878fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
9888fa1696eSCollin L. Walling 	return 0;
9898fa1696eSCollin L. Walling }
9908fa1696eSCollin L. Walling 
99172f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
99272f25020SJason J. Herne {
99372f25020SJason J. Herne 	u8 gtod_high = 0;
99472f25020SJason J. Herne 
99572f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
99672f25020SJason J. Herne 					 sizeof(gtod_high)))
99772f25020SJason J. Herne 		return -EFAULT;
99858c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
99972f25020SJason J. Herne 
100072f25020SJason J. Herne 	return 0;
100172f25020SJason J. Herne }
100272f25020SJason J. Herne 
100372f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
100472f25020SJason J. Herne {
10055a3d883aSDavid Hildenbrand 	u64 gtod;
100672f25020SJason J. Herne 
100760417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
100872f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
100972f25020SJason J. Herne 		return -EFAULT;
101058c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
101172f25020SJason J. Herne 
101272f25020SJason J. Herne 	return 0;
101372f25020SJason J. Herne }
101472f25020SJason J. Herne 
101572f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
101672f25020SJason J. Herne {
101772f25020SJason J. Herne 	int ret;
101872f25020SJason J. Herne 
101972f25020SJason J. Herne 	if (attr->flags)
102072f25020SJason J. Herne 		return -EINVAL;
102172f25020SJason J. Herne 
102272f25020SJason J. Herne 	switch (attr->attr) {
10238fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
10248fa1696eSCollin L. Walling 		ret = kvm_s390_get_tod_ext(kvm, attr);
10258fa1696eSCollin L. Walling 		break;
102672f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
102772f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
102872f25020SJason J. Herne 		break;
102972f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
103072f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
103172f25020SJason J. Herne 		break;
103272f25020SJason J. Herne 	default:
103372f25020SJason J. Herne 		ret = -ENXIO;
103472f25020SJason J. Herne 		break;
103572f25020SJason J. Herne 	}
103672f25020SJason J. Herne 	return ret;
103772f25020SJason J. Herne }
103872f25020SJason J. Herne 
1039658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1040658b6edaSMichael Mueller {
1041658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1042053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
1043658b6edaSMichael Mueller 	int ret = 0;
1044658b6edaSMichael Mueller 
1045658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
1046a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
1047658b6edaSMichael Mueller 		ret = -EBUSY;
1048658b6edaSMichael Mueller 		goto out;
1049658b6edaSMichael Mueller 	}
1050658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1051658b6edaSMichael Mueller 	if (!proc) {
1052658b6edaSMichael Mueller 		ret = -ENOMEM;
1053658b6edaSMichael Mueller 		goto out;
1054658b6edaSMichael Mueller 	}
1055658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
1056658b6edaSMichael Mueller 			    sizeof(*proc))) {
10579bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
1058053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1059053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
10600487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
1061053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
1062053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
1063053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
1064053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
1065053dd230SDavid Hildenbrand 			else
1066658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
1067053dd230SDavid Hildenbrand 		}
1068c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1069658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1070a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1071a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1072a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1073a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1074a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1075a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1076a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1077658b6edaSMichael Mueller 	} else
1078658b6edaSMichael Mueller 		ret = -EFAULT;
1079658b6edaSMichael Mueller 	kfree(proc);
1080658b6edaSMichael Mueller out:
1081658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1082658b6edaSMichael Mueller 	return ret;
1083658b6edaSMichael Mueller }
1084658b6edaSMichael Mueller 
108515c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
108615c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
108715c9705fSDavid Hildenbrand {
108815c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
108915c9705fSDavid Hildenbrand 	int ret = -EBUSY;
109015c9705fSDavid Hildenbrand 
109115c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
109215c9705fSDavid Hildenbrand 		return -EFAULT;
109315c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
109415c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
109515c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
109615c9705fSDavid Hildenbrand 		return -EINVAL;
109715c9705fSDavid Hildenbrand 
109815c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
109915c9705fSDavid Hildenbrand 	if (!atomic_read(&kvm->online_vcpus)) {
110015c9705fSDavid Hildenbrand 		bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
110115c9705fSDavid Hildenbrand 			    KVM_S390_VM_CPU_FEAT_NR_BITS);
110215c9705fSDavid Hildenbrand 		ret = 0;
110315c9705fSDavid Hildenbrand 	}
110415c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
110515c9705fSDavid Hildenbrand 	return ret;
110615c9705fSDavid Hildenbrand }
110715c9705fSDavid Hildenbrand 
11080a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
11090a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
11100a763c78SDavid Hildenbrand {
11110a763c78SDavid Hildenbrand 	/*
11120a763c78SDavid Hildenbrand 	 * Once supported by kernel + hw, we have to store the subfunctions
11130a763c78SDavid Hildenbrand 	 * in kvm->arch and remember that user space configured them.
11140a763c78SDavid Hildenbrand 	 */
11150a763c78SDavid Hildenbrand 	return -ENXIO;
11160a763c78SDavid Hildenbrand }
11170a763c78SDavid Hildenbrand 
1118658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1119658b6edaSMichael Mueller {
1120658b6edaSMichael Mueller 	int ret = -ENXIO;
1121658b6edaSMichael Mueller 
1122658b6edaSMichael Mueller 	switch (attr->attr) {
1123658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1124658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1125658b6edaSMichael Mueller 		break;
112615c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
112715c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
112815c9705fSDavid Hildenbrand 		break;
11290a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
11300a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
11310a763c78SDavid Hildenbrand 		break;
1132658b6edaSMichael Mueller 	}
1133658b6edaSMichael Mueller 	return ret;
1134658b6edaSMichael Mueller }
1135658b6edaSMichael Mueller 
1136658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1137658b6edaSMichael Mueller {
1138658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1139658b6edaSMichael Mueller 	int ret = 0;
1140658b6edaSMichael Mueller 
1141658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1142658b6edaSMichael Mueller 	if (!proc) {
1143658b6edaSMichael Mueller 		ret = -ENOMEM;
1144658b6edaSMichael Mueller 		goto out;
1145658b6edaSMichael Mueller 	}
11469bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1147658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1148c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1149c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1150a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1151a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1152a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1153a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1154a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1155a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1156a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1157658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1158658b6edaSMichael Mueller 		ret = -EFAULT;
1159658b6edaSMichael Mueller 	kfree(proc);
1160658b6edaSMichael Mueller out:
1161658b6edaSMichael Mueller 	return ret;
1162658b6edaSMichael Mueller }
1163658b6edaSMichael Mueller 
1164658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1165658b6edaSMichael Mueller {
1166658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1167658b6edaSMichael Mueller 	int ret = 0;
1168658b6edaSMichael Mueller 
1169658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1170658b6edaSMichael Mueller 	if (!mach) {
1171658b6edaSMichael Mueller 		ret = -ENOMEM;
1172658b6edaSMichael Mueller 		goto out;
1173658b6edaSMichael Mueller 	}
1174658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
117537c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1176c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1177981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1178658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
117904478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
1180a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1181a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1182a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1183a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1184a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1185a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1186a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1187a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1188a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1189a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1190a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1191658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1192658b6edaSMichael Mueller 		ret = -EFAULT;
1193658b6edaSMichael Mueller 	kfree(mach);
1194658b6edaSMichael Mueller out:
1195658b6edaSMichael Mueller 	return ret;
1196658b6edaSMichael Mueller }
1197658b6edaSMichael Mueller 
119815c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
119915c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
120015c9705fSDavid Hildenbrand {
120115c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
120215c9705fSDavid Hildenbrand 
120315c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
120415c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
120515c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
120615c9705fSDavid Hildenbrand 		return -EFAULT;
120715c9705fSDavid Hildenbrand 	return 0;
120815c9705fSDavid Hildenbrand }
120915c9705fSDavid Hildenbrand 
121015c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
121115c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
121215c9705fSDavid Hildenbrand {
121315c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
121415c9705fSDavid Hildenbrand 
121515c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
121615c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
121715c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
121815c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
121915c9705fSDavid Hildenbrand 		return -EFAULT;
122015c9705fSDavid Hildenbrand 	return 0;
122115c9705fSDavid Hildenbrand }
122215c9705fSDavid Hildenbrand 
12230a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
12240a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
12250a763c78SDavid Hildenbrand {
12260a763c78SDavid Hildenbrand 	/*
12270a763c78SDavid Hildenbrand 	 * Once we can actually configure subfunctions (kernel + hw support),
12280a763c78SDavid Hildenbrand 	 * we have to check if they were already set by user space, if so copy
12290a763c78SDavid Hildenbrand 	 * them from kvm->arch.
12300a763c78SDavid Hildenbrand 	 */
12310a763c78SDavid Hildenbrand 	return -ENXIO;
12320a763c78SDavid Hildenbrand }
12330a763c78SDavid Hildenbrand 
12340a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
12350a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
12360a763c78SDavid Hildenbrand {
12370a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
12380a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
12390a763c78SDavid Hildenbrand 		return -EFAULT;
12400a763c78SDavid Hildenbrand 	return 0;
12410a763c78SDavid Hildenbrand }
1242658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1243658b6edaSMichael Mueller {
1244658b6edaSMichael Mueller 	int ret = -ENXIO;
1245658b6edaSMichael Mueller 
1246658b6edaSMichael Mueller 	switch (attr->attr) {
1247658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1248658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1249658b6edaSMichael Mueller 		break;
1250658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1251658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1252658b6edaSMichael Mueller 		break;
125315c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
125415c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
125515c9705fSDavid Hildenbrand 		break;
125615c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
125715c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
125815c9705fSDavid Hildenbrand 		break;
12590a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
12600a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
12610a763c78SDavid Hildenbrand 		break;
12620a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
12630a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
12640a763c78SDavid Hildenbrand 		break;
1265658b6edaSMichael Mueller 	}
1266658b6edaSMichael Mueller 	return ret;
1267658b6edaSMichael Mueller }
1268658b6edaSMichael Mueller 
1269f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1270f2061656SDominik Dingel {
1271f2061656SDominik Dingel 	int ret;
1272f2061656SDominik Dingel 
1273f2061656SDominik Dingel 	switch (attr->group) {
12744f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
12758c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
12764f718eabSDominik Dingel 		break;
127772f25020SJason J. Herne 	case KVM_S390_VM_TOD:
127872f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
127972f25020SJason J. Herne 		break;
1280658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1281658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1282658b6edaSMichael Mueller 		break;
1283a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1284a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1285a374e892STony Krowiak 		break;
1286190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1287190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1288190df4a2SClaudio Imbrenda 		break;
1289f2061656SDominik Dingel 	default:
1290f2061656SDominik Dingel 		ret = -ENXIO;
1291f2061656SDominik Dingel 		break;
1292f2061656SDominik Dingel 	}
1293f2061656SDominik Dingel 
1294f2061656SDominik Dingel 	return ret;
1295f2061656SDominik Dingel }
1296f2061656SDominik Dingel 
1297f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1298f2061656SDominik Dingel {
12998c0a7ce6SDominik Dingel 	int ret;
13008c0a7ce6SDominik Dingel 
13018c0a7ce6SDominik Dingel 	switch (attr->group) {
13028c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
13038c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
13048c0a7ce6SDominik Dingel 		break;
130572f25020SJason J. Herne 	case KVM_S390_VM_TOD:
130672f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
130772f25020SJason J. Herne 		break;
1308658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1309658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1310658b6edaSMichael Mueller 		break;
1311190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1312190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1313190df4a2SClaudio Imbrenda 		break;
13148c0a7ce6SDominik Dingel 	default:
13158c0a7ce6SDominik Dingel 		ret = -ENXIO;
13168c0a7ce6SDominik Dingel 		break;
13178c0a7ce6SDominik Dingel 	}
13188c0a7ce6SDominik Dingel 
13198c0a7ce6SDominik Dingel 	return ret;
1320f2061656SDominik Dingel }
1321f2061656SDominik Dingel 
1322f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1323f2061656SDominik Dingel {
1324f2061656SDominik Dingel 	int ret;
1325f2061656SDominik Dingel 
1326f2061656SDominik Dingel 	switch (attr->group) {
13274f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
13284f718eabSDominik Dingel 		switch (attr->attr) {
13294f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
13304f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1331f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1332f9cbd9b0SDavid Hildenbrand 			break;
13338c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
13344f718eabSDominik Dingel 			ret = 0;
13354f718eabSDominik Dingel 			break;
13364f718eabSDominik Dingel 		default:
13374f718eabSDominik Dingel 			ret = -ENXIO;
13384f718eabSDominik Dingel 			break;
13394f718eabSDominik Dingel 		}
13404f718eabSDominik Dingel 		break;
134172f25020SJason J. Herne 	case KVM_S390_VM_TOD:
134272f25020SJason J. Herne 		switch (attr->attr) {
134372f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
134472f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
134572f25020SJason J. Herne 			ret = 0;
134672f25020SJason J. Herne 			break;
134772f25020SJason J. Herne 		default:
134872f25020SJason J. Herne 			ret = -ENXIO;
134972f25020SJason J. Herne 			break;
135072f25020SJason J. Herne 		}
135172f25020SJason J. Herne 		break;
1352658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1353658b6edaSMichael Mueller 		switch (attr->attr) {
1354658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1355658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
135615c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
135715c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
13580a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1359658b6edaSMichael Mueller 			ret = 0;
1360658b6edaSMichael Mueller 			break;
13610a763c78SDavid Hildenbrand 		/* configuring subfunctions is not supported yet */
13620a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1363658b6edaSMichael Mueller 		default:
1364658b6edaSMichael Mueller 			ret = -ENXIO;
1365658b6edaSMichael Mueller 			break;
1366658b6edaSMichael Mueller 		}
1367658b6edaSMichael Mueller 		break;
1368a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1369a374e892STony Krowiak 		switch (attr->attr) {
1370a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1371a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1372a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1373a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1374a374e892STony Krowiak 			ret = 0;
1375a374e892STony Krowiak 			break;
1376a374e892STony Krowiak 		default:
1377a374e892STony Krowiak 			ret = -ENXIO;
1378a374e892STony Krowiak 			break;
1379a374e892STony Krowiak 		}
1380a374e892STony Krowiak 		break;
1381190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1382190df4a2SClaudio Imbrenda 		ret = 0;
1383190df4a2SClaudio Imbrenda 		break;
1384f2061656SDominik Dingel 	default:
1385f2061656SDominik Dingel 		ret = -ENXIO;
1386f2061656SDominik Dingel 		break;
1387f2061656SDominik Dingel 	}
1388f2061656SDominik Dingel 
1389f2061656SDominik Dingel 	return ret;
1390f2061656SDominik Dingel }
1391f2061656SDominik Dingel 
139230ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
139330ee2a98SJason J. Herne {
139430ee2a98SJason J. Herne 	uint8_t *keys;
139530ee2a98SJason J. Herne 	uint64_t hva;
13964f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
139730ee2a98SJason J. Herne 
139830ee2a98SJason J. Herne 	if (args->flags != 0)
139930ee2a98SJason J. Herne 		return -EINVAL;
140030ee2a98SJason J. Herne 
140130ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
140230ee2a98SJason J. Herne 	if (!mm_use_skey(current->mm))
140330ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
140430ee2a98SJason J. Herne 
140530ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
140630ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
140730ee2a98SJason J. Herne 		return -EINVAL;
140830ee2a98SJason J. Herne 
1409752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
141030ee2a98SJason J. Herne 	if (!keys)
141130ee2a98SJason J. Herne 		return -ENOMEM;
141230ee2a98SJason J. Herne 
1413d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
14144f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
141530ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
141630ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
141730ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
141830ee2a98SJason J. Herne 			r = -EFAULT;
1419d3ed1ceeSMartin Schwidefsky 			break;
142030ee2a98SJason J. Herne 		}
142130ee2a98SJason J. Herne 
1422154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1423154c8c19SDavid Hildenbrand 		if (r)
1424d3ed1ceeSMartin Schwidefsky 			break;
142530ee2a98SJason J. Herne 	}
14264f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1427d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
142830ee2a98SJason J. Herne 
1429d3ed1ceeSMartin Schwidefsky 	if (!r) {
143030ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
143130ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
143230ee2a98SJason J. Herne 		if (r)
143330ee2a98SJason J. Herne 			r = -EFAULT;
1434d3ed1ceeSMartin Schwidefsky 	}
1435d3ed1ceeSMartin Schwidefsky 
143630ee2a98SJason J. Herne 	kvfree(keys);
143730ee2a98SJason J. Herne 	return r;
143830ee2a98SJason J. Herne }
143930ee2a98SJason J. Herne 
144030ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
144130ee2a98SJason J. Herne {
144230ee2a98SJason J. Herne 	uint8_t *keys;
144330ee2a98SJason J. Herne 	uint64_t hva;
14444f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
144530ee2a98SJason J. Herne 
144630ee2a98SJason J. Herne 	if (args->flags != 0)
144730ee2a98SJason J. Herne 		return -EINVAL;
144830ee2a98SJason J. Herne 
144930ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
145030ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
145130ee2a98SJason J. Herne 		return -EINVAL;
145230ee2a98SJason J. Herne 
1453752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
145430ee2a98SJason J. Herne 	if (!keys)
145530ee2a98SJason J. Herne 		return -ENOMEM;
145630ee2a98SJason J. Herne 
145730ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
145830ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
145930ee2a98SJason J. Herne 	if (r) {
146030ee2a98SJason J. Herne 		r = -EFAULT;
146130ee2a98SJason J. Herne 		goto out;
146230ee2a98SJason J. Herne 	}
146330ee2a98SJason J. Herne 
146430ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
146514d4a425SDominik Dingel 	r = s390_enable_skey();
146614d4a425SDominik Dingel 	if (r)
146714d4a425SDominik Dingel 		goto out;
146830ee2a98SJason J. Herne 
1469d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
14704f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
147130ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
147230ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
147330ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
147430ee2a98SJason J. Herne 			r = -EFAULT;
1475d3ed1ceeSMartin Schwidefsky 			break;
147630ee2a98SJason J. Herne 		}
147730ee2a98SJason J. Herne 
147830ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
147930ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
148030ee2a98SJason J. Herne 			r = -EINVAL;
1481d3ed1ceeSMartin Schwidefsky 			break;
148230ee2a98SJason J. Herne 		}
148330ee2a98SJason J. Herne 
1484fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
148530ee2a98SJason J. Herne 		if (r)
1486d3ed1ceeSMartin Schwidefsky 			break;
148730ee2a98SJason J. Herne 	}
14884f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1489d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
149030ee2a98SJason J. Herne out:
149130ee2a98SJason J. Herne 	kvfree(keys);
149230ee2a98SJason J. Herne 	return r;
149330ee2a98SJason J. Herne }
149430ee2a98SJason J. Herne 
14954036e387SClaudio Imbrenda /*
14964036e387SClaudio Imbrenda  * Base address and length must be sent at the start of each block, therefore
14974036e387SClaudio Imbrenda  * it's cheaper to send some clean data, as long as it's less than the size of
14984036e387SClaudio Imbrenda  * two longs.
14994036e387SClaudio Imbrenda  */
15004036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
15014036e387SClaudio Imbrenda /* for consistency */
15024036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
15034036e387SClaudio Imbrenda 
15044036e387SClaudio Imbrenda /*
15054036e387SClaudio Imbrenda  * This function searches for the next page with dirty CMMA attributes, and
15064036e387SClaudio Imbrenda  * saves the attributes in the buffer up to either the end of the buffer or
15074036e387SClaudio Imbrenda  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
15084036e387SClaudio Imbrenda  * no trailing clean bytes are saved.
15094036e387SClaudio Imbrenda  * In case no dirty bits were found, or if CMMA was not enabled or used, the
15104036e387SClaudio Imbrenda  * output buffer will indicate 0 as length.
15114036e387SClaudio Imbrenda  */
15124036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
15134036e387SClaudio Imbrenda 				  struct kvm_s390_cmma_log *args)
15144036e387SClaudio Imbrenda {
15154036e387SClaudio Imbrenda 	struct kvm_s390_migration_state *s = kvm->arch.migration_state;
15164036e387SClaudio Imbrenda 	unsigned long bufsize, hva, pgstev, i, next, cur;
15174036e387SClaudio Imbrenda 	int srcu_idx, peek, r = 0, rr;
15184036e387SClaudio Imbrenda 	u8 *res;
15194036e387SClaudio Imbrenda 
15204036e387SClaudio Imbrenda 	cur = args->start_gfn;
15214036e387SClaudio Imbrenda 	i = next = pgstev = 0;
15224036e387SClaudio Imbrenda 
15234036e387SClaudio Imbrenda 	if (unlikely(!kvm->arch.use_cmma))
15244036e387SClaudio Imbrenda 		return -ENXIO;
15254036e387SClaudio Imbrenda 	/* Invalid/unsupported flags were specified */
15264036e387SClaudio Imbrenda 	if (args->flags & ~KVM_S390_CMMA_PEEK)
15274036e387SClaudio Imbrenda 		return -EINVAL;
15284036e387SClaudio Imbrenda 	/* Migration mode query, and we are not doing a migration */
15294036e387SClaudio Imbrenda 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
15304036e387SClaudio Imbrenda 	if (!peek && !s)
15314036e387SClaudio Imbrenda 		return -EINVAL;
15324036e387SClaudio Imbrenda 	/* CMMA is disabled or was not used, or the buffer has length zero */
15334036e387SClaudio Imbrenda 	bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
15344036e387SClaudio Imbrenda 	if (!bufsize || !kvm->mm->context.use_cmma) {
15354036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
15364036e387SClaudio Imbrenda 		return 0;
15374036e387SClaudio Imbrenda 	}
15384036e387SClaudio Imbrenda 
15394036e387SClaudio Imbrenda 	if (!peek) {
15404036e387SClaudio Imbrenda 		/* We are not peeking, and there are no dirty pages */
15414036e387SClaudio Imbrenda 		if (!atomic64_read(&s->dirty_pages)) {
15424036e387SClaudio Imbrenda 			memset(args, 0, sizeof(*args));
15434036e387SClaudio Imbrenda 			return 0;
15444036e387SClaudio Imbrenda 		}
15454036e387SClaudio Imbrenda 		cur = find_next_bit(s->pgste_bitmap, s->bitmap_size,
15464036e387SClaudio Imbrenda 				    args->start_gfn);
15474036e387SClaudio Imbrenda 		if (cur >= s->bitmap_size)	/* nothing found, loop back */
15484036e387SClaudio Imbrenda 			cur = find_next_bit(s->pgste_bitmap, s->bitmap_size, 0);
15494036e387SClaudio Imbrenda 		if (cur >= s->bitmap_size) {	/* again! (very unlikely) */
15504036e387SClaudio Imbrenda 			memset(args, 0, sizeof(*args));
15514036e387SClaudio Imbrenda 			return 0;
15524036e387SClaudio Imbrenda 		}
15534036e387SClaudio Imbrenda 		next = find_next_bit(s->pgste_bitmap, s->bitmap_size, cur + 1);
15544036e387SClaudio Imbrenda 	}
15554036e387SClaudio Imbrenda 
15564036e387SClaudio Imbrenda 	res = vmalloc(bufsize);
15574036e387SClaudio Imbrenda 	if (!res)
15584036e387SClaudio Imbrenda 		return -ENOMEM;
15594036e387SClaudio Imbrenda 
15604036e387SClaudio Imbrenda 	args->start_gfn = cur;
15614036e387SClaudio Imbrenda 
15624036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
15634036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
15644036e387SClaudio Imbrenda 	while (i < bufsize) {
15654036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur);
15664036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
15674036e387SClaudio Imbrenda 			r = -EFAULT;
15684036e387SClaudio Imbrenda 			break;
15694036e387SClaudio Imbrenda 		}
15704036e387SClaudio Imbrenda 		/* decrement only if we actually flipped the bit to 0 */
15714036e387SClaudio Imbrenda 		if (!peek && test_and_clear_bit(cur, s->pgste_bitmap))
15724036e387SClaudio Imbrenda 			atomic64_dec(&s->dirty_pages);
15734036e387SClaudio Imbrenda 		r = get_pgste(kvm->mm, hva, &pgstev);
15744036e387SClaudio Imbrenda 		if (r < 0)
15754036e387SClaudio Imbrenda 			pgstev = 0;
15764036e387SClaudio Imbrenda 		/* save the value */
15771bab1c02SClaudio Imbrenda 		res[i++] = (pgstev >> 24) & 0x43;
15784036e387SClaudio Imbrenda 		/*
15794036e387SClaudio Imbrenda 		 * if the next bit is too far away, stop.
15804036e387SClaudio Imbrenda 		 * if we reached the previous "next", find the next one
15814036e387SClaudio Imbrenda 		 */
15824036e387SClaudio Imbrenda 		if (!peek) {
15834036e387SClaudio Imbrenda 			if (next > cur + KVM_S390_MAX_BIT_DISTANCE)
15844036e387SClaudio Imbrenda 				break;
15854036e387SClaudio Imbrenda 			if (cur == next)
15864036e387SClaudio Imbrenda 				next = find_next_bit(s->pgste_bitmap,
15874036e387SClaudio Imbrenda 						     s->bitmap_size, cur + 1);
15884036e387SClaudio Imbrenda 		/* reached the end of the bitmap or of the buffer, stop */
15894036e387SClaudio Imbrenda 			if ((next >= s->bitmap_size) ||
15904036e387SClaudio Imbrenda 			    (next >= args->start_gfn + bufsize))
15914036e387SClaudio Imbrenda 				break;
15924036e387SClaudio Imbrenda 		}
15934036e387SClaudio Imbrenda 		cur++;
15944036e387SClaudio Imbrenda 	}
15954036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
15964036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
15974036e387SClaudio Imbrenda 	args->count = i;
15984036e387SClaudio Imbrenda 	args->remaining = s ? atomic64_read(&s->dirty_pages) : 0;
15994036e387SClaudio Imbrenda 
16004036e387SClaudio Imbrenda 	rr = copy_to_user((void __user *)args->values, res, args->count);
16014036e387SClaudio Imbrenda 	if (rr)
16024036e387SClaudio Imbrenda 		r = -EFAULT;
16034036e387SClaudio Imbrenda 
16044036e387SClaudio Imbrenda 	vfree(res);
16054036e387SClaudio Imbrenda 	return r;
16064036e387SClaudio Imbrenda }
16074036e387SClaudio Imbrenda 
16084036e387SClaudio Imbrenda /*
16094036e387SClaudio Imbrenda  * This function sets the CMMA attributes for the given pages. If the input
16104036e387SClaudio Imbrenda  * buffer has zero length, no action is taken, otherwise the attributes are
16114036e387SClaudio Imbrenda  * set and the mm->context.use_cmma flag is set.
16124036e387SClaudio Imbrenda  */
16134036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
16144036e387SClaudio Imbrenda 				  const struct kvm_s390_cmma_log *args)
16154036e387SClaudio Imbrenda {
16164036e387SClaudio Imbrenda 	unsigned long hva, mask, pgstev, i;
16174036e387SClaudio Imbrenda 	uint8_t *bits;
16184036e387SClaudio Imbrenda 	int srcu_idx, r = 0;
16194036e387SClaudio Imbrenda 
16204036e387SClaudio Imbrenda 	mask = args->mask;
16214036e387SClaudio Imbrenda 
16224036e387SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
16234036e387SClaudio Imbrenda 		return -ENXIO;
16244036e387SClaudio Imbrenda 	/* invalid/unsupported flags */
16254036e387SClaudio Imbrenda 	if (args->flags != 0)
16264036e387SClaudio Imbrenda 		return -EINVAL;
16274036e387SClaudio Imbrenda 	/* Enforce sane limit on memory allocation */
16284036e387SClaudio Imbrenda 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
16294036e387SClaudio Imbrenda 		return -EINVAL;
16304036e387SClaudio Imbrenda 	/* Nothing to do */
16314036e387SClaudio Imbrenda 	if (args->count == 0)
16324036e387SClaudio Imbrenda 		return 0;
16334036e387SClaudio Imbrenda 
16344036e387SClaudio Imbrenda 	bits = vmalloc(sizeof(*bits) * args->count);
16354036e387SClaudio Imbrenda 	if (!bits)
16364036e387SClaudio Imbrenda 		return -ENOMEM;
16374036e387SClaudio Imbrenda 
16384036e387SClaudio Imbrenda 	r = copy_from_user(bits, (void __user *)args->values, args->count);
16394036e387SClaudio Imbrenda 	if (r) {
16404036e387SClaudio Imbrenda 		r = -EFAULT;
16414036e387SClaudio Imbrenda 		goto out;
16424036e387SClaudio Imbrenda 	}
16434036e387SClaudio Imbrenda 
16444036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
16454036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
16464036e387SClaudio Imbrenda 	for (i = 0; i < args->count; i++) {
16474036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, args->start_gfn + i);
16484036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
16494036e387SClaudio Imbrenda 			r = -EFAULT;
16504036e387SClaudio Imbrenda 			break;
16514036e387SClaudio Imbrenda 		}
16524036e387SClaudio Imbrenda 
16534036e387SClaudio Imbrenda 		pgstev = bits[i];
16544036e387SClaudio Imbrenda 		pgstev = pgstev << 24;
16551bab1c02SClaudio Imbrenda 		mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
16564036e387SClaudio Imbrenda 		set_pgste_bits(kvm->mm, hva, mask, pgstev);
16574036e387SClaudio Imbrenda 	}
16584036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
16594036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
16604036e387SClaudio Imbrenda 
16614036e387SClaudio Imbrenda 	if (!kvm->mm->context.use_cmma) {
16624036e387SClaudio Imbrenda 		down_write(&kvm->mm->mmap_sem);
16634036e387SClaudio Imbrenda 		kvm->mm->context.use_cmma = 1;
16644036e387SClaudio Imbrenda 		up_write(&kvm->mm->mmap_sem);
16654036e387SClaudio Imbrenda 	}
16664036e387SClaudio Imbrenda out:
16674036e387SClaudio Imbrenda 	vfree(bits);
16684036e387SClaudio Imbrenda 	return r;
16694036e387SClaudio Imbrenda }
16704036e387SClaudio Imbrenda 
1671b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
1672b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
1673b0c632dbSHeiko Carstens {
1674b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
1675b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
1676f2061656SDominik Dingel 	struct kvm_device_attr attr;
1677b0c632dbSHeiko Carstens 	int r;
1678b0c632dbSHeiko Carstens 
1679b0c632dbSHeiko Carstens 	switch (ioctl) {
1680ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
1681ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
1682ba5c1e9bSCarsten Otte 
1683ba5c1e9bSCarsten Otte 		r = -EFAULT;
1684ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
1685ba5c1e9bSCarsten Otte 			break;
1686ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
1687ba5c1e9bSCarsten Otte 		break;
1688ba5c1e9bSCarsten Otte 	}
1689d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
1690d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
1691d938dc55SCornelia Huck 		r = -EFAULT;
1692d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1693d938dc55SCornelia Huck 			break;
1694d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1695d938dc55SCornelia Huck 		break;
1696d938dc55SCornelia Huck 	}
169784223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
169884223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
169984223598SCornelia Huck 
170084223598SCornelia Huck 		r = -EINVAL;
170184223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
170284223598SCornelia Huck 			/* Set up dummy routing. */
170384223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
1704152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
170584223598SCornelia Huck 		}
170684223598SCornelia Huck 		break;
170784223598SCornelia Huck 	}
1708f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
1709f2061656SDominik Dingel 		r = -EFAULT;
1710f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1711f2061656SDominik Dingel 			break;
1712f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
1713f2061656SDominik Dingel 		break;
1714f2061656SDominik Dingel 	}
1715f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
1716f2061656SDominik Dingel 		r = -EFAULT;
1717f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1718f2061656SDominik Dingel 			break;
1719f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
1720f2061656SDominik Dingel 		break;
1721f2061656SDominik Dingel 	}
1722f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
1723f2061656SDominik Dingel 		r = -EFAULT;
1724f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1725f2061656SDominik Dingel 			break;
1726f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
1727f2061656SDominik Dingel 		break;
1728f2061656SDominik Dingel 	}
172930ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
173030ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
173130ee2a98SJason J. Herne 
173230ee2a98SJason J. Herne 		r = -EFAULT;
173330ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
173430ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
173530ee2a98SJason J. Herne 			break;
173630ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
173730ee2a98SJason J. Herne 		break;
173830ee2a98SJason J. Herne 	}
173930ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
174030ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
174130ee2a98SJason J. Herne 
174230ee2a98SJason J. Herne 		r = -EFAULT;
174330ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
174430ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
174530ee2a98SJason J. Herne 			break;
174630ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
174730ee2a98SJason J. Herne 		break;
174830ee2a98SJason J. Herne 	}
17494036e387SClaudio Imbrenda 	case KVM_S390_GET_CMMA_BITS: {
17504036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
17514036e387SClaudio Imbrenda 
17524036e387SClaudio Imbrenda 		r = -EFAULT;
17534036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
17544036e387SClaudio Imbrenda 			break;
17554036e387SClaudio Imbrenda 		r = kvm_s390_get_cmma_bits(kvm, &args);
17564036e387SClaudio Imbrenda 		if (!r) {
17574036e387SClaudio Imbrenda 			r = copy_to_user(argp, &args, sizeof(args));
17584036e387SClaudio Imbrenda 			if (r)
17594036e387SClaudio Imbrenda 				r = -EFAULT;
17604036e387SClaudio Imbrenda 		}
17614036e387SClaudio Imbrenda 		break;
17624036e387SClaudio Imbrenda 	}
17634036e387SClaudio Imbrenda 	case KVM_S390_SET_CMMA_BITS: {
17644036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
17654036e387SClaudio Imbrenda 
17664036e387SClaudio Imbrenda 		r = -EFAULT;
17674036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
17684036e387SClaudio Imbrenda 			break;
17694036e387SClaudio Imbrenda 		r = kvm_s390_set_cmma_bits(kvm, &args);
17704036e387SClaudio Imbrenda 		break;
17714036e387SClaudio Imbrenda 	}
1772b0c632dbSHeiko Carstens 	default:
1773367e1319SAvi Kivity 		r = -ENOTTY;
1774b0c632dbSHeiko Carstens 	}
1775b0c632dbSHeiko Carstens 
1776b0c632dbSHeiko Carstens 	return r;
1777b0c632dbSHeiko Carstens }
1778b0c632dbSHeiko Carstens 
177945c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
178045c9b47cSTony Krowiak {
178145c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
178286044c8cSChristian Borntraeger 	u32 cc = 0;
178345c9b47cSTony Krowiak 
178486044c8cSChristian Borntraeger 	memset(config, 0, 128);
178545c9b47cSTony Krowiak 	asm volatile(
178645c9b47cSTony Krowiak 		"lgr 0,%1\n"
178745c9b47cSTony Krowiak 		"lgr 2,%2\n"
178845c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
178986044c8cSChristian Borntraeger 		"0: ipm %0\n"
179045c9b47cSTony Krowiak 		"srl %0,28\n"
179186044c8cSChristian Borntraeger 		"1:\n"
179286044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
179386044c8cSChristian Borntraeger 		: "+r" (cc)
179445c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
179545c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
179645c9b47cSTony Krowiak 	);
179745c9b47cSTony Krowiak 
179845c9b47cSTony Krowiak 	return cc;
179945c9b47cSTony Krowiak }
180045c9b47cSTony Krowiak 
180145c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
180245c9b47cSTony Krowiak {
180345c9b47cSTony Krowiak 	u8 config[128];
180445c9b47cSTony Krowiak 	int cc;
180545c9b47cSTony Krowiak 
1806a6aacc3fSHeiko Carstens 	if (test_facility(12)) {
180745c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
180845c9b47cSTony Krowiak 
180945c9b47cSTony Krowiak 		if (cc)
181045c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
181145c9b47cSTony Krowiak 		else
181245c9b47cSTony Krowiak 			return config[0] & 0x40;
181345c9b47cSTony Krowiak 	}
181445c9b47cSTony Krowiak 
181545c9b47cSTony Krowiak 	return 0;
181645c9b47cSTony Krowiak }
181745c9b47cSTony Krowiak 
181845c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
181945c9b47cSTony Krowiak {
182045c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
182145c9b47cSTony Krowiak 
182245c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
182345c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
182445c9b47cSTony Krowiak 	else
182545c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
182645c9b47cSTony Krowiak }
182745c9b47cSTony Krowiak 
18289bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
18299d8d5786SMichael Mueller {
18309bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
18319bb0ec09SDavid Hildenbrand 
18329bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
18339bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
18349bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
18359d8d5786SMichael Mueller }
18369d8d5786SMichael Mueller 
1837c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
18385102ee87STony Krowiak {
18399d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
1840c54f0d6aSDavid Hildenbrand 		return;
18415102ee87STony Krowiak 
1842c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
184345c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
18445102ee87STony Krowiak 
1845ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1846ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1847ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1848ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1849ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1850ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1851ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
18525102ee87STony Krowiak }
18535102ee87STony Krowiak 
18547d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
18557d43bafcSEugene (jno) Dvurechenski {
18567d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
18575e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
18587d43bafcSEugene (jno) Dvurechenski 	else
18597d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
18607d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
18617d43bafcSEugene (jno) Dvurechenski }
18627d43bafcSEugene (jno) Dvurechenski 
1863e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1864b0c632dbSHeiko Carstens {
186576a6dd72SDavid Hildenbrand 	gfp_t alloc_flags = GFP_KERNEL;
18669d8d5786SMichael Mueller 	int i, rc;
1867b0c632dbSHeiko Carstens 	char debug_name[16];
1868f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1869b0c632dbSHeiko Carstens 
1870e08b9637SCarsten Otte 	rc = -EINVAL;
1871e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1872e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1873e08b9637SCarsten Otte 		goto out_err;
1874e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1875e08b9637SCarsten Otte 		goto out_err;
1876e08b9637SCarsten Otte #else
1877e08b9637SCarsten Otte 	if (type)
1878e08b9637SCarsten Otte 		goto out_err;
1879e08b9637SCarsten Otte #endif
1880e08b9637SCarsten Otte 
1881b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1882b0c632dbSHeiko Carstens 	if (rc)
1883d89f5effSJan Kiszka 		goto out_err;
1884b0c632dbSHeiko Carstens 
1885b290411aSCarsten Otte 	rc = -ENOMEM;
1886b290411aSCarsten Otte 
18877d0a5e62SJanosch Frank 	ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);
18887d0a5e62SJanosch Frank 
18897d43bafcSEugene (jno) Dvurechenski 	kvm->arch.use_esca = 0; /* start with basic SCA */
189076a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
189176a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
18925e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
189376a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
1894b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1895d89f5effSJan Kiszka 		goto out_err;
1896f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
1897c5c2c393SDavid Hildenbrand 	sca_offset += 16;
1898bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
1899c5c2c393SDavid Hildenbrand 		sca_offset = 0;
1900bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
1901bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
1902f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
1903b0c632dbSHeiko Carstens 
1904b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
1905b0c632dbSHeiko Carstens 
19061cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
1907b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
190840f5b735SDominik Dingel 		goto out_err;
1909b0c632dbSHeiko Carstens 
1910c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
1911c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1912c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
191340f5b735SDominik Dingel 		goto out_err;
19149d8d5786SMichael Mueller 
1915fb5bf93fSMichael Mueller 	/* Populate the facility mask initially. */
1916c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
191704478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
19189d8d5786SMichael Mueller 	for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
19199d8d5786SMichael Mueller 		if (i < kvm_s390_fac_list_mask_size())
1920c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
19219d8d5786SMichael Mueller 		else
1922c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] = 0UL;
19239d8d5786SMichael Mueller 	}
19249d8d5786SMichael Mueller 
1925981467c9SMichael Mueller 	/* Populate the facility list initially. */
1926c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1927c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
1928981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1929981467c9SMichael Mueller 
19301935222dSDavid Hildenbrand 	/* we are always in czam mode - even on pre z14 machines */
19311935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
19321935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_list, 138);
19331935222dSDavid Hildenbrand 	/* we emulate STHYI in kvm */
193495ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
193595ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
19361bab1c02SClaudio Imbrenda 	if (MACHINE_HAS_TLB_GUEST) {
19371bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
19381bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_list, 147);
19391bab1c02SClaudio Imbrenda 	}
194095ca2cb5SJanosch Frank 
19419bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
194237c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
19439d8d5786SMichael Mueller 
1944c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
19455102ee87STony Krowiak 
194651978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
194751978393SFei Li 	kvm->arch.float_int.simm = 0;
194851978393SFei Li 	kvm->arch.float_int.nimm = 0;
1949ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
19506d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
19516d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
19528a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
1953a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
1954ba5c1e9bSCarsten Otte 
1955b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
195678f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
1957b0c632dbSHeiko Carstens 
1958e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
1959e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
1960a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
1961e08b9637SCarsten Otte 	} else {
196232e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
1963ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
196432e6b236SGuenther Hutzl 		else
1965ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
196632e6b236SGuenther Hutzl 						    sclp.hamax + 1);
19676ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
1968598841caSCarsten Otte 		if (!kvm->arch.gmap)
196940f5b735SDominik Dingel 			goto out_err;
19702c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
197124eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
1972e08b9637SCarsten Otte 	}
1973fa6b7fe9SCornelia Huck 
1974fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
197584223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
197672f25020SJason J. Herne 	kvm->arch.epoch = 0;
1977fa6b7fe9SCornelia Huck 
19788ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
1979a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
19808335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
19818ad35755SDavid Hildenbrand 
1982d89f5effSJan Kiszka 	return 0;
1983d89f5effSJan Kiszka out_err:
1984c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
198540f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
19867d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
198778f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
1988d89f5effSJan Kiszka 	return rc;
1989b0c632dbSHeiko Carstens }
1990b0c632dbSHeiko Carstens 
1991235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void)
1992235539b4SLuiz Capitulino {
1993235539b4SLuiz Capitulino 	return false;
1994235539b4SLuiz Capitulino }
1995235539b4SLuiz Capitulino 
1996235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
1997235539b4SLuiz Capitulino {
1998235539b4SLuiz Capitulino 	return 0;
1999235539b4SLuiz Capitulino }
2000235539b4SLuiz Capitulino 
2001d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2002d329c035SChristian Borntraeger {
2003d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2004ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
200567335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
20063c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
2007bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
2008a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
200927e0393fSCarsten Otte 
201027e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
20116ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
201227e0393fSCarsten Otte 
2013e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
2014b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
2015d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2016b31288faSKonstantin Weitz 
20176692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
2018b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
2019d329c035SChristian Borntraeger }
2020d329c035SChristian Borntraeger 
2021d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
2022d329c035SChristian Borntraeger {
2023d329c035SChristian Borntraeger 	unsigned int i;
2024988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
2025d329c035SChristian Borntraeger 
2026988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
2027988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
2028988a2caeSGleb Natapov 
2029988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
2030988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2031d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
2032988a2caeSGleb Natapov 
2033988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
2034988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
2035d329c035SChristian Borntraeger }
2036d329c035SChristian Borntraeger 
2037b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
2038b0c632dbSHeiko Carstens {
2039d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
20407d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
2041d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
2042c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
204327e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
20446ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
2045841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
204667335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
2047a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
2048190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state) {
2049190df4a2SClaudio Imbrenda 		vfree(kvm->arch.migration_state->pgste_bitmap);
2050190df4a2SClaudio Imbrenda 		kfree(kvm->arch.migration_state);
2051190df4a2SClaudio Imbrenda 	}
20528335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2053b0c632dbSHeiko Carstens }
2054b0c632dbSHeiko Carstens 
2055b0c632dbSHeiko Carstens /* Section: vcpu related */
2056dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2057b0c632dbSHeiko Carstens {
20586ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
205927e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
206027e0393fSCarsten Otte 		return -ENOMEM;
20612c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
2062dafd032aSDominik Dingel 
206327e0393fSCarsten Otte 	return 0;
206427e0393fSCarsten Otte }
206527e0393fSCarsten Otte 
2066a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2067a6e2f683SEugene (jno) Dvurechenski {
2068a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
2069a6940674SDavid Hildenbrand 		return;
20705e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
20717d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
20727d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
20737d43bafcSEugene (jno) Dvurechenski 
20747d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
20757d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
20767d43bafcSEugene (jno) Dvurechenski 	} else {
2077bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2078a6e2f683SEugene (jno) Dvurechenski 
2079a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2080a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
2081a6e2f683SEugene (jno) Dvurechenski 	}
20825e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
20837d43bafcSEugene (jno) Dvurechenski }
2084a6e2f683SEugene (jno) Dvurechenski 
2085eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2086a6e2f683SEugene (jno) Dvurechenski {
2087a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2088a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2089a6940674SDavid Hildenbrand 
2090a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
2091a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2092a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2093a6940674SDavid Hildenbrand 	}
2094eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
2095eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
2096eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
20977d43bafcSEugene (jno) Dvurechenski 
2098eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
20997d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
21007d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
21010c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2102eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
21037d43bafcSEugene (jno) Dvurechenski 	} else {
2104eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2105a6e2f683SEugene (jno) Dvurechenski 
2106eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2107a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2108a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2109eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2110a6e2f683SEugene (jno) Dvurechenski 	}
2111eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
21125e044315SEugene (jno) Dvurechenski }
21135e044315SEugene (jno) Dvurechenski 
21145e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
21155e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
21165e044315SEugene (jno) Dvurechenski {
21175e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
21185e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
21195e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
21205e044315SEugene (jno) Dvurechenski }
21215e044315SEugene (jno) Dvurechenski 
21225e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
21235e044315SEugene (jno) Dvurechenski {
21245e044315SEugene (jno) Dvurechenski 	int i;
21255e044315SEugene (jno) Dvurechenski 
21265e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
21275e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
21285e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
21295e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
21305e044315SEugene (jno) Dvurechenski }
21315e044315SEugene (jno) Dvurechenski 
21325e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
21335e044315SEugene (jno) Dvurechenski {
21345e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
21355e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
21365e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
21375e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
21385e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
21395e044315SEugene (jno) Dvurechenski 
21405e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
21415e044315SEugene (jno) Dvurechenski 	if (!new_sca)
21425e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
21435e044315SEugene (jno) Dvurechenski 
21445e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
21455e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
21465e044315SEugene (jno) Dvurechenski 
21475e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
21485e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
21495e044315SEugene (jno) Dvurechenski 
21505e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
21515e044315SEugene (jno) Dvurechenski 
21525e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
21535e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
21545e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
21550c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
21565e044315SEugene (jno) Dvurechenski 	}
21575e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
21585e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
21595e044315SEugene (jno) Dvurechenski 
21605e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
21615e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
21625e044315SEugene (jno) Dvurechenski 
21635e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
21645e044315SEugene (jno) Dvurechenski 
21658335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
21668335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
21675e044315SEugene (jno) Dvurechenski 	return 0;
21687d43bafcSEugene (jno) Dvurechenski }
2169a6e2f683SEugene (jno) Dvurechenski 
2170a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2171a6e2f683SEugene (jno) Dvurechenski {
21725e044315SEugene (jno) Dvurechenski 	int rc;
21735e044315SEugene (jno) Dvurechenski 
2174a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2175a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
2176a6940674SDavid Hildenbrand 			return true;
2177a6940674SDavid Hildenbrand 		return false;
2178a6940674SDavid Hildenbrand 	}
21795e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
21805e044315SEugene (jno) Dvurechenski 		return true;
218176a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
21825e044315SEugene (jno) Dvurechenski 		return false;
21835e044315SEugene (jno) Dvurechenski 
21845e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
21855e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
21865e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
21875e044315SEugene (jno) Dvurechenski 
21885e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2189a6e2f683SEugene (jno) Dvurechenski }
2190a6e2f683SEugene (jno) Dvurechenski 
2191dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
2192dafd032aSDominik Dingel {
2193dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
2194dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
219559674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
219659674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
21979eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
2198b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
2199b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
2200b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
220175a4615cSJulius Niedworok 	kvm_s390_set_prefix(vcpu, 0);
2202c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
2203c6e5f166SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
22044e0b1ab7SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 133))
22054e0b1ab7SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
2206f6aa6dc4SDavid Hildenbrand 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
2207f6aa6dc4SDavid Hildenbrand 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
2208f6aa6dc4SDavid Hildenbrand 	 */
2209f6aa6dc4SDavid Hildenbrand 	if (MACHINE_HAS_VX)
221068c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
22116fd8e67dSDavid Hildenbrand 	else
22126fd8e67dSDavid Hildenbrand 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
2213dafd032aSDominik Dingel 
2214dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
2215dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
2216dafd032aSDominik Dingel 
2217b0c632dbSHeiko Carstens 	return 0;
2218b0c632dbSHeiko Carstens }
2219b0c632dbSHeiko Carstens 
2220db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2221db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2222db0758b2SDavid Hildenbrand {
2223db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
22249c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2225db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
22269c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2227db0758b2SDavid Hildenbrand }
2228db0758b2SDavid Hildenbrand 
2229db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2230db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2231db0758b2SDavid Hildenbrand {
2232db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
22339c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2234db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2235db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
22369c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2237db0758b2SDavid Hildenbrand }
2238db0758b2SDavid Hildenbrand 
2239db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2240db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2241db0758b2SDavid Hildenbrand {
2242db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2243db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
2244db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
2245db0758b2SDavid Hildenbrand }
2246db0758b2SDavid Hildenbrand 
2247db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2248db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2249db0758b2SDavid Hildenbrand {
2250db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2251db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
2252db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
2253db0758b2SDavid Hildenbrand }
2254db0758b2SDavid Hildenbrand 
2255db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2256db0758b2SDavid Hildenbrand {
2257db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2258db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
2259db0758b2SDavid Hildenbrand 	preempt_enable();
2260db0758b2SDavid Hildenbrand }
2261db0758b2SDavid Hildenbrand 
2262db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2263db0758b2SDavid Hildenbrand {
2264db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2265db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
2266db0758b2SDavid Hildenbrand 	preempt_enable();
2267db0758b2SDavid Hildenbrand }
2268db0758b2SDavid Hildenbrand 
22694287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
22704287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
22714287f247SDavid Hildenbrand {
2272db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
22739c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2274db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
2275db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
22764287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
22779c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2278db0758b2SDavid Hildenbrand 	preempt_enable();
22794287f247SDavid Hildenbrand }
22804287f247SDavid Hildenbrand 
2281db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
22824287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
22834287f247SDavid Hildenbrand {
22849c23a131SDavid Hildenbrand 	unsigned int seq;
2285db0758b2SDavid Hildenbrand 	__u64 value;
2286db0758b2SDavid Hildenbrand 
2287db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
22884287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
2289db0758b2SDavid Hildenbrand 
22909c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
22919c23a131SDavid Hildenbrand 	do {
22929c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
22939c23a131SDavid Hildenbrand 		/*
22949c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
22959c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
22969c23a131SDavid Hildenbrand 		 */
22979c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
2298db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
22999c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
23009c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
2301db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
23029c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
23039c23a131SDavid Hildenbrand 	preempt_enable();
2304db0758b2SDavid Hildenbrand 	return value;
23054287f247SDavid Hildenbrand }
23064287f247SDavid Hildenbrand 
2307b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2308b0c632dbSHeiko Carstens {
23099977e886SHendrik Brueckner 
231037d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
2311805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
23125ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2313db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
231401a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
2315b0c632dbSHeiko Carstens }
2316b0c632dbSHeiko Carstens 
2317b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2318b0c632dbSHeiko Carstens {
231901a745acSDavid Hildenbrand 	vcpu->cpu = -1;
23205ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2321db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
2322805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
232337d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
232437d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
23259977e886SHendrik Brueckner 
2326b0c632dbSHeiko Carstens }
2327b0c632dbSHeiko Carstens 
2328b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
2329b0c632dbSHeiko Carstens {
2330b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
2331b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
2332b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
23338d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
23344287f247SDavid Hildenbrand 	kvm_s390_set_cpu_timer(vcpu, 0);
2335b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
2336b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
2337b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
2338b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
2339b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
23409abc2a08SDavid Hildenbrand 	/* make sure the new fpc will be lazily loaded */
23419abc2a08SDavid Hildenbrand 	save_fpu_regs();
23429abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = 0;
2343b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
2344672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
23453c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
23463c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
23476352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
23486852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
23492ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
2350b0c632dbSHeiko Carstens }
2351b0c632dbSHeiko Carstens 
235231928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
235342897d86SMarcelo Tosatti {
235472f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
2355fdf03650SFan Zhang 	preempt_disable();
235672f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
2357fdf03650SFan Zhang 	preempt_enable();
235872f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
235925508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
2360dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
2361eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
236225508824SDavid Hildenbrand 	}
23636502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
23646502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
236537d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
236637d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
236742897d86SMarcelo Tosatti }
236842897d86SMarcelo Tosatti 
23695102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
23705102ee87STony Krowiak {
23719d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
23725102ee87STony Krowiak 		return;
23735102ee87STony Krowiak 
2374a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
2375a374e892STony Krowiak 
2376a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
2377a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
2378a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
2379a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
2380a374e892STony Krowiak 
23815102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
23825102ee87STony Krowiak }
23835102ee87STony Krowiak 
2384b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
2385b31605c1SDominik Dingel {
2386b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
2387b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
2388b31605c1SDominik Dingel }
2389b31605c1SDominik Dingel 
2390b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
2391b31605c1SDominik Dingel {
2392b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
2393b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
2394b31605c1SDominik Dingel 		return -ENOMEM;
2395b31605c1SDominik Dingel 
23960c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->ecb2 &= ~ECB2_PFMFI;
2397b31605c1SDominik Dingel 	return 0;
2398b31605c1SDominik Dingel }
2399b31605c1SDominik Dingel 
240091520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
240191520f1aSMichael Mueller {
240291520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
240391520f1aSMichael Mueller 
240491520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
240580bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
2406c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
240791520f1aSMichael Mueller }
240891520f1aSMichael Mueller 
2409b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
2410b0c632dbSHeiko Carstens {
2411b31605c1SDominik Dingel 	int rc = 0;
2412b31288faSKonstantin Weitz 
24139e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
24149e6dabefSCornelia Huck 						    CPUSTAT_SM |
2415a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
2416a4a4f191SGuenther Hutzl 
241753df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
2418805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
241953df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
2420805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
2421a4a4f191SGuenther Hutzl 
242291520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
242391520f1aSMichael Mueller 
2424bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2425bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
24260c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
2427bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
24280c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
2429f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
24300c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
24317feb6bb8SMichael Mueller 
2432873b425eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi)
24330c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
2434cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
24350c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
24360c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
243748ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
24380c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
243911ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
24400c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
244137c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
24420c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
244337c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
24440c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
244518280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
24460c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
24470c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
244813211ea7SEric Farman 	}
24498fa1696eSCollin L. Walling 	if (test_kvm_facility(vcpu->kvm, 139))
24508fa1696eSCollin L. Walling 		vcpu->arch.sie_block->ecd |= ECD_MEF;
24518fa1696eSCollin L. Walling 
24524e0b1ab7SFan Zhang 	vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
24534e0b1ab7SFan Zhang 					| SDNXC;
2454c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
2455730cd632SFarhan Ali 
2456730cd632SFarhan Ali 	if (sclp.has_kss)
2457730cd632SFarhan Ali 		atomic_or(CPUSTAT_KSS, &vcpu->arch.sie_block->cpuflags);
2458730cd632SFarhan Ali 	else
2459492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
24605a5e6536SMatthew Rosato 
2461e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
2462b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
2463b31605c1SDominik Dingel 		if (rc)
2464b31605c1SDominik Dingel 			return rc;
2465b31288faSKonstantin Weitz 	}
24660ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2467ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
24689d8d5786SMichael Mueller 
24695102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
24705102ee87STony Krowiak 
2471b31605c1SDominik Dingel 	return rc;
2472b0c632dbSHeiko Carstens }
2473b0c632dbSHeiko Carstens 
2474b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2475b0c632dbSHeiko Carstens 				      unsigned int id)
2476b0c632dbSHeiko Carstens {
24774d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
24787feb6bb8SMichael Mueller 	struct sie_page *sie_page;
24794d47555aSCarsten Otte 	int rc = -EINVAL;
2480b0c632dbSHeiko Carstens 
24814215825eSDavid Hildenbrand 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
24824d47555aSCarsten Otte 		goto out;
24834d47555aSCarsten Otte 
24844d47555aSCarsten Otte 	rc = -ENOMEM;
24854d47555aSCarsten Otte 
2486b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2487b0c632dbSHeiko Carstens 	if (!vcpu)
24884d47555aSCarsten Otte 		goto out;
2489b0c632dbSHeiko Carstens 
2490da72ca4dSQingFeng Hao 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
24917feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
24927feb6bb8SMichael Mueller 	if (!sie_page)
2493b0c632dbSHeiko Carstens 		goto out_free_cpu;
2494b0c632dbSHeiko Carstens 
24957feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
24967feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
24977feb6bb8SMichael Mueller 
2498efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
2499efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
2500efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
2501efed1104SDavid Hildenbrand 
2502b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
2503ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
2504ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
2505d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
25065288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
25079c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
2508ba5c1e9bSCarsten Otte 
2509b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
2510b0c632dbSHeiko Carstens 	if (rc)
25119abc2a08SDavid Hildenbrand 		goto out_free_sie_block;
25128335713aSChristian Borntraeger 	VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2513b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
2514ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2515b0c632dbSHeiko Carstens 
2516b0c632dbSHeiko Carstens 	return vcpu;
25177b06bf2fSWei Yongjun out_free_sie_block:
25187b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
2519b0c632dbSHeiko Carstens out_free_cpu:
2520b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
25214d47555aSCarsten Otte out:
2522b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
2523b0c632dbSHeiko Carstens }
2524b0c632dbSHeiko Carstens 
2525b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2526b0c632dbSHeiko Carstens {
25279a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
2528b0c632dbSHeiko Carstens }
2529b0c632dbSHeiko Carstens 
2530199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
2531199b5763SLongpeng(Mike) {
25320546c63dSLongpeng(Mike) 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
2533199b5763SLongpeng(Mike) }
2534199b5763SLongpeng(Mike) 
253527406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
253649b99e1eSChristian Borntraeger {
2537805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
253861a6df54SDavid Hildenbrand 	exit_sie(vcpu);
253949b99e1eSChristian Borntraeger }
254049b99e1eSChristian Borntraeger 
254127406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
254249b99e1eSChristian Borntraeger {
2543805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
254449b99e1eSChristian Borntraeger }
254549b99e1eSChristian Borntraeger 
25468e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
25478e236546SChristian Borntraeger {
2548805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
254961a6df54SDavid Hildenbrand 	exit_sie(vcpu);
25508e236546SChristian Borntraeger }
25518e236546SChristian Borntraeger 
25528e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
25538e236546SChristian Borntraeger {
25549bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
25558e236546SChristian Borntraeger }
25568e236546SChristian Borntraeger 
255749b99e1eSChristian Borntraeger /*
255849b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
255949b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
256049b99e1eSChristian Borntraeger  * return immediately. */
256149b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
256249b99e1eSChristian Borntraeger {
2563805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
256449b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
256549b99e1eSChristian Borntraeger 		cpu_relax();
256649b99e1eSChristian Borntraeger }
256749b99e1eSChristian Borntraeger 
25688e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
25698e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
257049b99e1eSChristian Borntraeger {
25718e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
25728e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
257349b99e1eSChristian Borntraeger }
257449b99e1eSChristian Borntraeger 
2575414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
2576414d3b07SMartin Schwidefsky 			      unsigned long end)
25772c70fe44SChristian Borntraeger {
25782c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
25792c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
2580414d3b07SMartin Schwidefsky 	unsigned long prefix;
2581414d3b07SMartin Schwidefsky 	int i;
25822c70fe44SChristian Borntraeger 
258365d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
258465d0b0d4SDavid Hildenbrand 		return;
2585414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
2586414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
2587414d3b07SMartin Schwidefsky 		return;
25882c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
25892c70fe44SChristian Borntraeger 		/* match against both prefix pages */
2590414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
2591414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
2592414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
2593414d3b07SMartin Schwidefsky 				   start, end);
25948e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
25952c70fe44SChristian Borntraeger 		}
25962c70fe44SChristian Borntraeger 	}
25972c70fe44SChristian Borntraeger }
25982c70fe44SChristian Borntraeger 
2599b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
2600b6d33834SChristoffer Dall {
2601b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
2602b6d33834SChristoffer Dall 	BUG();
2603b6d33834SChristoffer Dall 	return 0;
2604b6d33834SChristoffer Dall }
2605b6d33834SChristoffer Dall 
260614eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
260714eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
260814eebd91SCarsten Otte {
260914eebd91SCarsten Otte 	int r = -EINVAL;
261014eebd91SCarsten Otte 
261114eebd91SCarsten Otte 	switch (reg->id) {
261229b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
261329b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
261429b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
261529b7c71bSCarsten Otte 		break;
261629b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
261729b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
261829b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
261929b7c71bSCarsten Otte 		break;
262046a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
26214287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
262246a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
262346a6dd1cSJason J. herne 		break;
262446a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
262546a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
262646a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
262746a6dd1cSJason J. herne 		break;
2628536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2629536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
2630536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2631536336c2SDominik Dingel 		break;
2632536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2633536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
2634536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2635536336c2SDominik Dingel 		break;
2636536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2637536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
2638536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2639536336c2SDominik Dingel 		break;
2640672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2641672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
2642672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2643672550fbSChristian Borntraeger 		break;
2644afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2645afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
2646afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2647afa45ff5SChristian Borntraeger 		break;
264814eebd91SCarsten Otte 	default:
264914eebd91SCarsten Otte 		break;
265014eebd91SCarsten Otte 	}
265114eebd91SCarsten Otte 
265214eebd91SCarsten Otte 	return r;
265314eebd91SCarsten Otte }
265414eebd91SCarsten Otte 
265514eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
265614eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
265714eebd91SCarsten Otte {
265814eebd91SCarsten Otte 	int r = -EINVAL;
26594287f247SDavid Hildenbrand 	__u64 val;
266014eebd91SCarsten Otte 
266114eebd91SCarsten Otte 	switch (reg->id) {
266229b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
266329b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
266429b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
266529b7c71bSCarsten Otte 		break;
266629b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
266729b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
266829b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
266929b7c71bSCarsten Otte 		break;
267046a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
26714287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
26724287f247SDavid Hildenbrand 		if (!r)
26734287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
267446a6dd1cSJason J. herne 		break;
267546a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
267646a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
267746a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
267846a6dd1cSJason J. herne 		break;
2679536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2680536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
2681536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
26829fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
26839fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2684536336c2SDominik Dingel 		break;
2685536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2686536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
2687536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2688536336c2SDominik Dingel 		break;
2689536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2690536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
2691536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2692536336c2SDominik Dingel 		break;
2693672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2694672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
2695672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2696672550fbSChristian Borntraeger 		break;
2697afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2698afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
2699afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2700afa45ff5SChristian Borntraeger 		break;
270114eebd91SCarsten Otte 	default:
270214eebd91SCarsten Otte 		break;
270314eebd91SCarsten Otte 	}
270414eebd91SCarsten Otte 
270514eebd91SCarsten Otte 	return r;
270614eebd91SCarsten Otte }
2707b6d33834SChristoffer Dall 
2708b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
2709b0c632dbSHeiko Carstens {
2710b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
2711b0c632dbSHeiko Carstens 	return 0;
2712b0c632dbSHeiko Carstens }
2713b0c632dbSHeiko Carstens 
2714b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2715b0c632dbSHeiko Carstens {
27165a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
2717b0c632dbSHeiko Carstens 	return 0;
2718b0c632dbSHeiko Carstens }
2719b0c632dbSHeiko Carstens 
2720b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2721b0c632dbSHeiko Carstens {
27225a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
2723b0c632dbSHeiko Carstens 	return 0;
2724b0c632dbSHeiko Carstens }
2725b0c632dbSHeiko Carstens 
2726b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2727b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2728b0c632dbSHeiko Carstens {
272959674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
2730b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
2731b0c632dbSHeiko Carstens 	return 0;
2732b0c632dbSHeiko Carstens }
2733b0c632dbSHeiko Carstens 
2734b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2735b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2736b0c632dbSHeiko Carstens {
273759674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
2738b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
2739b0c632dbSHeiko Carstens 	return 0;
2740b0c632dbSHeiko Carstens }
2741b0c632dbSHeiko Carstens 
2742b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2743b0c632dbSHeiko Carstens {
27444725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
27454725c860SMartin Schwidefsky 		return -EINVAL;
2746e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
27479abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2748a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
2749a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
27509abc2a08SDavid Hildenbrand 	else
2751a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
2752b0c632dbSHeiko Carstens 	return 0;
2753b0c632dbSHeiko Carstens }
2754b0c632dbSHeiko Carstens 
2755b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2756b0c632dbSHeiko Carstens {
27579abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
27589abc2a08SDavid Hildenbrand 	save_fpu_regs();
27599abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2760a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
2761a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
27629abc2a08SDavid Hildenbrand 	else
2763a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
2764e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
2765b0c632dbSHeiko Carstens 	return 0;
2766b0c632dbSHeiko Carstens }
2767b0c632dbSHeiko Carstens 
2768b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
2769b0c632dbSHeiko Carstens {
2770b0c632dbSHeiko Carstens 	int rc = 0;
2771b0c632dbSHeiko Carstens 
27727a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
2773b0c632dbSHeiko Carstens 		rc = -EBUSY;
2774d7b0b5ebSCarsten Otte 	else {
2775d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
2776d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
2777d7b0b5ebSCarsten Otte 	}
2778b0c632dbSHeiko Carstens 	return rc;
2779b0c632dbSHeiko Carstens }
2780b0c632dbSHeiko Carstens 
2781b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2782b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
2783b0c632dbSHeiko Carstens {
2784b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
2785b0c632dbSHeiko Carstens }
2786b0c632dbSHeiko Carstens 
278727291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
278827291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
278927291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
279027291e21SDavid Hildenbrand 
2791d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2792d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
2793b0c632dbSHeiko Carstens {
279427291e21SDavid Hildenbrand 	int rc = 0;
279527291e21SDavid Hildenbrand 
279627291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
279727291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
279827291e21SDavid Hildenbrand 
27992de3bfc2SDavid Hildenbrand 	if (dbg->control & ~VALID_GUESTDBG_FLAGS)
280027291e21SDavid Hildenbrand 		return -EINVAL;
280189b5b4deSDavid Hildenbrand 	if (!sclp.has_gpere)
280289b5b4deSDavid Hildenbrand 		return -EINVAL;
280327291e21SDavid Hildenbrand 
280427291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
280527291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
280627291e21SDavid Hildenbrand 		/* enforce guest PER */
2807805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
280827291e21SDavid Hildenbrand 
280927291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
281027291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
281127291e21SDavid Hildenbrand 	} else {
2812805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
281327291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
281427291e21SDavid Hildenbrand 	}
281527291e21SDavid Hildenbrand 
281627291e21SDavid Hildenbrand 	if (rc) {
281727291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
281827291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
2819805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
282027291e21SDavid Hildenbrand 	}
282127291e21SDavid Hildenbrand 
282227291e21SDavid Hildenbrand 	return rc;
2823b0c632dbSHeiko Carstens }
2824b0c632dbSHeiko Carstens 
282562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
282662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
282762d9f0dbSMarcelo Tosatti {
28286352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
28296352e4d2SDavid Hildenbrand 	return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
28306352e4d2SDavid Hildenbrand 				       KVM_MP_STATE_OPERATING;
283162d9f0dbSMarcelo Tosatti }
283262d9f0dbSMarcelo Tosatti 
283362d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
283462d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
283562d9f0dbSMarcelo Tosatti {
28366352e4d2SDavid Hildenbrand 	int rc = 0;
28376352e4d2SDavid Hildenbrand 
28386352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
28396352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
28406352e4d2SDavid Hildenbrand 
28416352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
28426352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
28436352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
28446352e4d2SDavid Hildenbrand 		break;
28456352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
28466352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
28476352e4d2SDavid Hildenbrand 		break;
28486352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
28496352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
28506352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
28516352e4d2SDavid Hildenbrand 	default:
28526352e4d2SDavid Hildenbrand 		rc = -ENXIO;
28536352e4d2SDavid Hildenbrand 	}
28546352e4d2SDavid Hildenbrand 
28556352e4d2SDavid Hildenbrand 	return rc;
285662d9f0dbSMarcelo Tosatti }
285762d9f0dbSMarcelo Tosatti 
28588ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
28598ad35755SDavid Hildenbrand {
28608ad35755SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
28618ad35755SDavid Hildenbrand }
28628ad35755SDavid Hildenbrand 
28632c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
28642c70fe44SChristian Borntraeger {
28658ad35755SDavid Hildenbrand retry:
28668e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
28672fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
2868586b7ccdSChristian Borntraeger 		return 0;
28692c70fe44SChristian Borntraeger 	/*
28702c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2871b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
28722c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
28732c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
28742c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
28752c70fe44SChristian Borntraeger 	 */
28768ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
28772c70fe44SChristian Borntraeger 		int rc;
2878b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
2879fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
2880b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
2881aca411a4SJulius Niedworok 		if (rc) {
2882aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
28832c70fe44SChristian Borntraeger 			return rc;
2884aca411a4SJulius Niedworok 		}
28858ad35755SDavid Hildenbrand 		goto retry;
28862c70fe44SChristian Borntraeger 	}
28878ad35755SDavid Hildenbrand 
2888d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2889d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
2890d3d692c8SDavid Hildenbrand 		goto retry;
2891d3d692c8SDavid Hildenbrand 	}
2892d3d692c8SDavid Hildenbrand 
28938ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
28948ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
28958ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
2896805de8f4SPeter Zijlstra 			atomic_or(CPUSTAT_IBS,
28978ad35755SDavid Hildenbrand 					&vcpu->arch.sie_block->cpuflags);
28988ad35755SDavid Hildenbrand 		}
28998ad35755SDavid Hildenbrand 		goto retry;
29008ad35755SDavid Hildenbrand 	}
29018ad35755SDavid Hildenbrand 
29028ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
29038ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
29048ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
2905805de8f4SPeter Zijlstra 			atomic_andnot(CPUSTAT_IBS,
29068ad35755SDavid Hildenbrand 					  &vcpu->arch.sie_block->cpuflags);
29078ad35755SDavid Hildenbrand 		}
29088ad35755SDavid Hildenbrand 		goto retry;
29098ad35755SDavid Hildenbrand 	}
29108ad35755SDavid Hildenbrand 
29116502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
29126502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
29136502a34cSDavid Hildenbrand 		goto retry;
29146502a34cSDavid Hildenbrand 	}
29156502a34cSDavid Hildenbrand 
2916190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
2917190df4a2SClaudio Imbrenda 		/*
2918190df4a2SClaudio Imbrenda 		 * Disable CMMA virtualization; we will emulate the ESSA
2919190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
2920190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
2921190df4a2SClaudio Imbrenda 		 */
2922190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
2923190df4a2SClaudio Imbrenda 		goto retry;
2924190df4a2SClaudio Imbrenda 	}
2925190df4a2SClaudio Imbrenda 
2926190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
2927190df4a2SClaudio Imbrenda 		/*
2928190df4a2SClaudio Imbrenda 		 * Re-enable CMMA virtualization if CMMA is available and
2929190df4a2SClaudio Imbrenda 		 * was used.
2930190df4a2SClaudio Imbrenda 		 */
2931190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
2932190df4a2SClaudio Imbrenda 		    (vcpu->kvm->mm->context.use_cmma))
2933190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
2934190df4a2SClaudio Imbrenda 		goto retry;
2935190df4a2SClaudio Imbrenda 	}
2936190df4a2SClaudio Imbrenda 
29370759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
293872875d8aSRadim Krčmář 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
29390759d068SDavid Hildenbrand 
29402c70fe44SChristian Borntraeger 	return 0;
29412c70fe44SChristian Borntraeger }
29422c70fe44SChristian Borntraeger 
29438fa1696eSCollin L. Walling void kvm_s390_set_tod_clock_ext(struct kvm *kvm,
29448fa1696eSCollin L. Walling 				 const struct kvm_s390_vm_tod_clock *gtod)
29458fa1696eSCollin L. Walling {
29468fa1696eSCollin L. Walling 	struct kvm_vcpu *vcpu;
29478fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
29488fa1696eSCollin L. Walling 	int i;
29498fa1696eSCollin L. Walling 
29508fa1696eSCollin L. Walling 	mutex_lock(&kvm->lock);
29518fa1696eSCollin L. Walling 	preempt_disable();
29528fa1696eSCollin L. Walling 
29538fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
29548fa1696eSCollin L. Walling 
29558fa1696eSCollin L. Walling 	kvm->arch.epoch = gtod->tod - htod.tod;
29568fa1696eSCollin L. Walling 	kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
29578fa1696eSCollin L. Walling 
29588fa1696eSCollin L. Walling 	if (kvm->arch.epoch > gtod->tod)
29598fa1696eSCollin L. Walling 		kvm->arch.epdx -= 1;
29608fa1696eSCollin L. Walling 
29618fa1696eSCollin L. Walling 	kvm_s390_vcpu_block_all(kvm);
29628fa1696eSCollin L. Walling 	kvm_for_each_vcpu(i, vcpu, kvm) {
29638fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
29648fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
29658fa1696eSCollin L. Walling 	}
29668fa1696eSCollin L. Walling 
29678fa1696eSCollin L. Walling 	kvm_s390_vcpu_unblock_all(kvm);
29688fa1696eSCollin L. Walling 	preempt_enable();
29698fa1696eSCollin L. Walling 	mutex_unlock(&kvm->lock);
29708fa1696eSCollin L. Walling }
29718fa1696eSCollin L. Walling 
297225ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
297325ed1675SDavid Hildenbrand {
297425ed1675SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
297525ed1675SDavid Hildenbrand 	int i;
297625ed1675SDavid Hildenbrand 
297725ed1675SDavid Hildenbrand 	mutex_lock(&kvm->lock);
297825ed1675SDavid Hildenbrand 	preempt_disable();
297925ed1675SDavid Hildenbrand 	kvm->arch.epoch = tod - get_tod_clock();
298025ed1675SDavid Hildenbrand 	kvm_s390_vcpu_block_all(kvm);
298125ed1675SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm)
298225ed1675SDavid Hildenbrand 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
298325ed1675SDavid Hildenbrand 	kvm_s390_vcpu_unblock_all(kvm);
298425ed1675SDavid Hildenbrand 	preempt_enable();
298525ed1675SDavid Hildenbrand 	mutex_unlock(&kvm->lock);
298625ed1675SDavid Hildenbrand }
298725ed1675SDavid Hildenbrand 
2988fa576c58SThomas Huth /**
2989fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
2990fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
2991fa576c58SThomas Huth  * @gpa: Guest physical address
2992fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
2993fa576c58SThomas Huth  *
2994fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
2995fa576c58SThomas Huth  *
2996fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
2997fa576c58SThomas Huth  */
2998fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
299924eb3a82SDominik Dingel {
3000527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
3001527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
300224eb3a82SDominik Dingel }
300324eb3a82SDominik Dingel 
30043c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
30053c038e6bSDominik Dingel 				      unsigned long token)
30063c038e6bSDominik Dingel {
30073c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
3008383d0b05SJens Freimann 	struct kvm_s390_irq irq;
30093c038e6bSDominik Dingel 
30103c038e6bSDominik Dingel 	if (start_token) {
3011383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
3012383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
3013383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
30143c038e6bSDominik Dingel 	} else {
30153c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
3016383d0b05SJens Freimann 		inti.parm64 = token;
30173c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
30183c038e6bSDominik Dingel 	}
30193c038e6bSDominik Dingel }
30203c038e6bSDominik Dingel 
30213c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
30223c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
30233c038e6bSDominik Dingel {
30243c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
30253c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
30263c038e6bSDominik Dingel }
30273c038e6bSDominik Dingel 
30283c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
30293c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
30303c038e6bSDominik Dingel {
30313c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
30323c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
30333c038e6bSDominik Dingel }
30343c038e6bSDominik Dingel 
30353c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
30363c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
30373c038e6bSDominik Dingel {
30383c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
30393c038e6bSDominik Dingel }
30403c038e6bSDominik Dingel 
30413c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
30423c038e6bSDominik Dingel {
30433c038e6bSDominik Dingel 	/*
30443c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
30453c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
30463c038e6bSDominik Dingel 	 */
30473c038e6bSDominik Dingel 	return true;
30483c038e6bSDominik Dingel }
30493c038e6bSDominik Dingel 
30503c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
30513c038e6bSDominik Dingel {
30523c038e6bSDominik Dingel 	hva_t hva;
30533c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
30543c038e6bSDominik Dingel 	int rc;
30553c038e6bSDominik Dingel 
30563c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
30573c038e6bSDominik Dingel 		return 0;
30583c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
30593c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
30603c038e6bSDominik Dingel 		return 0;
30613c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
30623c038e6bSDominik Dingel 		return 0;
30639a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
30643c038e6bSDominik Dingel 		return 0;
30653c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
30663c038e6bSDominik Dingel 		return 0;
30673c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
30683c038e6bSDominik Dingel 		return 0;
30693c038e6bSDominik Dingel 
307081480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
307181480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
307281480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
30733c038e6bSDominik Dingel 		return 0;
30743c038e6bSDominik Dingel 
30753c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
30763c038e6bSDominik Dingel 	return rc;
30773c038e6bSDominik Dingel }
30783c038e6bSDominik Dingel 
30793fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3080b0c632dbSHeiko Carstens {
30813fb4c40fSThomas Huth 	int rc, cpuflags;
3082e168bf8dSCarsten Otte 
30833c038e6bSDominik Dingel 	/*
30843c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
30853c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
30863c038e6bSDominik Dingel 	 * handled outside the worker.
30873c038e6bSDominik Dingel 	 */
30883c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
30893c038e6bSDominik Dingel 
30907ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
30917ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
3092b0c632dbSHeiko Carstens 
3093b0c632dbSHeiko Carstens 	if (need_resched())
3094b0c632dbSHeiko Carstens 		schedule();
3095b0c632dbSHeiko Carstens 
3096d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
309771cde587SChristian Borntraeger 		s390_handle_mcck();
309871cde587SChristian Borntraeger 
309979395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
310079395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
310179395031SJens Freimann 		if (rc)
310279395031SJens Freimann 			return rc;
310379395031SJens Freimann 	}
31040ff31867SCarsten Otte 
31052c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
31062c70fe44SChristian Borntraeger 	if (rc)
31072c70fe44SChristian Borntraeger 		return rc;
31082c70fe44SChristian Borntraeger 
310927291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
311027291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
311127291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
311227291e21SDavid Hildenbrand 	}
311327291e21SDavid Hildenbrand 
3114b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
31153fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
31163fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
31173fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
31182b29a9fdSDominik Dingel 
31193fb4c40fSThomas Huth 	return 0;
31203fb4c40fSThomas Huth }
31213fb4c40fSThomas Huth 
3122492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
3123492d8642SThomas Huth {
312456317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
312556317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
312656317920SDavid Hildenbrand 	};
312756317920SDavid Hildenbrand 	u8 opcode, ilen;
3128492d8642SThomas Huth 	int rc;
3129492d8642SThomas Huth 
3130492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
3131492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
3132492d8642SThomas Huth 
3133492d8642SThomas Huth 	/*
3134492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
3135492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
3136492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
3137492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
3138492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
3139492d8642SThomas Huth 	 * to be able to forward the PSW.
3140492d8642SThomas Huth 	 */
31413fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
314256317920SDavid Hildenbrand 	ilen = insn_length(opcode);
31439b0d721aSDavid Hildenbrand 	if (rc < 0) {
31449b0d721aSDavid Hildenbrand 		return rc;
31459b0d721aSDavid Hildenbrand 	} else if (rc) {
31469b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
31479b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
31489b0d721aSDavid Hildenbrand 		 * nullification if necessary.
31499b0d721aSDavid Hildenbrand 		 */
31509b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
31519b0d721aSDavid Hildenbrand 		ilen = 4;
31529b0d721aSDavid Hildenbrand 	}
315356317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
315456317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
315556317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
3156492d8642SThomas Huth }
3157492d8642SThomas Huth 
31583fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
31593fb4c40fSThomas Huth {
31604d62fcc0SQingFeng Hao 	struct mcck_volatile_info *mcck_info;
31614d62fcc0SQingFeng Hao 	struct sie_page *sie_page;
31624d62fcc0SQingFeng Hao 
31632b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
31642b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
31652b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
31662b29a9fdSDominik Dingel 
316727291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
316827291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
316927291e21SDavid Hildenbrand 
31707ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
31717ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
317271f116bfSDavid Hildenbrand 
31734d62fcc0SQingFeng Hao 	if (exit_reason == -EINTR) {
31744d62fcc0SQingFeng Hao 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
31754d62fcc0SQingFeng Hao 		sie_page = container_of(vcpu->arch.sie_block,
31764d62fcc0SQingFeng Hao 					struct sie_page, sie_block);
31774d62fcc0SQingFeng Hao 		mcck_info = &sie_page->mcck_info;
31784d62fcc0SQingFeng Hao 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
31794d62fcc0SQingFeng Hao 		return 0;
31804d62fcc0SQingFeng Hao 	}
31814d62fcc0SQingFeng Hao 
318271f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
318371f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
318471f116bfSDavid Hildenbrand 
318571f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
318671f116bfSDavid Hildenbrand 			return rc;
318771f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
318871f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
318971f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
319071f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
319171f116bfSDavid Hildenbrand 		return -EREMOTE;
319271f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
319371f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
319471f116bfSDavid Hildenbrand 		return 0;
3195210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
3196210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
3197210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
3198210b1607SThomas Huth 						current->thread.gmap_addr;
3199210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
320071f116bfSDavid Hildenbrand 		return -EREMOTE;
320124eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
32023c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
320324eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
320471f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
320571f116bfSDavid Hildenbrand 			return 0;
320671f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
3207fa576c58SThomas Huth 	}
320871f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
32093fb4c40fSThomas Huth }
32103fb4c40fSThomas Huth 
32113fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
32123fb4c40fSThomas Huth {
32133fb4c40fSThomas Huth 	int rc, exit_reason;
32143fb4c40fSThomas Huth 
3215800c1065SThomas Huth 	/*
3216800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
3217800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
3218800c1065SThomas Huth 	 */
3219800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3220800c1065SThomas Huth 
3221a76ccff6SThomas Huth 	do {
32223fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
32233fb4c40fSThomas Huth 		if (rc)
3224a76ccff6SThomas Huth 			break;
32253fb4c40fSThomas Huth 
3226800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
32273fb4c40fSThomas Huth 		/*
3228a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
3229a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
32303fb4c40fSThomas Huth 		 */
32310097d12eSChristian Borntraeger 		local_irq_disable();
32326edaa530SPaolo Bonzini 		guest_enter_irqoff();
3233db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
32340097d12eSChristian Borntraeger 		local_irq_enable();
3235a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
3236a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
32370097d12eSChristian Borntraeger 		local_irq_disable();
3238db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
32396edaa530SPaolo Bonzini 		guest_exit_irqoff();
32400097d12eSChristian Borntraeger 		local_irq_enable();
3241800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
32423fb4c40fSThomas Huth 
32433fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
324427291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
32453fb4c40fSThomas Huth 
3246800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
3247e168bf8dSCarsten Otte 	return rc;
3248b0c632dbSHeiko Carstens }
3249b0c632dbSHeiko Carstens 
3250b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3251b028ee3eSDavid Hildenbrand {
32524d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
32534e0b1ab7SFan Zhang 	struct gs_cb *gscb;
32544d5f2c04SChristian Borntraeger 
32554d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
32564e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
3257b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
3258b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
3259b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
3260b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
3261b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
3262b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
3263d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
3264d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3265b028ee3eSDavid Hildenbrand 	}
3266b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
32674287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
3268b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
3269b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
3270b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
3271b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
3272b028ee3eSDavid Hildenbrand 	}
3273b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
3274b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
3275b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
3276b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
32779fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
32789fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
3279b028ee3eSDavid Hildenbrand 	}
328080cd8763SFan Zhang 	/*
328180cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
328280cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
328380cd8763SFan Zhang 	 */
328480cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
32854d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
3286*bb59c2daSAlice Frosi 	    riccb->v &&
32870c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
32884d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
32890c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
329080cd8763SFan Zhang 	}
32914e0b1ab7SFan Zhang 	/*
32924e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
32934e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
32944e0b1ab7SFan Zhang 	 */
32954e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
32964e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
32974e0b1ab7SFan Zhang 	    gscb->gssm &&
32984e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
32994e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
33004e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
33014e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
33024e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
330380cd8763SFan Zhang 	}
330431d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->arch.host_acrs);
330531d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
3306e1788bb9SChristian Borntraeger 	/* save host (userspace) fprs/vrs */
3307e1788bb9SChristian Borntraeger 	save_fpu_regs();
3308e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
3309e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
3310e1788bb9SChristian Borntraeger 	if (MACHINE_HAS_VX)
3311e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
3312e1788bb9SChristian Borntraeger 	else
3313e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
3314e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
3315e1788bb9SChristian Borntraeger 	if (test_fp_ctl(current->thread.fpu.fpc))
3316e1788bb9SChristian Borntraeger 		/* User space provided an invalid FPC, let's clear it */
3317e1788bb9SChristian Borntraeger 		current->thread.fpu.fpc = 0;
33184e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
33194e0b1ab7SFan Zhang 		preempt_disable();
33204e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
33214e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
33224e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
33234e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
33244e0b1ab7SFan Zhang 		}
33254e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
33264e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
33274e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
33284e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
33294e0b1ab7SFan Zhang 		}
33304e0b1ab7SFan Zhang 		preempt_enable();
33314e0b1ab7SFan Zhang 	}
333280cd8763SFan Zhang 
3333b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
3334b028ee3eSDavid Hildenbrand }
3335b028ee3eSDavid Hildenbrand 
3336b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3337b028ee3eSDavid Hildenbrand {
3338b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
3339b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
3340b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
3341b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
33424287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
3343b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
3344b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
3345b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
3346b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
3347b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
3348b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
3349b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
335031d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
335131d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->arch.host_acrs);
3352e1788bb9SChristian Borntraeger 	/* Save guest register state */
3353e1788bb9SChristian Borntraeger 	save_fpu_regs();
3354e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3355e1788bb9SChristian Borntraeger 	/* Restore will be done lazily at return */
3356e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
3357e1788bb9SChristian Borntraeger 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
33584e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
33594e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
33604e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
33614e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
33624e0b1ab7SFan Zhang 		preempt_disable();
33634e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
33644e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
33654e0b1ab7SFan Zhang 		preempt_enable();
33664e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
33674e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
33684e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
33694e0b1ab7SFan Zhang 	}
3370e1788bb9SChristian Borntraeger 
3371b028ee3eSDavid Hildenbrand }
3372b028ee3eSDavid Hildenbrand 
3373b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3374b0c632dbSHeiko Carstens {
33758f2abe6aSChristian Borntraeger 	int rc;
3376b0c632dbSHeiko Carstens 	sigset_t sigsaved;
3377b0c632dbSHeiko Carstens 
3378460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
3379460df4c1SPaolo Bonzini 		return -EINTR;
3380460df4c1SPaolo Bonzini 
338127291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
338227291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
338327291e21SDavid Hildenbrand 		return 0;
338427291e21SDavid Hildenbrand 	}
338527291e21SDavid Hildenbrand 
3386b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
3387b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3388b0c632dbSHeiko Carstens 
33896352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
33906852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
33916352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
3392ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
33936352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
33946352e4d2SDavid Hildenbrand 		return -EINVAL;
33956352e4d2SDavid Hildenbrand 	}
3396b0c632dbSHeiko Carstens 
3397b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
3398db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
3399d7b0b5ebSCarsten Otte 
3400dab4079dSHeiko Carstens 	might_fault();
3401e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
34029ace903dSChristian Ehrhardt 
3403b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
3404b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
34058f2abe6aSChristian Borntraeger 		rc = -EINTR;
3406b1d16c49SChristian Ehrhardt 	}
34078f2abe6aSChristian Borntraeger 
340827291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
340927291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
341027291e21SDavid Hildenbrand 		rc = 0;
341127291e21SDavid Hildenbrand 	}
341227291e21SDavid Hildenbrand 
34138f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
341471f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
34158f2abe6aSChristian Borntraeger 		rc = 0;
34168f2abe6aSChristian Borntraeger 	}
34178f2abe6aSChristian Borntraeger 
3418db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
3419b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
3420d7b0b5ebSCarsten Otte 
3421b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
3422b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3423b0c632dbSHeiko Carstens 
3424b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
34257e8e6ab4SHeiko Carstens 	return rc;
3426b0c632dbSHeiko Carstens }
3427b0c632dbSHeiko Carstens 
3428b0c632dbSHeiko Carstens /*
3429b0c632dbSHeiko Carstens  * store status at address
3430b0c632dbSHeiko Carstens  * we use have two special cases:
3431b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3432b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3433b0c632dbSHeiko Carstens  */
3434d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
3435b0c632dbSHeiko Carstens {
3436092670cdSCarsten Otte 	unsigned char archmode = 1;
34379abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
3438fda902cbSMichael Mueller 	unsigned int px;
34394287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
3440d0bce605SHeiko Carstens 	int rc;
3441b0c632dbSHeiko Carstens 
3442d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
3443d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
3444d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
3445b0c632dbSHeiko Carstens 			return -EFAULT;
3446d9a3a09aSMartin Schwidefsky 		gpa = 0;
3447d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
3448d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
3449b0c632dbSHeiko Carstens 			return -EFAULT;
3450d9a3a09aSMartin Schwidefsky 		gpa = px;
3451d9a3a09aSMartin Schwidefsky 	} else
3452d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
34539abc2a08SDavid Hildenbrand 
34549abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
34559abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
34569522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
3457d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
34589abc2a08SDavid Hildenbrand 				     fprs, 128);
34599abc2a08SDavid Hildenbrand 	} else {
34609abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
34616fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
34629abc2a08SDavid Hildenbrand 	}
3463d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
3464d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
3465d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
3466d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
3467d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
3468fda902cbSMichael Mueller 			      &px, 4);
3469d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
34709abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
3471d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
3472d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
34734287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
3474d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
34754287f247SDavid Hildenbrand 			      &cputm, 8);
3476178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
3477d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
3478d0bce605SHeiko Carstens 			      &clkcomp, 8);
3479d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
3480d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
3481d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
3482d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
3483d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
3484b0c632dbSHeiko Carstens }
3485b0c632dbSHeiko Carstens 
3486e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
3487e879892cSThomas Huth {
3488e879892cSThomas Huth 	/*
3489e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
349031d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
3491e879892cSThomas Huth 	 * it into the save area
3492e879892cSThomas Huth 	 */
3493d0164ee2SHendrik Brueckner 	save_fpu_regs();
34949abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3495e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
3496e879892cSThomas Huth 
3497e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
3498e879892cSThomas Huth }
3499e879892cSThomas Huth 
35008ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
35018ad35755SDavid Hildenbrand {
35028ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
35038e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
35048ad35755SDavid Hildenbrand }
35058ad35755SDavid Hildenbrand 
35068ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
35078ad35755SDavid Hildenbrand {
35088ad35755SDavid Hildenbrand 	unsigned int i;
35098ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
35108ad35755SDavid Hildenbrand 
35118ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
35128ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
35138ad35755SDavid Hildenbrand 	}
35148ad35755SDavid Hildenbrand }
35158ad35755SDavid Hildenbrand 
35168ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
35178ad35755SDavid Hildenbrand {
351809a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
351909a400e7SDavid Hildenbrand 		return;
35208ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
35218e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
35228ad35755SDavid Hildenbrand }
35238ad35755SDavid Hildenbrand 
35246852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
35256852d7b6SDavid Hildenbrand {
35268ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
35278ad35755SDavid Hildenbrand 
35288ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
35298ad35755SDavid Hildenbrand 		return;
35308ad35755SDavid Hildenbrand 
35316852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
35328ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3533433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
35348ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
35358ad35755SDavid Hildenbrand 
35368ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
35378ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
35388ad35755SDavid Hildenbrand 			started_vcpus++;
35398ad35755SDavid Hildenbrand 	}
35408ad35755SDavid Hildenbrand 
35418ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
35428ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
35438ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
35448ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
35458ad35755SDavid Hildenbrand 		/*
35468ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
35478ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
35488ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
35498ad35755SDavid Hildenbrand 		 */
35508ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
35518ad35755SDavid Hildenbrand 	}
35528ad35755SDavid Hildenbrand 
3553805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
35548ad35755SDavid Hildenbrand 	/*
35558ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
35568ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
35578ad35755SDavid Hildenbrand 	 */
3558d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3559433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
35608ad35755SDavid Hildenbrand 	return;
35616852d7b6SDavid Hildenbrand }
35626852d7b6SDavid Hildenbrand 
35636852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
35646852d7b6SDavid Hildenbrand {
35658ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
35668ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
35678ad35755SDavid Hildenbrand 
35688ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
35698ad35755SDavid Hildenbrand 		return;
35708ad35755SDavid Hildenbrand 
35716852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
35728ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3573433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
35748ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
35758ad35755SDavid Hildenbrand 
357632f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
35776cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
357832f5ff63SDavid Hildenbrand 
3579805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
35808ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
35818ad35755SDavid Hildenbrand 
35828ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
35838ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
35848ad35755SDavid Hildenbrand 			started_vcpus++;
35858ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
35868ad35755SDavid Hildenbrand 		}
35878ad35755SDavid Hildenbrand 	}
35888ad35755SDavid Hildenbrand 
35898ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
35908ad35755SDavid Hildenbrand 		/*
35918ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
35928ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
35938ad35755SDavid Hildenbrand 		 */
35948ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
35958ad35755SDavid Hildenbrand 	}
35968ad35755SDavid Hildenbrand 
3597433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
35988ad35755SDavid Hildenbrand 	return;
35996852d7b6SDavid Hildenbrand }
36006852d7b6SDavid Hildenbrand 
3601d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3602d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
3603d6712df9SCornelia Huck {
3604d6712df9SCornelia Huck 	int r;
3605d6712df9SCornelia Huck 
3606d6712df9SCornelia Huck 	if (cap->flags)
3607d6712df9SCornelia Huck 		return -EINVAL;
3608d6712df9SCornelia Huck 
3609d6712df9SCornelia Huck 	switch (cap->cap) {
3610fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
3611fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
3612fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
3613c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
3614fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
3615fa6b7fe9SCornelia Huck 		}
3616fa6b7fe9SCornelia Huck 		r = 0;
3617fa6b7fe9SCornelia Huck 		break;
3618d6712df9SCornelia Huck 	default:
3619d6712df9SCornelia Huck 		r = -EINVAL;
3620d6712df9SCornelia Huck 		break;
3621d6712df9SCornelia Huck 	}
3622d6712df9SCornelia Huck 	return r;
3623d6712df9SCornelia Huck }
3624d6712df9SCornelia Huck 
362541408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
362641408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
362741408c28SThomas Huth {
362841408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
362941408c28SThomas Huth 	void *tmpbuf = NULL;
363041408c28SThomas Huth 	int r, srcu_idx;
363141408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
363241408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
363341408c28SThomas Huth 
363441408c28SThomas Huth 	if (mop->flags & ~supported_flags)
363541408c28SThomas Huth 		return -EINVAL;
363641408c28SThomas Huth 
363741408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
363841408c28SThomas Huth 		return -E2BIG;
363941408c28SThomas Huth 
364041408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
364141408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
364241408c28SThomas Huth 		if (!tmpbuf)
364341408c28SThomas Huth 			return -ENOMEM;
364441408c28SThomas Huth 	}
364541408c28SThomas Huth 
364641408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
364741408c28SThomas Huth 
364841408c28SThomas Huth 	switch (mop->op) {
364941408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
365041408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
365192c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
365292c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
365341408c28SThomas Huth 			break;
365441408c28SThomas Huth 		}
365541408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
365641408c28SThomas Huth 		if (r == 0) {
365741408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
365841408c28SThomas Huth 				r = -EFAULT;
365941408c28SThomas Huth 		}
366041408c28SThomas Huth 		break;
366141408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
366241408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
366392c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
366492c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
366541408c28SThomas Huth 			break;
366641408c28SThomas Huth 		}
366741408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
366841408c28SThomas Huth 			r = -EFAULT;
366941408c28SThomas Huth 			break;
367041408c28SThomas Huth 		}
367141408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
367241408c28SThomas Huth 		break;
367341408c28SThomas Huth 	default:
367441408c28SThomas Huth 		r = -EINVAL;
367541408c28SThomas Huth 	}
367641408c28SThomas Huth 
367741408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
367841408c28SThomas Huth 
367941408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
368041408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
368141408c28SThomas Huth 
368241408c28SThomas Huth 	vfree(tmpbuf);
368341408c28SThomas Huth 	return r;
368441408c28SThomas Huth }
368541408c28SThomas Huth 
3686b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
3687b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
3688b0c632dbSHeiko Carstens {
3689b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
3690b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
3691800c1065SThomas Huth 	int idx;
3692bc923cc9SAvi Kivity 	long r;
3693b0c632dbSHeiko Carstens 
369493736624SAvi Kivity 	switch (ioctl) {
369547b43c52SJens Freimann 	case KVM_S390_IRQ: {
369647b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
369747b43c52SJens Freimann 
369847b43c52SJens Freimann 		r = -EFAULT;
369947b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
370047b43c52SJens Freimann 			break;
370147b43c52SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
370247b43c52SJens Freimann 		break;
370347b43c52SJens Freimann 	}
370493736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
3705ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
3706383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
3707ba5c1e9bSCarsten Otte 
370893736624SAvi Kivity 		r = -EFAULT;
3709ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
371093736624SAvi Kivity 			break;
3711383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
3712383d0b05SJens Freimann 			return -EINVAL;
3713383d0b05SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
371493736624SAvi Kivity 		break;
3715ba5c1e9bSCarsten Otte 	}
3716b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
3717800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
3718bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
3719800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
3720bc923cc9SAvi Kivity 		break;
3721b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
3722b0c632dbSHeiko Carstens 		psw_t psw;
3723b0c632dbSHeiko Carstens 
3724bc923cc9SAvi Kivity 		r = -EFAULT;
3725b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
3726bc923cc9SAvi Kivity 			break;
3727bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
3728bc923cc9SAvi Kivity 		break;
3729b0c632dbSHeiko Carstens 	}
3730b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
3731bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3732bc923cc9SAvi Kivity 		break;
373314eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
373414eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
373514eebd91SCarsten Otte 		struct kvm_one_reg reg;
373614eebd91SCarsten Otte 		r = -EFAULT;
373714eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
373814eebd91SCarsten Otte 			break;
373914eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
374014eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
374114eebd91SCarsten Otte 		else
374214eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
374314eebd91SCarsten Otte 		break;
374414eebd91SCarsten Otte 	}
374527e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
374627e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
374727e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
374827e0393fSCarsten Otte 
374927e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
375027e0393fSCarsten Otte 			r = -EFAULT;
375127e0393fSCarsten Otte 			break;
375227e0393fSCarsten Otte 		}
375327e0393fSCarsten Otte 
375427e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
375527e0393fSCarsten Otte 			r = -EINVAL;
375627e0393fSCarsten Otte 			break;
375727e0393fSCarsten Otte 		}
375827e0393fSCarsten Otte 
375927e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
376027e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
376127e0393fSCarsten Otte 		break;
376227e0393fSCarsten Otte 	}
376327e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
376427e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
376527e0393fSCarsten Otte 
376627e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
376727e0393fSCarsten Otte 			r = -EFAULT;
376827e0393fSCarsten Otte 			break;
376927e0393fSCarsten Otte 		}
377027e0393fSCarsten Otte 
377127e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
377227e0393fSCarsten Otte 			r = -EINVAL;
377327e0393fSCarsten Otte 			break;
377427e0393fSCarsten Otte 		}
377527e0393fSCarsten Otte 
377627e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
377727e0393fSCarsten Otte 			ucasmap.length);
377827e0393fSCarsten Otte 		break;
377927e0393fSCarsten Otte 	}
378027e0393fSCarsten Otte #endif
3781ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
3782527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
3783ccc7910fSCarsten Otte 		break;
3784ccc7910fSCarsten Otte 	}
3785d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
3786d6712df9SCornelia Huck 	{
3787d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
3788d6712df9SCornelia Huck 		r = -EFAULT;
3789d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
3790d6712df9SCornelia Huck 			break;
3791d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3792d6712df9SCornelia Huck 		break;
3793d6712df9SCornelia Huck 	}
379441408c28SThomas Huth 	case KVM_S390_MEM_OP: {
379541408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
379641408c28SThomas Huth 
379741408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
379841408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
379941408c28SThomas Huth 		else
380041408c28SThomas Huth 			r = -EFAULT;
380141408c28SThomas Huth 		break;
380241408c28SThomas Huth 	}
3803816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
3804816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3805816c7667SJens Freimann 
3806816c7667SJens Freimann 		r = -EFAULT;
3807816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3808816c7667SJens Freimann 			break;
3809816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
3810816c7667SJens Freimann 		    irq_state.len == 0 ||
3811816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
3812816c7667SJens Freimann 			r = -EINVAL;
3813816c7667SJens Freimann 			break;
3814816c7667SJens Freimann 		}
3815816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
3816816c7667SJens Freimann 					   (void __user *) irq_state.buf,
3817816c7667SJens Freimann 					   irq_state.len);
3818816c7667SJens Freimann 		break;
3819816c7667SJens Freimann 	}
3820816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
3821816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3822816c7667SJens Freimann 
3823816c7667SJens Freimann 		r = -EFAULT;
3824816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3825816c7667SJens Freimann 			break;
3826816c7667SJens Freimann 		if (irq_state.len == 0) {
3827816c7667SJens Freimann 			r = -EINVAL;
3828816c7667SJens Freimann 			break;
3829816c7667SJens Freimann 		}
3830816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
3831816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
3832816c7667SJens Freimann 					   irq_state.len);
3833816c7667SJens Freimann 		break;
3834816c7667SJens Freimann 	}
3835b0c632dbSHeiko Carstens 	default:
38363e6afcf1SCarsten Otte 		r = -ENOTTY;
3837b0c632dbSHeiko Carstens 	}
3838bc923cc9SAvi Kivity 	return r;
3839b0c632dbSHeiko Carstens }
3840b0c632dbSHeiko Carstens 
38415b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
38425b1c1493SCarsten Otte {
38435b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
38445b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
38455b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
38465b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
38475b1c1493SCarsten Otte 		get_page(vmf->page);
38485b1c1493SCarsten Otte 		return 0;
38495b1c1493SCarsten Otte 	}
38505b1c1493SCarsten Otte #endif
38515b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
38525b1c1493SCarsten Otte }
38535b1c1493SCarsten Otte 
38545587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
38555587027cSAneesh Kumar K.V 			    unsigned long npages)
3856db3fe4ebSTakuya Yoshikawa {
3857db3fe4ebSTakuya Yoshikawa 	return 0;
3858db3fe4ebSTakuya Yoshikawa }
3859db3fe4ebSTakuya Yoshikawa 
3860b0c632dbSHeiko Carstens /* Section: memory related */
3861f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
3862f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
386309170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
38647b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
3865b0c632dbSHeiko Carstens {
3866dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
3867dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
3868dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
3869dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
3870b0c632dbSHeiko Carstens 
3871598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
3872b0c632dbSHeiko Carstens 		return -EINVAL;
3873b0c632dbSHeiko Carstens 
3874598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
3875b0c632dbSHeiko Carstens 		return -EINVAL;
3876b0c632dbSHeiko Carstens 
3877a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
3878a3a92c31SDominik Dingel 		return -EINVAL;
3879a3a92c31SDominik Dingel 
3880f7784b8eSMarcelo Tosatti 	return 0;
3881f7784b8eSMarcelo Tosatti }
3882f7784b8eSMarcelo Tosatti 
3883f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
388409170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
38858482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
3886f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
38878482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
3888f7784b8eSMarcelo Tosatti {
3889f7850c92SCarsten Otte 	int rc;
3890f7784b8eSMarcelo Tosatti 
38912cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
38922cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
38932cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
38942cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
38952cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
38962cef4debSChristian Borntraeger 	 */
38972cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
38982cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
38992cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
39002cef4debSChristian Borntraeger 		return;
3901598841caSCarsten Otte 
3902598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
3903598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
3904598841caSCarsten Otte 	if (rc)
3905ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
3906598841caSCarsten Otte 	return;
3907b0c632dbSHeiko Carstens }
3908b0c632dbSHeiko Carstens 
390960a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
391060a37709SAlexander Yarygin {
391160a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
391260a37709SAlexander Yarygin 
391360a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
391460a37709SAlexander Yarygin }
391560a37709SAlexander Yarygin 
39163491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
39173491caf2SChristian Borntraeger {
39183491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
39193491caf2SChristian Borntraeger }
39203491caf2SChristian Borntraeger 
3921b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
3922b0c632dbSHeiko Carstens {
392360a37709SAlexander Yarygin 	int i;
392460a37709SAlexander Yarygin 
392507197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
392607197fd0SDavid Hildenbrand 		pr_info("SIE not available\n");
392707197fd0SDavid Hildenbrand 		return -ENODEV;
392807197fd0SDavid Hildenbrand 	}
392907197fd0SDavid Hildenbrand 
393060a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
393160a37709SAlexander Yarygin 		kvm_s390_fac_list_mask[i] |=
393260a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
393360a37709SAlexander Yarygin 
39349d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
3935b0c632dbSHeiko Carstens }
3936b0c632dbSHeiko Carstens 
3937b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
3938b0c632dbSHeiko Carstens {
3939b0c632dbSHeiko Carstens 	kvm_exit();
3940b0c632dbSHeiko Carstens }
3941b0c632dbSHeiko Carstens 
3942b0c632dbSHeiko Carstens module_init(kvm_s390_init);
3943b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
3944566af940SCornelia Huck 
3945566af940SCornelia Huck /*
3946566af940SCornelia Huck  * Enable autoloading of the kvm module.
3947566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3948566af940SCornelia Huck  * since x86 takes a different approach.
3949566af940SCornelia Huck  */
3950566af940SCornelia Huck #include <linux/miscdevice.h>
3951566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
3952566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
3953