xref: /linux/arch/s390/kvm/kvm-s390.c (revision e1788bb995befed5831c99cee6527dfb080a46c0)
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>
26a374e892STony Krowiak #include <linux/random.h>
27b0c632dbSHeiko Carstens #include <linux/slab.h>
28ba5c1e9bSCarsten Otte #include <linux/timer.h>
2941408c28SThomas Huth #include <linux/vmalloc.h>
3015c9705fSDavid Hildenbrand #include <linux/bitmap.h>
31cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
32b0c632dbSHeiko Carstens #include <asm/lowcore.h>
33fd5ada04SMartin Schwidefsky #include <asm/stp.h>
34b0c632dbSHeiko Carstens #include <asm/pgtable.h>
351e133ab2SMartin Schwidefsky #include <asm/gmap.h>
36f5daba1dSHeiko Carstens #include <asm/nmi.h>
37a0616cdeSDavid Howells #include <asm/switch_to.h>
386d3da241SJens Freimann #include <asm/isc.h>
391526bf9cSChristian Borntraeger #include <asm/sclp.h>
400a763c78SDavid Hildenbrand #include <asm/cpacf.h>
41221bb8a4SLinus Torvalds #include <asm/timex.h>
428f2abe6aSChristian Borntraeger #include "kvm-s390.h"
43b0c632dbSHeiko Carstens #include "gaccess.h"
44b0c632dbSHeiko Carstens 
45ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
46ea2cdd27SDavid Hildenbrand #undef pr_fmt
47ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
48ea2cdd27SDavid Hildenbrand 
495786fffaSCornelia Huck #define CREATE_TRACE_POINTS
505786fffaSCornelia Huck #include "trace.h"
51ade38c31SCornelia Huck #include "trace-s390.h"
525786fffaSCornelia Huck 
5341408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
54816c7667SJens Freimann #define LOCAL_IRQS 32
55816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
56816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
5741408c28SThomas Huth 
58b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
59b0c632dbSHeiko Carstens 
60b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
61b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
620eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
638f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
648f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
658f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
668f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
67ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
689ec6de19SAlexander Yarygin 	{ "exit_pei", VCPU_STAT(exit_pei) },
69ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
70ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
71a011eeb2SJanosch Frank 	{ "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
72f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
7362bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
743491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
75ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
76f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
77ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
78aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
79aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
80ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
817697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
82ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
83ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
84ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
85ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
86ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
87ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
88ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
8969d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
90453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
91453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
92453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
93453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
94453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
958a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
96453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
97453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
98b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
99453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
100453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
101bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
10295ca2cb5SJanosch Frank 	{ "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
103a3508fbeSDavid Hildenbrand 	{ "instruction_sie", VCPU_STAT(instruction_sie) },
1045288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
105bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
1067697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
1075288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
10842cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
10942cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1105288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
11142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
11242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
113cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1145288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1155288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1165288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
11742cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
11842cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
11942cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
120388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
121e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
12241628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
123175a5c9eSChristian Borntraeger 	{ "diagnose_258", VCPU_STAT(diagnose_258) },
124175a5c9eSChristian Borntraeger 	{ "diagnose_308", VCPU_STAT(diagnose_308) },
125175a5c9eSChristian Borntraeger 	{ "diagnose_500", VCPU_STAT(diagnose_500) },
126b0c632dbSHeiko Carstens 	{ NULL }
127b0c632dbSHeiko Carstens };
128b0c632dbSHeiko Carstens 
129a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
130a411edf1SDavid Hildenbrand static int nested;
131a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
132a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
133a411edf1SDavid Hildenbrand 
1349d8d5786SMichael Mueller /* upper facilities limit for kvm */
135f6c1d359SHeiko Carstens unsigned long kvm_s390_fac_list_mask[16] = { FACILITIES_KVM };
136b0c632dbSHeiko Carstens 
1379d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void)
13878c4b59fSMichael Mueller {
1399d8d5786SMichael Mueller 	BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
1409d8d5786SMichael Mueller 	return ARRAY_SIZE(kvm_s390_fac_list_mask);
14178c4b59fSMichael Mueller }
14278c4b59fSMichael Mueller 
14315c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
14415c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1450a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
1460a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
14715c9705fSDavid Hildenbrand 
1489d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
149a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
15078f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
1519d8d5786SMichael Mueller 
152b0c632dbSHeiko Carstens /* Section: not file related */
15313a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
154b0c632dbSHeiko Carstens {
155b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
15610474ae8SAlexander Graf 	return 0;
157b0c632dbSHeiko Carstens }
158b0c632dbSHeiko Carstens 
159414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
160414d3b07SMartin Schwidefsky 			      unsigned long end);
1612c70fe44SChristian Borntraeger 
162fdf03650SFan Zhang /*
163fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
164fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
165fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
166fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
167fdf03650SFan Zhang  */
168fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
169fdf03650SFan Zhang 			  void *v)
170fdf03650SFan Zhang {
171fdf03650SFan Zhang 	struct kvm *kvm;
172fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
173fdf03650SFan Zhang 	int i;
174fdf03650SFan Zhang 	unsigned long long *delta = v;
175fdf03650SFan Zhang 
176fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
177fdf03650SFan Zhang 		kvm->arch.epoch -= *delta;
178fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
179fdf03650SFan Zhang 			vcpu->arch.sie_block->epoch -= *delta;
180db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
181db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
18291473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
18391473b48SDavid Hildenbrand 				vcpu->arch.vsie_block->epoch -= *delta;
184fdf03650SFan Zhang 		}
185fdf03650SFan Zhang 	}
186fdf03650SFan Zhang 	return NOTIFY_OK;
187fdf03650SFan Zhang }
188fdf03650SFan Zhang 
189fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
190fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
191fdf03650SFan Zhang };
192fdf03650SFan Zhang 
193b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
194b0c632dbSHeiko Carstens {
1952c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
196b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
197a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
198a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
199fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
200fdf03650SFan Zhang 				       &kvm_clock_notifier);
201b0c632dbSHeiko Carstens 	return 0;
202b0c632dbSHeiko Carstens }
203b0c632dbSHeiko Carstens 
204b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
205b0c632dbSHeiko Carstens {
206b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
207a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
208fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
209fdf03650SFan Zhang 					 &kvm_clock_notifier);
210b0c632dbSHeiko Carstens }
211b0c632dbSHeiko Carstens 
21222be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
21322be5a13SDavid Hildenbrand {
21422be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
21522be5a13SDavid Hildenbrand }
21622be5a13SDavid Hildenbrand 
2170a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
2180a763c78SDavid Hildenbrand {
2190a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
2200a763c78SDavid Hildenbrand 	int cc = 3; /* subfunction not available */
2210a763c78SDavid Hildenbrand 
2220a763c78SDavid Hildenbrand 	asm volatile(
2230a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
2240a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
2250a763c78SDavid Hildenbrand 		"	ipm	%0\n"
2260a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
2270a763c78SDavid Hildenbrand 		: "=d" (cc)
2280a763c78SDavid Hildenbrand 		: "d" (r0)
2290a763c78SDavid Hildenbrand 		: "cc");
2300a763c78SDavid Hildenbrand 	return cc == 0;
2310a763c78SDavid Hildenbrand }
2320a763c78SDavid Hildenbrand 
23322be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
23422be5a13SDavid Hildenbrand {
2350a763c78SDavid Hildenbrand 	int i;
2360a763c78SDavid Hildenbrand 
2370a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
2380a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
2390a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
2400a763c78SDavid Hildenbrand 	}
2410a763c78SDavid Hildenbrand 
2420a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
243221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
244221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
245221bb8a4SLinus Torvalds 		     PTFF_QAF);
2460a763c78SDavid Hildenbrand 
2470a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
24869c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
24969c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
25069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
25169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
25269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
25369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
25469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
25569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
25669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
25769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
2580a763c78SDavid Hildenbrand 	}
2590a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
26069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
26169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
2620a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
26369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
26469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
26569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
26669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
26769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
26869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
26969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
27069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
2710a763c78SDavid Hildenbrand 	}
2720a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
27369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PPNO, (cpacf_mask_t *)
27469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
2750a763c78SDavid Hildenbrand 
27622be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
27722be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
278a3508fbeSDavid Hildenbrand 	/*
279a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
280a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
281a3508fbeSDavid Hildenbrand 	 */
282a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
283a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
284a3508fbeSDavid Hildenbrand 		return;
285a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
28619c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
28719c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
2880615a326SDavid Hildenbrand 	if (sclp.has_siif)
2890615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
29077d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
29177d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
292a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
293a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
2945630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
2955630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
29613ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
29713ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
2987fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
2997fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
3005d3876a8SDavid Hildenbrand 	/*
3015d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
3025d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
3035d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
3045d3876a8SDavid Hildenbrand 	 *
3055d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
3065d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
3075d3876a8SDavid Hildenbrand 	 *
3085d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
3095d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
3105d3876a8SDavid Hildenbrand 	 *
3115d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
3125d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
3135d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
3145d3876a8SDavid Hildenbrand 	 *
3155d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
3165d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
3175d3876a8SDavid Hildenbrand 	 */
31822be5a13SDavid Hildenbrand }
31922be5a13SDavid Hildenbrand 
320b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
321b0c632dbSHeiko Carstens {
32278f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
32378f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
32478f26131SChristian Borntraeger 		return -ENOMEM;
32578f26131SChristian Borntraeger 
32678f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
32778f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
32878f26131SChristian Borntraeger 		return -ENOMEM;
32978f26131SChristian Borntraeger 	}
33078f26131SChristian Borntraeger 
33122be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
33222be5a13SDavid Hildenbrand 
33384877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
33484877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
335b0c632dbSHeiko Carstens }
336b0c632dbSHeiko Carstens 
33778f26131SChristian Borntraeger void kvm_arch_exit(void)
33878f26131SChristian Borntraeger {
33978f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
34078f26131SChristian Borntraeger }
34178f26131SChristian Borntraeger 
342b0c632dbSHeiko Carstens /* Section: device related */
343b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
344b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
345b0c632dbSHeiko Carstens {
346b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
347b0c632dbSHeiko Carstens 		return s390_enable_sie();
348b0c632dbSHeiko Carstens 	return -EINVAL;
349b0c632dbSHeiko Carstens }
350b0c632dbSHeiko Carstens 
351784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
352b0c632dbSHeiko Carstens {
353d7b0b5ebSCarsten Otte 	int r;
354d7b0b5ebSCarsten Otte 
3552bd0ac4eSCarsten Otte 	switch (ext) {
356d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
357b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
35852e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
3591efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
3601efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
3611efd0f59SCarsten Otte #endif
3623c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
36360b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
36414eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
365d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
366fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
36710ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
368c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
369d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
37078599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
371f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
3726352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
37347b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
3742444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
375e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
37630ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
377816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
3786502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
379d7b0b5ebSCarsten Otte 		r = 1;
380d7b0b5ebSCarsten Otte 		break;
38141408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
38241408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
38341408c28SThomas Huth 		break;
384e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
385e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
38676a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
387a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
388a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
389a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
39076a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
391e726b1bdSChristian Borntraeger 		break;
392e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
393e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
394e1e2e605SNick Wang 		break;
3951526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
396abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
3971526bf9cSChristian Borntraeger 		break;
39868c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
39968c55750SEric Farman 		r = MACHINE_HAS_VX;
40068c55750SEric Farman 		break;
401c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
402c6e5f166SFan Zhang 		r = test_facility(64);
403c6e5f166SFan Zhang 		break;
4042bd0ac4eSCarsten Otte 	default:
405d7b0b5ebSCarsten Otte 		r = 0;
406b0c632dbSHeiko Carstens 	}
407d7b0b5ebSCarsten Otte 	return r;
4082bd0ac4eSCarsten Otte }
409b0c632dbSHeiko Carstens 
41015f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
41115f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
41215f36ebdSJason J. Herne {
41315f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
41415f36ebdSJason J. Herne 	unsigned long address;
41515f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
41615f36ebdSJason J. Herne 
41715f36ebdSJason J. Herne 	/* Loop over all guest pages */
41815f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
41915f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
42015f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
42115f36ebdSJason J. Herne 
4221e133ab2SMartin Schwidefsky 		if (test_and_clear_guest_dirty(gmap->mm, address))
42315f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
4241763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
4251763f8d0SChristian Borntraeger 			return;
42670c88a00SChristian Borntraeger 		cond_resched();
42715f36ebdSJason J. Herne 	}
42815f36ebdSJason J. Herne }
42915f36ebdSJason J. Herne 
430b0c632dbSHeiko Carstens /* Section: vm related */
431a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
432a6e2f683SEugene (jno) Dvurechenski 
433b0c632dbSHeiko Carstens /*
434b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
435b0c632dbSHeiko Carstens  */
436b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
437b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
438b0c632dbSHeiko Carstens {
43915f36ebdSJason J. Herne 	int r;
44015f36ebdSJason J. Herne 	unsigned long n;
4419f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
44215f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
44315f36ebdSJason J. Herne 	int is_dirty = 0;
44415f36ebdSJason J. Herne 
44515f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
44615f36ebdSJason J. Herne 
44715f36ebdSJason J. Herne 	r = -EINVAL;
44815f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
44915f36ebdSJason J. Herne 		goto out;
45015f36ebdSJason J. Herne 
4519f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
4529f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
45315f36ebdSJason J. Herne 	r = -ENOENT;
45415f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
45515f36ebdSJason J. Herne 		goto out;
45615f36ebdSJason J. Herne 
45715f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
45815f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
45915f36ebdSJason J. Herne 	if (r)
46015f36ebdSJason J. Herne 		goto out;
46115f36ebdSJason J. Herne 
46215f36ebdSJason J. Herne 	/* Clear the dirty log */
46315f36ebdSJason J. Herne 	if (is_dirty) {
46415f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
46515f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
46615f36ebdSJason J. Herne 	}
46715f36ebdSJason J. Herne 	r = 0;
46815f36ebdSJason J. Herne out:
46915f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
47015f36ebdSJason J. Herne 	return r;
471b0c632dbSHeiko Carstens }
472b0c632dbSHeiko Carstens 
4736502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
4746502a34cSDavid Hildenbrand {
4756502a34cSDavid Hildenbrand 	unsigned int i;
4766502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
4776502a34cSDavid Hildenbrand 
4786502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
4796502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
4806502a34cSDavid Hildenbrand 	}
4816502a34cSDavid Hildenbrand }
4826502a34cSDavid Hildenbrand 
483d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
484d938dc55SCornelia Huck {
485d938dc55SCornelia Huck 	int r;
486d938dc55SCornelia Huck 
487d938dc55SCornelia Huck 	if (cap->flags)
488d938dc55SCornelia Huck 		return -EINVAL;
489d938dc55SCornelia Huck 
490d938dc55SCornelia Huck 	switch (cap->cap) {
49184223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
492c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
49384223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
49484223598SCornelia Huck 		r = 0;
49584223598SCornelia Huck 		break;
4962444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
497c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
4982444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
4992444b352SDavid Hildenbrand 		r = 0;
5002444b352SDavid Hildenbrand 		break;
50168c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
5025967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
503a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
5045967c17bSDavid Hildenbrand 			r = -EBUSY;
5055967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
506c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
507c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
50818280d8bSMichael Mueller 			r = 0;
50918280d8bSMichael Mueller 		} else
51018280d8bSMichael Mueller 			r = -EINVAL;
5115967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
512c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
513c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
51468c55750SEric Farman 		break;
515c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
516c6e5f166SFan Zhang 		r = -EINVAL;
517c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
518a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
519c6e5f166SFan Zhang 			r = -EBUSY;
520c6e5f166SFan Zhang 		} else if (test_facility(64)) {
521c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
522c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
523c6e5f166SFan Zhang 			r = 0;
524c6e5f166SFan Zhang 		}
525c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
526c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
527c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
528c6e5f166SFan Zhang 		break;
529e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
530c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
531e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
532e44fc8c9SEkaterina Tumanova 		r = 0;
533e44fc8c9SEkaterina Tumanova 		break;
5346502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5356502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
5366502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
5376502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
5386502a34cSDavid Hildenbrand 		r = 0;
5396502a34cSDavid Hildenbrand 		break;
540d938dc55SCornelia Huck 	default:
541d938dc55SCornelia Huck 		r = -EINVAL;
542d938dc55SCornelia Huck 		break;
543d938dc55SCornelia Huck 	}
544d938dc55SCornelia Huck 	return r;
545d938dc55SCornelia Huck }
546d938dc55SCornelia Huck 
5478c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
5488c0a7ce6SDominik Dingel {
5498c0a7ce6SDominik Dingel 	int ret;
5508c0a7ce6SDominik Dingel 
5518c0a7ce6SDominik Dingel 	switch (attr->attr) {
5528c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
5538c0a7ce6SDominik Dingel 		ret = 0;
554c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
555a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
556a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
5578c0a7ce6SDominik Dingel 			ret = -EFAULT;
5588c0a7ce6SDominik Dingel 		break;
5598c0a7ce6SDominik Dingel 	default:
5608c0a7ce6SDominik Dingel 		ret = -ENXIO;
5618c0a7ce6SDominik Dingel 		break;
5628c0a7ce6SDominik Dingel 	}
5638c0a7ce6SDominik Dingel 	return ret;
5648c0a7ce6SDominik Dingel }
5658c0a7ce6SDominik Dingel 
5668c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
5674f718eabSDominik Dingel {
5684f718eabSDominik Dingel 	int ret;
5694f718eabSDominik Dingel 	unsigned int idx;
5704f718eabSDominik Dingel 	switch (attr->attr) {
5714f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
572f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
573c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
574e6db1d61SDominik Dingel 			break;
575e6db1d61SDominik Dingel 
5764f718eabSDominik Dingel 		ret = -EBUSY;
577c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
5784f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
579a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
5804f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
5814f718eabSDominik Dingel 			ret = 0;
5824f718eabSDominik Dingel 		}
5834f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
5844f718eabSDominik Dingel 		break;
5854f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
586f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
587f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
588f9cbd9b0SDavid Hildenbrand 			break;
589c3489155SDominik Dingel 		ret = -EINVAL;
590c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
591c3489155SDominik Dingel 			break;
592c3489155SDominik Dingel 
593c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
5944f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
5954f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
596a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
5974f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
5984f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
5994f718eabSDominik Dingel 		ret = 0;
6004f718eabSDominik Dingel 		break;
6018c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
6028c0a7ce6SDominik Dingel 		unsigned long new_limit;
6038c0a7ce6SDominik Dingel 
6048c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
6058c0a7ce6SDominik Dingel 			return -EINVAL;
6068c0a7ce6SDominik Dingel 
6078c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
6088c0a7ce6SDominik Dingel 			return -EFAULT;
6098c0a7ce6SDominik Dingel 
610a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
611a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
6128c0a7ce6SDominik Dingel 			return -E2BIG;
6138c0a7ce6SDominik Dingel 
614a3a92c31SDominik Dingel 		if (!new_limit)
615a3a92c31SDominik Dingel 			return -EINVAL;
616a3a92c31SDominik Dingel 
6176ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
618a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
619a3a92c31SDominik Dingel 			new_limit -= 1;
620a3a92c31SDominik Dingel 
6218c0a7ce6SDominik Dingel 		ret = -EBUSY;
6228c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
623a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6246ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
6256ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
6268c0a7ce6SDominik Dingel 
6278c0a7ce6SDominik Dingel 			if (!new) {
6288c0a7ce6SDominik Dingel 				ret = -ENOMEM;
6298c0a7ce6SDominik Dingel 			} else {
6306ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
6318c0a7ce6SDominik Dingel 				new->private = kvm;
6328c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
6338c0a7ce6SDominik Dingel 				ret = 0;
6348c0a7ce6SDominik Dingel 			}
6358c0a7ce6SDominik Dingel 		}
6368c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
637a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
638a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
639a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
6408c0a7ce6SDominik Dingel 		break;
6418c0a7ce6SDominik Dingel 	}
6424f718eabSDominik Dingel 	default:
6434f718eabSDominik Dingel 		ret = -ENXIO;
6444f718eabSDominik Dingel 		break;
6454f718eabSDominik Dingel 	}
6464f718eabSDominik Dingel 	return ret;
6474f718eabSDominik Dingel }
6484f718eabSDominik Dingel 
649a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
650a374e892STony Krowiak 
651a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
652a374e892STony Krowiak {
653a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
654a374e892STony Krowiak 	int i;
655a374e892STony Krowiak 
6569d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
657a374e892STony Krowiak 		return -EINVAL;
658a374e892STony Krowiak 
659a374e892STony Krowiak 	mutex_lock(&kvm->lock);
660a374e892STony Krowiak 	switch (attr->attr) {
661a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
662a374e892STony Krowiak 		get_random_bytes(
663a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
664a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
665a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
666c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
667a374e892STony Krowiak 		break;
668a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
669a374e892STony Krowiak 		get_random_bytes(
670a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
671a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
672a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
673c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
674a374e892STony Krowiak 		break;
675a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
676a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
677a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
678a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
679c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
680a374e892STony Krowiak 		break;
681a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
682a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
683a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
684a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
685c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
686a374e892STony Krowiak 		break;
687a374e892STony Krowiak 	default:
688a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
689a374e892STony Krowiak 		return -ENXIO;
690a374e892STony Krowiak 	}
691a374e892STony Krowiak 
692a374e892STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm) {
693a374e892STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
694a374e892STony Krowiak 		exit_sie(vcpu);
695a374e892STony Krowiak 	}
696a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
697a374e892STony Krowiak 	return 0;
698a374e892STony Krowiak }
699a374e892STony Krowiak 
70072f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
70172f25020SJason J. Herne {
70272f25020SJason J. Herne 	u8 gtod_high;
70372f25020SJason J. Herne 
70472f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
70572f25020SJason J. Herne 					   sizeof(gtod_high)))
70672f25020SJason J. Herne 		return -EFAULT;
70772f25020SJason J. Herne 
70872f25020SJason J. Herne 	if (gtod_high != 0)
70972f25020SJason J. Herne 		return -EINVAL;
71058c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
71172f25020SJason J. Herne 
71272f25020SJason J. Herne 	return 0;
71372f25020SJason J. Herne }
71472f25020SJason J. Herne 
71572f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
71672f25020SJason J. Herne {
7175a3d883aSDavid Hildenbrand 	u64 gtod;
71872f25020SJason J. Herne 
71972f25020SJason J. Herne 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
72072f25020SJason J. Herne 		return -EFAULT;
72172f25020SJason J. Herne 
72225ed1675SDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, gtod);
72358c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
72472f25020SJason J. Herne 	return 0;
72572f25020SJason J. Herne }
72672f25020SJason J. Herne 
72772f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
72872f25020SJason J. Herne {
72972f25020SJason J. Herne 	int ret;
73072f25020SJason J. Herne 
73172f25020SJason J. Herne 	if (attr->flags)
73272f25020SJason J. Herne 		return -EINVAL;
73372f25020SJason J. Herne 
73472f25020SJason J. Herne 	switch (attr->attr) {
73572f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
73672f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
73772f25020SJason J. Herne 		break;
73872f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
73972f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
74072f25020SJason J. Herne 		break;
74172f25020SJason J. Herne 	default:
74272f25020SJason J. Herne 		ret = -ENXIO;
74372f25020SJason J. Herne 		break;
74472f25020SJason J. Herne 	}
74572f25020SJason J. Herne 	return ret;
74672f25020SJason J. Herne }
74772f25020SJason J. Herne 
74872f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
74972f25020SJason J. Herne {
75072f25020SJason J. Herne 	u8 gtod_high = 0;
75172f25020SJason J. Herne 
75272f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
75372f25020SJason J. Herne 					 sizeof(gtod_high)))
75472f25020SJason J. Herne 		return -EFAULT;
75558c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
75672f25020SJason J. Herne 
75772f25020SJason J. Herne 	return 0;
75872f25020SJason J. Herne }
75972f25020SJason J. Herne 
76072f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
76172f25020SJason J. Herne {
7625a3d883aSDavid Hildenbrand 	u64 gtod;
76372f25020SJason J. Herne 
76460417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
76572f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
76672f25020SJason J. Herne 		return -EFAULT;
76758c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
76872f25020SJason J. Herne 
76972f25020SJason J. Herne 	return 0;
77072f25020SJason J. Herne }
77172f25020SJason J. Herne 
77272f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
77372f25020SJason J. Herne {
77472f25020SJason J. Herne 	int ret;
77572f25020SJason J. Herne 
77672f25020SJason J. Herne 	if (attr->flags)
77772f25020SJason J. Herne 		return -EINVAL;
77872f25020SJason J. Herne 
77972f25020SJason J. Herne 	switch (attr->attr) {
78072f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
78172f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
78272f25020SJason J. Herne 		break;
78372f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
78472f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
78572f25020SJason J. Herne 		break;
78672f25020SJason J. Herne 	default:
78772f25020SJason J. Herne 		ret = -ENXIO;
78872f25020SJason J. Herne 		break;
78972f25020SJason J. Herne 	}
79072f25020SJason J. Herne 	return ret;
79172f25020SJason J. Herne }
79272f25020SJason J. Herne 
793658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
794658b6edaSMichael Mueller {
795658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
796053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
797658b6edaSMichael Mueller 	int ret = 0;
798658b6edaSMichael Mueller 
799658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
800a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
801658b6edaSMichael Mueller 		ret = -EBUSY;
802658b6edaSMichael Mueller 		goto out;
803658b6edaSMichael Mueller 	}
804658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
805658b6edaSMichael Mueller 	if (!proc) {
806658b6edaSMichael Mueller 		ret = -ENOMEM;
807658b6edaSMichael Mueller 		goto out;
808658b6edaSMichael Mueller 	}
809658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
810658b6edaSMichael Mueller 			    sizeof(*proc))) {
8119bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
812053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
813053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
8140487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
815053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
816053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
817053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
818053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
819053dd230SDavid Hildenbrand 			else
820658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
821053dd230SDavid Hildenbrand 		}
822c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
823658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
824658b6edaSMichael Mueller 	} else
825658b6edaSMichael Mueller 		ret = -EFAULT;
826658b6edaSMichael Mueller 	kfree(proc);
827658b6edaSMichael Mueller out:
828658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
829658b6edaSMichael Mueller 	return ret;
830658b6edaSMichael Mueller }
831658b6edaSMichael Mueller 
83215c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
83315c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
83415c9705fSDavid Hildenbrand {
83515c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
83615c9705fSDavid Hildenbrand 	int ret = -EBUSY;
83715c9705fSDavid Hildenbrand 
83815c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
83915c9705fSDavid Hildenbrand 		return -EFAULT;
84015c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
84115c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
84215c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
84315c9705fSDavid Hildenbrand 		return -EINVAL;
84415c9705fSDavid Hildenbrand 
84515c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
84615c9705fSDavid Hildenbrand 	if (!atomic_read(&kvm->online_vcpus)) {
84715c9705fSDavid Hildenbrand 		bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
84815c9705fSDavid Hildenbrand 			    KVM_S390_VM_CPU_FEAT_NR_BITS);
84915c9705fSDavid Hildenbrand 		ret = 0;
85015c9705fSDavid Hildenbrand 	}
85115c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
85215c9705fSDavid Hildenbrand 	return ret;
85315c9705fSDavid Hildenbrand }
85415c9705fSDavid Hildenbrand 
8550a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
8560a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
8570a763c78SDavid Hildenbrand {
8580a763c78SDavid Hildenbrand 	/*
8590a763c78SDavid Hildenbrand 	 * Once supported by kernel + hw, we have to store the subfunctions
8600a763c78SDavid Hildenbrand 	 * in kvm->arch and remember that user space configured them.
8610a763c78SDavid Hildenbrand 	 */
8620a763c78SDavid Hildenbrand 	return -ENXIO;
8630a763c78SDavid Hildenbrand }
8640a763c78SDavid Hildenbrand 
865658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
866658b6edaSMichael Mueller {
867658b6edaSMichael Mueller 	int ret = -ENXIO;
868658b6edaSMichael Mueller 
869658b6edaSMichael Mueller 	switch (attr->attr) {
870658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
871658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
872658b6edaSMichael Mueller 		break;
87315c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
87415c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
87515c9705fSDavid Hildenbrand 		break;
8760a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
8770a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
8780a763c78SDavid Hildenbrand 		break;
879658b6edaSMichael Mueller 	}
880658b6edaSMichael Mueller 	return ret;
881658b6edaSMichael Mueller }
882658b6edaSMichael Mueller 
883658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
884658b6edaSMichael Mueller {
885658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
886658b6edaSMichael Mueller 	int ret = 0;
887658b6edaSMichael Mueller 
888658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
889658b6edaSMichael Mueller 	if (!proc) {
890658b6edaSMichael Mueller 		ret = -ENOMEM;
891658b6edaSMichael Mueller 		goto out;
892658b6edaSMichael Mueller 	}
8939bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
894658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
895c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
896c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
897658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
898658b6edaSMichael Mueller 		ret = -EFAULT;
899658b6edaSMichael Mueller 	kfree(proc);
900658b6edaSMichael Mueller out:
901658b6edaSMichael Mueller 	return ret;
902658b6edaSMichael Mueller }
903658b6edaSMichael Mueller 
904658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
905658b6edaSMichael Mueller {
906658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
907658b6edaSMichael Mueller 	int ret = 0;
908658b6edaSMichael Mueller 
909658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
910658b6edaSMichael Mueller 	if (!mach) {
911658b6edaSMichael Mueller 		ret = -ENOMEM;
912658b6edaSMichael Mueller 		goto out;
913658b6edaSMichael Mueller 	}
914658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
91537c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
916c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
917981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
918658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
91994422ee8SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
920658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
921658b6edaSMichael Mueller 		ret = -EFAULT;
922658b6edaSMichael Mueller 	kfree(mach);
923658b6edaSMichael Mueller out:
924658b6edaSMichael Mueller 	return ret;
925658b6edaSMichael Mueller }
926658b6edaSMichael Mueller 
92715c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
92815c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
92915c9705fSDavid Hildenbrand {
93015c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
93115c9705fSDavid Hildenbrand 
93215c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
93315c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
93415c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
93515c9705fSDavid Hildenbrand 		return -EFAULT;
93615c9705fSDavid Hildenbrand 	return 0;
93715c9705fSDavid Hildenbrand }
93815c9705fSDavid Hildenbrand 
93915c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
94015c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
94115c9705fSDavid Hildenbrand {
94215c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
94315c9705fSDavid Hildenbrand 
94415c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
94515c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
94615c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
94715c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
94815c9705fSDavid Hildenbrand 		return -EFAULT;
94915c9705fSDavid Hildenbrand 	return 0;
95015c9705fSDavid Hildenbrand }
95115c9705fSDavid Hildenbrand 
9520a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
9530a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
9540a763c78SDavid Hildenbrand {
9550a763c78SDavid Hildenbrand 	/*
9560a763c78SDavid Hildenbrand 	 * Once we can actually configure subfunctions (kernel + hw support),
9570a763c78SDavid Hildenbrand 	 * we have to check if they were already set by user space, if so copy
9580a763c78SDavid Hildenbrand 	 * them from kvm->arch.
9590a763c78SDavid Hildenbrand 	 */
9600a763c78SDavid Hildenbrand 	return -ENXIO;
9610a763c78SDavid Hildenbrand }
9620a763c78SDavid Hildenbrand 
9630a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
9640a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
9650a763c78SDavid Hildenbrand {
9660a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
9670a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
9680a763c78SDavid Hildenbrand 		return -EFAULT;
9690a763c78SDavid Hildenbrand 	return 0;
9700a763c78SDavid Hildenbrand }
971658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
972658b6edaSMichael Mueller {
973658b6edaSMichael Mueller 	int ret = -ENXIO;
974658b6edaSMichael Mueller 
975658b6edaSMichael Mueller 	switch (attr->attr) {
976658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
977658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
978658b6edaSMichael Mueller 		break;
979658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
980658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
981658b6edaSMichael Mueller 		break;
98215c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
98315c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
98415c9705fSDavid Hildenbrand 		break;
98515c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
98615c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
98715c9705fSDavid Hildenbrand 		break;
9880a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
9890a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
9900a763c78SDavid Hildenbrand 		break;
9910a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
9920a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
9930a763c78SDavid Hildenbrand 		break;
994658b6edaSMichael Mueller 	}
995658b6edaSMichael Mueller 	return ret;
996658b6edaSMichael Mueller }
997658b6edaSMichael Mueller 
998f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
999f2061656SDominik Dingel {
1000f2061656SDominik Dingel 	int ret;
1001f2061656SDominik Dingel 
1002f2061656SDominik Dingel 	switch (attr->group) {
10034f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
10048c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
10054f718eabSDominik Dingel 		break;
100672f25020SJason J. Herne 	case KVM_S390_VM_TOD:
100772f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
100872f25020SJason J. Herne 		break;
1009658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1010658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1011658b6edaSMichael Mueller 		break;
1012a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1013a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1014a374e892STony Krowiak 		break;
1015f2061656SDominik Dingel 	default:
1016f2061656SDominik Dingel 		ret = -ENXIO;
1017f2061656SDominik Dingel 		break;
1018f2061656SDominik Dingel 	}
1019f2061656SDominik Dingel 
1020f2061656SDominik Dingel 	return ret;
1021f2061656SDominik Dingel }
1022f2061656SDominik Dingel 
1023f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1024f2061656SDominik Dingel {
10258c0a7ce6SDominik Dingel 	int ret;
10268c0a7ce6SDominik Dingel 
10278c0a7ce6SDominik Dingel 	switch (attr->group) {
10288c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
10298c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
10308c0a7ce6SDominik Dingel 		break;
103172f25020SJason J. Herne 	case KVM_S390_VM_TOD:
103272f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
103372f25020SJason J. Herne 		break;
1034658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1035658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1036658b6edaSMichael Mueller 		break;
10378c0a7ce6SDominik Dingel 	default:
10388c0a7ce6SDominik Dingel 		ret = -ENXIO;
10398c0a7ce6SDominik Dingel 		break;
10408c0a7ce6SDominik Dingel 	}
10418c0a7ce6SDominik Dingel 
10428c0a7ce6SDominik Dingel 	return ret;
1043f2061656SDominik Dingel }
1044f2061656SDominik Dingel 
1045f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1046f2061656SDominik Dingel {
1047f2061656SDominik Dingel 	int ret;
1048f2061656SDominik Dingel 
1049f2061656SDominik Dingel 	switch (attr->group) {
10504f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
10514f718eabSDominik Dingel 		switch (attr->attr) {
10524f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
10534f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1054f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1055f9cbd9b0SDavid Hildenbrand 			break;
10568c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
10574f718eabSDominik Dingel 			ret = 0;
10584f718eabSDominik Dingel 			break;
10594f718eabSDominik Dingel 		default:
10604f718eabSDominik Dingel 			ret = -ENXIO;
10614f718eabSDominik Dingel 			break;
10624f718eabSDominik Dingel 		}
10634f718eabSDominik Dingel 		break;
106472f25020SJason J. Herne 	case KVM_S390_VM_TOD:
106572f25020SJason J. Herne 		switch (attr->attr) {
106672f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
106772f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
106872f25020SJason J. Herne 			ret = 0;
106972f25020SJason J. Herne 			break;
107072f25020SJason J. Herne 		default:
107172f25020SJason J. Herne 			ret = -ENXIO;
107272f25020SJason J. Herne 			break;
107372f25020SJason J. Herne 		}
107472f25020SJason J. Herne 		break;
1075658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1076658b6edaSMichael Mueller 		switch (attr->attr) {
1077658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1078658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
107915c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
108015c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
10810a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1082658b6edaSMichael Mueller 			ret = 0;
1083658b6edaSMichael Mueller 			break;
10840a763c78SDavid Hildenbrand 		/* configuring subfunctions is not supported yet */
10850a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1086658b6edaSMichael Mueller 		default:
1087658b6edaSMichael Mueller 			ret = -ENXIO;
1088658b6edaSMichael Mueller 			break;
1089658b6edaSMichael Mueller 		}
1090658b6edaSMichael Mueller 		break;
1091a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1092a374e892STony Krowiak 		switch (attr->attr) {
1093a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1094a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1095a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1096a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1097a374e892STony Krowiak 			ret = 0;
1098a374e892STony Krowiak 			break;
1099a374e892STony Krowiak 		default:
1100a374e892STony Krowiak 			ret = -ENXIO;
1101a374e892STony Krowiak 			break;
1102a374e892STony Krowiak 		}
1103a374e892STony Krowiak 		break;
1104f2061656SDominik Dingel 	default:
1105f2061656SDominik Dingel 		ret = -ENXIO;
1106f2061656SDominik Dingel 		break;
1107f2061656SDominik Dingel 	}
1108f2061656SDominik Dingel 
1109f2061656SDominik Dingel 	return ret;
1110f2061656SDominik Dingel }
1111f2061656SDominik Dingel 
111230ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
111330ee2a98SJason J. Herne {
111430ee2a98SJason J. Herne 	uint8_t *keys;
111530ee2a98SJason J. Herne 	uint64_t hva;
111630ee2a98SJason J. Herne 	int i, r = 0;
111730ee2a98SJason J. Herne 
111830ee2a98SJason J. Herne 	if (args->flags != 0)
111930ee2a98SJason J. Herne 		return -EINVAL;
112030ee2a98SJason J. Herne 
112130ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
112230ee2a98SJason J. Herne 	if (!mm_use_skey(current->mm))
112330ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
112430ee2a98SJason J. Herne 
112530ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
112630ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
112730ee2a98SJason J. Herne 		return -EINVAL;
112830ee2a98SJason J. Herne 
112930ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
113030ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
113130ee2a98SJason J. Herne 	if (!keys)
113230ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
113330ee2a98SJason J. Herne 	if (!keys)
113430ee2a98SJason J. Herne 		return -ENOMEM;
113530ee2a98SJason J. Herne 
1136d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
113730ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
113830ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
113930ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
114030ee2a98SJason J. Herne 			r = -EFAULT;
1141d3ed1ceeSMartin Schwidefsky 			break;
114230ee2a98SJason J. Herne 		}
114330ee2a98SJason J. Herne 
1144154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1145154c8c19SDavid Hildenbrand 		if (r)
1146d3ed1ceeSMartin Schwidefsky 			break;
114730ee2a98SJason J. Herne 	}
1148d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
114930ee2a98SJason J. Herne 
1150d3ed1ceeSMartin Schwidefsky 	if (!r) {
115130ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
115230ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
115330ee2a98SJason J. Herne 		if (r)
115430ee2a98SJason J. Herne 			r = -EFAULT;
1155d3ed1ceeSMartin Schwidefsky 	}
1156d3ed1ceeSMartin Schwidefsky 
115730ee2a98SJason J. Herne 	kvfree(keys);
115830ee2a98SJason J. Herne 	return r;
115930ee2a98SJason J. Herne }
116030ee2a98SJason J. Herne 
116130ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
116230ee2a98SJason J. Herne {
116330ee2a98SJason J. Herne 	uint8_t *keys;
116430ee2a98SJason J. Herne 	uint64_t hva;
116530ee2a98SJason J. Herne 	int i, r = 0;
116630ee2a98SJason J. Herne 
116730ee2a98SJason J. Herne 	if (args->flags != 0)
116830ee2a98SJason J. Herne 		return -EINVAL;
116930ee2a98SJason J. Herne 
117030ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
117130ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
117230ee2a98SJason J. Herne 		return -EINVAL;
117330ee2a98SJason J. Herne 
117430ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
117530ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
117630ee2a98SJason J. Herne 	if (!keys)
117730ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
117830ee2a98SJason J. Herne 	if (!keys)
117930ee2a98SJason J. Herne 		return -ENOMEM;
118030ee2a98SJason J. Herne 
118130ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
118230ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
118330ee2a98SJason J. Herne 	if (r) {
118430ee2a98SJason J. Herne 		r = -EFAULT;
118530ee2a98SJason J. Herne 		goto out;
118630ee2a98SJason J. Herne 	}
118730ee2a98SJason J. Herne 
118830ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
118914d4a425SDominik Dingel 	r = s390_enable_skey();
119014d4a425SDominik Dingel 	if (r)
119114d4a425SDominik Dingel 		goto out;
119230ee2a98SJason J. Herne 
1193d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
119430ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
119530ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
119630ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
119730ee2a98SJason J. Herne 			r = -EFAULT;
1198d3ed1ceeSMartin Schwidefsky 			break;
119930ee2a98SJason J. Herne 		}
120030ee2a98SJason J. Herne 
120130ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
120230ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
120330ee2a98SJason J. Herne 			r = -EINVAL;
1204d3ed1ceeSMartin Schwidefsky 			break;
120530ee2a98SJason J. Herne 		}
120630ee2a98SJason J. Herne 
1207fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
120830ee2a98SJason J. Herne 		if (r)
1209d3ed1ceeSMartin Schwidefsky 			break;
121030ee2a98SJason J. Herne 	}
1211d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
121230ee2a98SJason J. Herne out:
121330ee2a98SJason J. Herne 	kvfree(keys);
121430ee2a98SJason J. Herne 	return r;
121530ee2a98SJason J. Herne }
121630ee2a98SJason J. Herne 
1217b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
1218b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
1219b0c632dbSHeiko Carstens {
1220b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
1221b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
1222f2061656SDominik Dingel 	struct kvm_device_attr attr;
1223b0c632dbSHeiko Carstens 	int r;
1224b0c632dbSHeiko Carstens 
1225b0c632dbSHeiko Carstens 	switch (ioctl) {
1226ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
1227ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
1228ba5c1e9bSCarsten Otte 
1229ba5c1e9bSCarsten Otte 		r = -EFAULT;
1230ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
1231ba5c1e9bSCarsten Otte 			break;
1232ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
1233ba5c1e9bSCarsten Otte 		break;
1234ba5c1e9bSCarsten Otte 	}
1235d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
1236d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
1237d938dc55SCornelia Huck 		r = -EFAULT;
1238d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1239d938dc55SCornelia Huck 			break;
1240d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1241d938dc55SCornelia Huck 		break;
1242d938dc55SCornelia Huck 	}
124384223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
124484223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
124584223598SCornelia Huck 
124684223598SCornelia Huck 		r = -EINVAL;
124784223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
124884223598SCornelia Huck 			/* Set up dummy routing. */
124984223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
1250152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
125184223598SCornelia Huck 		}
125284223598SCornelia Huck 		break;
125384223598SCornelia Huck 	}
1254f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
1255f2061656SDominik Dingel 		r = -EFAULT;
1256f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1257f2061656SDominik Dingel 			break;
1258f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
1259f2061656SDominik Dingel 		break;
1260f2061656SDominik Dingel 	}
1261f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
1262f2061656SDominik Dingel 		r = -EFAULT;
1263f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1264f2061656SDominik Dingel 			break;
1265f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
1266f2061656SDominik Dingel 		break;
1267f2061656SDominik Dingel 	}
1268f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
1269f2061656SDominik Dingel 		r = -EFAULT;
1270f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1271f2061656SDominik Dingel 			break;
1272f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
1273f2061656SDominik Dingel 		break;
1274f2061656SDominik Dingel 	}
127530ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
127630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
127730ee2a98SJason J. Herne 
127830ee2a98SJason J. Herne 		r = -EFAULT;
127930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
128030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
128130ee2a98SJason J. Herne 			break;
128230ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
128330ee2a98SJason J. Herne 		break;
128430ee2a98SJason J. Herne 	}
128530ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
128630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
128730ee2a98SJason J. Herne 
128830ee2a98SJason J. Herne 		r = -EFAULT;
128930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
129030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
129130ee2a98SJason J. Herne 			break;
129230ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
129330ee2a98SJason J. Herne 		break;
129430ee2a98SJason J. Herne 	}
1295b0c632dbSHeiko Carstens 	default:
1296367e1319SAvi Kivity 		r = -ENOTTY;
1297b0c632dbSHeiko Carstens 	}
1298b0c632dbSHeiko Carstens 
1299b0c632dbSHeiko Carstens 	return r;
1300b0c632dbSHeiko Carstens }
1301b0c632dbSHeiko Carstens 
130245c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
130345c9b47cSTony Krowiak {
130445c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
130586044c8cSChristian Borntraeger 	u32 cc = 0;
130645c9b47cSTony Krowiak 
130786044c8cSChristian Borntraeger 	memset(config, 0, 128);
130845c9b47cSTony Krowiak 	asm volatile(
130945c9b47cSTony Krowiak 		"lgr 0,%1\n"
131045c9b47cSTony Krowiak 		"lgr 2,%2\n"
131145c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
131286044c8cSChristian Borntraeger 		"0: ipm %0\n"
131345c9b47cSTony Krowiak 		"srl %0,28\n"
131486044c8cSChristian Borntraeger 		"1:\n"
131586044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
131686044c8cSChristian Borntraeger 		: "+r" (cc)
131745c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
131845c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
131945c9b47cSTony Krowiak 	);
132045c9b47cSTony Krowiak 
132145c9b47cSTony Krowiak 	return cc;
132245c9b47cSTony Krowiak }
132345c9b47cSTony Krowiak 
132445c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
132545c9b47cSTony Krowiak {
132645c9b47cSTony Krowiak 	u8 config[128];
132745c9b47cSTony Krowiak 	int cc;
132845c9b47cSTony Krowiak 
1329a6aacc3fSHeiko Carstens 	if (test_facility(12)) {
133045c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
133145c9b47cSTony Krowiak 
133245c9b47cSTony Krowiak 		if (cc)
133345c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
133445c9b47cSTony Krowiak 		else
133545c9b47cSTony Krowiak 			return config[0] & 0x40;
133645c9b47cSTony Krowiak 	}
133745c9b47cSTony Krowiak 
133845c9b47cSTony Krowiak 	return 0;
133945c9b47cSTony Krowiak }
134045c9b47cSTony Krowiak 
134145c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
134245c9b47cSTony Krowiak {
134345c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
134445c9b47cSTony Krowiak 
134545c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
134645c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
134745c9b47cSTony Krowiak 	else
134845c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
134945c9b47cSTony Krowiak }
135045c9b47cSTony Krowiak 
13519bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
13529d8d5786SMichael Mueller {
13539bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
13549bb0ec09SDavid Hildenbrand 
13559bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
13569bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
13579bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
13589d8d5786SMichael Mueller }
13599d8d5786SMichael Mueller 
1360c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
13615102ee87STony Krowiak {
13629d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
1363c54f0d6aSDavid Hildenbrand 		return;
13645102ee87STony Krowiak 
1365c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
136645c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
13675102ee87STony Krowiak 
1368ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1369ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1370ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1371ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1372ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1373ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1374ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
13755102ee87STony Krowiak }
13765102ee87STony Krowiak 
13777d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
13787d43bafcSEugene (jno) Dvurechenski {
13797d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
13805e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
13817d43bafcSEugene (jno) Dvurechenski 	else
13827d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
13837d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
13847d43bafcSEugene (jno) Dvurechenski }
13857d43bafcSEugene (jno) Dvurechenski 
1386e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1387b0c632dbSHeiko Carstens {
138876a6dd72SDavid Hildenbrand 	gfp_t alloc_flags = GFP_KERNEL;
13899d8d5786SMichael Mueller 	int i, rc;
1390b0c632dbSHeiko Carstens 	char debug_name[16];
1391f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1392b0c632dbSHeiko Carstens 
1393e08b9637SCarsten Otte 	rc = -EINVAL;
1394e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1395e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1396e08b9637SCarsten Otte 		goto out_err;
1397e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1398e08b9637SCarsten Otte 		goto out_err;
1399e08b9637SCarsten Otte #else
1400e08b9637SCarsten Otte 	if (type)
1401e08b9637SCarsten Otte 		goto out_err;
1402e08b9637SCarsten Otte #endif
1403e08b9637SCarsten Otte 
1404b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1405b0c632dbSHeiko Carstens 	if (rc)
1406d89f5effSJan Kiszka 		goto out_err;
1407b0c632dbSHeiko Carstens 
1408b290411aSCarsten Otte 	rc = -ENOMEM;
1409b290411aSCarsten Otte 
14107d0a5e62SJanosch Frank 	ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);
14117d0a5e62SJanosch Frank 
14127d43bafcSEugene (jno) Dvurechenski 	kvm->arch.use_esca = 0; /* start with basic SCA */
141376a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
141476a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
14155e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
141676a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
1417b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1418d89f5effSJan Kiszka 		goto out_err;
1419f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
1420c5c2c393SDavid Hildenbrand 	sca_offset += 16;
1421bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
1422c5c2c393SDavid Hildenbrand 		sca_offset = 0;
1423bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
1424bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
1425f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
1426b0c632dbSHeiko Carstens 
1427b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
1428b0c632dbSHeiko Carstens 
14291cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
1430b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
143140f5b735SDominik Dingel 		goto out_err;
1432b0c632dbSHeiko Carstens 
1433c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
1434c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1435c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
143640f5b735SDominik Dingel 		goto out_err;
14379d8d5786SMichael Mueller 
1438fb5bf93fSMichael Mueller 	/* Populate the facility mask initially. */
1439c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
144094422ee8SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
14419d8d5786SMichael Mueller 	for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
14429d8d5786SMichael Mueller 		if (i < kvm_s390_fac_list_mask_size())
1443c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
14449d8d5786SMichael Mueller 		else
1445c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] = 0UL;
14469d8d5786SMichael Mueller 	}
14479d8d5786SMichael Mueller 
1448981467c9SMichael Mueller 	/* Populate the facility list initially. */
1449c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1450c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
1451981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1452981467c9SMichael Mueller 
145395ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
145495ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
145595ca2cb5SJanosch Frank 
14569bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
145737c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
14589d8d5786SMichael Mueller 
1459c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
14605102ee87STony Krowiak 
1461ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
14626d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
14636d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
14648a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
1465a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
1466ba5c1e9bSCarsten Otte 
1467b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
146878f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
1469b0c632dbSHeiko Carstens 
1470e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
1471e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
1472a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
1473e08b9637SCarsten Otte 	} else {
147432e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
1475a3a92c31SDominik Dingel 			kvm->arch.mem_limit = TASK_MAX_SIZE;
147632e6b236SGuenther Hutzl 		else
147732e6b236SGuenther Hutzl 			kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE,
147832e6b236SGuenther Hutzl 						    sclp.hamax + 1);
14796ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
1480598841caSCarsten Otte 		if (!kvm->arch.gmap)
148140f5b735SDominik Dingel 			goto out_err;
14822c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
148324eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
1484e08b9637SCarsten Otte 	}
1485fa6b7fe9SCornelia Huck 
1486fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
148784223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
148872f25020SJason J. Herne 	kvm->arch.epoch = 0;
1489fa6b7fe9SCornelia Huck 
14908ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
1491a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
14928335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
14938ad35755SDavid Hildenbrand 
1494d89f5effSJan Kiszka 	return 0;
1495d89f5effSJan Kiszka out_err:
1496c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
149740f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
14987d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
149978f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
1500d89f5effSJan Kiszka 	return rc;
1501b0c632dbSHeiko Carstens }
1502b0c632dbSHeiko Carstens 
1503235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void)
1504235539b4SLuiz Capitulino {
1505235539b4SLuiz Capitulino 	return false;
1506235539b4SLuiz Capitulino }
1507235539b4SLuiz Capitulino 
1508235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
1509235539b4SLuiz Capitulino {
1510235539b4SLuiz Capitulino 	return 0;
1511235539b4SLuiz Capitulino }
1512235539b4SLuiz Capitulino 
1513d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1514d329c035SChristian Borntraeger {
1515d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
1516ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
151767335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
15183c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
1519bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
1520a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
152127e0393fSCarsten Otte 
152227e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
15236ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
152427e0393fSCarsten Otte 
1525e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
1526b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
1527d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
1528b31288faSKonstantin Weitz 
15296692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
1530b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
1531d329c035SChristian Borntraeger }
1532d329c035SChristian Borntraeger 
1533d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
1534d329c035SChristian Borntraeger {
1535d329c035SChristian Borntraeger 	unsigned int i;
1536988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
1537d329c035SChristian Borntraeger 
1538988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
1539988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
1540988a2caeSGleb Natapov 
1541988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
1542988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1543d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
1544988a2caeSGleb Natapov 
1545988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
1546988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
1547d329c035SChristian Borntraeger }
1548d329c035SChristian Borntraeger 
1549b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
1550b0c632dbSHeiko Carstens {
1551d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
15527d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
1553d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
1554c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
155527e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
15566ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
1557841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
155867335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
1559a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
15608335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
1561b0c632dbSHeiko Carstens }
1562b0c632dbSHeiko Carstens 
1563b0c632dbSHeiko Carstens /* Section: vcpu related */
1564dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1565b0c632dbSHeiko Carstens {
15666ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
156727e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
156827e0393fSCarsten Otte 		return -ENOMEM;
15692c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
1570dafd032aSDominik Dingel 
157127e0393fSCarsten Otte 	return 0;
157227e0393fSCarsten Otte }
157327e0393fSCarsten Otte 
1574a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1575a6e2f683SEugene (jno) Dvurechenski {
1576a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
1577a6940674SDavid Hildenbrand 		return;
15785e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
15797d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
15807d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
15817d43bafcSEugene (jno) Dvurechenski 
15827d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
15837d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
15847d43bafcSEugene (jno) Dvurechenski 	} else {
1585bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1586a6e2f683SEugene (jno) Dvurechenski 
1587a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1588a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
1589a6e2f683SEugene (jno) Dvurechenski 	}
15905e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
15917d43bafcSEugene (jno) Dvurechenski }
1592a6e2f683SEugene (jno) Dvurechenski 
1593eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
1594a6e2f683SEugene (jno) Dvurechenski {
1595a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
1596a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1597a6940674SDavid Hildenbrand 
1598a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
1599a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1600a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1601a6940674SDavid Hildenbrand 	}
1602eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
1603eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
1604eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
16057d43bafcSEugene (jno) Dvurechenski 
1606eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
16077d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
16087d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
160925508824SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= 0x04U;
1610eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
16117d43bafcSEugene (jno) Dvurechenski 	} else {
1612eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1613a6e2f683SEugene (jno) Dvurechenski 
1614eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
1615a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1616a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1617eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1618a6e2f683SEugene (jno) Dvurechenski 	}
1619eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
16205e044315SEugene (jno) Dvurechenski }
16215e044315SEugene (jno) Dvurechenski 
16225e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
16235e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
16245e044315SEugene (jno) Dvurechenski {
16255e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
16265e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
16275e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
16285e044315SEugene (jno) Dvurechenski }
16295e044315SEugene (jno) Dvurechenski 
16305e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
16315e044315SEugene (jno) Dvurechenski {
16325e044315SEugene (jno) Dvurechenski 	int i;
16335e044315SEugene (jno) Dvurechenski 
16345e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
16355e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
16365e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
16375e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
16385e044315SEugene (jno) Dvurechenski }
16395e044315SEugene (jno) Dvurechenski 
16405e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
16415e044315SEugene (jno) Dvurechenski {
16425e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
16435e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
16445e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
16455e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
16465e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
16475e044315SEugene (jno) Dvurechenski 
16485e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
16495e044315SEugene (jno) Dvurechenski 	if (!new_sca)
16505e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
16515e044315SEugene (jno) Dvurechenski 
16525e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
16535e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
16545e044315SEugene (jno) Dvurechenski 
16555e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
16565e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
16575e044315SEugene (jno) Dvurechenski 
16585e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
16595e044315SEugene (jno) Dvurechenski 
16605e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
16615e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
16625e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
16635e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->ecb2 |= 0x04U;
16645e044315SEugene (jno) Dvurechenski 	}
16655e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
16665e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
16675e044315SEugene (jno) Dvurechenski 
16685e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
16695e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
16705e044315SEugene (jno) Dvurechenski 
16715e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
16725e044315SEugene (jno) Dvurechenski 
16738335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
16748335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
16755e044315SEugene (jno) Dvurechenski 	return 0;
16767d43bafcSEugene (jno) Dvurechenski }
1677a6e2f683SEugene (jno) Dvurechenski 
1678a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1679a6e2f683SEugene (jno) Dvurechenski {
16805e044315SEugene (jno) Dvurechenski 	int rc;
16815e044315SEugene (jno) Dvurechenski 
1682a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
1683a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
1684a6940674SDavid Hildenbrand 			return true;
1685a6940674SDavid Hildenbrand 		return false;
1686a6940674SDavid Hildenbrand 	}
16875e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
16885e044315SEugene (jno) Dvurechenski 		return true;
168976a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
16905e044315SEugene (jno) Dvurechenski 		return false;
16915e044315SEugene (jno) Dvurechenski 
16925e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
16935e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
16945e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
16955e044315SEugene (jno) Dvurechenski 
16965e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
1697a6e2f683SEugene (jno) Dvurechenski }
1698a6e2f683SEugene (jno) Dvurechenski 
1699dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1700dafd032aSDominik Dingel {
1701dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1702dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
170359674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
170459674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
17059eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
1706b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
1707b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
1708b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
170975a4615cSJulius Niedworok 	kvm_s390_set_prefix(vcpu, 0);
1710c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
1711c6e5f166SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
1712f6aa6dc4SDavid Hildenbrand 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
1713f6aa6dc4SDavid Hildenbrand 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
1714f6aa6dc4SDavid Hildenbrand 	 */
1715f6aa6dc4SDavid Hildenbrand 	if (MACHINE_HAS_VX)
171668c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
17176fd8e67dSDavid Hildenbrand 	else
17186fd8e67dSDavid Hildenbrand 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
1719dafd032aSDominik Dingel 
1720dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
1721dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
1722dafd032aSDominik Dingel 
1723b0c632dbSHeiko Carstens 	return 0;
1724b0c632dbSHeiko Carstens }
1725b0c632dbSHeiko Carstens 
1726db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1727db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1728db0758b2SDavid Hildenbrand {
1729db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
17309c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1731db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
17329c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1733db0758b2SDavid Hildenbrand }
1734db0758b2SDavid Hildenbrand 
1735db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1736db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1737db0758b2SDavid Hildenbrand {
1738db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
17399c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1740db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1741db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
17429c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1743db0758b2SDavid Hildenbrand }
1744db0758b2SDavid Hildenbrand 
1745db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1746db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1747db0758b2SDavid Hildenbrand {
1748db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
1749db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
1750db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
1751db0758b2SDavid Hildenbrand }
1752db0758b2SDavid Hildenbrand 
1753db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1754db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1755db0758b2SDavid Hildenbrand {
1756db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
1757db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
1758db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
1759db0758b2SDavid Hildenbrand }
1760db0758b2SDavid Hildenbrand 
1761db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1762db0758b2SDavid Hildenbrand {
1763db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1764db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
1765db0758b2SDavid Hildenbrand 	preempt_enable();
1766db0758b2SDavid Hildenbrand }
1767db0758b2SDavid Hildenbrand 
1768db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1769db0758b2SDavid Hildenbrand {
1770db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1771db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
1772db0758b2SDavid Hildenbrand 	preempt_enable();
1773db0758b2SDavid Hildenbrand }
1774db0758b2SDavid Hildenbrand 
17754287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
17764287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
17774287f247SDavid Hildenbrand {
1778db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
17799c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1780db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
1781db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
17824287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
17839c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1784db0758b2SDavid Hildenbrand 	preempt_enable();
17854287f247SDavid Hildenbrand }
17864287f247SDavid Hildenbrand 
1787db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
17884287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
17894287f247SDavid Hildenbrand {
17909c23a131SDavid Hildenbrand 	unsigned int seq;
1791db0758b2SDavid Hildenbrand 	__u64 value;
1792db0758b2SDavid Hildenbrand 
1793db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
17944287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
1795db0758b2SDavid Hildenbrand 
17969c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
17979c23a131SDavid Hildenbrand 	do {
17989c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
17999c23a131SDavid Hildenbrand 		/*
18009c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
18019c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
18029c23a131SDavid Hildenbrand 		 */
18039c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
1804db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
18059c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
18069c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
1807db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
18089c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
18099c23a131SDavid Hildenbrand 	preempt_enable();
1810db0758b2SDavid Hildenbrand 	return value;
18114287f247SDavid Hildenbrand }
18124287f247SDavid Hildenbrand 
1813b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1814b0c632dbSHeiko Carstens {
18159977e886SHendrik Brueckner 
181637d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
1817805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
18185ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
1819db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
182001a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
1821b0c632dbSHeiko Carstens }
1822b0c632dbSHeiko Carstens 
1823b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1824b0c632dbSHeiko Carstens {
182501a745acSDavid Hildenbrand 	vcpu->cpu = -1;
18265ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
1827db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
1828805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
182937d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
183037d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
18319977e886SHendrik Brueckner 
1832b0c632dbSHeiko Carstens }
1833b0c632dbSHeiko Carstens 
1834b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1835b0c632dbSHeiko Carstens {
1836b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
1837b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
1838b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
18398d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
18404287f247SDavid Hildenbrand 	kvm_s390_set_cpu_timer(vcpu, 0);
1841b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
1842b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
1843b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1844b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
1845b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
18469abc2a08SDavid Hildenbrand 	/* make sure the new fpc will be lazily loaded */
18479abc2a08SDavid Hildenbrand 	save_fpu_regs();
18489abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = 0;
1849b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
1850672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
18513c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
18523c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
18536352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
18546852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
18552ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
1856b0c632dbSHeiko Carstens }
1857b0c632dbSHeiko Carstens 
185831928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
185942897d86SMarcelo Tosatti {
186072f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
1861fdf03650SFan Zhang 	preempt_disable();
186272f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1863fdf03650SFan Zhang 	preempt_enable();
186472f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
186525508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
1866dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
1867eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
186825508824SDavid Hildenbrand 	}
18696502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
18706502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
187137d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
187237d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
187342897d86SMarcelo Tosatti }
187442897d86SMarcelo Tosatti 
18755102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
18765102ee87STony Krowiak {
18779d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
18785102ee87STony Krowiak 		return;
18795102ee87STony Krowiak 
1880a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1881a374e892STony Krowiak 
1882a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
1883a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1884a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
1885a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1886a374e892STony Krowiak 
18875102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
18885102ee87STony Krowiak }
18895102ee87STony Krowiak 
1890b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1891b31605c1SDominik Dingel {
1892b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
1893b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
1894b31605c1SDominik Dingel }
1895b31605c1SDominik Dingel 
1896b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1897b31605c1SDominik Dingel {
1898b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1899b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
1900b31605c1SDominik Dingel 		return -ENOMEM;
1901b31605c1SDominik Dingel 
1902b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 |= 0x80;
1903b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 &= ~0x08;
1904b31605c1SDominik Dingel 	return 0;
1905b31605c1SDominik Dingel }
1906b31605c1SDominik Dingel 
190791520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
190891520f1aSMichael Mueller {
190991520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
191091520f1aSMichael Mueller 
191191520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
191280bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
1913c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
191491520f1aSMichael Mueller }
191591520f1aSMichael Mueller 
1916b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1917b0c632dbSHeiko Carstens {
1918b31605c1SDominik Dingel 	int rc = 0;
1919b31288faSKonstantin Weitz 
19209e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
19219e6dabefSCornelia Huck 						    CPUSTAT_SM |
1922a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
1923a4a4f191SGuenther Hutzl 
192453df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
1925805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
192653df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
1927805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
1928a4a4f191SGuenther Hutzl 
192991520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
193091520f1aSMichael Mueller 
1931bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
1932bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
1933bdab09f3SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= 0x02;
1934bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
1935bd50e8ecSDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= 0x04;
1936f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
19377feb6bb8SMichael Mueller 		vcpu->arch.sie_block->ecb |= 0x10;
19387feb6bb8SMichael Mueller 
1939873b425eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi)
1940d6af0b49SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= 0x08;
194148ee7d3aSDavid Hildenbrand 	vcpu->arch.sie_block->eca = 0x1002000U;
194248ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
194348ee7d3aSDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x80000000U;
194411ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
194511ad65b7SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x40000000U;
194637c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
1947217a4406SHeiko Carstens 		vcpu->arch.sie_block->eca |= 1;
194837c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
1949ea5f4969SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x10000000U;
195018280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
195113211ea7SEric Farman 		vcpu->arch.sie_block->eca |= 0x00020000;
195213211ea7SEric Farman 		vcpu->arch.sie_block->ecd |= 0x20000000;
195313211ea7SEric Farman 	}
1954c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
1955492d8642SThomas Huth 	vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
19565a5e6536SMatthew Rosato 
1957e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
1958b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
1959b31605c1SDominik Dingel 		if (rc)
1960b31605c1SDominik Dingel 			return rc;
1961b31288faSKonstantin Weitz 	}
19620ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1963ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
19649d8d5786SMichael Mueller 
19655102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
19665102ee87STony Krowiak 
1967b31605c1SDominik Dingel 	return rc;
1968b0c632dbSHeiko Carstens }
1969b0c632dbSHeiko Carstens 
1970b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
1971b0c632dbSHeiko Carstens 				      unsigned int id)
1972b0c632dbSHeiko Carstens {
19734d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
19747feb6bb8SMichael Mueller 	struct sie_page *sie_page;
19754d47555aSCarsten Otte 	int rc = -EINVAL;
1976b0c632dbSHeiko Carstens 
19774215825eSDavid Hildenbrand 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
19784d47555aSCarsten Otte 		goto out;
19794d47555aSCarsten Otte 
19804d47555aSCarsten Otte 	rc = -ENOMEM;
19814d47555aSCarsten Otte 
1982b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1983b0c632dbSHeiko Carstens 	if (!vcpu)
19844d47555aSCarsten Otte 		goto out;
1985b0c632dbSHeiko Carstens 
19867feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
19877feb6bb8SMichael Mueller 	if (!sie_page)
1988b0c632dbSHeiko Carstens 		goto out_free_cpu;
1989b0c632dbSHeiko Carstens 
19907feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
19917feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
19927feb6bb8SMichael Mueller 
1993efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
1994efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
1995efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
1996efed1104SDavid Hildenbrand 
1997b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
1998ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
1999ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
2000d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
20015288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
20029c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
2003ba5c1e9bSCarsten Otte 
2004b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
2005b0c632dbSHeiko Carstens 	if (rc)
20069abc2a08SDavid Hildenbrand 		goto out_free_sie_block;
20078335713aSChristian Borntraeger 	VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2008b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
2009ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2010b0c632dbSHeiko Carstens 
2011b0c632dbSHeiko Carstens 	return vcpu;
20127b06bf2fSWei Yongjun out_free_sie_block:
20137b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
2014b0c632dbSHeiko Carstens out_free_cpu:
2015b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
20164d47555aSCarsten Otte out:
2017b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
2018b0c632dbSHeiko Carstens }
2019b0c632dbSHeiko Carstens 
2020b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2021b0c632dbSHeiko Carstens {
20229a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
2023b0c632dbSHeiko Carstens }
2024b0c632dbSHeiko Carstens 
202527406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
202649b99e1eSChristian Borntraeger {
2027805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
202861a6df54SDavid Hildenbrand 	exit_sie(vcpu);
202949b99e1eSChristian Borntraeger }
203049b99e1eSChristian Borntraeger 
203127406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
203249b99e1eSChristian Borntraeger {
2033805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
203449b99e1eSChristian Borntraeger }
203549b99e1eSChristian Borntraeger 
20368e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
20378e236546SChristian Borntraeger {
2038805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
203961a6df54SDavid Hildenbrand 	exit_sie(vcpu);
20408e236546SChristian Borntraeger }
20418e236546SChristian Borntraeger 
20428e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
20438e236546SChristian Borntraeger {
20449bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
20458e236546SChristian Borntraeger }
20468e236546SChristian Borntraeger 
204749b99e1eSChristian Borntraeger /*
204849b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
204949b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
205049b99e1eSChristian Borntraeger  * return immediately. */
205149b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
205249b99e1eSChristian Borntraeger {
2053805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
205449b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
205549b99e1eSChristian Borntraeger 		cpu_relax();
205649b99e1eSChristian Borntraeger }
205749b99e1eSChristian Borntraeger 
20588e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
20598e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
206049b99e1eSChristian Borntraeger {
20618e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
20628e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
206349b99e1eSChristian Borntraeger }
206449b99e1eSChristian Borntraeger 
2065414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
2066414d3b07SMartin Schwidefsky 			      unsigned long end)
20672c70fe44SChristian Borntraeger {
20682c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
20692c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
2070414d3b07SMartin Schwidefsky 	unsigned long prefix;
2071414d3b07SMartin Schwidefsky 	int i;
20722c70fe44SChristian Borntraeger 
207365d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
207465d0b0d4SDavid Hildenbrand 		return;
2075414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
2076414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
2077414d3b07SMartin Schwidefsky 		return;
20782c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
20792c70fe44SChristian Borntraeger 		/* match against both prefix pages */
2080414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
2081414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
2082414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
2083414d3b07SMartin Schwidefsky 				   start, end);
20848e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
20852c70fe44SChristian Borntraeger 		}
20862c70fe44SChristian Borntraeger 	}
20872c70fe44SChristian Borntraeger }
20882c70fe44SChristian Borntraeger 
2089b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
2090b6d33834SChristoffer Dall {
2091b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
2092b6d33834SChristoffer Dall 	BUG();
2093b6d33834SChristoffer Dall 	return 0;
2094b6d33834SChristoffer Dall }
2095b6d33834SChristoffer Dall 
209614eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
209714eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
209814eebd91SCarsten Otte {
209914eebd91SCarsten Otte 	int r = -EINVAL;
210014eebd91SCarsten Otte 
210114eebd91SCarsten Otte 	switch (reg->id) {
210229b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
210329b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
210429b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
210529b7c71bSCarsten Otte 		break;
210629b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
210729b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
210829b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
210929b7c71bSCarsten Otte 		break;
211046a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
21114287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
211246a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
211346a6dd1cSJason J. herne 		break;
211446a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
211546a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
211646a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
211746a6dd1cSJason J. herne 		break;
2118536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2119536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
2120536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2121536336c2SDominik Dingel 		break;
2122536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2123536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
2124536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2125536336c2SDominik Dingel 		break;
2126536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2127536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
2128536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2129536336c2SDominik Dingel 		break;
2130672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2131672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
2132672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2133672550fbSChristian Borntraeger 		break;
2134afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2135afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
2136afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2137afa45ff5SChristian Borntraeger 		break;
213814eebd91SCarsten Otte 	default:
213914eebd91SCarsten Otte 		break;
214014eebd91SCarsten Otte 	}
214114eebd91SCarsten Otte 
214214eebd91SCarsten Otte 	return r;
214314eebd91SCarsten Otte }
214414eebd91SCarsten Otte 
214514eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
214614eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
214714eebd91SCarsten Otte {
214814eebd91SCarsten Otte 	int r = -EINVAL;
21494287f247SDavid Hildenbrand 	__u64 val;
215014eebd91SCarsten Otte 
215114eebd91SCarsten Otte 	switch (reg->id) {
215229b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
215329b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
215429b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
215529b7c71bSCarsten Otte 		break;
215629b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
215729b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
215829b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
215929b7c71bSCarsten Otte 		break;
216046a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
21614287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
21624287f247SDavid Hildenbrand 		if (!r)
21634287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
216446a6dd1cSJason J. herne 		break;
216546a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
216646a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
216746a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
216846a6dd1cSJason J. herne 		break;
2169536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2170536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
2171536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
21729fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
21739fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2174536336c2SDominik Dingel 		break;
2175536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2176536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
2177536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2178536336c2SDominik Dingel 		break;
2179536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2180536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
2181536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2182536336c2SDominik Dingel 		break;
2183672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2184672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
2185672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2186672550fbSChristian Borntraeger 		break;
2187afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2188afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
2189afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2190afa45ff5SChristian Borntraeger 		break;
219114eebd91SCarsten Otte 	default:
219214eebd91SCarsten Otte 		break;
219314eebd91SCarsten Otte 	}
219414eebd91SCarsten Otte 
219514eebd91SCarsten Otte 	return r;
219614eebd91SCarsten Otte }
2197b6d33834SChristoffer Dall 
2198b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
2199b0c632dbSHeiko Carstens {
2200b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
2201b0c632dbSHeiko Carstens 	return 0;
2202b0c632dbSHeiko Carstens }
2203b0c632dbSHeiko Carstens 
2204b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2205b0c632dbSHeiko Carstens {
22065a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
2207b0c632dbSHeiko Carstens 	return 0;
2208b0c632dbSHeiko Carstens }
2209b0c632dbSHeiko Carstens 
2210b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2211b0c632dbSHeiko Carstens {
22125a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
2213b0c632dbSHeiko Carstens 	return 0;
2214b0c632dbSHeiko Carstens }
2215b0c632dbSHeiko Carstens 
2216b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2217b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2218b0c632dbSHeiko Carstens {
221959674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
2220b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
2221b0c632dbSHeiko Carstens 	return 0;
2222b0c632dbSHeiko Carstens }
2223b0c632dbSHeiko Carstens 
2224b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2225b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2226b0c632dbSHeiko Carstens {
222759674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
2228b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
2229b0c632dbSHeiko Carstens 	return 0;
2230b0c632dbSHeiko Carstens }
2231b0c632dbSHeiko Carstens 
2232b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2233b0c632dbSHeiko Carstens {
22344725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
22354725c860SMartin Schwidefsky 		return -EINVAL;
2236*e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
22379abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2238a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
2239a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
22409abc2a08SDavid Hildenbrand 	else
2241a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
2242b0c632dbSHeiko Carstens 	return 0;
2243b0c632dbSHeiko Carstens }
2244b0c632dbSHeiko Carstens 
2245b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2246b0c632dbSHeiko Carstens {
22479abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
22489abc2a08SDavid Hildenbrand 	save_fpu_regs();
22499abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2250a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
2251a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
22529abc2a08SDavid Hildenbrand 	else
2253a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
2254*e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
2255b0c632dbSHeiko Carstens 	return 0;
2256b0c632dbSHeiko Carstens }
2257b0c632dbSHeiko Carstens 
2258b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
2259b0c632dbSHeiko Carstens {
2260b0c632dbSHeiko Carstens 	int rc = 0;
2261b0c632dbSHeiko Carstens 
22627a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
2263b0c632dbSHeiko Carstens 		rc = -EBUSY;
2264d7b0b5ebSCarsten Otte 	else {
2265d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
2266d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
2267d7b0b5ebSCarsten Otte 	}
2268b0c632dbSHeiko Carstens 	return rc;
2269b0c632dbSHeiko Carstens }
2270b0c632dbSHeiko Carstens 
2271b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2272b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
2273b0c632dbSHeiko Carstens {
2274b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
2275b0c632dbSHeiko Carstens }
2276b0c632dbSHeiko Carstens 
227727291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
227827291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
227927291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
228027291e21SDavid Hildenbrand 
2281d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2282d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
2283b0c632dbSHeiko Carstens {
228427291e21SDavid Hildenbrand 	int rc = 0;
228527291e21SDavid Hildenbrand 
228627291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
228727291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
228827291e21SDavid Hildenbrand 
22892de3bfc2SDavid Hildenbrand 	if (dbg->control & ~VALID_GUESTDBG_FLAGS)
229027291e21SDavid Hildenbrand 		return -EINVAL;
229189b5b4deSDavid Hildenbrand 	if (!sclp.has_gpere)
229289b5b4deSDavid Hildenbrand 		return -EINVAL;
229327291e21SDavid Hildenbrand 
229427291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
229527291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
229627291e21SDavid Hildenbrand 		/* enforce guest PER */
2297805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
229827291e21SDavid Hildenbrand 
229927291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
230027291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
230127291e21SDavid Hildenbrand 	} else {
2302805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
230327291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
230427291e21SDavid Hildenbrand 	}
230527291e21SDavid Hildenbrand 
230627291e21SDavid Hildenbrand 	if (rc) {
230727291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
230827291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
2309805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
231027291e21SDavid Hildenbrand 	}
231127291e21SDavid Hildenbrand 
231227291e21SDavid Hildenbrand 	return rc;
2313b0c632dbSHeiko Carstens }
2314b0c632dbSHeiko Carstens 
231562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
231662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
231762d9f0dbSMarcelo Tosatti {
23186352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
23196352e4d2SDavid Hildenbrand 	return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
23206352e4d2SDavid Hildenbrand 				       KVM_MP_STATE_OPERATING;
232162d9f0dbSMarcelo Tosatti }
232262d9f0dbSMarcelo Tosatti 
232362d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
232462d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
232562d9f0dbSMarcelo Tosatti {
23266352e4d2SDavid Hildenbrand 	int rc = 0;
23276352e4d2SDavid Hildenbrand 
23286352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
23296352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
23306352e4d2SDavid Hildenbrand 
23316352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
23326352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
23336352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
23346352e4d2SDavid Hildenbrand 		break;
23356352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
23366352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
23376352e4d2SDavid Hildenbrand 		break;
23386352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
23396352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
23406352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
23416352e4d2SDavid Hildenbrand 	default:
23426352e4d2SDavid Hildenbrand 		rc = -ENXIO;
23436352e4d2SDavid Hildenbrand 	}
23446352e4d2SDavid Hildenbrand 
23456352e4d2SDavid Hildenbrand 	return rc;
234662d9f0dbSMarcelo Tosatti }
234762d9f0dbSMarcelo Tosatti 
23488ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
23498ad35755SDavid Hildenbrand {
23508ad35755SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
23518ad35755SDavid Hildenbrand }
23528ad35755SDavid Hildenbrand 
23532c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
23542c70fe44SChristian Borntraeger {
23558ad35755SDavid Hildenbrand retry:
23568e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
2357586b7ccdSChristian Borntraeger 	if (!vcpu->requests)
2358586b7ccdSChristian Borntraeger 		return 0;
23592c70fe44SChristian Borntraeger 	/*
23602c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2361b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
23622c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
23632c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
23642c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
23652c70fe44SChristian Borntraeger 	 */
23668ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
23672c70fe44SChristian Borntraeger 		int rc;
2368b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
2369fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
2370b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
2371aca411a4SJulius Niedworok 		if (rc) {
2372aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
23732c70fe44SChristian Borntraeger 			return rc;
2374aca411a4SJulius Niedworok 		}
23758ad35755SDavid Hildenbrand 		goto retry;
23762c70fe44SChristian Borntraeger 	}
23778ad35755SDavid Hildenbrand 
2378d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2379d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
2380d3d692c8SDavid Hildenbrand 		goto retry;
2381d3d692c8SDavid Hildenbrand 	}
2382d3d692c8SDavid Hildenbrand 
23838ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
23848ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
23858ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
2386805de8f4SPeter Zijlstra 			atomic_or(CPUSTAT_IBS,
23878ad35755SDavid Hildenbrand 					&vcpu->arch.sie_block->cpuflags);
23888ad35755SDavid Hildenbrand 		}
23898ad35755SDavid Hildenbrand 		goto retry;
23908ad35755SDavid Hildenbrand 	}
23918ad35755SDavid Hildenbrand 
23928ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
23938ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
23948ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
2395805de8f4SPeter Zijlstra 			atomic_andnot(CPUSTAT_IBS,
23968ad35755SDavid Hildenbrand 					  &vcpu->arch.sie_block->cpuflags);
23978ad35755SDavid Hildenbrand 		}
23988ad35755SDavid Hildenbrand 		goto retry;
23998ad35755SDavid Hildenbrand 	}
24008ad35755SDavid Hildenbrand 
24016502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
24026502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
24036502a34cSDavid Hildenbrand 		goto retry;
24046502a34cSDavid Hildenbrand 	}
24056502a34cSDavid Hildenbrand 
24060759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
24070759d068SDavid Hildenbrand 	clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
24080759d068SDavid Hildenbrand 
24092c70fe44SChristian Borntraeger 	return 0;
24102c70fe44SChristian Borntraeger }
24112c70fe44SChristian Borntraeger 
241225ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
241325ed1675SDavid Hildenbrand {
241425ed1675SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
241525ed1675SDavid Hildenbrand 	int i;
241625ed1675SDavid Hildenbrand 
241725ed1675SDavid Hildenbrand 	mutex_lock(&kvm->lock);
241825ed1675SDavid Hildenbrand 	preempt_disable();
241925ed1675SDavid Hildenbrand 	kvm->arch.epoch = tod - get_tod_clock();
242025ed1675SDavid Hildenbrand 	kvm_s390_vcpu_block_all(kvm);
242125ed1675SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm)
242225ed1675SDavid Hildenbrand 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
242325ed1675SDavid Hildenbrand 	kvm_s390_vcpu_unblock_all(kvm);
242425ed1675SDavid Hildenbrand 	preempt_enable();
242525ed1675SDavid Hildenbrand 	mutex_unlock(&kvm->lock);
242625ed1675SDavid Hildenbrand }
242725ed1675SDavid Hildenbrand 
2428fa576c58SThomas Huth /**
2429fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
2430fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
2431fa576c58SThomas Huth  * @gpa: Guest physical address
2432fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
2433fa576c58SThomas Huth  *
2434fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
2435fa576c58SThomas Huth  *
2436fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
2437fa576c58SThomas Huth  */
2438fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
243924eb3a82SDominik Dingel {
2440527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
2441527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
244224eb3a82SDominik Dingel }
244324eb3a82SDominik Dingel 
24443c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
24453c038e6bSDominik Dingel 				      unsigned long token)
24463c038e6bSDominik Dingel {
24473c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
2448383d0b05SJens Freimann 	struct kvm_s390_irq irq;
24493c038e6bSDominik Dingel 
24503c038e6bSDominik Dingel 	if (start_token) {
2451383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
2452383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
2453383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
24543c038e6bSDominik Dingel 	} else {
24553c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
2456383d0b05SJens Freimann 		inti.parm64 = token;
24573c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
24583c038e6bSDominik Dingel 	}
24593c038e6bSDominik Dingel }
24603c038e6bSDominik Dingel 
24613c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
24623c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
24633c038e6bSDominik Dingel {
24643c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
24653c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
24663c038e6bSDominik Dingel }
24673c038e6bSDominik Dingel 
24683c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
24693c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
24703c038e6bSDominik Dingel {
24713c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
24723c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
24733c038e6bSDominik Dingel }
24743c038e6bSDominik Dingel 
24753c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
24763c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
24773c038e6bSDominik Dingel {
24783c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
24793c038e6bSDominik Dingel }
24803c038e6bSDominik Dingel 
24813c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
24823c038e6bSDominik Dingel {
24833c038e6bSDominik Dingel 	/*
24843c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
24853c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
24863c038e6bSDominik Dingel 	 */
24873c038e6bSDominik Dingel 	return true;
24883c038e6bSDominik Dingel }
24893c038e6bSDominik Dingel 
24903c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
24913c038e6bSDominik Dingel {
24923c038e6bSDominik Dingel 	hva_t hva;
24933c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
24943c038e6bSDominik Dingel 	int rc;
24953c038e6bSDominik Dingel 
24963c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
24973c038e6bSDominik Dingel 		return 0;
24983c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
24993c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
25003c038e6bSDominik Dingel 		return 0;
25013c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
25023c038e6bSDominik Dingel 		return 0;
25039a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
25043c038e6bSDominik Dingel 		return 0;
25053c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
25063c038e6bSDominik Dingel 		return 0;
25073c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
25083c038e6bSDominik Dingel 		return 0;
25093c038e6bSDominik Dingel 
251081480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
251181480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
251281480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
25133c038e6bSDominik Dingel 		return 0;
25143c038e6bSDominik Dingel 
25153c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
25163c038e6bSDominik Dingel 	return rc;
25173c038e6bSDominik Dingel }
25183c038e6bSDominik Dingel 
25193fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
2520b0c632dbSHeiko Carstens {
25213fb4c40fSThomas Huth 	int rc, cpuflags;
2522e168bf8dSCarsten Otte 
25233c038e6bSDominik Dingel 	/*
25243c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
25253c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
25263c038e6bSDominik Dingel 	 * handled outside the worker.
25273c038e6bSDominik Dingel 	 */
25283c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
25293c038e6bSDominik Dingel 
25307ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
25317ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
2532b0c632dbSHeiko Carstens 
2533b0c632dbSHeiko Carstens 	if (need_resched())
2534b0c632dbSHeiko Carstens 		schedule();
2535b0c632dbSHeiko Carstens 
2536d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
253771cde587SChristian Borntraeger 		s390_handle_mcck();
253871cde587SChristian Borntraeger 
253979395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
254079395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
254179395031SJens Freimann 		if (rc)
254279395031SJens Freimann 			return rc;
254379395031SJens Freimann 	}
25440ff31867SCarsten Otte 
25452c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
25462c70fe44SChristian Borntraeger 	if (rc)
25472c70fe44SChristian Borntraeger 		return rc;
25482c70fe44SChristian Borntraeger 
254927291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
255027291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
255127291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
255227291e21SDavid Hildenbrand 	}
255327291e21SDavid Hildenbrand 
2554b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
25553fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
25563fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
25573fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
25582b29a9fdSDominik Dingel 
25593fb4c40fSThomas Huth 	return 0;
25603fb4c40fSThomas Huth }
25613fb4c40fSThomas Huth 
2562492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2563492d8642SThomas Huth {
256456317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
256556317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
256656317920SDavid Hildenbrand 	};
256756317920SDavid Hildenbrand 	u8 opcode, ilen;
2568492d8642SThomas Huth 	int rc;
2569492d8642SThomas Huth 
2570492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2571492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
2572492d8642SThomas Huth 
2573492d8642SThomas Huth 	/*
2574492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
2575492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
2576492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
2577492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
2578492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
2579492d8642SThomas Huth 	 * to be able to forward the PSW.
2580492d8642SThomas Huth 	 */
258165977322SDavid Hildenbrand 	rc = read_guest_instr(vcpu, &opcode, 1);
258256317920SDavid Hildenbrand 	ilen = insn_length(opcode);
25839b0d721aSDavid Hildenbrand 	if (rc < 0) {
25849b0d721aSDavid Hildenbrand 		return rc;
25859b0d721aSDavid Hildenbrand 	} else if (rc) {
25869b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
25879b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
25889b0d721aSDavid Hildenbrand 		 * nullification if necessary.
25899b0d721aSDavid Hildenbrand 		 */
25909b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
25919b0d721aSDavid Hildenbrand 		ilen = 4;
25929b0d721aSDavid Hildenbrand 	}
259356317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
259456317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
259556317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
2596492d8642SThomas Huth }
2597492d8642SThomas Huth 
25983fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
25993fb4c40fSThomas Huth {
26002b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
26012b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
26022b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
26032b29a9fdSDominik Dingel 
260427291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
260527291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
260627291e21SDavid Hildenbrand 
26077ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
26087ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
260971f116bfSDavid Hildenbrand 
261071f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
261171f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
261271f116bfSDavid Hildenbrand 
261371f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
261471f116bfSDavid Hildenbrand 			return rc;
261571f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
261671f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
261771f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
261871f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
261971f116bfSDavid Hildenbrand 		return -EREMOTE;
262071f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
262171f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
262271f116bfSDavid Hildenbrand 		return 0;
2623210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
2624210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2625210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
2626210b1607SThomas Huth 						current->thread.gmap_addr;
2627210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
262871f116bfSDavid Hildenbrand 		return -EREMOTE;
262924eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
26303c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
263124eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
263271f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
263371f116bfSDavid Hildenbrand 			return 0;
263471f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
2635fa576c58SThomas Huth 	}
263671f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
26373fb4c40fSThomas Huth }
26383fb4c40fSThomas Huth 
26393fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
26403fb4c40fSThomas Huth {
26413fb4c40fSThomas Huth 	int rc, exit_reason;
26423fb4c40fSThomas Huth 
2643800c1065SThomas Huth 	/*
2644800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2645800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
2646800c1065SThomas Huth 	 */
2647800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2648800c1065SThomas Huth 
2649a76ccff6SThomas Huth 	do {
26503fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
26513fb4c40fSThomas Huth 		if (rc)
2652a76ccff6SThomas Huth 			break;
26533fb4c40fSThomas Huth 
2654800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
26553fb4c40fSThomas Huth 		/*
2656a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
2657a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
26583fb4c40fSThomas Huth 		 */
26590097d12eSChristian Borntraeger 		local_irq_disable();
26606edaa530SPaolo Bonzini 		guest_enter_irqoff();
2661db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
26620097d12eSChristian Borntraeger 		local_irq_enable();
2663a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
2664a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
26650097d12eSChristian Borntraeger 		local_irq_disable();
2666db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
26676edaa530SPaolo Bonzini 		guest_exit_irqoff();
26680097d12eSChristian Borntraeger 		local_irq_enable();
2669800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
26703fb4c40fSThomas Huth 
26713fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
267227291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
26733fb4c40fSThomas Huth 
2674800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2675e168bf8dSCarsten Otte 	return rc;
2676b0c632dbSHeiko Carstens }
2677b0c632dbSHeiko Carstens 
2678b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2679b028ee3eSDavid Hildenbrand {
2680b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2681b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2682b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2683b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2684b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2685b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
2686d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
2687d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2688b028ee3eSDavid Hildenbrand 	}
2689b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
26904287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
2691b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2692b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2693b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2694b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2695b028ee3eSDavid Hildenbrand 	}
2696b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2697b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2698b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2699b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
27009fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
27019fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2702b028ee3eSDavid Hildenbrand 	}
270380cd8763SFan Zhang 	/*
270480cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
270580cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
270680cd8763SFan Zhang 	 */
270780cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
270880cd8763SFan Zhang 	    test_kvm_facility(vcpu->kvm, 64)) {
270980cd8763SFan Zhang 		struct runtime_instr_cb *riccb =
271080cd8763SFan Zhang 			(struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
271180cd8763SFan Zhang 
271280cd8763SFan Zhang 		if (riccb->valid)
271380cd8763SFan Zhang 			vcpu->arch.sie_block->ecb3 |= 0x01;
271480cd8763SFan Zhang 	}
271531d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->arch.host_acrs);
271631d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
2717*e1788bb9SChristian Borntraeger 	/* save host (userspace) fprs/vrs */
2718*e1788bb9SChristian Borntraeger 	save_fpu_regs();
2719*e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
2720*e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
2721*e1788bb9SChristian Borntraeger 	if (MACHINE_HAS_VX)
2722*e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
2723*e1788bb9SChristian Borntraeger 	else
2724*e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
2725*e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
2726*e1788bb9SChristian Borntraeger 	if (test_fp_ctl(current->thread.fpu.fpc))
2727*e1788bb9SChristian Borntraeger 		/* User space provided an invalid FPC, let's clear it */
2728*e1788bb9SChristian Borntraeger 		current->thread.fpu.fpc = 0;
272980cd8763SFan Zhang 
2730b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
2731b028ee3eSDavid Hildenbrand }
2732b028ee3eSDavid Hildenbrand 
2733b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2734b028ee3eSDavid Hildenbrand {
2735b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2736b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2737b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2738b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
27394287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
2740b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2741b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2742b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2743b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2744b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2745b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2746b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
274731d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
274831d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->arch.host_acrs);
2749*e1788bb9SChristian Borntraeger 	/* Save guest register state */
2750*e1788bb9SChristian Borntraeger 	save_fpu_regs();
2751*e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
2752*e1788bb9SChristian Borntraeger 	/* Restore will be done lazily at return */
2753*e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
2754*e1788bb9SChristian Borntraeger 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
2755*e1788bb9SChristian Borntraeger 
2756b028ee3eSDavid Hildenbrand }
2757b028ee3eSDavid Hildenbrand 
2758b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2759b0c632dbSHeiko Carstens {
27608f2abe6aSChristian Borntraeger 	int rc;
2761b0c632dbSHeiko Carstens 	sigset_t sigsaved;
2762b0c632dbSHeiko Carstens 
276327291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
276427291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
276527291e21SDavid Hildenbrand 		return 0;
276627291e21SDavid Hildenbrand 	}
276727291e21SDavid Hildenbrand 
2768b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2769b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2770b0c632dbSHeiko Carstens 
27716352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
27726852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
27736352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
2774ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
27756352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
27766352e4d2SDavid Hildenbrand 		return -EINVAL;
27776352e4d2SDavid Hildenbrand 	}
2778b0c632dbSHeiko Carstens 
2779b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
2780db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
2781d7b0b5ebSCarsten Otte 
2782dab4079dSHeiko Carstens 	might_fault();
2783e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
27849ace903dSChristian Ehrhardt 
2785b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
2786b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
27878f2abe6aSChristian Borntraeger 		rc = -EINTR;
2788b1d16c49SChristian Ehrhardt 	}
27898f2abe6aSChristian Borntraeger 
279027291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
279127291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
279227291e21SDavid Hildenbrand 		rc = 0;
279327291e21SDavid Hildenbrand 	}
279427291e21SDavid Hildenbrand 
27958f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
279671f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
27978f2abe6aSChristian Borntraeger 		rc = 0;
27988f2abe6aSChristian Borntraeger 	}
27998f2abe6aSChristian Borntraeger 
2800db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
2801b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
2802d7b0b5ebSCarsten Otte 
2803b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2804b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2805b0c632dbSHeiko Carstens 
2806b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
28077e8e6ab4SHeiko Carstens 	return rc;
2808b0c632dbSHeiko Carstens }
2809b0c632dbSHeiko Carstens 
2810b0c632dbSHeiko Carstens /*
2811b0c632dbSHeiko Carstens  * store status at address
2812b0c632dbSHeiko Carstens  * we use have two special cases:
2813b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2814b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2815b0c632dbSHeiko Carstens  */
2816d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
2817b0c632dbSHeiko Carstens {
2818092670cdSCarsten Otte 	unsigned char archmode = 1;
28199abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
2820fda902cbSMichael Mueller 	unsigned int px;
28214287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
2822d0bce605SHeiko Carstens 	int rc;
2823b0c632dbSHeiko Carstens 
2824d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
2825d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2826d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
2827b0c632dbSHeiko Carstens 			return -EFAULT;
2828d9a3a09aSMartin Schwidefsky 		gpa = 0;
2829d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2830d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
2831b0c632dbSHeiko Carstens 			return -EFAULT;
2832d9a3a09aSMartin Schwidefsky 		gpa = px;
2833d9a3a09aSMartin Schwidefsky 	} else
2834d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
28359abc2a08SDavid Hildenbrand 
28369abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
28379abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
28389522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
2839d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
28409abc2a08SDavid Hildenbrand 				     fprs, 128);
28419abc2a08SDavid Hildenbrand 	} else {
28429abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
28436fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
28449abc2a08SDavid Hildenbrand 	}
2845d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
2846d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
2847d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
2848d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
2849d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
2850fda902cbSMichael Mueller 			      &px, 4);
2851d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
28529abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
2853d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
2854d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
28554287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
2856d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
28574287f247SDavid Hildenbrand 			      &cputm, 8);
2858178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
2859d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
2860d0bce605SHeiko Carstens 			      &clkcomp, 8);
2861d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
2862d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
2863d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
2864d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
2865d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
2866b0c632dbSHeiko Carstens }
2867b0c632dbSHeiko Carstens 
2868e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2869e879892cSThomas Huth {
2870e879892cSThomas Huth 	/*
2871e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
287231d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
2873e879892cSThomas Huth 	 * it into the save area
2874e879892cSThomas Huth 	 */
2875d0164ee2SHendrik Brueckner 	save_fpu_regs();
28769abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
2877e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
2878e879892cSThomas Huth 
2879e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
2880e879892cSThomas Huth }
2881e879892cSThomas Huth 
28828ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
28838ad35755SDavid Hildenbrand {
28848ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
28858e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
28868ad35755SDavid Hildenbrand }
28878ad35755SDavid Hildenbrand 
28888ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
28898ad35755SDavid Hildenbrand {
28908ad35755SDavid Hildenbrand 	unsigned int i;
28918ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
28928ad35755SDavid Hildenbrand 
28938ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
28948ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
28958ad35755SDavid Hildenbrand 	}
28968ad35755SDavid Hildenbrand }
28978ad35755SDavid Hildenbrand 
28988ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
28998ad35755SDavid Hildenbrand {
290009a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
290109a400e7SDavid Hildenbrand 		return;
29028ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
29038e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
29048ad35755SDavid Hildenbrand }
29058ad35755SDavid Hildenbrand 
29066852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
29076852d7b6SDavid Hildenbrand {
29088ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
29098ad35755SDavid Hildenbrand 
29108ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
29118ad35755SDavid Hildenbrand 		return;
29128ad35755SDavid Hildenbrand 
29136852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
29148ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
2915433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
29168ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
29178ad35755SDavid Hildenbrand 
29188ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
29198ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
29208ad35755SDavid Hildenbrand 			started_vcpus++;
29218ad35755SDavid Hildenbrand 	}
29228ad35755SDavid Hildenbrand 
29238ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
29248ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
29258ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
29268ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
29278ad35755SDavid Hildenbrand 		/*
29288ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
29298ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
29308ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
29318ad35755SDavid Hildenbrand 		 */
29328ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
29338ad35755SDavid Hildenbrand 	}
29348ad35755SDavid Hildenbrand 
2935805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
29368ad35755SDavid Hildenbrand 	/*
29378ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
29388ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
29398ad35755SDavid Hildenbrand 	 */
2940d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2941433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
29428ad35755SDavid Hildenbrand 	return;
29436852d7b6SDavid Hildenbrand }
29446852d7b6SDavid Hildenbrand 
29456852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
29466852d7b6SDavid Hildenbrand {
29478ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
29488ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
29498ad35755SDavid Hildenbrand 
29508ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
29518ad35755SDavid Hildenbrand 		return;
29528ad35755SDavid Hildenbrand 
29536852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
29548ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
2955433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
29568ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
29578ad35755SDavid Hildenbrand 
295832f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
29596cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
296032f5ff63SDavid Hildenbrand 
2961805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
29628ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
29638ad35755SDavid Hildenbrand 
29648ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
29658ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
29668ad35755SDavid Hildenbrand 			started_vcpus++;
29678ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
29688ad35755SDavid Hildenbrand 		}
29698ad35755SDavid Hildenbrand 	}
29708ad35755SDavid Hildenbrand 
29718ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
29728ad35755SDavid Hildenbrand 		/*
29738ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
29748ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
29758ad35755SDavid Hildenbrand 		 */
29768ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
29778ad35755SDavid Hildenbrand 	}
29788ad35755SDavid Hildenbrand 
2979433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
29808ad35755SDavid Hildenbrand 	return;
29816852d7b6SDavid Hildenbrand }
29826852d7b6SDavid Hildenbrand 
2983d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
2984d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
2985d6712df9SCornelia Huck {
2986d6712df9SCornelia Huck 	int r;
2987d6712df9SCornelia Huck 
2988d6712df9SCornelia Huck 	if (cap->flags)
2989d6712df9SCornelia Huck 		return -EINVAL;
2990d6712df9SCornelia Huck 
2991d6712df9SCornelia Huck 	switch (cap->cap) {
2992fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
2993fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
2994fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
2995c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
2996fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
2997fa6b7fe9SCornelia Huck 		}
2998fa6b7fe9SCornelia Huck 		r = 0;
2999fa6b7fe9SCornelia Huck 		break;
3000d6712df9SCornelia Huck 	default:
3001d6712df9SCornelia Huck 		r = -EINVAL;
3002d6712df9SCornelia Huck 		break;
3003d6712df9SCornelia Huck 	}
3004d6712df9SCornelia Huck 	return r;
3005d6712df9SCornelia Huck }
3006d6712df9SCornelia Huck 
300741408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
300841408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
300941408c28SThomas Huth {
301041408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
301141408c28SThomas Huth 	void *tmpbuf = NULL;
301241408c28SThomas Huth 	int r, srcu_idx;
301341408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
301441408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
301541408c28SThomas Huth 
301641408c28SThomas Huth 	if (mop->flags & ~supported_flags)
301741408c28SThomas Huth 		return -EINVAL;
301841408c28SThomas Huth 
301941408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
302041408c28SThomas Huth 		return -E2BIG;
302141408c28SThomas Huth 
302241408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
302341408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
302441408c28SThomas Huth 		if (!tmpbuf)
302541408c28SThomas Huth 			return -ENOMEM;
302641408c28SThomas Huth 	}
302741408c28SThomas Huth 
302841408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
302941408c28SThomas Huth 
303041408c28SThomas Huth 	switch (mop->op) {
303141408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
303241408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
303392c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
303492c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
303541408c28SThomas Huth 			break;
303641408c28SThomas Huth 		}
303741408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
303841408c28SThomas Huth 		if (r == 0) {
303941408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
304041408c28SThomas Huth 				r = -EFAULT;
304141408c28SThomas Huth 		}
304241408c28SThomas Huth 		break;
304341408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
304441408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
304592c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
304692c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
304741408c28SThomas Huth 			break;
304841408c28SThomas Huth 		}
304941408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
305041408c28SThomas Huth 			r = -EFAULT;
305141408c28SThomas Huth 			break;
305241408c28SThomas Huth 		}
305341408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
305441408c28SThomas Huth 		break;
305541408c28SThomas Huth 	default:
305641408c28SThomas Huth 		r = -EINVAL;
305741408c28SThomas Huth 	}
305841408c28SThomas Huth 
305941408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
306041408c28SThomas Huth 
306141408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
306241408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
306341408c28SThomas Huth 
306441408c28SThomas Huth 	vfree(tmpbuf);
306541408c28SThomas Huth 	return r;
306641408c28SThomas Huth }
306741408c28SThomas Huth 
3068b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
3069b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
3070b0c632dbSHeiko Carstens {
3071b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
3072b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
3073800c1065SThomas Huth 	int idx;
3074bc923cc9SAvi Kivity 	long r;
3075b0c632dbSHeiko Carstens 
307693736624SAvi Kivity 	switch (ioctl) {
307747b43c52SJens Freimann 	case KVM_S390_IRQ: {
307847b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
307947b43c52SJens Freimann 
308047b43c52SJens Freimann 		r = -EFAULT;
308147b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
308247b43c52SJens Freimann 			break;
308347b43c52SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
308447b43c52SJens Freimann 		break;
308547b43c52SJens Freimann 	}
308693736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
3087ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
3088383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
3089ba5c1e9bSCarsten Otte 
309093736624SAvi Kivity 		r = -EFAULT;
3091ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
309293736624SAvi Kivity 			break;
3093383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
3094383d0b05SJens Freimann 			return -EINVAL;
3095383d0b05SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
309693736624SAvi Kivity 		break;
3097ba5c1e9bSCarsten Otte 	}
3098b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
3099800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
3100bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
3101800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
3102bc923cc9SAvi Kivity 		break;
3103b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
3104b0c632dbSHeiko Carstens 		psw_t psw;
3105b0c632dbSHeiko Carstens 
3106bc923cc9SAvi Kivity 		r = -EFAULT;
3107b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
3108bc923cc9SAvi Kivity 			break;
3109bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
3110bc923cc9SAvi Kivity 		break;
3111b0c632dbSHeiko Carstens 	}
3112b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
3113bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3114bc923cc9SAvi Kivity 		break;
311514eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
311614eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
311714eebd91SCarsten Otte 		struct kvm_one_reg reg;
311814eebd91SCarsten Otte 		r = -EFAULT;
311914eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
312014eebd91SCarsten Otte 			break;
312114eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
312214eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
312314eebd91SCarsten Otte 		else
312414eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
312514eebd91SCarsten Otte 		break;
312614eebd91SCarsten Otte 	}
312727e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
312827e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
312927e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
313027e0393fSCarsten Otte 
313127e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
313227e0393fSCarsten Otte 			r = -EFAULT;
313327e0393fSCarsten Otte 			break;
313427e0393fSCarsten Otte 		}
313527e0393fSCarsten Otte 
313627e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
313727e0393fSCarsten Otte 			r = -EINVAL;
313827e0393fSCarsten Otte 			break;
313927e0393fSCarsten Otte 		}
314027e0393fSCarsten Otte 
314127e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
314227e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
314327e0393fSCarsten Otte 		break;
314427e0393fSCarsten Otte 	}
314527e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
314627e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
314727e0393fSCarsten Otte 
314827e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
314927e0393fSCarsten Otte 			r = -EFAULT;
315027e0393fSCarsten Otte 			break;
315127e0393fSCarsten Otte 		}
315227e0393fSCarsten Otte 
315327e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
315427e0393fSCarsten Otte 			r = -EINVAL;
315527e0393fSCarsten Otte 			break;
315627e0393fSCarsten Otte 		}
315727e0393fSCarsten Otte 
315827e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
315927e0393fSCarsten Otte 			ucasmap.length);
316027e0393fSCarsten Otte 		break;
316127e0393fSCarsten Otte 	}
316227e0393fSCarsten Otte #endif
3163ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
3164527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
3165ccc7910fSCarsten Otte 		break;
3166ccc7910fSCarsten Otte 	}
3167d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
3168d6712df9SCornelia Huck 	{
3169d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
3170d6712df9SCornelia Huck 		r = -EFAULT;
3171d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
3172d6712df9SCornelia Huck 			break;
3173d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3174d6712df9SCornelia Huck 		break;
3175d6712df9SCornelia Huck 	}
317641408c28SThomas Huth 	case KVM_S390_MEM_OP: {
317741408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
317841408c28SThomas Huth 
317941408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
318041408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
318141408c28SThomas Huth 		else
318241408c28SThomas Huth 			r = -EFAULT;
318341408c28SThomas Huth 		break;
318441408c28SThomas Huth 	}
3185816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
3186816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3187816c7667SJens Freimann 
3188816c7667SJens Freimann 		r = -EFAULT;
3189816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3190816c7667SJens Freimann 			break;
3191816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
3192816c7667SJens Freimann 		    irq_state.len == 0 ||
3193816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
3194816c7667SJens Freimann 			r = -EINVAL;
3195816c7667SJens Freimann 			break;
3196816c7667SJens Freimann 		}
3197816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
3198816c7667SJens Freimann 					   (void __user *) irq_state.buf,
3199816c7667SJens Freimann 					   irq_state.len);
3200816c7667SJens Freimann 		break;
3201816c7667SJens Freimann 	}
3202816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
3203816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3204816c7667SJens Freimann 
3205816c7667SJens Freimann 		r = -EFAULT;
3206816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3207816c7667SJens Freimann 			break;
3208816c7667SJens Freimann 		if (irq_state.len == 0) {
3209816c7667SJens Freimann 			r = -EINVAL;
3210816c7667SJens Freimann 			break;
3211816c7667SJens Freimann 		}
3212816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
3213816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
3214816c7667SJens Freimann 					   irq_state.len);
3215816c7667SJens Freimann 		break;
3216816c7667SJens Freimann 	}
3217b0c632dbSHeiko Carstens 	default:
32183e6afcf1SCarsten Otte 		r = -ENOTTY;
3219b0c632dbSHeiko Carstens 	}
3220bc923cc9SAvi Kivity 	return r;
3221b0c632dbSHeiko Carstens }
3222b0c632dbSHeiko Carstens 
32235b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
32245b1c1493SCarsten Otte {
32255b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
32265b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
32275b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
32285b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
32295b1c1493SCarsten Otte 		get_page(vmf->page);
32305b1c1493SCarsten Otte 		return 0;
32315b1c1493SCarsten Otte 	}
32325b1c1493SCarsten Otte #endif
32335b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
32345b1c1493SCarsten Otte }
32355b1c1493SCarsten Otte 
32365587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
32375587027cSAneesh Kumar K.V 			    unsigned long npages)
3238db3fe4ebSTakuya Yoshikawa {
3239db3fe4ebSTakuya Yoshikawa 	return 0;
3240db3fe4ebSTakuya Yoshikawa }
3241db3fe4ebSTakuya Yoshikawa 
3242b0c632dbSHeiko Carstens /* Section: memory related */
3243f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
3244f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
324509170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
32467b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
3247b0c632dbSHeiko Carstens {
3248dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
3249dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
3250dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
3251dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
3252b0c632dbSHeiko Carstens 
3253598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
3254b0c632dbSHeiko Carstens 		return -EINVAL;
3255b0c632dbSHeiko Carstens 
3256598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
3257b0c632dbSHeiko Carstens 		return -EINVAL;
3258b0c632dbSHeiko Carstens 
3259a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
3260a3a92c31SDominik Dingel 		return -EINVAL;
3261a3a92c31SDominik Dingel 
3262f7784b8eSMarcelo Tosatti 	return 0;
3263f7784b8eSMarcelo Tosatti }
3264f7784b8eSMarcelo Tosatti 
3265f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
326609170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
32678482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
3268f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
32698482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
3270f7784b8eSMarcelo Tosatti {
3271f7850c92SCarsten Otte 	int rc;
3272f7784b8eSMarcelo Tosatti 
32732cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
32742cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
32752cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
32762cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
32772cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
32782cef4debSChristian Borntraeger 	 */
32792cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
32802cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
32812cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
32822cef4debSChristian Borntraeger 		return;
3283598841caSCarsten Otte 
3284598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
3285598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
3286598841caSCarsten Otte 	if (rc)
3287ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
3288598841caSCarsten Otte 	return;
3289b0c632dbSHeiko Carstens }
3290b0c632dbSHeiko Carstens 
329160a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
329260a37709SAlexander Yarygin {
329360a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
329460a37709SAlexander Yarygin 
329560a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
329660a37709SAlexander Yarygin }
329760a37709SAlexander Yarygin 
32983491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
32993491caf2SChristian Borntraeger {
33003491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
33013491caf2SChristian Borntraeger }
33023491caf2SChristian Borntraeger 
3303b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
3304b0c632dbSHeiko Carstens {
330560a37709SAlexander Yarygin 	int i;
330660a37709SAlexander Yarygin 
330707197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
330807197fd0SDavid Hildenbrand 		pr_info("SIE not available\n");
330907197fd0SDavid Hildenbrand 		return -ENODEV;
331007197fd0SDavid Hildenbrand 	}
331107197fd0SDavid Hildenbrand 
331260a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
331360a37709SAlexander Yarygin 		kvm_s390_fac_list_mask[i] |=
331460a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
331560a37709SAlexander Yarygin 
33169d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
3317b0c632dbSHeiko Carstens }
3318b0c632dbSHeiko Carstens 
3319b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
3320b0c632dbSHeiko Carstens {
3321b0c632dbSHeiko Carstens 	kvm_exit();
3322b0c632dbSHeiko Carstens }
3323b0c632dbSHeiko Carstens 
3324b0c632dbSHeiko Carstens module_init(kvm_s390_init);
3325b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
3326566af940SCornelia Huck 
3327566af940SCornelia Huck /*
3328566af940SCornelia Huck  * Enable autoloading of the kvm module.
3329566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3330566af940SCornelia Huck  * since x86 takes a different approach.
3331566af940SCornelia Huck  */
3332566af940SCornelia Huck #include <linux/miscdevice.h>
3333566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
3334566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
3335