xref: /linux/arch/s390/kvm/kvm-s390.c (revision accb757d798c9b4d85cfe3e5972134c586525168)
1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b0c632dbSHeiko Carstens /*
3bb64da9aSChristian Borntraeger  * hosting IBM Z kernel virtual machines (s390x)
4b0c632dbSHeiko Carstens  *
5bb64da9aSChristian Borntraeger  * Copyright IBM Corp. 2008, 2017
6b0c632dbSHeiko Carstens  *
7b0c632dbSHeiko Carstens  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8b0c632dbSHeiko Carstens  *               Christian Borntraeger <borntraeger@de.ibm.com>
9b0c632dbSHeiko Carstens  *               Heiko Carstens <heiko.carstens@de.ibm.com>
10628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
1115f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
12b0c632dbSHeiko Carstens  */
13b0c632dbSHeiko Carstens 
14b0c632dbSHeiko Carstens #include <linux/compiler.h>
15b0c632dbSHeiko Carstens #include <linux/err.h>
16b0c632dbSHeiko Carstens #include <linux/fs.h>
17ca872302SChristian Borntraeger #include <linux/hrtimer.h>
18b0c632dbSHeiko Carstens #include <linux/init.h>
19b0c632dbSHeiko Carstens #include <linux/kvm.h>
20b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
21b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
22b0c632dbSHeiko Carstens #include <linux/module.h>
23d3217967SPaul Gortmaker #include <linux/moduleparam.h>
24a374e892STony Krowiak #include <linux/random.h>
25b0c632dbSHeiko Carstens #include <linux/slab.h>
26ba5c1e9bSCarsten Otte #include <linux/timer.h>
2741408c28SThomas Huth #include <linux/vmalloc.h>
2815c9705fSDavid Hildenbrand #include <linux/bitmap.h>
29174cd4b1SIngo Molnar #include <linux/sched/signal.h>
30190df4a2SClaudio Imbrenda #include <linux/string.h>
31174cd4b1SIngo Molnar 
32cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
33b0c632dbSHeiko Carstens #include <asm/lowcore.h>
34fd5ada04SMartin Schwidefsky #include <asm/stp.h>
35b0c632dbSHeiko Carstens #include <asm/pgtable.h>
361e133ab2SMartin Schwidefsky #include <asm/gmap.h>
37f5daba1dSHeiko Carstens #include <asm/nmi.h>
38a0616cdeSDavid Howells #include <asm/switch_to.h>
396d3da241SJens Freimann #include <asm/isc.h>
401526bf9cSChristian Borntraeger #include <asm/sclp.h>
410a763c78SDavid Hildenbrand #include <asm/cpacf.h>
42221bb8a4SLinus Torvalds #include <asm/timex.h>
438f2abe6aSChristian Borntraeger #include "kvm-s390.h"
44b0c632dbSHeiko Carstens #include "gaccess.h"
45b0c632dbSHeiko Carstens 
46ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
47ea2cdd27SDavid Hildenbrand #undef pr_fmt
48ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
49ea2cdd27SDavid Hildenbrand 
505786fffaSCornelia Huck #define CREATE_TRACE_POINTS
515786fffaSCornelia Huck #include "trace.h"
52ade38c31SCornelia Huck #include "trace-s390.h"
535786fffaSCornelia Huck 
5441408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
55816c7667SJens Freimann #define LOCAL_IRQS 32
56816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
57816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
5841408c28SThomas Huth 
59b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
60b0c632dbSHeiko Carstens 
61b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
62b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
630eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
648f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
658f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
668f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
678f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
68ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
699ec6de19SAlexander Yarygin 	{ "exit_pei", VCPU_STAT(exit_pei) },
70ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
71ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
72a011eeb2SJanosch Frank 	{ "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
73f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
7462bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
753491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
76ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
77f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
78ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
79aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
80aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
81ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
827697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
83ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
84ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
85ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
86ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
87ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
88ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
89ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
9069d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
91453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
92453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
93453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
94453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
95453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
968a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
97453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
98453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
99b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
100453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
101453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
102bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
10395ca2cb5SJanosch Frank 	{ "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
104a3508fbeSDavid Hildenbrand 	{ "instruction_sie", VCPU_STAT(instruction_sie) },
1055288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
106bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
1077697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
1085288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
10942cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
11042cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1115288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
11242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
11342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
114cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1155288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1165288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1175288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
11842cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
11942cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
12042cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
121388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
122e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
12341628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
124175a5c9eSChristian Borntraeger 	{ "diagnose_258", VCPU_STAT(diagnose_258) },
125175a5c9eSChristian Borntraeger 	{ "diagnose_308", VCPU_STAT(diagnose_308) },
126175a5c9eSChristian Borntraeger 	{ "diagnose_500", VCPU_STAT(diagnose_500) },
127b0c632dbSHeiko Carstens 	{ NULL }
128b0c632dbSHeiko Carstens };
129b0c632dbSHeiko Carstens 
1308fa1696eSCollin L. Walling struct kvm_s390_tod_clock_ext {
1318fa1696eSCollin L. Walling 	__u8 epoch_idx;
1328fa1696eSCollin L. Walling 	__u64 tod;
1338fa1696eSCollin L. Walling 	__u8 reserved[7];
1348fa1696eSCollin L. Walling } __packed;
1358fa1696eSCollin L. Walling 
136a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
137a411edf1SDavid Hildenbrand static int nested;
138a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
139a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
140a411edf1SDavid Hildenbrand 
1419d8d5786SMichael Mueller /* upper facilities limit for kvm */
142f6c1d359SHeiko Carstens unsigned long kvm_s390_fac_list_mask[16] = { FACILITIES_KVM };
143b0c632dbSHeiko Carstens 
1449d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void)
14578c4b59fSMichael Mueller {
1469d8d5786SMichael Mueller 	BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
1479d8d5786SMichael Mueller 	return ARRAY_SIZE(kvm_s390_fac_list_mask);
14878c4b59fSMichael Mueller }
14978c4b59fSMichael Mueller 
15015c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
15115c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1520a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
1530a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
15415c9705fSDavid Hildenbrand 
1559d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
156a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
15778f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
1589d8d5786SMichael Mueller 
159b0c632dbSHeiko Carstens /* Section: not file related */
16013a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
161b0c632dbSHeiko Carstens {
162b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
16310474ae8SAlexander Graf 	return 0;
164b0c632dbSHeiko Carstens }
165b0c632dbSHeiko Carstens 
166414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
167414d3b07SMartin Schwidefsky 			      unsigned long end);
1682c70fe44SChristian Borntraeger 
169fdf03650SFan Zhang /*
170fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
171fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
172fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
173fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
174fdf03650SFan Zhang  */
175fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
176fdf03650SFan Zhang 			  void *v)
177fdf03650SFan Zhang {
178fdf03650SFan Zhang 	struct kvm *kvm;
179fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
180fdf03650SFan Zhang 	int i;
181fdf03650SFan Zhang 	unsigned long long *delta = v;
182fdf03650SFan Zhang 
183fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
184fdf03650SFan Zhang 		kvm->arch.epoch -= *delta;
185fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
186fdf03650SFan Zhang 			vcpu->arch.sie_block->epoch -= *delta;
187db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
188db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
18991473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
19091473b48SDavid Hildenbrand 				vcpu->arch.vsie_block->epoch -= *delta;
191fdf03650SFan Zhang 		}
192fdf03650SFan Zhang 	}
193fdf03650SFan Zhang 	return NOTIFY_OK;
194fdf03650SFan Zhang }
195fdf03650SFan Zhang 
196fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
197fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
198fdf03650SFan Zhang };
199fdf03650SFan Zhang 
200b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
201b0c632dbSHeiko Carstens {
2022c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
203b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
204a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
205a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
206fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
207fdf03650SFan Zhang 				       &kvm_clock_notifier);
208b0c632dbSHeiko Carstens 	return 0;
209b0c632dbSHeiko Carstens }
210b0c632dbSHeiko Carstens 
211b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
212b0c632dbSHeiko Carstens {
213b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
214a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
215fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
216fdf03650SFan Zhang 					 &kvm_clock_notifier);
217b0c632dbSHeiko Carstens }
218b0c632dbSHeiko Carstens 
21922be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
22022be5a13SDavid Hildenbrand {
22122be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
22222be5a13SDavid Hildenbrand }
22322be5a13SDavid Hildenbrand 
2240a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
2250a763c78SDavid Hildenbrand {
2260a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
227d051ae53SHeiko Carstens 	int cc;
2280a763c78SDavid Hildenbrand 
2290a763c78SDavid Hildenbrand 	asm volatile(
2300a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
2310a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
2320a763c78SDavid Hildenbrand 		"	ipm	%0\n"
2330a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
2340a763c78SDavid Hildenbrand 		: "=d" (cc)
2350a763c78SDavid Hildenbrand 		: "d" (r0)
2360a763c78SDavid Hildenbrand 		: "cc");
2370a763c78SDavid Hildenbrand 	return cc == 0;
2380a763c78SDavid Hildenbrand }
2390a763c78SDavid Hildenbrand 
24022be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
24122be5a13SDavid Hildenbrand {
2420a763c78SDavid Hildenbrand 	int i;
2430a763c78SDavid Hildenbrand 
2440a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
2450a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
2460a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
2470a763c78SDavid Hildenbrand 	}
2480a763c78SDavid Hildenbrand 
2490a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
250221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
251221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
252221bb8a4SLinus Torvalds 		     PTFF_QAF);
2530a763c78SDavid Hildenbrand 
2540a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
25569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
25669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
25769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
25869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
25969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
26069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
26169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
26269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
26369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
26469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
2650a763c78SDavid Hildenbrand 	}
2660a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
26769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
26869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
2690a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
27069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
27169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
27269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
27369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
27469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
27569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
27669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
27769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
2780a763c78SDavid Hildenbrand 	}
2790a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
280985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
28169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
2820a763c78SDavid Hildenbrand 
283e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
284e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
285e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
286e000b8e0SJason J. Herne 
28722be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
28822be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
289a3508fbeSDavid Hildenbrand 	/*
290a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
291a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
292a3508fbeSDavid Hildenbrand 	 */
293a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
294a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
295a3508fbeSDavid Hildenbrand 		return;
296a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
29719c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
29819c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
2990615a326SDavid Hildenbrand 	if (sclp.has_siif)
3000615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
30177d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
30277d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
303a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
304a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
3055630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
3065630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
30713ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
30813ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
3097fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
3107fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
311730cd632SFarhan Ali 	if (sclp.has_kss)
312730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
3135d3876a8SDavid Hildenbrand 	/*
3145d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
3155d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
3165d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
3175d3876a8SDavid Hildenbrand 	 *
3185d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
3195d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
3205d3876a8SDavid Hildenbrand 	 *
3215d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
3225d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
3235d3876a8SDavid Hildenbrand 	 *
3245d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
3255d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
3265d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
3275d3876a8SDavid Hildenbrand 	 *
3285d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
3295d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
3305d3876a8SDavid Hildenbrand 	 */
33122be5a13SDavid Hildenbrand }
33222be5a13SDavid Hildenbrand 
333b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
334b0c632dbSHeiko Carstens {
33578f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
33678f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
33778f26131SChristian Borntraeger 		return -ENOMEM;
33878f26131SChristian Borntraeger 
33978f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
34078f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
34178f26131SChristian Borntraeger 		return -ENOMEM;
34278f26131SChristian Borntraeger 	}
34378f26131SChristian Borntraeger 
34422be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
34522be5a13SDavid Hildenbrand 
34684877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
34784877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
348b0c632dbSHeiko Carstens }
349b0c632dbSHeiko Carstens 
35078f26131SChristian Borntraeger void kvm_arch_exit(void)
35178f26131SChristian Borntraeger {
35278f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
35378f26131SChristian Borntraeger }
35478f26131SChristian Borntraeger 
355b0c632dbSHeiko Carstens /* Section: device related */
356b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
357b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
358b0c632dbSHeiko Carstens {
359b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
360b0c632dbSHeiko Carstens 		return s390_enable_sie();
361b0c632dbSHeiko Carstens 	return -EINVAL;
362b0c632dbSHeiko Carstens }
363b0c632dbSHeiko Carstens 
364784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
365b0c632dbSHeiko Carstens {
366d7b0b5ebSCarsten Otte 	int r;
367d7b0b5ebSCarsten Otte 
3682bd0ac4eSCarsten Otte 	switch (ext) {
369d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
370b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
37152e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
3721efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
3731efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
3741efd0f59SCarsten Otte #endif
3753c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
37660b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
37714eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
378d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
379fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
38010ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
381c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
382d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
38378599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
384f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
3856352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
386460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
38747b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
3882444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
389e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
39030ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
391816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
3926502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
3934036e387SClaudio Imbrenda 	case KVM_CAP_S390_CMMA_MIGRATION:
39447a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
395da9a1446SChristian Borntraeger 	case KVM_CAP_S390_AIS_MIGRATION:
396d7b0b5ebSCarsten Otte 		r = 1;
397d7b0b5ebSCarsten Otte 		break;
39841408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
39941408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
40041408c28SThomas Huth 		break;
401e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
402e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
40376a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
404a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
405a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
406a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
40776a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
408e726b1bdSChristian Borntraeger 		break;
409e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
410e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
411e1e2e605SNick Wang 		break;
4121526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
413abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
4141526bf9cSChristian Borntraeger 		break;
41568c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
41668c55750SEric Farman 		r = MACHINE_HAS_VX;
41768c55750SEric Farman 		break;
418c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
419c6e5f166SFan Zhang 		r = test_facility(64);
420c6e5f166SFan Zhang 		break;
4214e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
4224e0b1ab7SFan Zhang 		r = test_facility(133);
4234e0b1ab7SFan Zhang 		break;
4242bd0ac4eSCarsten Otte 	default:
425d7b0b5ebSCarsten Otte 		r = 0;
426b0c632dbSHeiko Carstens 	}
427d7b0b5ebSCarsten Otte 	return r;
4282bd0ac4eSCarsten Otte }
429b0c632dbSHeiko Carstens 
43015f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
43115f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
43215f36ebdSJason J. Herne {
43315f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
43415f36ebdSJason J. Herne 	unsigned long address;
43515f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
43615f36ebdSJason J. Herne 
43715f36ebdSJason J. Herne 	/* Loop over all guest pages */
43815f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
43915f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
44015f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
44115f36ebdSJason J. Herne 
4421e133ab2SMartin Schwidefsky 		if (test_and_clear_guest_dirty(gmap->mm, address))
44315f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
4441763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
4451763f8d0SChristian Borntraeger 			return;
44670c88a00SChristian Borntraeger 		cond_resched();
44715f36ebdSJason J. Herne 	}
44815f36ebdSJason J. Herne }
44915f36ebdSJason J. Herne 
450b0c632dbSHeiko Carstens /* Section: vm related */
451a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
452a6e2f683SEugene (jno) Dvurechenski 
453b0c632dbSHeiko Carstens /*
454b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
455b0c632dbSHeiko Carstens  */
456b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
457b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
458b0c632dbSHeiko Carstens {
45915f36ebdSJason J. Herne 	int r;
46015f36ebdSJason J. Herne 	unsigned long n;
4619f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
46215f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
46315f36ebdSJason J. Herne 	int is_dirty = 0;
46415f36ebdSJason J. Herne 
465e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
466e1e8a962SJanosch Frank 		return -EINVAL;
467e1e8a962SJanosch Frank 
46815f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
46915f36ebdSJason J. Herne 
47015f36ebdSJason J. Herne 	r = -EINVAL;
47115f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
47215f36ebdSJason J. Herne 		goto out;
47315f36ebdSJason J. Herne 
4749f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
4759f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
47615f36ebdSJason J. Herne 	r = -ENOENT;
47715f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
47815f36ebdSJason J. Herne 		goto out;
47915f36ebdSJason J. Herne 
48015f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
48115f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
48215f36ebdSJason J. Herne 	if (r)
48315f36ebdSJason J. Herne 		goto out;
48415f36ebdSJason J. Herne 
48515f36ebdSJason J. Herne 	/* Clear the dirty log */
48615f36ebdSJason J. Herne 	if (is_dirty) {
48715f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
48815f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
48915f36ebdSJason J. Herne 	}
49015f36ebdSJason J. Herne 	r = 0;
49115f36ebdSJason J. Herne out:
49215f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
49315f36ebdSJason J. Herne 	return r;
494b0c632dbSHeiko Carstens }
495b0c632dbSHeiko Carstens 
4966502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
4976502a34cSDavid Hildenbrand {
4986502a34cSDavid Hildenbrand 	unsigned int i;
4996502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
5006502a34cSDavid Hildenbrand 
5016502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
5026502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
5036502a34cSDavid Hildenbrand 	}
5046502a34cSDavid Hildenbrand }
5056502a34cSDavid Hildenbrand 
506d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
507d938dc55SCornelia Huck {
508d938dc55SCornelia Huck 	int r;
509d938dc55SCornelia Huck 
510d938dc55SCornelia Huck 	if (cap->flags)
511d938dc55SCornelia Huck 		return -EINVAL;
512d938dc55SCornelia Huck 
513d938dc55SCornelia Huck 	switch (cap->cap) {
51484223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
515c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
51684223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
51784223598SCornelia Huck 		r = 0;
51884223598SCornelia Huck 		break;
5192444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
520c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
5212444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
5222444b352SDavid Hildenbrand 		r = 0;
5232444b352SDavid Hildenbrand 		break;
52468c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
5255967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
526a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
5275967c17bSDavid Hildenbrand 			r = -EBUSY;
5285967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
529c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
530c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
5312f87d942SGuenther Hutzl 			if (test_facility(134)) {
5322f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
5332f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
5342f87d942SGuenther Hutzl 			}
53553743aa7SMaxim Samoylov 			if (test_facility(135)) {
53653743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
53753743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
53853743aa7SMaxim Samoylov 			}
53918280d8bSMichael Mueller 			r = 0;
54018280d8bSMichael Mueller 		} else
54118280d8bSMichael Mueller 			r = -EINVAL;
5425967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
543c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
544c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
54568c55750SEric Farman 		break;
546c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
547c6e5f166SFan Zhang 		r = -EINVAL;
548c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
549a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
550c6e5f166SFan Zhang 			r = -EBUSY;
551c6e5f166SFan Zhang 		} else if (test_facility(64)) {
552c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
553c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
554c6e5f166SFan Zhang 			r = 0;
555c6e5f166SFan Zhang 		}
556c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
557c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
558c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
559c6e5f166SFan Zhang 		break;
56047a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
56147a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
56247a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
56347a4693eSYi Min Zhao 			r = -EBUSY;
56447a4693eSYi Min Zhao 		} else {
56547a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
56647a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
56747a4693eSYi Min Zhao 			r = 0;
56847a4693eSYi Min Zhao 		}
56947a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
57047a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
57147a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
57247a4693eSYi Min Zhao 		break;
5734e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
5744e0b1ab7SFan Zhang 		r = -EINVAL;
5754e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
5764e0b1ab7SFan Zhang 		if (atomic_read(&kvm->online_vcpus)) {
5774e0b1ab7SFan Zhang 			r = -EBUSY;
5784e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
5794e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
5804e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
5814e0b1ab7SFan Zhang 			r = 0;
5824e0b1ab7SFan Zhang 		}
5834e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
5844e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
5854e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
5864e0b1ab7SFan Zhang 		break;
587e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
588c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
589e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
590e44fc8c9SEkaterina Tumanova 		r = 0;
591e44fc8c9SEkaterina Tumanova 		break;
5926502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5936502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
5946502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
5956502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
5966502a34cSDavid Hildenbrand 		r = 0;
5976502a34cSDavid Hildenbrand 		break;
598d938dc55SCornelia Huck 	default:
599d938dc55SCornelia Huck 		r = -EINVAL;
600d938dc55SCornelia Huck 		break;
601d938dc55SCornelia Huck 	}
602d938dc55SCornelia Huck 	return r;
603d938dc55SCornelia Huck }
604d938dc55SCornelia Huck 
6058c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
6068c0a7ce6SDominik Dingel {
6078c0a7ce6SDominik Dingel 	int ret;
6088c0a7ce6SDominik Dingel 
6098c0a7ce6SDominik Dingel 	switch (attr->attr) {
6108c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
6118c0a7ce6SDominik Dingel 		ret = 0;
612c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
613a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
614a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
6158c0a7ce6SDominik Dingel 			ret = -EFAULT;
6168c0a7ce6SDominik Dingel 		break;
6178c0a7ce6SDominik Dingel 	default:
6188c0a7ce6SDominik Dingel 		ret = -ENXIO;
6198c0a7ce6SDominik Dingel 		break;
6208c0a7ce6SDominik Dingel 	}
6218c0a7ce6SDominik Dingel 	return ret;
6228c0a7ce6SDominik Dingel }
6238c0a7ce6SDominik Dingel 
6248c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
6254f718eabSDominik Dingel {
6264f718eabSDominik Dingel 	int ret;
6274f718eabSDominik Dingel 	unsigned int idx;
6284f718eabSDominik Dingel 	switch (attr->attr) {
6294f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
630f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
631c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
632e6db1d61SDominik Dingel 			break;
633e6db1d61SDominik Dingel 
6344f718eabSDominik Dingel 		ret = -EBUSY;
635c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
6364f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
637a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6384f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
6394f718eabSDominik Dingel 			ret = 0;
6404f718eabSDominik Dingel 		}
6414f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
6424f718eabSDominik Dingel 		break;
6434f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
644f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
645f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
646f9cbd9b0SDavid Hildenbrand 			break;
647c3489155SDominik Dingel 		ret = -EINVAL;
648c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
649c3489155SDominik Dingel 			break;
650c3489155SDominik Dingel 
651c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
6524f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
6534f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
654a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
6554f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
6564f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
6574f718eabSDominik Dingel 		ret = 0;
6584f718eabSDominik Dingel 		break;
6598c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
6608c0a7ce6SDominik Dingel 		unsigned long new_limit;
6618c0a7ce6SDominik Dingel 
6628c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
6638c0a7ce6SDominik Dingel 			return -EINVAL;
6648c0a7ce6SDominik Dingel 
6658c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
6668c0a7ce6SDominik Dingel 			return -EFAULT;
6678c0a7ce6SDominik Dingel 
668a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
669a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
6708c0a7ce6SDominik Dingel 			return -E2BIG;
6718c0a7ce6SDominik Dingel 
672a3a92c31SDominik Dingel 		if (!new_limit)
673a3a92c31SDominik Dingel 			return -EINVAL;
674a3a92c31SDominik Dingel 
6756ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
676a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
677a3a92c31SDominik Dingel 			new_limit -= 1;
678a3a92c31SDominik Dingel 
6798c0a7ce6SDominik Dingel 		ret = -EBUSY;
6808c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
681a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6826ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
6836ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
6848c0a7ce6SDominik Dingel 
6858c0a7ce6SDominik Dingel 			if (!new) {
6868c0a7ce6SDominik Dingel 				ret = -ENOMEM;
6878c0a7ce6SDominik Dingel 			} else {
6886ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
6898c0a7ce6SDominik Dingel 				new->private = kvm;
6908c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
6918c0a7ce6SDominik Dingel 				ret = 0;
6928c0a7ce6SDominik Dingel 			}
6938c0a7ce6SDominik Dingel 		}
6948c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
695a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
696a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
697a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
6988c0a7ce6SDominik Dingel 		break;
6998c0a7ce6SDominik Dingel 	}
7004f718eabSDominik Dingel 	default:
7014f718eabSDominik Dingel 		ret = -ENXIO;
7024f718eabSDominik Dingel 		break;
7034f718eabSDominik Dingel 	}
7044f718eabSDominik Dingel 	return ret;
7054f718eabSDominik Dingel }
7064f718eabSDominik Dingel 
707a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
708a374e892STony Krowiak 
709a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
710a374e892STony Krowiak {
711a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
712a374e892STony Krowiak 	int i;
713a374e892STony Krowiak 
7149d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
715a374e892STony Krowiak 		return -EINVAL;
716a374e892STony Krowiak 
717a374e892STony Krowiak 	mutex_lock(&kvm->lock);
718a374e892STony Krowiak 	switch (attr->attr) {
719a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
720a374e892STony Krowiak 		get_random_bytes(
721a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
722a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
723a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
724c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
725a374e892STony Krowiak 		break;
726a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
727a374e892STony Krowiak 		get_random_bytes(
728a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
729a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
730a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
731c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
732a374e892STony Krowiak 		break;
733a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
734a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
735a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
736a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
737c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
738a374e892STony Krowiak 		break;
739a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
740a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
741a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
742a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
743c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
744a374e892STony Krowiak 		break;
745a374e892STony Krowiak 	default:
746a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
747a374e892STony Krowiak 		return -ENXIO;
748a374e892STony Krowiak 	}
749a374e892STony Krowiak 
750a374e892STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm) {
751a374e892STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
752a374e892STony Krowiak 		exit_sie(vcpu);
753a374e892STony Krowiak 	}
754a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
755a374e892STony Krowiak 	return 0;
756a374e892STony Krowiak }
757a374e892STony Krowiak 
758190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
759190df4a2SClaudio Imbrenda {
760190df4a2SClaudio Imbrenda 	int cx;
761190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
762190df4a2SClaudio Imbrenda 
763190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
764190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
765190df4a2SClaudio Imbrenda }
766190df4a2SClaudio Imbrenda 
767190df4a2SClaudio Imbrenda /*
768190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
769190df4a2SClaudio Imbrenda  * kvm->lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
770190df4a2SClaudio Imbrenda  */
771190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
772190df4a2SClaudio Imbrenda {
773190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
774190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
775190df4a2SClaudio Imbrenda 	/* should be the only one */
776190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
777190df4a2SClaudio Imbrenda 	unsigned long ram_pages;
778190df4a2SClaudio Imbrenda 	int slotnr;
779190df4a2SClaudio Imbrenda 
780190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
781190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state)
782190df4a2SClaudio Imbrenda 		return 0;
783190df4a2SClaudio Imbrenda 
784190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
785190df4a2SClaudio Imbrenda 	if (!slots || !slots->used_slots)
786190df4a2SClaudio Imbrenda 		return -EINVAL;
787190df4a2SClaudio Imbrenda 
788190df4a2SClaudio Imbrenda 	mgs = kzalloc(sizeof(*mgs), GFP_KERNEL);
789190df4a2SClaudio Imbrenda 	if (!mgs)
790190df4a2SClaudio Imbrenda 		return -ENOMEM;
791190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = mgs;
792190df4a2SClaudio Imbrenda 
793190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
794190df4a2SClaudio Imbrenda 		/*
795190df4a2SClaudio Imbrenda 		 * Get the last slot. They should be sorted by base_gfn, so the
796190df4a2SClaudio Imbrenda 		 * last slot is also the one at the end of the address space.
797190df4a2SClaudio Imbrenda 		 * We have verified above that at least one slot is present.
798190df4a2SClaudio Imbrenda 		 */
799190df4a2SClaudio Imbrenda 		ms = slots->memslots + slots->used_slots - 1;
800190df4a2SClaudio Imbrenda 		/* round up so we only use full longs */
801190df4a2SClaudio Imbrenda 		ram_pages = roundup(ms->base_gfn + ms->npages, BITS_PER_LONG);
802190df4a2SClaudio Imbrenda 		/* allocate enough bytes to store all the bits */
803190df4a2SClaudio Imbrenda 		mgs->pgste_bitmap = vmalloc(ram_pages / 8);
804190df4a2SClaudio Imbrenda 		if (!mgs->pgste_bitmap) {
805190df4a2SClaudio Imbrenda 			kfree(mgs);
806190df4a2SClaudio Imbrenda 			kvm->arch.migration_state = NULL;
807190df4a2SClaudio Imbrenda 			return -ENOMEM;
808190df4a2SClaudio Imbrenda 		}
809190df4a2SClaudio Imbrenda 
810190df4a2SClaudio Imbrenda 		mgs->bitmap_size = ram_pages;
811190df4a2SClaudio Imbrenda 		atomic64_set(&mgs->dirty_pages, ram_pages);
812190df4a2SClaudio Imbrenda 		/* mark all the pages in active slots as dirty */
813190df4a2SClaudio Imbrenda 		for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
814190df4a2SClaudio Imbrenda 			ms = slots->memslots + slotnr;
815190df4a2SClaudio Imbrenda 			bitmap_set(mgs->pgste_bitmap, ms->base_gfn, ms->npages);
816190df4a2SClaudio Imbrenda 		}
817190df4a2SClaudio Imbrenda 
818190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
819190df4a2SClaudio Imbrenda 	}
820190df4a2SClaudio Imbrenda 	return 0;
821190df4a2SClaudio Imbrenda }
822190df4a2SClaudio Imbrenda 
823190df4a2SClaudio Imbrenda /*
824190df4a2SClaudio Imbrenda  * Must be called with kvm->lock to avoid races with ourselves and
825190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
826190df4a2SClaudio Imbrenda  */
827190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
828190df4a2SClaudio Imbrenda {
829190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
830190df4a2SClaudio Imbrenda 
831190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
832190df4a2SClaudio Imbrenda 	if (!kvm->arch.migration_state)
833190df4a2SClaudio Imbrenda 		return 0;
834190df4a2SClaudio Imbrenda 	mgs = kvm->arch.migration_state;
835190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = NULL;
836190df4a2SClaudio Imbrenda 
837190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
838190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
839190df4a2SClaudio Imbrenda 		vfree(mgs->pgste_bitmap);
840190df4a2SClaudio Imbrenda 	}
841190df4a2SClaudio Imbrenda 	kfree(mgs);
842190df4a2SClaudio Imbrenda 	return 0;
843190df4a2SClaudio Imbrenda }
844190df4a2SClaudio Imbrenda 
845190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
846190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
847190df4a2SClaudio Imbrenda {
848190df4a2SClaudio Imbrenda 	int idx, res = -ENXIO;
849190df4a2SClaudio Imbrenda 
850190df4a2SClaudio Imbrenda 	mutex_lock(&kvm->lock);
851190df4a2SClaudio Imbrenda 	switch (attr->attr) {
852190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
853190df4a2SClaudio Imbrenda 		idx = srcu_read_lock(&kvm->srcu);
854190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
855190df4a2SClaudio Imbrenda 		srcu_read_unlock(&kvm->srcu, idx);
856190df4a2SClaudio Imbrenda 		break;
857190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
858190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
859190df4a2SClaudio Imbrenda 		break;
860190df4a2SClaudio Imbrenda 	default:
861190df4a2SClaudio Imbrenda 		break;
862190df4a2SClaudio Imbrenda 	}
863190df4a2SClaudio Imbrenda 	mutex_unlock(&kvm->lock);
864190df4a2SClaudio Imbrenda 
865190df4a2SClaudio Imbrenda 	return res;
866190df4a2SClaudio Imbrenda }
867190df4a2SClaudio Imbrenda 
868190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
869190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
870190df4a2SClaudio Imbrenda {
871190df4a2SClaudio Imbrenda 	u64 mig = (kvm->arch.migration_state != NULL);
872190df4a2SClaudio Imbrenda 
873190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
874190df4a2SClaudio Imbrenda 		return -ENXIO;
875190df4a2SClaudio Imbrenda 
876190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
877190df4a2SClaudio Imbrenda 		return -EFAULT;
878190df4a2SClaudio Imbrenda 	return 0;
879190df4a2SClaudio Imbrenda }
880190df4a2SClaudio Imbrenda 
8818fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
8828fa1696eSCollin L. Walling {
8838fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
8848fa1696eSCollin L. Walling 
8858fa1696eSCollin L. Walling 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
8868fa1696eSCollin L. Walling 		return -EFAULT;
8878fa1696eSCollin L. Walling 
8888fa1696eSCollin L. Walling 	if (test_kvm_facility(kvm, 139))
8898fa1696eSCollin L. Walling 		kvm_s390_set_tod_clock_ext(kvm, &gtod);
8908fa1696eSCollin L. Walling 	else if (gtod.epoch_idx == 0)
8918fa1696eSCollin L. Walling 		kvm_s390_set_tod_clock(kvm, gtod.tod);
8928fa1696eSCollin L. Walling 	else
8938fa1696eSCollin L. Walling 		return -EINVAL;
8948fa1696eSCollin L. Walling 
8958fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
8968fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
8978fa1696eSCollin L. Walling 
8988fa1696eSCollin L. Walling 	return 0;
8998fa1696eSCollin L. Walling }
9008fa1696eSCollin L. Walling 
90172f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
90272f25020SJason J. Herne {
90372f25020SJason J. Herne 	u8 gtod_high;
90472f25020SJason J. Herne 
90572f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
90672f25020SJason J. Herne 					   sizeof(gtod_high)))
90772f25020SJason J. Herne 		return -EFAULT;
90872f25020SJason J. Herne 
90972f25020SJason J. Herne 	if (gtod_high != 0)
91072f25020SJason J. Herne 		return -EINVAL;
91158c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
91272f25020SJason J. Herne 
91372f25020SJason J. Herne 	return 0;
91472f25020SJason J. Herne }
91572f25020SJason J. Herne 
91672f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
91772f25020SJason J. Herne {
9185a3d883aSDavid Hildenbrand 	u64 gtod;
91972f25020SJason J. Herne 
92072f25020SJason J. Herne 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
92172f25020SJason J. Herne 		return -EFAULT;
92272f25020SJason J. Herne 
92325ed1675SDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, gtod);
92458c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
92572f25020SJason J. Herne 	return 0;
92672f25020SJason J. Herne }
92772f25020SJason J. Herne 
92872f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
92972f25020SJason J. Herne {
93072f25020SJason J. Herne 	int ret;
93172f25020SJason J. Herne 
93272f25020SJason J. Herne 	if (attr->flags)
93372f25020SJason J. Herne 		return -EINVAL;
93472f25020SJason J. Herne 
93572f25020SJason J. Herne 	switch (attr->attr) {
9368fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
9378fa1696eSCollin L. Walling 		ret = kvm_s390_set_tod_ext(kvm, attr);
9388fa1696eSCollin L. Walling 		break;
93972f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
94072f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
94172f25020SJason J. Herne 		break;
94272f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
94372f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
94472f25020SJason J. Herne 		break;
94572f25020SJason J. Herne 	default:
94672f25020SJason J. Herne 		ret = -ENXIO;
94772f25020SJason J. Herne 		break;
94872f25020SJason J. Herne 	}
94972f25020SJason J. Herne 	return ret;
95072f25020SJason J. Herne }
95172f25020SJason J. Herne 
9528fa1696eSCollin L. Walling static void kvm_s390_get_tod_clock_ext(struct kvm *kvm,
9538fa1696eSCollin L. Walling 					struct kvm_s390_vm_tod_clock *gtod)
9548fa1696eSCollin L. Walling {
9558fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
9568fa1696eSCollin L. Walling 
9578fa1696eSCollin L. Walling 	preempt_disable();
9588fa1696eSCollin L. Walling 
9598fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
9608fa1696eSCollin L. Walling 
9618fa1696eSCollin L. Walling 	gtod->tod = htod.tod + kvm->arch.epoch;
9628fa1696eSCollin L. Walling 	gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx;
9638fa1696eSCollin L. Walling 
9648fa1696eSCollin L. Walling 	if (gtod->tod < htod.tod)
9658fa1696eSCollin L. Walling 		gtod->epoch_idx += 1;
9668fa1696eSCollin L. Walling 
9678fa1696eSCollin L. Walling 	preempt_enable();
9688fa1696eSCollin L. Walling }
9698fa1696eSCollin L. Walling 
9708fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
9718fa1696eSCollin L. Walling {
9728fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
9738fa1696eSCollin L. Walling 
9748fa1696eSCollin L. Walling 	memset(&gtod, 0, sizeof(gtod));
9758fa1696eSCollin L. Walling 
9768fa1696eSCollin L. Walling 	if (test_kvm_facility(kvm, 139))
9778fa1696eSCollin L. Walling 		kvm_s390_get_tod_clock_ext(kvm, &gtod);
9788fa1696eSCollin L. Walling 	else
9798fa1696eSCollin L. Walling 		gtod.tod = kvm_s390_get_tod_clock_fast(kvm);
9808fa1696eSCollin L. Walling 
9818fa1696eSCollin L. Walling 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
9828fa1696eSCollin L. Walling 		return -EFAULT;
9838fa1696eSCollin L. Walling 
9848fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
9858fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
9868fa1696eSCollin L. Walling 	return 0;
9878fa1696eSCollin L. Walling }
9888fa1696eSCollin L. Walling 
98972f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
99072f25020SJason J. Herne {
99172f25020SJason J. Herne 	u8 gtod_high = 0;
99272f25020SJason J. Herne 
99372f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
99472f25020SJason J. Herne 					 sizeof(gtod_high)))
99572f25020SJason J. Herne 		return -EFAULT;
99658c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
99772f25020SJason J. Herne 
99872f25020SJason J. Herne 	return 0;
99972f25020SJason J. Herne }
100072f25020SJason J. Herne 
100172f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
100272f25020SJason J. Herne {
10035a3d883aSDavid Hildenbrand 	u64 gtod;
100472f25020SJason J. Herne 
100560417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
100672f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
100772f25020SJason J. Herne 		return -EFAULT;
100858c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
100972f25020SJason J. Herne 
101072f25020SJason J. Herne 	return 0;
101172f25020SJason J. Herne }
101272f25020SJason J. Herne 
101372f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
101472f25020SJason J. Herne {
101572f25020SJason J. Herne 	int ret;
101672f25020SJason J. Herne 
101772f25020SJason J. Herne 	if (attr->flags)
101872f25020SJason J. Herne 		return -EINVAL;
101972f25020SJason J. Herne 
102072f25020SJason J. Herne 	switch (attr->attr) {
10218fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
10228fa1696eSCollin L. Walling 		ret = kvm_s390_get_tod_ext(kvm, attr);
10238fa1696eSCollin L. Walling 		break;
102472f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
102572f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
102672f25020SJason J. Herne 		break;
102772f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
102872f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
102972f25020SJason J. Herne 		break;
103072f25020SJason J. Herne 	default:
103172f25020SJason J. Herne 		ret = -ENXIO;
103272f25020SJason J. Herne 		break;
103372f25020SJason J. Herne 	}
103472f25020SJason J. Herne 	return ret;
103572f25020SJason J. Herne }
103672f25020SJason J. Herne 
1037658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1038658b6edaSMichael Mueller {
1039658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1040053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
1041658b6edaSMichael Mueller 	int ret = 0;
1042658b6edaSMichael Mueller 
1043658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
1044a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
1045658b6edaSMichael Mueller 		ret = -EBUSY;
1046658b6edaSMichael Mueller 		goto out;
1047658b6edaSMichael Mueller 	}
1048658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1049658b6edaSMichael Mueller 	if (!proc) {
1050658b6edaSMichael Mueller 		ret = -ENOMEM;
1051658b6edaSMichael Mueller 		goto out;
1052658b6edaSMichael Mueller 	}
1053658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
1054658b6edaSMichael Mueller 			    sizeof(*proc))) {
10559bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
1056053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1057053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
10580487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
1059053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
1060053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
1061053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
1062053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
1063053dd230SDavid Hildenbrand 			else
1064658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
1065053dd230SDavid Hildenbrand 		}
1066c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1067658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1068a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1069a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1070a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1071a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1072a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1073a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1074a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1075658b6edaSMichael Mueller 	} else
1076658b6edaSMichael Mueller 		ret = -EFAULT;
1077658b6edaSMichael Mueller 	kfree(proc);
1078658b6edaSMichael Mueller out:
1079658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1080658b6edaSMichael Mueller 	return ret;
1081658b6edaSMichael Mueller }
1082658b6edaSMichael Mueller 
108315c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
108415c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
108515c9705fSDavid Hildenbrand {
108615c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
108715c9705fSDavid Hildenbrand 	int ret = -EBUSY;
108815c9705fSDavid Hildenbrand 
108915c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
109015c9705fSDavid Hildenbrand 		return -EFAULT;
109115c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
109215c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
109315c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
109415c9705fSDavid Hildenbrand 		return -EINVAL;
109515c9705fSDavid Hildenbrand 
109615c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
109715c9705fSDavid Hildenbrand 	if (!atomic_read(&kvm->online_vcpus)) {
109815c9705fSDavid Hildenbrand 		bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
109915c9705fSDavid Hildenbrand 			    KVM_S390_VM_CPU_FEAT_NR_BITS);
110015c9705fSDavid Hildenbrand 		ret = 0;
110115c9705fSDavid Hildenbrand 	}
110215c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
110315c9705fSDavid Hildenbrand 	return ret;
110415c9705fSDavid Hildenbrand }
110515c9705fSDavid Hildenbrand 
11060a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
11070a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
11080a763c78SDavid Hildenbrand {
11090a763c78SDavid Hildenbrand 	/*
11100a763c78SDavid Hildenbrand 	 * Once supported by kernel + hw, we have to store the subfunctions
11110a763c78SDavid Hildenbrand 	 * in kvm->arch and remember that user space configured them.
11120a763c78SDavid Hildenbrand 	 */
11130a763c78SDavid Hildenbrand 	return -ENXIO;
11140a763c78SDavid Hildenbrand }
11150a763c78SDavid Hildenbrand 
1116658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1117658b6edaSMichael Mueller {
1118658b6edaSMichael Mueller 	int ret = -ENXIO;
1119658b6edaSMichael Mueller 
1120658b6edaSMichael Mueller 	switch (attr->attr) {
1121658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1122658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1123658b6edaSMichael Mueller 		break;
112415c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
112515c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
112615c9705fSDavid Hildenbrand 		break;
11270a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
11280a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
11290a763c78SDavid Hildenbrand 		break;
1130658b6edaSMichael Mueller 	}
1131658b6edaSMichael Mueller 	return ret;
1132658b6edaSMichael Mueller }
1133658b6edaSMichael Mueller 
1134658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1135658b6edaSMichael Mueller {
1136658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1137658b6edaSMichael Mueller 	int ret = 0;
1138658b6edaSMichael Mueller 
1139658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1140658b6edaSMichael Mueller 	if (!proc) {
1141658b6edaSMichael Mueller 		ret = -ENOMEM;
1142658b6edaSMichael Mueller 		goto out;
1143658b6edaSMichael Mueller 	}
11449bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1145658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1146c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1147c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1148a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1149a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1150a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1151a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1152a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1153a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1154a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1155658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1156658b6edaSMichael Mueller 		ret = -EFAULT;
1157658b6edaSMichael Mueller 	kfree(proc);
1158658b6edaSMichael Mueller out:
1159658b6edaSMichael Mueller 	return ret;
1160658b6edaSMichael Mueller }
1161658b6edaSMichael Mueller 
1162658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1163658b6edaSMichael Mueller {
1164658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1165658b6edaSMichael Mueller 	int ret = 0;
1166658b6edaSMichael Mueller 
1167658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1168658b6edaSMichael Mueller 	if (!mach) {
1169658b6edaSMichael Mueller 		ret = -ENOMEM;
1170658b6edaSMichael Mueller 		goto out;
1171658b6edaSMichael Mueller 	}
1172658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
117337c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1174c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1175981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1176658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
117704478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
1178a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1179a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1180a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1181a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1182a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1183a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1184a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1185a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1186a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1187a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1188a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1189658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1190658b6edaSMichael Mueller 		ret = -EFAULT;
1191658b6edaSMichael Mueller 	kfree(mach);
1192658b6edaSMichael Mueller out:
1193658b6edaSMichael Mueller 	return ret;
1194658b6edaSMichael Mueller }
1195658b6edaSMichael Mueller 
119615c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
119715c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
119815c9705fSDavid Hildenbrand {
119915c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
120015c9705fSDavid Hildenbrand 
120115c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
120215c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
120315c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
120415c9705fSDavid Hildenbrand 		return -EFAULT;
120515c9705fSDavid Hildenbrand 	return 0;
120615c9705fSDavid Hildenbrand }
120715c9705fSDavid Hildenbrand 
120815c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
120915c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
121015c9705fSDavid Hildenbrand {
121115c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
121215c9705fSDavid Hildenbrand 
121315c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
121415c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
121515c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
121615c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
121715c9705fSDavid Hildenbrand 		return -EFAULT;
121815c9705fSDavid Hildenbrand 	return 0;
121915c9705fSDavid Hildenbrand }
122015c9705fSDavid Hildenbrand 
12210a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
12220a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
12230a763c78SDavid Hildenbrand {
12240a763c78SDavid Hildenbrand 	/*
12250a763c78SDavid Hildenbrand 	 * Once we can actually configure subfunctions (kernel + hw support),
12260a763c78SDavid Hildenbrand 	 * we have to check if they were already set by user space, if so copy
12270a763c78SDavid Hildenbrand 	 * them from kvm->arch.
12280a763c78SDavid Hildenbrand 	 */
12290a763c78SDavid Hildenbrand 	return -ENXIO;
12300a763c78SDavid Hildenbrand }
12310a763c78SDavid Hildenbrand 
12320a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
12330a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
12340a763c78SDavid Hildenbrand {
12350a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
12360a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
12370a763c78SDavid Hildenbrand 		return -EFAULT;
12380a763c78SDavid Hildenbrand 	return 0;
12390a763c78SDavid Hildenbrand }
1240658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1241658b6edaSMichael Mueller {
1242658b6edaSMichael Mueller 	int ret = -ENXIO;
1243658b6edaSMichael Mueller 
1244658b6edaSMichael Mueller 	switch (attr->attr) {
1245658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1246658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1247658b6edaSMichael Mueller 		break;
1248658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1249658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1250658b6edaSMichael Mueller 		break;
125115c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
125215c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
125315c9705fSDavid Hildenbrand 		break;
125415c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
125515c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
125615c9705fSDavid Hildenbrand 		break;
12570a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
12580a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
12590a763c78SDavid Hildenbrand 		break;
12600a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
12610a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
12620a763c78SDavid Hildenbrand 		break;
1263658b6edaSMichael Mueller 	}
1264658b6edaSMichael Mueller 	return ret;
1265658b6edaSMichael Mueller }
1266658b6edaSMichael Mueller 
1267f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1268f2061656SDominik Dingel {
1269f2061656SDominik Dingel 	int ret;
1270f2061656SDominik Dingel 
1271f2061656SDominik Dingel 	switch (attr->group) {
12724f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
12738c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
12744f718eabSDominik Dingel 		break;
127572f25020SJason J. Herne 	case KVM_S390_VM_TOD:
127672f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
127772f25020SJason J. Herne 		break;
1278658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1279658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1280658b6edaSMichael Mueller 		break;
1281a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1282a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1283a374e892STony Krowiak 		break;
1284190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1285190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1286190df4a2SClaudio Imbrenda 		break;
1287f2061656SDominik Dingel 	default:
1288f2061656SDominik Dingel 		ret = -ENXIO;
1289f2061656SDominik Dingel 		break;
1290f2061656SDominik Dingel 	}
1291f2061656SDominik Dingel 
1292f2061656SDominik Dingel 	return ret;
1293f2061656SDominik Dingel }
1294f2061656SDominik Dingel 
1295f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1296f2061656SDominik Dingel {
12978c0a7ce6SDominik Dingel 	int ret;
12988c0a7ce6SDominik Dingel 
12998c0a7ce6SDominik Dingel 	switch (attr->group) {
13008c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
13018c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
13028c0a7ce6SDominik Dingel 		break;
130372f25020SJason J. Herne 	case KVM_S390_VM_TOD:
130472f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
130572f25020SJason J. Herne 		break;
1306658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1307658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1308658b6edaSMichael Mueller 		break;
1309190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1310190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1311190df4a2SClaudio Imbrenda 		break;
13128c0a7ce6SDominik Dingel 	default:
13138c0a7ce6SDominik Dingel 		ret = -ENXIO;
13148c0a7ce6SDominik Dingel 		break;
13158c0a7ce6SDominik Dingel 	}
13168c0a7ce6SDominik Dingel 
13178c0a7ce6SDominik Dingel 	return ret;
1318f2061656SDominik Dingel }
1319f2061656SDominik Dingel 
1320f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1321f2061656SDominik Dingel {
1322f2061656SDominik Dingel 	int ret;
1323f2061656SDominik Dingel 
1324f2061656SDominik Dingel 	switch (attr->group) {
13254f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
13264f718eabSDominik Dingel 		switch (attr->attr) {
13274f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
13284f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1329f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1330f9cbd9b0SDavid Hildenbrand 			break;
13318c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
13324f718eabSDominik Dingel 			ret = 0;
13334f718eabSDominik Dingel 			break;
13344f718eabSDominik Dingel 		default:
13354f718eabSDominik Dingel 			ret = -ENXIO;
13364f718eabSDominik Dingel 			break;
13374f718eabSDominik Dingel 		}
13384f718eabSDominik Dingel 		break;
133972f25020SJason J. Herne 	case KVM_S390_VM_TOD:
134072f25020SJason J. Herne 		switch (attr->attr) {
134172f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
134272f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
134372f25020SJason J. Herne 			ret = 0;
134472f25020SJason J. Herne 			break;
134572f25020SJason J. Herne 		default:
134672f25020SJason J. Herne 			ret = -ENXIO;
134772f25020SJason J. Herne 			break;
134872f25020SJason J. Herne 		}
134972f25020SJason J. Herne 		break;
1350658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1351658b6edaSMichael Mueller 		switch (attr->attr) {
1352658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1353658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
135415c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
135515c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
13560a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1357658b6edaSMichael Mueller 			ret = 0;
1358658b6edaSMichael Mueller 			break;
13590a763c78SDavid Hildenbrand 		/* configuring subfunctions is not supported yet */
13600a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1361658b6edaSMichael Mueller 		default:
1362658b6edaSMichael Mueller 			ret = -ENXIO;
1363658b6edaSMichael Mueller 			break;
1364658b6edaSMichael Mueller 		}
1365658b6edaSMichael Mueller 		break;
1366a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1367a374e892STony Krowiak 		switch (attr->attr) {
1368a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1369a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1370a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1371a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1372a374e892STony Krowiak 			ret = 0;
1373a374e892STony Krowiak 			break;
1374a374e892STony Krowiak 		default:
1375a374e892STony Krowiak 			ret = -ENXIO;
1376a374e892STony Krowiak 			break;
1377a374e892STony Krowiak 		}
1378a374e892STony Krowiak 		break;
1379190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1380190df4a2SClaudio Imbrenda 		ret = 0;
1381190df4a2SClaudio Imbrenda 		break;
1382f2061656SDominik Dingel 	default:
1383f2061656SDominik Dingel 		ret = -ENXIO;
1384f2061656SDominik Dingel 		break;
1385f2061656SDominik Dingel 	}
1386f2061656SDominik Dingel 
1387f2061656SDominik Dingel 	return ret;
1388f2061656SDominik Dingel }
1389f2061656SDominik Dingel 
139030ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
139130ee2a98SJason J. Herne {
139230ee2a98SJason J. Herne 	uint8_t *keys;
139330ee2a98SJason J. Herne 	uint64_t hva;
13944f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
139530ee2a98SJason J. Herne 
139630ee2a98SJason J. Herne 	if (args->flags != 0)
139730ee2a98SJason J. Herne 		return -EINVAL;
139830ee2a98SJason J. Herne 
139930ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
140030ee2a98SJason J. Herne 	if (!mm_use_skey(current->mm))
140130ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
140230ee2a98SJason J. Herne 
140330ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
140430ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
140530ee2a98SJason J. Herne 		return -EINVAL;
140630ee2a98SJason J. Herne 
1407752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
140830ee2a98SJason J. Herne 	if (!keys)
140930ee2a98SJason J. Herne 		return -ENOMEM;
141030ee2a98SJason J. Herne 
1411d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
14124f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
141330ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
141430ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
141530ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
141630ee2a98SJason J. Herne 			r = -EFAULT;
1417d3ed1ceeSMartin Schwidefsky 			break;
141830ee2a98SJason J. Herne 		}
141930ee2a98SJason J. Herne 
1420154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1421154c8c19SDavid Hildenbrand 		if (r)
1422d3ed1ceeSMartin Schwidefsky 			break;
142330ee2a98SJason J. Herne 	}
14244f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1425d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
142630ee2a98SJason J. Herne 
1427d3ed1ceeSMartin Schwidefsky 	if (!r) {
142830ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
142930ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
143030ee2a98SJason J. Herne 		if (r)
143130ee2a98SJason J. Herne 			r = -EFAULT;
1432d3ed1ceeSMartin Schwidefsky 	}
1433d3ed1ceeSMartin Schwidefsky 
143430ee2a98SJason J. Herne 	kvfree(keys);
143530ee2a98SJason J. Herne 	return r;
143630ee2a98SJason J. Herne }
143730ee2a98SJason J. Herne 
143830ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
143930ee2a98SJason J. Herne {
144030ee2a98SJason J. Herne 	uint8_t *keys;
144130ee2a98SJason J. Herne 	uint64_t hva;
14424f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
144330ee2a98SJason J. Herne 
144430ee2a98SJason J. Herne 	if (args->flags != 0)
144530ee2a98SJason J. Herne 		return -EINVAL;
144630ee2a98SJason J. Herne 
144730ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
144830ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
144930ee2a98SJason J. Herne 		return -EINVAL;
145030ee2a98SJason J. Herne 
1451752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
145230ee2a98SJason J. Herne 	if (!keys)
145330ee2a98SJason J. Herne 		return -ENOMEM;
145430ee2a98SJason J. Herne 
145530ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
145630ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
145730ee2a98SJason J. Herne 	if (r) {
145830ee2a98SJason J. Herne 		r = -EFAULT;
145930ee2a98SJason J. Herne 		goto out;
146030ee2a98SJason J. Herne 	}
146130ee2a98SJason J. Herne 
146230ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
146314d4a425SDominik Dingel 	r = s390_enable_skey();
146414d4a425SDominik Dingel 	if (r)
146514d4a425SDominik Dingel 		goto out;
146630ee2a98SJason J. Herne 
1467d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
14684f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
146930ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
147030ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
147130ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
147230ee2a98SJason J. Herne 			r = -EFAULT;
1473d3ed1ceeSMartin Schwidefsky 			break;
147430ee2a98SJason J. Herne 		}
147530ee2a98SJason J. Herne 
147630ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
147730ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
147830ee2a98SJason J. Herne 			r = -EINVAL;
1479d3ed1ceeSMartin Schwidefsky 			break;
148030ee2a98SJason J. Herne 		}
148130ee2a98SJason J. Herne 
1482fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
148330ee2a98SJason J. Herne 		if (r)
1484d3ed1ceeSMartin Schwidefsky 			break;
148530ee2a98SJason J. Herne 	}
14864f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1487d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
148830ee2a98SJason J. Herne out:
148930ee2a98SJason J. Herne 	kvfree(keys);
149030ee2a98SJason J. Herne 	return r;
149130ee2a98SJason J. Herne }
149230ee2a98SJason J. Herne 
14934036e387SClaudio Imbrenda /*
14944036e387SClaudio Imbrenda  * Base address and length must be sent at the start of each block, therefore
14954036e387SClaudio Imbrenda  * it's cheaper to send some clean data, as long as it's less than the size of
14964036e387SClaudio Imbrenda  * two longs.
14974036e387SClaudio Imbrenda  */
14984036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
14994036e387SClaudio Imbrenda /* for consistency */
15004036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
15014036e387SClaudio Imbrenda 
15024036e387SClaudio Imbrenda /*
15034036e387SClaudio Imbrenda  * This function searches for the next page with dirty CMMA attributes, and
15044036e387SClaudio Imbrenda  * saves the attributes in the buffer up to either the end of the buffer or
15054036e387SClaudio Imbrenda  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
15064036e387SClaudio Imbrenda  * no trailing clean bytes are saved.
15074036e387SClaudio Imbrenda  * In case no dirty bits were found, or if CMMA was not enabled or used, the
15084036e387SClaudio Imbrenda  * output buffer will indicate 0 as length.
15094036e387SClaudio Imbrenda  */
15104036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
15114036e387SClaudio Imbrenda 				  struct kvm_s390_cmma_log *args)
15124036e387SClaudio Imbrenda {
15134036e387SClaudio Imbrenda 	struct kvm_s390_migration_state *s = kvm->arch.migration_state;
15144036e387SClaudio Imbrenda 	unsigned long bufsize, hva, pgstev, i, next, cur;
15154036e387SClaudio Imbrenda 	int srcu_idx, peek, r = 0, rr;
15164036e387SClaudio Imbrenda 	u8 *res;
15174036e387SClaudio Imbrenda 
15184036e387SClaudio Imbrenda 	cur = args->start_gfn;
15194036e387SClaudio Imbrenda 	i = next = pgstev = 0;
15204036e387SClaudio Imbrenda 
15214036e387SClaudio Imbrenda 	if (unlikely(!kvm->arch.use_cmma))
15224036e387SClaudio Imbrenda 		return -ENXIO;
15234036e387SClaudio Imbrenda 	/* Invalid/unsupported flags were specified */
15244036e387SClaudio Imbrenda 	if (args->flags & ~KVM_S390_CMMA_PEEK)
15254036e387SClaudio Imbrenda 		return -EINVAL;
15264036e387SClaudio Imbrenda 	/* Migration mode query, and we are not doing a migration */
15274036e387SClaudio Imbrenda 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
15284036e387SClaudio Imbrenda 	if (!peek && !s)
15294036e387SClaudio Imbrenda 		return -EINVAL;
15304036e387SClaudio Imbrenda 	/* CMMA is disabled or was not used, or the buffer has length zero */
15314036e387SClaudio Imbrenda 	bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
15324036e387SClaudio Imbrenda 	if (!bufsize || !kvm->mm->context.use_cmma) {
15334036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
15344036e387SClaudio Imbrenda 		return 0;
15354036e387SClaudio Imbrenda 	}
15364036e387SClaudio Imbrenda 
15374036e387SClaudio Imbrenda 	if (!peek) {
15384036e387SClaudio Imbrenda 		/* We are not peeking, and there are no dirty pages */
15394036e387SClaudio Imbrenda 		if (!atomic64_read(&s->dirty_pages)) {
15404036e387SClaudio Imbrenda 			memset(args, 0, sizeof(*args));
15414036e387SClaudio Imbrenda 			return 0;
15424036e387SClaudio Imbrenda 		}
15434036e387SClaudio Imbrenda 		cur = find_next_bit(s->pgste_bitmap, s->bitmap_size,
15444036e387SClaudio Imbrenda 				    args->start_gfn);
15454036e387SClaudio Imbrenda 		if (cur >= s->bitmap_size)	/* nothing found, loop back */
15464036e387SClaudio Imbrenda 			cur = find_next_bit(s->pgste_bitmap, s->bitmap_size, 0);
15474036e387SClaudio Imbrenda 		if (cur >= s->bitmap_size) {	/* again! (very unlikely) */
15484036e387SClaudio Imbrenda 			memset(args, 0, sizeof(*args));
15494036e387SClaudio Imbrenda 			return 0;
15504036e387SClaudio Imbrenda 		}
15514036e387SClaudio Imbrenda 		next = find_next_bit(s->pgste_bitmap, s->bitmap_size, cur + 1);
15524036e387SClaudio Imbrenda 	}
15534036e387SClaudio Imbrenda 
15544036e387SClaudio Imbrenda 	res = vmalloc(bufsize);
15554036e387SClaudio Imbrenda 	if (!res)
15564036e387SClaudio Imbrenda 		return -ENOMEM;
15574036e387SClaudio Imbrenda 
15584036e387SClaudio Imbrenda 	args->start_gfn = cur;
15594036e387SClaudio Imbrenda 
15604036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
15614036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
15624036e387SClaudio Imbrenda 	while (i < bufsize) {
15634036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur);
15644036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
15654036e387SClaudio Imbrenda 			r = -EFAULT;
15664036e387SClaudio Imbrenda 			break;
15674036e387SClaudio Imbrenda 		}
15684036e387SClaudio Imbrenda 		/* decrement only if we actually flipped the bit to 0 */
15694036e387SClaudio Imbrenda 		if (!peek && test_and_clear_bit(cur, s->pgste_bitmap))
15704036e387SClaudio Imbrenda 			atomic64_dec(&s->dirty_pages);
15714036e387SClaudio Imbrenda 		r = get_pgste(kvm->mm, hva, &pgstev);
15724036e387SClaudio Imbrenda 		if (r < 0)
15734036e387SClaudio Imbrenda 			pgstev = 0;
15744036e387SClaudio Imbrenda 		/* save the value */
15751bab1c02SClaudio Imbrenda 		res[i++] = (pgstev >> 24) & 0x43;
15764036e387SClaudio Imbrenda 		/*
15774036e387SClaudio Imbrenda 		 * if the next bit is too far away, stop.
15784036e387SClaudio Imbrenda 		 * if we reached the previous "next", find the next one
15794036e387SClaudio Imbrenda 		 */
15804036e387SClaudio Imbrenda 		if (!peek) {
15814036e387SClaudio Imbrenda 			if (next > cur + KVM_S390_MAX_BIT_DISTANCE)
15824036e387SClaudio Imbrenda 				break;
15834036e387SClaudio Imbrenda 			if (cur == next)
15844036e387SClaudio Imbrenda 				next = find_next_bit(s->pgste_bitmap,
15854036e387SClaudio Imbrenda 						     s->bitmap_size, cur + 1);
15864036e387SClaudio Imbrenda 		/* reached the end of the bitmap or of the buffer, stop */
15874036e387SClaudio Imbrenda 			if ((next >= s->bitmap_size) ||
15884036e387SClaudio Imbrenda 			    (next >= args->start_gfn + bufsize))
15894036e387SClaudio Imbrenda 				break;
15904036e387SClaudio Imbrenda 		}
15914036e387SClaudio Imbrenda 		cur++;
15924036e387SClaudio Imbrenda 	}
15934036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
15944036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
15954036e387SClaudio Imbrenda 	args->count = i;
15964036e387SClaudio Imbrenda 	args->remaining = s ? atomic64_read(&s->dirty_pages) : 0;
15974036e387SClaudio Imbrenda 
15984036e387SClaudio Imbrenda 	rr = copy_to_user((void __user *)args->values, res, args->count);
15994036e387SClaudio Imbrenda 	if (rr)
16004036e387SClaudio Imbrenda 		r = -EFAULT;
16014036e387SClaudio Imbrenda 
16024036e387SClaudio Imbrenda 	vfree(res);
16034036e387SClaudio Imbrenda 	return r;
16044036e387SClaudio Imbrenda }
16054036e387SClaudio Imbrenda 
16064036e387SClaudio Imbrenda /*
16074036e387SClaudio Imbrenda  * This function sets the CMMA attributes for the given pages. If the input
16084036e387SClaudio Imbrenda  * buffer has zero length, no action is taken, otherwise the attributes are
16094036e387SClaudio Imbrenda  * set and the mm->context.use_cmma flag is set.
16104036e387SClaudio Imbrenda  */
16114036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
16124036e387SClaudio Imbrenda 				  const struct kvm_s390_cmma_log *args)
16134036e387SClaudio Imbrenda {
16144036e387SClaudio Imbrenda 	unsigned long hva, mask, pgstev, i;
16154036e387SClaudio Imbrenda 	uint8_t *bits;
16164036e387SClaudio Imbrenda 	int srcu_idx, r = 0;
16174036e387SClaudio Imbrenda 
16184036e387SClaudio Imbrenda 	mask = args->mask;
16194036e387SClaudio Imbrenda 
16204036e387SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
16214036e387SClaudio Imbrenda 		return -ENXIO;
16224036e387SClaudio Imbrenda 	/* invalid/unsupported flags */
16234036e387SClaudio Imbrenda 	if (args->flags != 0)
16244036e387SClaudio Imbrenda 		return -EINVAL;
16254036e387SClaudio Imbrenda 	/* Enforce sane limit on memory allocation */
16264036e387SClaudio Imbrenda 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
16274036e387SClaudio Imbrenda 		return -EINVAL;
16284036e387SClaudio Imbrenda 	/* Nothing to do */
16294036e387SClaudio Imbrenda 	if (args->count == 0)
16304036e387SClaudio Imbrenda 		return 0;
16314036e387SClaudio Imbrenda 
16324036e387SClaudio Imbrenda 	bits = vmalloc(sizeof(*bits) * args->count);
16334036e387SClaudio Imbrenda 	if (!bits)
16344036e387SClaudio Imbrenda 		return -ENOMEM;
16354036e387SClaudio Imbrenda 
16364036e387SClaudio Imbrenda 	r = copy_from_user(bits, (void __user *)args->values, args->count);
16374036e387SClaudio Imbrenda 	if (r) {
16384036e387SClaudio Imbrenda 		r = -EFAULT;
16394036e387SClaudio Imbrenda 		goto out;
16404036e387SClaudio Imbrenda 	}
16414036e387SClaudio Imbrenda 
16424036e387SClaudio Imbrenda 	down_read(&kvm->mm->mmap_sem);
16434036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
16444036e387SClaudio Imbrenda 	for (i = 0; i < args->count; i++) {
16454036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, args->start_gfn + i);
16464036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
16474036e387SClaudio Imbrenda 			r = -EFAULT;
16484036e387SClaudio Imbrenda 			break;
16494036e387SClaudio Imbrenda 		}
16504036e387SClaudio Imbrenda 
16514036e387SClaudio Imbrenda 		pgstev = bits[i];
16524036e387SClaudio Imbrenda 		pgstev = pgstev << 24;
16531bab1c02SClaudio Imbrenda 		mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
16544036e387SClaudio Imbrenda 		set_pgste_bits(kvm->mm, hva, mask, pgstev);
16554036e387SClaudio Imbrenda 	}
16564036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
16574036e387SClaudio Imbrenda 	up_read(&kvm->mm->mmap_sem);
16584036e387SClaudio Imbrenda 
16594036e387SClaudio Imbrenda 	if (!kvm->mm->context.use_cmma) {
16604036e387SClaudio Imbrenda 		down_write(&kvm->mm->mmap_sem);
16614036e387SClaudio Imbrenda 		kvm->mm->context.use_cmma = 1;
16624036e387SClaudio Imbrenda 		up_write(&kvm->mm->mmap_sem);
16634036e387SClaudio Imbrenda 	}
16644036e387SClaudio Imbrenda out:
16654036e387SClaudio Imbrenda 	vfree(bits);
16664036e387SClaudio Imbrenda 	return r;
16674036e387SClaudio Imbrenda }
16684036e387SClaudio Imbrenda 
1669b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
1670b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
1671b0c632dbSHeiko Carstens {
1672b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
1673b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
1674f2061656SDominik Dingel 	struct kvm_device_attr attr;
1675b0c632dbSHeiko Carstens 	int r;
1676b0c632dbSHeiko Carstens 
1677b0c632dbSHeiko Carstens 	switch (ioctl) {
1678ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
1679ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
1680ba5c1e9bSCarsten Otte 
1681ba5c1e9bSCarsten Otte 		r = -EFAULT;
1682ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
1683ba5c1e9bSCarsten Otte 			break;
1684ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
1685ba5c1e9bSCarsten Otte 		break;
1686ba5c1e9bSCarsten Otte 	}
1687d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
1688d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
1689d938dc55SCornelia Huck 		r = -EFAULT;
1690d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1691d938dc55SCornelia Huck 			break;
1692d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1693d938dc55SCornelia Huck 		break;
1694d938dc55SCornelia Huck 	}
169584223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
169684223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
169784223598SCornelia Huck 
169884223598SCornelia Huck 		r = -EINVAL;
169984223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
170084223598SCornelia Huck 			/* Set up dummy routing. */
170184223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
1702152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
170384223598SCornelia Huck 		}
170484223598SCornelia Huck 		break;
170584223598SCornelia Huck 	}
1706f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
1707f2061656SDominik Dingel 		r = -EFAULT;
1708f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1709f2061656SDominik Dingel 			break;
1710f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
1711f2061656SDominik Dingel 		break;
1712f2061656SDominik Dingel 	}
1713f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
1714f2061656SDominik Dingel 		r = -EFAULT;
1715f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1716f2061656SDominik Dingel 			break;
1717f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
1718f2061656SDominik Dingel 		break;
1719f2061656SDominik Dingel 	}
1720f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
1721f2061656SDominik Dingel 		r = -EFAULT;
1722f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1723f2061656SDominik Dingel 			break;
1724f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
1725f2061656SDominik Dingel 		break;
1726f2061656SDominik Dingel 	}
172730ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
172830ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
172930ee2a98SJason J. Herne 
173030ee2a98SJason J. Herne 		r = -EFAULT;
173130ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
173230ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
173330ee2a98SJason J. Herne 			break;
173430ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
173530ee2a98SJason J. Herne 		break;
173630ee2a98SJason J. Herne 	}
173730ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
173830ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
173930ee2a98SJason J. Herne 
174030ee2a98SJason J. Herne 		r = -EFAULT;
174130ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
174230ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
174330ee2a98SJason J. Herne 			break;
174430ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
174530ee2a98SJason J. Herne 		break;
174630ee2a98SJason J. Herne 	}
17474036e387SClaudio Imbrenda 	case KVM_S390_GET_CMMA_BITS: {
17484036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
17494036e387SClaudio Imbrenda 
17504036e387SClaudio Imbrenda 		r = -EFAULT;
17514036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
17524036e387SClaudio Imbrenda 			break;
17534036e387SClaudio Imbrenda 		r = kvm_s390_get_cmma_bits(kvm, &args);
17544036e387SClaudio Imbrenda 		if (!r) {
17554036e387SClaudio Imbrenda 			r = copy_to_user(argp, &args, sizeof(args));
17564036e387SClaudio Imbrenda 			if (r)
17574036e387SClaudio Imbrenda 				r = -EFAULT;
17584036e387SClaudio Imbrenda 		}
17594036e387SClaudio Imbrenda 		break;
17604036e387SClaudio Imbrenda 	}
17614036e387SClaudio Imbrenda 	case KVM_S390_SET_CMMA_BITS: {
17624036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
17634036e387SClaudio Imbrenda 
17644036e387SClaudio Imbrenda 		r = -EFAULT;
17654036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
17664036e387SClaudio Imbrenda 			break;
17674036e387SClaudio Imbrenda 		r = kvm_s390_set_cmma_bits(kvm, &args);
17684036e387SClaudio Imbrenda 		break;
17694036e387SClaudio Imbrenda 	}
1770b0c632dbSHeiko Carstens 	default:
1771367e1319SAvi Kivity 		r = -ENOTTY;
1772b0c632dbSHeiko Carstens 	}
1773b0c632dbSHeiko Carstens 
1774b0c632dbSHeiko Carstens 	return r;
1775b0c632dbSHeiko Carstens }
1776b0c632dbSHeiko Carstens 
177745c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
177845c9b47cSTony Krowiak {
177945c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
178086044c8cSChristian Borntraeger 	u32 cc = 0;
178145c9b47cSTony Krowiak 
178286044c8cSChristian Borntraeger 	memset(config, 0, 128);
178345c9b47cSTony Krowiak 	asm volatile(
178445c9b47cSTony Krowiak 		"lgr 0,%1\n"
178545c9b47cSTony Krowiak 		"lgr 2,%2\n"
178645c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
178786044c8cSChristian Borntraeger 		"0: ipm %0\n"
178845c9b47cSTony Krowiak 		"srl %0,28\n"
178986044c8cSChristian Borntraeger 		"1:\n"
179086044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
179186044c8cSChristian Borntraeger 		: "+r" (cc)
179245c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
179345c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
179445c9b47cSTony Krowiak 	);
179545c9b47cSTony Krowiak 
179645c9b47cSTony Krowiak 	return cc;
179745c9b47cSTony Krowiak }
179845c9b47cSTony Krowiak 
179945c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
180045c9b47cSTony Krowiak {
180145c9b47cSTony Krowiak 	u8 config[128];
180245c9b47cSTony Krowiak 	int cc;
180345c9b47cSTony Krowiak 
1804a6aacc3fSHeiko Carstens 	if (test_facility(12)) {
180545c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
180645c9b47cSTony Krowiak 
180745c9b47cSTony Krowiak 		if (cc)
180845c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
180945c9b47cSTony Krowiak 		else
181045c9b47cSTony Krowiak 			return config[0] & 0x40;
181145c9b47cSTony Krowiak 	}
181245c9b47cSTony Krowiak 
181345c9b47cSTony Krowiak 	return 0;
181445c9b47cSTony Krowiak }
181545c9b47cSTony Krowiak 
181645c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
181745c9b47cSTony Krowiak {
181845c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
181945c9b47cSTony Krowiak 
182045c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
182145c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
182245c9b47cSTony Krowiak 	else
182345c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
182445c9b47cSTony Krowiak }
182545c9b47cSTony Krowiak 
18269bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
18279d8d5786SMichael Mueller {
18289bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
18299bb0ec09SDavid Hildenbrand 
18309bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
18319bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
18329bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
18339d8d5786SMichael Mueller }
18349d8d5786SMichael Mueller 
1835c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
18365102ee87STony Krowiak {
18379d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
1838c54f0d6aSDavid Hildenbrand 		return;
18395102ee87STony Krowiak 
1840c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
184145c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
18425102ee87STony Krowiak 
1843ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1844ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1845ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1846ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1847ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1848ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1849ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
18505102ee87STony Krowiak }
18515102ee87STony Krowiak 
18527d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
18537d43bafcSEugene (jno) Dvurechenski {
18547d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
18555e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
18567d43bafcSEugene (jno) Dvurechenski 	else
18577d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
18587d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
18597d43bafcSEugene (jno) Dvurechenski }
18607d43bafcSEugene (jno) Dvurechenski 
1861e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1862b0c632dbSHeiko Carstens {
186376a6dd72SDavid Hildenbrand 	gfp_t alloc_flags = GFP_KERNEL;
18649d8d5786SMichael Mueller 	int i, rc;
1865b0c632dbSHeiko Carstens 	char debug_name[16];
1866f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1867b0c632dbSHeiko Carstens 
1868e08b9637SCarsten Otte 	rc = -EINVAL;
1869e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1870e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1871e08b9637SCarsten Otte 		goto out_err;
1872e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1873e08b9637SCarsten Otte 		goto out_err;
1874e08b9637SCarsten Otte #else
1875e08b9637SCarsten Otte 	if (type)
1876e08b9637SCarsten Otte 		goto out_err;
1877e08b9637SCarsten Otte #endif
1878e08b9637SCarsten Otte 
1879b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1880b0c632dbSHeiko Carstens 	if (rc)
1881d89f5effSJan Kiszka 		goto out_err;
1882b0c632dbSHeiko Carstens 
1883b290411aSCarsten Otte 	rc = -ENOMEM;
1884b290411aSCarsten Otte 
18857d43bafcSEugene (jno) Dvurechenski 	kvm->arch.use_esca = 0; /* start with basic SCA */
188676a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
188776a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
18885e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
188976a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
1890b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1891d89f5effSJan Kiszka 		goto out_err;
1892f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
1893c5c2c393SDavid Hildenbrand 	sca_offset += 16;
1894bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
1895c5c2c393SDavid Hildenbrand 		sca_offset = 0;
1896bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
1897bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
1898f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
1899b0c632dbSHeiko Carstens 
1900b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
1901b0c632dbSHeiko Carstens 
19021cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
1903b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
190440f5b735SDominik Dingel 		goto out_err;
1905b0c632dbSHeiko Carstens 
1906c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
1907c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1908c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
190940f5b735SDominik Dingel 		goto out_err;
19109d8d5786SMichael Mueller 
1911fb5bf93fSMichael Mueller 	/* Populate the facility mask initially. */
1912c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
191304478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
19149d8d5786SMichael Mueller 	for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
19159d8d5786SMichael Mueller 		if (i < kvm_s390_fac_list_mask_size())
1916c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
19179d8d5786SMichael Mueller 		else
1918c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] = 0UL;
19199d8d5786SMichael Mueller 	}
19209d8d5786SMichael Mueller 
1921981467c9SMichael Mueller 	/* Populate the facility list initially. */
1922c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1923c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
1924981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1925981467c9SMichael Mueller 
19261935222dSDavid Hildenbrand 	/* we are always in czam mode - even on pre z14 machines */
19271935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
19281935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_list, 138);
19291935222dSDavid Hildenbrand 	/* we emulate STHYI in kvm */
193095ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
193195ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
19321bab1c02SClaudio Imbrenda 	if (MACHINE_HAS_TLB_GUEST) {
19331bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
19341bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_list, 147);
19351bab1c02SClaudio Imbrenda 	}
193695ca2cb5SJanosch Frank 
19379bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
193837c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
19399d8d5786SMichael Mueller 
1940c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
19415102ee87STony Krowiak 
194251978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
194351978393SFei Li 	kvm->arch.float_int.simm = 0;
194451978393SFei Li 	kvm->arch.float_int.nimm = 0;
1945ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
19466d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
19476d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
19488a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
1949a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
1950ba5c1e9bSCarsten Otte 
1951b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
195278f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
1953b0c632dbSHeiko Carstens 
1954e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
1955e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
1956a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
1957e08b9637SCarsten Otte 	} else {
195832e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
1959ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
196032e6b236SGuenther Hutzl 		else
1961ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
196232e6b236SGuenther Hutzl 						    sclp.hamax + 1);
19636ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
1964598841caSCarsten Otte 		if (!kvm->arch.gmap)
196540f5b735SDominik Dingel 			goto out_err;
19662c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
196724eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
1968e08b9637SCarsten Otte 	}
1969fa6b7fe9SCornelia Huck 
1970fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
197184223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
197272f25020SJason J. Herne 	kvm->arch.epoch = 0;
1973fa6b7fe9SCornelia Huck 
19748ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
1975a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
19768335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
19778ad35755SDavid Hildenbrand 
1978d89f5effSJan Kiszka 	return 0;
1979d89f5effSJan Kiszka out_err:
1980c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
198140f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
19827d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
198378f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
1984d89f5effSJan Kiszka 	return rc;
1985b0c632dbSHeiko Carstens }
1986b0c632dbSHeiko Carstens 
1987235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void)
1988235539b4SLuiz Capitulino {
1989235539b4SLuiz Capitulino 	return false;
1990235539b4SLuiz Capitulino }
1991235539b4SLuiz Capitulino 
1992235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
1993235539b4SLuiz Capitulino {
1994235539b4SLuiz Capitulino 	return 0;
1995235539b4SLuiz Capitulino }
1996235539b4SLuiz Capitulino 
1997d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1998d329c035SChristian Borntraeger {
1999d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2000ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
200167335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
20023c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
2003bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
2004a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
200527e0393fSCarsten Otte 
200627e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
20076ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
200827e0393fSCarsten Otte 
2009e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
2010b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
2011d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2012b31288faSKonstantin Weitz 
20136692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
2014b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
2015d329c035SChristian Borntraeger }
2016d329c035SChristian Borntraeger 
2017d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
2018d329c035SChristian Borntraeger {
2019d329c035SChristian Borntraeger 	unsigned int i;
2020988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
2021d329c035SChristian Borntraeger 
2022988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
2023988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
2024988a2caeSGleb Natapov 
2025988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
2026988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2027d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
2028988a2caeSGleb Natapov 
2029988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
2030988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
2031d329c035SChristian Borntraeger }
2032d329c035SChristian Borntraeger 
2033b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
2034b0c632dbSHeiko Carstens {
2035d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
20367d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
2037d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
2038c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
203927e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
20406ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
2041841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
204267335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
2043a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
2044190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state) {
2045190df4a2SClaudio Imbrenda 		vfree(kvm->arch.migration_state->pgste_bitmap);
2046190df4a2SClaudio Imbrenda 		kfree(kvm->arch.migration_state);
2047190df4a2SClaudio Imbrenda 	}
20488335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2049b0c632dbSHeiko Carstens }
2050b0c632dbSHeiko Carstens 
2051b0c632dbSHeiko Carstens /* Section: vcpu related */
2052dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2053b0c632dbSHeiko Carstens {
20546ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
205527e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
205627e0393fSCarsten Otte 		return -ENOMEM;
20572c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
2058dafd032aSDominik Dingel 
205927e0393fSCarsten Otte 	return 0;
206027e0393fSCarsten Otte }
206127e0393fSCarsten Otte 
2062a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2063a6e2f683SEugene (jno) Dvurechenski {
2064a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
2065a6940674SDavid Hildenbrand 		return;
20665e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
20677d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
20687d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
20697d43bafcSEugene (jno) Dvurechenski 
20707d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
20717d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
20727d43bafcSEugene (jno) Dvurechenski 	} else {
2073bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2074a6e2f683SEugene (jno) Dvurechenski 
2075a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2076a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
2077a6e2f683SEugene (jno) Dvurechenski 	}
20785e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
20797d43bafcSEugene (jno) Dvurechenski }
2080a6e2f683SEugene (jno) Dvurechenski 
2081eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2082a6e2f683SEugene (jno) Dvurechenski {
2083a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2084a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2085a6940674SDavid Hildenbrand 
2086a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
2087a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2088a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2089a6940674SDavid Hildenbrand 	}
2090eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
2091eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
2092eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
20937d43bafcSEugene (jno) Dvurechenski 
2094eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
20957d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
20967d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
20970c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2098eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
20997d43bafcSEugene (jno) Dvurechenski 	} else {
2100eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2101a6e2f683SEugene (jno) Dvurechenski 
2102eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2103a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2104a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2105eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2106a6e2f683SEugene (jno) Dvurechenski 	}
2107eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
21085e044315SEugene (jno) Dvurechenski }
21095e044315SEugene (jno) Dvurechenski 
21105e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
21115e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
21125e044315SEugene (jno) Dvurechenski {
21135e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
21145e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
21155e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
21165e044315SEugene (jno) Dvurechenski }
21175e044315SEugene (jno) Dvurechenski 
21185e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
21195e044315SEugene (jno) Dvurechenski {
21205e044315SEugene (jno) Dvurechenski 	int i;
21215e044315SEugene (jno) Dvurechenski 
21225e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
21235e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
21245e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
21255e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
21265e044315SEugene (jno) Dvurechenski }
21275e044315SEugene (jno) Dvurechenski 
21285e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
21295e044315SEugene (jno) Dvurechenski {
21305e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
21315e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
21325e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
21335e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
21345e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
21355e044315SEugene (jno) Dvurechenski 
21365e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
21375e044315SEugene (jno) Dvurechenski 	if (!new_sca)
21385e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
21395e044315SEugene (jno) Dvurechenski 
21405e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
21415e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
21425e044315SEugene (jno) Dvurechenski 
21435e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
21445e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
21455e044315SEugene (jno) Dvurechenski 
21465e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
21475e044315SEugene (jno) Dvurechenski 
21485e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
21495e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
21505e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
21510c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
21525e044315SEugene (jno) Dvurechenski 	}
21535e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
21545e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
21555e044315SEugene (jno) Dvurechenski 
21565e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
21575e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
21585e044315SEugene (jno) Dvurechenski 
21595e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
21605e044315SEugene (jno) Dvurechenski 
21618335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
21628335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
21635e044315SEugene (jno) Dvurechenski 	return 0;
21647d43bafcSEugene (jno) Dvurechenski }
2165a6e2f683SEugene (jno) Dvurechenski 
2166a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2167a6e2f683SEugene (jno) Dvurechenski {
21685e044315SEugene (jno) Dvurechenski 	int rc;
21695e044315SEugene (jno) Dvurechenski 
2170a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2171a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
2172a6940674SDavid Hildenbrand 			return true;
2173a6940674SDavid Hildenbrand 		return false;
2174a6940674SDavid Hildenbrand 	}
21755e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
21765e044315SEugene (jno) Dvurechenski 		return true;
217776a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
21785e044315SEugene (jno) Dvurechenski 		return false;
21795e044315SEugene (jno) Dvurechenski 
21805e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
21815e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
21825e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
21835e044315SEugene (jno) Dvurechenski 
21845e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2185a6e2f683SEugene (jno) Dvurechenski }
2186a6e2f683SEugene (jno) Dvurechenski 
2187dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
2188dafd032aSDominik Dingel {
2189dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
2190dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
219159674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
219259674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
21939eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
2194b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
2195b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
2196b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
219775a4615cSJulius Niedworok 	kvm_s390_set_prefix(vcpu, 0);
2198c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
2199c6e5f166SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
22004e0b1ab7SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 133))
22014e0b1ab7SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
2202f6aa6dc4SDavid Hildenbrand 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
2203f6aa6dc4SDavid Hildenbrand 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
2204f6aa6dc4SDavid Hildenbrand 	 */
2205f6aa6dc4SDavid Hildenbrand 	if (MACHINE_HAS_VX)
220668c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
22076fd8e67dSDavid Hildenbrand 	else
22086fd8e67dSDavid Hildenbrand 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
2209dafd032aSDominik Dingel 
2210dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
2211dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
2212dafd032aSDominik Dingel 
2213b0c632dbSHeiko Carstens 	return 0;
2214b0c632dbSHeiko Carstens }
2215b0c632dbSHeiko Carstens 
2216db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2217db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2218db0758b2SDavid Hildenbrand {
2219db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
22209c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2221db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
22229c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2223db0758b2SDavid Hildenbrand }
2224db0758b2SDavid Hildenbrand 
2225db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2226db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2227db0758b2SDavid Hildenbrand {
2228db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
22299c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2230db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2231db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
22329c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2233db0758b2SDavid Hildenbrand }
2234db0758b2SDavid Hildenbrand 
2235db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2236db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2237db0758b2SDavid Hildenbrand {
2238db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2239db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
2240db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
2241db0758b2SDavid Hildenbrand }
2242db0758b2SDavid Hildenbrand 
2243db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2244db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2245db0758b2SDavid Hildenbrand {
2246db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2247db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
2248db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
2249db0758b2SDavid Hildenbrand }
2250db0758b2SDavid Hildenbrand 
2251db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2252db0758b2SDavid Hildenbrand {
2253db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2254db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
2255db0758b2SDavid Hildenbrand 	preempt_enable();
2256db0758b2SDavid Hildenbrand }
2257db0758b2SDavid Hildenbrand 
2258db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2259db0758b2SDavid Hildenbrand {
2260db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2261db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
2262db0758b2SDavid Hildenbrand 	preempt_enable();
2263db0758b2SDavid Hildenbrand }
2264db0758b2SDavid Hildenbrand 
22654287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
22664287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
22674287f247SDavid Hildenbrand {
2268db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
22699c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2270db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
2271db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
22724287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
22739c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2274db0758b2SDavid Hildenbrand 	preempt_enable();
22754287f247SDavid Hildenbrand }
22764287f247SDavid Hildenbrand 
2277db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
22784287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
22794287f247SDavid Hildenbrand {
22809c23a131SDavid Hildenbrand 	unsigned int seq;
2281db0758b2SDavid Hildenbrand 	__u64 value;
2282db0758b2SDavid Hildenbrand 
2283db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
22844287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
2285db0758b2SDavid Hildenbrand 
22869c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
22879c23a131SDavid Hildenbrand 	do {
22889c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
22899c23a131SDavid Hildenbrand 		/*
22909c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
22919c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
22929c23a131SDavid Hildenbrand 		 */
22939c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
2294db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
22959c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
22969c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
2297db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
22989c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
22999c23a131SDavid Hildenbrand 	preempt_enable();
2300db0758b2SDavid Hildenbrand 	return value;
23014287f247SDavid Hildenbrand }
23024287f247SDavid Hildenbrand 
2303b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2304b0c632dbSHeiko Carstens {
23059977e886SHendrik Brueckner 
230637d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
2307805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
23085ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2309db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
231001a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
2311b0c632dbSHeiko Carstens }
2312b0c632dbSHeiko Carstens 
2313b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2314b0c632dbSHeiko Carstens {
231501a745acSDavid Hildenbrand 	vcpu->cpu = -1;
23165ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2317db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
2318805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
231937d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
232037d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
23219977e886SHendrik Brueckner 
2322b0c632dbSHeiko Carstens }
2323b0c632dbSHeiko Carstens 
2324b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
2325b0c632dbSHeiko Carstens {
2326b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
2327b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
2328b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
23298d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
23304287f247SDavid Hildenbrand 	kvm_s390_set_cpu_timer(vcpu, 0);
2331b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
2332b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
2333b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
2334b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
2335b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
23369abc2a08SDavid Hildenbrand 	/* make sure the new fpc will be lazily loaded */
23379abc2a08SDavid Hildenbrand 	save_fpu_regs();
23389abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = 0;
2339b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
2340672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
23413c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
23423c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
23436352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
23446852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
23452ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
2346b0c632dbSHeiko Carstens }
2347b0c632dbSHeiko Carstens 
234831928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
234942897d86SMarcelo Tosatti {
235072f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
2351fdf03650SFan Zhang 	preempt_disable();
235272f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
2353fdf03650SFan Zhang 	preempt_enable();
235472f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
235525508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
2356dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
2357eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
235825508824SDavid Hildenbrand 	}
23596502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
23606502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
236137d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
236237d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
236342897d86SMarcelo Tosatti }
236442897d86SMarcelo Tosatti 
23655102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
23665102ee87STony Krowiak {
23679d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
23685102ee87STony Krowiak 		return;
23695102ee87STony Krowiak 
2370a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
2371a374e892STony Krowiak 
2372a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
2373a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
2374a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
2375a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
2376a374e892STony Krowiak 
23775102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
23785102ee87STony Krowiak }
23795102ee87STony Krowiak 
2380b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
2381b31605c1SDominik Dingel {
2382b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
2383b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
2384b31605c1SDominik Dingel }
2385b31605c1SDominik Dingel 
2386b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
2387b31605c1SDominik Dingel {
2388b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
2389b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
2390b31605c1SDominik Dingel 		return -ENOMEM;
2391b31605c1SDominik Dingel 
23920c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->ecb2 &= ~ECB2_PFMFI;
2393b31605c1SDominik Dingel 	return 0;
2394b31605c1SDominik Dingel }
2395b31605c1SDominik Dingel 
239691520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
239791520f1aSMichael Mueller {
239891520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
239991520f1aSMichael Mueller 
240091520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
240180bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
2402c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
240391520f1aSMichael Mueller }
240491520f1aSMichael Mueller 
2405b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
2406b0c632dbSHeiko Carstens {
2407b31605c1SDominik Dingel 	int rc = 0;
2408b31288faSKonstantin Weitz 
24099e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
24109e6dabefSCornelia Huck 						    CPUSTAT_SM |
2411a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
2412a4a4f191SGuenther Hutzl 
241353df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
2414805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
241553df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
2416805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
2417a4a4f191SGuenther Hutzl 
241891520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
241991520f1aSMichael Mueller 
2420bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2421bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
24220c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
2423bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
24240c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
2425f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
24260c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
24277feb6bb8SMichael Mueller 
2428873b425eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi)
24290c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
2430cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
24310c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
24320c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
243348ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
24340c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
243511ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
24360c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
243737c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
24380c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
243937c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
24400c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
244118280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
24420c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
24430c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
244413211ea7SEric Farman 	}
24458fa1696eSCollin L. Walling 	if (test_kvm_facility(vcpu->kvm, 139))
24468fa1696eSCollin L. Walling 		vcpu->arch.sie_block->ecd |= ECD_MEF;
24478fa1696eSCollin L. Walling 
24484e0b1ab7SFan Zhang 	vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
24494e0b1ab7SFan Zhang 					| SDNXC;
2450c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
2451730cd632SFarhan Ali 
2452730cd632SFarhan Ali 	if (sclp.has_kss)
2453730cd632SFarhan Ali 		atomic_or(CPUSTAT_KSS, &vcpu->arch.sie_block->cpuflags);
2454730cd632SFarhan Ali 	else
2455492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
24565a5e6536SMatthew Rosato 
2457e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
2458b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
2459b31605c1SDominik Dingel 		if (rc)
2460b31605c1SDominik Dingel 			return rc;
2461b31288faSKonstantin Weitz 	}
24620ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2463ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
24649d8d5786SMichael Mueller 
24655102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
24665102ee87STony Krowiak 
2467b31605c1SDominik Dingel 	return rc;
2468b0c632dbSHeiko Carstens }
2469b0c632dbSHeiko Carstens 
2470b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2471b0c632dbSHeiko Carstens 				      unsigned int id)
2472b0c632dbSHeiko Carstens {
24734d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
24747feb6bb8SMichael Mueller 	struct sie_page *sie_page;
24754d47555aSCarsten Otte 	int rc = -EINVAL;
2476b0c632dbSHeiko Carstens 
24774215825eSDavid Hildenbrand 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
24784d47555aSCarsten Otte 		goto out;
24794d47555aSCarsten Otte 
24804d47555aSCarsten Otte 	rc = -ENOMEM;
24814d47555aSCarsten Otte 
2482b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2483b0c632dbSHeiko Carstens 	if (!vcpu)
24844d47555aSCarsten Otte 		goto out;
2485b0c632dbSHeiko Carstens 
2486da72ca4dSQingFeng Hao 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
24877feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
24887feb6bb8SMichael Mueller 	if (!sie_page)
2489b0c632dbSHeiko Carstens 		goto out_free_cpu;
2490b0c632dbSHeiko Carstens 
24917feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
24927feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
24937feb6bb8SMichael Mueller 
2494efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
2495efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
2496efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
2497efed1104SDavid Hildenbrand 
2498b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
2499ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
2500ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
2501d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
25025288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
25039c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
2504ba5c1e9bSCarsten Otte 
2505b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
2506b0c632dbSHeiko Carstens 	if (rc)
25079abc2a08SDavid Hildenbrand 		goto out_free_sie_block;
25088335713aSChristian Borntraeger 	VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2509b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
2510ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2511b0c632dbSHeiko Carstens 
2512b0c632dbSHeiko Carstens 	return vcpu;
25137b06bf2fSWei Yongjun out_free_sie_block:
25147b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
2515b0c632dbSHeiko Carstens out_free_cpu:
2516b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
25174d47555aSCarsten Otte out:
2518b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
2519b0c632dbSHeiko Carstens }
2520b0c632dbSHeiko Carstens 
2521b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2522b0c632dbSHeiko Carstens {
25239a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
2524b0c632dbSHeiko Carstens }
2525b0c632dbSHeiko Carstens 
2526199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
2527199b5763SLongpeng(Mike) {
25280546c63dSLongpeng(Mike) 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
2529199b5763SLongpeng(Mike) }
2530199b5763SLongpeng(Mike) 
253127406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
253249b99e1eSChristian Borntraeger {
2533805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
253461a6df54SDavid Hildenbrand 	exit_sie(vcpu);
253549b99e1eSChristian Borntraeger }
253649b99e1eSChristian Borntraeger 
253727406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
253849b99e1eSChristian Borntraeger {
2539805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
254049b99e1eSChristian Borntraeger }
254149b99e1eSChristian Borntraeger 
25428e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
25438e236546SChristian Borntraeger {
2544805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
254561a6df54SDavid Hildenbrand 	exit_sie(vcpu);
25468e236546SChristian Borntraeger }
25478e236546SChristian Borntraeger 
25488e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
25498e236546SChristian Borntraeger {
25509bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
25518e236546SChristian Borntraeger }
25528e236546SChristian Borntraeger 
255349b99e1eSChristian Borntraeger /*
255449b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
255549b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
255649b99e1eSChristian Borntraeger  * return immediately. */
255749b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
255849b99e1eSChristian Borntraeger {
2559805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
256049b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
256149b99e1eSChristian Borntraeger 		cpu_relax();
256249b99e1eSChristian Borntraeger }
256349b99e1eSChristian Borntraeger 
25648e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
25658e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
256649b99e1eSChristian Borntraeger {
25678e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
25688e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
256949b99e1eSChristian Borntraeger }
257049b99e1eSChristian Borntraeger 
2571414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
2572414d3b07SMartin Schwidefsky 			      unsigned long end)
25732c70fe44SChristian Borntraeger {
25742c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
25752c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
2576414d3b07SMartin Schwidefsky 	unsigned long prefix;
2577414d3b07SMartin Schwidefsky 	int i;
25782c70fe44SChristian Borntraeger 
257965d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
258065d0b0d4SDavid Hildenbrand 		return;
2581414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
2582414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
2583414d3b07SMartin Schwidefsky 		return;
25842c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
25852c70fe44SChristian Borntraeger 		/* match against both prefix pages */
2586414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
2587414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
2588414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
2589414d3b07SMartin Schwidefsky 				   start, end);
25908e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
25912c70fe44SChristian Borntraeger 		}
25922c70fe44SChristian Borntraeger 	}
25932c70fe44SChristian Borntraeger }
25942c70fe44SChristian Borntraeger 
2595b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
2596b6d33834SChristoffer Dall {
2597b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
2598b6d33834SChristoffer Dall 	BUG();
2599b6d33834SChristoffer Dall 	return 0;
2600b6d33834SChristoffer Dall }
2601b6d33834SChristoffer Dall 
260214eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
260314eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
260414eebd91SCarsten Otte {
260514eebd91SCarsten Otte 	int r = -EINVAL;
260614eebd91SCarsten Otte 
260714eebd91SCarsten Otte 	switch (reg->id) {
260829b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
260929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
261029b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
261129b7c71bSCarsten Otte 		break;
261229b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
261329b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
261429b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
261529b7c71bSCarsten Otte 		break;
261646a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
26174287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
261846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
261946a6dd1cSJason J. herne 		break;
262046a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
262146a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
262246a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
262346a6dd1cSJason J. herne 		break;
2624536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2625536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
2626536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2627536336c2SDominik Dingel 		break;
2628536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2629536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
2630536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2631536336c2SDominik Dingel 		break;
2632536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2633536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
2634536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2635536336c2SDominik Dingel 		break;
2636672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2637672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
2638672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2639672550fbSChristian Borntraeger 		break;
2640afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2641afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
2642afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2643afa45ff5SChristian Borntraeger 		break;
264414eebd91SCarsten Otte 	default:
264514eebd91SCarsten Otte 		break;
264614eebd91SCarsten Otte 	}
264714eebd91SCarsten Otte 
264814eebd91SCarsten Otte 	return r;
264914eebd91SCarsten Otte }
265014eebd91SCarsten Otte 
265114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
265214eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
265314eebd91SCarsten Otte {
265414eebd91SCarsten Otte 	int r = -EINVAL;
26554287f247SDavid Hildenbrand 	__u64 val;
265614eebd91SCarsten Otte 
265714eebd91SCarsten Otte 	switch (reg->id) {
265829b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
265929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
266029b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
266129b7c71bSCarsten Otte 		break;
266229b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
266329b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
266429b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
266529b7c71bSCarsten Otte 		break;
266646a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
26674287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
26684287f247SDavid Hildenbrand 		if (!r)
26694287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
267046a6dd1cSJason J. herne 		break;
267146a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
267246a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
267346a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
267446a6dd1cSJason J. herne 		break;
2675536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2676536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
2677536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
26789fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
26799fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2680536336c2SDominik Dingel 		break;
2681536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2682536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
2683536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2684536336c2SDominik Dingel 		break;
2685536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2686536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
2687536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2688536336c2SDominik Dingel 		break;
2689672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2690672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
2691672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2692672550fbSChristian Borntraeger 		break;
2693afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2694afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
2695afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2696afa45ff5SChristian Borntraeger 		break;
269714eebd91SCarsten Otte 	default:
269814eebd91SCarsten Otte 		break;
269914eebd91SCarsten Otte 	}
270014eebd91SCarsten Otte 
270114eebd91SCarsten Otte 	return r;
270214eebd91SCarsten Otte }
2703b6d33834SChristoffer Dall 
2704b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
2705b0c632dbSHeiko Carstens {
2706b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
2707b0c632dbSHeiko Carstens 	return 0;
2708b0c632dbSHeiko Carstens }
2709b0c632dbSHeiko Carstens 
2710b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2711b0c632dbSHeiko Carstens {
27125a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
2713b0c632dbSHeiko Carstens 	return 0;
2714b0c632dbSHeiko Carstens }
2715b0c632dbSHeiko Carstens 
2716b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2717b0c632dbSHeiko Carstens {
27185a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
2719b0c632dbSHeiko Carstens 	return 0;
2720b0c632dbSHeiko Carstens }
2721b0c632dbSHeiko Carstens 
2722b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2723b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2724b0c632dbSHeiko Carstens {
272559674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
2726b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
2727b0c632dbSHeiko Carstens 	return 0;
2728b0c632dbSHeiko Carstens }
2729b0c632dbSHeiko Carstens 
2730b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2731b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2732b0c632dbSHeiko Carstens {
273359674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
2734b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
2735b0c632dbSHeiko Carstens 	return 0;
2736b0c632dbSHeiko Carstens }
2737b0c632dbSHeiko Carstens 
2738b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2739b0c632dbSHeiko Carstens {
27404725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
27414725c860SMartin Schwidefsky 		return -EINVAL;
2742e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
27439abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2744a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
2745a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
27469abc2a08SDavid Hildenbrand 	else
2747a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
2748b0c632dbSHeiko Carstens 	return 0;
2749b0c632dbSHeiko Carstens }
2750b0c632dbSHeiko Carstens 
2751b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2752b0c632dbSHeiko Carstens {
27539abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
27549abc2a08SDavid Hildenbrand 	save_fpu_regs();
27559abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2756a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
2757a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
27589abc2a08SDavid Hildenbrand 	else
2759a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
2760e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
2761b0c632dbSHeiko Carstens 	return 0;
2762b0c632dbSHeiko Carstens }
2763b0c632dbSHeiko Carstens 
2764b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
2765b0c632dbSHeiko Carstens {
2766b0c632dbSHeiko Carstens 	int rc = 0;
2767b0c632dbSHeiko Carstens 
27687a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
2769b0c632dbSHeiko Carstens 		rc = -EBUSY;
2770d7b0b5ebSCarsten Otte 	else {
2771d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
2772d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
2773d7b0b5ebSCarsten Otte 	}
2774b0c632dbSHeiko Carstens 	return rc;
2775b0c632dbSHeiko Carstens }
2776b0c632dbSHeiko Carstens 
2777b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2778b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
2779b0c632dbSHeiko Carstens {
2780b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
2781b0c632dbSHeiko Carstens }
2782b0c632dbSHeiko Carstens 
278327291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
278427291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
278527291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
278627291e21SDavid Hildenbrand 
2787d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2788d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
2789b0c632dbSHeiko Carstens {
279027291e21SDavid Hildenbrand 	int rc = 0;
279127291e21SDavid Hildenbrand 
279227291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
279327291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
279427291e21SDavid Hildenbrand 
27952de3bfc2SDavid Hildenbrand 	if (dbg->control & ~VALID_GUESTDBG_FLAGS)
279627291e21SDavid Hildenbrand 		return -EINVAL;
279789b5b4deSDavid Hildenbrand 	if (!sclp.has_gpere)
279889b5b4deSDavid Hildenbrand 		return -EINVAL;
279927291e21SDavid Hildenbrand 
280027291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
280127291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
280227291e21SDavid Hildenbrand 		/* enforce guest PER */
2803805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
280427291e21SDavid Hildenbrand 
280527291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
280627291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
280727291e21SDavid Hildenbrand 	} else {
2808805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
280927291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
281027291e21SDavid Hildenbrand 	}
281127291e21SDavid Hildenbrand 
281227291e21SDavid Hildenbrand 	if (rc) {
281327291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
281427291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
2815805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
281627291e21SDavid Hildenbrand 	}
281727291e21SDavid Hildenbrand 
281827291e21SDavid Hildenbrand 	return rc;
2819b0c632dbSHeiko Carstens }
2820b0c632dbSHeiko Carstens 
282162d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
282262d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
282362d9f0dbSMarcelo Tosatti {
28246352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
28256352e4d2SDavid Hildenbrand 	return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
28266352e4d2SDavid Hildenbrand 				       KVM_MP_STATE_OPERATING;
282762d9f0dbSMarcelo Tosatti }
282862d9f0dbSMarcelo Tosatti 
282962d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
283062d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
283162d9f0dbSMarcelo Tosatti {
28326352e4d2SDavid Hildenbrand 	int rc = 0;
28336352e4d2SDavid Hildenbrand 
28346352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
28356352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
28366352e4d2SDavid Hildenbrand 
28376352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
28386352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
28396352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
28406352e4d2SDavid Hildenbrand 		break;
28416352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
28426352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
28436352e4d2SDavid Hildenbrand 		break;
28446352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
28456352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
28466352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
28476352e4d2SDavid Hildenbrand 	default:
28486352e4d2SDavid Hildenbrand 		rc = -ENXIO;
28496352e4d2SDavid Hildenbrand 	}
28506352e4d2SDavid Hildenbrand 
28516352e4d2SDavid Hildenbrand 	return rc;
285262d9f0dbSMarcelo Tosatti }
285362d9f0dbSMarcelo Tosatti 
28548ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
28558ad35755SDavid Hildenbrand {
28568ad35755SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
28578ad35755SDavid Hildenbrand }
28588ad35755SDavid Hildenbrand 
28592c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
28602c70fe44SChristian Borntraeger {
28618ad35755SDavid Hildenbrand retry:
28628e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
28632fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
2864586b7ccdSChristian Borntraeger 		return 0;
28652c70fe44SChristian Borntraeger 	/*
28662c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2867b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
28682c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
28692c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
28702c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
28712c70fe44SChristian Borntraeger 	 */
28728ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
28732c70fe44SChristian Borntraeger 		int rc;
2874b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
2875fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
2876b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
2877aca411a4SJulius Niedworok 		if (rc) {
2878aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
28792c70fe44SChristian Borntraeger 			return rc;
2880aca411a4SJulius Niedworok 		}
28818ad35755SDavid Hildenbrand 		goto retry;
28822c70fe44SChristian Borntraeger 	}
28838ad35755SDavid Hildenbrand 
2884d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2885d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
2886d3d692c8SDavid Hildenbrand 		goto retry;
2887d3d692c8SDavid Hildenbrand 	}
2888d3d692c8SDavid Hildenbrand 
28898ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
28908ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
28918ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
2892805de8f4SPeter Zijlstra 			atomic_or(CPUSTAT_IBS,
28938ad35755SDavid Hildenbrand 					&vcpu->arch.sie_block->cpuflags);
28948ad35755SDavid Hildenbrand 		}
28958ad35755SDavid Hildenbrand 		goto retry;
28968ad35755SDavid Hildenbrand 	}
28978ad35755SDavid Hildenbrand 
28988ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
28998ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
29008ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
2901805de8f4SPeter Zijlstra 			atomic_andnot(CPUSTAT_IBS,
29028ad35755SDavid Hildenbrand 					  &vcpu->arch.sie_block->cpuflags);
29038ad35755SDavid Hildenbrand 		}
29048ad35755SDavid Hildenbrand 		goto retry;
29058ad35755SDavid Hildenbrand 	}
29068ad35755SDavid Hildenbrand 
29076502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
29086502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
29096502a34cSDavid Hildenbrand 		goto retry;
29106502a34cSDavid Hildenbrand 	}
29116502a34cSDavid Hildenbrand 
2912190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
2913190df4a2SClaudio Imbrenda 		/*
2914190df4a2SClaudio Imbrenda 		 * Disable CMMA virtualization; we will emulate the ESSA
2915190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
2916190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
2917190df4a2SClaudio Imbrenda 		 */
2918190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
2919190df4a2SClaudio Imbrenda 		goto retry;
2920190df4a2SClaudio Imbrenda 	}
2921190df4a2SClaudio Imbrenda 
2922190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
2923190df4a2SClaudio Imbrenda 		/*
2924190df4a2SClaudio Imbrenda 		 * Re-enable CMMA virtualization if CMMA is available and
2925190df4a2SClaudio Imbrenda 		 * was used.
2926190df4a2SClaudio Imbrenda 		 */
2927190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
2928190df4a2SClaudio Imbrenda 		    (vcpu->kvm->mm->context.use_cmma))
2929190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
2930190df4a2SClaudio Imbrenda 		goto retry;
2931190df4a2SClaudio Imbrenda 	}
2932190df4a2SClaudio Imbrenda 
29330759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
293472875d8aSRadim Krčmář 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
29350759d068SDavid Hildenbrand 
29362c70fe44SChristian Borntraeger 	return 0;
29372c70fe44SChristian Borntraeger }
29382c70fe44SChristian Borntraeger 
29398fa1696eSCollin L. Walling void kvm_s390_set_tod_clock_ext(struct kvm *kvm,
29408fa1696eSCollin L. Walling 				 const struct kvm_s390_vm_tod_clock *gtod)
29418fa1696eSCollin L. Walling {
29428fa1696eSCollin L. Walling 	struct kvm_vcpu *vcpu;
29438fa1696eSCollin L. Walling 	struct kvm_s390_tod_clock_ext htod;
29448fa1696eSCollin L. Walling 	int i;
29458fa1696eSCollin L. Walling 
29468fa1696eSCollin L. Walling 	mutex_lock(&kvm->lock);
29478fa1696eSCollin L. Walling 	preempt_disable();
29488fa1696eSCollin L. Walling 
29498fa1696eSCollin L. Walling 	get_tod_clock_ext((char *)&htod);
29508fa1696eSCollin L. Walling 
29518fa1696eSCollin L. Walling 	kvm->arch.epoch = gtod->tod - htod.tod;
29528fa1696eSCollin L. Walling 	kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
29538fa1696eSCollin L. Walling 
29548fa1696eSCollin L. Walling 	if (kvm->arch.epoch > gtod->tod)
29558fa1696eSCollin L. Walling 		kvm->arch.epdx -= 1;
29568fa1696eSCollin L. Walling 
29578fa1696eSCollin L. Walling 	kvm_s390_vcpu_block_all(kvm);
29588fa1696eSCollin L. Walling 	kvm_for_each_vcpu(i, vcpu, kvm) {
29598fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
29608fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
29618fa1696eSCollin L. Walling 	}
29628fa1696eSCollin L. Walling 
29638fa1696eSCollin L. Walling 	kvm_s390_vcpu_unblock_all(kvm);
29648fa1696eSCollin L. Walling 	preempt_enable();
29658fa1696eSCollin L. Walling 	mutex_unlock(&kvm->lock);
29668fa1696eSCollin L. Walling }
29678fa1696eSCollin L. Walling 
296825ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
296925ed1675SDavid Hildenbrand {
297025ed1675SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
297125ed1675SDavid Hildenbrand 	int i;
297225ed1675SDavid Hildenbrand 
297325ed1675SDavid Hildenbrand 	mutex_lock(&kvm->lock);
297425ed1675SDavid Hildenbrand 	preempt_disable();
297525ed1675SDavid Hildenbrand 	kvm->arch.epoch = tod - get_tod_clock();
297625ed1675SDavid Hildenbrand 	kvm_s390_vcpu_block_all(kvm);
297725ed1675SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm)
297825ed1675SDavid Hildenbrand 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
297925ed1675SDavid Hildenbrand 	kvm_s390_vcpu_unblock_all(kvm);
298025ed1675SDavid Hildenbrand 	preempt_enable();
298125ed1675SDavid Hildenbrand 	mutex_unlock(&kvm->lock);
298225ed1675SDavid Hildenbrand }
298325ed1675SDavid Hildenbrand 
2984fa576c58SThomas Huth /**
2985fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
2986fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
2987fa576c58SThomas Huth  * @gpa: Guest physical address
2988fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
2989fa576c58SThomas Huth  *
2990fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
2991fa576c58SThomas Huth  *
2992fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
2993fa576c58SThomas Huth  */
2994fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
299524eb3a82SDominik Dingel {
2996527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
2997527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
299824eb3a82SDominik Dingel }
299924eb3a82SDominik Dingel 
30003c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
30013c038e6bSDominik Dingel 				      unsigned long token)
30023c038e6bSDominik Dingel {
30033c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
3004383d0b05SJens Freimann 	struct kvm_s390_irq irq;
30053c038e6bSDominik Dingel 
30063c038e6bSDominik Dingel 	if (start_token) {
3007383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
3008383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
3009383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
30103c038e6bSDominik Dingel 	} else {
30113c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
3012383d0b05SJens Freimann 		inti.parm64 = token;
30133c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
30143c038e6bSDominik Dingel 	}
30153c038e6bSDominik Dingel }
30163c038e6bSDominik Dingel 
30173c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
30183c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
30193c038e6bSDominik Dingel {
30203c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
30213c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
30223c038e6bSDominik Dingel }
30233c038e6bSDominik Dingel 
30243c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
30253c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
30263c038e6bSDominik Dingel {
30273c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
30283c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
30293c038e6bSDominik Dingel }
30303c038e6bSDominik Dingel 
30313c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
30323c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
30333c038e6bSDominik Dingel {
30343c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
30353c038e6bSDominik Dingel }
30363c038e6bSDominik Dingel 
30373c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
30383c038e6bSDominik Dingel {
30393c038e6bSDominik Dingel 	/*
30403c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
30413c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
30423c038e6bSDominik Dingel 	 */
30433c038e6bSDominik Dingel 	return true;
30443c038e6bSDominik Dingel }
30453c038e6bSDominik Dingel 
30463c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
30473c038e6bSDominik Dingel {
30483c038e6bSDominik Dingel 	hva_t hva;
30493c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
30503c038e6bSDominik Dingel 	int rc;
30513c038e6bSDominik Dingel 
30523c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
30533c038e6bSDominik Dingel 		return 0;
30543c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
30553c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
30563c038e6bSDominik Dingel 		return 0;
30573c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
30583c038e6bSDominik Dingel 		return 0;
30599a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
30603c038e6bSDominik Dingel 		return 0;
30613c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
30623c038e6bSDominik Dingel 		return 0;
30633c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
30643c038e6bSDominik Dingel 		return 0;
30653c038e6bSDominik Dingel 
306681480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
306781480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
306881480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
30693c038e6bSDominik Dingel 		return 0;
30703c038e6bSDominik Dingel 
30713c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
30723c038e6bSDominik Dingel 	return rc;
30733c038e6bSDominik Dingel }
30743c038e6bSDominik Dingel 
30753fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3076b0c632dbSHeiko Carstens {
30773fb4c40fSThomas Huth 	int rc, cpuflags;
3078e168bf8dSCarsten Otte 
30793c038e6bSDominik Dingel 	/*
30803c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
30813c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
30823c038e6bSDominik Dingel 	 * handled outside the worker.
30833c038e6bSDominik Dingel 	 */
30843c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
30853c038e6bSDominik Dingel 
30867ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
30877ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
3088b0c632dbSHeiko Carstens 
3089b0c632dbSHeiko Carstens 	if (need_resched())
3090b0c632dbSHeiko Carstens 		schedule();
3091b0c632dbSHeiko Carstens 
3092d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
309371cde587SChristian Borntraeger 		s390_handle_mcck();
309471cde587SChristian Borntraeger 
309579395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
309679395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
309779395031SJens Freimann 		if (rc)
309879395031SJens Freimann 			return rc;
309979395031SJens Freimann 	}
31000ff31867SCarsten Otte 
31012c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
31022c70fe44SChristian Borntraeger 	if (rc)
31032c70fe44SChristian Borntraeger 		return rc;
31042c70fe44SChristian Borntraeger 
310527291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
310627291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
310727291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
310827291e21SDavid Hildenbrand 	}
310927291e21SDavid Hildenbrand 
3110b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
31113fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
31123fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
31133fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
31142b29a9fdSDominik Dingel 
31153fb4c40fSThomas Huth 	return 0;
31163fb4c40fSThomas Huth }
31173fb4c40fSThomas Huth 
3118492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
3119492d8642SThomas Huth {
312056317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
312156317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
312256317920SDavid Hildenbrand 	};
312356317920SDavid Hildenbrand 	u8 opcode, ilen;
3124492d8642SThomas Huth 	int rc;
3125492d8642SThomas Huth 
3126492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
3127492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
3128492d8642SThomas Huth 
3129492d8642SThomas Huth 	/*
3130492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
3131492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
3132492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
3133492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
3134492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
3135492d8642SThomas Huth 	 * to be able to forward the PSW.
3136492d8642SThomas Huth 	 */
31373fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
313856317920SDavid Hildenbrand 	ilen = insn_length(opcode);
31399b0d721aSDavid Hildenbrand 	if (rc < 0) {
31409b0d721aSDavid Hildenbrand 		return rc;
31419b0d721aSDavid Hildenbrand 	} else if (rc) {
31429b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
31439b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
31449b0d721aSDavid Hildenbrand 		 * nullification if necessary.
31459b0d721aSDavid Hildenbrand 		 */
31469b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
31479b0d721aSDavid Hildenbrand 		ilen = 4;
31489b0d721aSDavid Hildenbrand 	}
314956317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
315056317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
315156317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
3152492d8642SThomas Huth }
3153492d8642SThomas Huth 
31543fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
31553fb4c40fSThomas Huth {
31564d62fcc0SQingFeng Hao 	struct mcck_volatile_info *mcck_info;
31574d62fcc0SQingFeng Hao 	struct sie_page *sie_page;
31584d62fcc0SQingFeng Hao 
31592b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
31602b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
31612b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
31622b29a9fdSDominik Dingel 
316327291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
316427291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
316527291e21SDavid Hildenbrand 
31667ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
31677ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
316871f116bfSDavid Hildenbrand 
31694d62fcc0SQingFeng Hao 	if (exit_reason == -EINTR) {
31704d62fcc0SQingFeng Hao 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
31714d62fcc0SQingFeng Hao 		sie_page = container_of(vcpu->arch.sie_block,
31724d62fcc0SQingFeng Hao 					struct sie_page, sie_block);
31734d62fcc0SQingFeng Hao 		mcck_info = &sie_page->mcck_info;
31744d62fcc0SQingFeng Hao 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
31754d62fcc0SQingFeng Hao 		return 0;
31764d62fcc0SQingFeng Hao 	}
31774d62fcc0SQingFeng Hao 
317871f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
317971f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
318071f116bfSDavid Hildenbrand 
318171f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
318271f116bfSDavid Hildenbrand 			return rc;
318371f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
318471f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
318571f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
318671f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
318771f116bfSDavid Hildenbrand 		return -EREMOTE;
318871f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
318971f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
319071f116bfSDavid Hildenbrand 		return 0;
3191210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
3192210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
3193210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
3194210b1607SThomas Huth 						current->thread.gmap_addr;
3195210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
319671f116bfSDavid Hildenbrand 		return -EREMOTE;
319724eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
31983c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
319924eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
320071f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
320171f116bfSDavid Hildenbrand 			return 0;
320271f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
3203fa576c58SThomas Huth 	}
320471f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
32053fb4c40fSThomas Huth }
32063fb4c40fSThomas Huth 
32073fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
32083fb4c40fSThomas Huth {
32093fb4c40fSThomas Huth 	int rc, exit_reason;
32103fb4c40fSThomas Huth 
3211800c1065SThomas Huth 	/*
3212800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
3213800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
3214800c1065SThomas Huth 	 */
3215800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3216800c1065SThomas Huth 
3217a76ccff6SThomas Huth 	do {
32183fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
32193fb4c40fSThomas Huth 		if (rc)
3220a76ccff6SThomas Huth 			break;
32213fb4c40fSThomas Huth 
3222800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
32233fb4c40fSThomas Huth 		/*
3224a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
3225a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
32263fb4c40fSThomas Huth 		 */
32270097d12eSChristian Borntraeger 		local_irq_disable();
32286edaa530SPaolo Bonzini 		guest_enter_irqoff();
3229db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
32300097d12eSChristian Borntraeger 		local_irq_enable();
3231a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
3232a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
32330097d12eSChristian Borntraeger 		local_irq_disable();
3234db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
32356edaa530SPaolo Bonzini 		guest_exit_irqoff();
32360097d12eSChristian Borntraeger 		local_irq_enable();
3237800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
32383fb4c40fSThomas Huth 
32393fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
324027291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
32413fb4c40fSThomas Huth 
3242800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
3243e168bf8dSCarsten Otte 	return rc;
3244b0c632dbSHeiko Carstens }
3245b0c632dbSHeiko Carstens 
3246b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3247b028ee3eSDavid Hildenbrand {
32484d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
32494e0b1ab7SFan Zhang 	struct gs_cb *gscb;
32504d5f2c04SChristian Borntraeger 
32514d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
32524e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
3253b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
3254b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
3255b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
3256b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
3257b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
3258b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
3259d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
3260d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3261b028ee3eSDavid Hildenbrand 	}
3262b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
32634287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
3264b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
3265b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
3266b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
3267b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
3268b028ee3eSDavid Hildenbrand 	}
3269b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
3270b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
3271b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
3272b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
32739fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
32749fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
3275b028ee3eSDavid Hildenbrand 	}
327680cd8763SFan Zhang 	/*
327780cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
327880cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
327980cd8763SFan Zhang 	 */
328080cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
32814d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
3282bb59c2daSAlice Frosi 	    riccb->v &&
32830c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
32844d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
32850c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
328680cd8763SFan Zhang 	}
32874e0b1ab7SFan Zhang 	/*
32884e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
32894e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
32904e0b1ab7SFan Zhang 	 */
32914e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
32924e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
32934e0b1ab7SFan Zhang 	    gscb->gssm &&
32944e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
32954e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
32964e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
32974e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
32984e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
329980cd8763SFan Zhang 	}
330031d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->arch.host_acrs);
330131d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
3302e1788bb9SChristian Borntraeger 	/* save host (userspace) fprs/vrs */
3303e1788bb9SChristian Borntraeger 	save_fpu_regs();
3304e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
3305e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
3306e1788bb9SChristian Borntraeger 	if (MACHINE_HAS_VX)
3307e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
3308e1788bb9SChristian Borntraeger 	else
3309e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
3310e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
3311e1788bb9SChristian Borntraeger 	if (test_fp_ctl(current->thread.fpu.fpc))
3312e1788bb9SChristian Borntraeger 		/* User space provided an invalid FPC, let's clear it */
3313e1788bb9SChristian Borntraeger 		current->thread.fpu.fpc = 0;
33144e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
33154e0b1ab7SFan Zhang 		preempt_disable();
33164e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
33174e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
33184e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
33194e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
33204e0b1ab7SFan Zhang 		}
33214e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
33224e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
33234e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
33244e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
33254e0b1ab7SFan Zhang 		}
33264e0b1ab7SFan Zhang 		preempt_enable();
33274e0b1ab7SFan Zhang 	}
332880cd8763SFan Zhang 
3329b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
3330b028ee3eSDavid Hildenbrand }
3331b028ee3eSDavid Hildenbrand 
3332b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3333b028ee3eSDavid Hildenbrand {
3334b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
3335b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
3336b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
3337b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
33384287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
3339b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
3340b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
3341b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
3342b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
3343b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
3344b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
3345b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
334631d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
334731d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->arch.host_acrs);
3348e1788bb9SChristian Borntraeger 	/* Save guest register state */
3349e1788bb9SChristian Borntraeger 	save_fpu_regs();
3350e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3351e1788bb9SChristian Borntraeger 	/* Restore will be done lazily at return */
3352e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
3353e1788bb9SChristian Borntraeger 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
33544e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
33554e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
33564e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
33574e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
33584e0b1ab7SFan Zhang 		preempt_disable();
33594e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
33604e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
33614e0b1ab7SFan Zhang 		preempt_enable();
33624e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
33634e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
33644e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
33654e0b1ab7SFan Zhang 	}
3366e1788bb9SChristian Borntraeger 
3367b028ee3eSDavid Hildenbrand }
3368b028ee3eSDavid Hildenbrand 
3369b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3370b0c632dbSHeiko Carstens {
33718f2abe6aSChristian Borntraeger 	int rc;
3372b0c632dbSHeiko Carstens 
3373460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
3374460df4c1SPaolo Bonzini 		return -EINTR;
3375460df4c1SPaolo Bonzini 
3376*accb757dSChristoffer Dall 	vcpu_load(vcpu);
3377*accb757dSChristoffer Dall 
337827291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
337927291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
3380*accb757dSChristoffer Dall 		rc = 0;
3381*accb757dSChristoffer Dall 		goto out;
338227291e21SDavid Hildenbrand 	}
338327291e21SDavid Hildenbrand 
338420b7035cSJan H. Schönherr 	kvm_sigset_activate(vcpu);
3385b0c632dbSHeiko Carstens 
33866352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
33876852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
33886352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
3389ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
33906352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
3391*accb757dSChristoffer Dall 		rc = -EINVAL;
3392*accb757dSChristoffer Dall 		goto out;
33936352e4d2SDavid Hildenbrand 	}
3394b0c632dbSHeiko Carstens 
3395b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
3396db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
3397d7b0b5ebSCarsten Otte 
3398dab4079dSHeiko Carstens 	might_fault();
3399e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
34009ace903dSChristian Ehrhardt 
3401b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
3402b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
34038f2abe6aSChristian Borntraeger 		rc = -EINTR;
3404b1d16c49SChristian Ehrhardt 	}
34058f2abe6aSChristian Borntraeger 
340627291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
340727291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
340827291e21SDavid Hildenbrand 		rc = 0;
340927291e21SDavid Hildenbrand 	}
341027291e21SDavid Hildenbrand 
34118f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
341271f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
34138f2abe6aSChristian Borntraeger 		rc = 0;
34148f2abe6aSChristian Borntraeger 	}
34158f2abe6aSChristian Borntraeger 
3416db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
3417b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
3418d7b0b5ebSCarsten Otte 
341920b7035cSJan H. Schönherr 	kvm_sigset_deactivate(vcpu);
3420b0c632dbSHeiko Carstens 
3421b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
3422*accb757dSChristoffer Dall out:
3423*accb757dSChristoffer Dall 	vcpu_put(vcpu);
34247e8e6ab4SHeiko Carstens 	return rc;
3425b0c632dbSHeiko Carstens }
3426b0c632dbSHeiko Carstens 
3427b0c632dbSHeiko Carstens /*
3428b0c632dbSHeiko Carstens  * store status at address
3429b0c632dbSHeiko Carstens  * we use have two special cases:
3430b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3431b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3432b0c632dbSHeiko Carstens  */
3433d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
3434b0c632dbSHeiko Carstens {
3435092670cdSCarsten Otte 	unsigned char archmode = 1;
34369abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
3437fda902cbSMichael Mueller 	unsigned int px;
34384287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
3439d0bce605SHeiko Carstens 	int rc;
3440b0c632dbSHeiko Carstens 
3441d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
3442d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
3443d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
3444b0c632dbSHeiko Carstens 			return -EFAULT;
3445d9a3a09aSMartin Schwidefsky 		gpa = 0;
3446d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
3447d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
3448b0c632dbSHeiko Carstens 			return -EFAULT;
3449d9a3a09aSMartin Schwidefsky 		gpa = px;
3450d9a3a09aSMartin Schwidefsky 	} else
3451d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
34529abc2a08SDavid Hildenbrand 
34539abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
34549abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
34559522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
3456d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
34579abc2a08SDavid Hildenbrand 				     fprs, 128);
34589abc2a08SDavid Hildenbrand 	} else {
34599abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
34606fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
34619abc2a08SDavid Hildenbrand 	}
3462d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
3463d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
3464d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
3465d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
3466d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
3467fda902cbSMichael Mueller 			      &px, 4);
3468d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
34699abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
3470d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
3471d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
34724287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
3473d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
34744287f247SDavid Hildenbrand 			      &cputm, 8);
3475178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
3476d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
3477d0bce605SHeiko Carstens 			      &clkcomp, 8);
3478d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
3479d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
3480d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
3481d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
3482d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
3483b0c632dbSHeiko Carstens }
3484b0c632dbSHeiko Carstens 
3485e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
3486e879892cSThomas Huth {
3487e879892cSThomas Huth 	/*
3488e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
348931d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
3490e879892cSThomas Huth 	 * it into the save area
3491e879892cSThomas Huth 	 */
3492d0164ee2SHendrik Brueckner 	save_fpu_regs();
34939abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3494e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
3495e879892cSThomas Huth 
3496e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
3497e879892cSThomas Huth }
3498e879892cSThomas Huth 
34998ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
35008ad35755SDavid Hildenbrand {
35018ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
35028e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
35038ad35755SDavid Hildenbrand }
35048ad35755SDavid Hildenbrand 
35058ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
35068ad35755SDavid Hildenbrand {
35078ad35755SDavid Hildenbrand 	unsigned int i;
35088ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
35098ad35755SDavid Hildenbrand 
35108ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
35118ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
35128ad35755SDavid Hildenbrand 	}
35138ad35755SDavid Hildenbrand }
35148ad35755SDavid Hildenbrand 
35158ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
35168ad35755SDavid Hildenbrand {
351709a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
351809a400e7SDavid Hildenbrand 		return;
35198ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
35208e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
35218ad35755SDavid Hildenbrand }
35228ad35755SDavid Hildenbrand 
35236852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
35246852d7b6SDavid Hildenbrand {
35258ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
35268ad35755SDavid Hildenbrand 
35278ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
35288ad35755SDavid Hildenbrand 		return;
35298ad35755SDavid Hildenbrand 
35306852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
35318ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3532433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
35338ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
35348ad35755SDavid Hildenbrand 
35358ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
35368ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
35378ad35755SDavid Hildenbrand 			started_vcpus++;
35388ad35755SDavid Hildenbrand 	}
35398ad35755SDavid Hildenbrand 
35408ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
35418ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
35428ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
35438ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
35448ad35755SDavid Hildenbrand 		/*
35458ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
35468ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
35478ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
35488ad35755SDavid Hildenbrand 		 */
35498ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
35508ad35755SDavid Hildenbrand 	}
35518ad35755SDavid Hildenbrand 
3552805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
35538ad35755SDavid Hildenbrand 	/*
35548ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
35558ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
35568ad35755SDavid Hildenbrand 	 */
3557d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3558433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
35598ad35755SDavid Hildenbrand 	return;
35606852d7b6SDavid Hildenbrand }
35616852d7b6SDavid Hildenbrand 
35626852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
35636852d7b6SDavid Hildenbrand {
35648ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
35658ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
35668ad35755SDavid Hildenbrand 
35678ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
35688ad35755SDavid Hildenbrand 		return;
35698ad35755SDavid Hildenbrand 
35706852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
35718ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3572433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
35738ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
35748ad35755SDavid Hildenbrand 
357532f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
35766cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
357732f5ff63SDavid Hildenbrand 
3578805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
35798ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
35808ad35755SDavid Hildenbrand 
35818ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
35828ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
35838ad35755SDavid Hildenbrand 			started_vcpus++;
35848ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
35858ad35755SDavid Hildenbrand 		}
35868ad35755SDavid Hildenbrand 	}
35878ad35755SDavid Hildenbrand 
35888ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
35898ad35755SDavid Hildenbrand 		/*
35908ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
35918ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
35928ad35755SDavid Hildenbrand 		 */
35938ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
35948ad35755SDavid Hildenbrand 	}
35958ad35755SDavid Hildenbrand 
3596433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
35978ad35755SDavid Hildenbrand 	return;
35986852d7b6SDavid Hildenbrand }
35996852d7b6SDavid Hildenbrand 
3600d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3601d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
3602d6712df9SCornelia Huck {
3603d6712df9SCornelia Huck 	int r;
3604d6712df9SCornelia Huck 
3605d6712df9SCornelia Huck 	if (cap->flags)
3606d6712df9SCornelia Huck 		return -EINVAL;
3607d6712df9SCornelia Huck 
3608d6712df9SCornelia Huck 	switch (cap->cap) {
3609fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
3610fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
3611fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
3612c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
3613fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
3614fa6b7fe9SCornelia Huck 		}
3615fa6b7fe9SCornelia Huck 		r = 0;
3616fa6b7fe9SCornelia Huck 		break;
3617d6712df9SCornelia Huck 	default:
3618d6712df9SCornelia Huck 		r = -EINVAL;
3619d6712df9SCornelia Huck 		break;
3620d6712df9SCornelia Huck 	}
3621d6712df9SCornelia Huck 	return r;
3622d6712df9SCornelia Huck }
3623d6712df9SCornelia Huck 
362441408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
362541408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
362641408c28SThomas Huth {
362741408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
362841408c28SThomas Huth 	void *tmpbuf = NULL;
362941408c28SThomas Huth 	int r, srcu_idx;
363041408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
363141408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
363241408c28SThomas Huth 
363341408c28SThomas Huth 	if (mop->flags & ~supported_flags)
363441408c28SThomas Huth 		return -EINVAL;
363541408c28SThomas Huth 
363641408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
363741408c28SThomas Huth 		return -E2BIG;
363841408c28SThomas Huth 
363941408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
364041408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
364141408c28SThomas Huth 		if (!tmpbuf)
364241408c28SThomas Huth 			return -ENOMEM;
364341408c28SThomas Huth 	}
364441408c28SThomas Huth 
364541408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
364641408c28SThomas Huth 
364741408c28SThomas Huth 	switch (mop->op) {
364841408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
364941408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
365092c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
365192c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
365241408c28SThomas Huth 			break;
365341408c28SThomas Huth 		}
365441408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
365541408c28SThomas Huth 		if (r == 0) {
365641408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
365741408c28SThomas Huth 				r = -EFAULT;
365841408c28SThomas Huth 		}
365941408c28SThomas Huth 		break;
366041408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
366141408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
366292c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
366392c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
366441408c28SThomas Huth 			break;
366541408c28SThomas Huth 		}
366641408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
366741408c28SThomas Huth 			r = -EFAULT;
366841408c28SThomas Huth 			break;
366941408c28SThomas Huth 		}
367041408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
367141408c28SThomas Huth 		break;
367241408c28SThomas Huth 	default:
367341408c28SThomas Huth 		r = -EINVAL;
367441408c28SThomas Huth 	}
367541408c28SThomas Huth 
367641408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
367741408c28SThomas Huth 
367841408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
367941408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
368041408c28SThomas Huth 
368141408c28SThomas Huth 	vfree(tmpbuf);
368241408c28SThomas Huth 	return r;
368341408c28SThomas Huth }
368441408c28SThomas Huth 
3685b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
3686b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
3687b0c632dbSHeiko Carstens {
3688b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
3689b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
3690800c1065SThomas Huth 	int idx;
3691bc923cc9SAvi Kivity 	long r;
3692b0c632dbSHeiko Carstens 
369393736624SAvi Kivity 	switch (ioctl) {
369447b43c52SJens Freimann 	case KVM_S390_IRQ: {
369547b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
369647b43c52SJens Freimann 
369747b43c52SJens Freimann 		r = -EFAULT;
369847b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
369947b43c52SJens Freimann 			break;
370047b43c52SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
370147b43c52SJens Freimann 		break;
370247b43c52SJens Freimann 	}
370393736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
3704ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
3705383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
3706ba5c1e9bSCarsten Otte 
370793736624SAvi Kivity 		r = -EFAULT;
3708ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
370993736624SAvi Kivity 			break;
3710383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
3711383d0b05SJens Freimann 			return -EINVAL;
3712383d0b05SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
371393736624SAvi Kivity 		break;
3714ba5c1e9bSCarsten Otte 	}
3715b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
3716800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
3717bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
3718800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
3719bc923cc9SAvi Kivity 		break;
3720b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
3721b0c632dbSHeiko Carstens 		psw_t psw;
3722b0c632dbSHeiko Carstens 
3723bc923cc9SAvi Kivity 		r = -EFAULT;
3724b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
3725bc923cc9SAvi Kivity 			break;
3726bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
3727bc923cc9SAvi Kivity 		break;
3728b0c632dbSHeiko Carstens 	}
3729b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
3730bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3731bc923cc9SAvi Kivity 		break;
373214eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
373314eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
373414eebd91SCarsten Otte 		struct kvm_one_reg reg;
373514eebd91SCarsten Otte 		r = -EFAULT;
373614eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
373714eebd91SCarsten Otte 			break;
373814eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
373914eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
374014eebd91SCarsten Otte 		else
374114eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
374214eebd91SCarsten Otte 		break;
374314eebd91SCarsten Otte 	}
374427e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
374527e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
374627e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
374727e0393fSCarsten Otte 
374827e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
374927e0393fSCarsten Otte 			r = -EFAULT;
375027e0393fSCarsten Otte 			break;
375127e0393fSCarsten Otte 		}
375227e0393fSCarsten Otte 
375327e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
375427e0393fSCarsten Otte 			r = -EINVAL;
375527e0393fSCarsten Otte 			break;
375627e0393fSCarsten Otte 		}
375727e0393fSCarsten Otte 
375827e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
375927e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
376027e0393fSCarsten Otte 		break;
376127e0393fSCarsten Otte 	}
376227e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
376327e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
376427e0393fSCarsten Otte 
376527e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
376627e0393fSCarsten Otte 			r = -EFAULT;
376727e0393fSCarsten Otte 			break;
376827e0393fSCarsten Otte 		}
376927e0393fSCarsten Otte 
377027e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
377127e0393fSCarsten Otte 			r = -EINVAL;
377227e0393fSCarsten Otte 			break;
377327e0393fSCarsten Otte 		}
377427e0393fSCarsten Otte 
377527e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
377627e0393fSCarsten Otte 			ucasmap.length);
377727e0393fSCarsten Otte 		break;
377827e0393fSCarsten Otte 	}
377927e0393fSCarsten Otte #endif
3780ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
3781527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
3782ccc7910fSCarsten Otte 		break;
3783ccc7910fSCarsten Otte 	}
3784d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
3785d6712df9SCornelia Huck 	{
3786d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
3787d6712df9SCornelia Huck 		r = -EFAULT;
3788d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
3789d6712df9SCornelia Huck 			break;
3790d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3791d6712df9SCornelia Huck 		break;
3792d6712df9SCornelia Huck 	}
379341408c28SThomas Huth 	case KVM_S390_MEM_OP: {
379441408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
379541408c28SThomas Huth 
379641408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
379741408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
379841408c28SThomas Huth 		else
379941408c28SThomas Huth 			r = -EFAULT;
380041408c28SThomas Huth 		break;
380141408c28SThomas Huth 	}
3802816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
3803816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3804816c7667SJens Freimann 
3805816c7667SJens Freimann 		r = -EFAULT;
3806816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3807816c7667SJens Freimann 			break;
3808816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
3809816c7667SJens Freimann 		    irq_state.len == 0 ||
3810816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
3811816c7667SJens Freimann 			r = -EINVAL;
3812816c7667SJens Freimann 			break;
3813816c7667SJens Freimann 		}
3814bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
3815816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
3816816c7667SJens Freimann 					   (void __user *) irq_state.buf,
3817816c7667SJens Freimann 					   irq_state.len);
3818816c7667SJens Freimann 		break;
3819816c7667SJens Freimann 	}
3820816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
3821816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3822816c7667SJens Freimann 
3823816c7667SJens Freimann 		r = -EFAULT;
3824816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3825816c7667SJens Freimann 			break;
3826816c7667SJens Freimann 		if (irq_state.len == 0) {
3827816c7667SJens Freimann 			r = -EINVAL;
3828816c7667SJens Freimann 			break;
3829816c7667SJens Freimann 		}
3830bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
3831816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
3832816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
3833816c7667SJens Freimann 					   irq_state.len);
3834816c7667SJens Freimann 		break;
3835816c7667SJens Freimann 	}
3836b0c632dbSHeiko Carstens 	default:
38373e6afcf1SCarsten Otte 		r = -ENOTTY;
3838b0c632dbSHeiko Carstens 	}
3839bc923cc9SAvi Kivity 	return r;
3840b0c632dbSHeiko Carstens }
3841b0c632dbSHeiko Carstens 
38425b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
38435b1c1493SCarsten Otte {
38445b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
38455b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
38465b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
38475b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
38485b1c1493SCarsten Otte 		get_page(vmf->page);
38495b1c1493SCarsten Otte 		return 0;
38505b1c1493SCarsten Otte 	}
38515b1c1493SCarsten Otte #endif
38525b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
38535b1c1493SCarsten Otte }
38545b1c1493SCarsten Otte 
38555587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
38565587027cSAneesh Kumar K.V 			    unsigned long npages)
3857db3fe4ebSTakuya Yoshikawa {
3858db3fe4ebSTakuya Yoshikawa 	return 0;
3859db3fe4ebSTakuya Yoshikawa }
3860db3fe4ebSTakuya Yoshikawa 
3861b0c632dbSHeiko Carstens /* Section: memory related */
3862f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
3863f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
386409170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
38657b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
3866b0c632dbSHeiko Carstens {
3867dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
3868dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
3869dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
3870dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
3871b0c632dbSHeiko Carstens 
3872598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
3873b0c632dbSHeiko Carstens 		return -EINVAL;
3874b0c632dbSHeiko Carstens 
3875598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
3876b0c632dbSHeiko Carstens 		return -EINVAL;
3877b0c632dbSHeiko Carstens 
3878a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
3879a3a92c31SDominik Dingel 		return -EINVAL;
3880a3a92c31SDominik Dingel 
3881f7784b8eSMarcelo Tosatti 	return 0;
3882f7784b8eSMarcelo Tosatti }
3883f7784b8eSMarcelo Tosatti 
3884f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
388509170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
38868482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
3887f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
38888482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
3889f7784b8eSMarcelo Tosatti {
3890f7850c92SCarsten Otte 	int rc;
3891f7784b8eSMarcelo Tosatti 
38922cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
38932cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
38942cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
38952cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
38962cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
38972cef4debSChristian Borntraeger 	 */
38982cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
38992cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
39002cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
39012cef4debSChristian Borntraeger 		return;
3902598841caSCarsten Otte 
3903598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
3904598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
3905598841caSCarsten Otte 	if (rc)
3906ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
3907598841caSCarsten Otte 	return;
3908b0c632dbSHeiko Carstens }
3909b0c632dbSHeiko Carstens 
391060a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
391160a37709SAlexander Yarygin {
391260a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
391360a37709SAlexander Yarygin 
391460a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
391560a37709SAlexander Yarygin }
391660a37709SAlexander Yarygin 
39173491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
39183491caf2SChristian Borntraeger {
39193491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
39203491caf2SChristian Borntraeger }
39213491caf2SChristian Borntraeger 
3922b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
3923b0c632dbSHeiko Carstens {
392460a37709SAlexander Yarygin 	int i;
392560a37709SAlexander Yarygin 
392607197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
392707197fd0SDavid Hildenbrand 		pr_info("SIE not available\n");
392807197fd0SDavid Hildenbrand 		return -ENODEV;
392907197fd0SDavid Hildenbrand 	}
393007197fd0SDavid Hildenbrand 
393160a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
393260a37709SAlexander Yarygin 		kvm_s390_fac_list_mask[i] |=
393360a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
393460a37709SAlexander Yarygin 
39359d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
3936b0c632dbSHeiko Carstens }
3937b0c632dbSHeiko Carstens 
3938b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
3939b0c632dbSHeiko Carstens {
3940b0c632dbSHeiko Carstens 	kvm_exit();
3941b0c632dbSHeiko Carstens }
3942b0c632dbSHeiko Carstens 
3943b0c632dbSHeiko Carstens module_init(kvm_s390_init);
3944b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
3945566af940SCornelia Huck 
3946566af940SCornelia Huck /*
3947566af940SCornelia Huck  * Enable autoloading of the kvm module.
3948566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3949566af940SCornelia Huck  * since x86 takes a different approach.
3950566af940SCornelia Huck  */
3951566af940SCornelia Huck #include <linux/miscdevice.h>
3952566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
3953566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
3954