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