1 // SPDX-License-Identifier: GPL-2.0
3 * hosting IBM Z kernel virtual machines (s390x)
5 * Copyright IBM Corp. 2008, 2020
14 #define KMSG_COMPONENT "kvm-s390"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 #include <linux/compiler.h>
18 #include <linux/err.h>
20 #include <linux/hrtimer.h>
21 #include <linux/init.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/mman.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/random.h>
28 #include <linux/slab.h>
29 #include <linux/timer.h>
30 #include <linux/vmalloc.h>
31 #include <linux/bitmap.h>
32 #include <linux/sched/signal.h>
33 #include <linux/string.h>
34 #include <linux/pgtable.h>
36 #include <asm/asm-offsets.h>
37 #include <asm/lowcore.h>
41 #include <asm/switch_to.h>
44 #include <asm/cpacf.h>
45 #include <asm/timex.h>
48 #include <asm/fpu/api.h>
52 #define CREATE_TRACE_POINTS
54 #include "trace-s390.h"
56 #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
58 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
59 (KVM_MAX_VCPUS + LOCAL_IRQS))
61 const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
62 KVM_GENERIC_VM_STATS(),
63 STATS_DESC_COUNTER(VM, inject_io),
64 STATS_DESC_COUNTER(VM, inject_float_mchk),
65 STATS_DESC_COUNTER(VM, inject_pfault_done),
66 STATS_DESC_COUNTER(VM, inject_service_signal),
67 STATS_DESC_COUNTER(VM, inject_virtio)
70 const struct kvm_stats_header kvm_vm_stats_header = {
71 .name_size = KVM_STATS_NAME_SIZE,
72 .num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
73 .id_offset = sizeof(struct kvm_stats_header),
74 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
75 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
76 sizeof(kvm_vm_stats_desc),
79 const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
80 KVM_GENERIC_VCPU_STATS(),
81 STATS_DESC_COUNTER(VCPU, exit_userspace),
82 STATS_DESC_COUNTER(VCPU, exit_null),
83 STATS_DESC_COUNTER(VCPU, exit_external_request),
84 STATS_DESC_COUNTER(VCPU, exit_io_request),
85 STATS_DESC_COUNTER(VCPU, exit_external_interrupt),
86 STATS_DESC_COUNTER(VCPU, exit_stop_request),
87 STATS_DESC_COUNTER(VCPU, exit_validity),
88 STATS_DESC_COUNTER(VCPU, exit_instruction),
89 STATS_DESC_COUNTER(VCPU, exit_pei),
90 STATS_DESC_COUNTER(VCPU, halt_no_poll_steal),
91 STATS_DESC_COUNTER(VCPU, instruction_lctl),
92 STATS_DESC_COUNTER(VCPU, instruction_lctlg),
93 STATS_DESC_COUNTER(VCPU, instruction_stctl),
94 STATS_DESC_COUNTER(VCPU, instruction_stctg),
95 STATS_DESC_COUNTER(VCPU, exit_program_interruption),
96 STATS_DESC_COUNTER(VCPU, exit_instr_and_program),
97 STATS_DESC_COUNTER(VCPU, exit_operation_exception),
98 STATS_DESC_COUNTER(VCPU, deliver_ckc),
99 STATS_DESC_COUNTER(VCPU, deliver_cputm),
100 STATS_DESC_COUNTER(VCPU, deliver_external_call),
101 STATS_DESC_COUNTER(VCPU, deliver_emergency_signal),
102 STATS_DESC_COUNTER(VCPU, deliver_service_signal),
103 STATS_DESC_COUNTER(VCPU, deliver_virtio),
104 STATS_DESC_COUNTER(VCPU, deliver_stop_signal),
105 STATS_DESC_COUNTER(VCPU, deliver_prefix_signal),
106 STATS_DESC_COUNTER(VCPU, deliver_restart_signal),
107 STATS_DESC_COUNTER(VCPU, deliver_program),
108 STATS_DESC_COUNTER(VCPU, deliver_io),
109 STATS_DESC_COUNTER(VCPU, deliver_machine_check),
110 STATS_DESC_COUNTER(VCPU, exit_wait_state),
111 STATS_DESC_COUNTER(VCPU, inject_ckc),
112 STATS_DESC_COUNTER(VCPU, inject_cputm),
113 STATS_DESC_COUNTER(VCPU, inject_external_call),
114 STATS_DESC_COUNTER(VCPU, inject_emergency_signal),
115 STATS_DESC_COUNTER(VCPU, inject_mchk),
116 STATS_DESC_COUNTER(VCPU, inject_pfault_init),
117 STATS_DESC_COUNTER(VCPU, inject_program),
118 STATS_DESC_COUNTER(VCPU, inject_restart),
119 STATS_DESC_COUNTER(VCPU, inject_set_prefix),
120 STATS_DESC_COUNTER(VCPU, inject_stop_signal),
121 STATS_DESC_COUNTER(VCPU, instruction_epsw),
122 STATS_DESC_COUNTER(VCPU, instruction_gs),
123 STATS_DESC_COUNTER(VCPU, instruction_io_other),
124 STATS_DESC_COUNTER(VCPU, instruction_lpsw),
125 STATS_DESC_COUNTER(VCPU, instruction_lpswe),
126 STATS_DESC_COUNTER(VCPU, instruction_pfmf),
127 STATS_DESC_COUNTER(VCPU, instruction_ptff),
128 STATS_DESC_COUNTER(VCPU, instruction_sck),
129 STATS_DESC_COUNTER(VCPU, instruction_sckpf),
130 STATS_DESC_COUNTER(VCPU, instruction_stidp),
131 STATS_DESC_COUNTER(VCPU, instruction_spx),
132 STATS_DESC_COUNTER(VCPU, instruction_stpx),
133 STATS_DESC_COUNTER(VCPU, instruction_stap),
134 STATS_DESC_COUNTER(VCPU, instruction_iske),
135 STATS_DESC_COUNTER(VCPU, instruction_ri),
136 STATS_DESC_COUNTER(VCPU, instruction_rrbe),
137 STATS_DESC_COUNTER(VCPU, instruction_sske),
138 STATS_DESC_COUNTER(VCPU, instruction_ipte_interlock),
139 STATS_DESC_COUNTER(VCPU, instruction_stsi),
140 STATS_DESC_COUNTER(VCPU, instruction_stfl),
141 STATS_DESC_COUNTER(VCPU, instruction_tb),
142 STATS_DESC_COUNTER(VCPU, instruction_tpi),
143 STATS_DESC_COUNTER(VCPU, instruction_tprot),
144 STATS_DESC_COUNTER(VCPU, instruction_tsch),
145 STATS_DESC_COUNTER(VCPU, instruction_sie),
146 STATS_DESC_COUNTER(VCPU, instruction_essa),
147 STATS_DESC_COUNTER(VCPU, instruction_sthyi),
148 STATS_DESC_COUNTER(VCPU, instruction_sigp_sense),
149 STATS_DESC_COUNTER(VCPU, instruction_sigp_sense_running),
150 STATS_DESC_COUNTER(VCPU, instruction_sigp_external_call),
151 STATS_DESC_COUNTER(VCPU, instruction_sigp_emergency),
152 STATS_DESC_COUNTER(VCPU, instruction_sigp_cond_emergency),
153 STATS_DESC_COUNTER(VCPU, instruction_sigp_start),
154 STATS_DESC_COUNTER(VCPU, instruction_sigp_stop),
155 STATS_DESC_COUNTER(VCPU, instruction_sigp_stop_store_status),
156 STATS_DESC_COUNTER(VCPU, instruction_sigp_store_status),
157 STATS_DESC_COUNTER(VCPU, instruction_sigp_store_adtl_status),
158 STATS_DESC_COUNTER(VCPU, instruction_sigp_arch),
159 STATS_DESC_COUNTER(VCPU, instruction_sigp_prefix),
160 STATS_DESC_COUNTER(VCPU, instruction_sigp_restart),
161 STATS_DESC_COUNTER(VCPU, instruction_sigp_init_cpu_reset),
162 STATS_DESC_COUNTER(VCPU, instruction_sigp_cpu_reset),
163 STATS_DESC_COUNTER(VCPU, instruction_sigp_unknown),
164 STATS_DESC_COUNTER(VCPU, instruction_diagnose_10),
165 STATS_DESC_COUNTER(VCPU, instruction_diagnose_44),
166 STATS_DESC_COUNTER(VCPU, instruction_diagnose_9c),
167 STATS_DESC_COUNTER(VCPU, diag_9c_ignored),
168 STATS_DESC_COUNTER(VCPU, diag_9c_forward),
169 STATS_DESC_COUNTER(VCPU, instruction_diagnose_258),
170 STATS_DESC_COUNTER(VCPU, instruction_diagnose_308),
171 STATS_DESC_COUNTER(VCPU, instruction_diagnose_500),
172 STATS_DESC_COUNTER(VCPU, instruction_diagnose_other),
173 STATS_DESC_COUNTER(VCPU, pfault_sync)
176 const struct kvm_stats_header kvm_vcpu_stats_header = {
177 .name_size = KVM_STATS_NAME_SIZE,
178 .num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
179 .id_offset = sizeof(struct kvm_stats_header),
180 .desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
181 .data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
182 sizeof(kvm_vcpu_stats_desc),
185 /* allow nested virtualization in KVM (if enabled by user space) */
187 module_param(nested, int, S_IRUGO);
188 MODULE_PARM_DESC(nested, "Nested virtualization support");
190 /* allow 1m huge page guest backing, if !nested */
192 module_param(hpage, int, 0444);
193 MODULE_PARM_DESC(hpage, "1m huge page backing support");
195 /* maximum percentage of steal time for polling. >100 is treated like 100 */
196 static u8 halt_poll_max_steal = 10;
197 module_param(halt_poll_max_steal, byte, 0644);
198 MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
200 /* if set to true, the GISA will be initialized and used if available */
201 static bool use_gisa = true;
202 module_param(use_gisa, bool, 0644);
203 MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
205 /* maximum diag9c forwarding per second */
206 unsigned int diag9c_forwarding_hz;
207 module_param(diag9c_forwarding_hz, uint, 0644);
208 MODULE_PARM_DESC(diag9c_forwarding_hz, "Maximum diag9c forwarding per second, 0 to turn off");
211 * For now we handle at most 16 double words as this is what the s390 base
212 * kernel handles and stores in the prefix page. If we ever need to go beyond
213 * this, this requires changes to code, but the external uapi can stay.
215 #define SIZE_INTERNAL 16
218 * Base feature mask that defines default mask for facilities. Consists of the
219 * defines in FACILITIES_KVM and the non-hypervisor managed bits.
221 static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
223 * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
224 * and defines the facilities that can be enabled via a cpu model.
226 static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
228 static unsigned long kvm_s390_fac_size(void)
230 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
231 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
232 BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
233 sizeof(stfle_fac_list));
235 return SIZE_INTERNAL;
238 /* available cpu features supported by kvm */
239 static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
240 /* available subfunctions indicated via query / "test bit" */
241 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
243 static struct gmap_notifier gmap_notifier;
244 static struct gmap_notifier vsie_gmap_notifier;
245 debug_info_t *kvm_s390_dbf;
246 debug_info_t *kvm_s390_dbf_uv;
248 /* Section: not file related */
249 int kvm_arch_hardware_enable(void)
251 /* every s390 is virtualization enabled ;-) */
255 int kvm_arch_check_processor_compat(void *opaque)
260 /* forward declarations */
261 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
263 static int sca_switch_to_extended(struct kvm *kvm);
265 static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
270 * The TOD jumps by delta, we have to compensate this by adding
271 * -delta to the epoch.
275 /* sign-extension - we're adding to signed values below */
280 if (scb->ecd & ECD_MEF) {
281 scb->epdx += delta_idx;
282 if (scb->epoch < delta)
288 * This callback is executed during stop_machine(). All CPUs are therefore
289 * temporarily stopped. In order not to change guest behavior, we have to
290 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
291 * so a CPU won't be stopped while calculating with the epoch.
293 static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
297 struct kvm_vcpu *vcpu;
299 unsigned long long *delta = v;
301 list_for_each_entry(kvm, &vm_list, vm_list) {
302 kvm_for_each_vcpu(i, vcpu, kvm) {
303 kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
305 kvm->arch.epoch = vcpu->arch.sie_block->epoch;
306 kvm->arch.epdx = vcpu->arch.sie_block->epdx;
308 if (vcpu->arch.cputm_enabled)
309 vcpu->arch.cputm_start += *delta;
310 if (vcpu->arch.vsie_block)
311 kvm_clock_sync_scb(vcpu->arch.vsie_block,
318 static struct notifier_block kvm_clock_notifier = {
319 .notifier_call = kvm_clock_sync,
322 int kvm_arch_hardware_setup(void *opaque)
324 gmap_notifier.notifier_call = kvm_gmap_notifier;
325 gmap_register_pte_notifier(&gmap_notifier);
326 vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
327 gmap_register_pte_notifier(&vsie_gmap_notifier);
328 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
329 &kvm_clock_notifier);
333 void kvm_arch_hardware_unsetup(void)
335 gmap_unregister_pte_notifier(&gmap_notifier);
336 gmap_unregister_pte_notifier(&vsie_gmap_notifier);
337 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
338 &kvm_clock_notifier);
341 static void allow_cpu_feat(unsigned long nr)
343 set_bit_inv(nr, kvm_s390_available_cpu_feat);
346 static inline int plo_test_bit(unsigned char nr)
348 unsigned long function = (unsigned long)nr | 0x100;
352 " lgr 0,%[function]\n"
353 /* Parameter registers are ignored for "test bit" */
358 : [function] "d" (function)
363 static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
368 /* Parameter registers are ignored */
369 " .insn rrf,%[opc] << 16,2,4,6,0\n"
371 : [query] "d" ((unsigned long)query), [opc] "i" (opcode)
372 : "cc", "memory", "0", "1");
375 #define INSN_SORTL 0xb938
376 #define INSN_DFLTCC 0xb939
378 static void kvm_s390_cpu_feat_init(void)
382 for (i = 0; i < 256; ++i) {
384 kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
387 if (test_facility(28)) /* TOD-clock steering */
388 ptff(kvm_s390_available_subfunc.ptff,
389 sizeof(kvm_s390_available_subfunc.ptff),
392 if (test_facility(17)) { /* MSA */
393 __cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
394 kvm_s390_available_subfunc.kmac);
395 __cpacf_query(CPACF_KMC, (cpacf_mask_t *)
396 kvm_s390_available_subfunc.kmc);
397 __cpacf_query(CPACF_KM, (cpacf_mask_t *)
398 kvm_s390_available_subfunc.km);
399 __cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
400 kvm_s390_available_subfunc.kimd);
401 __cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
402 kvm_s390_available_subfunc.klmd);
404 if (test_facility(76)) /* MSA3 */
405 __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
406 kvm_s390_available_subfunc.pckmo);
407 if (test_facility(77)) { /* MSA4 */
408 __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
409 kvm_s390_available_subfunc.kmctr);
410 __cpacf_query(CPACF_KMF, (cpacf_mask_t *)
411 kvm_s390_available_subfunc.kmf);
412 __cpacf_query(CPACF_KMO, (cpacf_mask_t *)
413 kvm_s390_available_subfunc.kmo);
414 __cpacf_query(CPACF_PCC, (cpacf_mask_t *)
415 kvm_s390_available_subfunc.pcc);
417 if (test_facility(57)) /* MSA5 */
418 __cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
419 kvm_s390_available_subfunc.ppno);
421 if (test_facility(146)) /* MSA8 */
422 __cpacf_query(CPACF_KMA, (cpacf_mask_t *)
423 kvm_s390_available_subfunc.kma);
425 if (test_facility(155)) /* MSA9 */
426 __cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
427 kvm_s390_available_subfunc.kdsa);
429 if (test_facility(150)) /* SORTL */
430 __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
432 if (test_facility(151)) /* DFLTCC */
433 __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
435 if (MACHINE_HAS_ESOP)
436 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
438 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
439 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
441 if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
442 !test_facility(3) || !nested)
444 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
445 if (sclp.has_64bscao)
446 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
448 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
450 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
452 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
454 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
456 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
458 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
460 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
462 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
463 * all skey handling functions read/set the skey from the PGSTE
464 * instead of the real storage key.
466 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
467 * pages being detected as preserved although they are resident.
469 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
470 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
472 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
473 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
474 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
476 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
477 * cannot easily shadow the SCA because of the ipte lock.
481 int kvm_arch_init(void *opaque)
485 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
489 kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
490 if (!kvm_s390_dbf_uv)
493 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
494 debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
497 kvm_s390_cpu_feat_init();
499 /* Register floating interrupt controller interface. */
500 rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
502 pr_err("A FLIC registration call failed with rc=%d\n", rc);
506 rc = kvm_s390_gib_init(GAL_ISC);
517 void kvm_arch_exit(void)
519 kvm_s390_gib_destroy();
520 debug_unregister(kvm_s390_dbf);
521 debug_unregister(kvm_s390_dbf_uv);
524 /* Section: device related */
525 long kvm_arch_dev_ioctl(struct file *filp,
526 unsigned int ioctl, unsigned long arg)
528 if (ioctl == KVM_S390_ENABLE_SIE)
529 return s390_enable_sie();
533 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
538 case KVM_CAP_S390_PSW:
539 case KVM_CAP_S390_GMAP:
540 case KVM_CAP_SYNC_MMU:
541 #ifdef CONFIG_KVM_S390_UCONTROL
542 case KVM_CAP_S390_UCONTROL:
544 case KVM_CAP_ASYNC_PF:
545 case KVM_CAP_SYNC_REGS:
546 case KVM_CAP_ONE_REG:
547 case KVM_CAP_ENABLE_CAP:
548 case KVM_CAP_S390_CSS_SUPPORT:
549 case KVM_CAP_IOEVENTFD:
550 case KVM_CAP_DEVICE_CTRL:
551 case KVM_CAP_S390_IRQCHIP:
552 case KVM_CAP_VM_ATTRIBUTES:
553 case KVM_CAP_MP_STATE:
554 case KVM_CAP_IMMEDIATE_EXIT:
555 case KVM_CAP_S390_INJECT_IRQ:
556 case KVM_CAP_S390_USER_SIGP:
557 case KVM_CAP_S390_USER_STSI:
558 case KVM_CAP_S390_SKEYS:
559 case KVM_CAP_S390_IRQ_STATE:
560 case KVM_CAP_S390_USER_INSTR0:
561 case KVM_CAP_S390_CMMA_MIGRATION:
562 case KVM_CAP_S390_AIS:
563 case KVM_CAP_S390_AIS_MIGRATION:
564 case KVM_CAP_S390_VCPU_RESETS:
565 case KVM_CAP_SET_GUEST_DEBUG:
566 case KVM_CAP_S390_DIAG318:
569 case KVM_CAP_SET_GUEST_DEBUG2:
570 r = KVM_GUESTDBG_VALID_MASK;
572 case KVM_CAP_S390_HPAGE_1M:
574 if (hpage && !kvm_is_ucontrol(kvm))
577 case KVM_CAP_S390_MEM_OP:
580 case KVM_CAP_NR_VCPUS:
581 case KVM_CAP_MAX_VCPUS:
582 case KVM_CAP_MAX_VCPU_ID:
583 r = KVM_S390_BSCA_CPU_SLOTS;
584 if (!kvm_s390_use_sca_entries())
586 else if (sclp.has_esca && sclp.has_64bscao)
587 r = KVM_S390_ESCA_CPU_SLOTS;
588 if (ext == KVM_CAP_NR_VCPUS)
589 r = min_t(unsigned int, num_online_cpus(), r);
591 case KVM_CAP_S390_COW:
592 r = MACHINE_HAS_ESOP;
594 case KVM_CAP_S390_VECTOR_REGISTERS:
597 case KVM_CAP_S390_RI:
598 r = test_facility(64);
600 case KVM_CAP_S390_GS:
601 r = test_facility(133);
603 case KVM_CAP_S390_BPB:
604 r = test_facility(82);
606 case KVM_CAP_S390_PROTECTED:
607 r = is_prot_virt_host();
615 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
618 gfn_t cur_gfn, last_gfn;
619 unsigned long gaddr, vmaddr;
620 struct gmap *gmap = kvm->arch.gmap;
621 DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
623 /* Loop over all guest segments */
624 cur_gfn = memslot->base_gfn;
625 last_gfn = memslot->base_gfn + memslot->npages;
626 for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
627 gaddr = gfn_to_gpa(cur_gfn);
628 vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
629 if (kvm_is_error_hva(vmaddr))
632 bitmap_zero(bitmap, _PAGE_ENTRIES);
633 gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
634 for (i = 0; i < _PAGE_ENTRIES; i++) {
635 if (test_bit(i, bitmap))
636 mark_page_dirty(kvm, cur_gfn + i);
639 if (fatal_signal_pending(current))
645 /* Section: vm related */
646 static void sca_del_vcpu(struct kvm_vcpu *vcpu);
649 * Get (and clear) the dirty memory log for a memory slot.
651 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
652 struct kvm_dirty_log *log)
656 struct kvm_memory_slot *memslot;
659 if (kvm_is_ucontrol(kvm))
662 mutex_lock(&kvm->slots_lock);
665 if (log->slot >= KVM_USER_MEM_SLOTS)
668 r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
672 /* Clear the dirty log */
674 n = kvm_dirty_bitmap_bytes(memslot);
675 memset(memslot->dirty_bitmap, 0, n);
679 mutex_unlock(&kvm->slots_lock);
683 static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
686 struct kvm_vcpu *vcpu;
688 kvm_for_each_vcpu(i, vcpu, kvm) {
689 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
693 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
701 case KVM_CAP_S390_IRQCHIP:
702 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
703 kvm->arch.use_irqchip = 1;
706 case KVM_CAP_S390_USER_SIGP:
707 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
708 kvm->arch.user_sigp = 1;
711 case KVM_CAP_S390_VECTOR_REGISTERS:
712 mutex_lock(&kvm->lock);
713 if (kvm->created_vcpus) {
715 } else if (MACHINE_HAS_VX) {
716 set_kvm_facility(kvm->arch.model.fac_mask, 129);
717 set_kvm_facility(kvm->arch.model.fac_list, 129);
718 if (test_facility(134)) {
719 set_kvm_facility(kvm->arch.model.fac_mask, 134);
720 set_kvm_facility(kvm->arch.model.fac_list, 134);
722 if (test_facility(135)) {
723 set_kvm_facility(kvm->arch.model.fac_mask, 135);
724 set_kvm_facility(kvm->arch.model.fac_list, 135);
726 if (test_facility(148)) {
727 set_kvm_facility(kvm->arch.model.fac_mask, 148);
728 set_kvm_facility(kvm->arch.model.fac_list, 148);
730 if (test_facility(152)) {
731 set_kvm_facility(kvm->arch.model.fac_mask, 152);
732 set_kvm_facility(kvm->arch.model.fac_list, 152);
734 if (test_facility(192)) {
735 set_kvm_facility(kvm->arch.model.fac_mask, 192);
736 set_kvm_facility(kvm->arch.model.fac_list, 192);
741 mutex_unlock(&kvm->lock);
742 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
743 r ? "(not available)" : "(success)");
745 case KVM_CAP_S390_RI:
747 mutex_lock(&kvm->lock);
748 if (kvm->created_vcpus) {
750 } else if (test_facility(64)) {
751 set_kvm_facility(kvm->arch.model.fac_mask, 64);
752 set_kvm_facility(kvm->arch.model.fac_list, 64);
755 mutex_unlock(&kvm->lock);
756 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
757 r ? "(not available)" : "(success)");
759 case KVM_CAP_S390_AIS:
760 mutex_lock(&kvm->lock);
761 if (kvm->created_vcpus) {
764 set_kvm_facility(kvm->arch.model.fac_mask, 72);
765 set_kvm_facility(kvm->arch.model.fac_list, 72);
768 mutex_unlock(&kvm->lock);
769 VM_EVENT(kvm, 3, "ENABLE: AIS %s",
770 r ? "(not available)" : "(success)");
772 case KVM_CAP_S390_GS:
774 mutex_lock(&kvm->lock);
775 if (kvm->created_vcpus) {
777 } else if (test_facility(133)) {
778 set_kvm_facility(kvm->arch.model.fac_mask, 133);
779 set_kvm_facility(kvm->arch.model.fac_list, 133);
782 mutex_unlock(&kvm->lock);
783 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
784 r ? "(not available)" : "(success)");
786 case KVM_CAP_S390_HPAGE_1M:
787 mutex_lock(&kvm->lock);
788 if (kvm->created_vcpus)
790 else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
794 mmap_write_lock(kvm->mm);
795 kvm->mm->context.allow_gmap_hpage_1m = 1;
796 mmap_write_unlock(kvm->mm);
798 * We might have to create fake 4k page
799 * tables. To avoid that the hardware works on
800 * stale PGSTEs, we emulate these instructions.
802 kvm->arch.use_skf = 0;
803 kvm->arch.use_pfmfi = 0;
805 mutex_unlock(&kvm->lock);
806 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
807 r ? "(not available)" : "(success)");
809 case KVM_CAP_S390_USER_STSI:
810 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
811 kvm->arch.user_stsi = 1;
814 case KVM_CAP_S390_USER_INSTR0:
815 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
816 kvm->arch.user_instr0 = 1;
817 icpt_operexc_on_all_vcpus(kvm);
827 static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
831 switch (attr->attr) {
832 case KVM_S390_VM_MEM_LIMIT_SIZE:
834 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
835 kvm->arch.mem_limit);
836 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
846 static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
850 switch (attr->attr) {
851 case KVM_S390_VM_MEM_ENABLE_CMMA:
856 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
857 mutex_lock(&kvm->lock);
858 if (kvm->created_vcpus)
860 else if (kvm->mm->context.allow_gmap_hpage_1m)
863 kvm->arch.use_cmma = 1;
864 /* Not compatible with cmma. */
865 kvm->arch.use_pfmfi = 0;
868 mutex_unlock(&kvm->lock);
870 case KVM_S390_VM_MEM_CLR_CMMA:
875 if (!kvm->arch.use_cmma)
878 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
879 mutex_lock(&kvm->lock);
880 idx = srcu_read_lock(&kvm->srcu);
881 s390_reset_cmma(kvm->arch.gmap->mm);
882 srcu_read_unlock(&kvm->srcu, idx);
883 mutex_unlock(&kvm->lock);
886 case KVM_S390_VM_MEM_LIMIT_SIZE: {
887 unsigned long new_limit;
889 if (kvm_is_ucontrol(kvm))
892 if (get_user(new_limit, (u64 __user *)attr->addr))
895 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
896 new_limit > kvm->arch.mem_limit)
902 /* gmap_create takes last usable address */
903 if (new_limit != KVM_S390_NO_MEM_LIMIT)
907 mutex_lock(&kvm->lock);
908 if (!kvm->created_vcpus) {
909 /* gmap_create will round the limit up */
910 struct gmap *new = gmap_create(current->mm, new_limit);
915 gmap_remove(kvm->arch.gmap);
917 kvm->arch.gmap = new;
921 mutex_unlock(&kvm->lock);
922 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
923 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
924 (void *) kvm->arch.gmap->asce);
934 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
936 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
938 struct kvm_vcpu *vcpu;
941 kvm_s390_vcpu_block_all(kvm);
943 kvm_for_each_vcpu(i, vcpu, kvm) {
944 kvm_s390_vcpu_crypto_setup(vcpu);
945 /* recreate the shadow crycb by leaving the VSIE handler */
946 kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
949 kvm_s390_vcpu_unblock_all(kvm);
952 static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
954 mutex_lock(&kvm->lock);
955 switch (attr->attr) {
956 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
957 if (!test_kvm_facility(kvm, 76)) {
958 mutex_unlock(&kvm->lock);
962 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
963 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
964 kvm->arch.crypto.aes_kw = 1;
965 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
967 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
968 if (!test_kvm_facility(kvm, 76)) {
969 mutex_unlock(&kvm->lock);
973 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
974 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
975 kvm->arch.crypto.dea_kw = 1;
976 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
978 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
979 if (!test_kvm_facility(kvm, 76)) {
980 mutex_unlock(&kvm->lock);
983 kvm->arch.crypto.aes_kw = 0;
984 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
985 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
986 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
988 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
989 if (!test_kvm_facility(kvm, 76)) {
990 mutex_unlock(&kvm->lock);
993 kvm->arch.crypto.dea_kw = 0;
994 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
995 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
996 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
998 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
999 if (!ap_instructions_available()) {
1000 mutex_unlock(&kvm->lock);
1003 kvm->arch.crypto.apie = 1;
1005 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
1006 if (!ap_instructions_available()) {
1007 mutex_unlock(&kvm->lock);
1010 kvm->arch.crypto.apie = 0;
1013 mutex_unlock(&kvm->lock);
1017 kvm_s390_vcpu_crypto_reset_all(kvm);
1018 mutex_unlock(&kvm->lock);
1022 static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
1025 struct kvm_vcpu *vcpu;
1027 kvm_for_each_vcpu(cx, vcpu, kvm)
1028 kvm_s390_sync_request(req, vcpu);
1032 * Must be called with kvm->srcu held to avoid races on memslots, and with
1033 * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
1035 static int kvm_s390_vm_start_migration(struct kvm *kvm)
1037 struct kvm_memory_slot *ms;
1038 struct kvm_memslots *slots;
1039 unsigned long ram_pages = 0;
1042 /* migration mode already enabled */
1043 if (kvm->arch.migration_mode)
1045 slots = kvm_memslots(kvm);
1046 if (!slots || !slots->used_slots)
1049 if (!kvm->arch.use_cmma) {
1050 kvm->arch.migration_mode = 1;
1053 /* mark all the pages in active slots as dirty */
1054 for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
1055 ms = slots->memslots + slotnr;
1056 if (!ms->dirty_bitmap)
1059 * The second half of the bitmap is only used on x86,
1060 * and would be wasted otherwise, so we put it to good
1061 * use here to keep track of the state of the storage
1064 memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1065 ram_pages += ms->npages;
1067 atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1068 kvm->arch.migration_mode = 1;
1069 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1074 * Must be called with kvm->slots_lock to avoid races with ourselves and
1075 * kvm_s390_vm_start_migration.
1077 static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1079 /* migration mode already disabled */
1080 if (!kvm->arch.migration_mode)
1082 kvm->arch.migration_mode = 0;
1083 if (kvm->arch.use_cmma)
1084 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1088 static int kvm_s390_vm_set_migration(struct kvm *kvm,
1089 struct kvm_device_attr *attr)
1093 mutex_lock(&kvm->slots_lock);
1094 switch (attr->attr) {
1095 case KVM_S390_VM_MIGRATION_START:
1096 res = kvm_s390_vm_start_migration(kvm);
1098 case KVM_S390_VM_MIGRATION_STOP:
1099 res = kvm_s390_vm_stop_migration(kvm);
1104 mutex_unlock(&kvm->slots_lock);
1109 static int kvm_s390_vm_get_migration(struct kvm *kvm,
1110 struct kvm_device_attr *attr)
1112 u64 mig = kvm->arch.migration_mode;
1114 if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1117 if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1122 static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1124 struct kvm_s390_vm_tod_clock gtod;
1126 if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod)))
1129 if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
1131 kvm_s390_set_tod_clock(kvm, >od);
1133 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
1134 gtod.epoch_idx, gtod.tod);
1139 static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1143 if (copy_from_user(>od_high, (void __user *)attr->addr,
1149 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
1154 static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1156 struct kvm_s390_vm_tod_clock gtod = { 0 };
1158 if (copy_from_user(>od.tod, (void __user *)attr->addr,
1162 kvm_s390_set_tod_clock(kvm, >od);
1163 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
1167 static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1174 switch (attr->attr) {
1175 case KVM_S390_VM_TOD_EXT:
1176 ret = kvm_s390_set_tod_ext(kvm, attr);
1178 case KVM_S390_VM_TOD_HIGH:
1179 ret = kvm_s390_set_tod_high(kvm, attr);
1181 case KVM_S390_VM_TOD_LOW:
1182 ret = kvm_s390_set_tod_low(kvm, attr);
1191 static void kvm_s390_get_tod_clock(struct kvm *kvm,
1192 struct kvm_s390_vm_tod_clock *gtod)
1194 union tod_clock clk;
1198 store_tod_clock_ext(&clk);
1200 gtod->tod = clk.tod + kvm->arch.epoch;
1201 gtod->epoch_idx = 0;
1202 if (test_kvm_facility(kvm, 139)) {
1203 gtod->epoch_idx = clk.ei + kvm->arch.epdx;
1204 if (gtod->tod < clk.tod)
1205 gtod->epoch_idx += 1;
1211 static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1213 struct kvm_s390_vm_tod_clock gtod;
1215 memset(>od, 0, sizeof(gtod));
1216 kvm_s390_get_tod_clock(kvm, >od);
1217 if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
1220 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1221 gtod.epoch_idx, gtod.tod);
1225 static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1229 if (copy_to_user((void __user *)attr->addr, >od_high,
1232 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
1237 static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1241 gtod = kvm_s390_get_tod_clock_fast(kvm);
1242 if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
1244 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
1249 static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1256 switch (attr->attr) {
1257 case KVM_S390_VM_TOD_EXT:
1258 ret = kvm_s390_get_tod_ext(kvm, attr);
1260 case KVM_S390_VM_TOD_HIGH:
1261 ret = kvm_s390_get_tod_high(kvm, attr);
1263 case KVM_S390_VM_TOD_LOW:
1264 ret = kvm_s390_get_tod_low(kvm, attr);
1273 static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1275 struct kvm_s390_vm_cpu_processor *proc;
1276 u16 lowest_ibc, unblocked_ibc;
1279 mutex_lock(&kvm->lock);
1280 if (kvm->created_vcpus) {
1284 proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1289 if (!copy_from_user(proc, (void __user *)attr->addr,
1291 kvm->arch.model.cpuid = proc->cpuid;
1292 lowest_ibc = sclp.ibc >> 16 & 0xfff;
1293 unblocked_ibc = sclp.ibc & 0xfff;
1294 if (lowest_ibc && proc->ibc) {
1295 if (proc->ibc > unblocked_ibc)
1296 kvm->arch.model.ibc = unblocked_ibc;
1297 else if (proc->ibc < lowest_ibc)
1298 kvm->arch.model.ibc = lowest_ibc;
1300 kvm->arch.model.ibc = proc->ibc;
1302 memcpy(kvm->arch.model.fac_list, proc->fac_list,
1303 S390_ARCH_FAC_LIST_SIZE_BYTE);
1304 VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1305 kvm->arch.model.ibc,
1306 kvm->arch.model.cpuid);
1307 VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1308 kvm->arch.model.fac_list[0],
1309 kvm->arch.model.fac_list[1],
1310 kvm->arch.model.fac_list[2]);
1315 mutex_unlock(&kvm->lock);
1319 static int kvm_s390_set_processor_feat(struct kvm *kvm,
1320 struct kvm_device_attr *attr)
1322 struct kvm_s390_vm_cpu_feat data;
1324 if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
1326 if (!bitmap_subset((unsigned long *) data.feat,
1327 kvm_s390_available_cpu_feat,
1328 KVM_S390_VM_CPU_FEAT_NR_BITS))
1331 mutex_lock(&kvm->lock);
1332 if (kvm->created_vcpus) {
1333 mutex_unlock(&kvm->lock);
1336 bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
1337 KVM_S390_VM_CPU_FEAT_NR_BITS);
1338 mutex_unlock(&kvm->lock);
1339 VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1346 static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
1347 struct kvm_device_attr *attr)
1349 mutex_lock(&kvm->lock);
1350 if (kvm->created_vcpus) {
1351 mutex_unlock(&kvm->lock);
1355 if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1356 sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1357 mutex_unlock(&kvm->lock);
1360 mutex_unlock(&kvm->lock);
1362 VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1363 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1364 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1365 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1366 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1367 VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1368 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1369 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1370 VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1371 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1372 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1373 VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1374 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1375 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1376 VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
1377 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1378 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1379 VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1380 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1381 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1382 VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1383 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1384 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1385 VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1386 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1387 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1388 VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1389 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1390 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1391 VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1392 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1393 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1394 VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1395 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1396 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1397 VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1398 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1399 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1400 VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1401 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1402 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1403 VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1404 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1405 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1406 VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1407 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1408 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1409 VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1410 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1411 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1412 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1413 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1414 VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1415 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1416 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1417 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1418 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1423 static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1427 switch (attr->attr) {
1428 case KVM_S390_VM_CPU_PROCESSOR:
1429 ret = kvm_s390_set_processor(kvm, attr);
1431 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1432 ret = kvm_s390_set_processor_feat(kvm, attr);
1434 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1435 ret = kvm_s390_set_processor_subfunc(kvm, attr);
1441 static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1443 struct kvm_s390_vm_cpu_processor *proc;
1446 proc = kzalloc(sizeof(*proc), GFP_KERNEL_ACCOUNT);
1451 proc->cpuid = kvm->arch.model.cpuid;
1452 proc->ibc = kvm->arch.model.ibc;
1453 memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1454 S390_ARCH_FAC_LIST_SIZE_BYTE);
1455 VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1456 kvm->arch.model.ibc,
1457 kvm->arch.model.cpuid);
1458 VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1459 kvm->arch.model.fac_list[0],
1460 kvm->arch.model.fac_list[1],
1461 kvm->arch.model.fac_list[2]);
1462 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1469 static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1471 struct kvm_s390_vm_cpu_machine *mach;
1474 mach = kzalloc(sizeof(*mach), GFP_KERNEL_ACCOUNT);
1479 get_cpu_id((struct cpuid *) &mach->cpuid);
1480 mach->ibc = sclp.ibc;
1481 memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1482 S390_ARCH_FAC_LIST_SIZE_BYTE);
1483 memcpy((unsigned long *)&mach->fac_list, stfle_fac_list,
1484 sizeof(stfle_fac_list));
1485 VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1486 kvm->arch.model.ibc,
1487 kvm->arch.model.cpuid);
1488 VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1492 VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1496 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1503 static int kvm_s390_get_processor_feat(struct kvm *kvm,
1504 struct kvm_device_attr *attr)
1506 struct kvm_s390_vm_cpu_feat data;
1508 bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
1509 KVM_S390_VM_CPU_FEAT_NR_BITS);
1510 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1512 VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1519 static int kvm_s390_get_machine_feat(struct kvm *kvm,
1520 struct kvm_device_attr *attr)
1522 struct kvm_s390_vm_cpu_feat data;
1524 bitmap_copy((unsigned long *) data.feat,
1525 kvm_s390_available_cpu_feat,
1526 KVM_S390_VM_CPU_FEAT_NR_BITS);
1527 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1529 VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1536 static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
1537 struct kvm_device_attr *attr)
1539 if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1540 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1543 VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1544 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1545 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1546 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1547 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1548 VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1549 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1550 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1551 VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1552 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1553 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1554 VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1555 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1556 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1557 VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
1558 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1559 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1560 VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1561 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1562 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1563 VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1564 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1565 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1566 VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1567 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1568 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1569 VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1570 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1571 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1572 VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1573 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1574 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1575 VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1576 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1577 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1578 VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1579 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1580 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1581 VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1582 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1583 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1584 VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1585 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1586 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1587 VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1588 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1589 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1590 VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1591 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1592 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1593 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1594 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1595 VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1596 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1597 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1598 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1599 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1604 static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
1605 struct kvm_device_attr *attr)
1607 if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
1608 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1611 VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1612 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
1613 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
1614 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
1615 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
1616 VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
1617 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
1618 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
1619 VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
1620 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
1621 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
1622 VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
1623 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
1624 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
1625 VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
1626 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
1627 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
1628 VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
1629 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
1630 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
1631 VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
1632 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
1633 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
1634 VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
1635 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
1636 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
1637 VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
1638 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
1639 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
1640 VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
1641 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
1642 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
1643 VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
1644 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
1645 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
1646 VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
1647 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
1648 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
1649 VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
1650 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
1651 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
1652 VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
1653 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
1654 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
1655 VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx",
1656 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
1657 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1658 VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1659 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1660 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1661 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1662 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
1663 VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1664 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
1665 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
1666 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
1667 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
1672 static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1676 switch (attr->attr) {
1677 case KVM_S390_VM_CPU_PROCESSOR:
1678 ret = kvm_s390_get_processor(kvm, attr);
1680 case KVM_S390_VM_CPU_MACHINE:
1681 ret = kvm_s390_get_machine(kvm, attr);
1683 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1684 ret = kvm_s390_get_processor_feat(kvm, attr);
1686 case KVM_S390_VM_CPU_MACHINE_FEAT:
1687 ret = kvm_s390_get_machine_feat(kvm, attr);
1689 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1690 ret = kvm_s390_get_processor_subfunc(kvm, attr);
1692 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1693 ret = kvm_s390_get_machine_subfunc(kvm, attr);
1699 static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1703 switch (attr->group) {
1704 case KVM_S390_VM_MEM_CTRL:
1705 ret = kvm_s390_set_mem_control(kvm, attr);
1707 case KVM_S390_VM_TOD:
1708 ret = kvm_s390_set_tod(kvm, attr);
1710 case KVM_S390_VM_CPU_MODEL:
1711 ret = kvm_s390_set_cpu_model(kvm, attr);
1713 case KVM_S390_VM_CRYPTO:
1714 ret = kvm_s390_vm_set_crypto(kvm, attr);
1716 case KVM_S390_VM_MIGRATION:
1717 ret = kvm_s390_vm_set_migration(kvm, attr);
1727 static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1731 switch (attr->group) {
1732 case KVM_S390_VM_MEM_CTRL:
1733 ret = kvm_s390_get_mem_control(kvm, attr);
1735 case KVM_S390_VM_TOD:
1736 ret = kvm_s390_get_tod(kvm, attr);
1738 case KVM_S390_VM_CPU_MODEL:
1739 ret = kvm_s390_get_cpu_model(kvm, attr);
1741 case KVM_S390_VM_MIGRATION:
1742 ret = kvm_s390_vm_get_migration(kvm, attr);
1752 static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1756 switch (attr->group) {
1757 case KVM_S390_VM_MEM_CTRL:
1758 switch (attr->attr) {
1759 case KVM_S390_VM_MEM_ENABLE_CMMA:
1760 case KVM_S390_VM_MEM_CLR_CMMA:
1761 ret = sclp.has_cmma ? 0 : -ENXIO;
1763 case KVM_S390_VM_MEM_LIMIT_SIZE:
1771 case KVM_S390_VM_TOD:
1772 switch (attr->attr) {
1773 case KVM_S390_VM_TOD_LOW:
1774 case KVM_S390_VM_TOD_HIGH:
1782 case KVM_S390_VM_CPU_MODEL:
1783 switch (attr->attr) {
1784 case KVM_S390_VM_CPU_PROCESSOR:
1785 case KVM_S390_VM_CPU_MACHINE:
1786 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1787 case KVM_S390_VM_CPU_MACHINE_FEAT:
1788 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1789 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1797 case KVM_S390_VM_CRYPTO:
1798 switch (attr->attr) {
1799 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1800 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1801 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1802 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1805 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
1806 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
1807 ret = ap_instructions_available() ? 0 : -ENXIO;
1814 case KVM_S390_VM_MIGRATION:
1825 static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1829 int srcu_idx, i, r = 0;
1831 if (args->flags != 0)
1834 /* Is this guest using storage keys? */
1835 if (!mm_uses_skeys(current->mm))
1836 return KVM_S390_GET_SKEYS_NONE;
1838 /* Enforce sane limit on memory allocation */
1839 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1842 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
1846 mmap_read_lock(current->mm);
1847 srcu_idx = srcu_read_lock(&kvm->srcu);
1848 for (i = 0; i < args->count; i++) {
1849 hva = gfn_to_hva(kvm, args->start_gfn + i);
1850 if (kvm_is_error_hva(hva)) {
1855 r = get_guest_storage_key(current->mm, hva, &keys[i]);
1859 srcu_read_unlock(&kvm->srcu, srcu_idx);
1860 mmap_read_unlock(current->mm);
1863 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
1864 sizeof(uint8_t) * args->count);
1873 static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1877 int srcu_idx, i, r = 0;
1880 if (args->flags != 0)
1883 /* Enforce sane limit on memory allocation */
1884 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1887 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL_ACCOUNT);
1891 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
1892 sizeof(uint8_t) * args->count);
1898 /* Enable storage key handling for the guest */
1899 r = s390_enable_skey();
1904 mmap_read_lock(current->mm);
1905 srcu_idx = srcu_read_lock(&kvm->srcu);
1906 while (i < args->count) {
1908 hva = gfn_to_hva(kvm, args->start_gfn + i);
1909 if (kvm_is_error_hva(hva)) {
1914 /* Lowest order bit is reserved */
1915 if (keys[i] & 0x01) {
1920 r = set_guest_storage_key(current->mm, hva, keys[i], 0);
1922 r = fixup_user_fault(current->mm, hva,
1923 FAULT_FLAG_WRITE, &unlocked);
1930 srcu_read_unlock(&kvm->srcu, srcu_idx);
1931 mmap_read_unlock(current->mm);
1938 * Base address and length must be sent at the start of each block, therefore
1939 * it's cheaper to send some clean data, as long as it's less than the size of
1942 #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
1943 /* for consistency */
1944 #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
1947 * Similar to gfn_to_memslot, but returns the index of a memslot also when the
1948 * address falls in a hole. In that case the index of one of the memslots
1949 * bordering the hole is returned.
1951 static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn)
1953 int start = 0, end = slots->used_slots;
1954 int slot = atomic_read(&slots->last_used_slot);
1955 struct kvm_memory_slot *memslots = slots->memslots;
1957 if (gfn >= memslots[slot].base_gfn &&
1958 gfn < memslots[slot].base_gfn + memslots[slot].npages)
1961 while (start < end) {
1962 slot = start + (end - start) / 2;
1964 if (gfn >= memslots[slot].base_gfn)
1970 if (start >= slots->used_slots)
1971 return slots->used_slots - 1;
1973 if (gfn >= memslots[start].base_gfn &&
1974 gfn < memslots[start].base_gfn + memslots[start].npages) {
1975 atomic_set(&slots->last_used_slot, start);
1981 static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1982 u8 *res, unsigned long bufsize)
1984 unsigned long pgstev, hva, cur_gfn = args->start_gfn;
1987 while (args->count < bufsize) {
1988 hva = gfn_to_hva(kvm, cur_gfn);
1990 * We return an error if the first value was invalid, but we
1991 * return successfully if at least one value was copied.
1993 if (kvm_is_error_hva(hva))
1994 return args->count ? 0 : -EFAULT;
1995 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
1997 res[args->count++] = (pgstev >> 24) & 0x43;
2004 static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
2005 unsigned long cur_gfn)
2007 int slotidx = gfn_to_memslot_approx(slots, cur_gfn);
2008 struct kvm_memory_slot *ms = slots->memslots + slotidx;
2009 unsigned long ofs = cur_gfn - ms->base_gfn;
2011 if (ms->base_gfn + ms->npages <= cur_gfn) {
2013 /* If we are above the highest slot, wrap around */
2015 slotidx = slots->used_slots - 1;
2017 ms = slots->memslots + slotidx;
2020 ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
2021 while ((slotidx > 0) && (ofs >= ms->npages)) {
2023 ms = slots->memslots + slotidx;
2024 ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0);
2026 return ms->base_gfn + ofs;
2029 static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2030 u8 *res, unsigned long bufsize)
2032 unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2033 struct kvm_memslots *slots = kvm_memslots(kvm);
2034 struct kvm_memory_slot *ms;
2036 if (unlikely(!slots->used_slots))
2039 cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2040 ms = gfn_to_memslot(kvm, cur_gfn);
2042 args->start_gfn = cur_gfn;
2045 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2046 mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages;
2048 while (args->count < bufsize) {
2049 hva = gfn_to_hva(kvm, cur_gfn);
2050 if (kvm_is_error_hva(hva))
2052 /* Decrement only if we actually flipped the bit to 0 */
2053 if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2054 atomic64_dec(&kvm->arch.cmma_dirty_pages);
2055 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2057 /* Save the value */
2058 res[args->count++] = (pgstev >> 24) & 0x43;
2059 /* If the next bit is too far away, stop. */
2060 if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2062 /* If we reached the previous "next", find the next one */
2063 if (cur_gfn == next_gfn)
2064 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2065 /* Reached the end of memory or of the buffer, stop */
2066 if ((next_gfn >= mem_end) ||
2067 (next_gfn - args->start_gfn >= bufsize))
2070 /* Reached the end of the current memslot, take the next one. */
2071 if (cur_gfn - ms->base_gfn >= ms->npages) {
2072 ms = gfn_to_memslot(kvm, cur_gfn);
2081 * This function searches for the next page with dirty CMMA attributes, and
2082 * saves the attributes in the buffer up to either the end of the buffer or
2083 * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
2084 * no trailing clean bytes are saved.
2085 * In case no dirty bits were found, or if CMMA was not enabled or used, the
2086 * output buffer will indicate 0 as length.
2088 static int kvm_s390_get_cmma_bits(struct kvm *kvm,
2089 struct kvm_s390_cmma_log *args)
2091 unsigned long bufsize;
2092 int srcu_idx, peek, ret;
2095 if (!kvm->arch.use_cmma)
2097 /* Invalid/unsupported flags were specified */
2098 if (args->flags & ~KVM_S390_CMMA_PEEK)
2100 /* Migration mode query, and we are not doing a migration */
2101 peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2102 if (!peek && !kvm->arch.migration_mode)
2104 /* CMMA is disabled or was not used, or the buffer has length zero */
2105 bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2106 if (!bufsize || !kvm->mm->context.uses_cmm) {
2107 memset(args, 0, sizeof(*args));
2110 /* We are not peeking, and there are no dirty pages */
2111 if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
2112 memset(args, 0, sizeof(*args));
2116 values = vmalloc(bufsize);
2120 mmap_read_lock(kvm->mm);
2121 srcu_idx = srcu_read_lock(&kvm->srcu);
2123 ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2125 ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
2126 srcu_read_unlock(&kvm->srcu, srcu_idx);
2127 mmap_read_unlock(kvm->mm);
2129 if (kvm->arch.migration_mode)
2130 args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2132 args->remaining = 0;
2134 if (copy_to_user((void __user *)args->values, values, args->count))
2142 * This function sets the CMMA attributes for the given pages. If the input
2143 * buffer has zero length, no action is taken, otherwise the attributes are
2144 * set and the mm->context.uses_cmm flag is set.
2146 static int kvm_s390_set_cmma_bits(struct kvm *kvm,
2147 const struct kvm_s390_cmma_log *args)
2149 unsigned long hva, mask, pgstev, i;
2151 int srcu_idx, r = 0;
2155 if (!kvm->arch.use_cmma)
2157 /* invalid/unsupported flags */
2158 if (args->flags != 0)
2160 /* Enforce sane limit on memory allocation */
2161 if (args->count > KVM_S390_CMMA_SIZE_MAX)
2164 if (args->count == 0)
2167 bits = vmalloc(array_size(sizeof(*bits), args->count));
2171 r = copy_from_user(bits, (void __user *)args->values, args->count);
2177 mmap_read_lock(kvm->mm);
2178 srcu_idx = srcu_read_lock(&kvm->srcu);
2179 for (i = 0; i < args->count; i++) {
2180 hva = gfn_to_hva(kvm, args->start_gfn + i);
2181 if (kvm_is_error_hva(hva)) {
2187 pgstev = pgstev << 24;
2188 mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
2189 set_pgste_bits(kvm->mm, hva, mask, pgstev);
2191 srcu_read_unlock(&kvm->srcu, srcu_idx);
2192 mmap_read_unlock(kvm->mm);
2194 if (!kvm->mm->context.uses_cmm) {
2195 mmap_write_lock(kvm->mm);
2196 kvm->mm->context.uses_cmm = 1;
2197 mmap_write_unlock(kvm->mm);
2204 static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp)
2206 struct kvm_vcpu *vcpu;
2212 * We ignore failures and try to destroy as many CPUs as possible.
2213 * At the same time we must not free the assigned resources when
2214 * this fails, as the ultravisor has still access to that memory.
2215 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
2217 * We want to return the first failure rc and rrc, though.
2219 kvm_for_each_vcpu(i, vcpu, kvm) {
2220 mutex_lock(&vcpu->mutex);
2221 if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) {
2226 mutex_unlock(&vcpu->mutex);
2231 static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
2236 struct kvm_vcpu *vcpu;
2238 kvm_for_each_vcpu(i, vcpu, kvm) {
2239 mutex_lock(&vcpu->mutex);
2240 r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
2241 mutex_unlock(&vcpu->mutex);
2246 kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
2250 static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
2254 void __user *argp = (void __user *)cmd->data;
2257 case KVM_PV_ENABLE: {
2259 if (kvm_s390_pv_is_protected(kvm))
2263 * FMT 4 SIE needs esca. As we never switch back to bsca from
2264 * esca, we need no cleanup in the error cases below
2266 r = sca_switch_to_extended(kvm);
2270 mmap_write_lock(current->mm);
2271 r = gmap_mark_unmergeable();
2272 mmap_write_unlock(current->mm);
2276 r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
2280 r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
2282 kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
2284 /* we need to block service interrupts from now on */
2285 set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2288 case KVM_PV_DISABLE: {
2290 if (!kvm_s390_pv_is_protected(kvm))
2293 r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2295 * If a CPU could not be destroyed, destroy VM will also fail.
2296 * There is no point in trying to destroy it. Instead return
2297 * the rc and rrc from the first CPU that failed destroying.
2301 r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc);
2303 /* no need to block service interrupts any more */
2304 clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2307 case KVM_PV_SET_SEC_PARMS: {
2308 struct kvm_s390_pv_sec_parm parms = {};
2312 if (!kvm_s390_pv_is_protected(kvm))
2316 if (copy_from_user(&parms, argp, sizeof(parms)))
2319 /* Currently restricted to 8KB */
2321 if (parms.length > PAGE_SIZE * 2)
2325 hdr = vmalloc(parms.length);
2330 if (!copy_from_user(hdr, (void __user *)parms.origin,
2332 r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
2333 &cmd->rc, &cmd->rrc);
2338 case KVM_PV_UNPACK: {
2339 struct kvm_s390_pv_unp unp = {};
2342 if (!kvm_s390_pv_is_protected(kvm) || !mm_is_protected(kvm->mm))
2346 if (copy_from_user(&unp, argp, sizeof(unp)))
2349 r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
2350 &cmd->rc, &cmd->rrc);
2353 case KVM_PV_VERIFY: {
2355 if (!kvm_s390_pv_is_protected(kvm))
2358 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2359 UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
2360 KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
2364 case KVM_PV_PREP_RESET: {
2366 if (!kvm_s390_pv_is_protected(kvm))
2369 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2370 UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2371 KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2375 case KVM_PV_UNSHARE_ALL: {
2377 if (!kvm_s390_pv_is_protected(kvm))
2380 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2381 UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2382 KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2392 long kvm_arch_vm_ioctl(struct file *filp,
2393 unsigned int ioctl, unsigned long arg)
2395 struct kvm *kvm = filp->private_data;
2396 void __user *argp = (void __user *)arg;
2397 struct kvm_device_attr attr;
2401 case KVM_S390_INTERRUPT: {
2402 struct kvm_s390_interrupt s390int;
2405 if (copy_from_user(&s390int, argp, sizeof(s390int)))
2407 r = kvm_s390_inject_vm(kvm, &s390int);
2410 case KVM_CREATE_IRQCHIP: {
2411 struct kvm_irq_routing_entry routing;
2414 if (kvm->arch.use_irqchip) {
2415 /* Set up dummy routing. */
2416 memset(&routing, 0, sizeof(routing));
2417 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
2421 case KVM_SET_DEVICE_ATTR: {
2423 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2425 r = kvm_s390_vm_set_attr(kvm, &attr);
2428 case KVM_GET_DEVICE_ATTR: {
2430 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2432 r = kvm_s390_vm_get_attr(kvm, &attr);
2435 case KVM_HAS_DEVICE_ATTR: {
2437 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2439 r = kvm_s390_vm_has_attr(kvm, &attr);
2442 case KVM_S390_GET_SKEYS: {
2443 struct kvm_s390_skeys args;
2446 if (copy_from_user(&args, argp,
2447 sizeof(struct kvm_s390_skeys)))
2449 r = kvm_s390_get_skeys(kvm, &args);
2452 case KVM_S390_SET_SKEYS: {
2453 struct kvm_s390_skeys args;
2456 if (copy_from_user(&args, argp,
2457 sizeof(struct kvm_s390_skeys)))
2459 r = kvm_s390_set_skeys(kvm, &args);
2462 case KVM_S390_GET_CMMA_BITS: {
2463 struct kvm_s390_cmma_log args;
2466 if (copy_from_user(&args, argp, sizeof(args)))
2468 mutex_lock(&kvm->slots_lock);
2469 r = kvm_s390_get_cmma_bits(kvm, &args);
2470 mutex_unlock(&kvm->slots_lock);
2472 r = copy_to_user(argp, &args, sizeof(args));
2478 case KVM_S390_SET_CMMA_BITS: {
2479 struct kvm_s390_cmma_log args;
2482 if (copy_from_user(&args, argp, sizeof(args)))
2484 mutex_lock(&kvm->slots_lock);
2485 r = kvm_s390_set_cmma_bits(kvm, &args);
2486 mutex_unlock(&kvm->slots_lock);
2489 case KVM_S390_PV_COMMAND: {
2490 struct kvm_pv_cmd args;
2492 /* protvirt means user cpu state */
2493 kvm_s390_set_user_cpu_state_ctrl(kvm);
2495 if (!is_prot_virt_host()) {
2499 if (copy_from_user(&args, argp, sizeof(args))) {
2507 mutex_lock(&kvm->lock);
2508 r = kvm_s390_handle_pv(kvm, &args);
2509 mutex_unlock(&kvm->lock);
2510 if (copy_to_user(argp, &args, sizeof(args))) {
2523 static int kvm_s390_apxa_installed(void)
2525 struct ap_config_info info;
2527 if (ap_instructions_available()) {
2528 if (ap_qci(&info) == 0)
2536 * The format of the crypto control block (CRYCB) is specified in the 3 low
2537 * order bits of the CRYCB designation (CRYCBD) field as follows:
2538 * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2539 * AP extended addressing (APXA) facility are installed.
2540 * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2541 * Format 2: Both the APXA and MSAX3 facilities are installed
2543 static void kvm_s390_set_crycb_format(struct kvm *kvm)
2545 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
2547 /* Clear the CRYCB format bits - i.e., set format 0 by default */
2548 kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2550 /* Check whether MSAX3 is installed */
2551 if (!test_kvm_facility(kvm, 76))
2554 if (kvm_s390_apxa_installed())
2555 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
2557 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
2561 * kvm_arch_crypto_set_masks
2563 * @kvm: pointer to the target guest's KVM struct containing the crypto masks
2565 * @apm: the mask identifying the accessible AP adapters
2566 * @aqm: the mask identifying the accessible AP domains
2567 * @adm: the mask identifying the accessible AP control domains
2569 * Set the masks that identify the adapters, domains and control domains to
2570 * which the KVM guest is granted access.
2572 * Note: The kvm->lock mutex must be locked by the caller before invoking this
2575 void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
2576 unsigned long *aqm, unsigned long *adm)
2578 struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
2580 kvm_s390_vcpu_block_all(kvm);
2582 switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
2583 case CRYCB_FORMAT2: /* APCB1 use 256 bits */
2584 memcpy(crycb->apcb1.apm, apm, 32);
2585 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
2586 apm[0], apm[1], apm[2], apm[3]);
2587 memcpy(crycb->apcb1.aqm, aqm, 32);
2588 VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
2589 aqm[0], aqm[1], aqm[2], aqm[3]);
2590 memcpy(crycb->apcb1.adm, adm, 32);
2591 VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
2592 adm[0], adm[1], adm[2], adm[3]);
2595 case CRYCB_FORMAT0: /* Fall through both use APCB0 */
2596 memcpy(crycb->apcb0.apm, apm, 8);
2597 memcpy(crycb->apcb0.aqm, aqm, 2);
2598 memcpy(crycb->apcb0.adm, adm, 2);
2599 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
2600 apm[0], *((unsigned short *)aqm),
2601 *((unsigned short *)adm));
2603 default: /* Can not happen */
2607 /* recreate the shadow crycb for each vcpu */
2608 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2609 kvm_s390_vcpu_unblock_all(kvm);
2611 EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
2614 * kvm_arch_crypto_clear_masks
2616 * @kvm: pointer to the target guest's KVM struct containing the crypto masks
2619 * Clear the masks that identify the adapters, domains and control domains to
2620 * which the KVM guest is granted access.
2622 * Note: The kvm->lock mutex must be locked by the caller before invoking this
2625 void kvm_arch_crypto_clear_masks(struct kvm *kvm)
2627 kvm_s390_vcpu_block_all(kvm);
2629 memset(&kvm->arch.crypto.crycb->apcb0, 0,
2630 sizeof(kvm->arch.crypto.crycb->apcb0));
2631 memset(&kvm->arch.crypto.crycb->apcb1, 0,
2632 sizeof(kvm->arch.crypto.crycb->apcb1));
2634 VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
2635 /* recreate the shadow crycb for each vcpu */
2636 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2637 kvm_s390_vcpu_unblock_all(kvm);
2639 EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
2641 static u64 kvm_s390_get_initial_cpuid(void)
2646 cpuid.version = 0xff;
2647 return *((u64 *) &cpuid);
2650 static void kvm_s390_crypto_init(struct kvm *kvm)
2652 kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
2653 kvm_s390_set_crycb_format(kvm);
2654 init_rwsem(&kvm->arch.crypto.pqap_hook_rwsem);
2656 if (!test_kvm_facility(kvm, 76))
2659 /* Enable AES/DEA protected key functions by default */
2660 kvm->arch.crypto.aes_kw = 1;
2661 kvm->arch.crypto.dea_kw = 1;
2662 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
2663 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
2664 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
2665 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
2668 static void sca_dispose(struct kvm *kvm)
2670 if (kvm->arch.use_esca)
2671 free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
2673 free_page((unsigned long)(kvm->arch.sca));
2674 kvm->arch.sca = NULL;
2677 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
2679 gfp_t alloc_flags = GFP_KERNEL_ACCOUNT;
2681 char debug_name[16];
2682 static unsigned long sca_offset;
2685 #ifdef CONFIG_KVM_S390_UCONTROL
2686 if (type & ~KVM_VM_S390_UCONTROL)
2688 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
2695 rc = s390_enable_sie();
2701 if (!sclp.has_64bscao)
2702 alloc_flags |= GFP_DMA;
2703 rwlock_init(&kvm->arch.sca_lock);
2704 /* start with basic SCA */
2705 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
2708 mutex_lock(&kvm_lock);
2710 if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2712 kvm->arch.sca = (struct bsca_block *)
2713 ((char *) kvm->arch.sca + sca_offset);
2714 mutex_unlock(&kvm_lock);
2716 sprintf(debug_name, "kvm-%u", current->pid);
2718 kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2722 BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
2723 kvm->arch.sie_page2 =
2724 (struct sie_page2 *) get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
2725 if (!kvm->arch.sie_page2)
2728 kvm->arch.sie_page2->kvm = kvm;
2729 kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2731 for (i = 0; i < kvm_s390_fac_size(); i++) {
2732 kvm->arch.model.fac_mask[i] = stfle_fac_list[i] &
2733 (kvm_s390_fac_base[i] |
2734 kvm_s390_fac_ext[i]);
2735 kvm->arch.model.fac_list[i] = stfle_fac_list[i] &
2736 kvm_s390_fac_base[i];
2738 kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
2740 /* we are always in czam mode - even on pre z14 machines */
2741 set_kvm_facility(kvm->arch.model.fac_mask, 138);
2742 set_kvm_facility(kvm->arch.model.fac_list, 138);
2743 /* we emulate STHYI in kvm */
2744 set_kvm_facility(kvm->arch.model.fac_mask, 74);
2745 set_kvm_facility(kvm->arch.model.fac_list, 74);
2746 if (MACHINE_HAS_TLB_GUEST) {
2747 set_kvm_facility(kvm->arch.model.fac_mask, 147);
2748 set_kvm_facility(kvm->arch.model.fac_list, 147);
2751 if (css_general_characteristics.aiv && test_facility(65))
2752 set_kvm_facility(kvm->arch.model.fac_mask, 65);
2754 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
2755 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
2757 kvm_s390_crypto_init(kvm);
2759 mutex_init(&kvm->arch.float_int.ais_lock);
2760 spin_lock_init(&kvm->arch.float_int.lock);
2761 for (i = 0; i < FIRQ_LIST_COUNT; i++)
2762 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
2763 init_waitqueue_head(&kvm->arch.ipte_wq);
2764 mutex_init(&kvm->arch.ipte_mutex);
2766 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
2767 VM_EVENT(kvm, 3, "vm created with type %lu", type);
2769 if (type & KVM_VM_S390_UCONTROL) {
2770 kvm->arch.gmap = NULL;
2771 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2773 if (sclp.hamax == U64_MAX)
2774 kvm->arch.mem_limit = TASK_SIZE_MAX;
2776 kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
2778 kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2779 if (!kvm->arch.gmap)
2781 kvm->arch.gmap->private = kvm;
2782 kvm->arch.gmap->pfault_enabled = 0;
2785 kvm->arch.use_pfmfi = sclp.has_pfmfi;
2786 kvm->arch.use_skf = sclp.has_skey;
2787 spin_lock_init(&kvm->arch.start_stop_lock);
2788 kvm_s390_vsie_init(kvm);
2790 kvm_s390_gisa_init(kvm);
2791 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
2795 free_page((unsigned long)kvm->arch.sie_page2);
2796 debug_unregister(kvm->arch.dbf);
2798 KVM_EVENT(3, "creation of vm failed: %d", rc);
2802 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2806 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2807 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
2808 kvm_s390_clear_local_irqs(vcpu);
2809 kvm_clear_async_pf_completion_queue(vcpu);
2810 if (!kvm_is_ucontrol(vcpu->kvm))
2813 if (kvm_is_ucontrol(vcpu->kvm))
2814 gmap_remove(vcpu->arch.gmap);
2816 if (vcpu->kvm->arch.use_cmma)
2817 kvm_s390_vcpu_unsetup_cmma(vcpu);
2818 /* We can not hold the vcpu mutex here, we are already dying */
2819 if (kvm_s390_pv_cpu_get_handle(vcpu))
2820 kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
2821 free_page((unsigned long)(vcpu->arch.sie_block));
2824 static void kvm_free_vcpus(struct kvm *kvm)
2827 struct kvm_vcpu *vcpu;
2829 kvm_for_each_vcpu(i, vcpu, kvm)
2830 kvm_vcpu_destroy(vcpu);
2832 mutex_lock(&kvm->lock);
2833 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2834 kvm->vcpus[i] = NULL;
2836 atomic_set(&kvm->online_vcpus, 0);
2837 mutex_unlock(&kvm->lock);
2840 void kvm_arch_destroy_vm(struct kvm *kvm)
2844 kvm_free_vcpus(kvm);
2846 kvm_s390_gisa_destroy(kvm);
2848 * We are already at the end of life and kvm->lock is not taken.
2849 * This is ok as the file descriptor is closed by now and nobody
2850 * can mess with the pv state. To avoid lockdep_assert_held from
2851 * complaining we do not use kvm_s390_pv_is_protected.
2853 if (kvm_s390_pv_get_handle(kvm))
2854 kvm_s390_pv_deinit_vm(kvm, &rc, &rrc);
2855 debug_unregister(kvm->arch.dbf);
2856 free_page((unsigned long)kvm->arch.sie_page2);
2857 if (!kvm_is_ucontrol(kvm))
2858 gmap_remove(kvm->arch.gmap);
2859 kvm_s390_destroy_adapters(kvm);
2860 kvm_s390_clear_float_irqs(kvm);
2861 kvm_s390_vsie_destroy(kvm);
2862 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2865 /* Section: vcpu related */
2866 static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2868 vcpu->arch.gmap = gmap_create(current->mm, -1UL);
2869 if (!vcpu->arch.gmap)
2871 vcpu->arch.gmap->private = vcpu->kvm;
2876 static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2878 if (!kvm_s390_use_sca_entries())
2880 read_lock(&vcpu->kvm->arch.sca_lock);
2881 if (vcpu->kvm->arch.use_esca) {
2882 struct esca_block *sca = vcpu->kvm->arch.sca;
2884 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2885 sca->cpu[vcpu->vcpu_id].sda = 0;
2887 struct bsca_block *sca = vcpu->kvm->arch.sca;
2889 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2890 sca->cpu[vcpu->vcpu_id].sda = 0;
2892 read_unlock(&vcpu->kvm->arch.sca_lock);
2895 static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2897 if (!kvm_s390_use_sca_entries()) {
2898 struct bsca_block *sca = vcpu->kvm->arch.sca;
2900 /* we still need the basic sca for the ipte control */
2901 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2902 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2905 read_lock(&vcpu->kvm->arch.sca_lock);
2906 if (vcpu->kvm->arch.use_esca) {
2907 struct esca_block *sca = vcpu->kvm->arch.sca;
2909 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2910 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2911 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
2912 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2913 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2915 struct bsca_block *sca = vcpu->kvm->arch.sca;
2917 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2918 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2919 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2920 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2922 read_unlock(&vcpu->kvm->arch.sca_lock);
2925 /* Basic SCA to Extended SCA data copy routines */
2926 static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
2929 d->sigp_ctrl.c = s->sigp_ctrl.c;
2930 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
2933 static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
2937 d->ipte_control = s->ipte_control;
2939 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
2940 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
2943 static int sca_switch_to_extended(struct kvm *kvm)
2945 struct bsca_block *old_sca = kvm->arch.sca;
2946 struct esca_block *new_sca;
2947 struct kvm_vcpu *vcpu;
2948 unsigned int vcpu_idx;
2951 if (kvm->arch.use_esca)
2954 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL_ACCOUNT | __GFP_ZERO);
2958 scaoh = (u32)((u64)(new_sca) >> 32);
2959 scaol = (u32)(u64)(new_sca) & ~0x3fU;
2961 kvm_s390_vcpu_block_all(kvm);
2962 write_lock(&kvm->arch.sca_lock);
2964 sca_copy_b_to_e(new_sca, old_sca);
2966 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
2967 vcpu->arch.sie_block->scaoh = scaoh;
2968 vcpu->arch.sie_block->scaol = scaol;
2969 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2971 kvm->arch.sca = new_sca;
2972 kvm->arch.use_esca = 1;
2974 write_unlock(&kvm->arch.sca_lock);
2975 kvm_s390_vcpu_unblock_all(kvm);
2977 free_page((unsigned long)old_sca);
2979 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
2980 old_sca, kvm->arch.sca);
2984 static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2988 if (!kvm_s390_use_sca_entries()) {
2989 if (id < KVM_MAX_VCPUS)
2993 if (id < KVM_S390_BSCA_CPU_SLOTS)
2995 if (!sclp.has_esca || !sclp.has_64bscao)
2998 mutex_lock(&kvm->lock);
2999 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
3000 mutex_unlock(&kvm->lock);
3002 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
3005 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3006 static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3008 WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
3009 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3010 vcpu->arch.cputm_start = get_tod_clock_fast();
3011 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3014 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3015 static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3017 WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
3018 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3019 vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3020 vcpu->arch.cputm_start = 0;
3021 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3024 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3025 static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3027 WARN_ON_ONCE(vcpu->arch.cputm_enabled);
3028 vcpu->arch.cputm_enabled = true;
3029 __start_cpu_timer_accounting(vcpu);
3032 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
3033 static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3035 WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
3036 __stop_cpu_timer_accounting(vcpu);
3037 vcpu->arch.cputm_enabled = false;
3040 static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3042 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3043 __enable_cpu_timer_accounting(vcpu);
3047 static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
3049 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3050 __disable_cpu_timer_accounting(vcpu);
3054 /* set the cpu timer - may only be called from the VCPU thread itself */
3055 void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
3057 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3058 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3059 if (vcpu->arch.cputm_enabled)
3060 vcpu->arch.cputm_start = get_tod_clock_fast();
3061 vcpu->arch.sie_block->cputm = cputm;
3062 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3066 /* update and get the cpu timer - can also be called from other VCPU threads */
3067 __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
3072 if (unlikely(!vcpu->arch.cputm_enabled))
3073 return vcpu->arch.sie_block->cputm;
3075 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3077 seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
3079 * If the writer would ever execute a read in the critical
3080 * section, e.g. in irq context, we have a deadlock.
3082 WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3083 value = vcpu->arch.sie_block->cputm;
3084 /* if cputm_start is 0, accounting is being started/stopped */
3085 if (likely(vcpu->arch.cputm_start))
3086 value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3087 } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
3092 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3095 gmap_enable(vcpu->arch.enabled_gmap);
3096 kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
3097 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3098 __start_cpu_timer_accounting(vcpu);
3102 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3105 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3106 __stop_cpu_timer_accounting(vcpu);
3107 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
3108 vcpu->arch.enabled_gmap = gmap_get_enabled();
3109 gmap_disable(vcpu->arch.enabled_gmap);
3113 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
3115 mutex_lock(&vcpu->kvm->lock);
3117 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3118 vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3120 mutex_unlock(&vcpu->kvm->lock);
3121 if (!kvm_is_ucontrol(vcpu->kvm)) {
3122 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3125 if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
3126 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
3127 /* make vcpu_load load the right gmap on the first trigger */
3128 vcpu->arch.enabled_gmap = vcpu->arch.gmap;
3131 static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
3133 if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
3134 test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
3139 static bool kvm_has_pckmo_ecc(struct kvm *kvm)
3141 /* At least one ECC subfunction must be present */
3142 return kvm_has_pckmo_subfunc(kvm, 32) ||
3143 kvm_has_pckmo_subfunc(kvm, 33) ||
3144 kvm_has_pckmo_subfunc(kvm, 34) ||
3145 kvm_has_pckmo_subfunc(kvm, 40) ||
3146 kvm_has_pckmo_subfunc(kvm, 41);
3150 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
3153 * If the AP instructions are not being interpreted and the MSAX3
3154 * facility is not configured for the guest, there is nothing to set up.
3156 if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
3159 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3160 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
3161 vcpu->arch.sie_block->eca &= ~ECA_APIE;
3162 vcpu->arch.sie_block->ecd &= ~ECD_ECC;
3164 if (vcpu->kvm->arch.crypto.apie)
3165 vcpu->arch.sie_block->eca |= ECA_APIE;
3167 /* Set up protected key support */
3168 if (vcpu->kvm->arch.crypto.aes_kw) {
3169 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
3170 /* ecc is also wrapped with AES key */
3171 if (kvm_has_pckmo_ecc(vcpu->kvm))
3172 vcpu->arch.sie_block->ecd |= ECD_ECC;
3175 if (vcpu->kvm->arch.crypto.dea_kw)
3176 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
3179 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3181 free_page(vcpu->arch.sie_block->cbrlo);
3182 vcpu->arch.sie_block->cbrlo = 0;
3185 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3187 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL_ACCOUNT);
3188 if (!vcpu->arch.sie_block->cbrlo)
3193 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
3195 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
3197 vcpu->arch.sie_block->ibc = model->ibc;
3198 if (test_kvm_facility(vcpu->kvm, 7))
3199 vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
3202 static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3207 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3211 if (test_kvm_facility(vcpu->kvm, 78))
3212 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
3213 else if (test_kvm_facility(vcpu->kvm, 8))
3214 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3216 kvm_s390_vcpu_setup_model(vcpu);
3218 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3219 if (MACHINE_HAS_ESOP)
3220 vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3221 if (test_kvm_facility(vcpu->kvm, 9))
3222 vcpu->arch.sie_block->ecb |= ECB_SRSI;
3223 if (test_kvm_facility(vcpu->kvm, 73))
3224 vcpu->arch.sie_block->ecb |= ECB_TE;
3225 if (!kvm_is_ucontrol(vcpu->kvm))
3226 vcpu->arch.sie_block->ecb |= ECB_SPECI;
3228 if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
3229 vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3230 if (test_kvm_facility(vcpu->kvm, 130))
3231 vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
3232 vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
3234 vcpu->arch.sie_block->eca |= ECA_CEI;
3236 vcpu->arch.sie_block->eca |= ECA_IB;
3238 vcpu->arch.sie_block->eca |= ECA_SII;
3239 if (sclp.has_sigpif)
3240 vcpu->arch.sie_block->eca |= ECA_SIGPI;
3241 if (test_kvm_facility(vcpu->kvm, 129)) {
3242 vcpu->arch.sie_block->eca |= ECA_VX;
3243 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
3245 if (test_kvm_facility(vcpu->kvm, 139))
3246 vcpu->arch.sie_block->ecd |= ECD_MEF;
3247 if (test_kvm_facility(vcpu->kvm, 156))
3248 vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3249 if (vcpu->arch.sie_block->gd) {
3250 vcpu->arch.sie_block->eca |= ECA_AIV;
3251 VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3252 vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3254 vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
3256 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
3259 kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3261 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
3263 if (vcpu->kvm->arch.use_cmma) {
3264 rc = kvm_s390_vcpu_setup_cmma(vcpu);
3268 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3269 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
3271 vcpu->arch.sie_block->hpid = HPID_KVM;
3273 kvm_s390_vcpu_crypto_setup(vcpu);
3275 mutex_lock(&vcpu->kvm->lock);
3276 if (kvm_s390_pv_is_protected(vcpu->kvm)) {
3277 rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
3279 kvm_s390_vcpu_unsetup_cmma(vcpu);
3281 mutex_unlock(&vcpu->kvm->lock);
3286 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3288 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3293 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3295 struct sie_page *sie_page;
3298 BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
3299 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL_ACCOUNT);
3303 vcpu->arch.sie_block = &sie_page->sie_block;
3304 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
3306 /* the real guest size will always be smaller than msl */
3307 vcpu->arch.sie_block->mso = 0;
3308 vcpu->arch.sie_block->msl = sclp.hamax;
3310 vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3311 spin_lock_init(&vcpu->arch.local_int.lock);
3312 vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin;
3313 if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
3314 vcpu->arch.sie_block->gd |= GISA_FORMAT1;
3315 seqcount_init(&vcpu->arch.cputm_seqcount);
3317 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3318 kvm_clear_async_pf_completion_queue(vcpu);
3319 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3326 kvm_s390_set_prefix(vcpu, 0);
3327 if (test_kvm_facility(vcpu->kvm, 64))
3328 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3329 if (test_kvm_facility(vcpu->kvm, 82))
3330 vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3331 if (test_kvm_facility(vcpu->kvm, 133))
3332 vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3333 if (test_kvm_facility(vcpu->kvm, 156))
3334 vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3335 /* fprs can be synchronized via vrs, even if the guest has no vx. With
3336 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3339 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3341 vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3343 if (kvm_is_ucontrol(vcpu->kvm)) {
3344 rc = __kvm_ucontrol_vcpu_init(vcpu);
3346 goto out_free_sie_block;
3349 VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3350 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3351 trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3353 rc = kvm_s390_vcpu_setup(vcpu);
3355 goto out_ucontrol_uninit;
3358 out_ucontrol_uninit:
3359 if (kvm_is_ucontrol(vcpu->kvm))
3360 gmap_remove(vcpu->arch.gmap);
3362 free_page((unsigned long)(vcpu->arch.sie_block));
3366 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3368 clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
3369 return kvm_s390_vcpu_has_irq(vcpu, 0);
3372 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
3374 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3377 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
3379 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3383 void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
3385 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3388 static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
3390 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3394 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
3396 return atomic_read(&vcpu->arch.sie_block->prog20) &
3397 (PROG_BLOCK_SIE | PROG_REQUEST);
3400 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
3402 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3406 * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
3407 * If the CPU is not running (e.g. waiting as idle) the function will
3408 * return immediately. */
3409 void exit_sie(struct kvm_vcpu *vcpu)
3411 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
3412 kvm_s390_vsie_kick(vcpu);
3413 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
3417 /* Kick a guest cpu out of SIE to process a request synchronously */
3418 void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
3420 kvm_make_request(req, vcpu);
3421 kvm_s390_vcpu_request(vcpu);
3424 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
3427 struct kvm *kvm = gmap->private;
3428 struct kvm_vcpu *vcpu;
3429 unsigned long prefix;
3432 if (gmap_is_shadow(gmap))
3434 if (start >= 1UL << 31)
3435 /* We are only interested in prefix pages */
3437 kvm_for_each_vcpu(i, vcpu, kvm) {
3438 /* match against both prefix pages */
3439 prefix = kvm_s390_get_prefix(vcpu);
3440 if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3441 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3443 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
3448 bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
3450 /* do not poll with more than halt_poll_max_steal percent of steal time */
3451 if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
3452 halt_poll_max_steal) {
3453 vcpu->stat.halt_no_poll_steal++;
3459 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3461 /* kvm common code refers to this, but never calls it */
3466 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
3467 struct kvm_one_reg *reg)
3472 case KVM_REG_S390_TODPR:
3473 r = put_user(vcpu->arch.sie_block->todpr,
3474 (u32 __user *)reg->addr);
3476 case KVM_REG_S390_EPOCHDIFF:
3477 r = put_user(vcpu->arch.sie_block->epoch,
3478 (u64 __user *)reg->addr);
3480 case KVM_REG_S390_CPU_TIMER:
3481 r = put_user(kvm_s390_get_cpu_timer(vcpu),
3482 (u64 __user *)reg->addr);
3484 case KVM_REG_S390_CLOCK_COMP:
3485 r = put_user(vcpu->arch.sie_block->ckc,
3486 (u64 __user *)reg->addr);
3488 case KVM_REG_S390_PFTOKEN:
3489 r = put_user(vcpu->arch.pfault_token,
3490 (u64 __user *)reg->addr);
3492 case KVM_REG_S390_PFCOMPARE:
3493 r = put_user(vcpu->arch.pfault_compare,
3494 (u64 __user *)reg->addr);
3496 case KVM_REG_S390_PFSELECT:
3497 r = put_user(vcpu->arch.pfault_select,
3498 (u64 __user *)reg->addr);
3500 case KVM_REG_S390_PP:
3501 r = put_user(vcpu->arch.sie_block->pp,
3502 (u64 __user *)reg->addr);
3504 case KVM_REG_S390_GBEA:
3505 r = put_user(vcpu->arch.sie_block->gbea,
3506 (u64 __user *)reg->addr);
3515 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
3516 struct kvm_one_reg *reg)
3522 case KVM_REG_S390_TODPR:
3523 r = get_user(vcpu->arch.sie_block->todpr,
3524 (u32 __user *)reg->addr);
3526 case KVM_REG_S390_EPOCHDIFF:
3527 r = get_user(vcpu->arch.sie_block->epoch,
3528 (u64 __user *)reg->addr);
3530 case KVM_REG_S390_CPU_TIMER:
3531 r = get_user(val, (u64 __user *)reg->addr);
3533 kvm_s390_set_cpu_timer(vcpu, val);
3535 case KVM_REG_S390_CLOCK_COMP:
3536 r = get_user(vcpu->arch.sie_block->ckc,
3537 (u64 __user *)reg->addr);
3539 case KVM_REG_S390_PFTOKEN:
3540 r = get_user(vcpu->arch.pfault_token,
3541 (u64 __user *)reg->addr);
3542 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3543 kvm_clear_async_pf_completion_queue(vcpu);
3545 case KVM_REG_S390_PFCOMPARE:
3546 r = get_user(vcpu->arch.pfault_compare,
3547 (u64 __user *)reg->addr);
3549 case KVM_REG_S390_PFSELECT:
3550 r = get_user(vcpu->arch.pfault_select,
3551 (u64 __user *)reg->addr);
3553 case KVM_REG_S390_PP:
3554 r = get_user(vcpu->arch.sie_block->pp,
3555 (u64 __user *)reg->addr);
3557 case KVM_REG_S390_GBEA:
3558 r = get_user(vcpu->arch.sie_block->gbea,
3559 (u64 __user *)reg->addr);
3568 static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
3570 vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
3571 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3572 memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
3574 kvm_clear_async_pf_completion_queue(vcpu);
3575 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
3576 kvm_s390_vcpu_stop(vcpu);
3577 kvm_s390_clear_local_irqs(vcpu);
3580 static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
3582 /* Initial reset is a superset of the normal reset */
3583 kvm_arch_vcpu_ioctl_normal_reset(vcpu);
3586 * This equals initial cpu reset in pop, but we don't switch to ESA.
3587 * We do not only reset the internal data, but also ...
3589 vcpu->arch.sie_block->gpsw.mask = 0;
3590 vcpu->arch.sie_block->gpsw.addr = 0;
3591 kvm_s390_set_prefix(vcpu, 0);
3592 kvm_s390_set_cpu_timer(vcpu, 0);
3593 vcpu->arch.sie_block->ckc = 0;
3594 memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
3595 vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
3596 vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
3598 /* ... the data in sync regs */
3599 memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
3600 vcpu->run->s.regs.ckc = 0;
3601 vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
3602 vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
3603 vcpu->run->psw_addr = 0;
3604 vcpu->run->psw_mask = 0;
3605 vcpu->run->s.regs.todpr = 0;
3606 vcpu->run->s.regs.cputm = 0;
3607 vcpu->run->s.regs.ckc = 0;
3608 vcpu->run->s.regs.pp = 0;
3609 vcpu->run->s.regs.gbea = 1;
3610 vcpu->run->s.regs.fpc = 0;
3612 * Do not reset these registers in the protected case, as some of
3613 * them are overlayed and they are not accessible in this case
3616 if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
3617 vcpu->arch.sie_block->gbea = 1;
3618 vcpu->arch.sie_block->pp = 0;
3619 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
3620 vcpu->arch.sie_block->todpr = 0;
3624 static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
3626 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
3628 /* Clear reset is a superset of the initial reset */
3629 kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3631 memset(®s->gprs, 0, sizeof(regs->gprs));
3632 memset(®s->vrs, 0, sizeof(regs->vrs));
3633 memset(®s->acrs, 0, sizeof(regs->acrs));
3634 memset(®s->gscb, 0, sizeof(regs->gscb));
3637 regs->etoken_extension = 0;
3640 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3643 memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs));
3648 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3651 memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
3656 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3657 struct kvm_sregs *sregs)
3661 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
3662 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
3668 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3669 struct kvm_sregs *sregs)
3673 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
3674 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
3680 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3686 if (test_fp_ctl(fpu->fpc)) {
3690 vcpu->run->s.regs.fpc = fpu->fpc;
3692 convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
3693 (freg_t *) fpu->fprs);
3695 memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
3702 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3706 /* make sure we have the latest values */
3709 convert_vx_to_fp((freg_t *) fpu->fprs,
3710 (__vector128 *) vcpu->run->s.regs.vrs);
3712 memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
3713 fpu->fpc = vcpu->run->s.regs.fpc;
3719 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
3723 if (!is_vcpu_stopped(vcpu))
3726 vcpu->run->psw_mask = psw.mask;
3727 vcpu->run->psw_addr = psw.addr;
3732 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3733 struct kvm_translation *tr)
3735 return -EINVAL; /* not implemented yet */
3738 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
3739 KVM_GUESTDBG_USE_HW_BP | \
3740 KVM_GUESTDBG_ENABLE)
3742 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
3743 struct kvm_guest_debug *dbg)
3749 vcpu->guest_debug = 0;
3750 kvm_s390_clear_bp_data(vcpu);
3752 if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
3756 if (!sclp.has_gpere) {
3761 if (dbg->control & KVM_GUESTDBG_ENABLE) {
3762 vcpu->guest_debug = dbg->control;
3763 /* enforce guest PER */
3764 kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
3766 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
3767 rc = kvm_s390_import_bp_data(vcpu, dbg);
3769 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
3770 vcpu->arch.guestdbg.last_bp = 0;
3774 vcpu->guest_debug = 0;
3775 kvm_s390_clear_bp_data(vcpu);
3776 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
3784 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3785 struct kvm_mp_state *mp_state)
3791 /* CHECK_STOP and LOAD are not supported yet */
3792 ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
3793 KVM_MP_STATE_OPERATING;
3799 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3800 struct kvm_mp_state *mp_state)
3806 /* user space knows about this interface - let it control the state */
3807 kvm_s390_set_user_cpu_state_ctrl(vcpu->kvm);
3809 switch (mp_state->mp_state) {
3810 case KVM_MP_STATE_STOPPED:
3811 rc = kvm_s390_vcpu_stop(vcpu);
3813 case KVM_MP_STATE_OPERATING:
3814 rc = kvm_s390_vcpu_start(vcpu);
3816 case KVM_MP_STATE_LOAD:
3817 if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
3821 rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
3823 case KVM_MP_STATE_CHECK_STOP:
3824 fallthrough; /* CHECK_STOP and LOAD are not supported yet */
3833 static bool ibs_enabled(struct kvm_vcpu *vcpu)
3835 return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
3838 static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
3841 kvm_s390_vcpu_request_handled(vcpu);
3842 if (!kvm_request_pending(vcpu))
3845 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3846 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
3847 * This ensures that the ipte instruction for this request has
3848 * already finished. We might race against a second unmapper that
3849 * wants to set the blocking bit. Lets just retry the request loop.
3851 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
3853 rc = gmap_mprotect_notify(vcpu->arch.gmap,
3854 kvm_s390_get_prefix(vcpu),
3855 PAGE_SIZE * 2, PROT_WRITE);
3857 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
3863 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3864 vcpu->arch.sie_block->ihcpu = 0xffff;
3868 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
3869 if (!ibs_enabled(vcpu)) {
3870 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3871 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
3876 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
3877 if (ibs_enabled(vcpu)) {
3878 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
3879 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
3884 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
3885 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
3889 if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3891 * Disable CMM virtualization; we will emulate the ESSA
3892 * instruction manually, in order to provide additional
3893 * functionalities needed for live migration.
3895 vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3899 if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3901 * Re-enable CMM virtualization if CMMA is available and
3902 * CMM has been used.
3904 if ((vcpu->kvm->arch.use_cmma) &&
3905 (vcpu->kvm->mm->context.uses_cmm))
3906 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3910 /* nothing to do, just clear the request */
3911 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
3912 /* we left the vsie handler, nothing to do, just clear the request */
3913 kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
3918 void kvm_s390_set_tod_clock(struct kvm *kvm,
3919 const struct kvm_s390_vm_tod_clock *gtod)
3921 struct kvm_vcpu *vcpu;
3922 union tod_clock clk;
3925 mutex_lock(&kvm->lock);
3928 store_tod_clock_ext(&clk);
3930 kvm->arch.epoch = gtod->tod - clk.tod;
3932 if (test_kvm_facility(kvm, 139)) {
3933 kvm->arch.epdx = gtod->epoch_idx - clk.ei;
3934 if (kvm->arch.epoch > gtod->tod)
3935 kvm->arch.epdx -= 1;
3938 kvm_s390_vcpu_block_all(kvm);
3939 kvm_for_each_vcpu(i, vcpu, kvm) {
3940 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
3941 vcpu->arch.sie_block->epdx = kvm->arch.epdx;
3944 kvm_s390_vcpu_unblock_all(kvm);
3946 mutex_unlock(&kvm->lock);
3950 * kvm_arch_fault_in_page - fault-in guest page if necessary
3951 * @vcpu: The corresponding virtual cpu
3952 * @gpa: Guest physical address
3953 * @writable: Whether the page should be writable or not
3955 * Make sure that a guest page has been faulted-in on the host.
3957 * Return: Zero on success, negative error code otherwise.
3959 long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
3961 return gmap_fault(vcpu->arch.gmap, gpa,
3962 writable ? FAULT_FLAG_WRITE : 0);
3965 static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
3966 unsigned long token)
3968 struct kvm_s390_interrupt inti;
3969 struct kvm_s390_irq irq;
3972 irq.u.ext.ext_params2 = token;
3973 irq.type = KVM_S390_INT_PFAULT_INIT;
3974 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
3976 inti.type = KVM_S390_INT_PFAULT_DONE;
3977 inti.parm64 = token;
3978 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
3982 bool kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
3983 struct kvm_async_pf *work)
3985 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
3986 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
3991 void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
3992 struct kvm_async_pf *work)
3994 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
3995 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
3998 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
3999 struct kvm_async_pf *work)
4001 /* s390 will always inject the page directly */
4004 bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu)
4007 * s390 will always inject the page directly,
4008 * but we still want check_async_completion to cleanup
4013 static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
4016 struct kvm_arch_async_pf arch;
4018 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4020 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
4021 vcpu->arch.pfault_compare)
4023 if (psw_extint_disabled(vcpu))
4025 if (kvm_s390_vcpu_has_irq(vcpu, 0))
4027 if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
4029 if (!vcpu->arch.gmap->pfault_enabled)
4032 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
4033 hva += current->thread.gmap_addr & ~PAGE_MASK;
4034 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
4037 return kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
4040 static int vcpu_pre_run(struct kvm_vcpu *vcpu)
4045 * On s390 notifications for arriving pages will be delivered directly
4046 * to the guest but the house keeping for completed pfaults is
4047 * handled outside the worker.
4049 kvm_check_async_pf_completion(vcpu);
4051 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
4052 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
4057 if (!kvm_is_ucontrol(vcpu->kvm)) {
4058 rc = kvm_s390_deliver_pending_interrupts(vcpu);
4063 rc = kvm_s390_handle_requests(vcpu);
4067 if (guestdbg_enabled(vcpu)) {
4068 kvm_s390_backup_guest_per_regs(vcpu);
4069 kvm_s390_patch_guest_per_regs(vcpu);
4072 clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.gisa_int.kicked_mask);
4074 vcpu->arch.sie_block->icptcode = 0;
4075 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
4076 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
4077 trace_kvm_s390_sie_enter(vcpu, cpuflags);
4082 static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
4084 struct kvm_s390_pgm_info pgm_info = {
4085 .code = PGM_ADDRESSING,
4090 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4091 trace_kvm_s390_sie_fault(vcpu);
4094 * We want to inject an addressing exception, which is defined as a
4095 * suppressing or terminating exception. However, since we came here
4096 * by a DAT access exception, the PSW still points to the faulting
4097 * instruction since DAT exceptions are nullifying. So we've got
4098 * to look up the current opcode to get the length of the instruction
4099 * to be able to forward the PSW.
4101 rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
4102 ilen = insn_length(opcode);
4106 /* Instruction-Fetching Exceptions - we can't detect the ilen.
4107 * Forward by arbitrary ilc, injection will take care of
4108 * nullification if necessary.
4110 pgm_info = vcpu->arch.pgm;
4113 pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
4114 kvm_s390_forward_psw(vcpu, ilen);
4115 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
4118 static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
4120 struct mcck_volatile_info *mcck_info;
4121 struct sie_page *sie_page;
4123 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
4124 vcpu->arch.sie_block->icptcode);
4125 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
4127 if (guestdbg_enabled(vcpu))
4128 kvm_s390_restore_guest_per_regs(vcpu);
4130 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
4131 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
4133 if (exit_reason == -EINTR) {
4134 VCPU_EVENT(vcpu, 3, "%s", "machine check");
4135 sie_page = container_of(vcpu->arch.sie_block,
4136 struct sie_page, sie_block);
4137 mcck_info = &sie_page->mcck_info;
4138 kvm_s390_reinject_machine_check(vcpu, mcck_info);
4142 if (vcpu->arch.sie_block->icptcode > 0) {
4143 int rc = kvm_handle_sie_intercept(vcpu);
4145 if (rc != -EOPNOTSUPP)
4147 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
4148 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
4149 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
4150 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
4152 } else if (exit_reason != -EFAULT) {
4153 vcpu->stat.exit_null++;
4155 } else if (kvm_is_ucontrol(vcpu->kvm)) {
4156 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4157 vcpu->run->s390_ucontrol.trans_exc_code =
4158 current->thread.gmap_addr;
4159 vcpu->run->s390_ucontrol.pgm_code = 0x10;
4161 } else if (current->thread.gmap_pfault) {
4162 trace_kvm_s390_major_guest_pfault(vcpu);
4163 current->thread.gmap_pfault = 0;
4164 if (kvm_arch_setup_async_pf(vcpu))
4166 vcpu->stat.pfault_sync++;
4167 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
4169 return vcpu_post_run_fault_in_sie(vcpu);
4172 #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
4173 static int __vcpu_run(struct kvm_vcpu *vcpu)
4175 int rc, exit_reason;
4176 struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
4179 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4180 * ning the guest), so that memslots (and other stuff) are protected
4182 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4185 rc = vcpu_pre_run(vcpu);
4189 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4191 * As PF_VCPU will be used in fault handler, between
4192 * guest_enter and guest_exit should be no uaccess.
4194 local_irq_disable();
4195 guest_enter_irqoff();
4196 __disable_cpu_timer_accounting(vcpu);
4198 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4199 memcpy(sie_page->pv_grregs,
4200 vcpu->run->s.regs.gprs,
4201 sizeof(sie_page->pv_grregs));
4203 if (test_cpu_flag(CIF_FPU))
4205 exit_reason = sie64a(vcpu->arch.sie_block,
4206 vcpu->run->s.regs.gprs);
4207 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4208 memcpy(vcpu->run->s.regs.gprs,
4209 sie_page->pv_grregs,
4210 sizeof(sie_page->pv_grregs));
4212 * We're not allowed to inject interrupts on intercepts
4213 * that leave the guest state in an "in-between" state
4214 * where the next SIE entry will do a continuation.
4215 * Fence interrupts in our "internal" PSW.
4217 if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
4218 vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
4219 vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
4222 local_irq_disable();
4223 __enable_cpu_timer_accounting(vcpu);
4224 guest_exit_irqoff();
4226 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4228 rc = vcpu_post_run(vcpu, exit_reason);
4229 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
4231 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4235 static void sync_regs_fmt2(struct kvm_vcpu *vcpu)
4237 struct kvm_run *kvm_run = vcpu->run;
4238 struct runtime_instr_cb *riccb;
4241 riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
4242 gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4243 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4244 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4245 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4246 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4247 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4248 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4250 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4251 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4252 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4253 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
4254 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4255 kvm_clear_async_pf_completion_queue(vcpu);
4257 if (kvm_run->kvm_dirty_regs & KVM_SYNC_DIAG318) {
4258 vcpu->arch.diag318_info.val = kvm_run->s.regs.diag318;
4259 vcpu->arch.sie_block->cpnc = vcpu->arch.diag318_info.cpnc;
4260 VCPU_EVENT(vcpu, 3, "setting cpnc to %d", vcpu->arch.diag318_info.cpnc);
4263 * If userspace sets the riccb (e.g. after migration) to a valid state,
4264 * we should enable RI here instead of doing the lazy enablement.
4266 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
4267 test_kvm_facility(vcpu->kvm, 64) &&
4269 !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
4270 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
4271 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
4274 * If userspace sets the gscb (e.g. after migration) to non-zero,
4275 * we should enable GS here instead of doing the lazy enablement.
4277 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
4278 test_kvm_facility(vcpu->kvm, 133) &&
4280 !vcpu->arch.gs_enabled) {
4281 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
4282 vcpu->arch.sie_block->ecb |= ECB_GS;
4283 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
4284 vcpu->arch.gs_enabled = 1;
4286 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
4287 test_kvm_facility(vcpu->kvm, 82)) {
4288 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
4289 vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
4291 if (MACHINE_HAS_GS) {
4293 __ctl_set_bit(2, 4);
4294 if (current->thread.gs_cb) {
4295 vcpu->arch.host_gscb = current->thread.gs_cb;
4296 save_gs_cb(vcpu->arch.host_gscb);
4298 if (vcpu->arch.gs_enabled) {
4299 current->thread.gs_cb = (struct gs_cb *)
4300 &vcpu->run->s.regs.gscb;
4301 restore_gs_cb(current->thread.gs_cb);
4305 /* SIE will load etoken directly from SDNX and therefore kvm_run */
4308 static void sync_regs(struct kvm_vcpu *vcpu)
4310 struct kvm_run *kvm_run = vcpu->run;
4312 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4313 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4314 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4315 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4316 /* some control register changes require a tlb flush */
4317 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4319 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4320 kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4321 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4323 save_access_regs(vcpu->arch.host_acrs);
4324 restore_access_regs(vcpu->run->s.regs.acrs);
4325 /* save host (userspace) fprs/vrs */
4327 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4328 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4330 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4332 current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4333 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4334 if (test_fp_ctl(current->thread.fpu.fpc))
4335 /* User space provided an invalid FPC, let's clear it */
4336 current->thread.fpu.fpc = 0;
4338 /* Sync fmt2 only data */
4339 if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
4340 sync_regs_fmt2(vcpu);
4343 * In several places we have to modify our internal view to
4344 * not do things that are disallowed by the ultravisor. For
4345 * example we must not inject interrupts after specific exits
4346 * (e.g. 112 prefix page not secure). We do this by turning
4347 * off the machine check, external and I/O interrupt bits
4348 * of our PSW copy. To avoid getting validity intercepts, we
4349 * do only accept the condition code from userspace.
4351 vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4352 vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4356 kvm_run->kvm_dirty_regs = 0;
4359 static void store_regs_fmt2(struct kvm_vcpu *vcpu)
4361 struct kvm_run *kvm_run = vcpu->run;
4363 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4364 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4365 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
4366 kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
4367 kvm_run->s.regs.diag318 = vcpu->arch.diag318_info.val;
4368 if (MACHINE_HAS_GS) {
4370 __ctl_set_bit(2, 4);
4371 if (vcpu->arch.gs_enabled)
4372 save_gs_cb(current->thread.gs_cb);
4373 current->thread.gs_cb = vcpu->arch.host_gscb;
4374 restore_gs_cb(vcpu->arch.host_gscb);
4375 if (!vcpu->arch.host_gscb)
4376 __ctl_clear_bit(2, 4);
4377 vcpu->arch.host_gscb = NULL;
4380 /* SIE will save etoken directly into SDNX and therefore kvm_run */
4383 static void store_regs(struct kvm_vcpu *vcpu)
4385 struct kvm_run *kvm_run = vcpu->run;
4387 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
4388 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
4389 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
4390 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
4391 kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
4392 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
4393 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
4394 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
4395 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
4396 save_access_regs(vcpu->run->s.regs.acrs);
4397 restore_access_regs(vcpu->arch.host_acrs);
4398 /* Save guest register state */
4400 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4401 /* Restore will be done lazily at return */
4402 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
4403 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
4404 if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
4405 store_regs_fmt2(vcpu);
4408 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
4410 struct kvm_run *kvm_run = vcpu->run;
4413 if (kvm_run->immediate_exit)
4416 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
4417 kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
4422 if (guestdbg_exit_pending(vcpu)) {
4423 kvm_s390_prepare_debug_exit(vcpu);
4428 kvm_sigset_activate(vcpu);
4431 * no need to check the return value of vcpu_start as it can only have
4432 * an error for protvirt, but protvirt means user cpu state
4434 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
4435 kvm_s390_vcpu_start(vcpu);
4436 } else if (is_vcpu_stopped(vcpu)) {
4437 pr_err_ratelimited("can't run stopped vcpu %d\n",
4444 enable_cpu_timer_accounting(vcpu);
4447 rc = __vcpu_run(vcpu);
4449 if (signal_pending(current) && !rc) {
4450 kvm_run->exit_reason = KVM_EXIT_INTR;
4454 if (guestdbg_exit_pending(vcpu) && !rc) {
4455 kvm_s390_prepare_debug_exit(vcpu);
4459 if (rc == -EREMOTE) {
4460 /* userspace support is needed, kvm_run has been prepared */
4464 disable_cpu_timer_accounting(vcpu);
4467 kvm_sigset_deactivate(vcpu);
4469 vcpu->stat.exit_userspace++;
4476 * store status at address
4477 * we use have two special cases:
4478 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
4479 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
4481 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
4483 unsigned char archmode = 1;
4484 freg_t fprs[NUM_FPRS];
4489 px = kvm_s390_get_prefix(vcpu);
4490 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
4491 if (write_guest_abs(vcpu, 163, &archmode, 1))
4494 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
4495 if (write_guest_real(vcpu, 163, &archmode, 1))
4499 gpa -= __LC_FPREGS_SAVE_AREA;
4501 /* manually convert vector registers if necessary */
4502 if (MACHINE_HAS_VX) {
4503 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
4504 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
4507 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
4508 vcpu->run->s.regs.fprs, 128);
4510 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
4511 vcpu->run->s.regs.gprs, 128);
4512 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
4513 &vcpu->arch.sie_block->gpsw, 16);
4514 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
4516 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
4517 &vcpu->run->s.regs.fpc, 4);
4518 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
4519 &vcpu->arch.sie_block->todpr, 4);
4520 cputm = kvm_s390_get_cpu_timer(vcpu);
4521 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
4523 clkcomp = vcpu->arch.sie_block->ckc >> 8;
4524 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
4526 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
4527 &vcpu->run->s.regs.acrs, 64);
4528 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
4529 &vcpu->arch.sie_block->gcr, 128);
4530 return rc ? -EFAULT : 0;
4533 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
4536 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
4537 * switch in the run ioctl. Let's update our copies before we save
4538 * it into the save area
4541 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4542 save_access_regs(vcpu->run->s.regs.acrs);
4544 return kvm_s390_store_status_unloaded(vcpu, addr);
4547 static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
4549 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
4550 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
4553 static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
4556 struct kvm_vcpu *vcpu;
4558 kvm_for_each_vcpu(i, vcpu, kvm) {
4559 __disable_ibs_on_vcpu(vcpu);
4563 static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
4567 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
4568 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
4571 int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
4573 int i, online_vcpus, r = 0, started_vcpus = 0;
4575 if (!is_vcpu_stopped(vcpu))
4578 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
4579 /* Only one cpu at a time may enter/leave the STOPPED state. */
4580 spin_lock(&vcpu->kvm->arch.start_stop_lock);
4581 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
4583 /* Let's tell the UV that we want to change into the operating state */
4584 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4585 r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
4587 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4592 for (i = 0; i < online_vcpus; i++) {
4593 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
4597 if (started_vcpus == 0) {
4598 /* we're the only active VCPU -> speed it up */
4599 __enable_ibs_on_vcpu(vcpu);
4600 } else if (started_vcpus == 1) {
4602 * As we are starting a second VCPU, we have to disable
4603 * the IBS facility on all VCPUs to remove potentially
4604 * outstanding ENABLE requests.
4606 __disable_ibs_on_all_vcpus(vcpu->kvm);
4609 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
4611 * The real PSW might have changed due to a RESTART interpreted by the
4612 * ultravisor. We block all interrupts and let the next sie exit
4615 if (kvm_s390_pv_cpu_is_protected(vcpu))
4616 vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
4618 * Another VCPU might have used IBS while we were offline.
4619 * Let's play safe and flush the VCPU at startup.
4621 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4622 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4626 int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
4628 int i, online_vcpus, r = 0, started_vcpus = 0;
4629 struct kvm_vcpu *started_vcpu = NULL;
4631 if (is_vcpu_stopped(vcpu))
4634 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
4635 /* Only one cpu at a time may enter/leave the STOPPED state. */
4636 spin_lock(&vcpu->kvm->arch.start_stop_lock);
4637 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
4639 /* Let's tell the UV that we want to change into the stopped state */
4640 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4641 r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
4643 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4648 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
4649 kvm_s390_clear_stop_irq(vcpu);
4651 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
4652 __disable_ibs_on_vcpu(vcpu);
4654 for (i = 0; i < online_vcpus; i++) {
4655 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
4657 started_vcpu = vcpu->kvm->vcpus[i];
4661 if (started_vcpus == 1) {
4663 * As we only have one VCPU left, we want to enable the
4664 * IBS facility for that VCPU to speed it up.
4666 __enable_ibs_on_vcpu(started_vcpu);
4669 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4673 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4674 struct kvm_enable_cap *cap)
4682 case KVM_CAP_S390_CSS_SUPPORT:
4683 if (!vcpu->kvm->arch.css_support) {
4684 vcpu->kvm->arch.css_support = 1;
4685 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
4686 trace_kvm_s390_enable_css(vcpu->kvm);
4697 static long kvm_s390_guest_sida_op(struct kvm_vcpu *vcpu,
4698 struct kvm_s390_mem_op *mop)
4700 void __user *uaddr = (void __user *)mop->buf;
4703 if (mop->flags || !mop->size)
4705 if (mop->size + mop->sida_offset < mop->size)
4707 if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
4711 case KVM_S390_MEMOP_SIDA_READ:
4712 if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) +
4713 mop->sida_offset), mop->size))
4717 case KVM_S390_MEMOP_SIDA_WRITE:
4718 if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) +
4719 mop->sida_offset), uaddr, mop->size))
4725 static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
4726 struct kvm_s390_mem_op *mop)
4728 void __user *uaddr = (void __user *)mop->buf;
4729 void *tmpbuf = NULL;
4731 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
4732 | KVM_S390_MEMOP_F_CHECK_ONLY;
4734 if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
4737 if (mop->size > MEM_OP_MAX_SIZE)
4740 if (kvm_s390_pv_cpu_is_protected(vcpu))
4743 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
4744 tmpbuf = vmalloc(mop->size);
4750 case KVM_S390_MEMOP_LOGICAL_READ:
4751 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4752 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4753 mop->size, GACC_FETCH);
4756 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4758 if (copy_to_user(uaddr, tmpbuf, mop->size))
4762 case KVM_S390_MEMOP_LOGICAL_WRITE:
4763 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4764 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4765 mop->size, GACC_STORE);
4768 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
4772 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4776 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
4777 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
4783 static long kvm_s390_guest_memsida_op(struct kvm_vcpu *vcpu,
4784 struct kvm_s390_mem_op *mop)
4788 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4791 case KVM_S390_MEMOP_LOGICAL_READ:
4792 case KVM_S390_MEMOP_LOGICAL_WRITE:
4793 r = kvm_s390_guest_mem_op(vcpu, mop);
4795 case KVM_S390_MEMOP_SIDA_READ:
4796 case KVM_S390_MEMOP_SIDA_WRITE:
4797 /* we are locked against sida going away by the vcpu->mutex */
4798 r = kvm_s390_guest_sida_op(vcpu, mop);
4804 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
4808 long kvm_arch_vcpu_async_ioctl(struct file *filp,
4809 unsigned int ioctl, unsigned long arg)
4811 struct kvm_vcpu *vcpu = filp->private_data;
4812 void __user *argp = (void __user *)arg;
4815 case KVM_S390_IRQ: {
4816 struct kvm_s390_irq s390irq;
4818 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
4820 return kvm_s390_inject_vcpu(vcpu, &s390irq);
4822 case KVM_S390_INTERRUPT: {
4823 struct kvm_s390_interrupt s390int;
4824 struct kvm_s390_irq s390irq = {};
4826 if (copy_from_user(&s390int, argp, sizeof(s390int)))
4828 if (s390int_to_s390irq(&s390int, &s390irq))
4830 return kvm_s390_inject_vcpu(vcpu, &s390irq);
4833 return -ENOIOCTLCMD;
4836 long kvm_arch_vcpu_ioctl(struct file *filp,
4837 unsigned int ioctl, unsigned long arg)
4839 struct kvm_vcpu *vcpu = filp->private_data;
4840 void __user *argp = (void __user *)arg;
4848 case KVM_S390_STORE_STATUS:
4849 idx = srcu_read_lock(&vcpu->kvm->srcu);
4850 r = kvm_s390_store_status_unloaded(vcpu, arg);
4851 srcu_read_unlock(&vcpu->kvm->srcu, idx);
4853 case KVM_S390_SET_INITIAL_PSW: {
4857 if (copy_from_user(&psw, argp, sizeof(psw)))
4859 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
4862 case KVM_S390_CLEAR_RESET:
4864 kvm_arch_vcpu_ioctl_clear_reset(vcpu);
4865 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4866 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
4867 UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
4868 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
4872 case KVM_S390_INITIAL_RESET:
4874 kvm_arch_vcpu_ioctl_initial_reset(vcpu);
4875 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4876 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
4877 UVC_CMD_CPU_RESET_INITIAL,
4879 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
4883 case KVM_S390_NORMAL_RESET:
4885 kvm_arch_vcpu_ioctl_normal_reset(vcpu);
4886 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4887 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
4888 UVC_CMD_CPU_RESET, &rc, &rrc);
4889 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
4893 case KVM_SET_ONE_REG:
4894 case KVM_GET_ONE_REG: {
4895 struct kvm_one_reg reg;
4897 if (kvm_s390_pv_cpu_is_protected(vcpu))
4900 if (copy_from_user(®, argp, sizeof(reg)))
4902 if (ioctl == KVM_SET_ONE_REG)
4903 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®);
4905 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®);
4908 #ifdef CONFIG_KVM_S390_UCONTROL
4909 case KVM_S390_UCAS_MAP: {
4910 struct kvm_s390_ucas_mapping ucasmap;
4912 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4917 if (!kvm_is_ucontrol(vcpu->kvm)) {
4922 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
4923 ucasmap.vcpu_addr, ucasmap.length);
4926 case KVM_S390_UCAS_UNMAP: {
4927 struct kvm_s390_ucas_mapping ucasmap;
4929 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4934 if (!kvm_is_ucontrol(vcpu->kvm)) {
4939 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
4944 case KVM_S390_VCPU_FAULT: {
4945 r = gmap_fault(vcpu->arch.gmap, arg, 0);
4948 case KVM_ENABLE_CAP:
4950 struct kvm_enable_cap cap;
4952 if (copy_from_user(&cap, argp, sizeof(cap)))
4954 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4957 case KVM_S390_MEM_OP: {
4958 struct kvm_s390_mem_op mem_op;
4960 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
4961 r = kvm_s390_guest_memsida_op(vcpu, &mem_op);
4966 case KVM_S390_SET_IRQ_STATE: {
4967 struct kvm_s390_irq_state irq_state;
4970 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4972 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
4973 irq_state.len == 0 ||
4974 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
4978 /* do not use irq_state.flags, it will break old QEMUs */
4979 r = kvm_s390_set_irq_state(vcpu,
4980 (void __user *) irq_state.buf,
4984 case KVM_S390_GET_IRQ_STATE: {
4985 struct kvm_s390_irq_state irq_state;
4988 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4990 if (irq_state.len == 0) {
4994 /* do not use irq_state.flags, it will break old QEMUs */
4995 r = kvm_s390_get_irq_state(vcpu,
4996 (__u8 __user *) irq_state.buf,
5008 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
5010 #ifdef CONFIG_KVM_S390_UCONTROL
5011 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
5012 && (kvm_is_ucontrol(vcpu->kvm))) {
5013 vmf->page = virt_to_page(vcpu->arch.sie_block);
5014 get_page(vmf->page);
5018 return VM_FAULT_SIGBUS;
5021 /* Section: memory related */
5022 int kvm_arch_prepare_memory_region(struct kvm *kvm,
5023 struct kvm_memory_slot *memslot,
5024 const struct kvm_userspace_memory_region *mem,
5025 enum kvm_mr_change change)
5027 /* A few sanity checks. We can have memory slots which have to be
5028 located/ended at a segment boundary (1MB). The memory in userland is
5029 ok to be fragmented into various different vmas. It is okay to mmap()
5030 and munmap() stuff in this slot after doing this call at any time */
5032 if (mem->userspace_addr & 0xffffful)
5035 if (mem->memory_size & 0xffffful)
5038 if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
5041 /* When we are protected, we should not change the memory slots */
5042 if (kvm_s390_pv_get_handle(kvm))
5047 void kvm_arch_commit_memory_region(struct kvm *kvm,
5048 const struct kvm_userspace_memory_region *mem,
5049 struct kvm_memory_slot *old,
5050 const struct kvm_memory_slot *new,
5051 enum kvm_mr_change change)
5057 rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
5058 old->npages * PAGE_SIZE);
5061 rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
5062 old->npages * PAGE_SIZE);
5067 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
5068 mem->guest_phys_addr, mem->memory_size);
5070 case KVM_MR_FLAGS_ONLY:
5073 WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
5076 pr_warn("failed to commit memory region\n");
5080 static inline unsigned long nonhyp_mask(int i)
5082 unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
5084 return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
5087 void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
5089 vcpu->valid_wakeup = false;
5092 static int __init kvm_s390_init(void)
5096 if (!sclp.has_sief2) {
5097 pr_info("SIE is not available\n");
5101 if (nested && hpage) {
5102 pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
5106 for (i = 0; i < 16; i++)
5107 kvm_s390_fac_base[i] |=
5108 stfle_fac_list[i] & nonhyp_mask(i);
5110 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
5113 static void __exit kvm_s390_exit(void)
5118 module_init(kvm_s390_init);
5119 module_exit(kvm_s390_exit);
5122 * Enable autoloading of the kvm module.
5123 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5124 * since x86 takes a different approach.
5126 #include <linux/miscdevice.h>
5127 MODULE_ALIAS_MISCDEV(KVM_MINOR);
5128 MODULE_ALIAS("devname:kvm");