xref: /openbmc/linux/arch/s390/kvm/kvm-s390.c (revision 2cfd7b73f593ebf53e90a3464aa66c9ca996936e)
1d809aa23SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b0c632dbSHeiko Carstens /*
3bb64da9aSChristian Borntraeger  * hosting IBM Z kernel virtual machines (s390x)
4b0c632dbSHeiko Carstens  *
53e6c5568SJanosch Frank  * Copyright IBM Corp. 2008, 2020
6b0c632dbSHeiko Carstens  *
7b0c632dbSHeiko Carstens  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8b0c632dbSHeiko Carstens  *               Christian Borntraeger <borntraeger@de.ibm.com>
9b0c632dbSHeiko Carstens  *               Heiko Carstens <heiko.carstens@de.ibm.com>
10628eb9b8SChristian Ehrhardt  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
1115f36ebdSJason J. Herne  *               Jason J. Herne <jjherne@us.ibm.com>
12b0c632dbSHeiko Carstens  */
13b0c632dbSHeiko Carstens 
147aedd9d4SMichael Mueller #define KMSG_COMPONENT "kvm-s390"
157aedd9d4SMichael Mueller #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
167aedd9d4SMichael Mueller 
17b0c632dbSHeiko Carstens #include <linux/compiler.h>
18b0c632dbSHeiko Carstens #include <linux/err.h>
19b0c632dbSHeiko Carstens #include <linux/fs.h>
20ca872302SChristian Borntraeger #include <linux/hrtimer.h>
21b0c632dbSHeiko Carstens #include <linux/init.h>
22b0c632dbSHeiko Carstens #include <linux/kvm.h>
23b0c632dbSHeiko Carstens #include <linux/kvm_host.h>
24b2d73b2aSMartin Schwidefsky #include <linux/mman.h>
25b0c632dbSHeiko Carstens #include <linux/module.h>
26d3217967SPaul Gortmaker #include <linux/moduleparam.h>
27a374e892STony Krowiak #include <linux/random.h>
28b0c632dbSHeiko Carstens #include <linux/slab.h>
29ba5c1e9bSCarsten Otte #include <linux/timer.h>
3041408c28SThomas Huth #include <linux/vmalloc.h>
3115c9705fSDavid Hildenbrand #include <linux/bitmap.h>
32174cd4b1SIngo Molnar #include <linux/sched/signal.h>
33190df4a2SClaudio Imbrenda #include <linux/string.h>
3465fddcfcSMike Rapoport #include <linux/pgtable.h>
35174cd4b1SIngo Molnar 
36cbb870c8SHeiko Carstens #include <asm/asm-offsets.h>
37b0c632dbSHeiko Carstens #include <asm/lowcore.h>
38fd5ada04SMartin Schwidefsky #include <asm/stp.h>
391e133ab2SMartin Schwidefsky #include <asm/gmap.h>
40f5daba1dSHeiko Carstens #include <asm/nmi.h>
41a0616cdeSDavid Howells #include <asm/switch_to.h>
426d3da241SJens Freimann #include <asm/isc.h>
431526bf9cSChristian Borntraeger #include <asm/sclp.h>
440a763c78SDavid Hildenbrand #include <asm/cpacf.h>
45221bb8a4SLinus Torvalds #include <asm/timex.h>
46e585b24aSTony Krowiak #include <asm/ap.h>
4729b40f10SJanosch Frank #include <asm/uv.h>
4856e62a73SSven Schnelle #include <asm/fpu/api.h>
498f2abe6aSChristian Borntraeger #include "kvm-s390.h"
50b0c632dbSHeiko Carstens #include "gaccess.h"
51b0c632dbSHeiko Carstens 
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 struct kvm_stats_debugfs_item debugfs_entries[] = {
62812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("userspace_handled", exit_userspace),
63812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_null", exit_null),
6450a05be4SChristian Borntraeger 	VCPU_STAT("pfault_sync", pfault_sync),
65812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_validity", exit_validity),
66812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_stop_request", exit_stop_request),
67812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_external_request", exit_external_request),
68812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_io_request", exit_io_request),
69812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_external_interrupt", exit_external_interrupt),
70812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_instruction", exit_instruction),
71812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_pei", exit_pei),
72812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_program_interruption", exit_program_interruption),
73812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_instr_and_program_int", exit_instr_and_program),
74812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_operation_exception", exit_operation_exception),
75812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("halt_successful_poll", halt_successful_poll),
76812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
77812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
78812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("halt_no_poll_steal", halt_no_poll_steal),
79812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("halt_wakeup", halt_wakeup),
80cb953129SDavid Matlack 	VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
81cb953129SDavid Matlack 	VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
82812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_lctlg", instruction_lctlg),
83812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_lctl", instruction_lctl),
84812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_stctl", instruction_stctl),
85812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_stctg", instruction_stctg),
86812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_ckc", deliver_ckc),
87812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_cputm", deliver_cputm),
88812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_emergency_signal", deliver_emergency_signal),
89812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_external_call", deliver_external_call),
90812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_service_signal", deliver_service_signal),
91812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_virtio", deliver_virtio),
92812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_stop_signal", deliver_stop_signal),
93812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_prefix_signal", deliver_prefix_signal),
94812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_restart_signal", deliver_restart_signal),
95812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_program", deliver_program),
96812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_io", deliver_io),
97812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("deliver_machine_check", deliver_machine_check),
98812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("exit_wait_state", exit_wait_state),
99812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_ckc", inject_ckc),
100812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_cputm", inject_cputm),
101812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_external_call", inject_external_call),
102812756a8SEmanuele Giuseppe Esposito 	VM_STAT("inject_float_mchk", inject_float_mchk),
103812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_emergency_signal", inject_emergency_signal),
104812756a8SEmanuele Giuseppe Esposito 	VM_STAT("inject_io", inject_io),
105812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_mchk", inject_mchk),
106812756a8SEmanuele Giuseppe Esposito 	VM_STAT("inject_pfault_done", inject_pfault_done),
107812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_program", inject_program),
108812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_restart", inject_restart),
109812756a8SEmanuele Giuseppe Esposito 	VM_STAT("inject_service_signal", inject_service_signal),
110812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_set_prefix", inject_set_prefix),
111812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_stop_signal", inject_stop_signal),
112812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("inject_pfault_init", inject_pfault_init),
113812756a8SEmanuele Giuseppe Esposito 	VM_STAT("inject_virtio", inject_virtio),
114812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_epsw", instruction_epsw),
115812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_gs", instruction_gs),
116812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_io_other", instruction_io_other),
117812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_lpsw", instruction_lpsw),
118812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_lpswe", instruction_lpswe),
119812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_pfmf", instruction_pfmf),
120812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_ptff", instruction_ptff),
121812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_stidp", instruction_stidp),
122812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sck", instruction_sck),
123812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sckpf", instruction_sckpf),
124812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_spx", instruction_spx),
125812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_stpx", instruction_stpx),
126812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_stap", instruction_stap),
127812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_iske", instruction_iske),
128812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_ri", instruction_ri),
129812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_rrbe", instruction_rrbe),
130812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sske", instruction_sske),
131812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_ipte_interlock", instruction_ipte_interlock),
132812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_essa", instruction_essa),
133812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_stsi", instruction_stsi),
134812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_stfl", instruction_stfl),
135812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_tb", instruction_tb),
136812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_tpi", instruction_tpi),
137812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_tprot", instruction_tprot),
138812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_tsch", instruction_tsch),
139812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sthyi", instruction_sthyi),
140812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sie", instruction_sie),
141812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_sense", instruction_sigp_sense),
142812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_sense_running", instruction_sigp_sense_running),
143812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_external_call", instruction_sigp_external_call),
144812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_emergency", instruction_sigp_emergency),
145812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_cond_emergency", instruction_sigp_cond_emergency),
146812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_start", instruction_sigp_start),
147812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_stop", instruction_sigp_stop),
148812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_stop_store_status", instruction_sigp_stop_store_status),
149812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_store_status", instruction_sigp_store_status),
150812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_store_adtl_status", instruction_sigp_store_adtl_status),
151812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_set_arch", instruction_sigp_arch),
152812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_set_prefix", instruction_sigp_prefix),
153812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_restart", instruction_sigp_restart),
154812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_cpu_reset", instruction_sigp_cpu_reset),
155812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_init_cpu_reset", instruction_sigp_init_cpu_reset),
156812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_sigp_unknown", instruction_sigp_unknown),
157812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_diag_10", diagnose_10),
158812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_diag_44", diagnose_44),
159812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_diag_9c", diagnose_9c),
160812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("diag_9c_ignored", diagnose_9c_ignored),
161812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_diag_258", diagnose_258),
162812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_diag_308", diagnose_308),
163812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_diag_500", diagnose_500),
164812756a8SEmanuele Giuseppe Esposito 	VCPU_STAT("instruction_diag_other", diagnose_other),
165b0c632dbSHeiko Carstens 	{ NULL }
166b0c632dbSHeiko Carstens };
167b0c632dbSHeiko Carstens 
168a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
169a411edf1SDavid Hildenbrand static int nested;
170a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
171a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
172a411edf1SDavid Hildenbrand 
173a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */
174a4499382SJanosch Frank static int hpage;
175a4499382SJanosch Frank module_param(hpage, int, 0444);
176a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support");
177b0c632dbSHeiko Carstens 
1788b905d28SChristian Borntraeger /* maximum percentage of steal time for polling.  >100 is treated like 100 */
1798b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10;
1808b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644);
181b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
1828b905d28SChristian Borntraeger 
183cc674ef2SMichael Mueller /* if set to true, the GISA will be initialized and used if available */
184cc674ef2SMichael Mueller static bool use_gisa  = true;
185cc674ef2SMichael Mueller module_param(use_gisa, bool, 0644);
186cc674ef2SMichael Mueller MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
187cc674ef2SMichael Mueller 
188c3b9e3e1SChristian Borntraeger /*
189c3b9e3e1SChristian Borntraeger  * For now we handle at most 16 double words as this is what the s390 base
190c3b9e3e1SChristian Borntraeger  * kernel handles and stores in the prefix page. If we ever need to go beyond
191c3b9e3e1SChristian Borntraeger  * this, this requires changes to code, but the external uapi can stay.
192c3b9e3e1SChristian Borntraeger  */
193c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16
194c3b9e3e1SChristian Borntraeger 
195c3b9e3e1SChristian Borntraeger /*
196c3b9e3e1SChristian Borntraeger  * Base feature mask that defines default mask for facilities. Consists of the
197c3b9e3e1SChristian Borntraeger  * defines in FACILITIES_KVM and the non-hypervisor managed bits.
198c3b9e3e1SChristian Borntraeger  */
199c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
200c3b9e3e1SChristian Borntraeger /*
201c3b9e3e1SChristian Borntraeger  * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
202c3b9e3e1SChristian Borntraeger  * and defines the facilities that can be enabled via a cpu model.
203c3b9e3e1SChristian Borntraeger  */
204c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
205c3b9e3e1SChristian Borntraeger 
206c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void)
20778c4b59fSMichael Mueller {
208c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
209c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
210c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
211c3b9e3e1SChristian Borntraeger 		sizeof(S390_lowcore.stfle_fac_list));
212c3b9e3e1SChristian Borntraeger 
213c3b9e3e1SChristian Borntraeger 	return SIZE_INTERNAL;
21478c4b59fSMichael Mueller }
21578c4b59fSMichael Mueller 
21615c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
21715c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
2180a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
2190a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
22015c9705fSDavid Hildenbrand 
2219d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
222a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
22378f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
2243e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv;
2259d8d5786SMichael Mueller 
226b0c632dbSHeiko Carstens /* Section: not file related */
22713a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
228b0c632dbSHeiko Carstens {
229b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
23010474ae8SAlexander Graf 	return 0;
231b0c632dbSHeiko Carstens }
232b0c632dbSHeiko Carstens 
233b9904085SSean Christopherson int kvm_arch_check_processor_compat(void *opaque)
234f257d6dcSSean Christopherson {
235f257d6dcSSean Christopherson 	return 0;
236f257d6dcSSean Christopherson }
237f257d6dcSSean Christopherson 
23829b40f10SJanosch Frank /* forward declarations */
239414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
240414d3b07SMartin Schwidefsky 			      unsigned long end);
24129b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm);
2422c70fe44SChristian Borntraeger 
2431575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
2441575767eSDavid Hildenbrand {
2451575767eSDavid Hildenbrand 	u8 delta_idx = 0;
2461575767eSDavid Hildenbrand 
2471575767eSDavid Hildenbrand 	/*
2481575767eSDavid Hildenbrand 	 * The TOD jumps by delta, we have to compensate this by adding
2491575767eSDavid Hildenbrand 	 * -delta to the epoch.
2501575767eSDavid Hildenbrand 	 */
2511575767eSDavid Hildenbrand 	delta = -delta;
2521575767eSDavid Hildenbrand 
2531575767eSDavid Hildenbrand 	/* sign-extension - we're adding to signed values below */
2541575767eSDavid Hildenbrand 	if ((s64)delta < 0)
2551575767eSDavid Hildenbrand 		delta_idx = -1;
2561575767eSDavid Hildenbrand 
2571575767eSDavid Hildenbrand 	scb->epoch += delta;
2581575767eSDavid Hildenbrand 	if (scb->ecd & ECD_MEF) {
2591575767eSDavid Hildenbrand 		scb->epdx += delta_idx;
2601575767eSDavid Hildenbrand 		if (scb->epoch < delta)
2611575767eSDavid Hildenbrand 			scb->epdx += 1;
2621575767eSDavid Hildenbrand 	}
2631575767eSDavid Hildenbrand }
2641575767eSDavid Hildenbrand 
265fdf03650SFan Zhang /*
266fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
267fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
268fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
269fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
270fdf03650SFan Zhang  */
271fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
272fdf03650SFan Zhang 			  void *v)
273fdf03650SFan Zhang {
274fdf03650SFan Zhang 	struct kvm *kvm;
275fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
276fdf03650SFan Zhang 	int i;
277fdf03650SFan Zhang 	unsigned long long *delta = v;
278fdf03650SFan Zhang 
279fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
280fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
2811575767eSDavid Hildenbrand 			kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
2821575767eSDavid Hildenbrand 			if (i == 0) {
2831575767eSDavid Hildenbrand 				kvm->arch.epoch = vcpu->arch.sie_block->epoch;
2841575767eSDavid Hildenbrand 				kvm->arch.epdx = vcpu->arch.sie_block->epdx;
2851575767eSDavid Hildenbrand 			}
286db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
287db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
28891473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
2891575767eSDavid Hildenbrand 				kvm_clock_sync_scb(vcpu->arch.vsie_block,
2901575767eSDavid Hildenbrand 						   *delta);
291fdf03650SFan Zhang 		}
292fdf03650SFan Zhang 	}
293fdf03650SFan Zhang 	return NOTIFY_OK;
294fdf03650SFan Zhang }
295fdf03650SFan Zhang 
296fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
297fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
298fdf03650SFan Zhang };
299fdf03650SFan Zhang 
300b9904085SSean Christopherson int kvm_arch_hardware_setup(void *opaque)
301b0c632dbSHeiko Carstens {
3022c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
303b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
304a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
305a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
306fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
307fdf03650SFan Zhang 				       &kvm_clock_notifier);
308b0c632dbSHeiko Carstens 	return 0;
309b0c632dbSHeiko Carstens }
310b0c632dbSHeiko Carstens 
311b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
312b0c632dbSHeiko Carstens {
313b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
314a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
315fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
316fdf03650SFan Zhang 					 &kvm_clock_notifier);
317b0c632dbSHeiko Carstens }
318b0c632dbSHeiko Carstens 
31922be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
32022be5a13SDavid Hildenbrand {
32122be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
32222be5a13SDavid Hildenbrand }
32322be5a13SDavid Hildenbrand 
3240a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
3250a763c78SDavid Hildenbrand {
3260a763c78SDavid Hildenbrand 	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
327d051ae53SHeiko Carstens 	int cc;
3280a763c78SDavid Hildenbrand 
3290a763c78SDavid Hildenbrand 	asm volatile(
3300a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
3310a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
3320a763c78SDavid Hildenbrand 		"	ipm	%0\n"
3330a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
3340a763c78SDavid Hildenbrand 		: "=d" (cc)
3350a763c78SDavid Hildenbrand 		: "d" (r0)
3360a763c78SDavid Hildenbrand 		: "cc");
3370a763c78SDavid Hildenbrand 	return cc == 0;
3380a763c78SDavid Hildenbrand }
3390a763c78SDavid Hildenbrand 
340d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
341d6681397SChristian Borntraeger {
342d6681397SChristian Borntraeger 	register unsigned long r0 asm("0") = 0;	/* query function */
343d6681397SChristian Borntraeger 	register unsigned long r1 asm("1") = (unsigned long) query;
344d6681397SChristian Borntraeger 
345d6681397SChristian Borntraeger 	asm volatile(
346d6681397SChristian Borntraeger 		/* Parameter regs are ignored */
347d6681397SChristian Borntraeger 		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
348b1c41ac3SHeiko Carstens 		:
349d6681397SChristian Borntraeger 		: "d" (r0), "a" (r1), [opc] "i" (opcode)
350b1c41ac3SHeiko Carstens 		: "cc", "memory");
351d6681397SChristian Borntraeger }
352d6681397SChristian Borntraeger 
353173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938
3544f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939
355173aec2dSChristian Borntraeger 
35622be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
35722be5a13SDavid Hildenbrand {
3580a763c78SDavid Hildenbrand 	int i;
3590a763c78SDavid Hildenbrand 
3600a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
3610a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
3620a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
3630a763c78SDavid Hildenbrand 	}
3640a763c78SDavid Hildenbrand 
3650a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
366221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
367221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
368221bb8a4SLinus Torvalds 		     PTFF_QAF);
3690a763c78SDavid Hildenbrand 
3700a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
37169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
37269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
37369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
37469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
37569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
37669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
37769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
37869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
37969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
38069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
3810a763c78SDavid Hildenbrand 	}
3820a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
38369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
38469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
3850a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
38669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
38769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
38869c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
38969c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
39069c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
39169c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
39269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
39369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
3940a763c78SDavid Hildenbrand 	}
3950a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
396985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
39769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
3980a763c78SDavid Hildenbrand 
399e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
400e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
401e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
402e000b8e0SJason J. Herne 
40313209ad0SChristian Borntraeger 	if (test_facility(155)) /* MSA9 */
40413209ad0SChristian Borntraeger 		__cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
40513209ad0SChristian Borntraeger 			      kvm_s390_available_subfunc.kdsa);
40613209ad0SChristian Borntraeger 
407173aec2dSChristian Borntraeger 	if (test_facility(150)) /* SORTL */
408173aec2dSChristian Borntraeger 		__insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
409173aec2dSChristian Borntraeger 
4104f45b90eSChristian Borntraeger 	if (test_facility(151)) /* DFLTCC */
4114f45b90eSChristian Borntraeger 		__insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
4124f45b90eSChristian Borntraeger 
41322be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
41422be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
415a3508fbeSDavid Hildenbrand 	/*
416a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
417a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
418a3508fbeSDavid Hildenbrand 	 */
419a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
420a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
421a3508fbeSDavid Hildenbrand 		return;
422a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
42319c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
42419c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
4250615a326SDavid Hildenbrand 	if (sclp.has_siif)
4260615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
42777d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
42877d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
429a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
430a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
4315630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
4325630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
43313ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
43413ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
4357fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
4367fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
437730cd632SFarhan Ali 	if (sclp.has_kss)
438730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
4395d3876a8SDavid Hildenbrand 	/*
4405d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
4415d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
4425d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
4435d3876a8SDavid Hildenbrand 	 *
4445d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
4455d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
4465d3876a8SDavid Hildenbrand 	 *
4475d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
4485d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
4495d3876a8SDavid Hildenbrand 	 *
4505d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
4515d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
4525d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
4535d3876a8SDavid Hildenbrand 	 *
4545d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
4555d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
4565d3876a8SDavid Hildenbrand 	 */
45722be5a13SDavid Hildenbrand }
45822be5a13SDavid Hildenbrand 
459b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
460b0c632dbSHeiko Carstens {
461f76f6371SJanosch Frank 	int rc = -ENOMEM;
462308c3e66SMichael Mueller 
46378f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
46478f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
46578f26131SChristian Borntraeger 		return -ENOMEM;
46678f26131SChristian Borntraeger 
4673e6c5568SJanosch Frank 	kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
4683e6c5568SJanosch Frank 	if (!kvm_s390_dbf_uv)
4693e6c5568SJanosch Frank 		goto out;
4703e6c5568SJanosch Frank 
4713e6c5568SJanosch Frank 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
4723e6c5568SJanosch Frank 	    debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
473f76f6371SJanosch Frank 		goto out;
47478f26131SChristian Borntraeger 
47522be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
47622be5a13SDavid Hildenbrand 
47784877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
478308c3e66SMichael Mueller 	rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
479308c3e66SMichael Mueller 	if (rc) {
4808d43d570SMichael Mueller 		pr_err("A FLIC registration call failed with rc=%d\n", rc);
481f76f6371SJanosch Frank 		goto out;
482308c3e66SMichael Mueller 	}
483b1d1e76eSMichael Mueller 
484b1d1e76eSMichael Mueller 	rc = kvm_s390_gib_init(GAL_ISC);
485b1d1e76eSMichael Mueller 	if (rc)
486f76f6371SJanosch Frank 		goto out;
487b1d1e76eSMichael Mueller 
488308c3e66SMichael Mueller 	return 0;
489308c3e66SMichael Mueller 
490f76f6371SJanosch Frank out:
491f76f6371SJanosch Frank 	kvm_arch_exit();
492308c3e66SMichael Mueller 	return rc;
493b0c632dbSHeiko Carstens }
494b0c632dbSHeiko Carstens 
49578f26131SChristian Borntraeger void kvm_arch_exit(void)
49678f26131SChristian Borntraeger {
4971282c21eSMichael Mueller 	kvm_s390_gib_destroy();
49878f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
4993e6c5568SJanosch Frank 	debug_unregister(kvm_s390_dbf_uv);
50078f26131SChristian Borntraeger }
50178f26131SChristian Borntraeger 
502b0c632dbSHeiko Carstens /* Section: device related */
503b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
504b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
505b0c632dbSHeiko Carstens {
506b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
507b0c632dbSHeiko Carstens 		return s390_enable_sie();
508b0c632dbSHeiko Carstens 	return -EINVAL;
509b0c632dbSHeiko Carstens }
510b0c632dbSHeiko Carstens 
511784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
512b0c632dbSHeiko Carstens {
513d7b0b5ebSCarsten Otte 	int r;
514d7b0b5ebSCarsten Otte 
5152bd0ac4eSCarsten Otte 	switch (ext) {
516d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
517b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
51852e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
5191efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
5201efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
5211efd0f59SCarsten Otte #endif
5223c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
52360b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
52414eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
525d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
526fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
52710ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
528c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
52978599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
530f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
5316352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
532460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
53347b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
5342444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
535e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
53630ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
537816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
5386502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5394036e387SClaudio Imbrenda 	case KVM_CAP_S390_CMMA_MIGRATION:
54047a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
541da9a1446SChristian Borntraeger 	case KVM_CAP_S390_AIS_MIGRATION:
5427de3f142SJanosch Frank 	case KVM_CAP_S390_VCPU_RESETS:
543b9b2782cSPeter Xu 	case KVM_CAP_SET_GUEST_DEBUG:
54423a60f83SCollin Walling 	case KVM_CAP_S390_DIAG318:
545d7b0b5ebSCarsten Otte 		r = 1;
546d7b0b5ebSCarsten Otte 		break;
547a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
548a4499382SJanosch Frank 		r = 0;
54940ebdb8eSJanosch Frank 		if (hpage && !kvm_is_ucontrol(kvm))
550a4499382SJanosch Frank 			r = 1;
551a4499382SJanosch Frank 		break;
55241408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
55341408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
55441408c28SThomas Huth 		break;
555e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
556e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
557a86cb413SThomas Huth 	case KVM_CAP_MAX_VCPU_ID:
55876a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
559a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
560a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
561a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
56276a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
563e726b1bdSChristian Borntraeger 		break;
5641526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
565abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
5661526bf9cSChristian Borntraeger 		break;
56768c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
56868c55750SEric Farman 		r = MACHINE_HAS_VX;
56968c55750SEric Farman 		break;
570c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
571c6e5f166SFan Zhang 		r = test_facility(64);
572c6e5f166SFan Zhang 		break;
5734e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
5744e0b1ab7SFan Zhang 		r = test_facility(133);
5754e0b1ab7SFan Zhang 		break;
57635b3fde6SChristian Borntraeger 	case KVM_CAP_S390_BPB:
57735b3fde6SChristian Borntraeger 		r = test_facility(82);
57835b3fde6SChristian Borntraeger 		break;
57913da9ae1SChristian Borntraeger 	case KVM_CAP_S390_PROTECTED:
58013da9ae1SChristian Borntraeger 		r = is_prot_virt_host();
58113da9ae1SChristian Borntraeger 		break;
5822bd0ac4eSCarsten Otte 	default:
583d7b0b5ebSCarsten Otte 		r = 0;
584b0c632dbSHeiko Carstens 	}
585d7b0b5ebSCarsten Otte 	return r;
5862bd0ac4eSCarsten Otte }
587b0c632dbSHeiko Carstens 
5880dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
58915f36ebdSJason J. Herne {
5900959e168SJanosch Frank 	int i;
59115f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
5920959e168SJanosch Frank 	unsigned long gaddr, vmaddr;
59315f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
5940959e168SJanosch Frank 	DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
59515f36ebdSJason J. Herne 
5960959e168SJanosch Frank 	/* Loop over all guest segments */
5970959e168SJanosch Frank 	cur_gfn = memslot->base_gfn;
59815f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
5990959e168SJanosch Frank 	for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
6000959e168SJanosch Frank 		gaddr = gfn_to_gpa(cur_gfn);
6010959e168SJanosch Frank 		vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
6020959e168SJanosch Frank 		if (kvm_is_error_hva(vmaddr))
6030959e168SJanosch Frank 			continue;
60415f36ebdSJason J. Herne 
6050959e168SJanosch Frank 		bitmap_zero(bitmap, _PAGE_ENTRIES);
6060959e168SJanosch Frank 		gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
6070959e168SJanosch Frank 		for (i = 0; i < _PAGE_ENTRIES; i++) {
6080959e168SJanosch Frank 			if (test_bit(i, bitmap))
6090959e168SJanosch Frank 				mark_page_dirty(kvm, cur_gfn + i);
6100959e168SJanosch Frank 		}
6110959e168SJanosch Frank 
6121763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
6131763f8d0SChristian Borntraeger 			return;
61470c88a00SChristian Borntraeger 		cond_resched();
61515f36ebdSJason J. Herne 	}
61615f36ebdSJason J. Herne }
61715f36ebdSJason J. Herne 
618b0c632dbSHeiko Carstens /* Section: vm related */
619a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
620a6e2f683SEugene (jno) Dvurechenski 
621b0c632dbSHeiko Carstens /*
622b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
623b0c632dbSHeiko Carstens  */
624b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
625b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
626b0c632dbSHeiko Carstens {
62715f36ebdSJason J. Herne 	int r;
62815f36ebdSJason J. Herne 	unsigned long n;
62915f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
6302a49f61dSSean Christopherson 	int is_dirty;
63115f36ebdSJason J. Herne 
632e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
633e1e8a962SJanosch Frank 		return -EINVAL;
634e1e8a962SJanosch Frank 
63515f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
63615f36ebdSJason J. Herne 
63715f36ebdSJason J. Herne 	r = -EINVAL;
63815f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
63915f36ebdSJason J. Herne 		goto out;
64015f36ebdSJason J. Herne 
6412a49f61dSSean Christopherson 	r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
64215f36ebdSJason J. Herne 	if (r)
64315f36ebdSJason J. Herne 		goto out;
64415f36ebdSJason J. Herne 
64515f36ebdSJason J. Herne 	/* Clear the dirty log */
64615f36ebdSJason J. Herne 	if (is_dirty) {
64715f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
64815f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
64915f36ebdSJason J. Herne 	}
65015f36ebdSJason J. Herne 	r = 0;
65115f36ebdSJason J. Herne out:
65215f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
65315f36ebdSJason J. Herne 	return r;
654b0c632dbSHeiko Carstens }
655b0c632dbSHeiko Carstens 
6566502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
6576502a34cSDavid Hildenbrand {
6586502a34cSDavid Hildenbrand 	unsigned int i;
6596502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
6606502a34cSDavid Hildenbrand 
6616502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
6626502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
6636502a34cSDavid Hildenbrand 	}
6646502a34cSDavid Hildenbrand }
6656502a34cSDavid Hildenbrand 
666e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
667d938dc55SCornelia Huck {
668d938dc55SCornelia Huck 	int r;
669d938dc55SCornelia Huck 
670d938dc55SCornelia Huck 	if (cap->flags)
671d938dc55SCornelia Huck 		return -EINVAL;
672d938dc55SCornelia Huck 
673d938dc55SCornelia Huck 	switch (cap->cap) {
67484223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
675c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
67684223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
67784223598SCornelia Huck 		r = 0;
67884223598SCornelia Huck 		break;
6792444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
680c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
6812444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
6822444b352SDavid Hildenbrand 		r = 0;
6832444b352SDavid Hildenbrand 		break;
68468c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
6855967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
686a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
6875967c17bSDavid Hildenbrand 			r = -EBUSY;
6885967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
689c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
690c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
6912f87d942SGuenther Hutzl 			if (test_facility(134)) {
6922f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
6932f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
6942f87d942SGuenther Hutzl 			}
69553743aa7SMaxim Samoylov 			if (test_facility(135)) {
69653743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
69753743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
69853743aa7SMaxim Samoylov 			}
6997832e91cSChristian Borntraeger 			if (test_facility(148)) {
7007832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 148);
7017832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 148);
7027832e91cSChristian Borntraeger 			}
703d5cb6ab1SChristian Borntraeger 			if (test_facility(152)) {
704d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 152);
705d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 152);
706d5cb6ab1SChristian Borntraeger 			}
70718280d8bSMichael Mueller 			r = 0;
70818280d8bSMichael Mueller 		} else
70918280d8bSMichael Mueller 			r = -EINVAL;
7105967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
711c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
712c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
71368c55750SEric Farman 		break;
714c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
715c6e5f166SFan Zhang 		r = -EINVAL;
716c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
717a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
718c6e5f166SFan Zhang 			r = -EBUSY;
719c6e5f166SFan Zhang 		} else if (test_facility(64)) {
720c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
721c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
722c6e5f166SFan Zhang 			r = 0;
723c6e5f166SFan Zhang 		}
724c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
725c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
726c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
727c6e5f166SFan Zhang 		break;
72847a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
72947a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
73047a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
73147a4693eSYi Min Zhao 			r = -EBUSY;
73247a4693eSYi Min Zhao 		} else {
73347a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
73447a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
73547a4693eSYi Min Zhao 			r = 0;
73647a4693eSYi Min Zhao 		}
73747a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
73847a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
73947a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
74047a4693eSYi Min Zhao 		break;
7414e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
7424e0b1ab7SFan Zhang 		r = -EINVAL;
7434e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
744241e3ec0SChristian Borntraeger 		if (kvm->created_vcpus) {
7454e0b1ab7SFan Zhang 			r = -EBUSY;
7464e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
7474e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
7484e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
7494e0b1ab7SFan Zhang 			r = 0;
7504e0b1ab7SFan Zhang 		}
7514e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
7524e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
7534e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
7544e0b1ab7SFan Zhang 		break;
755a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
756a4499382SJanosch Frank 		mutex_lock(&kvm->lock);
757a4499382SJanosch Frank 		if (kvm->created_vcpus)
758a4499382SJanosch Frank 			r = -EBUSY;
75940ebdb8eSJanosch Frank 		else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
760a4499382SJanosch Frank 			r = -EINVAL;
761a4499382SJanosch Frank 		else {
762a4499382SJanosch Frank 			r = 0;
763d8ed45c5SMichel Lespinasse 			mmap_write_lock(kvm->mm);
764a4499382SJanosch Frank 			kvm->mm->context.allow_gmap_hpage_1m = 1;
765d8ed45c5SMichel Lespinasse 			mmap_write_unlock(kvm->mm);
766a4499382SJanosch Frank 			/*
767a4499382SJanosch Frank 			 * We might have to create fake 4k page
768a4499382SJanosch Frank 			 * tables. To avoid that the hardware works on
769a4499382SJanosch Frank 			 * stale PGSTEs, we emulate these instructions.
770a4499382SJanosch Frank 			 */
771a4499382SJanosch Frank 			kvm->arch.use_skf = 0;
772a4499382SJanosch Frank 			kvm->arch.use_pfmfi = 0;
773a4499382SJanosch Frank 		}
774a4499382SJanosch Frank 		mutex_unlock(&kvm->lock);
775a4499382SJanosch Frank 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
776a4499382SJanosch Frank 			 r ? "(not available)" : "(success)");
777a4499382SJanosch Frank 		break;
778e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
779c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
780e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
781e44fc8c9SEkaterina Tumanova 		r = 0;
782e44fc8c9SEkaterina Tumanova 		break;
7836502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
7846502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
7856502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
7866502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
7876502a34cSDavid Hildenbrand 		r = 0;
7886502a34cSDavid Hildenbrand 		break;
789d938dc55SCornelia Huck 	default:
790d938dc55SCornelia Huck 		r = -EINVAL;
791d938dc55SCornelia Huck 		break;
792d938dc55SCornelia Huck 	}
793d938dc55SCornelia Huck 	return r;
794d938dc55SCornelia Huck }
795d938dc55SCornelia Huck 
7968c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
7978c0a7ce6SDominik Dingel {
7988c0a7ce6SDominik Dingel 	int ret;
7998c0a7ce6SDominik Dingel 
8008c0a7ce6SDominik Dingel 	switch (attr->attr) {
8018c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
8028c0a7ce6SDominik Dingel 		ret = 0;
803c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
804a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
805a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
8068c0a7ce6SDominik Dingel 			ret = -EFAULT;
8078c0a7ce6SDominik Dingel 		break;
8088c0a7ce6SDominik Dingel 	default:
8098c0a7ce6SDominik Dingel 		ret = -ENXIO;
8108c0a7ce6SDominik Dingel 		break;
8118c0a7ce6SDominik Dingel 	}
8128c0a7ce6SDominik Dingel 	return ret;
8138c0a7ce6SDominik Dingel }
8148c0a7ce6SDominik Dingel 
8158c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
8164f718eabSDominik Dingel {
8174f718eabSDominik Dingel 	int ret;
8184f718eabSDominik Dingel 	unsigned int idx;
8194f718eabSDominik Dingel 	switch (attr->attr) {
8204f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
821f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
822c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
823e6db1d61SDominik Dingel 			break;
824e6db1d61SDominik Dingel 
825c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
8264f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
827a4499382SJanosch Frank 		if (kvm->created_vcpus)
828a4499382SJanosch Frank 			ret = -EBUSY;
829a4499382SJanosch Frank 		else if (kvm->mm->context.allow_gmap_hpage_1m)
830a4499382SJanosch Frank 			ret = -EINVAL;
831a4499382SJanosch Frank 		else {
8324f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
833c9f0a2b8SJanosch Frank 			/* Not compatible with cmma. */
834c9f0a2b8SJanosch Frank 			kvm->arch.use_pfmfi = 0;
8354f718eabSDominik Dingel 			ret = 0;
8364f718eabSDominik Dingel 		}
8374f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
8384f718eabSDominik Dingel 		break;
8394f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
840f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
841f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
842f9cbd9b0SDavid Hildenbrand 			break;
843c3489155SDominik Dingel 		ret = -EINVAL;
844c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
845c3489155SDominik Dingel 			break;
846c3489155SDominik Dingel 
847c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
8484f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
8494f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
850a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
8514f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
8524f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
8534f718eabSDominik Dingel 		ret = 0;
8544f718eabSDominik Dingel 		break;
8558c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
8568c0a7ce6SDominik Dingel 		unsigned long new_limit;
8578c0a7ce6SDominik Dingel 
8588c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
8598c0a7ce6SDominik Dingel 			return -EINVAL;
8608c0a7ce6SDominik Dingel 
8618c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
8628c0a7ce6SDominik Dingel 			return -EFAULT;
8638c0a7ce6SDominik Dingel 
864a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
865a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
8668c0a7ce6SDominik Dingel 			return -E2BIG;
8678c0a7ce6SDominik Dingel 
868a3a92c31SDominik Dingel 		if (!new_limit)
869a3a92c31SDominik Dingel 			return -EINVAL;
870a3a92c31SDominik Dingel 
8716ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
872a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
873a3a92c31SDominik Dingel 			new_limit -= 1;
874a3a92c31SDominik Dingel 
8758c0a7ce6SDominik Dingel 		ret = -EBUSY;
8768c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
877a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
8786ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
8796ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
8808c0a7ce6SDominik Dingel 
8818c0a7ce6SDominik Dingel 			if (!new) {
8828c0a7ce6SDominik Dingel 				ret = -ENOMEM;
8838c0a7ce6SDominik Dingel 			} else {
8846ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
8858c0a7ce6SDominik Dingel 				new->private = kvm;
8868c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
8878c0a7ce6SDominik Dingel 				ret = 0;
8888c0a7ce6SDominik Dingel 			}
8898c0a7ce6SDominik Dingel 		}
8908c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
891a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
892a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
893a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
8948c0a7ce6SDominik Dingel 		break;
8958c0a7ce6SDominik Dingel 	}
8964f718eabSDominik Dingel 	default:
8974f718eabSDominik Dingel 		ret = -ENXIO;
8984f718eabSDominik Dingel 		break;
8994f718eabSDominik Dingel 	}
9004f718eabSDominik Dingel 	return ret;
9014f718eabSDominik Dingel }
9024f718eabSDominik Dingel 
903a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
904a374e892STony Krowiak 
90520c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
906a374e892STony Krowiak {
907a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
908a374e892STony Krowiak 	int i;
909a374e892STony Krowiak 
91020c922f0STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
91120c922f0STony Krowiak 
9123194cdb7SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
91320c922f0STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
9143194cdb7SDavid Hildenbrand 		/* recreate the shadow crycb by leaving the VSIE handler */
9153194cdb7SDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
9163194cdb7SDavid Hildenbrand 	}
91720c922f0STony Krowiak 
91820c922f0STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
91920c922f0STony Krowiak }
92020c922f0STony Krowiak 
92120c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
92220c922f0STony Krowiak {
923a374e892STony Krowiak 	mutex_lock(&kvm->lock);
924a374e892STony Krowiak 	switch (attr->attr) {
925a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
9268e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9278e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
92837940fb0STony Krowiak 			return -EINVAL;
9298e41bd54SChristian Borntraeger 		}
930a374e892STony Krowiak 		get_random_bytes(
931a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
932a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
933a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
934c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
935a374e892STony Krowiak 		break;
936a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
9378e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9388e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
93937940fb0STony Krowiak 			return -EINVAL;
9408e41bd54SChristian Borntraeger 		}
941a374e892STony Krowiak 		get_random_bytes(
942a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
943a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
944a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
945c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
946a374e892STony Krowiak 		break;
947a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
9488e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9498e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
95037940fb0STony Krowiak 			return -EINVAL;
9518e41bd54SChristian Borntraeger 		}
952a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
953a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
954a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
955c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
956a374e892STony Krowiak 		break;
957a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
9588e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9598e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
96037940fb0STony Krowiak 			return -EINVAL;
9618e41bd54SChristian Borntraeger 		}
962a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
963a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
964a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
965c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
966a374e892STony Krowiak 		break;
96737940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_APIE:
96837940fb0STony Krowiak 		if (!ap_instructions_available()) {
96937940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
97037940fb0STony Krowiak 			return -EOPNOTSUPP;
97137940fb0STony Krowiak 		}
97237940fb0STony Krowiak 		kvm->arch.crypto.apie = 1;
97337940fb0STony Krowiak 		break;
97437940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_APIE:
97537940fb0STony Krowiak 		if (!ap_instructions_available()) {
97637940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
97737940fb0STony Krowiak 			return -EOPNOTSUPP;
97837940fb0STony Krowiak 		}
97937940fb0STony Krowiak 		kvm->arch.crypto.apie = 0;
98037940fb0STony Krowiak 		break;
981a374e892STony Krowiak 	default:
982a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
983a374e892STony Krowiak 		return -ENXIO;
984a374e892STony Krowiak 	}
985a374e892STony Krowiak 
98620c922f0STony Krowiak 	kvm_s390_vcpu_crypto_reset_all(kvm);
987a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
988a374e892STony Krowiak 	return 0;
989a374e892STony Krowiak }
990a374e892STony Krowiak 
991190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
992190df4a2SClaudio Imbrenda {
993190df4a2SClaudio Imbrenda 	int cx;
994190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
995190df4a2SClaudio Imbrenda 
996190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
997190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
998190df4a2SClaudio Imbrenda }
999190df4a2SClaudio Imbrenda 
1000190df4a2SClaudio Imbrenda /*
1001190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
10021de1ea7eSChristian Borntraeger  * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
1003190df4a2SClaudio Imbrenda  */
1004190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
1005190df4a2SClaudio Imbrenda {
1006190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
1007190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
1008afdad616SClaudio Imbrenda 	unsigned long ram_pages = 0;
1009190df4a2SClaudio Imbrenda 	int slotnr;
1010190df4a2SClaudio Imbrenda 
1011190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
1012afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
1013190df4a2SClaudio Imbrenda 		return 0;
1014190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
1015190df4a2SClaudio Imbrenda 	if (!slots || !slots->used_slots)
1016190df4a2SClaudio Imbrenda 		return -EINVAL;
1017190df4a2SClaudio Imbrenda 
1018afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma) {
1019afdad616SClaudio Imbrenda 		kvm->arch.migration_mode = 1;
1020afdad616SClaudio Imbrenda 		return 0;
1021190df4a2SClaudio Imbrenda 	}
1022190df4a2SClaudio Imbrenda 	/* mark all the pages in active slots as dirty */
1023190df4a2SClaudio Imbrenda 	for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
1024190df4a2SClaudio Imbrenda 		ms = slots->memslots + slotnr;
102513a17cc0SIgor Mammedov 		if (!ms->dirty_bitmap)
102613a17cc0SIgor Mammedov 			return -EINVAL;
1027afdad616SClaudio Imbrenda 		/*
1028afdad616SClaudio Imbrenda 		 * The second half of the bitmap is only used on x86,
1029afdad616SClaudio Imbrenda 		 * and would be wasted otherwise, so we put it to good
1030afdad616SClaudio Imbrenda 		 * use here to keep track of the state of the storage
1031afdad616SClaudio Imbrenda 		 * attributes.
1032afdad616SClaudio Imbrenda 		 */
1033afdad616SClaudio Imbrenda 		memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1034afdad616SClaudio Imbrenda 		ram_pages += ms->npages;
1035190df4a2SClaudio Imbrenda 	}
1036afdad616SClaudio Imbrenda 	atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1037afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 1;
1038190df4a2SClaudio Imbrenda 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1039190df4a2SClaudio Imbrenda 	return 0;
1040190df4a2SClaudio Imbrenda }
1041190df4a2SClaudio Imbrenda 
1042190df4a2SClaudio Imbrenda /*
10431de1ea7eSChristian Borntraeger  * Must be called with kvm->slots_lock to avoid races with ourselves and
1044190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
1045190df4a2SClaudio Imbrenda  */
1046190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1047190df4a2SClaudio Imbrenda {
1048190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
1049afdad616SClaudio Imbrenda 	if (!kvm->arch.migration_mode)
1050190df4a2SClaudio Imbrenda 		return 0;
1051afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 0;
1052afdad616SClaudio Imbrenda 	if (kvm->arch.use_cmma)
1053190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1054190df4a2SClaudio Imbrenda 	return 0;
1055190df4a2SClaudio Imbrenda }
1056190df4a2SClaudio Imbrenda 
1057190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
1058190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1059190df4a2SClaudio Imbrenda {
10601de1ea7eSChristian Borntraeger 	int res = -ENXIO;
1061190df4a2SClaudio Imbrenda 
10621de1ea7eSChristian Borntraeger 	mutex_lock(&kvm->slots_lock);
1063190df4a2SClaudio Imbrenda 	switch (attr->attr) {
1064190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
1065190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
1066190df4a2SClaudio Imbrenda 		break;
1067190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
1068190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
1069190df4a2SClaudio Imbrenda 		break;
1070190df4a2SClaudio Imbrenda 	default:
1071190df4a2SClaudio Imbrenda 		break;
1072190df4a2SClaudio Imbrenda 	}
10731de1ea7eSChristian Borntraeger 	mutex_unlock(&kvm->slots_lock);
1074190df4a2SClaudio Imbrenda 
1075190df4a2SClaudio Imbrenda 	return res;
1076190df4a2SClaudio Imbrenda }
1077190df4a2SClaudio Imbrenda 
1078190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
1079190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1080190df4a2SClaudio Imbrenda {
1081afdad616SClaudio Imbrenda 	u64 mig = kvm->arch.migration_mode;
1082190df4a2SClaudio Imbrenda 
1083190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1084190df4a2SClaudio Imbrenda 		return -ENXIO;
1085190df4a2SClaudio Imbrenda 
1086190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1087190df4a2SClaudio Imbrenda 		return -EFAULT;
1088190df4a2SClaudio Imbrenda 	return 0;
1089190df4a2SClaudio Imbrenda }
1090190df4a2SClaudio Imbrenda 
10918fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
10928fa1696eSCollin L. Walling {
10938fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
10948fa1696eSCollin L. Walling 
10958fa1696eSCollin L. Walling 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
10968fa1696eSCollin L. Walling 		return -EFAULT;
10978fa1696eSCollin L. Walling 
10980e7def5fSDavid Hildenbrand 	if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
10998fa1696eSCollin L. Walling 		return -EINVAL;
11000e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
11018fa1696eSCollin L. Walling 
11028fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
11038fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
11048fa1696eSCollin L. Walling 
11058fa1696eSCollin L. Walling 	return 0;
11068fa1696eSCollin L. Walling }
11078fa1696eSCollin L. Walling 
110872f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
110972f25020SJason J. Herne {
111072f25020SJason J. Herne 	u8 gtod_high;
111172f25020SJason J. Herne 
111272f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
111372f25020SJason J. Herne 					   sizeof(gtod_high)))
111472f25020SJason J. Herne 		return -EFAULT;
111572f25020SJason J. Herne 
111672f25020SJason J. Herne 	if (gtod_high != 0)
111772f25020SJason J. Herne 		return -EINVAL;
111858c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
111972f25020SJason J. Herne 
112072f25020SJason J. Herne 	return 0;
112172f25020SJason J. Herne }
112272f25020SJason J. Herne 
112372f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
112472f25020SJason J. Herne {
11250e7def5fSDavid Hildenbrand 	struct kvm_s390_vm_tod_clock gtod = { 0 };
112672f25020SJason J. Herne 
11270e7def5fSDavid Hildenbrand 	if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
11280e7def5fSDavid Hildenbrand 			   sizeof(gtod.tod)))
112972f25020SJason J. Herne 		return -EFAULT;
113072f25020SJason J. Herne 
11310e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
11320e7def5fSDavid Hildenbrand 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
113372f25020SJason J. Herne 	return 0;
113472f25020SJason J. Herne }
113572f25020SJason J. Herne 
113672f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
113772f25020SJason J. Herne {
113872f25020SJason J. Herne 	int ret;
113972f25020SJason J. Herne 
114072f25020SJason J. Herne 	if (attr->flags)
114172f25020SJason J. Herne 		return -EINVAL;
114272f25020SJason J. Herne 
114372f25020SJason J. Herne 	switch (attr->attr) {
11448fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
11458fa1696eSCollin L. Walling 		ret = kvm_s390_set_tod_ext(kvm, attr);
11468fa1696eSCollin L. Walling 		break;
114772f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
114872f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
114972f25020SJason J. Herne 		break;
115072f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
115172f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
115272f25020SJason J. Herne 		break;
115372f25020SJason J. Herne 	default:
115472f25020SJason J. Herne 		ret = -ENXIO;
115572f25020SJason J. Herne 		break;
115672f25020SJason J. Herne 	}
115772f25020SJason J. Herne 	return ret;
115872f25020SJason J. Herne }
115972f25020SJason J. Herne 
116033d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm,
11618fa1696eSCollin L. Walling 				   struct kvm_s390_vm_tod_clock *gtod)
11628fa1696eSCollin L. Walling {
1163*2cfd7b73SHeiko Carstens 	union tod_clock clk;
11648fa1696eSCollin L. Walling 
11658fa1696eSCollin L. Walling 	preempt_disable();
11668fa1696eSCollin L. Walling 
1167*2cfd7b73SHeiko Carstens 	store_tod_clock_ext(&clk);
11688fa1696eSCollin L. Walling 
1169*2cfd7b73SHeiko Carstens 	gtod->tod = clk.tod + kvm->arch.epoch;
117033d1b272SDavid Hildenbrand 	gtod->epoch_idx = 0;
117133d1b272SDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
1172*2cfd7b73SHeiko Carstens 		gtod->epoch_idx = clk.ei + kvm->arch.epdx;
1173*2cfd7b73SHeiko Carstens 		if (gtod->tod < clk.tod)
11748fa1696eSCollin L. Walling 			gtod->epoch_idx += 1;
117533d1b272SDavid Hildenbrand 	}
11768fa1696eSCollin L. Walling 
11778fa1696eSCollin L. Walling 	preempt_enable();
11788fa1696eSCollin L. Walling }
11798fa1696eSCollin L. Walling 
11808fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
11818fa1696eSCollin L. Walling {
11828fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
11838fa1696eSCollin L. Walling 
11848fa1696eSCollin L. Walling 	memset(&gtod, 0, sizeof(gtod));
118533d1b272SDavid Hildenbrand 	kvm_s390_get_tod_clock(kvm, &gtod);
11868fa1696eSCollin L. Walling 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
11878fa1696eSCollin L. Walling 		return -EFAULT;
11888fa1696eSCollin L. Walling 
11898fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
11908fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
11918fa1696eSCollin L. Walling 	return 0;
11928fa1696eSCollin L. Walling }
11938fa1696eSCollin L. Walling 
119472f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
119572f25020SJason J. Herne {
119672f25020SJason J. Herne 	u8 gtod_high = 0;
119772f25020SJason J. Herne 
119872f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
119972f25020SJason J. Herne 					 sizeof(gtod_high)))
120072f25020SJason J. Herne 		return -EFAULT;
120158c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
120272f25020SJason J. Herne 
120372f25020SJason J. Herne 	return 0;
120472f25020SJason J. Herne }
120572f25020SJason J. Herne 
120672f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
120772f25020SJason J. Herne {
12085a3d883aSDavid Hildenbrand 	u64 gtod;
120972f25020SJason J. Herne 
121060417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
121172f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
121272f25020SJason J. Herne 		return -EFAULT;
121358c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
121472f25020SJason J. Herne 
121572f25020SJason J. Herne 	return 0;
121672f25020SJason J. Herne }
121772f25020SJason J. Herne 
121872f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
121972f25020SJason J. Herne {
122072f25020SJason J. Herne 	int ret;
122172f25020SJason J. Herne 
122272f25020SJason J. Herne 	if (attr->flags)
122372f25020SJason J. Herne 		return -EINVAL;
122472f25020SJason J. Herne 
122572f25020SJason J. Herne 	switch (attr->attr) {
12268fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
12278fa1696eSCollin L. Walling 		ret = kvm_s390_get_tod_ext(kvm, attr);
12288fa1696eSCollin L. Walling 		break;
122972f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
123072f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
123172f25020SJason J. Herne 		break;
123272f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
123372f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
123472f25020SJason J. Herne 		break;
123572f25020SJason J. Herne 	default:
123672f25020SJason J. Herne 		ret = -ENXIO;
123772f25020SJason J. Herne 		break;
123872f25020SJason J. Herne 	}
123972f25020SJason J. Herne 	return ret;
124072f25020SJason J. Herne }
124172f25020SJason J. Herne 
1242658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1243658b6edaSMichael Mueller {
1244658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1245053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
1246658b6edaSMichael Mueller 	int ret = 0;
1247658b6edaSMichael Mueller 
1248658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
1249a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
1250658b6edaSMichael Mueller 		ret = -EBUSY;
1251658b6edaSMichael Mueller 		goto out;
1252658b6edaSMichael Mueller 	}
1253c4196218SChristian Borntraeger 	proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1254658b6edaSMichael Mueller 	if (!proc) {
1255658b6edaSMichael Mueller 		ret = -ENOMEM;
1256658b6edaSMichael Mueller 		goto out;
1257658b6edaSMichael Mueller 	}
1258658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
1259658b6edaSMichael Mueller 			    sizeof(*proc))) {
12609bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
1261053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1262053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
12630487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
1264053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
1265053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
1266053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
1267053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
1268053dd230SDavid Hildenbrand 			else
1269658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
1270053dd230SDavid Hildenbrand 		}
1271c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1272658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1273a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1274a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1275a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1276a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1277a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1278a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1279a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1280658b6edaSMichael Mueller 	} else
1281658b6edaSMichael Mueller 		ret = -EFAULT;
1282658b6edaSMichael Mueller 	kfree(proc);
1283658b6edaSMichael Mueller out:
1284658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1285658b6edaSMichael Mueller 	return ret;
1286658b6edaSMichael Mueller }
1287658b6edaSMichael Mueller 
128815c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
128915c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
129015c9705fSDavid Hildenbrand {
129115c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
129215c9705fSDavid Hildenbrand 
129315c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
129415c9705fSDavid Hildenbrand 		return -EFAULT;
129515c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
129615c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
129715c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
129815c9705fSDavid Hildenbrand 		return -EINVAL;
129915c9705fSDavid Hildenbrand 
130015c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
13012f8311c9SChristian Borntraeger 	if (kvm->created_vcpus) {
13022f8311c9SChristian Borntraeger 		mutex_unlock(&kvm->lock);
13032f8311c9SChristian Borntraeger 		return -EBUSY;
13042f8311c9SChristian Borntraeger 	}
130515c9705fSDavid Hildenbrand 	bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
130615c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
130715c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
13082f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
13092f8311c9SChristian Borntraeger 			 data.feat[0],
13102f8311c9SChristian Borntraeger 			 data.feat[1],
13112f8311c9SChristian Borntraeger 			 data.feat[2]);
13122f8311c9SChristian Borntraeger 	return 0;
131315c9705fSDavid Hildenbrand }
131415c9705fSDavid Hildenbrand 
13150a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
13160a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
13170a763c78SDavid Hildenbrand {
1318346fa2f8SChristian Borntraeger 	mutex_lock(&kvm->lock);
1319346fa2f8SChristian Borntraeger 	if (kvm->created_vcpus) {
1320346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1321346fa2f8SChristian Borntraeger 		return -EBUSY;
1322346fa2f8SChristian Borntraeger 	}
1323346fa2f8SChristian Borntraeger 
1324346fa2f8SChristian Borntraeger 	if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1325346fa2f8SChristian Borntraeger 			   sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1326346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1327346fa2f8SChristian Borntraeger 		return -EFAULT;
1328346fa2f8SChristian Borntraeger 	}
1329346fa2f8SChristian Borntraeger 	mutex_unlock(&kvm->lock);
1330346fa2f8SChristian Borntraeger 
133111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
133211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
133311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
133411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
133511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
133611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
133711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
133811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
133911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
134011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
134111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
134211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
134311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
134411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
134511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KM     subfunc 0x%16.16lx.%16.16lx",
134611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
134711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
134811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
134911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
135011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
135111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
135211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
135311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
135411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
135511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
135611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
135711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
135811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
135911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
136011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
136111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
136211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
136311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
136411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
136511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
136611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
136711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
136811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
136911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
137011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
137111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
137211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
137311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
137411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
137513209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
137613209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
137713209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1378173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1379173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1380173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1381173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1382173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
13834f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
13844f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
13854f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
13864f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
13874f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
138811ba5961SChristian Borntraeger 
1389346fa2f8SChristian Borntraeger 	return 0;
13900a763c78SDavid Hildenbrand }
13910a763c78SDavid Hildenbrand 
1392658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1393658b6edaSMichael Mueller {
1394658b6edaSMichael Mueller 	int ret = -ENXIO;
1395658b6edaSMichael Mueller 
1396658b6edaSMichael Mueller 	switch (attr->attr) {
1397658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1398658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1399658b6edaSMichael Mueller 		break;
140015c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
140115c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
140215c9705fSDavid Hildenbrand 		break;
14030a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
14040a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
14050a763c78SDavid Hildenbrand 		break;
1406658b6edaSMichael Mueller 	}
1407658b6edaSMichael Mueller 	return ret;
1408658b6edaSMichael Mueller }
1409658b6edaSMichael Mueller 
1410658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1411658b6edaSMichael Mueller {
1412658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1413658b6edaSMichael Mueller 	int ret = 0;
1414658b6edaSMichael Mueller 
1415c4196218SChristian Borntraeger 	proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1416658b6edaSMichael Mueller 	if (!proc) {
1417658b6edaSMichael Mueller 		ret = -ENOMEM;
1418658b6edaSMichael Mueller 		goto out;
1419658b6edaSMichael Mueller 	}
14209bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1421658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1422c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1423c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1424a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1425a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1426a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1427a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1428a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1429a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1430a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1431658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1432658b6edaSMichael Mueller 		ret = -EFAULT;
1433658b6edaSMichael Mueller 	kfree(proc);
1434658b6edaSMichael Mueller out:
1435658b6edaSMichael Mueller 	return ret;
1436658b6edaSMichael Mueller }
1437658b6edaSMichael Mueller 
1438658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1439658b6edaSMichael Mueller {
1440658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1441658b6edaSMichael Mueller 	int ret = 0;
1442658b6edaSMichael Mueller 
1443c4196218SChristian Borntraeger 	mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT);
1444658b6edaSMichael Mueller 	if (!mach) {
1445658b6edaSMichael Mueller 		ret = -ENOMEM;
1446658b6edaSMichael Mueller 		goto out;
1447658b6edaSMichael Mueller 	}
1448658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
144937c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1450c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1451981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1452658b6edaSMichael Mueller 	memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
145304478197SChristian Borntraeger 	       sizeof(S390_lowcore.stfle_fac_list));
1454a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1455a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1456a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1457a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1458a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1459a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1460a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1461a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1462a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1463a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1464a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1465658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1466658b6edaSMichael Mueller 		ret = -EFAULT;
1467658b6edaSMichael Mueller 	kfree(mach);
1468658b6edaSMichael Mueller out:
1469658b6edaSMichael Mueller 	return ret;
1470658b6edaSMichael Mueller }
1471658b6edaSMichael Mueller 
147215c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
147315c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
147415c9705fSDavid Hildenbrand {
147515c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
147615c9705fSDavid Hildenbrand 
147715c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
147815c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
147915c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
148015c9705fSDavid Hildenbrand 		return -EFAULT;
14812f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
14822f8311c9SChristian Borntraeger 			 data.feat[0],
14832f8311c9SChristian Borntraeger 			 data.feat[1],
14842f8311c9SChristian Borntraeger 			 data.feat[2]);
148515c9705fSDavid Hildenbrand 	return 0;
148615c9705fSDavid Hildenbrand }
148715c9705fSDavid Hildenbrand 
148815c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
148915c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
149015c9705fSDavid Hildenbrand {
149115c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
149215c9705fSDavid Hildenbrand 
149315c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
149415c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
149515c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
149615c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
149715c9705fSDavid Hildenbrand 		return -EFAULT;
14982f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host feat:  0x%16.16llx.0x%16.16llx.0x%16.16llx",
14992f8311c9SChristian Borntraeger 			 data.feat[0],
15002f8311c9SChristian Borntraeger 			 data.feat[1],
15012f8311c9SChristian Borntraeger 			 data.feat[2]);
150215c9705fSDavid Hildenbrand 	return 0;
150315c9705fSDavid Hildenbrand }
150415c9705fSDavid Hildenbrand 
15050a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
15060a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
15070a763c78SDavid Hildenbrand {
1508346fa2f8SChristian Borntraeger 	if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1509346fa2f8SChristian Borntraeger 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
1510346fa2f8SChristian Borntraeger 		return -EFAULT;
1511346fa2f8SChristian Borntraeger 
151211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
151311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
151411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
151511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
151611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
151711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
151811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
151911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
152011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
152111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
152211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
152311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
152411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
152511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
152611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KM     subfunc 0x%16.16lx.%16.16lx",
152711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
152811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
152911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
153011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
153111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
153211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
153311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
153411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
153511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
153611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
153711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
153811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
153911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
154011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
154111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
154211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
154311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
154411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
154511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
154611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
154711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
154811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
154911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
155011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
155111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
155211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
155311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
155411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
155511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
155613209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
155713209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
155813209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1559173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1560173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1561173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1562173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1563173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
15644f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
15654f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
15664f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
15674f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
15684f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
156911ba5961SChristian Borntraeger 
1570346fa2f8SChristian Borntraeger 	return 0;
15710a763c78SDavid Hildenbrand }
15720a763c78SDavid Hildenbrand 
15730a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
15740a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
15750a763c78SDavid Hildenbrand {
15760a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
15770a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
15780a763c78SDavid Hildenbrand 		return -EFAULT;
157911ba5961SChristian Borntraeger 
158011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
158111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
158211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
158311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
158411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
158511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PTFF   subfunc 0x%16.16lx.%16.16lx",
158611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
158711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
158811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMAC   subfunc 0x%16.16lx.%16.16lx",
158911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
159011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
159111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMC    subfunc 0x%16.16lx.%16.16lx",
159211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
159311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
159411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KM     subfunc 0x%16.16lx.%16.16lx",
159511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
159611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
159711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KIMD   subfunc 0x%16.16lx.%16.16lx",
159811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
159911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
160011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KLMD   subfunc 0x%16.16lx.%16.16lx",
160111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
160211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
160311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCKMO  subfunc 0x%16.16lx.%16.16lx",
160411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
160511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
160611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMCTR  subfunc 0x%16.16lx.%16.16lx",
160711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
160811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
160911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMF    subfunc 0x%16.16lx.%16.16lx",
161011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
161111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
161211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMO    subfunc 0x%16.16lx.%16.16lx",
161311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
161411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
161511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCC    subfunc 0x%16.16lx.%16.16lx",
161611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
161711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
161811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PPNO   subfunc 0x%16.16lx.%16.16lx",
161911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
162011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
162111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMA    subfunc 0x%16.16lx.%16.16lx",
162211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
162311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
162413209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KDSA   subfunc 0x%16.16lx.%16.16lx",
162513209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
162613209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1627173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1628173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1629173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1630173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1631173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
16324f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
16334f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
16344f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
16354f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
16364f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
163711ba5961SChristian Borntraeger 
16380a763c78SDavid Hildenbrand 	return 0;
16390a763c78SDavid Hildenbrand }
1640346fa2f8SChristian Borntraeger 
1641658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1642658b6edaSMichael Mueller {
1643658b6edaSMichael Mueller 	int ret = -ENXIO;
1644658b6edaSMichael Mueller 
1645658b6edaSMichael Mueller 	switch (attr->attr) {
1646658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1647658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1648658b6edaSMichael Mueller 		break;
1649658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1650658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1651658b6edaSMichael Mueller 		break;
165215c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
165315c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
165415c9705fSDavid Hildenbrand 		break;
165515c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
165615c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
165715c9705fSDavid Hildenbrand 		break;
16580a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
16590a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
16600a763c78SDavid Hildenbrand 		break;
16610a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
16620a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
16630a763c78SDavid Hildenbrand 		break;
1664658b6edaSMichael Mueller 	}
1665658b6edaSMichael Mueller 	return ret;
1666658b6edaSMichael Mueller }
1667658b6edaSMichael Mueller 
1668f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1669f2061656SDominik Dingel {
1670f2061656SDominik Dingel 	int ret;
1671f2061656SDominik Dingel 
1672f2061656SDominik Dingel 	switch (attr->group) {
16734f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
16748c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
16754f718eabSDominik Dingel 		break;
167672f25020SJason J. Herne 	case KVM_S390_VM_TOD:
167772f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
167872f25020SJason J. Herne 		break;
1679658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1680658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1681658b6edaSMichael Mueller 		break;
1682a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1683a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1684a374e892STony Krowiak 		break;
1685190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1686190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1687190df4a2SClaudio Imbrenda 		break;
1688f2061656SDominik Dingel 	default:
1689f2061656SDominik Dingel 		ret = -ENXIO;
1690f2061656SDominik Dingel 		break;
1691f2061656SDominik Dingel 	}
1692f2061656SDominik Dingel 
1693f2061656SDominik Dingel 	return ret;
1694f2061656SDominik Dingel }
1695f2061656SDominik Dingel 
1696f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1697f2061656SDominik Dingel {
16988c0a7ce6SDominik Dingel 	int ret;
16998c0a7ce6SDominik Dingel 
17008c0a7ce6SDominik Dingel 	switch (attr->group) {
17018c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
17028c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
17038c0a7ce6SDominik Dingel 		break;
170472f25020SJason J. Herne 	case KVM_S390_VM_TOD:
170572f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
170672f25020SJason J. Herne 		break;
1707658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1708658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1709658b6edaSMichael Mueller 		break;
1710190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1711190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1712190df4a2SClaudio Imbrenda 		break;
17138c0a7ce6SDominik Dingel 	default:
17148c0a7ce6SDominik Dingel 		ret = -ENXIO;
17158c0a7ce6SDominik Dingel 		break;
17168c0a7ce6SDominik Dingel 	}
17178c0a7ce6SDominik Dingel 
17188c0a7ce6SDominik Dingel 	return ret;
1719f2061656SDominik Dingel }
1720f2061656SDominik Dingel 
1721f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1722f2061656SDominik Dingel {
1723f2061656SDominik Dingel 	int ret;
1724f2061656SDominik Dingel 
1725f2061656SDominik Dingel 	switch (attr->group) {
17264f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
17274f718eabSDominik Dingel 		switch (attr->attr) {
17284f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
17294f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1730f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1731f9cbd9b0SDavid Hildenbrand 			break;
17328c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
17334f718eabSDominik Dingel 			ret = 0;
17344f718eabSDominik Dingel 			break;
17354f718eabSDominik Dingel 		default:
17364f718eabSDominik Dingel 			ret = -ENXIO;
17374f718eabSDominik Dingel 			break;
17384f718eabSDominik Dingel 		}
17394f718eabSDominik Dingel 		break;
174072f25020SJason J. Herne 	case KVM_S390_VM_TOD:
174172f25020SJason J. Herne 		switch (attr->attr) {
174272f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
174372f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
174472f25020SJason J. Herne 			ret = 0;
174572f25020SJason J. Herne 			break;
174672f25020SJason J. Herne 		default:
174772f25020SJason J. Herne 			ret = -ENXIO;
174872f25020SJason J. Herne 			break;
174972f25020SJason J. Herne 		}
175072f25020SJason J. Herne 		break;
1751658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1752658b6edaSMichael Mueller 		switch (attr->attr) {
1753658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1754658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
175515c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
175615c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
17570a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1758346fa2f8SChristian Borntraeger 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1759658b6edaSMichael Mueller 			ret = 0;
1760658b6edaSMichael Mueller 			break;
1761658b6edaSMichael Mueller 		default:
1762658b6edaSMichael Mueller 			ret = -ENXIO;
1763658b6edaSMichael Mueller 			break;
1764658b6edaSMichael Mueller 		}
1765658b6edaSMichael Mueller 		break;
1766a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1767a374e892STony Krowiak 		switch (attr->attr) {
1768a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1769a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1770a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1771a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1772a374e892STony Krowiak 			ret = 0;
1773a374e892STony Krowiak 			break;
177437940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_APIE:
177537940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_APIE:
177637940fb0STony Krowiak 			ret = ap_instructions_available() ? 0 : -ENXIO;
177737940fb0STony Krowiak 			break;
1778a374e892STony Krowiak 		default:
1779a374e892STony Krowiak 			ret = -ENXIO;
1780a374e892STony Krowiak 			break;
1781a374e892STony Krowiak 		}
1782a374e892STony Krowiak 		break;
1783190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1784190df4a2SClaudio Imbrenda 		ret = 0;
1785190df4a2SClaudio Imbrenda 		break;
1786f2061656SDominik Dingel 	default:
1787f2061656SDominik Dingel 		ret = -ENXIO;
1788f2061656SDominik Dingel 		break;
1789f2061656SDominik Dingel 	}
1790f2061656SDominik Dingel 
1791f2061656SDominik Dingel 	return ret;
1792f2061656SDominik Dingel }
1793f2061656SDominik Dingel 
179430ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
179530ee2a98SJason J. Herne {
179630ee2a98SJason J. Herne 	uint8_t *keys;
179730ee2a98SJason J. Herne 	uint64_t hva;
17984f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
179930ee2a98SJason J. Herne 
180030ee2a98SJason J. Herne 	if (args->flags != 0)
180130ee2a98SJason J. Herne 		return -EINVAL;
180230ee2a98SJason J. Herne 
180330ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
180455531b74SJanosch Frank 	if (!mm_uses_skeys(current->mm))
180530ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
180630ee2a98SJason J. Herne 
180730ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
180830ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
180930ee2a98SJason J. Herne 		return -EINVAL;
181030ee2a98SJason J. Herne 
1811c4196218SChristian Borntraeger 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
181230ee2a98SJason J. Herne 	if (!keys)
181330ee2a98SJason J. Herne 		return -ENOMEM;
181430ee2a98SJason J. Herne 
1815d8ed45c5SMichel Lespinasse 	mmap_read_lock(current->mm);
18164f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
181730ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
181830ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
181930ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
182030ee2a98SJason J. Herne 			r = -EFAULT;
1821d3ed1ceeSMartin Schwidefsky 			break;
182230ee2a98SJason J. Herne 		}
182330ee2a98SJason J. Herne 
1824154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1825154c8c19SDavid Hildenbrand 		if (r)
1826d3ed1ceeSMartin Schwidefsky 			break;
182730ee2a98SJason J. Herne 	}
18284f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1829d8ed45c5SMichel Lespinasse 	mmap_read_unlock(current->mm);
183030ee2a98SJason J. Herne 
1831d3ed1ceeSMartin Schwidefsky 	if (!r) {
183230ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
183330ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
183430ee2a98SJason J. Herne 		if (r)
183530ee2a98SJason J. Herne 			r = -EFAULT;
1836d3ed1ceeSMartin Schwidefsky 	}
1837d3ed1ceeSMartin Schwidefsky 
183830ee2a98SJason J. Herne 	kvfree(keys);
183930ee2a98SJason J. Herne 	return r;
184030ee2a98SJason J. Herne }
184130ee2a98SJason J. Herne 
184230ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
184330ee2a98SJason J. Herne {
184430ee2a98SJason J. Herne 	uint8_t *keys;
184530ee2a98SJason J. Herne 	uint64_t hva;
18464f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
1847bd096f64SJanosch Frank 	bool unlocked;
184830ee2a98SJason J. Herne 
184930ee2a98SJason J. Herne 	if (args->flags != 0)
185030ee2a98SJason J. Herne 		return -EINVAL;
185130ee2a98SJason J. Herne 
185230ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
185330ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
185430ee2a98SJason J. Herne 		return -EINVAL;
185530ee2a98SJason J. Herne 
1856c4196218SChristian Borntraeger 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
185730ee2a98SJason J. Herne 	if (!keys)
185830ee2a98SJason J. Herne 		return -ENOMEM;
185930ee2a98SJason J. Herne 
186030ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
186130ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
186230ee2a98SJason J. Herne 	if (r) {
186330ee2a98SJason J. Herne 		r = -EFAULT;
186430ee2a98SJason J. Herne 		goto out;
186530ee2a98SJason J. Herne 	}
186630ee2a98SJason J. Herne 
186730ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
186814d4a425SDominik Dingel 	r = s390_enable_skey();
186914d4a425SDominik Dingel 	if (r)
187014d4a425SDominik Dingel 		goto out;
187130ee2a98SJason J. Herne 
1872bd096f64SJanosch Frank 	i = 0;
1873d8ed45c5SMichel Lespinasse 	mmap_read_lock(current->mm);
18744f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
1875bd096f64SJanosch Frank         while (i < args->count) {
1876bd096f64SJanosch Frank 		unlocked = false;
187730ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
187830ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
187930ee2a98SJason J. Herne 			r = -EFAULT;
1880d3ed1ceeSMartin Schwidefsky 			break;
188130ee2a98SJason J. Herne 		}
188230ee2a98SJason J. Herne 
188330ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
188430ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
188530ee2a98SJason J. Herne 			r = -EINVAL;
1886d3ed1ceeSMartin Schwidefsky 			break;
188730ee2a98SJason J. Herne 		}
188830ee2a98SJason J. Herne 
1889fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
1890bd096f64SJanosch Frank 		if (r) {
189164019a2eSPeter Xu 			r = fixup_user_fault(current->mm, hva,
1892bd096f64SJanosch Frank 					     FAULT_FLAG_WRITE, &unlocked);
189330ee2a98SJason J. Herne 			if (r)
1894d3ed1ceeSMartin Schwidefsky 				break;
189530ee2a98SJason J. Herne 		}
1896bd096f64SJanosch Frank 		if (!r)
1897bd096f64SJanosch Frank 			i++;
1898bd096f64SJanosch Frank 	}
18994f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1900d8ed45c5SMichel Lespinasse 	mmap_read_unlock(current->mm);
190130ee2a98SJason J. Herne out:
190230ee2a98SJason J. Herne 	kvfree(keys);
190330ee2a98SJason J. Herne 	return r;
190430ee2a98SJason J. Herne }
190530ee2a98SJason J. Herne 
19064036e387SClaudio Imbrenda /*
19074036e387SClaudio Imbrenda  * Base address and length must be sent at the start of each block, therefore
19084036e387SClaudio Imbrenda  * it's cheaper to send some clean data, as long as it's less than the size of
19094036e387SClaudio Imbrenda  * two longs.
19104036e387SClaudio Imbrenda  */
19114036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
19124036e387SClaudio Imbrenda /* for consistency */
19134036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
19144036e387SClaudio Imbrenda 
19154036e387SClaudio Imbrenda /*
1916afdad616SClaudio Imbrenda  * Similar to gfn_to_memslot, but returns the index of a memslot also when the
1917afdad616SClaudio Imbrenda  * address falls in a hole. In that case the index of one of the memslots
1918afdad616SClaudio Imbrenda  * bordering the hole is returned.
1919afdad616SClaudio Imbrenda  */
1920afdad616SClaudio Imbrenda static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn)
1921afdad616SClaudio Imbrenda {
1922afdad616SClaudio Imbrenda 	int start = 0, end = slots->used_slots;
1923afdad616SClaudio Imbrenda 	int slot = atomic_read(&slots->lru_slot);
1924afdad616SClaudio Imbrenda 	struct kvm_memory_slot *memslots = slots->memslots;
1925afdad616SClaudio Imbrenda 
1926afdad616SClaudio Imbrenda 	if (gfn >= memslots[slot].base_gfn &&
1927afdad616SClaudio Imbrenda 	    gfn < memslots[slot].base_gfn + memslots[slot].npages)
1928afdad616SClaudio Imbrenda 		return slot;
1929afdad616SClaudio Imbrenda 
1930afdad616SClaudio Imbrenda 	while (start < end) {
1931afdad616SClaudio Imbrenda 		slot = start + (end - start) / 2;
1932afdad616SClaudio Imbrenda 
1933afdad616SClaudio Imbrenda 		if (gfn >= memslots[slot].base_gfn)
1934afdad616SClaudio Imbrenda 			end = slot;
1935afdad616SClaudio Imbrenda 		else
1936afdad616SClaudio Imbrenda 			start = slot + 1;
1937afdad616SClaudio Imbrenda 	}
1938afdad616SClaudio Imbrenda 
193997daa028SSean Christopherson 	if (start >= slots->used_slots)
194097daa028SSean Christopherson 		return slots->used_slots - 1;
194197daa028SSean Christopherson 
1942afdad616SClaudio Imbrenda 	if (gfn >= memslots[start].base_gfn &&
1943afdad616SClaudio Imbrenda 	    gfn < memslots[start].base_gfn + memslots[start].npages) {
1944afdad616SClaudio Imbrenda 		atomic_set(&slots->lru_slot, start);
1945afdad616SClaudio Imbrenda 	}
1946afdad616SClaudio Imbrenda 
1947afdad616SClaudio Imbrenda 	return start;
1948afdad616SClaudio Imbrenda }
1949afdad616SClaudio Imbrenda 
1950afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1951afdad616SClaudio Imbrenda 			      u8 *res, unsigned long bufsize)
1952afdad616SClaudio Imbrenda {
1953afdad616SClaudio Imbrenda 	unsigned long pgstev, hva, cur_gfn = args->start_gfn;
1954afdad616SClaudio Imbrenda 
1955afdad616SClaudio Imbrenda 	args->count = 0;
1956afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
1957afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
1958afdad616SClaudio Imbrenda 		/*
1959afdad616SClaudio Imbrenda 		 * We return an error if the first value was invalid, but we
1960afdad616SClaudio Imbrenda 		 * return successfully if at least one value was copied.
1961afdad616SClaudio Imbrenda 		 */
1962afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
1963afdad616SClaudio Imbrenda 			return args->count ? 0 : -EFAULT;
1964afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
1965afdad616SClaudio Imbrenda 			pgstev = 0;
1966afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
1967afdad616SClaudio Imbrenda 		cur_gfn++;
1968afdad616SClaudio Imbrenda 	}
1969afdad616SClaudio Imbrenda 
1970afdad616SClaudio Imbrenda 	return 0;
1971afdad616SClaudio Imbrenda }
1972afdad616SClaudio Imbrenda 
1973afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
1974afdad616SClaudio Imbrenda 					      unsigned long cur_gfn)
1975afdad616SClaudio Imbrenda {
1976afdad616SClaudio Imbrenda 	int slotidx = gfn_to_memslot_approx(slots, cur_gfn);
1977afdad616SClaudio Imbrenda 	struct kvm_memory_slot *ms = slots->memslots + slotidx;
1978afdad616SClaudio Imbrenda 	unsigned long ofs = cur_gfn - ms->base_gfn;
1979afdad616SClaudio Imbrenda 
1980afdad616SClaudio Imbrenda 	if (ms->base_gfn + ms->npages <= cur_gfn) {
1981afdad616SClaudio Imbrenda 		slotidx--;
1982afdad616SClaudio Imbrenda 		/* If we are above the highest slot, wrap around */
1983afdad616SClaudio Imbrenda 		if (slotidx < 0)
1984afdad616SClaudio Imbrenda 			slotidx = slots->used_slots - 1;
1985afdad616SClaudio Imbrenda 
1986afdad616SClaudio Imbrenda 		ms = slots->memslots + slotidx;
1987afdad616SClaudio Imbrenda 		ofs = 0;
1988afdad616SClaudio Imbrenda 	}
1989afdad616SClaudio Imbrenda 	ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
1990afdad616SClaudio Imbrenda 	while ((slotidx > 0) && (ofs >= ms->npages)) {
1991afdad616SClaudio Imbrenda 		slotidx--;
1992afdad616SClaudio Imbrenda 		ms = slots->memslots + slotidx;
1993afdad616SClaudio Imbrenda 		ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0);
1994afdad616SClaudio Imbrenda 	}
1995afdad616SClaudio Imbrenda 	return ms->base_gfn + ofs;
1996afdad616SClaudio Imbrenda }
1997afdad616SClaudio Imbrenda 
1998afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1999afdad616SClaudio Imbrenda 			     u8 *res, unsigned long bufsize)
2000afdad616SClaudio Imbrenda {
2001afdad616SClaudio Imbrenda 	unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2002afdad616SClaudio Imbrenda 	struct kvm_memslots *slots = kvm_memslots(kvm);
2003afdad616SClaudio Imbrenda 	struct kvm_memory_slot *ms;
2004afdad616SClaudio Imbrenda 
20050774a964SSean Christopherson 	if (unlikely(!slots->used_slots))
20060774a964SSean Christopherson 		return 0;
20070774a964SSean Christopherson 
2008afdad616SClaudio Imbrenda 	cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2009afdad616SClaudio Imbrenda 	ms = gfn_to_memslot(kvm, cur_gfn);
2010afdad616SClaudio Imbrenda 	args->count = 0;
2011afdad616SClaudio Imbrenda 	args->start_gfn = cur_gfn;
2012afdad616SClaudio Imbrenda 	if (!ms)
2013afdad616SClaudio Imbrenda 		return 0;
2014afdad616SClaudio Imbrenda 	next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2015afdad616SClaudio Imbrenda 	mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages;
2016afdad616SClaudio Imbrenda 
2017afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
2018afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
2019afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
2020afdad616SClaudio Imbrenda 			return 0;
2021afdad616SClaudio Imbrenda 		/* Decrement only if we actually flipped the bit to 0 */
2022afdad616SClaudio Imbrenda 		if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2023afdad616SClaudio Imbrenda 			atomic64_dec(&kvm->arch.cmma_dirty_pages);
2024afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2025afdad616SClaudio Imbrenda 			pgstev = 0;
2026afdad616SClaudio Imbrenda 		/* Save the value */
2027afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
2028afdad616SClaudio Imbrenda 		/* If the next bit is too far away, stop. */
2029afdad616SClaudio Imbrenda 		if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2030afdad616SClaudio Imbrenda 			return 0;
2031afdad616SClaudio Imbrenda 		/* If we reached the previous "next", find the next one */
2032afdad616SClaudio Imbrenda 		if (cur_gfn == next_gfn)
2033afdad616SClaudio Imbrenda 			next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2034afdad616SClaudio Imbrenda 		/* Reached the end of memory or of the buffer, stop */
2035afdad616SClaudio Imbrenda 		if ((next_gfn >= mem_end) ||
2036afdad616SClaudio Imbrenda 		    (next_gfn - args->start_gfn >= bufsize))
2037afdad616SClaudio Imbrenda 			return 0;
2038afdad616SClaudio Imbrenda 		cur_gfn++;
2039afdad616SClaudio Imbrenda 		/* Reached the end of the current memslot, take the next one. */
2040afdad616SClaudio Imbrenda 		if (cur_gfn - ms->base_gfn >= ms->npages) {
2041afdad616SClaudio Imbrenda 			ms = gfn_to_memslot(kvm, cur_gfn);
2042afdad616SClaudio Imbrenda 			if (!ms)
2043afdad616SClaudio Imbrenda 				return 0;
2044afdad616SClaudio Imbrenda 		}
2045afdad616SClaudio Imbrenda 	}
2046afdad616SClaudio Imbrenda 	return 0;
2047afdad616SClaudio Imbrenda }
2048afdad616SClaudio Imbrenda 
2049afdad616SClaudio Imbrenda /*
20504036e387SClaudio Imbrenda  * This function searches for the next page with dirty CMMA attributes, and
20514036e387SClaudio Imbrenda  * saves the attributes in the buffer up to either the end of the buffer or
20524036e387SClaudio Imbrenda  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
20534036e387SClaudio Imbrenda  * no trailing clean bytes are saved.
20544036e387SClaudio Imbrenda  * In case no dirty bits were found, or if CMMA was not enabled or used, the
20554036e387SClaudio Imbrenda  * output buffer will indicate 0 as length.
20564036e387SClaudio Imbrenda  */
20574036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
20584036e387SClaudio Imbrenda 				  struct kvm_s390_cmma_log *args)
20594036e387SClaudio Imbrenda {
2060afdad616SClaudio Imbrenda 	unsigned long bufsize;
2061afdad616SClaudio Imbrenda 	int srcu_idx, peek, ret;
2062afdad616SClaudio Imbrenda 	u8 *values;
20634036e387SClaudio Imbrenda 
2064afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
20654036e387SClaudio Imbrenda 		return -ENXIO;
20664036e387SClaudio Imbrenda 	/* Invalid/unsupported flags were specified */
20674036e387SClaudio Imbrenda 	if (args->flags & ~KVM_S390_CMMA_PEEK)
20684036e387SClaudio Imbrenda 		return -EINVAL;
20694036e387SClaudio Imbrenda 	/* Migration mode query, and we are not doing a migration */
20704036e387SClaudio Imbrenda 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2071afdad616SClaudio Imbrenda 	if (!peek && !kvm->arch.migration_mode)
20724036e387SClaudio Imbrenda 		return -EINVAL;
20734036e387SClaudio Imbrenda 	/* CMMA is disabled or was not used, or the buffer has length zero */
20744036e387SClaudio Imbrenda 	bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2075c9f0a2b8SJanosch Frank 	if (!bufsize || !kvm->mm->context.uses_cmm) {
20764036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
20774036e387SClaudio Imbrenda 		return 0;
20784036e387SClaudio Imbrenda 	}
20794036e387SClaudio Imbrenda 	/* We are not peeking, and there are no dirty pages */
2080afdad616SClaudio Imbrenda 	if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
20814036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
20824036e387SClaudio Imbrenda 		return 0;
20834036e387SClaudio Imbrenda 	}
20844036e387SClaudio Imbrenda 
2085afdad616SClaudio Imbrenda 	values = vmalloc(bufsize);
2086afdad616SClaudio Imbrenda 	if (!values)
20874036e387SClaudio Imbrenda 		return -ENOMEM;
20884036e387SClaudio Imbrenda 
2089d8ed45c5SMichel Lespinasse 	mmap_read_lock(kvm->mm);
20904036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
2091afdad616SClaudio Imbrenda 	if (peek)
2092afdad616SClaudio Imbrenda 		ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2093afdad616SClaudio Imbrenda 	else
2094afdad616SClaudio Imbrenda 		ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
20954036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2096d8ed45c5SMichel Lespinasse 	mmap_read_unlock(kvm->mm);
20974036e387SClaudio Imbrenda 
2098afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
2099afdad616SClaudio Imbrenda 		args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2100afdad616SClaudio Imbrenda 	else
2101afdad616SClaudio Imbrenda 		args->remaining = 0;
21024036e387SClaudio Imbrenda 
2103afdad616SClaudio Imbrenda 	if (copy_to_user((void __user *)args->values, values, args->count))
2104afdad616SClaudio Imbrenda 		ret = -EFAULT;
2105afdad616SClaudio Imbrenda 
2106afdad616SClaudio Imbrenda 	vfree(values);
2107afdad616SClaudio Imbrenda 	return ret;
21084036e387SClaudio Imbrenda }
21094036e387SClaudio Imbrenda 
21104036e387SClaudio Imbrenda /*
21114036e387SClaudio Imbrenda  * This function sets the CMMA attributes for the given pages. If the input
21124036e387SClaudio Imbrenda  * buffer has zero length, no action is taken, otherwise the attributes are
2113c9f0a2b8SJanosch Frank  * set and the mm->context.uses_cmm flag is set.
21144036e387SClaudio Imbrenda  */
21154036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
21164036e387SClaudio Imbrenda 				  const struct kvm_s390_cmma_log *args)
21174036e387SClaudio Imbrenda {
21184036e387SClaudio Imbrenda 	unsigned long hva, mask, pgstev, i;
21194036e387SClaudio Imbrenda 	uint8_t *bits;
21204036e387SClaudio Imbrenda 	int srcu_idx, r = 0;
21214036e387SClaudio Imbrenda 
21224036e387SClaudio Imbrenda 	mask = args->mask;
21234036e387SClaudio Imbrenda 
21244036e387SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
21254036e387SClaudio Imbrenda 		return -ENXIO;
21264036e387SClaudio Imbrenda 	/* invalid/unsupported flags */
21274036e387SClaudio Imbrenda 	if (args->flags != 0)
21284036e387SClaudio Imbrenda 		return -EINVAL;
21294036e387SClaudio Imbrenda 	/* Enforce sane limit on memory allocation */
21304036e387SClaudio Imbrenda 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
21314036e387SClaudio Imbrenda 		return -EINVAL;
21324036e387SClaudio Imbrenda 	/* Nothing to do */
21334036e387SClaudio Imbrenda 	if (args->count == 0)
21344036e387SClaudio Imbrenda 		return 0;
21354036e387SClaudio Imbrenda 
213642bc47b3SKees Cook 	bits = vmalloc(array_size(sizeof(*bits), args->count));
21374036e387SClaudio Imbrenda 	if (!bits)
21384036e387SClaudio Imbrenda 		return -ENOMEM;
21394036e387SClaudio Imbrenda 
21404036e387SClaudio Imbrenda 	r = copy_from_user(bits, (void __user *)args->values, args->count);
21414036e387SClaudio Imbrenda 	if (r) {
21424036e387SClaudio Imbrenda 		r = -EFAULT;
21434036e387SClaudio Imbrenda 		goto out;
21444036e387SClaudio Imbrenda 	}
21454036e387SClaudio Imbrenda 
2146d8ed45c5SMichel Lespinasse 	mmap_read_lock(kvm->mm);
21474036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
21484036e387SClaudio Imbrenda 	for (i = 0; i < args->count; i++) {
21494036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, args->start_gfn + i);
21504036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
21514036e387SClaudio Imbrenda 			r = -EFAULT;
21524036e387SClaudio Imbrenda 			break;
21534036e387SClaudio Imbrenda 		}
21544036e387SClaudio Imbrenda 
21554036e387SClaudio Imbrenda 		pgstev = bits[i];
21564036e387SClaudio Imbrenda 		pgstev = pgstev << 24;
21571bab1c02SClaudio Imbrenda 		mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
21584036e387SClaudio Imbrenda 		set_pgste_bits(kvm->mm, hva, mask, pgstev);
21594036e387SClaudio Imbrenda 	}
21604036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2161d8ed45c5SMichel Lespinasse 	mmap_read_unlock(kvm->mm);
21624036e387SClaudio Imbrenda 
2163c9f0a2b8SJanosch Frank 	if (!kvm->mm->context.uses_cmm) {
2164d8ed45c5SMichel Lespinasse 		mmap_write_lock(kvm->mm);
2165c9f0a2b8SJanosch Frank 		kvm->mm->context.uses_cmm = 1;
2166d8ed45c5SMichel Lespinasse 		mmap_write_unlock(kvm->mm);
21674036e387SClaudio Imbrenda 	}
21684036e387SClaudio Imbrenda out:
21694036e387SClaudio Imbrenda 	vfree(bits);
21704036e387SClaudio Imbrenda 	return r;
21714036e387SClaudio Imbrenda }
21724036e387SClaudio Imbrenda 
217329b40f10SJanosch Frank static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp)
217429b40f10SJanosch Frank {
217529b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
217629b40f10SJanosch Frank 	u16 rc, rrc;
217729b40f10SJanosch Frank 	int ret = 0;
217829b40f10SJanosch Frank 	int i;
217929b40f10SJanosch Frank 
218029b40f10SJanosch Frank 	/*
218129b40f10SJanosch Frank 	 * We ignore failures and try to destroy as many CPUs as possible.
218229b40f10SJanosch Frank 	 * At the same time we must not free the assigned resources when
218329b40f10SJanosch Frank 	 * this fails, as the ultravisor has still access to that memory.
218429b40f10SJanosch Frank 	 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
218529b40f10SJanosch Frank 	 * behind.
218629b40f10SJanosch Frank 	 * We want to return the first failure rc and rrc, though.
218729b40f10SJanosch Frank 	 */
218829b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
218929b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
219029b40f10SJanosch Frank 		if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) {
219129b40f10SJanosch Frank 			*rcp = rc;
219229b40f10SJanosch Frank 			*rrcp = rrc;
219329b40f10SJanosch Frank 			ret = -EIO;
219429b40f10SJanosch Frank 		}
219529b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
219629b40f10SJanosch Frank 	}
219729b40f10SJanosch Frank 	return ret;
219829b40f10SJanosch Frank }
219929b40f10SJanosch Frank 
220029b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
220129b40f10SJanosch Frank {
220229b40f10SJanosch Frank 	int i, r = 0;
220329b40f10SJanosch Frank 	u16 dummy;
220429b40f10SJanosch Frank 
220529b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
220629b40f10SJanosch Frank 
220729b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
220829b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
220929b40f10SJanosch Frank 		r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
221029b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
221129b40f10SJanosch Frank 		if (r)
221229b40f10SJanosch Frank 			break;
221329b40f10SJanosch Frank 	}
221429b40f10SJanosch Frank 	if (r)
221529b40f10SJanosch Frank 		kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
221629b40f10SJanosch Frank 	return r;
221729b40f10SJanosch Frank }
221829b40f10SJanosch Frank 
221929b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
222029b40f10SJanosch Frank {
222129b40f10SJanosch Frank 	int r = 0;
222229b40f10SJanosch Frank 	u16 dummy;
222329b40f10SJanosch Frank 	void __user *argp = (void __user *)cmd->data;
222429b40f10SJanosch Frank 
222529b40f10SJanosch Frank 	switch (cmd->cmd) {
222629b40f10SJanosch Frank 	case KVM_PV_ENABLE: {
222729b40f10SJanosch Frank 		r = -EINVAL;
222829b40f10SJanosch Frank 		if (kvm_s390_pv_is_protected(kvm))
222929b40f10SJanosch Frank 			break;
223029b40f10SJanosch Frank 
223129b40f10SJanosch Frank 		/*
223229b40f10SJanosch Frank 		 *  FMT 4 SIE needs esca. As we never switch back to bsca from
223329b40f10SJanosch Frank 		 *  esca, we need no cleanup in the error cases below
223429b40f10SJanosch Frank 		 */
223529b40f10SJanosch Frank 		r = sca_switch_to_extended(kvm);
223629b40f10SJanosch Frank 		if (r)
223729b40f10SJanosch Frank 			break;
223829b40f10SJanosch Frank 
2239d8ed45c5SMichel Lespinasse 		mmap_write_lock(current->mm);
2240fa0c5eabSJanosch Frank 		r = gmap_mark_unmergeable();
2241d8ed45c5SMichel Lespinasse 		mmap_write_unlock(current->mm);
2242fa0c5eabSJanosch Frank 		if (r)
2243fa0c5eabSJanosch Frank 			break;
2244fa0c5eabSJanosch Frank 
224529b40f10SJanosch Frank 		r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
224629b40f10SJanosch Frank 		if (r)
224729b40f10SJanosch Frank 			break;
224829b40f10SJanosch Frank 
224929b40f10SJanosch Frank 		r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
225029b40f10SJanosch Frank 		if (r)
225129b40f10SJanosch Frank 			kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
22520890ddeaSChristian Borntraeger 
22530890ddeaSChristian Borntraeger 		/* we need to block service interrupts from now on */
22540890ddeaSChristian Borntraeger 		set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
225529b40f10SJanosch Frank 		break;
225629b40f10SJanosch Frank 	}
225729b40f10SJanosch Frank 	case KVM_PV_DISABLE: {
225829b40f10SJanosch Frank 		r = -EINVAL;
225929b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
226029b40f10SJanosch Frank 			break;
226129b40f10SJanosch Frank 
226229b40f10SJanosch Frank 		r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
226329b40f10SJanosch Frank 		/*
226429b40f10SJanosch Frank 		 * If a CPU could not be destroyed, destroy VM will also fail.
226529b40f10SJanosch Frank 		 * There is no point in trying to destroy it. Instead return
226629b40f10SJanosch Frank 		 * the rc and rrc from the first CPU that failed destroying.
226729b40f10SJanosch Frank 		 */
226829b40f10SJanosch Frank 		if (r)
226929b40f10SJanosch Frank 			break;
227029b40f10SJanosch Frank 		r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc);
22710890ddeaSChristian Borntraeger 
22720890ddeaSChristian Borntraeger 		/* no need to block service interrupts any more */
22730890ddeaSChristian Borntraeger 		clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
227429b40f10SJanosch Frank 		break;
227529b40f10SJanosch Frank 	}
227629b40f10SJanosch Frank 	case KVM_PV_SET_SEC_PARMS: {
227729b40f10SJanosch Frank 		struct kvm_s390_pv_sec_parm parms = {};
227829b40f10SJanosch Frank 		void *hdr;
227929b40f10SJanosch Frank 
228029b40f10SJanosch Frank 		r = -EINVAL;
228129b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
228229b40f10SJanosch Frank 			break;
228329b40f10SJanosch Frank 
228429b40f10SJanosch Frank 		r = -EFAULT;
228529b40f10SJanosch Frank 		if (copy_from_user(&parms, argp, sizeof(parms)))
228629b40f10SJanosch Frank 			break;
228729b40f10SJanosch Frank 
228829b40f10SJanosch Frank 		/* Currently restricted to 8KB */
228929b40f10SJanosch Frank 		r = -EINVAL;
229029b40f10SJanosch Frank 		if (parms.length > PAGE_SIZE * 2)
229129b40f10SJanosch Frank 			break;
229229b40f10SJanosch Frank 
229329b40f10SJanosch Frank 		r = -ENOMEM;
229429b40f10SJanosch Frank 		hdr = vmalloc(parms.length);
229529b40f10SJanosch Frank 		if (!hdr)
229629b40f10SJanosch Frank 			break;
229729b40f10SJanosch Frank 
229829b40f10SJanosch Frank 		r = -EFAULT;
229929b40f10SJanosch Frank 		if (!copy_from_user(hdr, (void __user *)parms.origin,
230029b40f10SJanosch Frank 				    parms.length))
230129b40f10SJanosch Frank 			r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
230229b40f10SJanosch Frank 						      &cmd->rc, &cmd->rrc);
230329b40f10SJanosch Frank 
230429b40f10SJanosch Frank 		vfree(hdr);
230529b40f10SJanosch Frank 		break;
230629b40f10SJanosch Frank 	}
230729b40f10SJanosch Frank 	case KVM_PV_UNPACK: {
230829b40f10SJanosch Frank 		struct kvm_s390_pv_unp unp = {};
230929b40f10SJanosch Frank 
231029b40f10SJanosch Frank 		r = -EINVAL;
23111ed576a2SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
231229b40f10SJanosch Frank 			break;
231329b40f10SJanosch Frank 
231429b40f10SJanosch Frank 		r = -EFAULT;
231529b40f10SJanosch Frank 		if (copy_from_user(&unp, argp, sizeof(unp)))
231629b40f10SJanosch Frank 			break;
231729b40f10SJanosch Frank 
231829b40f10SJanosch Frank 		r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
231929b40f10SJanosch Frank 				       &cmd->rc, &cmd->rrc);
232029b40f10SJanosch Frank 		break;
232129b40f10SJanosch Frank 	}
232229b40f10SJanosch Frank 	case KVM_PV_VERIFY: {
232329b40f10SJanosch Frank 		r = -EINVAL;
232429b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
232529b40f10SJanosch Frank 			break;
232629b40f10SJanosch Frank 
232729b40f10SJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
232829b40f10SJanosch Frank 				  UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
232929b40f10SJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
233029b40f10SJanosch Frank 			     cmd->rrc);
233129b40f10SJanosch Frank 		break;
233229b40f10SJanosch Frank 	}
2333e0d2773dSJanosch Frank 	case KVM_PV_PREP_RESET: {
2334e0d2773dSJanosch Frank 		r = -EINVAL;
2335e0d2773dSJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2336e0d2773dSJanosch Frank 			break;
2337e0d2773dSJanosch Frank 
2338e0d2773dSJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2339e0d2773dSJanosch Frank 				  UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2340e0d2773dSJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2341e0d2773dSJanosch Frank 			     cmd->rc, cmd->rrc);
2342e0d2773dSJanosch Frank 		break;
2343e0d2773dSJanosch Frank 	}
2344e0d2773dSJanosch Frank 	case KVM_PV_UNSHARE_ALL: {
2345e0d2773dSJanosch Frank 		r = -EINVAL;
2346e0d2773dSJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2347e0d2773dSJanosch Frank 			break;
2348e0d2773dSJanosch Frank 
2349e0d2773dSJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2350e0d2773dSJanosch Frank 				  UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2351e0d2773dSJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2352e0d2773dSJanosch Frank 			     cmd->rc, cmd->rrc);
2353e0d2773dSJanosch Frank 		break;
2354e0d2773dSJanosch Frank 	}
235529b40f10SJanosch Frank 	default:
235629b40f10SJanosch Frank 		r = -ENOTTY;
235729b40f10SJanosch Frank 	}
235829b40f10SJanosch Frank 	return r;
235929b40f10SJanosch Frank }
236029b40f10SJanosch Frank 
2361b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
2362b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
2363b0c632dbSHeiko Carstens {
2364b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
2365b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
2366f2061656SDominik Dingel 	struct kvm_device_attr attr;
2367b0c632dbSHeiko Carstens 	int r;
2368b0c632dbSHeiko Carstens 
2369b0c632dbSHeiko Carstens 	switch (ioctl) {
2370ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
2371ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
2372ba5c1e9bSCarsten Otte 
2373ba5c1e9bSCarsten Otte 		r = -EFAULT;
2374ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
2375ba5c1e9bSCarsten Otte 			break;
2376ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
2377ba5c1e9bSCarsten Otte 		break;
2378ba5c1e9bSCarsten Otte 	}
237984223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
238084223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
238184223598SCornelia Huck 
238284223598SCornelia Huck 		r = -EINVAL;
238384223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
238484223598SCornelia Huck 			/* Set up dummy routing. */
238584223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
2386152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
238784223598SCornelia Huck 		}
238884223598SCornelia Huck 		break;
238984223598SCornelia Huck 	}
2390f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
2391f2061656SDominik Dingel 		r = -EFAULT;
2392f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2393f2061656SDominik Dingel 			break;
2394f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
2395f2061656SDominik Dingel 		break;
2396f2061656SDominik Dingel 	}
2397f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
2398f2061656SDominik Dingel 		r = -EFAULT;
2399f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2400f2061656SDominik Dingel 			break;
2401f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
2402f2061656SDominik Dingel 		break;
2403f2061656SDominik Dingel 	}
2404f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
2405f2061656SDominik Dingel 		r = -EFAULT;
2406f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2407f2061656SDominik Dingel 			break;
2408f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
2409f2061656SDominik Dingel 		break;
2410f2061656SDominik Dingel 	}
241130ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
241230ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
241330ee2a98SJason J. Herne 
241430ee2a98SJason J. Herne 		r = -EFAULT;
241530ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
241630ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
241730ee2a98SJason J. Herne 			break;
241830ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
241930ee2a98SJason J. Herne 		break;
242030ee2a98SJason J. Herne 	}
242130ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
242230ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
242330ee2a98SJason J. Herne 
242430ee2a98SJason J. Herne 		r = -EFAULT;
242530ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
242630ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
242730ee2a98SJason J. Herne 			break;
242830ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
242930ee2a98SJason J. Herne 		break;
243030ee2a98SJason J. Herne 	}
24314036e387SClaudio Imbrenda 	case KVM_S390_GET_CMMA_BITS: {
24324036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
24334036e387SClaudio Imbrenda 
24344036e387SClaudio Imbrenda 		r = -EFAULT;
24354036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
24364036e387SClaudio Imbrenda 			break;
24371de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
24384036e387SClaudio Imbrenda 		r = kvm_s390_get_cmma_bits(kvm, &args);
24391de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
24404036e387SClaudio Imbrenda 		if (!r) {
24414036e387SClaudio Imbrenda 			r = copy_to_user(argp, &args, sizeof(args));
24424036e387SClaudio Imbrenda 			if (r)
24434036e387SClaudio Imbrenda 				r = -EFAULT;
24444036e387SClaudio Imbrenda 		}
24454036e387SClaudio Imbrenda 		break;
24464036e387SClaudio Imbrenda 	}
24474036e387SClaudio Imbrenda 	case KVM_S390_SET_CMMA_BITS: {
24484036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
24494036e387SClaudio Imbrenda 
24504036e387SClaudio Imbrenda 		r = -EFAULT;
24514036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
24524036e387SClaudio Imbrenda 			break;
24531de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
24544036e387SClaudio Imbrenda 		r = kvm_s390_set_cmma_bits(kvm, &args);
24551de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
24564036e387SClaudio Imbrenda 		break;
24574036e387SClaudio Imbrenda 	}
245829b40f10SJanosch Frank 	case KVM_S390_PV_COMMAND: {
245929b40f10SJanosch Frank 		struct kvm_pv_cmd args;
246029b40f10SJanosch Frank 
2461fe28c786SJanosch Frank 		/* protvirt means user sigp */
2462fe28c786SJanosch Frank 		kvm->arch.user_cpu_state_ctrl = 1;
246329b40f10SJanosch Frank 		r = 0;
246429b40f10SJanosch Frank 		if (!is_prot_virt_host()) {
246529b40f10SJanosch Frank 			r = -EINVAL;
246629b40f10SJanosch Frank 			break;
246729b40f10SJanosch Frank 		}
246829b40f10SJanosch Frank 		if (copy_from_user(&args, argp, sizeof(args))) {
246929b40f10SJanosch Frank 			r = -EFAULT;
247029b40f10SJanosch Frank 			break;
247129b40f10SJanosch Frank 		}
247229b40f10SJanosch Frank 		if (args.flags) {
247329b40f10SJanosch Frank 			r = -EINVAL;
247429b40f10SJanosch Frank 			break;
247529b40f10SJanosch Frank 		}
247629b40f10SJanosch Frank 		mutex_lock(&kvm->lock);
247729b40f10SJanosch Frank 		r = kvm_s390_handle_pv(kvm, &args);
247829b40f10SJanosch Frank 		mutex_unlock(&kvm->lock);
247929b40f10SJanosch Frank 		if (copy_to_user(argp, &args, sizeof(args))) {
248029b40f10SJanosch Frank 			r = -EFAULT;
248129b40f10SJanosch Frank 			break;
248229b40f10SJanosch Frank 		}
248329b40f10SJanosch Frank 		break;
248429b40f10SJanosch Frank 	}
2485b0c632dbSHeiko Carstens 	default:
2486367e1319SAvi Kivity 		r = -ENOTTY;
2487b0c632dbSHeiko Carstens 	}
2488b0c632dbSHeiko Carstens 
2489b0c632dbSHeiko Carstens 	return r;
2490b0c632dbSHeiko Carstens }
2491b0c632dbSHeiko Carstens 
249245c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
249345c9b47cSTony Krowiak {
2494e585b24aSTony Krowiak 	struct ap_config_info info;
249545c9b47cSTony Krowiak 
2496e585b24aSTony Krowiak 	if (ap_instructions_available()) {
2497e585b24aSTony Krowiak 		if (ap_qci(&info) == 0)
2498e585b24aSTony Krowiak 			return info.apxa;
249945c9b47cSTony Krowiak 	}
250045c9b47cSTony Krowiak 
250145c9b47cSTony Krowiak 	return 0;
250245c9b47cSTony Krowiak }
250345c9b47cSTony Krowiak 
2504e585b24aSTony Krowiak /*
2505e585b24aSTony Krowiak  * The format of the crypto control block (CRYCB) is specified in the 3 low
2506e585b24aSTony Krowiak  * order bits of the CRYCB designation (CRYCBD) field as follows:
2507e585b24aSTony Krowiak  * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2508e585b24aSTony Krowiak  *	     AP extended addressing (APXA) facility are installed.
2509e585b24aSTony Krowiak  * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2510e585b24aSTony Krowiak  * Format 2: Both the APXA and MSAX3 facilities are installed
2511e585b24aSTony Krowiak  */
251245c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
251345c9b47cSTony Krowiak {
251445c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
251545c9b47cSTony Krowiak 
2516e585b24aSTony Krowiak 	/* Clear the CRYCB format bits - i.e., set format 0 by default */
2517e585b24aSTony Krowiak 	kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2518e585b24aSTony Krowiak 
2519e585b24aSTony Krowiak 	/* Check whether MSAX3 is installed */
2520e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
2521e585b24aSTony Krowiak 		return;
2522e585b24aSTony Krowiak 
252345c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
252445c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
252545c9b47cSTony Krowiak 	else
252645c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
252745c9b47cSTony Krowiak }
252845c9b47cSTony Krowiak 
25290e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
25300e237e44SPierre Morel 			       unsigned long *aqm, unsigned long *adm)
25310e237e44SPierre Morel {
25320e237e44SPierre Morel 	struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
25330e237e44SPierre Morel 
25340e237e44SPierre Morel 	mutex_lock(&kvm->lock);
25350e237e44SPierre Morel 	kvm_s390_vcpu_block_all(kvm);
25360e237e44SPierre Morel 
25370e237e44SPierre Morel 	switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
25380e237e44SPierre Morel 	case CRYCB_FORMAT2: /* APCB1 use 256 bits */
25390e237e44SPierre Morel 		memcpy(crycb->apcb1.apm, apm, 32);
25400e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
25410e237e44SPierre Morel 			 apm[0], apm[1], apm[2], apm[3]);
25420e237e44SPierre Morel 		memcpy(crycb->apcb1.aqm, aqm, 32);
25430e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
25440e237e44SPierre Morel 			 aqm[0], aqm[1], aqm[2], aqm[3]);
25450e237e44SPierre Morel 		memcpy(crycb->apcb1.adm, adm, 32);
25460e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
25470e237e44SPierre Morel 			 adm[0], adm[1], adm[2], adm[3]);
25480e237e44SPierre Morel 		break;
25490e237e44SPierre Morel 	case CRYCB_FORMAT1:
25500e237e44SPierre Morel 	case CRYCB_FORMAT0: /* Fall through both use APCB0 */
25510e237e44SPierre Morel 		memcpy(crycb->apcb0.apm, apm, 8);
25520e237e44SPierre Morel 		memcpy(crycb->apcb0.aqm, aqm, 2);
25530e237e44SPierre Morel 		memcpy(crycb->apcb0.adm, adm, 2);
25540e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
25550e237e44SPierre Morel 			 apm[0], *((unsigned short *)aqm),
25560e237e44SPierre Morel 			 *((unsigned short *)adm));
25570e237e44SPierre Morel 		break;
25580e237e44SPierre Morel 	default:	/* Can not happen */
25590e237e44SPierre Morel 		break;
25600e237e44SPierre Morel 	}
25610e237e44SPierre Morel 
25620e237e44SPierre Morel 	/* recreate the shadow crycb for each vcpu */
25630e237e44SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
25640e237e44SPierre Morel 	kvm_s390_vcpu_unblock_all(kvm);
25650e237e44SPierre Morel 	mutex_unlock(&kvm->lock);
25660e237e44SPierre Morel }
25670e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
25680e237e44SPierre Morel 
256942104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm)
257042104598STony Krowiak {
257142104598STony Krowiak 	mutex_lock(&kvm->lock);
257242104598STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
257342104598STony Krowiak 
257442104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb0, 0,
257542104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb0));
257642104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb1, 0,
257742104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb1));
257842104598STony Krowiak 
25790e237e44SPierre Morel 	VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
25806cc571b1SPierre Morel 	/* recreate the shadow crycb for each vcpu */
25816cc571b1SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
258242104598STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
258342104598STony Krowiak 	mutex_unlock(&kvm->lock);
258442104598STony Krowiak }
258542104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
258642104598STony Krowiak 
25879bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
25889d8d5786SMichael Mueller {
25899bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
25909bb0ec09SDavid Hildenbrand 
25919bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
25929bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
25939bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
25949d8d5786SMichael Mueller }
25959d8d5786SMichael Mueller 
2596c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
25975102ee87STony Krowiak {
2598c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
259945c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
26005102ee87STony Krowiak 
2601e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
2602e585b24aSTony Krowiak 		return;
2603e585b24aSTony Krowiak 
2604ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
2605ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
2606ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
2607ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
2608ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
2609ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
2610ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
26115102ee87STony Krowiak }
26125102ee87STony Krowiak 
26137d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
26147d43bafcSEugene (jno) Dvurechenski {
26157d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
26165e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
26177d43bafcSEugene (jno) Dvurechenski 	else
26187d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
26197d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
26207d43bafcSEugene (jno) Dvurechenski }
26217d43bafcSEugene (jno) Dvurechenski 
2622e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
2623b0c632dbSHeiko Carstens {
2624c4196218SChristian Borntraeger 	gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
26259d8d5786SMichael Mueller 	int i, rc;
2626b0c632dbSHeiko Carstens 	char debug_name[16];
2627f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
2628b0c632dbSHeiko Carstens 
2629e08b9637SCarsten Otte 	rc = -EINVAL;
2630e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
2631e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
2632e08b9637SCarsten Otte 		goto out_err;
2633e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
2634e08b9637SCarsten Otte 		goto out_err;
2635e08b9637SCarsten Otte #else
2636e08b9637SCarsten Otte 	if (type)
2637e08b9637SCarsten Otte 		goto out_err;
2638e08b9637SCarsten Otte #endif
2639e08b9637SCarsten Otte 
2640b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
2641b0c632dbSHeiko Carstens 	if (rc)
2642d89f5effSJan Kiszka 		goto out_err;
2643b0c632dbSHeiko Carstens 
2644b290411aSCarsten Otte 	rc = -ENOMEM;
2645b290411aSCarsten Otte 
264676a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
264776a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
26485e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
26499ac96d75SDavid Hildenbrand 	/* start with basic SCA */
265076a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
2651b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
2652d89f5effSJan Kiszka 		goto out_err;
26530d9ce162SJunaid Shahid 	mutex_lock(&kvm_lock);
2654c5c2c393SDavid Hildenbrand 	sca_offset += 16;
2655bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2656c5c2c393SDavid Hildenbrand 		sca_offset = 0;
2657bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
2658bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
26590d9ce162SJunaid Shahid 	mutex_unlock(&kvm_lock);
2660b0c632dbSHeiko Carstens 
2661b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
2662b0c632dbSHeiko Carstens 
26631cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2664b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
266540f5b735SDominik Dingel 		goto out_err;
2666b0c632dbSHeiko Carstens 
266719114bebSMichael Mueller 	BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
2668c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
2669c4196218SChristian Borntraeger 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
2670c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
267140f5b735SDominik Dingel 		goto out_err;
26729d8d5786SMichael Mueller 
267325c84dbaSMichael Mueller 	kvm->arch.sie_page2->kvm = kvm;
2674c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2675c3b9e3e1SChristian Borntraeger 
2676c3b9e3e1SChristian Borntraeger 	for (i = 0; i < kvm_s390_fac_size(); i++) {
2677c3b9e3e1SChristian Borntraeger 		kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] &
2678c3b9e3e1SChristian Borntraeger 					      (kvm_s390_fac_base[i] |
2679c3b9e3e1SChristian Borntraeger 					       kvm_s390_fac_ext[i]);
2680c3b9e3e1SChristian Borntraeger 		kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] &
2681c3b9e3e1SChristian Borntraeger 					      kvm_s390_fac_base[i];
2682c3b9e3e1SChristian Borntraeger 	}
2683346fa2f8SChristian Borntraeger 	kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
2684981467c9SMichael Mueller 
26851935222dSDavid Hildenbrand 	/* we are always in czam mode - even on pre z14 machines */
26861935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
26871935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_list, 138);
26881935222dSDavid Hildenbrand 	/* we emulate STHYI in kvm */
268995ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
269095ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
26911bab1c02SClaudio Imbrenda 	if (MACHINE_HAS_TLB_GUEST) {
26921bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
26931bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_list, 147);
26941bab1c02SClaudio Imbrenda 	}
269595ca2cb5SJanosch Frank 
269605f31e3bSPierre Morel 	if (css_general_characteristics.aiv && test_facility(65))
269705f31e3bSPierre Morel 		set_kvm_facility(kvm->arch.model.fac_mask, 65);
269805f31e3bSPierre Morel 
26999bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
270037c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
27019d8d5786SMichael Mueller 
2702c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
27035102ee87STony Krowiak 
270451978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
2705ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
27066d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
27076d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
27088a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
2709a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
2710ba5c1e9bSCarsten Otte 
2711b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
271278f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
2713b0c632dbSHeiko Carstens 
2714e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
2715e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
2716a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2717e08b9637SCarsten Otte 	} else {
271832e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
2719ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
272032e6b236SGuenther Hutzl 		else
2721ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
272232e6b236SGuenther Hutzl 						    sclp.hamax + 1);
27236ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2724598841caSCarsten Otte 		if (!kvm->arch.gmap)
272540f5b735SDominik Dingel 			goto out_err;
27262c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
272724eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
2728e08b9637SCarsten Otte 	}
2729fa6b7fe9SCornelia Huck 
2730c9f0a2b8SJanosch Frank 	kvm->arch.use_pfmfi = sclp.has_pfmfi;
273155531b74SJanosch Frank 	kvm->arch.use_skf = sclp.has_skey;
27328ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
2733a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
2734cc674ef2SMichael Mueller 	if (use_gisa)
2735d7c5cb01SMichael Mueller 		kvm_s390_gisa_init(kvm);
27368335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
27378ad35755SDavid Hildenbrand 
2738d89f5effSJan Kiszka 	return 0;
2739d89f5effSJan Kiszka out_err:
2740c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
274140f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
27427d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
274378f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
2744d89f5effSJan Kiszka 	return rc;
2745b0c632dbSHeiko Carstens }
2746b0c632dbSHeiko Carstens 
2747d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2748d329c035SChristian Borntraeger {
274929b40f10SJanosch Frank 	u16 rc, rrc;
275029b40f10SJanosch Frank 
2751d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2752ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
275367335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
27543c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
2755bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
2756a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
275727e0393fSCarsten Otte 
275827e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
27596ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
276027e0393fSCarsten Otte 
2761e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
2762b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
276329b40f10SJanosch Frank 	/* We can not hold the vcpu mutex here, we are already dying */
276429b40f10SJanosch Frank 	if (kvm_s390_pv_cpu_get_handle(vcpu))
276529b40f10SJanosch Frank 		kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
2766d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2767d329c035SChristian Borntraeger }
2768d329c035SChristian Borntraeger 
2769d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
2770d329c035SChristian Borntraeger {
2771d329c035SChristian Borntraeger 	unsigned int i;
2772988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
2773d329c035SChristian Borntraeger 
2774988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
27754543bdc0SSean Christopherson 		kvm_vcpu_destroy(vcpu);
2776988a2caeSGleb Natapov 
2777988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
2778988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2779d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
2780988a2caeSGleb Natapov 
2781988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
2782988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
2783d329c035SChristian Borntraeger }
2784d329c035SChristian Borntraeger 
2785b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
2786b0c632dbSHeiko Carstens {
278729b40f10SJanosch Frank 	u16 rc, rrc;
278829b40f10SJanosch Frank 
2789d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
27907d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
2791d7c5cb01SMichael Mueller 	kvm_s390_gisa_destroy(kvm);
279229b40f10SJanosch Frank 	/*
279329b40f10SJanosch Frank 	 * We are already at the end of life and kvm->lock is not taken.
279429b40f10SJanosch Frank 	 * This is ok as the file descriptor is closed by now and nobody
279529b40f10SJanosch Frank 	 * can mess with the pv state. To avoid lockdep_assert_held from
279629b40f10SJanosch Frank 	 * complaining we do not use kvm_s390_pv_is_protected.
279729b40f10SJanosch Frank 	 */
279829b40f10SJanosch Frank 	if (kvm_s390_pv_get_handle(kvm))
279929b40f10SJanosch Frank 		kvm_s390_pv_deinit_vm(kvm, &rc, &rrc);
280029b40f10SJanosch Frank 	debug_unregister(kvm->arch.dbf);
2801c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
280227e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
28036ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
2804841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
280567335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
2806a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
28078335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2808b0c632dbSHeiko Carstens }
2809b0c632dbSHeiko Carstens 
2810b0c632dbSHeiko Carstens /* Section: vcpu related */
2811dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2812b0c632dbSHeiko Carstens {
28136ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
281427e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
281527e0393fSCarsten Otte 		return -ENOMEM;
28162c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
2817dafd032aSDominik Dingel 
281827e0393fSCarsten Otte 	return 0;
281927e0393fSCarsten Otte }
282027e0393fSCarsten Otte 
2821a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2822a6e2f683SEugene (jno) Dvurechenski {
2823a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
2824a6940674SDavid Hildenbrand 		return;
28255e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
28267d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
28277d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
28287d43bafcSEugene (jno) Dvurechenski 
28297d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
28307d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
28317d43bafcSEugene (jno) Dvurechenski 	} else {
2832bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2833a6e2f683SEugene (jno) Dvurechenski 
2834a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2835a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
2836a6e2f683SEugene (jno) Dvurechenski 	}
28375e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
28387d43bafcSEugene (jno) Dvurechenski }
2839a6e2f683SEugene (jno) Dvurechenski 
2840eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2841a6e2f683SEugene (jno) Dvurechenski {
2842a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2843a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2844a6940674SDavid Hildenbrand 
2845a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
2846a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2847a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2848f07afa04SDavid Hildenbrand 		return;
2849a6940674SDavid Hildenbrand 	}
2850eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
2851eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
2852eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
28537d43bafcSEugene (jno) Dvurechenski 
2854eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
28557d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
28567d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
28570c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2858eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
28597d43bafcSEugene (jno) Dvurechenski 	} else {
2860eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2861a6e2f683SEugene (jno) Dvurechenski 
2862eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2863a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2864a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2865eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2866a6e2f683SEugene (jno) Dvurechenski 	}
2867eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
28685e044315SEugene (jno) Dvurechenski }
28695e044315SEugene (jno) Dvurechenski 
28705e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
28715e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
28725e044315SEugene (jno) Dvurechenski {
28735e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
28745e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
28755e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
28765e044315SEugene (jno) Dvurechenski }
28775e044315SEugene (jno) Dvurechenski 
28785e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
28795e044315SEugene (jno) Dvurechenski {
28805e044315SEugene (jno) Dvurechenski 	int i;
28815e044315SEugene (jno) Dvurechenski 
28825e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
28835e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
28845e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
28855e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
28865e044315SEugene (jno) Dvurechenski }
28875e044315SEugene (jno) Dvurechenski 
28885e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
28895e044315SEugene (jno) Dvurechenski {
28905e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
28915e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
28925e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
28935e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
28945e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
28955e044315SEugene (jno) Dvurechenski 
289629b40f10SJanosch Frank 	if (kvm->arch.use_esca)
289729b40f10SJanosch Frank 		return 0;
289829b40f10SJanosch Frank 
2899c4196218SChristian Borntraeger 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
29005e044315SEugene (jno) Dvurechenski 	if (!new_sca)
29015e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
29025e044315SEugene (jno) Dvurechenski 
29035e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
29045e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
29055e044315SEugene (jno) Dvurechenski 
29065e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
29075e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
29085e044315SEugene (jno) Dvurechenski 
29095e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
29105e044315SEugene (jno) Dvurechenski 
29115e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
29125e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
29135e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
29140c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
29155e044315SEugene (jno) Dvurechenski 	}
29165e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
29175e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
29185e044315SEugene (jno) Dvurechenski 
29195e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
29205e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
29215e044315SEugene (jno) Dvurechenski 
29225e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
29235e044315SEugene (jno) Dvurechenski 
29248335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
29258335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
29265e044315SEugene (jno) Dvurechenski 	return 0;
29277d43bafcSEugene (jno) Dvurechenski }
2928a6e2f683SEugene (jno) Dvurechenski 
2929a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2930a6e2f683SEugene (jno) Dvurechenski {
29315e044315SEugene (jno) Dvurechenski 	int rc;
29325e044315SEugene (jno) Dvurechenski 
2933a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2934a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
2935a6940674SDavid Hildenbrand 			return true;
2936a6940674SDavid Hildenbrand 		return false;
2937a6940674SDavid Hildenbrand 	}
29385e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
29395e044315SEugene (jno) Dvurechenski 		return true;
294076a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
29415e044315SEugene (jno) Dvurechenski 		return false;
29425e044315SEugene (jno) Dvurechenski 
29435e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
29445e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
29455e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
29465e044315SEugene (jno) Dvurechenski 
29475e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2948a6e2f683SEugene (jno) Dvurechenski }
2949a6e2f683SEugene (jno) Dvurechenski 
2950db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2951db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2952db0758b2SDavid Hildenbrand {
2953db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
29549c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2955db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
29569c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2957db0758b2SDavid Hildenbrand }
2958db0758b2SDavid Hildenbrand 
2959db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2960db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2961db0758b2SDavid Hildenbrand {
2962db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
29639c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2964db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2965db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
29669c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2967db0758b2SDavid Hildenbrand }
2968db0758b2SDavid Hildenbrand 
2969db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2970db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2971db0758b2SDavid Hildenbrand {
2972db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2973db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
2974db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
2975db0758b2SDavid Hildenbrand }
2976db0758b2SDavid Hildenbrand 
2977db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2978db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2979db0758b2SDavid Hildenbrand {
2980db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2981db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
2982db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
2983db0758b2SDavid Hildenbrand }
2984db0758b2SDavid Hildenbrand 
2985db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2986db0758b2SDavid Hildenbrand {
2987db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2988db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
2989db0758b2SDavid Hildenbrand 	preempt_enable();
2990db0758b2SDavid Hildenbrand }
2991db0758b2SDavid Hildenbrand 
2992db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2993db0758b2SDavid Hildenbrand {
2994db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2995db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
2996db0758b2SDavid Hildenbrand 	preempt_enable();
2997db0758b2SDavid Hildenbrand }
2998db0758b2SDavid Hildenbrand 
29994287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
30004287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
30014287f247SDavid Hildenbrand {
3002db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
30039c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3004db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
3005db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
30064287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
30079c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3008db0758b2SDavid Hildenbrand 	preempt_enable();
30094287f247SDavid Hildenbrand }
30104287f247SDavid Hildenbrand 
3011db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
30124287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
30134287f247SDavid Hildenbrand {
30149c23a131SDavid Hildenbrand 	unsigned int seq;
3015db0758b2SDavid Hildenbrand 	__u64 value;
3016db0758b2SDavid Hildenbrand 
3017db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
30184287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
3019db0758b2SDavid Hildenbrand 
30209c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
30219c23a131SDavid Hildenbrand 	do {
30229c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
30239c23a131SDavid Hildenbrand 		/*
30249c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
30259c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
30269c23a131SDavid Hildenbrand 		 */
30279c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3028db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
30299c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
30309c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
3031db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
30329c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
30339c23a131SDavid Hildenbrand 	preempt_enable();
3034db0758b2SDavid Hildenbrand 	return value;
30354287f247SDavid Hildenbrand }
30364287f247SDavid Hildenbrand 
3037b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3038b0c632dbSHeiko Carstens {
30399977e886SHendrik Brueckner 
304037d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
3041ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
30425ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3043db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
304401a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
3045b0c632dbSHeiko Carstens }
3046b0c632dbSHeiko Carstens 
3047b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3048b0c632dbSHeiko Carstens {
304901a745acSDavid Hildenbrand 	vcpu->cpu = -1;
30505ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3051db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
30529daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
305337d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
305437d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
30559977e886SHendrik Brueckner 
3056b0c632dbSHeiko Carstens }
3057b0c632dbSHeiko Carstens 
305831928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
305942897d86SMarcelo Tosatti {
306072f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
3061fdf03650SFan Zhang 	preempt_disable();
306272f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3063d16b52cbSDavid Hildenbrand 	vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3064fdf03650SFan Zhang 	preempt_enable();
306572f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
306625508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
3067dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3068eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
306925508824SDavid Hildenbrand 	}
30706502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
30716502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
307237d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
307337d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
307442897d86SMarcelo Tosatti }
307542897d86SMarcelo Tosatti 
30768ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
30778ec2fa52SChristian Borntraeger {
30788ec2fa52SChristian Borntraeger 	if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
30798ec2fa52SChristian Borntraeger 	    test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
30808ec2fa52SChristian Borntraeger 		return true;
30818ec2fa52SChristian Borntraeger 	return false;
30828ec2fa52SChristian Borntraeger }
30838ec2fa52SChristian Borntraeger 
30848ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm)
30858ec2fa52SChristian Borntraeger {
30868ec2fa52SChristian Borntraeger 	/* At least one ECC subfunction must be present */
30878ec2fa52SChristian Borntraeger 	return kvm_has_pckmo_subfunc(kvm, 32) ||
30888ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 33) ||
30898ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 34) ||
30908ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 40) ||
30918ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 41);
30928ec2fa52SChristian Borntraeger 
30938ec2fa52SChristian Borntraeger }
30948ec2fa52SChristian Borntraeger 
30955102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
30965102ee87STony Krowiak {
3097e585b24aSTony Krowiak 	/*
3098e585b24aSTony Krowiak 	 * If the AP instructions are not being interpreted and the MSAX3
3099e585b24aSTony Krowiak 	 * facility is not configured for the guest, there is nothing to set up.
3100e585b24aSTony Krowiak 	 */
3101e585b24aSTony Krowiak 	if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
31025102ee87STony Krowiak 		return;
31035102ee87STony Krowiak 
3104e585b24aSTony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3105a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
310637940fb0STony Krowiak 	vcpu->arch.sie_block->eca &= ~ECA_APIE;
31078ec2fa52SChristian Borntraeger 	vcpu->arch.sie_block->ecd &= ~ECD_ECC;
3108a374e892STony Krowiak 
3109e585b24aSTony Krowiak 	if (vcpu->kvm->arch.crypto.apie)
3110e585b24aSTony Krowiak 		vcpu->arch.sie_block->eca |= ECA_APIE;
3111e585b24aSTony Krowiak 
3112e585b24aSTony Krowiak 	/* Set up protected key support */
31138ec2fa52SChristian Borntraeger 	if (vcpu->kvm->arch.crypto.aes_kw) {
3114a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
31158ec2fa52SChristian Borntraeger 		/* ecc is also wrapped with AES key */
31168ec2fa52SChristian Borntraeger 		if (kvm_has_pckmo_ecc(vcpu->kvm))
31178ec2fa52SChristian Borntraeger 			vcpu->arch.sie_block->ecd |= ECD_ECC;
31188ec2fa52SChristian Borntraeger 	}
31198ec2fa52SChristian Borntraeger 
3120a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
3121a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
31225102ee87STony Krowiak }
31235102ee87STony Krowiak 
3124b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3125b31605c1SDominik Dingel {
3126b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
3127b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
3128b31605c1SDominik Dingel }
3129b31605c1SDominik Dingel 
3130b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3131b31605c1SDominik Dingel {
3132c4196218SChristian Borntraeger 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL_ACCOUNT);
3133b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
3134b31605c1SDominik Dingel 		return -ENOMEM;
3135b31605c1SDominik Dingel 	return 0;
3136b31605c1SDominik Dingel }
3137b31605c1SDominik Dingel 
313891520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
313991520f1aSMichael Mueller {
314091520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
314191520f1aSMichael Mueller 
314291520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
314380bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
3144c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
314591520f1aSMichael Mueller }
314691520f1aSMichael Mueller 
3147ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3148ff72bb55SSean Christopherson {
3149b31605c1SDominik Dingel 	int rc = 0;
315029b40f10SJanosch Frank 	u16 uvrc, uvrrc;
3151b31288faSKonstantin Weitz 
31529e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
31539e6dabefSCornelia Huck 						    CPUSTAT_SM |
3154a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
3155a4a4f191SGuenther Hutzl 
315653df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
3157ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
315853df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
3159ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3160a4a4f191SGuenther Hutzl 
316191520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
316291520f1aSMichael Mueller 
3163bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3164bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
31650c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3166bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
31670c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
3168f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
31690c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
31707feb6bb8SMichael Mueller 
3171c9f0a2b8SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
31720c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3173cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
31740c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
31750c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
317648ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
31770c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
317811ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
31790c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
318037c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
31810c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
318237c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
31830c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
318418280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
31850c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
31860c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
318713211ea7SEric Farman 	}
31888fa1696eSCollin L. Walling 	if (test_kvm_facility(vcpu->kvm, 139))
31898fa1696eSCollin L. Walling 		vcpu->arch.sie_block->ecd |= ECD_MEF;
3190a3da7b4aSChristian Borntraeger 	if (test_kvm_facility(vcpu->kvm, 156))
3191a3da7b4aSChristian Borntraeger 		vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3192d7c5cb01SMichael Mueller 	if (vcpu->arch.sie_block->gd) {
3193d7c5cb01SMichael Mueller 		vcpu->arch.sie_block->eca |= ECA_AIV;
3194d7c5cb01SMichael Mueller 		VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3195d7c5cb01SMichael Mueller 			   vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3196d7c5cb01SMichael Mueller 	}
31974e0b1ab7SFan Zhang 	vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
31984e0b1ab7SFan Zhang 					| SDNXC;
3199c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
3200730cd632SFarhan Ali 
3201730cd632SFarhan Ali 	if (sclp.has_kss)
3202ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3203730cd632SFarhan Ali 	else
3204492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
32055a5e6536SMatthew Rosato 
3206e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
3207b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
3208b31605c1SDominik Dingel 		if (rc)
3209b31605c1SDominik Dingel 			return rc;
3210b31288faSKonstantin Weitz 	}
32110ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3212ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
32139d8d5786SMichael Mueller 
321467d49d52SCollin Walling 	vcpu->arch.sie_block->hpid = HPID_KVM;
321567d49d52SCollin Walling 
32165102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
32175102ee87STony Krowiak 
321829b40f10SJanosch Frank 	mutex_lock(&vcpu->kvm->lock);
321929b40f10SJanosch Frank 	if (kvm_s390_pv_is_protected(vcpu->kvm)) {
322029b40f10SJanosch Frank 		rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
322129b40f10SJanosch Frank 		if (rc)
322229b40f10SJanosch Frank 			kvm_s390_vcpu_unsetup_cmma(vcpu);
322329b40f10SJanosch Frank 	}
322429b40f10SJanosch Frank 	mutex_unlock(&vcpu->kvm->lock);
322529b40f10SJanosch Frank 
3226b31605c1SDominik Dingel 	return rc;
3227b0c632dbSHeiko Carstens }
3228b0c632dbSHeiko Carstens 
3229897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3230897cc38eSSean Christopherson {
3231897cc38eSSean Christopherson 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3232897cc38eSSean Christopherson 		return -EINVAL;
3233897cc38eSSean Christopherson 	return 0;
3234897cc38eSSean Christopherson }
3235897cc38eSSean Christopherson 
3236e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3237b0c632dbSHeiko Carstens {
32387feb6bb8SMichael Mueller 	struct sie_page *sie_page;
3239897cc38eSSean Christopherson 	int rc;
32404d47555aSCarsten Otte 
3241da72ca4dSQingFeng Hao 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
3242c4196218SChristian Borntraeger 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
32437feb6bb8SMichael Mueller 	if (!sie_page)
3244e529ef66SSean Christopherson 		return -ENOMEM;
3245b0c632dbSHeiko Carstens 
32467feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
32477feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
32487feb6bb8SMichael Mueller 
3249efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
3250efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
3251efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
3252efed1104SDavid Hildenbrand 
3253e529ef66SSean Christopherson 	vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3254ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
3255e529ef66SSean Christopherson 	vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin;
32564b9f9525SMichael Mueller 	if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
32574b9f9525SMichael Mueller 		vcpu->arch.sie_block->gd |= GISA_FORMAT1;
32589c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
3259ba5c1e9bSCarsten Otte 
3260321f8ee5SSean Christopherson 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3261321f8ee5SSean Christopherson 	kvm_clear_async_pf_completion_queue(vcpu);
3262321f8ee5SSean Christopherson 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3263321f8ee5SSean Christopherson 				    KVM_SYNC_GPRS |
3264321f8ee5SSean Christopherson 				    KVM_SYNC_ACRS |
3265321f8ee5SSean Christopherson 				    KVM_SYNC_CRS |
3266321f8ee5SSean Christopherson 				    KVM_SYNC_ARCH0 |
326723a60f83SCollin Walling 				    KVM_SYNC_PFAULT |
326823a60f83SCollin Walling 				    KVM_SYNC_DIAG318;
3269321f8ee5SSean Christopherson 	kvm_s390_set_prefix(vcpu, 0);
3270321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 64))
3271321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3272321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 82))
3273321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3274321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 133))
3275321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3276321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 156))
3277321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3278321f8ee5SSean Christopherson 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
3279321f8ee5SSean Christopherson 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3280321f8ee5SSean Christopherson 	 */
3281321f8ee5SSean Christopherson 	if (MACHINE_HAS_VX)
3282321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3283321f8ee5SSean Christopherson 	else
3284321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3285321f8ee5SSean Christopherson 
3286321f8ee5SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm)) {
3287321f8ee5SSean Christopherson 		rc = __kvm_ucontrol_vcpu_init(vcpu);
3288321f8ee5SSean Christopherson 		if (rc)
3289a2017f17SSean Christopherson 			goto out_free_sie_block;
3290321f8ee5SSean Christopherson 	}
3291321f8ee5SSean Christopherson 
3292e529ef66SSean Christopherson 	VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3293e529ef66SSean Christopherson 		 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3294e529ef66SSean Christopherson 	trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3295b0c632dbSHeiko Carstens 
3296ff72bb55SSean Christopherson 	rc = kvm_s390_vcpu_setup(vcpu);
3297ff72bb55SSean Christopherson 	if (rc)
3298ff72bb55SSean Christopherson 		goto out_ucontrol_uninit;
3299e529ef66SSean Christopherson 	return 0;
3300e529ef66SSean Christopherson 
3301ff72bb55SSean Christopherson out_ucontrol_uninit:
3302ff72bb55SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm))
3303ff72bb55SSean Christopherson 		gmap_remove(vcpu->arch.gmap);
33047b06bf2fSWei Yongjun out_free_sie_block:
33057b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
3306e529ef66SSean Christopherson 	return rc;
3307b0c632dbSHeiko Carstens }
3308b0c632dbSHeiko Carstens 
3309b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3310b0c632dbSHeiko Carstens {
33119a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
3312b0c632dbSHeiko Carstens }
3313b0c632dbSHeiko Carstens 
3314199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
3315199b5763SLongpeng(Mike) {
33160546c63dSLongpeng(Mike) 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3317199b5763SLongpeng(Mike) }
3318199b5763SLongpeng(Mike) 
331927406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
332049b99e1eSChristian Borntraeger {
3321805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
332261a6df54SDavid Hildenbrand 	exit_sie(vcpu);
332349b99e1eSChristian Borntraeger }
332449b99e1eSChristian Borntraeger 
332527406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
332649b99e1eSChristian Borntraeger {
3327805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
332849b99e1eSChristian Borntraeger }
332949b99e1eSChristian Borntraeger 
33308e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
33318e236546SChristian Borntraeger {
3332805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
333361a6df54SDavid Hildenbrand 	exit_sie(vcpu);
33348e236546SChristian Borntraeger }
33358e236546SChristian Borntraeger 
33369ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
33379ea59728SDavid Hildenbrand {
33389ea59728SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->prog20) &
33399ea59728SDavid Hildenbrand 	       (PROG_BLOCK_SIE | PROG_REQUEST);
33409ea59728SDavid Hildenbrand }
33419ea59728SDavid Hildenbrand 
33428e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
33438e236546SChristian Borntraeger {
33449bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
33458e236546SChristian Borntraeger }
33468e236546SChristian Borntraeger 
334749b99e1eSChristian Borntraeger /*
33489ea59728SDavid Hildenbrand  * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
334949b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
335049b99e1eSChristian Borntraeger  * return immediately. */
335149b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
335249b99e1eSChristian Borntraeger {
3353ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
33549ea59728SDavid Hildenbrand 	kvm_s390_vsie_kick(vcpu);
335549b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
335649b99e1eSChristian Borntraeger 		cpu_relax();
335749b99e1eSChristian Borntraeger }
335849b99e1eSChristian Borntraeger 
33598e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
33608e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
336149b99e1eSChristian Borntraeger {
33628e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
33638e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
336449b99e1eSChristian Borntraeger }
336549b99e1eSChristian Borntraeger 
3366414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
3367414d3b07SMartin Schwidefsky 			      unsigned long end)
33682c70fe44SChristian Borntraeger {
33692c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
33702c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
3371414d3b07SMartin Schwidefsky 	unsigned long prefix;
3372414d3b07SMartin Schwidefsky 	int i;
33732c70fe44SChristian Borntraeger 
337465d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
337565d0b0d4SDavid Hildenbrand 		return;
3376414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
3377414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
3378414d3b07SMartin Schwidefsky 		return;
33792c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
33802c70fe44SChristian Borntraeger 		/* match against both prefix pages */
3381414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
3382414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3383414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3384414d3b07SMartin Schwidefsky 				   start, end);
33858e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
33862c70fe44SChristian Borntraeger 		}
33872c70fe44SChristian Borntraeger 	}
33882c70fe44SChristian Borntraeger }
33892c70fe44SChristian Borntraeger 
33908b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
33918b905d28SChristian Borntraeger {
33928b905d28SChristian Borntraeger 	/* do not poll with more than halt_poll_max_steal percent of steal time */
33938b905d28SChristian Borntraeger 	if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
33948b905d28SChristian Borntraeger 	    halt_poll_max_steal) {
33958b905d28SChristian Borntraeger 		vcpu->stat.halt_no_poll_steal++;
33968b905d28SChristian Borntraeger 		return true;
33978b905d28SChristian Borntraeger 	}
33988b905d28SChristian Borntraeger 	return false;
33998b905d28SChristian Borntraeger }
34008b905d28SChristian Borntraeger 
3401b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3402b6d33834SChristoffer Dall {
3403b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
3404b6d33834SChristoffer Dall 	BUG();
3405b6d33834SChristoffer Dall 	return 0;
3406b6d33834SChristoffer Dall }
3407b6d33834SChristoffer Dall 
340814eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
340914eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
341014eebd91SCarsten Otte {
341114eebd91SCarsten Otte 	int r = -EINVAL;
341214eebd91SCarsten Otte 
341314eebd91SCarsten Otte 	switch (reg->id) {
341429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
341529b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
341629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
341729b7c71bSCarsten Otte 		break;
341829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
341929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
342029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
342129b7c71bSCarsten Otte 		break;
342246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
34234287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
342446a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
342546a6dd1cSJason J. herne 		break;
342646a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
342746a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
342846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
342946a6dd1cSJason J. herne 		break;
3430536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3431536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
3432536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3433536336c2SDominik Dingel 		break;
3434536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3435536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
3436536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3437536336c2SDominik Dingel 		break;
3438536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3439536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
3440536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3441536336c2SDominik Dingel 		break;
3442672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3443672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
3444672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3445672550fbSChristian Borntraeger 		break;
3446afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
3447afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
3448afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
3449afa45ff5SChristian Borntraeger 		break;
345014eebd91SCarsten Otte 	default:
345114eebd91SCarsten Otte 		break;
345214eebd91SCarsten Otte 	}
345314eebd91SCarsten Otte 
345414eebd91SCarsten Otte 	return r;
345514eebd91SCarsten Otte }
345614eebd91SCarsten Otte 
345714eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
345814eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
345914eebd91SCarsten Otte {
346014eebd91SCarsten Otte 	int r = -EINVAL;
34614287f247SDavid Hildenbrand 	__u64 val;
346214eebd91SCarsten Otte 
346314eebd91SCarsten Otte 	switch (reg->id) {
346429b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
346529b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
346629b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
346729b7c71bSCarsten Otte 		break;
346829b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
346929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
347029b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
347129b7c71bSCarsten Otte 		break;
347246a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
34734287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
34744287f247SDavid Hildenbrand 		if (!r)
34754287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
347646a6dd1cSJason J. herne 		break;
347746a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
347846a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
347946a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
348046a6dd1cSJason J. herne 		break;
3481536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3482536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
3483536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
34849fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
34859fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
3486536336c2SDominik Dingel 		break;
3487536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3488536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
3489536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3490536336c2SDominik Dingel 		break;
3491536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3492536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
3493536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3494536336c2SDominik Dingel 		break;
3495672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3496672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
3497672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3498672550fbSChristian Borntraeger 		break;
3499afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
3500afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
3501afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
3502afa45ff5SChristian Borntraeger 		break;
350314eebd91SCarsten Otte 	default:
350414eebd91SCarsten Otte 		break;
350514eebd91SCarsten Otte 	}
350614eebd91SCarsten Otte 
350714eebd91SCarsten Otte 	return r;
350814eebd91SCarsten Otte }
3509b6d33834SChristoffer Dall 
35107de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
3511b0c632dbSHeiko Carstens {
35127de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
35137de3f142SJanosch Frank 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
35147de3f142SJanosch Frank 	memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
35157de3f142SJanosch Frank 
35167de3f142SJanosch Frank 	kvm_clear_async_pf_completion_queue(vcpu);
35177de3f142SJanosch Frank 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
35187de3f142SJanosch Frank 		kvm_s390_vcpu_stop(vcpu);
35197de3f142SJanosch Frank 	kvm_s390_clear_local_irqs(vcpu);
35207de3f142SJanosch Frank }
35217de3f142SJanosch Frank 
35227de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
35237de3f142SJanosch Frank {
35247de3f142SJanosch Frank 	/* Initial reset is a superset of the normal reset */
35257de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_normal_reset(vcpu);
35267de3f142SJanosch Frank 
3527e93fc7b4SChristian Borntraeger 	/*
3528e93fc7b4SChristian Borntraeger 	 * This equals initial cpu reset in pop, but we don't switch to ESA.
3529e93fc7b4SChristian Borntraeger 	 * We do not only reset the internal data, but also ...
3530e93fc7b4SChristian Borntraeger 	 */
35317de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask = 0;
35327de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.addr = 0;
35337de3f142SJanosch Frank 	kvm_s390_set_prefix(vcpu, 0);
35347de3f142SJanosch Frank 	kvm_s390_set_cpu_timer(vcpu, 0);
35357de3f142SJanosch Frank 	vcpu->arch.sie_block->ckc = 0;
35367de3f142SJanosch Frank 	memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
35377de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
35387de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
3539e93fc7b4SChristian Borntraeger 
3540e93fc7b4SChristian Borntraeger 	/* ... the data in sync regs */
3541e93fc7b4SChristian Borntraeger 	memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
3542e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.ckc = 0;
3543e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
3544e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
3545e93fc7b4SChristian Borntraeger 	vcpu->run->psw_addr = 0;
3546e93fc7b4SChristian Borntraeger 	vcpu->run->psw_mask = 0;
3547e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.todpr = 0;
3548e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.cputm = 0;
3549e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.ckc = 0;
3550e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.pp = 0;
3551e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.gbea = 1;
35527de3f142SJanosch Frank 	vcpu->run->s.regs.fpc = 0;
35530f303504SJanosch Frank 	/*
35540f303504SJanosch Frank 	 * Do not reset these registers in the protected case, as some of
35550f303504SJanosch Frank 	 * them are overlayed and they are not accessible in this case
35560f303504SJanosch Frank 	 * anyway.
35570f303504SJanosch Frank 	 */
35580f303504SJanosch Frank 	if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
35597de3f142SJanosch Frank 		vcpu->arch.sie_block->gbea = 1;
35607de3f142SJanosch Frank 		vcpu->arch.sie_block->pp = 0;
35617de3f142SJanosch Frank 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
35620f303504SJanosch Frank 		vcpu->arch.sie_block->todpr = 0;
35630f303504SJanosch Frank 	}
35647de3f142SJanosch Frank }
35657de3f142SJanosch Frank 
35667de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
35677de3f142SJanosch Frank {
35687de3f142SJanosch Frank 	struct kvm_sync_regs *regs = &vcpu->run->s.regs;
35697de3f142SJanosch Frank 
35707de3f142SJanosch Frank 	/* Clear reset is a superset of the initial reset */
35717de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_initial_reset(vcpu);
35727de3f142SJanosch Frank 
35737de3f142SJanosch Frank 	memset(&regs->gprs, 0, sizeof(regs->gprs));
35747de3f142SJanosch Frank 	memset(&regs->vrs, 0, sizeof(regs->vrs));
35757de3f142SJanosch Frank 	memset(&regs->acrs, 0, sizeof(regs->acrs));
35767de3f142SJanosch Frank 	memset(&regs->gscb, 0, sizeof(regs->gscb));
35777de3f142SJanosch Frank 
35787de3f142SJanosch Frank 	regs->etoken = 0;
35797de3f142SJanosch Frank 	regs->etoken_extension = 0;
3580b0c632dbSHeiko Carstens }
3581b0c632dbSHeiko Carstens 
3582b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3583b0c632dbSHeiko Carstens {
3584875656feSChristoffer Dall 	vcpu_load(vcpu);
35855a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
3586875656feSChristoffer Dall 	vcpu_put(vcpu);
3587b0c632dbSHeiko Carstens 	return 0;
3588b0c632dbSHeiko Carstens }
3589b0c632dbSHeiko Carstens 
3590b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3591b0c632dbSHeiko Carstens {
35921fc9b76bSChristoffer Dall 	vcpu_load(vcpu);
35935a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
35941fc9b76bSChristoffer Dall 	vcpu_put(vcpu);
3595b0c632dbSHeiko Carstens 	return 0;
3596b0c632dbSHeiko Carstens }
3597b0c632dbSHeiko Carstens 
3598b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3599b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
3600b0c632dbSHeiko Carstens {
3601b4ef9d4eSChristoffer Dall 	vcpu_load(vcpu);
3602b4ef9d4eSChristoffer Dall 
360359674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
3604b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
3605b4ef9d4eSChristoffer Dall 
3606b4ef9d4eSChristoffer Dall 	vcpu_put(vcpu);
3607b0c632dbSHeiko Carstens 	return 0;
3608b0c632dbSHeiko Carstens }
3609b0c632dbSHeiko Carstens 
3610b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3611b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
3612b0c632dbSHeiko Carstens {
3613bcdec41cSChristoffer Dall 	vcpu_load(vcpu);
3614bcdec41cSChristoffer Dall 
361559674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
3616b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
3617bcdec41cSChristoffer Dall 
3618bcdec41cSChristoffer Dall 	vcpu_put(vcpu);
3619b0c632dbSHeiko Carstens 	return 0;
3620b0c632dbSHeiko Carstens }
3621b0c632dbSHeiko Carstens 
3622b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3623b0c632dbSHeiko Carstens {
36246a96bc7fSChristoffer Dall 	int ret = 0;
36256a96bc7fSChristoffer Dall 
36266a96bc7fSChristoffer Dall 	vcpu_load(vcpu);
36276a96bc7fSChristoffer Dall 
36286a96bc7fSChristoffer Dall 	if (test_fp_ctl(fpu->fpc)) {
36296a96bc7fSChristoffer Dall 		ret = -EINVAL;
36306a96bc7fSChristoffer Dall 		goto out;
36316a96bc7fSChristoffer Dall 	}
3632e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
36339abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
3634a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
3635a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
36369abc2a08SDavid Hildenbrand 	else
3637a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
36386a96bc7fSChristoffer Dall 
36396a96bc7fSChristoffer Dall out:
36406a96bc7fSChristoffer Dall 	vcpu_put(vcpu);
36416a96bc7fSChristoffer Dall 	return ret;
3642b0c632dbSHeiko Carstens }
3643b0c632dbSHeiko Carstens 
3644b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3645b0c632dbSHeiko Carstens {
36461393123eSChristoffer Dall 	vcpu_load(vcpu);
36471393123eSChristoffer Dall 
36489abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
36499abc2a08SDavid Hildenbrand 	save_fpu_regs();
36509abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
3651a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
3652a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
36539abc2a08SDavid Hildenbrand 	else
3654a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
3655e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
36561393123eSChristoffer Dall 
36571393123eSChristoffer Dall 	vcpu_put(vcpu);
3658b0c632dbSHeiko Carstens 	return 0;
3659b0c632dbSHeiko Carstens }
3660b0c632dbSHeiko Carstens 
3661b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
3662b0c632dbSHeiko Carstens {
3663b0c632dbSHeiko Carstens 	int rc = 0;
3664b0c632dbSHeiko Carstens 
36657a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
3666b0c632dbSHeiko Carstens 		rc = -EBUSY;
3667d7b0b5ebSCarsten Otte 	else {
3668d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
3669d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
3670d7b0b5ebSCarsten Otte 	}
3671b0c632dbSHeiko Carstens 	return rc;
3672b0c632dbSHeiko Carstens }
3673b0c632dbSHeiko Carstens 
3674b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3675b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
3676b0c632dbSHeiko Carstens {
3677b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
3678b0c632dbSHeiko Carstens }
3679b0c632dbSHeiko Carstens 
368027291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
368127291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
368227291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
368327291e21SDavid Hildenbrand 
3684d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
3685d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
3686b0c632dbSHeiko Carstens {
368727291e21SDavid Hildenbrand 	int rc = 0;
368827291e21SDavid Hildenbrand 
368966b56562SChristoffer Dall 	vcpu_load(vcpu);
369066b56562SChristoffer Dall 
369127291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
369227291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
369327291e21SDavid Hildenbrand 
369466b56562SChristoffer Dall 	if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
369566b56562SChristoffer Dall 		rc = -EINVAL;
369666b56562SChristoffer Dall 		goto out;
369766b56562SChristoffer Dall 	}
369866b56562SChristoffer Dall 	if (!sclp.has_gpere) {
369966b56562SChristoffer Dall 		rc = -EINVAL;
370066b56562SChristoffer Dall 		goto out;
370166b56562SChristoffer Dall 	}
370227291e21SDavid Hildenbrand 
370327291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
370427291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
370527291e21SDavid Hildenbrand 		/* enforce guest PER */
3706ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
370727291e21SDavid Hildenbrand 
370827291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
370927291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
371027291e21SDavid Hildenbrand 	} else {
37119daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
371227291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
371327291e21SDavid Hildenbrand 	}
371427291e21SDavid Hildenbrand 
371527291e21SDavid Hildenbrand 	if (rc) {
371627291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
371727291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
37189daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
371927291e21SDavid Hildenbrand 	}
372027291e21SDavid Hildenbrand 
372166b56562SChristoffer Dall out:
372266b56562SChristoffer Dall 	vcpu_put(vcpu);
372327291e21SDavid Hildenbrand 	return rc;
3724b0c632dbSHeiko Carstens }
3725b0c632dbSHeiko Carstens 
372662d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
372762d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
372862d9f0dbSMarcelo Tosatti {
3729fd232561SChristoffer Dall 	int ret;
3730fd232561SChristoffer Dall 
3731fd232561SChristoffer Dall 	vcpu_load(vcpu);
3732fd232561SChristoffer Dall 
37336352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
3734fd232561SChristoffer Dall 	ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
37356352e4d2SDavid Hildenbrand 				      KVM_MP_STATE_OPERATING;
3736fd232561SChristoffer Dall 
3737fd232561SChristoffer Dall 	vcpu_put(vcpu);
3738fd232561SChristoffer Dall 	return ret;
373962d9f0dbSMarcelo Tosatti }
374062d9f0dbSMarcelo Tosatti 
374162d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
374262d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
374362d9f0dbSMarcelo Tosatti {
37446352e4d2SDavid Hildenbrand 	int rc = 0;
37456352e4d2SDavid Hildenbrand 
3746e83dff5eSChristoffer Dall 	vcpu_load(vcpu);
3747e83dff5eSChristoffer Dall 
37486352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
37496352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
37506352e4d2SDavid Hildenbrand 
37516352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
37526352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
3753fe28c786SJanosch Frank 		rc = kvm_s390_vcpu_stop(vcpu);
37546352e4d2SDavid Hildenbrand 		break;
37556352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
3756fe28c786SJanosch Frank 		rc = kvm_s390_vcpu_start(vcpu);
37576352e4d2SDavid Hildenbrand 		break;
37586352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
37597c36a3fcSJanosch Frank 		if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
37607c36a3fcSJanosch Frank 			rc = -ENXIO;
37617c36a3fcSJanosch Frank 			break;
37627c36a3fcSJanosch Frank 		}
37637c36a3fcSJanosch Frank 		rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
37647c36a3fcSJanosch Frank 		break;
37656352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
37663b684a42SJoe Perches 		fallthrough;	/* CHECK_STOP and LOAD are not supported yet */
37676352e4d2SDavid Hildenbrand 	default:
37686352e4d2SDavid Hildenbrand 		rc = -ENXIO;
37696352e4d2SDavid Hildenbrand 	}
37706352e4d2SDavid Hildenbrand 
3771e83dff5eSChristoffer Dall 	vcpu_put(vcpu);
37726352e4d2SDavid Hildenbrand 	return rc;
377362d9f0dbSMarcelo Tosatti }
377462d9f0dbSMarcelo Tosatti 
37758ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
37768ad35755SDavid Hildenbrand {
37778d5fb0dcSDavid Hildenbrand 	return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
37788ad35755SDavid Hildenbrand }
37798ad35755SDavid Hildenbrand 
37802c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
37812c70fe44SChristian Borntraeger {
37828ad35755SDavid Hildenbrand retry:
37838e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
37842fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
3785586b7ccdSChristian Borntraeger 		return 0;
37862c70fe44SChristian Borntraeger 	/*
37872c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3788b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
37892c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
37902c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
37912c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
37922c70fe44SChristian Borntraeger 	 */
37938ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
37942c70fe44SChristian Borntraeger 		int rc;
3795b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
3796fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
3797b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
3798aca411a4SJulius Niedworok 		if (rc) {
3799aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
38002c70fe44SChristian Borntraeger 			return rc;
3801aca411a4SJulius Niedworok 		}
38028ad35755SDavid Hildenbrand 		goto retry;
38032c70fe44SChristian Borntraeger 	}
38048ad35755SDavid Hildenbrand 
3805d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3806d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
3807d3d692c8SDavid Hildenbrand 		goto retry;
3808d3d692c8SDavid Hildenbrand 	}
3809d3d692c8SDavid Hildenbrand 
38108ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
38118ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
38128ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3813ef8f4f49SDavid Hildenbrand 			kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
38148ad35755SDavid Hildenbrand 		}
38158ad35755SDavid Hildenbrand 		goto retry;
38168ad35755SDavid Hildenbrand 	}
38178ad35755SDavid Hildenbrand 
38188ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
38198ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
38208ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
38219daecfc6SDavid Hildenbrand 			kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
38228ad35755SDavid Hildenbrand 		}
38238ad35755SDavid Hildenbrand 		goto retry;
38248ad35755SDavid Hildenbrand 	}
38258ad35755SDavid Hildenbrand 
38266502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
38276502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
38286502a34cSDavid Hildenbrand 		goto retry;
38296502a34cSDavid Hildenbrand 	}
38306502a34cSDavid Hildenbrand 
3831190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3832190df4a2SClaudio Imbrenda 		/*
3833c9f0a2b8SJanosch Frank 		 * Disable CMM virtualization; we will emulate the ESSA
3834190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
3835190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
3836190df4a2SClaudio Imbrenda 		 */
3837190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3838190df4a2SClaudio Imbrenda 		goto retry;
3839190df4a2SClaudio Imbrenda 	}
3840190df4a2SClaudio Imbrenda 
3841190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3842190df4a2SClaudio Imbrenda 		/*
3843c9f0a2b8SJanosch Frank 		 * Re-enable CMM virtualization if CMMA is available and
3844c9f0a2b8SJanosch Frank 		 * CMM has been used.
3845190df4a2SClaudio Imbrenda 		 */
3846190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
3847c9f0a2b8SJanosch Frank 		    (vcpu->kvm->mm->context.uses_cmm))
3848190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3849190df4a2SClaudio Imbrenda 		goto retry;
3850190df4a2SClaudio Imbrenda 	}
3851190df4a2SClaudio Imbrenda 
38520759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
385372875d8aSRadim Krčmář 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
38543194cdb7SDavid Hildenbrand 	/* we left the vsie handler, nothing to do, just clear the request */
38553194cdb7SDavid Hildenbrand 	kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
38560759d068SDavid Hildenbrand 
38572c70fe44SChristian Borntraeger 	return 0;
38582c70fe44SChristian Borntraeger }
38592c70fe44SChristian Borntraeger 
38600e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm,
38618fa1696eSCollin L. Walling 			    const struct kvm_s390_vm_tod_clock *gtod)
38628fa1696eSCollin L. Walling {
38638fa1696eSCollin L. Walling 	struct kvm_vcpu *vcpu;
3864*2cfd7b73SHeiko Carstens 	union tod_clock clk;
38658fa1696eSCollin L. Walling 	int i;
38668fa1696eSCollin L. Walling 
38678fa1696eSCollin L. Walling 	mutex_lock(&kvm->lock);
38688fa1696eSCollin L. Walling 	preempt_disable();
38698fa1696eSCollin L. Walling 
3870*2cfd7b73SHeiko Carstens 	store_tod_clock_ext(&clk);
38718fa1696eSCollin L. Walling 
3872*2cfd7b73SHeiko Carstens 	kvm->arch.epoch = gtod->tod - clk.tod;
38730e7def5fSDavid Hildenbrand 	kvm->arch.epdx = 0;
38740e7def5fSDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
3875*2cfd7b73SHeiko Carstens 		kvm->arch.epdx = gtod->epoch_idx - clk.ei;
38768fa1696eSCollin L. Walling 		if (kvm->arch.epoch > gtod->tod)
38778fa1696eSCollin L. Walling 			kvm->arch.epdx -= 1;
38780e7def5fSDavid Hildenbrand 	}
38798fa1696eSCollin L. Walling 
38808fa1696eSCollin L. Walling 	kvm_s390_vcpu_block_all(kvm);
38818fa1696eSCollin L. Walling 	kvm_for_each_vcpu(i, vcpu, kvm) {
38828fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
38838fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
38848fa1696eSCollin L. Walling 	}
38858fa1696eSCollin L. Walling 
38868fa1696eSCollin L. Walling 	kvm_s390_vcpu_unblock_all(kvm);
38878fa1696eSCollin L. Walling 	preempt_enable();
38888fa1696eSCollin L. Walling 	mutex_unlock(&kvm->lock);
38898fa1696eSCollin L. Walling }
38908fa1696eSCollin L. Walling 
3891fa576c58SThomas Huth /**
3892fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
3893fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
3894fa576c58SThomas Huth  * @gpa: Guest physical address
3895fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
3896fa576c58SThomas Huth  *
3897fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
3898fa576c58SThomas Huth  *
3899fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
3900fa576c58SThomas Huth  */
3901fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
390224eb3a82SDominik Dingel {
3903527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
3904527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
390524eb3a82SDominik Dingel }
390624eb3a82SDominik Dingel 
39073c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
39083c038e6bSDominik Dingel 				      unsigned long token)
39093c038e6bSDominik Dingel {
39103c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
3911383d0b05SJens Freimann 	struct kvm_s390_irq irq;
39123c038e6bSDominik Dingel 
39133c038e6bSDominik Dingel 	if (start_token) {
3914383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
3915383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
3916383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
39173c038e6bSDominik Dingel 	} else {
39183c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
3919383d0b05SJens Freimann 		inti.parm64 = token;
39203c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
39213c038e6bSDominik Dingel 	}
39223c038e6bSDominik Dingel }
39233c038e6bSDominik Dingel 
39242a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
39253c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
39263c038e6bSDominik Dingel {
39273c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
39283c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
39292a18b7e7SVitaly Kuznetsov 
39302a18b7e7SVitaly Kuznetsov 	return true;
39313c038e6bSDominik Dingel }
39323c038e6bSDominik Dingel 
39333c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
39343c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
39353c038e6bSDominik Dingel {
39363c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
39373c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
39383c038e6bSDominik Dingel }
39393c038e6bSDominik Dingel 
39403c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
39413c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
39423c038e6bSDominik Dingel {
39433c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
39443c038e6bSDominik Dingel }
39453c038e6bSDominik Dingel 
39467c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
39473c038e6bSDominik Dingel {
39483c038e6bSDominik Dingel 	/*
39493c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
39503c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
39513c038e6bSDominik Dingel 	 */
39523c038e6bSDominik Dingel 	return true;
39533c038e6bSDominik Dingel }
39543c038e6bSDominik Dingel 
3955e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
39563c038e6bSDominik Dingel {
39573c038e6bSDominik Dingel 	hva_t hva;
39583c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
39593c038e6bSDominik Dingel 
39603c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3961e8c22266SVitaly Kuznetsov 		return false;
39623c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
39633c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
3964e8c22266SVitaly Kuznetsov 		return false;
39653c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
3966e8c22266SVitaly Kuznetsov 		return false;
39679a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
3968e8c22266SVitaly Kuznetsov 		return false;
3969b9224cd7SDavid Hildenbrand 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
3970e8c22266SVitaly Kuznetsov 		return false;
39713c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
3972e8c22266SVitaly Kuznetsov 		return false;
39733c038e6bSDominik Dingel 
397481480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
397581480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
397681480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
3977e8c22266SVitaly Kuznetsov 		return false;
39783c038e6bSDominik Dingel 
3979e8c22266SVitaly Kuznetsov 	return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
39803c038e6bSDominik Dingel }
39813c038e6bSDominik Dingel 
39823fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3983b0c632dbSHeiko Carstens {
39843fb4c40fSThomas Huth 	int rc, cpuflags;
3985e168bf8dSCarsten Otte 
39863c038e6bSDominik Dingel 	/*
39873c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
39883c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
39893c038e6bSDominik Dingel 	 * handled outside the worker.
39903c038e6bSDominik Dingel 	 */
39913c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
39923c038e6bSDominik Dingel 
39937ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
39947ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
3995b0c632dbSHeiko Carstens 
3996b0c632dbSHeiko Carstens 	if (need_resched())
3997b0c632dbSHeiko Carstens 		schedule();
3998b0c632dbSHeiko Carstens 
399979395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
400079395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
400179395031SJens Freimann 		if (rc)
400279395031SJens Freimann 			return rc;
400379395031SJens Freimann 	}
40040ff31867SCarsten Otte 
40052c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
40062c70fe44SChristian Borntraeger 	if (rc)
40072c70fe44SChristian Borntraeger 		return rc;
40082c70fe44SChristian Borntraeger 
400927291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
401027291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
401127291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
401227291e21SDavid Hildenbrand 	}
401327291e21SDavid Hildenbrand 
40149f30f621SMichael Mueller 	clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask);
40159f30f621SMichael Mueller 
4016b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
40173fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
40183fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
40193fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
40202b29a9fdSDominik Dingel 
40213fb4c40fSThomas Huth 	return 0;
40223fb4c40fSThomas Huth }
40233fb4c40fSThomas Huth 
4024492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
4025492d8642SThomas Huth {
402656317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
402756317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
402856317920SDavid Hildenbrand 	};
402956317920SDavid Hildenbrand 	u8 opcode, ilen;
4030492d8642SThomas Huth 	int rc;
4031492d8642SThomas Huth 
4032492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4033492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
4034492d8642SThomas Huth 
4035492d8642SThomas Huth 	/*
4036492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
4037492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
4038492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
4039492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
4040492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
4041492d8642SThomas Huth 	 * to be able to forward the PSW.
4042492d8642SThomas Huth 	 */
40433fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
404456317920SDavid Hildenbrand 	ilen = insn_length(opcode);
40459b0d721aSDavid Hildenbrand 	if (rc < 0) {
40469b0d721aSDavid Hildenbrand 		return rc;
40479b0d721aSDavid Hildenbrand 	} else if (rc) {
40489b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
40499b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
40509b0d721aSDavid Hildenbrand 		 * nullification if necessary.
40519b0d721aSDavid Hildenbrand 		 */
40529b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
40539b0d721aSDavid Hildenbrand 		ilen = 4;
40549b0d721aSDavid Hildenbrand 	}
405556317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
405656317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
405756317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
4058492d8642SThomas Huth }
4059492d8642SThomas Huth 
40603fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
40613fb4c40fSThomas Huth {
40624d62fcc0SQingFeng Hao 	struct mcck_volatile_info *mcck_info;
40634d62fcc0SQingFeng Hao 	struct sie_page *sie_page;
40644d62fcc0SQingFeng Hao 
40652b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
40662b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
40672b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
40682b29a9fdSDominik Dingel 
406927291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
407027291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
407127291e21SDavid Hildenbrand 
40727ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
40737ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
407471f116bfSDavid Hildenbrand 
40754d62fcc0SQingFeng Hao 	if (exit_reason == -EINTR) {
40764d62fcc0SQingFeng Hao 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
40774d62fcc0SQingFeng Hao 		sie_page = container_of(vcpu->arch.sie_block,
40784d62fcc0SQingFeng Hao 					struct sie_page, sie_block);
40794d62fcc0SQingFeng Hao 		mcck_info = &sie_page->mcck_info;
40804d62fcc0SQingFeng Hao 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
40814d62fcc0SQingFeng Hao 		return 0;
40824d62fcc0SQingFeng Hao 	}
40834d62fcc0SQingFeng Hao 
408471f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
408571f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
408671f116bfSDavid Hildenbrand 
408771f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
408871f116bfSDavid Hildenbrand 			return rc;
408971f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
409071f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
409171f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
409271f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
409371f116bfSDavid Hildenbrand 		return -EREMOTE;
409471f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
409571f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
409671f116bfSDavid Hildenbrand 		return 0;
4097210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
4098210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4099210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
4100210b1607SThomas Huth 						current->thread.gmap_addr;
4101210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
410271f116bfSDavid Hildenbrand 		return -EREMOTE;
410324eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
41043c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
410524eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
410671f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
410771f116bfSDavid Hildenbrand 			return 0;
410850a05be4SChristian Borntraeger 		vcpu->stat.pfault_sync++;
410971f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
4110fa576c58SThomas Huth 	}
411171f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
41123fb4c40fSThomas Huth }
41133fb4c40fSThomas Huth 
41143adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
41153fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
41163fb4c40fSThomas Huth {
41173fb4c40fSThomas Huth 	int rc, exit_reason;
4118c8aac234SJanosch Frank 	struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
41193fb4c40fSThomas Huth 
4120800c1065SThomas Huth 	/*
4121800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4122800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
4123800c1065SThomas Huth 	 */
4124800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4125800c1065SThomas Huth 
4126a76ccff6SThomas Huth 	do {
41273fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
41283fb4c40fSThomas Huth 		if (rc)
4129a76ccff6SThomas Huth 			break;
41303fb4c40fSThomas Huth 
4131800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
41323fb4c40fSThomas Huth 		/*
4133a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
4134a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
41353fb4c40fSThomas Huth 		 */
41360097d12eSChristian Borntraeger 		local_irq_disable();
41376edaa530SPaolo Bonzini 		guest_enter_irqoff();
4138db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
41390097d12eSChristian Borntraeger 		local_irq_enable();
4140c8aac234SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4141c8aac234SJanosch Frank 			memcpy(sie_page->pv_grregs,
4142c8aac234SJanosch Frank 			       vcpu->run->s.regs.gprs,
4143c8aac234SJanosch Frank 			       sizeof(sie_page->pv_grregs));
4144c8aac234SJanosch Frank 		}
414556e62a73SSven Schnelle 		if (test_cpu_flag(CIF_FPU))
414656e62a73SSven Schnelle 			load_fpu_regs();
4147a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
4148a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
4149c8aac234SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4150c8aac234SJanosch Frank 			memcpy(vcpu->run->s.regs.gprs,
4151c8aac234SJanosch Frank 			       sie_page->pv_grregs,
4152c8aac234SJanosch Frank 			       sizeof(sie_page->pv_grregs));
41533adae0b4SJanosch Frank 			/*
41543adae0b4SJanosch Frank 			 * We're not allowed to inject interrupts on intercepts
41553adae0b4SJanosch Frank 			 * that leave the guest state in an "in-between" state
41563adae0b4SJanosch Frank 			 * where the next SIE entry will do a continuation.
41573adae0b4SJanosch Frank 			 * Fence interrupts in our "internal" PSW.
41583adae0b4SJanosch Frank 			 */
41593adae0b4SJanosch Frank 			if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
41603adae0b4SJanosch Frank 			    vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
41613adae0b4SJanosch Frank 				vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
41623adae0b4SJanosch Frank 			}
4163c8aac234SJanosch Frank 		}
41640097d12eSChristian Borntraeger 		local_irq_disable();
4165db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
41666edaa530SPaolo Bonzini 		guest_exit_irqoff();
41670097d12eSChristian Borntraeger 		local_irq_enable();
4168800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
41693fb4c40fSThomas Huth 
41703fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
417127291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
41723fb4c40fSThomas Huth 
4173800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4174e168bf8dSCarsten Otte 	return rc;
4175b0c632dbSHeiko Carstens }
4176b0c632dbSHeiko Carstens 
41772f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
4178b028ee3eSDavid Hildenbrand {
41792f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
41804d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
41814e0b1ab7SFan Zhang 	struct gs_cb *gscb;
41824d5f2c04SChristian Borntraeger 
41834d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
41844e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4185b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4186b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4187b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4188b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4189b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4190b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4191b028ee3eSDavid Hildenbrand 	}
4192b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4193b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4194b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4195b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
41969fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
41979fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
4198b028ee3eSDavid Hildenbrand 	}
419923a60f83SCollin Walling 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
420023a60f83SCollin Walling 		vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
420123a60f83SCollin Walling 		vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
420223a60f83SCollin Walling 	}
420380cd8763SFan Zhang 	/*
420480cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
420580cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
420680cd8763SFan Zhang 	 */
420780cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
42084d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
4209bb59c2daSAlice Frosi 	    riccb->v &&
42100c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
42114d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
42120c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
421380cd8763SFan Zhang 	}
42144e0b1ab7SFan Zhang 	/*
42154e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
42164e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
42174e0b1ab7SFan Zhang 	 */
42184e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
42194e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
42204e0b1ab7SFan Zhang 	    gscb->gssm &&
42214e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
42224e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
42234e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
42244e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
42254e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
422680cd8763SFan Zhang 	}
422735b3fde6SChristian Borntraeger 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
422835b3fde6SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 82)) {
422935b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
423035b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
423135b3fde6SChristian Borntraeger 	}
42324e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
42334e0b1ab7SFan Zhang 		preempt_disable();
42344e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
42354e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
42364e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
42374e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
42384e0b1ab7SFan Zhang 		}
42394e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
42404e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
42414e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
42424e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
42434e0b1ab7SFan Zhang 		}
42444e0b1ab7SFan Zhang 		preempt_enable();
42454e0b1ab7SFan Zhang 	}
4246a3da7b4aSChristian Borntraeger 	/* SIE will load etoken directly from SDNX and therefore kvm_run */
4247811ea797SJanosch Frank }
4248811ea797SJanosch Frank 
42492f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu)
4250811ea797SJanosch Frank {
42512f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
42522f0a83beSTianjia Zhang 
4253811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4254811ea797SJanosch Frank 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4255811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4256811ea797SJanosch Frank 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4257811ea797SJanosch Frank 		/* some control register changes require a tlb flush */
4258811ea797SJanosch Frank 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4259811ea797SJanosch Frank 	}
4260811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4261811ea797SJanosch Frank 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4262811ea797SJanosch Frank 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4263811ea797SJanosch Frank 	}
4264811ea797SJanosch Frank 	save_access_regs(vcpu->arch.host_acrs);
4265811ea797SJanosch Frank 	restore_access_regs(vcpu->run->s.regs.acrs);
4266811ea797SJanosch Frank 	/* save host (userspace) fprs/vrs */
4267811ea797SJanosch Frank 	save_fpu_regs();
4268811ea797SJanosch Frank 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4269811ea797SJanosch Frank 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4270811ea797SJanosch Frank 	if (MACHINE_HAS_VX)
4271811ea797SJanosch Frank 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4272811ea797SJanosch Frank 	else
4273811ea797SJanosch Frank 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4274811ea797SJanosch Frank 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4275811ea797SJanosch Frank 	if (test_fp_ctl(current->thread.fpu.fpc))
4276811ea797SJanosch Frank 		/* User space provided an invalid FPC, let's clear it */
4277811ea797SJanosch Frank 		current->thread.fpu.fpc = 0;
4278811ea797SJanosch Frank 
4279811ea797SJanosch Frank 	/* Sync fmt2 only data */
4280811ea797SJanosch Frank 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
42812f0a83beSTianjia Zhang 		sync_regs_fmt2(vcpu);
4282811ea797SJanosch Frank 	} else {
4283811ea797SJanosch Frank 		/*
4284811ea797SJanosch Frank 		 * In several places we have to modify our internal view to
4285811ea797SJanosch Frank 		 * not do things that are disallowed by the ultravisor. For
4286811ea797SJanosch Frank 		 * example we must not inject interrupts after specific exits
4287811ea797SJanosch Frank 		 * (e.g. 112 prefix page not secure). We do this by turning
4288811ea797SJanosch Frank 		 * off the machine check, external and I/O interrupt bits
4289811ea797SJanosch Frank 		 * of our PSW copy. To avoid getting validity intercepts, we
4290811ea797SJanosch Frank 		 * do only accept the condition code from userspace.
4291811ea797SJanosch Frank 		 */
4292811ea797SJanosch Frank 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4293811ea797SJanosch Frank 		vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4294811ea797SJanosch Frank 						   PSW_MASK_CC;
4295811ea797SJanosch Frank 	}
429680cd8763SFan Zhang 
4297b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
4298b028ee3eSDavid Hildenbrand }
4299b028ee3eSDavid Hildenbrand 
43002f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu)
4301b028ee3eSDavid Hildenbrand {
43022f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
43032f0a83beSTianjia Zhang 
4304b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4305b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4306b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
430735b3fde6SChristian Borntraeger 	kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
430823a60f83SCollin Walling 	kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
43094e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
43104e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
43114e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
43124e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
43134e0b1ab7SFan Zhang 		preempt_disable();
43144e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
43154e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
43164e0b1ab7SFan Zhang 		preempt_enable();
43174e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
43184e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
43194e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
43204e0b1ab7SFan Zhang 	}
4321a3da7b4aSChristian Borntraeger 	/* SIE will save etoken directly into SDNX and therefore kvm_run */
4322b028ee3eSDavid Hildenbrand }
4323b028ee3eSDavid Hildenbrand 
43242f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu)
4325811ea797SJanosch Frank {
43262f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
43272f0a83beSTianjia Zhang 
4328811ea797SJanosch Frank 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
4329811ea797SJanosch Frank 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
4330811ea797SJanosch Frank 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
4331811ea797SJanosch Frank 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
4332811ea797SJanosch Frank 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
4333811ea797SJanosch Frank 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
4334811ea797SJanosch Frank 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
4335811ea797SJanosch Frank 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
4336811ea797SJanosch Frank 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
4337811ea797SJanosch Frank 	save_access_regs(vcpu->run->s.regs.acrs);
4338811ea797SJanosch Frank 	restore_access_regs(vcpu->arch.host_acrs);
4339811ea797SJanosch Frank 	/* Save guest register state */
4340811ea797SJanosch Frank 	save_fpu_regs();
4341811ea797SJanosch Frank 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4342811ea797SJanosch Frank 	/* Restore will be done lazily at return */
4343811ea797SJanosch Frank 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
4344811ea797SJanosch Frank 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
4345811ea797SJanosch Frank 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
43462f0a83beSTianjia Zhang 		store_regs_fmt2(vcpu);
4347811ea797SJanosch Frank }
4348811ea797SJanosch Frank 
43491b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
4350b0c632dbSHeiko Carstens {
43511b94f6f8STianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
43528f2abe6aSChristian Borntraeger 	int rc;
4353b0c632dbSHeiko Carstens 
4354460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
4355460df4c1SPaolo Bonzini 		return -EINTR;
4356460df4c1SPaolo Bonzini 
4357200824f5SThomas Huth 	if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
4358200824f5SThomas Huth 	    kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
4359200824f5SThomas Huth 		return -EINVAL;
4360200824f5SThomas Huth 
4361accb757dSChristoffer Dall 	vcpu_load(vcpu);
4362accb757dSChristoffer Dall 
436327291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
436427291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
4365accb757dSChristoffer Dall 		rc = 0;
4366accb757dSChristoffer Dall 		goto out;
436727291e21SDavid Hildenbrand 	}
436827291e21SDavid Hildenbrand 
436920b7035cSJan H. Schönherr 	kvm_sigset_activate(vcpu);
4370b0c632dbSHeiko Carstens 
4371fe28c786SJanosch Frank 	/*
4372fe28c786SJanosch Frank 	 * no need to check the return value of vcpu_start as it can only have
4373fe28c786SJanosch Frank 	 * an error for protvirt, but protvirt means user cpu state
4374fe28c786SJanosch Frank 	 */
43756352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
43766852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
43776352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
4378ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
43796352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
4380accb757dSChristoffer Dall 		rc = -EINVAL;
4381accb757dSChristoffer Dall 		goto out;
43826352e4d2SDavid Hildenbrand 	}
4383b0c632dbSHeiko Carstens 
43842f0a83beSTianjia Zhang 	sync_regs(vcpu);
4385db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
4386d7b0b5ebSCarsten Otte 
4387dab4079dSHeiko Carstens 	might_fault();
4388e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
43899ace903dSChristian Ehrhardt 
4390b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
4391b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
43928f2abe6aSChristian Borntraeger 		rc = -EINTR;
4393b1d16c49SChristian Ehrhardt 	}
43948f2abe6aSChristian Borntraeger 
439527291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
439627291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
439727291e21SDavid Hildenbrand 		rc = 0;
439827291e21SDavid Hildenbrand 	}
439927291e21SDavid Hildenbrand 
44008f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
440171f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
44028f2abe6aSChristian Borntraeger 		rc = 0;
44038f2abe6aSChristian Borntraeger 	}
44048f2abe6aSChristian Borntraeger 
4405db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
44062f0a83beSTianjia Zhang 	store_regs(vcpu);
4407d7b0b5ebSCarsten Otte 
440820b7035cSJan H. Schönherr 	kvm_sigset_deactivate(vcpu);
4409b0c632dbSHeiko Carstens 
4410b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
4411accb757dSChristoffer Dall out:
4412accb757dSChristoffer Dall 	vcpu_put(vcpu);
44137e8e6ab4SHeiko Carstens 	return rc;
4414b0c632dbSHeiko Carstens }
4415b0c632dbSHeiko Carstens 
4416b0c632dbSHeiko Carstens /*
4417b0c632dbSHeiko Carstens  * store status at address
4418b0c632dbSHeiko Carstens  * we use have two special cases:
4419b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
4420b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
4421b0c632dbSHeiko Carstens  */
4422d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
4423b0c632dbSHeiko Carstens {
4424092670cdSCarsten Otte 	unsigned char archmode = 1;
44259abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
4426fda902cbSMichael Mueller 	unsigned int px;
44274287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
4428d0bce605SHeiko Carstens 	int rc;
4429b0c632dbSHeiko Carstens 
4430d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
4431d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
4432d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
4433b0c632dbSHeiko Carstens 			return -EFAULT;
4434d9a3a09aSMartin Schwidefsky 		gpa = 0;
4435d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
4436d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
4437b0c632dbSHeiko Carstens 			return -EFAULT;
4438d9a3a09aSMartin Schwidefsky 		gpa = px;
4439d9a3a09aSMartin Schwidefsky 	} else
4440d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
44419abc2a08SDavid Hildenbrand 
44429abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
44439abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
44449522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
4445d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
44469abc2a08SDavid Hildenbrand 				     fprs, 128);
44479abc2a08SDavid Hildenbrand 	} else {
44489abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
44496fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
44509abc2a08SDavid Hildenbrand 	}
4451d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
4452d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
4453d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
4454d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
4455d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
4456fda902cbSMichael Mueller 			      &px, 4);
4457d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
44589abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
4459d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
4460d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
44614287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
4462d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
44634287f247SDavid Hildenbrand 			      &cputm, 8);
4464178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
4465d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
4466d0bce605SHeiko Carstens 			      &clkcomp, 8);
4467d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
4468d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
4469d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
4470d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
4471d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
4472b0c632dbSHeiko Carstens }
4473b0c632dbSHeiko Carstens 
4474e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
4475e879892cSThomas Huth {
4476e879892cSThomas Huth 	/*
4477e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
447831d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
4479e879892cSThomas Huth 	 * it into the save area
4480e879892cSThomas Huth 	 */
4481d0164ee2SHendrik Brueckner 	save_fpu_regs();
44829abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4483e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
4484e879892cSThomas Huth 
4485e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
4486e879892cSThomas Huth }
4487e879892cSThomas Huth 
44888ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
44898ad35755SDavid Hildenbrand {
44908ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
44918e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
44928ad35755SDavid Hildenbrand }
44938ad35755SDavid Hildenbrand 
44948ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
44958ad35755SDavid Hildenbrand {
44968ad35755SDavid Hildenbrand 	unsigned int i;
44978ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
44988ad35755SDavid Hildenbrand 
44998ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
45008ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
45018ad35755SDavid Hildenbrand 	}
45028ad35755SDavid Hildenbrand }
45038ad35755SDavid Hildenbrand 
45048ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
45058ad35755SDavid Hildenbrand {
450609a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
450709a400e7SDavid Hildenbrand 		return;
45088ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
45098e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
45108ad35755SDavid Hildenbrand }
45118ad35755SDavid Hildenbrand 
4512fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
45136852d7b6SDavid Hildenbrand {
4514fe28c786SJanosch Frank 	int i, online_vcpus, r = 0, started_vcpus = 0;
45158ad35755SDavid Hildenbrand 
45168ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
4517fe28c786SJanosch Frank 		return 0;
45188ad35755SDavid Hildenbrand 
45196852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
45208ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
4521433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
45228ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
45238ad35755SDavid Hildenbrand 
4524fe28c786SJanosch Frank 	/* Let's tell the UV that we want to change into the operating state */
4525fe28c786SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4526fe28c786SJanosch Frank 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
4527fe28c786SJanosch Frank 		if (r) {
4528fe28c786SJanosch Frank 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4529fe28c786SJanosch Frank 			return r;
4530fe28c786SJanosch Frank 		}
4531fe28c786SJanosch Frank 	}
4532fe28c786SJanosch Frank 
45338ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
45348ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
45358ad35755SDavid Hildenbrand 			started_vcpus++;
45368ad35755SDavid Hildenbrand 	}
45378ad35755SDavid Hildenbrand 
45388ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
45398ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
45408ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
45418ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
45428ad35755SDavid Hildenbrand 		/*
45438ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
45448ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
45458ad35755SDavid Hildenbrand 		 * oustanding ENABLE requests.
45468ad35755SDavid Hildenbrand 		 */
45478ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
45488ad35755SDavid Hildenbrand 	}
45498ad35755SDavid Hildenbrand 
45509daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
45518ad35755SDavid Hildenbrand 	/*
455272f21820SChristian Borntraeger 	 * The real PSW might have changed due to a RESTART interpreted by the
455372f21820SChristian Borntraeger 	 * ultravisor. We block all interrupts and let the next sie exit
455472f21820SChristian Borntraeger 	 * refresh our view.
455572f21820SChristian Borntraeger 	 */
455672f21820SChristian Borntraeger 	if (kvm_s390_pv_cpu_is_protected(vcpu))
455772f21820SChristian Borntraeger 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
455872f21820SChristian Borntraeger 	/*
45598ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
45608ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
45618ad35755SDavid Hildenbrand 	 */
4562d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4563433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4564fe28c786SJanosch Frank 	return 0;
45656852d7b6SDavid Hildenbrand }
45666852d7b6SDavid Hildenbrand 
4567fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
45686852d7b6SDavid Hildenbrand {
4569fe28c786SJanosch Frank 	int i, online_vcpus, r = 0, started_vcpus = 0;
45708ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
45718ad35755SDavid Hildenbrand 
45728ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
4573fe28c786SJanosch Frank 		return 0;
45748ad35755SDavid Hildenbrand 
45756852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
45768ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
4577433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
45788ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
45798ad35755SDavid Hildenbrand 
4580fe28c786SJanosch Frank 	/* Let's tell the UV that we want to change into the stopped state */
4581fe28c786SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4582fe28c786SJanosch Frank 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
4583fe28c786SJanosch Frank 		if (r) {
4584fe28c786SJanosch Frank 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4585fe28c786SJanosch Frank 			return r;
4586fe28c786SJanosch Frank 		}
4587fe28c786SJanosch Frank 	}
4588fe28c786SJanosch Frank 
458932f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
45906cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
459132f5ff63SDavid Hildenbrand 
4592ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
45938ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
45948ad35755SDavid Hildenbrand 
45958ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
45968ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
45978ad35755SDavid Hildenbrand 			started_vcpus++;
45988ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
45998ad35755SDavid Hildenbrand 		}
46008ad35755SDavid Hildenbrand 	}
46018ad35755SDavid Hildenbrand 
46028ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
46038ad35755SDavid Hildenbrand 		/*
46048ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
46058ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
46068ad35755SDavid Hildenbrand 		 */
46078ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
46088ad35755SDavid Hildenbrand 	}
46098ad35755SDavid Hildenbrand 
4610433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4611fe28c786SJanosch Frank 	return 0;
46126852d7b6SDavid Hildenbrand }
46136852d7b6SDavid Hildenbrand 
4614d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4615d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
4616d6712df9SCornelia Huck {
4617d6712df9SCornelia Huck 	int r;
4618d6712df9SCornelia Huck 
4619d6712df9SCornelia Huck 	if (cap->flags)
4620d6712df9SCornelia Huck 		return -EINVAL;
4621d6712df9SCornelia Huck 
4622d6712df9SCornelia Huck 	switch (cap->cap) {
4623fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
4624fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
4625fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
4626c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
4627fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
4628fa6b7fe9SCornelia Huck 		}
4629fa6b7fe9SCornelia Huck 		r = 0;
4630fa6b7fe9SCornelia Huck 		break;
4631d6712df9SCornelia Huck 	default:
4632d6712df9SCornelia Huck 		r = -EINVAL;
4633d6712df9SCornelia Huck 		break;
4634d6712df9SCornelia Huck 	}
4635d6712df9SCornelia Huck 	return r;
4636d6712df9SCornelia Huck }
4637d6712df9SCornelia Huck 
463819e12277SJanosch Frank static long kvm_s390_guest_sida_op(struct kvm_vcpu *vcpu,
463919e12277SJanosch Frank 				   struct kvm_s390_mem_op *mop)
464019e12277SJanosch Frank {
464119e12277SJanosch Frank 	void __user *uaddr = (void __user *)mop->buf;
464219e12277SJanosch Frank 	int r = 0;
464319e12277SJanosch Frank 
464419e12277SJanosch Frank 	if (mop->flags || !mop->size)
464519e12277SJanosch Frank 		return -EINVAL;
464619e12277SJanosch Frank 	if (mop->size + mop->sida_offset < mop->size)
464719e12277SJanosch Frank 		return -EINVAL;
464819e12277SJanosch Frank 	if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
464919e12277SJanosch Frank 		return -E2BIG;
465019e12277SJanosch Frank 
465119e12277SJanosch Frank 	switch (mop->op) {
465219e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_READ:
465319e12277SJanosch Frank 		if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) +
465419e12277SJanosch Frank 				 mop->sida_offset), mop->size))
465519e12277SJanosch Frank 			r = -EFAULT;
465619e12277SJanosch Frank 
465719e12277SJanosch Frank 		break;
465819e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_WRITE:
465919e12277SJanosch Frank 		if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) +
466019e12277SJanosch Frank 				   mop->sida_offset), uaddr, mop->size))
466119e12277SJanosch Frank 			r = -EFAULT;
466219e12277SJanosch Frank 		break;
466319e12277SJanosch Frank 	}
466419e12277SJanosch Frank 	return r;
466519e12277SJanosch Frank }
466641408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
466741408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
466841408c28SThomas Huth {
466941408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
467041408c28SThomas Huth 	void *tmpbuf = NULL;
467119e12277SJanosch Frank 	int r = 0;
467241408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
467341408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
467441408c28SThomas Huth 
4675a13b03bbSThomas Huth 	if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
467641408c28SThomas Huth 		return -EINVAL;
467741408c28SThomas Huth 
467841408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
467941408c28SThomas Huth 		return -E2BIG;
468041408c28SThomas Huth 
468119e12277SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu))
468219e12277SJanosch Frank 		return -EINVAL;
468319e12277SJanosch Frank 
468441408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
468541408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
468641408c28SThomas Huth 		if (!tmpbuf)
468741408c28SThomas Huth 			return -ENOMEM;
468841408c28SThomas Huth 	}
468941408c28SThomas Huth 
469041408c28SThomas Huth 	switch (mop->op) {
469141408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
469241408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
469392c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
469492c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
469541408c28SThomas Huth 			break;
469641408c28SThomas Huth 		}
469741408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
469841408c28SThomas Huth 		if (r == 0) {
469941408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
470041408c28SThomas Huth 				r = -EFAULT;
470141408c28SThomas Huth 		}
470241408c28SThomas Huth 		break;
470341408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
470441408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
470592c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
470692c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
470741408c28SThomas Huth 			break;
470841408c28SThomas Huth 		}
470941408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
471041408c28SThomas Huth 			r = -EFAULT;
471141408c28SThomas Huth 			break;
471241408c28SThomas Huth 		}
471341408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
471441408c28SThomas Huth 		break;
471541408c28SThomas Huth 	}
471641408c28SThomas Huth 
471741408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
471841408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
471941408c28SThomas Huth 
472041408c28SThomas Huth 	vfree(tmpbuf);
472141408c28SThomas Huth 	return r;
472241408c28SThomas Huth }
472341408c28SThomas Huth 
472419e12277SJanosch Frank static long kvm_s390_guest_memsida_op(struct kvm_vcpu *vcpu,
472519e12277SJanosch Frank 				      struct kvm_s390_mem_op *mop)
472619e12277SJanosch Frank {
472719e12277SJanosch Frank 	int r, srcu_idx;
472819e12277SJanosch Frank 
472919e12277SJanosch Frank 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
473019e12277SJanosch Frank 
473119e12277SJanosch Frank 	switch (mop->op) {
473219e12277SJanosch Frank 	case KVM_S390_MEMOP_LOGICAL_READ:
473319e12277SJanosch Frank 	case KVM_S390_MEMOP_LOGICAL_WRITE:
473419e12277SJanosch Frank 		r = kvm_s390_guest_mem_op(vcpu, mop);
473519e12277SJanosch Frank 		break;
473619e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_READ:
473719e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_WRITE:
473819e12277SJanosch Frank 		/* we are locked against sida going away by the vcpu->mutex */
473919e12277SJanosch Frank 		r = kvm_s390_guest_sida_op(vcpu, mop);
474019e12277SJanosch Frank 		break;
474119e12277SJanosch Frank 	default:
474219e12277SJanosch Frank 		r = -EINVAL;
474319e12277SJanosch Frank 	}
474419e12277SJanosch Frank 
474519e12277SJanosch Frank 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
474619e12277SJanosch Frank 	return r;
474719e12277SJanosch Frank }
474819e12277SJanosch Frank 
47495cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp,
4750b0c632dbSHeiko Carstens 			       unsigned int ioctl, unsigned long arg)
4751b0c632dbSHeiko Carstens {
4752b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
4753b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
4754b0c632dbSHeiko Carstens 
475593736624SAvi Kivity 	switch (ioctl) {
475647b43c52SJens Freimann 	case KVM_S390_IRQ: {
475747b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
475847b43c52SJens Freimann 
475947b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
47609b062471SChristoffer Dall 			return -EFAULT;
47619b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
476247b43c52SJens Freimann 	}
476393736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
4764ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
476553936b5bSThomas Huth 		struct kvm_s390_irq s390irq = {};
4766ba5c1e9bSCarsten Otte 
4767ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
47689b062471SChristoffer Dall 			return -EFAULT;
4769383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
4770383d0b05SJens Freimann 			return -EINVAL;
47719b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
4772ba5c1e9bSCarsten Otte 	}
47739b062471SChristoffer Dall 	}
47745cb0944cSPaolo Bonzini 	return -ENOIOCTLCMD;
47755cb0944cSPaolo Bonzini }
47765cb0944cSPaolo Bonzini 
47775cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp,
47785cb0944cSPaolo Bonzini 			 unsigned int ioctl, unsigned long arg)
47795cb0944cSPaolo Bonzini {
47805cb0944cSPaolo Bonzini 	struct kvm_vcpu *vcpu = filp->private_data;
47815cb0944cSPaolo Bonzini 	void __user *argp = (void __user *)arg;
47825cb0944cSPaolo Bonzini 	int idx;
47835cb0944cSPaolo Bonzini 	long r;
47848a8378faSJanosch Frank 	u16 rc, rrc;
47859b062471SChristoffer Dall 
47869b062471SChristoffer Dall 	vcpu_load(vcpu);
47879b062471SChristoffer Dall 
47889b062471SChristoffer Dall 	switch (ioctl) {
4789b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
4790800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
479155680890SChristian Borntraeger 		r = kvm_s390_store_status_unloaded(vcpu, arg);
4792800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
4793bc923cc9SAvi Kivity 		break;
4794b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
4795b0c632dbSHeiko Carstens 		psw_t psw;
4796b0c632dbSHeiko Carstens 
4797bc923cc9SAvi Kivity 		r = -EFAULT;
4798b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
4799bc923cc9SAvi Kivity 			break;
4800bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
4801bc923cc9SAvi Kivity 		break;
4802b0c632dbSHeiko Carstens 	}
48037de3f142SJanosch Frank 	case KVM_S390_CLEAR_RESET:
48047de3f142SJanosch Frank 		r = 0;
48057de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_clear_reset(vcpu);
48068a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
48078a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
48088a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
48098a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
48108a8378faSJanosch Frank 				   rc, rrc);
48118a8378faSJanosch Frank 		}
48127de3f142SJanosch Frank 		break;
4813b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
48147de3f142SJanosch Frank 		r = 0;
48157de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_initial_reset(vcpu);
48168a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
48178a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
48188a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET_INITIAL,
48198a8378faSJanosch Frank 					  &rc, &rrc);
48208a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
48218a8378faSJanosch Frank 				   rc, rrc);
48228a8378faSJanosch Frank 		}
48237de3f142SJanosch Frank 		break;
48247de3f142SJanosch Frank 	case KVM_S390_NORMAL_RESET:
48257de3f142SJanosch Frank 		r = 0;
48267de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_normal_reset(vcpu);
48278a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
48288a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
48298a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET, &rc, &rrc);
48308a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
48318a8378faSJanosch Frank 				   rc, rrc);
48328a8378faSJanosch Frank 		}
4833bc923cc9SAvi Kivity 		break;
483414eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
483514eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
483614eebd91SCarsten Otte 		struct kvm_one_reg reg;
483768cf7b1fSJanosch Frank 		r = -EINVAL;
483868cf7b1fSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu))
483968cf7b1fSJanosch Frank 			break;
484014eebd91SCarsten Otte 		r = -EFAULT;
484114eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
484214eebd91SCarsten Otte 			break;
484314eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
484414eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
484514eebd91SCarsten Otte 		else
484614eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
484714eebd91SCarsten Otte 		break;
484814eebd91SCarsten Otte 	}
484927e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
485027e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
485127e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
485227e0393fSCarsten Otte 
485327e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
485427e0393fSCarsten Otte 			r = -EFAULT;
485527e0393fSCarsten Otte 			break;
485627e0393fSCarsten Otte 		}
485727e0393fSCarsten Otte 
485827e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
485927e0393fSCarsten Otte 			r = -EINVAL;
486027e0393fSCarsten Otte 			break;
486127e0393fSCarsten Otte 		}
486227e0393fSCarsten Otte 
486327e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
486427e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
486527e0393fSCarsten Otte 		break;
486627e0393fSCarsten Otte 	}
486727e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
486827e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
486927e0393fSCarsten Otte 
487027e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
487127e0393fSCarsten Otte 			r = -EFAULT;
487227e0393fSCarsten Otte 			break;
487327e0393fSCarsten Otte 		}
487427e0393fSCarsten Otte 
487527e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
487627e0393fSCarsten Otte 			r = -EINVAL;
487727e0393fSCarsten Otte 			break;
487827e0393fSCarsten Otte 		}
487927e0393fSCarsten Otte 
488027e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
488127e0393fSCarsten Otte 			ucasmap.length);
488227e0393fSCarsten Otte 		break;
488327e0393fSCarsten Otte 	}
488427e0393fSCarsten Otte #endif
4885ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
4886527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
4887ccc7910fSCarsten Otte 		break;
4888ccc7910fSCarsten Otte 	}
4889d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
4890d6712df9SCornelia Huck 	{
4891d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
4892d6712df9SCornelia Huck 		r = -EFAULT;
4893d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
4894d6712df9SCornelia Huck 			break;
4895d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4896d6712df9SCornelia Huck 		break;
4897d6712df9SCornelia Huck 	}
489841408c28SThomas Huth 	case KVM_S390_MEM_OP: {
489941408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
490041408c28SThomas Huth 
490141408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
490219e12277SJanosch Frank 			r = kvm_s390_guest_memsida_op(vcpu, &mem_op);
490341408c28SThomas Huth 		else
490441408c28SThomas Huth 			r = -EFAULT;
490541408c28SThomas Huth 		break;
490641408c28SThomas Huth 	}
4907816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
4908816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
4909816c7667SJens Freimann 
4910816c7667SJens Freimann 		r = -EFAULT;
4911816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4912816c7667SJens Freimann 			break;
4913816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
4914816c7667SJens Freimann 		    irq_state.len == 0 ||
4915816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
4916816c7667SJens Freimann 			r = -EINVAL;
4917816c7667SJens Freimann 			break;
4918816c7667SJens Freimann 		}
4919bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
4920816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
4921816c7667SJens Freimann 					   (void __user *) irq_state.buf,
4922816c7667SJens Freimann 					   irq_state.len);
4923816c7667SJens Freimann 		break;
4924816c7667SJens Freimann 	}
4925816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
4926816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
4927816c7667SJens Freimann 
4928816c7667SJens Freimann 		r = -EFAULT;
4929816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4930816c7667SJens Freimann 			break;
4931816c7667SJens Freimann 		if (irq_state.len == 0) {
4932816c7667SJens Freimann 			r = -EINVAL;
4933816c7667SJens Freimann 			break;
4934816c7667SJens Freimann 		}
4935bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
4936816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
4937816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
4938816c7667SJens Freimann 					   irq_state.len);
4939816c7667SJens Freimann 		break;
4940816c7667SJens Freimann 	}
4941b0c632dbSHeiko Carstens 	default:
49423e6afcf1SCarsten Otte 		r = -ENOTTY;
4943b0c632dbSHeiko Carstens 	}
49449b062471SChristoffer Dall 
49459b062471SChristoffer Dall 	vcpu_put(vcpu);
4946bc923cc9SAvi Kivity 	return r;
4947b0c632dbSHeiko Carstens }
4948b0c632dbSHeiko Carstens 
49491499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
49505b1c1493SCarsten Otte {
49515b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
49525b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
49535b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
49545b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
49555b1c1493SCarsten Otte 		get_page(vmf->page);
49565b1c1493SCarsten Otte 		return 0;
49575b1c1493SCarsten Otte 	}
49585b1c1493SCarsten Otte #endif
49595b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
49605b1c1493SCarsten Otte }
49615b1c1493SCarsten Otte 
4962b0c632dbSHeiko Carstens /* Section: memory related */
4963f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
4964f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
496509170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
49667b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
4967b0c632dbSHeiko Carstens {
4968dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
4969dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
4970dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
4971dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
4972b0c632dbSHeiko Carstens 
4973598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
4974b0c632dbSHeiko Carstens 		return -EINVAL;
4975b0c632dbSHeiko Carstens 
4976598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
4977b0c632dbSHeiko Carstens 		return -EINVAL;
4978b0c632dbSHeiko Carstens 
4979a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
4980a3a92c31SDominik Dingel 		return -EINVAL;
4981a3a92c31SDominik Dingel 
498229b40f10SJanosch Frank 	/* When we are protected, we should not change the memory slots */
498329b40f10SJanosch Frank 	if (kvm_s390_pv_get_handle(kvm))
498429b40f10SJanosch Frank 		return -EINVAL;
4985f7784b8eSMarcelo Tosatti 	return 0;
4986f7784b8eSMarcelo Tosatti }
4987f7784b8eSMarcelo Tosatti 
4988f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
498909170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
49909d4c197cSSean Christopherson 				struct kvm_memory_slot *old,
4991f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
49928482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
4993f7784b8eSMarcelo Tosatti {
499419ec166cSChristian Borntraeger 	int rc = 0;
4995f7784b8eSMarcelo Tosatti 
499619ec166cSChristian Borntraeger 	switch (change) {
499719ec166cSChristian Borntraeger 	case KVM_MR_DELETE:
499819ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
499919ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
500019ec166cSChristian Borntraeger 		break;
500119ec166cSChristian Borntraeger 	case KVM_MR_MOVE:
500219ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
500319ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
500419ec166cSChristian Borntraeger 		if (rc)
500519ec166cSChristian Borntraeger 			break;
50063b684a42SJoe Perches 		fallthrough;
500719ec166cSChristian Borntraeger 	case KVM_MR_CREATE:
5008598841caSCarsten Otte 		rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
5009598841caSCarsten Otte 				      mem->guest_phys_addr, mem->memory_size);
501019ec166cSChristian Borntraeger 		break;
501119ec166cSChristian Borntraeger 	case KVM_MR_FLAGS_ONLY:
501219ec166cSChristian Borntraeger 		break;
501319ec166cSChristian Borntraeger 	default:
501419ec166cSChristian Borntraeger 		WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
501519ec166cSChristian Borntraeger 	}
5016598841caSCarsten Otte 	if (rc)
5017ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
5018598841caSCarsten Otte 	return;
5019b0c632dbSHeiko Carstens }
5020b0c632dbSHeiko Carstens 
502160a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
502260a37709SAlexander Yarygin {
502360a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
502460a37709SAlexander Yarygin 
502560a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
502660a37709SAlexander Yarygin }
502760a37709SAlexander Yarygin 
50283491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
50293491caf2SChristian Borntraeger {
50303491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
50313491caf2SChristian Borntraeger }
50323491caf2SChristian Borntraeger 
5033b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
5034b0c632dbSHeiko Carstens {
503560a37709SAlexander Yarygin 	int i;
503660a37709SAlexander Yarygin 
503707197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
50388d43d570SMichael Mueller 		pr_info("SIE is not available\n");
503907197fd0SDavid Hildenbrand 		return -ENODEV;
504007197fd0SDavid Hildenbrand 	}
504107197fd0SDavid Hildenbrand 
5042a4499382SJanosch Frank 	if (nested && hpage) {
50438d43d570SMichael Mueller 		pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
5044a4499382SJanosch Frank 		return -EINVAL;
5045a4499382SJanosch Frank 	}
5046a4499382SJanosch Frank 
504760a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
5048c3b9e3e1SChristian Borntraeger 		kvm_s390_fac_base[i] |=
504960a37709SAlexander Yarygin 			S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
505060a37709SAlexander Yarygin 
50519d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
5052b0c632dbSHeiko Carstens }
5053b0c632dbSHeiko Carstens 
5054b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
5055b0c632dbSHeiko Carstens {
5056b0c632dbSHeiko Carstens 	kvm_exit();
5057b0c632dbSHeiko Carstens }
5058b0c632dbSHeiko Carstens 
5059b0c632dbSHeiko Carstens module_init(kvm_s390_init);
5060b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
5061566af940SCornelia Huck 
5062566af940SCornelia Huck /*
5063566af940SCornelia Huck  * Enable autoloading of the kvm module.
5064566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5065566af940SCornelia Huck  * since x86 takes a different approach.
5066566af940SCornelia Huck  */
5067566af940SCornelia Huck #include <linux/miscdevice.h>
5068566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
5069566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
5070