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