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