]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/s390/kvm/kvm-s390.c
KVM: s390: enable MSA9 keywrapping functions depending on cpu model
[mirror_ubuntu-bionic-kernel.git] / arch / s390 / kvm / kvm-s390.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * hosting IBM Z kernel virtual machines (s390x)
4 *
5 * Copyright IBM Corp. 2008, 2018
6 *
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
9 * Heiko Carstens <heiko.carstens@de.ibm.com>
10 * Christian Ehrhardt <ehrhardt@de.ibm.com>
11 * Jason J. Herne <jjherne@us.ibm.com>
12 */
13
14 #include <linux/compiler.h>
15 #include <linux/err.h>
16 #include <linux/fs.h>
17 #include <linux/hrtimer.h>
18 #include <linux/init.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_host.h>
21 #include <linux/mman.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/random.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/vmalloc.h>
28 #include <linux/bitmap.h>
29 #include <linux/sched/signal.h>
30 #include <linux/string.h>
31
32 #include <asm/asm-offsets.h>
33 #include <asm/lowcore.h>
34 #include <asm/stp.h>
35 #include <asm/pgtable.h>
36 #include <asm/gmap.h>
37 #include <asm/nmi.h>
38 #include <asm/switch_to.h>
39 #include <asm/isc.h>
40 #include <asm/sclp.h>
41 #include <asm/cpacf.h>
42 #include <asm/timex.h>
43 #include <asm/ap.h>
44 #include "kvm-s390.h"
45 #include "gaccess.h"
46
47 #define KMSG_COMPONENT "kvm-s390"
48 #undef pr_fmt
49 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
50
51 #define CREATE_TRACE_POINTS
52 #include "trace.h"
53 #include "trace-s390.h"
54
55 #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
56 #define LOCAL_IRQS 32
57 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
58 (KVM_MAX_VCPUS + LOCAL_IRQS))
59
60 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
61
62 struct kvm_stats_debugfs_item debugfs_entries[] = {
63 { "userspace_handled", VCPU_STAT(exit_userspace) },
64 { "exit_null", VCPU_STAT(exit_null) },
65 { "exit_validity", VCPU_STAT(exit_validity) },
66 { "exit_stop_request", VCPU_STAT(exit_stop_request) },
67 { "exit_external_request", VCPU_STAT(exit_external_request) },
68 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
69 { "exit_instruction", VCPU_STAT(exit_instruction) },
70 { "exit_pei", VCPU_STAT(exit_pei) },
71 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
72 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
73 { "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
74 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
75 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
76 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
77 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
78 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
79 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
80 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
81 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
82 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
83 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
84 { "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
85 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt) },
86 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
87 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
88 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
89 { "deliver_program_interruption", VCPU_STAT(deliver_program_int) },
90 { "exit_wait_state", VCPU_STAT(exit_wait_state) },
91 { "instruction_epsw", VCPU_STAT(instruction_epsw) },
92 { "instruction_gs", VCPU_STAT(instruction_gs) },
93 { "instruction_io_other", VCPU_STAT(instruction_io_other) },
94 { "instruction_lpsw", VCPU_STAT(instruction_lpsw) },
95 { "instruction_lpswe", VCPU_STAT(instruction_lpswe) },
96 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
97 { "instruction_ptff", VCPU_STAT(instruction_ptff) },
98 { "instruction_stidp", VCPU_STAT(instruction_stidp) },
99 { "instruction_sck", VCPU_STAT(instruction_sck) },
100 { "instruction_sckpf", VCPU_STAT(instruction_sckpf) },
101 { "instruction_spx", VCPU_STAT(instruction_spx) },
102 { "instruction_stpx", VCPU_STAT(instruction_stpx) },
103 { "instruction_stap", VCPU_STAT(instruction_stap) },
104 { "instruction_iske", VCPU_STAT(instruction_iske) },
105 { "instruction_ri", VCPU_STAT(instruction_ri) },
106 { "instruction_rrbe", VCPU_STAT(instruction_rrbe) },
107 { "instruction_sske", VCPU_STAT(instruction_sske) },
108 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
109 { "instruction_essa", VCPU_STAT(instruction_essa) },
110 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
111 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
112 { "instruction_tb", VCPU_STAT(instruction_tb) },
113 { "instruction_tpi", VCPU_STAT(instruction_tpi) },
114 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
115 { "instruction_tsch", VCPU_STAT(instruction_tsch) },
116 { "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
117 { "instruction_sie", VCPU_STAT(instruction_sie) },
118 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
119 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
120 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
121 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
122 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
123 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
124 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
125 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
126 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
127 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
128 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
129 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
130 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
131 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
132 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
133 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
134 { "instruction_diag_10", VCPU_STAT(diagnose_10) },
135 { "instruction_diag_44", VCPU_STAT(diagnose_44) },
136 { "instruction_diag_9c", VCPU_STAT(diagnose_9c) },
137 { "instruction_diag_258", VCPU_STAT(diagnose_258) },
138 { "instruction_diag_308", VCPU_STAT(diagnose_308) },
139 { "instruction_diag_500", VCPU_STAT(diagnose_500) },
140 { "instruction_diag_other", VCPU_STAT(diagnose_other) },
141 { NULL }
142 };
143
144 struct kvm_s390_tod_clock_ext {
145 __u8 epoch_idx;
146 __u64 tod;
147 __u8 reserved[7];
148 } __packed;
149
150 /* allow nested virtualization in KVM (if enabled by user space) */
151 static int nested;
152 module_param(nested, int, S_IRUGO);
153 MODULE_PARM_DESC(nested, "Nested virtualization support");
154
155
156 /*
157 * For now we handle at most 16 double words as this is what the s390 base
158 * kernel handles and stores in the prefix page. If we ever need to go beyond
159 * this, this requires changes to code, but the external uapi can stay.
160 */
161 #define SIZE_INTERNAL 16
162
163 /*
164 * Base feature mask that defines default mask for facilities. Consists of the
165 * defines in FACILITIES_KVM and the non-hypervisor managed bits.
166 */
167 static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
168 /*
169 * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
170 * and defines the facilities that can be enabled via a cpu model.
171 */
172 static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
173
174 static unsigned long kvm_s390_fac_size(void)
175 {
176 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
177 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
178 BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
179 sizeof(S390_lowcore.stfle_fac_list));
180
181 return SIZE_INTERNAL;
182 }
183
184 /* available cpu features supported by kvm */
185 static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
186 /* available subfunctions indicated via query / "test bit" */
187 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
188
189 static struct gmap_notifier gmap_notifier;
190 static struct gmap_notifier vsie_gmap_notifier;
191 debug_info_t *kvm_s390_dbf;
192
193 /* Section: not file related */
194 int kvm_arch_hardware_enable(void)
195 {
196 /* every s390 is virtualization enabled ;-) */
197 return 0;
198 }
199
200 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
201 unsigned long end);
202
203 static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
204 {
205 u8 delta_idx = 0;
206
207 /*
208 * The TOD jumps by delta, we have to compensate this by adding
209 * -delta to the epoch.
210 */
211 delta = -delta;
212
213 /* sign-extension - we're adding to signed values below */
214 if ((s64)delta < 0)
215 delta_idx = -1;
216
217 scb->epoch += delta;
218 if (scb->ecd & ECD_MEF) {
219 scb->epdx += delta_idx;
220 if (scb->epoch < delta)
221 scb->epdx += 1;
222 }
223 }
224
225 /*
226 * This callback is executed during stop_machine(). All CPUs are therefore
227 * temporarily stopped. In order not to change guest behavior, we have to
228 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
229 * so a CPU won't be stopped while calculating with the epoch.
230 */
231 static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
232 void *v)
233 {
234 struct kvm *kvm;
235 struct kvm_vcpu *vcpu;
236 int i;
237 unsigned long long *delta = v;
238
239 list_for_each_entry(kvm, &vm_list, vm_list) {
240 kvm_for_each_vcpu(i, vcpu, kvm) {
241 kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
242 if (i == 0) {
243 kvm->arch.epoch = vcpu->arch.sie_block->epoch;
244 kvm->arch.epdx = vcpu->arch.sie_block->epdx;
245 }
246 if (vcpu->arch.cputm_enabled)
247 vcpu->arch.cputm_start += *delta;
248 if (vcpu->arch.vsie_block)
249 kvm_clock_sync_scb(vcpu->arch.vsie_block,
250 *delta);
251 }
252 }
253 return NOTIFY_OK;
254 }
255
256 static struct notifier_block kvm_clock_notifier = {
257 .notifier_call = kvm_clock_sync,
258 };
259
260 int kvm_arch_hardware_setup(void)
261 {
262 gmap_notifier.notifier_call = kvm_gmap_notifier;
263 gmap_register_pte_notifier(&gmap_notifier);
264 vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
265 gmap_register_pte_notifier(&vsie_gmap_notifier);
266 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
267 &kvm_clock_notifier);
268 return 0;
269 }
270
271 void kvm_arch_hardware_unsetup(void)
272 {
273 gmap_unregister_pte_notifier(&gmap_notifier);
274 gmap_unregister_pte_notifier(&vsie_gmap_notifier);
275 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
276 &kvm_clock_notifier);
277 }
278
279 static void allow_cpu_feat(unsigned long nr)
280 {
281 set_bit_inv(nr, kvm_s390_available_cpu_feat);
282 }
283
284 static inline int plo_test_bit(unsigned char nr)
285 {
286 register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
287 int cc;
288
289 asm volatile(
290 /* Parameter registers are ignored for "test bit" */
291 " plo 0,0,0,0(0)\n"
292 " ipm %0\n"
293 " srl %0,28\n"
294 : "=d" (cc)
295 : "d" (r0)
296 : "cc");
297 return cc == 0;
298 }
299
300 static inline void __insn32_query(unsigned int opcode, u8 query[32])
301 {
302 register unsigned long r0 asm("0") = 0; /* query function */
303 register unsigned long r1 asm("1") = (unsigned long) query;
304
305 asm volatile(
306 /* Parameter regs are ignored */
307 " .insn rrf,%[opc] << 16,2,4,6,0\n"
308 : "=m" (*query)
309 : "d" (r0), "a" (r1), [opc] "i" (opcode)
310 : "cc");
311 }
312
313 #define INSN_SORTL 0xb938
314 #define INSN_DFLTCC 0xb939
315
316 static void kvm_s390_cpu_feat_init(void)
317 {
318 int i;
319
320 for (i = 0; i < 256; ++i) {
321 if (plo_test_bit(i))
322 kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
323 }
324
325 if (test_facility(28)) /* TOD-clock steering */
326 ptff(kvm_s390_available_subfunc.ptff,
327 sizeof(kvm_s390_available_subfunc.ptff),
328 PTFF_QAF);
329
330 if (test_facility(17)) { /* MSA */
331 __cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
332 kvm_s390_available_subfunc.kmac);
333 __cpacf_query(CPACF_KMC, (cpacf_mask_t *)
334 kvm_s390_available_subfunc.kmc);
335 __cpacf_query(CPACF_KM, (cpacf_mask_t *)
336 kvm_s390_available_subfunc.km);
337 __cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
338 kvm_s390_available_subfunc.kimd);
339 __cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
340 kvm_s390_available_subfunc.klmd);
341 }
342 if (test_facility(76)) /* MSA3 */
343 __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
344 kvm_s390_available_subfunc.pckmo);
345 if (test_facility(77)) { /* MSA4 */
346 __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
347 kvm_s390_available_subfunc.kmctr);
348 __cpacf_query(CPACF_KMF, (cpacf_mask_t *)
349 kvm_s390_available_subfunc.kmf);
350 __cpacf_query(CPACF_KMO, (cpacf_mask_t *)
351 kvm_s390_available_subfunc.kmo);
352 __cpacf_query(CPACF_PCC, (cpacf_mask_t *)
353 kvm_s390_available_subfunc.pcc);
354 }
355 if (test_facility(57)) /* MSA5 */
356 __cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
357 kvm_s390_available_subfunc.ppno);
358
359 if (test_facility(146)) /* MSA8 */
360 __cpacf_query(CPACF_KMA, (cpacf_mask_t *)
361 kvm_s390_available_subfunc.kma);
362
363 if (test_facility(155)) /* MSA9 */
364 __cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
365 kvm_s390_available_subfunc.kdsa);
366
367 if (test_facility(150)) /* SORTL */
368 __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
369
370 if (test_facility(151)) /* DFLTCC */
371 __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
372
373 if (MACHINE_HAS_ESOP)
374 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
375 /*
376 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
377 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
378 */
379 if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
380 !test_facility(3) || !nested)
381 return;
382 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
383 if (sclp.has_64bscao)
384 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
385 if (sclp.has_siif)
386 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
387 if (sclp.has_gpere)
388 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
389 if (sclp.has_gsls)
390 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
391 if (sclp.has_ib)
392 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
393 if (sclp.has_cei)
394 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
395 if (sclp.has_ibs)
396 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
397 if (sclp.has_kss)
398 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
399 /*
400 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
401 * all skey handling functions read/set the skey from the PGSTE
402 * instead of the real storage key.
403 *
404 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
405 * pages being detected as preserved although they are resident.
406 *
407 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
408 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
409 *
410 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
411 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
412 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
413 *
414 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
415 * cannot easily shadow the SCA because of the ipte lock.
416 */
417 }
418
419 int kvm_arch_init(void *opaque)
420 {
421 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
422 if (!kvm_s390_dbf)
423 return -ENOMEM;
424
425 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
426 debug_unregister(kvm_s390_dbf);
427 return -ENOMEM;
428 }
429
430 kvm_s390_cpu_feat_init();
431
432 /* Register floating interrupt controller interface. */
433 return kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
434 }
435
436 void kvm_arch_exit(void)
437 {
438 debug_unregister(kvm_s390_dbf);
439 }
440
441 /* Section: device related */
442 long kvm_arch_dev_ioctl(struct file *filp,
443 unsigned int ioctl, unsigned long arg)
444 {
445 if (ioctl == KVM_S390_ENABLE_SIE)
446 return s390_enable_sie();
447 return -EINVAL;
448 }
449
450 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
451 {
452 int r;
453
454 switch (ext) {
455 case KVM_CAP_S390_PSW:
456 case KVM_CAP_S390_GMAP:
457 case KVM_CAP_SYNC_MMU:
458 #ifdef CONFIG_KVM_S390_UCONTROL
459 case KVM_CAP_S390_UCONTROL:
460 #endif
461 case KVM_CAP_ASYNC_PF:
462 case KVM_CAP_SYNC_REGS:
463 case KVM_CAP_ONE_REG:
464 case KVM_CAP_ENABLE_CAP:
465 case KVM_CAP_S390_CSS_SUPPORT:
466 case KVM_CAP_IOEVENTFD:
467 case KVM_CAP_DEVICE_CTRL:
468 case KVM_CAP_ENABLE_CAP_VM:
469 case KVM_CAP_S390_IRQCHIP:
470 case KVM_CAP_VM_ATTRIBUTES:
471 case KVM_CAP_MP_STATE:
472 case KVM_CAP_IMMEDIATE_EXIT:
473 case KVM_CAP_S390_INJECT_IRQ:
474 case KVM_CAP_S390_USER_SIGP:
475 case KVM_CAP_S390_USER_STSI:
476 case KVM_CAP_S390_SKEYS:
477 case KVM_CAP_S390_IRQ_STATE:
478 case KVM_CAP_S390_USER_INSTR0:
479 case KVM_CAP_S390_CMMA_MIGRATION:
480 case KVM_CAP_S390_AIS:
481 case KVM_CAP_S390_AIS_MIGRATION:
482 r = 1;
483 break;
484 case KVM_CAP_S390_MEM_OP:
485 r = MEM_OP_MAX_SIZE;
486 break;
487 case KVM_CAP_NR_VCPUS:
488 case KVM_CAP_MAX_VCPUS:
489 r = KVM_S390_BSCA_CPU_SLOTS;
490 if (!kvm_s390_use_sca_entries())
491 r = KVM_MAX_VCPUS;
492 else if (sclp.has_esca && sclp.has_64bscao)
493 r = KVM_S390_ESCA_CPU_SLOTS;
494 break;
495 case KVM_CAP_NR_MEMSLOTS:
496 r = KVM_USER_MEM_SLOTS;
497 break;
498 case KVM_CAP_S390_COW:
499 r = MACHINE_HAS_ESOP;
500 break;
501 case KVM_CAP_S390_VECTOR_REGISTERS:
502 r = MACHINE_HAS_VX;
503 break;
504 case KVM_CAP_S390_RI:
505 r = test_facility(64);
506 break;
507 case KVM_CAP_S390_GS:
508 r = test_facility(133);
509 break;
510 case KVM_CAP_S390_BPB:
511 r = test_facility(82);
512 break;
513 default:
514 r = 0;
515 }
516 return r;
517 }
518
519 static void kvm_s390_sync_dirty_log(struct kvm *kvm,
520 struct kvm_memory_slot *memslot)
521 {
522 gfn_t cur_gfn, last_gfn;
523 unsigned long address;
524 struct gmap *gmap = kvm->arch.gmap;
525
526 /* Loop over all guest pages */
527 last_gfn = memslot->base_gfn + memslot->npages;
528 for (cur_gfn = memslot->base_gfn; cur_gfn <= last_gfn; cur_gfn++) {
529 address = gfn_to_hva_memslot(memslot, cur_gfn);
530
531 if (test_and_clear_guest_dirty(gmap->mm, address))
532 mark_page_dirty(kvm, cur_gfn);
533 if (fatal_signal_pending(current))
534 return;
535 cond_resched();
536 }
537 }
538
539 /* Section: vm related */
540 static void sca_del_vcpu(struct kvm_vcpu *vcpu);
541
542 /*
543 * Get (and clear) the dirty memory log for a memory slot.
544 */
545 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
546 struct kvm_dirty_log *log)
547 {
548 int r;
549 unsigned long n;
550 struct kvm_memslots *slots;
551 struct kvm_memory_slot *memslot;
552 int is_dirty = 0;
553
554 if (kvm_is_ucontrol(kvm))
555 return -EINVAL;
556
557 mutex_lock(&kvm->slots_lock);
558
559 r = -EINVAL;
560 if (log->slot >= KVM_USER_MEM_SLOTS)
561 goto out;
562
563 slots = kvm_memslots(kvm);
564 memslot = id_to_memslot(slots, log->slot);
565 r = -ENOENT;
566 if (!memslot->dirty_bitmap)
567 goto out;
568
569 kvm_s390_sync_dirty_log(kvm, memslot);
570 r = kvm_get_dirty_log(kvm, log, &is_dirty);
571 if (r)
572 goto out;
573
574 /* Clear the dirty log */
575 if (is_dirty) {
576 n = kvm_dirty_bitmap_bytes(memslot);
577 memset(memslot->dirty_bitmap, 0, n);
578 }
579 r = 0;
580 out:
581 mutex_unlock(&kvm->slots_lock);
582 return r;
583 }
584
585 static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
586 {
587 unsigned int i;
588 struct kvm_vcpu *vcpu;
589
590 kvm_for_each_vcpu(i, vcpu, kvm) {
591 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
592 }
593 }
594
595 static int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
596 {
597 int r;
598
599 if (cap->flags)
600 return -EINVAL;
601
602 switch (cap->cap) {
603 case KVM_CAP_S390_IRQCHIP:
604 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
605 kvm->arch.use_irqchip = 1;
606 r = 0;
607 break;
608 case KVM_CAP_S390_USER_SIGP:
609 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
610 kvm->arch.user_sigp = 1;
611 r = 0;
612 break;
613 case KVM_CAP_S390_VECTOR_REGISTERS:
614 mutex_lock(&kvm->lock);
615 if (kvm->created_vcpus) {
616 r = -EBUSY;
617 } else if (MACHINE_HAS_VX) {
618 set_kvm_facility(kvm->arch.model.fac_mask, 129);
619 set_kvm_facility(kvm->arch.model.fac_list, 129);
620 if (test_facility(134)) {
621 set_kvm_facility(kvm->arch.model.fac_mask, 134);
622 set_kvm_facility(kvm->arch.model.fac_list, 134);
623 }
624 if (test_facility(135)) {
625 set_kvm_facility(kvm->arch.model.fac_mask, 135);
626 set_kvm_facility(kvm->arch.model.fac_list, 135);
627 }
628 if (test_facility(148)) {
629 set_kvm_facility(kvm->arch.model.fac_mask, 148);
630 set_kvm_facility(kvm->arch.model.fac_list, 148);
631 }
632 if (test_facility(152)) {
633 set_kvm_facility(kvm->arch.model.fac_mask, 152);
634 set_kvm_facility(kvm->arch.model.fac_list, 152);
635 }
636 r = 0;
637 } else
638 r = -EINVAL;
639 mutex_unlock(&kvm->lock);
640 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
641 r ? "(not available)" : "(success)");
642 break;
643 case KVM_CAP_S390_RI:
644 r = -EINVAL;
645 mutex_lock(&kvm->lock);
646 if (kvm->created_vcpus) {
647 r = -EBUSY;
648 } else if (test_facility(64)) {
649 set_kvm_facility(kvm->arch.model.fac_mask, 64);
650 set_kvm_facility(kvm->arch.model.fac_list, 64);
651 r = 0;
652 }
653 mutex_unlock(&kvm->lock);
654 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
655 r ? "(not available)" : "(success)");
656 break;
657 case KVM_CAP_S390_AIS:
658 mutex_lock(&kvm->lock);
659 if (kvm->created_vcpus) {
660 r = -EBUSY;
661 } else {
662 set_kvm_facility(kvm->arch.model.fac_mask, 72);
663 set_kvm_facility(kvm->arch.model.fac_list, 72);
664 r = 0;
665 }
666 mutex_unlock(&kvm->lock);
667 VM_EVENT(kvm, 3, "ENABLE: AIS %s",
668 r ? "(not available)" : "(success)");
669 break;
670 case KVM_CAP_S390_GS:
671 r = -EINVAL;
672 mutex_lock(&kvm->lock);
673 if (kvm->created_vcpus) {
674 r = -EBUSY;
675 } else if (test_facility(133)) {
676 set_kvm_facility(kvm->arch.model.fac_mask, 133);
677 set_kvm_facility(kvm->arch.model.fac_list, 133);
678 r = 0;
679 }
680 mutex_unlock(&kvm->lock);
681 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
682 r ? "(not available)" : "(success)");
683 break;
684 case KVM_CAP_S390_USER_STSI:
685 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
686 kvm->arch.user_stsi = 1;
687 r = 0;
688 break;
689 case KVM_CAP_S390_USER_INSTR0:
690 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
691 kvm->arch.user_instr0 = 1;
692 icpt_operexc_on_all_vcpus(kvm);
693 r = 0;
694 break;
695 default:
696 r = -EINVAL;
697 break;
698 }
699 return r;
700 }
701
702 static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
703 {
704 int ret;
705
706 switch (attr->attr) {
707 case KVM_S390_VM_MEM_LIMIT_SIZE:
708 ret = 0;
709 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
710 kvm->arch.mem_limit);
711 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
712 ret = -EFAULT;
713 break;
714 default:
715 ret = -ENXIO;
716 break;
717 }
718 return ret;
719 }
720
721 static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
722 {
723 int ret;
724 unsigned int idx;
725 switch (attr->attr) {
726 case KVM_S390_VM_MEM_ENABLE_CMMA:
727 ret = -ENXIO;
728 if (!sclp.has_cmma)
729 break;
730
731 ret = -EBUSY;
732 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
733 mutex_lock(&kvm->lock);
734 if (!kvm->created_vcpus) {
735 kvm->arch.use_cmma = 1;
736 ret = 0;
737 }
738 mutex_unlock(&kvm->lock);
739 break;
740 case KVM_S390_VM_MEM_CLR_CMMA:
741 ret = -ENXIO;
742 if (!sclp.has_cmma)
743 break;
744 ret = -EINVAL;
745 if (!kvm->arch.use_cmma)
746 break;
747
748 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
749 mutex_lock(&kvm->lock);
750 idx = srcu_read_lock(&kvm->srcu);
751 s390_reset_cmma(kvm->arch.gmap->mm);
752 srcu_read_unlock(&kvm->srcu, idx);
753 mutex_unlock(&kvm->lock);
754 ret = 0;
755 break;
756 case KVM_S390_VM_MEM_LIMIT_SIZE: {
757 unsigned long new_limit;
758
759 if (kvm_is_ucontrol(kvm))
760 return -EINVAL;
761
762 if (get_user(new_limit, (u64 __user *)attr->addr))
763 return -EFAULT;
764
765 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
766 new_limit > kvm->arch.mem_limit)
767 return -E2BIG;
768
769 if (!new_limit)
770 return -EINVAL;
771
772 /* gmap_create takes last usable address */
773 if (new_limit != KVM_S390_NO_MEM_LIMIT)
774 new_limit -= 1;
775
776 ret = -EBUSY;
777 mutex_lock(&kvm->lock);
778 if (!kvm->created_vcpus) {
779 /* gmap_create will round the limit up */
780 struct gmap *new = gmap_create(current->mm, new_limit);
781
782 if (!new) {
783 ret = -ENOMEM;
784 } else {
785 gmap_remove(kvm->arch.gmap);
786 new->private = kvm;
787 kvm->arch.gmap = new;
788 ret = 0;
789 }
790 }
791 mutex_unlock(&kvm->lock);
792 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
793 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
794 (void *) kvm->arch.gmap->asce);
795 break;
796 }
797 default:
798 ret = -ENXIO;
799 break;
800 }
801 return ret;
802 }
803
804 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
805
806 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
807 {
808 struct kvm_vcpu *vcpu;
809 int i;
810
811 kvm_s390_vcpu_block_all(kvm);
812
813 kvm_for_each_vcpu(i, vcpu, kvm) {
814 kvm_s390_vcpu_crypto_setup(vcpu);
815 /* recreate the shadow crycb by leaving the VSIE handler */
816 kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
817 }
818
819 kvm_s390_vcpu_unblock_all(kvm);
820 }
821
822 static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
823 {
824 mutex_lock(&kvm->lock);
825 switch (attr->attr) {
826 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
827 if (!test_kvm_facility(kvm, 76)) {
828 mutex_unlock(&kvm->lock);
829 return -EINVAL;
830 }
831 get_random_bytes(
832 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
833 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
834 kvm->arch.crypto.aes_kw = 1;
835 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
836 break;
837 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
838 if (!test_kvm_facility(kvm, 76)) {
839 mutex_unlock(&kvm->lock);
840 return -EINVAL;
841 }
842 get_random_bytes(
843 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
844 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
845 kvm->arch.crypto.dea_kw = 1;
846 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
847 break;
848 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
849 if (!test_kvm_facility(kvm, 76)) {
850 mutex_unlock(&kvm->lock);
851 return -EINVAL;
852 }
853 kvm->arch.crypto.aes_kw = 0;
854 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
855 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
856 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
857 break;
858 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
859 if (!test_kvm_facility(kvm, 76)) {
860 mutex_unlock(&kvm->lock);
861 return -EINVAL;
862 }
863 kvm->arch.crypto.dea_kw = 0;
864 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
865 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
866 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
867 break;
868 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
869 if (!ap_instructions_available()) {
870 mutex_unlock(&kvm->lock);
871 return -EOPNOTSUPP;
872 }
873 kvm->arch.crypto.apie = 1;
874 break;
875 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
876 if (!ap_instructions_available()) {
877 mutex_unlock(&kvm->lock);
878 return -EOPNOTSUPP;
879 }
880 kvm->arch.crypto.apie = 0;
881 break;
882 default:
883 mutex_unlock(&kvm->lock);
884 return -ENXIO;
885 }
886
887 kvm_s390_vcpu_crypto_reset_all(kvm);
888 mutex_unlock(&kvm->lock);
889 return 0;
890 }
891
892 static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
893 {
894 int cx;
895 struct kvm_vcpu *vcpu;
896
897 kvm_for_each_vcpu(cx, vcpu, kvm)
898 kvm_s390_sync_request(req, vcpu);
899 }
900
901 /*
902 * Must be called with kvm->srcu held to avoid races on memslots, and with
903 * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
904 */
905 static int kvm_s390_vm_start_migration(struct kvm *kvm)
906 {
907 struct kvm_s390_migration_state *mgs;
908 struct kvm_memory_slot *ms;
909 /* should be the only one */
910 struct kvm_memslots *slots;
911 unsigned long ram_pages;
912 int slotnr;
913
914 /* migration mode already enabled */
915 if (kvm->arch.migration_state)
916 return 0;
917
918 slots = kvm_memslots(kvm);
919 if (!slots || !slots->used_slots)
920 return -EINVAL;
921
922 mgs = kzalloc(sizeof(*mgs), GFP_KERNEL);
923 if (!mgs)
924 return -ENOMEM;
925 kvm->arch.migration_state = mgs;
926
927 if (kvm->arch.use_cmma) {
928 /*
929 * Get the first slot. They are reverse sorted by base_gfn, so
930 * the first slot is also the one at the end of the address
931 * space. We have verified above that at least one slot is
932 * present.
933 */
934 ms = slots->memslots;
935 /* round up so we only use full longs */
936 ram_pages = roundup(ms->base_gfn + ms->npages, BITS_PER_LONG);
937 /* allocate enough bytes to store all the bits */
938 mgs->pgste_bitmap = vmalloc(ram_pages / 8);
939 if (!mgs->pgste_bitmap) {
940 kfree(mgs);
941 kvm->arch.migration_state = NULL;
942 return -ENOMEM;
943 }
944
945 mgs->bitmap_size = ram_pages;
946 atomic64_set(&mgs->dirty_pages, ram_pages);
947 /* mark all the pages in active slots as dirty */
948 for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
949 ms = slots->memslots + slotnr;
950 bitmap_set(mgs->pgste_bitmap, ms->base_gfn, ms->npages);
951 }
952
953 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
954 }
955 return 0;
956 }
957
958 /*
959 * Must be called with kvm->slots_lock to avoid races with ourselves and
960 * kvm_s390_vm_start_migration.
961 */
962 static int kvm_s390_vm_stop_migration(struct kvm *kvm)
963 {
964 struct kvm_s390_migration_state *mgs;
965
966 /* migration mode already disabled */
967 if (!kvm->arch.migration_state)
968 return 0;
969 mgs = kvm->arch.migration_state;
970 kvm->arch.migration_state = NULL;
971
972 if (kvm->arch.use_cmma) {
973 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
974 /* We have to wait for the essa emulation to finish */
975 synchronize_srcu(&kvm->srcu);
976 vfree(mgs->pgste_bitmap);
977 }
978 kfree(mgs);
979 return 0;
980 }
981
982 static int kvm_s390_vm_set_migration(struct kvm *kvm,
983 struct kvm_device_attr *attr)
984 {
985 int res = -ENXIO;
986
987 mutex_lock(&kvm->slots_lock);
988 switch (attr->attr) {
989 case KVM_S390_VM_MIGRATION_START:
990 res = kvm_s390_vm_start_migration(kvm);
991 break;
992 case KVM_S390_VM_MIGRATION_STOP:
993 res = kvm_s390_vm_stop_migration(kvm);
994 break;
995 default:
996 break;
997 }
998 mutex_unlock(&kvm->slots_lock);
999
1000 return res;
1001 }
1002
1003 static int kvm_s390_vm_get_migration(struct kvm *kvm,
1004 struct kvm_device_attr *attr)
1005 {
1006 u64 mig = (kvm->arch.migration_state != NULL);
1007
1008 if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1009 return -ENXIO;
1010
1011 if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1012 return -EFAULT;
1013 return 0;
1014 }
1015
1016 static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1017 {
1018 struct kvm_s390_vm_tod_clock gtod;
1019
1020 if (copy_from_user(&gtod, (void __user *)attr->addr, sizeof(gtod)))
1021 return -EFAULT;
1022
1023 if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
1024 return -EINVAL;
1025 kvm_s390_set_tod_clock(kvm, &gtod);
1026
1027 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
1028 gtod.epoch_idx, gtod.tod);
1029
1030 return 0;
1031 }
1032
1033 static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1034 {
1035 u8 gtod_high;
1036
1037 if (copy_from_user(&gtod_high, (void __user *)attr->addr,
1038 sizeof(gtod_high)))
1039 return -EFAULT;
1040
1041 if (gtod_high != 0)
1042 return -EINVAL;
1043 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
1044
1045 return 0;
1046 }
1047
1048 static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1049 {
1050 struct kvm_s390_vm_tod_clock gtod = { 0 };
1051
1052 if (copy_from_user(&gtod.tod, (void __user *)attr->addr,
1053 sizeof(gtod.tod)))
1054 return -EFAULT;
1055
1056 kvm_s390_set_tod_clock(kvm, &gtod);
1057 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
1058 return 0;
1059 }
1060
1061 static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1062 {
1063 int ret;
1064
1065 if (attr->flags)
1066 return -EINVAL;
1067
1068 switch (attr->attr) {
1069 case KVM_S390_VM_TOD_EXT:
1070 ret = kvm_s390_set_tod_ext(kvm, attr);
1071 break;
1072 case KVM_S390_VM_TOD_HIGH:
1073 ret = kvm_s390_set_tod_high(kvm, attr);
1074 break;
1075 case KVM_S390_VM_TOD_LOW:
1076 ret = kvm_s390_set_tod_low(kvm, attr);
1077 break;
1078 default:
1079 ret = -ENXIO;
1080 break;
1081 }
1082 return ret;
1083 }
1084
1085 static void kvm_s390_get_tod_clock_ext(struct kvm *kvm,
1086 struct kvm_s390_vm_tod_clock *gtod)
1087 {
1088 struct kvm_s390_tod_clock_ext htod;
1089
1090 preempt_disable();
1091
1092 get_tod_clock_ext((char *)&htod);
1093
1094 gtod->tod = htod.tod + kvm->arch.epoch;
1095 gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx;
1096
1097 if (gtod->tod < htod.tod)
1098 gtod->epoch_idx += 1;
1099
1100 preempt_enable();
1101 }
1102
1103 static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1104 {
1105 struct kvm_s390_vm_tod_clock gtod;
1106
1107 memset(&gtod, 0, sizeof(gtod));
1108
1109 if (test_kvm_facility(kvm, 139))
1110 kvm_s390_get_tod_clock_ext(kvm, &gtod);
1111 else
1112 gtod.tod = kvm_s390_get_tod_clock_fast(kvm);
1113
1114 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
1115 return -EFAULT;
1116
1117 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1118 gtod.epoch_idx, gtod.tod);
1119 return 0;
1120 }
1121
1122 static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1123 {
1124 u8 gtod_high = 0;
1125
1126 if (copy_to_user((void __user *)attr->addr, &gtod_high,
1127 sizeof(gtod_high)))
1128 return -EFAULT;
1129 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
1130
1131 return 0;
1132 }
1133
1134 static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1135 {
1136 u64 gtod;
1137
1138 gtod = kvm_s390_get_tod_clock_fast(kvm);
1139 if (copy_to_user((void __user *)attr->addr, &gtod, sizeof(gtod)))
1140 return -EFAULT;
1141 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
1142
1143 return 0;
1144 }
1145
1146 static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1147 {
1148 int ret;
1149
1150 if (attr->flags)
1151 return -EINVAL;
1152
1153 switch (attr->attr) {
1154 case KVM_S390_VM_TOD_EXT:
1155 ret = kvm_s390_get_tod_ext(kvm, attr);
1156 break;
1157 case KVM_S390_VM_TOD_HIGH:
1158 ret = kvm_s390_get_tod_high(kvm, attr);
1159 break;
1160 case KVM_S390_VM_TOD_LOW:
1161 ret = kvm_s390_get_tod_low(kvm, attr);
1162 break;
1163 default:
1164 ret = -ENXIO;
1165 break;
1166 }
1167 return ret;
1168 }
1169
1170 static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1171 {
1172 struct kvm_s390_vm_cpu_processor *proc;
1173 u16 lowest_ibc, unblocked_ibc;
1174 int ret = 0;
1175
1176 mutex_lock(&kvm->lock);
1177 if (kvm->created_vcpus) {
1178 ret = -EBUSY;
1179 goto out;
1180 }
1181 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1182 if (!proc) {
1183 ret = -ENOMEM;
1184 goto out;
1185 }
1186 if (!copy_from_user(proc, (void __user *)attr->addr,
1187 sizeof(*proc))) {
1188 kvm->arch.model.cpuid = proc->cpuid;
1189 lowest_ibc = sclp.ibc >> 16 & 0xfff;
1190 unblocked_ibc = sclp.ibc & 0xfff;
1191 if (lowest_ibc && proc->ibc) {
1192 if (proc->ibc > unblocked_ibc)
1193 kvm->arch.model.ibc = unblocked_ibc;
1194 else if (proc->ibc < lowest_ibc)
1195 kvm->arch.model.ibc = lowest_ibc;
1196 else
1197 kvm->arch.model.ibc = proc->ibc;
1198 }
1199 memcpy(kvm->arch.model.fac_list, proc->fac_list,
1200 S390_ARCH_FAC_LIST_SIZE_BYTE);
1201 VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1202 kvm->arch.model.ibc,
1203 kvm->arch.model.cpuid);
1204 VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1205 kvm->arch.model.fac_list[0],
1206 kvm->arch.model.fac_list[1],
1207 kvm->arch.model.fac_list[2]);
1208 } else
1209 ret = -EFAULT;
1210 kfree(proc);
1211 out:
1212 mutex_unlock(&kvm->lock);
1213 return ret;
1214 }
1215
1216 static int kvm_s390_set_processor_feat(struct kvm *kvm,
1217 struct kvm_device_attr *attr)
1218 {
1219 struct kvm_s390_vm_cpu_feat data;
1220 int ret = -EBUSY;
1221
1222 if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
1223 return -EFAULT;
1224 if (!bitmap_subset((unsigned long *) data.feat,
1225 kvm_s390_available_cpu_feat,
1226 KVM_S390_VM_CPU_FEAT_NR_BITS))
1227 return -EINVAL;
1228
1229 mutex_lock(&kvm->lock);
1230 if (!kvm->created_vcpus) {
1231 bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
1232 KVM_S390_VM_CPU_FEAT_NR_BITS);
1233 ret = 0;
1234 }
1235 mutex_unlock(&kvm->lock);
1236 return ret;
1237 }
1238
1239 static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
1240 struct kvm_device_attr *attr)
1241 {
1242 mutex_lock(&kvm->lock);
1243 if (kvm->created_vcpus) {
1244 mutex_unlock(&kvm->lock);
1245 return -EBUSY;
1246 }
1247
1248 if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1249 sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1250 mutex_unlock(&kvm->lock);
1251 return -EFAULT;
1252 }
1253 mutex_unlock(&kvm->lock);
1254
1255 VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1256 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1257 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1258 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1259 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1260 VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1261 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1262 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1263 VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1264 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1265 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1266 VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1267 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1268 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1269 VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
1270 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1271 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1272 VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1273 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1274 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1275 VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1276 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1277 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1278 VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1279 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1280 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1281 VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1282 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1283 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1284 VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1285 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1286 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1287 VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1288 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1289 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1290 VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1291 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1292 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1293 VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1294 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1295 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1296 VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1297 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1298 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1299 VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1300 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1301 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1302 VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1303 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1304 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1305 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1306 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1307 VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1308 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1309 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1310 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1311 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1312
1313 return 0;
1314 }
1315
1316 static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1317 {
1318 int ret = -ENXIO;
1319
1320 switch (attr->attr) {
1321 case KVM_S390_VM_CPU_PROCESSOR:
1322 ret = kvm_s390_set_processor(kvm, attr);
1323 break;
1324 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1325 ret = kvm_s390_set_processor_feat(kvm, attr);
1326 break;
1327 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1328 ret = kvm_s390_set_processor_subfunc(kvm, attr);
1329 break;
1330 }
1331 return ret;
1332 }
1333
1334 static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1335 {
1336 struct kvm_s390_vm_cpu_processor *proc;
1337 int ret = 0;
1338
1339 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1340 if (!proc) {
1341 ret = -ENOMEM;
1342 goto out;
1343 }
1344 proc->cpuid = kvm->arch.model.cpuid;
1345 proc->ibc = kvm->arch.model.ibc;
1346 memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1347 S390_ARCH_FAC_LIST_SIZE_BYTE);
1348 VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1349 kvm->arch.model.ibc,
1350 kvm->arch.model.cpuid);
1351 VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1352 kvm->arch.model.fac_list[0],
1353 kvm->arch.model.fac_list[1],
1354 kvm->arch.model.fac_list[2]);
1355 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1356 ret = -EFAULT;
1357 kfree(proc);
1358 out:
1359 return ret;
1360 }
1361
1362 static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1363 {
1364 struct kvm_s390_vm_cpu_machine *mach;
1365 int ret = 0;
1366
1367 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1368 if (!mach) {
1369 ret = -ENOMEM;
1370 goto out;
1371 }
1372 get_cpu_id((struct cpuid *) &mach->cpuid);
1373 mach->ibc = sclp.ibc;
1374 memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1375 S390_ARCH_FAC_LIST_SIZE_BYTE);
1376 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
1377 sizeof(S390_lowcore.stfle_fac_list));
1378 VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1379 kvm->arch.model.ibc,
1380 kvm->arch.model.cpuid);
1381 VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1382 mach->fac_mask[0],
1383 mach->fac_mask[1],
1384 mach->fac_mask[2]);
1385 VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1386 mach->fac_list[0],
1387 mach->fac_list[1],
1388 mach->fac_list[2]);
1389 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1390 ret = -EFAULT;
1391 kfree(mach);
1392 out:
1393 return ret;
1394 }
1395
1396 static int kvm_s390_get_processor_feat(struct kvm *kvm,
1397 struct kvm_device_attr *attr)
1398 {
1399 struct kvm_s390_vm_cpu_feat data;
1400
1401 bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
1402 KVM_S390_VM_CPU_FEAT_NR_BITS);
1403 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1404 return -EFAULT;
1405 return 0;
1406 }
1407
1408 static int kvm_s390_get_machine_feat(struct kvm *kvm,
1409 struct kvm_device_attr *attr)
1410 {
1411 struct kvm_s390_vm_cpu_feat data;
1412
1413 bitmap_copy((unsigned long *) data.feat,
1414 kvm_s390_available_cpu_feat,
1415 KVM_S390_VM_CPU_FEAT_NR_BITS);
1416 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1417 return -EFAULT;
1418 return 0;
1419 }
1420
1421 static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
1422 struct kvm_device_attr *attr)
1423 {
1424 if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1425 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1426 return -EFAULT;
1427
1428 VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1429 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1430 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1431 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1432 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1433 VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1434 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1435 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1436 VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1437 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1438 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1439 VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1440 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1441 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1442 VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
1443 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1444 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1445 VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1446 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1447 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1448 VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1449 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1450 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1451 VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1452 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1453 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1454 VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1455 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1456 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1457 VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1458 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1459 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1460 VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1461 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1462 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1463 VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1464 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1465 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1466 VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1467 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1468 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1469 VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1470 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1471 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1472 VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1473 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1474 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1475 VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1476 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1477 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1478 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1479 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1480 VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1481 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1482 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1483 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1484 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1485
1486 return 0;
1487 }
1488
1489 static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
1490 struct kvm_device_attr *attr)
1491 {
1492 if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
1493 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1494 return -EFAULT;
1495
1496 VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1497 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
1498 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
1499 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
1500 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
1501 VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
1502 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
1503 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
1504 VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
1505 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
1506 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
1507 VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
1508 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
1509 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
1510 VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
1511 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
1512 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
1513 VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
1514 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
1515 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
1516 VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
1517 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
1518 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
1519 VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
1520 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
1521 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
1522 VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
1523 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
1524 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
1525 VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
1526 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
1527 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
1528 VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
1529 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
1530 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
1531 VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
1532 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
1533 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
1534 VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
1535 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
1536 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
1537 VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
1538 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
1539 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
1540 VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx",
1541 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
1542 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1543 VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1544 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1545 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1546 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1547 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
1548 VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1549 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
1550 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
1551 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
1552 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
1553
1554 return 0;
1555 }
1556
1557 static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1558 {
1559 int ret = -ENXIO;
1560
1561 switch (attr->attr) {
1562 case KVM_S390_VM_CPU_PROCESSOR:
1563 ret = kvm_s390_get_processor(kvm, attr);
1564 break;
1565 case KVM_S390_VM_CPU_MACHINE:
1566 ret = kvm_s390_get_machine(kvm, attr);
1567 break;
1568 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1569 ret = kvm_s390_get_processor_feat(kvm, attr);
1570 break;
1571 case KVM_S390_VM_CPU_MACHINE_FEAT:
1572 ret = kvm_s390_get_machine_feat(kvm, attr);
1573 break;
1574 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1575 ret = kvm_s390_get_processor_subfunc(kvm, attr);
1576 break;
1577 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1578 ret = kvm_s390_get_machine_subfunc(kvm, attr);
1579 break;
1580 }
1581 return ret;
1582 }
1583
1584 static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1585 {
1586 int ret;
1587
1588 switch (attr->group) {
1589 case KVM_S390_VM_MEM_CTRL:
1590 ret = kvm_s390_set_mem_control(kvm, attr);
1591 break;
1592 case KVM_S390_VM_TOD:
1593 ret = kvm_s390_set_tod(kvm, attr);
1594 break;
1595 case KVM_S390_VM_CPU_MODEL:
1596 ret = kvm_s390_set_cpu_model(kvm, attr);
1597 break;
1598 case KVM_S390_VM_CRYPTO:
1599 ret = kvm_s390_vm_set_crypto(kvm, attr);
1600 break;
1601 case KVM_S390_VM_MIGRATION:
1602 ret = kvm_s390_vm_set_migration(kvm, attr);
1603 break;
1604 default:
1605 ret = -ENXIO;
1606 break;
1607 }
1608
1609 return ret;
1610 }
1611
1612 static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1613 {
1614 int ret;
1615
1616 switch (attr->group) {
1617 case KVM_S390_VM_MEM_CTRL:
1618 ret = kvm_s390_get_mem_control(kvm, attr);
1619 break;
1620 case KVM_S390_VM_TOD:
1621 ret = kvm_s390_get_tod(kvm, attr);
1622 break;
1623 case KVM_S390_VM_CPU_MODEL:
1624 ret = kvm_s390_get_cpu_model(kvm, attr);
1625 break;
1626 case KVM_S390_VM_MIGRATION:
1627 ret = kvm_s390_vm_get_migration(kvm, attr);
1628 break;
1629 default:
1630 ret = -ENXIO;
1631 break;
1632 }
1633
1634 return ret;
1635 }
1636
1637 static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1638 {
1639 int ret;
1640
1641 switch (attr->group) {
1642 case KVM_S390_VM_MEM_CTRL:
1643 switch (attr->attr) {
1644 case KVM_S390_VM_MEM_ENABLE_CMMA:
1645 case KVM_S390_VM_MEM_CLR_CMMA:
1646 ret = sclp.has_cmma ? 0 : -ENXIO;
1647 break;
1648 case KVM_S390_VM_MEM_LIMIT_SIZE:
1649 ret = 0;
1650 break;
1651 default:
1652 ret = -ENXIO;
1653 break;
1654 }
1655 break;
1656 case KVM_S390_VM_TOD:
1657 switch (attr->attr) {
1658 case KVM_S390_VM_TOD_LOW:
1659 case KVM_S390_VM_TOD_HIGH:
1660 ret = 0;
1661 break;
1662 default:
1663 ret = -ENXIO;
1664 break;
1665 }
1666 break;
1667 case KVM_S390_VM_CPU_MODEL:
1668 switch (attr->attr) {
1669 case KVM_S390_VM_CPU_PROCESSOR:
1670 case KVM_S390_VM_CPU_MACHINE:
1671 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1672 case KVM_S390_VM_CPU_MACHINE_FEAT:
1673 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1674 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1675 ret = 0;
1676 break;
1677 default:
1678 ret = -ENXIO;
1679 break;
1680 }
1681 break;
1682 case KVM_S390_VM_CRYPTO:
1683 switch (attr->attr) {
1684 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1685 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1686 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1687 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1688 ret = 0;
1689 break;
1690 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
1691 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
1692 ret = ap_instructions_available() ? 0 : -ENXIO;
1693 break;
1694 default:
1695 ret = -ENXIO;
1696 break;
1697 }
1698 break;
1699 case KVM_S390_VM_MIGRATION:
1700 ret = 0;
1701 break;
1702 default:
1703 ret = -ENXIO;
1704 break;
1705 }
1706
1707 return ret;
1708 }
1709
1710 static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1711 {
1712 uint8_t *keys;
1713 uint64_t hva;
1714 int srcu_idx, i, r = 0;
1715
1716 if (args->flags != 0)
1717 return -EINVAL;
1718
1719 /* Is this guest using storage keys? */
1720 if (!mm_use_skey(current->mm))
1721 return KVM_S390_GET_SKEYS_NONE;
1722
1723 /* Enforce sane limit on memory allocation */
1724 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1725 return -EINVAL;
1726
1727 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
1728 if (!keys)
1729 return -ENOMEM;
1730
1731 down_read(&current->mm->mmap_sem);
1732 srcu_idx = srcu_read_lock(&kvm->srcu);
1733 for (i = 0; i < args->count; i++) {
1734 hva = gfn_to_hva(kvm, args->start_gfn + i);
1735 if (kvm_is_error_hva(hva)) {
1736 r = -EFAULT;
1737 break;
1738 }
1739
1740 r = get_guest_storage_key(current->mm, hva, &keys[i]);
1741 if (r)
1742 break;
1743 }
1744 srcu_read_unlock(&kvm->srcu, srcu_idx);
1745 up_read(&current->mm->mmap_sem);
1746
1747 if (!r) {
1748 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
1749 sizeof(uint8_t) * args->count);
1750 if (r)
1751 r = -EFAULT;
1752 }
1753
1754 kvfree(keys);
1755 return r;
1756 }
1757
1758 static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1759 {
1760 uint8_t *keys;
1761 uint64_t hva;
1762 int srcu_idx, i, r = 0;
1763
1764 if (args->flags != 0)
1765 return -EINVAL;
1766
1767 /* Enforce sane limit on memory allocation */
1768 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1769 return -EINVAL;
1770
1771 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
1772 if (!keys)
1773 return -ENOMEM;
1774
1775 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
1776 sizeof(uint8_t) * args->count);
1777 if (r) {
1778 r = -EFAULT;
1779 goto out;
1780 }
1781
1782 /* Enable storage key handling for the guest */
1783 r = s390_enable_skey();
1784 if (r)
1785 goto out;
1786
1787 down_read(&current->mm->mmap_sem);
1788 srcu_idx = srcu_read_lock(&kvm->srcu);
1789 for (i = 0; i < args->count; i++) {
1790 hva = gfn_to_hva(kvm, args->start_gfn + i);
1791 if (kvm_is_error_hva(hva)) {
1792 r = -EFAULT;
1793 break;
1794 }
1795
1796 /* Lowest order bit is reserved */
1797 if (keys[i] & 0x01) {
1798 r = -EINVAL;
1799 break;
1800 }
1801
1802 r = set_guest_storage_key(current->mm, hva, keys[i], 0);
1803 if (r)
1804 break;
1805 }
1806 srcu_read_unlock(&kvm->srcu, srcu_idx);
1807 up_read(&current->mm->mmap_sem);
1808 out:
1809 kvfree(keys);
1810 return r;
1811 }
1812
1813 /*
1814 * Base address and length must be sent at the start of each block, therefore
1815 * it's cheaper to send some clean data, as long as it's less than the size of
1816 * two longs.
1817 */
1818 #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
1819 /* for consistency */
1820 #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
1821
1822 /*
1823 * This function searches for the next page with dirty CMMA attributes, and
1824 * saves the attributes in the buffer up to either the end of the buffer or
1825 * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
1826 * no trailing clean bytes are saved.
1827 * In case no dirty bits were found, or if CMMA was not enabled or used, the
1828 * output buffer will indicate 0 as length.
1829 */
1830 static int kvm_s390_get_cmma_bits(struct kvm *kvm,
1831 struct kvm_s390_cmma_log *args)
1832 {
1833 struct kvm_s390_migration_state *s = kvm->arch.migration_state;
1834 unsigned long bufsize, hva, pgstev, i, next, cur;
1835 int srcu_idx, peek, r = 0, rr;
1836 u8 *res;
1837
1838 cur = args->start_gfn;
1839 i = next = pgstev = 0;
1840
1841 if (unlikely(!kvm->arch.use_cmma))
1842 return -ENXIO;
1843 /* Invalid/unsupported flags were specified */
1844 if (args->flags & ~KVM_S390_CMMA_PEEK)
1845 return -EINVAL;
1846 /* Migration mode query, and we are not doing a migration */
1847 peek = !!(args->flags & KVM_S390_CMMA_PEEK);
1848 if (!peek && !s)
1849 return -EINVAL;
1850 /* CMMA is disabled or was not used, or the buffer has length zero */
1851 bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
1852 if (!bufsize || !kvm->mm->context.use_cmma) {
1853 memset(args, 0, sizeof(*args));
1854 return 0;
1855 }
1856
1857 if (!peek) {
1858 /* We are not peeking, and there are no dirty pages */
1859 if (!atomic64_read(&s->dirty_pages)) {
1860 memset(args, 0, sizeof(*args));
1861 return 0;
1862 }
1863 cur = find_next_bit(s->pgste_bitmap, s->bitmap_size,
1864 args->start_gfn);
1865 if (cur >= s->bitmap_size) /* nothing found, loop back */
1866 cur = find_next_bit(s->pgste_bitmap, s->bitmap_size, 0);
1867 if (cur >= s->bitmap_size) { /* again! (very unlikely) */
1868 memset(args, 0, sizeof(*args));
1869 return 0;
1870 }
1871 next = find_next_bit(s->pgste_bitmap, s->bitmap_size, cur + 1);
1872 }
1873
1874 res = vmalloc(bufsize);
1875 if (!res)
1876 return -ENOMEM;
1877
1878 args->start_gfn = cur;
1879
1880 down_read(&kvm->mm->mmap_sem);
1881 srcu_idx = srcu_read_lock(&kvm->srcu);
1882 while (i < bufsize) {
1883 hva = gfn_to_hva(kvm, cur);
1884 if (kvm_is_error_hva(hva)) {
1885 r = -EFAULT;
1886 break;
1887 }
1888 /* decrement only if we actually flipped the bit to 0 */
1889 if (!peek && test_and_clear_bit(cur, s->pgste_bitmap))
1890 atomic64_dec(&s->dirty_pages);
1891 r = get_pgste(kvm->mm, hva, &pgstev);
1892 if (r < 0)
1893 pgstev = 0;
1894 /* save the value */
1895 res[i++] = (pgstev >> 24) & 0x43;
1896 /*
1897 * if the next bit is too far away, stop.
1898 * if we reached the previous "next", find the next one
1899 */
1900 if (!peek) {
1901 if (next > cur + KVM_S390_MAX_BIT_DISTANCE)
1902 break;
1903 if (cur == next)
1904 next = find_next_bit(s->pgste_bitmap,
1905 s->bitmap_size, cur + 1);
1906 /* reached the end of the bitmap or of the buffer, stop */
1907 if ((next >= s->bitmap_size) ||
1908 (next >= args->start_gfn + bufsize))
1909 break;
1910 }
1911 cur++;
1912 }
1913 srcu_read_unlock(&kvm->srcu, srcu_idx);
1914 up_read(&kvm->mm->mmap_sem);
1915 args->count = i;
1916 args->remaining = s ? atomic64_read(&s->dirty_pages) : 0;
1917
1918 rr = copy_to_user((void __user *)args->values, res, args->count);
1919 if (rr)
1920 r = -EFAULT;
1921
1922 vfree(res);
1923 return r;
1924 }
1925
1926 /*
1927 * This function sets the CMMA attributes for the given pages. If the input
1928 * buffer has zero length, no action is taken, otherwise the attributes are
1929 * set and the mm->context.use_cmma flag is set.
1930 */
1931 static int kvm_s390_set_cmma_bits(struct kvm *kvm,
1932 const struct kvm_s390_cmma_log *args)
1933 {
1934 unsigned long hva, mask, pgstev, i;
1935 uint8_t *bits;
1936 int srcu_idx, r = 0;
1937
1938 mask = args->mask;
1939
1940 if (!kvm->arch.use_cmma)
1941 return -ENXIO;
1942 /* invalid/unsupported flags */
1943 if (args->flags != 0)
1944 return -EINVAL;
1945 /* Enforce sane limit on memory allocation */
1946 if (args->count > KVM_S390_CMMA_SIZE_MAX)
1947 return -EINVAL;
1948 /* Nothing to do */
1949 if (args->count == 0)
1950 return 0;
1951
1952 bits = vmalloc(sizeof(*bits) * args->count);
1953 if (!bits)
1954 return -ENOMEM;
1955
1956 r = copy_from_user(bits, (void __user *)args->values, args->count);
1957 if (r) {
1958 r = -EFAULT;
1959 goto out;
1960 }
1961
1962 down_read(&kvm->mm->mmap_sem);
1963 srcu_idx = srcu_read_lock(&kvm->srcu);
1964 for (i = 0; i < args->count; i++) {
1965 hva = gfn_to_hva(kvm, args->start_gfn + i);
1966 if (kvm_is_error_hva(hva)) {
1967 r = -EFAULT;
1968 break;
1969 }
1970
1971 pgstev = bits[i];
1972 pgstev = pgstev << 24;
1973 mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
1974 set_pgste_bits(kvm->mm, hva, mask, pgstev);
1975 }
1976 srcu_read_unlock(&kvm->srcu, srcu_idx);
1977 up_read(&kvm->mm->mmap_sem);
1978
1979 if (!kvm->mm->context.use_cmma) {
1980 down_write(&kvm->mm->mmap_sem);
1981 kvm->mm->context.use_cmma = 1;
1982 up_write(&kvm->mm->mmap_sem);
1983 }
1984 out:
1985 vfree(bits);
1986 return r;
1987 }
1988
1989 long kvm_arch_vm_ioctl(struct file *filp,
1990 unsigned int ioctl, unsigned long arg)
1991 {
1992 struct kvm *kvm = filp->private_data;
1993 void __user *argp = (void __user *)arg;
1994 struct kvm_device_attr attr;
1995 int r;
1996
1997 switch (ioctl) {
1998 case KVM_S390_INTERRUPT: {
1999 struct kvm_s390_interrupt s390int;
2000
2001 r = -EFAULT;
2002 if (copy_from_user(&s390int, argp, sizeof(s390int)))
2003 break;
2004 r = kvm_s390_inject_vm(kvm, &s390int);
2005 break;
2006 }
2007 case KVM_ENABLE_CAP: {
2008 struct kvm_enable_cap cap;
2009 r = -EFAULT;
2010 if (copy_from_user(&cap, argp, sizeof(cap)))
2011 break;
2012 r = kvm_vm_ioctl_enable_cap(kvm, &cap);
2013 break;
2014 }
2015 case KVM_CREATE_IRQCHIP: {
2016 struct kvm_irq_routing_entry routing;
2017
2018 r = -EINVAL;
2019 if (kvm->arch.use_irqchip) {
2020 /* Set up dummy routing. */
2021 memset(&routing, 0, sizeof(routing));
2022 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
2023 }
2024 break;
2025 }
2026 case KVM_SET_DEVICE_ATTR: {
2027 r = -EFAULT;
2028 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2029 break;
2030 r = kvm_s390_vm_set_attr(kvm, &attr);
2031 break;
2032 }
2033 case KVM_GET_DEVICE_ATTR: {
2034 r = -EFAULT;
2035 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2036 break;
2037 r = kvm_s390_vm_get_attr(kvm, &attr);
2038 break;
2039 }
2040 case KVM_HAS_DEVICE_ATTR: {
2041 r = -EFAULT;
2042 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2043 break;
2044 r = kvm_s390_vm_has_attr(kvm, &attr);
2045 break;
2046 }
2047 case KVM_S390_GET_SKEYS: {
2048 struct kvm_s390_skeys args;
2049
2050 r = -EFAULT;
2051 if (copy_from_user(&args, argp,
2052 sizeof(struct kvm_s390_skeys)))
2053 break;
2054 r = kvm_s390_get_skeys(kvm, &args);
2055 break;
2056 }
2057 case KVM_S390_SET_SKEYS: {
2058 struct kvm_s390_skeys args;
2059
2060 r = -EFAULT;
2061 if (copy_from_user(&args, argp,
2062 sizeof(struct kvm_s390_skeys)))
2063 break;
2064 r = kvm_s390_set_skeys(kvm, &args);
2065 break;
2066 }
2067 case KVM_S390_GET_CMMA_BITS: {
2068 struct kvm_s390_cmma_log args;
2069
2070 r = -EFAULT;
2071 if (copy_from_user(&args, argp, sizeof(args)))
2072 break;
2073 mutex_lock(&kvm->slots_lock);
2074 r = kvm_s390_get_cmma_bits(kvm, &args);
2075 mutex_unlock(&kvm->slots_lock);
2076 if (!r) {
2077 r = copy_to_user(argp, &args, sizeof(args));
2078 if (r)
2079 r = -EFAULT;
2080 }
2081 break;
2082 }
2083 case KVM_S390_SET_CMMA_BITS: {
2084 struct kvm_s390_cmma_log args;
2085
2086 r = -EFAULT;
2087 if (copy_from_user(&args, argp, sizeof(args)))
2088 break;
2089 mutex_lock(&kvm->slots_lock);
2090 r = kvm_s390_set_cmma_bits(kvm, &args);
2091 mutex_unlock(&kvm->slots_lock);
2092 break;
2093 }
2094 default:
2095 r = -ENOTTY;
2096 }
2097
2098 return r;
2099 }
2100
2101 static int kvm_s390_apxa_installed(void)
2102 {
2103 struct ap_config_info info;
2104
2105 if (ap_instructions_available()) {
2106 if (ap_qci(&info) == 0)
2107 return info.apxa;
2108 }
2109
2110 return 0;
2111 }
2112
2113 /*
2114 * The format of the crypto control block (CRYCB) is specified in the 3 low
2115 * order bits of the CRYCB designation (CRYCBD) field as follows:
2116 * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2117 * AP extended addressing (APXA) facility are installed.
2118 * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2119 * Format 2: Both the APXA and MSAX3 facilities are installed
2120 */
2121 static void kvm_s390_set_crycb_format(struct kvm *kvm)
2122 {
2123 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
2124
2125 /* Clear the CRYCB format bits - i.e., set format 0 by default */
2126 kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2127
2128 /* Check whether MSAX3 is installed */
2129 if (!test_kvm_facility(kvm, 76))
2130 return;
2131
2132 if (kvm_s390_apxa_installed())
2133 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
2134 else
2135 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
2136 }
2137
2138 void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
2139 unsigned long *aqm, unsigned long *adm)
2140 {
2141 struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
2142
2143 mutex_lock(&kvm->lock);
2144 kvm_s390_vcpu_block_all(kvm);
2145
2146 switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
2147 case CRYCB_FORMAT2: /* APCB1 use 256 bits */
2148 memcpy(crycb->apcb1.apm, apm, 32);
2149 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
2150 apm[0], apm[1], apm[2], apm[3]);
2151 memcpy(crycb->apcb1.aqm, aqm, 32);
2152 VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
2153 aqm[0], aqm[1], aqm[2], aqm[3]);
2154 memcpy(crycb->apcb1.adm, adm, 32);
2155 VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
2156 adm[0], adm[1], adm[2], adm[3]);
2157 break;
2158 case CRYCB_FORMAT1:
2159 case CRYCB_FORMAT0: /* Fall through both use APCB0 */
2160 memcpy(crycb->apcb0.apm, apm, 8);
2161 memcpy(crycb->apcb0.aqm, aqm, 2);
2162 memcpy(crycb->apcb0.adm, adm, 2);
2163 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
2164 apm[0], *((unsigned short *)aqm),
2165 *((unsigned short *)adm));
2166 break;
2167 default: /* Can not happen */
2168 break;
2169 }
2170
2171 /* recreate the shadow crycb for each vcpu */
2172 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2173 kvm_s390_vcpu_unblock_all(kvm);
2174 mutex_unlock(&kvm->lock);
2175 }
2176 EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
2177
2178 void kvm_arch_crypto_clear_masks(struct kvm *kvm)
2179 {
2180 mutex_lock(&kvm->lock);
2181 kvm_s390_vcpu_block_all(kvm);
2182
2183 memset(&kvm->arch.crypto.crycb->apcb0, 0,
2184 sizeof(kvm->arch.crypto.crycb->apcb0));
2185 memset(&kvm->arch.crypto.crycb->apcb1, 0,
2186 sizeof(kvm->arch.crypto.crycb->apcb1));
2187
2188 VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
2189 /* recreate the shadow crycb for each vcpu */
2190 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2191 kvm_s390_vcpu_unblock_all(kvm);
2192 mutex_unlock(&kvm->lock);
2193 }
2194 EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
2195
2196 static u64 kvm_s390_get_initial_cpuid(void)
2197 {
2198 struct cpuid cpuid;
2199
2200 get_cpu_id(&cpuid);
2201 cpuid.version = 0xff;
2202 return *((u64 *) &cpuid);
2203 }
2204
2205 static void kvm_s390_crypto_init(struct kvm *kvm)
2206 {
2207 kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
2208 kvm_s390_set_crycb_format(kvm);
2209
2210 if (!test_kvm_facility(kvm, 76))
2211 return;
2212
2213 /* Enable AES/DEA protected key functions by default */
2214 kvm->arch.crypto.aes_kw = 1;
2215 kvm->arch.crypto.dea_kw = 1;
2216 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
2217 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
2218 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
2219 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
2220 }
2221
2222 static void sca_dispose(struct kvm *kvm)
2223 {
2224 if (kvm->arch.use_esca)
2225 free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
2226 else
2227 free_page((unsigned long)(kvm->arch.sca));
2228 kvm->arch.sca = NULL;
2229 }
2230
2231 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
2232 {
2233 gfp_t alloc_flags = GFP_KERNEL;
2234 int i, rc;
2235 char debug_name[16];
2236 static unsigned long sca_offset;
2237
2238 rc = -EINVAL;
2239 #ifdef CONFIG_KVM_S390_UCONTROL
2240 if (type & ~KVM_VM_S390_UCONTROL)
2241 goto out_err;
2242 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
2243 goto out_err;
2244 #else
2245 if (type)
2246 goto out_err;
2247 #endif
2248
2249 rc = s390_enable_sie();
2250 if (rc)
2251 goto out_err;
2252
2253 rc = -ENOMEM;
2254
2255 kvm->arch.use_esca = 0; /* start with basic SCA */
2256 if (!sclp.has_64bscao)
2257 alloc_flags |= GFP_DMA;
2258 rwlock_init(&kvm->arch.sca_lock);
2259 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
2260 if (!kvm->arch.sca)
2261 goto out_err;
2262 spin_lock(&kvm_lock);
2263 sca_offset += 16;
2264 if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2265 sca_offset = 0;
2266 kvm->arch.sca = (struct bsca_block *)
2267 ((char *) kvm->arch.sca + sca_offset);
2268 spin_unlock(&kvm_lock);
2269
2270 sprintf(debug_name, "kvm-%u", current->pid);
2271
2272 kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2273 if (!kvm->arch.dbf)
2274 goto out_err;
2275
2276 kvm->arch.sie_page2 =
2277 (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
2278 if (!kvm->arch.sie_page2)
2279 goto out_err;
2280
2281 kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2282
2283 for (i = 0; i < kvm_s390_fac_size(); i++) {
2284 kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] &
2285 (kvm_s390_fac_base[i] |
2286 kvm_s390_fac_ext[i]);
2287 kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] &
2288 kvm_s390_fac_base[i];
2289 }
2290 kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
2291
2292 /* we are always in czam mode - even on pre z14 machines */
2293 set_kvm_facility(kvm->arch.model.fac_mask, 138);
2294 set_kvm_facility(kvm->arch.model.fac_list, 138);
2295 /* we emulate STHYI in kvm */
2296 set_kvm_facility(kvm->arch.model.fac_mask, 74);
2297 set_kvm_facility(kvm->arch.model.fac_list, 74);
2298 if (MACHINE_HAS_TLB_GUEST) {
2299 set_kvm_facility(kvm->arch.model.fac_mask, 147);
2300 set_kvm_facility(kvm->arch.model.fac_list, 147);
2301 }
2302
2303 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
2304 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
2305
2306 kvm_s390_crypto_init(kvm);
2307
2308 mutex_init(&kvm->arch.float_int.ais_lock);
2309 kvm->arch.float_int.simm = 0;
2310 kvm->arch.float_int.nimm = 0;
2311 spin_lock_init(&kvm->arch.float_int.lock);
2312 for (i = 0; i < FIRQ_LIST_COUNT; i++)
2313 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
2314 init_waitqueue_head(&kvm->arch.ipte_wq);
2315 mutex_init(&kvm->arch.ipte_mutex);
2316
2317 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
2318 VM_EVENT(kvm, 3, "vm created with type %lu", type);
2319
2320 if (type & KVM_VM_S390_UCONTROL) {
2321 kvm->arch.gmap = NULL;
2322 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2323 } else {
2324 if (sclp.hamax == U64_MAX)
2325 kvm->arch.mem_limit = TASK_SIZE_MAX;
2326 else
2327 kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
2328 sclp.hamax + 1);
2329 kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2330 if (!kvm->arch.gmap)
2331 goto out_err;
2332 kvm->arch.gmap->private = kvm;
2333 kvm->arch.gmap->pfault_enabled = 0;
2334 }
2335
2336 kvm->arch.css_support = 0;
2337 kvm->arch.use_irqchip = 0;
2338 kvm->arch.epoch = 0;
2339
2340 spin_lock_init(&kvm->arch.start_stop_lock);
2341 kvm_s390_vsie_init(kvm);
2342 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
2343
2344 return 0;
2345 out_err:
2346 free_page((unsigned long)kvm->arch.sie_page2);
2347 debug_unregister(kvm->arch.dbf);
2348 sca_dispose(kvm);
2349 KVM_EVENT(3, "creation of vm failed: %d", rc);
2350 return rc;
2351 }
2352
2353 bool kvm_arch_has_vcpu_debugfs(void)
2354 {
2355 return false;
2356 }
2357
2358 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
2359 {
2360 return 0;
2361 }
2362
2363 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2364 {
2365 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2366 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
2367 kvm_s390_clear_local_irqs(vcpu);
2368 kvm_clear_async_pf_completion_queue(vcpu);
2369 if (!kvm_is_ucontrol(vcpu->kvm))
2370 sca_del_vcpu(vcpu);
2371
2372 if (kvm_is_ucontrol(vcpu->kvm))
2373 gmap_remove(vcpu->arch.gmap);
2374
2375 if (vcpu->kvm->arch.use_cmma)
2376 kvm_s390_vcpu_unsetup_cmma(vcpu);
2377 free_page((unsigned long)(vcpu->arch.sie_block));
2378
2379 kvm_vcpu_uninit(vcpu);
2380 kmem_cache_free(kvm_vcpu_cache, vcpu);
2381 }
2382
2383 static void kvm_free_vcpus(struct kvm *kvm)
2384 {
2385 unsigned int i;
2386 struct kvm_vcpu *vcpu;
2387
2388 kvm_for_each_vcpu(i, vcpu, kvm)
2389 kvm_arch_vcpu_destroy(vcpu);
2390
2391 mutex_lock(&kvm->lock);
2392 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2393 kvm->vcpus[i] = NULL;
2394
2395 atomic_set(&kvm->online_vcpus, 0);
2396 mutex_unlock(&kvm->lock);
2397 }
2398
2399 void kvm_arch_destroy_vm(struct kvm *kvm)
2400 {
2401 kvm_free_vcpus(kvm);
2402 sca_dispose(kvm);
2403 debug_unregister(kvm->arch.dbf);
2404 free_page((unsigned long)kvm->arch.sie_page2);
2405 if (!kvm_is_ucontrol(kvm))
2406 gmap_remove(kvm->arch.gmap);
2407 kvm_s390_destroy_adapters(kvm);
2408 kvm_s390_clear_float_irqs(kvm);
2409 kvm_s390_vsie_destroy(kvm);
2410 if (kvm->arch.migration_state) {
2411 vfree(kvm->arch.migration_state->pgste_bitmap);
2412 kfree(kvm->arch.migration_state);
2413 }
2414 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2415 }
2416
2417 /* Section: vcpu related */
2418 static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2419 {
2420 vcpu->arch.gmap = gmap_create(current->mm, -1UL);
2421 if (!vcpu->arch.gmap)
2422 return -ENOMEM;
2423 vcpu->arch.gmap->private = vcpu->kvm;
2424
2425 return 0;
2426 }
2427
2428 static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2429 {
2430 if (!kvm_s390_use_sca_entries())
2431 return;
2432 read_lock(&vcpu->kvm->arch.sca_lock);
2433 if (vcpu->kvm->arch.use_esca) {
2434 struct esca_block *sca = vcpu->kvm->arch.sca;
2435
2436 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2437 sca->cpu[vcpu->vcpu_id].sda = 0;
2438 } else {
2439 struct bsca_block *sca = vcpu->kvm->arch.sca;
2440
2441 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2442 sca->cpu[vcpu->vcpu_id].sda = 0;
2443 }
2444 read_unlock(&vcpu->kvm->arch.sca_lock);
2445 }
2446
2447 static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2448 {
2449 if (!kvm_s390_use_sca_entries()) {
2450 struct bsca_block *sca = vcpu->kvm->arch.sca;
2451
2452 /* we still need the basic sca for the ipte control */
2453 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2454 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2455 return;
2456 }
2457 read_lock(&vcpu->kvm->arch.sca_lock);
2458 if (vcpu->kvm->arch.use_esca) {
2459 struct esca_block *sca = vcpu->kvm->arch.sca;
2460
2461 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2462 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2463 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
2464 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2465 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2466 } else {
2467 struct bsca_block *sca = vcpu->kvm->arch.sca;
2468
2469 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2470 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2471 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2472 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2473 }
2474 read_unlock(&vcpu->kvm->arch.sca_lock);
2475 }
2476
2477 /* Basic SCA to Extended SCA data copy routines */
2478 static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
2479 {
2480 d->sda = s->sda;
2481 d->sigp_ctrl.c = s->sigp_ctrl.c;
2482 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
2483 }
2484
2485 static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
2486 {
2487 int i;
2488
2489 d->ipte_control = s->ipte_control;
2490 d->mcn[0] = s->mcn;
2491 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
2492 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
2493 }
2494
2495 static int sca_switch_to_extended(struct kvm *kvm)
2496 {
2497 struct bsca_block *old_sca = kvm->arch.sca;
2498 struct esca_block *new_sca;
2499 struct kvm_vcpu *vcpu;
2500 unsigned int vcpu_idx;
2501 u32 scaol, scaoh;
2502
2503 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
2504 if (!new_sca)
2505 return -ENOMEM;
2506
2507 scaoh = (u32)((u64)(new_sca) >> 32);
2508 scaol = (u32)(u64)(new_sca) & ~0x3fU;
2509
2510 kvm_s390_vcpu_block_all(kvm);
2511 write_lock(&kvm->arch.sca_lock);
2512
2513 sca_copy_b_to_e(new_sca, old_sca);
2514
2515 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
2516 vcpu->arch.sie_block->scaoh = scaoh;
2517 vcpu->arch.sie_block->scaol = scaol;
2518 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2519 }
2520 kvm->arch.sca = new_sca;
2521 kvm->arch.use_esca = 1;
2522
2523 write_unlock(&kvm->arch.sca_lock);
2524 kvm_s390_vcpu_unblock_all(kvm);
2525
2526 free_page((unsigned long)old_sca);
2527
2528 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
2529 old_sca, kvm->arch.sca);
2530 return 0;
2531 }
2532
2533 static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2534 {
2535 int rc;
2536
2537 if (!kvm_s390_use_sca_entries()) {
2538 if (id < KVM_MAX_VCPUS)
2539 return true;
2540 return false;
2541 }
2542 if (id < KVM_S390_BSCA_CPU_SLOTS)
2543 return true;
2544 if (!sclp.has_esca || !sclp.has_64bscao)
2545 return false;
2546
2547 mutex_lock(&kvm->lock);
2548 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
2549 mutex_unlock(&kvm->lock);
2550
2551 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2552 }
2553
2554 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
2555 {
2556 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
2557 kvm_clear_async_pf_completion_queue(vcpu);
2558 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
2559 KVM_SYNC_GPRS |
2560 KVM_SYNC_ACRS |
2561 KVM_SYNC_CRS |
2562 KVM_SYNC_ARCH0 |
2563 KVM_SYNC_PFAULT;
2564 kvm_s390_set_prefix(vcpu, 0);
2565 if (test_kvm_facility(vcpu->kvm, 64))
2566 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
2567 if (test_kvm_facility(vcpu->kvm, 82))
2568 vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
2569 if (test_kvm_facility(vcpu->kvm, 133))
2570 vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
2571 if (test_kvm_facility(vcpu->kvm, 156))
2572 vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
2573 /* fprs can be synchronized via vrs, even if the guest has no vx. With
2574 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
2575 */
2576 if (MACHINE_HAS_VX)
2577 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
2578 else
2579 vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
2580
2581 if (kvm_is_ucontrol(vcpu->kvm))
2582 return __kvm_ucontrol_vcpu_init(vcpu);
2583
2584 return 0;
2585 }
2586
2587 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2588 static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2589 {
2590 WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
2591 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2592 vcpu->arch.cputm_start = get_tod_clock_fast();
2593 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2594 }
2595
2596 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2597 static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2598 {
2599 WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
2600 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2601 vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2602 vcpu->arch.cputm_start = 0;
2603 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2604 }
2605
2606 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2607 static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2608 {
2609 WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2610 vcpu->arch.cputm_enabled = true;
2611 __start_cpu_timer_accounting(vcpu);
2612 }
2613
2614 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2615 static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2616 {
2617 WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2618 __stop_cpu_timer_accounting(vcpu);
2619 vcpu->arch.cputm_enabled = false;
2620 }
2621
2622 static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2623 {
2624 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2625 __enable_cpu_timer_accounting(vcpu);
2626 preempt_enable();
2627 }
2628
2629 static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2630 {
2631 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2632 __disable_cpu_timer_accounting(vcpu);
2633 preempt_enable();
2634 }
2635
2636 /* set the cpu timer - may only be called from the VCPU thread itself */
2637 void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
2638 {
2639 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2640 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2641 if (vcpu->arch.cputm_enabled)
2642 vcpu->arch.cputm_start = get_tod_clock_fast();
2643 vcpu->arch.sie_block->cputm = cputm;
2644 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2645 preempt_enable();
2646 }
2647
2648 /* update and get the cpu timer - can also be called from other VCPU threads */
2649 __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
2650 {
2651 unsigned int seq;
2652 __u64 value;
2653
2654 if (unlikely(!vcpu->arch.cputm_enabled))
2655 return vcpu->arch.sie_block->cputm;
2656
2657 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2658 do {
2659 seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
2660 /*
2661 * If the writer would ever execute a read in the critical
2662 * section, e.g. in irq context, we have a deadlock.
2663 */
2664 WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
2665 value = vcpu->arch.sie_block->cputm;
2666 /* if cputm_start is 0, accounting is being started/stopped */
2667 if (likely(vcpu->arch.cputm_start))
2668 value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2669 } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
2670 preempt_enable();
2671 return value;
2672 }
2673
2674 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2675 {
2676
2677 gmap_enable(vcpu->arch.enabled_gmap);
2678 atomic_or(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
2679 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2680 __start_cpu_timer_accounting(vcpu);
2681 vcpu->cpu = cpu;
2682 }
2683
2684 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2685 {
2686 vcpu->cpu = -1;
2687 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2688 __stop_cpu_timer_accounting(vcpu);
2689 atomic_andnot(CPUSTAT_RUNNING, &vcpu->arch.sie_block->cpuflags);
2690 vcpu->arch.enabled_gmap = gmap_get_enabled();
2691 gmap_disable(vcpu->arch.enabled_gmap);
2692
2693 }
2694
2695 static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
2696 {
2697 /* this equals initial cpu reset in pop, but we don't switch to ESA */
2698 vcpu->arch.sie_block->gpsw.mask = 0UL;
2699 vcpu->arch.sie_block->gpsw.addr = 0UL;
2700 kvm_s390_set_prefix(vcpu, 0);
2701 kvm_s390_set_cpu_timer(vcpu, 0);
2702 vcpu->arch.sie_block->ckc = 0UL;
2703 vcpu->arch.sie_block->todpr = 0;
2704 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
2705 vcpu->arch.sie_block->gcr[0] = 0xE0UL;
2706 vcpu->arch.sie_block->gcr[14] = 0xC2000000UL;
2707 /* make sure the new fpc will be lazily loaded */
2708 save_fpu_regs();
2709 current->thread.fpu.fpc = 0;
2710 vcpu->arch.sie_block->gbea = 1;
2711 vcpu->arch.sie_block->pp = 0;
2712 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
2713 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
2714 kvm_clear_async_pf_completion_queue(vcpu);
2715 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
2716 kvm_s390_vcpu_stop(vcpu);
2717 kvm_s390_clear_local_irqs(vcpu);
2718 }
2719
2720 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
2721 {
2722 mutex_lock(&vcpu->kvm->lock);
2723 preempt_disable();
2724 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
2725 vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
2726 preempt_enable();
2727 mutex_unlock(&vcpu->kvm->lock);
2728 if (!kvm_is_ucontrol(vcpu->kvm)) {
2729 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
2730 sca_add_vcpu(vcpu);
2731 }
2732 if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
2733 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
2734 /* make vcpu_load load the right gmap on the first trigger */
2735 vcpu->arch.enabled_gmap = vcpu->arch.gmap;
2736 }
2737
2738 static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
2739 {
2740 if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
2741 test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
2742 return true;
2743 return false;
2744 }
2745
2746 static bool kvm_has_pckmo_ecc(struct kvm *kvm)
2747 {
2748 /* At least one ECC subfunction must be present */
2749 return kvm_has_pckmo_subfunc(kvm, 32) ||
2750 kvm_has_pckmo_subfunc(kvm, 33) ||
2751 kvm_has_pckmo_subfunc(kvm, 34) ||
2752 kvm_has_pckmo_subfunc(kvm, 40) ||
2753 kvm_has_pckmo_subfunc(kvm, 41);
2754
2755 }
2756
2757 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
2758 {
2759 /*
2760 * If the AP instructions are not being interpreted and the MSAX3
2761 * facility is not configured for the guest, there is nothing to set up.
2762 */
2763 if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
2764 return;
2765
2766 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
2767 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
2768 vcpu->arch.sie_block->eca &= ~ECA_APIE;
2769 vcpu->arch.sie_block->ecd &= ~ECD_ECC;
2770
2771 if (vcpu->kvm->arch.crypto.apie)
2772 vcpu->arch.sie_block->eca |= ECA_APIE;
2773
2774 /* Set up protected key support */
2775 if (vcpu->kvm->arch.crypto.aes_kw) {
2776 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
2777 /* ecc is also wrapped with AES key */
2778 if (kvm_has_pckmo_ecc(vcpu->kvm))
2779 vcpu->arch.sie_block->ecd |= ECD_ECC;
2780 }
2781
2782 if (vcpu->kvm->arch.crypto.dea_kw)
2783 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
2784 }
2785
2786 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
2787 {
2788 free_page(vcpu->arch.sie_block->cbrlo);
2789 vcpu->arch.sie_block->cbrlo = 0;
2790 }
2791
2792 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
2793 {
2794 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
2795 if (!vcpu->arch.sie_block->cbrlo)
2796 return -ENOMEM;
2797
2798 vcpu->arch.sie_block->ecb2 &= ~ECB2_PFMFI;
2799 return 0;
2800 }
2801
2802 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
2803 {
2804 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
2805
2806 vcpu->arch.sie_block->ibc = model->ibc;
2807 if (test_kvm_facility(vcpu->kvm, 7))
2808 vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
2809 }
2810
2811 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
2812 {
2813 int rc = 0;
2814
2815 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
2816 CPUSTAT_SM |
2817 CPUSTAT_STOPPED);
2818
2819 if (test_kvm_facility(vcpu->kvm, 78))
2820 atomic_or(CPUSTAT_GED2, &vcpu->arch.sie_block->cpuflags);
2821 else if (test_kvm_facility(vcpu->kvm, 8))
2822 atomic_or(CPUSTAT_GED, &vcpu->arch.sie_block->cpuflags);
2823
2824 kvm_s390_vcpu_setup_model(vcpu);
2825
2826 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2827 if (MACHINE_HAS_ESOP)
2828 vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
2829 if (test_kvm_facility(vcpu->kvm, 9))
2830 vcpu->arch.sie_block->ecb |= ECB_SRSI;
2831 if (test_kvm_facility(vcpu->kvm, 73))
2832 vcpu->arch.sie_block->ecb |= ECB_TE;
2833
2834 if (test_kvm_facility(vcpu->kvm, 8) && sclp.has_pfmfi)
2835 vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
2836 if (test_kvm_facility(vcpu->kvm, 130))
2837 vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
2838 vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
2839 if (sclp.has_cei)
2840 vcpu->arch.sie_block->eca |= ECA_CEI;
2841 if (sclp.has_ib)
2842 vcpu->arch.sie_block->eca |= ECA_IB;
2843 if (sclp.has_siif)
2844 vcpu->arch.sie_block->eca |= ECA_SII;
2845 if (sclp.has_sigpif)
2846 vcpu->arch.sie_block->eca |= ECA_SIGPI;
2847 if (test_kvm_facility(vcpu->kvm, 129)) {
2848 vcpu->arch.sie_block->eca |= ECA_VX;
2849 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
2850 }
2851 if (test_kvm_facility(vcpu->kvm, 139))
2852 vcpu->arch.sie_block->ecd |= ECD_MEF;
2853 if (test_kvm_facility(vcpu->kvm, 156))
2854 vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
2855 vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
2856 | SDNXC;
2857 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
2858
2859 if (sclp.has_kss)
2860 atomic_or(CPUSTAT_KSS, &vcpu->arch.sie_block->cpuflags);
2861 else
2862 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
2863
2864 if (vcpu->kvm->arch.use_cmma) {
2865 rc = kvm_s390_vcpu_setup_cmma(vcpu);
2866 if (rc)
2867 return rc;
2868 }
2869 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2870 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
2871
2872 kvm_s390_vcpu_crypto_setup(vcpu);
2873
2874 return rc;
2875 }
2876
2877 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2878 unsigned int id)
2879 {
2880 struct kvm_vcpu *vcpu;
2881 struct sie_page *sie_page;
2882 int rc = -EINVAL;
2883
2884 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
2885 goto out;
2886
2887 rc = -ENOMEM;
2888
2889 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2890 if (!vcpu)
2891 goto out;
2892
2893 BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
2894 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
2895 if (!sie_page)
2896 goto out_free_cpu;
2897
2898 vcpu->arch.sie_block = &sie_page->sie_block;
2899 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
2900
2901 /* the real guest size will always be smaller than msl */
2902 vcpu->arch.sie_block->mso = 0;
2903 vcpu->arch.sie_block->msl = sclp.hamax;
2904
2905 vcpu->arch.sie_block->icpua = id;
2906 spin_lock_init(&vcpu->arch.local_int.lock);
2907 vcpu->arch.local_int.float_int = &kvm->arch.float_int;
2908 vcpu->arch.local_int.wq = &vcpu->wq;
2909 vcpu->arch.local_int.cpuflags = &vcpu->arch.sie_block->cpuflags;
2910 seqcount_init(&vcpu->arch.cputm_seqcount);
2911
2912 rc = kvm_vcpu_init(vcpu, kvm, id);
2913 if (rc)
2914 goto out_free_sie_block;
2915 VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2916 vcpu->arch.sie_block);
2917 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2918
2919 return vcpu;
2920 out_free_sie_block:
2921 free_page((unsigned long)(vcpu->arch.sie_block));
2922 out_free_cpu:
2923 kmem_cache_free(kvm_vcpu_cache, vcpu);
2924 out:
2925 return ERR_PTR(rc);
2926 }
2927
2928 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2929 {
2930 return kvm_s390_vcpu_has_irq(vcpu, 0);
2931 }
2932
2933 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
2934 {
2935 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
2936 }
2937
2938 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
2939 {
2940 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
2941 exit_sie(vcpu);
2942 }
2943
2944 void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
2945 {
2946 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
2947 }
2948
2949 static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
2950 {
2951 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
2952 exit_sie(vcpu);
2953 }
2954
2955 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
2956 {
2957 return atomic_read(&vcpu->arch.sie_block->prog20) &
2958 (PROG_BLOCK_SIE | PROG_REQUEST);
2959 }
2960
2961 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
2962 {
2963 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
2964 }
2965
2966 /*
2967 * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
2968 * If the CPU is not running (e.g. waiting as idle) the function will
2969 * return immediately. */
2970 void exit_sie(struct kvm_vcpu *vcpu)
2971 {
2972 atomic_or(CPUSTAT_STOP_INT, &vcpu->arch.sie_block->cpuflags);
2973 kvm_s390_vsie_kick(vcpu);
2974 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
2975 cpu_relax();
2976 }
2977
2978 /* Kick a guest cpu out of SIE to process a request synchronously */
2979 void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
2980 {
2981 kvm_make_request(req, vcpu);
2982 kvm_s390_vcpu_request(vcpu);
2983 }
2984
2985 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
2986 unsigned long end)
2987 {
2988 struct kvm *kvm = gmap->private;
2989 struct kvm_vcpu *vcpu;
2990 unsigned long prefix;
2991 int i;
2992
2993 if (gmap_is_shadow(gmap))
2994 return;
2995 if (start >= 1UL << 31)
2996 /* We are only interested in prefix pages */
2997 return;
2998 kvm_for_each_vcpu(i, vcpu, kvm) {
2999 /* match against both prefix pages */
3000 prefix = kvm_s390_get_prefix(vcpu);
3001 if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3002 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3003 start, end);
3004 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
3005 }
3006 }
3007 }
3008
3009 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3010 {
3011 /* kvm common code refers to this, but never calls it */
3012 BUG();
3013 return 0;
3014 }
3015
3016 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
3017 struct kvm_one_reg *reg)
3018 {
3019 int r = -EINVAL;
3020
3021 switch (reg->id) {
3022 case KVM_REG_S390_TODPR:
3023 r = put_user(vcpu->arch.sie_block->todpr,
3024 (u32 __user *)reg->addr);
3025 break;
3026 case KVM_REG_S390_EPOCHDIFF:
3027 r = put_user(vcpu->arch.sie_block->epoch,
3028 (u64 __user *)reg->addr);
3029 break;
3030 case KVM_REG_S390_CPU_TIMER:
3031 r = put_user(kvm_s390_get_cpu_timer(vcpu),
3032 (u64 __user *)reg->addr);
3033 break;
3034 case KVM_REG_S390_CLOCK_COMP:
3035 r = put_user(vcpu->arch.sie_block->ckc,
3036 (u64 __user *)reg->addr);
3037 break;
3038 case KVM_REG_S390_PFTOKEN:
3039 r = put_user(vcpu->arch.pfault_token,
3040 (u64 __user *)reg->addr);
3041 break;
3042 case KVM_REG_S390_PFCOMPARE:
3043 r = put_user(vcpu->arch.pfault_compare,
3044 (u64 __user *)reg->addr);
3045 break;
3046 case KVM_REG_S390_PFSELECT:
3047 r = put_user(vcpu->arch.pfault_select,
3048 (u64 __user *)reg->addr);
3049 break;
3050 case KVM_REG_S390_PP:
3051 r = put_user(vcpu->arch.sie_block->pp,
3052 (u64 __user *)reg->addr);
3053 break;
3054 case KVM_REG_S390_GBEA:
3055 r = put_user(vcpu->arch.sie_block->gbea,
3056 (u64 __user *)reg->addr);
3057 break;
3058 default:
3059 break;
3060 }
3061
3062 return r;
3063 }
3064
3065 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
3066 struct kvm_one_reg *reg)
3067 {
3068 int r = -EINVAL;
3069 __u64 val;
3070
3071 switch (reg->id) {
3072 case KVM_REG_S390_TODPR:
3073 r = get_user(vcpu->arch.sie_block->todpr,
3074 (u32 __user *)reg->addr);
3075 break;
3076 case KVM_REG_S390_EPOCHDIFF:
3077 r = get_user(vcpu->arch.sie_block->epoch,
3078 (u64 __user *)reg->addr);
3079 break;
3080 case KVM_REG_S390_CPU_TIMER:
3081 r = get_user(val, (u64 __user *)reg->addr);
3082 if (!r)
3083 kvm_s390_set_cpu_timer(vcpu, val);
3084 break;
3085 case KVM_REG_S390_CLOCK_COMP:
3086 r = get_user(vcpu->arch.sie_block->ckc,
3087 (u64 __user *)reg->addr);
3088 break;
3089 case KVM_REG_S390_PFTOKEN:
3090 r = get_user(vcpu->arch.pfault_token,
3091 (u64 __user *)reg->addr);
3092 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3093 kvm_clear_async_pf_completion_queue(vcpu);
3094 break;
3095 case KVM_REG_S390_PFCOMPARE:
3096 r = get_user(vcpu->arch.pfault_compare,
3097 (u64 __user *)reg->addr);
3098 break;
3099 case KVM_REG_S390_PFSELECT:
3100 r = get_user(vcpu->arch.pfault_select,
3101 (u64 __user *)reg->addr);
3102 break;
3103 case KVM_REG_S390_PP:
3104 r = get_user(vcpu->arch.sie_block->pp,
3105 (u64 __user *)reg->addr);
3106 break;
3107 case KVM_REG_S390_GBEA:
3108 r = get_user(vcpu->arch.sie_block->gbea,
3109 (u64 __user *)reg->addr);
3110 break;
3111 default:
3112 break;
3113 }
3114
3115 return r;
3116 }
3117
3118 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
3119 {
3120 kvm_s390_vcpu_initial_reset(vcpu);
3121 return 0;
3122 }
3123
3124 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3125 {
3126 memcpy(&vcpu->run->s.regs.gprs, &regs->gprs, sizeof(regs->gprs));
3127 return 0;
3128 }
3129
3130 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3131 {
3132 memcpy(&regs->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
3133 return 0;
3134 }
3135
3136 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3137 struct kvm_sregs *sregs)
3138 {
3139 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
3140 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
3141 return 0;
3142 }
3143
3144 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3145 struct kvm_sregs *sregs)
3146 {
3147 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
3148 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
3149 return 0;
3150 }
3151
3152 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3153 {
3154 if (test_fp_ctl(fpu->fpc))
3155 return -EINVAL;
3156 vcpu->run->s.regs.fpc = fpu->fpc;
3157 if (MACHINE_HAS_VX)
3158 convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
3159 (freg_t *) fpu->fprs);
3160 else
3161 memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
3162 return 0;
3163 }
3164
3165 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3166 {
3167 /* make sure we have the latest values */
3168 save_fpu_regs();
3169 if (MACHINE_HAS_VX)
3170 convert_vx_to_fp((freg_t *) fpu->fprs,
3171 (__vector128 *) vcpu->run->s.regs.vrs);
3172 else
3173 memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
3174 fpu->fpc = vcpu->run->s.regs.fpc;
3175 return 0;
3176 }
3177
3178 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
3179 {
3180 int rc = 0;
3181
3182 if (!is_vcpu_stopped(vcpu))
3183 rc = -EBUSY;
3184 else {
3185 vcpu->run->psw_mask = psw.mask;
3186 vcpu->run->psw_addr = psw.addr;
3187 }
3188 return rc;
3189 }
3190
3191 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3192 struct kvm_translation *tr)
3193 {
3194 return -EINVAL; /* not implemented yet */
3195 }
3196
3197 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
3198 KVM_GUESTDBG_USE_HW_BP | \
3199 KVM_GUESTDBG_ENABLE)
3200
3201 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
3202 struct kvm_guest_debug *dbg)
3203 {
3204 int rc = 0;
3205
3206 vcpu->guest_debug = 0;
3207 kvm_s390_clear_bp_data(vcpu);
3208
3209 if (dbg->control & ~VALID_GUESTDBG_FLAGS)
3210 return -EINVAL;
3211 if (!sclp.has_gpere)
3212 return -EINVAL;
3213
3214 if (dbg->control & KVM_GUESTDBG_ENABLE) {
3215 vcpu->guest_debug = dbg->control;
3216 /* enforce guest PER */
3217 atomic_or(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
3218
3219 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
3220 rc = kvm_s390_import_bp_data(vcpu, dbg);
3221 } else {
3222 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
3223 vcpu->arch.guestdbg.last_bp = 0;
3224 }
3225
3226 if (rc) {
3227 vcpu->guest_debug = 0;
3228 kvm_s390_clear_bp_data(vcpu);
3229 atomic_andnot(CPUSTAT_P, &vcpu->arch.sie_block->cpuflags);
3230 }
3231
3232 return rc;
3233 }
3234
3235 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3236 struct kvm_mp_state *mp_state)
3237 {
3238 /* CHECK_STOP and LOAD are not supported yet */
3239 return is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
3240 KVM_MP_STATE_OPERATING;
3241 }
3242
3243 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3244 struct kvm_mp_state *mp_state)
3245 {
3246 int rc = 0;
3247
3248 /* user space knows about this interface - let it control the state */
3249 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
3250
3251 switch (mp_state->mp_state) {
3252 case KVM_MP_STATE_STOPPED:
3253 kvm_s390_vcpu_stop(vcpu);
3254 break;
3255 case KVM_MP_STATE_OPERATING:
3256 kvm_s390_vcpu_start(vcpu);
3257 break;
3258 case KVM_MP_STATE_LOAD:
3259 case KVM_MP_STATE_CHECK_STOP:
3260 /* fall through - CHECK_STOP and LOAD are not supported yet */
3261 default:
3262 rc = -ENXIO;
3263 }
3264
3265 return rc;
3266 }
3267
3268 static bool ibs_enabled(struct kvm_vcpu *vcpu)
3269 {
3270 return atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_IBS;
3271 }
3272
3273 static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
3274 {
3275 retry:
3276 kvm_s390_vcpu_request_handled(vcpu);
3277 if (!kvm_request_pending(vcpu))
3278 return 0;
3279 /*
3280 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3281 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
3282 * This ensures that the ipte instruction for this request has
3283 * already finished. We might race against a second unmapper that
3284 * wants to set the blocking bit. Lets just retry the request loop.
3285 */
3286 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
3287 int rc;
3288 rc = gmap_mprotect_notify(vcpu->arch.gmap,
3289 kvm_s390_get_prefix(vcpu),
3290 PAGE_SIZE * 2, PROT_WRITE);
3291 if (rc) {
3292 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
3293 return rc;
3294 }
3295 goto retry;
3296 }
3297
3298 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3299 vcpu->arch.sie_block->ihcpu = 0xffff;
3300 goto retry;
3301 }
3302
3303 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
3304 if (!ibs_enabled(vcpu)) {
3305 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3306 atomic_or(CPUSTAT_IBS,
3307 &vcpu->arch.sie_block->cpuflags);
3308 }
3309 goto retry;
3310 }
3311
3312 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
3313 if (ibs_enabled(vcpu)) {
3314 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
3315 atomic_andnot(CPUSTAT_IBS,
3316 &vcpu->arch.sie_block->cpuflags);
3317 }
3318 goto retry;
3319 }
3320
3321 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
3322 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
3323 goto retry;
3324 }
3325
3326 if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3327 /*
3328 * Disable CMMA virtualization; we will emulate the ESSA
3329 * instruction manually, in order to provide additional
3330 * functionalities needed for live migration.
3331 */
3332 vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3333 goto retry;
3334 }
3335
3336 if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3337 /*
3338 * Re-enable CMMA virtualization if CMMA is available and
3339 * was used.
3340 */
3341 if ((vcpu->kvm->arch.use_cmma) &&
3342 (vcpu->kvm->mm->context.use_cmma))
3343 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3344 goto retry;
3345 }
3346
3347 /* nothing to do, just clear the request */
3348 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
3349 /* we left the vsie handler, nothing to do, just clear the request */
3350 kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
3351
3352 return 0;
3353 }
3354
3355 void kvm_s390_set_tod_clock(struct kvm *kvm,
3356 const struct kvm_s390_vm_tod_clock *gtod)
3357 {
3358 struct kvm_vcpu *vcpu;
3359 struct kvm_s390_tod_clock_ext htod;
3360 int i;
3361
3362 mutex_lock(&kvm->lock);
3363 preempt_disable();
3364
3365 get_tod_clock_ext((char *)&htod);
3366
3367 kvm->arch.epoch = gtod->tod - htod.tod;
3368 kvm->arch.epdx = 0;
3369 if (test_kvm_facility(kvm, 139)) {
3370 kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
3371 if (kvm->arch.epoch > gtod->tod)
3372 kvm->arch.epdx -= 1;
3373 }
3374
3375 kvm_s390_vcpu_block_all(kvm);
3376 kvm_for_each_vcpu(i, vcpu, kvm) {
3377 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
3378 vcpu->arch.sie_block->epdx = kvm->arch.epdx;
3379 }
3380
3381 kvm_s390_vcpu_unblock_all(kvm);
3382 preempt_enable();
3383 mutex_unlock(&kvm->lock);
3384 }
3385
3386 /**
3387 * kvm_arch_fault_in_page - fault-in guest page if necessary
3388 * @vcpu: The corresponding virtual cpu
3389 * @gpa: Guest physical address
3390 * @writable: Whether the page should be writable or not
3391 *
3392 * Make sure that a guest page has been faulted-in on the host.
3393 *
3394 * Return: Zero on success, negative error code otherwise.
3395 */
3396 long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
3397 {
3398 return gmap_fault(vcpu->arch.gmap, gpa,
3399 writable ? FAULT_FLAG_WRITE : 0);
3400 }
3401
3402 static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
3403 unsigned long token)
3404 {
3405 struct kvm_s390_interrupt inti;
3406 struct kvm_s390_irq irq;
3407
3408 if (start_token) {
3409 irq.u.ext.ext_params2 = token;
3410 irq.type = KVM_S390_INT_PFAULT_INIT;
3411 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
3412 } else {
3413 inti.type = KVM_S390_INT_PFAULT_DONE;
3414 inti.parm64 = token;
3415 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
3416 }
3417 }
3418
3419 void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
3420 struct kvm_async_pf *work)
3421 {
3422 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
3423 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
3424 }
3425
3426 void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
3427 struct kvm_async_pf *work)
3428 {
3429 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
3430 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
3431 }
3432
3433 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
3434 struct kvm_async_pf *work)
3435 {
3436 /* s390 will always inject the page directly */
3437 }
3438
3439 bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
3440 {
3441 /*
3442 * s390 will always inject the page directly,
3443 * but we still want check_async_completion to cleanup
3444 */
3445 return true;
3446 }
3447
3448 static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
3449 {
3450 hva_t hva;
3451 struct kvm_arch_async_pf arch;
3452 int rc;
3453
3454 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3455 return 0;
3456 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
3457 vcpu->arch.pfault_compare)
3458 return 0;
3459 if (psw_extint_disabled(vcpu))
3460 return 0;
3461 if (kvm_s390_vcpu_has_irq(vcpu, 0))
3462 return 0;
3463 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
3464 return 0;
3465 if (!vcpu->arch.gmap->pfault_enabled)
3466 return 0;
3467
3468 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
3469 hva += current->thread.gmap_addr & ~PAGE_MASK;
3470 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
3471 return 0;
3472
3473 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
3474 return rc;
3475 }
3476
3477 static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3478 {
3479 int rc, cpuflags;
3480
3481 /*
3482 * On s390 notifications for arriving pages will be delivered directly
3483 * to the guest but the house keeping for completed pfaults is
3484 * handled outside the worker.
3485 */
3486 kvm_check_async_pf_completion(vcpu);
3487
3488 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
3489 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
3490
3491 if (need_resched())
3492 schedule();
3493
3494 if (test_cpu_flag(CIF_MCCK_PENDING))
3495 s390_handle_mcck();
3496
3497 if (!kvm_is_ucontrol(vcpu->kvm)) {
3498 rc = kvm_s390_deliver_pending_interrupts(vcpu);
3499 if (rc)
3500 return rc;
3501 }
3502
3503 rc = kvm_s390_handle_requests(vcpu);
3504 if (rc)
3505 return rc;
3506
3507 if (guestdbg_enabled(vcpu)) {
3508 kvm_s390_backup_guest_per_regs(vcpu);
3509 kvm_s390_patch_guest_per_regs(vcpu);
3510 }
3511
3512 vcpu->arch.sie_block->icptcode = 0;
3513 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
3514 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
3515 trace_kvm_s390_sie_enter(vcpu, cpuflags);
3516
3517 return 0;
3518 }
3519
3520 static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
3521 {
3522 struct kvm_s390_pgm_info pgm_info = {
3523 .code = PGM_ADDRESSING,
3524 };
3525 u8 opcode, ilen;
3526 int rc;
3527
3528 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
3529 trace_kvm_s390_sie_fault(vcpu);
3530
3531 /*
3532 * We want to inject an addressing exception, which is defined as a
3533 * suppressing or terminating exception. However, since we came here
3534 * by a DAT access exception, the PSW still points to the faulting
3535 * instruction since DAT exceptions are nullifying. So we've got
3536 * to look up the current opcode to get the length of the instruction
3537 * to be able to forward the PSW.
3538 */
3539 rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
3540 ilen = insn_length(opcode);
3541 if (rc < 0) {
3542 return rc;
3543 } else if (rc) {
3544 /* Instruction-Fetching Exceptions - we can't detect the ilen.
3545 * Forward by arbitrary ilc, injection will take care of
3546 * nullification if necessary.
3547 */
3548 pgm_info = vcpu->arch.pgm;
3549 ilen = 4;
3550 }
3551 pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
3552 kvm_s390_forward_psw(vcpu, ilen);
3553 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
3554 }
3555
3556 static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
3557 {
3558 struct mcck_volatile_info *mcck_info;
3559 struct sie_page *sie_page;
3560
3561 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
3562 vcpu->arch.sie_block->icptcode);
3563 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
3564
3565 if (guestdbg_enabled(vcpu))
3566 kvm_s390_restore_guest_per_regs(vcpu);
3567
3568 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
3569 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
3570
3571 if (exit_reason == -EINTR) {
3572 VCPU_EVENT(vcpu, 3, "%s", "machine check");
3573 sie_page = container_of(vcpu->arch.sie_block,
3574 struct sie_page, sie_block);
3575 mcck_info = &sie_page->mcck_info;
3576 kvm_s390_reinject_machine_check(vcpu, mcck_info);
3577 return 0;
3578 }
3579
3580 if (vcpu->arch.sie_block->icptcode > 0) {
3581 int rc = kvm_handle_sie_intercept(vcpu);
3582
3583 if (rc != -EOPNOTSUPP)
3584 return rc;
3585 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
3586 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
3587 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
3588 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
3589 return -EREMOTE;
3590 } else if (exit_reason != -EFAULT) {
3591 vcpu->stat.exit_null++;
3592 return 0;
3593 } else if (kvm_is_ucontrol(vcpu->kvm)) {
3594 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
3595 vcpu->run->s390_ucontrol.trans_exc_code =
3596 current->thread.gmap_addr;
3597 vcpu->run->s390_ucontrol.pgm_code = 0x10;
3598 return -EREMOTE;
3599 } else if (current->thread.gmap_pfault) {
3600 trace_kvm_s390_major_guest_pfault(vcpu);
3601 current->thread.gmap_pfault = 0;
3602 if (kvm_arch_setup_async_pf(vcpu))
3603 return 0;
3604 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
3605 }
3606 return vcpu_post_run_fault_in_sie(vcpu);
3607 }
3608
3609 static int __vcpu_run(struct kvm_vcpu *vcpu)
3610 {
3611 int rc, exit_reason;
3612
3613 /*
3614 * We try to hold kvm->srcu during most of vcpu_run (except when run-
3615 * ning the guest), so that memslots (and other stuff) are protected
3616 */
3617 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3618
3619 do {
3620 rc = vcpu_pre_run(vcpu);
3621 if (rc)
3622 break;
3623
3624 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
3625 /*
3626 * As PF_VCPU will be used in fault handler, between
3627 * guest_enter and guest_exit should be no uaccess.
3628 */
3629 local_irq_disable();
3630 guest_enter_irqoff();
3631 __disable_cpu_timer_accounting(vcpu);
3632 local_irq_enable();
3633 exit_reason = sie64a(vcpu->arch.sie_block,
3634 vcpu->run->s.regs.gprs);
3635 local_irq_disable();
3636 __enable_cpu_timer_accounting(vcpu);
3637 guest_exit_irqoff();
3638 local_irq_enable();
3639 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3640
3641 rc = vcpu_post_run(vcpu, exit_reason);
3642 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
3643
3644 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
3645 return rc;
3646 }
3647
3648 static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3649 {
3650 struct runtime_instr_cb *riccb;
3651 struct gs_cb *gscb;
3652
3653 riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
3654 gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
3655 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
3656 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
3657 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
3658 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
3659 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
3660 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
3661 /* some control register changes require a tlb flush */
3662 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3663 }
3664 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
3665 kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
3666 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
3667 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
3668 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
3669 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
3670 }
3671 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
3672 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
3673 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
3674 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
3675 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3676 kvm_clear_async_pf_completion_queue(vcpu);
3677 }
3678 /*
3679 * If userspace sets the riccb (e.g. after migration) to a valid state,
3680 * we should enable RI here instead of doing the lazy enablement.
3681 */
3682 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
3683 test_kvm_facility(vcpu->kvm, 64) &&
3684 riccb->v &&
3685 !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
3686 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
3687 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
3688 }
3689 /*
3690 * If userspace sets the gscb (e.g. after migration) to non-zero,
3691 * we should enable GS here instead of doing the lazy enablement.
3692 */
3693 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
3694 test_kvm_facility(vcpu->kvm, 133) &&
3695 gscb->gssm &&
3696 !vcpu->arch.gs_enabled) {
3697 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
3698 vcpu->arch.sie_block->ecb |= ECB_GS;
3699 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
3700 vcpu->arch.gs_enabled = 1;
3701 }
3702 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
3703 test_kvm_facility(vcpu->kvm, 82)) {
3704 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
3705 vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
3706 }
3707 save_access_regs(vcpu->arch.host_acrs);
3708 restore_access_regs(vcpu->run->s.regs.acrs);
3709 /* save host (userspace) fprs/vrs */
3710 save_fpu_regs();
3711 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
3712 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
3713 if (MACHINE_HAS_VX)
3714 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
3715 else
3716 current->thread.fpu.regs = vcpu->run->s.regs.fprs;
3717 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
3718 if (test_fp_ctl(current->thread.fpu.fpc))
3719 /* User space provided an invalid FPC, let's clear it */
3720 current->thread.fpu.fpc = 0;
3721 if (MACHINE_HAS_GS) {
3722 preempt_disable();
3723 __ctl_set_bit(2, 4);
3724 if (current->thread.gs_cb) {
3725 vcpu->arch.host_gscb = current->thread.gs_cb;
3726 save_gs_cb(vcpu->arch.host_gscb);
3727 }
3728 if (vcpu->arch.gs_enabled) {
3729 current->thread.gs_cb = (struct gs_cb *)
3730 &vcpu->run->s.regs.gscb;
3731 restore_gs_cb(current->thread.gs_cb);
3732 }
3733 preempt_enable();
3734 }
3735 /* SIE will load etoken directly from SDNX and therefore kvm_run */
3736
3737 kvm_run->kvm_dirty_regs = 0;
3738 }
3739
3740 static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3741 {
3742 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
3743 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
3744 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
3745 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
3746 kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
3747 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
3748 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
3749 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
3750 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
3751 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
3752 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
3753 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
3754 kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
3755 save_access_regs(vcpu->run->s.regs.acrs);
3756 restore_access_regs(vcpu->arch.host_acrs);
3757 /* Save guest register state */
3758 save_fpu_regs();
3759 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3760 /* Restore will be done lazily at return */
3761 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
3762 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
3763 if (MACHINE_HAS_GS) {
3764 __ctl_set_bit(2, 4);
3765 if (vcpu->arch.gs_enabled)
3766 save_gs_cb(current->thread.gs_cb);
3767 preempt_disable();
3768 current->thread.gs_cb = vcpu->arch.host_gscb;
3769 restore_gs_cb(vcpu->arch.host_gscb);
3770 preempt_enable();
3771 if (!vcpu->arch.host_gscb)
3772 __ctl_clear_bit(2, 4);
3773 vcpu->arch.host_gscb = NULL;
3774 }
3775 /* SIE will save etoken directly into SDNX and therefore kvm_run */
3776 }
3777
3778 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3779 {
3780 int rc;
3781
3782 if (kvm_run->immediate_exit)
3783 return -EINTR;
3784
3785 if (guestdbg_exit_pending(vcpu)) {
3786 kvm_s390_prepare_debug_exit(vcpu);
3787 return 0;
3788 }
3789
3790 kvm_sigset_activate(vcpu);
3791
3792 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
3793 kvm_s390_vcpu_start(vcpu);
3794 } else if (is_vcpu_stopped(vcpu)) {
3795 pr_err_ratelimited("can't run stopped vcpu %d\n",
3796 vcpu->vcpu_id);
3797 return -EINVAL;
3798 }
3799
3800 sync_regs(vcpu, kvm_run);
3801 enable_cpu_timer_accounting(vcpu);
3802
3803 might_fault();
3804 rc = __vcpu_run(vcpu);
3805
3806 if (signal_pending(current) && !rc) {
3807 kvm_run->exit_reason = KVM_EXIT_INTR;
3808 rc = -EINTR;
3809 }
3810
3811 if (guestdbg_exit_pending(vcpu) && !rc) {
3812 kvm_s390_prepare_debug_exit(vcpu);
3813 rc = 0;
3814 }
3815
3816 if (rc == -EREMOTE) {
3817 /* userspace support is needed, kvm_run has been prepared */
3818 rc = 0;
3819 }
3820
3821 disable_cpu_timer_accounting(vcpu);
3822 store_regs(vcpu, kvm_run);
3823
3824 kvm_sigset_deactivate(vcpu);
3825
3826 vcpu->stat.exit_userspace++;
3827 return rc;
3828 }
3829
3830 /*
3831 * store status at address
3832 * we use have two special cases:
3833 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3834 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3835 */
3836 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
3837 {
3838 unsigned char archmode = 1;
3839 freg_t fprs[NUM_FPRS];
3840 unsigned int px;
3841 u64 clkcomp, cputm;
3842 int rc;
3843
3844 px = kvm_s390_get_prefix(vcpu);
3845 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
3846 if (write_guest_abs(vcpu, 163, &archmode, 1))
3847 return -EFAULT;
3848 gpa = 0;
3849 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
3850 if (write_guest_real(vcpu, 163, &archmode, 1))
3851 return -EFAULT;
3852 gpa = px;
3853 } else
3854 gpa -= __LC_FPREGS_SAVE_AREA;
3855
3856 /* manually convert vector registers if necessary */
3857 if (MACHINE_HAS_VX) {
3858 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
3859 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
3860 fprs, 128);
3861 } else {
3862 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
3863 vcpu->run->s.regs.fprs, 128);
3864 }
3865 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
3866 vcpu->run->s.regs.gprs, 128);
3867 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
3868 &vcpu->arch.sie_block->gpsw, 16);
3869 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
3870 &px, 4);
3871 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
3872 &vcpu->run->s.regs.fpc, 4);
3873 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
3874 &vcpu->arch.sie_block->todpr, 4);
3875 cputm = kvm_s390_get_cpu_timer(vcpu);
3876 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
3877 &cputm, 8);
3878 clkcomp = vcpu->arch.sie_block->ckc >> 8;
3879 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
3880 &clkcomp, 8);
3881 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
3882 &vcpu->run->s.regs.acrs, 64);
3883 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
3884 &vcpu->arch.sie_block->gcr, 128);
3885 return rc ? -EFAULT : 0;
3886 }
3887
3888 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
3889 {
3890 /*
3891 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
3892 * switch in the run ioctl. Let's update our copies before we save
3893 * it into the save area
3894 */
3895 save_fpu_regs();
3896 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3897 save_access_regs(vcpu->run->s.regs.acrs);
3898
3899 return kvm_s390_store_status_unloaded(vcpu, addr);
3900 }
3901
3902 static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
3903 {
3904 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
3905 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
3906 }
3907
3908 static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
3909 {
3910 unsigned int i;
3911 struct kvm_vcpu *vcpu;
3912
3913 kvm_for_each_vcpu(i, vcpu, kvm) {
3914 __disable_ibs_on_vcpu(vcpu);
3915 }
3916 }
3917
3918 static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
3919 {
3920 if (!sclp.has_ibs)
3921 return;
3922 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
3923 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
3924 }
3925
3926 void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
3927 {
3928 int i, online_vcpus, started_vcpus = 0;
3929
3930 if (!is_vcpu_stopped(vcpu))
3931 return;
3932
3933 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
3934 /* Only one cpu at a time may enter/leave the STOPPED state. */
3935 spin_lock(&vcpu->kvm->arch.start_stop_lock);
3936 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
3937
3938 for (i = 0; i < online_vcpus; i++) {
3939 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
3940 started_vcpus++;
3941 }
3942
3943 if (started_vcpus == 0) {
3944 /* we're the only active VCPU -> speed it up */
3945 __enable_ibs_on_vcpu(vcpu);
3946 } else if (started_vcpus == 1) {
3947 /*
3948 * As we are starting a second VCPU, we have to disable
3949 * the IBS facility on all VCPUs to remove potentially
3950 * oustanding ENABLE requests.
3951 */
3952 __disable_ibs_on_all_vcpus(vcpu->kvm);
3953 }
3954
3955 atomic_andnot(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
3956 /*
3957 * Another VCPU might have used IBS while we were offline.
3958 * Let's play safe and flush the VCPU at startup.
3959 */
3960 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3961 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
3962 return;
3963 }
3964
3965 void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
3966 {
3967 int i, online_vcpus, started_vcpus = 0;
3968 struct kvm_vcpu *started_vcpu = NULL;
3969
3970 if (is_vcpu_stopped(vcpu))
3971 return;
3972
3973 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
3974 /* Only one cpu at a time may enter/leave the STOPPED state. */
3975 spin_lock(&vcpu->kvm->arch.start_stop_lock);
3976 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
3977
3978 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
3979 kvm_s390_clear_stop_irq(vcpu);
3980
3981 atomic_or(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags);
3982 __disable_ibs_on_vcpu(vcpu);
3983
3984 for (i = 0; i < online_vcpus; i++) {
3985 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
3986 started_vcpus++;
3987 started_vcpu = vcpu->kvm->vcpus[i];
3988 }
3989 }
3990
3991 if (started_vcpus == 1) {
3992 /*
3993 * As we only have one VCPU left, we want to enable the
3994 * IBS facility for that VCPU to speed it up.
3995 */
3996 __enable_ibs_on_vcpu(started_vcpu);
3997 }
3998
3999 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4000 return;
4001 }
4002
4003 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4004 struct kvm_enable_cap *cap)
4005 {
4006 int r;
4007
4008 if (cap->flags)
4009 return -EINVAL;
4010
4011 switch (cap->cap) {
4012 case KVM_CAP_S390_CSS_SUPPORT:
4013 if (!vcpu->kvm->arch.css_support) {
4014 vcpu->kvm->arch.css_support = 1;
4015 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
4016 trace_kvm_s390_enable_css(vcpu->kvm);
4017 }
4018 r = 0;
4019 break;
4020 default:
4021 r = -EINVAL;
4022 break;
4023 }
4024 return r;
4025 }
4026
4027 static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
4028 struct kvm_s390_mem_op *mop)
4029 {
4030 void __user *uaddr = (void __user *)mop->buf;
4031 void *tmpbuf = NULL;
4032 int r, srcu_idx;
4033 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
4034 | KVM_S390_MEMOP_F_CHECK_ONLY;
4035
4036 if (mop->flags & ~supported_flags)
4037 return -EINVAL;
4038
4039 if (mop->size > MEM_OP_MAX_SIZE)
4040 return -E2BIG;
4041
4042 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
4043 tmpbuf = vmalloc(mop->size);
4044 if (!tmpbuf)
4045 return -ENOMEM;
4046 }
4047
4048 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4049
4050 switch (mop->op) {
4051 case KVM_S390_MEMOP_LOGICAL_READ:
4052 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4053 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4054 mop->size, GACC_FETCH);
4055 break;
4056 }
4057 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4058 if (r == 0) {
4059 if (copy_to_user(uaddr, tmpbuf, mop->size))
4060 r = -EFAULT;
4061 }
4062 break;
4063 case KVM_S390_MEMOP_LOGICAL_WRITE:
4064 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4065 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4066 mop->size, GACC_STORE);
4067 break;
4068 }
4069 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
4070 r = -EFAULT;
4071 break;
4072 }
4073 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4074 break;
4075 default:
4076 r = -EINVAL;
4077 }
4078
4079 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
4080
4081 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
4082 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
4083
4084 vfree(tmpbuf);
4085 return r;
4086 }
4087
4088 long kvm_arch_vcpu_ioctl(struct file *filp,
4089 unsigned int ioctl, unsigned long arg)
4090 {
4091 struct kvm_vcpu *vcpu = filp->private_data;
4092 void __user *argp = (void __user *)arg;
4093 int idx;
4094 long r;
4095
4096 switch (ioctl) {
4097 case KVM_S390_IRQ: {
4098 struct kvm_s390_irq s390irq;
4099
4100 r = -EFAULT;
4101 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
4102 break;
4103 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
4104 break;
4105 }
4106 case KVM_S390_INTERRUPT: {
4107 struct kvm_s390_interrupt s390int;
4108 struct kvm_s390_irq s390irq;
4109
4110 r = -EFAULT;
4111 if (copy_from_user(&s390int, argp, sizeof(s390int)))
4112 break;
4113 if (s390int_to_s390irq(&s390int, &s390irq))
4114 return -EINVAL;
4115 r = kvm_s390_inject_vcpu(vcpu, &s390irq);
4116 break;
4117 }
4118 case KVM_S390_STORE_STATUS:
4119 idx = srcu_read_lock(&vcpu->kvm->srcu);
4120 r = kvm_s390_vcpu_store_status(vcpu, arg);
4121 srcu_read_unlock(&vcpu->kvm->srcu, idx);
4122 break;
4123 case KVM_S390_SET_INITIAL_PSW: {
4124 psw_t psw;
4125
4126 r = -EFAULT;
4127 if (copy_from_user(&psw, argp, sizeof(psw)))
4128 break;
4129 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
4130 break;
4131 }
4132 case KVM_S390_INITIAL_RESET:
4133 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
4134 break;
4135 case KVM_SET_ONE_REG:
4136 case KVM_GET_ONE_REG: {
4137 struct kvm_one_reg reg;
4138 r = -EFAULT;
4139 if (copy_from_user(&reg, argp, sizeof(reg)))
4140 break;
4141 if (ioctl == KVM_SET_ONE_REG)
4142 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, &reg);
4143 else
4144 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, &reg);
4145 break;
4146 }
4147 #ifdef CONFIG_KVM_S390_UCONTROL
4148 case KVM_S390_UCAS_MAP: {
4149 struct kvm_s390_ucas_mapping ucasmap;
4150
4151 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4152 r = -EFAULT;
4153 break;
4154 }
4155
4156 if (!kvm_is_ucontrol(vcpu->kvm)) {
4157 r = -EINVAL;
4158 break;
4159 }
4160
4161 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
4162 ucasmap.vcpu_addr, ucasmap.length);
4163 break;
4164 }
4165 case KVM_S390_UCAS_UNMAP: {
4166 struct kvm_s390_ucas_mapping ucasmap;
4167
4168 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4169 r = -EFAULT;
4170 break;
4171 }
4172
4173 if (!kvm_is_ucontrol(vcpu->kvm)) {
4174 r = -EINVAL;
4175 break;
4176 }
4177
4178 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
4179 ucasmap.length);
4180 break;
4181 }
4182 #endif
4183 case KVM_S390_VCPU_FAULT: {
4184 r = gmap_fault(vcpu->arch.gmap, arg, 0);
4185 break;
4186 }
4187 case KVM_ENABLE_CAP:
4188 {
4189 struct kvm_enable_cap cap;
4190 r = -EFAULT;
4191 if (copy_from_user(&cap, argp, sizeof(cap)))
4192 break;
4193 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4194 break;
4195 }
4196 case KVM_S390_MEM_OP: {
4197 struct kvm_s390_mem_op mem_op;
4198
4199 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
4200 r = kvm_s390_guest_mem_op(vcpu, &mem_op);
4201 else
4202 r = -EFAULT;
4203 break;
4204 }
4205 case KVM_S390_SET_IRQ_STATE: {
4206 struct kvm_s390_irq_state irq_state;
4207
4208 r = -EFAULT;
4209 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4210 break;
4211 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
4212 irq_state.len == 0 ||
4213 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
4214 r = -EINVAL;
4215 break;
4216 }
4217 /* do not use irq_state.flags, it will break old QEMUs */
4218 r = kvm_s390_set_irq_state(vcpu,
4219 (void __user *) irq_state.buf,
4220 irq_state.len);
4221 break;
4222 }
4223 case KVM_S390_GET_IRQ_STATE: {
4224 struct kvm_s390_irq_state irq_state;
4225
4226 r = -EFAULT;
4227 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4228 break;
4229 if (irq_state.len == 0) {
4230 r = -EINVAL;
4231 break;
4232 }
4233 /* do not use irq_state.flags, it will break old QEMUs */
4234 r = kvm_s390_get_irq_state(vcpu,
4235 (__u8 __user *) irq_state.buf,
4236 irq_state.len);
4237 break;
4238 }
4239 default:
4240 r = -ENOTTY;
4241 }
4242 return r;
4243 }
4244
4245 int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
4246 {
4247 #ifdef CONFIG_KVM_S390_UCONTROL
4248 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
4249 && (kvm_is_ucontrol(vcpu->kvm))) {
4250 vmf->page = virt_to_page(vcpu->arch.sie_block);
4251 get_page(vmf->page);
4252 return 0;
4253 }
4254 #endif
4255 return VM_FAULT_SIGBUS;
4256 }
4257
4258 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
4259 unsigned long npages)
4260 {
4261 return 0;
4262 }
4263
4264 /* Section: memory related */
4265 int kvm_arch_prepare_memory_region(struct kvm *kvm,
4266 struct kvm_memory_slot *memslot,
4267 const struct kvm_userspace_memory_region *mem,
4268 enum kvm_mr_change change)
4269 {
4270 /* A few sanity checks. We can have memory slots which have to be
4271 located/ended at a segment boundary (1MB). The memory in userland is
4272 ok to be fragmented into various different vmas. It is okay to mmap()
4273 and munmap() stuff in this slot after doing this call at any time */
4274
4275 if (mem->userspace_addr & 0xffffful)
4276 return -EINVAL;
4277
4278 if (mem->memory_size & 0xffffful)
4279 return -EINVAL;
4280
4281 if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
4282 return -EINVAL;
4283
4284 return 0;
4285 }
4286
4287 void kvm_arch_commit_memory_region(struct kvm *kvm,
4288 const struct kvm_userspace_memory_region *mem,
4289 const struct kvm_memory_slot *old,
4290 const struct kvm_memory_slot *new,
4291 enum kvm_mr_change change)
4292 {
4293 int rc;
4294
4295 /* If the basics of the memslot do not change, we do not want
4296 * to update the gmap. Every update causes several unnecessary
4297 * segment translation exceptions. This is usually handled just
4298 * fine by the normal fault handler + gmap, but it will also
4299 * cause faults on the prefix page of running guest CPUs.
4300 */
4301 if (old->userspace_addr == mem->userspace_addr &&
4302 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
4303 old->npages * PAGE_SIZE == mem->memory_size)
4304 return;
4305
4306 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
4307 mem->guest_phys_addr, mem->memory_size);
4308 if (rc)
4309 pr_warn("failed to commit memory region\n");
4310 return;
4311 }
4312
4313 static inline unsigned long nonhyp_mask(int i)
4314 {
4315 unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
4316
4317 return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
4318 }
4319
4320 void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
4321 {
4322 vcpu->valid_wakeup = false;
4323 }
4324
4325 static int __init kvm_s390_init(void)
4326 {
4327 int i;
4328
4329 if (!sclp.has_sief2) {
4330 pr_info("SIE not available\n");
4331 return -ENODEV;
4332 }
4333
4334 for (i = 0; i < 16; i++)
4335 kvm_s390_fac_base[i] |=
4336 S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
4337
4338 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
4339 }
4340
4341 static void __exit kvm_s390_exit(void)
4342 {
4343 kvm_exit();
4344 }
4345
4346 module_init(kvm_s390_init);
4347 module_exit(kvm_s390_exit);
4348
4349 /*
4350 * Enable autoloading of the kvm module.
4351 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
4352 * since x86 takes a different approach.
4353 */
4354 #include <linux/miscdevice.h>
4355 MODULE_ALIAS_MISCDEV(KVM_MINOR);
4356 MODULE_ALIAS("devname:kvm");