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