1 // SPDX-License-Identifier: GPL-2.0
3 * hosting IBM Z kernel virtual machines (s390x)
5 * Copyright IBM Corp. 2008, 2018
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
9 * Heiko Carstens <heiko.carstens@de.ibm.com>
10 * Christian Ehrhardt <ehrhardt@de.ibm.com>
11 * Jason J. Herne <jjherne@us.ibm.com>
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>
35 #include <asm/asm-offsets.h>
36 #include <asm/lowcore.h>
38 #include <asm/pgtable.h>
41 #include <asm/switch_to.h>
44 #include <asm/cpacf.h>
45 #include <asm/timex.h>
50 #define CREATE_TRACE_POINTS
52 #include "trace-s390.h"
54 #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
56 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
57 (KVM_MAX_VCPUS + LOCAL_IRQS))
59 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
60 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
62 struct kvm_stats_debugfs_item debugfs_entries[] = {
63 { "userspace_handled", VCPU_STAT(exit_userspace) },
64 { "exit_null", VCPU_STAT(exit_null) },
65 { "exit_validity", VCPU_STAT(exit_validity) },
66 { "exit_stop_request", VCPU_STAT(exit_stop_request) },
67 { "exit_external_request", VCPU_STAT(exit_external_request) },
68 { "exit_io_request", VCPU_STAT(exit_io_request) },
69 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
70 { "exit_instruction", VCPU_STAT(exit_instruction) },
71 { "exit_pei", VCPU_STAT(exit_pei) },
72 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
73 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
74 { "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
75 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
76 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
77 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
78 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
79 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
80 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
81 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
82 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
83 { "deliver_ckc", VCPU_STAT(deliver_ckc) },
84 { "deliver_cputm", VCPU_STAT(deliver_cputm) },
85 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
86 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
87 { "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
88 { "deliver_virtio", VCPU_STAT(deliver_virtio) },
89 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
90 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
91 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
92 { "deliver_program", VCPU_STAT(deliver_program) },
93 { "deliver_io", VCPU_STAT(deliver_io) },
94 { "deliver_machine_check", VCPU_STAT(deliver_machine_check) },
95 { "exit_wait_state", VCPU_STAT(exit_wait_state) },
96 { "inject_ckc", VCPU_STAT(inject_ckc) },
97 { "inject_cputm", VCPU_STAT(inject_cputm) },
98 { "inject_external_call", VCPU_STAT(inject_external_call) },
99 { "inject_float_mchk", VM_STAT(inject_float_mchk) },
100 { "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) },
101 { "inject_io", VM_STAT(inject_io) },
102 { "inject_mchk", VCPU_STAT(inject_mchk) },
103 { "inject_pfault_done", VM_STAT(inject_pfault_done) },
104 { "inject_program", VCPU_STAT(inject_program) },
105 { "inject_restart", VCPU_STAT(inject_restart) },
106 { "inject_service_signal", VM_STAT(inject_service_signal) },
107 { "inject_set_prefix", VCPU_STAT(inject_set_prefix) },
108 { "inject_stop_signal", VCPU_STAT(inject_stop_signal) },
109 { "inject_pfault_init", VCPU_STAT(inject_pfault_init) },
110 { "inject_virtio", VM_STAT(inject_virtio) },
111 { "instruction_epsw", VCPU_STAT(instruction_epsw) },
112 { "instruction_gs", VCPU_STAT(instruction_gs) },
113 { "instruction_io_other", VCPU_STAT(instruction_io_other) },
114 { "instruction_lpsw", VCPU_STAT(instruction_lpsw) },
115 { "instruction_lpswe", VCPU_STAT(instruction_lpswe) },
116 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
117 { "instruction_ptff", VCPU_STAT(instruction_ptff) },
118 { "instruction_stidp", VCPU_STAT(instruction_stidp) },
119 { "instruction_sck", VCPU_STAT(instruction_sck) },
120 { "instruction_sckpf", VCPU_STAT(instruction_sckpf) },
121 { "instruction_spx", VCPU_STAT(instruction_spx) },
122 { "instruction_stpx", VCPU_STAT(instruction_stpx) },
123 { "instruction_stap", VCPU_STAT(instruction_stap) },
124 { "instruction_iske", VCPU_STAT(instruction_iske) },
125 { "instruction_ri", VCPU_STAT(instruction_ri) },
126 { "instruction_rrbe", VCPU_STAT(instruction_rrbe) },
127 { "instruction_sske", VCPU_STAT(instruction_sske) },
128 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
129 { "instruction_essa", VCPU_STAT(instruction_essa) },
130 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
131 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
132 { "instruction_tb", VCPU_STAT(instruction_tb) },
133 { "instruction_tpi", VCPU_STAT(instruction_tpi) },
134 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
135 { "instruction_tsch", VCPU_STAT(instruction_tsch) },
136 { "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
137 { "instruction_sie", VCPU_STAT(instruction_sie) },
138 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
139 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
140 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
141 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
142 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
143 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
144 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
145 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
146 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
147 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
148 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
149 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
150 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
151 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
152 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
153 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
154 { "instruction_diag_10", VCPU_STAT(diagnose_10) },
155 { "instruction_diag_44", VCPU_STAT(diagnose_44) },
156 { "instruction_diag_9c", VCPU_STAT(diagnose_9c) },
157 { "instruction_diag_258", VCPU_STAT(diagnose_258) },
158 { "instruction_diag_308", VCPU_STAT(diagnose_308) },
159 { "instruction_diag_500", VCPU_STAT(diagnose_500) },
160 { "instruction_diag_other", VCPU_STAT(diagnose_other) },
164 struct kvm_s390_tod_clock_ext {
170 /* allow nested virtualization in KVM (if enabled by user space) */
172 module_param(nested, int, S_IRUGO);
173 MODULE_PARM_DESC(nested, "Nested virtualization support");
175 /* allow 1m huge page guest backing, if !nested */
177 module_param(hpage, int, 0444);
178 MODULE_PARM_DESC(hpage, "1m huge page backing support");
181 * For now we handle at most 16 double words as this is what the s390 base
182 * kernel handles and stores in the prefix page. If we ever need to go beyond
183 * this, this requires changes to code, but the external uapi can stay.
185 #define SIZE_INTERNAL 16
188 * Base feature mask that defines default mask for facilities. Consists of the
189 * defines in FACILITIES_KVM and the non-hypervisor managed bits.
191 static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
193 * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
194 * and defines the facilities that can be enabled via a cpu model.
196 static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
198 static unsigned long kvm_s390_fac_size(void)
200 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
201 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
202 BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
203 sizeof(S390_lowcore.stfle_fac_list));
205 return SIZE_INTERNAL;
208 /* available cpu features supported by kvm */
209 static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
210 /* available subfunctions indicated via query / "test bit" */
211 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
213 static struct gmap_notifier gmap_notifier;
214 static struct gmap_notifier vsie_gmap_notifier;
215 debug_info_t *kvm_s390_dbf;
217 /* Section: not file related */
218 int kvm_arch_hardware_enable(void)
220 /* every s390 is virtualization enabled ;-) */
224 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
227 static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
232 * The TOD jumps by delta, we have to compensate this by adding
233 * -delta to the epoch.
237 /* sign-extension - we're adding to signed values below */
242 if (scb->ecd & ECD_MEF) {
243 scb->epdx += delta_idx;
244 if (scb->epoch < delta)
250 * This callback is executed during stop_machine(). All CPUs are therefore
251 * temporarily stopped. In order not to change guest behavior, we have to
252 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
253 * so a CPU won't be stopped while calculating with the epoch.
255 static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
259 struct kvm_vcpu *vcpu;
261 unsigned long long *delta = v;
263 list_for_each_entry(kvm, &vm_list, vm_list) {
264 kvm_for_each_vcpu(i, vcpu, kvm) {
265 kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
267 kvm->arch.epoch = vcpu->arch.sie_block->epoch;
268 kvm->arch.epdx = vcpu->arch.sie_block->epdx;
270 if (vcpu->arch.cputm_enabled)
271 vcpu->arch.cputm_start += *delta;
272 if (vcpu->arch.vsie_block)
273 kvm_clock_sync_scb(vcpu->arch.vsie_block,
280 static struct notifier_block kvm_clock_notifier = {
281 .notifier_call = kvm_clock_sync,
284 int kvm_arch_hardware_setup(void)
286 gmap_notifier.notifier_call = kvm_gmap_notifier;
287 gmap_register_pte_notifier(&gmap_notifier);
288 vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
289 gmap_register_pte_notifier(&vsie_gmap_notifier);
290 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
291 &kvm_clock_notifier);
295 void kvm_arch_hardware_unsetup(void)
297 gmap_unregister_pte_notifier(&gmap_notifier);
298 gmap_unregister_pte_notifier(&vsie_gmap_notifier);
299 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
300 &kvm_clock_notifier);
303 static void allow_cpu_feat(unsigned long nr)
305 set_bit_inv(nr, kvm_s390_available_cpu_feat);
308 static inline int plo_test_bit(unsigned char nr)
310 register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
314 /* Parameter registers are ignored for "test bit" */
324 static void kvm_s390_cpu_feat_init(void)
328 for (i = 0; i < 256; ++i) {
330 kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
333 if (test_facility(28)) /* TOD-clock steering */
334 ptff(kvm_s390_available_subfunc.ptff,
335 sizeof(kvm_s390_available_subfunc.ptff),
338 if (test_facility(17)) { /* MSA */
339 __cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
340 kvm_s390_available_subfunc.kmac);
341 __cpacf_query(CPACF_KMC, (cpacf_mask_t *)
342 kvm_s390_available_subfunc.kmc);
343 __cpacf_query(CPACF_KM, (cpacf_mask_t *)
344 kvm_s390_available_subfunc.km);
345 __cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
346 kvm_s390_available_subfunc.kimd);
347 __cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
348 kvm_s390_available_subfunc.klmd);
350 if (test_facility(76)) /* MSA3 */
351 __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
352 kvm_s390_available_subfunc.pckmo);
353 if (test_facility(77)) { /* MSA4 */
354 __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
355 kvm_s390_available_subfunc.kmctr);
356 __cpacf_query(CPACF_KMF, (cpacf_mask_t *)
357 kvm_s390_available_subfunc.kmf);
358 __cpacf_query(CPACF_KMO, (cpacf_mask_t *)
359 kvm_s390_available_subfunc.kmo);
360 __cpacf_query(CPACF_PCC, (cpacf_mask_t *)
361 kvm_s390_available_subfunc.pcc);
363 if (test_facility(57)) /* MSA5 */
364 __cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
365 kvm_s390_available_subfunc.ppno);
367 if (test_facility(146)) /* MSA8 */
368 __cpacf_query(CPACF_KMA, (cpacf_mask_t *)
369 kvm_s390_available_subfunc.kma);
371 if (MACHINE_HAS_ESOP)
372 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
374 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
375 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
377 if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
378 !test_facility(3) || !nested)
380 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
381 if (sclp.has_64bscao)
382 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
384 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
386 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
388 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
390 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
392 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
394 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
396 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
398 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
399 * all skey handling functions read/set the skey from the PGSTE
400 * instead of the real storage key.
402 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
403 * pages being detected as preserved although they are resident.
405 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
406 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
408 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
409 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
410 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
412 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
413 * cannot easily shadow the SCA because of the ipte lock.
417 int kvm_arch_init(void *opaque)
421 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
425 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view)) {
427 goto out_debug_unreg;
430 kvm_s390_cpu_feat_init();
432 /* Register floating interrupt controller interface. */
433 rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
435 pr_err("A FLIC registration call failed with rc=%d\n", rc);
436 goto out_debug_unreg;
439 rc = kvm_s390_gib_init(GAL_ISC);
441 goto out_gib_destroy;
446 kvm_s390_gib_destroy();
448 debug_unregister(kvm_s390_dbf);
452 void kvm_arch_exit(void)
454 kvm_s390_gib_destroy();
455 debug_unregister(kvm_s390_dbf);
458 /* Section: device related */
459 long kvm_arch_dev_ioctl(struct file *filp,
460 unsigned int ioctl, unsigned long arg)
462 if (ioctl == KVM_S390_ENABLE_SIE)
463 return s390_enable_sie();
467 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
472 case KVM_CAP_S390_PSW:
473 case KVM_CAP_S390_GMAP:
474 case KVM_CAP_SYNC_MMU:
475 #ifdef CONFIG_KVM_S390_UCONTROL
476 case KVM_CAP_S390_UCONTROL:
478 case KVM_CAP_ASYNC_PF:
479 case KVM_CAP_SYNC_REGS:
480 case KVM_CAP_ONE_REG:
481 case KVM_CAP_ENABLE_CAP:
482 case KVM_CAP_S390_CSS_SUPPORT:
483 case KVM_CAP_IOEVENTFD:
484 case KVM_CAP_DEVICE_CTRL:
485 case KVM_CAP_S390_IRQCHIP:
486 case KVM_CAP_VM_ATTRIBUTES:
487 case KVM_CAP_MP_STATE:
488 case KVM_CAP_IMMEDIATE_EXIT:
489 case KVM_CAP_S390_INJECT_IRQ:
490 case KVM_CAP_S390_USER_SIGP:
491 case KVM_CAP_S390_USER_STSI:
492 case KVM_CAP_S390_SKEYS:
493 case KVM_CAP_S390_IRQ_STATE:
494 case KVM_CAP_S390_USER_INSTR0:
495 case KVM_CAP_S390_CMMA_MIGRATION:
496 case KVM_CAP_S390_AIS:
497 case KVM_CAP_S390_AIS_MIGRATION:
500 case KVM_CAP_S390_HPAGE_1M:
502 if (hpage && !kvm_is_ucontrol(kvm))
505 case KVM_CAP_S390_MEM_OP:
508 case KVM_CAP_NR_VCPUS:
509 case KVM_CAP_MAX_VCPUS:
510 r = KVM_S390_BSCA_CPU_SLOTS;
511 if (!kvm_s390_use_sca_entries())
513 else if (sclp.has_esca && sclp.has_64bscao)
514 r = KVM_S390_ESCA_CPU_SLOTS;
516 case KVM_CAP_NR_MEMSLOTS:
517 r = KVM_USER_MEM_SLOTS;
519 case KVM_CAP_S390_COW:
520 r = MACHINE_HAS_ESOP;
522 case KVM_CAP_S390_VECTOR_REGISTERS:
525 case KVM_CAP_S390_RI:
526 r = test_facility(64);
528 case KVM_CAP_S390_GS:
529 r = test_facility(133);
531 case KVM_CAP_S390_BPB:
532 r = test_facility(82);
540 static void kvm_s390_sync_dirty_log(struct kvm *kvm,
541 struct kvm_memory_slot *memslot)
544 gfn_t cur_gfn, last_gfn;
545 unsigned long gaddr, vmaddr;
546 struct gmap *gmap = kvm->arch.gmap;
547 DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
549 /* Loop over all guest segments */
550 cur_gfn = memslot->base_gfn;
551 last_gfn = memslot->base_gfn + memslot->npages;
552 for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
553 gaddr = gfn_to_gpa(cur_gfn);
554 vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
555 if (kvm_is_error_hva(vmaddr))
558 bitmap_zero(bitmap, _PAGE_ENTRIES);
559 gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
560 for (i = 0; i < _PAGE_ENTRIES; i++) {
561 if (test_bit(i, bitmap))
562 mark_page_dirty(kvm, cur_gfn + i);
565 if (fatal_signal_pending(current))
571 /* Section: vm related */
572 static void sca_del_vcpu(struct kvm_vcpu *vcpu);
575 * Get (and clear) the dirty memory log for a memory slot.
577 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
578 struct kvm_dirty_log *log)
582 struct kvm_memslots *slots;
583 struct kvm_memory_slot *memslot;
586 if (kvm_is_ucontrol(kvm))
589 mutex_lock(&kvm->slots_lock);
592 if (log->slot >= KVM_USER_MEM_SLOTS)
595 slots = kvm_memslots(kvm);
596 memslot = id_to_memslot(slots, log->slot);
598 if (!memslot->dirty_bitmap)
601 kvm_s390_sync_dirty_log(kvm, memslot);
602 r = kvm_get_dirty_log(kvm, log, &is_dirty);
606 /* Clear the dirty log */
608 n = kvm_dirty_bitmap_bytes(memslot);
609 memset(memslot->dirty_bitmap, 0, n);
613 mutex_unlock(&kvm->slots_lock);
617 static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
620 struct kvm_vcpu *vcpu;
622 kvm_for_each_vcpu(i, vcpu, kvm) {
623 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
627 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
635 case KVM_CAP_S390_IRQCHIP:
636 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
637 kvm->arch.use_irqchip = 1;
640 case KVM_CAP_S390_USER_SIGP:
641 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
642 kvm->arch.user_sigp = 1;
645 case KVM_CAP_S390_VECTOR_REGISTERS:
646 mutex_lock(&kvm->lock);
647 if (kvm->created_vcpus) {
649 } else if (MACHINE_HAS_VX) {
650 set_kvm_facility(kvm->arch.model.fac_mask, 129);
651 set_kvm_facility(kvm->arch.model.fac_list, 129);
652 if (test_facility(134)) {
653 set_kvm_facility(kvm->arch.model.fac_mask, 134);
654 set_kvm_facility(kvm->arch.model.fac_list, 134);
656 if (test_facility(135)) {
657 set_kvm_facility(kvm->arch.model.fac_mask, 135);
658 set_kvm_facility(kvm->arch.model.fac_list, 135);
663 mutex_unlock(&kvm->lock);
664 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
665 r ? "(not available)" : "(success)");
667 case KVM_CAP_S390_RI:
669 mutex_lock(&kvm->lock);
670 if (kvm->created_vcpus) {
672 } else if (test_facility(64)) {
673 set_kvm_facility(kvm->arch.model.fac_mask, 64);
674 set_kvm_facility(kvm->arch.model.fac_list, 64);
677 mutex_unlock(&kvm->lock);
678 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
679 r ? "(not available)" : "(success)");
681 case KVM_CAP_S390_AIS:
682 mutex_lock(&kvm->lock);
683 if (kvm->created_vcpus) {
686 set_kvm_facility(kvm->arch.model.fac_mask, 72);
687 set_kvm_facility(kvm->arch.model.fac_list, 72);
690 mutex_unlock(&kvm->lock);
691 VM_EVENT(kvm, 3, "ENABLE: AIS %s",
692 r ? "(not available)" : "(success)");
694 case KVM_CAP_S390_GS:
696 mutex_lock(&kvm->lock);
697 if (kvm->created_vcpus) {
699 } else if (test_facility(133)) {
700 set_kvm_facility(kvm->arch.model.fac_mask, 133);
701 set_kvm_facility(kvm->arch.model.fac_list, 133);
704 mutex_unlock(&kvm->lock);
705 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
706 r ? "(not available)" : "(success)");
708 case KVM_CAP_S390_HPAGE_1M:
709 mutex_lock(&kvm->lock);
710 if (kvm->created_vcpus)
712 else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
716 down_write(&kvm->mm->mmap_sem);
717 kvm->mm->context.allow_gmap_hpage_1m = 1;
718 up_write(&kvm->mm->mmap_sem);
720 * We might have to create fake 4k page
721 * tables. To avoid that the hardware works on
722 * stale PGSTEs, we emulate these instructions.
724 kvm->arch.use_skf = 0;
725 kvm->arch.use_pfmfi = 0;
727 mutex_unlock(&kvm->lock);
728 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
729 r ? "(not available)" : "(success)");
731 case KVM_CAP_S390_USER_STSI:
732 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
733 kvm->arch.user_stsi = 1;
736 case KVM_CAP_S390_USER_INSTR0:
737 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
738 kvm->arch.user_instr0 = 1;
739 icpt_operexc_on_all_vcpus(kvm);
749 static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
753 switch (attr->attr) {
754 case KVM_S390_VM_MEM_LIMIT_SIZE:
756 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
757 kvm->arch.mem_limit);
758 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
768 static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
772 switch (attr->attr) {
773 case KVM_S390_VM_MEM_ENABLE_CMMA:
778 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
779 mutex_lock(&kvm->lock);
780 if (kvm->created_vcpus)
782 else if (kvm->mm->context.allow_gmap_hpage_1m)
785 kvm->arch.use_cmma = 1;
786 /* Not compatible with cmma. */
787 kvm->arch.use_pfmfi = 0;
790 mutex_unlock(&kvm->lock);
792 case KVM_S390_VM_MEM_CLR_CMMA:
797 if (!kvm->arch.use_cmma)
800 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
801 mutex_lock(&kvm->lock);
802 idx = srcu_read_lock(&kvm->srcu);
803 s390_reset_cmma(kvm->arch.gmap->mm);
804 srcu_read_unlock(&kvm->srcu, idx);
805 mutex_unlock(&kvm->lock);
808 case KVM_S390_VM_MEM_LIMIT_SIZE: {
809 unsigned long new_limit;
811 if (kvm_is_ucontrol(kvm))
814 if (get_user(new_limit, (u64 __user *)attr->addr))
817 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
818 new_limit > kvm->arch.mem_limit)
824 /* gmap_create takes last usable address */
825 if (new_limit != KVM_S390_NO_MEM_LIMIT)
829 mutex_lock(&kvm->lock);
830 if (!kvm->created_vcpus) {
831 /* gmap_create will round the limit up */
832 struct gmap *new = gmap_create(current->mm, new_limit);
837 gmap_remove(kvm->arch.gmap);
839 kvm->arch.gmap = new;
843 mutex_unlock(&kvm->lock);
844 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
845 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
846 (void *) kvm->arch.gmap->asce);
856 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
858 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
860 struct kvm_vcpu *vcpu;
863 kvm_s390_vcpu_block_all(kvm);
865 kvm_for_each_vcpu(i, vcpu, kvm) {
866 kvm_s390_vcpu_crypto_setup(vcpu);
867 /* recreate the shadow crycb by leaving the VSIE handler */
868 kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
871 kvm_s390_vcpu_unblock_all(kvm);
874 static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
876 mutex_lock(&kvm->lock);
877 switch (attr->attr) {
878 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
879 if (!test_kvm_facility(kvm, 76)) {
880 mutex_unlock(&kvm->lock);
884 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
885 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
886 kvm->arch.crypto.aes_kw = 1;
887 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
889 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
890 if (!test_kvm_facility(kvm, 76)) {
891 mutex_unlock(&kvm->lock);
895 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
896 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
897 kvm->arch.crypto.dea_kw = 1;
898 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
900 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
901 if (!test_kvm_facility(kvm, 76)) {
902 mutex_unlock(&kvm->lock);
905 kvm->arch.crypto.aes_kw = 0;
906 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
907 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
908 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
910 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
911 if (!test_kvm_facility(kvm, 76)) {
912 mutex_unlock(&kvm->lock);
915 kvm->arch.crypto.dea_kw = 0;
916 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
917 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
918 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
920 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
921 if (!ap_instructions_available()) {
922 mutex_unlock(&kvm->lock);
925 kvm->arch.crypto.apie = 1;
927 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
928 if (!ap_instructions_available()) {
929 mutex_unlock(&kvm->lock);
932 kvm->arch.crypto.apie = 0;
935 mutex_unlock(&kvm->lock);
939 kvm_s390_vcpu_crypto_reset_all(kvm);
940 mutex_unlock(&kvm->lock);
944 static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
947 struct kvm_vcpu *vcpu;
949 kvm_for_each_vcpu(cx, vcpu, kvm)
950 kvm_s390_sync_request(req, vcpu);
954 * Must be called with kvm->srcu held to avoid races on memslots, and with
955 * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
957 static int kvm_s390_vm_start_migration(struct kvm *kvm)
959 struct kvm_memory_slot *ms;
960 struct kvm_memslots *slots;
961 unsigned long ram_pages = 0;
964 /* migration mode already enabled */
965 if (kvm->arch.migration_mode)
967 slots = kvm_memslots(kvm);
968 if (!slots || !slots->used_slots)
971 if (!kvm->arch.use_cmma) {
972 kvm->arch.migration_mode = 1;
975 /* mark all the pages in active slots as dirty */
976 for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
977 ms = slots->memslots + slotnr;
979 * The second half of the bitmap is only used on x86,
980 * and would be wasted otherwise, so we put it to good
981 * use here to keep track of the state of the storage
984 memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
985 ram_pages += ms->npages;
987 atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
988 kvm->arch.migration_mode = 1;
989 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
994 * Must be called with kvm->slots_lock to avoid races with ourselves and
995 * kvm_s390_vm_start_migration.
997 static int kvm_s390_vm_stop_migration(struct kvm *kvm)
999 /* migration mode already disabled */
1000 if (!kvm->arch.migration_mode)
1002 kvm->arch.migration_mode = 0;
1003 if (kvm->arch.use_cmma)
1004 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1008 static int kvm_s390_vm_set_migration(struct kvm *kvm,
1009 struct kvm_device_attr *attr)
1013 mutex_lock(&kvm->slots_lock);
1014 switch (attr->attr) {
1015 case KVM_S390_VM_MIGRATION_START:
1016 res = kvm_s390_vm_start_migration(kvm);
1018 case KVM_S390_VM_MIGRATION_STOP:
1019 res = kvm_s390_vm_stop_migration(kvm);
1024 mutex_unlock(&kvm->slots_lock);
1029 static int kvm_s390_vm_get_migration(struct kvm *kvm,
1030 struct kvm_device_attr *attr)
1032 u64 mig = kvm->arch.migration_mode;
1034 if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1037 if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1042 static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1044 struct kvm_s390_vm_tod_clock gtod;
1046 if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod)))
1049 if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
1051 kvm_s390_set_tod_clock(kvm, >od);
1053 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
1054 gtod.epoch_idx, gtod.tod);
1059 static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1063 if (copy_from_user(>od_high, (void __user *)attr->addr,
1069 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
1074 static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1076 struct kvm_s390_vm_tod_clock gtod = { 0 };
1078 if (copy_from_user(>od.tod, (void __user *)attr->addr,
1082 kvm_s390_set_tod_clock(kvm, >od);
1083 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
1087 static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1094 switch (attr->attr) {
1095 case KVM_S390_VM_TOD_EXT:
1096 ret = kvm_s390_set_tod_ext(kvm, attr);
1098 case KVM_S390_VM_TOD_HIGH:
1099 ret = kvm_s390_set_tod_high(kvm, attr);
1101 case KVM_S390_VM_TOD_LOW:
1102 ret = kvm_s390_set_tod_low(kvm, attr);
1111 static void kvm_s390_get_tod_clock(struct kvm *kvm,
1112 struct kvm_s390_vm_tod_clock *gtod)
1114 struct kvm_s390_tod_clock_ext htod;
1118 get_tod_clock_ext((char *)&htod);
1120 gtod->tod = htod.tod + kvm->arch.epoch;
1121 gtod->epoch_idx = 0;
1122 if (test_kvm_facility(kvm, 139)) {
1123 gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx;
1124 if (gtod->tod < htod.tod)
1125 gtod->epoch_idx += 1;
1131 static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1133 struct kvm_s390_vm_tod_clock gtod;
1135 memset(>od, 0, sizeof(gtod));
1136 kvm_s390_get_tod_clock(kvm, >od);
1137 if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
1140 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1141 gtod.epoch_idx, gtod.tod);
1145 static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1149 if (copy_to_user((void __user *)attr->addr, >od_high,
1152 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
1157 static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1161 gtod = kvm_s390_get_tod_clock_fast(kvm);
1162 if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
1164 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
1169 static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1176 switch (attr->attr) {
1177 case KVM_S390_VM_TOD_EXT:
1178 ret = kvm_s390_get_tod_ext(kvm, attr);
1180 case KVM_S390_VM_TOD_HIGH:
1181 ret = kvm_s390_get_tod_high(kvm, attr);
1183 case KVM_S390_VM_TOD_LOW:
1184 ret = kvm_s390_get_tod_low(kvm, attr);
1193 static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1195 struct kvm_s390_vm_cpu_processor *proc;
1196 u16 lowest_ibc, unblocked_ibc;
1199 mutex_lock(&kvm->lock);
1200 if (kvm->created_vcpus) {
1204 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1209 if (!copy_from_user(proc, (void __user *)attr->addr,
1211 kvm->arch.model.cpuid = proc->cpuid;
1212 lowest_ibc = sclp.ibc >> 16 & 0xfff;
1213 unblocked_ibc = sclp.ibc & 0xfff;
1214 if (lowest_ibc && proc->ibc) {
1215 if (proc->ibc > unblocked_ibc)
1216 kvm->arch.model.ibc = unblocked_ibc;
1217 else if (proc->ibc < lowest_ibc)
1218 kvm->arch.model.ibc = lowest_ibc;
1220 kvm->arch.model.ibc = proc->ibc;
1222 memcpy(kvm->arch.model.fac_list, proc->fac_list,
1223 S390_ARCH_FAC_LIST_SIZE_BYTE);
1224 VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1225 kvm->arch.model.ibc,
1226 kvm->arch.model.cpuid);
1227 VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1228 kvm->arch.model.fac_list[0],
1229 kvm->arch.model.fac_list[1],
1230 kvm->arch.model.fac_list[2]);
1235 mutex_unlock(&kvm->lock);
1239 static int kvm_s390_set_processor_feat(struct kvm *kvm,
1240 struct kvm_device_attr *attr)
1242 struct kvm_s390_vm_cpu_feat data;
1244 if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
1246 if (!bitmap_subset((unsigned long *) data.feat,
1247 kvm_s390_available_cpu_feat,
1248 KVM_S390_VM_CPU_FEAT_NR_BITS))
1251 mutex_lock(&kvm->lock);
1252 if (kvm->created_vcpus) {
1253 mutex_unlock(&kvm->lock);
1256 bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
1257 KVM_S390_VM_CPU_FEAT_NR_BITS);
1258 mutex_unlock(&kvm->lock);
1259 VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1266 static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
1267 struct kvm_device_attr *attr)
1269 mutex_lock(&kvm->lock);
1270 if (kvm->created_vcpus) {
1271 mutex_unlock(&kvm->lock);
1275 if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1276 sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1277 mutex_unlock(&kvm->lock);
1280 mutex_unlock(&kvm->lock);
1282 VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1283 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1284 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1285 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1286 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1287 VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1288 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1289 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1290 VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1291 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1292 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1293 VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1294 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1295 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1296 VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
1297 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1298 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1299 VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1300 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1301 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1302 VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1303 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1304 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1305 VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1306 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1307 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1308 VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1309 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1310 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1311 VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1312 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1313 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1314 VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1315 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1316 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1317 VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1318 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1319 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1320 VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1321 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1322 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1323 VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1324 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1325 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1330 static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1334 switch (attr->attr) {
1335 case KVM_S390_VM_CPU_PROCESSOR:
1336 ret = kvm_s390_set_processor(kvm, attr);
1338 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1339 ret = kvm_s390_set_processor_feat(kvm, attr);
1341 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1342 ret = kvm_s390_set_processor_subfunc(kvm, attr);
1348 static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1350 struct kvm_s390_vm_cpu_processor *proc;
1353 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1358 proc->cpuid = kvm->arch.model.cpuid;
1359 proc->ibc = kvm->arch.model.ibc;
1360 memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1361 S390_ARCH_FAC_LIST_SIZE_BYTE);
1362 VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1363 kvm->arch.model.ibc,
1364 kvm->arch.model.cpuid);
1365 VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1366 kvm->arch.model.fac_list[0],
1367 kvm->arch.model.fac_list[1],
1368 kvm->arch.model.fac_list[2]);
1369 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1376 static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1378 struct kvm_s390_vm_cpu_machine *mach;
1381 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1386 get_cpu_id((struct cpuid *) &mach->cpuid);
1387 mach->ibc = sclp.ibc;
1388 memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1389 S390_ARCH_FAC_LIST_SIZE_BYTE);
1390 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
1391 sizeof(S390_lowcore.stfle_fac_list));
1392 VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1393 kvm->arch.model.ibc,
1394 kvm->arch.model.cpuid);
1395 VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1399 VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1403 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1410 static int kvm_s390_get_processor_feat(struct kvm *kvm,
1411 struct kvm_device_attr *attr)
1413 struct kvm_s390_vm_cpu_feat data;
1415 bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
1416 KVM_S390_VM_CPU_FEAT_NR_BITS);
1417 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1419 VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1426 static int kvm_s390_get_machine_feat(struct kvm *kvm,
1427 struct kvm_device_attr *attr)
1429 struct kvm_s390_vm_cpu_feat data;
1431 bitmap_copy((unsigned long *) data.feat,
1432 kvm_s390_available_cpu_feat,
1433 KVM_S390_VM_CPU_FEAT_NR_BITS);
1434 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1436 VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1443 static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
1444 struct kvm_device_attr *attr)
1446 if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1447 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1450 VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1451 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1452 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1453 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1454 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1455 VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1456 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1457 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1458 VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1459 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1460 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1461 VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1462 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1463 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1464 VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
1465 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1466 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1467 VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1468 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1469 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1470 VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1471 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1472 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1473 VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1474 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1475 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1476 VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1477 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1478 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1479 VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1480 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1481 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1482 VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1483 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1484 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1485 VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1486 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1487 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1488 VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1489 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1490 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1491 VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1492 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1493 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1498 static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
1499 struct kvm_device_attr *attr)
1501 if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
1502 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1505 VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1506 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
1507 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
1508 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
1509 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
1510 VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
1511 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
1512 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
1513 VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
1514 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
1515 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
1516 VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
1517 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
1518 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
1519 VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
1520 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
1521 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
1522 VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
1523 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
1524 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
1525 VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
1526 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
1527 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
1528 VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
1529 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
1530 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
1531 VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
1532 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
1533 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
1534 VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
1535 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
1536 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
1537 VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
1538 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
1539 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
1540 VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
1541 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
1542 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
1543 VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
1544 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
1545 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
1546 VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
1547 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
1548 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
1553 static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1557 switch (attr->attr) {
1558 case KVM_S390_VM_CPU_PROCESSOR:
1559 ret = kvm_s390_get_processor(kvm, attr);
1561 case KVM_S390_VM_CPU_MACHINE:
1562 ret = kvm_s390_get_machine(kvm, attr);
1564 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1565 ret = kvm_s390_get_processor_feat(kvm, attr);
1567 case KVM_S390_VM_CPU_MACHINE_FEAT:
1568 ret = kvm_s390_get_machine_feat(kvm, attr);
1570 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1571 ret = kvm_s390_get_processor_subfunc(kvm, attr);
1573 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1574 ret = kvm_s390_get_machine_subfunc(kvm, attr);
1580 static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1584 switch (attr->group) {
1585 case KVM_S390_VM_MEM_CTRL:
1586 ret = kvm_s390_set_mem_control(kvm, attr);
1588 case KVM_S390_VM_TOD:
1589 ret = kvm_s390_set_tod(kvm, attr);
1591 case KVM_S390_VM_CPU_MODEL:
1592 ret = kvm_s390_set_cpu_model(kvm, attr);
1594 case KVM_S390_VM_CRYPTO:
1595 ret = kvm_s390_vm_set_crypto(kvm, attr);
1597 case KVM_S390_VM_MIGRATION:
1598 ret = kvm_s390_vm_set_migration(kvm, attr);
1608 static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1612 switch (attr->group) {
1613 case KVM_S390_VM_MEM_CTRL:
1614 ret = kvm_s390_get_mem_control(kvm, attr);
1616 case KVM_S390_VM_TOD:
1617 ret = kvm_s390_get_tod(kvm, attr);
1619 case KVM_S390_VM_CPU_MODEL:
1620 ret = kvm_s390_get_cpu_model(kvm, attr);
1622 case KVM_S390_VM_MIGRATION:
1623 ret = kvm_s390_vm_get_migration(kvm, attr);
1633 static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1637 switch (attr->group) {
1638 case KVM_S390_VM_MEM_CTRL:
1639 switch (attr->attr) {
1640 case KVM_S390_VM_MEM_ENABLE_CMMA:
1641 case KVM_S390_VM_MEM_CLR_CMMA:
1642 ret = sclp.has_cmma ? 0 : -ENXIO;
1644 case KVM_S390_VM_MEM_LIMIT_SIZE:
1652 case KVM_S390_VM_TOD:
1653 switch (attr->attr) {
1654 case KVM_S390_VM_TOD_LOW:
1655 case KVM_S390_VM_TOD_HIGH:
1663 case KVM_S390_VM_CPU_MODEL:
1664 switch (attr->attr) {
1665 case KVM_S390_VM_CPU_PROCESSOR:
1666 case KVM_S390_VM_CPU_MACHINE:
1667 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1668 case KVM_S390_VM_CPU_MACHINE_FEAT:
1669 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1670 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1678 case KVM_S390_VM_CRYPTO:
1679 switch (attr->attr) {
1680 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1681 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1682 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1683 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1686 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
1687 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
1688 ret = ap_instructions_available() ? 0 : -ENXIO;
1695 case KVM_S390_VM_MIGRATION:
1706 static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1710 int srcu_idx, i, r = 0;
1712 if (args->flags != 0)
1715 /* Is this guest using storage keys? */
1716 if (!mm_uses_skeys(current->mm))
1717 return KVM_S390_GET_SKEYS_NONE;
1719 /* Enforce sane limit on memory allocation */
1720 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1723 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
1727 down_read(¤t->mm->mmap_sem);
1728 srcu_idx = srcu_read_lock(&kvm->srcu);
1729 for (i = 0; i < args->count; i++) {
1730 hva = gfn_to_hva(kvm, args->start_gfn + i);
1731 if (kvm_is_error_hva(hva)) {
1736 r = get_guest_storage_key(current->mm, hva, &keys[i]);
1740 srcu_read_unlock(&kvm->srcu, srcu_idx);
1741 up_read(¤t->mm->mmap_sem);
1744 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
1745 sizeof(uint8_t) * args->count);
1754 static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1758 int srcu_idx, i, r = 0;
1761 if (args->flags != 0)
1764 /* Enforce sane limit on memory allocation */
1765 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1768 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
1772 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
1773 sizeof(uint8_t) * args->count);
1779 /* Enable storage key handling for the guest */
1780 r = s390_enable_skey();
1785 down_read(¤t->mm->mmap_sem);
1786 srcu_idx = srcu_read_lock(&kvm->srcu);
1787 while (i < args->count) {
1789 hva = gfn_to_hva(kvm, args->start_gfn + i);
1790 if (kvm_is_error_hva(hva)) {
1795 /* Lowest order bit is reserved */
1796 if (keys[i] & 0x01) {
1801 r = set_guest_storage_key(current->mm, hva, keys[i], 0);
1803 r = fixup_user_fault(current, current->mm, hva,
1804 FAULT_FLAG_WRITE, &unlocked);
1811 srcu_read_unlock(&kvm->srcu, srcu_idx);
1812 up_read(¤t->mm->mmap_sem);
1819 * Base address and length must be sent at the start of each block, therefore
1820 * it's cheaper to send some clean data, as long as it's less than the size of
1823 #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
1824 /* for consistency */
1825 #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
1828 * Similar to gfn_to_memslot, but returns the index of a memslot also when the
1829 * address falls in a hole. In that case the index of one of the memslots
1830 * bordering the hole is returned.
1832 static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn)
1834 int start = 0, end = slots->used_slots;
1835 int slot = atomic_read(&slots->lru_slot);
1836 struct kvm_memory_slot *memslots = slots->memslots;
1838 if (gfn >= memslots[slot].base_gfn &&
1839 gfn < memslots[slot].base_gfn + memslots[slot].npages)
1842 while (start < end) {
1843 slot = start + (end - start) / 2;
1845 if (gfn >= memslots[slot].base_gfn)
1851 if (gfn >= memslots[start].base_gfn &&
1852 gfn < memslots[start].base_gfn + memslots[start].npages) {
1853 atomic_set(&slots->lru_slot, start);
1859 static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1860 u8 *res, unsigned long bufsize)
1862 unsigned long pgstev, hva, cur_gfn = args->start_gfn;
1865 while (args->count < bufsize) {
1866 hva = gfn_to_hva(kvm, cur_gfn);
1868 * We return an error if the first value was invalid, but we
1869 * return successfully if at least one value was copied.
1871 if (kvm_is_error_hva(hva))
1872 return args->count ? 0 : -EFAULT;
1873 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
1875 res[args->count++] = (pgstev >> 24) & 0x43;
1882 static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
1883 unsigned long cur_gfn)
1885 int slotidx = gfn_to_memslot_approx(slots, cur_gfn);
1886 struct kvm_memory_slot *ms = slots->memslots + slotidx;
1887 unsigned long ofs = cur_gfn - ms->base_gfn;
1889 if (ms->base_gfn + ms->npages <= cur_gfn) {
1891 /* If we are above the highest slot, wrap around */
1893 slotidx = slots->used_slots - 1;
1895 ms = slots->memslots + slotidx;
1898 ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
1899 while ((slotidx > 0) && (ofs >= ms->npages)) {
1901 ms = slots->memslots + slotidx;
1902 ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0);
1904 return ms->base_gfn + ofs;
1907 static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1908 u8 *res, unsigned long bufsize)
1910 unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
1911 struct kvm_memslots *slots = kvm_memslots(kvm);
1912 struct kvm_memory_slot *ms;
1914 cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
1915 ms = gfn_to_memslot(kvm, cur_gfn);
1917 args->start_gfn = cur_gfn;
1920 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
1921 mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages;
1923 while (args->count < bufsize) {
1924 hva = gfn_to_hva(kvm, cur_gfn);
1925 if (kvm_is_error_hva(hva))
1927 /* Decrement only if we actually flipped the bit to 0 */
1928 if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
1929 atomic64_dec(&kvm->arch.cmma_dirty_pages);
1930 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
1932 /* Save the value */
1933 res[args->count++] = (pgstev >> 24) & 0x43;
1934 /* If the next bit is too far away, stop. */
1935 if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
1937 /* If we reached the previous "next", find the next one */
1938 if (cur_gfn == next_gfn)
1939 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
1940 /* Reached the end of memory or of the buffer, stop */
1941 if ((next_gfn >= mem_end) ||
1942 (next_gfn - args->start_gfn >= bufsize))
1945 /* Reached the end of the current memslot, take the next one. */
1946 if (cur_gfn - ms->base_gfn >= ms->npages) {
1947 ms = gfn_to_memslot(kvm, cur_gfn);
1956 * This function searches for the next page with dirty CMMA attributes, and
1957 * saves the attributes in the buffer up to either the end of the buffer or
1958 * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
1959 * no trailing clean bytes are saved.
1960 * In case no dirty bits were found, or if CMMA was not enabled or used, the
1961 * output buffer will indicate 0 as length.
1963 static int kvm_s390_get_cmma_bits(struct kvm *kvm,
1964 struct kvm_s390_cmma_log *args)
1966 unsigned long bufsize;
1967 int srcu_idx, peek, ret;
1970 if (!kvm->arch.use_cmma)
1972 /* Invalid/unsupported flags were specified */
1973 if (args->flags & ~KVM_S390_CMMA_PEEK)
1975 /* Migration mode query, and we are not doing a migration */
1976 peek = !!(args->flags & KVM_S390_CMMA_PEEK);
1977 if (!peek && !kvm->arch.migration_mode)
1979 /* CMMA is disabled or was not used, or the buffer has length zero */
1980 bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
1981 if (!bufsize || !kvm->mm->context.uses_cmm) {
1982 memset(args, 0, sizeof(*args));
1985 /* We are not peeking, and there are no dirty pages */
1986 if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
1987 memset(args, 0, sizeof(*args));
1991 values = vmalloc(bufsize);
1995 down_read(&kvm->mm->mmap_sem);
1996 srcu_idx = srcu_read_lock(&kvm->srcu);
1998 ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2000 ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
2001 srcu_read_unlock(&kvm->srcu, srcu_idx);
2002 up_read(&kvm->mm->mmap_sem);
2004 if (kvm->arch.migration_mode)
2005 args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2007 args->remaining = 0;
2009 if (copy_to_user((void __user *)args->values, values, args->count))
2017 * This function sets the CMMA attributes for the given pages. If the input
2018 * buffer has zero length, no action is taken, otherwise the attributes are
2019 * set and the mm->context.uses_cmm flag is set.
2021 static int kvm_s390_set_cmma_bits(struct kvm *kvm,
2022 const struct kvm_s390_cmma_log *args)
2024 unsigned long hva, mask, pgstev, i;
2026 int srcu_idx, r = 0;
2030 if (!kvm->arch.use_cmma)
2032 /* invalid/unsupported flags */
2033 if (args->flags != 0)
2035 /* Enforce sane limit on memory allocation */
2036 if (args->count > KVM_S390_CMMA_SIZE_MAX)
2039 if (args->count == 0)
2042 bits = vmalloc(array_size(sizeof(*bits), args->count));
2046 r = copy_from_user(bits, (void __user *)args->values, args->count);
2052 down_read(&kvm->mm->mmap_sem);
2053 srcu_idx = srcu_read_lock(&kvm->srcu);
2054 for (i = 0; i < args->count; i++) {
2055 hva = gfn_to_hva(kvm, args->start_gfn + i);
2056 if (kvm_is_error_hva(hva)) {
2062 pgstev = pgstev << 24;
2063 mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
2064 set_pgste_bits(kvm->mm, hva, mask, pgstev);
2066 srcu_read_unlock(&kvm->srcu, srcu_idx);
2067 up_read(&kvm->mm->mmap_sem);
2069 if (!kvm->mm->context.uses_cmm) {
2070 down_write(&kvm->mm->mmap_sem);
2071 kvm->mm->context.uses_cmm = 1;
2072 up_write(&kvm->mm->mmap_sem);
2079 long kvm_arch_vm_ioctl(struct file *filp,
2080 unsigned int ioctl, unsigned long arg)
2082 struct kvm *kvm = filp->private_data;
2083 void __user *argp = (void __user *)arg;
2084 struct kvm_device_attr attr;
2088 case KVM_S390_INTERRUPT: {
2089 struct kvm_s390_interrupt s390int;
2092 if (copy_from_user(&s390int, argp, sizeof(s390int)))
2094 r = kvm_s390_inject_vm(kvm, &s390int);
2097 case KVM_CREATE_IRQCHIP: {
2098 struct kvm_irq_routing_entry routing;
2101 if (kvm->arch.use_irqchip) {
2102 /* Set up dummy routing. */
2103 memset(&routing, 0, sizeof(routing));
2104 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
2108 case KVM_SET_DEVICE_ATTR: {
2110 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2112 r = kvm_s390_vm_set_attr(kvm, &attr);
2115 case KVM_GET_DEVICE_ATTR: {
2117 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2119 r = kvm_s390_vm_get_attr(kvm, &attr);
2122 case KVM_HAS_DEVICE_ATTR: {
2124 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2126 r = kvm_s390_vm_has_attr(kvm, &attr);
2129 case KVM_S390_GET_SKEYS: {
2130 struct kvm_s390_skeys args;
2133 if (copy_from_user(&args, argp,
2134 sizeof(struct kvm_s390_skeys)))
2136 r = kvm_s390_get_skeys(kvm, &args);
2139 case KVM_S390_SET_SKEYS: {
2140 struct kvm_s390_skeys args;
2143 if (copy_from_user(&args, argp,
2144 sizeof(struct kvm_s390_skeys)))
2146 r = kvm_s390_set_skeys(kvm, &args);
2149 case KVM_S390_GET_CMMA_BITS: {
2150 struct kvm_s390_cmma_log args;
2153 if (copy_from_user(&args, argp, sizeof(args)))
2155 mutex_lock(&kvm->slots_lock);
2156 r = kvm_s390_get_cmma_bits(kvm, &args);
2157 mutex_unlock(&kvm->slots_lock);
2159 r = copy_to_user(argp, &args, sizeof(args));
2165 case KVM_S390_SET_CMMA_BITS: {
2166 struct kvm_s390_cmma_log args;
2169 if (copy_from_user(&args, argp, sizeof(args)))
2171 mutex_lock(&kvm->slots_lock);
2172 r = kvm_s390_set_cmma_bits(kvm, &args);
2173 mutex_unlock(&kvm->slots_lock);
2183 static int kvm_s390_apxa_installed(void)
2185 struct ap_config_info info;
2187 if (ap_instructions_available()) {
2188 if (ap_qci(&info) == 0)
2196 * The format of the crypto control block (CRYCB) is specified in the 3 low
2197 * order bits of the CRYCB designation (CRYCBD) field as follows:
2198 * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2199 * AP extended addressing (APXA) facility are installed.
2200 * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2201 * Format 2: Both the APXA and MSAX3 facilities are installed
2203 static void kvm_s390_set_crycb_format(struct kvm *kvm)
2205 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
2207 /* Clear the CRYCB format bits - i.e., set format 0 by default */
2208 kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2210 /* Check whether MSAX3 is installed */
2211 if (!test_kvm_facility(kvm, 76))
2214 if (kvm_s390_apxa_installed())
2215 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
2217 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
2220 void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
2221 unsigned long *aqm, unsigned long *adm)
2223 struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
2225 mutex_lock(&kvm->lock);
2226 kvm_s390_vcpu_block_all(kvm);
2228 switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
2229 case CRYCB_FORMAT2: /* APCB1 use 256 bits */
2230 memcpy(crycb->apcb1.apm, apm, 32);
2231 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
2232 apm[0], apm[1], apm[2], apm[3]);
2233 memcpy(crycb->apcb1.aqm, aqm, 32);
2234 VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
2235 aqm[0], aqm[1], aqm[2], aqm[3]);
2236 memcpy(crycb->apcb1.adm, adm, 32);
2237 VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
2238 adm[0], adm[1], adm[2], adm[3]);
2241 case CRYCB_FORMAT0: /* Fall through both use APCB0 */
2242 memcpy(crycb->apcb0.apm, apm, 8);
2243 memcpy(crycb->apcb0.aqm, aqm, 2);
2244 memcpy(crycb->apcb0.adm, adm, 2);
2245 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
2246 apm[0], *((unsigned short *)aqm),
2247 *((unsigned short *)adm));
2249 default: /* Can not happen */
2253 /* recreate the shadow crycb for each vcpu */
2254 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2255 kvm_s390_vcpu_unblock_all(kvm);
2256 mutex_unlock(&kvm->lock);
2258 EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
2260 void kvm_arch_crypto_clear_masks(struct kvm *kvm)
2262 mutex_lock(&kvm->lock);
2263 kvm_s390_vcpu_block_all(kvm);
2265 memset(&kvm->arch.crypto.crycb->apcb0, 0,
2266 sizeof(kvm->arch.crypto.crycb->apcb0));
2267 memset(&kvm->arch.crypto.crycb->apcb1, 0,
2268 sizeof(kvm->arch.crypto.crycb->apcb1));
2270 VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
2271 /* recreate the shadow crycb for each vcpu */
2272 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2273 kvm_s390_vcpu_unblock_all(kvm);
2274 mutex_unlock(&kvm->lock);
2276 EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
2278 static u64 kvm_s390_get_initial_cpuid(void)
2283 cpuid.version = 0xff;
2284 return *((u64 *) &cpuid);
2287 static void kvm_s390_crypto_init(struct kvm *kvm)
2289 kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
2290 kvm_s390_set_crycb_format(kvm);
2292 if (!test_kvm_facility(kvm, 76))
2295 /* Enable AES/DEA protected key functions by default */
2296 kvm->arch.crypto.aes_kw = 1;
2297 kvm->arch.crypto.dea_kw = 1;
2298 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
2299 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
2300 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
2301 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
2304 static void sca_dispose(struct kvm *kvm)
2306 if (kvm->arch.use_esca)
2307 free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
2309 free_page((unsigned long)(kvm->arch.sca));
2310 kvm->arch.sca = NULL;
2313 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
2315 gfp_t alloc_flags = GFP_KERNEL;
2317 char debug_name[16];
2318 static unsigned long sca_offset;
2321 #ifdef CONFIG_KVM_S390_UCONTROL
2322 if (type & ~KVM_VM_S390_UCONTROL)
2324 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
2331 rc = s390_enable_sie();
2337 if (!sclp.has_64bscao)
2338 alloc_flags |= GFP_DMA;
2339 rwlock_init(&kvm->arch.sca_lock);
2340 /* start with basic SCA */
2341 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
2344 spin_lock(&kvm_lock);
2346 if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2348 kvm->arch.sca = (struct bsca_block *)
2349 ((char *) kvm->arch.sca + sca_offset);
2350 spin_unlock(&kvm_lock);
2352 sprintf(debug_name, "kvm-%u", current->pid);
2354 kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2358 BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
2359 kvm->arch.sie_page2 =
2360 (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
2361 if (!kvm->arch.sie_page2)
2364 kvm->arch.sie_page2->kvm = kvm;
2365 kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2367 for (i = 0; i < kvm_s390_fac_size(); i++) {
2368 kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] &
2369 (kvm_s390_fac_base[i] |
2370 kvm_s390_fac_ext[i]);
2371 kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] &
2372 kvm_s390_fac_base[i];
2374 kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
2376 /* we are always in czam mode - even on pre z14 machines */
2377 set_kvm_facility(kvm->arch.model.fac_mask, 138);
2378 set_kvm_facility(kvm->arch.model.fac_list, 138);
2379 /* we emulate STHYI in kvm */
2380 set_kvm_facility(kvm->arch.model.fac_mask, 74);
2381 set_kvm_facility(kvm->arch.model.fac_list, 74);
2382 if (MACHINE_HAS_TLB_GUEST) {
2383 set_kvm_facility(kvm->arch.model.fac_mask, 147);
2384 set_kvm_facility(kvm->arch.model.fac_list, 147);
2387 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
2388 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
2390 kvm_s390_crypto_init(kvm);
2392 mutex_init(&kvm->arch.float_int.ais_lock);
2393 spin_lock_init(&kvm->arch.float_int.lock);
2394 for (i = 0; i < FIRQ_LIST_COUNT; i++)
2395 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
2396 init_waitqueue_head(&kvm->arch.ipte_wq);
2397 mutex_init(&kvm->arch.ipte_mutex);
2399 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
2400 VM_EVENT(kvm, 3, "vm created with type %lu", type);
2402 if (type & KVM_VM_S390_UCONTROL) {
2403 kvm->arch.gmap = NULL;
2404 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2406 if (sclp.hamax == U64_MAX)
2407 kvm->arch.mem_limit = TASK_SIZE_MAX;
2409 kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
2411 kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2412 if (!kvm->arch.gmap)
2414 kvm->arch.gmap->private = kvm;
2415 kvm->arch.gmap->pfault_enabled = 0;
2418 kvm->arch.use_pfmfi = sclp.has_pfmfi;
2419 kvm->arch.use_skf = sclp.has_skey;
2420 spin_lock_init(&kvm->arch.start_stop_lock);
2421 kvm_s390_vsie_init(kvm);
2422 kvm_s390_gisa_init(kvm);
2423 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
2427 free_page((unsigned long)kvm->arch.sie_page2);
2428 debug_unregister(kvm->arch.dbf);
2430 KVM_EVENT(3, "creation of vm failed: %d", rc);
2434 bool kvm_arch_has_vcpu_debugfs(void)
2439 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
2444 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2446 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2447 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
2448 kvm_s390_clear_local_irqs(vcpu);
2449 kvm_clear_async_pf_completion_queue(vcpu);
2450 if (!kvm_is_ucontrol(vcpu->kvm))
2453 if (kvm_is_ucontrol(vcpu->kvm))
2454 gmap_remove(vcpu->arch.gmap);
2456 if (vcpu->kvm->arch.use_cmma)
2457 kvm_s390_vcpu_unsetup_cmma(vcpu);
2458 free_page((unsigned long)(vcpu->arch.sie_block));
2460 kvm_vcpu_uninit(vcpu);
2461 kmem_cache_free(kvm_vcpu_cache, vcpu);
2464 static void kvm_free_vcpus(struct kvm *kvm)
2467 struct kvm_vcpu *vcpu;
2469 kvm_for_each_vcpu(i, vcpu, kvm)
2470 kvm_arch_vcpu_destroy(vcpu);
2472 mutex_lock(&kvm->lock);
2473 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2474 kvm->vcpus[i] = NULL;
2476 atomic_set(&kvm->online_vcpus, 0);
2477 mutex_unlock(&kvm->lock);
2480 void kvm_arch_destroy_vm(struct kvm *kvm)
2482 kvm_free_vcpus(kvm);
2484 debug_unregister(kvm->arch.dbf);
2485 kvm_s390_gisa_destroy(kvm);
2486 free_page((unsigned long)kvm->arch.sie_page2);
2487 if (!kvm_is_ucontrol(kvm))
2488 gmap_remove(kvm->arch.gmap);
2489 kvm_s390_destroy_adapters(kvm);
2490 kvm_s390_clear_float_irqs(kvm);
2491 kvm_s390_vsie_destroy(kvm);
2492 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2495 /* Section: vcpu related */
2496 static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2498 vcpu->arch.gmap = gmap_create(current->mm, -1UL);
2499 if (!vcpu->arch.gmap)
2501 vcpu->arch.gmap->private = vcpu->kvm;
2506 static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2508 if (!kvm_s390_use_sca_entries())
2510 read_lock(&vcpu->kvm->arch.sca_lock);
2511 if (vcpu->kvm->arch.use_esca) {
2512 struct esca_block *sca = vcpu->kvm->arch.sca;
2514 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2515 sca->cpu[vcpu->vcpu_id].sda = 0;
2517 struct bsca_block *sca = vcpu->kvm->arch.sca;
2519 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2520 sca->cpu[vcpu->vcpu_id].sda = 0;
2522 read_unlock(&vcpu->kvm->arch.sca_lock);
2525 static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2527 if (!kvm_s390_use_sca_entries()) {
2528 struct bsca_block *sca = vcpu->kvm->arch.sca;
2530 /* we still need the basic sca for the ipte control */
2531 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2532 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2535 read_lock(&vcpu->kvm->arch.sca_lock);
2536 if (vcpu->kvm->arch.use_esca) {
2537 struct esca_block *sca = vcpu->kvm->arch.sca;
2539 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2540 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2541 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
2542 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2543 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2545 struct bsca_block *sca = vcpu->kvm->arch.sca;
2547 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2548 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2549 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2550 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2552 read_unlock(&vcpu->kvm->arch.sca_lock);
2555 /* Basic SCA to Extended SCA data copy routines */
2556 static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
2559 d->sigp_ctrl.c = s->sigp_ctrl.c;
2560 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
2563 static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
2567 d->ipte_control = s->ipte_control;
2569 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
2570 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
2573 static int sca_switch_to_extended(struct kvm *kvm)
2575 struct bsca_block *old_sca = kvm->arch.sca;
2576 struct esca_block *new_sca;
2577 struct kvm_vcpu *vcpu;
2578 unsigned int vcpu_idx;
2581 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
2585 scaoh = (u32)((u64)(new_sca) >> 32);
2586 scaol = (u32)(u64)(new_sca) & ~0x3fU;
2588 kvm_s390_vcpu_block_all(kvm);
2589 write_lock(&kvm->arch.sca_lock);
2591 sca_copy_b_to_e(new_sca, old_sca);
2593 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
2594 vcpu->arch.sie_block->scaoh = scaoh;
2595 vcpu->arch.sie_block->scaol = scaol;
2596 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2598 kvm->arch.sca = new_sca;
2599 kvm->arch.use_esca = 1;
2601 write_unlock(&kvm->arch.sca_lock);
2602 kvm_s390_vcpu_unblock_all(kvm);
2604 free_page((unsigned long)old_sca);
2606 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
2607 old_sca, kvm->arch.sca);
2611 static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2615 if (!kvm_s390_use_sca_entries()) {
2616 if (id < KVM_MAX_VCPUS)
2620 if (id < KVM_S390_BSCA_CPU_SLOTS)
2622 if (!sclp.has_esca || !sclp.has_64bscao)
2625 mutex_lock(&kvm->lock);
2626 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
2627 mutex_unlock(&kvm->lock);
2629 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2632 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
2634 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
2635 kvm_clear_async_pf_completion_queue(vcpu);
2636 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
2642 kvm_s390_set_prefix(vcpu, 0);
2643 if (test_kvm_facility(vcpu->kvm, 64))
2644 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
2645 if (test_kvm_facility(vcpu->kvm, 82))
2646 vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
2647 if (test_kvm_facility(vcpu->kvm, 133))
2648 vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
2649 if (test_kvm_facility(vcpu->kvm, 156))
2650 vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
2651 /* fprs can be synchronized via vrs, even if the guest has no vx. With
2652 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
2655 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
2657 vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
2659 if (kvm_is_ucontrol(vcpu->kvm))
2660 return __kvm_ucontrol_vcpu_init(vcpu);
2665 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2666 static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2668 WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
2669 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2670 vcpu->arch.cputm_start = get_tod_clock_fast();
2671 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2674 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2675 static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2677 WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
2678 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2679 vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2680 vcpu->arch.cputm_start = 0;
2681 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2684 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2685 static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2687 WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2688 vcpu->arch.cputm_enabled = true;
2689 __start_cpu_timer_accounting(vcpu);
2692 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2693 static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2695 WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2696 __stop_cpu_timer_accounting(vcpu);
2697 vcpu->arch.cputm_enabled = false;
2700 static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2702 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2703 __enable_cpu_timer_accounting(vcpu);
2707 static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2709 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2710 __disable_cpu_timer_accounting(vcpu);
2714 /* set the cpu timer - may only be called from the VCPU thread itself */
2715 void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
2717 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2718 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2719 if (vcpu->arch.cputm_enabled)
2720 vcpu->arch.cputm_start = get_tod_clock_fast();
2721 vcpu->arch.sie_block->cputm = cputm;
2722 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2726 /* update and get the cpu timer - can also be called from other VCPU threads */
2727 __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
2732 if (unlikely(!vcpu->arch.cputm_enabled))
2733 return vcpu->arch.sie_block->cputm;
2735 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2737 seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
2739 * If the writer would ever execute a read in the critical
2740 * section, e.g. in irq context, we have a deadlock.
2742 WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
2743 value = vcpu->arch.sie_block->cputm;
2744 /* if cputm_start is 0, accounting is being started/stopped */
2745 if (likely(vcpu->arch.cputm_start))
2746 value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2747 } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
2752 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2755 gmap_enable(vcpu->arch.enabled_gmap);
2756 kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
2757 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2758 __start_cpu_timer_accounting(vcpu);
2762 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
2765 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
2766 __stop_cpu_timer_accounting(vcpu);
2767 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
2768 vcpu->arch.enabled_gmap = gmap_get_enabled();
2769 gmap_disable(vcpu->arch.enabled_gmap);
2773 static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu *vcpu)
2775 /* this equals initial cpu reset in pop, but we don't switch to ESA */
2776 vcpu->arch.sie_block->gpsw.mask = 0UL;
2777 vcpu->arch.sie_block->gpsw.addr = 0UL;
2778 kvm_s390_set_prefix(vcpu, 0);
2779 kvm_s390_set_cpu_timer(vcpu, 0);
2780 vcpu->arch.sie_block->ckc = 0UL;
2781 vcpu->arch.sie_block->todpr = 0;
2782 memset(vcpu->arch.sie_block->gcr, 0, 16 * sizeof(__u64));
2783 vcpu->arch.sie_block->gcr[0] = CR0_UNUSED_56 |
2784 CR0_INTERRUPT_KEY_SUBMASK |
2785 CR0_MEASUREMENT_ALERT_SUBMASK;
2786 vcpu->arch.sie_block->gcr[14] = CR14_UNUSED_32 |
2788 CR14_EXTERNAL_DAMAGE_SUBMASK;
2789 /* make sure the new fpc will be lazily loaded */
2791 current->thread.fpu.fpc = 0;
2792 vcpu->arch.sie_block->gbea = 1;
2793 vcpu->arch.sie_block->pp = 0;
2794 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
2795 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
2796 kvm_clear_async_pf_completion_queue(vcpu);
2797 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
2798 kvm_s390_vcpu_stop(vcpu);
2799 kvm_s390_clear_local_irqs(vcpu);
2802 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
2804 mutex_lock(&vcpu->kvm->lock);
2806 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
2807 vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
2809 mutex_unlock(&vcpu->kvm->lock);
2810 if (!kvm_is_ucontrol(vcpu->kvm)) {
2811 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
2814 if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
2815 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
2816 /* make vcpu_load load the right gmap on the first trigger */
2817 vcpu->arch.enabled_gmap = vcpu->arch.gmap;
2820 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
2823 * If the AP instructions are not being interpreted and the MSAX3
2824 * facility is not configured for the guest, there is nothing to set up.
2826 if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
2829 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
2830 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
2831 vcpu->arch.sie_block->eca &= ~ECA_APIE;
2833 if (vcpu->kvm->arch.crypto.apie)
2834 vcpu->arch.sie_block->eca |= ECA_APIE;
2836 /* Set up protected key support */
2837 if (vcpu->kvm->arch.crypto.aes_kw)
2838 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
2839 if (vcpu->kvm->arch.crypto.dea_kw)
2840 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
2843 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
2845 free_page(vcpu->arch.sie_block->cbrlo);
2846 vcpu->arch.sie_block->cbrlo = 0;
2849 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
2851 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
2852 if (!vcpu->arch.sie_block->cbrlo)
2857 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
2859 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
2861 vcpu->arch.sie_block->ibc = model->ibc;
2862 if (test_kvm_facility(vcpu->kvm, 7))
2863 vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
2866 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
2870 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
2874 if (test_kvm_facility(vcpu->kvm, 78))
2875 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
2876 else if (test_kvm_facility(vcpu->kvm, 8))
2877 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
2879 kvm_s390_vcpu_setup_model(vcpu);
2881 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2882 if (MACHINE_HAS_ESOP)
2883 vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
2884 if (test_kvm_facility(vcpu->kvm, 9))
2885 vcpu->arch.sie_block->ecb |= ECB_SRSI;
2886 if (test_kvm_facility(vcpu->kvm, 73))
2887 vcpu->arch.sie_block->ecb |= ECB_TE;
2889 if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
2890 vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
2891 if (test_kvm_facility(vcpu->kvm, 130))
2892 vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
2893 vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
2895 vcpu->arch.sie_block->eca |= ECA_CEI;
2897 vcpu->arch.sie_block->eca |= ECA_IB;
2899 vcpu->arch.sie_block->eca |= ECA_SII;
2900 if (sclp.has_sigpif)
2901 vcpu->arch.sie_block->eca |= ECA_SIGPI;
2902 if (test_kvm_facility(vcpu->kvm, 129)) {
2903 vcpu->arch.sie_block->eca |= ECA_VX;
2904 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
2906 if (test_kvm_facility(vcpu->kvm, 139))
2907 vcpu->arch.sie_block->ecd |= ECD_MEF;
2908 if (test_kvm_facility(vcpu->kvm, 156))
2909 vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
2910 if (vcpu->arch.sie_block->gd) {
2911 vcpu->arch.sie_block->eca |= ECA_AIV;
2912 VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
2913 vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
2915 vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
2917 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
2920 kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
2922 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
2924 if (vcpu->kvm->arch.use_cmma) {
2925 rc = kvm_s390_vcpu_setup_cmma(vcpu);
2929 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2930 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
2932 vcpu->arch.sie_block->hpid = HPID_KVM;
2934 kvm_s390_vcpu_crypto_setup(vcpu);
2939 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
2942 struct kvm_vcpu *vcpu;
2943 struct sie_page *sie_page;
2946 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
2951 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2955 BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
2956 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
2960 vcpu->arch.sie_block = &sie_page->sie_block;
2961 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
2963 /* the real guest size will always be smaller than msl */
2964 vcpu->arch.sie_block->mso = 0;
2965 vcpu->arch.sie_block->msl = sclp.hamax;
2967 vcpu->arch.sie_block->icpua = id;
2968 spin_lock_init(&vcpu->arch.local_int.lock);
2969 vcpu->arch.sie_block->gd = (u32)(u64)kvm->arch.gisa_int.origin;
2970 if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
2971 vcpu->arch.sie_block->gd |= GISA_FORMAT1;
2972 seqcount_init(&vcpu->arch.cputm_seqcount);
2974 rc = kvm_vcpu_init(vcpu, kvm, id);
2976 goto out_free_sie_block;
2977 VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
2978 vcpu->arch.sie_block);
2979 trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
2983 free_page((unsigned long)(vcpu->arch.sie_block));
2985 kmem_cache_free(kvm_vcpu_cache, vcpu);
2990 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
2992 return kvm_s390_vcpu_has_irq(vcpu, 0);
2995 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
2997 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3000 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
3002 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3006 void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
3008 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3011 static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
3013 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3017 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
3019 return atomic_read(&vcpu->arch.sie_block->prog20) &
3020 (PROG_BLOCK_SIE | PROG_REQUEST);
3023 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
3025 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3029 * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
3030 * If the CPU is not running (e.g. waiting as idle) the function will
3031 * return immediately. */
3032 void exit_sie(struct kvm_vcpu *vcpu)
3034 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
3035 kvm_s390_vsie_kick(vcpu);
3036 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
3040 /* Kick a guest cpu out of SIE to process a request synchronously */
3041 void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
3043 kvm_make_request(req, vcpu);
3044 kvm_s390_vcpu_request(vcpu);
3047 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
3050 struct kvm *kvm = gmap->private;
3051 struct kvm_vcpu *vcpu;
3052 unsigned long prefix;
3055 if (gmap_is_shadow(gmap))
3057 if (start >= 1UL << 31)
3058 /* We are only interested in prefix pages */
3060 kvm_for_each_vcpu(i, vcpu, kvm) {
3061 /* match against both prefix pages */
3062 prefix = kvm_s390_get_prefix(vcpu);
3063 if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3064 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3066 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
3071 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3073 /* kvm common code refers to this, but never calls it */
3078 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
3079 struct kvm_one_reg *reg)
3084 case KVM_REG_S390_TODPR:
3085 r = put_user(vcpu->arch.sie_block->todpr,
3086 (u32 __user *)reg->addr);
3088 case KVM_REG_S390_EPOCHDIFF:
3089 r = put_user(vcpu->arch.sie_block->epoch,
3090 (u64 __user *)reg->addr);
3092 case KVM_REG_S390_CPU_TIMER:
3093 r = put_user(kvm_s390_get_cpu_timer(vcpu),
3094 (u64 __user *)reg->addr);
3096 case KVM_REG_S390_CLOCK_COMP:
3097 r = put_user(vcpu->arch.sie_block->ckc,
3098 (u64 __user *)reg->addr);
3100 case KVM_REG_S390_PFTOKEN:
3101 r = put_user(vcpu->arch.pfault_token,
3102 (u64 __user *)reg->addr);
3104 case KVM_REG_S390_PFCOMPARE:
3105 r = put_user(vcpu->arch.pfault_compare,
3106 (u64 __user *)reg->addr);
3108 case KVM_REG_S390_PFSELECT:
3109 r = put_user(vcpu->arch.pfault_select,
3110 (u64 __user *)reg->addr);
3112 case KVM_REG_S390_PP:
3113 r = put_user(vcpu->arch.sie_block->pp,
3114 (u64 __user *)reg->addr);
3116 case KVM_REG_S390_GBEA:
3117 r = put_user(vcpu->arch.sie_block->gbea,
3118 (u64 __user *)reg->addr);
3127 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
3128 struct kvm_one_reg *reg)
3134 case KVM_REG_S390_TODPR:
3135 r = get_user(vcpu->arch.sie_block->todpr,
3136 (u32 __user *)reg->addr);
3138 case KVM_REG_S390_EPOCHDIFF:
3139 r = get_user(vcpu->arch.sie_block->epoch,
3140 (u64 __user *)reg->addr);
3142 case KVM_REG_S390_CPU_TIMER:
3143 r = get_user(val, (u64 __user *)reg->addr);
3145 kvm_s390_set_cpu_timer(vcpu, val);
3147 case KVM_REG_S390_CLOCK_COMP:
3148 r = get_user(vcpu->arch.sie_block->ckc,
3149 (u64 __user *)reg->addr);
3151 case KVM_REG_S390_PFTOKEN:
3152 r = get_user(vcpu->arch.pfault_token,
3153 (u64 __user *)reg->addr);
3154 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3155 kvm_clear_async_pf_completion_queue(vcpu);
3157 case KVM_REG_S390_PFCOMPARE:
3158 r = get_user(vcpu->arch.pfault_compare,
3159 (u64 __user *)reg->addr);
3161 case KVM_REG_S390_PFSELECT:
3162 r = get_user(vcpu->arch.pfault_select,
3163 (u64 __user *)reg->addr);
3165 case KVM_REG_S390_PP:
3166 r = get_user(vcpu->arch.sie_block->pp,
3167 (u64 __user *)reg->addr);
3169 case KVM_REG_S390_GBEA:
3170 r = get_user(vcpu->arch.sie_block->gbea,
3171 (u64 __user *)reg->addr);
3180 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
3182 kvm_s390_vcpu_initial_reset(vcpu);
3186 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3189 memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs));
3194 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3197 memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
3202 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3203 struct kvm_sregs *sregs)
3207 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
3208 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
3214 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3215 struct kvm_sregs *sregs)
3219 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
3220 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
3226 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3232 if (test_fp_ctl(fpu->fpc)) {
3236 vcpu->run->s.regs.fpc = fpu->fpc;
3238 convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
3239 (freg_t *) fpu->fprs);
3241 memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
3248 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3252 /* make sure we have the latest values */
3255 convert_vx_to_fp((freg_t *) fpu->fprs,
3256 (__vector128 *) vcpu->run->s.regs.vrs);
3258 memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
3259 fpu->fpc = vcpu->run->s.regs.fpc;
3265 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
3269 if (!is_vcpu_stopped(vcpu))
3272 vcpu->run->psw_mask = psw.mask;
3273 vcpu->run->psw_addr = psw.addr;
3278 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3279 struct kvm_translation *tr)
3281 return -EINVAL; /* not implemented yet */
3284 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
3285 KVM_GUESTDBG_USE_HW_BP | \
3286 KVM_GUESTDBG_ENABLE)
3288 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
3289 struct kvm_guest_debug *dbg)
3295 vcpu->guest_debug = 0;
3296 kvm_s390_clear_bp_data(vcpu);
3298 if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
3302 if (!sclp.has_gpere) {
3307 if (dbg->control & KVM_GUESTDBG_ENABLE) {
3308 vcpu->guest_debug = dbg->control;
3309 /* enforce guest PER */
3310 kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
3312 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
3313 rc = kvm_s390_import_bp_data(vcpu, dbg);
3315 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
3316 vcpu->arch.guestdbg.last_bp = 0;
3320 vcpu->guest_debug = 0;
3321 kvm_s390_clear_bp_data(vcpu);
3322 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
3330 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3331 struct kvm_mp_state *mp_state)
3337 /* CHECK_STOP and LOAD are not supported yet */
3338 ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
3339 KVM_MP_STATE_OPERATING;
3345 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3346 struct kvm_mp_state *mp_state)
3352 /* user space knows about this interface - let it control the state */
3353 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
3355 switch (mp_state->mp_state) {
3356 case KVM_MP_STATE_STOPPED:
3357 kvm_s390_vcpu_stop(vcpu);
3359 case KVM_MP_STATE_OPERATING:
3360 kvm_s390_vcpu_start(vcpu);
3362 case KVM_MP_STATE_LOAD:
3363 case KVM_MP_STATE_CHECK_STOP:
3364 /* fall through - CHECK_STOP and LOAD are not supported yet */
3373 static bool ibs_enabled(struct kvm_vcpu *vcpu)
3375 return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
3378 static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
3381 kvm_s390_vcpu_request_handled(vcpu);
3382 if (!kvm_request_pending(vcpu))
3385 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3386 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
3387 * This ensures that the ipte instruction for this request has
3388 * already finished. We might race against a second unmapper that
3389 * wants to set the blocking bit. Lets just retry the request loop.
3391 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
3393 rc = gmap_mprotect_notify(vcpu->arch.gmap,
3394 kvm_s390_get_prefix(vcpu),
3395 PAGE_SIZE * 2, PROT_WRITE);
3397 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
3403 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3404 vcpu->arch.sie_block->ihcpu = 0xffff;
3408 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
3409 if (!ibs_enabled(vcpu)) {
3410 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3411 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
3416 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
3417 if (ibs_enabled(vcpu)) {
3418 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
3419 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
3424 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
3425 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
3429 if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3431 * Disable CMM virtualization; we will emulate the ESSA
3432 * instruction manually, in order to provide additional
3433 * functionalities needed for live migration.
3435 vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3439 if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3441 * Re-enable CMM virtualization if CMMA is available and
3442 * CMM has been used.
3444 if ((vcpu->kvm->arch.use_cmma) &&
3445 (vcpu->kvm->mm->context.uses_cmm))
3446 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3450 /* nothing to do, just clear the request */
3451 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
3452 /* we left the vsie handler, nothing to do, just clear the request */
3453 kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
3458 void kvm_s390_set_tod_clock(struct kvm *kvm,
3459 const struct kvm_s390_vm_tod_clock *gtod)
3461 struct kvm_vcpu *vcpu;
3462 struct kvm_s390_tod_clock_ext htod;
3465 mutex_lock(&kvm->lock);
3468 get_tod_clock_ext((char *)&htod);
3470 kvm->arch.epoch = gtod->tod - htod.tod;
3472 if (test_kvm_facility(kvm, 139)) {
3473 kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
3474 if (kvm->arch.epoch > gtod->tod)
3475 kvm->arch.epdx -= 1;
3478 kvm_s390_vcpu_block_all(kvm);
3479 kvm_for_each_vcpu(i, vcpu, kvm) {
3480 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
3481 vcpu->arch.sie_block->epdx = kvm->arch.epdx;
3484 kvm_s390_vcpu_unblock_all(kvm);
3486 mutex_unlock(&kvm->lock);
3490 * kvm_arch_fault_in_page - fault-in guest page if necessary
3491 * @vcpu: The corresponding virtual cpu
3492 * @gpa: Guest physical address
3493 * @writable: Whether the page should be writable or not
3495 * Make sure that a guest page has been faulted-in on the host.
3497 * Return: Zero on success, negative error code otherwise.
3499 long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
3501 return gmap_fault(vcpu->arch.gmap, gpa,
3502 writable ? FAULT_FLAG_WRITE : 0);
3505 static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
3506 unsigned long token)
3508 struct kvm_s390_interrupt inti;
3509 struct kvm_s390_irq irq;
3512 irq.u.ext.ext_params2 = token;
3513 irq.type = KVM_S390_INT_PFAULT_INIT;
3514 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
3516 inti.type = KVM_S390_INT_PFAULT_DONE;
3517 inti.parm64 = token;
3518 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
3522 void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
3523 struct kvm_async_pf *work)
3525 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
3526 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
3529 void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
3530 struct kvm_async_pf *work)
3532 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
3533 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
3536 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
3537 struct kvm_async_pf *work)
3539 /* s390 will always inject the page directly */
3542 bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
3545 * s390 will always inject the page directly,
3546 * but we still want check_async_completion to cleanup
3551 static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
3554 struct kvm_arch_async_pf arch;
3557 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3559 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
3560 vcpu->arch.pfault_compare)
3562 if (psw_extint_disabled(vcpu))
3564 if (kvm_s390_vcpu_has_irq(vcpu, 0))
3566 if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
3568 if (!vcpu->arch.gmap->pfault_enabled)
3571 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
3572 hva += current->thread.gmap_addr & ~PAGE_MASK;
3573 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
3576 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
3580 static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3585 * On s390 notifications for arriving pages will be delivered directly
3586 * to the guest but the house keeping for completed pfaults is
3587 * handled outside the worker.
3589 kvm_check_async_pf_completion(vcpu);
3591 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
3592 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
3597 if (test_cpu_flag(CIF_MCCK_PENDING))
3600 if (!kvm_is_ucontrol(vcpu->kvm)) {
3601 rc = kvm_s390_deliver_pending_interrupts(vcpu);
3606 rc = kvm_s390_handle_requests(vcpu);
3610 if (guestdbg_enabled(vcpu)) {
3611 kvm_s390_backup_guest_per_regs(vcpu);
3612 kvm_s390_patch_guest_per_regs(vcpu);
3615 clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask);
3617 vcpu->arch.sie_block->icptcode = 0;
3618 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
3619 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
3620 trace_kvm_s390_sie_enter(vcpu, cpuflags);
3625 static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
3627 struct kvm_s390_pgm_info pgm_info = {
3628 .code = PGM_ADDRESSING,
3633 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
3634 trace_kvm_s390_sie_fault(vcpu);
3637 * We want to inject an addressing exception, which is defined as a
3638 * suppressing or terminating exception. However, since we came here
3639 * by a DAT access exception, the PSW still points to the faulting
3640 * instruction since DAT exceptions are nullifying. So we've got
3641 * to look up the current opcode to get the length of the instruction
3642 * to be able to forward the PSW.
3644 rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
3645 ilen = insn_length(opcode);
3649 /* Instruction-Fetching Exceptions - we can't detect the ilen.
3650 * Forward by arbitrary ilc, injection will take care of
3651 * nullification if necessary.
3653 pgm_info = vcpu->arch.pgm;
3656 pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
3657 kvm_s390_forward_psw(vcpu, ilen);
3658 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
3661 static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
3663 struct mcck_volatile_info *mcck_info;
3664 struct sie_page *sie_page;
3666 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
3667 vcpu->arch.sie_block->icptcode);
3668 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
3670 if (guestdbg_enabled(vcpu))
3671 kvm_s390_restore_guest_per_regs(vcpu);
3673 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
3674 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
3676 if (exit_reason == -EINTR) {
3677 VCPU_EVENT(vcpu, 3, "%s", "machine check");
3678 sie_page = container_of(vcpu->arch.sie_block,
3679 struct sie_page, sie_block);
3680 mcck_info = &sie_page->mcck_info;
3681 kvm_s390_reinject_machine_check(vcpu, mcck_info);
3685 if (vcpu->arch.sie_block->icptcode > 0) {
3686 int rc = kvm_handle_sie_intercept(vcpu);
3688 if (rc != -EOPNOTSUPP)
3690 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
3691 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
3692 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
3693 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
3695 } else if (exit_reason != -EFAULT) {
3696 vcpu->stat.exit_null++;
3698 } else if (kvm_is_ucontrol(vcpu->kvm)) {
3699 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
3700 vcpu->run->s390_ucontrol.trans_exc_code =
3701 current->thread.gmap_addr;
3702 vcpu->run->s390_ucontrol.pgm_code = 0x10;
3704 } else if (current->thread.gmap_pfault) {
3705 trace_kvm_s390_major_guest_pfault(vcpu);
3706 current->thread.gmap_pfault = 0;
3707 if (kvm_arch_setup_async_pf(vcpu))
3709 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
3711 return vcpu_post_run_fault_in_sie(vcpu);
3714 static int __vcpu_run(struct kvm_vcpu *vcpu)
3716 int rc, exit_reason;
3719 * We try to hold kvm->srcu during most of vcpu_run (except when run-
3720 * ning the guest), so that memslots (and other stuff) are protected
3722 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3725 rc = vcpu_pre_run(vcpu);
3729 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
3731 * As PF_VCPU will be used in fault handler, between
3732 * guest_enter and guest_exit should be no uaccess.
3734 local_irq_disable();
3735 guest_enter_irqoff();
3736 __disable_cpu_timer_accounting(vcpu);
3738 exit_reason = sie64a(vcpu->arch.sie_block,
3739 vcpu->run->s.regs.gprs);
3740 local_irq_disable();
3741 __enable_cpu_timer_accounting(vcpu);
3742 guest_exit_irqoff();
3744 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3746 rc = vcpu_post_run(vcpu, exit_reason);
3747 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
3749 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
3753 static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3755 struct runtime_instr_cb *riccb;
3758 riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
3759 gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
3760 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
3761 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
3762 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
3763 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
3764 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
3765 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
3766 /* some control register changes require a tlb flush */
3767 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
3769 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
3770 kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
3771 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
3772 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
3773 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
3774 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
3776 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
3777 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
3778 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
3779 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
3780 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3781 kvm_clear_async_pf_completion_queue(vcpu);
3784 * If userspace sets the riccb (e.g. after migration) to a valid state,
3785 * we should enable RI here instead of doing the lazy enablement.
3787 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
3788 test_kvm_facility(vcpu->kvm, 64) &&
3790 !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
3791 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
3792 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
3795 * If userspace sets the gscb (e.g. after migration) to non-zero,
3796 * we should enable GS here instead of doing the lazy enablement.
3798 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
3799 test_kvm_facility(vcpu->kvm, 133) &&
3801 !vcpu->arch.gs_enabled) {
3802 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
3803 vcpu->arch.sie_block->ecb |= ECB_GS;
3804 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
3805 vcpu->arch.gs_enabled = 1;
3807 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
3808 test_kvm_facility(vcpu->kvm, 82)) {
3809 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
3810 vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
3812 save_access_regs(vcpu->arch.host_acrs);
3813 restore_access_regs(vcpu->run->s.regs.acrs);
3814 /* save host (userspace) fprs/vrs */
3816 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
3817 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
3819 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
3821 current->thread.fpu.regs = vcpu->run->s.regs.fprs;
3822 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
3823 if (test_fp_ctl(current->thread.fpu.fpc))
3824 /* User space provided an invalid FPC, let's clear it */
3825 current->thread.fpu.fpc = 0;
3826 if (MACHINE_HAS_GS) {
3828 __ctl_set_bit(2, 4);
3829 if (current->thread.gs_cb) {
3830 vcpu->arch.host_gscb = current->thread.gs_cb;
3831 save_gs_cb(vcpu->arch.host_gscb);
3833 if (vcpu->arch.gs_enabled) {
3834 current->thread.gs_cb = (struct gs_cb *)
3835 &vcpu->run->s.regs.gscb;
3836 restore_gs_cb(current->thread.gs_cb);
3840 /* SIE will load etoken directly from SDNX and therefore kvm_run */
3842 kvm_run->kvm_dirty_regs = 0;
3845 static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3847 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
3848 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
3849 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
3850 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
3851 kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
3852 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
3853 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
3854 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
3855 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
3856 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
3857 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
3858 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
3859 kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
3860 save_access_regs(vcpu->run->s.regs.acrs);
3861 restore_access_regs(vcpu->arch.host_acrs);
3862 /* Save guest register state */
3864 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
3865 /* Restore will be done lazily at return */
3866 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
3867 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
3868 if (MACHINE_HAS_GS) {
3869 __ctl_set_bit(2, 4);
3870 if (vcpu->arch.gs_enabled)
3871 save_gs_cb(current->thread.gs_cb);
3873 current->thread.gs_cb = vcpu->arch.host_gscb;
3874 restore_gs_cb(vcpu->arch.host_gscb);
3876 if (!vcpu->arch.host_gscb)
3877 __ctl_clear_bit(2, 4);
3878 vcpu->arch.host_gscb = NULL;
3880 /* SIE will save etoken directly into SDNX and therefore kvm_run */
3883 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
3887 if (kvm_run->immediate_exit)
3892 if (guestdbg_exit_pending(vcpu)) {
3893 kvm_s390_prepare_debug_exit(vcpu);
3898 kvm_sigset_activate(vcpu);
3900 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
3901 kvm_s390_vcpu_start(vcpu);
3902 } else if (is_vcpu_stopped(vcpu)) {
3903 pr_err_ratelimited("can't run stopped vcpu %d\n",
3909 sync_regs(vcpu, kvm_run);
3910 enable_cpu_timer_accounting(vcpu);
3913 rc = __vcpu_run(vcpu);
3915 if (signal_pending(current) && !rc) {
3916 kvm_run->exit_reason = KVM_EXIT_INTR;
3920 if (guestdbg_exit_pending(vcpu) && !rc) {
3921 kvm_s390_prepare_debug_exit(vcpu);
3925 if (rc == -EREMOTE) {
3926 /* userspace support is needed, kvm_run has been prepared */
3930 disable_cpu_timer_accounting(vcpu);
3931 store_regs(vcpu, kvm_run);
3933 kvm_sigset_deactivate(vcpu);
3935 vcpu->stat.exit_userspace++;
3942 * store status at address
3943 * we use have two special cases:
3944 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3945 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3947 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
3949 unsigned char archmode = 1;
3950 freg_t fprs[NUM_FPRS];
3955 px = kvm_s390_get_prefix(vcpu);
3956 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
3957 if (write_guest_abs(vcpu, 163, &archmode, 1))
3960 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
3961 if (write_guest_real(vcpu, 163, &archmode, 1))
3965 gpa -= __LC_FPREGS_SAVE_AREA;
3967 /* manually convert vector registers if necessary */
3968 if (MACHINE_HAS_VX) {
3969 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
3970 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
3973 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
3974 vcpu->run->s.regs.fprs, 128);
3976 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
3977 vcpu->run->s.regs.gprs, 128);
3978 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
3979 &vcpu->arch.sie_block->gpsw, 16);
3980 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
3982 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
3983 &vcpu->run->s.regs.fpc, 4);
3984 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
3985 &vcpu->arch.sie_block->todpr, 4);
3986 cputm = kvm_s390_get_cpu_timer(vcpu);
3987 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
3989 clkcomp = vcpu->arch.sie_block->ckc >> 8;
3990 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
3992 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
3993 &vcpu->run->s.regs.acrs, 64);
3994 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
3995 &vcpu->arch.sie_block->gcr, 128);
3996 return rc ? -EFAULT : 0;
3999 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
4002 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
4003 * switch in the run ioctl. Let's update our copies before we save
4004 * it into the save area
4007 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4008 save_access_regs(vcpu->run->s.regs.acrs);
4010 return kvm_s390_store_status_unloaded(vcpu, addr);
4013 static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
4015 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
4016 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
4019 static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
4022 struct kvm_vcpu *vcpu;
4024 kvm_for_each_vcpu(i, vcpu, kvm) {
4025 __disable_ibs_on_vcpu(vcpu);
4029 static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
4033 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
4034 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
4037 void kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
4039 int i, online_vcpus, started_vcpus = 0;
4041 if (!is_vcpu_stopped(vcpu))
4044 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
4045 /* Only one cpu at a time may enter/leave the STOPPED state. */
4046 spin_lock(&vcpu->kvm->arch.start_stop_lock);
4047 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
4049 for (i = 0; i < online_vcpus; i++) {
4050 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
4054 if (started_vcpus == 0) {
4055 /* we're the only active VCPU -> speed it up */
4056 __enable_ibs_on_vcpu(vcpu);
4057 } else if (started_vcpus == 1) {
4059 * As we are starting a second VCPU, we have to disable
4060 * the IBS facility on all VCPUs to remove potentially
4061 * oustanding ENABLE requests.
4063 __disable_ibs_on_all_vcpus(vcpu->kvm);
4066 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
4068 * Another VCPU might have used IBS while we were offline.
4069 * Let's play safe and flush the VCPU at startup.
4071 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4072 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4076 void kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
4078 int i, online_vcpus, started_vcpus = 0;
4079 struct kvm_vcpu *started_vcpu = NULL;
4081 if (is_vcpu_stopped(vcpu))
4084 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
4085 /* Only one cpu at a time may enter/leave the STOPPED state. */
4086 spin_lock(&vcpu->kvm->arch.start_stop_lock);
4087 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
4089 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
4090 kvm_s390_clear_stop_irq(vcpu);
4092 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
4093 __disable_ibs_on_vcpu(vcpu);
4095 for (i = 0; i < online_vcpus; i++) {
4096 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
4098 started_vcpu = vcpu->kvm->vcpus[i];
4102 if (started_vcpus == 1) {
4104 * As we only have one VCPU left, we want to enable the
4105 * IBS facility for that VCPU to speed it up.
4107 __enable_ibs_on_vcpu(started_vcpu);
4110 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4114 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4115 struct kvm_enable_cap *cap)
4123 case KVM_CAP_S390_CSS_SUPPORT:
4124 if (!vcpu->kvm->arch.css_support) {
4125 vcpu->kvm->arch.css_support = 1;
4126 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
4127 trace_kvm_s390_enable_css(vcpu->kvm);
4138 static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
4139 struct kvm_s390_mem_op *mop)
4141 void __user *uaddr = (void __user *)mop->buf;
4142 void *tmpbuf = NULL;
4144 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
4145 | KVM_S390_MEMOP_F_CHECK_ONLY;
4147 if (mop->flags & ~supported_flags)
4150 if (mop->size > MEM_OP_MAX_SIZE)
4153 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
4154 tmpbuf = vmalloc(mop->size);
4159 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4162 case KVM_S390_MEMOP_LOGICAL_READ:
4163 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4164 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4165 mop->size, GACC_FETCH);
4168 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4170 if (copy_to_user(uaddr, tmpbuf, mop->size))
4174 case KVM_S390_MEMOP_LOGICAL_WRITE:
4175 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4176 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4177 mop->size, GACC_STORE);
4180 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
4184 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4190 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
4192 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
4193 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
4199 long kvm_arch_vcpu_async_ioctl(struct file *filp,
4200 unsigned int ioctl, unsigned long arg)
4202 struct kvm_vcpu *vcpu = filp->private_data;
4203 void __user *argp = (void __user *)arg;
4206 case KVM_S390_IRQ: {
4207 struct kvm_s390_irq s390irq;
4209 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
4211 return kvm_s390_inject_vcpu(vcpu, &s390irq);
4213 case KVM_S390_INTERRUPT: {
4214 struct kvm_s390_interrupt s390int;
4215 struct kvm_s390_irq s390irq;
4217 if (copy_from_user(&s390int, argp, sizeof(s390int)))
4219 if (s390int_to_s390irq(&s390int, &s390irq))
4221 return kvm_s390_inject_vcpu(vcpu, &s390irq);
4224 return -ENOIOCTLCMD;
4227 long kvm_arch_vcpu_ioctl(struct file *filp,
4228 unsigned int ioctl, unsigned long arg)
4230 struct kvm_vcpu *vcpu = filp->private_data;
4231 void __user *argp = (void __user *)arg;
4238 case KVM_S390_STORE_STATUS:
4239 idx = srcu_read_lock(&vcpu->kvm->srcu);
4240 r = kvm_s390_vcpu_store_status(vcpu, arg);
4241 srcu_read_unlock(&vcpu->kvm->srcu, idx);
4243 case KVM_S390_SET_INITIAL_PSW: {
4247 if (copy_from_user(&psw, argp, sizeof(psw)))
4249 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
4252 case KVM_S390_INITIAL_RESET:
4253 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
4255 case KVM_SET_ONE_REG:
4256 case KVM_GET_ONE_REG: {
4257 struct kvm_one_reg reg;
4259 if (copy_from_user(®, argp, sizeof(reg)))
4261 if (ioctl == KVM_SET_ONE_REG)
4262 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®);
4264 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®);
4267 #ifdef CONFIG_KVM_S390_UCONTROL
4268 case KVM_S390_UCAS_MAP: {
4269 struct kvm_s390_ucas_mapping ucasmap;
4271 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4276 if (!kvm_is_ucontrol(vcpu->kvm)) {
4281 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
4282 ucasmap.vcpu_addr, ucasmap.length);
4285 case KVM_S390_UCAS_UNMAP: {
4286 struct kvm_s390_ucas_mapping ucasmap;
4288 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4293 if (!kvm_is_ucontrol(vcpu->kvm)) {
4298 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
4303 case KVM_S390_VCPU_FAULT: {
4304 r = gmap_fault(vcpu->arch.gmap, arg, 0);
4307 case KVM_ENABLE_CAP:
4309 struct kvm_enable_cap cap;
4311 if (copy_from_user(&cap, argp, sizeof(cap)))
4313 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4316 case KVM_S390_MEM_OP: {
4317 struct kvm_s390_mem_op mem_op;
4319 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
4320 r = kvm_s390_guest_mem_op(vcpu, &mem_op);
4325 case KVM_S390_SET_IRQ_STATE: {
4326 struct kvm_s390_irq_state irq_state;
4329 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4331 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
4332 irq_state.len == 0 ||
4333 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
4337 /* do not use irq_state.flags, it will break old QEMUs */
4338 r = kvm_s390_set_irq_state(vcpu,
4339 (void __user *) irq_state.buf,
4343 case KVM_S390_GET_IRQ_STATE: {
4344 struct kvm_s390_irq_state irq_state;
4347 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4349 if (irq_state.len == 0) {
4353 /* do not use irq_state.flags, it will break old QEMUs */
4354 r = kvm_s390_get_irq_state(vcpu,
4355 (__u8 __user *) irq_state.buf,
4367 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
4369 #ifdef CONFIG_KVM_S390_UCONTROL
4370 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
4371 && (kvm_is_ucontrol(vcpu->kvm))) {
4372 vmf->page = virt_to_page(vcpu->arch.sie_block);
4373 get_page(vmf->page);
4377 return VM_FAULT_SIGBUS;
4380 int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
4381 unsigned long npages)
4386 /* Section: memory related */
4387 int kvm_arch_prepare_memory_region(struct kvm *kvm,
4388 struct kvm_memory_slot *memslot,
4389 const struct kvm_userspace_memory_region *mem,
4390 enum kvm_mr_change change)
4392 /* A few sanity checks. We can have memory slots which have to be
4393 located/ended at a segment boundary (1MB). The memory in userland is
4394 ok to be fragmented into various different vmas. It is okay to mmap()
4395 and munmap() stuff in this slot after doing this call at any time */
4397 if (mem->userspace_addr & 0xffffful)
4400 if (mem->memory_size & 0xffffful)
4403 if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
4409 void kvm_arch_commit_memory_region(struct kvm *kvm,
4410 const struct kvm_userspace_memory_region *mem,
4411 const struct kvm_memory_slot *old,
4412 const struct kvm_memory_slot *new,
4413 enum kvm_mr_change change)
4417 /* If the basics of the memslot do not change, we do not want
4418 * to update the gmap. Every update causes several unnecessary
4419 * segment translation exceptions. This is usually handled just
4420 * fine by the normal fault handler + gmap, but it will also
4421 * cause faults on the prefix page of running guest CPUs.
4423 if (old->userspace_addr == mem->userspace_addr &&
4424 old->base_gfn * PAGE_SIZE == mem->guest_phys_addr &&
4425 old->npages * PAGE_SIZE == mem->memory_size)
4428 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
4429 mem->guest_phys_addr, mem->memory_size);
4431 pr_warn("failed to commit memory region\n");
4435 static inline unsigned long nonhyp_mask(int i)
4437 unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
4439 return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
4442 void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
4444 vcpu->valid_wakeup = false;
4447 static int __init kvm_s390_init(void)
4451 if (!sclp.has_sief2) {
4452 pr_info("SIE is not available\n");
4456 if (nested && hpage) {
4457 pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
4461 for (i = 0; i < 16; i++)
4462 kvm_s390_fac_base[i] |=
4463 S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
4465 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
4468 static void __exit kvm_s390_exit(void)
4473 module_init(kvm_s390_init);
4474 module_exit(kvm_s390_exit);
4477 * Enable autoloading of the kvm module.
4478 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
4479 * since x86 takes a different approach.
4481 #include <linux/miscdevice.h>
4482 MODULE_ALIAS_MISCDEV(KVM_MINOR);
4483 MODULE_ALIAS("devname:kvm");