xref: /linux/arch/s390/kvm/kvm-s390.c (revision 174cd4b1e5fbd0d74c68cf3a74f5bd4923485512)
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>
32*174cd4b1SIngo Molnar #include <linux/sched/signal.h>
33*174cd4b1SIngo Molnar 
34cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
35b0c632dbSHeiko Carstens #include <asm/lowcore.h>
36fd5ada04SMartin Schwidefsky #include <asm/stp.h>
37b0c632dbSHeiko Carstens #include <asm/pgtable.h>
381e133ab2SMartin Schwidefsky #include <asm/gmap.h>
39f5daba1dSHeiko Carstens #include <asm/nmi.h>
40a0616cdeSDavid Howells #include <asm/switch_to.h>
416d3da241SJens Freimann #include <asm/isc.h>
421526bf9cSChristian Borntraeger #include <asm/sclp.h>
430a763c78SDavid Hildenbrand #include <asm/cpacf.h>
44221bb8a4SLinus Torvalds #include <asm/timex.h>
458f2abe6aSChristian Borntraeger #include "kvm-s390.h"
46b0c632dbSHeiko Carstens #include "gaccess.h"
47b0c632dbSHeiko Carstens 
48ea2cdd27SDavid Hildenbrand #define KMSG_COMPONENT "kvm-s390"
49ea2cdd27SDavid Hildenbrand #undef pr_fmt
50ea2cdd27SDavid Hildenbrand #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
51ea2cdd27SDavid Hildenbrand 
525786fffaSCornelia Huck #define CREATE_TRACE_POINTS
535786fffaSCornelia Huck #include "trace.h"
54ade38c31SCornelia Huck #include "trace-s390.h"
555786fffaSCornelia Huck 
5641408c28SThomas Huth #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
57816c7667SJens Freimann #define LOCAL_IRQS 32
58816c7667SJens Freimann #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
59816c7667SJens Freimann 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
6041408c28SThomas Huth 
61b0c632dbSHeiko Carstens #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
62b0c632dbSHeiko Carstens 
63b0c632dbSHeiko Carstens struct kvm_stats_debugfs_item debugfs_entries[] = {
64b0c632dbSHeiko Carstens 	{ "userspace_handled", VCPU_STAT(exit_userspace) },
650eaeafa1SChristian Borntraeger 	{ "exit_null", VCPU_STAT(exit_null) },
668f2abe6aSChristian Borntraeger 	{ "exit_validity", VCPU_STAT(exit_validity) },
678f2abe6aSChristian Borntraeger 	{ "exit_stop_request", VCPU_STAT(exit_stop_request) },
688f2abe6aSChristian Borntraeger 	{ "exit_external_request", VCPU_STAT(exit_external_request) },
698f2abe6aSChristian Borntraeger 	{ "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
70ba5c1e9bSCarsten Otte 	{ "exit_instruction", VCPU_STAT(exit_instruction) },
719ec6de19SAlexander Yarygin 	{ "exit_pei", VCPU_STAT(exit_pei) },
72ba5c1e9bSCarsten Otte 	{ "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
73ba5c1e9bSCarsten Otte 	{ "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
74a011eeb2SJanosch Frank 	{ "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
75f7819512SPaolo Bonzini 	{ "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
7662bea5bfSPaolo Bonzini 	{ "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
773491caf2SChristian Borntraeger 	{ "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
78ce2e4f0bSDavid Hildenbrand 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
79f5e10b09SChristian Borntraeger 	{ "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
80ba5c1e9bSCarsten Otte 	{ "instruction_lctl", VCPU_STAT(instruction_lctl) },
81aba07508SDavid Hildenbrand 	{ "instruction_stctl", VCPU_STAT(instruction_stctl) },
82aba07508SDavid Hildenbrand 	{ "instruction_stctg", VCPU_STAT(instruction_stctg) },
83ba5c1e9bSCarsten Otte 	{ "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
847697e71fSChristian Ehrhardt 	{ "deliver_external_call", VCPU_STAT(deliver_external_call) },
85ba5c1e9bSCarsten Otte 	{ "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
86ba5c1e9bSCarsten Otte 	{ "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
87ba5c1e9bSCarsten Otte 	{ "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
88ba5c1e9bSCarsten Otte 	{ "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
89ba5c1e9bSCarsten Otte 	{ "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
90ba5c1e9bSCarsten Otte 	{ "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
91ba5c1e9bSCarsten Otte 	{ "exit_wait_state", VCPU_STAT(exit_wait_state) },
9269d0d3a3SChristian Borntraeger 	{ "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
93453423dcSChristian Borntraeger 	{ "instruction_stidp", VCPU_STAT(instruction_stidp) },
94453423dcSChristian Borntraeger 	{ "instruction_spx", VCPU_STAT(instruction_spx) },
95453423dcSChristian Borntraeger 	{ "instruction_stpx", VCPU_STAT(instruction_stpx) },
96453423dcSChristian Borntraeger 	{ "instruction_stap", VCPU_STAT(instruction_stap) },
97453423dcSChristian Borntraeger 	{ "instruction_storage_key", VCPU_STAT(instruction_storage_key) },
988a242234SHeiko Carstens 	{ "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
99453423dcSChristian Borntraeger 	{ "instruction_stsch", VCPU_STAT(instruction_stsch) },
100453423dcSChristian Borntraeger 	{ "instruction_chsc", VCPU_STAT(instruction_chsc) },
101b31288faSKonstantin Weitz 	{ "instruction_essa", VCPU_STAT(instruction_essa) },
102453423dcSChristian Borntraeger 	{ "instruction_stsi", VCPU_STAT(instruction_stsi) },
103453423dcSChristian Borntraeger 	{ "instruction_stfl", VCPU_STAT(instruction_stfl) },
104bb25b9baSChristian Borntraeger 	{ "instruction_tprot", VCPU_STAT(instruction_tprot) },
10595ca2cb5SJanosch Frank 	{ "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
106a3508fbeSDavid Hildenbrand 	{ "instruction_sie", VCPU_STAT(instruction_sie) },
1075288fbf0SChristian Borntraeger 	{ "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
108bd59d3a4SCornelia Huck 	{ "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
1097697e71fSChristian Ehrhardt 	{ "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
1105288fbf0SChristian Borntraeger 	{ "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
11142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
11242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
1135288fbf0SChristian Borntraeger 	{ "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
11442cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
11542cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
116cd7b4b61SEric Farman 	{ "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
1175288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
1185288fbf0SChristian Borntraeger 	{ "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
1195288fbf0SChristian Borntraeger 	{ "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
12042cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
12142cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
12242cb0c9fSDavid Hildenbrand 	{ "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
123388186bcSChristian Borntraeger 	{ "diagnose_10", VCPU_STAT(diagnose_10) },
124e28acfeaSChristian Borntraeger 	{ "diagnose_44", VCPU_STAT(diagnose_44) },
12541628d33SKonstantin Weitz 	{ "diagnose_9c", VCPU_STAT(diagnose_9c) },
126175a5c9eSChristian Borntraeger 	{ "diagnose_258", VCPU_STAT(diagnose_258) },
127175a5c9eSChristian Borntraeger 	{ "diagnose_308", VCPU_STAT(diagnose_308) },
128175a5c9eSChristian Borntraeger 	{ "diagnose_500", VCPU_STAT(diagnose_500) },
129b0c632dbSHeiko Carstens 	{ NULL }
130b0c632dbSHeiko Carstens };
131b0c632dbSHeiko Carstens 
132a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
133a411edf1SDavid Hildenbrand static int nested;
134a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
135a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
136a411edf1SDavid Hildenbrand 
1379d8d5786SMichael Mueller /* upper facilities limit for kvm */
138f6c1d359SHeiko Carstens unsigned long kvm_s390_fac_list_mask[16] = { FACILITIES_KVM };
139b0c632dbSHeiko Carstens 
1409d8d5786SMichael Mueller unsigned long kvm_s390_fac_list_mask_size(void)
14178c4b59fSMichael Mueller {
1429d8d5786SMichael Mueller 	BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask) > S390_ARCH_FAC_MASK_SIZE_U64);
1439d8d5786SMichael Mueller 	return ARRAY_SIZE(kvm_s390_fac_list_mask);
14478c4b59fSMichael Mueller }
14578c4b59fSMichael Mueller 
14615c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
14715c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1480a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
1490a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
15015c9705fSDavid Hildenbrand 
1519d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
152a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
15378f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
1549d8d5786SMichael Mueller 
155b0c632dbSHeiko Carstens /* Section: not file related */
15613a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
157b0c632dbSHeiko Carstens {
158b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
15910474ae8SAlexander Graf 	return 0;
160b0c632dbSHeiko Carstens }
161b0c632dbSHeiko Carstens 
162414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
163414d3b07SMartin Schwidefsky 			      unsigned long end);
1642c70fe44SChristian Borntraeger 
165fdf03650SFan Zhang /*
166fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
167fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
168fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
169fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
170fdf03650SFan Zhang  */
171fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
172fdf03650SFan Zhang 			  void *v)
173fdf03650SFan Zhang {
174fdf03650SFan Zhang 	struct kvm *kvm;
175fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
176fdf03650SFan Zhang 	int i;
177fdf03650SFan Zhang 	unsigned long long *delta = v;
178fdf03650SFan Zhang 
179fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
180fdf03650SFan Zhang 		kvm->arch.epoch -= *delta;
181fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
182fdf03650SFan Zhang 			vcpu->arch.sie_block->epoch -= *delta;
183db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
184db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
18591473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
18691473b48SDavid Hildenbrand 				vcpu->arch.vsie_block->epoch -= *delta;
187fdf03650SFan Zhang 		}
188fdf03650SFan Zhang 	}
189fdf03650SFan Zhang 	return NOTIFY_OK;
190fdf03650SFan Zhang }
191fdf03650SFan Zhang 
192fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
193fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
194fdf03650SFan Zhang };
195fdf03650SFan Zhang 
196b0c632dbSHeiko Carstens int kvm_arch_hardware_setup(void)
197b0c632dbSHeiko Carstens {
1982c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
199b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
200a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
201a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
202fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
203fdf03650SFan Zhang 				       &kvm_clock_notifier);
204b0c632dbSHeiko Carstens 	return 0;
205b0c632dbSHeiko Carstens }
206b0c632dbSHeiko Carstens 
207b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
208b0c632dbSHeiko Carstens {
209b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
210a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
211fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
212fdf03650SFan Zhang 					 &kvm_clock_notifier);
213b0c632dbSHeiko Carstens }
214b0c632dbSHeiko Carstens 
21522be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
21622be5a13SDavid Hildenbrand {
21722be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
21822be5a13SDavid Hildenbrand }
21922be5a13SDavid Hildenbrand 
2200a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
2210a763c78SDavid Hildenbrand {
2220a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
223d051ae53SHeiko Carstens 	int cc;
2240a763c78SDavid Hildenbrand 
2250a763c78SDavid Hildenbrand 	asm volatile(
2260a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
2270a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
2280a763c78SDavid Hildenbrand 		"	ipm	%0\n"
2290a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
2300a763c78SDavid Hildenbrand 		: "=d" (cc)
2310a763c78SDavid Hildenbrand 		: "d" (r0)
2320a763c78SDavid Hildenbrand 		: "cc");
2330a763c78SDavid Hildenbrand 	return cc == 0;
2340a763c78SDavid Hildenbrand }
2350a763c78SDavid Hildenbrand 
23622be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
23722be5a13SDavid Hildenbrand {
2380a763c78SDavid Hildenbrand 	int i;
2390a763c78SDavid Hildenbrand 
2400a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
2410a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
2420a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
2430a763c78SDavid Hildenbrand 	}
2440a763c78SDavid Hildenbrand 
2450a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
246221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
247221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
248221bb8a4SLinus Torvalds 		     PTFF_QAF);
2490a763c78SDavid Hildenbrand 
2500a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
25169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
25269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
25369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
25469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
25569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
25669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
25769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
25869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
25969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
26069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
2610a763c78SDavid Hildenbrand 	}
2620a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
26369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
26469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
2650a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
26669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
26769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
26869c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
26969c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
27069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
27169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
27269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
27369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
2740a763c78SDavid Hildenbrand 	}
2750a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
27669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PPNO, (cpacf_mask_t *)
27769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
2780a763c78SDavid Hildenbrand 
27922be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
28022be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
281a3508fbeSDavid Hildenbrand 	/*
282a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
283a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
284a3508fbeSDavid Hildenbrand 	 */
285a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
286a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
287a3508fbeSDavid Hildenbrand 		return;
288a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
28919c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
29019c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
2910615a326SDavid Hildenbrand 	if (sclp.has_siif)
2920615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
29377d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
29477d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
295a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
296a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
2975630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
2985630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
29913ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
30013ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
3017fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
3027fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
3035d3876a8SDavid Hildenbrand 	/*
3045d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
3055d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
3065d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
3075d3876a8SDavid Hildenbrand 	 *
3085d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
3095d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
3105d3876a8SDavid Hildenbrand 	 *
3115d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
3125d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
3135d3876a8SDavid Hildenbrand 	 *
3145d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
3155d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
3165d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
3175d3876a8SDavid Hildenbrand 	 *
3185d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
3195d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
3205d3876a8SDavid Hildenbrand 	 */
32122be5a13SDavid Hildenbrand }
32222be5a13SDavid Hildenbrand 
323b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
324b0c632dbSHeiko Carstens {
32578f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
32678f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
32778f26131SChristian Borntraeger 		return -ENOMEM;
32878f26131SChristian Borntraeger 
32978f26131SChristian Borntraeger 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
33078f26131SChristian Borntraeger 		debug_unregister(kvm_s390_dbf);
33178f26131SChristian Borntraeger 		return -ENOMEM;
33278f26131SChristian Borntraeger 	}
33378f26131SChristian Borntraeger 
33422be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
33522be5a13SDavid Hildenbrand 
33684877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
33784877d93SCornelia Huck 	return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
338b0c632dbSHeiko Carstens }
339b0c632dbSHeiko Carstens 
34078f26131SChristian Borntraeger void kvm_arch_exit(void)
34178f26131SChristian Borntraeger {
34278f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
34378f26131SChristian Borntraeger }
34478f26131SChristian Borntraeger 
345b0c632dbSHeiko Carstens /* Section: device related */
346b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
347b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
348b0c632dbSHeiko Carstens {
349b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
350b0c632dbSHeiko Carstens 		return s390_enable_sie();
351b0c632dbSHeiko Carstens 	return -EINVAL;
352b0c632dbSHeiko Carstens }
353b0c632dbSHeiko Carstens 
354784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
355b0c632dbSHeiko Carstens {
356d7b0b5ebSCarsten Otte 	int r;
357d7b0b5ebSCarsten Otte 
3582bd0ac4eSCarsten Otte 	switch (ext) {
359d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
360b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
36152e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
3621efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
3631efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
3641efd0f59SCarsten Otte #endif
3653c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
36660b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
36714eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
368d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
369fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
37010ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
371c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
372d938dc55SCornelia Huck 	case KVM_CAP_ENABLE_CAP_VM:
37378599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
374f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
3756352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
376460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
37747b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
3782444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
379e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
38030ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
381816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
3826502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
383d7b0b5ebSCarsten Otte 		r = 1;
384d7b0b5ebSCarsten Otte 		break;
38541408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
38641408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
38741408c28SThomas Huth 		break;
388e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
389e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
39076a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
391a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
392a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
393a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
39476a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
395e726b1bdSChristian Borntraeger 		break;
396e1e2e605SNick Wang 	case KVM_CAP_NR_MEMSLOTS:
397e1e2e605SNick Wang 		r = KVM_USER_MEM_SLOTS;
398e1e2e605SNick Wang 		break;
3991526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
400abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
4011526bf9cSChristian Borntraeger 		break;
40268c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
40368c55750SEric Farman 		r = MACHINE_HAS_VX;
40468c55750SEric Farman 		break;
405c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
406c6e5f166SFan Zhang 		r = test_facility(64);
407c6e5f166SFan Zhang 		break;
4082bd0ac4eSCarsten Otte 	default:
409d7b0b5ebSCarsten Otte 		r = 0;
410b0c632dbSHeiko Carstens 	}
411d7b0b5ebSCarsten Otte 	return r;
4122bd0ac4eSCarsten Otte }
413b0c632dbSHeiko Carstens 
41415f36ebdSJason J. Herne static void kvm_s390_sync_dirty_log(struct kvm *kvm,
41515f36ebdSJason J. Herne 					struct kvm_memory_slot *memslot)
41615f36ebdSJason J. Herne {
41715f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
41815f36ebdSJason J. Herne 	unsigned long address;
41915f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
42015f36ebdSJason J. Herne 
42115f36ebdSJason J. Herne 	/* Loop over all guest pages */
42215f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
42315f36ebdSJason J. Herne 	for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
42415f36ebdSJason J. Herne 		address = gfn_to_hva_memslot(memslot, cur_gfn);
42515f36ebdSJason J. Herne 
4261e133ab2SMartin Schwidefsky 		if (test_and_clear_guest_dirty(gmap->mm, address))
42715f36ebdSJason J. Herne 			mark_page_dirty(kvm, cur_gfn);
4281763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
4291763f8d0SChristian Borntraeger 			return;
43070c88a00SChristian Borntraeger 		cond_resched();
43115f36ebdSJason J. Herne 	}
43215f36ebdSJason J. Herne }
43315f36ebdSJason J. Herne 
434b0c632dbSHeiko Carstens /* Section: vm related */
435a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
436a6e2f683SEugene (jno) Dvurechenski 
437b0c632dbSHeiko Carstens /*
438b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
439b0c632dbSHeiko Carstens  */
440b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
441b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
442b0c632dbSHeiko Carstens {
44315f36ebdSJason J. Herne 	int r;
44415f36ebdSJason J. Herne 	unsigned long n;
4459f6b8029SPaolo Bonzini 	struct kvm_memslots *slots;
44615f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
44715f36ebdSJason J. Herne 	int is_dirty = 0;
44815f36ebdSJason J. Herne 
449e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
450e1e8a962SJanosch Frank 		return -EINVAL;
451e1e8a962SJanosch Frank 
45215f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
45315f36ebdSJason J. Herne 
45415f36ebdSJason J. Herne 	r = -EINVAL;
45515f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
45615f36ebdSJason J. Herne 		goto out;
45715f36ebdSJason J. Herne 
4589f6b8029SPaolo Bonzini 	slots = kvm_memslots(kvm);
4599f6b8029SPaolo Bonzini 	memslot = id_to_memslot(slots, log->slot);
46015f36ebdSJason J. Herne 	r = -ENOENT;
46115f36ebdSJason J. Herne 	if (!memslot->dirty_bitmap)
46215f36ebdSJason J. Herne 		goto out;
46315f36ebdSJason J. Herne 
46415f36ebdSJason J. Herne 	kvm_s390_sync_dirty_log(kvm, memslot);
46515f36ebdSJason J. Herne 	r = kvm_get_dirty_log(kvm, log, &is_dirty);
46615f36ebdSJason J. Herne 	if (r)
46715f36ebdSJason J. Herne 		goto out;
46815f36ebdSJason J. Herne 
46915f36ebdSJason J. Herne 	/* Clear the dirty log */
47015f36ebdSJason J. Herne 	if (is_dirty) {
47115f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
47215f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
47315f36ebdSJason J. Herne 	}
47415f36ebdSJason J. Herne 	r = 0;
47515f36ebdSJason J. Herne out:
47615f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
47715f36ebdSJason J. Herne 	return r;
478b0c632dbSHeiko Carstens }
479b0c632dbSHeiko Carstens 
4806502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
4816502a34cSDavid Hildenbrand {
4826502a34cSDavid Hildenbrand 	unsigned int i;
4836502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
4846502a34cSDavid Hildenbrand 
4856502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
4866502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
4876502a34cSDavid Hildenbrand 	}
4886502a34cSDavid Hildenbrand }
4896502a34cSDavid Hildenbrand 
490d938dc55SCornelia Huck static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
491d938dc55SCornelia Huck {
492d938dc55SCornelia Huck 	int r;
493d938dc55SCornelia Huck 
494d938dc55SCornelia Huck 	if (cap->flags)
495d938dc55SCornelia Huck 		return -EINVAL;
496d938dc55SCornelia Huck 
497d938dc55SCornelia Huck 	switch (cap->cap) {
49884223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
499c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
50084223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
50184223598SCornelia Huck 		r = 0;
50284223598SCornelia Huck 		break;
5032444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
504c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
5052444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
5062444b352SDavid Hildenbrand 		r = 0;
5072444b352SDavid Hildenbrand 		break;
50868c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
5095967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
510a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
5115967c17bSDavid Hildenbrand 			r = -EBUSY;
5125967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
513c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
514c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
5152f87d942SGuenther Hutzl 			if (test_facility(134)) {
5162f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
5172f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
5182f87d942SGuenther Hutzl 			}
51953743aa7SMaxim Samoylov 			if (test_facility(135)) {
52053743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
52153743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
52253743aa7SMaxim Samoylov 			}
52318280d8bSMichael Mueller 			r = 0;
52418280d8bSMichael Mueller 		} else
52518280d8bSMichael Mueller 			r = -EINVAL;
5265967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
527c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
528c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
52968c55750SEric Farman 		break;
530c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
531c6e5f166SFan Zhang 		r = -EINVAL;
532c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
533a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
534c6e5f166SFan Zhang 			r = -EBUSY;
535c6e5f166SFan Zhang 		} else if (test_facility(64)) {
536c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
537c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
538c6e5f166SFan Zhang 			r = 0;
539c6e5f166SFan Zhang 		}
540c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
541c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
542c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
543c6e5f166SFan Zhang 		break;
544e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
545c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
546e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
547e44fc8c9SEkaterina Tumanova 		r = 0;
548e44fc8c9SEkaterina Tumanova 		break;
5496502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5506502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
5516502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
5526502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
5536502a34cSDavid Hildenbrand 		r = 0;
5546502a34cSDavid Hildenbrand 		break;
555d938dc55SCornelia Huck 	default:
556d938dc55SCornelia Huck 		r = -EINVAL;
557d938dc55SCornelia Huck 		break;
558d938dc55SCornelia Huck 	}
559d938dc55SCornelia Huck 	return r;
560d938dc55SCornelia Huck }
561d938dc55SCornelia Huck 
5628c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
5638c0a7ce6SDominik Dingel {
5648c0a7ce6SDominik Dingel 	int ret;
5658c0a7ce6SDominik Dingel 
5668c0a7ce6SDominik Dingel 	switch (attr->attr) {
5678c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
5688c0a7ce6SDominik Dingel 		ret = 0;
569c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
570a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
571a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
5728c0a7ce6SDominik Dingel 			ret = -EFAULT;
5738c0a7ce6SDominik Dingel 		break;
5748c0a7ce6SDominik Dingel 	default:
5758c0a7ce6SDominik Dingel 		ret = -ENXIO;
5768c0a7ce6SDominik Dingel 		break;
5778c0a7ce6SDominik Dingel 	}
5788c0a7ce6SDominik Dingel 	return ret;
5798c0a7ce6SDominik Dingel }
5808c0a7ce6SDominik Dingel 
5818c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
5824f718eabSDominik Dingel {
5834f718eabSDominik Dingel 	int ret;
5844f718eabSDominik Dingel 	unsigned int idx;
5854f718eabSDominik Dingel 	switch (attr->attr) {
5864f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
587f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
588c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
589e6db1d61SDominik Dingel 			break;
590e6db1d61SDominik Dingel 
5914f718eabSDominik Dingel 		ret = -EBUSY;
592c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
5934f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
594a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
5954f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
5964f718eabSDominik Dingel 			ret = 0;
5974f718eabSDominik Dingel 		}
5984f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
5994f718eabSDominik Dingel 		break;
6004f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
601f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
602f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
603f9cbd9b0SDavid Hildenbrand 			break;
604c3489155SDominik Dingel 		ret = -EINVAL;
605c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
606c3489155SDominik Dingel 			break;
607c3489155SDominik Dingel 
608c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
6094f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
6104f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
611a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
6124f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
6134f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
6144f718eabSDominik Dingel 		ret = 0;
6154f718eabSDominik Dingel 		break;
6168c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
6178c0a7ce6SDominik Dingel 		unsigned long new_limit;
6188c0a7ce6SDominik Dingel 
6198c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
6208c0a7ce6SDominik Dingel 			return -EINVAL;
6218c0a7ce6SDominik Dingel 
6228c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
6238c0a7ce6SDominik Dingel 			return -EFAULT;
6248c0a7ce6SDominik Dingel 
625a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
626a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
6278c0a7ce6SDominik Dingel 			return -E2BIG;
6288c0a7ce6SDominik Dingel 
629a3a92c31SDominik Dingel 		if (!new_limit)
630a3a92c31SDominik Dingel 			return -EINVAL;
631a3a92c31SDominik Dingel 
6326ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
633a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
634a3a92c31SDominik Dingel 			new_limit -= 1;
635a3a92c31SDominik Dingel 
6368c0a7ce6SDominik Dingel 		ret = -EBUSY;
6378c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
638a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
6396ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
6406ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
6418c0a7ce6SDominik Dingel 
6428c0a7ce6SDominik Dingel 			if (!new) {
6438c0a7ce6SDominik Dingel 				ret = -ENOMEM;
6448c0a7ce6SDominik Dingel 			} else {
6456ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
6468c0a7ce6SDominik Dingel 				new->private = kvm;
6478c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
6488c0a7ce6SDominik Dingel 				ret = 0;
6498c0a7ce6SDominik Dingel 			}
6508c0a7ce6SDominik Dingel 		}
6518c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
652a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
653a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
654a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
6558c0a7ce6SDominik Dingel 		break;
6568c0a7ce6SDominik Dingel 	}
6574f718eabSDominik Dingel 	default:
6584f718eabSDominik Dingel 		ret = -ENXIO;
6594f718eabSDominik Dingel 		break;
6604f718eabSDominik Dingel 	}
6614f718eabSDominik Dingel 	return ret;
6624f718eabSDominik Dingel }
6634f718eabSDominik Dingel 
664a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
665a374e892STony Krowiak 
666a374e892STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
667a374e892STony Krowiak {
668a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
669a374e892STony Krowiak 	int i;
670a374e892STony Krowiak 
6719d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
672a374e892STony Krowiak 		return -EINVAL;
673a374e892STony Krowiak 
674a374e892STony Krowiak 	mutex_lock(&kvm->lock);
675a374e892STony Krowiak 	switch (attr->attr) {
676a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
677a374e892STony Krowiak 		get_random_bytes(
678a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
679a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
680a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
681c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
682a374e892STony Krowiak 		break;
683a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
684a374e892STony Krowiak 		get_random_bytes(
685a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
686a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
687a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
688c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
689a374e892STony Krowiak 		break;
690a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
691a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
692a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
693a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
694c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
695a374e892STony Krowiak 		break;
696a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
697a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
698a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
699a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
700c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
701a374e892STony Krowiak 		break;
702a374e892STony Krowiak 	default:
703a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
704a374e892STony Krowiak 		return -ENXIO;
705a374e892STony Krowiak 	}
706a374e892STony Krowiak 
707a374e892STony Krowiak 	kvm_for_each_vcpu(i, vcpu, kvm) {
708a374e892STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
709a374e892STony Krowiak 		exit_sie(vcpu);
710a374e892STony Krowiak 	}
711a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
712a374e892STony Krowiak 	return 0;
713a374e892STony Krowiak }
714a374e892STony Krowiak 
71572f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
71672f25020SJason J. Herne {
71772f25020SJason J. Herne 	u8 gtod_high;
71872f25020SJason J. Herne 
71972f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
72072f25020SJason J. Herne 					   sizeof(gtod_high)))
72172f25020SJason J. Herne 		return -EFAULT;
72272f25020SJason J. Herne 
72372f25020SJason J. Herne 	if (gtod_high != 0)
72472f25020SJason J. Herne 		return -EINVAL;
72558c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
72672f25020SJason J. Herne 
72772f25020SJason J. Herne 	return 0;
72872f25020SJason J. Herne }
72972f25020SJason J. Herne 
73072f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
73172f25020SJason J. Herne {
7325a3d883aSDavid Hildenbrand 	u64 gtod;
73372f25020SJason J. Herne 
73472f25020SJason J. Herne 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
73572f25020SJason J. Herne 		return -EFAULT;
73672f25020SJason J. Herne 
73725ed1675SDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, gtod);
73858c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod);
73972f25020SJason J. Herne 	return 0;
74072f25020SJason J. Herne }
74172f25020SJason J. Herne 
74272f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
74372f25020SJason J. Herne {
74472f25020SJason J. Herne 	int ret;
74572f25020SJason J. Herne 
74672f25020SJason J. Herne 	if (attr->flags)
74772f25020SJason J. Herne 		return -EINVAL;
74872f25020SJason J. Herne 
74972f25020SJason J. Herne 	switch (attr->attr) {
75072f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
75172f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
75272f25020SJason J. Herne 		break;
75372f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
75472f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
75572f25020SJason J. Herne 		break;
75672f25020SJason J. Herne 	default:
75772f25020SJason J. Herne 		ret = -ENXIO;
75872f25020SJason J. Herne 		break;
75972f25020SJason J. Herne 	}
76072f25020SJason J. Herne 	return ret;
76172f25020SJason J. Herne }
76272f25020SJason J. Herne 
76372f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
76472f25020SJason J. Herne {
76572f25020SJason J. Herne 	u8 gtod_high = 0;
76672f25020SJason J. Herne 
76772f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
76872f25020SJason J. Herne 					 sizeof(gtod_high)))
76972f25020SJason J. Herne 		return -EFAULT;
77058c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
77172f25020SJason J. Herne 
77272f25020SJason J. Herne 	return 0;
77372f25020SJason J. Herne }
77472f25020SJason J. Herne 
77572f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
77672f25020SJason J. Herne {
7775a3d883aSDavid Hildenbrand 	u64 gtod;
77872f25020SJason J. Herne 
77960417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
78072f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
78172f25020SJason J. Herne 		return -EFAULT;
78258c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
78372f25020SJason J. Herne 
78472f25020SJason J. Herne 	return 0;
78572f25020SJason J. Herne }
78672f25020SJason J. Herne 
78772f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
78872f25020SJason J. Herne {
78972f25020SJason J. Herne 	int ret;
79072f25020SJason J. Herne 
79172f25020SJason J. Herne 	if (attr->flags)
79272f25020SJason J. Herne 		return -EINVAL;
79372f25020SJason J. Herne 
79472f25020SJason J. Herne 	switch (attr->attr) {
79572f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
79672f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
79772f25020SJason J. Herne 		break;
79872f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
79972f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
80072f25020SJason J. Herne 		break;
80172f25020SJason J. Herne 	default:
80272f25020SJason J. Herne 		ret = -ENXIO;
80372f25020SJason J. Herne 		break;
80472f25020SJason J. Herne 	}
80572f25020SJason J. Herne 	return ret;
80672f25020SJason J. Herne }
80772f25020SJason J. Herne 
808658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
809658b6edaSMichael Mueller {
810658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
811053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
812658b6edaSMichael Mueller 	int ret = 0;
813658b6edaSMichael Mueller 
814658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
815a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
816658b6edaSMichael Mueller 		ret = -EBUSY;
817658b6edaSMichael Mueller 		goto out;
818658b6edaSMichael Mueller 	}
819658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
820658b6edaSMichael Mueller 	if (!proc) {
821658b6edaSMichael Mueller 		ret = -ENOMEM;
822658b6edaSMichael Mueller 		goto out;
823658b6edaSMichael Mueller 	}
824658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
825658b6edaSMichael Mueller 			    sizeof(*proc))) {
8269bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
827053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
828053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
8290487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
830053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
831053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
832053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
833053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
834053dd230SDavid Hildenbrand 			else
835658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
836053dd230SDavid Hildenbrand 		}
837c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
838658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
839a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
840a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
841a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
842a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
843a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
844a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
845a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
846658b6edaSMichael Mueller 	} else
847658b6edaSMichael Mueller 		ret = -EFAULT;
848658b6edaSMichael Mueller 	kfree(proc);
849658b6edaSMichael Mueller out:
850658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
851658b6edaSMichael Mueller 	return ret;
852658b6edaSMichael Mueller }
853658b6edaSMichael Mueller 
85415c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
85515c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
85615c9705fSDavid Hildenbrand {
85715c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
85815c9705fSDavid Hildenbrand 	int ret = -EBUSY;
85915c9705fSDavid Hildenbrand 
86015c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
86115c9705fSDavid Hildenbrand 		return -EFAULT;
86215c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
86315c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
86415c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
86515c9705fSDavid Hildenbrand 		return -EINVAL;
86615c9705fSDavid Hildenbrand 
86715c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
86815c9705fSDavid Hildenbrand 	if (!atomic_read(&kvm->online_vcpus)) {
86915c9705fSDavid Hildenbrand 		bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
87015c9705fSDavid Hildenbrand 			    KVM_S390_VM_CPU_FEAT_NR_BITS);
87115c9705fSDavid Hildenbrand 		ret = 0;
87215c9705fSDavid Hildenbrand 	}
87315c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
87415c9705fSDavid Hildenbrand 	return ret;
87515c9705fSDavid Hildenbrand }
87615c9705fSDavid Hildenbrand 
8770a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
8780a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
8790a763c78SDavid Hildenbrand {
8800a763c78SDavid Hildenbrand 	/*
8810a763c78SDavid Hildenbrand 	 * Once supported by kernel + hw, we have to store the subfunctions
8820a763c78SDavid Hildenbrand 	 * in kvm->arch and remember that user space configured them.
8830a763c78SDavid Hildenbrand 	 */
8840a763c78SDavid Hildenbrand 	return -ENXIO;
8850a763c78SDavid Hildenbrand }
8860a763c78SDavid Hildenbrand 
887658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
888658b6edaSMichael Mueller {
889658b6edaSMichael Mueller 	int ret = -ENXIO;
890658b6edaSMichael Mueller 
891658b6edaSMichael Mueller 	switch (attr->attr) {
892658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
893658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
894658b6edaSMichael Mueller 		break;
89515c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
89615c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
89715c9705fSDavid Hildenbrand 		break;
8980a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
8990a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
9000a763c78SDavid Hildenbrand 		break;
901658b6edaSMichael Mueller 	}
902658b6edaSMichael Mueller 	return ret;
903658b6edaSMichael Mueller }
904658b6edaSMichael Mueller 
905658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
906658b6edaSMichael Mueller {
907658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
908658b6edaSMichael Mueller 	int ret = 0;
909658b6edaSMichael Mueller 
910658b6edaSMichael Mueller 	proc = kzalloc(sizeof(*proc), GFP_KERNEL);
911658b6edaSMichael Mueller 	if (!proc) {
912658b6edaSMichael Mueller 		ret = -ENOMEM;
913658b6edaSMichael Mueller 		goto out;
914658b6edaSMichael Mueller 	}
9159bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
916658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
917c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
918c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
919a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
920a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
921a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
922a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
923a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
924a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
925a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
926658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
927658b6edaSMichael Mueller 		ret = -EFAULT;
928658b6edaSMichael Mueller 	kfree(proc);
929658b6edaSMichael Mueller out:
930658b6edaSMichael Mueller 	return ret;
931658b6edaSMichael Mueller }
932658b6edaSMichael Mueller 
933658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
934658b6edaSMichael Mueller {
935658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
936658b6edaSMichael Mueller 	int ret = 0;
937658b6edaSMichael Mueller 
938658b6edaSMichael Mueller 	mach = kzalloc(sizeof(*mach), GFP_KERNEL);
939658b6edaSMichael Mueller 	if (!mach) {
940658b6edaSMichael Mueller 		ret = -ENOMEM;
941658b6edaSMichael Mueller 		goto out;
942658b6edaSMichael Mueller 	}
943658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
94437c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
945c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
946981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
947658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
94804478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
949a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
950a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
951a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
952a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
953a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
954a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
955a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
956a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
957a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
958a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
959a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
960658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
961658b6edaSMichael Mueller 		ret = -EFAULT;
962658b6edaSMichael Mueller 	kfree(mach);
963658b6edaSMichael Mueller out:
964658b6edaSMichael Mueller 	return ret;
965658b6edaSMichael Mueller }
966658b6edaSMichael Mueller 
96715c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
96815c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
96915c9705fSDavid Hildenbrand {
97015c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
97115c9705fSDavid Hildenbrand 
97215c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
97315c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
97415c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
97515c9705fSDavid Hildenbrand 		return -EFAULT;
97615c9705fSDavid Hildenbrand 	return 0;
97715c9705fSDavid Hildenbrand }
97815c9705fSDavid Hildenbrand 
97915c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
98015c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
98115c9705fSDavid Hildenbrand {
98215c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
98315c9705fSDavid Hildenbrand 
98415c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
98515c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
98615c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
98715c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
98815c9705fSDavid Hildenbrand 		return -EFAULT;
98915c9705fSDavid Hildenbrand 	return 0;
99015c9705fSDavid Hildenbrand }
99115c9705fSDavid Hildenbrand 
9920a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
9930a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
9940a763c78SDavid Hildenbrand {
9950a763c78SDavid Hildenbrand 	/*
9960a763c78SDavid Hildenbrand 	 * Once we can actually configure subfunctions (kernel + hw support),
9970a763c78SDavid Hildenbrand 	 * we have to check if they were already set by user space, if so copy
9980a763c78SDavid Hildenbrand 	 * them from kvm->arch.
9990a763c78SDavid Hildenbrand 	 */
10000a763c78SDavid Hildenbrand 	return -ENXIO;
10010a763c78SDavid Hildenbrand }
10020a763c78SDavid Hildenbrand 
10030a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
10040a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
10050a763c78SDavid Hildenbrand {
10060a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
10070a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
10080a763c78SDavid Hildenbrand 		return -EFAULT;
10090a763c78SDavid Hildenbrand 	return 0;
10100a763c78SDavid Hildenbrand }
1011658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1012658b6edaSMichael Mueller {
1013658b6edaSMichael Mueller 	int ret = -ENXIO;
1014658b6edaSMichael Mueller 
1015658b6edaSMichael Mueller 	switch (attr->attr) {
1016658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1017658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1018658b6edaSMichael Mueller 		break;
1019658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1020658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1021658b6edaSMichael Mueller 		break;
102215c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
102315c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
102415c9705fSDavid Hildenbrand 		break;
102515c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
102615c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
102715c9705fSDavid Hildenbrand 		break;
10280a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
10290a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
10300a763c78SDavid Hildenbrand 		break;
10310a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
10320a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
10330a763c78SDavid Hildenbrand 		break;
1034658b6edaSMichael Mueller 	}
1035658b6edaSMichael Mueller 	return ret;
1036658b6edaSMichael Mueller }
1037658b6edaSMichael Mueller 
1038f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1039f2061656SDominik Dingel {
1040f2061656SDominik Dingel 	int ret;
1041f2061656SDominik Dingel 
1042f2061656SDominik Dingel 	switch (attr->group) {
10434f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
10448c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
10454f718eabSDominik Dingel 		break;
104672f25020SJason J. Herne 	case KVM_S390_VM_TOD:
104772f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
104872f25020SJason J. Herne 		break;
1049658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1050658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1051658b6edaSMichael Mueller 		break;
1052a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1053a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1054a374e892STony Krowiak 		break;
1055f2061656SDominik Dingel 	default:
1056f2061656SDominik Dingel 		ret = -ENXIO;
1057f2061656SDominik Dingel 		break;
1058f2061656SDominik Dingel 	}
1059f2061656SDominik Dingel 
1060f2061656SDominik Dingel 	return ret;
1061f2061656SDominik Dingel }
1062f2061656SDominik Dingel 
1063f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1064f2061656SDominik Dingel {
10658c0a7ce6SDominik Dingel 	int ret;
10668c0a7ce6SDominik Dingel 
10678c0a7ce6SDominik Dingel 	switch (attr->group) {
10688c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
10698c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
10708c0a7ce6SDominik Dingel 		break;
107172f25020SJason J. Herne 	case KVM_S390_VM_TOD:
107272f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
107372f25020SJason J. Herne 		break;
1074658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1075658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1076658b6edaSMichael Mueller 		break;
10778c0a7ce6SDominik Dingel 	default:
10788c0a7ce6SDominik Dingel 		ret = -ENXIO;
10798c0a7ce6SDominik Dingel 		break;
10808c0a7ce6SDominik Dingel 	}
10818c0a7ce6SDominik Dingel 
10828c0a7ce6SDominik Dingel 	return ret;
1083f2061656SDominik Dingel }
1084f2061656SDominik Dingel 
1085f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1086f2061656SDominik Dingel {
1087f2061656SDominik Dingel 	int ret;
1088f2061656SDominik Dingel 
1089f2061656SDominik Dingel 	switch (attr->group) {
10904f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
10914f718eabSDominik Dingel 		switch (attr->attr) {
10924f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
10934f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1094f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1095f9cbd9b0SDavid Hildenbrand 			break;
10968c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
10974f718eabSDominik Dingel 			ret = 0;
10984f718eabSDominik Dingel 			break;
10994f718eabSDominik Dingel 		default:
11004f718eabSDominik Dingel 			ret = -ENXIO;
11014f718eabSDominik Dingel 			break;
11024f718eabSDominik Dingel 		}
11034f718eabSDominik Dingel 		break;
110472f25020SJason J. Herne 	case KVM_S390_VM_TOD:
110572f25020SJason J. Herne 		switch (attr->attr) {
110672f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
110772f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
110872f25020SJason J. Herne 			ret = 0;
110972f25020SJason J. Herne 			break;
111072f25020SJason J. Herne 		default:
111172f25020SJason J. Herne 			ret = -ENXIO;
111272f25020SJason J. Herne 			break;
111372f25020SJason J. Herne 		}
111472f25020SJason J. Herne 		break;
1115658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1116658b6edaSMichael Mueller 		switch (attr->attr) {
1117658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1118658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
111915c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
112015c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
11210a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1122658b6edaSMichael Mueller 			ret = 0;
1123658b6edaSMichael Mueller 			break;
11240a763c78SDavid Hildenbrand 		/* configuring subfunctions is not supported yet */
11250a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1126658b6edaSMichael Mueller 		default:
1127658b6edaSMichael Mueller 			ret = -ENXIO;
1128658b6edaSMichael Mueller 			break;
1129658b6edaSMichael Mueller 		}
1130658b6edaSMichael Mueller 		break;
1131a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1132a374e892STony Krowiak 		switch (attr->attr) {
1133a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1134a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1135a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1136a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1137a374e892STony Krowiak 			ret = 0;
1138a374e892STony Krowiak 			break;
1139a374e892STony Krowiak 		default:
1140a374e892STony Krowiak 			ret = -ENXIO;
1141a374e892STony Krowiak 			break;
1142a374e892STony Krowiak 		}
1143a374e892STony Krowiak 		break;
1144f2061656SDominik Dingel 	default:
1145f2061656SDominik Dingel 		ret = -ENXIO;
1146f2061656SDominik Dingel 		break;
1147f2061656SDominik Dingel 	}
1148f2061656SDominik Dingel 
1149f2061656SDominik Dingel 	return ret;
1150f2061656SDominik Dingel }
1151f2061656SDominik Dingel 
115230ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
115330ee2a98SJason J. Herne {
115430ee2a98SJason J. Herne 	uint8_t *keys;
115530ee2a98SJason J. Herne 	uint64_t hva;
115630ee2a98SJason J. Herne 	int i, r = 0;
115730ee2a98SJason J. Herne 
115830ee2a98SJason J. Herne 	if (args->flags != 0)
115930ee2a98SJason J. Herne 		return -EINVAL;
116030ee2a98SJason J. Herne 
116130ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
116230ee2a98SJason J. Herne 	if (!mm_use_skey(current->mm))
116330ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
116430ee2a98SJason J. Herne 
116530ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
116630ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
116730ee2a98SJason J. Herne 		return -EINVAL;
116830ee2a98SJason J. Herne 
116930ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
117030ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
117130ee2a98SJason J. Herne 	if (!keys)
117230ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
117330ee2a98SJason J. Herne 	if (!keys)
117430ee2a98SJason J. Herne 		return -ENOMEM;
117530ee2a98SJason J. Herne 
1176d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
117730ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
117830ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
117930ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
118030ee2a98SJason J. Herne 			r = -EFAULT;
1181d3ed1ceeSMartin Schwidefsky 			break;
118230ee2a98SJason J. Herne 		}
118330ee2a98SJason J. Herne 
1184154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1185154c8c19SDavid Hildenbrand 		if (r)
1186d3ed1ceeSMartin Schwidefsky 			break;
118730ee2a98SJason J. Herne 	}
1188d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
118930ee2a98SJason J. Herne 
1190d3ed1ceeSMartin Schwidefsky 	if (!r) {
119130ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
119230ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
119330ee2a98SJason J. Herne 		if (r)
119430ee2a98SJason J. Herne 			r = -EFAULT;
1195d3ed1ceeSMartin Schwidefsky 	}
1196d3ed1ceeSMartin Schwidefsky 
119730ee2a98SJason J. Herne 	kvfree(keys);
119830ee2a98SJason J. Herne 	return r;
119930ee2a98SJason J. Herne }
120030ee2a98SJason J. Herne 
120130ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
120230ee2a98SJason J. Herne {
120330ee2a98SJason J. Herne 	uint8_t *keys;
120430ee2a98SJason J. Herne 	uint64_t hva;
120530ee2a98SJason J. Herne 	int i, r = 0;
120630ee2a98SJason J. Herne 
120730ee2a98SJason J. Herne 	if (args->flags != 0)
120830ee2a98SJason J. Herne 		return -EINVAL;
120930ee2a98SJason J. Herne 
121030ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
121130ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
121230ee2a98SJason J. Herne 		return -EINVAL;
121330ee2a98SJason J. Herne 
121430ee2a98SJason J. Herne 	keys = kmalloc_array(args->count, sizeof(uint8_t),
121530ee2a98SJason J. Herne 			     GFP_KERNEL | __GFP_NOWARN);
121630ee2a98SJason J. Herne 	if (!keys)
121730ee2a98SJason J. Herne 		keys = vmalloc(sizeof(uint8_t) * args->count);
121830ee2a98SJason J. Herne 	if (!keys)
121930ee2a98SJason J. Herne 		return -ENOMEM;
122030ee2a98SJason J. Herne 
122130ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
122230ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
122330ee2a98SJason J. Herne 	if (r) {
122430ee2a98SJason J. Herne 		r = -EFAULT;
122530ee2a98SJason J. Herne 		goto out;
122630ee2a98SJason J. Herne 	}
122730ee2a98SJason J. Herne 
122830ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
122914d4a425SDominik Dingel 	r = s390_enable_skey();
123014d4a425SDominik Dingel 	if (r)
123114d4a425SDominik Dingel 		goto out;
123230ee2a98SJason J. Herne 
1233d3ed1ceeSMartin Schwidefsky 	down_read(&current->mm->mmap_sem);
123430ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
123530ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
123630ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
123730ee2a98SJason J. Herne 			r = -EFAULT;
1238d3ed1ceeSMartin Schwidefsky 			break;
123930ee2a98SJason J. Herne 		}
124030ee2a98SJason J. Herne 
124130ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
124230ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
124330ee2a98SJason J. Herne 			r = -EINVAL;
1244d3ed1ceeSMartin Schwidefsky 			break;
124530ee2a98SJason J. Herne 		}
124630ee2a98SJason J. Herne 
1247fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
124830ee2a98SJason J. Herne 		if (r)
1249d3ed1ceeSMartin Schwidefsky 			break;
125030ee2a98SJason J. Herne 	}
1251d3ed1ceeSMartin Schwidefsky 	up_read(&current->mm->mmap_sem);
125230ee2a98SJason J. Herne out:
125330ee2a98SJason J. Herne 	kvfree(keys);
125430ee2a98SJason J. Herne 	return r;
125530ee2a98SJason J. Herne }
125630ee2a98SJason J. Herne 
1257b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
1258b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
1259b0c632dbSHeiko Carstens {
1260b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
1261b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
1262f2061656SDominik Dingel 	struct kvm_device_attr attr;
1263b0c632dbSHeiko Carstens 	int r;
1264b0c632dbSHeiko Carstens 
1265b0c632dbSHeiko Carstens 	switch (ioctl) {
1266ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
1267ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
1268ba5c1e9bSCarsten Otte 
1269ba5c1e9bSCarsten Otte 		r = -EFAULT;
1270ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
1271ba5c1e9bSCarsten Otte 			break;
1272ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
1273ba5c1e9bSCarsten Otte 		break;
1274ba5c1e9bSCarsten Otte 	}
1275d938dc55SCornelia Huck 	case KVM_ENABLE_CAP: {
1276d938dc55SCornelia Huck 		struct kvm_enable_cap cap;
1277d938dc55SCornelia Huck 		r = -EFAULT;
1278d938dc55SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
1279d938dc55SCornelia Huck 			break;
1280d938dc55SCornelia Huck 		r = kvm_vm_ioctl_enable_cap(kvm, &cap);
1281d938dc55SCornelia Huck 		break;
1282d938dc55SCornelia Huck 	}
128384223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
128484223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
128584223598SCornelia Huck 
128684223598SCornelia Huck 		r = -EINVAL;
128784223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
128884223598SCornelia Huck 			/* Set up dummy routing. */
128984223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
1290152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
129184223598SCornelia Huck 		}
129284223598SCornelia Huck 		break;
129384223598SCornelia Huck 	}
1294f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
1295f2061656SDominik Dingel 		r = -EFAULT;
1296f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1297f2061656SDominik Dingel 			break;
1298f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
1299f2061656SDominik Dingel 		break;
1300f2061656SDominik Dingel 	}
1301f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
1302f2061656SDominik Dingel 		r = -EFAULT;
1303f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1304f2061656SDominik Dingel 			break;
1305f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
1306f2061656SDominik Dingel 		break;
1307f2061656SDominik Dingel 	}
1308f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
1309f2061656SDominik Dingel 		r = -EFAULT;
1310f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
1311f2061656SDominik Dingel 			break;
1312f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
1313f2061656SDominik Dingel 		break;
1314f2061656SDominik Dingel 	}
131530ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
131630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
131730ee2a98SJason J. Herne 
131830ee2a98SJason J. Herne 		r = -EFAULT;
131930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
132030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
132130ee2a98SJason J. Herne 			break;
132230ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
132330ee2a98SJason J. Herne 		break;
132430ee2a98SJason J. Herne 	}
132530ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
132630ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
132730ee2a98SJason J. Herne 
132830ee2a98SJason J. Herne 		r = -EFAULT;
132930ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
133030ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
133130ee2a98SJason J. Herne 			break;
133230ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
133330ee2a98SJason J. Herne 		break;
133430ee2a98SJason J. Herne 	}
1335b0c632dbSHeiko Carstens 	default:
1336367e1319SAvi Kivity 		r = -ENOTTY;
1337b0c632dbSHeiko Carstens 	}
1338b0c632dbSHeiko Carstens 
1339b0c632dbSHeiko Carstens 	return r;
1340b0c632dbSHeiko Carstens }
1341b0c632dbSHeiko Carstens 
134245c9b47cSTony Krowiak static int kvm_s390_query_ap_config(u8 *config)
134345c9b47cSTony Krowiak {
134445c9b47cSTony Krowiak 	u32 fcn_code = 0x04000000UL;
134586044c8cSChristian Borntraeger 	u32 cc = 0;
134645c9b47cSTony Krowiak 
134786044c8cSChristian Borntraeger 	memset(config, 0, 128);
134845c9b47cSTony Krowiak 	asm volatile(
134945c9b47cSTony Krowiak 		"lgr 0,%1\n"
135045c9b47cSTony Krowiak 		"lgr 2,%2\n"
135145c9b47cSTony Krowiak 		".long 0xb2af0000\n"		/* PQAP(QCI) */
135286044c8cSChristian Borntraeger 		"0: ipm %0\n"
135345c9b47cSTony Krowiak 		"srl %0,28\n"
135486044c8cSChristian Borntraeger 		"1:\n"
135586044c8cSChristian Borntraeger 		EX_TABLE(0b, 1b)
135686044c8cSChristian Borntraeger 		: "+r" (cc)
135745c9b47cSTony Krowiak 		: "r" (fcn_code), "r" (config)
135845c9b47cSTony Krowiak 		: "cc", "0", "2", "memory"
135945c9b47cSTony Krowiak 	);
136045c9b47cSTony Krowiak 
136145c9b47cSTony Krowiak 	return cc;
136245c9b47cSTony Krowiak }
136345c9b47cSTony Krowiak 
136445c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
136545c9b47cSTony Krowiak {
136645c9b47cSTony Krowiak 	u8 config[128];
136745c9b47cSTony Krowiak 	int cc;
136845c9b47cSTony Krowiak 
1369a6aacc3fSHeiko Carstens 	if (test_facility(12)) {
137045c9b47cSTony Krowiak 		cc = kvm_s390_query_ap_config(config);
137145c9b47cSTony Krowiak 
137245c9b47cSTony Krowiak 		if (cc)
137345c9b47cSTony Krowiak 			pr_err("PQAP(QCI) failed with cc=%d", cc);
137445c9b47cSTony Krowiak 		else
137545c9b47cSTony Krowiak 			return config[0] & 0x40;
137645c9b47cSTony Krowiak 	}
137745c9b47cSTony Krowiak 
137845c9b47cSTony Krowiak 	return 0;
137945c9b47cSTony Krowiak }
138045c9b47cSTony Krowiak 
138145c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
138245c9b47cSTony Krowiak {
138345c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
138445c9b47cSTony Krowiak 
138545c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
138645c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
138745c9b47cSTony Krowiak 	else
138845c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
138945c9b47cSTony Krowiak }
139045c9b47cSTony Krowiak 
13919bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
13929d8d5786SMichael Mueller {
13939bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
13949bb0ec09SDavid Hildenbrand 
13959bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
13969bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
13979bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
13989d8d5786SMichael Mueller }
13999d8d5786SMichael Mueller 
1400c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
14015102ee87STony Krowiak {
14029d8d5786SMichael Mueller 	if (!test_kvm_facility(kvm, 76))
1403c54f0d6aSDavid Hildenbrand 		return;
14045102ee87STony Krowiak 
1405c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
140645c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
14075102ee87STony Krowiak 
1408ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
1409ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
1410ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
1411ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1412ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1413ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1414ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
14155102ee87STony Krowiak }
14165102ee87STony Krowiak 
14177d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
14187d43bafcSEugene (jno) Dvurechenski {
14197d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
14205e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
14217d43bafcSEugene (jno) Dvurechenski 	else
14227d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
14237d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
14247d43bafcSEugene (jno) Dvurechenski }
14257d43bafcSEugene (jno) Dvurechenski 
1426e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
1427b0c632dbSHeiko Carstens {
142876a6dd72SDavid Hildenbrand 	gfp_t alloc_flags = GFP_KERNEL;
14299d8d5786SMichael Mueller 	int i, rc;
1430b0c632dbSHeiko Carstens 	char debug_name[16];
1431f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
1432b0c632dbSHeiko Carstens 
1433e08b9637SCarsten Otte 	rc = -EINVAL;
1434e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
1435e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
1436e08b9637SCarsten Otte 		goto out_err;
1437e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
1438e08b9637SCarsten Otte 		goto out_err;
1439e08b9637SCarsten Otte #else
1440e08b9637SCarsten Otte 	if (type)
1441e08b9637SCarsten Otte 		goto out_err;
1442e08b9637SCarsten Otte #endif
1443e08b9637SCarsten Otte 
1444b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
1445b0c632dbSHeiko Carstens 	if (rc)
1446d89f5effSJan Kiszka 		goto out_err;
1447b0c632dbSHeiko Carstens 
1448b290411aSCarsten Otte 	rc = -ENOMEM;
1449b290411aSCarsten Otte 
14507d0a5e62SJanosch Frank 	ratelimit_state_init(&kvm->arch.sthyi_limit, 5 * HZ, 500);
14517d0a5e62SJanosch Frank 
14527d43bafcSEugene (jno) Dvurechenski 	kvm->arch.use_esca = 0; /* start with basic SCA */
145376a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
145476a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
14555e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
145676a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
1457b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
1458d89f5effSJan Kiszka 		goto out_err;
1459f6c137ffSChristian Borntraeger 	spin_lock(&kvm_lock);
1460c5c2c393SDavid Hildenbrand 	sca_offset += 16;
1461bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
1462c5c2c393SDavid Hildenbrand 		sca_offset = 0;
1463bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
1464bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
1465f6c137ffSChristian Borntraeger 	spin_unlock(&kvm_lock);
1466b0c632dbSHeiko Carstens 
1467b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
1468b0c632dbSHeiko Carstens 
14691cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
1470b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
147140f5b735SDominik Dingel 		goto out_err;
1472b0c632dbSHeiko Carstens 
1473c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
1474c54f0d6aSDavid Hildenbrand 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
1475c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
147640f5b735SDominik Dingel 		goto out_err;
14779d8d5786SMichael Mueller 
1478fb5bf93fSMichael Mueller 	/* Populate the facility mask initially. */
1479c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_mask, S390_lowcore.stfle_fac_list,
148004478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
14819d8d5786SMichael Mueller 	for (i = 0; i < S390_ARCH_FAC_LIST_SIZE_U64; i++) {
14829d8d5786SMichael Mueller 		if (i < kvm_s390_fac_list_mask_size())
1483c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] &= kvm_s390_fac_list_mask[i];
14849d8d5786SMichael Mueller 		else
1485c54f0d6aSDavid Hildenbrand 			kvm->arch.model.fac_mask[i] = 0UL;
14869d8d5786SMichael Mueller 	}
14879d8d5786SMichael Mueller 
1488981467c9SMichael Mueller 	/* Populate the facility list initially. */
1489c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
1490c54f0d6aSDavid Hildenbrand 	memcpy(kvm->arch.model.fac_list, kvm->arch.model.fac_mask,
1491981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1492981467c9SMichael Mueller 
149395ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
149495ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
149595ca2cb5SJanosch Frank 
14969bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
149737c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
14989d8d5786SMichael Mueller 
1499c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
15005102ee87STony Krowiak 
1501ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
15026d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
15036d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
15048a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
1505a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
1506ba5c1e9bSCarsten Otte 
1507b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
150878f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
1509b0c632dbSHeiko Carstens 
1510e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
1511e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
1512a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
1513e08b9637SCarsten Otte 	} else {
151432e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
1515a3a92c31SDominik Dingel 			kvm->arch.mem_limit = TASK_MAX_SIZE;
151632e6b236SGuenther Hutzl 		else
151732e6b236SGuenther Hutzl 			kvm->arch.mem_limit = min_t(unsigned long, TASK_MAX_SIZE,
151832e6b236SGuenther Hutzl 						    sclp.hamax + 1);
15196ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
1520598841caSCarsten Otte 		if (!kvm->arch.gmap)
152140f5b735SDominik Dingel 			goto out_err;
15222c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
152324eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
1524e08b9637SCarsten Otte 	}
1525fa6b7fe9SCornelia Huck 
1526fa6b7fe9SCornelia Huck 	kvm->arch.css_support = 0;
152784223598SCornelia Huck 	kvm->arch.use_irqchip = 0;
152872f25020SJason J. Herne 	kvm->arch.epoch = 0;
1529fa6b7fe9SCornelia Huck 
15308ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
1531a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
15328335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
15338ad35755SDavid Hildenbrand 
1534d89f5effSJan Kiszka 	return 0;
1535d89f5effSJan Kiszka out_err:
1536c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
153740f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
15387d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
153978f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
1540d89f5effSJan Kiszka 	return rc;
1541b0c632dbSHeiko Carstens }
1542b0c632dbSHeiko Carstens 
1543235539b4SLuiz Capitulino bool kvm_arch_has_vcpu_debugfs(void)
1544235539b4SLuiz Capitulino {
1545235539b4SLuiz Capitulino 	return false;
1546235539b4SLuiz Capitulino }
1547235539b4SLuiz Capitulino 
1548235539b4SLuiz Capitulino int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
1549235539b4SLuiz Capitulino {
1550235539b4SLuiz Capitulino 	return 0;
1551235539b4SLuiz Capitulino }
1552235539b4SLuiz Capitulino 
1553d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
1554d329c035SChristian Borntraeger {
1555d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
1556ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
155767335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
15583c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
1559bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
1560a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
156127e0393fSCarsten Otte 
156227e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
15636ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
156427e0393fSCarsten Otte 
1565e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
1566b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
1567d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
1568b31288faSKonstantin Weitz 
15696692cef3SChristian Borntraeger 	kvm_vcpu_uninit(vcpu);
1570b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
1571d329c035SChristian Borntraeger }
1572d329c035SChristian Borntraeger 
1573d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
1574d329c035SChristian Borntraeger {
1575d329c035SChristian Borntraeger 	unsigned int i;
1576988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
1577d329c035SChristian Borntraeger 
1578988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
1579988a2caeSGleb Natapov 		kvm_arch_vcpu_destroy(vcpu);
1580988a2caeSGleb Natapov 
1581988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
1582988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
1583d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
1584988a2caeSGleb Natapov 
1585988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
1586988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
1587d329c035SChristian Borntraeger }
1588d329c035SChristian Borntraeger 
1589b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
1590b0c632dbSHeiko Carstens {
1591d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
15927d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
1593d329c035SChristian Borntraeger 	debug_unregister(kvm->arch.dbf);
1594c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
159527e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
15966ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
1597841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
159867335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
1599a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
16008335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
1601b0c632dbSHeiko Carstens }
1602b0c632dbSHeiko Carstens 
1603b0c632dbSHeiko Carstens /* Section: vcpu related */
1604dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
1605b0c632dbSHeiko Carstens {
16066ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
160727e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
160827e0393fSCarsten Otte 		return -ENOMEM;
16092c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
1610dafd032aSDominik Dingel 
161127e0393fSCarsten Otte 	return 0;
161227e0393fSCarsten Otte }
161327e0393fSCarsten Otte 
1614a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
1615a6e2f683SEugene (jno) Dvurechenski {
1616a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
1617a6940674SDavid Hildenbrand 		return;
16185e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
16197d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
16207d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
16217d43bafcSEugene (jno) Dvurechenski 
16227d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
16237d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
16247d43bafcSEugene (jno) Dvurechenski 	} else {
1625bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1626a6e2f683SEugene (jno) Dvurechenski 
1627a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1628a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
1629a6e2f683SEugene (jno) Dvurechenski 	}
16305e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
16317d43bafcSEugene (jno) Dvurechenski }
1632a6e2f683SEugene (jno) Dvurechenski 
1633eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
1634a6e2f683SEugene (jno) Dvurechenski {
1635a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
1636a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1637a6940674SDavid Hildenbrand 
1638a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
1639a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1640a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1641a6940674SDavid Hildenbrand 	}
1642eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
1643eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
1644eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
16457d43bafcSEugene (jno) Dvurechenski 
1646eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
16477d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
16487d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
164925508824SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= 0x04U;
1650eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
16517d43bafcSEugene (jno) Dvurechenski 	} else {
1652eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
1653a6e2f683SEugene (jno) Dvurechenski 
1654eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
1655a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
1656a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
1657eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
1658a6e2f683SEugene (jno) Dvurechenski 	}
1659eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
16605e044315SEugene (jno) Dvurechenski }
16615e044315SEugene (jno) Dvurechenski 
16625e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
16635e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
16645e044315SEugene (jno) Dvurechenski {
16655e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
16665e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
16675e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
16685e044315SEugene (jno) Dvurechenski }
16695e044315SEugene (jno) Dvurechenski 
16705e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
16715e044315SEugene (jno) Dvurechenski {
16725e044315SEugene (jno) Dvurechenski 	int i;
16735e044315SEugene (jno) Dvurechenski 
16745e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
16755e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
16765e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
16775e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
16785e044315SEugene (jno) Dvurechenski }
16795e044315SEugene (jno) Dvurechenski 
16805e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
16815e044315SEugene (jno) Dvurechenski {
16825e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
16835e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
16845e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
16855e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
16865e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
16875e044315SEugene (jno) Dvurechenski 
16885e044315SEugene (jno) Dvurechenski 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
16895e044315SEugene (jno) Dvurechenski 	if (!new_sca)
16905e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
16915e044315SEugene (jno) Dvurechenski 
16925e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
16935e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
16945e044315SEugene (jno) Dvurechenski 
16955e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
16965e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
16975e044315SEugene (jno) Dvurechenski 
16985e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
16995e044315SEugene (jno) Dvurechenski 
17005e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
17015e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
17025e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
17035e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->ecb2 |= 0x04U;
17045e044315SEugene (jno) Dvurechenski 	}
17055e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
17065e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
17075e044315SEugene (jno) Dvurechenski 
17085e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
17095e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
17105e044315SEugene (jno) Dvurechenski 
17115e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
17125e044315SEugene (jno) Dvurechenski 
17138335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
17148335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
17155e044315SEugene (jno) Dvurechenski 	return 0;
17167d43bafcSEugene (jno) Dvurechenski }
1717a6e2f683SEugene (jno) Dvurechenski 
1718a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
1719a6e2f683SEugene (jno) Dvurechenski {
17205e044315SEugene (jno) Dvurechenski 	int rc;
17215e044315SEugene (jno) Dvurechenski 
1722a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
1723a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
1724a6940674SDavid Hildenbrand 			return true;
1725a6940674SDavid Hildenbrand 		return false;
1726a6940674SDavid Hildenbrand 	}
17275e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
17285e044315SEugene (jno) Dvurechenski 		return true;
172976a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
17305e044315SEugene (jno) Dvurechenski 		return false;
17315e044315SEugene (jno) Dvurechenski 
17325e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
17335e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
17345e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
17355e044315SEugene (jno) Dvurechenski 
17365e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
1737a6e2f683SEugene (jno) Dvurechenski }
1738a6e2f683SEugene (jno) Dvurechenski 
1739dafd032aSDominik Dingel int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
1740dafd032aSDominik Dingel {
1741dafd032aSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
1742dafd032aSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
174359674c1aSChristian Borntraeger 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
174459674c1aSChristian Borntraeger 				    KVM_SYNC_GPRS |
17459eed0735SChristian Borntraeger 				    KVM_SYNC_ACRS |
1746b028ee3eSDavid Hildenbrand 				    KVM_SYNC_CRS |
1747b028ee3eSDavid Hildenbrand 				    KVM_SYNC_ARCH0 |
1748b028ee3eSDavid Hildenbrand 				    KVM_SYNC_PFAULT;
174975a4615cSJulius Niedworok 	kvm_s390_set_prefix(vcpu, 0);
1750c6e5f166SFan Zhang 	if (test_kvm_facility(vcpu->kvm, 64))
1751c6e5f166SFan Zhang 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
1752f6aa6dc4SDavid Hildenbrand 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
1753f6aa6dc4SDavid Hildenbrand 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
1754f6aa6dc4SDavid Hildenbrand 	 */
1755f6aa6dc4SDavid Hildenbrand 	if (MACHINE_HAS_VX)
175668c55750SEric Farman 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
17576fd8e67dSDavid Hildenbrand 	else
17586fd8e67dSDavid Hildenbrand 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
1759dafd032aSDominik Dingel 
1760dafd032aSDominik Dingel 	if (kvm_is_ucontrol(vcpu->kvm))
1761dafd032aSDominik Dingel 		return __kvm_ucontrol_vcpu_init(vcpu);
1762dafd032aSDominik Dingel 
1763b0c632dbSHeiko Carstens 	return 0;
1764b0c632dbSHeiko Carstens }
1765b0c632dbSHeiko Carstens 
1766db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1767db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1768db0758b2SDavid Hildenbrand {
1769db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
17709c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1771db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
17729c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1773db0758b2SDavid Hildenbrand }
1774db0758b2SDavid Hildenbrand 
1775db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1776db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1777db0758b2SDavid Hildenbrand {
1778db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
17799c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1780db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
1781db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
17829c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1783db0758b2SDavid Hildenbrand }
1784db0758b2SDavid Hildenbrand 
1785db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1786db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1787db0758b2SDavid Hildenbrand {
1788db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
1789db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
1790db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
1791db0758b2SDavid Hildenbrand }
1792db0758b2SDavid Hildenbrand 
1793db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1794db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1795db0758b2SDavid Hildenbrand {
1796db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
1797db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
1798db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
1799db0758b2SDavid Hildenbrand }
1800db0758b2SDavid Hildenbrand 
1801db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1802db0758b2SDavid Hildenbrand {
1803db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1804db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
1805db0758b2SDavid Hildenbrand 	preempt_enable();
1806db0758b2SDavid Hildenbrand }
1807db0758b2SDavid Hildenbrand 
1808db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
1809db0758b2SDavid Hildenbrand {
1810db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1811db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
1812db0758b2SDavid Hildenbrand 	preempt_enable();
1813db0758b2SDavid Hildenbrand }
1814db0758b2SDavid Hildenbrand 
18154287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
18164287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
18174287f247SDavid Hildenbrand {
1818db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
18199c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
1820db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
1821db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
18224287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
18239c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
1824db0758b2SDavid Hildenbrand 	preempt_enable();
18254287f247SDavid Hildenbrand }
18264287f247SDavid Hildenbrand 
1827db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
18284287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
18294287f247SDavid Hildenbrand {
18309c23a131SDavid Hildenbrand 	unsigned int seq;
1831db0758b2SDavid Hildenbrand 	__u64 value;
1832db0758b2SDavid Hildenbrand 
1833db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
18344287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
1835db0758b2SDavid Hildenbrand 
18369c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
18379c23a131SDavid Hildenbrand 	do {
18389c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
18399c23a131SDavid Hildenbrand 		/*
18409c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
18419c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
18429c23a131SDavid Hildenbrand 		 */
18439c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
1844db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
18459c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
18469c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
1847db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
18489c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
18499c23a131SDavid Hildenbrand 	preempt_enable();
1850db0758b2SDavid Hildenbrand 	return value;
18514287f247SDavid Hildenbrand }
18524287f247SDavid Hildenbrand 
1853b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1854b0c632dbSHeiko Carstens {
18559977e886SHendrik Brueckner 
185637d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
1857805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
18585ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
1859db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
186001a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
1861b0c632dbSHeiko Carstens }
1862b0c632dbSHeiko Carstens 
1863b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
1864b0c632dbSHeiko Carstens {
186501a745acSDavid Hildenbrand 	vcpu->cpu = -1;
18665ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
1867db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
1868805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
186937d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
187037d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
18719977e886SHendrik Brueckner 
1872b0c632dbSHeiko Carstens }
1873b0c632dbSHeiko Carstens 
1874b0c632dbSHeiko Carstens static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
1875b0c632dbSHeiko Carstens {
1876b0c632dbSHeiko Carstens 	/* this equals initial cpu reset in pop, but we don't switch to ESA */
1877b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.mask = 0UL;
1878b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gpsw.addr = 0UL;
18798d26cf7bSChristian Borntraeger 	kvm_s390_set_prefix(vcpu, 0);
18804287f247SDavid Hildenbrand 	kvm_s390_set_cpu_timer(vcpu, 0);
1881b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->ckc       = 0UL;
1882b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->todpr     = 0;
1883b0c632dbSHeiko Carstens 	memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
1884b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[0]  = 0xE0UL;
1885b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
18869abc2a08SDavid Hildenbrand 	/* make sure the new fpc will be lazily loaded */
18879abc2a08SDavid Hildenbrand 	save_fpu_regs();
18889abc2a08SDavid Hildenbrand 	current->thread.fpu.fpc = 0;
1889b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->gbea = 1;
1890672550fbSChristian Borntraeger 	vcpu->arch.sie_block->pp = 0;
18913c038e6bSDominik Dingel 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
18923c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
18936352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
18946852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
18952ed10cc1SJens Freimann 	kvm_s390_clear_local_irqs(vcpu);
1896b0c632dbSHeiko Carstens }
1897b0c632dbSHeiko Carstens 
189831928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
189942897d86SMarcelo Tosatti {
190072f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
1901fdf03650SFan Zhang 	preempt_disable();
190272f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
1903fdf03650SFan Zhang 	preempt_enable();
190472f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
190525508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
1906dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
1907eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
190825508824SDavid Hildenbrand 	}
19096502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
19106502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
191137d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
191237d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
191342897d86SMarcelo Tosatti }
191442897d86SMarcelo Tosatti 
19155102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
19165102ee87STony Krowiak {
19179d8d5786SMichael Mueller 	if (!test_kvm_facility(vcpu->kvm, 76))
19185102ee87STony Krowiak 		return;
19195102ee87STony Krowiak 
1920a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
1921a374e892STony Krowiak 
1922a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.aes_kw)
1923a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
1924a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
1925a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
1926a374e892STony Krowiak 
19275102ee87STony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
19285102ee87STony Krowiak }
19295102ee87STony Krowiak 
1930b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
1931b31605c1SDominik Dingel {
1932b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
1933b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
1934b31605c1SDominik Dingel }
1935b31605c1SDominik Dingel 
1936b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
1937b31605c1SDominik Dingel {
1938b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
1939b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
1940b31605c1SDominik Dingel 		return -ENOMEM;
1941b31605c1SDominik Dingel 
1942b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 |= 0x80;
1943b31605c1SDominik Dingel 	vcpu->arch.sie_block->ecb2 &= ~0x08;
1944b31605c1SDominik Dingel 	return 0;
1945b31605c1SDominik Dingel }
1946b31605c1SDominik Dingel 
194791520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
194891520f1aSMichael Mueller {
194991520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
195091520f1aSMichael Mueller 
195191520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
195280bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
1953c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
195491520f1aSMichael Mueller }
195591520f1aSMichael Mueller 
1956b0c632dbSHeiko Carstens int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1957b0c632dbSHeiko Carstens {
1958b31605c1SDominik Dingel 	int rc = 0;
1959b31288faSKonstantin Weitz 
19609e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
19619e6dabefSCornelia Huck 						    CPUSTAT_SM |
1962a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
1963a4a4f191SGuenther Hutzl 
196453df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
1965805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
196653df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
1967805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
1968a4a4f191SGuenther Hutzl 
196991520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
197091520f1aSMichael Mueller 
1971bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
1972bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
1973bdab09f3SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= 0x02;
1974bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
1975bd50e8ecSDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= 0x04;
1976f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
19777feb6bb8SMichael Mueller 		vcpu->arch.sie_block->ecb |= 0x10;
19787feb6bb8SMichael Mueller 
1979873b425eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi)
1980d6af0b49SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= 0x08;
1981cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
1982cd1836f5SJanosch Frank 		vcpu->arch.sie_block->ecb2 |= 0x20;
198348ee7d3aSDavid Hildenbrand 	vcpu->arch.sie_block->eca = 0x1002000U;
198448ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
198548ee7d3aSDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x80000000U;
198611ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
198711ad65b7SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x40000000U;
198837c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
1989217a4406SHeiko Carstens 		vcpu->arch.sie_block->eca |= 1;
199037c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
1991ea5f4969SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= 0x10000000U;
199218280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
199313211ea7SEric Farman 		vcpu->arch.sie_block->eca |= 0x00020000;
199413211ea7SEric Farman 		vcpu->arch.sie_block->ecd |= 0x20000000;
199513211ea7SEric Farman 	}
1996c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
1997492d8642SThomas Huth 	vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
19985a5e6536SMatthew Rosato 
1999e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
2000b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
2001b31605c1SDominik Dingel 		if (rc)
2002b31605c1SDominik Dingel 			return rc;
2003b31288faSKonstantin Weitz 	}
20040ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2005ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
20069d8d5786SMichael Mueller 
20075102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
20085102ee87STony Krowiak 
2009b31605c1SDominik Dingel 	return rc;
2010b0c632dbSHeiko Carstens }
2011b0c632dbSHeiko Carstens 
2012b0c632dbSHeiko Carstens struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2013b0c632dbSHeiko Carstens 				      unsigned int id)
2014b0c632dbSHeiko Carstens {
20154d47555aSCarsten Otte 	struct kvm_vcpu *vcpu;
20167feb6bb8SMichael Mueller 	struct sie_page *sie_page;
20174d47555aSCarsten Otte 	int rc = -EINVAL;
2018b0c632dbSHeiko Carstens 
20194215825eSDavid Hildenbrand 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
20204d47555aSCarsten Otte 		goto out;
20214d47555aSCarsten Otte 
20224d47555aSCarsten Otte 	rc = -ENOMEM;
20234d47555aSCarsten Otte 
2024b110feafSMichael Mueller 	vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2025b0c632dbSHeiko Carstens 	if (!vcpu)
20264d47555aSCarsten Otte 		goto out;
2027b0c632dbSHeiko Carstens 
20287feb6bb8SMichael Mueller 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
20297feb6bb8SMichael Mueller 	if (!sie_page)
2030b0c632dbSHeiko Carstens 		goto out_free_cpu;
2031b0c632dbSHeiko Carstens 
20327feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
20337feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
20347feb6bb8SMichael Mueller 
2035efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
2036efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
2037efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
2038efed1104SDavid Hildenbrand 
2039b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icpua = id;
2040ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
2041ba5c1e9bSCarsten Otte 	vcpu->arch.local_int.float_int = &kvm->arch.float_int;
2042d0321a24SChristian Borntraeger 	vcpu->arch.local_int.wq = &vcpu->wq;
20435288fbf0SChristian Borntraeger 	vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
20449c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
2045ba5c1e9bSCarsten Otte 
2046b0c632dbSHeiko Carstens 	rc = kvm_vcpu_init(vcpu, kvm, id);
2047b0c632dbSHeiko Carstens 	if (rc)
20489abc2a08SDavid Hildenbrand 		goto out_free_sie_block;
20498335713aSChristian Borntraeger 	VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2050b0c632dbSHeiko Carstens 		 vcpu->arch.sie_block);
2051ade38c31SCornelia Huck 	trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2052b0c632dbSHeiko Carstens 
2053b0c632dbSHeiko Carstens 	return vcpu;
20547b06bf2fSWei Yongjun out_free_sie_block:
20557b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
2056b0c632dbSHeiko Carstens out_free_cpu:
2057b110feafSMichael Mueller 	kmem_cache_free(kvm_vcpu_cache, vcpu);
20584d47555aSCarsten Otte out:
2059b0c632dbSHeiko Carstens 	return ERR_PTR(rc);
2060b0c632dbSHeiko Carstens }
2061b0c632dbSHeiko Carstens 
2062b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2063b0c632dbSHeiko Carstens {
20649a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
2065b0c632dbSHeiko Carstens }
2066b0c632dbSHeiko Carstens 
206727406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
206849b99e1eSChristian Borntraeger {
2069805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
207061a6df54SDavid Hildenbrand 	exit_sie(vcpu);
207149b99e1eSChristian Borntraeger }
207249b99e1eSChristian Borntraeger 
207327406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
207449b99e1eSChristian Borntraeger {
2075805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
207649b99e1eSChristian Borntraeger }
207749b99e1eSChristian Borntraeger 
20788e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
20798e236546SChristian Borntraeger {
2080805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
208161a6df54SDavid Hildenbrand 	exit_sie(vcpu);
20828e236546SChristian Borntraeger }
20838e236546SChristian Borntraeger 
20848e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
20858e236546SChristian Borntraeger {
20869bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
20878e236546SChristian Borntraeger }
20888e236546SChristian Borntraeger 
208949b99e1eSChristian Borntraeger /*
209049b99e1eSChristian Borntraeger  * Kick a guest cpu out of SIE and wait until SIE is not running.
209149b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
209249b99e1eSChristian Borntraeger  * return immediately. */
209349b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
209449b99e1eSChristian Borntraeger {
2095805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
209649b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
209749b99e1eSChristian Borntraeger 		cpu_relax();
209849b99e1eSChristian Borntraeger }
209949b99e1eSChristian Borntraeger 
21008e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
21018e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
210249b99e1eSChristian Borntraeger {
21038e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
21048e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
210549b99e1eSChristian Borntraeger }
210649b99e1eSChristian Borntraeger 
2107414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
2108414d3b07SMartin Schwidefsky 			      unsigned long end)
21092c70fe44SChristian Borntraeger {
21102c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
21112c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
2112414d3b07SMartin Schwidefsky 	unsigned long prefix;
2113414d3b07SMartin Schwidefsky 	int i;
21142c70fe44SChristian Borntraeger 
211565d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
211665d0b0d4SDavid Hildenbrand 		return;
2117414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
2118414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
2119414d3b07SMartin Schwidefsky 		return;
21202c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
21212c70fe44SChristian Borntraeger 		/* match against both prefix pages */
2122414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
2123414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
2124414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
2125414d3b07SMartin Schwidefsky 				   start, end);
21268e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
21272c70fe44SChristian Borntraeger 		}
21282c70fe44SChristian Borntraeger 	}
21292c70fe44SChristian Borntraeger }
21302c70fe44SChristian Borntraeger 
2131b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
2132b6d33834SChristoffer Dall {
2133b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
2134b6d33834SChristoffer Dall 	BUG();
2135b6d33834SChristoffer Dall 	return 0;
2136b6d33834SChristoffer Dall }
2137b6d33834SChristoffer Dall 
213814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
213914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
214014eebd91SCarsten Otte {
214114eebd91SCarsten Otte 	int r = -EINVAL;
214214eebd91SCarsten Otte 
214314eebd91SCarsten Otte 	switch (reg->id) {
214429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
214529b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
214629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
214729b7c71bSCarsten Otte 		break;
214829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
214929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
215029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
215129b7c71bSCarsten Otte 		break;
215246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
21534287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
215446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
215546a6dd1cSJason J. herne 		break;
215646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
215746a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
215846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
215946a6dd1cSJason J. herne 		break;
2160536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2161536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
2162536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2163536336c2SDominik Dingel 		break;
2164536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2165536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
2166536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2167536336c2SDominik Dingel 		break;
2168536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2169536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
2170536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2171536336c2SDominik Dingel 		break;
2172672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2173672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
2174672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2175672550fbSChristian Borntraeger 		break;
2176afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2177afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
2178afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2179afa45ff5SChristian Borntraeger 		break;
218014eebd91SCarsten Otte 	default:
218114eebd91SCarsten Otte 		break;
218214eebd91SCarsten Otte 	}
218314eebd91SCarsten Otte 
218414eebd91SCarsten Otte 	return r;
218514eebd91SCarsten Otte }
218614eebd91SCarsten Otte 
218714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
218814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
218914eebd91SCarsten Otte {
219014eebd91SCarsten Otte 	int r = -EINVAL;
21914287f247SDavid Hildenbrand 	__u64 val;
219214eebd91SCarsten Otte 
219314eebd91SCarsten Otte 	switch (reg->id) {
219429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
219529b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
219629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
219729b7c71bSCarsten Otte 		break;
219829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
219929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
220029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
220129b7c71bSCarsten Otte 		break;
220246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
22034287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
22044287f247SDavid Hildenbrand 		if (!r)
22054287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
220646a6dd1cSJason J. herne 		break;
220746a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
220846a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
220946a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
221046a6dd1cSJason J. herne 		break;
2211536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
2212536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
2213536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
22149fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
22159fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2216536336c2SDominik Dingel 		break;
2217536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
2218536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
2219536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2220536336c2SDominik Dingel 		break;
2221536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
2222536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
2223536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
2224536336c2SDominik Dingel 		break;
2225672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
2226672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
2227672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
2228672550fbSChristian Borntraeger 		break;
2229afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
2230afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
2231afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
2232afa45ff5SChristian Borntraeger 		break;
223314eebd91SCarsten Otte 	default:
223414eebd91SCarsten Otte 		break;
223514eebd91SCarsten Otte 	}
223614eebd91SCarsten Otte 
223714eebd91SCarsten Otte 	return r;
223814eebd91SCarsten Otte }
2239b6d33834SChristoffer Dall 
2240b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
2241b0c632dbSHeiko Carstens {
2242b0c632dbSHeiko Carstens 	kvm_s390_vcpu_initial_reset(vcpu);
2243b0c632dbSHeiko Carstens 	return 0;
2244b0c632dbSHeiko Carstens }
2245b0c632dbSHeiko Carstens 
2246b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2247b0c632dbSHeiko Carstens {
22485a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
2249b0c632dbSHeiko Carstens 	return 0;
2250b0c632dbSHeiko Carstens }
2251b0c632dbSHeiko Carstens 
2252b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
2253b0c632dbSHeiko Carstens {
22545a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
2255b0c632dbSHeiko Carstens 	return 0;
2256b0c632dbSHeiko Carstens }
2257b0c632dbSHeiko Carstens 
2258b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
2259b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2260b0c632dbSHeiko Carstens {
226159674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
2262b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
2263b0c632dbSHeiko Carstens 	return 0;
2264b0c632dbSHeiko Carstens }
2265b0c632dbSHeiko Carstens 
2266b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
2267b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
2268b0c632dbSHeiko Carstens {
226959674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
2270b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
2271b0c632dbSHeiko Carstens 	return 0;
2272b0c632dbSHeiko Carstens }
2273b0c632dbSHeiko Carstens 
2274b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2275b0c632dbSHeiko Carstens {
22764725c860SMartin Schwidefsky 	if (test_fp_ctl(fpu->fpc))
22774725c860SMartin Schwidefsky 		return -EINVAL;
2278e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
22799abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2280a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
2281a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
22829abc2a08SDavid Hildenbrand 	else
2283a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
2284b0c632dbSHeiko Carstens 	return 0;
2285b0c632dbSHeiko Carstens }
2286b0c632dbSHeiko Carstens 
2287b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
2288b0c632dbSHeiko Carstens {
22899abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
22909abc2a08SDavid Hildenbrand 	save_fpu_regs();
22919abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
2292a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
2293a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
22949abc2a08SDavid Hildenbrand 	else
2295a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
2296e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
2297b0c632dbSHeiko Carstens 	return 0;
2298b0c632dbSHeiko Carstens }
2299b0c632dbSHeiko Carstens 
2300b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
2301b0c632dbSHeiko Carstens {
2302b0c632dbSHeiko Carstens 	int rc = 0;
2303b0c632dbSHeiko Carstens 
23047a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
2305b0c632dbSHeiko Carstens 		rc = -EBUSY;
2306d7b0b5ebSCarsten Otte 	else {
2307d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
2308d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
2309d7b0b5ebSCarsten Otte 	}
2310b0c632dbSHeiko Carstens 	return rc;
2311b0c632dbSHeiko Carstens }
2312b0c632dbSHeiko Carstens 
2313b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
2314b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
2315b0c632dbSHeiko Carstens {
2316b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
2317b0c632dbSHeiko Carstens }
2318b0c632dbSHeiko Carstens 
231927291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
232027291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
232127291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
232227291e21SDavid Hildenbrand 
2323d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2324d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
2325b0c632dbSHeiko Carstens {
232627291e21SDavid Hildenbrand 	int rc = 0;
232727291e21SDavid Hildenbrand 
232827291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
232927291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
233027291e21SDavid Hildenbrand 
23312de3bfc2SDavid Hildenbrand 	if (dbg->control & ~VALID_GUESTDBG_FLAGS)
233227291e21SDavid Hildenbrand 		return -EINVAL;
233389b5b4deSDavid Hildenbrand 	if (!sclp.has_gpere)
233489b5b4deSDavid Hildenbrand 		return -EINVAL;
233527291e21SDavid Hildenbrand 
233627291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
233727291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
233827291e21SDavid Hildenbrand 		/* enforce guest PER */
2339805de8f4SPeter Zijlstra 		atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
234027291e21SDavid Hildenbrand 
234127291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
234227291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
234327291e21SDavid Hildenbrand 	} else {
2344805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
234527291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
234627291e21SDavid Hildenbrand 	}
234727291e21SDavid Hildenbrand 
234827291e21SDavid Hildenbrand 	if (rc) {
234927291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
235027291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
2351805de8f4SPeter Zijlstra 		atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
235227291e21SDavid Hildenbrand 	}
235327291e21SDavid Hildenbrand 
235427291e21SDavid Hildenbrand 	return rc;
2355b0c632dbSHeiko Carstens }
2356b0c632dbSHeiko Carstens 
235762d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
235862d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
235962d9f0dbSMarcelo Tosatti {
23606352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
23616352e4d2SDavid Hildenbrand 	return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
23626352e4d2SDavid Hildenbrand 				       KVM_MP_STATE_OPERATING;
236362d9f0dbSMarcelo Tosatti }
236462d9f0dbSMarcelo Tosatti 
236562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
236662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
236762d9f0dbSMarcelo Tosatti {
23686352e4d2SDavid Hildenbrand 	int rc = 0;
23696352e4d2SDavid Hildenbrand 
23706352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
23716352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
23726352e4d2SDavid Hildenbrand 
23736352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
23746352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
23756352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_stop(vcpu);
23766352e4d2SDavid Hildenbrand 		break;
23776352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
23786352e4d2SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
23796352e4d2SDavid Hildenbrand 		break;
23806352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
23816352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
23826352e4d2SDavid Hildenbrand 		/* fall through - CHECK_STOP and LOAD are not supported yet */
23836352e4d2SDavid Hildenbrand 	default:
23846352e4d2SDavid Hildenbrand 		rc = -ENXIO;
23856352e4d2SDavid Hildenbrand 	}
23866352e4d2SDavid Hildenbrand 
23876352e4d2SDavid Hildenbrand 	return rc;
238862d9f0dbSMarcelo Tosatti }
238962d9f0dbSMarcelo Tosatti 
23908ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
23918ad35755SDavid Hildenbrand {
23928ad35755SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
23938ad35755SDavid Hildenbrand }
23948ad35755SDavid Hildenbrand 
23952c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
23962c70fe44SChristian Borntraeger {
23978ad35755SDavid Hildenbrand retry:
23988e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
2399586b7ccdSChristian Borntraeger 	if (!vcpu->requests)
2400586b7ccdSChristian Borntraeger 		return 0;
24012c70fe44SChristian Borntraeger 	/*
24022c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2403b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
24042c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
24052c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
24062c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
24072c70fe44SChristian Borntraeger 	 */
24088ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
24092c70fe44SChristian Borntraeger 		int rc;
2410b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
2411fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
2412b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
2413aca411a4SJulius Niedworok 		if (rc) {
2414aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
24152c70fe44SChristian Borntraeger 			return rc;
2416aca411a4SJulius Niedworok 		}
24178ad35755SDavid Hildenbrand 		goto retry;
24182c70fe44SChristian Borntraeger 	}
24198ad35755SDavid Hildenbrand 
2420d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
2421d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
2422d3d692c8SDavid Hildenbrand 		goto retry;
2423d3d692c8SDavid Hildenbrand 	}
2424d3d692c8SDavid Hildenbrand 
24258ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
24268ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
24278ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
2428805de8f4SPeter Zijlstra 			atomic_or(CPUSTAT_IBS,
24298ad35755SDavid Hildenbrand 					&vcpu->arch.sie_block->cpuflags);
24308ad35755SDavid Hildenbrand 		}
24318ad35755SDavid Hildenbrand 		goto retry;
24328ad35755SDavid Hildenbrand 	}
24338ad35755SDavid Hildenbrand 
24348ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
24358ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
24368ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
2437805de8f4SPeter Zijlstra 			atomic_andnot(CPUSTAT_IBS,
24388ad35755SDavid Hildenbrand 					  &vcpu->arch.sie_block->cpuflags);
24398ad35755SDavid Hildenbrand 		}
24408ad35755SDavid Hildenbrand 		goto retry;
24418ad35755SDavid Hildenbrand 	}
24428ad35755SDavid Hildenbrand 
24436502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
24446502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
24456502a34cSDavid Hildenbrand 		goto retry;
24466502a34cSDavid Hildenbrand 	}
24476502a34cSDavid Hildenbrand 
24480759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
24490759d068SDavid Hildenbrand 	clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
24500759d068SDavid Hildenbrand 
24512c70fe44SChristian Borntraeger 	return 0;
24522c70fe44SChristian Borntraeger }
24532c70fe44SChristian Borntraeger 
245425ed1675SDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm, u64 tod)
245525ed1675SDavid Hildenbrand {
245625ed1675SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
245725ed1675SDavid Hildenbrand 	int i;
245825ed1675SDavid Hildenbrand 
245925ed1675SDavid Hildenbrand 	mutex_lock(&kvm->lock);
246025ed1675SDavid Hildenbrand 	preempt_disable();
246125ed1675SDavid Hildenbrand 	kvm->arch.epoch = tod - get_tod_clock();
246225ed1675SDavid Hildenbrand 	kvm_s390_vcpu_block_all(kvm);
246325ed1675SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm)
246425ed1675SDavid Hildenbrand 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
246525ed1675SDavid Hildenbrand 	kvm_s390_vcpu_unblock_all(kvm);
246625ed1675SDavid Hildenbrand 	preempt_enable();
246725ed1675SDavid Hildenbrand 	mutex_unlock(&kvm->lock);
246825ed1675SDavid Hildenbrand }
246925ed1675SDavid Hildenbrand 
2470fa576c58SThomas Huth /**
2471fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
2472fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
2473fa576c58SThomas Huth  * @gpa: Guest physical address
2474fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
2475fa576c58SThomas Huth  *
2476fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
2477fa576c58SThomas Huth  *
2478fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
2479fa576c58SThomas Huth  */
2480fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
248124eb3a82SDominik Dingel {
2482527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
2483527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
248424eb3a82SDominik Dingel }
248524eb3a82SDominik Dingel 
24863c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
24873c038e6bSDominik Dingel 				      unsigned long token)
24883c038e6bSDominik Dingel {
24893c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
2490383d0b05SJens Freimann 	struct kvm_s390_irq irq;
24913c038e6bSDominik Dingel 
24923c038e6bSDominik Dingel 	if (start_token) {
2493383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
2494383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
2495383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
24963c038e6bSDominik Dingel 	} else {
24973c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
2498383d0b05SJens Freimann 		inti.parm64 = token;
24993c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
25003c038e6bSDominik Dingel 	}
25013c038e6bSDominik Dingel }
25023c038e6bSDominik Dingel 
25033c038e6bSDominik Dingel void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
25043c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
25053c038e6bSDominik Dingel {
25063c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
25073c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
25083c038e6bSDominik Dingel }
25093c038e6bSDominik Dingel 
25103c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
25113c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
25123c038e6bSDominik Dingel {
25133c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
25143c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
25153c038e6bSDominik Dingel }
25163c038e6bSDominik Dingel 
25173c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
25183c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
25193c038e6bSDominik Dingel {
25203c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
25213c038e6bSDominik Dingel }
25223c038e6bSDominik Dingel 
25233c038e6bSDominik Dingel bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
25243c038e6bSDominik Dingel {
25253c038e6bSDominik Dingel 	/*
25263c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
25273c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
25283c038e6bSDominik Dingel 	 */
25293c038e6bSDominik Dingel 	return true;
25303c038e6bSDominik Dingel }
25313c038e6bSDominik Dingel 
25323c038e6bSDominik Dingel static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
25333c038e6bSDominik Dingel {
25343c038e6bSDominik Dingel 	hva_t hva;
25353c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
25363c038e6bSDominik Dingel 	int rc;
25373c038e6bSDominik Dingel 
25383c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
25393c038e6bSDominik Dingel 		return 0;
25403c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
25413c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
25423c038e6bSDominik Dingel 		return 0;
25433c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
25443c038e6bSDominik Dingel 		return 0;
25459a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
25463c038e6bSDominik Dingel 		return 0;
25473c038e6bSDominik Dingel 	if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
25483c038e6bSDominik Dingel 		return 0;
25493c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
25503c038e6bSDominik Dingel 		return 0;
25513c038e6bSDominik Dingel 
255281480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
255381480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
255481480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
25553c038e6bSDominik Dingel 		return 0;
25563c038e6bSDominik Dingel 
25573c038e6bSDominik Dingel 	rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
25583c038e6bSDominik Dingel 	return rc;
25593c038e6bSDominik Dingel }
25603c038e6bSDominik Dingel 
25613fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
2562b0c632dbSHeiko Carstens {
25633fb4c40fSThomas Huth 	int rc, cpuflags;
2564e168bf8dSCarsten Otte 
25653c038e6bSDominik Dingel 	/*
25663c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
25673c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
25683c038e6bSDominik Dingel 	 * handled outside the worker.
25693c038e6bSDominik Dingel 	 */
25703c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
25713c038e6bSDominik Dingel 
25727ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
25737ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
2574b0c632dbSHeiko Carstens 
2575b0c632dbSHeiko Carstens 	if (need_resched())
2576b0c632dbSHeiko Carstens 		schedule();
2577b0c632dbSHeiko Carstens 
2578d3a73acbSMartin Schwidefsky 	if (test_cpu_flag(CIF_MCCK_PENDING))
257971cde587SChristian Borntraeger 		s390_handle_mcck();
258071cde587SChristian Borntraeger 
258179395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
258279395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
258379395031SJens Freimann 		if (rc)
258479395031SJens Freimann 			return rc;
258579395031SJens Freimann 	}
25860ff31867SCarsten Otte 
25872c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
25882c70fe44SChristian Borntraeger 	if (rc)
25892c70fe44SChristian Borntraeger 		return rc;
25902c70fe44SChristian Borntraeger 
259127291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
259227291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
259327291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
259427291e21SDavid Hildenbrand 	}
259527291e21SDavid Hildenbrand 
2596b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
25973fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
25983fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
25993fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
26002b29a9fdSDominik Dingel 
26013fb4c40fSThomas Huth 	return 0;
26023fb4c40fSThomas Huth }
26033fb4c40fSThomas Huth 
2604492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
2605492d8642SThomas Huth {
260656317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
260756317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
260856317920SDavid Hildenbrand 	};
260956317920SDavid Hildenbrand 	u8 opcode, ilen;
2610492d8642SThomas Huth 	int rc;
2611492d8642SThomas Huth 
2612492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
2613492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
2614492d8642SThomas Huth 
2615492d8642SThomas Huth 	/*
2616492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
2617492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
2618492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
2619492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
2620492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
2621492d8642SThomas Huth 	 * to be able to forward the PSW.
2622492d8642SThomas Huth 	 */
26233fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
262456317920SDavid Hildenbrand 	ilen = insn_length(opcode);
26259b0d721aSDavid Hildenbrand 	if (rc < 0) {
26269b0d721aSDavid Hildenbrand 		return rc;
26279b0d721aSDavid Hildenbrand 	} else if (rc) {
26289b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
26299b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
26309b0d721aSDavid Hildenbrand 		 * nullification if necessary.
26319b0d721aSDavid Hildenbrand 		 */
26329b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
26339b0d721aSDavid Hildenbrand 		ilen = 4;
26349b0d721aSDavid Hildenbrand 	}
263556317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
263656317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
263756317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
2638492d8642SThomas Huth }
2639492d8642SThomas Huth 
26403fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
26413fb4c40fSThomas Huth {
26422b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
26432b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
26442b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
26452b29a9fdSDominik Dingel 
264627291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
264727291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
264827291e21SDavid Hildenbrand 
26497ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
26507ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
265171f116bfSDavid Hildenbrand 
265271f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
265371f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
265471f116bfSDavid Hildenbrand 
265571f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
265671f116bfSDavid Hildenbrand 			return rc;
265771f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
265871f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
265971f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
266071f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
266171f116bfSDavid Hildenbrand 		return -EREMOTE;
266271f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
266371f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
266471f116bfSDavid Hildenbrand 		return 0;
2665210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
2666210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
2667210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
2668210b1607SThomas Huth 						current->thread.gmap_addr;
2669210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
267071f116bfSDavid Hildenbrand 		return -EREMOTE;
267124eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
26723c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
267324eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
267471f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
267571f116bfSDavid Hildenbrand 			return 0;
267671f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
2677fa576c58SThomas Huth 	}
267871f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
26793fb4c40fSThomas Huth }
26803fb4c40fSThomas Huth 
26813fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
26823fb4c40fSThomas Huth {
26833fb4c40fSThomas Huth 	int rc, exit_reason;
26843fb4c40fSThomas Huth 
2685800c1065SThomas Huth 	/*
2686800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2687800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
2688800c1065SThomas Huth 	 */
2689800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2690800c1065SThomas Huth 
2691a76ccff6SThomas Huth 	do {
26923fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
26933fb4c40fSThomas Huth 		if (rc)
2694a76ccff6SThomas Huth 			break;
26953fb4c40fSThomas Huth 
2696800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
26973fb4c40fSThomas Huth 		/*
2698a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
2699a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
27003fb4c40fSThomas Huth 		 */
27010097d12eSChristian Borntraeger 		local_irq_disable();
27026edaa530SPaolo Bonzini 		guest_enter_irqoff();
2703db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
27040097d12eSChristian Borntraeger 		local_irq_enable();
2705a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
2706a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
27070097d12eSChristian Borntraeger 		local_irq_disable();
2708db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
27096edaa530SPaolo Bonzini 		guest_exit_irqoff();
27100097d12eSChristian Borntraeger 		local_irq_enable();
2711800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
27123fb4c40fSThomas Huth 
27133fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
271427291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
27153fb4c40fSThomas Huth 
2716800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
2717e168bf8dSCarsten Otte 	return rc;
2718b0c632dbSHeiko Carstens }
2719b0c632dbSHeiko Carstens 
2720b028ee3eSDavid Hildenbrand static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2721b028ee3eSDavid Hildenbrand {
2722b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
2723b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
2724b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
2725b028ee3eSDavid Hildenbrand 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
2726b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
2727b028ee3eSDavid Hildenbrand 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
2728d3d692c8SDavid Hildenbrand 		/* some control register changes require a tlb flush */
2729d3d692c8SDavid Hildenbrand 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2730b028ee3eSDavid Hildenbrand 	}
2731b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
27324287f247SDavid Hildenbrand 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
2733b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
2734b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
2735b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
2736b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
2737b028ee3eSDavid Hildenbrand 	}
2738b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
2739b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
2740b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
2741b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
27429fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
27439fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
2744b028ee3eSDavid Hildenbrand 	}
274580cd8763SFan Zhang 	/*
274680cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
274780cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
274880cd8763SFan Zhang 	 */
274980cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
275080cd8763SFan Zhang 	    test_kvm_facility(vcpu->kvm, 64)) {
275180cd8763SFan Zhang 		struct runtime_instr_cb *riccb =
275280cd8763SFan Zhang 			(struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
275380cd8763SFan Zhang 
275480cd8763SFan Zhang 		if (riccb->valid)
275580cd8763SFan Zhang 			vcpu->arch.sie_block->ecb3 |= 0x01;
275680cd8763SFan Zhang 	}
275731d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->arch.host_acrs);
275831d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->run->s.regs.acrs);
2759e1788bb9SChristian Borntraeger 	/* save host (userspace) fprs/vrs */
2760e1788bb9SChristian Borntraeger 	save_fpu_regs();
2761e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
2762e1788bb9SChristian Borntraeger 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
2763e1788bb9SChristian Borntraeger 	if (MACHINE_HAS_VX)
2764e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
2765e1788bb9SChristian Borntraeger 	else
2766e1788bb9SChristian Borntraeger 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
2767e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
2768e1788bb9SChristian Borntraeger 	if (test_fp_ctl(current->thread.fpu.fpc))
2769e1788bb9SChristian Borntraeger 		/* User space provided an invalid FPC, let's clear it */
2770e1788bb9SChristian Borntraeger 		current->thread.fpu.fpc = 0;
277180cd8763SFan Zhang 
2772b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
2773b028ee3eSDavid Hildenbrand }
2774b028ee3eSDavid Hildenbrand 
2775b028ee3eSDavid Hildenbrand static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2776b028ee3eSDavid Hildenbrand {
2777b028ee3eSDavid Hildenbrand 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
2778b028ee3eSDavid Hildenbrand 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
2779b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
2780b028ee3eSDavid Hildenbrand 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
27814287f247SDavid Hildenbrand 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
2782b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
2783b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
2784b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
2785b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
2786b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
2787b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
2788b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
278931d8b8d4SChristian Borntraeger 	save_access_regs(vcpu->run->s.regs.acrs);
279031d8b8d4SChristian Borntraeger 	restore_access_regs(vcpu->arch.host_acrs);
2791e1788bb9SChristian Borntraeger 	/* Save guest register state */
2792e1788bb9SChristian Borntraeger 	save_fpu_regs();
2793e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
2794e1788bb9SChristian Borntraeger 	/* Restore will be done lazily at return */
2795e1788bb9SChristian Borntraeger 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
2796e1788bb9SChristian Borntraeger 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
2797e1788bb9SChristian Borntraeger 
2798b028ee3eSDavid Hildenbrand }
2799b028ee3eSDavid Hildenbrand 
2800b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2801b0c632dbSHeiko Carstens {
28028f2abe6aSChristian Borntraeger 	int rc;
2803b0c632dbSHeiko Carstens 	sigset_t sigsaved;
2804b0c632dbSHeiko Carstens 
2805460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
2806460df4c1SPaolo Bonzini 		return -EINTR;
2807460df4c1SPaolo Bonzini 
280827291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
280927291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
281027291e21SDavid Hildenbrand 		return 0;
281127291e21SDavid Hildenbrand 	}
281227291e21SDavid Hildenbrand 
2813b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2814b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
2815b0c632dbSHeiko Carstens 
28166352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
28176852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
28186352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
2819ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
28206352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
28216352e4d2SDavid Hildenbrand 		return -EINVAL;
28226352e4d2SDavid Hildenbrand 	}
2823b0c632dbSHeiko Carstens 
2824b028ee3eSDavid Hildenbrand 	sync_regs(vcpu, kvm_run);
2825db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
2826d7b0b5ebSCarsten Otte 
2827dab4079dSHeiko Carstens 	might_fault();
2828e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
28299ace903dSChristian Ehrhardt 
2830b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
2831b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
28328f2abe6aSChristian Borntraeger 		rc = -EINTR;
2833b1d16c49SChristian Ehrhardt 	}
28348f2abe6aSChristian Borntraeger 
283527291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
283627291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
283727291e21SDavid Hildenbrand 		rc = 0;
283827291e21SDavid Hildenbrand 	}
283927291e21SDavid Hildenbrand 
28408f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
284171f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
28428f2abe6aSChristian Borntraeger 		rc = 0;
28438f2abe6aSChristian Borntraeger 	}
28448f2abe6aSChristian Borntraeger 
2845db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
2846b028ee3eSDavid Hildenbrand 	store_regs(vcpu, kvm_run);
2847d7b0b5ebSCarsten Otte 
2848b0c632dbSHeiko Carstens 	if (vcpu->sigset_active)
2849b0c632dbSHeiko Carstens 		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
2850b0c632dbSHeiko Carstens 
2851b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
28527e8e6ab4SHeiko Carstens 	return rc;
2853b0c632dbSHeiko Carstens }
2854b0c632dbSHeiko Carstens 
2855b0c632dbSHeiko Carstens /*
2856b0c632dbSHeiko Carstens  * store status at address
2857b0c632dbSHeiko Carstens  * we use have two special cases:
2858b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2859b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2860b0c632dbSHeiko Carstens  */
2861d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
2862b0c632dbSHeiko Carstens {
2863092670cdSCarsten Otte 	unsigned char archmode = 1;
28649abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
2865fda902cbSMichael Mueller 	unsigned int px;
28664287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
2867d0bce605SHeiko Carstens 	int rc;
2868b0c632dbSHeiko Carstens 
2869d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
2870d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
2871d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
2872b0c632dbSHeiko Carstens 			return -EFAULT;
2873d9a3a09aSMartin Schwidefsky 		gpa = 0;
2874d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
2875d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
2876b0c632dbSHeiko Carstens 			return -EFAULT;
2877d9a3a09aSMartin Schwidefsky 		gpa = px;
2878d9a3a09aSMartin Schwidefsky 	} else
2879d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
28809abc2a08SDavid Hildenbrand 
28819abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
28829abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
28839522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
2884d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
28859abc2a08SDavid Hildenbrand 				     fprs, 128);
28869abc2a08SDavid Hildenbrand 	} else {
28879abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
28886fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
28899abc2a08SDavid Hildenbrand 	}
2890d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
2891d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
2892d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
2893d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
2894d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
2895fda902cbSMichael Mueller 			      &px, 4);
2896d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
28979abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
2898d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
2899d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
29004287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
2901d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
29024287f247SDavid Hildenbrand 			      &cputm, 8);
2903178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
2904d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
2905d0bce605SHeiko Carstens 			      &clkcomp, 8);
2906d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
2907d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
2908d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
2909d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
2910d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
2911b0c632dbSHeiko Carstens }
2912b0c632dbSHeiko Carstens 
2913e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
2914e879892cSThomas Huth {
2915e879892cSThomas Huth 	/*
2916e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
291731d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
2918e879892cSThomas Huth 	 * it into the save area
2919e879892cSThomas Huth 	 */
2920d0164ee2SHendrik Brueckner 	save_fpu_regs();
29219abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
2922e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
2923e879892cSThomas Huth 
2924e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
2925e879892cSThomas Huth }
2926e879892cSThomas Huth 
29278ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
29288ad35755SDavid Hildenbrand {
29298ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
29308e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
29318ad35755SDavid Hildenbrand }
29328ad35755SDavid Hildenbrand 
29338ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
29348ad35755SDavid Hildenbrand {
29358ad35755SDavid Hildenbrand 	unsigned int i;
29368ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
29378ad35755SDavid Hildenbrand 
29388ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
29398ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
29408ad35755SDavid Hildenbrand 	}
29418ad35755SDavid Hildenbrand }
29428ad35755SDavid Hildenbrand 
29438ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
29448ad35755SDavid Hildenbrand {
294509a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
294609a400e7SDavid Hildenbrand 		return;
29478ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
29488e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
29498ad35755SDavid Hildenbrand }
29508ad35755SDavid Hildenbrand 
29516852d7b6SDavid Hildenbrand void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
29526852d7b6SDavid Hildenbrand {
29538ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
29548ad35755SDavid Hildenbrand 
29558ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
29568ad35755SDavid Hildenbrand 		return;
29578ad35755SDavid Hildenbrand 
29586852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
29598ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
2960433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
29618ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
29628ad35755SDavid Hildenbrand 
29638ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
29648ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
29658ad35755SDavid Hildenbrand 			started_vcpus++;
29668ad35755SDavid Hildenbrand 	}
29678ad35755SDavid Hildenbrand 
29688ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
29698ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
29708ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
29718ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
29728ad35755SDavid Hildenbrand 		/*
29738ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
29748ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
29758ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
29768ad35755SDavid Hildenbrand 		 */
29778ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
29788ad35755SDavid Hildenbrand 	}
29798ad35755SDavid Hildenbrand 
2980805de8f4SPeter Zijlstra 	atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
29818ad35755SDavid Hildenbrand 	/*
29828ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
29838ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
29848ad35755SDavid Hildenbrand 	 */
2985d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2986433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
29878ad35755SDavid Hildenbrand 	return;
29886852d7b6SDavid Hildenbrand }
29896852d7b6SDavid Hildenbrand 
29906852d7b6SDavid Hildenbrand void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
29916852d7b6SDavid Hildenbrand {
29928ad35755SDavid Hildenbrand 	int i, online_vcpus, started_vcpus = 0;
29938ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
29948ad35755SDavid Hildenbrand 
29958ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
29968ad35755SDavid Hildenbrand 		return;
29978ad35755SDavid Hildenbrand 
29986852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
29998ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
3000433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
30018ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
30028ad35755SDavid Hildenbrand 
300332f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
30046cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
300532f5ff63SDavid Hildenbrand 
3006805de8f4SPeter Zijlstra 	atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
30078ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
30088ad35755SDavid Hildenbrand 
30098ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
30108ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
30118ad35755SDavid Hildenbrand 			started_vcpus++;
30128ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
30138ad35755SDavid Hildenbrand 		}
30148ad35755SDavid Hildenbrand 	}
30158ad35755SDavid Hildenbrand 
30168ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
30178ad35755SDavid Hildenbrand 		/*
30188ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
30198ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
30208ad35755SDavid Hildenbrand 		 */
30218ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
30228ad35755SDavid Hildenbrand 	}
30238ad35755SDavid Hildenbrand 
3024433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
30258ad35755SDavid Hildenbrand 	return;
30266852d7b6SDavid Hildenbrand }
30276852d7b6SDavid Hildenbrand 
3028d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
3029d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
3030d6712df9SCornelia Huck {
3031d6712df9SCornelia Huck 	int r;
3032d6712df9SCornelia Huck 
3033d6712df9SCornelia Huck 	if (cap->flags)
3034d6712df9SCornelia Huck 		return -EINVAL;
3035d6712df9SCornelia Huck 
3036d6712df9SCornelia Huck 	switch (cap->cap) {
3037fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
3038fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
3039fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
3040c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
3041fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
3042fa6b7fe9SCornelia Huck 		}
3043fa6b7fe9SCornelia Huck 		r = 0;
3044fa6b7fe9SCornelia Huck 		break;
3045d6712df9SCornelia Huck 	default:
3046d6712df9SCornelia Huck 		r = -EINVAL;
3047d6712df9SCornelia Huck 		break;
3048d6712df9SCornelia Huck 	}
3049d6712df9SCornelia Huck 	return r;
3050d6712df9SCornelia Huck }
3051d6712df9SCornelia Huck 
305241408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
305341408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
305441408c28SThomas Huth {
305541408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
305641408c28SThomas Huth 	void *tmpbuf = NULL;
305741408c28SThomas Huth 	int r, srcu_idx;
305841408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
305941408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
306041408c28SThomas Huth 
306141408c28SThomas Huth 	if (mop->flags & ~supported_flags)
306241408c28SThomas Huth 		return -EINVAL;
306341408c28SThomas Huth 
306441408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
306541408c28SThomas Huth 		return -E2BIG;
306641408c28SThomas Huth 
306741408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
306841408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
306941408c28SThomas Huth 		if (!tmpbuf)
307041408c28SThomas Huth 			return -ENOMEM;
307141408c28SThomas Huth 	}
307241408c28SThomas Huth 
307341408c28SThomas Huth 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
307441408c28SThomas Huth 
307541408c28SThomas Huth 	switch (mop->op) {
307641408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
307741408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
307892c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
307992c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
308041408c28SThomas Huth 			break;
308141408c28SThomas Huth 		}
308241408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
308341408c28SThomas Huth 		if (r == 0) {
308441408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
308541408c28SThomas Huth 				r = -EFAULT;
308641408c28SThomas Huth 		}
308741408c28SThomas Huth 		break;
308841408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
308941408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
309092c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
309192c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
309241408c28SThomas Huth 			break;
309341408c28SThomas Huth 		}
309441408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
309541408c28SThomas Huth 			r = -EFAULT;
309641408c28SThomas Huth 			break;
309741408c28SThomas Huth 		}
309841408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
309941408c28SThomas Huth 		break;
310041408c28SThomas Huth 	default:
310141408c28SThomas Huth 		r = -EINVAL;
310241408c28SThomas Huth 	}
310341408c28SThomas Huth 
310441408c28SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
310541408c28SThomas Huth 
310641408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
310741408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
310841408c28SThomas Huth 
310941408c28SThomas Huth 	vfree(tmpbuf);
311041408c28SThomas Huth 	return r;
311141408c28SThomas Huth }
311241408c28SThomas Huth 
3113b0c632dbSHeiko Carstens long kvm_arch_vcpu_ioctl(struct file *filp,
3114b0c632dbSHeiko Carstens 			 unsigned int ioctl, unsigned long arg)
3115b0c632dbSHeiko Carstens {
3116b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
3117b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
3118800c1065SThomas Huth 	int idx;
3119bc923cc9SAvi Kivity 	long r;
3120b0c632dbSHeiko Carstens 
312193736624SAvi Kivity 	switch (ioctl) {
312247b43c52SJens Freimann 	case KVM_S390_IRQ: {
312347b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
312447b43c52SJens Freimann 
312547b43c52SJens Freimann 		r = -EFAULT;
312647b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
312747b43c52SJens Freimann 			break;
312847b43c52SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
312947b43c52SJens Freimann 		break;
313047b43c52SJens Freimann 	}
313193736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
3132ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
3133383d0b05SJens Freimann 		struct kvm_s390_irq s390irq;
3134ba5c1e9bSCarsten Otte 
313593736624SAvi Kivity 		r = -EFAULT;
3136ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
313793736624SAvi Kivity 			break;
3138383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
3139383d0b05SJens Freimann 			return -EINVAL;
3140383d0b05SJens Freimann 		r = kvm_s390_inject_vcpu(vcpu, &s390irq);
314193736624SAvi Kivity 		break;
3142ba5c1e9bSCarsten Otte 	}
3143b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
3144800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
3145bc923cc9SAvi Kivity 		r = kvm_s390_vcpu_store_status(vcpu, arg);
3146800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
3147bc923cc9SAvi Kivity 		break;
3148b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
3149b0c632dbSHeiko Carstens 		psw_t psw;
3150b0c632dbSHeiko Carstens 
3151bc923cc9SAvi Kivity 		r = -EFAULT;
3152b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
3153bc923cc9SAvi Kivity 			break;
3154bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
3155bc923cc9SAvi Kivity 		break;
3156b0c632dbSHeiko Carstens 	}
3157b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
3158bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3159bc923cc9SAvi Kivity 		break;
316014eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
316114eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
316214eebd91SCarsten Otte 		struct kvm_one_reg reg;
316314eebd91SCarsten Otte 		r = -EFAULT;
316414eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
316514eebd91SCarsten Otte 			break;
316614eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
316714eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
316814eebd91SCarsten Otte 		else
316914eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
317014eebd91SCarsten Otte 		break;
317114eebd91SCarsten Otte 	}
317227e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
317327e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
317427e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
317527e0393fSCarsten Otte 
317627e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
317727e0393fSCarsten Otte 			r = -EFAULT;
317827e0393fSCarsten Otte 			break;
317927e0393fSCarsten Otte 		}
318027e0393fSCarsten Otte 
318127e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
318227e0393fSCarsten Otte 			r = -EINVAL;
318327e0393fSCarsten Otte 			break;
318427e0393fSCarsten Otte 		}
318527e0393fSCarsten Otte 
318627e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
318727e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
318827e0393fSCarsten Otte 		break;
318927e0393fSCarsten Otte 	}
319027e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
319127e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
319227e0393fSCarsten Otte 
319327e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
319427e0393fSCarsten Otte 			r = -EFAULT;
319527e0393fSCarsten Otte 			break;
319627e0393fSCarsten Otte 		}
319727e0393fSCarsten Otte 
319827e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
319927e0393fSCarsten Otte 			r = -EINVAL;
320027e0393fSCarsten Otte 			break;
320127e0393fSCarsten Otte 		}
320227e0393fSCarsten Otte 
320327e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
320427e0393fSCarsten Otte 			ucasmap.length);
320527e0393fSCarsten Otte 		break;
320627e0393fSCarsten Otte 	}
320727e0393fSCarsten Otte #endif
3208ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
3209527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
3210ccc7910fSCarsten Otte 		break;
3211ccc7910fSCarsten Otte 	}
3212d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
3213d6712df9SCornelia Huck 	{
3214d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
3215d6712df9SCornelia Huck 		r = -EFAULT;
3216d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
3217d6712df9SCornelia Huck 			break;
3218d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
3219d6712df9SCornelia Huck 		break;
3220d6712df9SCornelia Huck 	}
322141408c28SThomas Huth 	case KVM_S390_MEM_OP: {
322241408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
322341408c28SThomas Huth 
322441408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
322541408c28SThomas Huth 			r = kvm_s390_guest_mem_op(vcpu, &mem_op);
322641408c28SThomas Huth 		else
322741408c28SThomas Huth 			r = -EFAULT;
322841408c28SThomas Huth 		break;
322941408c28SThomas Huth 	}
3230816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
3231816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3232816c7667SJens Freimann 
3233816c7667SJens Freimann 		r = -EFAULT;
3234816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3235816c7667SJens Freimann 			break;
3236816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
3237816c7667SJens Freimann 		    irq_state.len == 0 ||
3238816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
3239816c7667SJens Freimann 			r = -EINVAL;
3240816c7667SJens Freimann 			break;
3241816c7667SJens Freimann 		}
3242816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
3243816c7667SJens Freimann 					   (void __user *) irq_state.buf,
3244816c7667SJens Freimann 					   irq_state.len);
3245816c7667SJens Freimann 		break;
3246816c7667SJens Freimann 	}
3247816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
3248816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
3249816c7667SJens Freimann 
3250816c7667SJens Freimann 		r = -EFAULT;
3251816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
3252816c7667SJens Freimann 			break;
3253816c7667SJens Freimann 		if (irq_state.len == 0) {
3254816c7667SJens Freimann 			r = -EINVAL;
3255816c7667SJens Freimann 			break;
3256816c7667SJens Freimann 		}
3257816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
3258816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
3259816c7667SJens Freimann 					   irq_state.len);
3260816c7667SJens Freimann 		break;
3261816c7667SJens Freimann 	}
3262b0c632dbSHeiko Carstens 	default:
32633e6afcf1SCarsten Otte 		r = -ENOTTY;
3264b0c632dbSHeiko Carstens 	}
3265bc923cc9SAvi Kivity 	return r;
3266b0c632dbSHeiko Carstens }
3267b0c632dbSHeiko Carstens 
32685b1c1493SCarsten Otte int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
32695b1c1493SCarsten Otte {
32705b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
32715b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
32725b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
32735b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
32745b1c1493SCarsten Otte 		get_page(vmf->page);
32755b1c1493SCarsten Otte 		return 0;
32765b1c1493SCarsten Otte 	}
32775b1c1493SCarsten Otte #endif
32785b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
32795b1c1493SCarsten Otte }
32805b1c1493SCarsten Otte 
32815587027cSAneesh Kumar K.V int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
32825587027cSAneesh Kumar K.V 			    unsigned long npages)
3283db3fe4ebSTakuya Yoshikawa {
3284db3fe4ebSTakuya Yoshikawa 	return 0;
3285db3fe4ebSTakuya Yoshikawa }
3286db3fe4ebSTakuya Yoshikawa 
3287b0c632dbSHeiko Carstens /* Section: memory related */
3288f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
3289f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
329009170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
32917b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
3292b0c632dbSHeiko Carstens {
3293dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
3294dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
3295dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
3296dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
3297b0c632dbSHeiko Carstens 
3298598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
3299b0c632dbSHeiko Carstens 		return -EINVAL;
3300b0c632dbSHeiko Carstens 
3301598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
3302b0c632dbSHeiko Carstens 		return -EINVAL;
3303b0c632dbSHeiko Carstens 
3304a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
3305a3a92c31SDominik Dingel 		return -EINVAL;
3306a3a92c31SDominik Dingel 
3307f7784b8eSMarcelo Tosatti 	return 0;
3308f7784b8eSMarcelo Tosatti }
3309f7784b8eSMarcelo Tosatti 
3310f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
331109170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
33128482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old,
3313f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
33148482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
3315f7784b8eSMarcelo Tosatti {
3316f7850c92SCarsten Otte 	int rc;
3317f7784b8eSMarcelo Tosatti 
33182cef4debSChristian Borntraeger 	/* If the basics of the memslot do not change, we do not want
33192cef4debSChristian Borntraeger 	 * to update the gmap. Every update causes several unnecessary
33202cef4debSChristian Borntraeger 	 * segment translation exceptions. This is usually handled just
33212cef4debSChristian Borntraeger 	 * fine by the normal fault handler + gmap, but it will also
33222cef4debSChristian Borntraeger 	 * cause faults on the prefix page of running guest CPUs.
33232cef4debSChristian Borntraeger 	 */
33242cef4debSChristian Borntraeger 	if (old->userspace_addr == mem->userspace_addr &&
33252cef4debSChristian Borntraeger 	    old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
33262cef4debSChristian Borntraeger 	    old->npages * PAGE_SIZE == mem->memory_size)
33272cef4debSChristian Borntraeger 		return;
3328598841caSCarsten Otte 
3329598841caSCarsten Otte 	rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
3330598841caSCarsten Otte 		mem->guest_phys_addr, mem->memory_size);
3331598841caSCarsten Otte 	if (rc)
3332ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
3333598841caSCarsten Otte 	return;
3334b0c632dbSHeiko Carstens }
3335b0c632dbSHeiko Carstens 
333660a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
333760a37709SAlexander Yarygin {
333860a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
333960a37709SAlexander Yarygin 
334060a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
334160a37709SAlexander Yarygin }
334260a37709SAlexander Yarygin 
33433491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
33443491caf2SChristian Borntraeger {
33453491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
33463491caf2SChristian Borntraeger }
33473491caf2SChristian Borntraeger 
3348b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
3349b0c632dbSHeiko Carstens {
335060a37709SAlexander Yarygin 	int i;
335160a37709SAlexander Yarygin 
335207197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
335307197fd0SDavid Hildenbrand 		pr_info("SIE not available\n");
335407197fd0SDavid Hildenbrand 		return -ENODEV;
335507197fd0SDavid Hildenbrand 	}
335607197fd0SDavid Hildenbrand 
335760a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
335860a37709SAlexander Yarygin 		kvm_s390_fac_list_mask[i] |=
335960a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
336060a37709SAlexander Yarygin 
33619d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
3362b0c632dbSHeiko Carstens }
3363b0c632dbSHeiko Carstens 
3364b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
3365b0c632dbSHeiko Carstens {
3366b0c632dbSHeiko Carstens 	kvm_exit();
3367b0c632dbSHeiko Carstens }
3368b0c632dbSHeiko Carstens 
3369b0c632dbSHeiko Carstens module_init(kvm_s390_init);
3370b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
3371566af940SCornelia Huck 
3372566af940SCornelia Huck /*
3373566af940SCornelia Huck  * Enable autoloading of the kvm module.
3374566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3375566af940SCornelia Huck  * since x86 takes a different approach.
3376566af940SCornelia Huck  */
3377566af940SCornelia Huck #include <linux/miscdevice.h>
3378566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
3379566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
3380