xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision 190df4a212a708fdd18f6cabfdd82594c91fdf25)
1b0c632dbSHeiko Carstens /*
2a53c8fabSHeiko Carstens  * hosting zSeries kernel virtual machines
3b0c632dbSHeiko Carstens  *
4628eb9b8SChristian Ehrhardt  * Copyright IBM Corp. 2008, 2009
5b0c632dbSHeiko Carstens  *
6b0c632dbSHeiko Carstens  * This program is free software; you can redistribute it and/or modify
7b0c632dbSHeiko Carstens  * it under the terms of the GNU General Public License (version 2 only)
8b0c632dbSHeiko Carstens  * as published by the Free Software Foundation.
9b0c632dbSHeiko Carstens  *
10b0c632dbSHeiko Carstens  *    Author(s): Carsten Otte <cotte@de.ibm.com>
11b0c632dbSHeiko Carstens  *               Christian Borntraeger <borntraeger@de.ibm.com>
12b0c632dbSHeiko Carstens  *               Heiko Carstens <heiko.carstens@de.ibm.com>
13628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
1415f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
15b0c632dbSHeiko Carstens  */
16b0c632dbSHeiko Carstens 
17b0c632dbSHeiko Carstens #include <linux/compiler.h>
18b0c632dbSHeiko Carstens #include <linux/err.h>
19b0c632dbSHeiko Carstens #include <linux/fs.h>
20ca872302SChristian Borntraeger #include <linux/hrtimer.h>
21b0c632dbSHeiko Carstens #include <linux/init.h>
22b0c632dbSHeiko Carstens #include <linux/kvm.h>
23b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
24b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
25b0c632dbSHeiko Carstens #include <linux/module.h>
26d3217967SPaul Gortmaker #include <linux/moduleparam.h>
27a374e892STony Krowiak #include <linux/random.h>
28b0c632dbSHeiko Carstens #include <linux/slab.h>
29ba5c1e9bSCarsten Otte #include <linux/timer.h>
3041408c28SThomas Huth #include <linux/vmalloc.h>
3115c9705fSDavid Hildenbrand #include <linux/bitmap.h>
32174cd4b1SIngo Molnar #include <linux/sched/signal.h>
33174cd4b1SIngo Molnar 
34*190df4a2SClaudio Imbrenda #include <linux/string.h>
35cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
36b0c632dbSHeiko Carstens #include <asm/lowcore.h>
37fd5ada04SMartin Schwidefsky #include <asm/stp.h>
38b0c632dbSHeiko Carstens #include <asm/pgtable.h>
391e133ab2SMartin Schwidefsky #include <asm/gmap.h>
40f5daba1dSHeiko Carstens #include <asm/nmi.h>
41a0616cdeSDavid Howells #include <asm/switch_to.h>
426d3da241SJens Freimann #include <asm/isc.h>
431526bf9cSChristian Borntraeger #include <asm/sclp.h>
440a763c78SDavid Hildenbrand #include <asm/cpacf.h>
45221bb8a4SLinus Torvalds #include <asm/timex.h>
468f2abe6aSChristian Borntraeger #include "kvm-s390.h"
47b0c632dbSHeiko Carstens #include "gaccess.h"
48b0c632dbSHeiko Carstens 
49ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
50ea2cdd27SDavid Hildenbrand #undef pr_fmt
51ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
52ea2cdd27SDavid Hildenbrand 
535786fffaSCornelia Huck #define CREATE_TRACE_POINTS
545786fffaSCornelia Huck #include "trace.h"
55ade38c31SCornelia Huck #include "trace-s390.h"
565786fffaSCornelia Huck 
5741408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
58816c7667SJens Freimann #define LOCAL_IRQS 32
59816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
60816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
6141408c28SThomas Huth 
62b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
63b0c632dbSHeiko Carstens 
64b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
65b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
660eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
678f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
688f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
698f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
708f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
71ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
729ec6de19SAlexander Yarygin 	{ "exit_pei", VCPU_STAT(exit_pei) },
73ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
74ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
75a011eeb2SJanosch Frank 	{ "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
76f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
7762bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
783491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
79ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
80f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
81ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
82aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
83aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
84ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
857697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
86ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
87ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
88ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
89ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
90ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
91ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
92ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
9369d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
94453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
95453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
96453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
97453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
98453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
998a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
100453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
101453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
102b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
103453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
104453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
105bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
10695ca2cb5SJanosch Frank 	{ "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
107a3508fbeSDavid Hildenbrand 	{ "instruction_sie", VCPU_STAT(instruction_sie) },
1085288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
109bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
1107697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
1115288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
11242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
11342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1145288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
11542cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
11642cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
117cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1185288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1195288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1205288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
12142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
12242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
12342cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
124388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
125e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
12641628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
127175a5c9eSChristian Borntraeger 	{ "diagnose_258", VCPU_STAT(diagnose_258) },
128175a5c9eSChristian Borntraeger 	{ "diagnose_308", VCPU_STAT(diagnose_308) },
129175a5c9eSChristian Borntraeger 	{ "diagnose_500", VCPU_STAT(diagnose_500) },
130b0c632dbSHeiko Carstens 	{ NULL }
131b0c632dbSHeiko Carstens };
132b0c632dbSHeiko Carstens 
133a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
134a411edf1SDavid Hildenbrand static int nested;
135a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
136a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
137a411edf1SDavid Hildenbrand 
1389d8d5786SMichael Mueller /* upper facilities limit for kvm */
139f6c1d359SHeiko Carstens unsigned long kvm_s390_fac_list_mask[16] = { FACILITIES_KVM };
140b0c632dbSHeiko Carstens 
1419d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void)
14278c4b59fSMichael Mueller {
1439d8d5786SMichael Mueller 	BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
1449d8d5786SMichael Mueller 	return ARRAY_SIZE(kvm_s390_fac_list_mask);
14578c4b59fSMichael Mueller }
14678c4b59fSMichael Mueller 
14715c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
14815c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1490a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
1500a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
15115c9705fSDavid Hildenbrand 
1529d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
153a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
15478f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
1559d8d5786SMichael Mueller 
156b0c632dbSHeiko Carstens /* Section: not file related */
15713a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
158b0c632dbSHeiko Carstens {
159b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
16010474ae8SAlexander Graf 	return 0;
161b0c632dbSHeiko Carstens }
162b0c632dbSHeiko Carstens 
163414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
164414d3b07SMartin Schwidefsky 			      unsigned long end);
1652c70fe44SChristian Borntraeger 
166fdf03650SFan Zhang /*
167fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
168fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
169fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
170fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
171fdf03650SFan Zhang  */
172fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
173fdf03650SFan Zhang 			  void *v)
174fdf03650SFan Zhang {
175fdf03650SFan Zhang 	struct kvm *kvm;
176fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
177fdf03650SFan Zhang 	int i;
178fdf03650SFan Zhang 	unsigned long long *delta = v;
179fdf03650SFan Zhang 
180fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
181fdf03650SFan Zhang 		kvm->arch.epoch -= *delta;
182fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
183fdf03650SFan Zhang 			vcpu->arch.sie_block->epoch -= *delta;
184db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
185db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
18691473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
18791473b48SDavid Hildenbrand 				vcpu->arch.vsie_block->epoch -= *delta;
188fdf03650SFan Zhang 		}
189fdf03650SFan Zhang 	}
190fdf03650SFan Zhang 	return NOTIFY_OK;
191fdf03650SFan Zhang }
192fdf03650SFan Zhang 
193fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
194fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
195fdf03650SFan Zhang };
196fdf03650SFan Zhang 
197b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
198b0c632dbSHeiko Carstens {
1992c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
200b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
201a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
202a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
203fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
204fdf03650SFan Zhang 				       &kvm_clock_notifier);
205b0c632dbSHeiko Carstens 	return 0;
206b0c632dbSHeiko Carstens }
207b0c632dbSHeiko Carstens 
208b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
209b0c632dbSHeiko Carstens {
210b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
211a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
212fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
213fdf03650SFan Zhang 					 &kvm_clock_notifier);
214b0c632dbSHeiko Carstens }
215b0c632dbSHeiko Carstens 
21622be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
21722be5a13SDavid Hildenbrand {
21822be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
21922be5a13SDavid Hildenbrand }
22022be5a13SDavid Hildenbrand 
2210a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
2220a763c78SDavid Hildenbrand {
2230a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
224d051ae53SHeiko Carstens 	int cc;
2250a763c78SDavid Hildenbrand 
2260a763c78SDavid Hildenbrand 	asm volatile(
2270a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
2280a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
2290a763c78SDavid Hildenbrand 		"	ipm	%0\n"
2300a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
2310a763c78SDavid Hildenbrand 		: "=d" (cc)
2320a763c78SDavid Hildenbrand 		: "d" (r0)
2330a763c78SDavid Hildenbrand 		: "cc");
2340a763c78SDavid Hildenbrand 	return cc == 0;
2350a763c78SDavid Hildenbrand }
2360a763c78SDavid Hildenbrand 
23722be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
23822be5a13SDavid Hildenbrand {
2390a763c78SDavid Hildenbrand 	int i;
2400a763c78SDavid Hildenbrand 
2410a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
2420a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
2430a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
2440a763c78SDavid Hildenbrand 	}
2450a763c78SDavid Hildenbrand 
2460a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
247221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
248221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
249221bb8a4SLinus Torvalds 		     PTFF_QAF);
2500a763c78SDavid Hildenbrand 
2510a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
25269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
25369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
25469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
25569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
25669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
25769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
25869c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
25969c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
26069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
26169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
2620a763c78SDavid Hildenbrand 	}
2630a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
26469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
26569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
2660a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
26769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
26869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
26969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
27069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
27169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
27269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
27369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
27469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
2750a763c78SDavid Hildenbrand 	}
2760a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
277985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
27869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
2790a763c78SDavid Hildenbrand 
280e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
281e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
282e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
283e000b8e0SJason J. Herne 
28422be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
28522be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
286a3508fbeSDavid Hildenbrand 	/*
287a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
288a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
289a3508fbeSDavid Hildenbrand 	 */
290a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
291a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
292a3508fbeSDavid Hildenbrand 		return;
293a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
29419c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
29519c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
2960615a326SDavid Hildenbrand 	if (sclp.has_siif)
2970615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
29877d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
29977d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
300a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
301a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
3025630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
3035630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
30413ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
30513ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
3067fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
3077fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
308730cd632SFarhan Ali 	if (sclp.has_kss)
309730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
3105d3876a8SDavid Hildenbrand 	/*
3115d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
3125d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
3135d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
3145d3876a8SDavid Hildenbrand 	 *
3155d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
3165d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
3175d3876a8SDavid Hildenbrand 	 *
3185d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
3195d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
3205d3876a8SDavid Hildenbrand 	 *
3215d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
3225d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
3235d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
3245d3876a8SDavid Hildenbrand 	 *
3255d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
3265d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
3275d3876a8SDavid Hildenbrand 	 */
32822be5a13SDavid Hildenbrand }
32922be5a13SDavid Hildenbrand 
330b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
331b0c632dbSHeiko Carstens {
33278f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
33378f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
33478f26131SChristian Borntraeger 		return -ENOMEM;
33578f26131SChristian Borntraeger 
33678f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
33778f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
33878f26131SChristian Borntraeger 		return -ENOMEM;
33978f26131SChristian Borntraeger 	}
34078f26131SChristian Borntraeger 
34122be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
34222be5a13SDavid Hildenbrand 
34384877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
34484877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
345b0c632dbSHeiko Carstens }
346b0c632dbSHeiko Carstens 
34778f26131SChristian Borntraeger void kvm_arch_exit(void)
34878f26131SChristian Borntraeger {
34978f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
35078f26131SChristian Borntraeger }
35178f26131SChristian Borntraeger 
352b0c632dbSHeiko Carstens /* Section: device related */
353b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
354b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
355b0c632dbSHeiko Carstens {
356b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
357b0c632dbSHeiko Carstens 		return s390_enable_sie();
358b0c632dbSHeiko Carstens 	return -EINVAL;
359b0c632dbSHeiko Carstens }
360b0c632dbSHeiko Carstens 
361784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
362b0c632dbSHeiko Carstens {
363d7b0b5ebSCarsten Otte 	int r;
364d7b0b5ebSCarsten Otte 
3652bd0ac4eSCarsten Otte 	switch (ext) {
366d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
367b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
36852e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
3691efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
3701efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
3711efd0f59SCarsten Otte #endif
3723c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
37360b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
37414eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
375d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
376fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
37710ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
378c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
379d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
38078599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
381f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
3826352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
383460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
38447b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
3852444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
386e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
38730ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
388816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
3896502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
39047a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
391d7b0b5ebSCarsten Otte 		r = 1;
392d7b0b5ebSCarsten Otte 		break;
39341408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
39441408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
39541408c28SThomas Huth 		break;
396e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
397e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
39876a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
399a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
400a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
401a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
40276a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
403e726b1bdSChristian Borntraeger 		break;
404e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
405e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
406e1e2e605SNick Wang 		break;
4071526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
408abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
4091526bf9cSChristian Borntraeger 		break;
41068c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
41168c55750SEric Farman 		r = MACHINE_HAS_VX;
41268c55750SEric Farman 		break;
413c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
414c6e5f166SFan Zhang 		r = test_facility(64);
415c6e5f166SFan Zhang 		break;
4164e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
4174e0b1ab7SFan Zhang 		r = test_facility(133);
4184e0b1ab7SFan Zhang 		break;
4192bd0ac4eSCarsten Otte 	default:
420d7b0b5ebSCarsten Otte 		r = 0;
421b0c632dbSHeiko Carstens 	}
422d7b0b5ebSCarsten Otte 	return r;
4232bd0ac4eSCarsten Otte }
424b0c632dbSHeiko Carstens 
42515f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
42615f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
42715f36ebdSJason J. Herne {
42815f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
42915f36ebdSJason J. Herne 	unsigned long address;
43015f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
43115f36ebdSJason J. Herne 
43215f36ebdSJason J. Herne 	/* Loop over all guest pages */
43315f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
43415f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
43515f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
43615f36ebdSJason J. Herne 
4371e133ab2SMartin Schwidefsky 		if (test_and_clear_guest_dirty(gmap->mm, address))
43815f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
4391763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
4401763f8d0SChristian Borntraeger 			return;
44170c88a00SChristian Borntraeger 		cond_resched();
44215f36ebdSJason J. Herne 	}
44315f36ebdSJason J. Herne }
44415f36ebdSJason J. Herne 
445b0c632dbSHeiko Carstens /* Section: vm related */
446a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
447a6e2f683SEugene (jno) Dvurechenski 
448b0c632dbSHeiko Carstens /*
449b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
450b0c632dbSHeiko Carstens  */
451b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
452b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
453b0c632dbSHeiko Carstens {
45415f36ebdSJason J. Herne 	int r;
45515f36ebdSJason J. Herne 	unsigned long n;
4569f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
45715f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
45815f36ebdSJason J. Herne 	int is_dirty = 0;
45915f36ebdSJason J. Herne 
460e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
461e1e8a962SJanosch Frank 		return -EINVAL;
462e1e8a962SJanosch Frank 
46315f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
46415f36ebdSJason J. Herne 
46515f36ebdSJason J. Herne 	r = -EINVAL;
46615f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
46715f36ebdSJason J. Herne 		goto out;
46815f36ebdSJason J. Herne 
4699f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
4709f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
47115f36ebdSJason J. Herne 	r = -ENOENT;
47215f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
47315f36ebdSJason J. Herne 		goto out;
47415f36ebdSJason J. Herne 
47515f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
47615f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
47715f36ebdSJason J. Herne 	if (r)
47815f36ebdSJason J. Herne 		goto out;
47915f36ebdSJason J. Herne 
48015f36ebdSJason J. Herne 	/* Clear the dirty log */
48115f36ebdSJason J. Herne 	if (is_dirty) {
48215f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
48315f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
48415f36ebdSJason J. Herne 	}
48515f36ebdSJason J. Herne 	r = 0;
48615f36ebdSJason J. Herne out:
48715f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
48815f36ebdSJason J. Herne 	return r;
489b0c632dbSHeiko Carstens }
490b0c632dbSHeiko Carstens 
4916502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
4926502a34cSDavid Hildenbrand {
4936502a34cSDavid Hildenbrand 	unsigned int i;
4946502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
4956502a34cSDavid Hildenbrand 
4966502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
4976502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
4986502a34cSDavid Hildenbrand 	}
4996502a34cSDavid Hildenbrand }
5006502a34cSDavid Hildenbrand 
501d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
502d938dc55SCornelia Huck {
503d938dc55SCornelia Huck 	int r;
504d938dc55SCornelia Huck 
505d938dc55SCornelia Huck 	if (cap->flags)
506d938dc55SCornelia Huck 		return -EINVAL;
507d938dc55SCornelia Huck 
508d938dc55SCornelia Huck 	switch (cap->cap) {
50984223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
510c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
51184223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
51284223598SCornelia Huck 		r = 0;
51384223598SCornelia Huck 		break;
5142444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
515c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
5162444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
5172444b352SDavid Hildenbrand 		r = 0;
5182444b352SDavid Hildenbrand 		break;
51968c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
5205967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
521a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
5225967c17bSDavid Hildenbrand 			r = -EBUSY;
5235967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
524c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
525c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
5262f87d942SGuenther Hutzl 			if (test_facility(134)) {
5272f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
5282f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
5292f87d942SGuenther Hutzl 			}
53053743aa7SMaxim Samoylov 			if (test_facility(135)) {
53153743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
53253743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
53353743aa7SMaxim Samoylov 			}
53418280d8bSMichael Mueller 			r = 0;
53518280d8bSMichael Mueller 		} else
53618280d8bSMichael Mueller 			r = -EINVAL;
5375967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
538c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
539c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
54068c55750SEric Farman 		break;
541c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
542c6e5f166SFan Zhang 		r = -EINVAL;
543c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
544a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
545c6e5f166SFan Zhang 			r = -EBUSY;
546c6e5f166SFan Zhang 		} else if (test_facility(64)) {
547c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
548c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
549c6e5f166SFan Zhang 			r = 0;
550c6e5f166SFan Zhang 		}
551c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
552c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
553c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
554c6e5f166SFan Zhang 		break;
55547a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
55647a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
55747a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
55847a4693eSYi Min Zhao 			r = -EBUSY;
55947a4693eSYi Min Zhao 		} else {
56047a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
56147a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
56247a4693eSYi Min Zhao 			kvm->arch.float_int.ais_enabled = 1;
56347a4693eSYi Min Zhao 			r = 0;
56447a4693eSYi Min Zhao 		}
56547a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
56647a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
56747a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
56847a4693eSYi Min Zhao 		break;
5694e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
5704e0b1ab7SFan Zhang 		r = -EINVAL;
5714e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
5724e0b1ab7SFan Zhang 		if (atomic_read(&kvm->online_vcpus)) {
5734e0b1ab7SFan Zhang 			r = -EBUSY;
5744e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
5754e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
5764e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
5774e0b1ab7SFan Zhang 			r = 0;
5784e0b1ab7SFan Zhang 		}
5794e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
5804e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
5814e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
5824e0b1ab7SFan Zhang 		break;
583e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
584c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
585e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
586e44fc8c9SEkaterina Tumanova 		r = 0;
587e44fc8c9SEkaterina Tumanova 		break;
5886502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5896502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
5906502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
5916502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
5926502a34cSDavid Hildenbrand 		r = 0;
5936502a34cSDavid Hildenbrand 		break;
594d938dc55SCornelia Huck 	default:
595d938dc55SCornelia Huck 		r = -EINVAL;
596d938dc55SCornelia Huck 		break;
597d938dc55SCornelia Huck 	}
598d938dc55SCornelia Huck 	return r;
599d938dc55SCornelia Huck }
600d938dc55SCornelia Huck 
6018c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
6028c0a7ce6SDominik Dingel {
6038c0a7ce6SDominik Dingel 	int ret;
6048c0a7ce6SDominik Dingel 
6058c0a7ce6SDominik Dingel 	switch (attr->attr) {
6068c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
6078c0a7ce6SDominik Dingel 		ret = 0;
608c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
609a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
610a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
6118c0a7ce6SDominik Dingel 			ret = -EFAULT;
6128c0a7ce6SDominik Dingel 		break;
6138c0a7ce6SDominik Dingel 	default:
6148c0a7ce6SDominik Dingel 		ret = -ENXIO;
6158c0a7ce6SDominik Dingel 		break;
6168c0a7ce6SDominik Dingel 	}
6178c0a7ce6SDominik Dingel 	return ret;
6188c0a7ce6SDominik Dingel }
6198c0a7ce6SDominik Dingel 
6208c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
6214f718eabSDominik Dingel {
6224f718eabSDominik Dingel 	int ret;
6234f718eabSDominik Dingel 	unsigned int idx;
6244f718eabSDominik Dingel 	switch (attr->attr) {
6254f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
626f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
627c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
628e6db1d61SDominik Dingel 			break;
629e6db1d61SDominik Dingel 
6304f718eabSDominik Dingel 		ret = -EBUSY;
631c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
6324f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
633a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6344f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
6354f718eabSDominik Dingel 			ret = 0;
6364f718eabSDominik Dingel 		}
6374f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
6384f718eabSDominik Dingel 		break;
6394f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
640f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
641f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
642f9cbd9b0SDavid Hildenbrand 			break;
643c3489155SDominik Dingel 		ret = -EINVAL;
644c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
645c3489155SDominik Dingel 			break;
646c3489155SDominik Dingel 
647c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
6484f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
6494f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
650a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
6514f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
6524f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
6534f718eabSDominik Dingel 		ret = 0;
6544f718eabSDominik Dingel 		break;
6558c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
6568c0a7ce6SDominik Dingel 		unsigned long new_limit;
6578c0a7ce6SDominik Dingel 
6588c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
6598c0a7ce6SDominik Dingel 			return -EINVAL;
6608c0a7ce6SDominik Dingel 
6618c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
6628c0a7ce6SDominik Dingel 			return -EFAULT;
6638c0a7ce6SDominik Dingel 
664a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
665a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
6668c0a7ce6SDominik Dingel 			return -E2BIG;
6678c0a7ce6SDominik Dingel 
668a3a92c31SDominik Dingel 		if (!new_limit)
669a3a92c31SDominik Dingel 			return -EINVAL;
670a3a92c31SDominik Dingel 
6716ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
672a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
673a3a92c31SDominik Dingel 			new_limit -= 1;
674a3a92c31SDominik Dingel 
6758c0a7ce6SDominik Dingel 		ret = -EBUSY;
6768c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
677a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6786ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
6796ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
6808c0a7ce6SDominik Dingel 
6818c0a7ce6SDominik Dingel 			if (!new) {
6828c0a7ce6SDominik Dingel 				ret = -ENOMEM;
6838c0a7ce6SDominik Dingel 			} else {
6846ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
6858c0a7ce6SDominik Dingel 				new->private = kvm;
6868c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
6878c0a7ce6SDominik Dingel 				ret = 0;
6888c0a7ce6SDominik Dingel 			}
6898c0a7ce6SDominik Dingel 		}
6908c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
691a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
692a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
693a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
6948c0a7ce6SDominik Dingel 		break;
6958c0a7ce6SDominik Dingel 	}
6964f718eabSDominik Dingel 	default:
6974f718eabSDominik Dingel 		ret = -ENXIO;
6984f718eabSDominik Dingel 		break;
6994f718eabSDominik Dingel 	}
7004f718eabSDominik Dingel 	return ret;
7014f718eabSDominik Dingel }
7024f718eabSDominik Dingel 
703a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
704a374e892STony Krowiak 
705a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
706a374e892STony Krowiak {
707a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
708a374e892STony Krowiak 	int i;
709a374e892STony Krowiak 
7109d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
711a374e892STony Krowiak 		return -EINVAL;
712a374e892STony Krowiak 
713a374e892STony Krowiak 	mutex_lock(&kvm->lock);
714a374e892STony Krowiak 	switch (attr->attr) {
715a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
716a374e892STony Krowiak 		get_random_bytes(
717a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
718a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
719a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
720c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
721a374e892STony Krowiak 		break;
722a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
723a374e892STony Krowiak 		get_random_bytes(
724a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
725a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
726a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
727c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
728a374e892STony Krowiak 		break;
729a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
730a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
731a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
732a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
733c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
734a374e892STony Krowiak 		break;
735a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
736a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
737a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
738a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
739c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
740a374e892STony Krowiak 		break;
741a374e892STony Krowiak 	default:
742a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
743a374e892STony Krowiak 		return -ENXIO;
744a374e892STony Krowiak 	}
745a374e892STony Krowiak 
746a374e892STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm) {
747a374e892STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
748a374e892STony Krowiak 		exit_sie(vcpu);
749a374e892STony Krowiak 	}
750a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
751a374e892STony Krowiak 	return 0;
752a374e892STony Krowiak }
753a374e892STony Krowiak 
754*190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
755*190df4a2SClaudio Imbrenda {
756*190df4a2SClaudio Imbrenda 	int cx;
757*190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
758*190df4a2SClaudio Imbrenda 
759*190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
760*190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
761*190df4a2SClaudio Imbrenda }
762*190df4a2SClaudio Imbrenda 
763*190df4a2SClaudio Imbrenda /*
764*190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
765*190df4a2SClaudio Imbrenda  * kvm->lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
766*190df4a2SClaudio Imbrenda  */
767*190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
768*190df4a2SClaudio Imbrenda {
769*190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
770*190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
771*190df4a2SClaudio Imbrenda 	/* should be the only one */
772*190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
773*190df4a2SClaudio Imbrenda 	unsigned long ram_pages;
774*190df4a2SClaudio Imbrenda 	int slotnr;
775*190df4a2SClaudio Imbrenda 
776*190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
777*190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state)
778*190df4a2SClaudio Imbrenda 		return 0;
779*190df4a2SClaudio Imbrenda 
780*190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
781*190df4a2SClaudio Imbrenda 	if (!slots || !slots->used_slots)
782*190df4a2SClaudio Imbrenda 		return -EINVAL;
783*190df4a2SClaudio Imbrenda 
784*190df4a2SClaudio Imbrenda 	mgs = kzalloc(sizeof(*mgs), GFP_KERNEL);
785*190df4a2SClaudio Imbrenda 	if (!mgs)
786*190df4a2SClaudio Imbrenda 		return -ENOMEM;
787*190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = mgs;
788*190df4a2SClaudio Imbrenda 
789*190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
790*190df4a2SClaudio Imbrenda 		/*
791*190df4a2SClaudio Imbrenda 		 * Get the last slot. They should be sorted by base_gfn, so the
792*190df4a2SClaudio Imbrenda 		 * last slot is also the one at the end of the address space.
793*190df4a2SClaudio Imbrenda 		 * We have verified above that at least one slot is present.
794*190df4a2SClaudio Imbrenda 		 */
795*190df4a2SClaudio Imbrenda 		ms = slots->memslots + slots->used_slots - 1;
796*190df4a2SClaudio Imbrenda 		/* round up so we only use full longs */
797*190df4a2SClaudio Imbrenda 		ram_pages = roundup(ms->base_gfn + ms->npages, BITS_PER_LONG);
798*190df4a2SClaudio Imbrenda 		/* allocate enough bytes to store all the bits */
799*190df4a2SClaudio Imbrenda 		mgs->pgste_bitmap = vmalloc(ram_pages / 8);
800*190df4a2SClaudio Imbrenda 		if (!mgs->pgste_bitmap) {
801*190df4a2SClaudio Imbrenda 			kfree(mgs);
802*190df4a2SClaudio Imbrenda 			kvm->arch.migration_state = NULL;
803*190df4a2SClaudio Imbrenda 			return -ENOMEM;
804*190df4a2SClaudio Imbrenda 		}
805*190df4a2SClaudio Imbrenda 
806*190df4a2SClaudio Imbrenda 		mgs->bitmap_size = ram_pages;
807*190df4a2SClaudio Imbrenda 		atomic64_set(&mgs->dirty_pages, ram_pages);
808*190df4a2SClaudio Imbrenda 		/* mark all the pages in active slots as dirty */
809*190df4a2SClaudio Imbrenda 		for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
810*190df4a2SClaudio Imbrenda 			ms = slots->memslots + slotnr;
811*190df4a2SClaudio Imbrenda 			bitmap_set(mgs->pgste_bitmap, ms->base_gfn, ms->npages);
812*190df4a2SClaudio Imbrenda 		}
813*190df4a2SClaudio Imbrenda 
814*190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
815*190df4a2SClaudio Imbrenda 	}
816*190df4a2SClaudio Imbrenda 	return 0;
817*190df4a2SClaudio Imbrenda }
818*190df4a2SClaudio Imbrenda 
819*190df4a2SClaudio Imbrenda /*
820*190df4a2SClaudio Imbrenda  * Must be called with kvm->lock to avoid races with ourselves and
821*190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
822*190df4a2SClaudio Imbrenda  */
823*190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
824*190df4a2SClaudio Imbrenda {
825*190df4a2SClaudio Imbrenda 	struct kvm_s390_migration_state *mgs;
826*190df4a2SClaudio Imbrenda 
827*190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
828*190df4a2SClaudio Imbrenda 	if (!kvm->arch.migration_state)
829*190df4a2SClaudio Imbrenda 		return 0;
830*190df4a2SClaudio Imbrenda 	mgs = kvm->arch.migration_state;
831*190df4a2SClaudio Imbrenda 	kvm->arch.migration_state = NULL;
832*190df4a2SClaudio Imbrenda 
833*190df4a2SClaudio Imbrenda 	if (kvm->arch.use_cmma) {
834*190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
835*190df4a2SClaudio Imbrenda 		vfree(mgs->pgste_bitmap);
836*190df4a2SClaudio Imbrenda 	}
837*190df4a2SClaudio Imbrenda 	kfree(mgs);
838*190df4a2SClaudio Imbrenda 	return 0;
839*190df4a2SClaudio Imbrenda }
840*190df4a2SClaudio Imbrenda 
841*190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
842*190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
843*190df4a2SClaudio Imbrenda {
844*190df4a2SClaudio Imbrenda 	int idx, res = -ENXIO;
845*190df4a2SClaudio Imbrenda 
846*190df4a2SClaudio Imbrenda 	mutex_lock(&kvm->lock);
847*190df4a2SClaudio Imbrenda 	switch (attr->attr) {
848*190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
849*190df4a2SClaudio Imbrenda 		idx = srcu_read_lock(&kvm->srcu);
850*190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
851*190df4a2SClaudio Imbrenda 		srcu_read_unlock(&kvm->srcu, idx);
852*190df4a2SClaudio Imbrenda 		break;
853*190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
854*190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
855*190df4a2SClaudio Imbrenda 		break;
856*190df4a2SClaudio Imbrenda 	default:
857*190df4a2SClaudio Imbrenda 		break;
858*190df4a2SClaudio Imbrenda 	}
859*190df4a2SClaudio Imbrenda 	mutex_unlock(&kvm->lock);
860*190df4a2SClaudio Imbrenda 
861*190df4a2SClaudio Imbrenda 	return res;
862*190df4a2SClaudio Imbrenda }
863*190df4a2SClaudio Imbrenda 
864*190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
865*190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
866*190df4a2SClaudio Imbrenda {
867*190df4a2SClaudio Imbrenda 	u64 mig = (kvm->arch.migration_state != NULL);
868*190df4a2SClaudio Imbrenda 
869*190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
870*190df4a2SClaudio Imbrenda 		return -ENXIO;
871*190df4a2SClaudio Imbrenda 
872*190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
873*190df4a2SClaudio Imbrenda 		return -EFAULT;
874*190df4a2SClaudio Imbrenda 	return 0;
875*190df4a2SClaudio Imbrenda }
876*190df4a2SClaudio Imbrenda 
87772f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
87872f25020SJason J. Herne {
87972f25020SJason J. Herne 	u8 gtod_high;
88072f25020SJason J. Herne 
88172f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
88272f25020SJason J. Herne 					   sizeof(gtod_high)))
88372f25020SJason J. Herne 		return -EFAULT;
88472f25020SJason J. Herne 
88572f25020SJason J. Herne 	if (gtod_high != 0)
88672f25020SJason J. Herne 		return -EINVAL;
88758c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
88872f25020SJason J. Herne 
88972f25020SJason J. Herne 	return 0;
89072f25020SJason J. Herne }
89172f25020SJason J. Herne 
89272f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
89372f25020SJason J. Herne {
8945a3d883aSDavid Hildenbrand 	u64 gtod;
89572f25020SJason J. Herne 
89672f25020SJason J. Herne 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
89772f25020SJason J. Herne 		return -EFAULT;
89872f25020SJason J. Herne 
89925ed1675SDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, gtod);
90058c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
90172f25020SJason J. Herne 	return 0;
90272f25020SJason J. Herne }
90372f25020SJason J. Herne 
90472f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
90572f25020SJason J. Herne {
90672f25020SJason J. Herne 	int ret;
90772f25020SJason J. Herne 
90872f25020SJason J. Herne 	if (attr->flags)
90972f25020SJason J. Herne 		return -EINVAL;
91072f25020SJason J. Herne 
91172f25020SJason J. Herne 	switch (attr->attr) {
91272f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
91372f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
91472f25020SJason J. Herne 		break;
91572f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
91672f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
91772f25020SJason J. Herne 		break;
91872f25020SJason J. Herne 	default:
91972f25020SJason J. Herne 		ret = -ENXIO;
92072f25020SJason J. Herne 		break;
92172f25020SJason J. Herne 	}
92272f25020SJason J. Herne 	return ret;
92372f25020SJason J. Herne }
92472f25020SJason J. Herne 
92572f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
92672f25020SJason J. Herne {
92772f25020SJason J. Herne 	u8 gtod_high = 0;
92872f25020SJason J. Herne 
92972f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
93072f25020SJason J. Herne 					 sizeof(gtod_high)))
93172f25020SJason J. Herne 		return -EFAULT;
93258c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
93372f25020SJason J. Herne 
93472f25020SJason J. Herne 	return 0;
93572f25020SJason J. Herne }
93672f25020SJason J. Herne 
93772f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
93872f25020SJason J. Herne {
9395a3d883aSDavid Hildenbrand 	u64 gtod;
94072f25020SJason J. Herne 
94160417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
94272f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
94372f25020SJason J. Herne 		return -EFAULT;
94458c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
94572f25020SJason J. Herne 
94672f25020SJason J. Herne 	return 0;
94772f25020SJason J. Herne }
94872f25020SJason J. Herne 
94972f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
95072f25020SJason J. Herne {
95172f25020SJason J. Herne 	int ret;
95272f25020SJason J. Herne 
95372f25020SJason J. Herne 	if (attr->flags)
95472f25020SJason J. Herne 		return -EINVAL;
95572f25020SJason J. Herne 
95672f25020SJason J. Herne 	switch (attr->attr) {
95772f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
95872f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
95972f25020SJason J. Herne 		break;
96072f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
96172f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
96272f25020SJason J. Herne 		break;
96372f25020SJason J. Herne 	default:
96472f25020SJason J. Herne 		ret = -ENXIO;
96572f25020SJason J. Herne 		break;
96672f25020SJason J. Herne 	}
96772f25020SJason J. Herne 	return ret;
96872f25020SJason J. Herne }
96972f25020SJason J. Herne 
970658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
971658b6edaSMichael Mueller {
972658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
973053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
974658b6edaSMichael Mueller 	int ret = 0;
975658b6edaSMichael Mueller 
976658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
977a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
978658b6edaSMichael Mueller 		ret = -EBUSY;
979658b6edaSMichael Mueller 		goto out;
980658b6edaSMichael Mueller 	}
981658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
982658b6edaSMichael Mueller 	if (!proc) {
983658b6edaSMichael Mueller 		ret = -ENOMEM;
984658b6edaSMichael Mueller 		goto out;
985658b6edaSMichael Mueller 	}
986658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
987658b6edaSMichael Mueller 			    sizeof(*proc))) {
9889bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
989053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
990053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
9910487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
992053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
993053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
994053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
995053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
996053dd230SDavid Hildenbrand 			else
997658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
998053dd230SDavid Hildenbrand 		}
999c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1000658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1001a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1002a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1003a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1004a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1005a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1006a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1007a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1008658b6edaSMichael Mueller 	} else
1009658b6edaSMichael Mueller 		ret = -EFAULT;
1010658b6edaSMichael Mueller 	kfree(proc);
1011658b6edaSMichael Mueller out:
1012658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1013658b6edaSMichael Mueller 	return ret;
1014658b6edaSMichael Mueller }
1015658b6edaSMichael Mueller 
101615c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
101715c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
101815c9705fSDavid Hildenbrand {
101915c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
102015c9705fSDavid Hildenbrand 	int ret = -EBUSY;
102115c9705fSDavid Hildenbrand 
102215c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
102315c9705fSDavid Hildenbrand 		return -EFAULT;
102415c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
102515c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
102615c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
102715c9705fSDavid Hildenbrand 		return -EINVAL;
102815c9705fSDavid Hildenbrand 
102915c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
103015c9705fSDavid Hildenbrand 	if (!atomic_read(&kvm->online_vcpus)) {
103115c9705fSDavid Hildenbrand 		bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
103215c9705fSDavid Hildenbrand 			    KVM_S390_VM_CPU_FEAT_NR_BITS);
103315c9705fSDavid Hildenbrand 		ret = 0;
103415c9705fSDavid Hildenbrand 	}
103515c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
103615c9705fSDavid Hildenbrand 	return ret;
103715c9705fSDavid Hildenbrand }
103815c9705fSDavid Hildenbrand 
10390a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
10400a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
10410a763c78SDavid Hildenbrand {
10420a763c78SDavid Hildenbrand 	/*
10430a763c78SDavid Hildenbrand 	 * Once supported by kernel + hw, we have to store the subfunctions
10440a763c78SDavid Hildenbrand 	 * in kvm->arch and remember that user space configured them.
10450a763c78SDavid Hildenbrand 	 */
10460a763c78SDavid Hildenbrand 	return -ENXIO;
10470a763c78SDavid Hildenbrand }
10480a763c78SDavid Hildenbrand 
1049658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1050658b6edaSMichael Mueller {
1051658b6edaSMichael Mueller 	int ret = -ENXIO;
1052658b6edaSMichael Mueller 
1053658b6edaSMichael Mueller 	switch (attr->attr) {
1054658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1055658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1056658b6edaSMichael Mueller 		break;
105715c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
105815c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
105915c9705fSDavid Hildenbrand 		break;
10600a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
10610a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
10620a763c78SDavid Hildenbrand 		break;
1063658b6edaSMichael Mueller 	}
1064658b6edaSMichael Mueller 	return ret;
1065658b6edaSMichael Mueller }
1066658b6edaSMichael Mueller 
1067658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1068658b6edaSMichael Mueller {
1069658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1070658b6edaSMichael Mueller 	int ret = 0;
1071658b6edaSMichael Mueller 
1072658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1073658b6edaSMichael Mueller 	if (!proc) {
1074658b6edaSMichael Mueller 		ret = -ENOMEM;
1075658b6edaSMichael Mueller 		goto out;
1076658b6edaSMichael Mueller 	}
10779bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1078658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1079c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1080c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1081a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1082a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1083a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1084a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1085a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1086a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1087a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1088658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1089658b6edaSMichael Mueller 		ret = -EFAULT;
1090658b6edaSMichael Mueller 	kfree(proc);
1091658b6edaSMichael Mueller out:
1092658b6edaSMichael Mueller 	return ret;
1093658b6edaSMichael Mueller }
1094658b6edaSMichael Mueller 
1095658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1096658b6edaSMichael Mueller {
1097658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1098658b6edaSMichael Mueller 	int ret = 0;
1099658b6edaSMichael Mueller 
1100658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1101658b6edaSMichael Mueller 	if (!mach) {
1102658b6edaSMichael Mueller 		ret = -ENOMEM;
1103658b6edaSMichael Mueller 		goto out;
1104658b6edaSMichael Mueller 	}
1105658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
110637c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1107c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1108981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1109658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
111004478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
1111a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1112a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1113a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1114a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1115a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1116a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1117a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1118a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1119a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1120a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1121a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1122658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1123658b6edaSMichael Mueller 		ret = -EFAULT;
1124658b6edaSMichael Mueller 	kfree(mach);
1125658b6edaSMichael Mueller out:
1126658b6edaSMichael Mueller 	return ret;
1127658b6edaSMichael Mueller }
1128658b6edaSMichael Mueller 
112915c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
113015c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
113115c9705fSDavid Hildenbrand {
113215c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
113315c9705fSDavid Hildenbrand 
113415c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
113515c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
113615c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
113715c9705fSDavid Hildenbrand 		return -EFAULT;
113815c9705fSDavid Hildenbrand 	return 0;
113915c9705fSDavid Hildenbrand }
114015c9705fSDavid Hildenbrand 
114115c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
114215c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
114315c9705fSDavid Hildenbrand {
114415c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
114515c9705fSDavid Hildenbrand 
114615c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
114715c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
114815c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
114915c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
115015c9705fSDavid Hildenbrand 		return -EFAULT;
115115c9705fSDavid Hildenbrand 	return 0;
115215c9705fSDavid Hildenbrand }
115315c9705fSDavid Hildenbrand 
11540a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
11550a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
11560a763c78SDavid Hildenbrand {
11570a763c78SDavid Hildenbrand 	/*
11580a763c78SDavid Hildenbrand 	 * Once we can actually configure subfunctions (kernel + hw support),
11590a763c78SDavid Hildenbrand 	 * we have to check if they were already set by user space, if so copy
11600a763c78SDavid Hildenbrand 	 * them from kvm->arch.
11610a763c78SDavid Hildenbrand 	 */
11620a763c78SDavid Hildenbrand 	return -ENXIO;
11630a763c78SDavid Hildenbrand }
11640a763c78SDavid Hildenbrand 
11650a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
11660a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
11670a763c78SDavid Hildenbrand {
11680a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
11690a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
11700a763c78SDavid Hildenbrand 		return -EFAULT;
11710a763c78SDavid Hildenbrand 	return 0;
11720a763c78SDavid Hildenbrand }
1173658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1174658b6edaSMichael Mueller {
1175658b6edaSMichael Mueller 	int ret = -ENXIO;
1176658b6edaSMichael Mueller 
1177658b6edaSMichael Mueller 	switch (attr->attr) {
1178658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1179658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1180658b6edaSMichael Mueller 		break;
1181658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1182658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1183658b6edaSMichael Mueller 		break;
118415c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
118515c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
118615c9705fSDavid Hildenbrand 		break;
118715c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
118815c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
118915c9705fSDavid Hildenbrand 		break;
11900a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
11910a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
11920a763c78SDavid Hildenbrand 		break;
11930a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
11940a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
11950a763c78SDavid Hildenbrand 		break;
1196658b6edaSMichael Mueller 	}
1197658b6edaSMichael Mueller 	return ret;
1198658b6edaSMichael Mueller }
1199658b6edaSMichael Mueller 
1200f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1201f2061656SDominik Dingel {
1202f2061656SDominik Dingel 	int ret;
1203f2061656SDominik Dingel 
1204f2061656SDominik Dingel 	switch (attr->group) {
12054f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
12068c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
12074f718eabSDominik Dingel 		break;
120872f25020SJason J. Herne 	case KVM_S390_VM_TOD:
120972f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
121072f25020SJason J. Herne 		break;
1211658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1212658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1213658b6edaSMichael Mueller 		break;
1214a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1215a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1216a374e892STony Krowiak 		break;
1217*190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1218*190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1219*190df4a2SClaudio Imbrenda 		break;
1220f2061656SDominik Dingel 	default:
1221f2061656SDominik Dingel 		ret = -ENXIO;
1222f2061656SDominik Dingel 		break;
1223f2061656SDominik Dingel 	}
1224f2061656SDominik Dingel 
1225f2061656SDominik Dingel 	return ret;
1226f2061656SDominik Dingel }
1227f2061656SDominik Dingel 
1228f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1229f2061656SDominik Dingel {
12308c0a7ce6SDominik Dingel 	int ret;
12318c0a7ce6SDominik Dingel 
12328c0a7ce6SDominik Dingel 	switch (attr->group) {
12338c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
12348c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
12358c0a7ce6SDominik Dingel 		break;
123672f25020SJason J. Herne 	case KVM_S390_VM_TOD:
123772f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
123872f25020SJason J. Herne 		break;
1239658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1240658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1241658b6edaSMichael Mueller 		break;
1242*190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1243*190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1244*190df4a2SClaudio Imbrenda 		break;
12458c0a7ce6SDominik Dingel 	default:
12468c0a7ce6SDominik Dingel 		ret = -ENXIO;
12478c0a7ce6SDominik Dingel 		break;
12488c0a7ce6SDominik Dingel 	}
12498c0a7ce6SDominik Dingel 
12508c0a7ce6SDominik Dingel 	return ret;
1251f2061656SDominik Dingel }
1252f2061656SDominik Dingel 
1253f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1254f2061656SDominik Dingel {
1255f2061656SDominik Dingel 	int ret;
1256f2061656SDominik Dingel 
1257f2061656SDominik Dingel 	switch (attr->group) {
12584f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
12594f718eabSDominik Dingel 		switch (attr->attr) {
12604f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
12614f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1262f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1263f9cbd9b0SDavid Hildenbrand 			break;
12648c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
12654f718eabSDominik Dingel 			ret = 0;
12664f718eabSDominik Dingel 			break;
12674f718eabSDominik Dingel 		default:
12684f718eabSDominik Dingel 			ret = -ENXIO;
12694f718eabSDominik Dingel 			break;
12704f718eabSDominik Dingel 		}
12714f718eabSDominik Dingel 		break;
127272f25020SJason J. Herne 	case KVM_S390_VM_TOD:
127372f25020SJason J. Herne 		switch (attr->attr) {
127472f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
127572f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
127672f25020SJason J. Herne 			ret = 0;
127772f25020SJason J. Herne 			break;
127872f25020SJason J. Herne 		default:
127972f25020SJason J. Herne 			ret = -ENXIO;
128072f25020SJason J. Herne 			break;
128172f25020SJason J. Herne 		}
128272f25020SJason J. Herne 		break;
1283658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1284658b6edaSMichael Mueller 		switch (attr->attr) {
1285658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1286658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
128715c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
128815c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
12890a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1290658b6edaSMichael Mueller 			ret = 0;
1291658b6edaSMichael Mueller 			break;
12920a763c78SDavid Hildenbrand 		/* configuring subfunctions is not supported yet */
12930a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1294658b6edaSMichael Mueller 		default:
1295658b6edaSMichael Mueller 			ret = -ENXIO;
1296658b6edaSMichael Mueller 			break;
1297658b6edaSMichael Mueller 		}
1298658b6edaSMichael Mueller 		break;
1299a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1300a374e892STony Krowiak 		switch (attr->attr) {
1301a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1302a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1303a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1304a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1305a374e892STony Krowiak 			ret = 0;
1306a374e892STony Krowiak 			break;
1307a374e892STony Krowiak 		default:
1308a374e892STony Krowiak 			ret = -ENXIO;
1309a374e892STony Krowiak 			break;
1310a374e892STony Krowiak 		}
1311a374e892STony Krowiak 		break;
1312*190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1313*190df4a2SClaudio Imbrenda 		ret = 0;
1314*190df4a2SClaudio Imbrenda 		break;
1315f2061656SDominik Dingel 	default:
1316f2061656SDominik Dingel 		ret = -ENXIO;
1317f2061656SDominik Dingel 		break;
1318f2061656SDominik Dingel 	}
1319f2061656SDominik Dingel 
1320f2061656SDominik Dingel 	return ret;
1321f2061656SDominik Dingel }
1322f2061656SDominik Dingel 
132330ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
132430ee2a98SJason J. Herne {
132530ee2a98SJason J. Herne 	uint8_t *keys;
132630ee2a98SJason J. Herne 	uint64_t hva;
132730ee2a98SJason J. Herne 	int i, r = 0;
132830ee2a98SJason J. Herne 
132930ee2a98SJason J. Herne 	if (args->flags != 0)
133030ee2a98SJason J. Herne 		return -EINVAL;
133130ee2a98SJason J. Herne 
133230ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
133330ee2a98SJason J. Herne 	if (!mm_use_skey(current->mm))
133430ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
133530ee2a98SJason J. Herne 
133630ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
133730ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
133830ee2a98SJason J. Herne 		return -EINVAL;
133930ee2a98SJason J. Herne 
1340752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
134130ee2a98SJason J. Herne 	if (!keys)
134230ee2a98SJason J. Herne 		return -ENOMEM;
134330ee2a98SJason J. Herne 
1344d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
134530ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
134630ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
134730ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
134830ee2a98SJason J. Herne 			r = -EFAULT;
1349d3ed1ceeSMartin Schwidefsky 			break;
135030ee2a98SJason J. Herne 		}
135130ee2a98SJason J. Herne 
1352154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1353154c8c19SDavid Hildenbrand 		if (r)
1354d3ed1ceeSMartin Schwidefsky 			break;
135530ee2a98SJason J. Herne 	}
1356d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
135730ee2a98SJason J. Herne 
1358d3ed1ceeSMartin Schwidefsky 	if (!r) {
135930ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
136030ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
136130ee2a98SJason J. Herne 		if (r)
136230ee2a98SJason J. Herne 			r = -EFAULT;
1363d3ed1ceeSMartin Schwidefsky 	}
1364d3ed1ceeSMartin Schwidefsky 
136530ee2a98SJason J. Herne 	kvfree(keys);
136630ee2a98SJason J. Herne 	return r;
136730ee2a98SJason J. Herne }
136830ee2a98SJason J. Herne 
136930ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
137030ee2a98SJason J. Herne {
137130ee2a98SJason J. Herne 	uint8_t *keys;
137230ee2a98SJason J. Herne 	uint64_t hva;
137330ee2a98SJason J. Herne 	int i, r = 0;
137430ee2a98SJason J. Herne 
137530ee2a98SJason J. Herne 	if (args->flags != 0)
137630ee2a98SJason J. Herne 		return -EINVAL;
137730ee2a98SJason J. Herne 
137830ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
137930ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
138030ee2a98SJason J. Herne 		return -EINVAL;
138130ee2a98SJason J. Herne 
1382752ade68SMichal Hocko 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
138330ee2a98SJason J. Herne 	if (!keys)
138430ee2a98SJason J. Herne 		return -ENOMEM;
138530ee2a98SJason J. Herne 
138630ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
138730ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
138830ee2a98SJason J. Herne 	if (r) {
138930ee2a98SJason J. Herne 		r = -EFAULT;
139030ee2a98SJason J. Herne 		goto out;
139130ee2a98SJason J. Herne 	}
139230ee2a98SJason J. Herne 
139330ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
139414d4a425SDominik Dingel 	r = s390_enable_skey();
139514d4a425SDominik Dingel 	if (r)
139614d4a425SDominik Dingel 		goto out;
139730ee2a98SJason J. Herne 
1398d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
139930ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
140030ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
140130ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
140230ee2a98SJason J. Herne 			r = -EFAULT;
1403d3ed1ceeSMartin Schwidefsky 			break;
140430ee2a98SJason J. Herne 		}
140530ee2a98SJason J. Herne 
140630ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
140730ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
140830ee2a98SJason J. Herne 			r = -EINVAL;
1409d3ed1ceeSMartin Schwidefsky 			break;
141030ee2a98SJason J. Herne 		}
141130ee2a98SJason J. Herne 
1412fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
141330ee2a98SJason J. Herne 		if (r)
1414d3ed1ceeSMartin Schwidefsky 			break;
141530ee2a98SJason J. Herne 	}
1416d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
141730ee2a98SJason J. Herne out:
141830ee2a98SJason J. Herne 	kvfree(keys);
141930ee2a98SJason J. Herne 	return r;
142030ee2a98SJason J. Herne }
142130ee2a98SJason J. Herne 
1422b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
1423b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
1424b0c632dbSHeiko Carstens {
1425b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
1426b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
1427f2061656SDominik Dingel 	struct kvm_device_attr attr;
1428b0c632dbSHeiko Carstens 	int r;
1429b0c632dbSHeiko Carstens 
1430b0c632dbSHeiko Carstens 	switch (ioctl) {
1431ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
1432ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
1433ba5c1e9bSCarsten Otte 
1434ba5c1e9bSCarsten Otte 		r = -EFAULT;
1435ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
1436ba5c1e9bSCarsten Otte 			break;
1437ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
1438ba5c1e9bSCarsten Otte 		break;
1439ba5c1e9bSCarsten Otte 	}
1440d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
1441d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
1442d938dc55SCornelia Huck 		r = -EFAULT;
1443d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1444d938dc55SCornelia Huck 			break;
1445d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1446d938dc55SCornelia Huck 		break;
1447d938dc55SCornelia Huck 	}
144884223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
144984223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
145084223598SCornelia Huck 
145184223598SCornelia Huck 		r = -EINVAL;
145284223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
145384223598SCornelia Huck 			/* Set up dummy routing. */
145484223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
1455152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
145684223598SCornelia Huck 		}
145784223598SCornelia Huck 		break;
145884223598SCornelia Huck 	}
1459f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
1460f2061656SDominik Dingel 		r = -EFAULT;
1461f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1462f2061656SDominik Dingel 			break;
1463f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
1464f2061656SDominik Dingel 		break;
1465f2061656SDominik Dingel 	}
1466f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
1467f2061656SDominik Dingel 		r = -EFAULT;
1468f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1469f2061656SDominik Dingel 			break;
1470f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
1471f2061656SDominik Dingel 		break;
1472f2061656SDominik Dingel 	}
1473f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
1474f2061656SDominik Dingel 		r = -EFAULT;
1475f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1476f2061656SDominik Dingel 			break;
1477f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
1478f2061656SDominik Dingel 		break;
1479f2061656SDominik Dingel 	}
148030ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
148130ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
148230ee2a98SJason J. Herne 
148330ee2a98SJason J. Herne 		r = -EFAULT;
148430ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
148530ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
148630ee2a98SJason J. Herne 			break;
148730ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
148830ee2a98SJason J. Herne 		break;
148930ee2a98SJason J. Herne 	}
149030ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
149130ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
149230ee2a98SJason J. Herne 
149330ee2a98SJason J. Herne 		r = -EFAULT;
149430ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
149530ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
149630ee2a98SJason J. Herne 			break;
149730ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
149830ee2a98SJason J. Herne 		break;
149930ee2a98SJason J. Herne 	}
1500b0c632dbSHeiko Carstens 	default:
1501367e1319SAvi Kivity 		r = -ENOTTY;
1502b0c632dbSHeiko Carstens 	}
1503b0c632dbSHeiko Carstens 
1504b0c632dbSHeiko Carstens 	return r;
1505b0c632dbSHeiko Carstens }
1506b0c632dbSHeiko Carstens 
150745c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
150845c9b47cSTony Krowiak {
150945c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
151086044c8cSChristian Borntraeger 	u32 cc = 0;
151145c9b47cSTony Krowiak 
151286044c8cSChristian Borntraeger 	memset(config, 0, 128);
151345c9b47cSTony Krowiak 	asm volatile(
151445c9b47cSTony Krowiak 		"lgr 0,%1\n"
151545c9b47cSTony Krowiak 		"lgr 2,%2\n"
151645c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
151786044c8cSChristian Borntraeger 		"0: ipm %0\n"
151845c9b47cSTony Krowiak 		"srl %0,28\n"
151986044c8cSChristian Borntraeger 		"1:\n"
152086044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
152186044c8cSChristian Borntraeger 		: "+r" (cc)
152245c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
152345c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
152445c9b47cSTony Krowiak 	);
152545c9b47cSTony Krowiak 
152645c9b47cSTony Krowiak 	return cc;
152745c9b47cSTony Krowiak }
152845c9b47cSTony Krowiak 
152945c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
153045c9b47cSTony Krowiak {
153145c9b47cSTony Krowiak 	u8 config[128];
153245c9b47cSTony Krowiak 	int cc;
153345c9b47cSTony Krowiak 
1534a6aacc3fSHeiko Carstens 	if (test_facility(12)) {
153545c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
153645c9b47cSTony Krowiak 
153745c9b47cSTony Krowiak 		if (cc)
153845c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
153945c9b47cSTony Krowiak 		else
154045c9b47cSTony Krowiak 			return config[0] & 0x40;
154145c9b47cSTony Krowiak 	}
154245c9b47cSTony Krowiak 
154345c9b47cSTony Krowiak 	return 0;
154445c9b47cSTony Krowiak }
154545c9b47cSTony Krowiak 
154645c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
154745c9b47cSTony Krowiak {
154845c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
154945c9b47cSTony Krowiak 
155045c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
155145c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
155245c9b47cSTony Krowiak 	else
155345c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
155445c9b47cSTony Krowiak }
155545c9b47cSTony Krowiak 
15569bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
15579d8d5786SMichael Mueller {
15589bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
15599bb0ec09SDavid Hildenbrand 
15609bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
15619bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
15629bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
15639d8d5786SMichael Mueller }
15649d8d5786SMichael Mueller 
1565c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
15665102ee87STony Krowiak {
15679d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
1568c54f0d6aSDavid Hildenbrand 		return;
15695102ee87STony Krowiak 
1570c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
157145c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
15725102ee87STony Krowiak 
1573ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1574ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1575ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1576ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1577ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1578ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1579ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
15805102ee87STony Krowiak }
15815102ee87STony Krowiak 
15827d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
15837d43bafcSEugene (jno) Dvurechenski {
15847d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
15855e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
15867d43bafcSEugene (jno) Dvurechenski 	else
15877d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
15887d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
15897d43bafcSEugene (jno) Dvurechenski }
15907d43bafcSEugene (jno) Dvurechenski 
1591e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1592b0c632dbSHeiko Carstens {
159376a6dd72SDavid Hildenbrand 	gfp_t alloc_flags = GFP_KERNEL;
15949d8d5786SMichael Mueller 	int i, rc;
1595b0c632dbSHeiko Carstens 	char debug_name[16];
1596f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1597b0c632dbSHeiko Carstens 
1598e08b9637SCarsten Otte 	rc = -EINVAL;
1599e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1600e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1601e08b9637SCarsten Otte 		goto out_err;
1602e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1603e08b9637SCarsten Otte 		goto out_err;
1604e08b9637SCarsten Otte #else
1605e08b9637SCarsten Otte 	if (type)
1606e08b9637SCarsten Otte 		goto out_err;
1607e08b9637SCarsten Otte #endif
1608e08b9637SCarsten Otte 
1609b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1610b0c632dbSHeiko Carstens 	if (rc)
1611d89f5effSJan Kiszka 		goto out_err;
1612b0c632dbSHeiko Carstens 
1613b290411aSCarsten Otte 	rc = -ENOMEM;
1614b290411aSCarsten Otte 
16157d0a5e62SJanosch Frank 	ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);
16167d0a5e62SJanosch Frank 
16177d43bafcSEugene (jno) Dvurechenski 	kvm->arch.use_esca = 0; /* start with basic SCA */
161876a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
161976a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
16205e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
162176a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
1622b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1623d89f5effSJan Kiszka 		goto out_err;
1624f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
1625c5c2c393SDavid Hildenbrand 	sca_offset += 16;
1626bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
1627c5c2c393SDavid Hildenbrand 		sca_offset = 0;
1628bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
1629bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
1630f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
1631b0c632dbSHeiko Carstens 
1632b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
1633b0c632dbSHeiko Carstens 
16341cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
1635b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
163640f5b735SDominik Dingel 		goto out_err;
1637b0c632dbSHeiko Carstens 
1638c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
1639c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1640c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
164140f5b735SDominik Dingel 		goto out_err;
16429d8d5786SMichael Mueller 
1643fb5bf93fSMichael Mueller 	/* Populate the facility mask initially. */
1644c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
164504478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
16469d8d5786SMichael Mueller 	for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
16479d8d5786SMichael Mueller 		if (i < kvm_s390_fac_list_mask_size())
1648c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
16499d8d5786SMichael Mueller 		else
1650c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] = 0UL;
16519d8d5786SMichael Mueller 	}
16529d8d5786SMichael Mueller 
1653981467c9SMichael Mueller 	/* Populate the facility list initially. */
1654c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1655c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
1656981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1657981467c9SMichael Mueller 
165895ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
165995ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
166095ca2cb5SJanosch Frank 
16619bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
166237c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
16639d8d5786SMichael Mueller 
1664c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
16655102ee87STony Krowiak 
166651978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
166751978393SFei Li 	kvm->arch.float_int.simm = 0;
166851978393SFei Li 	kvm->arch.float_int.nimm = 0;
166951978393SFei Li 	kvm->arch.float_int.ais_enabled = 0;
1670ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
16716d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
16726d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
16738a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
1674a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
1675ba5c1e9bSCarsten Otte 
1676b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
167778f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
1678b0c632dbSHeiko Carstens 
1679e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
1680e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
1681a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
1682e08b9637SCarsten Otte 	} else {
168332e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
1684ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
168532e6b236SGuenther Hutzl 		else
1686ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
168732e6b236SGuenther Hutzl 						    sclp.hamax + 1);
16886ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
1689598841caSCarsten Otte 		if (!kvm->arch.gmap)
169040f5b735SDominik Dingel 			goto out_err;
16912c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
169224eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
1693e08b9637SCarsten Otte 	}
1694fa6b7fe9SCornelia Huck 
1695fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
169684223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
169772f25020SJason J. Herne 	kvm->arch.epoch = 0;
1698fa6b7fe9SCornelia Huck 
16998ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
1700a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
17018335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
17028ad35755SDavid Hildenbrand 
1703d89f5effSJan Kiszka 	return 0;
1704d89f5effSJan Kiszka out_err:
1705c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
170640f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
17077d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
170878f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
1709d89f5effSJan Kiszka 	return rc;
1710b0c632dbSHeiko Carstens }
1711b0c632dbSHeiko Carstens 
1712235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void)
1713235539b4SLuiz Capitulino {
1714235539b4SLuiz Capitulino 	return false;
1715235539b4SLuiz Capitulino }
1716235539b4SLuiz Capitulino 
1717235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
1718235539b4SLuiz Capitulino {
1719235539b4SLuiz Capitulino 	return 0;
1720235539b4SLuiz Capitulino }
1721235539b4SLuiz Capitulino 
1722d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1723d329c035SChristian Borntraeger {
1724d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
1725ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
172667335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
17273c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
1728bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
1729a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
173027e0393fSCarsten Otte 
173127e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
17326ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
173327e0393fSCarsten Otte 
1734e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
1735b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
1736d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
1737b31288faSKonstantin Weitz 
17386692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
1739b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
1740d329c035SChristian Borntraeger }
1741d329c035SChristian Borntraeger 
1742d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
1743d329c035SChristian Borntraeger {
1744d329c035SChristian Borntraeger 	unsigned int i;
1745988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
1746d329c035SChristian Borntraeger 
1747988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
1748988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
1749988a2caeSGleb Natapov 
1750988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
1751988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1752d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
1753988a2caeSGleb Natapov 
1754988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
1755988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
1756d329c035SChristian Borntraeger }
1757d329c035SChristian Borntraeger 
1758b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
1759b0c632dbSHeiko Carstens {
1760d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
17617d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
1762d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
1763c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
176427e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
17656ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
1766841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
176767335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
1768a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
1769*190df4a2SClaudio Imbrenda 	if (kvm->arch.migration_state) {
1770*190df4a2SClaudio Imbrenda 		vfree(kvm->arch.migration_state->pgste_bitmap);
1771*190df4a2SClaudio Imbrenda 		kfree(kvm->arch.migration_state);
1772*190df4a2SClaudio Imbrenda 	}
17738335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
1774b0c632dbSHeiko Carstens }
1775b0c632dbSHeiko Carstens 
1776b0c632dbSHeiko Carstens /* Section: vcpu related */
1777dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1778b0c632dbSHeiko Carstens {
17796ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
178027e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
178127e0393fSCarsten Otte 		return -ENOMEM;
17822c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
1783dafd032aSDominik Dingel 
178427e0393fSCarsten Otte 	return 0;
178527e0393fSCarsten Otte }
178627e0393fSCarsten Otte 
1787a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1788a6e2f683SEugene (jno) Dvurechenski {
1789a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
1790a6940674SDavid Hildenbrand 		return;
17915e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
17927d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
17937d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
17947d43bafcSEugene (jno) Dvurechenski 
17957d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
17967d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
17977d43bafcSEugene (jno) Dvurechenski 	} else {
1798bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1799a6e2f683SEugene (jno) Dvurechenski 
1800a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1801a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
1802a6e2f683SEugene (jno) Dvurechenski 	}
18035e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
18047d43bafcSEugene (jno) Dvurechenski }
1805a6e2f683SEugene (jno) Dvurechenski 
1806eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
1807a6e2f683SEugene (jno) Dvurechenski {
1808a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
1809a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1810a6940674SDavid Hildenbrand 
1811a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
1812a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1813a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1814a6940674SDavid Hildenbrand 	}
1815eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
1816eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
1817eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
18187d43bafcSEugene (jno) Dvurechenski 
1819eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
18207d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
18217d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
18220c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
1823eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
18247d43bafcSEugene (jno) Dvurechenski 	} else {
1825eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1826a6e2f683SEugene (jno) Dvurechenski 
1827eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
1828a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1829a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1830eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1831a6e2f683SEugene (jno) Dvurechenski 	}
1832eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
18335e044315SEugene (jno) Dvurechenski }
18345e044315SEugene (jno) Dvurechenski 
18355e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
18365e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
18375e044315SEugene (jno) Dvurechenski {
18385e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
18395e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
18405e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
18415e044315SEugene (jno) Dvurechenski }
18425e044315SEugene (jno) Dvurechenski 
18435e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
18445e044315SEugene (jno) Dvurechenski {
18455e044315SEugene (jno) Dvurechenski 	int i;
18465e044315SEugene (jno) Dvurechenski 
18475e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
18485e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
18495e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
18505e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
18515e044315SEugene (jno) Dvurechenski }
18525e044315SEugene (jno) Dvurechenski 
18535e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
18545e044315SEugene (jno) Dvurechenski {
18555e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
18565e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
18575e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
18585e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
18595e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
18605e044315SEugene (jno) Dvurechenski 
18615e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
18625e044315SEugene (jno) Dvurechenski 	if (!new_sca)
18635e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
18645e044315SEugene (jno) Dvurechenski 
18655e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
18665e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
18675e044315SEugene (jno) Dvurechenski 
18685e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
18695e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
18705e044315SEugene (jno) Dvurechenski 
18715e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
18725e044315SEugene (jno) Dvurechenski 
18735e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
18745e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
18755e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
18760c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
18775e044315SEugene (jno) Dvurechenski 	}
18785e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
18795e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
18805e044315SEugene (jno) Dvurechenski 
18815e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
18825e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
18835e044315SEugene (jno) Dvurechenski 
18845e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
18855e044315SEugene (jno) Dvurechenski 
18868335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
18878335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
18885e044315SEugene (jno) Dvurechenski 	return 0;
18897d43bafcSEugene (jno) Dvurechenski }
1890a6e2f683SEugene (jno) Dvurechenski 
1891a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1892a6e2f683SEugene (jno) Dvurechenski {
18935e044315SEugene (jno) Dvurechenski 	int rc;
18945e044315SEugene (jno) Dvurechenski 
1895a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
1896a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
1897a6940674SDavid Hildenbrand 			return true;
1898a6940674SDavid Hildenbrand 		return false;
1899a6940674SDavid Hildenbrand 	}
19005e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
19015e044315SEugene (jno) Dvurechenski 		return true;
190276a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
19035e044315SEugene (jno) Dvurechenski 		return false;
19045e044315SEugene (jno) Dvurechenski 
19055e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
19065e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
19075e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
19085e044315SEugene (jno) Dvurechenski 
19095e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
1910a6e2f683SEugene (jno) Dvurechenski }
1911a6e2f683SEugene (jno) Dvurechenski 
1912dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1913dafd032aSDominik Dingel {
1914dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1915dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
191659674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
191759674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
19189eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
1919b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
1920b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
1921b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
192275a4615cSJulius Niedworok 	kvm_s390_set_prefix(vcpu, 0);
1923c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
1924c6e5f166SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
19254e0b1ab7SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 133))
19264e0b1ab7SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
1927f6aa6dc4SDavid Hildenbrand 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
1928f6aa6dc4SDavid Hildenbrand 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
1929f6aa6dc4SDavid Hildenbrand 	 */
1930f6aa6dc4SDavid Hildenbrand 	if (MACHINE_HAS_VX)
193168c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
19326fd8e67dSDavid Hildenbrand 	else
19336fd8e67dSDavid Hildenbrand 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
1934dafd032aSDominik Dingel 
1935dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
1936dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
1937dafd032aSDominik Dingel 
1938b0c632dbSHeiko Carstens 	return 0;
1939b0c632dbSHeiko Carstens }
1940b0c632dbSHeiko Carstens 
1941db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1942db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1943db0758b2SDavid Hildenbrand {
1944db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
19459c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1946db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
19479c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1948db0758b2SDavid Hildenbrand }
1949db0758b2SDavid Hildenbrand 
1950db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1951db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1952db0758b2SDavid Hildenbrand {
1953db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
19549c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1955db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1956db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
19579c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1958db0758b2SDavid Hildenbrand }
1959db0758b2SDavid Hildenbrand 
1960db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1961db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1962db0758b2SDavid Hildenbrand {
1963db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
1964db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
1965db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
1966db0758b2SDavid Hildenbrand }
1967db0758b2SDavid Hildenbrand 
1968db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1969db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1970db0758b2SDavid Hildenbrand {
1971db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
1972db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
1973db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
1974db0758b2SDavid Hildenbrand }
1975db0758b2SDavid Hildenbrand 
1976db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1977db0758b2SDavid Hildenbrand {
1978db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1979db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
1980db0758b2SDavid Hildenbrand 	preempt_enable();
1981db0758b2SDavid Hildenbrand }
1982db0758b2SDavid Hildenbrand 
1983db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1984db0758b2SDavid Hildenbrand {
1985db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1986db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
1987db0758b2SDavid Hildenbrand 	preempt_enable();
1988db0758b2SDavid Hildenbrand }
1989db0758b2SDavid Hildenbrand 
19904287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
19914287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
19924287f247SDavid Hildenbrand {
1993db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
19949c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1995db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
1996db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
19974287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
19989c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1999db0758b2SDavid Hildenbrand 	preempt_enable();
20004287f247SDavid Hildenbrand }
20014287f247SDavid Hildenbrand 
2002db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
20034287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
20044287f247SDavid Hildenbrand {
20059c23a131SDavid Hildenbrand 	unsigned int seq;
2006db0758b2SDavid Hildenbrand 	__u64 value;
2007db0758b2SDavid Hildenbrand 
2008db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
20094287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
2010db0758b2SDavid Hildenbrand 
20119c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
20129c23a131SDavid Hildenbrand 	do {
20139c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
20149c23a131SDavid Hildenbrand 		/*
20159c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
20169c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
20179c23a131SDavid Hildenbrand 		 */
20189c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
2019db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
20209c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
20219c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
2022db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
20239c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
20249c23a131SDavid Hildenbrand 	preempt_enable();
2025db0758b2SDavid Hildenbrand 	return value;
20264287f247SDavid Hildenbrand }
20274287f247SDavid Hildenbrand 
2028b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2029b0c632dbSHeiko Carstens {
20309977e886SHendrik Brueckner 
203137d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
2032805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
20335ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2034db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
203501a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
2036b0c632dbSHeiko Carstens }
2037b0c632dbSHeiko Carstens 
2038b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2039b0c632dbSHeiko Carstens {
204001a745acSDavid Hildenbrand 	vcpu->cpu = -1;
20415ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2042db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
2043805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
204437d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
204537d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
20469977e886SHendrik Brueckner 
2047b0c632dbSHeiko Carstens }
2048b0c632dbSHeiko Carstens 
2049b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
2050b0c632dbSHeiko Carstens {
2051b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
2052b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
2053b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
20548d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
20554287f247SDavid Hildenbrand 	kvm_s390_set_cpu_timer(vcpu, 0);
2056b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
2057b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
2058b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
2059b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
2060b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
20619abc2a08SDavid Hildenbrand 	/* make sure the new fpc will be lazily loaded */
20629abc2a08SDavid Hildenbrand 	save_fpu_regs();
20639abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = 0;
2064b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
2065672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
20663c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
20673c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
20686352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
20696852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
20702ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
2071b0c632dbSHeiko Carstens }
2072b0c632dbSHeiko Carstens 
207331928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
207442897d86SMarcelo Tosatti {
207572f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
2076fdf03650SFan Zhang 	preempt_disable();
207772f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
2078fdf03650SFan Zhang 	preempt_enable();
207972f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
208025508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
2081dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
2082eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
208325508824SDavid Hildenbrand 	}
20846502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
20856502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
208637d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
208737d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
208842897d86SMarcelo Tosatti }
208942897d86SMarcelo Tosatti 
20905102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
20915102ee87STony Krowiak {
20929d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
20935102ee87STony Krowiak 		return;
20945102ee87STony Krowiak 
2095a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
2096a374e892STony Krowiak 
2097a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
2098a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
2099a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
2100a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
2101a374e892STony Krowiak 
21025102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
21035102ee87STony Krowiak }
21045102ee87STony Krowiak 
2105b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
2106b31605c1SDominik Dingel {
2107b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
2108b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
2109b31605c1SDominik Dingel }
2110b31605c1SDominik Dingel 
2111b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
2112b31605c1SDominik Dingel {
2113b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
2114b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
2115b31605c1SDominik Dingel 		return -ENOMEM;
2116b31605c1SDominik Dingel 
21170c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->ecb2 &= ~ECB2_PFMFI;
2118b31605c1SDominik Dingel 	return 0;
2119b31605c1SDominik Dingel }
2120b31605c1SDominik Dingel 
212191520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
212291520f1aSMichael Mueller {
212391520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
212491520f1aSMichael Mueller 
212591520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
212680bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
2127c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
212891520f1aSMichael Mueller }
212991520f1aSMichael Mueller 
2130b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
2131b0c632dbSHeiko Carstens {
2132b31605c1SDominik Dingel 	int rc = 0;
2133b31288faSKonstantin Weitz 
21349e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
21359e6dabefSCornelia Huck 						    CPUSTAT_SM |
2136a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
2137a4a4f191SGuenther Hutzl 
213853df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
2139805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
214053df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
2141805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
2142a4a4f191SGuenther Hutzl 
214391520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
214491520f1aSMichael Mueller 
2145bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2146bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
21470c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
2148bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
21490c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
2150f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
21510c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
21527feb6bb8SMichael Mueller 
2153873b425eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi)
21540c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
2155cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
21560c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
21570c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
215848ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
21590c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
216011ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
21610c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
216237c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
21630c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
216437c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
21650c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
216618280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
21670c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
21680c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
216913211ea7SEric Farman 	}
21704e0b1ab7SFan Zhang 	vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
21714e0b1ab7SFan Zhang 					| SDNXC;
2172c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
2173730cd632SFarhan Ali 
2174730cd632SFarhan Ali 	if (sclp.has_kss)
2175730cd632SFarhan Ali 		atomic_or(CPUSTAT_KSS, &vcpu->arch.sie_block->cpuflags);
2176730cd632SFarhan Ali 	else
2177492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
21785a5e6536SMatthew Rosato 
2179e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
2180b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
2181b31605c1SDominik Dingel 		if (rc)
2182b31605c1SDominik Dingel 			return rc;
2183b31288faSKonstantin Weitz 	}
21840ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2185ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
21869d8d5786SMichael Mueller 
21875102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
21885102ee87STony Krowiak 
2189b31605c1SDominik Dingel 	return rc;
2190b0c632dbSHeiko Carstens }
2191b0c632dbSHeiko Carstens 
2192b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2193b0c632dbSHeiko Carstens 				      unsigned int id)
2194b0c632dbSHeiko Carstens {
21954d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
21967feb6bb8SMichael Mueller 	struct sie_page *sie_page;
21974d47555aSCarsten Otte 	int rc = -EINVAL;
2198b0c632dbSHeiko Carstens 
21994215825eSDavid Hildenbrand 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
22004d47555aSCarsten Otte 		goto out;
22014d47555aSCarsten Otte 
22024d47555aSCarsten Otte 	rc = -ENOMEM;
22034d47555aSCarsten Otte 
2204b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2205b0c632dbSHeiko Carstens 	if (!vcpu)
22064d47555aSCarsten Otte 		goto out;
2207b0c632dbSHeiko Carstens 
22087feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
22097feb6bb8SMichael Mueller 	if (!sie_page)
2210b0c632dbSHeiko Carstens 		goto out_free_cpu;
2211b0c632dbSHeiko Carstens 
22127feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
22137feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
22147feb6bb8SMichael Mueller 
2215efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
2216efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
2217efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
2218efed1104SDavid Hildenbrand 
2219b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
2220ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
2221ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
2222d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
22235288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
22249c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
2225ba5c1e9bSCarsten Otte 
2226b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
2227b0c632dbSHeiko Carstens 	if (rc)
22289abc2a08SDavid Hildenbrand 		goto out_free_sie_block;
22298335713aSChristian Borntraeger 	VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2230b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
2231ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2232b0c632dbSHeiko Carstens 
2233b0c632dbSHeiko Carstens 	return vcpu;
22347b06bf2fSWei Yongjun out_free_sie_block:
22357b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
2236b0c632dbSHeiko Carstens out_free_cpu:
2237b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
22384d47555aSCarsten Otte out:
2239b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
2240b0c632dbSHeiko Carstens }
2241b0c632dbSHeiko Carstens 
2242b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2243b0c632dbSHeiko Carstens {
22449a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
2245b0c632dbSHeiko Carstens }
2246b0c632dbSHeiko Carstens 
224727406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
224849b99e1eSChristian Borntraeger {
2249805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
225061a6df54SDavid Hildenbrand 	exit_sie(vcpu);
225149b99e1eSChristian Borntraeger }
225249b99e1eSChristian Borntraeger 
225327406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
225449b99e1eSChristian Borntraeger {
2255805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
225649b99e1eSChristian Borntraeger }
225749b99e1eSChristian Borntraeger 
22588e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
22598e236546SChristian Borntraeger {
2260805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
226161a6df54SDavid Hildenbrand 	exit_sie(vcpu);
22628e236546SChristian Borntraeger }
22638e236546SChristian Borntraeger 
22648e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
22658e236546SChristian Borntraeger {
22669bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
22678e236546SChristian Borntraeger }
22688e236546SChristian Borntraeger 
226949b99e1eSChristian Borntraeger /*
227049b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
227149b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
227249b99e1eSChristian Borntraeger  * return immediately. */
227349b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
227449b99e1eSChristian Borntraeger {
2275805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
227649b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
227749b99e1eSChristian Borntraeger 		cpu_relax();
227849b99e1eSChristian Borntraeger }
227949b99e1eSChristian Borntraeger 
22808e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
22818e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
228249b99e1eSChristian Borntraeger {
22838e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
22848e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
228549b99e1eSChristian Borntraeger }
228649b99e1eSChristian Borntraeger 
2287414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
2288414d3b07SMartin Schwidefsky 			      unsigned long end)
22892c70fe44SChristian Borntraeger {
22902c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
22912c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
2292414d3b07SMartin Schwidefsky 	unsigned long prefix;
2293414d3b07SMartin Schwidefsky 	int i;
22942c70fe44SChristian Borntraeger 
229565d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
229665d0b0d4SDavid Hildenbrand 		return;
2297414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
2298414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
2299414d3b07SMartin Schwidefsky 		return;
23002c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
23012c70fe44SChristian Borntraeger 		/* match against both prefix pages */
2302414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
2303414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
2304414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
2305414d3b07SMartin Schwidefsky 				   start, end);
23068e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
23072c70fe44SChristian Borntraeger 		}
23082c70fe44SChristian Borntraeger 	}
23092c70fe44SChristian Borntraeger }
23102c70fe44SChristian Borntraeger 
2311b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
2312b6d33834SChristoffer Dall {
2313b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
2314b6d33834SChristoffer Dall 	BUG();
2315b6d33834SChristoffer Dall 	return 0;
2316b6d33834SChristoffer Dall }
2317b6d33834SChristoffer Dall 
231814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
231914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
232014eebd91SCarsten Otte {
232114eebd91SCarsten Otte 	int r = -EINVAL;
232214eebd91SCarsten Otte 
232314eebd91SCarsten Otte 	switch (reg->id) {
232429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
232529b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
232629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
232729b7c71bSCarsten Otte 		break;
232829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
232929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
233029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
233129b7c71bSCarsten Otte 		break;
233246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
23334287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
233446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
233546a6dd1cSJason J. herne 		break;
233646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
233746a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
233846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
233946a6dd1cSJason J. herne 		break;
2340536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2341536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
2342536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2343536336c2SDominik Dingel 		break;
2344536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2345536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
2346536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2347536336c2SDominik Dingel 		break;
2348536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2349536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
2350536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2351536336c2SDominik Dingel 		break;
2352672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2353672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
2354672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2355672550fbSChristian Borntraeger 		break;
2356afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2357afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
2358afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2359afa45ff5SChristian Borntraeger 		break;
236014eebd91SCarsten Otte 	default:
236114eebd91SCarsten Otte 		break;
236214eebd91SCarsten Otte 	}
236314eebd91SCarsten Otte 
236414eebd91SCarsten Otte 	return r;
236514eebd91SCarsten Otte }
236614eebd91SCarsten Otte 
236714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
236814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
236914eebd91SCarsten Otte {
237014eebd91SCarsten Otte 	int r = -EINVAL;
23714287f247SDavid Hildenbrand 	__u64 val;
237214eebd91SCarsten Otte 
237314eebd91SCarsten Otte 	switch (reg->id) {
237429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
237529b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
237629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
237729b7c71bSCarsten Otte 		break;
237829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
237929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
238029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
238129b7c71bSCarsten Otte 		break;
238246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
23834287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
23844287f247SDavid Hildenbrand 		if (!r)
23854287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
238646a6dd1cSJason J. herne 		break;
238746a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
238846a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
238946a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
239046a6dd1cSJason J. herne 		break;
2391536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2392536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
2393536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
23949fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
23959fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2396536336c2SDominik Dingel 		break;
2397536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2398536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
2399536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2400536336c2SDominik Dingel 		break;
2401536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2402536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
2403536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2404536336c2SDominik Dingel 		break;
2405672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2406672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
2407672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2408672550fbSChristian Borntraeger 		break;
2409afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2410afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
2411afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2412afa45ff5SChristian Borntraeger 		break;
241314eebd91SCarsten Otte 	default:
241414eebd91SCarsten Otte 		break;
241514eebd91SCarsten Otte 	}
241614eebd91SCarsten Otte 
241714eebd91SCarsten Otte 	return r;
241814eebd91SCarsten Otte }
2419b6d33834SChristoffer Dall 
2420b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
2421b0c632dbSHeiko Carstens {
2422b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
2423b0c632dbSHeiko Carstens 	return 0;
2424b0c632dbSHeiko Carstens }
2425b0c632dbSHeiko Carstens 
2426b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2427b0c632dbSHeiko Carstens {
24285a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
2429b0c632dbSHeiko Carstens 	return 0;
2430b0c632dbSHeiko Carstens }
2431b0c632dbSHeiko Carstens 
2432b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2433b0c632dbSHeiko Carstens {
24345a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
2435b0c632dbSHeiko Carstens 	return 0;
2436b0c632dbSHeiko Carstens }
2437b0c632dbSHeiko Carstens 
2438b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2439b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2440b0c632dbSHeiko Carstens {
244159674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
2442b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
2443b0c632dbSHeiko Carstens 	return 0;
2444b0c632dbSHeiko Carstens }
2445b0c632dbSHeiko Carstens 
2446b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2447b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2448b0c632dbSHeiko Carstens {
244959674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
2450b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
2451b0c632dbSHeiko Carstens 	return 0;
2452b0c632dbSHeiko Carstens }
2453b0c632dbSHeiko Carstens 
2454b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2455b0c632dbSHeiko Carstens {
24564725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
24574725c860SMartin Schwidefsky 		return -EINVAL;
2458e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
24599abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2460a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
2461a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
24629abc2a08SDavid Hildenbrand 	else
2463a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
2464b0c632dbSHeiko Carstens 	return 0;
2465b0c632dbSHeiko Carstens }
2466b0c632dbSHeiko Carstens 
2467b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2468b0c632dbSHeiko Carstens {
24699abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
24709abc2a08SDavid Hildenbrand 	save_fpu_regs();
24719abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2472a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
2473a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
24749abc2a08SDavid Hildenbrand 	else
2475a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
2476e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
2477b0c632dbSHeiko Carstens 	return 0;
2478b0c632dbSHeiko Carstens }
2479b0c632dbSHeiko Carstens 
2480b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
2481b0c632dbSHeiko Carstens {
2482b0c632dbSHeiko Carstens 	int rc = 0;
2483b0c632dbSHeiko Carstens 
24847a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
2485b0c632dbSHeiko Carstens 		rc = -EBUSY;
2486d7b0b5ebSCarsten Otte 	else {
2487d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
2488d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
2489d7b0b5ebSCarsten Otte 	}
2490b0c632dbSHeiko Carstens 	return rc;
2491b0c632dbSHeiko Carstens }
2492b0c632dbSHeiko Carstens 
2493b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2494b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
2495b0c632dbSHeiko Carstens {
2496b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
2497b0c632dbSHeiko Carstens }
2498b0c632dbSHeiko Carstens 
249927291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
250027291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
250127291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
250227291e21SDavid Hildenbrand 
2503d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2504d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
2505b0c632dbSHeiko Carstens {
250627291e21SDavid Hildenbrand 	int rc = 0;
250727291e21SDavid Hildenbrand 
250827291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
250927291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
251027291e21SDavid Hildenbrand 
25112de3bfc2SDavid Hildenbrand 	if (dbg->control & ~VALID_GUESTDBG_FLAGS)
251227291e21SDavid Hildenbrand 		return -EINVAL;
251389b5b4deSDavid Hildenbrand 	if (!sclp.has_gpere)
251489b5b4deSDavid Hildenbrand 		return -EINVAL;
251527291e21SDavid Hildenbrand 
251627291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
251727291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
251827291e21SDavid Hildenbrand 		/* enforce guest PER */
2519805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
252027291e21SDavid Hildenbrand 
252127291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
252227291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
252327291e21SDavid Hildenbrand 	} else {
2524805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
252527291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
252627291e21SDavid Hildenbrand 	}
252727291e21SDavid Hildenbrand 
252827291e21SDavid Hildenbrand 	if (rc) {
252927291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
253027291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
2531805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
253227291e21SDavid Hildenbrand 	}
253327291e21SDavid Hildenbrand 
253427291e21SDavid Hildenbrand 	return rc;
2535b0c632dbSHeiko Carstens }
2536b0c632dbSHeiko Carstens 
253762d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
253862d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
253962d9f0dbSMarcelo Tosatti {
25406352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
25416352e4d2SDavid Hildenbrand 	return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
25426352e4d2SDavid Hildenbrand 				       KVM_MP_STATE_OPERATING;
254362d9f0dbSMarcelo Tosatti }
254462d9f0dbSMarcelo Tosatti 
254562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
254662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
254762d9f0dbSMarcelo Tosatti {
25486352e4d2SDavid Hildenbrand 	int rc = 0;
25496352e4d2SDavid Hildenbrand 
25506352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
25516352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
25526352e4d2SDavid Hildenbrand 
25536352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
25546352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
25556352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
25566352e4d2SDavid Hildenbrand 		break;
25576352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
25586352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
25596352e4d2SDavid Hildenbrand 		break;
25606352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
25616352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
25626352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
25636352e4d2SDavid Hildenbrand 	default:
25646352e4d2SDavid Hildenbrand 		rc = -ENXIO;
25656352e4d2SDavid Hildenbrand 	}
25666352e4d2SDavid Hildenbrand 
25676352e4d2SDavid Hildenbrand 	return rc;
256862d9f0dbSMarcelo Tosatti }
256962d9f0dbSMarcelo Tosatti 
25708ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
25718ad35755SDavid Hildenbrand {
25728ad35755SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
25738ad35755SDavid Hildenbrand }
25748ad35755SDavid Hildenbrand 
25752c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
25762c70fe44SChristian Borntraeger {
25778ad35755SDavid Hildenbrand retry:
25788e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
2579586b7ccdSChristian Borntraeger 	if (!vcpu->requests)
2580586b7ccdSChristian Borntraeger 		return 0;
25812c70fe44SChristian Borntraeger 	/*
25822c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2583b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
25842c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
25852c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
25862c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
25872c70fe44SChristian Borntraeger 	 */
25888ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
25892c70fe44SChristian Borntraeger 		int rc;
2590b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
2591fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
2592b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
2593aca411a4SJulius Niedworok 		if (rc) {
2594aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
25952c70fe44SChristian Borntraeger 			return rc;
2596aca411a4SJulius Niedworok 		}
25978ad35755SDavid Hildenbrand 		goto retry;
25982c70fe44SChristian Borntraeger 	}
25998ad35755SDavid Hildenbrand 
2600d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2601d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
2602d3d692c8SDavid Hildenbrand 		goto retry;
2603d3d692c8SDavid Hildenbrand 	}
2604d3d692c8SDavid Hildenbrand 
26058ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
26068ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
26078ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
2608805de8f4SPeter Zijlstra 			atomic_or(CPUSTAT_IBS,
26098ad35755SDavid Hildenbrand 					&vcpu->arch.sie_block->cpuflags);
26108ad35755SDavid Hildenbrand 		}
26118ad35755SDavid Hildenbrand 		goto retry;
26128ad35755SDavid Hildenbrand 	}
26138ad35755SDavid Hildenbrand 
26148ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
26158ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
26168ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
2617805de8f4SPeter Zijlstra 			atomic_andnot(CPUSTAT_IBS,
26188ad35755SDavid Hildenbrand 					  &vcpu->arch.sie_block->cpuflags);
26198ad35755SDavid Hildenbrand 		}
26208ad35755SDavid Hildenbrand 		goto retry;
26218ad35755SDavid Hildenbrand 	}
26228ad35755SDavid Hildenbrand 
26236502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
26246502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
26256502a34cSDavid Hildenbrand 		goto retry;
26266502a34cSDavid Hildenbrand 	}
26276502a34cSDavid Hildenbrand 
2628*190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
2629*190df4a2SClaudio Imbrenda 		/*
2630*190df4a2SClaudio Imbrenda 		 * Disable CMMA virtualization; we will emulate the ESSA
2631*190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
2632*190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
2633*190df4a2SClaudio Imbrenda 		 */
2634*190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
2635*190df4a2SClaudio Imbrenda 		goto retry;
2636*190df4a2SClaudio Imbrenda 	}
2637*190df4a2SClaudio Imbrenda 
2638*190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
2639*190df4a2SClaudio Imbrenda 		/*
2640*190df4a2SClaudio Imbrenda 		 * Re-enable CMMA virtualization if CMMA is available and
2641*190df4a2SClaudio Imbrenda 		 * was used.
2642*190df4a2SClaudio Imbrenda 		 */
2643*190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
2644*190df4a2SClaudio Imbrenda 		    (vcpu->kvm->mm->context.use_cmma))
2645*190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
2646*190df4a2SClaudio Imbrenda 		goto retry;
2647*190df4a2SClaudio Imbrenda 	}
2648*190df4a2SClaudio Imbrenda 
26490759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
265072875d8aSRadim Krčmář 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
26510759d068SDavid Hildenbrand 
26522c70fe44SChristian Borntraeger 	return 0;
26532c70fe44SChristian Borntraeger }
26542c70fe44SChristian Borntraeger 
265525ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
265625ed1675SDavid Hildenbrand {
265725ed1675SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
265825ed1675SDavid Hildenbrand 	int i;
265925ed1675SDavid Hildenbrand 
266025ed1675SDavid Hildenbrand 	mutex_lock(&kvm->lock);
266125ed1675SDavid Hildenbrand 	preempt_disable();
266225ed1675SDavid Hildenbrand 	kvm->arch.epoch = tod - get_tod_clock();
266325ed1675SDavid Hildenbrand 	kvm_s390_vcpu_block_all(kvm);
266425ed1675SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm)
266525ed1675SDavid Hildenbrand 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
266625ed1675SDavid Hildenbrand 	kvm_s390_vcpu_unblock_all(kvm);
266725ed1675SDavid Hildenbrand 	preempt_enable();
266825ed1675SDavid Hildenbrand 	mutex_unlock(&kvm->lock);
266925ed1675SDavid Hildenbrand }
267025ed1675SDavid Hildenbrand 
2671fa576c58SThomas Huth /**
2672fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
2673fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
2674fa576c58SThomas Huth  * @gpa: Guest physical address
2675fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
2676fa576c58SThomas Huth  *
2677fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
2678fa576c58SThomas Huth  *
2679fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
2680fa576c58SThomas Huth  */
2681fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
268224eb3a82SDominik Dingel {
2683527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
2684527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
268524eb3a82SDominik Dingel }
268624eb3a82SDominik Dingel 
26873c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
26883c038e6bSDominik Dingel 				      unsigned long token)
26893c038e6bSDominik Dingel {
26903c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
2691383d0b05SJens Freimann 	struct kvm_s390_irq irq;
26923c038e6bSDominik Dingel 
26933c038e6bSDominik Dingel 	if (start_token) {
2694383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
2695383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
2696383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
26973c038e6bSDominik Dingel 	} else {
26983c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
2699383d0b05SJens Freimann 		inti.parm64 = token;
27003c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
27013c038e6bSDominik Dingel 	}
27023c038e6bSDominik Dingel }
27033c038e6bSDominik Dingel 
27043c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
27053c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
27063c038e6bSDominik Dingel {
27073c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
27083c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
27093c038e6bSDominik Dingel }
27103c038e6bSDominik Dingel 
27113c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
27123c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
27133c038e6bSDominik Dingel {
27143c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
27153c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
27163c038e6bSDominik Dingel }
27173c038e6bSDominik Dingel 
27183c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
27193c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
27203c038e6bSDominik Dingel {
27213c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
27223c038e6bSDominik Dingel }
27233c038e6bSDominik Dingel 
27243c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
27253c038e6bSDominik Dingel {
27263c038e6bSDominik Dingel 	/*
27273c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
27283c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
27293c038e6bSDominik Dingel 	 */
27303c038e6bSDominik Dingel 	return true;
27313c038e6bSDominik Dingel }
27323c038e6bSDominik Dingel 
27333c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
27343c038e6bSDominik Dingel {
27353c038e6bSDominik Dingel 	hva_t hva;
27363c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
27373c038e6bSDominik Dingel 	int rc;
27383c038e6bSDominik Dingel 
27393c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
27403c038e6bSDominik Dingel 		return 0;
27413c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
27423c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
27433c038e6bSDominik Dingel 		return 0;
27443c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
27453c038e6bSDominik Dingel 		return 0;
27469a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
27473c038e6bSDominik Dingel 		return 0;
27483c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
27493c038e6bSDominik Dingel 		return 0;
27503c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
27513c038e6bSDominik Dingel 		return 0;
27523c038e6bSDominik Dingel 
275381480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
275481480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
275581480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
27563c038e6bSDominik Dingel 		return 0;
27573c038e6bSDominik Dingel 
27583c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
27593c038e6bSDominik Dingel 	return rc;
27603c038e6bSDominik Dingel }
27613c038e6bSDominik Dingel 
27623fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
2763b0c632dbSHeiko Carstens {
27643fb4c40fSThomas Huth 	int rc, cpuflags;
2765e168bf8dSCarsten Otte 
27663c038e6bSDominik Dingel 	/*
27673c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
27683c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
27693c038e6bSDominik Dingel 	 * handled outside the worker.
27703c038e6bSDominik Dingel 	 */
27713c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
27723c038e6bSDominik Dingel 
27737ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
27747ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
2775b0c632dbSHeiko Carstens 
2776b0c632dbSHeiko Carstens 	if (need_resched())
2777b0c632dbSHeiko Carstens 		schedule();
2778b0c632dbSHeiko Carstens 
2779d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
278071cde587SChristian Borntraeger 		s390_handle_mcck();
278171cde587SChristian Borntraeger 
278279395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
278379395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
278479395031SJens Freimann 		if (rc)
278579395031SJens Freimann 			return rc;
278679395031SJens Freimann 	}
27870ff31867SCarsten Otte 
27882c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
27892c70fe44SChristian Borntraeger 	if (rc)
27902c70fe44SChristian Borntraeger 		return rc;
27912c70fe44SChristian Borntraeger 
279227291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
279327291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
279427291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
279527291e21SDavid Hildenbrand 	}
279627291e21SDavid Hildenbrand 
2797b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
27983fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
27993fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
28003fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
28012b29a9fdSDominik Dingel 
28023fb4c40fSThomas Huth 	return 0;
28033fb4c40fSThomas Huth }
28043fb4c40fSThomas Huth 
2805492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2806492d8642SThomas Huth {
280756317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
280856317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
280956317920SDavid Hildenbrand 	};
281056317920SDavid Hildenbrand 	u8 opcode, ilen;
2811492d8642SThomas Huth 	int rc;
2812492d8642SThomas Huth 
2813492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2814492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
2815492d8642SThomas Huth 
2816492d8642SThomas Huth 	/*
2817492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
2818492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
2819492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
2820492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
2821492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
2822492d8642SThomas Huth 	 * to be able to forward the PSW.
2823492d8642SThomas Huth 	 */
28243fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
282556317920SDavid Hildenbrand 	ilen = insn_length(opcode);
28269b0d721aSDavid Hildenbrand 	if (rc < 0) {
28279b0d721aSDavid Hildenbrand 		return rc;
28289b0d721aSDavid Hildenbrand 	} else if (rc) {
28299b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
28309b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
28319b0d721aSDavid Hildenbrand 		 * nullification if necessary.
28329b0d721aSDavid Hildenbrand 		 */
28339b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
28349b0d721aSDavid Hildenbrand 		ilen = 4;
28359b0d721aSDavid Hildenbrand 	}
283656317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
283756317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
283856317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
2839492d8642SThomas Huth }
2840492d8642SThomas Huth 
28413fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
28423fb4c40fSThomas Huth {
28432b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
28442b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
28452b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
28462b29a9fdSDominik Dingel 
284727291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
284827291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
284927291e21SDavid Hildenbrand 
28507ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
28517ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
285271f116bfSDavid Hildenbrand 
285371f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
285471f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
285571f116bfSDavid Hildenbrand 
285671f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
285771f116bfSDavid Hildenbrand 			return rc;
285871f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
285971f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
286071f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
286171f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
286271f116bfSDavid Hildenbrand 		return -EREMOTE;
286371f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
286471f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
286571f116bfSDavid Hildenbrand 		return 0;
2866210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
2867210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2868210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
2869210b1607SThomas Huth 						current->thread.gmap_addr;
2870210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
287171f116bfSDavid Hildenbrand 		return -EREMOTE;
287224eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
28733c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
287424eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
287571f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
287671f116bfSDavid Hildenbrand 			return 0;
287771f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
2878fa576c58SThomas Huth 	}
287971f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
28803fb4c40fSThomas Huth }
28813fb4c40fSThomas Huth 
28823fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
28833fb4c40fSThomas Huth {
28843fb4c40fSThomas Huth 	int rc, exit_reason;
28853fb4c40fSThomas Huth 
2886800c1065SThomas Huth 	/*
2887800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2888800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
2889800c1065SThomas Huth 	 */
2890800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2891800c1065SThomas Huth 
2892a76ccff6SThomas Huth 	do {
28933fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
28943fb4c40fSThomas Huth 		if (rc)
2895a76ccff6SThomas Huth 			break;
28963fb4c40fSThomas Huth 
2897800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
28983fb4c40fSThomas Huth 		/*
2899a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
2900a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
29013fb4c40fSThomas Huth 		 */
29020097d12eSChristian Borntraeger 		local_irq_disable();
29036edaa530SPaolo Bonzini 		guest_enter_irqoff();
2904db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
29050097d12eSChristian Borntraeger 		local_irq_enable();
2906a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
2907a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
29080097d12eSChristian Borntraeger 		local_irq_disable();
2909db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
29106edaa530SPaolo Bonzini 		guest_exit_irqoff();
29110097d12eSChristian Borntraeger 		local_irq_enable();
2912800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
29133fb4c40fSThomas Huth 
29143fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
291527291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
29163fb4c40fSThomas Huth 
2917800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2918e168bf8dSCarsten Otte 	return rc;
2919b0c632dbSHeiko Carstens }
2920b0c632dbSHeiko Carstens 
2921b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2922b028ee3eSDavid Hildenbrand {
29234d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
29244e0b1ab7SFan Zhang 	struct gs_cb *gscb;
29254d5f2c04SChristian Borntraeger 
29264d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
29274e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
2928b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2929b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2930b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2931b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2932b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2933b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
2934d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
2935d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2936b028ee3eSDavid Hildenbrand 	}
2937b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
29384287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
2939b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2940b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2941b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2942b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2943b028ee3eSDavid Hildenbrand 	}
2944b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2945b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2946b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2947b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
29489fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
29499fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2950b028ee3eSDavid Hildenbrand 	}
295180cd8763SFan Zhang 	/*
295280cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
295380cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
295480cd8763SFan Zhang 	 */
295580cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
29564d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
29574d5f2c04SChristian Borntraeger 	    riccb->valid &&
29580c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
29594d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
29600c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
296180cd8763SFan Zhang 	}
29624e0b1ab7SFan Zhang 	/*
29634e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
29644e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
29654e0b1ab7SFan Zhang 	 */
29664e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
29674e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
29684e0b1ab7SFan Zhang 	    gscb->gssm &&
29694e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
29704e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
29714e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
29724e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
29734e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
297480cd8763SFan Zhang 	}
297531d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->arch.host_acrs);
297631d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
2977e1788bb9SChristian Borntraeger 	/* save host (userspace) fprs/vrs */
2978e1788bb9SChristian Borntraeger 	save_fpu_regs();
2979e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
2980e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
2981e1788bb9SChristian Borntraeger 	if (MACHINE_HAS_VX)
2982e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
2983e1788bb9SChristian Borntraeger 	else
2984e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
2985e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
2986e1788bb9SChristian Borntraeger 	if (test_fp_ctl(current->thread.fpu.fpc))
2987e1788bb9SChristian Borntraeger 		/* User space provided an invalid FPC, let's clear it */
2988e1788bb9SChristian Borntraeger 		current->thread.fpu.fpc = 0;
29894e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
29904e0b1ab7SFan Zhang 		preempt_disable();
29914e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
29924e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
29934e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
29944e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
29954e0b1ab7SFan Zhang 		}
29964e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
29974e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
29984e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
29994e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
30004e0b1ab7SFan Zhang 		}
30014e0b1ab7SFan Zhang 		preempt_enable();
30024e0b1ab7SFan Zhang 	}
300380cd8763SFan Zhang 
3004b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
3005b028ee3eSDavid Hildenbrand }
3006b028ee3eSDavid Hildenbrand 
3007b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3008b028ee3eSDavid Hildenbrand {
3009b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
3010b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
3011b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
3012b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
30134287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
3014b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
3015b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
3016b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
3017b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
3018b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
3019b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
3020b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
302131d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
302231d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->arch.host_acrs);
3023e1788bb9SChristian Borntraeger 	/* Save guest register state */
3024e1788bb9SChristian Borntraeger 	save_fpu_regs();
3025e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3026e1788bb9SChristian Borntraeger 	/* Restore will be done lazily at return */
3027e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
3028e1788bb9SChristian Borntraeger 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
30294e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
30304e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
30314e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
30324e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
30334e0b1ab7SFan Zhang 		preempt_disable();
30344e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
30354e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
30364e0b1ab7SFan Zhang 		preempt_enable();
30374e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
30384e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
30394e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
30404e0b1ab7SFan Zhang 	}
3041e1788bb9SChristian Borntraeger 
3042b028ee3eSDavid Hildenbrand }
3043b028ee3eSDavid Hildenbrand 
3044b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3045b0c632dbSHeiko Carstens {
30468f2abe6aSChristian Borntraeger 	int rc;
3047b0c632dbSHeiko Carstens 	sigset_t sigsaved;
3048b0c632dbSHeiko Carstens 
3049460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
3050460df4c1SPaolo Bonzini 		return -EINTR;
3051460df4c1SPaolo Bonzini 
305227291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
305327291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
305427291e21SDavid Hildenbrand 		return 0;
305527291e21SDavid Hildenbrand 	}
305627291e21SDavid Hildenbrand 
3057b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
3058b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
3059b0c632dbSHeiko Carstens 
30606352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
30616852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
30626352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
3063ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
30646352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
30656352e4d2SDavid Hildenbrand 		return -EINVAL;
30666352e4d2SDavid Hildenbrand 	}
3067b0c632dbSHeiko Carstens 
3068b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
3069db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
3070d7b0b5ebSCarsten Otte 
3071dab4079dSHeiko Carstens 	might_fault();
3072e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
30739ace903dSChristian Ehrhardt 
3074b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
3075b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
30768f2abe6aSChristian Borntraeger 		rc = -EINTR;
3077b1d16c49SChristian Ehrhardt 	}
30788f2abe6aSChristian Borntraeger 
307927291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
308027291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
308127291e21SDavid Hildenbrand 		rc = 0;
308227291e21SDavid Hildenbrand 	}
308327291e21SDavid Hildenbrand 
30848f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
308571f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
30868f2abe6aSChristian Borntraeger 		rc = 0;
30878f2abe6aSChristian Borntraeger 	}
30888f2abe6aSChristian Borntraeger 
3089db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
3090b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
3091d7b0b5ebSCarsten Otte 
3092b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
3093b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
3094b0c632dbSHeiko Carstens 
3095b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
30967e8e6ab4SHeiko Carstens 	return rc;
3097b0c632dbSHeiko Carstens }
3098b0c632dbSHeiko Carstens 
3099b0c632dbSHeiko Carstens /*
3100b0c632dbSHeiko Carstens  * store status at address
3101b0c632dbSHeiko Carstens  * we use have two special cases:
3102b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3103b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3104b0c632dbSHeiko Carstens  */
3105d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
3106b0c632dbSHeiko Carstens {
3107092670cdSCarsten Otte 	unsigned char archmode = 1;
31089abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
3109fda902cbSMichael Mueller 	unsigned int px;
31104287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
3111d0bce605SHeiko Carstens 	int rc;
3112b0c632dbSHeiko Carstens 
3113d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
3114d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
3115d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
3116b0c632dbSHeiko Carstens 			return -EFAULT;
3117d9a3a09aSMartin Schwidefsky 		gpa = 0;
3118d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
3119d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
3120b0c632dbSHeiko Carstens 			return -EFAULT;
3121d9a3a09aSMartin Schwidefsky 		gpa = px;
3122d9a3a09aSMartin Schwidefsky 	} else
3123d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
31249abc2a08SDavid Hildenbrand 
31259abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
31269abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
31279522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
3128d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
31299abc2a08SDavid Hildenbrand 				     fprs, 128);
31309abc2a08SDavid Hildenbrand 	} else {
31319abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
31326fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
31339abc2a08SDavid Hildenbrand 	}
3134d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
3135d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
3136d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
3137d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
3138d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
3139fda902cbSMichael Mueller 			      &px, 4);
3140d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
31419abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
3142d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
3143d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
31444287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
3145d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
31464287f247SDavid Hildenbrand 			      &cputm, 8);
3147178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
3148d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
3149d0bce605SHeiko Carstens 			      &clkcomp, 8);
3150d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
3151d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
3152d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
3153d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
3154d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
3155b0c632dbSHeiko Carstens }
3156b0c632dbSHeiko Carstens 
3157e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
3158e879892cSThomas Huth {
3159e879892cSThomas Huth 	/*
3160e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
316131d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
3162e879892cSThomas Huth 	 * it into the save area
3163e879892cSThomas Huth 	 */
3164d0164ee2SHendrik Brueckner 	save_fpu_regs();
31659abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3166e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
3167e879892cSThomas Huth 
3168e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
3169e879892cSThomas Huth }
3170e879892cSThomas Huth 
31718ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
31728ad35755SDavid Hildenbrand {
31738ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
31748e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
31758ad35755SDavid Hildenbrand }
31768ad35755SDavid Hildenbrand 
31778ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
31788ad35755SDavid Hildenbrand {
31798ad35755SDavid Hildenbrand 	unsigned int i;
31808ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
31818ad35755SDavid Hildenbrand 
31828ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
31838ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
31848ad35755SDavid Hildenbrand 	}
31858ad35755SDavid Hildenbrand }
31868ad35755SDavid Hildenbrand 
31878ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
31888ad35755SDavid Hildenbrand {
318909a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
319009a400e7SDavid Hildenbrand 		return;
31918ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
31928e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
31938ad35755SDavid Hildenbrand }
31948ad35755SDavid Hildenbrand 
31956852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
31966852d7b6SDavid Hildenbrand {
31978ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
31988ad35755SDavid Hildenbrand 
31998ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
32008ad35755SDavid Hildenbrand 		return;
32018ad35755SDavid Hildenbrand 
32026852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
32038ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3204433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
32058ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
32068ad35755SDavid Hildenbrand 
32078ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
32088ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
32098ad35755SDavid Hildenbrand 			started_vcpus++;
32108ad35755SDavid Hildenbrand 	}
32118ad35755SDavid Hildenbrand 
32128ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
32138ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
32148ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
32158ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
32168ad35755SDavid Hildenbrand 		/*
32178ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
32188ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
32198ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
32208ad35755SDavid Hildenbrand 		 */
32218ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
32228ad35755SDavid Hildenbrand 	}
32238ad35755SDavid Hildenbrand 
3224805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
32258ad35755SDavid Hildenbrand 	/*
32268ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
32278ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
32288ad35755SDavid Hildenbrand 	 */
3229d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3230433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
32318ad35755SDavid Hildenbrand 	return;
32326852d7b6SDavid Hildenbrand }
32336852d7b6SDavid Hildenbrand 
32346852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
32356852d7b6SDavid Hildenbrand {
32368ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
32378ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
32388ad35755SDavid Hildenbrand 
32398ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
32408ad35755SDavid Hildenbrand 		return;
32418ad35755SDavid Hildenbrand 
32426852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
32438ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3244433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
32458ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
32468ad35755SDavid Hildenbrand 
324732f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
32486cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
324932f5ff63SDavid Hildenbrand 
3250805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
32518ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
32528ad35755SDavid Hildenbrand 
32538ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
32548ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
32558ad35755SDavid Hildenbrand 			started_vcpus++;
32568ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
32578ad35755SDavid Hildenbrand 		}
32588ad35755SDavid Hildenbrand 	}
32598ad35755SDavid Hildenbrand 
32608ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
32618ad35755SDavid Hildenbrand 		/*
32628ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
32638ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
32648ad35755SDavid Hildenbrand 		 */
32658ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
32668ad35755SDavid Hildenbrand 	}
32678ad35755SDavid Hildenbrand 
3268433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
32698ad35755SDavid Hildenbrand 	return;
32706852d7b6SDavid Hildenbrand }
32716852d7b6SDavid Hildenbrand 
3272d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3273d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
3274d6712df9SCornelia Huck {
3275d6712df9SCornelia Huck 	int r;
3276d6712df9SCornelia Huck 
3277d6712df9SCornelia Huck 	if (cap->flags)
3278d6712df9SCornelia Huck 		return -EINVAL;
3279d6712df9SCornelia Huck 
3280d6712df9SCornelia Huck 	switch (cap->cap) {
3281fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
3282fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
3283fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
3284c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
3285fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
3286fa6b7fe9SCornelia Huck 		}
3287fa6b7fe9SCornelia Huck 		r = 0;
3288fa6b7fe9SCornelia Huck 		break;
3289d6712df9SCornelia Huck 	default:
3290d6712df9SCornelia Huck 		r = -EINVAL;
3291d6712df9SCornelia Huck 		break;
3292d6712df9SCornelia Huck 	}
3293d6712df9SCornelia Huck 	return r;
3294d6712df9SCornelia Huck }
3295d6712df9SCornelia Huck 
329641408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
329741408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
329841408c28SThomas Huth {
329941408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
330041408c28SThomas Huth 	void *tmpbuf = NULL;
330141408c28SThomas Huth 	int r, srcu_idx;
330241408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
330341408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
330441408c28SThomas Huth 
330541408c28SThomas Huth 	if (mop->flags & ~supported_flags)
330641408c28SThomas Huth 		return -EINVAL;
330741408c28SThomas Huth 
330841408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
330941408c28SThomas Huth 		return -E2BIG;
331041408c28SThomas Huth 
331141408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
331241408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
331341408c28SThomas Huth 		if (!tmpbuf)
331441408c28SThomas Huth 			return -ENOMEM;
331541408c28SThomas Huth 	}
331641408c28SThomas Huth 
331741408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
331841408c28SThomas Huth 
331941408c28SThomas Huth 	switch (mop->op) {
332041408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
332141408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
332292c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
332392c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
332441408c28SThomas Huth 			break;
332541408c28SThomas Huth 		}
332641408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
332741408c28SThomas Huth 		if (r == 0) {
332841408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
332941408c28SThomas Huth 				r = -EFAULT;
333041408c28SThomas Huth 		}
333141408c28SThomas Huth 		break;
333241408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
333341408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
333492c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
333592c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
333641408c28SThomas Huth 			break;
333741408c28SThomas Huth 		}
333841408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
333941408c28SThomas Huth 			r = -EFAULT;
334041408c28SThomas Huth 			break;
334141408c28SThomas Huth 		}
334241408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
334341408c28SThomas Huth 		break;
334441408c28SThomas Huth 	default:
334541408c28SThomas Huth 		r = -EINVAL;
334641408c28SThomas Huth 	}
334741408c28SThomas Huth 
334841408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
334941408c28SThomas Huth 
335041408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
335141408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
335241408c28SThomas Huth 
335341408c28SThomas Huth 	vfree(tmpbuf);
335441408c28SThomas Huth 	return r;
335541408c28SThomas Huth }
335641408c28SThomas Huth 
3357b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
3358b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
3359b0c632dbSHeiko Carstens {
3360b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
3361b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
3362800c1065SThomas Huth 	int idx;
3363bc923cc9SAvi Kivity 	long r;
3364b0c632dbSHeiko Carstens 
336593736624SAvi Kivity 	switch (ioctl) {
336647b43c52SJens Freimann 	case KVM_S390_IRQ: {
336747b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
336847b43c52SJens Freimann 
336947b43c52SJens Freimann 		r = -EFAULT;
337047b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
337147b43c52SJens Freimann 			break;
337247b43c52SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
337347b43c52SJens Freimann 		break;
337447b43c52SJens Freimann 	}
337593736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
3376ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
3377383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
3378ba5c1e9bSCarsten Otte 
337993736624SAvi Kivity 		r = -EFAULT;
3380ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
338193736624SAvi Kivity 			break;
3382383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
3383383d0b05SJens Freimann 			return -EINVAL;
3384383d0b05SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
338593736624SAvi Kivity 		break;
3386ba5c1e9bSCarsten Otte 	}
3387b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
3388800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
3389bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
3390800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
3391bc923cc9SAvi Kivity 		break;
3392b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
3393b0c632dbSHeiko Carstens 		psw_t psw;
3394b0c632dbSHeiko Carstens 
3395bc923cc9SAvi Kivity 		r = -EFAULT;
3396b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
3397bc923cc9SAvi Kivity 			break;
3398bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
3399bc923cc9SAvi Kivity 		break;
3400b0c632dbSHeiko Carstens 	}
3401b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
3402bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3403bc923cc9SAvi Kivity 		break;
340414eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
340514eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
340614eebd91SCarsten Otte 		struct kvm_one_reg reg;
340714eebd91SCarsten Otte 		r = -EFAULT;
340814eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
340914eebd91SCarsten Otte 			break;
341014eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
341114eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
341214eebd91SCarsten Otte 		else
341314eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
341414eebd91SCarsten Otte 		break;
341514eebd91SCarsten Otte 	}
341627e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
341727e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
341827e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
341927e0393fSCarsten Otte 
342027e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
342127e0393fSCarsten Otte 			r = -EFAULT;
342227e0393fSCarsten Otte 			break;
342327e0393fSCarsten Otte 		}
342427e0393fSCarsten Otte 
342527e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
342627e0393fSCarsten Otte 			r = -EINVAL;
342727e0393fSCarsten Otte 			break;
342827e0393fSCarsten Otte 		}
342927e0393fSCarsten Otte 
343027e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
343127e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
343227e0393fSCarsten Otte 		break;
343327e0393fSCarsten Otte 	}
343427e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
343527e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
343627e0393fSCarsten Otte 
343727e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
343827e0393fSCarsten Otte 			r = -EFAULT;
343927e0393fSCarsten Otte 			break;
344027e0393fSCarsten Otte 		}
344127e0393fSCarsten Otte 
344227e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
344327e0393fSCarsten Otte 			r = -EINVAL;
344427e0393fSCarsten Otte 			break;
344527e0393fSCarsten Otte 		}
344627e0393fSCarsten Otte 
344727e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
344827e0393fSCarsten Otte 			ucasmap.length);
344927e0393fSCarsten Otte 		break;
345027e0393fSCarsten Otte 	}
345127e0393fSCarsten Otte #endif
3452ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
3453527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
3454ccc7910fSCarsten Otte 		break;
3455ccc7910fSCarsten Otte 	}
3456d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
3457d6712df9SCornelia Huck 	{
3458d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
3459d6712df9SCornelia Huck 		r = -EFAULT;
3460d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
3461d6712df9SCornelia Huck 			break;
3462d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3463d6712df9SCornelia Huck 		break;
3464d6712df9SCornelia Huck 	}
346541408c28SThomas Huth 	case KVM_S390_MEM_OP: {
346641408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
346741408c28SThomas Huth 
346841408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
346941408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
347041408c28SThomas Huth 		else
347141408c28SThomas Huth 			r = -EFAULT;
347241408c28SThomas Huth 		break;
347341408c28SThomas Huth 	}
3474816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
3475816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3476816c7667SJens Freimann 
3477816c7667SJens Freimann 		r = -EFAULT;
3478816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3479816c7667SJens Freimann 			break;
3480816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
3481816c7667SJens Freimann 		    irq_state.len == 0 ||
3482816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
3483816c7667SJens Freimann 			r = -EINVAL;
3484816c7667SJens Freimann 			break;
3485816c7667SJens Freimann 		}
3486816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
3487816c7667SJens Freimann 					   (void __user *) irq_state.buf,
3488816c7667SJens Freimann 					   irq_state.len);
3489816c7667SJens Freimann 		break;
3490816c7667SJens Freimann 	}
3491816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
3492816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3493816c7667SJens Freimann 
3494816c7667SJens Freimann 		r = -EFAULT;
3495816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3496816c7667SJens Freimann 			break;
3497816c7667SJens Freimann 		if (irq_state.len == 0) {
3498816c7667SJens Freimann 			r = -EINVAL;
3499816c7667SJens Freimann 			break;
3500816c7667SJens Freimann 		}
3501816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
3502816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
3503816c7667SJens Freimann 					   irq_state.len);
3504816c7667SJens Freimann 		break;
3505816c7667SJens Freimann 	}
3506b0c632dbSHeiko Carstens 	default:
35073e6afcf1SCarsten Otte 		r = -ENOTTY;
3508b0c632dbSHeiko Carstens 	}
3509bc923cc9SAvi Kivity 	return r;
3510b0c632dbSHeiko Carstens }
3511b0c632dbSHeiko Carstens 
35125b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
35135b1c1493SCarsten Otte {
35145b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
35155b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
35165b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
35175b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
35185b1c1493SCarsten Otte 		get_page(vmf->page);
35195b1c1493SCarsten Otte 		return 0;
35205b1c1493SCarsten Otte 	}
35215b1c1493SCarsten Otte #endif
35225b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
35235b1c1493SCarsten Otte }
35245b1c1493SCarsten Otte 
35255587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
35265587027cSAneesh Kumar K.V 			    unsigned long npages)
3527db3fe4ebSTakuya Yoshikawa {
3528db3fe4ebSTakuya Yoshikawa 	return 0;
3529db3fe4ebSTakuya Yoshikawa }
3530db3fe4ebSTakuya Yoshikawa 
3531b0c632dbSHeiko Carstens /* Section: memory related */
3532f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
3533f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
353409170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
35357b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
3536b0c632dbSHeiko Carstens {
3537dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
3538dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
3539dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
3540dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
3541b0c632dbSHeiko Carstens 
3542598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
3543b0c632dbSHeiko Carstens 		return -EINVAL;
3544b0c632dbSHeiko Carstens 
3545598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
3546b0c632dbSHeiko Carstens 		return -EINVAL;
3547b0c632dbSHeiko Carstens 
3548a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
3549a3a92c31SDominik Dingel 		return -EINVAL;
3550a3a92c31SDominik Dingel 
3551f7784b8eSMarcelo Tosatti 	return 0;
3552f7784b8eSMarcelo Tosatti }
3553f7784b8eSMarcelo Tosatti 
3554f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
355509170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
35568482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
3557f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
35588482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
3559f7784b8eSMarcelo Tosatti {
3560f7850c92SCarsten Otte 	int rc;
3561f7784b8eSMarcelo Tosatti 
35622cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
35632cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
35642cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
35652cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
35662cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
35672cef4debSChristian Borntraeger 	 */
35682cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
35692cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
35702cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
35712cef4debSChristian Borntraeger 		return;
3572598841caSCarsten Otte 
3573598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
3574598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
3575598841caSCarsten Otte 	if (rc)
3576ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
3577598841caSCarsten Otte 	return;
3578b0c632dbSHeiko Carstens }
3579b0c632dbSHeiko Carstens 
358060a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
358160a37709SAlexander Yarygin {
358260a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
358360a37709SAlexander Yarygin 
358460a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
358560a37709SAlexander Yarygin }
358660a37709SAlexander Yarygin 
35873491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
35883491caf2SChristian Borntraeger {
35893491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
35903491caf2SChristian Borntraeger }
35913491caf2SChristian Borntraeger 
3592b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
3593b0c632dbSHeiko Carstens {
359460a37709SAlexander Yarygin 	int i;
359560a37709SAlexander Yarygin 
359607197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
359707197fd0SDavid Hildenbrand 		pr_info("SIE not available\n");
359807197fd0SDavid Hildenbrand 		return -ENODEV;
359907197fd0SDavid Hildenbrand 	}
360007197fd0SDavid Hildenbrand 
360160a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
360260a37709SAlexander Yarygin 		kvm_s390_fac_list_mask[i] |=
360360a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
360460a37709SAlexander Yarygin 
36059d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
3606b0c632dbSHeiko Carstens }
3607b0c632dbSHeiko Carstens 
3608b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
3609b0c632dbSHeiko Carstens {
3610b0c632dbSHeiko Carstens 	kvm_exit();
3611b0c632dbSHeiko Carstens }
3612b0c632dbSHeiko Carstens 
3613b0c632dbSHeiko Carstens module_init(kvm_s390_init);
3614b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
3615566af940SCornelia Huck 
3616566af940SCornelia Huck /*
3617566af940SCornelia Huck  * Enable autoloading of the kvm module.
3618566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3619566af940SCornelia Huck  * since x86 takes a different approach.
3620566af940SCornelia Huck  */
3621566af940SCornelia Huck #include <linux/miscdevice.h>
3622566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
3623566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
3624