xref: /linux/arch/s390/kvm/kvm-s390.c (revision 1e753732bda6dcf888ea0b90b2a91ac1c1a0bae9)
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 
61fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
62fcfe1baeSJing Zhang 	KVM_GENERIC_VM_STATS(),
63fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_io),
64fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_float_mchk),
65fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_pfault_done),
66fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_service_signal),
67fcfe1baeSJing Zhang 	STATS_DESC_COUNTER(VM, inject_virtio)
68fcfe1baeSJing Zhang };
69fcfe1baeSJing Zhang static_assert(ARRAY_SIZE(kvm_vm_stats_desc) ==
70fcfe1baeSJing Zhang 		sizeof(struct kvm_vm_stat) / sizeof(u64));
71fcfe1baeSJing Zhang 
72fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = {
73fcfe1baeSJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
74fcfe1baeSJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
75fcfe1baeSJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
76fcfe1baeSJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
77fcfe1baeSJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
78fcfe1baeSJing Zhang 		       sizeof(kvm_vm_stats_desc),
79fcfe1baeSJing Zhang };
80fcfe1baeSJing Zhang 
81ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
82ce55c049SJing Zhang 	KVM_GENERIC_VCPU_STATS(),
83ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_userspace),
84ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_null),
85ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_external_request),
86ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_io_request),
87ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_external_interrupt),
88ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_stop_request),
89ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_validity),
90ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_instruction),
91ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_pei),
92ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, halt_no_poll_steal),
93ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lctl),
94ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lctlg),
95ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stctl),
96ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stctg),
97ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_program_interruption),
98ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_instr_and_program),
99ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_operation_exception),
100ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_ckc),
101ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_cputm),
102ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_external_call),
103ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_emergency_signal),
104ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_service_signal),
105ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_virtio),
106ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_stop_signal),
107ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_prefix_signal),
108ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_restart_signal),
109ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_program),
110ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_io),
111ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, deliver_machine_check),
112ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, exit_wait_state),
113ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_ckc),
114ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_cputm),
115ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_external_call),
116ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_emergency_signal),
117ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_mchk),
118ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_pfault_init),
119ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_program),
120ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_restart),
121ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_set_prefix),
122ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, inject_stop_signal),
123ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_epsw),
124ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_gs),
125ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_io_other),
126ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lpsw),
127ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_lpswe),
128ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_pfmf),
129ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_ptff),
130ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sck),
131ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sckpf),
132ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stidp),
133ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_spx),
134ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stpx),
135ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stap),
136ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_iske),
137ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_ri),
138ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_rrbe),
139ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sske),
140ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock),
141ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stsi),
142ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_stfl),
143ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tb),
144ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tpi),
145ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tprot),
146ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_tsch),
147ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sie),
148ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_essa),
149ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sthyi),
150ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_sense),
151ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running),
152ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call),
153ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency),
154ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency),
155ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_start),
156ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_stop),
157ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status),
158ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status),
159ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status),
160ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_arch),
161ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix),
162ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_restart),
163ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset),
164ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset),
165ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown),
166bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_10),
167bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_44),
168bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c),
169bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, diag_9c_ignored),
170bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, diag_9c_forward),
171bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_258),
172bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_308),
173bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_500),
174bb000f64SChristian Borntraeger 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_other),
175ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pfault_sync)
176ce55c049SJing Zhang };
177ce55c049SJing Zhang static_assert(ARRAY_SIZE(kvm_vcpu_stats_desc) ==
178ce55c049SJing Zhang 		sizeof(struct kvm_vcpu_stat) / sizeof(u64));
179ce55c049SJing Zhang 
180ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = {
181ce55c049SJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
182ce55c049SJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
183ce55c049SJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
184ce55c049SJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
185ce55c049SJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
186ce55c049SJing Zhang 		       sizeof(kvm_vcpu_stats_desc),
187ce55c049SJing Zhang };
188ce55c049SJing Zhang 
189a411edf1SDavid Hildenbrand /* allow nested virtualization in KVM (if enabled by user space) */
190a411edf1SDavid Hildenbrand static int nested;
191a411edf1SDavid Hildenbrand module_param(nested, int, S_IRUGO);
192a411edf1SDavid Hildenbrand MODULE_PARM_DESC(nested, "Nested virtualization support");
193a411edf1SDavid Hildenbrand 
194a4499382SJanosch Frank /* allow 1m huge page guest backing, if !nested */
195a4499382SJanosch Frank static int hpage;
196a4499382SJanosch Frank module_param(hpage, int, 0444);
197a4499382SJanosch Frank MODULE_PARM_DESC(hpage, "1m huge page backing support");
198b0c632dbSHeiko Carstens 
1998b905d28SChristian Borntraeger /* maximum percentage of steal time for polling.  >100 is treated like 100 */
2008b905d28SChristian Borntraeger static u8 halt_poll_max_steal = 10;
2018b905d28SChristian Borntraeger module_param(halt_poll_max_steal, byte, 0644);
202b41fb528SWei Yongjun MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
2038b905d28SChristian Borntraeger 
204cc674ef2SMichael Mueller /* if set to true, the GISA will be initialized and used if available */
205cc674ef2SMichael Mueller static bool use_gisa  = true;
206cc674ef2SMichael Mueller module_param(use_gisa, bool, 0644);
207cc674ef2SMichael Mueller MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
208cc674ef2SMichael Mueller 
20987e28a15SPierre Morel /* maximum diag9c forwarding per second */
21087e28a15SPierre Morel unsigned int diag9c_forwarding_hz;
21187e28a15SPierre Morel module_param(diag9c_forwarding_hz, uint, 0644);
21287e28a15SPierre Morel MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off");
21387e28a15SPierre Morel 
214c3b9e3e1SChristian Borntraeger /*
215c3b9e3e1SChristian Borntraeger  * For now we handle at most 16 double words as this is what the s390 base
216c3b9e3e1SChristian Borntraeger  * kernel handles and stores in the prefix page. If we ever need to go beyond
217c3b9e3e1SChristian Borntraeger  * this, this requires changes to code, but the external uapi can stay.
218c3b9e3e1SChristian Borntraeger  */
219c3b9e3e1SChristian Borntraeger #define SIZE_INTERNAL 16
220c3b9e3e1SChristian Borntraeger 
221c3b9e3e1SChristian Borntraeger /*
222c3b9e3e1SChristian Borntraeger  * Base feature mask that defines default mask for facilities. Consists of the
223c3b9e3e1SChristian Borntraeger  * defines in FACILITIES_KVM and the non-hypervisor managed bits.
224c3b9e3e1SChristian Borntraeger  */
225c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
226c3b9e3e1SChristian Borntraeger /*
227c3b9e3e1SChristian Borntraeger  * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
228c3b9e3e1SChristian Borntraeger  * and defines the facilities that can be enabled via a cpu model.
229c3b9e3e1SChristian Borntraeger  */
230c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
231c3b9e3e1SChristian Borntraeger 
232c3b9e3e1SChristian Borntraeger static unsigned long kvm_s390_fac_size(void)
23378c4b59fSMichael Mueller {
234c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
235c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
236c3b9e3e1SChristian Borntraeger 	BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
23717e89e13SSven Schnelle 		sizeof(stfle_fac_list));
238c3b9e3e1SChristian Borntraeger 
239c3b9e3e1SChristian Borntraeger 	return SIZE_INTERNAL;
24078c4b59fSMichael Mueller }
24178c4b59fSMichael Mueller 
24215c9705fSDavid Hildenbrand /* available cpu features supported by kvm */
24315c9705fSDavid Hildenbrand static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
2440a763c78SDavid Hildenbrand /* available subfunctions indicated via query / "test bit" */
2450a763c78SDavid Hildenbrand static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
24615c9705fSDavid Hildenbrand 
2479d8d5786SMichael Mueller static struct gmap_notifier gmap_notifier;
248a3508fbeSDavid Hildenbrand static struct gmap_notifier vsie_gmap_notifier;
24978f26131SChristian Borntraeger debug_info_t *kvm_s390_dbf;
2503e6c5568SJanosch Frank debug_info_t *kvm_s390_dbf_uv;
2519d8d5786SMichael Mueller 
252b0c632dbSHeiko Carstens /* Section: not file related */
25313a34e06SRadim Krčmář int kvm_arch_hardware_enable(void)
254b0c632dbSHeiko Carstens {
255b0c632dbSHeiko Carstens 	/* every s390 is virtualization enabled ;-) */
25610474ae8SAlexander Graf 	return 0;
257b0c632dbSHeiko Carstens }
258b0c632dbSHeiko Carstens 
259b9904085SSean Christopherson int kvm_arch_check_processor_compat(void *opaque)
260f257d6dcSSean Christopherson {
261f257d6dcSSean Christopherson 	return 0;
262f257d6dcSSean Christopherson }
263f257d6dcSSean Christopherson 
26429b40f10SJanosch Frank /* forward declarations */
265414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
266414d3b07SMartin Schwidefsky 			      unsigned long end);
26729b40f10SJanosch Frank static int sca_switch_to_extended(struct kvm *kvm);
2682c70fe44SChristian Borntraeger 
2691575767eSDavid Hildenbrand static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
2701575767eSDavid Hildenbrand {
2711575767eSDavid Hildenbrand 	u8 delta_idx = 0;
2721575767eSDavid Hildenbrand 
2731575767eSDavid Hildenbrand 	/*
2741575767eSDavid Hildenbrand 	 * The TOD jumps by delta, we have to compensate this by adding
2751575767eSDavid Hildenbrand 	 * -delta to the epoch.
2761575767eSDavid Hildenbrand 	 */
2771575767eSDavid Hildenbrand 	delta = -delta;
2781575767eSDavid Hildenbrand 
2791575767eSDavid Hildenbrand 	/* sign-extension - we're adding to signed values below */
2801575767eSDavid Hildenbrand 	if ((s64)delta < 0)
2811575767eSDavid Hildenbrand 		delta_idx = -1;
2821575767eSDavid Hildenbrand 
2831575767eSDavid Hildenbrand 	scb->epoch += delta;
2841575767eSDavid Hildenbrand 	if (scb->ecd & ECD_MEF) {
2851575767eSDavid Hildenbrand 		scb->epdx += delta_idx;
2861575767eSDavid Hildenbrand 		if (scb->epoch < delta)
2871575767eSDavid Hildenbrand 			scb->epdx += 1;
2881575767eSDavid Hildenbrand 	}
2891575767eSDavid Hildenbrand }
2901575767eSDavid Hildenbrand 
291fdf03650SFan Zhang /*
292fdf03650SFan Zhang  * This callback is executed during stop_machine(). All CPUs are therefore
293fdf03650SFan Zhang  * temporarily stopped. In order not to change guest behavior, we have to
294fdf03650SFan Zhang  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
295fdf03650SFan Zhang  * so a CPU won't be stopped while calculating with the epoch.
296fdf03650SFan Zhang  */
297fdf03650SFan Zhang static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
298fdf03650SFan Zhang 			  void *v)
299fdf03650SFan Zhang {
300fdf03650SFan Zhang 	struct kvm *kvm;
301fdf03650SFan Zhang 	struct kvm_vcpu *vcpu;
302fdf03650SFan Zhang 	int i;
303fdf03650SFan Zhang 	unsigned long long *delta = v;
304fdf03650SFan Zhang 
305fdf03650SFan Zhang 	list_for_each_entry(kvm, &vm_list, vm_list) {
306fdf03650SFan Zhang 		kvm_for_each_vcpu(i, vcpu, kvm) {
3071575767eSDavid Hildenbrand 			kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
3081575767eSDavid Hildenbrand 			if (i == 0) {
3091575767eSDavid Hildenbrand 				kvm->arch.epoch = vcpu->arch.sie_block->epoch;
3101575767eSDavid Hildenbrand 				kvm->arch.epdx = vcpu->arch.sie_block->epdx;
3111575767eSDavid Hildenbrand 			}
312db0758b2SDavid Hildenbrand 			if (vcpu->arch.cputm_enabled)
313db0758b2SDavid Hildenbrand 				vcpu->arch.cputm_start += *delta;
31491473b48SDavid Hildenbrand 			if (vcpu->arch.vsie_block)
3151575767eSDavid Hildenbrand 				kvm_clock_sync_scb(vcpu->arch.vsie_block,
3161575767eSDavid Hildenbrand 						   *delta);
317fdf03650SFan Zhang 		}
318fdf03650SFan Zhang 	}
319fdf03650SFan Zhang 	return NOTIFY_OK;
320fdf03650SFan Zhang }
321fdf03650SFan Zhang 
322fdf03650SFan Zhang static struct notifier_block kvm_clock_notifier = {
323fdf03650SFan Zhang 	.notifier_call = kvm_clock_sync,
324fdf03650SFan Zhang };
325fdf03650SFan Zhang 
326b9904085SSean Christopherson int kvm_arch_hardware_setup(void *opaque)
327b0c632dbSHeiko Carstens {
3282c70fe44SChristian Borntraeger 	gmap_notifier.notifier_call = kvm_gmap_notifier;
329b2d73b2aSMartin Schwidefsky 	gmap_register_pte_notifier(&gmap_notifier);
330a3508fbeSDavid Hildenbrand 	vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
331a3508fbeSDavid Hildenbrand 	gmap_register_pte_notifier(&vsie_gmap_notifier);
332fdf03650SFan Zhang 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
333fdf03650SFan Zhang 				       &kvm_clock_notifier);
334b0c632dbSHeiko Carstens 	return 0;
335b0c632dbSHeiko Carstens }
336b0c632dbSHeiko Carstens 
337b0c632dbSHeiko Carstens void kvm_arch_hardware_unsetup(void)
338b0c632dbSHeiko Carstens {
339b2d73b2aSMartin Schwidefsky 	gmap_unregister_pte_notifier(&gmap_notifier);
340a3508fbeSDavid Hildenbrand 	gmap_unregister_pte_notifier(&vsie_gmap_notifier);
341fdf03650SFan Zhang 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
342fdf03650SFan Zhang 					 &kvm_clock_notifier);
343b0c632dbSHeiko Carstens }
344b0c632dbSHeiko Carstens 
34522be5a13SDavid Hildenbrand static void allow_cpu_feat(unsigned long nr)
34622be5a13SDavid Hildenbrand {
34722be5a13SDavid Hildenbrand 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
34822be5a13SDavid Hildenbrand }
34922be5a13SDavid Hildenbrand 
3500a763c78SDavid Hildenbrand static inline int plo_test_bit(unsigned char nr)
3510a763c78SDavid Hildenbrand {
3524fa3b91bSHeiko Carstens 	unsigned long function = (unsigned long)nr | 0x100;
353d051ae53SHeiko Carstens 	int cc;
3540a763c78SDavid Hildenbrand 
3550a763c78SDavid Hildenbrand 	asm volatile(
3564fa3b91bSHeiko Carstens 		"	lgr	0,%[function]\n"
3570a763c78SDavid Hildenbrand 		/* Parameter registers are ignored for "test bit" */
3580a763c78SDavid Hildenbrand 		"	plo	0,0,0,0(0)\n"
3590a763c78SDavid Hildenbrand 		"	ipm	%0\n"
3600a763c78SDavid Hildenbrand 		"	srl	%0,28\n"
3610a763c78SDavid Hildenbrand 		: "=d" (cc)
3624fa3b91bSHeiko Carstens 		: [function] "d" (function)
3634fa3b91bSHeiko Carstens 		: "cc", "0");
3640a763c78SDavid Hildenbrand 	return cc == 0;
3650a763c78SDavid Hildenbrand }
3660a763c78SDavid Hildenbrand 
367d0dea733SHeiko Carstens static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
368d6681397SChristian Borntraeger {
369d6681397SChristian Borntraeger 	asm volatile(
3704fa3b91bSHeiko Carstens 		"	lghi	0,0\n"
3714fa3b91bSHeiko Carstens 		"	lgr	1,%[query]\n"
3724fa3b91bSHeiko Carstens 		/* Parameter registers are ignored */
373d6681397SChristian Borntraeger 		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
374b1c41ac3SHeiko Carstens 		:
3754fa3b91bSHeiko Carstens 		: [query] "d" ((unsigned long)query), [opc] "i" (opcode)
3764fa3b91bSHeiko Carstens 		: "cc", "memory", "0", "1");
377d6681397SChristian Borntraeger }
378d6681397SChristian Borntraeger 
379173aec2dSChristian Borntraeger #define INSN_SORTL 0xb938
3804f45b90eSChristian Borntraeger #define INSN_DFLTCC 0xb939
381173aec2dSChristian Borntraeger 
38222be5a13SDavid Hildenbrand static void kvm_s390_cpu_feat_init(void)
38322be5a13SDavid Hildenbrand {
3840a763c78SDavid Hildenbrand 	int i;
3850a763c78SDavid Hildenbrand 
3860a763c78SDavid Hildenbrand 	for (i = 0; i < 256; ++i) {
3870a763c78SDavid Hildenbrand 		if (plo_test_bit(i))
3880a763c78SDavid Hildenbrand 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
3890a763c78SDavid Hildenbrand 	}
3900a763c78SDavid Hildenbrand 
3910a763c78SDavid Hildenbrand 	if (test_facility(28)) /* TOD-clock steering */
392221bb8a4SLinus Torvalds 		ptff(kvm_s390_available_subfunc.ptff,
393221bb8a4SLinus Torvalds 		     sizeof(kvm_s390_available_subfunc.ptff),
394221bb8a4SLinus Torvalds 		     PTFF_QAF);
3950a763c78SDavid Hildenbrand 
3960a763c78SDavid Hildenbrand 	if (test_facility(17)) { /* MSA */
39769c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
39869c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmac);
39969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
40069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmc);
40169c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
40269c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.km);
40369c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
40469c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kimd);
40569c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
40669c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.klmd);
4070a763c78SDavid Hildenbrand 	}
4080a763c78SDavid Hildenbrand 	if (test_facility(76)) /* MSA3 */
40969c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
41069c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pckmo);
4110a763c78SDavid Hildenbrand 	if (test_facility(77)) { /* MSA4 */
41269c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
41369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmctr);
41469c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
41569c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmf);
41669c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
41769c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.kmo);
41869c0e360SMartin Schwidefsky 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
41969c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.pcc);
4200a763c78SDavid Hildenbrand 	}
4210a763c78SDavid Hildenbrand 	if (test_facility(57)) /* MSA5 */
422985a9d20SHarald Freudenberger 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
42369c0e360SMartin Schwidefsky 			      kvm_s390_available_subfunc.ppno);
4240a763c78SDavid Hildenbrand 
425e000b8e0SJason J. Herne 	if (test_facility(146)) /* MSA8 */
426e000b8e0SJason J. Herne 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
427e000b8e0SJason J. Herne 			      kvm_s390_available_subfunc.kma);
428e000b8e0SJason J. Herne 
42913209ad0SChristian Borntraeger 	if (test_facility(155)) /* MSA9 */
43013209ad0SChristian Borntraeger 		__cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
43113209ad0SChristian Borntraeger 			      kvm_s390_available_subfunc.kdsa);
43213209ad0SChristian Borntraeger 
433173aec2dSChristian Borntraeger 	if (test_facility(150)) /* SORTL */
434173aec2dSChristian Borntraeger 		__insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
435173aec2dSChristian Borntraeger 
4364f45b90eSChristian Borntraeger 	if (test_facility(151)) /* DFLTCC */
4374f45b90eSChristian Borntraeger 		__insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
4384f45b90eSChristian Borntraeger 
43922be5a13SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
44022be5a13SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
441a3508fbeSDavid Hildenbrand 	/*
442a3508fbeSDavid Hildenbrand 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
443a3508fbeSDavid Hildenbrand 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
444a3508fbeSDavid Hildenbrand 	 */
445a3508fbeSDavid Hildenbrand 	if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
446a411edf1SDavid Hildenbrand 	    !test_facility(3) || !nested)
447a3508fbeSDavid Hildenbrand 		return;
448a3508fbeSDavid Hildenbrand 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
44919c439b5SDavid Hildenbrand 	if (sclp.has_64bscao)
45019c439b5SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
4510615a326SDavid Hildenbrand 	if (sclp.has_siif)
4520615a326SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
45377d18f6dSDavid Hildenbrand 	if (sclp.has_gpere)
45477d18f6dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
455a1b7b9b2SDavid Hildenbrand 	if (sclp.has_gsls)
456a1b7b9b2SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
4575630a8e8SDavid Hildenbrand 	if (sclp.has_ib)
4585630a8e8SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
45913ee3f67SDavid Hildenbrand 	if (sclp.has_cei)
46013ee3f67SDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
4617fd7f39dSDavid Hildenbrand 	if (sclp.has_ibs)
4627fd7f39dSDavid Hildenbrand 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
463730cd632SFarhan Ali 	if (sclp.has_kss)
464730cd632SFarhan Ali 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
4655d3876a8SDavid Hildenbrand 	/*
4665d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
4675d3876a8SDavid Hildenbrand 	 * all skey handling functions read/set the skey from the PGSTE
4685d3876a8SDavid Hildenbrand 	 * instead of the real storage key.
4695d3876a8SDavid Hildenbrand 	 *
4705d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
4715d3876a8SDavid Hildenbrand 	 * pages being detected as preserved although they are resident.
4725d3876a8SDavid Hildenbrand 	 *
4735d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
4745d3876a8SDavid Hildenbrand 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
4755d3876a8SDavid Hildenbrand 	 *
4765d3876a8SDavid Hildenbrand 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
4775d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
4785d3876a8SDavid Hildenbrand 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
4795d3876a8SDavid Hildenbrand 	 *
4805d3876a8SDavid Hildenbrand 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
4815d3876a8SDavid Hildenbrand 	 * cannot easily shadow the SCA because of the ipte lock.
4825d3876a8SDavid Hildenbrand 	 */
48322be5a13SDavid Hildenbrand }
48422be5a13SDavid Hildenbrand 
485b0c632dbSHeiko Carstens int kvm_arch_init(void *opaque)
486b0c632dbSHeiko Carstens {
487f76f6371SJanosch Frank 	int rc = -ENOMEM;
488308c3e66SMichael Mueller 
48978f26131SChristian Borntraeger 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
49078f26131SChristian Borntraeger 	if (!kvm_s390_dbf)
49178f26131SChristian Borntraeger 		return -ENOMEM;
49278f26131SChristian Borntraeger 
4933e6c5568SJanosch Frank 	kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
4943e6c5568SJanosch Frank 	if (!kvm_s390_dbf_uv)
4953e6c5568SJanosch Frank 		goto out;
4963e6c5568SJanosch Frank 
4973e6c5568SJanosch Frank 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
4983e6c5568SJanosch Frank 	    debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
499f76f6371SJanosch Frank 		goto out;
50078f26131SChristian Borntraeger 
50122be5a13SDavid Hildenbrand 	kvm_s390_cpu_feat_init();
50222be5a13SDavid Hildenbrand 
50384877d93SCornelia Huck 	/* Register floating interrupt controller interface. */
504308c3e66SMichael Mueller 	rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
505308c3e66SMichael Mueller 	if (rc) {
5068d43d570SMichael Mueller 		pr_err("A FLIC registration call failed with rc=%d\n", rc);
507f76f6371SJanosch Frank 		goto out;
508308c3e66SMichael Mueller 	}
509b1d1e76eSMichael Mueller 
510b1d1e76eSMichael Mueller 	rc = kvm_s390_gib_init(GAL_ISC);
511b1d1e76eSMichael Mueller 	if (rc)
512f76f6371SJanosch Frank 		goto out;
513b1d1e76eSMichael Mueller 
514308c3e66SMichael Mueller 	return 0;
515308c3e66SMichael Mueller 
516f76f6371SJanosch Frank out:
517f76f6371SJanosch Frank 	kvm_arch_exit();
518308c3e66SMichael Mueller 	return rc;
519b0c632dbSHeiko Carstens }
520b0c632dbSHeiko Carstens 
52178f26131SChristian Borntraeger void kvm_arch_exit(void)
52278f26131SChristian Borntraeger {
5231282c21eSMichael Mueller 	kvm_s390_gib_destroy();
52478f26131SChristian Borntraeger 	debug_unregister(kvm_s390_dbf);
5253e6c5568SJanosch Frank 	debug_unregister(kvm_s390_dbf_uv);
52678f26131SChristian Borntraeger }
52778f26131SChristian Borntraeger 
528b0c632dbSHeiko Carstens /* Section: device related */
529b0c632dbSHeiko Carstens long kvm_arch_dev_ioctl(struct file *filp,
530b0c632dbSHeiko Carstens 			unsigned int ioctl, unsigned long arg)
531b0c632dbSHeiko Carstens {
532b0c632dbSHeiko Carstens 	if (ioctl == KVM_S390_ENABLE_SIE)
533b0c632dbSHeiko Carstens 		return s390_enable_sie();
534b0c632dbSHeiko Carstens 	return -EINVAL;
535b0c632dbSHeiko Carstens }
536b0c632dbSHeiko Carstens 
537784aa3d7SAlexander Graf int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
538b0c632dbSHeiko Carstens {
539d7b0b5ebSCarsten Otte 	int r;
540d7b0b5ebSCarsten Otte 
5412bd0ac4eSCarsten Otte 	switch (ext) {
542d7b0b5ebSCarsten Otte 	case KVM_CAP_S390_PSW:
543b6cf8788SChristian Borntraeger 	case KVM_CAP_S390_GMAP:
54452e16b18SChristian Borntraeger 	case KVM_CAP_SYNC_MMU:
5451efd0f59SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
5461efd0f59SCarsten Otte 	case KVM_CAP_S390_UCONTROL:
5471efd0f59SCarsten Otte #endif
5483c038e6bSDominik Dingel 	case KVM_CAP_ASYNC_PF:
54960b413c9SChristian Borntraeger 	case KVM_CAP_SYNC_REGS:
55014eebd91SCarsten Otte 	case KVM_CAP_ONE_REG:
551d6712df9SCornelia Huck 	case KVM_CAP_ENABLE_CAP:
552fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
55310ccaa1eSCornelia Huck 	case KVM_CAP_IOEVENTFD:
554c05c4186SJens Freimann 	case KVM_CAP_DEVICE_CTRL:
55578599d90SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
556f2061656SDominik Dingel 	case KVM_CAP_VM_ATTRIBUTES:
5576352e4d2SDavid Hildenbrand 	case KVM_CAP_MP_STATE:
558460df4c1SPaolo Bonzini 	case KVM_CAP_IMMEDIATE_EXIT:
55947b43c52SJens Freimann 	case KVM_CAP_S390_INJECT_IRQ:
5602444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
561e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
56230ee2a98SJason J. Herne 	case KVM_CAP_S390_SKEYS:
563816c7667SJens Freimann 	case KVM_CAP_S390_IRQ_STATE:
5646502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
5654036e387SClaudio Imbrenda 	case KVM_CAP_S390_CMMA_MIGRATION:
56647a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
567da9a1446SChristian Borntraeger 	case KVM_CAP_S390_AIS_MIGRATION:
5687de3f142SJanosch Frank 	case KVM_CAP_S390_VCPU_RESETS:
569b9b2782cSPeter Xu 	case KVM_CAP_SET_GUEST_DEBUG:
57023a60f83SCollin Walling 	case KVM_CAP_S390_DIAG318:
571d7b0b5ebSCarsten Otte 		r = 1;
572d7b0b5ebSCarsten Otte 		break;
573a43b80b7SMaxim Levitsky 	case KVM_CAP_SET_GUEST_DEBUG2:
574a43b80b7SMaxim Levitsky 		r = KVM_GUESTDBG_VALID_MASK;
575a43b80b7SMaxim Levitsky 		break;
576a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
577a4499382SJanosch Frank 		r = 0;
57840ebdb8eSJanosch Frank 		if (hpage && !kvm_is_ucontrol(kvm))
579a4499382SJanosch Frank 			r = 1;
580a4499382SJanosch Frank 		break;
58141408c28SThomas Huth 	case KVM_CAP_S390_MEM_OP:
58241408c28SThomas Huth 		r = MEM_OP_MAX_SIZE;
58341408c28SThomas Huth 		break;
584e726b1bdSChristian Borntraeger 	case KVM_CAP_NR_VCPUS:
585e726b1bdSChristian Borntraeger 	case KVM_CAP_MAX_VCPUS:
586a86cb413SThomas Huth 	case KVM_CAP_MAX_VCPU_ID:
58776a6dd72SDavid Hildenbrand 		r = KVM_S390_BSCA_CPU_SLOTS;
588a6940674SDavid Hildenbrand 		if (!kvm_s390_use_sca_entries())
589a6940674SDavid Hildenbrand 			r = KVM_MAX_VCPUS;
590a6940674SDavid Hildenbrand 		else if (sclp.has_esca && sclp.has_64bscao)
59176a6dd72SDavid Hildenbrand 			r = KVM_S390_ESCA_CPU_SLOTS;
592e726b1bdSChristian Borntraeger 		break;
5931526bf9cSChristian Borntraeger 	case KVM_CAP_S390_COW:
594abf09bedSMartin Schwidefsky 		r = MACHINE_HAS_ESOP;
5951526bf9cSChristian Borntraeger 		break;
59668c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
59768c55750SEric Farman 		r = MACHINE_HAS_VX;
59868c55750SEric Farman 		break;
599c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
600c6e5f166SFan Zhang 		r = test_facility(64);
601c6e5f166SFan Zhang 		break;
6024e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
6034e0b1ab7SFan Zhang 		r = test_facility(133);
6044e0b1ab7SFan Zhang 		break;
60535b3fde6SChristian Borntraeger 	case KVM_CAP_S390_BPB:
60635b3fde6SChristian Borntraeger 		r = test_facility(82);
60735b3fde6SChristian Borntraeger 		break;
60813da9ae1SChristian Borntraeger 	case KVM_CAP_S390_PROTECTED:
60913da9ae1SChristian Borntraeger 		r = is_prot_virt_host();
61013da9ae1SChristian Borntraeger 		break;
6112bd0ac4eSCarsten Otte 	default:
612d7b0b5ebSCarsten Otte 		r = 0;
613b0c632dbSHeiko Carstens 	}
614d7b0b5ebSCarsten Otte 	return r;
6152bd0ac4eSCarsten Otte }
616b0c632dbSHeiko Carstens 
6170dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
61815f36ebdSJason J. Herne {
6190959e168SJanosch Frank 	int i;
62015f36ebdSJason J. Herne 	gfn_t cur_gfn, last_gfn;
6210959e168SJanosch Frank 	unsigned long gaddr, vmaddr;
62215f36ebdSJason J. Herne 	struct gmap *gmap = kvm->arch.gmap;
6230959e168SJanosch Frank 	DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
62415f36ebdSJason J. Herne 
6250959e168SJanosch Frank 	/* Loop over all guest segments */
6260959e168SJanosch Frank 	cur_gfn = memslot->base_gfn;
62715f36ebdSJason J. Herne 	last_gfn = memslot->base_gfn + memslot->npages;
6280959e168SJanosch Frank 	for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
6290959e168SJanosch Frank 		gaddr = gfn_to_gpa(cur_gfn);
6300959e168SJanosch Frank 		vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
6310959e168SJanosch Frank 		if (kvm_is_error_hva(vmaddr))
6320959e168SJanosch Frank 			continue;
63315f36ebdSJason J. Herne 
6340959e168SJanosch Frank 		bitmap_zero(bitmap, _PAGE_ENTRIES);
6350959e168SJanosch Frank 		gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
6360959e168SJanosch Frank 		for (i = 0; i < _PAGE_ENTRIES; i++) {
6370959e168SJanosch Frank 			if (test_bit(i, bitmap))
6380959e168SJanosch Frank 				mark_page_dirty(kvm, cur_gfn + i);
6390959e168SJanosch Frank 		}
6400959e168SJanosch Frank 
6411763f8d0SChristian Borntraeger 		if (fatal_signal_pending(current))
6421763f8d0SChristian Borntraeger 			return;
64370c88a00SChristian Borntraeger 		cond_resched();
64415f36ebdSJason J. Herne 	}
64515f36ebdSJason J. Herne }
64615f36ebdSJason J. Herne 
647b0c632dbSHeiko Carstens /* Section: vm related */
648a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu);
649a6e2f683SEugene (jno) Dvurechenski 
650b0c632dbSHeiko Carstens /*
651b0c632dbSHeiko Carstens  * Get (and clear) the dirty memory log for a memory slot.
652b0c632dbSHeiko Carstens  */
653b0c632dbSHeiko Carstens int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
654b0c632dbSHeiko Carstens 			       struct kvm_dirty_log *log)
655b0c632dbSHeiko Carstens {
65615f36ebdSJason J. Herne 	int r;
65715f36ebdSJason J. Herne 	unsigned long n;
65815f36ebdSJason J. Herne 	struct kvm_memory_slot *memslot;
6592a49f61dSSean Christopherson 	int is_dirty;
66015f36ebdSJason J. Herne 
661e1e8a962SJanosch Frank 	if (kvm_is_ucontrol(kvm))
662e1e8a962SJanosch Frank 		return -EINVAL;
663e1e8a962SJanosch Frank 
66415f36ebdSJason J. Herne 	mutex_lock(&kvm->slots_lock);
66515f36ebdSJason J. Herne 
66615f36ebdSJason J. Herne 	r = -EINVAL;
66715f36ebdSJason J. Herne 	if (log->slot >= KVM_USER_MEM_SLOTS)
66815f36ebdSJason J. Herne 		goto out;
66915f36ebdSJason J. Herne 
6702a49f61dSSean Christopherson 	r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
67115f36ebdSJason J. Herne 	if (r)
67215f36ebdSJason J. Herne 		goto out;
67315f36ebdSJason J. Herne 
67415f36ebdSJason J. Herne 	/* Clear the dirty log */
67515f36ebdSJason J. Herne 	if (is_dirty) {
67615f36ebdSJason J. Herne 		n = kvm_dirty_bitmap_bytes(memslot);
67715f36ebdSJason J. Herne 		memset(memslot->dirty_bitmap, 0, n);
67815f36ebdSJason J. Herne 	}
67915f36ebdSJason J. Herne 	r = 0;
68015f36ebdSJason J. Herne out:
68115f36ebdSJason J. Herne 	mutex_unlock(&kvm->slots_lock);
68215f36ebdSJason J. Herne 	return r;
683b0c632dbSHeiko Carstens }
684b0c632dbSHeiko Carstens 
6856502a34cSDavid Hildenbrand static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
6866502a34cSDavid Hildenbrand {
6876502a34cSDavid Hildenbrand 	unsigned int i;
6886502a34cSDavid Hildenbrand 	struct kvm_vcpu *vcpu;
6896502a34cSDavid Hildenbrand 
6906502a34cSDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
6916502a34cSDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
6926502a34cSDavid Hildenbrand 	}
6936502a34cSDavid Hildenbrand }
6946502a34cSDavid Hildenbrand 
695e5d83c74SPaolo Bonzini int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
696d938dc55SCornelia Huck {
697d938dc55SCornelia Huck 	int r;
698d938dc55SCornelia Huck 
699d938dc55SCornelia Huck 	if (cap->flags)
700d938dc55SCornelia Huck 		return -EINVAL;
701d938dc55SCornelia Huck 
702d938dc55SCornelia Huck 	switch (cap->cap) {
70384223598SCornelia Huck 	case KVM_CAP_S390_IRQCHIP:
704c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
70584223598SCornelia Huck 		kvm->arch.use_irqchip = 1;
70684223598SCornelia Huck 		r = 0;
70784223598SCornelia Huck 		break;
7082444b352SDavid Hildenbrand 	case KVM_CAP_S390_USER_SIGP:
709c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
7102444b352SDavid Hildenbrand 		kvm->arch.user_sigp = 1;
7112444b352SDavid Hildenbrand 		r = 0;
7122444b352SDavid Hildenbrand 		break;
71368c55750SEric Farman 	case KVM_CAP_S390_VECTOR_REGISTERS:
7145967c17bSDavid Hildenbrand 		mutex_lock(&kvm->lock);
715a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
7165967c17bSDavid Hildenbrand 			r = -EBUSY;
7175967c17bSDavid Hildenbrand 		} else if (MACHINE_HAS_VX) {
718c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
719c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 129);
7202f87d942SGuenther Hutzl 			if (test_facility(134)) {
7212f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
7222f87d942SGuenther Hutzl 				set_kvm_facility(kvm->arch.model.fac_list, 134);
7232f87d942SGuenther Hutzl 			}
72453743aa7SMaxim Samoylov 			if (test_facility(135)) {
72553743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
72653743aa7SMaxim Samoylov 				set_kvm_facility(kvm->arch.model.fac_list, 135);
72753743aa7SMaxim Samoylov 			}
7287832e91cSChristian Borntraeger 			if (test_facility(148)) {
7297832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 148);
7307832e91cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 148);
7317832e91cSChristian Borntraeger 			}
732d5cb6ab1SChristian Borntraeger 			if (test_facility(152)) {
733d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 152);
734d5cb6ab1SChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 152);
735d5cb6ab1SChristian Borntraeger 			}
7361f703d2cSChristian Borntraeger 			if (test_facility(192)) {
7371f703d2cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_mask, 192);
7381f703d2cSChristian Borntraeger 				set_kvm_facility(kvm->arch.model.fac_list, 192);
7391f703d2cSChristian Borntraeger 			}
74018280d8bSMichael Mueller 			r = 0;
74118280d8bSMichael Mueller 		} else
74218280d8bSMichael Mueller 			r = -EINVAL;
7435967c17bSDavid Hildenbrand 		mutex_unlock(&kvm->lock);
744c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
745c92ea7b9SChristian Borntraeger 			 r ? "(not available)" : "(success)");
74668c55750SEric Farman 		break;
747c6e5f166SFan Zhang 	case KVM_CAP_S390_RI:
748c6e5f166SFan Zhang 		r = -EINVAL;
749c6e5f166SFan Zhang 		mutex_lock(&kvm->lock);
750a03825bbSPaolo Bonzini 		if (kvm->created_vcpus) {
751c6e5f166SFan Zhang 			r = -EBUSY;
752c6e5f166SFan Zhang 		} else if (test_facility(64)) {
753c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
754c54f0d6aSDavid Hildenbrand 			set_kvm_facility(kvm->arch.model.fac_list, 64);
755c6e5f166SFan Zhang 			r = 0;
756c6e5f166SFan Zhang 		}
757c6e5f166SFan Zhang 		mutex_unlock(&kvm->lock);
758c6e5f166SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
759c6e5f166SFan Zhang 			 r ? "(not available)" : "(success)");
760c6e5f166SFan Zhang 		break;
76147a4693eSYi Min Zhao 	case KVM_CAP_S390_AIS:
76247a4693eSYi Min Zhao 		mutex_lock(&kvm->lock);
76347a4693eSYi Min Zhao 		if (kvm->created_vcpus) {
76447a4693eSYi Min Zhao 			r = -EBUSY;
76547a4693eSYi Min Zhao 		} else {
76647a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
76747a4693eSYi Min Zhao 			set_kvm_facility(kvm->arch.model.fac_list, 72);
76847a4693eSYi Min Zhao 			r = 0;
76947a4693eSYi Min Zhao 		}
77047a4693eSYi Min Zhao 		mutex_unlock(&kvm->lock);
77147a4693eSYi Min Zhao 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
77247a4693eSYi Min Zhao 			 r ? "(not available)" : "(success)");
77347a4693eSYi Min Zhao 		break;
7744e0b1ab7SFan Zhang 	case KVM_CAP_S390_GS:
7754e0b1ab7SFan Zhang 		r = -EINVAL;
7764e0b1ab7SFan Zhang 		mutex_lock(&kvm->lock);
777241e3ec0SChristian Borntraeger 		if (kvm->created_vcpus) {
7784e0b1ab7SFan Zhang 			r = -EBUSY;
7794e0b1ab7SFan Zhang 		} else if (test_facility(133)) {
7804e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
7814e0b1ab7SFan Zhang 			set_kvm_facility(kvm->arch.model.fac_list, 133);
7824e0b1ab7SFan Zhang 			r = 0;
7834e0b1ab7SFan Zhang 		}
7844e0b1ab7SFan Zhang 		mutex_unlock(&kvm->lock);
7854e0b1ab7SFan Zhang 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
7864e0b1ab7SFan Zhang 			 r ? "(not available)" : "(success)");
7874e0b1ab7SFan Zhang 		break;
788a4499382SJanosch Frank 	case KVM_CAP_S390_HPAGE_1M:
789a4499382SJanosch Frank 		mutex_lock(&kvm->lock);
790a4499382SJanosch Frank 		if (kvm->created_vcpus)
791a4499382SJanosch Frank 			r = -EBUSY;
79240ebdb8eSJanosch Frank 		else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
793a4499382SJanosch Frank 			r = -EINVAL;
794a4499382SJanosch Frank 		else {
795a4499382SJanosch Frank 			r = 0;
796d8ed45c5SMichel Lespinasse 			mmap_write_lock(kvm->mm);
797a4499382SJanosch Frank 			kvm->mm->context.allow_gmap_hpage_1m = 1;
798d8ed45c5SMichel Lespinasse 			mmap_write_unlock(kvm->mm);
799a4499382SJanosch Frank 			/*
800a4499382SJanosch Frank 			 * We might have to create fake 4k page
801a4499382SJanosch Frank 			 * tables. To avoid that the hardware works on
802a4499382SJanosch Frank 			 * stale PGSTEs, we emulate these instructions.
803a4499382SJanosch Frank 			 */
804a4499382SJanosch Frank 			kvm->arch.use_skf = 0;
805a4499382SJanosch Frank 			kvm->arch.use_pfmfi = 0;
806a4499382SJanosch Frank 		}
807a4499382SJanosch Frank 		mutex_unlock(&kvm->lock);
808a4499382SJanosch Frank 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
809a4499382SJanosch Frank 			 r ? "(not available)" : "(success)");
810a4499382SJanosch Frank 		break;
811e44fc8c9SEkaterina Tumanova 	case KVM_CAP_S390_USER_STSI:
812c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
813e44fc8c9SEkaterina Tumanova 		kvm->arch.user_stsi = 1;
814e44fc8c9SEkaterina Tumanova 		r = 0;
815e44fc8c9SEkaterina Tumanova 		break;
8166502a34cSDavid Hildenbrand 	case KVM_CAP_S390_USER_INSTR0:
8176502a34cSDavid Hildenbrand 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
8186502a34cSDavid Hildenbrand 		kvm->arch.user_instr0 = 1;
8196502a34cSDavid Hildenbrand 		icpt_operexc_on_all_vcpus(kvm);
8206502a34cSDavid Hildenbrand 		r = 0;
8216502a34cSDavid Hildenbrand 		break;
822d938dc55SCornelia Huck 	default:
823d938dc55SCornelia Huck 		r = -EINVAL;
824d938dc55SCornelia Huck 		break;
825d938dc55SCornelia Huck 	}
826d938dc55SCornelia Huck 	return r;
827d938dc55SCornelia Huck }
828d938dc55SCornelia Huck 
8298c0a7ce6SDominik Dingel static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
8308c0a7ce6SDominik Dingel {
8318c0a7ce6SDominik Dingel 	int ret;
8328c0a7ce6SDominik Dingel 
8338c0a7ce6SDominik Dingel 	switch (attr->attr) {
8348c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE:
8358c0a7ce6SDominik Dingel 		ret = 0;
836c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
837a3a92c31SDominik Dingel 			 kvm->arch.mem_limit);
838a3a92c31SDominik Dingel 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
8398c0a7ce6SDominik Dingel 			ret = -EFAULT;
8408c0a7ce6SDominik Dingel 		break;
8418c0a7ce6SDominik Dingel 	default:
8428c0a7ce6SDominik Dingel 		ret = -ENXIO;
8438c0a7ce6SDominik Dingel 		break;
8448c0a7ce6SDominik Dingel 	}
8458c0a7ce6SDominik Dingel 	return ret;
8468c0a7ce6SDominik Dingel }
8478c0a7ce6SDominik Dingel 
8488c0a7ce6SDominik Dingel static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
8494f718eabSDominik Dingel {
8504f718eabSDominik Dingel 	int ret;
8514f718eabSDominik Dingel 	unsigned int idx;
8524f718eabSDominik Dingel 	switch (attr->attr) {
8534f718eabSDominik Dingel 	case KVM_S390_VM_MEM_ENABLE_CMMA:
854f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
855c24cc9c8SDavid Hildenbrand 		if (!sclp.has_cmma)
856e6db1d61SDominik Dingel 			break;
857e6db1d61SDominik Dingel 
858c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
8594f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
860a4499382SJanosch Frank 		if (kvm->created_vcpus)
861a4499382SJanosch Frank 			ret = -EBUSY;
862a4499382SJanosch Frank 		else if (kvm->mm->context.allow_gmap_hpage_1m)
863a4499382SJanosch Frank 			ret = -EINVAL;
864a4499382SJanosch Frank 		else {
8654f718eabSDominik Dingel 			kvm->arch.use_cmma = 1;
866c9f0a2b8SJanosch Frank 			/* Not compatible with cmma. */
867c9f0a2b8SJanosch Frank 			kvm->arch.use_pfmfi = 0;
8684f718eabSDominik Dingel 			ret = 0;
8694f718eabSDominik Dingel 		}
8704f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
8714f718eabSDominik Dingel 		break;
8724f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CLR_CMMA:
873f9cbd9b0SDavid Hildenbrand 		ret = -ENXIO;
874f9cbd9b0SDavid Hildenbrand 		if (!sclp.has_cmma)
875f9cbd9b0SDavid Hildenbrand 			break;
876c3489155SDominik Dingel 		ret = -EINVAL;
877c3489155SDominik Dingel 		if (!kvm->arch.use_cmma)
878c3489155SDominik Dingel 			break;
879c3489155SDominik Dingel 
880c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
8814f718eabSDominik Dingel 		mutex_lock(&kvm->lock);
8824f718eabSDominik Dingel 		idx = srcu_read_lock(&kvm->srcu);
883a13cff31SDominik Dingel 		s390_reset_cmma(kvm->arch.gmap->mm);
8844f718eabSDominik Dingel 		srcu_read_unlock(&kvm->srcu, idx);
8854f718eabSDominik Dingel 		mutex_unlock(&kvm->lock);
8864f718eabSDominik Dingel 		ret = 0;
8874f718eabSDominik Dingel 		break;
8888c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
8898c0a7ce6SDominik Dingel 		unsigned long new_limit;
8908c0a7ce6SDominik Dingel 
8918c0a7ce6SDominik Dingel 		if (kvm_is_ucontrol(kvm))
8928c0a7ce6SDominik Dingel 			return -EINVAL;
8938c0a7ce6SDominik Dingel 
8948c0a7ce6SDominik Dingel 		if (get_user(new_limit, (u64 __user *)attr->addr))
8958c0a7ce6SDominik Dingel 			return -EFAULT;
8968c0a7ce6SDominik Dingel 
897a3a92c31SDominik Dingel 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
898a3a92c31SDominik Dingel 		    new_limit > kvm->arch.mem_limit)
8998c0a7ce6SDominik Dingel 			return -E2BIG;
9008c0a7ce6SDominik Dingel 
901a3a92c31SDominik Dingel 		if (!new_limit)
902a3a92c31SDominik Dingel 			return -EINVAL;
903a3a92c31SDominik Dingel 
9046ea427bbSMartin Schwidefsky 		/* gmap_create takes last usable address */
905a3a92c31SDominik Dingel 		if (new_limit != KVM_S390_NO_MEM_LIMIT)
906a3a92c31SDominik Dingel 			new_limit -= 1;
907a3a92c31SDominik Dingel 
9088c0a7ce6SDominik Dingel 		ret = -EBUSY;
9098c0a7ce6SDominik Dingel 		mutex_lock(&kvm->lock);
910a03825bbSPaolo Bonzini 		if (!kvm->created_vcpus) {
9116ea427bbSMartin Schwidefsky 			/* gmap_create will round the limit up */
9126ea427bbSMartin Schwidefsky 			struct gmap *new = gmap_create(current->mm, new_limit);
9138c0a7ce6SDominik Dingel 
9148c0a7ce6SDominik Dingel 			if (!new) {
9158c0a7ce6SDominik Dingel 				ret = -ENOMEM;
9168c0a7ce6SDominik Dingel 			} else {
9176ea427bbSMartin Schwidefsky 				gmap_remove(kvm->arch.gmap);
9188c0a7ce6SDominik Dingel 				new->private = kvm;
9198c0a7ce6SDominik Dingel 				kvm->arch.gmap = new;
9208c0a7ce6SDominik Dingel 				ret = 0;
9218c0a7ce6SDominik Dingel 			}
9228c0a7ce6SDominik Dingel 		}
9238c0a7ce6SDominik Dingel 		mutex_unlock(&kvm->lock);
924a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
925a3a92c31SDominik Dingel 		VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
926a3a92c31SDominik Dingel 			 (void *) kvm->arch.gmap->asce);
9278c0a7ce6SDominik Dingel 		break;
9288c0a7ce6SDominik Dingel 	}
9294f718eabSDominik Dingel 	default:
9304f718eabSDominik Dingel 		ret = -ENXIO;
9314f718eabSDominik Dingel 		break;
9324f718eabSDominik Dingel 	}
9334f718eabSDominik Dingel 	return ret;
9344f718eabSDominik Dingel }
9354f718eabSDominik Dingel 
936a374e892STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
937a374e892STony Krowiak 
93820c922f0STony Krowiak void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
939a374e892STony Krowiak {
940a374e892STony Krowiak 	struct kvm_vcpu *vcpu;
941a374e892STony Krowiak 	int i;
942a374e892STony Krowiak 
94320c922f0STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
94420c922f0STony Krowiak 
9453194cdb7SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
94620c922f0STony Krowiak 		kvm_s390_vcpu_crypto_setup(vcpu);
9473194cdb7SDavid Hildenbrand 		/* recreate the shadow crycb by leaving the VSIE handler */
9483194cdb7SDavid Hildenbrand 		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
9493194cdb7SDavid Hildenbrand 	}
95020c922f0STony Krowiak 
95120c922f0STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
95220c922f0STony Krowiak }
95320c922f0STony Krowiak 
95420c922f0STony Krowiak static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
95520c922f0STony Krowiak {
956a374e892STony Krowiak 	mutex_lock(&kvm->lock);
957a374e892STony Krowiak 	switch (attr->attr) {
958a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
9598e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9608e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
96137940fb0STony Krowiak 			return -EINVAL;
9628e41bd54SChristian Borntraeger 		}
963a374e892STony Krowiak 		get_random_bytes(
964a374e892STony Krowiak 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
965a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
966a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 1;
967c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
968a374e892STony Krowiak 		break;
969a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
9708e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9718e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
97237940fb0STony Krowiak 			return -EINVAL;
9738e41bd54SChristian Borntraeger 		}
974a374e892STony Krowiak 		get_random_bytes(
975a374e892STony Krowiak 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
976a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
977a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 1;
978c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
979a374e892STony Krowiak 		break;
980a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
9818e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9828e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
98337940fb0STony Krowiak 			return -EINVAL;
9848e41bd54SChristian Borntraeger 		}
985a374e892STony Krowiak 		kvm->arch.crypto.aes_kw = 0;
986a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
987a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
988c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
989a374e892STony Krowiak 		break;
990a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
9918e41bd54SChristian Borntraeger 		if (!test_kvm_facility(kvm, 76)) {
9928e41bd54SChristian Borntraeger 			mutex_unlock(&kvm->lock);
99337940fb0STony Krowiak 			return -EINVAL;
9948e41bd54SChristian Borntraeger 		}
995a374e892STony Krowiak 		kvm->arch.crypto.dea_kw = 0;
996a374e892STony Krowiak 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
997a374e892STony Krowiak 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
998c92ea7b9SChristian Borntraeger 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
999a374e892STony Krowiak 		break;
100037940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_ENABLE_APIE:
100137940fb0STony Krowiak 		if (!ap_instructions_available()) {
100237940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
100337940fb0STony Krowiak 			return -EOPNOTSUPP;
100437940fb0STony Krowiak 		}
100537940fb0STony Krowiak 		kvm->arch.crypto.apie = 1;
100637940fb0STony Krowiak 		break;
100737940fb0STony Krowiak 	case KVM_S390_VM_CRYPTO_DISABLE_APIE:
100837940fb0STony Krowiak 		if (!ap_instructions_available()) {
100937940fb0STony Krowiak 			mutex_unlock(&kvm->lock);
101037940fb0STony Krowiak 			return -EOPNOTSUPP;
101137940fb0STony Krowiak 		}
101237940fb0STony Krowiak 		kvm->arch.crypto.apie = 0;
101337940fb0STony Krowiak 		break;
1014a374e892STony Krowiak 	default:
1015a374e892STony Krowiak 		mutex_unlock(&kvm->lock);
1016a374e892STony Krowiak 		return -ENXIO;
1017a374e892STony Krowiak 	}
1018a374e892STony Krowiak 
101920c922f0STony Krowiak 	kvm_s390_vcpu_crypto_reset_all(kvm);
1020a374e892STony Krowiak 	mutex_unlock(&kvm->lock);
1021a374e892STony Krowiak 	return 0;
1022a374e892STony Krowiak }
1023a374e892STony Krowiak 
1024190df4a2SClaudio Imbrenda static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
1025190df4a2SClaudio Imbrenda {
1026190df4a2SClaudio Imbrenda 	int cx;
1027190df4a2SClaudio Imbrenda 	struct kvm_vcpu *vcpu;
1028190df4a2SClaudio Imbrenda 
1029190df4a2SClaudio Imbrenda 	kvm_for_each_vcpu(cx, vcpu, kvm)
1030190df4a2SClaudio Imbrenda 		kvm_s390_sync_request(req, vcpu);
1031190df4a2SClaudio Imbrenda }
1032190df4a2SClaudio Imbrenda 
1033190df4a2SClaudio Imbrenda /*
1034190df4a2SClaudio Imbrenda  * Must be called with kvm->srcu held to avoid races on memslots, and with
10351de1ea7eSChristian Borntraeger  * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
1036190df4a2SClaudio Imbrenda  */
1037190df4a2SClaudio Imbrenda static int kvm_s390_vm_start_migration(struct kvm *kvm)
1038190df4a2SClaudio Imbrenda {
1039190df4a2SClaudio Imbrenda 	struct kvm_memory_slot *ms;
1040190df4a2SClaudio Imbrenda 	struct kvm_memslots *slots;
1041afdad616SClaudio Imbrenda 	unsigned long ram_pages = 0;
1042190df4a2SClaudio Imbrenda 	int slotnr;
1043190df4a2SClaudio Imbrenda 
1044190df4a2SClaudio Imbrenda 	/* migration mode already enabled */
1045afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
1046190df4a2SClaudio Imbrenda 		return 0;
1047190df4a2SClaudio Imbrenda 	slots = kvm_memslots(kvm);
1048190df4a2SClaudio Imbrenda 	if (!slots || !slots->used_slots)
1049190df4a2SClaudio Imbrenda 		return -EINVAL;
1050190df4a2SClaudio Imbrenda 
1051afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma) {
1052afdad616SClaudio Imbrenda 		kvm->arch.migration_mode = 1;
1053afdad616SClaudio Imbrenda 		return 0;
1054190df4a2SClaudio Imbrenda 	}
1055190df4a2SClaudio Imbrenda 	/* mark all the pages in active slots as dirty */
1056190df4a2SClaudio Imbrenda 	for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
1057190df4a2SClaudio Imbrenda 		ms = slots->memslots + slotnr;
105813a17cc0SIgor Mammedov 		if (!ms->dirty_bitmap)
105913a17cc0SIgor Mammedov 			return -EINVAL;
1060afdad616SClaudio Imbrenda 		/*
1061afdad616SClaudio Imbrenda 		 * The second half of the bitmap is only used on x86,
1062afdad616SClaudio Imbrenda 		 * and would be wasted otherwise, so we put it to good
1063afdad616SClaudio Imbrenda 		 * use here to keep track of the state of the storage
1064afdad616SClaudio Imbrenda 		 * attributes.
1065afdad616SClaudio Imbrenda 		 */
1066afdad616SClaudio Imbrenda 		memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1067afdad616SClaudio Imbrenda 		ram_pages += ms->npages;
1068190df4a2SClaudio Imbrenda 	}
1069afdad616SClaudio Imbrenda 	atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1070afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 1;
1071190df4a2SClaudio Imbrenda 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1072190df4a2SClaudio Imbrenda 	return 0;
1073190df4a2SClaudio Imbrenda }
1074190df4a2SClaudio Imbrenda 
1075190df4a2SClaudio Imbrenda /*
10761de1ea7eSChristian Borntraeger  * Must be called with kvm->slots_lock to avoid races with ourselves and
1077190df4a2SClaudio Imbrenda  * kvm_s390_vm_start_migration.
1078190df4a2SClaudio Imbrenda  */
1079190df4a2SClaudio Imbrenda static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1080190df4a2SClaudio Imbrenda {
1081190df4a2SClaudio Imbrenda 	/* migration mode already disabled */
1082afdad616SClaudio Imbrenda 	if (!kvm->arch.migration_mode)
1083190df4a2SClaudio Imbrenda 		return 0;
1084afdad616SClaudio Imbrenda 	kvm->arch.migration_mode = 0;
1085afdad616SClaudio Imbrenda 	if (kvm->arch.use_cmma)
1086190df4a2SClaudio Imbrenda 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1087190df4a2SClaudio Imbrenda 	return 0;
1088190df4a2SClaudio Imbrenda }
1089190df4a2SClaudio Imbrenda 
1090190df4a2SClaudio Imbrenda static int kvm_s390_vm_set_migration(struct kvm *kvm,
1091190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1092190df4a2SClaudio Imbrenda {
10931de1ea7eSChristian Borntraeger 	int res = -ENXIO;
1094190df4a2SClaudio Imbrenda 
10951de1ea7eSChristian Borntraeger 	mutex_lock(&kvm->slots_lock);
1096190df4a2SClaudio Imbrenda 	switch (attr->attr) {
1097190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_START:
1098190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_start_migration(kvm);
1099190df4a2SClaudio Imbrenda 		break;
1100190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION_STOP:
1101190df4a2SClaudio Imbrenda 		res = kvm_s390_vm_stop_migration(kvm);
1102190df4a2SClaudio Imbrenda 		break;
1103190df4a2SClaudio Imbrenda 	default:
1104190df4a2SClaudio Imbrenda 		break;
1105190df4a2SClaudio Imbrenda 	}
11061de1ea7eSChristian Borntraeger 	mutex_unlock(&kvm->slots_lock);
1107190df4a2SClaudio Imbrenda 
1108190df4a2SClaudio Imbrenda 	return res;
1109190df4a2SClaudio Imbrenda }
1110190df4a2SClaudio Imbrenda 
1111190df4a2SClaudio Imbrenda static int kvm_s390_vm_get_migration(struct kvm *kvm,
1112190df4a2SClaudio Imbrenda 				     struct kvm_device_attr *attr)
1113190df4a2SClaudio Imbrenda {
1114afdad616SClaudio Imbrenda 	u64 mig = kvm->arch.migration_mode;
1115190df4a2SClaudio Imbrenda 
1116190df4a2SClaudio Imbrenda 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1117190df4a2SClaudio Imbrenda 		return -ENXIO;
1118190df4a2SClaudio Imbrenda 
1119190df4a2SClaudio Imbrenda 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1120190df4a2SClaudio Imbrenda 		return -EFAULT;
1121190df4a2SClaudio Imbrenda 	return 0;
1122190df4a2SClaudio Imbrenda }
1123190df4a2SClaudio Imbrenda 
11248fa1696eSCollin L. Walling static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
11258fa1696eSCollin L. Walling {
11268fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
11278fa1696eSCollin L. Walling 
11288fa1696eSCollin L. Walling 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
11298fa1696eSCollin L. Walling 		return -EFAULT;
11308fa1696eSCollin L. Walling 
11310e7def5fSDavid Hildenbrand 	if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
11328fa1696eSCollin L. Walling 		return -EINVAL;
11330e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
11348fa1696eSCollin L. Walling 
11358fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
11368fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
11378fa1696eSCollin L. Walling 
11388fa1696eSCollin L. Walling 	return 0;
11398fa1696eSCollin L. Walling }
11408fa1696eSCollin L. Walling 
114172f25020SJason J. Herne static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
114272f25020SJason J. Herne {
114372f25020SJason J. Herne 	u8 gtod_high;
114472f25020SJason J. Herne 
114572f25020SJason J. Herne 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
114672f25020SJason J. Herne 					   sizeof(gtod_high)))
114772f25020SJason J. Herne 		return -EFAULT;
114872f25020SJason J. Herne 
114972f25020SJason J. Herne 	if (gtod_high != 0)
115072f25020SJason J. Herne 		return -EINVAL;
115158c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
115272f25020SJason J. Herne 
115372f25020SJason J. Herne 	return 0;
115472f25020SJason J. Herne }
115572f25020SJason J. Herne 
115672f25020SJason J. Herne static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
115772f25020SJason J. Herne {
11580e7def5fSDavid Hildenbrand 	struct kvm_s390_vm_tod_clock gtod = { 0 };
115972f25020SJason J. Herne 
11600e7def5fSDavid Hildenbrand 	if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
11610e7def5fSDavid Hildenbrand 			   sizeof(gtod.tod)))
116272f25020SJason J. Herne 		return -EFAULT;
116372f25020SJason J. Herne 
11640e7def5fSDavid Hildenbrand 	kvm_s390_set_tod_clock(kvm, &gtod);
11650e7def5fSDavid Hildenbrand 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
116672f25020SJason J. Herne 	return 0;
116772f25020SJason J. Herne }
116872f25020SJason J. Herne 
116972f25020SJason J. Herne static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
117072f25020SJason J. Herne {
117172f25020SJason J. Herne 	int ret;
117272f25020SJason J. Herne 
117372f25020SJason J. Herne 	if (attr->flags)
117472f25020SJason J. Herne 		return -EINVAL;
117572f25020SJason J. Herne 
117672f25020SJason J. Herne 	switch (attr->attr) {
11778fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
11788fa1696eSCollin L. Walling 		ret = kvm_s390_set_tod_ext(kvm, attr);
11798fa1696eSCollin L. Walling 		break;
118072f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
118172f25020SJason J. Herne 		ret = kvm_s390_set_tod_high(kvm, attr);
118272f25020SJason J. Herne 		break;
118372f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
118472f25020SJason J. Herne 		ret = kvm_s390_set_tod_low(kvm, attr);
118572f25020SJason J. Herne 		break;
118672f25020SJason J. Herne 	default:
118772f25020SJason J. Herne 		ret = -ENXIO;
118872f25020SJason J. Herne 		break;
118972f25020SJason J. Herne 	}
119072f25020SJason J. Herne 	return ret;
119172f25020SJason J. Herne }
119272f25020SJason J. Herne 
119333d1b272SDavid Hildenbrand static void kvm_s390_get_tod_clock(struct kvm *kvm,
11948fa1696eSCollin L. Walling 				   struct kvm_s390_vm_tod_clock *gtod)
11958fa1696eSCollin L. Walling {
11962cfd7b73SHeiko Carstens 	union tod_clock clk;
11978fa1696eSCollin L. Walling 
11988fa1696eSCollin L. Walling 	preempt_disable();
11998fa1696eSCollin L. Walling 
12002cfd7b73SHeiko Carstens 	store_tod_clock_ext(&clk);
12018fa1696eSCollin L. Walling 
12022cfd7b73SHeiko Carstens 	gtod->tod = clk.tod + kvm->arch.epoch;
120333d1b272SDavid Hildenbrand 	gtod->epoch_idx = 0;
120433d1b272SDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
12052cfd7b73SHeiko Carstens 		gtod->epoch_idx = clk.ei + kvm->arch.epdx;
12062cfd7b73SHeiko Carstens 		if (gtod->tod < clk.tod)
12078fa1696eSCollin L. Walling 			gtod->epoch_idx += 1;
120833d1b272SDavid Hildenbrand 	}
12098fa1696eSCollin L. Walling 
12108fa1696eSCollin L. Walling 	preempt_enable();
12118fa1696eSCollin L. Walling }
12128fa1696eSCollin L. Walling 
12138fa1696eSCollin L. Walling static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
12148fa1696eSCollin L. Walling {
12158fa1696eSCollin L. Walling 	struct kvm_s390_vm_tod_clock gtod;
12168fa1696eSCollin L. Walling 
12178fa1696eSCollin L. Walling 	memset(&gtod, 0, sizeof(gtod));
121833d1b272SDavid Hildenbrand 	kvm_s390_get_tod_clock(kvm, &gtod);
12198fa1696eSCollin L. Walling 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
12208fa1696eSCollin L. Walling 		return -EFAULT;
12218fa1696eSCollin L. Walling 
12228fa1696eSCollin L. Walling 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
12238fa1696eSCollin L. Walling 		gtod.epoch_idx, gtod.tod);
12248fa1696eSCollin L. Walling 	return 0;
12258fa1696eSCollin L. Walling }
12268fa1696eSCollin L. Walling 
122772f25020SJason J. Herne static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
122872f25020SJason J. Herne {
122972f25020SJason J. Herne 	u8 gtod_high = 0;
123072f25020SJason J. Herne 
123172f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
123272f25020SJason J. Herne 					 sizeof(gtod_high)))
123372f25020SJason J. Herne 		return -EFAULT;
123458c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
123572f25020SJason J. Herne 
123672f25020SJason J. Herne 	return 0;
123772f25020SJason J. Herne }
123872f25020SJason J. Herne 
123972f25020SJason J. Herne static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
124072f25020SJason J. Herne {
12415a3d883aSDavid Hildenbrand 	u64 gtod;
124272f25020SJason J. Herne 
124360417fccSDavid Hildenbrand 	gtod = kvm_s390_get_tod_clock_fast(kvm);
124472f25020SJason J. Herne 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
124572f25020SJason J. Herne 		return -EFAULT;
124658c383c6SChristian Borntraeger 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
124772f25020SJason J. Herne 
124872f25020SJason J. Herne 	return 0;
124972f25020SJason J. Herne }
125072f25020SJason J. Herne 
125172f25020SJason J. Herne static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
125272f25020SJason J. Herne {
125372f25020SJason J. Herne 	int ret;
125472f25020SJason J. Herne 
125572f25020SJason J. Herne 	if (attr->flags)
125672f25020SJason J. Herne 		return -EINVAL;
125772f25020SJason J. Herne 
125872f25020SJason J. Herne 	switch (attr->attr) {
12598fa1696eSCollin L. Walling 	case KVM_S390_VM_TOD_EXT:
12608fa1696eSCollin L. Walling 		ret = kvm_s390_get_tod_ext(kvm, attr);
12618fa1696eSCollin L. Walling 		break;
126272f25020SJason J. Herne 	case KVM_S390_VM_TOD_HIGH:
126372f25020SJason J. Herne 		ret = kvm_s390_get_tod_high(kvm, attr);
126472f25020SJason J. Herne 		break;
126572f25020SJason J. Herne 	case KVM_S390_VM_TOD_LOW:
126672f25020SJason J. Herne 		ret = kvm_s390_get_tod_low(kvm, attr);
126772f25020SJason J. Herne 		break;
126872f25020SJason J. Herne 	default:
126972f25020SJason J. Herne 		ret = -ENXIO;
127072f25020SJason J. Herne 		break;
127172f25020SJason J. Herne 	}
127272f25020SJason J. Herne 	return ret;
127372f25020SJason J. Herne }
127472f25020SJason J. Herne 
1275658b6edaSMichael Mueller static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1276658b6edaSMichael Mueller {
1277658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1278053dd230SDavid Hildenbrand 	u16 lowest_ibc, unblocked_ibc;
1279658b6edaSMichael Mueller 	int ret = 0;
1280658b6edaSMichael Mueller 
1281658b6edaSMichael Mueller 	mutex_lock(&kvm->lock);
1282a03825bbSPaolo Bonzini 	if (kvm->created_vcpus) {
1283658b6edaSMichael Mueller 		ret = -EBUSY;
1284658b6edaSMichael Mueller 		goto out;
1285658b6edaSMichael Mueller 	}
1286c4196218SChristian Borntraeger 	proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1287658b6edaSMichael Mueller 	if (!proc) {
1288658b6edaSMichael Mueller 		ret = -ENOMEM;
1289658b6edaSMichael Mueller 		goto out;
1290658b6edaSMichael Mueller 	}
1291658b6edaSMichael Mueller 	if (!copy_from_user(proc, (void __user *)attr->addr,
1292658b6edaSMichael Mueller 			    sizeof(*proc))) {
12939bb0ec09SDavid Hildenbrand 		kvm->arch.model.cpuid = proc->cpuid;
1294053dd230SDavid Hildenbrand 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1295053dd230SDavid Hildenbrand 		unblocked_ibc = sclp.ibc & 0xfff;
12960487c44dSDavid Hildenbrand 		if (lowest_ibc && proc->ibc) {
1297053dd230SDavid Hildenbrand 			if (proc->ibc > unblocked_ibc)
1298053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = unblocked_ibc;
1299053dd230SDavid Hildenbrand 			else if (proc->ibc < lowest_ibc)
1300053dd230SDavid Hildenbrand 				kvm->arch.model.ibc = lowest_ibc;
1301053dd230SDavid Hildenbrand 			else
1302658b6edaSMichael Mueller 				kvm->arch.model.ibc = proc->ibc;
1303053dd230SDavid Hildenbrand 		}
1304c54f0d6aSDavid Hildenbrand 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1305658b6edaSMichael Mueller 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1306a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1307a8c39dd7SChristian Borntraeger 			 kvm->arch.model.ibc,
1308a8c39dd7SChristian Borntraeger 			 kvm->arch.model.cpuid);
1309a8c39dd7SChristian Borntraeger 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1310a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[0],
1311a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[1],
1312a8c39dd7SChristian Borntraeger 			 kvm->arch.model.fac_list[2]);
1313658b6edaSMichael Mueller 	} else
1314658b6edaSMichael Mueller 		ret = -EFAULT;
1315658b6edaSMichael Mueller 	kfree(proc);
1316658b6edaSMichael Mueller out:
1317658b6edaSMichael Mueller 	mutex_unlock(&kvm->lock);
1318658b6edaSMichael Mueller 	return ret;
1319658b6edaSMichael Mueller }
1320658b6edaSMichael Mueller 
132115c9705fSDavid Hildenbrand static int kvm_s390_set_processor_feat(struct kvm *kvm,
132215c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
132315c9705fSDavid Hildenbrand {
132415c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
132515c9705fSDavid Hildenbrand 
132615c9705fSDavid Hildenbrand 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
132715c9705fSDavid Hildenbrand 		return -EFAULT;
132815c9705fSDavid Hildenbrand 	if (!bitmap_subset((unsigned long *) data.feat,
132915c9705fSDavid Hildenbrand 			   kvm_s390_available_cpu_feat,
133015c9705fSDavid Hildenbrand 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
133115c9705fSDavid Hildenbrand 		return -EINVAL;
133215c9705fSDavid Hildenbrand 
133315c9705fSDavid Hildenbrand 	mutex_lock(&kvm->lock);
13342f8311c9SChristian Borntraeger 	if (kvm->created_vcpus) {
13352f8311c9SChristian Borntraeger 		mutex_unlock(&kvm->lock);
13362f8311c9SChristian Borntraeger 		return -EBUSY;
13372f8311c9SChristian Borntraeger 	}
133815c9705fSDavid Hildenbrand 	bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
133915c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
134015c9705fSDavid Hildenbrand 	mutex_unlock(&kvm->lock);
13412f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
13422f8311c9SChristian Borntraeger 			 data.feat[0],
13432f8311c9SChristian Borntraeger 			 data.feat[1],
13442f8311c9SChristian Borntraeger 			 data.feat[2]);
13452f8311c9SChristian Borntraeger 	return 0;
134615c9705fSDavid Hildenbrand }
134715c9705fSDavid Hildenbrand 
13480a763c78SDavid Hildenbrand static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
13490a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
13500a763c78SDavid Hildenbrand {
1351346fa2f8SChristian Borntraeger 	mutex_lock(&kvm->lock);
1352346fa2f8SChristian Borntraeger 	if (kvm->created_vcpus) {
1353346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1354346fa2f8SChristian Borntraeger 		return -EBUSY;
1355346fa2f8SChristian Borntraeger 	}
1356346fa2f8SChristian Borntraeger 
1357346fa2f8SChristian Borntraeger 	if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1358346fa2f8SChristian Borntraeger 			   sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1359346fa2f8SChristian Borntraeger 		mutex_unlock(&kvm->lock);
1360346fa2f8SChristian Borntraeger 		return -EFAULT;
1361346fa2f8SChristian Borntraeger 	}
1362346fa2f8SChristian Borntraeger 	mutex_unlock(&kvm->lock);
1363346fa2f8SChristian Borntraeger 
136411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
136511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
136611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
136711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
136811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
136911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
137011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
137111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
137211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
137311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
137411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
137511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
137611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
137711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
137811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KM     subfunc 0x%16.16lx.%16.16lx",
137911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
138011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
138111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
138211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
138311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
138411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
138511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
138611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
138711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
138811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
138911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
139011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
139111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
139211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
139311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
139411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
139511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
139611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
139711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
139811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
139911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
140011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
140111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
140211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
140311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
140411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
140511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
140611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
140711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
140813209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
140913209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
141013209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1411173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1412173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1413173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1414173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1415173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
14164f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
14174f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
14184f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
14194f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
14204f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
142111ba5961SChristian Borntraeger 
1422346fa2f8SChristian Borntraeger 	return 0;
14230a763c78SDavid Hildenbrand }
14240a763c78SDavid Hildenbrand 
1425658b6edaSMichael Mueller static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1426658b6edaSMichael Mueller {
1427658b6edaSMichael Mueller 	int ret = -ENXIO;
1428658b6edaSMichael Mueller 
1429658b6edaSMichael Mueller 	switch (attr->attr) {
1430658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1431658b6edaSMichael Mueller 		ret = kvm_s390_set_processor(kvm, attr);
1432658b6edaSMichael Mueller 		break;
143315c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
143415c9705fSDavid Hildenbrand 		ret = kvm_s390_set_processor_feat(kvm, attr);
143515c9705fSDavid Hildenbrand 		break;
14360a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
14370a763c78SDavid Hildenbrand 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
14380a763c78SDavid Hildenbrand 		break;
1439658b6edaSMichael Mueller 	}
1440658b6edaSMichael Mueller 	return ret;
1441658b6edaSMichael Mueller }
1442658b6edaSMichael Mueller 
1443658b6edaSMichael Mueller static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1444658b6edaSMichael Mueller {
1445658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_processor *proc;
1446658b6edaSMichael Mueller 	int ret = 0;
1447658b6edaSMichael Mueller 
1448c4196218SChristian Borntraeger 	proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1449658b6edaSMichael Mueller 	if (!proc) {
1450658b6edaSMichael Mueller 		ret = -ENOMEM;
1451658b6edaSMichael Mueller 		goto out;
1452658b6edaSMichael Mueller 	}
14539bb0ec09SDavid Hildenbrand 	proc->cpuid = kvm->arch.model.cpuid;
1454658b6edaSMichael Mueller 	proc->ibc = kvm->arch.model.ibc;
1455c54f0d6aSDavid Hildenbrand 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1456c54f0d6aSDavid Hildenbrand 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1457a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1458a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1459a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1460a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1461a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[0],
1462a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[1],
1463a8c39dd7SChristian Borntraeger 		 kvm->arch.model.fac_list[2]);
1464658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1465658b6edaSMichael Mueller 		ret = -EFAULT;
1466658b6edaSMichael Mueller 	kfree(proc);
1467658b6edaSMichael Mueller out:
1468658b6edaSMichael Mueller 	return ret;
1469658b6edaSMichael Mueller }
1470658b6edaSMichael Mueller 
1471658b6edaSMichael Mueller static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1472658b6edaSMichael Mueller {
1473658b6edaSMichael Mueller 	struct kvm_s390_vm_cpu_machine *mach;
1474658b6edaSMichael Mueller 	int ret = 0;
1475658b6edaSMichael Mueller 
1476c4196218SChristian Borntraeger 	mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT);
1477658b6edaSMichael Mueller 	if (!mach) {
1478658b6edaSMichael Mueller 		ret = -ENOMEM;
1479658b6edaSMichael Mueller 		goto out;
1480658b6edaSMichael Mueller 	}
1481658b6edaSMichael Mueller 	get_cpu_id((struct cpuid *) &mach->cpuid);
148237c5f6c8SDavid Hildenbrand 	mach->ibc = sclp.ibc;
1483c54f0d6aSDavid Hildenbrand 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1484981467c9SMichael Mueller 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
148517e89e13SSven Schnelle 	memcpy((unsigned long *)&mach->fac_list, stfle_fac_list,
148617e89e13SSven Schnelle 	       sizeof(stfle_fac_list));
1487a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1488a8c39dd7SChristian Borntraeger 		 kvm->arch.model.ibc,
1489a8c39dd7SChristian Borntraeger 		 kvm->arch.model.cpuid);
1490a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1491a8c39dd7SChristian Borntraeger 		 mach->fac_mask[0],
1492a8c39dd7SChristian Borntraeger 		 mach->fac_mask[1],
1493a8c39dd7SChristian Borntraeger 		 mach->fac_mask[2]);
1494a8c39dd7SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1495a8c39dd7SChristian Borntraeger 		 mach->fac_list[0],
1496a8c39dd7SChristian Borntraeger 		 mach->fac_list[1],
1497a8c39dd7SChristian Borntraeger 		 mach->fac_list[2]);
1498658b6edaSMichael Mueller 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1499658b6edaSMichael Mueller 		ret = -EFAULT;
1500658b6edaSMichael Mueller 	kfree(mach);
1501658b6edaSMichael Mueller out:
1502658b6edaSMichael Mueller 	return ret;
1503658b6edaSMichael Mueller }
1504658b6edaSMichael Mueller 
150515c9705fSDavid Hildenbrand static int kvm_s390_get_processor_feat(struct kvm *kvm,
150615c9705fSDavid Hildenbrand 				       struct kvm_device_attr *attr)
150715c9705fSDavid Hildenbrand {
150815c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
150915c9705fSDavid Hildenbrand 
151015c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
151115c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
151215c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
151315c9705fSDavid Hildenbrand 		return -EFAULT;
15142f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
15152f8311c9SChristian Borntraeger 			 data.feat[0],
15162f8311c9SChristian Borntraeger 			 data.feat[1],
15172f8311c9SChristian Borntraeger 			 data.feat[2]);
151815c9705fSDavid Hildenbrand 	return 0;
151915c9705fSDavid Hildenbrand }
152015c9705fSDavid Hildenbrand 
152115c9705fSDavid Hildenbrand static int kvm_s390_get_machine_feat(struct kvm *kvm,
152215c9705fSDavid Hildenbrand 				     struct kvm_device_attr *attr)
152315c9705fSDavid Hildenbrand {
152415c9705fSDavid Hildenbrand 	struct kvm_s390_vm_cpu_feat data;
152515c9705fSDavid Hildenbrand 
152615c9705fSDavid Hildenbrand 	bitmap_copy((unsigned long *) data.feat,
152715c9705fSDavid Hildenbrand 		    kvm_s390_available_cpu_feat,
152815c9705fSDavid Hildenbrand 		    KVM_S390_VM_CPU_FEAT_NR_BITS);
152915c9705fSDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
153015c9705fSDavid Hildenbrand 		return -EFAULT;
15312f8311c9SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host feat:  0x%16.16llx.0x%16.16llx.0x%16.16llx",
15322f8311c9SChristian Borntraeger 			 data.feat[0],
15332f8311c9SChristian Borntraeger 			 data.feat[1],
15342f8311c9SChristian Borntraeger 			 data.feat[2]);
153515c9705fSDavid Hildenbrand 	return 0;
153615c9705fSDavid Hildenbrand }
153715c9705fSDavid Hildenbrand 
15380a763c78SDavid Hildenbrand static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
15390a763c78SDavid Hildenbrand 					  struct kvm_device_attr *attr)
15400a763c78SDavid Hildenbrand {
1541346fa2f8SChristian Borntraeger 	if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1542346fa2f8SChristian Borntraeger 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
1543346fa2f8SChristian Borntraeger 		return -EFAULT;
1544346fa2f8SChristian Borntraeger 
154511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
154611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
154711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
154811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
154911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
155011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
155111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
155211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
155311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
155411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
155511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
155611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
155711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
155811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
155911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KM     subfunc 0x%16.16lx.%16.16lx",
156011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
156111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
156211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
156311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
156411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
156511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
156611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
156711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
156811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
156911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
157011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
157111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
157211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
157311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
157411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
157511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
157611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
157711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
157811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
157911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
158011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
158111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
158211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
158311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
158411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
158511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
158611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
158711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
158811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
158913209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
159013209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
159113209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1592173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1593173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1594173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1595173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1596173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
15974f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
15984f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
15994f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
16004f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
16014f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
160211ba5961SChristian Borntraeger 
1603346fa2f8SChristian Borntraeger 	return 0;
16040a763c78SDavid Hildenbrand }
16050a763c78SDavid Hildenbrand 
16060a763c78SDavid Hildenbrand static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
16070a763c78SDavid Hildenbrand 					struct kvm_device_attr *attr)
16080a763c78SDavid Hildenbrand {
16090a763c78SDavid Hildenbrand 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
16100a763c78SDavid Hildenbrand 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
16110a763c78SDavid Hildenbrand 		return -EFAULT;
161211ba5961SChristian Borntraeger 
161311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
161411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
161511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
161611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
161711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
161811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PTFF   subfunc 0x%16.16lx.%16.16lx",
161911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
162011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
162111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMAC   subfunc 0x%16.16lx.%16.16lx",
162211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
162311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
162411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMC    subfunc 0x%16.16lx.%16.16lx",
162511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
162611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
162711ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KM     subfunc 0x%16.16lx.%16.16lx",
162811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
162911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
163011ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KIMD   subfunc 0x%16.16lx.%16.16lx",
163111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
163211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
163311ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KLMD   subfunc 0x%16.16lx.%16.16lx",
163411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
163511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
163611ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCKMO  subfunc 0x%16.16lx.%16.16lx",
163711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
163811ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
163911ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMCTR  subfunc 0x%16.16lx.%16.16lx",
164011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
164111ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
164211ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMF    subfunc 0x%16.16lx.%16.16lx",
164311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
164411ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
164511ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMO    subfunc 0x%16.16lx.%16.16lx",
164611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
164711ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
164811ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PCC    subfunc 0x%16.16lx.%16.16lx",
164911ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
165011ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
165111ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  PPNO   subfunc 0x%16.16lx.%16.16lx",
165211ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
165311ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
165411ba5961SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KMA    subfunc 0x%16.16lx.%16.16lx",
165511ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
165611ba5961SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
165713209ad0SChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  KDSA   subfunc 0x%16.16lx.%16.16lx",
165813209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
165913209ad0SChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1660173aec2dSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1661173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1662173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1663173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1664173aec2dSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
16654f45b90eSChristian Borntraeger 	VM_EVENT(kvm, 3, "GET: host  DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
16664f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
16674f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
16684f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
16694f45b90eSChristian Borntraeger 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
167011ba5961SChristian Borntraeger 
16710a763c78SDavid Hildenbrand 	return 0;
16720a763c78SDavid Hildenbrand }
1673346fa2f8SChristian Borntraeger 
1674658b6edaSMichael Mueller static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1675658b6edaSMichael Mueller {
1676658b6edaSMichael Mueller 	int ret = -ENXIO;
1677658b6edaSMichael Mueller 
1678658b6edaSMichael Mueller 	switch (attr->attr) {
1679658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_PROCESSOR:
1680658b6edaSMichael Mueller 		ret = kvm_s390_get_processor(kvm, attr);
1681658b6edaSMichael Mueller 		break;
1682658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MACHINE:
1683658b6edaSMichael Mueller 		ret = kvm_s390_get_machine(kvm, attr);
1684658b6edaSMichael Mueller 		break;
168515c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
168615c9705fSDavid Hildenbrand 		ret = kvm_s390_get_processor_feat(kvm, attr);
168715c9705fSDavid Hildenbrand 		break;
168815c9705fSDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_FEAT:
168915c9705fSDavid Hildenbrand 		ret = kvm_s390_get_machine_feat(kvm, attr);
169015c9705fSDavid Hildenbrand 		break;
16910a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
16920a763c78SDavid Hildenbrand 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
16930a763c78SDavid Hildenbrand 		break;
16940a763c78SDavid Hildenbrand 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
16950a763c78SDavid Hildenbrand 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
16960a763c78SDavid Hildenbrand 		break;
1697658b6edaSMichael Mueller 	}
1698658b6edaSMichael Mueller 	return ret;
1699658b6edaSMichael Mueller }
1700658b6edaSMichael Mueller 
1701f2061656SDominik Dingel static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1702f2061656SDominik Dingel {
1703f2061656SDominik Dingel 	int ret;
1704f2061656SDominik Dingel 
1705f2061656SDominik Dingel 	switch (attr->group) {
17064f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
17078c0a7ce6SDominik Dingel 		ret = kvm_s390_set_mem_control(kvm, attr);
17084f718eabSDominik Dingel 		break;
170972f25020SJason J. Herne 	case KVM_S390_VM_TOD:
171072f25020SJason J. Herne 		ret = kvm_s390_set_tod(kvm, attr);
171172f25020SJason J. Herne 		break;
1712658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1713658b6edaSMichael Mueller 		ret = kvm_s390_set_cpu_model(kvm, attr);
1714658b6edaSMichael Mueller 		break;
1715a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1716a374e892STony Krowiak 		ret = kvm_s390_vm_set_crypto(kvm, attr);
1717a374e892STony Krowiak 		break;
1718190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1719190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_set_migration(kvm, attr);
1720190df4a2SClaudio Imbrenda 		break;
1721f2061656SDominik Dingel 	default:
1722f2061656SDominik Dingel 		ret = -ENXIO;
1723f2061656SDominik Dingel 		break;
1724f2061656SDominik Dingel 	}
1725f2061656SDominik Dingel 
1726f2061656SDominik Dingel 	return ret;
1727f2061656SDominik Dingel }
1728f2061656SDominik Dingel 
1729f2061656SDominik Dingel static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1730f2061656SDominik Dingel {
17318c0a7ce6SDominik Dingel 	int ret;
17328c0a7ce6SDominik Dingel 
17338c0a7ce6SDominik Dingel 	switch (attr->group) {
17348c0a7ce6SDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
17358c0a7ce6SDominik Dingel 		ret = kvm_s390_get_mem_control(kvm, attr);
17368c0a7ce6SDominik Dingel 		break;
173772f25020SJason J. Herne 	case KVM_S390_VM_TOD:
173872f25020SJason J. Herne 		ret = kvm_s390_get_tod(kvm, attr);
173972f25020SJason J. Herne 		break;
1740658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1741658b6edaSMichael Mueller 		ret = kvm_s390_get_cpu_model(kvm, attr);
1742658b6edaSMichael Mueller 		break;
1743190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1744190df4a2SClaudio Imbrenda 		ret = kvm_s390_vm_get_migration(kvm, attr);
1745190df4a2SClaudio Imbrenda 		break;
17468c0a7ce6SDominik Dingel 	default:
17478c0a7ce6SDominik Dingel 		ret = -ENXIO;
17488c0a7ce6SDominik Dingel 		break;
17498c0a7ce6SDominik Dingel 	}
17508c0a7ce6SDominik Dingel 
17518c0a7ce6SDominik Dingel 	return ret;
1752f2061656SDominik Dingel }
1753f2061656SDominik Dingel 
1754f2061656SDominik Dingel static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1755f2061656SDominik Dingel {
1756f2061656SDominik Dingel 	int ret;
1757f2061656SDominik Dingel 
1758f2061656SDominik Dingel 	switch (attr->group) {
17594f718eabSDominik Dingel 	case KVM_S390_VM_MEM_CTRL:
17604f718eabSDominik Dingel 		switch (attr->attr) {
17614f718eabSDominik Dingel 		case KVM_S390_VM_MEM_ENABLE_CMMA:
17624f718eabSDominik Dingel 		case KVM_S390_VM_MEM_CLR_CMMA:
1763f9cbd9b0SDavid Hildenbrand 			ret = sclp.has_cmma ? 0 : -ENXIO;
1764f9cbd9b0SDavid Hildenbrand 			break;
17658c0a7ce6SDominik Dingel 		case KVM_S390_VM_MEM_LIMIT_SIZE:
17664f718eabSDominik Dingel 			ret = 0;
17674f718eabSDominik Dingel 			break;
17684f718eabSDominik Dingel 		default:
17694f718eabSDominik Dingel 			ret = -ENXIO;
17704f718eabSDominik Dingel 			break;
17714f718eabSDominik Dingel 		}
17724f718eabSDominik Dingel 		break;
177372f25020SJason J. Herne 	case KVM_S390_VM_TOD:
177472f25020SJason J. Herne 		switch (attr->attr) {
177572f25020SJason J. Herne 		case KVM_S390_VM_TOD_LOW:
177672f25020SJason J. Herne 		case KVM_S390_VM_TOD_HIGH:
177772f25020SJason J. Herne 			ret = 0;
177872f25020SJason J. Herne 			break;
177972f25020SJason J. Herne 		default:
178072f25020SJason J. Herne 			ret = -ENXIO;
178172f25020SJason J. Herne 			break;
178272f25020SJason J. Herne 		}
178372f25020SJason J. Herne 		break;
1784658b6edaSMichael Mueller 	case KVM_S390_VM_CPU_MODEL:
1785658b6edaSMichael Mueller 		switch (attr->attr) {
1786658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_PROCESSOR:
1787658b6edaSMichael Mueller 		case KVM_S390_VM_CPU_MACHINE:
178815c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
178915c9705fSDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_FEAT:
17900a763c78SDavid Hildenbrand 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1791346fa2f8SChristian Borntraeger 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1792658b6edaSMichael Mueller 			ret = 0;
1793658b6edaSMichael Mueller 			break;
1794658b6edaSMichael Mueller 		default:
1795658b6edaSMichael Mueller 			ret = -ENXIO;
1796658b6edaSMichael Mueller 			break;
1797658b6edaSMichael Mueller 		}
1798658b6edaSMichael Mueller 		break;
1799a374e892STony Krowiak 	case KVM_S390_VM_CRYPTO:
1800a374e892STony Krowiak 		switch (attr->attr) {
1801a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1802a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1803a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1804a374e892STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1805a374e892STony Krowiak 			ret = 0;
1806a374e892STony Krowiak 			break;
180737940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_ENABLE_APIE:
180837940fb0STony Krowiak 		case KVM_S390_VM_CRYPTO_DISABLE_APIE:
180937940fb0STony Krowiak 			ret = ap_instructions_available() ? 0 : -ENXIO;
181037940fb0STony Krowiak 			break;
1811a374e892STony Krowiak 		default:
1812a374e892STony Krowiak 			ret = -ENXIO;
1813a374e892STony Krowiak 			break;
1814a374e892STony Krowiak 		}
1815a374e892STony Krowiak 		break;
1816190df4a2SClaudio Imbrenda 	case KVM_S390_VM_MIGRATION:
1817190df4a2SClaudio Imbrenda 		ret = 0;
1818190df4a2SClaudio Imbrenda 		break;
1819f2061656SDominik Dingel 	default:
1820f2061656SDominik Dingel 		ret = -ENXIO;
1821f2061656SDominik Dingel 		break;
1822f2061656SDominik Dingel 	}
1823f2061656SDominik Dingel 
1824f2061656SDominik Dingel 	return ret;
1825f2061656SDominik Dingel }
1826f2061656SDominik Dingel 
182730ee2a98SJason J. Herne static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
182830ee2a98SJason J. Herne {
182930ee2a98SJason J. Herne 	uint8_t *keys;
183030ee2a98SJason J. Herne 	uint64_t hva;
18314f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
183230ee2a98SJason J. Herne 
183330ee2a98SJason J. Herne 	if (args->flags != 0)
183430ee2a98SJason J. Herne 		return -EINVAL;
183530ee2a98SJason J. Herne 
183630ee2a98SJason J. Herne 	/* Is this guest using storage keys? */
183755531b74SJanosch Frank 	if (!mm_uses_skeys(current->mm))
183830ee2a98SJason J. Herne 		return KVM_S390_GET_SKEYS_NONE;
183930ee2a98SJason J. Herne 
184030ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
184130ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
184230ee2a98SJason J. Herne 		return -EINVAL;
184330ee2a98SJason J. Herne 
1844c4196218SChristian Borntraeger 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
184530ee2a98SJason J. Herne 	if (!keys)
184630ee2a98SJason J. Herne 		return -ENOMEM;
184730ee2a98SJason J. Herne 
1848d8ed45c5SMichel Lespinasse 	mmap_read_lock(current->mm);
18494f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
185030ee2a98SJason J. Herne 	for (i = 0; i < args->count; i++) {
185130ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
185230ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
185330ee2a98SJason J. Herne 			r = -EFAULT;
1854d3ed1ceeSMartin Schwidefsky 			break;
185530ee2a98SJason J. Herne 		}
185630ee2a98SJason J. Herne 
1857154c8c19SDavid Hildenbrand 		r = get_guest_storage_key(current->mm, hva, &keys[i]);
1858154c8c19SDavid Hildenbrand 		if (r)
1859d3ed1ceeSMartin Schwidefsky 			break;
186030ee2a98SJason J. Herne 	}
18614f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1862d8ed45c5SMichel Lespinasse 	mmap_read_unlock(current->mm);
186330ee2a98SJason J. Herne 
1864d3ed1ceeSMartin Schwidefsky 	if (!r) {
186530ee2a98SJason J. Herne 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
186630ee2a98SJason J. Herne 				 sizeof(uint8_t) * args->count);
186730ee2a98SJason J. Herne 		if (r)
186830ee2a98SJason J. Herne 			r = -EFAULT;
1869d3ed1ceeSMartin Schwidefsky 	}
1870d3ed1ceeSMartin Schwidefsky 
187130ee2a98SJason J. Herne 	kvfree(keys);
187230ee2a98SJason J. Herne 	return r;
187330ee2a98SJason J. Herne }
187430ee2a98SJason J. Herne 
187530ee2a98SJason J. Herne static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
187630ee2a98SJason J. Herne {
187730ee2a98SJason J. Herne 	uint8_t *keys;
187830ee2a98SJason J. Herne 	uint64_t hva;
18794f899147SChristian Borntraeger 	int srcu_idx, i, r = 0;
1880bd096f64SJanosch Frank 	bool unlocked;
188130ee2a98SJason J. Herne 
188230ee2a98SJason J. Herne 	if (args->flags != 0)
188330ee2a98SJason J. Herne 		return -EINVAL;
188430ee2a98SJason J. Herne 
188530ee2a98SJason J. Herne 	/* Enforce sane limit on memory allocation */
188630ee2a98SJason J. Herne 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
188730ee2a98SJason J. Herne 		return -EINVAL;
188830ee2a98SJason J. Herne 
1889c4196218SChristian Borntraeger 	keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
189030ee2a98SJason J. Herne 	if (!keys)
189130ee2a98SJason J. Herne 		return -ENOMEM;
189230ee2a98SJason J. Herne 
189330ee2a98SJason J. Herne 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
189430ee2a98SJason J. Herne 			   sizeof(uint8_t) * args->count);
189530ee2a98SJason J. Herne 	if (r) {
189630ee2a98SJason J. Herne 		r = -EFAULT;
189730ee2a98SJason J. Herne 		goto out;
189830ee2a98SJason J. Herne 	}
189930ee2a98SJason J. Herne 
190030ee2a98SJason J. Herne 	/* Enable storage key handling for the guest */
190114d4a425SDominik Dingel 	r = s390_enable_skey();
190214d4a425SDominik Dingel 	if (r)
190314d4a425SDominik Dingel 		goto out;
190430ee2a98SJason J. Herne 
1905bd096f64SJanosch Frank 	i = 0;
1906d8ed45c5SMichel Lespinasse 	mmap_read_lock(current->mm);
19074f899147SChristian Borntraeger 	srcu_idx = srcu_read_lock(&kvm->srcu);
1908bd096f64SJanosch Frank         while (i < args->count) {
1909bd096f64SJanosch Frank 		unlocked = false;
191030ee2a98SJason J. Herne 		hva = gfn_to_hva(kvm, args->start_gfn + i);
191130ee2a98SJason J. Herne 		if (kvm_is_error_hva(hva)) {
191230ee2a98SJason J. Herne 			r = -EFAULT;
1913d3ed1ceeSMartin Schwidefsky 			break;
191430ee2a98SJason J. Herne 		}
191530ee2a98SJason J. Herne 
191630ee2a98SJason J. Herne 		/* Lowest order bit is reserved */
191730ee2a98SJason J. Herne 		if (keys[i] & 0x01) {
191830ee2a98SJason J. Herne 			r = -EINVAL;
1919d3ed1ceeSMartin Schwidefsky 			break;
192030ee2a98SJason J. Herne 		}
192130ee2a98SJason J. Herne 
1922fe69eabfSDavid Hildenbrand 		r = set_guest_storage_key(current->mm, hva, keys[i], 0);
1923bd096f64SJanosch Frank 		if (r) {
192464019a2eSPeter Xu 			r = fixup_user_fault(current->mm, hva,
1925bd096f64SJanosch Frank 					     FAULT_FLAG_WRITE, &unlocked);
192630ee2a98SJason J. Herne 			if (r)
1927d3ed1ceeSMartin Schwidefsky 				break;
192830ee2a98SJason J. Herne 		}
1929bd096f64SJanosch Frank 		if (!r)
1930bd096f64SJanosch Frank 			i++;
1931bd096f64SJanosch Frank 	}
19324f899147SChristian Borntraeger 	srcu_read_unlock(&kvm->srcu, srcu_idx);
1933d8ed45c5SMichel Lespinasse 	mmap_read_unlock(current->mm);
193430ee2a98SJason J. Herne out:
193530ee2a98SJason J. Herne 	kvfree(keys);
193630ee2a98SJason J. Herne 	return r;
193730ee2a98SJason J. Herne }
193830ee2a98SJason J. Herne 
19394036e387SClaudio Imbrenda /*
19404036e387SClaudio Imbrenda  * Base address and length must be sent at the start of each block, therefore
19414036e387SClaudio Imbrenda  * it's cheaper to send some clean data, as long as it's less than the size of
19424036e387SClaudio Imbrenda  * two longs.
19434036e387SClaudio Imbrenda  */
19444036e387SClaudio Imbrenda #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
19454036e387SClaudio Imbrenda /* for consistency */
19464036e387SClaudio Imbrenda #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
19474036e387SClaudio Imbrenda 
19484036e387SClaudio Imbrenda /*
1949afdad616SClaudio Imbrenda  * Similar to gfn_to_memslot, but returns the index of a memslot also when the
1950afdad616SClaudio Imbrenda  * address falls in a hole. In that case the index of one of the memslots
1951afdad616SClaudio Imbrenda  * bordering the hole is returned.
1952afdad616SClaudio Imbrenda  */
1953afdad616SClaudio Imbrenda static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn)
1954afdad616SClaudio Imbrenda {
1955afdad616SClaudio Imbrenda 	int start = 0, end = slots->used_slots;
1956afdad616SClaudio Imbrenda 	int slot = atomic_read(&slots->lru_slot);
1957afdad616SClaudio Imbrenda 	struct kvm_memory_slot *memslots = slots->memslots;
1958afdad616SClaudio Imbrenda 
1959afdad616SClaudio Imbrenda 	if (gfn >= memslots[slot].base_gfn &&
1960afdad616SClaudio Imbrenda 	    gfn < memslots[slot].base_gfn + memslots[slot].npages)
1961afdad616SClaudio Imbrenda 		return slot;
1962afdad616SClaudio Imbrenda 
1963afdad616SClaudio Imbrenda 	while (start < end) {
1964afdad616SClaudio Imbrenda 		slot = start + (end - start) / 2;
1965afdad616SClaudio Imbrenda 
1966afdad616SClaudio Imbrenda 		if (gfn >= memslots[slot].base_gfn)
1967afdad616SClaudio Imbrenda 			end = slot;
1968afdad616SClaudio Imbrenda 		else
1969afdad616SClaudio Imbrenda 			start = slot + 1;
1970afdad616SClaudio Imbrenda 	}
1971afdad616SClaudio Imbrenda 
197297daa028SSean Christopherson 	if (start >= slots->used_slots)
197397daa028SSean Christopherson 		return slots->used_slots - 1;
197497daa028SSean Christopherson 
1975afdad616SClaudio Imbrenda 	if (gfn >= memslots[start].base_gfn &&
1976afdad616SClaudio Imbrenda 	    gfn < memslots[start].base_gfn + memslots[start].npages) {
1977afdad616SClaudio Imbrenda 		atomic_set(&slots->lru_slot, start);
1978afdad616SClaudio Imbrenda 	}
1979afdad616SClaudio Imbrenda 
1980afdad616SClaudio Imbrenda 	return start;
1981afdad616SClaudio Imbrenda }
1982afdad616SClaudio Imbrenda 
1983afdad616SClaudio Imbrenda static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1984afdad616SClaudio Imbrenda 			      u8 *res, unsigned long bufsize)
1985afdad616SClaudio Imbrenda {
1986afdad616SClaudio Imbrenda 	unsigned long pgstev, hva, cur_gfn = args->start_gfn;
1987afdad616SClaudio Imbrenda 
1988afdad616SClaudio Imbrenda 	args->count = 0;
1989afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
1990afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
1991afdad616SClaudio Imbrenda 		/*
1992afdad616SClaudio Imbrenda 		 * We return an error if the first value was invalid, but we
1993afdad616SClaudio Imbrenda 		 * return successfully if at least one value was copied.
1994afdad616SClaudio Imbrenda 		 */
1995afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
1996afdad616SClaudio Imbrenda 			return args->count ? 0 : -EFAULT;
1997afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
1998afdad616SClaudio Imbrenda 			pgstev = 0;
1999afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
2000afdad616SClaudio Imbrenda 		cur_gfn++;
2001afdad616SClaudio Imbrenda 	}
2002afdad616SClaudio Imbrenda 
2003afdad616SClaudio Imbrenda 	return 0;
2004afdad616SClaudio Imbrenda }
2005afdad616SClaudio Imbrenda 
2006afdad616SClaudio Imbrenda static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
2007afdad616SClaudio Imbrenda 					      unsigned long cur_gfn)
2008afdad616SClaudio Imbrenda {
2009afdad616SClaudio Imbrenda 	int slotidx = gfn_to_memslot_approx(slots, cur_gfn);
2010afdad616SClaudio Imbrenda 	struct kvm_memory_slot *ms = slots->memslots + slotidx;
2011afdad616SClaudio Imbrenda 	unsigned long ofs = cur_gfn - ms->base_gfn;
2012afdad616SClaudio Imbrenda 
2013afdad616SClaudio Imbrenda 	if (ms->base_gfn + ms->npages <= cur_gfn) {
2014afdad616SClaudio Imbrenda 		slotidx--;
2015afdad616SClaudio Imbrenda 		/* If we are above the highest slot, wrap around */
2016afdad616SClaudio Imbrenda 		if (slotidx < 0)
2017afdad616SClaudio Imbrenda 			slotidx = slots->used_slots - 1;
2018afdad616SClaudio Imbrenda 
2019afdad616SClaudio Imbrenda 		ms = slots->memslots + slotidx;
2020afdad616SClaudio Imbrenda 		ofs = 0;
2021afdad616SClaudio Imbrenda 	}
2022afdad616SClaudio Imbrenda 	ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
2023afdad616SClaudio Imbrenda 	while ((slotidx > 0) && (ofs >= ms->npages)) {
2024afdad616SClaudio Imbrenda 		slotidx--;
2025afdad616SClaudio Imbrenda 		ms = slots->memslots + slotidx;
2026afdad616SClaudio Imbrenda 		ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0);
2027afdad616SClaudio Imbrenda 	}
2028afdad616SClaudio Imbrenda 	return ms->base_gfn + ofs;
2029afdad616SClaudio Imbrenda }
2030afdad616SClaudio Imbrenda 
2031afdad616SClaudio Imbrenda static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2032afdad616SClaudio Imbrenda 			     u8 *res, unsigned long bufsize)
2033afdad616SClaudio Imbrenda {
2034afdad616SClaudio Imbrenda 	unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2035afdad616SClaudio Imbrenda 	struct kvm_memslots *slots = kvm_memslots(kvm);
2036afdad616SClaudio Imbrenda 	struct kvm_memory_slot *ms;
2037afdad616SClaudio Imbrenda 
20380774a964SSean Christopherson 	if (unlikely(!slots->used_slots))
20390774a964SSean Christopherson 		return 0;
20400774a964SSean Christopherson 
2041afdad616SClaudio Imbrenda 	cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2042afdad616SClaudio Imbrenda 	ms = gfn_to_memslot(kvm, cur_gfn);
2043afdad616SClaudio Imbrenda 	args->count = 0;
2044afdad616SClaudio Imbrenda 	args->start_gfn = cur_gfn;
2045afdad616SClaudio Imbrenda 	if (!ms)
2046afdad616SClaudio Imbrenda 		return 0;
2047afdad616SClaudio Imbrenda 	next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2048afdad616SClaudio Imbrenda 	mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages;
2049afdad616SClaudio Imbrenda 
2050afdad616SClaudio Imbrenda 	while (args->count < bufsize) {
2051afdad616SClaudio Imbrenda 		hva = gfn_to_hva(kvm, cur_gfn);
2052afdad616SClaudio Imbrenda 		if (kvm_is_error_hva(hva))
2053afdad616SClaudio Imbrenda 			return 0;
2054afdad616SClaudio Imbrenda 		/* Decrement only if we actually flipped the bit to 0 */
2055afdad616SClaudio Imbrenda 		if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2056afdad616SClaudio Imbrenda 			atomic64_dec(&kvm->arch.cmma_dirty_pages);
2057afdad616SClaudio Imbrenda 		if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2058afdad616SClaudio Imbrenda 			pgstev = 0;
2059afdad616SClaudio Imbrenda 		/* Save the value */
2060afdad616SClaudio Imbrenda 		res[args->count++] = (pgstev >> 24) & 0x43;
2061afdad616SClaudio Imbrenda 		/* If the next bit is too far away, stop. */
2062afdad616SClaudio Imbrenda 		if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2063afdad616SClaudio Imbrenda 			return 0;
2064afdad616SClaudio Imbrenda 		/* If we reached the previous "next", find the next one */
2065afdad616SClaudio Imbrenda 		if (cur_gfn == next_gfn)
2066afdad616SClaudio Imbrenda 			next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2067afdad616SClaudio Imbrenda 		/* Reached the end of memory or of the buffer, stop */
2068afdad616SClaudio Imbrenda 		if ((next_gfn >= mem_end) ||
2069afdad616SClaudio Imbrenda 		    (next_gfn - args->start_gfn >= bufsize))
2070afdad616SClaudio Imbrenda 			return 0;
2071afdad616SClaudio Imbrenda 		cur_gfn++;
2072afdad616SClaudio Imbrenda 		/* Reached the end of the current memslot, take the next one. */
2073afdad616SClaudio Imbrenda 		if (cur_gfn - ms->base_gfn >= ms->npages) {
2074afdad616SClaudio Imbrenda 			ms = gfn_to_memslot(kvm, cur_gfn);
2075afdad616SClaudio Imbrenda 			if (!ms)
2076afdad616SClaudio Imbrenda 				return 0;
2077afdad616SClaudio Imbrenda 		}
2078afdad616SClaudio Imbrenda 	}
2079afdad616SClaudio Imbrenda 	return 0;
2080afdad616SClaudio Imbrenda }
2081afdad616SClaudio Imbrenda 
2082afdad616SClaudio Imbrenda /*
20834036e387SClaudio Imbrenda  * This function searches for the next page with dirty CMMA attributes, and
20844036e387SClaudio Imbrenda  * saves the attributes in the buffer up to either the end of the buffer or
20854036e387SClaudio Imbrenda  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
20864036e387SClaudio Imbrenda  * no trailing clean bytes are saved.
20874036e387SClaudio Imbrenda  * In case no dirty bits were found, or if CMMA was not enabled or used, the
20884036e387SClaudio Imbrenda  * output buffer will indicate 0 as length.
20894036e387SClaudio Imbrenda  */
20904036e387SClaudio Imbrenda static int kvm_s390_get_cmma_bits(struct kvm *kvm,
20914036e387SClaudio Imbrenda 				  struct kvm_s390_cmma_log *args)
20924036e387SClaudio Imbrenda {
2093afdad616SClaudio Imbrenda 	unsigned long bufsize;
2094afdad616SClaudio Imbrenda 	int srcu_idx, peek, ret;
2095afdad616SClaudio Imbrenda 	u8 *values;
20964036e387SClaudio Imbrenda 
2097afdad616SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
20984036e387SClaudio Imbrenda 		return -ENXIO;
20994036e387SClaudio Imbrenda 	/* Invalid/unsupported flags were specified */
21004036e387SClaudio Imbrenda 	if (args->flags & ~KVM_S390_CMMA_PEEK)
21014036e387SClaudio Imbrenda 		return -EINVAL;
21024036e387SClaudio Imbrenda 	/* Migration mode query, and we are not doing a migration */
21034036e387SClaudio Imbrenda 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2104afdad616SClaudio Imbrenda 	if (!peek && !kvm->arch.migration_mode)
21054036e387SClaudio Imbrenda 		return -EINVAL;
21064036e387SClaudio Imbrenda 	/* CMMA is disabled or was not used, or the buffer has length zero */
21074036e387SClaudio Imbrenda 	bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2108c9f0a2b8SJanosch Frank 	if (!bufsize || !kvm->mm->context.uses_cmm) {
21094036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
21104036e387SClaudio Imbrenda 		return 0;
21114036e387SClaudio Imbrenda 	}
21124036e387SClaudio Imbrenda 	/* We are not peeking, and there are no dirty pages */
2113afdad616SClaudio Imbrenda 	if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
21144036e387SClaudio Imbrenda 		memset(args, 0, sizeof(*args));
21154036e387SClaudio Imbrenda 		return 0;
21164036e387SClaudio Imbrenda 	}
21174036e387SClaudio Imbrenda 
2118afdad616SClaudio Imbrenda 	values = vmalloc(bufsize);
2119afdad616SClaudio Imbrenda 	if (!values)
21204036e387SClaudio Imbrenda 		return -ENOMEM;
21214036e387SClaudio Imbrenda 
2122d8ed45c5SMichel Lespinasse 	mmap_read_lock(kvm->mm);
21234036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
2124afdad616SClaudio Imbrenda 	if (peek)
2125afdad616SClaudio Imbrenda 		ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2126afdad616SClaudio Imbrenda 	else
2127afdad616SClaudio Imbrenda 		ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
21284036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2129d8ed45c5SMichel Lespinasse 	mmap_read_unlock(kvm->mm);
21304036e387SClaudio Imbrenda 
2131afdad616SClaudio Imbrenda 	if (kvm->arch.migration_mode)
2132afdad616SClaudio Imbrenda 		args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2133afdad616SClaudio Imbrenda 	else
2134afdad616SClaudio Imbrenda 		args->remaining = 0;
21354036e387SClaudio Imbrenda 
2136afdad616SClaudio Imbrenda 	if (copy_to_user((void __user *)args->values, values, args->count))
2137afdad616SClaudio Imbrenda 		ret = -EFAULT;
2138afdad616SClaudio Imbrenda 
2139afdad616SClaudio Imbrenda 	vfree(values);
2140afdad616SClaudio Imbrenda 	return ret;
21414036e387SClaudio Imbrenda }
21424036e387SClaudio Imbrenda 
21434036e387SClaudio Imbrenda /*
21444036e387SClaudio Imbrenda  * This function sets the CMMA attributes for the given pages. If the input
21454036e387SClaudio Imbrenda  * buffer has zero length, no action is taken, otherwise the attributes are
2146c9f0a2b8SJanosch Frank  * set and the mm->context.uses_cmm flag is set.
21474036e387SClaudio Imbrenda  */
21484036e387SClaudio Imbrenda static int kvm_s390_set_cmma_bits(struct kvm *kvm,
21494036e387SClaudio Imbrenda 				  const struct kvm_s390_cmma_log *args)
21504036e387SClaudio Imbrenda {
21514036e387SClaudio Imbrenda 	unsigned long hva, mask, pgstev, i;
21524036e387SClaudio Imbrenda 	uint8_t *bits;
21534036e387SClaudio Imbrenda 	int srcu_idx, r = 0;
21544036e387SClaudio Imbrenda 
21554036e387SClaudio Imbrenda 	mask = args->mask;
21564036e387SClaudio Imbrenda 
21574036e387SClaudio Imbrenda 	if (!kvm->arch.use_cmma)
21584036e387SClaudio Imbrenda 		return -ENXIO;
21594036e387SClaudio Imbrenda 	/* invalid/unsupported flags */
21604036e387SClaudio Imbrenda 	if (args->flags != 0)
21614036e387SClaudio Imbrenda 		return -EINVAL;
21624036e387SClaudio Imbrenda 	/* Enforce sane limit on memory allocation */
21634036e387SClaudio Imbrenda 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
21644036e387SClaudio Imbrenda 		return -EINVAL;
21654036e387SClaudio Imbrenda 	/* Nothing to do */
21664036e387SClaudio Imbrenda 	if (args->count == 0)
21674036e387SClaudio Imbrenda 		return 0;
21684036e387SClaudio Imbrenda 
216942bc47b3SKees Cook 	bits = vmalloc(array_size(sizeof(*bits), args->count));
21704036e387SClaudio Imbrenda 	if (!bits)
21714036e387SClaudio Imbrenda 		return -ENOMEM;
21724036e387SClaudio Imbrenda 
21734036e387SClaudio Imbrenda 	r = copy_from_user(bits, (void __user *)args->values, args->count);
21744036e387SClaudio Imbrenda 	if (r) {
21754036e387SClaudio Imbrenda 		r = -EFAULT;
21764036e387SClaudio Imbrenda 		goto out;
21774036e387SClaudio Imbrenda 	}
21784036e387SClaudio Imbrenda 
2179d8ed45c5SMichel Lespinasse 	mmap_read_lock(kvm->mm);
21804036e387SClaudio Imbrenda 	srcu_idx = srcu_read_lock(&kvm->srcu);
21814036e387SClaudio Imbrenda 	for (i = 0; i < args->count; i++) {
21824036e387SClaudio Imbrenda 		hva = gfn_to_hva(kvm, args->start_gfn + i);
21834036e387SClaudio Imbrenda 		if (kvm_is_error_hva(hva)) {
21844036e387SClaudio Imbrenda 			r = -EFAULT;
21854036e387SClaudio Imbrenda 			break;
21864036e387SClaudio Imbrenda 		}
21874036e387SClaudio Imbrenda 
21884036e387SClaudio Imbrenda 		pgstev = bits[i];
21894036e387SClaudio Imbrenda 		pgstev = pgstev << 24;
21901bab1c02SClaudio Imbrenda 		mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
21914036e387SClaudio Imbrenda 		set_pgste_bits(kvm->mm, hva, mask, pgstev);
21924036e387SClaudio Imbrenda 	}
21934036e387SClaudio Imbrenda 	srcu_read_unlock(&kvm->srcu, srcu_idx);
2194d8ed45c5SMichel Lespinasse 	mmap_read_unlock(kvm->mm);
21954036e387SClaudio Imbrenda 
2196c9f0a2b8SJanosch Frank 	if (!kvm->mm->context.uses_cmm) {
2197d8ed45c5SMichel Lespinasse 		mmap_write_lock(kvm->mm);
2198c9f0a2b8SJanosch Frank 		kvm->mm->context.uses_cmm = 1;
2199d8ed45c5SMichel Lespinasse 		mmap_write_unlock(kvm->mm);
22004036e387SClaudio Imbrenda 	}
22014036e387SClaudio Imbrenda out:
22024036e387SClaudio Imbrenda 	vfree(bits);
22034036e387SClaudio Imbrenda 	return r;
22044036e387SClaudio Imbrenda }
22054036e387SClaudio Imbrenda 
220629b40f10SJanosch Frank static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp)
220729b40f10SJanosch Frank {
220829b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
220929b40f10SJanosch Frank 	u16 rc, rrc;
221029b40f10SJanosch Frank 	int ret = 0;
221129b40f10SJanosch Frank 	int i;
221229b40f10SJanosch Frank 
221329b40f10SJanosch Frank 	/*
221429b40f10SJanosch Frank 	 * We ignore failures and try to destroy as many CPUs as possible.
221529b40f10SJanosch Frank 	 * At the same time we must not free the assigned resources when
221629b40f10SJanosch Frank 	 * this fails, as the ultravisor has still access to that memory.
221729b40f10SJanosch Frank 	 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
221829b40f10SJanosch Frank 	 * behind.
221929b40f10SJanosch Frank 	 * We want to return the first failure rc and rrc, though.
222029b40f10SJanosch Frank 	 */
222129b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
222229b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
222329b40f10SJanosch Frank 		if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) {
222429b40f10SJanosch Frank 			*rcp = rc;
222529b40f10SJanosch Frank 			*rrcp = rrc;
222629b40f10SJanosch Frank 			ret = -EIO;
222729b40f10SJanosch Frank 		}
222829b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
222929b40f10SJanosch Frank 	}
223029b40f10SJanosch Frank 	return ret;
223129b40f10SJanosch Frank }
223229b40f10SJanosch Frank 
223329b40f10SJanosch Frank static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
223429b40f10SJanosch Frank {
223529b40f10SJanosch Frank 	int i, r = 0;
223629b40f10SJanosch Frank 	u16 dummy;
223729b40f10SJanosch Frank 
223829b40f10SJanosch Frank 	struct kvm_vcpu *vcpu;
223929b40f10SJanosch Frank 
224029b40f10SJanosch Frank 	kvm_for_each_vcpu(i, vcpu, kvm) {
224129b40f10SJanosch Frank 		mutex_lock(&vcpu->mutex);
224229b40f10SJanosch Frank 		r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
224329b40f10SJanosch Frank 		mutex_unlock(&vcpu->mutex);
224429b40f10SJanosch Frank 		if (r)
224529b40f10SJanosch Frank 			break;
224629b40f10SJanosch Frank 	}
224729b40f10SJanosch Frank 	if (r)
224829b40f10SJanosch Frank 		kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
224929b40f10SJanosch Frank 	return r;
225029b40f10SJanosch Frank }
225129b40f10SJanosch Frank 
225229b40f10SJanosch Frank static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
225329b40f10SJanosch Frank {
225429b40f10SJanosch Frank 	int r = 0;
225529b40f10SJanosch Frank 	u16 dummy;
225629b40f10SJanosch Frank 	void __user *argp = (void __user *)cmd->data;
225729b40f10SJanosch Frank 
225829b40f10SJanosch Frank 	switch (cmd->cmd) {
225929b40f10SJanosch Frank 	case KVM_PV_ENABLE: {
226029b40f10SJanosch Frank 		r = -EINVAL;
226129b40f10SJanosch Frank 		if (kvm_s390_pv_is_protected(kvm))
226229b40f10SJanosch Frank 			break;
226329b40f10SJanosch Frank 
226429b40f10SJanosch Frank 		/*
226529b40f10SJanosch Frank 		 *  FMT 4 SIE needs esca. As we never switch back to bsca from
226629b40f10SJanosch Frank 		 *  esca, we need no cleanup in the error cases below
226729b40f10SJanosch Frank 		 */
226829b40f10SJanosch Frank 		r = sca_switch_to_extended(kvm);
226929b40f10SJanosch Frank 		if (r)
227029b40f10SJanosch Frank 			break;
227129b40f10SJanosch Frank 
2272d8ed45c5SMichel Lespinasse 		mmap_write_lock(current->mm);
2273fa0c5eabSJanosch Frank 		r = gmap_mark_unmergeable();
2274d8ed45c5SMichel Lespinasse 		mmap_write_unlock(current->mm);
2275fa0c5eabSJanosch Frank 		if (r)
2276fa0c5eabSJanosch Frank 			break;
2277fa0c5eabSJanosch Frank 
227829b40f10SJanosch Frank 		r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
227929b40f10SJanosch Frank 		if (r)
228029b40f10SJanosch Frank 			break;
228129b40f10SJanosch Frank 
228229b40f10SJanosch Frank 		r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
228329b40f10SJanosch Frank 		if (r)
228429b40f10SJanosch Frank 			kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
22850890ddeaSChristian Borntraeger 
22860890ddeaSChristian Borntraeger 		/* we need to block service interrupts from now on */
22870890ddeaSChristian Borntraeger 		set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
228829b40f10SJanosch Frank 		break;
228929b40f10SJanosch Frank 	}
229029b40f10SJanosch Frank 	case KVM_PV_DISABLE: {
229129b40f10SJanosch Frank 		r = -EINVAL;
229229b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
229329b40f10SJanosch Frank 			break;
229429b40f10SJanosch Frank 
229529b40f10SJanosch Frank 		r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
229629b40f10SJanosch Frank 		/*
229729b40f10SJanosch Frank 		 * If a CPU could not be destroyed, destroy VM will also fail.
229829b40f10SJanosch Frank 		 * There is no point in trying to destroy it. Instead return
229929b40f10SJanosch Frank 		 * the rc and rrc from the first CPU that failed destroying.
230029b40f10SJanosch Frank 		 */
230129b40f10SJanosch Frank 		if (r)
230229b40f10SJanosch Frank 			break;
230329b40f10SJanosch Frank 		r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc);
23040890ddeaSChristian Borntraeger 
23050890ddeaSChristian Borntraeger 		/* no need to block service interrupts any more */
23060890ddeaSChristian Borntraeger 		clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
230729b40f10SJanosch Frank 		break;
230829b40f10SJanosch Frank 	}
230929b40f10SJanosch Frank 	case KVM_PV_SET_SEC_PARMS: {
231029b40f10SJanosch Frank 		struct kvm_s390_pv_sec_parm parms = {};
231129b40f10SJanosch Frank 		void *hdr;
231229b40f10SJanosch Frank 
231329b40f10SJanosch Frank 		r = -EINVAL;
231429b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
231529b40f10SJanosch Frank 			break;
231629b40f10SJanosch Frank 
231729b40f10SJanosch Frank 		r = -EFAULT;
231829b40f10SJanosch Frank 		if (copy_from_user(&parms, argp, sizeof(parms)))
231929b40f10SJanosch Frank 			break;
232029b40f10SJanosch Frank 
232129b40f10SJanosch Frank 		/* Currently restricted to 8KB */
232229b40f10SJanosch Frank 		r = -EINVAL;
232329b40f10SJanosch Frank 		if (parms.length > PAGE_SIZE * 2)
232429b40f10SJanosch Frank 			break;
232529b40f10SJanosch Frank 
232629b40f10SJanosch Frank 		r = -ENOMEM;
232729b40f10SJanosch Frank 		hdr = vmalloc(parms.length);
232829b40f10SJanosch Frank 		if (!hdr)
232929b40f10SJanosch Frank 			break;
233029b40f10SJanosch Frank 
233129b40f10SJanosch Frank 		r = -EFAULT;
233229b40f10SJanosch Frank 		if (!copy_from_user(hdr, (void __user *)parms.origin,
233329b40f10SJanosch Frank 				    parms.length))
233429b40f10SJanosch Frank 			r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
233529b40f10SJanosch Frank 						      &cmd->rc, &cmd->rrc);
233629b40f10SJanosch Frank 
233729b40f10SJanosch Frank 		vfree(hdr);
233829b40f10SJanosch Frank 		break;
233929b40f10SJanosch Frank 	}
234029b40f10SJanosch Frank 	case KVM_PV_UNPACK: {
234129b40f10SJanosch Frank 		struct kvm_s390_pv_unp unp = {};
234229b40f10SJanosch Frank 
234329b40f10SJanosch Frank 		r = -EINVAL;
23441ed576a2SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
234529b40f10SJanosch Frank 			break;
234629b40f10SJanosch Frank 
234729b40f10SJanosch Frank 		r = -EFAULT;
234829b40f10SJanosch Frank 		if (copy_from_user(&unp, argp, sizeof(unp)))
234929b40f10SJanosch Frank 			break;
235029b40f10SJanosch Frank 
235129b40f10SJanosch Frank 		r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
235229b40f10SJanosch Frank 				       &cmd->rc, &cmd->rrc);
235329b40f10SJanosch Frank 		break;
235429b40f10SJanosch Frank 	}
235529b40f10SJanosch Frank 	case KVM_PV_VERIFY: {
235629b40f10SJanosch Frank 		r = -EINVAL;
235729b40f10SJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
235829b40f10SJanosch Frank 			break;
235929b40f10SJanosch Frank 
236029b40f10SJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
236129b40f10SJanosch Frank 				  UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
236229b40f10SJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
236329b40f10SJanosch Frank 			     cmd->rrc);
236429b40f10SJanosch Frank 		break;
236529b40f10SJanosch Frank 	}
2366e0d2773dSJanosch Frank 	case KVM_PV_PREP_RESET: {
2367e0d2773dSJanosch Frank 		r = -EINVAL;
2368e0d2773dSJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2369e0d2773dSJanosch Frank 			break;
2370e0d2773dSJanosch Frank 
2371e0d2773dSJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2372e0d2773dSJanosch Frank 				  UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2373e0d2773dSJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2374e0d2773dSJanosch Frank 			     cmd->rc, cmd->rrc);
2375e0d2773dSJanosch Frank 		break;
2376e0d2773dSJanosch Frank 	}
2377e0d2773dSJanosch Frank 	case KVM_PV_UNSHARE_ALL: {
2378e0d2773dSJanosch Frank 		r = -EINVAL;
2379e0d2773dSJanosch Frank 		if (!kvm_s390_pv_is_protected(kvm))
2380e0d2773dSJanosch Frank 			break;
2381e0d2773dSJanosch Frank 
2382e0d2773dSJanosch Frank 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2383e0d2773dSJanosch Frank 				  UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2384e0d2773dSJanosch Frank 		KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2385e0d2773dSJanosch Frank 			     cmd->rc, cmd->rrc);
2386e0d2773dSJanosch Frank 		break;
2387e0d2773dSJanosch Frank 	}
238829b40f10SJanosch Frank 	default:
238929b40f10SJanosch Frank 		r = -ENOTTY;
239029b40f10SJanosch Frank 	}
239129b40f10SJanosch Frank 	return r;
239229b40f10SJanosch Frank }
239329b40f10SJanosch Frank 
2394b0c632dbSHeiko Carstens long kvm_arch_vm_ioctl(struct file *filp,
2395b0c632dbSHeiko Carstens 		       unsigned int ioctl, unsigned long arg)
2396b0c632dbSHeiko Carstens {
2397b0c632dbSHeiko Carstens 	struct kvm *kvm = filp->private_data;
2398b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
2399f2061656SDominik Dingel 	struct kvm_device_attr attr;
2400b0c632dbSHeiko Carstens 	int r;
2401b0c632dbSHeiko Carstens 
2402b0c632dbSHeiko Carstens 	switch (ioctl) {
2403ba5c1e9bSCarsten Otte 	case KVM_S390_INTERRUPT: {
2404ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
2405ba5c1e9bSCarsten Otte 
2406ba5c1e9bSCarsten Otte 		r = -EFAULT;
2407ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
2408ba5c1e9bSCarsten Otte 			break;
2409ba5c1e9bSCarsten Otte 		r = kvm_s390_inject_vm(kvm, &s390int);
2410ba5c1e9bSCarsten Otte 		break;
2411ba5c1e9bSCarsten Otte 	}
241284223598SCornelia Huck 	case KVM_CREATE_IRQCHIP: {
241384223598SCornelia Huck 		struct kvm_irq_routing_entry routing;
241484223598SCornelia Huck 
241584223598SCornelia Huck 		r = -EINVAL;
241684223598SCornelia Huck 		if (kvm->arch.use_irqchip) {
241784223598SCornelia Huck 			/* Set up dummy routing. */
241884223598SCornelia Huck 			memset(&routing, 0, sizeof(routing));
2419152b2839SNicholas Krause 			r = kvm_set_irq_routing(kvm, &routing, 0, 0);
242084223598SCornelia Huck 		}
242184223598SCornelia Huck 		break;
242284223598SCornelia Huck 	}
2423f2061656SDominik Dingel 	case KVM_SET_DEVICE_ATTR: {
2424f2061656SDominik Dingel 		r = -EFAULT;
2425f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2426f2061656SDominik Dingel 			break;
2427f2061656SDominik Dingel 		r = kvm_s390_vm_set_attr(kvm, &attr);
2428f2061656SDominik Dingel 		break;
2429f2061656SDominik Dingel 	}
2430f2061656SDominik Dingel 	case KVM_GET_DEVICE_ATTR: {
2431f2061656SDominik Dingel 		r = -EFAULT;
2432f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2433f2061656SDominik Dingel 			break;
2434f2061656SDominik Dingel 		r = kvm_s390_vm_get_attr(kvm, &attr);
2435f2061656SDominik Dingel 		break;
2436f2061656SDominik Dingel 	}
2437f2061656SDominik Dingel 	case KVM_HAS_DEVICE_ATTR: {
2438f2061656SDominik Dingel 		r = -EFAULT;
2439f2061656SDominik Dingel 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2440f2061656SDominik Dingel 			break;
2441f2061656SDominik Dingel 		r = kvm_s390_vm_has_attr(kvm, &attr);
2442f2061656SDominik Dingel 		break;
2443f2061656SDominik Dingel 	}
244430ee2a98SJason J. Herne 	case KVM_S390_GET_SKEYS: {
244530ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
244630ee2a98SJason J. Herne 
244730ee2a98SJason J. Herne 		r = -EFAULT;
244830ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
244930ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
245030ee2a98SJason J. Herne 			break;
245130ee2a98SJason J. Herne 		r = kvm_s390_get_skeys(kvm, &args);
245230ee2a98SJason J. Herne 		break;
245330ee2a98SJason J. Herne 	}
245430ee2a98SJason J. Herne 	case KVM_S390_SET_SKEYS: {
245530ee2a98SJason J. Herne 		struct kvm_s390_skeys args;
245630ee2a98SJason J. Herne 
245730ee2a98SJason J. Herne 		r = -EFAULT;
245830ee2a98SJason J. Herne 		if (copy_from_user(&args, argp,
245930ee2a98SJason J. Herne 				   sizeof(struct kvm_s390_skeys)))
246030ee2a98SJason J. Herne 			break;
246130ee2a98SJason J. Herne 		r = kvm_s390_set_skeys(kvm, &args);
246230ee2a98SJason J. Herne 		break;
246330ee2a98SJason J. Herne 	}
24644036e387SClaudio Imbrenda 	case KVM_S390_GET_CMMA_BITS: {
24654036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
24664036e387SClaudio Imbrenda 
24674036e387SClaudio Imbrenda 		r = -EFAULT;
24684036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
24694036e387SClaudio Imbrenda 			break;
24701de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
24714036e387SClaudio Imbrenda 		r = kvm_s390_get_cmma_bits(kvm, &args);
24721de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
24734036e387SClaudio Imbrenda 		if (!r) {
24744036e387SClaudio Imbrenda 			r = copy_to_user(argp, &args, sizeof(args));
24754036e387SClaudio Imbrenda 			if (r)
24764036e387SClaudio Imbrenda 				r = -EFAULT;
24774036e387SClaudio Imbrenda 		}
24784036e387SClaudio Imbrenda 		break;
24794036e387SClaudio Imbrenda 	}
24804036e387SClaudio Imbrenda 	case KVM_S390_SET_CMMA_BITS: {
24814036e387SClaudio Imbrenda 		struct kvm_s390_cmma_log args;
24824036e387SClaudio Imbrenda 
24834036e387SClaudio Imbrenda 		r = -EFAULT;
24844036e387SClaudio Imbrenda 		if (copy_from_user(&args, argp, sizeof(args)))
24854036e387SClaudio Imbrenda 			break;
24861de1ea7eSChristian Borntraeger 		mutex_lock(&kvm->slots_lock);
24874036e387SClaudio Imbrenda 		r = kvm_s390_set_cmma_bits(kvm, &args);
24881de1ea7eSChristian Borntraeger 		mutex_unlock(&kvm->slots_lock);
24894036e387SClaudio Imbrenda 		break;
24904036e387SClaudio Imbrenda 	}
249129b40f10SJanosch Frank 	case KVM_S390_PV_COMMAND: {
249229b40f10SJanosch Frank 		struct kvm_pv_cmd args;
249329b40f10SJanosch Frank 
2494fe28c786SJanosch Frank 		/* protvirt means user sigp */
2495fe28c786SJanosch Frank 		kvm->arch.user_cpu_state_ctrl = 1;
249629b40f10SJanosch Frank 		r = 0;
249729b40f10SJanosch Frank 		if (!is_prot_virt_host()) {
249829b40f10SJanosch Frank 			r = -EINVAL;
249929b40f10SJanosch Frank 			break;
250029b40f10SJanosch Frank 		}
250129b40f10SJanosch Frank 		if (copy_from_user(&args, argp, sizeof(args))) {
250229b40f10SJanosch Frank 			r = -EFAULT;
250329b40f10SJanosch Frank 			break;
250429b40f10SJanosch Frank 		}
250529b40f10SJanosch Frank 		if (args.flags) {
250629b40f10SJanosch Frank 			r = -EINVAL;
250729b40f10SJanosch Frank 			break;
250829b40f10SJanosch Frank 		}
250929b40f10SJanosch Frank 		mutex_lock(&kvm->lock);
251029b40f10SJanosch Frank 		r = kvm_s390_handle_pv(kvm, &args);
251129b40f10SJanosch Frank 		mutex_unlock(&kvm->lock);
251229b40f10SJanosch Frank 		if (copy_to_user(argp, &args, sizeof(args))) {
251329b40f10SJanosch Frank 			r = -EFAULT;
251429b40f10SJanosch Frank 			break;
251529b40f10SJanosch Frank 		}
251629b40f10SJanosch Frank 		break;
251729b40f10SJanosch Frank 	}
2518b0c632dbSHeiko Carstens 	default:
2519367e1319SAvi Kivity 		r = -ENOTTY;
2520b0c632dbSHeiko Carstens 	}
2521b0c632dbSHeiko Carstens 
2522b0c632dbSHeiko Carstens 	return r;
2523b0c632dbSHeiko Carstens }
2524b0c632dbSHeiko Carstens 
252545c9b47cSTony Krowiak static int kvm_s390_apxa_installed(void)
252645c9b47cSTony Krowiak {
2527e585b24aSTony Krowiak 	struct ap_config_info info;
252845c9b47cSTony Krowiak 
2529e585b24aSTony Krowiak 	if (ap_instructions_available()) {
2530e585b24aSTony Krowiak 		if (ap_qci(&info) == 0)
2531e585b24aSTony Krowiak 			return info.apxa;
253245c9b47cSTony Krowiak 	}
253345c9b47cSTony Krowiak 
253445c9b47cSTony Krowiak 	return 0;
253545c9b47cSTony Krowiak }
253645c9b47cSTony Krowiak 
2537e585b24aSTony Krowiak /*
2538e585b24aSTony Krowiak  * The format of the crypto control block (CRYCB) is specified in the 3 low
2539e585b24aSTony Krowiak  * order bits of the CRYCB designation (CRYCBD) field as follows:
2540e585b24aSTony Krowiak  * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2541e585b24aSTony Krowiak  *	     AP extended addressing (APXA) facility are installed.
2542e585b24aSTony Krowiak  * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2543e585b24aSTony Krowiak  * Format 2: Both the APXA and MSAX3 facilities are installed
2544e585b24aSTony Krowiak  */
254545c9b47cSTony Krowiak static void kvm_s390_set_crycb_format(struct kvm *kvm)
254645c9b47cSTony Krowiak {
254745c9b47cSTony Krowiak 	kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
254845c9b47cSTony Krowiak 
2549e585b24aSTony Krowiak 	/* Clear the CRYCB format bits - i.e., set format 0 by default */
2550e585b24aSTony Krowiak 	kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2551e585b24aSTony Krowiak 
2552e585b24aSTony Krowiak 	/* Check whether MSAX3 is installed */
2553e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
2554e585b24aSTony Krowiak 		return;
2555e585b24aSTony Krowiak 
255645c9b47cSTony Krowiak 	if (kvm_s390_apxa_installed())
255745c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
255845c9b47cSTony Krowiak 	else
255945c9b47cSTony Krowiak 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
256045c9b47cSTony Krowiak }
256145c9b47cSTony Krowiak 
25620e237e44SPierre Morel void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
25630e237e44SPierre Morel 			       unsigned long *aqm, unsigned long *adm)
25640e237e44SPierre Morel {
25650e237e44SPierre Morel 	struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
25660e237e44SPierre Morel 
25670e237e44SPierre Morel 	mutex_lock(&kvm->lock);
25680e237e44SPierre Morel 	kvm_s390_vcpu_block_all(kvm);
25690e237e44SPierre Morel 
25700e237e44SPierre Morel 	switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
25710e237e44SPierre Morel 	case CRYCB_FORMAT2: /* APCB1 use 256 bits */
25720e237e44SPierre Morel 		memcpy(crycb->apcb1.apm, apm, 32);
25730e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
25740e237e44SPierre Morel 			 apm[0], apm[1], apm[2], apm[3]);
25750e237e44SPierre Morel 		memcpy(crycb->apcb1.aqm, aqm, 32);
25760e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
25770e237e44SPierre Morel 			 aqm[0], aqm[1], aqm[2], aqm[3]);
25780e237e44SPierre Morel 		memcpy(crycb->apcb1.adm, adm, 32);
25790e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
25800e237e44SPierre Morel 			 adm[0], adm[1], adm[2], adm[3]);
25810e237e44SPierre Morel 		break;
25820e237e44SPierre Morel 	case CRYCB_FORMAT1:
25830e237e44SPierre Morel 	case CRYCB_FORMAT0: /* Fall through both use APCB0 */
25840e237e44SPierre Morel 		memcpy(crycb->apcb0.apm, apm, 8);
25850e237e44SPierre Morel 		memcpy(crycb->apcb0.aqm, aqm, 2);
25860e237e44SPierre Morel 		memcpy(crycb->apcb0.adm, adm, 2);
25870e237e44SPierre Morel 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
25880e237e44SPierre Morel 			 apm[0], *((unsigned short *)aqm),
25890e237e44SPierre Morel 			 *((unsigned short *)adm));
25900e237e44SPierre Morel 		break;
25910e237e44SPierre Morel 	default:	/* Can not happen */
25920e237e44SPierre Morel 		break;
25930e237e44SPierre Morel 	}
25940e237e44SPierre Morel 
25950e237e44SPierre Morel 	/* recreate the shadow crycb for each vcpu */
25960e237e44SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
25970e237e44SPierre Morel 	kvm_s390_vcpu_unblock_all(kvm);
25980e237e44SPierre Morel 	mutex_unlock(&kvm->lock);
25990e237e44SPierre Morel }
26000e237e44SPierre Morel EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
26010e237e44SPierre Morel 
260242104598STony Krowiak void kvm_arch_crypto_clear_masks(struct kvm *kvm)
260342104598STony Krowiak {
260442104598STony Krowiak 	mutex_lock(&kvm->lock);
260542104598STony Krowiak 	kvm_s390_vcpu_block_all(kvm);
260642104598STony Krowiak 
260742104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb0, 0,
260842104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb0));
260942104598STony Krowiak 	memset(&kvm->arch.crypto.crycb->apcb1, 0,
261042104598STony Krowiak 	       sizeof(kvm->arch.crypto.crycb->apcb1));
261142104598STony Krowiak 
26120e237e44SPierre Morel 	VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
26136cc571b1SPierre Morel 	/* recreate the shadow crycb for each vcpu */
26146cc571b1SPierre Morel 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
261542104598STony Krowiak 	kvm_s390_vcpu_unblock_all(kvm);
261642104598STony Krowiak 	mutex_unlock(&kvm->lock);
261742104598STony Krowiak }
261842104598STony Krowiak EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
261942104598STony Krowiak 
26209bb0ec09SDavid Hildenbrand static u64 kvm_s390_get_initial_cpuid(void)
26219d8d5786SMichael Mueller {
26229bb0ec09SDavid Hildenbrand 	struct cpuid cpuid;
26239bb0ec09SDavid Hildenbrand 
26249bb0ec09SDavid Hildenbrand 	get_cpu_id(&cpuid);
26259bb0ec09SDavid Hildenbrand 	cpuid.version = 0xff;
26269bb0ec09SDavid Hildenbrand 	return *((u64 *) &cpuid);
26279d8d5786SMichael Mueller }
26289d8d5786SMichael Mueller 
2629c54f0d6aSDavid Hildenbrand static void kvm_s390_crypto_init(struct kvm *kvm)
26305102ee87STony Krowiak {
2631c54f0d6aSDavid Hildenbrand 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
263245c9b47cSTony Krowiak 	kvm_s390_set_crycb_format(kvm);
2633*1e753732STony Krowiak 	init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
26345102ee87STony Krowiak 
2635e585b24aSTony Krowiak 	if (!test_kvm_facility(kvm, 76))
2636e585b24aSTony Krowiak 		return;
2637e585b24aSTony Krowiak 
2638ed6f76b4STony Krowiak 	/* Enable AES/DEA protected key functions by default */
2639ed6f76b4STony Krowiak 	kvm->arch.crypto.aes_kw = 1;
2640ed6f76b4STony Krowiak 	kvm->arch.crypto.dea_kw = 1;
2641ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
2642ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
2643ed6f76b4STony Krowiak 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
2644ed6f76b4STony Krowiak 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
26455102ee87STony Krowiak }
26465102ee87STony Krowiak 
26477d43bafcSEugene (jno) Dvurechenski static void sca_dispose(struct kvm *kvm)
26487d43bafcSEugene (jno) Dvurechenski {
26497d43bafcSEugene (jno) Dvurechenski 	if (kvm->arch.use_esca)
26505e044315SEugene (jno) Dvurechenski 		free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
26517d43bafcSEugene (jno) Dvurechenski 	else
26527d43bafcSEugene (jno) Dvurechenski 		free_page((unsigned long)(kvm->arch.sca));
26537d43bafcSEugene (jno) Dvurechenski 	kvm->arch.sca = NULL;
26547d43bafcSEugene (jno) Dvurechenski }
26557d43bafcSEugene (jno) Dvurechenski 
2656e08b9637SCarsten Otte int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
2657b0c632dbSHeiko Carstens {
2658c4196218SChristian Borntraeger 	gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
26599d8d5786SMichael Mueller 	int i, rc;
2660b0c632dbSHeiko Carstens 	char debug_name[16];
2661f6c137ffSChristian Borntraeger 	static unsigned long sca_offset;
2662b0c632dbSHeiko Carstens 
2663e08b9637SCarsten Otte 	rc = -EINVAL;
2664e08b9637SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
2665e08b9637SCarsten Otte 	if (type & ~KVM_VM_S390_UCONTROL)
2666e08b9637SCarsten Otte 		goto out_err;
2667e08b9637SCarsten Otte 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
2668e08b9637SCarsten Otte 		goto out_err;
2669e08b9637SCarsten Otte #else
2670e08b9637SCarsten Otte 	if (type)
2671e08b9637SCarsten Otte 		goto out_err;
2672e08b9637SCarsten Otte #endif
2673e08b9637SCarsten Otte 
2674b0c632dbSHeiko Carstens 	rc = s390_enable_sie();
2675b0c632dbSHeiko Carstens 	if (rc)
2676d89f5effSJan Kiszka 		goto out_err;
2677b0c632dbSHeiko Carstens 
2678b290411aSCarsten Otte 	rc = -ENOMEM;
2679b290411aSCarsten Otte 
268076a6dd72SDavid Hildenbrand 	if (!sclp.has_64bscao)
268176a6dd72SDavid Hildenbrand 		alloc_flags |= GFP_DMA;
26825e044315SEugene (jno) Dvurechenski 	rwlock_init(&kvm->arch.sca_lock);
26839ac96d75SDavid Hildenbrand 	/* start with basic SCA */
268476a6dd72SDavid Hildenbrand 	kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
2685b0c632dbSHeiko Carstens 	if (!kvm->arch.sca)
2686d89f5effSJan Kiszka 		goto out_err;
26870d9ce162SJunaid Shahid 	mutex_lock(&kvm_lock);
2688c5c2c393SDavid Hildenbrand 	sca_offset += 16;
2689bc784cceSEugene (jno) Dvurechenski 	if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2690c5c2c393SDavid Hildenbrand 		sca_offset = 0;
2691bc784cceSEugene (jno) Dvurechenski 	kvm->arch.sca = (struct bsca_block *)
2692bc784cceSEugene (jno) Dvurechenski 			((char *) kvm->arch.sca + sca_offset);
26930d9ce162SJunaid Shahid 	mutex_unlock(&kvm_lock);
2694b0c632dbSHeiko Carstens 
2695b0c632dbSHeiko Carstens 	sprintf(debug_name, "kvm-%u", current->pid);
2696b0c632dbSHeiko Carstens 
26971cb9cf72SChristian Borntraeger 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2698b0c632dbSHeiko Carstens 	if (!kvm->arch.dbf)
269940f5b735SDominik Dingel 		goto out_err;
2700b0c632dbSHeiko Carstens 
270119114bebSMichael Mueller 	BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
2702c54f0d6aSDavid Hildenbrand 	kvm->arch.sie_page2 =
2703c4196218SChristian Borntraeger 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
2704c54f0d6aSDavid Hildenbrand 	if (!kvm->arch.sie_page2)
270540f5b735SDominik Dingel 		goto out_err;
27069d8d5786SMichael Mueller 
270725c84dbaSMichael Mueller 	kvm->arch.sie_page2->kvm = kvm;
2708c54f0d6aSDavid Hildenbrand 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2709c3b9e3e1SChristian Borntraeger 
2710c3b9e3e1SChristian Borntraeger 	for (i = 0; i < kvm_s390_fac_size(); i++) {
271117e89e13SSven Schnelle 		kvm->arch.model.fac_mask[i] = stfle_fac_list[i] &
2712c3b9e3e1SChristian Borntraeger 					      (kvm_s390_fac_base[i] |
2713c3b9e3e1SChristian Borntraeger 					       kvm_s390_fac_ext[i]);
271417e89e13SSven Schnelle 		kvm->arch.model.fac_list[i] = stfle_fac_list[i] &
2715c3b9e3e1SChristian Borntraeger 					      kvm_s390_fac_base[i];
2716c3b9e3e1SChristian Borntraeger 	}
2717346fa2f8SChristian Borntraeger 	kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
2718981467c9SMichael Mueller 
27191935222dSDavid Hildenbrand 	/* we are always in czam mode - even on pre z14 machines */
27201935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
27211935222dSDavid Hildenbrand 	set_kvm_facility(kvm->arch.model.fac_list, 138);
27221935222dSDavid Hildenbrand 	/* we emulate STHYI in kvm */
272395ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
272495ca2cb5SJanosch Frank 	set_kvm_facility(kvm->arch.model.fac_list, 74);
27251bab1c02SClaudio Imbrenda 	if (MACHINE_HAS_TLB_GUEST) {
27261bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
27271bab1c02SClaudio Imbrenda 		set_kvm_facility(kvm->arch.model.fac_list, 147);
27281bab1c02SClaudio Imbrenda 	}
272995ca2cb5SJanosch Frank 
273005f31e3bSPierre Morel 	if (css_general_characteristics.aiv && test_facility(65))
273105f31e3bSPierre Morel 		set_kvm_facility(kvm->arch.model.fac_mask, 65);
273205f31e3bSPierre Morel 
27339bb0ec09SDavid Hildenbrand 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
273437c5f6c8SDavid Hildenbrand 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
27359d8d5786SMichael Mueller 
2736c54f0d6aSDavid Hildenbrand 	kvm_s390_crypto_init(kvm);
27375102ee87STony Krowiak 
273851978393SFei Li 	mutex_init(&kvm->arch.float_int.ais_lock);
2739ba5c1e9bSCarsten Otte 	spin_lock_init(&kvm->arch.float_int.lock);
27406d3da241SJens Freimann 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
27416d3da241SJens Freimann 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
27428a242234SHeiko Carstens 	init_waitqueue_head(&kvm->arch.ipte_wq);
2743a6b7e459SThomas Huth 	mutex_init(&kvm->arch.ipte_mutex);
2744ba5c1e9bSCarsten Otte 
2745b0c632dbSHeiko Carstens 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
274678f26131SChristian Borntraeger 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
2747b0c632dbSHeiko Carstens 
2748e08b9637SCarsten Otte 	if (type & KVM_VM_S390_UCONTROL) {
2749e08b9637SCarsten Otte 		kvm->arch.gmap = NULL;
2750a3a92c31SDominik Dingel 		kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2751e08b9637SCarsten Otte 	} else {
275232e6b236SGuenther Hutzl 		if (sclp.hamax == U64_MAX)
2753ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = TASK_SIZE_MAX;
275432e6b236SGuenther Hutzl 		else
2755ee71d16dSMartin Schwidefsky 			kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
275632e6b236SGuenther Hutzl 						    sclp.hamax + 1);
27576ea427bbSMartin Schwidefsky 		kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2758598841caSCarsten Otte 		if (!kvm->arch.gmap)
275940f5b735SDominik Dingel 			goto out_err;
27602c70fe44SChristian Borntraeger 		kvm->arch.gmap->private = kvm;
276124eb3a82SDominik Dingel 		kvm->arch.gmap->pfault_enabled = 0;
2762e08b9637SCarsten Otte 	}
2763fa6b7fe9SCornelia Huck 
2764c9f0a2b8SJanosch Frank 	kvm->arch.use_pfmfi = sclp.has_pfmfi;
276555531b74SJanosch Frank 	kvm->arch.use_skf = sclp.has_skey;
27668ad35755SDavid Hildenbrand 	spin_lock_init(&kvm->arch.start_stop_lock);
2767a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_init(kvm);
2768cc674ef2SMichael Mueller 	if (use_gisa)
2769d7c5cb01SMichael Mueller 		kvm_s390_gisa_init(kvm);
27708335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
27718ad35755SDavid Hildenbrand 
2772d89f5effSJan Kiszka 	return 0;
2773d89f5effSJan Kiszka out_err:
2774c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
277540f5b735SDominik Dingel 	debug_unregister(kvm->arch.dbf);
27767d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
277778f26131SChristian Borntraeger 	KVM_EVENT(3, "creation of vm failed: %d", rc);
2778d89f5effSJan Kiszka 	return rc;
2779b0c632dbSHeiko Carstens }
2780b0c632dbSHeiko Carstens 
2781d329c035SChristian Borntraeger void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2782d329c035SChristian Borntraeger {
278329b40f10SJanosch Frank 	u16 rc, rrc;
278429b40f10SJanosch Frank 
2785d329c035SChristian Borntraeger 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2786ade38c31SCornelia Huck 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
278767335e63SChristian Borntraeger 	kvm_s390_clear_local_irqs(vcpu);
27883c038e6bSDominik Dingel 	kvm_clear_async_pf_completion_queue(vcpu);
2789bc784cceSEugene (jno) Dvurechenski 	if (!kvm_is_ucontrol(vcpu->kvm))
2790a6e2f683SEugene (jno) Dvurechenski 		sca_del_vcpu(vcpu);
279127e0393fSCarsten Otte 
279227e0393fSCarsten Otte 	if (kvm_is_ucontrol(vcpu->kvm))
27936ea427bbSMartin Schwidefsky 		gmap_remove(vcpu->arch.gmap);
279427e0393fSCarsten Otte 
2795e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma)
2796b31605c1SDominik Dingel 		kvm_s390_vcpu_unsetup_cmma(vcpu);
279729b40f10SJanosch Frank 	/* We can not hold the vcpu mutex here, we are already dying */
279829b40f10SJanosch Frank 	if (kvm_s390_pv_cpu_get_handle(vcpu))
279929b40f10SJanosch Frank 		kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
2800d329c035SChristian Borntraeger 	free_page((unsigned long)(vcpu->arch.sie_block));
2801d329c035SChristian Borntraeger }
2802d329c035SChristian Borntraeger 
2803d329c035SChristian Borntraeger static void kvm_free_vcpus(struct kvm *kvm)
2804d329c035SChristian Borntraeger {
2805d329c035SChristian Borntraeger 	unsigned int i;
2806988a2caeSGleb Natapov 	struct kvm_vcpu *vcpu;
2807d329c035SChristian Borntraeger 
2808988a2caeSGleb Natapov 	kvm_for_each_vcpu(i, vcpu, kvm)
28094543bdc0SSean Christopherson 		kvm_vcpu_destroy(vcpu);
2810988a2caeSGleb Natapov 
2811988a2caeSGleb Natapov 	mutex_lock(&kvm->lock);
2812988a2caeSGleb Natapov 	for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2813d329c035SChristian Borntraeger 		kvm->vcpus[i] = NULL;
2814988a2caeSGleb Natapov 
2815988a2caeSGleb Natapov 	atomic_set(&kvm->online_vcpus, 0);
2816988a2caeSGleb Natapov 	mutex_unlock(&kvm->lock);
2817d329c035SChristian Borntraeger }
2818d329c035SChristian Borntraeger 
2819b0c632dbSHeiko Carstens void kvm_arch_destroy_vm(struct kvm *kvm)
2820b0c632dbSHeiko Carstens {
282129b40f10SJanosch Frank 	u16 rc, rrc;
282229b40f10SJanosch Frank 
2823d329c035SChristian Borntraeger 	kvm_free_vcpus(kvm);
28247d43bafcSEugene (jno) Dvurechenski 	sca_dispose(kvm);
2825d7c5cb01SMichael Mueller 	kvm_s390_gisa_destroy(kvm);
282629b40f10SJanosch Frank 	/*
282729b40f10SJanosch Frank 	 * We are already at the end of life and kvm->lock is not taken.
282829b40f10SJanosch Frank 	 * This is ok as the file descriptor is closed by now and nobody
282929b40f10SJanosch Frank 	 * can mess with the pv state. To avoid lockdep_assert_held from
283029b40f10SJanosch Frank 	 * complaining we do not use kvm_s390_pv_is_protected.
283129b40f10SJanosch Frank 	 */
283229b40f10SJanosch Frank 	if (kvm_s390_pv_get_handle(kvm))
283329b40f10SJanosch Frank 		kvm_s390_pv_deinit_vm(kvm, &rc, &rrc);
283429b40f10SJanosch Frank 	debug_unregister(kvm->arch.dbf);
2835c54f0d6aSDavid Hildenbrand 	free_page((unsigned long)kvm->arch.sie_page2);
283627e0393fSCarsten Otte 	if (!kvm_is_ucontrol(kvm))
28376ea427bbSMartin Schwidefsky 		gmap_remove(kvm->arch.gmap);
2838841b91c5SCornelia Huck 	kvm_s390_destroy_adapters(kvm);
283967335e63SChristian Borntraeger 	kvm_s390_clear_float_irqs(kvm);
2840a3508fbeSDavid Hildenbrand 	kvm_s390_vsie_destroy(kvm);
28418335713aSChristian Borntraeger 	KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2842b0c632dbSHeiko Carstens }
2843b0c632dbSHeiko Carstens 
2844b0c632dbSHeiko Carstens /* Section: vcpu related */
2845dafd032aSDominik Dingel static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2846b0c632dbSHeiko Carstens {
28476ea427bbSMartin Schwidefsky 	vcpu->arch.gmap = gmap_create(current->mm, -1UL);
284827e0393fSCarsten Otte 	if (!vcpu->arch.gmap)
284927e0393fSCarsten Otte 		return -ENOMEM;
28502c70fe44SChristian Borntraeger 	vcpu->arch.gmap->private = vcpu->kvm;
2851dafd032aSDominik Dingel 
285227e0393fSCarsten Otte 	return 0;
285327e0393fSCarsten Otte }
285427e0393fSCarsten Otte 
2855a6e2f683SEugene (jno) Dvurechenski static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2856a6e2f683SEugene (jno) Dvurechenski {
2857a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries())
2858a6940674SDavid Hildenbrand 		return;
28595e044315SEugene (jno) Dvurechenski 	read_lock(&vcpu->kvm->arch.sca_lock);
28607d43bafcSEugene (jno) Dvurechenski 	if (vcpu->kvm->arch.use_esca) {
28617d43bafcSEugene (jno) Dvurechenski 		struct esca_block *sca = vcpu->kvm->arch.sca;
28627d43bafcSEugene (jno) Dvurechenski 
28637d43bafcSEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
28647d43bafcSEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
28657d43bafcSEugene (jno) Dvurechenski 	} else {
2866bc784cceSEugene (jno) Dvurechenski 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2867a6e2f683SEugene (jno) Dvurechenski 
2868a6e2f683SEugene (jno) Dvurechenski 		clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2869a6e2f683SEugene (jno) Dvurechenski 		sca->cpu[vcpu->vcpu_id].sda = 0;
2870a6e2f683SEugene (jno) Dvurechenski 	}
28715e044315SEugene (jno) Dvurechenski 	read_unlock(&vcpu->kvm->arch.sca_lock);
28727d43bafcSEugene (jno) Dvurechenski }
2873a6e2f683SEugene (jno) Dvurechenski 
2874eaa78f34SDavid Hildenbrand static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2875a6e2f683SEugene (jno) Dvurechenski {
2876a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2877a6940674SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2878a6940674SDavid Hildenbrand 
2879a6940674SDavid Hildenbrand 		/* we still need the basic sca for the ipte control */
2880a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2881a6940674SDavid Hildenbrand 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2882f07afa04SDavid Hildenbrand 		return;
2883a6940674SDavid Hildenbrand 	}
2884eaa78f34SDavid Hildenbrand 	read_lock(&vcpu->kvm->arch.sca_lock);
2885eaa78f34SDavid Hildenbrand 	if (vcpu->kvm->arch.use_esca) {
2886eaa78f34SDavid Hildenbrand 		struct esca_block *sca = vcpu->kvm->arch.sca;
28877d43bafcSEugene (jno) Dvurechenski 
2888eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
28897d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
28907d43bafcSEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
28910c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2892eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
28937d43bafcSEugene (jno) Dvurechenski 	} else {
2894eaa78f34SDavid Hildenbrand 		struct bsca_block *sca = vcpu->kvm->arch.sca;
2895a6e2f683SEugene (jno) Dvurechenski 
2896eaa78f34SDavid Hildenbrand 		sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2897a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2898a6e2f683SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2899eaa78f34SDavid Hildenbrand 		set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2900a6e2f683SEugene (jno) Dvurechenski 	}
2901eaa78f34SDavid Hildenbrand 	read_unlock(&vcpu->kvm->arch.sca_lock);
29025e044315SEugene (jno) Dvurechenski }
29035e044315SEugene (jno) Dvurechenski 
29045e044315SEugene (jno) Dvurechenski /* Basic SCA to Extended SCA data copy routines */
29055e044315SEugene (jno) Dvurechenski static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
29065e044315SEugene (jno) Dvurechenski {
29075e044315SEugene (jno) Dvurechenski 	d->sda = s->sda;
29085e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.c = s->sigp_ctrl.c;
29095e044315SEugene (jno) Dvurechenski 	d->sigp_ctrl.scn = s->sigp_ctrl.scn;
29105e044315SEugene (jno) Dvurechenski }
29115e044315SEugene (jno) Dvurechenski 
29125e044315SEugene (jno) Dvurechenski static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
29135e044315SEugene (jno) Dvurechenski {
29145e044315SEugene (jno) Dvurechenski 	int i;
29155e044315SEugene (jno) Dvurechenski 
29165e044315SEugene (jno) Dvurechenski 	d->ipte_control = s->ipte_control;
29175e044315SEugene (jno) Dvurechenski 	d->mcn[0] = s->mcn;
29185e044315SEugene (jno) Dvurechenski 	for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
29195e044315SEugene (jno) Dvurechenski 		sca_copy_entry(&d->cpu[i], &s->cpu[i]);
29205e044315SEugene (jno) Dvurechenski }
29215e044315SEugene (jno) Dvurechenski 
29225e044315SEugene (jno) Dvurechenski static int sca_switch_to_extended(struct kvm *kvm)
29235e044315SEugene (jno) Dvurechenski {
29245e044315SEugene (jno) Dvurechenski 	struct bsca_block *old_sca = kvm->arch.sca;
29255e044315SEugene (jno) Dvurechenski 	struct esca_block *new_sca;
29265e044315SEugene (jno) Dvurechenski 	struct kvm_vcpu *vcpu;
29275e044315SEugene (jno) Dvurechenski 	unsigned int vcpu_idx;
29285e044315SEugene (jno) Dvurechenski 	u32 scaol, scaoh;
29295e044315SEugene (jno) Dvurechenski 
293029b40f10SJanosch Frank 	if (kvm->arch.use_esca)
293129b40f10SJanosch Frank 		return 0;
293229b40f10SJanosch Frank 
2933c4196218SChristian Borntraeger 	new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
29345e044315SEugene (jno) Dvurechenski 	if (!new_sca)
29355e044315SEugene (jno) Dvurechenski 		return -ENOMEM;
29365e044315SEugene (jno) Dvurechenski 
29375e044315SEugene (jno) Dvurechenski 	scaoh = (u32)((u64)(new_sca) >> 32);
29385e044315SEugene (jno) Dvurechenski 	scaol = (u32)(u64)(new_sca) & ~0x3fU;
29395e044315SEugene (jno) Dvurechenski 
29405e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_block_all(kvm);
29415e044315SEugene (jno) Dvurechenski 	write_lock(&kvm->arch.sca_lock);
29425e044315SEugene (jno) Dvurechenski 
29435e044315SEugene (jno) Dvurechenski 	sca_copy_b_to_e(new_sca, old_sca);
29445e044315SEugene (jno) Dvurechenski 
29455e044315SEugene (jno) Dvurechenski 	kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
29465e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaoh = scaoh;
29475e044315SEugene (jno) Dvurechenski 		vcpu->arch.sie_block->scaol = scaol;
29480c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
29495e044315SEugene (jno) Dvurechenski 	}
29505e044315SEugene (jno) Dvurechenski 	kvm->arch.sca = new_sca;
29515e044315SEugene (jno) Dvurechenski 	kvm->arch.use_esca = 1;
29525e044315SEugene (jno) Dvurechenski 
29535e044315SEugene (jno) Dvurechenski 	write_unlock(&kvm->arch.sca_lock);
29545e044315SEugene (jno) Dvurechenski 	kvm_s390_vcpu_unblock_all(kvm);
29555e044315SEugene (jno) Dvurechenski 
29565e044315SEugene (jno) Dvurechenski 	free_page((unsigned long)old_sca);
29575e044315SEugene (jno) Dvurechenski 
29588335713aSChristian Borntraeger 	VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
29598335713aSChristian Borntraeger 		 old_sca, kvm->arch.sca);
29605e044315SEugene (jno) Dvurechenski 	return 0;
29617d43bafcSEugene (jno) Dvurechenski }
2962a6e2f683SEugene (jno) Dvurechenski 
2963a6e2f683SEugene (jno) Dvurechenski static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2964a6e2f683SEugene (jno) Dvurechenski {
29655e044315SEugene (jno) Dvurechenski 	int rc;
29665e044315SEugene (jno) Dvurechenski 
2967a6940674SDavid Hildenbrand 	if (!kvm_s390_use_sca_entries()) {
2968a6940674SDavid Hildenbrand 		if (id < KVM_MAX_VCPUS)
2969a6940674SDavid Hildenbrand 			return true;
2970a6940674SDavid Hildenbrand 		return false;
2971a6940674SDavid Hildenbrand 	}
29725e044315SEugene (jno) Dvurechenski 	if (id < KVM_S390_BSCA_CPU_SLOTS)
29735e044315SEugene (jno) Dvurechenski 		return true;
297476a6dd72SDavid Hildenbrand 	if (!sclp.has_esca || !sclp.has_64bscao)
29755e044315SEugene (jno) Dvurechenski 		return false;
29765e044315SEugene (jno) Dvurechenski 
29775e044315SEugene (jno) Dvurechenski 	mutex_lock(&kvm->lock);
29785e044315SEugene (jno) Dvurechenski 	rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
29795e044315SEugene (jno) Dvurechenski 	mutex_unlock(&kvm->lock);
29805e044315SEugene (jno) Dvurechenski 
29815e044315SEugene (jno) Dvurechenski 	return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2982a6e2f683SEugene (jno) Dvurechenski }
2983a6e2f683SEugene (jno) Dvurechenski 
2984db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2985db0758b2SDavid Hildenbrand static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2986db0758b2SDavid Hildenbrand {
2987db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
29889c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2989db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = get_tod_clock_fast();
29909c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2991db0758b2SDavid Hildenbrand }
2992db0758b2SDavid Hildenbrand 
2993db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2994db0758b2SDavid Hildenbrand static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2995db0758b2SDavid Hildenbrand {
2996db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
29979c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2998db0758b2SDavid Hildenbrand 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2999db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_start = 0;
30009c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3001db0758b2SDavid Hildenbrand }
3002db0758b2SDavid Hildenbrand 
3003db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3004db0758b2SDavid Hildenbrand static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3005db0758b2SDavid Hildenbrand {
3006db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
3007db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = true;
3008db0758b2SDavid Hildenbrand 	__start_cpu_timer_accounting(vcpu);
3009db0758b2SDavid Hildenbrand }
3010db0758b2SDavid Hildenbrand 
3011db0758b2SDavid Hildenbrand /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3012db0758b2SDavid Hildenbrand static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3013db0758b2SDavid Hildenbrand {
3014db0758b2SDavid Hildenbrand 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
3015db0758b2SDavid Hildenbrand 	__stop_cpu_timer_accounting(vcpu);
3016db0758b2SDavid Hildenbrand 	vcpu->arch.cputm_enabled = false;
3017db0758b2SDavid Hildenbrand }
3018db0758b2SDavid Hildenbrand 
3019db0758b2SDavid Hildenbrand static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3020db0758b2SDavid Hildenbrand {
3021db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3022db0758b2SDavid Hildenbrand 	__enable_cpu_timer_accounting(vcpu);
3023db0758b2SDavid Hildenbrand 	preempt_enable();
3024db0758b2SDavid Hildenbrand }
3025db0758b2SDavid Hildenbrand 
3026db0758b2SDavid Hildenbrand static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3027db0758b2SDavid Hildenbrand {
3028db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3029db0758b2SDavid Hildenbrand 	__disable_cpu_timer_accounting(vcpu);
3030db0758b2SDavid Hildenbrand 	preempt_enable();
3031db0758b2SDavid Hildenbrand }
3032db0758b2SDavid Hildenbrand 
30334287f247SDavid Hildenbrand /* set the cpu timer - may only be called from the VCPU thread itself */
30344287f247SDavid Hildenbrand void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
30354287f247SDavid Hildenbrand {
3036db0758b2SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
30379c23a131SDavid Hildenbrand 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3038db0758b2SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled)
3039db0758b2SDavid Hildenbrand 		vcpu->arch.cputm_start = get_tod_clock_fast();
30404287f247SDavid Hildenbrand 	vcpu->arch.sie_block->cputm = cputm;
30419c23a131SDavid Hildenbrand 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3042db0758b2SDavid Hildenbrand 	preempt_enable();
30434287f247SDavid Hildenbrand }
30444287f247SDavid Hildenbrand 
3045db0758b2SDavid Hildenbrand /* update and get the cpu timer - can also be called from other VCPU threads */
30464287f247SDavid Hildenbrand __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
30474287f247SDavid Hildenbrand {
30489c23a131SDavid Hildenbrand 	unsigned int seq;
3049db0758b2SDavid Hildenbrand 	__u64 value;
3050db0758b2SDavid Hildenbrand 
3051db0758b2SDavid Hildenbrand 	if (unlikely(!vcpu->arch.cputm_enabled))
30524287f247SDavid Hildenbrand 		return vcpu->arch.sie_block->cputm;
3053db0758b2SDavid Hildenbrand 
30549c23a131SDavid Hildenbrand 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
30559c23a131SDavid Hildenbrand 	do {
30569c23a131SDavid Hildenbrand 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
30579c23a131SDavid Hildenbrand 		/*
30589c23a131SDavid Hildenbrand 		 * If the writer would ever execute a read in the critical
30599c23a131SDavid Hildenbrand 		 * section, e.g. in irq context, we have a deadlock.
30609c23a131SDavid Hildenbrand 		 */
30619c23a131SDavid Hildenbrand 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3062db0758b2SDavid Hildenbrand 		value = vcpu->arch.sie_block->cputm;
30639c23a131SDavid Hildenbrand 		/* if cputm_start is 0, accounting is being started/stopped */
30649c23a131SDavid Hildenbrand 		if (likely(vcpu->arch.cputm_start))
3065db0758b2SDavid Hildenbrand 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
30669c23a131SDavid Hildenbrand 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
30679c23a131SDavid Hildenbrand 	preempt_enable();
3068db0758b2SDavid Hildenbrand 	return value;
30694287f247SDavid Hildenbrand }
30704287f247SDavid Hildenbrand 
3071b0c632dbSHeiko Carstens void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3072b0c632dbSHeiko Carstens {
30739977e886SHendrik Brueckner 
307437d9df98SDavid Hildenbrand 	gmap_enable(vcpu->arch.enabled_gmap);
3075ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
30765ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3077db0758b2SDavid Hildenbrand 		__start_cpu_timer_accounting(vcpu);
307801a745acSDavid Hildenbrand 	vcpu->cpu = cpu;
3079b0c632dbSHeiko Carstens }
3080b0c632dbSHeiko Carstens 
3081b0c632dbSHeiko Carstens void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3082b0c632dbSHeiko Carstens {
308301a745acSDavid Hildenbrand 	vcpu->cpu = -1;
30845ebda316SDavid Hildenbrand 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3085db0758b2SDavid Hildenbrand 		__stop_cpu_timer_accounting(vcpu);
30869daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
308737d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = gmap_get_enabled();
308837d9df98SDavid Hildenbrand 	gmap_disable(vcpu->arch.enabled_gmap);
30899977e886SHendrik Brueckner 
3090b0c632dbSHeiko Carstens }
3091b0c632dbSHeiko Carstens 
309231928aa5SDominik Dingel void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
309342897d86SMarcelo Tosatti {
309472f25020SJason J. Herne 	mutex_lock(&vcpu->kvm->lock);
3095fdf03650SFan Zhang 	preempt_disable();
309672f25020SJason J. Herne 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3097d16b52cbSDavid Hildenbrand 	vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3098fdf03650SFan Zhang 	preempt_enable();
309972f25020SJason J. Herne 	mutex_unlock(&vcpu->kvm->lock);
310025508824SDavid Hildenbrand 	if (!kvm_is_ucontrol(vcpu->kvm)) {
3101dafd032aSDominik Dingel 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3102eaa78f34SDavid Hildenbrand 		sca_add_vcpu(vcpu);
310325508824SDavid Hildenbrand 	}
31046502a34cSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
31056502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
310637d9df98SDavid Hildenbrand 	/* make vcpu_load load the right gmap on the first trigger */
310737d9df98SDavid Hildenbrand 	vcpu->arch.enabled_gmap = vcpu->arch.gmap;
310842897d86SMarcelo Tosatti }
310942897d86SMarcelo Tosatti 
31108ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
31118ec2fa52SChristian Borntraeger {
31128ec2fa52SChristian Borntraeger 	if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
31138ec2fa52SChristian Borntraeger 	    test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
31148ec2fa52SChristian Borntraeger 		return true;
31158ec2fa52SChristian Borntraeger 	return false;
31168ec2fa52SChristian Borntraeger }
31178ec2fa52SChristian Borntraeger 
31188ec2fa52SChristian Borntraeger static bool kvm_has_pckmo_ecc(struct kvm *kvm)
31198ec2fa52SChristian Borntraeger {
31208ec2fa52SChristian Borntraeger 	/* At least one ECC subfunction must be present */
31218ec2fa52SChristian Borntraeger 	return kvm_has_pckmo_subfunc(kvm, 32) ||
31228ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 33) ||
31238ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 34) ||
31248ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 40) ||
31258ec2fa52SChristian Borntraeger 	       kvm_has_pckmo_subfunc(kvm, 41);
31268ec2fa52SChristian Borntraeger 
31278ec2fa52SChristian Borntraeger }
31288ec2fa52SChristian Borntraeger 
31295102ee87STony Krowiak static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
31305102ee87STony Krowiak {
3131e585b24aSTony Krowiak 	/*
3132e585b24aSTony Krowiak 	 * If the AP instructions are not being interpreted and the MSAX3
3133e585b24aSTony Krowiak 	 * facility is not configured for the guest, there is nothing to set up.
3134e585b24aSTony Krowiak 	 */
3135e585b24aSTony Krowiak 	if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
31365102ee87STony Krowiak 		return;
31375102ee87STony Krowiak 
3138e585b24aSTony Krowiak 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3139a374e892STony Krowiak 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
314037940fb0STony Krowiak 	vcpu->arch.sie_block->eca &= ~ECA_APIE;
31418ec2fa52SChristian Borntraeger 	vcpu->arch.sie_block->ecd &= ~ECD_ECC;
3142a374e892STony Krowiak 
3143e585b24aSTony Krowiak 	if (vcpu->kvm->arch.crypto.apie)
3144e585b24aSTony Krowiak 		vcpu->arch.sie_block->eca |= ECA_APIE;
3145e585b24aSTony Krowiak 
3146e585b24aSTony Krowiak 	/* Set up protected key support */
31478ec2fa52SChristian Borntraeger 	if (vcpu->kvm->arch.crypto.aes_kw) {
3148a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
31498ec2fa52SChristian Borntraeger 		/* ecc is also wrapped with AES key */
31508ec2fa52SChristian Borntraeger 		if (kvm_has_pckmo_ecc(vcpu->kvm))
31518ec2fa52SChristian Borntraeger 			vcpu->arch.sie_block->ecd |= ECD_ECC;
31528ec2fa52SChristian Borntraeger 	}
31538ec2fa52SChristian Borntraeger 
3154a374e892STony Krowiak 	if (vcpu->kvm->arch.crypto.dea_kw)
3155a374e892STony Krowiak 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
31565102ee87STony Krowiak }
31575102ee87STony Krowiak 
3158b31605c1SDominik Dingel void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3159b31605c1SDominik Dingel {
3160b31605c1SDominik Dingel 	free_page(vcpu->arch.sie_block->cbrlo);
3161b31605c1SDominik Dingel 	vcpu->arch.sie_block->cbrlo = 0;
3162b31605c1SDominik Dingel }
3163b31605c1SDominik Dingel 
3164b31605c1SDominik Dingel int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3165b31605c1SDominik Dingel {
3166c4196218SChristian Borntraeger 	vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL_ACCOUNT);
3167b31605c1SDominik Dingel 	if (!vcpu->arch.sie_block->cbrlo)
3168b31605c1SDominik Dingel 		return -ENOMEM;
3169b31605c1SDominik Dingel 	return 0;
3170b31605c1SDominik Dingel }
3171b31605c1SDominik Dingel 
317291520f1aSMichael Mueller static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
317391520f1aSMichael Mueller {
317491520f1aSMichael Mueller 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
317591520f1aSMichael Mueller 
317691520f1aSMichael Mueller 	vcpu->arch.sie_block->ibc = model->ibc;
317780bc79dcSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 7))
3178c54f0d6aSDavid Hildenbrand 		vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
317991520f1aSMichael Mueller }
318091520f1aSMichael Mueller 
3181ff72bb55SSean Christopherson static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3182ff72bb55SSean Christopherson {
3183b31605c1SDominik Dingel 	int rc = 0;
318429b40f10SJanosch Frank 	u16 uvrc, uvrrc;
3185b31288faSKonstantin Weitz 
31869e6dabefSCornelia Huck 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
31879e6dabefSCornelia Huck 						    CPUSTAT_SM |
3188a4a4f191SGuenther Hutzl 						    CPUSTAT_STOPPED);
3189a4a4f191SGuenther Hutzl 
319053df84f8SGuenther Hutzl 	if (test_kvm_facility(vcpu->kvm, 78))
3191ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
319253df84f8SGuenther Hutzl 	else if (test_kvm_facility(vcpu->kvm, 8))
3193ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3194a4a4f191SGuenther Hutzl 
319591520f1aSMichael Mueller 	kvm_s390_vcpu_setup_model(vcpu);
319691520f1aSMichael Mueller 
3197bdab09f3SDavid Hildenbrand 	/* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3198bdab09f3SDavid Hildenbrand 	if (MACHINE_HAS_ESOP)
31990c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3200bd50e8ecSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 9))
32010c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
3202f597d24eSDavid Hildenbrand 	if (test_kvm_facility(vcpu->kvm, 73))
32030c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb |= ECB_TE;
32047feb6bb8SMichael Mueller 
3205c9f0a2b8SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
32060c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3207cd1836f5SJanosch Frank 	if (test_kvm_facility(vcpu->kvm, 130))
32080c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
32090c9d8683SDavid Hildenbrand 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
321048ee7d3aSDavid Hildenbrand 	if (sclp.has_cei)
32110c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_CEI;
321211ad65b7SDavid Hildenbrand 	if (sclp.has_ib)
32130c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_IB;
321437c5f6c8SDavid Hildenbrand 	if (sclp.has_siif)
32150c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SII;
321637c5f6c8SDavid Hildenbrand 	if (sclp.has_sigpif)
32170c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
321818280d8bSMichael Mueller 	if (test_kvm_facility(vcpu->kvm, 129)) {
32190c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->eca |= ECA_VX;
32200c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
322113211ea7SEric Farman 	}
32228fa1696eSCollin L. Walling 	if (test_kvm_facility(vcpu->kvm, 139))
32238fa1696eSCollin L. Walling 		vcpu->arch.sie_block->ecd |= ECD_MEF;
3224a3da7b4aSChristian Borntraeger 	if (test_kvm_facility(vcpu->kvm, 156))
3225a3da7b4aSChristian Borntraeger 		vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3226d7c5cb01SMichael Mueller 	if (vcpu->arch.sie_block->gd) {
3227d7c5cb01SMichael Mueller 		vcpu->arch.sie_block->eca |= ECA_AIV;
3228d7c5cb01SMichael Mueller 		VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3229d7c5cb01SMichael Mueller 			   vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3230d7c5cb01SMichael Mueller 	}
32314e0b1ab7SFan Zhang 	vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
32324e0b1ab7SFan Zhang 					| SDNXC;
3233c6e5f166SFan Zhang 	vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
3234730cd632SFarhan Ali 
3235730cd632SFarhan Ali 	if (sclp.has_kss)
3236ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3237730cd632SFarhan Ali 	else
3238492d8642SThomas Huth 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
32395a5e6536SMatthew Rosato 
3240e6db1d61SDominik Dingel 	if (vcpu->kvm->arch.use_cmma) {
3241b31605c1SDominik Dingel 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
3242b31605c1SDominik Dingel 		if (rc)
3243b31605c1SDominik Dingel 			return rc;
3244b31288faSKonstantin Weitz 	}
32450ac96cafSDavid Hildenbrand 	hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3246ca872302SChristian Borntraeger 	vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
32479d8d5786SMichael Mueller 
324867d49d52SCollin Walling 	vcpu->arch.sie_block->hpid = HPID_KVM;
324967d49d52SCollin Walling 
32505102ee87STony Krowiak 	kvm_s390_vcpu_crypto_setup(vcpu);
32515102ee87STony Krowiak 
325229b40f10SJanosch Frank 	mutex_lock(&vcpu->kvm->lock);
325329b40f10SJanosch Frank 	if (kvm_s390_pv_is_protected(vcpu->kvm)) {
325429b40f10SJanosch Frank 		rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
325529b40f10SJanosch Frank 		if (rc)
325629b40f10SJanosch Frank 			kvm_s390_vcpu_unsetup_cmma(vcpu);
325729b40f10SJanosch Frank 	}
325829b40f10SJanosch Frank 	mutex_unlock(&vcpu->kvm->lock);
325929b40f10SJanosch Frank 
3260b31605c1SDominik Dingel 	return rc;
3261b0c632dbSHeiko Carstens }
3262b0c632dbSHeiko Carstens 
3263897cc38eSSean Christopherson int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3264897cc38eSSean Christopherson {
3265897cc38eSSean Christopherson 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3266897cc38eSSean Christopherson 		return -EINVAL;
3267897cc38eSSean Christopherson 	return 0;
3268897cc38eSSean Christopherson }
3269897cc38eSSean Christopherson 
3270e529ef66SSean Christopherson int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3271b0c632dbSHeiko Carstens {
32727feb6bb8SMichael Mueller 	struct sie_page *sie_page;
3273897cc38eSSean Christopherson 	int rc;
32744d47555aSCarsten Otte 
3275da72ca4dSQingFeng Hao 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
3276c4196218SChristian Borntraeger 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
32777feb6bb8SMichael Mueller 	if (!sie_page)
3278e529ef66SSean Christopherson 		return -ENOMEM;
3279b0c632dbSHeiko Carstens 
32807feb6bb8SMichael Mueller 	vcpu->arch.sie_block = &sie_page->sie_block;
32817feb6bb8SMichael Mueller 	vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
32827feb6bb8SMichael Mueller 
3283efed1104SDavid Hildenbrand 	/* the real guest size will always be smaller than msl */
3284efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->mso = 0;
3285efed1104SDavid Hildenbrand 	vcpu->arch.sie_block->msl = sclp.hamax;
3286efed1104SDavid Hildenbrand 
3287e529ef66SSean Christopherson 	vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3288ba5c1e9bSCarsten Otte 	spin_lock_init(&vcpu->arch.local_int.lock);
3289e529ef66SSean Christopherson 	vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin;
32904b9f9525SMichael Mueller 	if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
32914b9f9525SMichael Mueller 		vcpu->arch.sie_block->gd |= GISA_FORMAT1;
32929c23a131SDavid Hildenbrand 	seqcount_init(&vcpu->arch.cputm_seqcount);
3293ba5c1e9bSCarsten Otte 
3294321f8ee5SSean Christopherson 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3295321f8ee5SSean Christopherson 	kvm_clear_async_pf_completion_queue(vcpu);
3296321f8ee5SSean Christopherson 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3297321f8ee5SSean Christopherson 				    KVM_SYNC_GPRS |
3298321f8ee5SSean Christopherson 				    KVM_SYNC_ACRS |
3299321f8ee5SSean Christopherson 				    KVM_SYNC_CRS |
3300321f8ee5SSean Christopherson 				    KVM_SYNC_ARCH0 |
330123a60f83SCollin Walling 				    KVM_SYNC_PFAULT |
330223a60f83SCollin Walling 				    KVM_SYNC_DIAG318;
3303321f8ee5SSean Christopherson 	kvm_s390_set_prefix(vcpu, 0);
3304321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 64))
3305321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3306321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 82))
3307321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3308321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 133))
3309321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3310321f8ee5SSean Christopherson 	if (test_kvm_facility(vcpu->kvm, 156))
3311321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3312321f8ee5SSean Christopherson 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
3313321f8ee5SSean Christopherson 	 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3314321f8ee5SSean Christopherson 	 */
3315321f8ee5SSean Christopherson 	if (MACHINE_HAS_VX)
3316321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3317321f8ee5SSean Christopherson 	else
3318321f8ee5SSean Christopherson 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3319321f8ee5SSean Christopherson 
3320321f8ee5SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm)) {
3321321f8ee5SSean Christopherson 		rc = __kvm_ucontrol_vcpu_init(vcpu);
3322321f8ee5SSean Christopherson 		if (rc)
3323a2017f17SSean Christopherson 			goto out_free_sie_block;
3324321f8ee5SSean Christopherson 	}
3325321f8ee5SSean Christopherson 
3326e529ef66SSean Christopherson 	VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3327e529ef66SSean Christopherson 		 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3328e529ef66SSean Christopherson 	trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3329b0c632dbSHeiko Carstens 
3330ff72bb55SSean Christopherson 	rc = kvm_s390_vcpu_setup(vcpu);
3331ff72bb55SSean Christopherson 	if (rc)
3332ff72bb55SSean Christopherson 		goto out_ucontrol_uninit;
3333e529ef66SSean Christopherson 	return 0;
3334e529ef66SSean Christopherson 
3335ff72bb55SSean Christopherson out_ucontrol_uninit:
3336ff72bb55SSean Christopherson 	if (kvm_is_ucontrol(vcpu->kvm))
3337ff72bb55SSean Christopherson 		gmap_remove(vcpu->arch.gmap);
33387b06bf2fSWei Yongjun out_free_sie_block:
33397b06bf2fSWei Yongjun 	free_page((unsigned long)(vcpu->arch.sie_block));
3340e529ef66SSean Christopherson 	return rc;
3341b0c632dbSHeiko Carstens }
3342b0c632dbSHeiko Carstens 
3343b0c632dbSHeiko Carstens int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3344b0c632dbSHeiko Carstens {
33459a022067SDavid Hildenbrand 	return kvm_s390_vcpu_has_irq(vcpu, 0);
3346b0c632dbSHeiko Carstens }
3347b0c632dbSHeiko Carstens 
3348199b5763SLongpeng(Mike) bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
3349199b5763SLongpeng(Mike) {
33500546c63dSLongpeng(Mike) 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3351199b5763SLongpeng(Mike) }
3352199b5763SLongpeng(Mike) 
335327406cd5SChristian Borntraeger void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
335449b99e1eSChristian Borntraeger {
3355805de8f4SPeter Zijlstra 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
335661a6df54SDavid Hildenbrand 	exit_sie(vcpu);
335749b99e1eSChristian Borntraeger }
335849b99e1eSChristian Borntraeger 
335927406cd5SChristian Borntraeger void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
336049b99e1eSChristian Borntraeger {
3361805de8f4SPeter Zijlstra 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
336249b99e1eSChristian Borntraeger }
336349b99e1eSChristian Borntraeger 
33648e236546SChristian Borntraeger static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
33658e236546SChristian Borntraeger {
3366805de8f4SPeter Zijlstra 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
336761a6df54SDavid Hildenbrand 	exit_sie(vcpu);
33688e236546SChristian Borntraeger }
33698e236546SChristian Borntraeger 
33709ea59728SDavid Hildenbrand bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
33719ea59728SDavid Hildenbrand {
33729ea59728SDavid Hildenbrand 	return atomic_read(&vcpu->arch.sie_block->prog20) &
33739ea59728SDavid Hildenbrand 	       (PROG_BLOCK_SIE | PROG_REQUEST);
33749ea59728SDavid Hildenbrand }
33759ea59728SDavid Hildenbrand 
33768e236546SChristian Borntraeger static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
33778e236546SChristian Borntraeger {
33789bf9fde2SJason J. Herne 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
33798e236546SChristian Borntraeger }
33808e236546SChristian Borntraeger 
338149b99e1eSChristian Borntraeger /*
33829ea59728SDavid Hildenbrand  * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
338349b99e1eSChristian Borntraeger  * If the CPU is not running (e.g. waiting as idle) the function will
338449b99e1eSChristian Borntraeger  * return immediately. */
338549b99e1eSChristian Borntraeger void exit_sie(struct kvm_vcpu *vcpu)
338649b99e1eSChristian Borntraeger {
3387ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
33889ea59728SDavid Hildenbrand 	kvm_s390_vsie_kick(vcpu);
338949b99e1eSChristian Borntraeger 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
339049b99e1eSChristian Borntraeger 		cpu_relax();
339149b99e1eSChristian Borntraeger }
339249b99e1eSChristian Borntraeger 
33938e236546SChristian Borntraeger /* Kick a guest cpu out of SIE to process a request synchronously */
33948e236546SChristian Borntraeger void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
339549b99e1eSChristian Borntraeger {
33968e236546SChristian Borntraeger 	kvm_make_request(req, vcpu);
33978e236546SChristian Borntraeger 	kvm_s390_vcpu_request(vcpu);
339849b99e1eSChristian Borntraeger }
339949b99e1eSChristian Borntraeger 
3400414d3b07SMartin Schwidefsky static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
3401414d3b07SMartin Schwidefsky 			      unsigned long end)
34022c70fe44SChristian Borntraeger {
34032c70fe44SChristian Borntraeger 	struct kvm *kvm = gmap->private;
34042c70fe44SChristian Borntraeger 	struct kvm_vcpu *vcpu;
3405414d3b07SMartin Schwidefsky 	unsigned long prefix;
3406414d3b07SMartin Schwidefsky 	int i;
34072c70fe44SChristian Borntraeger 
340865d0b0d4SDavid Hildenbrand 	if (gmap_is_shadow(gmap))
340965d0b0d4SDavid Hildenbrand 		return;
3410414d3b07SMartin Schwidefsky 	if (start >= 1UL << 31)
3411414d3b07SMartin Schwidefsky 		/* We are only interested in prefix pages */
3412414d3b07SMartin Schwidefsky 		return;
34132c70fe44SChristian Borntraeger 	kvm_for_each_vcpu(i, vcpu, kvm) {
34142c70fe44SChristian Borntraeger 		/* match against both prefix pages */
3415414d3b07SMartin Schwidefsky 		prefix = kvm_s390_get_prefix(vcpu);
3416414d3b07SMartin Schwidefsky 		if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3417414d3b07SMartin Schwidefsky 			VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3418414d3b07SMartin Schwidefsky 				   start, end);
34198e236546SChristian Borntraeger 			kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
34202c70fe44SChristian Borntraeger 		}
34212c70fe44SChristian Borntraeger 	}
34222c70fe44SChristian Borntraeger }
34232c70fe44SChristian Borntraeger 
34248b905d28SChristian Borntraeger bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
34258b905d28SChristian Borntraeger {
34268b905d28SChristian Borntraeger 	/* do not poll with more than halt_poll_max_steal percent of steal time */
34278b905d28SChristian Borntraeger 	if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
34288b905d28SChristian Borntraeger 	    halt_poll_max_steal) {
34298b905d28SChristian Borntraeger 		vcpu->stat.halt_no_poll_steal++;
34308b905d28SChristian Borntraeger 		return true;
34318b905d28SChristian Borntraeger 	}
34328b905d28SChristian Borntraeger 	return false;
34338b905d28SChristian Borntraeger }
34348b905d28SChristian Borntraeger 
3435b6d33834SChristoffer Dall int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3436b6d33834SChristoffer Dall {
3437b6d33834SChristoffer Dall 	/* kvm common code refers to this, but never calls it */
3438b6d33834SChristoffer Dall 	BUG();
3439b6d33834SChristoffer Dall 	return 0;
3440b6d33834SChristoffer Dall }
3441b6d33834SChristoffer Dall 
344214eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
344314eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
344414eebd91SCarsten Otte {
344514eebd91SCarsten Otte 	int r = -EINVAL;
344614eebd91SCarsten Otte 
344714eebd91SCarsten Otte 	switch (reg->id) {
344829b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
344929b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->todpr,
345029b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
345129b7c71bSCarsten Otte 		break;
345229b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
345329b7c71bSCarsten Otte 		r = put_user(vcpu->arch.sie_block->epoch,
345429b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
345529b7c71bSCarsten Otte 		break;
345646a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
34574287f247SDavid Hildenbrand 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
345846a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
345946a6dd1cSJason J. herne 		break;
346046a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
346146a6dd1cSJason J. herne 		r = put_user(vcpu->arch.sie_block->ckc,
346246a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
346346a6dd1cSJason J. herne 		break;
3464536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3465536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_token,
3466536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3467536336c2SDominik Dingel 		break;
3468536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3469536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_compare,
3470536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3471536336c2SDominik Dingel 		break;
3472536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3473536336c2SDominik Dingel 		r = put_user(vcpu->arch.pfault_select,
3474536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3475536336c2SDominik Dingel 		break;
3476672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3477672550fbSChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->pp,
3478672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3479672550fbSChristian Borntraeger 		break;
3480afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
3481afa45ff5SChristian Borntraeger 		r = put_user(vcpu->arch.sie_block->gbea,
3482afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
3483afa45ff5SChristian Borntraeger 		break;
348414eebd91SCarsten Otte 	default:
348514eebd91SCarsten Otte 		break;
348614eebd91SCarsten Otte 	}
348714eebd91SCarsten Otte 
348814eebd91SCarsten Otte 	return r;
348914eebd91SCarsten Otte }
349014eebd91SCarsten Otte 
349114eebd91SCarsten Otte static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
349214eebd91SCarsten Otte 					   struct kvm_one_reg *reg)
349314eebd91SCarsten Otte {
349414eebd91SCarsten Otte 	int r = -EINVAL;
34954287f247SDavid Hildenbrand 	__u64 val;
349614eebd91SCarsten Otte 
349714eebd91SCarsten Otte 	switch (reg->id) {
349829b7c71bSCarsten Otte 	case KVM_REG_S390_TODPR:
349929b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->todpr,
350029b7c71bSCarsten Otte 			     (u32 __user *)reg->addr);
350129b7c71bSCarsten Otte 		break;
350229b7c71bSCarsten Otte 	case KVM_REG_S390_EPOCHDIFF:
350329b7c71bSCarsten Otte 		r = get_user(vcpu->arch.sie_block->epoch,
350429b7c71bSCarsten Otte 			     (u64 __user *)reg->addr);
350529b7c71bSCarsten Otte 		break;
350646a6dd1cSJason J. herne 	case KVM_REG_S390_CPU_TIMER:
35074287f247SDavid Hildenbrand 		r = get_user(val, (u64 __user *)reg->addr);
35084287f247SDavid Hildenbrand 		if (!r)
35094287f247SDavid Hildenbrand 			kvm_s390_set_cpu_timer(vcpu, val);
351046a6dd1cSJason J. herne 		break;
351146a6dd1cSJason J. herne 	case KVM_REG_S390_CLOCK_COMP:
351246a6dd1cSJason J. herne 		r = get_user(vcpu->arch.sie_block->ckc,
351346a6dd1cSJason J. herne 			     (u64 __user *)reg->addr);
351446a6dd1cSJason J. herne 		break;
3515536336c2SDominik Dingel 	case KVM_REG_S390_PFTOKEN:
3516536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_token,
3517536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
35189fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
35199fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
3520536336c2SDominik Dingel 		break;
3521536336c2SDominik Dingel 	case KVM_REG_S390_PFCOMPARE:
3522536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_compare,
3523536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3524536336c2SDominik Dingel 		break;
3525536336c2SDominik Dingel 	case KVM_REG_S390_PFSELECT:
3526536336c2SDominik Dingel 		r = get_user(vcpu->arch.pfault_select,
3527536336c2SDominik Dingel 			     (u64 __user *)reg->addr);
3528536336c2SDominik Dingel 		break;
3529672550fbSChristian Borntraeger 	case KVM_REG_S390_PP:
3530672550fbSChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->pp,
3531672550fbSChristian Borntraeger 			     (u64 __user *)reg->addr);
3532672550fbSChristian Borntraeger 		break;
3533afa45ff5SChristian Borntraeger 	case KVM_REG_S390_GBEA:
3534afa45ff5SChristian Borntraeger 		r = get_user(vcpu->arch.sie_block->gbea,
3535afa45ff5SChristian Borntraeger 			     (u64 __user *)reg->addr);
3536afa45ff5SChristian Borntraeger 		break;
353714eebd91SCarsten Otte 	default:
353814eebd91SCarsten Otte 		break;
353914eebd91SCarsten Otte 	}
354014eebd91SCarsten Otte 
354114eebd91SCarsten Otte 	return r;
354214eebd91SCarsten Otte }
3543b6d33834SChristoffer Dall 
35447de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
3545b0c632dbSHeiko Carstens {
35467de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
35477de3f142SJanosch Frank 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
35487de3f142SJanosch Frank 	memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
35497de3f142SJanosch Frank 
35507de3f142SJanosch Frank 	kvm_clear_async_pf_completion_queue(vcpu);
35517de3f142SJanosch Frank 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
35527de3f142SJanosch Frank 		kvm_s390_vcpu_stop(vcpu);
35537de3f142SJanosch Frank 	kvm_s390_clear_local_irqs(vcpu);
35547de3f142SJanosch Frank }
35557de3f142SJanosch Frank 
35567de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
35577de3f142SJanosch Frank {
35587de3f142SJanosch Frank 	/* Initial reset is a superset of the normal reset */
35597de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_normal_reset(vcpu);
35607de3f142SJanosch Frank 
3561e93fc7b4SChristian Borntraeger 	/*
3562e93fc7b4SChristian Borntraeger 	 * This equals initial cpu reset in pop, but we don't switch to ESA.
3563e93fc7b4SChristian Borntraeger 	 * We do not only reset the internal data, but also ...
3564e93fc7b4SChristian Borntraeger 	 */
35657de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.mask = 0;
35667de3f142SJanosch Frank 	vcpu->arch.sie_block->gpsw.addr = 0;
35677de3f142SJanosch Frank 	kvm_s390_set_prefix(vcpu, 0);
35687de3f142SJanosch Frank 	kvm_s390_set_cpu_timer(vcpu, 0);
35697de3f142SJanosch Frank 	vcpu->arch.sie_block->ckc = 0;
35707de3f142SJanosch Frank 	memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
35717de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
35727de3f142SJanosch Frank 	vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
3573e93fc7b4SChristian Borntraeger 
3574e93fc7b4SChristian Borntraeger 	/* ... the data in sync regs */
3575e93fc7b4SChristian Borntraeger 	memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
3576e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.ckc = 0;
3577e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
3578e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
3579e93fc7b4SChristian Borntraeger 	vcpu->run->psw_addr = 0;
3580e93fc7b4SChristian Borntraeger 	vcpu->run->psw_mask = 0;
3581e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.todpr = 0;
3582e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.cputm = 0;
3583e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.ckc = 0;
3584e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.pp = 0;
3585e93fc7b4SChristian Borntraeger 	vcpu->run->s.regs.gbea = 1;
35867de3f142SJanosch Frank 	vcpu->run->s.regs.fpc = 0;
35870f303504SJanosch Frank 	/*
35880f303504SJanosch Frank 	 * Do not reset these registers in the protected case, as some of
35890f303504SJanosch Frank 	 * them are overlayed and they are not accessible in this case
35900f303504SJanosch Frank 	 * anyway.
35910f303504SJanosch Frank 	 */
35920f303504SJanosch Frank 	if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
35937de3f142SJanosch Frank 		vcpu->arch.sie_block->gbea = 1;
35947de3f142SJanosch Frank 		vcpu->arch.sie_block->pp = 0;
35957de3f142SJanosch Frank 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
35960f303504SJanosch Frank 		vcpu->arch.sie_block->todpr = 0;
35970f303504SJanosch Frank 	}
35987de3f142SJanosch Frank }
35997de3f142SJanosch Frank 
36007de3f142SJanosch Frank static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
36017de3f142SJanosch Frank {
36027de3f142SJanosch Frank 	struct kvm_sync_regs *regs = &vcpu->run->s.regs;
36037de3f142SJanosch Frank 
36047de3f142SJanosch Frank 	/* Clear reset is a superset of the initial reset */
36057de3f142SJanosch Frank 	kvm_arch_vcpu_ioctl_initial_reset(vcpu);
36067de3f142SJanosch Frank 
36077de3f142SJanosch Frank 	memset(&regs->gprs, 0, sizeof(regs->gprs));
36087de3f142SJanosch Frank 	memset(&regs->vrs, 0, sizeof(regs->vrs));
36097de3f142SJanosch Frank 	memset(&regs->acrs, 0, sizeof(regs->acrs));
36107de3f142SJanosch Frank 	memset(&regs->gscb, 0, sizeof(regs->gscb));
36117de3f142SJanosch Frank 
36127de3f142SJanosch Frank 	regs->etoken = 0;
36137de3f142SJanosch Frank 	regs->etoken_extension = 0;
3614b0c632dbSHeiko Carstens }
3615b0c632dbSHeiko Carstens 
3616b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3617b0c632dbSHeiko Carstens {
3618875656feSChristoffer Dall 	vcpu_load(vcpu);
36195a32c1afSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
3620875656feSChristoffer Dall 	vcpu_put(vcpu);
3621b0c632dbSHeiko Carstens 	return 0;
3622b0c632dbSHeiko Carstens }
3623b0c632dbSHeiko Carstens 
3624b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3625b0c632dbSHeiko Carstens {
36261fc9b76bSChristoffer Dall 	vcpu_load(vcpu);
36275a32c1afSChristian Borntraeger 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
36281fc9b76bSChristoffer Dall 	vcpu_put(vcpu);
3629b0c632dbSHeiko Carstens 	return 0;
3630b0c632dbSHeiko Carstens }
3631b0c632dbSHeiko Carstens 
3632b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3633b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
3634b0c632dbSHeiko Carstens {
3635b4ef9d4eSChristoffer Dall 	vcpu_load(vcpu);
3636b4ef9d4eSChristoffer Dall 
363759674c1aSChristian Borntraeger 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
3638b0c632dbSHeiko Carstens 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
3639b4ef9d4eSChristoffer Dall 
3640b4ef9d4eSChristoffer Dall 	vcpu_put(vcpu);
3641b0c632dbSHeiko Carstens 	return 0;
3642b0c632dbSHeiko Carstens }
3643b0c632dbSHeiko Carstens 
3644b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3645b0c632dbSHeiko Carstens 				  struct kvm_sregs *sregs)
3646b0c632dbSHeiko Carstens {
3647bcdec41cSChristoffer Dall 	vcpu_load(vcpu);
3648bcdec41cSChristoffer Dall 
364959674c1aSChristian Borntraeger 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
3650b0c632dbSHeiko Carstens 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
3651bcdec41cSChristoffer Dall 
3652bcdec41cSChristoffer Dall 	vcpu_put(vcpu);
3653b0c632dbSHeiko Carstens 	return 0;
3654b0c632dbSHeiko Carstens }
3655b0c632dbSHeiko Carstens 
3656b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3657b0c632dbSHeiko Carstens {
36586a96bc7fSChristoffer Dall 	int ret = 0;
36596a96bc7fSChristoffer Dall 
36606a96bc7fSChristoffer Dall 	vcpu_load(vcpu);
36616a96bc7fSChristoffer Dall 
36626a96bc7fSChristoffer Dall 	if (test_fp_ctl(fpu->fpc)) {
36636a96bc7fSChristoffer Dall 		ret = -EINVAL;
36646a96bc7fSChristoffer Dall 		goto out;
36656a96bc7fSChristoffer Dall 	}
3666e1788bb9SChristian Borntraeger 	vcpu->run->s.regs.fpc = fpu->fpc;
36679abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
3668a7d4b8f2SDavid Hildenbrand 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
3669a7d4b8f2SDavid Hildenbrand 				 (freg_t *) fpu->fprs);
36709abc2a08SDavid Hildenbrand 	else
3671a7d4b8f2SDavid Hildenbrand 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
36726a96bc7fSChristoffer Dall 
36736a96bc7fSChristoffer Dall out:
36746a96bc7fSChristoffer Dall 	vcpu_put(vcpu);
36756a96bc7fSChristoffer Dall 	return ret;
3676b0c632dbSHeiko Carstens }
3677b0c632dbSHeiko Carstens 
3678b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3679b0c632dbSHeiko Carstens {
36801393123eSChristoffer Dall 	vcpu_load(vcpu);
36811393123eSChristoffer Dall 
36829abc2a08SDavid Hildenbrand 	/* make sure we have the latest values */
36839abc2a08SDavid Hildenbrand 	save_fpu_regs();
36849abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX)
3685a7d4b8f2SDavid Hildenbrand 		convert_vx_to_fp((freg_t *) fpu->fprs,
3686a7d4b8f2SDavid Hildenbrand 				 (__vector128 *) vcpu->run->s.regs.vrs);
36879abc2a08SDavid Hildenbrand 	else
3688a7d4b8f2SDavid Hildenbrand 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
3689e1788bb9SChristian Borntraeger 	fpu->fpc = vcpu->run->s.regs.fpc;
36901393123eSChristoffer Dall 
36911393123eSChristoffer Dall 	vcpu_put(vcpu);
3692b0c632dbSHeiko Carstens 	return 0;
3693b0c632dbSHeiko Carstens }
3694b0c632dbSHeiko Carstens 
3695b0c632dbSHeiko Carstens static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
3696b0c632dbSHeiko Carstens {
3697b0c632dbSHeiko Carstens 	int rc = 0;
3698b0c632dbSHeiko Carstens 
36997a42fdc2SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
3700b0c632dbSHeiko Carstens 		rc = -EBUSY;
3701d7b0b5ebSCarsten Otte 	else {
3702d7b0b5ebSCarsten Otte 		vcpu->run->psw_mask = psw.mask;
3703d7b0b5ebSCarsten Otte 		vcpu->run->psw_addr = psw.addr;
3704d7b0b5ebSCarsten Otte 	}
3705b0c632dbSHeiko Carstens 	return rc;
3706b0c632dbSHeiko Carstens }
3707b0c632dbSHeiko Carstens 
3708b0c632dbSHeiko Carstens int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3709b0c632dbSHeiko Carstens 				  struct kvm_translation *tr)
3710b0c632dbSHeiko Carstens {
3711b0c632dbSHeiko Carstens 	return -EINVAL; /* not implemented yet */
3712b0c632dbSHeiko Carstens }
3713b0c632dbSHeiko Carstens 
371427291e21SDavid Hildenbrand #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
371527291e21SDavid Hildenbrand 			      KVM_GUESTDBG_USE_HW_BP | \
371627291e21SDavid Hildenbrand 			      KVM_GUESTDBG_ENABLE)
371727291e21SDavid Hildenbrand 
3718d0bfb940SJan Kiszka int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
3719d0bfb940SJan Kiszka 					struct kvm_guest_debug *dbg)
3720b0c632dbSHeiko Carstens {
372127291e21SDavid Hildenbrand 	int rc = 0;
372227291e21SDavid Hildenbrand 
372366b56562SChristoffer Dall 	vcpu_load(vcpu);
372466b56562SChristoffer Dall 
372527291e21SDavid Hildenbrand 	vcpu->guest_debug = 0;
372627291e21SDavid Hildenbrand 	kvm_s390_clear_bp_data(vcpu);
372727291e21SDavid Hildenbrand 
372866b56562SChristoffer Dall 	if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
372966b56562SChristoffer Dall 		rc = -EINVAL;
373066b56562SChristoffer Dall 		goto out;
373166b56562SChristoffer Dall 	}
373266b56562SChristoffer Dall 	if (!sclp.has_gpere) {
373366b56562SChristoffer Dall 		rc = -EINVAL;
373466b56562SChristoffer Dall 		goto out;
373566b56562SChristoffer Dall 	}
373627291e21SDavid Hildenbrand 
373727291e21SDavid Hildenbrand 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
373827291e21SDavid Hildenbrand 		vcpu->guest_debug = dbg->control;
373927291e21SDavid Hildenbrand 		/* enforce guest PER */
3740ef8f4f49SDavid Hildenbrand 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
374127291e21SDavid Hildenbrand 
374227291e21SDavid Hildenbrand 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
374327291e21SDavid Hildenbrand 			rc = kvm_s390_import_bp_data(vcpu, dbg);
374427291e21SDavid Hildenbrand 	} else {
37459daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
374627291e21SDavid Hildenbrand 		vcpu->arch.guestdbg.last_bp = 0;
374727291e21SDavid Hildenbrand 	}
374827291e21SDavid Hildenbrand 
374927291e21SDavid Hildenbrand 	if (rc) {
375027291e21SDavid Hildenbrand 		vcpu->guest_debug = 0;
375127291e21SDavid Hildenbrand 		kvm_s390_clear_bp_data(vcpu);
37529daecfc6SDavid Hildenbrand 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
375327291e21SDavid Hildenbrand 	}
375427291e21SDavid Hildenbrand 
375566b56562SChristoffer Dall out:
375666b56562SChristoffer Dall 	vcpu_put(vcpu);
375727291e21SDavid Hildenbrand 	return rc;
3758b0c632dbSHeiko Carstens }
3759b0c632dbSHeiko Carstens 
376062d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
376162d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
376262d9f0dbSMarcelo Tosatti {
3763fd232561SChristoffer Dall 	int ret;
3764fd232561SChristoffer Dall 
3765fd232561SChristoffer Dall 	vcpu_load(vcpu);
3766fd232561SChristoffer Dall 
37676352e4d2SDavid Hildenbrand 	/* CHECK_STOP and LOAD are not supported yet */
3768fd232561SChristoffer Dall 	ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
37696352e4d2SDavid Hildenbrand 				      KVM_MP_STATE_OPERATING;
3770fd232561SChristoffer Dall 
3771fd232561SChristoffer Dall 	vcpu_put(vcpu);
3772fd232561SChristoffer Dall 	return ret;
377362d9f0dbSMarcelo Tosatti }
377462d9f0dbSMarcelo Tosatti 
377562d9f0dbSMarcelo Tosatti int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
377662d9f0dbSMarcelo Tosatti 				    struct kvm_mp_state *mp_state)
377762d9f0dbSMarcelo Tosatti {
37786352e4d2SDavid Hildenbrand 	int rc = 0;
37796352e4d2SDavid Hildenbrand 
3780e83dff5eSChristoffer Dall 	vcpu_load(vcpu);
3781e83dff5eSChristoffer Dall 
37826352e4d2SDavid Hildenbrand 	/* user space knows about this interface - let it control the state */
37836352e4d2SDavid Hildenbrand 	vcpu->kvm->arch.user_cpu_state_ctrl = 1;
37846352e4d2SDavid Hildenbrand 
37856352e4d2SDavid Hildenbrand 	switch (mp_state->mp_state) {
37866352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_STOPPED:
3787fe28c786SJanosch Frank 		rc = kvm_s390_vcpu_stop(vcpu);
37886352e4d2SDavid Hildenbrand 		break;
37896352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_OPERATING:
3790fe28c786SJanosch Frank 		rc = kvm_s390_vcpu_start(vcpu);
37916352e4d2SDavid Hildenbrand 		break;
37926352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_LOAD:
37937c36a3fcSJanosch Frank 		if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
37947c36a3fcSJanosch Frank 			rc = -ENXIO;
37957c36a3fcSJanosch Frank 			break;
37967c36a3fcSJanosch Frank 		}
37977c36a3fcSJanosch Frank 		rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
37987c36a3fcSJanosch Frank 		break;
37996352e4d2SDavid Hildenbrand 	case KVM_MP_STATE_CHECK_STOP:
38003b684a42SJoe Perches 		fallthrough;	/* CHECK_STOP and LOAD are not supported yet */
38016352e4d2SDavid Hildenbrand 	default:
38026352e4d2SDavid Hildenbrand 		rc = -ENXIO;
38036352e4d2SDavid Hildenbrand 	}
38046352e4d2SDavid Hildenbrand 
3805e83dff5eSChristoffer Dall 	vcpu_put(vcpu);
38066352e4d2SDavid Hildenbrand 	return rc;
380762d9f0dbSMarcelo Tosatti }
380862d9f0dbSMarcelo Tosatti 
38098ad35755SDavid Hildenbrand static bool ibs_enabled(struct kvm_vcpu *vcpu)
38108ad35755SDavid Hildenbrand {
38118d5fb0dcSDavid Hildenbrand 	return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
38128ad35755SDavid Hildenbrand }
38138ad35755SDavid Hildenbrand 
38142c70fe44SChristian Borntraeger static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
38152c70fe44SChristian Borntraeger {
38168ad35755SDavid Hildenbrand retry:
38178e236546SChristian Borntraeger 	kvm_s390_vcpu_request_handled(vcpu);
38182fa6e1e1SRadim Krčmář 	if (!kvm_request_pending(vcpu))
3819586b7ccdSChristian Borntraeger 		return 0;
38202c70fe44SChristian Borntraeger 	/*
38212c70fe44SChristian Borntraeger 	 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3822b2d73b2aSMartin Schwidefsky 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
38232c70fe44SChristian Borntraeger 	 * This ensures that the ipte instruction for this request has
38242c70fe44SChristian Borntraeger 	 * already finished. We might race against a second unmapper that
38252c70fe44SChristian Borntraeger 	 * wants to set the blocking bit. Lets just retry the request loop.
38262c70fe44SChristian Borntraeger 	 */
38278ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
38282c70fe44SChristian Borntraeger 		int rc;
3829b2d73b2aSMartin Schwidefsky 		rc = gmap_mprotect_notify(vcpu->arch.gmap,
3830fda902cbSMichael Mueller 					  kvm_s390_get_prefix(vcpu),
3831b2d73b2aSMartin Schwidefsky 					  PAGE_SIZE * 2, PROT_WRITE);
3832aca411a4SJulius Niedworok 		if (rc) {
3833aca411a4SJulius Niedworok 			kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
38342c70fe44SChristian Borntraeger 			return rc;
3835aca411a4SJulius Niedworok 		}
38368ad35755SDavid Hildenbrand 		goto retry;
38372c70fe44SChristian Borntraeger 	}
38388ad35755SDavid Hildenbrand 
3839d3d692c8SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3840d3d692c8SDavid Hildenbrand 		vcpu->arch.sie_block->ihcpu = 0xffff;
3841d3d692c8SDavid Hildenbrand 		goto retry;
3842d3d692c8SDavid Hildenbrand 	}
3843d3d692c8SDavid Hildenbrand 
38448ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
38458ad35755SDavid Hildenbrand 		if (!ibs_enabled(vcpu)) {
38468ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3847ef8f4f49SDavid Hildenbrand 			kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
38488ad35755SDavid Hildenbrand 		}
38498ad35755SDavid Hildenbrand 		goto retry;
38508ad35755SDavid Hildenbrand 	}
38518ad35755SDavid Hildenbrand 
38528ad35755SDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
38538ad35755SDavid Hildenbrand 		if (ibs_enabled(vcpu)) {
38548ad35755SDavid Hildenbrand 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
38559daecfc6SDavid Hildenbrand 			kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
38568ad35755SDavid Hildenbrand 		}
38578ad35755SDavid Hildenbrand 		goto retry;
38588ad35755SDavid Hildenbrand 	}
38598ad35755SDavid Hildenbrand 
38606502a34cSDavid Hildenbrand 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
38616502a34cSDavid Hildenbrand 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
38626502a34cSDavid Hildenbrand 		goto retry;
38636502a34cSDavid Hildenbrand 	}
38646502a34cSDavid Hildenbrand 
3865190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3866190df4a2SClaudio Imbrenda 		/*
3867c9f0a2b8SJanosch Frank 		 * Disable CMM virtualization; we will emulate the ESSA
3868190df4a2SClaudio Imbrenda 		 * instruction manually, in order to provide additional
3869190df4a2SClaudio Imbrenda 		 * functionalities needed for live migration.
3870190df4a2SClaudio Imbrenda 		 */
3871190df4a2SClaudio Imbrenda 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3872190df4a2SClaudio Imbrenda 		goto retry;
3873190df4a2SClaudio Imbrenda 	}
3874190df4a2SClaudio Imbrenda 
3875190df4a2SClaudio Imbrenda 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3876190df4a2SClaudio Imbrenda 		/*
3877c9f0a2b8SJanosch Frank 		 * Re-enable CMM virtualization if CMMA is available and
3878c9f0a2b8SJanosch Frank 		 * CMM has been used.
3879190df4a2SClaudio Imbrenda 		 */
3880190df4a2SClaudio Imbrenda 		if ((vcpu->kvm->arch.use_cmma) &&
3881c9f0a2b8SJanosch Frank 		    (vcpu->kvm->mm->context.uses_cmm))
3882190df4a2SClaudio Imbrenda 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3883190df4a2SClaudio Imbrenda 		goto retry;
3884190df4a2SClaudio Imbrenda 	}
3885190df4a2SClaudio Imbrenda 
38860759d068SDavid Hildenbrand 	/* nothing to do, just clear the request */
388772875d8aSRadim Krčmář 	kvm_clear_request(KVM_REQ_UNHALT, vcpu);
38883194cdb7SDavid Hildenbrand 	/* we left the vsie handler, nothing to do, just clear the request */
38893194cdb7SDavid Hildenbrand 	kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
38900759d068SDavid Hildenbrand 
38912c70fe44SChristian Borntraeger 	return 0;
38922c70fe44SChristian Borntraeger }
38932c70fe44SChristian Borntraeger 
38940e7def5fSDavid Hildenbrand void kvm_s390_set_tod_clock(struct kvm *kvm,
38958fa1696eSCollin L. Walling 			    const struct kvm_s390_vm_tod_clock *gtod)
38968fa1696eSCollin L. Walling {
38978fa1696eSCollin L. Walling 	struct kvm_vcpu *vcpu;
38982cfd7b73SHeiko Carstens 	union tod_clock clk;
38998fa1696eSCollin L. Walling 	int i;
39008fa1696eSCollin L. Walling 
39018fa1696eSCollin L. Walling 	mutex_lock(&kvm->lock);
39028fa1696eSCollin L. Walling 	preempt_disable();
39038fa1696eSCollin L. Walling 
39042cfd7b73SHeiko Carstens 	store_tod_clock_ext(&clk);
39058fa1696eSCollin L. Walling 
39062cfd7b73SHeiko Carstens 	kvm->arch.epoch = gtod->tod - clk.tod;
39070e7def5fSDavid Hildenbrand 	kvm->arch.epdx = 0;
39080e7def5fSDavid Hildenbrand 	if (test_kvm_facility(kvm, 139)) {
39092cfd7b73SHeiko Carstens 		kvm->arch.epdx = gtod->epoch_idx - clk.ei;
39108fa1696eSCollin L. Walling 		if (kvm->arch.epoch > gtod->tod)
39118fa1696eSCollin L. Walling 			kvm->arch.epdx -= 1;
39120e7def5fSDavid Hildenbrand 	}
39138fa1696eSCollin L. Walling 
39148fa1696eSCollin L. Walling 	kvm_s390_vcpu_block_all(kvm);
39158fa1696eSCollin L. Walling 	kvm_for_each_vcpu(i, vcpu, kvm) {
39168fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
39178fa1696eSCollin L. Walling 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
39188fa1696eSCollin L. Walling 	}
39198fa1696eSCollin L. Walling 
39208fa1696eSCollin L. Walling 	kvm_s390_vcpu_unblock_all(kvm);
39218fa1696eSCollin L. Walling 	preempt_enable();
39228fa1696eSCollin L. Walling 	mutex_unlock(&kvm->lock);
39238fa1696eSCollin L. Walling }
39248fa1696eSCollin L. Walling 
3925fa576c58SThomas Huth /**
3926fa576c58SThomas Huth  * kvm_arch_fault_in_page - fault-in guest page if necessary
3927fa576c58SThomas Huth  * @vcpu: The corresponding virtual cpu
3928fa576c58SThomas Huth  * @gpa: Guest physical address
3929fa576c58SThomas Huth  * @writable: Whether the page should be writable or not
3930fa576c58SThomas Huth  *
3931fa576c58SThomas Huth  * Make sure that a guest page has been faulted-in on the host.
3932fa576c58SThomas Huth  *
3933fa576c58SThomas Huth  * Return: Zero on success, negative error code otherwise.
3934fa576c58SThomas Huth  */
3935fa576c58SThomas Huth long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
393624eb3a82SDominik Dingel {
3937527e30b4SMartin Schwidefsky 	return gmap_fault(vcpu->arch.gmap, gpa,
3938527e30b4SMartin Schwidefsky 			  writable ? FAULT_FLAG_WRITE : 0);
393924eb3a82SDominik Dingel }
394024eb3a82SDominik Dingel 
39413c038e6bSDominik Dingel static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
39423c038e6bSDominik Dingel 				      unsigned long token)
39433c038e6bSDominik Dingel {
39443c038e6bSDominik Dingel 	struct kvm_s390_interrupt inti;
3945383d0b05SJens Freimann 	struct kvm_s390_irq irq;
39463c038e6bSDominik Dingel 
39473c038e6bSDominik Dingel 	if (start_token) {
3948383d0b05SJens Freimann 		irq.u.ext.ext_params2 = token;
3949383d0b05SJens Freimann 		irq.type = KVM_S390_INT_PFAULT_INIT;
3950383d0b05SJens Freimann 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
39513c038e6bSDominik Dingel 	} else {
39523c038e6bSDominik Dingel 		inti.type = KVM_S390_INT_PFAULT_DONE;
3953383d0b05SJens Freimann 		inti.parm64 = token;
39543c038e6bSDominik Dingel 		WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
39553c038e6bSDominik Dingel 	}
39563c038e6bSDominik Dingel }
39573c038e6bSDominik Dingel 
39582a18b7e7SVitaly Kuznetsov bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
39593c038e6bSDominik Dingel 				     struct kvm_async_pf *work)
39603c038e6bSDominik Dingel {
39613c038e6bSDominik Dingel 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
39623c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
39632a18b7e7SVitaly Kuznetsov 
39642a18b7e7SVitaly Kuznetsov 	return true;
39653c038e6bSDominik Dingel }
39663c038e6bSDominik Dingel 
39673c038e6bSDominik Dingel void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
39683c038e6bSDominik Dingel 				 struct kvm_async_pf *work)
39693c038e6bSDominik Dingel {
39703c038e6bSDominik Dingel 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
39713c038e6bSDominik Dingel 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
39723c038e6bSDominik Dingel }
39733c038e6bSDominik Dingel 
39743c038e6bSDominik Dingel void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
39753c038e6bSDominik Dingel 			       struct kvm_async_pf *work)
39763c038e6bSDominik Dingel {
39773c038e6bSDominik Dingel 	/* s390 will always inject the page directly */
39783c038e6bSDominik Dingel }
39793c038e6bSDominik Dingel 
39807c0ade6cSVitaly Kuznetsov bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
39813c038e6bSDominik Dingel {
39823c038e6bSDominik Dingel 	/*
39833c038e6bSDominik Dingel 	 * s390 will always inject the page directly,
39843c038e6bSDominik Dingel 	 * but we still want check_async_completion to cleanup
39853c038e6bSDominik Dingel 	 */
39863c038e6bSDominik Dingel 	return true;
39873c038e6bSDominik Dingel }
39883c038e6bSDominik Dingel 
3989e8c22266SVitaly Kuznetsov static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
39903c038e6bSDominik Dingel {
39913c038e6bSDominik Dingel 	hva_t hva;
39923c038e6bSDominik Dingel 	struct kvm_arch_async_pf arch;
39933c038e6bSDominik Dingel 
39943c038e6bSDominik Dingel 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3995e8c22266SVitaly Kuznetsov 		return false;
39963c038e6bSDominik Dingel 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
39973c038e6bSDominik Dingel 	    vcpu->arch.pfault_compare)
3998e8c22266SVitaly Kuznetsov 		return false;
39993c038e6bSDominik Dingel 	if (psw_extint_disabled(vcpu))
4000e8c22266SVitaly Kuznetsov 		return false;
40019a022067SDavid Hildenbrand 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
4002e8c22266SVitaly Kuznetsov 		return false;
4003b9224cd7SDavid Hildenbrand 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
4004e8c22266SVitaly Kuznetsov 		return false;
40053c038e6bSDominik Dingel 	if (!vcpu->arch.gmap->pfault_enabled)
4006e8c22266SVitaly Kuznetsov 		return false;
40073c038e6bSDominik Dingel 
400881480cc1SHeiko Carstens 	hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
400981480cc1SHeiko Carstens 	hva += current->thread.gmap_addr & ~PAGE_MASK;
401081480cc1SHeiko Carstens 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
4011e8c22266SVitaly Kuznetsov 		return false;
40123c038e6bSDominik Dingel 
4013e8c22266SVitaly Kuznetsov 	return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
40143c038e6bSDominik Dingel }
40153c038e6bSDominik Dingel 
40163fb4c40fSThomas Huth static int vcpu_pre_run(struct kvm_vcpu *vcpu)
4017b0c632dbSHeiko Carstens {
40183fb4c40fSThomas Huth 	int rc, cpuflags;
4019e168bf8dSCarsten Otte 
40203c038e6bSDominik Dingel 	/*
40213c038e6bSDominik Dingel 	 * On s390 notifications for arriving pages will be delivered directly
40223c038e6bSDominik Dingel 	 * to the guest but the house keeping for completed pfaults is
40233c038e6bSDominik Dingel 	 * handled outside the worker.
40243c038e6bSDominik Dingel 	 */
40253c038e6bSDominik Dingel 	kvm_check_async_pf_completion(vcpu);
40263c038e6bSDominik Dingel 
40277ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
40287ec7c8c7SChristian Borntraeger 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
4029b0c632dbSHeiko Carstens 
4030b0c632dbSHeiko Carstens 	if (need_resched())
4031b0c632dbSHeiko Carstens 		schedule();
4032b0c632dbSHeiko Carstens 
403379395031SJens Freimann 	if (!kvm_is_ucontrol(vcpu->kvm)) {
403479395031SJens Freimann 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
403579395031SJens Freimann 		if (rc)
403679395031SJens Freimann 			return rc;
403779395031SJens Freimann 	}
40380ff31867SCarsten Otte 
40392c70fe44SChristian Borntraeger 	rc = kvm_s390_handle_requests(vcpu);
40402c70fe44SChristian Borntraeger 	if (rc)
40412c70fe44SChristian Borntraeger 		return rc;
40422c70fe44SChristian Borntraeger 
404327291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu)) {
404427291e21SDavid Hildenbrand 		kvm_s390_backup_guest_per_regs(vcpu);
404527291e21SDavid Hildenbrand 		kvm_s390_patch_guest_per_regs(vcpu);
404627291e21SDavid Hildenbrand 	}
404727291e21SDavid Hildenbrand 
40489f30f621SMichael Mueller 	clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask);
40499f30f621SMichael Mueller 
4050b0c632dbSHeiko Carstens 	vcpu->arch.sie_block->icptcode = 0;
40513fb4c40fSThomas Huth 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
40523fb4c40fSThomas Huth 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
40533fb4c40fSThomas Huth 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
40542b29a9fdSDominik Dingel 
40553fb4c40fSThomas Huth 	return 0;
40563fb4c40fSThomas Huth }
40573fb4c40fSThomas Huth 
4058492d8642SThomas Huth static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
4059492d8642SThomas Huth {
406056317920SDavid Hildenbrand 	struct kvm_s390_pgm_info pgm_info = {
406156317920SDavid Hildenbrand 		.code = PGM_ADDRESSING,
406256317920SDavid Hildenbrand 	};
406356317920SDavid Hildenbrand 	u8 opcode, ilen;
4064492d8642SThomas Huth 	int rc;
4065492d8642SThomas Huth 
4066492d8642SThomas Huth 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4067492d8642SThomas Huth 	trace_kvm_s390_sie_fault(vcpu);
4068492d8642SThomas Huth 
4069492d8642SThomas Huth 	/*
4070492d8642SThomas Huth 	 * We want to inject an addressing exception, which is defined as a
4071492d8642SThomas Huth 	 * suppressing or terminating exception. However, since we came here
4072492d8642SThomas Huth 	 * by a DAT access exception, the PSW still points to the faulting
4073492d8642SThomas Huth 	 * instruction since DAT exceptions are nullifying. So we've got
4074492d8642SThomas Huth 	 * to look up the current opcode to get the length of the instruction
4075492d8642SThomas Huth 	 * to be able to forward the PSW.
4076492d8642SThomas Huth 	 */
40773fa8cad7SDavid Hildenbrand 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
407856317920SDavid Hildenbrand 	ilen = insn_length(opcode);
40799b0d721aSDavid Hildenbrand 	if (rc < 0) {
40809b0d721aSDavid Hildenbrand 		return rc;
40819b0d721aSDavid Hildenbrand 	} else if (rc) {
40829b0d721aSDavid Hildenbrand 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
40839b0d721aSDavid Hildenbrand 		 * Forward by arbitrary ilc, injection will take care of
40849b0d721aSDavid Hildenbrand 		 * nullification if necessary.
40859b0d721aSDavid Hildenbrand 		 */
40869b0d721aSDavid Hildenbrand 		pgm_info = vcpu->arch.pgm;
40879b0d721aSDavid Hildenbrand 		ilen = 4;
40889b0d721aSDavid Hildenbrand 	}
408956317920SDavid Hildenbrand 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
409056317920SDavid Hildenbrand 	kvm_s390_forward_psw(vcpu, ilen);
409156317920SDavid Hildenbrand 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
4092492d8642SThomas Huth }
4093492d8642SThomas Huth 
40943fb4c40fSThomas Huth static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
40953fb4c40fSThomas Huth {
40964d62fcc0SQingFeng Hao 	struct mcck_volatile_info *mcck_info;
40974d62fcc0SQingFeng Hao 	struct sie_page *sie_page;
40984d62fcc0SQingFeng Hao 
40992b29a9fdSDominik Dingel 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
41002b29a9fdSDominik Dingel 		   vcpu->arch.sie_block->icptcode);
41012b29a9fdSDominik Dingel 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
41022b29a9fdSDominik Dingel 
410327291e21SDavid Hildenbrand 	if (guestdbg_enabled(vcpu))
410427291e21SDavid Hildenbrand 		kvm_s390_restore_guest_per_regs(vcpu);
410527291e21SDavid Hildenbrand 
41067ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
41077ec7c8c7SChristian Borntraeger 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
410871f116bfSDavid Hildenbrand 
41094d62fcc0SQingFeng Hao 	if (exit_reason == -EINTR) {
41104d62fcc0SQingFeng Hao 		VCPU_EVENT(vcpu, 3, "%s", "machine check");
41114d62fcc0SQingFeng Hao 		sie_page = container_of(vcpu->arch.sie_block,
41124d62fcc0SQingFeng Hao 					struct sie_page, sie_block);
41134d62fcc0SQingFeng Hao 		mcck_info = &sie_page->mcck_info;
41144d62fcc0SQingFeng Hao 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
41154d62fcc0SQingFeng Hao 		return 0;
41164d62fcc0SQingFeng Hao 	}
41174d62fcc0SQingFeng Hao 
411871f116bfSDavid Hildenbrand 	if (vcpu->arch.sie_block->icptcode > 0) {
411971f116bfSDavid Hildenbrand 		int rc = kvm_handle_sie_intercept(vcpu);
412071f116bfSDavid Hildenbrand 
412171f116bfSDavid Hildenbrand 		if (rc != -EOPNOTSUPP)
412271f116bfSDavid Hildenbrand 			return rc;
412371f116bfSDavid Hildenbrand 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
412471f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
412571f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
412671f116bfSDavid Hildenbrand 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
412771f116bfSDavid Hildenbrand 		return -EREMOTE;
412871f116bfSDavid Hildenbrand 	} else if (exit_reason != -EFAULT) {
412971f116bfSDavid Hildenbrand 		vcpu->stat.exit_null++;
413071f116bfSDavid Hildenbrand 		return 0;
4131210b1607SThomas Huth 	} else if (kvm_is_ucontrol(vcpu->kvm)) {
4132210b1607SThomas Huth 		vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4133210b1607SThomas Huth 		vcpu->run->s390_ucontrol.trans_exc_code =
4134210b1607SThomas Huth 						current->thread.gmap_addr;
4135210b1607SThomas Huth 		vcpu->run->s390_ucontrol.pgm_code = 0x10;
413671f116bfSDavid Hildenbrand 		return -EREMOTE;
413724eb3a82SDominik Dingel 	} else if (current->thread.gmap_pfault) {
41383c038e6bSDominik Dingel 		trace_kvm_s390_major_guest_pfault(vcpu);
413924eb3a82SDominik Dingel 		current->thread.gmap_pfault = 0;
414071f116bfSDavid Hildenbrand 		if (kvm_arch_setup_async_pf(vcpu))
414171f116bfSDavid Hildenbrand 			return 0;
414250a05be4SChristian Borntraeger 		vcpu->stat.pfault_sync++;
414371f116bfSDavid Hildenbrand 		return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
4144fa576c58SThomas Huth 	}
414571f116bfSDavid Hildenbrand 	return vcpu_post_run_fault_in_sie(vcpu);
41463fb4c40fSThomas Huth }
41473fb4c40fSThomas Huth 
41483adae0b4SJanosch Frank #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
41493fb4c40fSThomas Huth static int __vcpu_run(struct kvm_vcpu *vcpu)
41503fb4c40fSThomas Huth {
41513fb4c40fSThomas Huth 	int rc, exit_reason;
4152c8aac234SJanosch Frank 	struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
41533fb4c40fSThomas Huth 
4154800c1065SThomas Huth 	/*
4155800c1065SThomas Huth 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4156800c1065SThomas Huth 	 * ning the guest), so that memslots (and other stuff) are protected
4157800c1065SThomas Huth 	 */
4158800c1065SThomas Huth 	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4159800c1065SThomas Huth 
4160a76ccff6SThomas Huth 	do {
41613fb4c40fSThomas Huth 		rc = vcpu_pre_run(vcpu);
41623fb4c40fSThomas Huth 		if (rc)
4163a76ccff6SThomas Huth 			break;
41643fb4c40fSThomas Huth 
4165800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
41663fb4c40fSThomas Huth 		/*
4167a76ccff6SThomas Huth 		 * As PF_VCPU will be used in fault handler, between
4168a76ccff6SThomas Huth 		 * guest_enter and guest_exit should be no uaccess.
41693fb4c40fSThomas Huth 		 */
41700097d12eSChristian Borntraeger 		local_irq_disable();
41716edaa530SPaolo Bonzini 		guest_enter_irqoff();
4172db0758b2SDavid Hildenbrand 		__disable_cpu_timer_accounting(vcpu);
41730097d12eSChristian Borntraeger 		local_irq_enable();
4174c8aac234SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4175c8aac234SJanosch Frank 			memcpy(sie_page->pv_grregs,
4176c8aac234SJanosch Frank 			       vcpu->run->s.regs.gprs,
4177c8aac234SJanosch Frank 			       sizeof(sie_page->pv_grregs));
4178c8aac234SJanosch Frank 		}
417956e62a73SSven Schnelle 		if (test_cpu_flag(CIF_FPU))
418056e62a73SSven Schnelle 			load_fpu_regs();
4181a76ccff6SThomas Huth 		exit_reason = sie64a(vcpu->arch.sie_block,
4182a76ccff6SThomas Huth 				     vcpu->run->s.regs.gprs);
4183c8aac234SJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4184c8aac234SJanosch Frank 			memcpy(vcpu->run->s.regs.gprs,
4185c8aac234SJanosch Frank 			       sie_page->pv_grregs,
4186c8aac234SJanosch Frank 			       sizeof(sie_page->pv_grregs));
41873adae0b4SJanosch Frank 			/*
41883adae0b4SJanosch Frank 			 * We're not allowed to inject interrupts on intercepts
41893adae0b4SJanosch Frank 			 * that leave the guest state in an "in-between" state
41903adae0b4SJanosch Frank 			 * where the next SIE entry will do a continuation.
41913adae0b4SJanosch Frank 			 * Fence interrupts in our "internal" PSW.
41923adae0b4SJanosch Frank 			 */
41933adae0b4SJanosch Frank 			if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
41943adae0b4SJanosch Frank 			    vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
41953adae0b4SJanosch Frank 				vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
41963adae0b4SJanosch Frank 			}
4197c8aac234SJanosch Frank 		}
41980097d12eSChristian Borntraeger 		local_irq_disable();
4199db0758b2SDavid Hildenbrand 		__enable_cpu_timer_accounting(vcpu);
42006edaa530SPaolo Bonzini 		guest_exit_irqoff();
42010097d12eSChristian Borntraeger 		local_irq_enable();
4202800c1065SThomas Huth 		vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
42033fb4c40fSThomas Huth 
42043fb4c40fSThomas Huth 		rc = vcpu_post_run(vcpu, exit_reason);
420527291e21SDavid Hildenbrand 	} while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
42063fb4c40fSThomas Huth 
4207800c1065SThomas Huth 	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4208e168bf8dSCarsten Otte 	return rc;
4209b0c632dbSHeiko Carstens }
4210b0c632dbSHeiko Carstens 
42112f0a83beSTianjia Zhang static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
4212b028ee3eSDavid Hildenbrand {
42132f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
42144d5f2c04SChristian Borntraeger 	struct runtime_instr_cb *riccb;
42154e0b1ab7SFan Zhang 	struct gs_cb *gscb;
42164d5f2c04SChristian Borntraeger 
42174d5f2c04SChristian Borntraeger 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
42184e0b1ab7SFan Zhang 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4219b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4220b028ee3eSDavid Hildenbrand 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4221b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4222b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4223b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4224b028ee3eSDavid Hildenbrand 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4225b028ee3eSDavid Hildenbrand 	}
4226b028ee3eSDavid Hildenbrand 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4227b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4228b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4229b028ee3eSDavid Hildenbrand 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
42309fbd8082SDavid Hildenbrand 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
42319fbd8082SDavid Hildenbrand 			kvm_clear_async_pf_completion_queue(vcpu);
4232b028ee3eSDavid Hildenbrand 	}
423323a60f83SCollin Walling 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
423423a60f83SCollin Walling 		vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
423523a60f83SCollin Walling 		vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
423623a60f83SCollin Walling 	}
423780cd8763SFan Zhang 	/*
423880cd8763SFan Zhang 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
423980cd8763SFan Zhang 	 * we should enable RI here instead of doing the lazy enablement.
424080cd8763SFan Zhang 	 */
424180cd8763SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
42424d5f2c04SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 64) &&
4243bb59c2daSAlice Frosi 	    riccb->v &&
42440c9d8683SDavid Hildenbrand 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
42454d5f2c04SChristian Borntraeger 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
42460c9d8683SDavid Hildenbrand 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
424780cd8763SFan Zhang 	}
42484e0b1ab7SFan Zhang 	/*
42494e0b1ab7SFan Zhang 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
42504e0b1ab7SFan Zhang 	 * we should enable GS here instead of doing the lazy enablement.
42514e0b1ab7SFan Zhang 	 */
42524e0b1ab7SFan Zhang 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
42534e0b1ab7SFan Zhang 	    test_kvm_facility(vcpu->kvm, 133) &&
42544e0b1ab7SFan Zhang 	    gscb->gssm &&
42554e0b1ab7SFan Zhang 	    !vcpu->arch.gs_enabled) {
42564e0b1ab7SFan Zhang 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
42574e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecb |= ECB_GS;
42584e0b1ab7SFan Zhang 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
42594e0b1ab7SFan Zhang 		vcpu->arch.gs_enabled = 1;
426080cd8763SFan Zhang 	}
426135b3fde6SChristian Borntraeger 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
426235b3fde6SChristian Borntraeger 	    test_kvm_facility(vcpu->kvm, 82)) {
426335b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
426435b3fde6SChristian Borntraeger 		vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
426535b3fde6SChristian Borntraeger 	}
42664e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
42674e0b1ab7SFan Zhang 		preempt_disable();
42684e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
42694e0b1ab7SFan Zhang 		if (current->thread.gs_cb) {
42704e0b1ab7SFan Zhang 			vcpu->arch.host_gscb = current->thread.gs_cb;
42714e0b1ab7SFan Zhang 			save_gs_cb(vcpu->arch.host_gscb);
42724e0b1ab7SFan Zhang 		}
42734e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled) {
42744e0b1ab7SFan Zhang 			current->thread.gs_cb = (struct gs_cb *)
42754e0b1ab7SFan Zhang 						&vcpu->run->s.regs.gscb;
42764e0b1ab7SFan Zhang 			restore_gs_cb(current->thread.gs_cb);
42774e0b1ab7SFan Zhang 		}
42784e0b1ab7SFan Zhang 		preempt_enable();
42794e0b1ab7SFan Zhang 	}
4280a3da7b4aSChristian Borntraeger 	/* SIE will load etoken directly from SDNX and therefore kvm_run */
4281811ea797SJanosch Frank }
4282811ea797SJanosch Frank 
42832f0a83beSTianjia Zhang static void sync_regs(struct kvm_vcpu *vcpu)
4284811ea797SJanosch Frank {
42852f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
42862f0a83beSTianjia Zhang 
4287811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4288811ea797SJanosch Frank 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4289811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4290811ea797SJanosch Frank 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4291811ea797SJanosch Frank 		/* some control register changes require a tlb flush */
4292811ea797SJanosch Frank 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4293811ea797SJanosch Frank 	}
4294811ea797SJanosch Frank 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4295811ea797SJanosch Frank 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4296811ea797SJanosch Frank 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4297811ea797SJanosch Frank 	}
4298811ea797SJanosch Frank 	save_access_regs(vcpu->arch.host_acrs);
4299811ea797SJanosch Frank 	restore_access_regs(vcpu->run->s.regs.acrs);
4300811ea797SJanosch Frank 	/* save host (userspace) fprs/vrs */
4301811ea797SJanosch Frank 	save_fpu_regs();
4302811ea797SJanosch Frank 	vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4303811ea797SJanosch Frank 	vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4304811ea797SJanosch Frank 	if (MACHINE_HAS_VX)
4305811ea797SJanosch Frank 		current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4306811ea797SJanosch Frank 	else
4307811ea797SJanosch Frank 		current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4308811ea797SJanosch Frank 	current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4309811ea797SJanosch Frank 	if (test_fp_ctl(current->thread.fpu.fpc))
4310811ea797SJanosch Frank 		/* User space provided an invalid FPC, let's clear it */
4311811ea797SJanosch Frank 		current->thread.fpu.fpc = 0;
4312811ea797SJanosch Frank 
4313811ea797SJanosch Frank 	/* Sync fmt2 only data */
4314811ea797SJanosch Frank 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
43152f0a83beSTianjia Zhang 		sync_regs_fmt2(vcpu);
4316811ea797SJanosch Frank 	} else {
4317811ea797SJanosch Frank 		/*
4318811ea797SJanosch Frank 		 * In several places we have to modify our internal view to
4319811ea797SJanosch Frank 		 * not do things that are disallowed by the ultravisor. For
4320811ea797SJanosch Frank 		 * example we must not inject interrupts after specific exits
4321811ea797SJanosch Frank 		 * (e.g. 112 prefix page not secure). We do this by turning
4322811ea797SJanosch Frank 		 * off the machine check, external and I/O interrupt bits
4323811ea797SJanosch Frank 		 * of our PSW copy. To avoid getting validity intercepts, we
4324811ea797SJanosch Frank 		 * do only accept the condition code from userspace.
4325811ea797SJanosch Frank 		 */
4326811ea797SJanosch Frank 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4327811ea797SJanosch Frank 		vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4328811ea797SJanosch Frank 						   PSW_MASK_CC;
4329811ea797SJanosch Frank 	}
433080cd8763SFan Zhang 
4331b028ee3eSDavid Hildenbrand 	kvm_run->kvm_dirty_regs = 0;
4332b028ee3eSDavid Hildenbrand }
4333b028ee3eSDavid Hildenbrand 
43342f0a83beSTianjia Zhang static void store_regs_fmt2(struct kvm_vcpu *vcpu)
4335b028ee3eSDavid Hildenbrand {
43362f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
43372f0a83beSTianjia Zhang 
4338b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4339b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4340b028ee3eSDavid Hildenbrand 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
434135b3fde6SChristian Borntraeger 	kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
434223a60f83SCollin Walling 	kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
43434e0b1ab7SFan Zhang 	if (MACHINE_HAS_GS) {
434444bada28SHeiko Carstens 		preempt_disable();
43454e0b1ab7SFan Zhang 		__ctl_set_bit(2, 4);
43464e0b1ab7SFan Zhang 		if (vcpu->arch.gs_enabled)
43474e0b1ab7SFan Zhang 			save_gs_cb(current->thread.gs_cb);
43484e0b1ab7SFan Zhang 		current->thread.gs_cb = vcpu->arch.host_gscb;
43494e0b1ab7SFan Zhang 		restore_gs_cb(vcpu->arch.host_gscb);
43504e0b1ab7SFan Zhang 		if (!vcpu->arch.host_gscb)
43514e0b1ab7SFan Zhang 			__ctl_clear_bit(2, 4);
43524e0b1ab7SFan Zhang 		vcpu->arch.host_gscb = NULL;
435344bada28SHeiko Carstens 		preempt_enable();
43544e0b1ab7SFan Zhang 	}
4355a3da7b4aSChristian Borntraeger 	/* SIE will save etoken directly into SDNX and therefore kvm_run */
4356b028ee3eSDavid Hildenbrand }
4357b028ee3eSDavid Hildenbrand 
43582f0a83beSTianjia Zhang static void store_regs(struct kvm_vcpu *vcpu)
4359811ea797SJanosch Frank {
43602f0a83beSTianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
43612f0a83beSTianjia Zhang 
4362811ea797SJanosch Frank 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
4363811ea797SJanosch Frank 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
4364811ea797SJanosch Frank 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
4365811ea797SJanosch Frank 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
4366811ea797SJanosch Frank 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
4367811ea797SJanosch Frank 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
4368811ea797SJanosch Frank 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
4369811ea797SJanosch Frank 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
4370811ea797SJanosch Frank 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
4371811ea797SJanosch Frank 	save_access_regs(vcpu->run->s.regs.acrs);
4372811ea797SJanosch Frank 	restore_access_regs(vcpu->arch.host_acrs);
4373811ea797SJanosch Frank 	/* Save guest register state */
4374811ea797SJanosch Frank 	save_fpu_regs();
4375811ea797SJanosch Frank 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4376811ea797SJanosch Frank 	/* Restore will be done lazily at return */
4377811ea797SJanosch Frank 	current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
4378811ea797SJanosch Frank 	current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
4379811ea797SJanosch Frank 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
43802f0a83beSTianjia Zhang 		store_regs_fmt2(vcpu);
4381811ea797SJanosch Frank }
4382811ea797SJanosch Frank 
43831b94f6f8STianjia Zhang int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
4384b0c632dbSHeiko Carstens {
43851b94f6f8STianjia Zhang 	struct kvm_run *kvm_run = vcpu->run;
43868f2abe6aSChristian Borntraeger 	int rc;
4387b0c632dbSHeiko Carstens 
4388460df4c1SPaolo Bonzini 	if (kvm_run->immediate_exit)
4389460df4c1SPaolo Bonzini 		return -EINTR;
4390460df4c1SPaolo Bonzini 
4391200824f5SThomas Huth 	if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
4392200824f5SThomas Huth 	    kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
4393200824f5SThomas Huth 		return -EINVAL;
4394200824f5SThomas Huth 
4395accb757dSChristoffer Dall 	vcpu_load(vcpu);
4396accb757dSChristoffer Dall 
439727291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu)) {
439827291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
4399accb757dSChristoffer Dall 		rc = 0;
4400accb757dSChristoffer Dall 		goto out;
440127291e21SDavid Hildenbrand 	}
440227291e21SDavid Hildenbrand 
440320b7035cSJan H. Schönherr 	kvm_sigset_activate(vcpu);
4404b0c632dbSHeiko Carstens 
4405fe28c786SJanosch Frank 	/*
4406fe28c786SJanosch Frank 	 * no need to check the return value of vcpu_start as it can only have
4407fe28c786SJanosch Frank 	 * an error for protvirt, but protvirt means user cpu state
4408fe28c786SJanosch Frank 	 */
44096352e4d2SDavid Hildenbrand 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
44106852d7b6SDavid Hildenbrand 		kvm_s390_vcpu_start(vcpu);
44116352e4d2SDavid Hildenbrand 	} else if (is_vcpu_stopped(vcpu)) {
4412ea2cdd27SDavid Hildenbrand 		pr_err_ratelimited("can't run stopped vcpu %d\n",
44136352e4d2SDavid Hildenbrand 				   vcpu->vcpu_id);
4414accb757dSChristoffer Dall 		rc = -EINVAL;
4415accb757dSChristoffer Dall 		goto out;
44166352e4d2SDavid Hildenbrand 	}
4417b0c632dbSHeiko Carstens 
44182f0a83beSTianjia Zhang 	sync_regs(vcpu);
4419db0758b2SDavid Hildenbrand 	enable_cpu_timer_accounting(vcpu);
4420d7b0b5ebSCarsten Otte 
4421dab4079dSHeiko Carstens 	might_fault();
4422e168bf8dSCarsten Otte 	rc = __vcpu_run(vcpu);
44239ace903dSChristian Ehrhardt 
4424b1d16c49SChristian Ehrhardt 	if (signal_pending(current) && !rc) {
4425b1d16c49SChristian Ehrhardt 		kvm_run->exit_reason = KVM_EXIT_INTR;
44268f2abe6aSChristian Borntraeger 		rc = -EINTR;
4427b1d16c49SChristian Ehrhardt 	}
44288f2abe6aSChristian Borntraeger 
442927291e21SDavid Hildenbrand 	if (guestdbg_exit_pending(vcpu) && !rc)  {
443027291e21SDavid Hildenbrand 		kvm_s390_prepare_debug_exit(vcpu);
443127291e21SDavid Hildenbrand 		rc = 0;
443227291e21SDavid Hildenbrand 	}
443327291e21SDavid Hildenbrand 
44348f2abe6aSChristian Borntraeger 	if (rc == -EREMOTE) {
443571f116bfSDavid Hildenbrand 		/* userspace support is needed, kvm_run has been prepared */
44368f2abe6aSChristian Borntraeger 		rc = 0;
44378f2abe6aSChristian Borntraeger 	}
44388f2abe6aSChristian Borntraeger 
4439db0758b2SDavid Hildenbrand 	disable_cpu_timer_accounting(vcpu);
44402f0a83beSTianjia Zhang 	store_regs(vcpu);
4441d7b0b5ebSCarsten Otte 
444220b7035cSJan H. Schönherr 	kvm_sigset_deactivate(vcpu);
4443b0c632dbSHeiko Carstens 
4444b0c632dbSHeiko Carstens 	vcpu->stat.exit_userspace++;
4445accb757dSChristoffer Dall out:
4446accb757dSChristoffer Dall 	vcpu_put(vcpu);
44477e8e6ab4SHeiko Carstens 	return rc;
4448b0c632dbSHeiko Carstens }
4449b0c632dbSHeiko Carstens 
4450b0c632dbSHeiko Carstens /*
4451b0c632dbSHeiko Carstens  * store status at address
4452b0c632dbSHeiko Carstens  * we use have two special cases:
4453b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
4454b0c632dbSHeiko Carstens  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
4455b0c632dbSHeiko Carstens  */
4456d0bce605SHeiko Carstens int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
4457b0c632dbSHeiko Carstens {
4458092670cdSCarsten Otte 	unsigned char archmode = 1;
44599abc2a08SDavid Hildenbrand 	freg_t fprs[NUM_FPRS];
4460fda902cbSMichael Mueller 	unsigned int px;
44614287f247SDavid Hildenbrand 	u64 clkcomp, cputm;
4462d0bce605SHeiko Carstens 	int rc;
4463b0c632dbSHeiko Carstens 
4464d9a3a09aSMartin Schwidefsky 	px = kvm_s390_get_prefix(vcpu);
4465d0bce605SHeiko Carstens 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
4466d0bce605SHeiko Carstens 		if (write_guest_abs(vcpu, 163, &archmode, 1))
4467b0c632dbSHeiko Carstens 			return -EFAULT;
4468d9a3a09aSMartin Schwidefsky 		gpa = 0;
4469d0bce605SHeiko Carstens 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
4470d0bce605SHeiko Carstens 		if (write_guest_real(vcpu, 163, &archmode, 1))
4471b0c632dbSHeiko Carstens 			return -EFAULT;
4472d9a3a09aSMartin Schwidefsky 		gpa = px;
4473d9a3a09aSMartin Schwidefsky 	} else
4474d9a3a09aSMartin Schwidefsky 		gpa -= __LC_FPREGS_SAVE_AREA;
44759abc2a08SDavid Hildenbrand 
44769abc2a08SDavid Hildenbrand 	/* manually convert vector registers if necessary */
44779abc2a08SDavid Hildenbrand 	if (MACHINE_HAS_VX) {
44789522b37fSDavid Hildenbrand 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
4479d9a3a09aSMartin Schwidefsky 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
44809abc2a08SDavid Hildenbrand 				     fprs, 128);
44819abc2a08SDavid Hildenbrand 	} else {
44829abc2a08SDavid Hildenbrand 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
44836fd8e67dSDavid Hildenbrand 				     vcpu->run->s.regs.fprs, 128);
44849abc2a08SDavid Hildenbrand 	}
4485d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
4486d0bce605SHeiko Carstens 			      vcpu->run->s.regs.gprs, 128);
4487d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
4488d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gpsw, 16);
4489d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
4490fda902cbSMichael Mueller 			      &px, 4);
4491d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
44929abc2a08SDavid Hildenbrand 			      &vcpu->run->s.regs.fpc, 4);
4493d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
4494d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->todpr, 4);
44954287f247SDavid Hildenbrand 	cputm = kvm_s390_get_cpu_timer(vcpu);
4496d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
44974287f247SDavid Hildenbrand 			      &cputm, 8);
4498178bd789SThomas Huth 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
4499d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
4500d0bce605SHeiko Carstens 			      &clkcomp, 8);
4501d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
4502d0bce605SHeiko Carstens 			      &vcpu->run->s.regs.acrs, 64);
4503d9a3a09aSMartin Schwidefsky 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
4504d0bce605SHeiko Carstens 			      &vcpu->arch.sie_block->gcr, 128);
4505d0bce605SHeiko Carstens 	return rc ? -EFAULT : 0;
4506b0c632dbSHeiko Carstens }
4507b0c632dbSHeiko Carstens 
4508e879892cSThomas Huth int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
4509e879892cSThomas Huth {
4510e879892cSThomas Huth 	/*
4511e879892cSThomas Huth 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
451231d8b8d4SChristian Borntraeger 	 * switch in the run ioctl. Let's update our copies before we save
4513e879892cSThomas Huth 	 * it into the save area
4514e879892cSThomas Huth 	 */
4515d0164ee2SHendrik Brueckner 	save_fpu_regs();
45169abc2a08SDavid Hildenbrand 	vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4517e879892cSThomas Huth 	save_access_regs(vcpu->run->s.regs.acrs);
4518e879892cSThomas Huth 
4519e879892cSThomas Huth 	return kvm_s390_store_status_unloaded(vcpu, addr);
4520e879892cSThomas Huth }
4521e879892cSThomas Huth 
45228ad35755SDavid Hildenbrand static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
45238ad35755SDavid Hildenbrand {
45248ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
45258e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
45268ad35755SDavid Hildenbrand }
45278ad35755SDavid Hildenbrand 
45288ad35755SDavid Hildenbrand static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
45298ad35755SDavid Hildenbrand {
45308ad35755SDavid Hildenbrand 	unsigned int i;
45318ad35755SDavid Hildenbrand 	struct kvm_vcpu *vcpu;
45328ad35755SDavid Hildenbrand 
45338ad35755SDavid Hildenbrand 	kvm_for_each_vcpu(i, vcpu, kvm) {
45348ad35755SDavid Hildenbrand 		__disable_ibs_on_vcpu(vcpu);
45358ad35755SDavid Hildenbrand 	}
45368ad35755SDavid Hildenbrand }
45378ad35755SDavid Hildenbrand 
45388ad35755SDavid Hildenbrand static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
45398ad35755SDavid Hildenbrand {
454009a400e7SDavid Hildenbrand 	if (!sclp.has_ibs)
454109a400e7SDavid Hildenbrand 		return;
45428ad35755SDavid Hildenbrand 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
45438e236546SChristian Borntraeger 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
45448ad35755SDavid Hildenbrand }
45458ad35755SDavid Hildenbrand 
4546fe28c786SJanosch Frank int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
45476852d7b6SDavid Hildenbrand {
4548fe28c786SJanosch Frank 	int i, online_vcpus, r = 0, started_vcpus = 0;
45498ad35755SDavid Hildenbrand 
45508ad35755SDavid Hildenbrand 	if (!is_vcpu_stopped(vcpu))
4551fe28c786SJanosch Frank 		return 0;
45528ad35755SDavid Hildenbrand 
45536852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
45548ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
4555433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
45568ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
45578ad35755SDavid Hildenbrand 
4558fe28c786SJanosch Frank 	/* Let's tell the UV that we want to change into the operating state */
4559fe28c786SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4560fe28c786SJanosch Frank 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
4561fe28c786SJanosch Frank 		if (r) {
4562fe28c786SJanosch Frank 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4563fe28c786SJanosch Frank 			return r;
4564fe28c786SJanosch Frank 		}
4565fe28c786SJanosch Frank 	}
4566fe28c786SJanosch Frank 
45678ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
45688ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
45698ad35755SDavid Hildenbrand 			started_vcpus++;
45708ad35755SDavid Hildenbrand 	}
45718ad35755SDavid Hildenbrand 
45728ad35755SDavid Hildenbrand 	if (started_vcpus == 0) {
45738ad35755SDavid Hildenbrand 		/* we're the only active VCPU -> speed it up */
45748ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(vcpu);
45758ad35755SDavid Hildenbrand 	} else if (started_vcpus == 1) {
45768ad35755SDavid Hildenbrand 		/*
45778ad35755SDavid Hildenbrand 		 * As we are starting a second VCPU, we have to disable
45788ad35755SDavid Hildenbrand 		 * the IBS facility on all VCPUs to remove potentially
457938860756SBhaskar Chowdhury 		 * outstanding ENABLE requests.
45808ad35755SDavid Hildenbrand 		 */
45818ad35755SDavid Hildenbrand 		__disable_ibs_on_all_vcpus(vcpu->kvm);
45828ad35755SDavid Hildenbrand 	}
45838ad35755SDavid Hildenbrand 
45849daecfc6SDavid Hildenbrand 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
45858ad35755SDavid Hildenbrand 	/*
458672f21820SChristian Borntraeger 	 * The real PSW might have changed due to a RESTART interpreted by the
458772f21820SChristian Borntraeger 	 * ultravisor. We block all interrupts and let the next sie exit
458872f21820SChristian Borntraeger 	 * refresh our view.
458972f21820SChristian Borntraeger 	 */
459072f21820SChristian Borntraeger 	if (kvm_s390_pv_cpu_is_protected(vcpu))
459172f21820SChristian Borntraeger 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
459272f21820SChristian Borntraeger 	/*
45938ad35755SDavid Hildenbrand 	 * Another VCPU might have used IBS while we were offline.
45948ad35755SDavid Hildenbrand 	 * Let's play safe and flush the VCPU at startup.
45958ad35755SDavid Hildenbrand 	 */
4596d3d692c8SDavid Hildenbrand 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4597433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4598fe28c786SJanosch Frank 	return 0;
45996852d7b6SDavid Hildenbrand }
46006852d7b6SDavid Hildenbrand 
4601fe28c786SJanosch Frank int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
46026852d7b6SDavid Hildenbrand {
4603fe28c786SJanosch Frank 	int i, online_vcpus, r = 0, started_vcpus = 0;
46048ad35755SDavid Hildenbrand 	struct kvm_vcpu *started_vcpu = NULL;
46058ad35755SDavid Hildenbrand 
46068ad35755SDavid Hildenbrand 	if (is_vcpu_stopped(vcpu))
4607fe28c786SJanosch Frank 		return 0;
46088ad35755SDavid Hildenbrand 
46096852d7b6SDavid Hildenbrand 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
46108ad35755SDavid Hildenbrand 	/* Only one cpu at a time may enter/leave the STOPPED state. */
4611433b9ee4SDavid Hildenbrand 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
46128ad35755SDavid Hildenbrand 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
46138ad35755SDavid Hildenbrand 
4614fe28c786SJanosch Frank 	/* Let's tell the UV that we want to change into the stopped state */
4615fe28c786SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4616fe28c786SJanosch Frank 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
4617fe28c786SJanosch Frank 		if (r) {
4618fe28c786SJanosch Frank 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4619fe28c786SJanosch Frank 			return r;
4620fe28c786SJanosch Frank 		}
4621fe28c786SJanosch Frank 	}
4622fe28c786SJanosch Frank 
462332f5ff63SDavid Hildenbrand 	/* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
46246cddd432SDavid Hildenbrand 	kvm_s390_clear_stop_irq(vcpu);
462532f5ff63SDavid Hildenbrand 
4626ef8f4f49SDavid Hildenbrand 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
46278ad35755SDavid Hildenbrand 	__disable_ibs_on_vcpu(vcpu);
46288ad35755SDavid Hildenbrand 
46298ad35755SDavid Hildenbrand 	for (i = 0; i < online_vcpus; i++) {
46308ad35755SDavid Hildenbrand 		if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
46318ad35755SDavid Hildenbrand 			started_vcpus++;
46328ad35755SDavid Hildenbrand 			started_vcpu = vcpu->kvm->vcpus[i];
46338ad35755SDavid Hildenbrand 		}
46348ad35755SDavid Hildenbrand 	}
46358ad35755SDavid Hildenbrand 
46368ad35755SDavid Hildenbrand 	if (started_vcpus == 1) {
46378ad35755SDavid Hildenbrand 		/*
46388ad35755SDavid Hildenbrand 		 * As we only have one VCPU left, we want to enable the
46398ad35755SDavid Hildenbrand 		 * IBS facility for that VCPU to speed it up.
46408ad35755SDavid Hildenbrand 		 */
46418ad35755SDavid Hildenbrand 		__enable_ibs_on_vcpu(started_vcpu);
46428ad35755SDavid Hildenbrand 	}
46438ad35755SDavid Hildenbrand 
4644433b9ee4SDavid Hildenbrand 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4645fe28c786SJanosch Frank 	return 0;
46466852d7b6SDavid Hildenbrand }
46476852d7b6SDavid Hildenbrand 
4648d6712df9SCornelia Huck static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4649d6712df9SCornelia Huck 				     struct kvm_enable_cap *cap)
4650d6712df9SCornelia Huck {
4651d6712df9SCornelia Huck 	int r;
4652d6712df9SCornelia Huck 
4653d6712df9SCornelia Huck 	if (cap->flags)
4654d6712df9SCornelia Huck 		return -EINVAL;
4655d6712df9SCornelia Huck 
4656d6712df9SCornelia Huck 	switch (cap->cap) {
4657fa6b7fe9SCornelia Huck 	case KVM_CAP_S390_CSS_SUPPORT:
4658fa6b7fe9SCornelia Huck 		if (!vcpu->kvm->arch.css_support) {
4659fa6b7fe9SCornelia Huck 			vcpu->kvm->arch.css_support = 1;
4660c92ea7b9SChristian Borntraeger 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
4661fa6b7fe9SCornelia Huck 			trace_kvm_s390_enable_css(vcpu->kvm);
4662fa6b7fe9SCornelia Huck 		}
4663fa6b7fe9SCornelia Huck 		r = 0;
4664fa6b7fe9SCornelia Huck 		break;
4665d6712df9SCornelia Huck 	default:
4666d6712df9SCornelia Huck 		r = -EINVAL;
4667d6712df9SCornelia Huck 		break;
4668d6712df9SCornelia Huck 	}
4669d6712df9SCornelia Huck 	return r;
4670d6712df9SCornelia Huck }
4671d6712df9SCornelia Huck 
467219e12277SJanosch Frank static long kvm_s390_guest_sida_op(struct kvm_vcpu *vcpu,
467319e12277SJanosch Frank 				   struct kvm_s390_mem_op *mop)
467419e12277SJanosch Frank {
467519e12277SJanosch Frank 	void __user *uaddr = (void __user *)mop->buf;
467619e12277SJanosch Frank 	int r = 0;
467719e12277SJanosch Frank 
467819e12277SJanosch Frank 	if (mop->flags || !mop->size)
467919e12277SJanosch Frank 		return -EINVAL;
468019e12277SJanosch Frank 	if (mop->size + mop->sida_offset < mop->size)
468119e12277SJanosch Frank 		return -EINVAL;
468219e12277SJanosch Frank 	if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
468319e12277SJanosch Frank 		return -E2BIG;
468419e12277SJanosch Frank 
468519e12277SJanosch Frank 	switch (mop->op) {
468619e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_READ:
468719e12277SJanosch Frank 		if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) +
468819e12277SJanosch Frank 				 mop->sida_offset), mop->size))
468919e12277SJanosch Frank 			r = -EFAULT;
469019e12277SJanosch Frank 
469119e12277SJanosch Frank 		break;
469219e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_WRITE:
469319e12277SJanosch Frank 		if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) +
469419e12277SJanosch Frank 				   mop->sida_offset), uaddr, mop->size))
469519e12277SJanosch Frank 			r = -EFAULT;
469619e12277SJanosch Frank 		break;
469719e12277SJanosch Frank 	}
469819e12277SJanosch Frank 	return r;
469919e12277SJanosch Frank }
470041408c28SThomas Huth static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
470141408c28SThomas Huth 				  struct kvm_s390_mem_op *mop)
470241408c28SThomas Huth {
470341408c28SThomas Huth 	void __user *uaddr = (void __user *)mop->buf;
470441408c28SThomas Huth 	void *tmpbuf = NULL;
470519e12277SJanosch Frank 	int r = 0;
470641408c28SThomas Huth 	const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
470741408c28SThomas Huth 				    | KVM_S390_MEMOP_F_CHECK_ONLY;
470841408c28SThomas Huth 
4709a13b03bbSThomas Huth 	if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
471041408c28SThomas Huth 		return -EINVAL;
471141408c28SThomas Huth 
471241408c28SThomas Huth 	if (mop->size > MEM_OP_MAX_SIZE)
471341408c28SThomas Huth 		return -E2BIG;
471441408c28SThomas Huth 
471519e12277SJanosch Frank 	if (kvm_s390_pv_cpu_is_protected(vcpu))
471619e12277SJanosch Frank 		return -EINVAL;
471719e12277SJanosch Frank 
471841408c28SThomas Huth 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
471941408c28SThomas Huth 		tmpbuf = vmalloc(mop->size);
472041408c28SThomas Huth 		if (!tmpbuf)
472141408c28SThomas Huth 			return -ENOMEM;
472241408c28SThomas Huth 	}
472341408c28SThomas Huth 
472441408c28SThomas Huth 	switch (mop->op) {
472541408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_READ:
472641408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
472792c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
472892c96321SDavid Hildenbrand 					    mop->size, GACC_FETCH);
472941408c28SThomas Huth 			break;
473041408c28SThomas Huth 		}
473141408c28SThomas Huth 		r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
473241408c28SThomas Huth 		if (r == 0) {
473341408c28SThomas Huth 			if (copy_to_user(uaddr, tmpbuf, mop->size))
473441408c28SThomas Huth 				r = -EFAULT;
473541408c28SThomas Huth 		}
473641408c28SThomas Huth 		break;
473741408c28SThomas Huth 	case KVM_S390_MEMOP_LOGICAL_WRITE:
473841408c28SThomas Huth 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
473992c96321SDavid Hildenbrand 			r = check_gva_range(vcpu, mop->gaddr, mop->ar,
474092c96321SDavid Hildenbrand 					    mop->size, GACC_STORE);
474141408c28SThomas Huth 			break;
474241408c28SThomas Huth 		}
474341408c28SThomas Huth 		if (copy_from_user(tmpbuf, uaddr, mop->size)) {
474441408c28SThomas Huth 			r = -EFAULT;
474541408c28SThomas Huth 			break;
474641408c28SThomas Huth 		}
474741408c28SThomas Huth 		r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
474841408c28SThomas Huth 		break;
474941408c28SThomas Huth 	}
475041408c28SThomas Huth 
475141408c28SThomas Huth 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
475241408c28SThomas Huth 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
475341408c28SThomas Huth 
475441408c28SThomas Huth 	vfree(tmpbuf);
475541408c28SThomas Huth 	return r;
475641408c28SThomas Huth }
475741408c28SThomas Huth 
475819e12277SJanosch Frank static long kvm_s390_guest_memsida_op(struct kvm_vcpu *vcpu,
475919e12277SJanosch Frank 				      struct kvm_s390_mem_op *mop)
476019e12277SJanosch Frank {
476119e12277SJanosch Frank 	int r, srcu_idx;
476219e12277SJanosch Frank 
476319e12277SJanosch Frank 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
476419e12277SJanosch Frank 
476519e12277SJanosch Frank 	switch (mop->op) {
476619e12277SJanosch Frank 	case KVM_S390_MEMOP_LOGICAL_READ:
476719e12277SJanosch Frank 	case KVM_S390_MEMOP_LOGICAL_WRITE:
476819e12277SJanosch Frank 		r = kvm_s390_guest_mem_op(vcpu, mop);
476919e12277SJanosch Frank 		break;
477019e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_READ:
477119e12277SJanosch Frank 	case KVM_S390_MEMOP_SIDA_WRITE:
477219e12277SJanosch Frank 		/* we are locked against sida going away by the vcpu->mutex */
477319e12277SJanosch Frank 		r = kvm_s390_guest_sida_op(vcpu, mop);
477419e12277SJanosch Frank 		break;
477519e12277SJanosch Frank 	default:
477619e12277SJanosch Frank 		r = -EINVAL;
477719e12277SJanosch Frank 	}
477819e12277SJanosch Frank 
477919e12277SJanosch Frank 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
478019e12277SJanosch Frank 	return r;
478119e12277SJanosch Frank }
478219e12277SJanosch Frank 
47835cb0944cSPaolo Bonzini long kvm_arch_vcpu_async_ioctl(struct file *filp,
4784b0c632dbSHeiko Carstens 			       unsigned int ioctl, unsigned long arg)
4785b0c632dbSHeiko Carstens {
4786b0c632dbSHeiko Carstens 	struct kvm_vcpu *vcpu = filp->private_data;
4787b0c632dbSHeiko Carstens 	void __user *argp = (void __user *)arg;
4788b0c632dbSHeiko Carstens 
478993736624SAvi Kivity 	switch (ioctl) {
479047b43c52SJens Freimann 	case KVM_S390_IRQ: {
479147b43c52SJens Freimann 		struct kvm_s390_irq s390irq;
479247b43c52SJens Freimann 
479347b43c52SJens Freimann 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
47949b062471SChristoffer Dall 			return -EFAULT;
47959b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
479647b43c52SJens Freimann 	}
479793736624SAvi Kivity 	case KVM_S390_INTERRUPT: {
4798ba5c1e9bSCarsten Otte 		struct kvm_s390_interrupt s390int;
479953936b5bSThomas Huth 		struct kvm_s390_irq s390irq = {};
4800ba5c1e9bSCarsten Otte 
4801ba5c1e9bSCarsten Otte 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
48029b062471SChristoffer Dall 			return -EFAULT;
4803383d0b05SJens Freimann 		if (s390int_to_s390irq(&s390int, &s390irq))
4804383d0b05SJens Freimann 			return -EINVAL;
48059b062471SChristoffer Dall 		return kvm_s390_inject_vcpu(vcpu, &s390irq);
4806ba5c1e9bSCarsten Otte 	}
48079b062471SChristoffer Dall 	}
48085cb0944cSPaolo Bonzini 	return -ENOIOCTLCMD;
48095cb0944cSPaolo Bonzini }
48105cb0944cSPaolo Bonzini 
48115cb0944cSPaolo Bonzini long kvm_arch_vcpu_ioctl(struct file *filp,
48125cb0944cSPaolo Bonzini 			 unsigned int ioctl, unsigned long arg)
48135cb0944cSPaolo Bonzini {
48145cb0944cSPaolo Bonzini 	struct kvm_vcpu *vcpu = filp->private_data;
48155cb0944cSPaolo Bonzini 	void __user *argp = (void __user *)arg;
48165cb0944cSPaolo Bonzini 	int idx;
48175cb0944cSPaolo Bonzini 	long r;
48188a8378faSJanosch Frank 	u16 rc, rrc;
48199b062471SChristoffer Dall 
48209b062471SChristoffer Dall 	vcpu_load(vcpu);
48219b062471SChristoffer Dall 
48229b062471SChristoffer Dall 	switch (ioctl) {
4823b0c632dbSHeiko Carstens 	case KVM_S390_STORE_STATUS:
4824800c1065SThomas Huth 		idx = srcu_read_lock(&vcpu->kvm->srcu);
482555680890SChristian Borntraeger 		r = kvm_s390_store_status_unloaded(vcpu, arg);
4826800c1065SThomas Huth 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
4827bc923cc9SAvi Kivity 		break;
4828b0c632dbSHeiko Carstens 	case KVM_S390_SET_INITIAL_PSW: {
4829b0c632dbSHeiko Carstens 		psw_t psw;
4830b0c632dbSHeiko Carstens 
4831bc923cc9SAvi Kivity 		r = -EFAULT;
4832b0c632dbSHeiko Carstens 		if (copy_from_user(&psw, argp, sizeof(psw)))
4833bc923cc9SAvi Kivity 			break;
4834bc923cc9SAvi Kivity 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
4835bc923cc9SAvi Kivity 		break;
4836b0c632dbSHeiko Carstens 	}
48377de3f142SJanosch Frank 	case KVM_S390_CLEAR_RESET:
48387de3f142SJanosch Frank 		r = 0;
48397de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_clear_reset(vcpu);
48408a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
48418a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
48428a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
48438a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
48448a8378faSJanosch Frank 				   rc, rrc);
48458a8378faSJanosch Frank 		}
48467de3f142SJanosch Frank 		break;
4847b0c632dbSHeiko Carstens 	case KVM_S390_INITIAL_RESET:
48487de3f142SJanosch Frank 		r = 0;
48497de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_initial_reset(vcpu);
48508a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
48518a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
48528a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET_INITIAL,
48538a8378faSJanosch Frank 					  &rc, &rrc);
48548a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
48558a8378faSJanosch Frank 				   rc, rrc);
48568a8378faSJanosch Frank 		}
48577de3f142SJanosch Frank 		break;
48587de3f142SJanosch Frank 	case KVM_S390_NORMAL_RESET:
48597de3f142SJanosch Frank 		r = 0;
48607de3f142SJanosch Frank 		kvm_arch_vcpu_ioctl_normal_reset(vcpu);
48618a8378faSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
48628a8378faSJanosch Frank 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
48638a8378faSJanosch Frank 					  UVC_CMD_CPU_RESET, &rc, &rrc);
48648a8378faSJanosch Frank 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
48658a8378faSJanosch Frank 				   rc, rrc);
48668a8378faSJanosch Frank 		}
4867bc923cc9SAvi Kivity 		break;
486814eebd91SCarsten Otte 	case KVM_SET_ONE_REG:
486914eebd91SCarsten Otte 	case KVM_GET_ONE_REG: {
487014eebd91SCarsten Otte 		struct kvm_one_reg reg;
487168cf7b1fSJanosch Frank 		r = -EINVAL;
487268cf7b1fSJanosch Frank 		if (kvm_s390_pv_cpu_is_protected(vcpu))
487368cf7b1fSJanosch Frank 			break;
487414eebd91SCarsten Otte 		r = -EFAULT;
487514eebd91SCarsten Otte 		if (copy_from_user(&reg, argp, sizeof(reg)))
487614eebd91SCarsten Otte 			break;
487714eebd91SCarsten Otte 		if (ioctl == KVM_SET_ONE_REG)
487814eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
487914eebd91SCarsten Otte 		else
488014eebd91SCarsten Otte 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
488114eebd91SCarsten Otte 		break;
488214eebd91SCarsten Otte 	}
488327e0393fSCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
488427e0393fSCarsten Otte 	case KVM_S390_UCAS_MAP: {
488527e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
488627e0393fSCarsten Otte 
488727e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
488827e0393fSCarsten Otte 			r = -EFAULT;
488927e0393fSCarsten Otte 			break;
489027e0393fSCarsten Otte 		}
489127e0393fSCarsten Otte 
489227e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
489327e0393fSCarsten Otte 			r = -EINVAL;
489427e0393fSCarsten Otte 			break;
489527e0393fSCarsten Otte 		}
489627e0393fSCarsten Otte 
489727e0393fSCarsten Otte 		r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
489827e0393fSCarsten Otte 				     ucasmap.vcpu_addr, ucasmap.length);
489927e0393fSCarsten Otte 		break;
490027e0393fSCarsten Otte 	}
490127e0393fSCarsten Otte 	case KVM_S390_UCAS_UNMAP: {
490227e0393fSCarsten Otte 		struct kvm_s390_ucas_mapping ucasmap;
490327e0393fSCarsten Otte 
490427e0393fSCarsten Otte 		if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
490527e0393fSCarsten Otte 			r = -EFAULT;
490627e0393fSCarsten Otte 			break;
490727e0393fSCarsten Otte 		}
490827e0393fSCarsten Otte 
490927e0393fSCarsten Otte 		if (!kvm_is_ucontrol(vcpu->kvm)) {
491027e0393fSCarsten Otte 			r = -EINVAL;
491127e0393fSCarsten Otte 			break;
491227e0393fSCarsten Otte 		}
491327e0393fSCarsten Otte 
491427e0393fSCarsten Otte 		r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
491527e0393fSCarsten Otte 			ucasmap.length);
491627e0393fSCarsten Otte 		break;
491727e0393fSCarsten Otte 	}
491827e0393fSCarsten Otte #endif
4919ccc7910fSCarsten Otte 	case KVM_S390_VCPU_FAULT: {
4920527e30b4SMartin Schwidefsky 		r = gmap_fault(vcpu->arch.gmap, arg, 0);
4921ccc7910fSCarsten Otte 		break;
4922ccc7910fSCarsten Otte 	}
4923d6712df9SCornelia Huck 	case KVM_ENABLE_CAP:
4924d6712df9SCornelia Huck 	{
4925d6712df9SCornelia Huck 		struct kvm_enable_cap cap;
4926d6712df9SCornelia Huck 		r = -EFAULT;
4927d6712df9SCornelia Huck 		if (copy_from_user(&cap, argp, sizeof(cap)))
4928d6712df9SCornelia Huck 			break;
4929d6712df9SCornelia Huck 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4930d6712df9SCornelia Huck 		break;
4931d6712df9SCornelia Huck 	}
493241408c28SThomas Huth 	case KVM_S390_MEM_OP: {
493341408c28SThomas Huth 		struct kvm_s390_mem_op mem_op;
493441408c28SThomas Huth 
493541408c28SThomas Huth 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
493619e12277SJanosch Frank 			r = kvm_s390_guest_memsida_op(vcpu, &mem_op);
493741408c28SThomas Huth 		else
493841408c28SThomas Huth 			r = -EFAULT;
493941408c28SThomas Huth 		break;
494041408c28SThomas Huth 	}
4941816c7667SJens Freimann 	case KVM_S390_SET_IRQ_STATE: {
4942816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
4943816c7667SJens Freimann 
4944816c7667SJens Freimann 		r = -EFAULT;
4945816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4946816c7667SJens Freimann 			break;
4947816c7667SJens Freimann 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
4948816c7667SJens Freimann 		    irq_state.len == 0 ||
4949816c7667SJens Freimann 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
4950816c7667SJens Freimann 			r = -EINVAL;
4951816c7667SJens Freimann 			break;
4952816c7667SJens Freimann 		}
4953bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
4954816c7667SJens Freimann 		r = kvm_s390_set_irq_state(vcpu,
4955816c7667SJens Freimann 					   (void __user *) irq_state.buf,
4956816c7667SJens Freimann 					   irq_state.len);
4957816c7667SJens Freimann 		break;
4958816c7667SJens Freimann 	}
4959816c7667SJens Freimann 	case KVM_S390_GET_IRQ_STATE: {
4960816c7667SJens Freimann 		struct kvm_s390_irq_state irq_state;
4961816c7667SJens Freimann 
4962816c7667SJens Freimann 		r = -EFAULT;
4963816c7667SJens Freimann 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4964816c7667SJens Freimann 			break;
4965816c7667SJens Freimann 		if (irq_state.len == 0) {
4966816c7667SJens Freimann 			r = -EINVAL;
4967816c7667SJens Freimann 			break;
4968816c7667SJens Freimann 		}
4969bb64da9aSChristian Borntraeger 		/* do not use irq_state.flags, it will break old QEMUs */
4970816c7667SJens Freimann 		r = kvm_s390_get_irq_state(vcpu,
4971816c7667SJens Freimann 					   (__u8 __user *)  irq_state.buf,
4972816c7667SJens Freimann 					   irq_state.len);
4973816c7667SJens Freimann 		break;
4974816c7667SJens Freimann 	}
4975b0c632dbSHeiko Carstens 	default:
49763e6afcf1SCarsten Otte 		r = -ENOTTY;
4977b0c632dbSHeiko Carstens 	}
49789b062471SChristoffer Dall 
49799b062471SChristoffer Dall 	vcpu_put(vcpu);
4980bc923cc9SAvi Kivity 	return r;
4981b0c632dbSHeiko Carstens }
4982b0c632dbSHeiko Carstens 
49831499fa80SSouptick Joarder vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
49845b1c1493SCarsten Otte {
49855b1c1493SCarsten Otte #ifdef CONFIG_KVM_S390_UCONTROL
49865b1c1493SCarsten Otte 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
49875b1c1493SCarsten Otte 		 && (kvm_is_ucontrol(vcpu->kvm))) {
49885b1c1493SCarsten Otte 		vmf->page = virt_to_page(vcpu->arch.sie_block);
49895b1c1493SCarsten Otte 		get_page(vmf->page);
49905b1c1493SCarsten Otte 		return 0;
49915b1c1493SCarsten Otte 	}
49925b1c1493SCarsten Otte #endif
49935b1c1493SCarsten Otte 	return VM_FAULT_SIGBUS;
49945b1c1493SCarsten Otte }
49955b1c1493SCarsten Otte 
4996b0c632dbSHeiko Carstens /* Section: memory related */
4997f7784b8eSMarcelo Tosatti int kvm_arch_prepare_memory_region(struct kvm *kvm,
4998f7784b8eSMarcelo Tosatti 				   struct kvm_memory_slot *memslot,
499909170a49SPaolo Bonzini 				   const struct kvm_userspace_memory_region *mem,
50007b6195a9STakuya Yoshikawa 				   enum kvm_mr_change change)
5001b0c632dbSHeiko Carstens {
5002dd2887e7SNick Wang 	/* A few sanity checks. We can have memory slots which have to be
5003dd2887e7SNick Wang 	   located/ended at a segment boundary (1MB). The memory in userland is
5004dd2887e7SNick Wang 	   ok to be fragmented into various different vmas. It is okay to mmap()
5005dd2887e7SNick Wang 	   and munmap() stuff in this slot after doing this call at any time */
5006b0c632dbSHeiko Carstens 
5007598841caSCarsten Otte 	if (mem->userspace_addr & 0xffffful)
5008b0c632dbSHeiko Carstens 		return -EINVAL;
5009b0c632dbSHeiko Carstens 
5010598841caSCarsten Otte 	if (mem->memory_size & 0xffffful)
5011b0c632dbSHeiko Carstens 		return -EINVAL;
5012b0c632dbSHeiko Carstens 
5013a3a92c31SDominik Dingel 	if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
5014a3a92c31SDominik Dingel 		return -EINVAL;
5015a3a92c31SDominik Dingel 
501629b40f10SJanosch Frank 	/* When we are protected, we should not change the memory slots */
501729b40f10SJanosch Frank 	if (kvm_s390_pv_get_handle(kvm))
501829b40f10SJanosch Frank 		return -EINVAL;
5019f7784b8eSMarcelo Tosatti 	return 0;
5020f7784b8eSMarcelo Tosatti }
5021f7784b8eSMarcelo Tosatti 
5022f7784b8eSMarcelo Tosatti void kvm_arch_commit_memory_region(struct kvm *kvm,
502309170a49SPaolo Bonzini 				const struct kvm_userspace_memory_region *mem,
50249d4c197cSSean Christopherson 				struct kvm_memory_slot *old,
5025f36f3f28SPaolo Bonzini 				const struct kvm_memory_slot *new,
50268482644aSTakuya Yoshikawa 				enum kvm_mr_change change)
5027f7784b8eSMarcelo Tosatti {
502819ec166cSChristian Borntraeger 	int rc = 0;
5029f7784b8eSMarcelo Tosatti 
503019ec166cSChristian Borntraeger 	switch (change) {
503119ec166cSChristian Borntraeger 	case KVM_MR_DELETE:
503219ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
503319ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
503419ec166cSChristian Borntraeger 		break;
503519ec166cSChristian Borntraeger 	case KVM_MR_MOVE:
503619ec166cSChristian Borntraeger 		rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
503719ec166cSChristian Borntraeger 					old->npages * PAGE_SIZE);
503819ec166cSChristian Borntraeger 		if (rc)
503919ec166cSChristian Borntraeger 			break;
50403b684a42SJoe Perches 		fallthrough;
504119ec166cSChristian Borntraeger 	case KVM_MR_CREATE:
5042598841caSCarsten Otte 		rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
5043598841caSCarsten Otte 				      mem->guest_phys_addr, mem->memory_size);
504419ec166cSChristian Borntraeger 		break;
504519ec166cSChristian Borntraeger 	case KVM_MR_FLAGS_ONLY:
504619ec166cSChristian Borntraeger 		break;
504719ec166cSChristian Borntraeger 	default:
504819ec166cSChristian Borntraeger 		WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
504919ec166cSChristian Borntraeger 	}
5050598841caSCarsten Otte 	if (rc)
5051ea2cdd27SDavid Hildenbrand 		pr_warn("failed to commit memory region\n");
5052598841caSCarsten Otte 	return;
5053b0c632dbSHeiko Carstens }
5054b0c632dbSHeiko Carstens 
505560a37709SAlexander Yarygin static inline unsigned long nonhyp_mask(int i)
505660a37709SAlexander Yarygin {
505760a37709SAlexander Yarygin 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
505860a37709SAlexander Yarygin 
505960a37709SAlexander Yarygin 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
506060a37709SAlexander Yarygin }
506160a37709SAlexander Yarygin 
50623491caf2SChristian Borntraeger void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
50633491caf2SChristian Borntraeger {
50643491caf2SChristian Borntraeger 	vcpu->valid_wakeup = false;
50653491caf2SChristian Borntraeger }
50663491caf2SChristian Borntraeger 
5067b0c632dbSHeiko Carstens static int __init kvm_s390_init(void)
5068b0c632dbSHeiko Carstens {
506960a37709SAlexander Yarygin 	int i;
507060a37709SAlexander Yarygin 
507107197fd0SDavid Hildenbrand 	if (!sclp.has_sief2) {
50728d43d570SMichael Mueller 		pr_info("SIE is not available\n");
507307197fd0SDavid Hildenbrand 		return -ENODEV;
507407197fd0SDavid Hildenbrand 	}
507507197fd0SDavid Hildenbrand 
5076a4499382SJanosch Frank 	if (nested && hpage) {
50778d43d570SMichael Mueller 		pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
5078a4499382SJanosch Frank 		return -EINVAL;
5079a4499382SJanosch Frank 	}
5080a4499382SJanosch Frank 
508160a37709SAlexander Yarygin 	for (i = 0; i < 16; i++)
5082c3b9e3e1SChristian Borntraeger 		kvm_s390_fac_base[i] |=
508317e89e13SSven Schnelle 			stfle_fac_list[i] & nonhyp_mask(i);
508460a37709SAlexander Yarygin 
50859d8d5786SMichael Mueller 	return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
5086b0c632dbSHeiko Carstens }
5087b0c632dbSHeiko Carstens 
5088b0c632dbSHeiko Carstens static void __exit kvm_s390_exit(void)
5089b0c632dbSHeiko Carstens {
5090b0c632dbSHeiko Carstens 	kvm_exit();
5091b0c632dbSHeiko Carstens }
5092b0c632dbSHeiko Carstens 
5093b0c632dbSHeiko Carstens module_init(kvm_s390_init);
5094b0c632dbSHeiko Carstens module_exit(kvm_s390_exit);
5095566af940SCornelia Huck 
5096566af940SCornelia Huck /*
5097566af940SCornelia Huck  * Enable autoloading of the kvm module.
5098566af940SCornelia Huck  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5099566af940SCornelia Huck  * since x86 takes a different approach.
5100566af940SCornelia Huck  */
5101566af940SCornelia Huck #include <linux/miscdevice.h>
5102566af940SCornelia Huck MODULE_ALIAS_MISCDEV(KVM_MINOR);
5103566af940SCornelia Huck MODULE_ALIAS("devname:kvm");
5104