xref: /linux/arch/s390/kvm/kvm-s390.c (revision 91b16b53a08c3684ea2b0ad3cbf8ecd48c0f8b77)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * hosting IBM Z kernel virtual machines (s390x)
4  *
5  * Copyright IBM Corp. 2008, 2020
6  *
7  *    Author(s): Carsten Otte <cotte@de.ibm.com>
8  *               Christian Borntraeger <borntraeger@de.ibm.com>
9  *               Christian Ehrhardt <ehrhardt@de.ibm.com>
10  *               Jason J. Herne <jjherne@us.ibm.com>
11  */
12 
13 #define pr_fmt(fmt) "kvm-s390: " fmt
14 
15 #include <linux/compiler.h>
16 #include <linux/entry-virt.h>
17 #include <linux/export.h>
18 #include <linux/err.h>
19 #include <linux/fs.h>
20 #include <linux/hrtimer.h>
21 #include <linux/init.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/mman.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/cpufeature.h>
28 #include <linux/random.h>
29 #include <linux/slab.h>
30 #include <linux/timer.h>
31 #include <linux/vmalloc.h>
32 #include <linux/bitmap.h>
33 #include <linux/sched/signal.h>
34 #include <linux/string.h>
35 #include <linux/pgtable.h>
36 #include <linux/mmu_notifier.h>
37 
38 #include <asm/access-regs.h>
39 #include <asm/asm-offsets.h>
40 #include <asm/lowcore.h>
41 #include <asm/machine.h>
42 #include <asm/stp.h>
43 #include <asm/gmap_helpers.h>
44 #include <asm/nmi.h>
45 #include <asm/isc.h>
46 #include <asm/sclp.h>
47 #include <asm/cpacf.h>
48 #include <asm/timex.h>
49 #include <asm/asm.h>
50 #include <asm/fpu.h>
51 #include <asm/ap.h>
52 #include <asm/uv.h>
53 #include "kvm-s390.h"
54 #include "gaccess.h"
55 #include "gmap.h"
56 #include "faultin.h"
57 #include "pci.h"
58 
59 #define CREATE_TRACE_POINTS
60 #include "trace.h"
61 #include "trace-s390.h"
62 
63 #define MEM_OP_MAX_SIZE 65536	/* Maximum transfer size for KVM_S390_MEM_OP */
64 #define LOCAL_IRQS 32
65 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
66 			   (KVM_MAX_VCPUS + LOCAL_IRQS))
67 
68 const struct kvm_stats_desc kvm_vm_stats_desc[] = {
69 	KVM_GENERIC_VM_STATS(),
70 	STATS_DESC_COUNTER(VM, inject_io),
71 	STATS_DESC_COUNTER(VM, io_390_adapter_map),
72 	STATS_DESC_COUNTER(VM, io_390_adapter_unmap),
73 	STATS_DESC_COUNTER(VM, io_390_inatomic),
74 	STATS_DESC_COUNTER(VM, io_flic_inject_airq),
75 	STATS_DESC_COUNTER(VM, io_set_adapter_int),
76 	STATS_DESC_COUNTER(VM, io_390_inatomic_no_inject),
77 	STATS_DESC_COUNTER(VM, inject_float_mchk),
78 	STATS_DESC_COUNTER(VM, inject_pfault_done),
79 	STATS_DESC_COUNTER(VM, inject_service_signal),
80 	STATS_DESC_COUNTER(VM, inject_virtio),
81 	STATS_DESC_COUNTER(VM, aen_forward),
82 	STATS_DESC_COUNTER(VM, gmap_shadow_reuse),
83 	STATS_DESC_COUNTER(VM, gmap_shadow_create),
84 	STATS_DESC_COUNTER(VM, gmap_shadow_r1_entry),
85 	STATS_DESC_COUNTER(VM, gmap_shadow_r2_entry),
86 	STATS_DESC_COUNTER(VM, gmap_shadow_r3_entry),
87 	STATS_DESC_COUNTER(VM, gmap_shadow_sg_entry),
88 	STATS_DESC_COUNTER(VM, gmap_shadow_pg_entry),
89 };
90 
91 const struct kvm_stats_header kvm_vm_stats_header = {
92 	.name_size = KVM_STATS_NAME_SIZE,
93 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
94 	.id_offset = sizeof(struct kvm_stats_header),
95 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
96 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
97 		       sizeof(kvm_vm_stats_desc),
98 };
99 
100 const struct kvm_stats_desc kvm_vcpu_stats_desc[] = {
101 	KVM_GENERIC_VCPU_STATS(),
102 	STATS_DESC_COUNTER(VCPU, exit_userspace),
103 	STATS_DESC_COUNTER(VCPU, exit_null),
104 	STATS_DESC_COUNTER(VCPU, exit_external_request),
105 	STATS_DESC_COUNTER(VCPU, exit_io_request),
106 	STATS_DESC_COUNTER(VCPU, exit_external_interrupt),
107 	STATS_DESC_COUNTER(VCPU, exit_stop_request),
108 	STATS_DESC_COUNTER(VCPU, exit_validity),
109 	STATS_DESC_COUNTER(VCPU, exit_instruction),
110 	STATS_DESC_COUNTER(VCPU, exit_pei),
111 	STATS_DESC_COUNTER(VCPU, halt_no_poll_steal),
112 	STATS_DESC_COUNTER(VCPU, instruction_lctl),
113 	STATS_DESC_COUNTER(VCPU, instruction_lctlg),
114 	STATS_DESC_COUNTER(VCPU, instruction_stctl),
115 	STATS_DESC_COUNTER(VCPU, instruction_stctg),
116 	STATS_DESC_COUNTER(VCPU, exit_program_interruption),
117 	STATS_DESC_COUNTER(VCPU, exit_instr_and_program),
118 	STATS_DESC_COUNTER(VCPU, exit_operation_exception),
119 	STATS_DESC_COUNTER(VCPU, deliver_ckc),
120 	STATS_DESC_COUNTER(VCPU, deliver_cputm),
121 	STATS_DESC_COUNTER(VCPU, deliver_external_call),
122 	STATS_DESC_COUNTER(VCPU, deliver_emergency_signal),
123 	STATS_DESC_COUNTER(VCPU, deliver_service_signal),
124 	STATS_DESC_COUNTER(VCPU, deliver_virtio),
125 	STATS_DESC_COUNTER(VCPU, deliver_stop_signal),
126 	STATS_DESC_COUNTER(VCPU, deliver_prefix_signal),
127 	STATS_DESC_COUNTER(VCPU, deliver_restart_signal),
128 	STATS_DESC_COUNTER(VCPU, deliver_program),
129 	STATS_DESC_COUNTER(VCPU, deliver_io),
130 	STATS_DESC_COUNTER(VCPU, deliver_machine_check),
131 	STATS_DESC_COUNTER(VCPU, exit_wait_state),
132 	STATS_DESC_COUNTER(VCPU, inject_ckc),
133 	STATS_DESC_COUNTER(VCPU, inject_cputm),
134 	STATS_DESC_COUNTER(VCPU, inject_external_call),
135 	STATS_DESC_COUNTER(VCPU, inject_emergency_signal),
136 	STATS_DESC_COUNTER(VCPU, inject_mchk),
137 	STATS_DESC_COUNTER(VCPU, inject_pfault_init),
138 	STATS_DESC_COUNTER(VCPU, inject_program),
139 	STATS_DESC_COUNTER(VCPU, inject_restart),
140 	STATS_DESC_COUNTER(VCPU, inject_set_prefix),
141 	STATS_DESC_COUNTER(VCPU, inject_stop_signal),
142 	STATS_DESC_COUNTER(VCPU, instruction_epsw),
143 	STATS_DESC_COUNTER(VCPU, instruction_gs),
144 	STATS_DESC_COUNTER(VCPU, instruction_io_other),
145 	STATS_DESC_COUNTER(VCPU, instruction_lpsw),
146 	STATS_DESC_COUNTER(VCPU, instruction_lpswe),
147 	STATS_DESC_COUNTER(VCPU, instruction_lpswey),
148 	STATS_DESC_COUNTER(VCPU, instruction_pfmf),
149 	STATS_DESC_COUNTER(VCPU, instruction_ptff),
150 	STATS_DESC_COUNTER(VCPU, instruction_sck),
151 	STATS_DESC_COUNTER(VCPU, instruction_sckpf),
152 	STATS_DESC_COUNTER(VCPU, instruction_stidp),
153 	STATS_DESC_COUNTER(VCPU, instruction_spx),
154 	STATS_DESC_COUNTER(VCPU, instruction_stpx),
155 	STATS_DESC_COUNTER(VCPU, instruction_stap),
156 	STATS_DESC_COUNTER(VCPU, instruction_iske),
157 	STATS_DESC_COUNTER(VCPU, instruction_ri),
158 	STATS_DESC_COUNTER(VCPU, instruction_rrbe),
159 	STATS_DESC_COUNTER(VCPU, instruction_sske),
160 	STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock),
161 	STATS_DESC_COUNTER(VCPU, instruction_stsi),
162 	STATS_DESC_COUNTER(VCPU, instruction_stfl),
163 	STATS_DESC_COUNTER(VCPU, instruction_tb),
164 	STATS_DESC_COUNTER(VCPU, instruction_tpi),
165 	STATS_DESC_COUNTER(VCPU, instruction_tprot),
166 	STATS_DESC_COUNTER(VCPU, instruction_tsch),
167 	STATS_DESC_COUNTER(VCPU, instruction_sie),
168 	STATS_DESC_COUNTER(VCPU, instruction_essa),
169 	STATS_DESC_COUNTER(VCPU, instruction_sthyi),
170 	STATS_DESC_COUNTER(VCPU, instruction_sigp_sense),
171 	STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running),
172 	STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call),
173 	STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency),
174 	STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency),
175 	STATS_DESC_COUNTER(VCPU, instruction_sigp_start),
176 	STATS_DESC_COUNTER(VCPU, instruction_sigp_stop),
177 	STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status),
178 	STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status),
179 	STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status),
180 	STATS_DESC_COUNTER(VCPU, instruction_sigp_arch),
181 	STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix),
182 	STATS_DESC_COUNTER(VCPU, instruction_sigp_restart),
183 	STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset),
184 	STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset),
185 	STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown),
186 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_10),
187 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_44),
188 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c),
189 	STATS_DESC_COUNTER(VCPU, diag_9c_ignored),
190 	STATS_DESC_COUNTER(VCPU, diag_9c_forward),
191 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_258),
192 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_308),
193 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_500),
194 	STATS_DESC_COUNTER(VCPU, instruction_diagnose_other),
195 	STATS_DESC_COUNTER(VCPU, pfault_sync),
196 	STATS_DESC_COUNTER(VCPU, signal_exits)
197 };
198 
199 const struct kvm_stats_header kvm_vcpu_stats_header = {
200 	.name_size = KVM_STATS_NAME_SIZE,
201 	.num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
202 	.id_offset = sizeof(struct kvm_stats_header),
203 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
204 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
205 		       sizeof(kvm_vcpu_stats_desc),
206 };
207 
208 /* allow nested virtualization in KVM (if enabled by user space) */
209 static int nested;
210 module_param(nested, int, S_IRUGO);
211 MODULE_PARM_DESC(nested, "Nested virtualization support");
212 
213 /* allow 1m huge page guest backing */
214 static int hpage;
215 module_param(hpage, int, 0444);
216 MODULE_PARM_DESC(hpage, "1m huge page backing support");
217 
218 /* allow 2g huge page guest backing */
219 static int hpage_2g;
220 module_param(hpage_2g, int, 0444);
221 MODULE_PARM_DESC(hpage_2g, "2g huge page backing support");
222 
223 /* maximum percentage of steal time for polling.  >100 is treated like 100 */
224 static u8 halt_poll_max_steal = 10;
225 module_param(halt_poll_max_steal, byte, 0644);
226 MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
227 
228 /* if set to true, the GISA will be initialized and used if available */
229 static bool use_gisa  = true;
230 module_param(use_gisa, bool, 0644);
231 MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
232 
233 /* maximum diag9c forwarding per second */
234 unsigned int diag9c_forwarding_hz;
235 module_param(diag9c_forwarding_hz, uint, 0644);
236 MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off");
237 
238 /*
239  * allow asynchronous deinit for protected guests; enable by default since
240  * the feature is opt-in anyway
241  */
242 static int async_destroy = 1;
243 module_param(async_destroy, int, 0444);
244 MODULE_PARM_DESC(async_destroy, "Asynchronous destroy for protected guests");
245 
246 #define HMFAI_DWORDS 16
247 /*
248  * Base feature mask that defines default mask for facilities. Consists of the
249  * defines in FACILITIES_KVM and the non-hypervisor managed bits.
250  */
251 static unsigned long kvm_s390_fac_base[HMFAI_DWORDS] = { FACILITIES_KVM };
252 static_assert(ARRAY_SIZE(((long[]){ FACILITIES_KVM })) <= HMFAI_DWORDS);
253 static_assert(ARRAY_SIZE(kvm_s390_fac_base) <= S390_ARCH_FAC_MASK_SIZE_U64);
254 static_assert(ARRAY_SIZE(kvm_s390_fac_base) <= S390_ARCH_FAC_LIST_SIZE_U64);
255 static_assert(ARRAY_SIZE(kvm_s390_fac_base) <= ARRAY_SIZE(stfle_fac_list));
256 
257 /*
258  * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
259  * and defines the facilities that can be enabled via a cpu model.
260  */
261 static const unsigned long kvm_s390_fac_ext[] = { FACILITIES_KVM_CPUMODEL };
262 static_assert(ARRAY_SIZE(kvm_s390_fac_ext) <= S390_ARCH_FAC_MASK_SIZE_U64);
263 static_assert(ARRAY_SIZE(kvm_s390_fac_ext) <= S390_ARCH_FAC_LIST_SIZE_U64);
264 static_assert(ARRAY_SIZE(kvm_s390_fac_ext) <= ARRAY_SIZE(stfle_fac_list));
265 
266 /* available cpu features supported by kvm */
267 static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
268 /* available subfunctions indicated via query / "test bit" */
269 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
270 
271 debug_info_t *kvm_s390_dbf;
272 debug_info_t *kvm_s390_dbf_uv;
273 
274 /* Section: not file related */
275 /* forward declarations */
276 static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
277 {
278 	u8 delta_idx = 0;
279 
280 	/*
281 	 * The TOD jumps by delta, we have to compensate this by adding
282 	 * -delta to the epoch.
283 	 */
284 	delta = -delta;
285 
286 	/* sign-extension - we're adding to signed values below */
287 	if ((s64)delta < 0)
288 		delta_idx = -1;
289 
290 	scb->epoch += delta;
291 	if (scb->ecd & ECD_MEF) {
292 		scb->epdx += delta_idx;
293 		if (scb->epoch < delta)
294 			scb->epdx += 1;
295 	}
296 }
297 
298 /*
299  * This callback is executed during stop_machine(). All CPUs are therefore
300  * temporarily stopped. In order not to change guest behavior, we have to
301  * disable preemption whenever we touch the epoch of kvm and the VCPUs,
302  * so a CPU won't be stopped while calculating with the epoch.
303  */
304 static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
305 			  void *v)
306 {
307 	struct kvm *kvm;
308 	struct kvm_vcpu *vcpu;
309 	unsigned long i;
310 	unsigned long long *delta = v;
311 
312 	list_for_each_entry(kvm, &vm_list, vm_list) {
313 		kvm_for_each_vcpu(i, vcpu, kvm) {
314 			kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
315 			if (i == 0) {
316 				kvm->arch.epoch = vcpu->arch.sie_block->epoch;
317 				kvm->arch.epdx = vcpu->arch.sie_block->epdx;
318 			}
319 			if (vcpu->arch.cputm_enabled)
320 				vcpu->arch.cputm_start += *delta;
321 			if (vcpu->arch.vsie_block)
322 				kvm_clock_sync_scb(vcpu->arch.vsie_block,
323 						   *delta);
324 		}
325 	}
326 	return NOTIFY_OK;
327 }
328 
329 static struct notifier_block kvm_clock_notifier = {
330 	.notifier_call = kvm_clock_sync,
331 };
332 
333 static void allow_cpu_feat(unsigned long nr)
334 {
335 	set_bit_inv(nr, kvm_s390_available_cpu_feat);
336 }
337 
338 static inline int plo_test_bit(unsigned char nr)
339 {
340 	unsigned long function = (unsigned long)nr | 0x100;
341 	int cc;
342 
343 	asm volatile(
344 		"	lgr	0,%[function]\n"
345 		/* Parameter registers are ignored for "test bit" */
346 		"	plo	0,0,0,0(0)\n"
347 		CC_IPM(cc)
348 		: CC_OUT(cc, cc)
349 		: [function] "d" (function)
350 		: CC_CLOBBER_LIST("0"));
351 	return CC_TRANSFORM(cc) == 0;
352 }
353 
354 static __always_inline void pfcr_query(u8 (*query)[16])
355 {
356 	asm volatile(
357 		"	lghi	0,0\n"
358 		"	.insn   rsy,0xeb0000000016,0,0,%[query]"
359 		: [query] "=QS" (*query)
360 		:
361 		: "cc", "0");
362 }
363 
364 static __always_inline void __sortl_query(u8 (*query)[32])
365 {
366 	asm volatile(
367 		"	lghi	0,0\n"
368 		"	la	1,%[query]\n"
369 		/* Parameter registers are ignored */
370 		"	.insn	rre,0xb9380000,2,4"
371 		: [query] "=R" (*query)
372 		:
373 		: "cc", "0", "1");
374 }
375 
376 static __always_inline void __dfltcc_query(u8 (*query)[32])
377 {
378 	asm volatile(
379 		"	lghi	0,0\n"
380 		"	la	1,%[query]\n"
381 		/* Parameter registers are ignored */
382 		"	.insn	rrf,0xb9390000,2,4,6,0"
383 		: [query] "=R" (*query)
384 		:
385 		: "cc", "0", "1");
386 }
387 
388 static void __init kvm_s390_cpu_feat_init(void)
389 {
390 	int i;
391 
392 	for (i = 0; i < 256; ++i) {
393 		if (plo_test_bit(i))
394 			kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
395 	}
396 
397 	if (test_facility(28)) /* TOD-clock steering */
398 		ptff(kvm_s390_available_subfunc.ptff,
399 		     sizeof(kvm_s390_available_subfunc.ptff),
400 		     PTFF_QAF);
401 
402 	if (test_facility(17)) { /* MSA */
403 		__cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
404 			      kvm_s390_available_subfunc.kmac);
405 		__cpacf_query(CPACF_KMC, (cpacf_mask_t *)
406 			      kvm_s390_available_subfunc.kmc);
407 		__cpacf_query(CPACF_KM, (cpacf_mask_t *)
408 			      kvm_s390_available_subfunc.km);
409 		__cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
410 			      kvm_s390_available_subfunc.kimd);
411 		__cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
412 			      kvm_s390_available_subfunc.klmd);
413 	}
414 	if (test_facility(76)) /* MSA3 */
415 		__cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
416 			      kvm_s390_available_subfunc.pckmo);
417 	if (test_facility(77)) { /* MSA4 */
418 		__cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
419 			      kvm_s390_available_subfunc.kmctr);
420 		__cpacf_query(CPACF_KMF, (cpacf_mask_t *)
421 			      kvm_s390_available_subfunc.kmf);
422 		__cpacf_query(CPACF_KMO, (cpacf_mask_t *)
423 			      kvm_s390_available_subfunc.kmo);
424 		__cpacf_query(CPACF_PCC, (cpacf_mask_t *)
425 			      kvm_s390_available_subfunc.pcc);
426 	}
427 	if (test_facility(57)) /* MSA5 */
428 		__cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
429 			      kvm_s390_available_subfunc.ppno);
430 
431 	if (test_facility(146)) /* MSA8 */
432 		__cpacf_query(CPACF_KMA, (cpacf_mask_t *)
433 			      kvm_s390_available_subfunc.kma);
434 
435 	if (test_facility(155)) /* MSA9 */
436 		__cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
437 			      kvm_s390_available_subfunc.kdsa);
438 
439 	if (test_facility(150)) /* SORTL */
440 		__sortl_query(&kvm_s390_available_subfunc.sortl);
441 
442 	if (test_facility(151)) /* DFLTCC */
443 		__dfltcc_query(&kvm_s390_available_subfunc.dfltcc);
444 
445 	if (test_facility(201))	/* PFCR */
446 		pfcr_query(&kvm_s390_available_subfunc.pfcr);
447 
448 	if (machine_has_esop())
449 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
450 	/*
451 	 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
452 	 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
453 	 */
454 	if (!sclp.has_sief2 || !machine_has_esop() || !sclp.has_64bscao ||
455 	    !test_facility(3) || !nested)
456 		return;
457 	allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
458 	if (sclp.has_64bscao)
459 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
460 	if (sclp.has_siif)
461 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
462 	if (sclp.has_gpere)
463 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
464 	if (sclp.has_gsls)
465 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
466 	if (sclp.has_ib)
467 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
468 	if (sclp.has_cei)
469 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
470 	if (sclp.has_ibs)
471 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
472 	if (sclp.has_kss)
473 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
474 	if (sclp.has_astfleie2)
475 		allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ASTFLEIE2);
476 	/*
477 	 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
478 	 * all skey handling functions read/set the skey from the PGSTE
479 	 * instead of the real storage key.
480 	 *
481 	 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
482 	 * pages being detected as preserved although they are resident.
483 	 *
484 	 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
485 	 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
486 	 *
487 	 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
488 	 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
489 	 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
490 	 *
491 	 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
492 	 * cannot easily shadow the SCA because of the ipte lock.
493 	 */
494 }
495 
496 static int __init __kvm_s390_init(void)
497 {
498 	int rc = -ENOMEM;
499 
500 	kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
501 	if (!kvm_s390_dbf)
502 		return -ENOMEM;
503 
504 	kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
505 	if (!kvm_s390_dbf_uv)
506 		goto err_kvm_uv;
507 
508 	if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
509 	    debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
510 		goto err_debug_view;
511 
512 	kvm_s390_cpu_feat_init();
513 
514 	/* Register floating interrupt controller interface. */
515 	rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
516 	if (rc) {
517 		pr_err("A FLIC registration call failed with rc=%d\n", rc);
518 		goto err_flic;
519 	}
520 
521 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
522 		rc = kvm_s390_pci_init();
523 		if (rc) {
524 			pr_err("Unable to allocate AIFT for PCI\n");
525 			goto err_pci;
526 		}
527 	}
528 
529 	rc = kvm_s390_gib_init(GAL_ISC);
530 	if (rc)
531 		goto err_gib;
532 
533 	atomic_notifier_chain_register(&s390_epoch_delta_notifier,
534 				       &kvm_clock_notifier);
535 
536 	return 0;
537 
538 err_gib:
539 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
540 		kvm_s390_pci_exit();
541 err_pci:
542 err_flic:
543 err_debug_view:
544 	debug_unregister(kvm_s390_dbf_uv);
545 err_kvm_uv:
546 	debug_unregister(kvm_s390_dbf);
547 	return rc;
548 }
549 
550 static void __kvm_s390_exit(void)
551 {
552 	atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
553 					 &kvm_clock_notifier);
554 
555 	kvm_s390_gib_destroy();
556 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
557 		kvm_s390_pci_exit();
558 	debug_unregister(kvm_s390_dbf);
559 	debug_unregister(kvm_s390_dbf_uv);
560 }
561 
562 static int kvm_s390_keyop(struct kvm_s390_mmu_cache *mc, struct kvm *kvm, int op,
563 			  unsigned long addr, union skey skey)
564 {
565 	union asce asce = kvm->arch.gmap->asce;
566 	gfn_t gfn = gpa_to_gfn(addr);
567 	int r;
568 
569 	guard(read_lock)(&kvm->mmu_lock);
570 
571 	switch (op) {
572 	case KVM_S390_KEYOP_SSKE:
573 		r = dat_cond_set_storage_key(mc, asce, gfn, skey, &skey, 0, 0, 0);
574 		if (r >= 0)
575 			return skey.skey;
576 		break;
577 	case KVM_S390_KEYOP_ISKE:
578 		r = dat_get_storage_key(asce, gfn, &skey);
579 		if (!r)
580 			return skey.skey;
581 		break;
582 	case KVM_S390_KEYOP_RRBE:
583 		r = dat_reset_reference_bit(asce, gfn);
584 		if (r > 0)
585 			return r << 1;
586 		break;
587 	default:
588 		return -EINVAL;
589 	}
590 	return r;
591 }
592 
593 /* Section: device related */
594 long kvm_arch_dev_ioctl(struct file *filp,
595 			unsigned int ioctl, unsigned long arg)
596 {
597 	if (ioctl == KVM_S390_ENABLE_SIE)
598 		return 0;
599 	return -EINVAL;
600 }
601 
602 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
603 {
604 	int r;
605 
606 	switch (ext) {
607 	case KVM_CAP_S390_PSW:
608 	case KVM_CAP_S390_GMAP:
609 #ifdef CONFIG_KVM_S390_UCONTROL
610 	case KVM_CAP_S390_UCONTROL:
611 #endif
612 	case KVM_CAP_ASYNC_PF:
613 	case KVM_CAP_SYNC_REGS:
614 	case KVM_CAP_ONE_REG:
615 	case KVM_CAP_ENABLE_CAP:
616 	case KVM_CAP_S390_CSS_SUPPORT:
617 	case KVM_CAP_IOEVENTFD:
618 	case KVM_CAP_S390_IRQCHIP:
619 	case KVM_CAP_VM_ATTRIBUTES:
620 	case KVM_CAP_MP_STATE:
621 	case KVM_CAP_IMMEDIATE_EXIT:
622 	case KVM_CAP_S390_INJECT_IRQ:
623 	case KVM_CAP_S390_USER_SIGP:
624 	case KVM_CAP_S390_USER_STSI:
625 	case KVM_CAP_S390_SKEYS:
626 	case KVM_CAP_S390_IRQ_STATE:
627 	case KVM_CAP_S390_USER_INSTR0:
628 	case KVM_CAP_S390_CMMA_MIGRATION:
629 	case KVM_CAP_S390_AIS:
630 	case KVM_CAP_S390_AIS_MIGRATION:
631 	case KVM_CAP_S390_VCPU_RESETS:
632 	case KVM_CAP_SET_GUEST_DEBUG:
633 	case KVM_CAP_S390_DIAG318:
634 	case KVM_CAP_IRQFD_RESAMPLE:
635 	case KVM_CAP_S390_USER_OPEREXEC:
636 	case KVM_CAP_S390_KEYOP:
637 	case KVM_CAP_S390_VSIE_ESAMODE:
638 	case KVM_CAP_PRE_FAULT_MEMORY:
639 		r = 1;
640 		break;
641 	case KVM_CAP_SET_GUEST_DEBUG2:
642 		r = KVM_GUESTDBG_VALID_MASK;
643 		break;
644 	case KVM_CAP_S390_HPAGE_1M:
645 		r = 0;
646 		if (hpage && !(kvm && kvm_is_ucontrol(kvm)))
647 			r = 1;
648 		break;
649 	case KVM_CAP_S390_HPAGE_2G:
650 		r = 0;
651 		if (hpage_2g && !(kvm && kvm_is_ucontrol(kvm)))
652 			r = 1;
653 		break;
654 	case KVM_CAP_S390_MEM_OP:
655 		r = MEM_OP_MAX_SIZE;
656 		break;
657 	case KVM_CAP_S390_MEM_OP_EXTENSION:
658 		/*
659 		 * Flag bits indicating which extensions are supported.
660 		 * If r > 0, the base extension must also be supported/indicated,
661 		 * in order to maintain backwards compatibility.
662 		 */
663 		r = KVM_S390_MEMOP_EXTENSION_CAP_BASE |
664 		    KVM_S390_MEMOP_EXTENSION_CAP_CMPXCHG;
665 		break;
666 	case KVM_CAP_NR_VCPUS:
667 	case KVM_CAP_MAX_VCPUS:
668 	case KVM_CAP_MAX_VCPU_ID:
669 		/*
670 		 * Return the same value for KVM_CAP_MAX_VCPUS and
671 		 * KVM_CAP_MAX_VCPU_ID to conform with the KVM API.
672 		 */
673 		r = KVM_S390_ESCA_CPU_SLOTS;
674 		if (!kvm_s390_use_sca_entries())
675 			r = KVM_MAX_VCPUS;
676 		if (ext == KVM_CAP_NR_VCPUS)
677 			r = min_t(unsigned int, num_online_cpus(), r);
678 		break;
679 	case KVM_CAP_S390_COW:
680 		r = machine_has_esop();
681 		break;
682 	case KVM_CAP_S390_VECTOR_REGISTERS:
683 		r = test_facility(129);
684 		break;
685 	case KVM_CAP_S390_RI:
686 		r = test_facility(64);
687 		break;
688 	case KVM_CAP_S390_GS:
689 		r = test_facility(133);
690 		break;
691 	case KVM_CAP_S390_BPB:
692 		r = test_facility(82);
693 		break;
694 	case KVM_CAP_S390_PROTECTED_ASYNC_DISABLE:
695 		r = async_destroy && is_prot_virt_host();
696 		break;
697 	case KVM_CAP_S390_PROTECTED:
698 		r = is_prot_virt_host();
699 		break;
700 	case KVM_CAP_S390_PROTECTED_DUMP: {
701 		u64 pv_cmds_dump[] = {
702 			BIT_UVC_CMD_DUMP_INIT,
703 			BIT_UVC_CMD_DUMP_CONFIG_STOR_STATE,
704 			BIT_UVC_CMD_DUMP_CPU,
705 			BIT_UVC_CMD_DUMP_COMPLETE,
706 		};
707 		int i;
708 
709 		r = is_prot_virt_host();
710 
711 		for (i = 0; i < ARRAY_SIZE(pv_cmds_dump); i++) {
712 			if (!test_bit_inv(pv_cmds_dump[i],
713 					  (unsigned long *)&uv_info.inst_calls_list)) {
714 				r = 0;
715 				break;
716 			}
717 		}
718 		break;
719 	}
720 	case KVM_CAP_S390_ZPCI_OP:
721 		r = kvm_s390_pci_interp_allowed();
722 		break;
723 	case KVM_CAP_S390_CPU_TOPOLOGY:
724 		r = test_facility(11);
725 		break;
726 	default:
727 		r = 0;
728 	}
729 	return r;
730 }
731 
732 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
733 {
734 	gfn_t last_gfn = memslot->base_gfn + memslot->npages;
735 
736 	scoped_guard(read_lock, &kvm->mmu_lock)
737 		gmap_sync_dirty_log(kvm->arch.gmap, memslot->base_gfn, last_gfn);
738 }
739 
740 /* Section: vm related */
741 static void sca_del_vcpu(struct kvm_vcpu *vcpu);
742 
743 /*
744  * Get (and clear) the dirty memory log for a memory slot.
745  */
746 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
747 			       struct kvm_dirty_log *log)
748 {
749 	int r;
750 	unsigned long n;
751 	struct kvm_memory_slot *memslot;
752 	int is_dirty;
753 
754 	if (kvm_is_ucontrol(kvm))
755 		return -EINVAL;
756 
757 	mutex_lock(&kvm->slots_lock);
758 
759 	r = -EINVAL;
760 	if (log->slot >= KVM_USER_MEM_SLOTS)
761 		goto out;
762 
763 	r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
764 	if (r)
765 		goto out;
766 
767 	/* Clear the dirty log */
768 	if (is_dirty) {
769 		n = kvm_dirty_bitmap_bytes(memslot);
770 		memset(memslot->dirty_bitmap, 0, n);
771 	}
772 	r = 0;
773 out:
774 	mutex_unlock(&kvm->slots_lock);
775 	return r;
776 }
777 
778 static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
779 {
780 	unsigned long i;
781 	struct kvm_vcpu *vcpu;
782 
783 	kvm_for_each_vcpu(i, vcpu, kvm) {
784 		kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
785 	}
786 }
787 
788 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
789 {
790 	int r;
791 
792 	if (cap->flags)
793 		return -EINVAL;
794 
795 	switch (cap->cap) {
796 	case KVM_CAP_S390_IRQCHIP:
797 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
798 		kvm->arch.use_irqchip = 1;
799 		r = 0;
800 		break;
801 	case KVM_CAP_S390_USER_SIGP:
802 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
803 		kvm->arch.user_sigp = 1;
804 		r = 0;
805 		break;
806 	case KVM_CAP_S390_VECTOR_REGISTERS:
807 		mutex_lock(&kvm->lock);
808 		if (kvm->created_vcpus) {
809 			r = -EBUSY;
810 		} else if (cpu_has_vx()) {
811 			set_kvm_facility(kvm->arch.model.fac_mask, 129);
812 			set_kvm_facility(kvm->arch.model.fac_list, 129);
813 			if (test_facility(134)) {
814 				set_kvm_facility(kvm->arch.model.fac_mask, 134);
815 				set_kvm_facility(kvm->arch.model.fac_list, 134);
816 			}
817 			if (test_facility(135)) {
818 				set_kvm_facility(kvm->arch.model.fac_mask, 135);
819 				set_kvm_facility(kvm->arch.model.fac_list, 135);
820 			}
821 			if (test_facility(148)) {
822 				set_kvm_facility(kvm->arch.model.fac_mask, 148);
823 				set_kvm_facility(kvm->arch.model.fac_list, 148);
824 			}
825 			if (test_facility(152)) {
826 				set_kvm_facility(kvm->arch.model.fac_mask, 152);
827 				set_kvm_facility(kvm->arch.model.fac_list, 152);
828 			}
829 			if (test_facility(192)) {
830 				set_kvm_facility(kvm->arch.model.fac_mask, 192);
831 				set_kvm_facility(kvm->arch.model.fac_list, 192);
832 			}
833 			if (test_facility(198)) {
834 				set_kvm_facility(kvm->arch.model.fac_mask, 198);
835 				set_kvm_facility(kvm->arch.model.fac_list, 198);
836 			}
837 			if (test_facility(199)) {
838 				set_kvm_facility(kvm->arch.model.fac_mask, 199);
839 				set_kvm_facility(kvm->arch.model.fac_list, 199);
840 			}
841 			r = 0;
842 		} else
843 			r = -EINVAL;
844 		mutex_unlock(&kvm->lock);
845 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
846 			 r ? "(not available)" : "(success)");
847 		break;
848 	case KVM_CAP_S390_RI:
849 		r = -EINVAL;
850 		mutex_lock(&kvm->lock);
851 		if (kvm->created_vcpus) {
852 			r = -EBUSY;
853 		} else if (test_facility(64)) {
854 			set_kvm_facility(kvm->arch.model.fac_mask, 64);
855 			set_kvm_facility(kvm->arch.model.fac_list, 64);
856 			r = 0;
857 		}
858 		mutex_unlock(&kvm->lock);
859 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
860 			 r ? "(not available)" : "(success)");
861 		break;
862 	case KVM_CAP_S390_AIS:
863 		mutex_lock(&kvm->lock);
864 		if (kvm->created_vcpus) {
865 			r = -EBUSY;
866 		} else {
867 			set_kvm_facility(kvm->arch.model.fac_mask, 72);
868 			set_kvm_facility(kvm->arch.model.fac_list, 72);
869 			r = 0;
870 		}
871 		mutex_unlock(&kvm->lock);
872 		VM_EVENT(kvm, 3, "ENABLE: AIS %s",
873 			 r ? "(not available)" : "(success)");
874 		break;
875 	case KVM_CAP_S390_GS:
876 		r = -EINVAL;
877 		mutex_lock(&kvm->lock);
878 		if (kvm->created_vcpus) {
879 			r = -EBUSY;
880 		} else if (test_facility(133)) {
881 			set_kvm_facility(kvm->arch.model.fac_mask, 133);
882 			set_kvm_facility(kvm->arch.model.fac_list, 133);
883 			r = 0;
884 		}
885 		mutex_unlock(&kvm->lock);
886 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
887 			 r ? "(not available)" : "(success)");
888 		break;
889 	case KVM_CAP_S390_HPAGE_1M:
890 		mutex_lock(&kvm->lock);
891 		if (kvm->created_vcpus)
892 			r = -EBUSY;
893 		else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
894 			r = -EINVAL;
895 		else {
896 			r = 0;
897 			set_bit(GMAP_FLAG_ALLOW_HPAGE_1M, &kvm->arch.gmap->flags);
898 			/*
899 			 * We might have to create fake 4k page
900 			 * tables. To avoid that the hardware works on
901 			 * stale PGSTEs, we emulate these instructions.
902 			 */
903 			kvm->arch.use_skf = 0;
904 			kvm->arch.use_pfmfi = 0;
905 		}
906 		mutex_unlock(&kvm->lock);
907 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
908 			 r ? "(not available)" : "(success)");
909 		break;
910 	case KVM_CAP_S390_HPAGE_2G:
911 		mutex_lock(&kvm->lock);
912 		if (kvm->created_vcpus) {
913 			r = -EBUSY;
914 		} else if (!hpage_2g || kvm->arch.use_cmma || kvm_is_ucontrol(kvm)) {
915 			r = -EINVAL;
916 		} else {
917 			r = 0;
918 			set_bit(GMAP_FLAG_ALLOW_HPAGE_2G, &kvm->arch.gmap->flags);
919 			/*
920 			 * We might have to create fake 4k page
921 			 * tables. To avoid that the hardware works on
922 			 * stale PGSTEs, we emulate these instructions.
923 			 */
924 			kvm->arch.use_skf = 0;
925 			kvm->arch.use_pfmfi = 0;
926 		}
927 		mutex_unlock(&kvm->lock);
928 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE_2G %s",
929 			 r ? "(not available)" : "(success)");
930 		break;
931 	case KVM_CAP_S390_USER_STSI:
932 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
933 		kvm->arch.user_stsi = 1;
934 		r = 0;
935 		break;
936 	case KVM_CAP_S390_USER_INSTR0:
937 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
938 		kvm->arch.user_instr0 = 1;
939 		icpt_operexc_on_all_vcpus(kvm);
940 		r = 0;
941 		break;
942 	case KVM_CAP_S390_CPU_TOPOLOGY:
943 		r = -EINVAL;
944 		mutex_lock(&kvm->lock);
945 		if (kvm->created_vcpus) {
946 			r = -EBUSY;
947 		} else if (test_facility(11)) {
948 			set_kvm_facility(kvm->arch.model.fac_mask, 11);
949 			set_kvm_facility(kvm->arch.model.fac_list, 11);
950 			r = 0;
951 		}
952 		mutex_unlock(&kvm->lock);
953 		VM_EVENT(kvm, 3, "ENABLE: CAP_S390_CPU_TOPOLOGY %s",
954 			 r ? "(not available)" : "(success)");
955 		break;
956 	case KVM_CAP_S390_USER_OPEREXEC:
957 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_OPEREXEC");
958 		kvm->arch.user_operexec = 1;
959 		icpt_operexc_on_all_vcpus(kvm);
960 		r = 0;
961 		break;
962 	case KVM_CAP_S390_VSIE_ESAMODE:
963 		VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_VSIE_ESAMODE");
964 		kvm->arch.allow_vsie_esamode = 1;
965 		r = 0;
966 		break;
967 	default:
968 		r = -EINVAL;
969 		break;
970 	}
971 	return r;
972 }
973 
974 static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
975 {
976 	int ret;
977 
978 	switch (attr->attr) {
979 	case KVM_S390_VM_MEM_LIMIT_SIZE:
980 		ret = 0;
981 		VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
982 			 kvm->arch.mem_limit);
983 		if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
984 			ret = -EFAULT;
985 		break;
986 	default:
987 		ret = -ENXIO;
988 		break;
989 	}
990 	return ret;
991 }
992 
993 static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
994 {
995 	int ret;
996 
997 	switch (attr->attr) {
998 	case KVM_S390_VM_MEM_ENABLE_CMMA:
999 		ret = -ENXIO;
1000 		if (!sclp.has_cmma)
1001 			break;
1002 
1003 		VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
1004 		mutex_lock(&kvm->lock);
1005 		if (kvm->created_vcpus)
1006 			ret = -EBUSY;
1007 		else {
1008 			kvm->arch.use_cmma = 1;
1009 			/* Not compatible with cmma. */
1010 			kvm->arch.use_pfmfi = 0;
1011 			ret = 0;
1012 		}
1013 		mutex_unlock(&kvm->lock);
1014 		break;
1015 	case KVM_S390_VM_MEM_CLR_CMMA: {
1016 		gfn_t start_gfn = 0;
1017 
1018 		ret = -ENXIO;
1019 		if (!sclp.has_cmma)
1020 			break;
1021 		ret = -EINVAL;
1022 		if (!kvm->arch.use_cmma)
1023 			break;
1024 
1025 		guard(mutex)(&kvm->lock);
1026 		VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
1027 		do {
1028 			scoped_guard(read_lock, &kvm->mmu_lock)
1029 				start_gfn = dat_reset_cmma(kvm->arch.gmap->asce, start_gfn);
1030 			cond_resched();
1031 		} while (start_gfn);
1032 		ret = 0;
1033 		break;
1034 	}
1035 	case KVM_S390_VM_MEM_LIMIT_SIZE: {
1036 		struct kvm_memslots *slots;
1037 		struct kvm_memory_slot *ms;
1038 		unsigned long new_limit;
1039 		int bkt;
1040 
1041 		if (kvm_is_ucontrol(kvm))
1042 			return -EINVAL;
1043 
1044 		if (get_user(new_limit, (u64 __user *)attr->addr))
1045 			return -EFAULT;
1046 
1047 		guard(mutex)(&kvm->lock);
1048 
1049 		new_limit = ALIGN(new_limit, HPAGE_SIZE);
1050 		if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
1051 		    new_limit > kvm->arch.mem_limit)
1052 			return -E2BIG;
1053 
1054 		if (!new_limit)
1055 			return -EINVAL;
1056 
1057 		if (kvm->created_vcpus)
1058 			return -EBUSY;
1059 
1060 		ret = 0;
1061 		scoped_guard(mutex, &kvm->slots_lock) {
1062 			slots = kvm_memslots(kvm);
1063 			if (slots && !kvm_memslots_empty(slots)) {
1064 				kvm_for_each_memslot(ms, bkt, slots) {
1065 					if (gpa_to_gfn(new_limit) < ms->base_gfn + ms->npages) {
1066 						ret = -EBUSY;
1067 						break;
1068 					}
1069 				}
1070 			}
1071 			if (!ret)
1072 				ret = gmap_set_limit(kvm->arch.gmap, gpa_to_gfn(new_limit));
1073 		}
1074 		if (ret)
1075 			break;
1076 		VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
1077 		VM_EVENT(kvm, 3, "New guest asce: 0x%p", (void *)kvm->arch.gmap->asce.val);
1078 		break;
1079 	}
1080 	default:
1081 		ret = -ENXIO;
1082 		break;
1083 	}
1084 	return ret;
1085 }
1086 
1087 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
1088 
1089 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
1090 {
1091 	struct kvm_vcpu *vcpu;
1092 	unsigned long i;
1093 
1094 	kvm_s390_vcpu_block_all(kvm);
1095 
1096 	kvm_for_each_vcpu(i, vcpu, kvm) {
1097 		kvm_s390_vcpu_crypto_setup(vcpu);
1098 		/* recreate the shadow crycb by leaving the VSIE handler */
1099 		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
1100 	}
1101 
1102 	kvm_s390_vcpu_unblock_all(kvm);
1103 }
1104 
1105 static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
1106 {
1107 	mutex_lock(&kvm->lock);
1108 	switch (attr->attr) {
1109 	case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1110 		if (!test_kvm_facility(kvm, 76)) {
1111 			mutex_unlock(&kvm->lock);
1112 			return -EINVAL;
1113 		}
1114 		get_random_bytes(
1115 			kvm->arch.crypto.crycb->aes_wrapping_key_mask,
1116 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1117 		kvm->arch.crypto.aes_kw = 1;
1118 		VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
1119 		break;
1120 	case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1121 		if (!test_kvm_facility(kvm, 76)) {
1122 			mutex_unlock(&kvm->lock);
1123 			return -EINVAL;
1124 		}
1125 		get_random_bytes(
1126 			kvm->arch.crypto.crycb->dea_wrapping_key_mask,
1127 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1128 		kvm->arch.crypto.dea_kw = 1;
1129 		VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
1130 		break;
1131 	case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1132 		if (!test_kvm_facility(kvm, 76)) {
1133 			mutex_unlock(&kvm->lock);
1134 			return -EINVAL;
1135 		}
1136 		kvm->arch.crypto.aes_kw = 0;
1137 		memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
1138 			sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
1139 		VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
1140 		break;
1141 	case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1142 		if (!test_kvm_facility(kvm, 76)) {
1143 			mutex_unlock(&kvm->lock);
1144 			return -EINVAL;
1145 		}
1146 		kvm->arch.crypto.dea_kw = 0;
1147 		memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
1148 			sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
1149 		VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
1150 		break;
1151 	case KVM_S390_VM_CRYPTO_ENABLE_APIE:
1152 		if (!ap_instructions_available()) {
1153 			mutex_unlock(&kvm->lock);
1154 			return -EOPNOTSUPP;
1155 		}
1156 		kvm->arch.crypto.apie = 1;
1157 		break;
1158 	case KVM_S390_VM_CRYPTO_DISABLE_APIE:
1159 		if (!ap_instructions_available()) {
1160 			mutex_unlock(&kvm->lock);
1161 			return -EOPNOTSUPP;
1162 		}
1163 		kvm->arch.crypto.apie = 0;
1164 		break;
1165 	default:
1166 		mutex_unlock(&kvm->lock);
1167 		return -ENXIO;
1168 	}
1169 
1170 	kvm_s390_vcpu_crypto_reset_all(kvm);
1171 	mutex_unlock(&kvm->lock);
1172 	return 0;
1173 }
1174 
1175 static void kvm_s390_vcpu_pci_setup(struct kvm_vcpu *vcpu)
1176 {
1177 	/* Only set the ECB bits after guest requests zPCI interpretation */
1178 	if (!vcpu->kvm->arch.use_zpci_interp)
1179 		return;
1180 
1181 	vcpu->arch.sie_block->ecb2 |= ECB2_ZPCI_LSI;
1182 	vcpu->arch.sie_block->ecb3 |= ECB3_AISII + ECB3_AISI;
1183 }
1184 
1185 void kvm_s390_vcpu_pci_enable_interp(struct kvm *kvm)
1186 {
1187 	struct kvm_vcpu *vcpu;
1188 	unsigned long i;
1189 
1190 	lockdep_assert_held(&kvm->lock);
1191 
1192 	if (!kvm_s390_pci_interp_allowed())
1193 		return;
1194 
1195 	/*
1196 	 * If host is configured for PCI and the necessary facilities are
1197 	 * available, turn on interpretation for the life of this guest
1198 	 */
1199 	kvm->arch.use_zpci_interp = 1;
1200 
1201 	kvm_s390_vcpu_block_all(kvm);
1202 
1203 	kvm_for_each_vcpu(i, vcpu, kvm) {
1204 		kvm_s390_vcpu_pci_setup(vcpu);
1205 		kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
1206 	}
1207 
1208 	kvm_s390_vcpu_unblock_all(kvm);
1209 }
1210 
1211 static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
1212 {
1213 	unsigned long cx;
1214 	struct kvm_vcpu *vcpu;
1215 
1216 	kvm_for_each_vcpu(cx, vcpu, kvm)
1217 		kvm_s390_sync_request(req, vcpu);
1218 }
1219 
1220 /*
1221  * Must be called with kvm->srcu held to avoid races on memslots, and with
1222  * kvm->slots_lock to avoid races with ourselves, kvm_s390_vm_stop_migration(),
1223  * and kvm_s390_get_cmma_bits().
1224  */
1225 static int kvm_s390_vm_start_migration(struct kvm *kvm)
1226 {
1227 	struct kvm_memory_slot *ms;
1228 	struct kvm_memslots *slots;
1229 	int bkt;
1230 
1231 	/* migration mode already enabled */
1232 	if (kvm->arch.migration_mode)
1233 		return 0;
1234 	slots = kvm_memslots(kvm);
1235 	if (!slots || kvm_memslots_empty(slots))
1236 		return -EINVAL;
1237 
1238 	if (!kvm->arch.use_cmma) {
1239 		kvm->arch.migration_mode = 1;
1240 		return 0;
1241 	}
1242 	kvm_for_each_memslot(ms, bkt, slots) {
1243 		if (!ms->dirty_bitmap)
1244 			return -EINVAL;
1245 	}
1246 	/*
1247 	 * Set the flag and let KVM handle ESSA manually, potentially setting
1248 	 * the cmma_d bit in some PGSTEs and increasing cmma_dirty_pages.
1249 	 * At this point cmma_dirty_pages is still 0, and all existing PGSTEs
1250 	 * have their cmma_d bit set to 0.
1251 	 * Any newly allocated page table has its entries marked as cmma-clean,
1252 	 * which is fine because the CMMA values are not dirty.
1253 	 */
1254 	WRITE_ONCE(kvm->arch.migration_mode, 1);
1255 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1256 	/*
1257 	 * Mark all PGSTEs as cmma-dirty, increasing cmma_dirty_pages as needed,
1258 	 * but without double-counting pages that have become dirty on their own
1259 	 * in the meantime.
1260 	 * At this point some pages might have become dirty on their own already
1261 	 * and cmma_dirty_pages might therefore be non-zero.
1262 	 */
1263 	gmap_set_cmma_all_dirty(kvm->arch.gmap);
1264 	return 0;
1265 }
1266 
1267 /*
1268  * Must be called with kvm->slots_lock to avoid races with ourselves,
1269  * kvm_s390_vm_start_migration() and kvm_s390_get_cmma_bits().
1270  */
1271 static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1272 {
1273 	/* migration mode already disabled */
1274 	if (!kvm->arch.migration_mode)
1275 		return 0;
1276 	/*
1277 	 * Unset the flag and propagate to all vCPUs. From now on the cmma_d
1278 	 * bit will not be touched on any PGSTE.
1279 	 * At this point cmma_dirty_pages is possibly non-zero, and thus some
1280 	 * PGSTEs might have cmma_d set.
1281 	 */
1282 	WRITE_ONCE(kvm->arch.migration_mode, 0);
1283 	if (kvm->arch.use_cmma)
1284 		kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1285 	/* Clear cmma_d on all existing PGSTEs and set cmma_dirty_pages to 0. */
1286 	gmap_set_cmma_all_clean(kvm->arch.gmap);
1287 	atomic64_set(&kvm->arch.cmma_dirty_pages, 0);
1288 	/*
1289 	 * At this point the system has the expected state: migration_mode is 0,
1290 	 * cmma_dirty_pages is 0, and all existing PGSTEs have their cmma_d bit
1291 	 * set to 0.
1292 	 */
1293 	return 0;
1294 }
1295 
1296 static int kvm_s390_vm_set_migration(struct kvm *kvm,
1297 				     struct kvm_device_attr *attr)
1298 {
1299 	int res = -ENXIO;
1300 
1301 	mutex_lock(&kvm->slots_lock);
1302 	switch (attr->attr) {
1303 	case KVM_S390_VM_MIGRATION_START:
1304 		res = kvm_s390_vm_start_migration(kvm);
1305 		break;
1306 	case KVM_S390_VM_MIGRATION_STOP:
1307 		res = kvm_s390_vm_stop_migration(kvm);
1308 		break;
1309 	default:
1310 		break;
1311 	}
1312 	mutex_unlock(&kvm->slots_lock);
1313 
1314 	return res;
1315 }
1316 
1317 static int kvm_s390_vm_get_migration(struct kvm *kvm,
1318 				     struct kvm_device_attr *attr)
1319 {
1320 	u64 mig = kvm->arch.migration_mode;
1321 
1322 	if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1323 		return -ENXIO;
1324 
1325 	if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1326 		return -EFAULT;
1327 	return 0;
1328 }
1329 
1330 static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod);
1331 
1332 static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1333 {
1334 	struct kvm_s390_vm_tod_clock gtod;
1335 
1336 	if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
1337 		return -EFAULT;
1338 
1339 	if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
1340 		return -EINVAL;
1341 	__kvm_s390_set_tod_clock(kvm, &gtod);
1342 
1343 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
1344 		gtod.epoch_idx, gtod.tod);
1345 
1346 	return 0;
1347 }
1348 
1349 static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1350 {
1351 	u8 gtod_high;
1352 
1353 	if (copy_from_user(&gtod_high, (void __user *)attr->addr,
1354 					   sizeof(gtod_high)))
1355 		return -EFAULT;
1356 
1357 	if (gtod_high != 0)
1358 		return -EINVAL;
1359 	VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
1360 
1361 	return 0;
1362 }
1363 
1364 static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1365 {
1366 	struct kvm_s390_vm_tod_clock gtod = { 0 };
1367 
1368 	if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
1369 			   sizeof(gtod.tod)))
1370 		return -EFAULT;
1371 
1372 	__kvm_s390_set_tod_clock(kvm, &gtod);
1373 	VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
1374 	return 0;
1375 }
1376 
1377 static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1378 {
1379 	int ret;
1380 
1381 	if (attr->flags)
1382 		return -EINVAL;
1383 
1384 	mutex_lock(&kvm->lock);
1385 	/*
1386 	 * For protected guests, the TOD is managed by the ultravisor, so trying
1387 	 * to change it will never bring the expected results.
1388 	 */
1389 	if (kvm_s390_pv_is_protected(kvm)) {
1390 		ret = -EOPNOTSUPP;
1391 		goto out_unlock;
1392 	}
1393 
1394 	switch (attr->attr) {
1395 	case KVM_S390_VM_TOD_EXT:
1396 		ret = kvm_s390_set_tod_ext(kvm, attr);
1397 		break;
1398 	case KVM_S390_VM_TOD_HIGH:
1399 		ret = kvm_s390_set_tod_high(kvm, attr);
1400 		break;
1401 	case KVM_S390_VM_TOD_LOW:
1402 		ret = kvm_s390_set_tod_low(kvm, attr);
1403 		break;
1404 	default:
1405 		ret = -ENXIO;
1406 		break;
1407 	}
1408 
1409 out_unlock:
1410 	mutex_unlock(&kvm->lock);
1411 	return ret;
1412 }
1413 
1414 static void kvm_s390_get_tod_clock(struct kvm *kvm,
1415 				   struct kvm_s390_vm_tod_clock *gtod)
1416 {
1417 	union tod_clock clk;
1418 
1419 	preempt_disable();
1420 
1421 	store_tod_clock_ext(&clk);
1422 
1423 	gtod->tod = clk.tod + kvm->arch.epoch;
1424 	gtod->epoch_idx = 0;
1425 	if (test_kvm_facility(kvm, 139)) {
1426 		gtod->epoch_idx = clk.ei + kvm->arch.epdx;
1427 		if (gtod->tod < clk.tod)
1428 			gtod->epoch_idx += 1;
1429 	}
1430 
1431 	preempt_enable();
1432 }
1433 
1434 static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1435 {
1436 	struct kvm_s390_vm_tod_clock gtod;
1437 
1438 	memset(&gtod, 0, sizeof(gtod));
1439 	kvm_s390_get_tod_clock(kvm, &gtod);
1440 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
1441 		return -EFAULT;
1442 
1443 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1444 		gtod.epoch_idx, gtod.tod);
1445 	return 0;
1446 }
1447 
1448 static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1449 {
1450 	u8 gtod_high = 0;
1451 
1452 	if (copy_to_user((void __user *)attr->addr, &gtod_high,
1453 					 sizeof(gtod_high)))
1454 		return -EFAULT;
1455 	VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
1456 
1457 	return 0;
1458 }
1459 
1460 static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1461 {
1462 	u64 gtod;
1463 
1464 	gtod = kvm_s390_get_tod_clock_fast(kvm);
1465 	if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
1466 		return -EFAULT;
1467 	VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
1468 
1469 	return 0;
1470 }
1471 
1472 static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1473 {
1474 	int ret;
1475 
1476 	if (attr->flags)
1477 		return -EINVAL;
1478 
1479 	switch (attr->attr) {
1480 	case KVM_S390_VM_TOD_EXT:
1481 		ret = kvm_s390_get_tod_ext(kvm, attr);
1482 		break;
1483 	case KVM_S390_VM_TOD_HIGH:
1484 		ret = kvm_s390_get_tod_high(kvm, attr);
1485 		break;
1486 	case KVM_S390_VM_TOD_LOW:
1487 		ret = kvm_s390_get_tod_low(kvm, attr);
1488 		break;
1489 	default:
1490 		ret = -ENXIO;
1491 		break;
1492 	}
1493 	return ret;
1494 }
1495 
1496 static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1497 {
1498 	struct kvm_s390_vm_cpu_processor *proc;
1499 	u16 lowest_ibc, unblocked_ibc;
1500 	int ret = 0;
1501 
1502 	mutex_lock(&kvm->lock);
1503 	if (kvm->created_vcpus) {
1504 		ret = -EBUSY;
1505 		goto out;
1506 	}
1507 	proc = kzalloc_obj(*proc, GFP_KERNEL_ACCOUNT);
1508 	if (!proc) {
1509 		ret = -ENOMEM;
1510 		goto out;
1511 	}
1512 	if (!copy_from_user(proc, (void __user *)attr->addr,
1513 			    sizeof(*proc))) {
1514 		kvm->arch.model.cpuid = proc->cpuid;
1515 		lowest_ibc = sclp.ibc >> 16 & 0xfff;
1516 		unblocked_ibc = sclp.ibc & 0xfff;
1517 		if (lowest_ibc && proc->ibc) {
1518 			if (proc->ibc > unblocked_ibc)
1519 				kvm->arch.model.ibc = unblocked_ibc;
1520 			else if (proc->ibc < lowest_ibc)
1521 				kvm->arch.model.ibc = lowest_ibc;
1522 			else
1523 				kvm->arch.model.ibc = proc->ibc;
1524 		}
1525 		memcpy(kvm->arch.model.fac_list, proc->fac_list,
1526 		       S390_ARCH_FAC_LIST_SIZE_BYTE);
1527 		VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1528 			 kvm->arch.model.ibc,
1529 			 kvm->arch.model.cpuid);
1530 		VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1531 			 kvm->arch.model.fac_list[0],
1532 			 kvm->arch.model.fac_list[1],
1533 			 kvm->arch.model.fac_list[2]);
1534 	} else
1535 		ret = -EFAULT;
1536 	kfree(proc);
1537 out:
1538 	mutex_unlock(&kvm->lock);
1539 	return ret;
1540 }
1541 
1542 static int kvm_s390_set_processor_feat(struct kvm *kvm,
1543 				       struct kvm_device_attr *attr)
1544 {
1545 	struct kvm_s390_vm_cpu_feat data;
1546 
1547 	if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
1548 		return -EFAULT;
1549 	if (!bitmap_subset((unsigned long *) data.feat,
1550 			   kvm_s390_available_cpu_feat,
1551 			   KVM_S390_VM_CPU_FEAT_NR_BITS))
1552 		return -EINVAL;
1553 
1554 	mutex_lock(&kvm->lock);
1555 	if (kvm->created_vcpus) {
1556 		mutex_unlock(&kvm->lock);
1557 		return -EBUSY;
1558 	}
1559 	bitmap_from_arr64(kvm->arch.cpu_feat, data.feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1560 	mutex_unlock(&kvm->lock);
1561 	VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1562 			 data.feat[0],
1563 			 data.feat[1],
1564 			 data.feat[2]);
1565 	return 0;
1566 }
1567 
1568 static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
1569 					  struct kvm_device_attr *attr)
1570 {
1571 	mutex_lock(&kvm->lock);
1572 	if (kvm->created_vcpus) {
1573 		mutex_unlock(&kvm->lock);
1574 		return -EBUSY;
1575 	}
1576 
1577 	if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1578 			   sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1579 		mutex_unlock(&kvm->lock);
1580 		return -EFAULT;
1581 	}
1582 	mutex_unlock(&kvm->lock);
1583 
1584 	VM_EVENT(kvm, 3, "SET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1585 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1586 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1587 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1588 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1589 	VM_EVENT(kvm, 3, "SET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
1590 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1591 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1592 	VM_EVENT(kvm, 3, "SET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
1593 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1594 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1595 	VM_EVENT(kvm, 3, "SET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
1596 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1597 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1598 	VM_EVENT(kvm, 3, "SET: guest KM     subfunc 0x%16.16lx.%16.16lx",
1599 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1600 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1601 	VM_EVENT(kvm, 3, "SET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
1602 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1603 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1604 	VM_EVENT(kvm, 3, "SET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
1605 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1606 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1607 	VM_EVENT(kvm, 3, "SET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
1608 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1609 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1610 	VM_EVENT(kvm, 3, "SET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
1611 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1612 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1613 	VM_EVENT(kvm, 3, "SET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
1614 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1615 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1616 	VM_EVENT(kvm, 3, "SET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
1617 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1618 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1619 	VM_EVENT(kvm, 3, "SET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
1620 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1621 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1622 	VM_EVENT(kvm, 3, "SET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
1623 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1624 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1625 	VM_EVENT(kvm, 3, "SET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
1626 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1627 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1628 	VM_EVENT(kvm, 3, "SET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
1629 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1630 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1631 	VM_EVENT(kvm, 3, "SET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1632 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1633 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1634 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1635 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1636 	VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1637 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1638 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1639 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1640 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1641 	VM_EVENT(kvm, 3, "GET: guest PFCR   subfunc 0x%16.16lx.%16.16lx",
1642 		 ((unsigned long *) &kvm_s390_available_subfunc.pfcr)[0],
1643 		 ((unsigned long *) &kvm_s390_available_subfunc.pfcr)[1]);
1644 
1645 	return 0;
1646 }
1647 
1648 #define KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK	\
1649 (						\
1650 	((struct kvm_s390_vm_cpu_uv_feat){	\
1651 		.ap = 1,			\
1652 		.ap_intr = 1,			\
1653 	})					\
1654 	.feat					\
1655 )
1656 
1657 static int kvm_s390_set_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
1658 {
1659 	struct kvm_s390_vm_cpu_uv_feat __user *ptr = (void __user *)attr->addr;
1660 	unsigned long data, filter;
1661 
1662 	filter = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
1663 	if (get_user(data, &ptr->feat))
1664 		return -EFAULT;
1665 	if (!bitmap_subset(&data, &filter, KVM_S390_VM_CPU_UV_FEAT_NR_BITS))
1666 		return -EINVAL;
1667 
1668 	mutex_lock(&kvm->lock);
1669 	if (kvm->created_vcpus) {
1670 		mutex_unlock(&kvm->lock);
1671 		return -EBUSY;
1672 	}
1673 	kvm->arch.model.uv_feat_guest.feat = data;
1674 	mutex_unlock(&kvm->lock);
1675 
1676 	VM_EVENT(kvm, 3, "SET: guest UV-feat: 0x%16.16lx", data);
1677 
1678 	return 0;
1679 }
1680 
1681 static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1682 {
1683 	int ret = -ENXIO;
1684 
1685 	switch (attr->attr) {
1686 	case KVM_S390_VM_CPU_PROCESSOR:
1687 		ret = kvm_s390_set_processor(kvm, attr);
1688 		break;
1689 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1690 		ret = kvm_s390_set_processor_feat(kvm, attr);
1691 		break;
1692 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1693 		ret = kvm_s390_set_processor_subfunc(kvm, attr);
1694 		break;
1695 	case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
1696 		ret = kvm_s390_set_uv_feat(kvm, attr);
1697 		break;
1698 	}
1699 	return ret;
1700 }
1701 
1702 static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1703 {
1704 	struct kvm_s390_vm_cpu_processor *proc;
1705 	int ret = 0;
1706 
1707 	proc = kzalloc_obj(*proc, GFP_KERNEL_ACCOUNT);
1708 	if (!proc) {
1709 		ret = -ENOMEM;
1710 		goto out;
1711 	}
1712 	proc->cpuid = kvm->arch.model.cpuid;
1713 	proc->ibc = kvm->arch.model.ibc;
1714 	memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1715 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1716 	VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1717 		 kvm->arch.model.ibc,
1718 		 kvm->arch.model.cpuid);
1719 	VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1720 		 kvm->arch.model.fac_list[0],
1721 		 kvm->arch.model.fac_list[1],
1722 		 kvm->arch.model.fac_list[2]);
1723 	if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1724 		ret = -EFAULT;
1725 	kfree(proc);
1726 out:
1727 	return ret;
1728 }
1729 
1730 static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1731 {
1732 	struct kvm_s390_vm_cpu_machine *mach;
1733 	int ret = 0;
1734 
1735 	mach = kzalloc_obj(*mach, GFP_KERNEL_ACCOUNT);
1736 	if (!mach) {
1737 		ret = -ENOMEM;
1738 		goto out;
1739 	}
1740 	get_cpu_id((struct cpuid *) &mach->cpuid);
1741 	mach->ibc = sclp.ibc;
1742 	memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1743 	       S390_ARCH_FAC_LIST_SIZE_BYTE);
1744 	memcpy((unsigned long *)&mach->fac_list, stfle_fac_list,
1745 	       sizeof(stfle_fac_list));
1746 	VM_EVENT(kvm, 3, "GET: host ibc:  0x%4.4x, host cpuid:  0x%16.16llx",
1747 		 kvm->arch.model.ibc,
1748 		 kvm->arch.model.cpuid);
1749 	VM_EVENT(kvm, 3, "GET: host facmask:  0x%16.16llx.%16.16llx.%16.16llx",
1750 		 mach->fac_mask[0],
1751 		 mach->fac_mask[1],
1752 		 mach->fac_mask[2]);
1753 	VM_EVENT(kvm, 3, "GET: host faclist:  0x%16.16llx.%16.16llx.%16.16llx",
1754 		 mach->fac_list[0],
1755 		 mach->fac_list[1],
1756 		 mach->fac_list[2]);
1757 	if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1758 		ret = -EFAULT;
1759 	kfree(mach);
1760 out:
1761 	return ret;
1762 }
1763 
1764 static int kvm_s390_get_processor_feat(struct kvm *kvm,
1765 				       struct kvm_device_attr *attr)
1766 {
1767 	struct kvm_s390_vm_cpu_feat data;
1768 
1769 	bitmap_to_arr64(data.feat, kvm->arch.cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1770 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1771 		return -EFAULT;
1772 	VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1773 			 data.feat[0],
1774 			 data.feat[1],
1775 			 data.feat[2]);
1776 	return 0;
1777 }
1778 
1779 static int kvm_s390_get_machine_feat(struct kvm *kvm,
1780 				     struct kvm_device_attr *attr)
1781 {
1782 	struct kvm_s390_vm_cpu_feat data;
1783 
1784 	bitmap_to_arr64(data.feat, kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
1785 	if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1786 		return -EFAULT;
1787 	VM_EVENT(kvm, 3, "GET: host feat:  0x%16.16llx.0x%16.16llx.0x%16.16llx",
1788 			 data.feat[0],
1789 			 data.feat[1],
1790 			 data.feat[2]);
1791 	return 0;
1792 }
1793 
1794 static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
1795 					  struct kvm_device_attr *attr)
1796 {
1797 	if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1798 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
1799 		return -EFAULT;
1800 
1801 	VM_EVENT(kvm, 3, "GET: guest PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1802 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1803 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1804 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1805 		 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1806 	VM_EVENT(kvm, 3, "GET: guest PTFF   subfunc 0x%16.16lx.%16.16lx",
1807 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1808 		 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1809 	VM_EVENT(kvm, 3, "GET: guest KMAC   subfunc 0x%16.16lx.%16.16lx",
1810 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1811 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1812 	VM_EVENT(kvm, 3, "GET: guest KMC    subfunc 0x%16.16lx.%16.16lx",
1813 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1814 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1815 	VM_EVENT(kvm, 3, "GET: guest KM     subfunc 0x%16.16lx.%16.16lx",
1816 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1817 		 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1818 	VM_EVENT(kvm, 3, "GET: guest KIMD   subfunc 0x%16.16lx.%16.16lx",
1819 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1820 		 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1821 	VM_EVENT(kvm, 3, "GET: guest KLMD   subfunc 0x%16.16lx.%16.16lx",
1822 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1823 		 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1824 	VM_EVENT(kvm, 3, "GET: guest PCKMO  subfunc 0x%16.16lx.%16.16lx",
1825 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1826 		 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1827 	VM_EVENT(kvm, 3, "GET: guest KMCTR  subfunc 0x%16.16lx.%16.16lx",
1828 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1829 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1830 	VM_EVENT(kvm, 3, "GET: guest KMF    subfunc 0x%16.16lx.%16.16lx",
1831 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1832 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1833 	VM_EVENT(kvm, 3, "GET: guest KMO    subfunc 0x%16.16lx.%16.16lx",
1834 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1835 		 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1836 	VM_EVENT(kvm, 3, "GET: guest PCC    subfunc 0x%16.16lx.%16.16lx",
1837 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1838 		 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1839 	VM_EVENT(kvm, 3, "GET: guest PPNO   subfunc 0x%16.16lx.%16.16lx",
1840 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1841 		 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1842 	VM_EVENT(kvm, 3, "GET: guest KMA    subfunc 0x%16.16lx.%16.16lx",
1843 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1844 		 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1845 	VM_EVENT(kvm, 3, "GET: guest KDSA   subfunc 0x%16.16lx.%16.16lx",
1846 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1847 		 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1848 	VM_EVENT(kvm, 3, "GET: guest SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1849 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1850 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1851 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1852 		 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1853 	VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1854 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1855 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1856 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1857 		 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1858 	VM_EVENT(kvm, 3, "GET: guest PFCR   subfunc 0x%16.16lx.%16.16lx",
1859 		 ((unsigned long *) &kvm_s390_available_subfunc.pfcr)[0],
1860 		 ((unsigned long *) &kvm_s390_available_subfunc.pfcr)[1]);
1861 
1862 	return 0;
1863 }
1864 
1865 static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
1866 					struct kvm_device_attr *attr)
1867 {
1868 	if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
1869 	    sizeof(struct kvm_s390_vm_cpu_subfunc)))
1870 		return -EFAULT;
1871 
1872 	VM_EVENT(kvm, 3, "GET: host  PLO    subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1873 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
1874 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
1875 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
1876 		 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
1877 	VM_EVENT(kvm, 3, "GET: host  PTFF   subfunc 0x%16.16lx.%16.16lx",
1878 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
1879 		 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
1880 	VM_EVENT(kvm, 3, "GET: host  KMAC   subfunc 0x%16.16lx.%16.16lx",
1881 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
1882 		 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
1883 	VM_EVENT(kvm, 3, "GET: host  KMC    subfunc 0x%16.16lx.%16.16lx",
1884 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
1885 		 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
1886 	VM_EVENT(kvm, 3, "GET: host  KM     subfunc 0x%16.16lx.%16.16lx",
1887 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
1888 		 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
1889 	VM_EVENT(kvm, 3, "GET: host  KIMD   subfunc 0x%16.16lx.%16.16lx",
1890 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
1891 		 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
1892 	VM_EVENT(kvm, 3, "GET: host  KLMD   subfunc 0x%16.16lx.%16.16lx",
1893 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
1894 		 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
1895 	VM_EVENT(kvm, 3, "GET: host  PCKMO  subfunc 0x%16.16lx.%16.16lx",
1896 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
1897 		 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
1898 	VM_EVENT(kvm, 3, "GET: host  KMCTR  subfunc 0x%16.16lx.%16.16lx",
1899 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
1900 		 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
1901 	VM_EVENT(kvm, 3, "GET: host  KMF    subfunc 0x%16.16lx.%16.16lx",
1902 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
1903 		 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
1904 	VM_EVENT(kvm, 3, "GET: host  KMO    subfunc 0x%16.16lx.%16.16lx",
1905 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
1906 		 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
1907 	VM_EVENT(kvm, 3, "GET: host  PCC    subfunc 0x%16.16lx.%16.16lx",
1908 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
1909 		 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
1910 	VM_EVENT(kvm, 3, "GET: host  PPNO   subfunc 0x%16.16lx.%16.16lx",
1911 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
1912 		 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
1913 	VM_EVENT(kvm, 3, "GET: host  KMA    subfunc 0x%16.16lx.%16.16lx",
1914 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
1915 		 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
1916 	VM_EVENT(kvm, 3, "GET: host  KDSA   subfunc 0x%16.16lx.%16.16lx",
1917 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
1918 		 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1919 	VM_EVENT(kvm, 3, "GET: host  SORTL  subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1920 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1921 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1922 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1923 		 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
1924 	VM_EVENT(kvm, 3, "GET: host  DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1925 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
1926 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
1927 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
1928 		 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
1929 	VM_EVENT(kvm, 3, "GET: host  PFCR   subfunc 0x%16.16lx.%16.16lx",
1930 		 ((unsigned long *) &kvm_s390_available_subfunc.pfcr)[0],
1931 		 ((unsigned long *) &kvm_s390_available_subfunc.pfcr)[1]);
1932 
1933 	return 0;
1934 }
1935 
1936 static int kvm_s390_get_processor_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
1937 {
1938 	struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
1939 	unsigned long feat = kvm->arch.model.uv_feat_guest.feat;
1940 
1941 	if (put_user(feat, &dst->feat))
1942 		return -EFAULT;
1943 	VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
1944 
1945 	return 0;
1946 }
1947 
1948 static int kvm_s390_get_machine_uv_feat(struct kvm *kvm, struct kvm_device_attr *attr)
1949 {
1950 	struct kvm_s390_vm_cpu_uv_feat __user *dst = (void __user *)attr->addr;
1951 	unsigned long feat;
1952 
1953 	BUILD_BUG_ON(sizeof(*dst) != sizeof(uv_info.uv_feature_indications));
1954 
1955 	feat = uv_info.uv_feature_indications & KVM_S390_VM_CPU_UV_FEAT_GUEST_MASK;
1956 	if (put_user(feat, &dst->feat))
1957 		return -EFAULT;
1958 	VM_EVENT(kvm, 3, "GET: guest UV-feat: 0x%16.16lx", feat);
1959 
1960 	return 0;
1961 }
1962 
1963 static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1964 {
1965 	int ret = -ENXIO;
1966 
1967 	switch (attr->attr) {
1968 	case KVM_S390_VM_CPU_PROCESSOR:
1969 		ret = kvm_s390_get_processor(kvm, attr);
1970 		break;
1971 	case KVM_S390_VM_CPU_MACHINE:
1972 		ret = kvm_s390_get_machine(kvm, attr);
1973 		break;
1974 	case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1975 		ret = kvm_s390_get_processor_feat(kvm, attr);
1976 		break;
1977 	case KVM_S390_VM_CPU_MACHINE_FEAT:
1978 		ret = kvm_s390_get_machine_feat(kvm, attr);
1979 		break;
1980 	case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1981 		ret = kvm_s390_get_processor_subfunc(kvm, attr);
1982 		break;
1983 	case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1984 		ret = kvm_s390_get_machine_subfunc(kvm, attr);
1985 		break;
1986 	case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
1987 		ret = kvm_s390_get_processor_uv_feat(kvm, attr);
1988 		break;
1989 	case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
1990 		ret = kvm_s390_get_machine_uv_feat(kvm, attr);
1991 		break;
1992 	}
1993 	return ret;
1994 }
1995 
1996 /**
1997  * kvm_s390_update_topology_change_report - update CPU topology change report
1998  * @kvm: guest KVM description
1999  * @val: set or clear the MTCR bit
2000  *
2001  * Updates the Multiprocessor Topology-Change-Report bit to signal
2002  * the guest with a topology change.
2003  * This is only relevant if the topology facility is present.
2004  */
2005 static void kvm_s390_update_topology_change_report(struct kvm *kvm, bool val)
2006 {
2007 	union sca_utility new, old;
2008 	struct esca_block *sca;
2009 
2010 	sca = kvm->arch.sca;
2011 	old = READ_ONCE(sca->utility);
2012 	do {
2013 		new = old;
2014 		new.mtcr = val;
2015 	} while (!try_cmpxchg(&sca->utility.val, &old.val, new.val));
2016 }
2017 
2018 static int kvm_s390_set_topo_change_indication(struct kvm *kvm,
2019 					       struct kvm_device_attr *attr)
2020 {
2021 	if (!test_kvm_facility(kvm, 11))
2022 		return -ENXIO;
2023 
2024 	kvm_s390_update_topology_change_report(kvm, !!attr->attr);
2025 	return 0;
2026 }
2027 
2028 static int kvm_s390_get_topo_change_indication(struct kvm *kvm,
2029 					       struct kvm_device_attr *attr)
2030 {
2031 	u8 topo;
2032 
2033 	if (!test_kvm_facility(kvm, 11))
2034 		return -ENXIO;
2035 
2036 	topo = kvm->arch.sca->utility.mtcr;
2037 
2038 	return put_user(topo, (u8 __user *)attr->addr);
2039 }
2040 
2041 static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
2042 {
2043 	int ret;
2044 
2045 	switch (attr->group) {
2046 	case KVM_S390_VM_MEM_CTRL:
2047 		ret = kvm_s390_set_mem_control(kvm, attr);
2048 		break;
2049 	case KVM_S390_VM_TOD:
2050 		ret = kvm_s390_set_tod(kvm, attr);
2051 		break;
2052 	case KVM_S390_VM_CPU_MODEL:
2053 		ret = kvm_s390_set_cpu_model(kvm, attr);
2054 		break;
2055 	case KVM_S390_VM_CRYPTO:
2056 		ret = kvm_s390_vm_set_crypto(kvm, attr);
2057 		break;
2058 	case KVM_S390_VM_MIGRATION:
2059 		ret = kvm_s390_vm_set_migration(kvm, attr);
2060 		break;
2061 	case KVM_S390_VM_CPU_TOPOLOGY:
2062 		ret = kvm_s390_set_topo_change_indication(kvm, attr);
2063 		break;
2064 	default:
2065 		ret = -ENXIO;
2066 		break;
2067 	}
2068 
2069 	return ret;
2070 }
2071 
2072 static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
2073 {
2074 	int ret;
2075 
2076 	switch (attr->group) {
2077 	case KVM_S390_VM_MEM_CTRL:
2078 		ret = kvm_s390_get_mem_control(kvm, attr);
2079 		break;
2080 	case KVM_S390_VM_TOD:
2081 		ret = kvm_s390_get_tod(kvm, attr);
2082 		break;
2083 	case KVM_S390_VM_CPU_MODEL:
2084 		ret = kvm_s390_get_cpu_model(kvm, attr);
2085 		break;
2086 	case KVM_S390_VM_MIGRATION:
2087 		ret = kvm_s390_vm_get_migration(kvm, attr);
2088 		break;
2089 	case KVM_S390_VM_CPU_TOPOLOGY:
2090 		ret = kvm_s390_get_topo_change_indication(kvm, attr);
2091 		break;
2092 	default:
2093 		ret = -ENXIO;
2094 		break;
2095 	}
2096 
2097 	return ret;
2098 }
2099 
2100 static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
2101 {
2102 	int ret;
2103 
2104 	switch (attr->group) {
2105 	case KVM_S390_VM_MEM_CTRL:
2106 		switch (attr->attr) {
2107 		case KVM_S390_VM_MEM_ENABLE_CMMA:
2108 		case KVM_S390_VM_MEM_CLR_CMMA:
2109 			ret = sclp.has_cmma ? 0 : -ENXIO;
2110 			break;
2111 		case KVM_S390_VM_MEM_LIMIT_SIZE:
2112 			ret = 0;
2113 			break;
2114 		default:
2115 			ret = -ENXIO;
2116 			break;
2117 		}
2118 		break;
2119 	case KVM_S390_VM_TOD:
2120 		switch (attr->attr) {
2121 		case KVM_S390_VM_TOD_LOW:
2122 		case KVM_S390_VM_TOD_HIGH:
2123 			ret = 0;
2124 			break;
2125 		default:
2126 			ret = -ENXIO;
2127 			break;
2128 		}
2129 		break;
2130 	case KVM_S390_VM_CPU_MODEL:
2131 		switch (attr->attr) {
2132 		case KVM_S390_VM_CPU_PROCESSOR:
2133 		case KVM_S390_VM_CPU_MACHINE:
2134 		case KVM_S390_VM_CPU_PROCESSOR_FEAT:
2135 		case KVM_S390_VM_CPU_MACHINE_FEAT:
2136 		case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
2137 		case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
2138 		case KVM_S390_VM_CPU_MACHINE_UV_FEAT_GUEST:
2139 		case KVM_S390_VM_CPU_PROCESSOR_UV_FEAT_GUEST:
2140 			ret = 0;
2141 			break;
2142 		default:
2143 			ret = -ENXIO;
2144 			break;
2145 		}
2146 		break;
2147 	case KVM_S390_VM_CRYPTO:
2148 		switch (attr->attr) {
2149 		case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
2150 		case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
2151 		case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
2152 		case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
2153 			ret = 0;
2154 			break;
2155 		case KVM_S390_VM_CRYPTO_ENABLE_APIE:
2156 		case KVM_S390_VM_CRYPTO_DISABLE_APIE:
2157 			ret = ap_instructions_available() ? 0 : -ENXIO;
2158 			break;
2159 		default:
2160 			ret = -ENXIO;
2161 			break;
2162 		}
2163 		break;
2164 	case KVM_S390_VM_MIGRATION:
2165 		ret = 0;
2166 		break;
2167 	case KVM_S390_VM_CPU_TOPOLOGY:
2168 		ret = test_kvm_facility(kvm, 11) ? 0 : -ENXIO;
2169 		break;
2170 	default:
2171 		ret = -ENXIO;
2172 		break;
2173 	}
2174 
2175 	return ret;
2176 }
2177 
2178 static int kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
2179 {
2180 	union skey *keys;
2181 	int i, r = 0;
2182 
2183 	if (args->flags != 0)
2184 		return -EINVAL;
2185 
2186 	/* Is this guest using storage keys? */
2187 	if (!uses_skeys(kvm->arch.gmap))
2188 		return KVM_S390_GET_SKEYS_NONE;
2189 
2190 	/* Enforce sane limit on memory allocation */
2191 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
2192 		return -EINVAL;
2193 
2194 	keys = kvmalloc_array(args->count, sizeof(*keys), GFP_KERNEL_ACCOUNT);
2195 	if (!keys)
2196 		return -ENOMEM;
2197 
2198 	scoped_guard(read_lock, &kvm->mmu_lock) {
2199 		for (i = 0; i < args->count; i++) {
2200 			r = dat_get_storage_key(kvm->arch.gmap->asce,
2201 						args->start_gfn + i, keys + i);
2202 			if (r)
2203 				break;
2204 		}
2205 	}
2206 
2207 	if (!r) {
2208 		r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
2209 				 sizeof(uint8_t) * args->count);
2210 		if (r)
2211 			r = -EFAULT;
2212 	}
2213 
2214 	kvfree(keys);
2215 	return r;
2216 }
2217 
2218 static int kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
2219 {
2220 	struct kvm_s390_mmu_cache *mc;
2221 	union skey *keys;
2222 	int i, r = 0;
2223 
2224 	if (args->flags != 0)
2225 		return -EINVAL;
2226 
2227 	/* Enforce sane limit on memory allocation */
2228 	if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
2229 		return -EINVAL;
2230 
2231 	keys = kvmalloc_array(args->count, sizeof(*keys), GFP_KERNEL_ACCOUNT);
2232 	if (!keys)
2233 		return -ENOMEM;
2234 
2235 	r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
2236 			   sizeof(uint8_t) * args->count);
2237 	if (r) {
2238 		r = -EFAULT;
2239 		goto out;
2240 	}
2241 
2242 	/* Enable storage key handling for the guest */
2243 	r = gmap_enable_skeys(kvm->arch.gmap);
2244 	if (r)
2245 		goto out;
2246 
2247 	r = -EINVAL;
2248 	for (i = 0; i < args->count; i++) {
2249 		/* Lowest order bit is reserved */
2250 		if (keys[i].zero)
2251 			goto out;
2252 	}
2253 
2254 	mc = kvm_s390_new_mmu_cache();
2255 	if (!mc) {
2256 		r = -ENOMEM;
2257 		goto out;
2258 	}
2259 
2260 	r = 0;
2261 	do {
2262 		r = kvm_s390_mmu_cache_topup(mc);
2263 		if (r == -ENOMEM)
2264 			break;
2265 		scoped_guard(read_lock, &kvm->mmu_lock) {
2266 			for (i = 0 ; i < args->count; i++) {
2267 				r = dat_set_storage_key(mc, kvm->arch.gmap->asce,
2268 							args->start_gfn + i, keys[i], 0);
2269 				if (r)
2270 					break;
2271 			}
2272 		}
2273 	} while (r == -ENOMEM);
2274 	kvm_s390_free_mmu_cache(mc);
2275 out:
2276 	kvfree(keys);
2277 	return r;
2278 }
2279 
2280 /*
2281  * This function searches for the next page with dirty CMMA attributes, and
2282  * saves the attributes in the buffer up to either the end of the buffer or
2283  * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
2284  * no trailing clean bytes are saved.
2285  * In case no dirty bits were found, or if CMMA was not enabled or used, the
2286  * output buffer will indicate 0 as length.
2287  */
2288 static int kvm_s390_get_cmma_bits(struct kvm *kvm,
2289 				  struct kvm_s390_cmma_log *args)
2290 {
2291 	int peek, ret;
2292 	u8 *values;
2293 
2294 	if (!kvm->arch.use_cmma)
2295 		return -ENXIO;
2296 	/* Invalid/unsupported flags were specified */
2297 	if (args->flags & ~KVM_S390_CMMA_PEEK)
2298 		return -EINVAL;
2299 	/* Migration mode query, and we are not doing a migration */
2300 	peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2301 	if (!peek && !kvm->arch.migration_mode)
2302 		return -EINVAL;
2303 	/* CMMA is disabled or was not used, or the buffer has length zero */
2304 	args->count = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2305 	if (!args->count || !uses_cmm(kvm->arch.gmap)) {
2306 		memset(args, 0, sizeof(*args));
2307 		return 0;
2308 	}
2309 	/* We are not peeking, and there are no dirty pages */
2310 	if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
2311 		memset(args, 0, sizeof(*args));
2312 		return 0;
2313 	}
2314 
2315 	values = vzalloc(args->count);
2316 	if (!values)
2317 		return -ENOMEM;
2318 
2319 	scoped_guard(read_lock, &kvm->mmu_lock) {
2320 		if (peek)
2321 			ret = dat_peek_cmma(args->start_gfn, kvm->arch.gmap->asce, &args->count,
2322 					    values);
2323 		else
2324 			ret = dat_get_cmma(kvm->arch.gmap->asce, &args->start_gfn, &args->count,
2325 					   values, &kvm->arch.cmma_dirty_pages);
2326 	}
2327 
2328 	if (kvm->arch.migration_mode)
2329 		args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2330 	else
2331 		args->remaining = 0;
2332 
2333 	if (copy_to_user((void __user *)args->values, values, args->count))
2334 		ret = -EFAULT;
2335 
2336 	vfree(values);
2337 	return ret;
2338 }
2339 
2340 /*
2341  * This function sets the CMMA attributes for the given pages. If the input
2342  * buffer has zero length, no action is taken, otherwise the attributes are
2343  * set and the mm->context.uses_cmm flag is set.
2344  */
2345 static int kvm_s390_set_cmma_bits(struct kvm *kvm,
2346 				  const struct kvm_s390_cmma_log *args)
2347 {
2348 	struct kvm_s390_mmu_cache *mc __free(kvm_s390_mmu_cache) = NULL;
2349 	u8 *bits __free(kvfree) = NULL;
2350 	int r = 0;
2351 
2352 	if (!kvm->arch.use_cmma)
2353 		return -ENXIO;
2354 	/* invalid/unsupported flags */
2355 	if (args->flags != 0)
2356 		return -EINVAL;
2357 	/* Enforce sane limit on memory allocation */
2358 	if (args->count > KVM_S390_CMMA_SIZE_MAX)
2359 		return -EINVAL;
2360 	/* Nothing to do */
2361 	if (args->count == 0)
2362 		return 0;
2363 
2364 	mc = kvm_s390_new_mmu_cache();
2365 	if (!mc)
2366 		return -ENOMEM;
2367 	bits = vmalloc(array_size(sizeof(*bits), args->count));
2368 	if (!bits)
2369 		return -ENOMEM;
2370 
2371 	r = copy_from_user(bits, (void __user *)args->values, args->count);
2372 	if (r)
2373 		return -EFAULT;
2374 
2375 	do {
2376 		r = kvm_s390_mmu_cache_topup(mc);
2377 		if (r)
2378 			return r;
2379 		scoped_guard(read_lock, &kvm->mmu_lock) {
2380 			r = dat_set_cmma_bits(mc, kvm->arch.gmap->asce, args->start_gfn,
2381 					      args->count, args->mask, bits);
2382 		}
2383 	} while (r == -ENOMEM);
2384 
2385 	set_bit(GMAP_FLAG_USES_CMM, &kvm->arch.gmap->flags);
2386 
2387 	return r;
2388 }
2389 
2390 /**
2391  * kvm_s390_cpus_from_pv - Convert all protected vCPUs in a protected VM to
2392  * non protected.
2393  * @kvm: the VM whose protected vCPUs are to be converted
2394  * @rc: return value for the RC field of the UVC (in case of error)
2395  * @rrc: return value for the RRC field of the UVC (in case of error)
2396  *
2397  * Does not stop in case of error, tries to convert as many
2398  * CPUs as possible. In case of error, the RC and RRC of the last error are
2399  * returned.
2400  *
2401  * Return: 0 in case of success, otherwise -EIO
2402  */
2403 int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
2404 {
2405 	struct kvm_vcpu *vcpu;
2406 	unsigned long i;
2407 	u16 _rc, _rrc;
2408 	int ret = 0;
2409 
2410 	/*
2411 	 * We ignore failures and try to destroy as many CPUs as possible.
2412 	 * At the same time we must not free the assigned resources when
2413 	 * this fails, as the ultravisor has still access to that memory.
2414 	 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
2415 	 * behind.
2416 	 * We want to return the first failure rc and rrc, though.
2417 	 */
2418 	kvm_for_each_vcpu(i, vcpu, kvm) {
2419 		mutex_lock(&vcpu->mutex);
2420 		if (kvm_s390_pv_destroy_cpu(vcpu, &_rc, &_rrc) && !ret) {
2421 			*rc = _rc;
2422 			*rrc = _rrc;
2423 			ret = -EIO;
2424 		}
2425 		mutex_unlock(&vcpu->mutex);
2426 	}
2427 	/* Ensure that we re-enable gisa if the non-PV guest used it but the PV guest did not. */
2428 	if (use_gisa)
2429 		kvm_s390_gisa_enable(kvm);
2430 	return ret;
2431 }
2432 
2433 /**
2434  * kvm_s390_cpus_to_pv - Convert all non-protected vCPUs in a protected VM
2435  * to protected.
2436  * @kvm: the VM whose protected vCPUs are to be converted
2437  * @rc: return value for the RC field of the UVC (in case of error)
2438  * @rrc: return value for the RRC field of the UVC (in case of error)
2439  *
2440  * Tries to undo the conversion in case of error.
2441  *
2442  * Return: 0 in case of success, otherwise -EIO
2443  */
2444 static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
2445 {
2446 	unsigned long i;
2447 	int r = 0;
2448 	u16 dummy;
2449 
2450 	struct kvm_vcpu *vcpu;
2451 
2452 	/* Disable the GISA if the ultravisor does not support AIV. */
2453 	if (!uv_has_feature(BIT_UV_FEAT_AIV))
2454 		kvm_s390_gisa_disable(kvm);
2455 
2456 	kvm_for_each_vcpu(i, vcpu, kvm) {
2457 		mutex_lock(&vcpu->mutex);
2458 		r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
2459 		mutex_unlock(&vcpu->mutex);
2460 		if (r)
2461 			break;
2462 	}
2463 	if (r)
2464 		kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
2465 	return r;
2466 }
2467 
2468 /*
2469  * Here we provide user space with a direct interface to query UV
2470  * related data like UV maxima and available features as well as
2471  * feature specific data.
2472  *
2473  * To facilitate future extension of the data structures we'll try to
2474  * write data up to the maximum requested length.
2475  */
2476 static ssize_t kvm_s390_handle_pv_info(struct kvm_s390_pv_info *info)
2477 {
2478 	ssize_t len_min;
2479 
2480 	switch (info->header.id) {
2481 	case KVM_PV_INFO_VM: {
2482 		len_min =  sizeof(info->header) + sizeof(info->vm);
2483 
2484 		if (info->header.len_max < len_min)
2485 			return -EINVAL;
2486 
2487 		memcpy(info->vm.inst_calls_list,
2488 		       uv_info.inst_calls_list,
2489 		       sizeof(uv_info.inst_calls_list));
2490 
2491 		/* It's max cpuid not max cpus, so it's off by one */
2492 		info->vm.max_cpus = uv_info.max_guest_cpu_id + 1;
2493 		info->vm.max_guests = uv_info.max_num_sec_conf;
2494 		info->vm.max_guest_addr = uv_info.max_sec_stor_addr;
2495 		info->vm.feature_indication = uv_info.uv_feature_indications;
2496 
2497 		return len_min;
2498 	}
2499 	case KVM_PV_INFO_DUMP: {
2500 		len_min =  sizeof(info->header) + sizeof(info->dump);
2501 
2502 		if (info->header.len_max < len_min)
2503 			return -EINVAL;
2504 
2505 		info->dump.dump_cpu_buffer_len = uv_info.guest_cpu_stor_len;
2506 		info->dump.dump_config_mem_buffer_per_1m = uv_info.conf_dump_storage_state_len;
2507 		info->dump.dump_config_finalize_len = uv_info.conf_dump_finalize_len;
2508 		return len_min;
2509 	}
2510 	default:
2511 		return -EINVAL;
2512 	}
2513 }
2514 
2515 static int kvm_s390_pv_dmp(struct kvm *kvm, struct kvm_pv_cmd *cmd,
2516 			   struct kvm_s390_pv_dmp dmp)
2517 {
2518 	int r = -EINVAL;
2519 	void __user *result_buff = (void __user *)dmp.buff_addr;
2520 
2521 	switch (dmp.subcmd) {
2522 	case KVM_PV_DUMP_INIT: {
2523 		if (kvm->arch.pv.dumping)
2524 			break;
2525 
2526 		/*
2527 		 * Block SIE entry as concurrent dump UVCs could lead
2528 		 * to validities.
2529 		 */
2530 		kvm_s390_vcpu_block_all(kvm);
2531 
2532 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2533 				  UVC_CMD_DUMP_INIT, &cmd->rc, &cmd->rrc);
2534 		KVM_UV_EVENT(kvm, 3, "PROTVIRT DUMP INIT: rc %x rrc %x",
2535 			     cmd->rc, cmd->rrc);
2536 		if (!r) {
2537 			kvm->arch.pv.dumping = true;
2538 		} else {
2539 			kvm_s390_vcpu_unblock_all(kvm);
2540 			r = -EINVAL;
2541 		}
2542 		break;
2543 	}
2544 	case KVM_PV_DUMP_CONFIG_STOR_STATE: {
2545 		if (!kvm->arch.pv.dumping)
2546 			break;
2547 
2548 		/*
2549 		 * gaddr is an output parameter since we might stop
2550 		 * early. As dmp will be copied back in our caller, we
2551 		 * don't need to do it ourselves.
2552 		 */
2553 		r = kvm_s390_pv_dump_stor_state(kvm, result_buff, &dmp.gaddr, dmp.buff_len,
2554 						&cmd->rc, &cmd->rrc);
2555 		break;
2556 	}
2557 	case KVM_PV_DUMP_COMPLETE: {
2558 		if (!kvm->arch.pv.dumping)
2559 			break;
2560 
2561 		r = -EINVAL;
2562 		if (dmp.buff_len < uv_info.conf_dump_finalize_len)
2563 			break;
2564 
2565 		r = kvm_s390_pv_dump_complete(kvm, result_buff,
2566 					      &cmd->rc, &cmd->rrc);
2567 		break;
2568 	}
2569 	default:
2570 		r = -ENOTTY;
2571 		break;
2572 	}
2573 
2574 	return r;
2575 }
2576 
2577 static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
2578 {
2579 	const bool need_lock = (cmd->cmd != KVM_PV_ASYNC_CLEANUP_PERFORM);
2580 	void __user *argp = (void __user *)cmd->data;
2581 	int r = 0;
2582 	u16 dummy;
2583 
2584 	if (need_lock)
2585 		mutex_lock(&kvm->lock);
2586 
2587 	switch (cmd->cmd) {
2588 	case KVM_PV_ENABLE: {
2589 		r = -EINVAL;
2590 		if (kvm_s390_pv_is_protected(kvm))
2591 			break;
2592 
2593 		kvm_s390_unmap_all_adapters(kvm);
2594 		mmap_write_lock(kvm->mm);
2595 		/*
2596 		 * Disable creation of new THPs. Existing THPs can stay, they
2597 		 * will be split when any part of them gets imported.
2598 		 */
2599 		mm_flags_clear(MMF_DISABLE_THP_EXCEPT_ADVISED, kvm->mm);
2600 		mm_flags_set(MMF_DISABLE_THP_COMPLETELY, kvm->mm);
2601 		set_bit(GMAP_FLAG_EXPORT_ON_UNMAP, &kvm->arch.gmap->flags);
2602 		r = gmap_helper_disable_cow_sharing();
2603 		mmap_write_unlock(kvm->mm);
2604 		if (r)
2605 			break;
2606 
2607 		r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
2608 		if (r)
2609 			break;
2610 
2611 		r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
2612 		if (r)
2613 			kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
2614 
2615 		/* we need to block service interrupts from now on */
2616 		set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2617 		break;
2618 	}
2619 	case KVM_PV_ASYNC_CLEANUP_PREPARE:
2620 		r = -EINVAL;
2621 		if (!kvm_s390_pv_is_protected(kvm) || !async_destroy)
2622 			break;
2623 
2624 		r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2625 		/*
2626 		 * If a CPU could not be destroyed, destroy VM will also fail.
2627 		 * There is no point in trying to destroy it. Instead return
2628 		 * the rc and rrc from the first CPU that failed destroying.
2629 		 */
2630 		if (r)
2631 			break;
2632 		r = kvm_s390_pv_set_aside(kvm, &cmd->rc, &cmd->rrc);
2633 
2634 		/* no need to block service interrupts any more */
2635 		clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2636 		break;
2637 	case KVM_PV_ASYNC_CLEANUP_PERFORM:
2638 		r = -EINVAL;
2639 		if (!async_destroy)
2640 			break;
2641 		/* kvm->lock must not be held; this is asserted inside the function. */
2642 		r = kvm_s390_pv_deinit_aside_vm(kvm, &cmd->rc, &cmd->rrc);
2643 		break;
2644 	case KVM_PV_DISABLE: {
2645 		r = -EINVAL;
2646 		if (!kvm_s390_pv_is_protected(kvm))
2647 			break;
2648 
2649 		r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2650 		/*
2651 		 * If a CPU could not be destroyed, destroy VM will also fail.
2652 		 * There is no point in trying to destroy it. Instead return
2653 		 * the rc and rrc from the first CPU that failed destroying.
2654 		 */
2655 		if (r)
2656 			break;
2657 		r = kvm_s390_pv_deinit_cleanup_all(kvm, &cmd->rc, &cmd->rrc);
2658 
2659 		/* no need to block service interrupts any more */
2660 		clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2661 		break;
2662 	}
2663 	case KVM_PV_SET_SEC_PARMS: {
2664 		struct kvm_s390_pv_sec_parm parms = {};
2665 		void *hdr;
2666 
2667 		r = -EINVAL;
2668 		if (!kvm_s390_pv_is_protected(kvm))
2669 			break;
2670 
2671 		r = -EFAULT;
2672 		if (copy_from_user(&parms, argp, sizeof(parms)))
2673 			break;
2674 
2675 		/* Currently restricted to 1MiB */
2676 		r = -EINVAL;
2677 		if (parms.length > SZ_1M)
2678 			break;
2679 
2680 		r = -ENOMEM;
2681 		hdr = vmalloc(parms.length);
2682 		if (!hdr)
2683 			break;
2684 
2685 		r = -EFAULT;
2686 		if (!copy_from_user(hdr, (void __user *)parms.origin,
2687 				    parms.length))
2688 			r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
2689 						      &cmd->rc, &cmd->rrc);
2690 
2691 		vfree(hdr);
2692 		break;
2693 	}
2694 	case KVM_PV_UNPACK: {
2695 		struct kvm_s390_pv_unp unp = {};
2696 
2697 		r = -EINVAL;
2698 		if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
2699 			break;
2700 
2701 		r = -EFAULT;
2702 		if (copy_from_user(&unp, argp, sizeof(unp)))
2703 			break;
2704 
2705 		r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
2706 				       &cmd->rc, &cmd->rrc);
2707 		break;
2708 	}
2709 	case KVM_PV_VERIFY: {
2710 		r = -EINVAL;
2711 		if (!kvm_s390_pv_is_protected(kvm))
2712 			break;
2713 
2714 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2715 				  UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
2716 		KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
2717 			     cmd->rrc);
2718 		break;
2719 	}
2720 	case KVM_PV_PREP_RESET: {
2721 		r = -EINVAL;
2722 		if (!kvm_s390_pv_is_protected(kvm))
2723 			break;
2724 
2725 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2726 				  UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2727 		KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2728 			     cmd->rc, cmd->rrc);
2729 		break;
2730 	}
2731 	case KVM_PV_UNSHARE_ALL: {
2732 		r = -EINVAL;
2733 		if (!kvm_s390_pv_is_protected(kvm))
2734 			break;
2735 
2736 		r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2737 				  UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2738 		KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2739 			     cmd->rc, cmd->rrc);
2740 		break;
2741 	}
2742 	case KVM_PV_INFO: {
2743 		struct kvm_s390_pv_info info = {};
2744 		ssize_t data_len;
2745 
2746 		/*
2747 		 * No need to check the VM protection here.
2748 		 *
2749 		 * Maybe user space wants to query some of the data
2750 		 * when the VM is still unprotected. If we see the
2751 		 * need to fence a new data command we can still
2752 		 * return an error in the info handler.
2753 		 */
2754 
2755 		r = -EFAULT;
2756 		if (copy_from_user(&info, argp, sizeof(info.header)))
2757 			break;
2758 
2759 		r = -EINVAL;
2760 		if (info.header.len_max < sizeof(info.header))
2761 			break;
2762 
2763 		data_len = kvm_s390_handle_pv_info(&info);
2764 		if (data_len < 0) {
2765 			r = data_len;
2766 			break;
2767 		}
2768 		/*
2769 		 * If a data command struct is extended (multiple
2770 		 * times) this can be used to determine how much of it
2771 		 * is valid.
2772 		 */
2773 		info.header.len_written = data_len;
2774 
2775 		r = -EFAULT;
2776 		if (copy_to_user(argp, &info, data_len))
2777 			break;
2778 
2779 		r = 0;
2780 		break;
2781 	}
2782 	case KVM_PV_DUMP: {
2783 		struct kvm_s390_pv_dmp dmp;
2784 
2785 		r = -EINVAL;
2786 		if (!kvm_s390_pv_is_protected(kvm))
2787 			break;
2788 
2789 		r = -EFAULT;
2790 		if (copy_from_user(&dmp, argp, sizeof(dmp)))
2791 			break;
2792 
2793 		r = kvm_s390_pv_dmp(kvm, cmd, dmp);
2794 		if (r)
2795 			break;
2796 
2797 		if (copy_to_user(argp, &dmp, sizeof(dmp))) {
2798 			r = -EFAULT;
2799 			break;
2800 		}
2801 
2802 		break;
2803 	}
2804 	default:
2805 		r = -ENOTTY;
2806 	}
2807 	if (need_lock)
2808 		mutex_unlock(&kvm->lock);
2809 
2810 	return r;
2811 }
2812 
2813 static int mem_op_validate_common(struct kvm_s390_mem_op *mop, u64 supported_flags)
2814 {
2815 	if (mop->flags & ~supported_flags || !mop->size)
2816 		return -EINVAL;
2817 	if (mop->size > MEM_OP_MAX_SIZE)
2818 		return -E2BIG;
2819 	if (mop->flags & KVM_S390_MEMOP_F_SKEY_PROTECTION) {
2820 		if (mop->key > 0xf)
2821 			return -EINVAL;
2822 	} else {
2823 		mop->key = 0;
2824 	}
2825 	return 0;
2826 }
2827 
2828 static int kvm_s390_vm_mem_op_abs(struct kvm *kvm, struct kvm_s390_mem_op *mop)
2829 {
2830 	void __user *uaddr = (void __user *)mop->buf;
2831 	void *tmpbuf __free(kvfree) = NULL;
2832 	enum gacc_mode acc_mode;
2833 	int r;
2834 
2835 	r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION |
2836 					KVM_S390_MEMOP_F_CHECK_ONLY);
2837 	if (r)
2838 		return r;
2839 
2840 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
2841 		tmpbuf = vmalloc(mop->size);
2842 		if (!tmpbuf)
2843 			return -ENOMEM;
2844 	}
2845 
2846 	acc_mode = mop->op == KVM_S390_MEMOP_ABSOLUTE_READ ? GACC_FETCH : GACC_STORE;
2847 
2848 	scoped_guard(srcu, &kvm->srcu) {
2849 		if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)
2850 			return check_gpa_range(kvm, mop->gaddr, mop->size, acc_mode, mop->key);
2851 
2852 		if (acc_mode == GACC_STORE && copy_from_user(tmpbuf, uaddr, mop->size))
2853 			return -EFAULT;
2854 		r = access_guest_abs_with_key(kvm, mop->gaddr, tmpbuf,
2855 					      mop->size, acc_mode, mop->key);
2856 		if (r)
2857 			return r;
2858 		if (acc_mode != GACC_STORE && copy_to_user(uaddr, tmpbuf, mop->size))
2859 			return -EFAULT;
2860 	}
2861 	return 0;
2862 }
2863 
2864 static int kvm_s390_vm_mem_op_cmpxchg(struct kvm *kvm, struct kvm_s390_mem_op *mop)
2865 {
2866 	void __user *uaddr = (void __user *)mop->buf;
2867 	void __user *old_addr = (void __user *)mop->old_addr;
2868 	union kvm_s390_quad old = { .sixteen = 0 };
2869 	union kvm_s390_quad new = { .sixteen = 0 };
2870 	bool success = false;
2871 	int r;
2872 
2873 	r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_SKEY_PROTECTION);
2874 	if (r)
2875 		return r;
2876 	/*
2877 	 * This validates off_in_quad. Checking that size is a power
2878 	 * of two is not necessary, as cmpxchg_guest_abs_with_key
2879 	 * takes care of that
2880 	 */
2881 	if (mop->size > sizeof(new))
2882 		return -EINVAL;
2883 	if (copy_from_user(&new, uaddr, mop->size))
2884 		return -EFAULT;
2885 	if (copy_from_user(&old, old_addr, mop->size))
2886 		return -EFAULT;
2887 
2888 	scoped_guard(srcu, &kvm->srcu) {
2889 		r = cmpxchg_guest_abs_with_key(kvm, mop->gaddr, mop->size, &old, new,
2890 					       mop->key, &success);
2891 
2892 		if (!success && copy_to_user(old_addr, &old, mop->size))
2893 			return -EFAULT;
2894 	}
2895 	return r;
2896 }
2897 
2898 static int kvm_s390_vm_mem_op(struct kvm *kvm, struct kvm_s390_mem_op *mop)
2899 {
2900 	/*
2901 	 * This is technically a heuristic only, if the kvm->lock is not
2902 	 * taken, it is not guaranteed that the vm is/remains non-protected.
2903 	 * This is ok from a kernel perspective, wrongdoing is detected
2904 	 * on the access, -EFAULT is returned and the vm may crash the
2905 	 * next time it accesses the memory in question.
2906 	 * There is no sane usecase to do switching and a memop on two
2907 	 * different CPUs at the same time.
2908 	 */
2909 	if (kvm_s390_pv_get_handle(kvm))
2910 		return -EINVAL;
2911 
2912 	switch (mop->op) {
2913 	case KVM_S390_MEMOP_ABSOLUTE_READ:
2914 	case KVM_S390_MEMOP_ABSOLUTE_WRITE:
2915 		return kvm_s390_vm_mem_op_abs(kvm, mop);
2916 	case KVM_S390_MEMOP_ABSOLUTE_CMPXCHG:
2917 		return kvm_s390_vm_mem_op_cmpxchg(kvm, mop);
2918 	default:
2919 		return -EINVAL;
2920 	}
2921 }
2922 
2923 int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
2924 {
2925 	struct kvm *kvm = filp->private_data;
2926 	void __user *argp = (void __user *)arg;
2927 	struct kvm_device_attr attr;
2928 	int r;
2929 	struct kvm_s390_interrupt_info *inti;
2930 
2931 	switch (ioctl) {
2932 	case KVM_S390_INTERRUPT: {
2933 		struct kvm_s390_interrupt s390int;
2934 
2935 		r = -EFAULT;
2936 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
2937 			break;
2938 		inti = kzalloc_obj(*inti, GFP_KERNEL_ACCOUNT);
2939 		if (!inti)
2940 			return -ENOMEM;
2941 		r = kvm_s390_inject_vm(kvm, &s390int, inti);
2942 		if (r)
2943 			kfree(inti);
2944 		break;
2945 	}
2946 	case KVM_CREATE_IRQCHIP: {
2947 		r = -EINVAL;
2948 		if (kvm->arch.use_irqchip)
2949 			r = 0;
2950 		break;
2951 	}
2952 	case KVM_SET_DEVICE_ATTR: {
2953 		r = -EFAULT;
2954 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2955 			break;
2956 		r = kvm_s390_vm_set_attr(kvm, &attr);
2957 		break;
2958 	}
2959 	case KVM_GET_DEVICE_ATTR: {
2960 		r = -EFAULT;
2961 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2962 			break;
2963 		r = kvm_s390_vm_get_attr(kvm, &attr);
2964 		break;
2965 	}
2966 	case KVM_HAS_DEVICE_ATTR: {
2967 		r = -EFAULT;
2968 		if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2969 			break;
2970 		r = kvm_s390_vm_has_attr(kvm, &attr);
2971 		break;
2972 	}
2973 	case KVM_S390_GET_SKEYS: {
2974 		struct kvm_s390_skeys args;
2975 
2976 		r = -EFAULT;
2977 		if (copy_from_user(&args, argp,
2978 				   sizeof(struct kvm_s390_skeys)))
2979 			break;
2980 		r = kvm_s390_get_skeys(kvm, &args);
2981 		break;
2982 	}
2983 	case KVM_S390_SET_SKEYS: {
2984 		struct kvm_s390_skeys args;
2985 
2986 		r = -EFAULT;
2987 		if (copy_from_user(&args, argp,
2988 				   sizeof(struct kvm_s390_skeys)))
2989 			break;
2990 		r = kvm_s390_set_skeys(kvm, &args);
2991 		break;
2992 	}
2993 	case KVM_S390_GET_CMMA_BITS: {
2994 		struct kvm_s390_cmma_log args;
2995 
2996 		r = -EFAULT;
2997 		if (copy_from_user(&args, argp, sizeof(args)))
2998 			break;
2999 		mutex_lock(&kvm->slots_lock);
3000 		r = kvm_s390_get_cmma_bits(kvm, &args);
3001 		mutex_unlock(&kvm->slots_lock);
3002 		if (!r) {
3003 			r = copy_to_user(argp, &args, sizeof(args));
3004 			if (r)
3005 				r = -EFAULT;
3006 		}
3007 		break;
3008 	}
3009 	case KVM_S390_SET_CMMA_BITS: {
3010 		struct kvm_s390_cmma_log args;
3011 
3012 		r = -EFAULT;
3013 		if (copy_from_user(&args, argp, sizeof(args)))
3014 			break;
3015 		mutex_lock(&kvm->slots_lock);
3016 		r = kvm_s390_set_cmma_bits(kvm, &args);
3017 		mutex_unlock(&kvm->slots_lock);
3018 		break;
3019 	}
3020 	case KVM_S390_PV_COMMAND: {
3021 		struct kvm_pv_cmd args;
3022 
3023 		/* protvirt means user cpu state */
3024 		kvm_s390_set_user_cpu_state_ctrl(kvm);
3025 		r = 0;
3026 		if (!is_prot_virt_host()) {
3027 			r = -EINVAL;
3028 			break;
3029 		}
3030 		if (copy_from_user(&args, argp, sizeof(args))) {
3031 			r = -EFAULT;
3032 			break;
3033 		}
3034 		if (args.flags) {
3035 			r = -EINVAL;
3036 			break;
3037 		}
3038 		/* must be called without kvm->lock */
3039 		r = kvm_s390_handle_pv(kvm, &args);
3040 		if (copy_to_user(argp, &args, sizeof(args))) {
3041 			r = -EFAULT;
3042 			break;
3043 		}
3044 		break;
3045 	}
3046 	case KVM_S390_MEM_OP: {
3047 		struct kvm_s390_mem_op mem_op;
3048 
3049 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
3050 			r = kvm_s390_vm_mem_op(kvm, &mem_op);
3051 		else
3052 			r = -EFAULT;
3053 		break;
3054 	}
3055 	case KVM_S390_KEYOP: {
3056 		struct kvm_s390_mmu_cache *mc;
3057 		struct kvm_s390_keyop kop;
3058 		union skey skey;
3059 
3060 		if (copy_from_user(&kop, argp, sizeof(kop))) {
3061 			r = -EFAULT;
3062 			break;
3063 		}
3064 		skey.skey = kop.key;
3065 
3066 		mc = kvm_s390_new_mmu_cache();
3067 		if (!mc)
3068 			return -ENOMEM;
3069 
3070 		r = kvm_s390_keyop(mc, kvm, kop.operation, kop.guest_addr, skey);
3071 		kvm_s390_free_mmu_cache(mc);
3072 		if (r < 0)
3073 			break;
3074 
3075 		kop.key = r;
3076 		r = 0;
3077 		if (copy_to_user(argp, &kop, sizeof(kop)))
3078 			r = -EFAULT;
3079 		break;
3080 	}
3081 	case KVM_S390_ZPCI_OP: {
3082 		struct kvm_s390_zpci_op args;
3083 
3084 		r = -EINVAL;
3085 		if (!IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
3086 			break;
3087 		if (copy_from_user(&args, argp, sizeof(args))) {
3088 			r = -EFAULT;
3089 			break;
3090 		}
3091 		r = kvm_s390_pci_zpci_op(kvm, &args);
3092 		break;
3093 	}
3094 	default:
3095 		r = -ENOTTY;
3096 	}
3097 
3098 	return r;
3099 }
3100 
3101 static int kvm_s390_apxa_installed(void)
3102 {
3103 	struct ap_config_info info;
3104 
3105 	if (ap_instructions_available()) {
3106 		if (ap_qci(&info) == 0)
3107 			return info.apxa;
3108 	}
3109 
3110 	return 0;
3111 }
3112 
3113 /*
3114  * The format of the crypto control block (CRYCB) is specified in the 3 low
3115  * order bits of the CRYCB designation (CRYCBD) field as follows:
3116  * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
3117  *	     AP extended addressing (APXA) facility are installed.
3118  * Format 1: The APXA facility is not installed but the MSAX3 facility is.
3119  * Format 2: Both the APXA and MSAX3 facilities are installed
3120  */
3121 static void kvm_s390_set_crycb_format(struct kvm *kvm)
3122 {
3123 	kvm->arch.crypto.crycbd = virt_to_phys(kvm->arch.crypto.crycb);
3124 
3125 	/* Clear the CRYCB format bits - i.e., set format 0 by default */
3126 	kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
3127 
3128 	/* Check whether MSAX3 is installed */
3129 	if (!test_kvm_facility(kvm, 76))
3130 		return;
3131 
3132 	if (kvm_s390_apxa_installed())
3133 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
3134 	else
3135 		kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
3136 }
3137 
3138 /*
3139  * kvm_arch_crypto_set_masks
3140  *
3141  * @kvm: pointer to the target guest's KVM struct containing the crypto masks
3142  *	 to be set.
3143  * @apm: the mask identifying the accessible AP adapters
3144  * @aqm: the mask identifying the accessible AP domains
3145  * @adm: the mask identifying the accessible AP control domains
3146  *
3147  * Set the masks that identify the adapters, domains and control domains to
3148  * which the KVM guest is granted access.
3149  *
3150  * Note: The kvm->lock mutex must be locked by the caller before invoking this
3151  *	 function.
3152  */
3153 void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
3154 			       unsigned long *aqm, unsigned long *adm)
3155 {
3156 	struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
3157 
3158 	kvm_s390_vcpu_block_all(kvm);
3159 
3160 	switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
3161 	case CRYCB_FORMAT2: /* APCB1 use 256 bits */
3162 		memcpy(crycb->apcb1.apm, apm, 32);
3163 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
3164 			 apm[0], apm[1], apm[2], apm[3]);
3165 		memcpy(crycb->apcb1.aqm, aqm, 32);
3166 		VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
3167 			 aqm[0], aqm[1], aqm[2], aqm[3]);
3168 		memcpy(crycb->apcb1.adm, adm, 32);
3169 		VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
3170 			 adm[0], adm[1], adm[2], adm[3]);
3171 		break;
3172 	case CRYCB_FORMAT1:
3173 	case CRYCB_FORMAT0: /* Fall through both use APCB0 */
3174 		memcpy(crycb->apcb0.apm, apm, 8);
3175 		memcpy(crycb->apcb0.aqm, aqm, 2);
3176 		memcpy(crycb->apcb0.adm, adm, 2);
3177 		VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
3178 			 apm[0], *((unsigned short *)aqm),
3179 			 *((unsigned short *)adm));
3180 		break;
3181 	default:	/* Can not happen */
3182 		break;
3183 	}
3184 
3185 	/* recreate the shadow crycb for each vcpu */
3186 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
3187 	kvm_s390_vcpu_unblock_all(kvm);
3188 }
3189 EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
3190 
3191 /*
3192  * kvm_arch_crypto_clear_masks
3193  *
3194  * @kvm: pointer to the target guest's KVM struct containing the crypto masks
3195  *	 to be cleared.
3196  *
3197  * Clear the masks that identify the adapters, domains and control domains to
3198  * which the KVM guest is granted access.
3199  *
3200  * Note: The kvm->lock mutex must be locked by the caller before invoking this
3201  *	 function.
3202  */
3203 void kvm_arch_crypto_clear_masks(struct kvm *kvm)
3204 {
3205 	kvm_s390_vcpu_block_all(kvm);
3206 
3207 	memset(&kvm->arch.crypto.crycb->apcb0, 0,
3208 	       sizeof(kvm->arch.crypto.crycb->apcb0));
3209 	memset(&kvm->arch.crypto.crycb->apcb1, 0,
3210 	       sizeof(kvm->arch.crypto.crycb->apcb1));
3211 
3212 	VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
3213 	/* recreate the shadow crycb for each vcpu */
3214 	kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
3215 	kvm_s390_vcpu_unblock_all(kvm);
3216 }
3217 EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
3218 
3219 static u64 kvm_s390_get_initial_cpuid(void)
3220 {
3221 	struct cpuid cpuid;
3222 
3223 	get_cpu_id(&cpuid);
3224 	cpuid.version = 0xff;
3225 	return *((u64 *) &cpuid);
3226 }
3227 
3228 static void kvm_s390_crypto_init(struct kvm *kvm)
3229 {
3230 	kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
3231 	kvm_s390_set_crycb_format(kvm);
3232 	init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
3233 
3234 	if (!test_kvm_facility(kvm, 76))
3235 		return;
3236 
3237 	/* Enable AES/DEA protected key functions by default */
3238 	kvm->arch.crypto.aes_kw = 1;
3239 	kvm->arch.crypto.dea_kw = 1;
3240 	get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
3241 			 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
3242 	get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
3243 			 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
3244 }
3245 
3246 static void sca_dispose(struct kvm *kvm)
3247 {
3248 	free_pages_exact(kvm->arch.sca, sizeof(*kvm->arch.sca));
3249 	kvm->arch.sca = NULL;
3250 }
3251 
3252 void kvm_arch_free_vm(struct kvm *kvm)
3253 {
3254 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM))
3255 		kvm_s390_pci_clear_list(kvm);
3256 
3257 	__kvm_arch_free_vm(kvm);
3258 }
3259 
3260 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
3261 {
3262 	gfp_t alloc_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO;
3263 	char debug_name[16];
3264 	int i, rc;
3265 
3266 	mutex_init(&kvm->arch.pv.import_lock);
3267 
3268 	rc = -EINVAL;
3269 #ifdef CONFIG_KVM_S390_UCONTROL
3270 	if (type & ~KVM_VM_S390_UCONTROL)
3271 		goto out_err;
3272 	if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
3273 		goto out_err;
3274 #else
3275 	if (type)
3276 		goto out_err;
3277 #endif
3278 	rc = -ENOMEM;
3279 
3280 	if (!sclp.has_64bscao)
3281 		alloc_flags |= GFP_DMA;
3282 	mutex_lock(&kvm_lock);
3283 
3284 	kvm->arch.sca = alloc_pages_exact(sizeof(*kvm->arch.sca), alloc_flags);
3285 	mutex_unlock(&kvm_lock);
3286 	if (!kvm->arch.sca)
3287 		goto out_err;
3288 
3289 	snprintf(debug_name, sizeof(debug_name), "kvm-%u", current->pid);
3290 
3291 	kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
3292 	if (!kvm->arch.dbf)
3293 		goto out_err;
3294 
3295 	BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
3296 	kvm->arch.sie_page2 =
3297 	     (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
3298 	if (!kvm->arch.sie_page2)
3299 		goto out_err;
3300 
3301 	kvm->arch.sie_page2->kvm = kvm;
3302 	kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
3303 
3304 	for (i = 0; i < ARRAY_SIZE(kvm_s390_fac_base); i++) {
3305 		kvm->arch.model.fac_mask[i] = stfle_fac_list[i] &
3306 					      kvm_s390_fac_base[i];
3307 		kvm->arch.model.fac_list[i] = stfle_fac_list[i] &
3308 					      kvm_s390_fac_base[i];
3309 	}
3310 	for (i = 0; i < ARRAY_SIZE(kvm_s390_fac_ext); i++) {
3311 		kvm->arch.model.fac_mask[i] |= stfle_fac_list[i] &
3312 					       kvm_s390_fac_ext[i];
3313 	}
3314 	kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
3315 
3316 	/* we are always in czam mode - even on pre z14 machines */
3317 	set_kvm_facility(kvm->arch.model.fac_mask, 138);
3318 	set_kvm_facility(kvm->arch.model.fac_list, 138);
3319 	/* we emulate STHYI in kvm */
3320 	set_kvm_facility(kvm->arch.model.fac_mask, 74);
3321 	set_kvm_facility(kvm->arch.model.fac_list, 74);
3322 	if (machine_has_tlb_guest()) {
3323 		set_kvm_facility(kvm->arch.model.fac_mask, 147);
3324 		set_kvm_facility(kvm->arch.model.fac_list, 147);
3325 	}
3326 
3327 	if (css_general_characteristics.aiv && test_facility(65))
3328 		set_kvm_facility(kvm->arch.model.fac_mask, 65);
3329 
3330 	kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
3331 	kvm->arch.model.ibc = sclp.ibc & 0x0fff;
3332 
3333 	kvm->arch.model.uv_feat_guest.feat = 0;
3334 
3335 	kvm_s390_crypto_init(kvm);
3336 
3337 	if (IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
3338 		mutex_lock(&kvm->lock);
3339 		kvm_s390_pci_init_list(kvm);
3340 		kvm_s390_vcpu_pci_enable_interp(kvm);
3341 		mutex_unlock(&kvm->lock);
3342 	}
3343 
3344 	spin_lock_init(&kvm->arch.float_int.ais_lock);
3345 	spin_lock_init(&kvm->arch.float_int.lock);
3346 	for (i = 0; i < FIRQ_LIST_COUNT; i++)
3347 		INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
3348 	init_waitqueue_head(&kvm->arch.ipte_wq);
3349 	mutex_init(&kvm->arch.ipte_mutex);
3350 
3351 	debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
3352 	VM_EVENT(kvm, 3, "vm created with type %lu", type);
3353 
3354 	kvm->arch.mem_limit = type & KVM_VM_S390_UCONTROL ? KVM_S390_NO_MEM_LIMIT : sclp.hamax + 1;
3355 	kvm->arch.gmap = gmap_new(kvm, gpa_to_gfn(kvm->arch.mem_limit));
3356 	if (!kvm->arch.gmap)
3357 		goto out_err;
3358 	clear_bit(GMAP_FLAG_PFAULT_ENABLED, &kvm->arch.gmap->flags);
3359 
3360 	if (type & KVM_VM_S390_UCONTROL) {
3361 		struct kvm_userspace_memory_region2 fake_memslot = {
3362 			.slot = KVM_S390_UCONTROL_MEMSLOT,
3363 			.guest_phys_addr = 0,
3364 			.userspace_addr = 0,
3365 			.memory_size = ALIGN_DOWN(TASK_SIZE, _SEGMENT_SIZE),
3366 			.flags = 0,
3367 		};
3368 
3369 		/* one flat fake memslot covering the whole address-space */
3370 		mutex_lock(&kvm->slots_lock);
3371 		KVM_BUG_ON(kvm_set_internal_memslot(kvm, &fake_memslot), kvm);
3372 		mutex_unlock(&kvm->slots_lock);
3373 		set_bit(GMAP_FLAG_IS_UCONTROL, &kvm->arch.gmap->flags);
3374 	} else {
3375 		struct crst_table *table = dereference_asce(kvm->arch.gmap->asce);
3376 
3377 		crst_table_init((void *)table, _CRSTE_HOLE(table->crstes[0].h.tt).val);
3378 	}
3379 
3380 	kvm->arch.use_pfmfi = sclp.has_pfmfi;
3381 	kvm->arch.use_skf = sclp.has_skey;
3382 	spin_lock_init(&kvm->arch.start_stop_lock);
3383 	kvm_s390_vsie_init(kvm);
3384 	if (use_gisa)
3385 		kvm_s390_gisa_init(kvm);
3386 	INIT_LIST_HEAD(&kvm->arch.pv.need_cleanup);
3387 	kvm->arch.pv.set_aside = NULL;
3388 	KVM_EVENT(3, "vm 0x%p created by pid %u", kvm, current->pid);
3389 
3390 	return 0;
3391 out_err:
3392 	free_page((unsigned long)kvm->arch.sie_page2);
3393 	debug_unregister(kvm->arch.dbf);
3394 	sca_dispose(kvm);
3395 	KVM_EVENT(3, "creation of vm failed: %d", rc);
3396 	return rc;
3397 }
3398 
3399 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
3400 {
3401 	u16 rc, rrc;
3402 
3403 	VCPU_EVENT(vcpu, 3, "%s", "free cpu");
3404 	trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
3405 	kvm_s390_clear_local_irqs(vcpu);
3406 	kvm_clear_async_pf_completion_queue(vcpu);
3407 	if (!kvm_is_ucontrol(vcpu->kvm))
3408 		sca_del_vcpu(vcpu);
3409 	kvm_s390_update_topology_change_report(vcpu->kvm, 1);
3410 
3411 	if (kvm_is_ucontrol(vcpu->kvm)) {
3412 		scoped_guard(spinlock, &vcpu->kvm->arch.gmap->children_lock)
3413 			gmap_remove_child(vcpu->arch.gmap);
3414 		vcpu->arch.gmap = gmap_put(vcpu->arch.gmap);
3415 	}
3416 
3417 	if (vcpu->kvm->arch.use_cmma)
3418 		kvm_s390_vcpu_unsetup_cmma(vcpu);
3419 	/* We can not hold the vcpu mutex here, we are already dying */
3420 	if (kvm_s390_pv_cpu_get_handle(vcpu))
3421 		kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
3422 	free_page((unsigned long)(vcpu->arch.sie_block));
3423 	kvm_s390_free_mmu_cache(vcpu->arch.mc);
3424 }
3425 
3426 void kvm_arch_destroy_vm(struct kvm *kvm)
3427 {
3428 	u16 rc, rrc;
3429 
3430 	kvm_destroy_vcpus(kvm);
3431 	sca_dispose(kvm);
3432 	kvm_s390_gisa_destroy(kvm);
3433 	/*
3434 	 * We are already at the end of life and kvm->lock is not taken.
3435 	 * This is ok as the file descriptor is closed by now and nobody
3436 	 * can mess with the pv state.
3437 	 */
3438 	kvm_s390_pv_deinit_cleanup_all(kvm, &rc, &rrc);
3439 	/*
3440 	 * Remove the mmu notifier only when the whole KVM VM is torn down,
3441 	 * and only if one was registered to begin with. If the VM is
3442 	 * currently not protected, but has been previously been protected,
3443 	 * then it's possible that the notifier is still registered.
3444 	 */
3445 	if (kvm->arch.pv.mmu_notifier.ops)
3446 		mmu_notifier_unregister(&kvm->arch.pv.mmu_notifier, kvm->mm);
3447 
3448 	debug_unregister(kvm->arch.dbf);
3449 	free_page((unsigned long)kvm->arch.sie_page2);
3450 	kvm_s390_destroy_adapters(kvm);
3451 	kvm_s390_clear_float_irqs(kvm);
3452 	kvm_s390_vsie_destroy(kvm);
3453 	kvm->arch.gmap = gmap_put(kvm->arch.gmap);
3454 	KVM_EVENT(3, "vm 0x%p destroyed", kvm);
3455 }
3456 
3457 /* Section: vcpu related */
3458 static void sca_del_vcpu(struct kvm_vcpu *vcpu)
3459 {
3460 	struct esca_block *sca = vcpu->kvm->arch.sca;
3461 
3462 	if (!kvm_s390_use_sca_entries())
3463 		return;
3464 
3465 	clear_bit_inv(vcpu->vcpu_id, (unsigned long *)sca->mcn);
3466 	sca->cpu[vcpu->vcpu_id].sda = 0;
3467 }
3468 
3469 static void sca_add_vcpu(struct kvm_vcpu *vcpu)
3470 {
3471 	struct esca_block *sca = vcpu->kvm->arch.sca;
3472 	phys_addr_t sca_phys = virt_to_phys(sca);
3473 
3474 	/* we still need the sca header for the ipte control */
3475 	vcpu->arch.sie_block->scaoh = sca_phys >> 32;
3476 	vcpu->arch.sie_block->scaol = sca_phys & ESCA_SCAOL_MASK;
3477 	vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
3478 
3479 	if (!kvm_s390_use_sca_entries())
3480 		return;
3481 
3482 	set_bit_inv(vcpu->vcpu_id, (unsigned long *)sca->mcn);
3483 	sca->cpu[vcpu->vcpu_id].sda = virt_to_phys(vcpu->arch.sie_block);
3484 }
3485 
3486 static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
3487 {
3488 	if (!kvm_s390_use_sca_entries())
3489 		return id < KVM_MAX_VCPUS;
3490 
3491 	return id < KVM_S390_ESCA_CPU_SLOTS;
3492 }
3493 
3494 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3495 static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3496 {
3497 	WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
3498 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3499 	vcpu->arch.cputm_start = get_tod_clock_fast();
3500 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3501 }
3502 
3503 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3504 static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3505 {
3506 	WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
3507 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3508 	vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3509 	vcpu->arch.cputm_start = 0;
3510 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3511 }
3512 
3513 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3514 static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3515 {
3516 	WARN_ON_ONCE(vcpu->arch.cputm_enabled);
3517 	vcpu->arch.cputm_enabled = true;
3518 	__start_cpu_timer_accounting(vcpu);
3519 }
3520 
3521 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3522 static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3523 {
3524 	WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
3525 	__stop_cpu_timer_accounting(vcpu);
3526 	vcpu->arch.cputm_enabled = false;
3527 }
3528 
3529 static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3530 {
3531 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3532 	__enable_cpu_timer_accounting(vcpu);
3533 	preempt_enable();
3534 }
3535 
3536 static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3537 {
3538 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3539 	__disable_cpu_timer_accounting(vcpu);
3540 	preempt_enable();
3541 }
3542 
3543 /* set the cpu timer - may only be called from the VCPU thread itself */
3544 void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
3545 {
3546 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3547 	raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3548 	if (vcpu->arch.cputm_enabled)
3549 		vcpu->arch.cputm_start = get_tod_clock_fast();
3550 	vcpu->arch.sie_block->cputm = cputm;
3551 	raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3552 	preempt_enable();
3553 }
3554 
3555 /* update and get the cpu timer - can also be called from other VCPU threads */
3556 __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
3557 {
3558 	unsigned int seq;
3559 	__u64 value;
3560 
3561 	if (unlikely(!vcpu->arch.cputm_enabled))
3562 		return vcpu->arch.sie_block->cputm;
3563 
3564 	preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3565 	do {
3566 		seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
3567 		/*
3568 		 * If the writer would ever execute a read in the critical
3569 		 * section, e.g. in irq context, we have a deadlock.
3570 		 */
3571 		WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3572 		value = vcpu->arch.sie_block->cputm;
3573 		/* if cputm_start is 0, accounting is being started/stopped */
3574 		if (likely(vcpu->arch.cputm_start))
3575 			value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3576 	} while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
3577 	preempt_enable();
3578 	return value;
3579 }
3580 
3581 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3582 {
3583 
3584 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
3585 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3586 		__start_cpu_timer_accounting(vcpu);
3587 	vcpu->cpu = cpu;
3588 }
3589 
3590 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3591 {
3592 	vcpu->cpu = -1;
3593 	if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3594 		__stop_cpu_timer_accounting(vcpu);
3595 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
3596 
3597 }
3598 
3599 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
3600 {
3601 	mutex_lock(&vcpu->kvm->lock);
3602 	preempt_disable();
3603 	vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3604 	vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3605 	preempt_enable();
3606 	mutex_unlock(&vcpu->kvm->lock);
3607 	if (!kvm_is_ucontrol(vcpu->kvm)) {
3608 		vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3609 		sca_add_vcpu(vcpu);
3610 	}
3611 	if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0 ||
3612 	    vcpu->kvm->arch.user_operexec)
3613 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
3614 }
3615 
3616 static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
3617 {
3618 	if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
3619 	    test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
3620 		return true;
3621 	return false;
3622 }
3623 
3624 static bool kvm_has_pckmo_ecc(struct kvm *kvm)
3625 {
3626 	/* At least one ECC subfunction must be present */
3627 	return kvm_has_pckmo_subfunc(kvm, 32) ||
3628 	       kvm_has_pckmo_subfunc(kvm, 33) ||
3629 	       kvm_has_pckmo_subfunc(kvm, 34) ||
3630 	       kvm_has_pckmo_subfunc(kvm, 40) ||
3631 	       kvm_has_pckmo_subfunc(kvm, 41);
3632 
3633 }
3634 
3635 static bool kvm_has_pckmo_hmac(struct kvm *kvm)
3636 {
3637 	/* At least one HMAC subfunction must be present */
3638 	return kvm_has_pckmo_subfunc(kvm, 118) ||
3639 	       kvm_has_pckmo_subfunc(kvm, 122);
3640 }
3641 
3642 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
3643 {
3644 	/*
3645 	 * If the AP instructions are not being interpreted and the MSAX3
3646 	 * facility is not configured for the guest, there is nothing to set up.
3647 	 */
3648 	if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
3649 		return;
3650 
3651 	vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3652 	vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
3653 	vcpu->arch.sie_block->eca &= ~ECA_APIE;
3654 	vcpu->arch.sie_block->ecd &= ~(ECD_ECC | ECD_HMAC);
3655 
3656 	if (vcpu->kvm->arch.crypto.apie)
3657 		vcpu->arch.sie_block->eca |= ECA_APIE;
3658 
3659 	/* Set up protected key support */
3660 	if (vcpu->kvm->arch.crypto.aes_kw) {
3661 		vcpu->arch.sie_block->ecb3 |= ECB3_AES;
3662 		/* ecc/hmac is also wrapped with AES key */
3663 		if (kvm_has_pckmo_ecc(vcpu->kvm))
3664 			vcpu->arch.sie_block->ecd |= ECD_ECC;
3665 		if (kvm_has_pckmo_hmac(vcpu->kvm))
3666 			vcpu->arch.sie_block->ecd |= ECD_HMAC;
3667 	}
3668 
3669 	if (vcpu->kvm->arch.crypto.dea_kw)
3670 		vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
3671 }
3672 
3673 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3674 {
3675 	free_page((unsigned long)phys_to_virt(vcpu->arch.sie_block->cbrlo));
3676 	vcpu->arch.sie_block->cbrlo = 0;
3677 }
3678 
3679 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3680 {
3681 	void *cbrlo_page = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
3682 
3683 	if (!cbrlo_page)
3684 		return -ENOMEM;
3685 
3686 	vcpu->arch.sie_block->cbrlo = virt_to_phys(cbrlo_page);
3687 	return 0;
3688 }
3689 
3690 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
3691 {
3692 	struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
3693 
3694 	vcpu->arch.sie_block->ibc = model->ibc;
3695 	if (test_kvm_facility(vcpu->kvm, 7))
3696 		vcpu->arch.sie_block->fac = virt_to_phys(model->fac_list);
3697 }
3698 
3699 static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3700 {
3701 	int rc = 0;
3702 	u16 uvrc, uvrrc;
3703 
3704 	atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3705 						    CPUSTAT_SM |
3706 						    CPUSTAT_STOPPED);
3707 
3708 	if (test_kvm_facility(vcpu->kvm, 78))
3709 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
3710 	else if (test_kvm_facility(vcpu->kvm, 8))
3711 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3712 
3713 	kvm_s390_vcpu_setup_model(vcpu);
3714 
3715 	/* pgste_set_pte has special handling for !machine_has_esop() */
3716 	if (machine_has_esop())
3717 		vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3718 	if (test_kvm_facility(vcpu->kvm, 9))
3719 		vcpu->arch.sie_block->ecb |= ECB_SRSI;
3720 	if (test_kvm_facility(vcpu->kvm, 11))
3721 		vcpu->arch.sie_block->ecb |= ECB_PTF;
3722 	if (test_kvm_facility(vcpu->kvm, 73))
3723 		vcpu->arch.sie_block->ecb |= ECB_TE;
3724 	if (!kvm_is_ucontrol(vcpu->kvm))
3725 		vcpu->arch.sie_block->ecb |= ECB_SPECI;
3726 
3727 	if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
3728 		vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3729 	if (test_kvm_facility(vcpu->kvm, 130))
3730 		vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
3731 	vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
3732 	if (sclp.has_cei)
3733 		vcpu->arch.sie_block->eca |= ECA_CEI;
3734 	if (sclp.has_ib)
3735 		vcpu->arch.sie_block->eca |= ECA_IB;
3736 	if (sclp.has_siif)
3737 		vcpu->arch.sie_block->eca |= ECA_SII;
3738 	if (kvm_s390_use_sca_entries())
3739 		vcpu->arch.sie_block->eca |= ECA_SIGPI;
3740 	if (test_kvm_facility(vcpu->kvm, 129)) {
3741 		vcpu->arch.sie_block->eca |= ECA_VX;
3742 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
3743 	}
3744 	if (test_kvm_facility(vcpu->kvm, 139))
3745 		vcpu->arch.sie_block->ecd |= ECD_MEF;
3746 	if (test_kvm_facility(vcpu->kvm, 156))
3747 		vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3748 	if (vcpu->arch.sie_block->gd) {
3749 		vcpu->arch.sie_block->eca |= ECA_AIV;
3750 		VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3751 			   vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3752 	}
3753 	vcpu->arch.sie_block->sdnxo = virt_to_phys(&vcpu->run->s.regs.sdnx) | SDNXC;
3754 	vcpu->arch.sie_block->riccbd = virt_to_phys(&vcpu->run->s.regs.riccb);
3755 
3756 	if (sclp.has_kss)
3757 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3758 	else
3759 		vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
3760 
3761 	if (vcpu->kvm->arch.use_cmma) {
3762 		rc = kvm_s390_vcpu_setup_cmma(vcpu);
3763 		if (rc)
3764 			return rc;
3765 	}
3766 	hrtimer_setup(&vcpu->arch.ckc_timer, kvm_s390_idle_wakeup, CLOCK_MONOTONIC,
3767 		      HRTIMER_MODE_REL);
3768 
3769 	vcpu->arch.sie_block->hpid = HPID_KVM;
3770 
3771 	kvm_s390_vcpu_crypto_setup(vcpu);
3772 
3773 	kvm_s390_vcpu_pci_setup(vcpu);
3774 
3775 	mutex_lock(&vcpu->kvm->lock);
3776 	if (kvm_s390_pv_is_protected(vcpu->kvm)) {
3777 		rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
3778 		if (rc)
3779 			kvm_s390_vcpu_unsetup_cmma(vcpu);
3780 	}
3781 	mutex_unlock(&vcpu->kvm->lock);
3782 
3783 	return rc;
3784 }
3785 
3786 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3787 {
3788 	if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3789 		return -EINVAL;
3790 	return 0;
3791 }
3792 
3793 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3794 {
3795 	struct sie_page *sie_page;
3796 	int rc;
3797 
3798 	BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
3799 	vcpu->arch.mc = kvm_s390_new_mmu_cache();
3800 	if (!vcpu->arch.mc)
3801 		return -ENOMEM;
3802 	sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
3803 	if (!sie_page) {
3804 		kvm_s390_free_mmu_cache(vcpu->arch.mc);
3805 		vcpu->arch.mc = NULL;
3806 		return -ENOMEM;
3807 	}
3808 
3809 	vcpu->arch.sie_block = &sie_page->sie_block;
3810 	vcpu->arch.sie_block->itdba = virt_to_phys(&sie_page->itdb);
3811 
3812 	/* the real guest size will always be smaller than msl */
3813 	vcpu->arch.sie_block->mso = 0;
3814 	vcpu->arch.sie_block->msl = sclp.hamax;
3815 
3816 	vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3817 	spin_lock_init(&vcpu->arch.local_int.lock);
3818 	vcpu->arch.sie_block->gd = kvm_s390_get_gisa_desc(vcpu->kvm);
3819 	seqcount_init(&vcpu->arch.cputm_seqcount);
3820 
3821 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3822 	kvm_clear_async_pf_completion_queue(vcpu);
3823 	vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3824 				    KVM_SYNC_GPRS |
3825 				    KVM_SYNC_ACRS |
3826 				    KVM_SYNC_CRS |
3827 				    KVM_SYNC_ARCH0 |
3828 				    KVM_SYNC_PFAULT |
3829 				    KVM_SYNC_DIAG318;
3830 	vcpu->arch.acrs_loaded = false;
3831 	kvm_s390_set_prefix(vcpu, 0);
3832 	if (test_kvm_facility(vcpu->kvm, 64))
3833 		vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3834 	if (test_kvm_facility(vcpu->kvm, 82))
3835 		vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3836 	if (test_kvm_facility(vcpu->kvm, 133))
3837 		vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3838 	if (test_kvm_facility(vcpu->kvm, 156))
3839 		vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3840 	/* fprs can be synchronized via vrs, even if the guest has no vx. With
3841 	 * cpu_has_vx(), (load|store)_fpu_regs() will work with vrs format.
3842 	 */
3843 	if (cpu_has_vx())
3844 		vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3845 	else
3846 		vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3847 
3848 	if (kvm_is_ucontrol(vcpu->kvm)) {
3849 		rc = -ENOMEM;
3850 		vcpu->arch.gmap = gmap_new_child(vcpu->kvm->arch.gmap, -1UL);
3851 		if (!vcpu->arch.gmap)
3852 			goto out_free_sie_block;
3853 	}
3854 
3855 	VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%p, sie block at 0x%p",
3856 		 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3857 	trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3858 
3859 	rc = kvm_s390_vcpu_setup(vcpu);
3860 	if (rc)
3861 		goto out_ucontrol_uninit;
3862 
3863 	kvm_s390_update_topology_change_report(vcpu->kvm, 1);
3864 	return 0;
3865 
3866 out_ucontrol_uninit:
3867 	if (kvm_is_ucontrol(vcpu->kvm)) {
3868 		gmap_remove_child(vcpu->arch.gmap);
3869 		vcpu->arch.gmap = gmap_put(vcpu->arch.gmap);
3870 	}
3871 out_free_sie_block:
3872 	free_page((unsigned long)(vcpu->arch.sie_block));
3873 	return rc;
3874 }
3875 
3876 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3877 {
3878 	clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
3879 	return kvm_s390_vcpu_has_irq(vcpu, 0);
3880 }
3881 
3882 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
3883 {
3884 	return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3885 }
3886 
3887 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
3888 {
3889 	atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3890 	exit_sie(vcpu);
3891 }
3892 
3893 void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
3894 {
3895 	atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3896 }
3897 
3898 static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
3899 {
3900 	atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3901 	exit_sie(vcpu);
3902 }
3903 
3904 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
3905 {
3906 	return atomic_read(&vcpu->arch.sie_block->prog20) &
3907 	       (PROG_BLOCK_SIE | PROG_REQUEST);
3908 }
3909 
3910 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
3911 {
3912 	atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3913 }
3914 
3915 /*
3916  * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
3917  * If the CPU is not running (e.g. waiting as idle) the function will
3918  * return immediately. */
3919 void exit_sie(struct kvm_vcpu *vcpu)
3920 {
3921 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
3922 	kvm_s390_vsie_kick(vcpu);
3923 	while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
3924 		cpu_relax();
3925 }
3926 
3927 /* Kick a guest cpu out of SIE to process a request synchronously */
3928 void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
3929 {
3930 	__kvm_make_request(req, vcpu);
3931 	kvm_s390_vcpu_request(vcpu);
3932 }
3933 
3934 bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
3935 {
3936 	/* do not poll with more than halt_poll_max_steal percent of steal time */
3937 	if (get_lowcore()->avg_steal_timer * 100 / (TICK_USEC << 12) >=
3938 	    READ_ONCE(halt_poll_max_steal)) {
3939 		vcpu->stat.halt_no_poll_steal++;
3940 		return true;
3941 	}
3942 	return false;
3943 }
3944 
3945 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3946 {
3947 	/* kvm common code refers to this, but never calls it */
3948 	BUG();
3949 	return 0;
3950 }
3951 
3952 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
3953 					   struct kvm_one_reg *reg)
3954 {
3955 	int r = -EINVAL;
3956 
3957 	switch (reg->id) {
3958 	case KVM_REG_S390_TODPR:
3959 		r = put_user(vcpu->arch.sie_block->todpr,
3960 			     (u32 __user *)reg->addr);
3961 		break;
3962 	case KVM_REG_S390_EPOCHDIFF:
3963 		r = put_user(vcpu->arch.sie_block->epoch,
3964 			     (u64 __user *)reg->addr);
3965 		break;
3966 	case KVM_REG_S390_CPU_TIMER:
3967 		r = put_user(kvm_s390_get_cpu_timer(vcpu),
3968 			     (u64 __user *)reg->addr);
3969 		break;
3970 	case KVM_REG_S390_CLOCK_COMP:
3971 		r = put_user(vcpu->arch.sie_block->ckc,
3972 			     (u64 __user *)reg->addr);
3973 		break;
3974 	case KVM_REG_S390_PFTOKEN:
3975 		r = put_user(vcpu->arch.pfault_token,
3976 			     (u64 __user *)reg->addr);
3977 		break;
3978 	case KVM_REG_S390_PFCOMPARE:
3979 		r = put_user(vcpu->arch.pfault_compare,
3980 			     (u64 __user *)reg->addr);
3981 		break;
3982 	case KVM_REG_S390_PFSELECT:
3983 		r = put_user(vcpu->arch.pfault_select,
3984 			     (u64 __user *)reg->addr);
3985 		break;
3986 	case KVM_REG_S390_PP:
3987 		r = put_user(vcpu->arch.sie_block->pp,
3988 			     (u64 __user *)reg->addr);
3989 		break;
3990 	case KVM_REG_S390_GBEA:
3991 		r = put_user(vcpu->arch.sie_block->gbea,
3992 			     (u64 __user *)reg->addr);
3993 		break;
3994 	default:
3995 		break;
3996 	}
3997 
3998 	return r;
3999 }
4000 
4001 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
4002 					   struct kvm_one_reg *reg)
4003 {
4004 	int r = -EINVAL;
4005 	__u64 val;
4006 
4007 	switch (reg->id) {
4008 	case KVM_REG_S390_TODPR:
4009 		r = get_user(vcpu->arch.sie_block->todpr,
4010 			     (u32 __user *)reg->addr);
4011 		break;
4012 	case KVM_REG_S390_EPOCHDIFF:
4013 		r = get_user(vcpu->arch.sie_block->epoch,
4014 			     (u64 __user *)reg->addr);
4015 		break;
4016 	case KVM_REG_S390_CPU_TIMER:
4017 		r = get_user(val, (u64 __user *)reg->addr);
4018 		if (!r)
4019 			kvm_s390_set_cpu_timer(vcpu, val);
4020 		break;
4021 	case KVM_REG_S390_CLOCK_COMP:
4022 		r = get_user(vcpu->arch.sie_block->ckc,
4023 			     (u64 __user *)reg->addr);
4024 		break;
4025 	case KVM_REG_S390_PFTOKEN:
4026 		r = get_user(vcpu->arch.pfault_token,
4027 			     (u64 __user *)reg->addr);
4028 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4029 			kvm_clear_async_pf_completion_queue(vcpu);
4030 		break;
4031 	case KVM_REG_S390_PFCOMPARE:
4032 		r = get_user(vcpu->arch.pfault_compare,
4033 			     (u64 __user *)reg->addr);
4034 		break;
4035 	case KVM_REG_S390_PFSELECT:
4036 		r = get_user(vcpu->arch.pfault_select,
4037 			     (u64 __user *)reg->addr);
4038 		break;
4039 	case KVM_REG_S390_PP:
4040 		r = get_user(vcpu->arch.sie_block->pp,
4041 			     (u64 __user *)reg->addr);
4042 		break;
4043 	case KVM_REG_S390_GBEA:
4044 		r = get_user(vcpu->arch.sie_block->gbea,
4045 			     (u64 __user *)reg->addr);
4046 		break;
4047 	default:
4048 		break;
4049 	}
4050 
4051 	return r;
4052 }
4053 
4054 static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
4055 {
4056 	vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
4057 	vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
4058 	memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
4059 
4060 	kvm_clear_async_pf_completion_queue(vcpu);
4061 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
4062 		kvm_s390_vcpu_stop(vcpu);
4063 	kvm_s390_clear_local_irqs(vcpu);
4064 }
4065 
4066 static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
4067 {
4068 	/* Initial reset is a superset of the normal reset */
4069 	kvm_arch_vcpu_ioctl_normal_reset(vcpu);
4070 
4071 	/*
4072 	 * This equals initial cpu reset in pop, but we don't switch to ESA.
4073 	 * We do not only reset the internal data, but also ...
4074 	 */
4075 	vcpu->arch.sie_block->gpsw.mask = 0;
4076 	vcpu->arch.sie_block->gpsw.addr = 0;
4077 	kvm_s390_set_prefix(vcpu, 0);
4078 	kvm_s390_set_cpu_timer(vcpu, 0);
4079 	vcpu->arch.sie_block->ckc = 0;
4080 	memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
4081 	vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
4082 	vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
4083 
4084 	/* ... the data in sync regs */
4085 	memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
4086 	vcpu->run->s.regs.ckc = 0;
4087 	vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
4088 	vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
4089 	vcpu->run->psw_addr = 0;
4090 	vcpu->run->psw_mask = 0;
4091 	vcpu->run->s.regs.todpr = 0;
4092 	vcpu->run->s.regs.cputm = 0;
4093 	vcpu->run->s.regs.ckc = 0;
4094 	vcpu->run->s.regs.pp = 0;
4095 	vcpu->run->s.regs.gbea = 1;
4096 	vcpu->run->s.regs.fpc = 0;
4097 	/*
4098 	 * Do not reset these registers in the protected case, as some of
4099 	 * them are overlaid and they are not accessible in this case
4100 	 * anyway.
4101 	 */
4102 	if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
4103 		vcpu->arch.sie_block->gbea = 1;
4104 		vcpu->arch.sie_block->pp = 0;
4105 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
4106 		vcpu->arch.sie_block->todpr = 0;
4107 	}
4108 }
4109 
4110 static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
4111 {
4112 	struct kvm_sync_regs *regs = &vcpu->run->s.regs;
4113 
4114 	/* Clear reset is a superset of the initial reset */
4115 	kvm_arch_vcpu_ioctl_initial_reset(vcpu);
4116 
4117 	memset(&regs->gprs, 0, sizeof(regs->gprs));
4118 	memset(&regs->vrs, 0, sizeof(regs->vrs));
4119 	memset(&regs->acrs, 0, sizeof(regs->acrs));
4120 	memset(&regs->gscb, 0, sizeof(regs->gscb));
4121 
4122 	regs->etoken = 0;
4123 	regs->etoken_extension = 0;
4124 }
4125 
4126 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4127 {
4128 	vcpu_load(vcpu);
4129 	memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
4130 	vcpu_put(vcpu);
4131 	return 0;
4132 }
4133 
4134 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
4135 {
4136 	vcpu_load(vcpu);
4137 	memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
4138 	vcpu_put(vcpu);
4139 	return 0;
4140 }
4141 
4142 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
4143 				  struct kvm_sregs *sregs)
4144 {
4145 	vcpu_load(vcpu);
4146 
4147 	memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
4148 	memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
4149 
4150 	vcpu_put(vcpu);
4151 	return 0;
4152 }
4153 
4154 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
4155 				  struct kvm_sregs *sregs)
4156 {
4157 	vcpu_load(vcpu);
4158 
4159 	memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
4160 	memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
4161 
4162 	vcpu_put(vcpu);
4163 	return 0;
4164 }
4165 
4166 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4167 {
4168 	vcpu_load(vcpu);
4169 
4170 	vcpu->run->s.regs.fpc = fpu->fpc;
4171 	if (cpu_has_vx())
4172 		convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
4173 				 (freg_t *) fpu->fprs);
4174 	else
4175 		memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
4176 
4177 	vcpu_put(vcpu);
4178 	return 0;
4179 }
4180 
4181 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
4182 {
4183 	vcpu_load(vcpu);
4184 
4185 	if (cpu_has_vx())
4186 		convert_vx_to_fp((freg_t *) fpu->fprs,
4187 				 (__vector128 *) vcpu->run->s.regs.vrs);
4188 	else
4189 		memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
4190 	fpu->fpc = vcpu->run->s.regs.fpc;
4191 
4192 	vcpu_put(vcpu);
4193 	return 0;
4194 }
4195 
4196 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
4197 {
4198 	int rc = 0;
4199 
4200 	if (!is_vcpu_stopped(vcpu))
4201 		rc = -EBUSY;
4202 	else {
4203 		vcpu->run->psw_mask = psw.mask;
4204 		vcpu->run->psw_addr = psw.addr;
4205 	}
4206 	return rc;
4207 }
4208 
4209 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
4210 				  struct kvm_translation *tr)
4211 {
4212 	return -EINVAL; /* not implemented yet */
4213 }
4214 
4215 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
4216 			      KVM_GUESTDBG_USE_HW_BP | \
4217 			      KVM_GUESTDBG_ENABLE)
4218 
4219 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
4220 					struct kvm_guest_debug *dbg)
4221 {
4222 	int rc = 0;
4223 
4224 	vcpu_load(vcpu);
4225 
4226 	vcpu->guest_debug = 0;
4227 	kvm_s390_clear_bp_data(vcpu);
4228 
4229 	if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
4230 		rc = -EINVAL;
4231 		goto out;
4232 	}
4233 	if (!sclp.has_gpere) {
4234 		rc = -EINVAL;
4235 		goto out;
4236 	}
4237 
4238 	if (dbg->control & KVM_GUESTDBG_ENABLE) {
4239 		vcpu->guest_debug = dbg->control;
4240 		/* enforce guest PER */
4241 		kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
4242 
4243 		if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
4244 			rc = kvm_s390_import_bp_data(vcpu, dbg);
4245 	} else {
4246 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
4247 		vcpu->arch.guestdbg.last_bp = 0;
4248 	}
4249 
4250 	if (rc) {
4251 		vcpu->guest_debug = 0;
4252 		kvm_s390_clear_bp_data(vcpu);
4253 		kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
4254 	}
4255 
4256 out:
4257 	vcpu_put(vcpu);
4258 	return rc;
4259 }
4260 
4261 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
4262 				    struct kvm_mp_state *mp_state)
4263 {
4264 	int ret;
4265 
4266 	vcpu_load(vcpu);
4267 
4268 	/* CHECK_STOP and LOAD are not supported yet */
4269 	ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
4270 				      KVM_MP_STATE_OPERATING;
4271 
4272 	vcpu_put(vcpu);
4273 	return ret;
4274 }
4275 
4276 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
4277 				    struct kvm_mp_state *mp_state)
4278 {
4279 	int rc = 0;
4280 
4281 	vcpu_load(vcpu);
4282 
4283 	/* user space knows about this interface - let it control the state */
4284 	kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm);
4285 
4286 	switch (mp_state->mp_state) {
4287 	case KVM_MP_STATE_STOPPED:
4288 		rc = kvm_s390_vcpu_stop(vcpu);
4289 		break;
4290 	case KVM_MP_STATE_OPERATING:
4291 		rc = kvm_s390_vcpu_start(vcpu);
4292 		break;
4293 	case KVM_MP_STATE_LOAD:
4294 		if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
4295 			rc = -ENXIO;
4296 			break;
4297 		}
4298 		rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
4299 		break;
4300 	case KVM_MP_STATE_CHECK_STOP:
4301 		fallthrough;	/* CHECK_STOP and LOAD are not supported yet */
4302 	default:
4303 		rc = -ENXIO;
4304 	}
4305 
4306 	vcpu_put(vcpu);
4307 	return rc;
4308 }
4309 
4310 static bool ibs_enabled(struct kvm_vcpu *vcpu)
4311 {
4312 	return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
4313 }
4314 
4315 static int vcpu_ucontrol_translate(struct kvm_vcpu *vcpu, gpa_t *gaddr)
4316 {
4317 	int rc;
4318 
4319 	if (kvm_is_ucontrol(vcpu->kvm)) {
4320 		rc = gmap_ucas_translate(vcpu->arch.mc, vcpu->arch.gmap, gaddr);
4321 		if (rc == -EREMOTE) {
4322 			vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4323 			vcpu->run->s390_ucontrol.trans_exc_code = *gaddr;
4324 			vcpu->run->s390_ucontrol.pgm_code = PGM_SEGMENT_TRANSLATION;
4325 		}
4326 		return rc;
4327 	}
4328 	return 0;
4329 }
4330 
4331 static int kvm_s390_fixup_prefix(struct kvm_vcpu *vcpu)
4332 {
4333 	gpa_t gaddr = kvm_s390_get_prefix(vcpu);
4334 	gfn_t gfn;
4335 	int rc;
4336 
4337 	if (vcpu_ucontrol_translate(vcpu, &gaddr))
4338 		return -EREMOTE;
4339 	gfn = gpa_to_gfn(gaddr);
4340 
4341 	rc = kvm_s390_faultin_gfn_simple(vcpu, NULL, gfn, true);
4342 	if (rc)
4343 		return rc;
4344 	rc = kvm_s390_faultin_gfn_simple(vcpu, NULL, gfn + 1, true);
4345 	if (rc)
4346 		return rc;
4347 
4348 	scoped_guard(write_lock, &vcpu->kvm->mmu_lock)
4349 		rc = dat_set_prefix_notif_bit(vcpu->kvm->arch.gmap->asce, gfn);
4350 	return rc;
4351 }
4352 
4353 static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
4354 {
4355 retry:
4356 	kvm_s390_vcpu_request_handled(vcpu);
4357 	if (!kvm_request_pending(vcpu))
4358 		return 0;
4359 	/*
4360 	 * If the guest prefix changed, re-arm the ipte notifier for the
4361 	 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
4362 	 * This ensures that the ipte instruction for this request has
4363 	 * already finished. We might race against a second unmapper that
4364 	 * wants to set the blocking bit. Lets just retry the request loop.
4365 	 */
4366 	if (kvm_check_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu)) {
4367 		int rc;
4368 
4369 		rc = kvm_s390_fixup_prefix(vcpu);
4370 		if (rc) {
4371 			kvm_make_request(KVM_REQ_REFRESH_GUEST_PREFIX, vcpu);
4372 			return rc;
4373 		}
4374 		goto retry;
4375 	}
4376 
4377 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
4378 		vcpu->arch.sie_block->ihcpu = 0xffff;
4379 		goto retry;
4380 	}
4381 
4382 	if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
4383 		if (!ibs_enabled(vcpu)) {
4384 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
4385 			kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
4386 		}
4387 		goto retry;
4388 	}
4389 
4390 	if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
4391 		if (ibs_enabled(vcpu)) {
4392 			trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
4393 			kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
4394 		}
4395 		goto retry;
4396 	}
4397 
4398 	if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
4399 		vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
4400 		goto retry;
4401 	}
4402 
4403 	if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
4404 		/*
4405 		 * Disable CMM virtualization; we will emulate the ESSA
4406 		 * instruction manually, in order to provide additional
4407 		 * functionalities needed for live migration.
4408 		 */
4409 		vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
4410 		goto retry;
4411 	}
4412 
4413 	if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
4414 		/*
4415 		 * Re-enable CMM virtualization if CMMA is available and
4416 		 * CMM has been used.
4417 		 */
4418 		if (vcpu->kvm->arch.use_cmma && uses_cmm(vcpu->arch.gmap))
4419 			vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
4420 		goto retry;
4421 	}
4422 
4423 	/* we left the vsie handler, nothing to do, just clear the request */
4424 	kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
4425 
4426 	return 0;
4427 }
4428 
4429 static void __kvm_s390_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
4430 {
4431 	struct kvm_vcpu *vcpu;
4432 	union tod_clock clk;
4433 	unsigned long i;
4434 
4435 	preempt_disable();
4436 
4437 	store_tod_clock_ext(&clk);
4438 
4439 	kvm->arch.epoch = gtod->tod - clk.tod;
4440 	kvm->arch.epdx = 0;
4441 	if (test_kvm_facility(kvm, 139)) {
4442 		kvm->arch.epdx = gtod->epoch_idx - clk.ei;
4443 		if (kvm->arch.epoch > gtod->tod)
4444 			kvm->arch.epdx -= 1;
4445 	}
4446 
4447 	kvm_s390_vcpu_block_all(kvm);
4448 	kvm_for_each_vcpu(i, vcpu, kvm) {
4449 		vcpu->arch.sie_block->epoch = kvm->arch.epoch;
4450 		vcpu->arch.sie_block->epdx  = kvm->arch.epdx;
4451 	}
4452 
4453 	kvm_s390_vcpu_unblock_all(kvm);
4454 	preempt_enable();
4455 }
4456 
4457 int kvm_s390_try_set_tod_clock(struct kvm *kvm, const struct kvm_s390_vm_tod_clock *gtod)
4458 {
4459 	if (!mutex_trylock(&kvm->lock))
4460 		return 0;
4461 	__kvm_s390_set_tod_clock(kvm, gtod);
4462 	mutex_unlock(&kvm->lock);
4463 	return 1;
4464 }
4465 
4466 static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
4467 				     unsigned long token)
4468 {
4469 	struct kvm_s390_interrupt inti;
4470 	struct kvm_s390_irq irq;
4471 	struct kvm_s390_interrupt_info *inti_mem = NULL;
4472 	int ret = 0;
4473 
4474 	if (start_token) {
4475 		irq.u.ext.ext_params2 = token;
4476 		irq.type = KVM_S390_INT_PFAULT_INIT;
4477 		WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
4478 	} else {
4479 		inti_mem = kzalloc_obj(*inti_mem, GFP_KERNEL_ACCOUNT);
4480 		if (WARN_ON_ONCE(!inti_mem))
4481 			return;
4482 
4483 		inti.type = KVM_S390_INT_PFAULT_DONE;
4484 		inti.parm64 = token;
4485 		ret = kvm_s390_inject_vm(vcpu->kvm, &inti, inti_mem);
4486 		if (ret)
4487 			kfree(inti_mem);
4488 		WARN_ON_ONCE(ret);
4489 	}
4490 }
4491 
4492 bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
4493 				     struct kvm_async_pf *work)
4494 {
4495 	trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
4496 	__kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
4497 
4498 	return true;
4499 }
4500 
4501 void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
4502 				 struct kvm_async_pf *work)
4503 {
4504 	trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
4505 	__kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
4506 }
4507 
4508 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
4509 			       struct kvm_async_pf *work)
4510 {
4511 	/* s390 will always inject the page directly */
4512 }
4513 
4514 bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
4515 {
4516 	/*
4517 	 * s390 will always inject the page directly,
4518 	 * but we still want check_async_completion to cleanup
4519 	 */
4520 	return true;
4521 }
4522 
4523 bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
4524 {
4525 	hva_t hva;
4526 	struct kvm_arch_async_pf arch;
4527 
4528 	if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4529 		return false;
4530 	if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
4531 	    vcpu->arch.pfault_compare)
4532 		return false;
4533 	if (psw_extint_disabled(vcpu))
4534 		return false;
4535 	if (kvm_s390_vcpu_has_irq(vcpu, 0))
4536 		return false;
4537 	if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
4538 		return false;
4539 	if (!pfault_enabled(vcpu->arch.gmap))
4540 		return false;
4541 
4542 	hva = gfn_to_hva(vcpu->kvm, current->thread.gmap_teid.addr);
4543 	if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
4544 		return false;
4545 
4546 	return kvm_setup_async_pf(vcpu, current->thread.gmap_teid.addr * PAGE_SIZE, hva, &arch);
4547 }
4548 
4549 static int vcpu_pre_run(struct kvm_vcpu *vcpu)
4550 {
4551 	int rc, cpuflags;
4552 
4553 	/*
4554 	 * On s390 notifications for arriving pages will be delivered directly
4555 	 * to the guest but the house keeping for completed pfaults is
4556 	 * handled outside the worker.
4557 	 */
4558 	kvm_check_async_pf_completion(vcpu);
4559 
4560 	vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
4561 	vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
4562 
4563 	if (!kvm_is_ucontrol(vcpu->kvm)) {
4564 		rc = kvm_s390_deliver_pending_interrupts(vcpu);
4565 		if (rc || guestdbg_exit_pending(vcpu))
4566 			return rc;
4567 	}
4568 
4569 	rc = kvm_s390_handle_requests(vcpu);
4570 	if (rc)
4571 		return rc;
4572 
4573 	if (guestdbg_enabled(vcpu)) {
4574 		kvm_s390_backup_guest_per_regs(vcpu);
4575 		kvm_s390_patch_guest_per_regs(vcpu);
4576 	}
4577 
4578 	clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
4579 
4580 	vcpu->arch.sie_block->icptcode = 0;
4581 	current->thread.gmap_int_code = 0;
4582 	cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
4583 	VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
4584 	trace_kvm_s390_sie_enter(vcpu, cpuflags);
4585 
4586 	return 0;
4587 }
4588 
4589 static int vcpu_post_run_addressing_exception(struct kvm_vcpu *vcpu)
4590 {
4591 	struct kvm_s390_pgm_info pgm_info = {
4592 		.code = PGM_ADDRESSING,
4593 	};
4594 	u8 opcode, ilen;
4595 	int rc;
4596 
4597 	VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4598 	trace_kvm_s390_sie_fault(vcpu);
4599 
4600 	/*
4601 	 * We want to inject an addressing exception, which is defined as a
4602 	 * suppressing or terminating exception. However, since we came here
4603 	 * by a DAT access exception, the PSW still points to the faulting
4604 	 * instruction since DAT exceptions are nullifying. So we've got
4605 	 * to look up the current opcode to get the length of the instruction
4606 	 * to be able to forward the PSW.
4607 	 */
4608 	rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
4609 	ilen = insn_length(opcode);
4610 	if (rc < 0) {
4611 		return rc;
4612 	} else if (rc) {
4613 		/* Instruction-Fetching Exceptions - we can't detect the ilen.
4614 		 * Forward by arbitrary ilc, injection will take care of
4615 		 * nullification if necessary.
4616 		 */
4617 		pgm_info = vcpu->arch.pgm;
4618 		ilen = 4;
4619 	}
4620 	pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
4621 	kvm_s390_forward_psw(vcpu, ilen);
4622 	return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
4623 }
4624 
4625 static void kvm_s390_assert_primary_as(struct kvm_vcpu *vcpu)
4626 {
4627 	KVM_BUG(current->thread.gmap_teid.as != PSW_BITS_AS_PRIMARY, vcpu->kvm,
4628 		"Unexpected program interrupt 0x%x, TEID 0x%016lx",
4629 		current->thread.gmap_int_code, current->thread.gmap_teid.val);
4630 }
4631 
4632 static int vcpu_dat_fault_handler(struct kvm_vcpu *vcpu, gpa_t gaddr, bool wr)
4633 {
4634 	struct guest_fault f = {
4635 		.write_attempt = wr,
4636 		.attempt_pfault = pfault_enabled(vcpu->arch.gmap),
4637 	};
4638 	int rc;
4639 
4640 	if (vcpu_ucontrol_translate(vcpu, &gaddr))
4641 		return -EREMOTE;
4642 	f.gfn = gpa_to_gfn(gaddr);
4643 
4644 	rc = kvm_s390_faultin_gfn(vcpu, NULL, &f);
4645 	if (rc <= 0)
4646 		return rc;
4647 	if (rc == PGM_ADDRESSING)
4648 		return vcpu_post_run_addressing_exception(vcpu);
4649 	KVM_BUG_ON(rc, vcpu->kvm);
4650 	return -EINVAL;
4651 }
4652 
4653 static int vcpu_post_run_handle_fault(struct kvm_vcpu *vcpu)
4654 {
4655 	unsigned int foll = 0;
4656 	unsigned long gaddr;
4657 	int rc;
4658 
4659 	gaddr = current->thread.gmap_teid.addr * PAGE_SIZE;
4660 	if (kvm_s390_cur_gmap_fault_is_write())
4661 		foll = FOLL_WRITE;
4662 
4663 	switch (current->thread.gmap_int_code & PGM_INT_CODE_MASK) {
4664 	case 0:
4665 		vcpu->stat.exit_null++;
4666 		break;
4667 	case PGM_SECURE_STORAGE_ACCESS:
4668 	case PGM_SECURE_STORAGE_VIOLATION:
4669 		kvm_s390_assert_primary_as(vcpu);
4670 		/*
4671 		 * This can happen after a reboot with asynchronous teardown;
4672 		 * the new guest (normal or protected) will run on top of the
4673 		 * previous protected guest. The old pages need to be destroyed
4674 		 * so the new guest can use them.
4675 		 */
4676 		if (kvm_s390_pv_destroy_page(vcpu->kvm, gaddr)) {
4677 			/*
4678 			 * Either KVM messed up the secure guest mapping or the
4679 			 * same page is mapped into multiple secure guests.
4680 			 *
4681 			 * This exception is only triggered when a guest 2 is
4682 			 * running and can therefore never occur in kernel
4683 			 * context.
4684 			 */
4685 			pr_warn_ratelimited("Secure storage violation (%x) in task: %s, pid %d\n",
4686 					    current->thread.gmap_int_code, current->comm,
4687 					    current->pid);
4688 			send_sig(SIGSEGV, current, 0);
4689 		}
4690 		break;
4691 	case PGM_NON_SECURE_STORAGE_ACCESS:
4692 		kvm_s390_assert_primary_as(vcpu);
4693 		/*
4694 		 * This is normal operation; a page belonging to a protected
4695 		 * guest has not been imported yet. Try to import the page into
4696 		 * the protected guest.
4697 		 */
4698 		rc = kvm_s390_pv_convert_to_secure(vcpu->kvm, gaddr);
4699 		if (rc == -EINVAL)
4700 			send_sig(SIGSEGV, current, 0);
4701 		if (rc != -ENXIO)
4702 			break;
4703 		foll = FOLL_WRITE;
4704 		fallthrough;
4705 	case PGM_PROTECTION:
4706 	case PGM_SEGMENT_TRANSLATION:
4707 	case PGM_PAGE_TRANSLATION:
4708 	case PGM_ASCE_TYPE:
4709 	case PGM_REGION_FIRST_TRANS:
4710 	case PGM_REGION_SECOND_TRANS:
4711 	case PGM_REGION_THIRD_TRANS:
4712 		kvm_s390_assert_primary_as(vcpu);
4713 		return vcpu_dat_fault_handler(vcpu, gaddr, foll);
4714 	default:
4715 		KVM_BUG(1, vcpu->kvm, "Unexpected program interrupt 0x%x, TEID 0x%016lx",
4716 			current->thread.gmap_int_code, current->thread.gmap_teid.val);
4717 		send_sig(SIGSEGV, current, 0);
4718 		break;
4719 	}
4720 	return 0;
4721 }
4722 
4723 static int vcpu_post_run(struct kvm_vcpu *vcpu, int sie_return)
4724 {
4725 	struct mcck_volatile_info *mcck_info;
4726 	struct sie_page *sie_page;
4727 	int rc;
4728 
4729 	VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
4730 		   vcpu->arch.sie_block->icptcode);
4731 	trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
4732 
4733 	if (guestdbg_enabled(vcpu))
4734 		kvm_s390_restore_guest_per_regs(vcpu);
4735 
4736 	vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
4737 	vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
4738 
4739 	if (sie_return == SIE64_RETURN_MCCK) {
4740 		sie_page = container_of(vcpu->arch.sie_block,
4741 					struct sie_page, sie_block);
4742 		mcck_info = &sie_page->mcck_info;
4743 		kvm_s390_reinject_machine_check(vcpu, mcck_info);
4744 		return 0;
4745 	}
4746 	WARN_ON_ONCE(sie_return != SIE64_RETURN_NORMAL);
4747 
4748 	if (vcpu->arch.sie_block->icptcode > 0) {
4749 		rc = kvm_handle_sie_intercept(vcpu);
4750 
4751 		if (rc != -EOPNOTSUPP)
4752 			return rc;
4753 		vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
4754 		vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
4755 		vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
4756 		vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
4757 		return -EREMOTE;
4758 	}
4759 
4760 	return vcpu_post_run_handle_fault(vcpu);
4761 }
4762 
4763 int noinstr kvm_s390_enter_exit_sie(struct kvm_s390_sie_block *scb,
4764 				    u64 *gprs, unsigned long gasce)
4765 {
4766 	int ret;
4767 
4768 	guest_state_enter_irqoff();
4769 
4770 	/*
4771 	 * The guest_state_{enter,exit}_irqoff() functions inform lockdep and
4772 	 * tracing that entry to the guest will enable host IRQs, and exit from
4773 	 * the guest will disable host IRQs.
4774 	 */
4775 	ret = sie64a(scb, gprs, gasce);
4776 
4777 	guest_state_exit_irqoff();
4778 
4779 	return ret;
4780 }
4781 
4782 #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
4783 static int __vcpu_run(struct kvm_vcpu *vcpu)
4784 {
4785 	int rc, sie_return;
4786 	struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
4787 
4788 	/*
4789 	 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4790 	 * ning the guest), so that memslots (and other stuff) are protected
4791 	 */
4792 	kvm_vcpu_srcu_read_lock(vcpu);
4793 
4794 	while (true) {
4795 		rc = vcpu_pre_run(vcpu);
4796 		kvm_vcpu_srcu_read_unlock(vcpu);
4797 		if (rc || guestdbg_exit_pending(vcpu))
4798 			break;
4799 
4800 		/*
4801 		 * As PF_VCPU will be used in fault handler, between
4802 		 * guest_timing_enter_irqoff and guest_timing_exit_irqoff
4803 		 * should be no uaccess.
4804 		 */
4805 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4806 			memcpy(sie_page->pv_grregs,
4807 			       vcpu->run->s.regs.gprs,
4808 			       sizeof(sie_page->pv_grregs));
4809 		}
4810 
4811 xfer_to_guest_mode_check:
4812 		local_irq_disable();
4813 		xfer_to_guest_mode_prepare();
4814 		if (xfer_to_guest_mode_work_pending()) {
4815 			local_irq_enable();
4816 			rc = kvm_xfer_to_guest_mode_handle_work(vcpu);
4817 			if (rc)
4818 				break;
4819 			goto xfer_to_guest_mode_check;
4820 		}
4821 
4822 		guest_timing_enter_irqoff();
4823 		__disable_cpu_timer_accounting(vcpu);
4824 
4825 		sie_return = kvm_s390_enter_exit_sie(vcpu->arch.sie_block,
4826 						     vcpu->run->s.regs.gprs,
4827 						     vcpu->arch.gmap->asce.val);
4828 
4829 		__enable_cpu_timer_accounting(vcpu);
4830 		guest_timing_exit_irqoff();
4831 		local_irq_enable();
4832 
4833 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4834 			memcpy(vcpu->run->s.regs.gprs,
4835 			       sie_page->pv_grregs,
4836 			       sizeof(sie_page->pv_grregs));
4837 			/*
4838 			 * We're not allowed to inject interrupts on intercepts
4839 			 * that leave the guest state in an "in-between" state
4840 			 * where the next SIE entry will do a continuation.
4841 			 * Fence interrupts in our "internal" PSW.
4842 			 */
4843 			if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
4844 			    vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
4845 				vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
4846 			}
4847 		}
4848 		kvm_vcpu_srcu_read_lock(vcpu);
4849 
4850 		rc = vcpu_post_run(vcpu, sie_return);
4851 		if (rc || guestdbg_exit_pending(vcpu)) {
4852 			kvm_vcpu_srcu_read_unlock(vcpu);
4853 			break;
4854 		}
4855 	}
4856 
4857 	return rc;
4858 }
4859 
4860 static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
4861 {
4862 	struct kvm_run *kvm_run = vcpu->run;
4863 	struct runtime_instr_cb *riccb;
4864 	struct gs_cb *gscb;
4865 
4866 	riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
4867 	gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4868 	vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4869 	vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4870 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4871 		vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4872 		vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4873 		vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4874 	}
4875 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4876 		vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4877 		vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4878 		vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
4879 		if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4880 			kvm_clear_async_pf_completion_queue(vcpu);
4881 	}
4882 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
4883 		vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
4884 		vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
4885 		VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc);
4886 	}
4887 	/*
4888 	 * If userspace sets the riccb (e.g. after migration) to a valid state,
4889 	 * we should enable RI here instead of doing the lazy enablement.
4890 	 */
4891 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
4892 	    test_kvm_facility(vcpu->kvm, 64) &&
4893 	    riccb->v &&
4894 	    !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
4895 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
4896 		vcpu->arch.sie_block->ecb3 |= ECB3_RI;
4897 	}
4898 	/*
4899 	 * If userspace sets the gscb (e.g. after migration) to non-zero,
4900 	 * we should enable GS here instead of doing the lazy enablement.
4901 	 */
4902 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
4903 	    test_kvm_facility(vcpu->kvm, 133) &&
4904 	    gscb->gssm &&
4905 	    !vcpu->arch.gs_enabled) {
4906 		VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
4907 		vcpu->arch.sie_block->ecb |= ECB_GS;
4908 		vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
4909 		vcpu->arch.gs_enabled = 1;
4910 	}
4911 	if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
4912 	    test_kvm_facility(vcpu->kvm, 82)) {
4913 		vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
4914 		vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
4915 	}
4916 	if (cpu_has_gs()) {
4917 		preempt_disable();
4918 		local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT);
4919 		if (current->thread.gs_cb) {
4920 			vcpu->arch.host_gscb = current->thread.gs_cb;
4921 			save_gs_cb(vcpu->arch.host_gscb);
4922 		}
4923 		if (vcpu->arch.gs_enabled) {
4924 			current->thread.gs_cb = (struct gs_cb *)
4925 						&vcpu->run->s.regs.gscb;
4926 			restore_gs_cb(current->thread.gs_cb);
4927 		}
4928 		preempt_enable();
4929 	}
4930 	/* SIE will load etoken directly from SDNX and therefore kvm_run */
4931 }
4932 
4933 static void sync_regs(struct kvm_vcpu *vcpu)
4934 {
4935 	struct kvm_run *kvm_run = vcpu->run;
4936 
4937 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4938 		kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4939 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4940 		memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4941 		/* some control register changes require a tlb flush */
4942 		kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4943 	}
4944 	if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4945 		kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4946 		vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4947 	}
4948 	save_access_regs(vcpu->arch.host_acrs);
4949 	restore_access_regs(vcpu->run->s.regs.acrs);
4950 	vcpu->arch.acrs_loaded = true;
4951 	kvm_s390_fpu_load(vcpu->run);
4952 	/* Sync fmt2 only data */
4953 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
4954 		sync_regs_fmt2(vcpu);
4955 	} else {
4956 		/*
4957 		 * In several places we have to modify our internal view to
4958 		 * not do things that are disallowed by the ultravisor. For
4959 		 * example we must not inject interrupts after specific exits
4960 		 * (e.g. 112 prefix page not secure). We do this by turning
4961 		 * off the machine check, external and I/O interrupt bits
4962 		 * of our PSW copy. To avoid getting validity intercepts, we
4963 		 * do only accept the condition code from userspace.
4964 		 */
4965 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4966 		vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4967 						   PSW_MASK_CC;
4968 	}
4969 
4970 	kvm_run->kvm_dirty_regs = 0;
4971 }
4972 
4973 static void store_regs_fmt2(struct kvm_vcpu *vcpu)
4974 {
4975 	struct kvm_run *kvm_run = vcpu->run;
4976 
4977 	kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4978 	kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4979 	kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
4980 	kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
4981 	kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
4982 	if (cpu_has_gs()) {
4983 		preempt_disable();
4984 		local_ctl_set_bit(2, CR2_GUARDED_STORAGE_BIT);
4985 		if (vcpu->arch.gs_enabled)
4986 			save_gs_cb(current->thread.gs_cb);
4987 		current->thread.gs_cb = vcpu->arch.host_gscb;
4988 		restore_gs_cb(vcpu->arch.host_gscb);
4989 		if (!vcpu->arch.host_gscb)
4990 			local_ctl_clear_bit(2, CR2_GUARDED_STORAGE_BIT);
4991 		vcpu->arch.host_gscb = NULL;
4992 		preempt_enable();
4993 	}
4994 	/* SIE will save etoken directly into SDNX and therefore kvm_run */
4995 }
4996 
4997 static void store_regs(struct kvm_vcpu *vcpu)
4998 {
4999 	struct kvm_run *kvm_run = vcpu->run;
5000 
5001 	kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
5002 	kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
5003 	kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
5004 	memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
5005 	kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
5006 	kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
5007 	kvm_run->s.regs.pft = vcpu->arch.pfault_token;
5008 	kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
5009 	kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
5010 	save_access_regs(vcpu->run->s.regs.acrs);
5011 	restore_access_regs(vcpu->arch.host_acrs);
5012 	vcpu->arch.acrs_loaded = false;
5013 	kvm_s390_fpu_store(vcpu->run);
5014 	if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
5015 		store_regs_fmt2(vcpu);
5016 }
5017 
5018 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
5019 {
5020 	struct kvm_run *kvm_run = vcpu->run;
5021 	DECLARE_KERNEL_FPU_ONSTACK32(fpu);
5022 	int rc;
5023 
5024 	/*
5025 	 * Running a VM while dumping always has the potential to
5026 	 * produce inconsistent dump data. But for PV vcpus a SIE
5027 	 * entry while dumping could also lead to a fatal validity
5028 	 * intercept which we absolutely want to avoid.
5029 	 */
5030 	if (vcpu->kvm->arch.pv.dumping)
5031 		return -EINVAL;
5032 
5033 	if (!vcpu->wants_to_run)
5034 		return -EINTR;
5035 
5036 	if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
5037 	    kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
5038 		return -EINVAL;
5039 
5040 	vcpu_load(vcpu);
5041 
5042 	if (guestdbg_exit_pending(vcpu)) {
5043 		kvm_s390_prepare_debug_exit(vcpu);
5044 		rc = 0;
5045 		goto out;
5046 	}
5047 
5048 	kvm_sigset_activate(vcpu);
5049 
5050 	/*
5051 	 * no need to check the return value of vcpu_start as it can only have
5052 	 * an error for protvirt, but protvirt means user cpu state
5053 	 */
5054 	if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
5055 		kvm_s390_vcpu_start(vcpu);
5056 	} else if (is_vcpu_stopped(vcpu)) {
5057 		pr_err_ratelimited("can't run stopped vcpu %d\n",
5058 				   vcpu->vcpu_id);
5059 		rc = -EINVAL;
5060 		goto out;
5061 	}
5062 
5063 	kernel_fpu_begin(&fpu, KERNEL_FPC | KERNEL_VXR);
5064 	sync_regs(vcpu);
5065 	enable_cpu_timer_accounting(vcpu);
5066 
5067 	might_fault();
5068 	rc = __vcpu_run(vcpu);
5069 
5070 	if (signal_pending(current) && !rc) {
5071 		kvm_run->exit_reason = KVM_EXIT_INTR;
5072 		vcpu->stat.signal_exits++;
5073 		rc = -EINTR;
5074 	}
5075 
5076 	if (guestdbg_exit_pending(vcpu) && !rc)  {
5077 		kvm_s390_prepare_debug_exit(vcpu);
5078 		rc = 0;
5079 	}
5080 
5081 	if (rc == -EREMOTE) {
5082 		/* userspace support is needed, kvm_run has been prepared */
5083 		rc = 0;
5084 	}
5085 
5086 	disable_cpu_timer_accounting(vcpu);
5087 	store_regs(vcpu);
5088 	kernel_fpu_end(&fpu, KERNEL_FPC | KERNEL_VXR);
5089 
5090 	kvm_sigset_deactivate(vcpu);
5091 
5092 	vcpu->stat.exit_userspace++;
5093 out:
5094 	vcpu_put(vcpu);
5095 	return rc;
5096 }
5097 
5098 /*
5099  * store status at address
5100  * we use have two special cases:
5101  * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
5102  * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
5103  */
5104 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
5105 {
5106 	unsigned char archmode = 1;
5107 	freg_t fprs[NUM_FPRS];
5108 	unsigned int px;
5109 	u64 clkcomp, cputm;
5110 	int rc;
5111 
5112 	px = kvm_s390_get_prefix(vcpu);
5113 	if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
5114 		if (write_guest_abs(vcpu, 163, &archmode, 1))
5115 			return -EFAULT;
5116 		gpa = 0;
5117 	} else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
5118 		if (write_guest_real(vcpu, 163, &archmode, 1))
5119 			return -EFAULT;
5120 		gpa = px;
5121 	} else
5122 		gpa -= __LC_FPREGS_SAVE_AREA;
5123 
5124 	/* manually convert vector registers if necessary */
5125 	if (cpu_has_vx()) {
5126 		convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
5127 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
5128 				     fprs, 128);
5129 	} else {
5130 		rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
5131 				     vcpu->run->s.regs.fprs, 128);
5132 	}
5133 	rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
5134 			      vcpu->run->s.regs.gprs, 128);
5135 	rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
5136 			      &vcpu->arch.sie_block->gpsw, 16);
5137 	rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
5138 			      &px, 4);
5139 	rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
5140 			      &vcpu->run->s.regs.fpc, 4);
5141 	rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
5142 			      &vcpu->arch.sie_block->todpr, 4);
5143 	cputm = kvm_s390_get_cpu_timer(vcpu);
5144 	rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
5145 			      &cputm, 8);
5146 	clkcomp = vcpu->arch.sie_block->ckc >> 8;
5147 	rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
5148 			      &clkcomp, 8);
5149 	rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
5150 			      &vcpu->run->s.regs.acrs, 64);
5151 	rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
5152 			      &vcpu->arch.sie_block->gcr, 128);
5153 	return rc ? -EFAULT : 0;
5154 }
5155 
5156 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
5157 {
5158 	/*
5159 	 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
5160 	 * switch in the run ioctl. Let's update our copies before we save
5161 	 * it into the save area
5162 	 */
5163 	kvm_s390_fpu_store(vcpu->run);
5164 	save_access_regs(vcpu->run->s.regs.acrs);
5165 
5166 	return kvm_s390_store_status_unloaded(vcpu, addr);
5167 }
5168 
5169 static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
5170 {
5171 	kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
5172 	kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
5173 }
5174 
5175 static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
5176 {
5177 	unsigned long i;
5178 	struct kvm_vcpu *vcpu;
5179 
5180 	kvm_for_each_vcpu(i, vcpu, kvm) {
5181 		__disable_ibs_on_vcpu(vcpu);
5182 	}
5183 }
5184 
5185 static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
5186 {
5187 	if (!sclp.has_ibs)
5188 		return;
5189 	kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
5190 	kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
5191 }
5192 
5193 int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
5194 {
5195 	int i, online_vcpus, r = 0, started_vcpus = 0;
5196 
5197 	if (!is_vcpu_stopped(vcpu))
5198 		return 0;
5199 
5200 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
5201 	/* Only one cpu at a time may enter/leave the STOPPED state. */
5202 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
5203 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
5204 
5205 	/* Let's tell the UV that we want to change into the operating state */
5206 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5207 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
5208 		if (r) {
5209 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5210 			return r;
5211 		}
5212 	}
5213 
5214 	for (i = 0; i < online_vcpus; i++) {
5215 		if (!is_vcpu_stopped(kvm_get_vcpu(vcpu->kvm, i)))
5216 			started_vcpus++;
5217 	}
5218 
5219 	if (started_vcpus == 0) {
5220 		/* we're the only active VCPU -> speed it up */
5221 		__enable_ibs_on_vcpu(vcpu);
5222 	} else if (started_vcpus == 1) {
5223 		/*
5224 		 * As we are starting a second VCPU, we have to disable
5225 		 * the IBS facility on all VCPUs to remove potentially
5226 		 * outstanding ENABLE requests.
5227 		 */
5228 		__disable_ibs_on_all_vcpus(vcpu->kvm);
5229 	}
5230 
5231 	kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
5232 	/*
5233 	 * The real PSW might have changed due to a RESTART interpreted by the
5234 	 * ultravisor. We block all interrupts and let the next sie exit
5235 	 * refresh our view.
5236 	 */
5237 	if (kvm_s390_pv_cpu_is_protected(vcpu))
5238 		vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
5239 	/*
5240 	 * Another VCPU might have used IBS while we were offline.
5241 	 * Let's play safe and flush the VCPU at startup.
5242 	 */
5243 	kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
5244 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5245 	return 0;
5246 }
5247 
5248 int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
5249 {
5250 	int i, online_vcpus, r = 0, started_vcpus = 0;
5251 	struct kvm_vcpu *started_vcpu = NULL;
5252 
5253 	if (is_vcpu_stopped(vcpu))
5254 		return 0;
5255 
5256 	trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
5257 	/* Only one cpu at a time may enter/leave the STOPPED state. */
5258 	spin_lock(&vcpu->kvm->arch.start_stop_lock);
5259 	online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
5260 
5261 	/* Let's tell the UV that we want to change into the stopped state */
5262 	if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5263 		r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
5264 		if (r) {
5265 			spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5266 			return r;
5267 		}
5268 	}
5269 
5270 	/*
5271 	 * Set the VCPU to STOPPED and THEN clear the interrupt flag,
5272 	 * now that the SIGP STOP and SIGP STOP AND STORE STATUS orders
5273 	 * have been fully processed. This will ensure that the VCPU
5274 	 * is kept BUSY if another VCPU is inquiring with SIGP SENSE.
5275 	 */
5276 	kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
5277 	kvm_s390_clear_stop_irq(vcpu);
5278 
5279 	__disable_ibs_on_vcpu(vcpu);
5280 
5281 	for (i = 0; i < online_vcpus; i++) {
5282 		struct kvm_vcpu *tmp = kvm_get_vcpu(vcpu->kvm, i);
5283 
5284 		if (!is_vcpu_stopped(tmp)) {
5285 			started_vcpus++;
5286 			started_vcpu = tmp;
5287 		}
5288 	}
5289 
5290 	if (started_vcpus == 1) {
5291 		/*
5292 		 * As we only have one VCPU left, we want to enable the
5293 		 * IBS facility for that VCPU to speed it up.
5294 		 */
5295 		__enable_ibs_on_vcpu(started_vcpu);
5296 	}
5297 
5298 	spin_unlock(&vcpu->kvm->arch.start_stop_lock);
5299 	return 0;
5300 }
5301 
5302 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
5303 				     struct kvm_enable_cap *cap)
5304 {
5305 	int r;
5306 
5307 	if (cap->flags)
5308 		return -EINVAL;
5309 
5310 	switch (cap->cap) {
5311 	case KVM_CAP_S390_CSS_SUPPORT:
5312 		if (!vcpu->kvm->arch.css_support) {
5313 			vcpu->kvm->arch.css_support = 1;
5314 			VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
5315 			trace_kvm_s390_enable_css(vcpu->kvm);
5316 		}
5317 		r = 0;
5318 		break;
5319 	default:
5320 		r = -EINVAL;
5321 		break;
5322 	}
5323 	return r;
5324 }
5325 
5326 static long kvm_s390_vcpu_sida_op(struct kvm_vcpu *vcpu,
5327 				  struct kvm_s390_mem_op *mop)
5328 {
5329 	void __user *uaddr = (void __user *)mop->buf;
5330 	void *sida_addr;
5331 	int r = 0;
5332 
5333 	if (mop->flags || !mop->size)
5334 		return -EINVAL;
5335 	if (mop->size + mop->sida_offset < mop->size)
5336 		return -EINVAL;
5337 	if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
5338 		return -E2BIG;
5339 	if (!kvm_s390_pv_cpu_is_protected(vcpu))
5340 		return -EINVAL;
5341 
5342 	sida_addr = (char *)sida_addr(vcpu->arch.sie_block) + mop->sida_offset;
5343 
5344 	switch (mop->op) {
5345 	case KVM_S390_MEMOP_SIDA_READ:
5346 		if (copy_to_user(uaddr, sida_addr, mop->size))
5347 			r = -EFAULT;
5348 
5349 		break;
5350 	case KVM_S390_MEMOP_SIDA_WRITE:
5351 		if (copy_from_user(sida_addr, uaddr, mop->size))
5352 			r = -EFAULT;
5353 		break;
5354 	}
5355 	return r;
5356 }
5357 
5358 static long kvm_s390_vcpu_mem_op(struct kvm_vcpu *vcpu,
5359 				 struct kvm_s390_mem_op *mop)
5360 {
5361 	void __user *uaddr = (void __user *)mop->buf;
5362 	void *tmpbuf __free(kvfree) = NULL;
5363 	enum gacc_mode acc_mode;
5364 	int r;
5365 
5366 	r = mem_op_validate_common(mop, KVM_S390_MEMOP_F_INJECT_EXCEPTION |
5367 					KVM_S390_MEMOP_F_CHECK_ONLY |
5368 					KVM_S390_MEMOP_F_SKEY_PROTECTION);
5369 	if (r)
5370 		return r;
5371 	if (mop->ar >= NUM_ACRS)
5372 		return -EINVAL;
5373 	if (kvm_s390_pv_cpu_is_protected(vcpu))
5374 		return -EINVAL;
5375 	if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
5376 		tmpbuf = vmalloc(mop->size);
5377 		if (!tmpbuf)
5378 			return -ENOMEM;
5379 	}
5380 
5381 	acc_mode = mop->op == KVM_S390_MEMOP_LOGICAL_READ ? GACC_FETCH : GACC_STORE;
5382 	if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
5383 		r = check_gva_range(vcpu, mop->gaddr, mop->ar, mop->size,
5384 				    acc_mode, mop->key);
5385 	} else if (acc_mode == GACC_FETCH) {
5386 		r = read_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5387 					mop->size, mop->key);
5388 		if (!r && copy_to_user(uaddr, tmpbuf, mop->size))
5389 			return -EFAULT;
5390 	} else {
5391 		if (copy_from_user(tmpbuf, uaddr, mop->size))
5392 			return -EFAULT;
5393 		r = write_guest_with_key(vcpu, mop->gaddr, mop->ar, tmpbuf,
5394 					 mop->size, mop->key);
5395 	}
5396 
5397 	if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
5398 		kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
5399 
5400 	return r;
5401 }
5402 
5403 static long kvm_s390_vcpu_memsida_op(struct kvm_vcpu *vcpu,
5404 				     struct kvm_s390_mem_op *mop)
5405 {
5406 	int r, srcu_idx;
5407 
5408 	srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
5409 
5410 	switch (mop->op) {
5411 	case KVM_S390_MEMOP_LOGICAL_READ:
5412 	case KVM_S390_MEMOP_LOGICAL_WRITE:
5413 		r = kvm_s390_vcpu_mem_op(vcpu, mop);
5414 		break;
5415 	case KVM_S390_MEMOP_SIDA_READ:
5416 	case KVM_S390_MEMOP_SIDA_WRITE:
5417 		/* we are locked against sida going away by the vcpu->mutex */
5418 		r = kvm_s390_vcpu_sida_op(vcpu, mop);
5419 		break;
5420 	default:
5421 		r = -EINVAL;
5422 	}
5423 
5424 	srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
5425 	return r;
5426 }
5427 
5428 long kvm_arch_vcpu_unlocked_ioctl(struct file *filp, unsigned int ioctl,
5429 				  unsigned long arg)
5430 {
5431 	struct kvm_vcpu *vcpu = filp->private_data;
5432 	void __user *argp = (void __user *)arg;
5433 	int rc;
5434 
5435 	switch (ioctl) {
5436 	case KVM_S390_IRQ: {
5437 		struct kvm_s390_irq s390irq;
5438 
5439 		if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
5440 			return -EFAULT;
5441 		rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
5442 		break;
5443 	}
5444 	case KVM_S390_INTERRUPT: {
5445 		struct kvm_s390_interrupt s390int;
5446 		struct kvm_s390_irq s390irq = {};
5447 
5448 		if (copy_from_user(&s390int, argp, sizeof(s390int)))
5449 			return -EFAULT;
5450 		if (s390int_to_s390irq(&s390int, &s390irq))
5451 			return -EINVAL;
5452 		rc = kvm_s390_inject_vcpu(vcpu, &s390irq);
5453 		break;
5454 	}
5455 	default:
5456 		rc = -ENOIOCTLCMD;
5457 		break;
5458 	}
5459 
5460 	/*
5461 	 * To simplify single stepping of userspace-emulated instructions,
5462 	 * KVM_EXIT_S390_SIEIC exit sets KVM_GUESTDBG_EXIT_PENDING (see
5463 	 * should_handle_per_ifetch()). However, if userspace emulation injects
5464 	 * an interrupt, it needs to be cleared, so that KVM_EXIT_DEBUG happens
5465 	 * after (and not before) the interrupt delivery.
5466 	 */
5467 	if (!rc)
5468 		vcpu->guest_debug &= ~KVM_GUESTDBG_EXIT_PENDING;
5469 
5470 	return rc;
5471 }
5472 
5473 static int kvm_s390_handle_pv_vcpu_dump(struct kvm_vcpu *vcpu,
5474 					struct kvm_pv_cmd *cmd)
5475 {
5476 	struct kvm_s390_pv_dmp dmp;
5477 	void *data;
5478 	int ret;
5479 
5480 	/* Dump initialization is a prerequisite */
5481 	if (!vcpu->kvm->arch.pv.dumping)
5482 		return -EINVAL;
5483 
5484 	if (copy_from_user(&dmp, (__u8 __user *)cmd->data, sizeof(dmp)))
5485 		return -EFAULT;
5486 
5487 	/* We only handle this subcmd right now */
5488 	if (dmp.subcmd != KVM_PV_DUMP_CPU)
5489 		return -EINVAL;
5490 
5491 	/* CPU dump length is the same as create cpu storage donation. */
5492 	if (dmp.buff_len != uv_info.guest_cpu_stor_len)
5493 		return -EINVAL;
5494 
5495 	data = kvzalloc(uv_info.guest_cpu_stor_len, GFP_KERNEL);
5496 	if (!data)
5497 		return -ENOMEM;
5498 
5499 	ret = kvm_s390_pv_dump_cpu(vcpu, data, &cmd->rc, &cmd->rrc);
5500 
5501 	VCPU_EVENT(vcpu, 3, "PROTVIRT DUMP CPU %d rc %x rrc %x",
5502 		   vcpu->vcpu_id, cmd->rc, cmd->rrc);
5503 
5504 	if (ret)
5505 		ret = -EINVAL;
5506 
5507 	/* On success copy over the dump data */
5508 	if (!ret && copy_to_user((__u8 __user *)dmp.buff_addr, data, uv_info.guest_cpu_stor_len))
5509 		ret = -EFAULT;
5510 
5511 	kvfree(data);
5512 	return ret;
5513 }
5514 
5515 long kvm_arch_vcpu_ioctl(struct file *filp,
5516 			 unsigned int ioctl, unsigned long arg)
5517 {
5518 	struct kvm_vcpu *vcpu = filp->private_data;
5519 	void __user *argp = (void __user *)arg;
5520 	int idx;
5521 	long r;
5522 	u16 rc, rrc;
5523 
5524 	vcpu_load(vcpu);
5525 
5526 	switch (ioctl) {
5527 	case KVM_S390_STORE_STATUS:
5528 		idx = srcu_read_lock(&vcpu->kvm->srcu);
5529 		r = kvm_s390_store_status_unloaded(vcpu, arg);
5530 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
5531 		break;
5532 	case KVM_S390_SET_INITIAL_PSW: {
5533 		psw_t psw;
5534 
5535 		r = -EFAULT;
5536 		if (copy_from_user(&psw, argp, sizeof(psw)))
5537 			break;
5538 		r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
5539 		break;
5540 	}
5541 	case KVM_S390_CLEAR_RESET:
5542 		r = 0;
5543 		kvm_arch_vcpu_ioctl_clear_reset(vcpu);
5544 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5545 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
5546 					  UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
5547 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
5548 				   rc, rrc);
5549 		}
5550 		break;
5551 	case KVM_S390_INITIAL_RESET:
5552 		r = 0;
5553 		kvm_arch_vcpu_ioctl_initial_reset(vcpu);
5554 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5555 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
5556 					  UVC_CMD_CPU_RESET_INITIAL,
5557 					  &rc, &rrc);
5558 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
5559 				   rc, rrc);
5560 		}
5561 		break;
5562 	case KVM_S390_NORMAL_RESET:
5563 		r = 0;
5564 		kvm_arch_vcpu_ioctl_normal_reset(vcpu);
5565 		if (kvm_s390_pv_cpu_is_protected(vcpu)) {
5566 			r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
5567 					  UVC_CMD_CPU_RESET, &rc, &rrc);
5568 			VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
5569 				   rc, rrc);
5570 		}
5571 		break;
5572 	case KVM_SET_ONE_REG:
5573 	case KVM_GET_ONE_REG: {
5574 		struct kvm_one_reg reg;
5575 		r = -EINVAL;
5576 		if (kvm_s390_pv_cpu_is_protected(vcpu))
5577 			break;
5578 		r = -EFAULT;
5579 		if (copy_from_user(&reg, argp, sizeof(reg)))
5580 			break;
5581 		if (ioctl == KVM_SET_ONE_REG)
5582 			r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
5583 		else
5584 			r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
5585 		break;
5586 	}
5587 #ifdef CONFIG_KVM_S390_UCONTROL
5588 	case KVM_S390_UCAS_MAP: {
5589 		struct kvm_s390_ucas_mapping ucas;
5590 
5591 		r = -EFAULT;
5592 		if (copy_from_user(&ucas, argp, sizeof(ucas)))
5593 			break;
5594 
5595 		r = -EINVAL;
5596 		if (!kvm_is_ucontrol(vcpu->kvm))
5597 			break;
5598 		if (!IS_ALIGNED(ucas.user_addr | ucas.vcpu_addr | ucas.length, _SEGMENT_SIZE))
5599 			break;
5600 
5601 		r = gmap_ucas_map(vcpu->arch.gmap, gpa_to_gfn(ucas.user_addr),
5602 				  gpa_to_gfn(ucas.vcpu_addr),
5603 				  ucas.length >> _SEGMENT_SHIFT);
5604 		break;
5605 	}
5606 	case KVM_S390_UCAS_UNMAP: {
5607 		struct kvm_s390_ucas_mapping ucas;
5608 
5609 		r = -EFAULT;
5610 		if (copy_from_user(&ucas, argp, sizeof(ucas)))
5611 			break;
5612 
5613 		r = -EINVAL;
5614 		if (!kvm_is_ucontrol(vcpu->kvm))
5615 			break;
5616 		if (!IS_ALIGNED(ucas.vcpu_addr | ucas.length, _SEGMENT_SIZE))
5617 			break;
5618 
5619 		gmap_ucas_unmap(vcpu->arch.gmap, gpa_to_gfn(ucas.vcpu_addr),
5620 				ucas.length >> _SEGMENT_SHIFT);
5621 		r = 0;
5622 		break;
5623 	}
5624 #endif
5625 	case KVM_S390_VCPU_FAULT: {
5626 		gpa_t gaddr = arg;
5627 
5628 		scoped_guard(srcu, &vcpu->kvm->srcu) {
5629 			r = vcpu_ucontrol_translate(vcpu, &gaddr);
5630 			if (r)
5631 				break;
5632 
5633 			r = kvm_s390_faultin_gfn_simple(vcpu, NULL, gpa_to_gfn(gaddr), false);
5634 			if (r == PGM_ADDRESSING)
5635 				r = -EFAULT;
5636 			if (r <= 0)
5637 				break;
5638 			r = -EIO;
5639 			KVM_BUG_ON(r, vcpu->kvm);
5640 		}
5641 		break;
5642 	}
5643 	case KVM_ENABLE_CAP:
5644 	{
5645 		struct kvm_enable_cap cap;
5646 		r = -EFAULT;
5647 		if (copy_from_user(&cap, argp, sizeof(cap)))
5648 			break;
5649 		r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
5650 		break;
5651 	}
5652 	case KVM_S390_MEM_OP: {
5653 		struct kvm_s390_mem_op mem_op;
5654 
5655 		if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
5656 			r = kvm_s390_vcpu_memsida_op(vcpu, &mem_op);
5657 		else
5658 			r = -EFAULT;
5659 		break;
5660 	}
5661 	case KVM_S390_SET_IRQ_STATE: {
5662 		struct kvm_s390_irq_state irq_state;
5663 
5664 		r = -EFAULT;
5665 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5666 			break;
5667 		if (irq_state.len > VCPU_IRQS_MAX_BUF ||
5668 		    irq_state.len == 0 ||
5669 		    irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
5670 			r = -EINVAL;
5671 			break;
5672 		}
5673 		/* do not use irq_state.flags, it will break old QEMUs */
5674 		r = kvm_s390_set_irq_state(vcpu,
5675 					   (void __user *) irq_state.buf,
5676 					   irq_state.len);
5677 		break;
5678 	}
5679 	case KVM_S390_GET_IRQ_STATE: {
5680 		struct kvm_s390_irq_state irq_state;
5681 
5682 		r = -EFAULT;
5683 		if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
5684 			break;
5685 		if (irq_state.len == 0) {
5686 			r = -EINVAL;
5687 			break;
5688 		}
5689 		/* do not use irq_state.flags, it will break old QEMUs */
5690 		r = kvm_s390_get_irq_state(vcpu,
5691 					   (__u8 __user *)  irq_state.buf,
5692 					   irq_state.len);
5693 		break;
5694 	}
5695 	case KVM_S390_PV_CPU_COMMAND: {
5696 		struct kvm_pv_cmd cmd;
5697 
5698 		r = -EINVAL;
5699 		if (!is_prot_virt_host())
5700 			break;
5701 
5702 		r = -EFAULT;
5703 		if (copy_from_user(&cmd, argp, sizeof(cmd)))
5704 			break;
5705 
5706 		r = -EINVAL;
5707 		if (cmd.flags)
5708 			break;
5709 
5710 		/* We only handle this cmd right now */
5711 		if (cmd.cmd != KVM_PV_DUMP)
5712 			break;
5713 
5714 		r = kvm_s390_handle_pv_vcpu_dump(vcpu, &cmd);
5715 
5716 		/* Always copy over UV rc / rrc data */
5717 		if (copy_to_user((__u8 __user *)argp, &cmd.rc,
5718 				 sizeof(cmd.rc) + sizeof(cmd.rrc)))
5719 			r = -EFAULT;
5720 		break;
5721 	}
5722 	default:
5723 		r = -ENOTTY;
5724 	}
5725 
5726 	vcpu_put(vcpu);
5727 	return r;
5728 }
5729 
5730 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
5731 {
5732 #ifdef CONFIG_KVM_S390_UCONTROL
5733 	if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
5734 		 && (kvm_is_ucontrol(vcpu->kvm))) {
5735 		vmf->page = virt_to_page(vcpu->arch.sie_block);
5736 		get_page(vmf->page);
5737 		return 0;
5738 	}
5739 #endif
5740 	return VM_FAULT_SIGBUS;
5741 }
5742 
5743 bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
5744 {
5745 	return true;
5746 }
5747 
5748 /* Section: memory related */
5749 int kvm_arch_prepare_memory_region(struct kvm *kvm,
5750 				   const struct kvm_memory_slot *old,
5751 				   struct kvm_memory_slot *new,
5752 				   enum kvm_mr_change change)
5753 {
5754 	if (kvm_is_ucontrol(kvm) && new && new->id < KVM_USER_MEM_SLOTS)
5755 		return -EINVAL;
5756 
5757 	/* When we are protected, we should not change the memory slots */
5758 	if (kvm_s390_pv_get_handle(kvm))
5759 		return -EINVAL;
5760 
5761 	if (change != KVM_MR_DELETE && change != KVM_MR_FLAGS_ONLY) {
5762 		/*
5763 		 * A few sanity checks. The memory in userland is ok to be
5764 		 * fragmented into various different vmas. It is okay to mmap()
5765 		 * and munmap() stuff in this slot after doing this call at any
5766 		 * time.
5767 		 */
5768 		if (new->userspace_addr & ~PAGE_MASK)
5769 			return -EINVAL;
5770 		if ((new->base_gfn + new->npages) * PAGE_SIZE > kvm->arch.mem_limit)
5771 			return -EINVAL;
5772 		if (!asce_contains_gfn(kvm->arch.gmap->asce, new->base_gfn + new->npages - 1))
5773 			return -EINVAL;
5774 	}
5775 
5776 	if (!kvm->arch.migration_mode)
5777 		return 0;
5778 
5779 	/*
5780 	 * Turn off migration mode when:
5781 	 * - userspace creates a new memslot with dirty logging off,
5782 	 * - userspace modifies an existing memslot (MOVE or FLAGS_ONLY) and
5783 	 *   dirty logging is turned off.
5784 	 * Migration mode expects dirty page logging being enabled to store
5785 	 * its dirty bitmap.
5786 	 */
5787 	if (change != KVM_MR_DELETE &&
5788 	    !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
5789 		WARN(kvm_s390_vm_stop_migration(kvm),
5790 		     "Failed to stop migration mode");
5791 
5792 	return 0;
5793 }
5794 
5795 void kvm_arch_commit_memory_region(struct kvm *kvm,
5796 				struct kvm_memory_slot *old,
5797 				const struct kvm_memory_slot *new,
5798 				enum kvm_mr_change change)
5799 {
5800 	struct kvm_s390_mmu_cache *mc = NULL;
5801 	int rc = 0;
5802 
5803 	if (change == KVM_MR_FLAGS_ONLY)
5804 		return;
5805 
5806 	mc = kvm_s390_new_mmu_cache();
5807 	if (!mc) {
5808 		rc = -ENOMEM;
5809 		goto out;
5810 	}
5811 
5812 	scoped_guard(write_lock, &kvm->mmu_lock) {
5813 		switch (change) {
5814 		case KVM_MR_DELETE:
5815 			rc = dat_delete_slot(mc, kvm->arch.gmap->asce, old->base_gfn, old->npages);
5816 			break;
5817 		case KVM_MR_MOVE:
5818 			rc = dat_delete_slot(mc, kvm->arch.gmap->asce, old->base_gfn, old->npages);
5819 			if (rc)
5820 				break;
5821 			fallthrough;
5822 		case KVM_MR_CREATE:
5823 			rc = dat_create_slot(mc, kvm->arch.gmap->asce, new->base_gfn, new->npages);
5824 			break;
5825 		case KVM_MR_FLAGS_ONLY:
5826 			break;
5827 		default:
5828 			WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
5829 		}
5830 	}
5831 out:
5832 	if (rc)
5833 		pr_warn("failed to commit memory region\n");
5834 	kvm_s390_free_mmu_cache(mc);
5835 	return;
5836 }
5837 
5838 /**
5839  * kvm_arch_vcpu_pre_fault_memory() -- pre-fault and link gmap dat tables
5840  * @vcpu: the vcpu that shall appear to have generated the fault-in.
5841  * @range: the range that needs to be faulted in.
5842  *
5843  * The first page of the given range is faulted in and the corresponding gmap
5844  * page tables are created, as if the given vCPU had performed a read
5845  * operation.
5846  * If the range starts outside any memslots, an error is returned. An error is
5847  * also returned for UCONTROL VMs, which should instead use the
5848  * KVM_S390_VCPU_FAULT ioctl.
5849  *
5850  * Return:
5851  * * %-ENOENT if the range lies outside of a memslot.
5852  * * %-EINVAL in case of invalid state (for example if the VM is UCONTROL).
5853  * * %-EIO if errors happen while faulting-in the page (will trigger a warning
5854  *   in the caller).
5855  * * other error codes < 0 in case of other errors.
5856  * * otherwise a number > 0 of bytes that have been faulted in successfully.
5857  */
5858 long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range)
5859 {
5860 	struct guest_fault f = { .gfn = gpa_to_gfn(range->gpa), };
5861 	gpa_t end;
5862 	int rc;
5863 
5864 	if (kvm_is_ucontrol(vcpu->kvm))
5865 		return -EINVAL;
5866 
5867 	rc = kvm_s390_faultin_gfn(vcpu, NULL, &f);
5868 	if (rc == PGM_ADDRESSING)
5869 		return -ENOENT;
5870 	if (rc > 0)
5871 		return -EIO;
5872 	if (rc < 0)
5873 		return rc;
5874 
5875 	if (f.ptep)
5876 		return PAGE_SIZE;
5877 
5878 	end = ALIGN(range->gpa + PAGE_SIZE, f.crste_region3 ? _REGION3_SIZE : HPAGE_SIZE);
5879 	return min(range->size, end - range->gpa);
5880 }
5881 
5882 /**
5883  * kvm_test_age_gfn() - test young
5884  * @kvm: the kvm instance
5885  * @range: the range of guest addresses whose young status needs to be cleared
5886  *
5887  * Context: called by KVM common code without holding the kvm mmu lock
5888  * Return: true if any page in the given range is young, otherwise 0.
5889  */
5890 bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
5891 {
5892 	scoped_guard(read_lock, &kvm->mmu_lock)
5893 		return dat_test_age_gfn(kvm->arch.gmap->asce, range->start, range->end);
5894 }
5895 
5896 /**
5897  * kvm_age_gfn() - clear young
5898  * @kvm: the kvm instance
5899  * @range: the range of guest addresses whose young status needs to be cleared
5900  *
5901  * Context: called by KVM common code without holding the kvm mmu lock
5902  * Return: true if any page in the given range was young, otherwise 0.
5903  */
5904 bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range)
5905 {
5906 	scoped_guard(read_lock, &kvm->mmu_lock)
5907 		return gmap_age_gfn(kvm->arch.gmap, range->start, range->end);
5908 }
5909 
5910 /**
5911  * kvm_unmap_gfn_range() - Unmap a range of guest addresses
5912  * @kvm: the kvm instance
5913  * @range: the range of guest page frames to invalidate
5914  *
5915  * This function always returns false because every DAT table modification
5916  * has to use the appropriate DAT table manipulation instructions, which will
5917  * keep the TLB coherent, hence no additional TLB flush is ever required.
5918  *
5919  * Context: called by KVM common code with the kvm mmu write lock held
5920  * Return: false
5921  */
5922 bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range)
5923 {
5924 	return gmap_unmap_gfn_range(kvm->arch.gmap, range->slot, range->start, range->end);
5925 }
5926 
5927 static inline unsigned long nonhyp_mask(int i)
5928 {
5929 	unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
5930 
5931 	return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
5932 }
5933 
5934 static int __init kvm_s390_init(void)
5935 {
5936 	int i, r;
5937 
5938 	if (!sclp.has_sief2) {
5939 		pr_info("SIE is not available\n");
5940 		return -ENODEV;
5941 	}
5942 
5943 	if (hpage_2g && !hpage) {
5944 		hpage_2g = 0;
5945 		pr_info("Disabling 2G hugepage support, since 1M hugepage support is not enabled.\n");
5946 	}
5947 
5948 	for (i = 0; i < HMFAI_DWORDS; i++)
5949 		kvm_s390_fac_base[i] |= nonhyp_mask(i);
5950 
5951 	r = __kvm_s390_init();
5952 	if (r)
5953 		return r;
5954 
5955 	r = kvm_init(sizeof(struct kvm_vcpu), 0, THIS_MODULE);
5956 	if (r) {
5957 		__kvm_s390_exit();
5958 		return r;
5959 	}
5960 	return 0;
5961 }
5962 
5963 static void __exit kvm_s390_exit(void)
5964 {
5965 	kvm_exit();
5966 
5967 	__kvm_s390_exit();
5968 }
5969 
5970 module_init(kvm_s390_init);
5971 module_exit(kvm_s390_exit);
5972 
5973 /*
5974  * Enable autoloading of the kvm module.
5975  * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5976  * since x86 takes a different approach.
5977  */
5978 #include <linux/miscdevice.h>
5979 MODULE_ALIAS_MISCDEV(KVM_MINOR);
5980 MODULE_ALIAS("devname:kvm");
5981