1 // SPDX-License-Identifier: GPL-2.0
3 * hosting IBM Z kernel virtual machines (s390x)
5 * Copyright IBM Corp. 2008, 2020
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>
51 #define CREATE_TRACE_POINTS
53 #include "trace-s390.h"
55 #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
57 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
58 (KVM_MAX_VCPUS + LOCAL_IRQS))
60 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
61 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
63 struct kvm_stats_debugfs_item debugfs_entries[] = {
64 { "userspace_handled", VCPU_STAT(exit_userspace) },
65 { "exit_null", VCPU_STAT(exit_null) },
66 { "exit_validity", VCPU_STAT(exit_validity) },
67 { "exit_stop_request", VCPU_STAT(exit_stop_request) },
68 { "exit_external_request", VCPU_STAT(exit_external_request) },
69 { "exit_io_request", VCPU_STAT(exit_io_request) },
70 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt) },
71 { "exit_instruction", VCPU_STAT(exit_instruction) },
72 { "exit_pei", VCPU_STAT(exit_pei) },
73 { "exit_program_interruption", VCPU_STAT(exit_program_interruption) },
74 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program) },
75 { "exit_operation_exception", VCPU_STAT(exit_operation_exception) },
76 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
77 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
78 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
79 { "halt_no_poll_steal", VCPU_STAT(halt_no_poll_steal) },
80 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
81 { "instruction_lctlg", VCPU_STAT(instruction_lctlg) },
82 { "instruction_lctl", VCPU_STAT(instruction_lctl) },
83 { "instruction_stctl", VCPU_STAT(instruction_stctl) },
84 { "instruction_stctg", VCPU_STAT(instruction_stctg) },
85 { "deliver_ckc", VCPU_STAT(deliver_ckc) },
86 { "deliver_cputm", VCPU_STAT(deliver_cputm) },
87 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal) },
88 { "deliver_external_call", VCPU_STAT(deliver_external_call) },
89 { "deliver_service_signal", VCPU_STAT(deliver_service_signal) },
90 { "deliver_virtio", VCPU_STAT(deliver_virtio) },
91 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal) },
92 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal) },
93 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal) },
94 { "deliver_program", VCPU_STAT(deliver_program) },
95 { "deliver_io", VCPU_STAT(deliver_io) },
96 { "deliver_machine_check", VCPU_STAT(deliver_machine_check) },
97 { "exit_wait_state", VCPU_STAT(exit_wait_state) },
98 { "inject_ckc", VCPU_STAT(inject_ckc) },
99 { "inject_cputm", VCPU_STAT(inject_cputm) },
100 { "inject_external_call", VCPU_STAT(inject_external_call) },
101 { "inject_float_mchk", VM_STAT(inject_float_mchk) },
102 { "inject_emergency_signal", VCPU_STAT(inject_emergency_signal) },
103 { "inject_io", VM_STAT(inject_io) },
104 { "inject_mchk", VCPU_STAT(inject_mchk) },
105 { "inject_pfault_done", VM_STAT(inject_pfault_done) },
106 { "inject_program", VCPU_STAT(inject_program) },
107 { "inject_restart", VCPU_STAT(inject_restart) },
108 { "inject_service_signal", VM_STAT(inject_service_signal) },
109 { "inject_set_prefix", VCPU_STAT(inject_set_prefix) },
110 { "inject_stop_signal", VCPU_STAT(inject_stop_signal) },
111 { "inject_pfault_init", VCPU_STAT(inject_pfault_init) },
112 { "inject_virtio", VM_STAT(inject_virtio) },
113 { "instruction_epsw", VCPU_STAT(instruction_epsw) },
114 { "instruction_gs", VCPU_STAT(instruction_gs) },
115 { "instruction_io_other", VCPU_STAT(instruction_io_other) },
116 { "instruction_lpsw", VCPU_STAT(instruction_lpsw) },
117 { "instruction_lpswe", VCPU_STAT(instruction_lpswe) },
118 { "instruction_pfmf", VCPU_STAT(instruction_pfmf) },
119 { "instruction_ptff", VCPU_STAT(instruction_ptff) },
120 { "instruction_stidp", VCPU_STAT(instruction_stidp) },
121 { "instruction_sck", VCPU_STAT(instruction_sck) },
122 { "instruction_sckpf", VCPU_STAT(instruction_sckpf) },
123 { "instruction_spx", VCPU_STAT(instruction_spx) },
124 { "instruction_stpx", VCPU_STAT(instruction_stpx) },
125 { "instruction_stap", VCPU_STAT(instruction_stap) },
126 { "instruction_iske", VCPU_STAT(instruction_iske) },
127 { "instruction_ri", VCPU_STAT(instruction_ri) },
128 { "instruction_rrbe", VCPU_STAT(instruction_rrbe) },
129 { "instruction_sske", VCPU_STAT(instruction_sske) },
130 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock) },
131 { "instruction_essa", VCPU_STAT(instruction_essa) },
132 { "instruction_stsi", VCPU_STAT(instruction_stsi) },
133 { "instruction_stfl", VCPU_STAT(instruction_stfl) },
134 { "instruction_tb", VCPU_STAT(instruction_tb) },
135 { "instruction_tpi", VCPU_STAT(instruction_tpi) },
136 { "instruction_tprot", VCPU_STAT(instruction_tprot) },
137 { "instruction_tsch", VCPU_STAT(instruction_tsch) },
138 { "instruction_sthyi", VCPU_STAT(instruction_sthyi) },
139 { "instruction_sie", VCPU_STAT(instruction_sie) },
140 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense) },
141 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running) },
142 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call) },
143 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency) },
144 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency) },
145 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start) },
146 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop) },
147 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status) },
148 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status) },
149 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status) },
150 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch) },
151 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix) },
152 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart) },
153 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset) },
154 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset) },
155 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown) },
156 { "instruction_diag_10", VCPU_STAT(diagnose_10) },
157 { "instruction_diag_44", VCPU_STAT(diagnose_44) },
158 { "instruction_diag_9c", VCPU_STAT(diagnose_9c) },
159 { "diag_9c_ignored", VCPU_STAT(diagnose_9c_ignored) },
160 { "instruction_diag_258", VCPU_STAT(diagnose_258) },
161 { "instruction_diag_308", VCPU_STAT(diagnose_308) },
162 { "instruction_diag_500", VCPU_STAT(diagnose_500) },
163 { "instruction_diag_other", VCPU_STAT(diagnose_other) },
167 struct kvm_s390_tod_clock_ext {
173 /* allow nested virtualization in KVM (if enabled by user space) */
175 module_param(nested, int, S_IRUGO);
176 MODULE_PARM_DESC(nested, "Nested virtualization support");
178 /* allow 1m huge page guest backing, if !nested */
180 module_param(hpage, int, 0444);
181 MODULE_PARM_DESC(hpage, "1m huge page backing support");
183 /* maximum percentage of steal time for polling. >100 is treated like 100 */
184 static u8 halt_poll_max_steal = 10;
185 module_param(halt_poll_max_steal, byte, 0644);
186 MODULE_PARM_DESC(halt_poll_max_steal, "Maximum percentage of steal time to allow polling");
188 /* if set to true, the GISA will be initialized and used if available */
189 static bool use_gisa = true;
190 module_param(use_gisa, bool, 0644);
191 MODULE_PARM_DESC(use_gisa, "Use the GISA if the host supports it.");
194 * For now we handle at most 16 double words as this is what the s390 base
195 * kernel handles and stores in the prefix page. If we ever need to go beyond
196 * this, this requires changes to code, but the external uapi can stay.
198 #define SIZE_INTERNAL 16
201 * Base feature mask that defines default mask for facilities. Consists of the
202 * defines in FACILITIES_KVM and the non-hypervisor managed bits.
204 static unsigned long kvm_s390_fac_base[SIZE_INTERNAL] = { FACILITIES_KVM };
206 * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
207 * and defines the facilities that can be enabled via a cpu model.
209 static unsigned long kvm_s390_fac_ext[SIZE_INTERNAL] = { FACILITIES_KVM_CPUMODEL };
211 static unsigned long kvm_s390_fac_size(void)
213 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_MASK_SIZE_U64);
214 BUILD_BUG_ON(SIZE_INTERNAL > S390_ARCH_FAC_LIST_SIZE_U64);
215 BUILD_BUG_ON(SIZE_INTERNAL * sizeof(unsigned long) >
216 sizeof(S390_lowcore.stfle_fac_list));
218 return SIZE_INTERNAL;
221 /* available cpu features supported by kvm */
222 static DECLARE_BITMAP(kvm_s390_available_cpu_feat, KVM_S390_VM_CPU_FEAT_NR_BITS);
223 /* available subfunctions indicated via query / "test bit" */
224 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc;
226 static struct gmap_notifier gmap_notifier;
227 static struct gmap_notifier vsie_gmap_notifier;
228 debug_info_t *kvm_s390_dbf;
229 debug_info_t *kvm_s390_dbf_uv;
231 /* Section: not file related */
232 int kvm_arch_hardware_enable(void)
234 /* every s390 is virtualization enabled ;-) */
238 int kvm_arch_check_processor_compat(void *opaque)
243 /* forward declarations */
244 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
246 static int sca_switch_to_extended(struct kvm *kvm);
248 static void kvm_clock_sync_scb(struct kvm_s390_sie_block *scb, u64 delta)
253 * The TOD jumps by delta, we have to compensate this by adding
254 * -delta to the epoch.
258 /* sign-extension - we're adding to signed values below */
263 if (scb->ecd & ECD_MEF) {
264 scb->epdx += delta_idx;
265 if (scb->epoch < delta)
271 * This callback is executed during stop_machine(). All CPUs are therefore
272 * temporarily stopped. In order not to change guest behavior, we have to
273 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
274 * so a CPU won't be stopped while calculating with the epoch.
276 static int kvm_clock_sync(struct notifier_block *notifier, unsigned long val,
280 struct kvm_vcpu *vcpu;
282 unsigned long long *delta = v;
284 list_for_each_entry(kvm, &vm_list, vm_list) {
285 kvm_for_each_vcpu(i, vcpu, kvm) {
286 kvm_clock_sync_scb(vcpu->arch.sie_block, *delta);
288 kvm->arch.epoch = vcpu->arch.sie_block->epoch;
289 kvm->arch.epdx = vcpu->arch.sie_block->epdx;
291 if (vcpu->arch.cputm_enabled)
292 vcpu->arch.cputm_start += *delta;
293 if (vcpu->arch.vsie_block)
294 kvm_clock_sync_scb(vcpu->arch.vsie_block,
301 static struct notifier_block kvm_clock_notifier = {
302 .notifier_call = kvm_clock_sync,
305 int kvm_arch_hardware_setup(void *opaque)
307 gmap_notifier.notifier_call = kvm_gmap_notifier;
308 gmap_register_pte_notifier(&gmap_notifier);
309 vsie_gmap_notifier.notifier_call = kvm_s390_vsie_gmap_notifier;
310 gmap_register_pte_notifier(&vsie_gmap_notifier);
311 atomic_notifier_chain_register(&s390_epoch_delta_notifier,
312 &kvm_clock_notifier);
316 void kvm_arch_hardware_unsetup(void)
318 gmap_unregister_pte_notifier(&gmap_notifier);
319 gmap_unregister_pte_notifier(&vsie_gmap_notifier);
320 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier,
321 &kvm_clock_notifier);
324 static void allow_cpu_feat(unsigned long nr)
326 set_bit_inv(nr, kvm_s390_available_cpu_feat);
329 static inline int plo_test_bit(unsigned char nr)
331 register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
335 /* Parameter registers are ignored for "test bit" */
345 static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
347 register unsigned long r0 asm("0") = 0; /* query function */
348 register unsigned long r1 asm("1") = (unsigned long) query;
351 /* Parameter regs are ignored */
352 " .insn rrf,%[opc] << 16,2,4,6,0\n"
354 : "d" (r0), "a" (r1), [opc] "i" (opcode)
358 #define INSN_SORTL 0xb938
359 #define INSN_DFLTCC 0xb939
361 static void kvm_s390_cpu_feat_init(void)
365 for (i = 0; i < 256; ++i) {
367 kvm_s390_available_subfunc.plo[i >> 3] |= 0x80 >> (i & 7);
370 if (test_facility(28)) /* TOD-clock steering */
371 ptff(kvm_s390_available_subfunc.ptff,
372 sizeof(kvm_s390_available_subfunc.ptff),
375 if (test_facility(17)) { /* MSA */
376 __cpacf_query(CPACF_KMAC, (cpacf_mask_t *)
377 kvm_s390_available_subfunc.kmac);
378 __cpacf_query(CPACF_KMC, (cpacf_mask_t *)
379 kvm_s390_available_subfunc.kmc);
380 __cpacf_query(CPACF_KM, (cpacf_mask_t *)
381 kvm_s390_available_subfunc.km);
382 __cpacf_query(CPACF_KIMD, (cpacf_mask_t *)
383 kvm_s390_available_subfunc.kimd);
384 __cpacf_query(CPACF_KLMD, (cpacf_mask_t *)
385 kvm_s390_available_subfunc.klmd);
387 if (test_facility(76)) /* MSA3 */
388 __cpacf_query(CPACF_PCKMO, (cpacf_mask_t *)
389 kvm_s390_available_subfunc.pckmo);
390 if (test_facility(77)) { /* MSA4 */
391 __cpacf_query(CPACF_KMCTR, (cpacf_mask_t *)
392 kvm_s390_available_subfunc.kmctr);
393 __cpacf_query(CPACF_KMF, (cpacf_mask_t *)
394 kvm_s390_available_subfunc.kmf);
395 __cpacf_query(CPACF_KMO, (cpacf_mask_t *)
396 kvm_s390_available_subfunc.kmo);
397 __cpacf_query(CPACF_PCC, (cpacf_mask_t *)
398 kvm_s390_available_subfunc.pcc);
400 if (test_facility(57)) /* MSA5 */
401 __cpacf_query(CPACF_PRNO, (cpacf_mask_t *)
402 kvm_s390_available_subfunc.ppno);
404 if (test_facility(146)) /* MSA8 */
405 __cpacf_query(CPACF_KMA, (cpacf_mask_t *)
406 kvm_s390_available_subfunc.kma);
408 if (test_facility(155)) /* MSA9 */
409 __cpacf_query(CPACF_KDSA, (cpacf_mask_t *)
410 kvm_s390_available_subfunc.kdsa);
412 if (test_facility(150)) /* SORTL */
413 __insn32_query(INSN_SORTL, kvm_s390_available_subfunc.sortl);
415 if (test_facility(151)) /* DFLTCC */
416 __insn32_query(INSN_DFLTCC, kvm_s390_available_subfunc.dfltcc);
418 if (MACHINE_HAS_ESOP)
419 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP);
421 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
422 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
424 if (!sclp.has_sief2 || !MACHINE_HAS_ESOP || !sclp.has_64bscao ||
425 !test_facility(3) || !nested)
427 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2);
428 if (sclp.has_64bscao)
429 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO);
431 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF);
433 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE);
435 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS);
437 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB);
439 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI);
441 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS);
443 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS);
445 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
446 * all skey handling functions read/set the skey from the PGSTE
447 * instead of the real storage key.
449 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
450 * pages being detected as preserved although they are resident.
452 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
453 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
455 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
456 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
457 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
459 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
460 * cannot easily shadow the SCA because of the ipte lock.
464 int kvm_arch_init(void *opaque)
468 kvm_s390_dbf = debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
472 kvm_s390_dbf_uv = debug_register("kvm-uv", 32, 1, 7 * sizeof(long));
473 if (!kvm_s390_dbf_uv)
476 if (debug_register_view(kvm_s390_dbf, &debug_sprintf_view) ||
477 debug_register_view(kvm_s390_dbf_uv, &debug_sprintf_view))
480 kvm_s390_cpu_feat_init();
482 /* Register floating interrupt controller interface. */
483 rc = kvm_register_device_ops(&kvm_flic_ops, KVM_DEV_TYPE_FLIC);
485 pr_err("A FLIC registration call failed with rc=%d\n", rc);
489 rc = kvm_s390_gib_init(GAL_ISC);
500 void kvm_arch_exit(void)
502 kvm_s390_gib_destroy();
503 debug_unregister(kvm_s390_dbf);
504 debug_unregister(kvm_s390_dbf_uv);
507 /* Section: device related */
508 long kvm_arch_dev_ioctl(struct file *filp,
509 unsigned int ioctl, unsigned long arg)
511 if (ioctl == KVM_S390_ENABLE_SIE)
512 return s390_enable_sie();
516 int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
521 case KVM_CAP_S390_PSW:
522 case KVM_CAP_S390_GMAP:
523 case KVM_CAP_SYNC_MMU:
524 #ifdef CONFIG_KVM_S390_UCONTROL
525 case KVM_CAP_S390_UCONTROL:
527 case KVM_CAP_ASYNC_PF:
528 case KVM_CAP_SYNC_REGS:
529 case KVM_CAP_ONE_REG:
530 case KVM_CAP_ENABLE_CAP:
531 case KVM_CAP_S390_CSS_SUPPORT:
532 case KVM_CAP_IOEVENTFD:
533 case KVM_CAP_DEVICE_CTRL:
534 case KVM_CAP_S390_IRQCHIP:
535 case KVM_CAP_VM_ATTRIBUTES:
536 case KVM_CAP_MP_STATE:
537 case KVM_CAP_IMMEDIATE_EXIT:
538 case KVM_CAP_S390_INJECT_IRQ:
539 case KVM_CAP_S390_USER_SIGP:
540 case KVM_CAP_S390_USER_STSI:
541 case KVM_CAP_S390_SKEYS:
542 case KVM_CAP_S390_IRQ_STATE:
543 case KVM_CAP_S390_USER_INSTR0:
544 case KVM_CAP_S390_CMMA_MIGRATION:
545 case KVM_CAP_S390_AIS:
546 case KVM_CAP_S390_AIS_MIGRATION:
547 case KVM_CAP_S390_VCPU_RESETS:
550 case KVM_CAP_S390_HPAGE_1M:
552 if (hpage && !kvm_is_ucontrol(kvm))
555 case KVM_CAP_S390_MEM_OP:
558 case KVM_CAP_NR_VCPUS:
559 case KVM_CAP_MAX_VCPUS:
560 case KVM_CAP_MAX_VCPU_ID:
561 r = KVM_S390_BSCA_CPU_SLOTS;
562 if (!kvm_s390_use_sca_entries())
564 else if (sclp.has_esca && sclp.has_64bscao)
565 r = KVM_S390_ESCA_CPU_SLOTS;
567 case KVM_CAP_S390_COW:
568 r = MACHINE_HAS_ESOP;
570 case KVM_CAP_S390_VECTOR_REGISTERS:
573 case KVM_CAP_S390_RI:
574 r = test_facility(64);
576 case KVM_CAP_S390_GS:
577 r = test_facility(133);
579 case KVM_CAP_S390_BPB:
580 r = test_facility(82);
582 case KVM_CAP_S390_PROTECTED:
583 r = is_prot_virt_host();
591 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
594 gfn_t cur_gfn, last_gfn;
595 unsigned long gaddr, vmaddr;
596 struct gmap *gmap = kvm->arch.gmap;
597 DECLARE_BITMAP(bitmap, _PAGE_ENTRIES);
599 /* Loop over all guest segments */
600 cur_gfn = memslot->base_gfn;
601 last_gfn = memslot->base_gfn + memslot->npages;
602 for (; cur_gfn <= last_gfn; cur_gfn += _PAGE_ENTRIES) {
603 gaddr = gfn_to_gpa(cur_gfn);
604 vmaddr = gfn_to_hva_memslot(memslot, cur_gfn);
605 if (kvm_is_error_hva(vmaddr))
608 bitmap_zero(bitmap, _PAGE_ENTRIES);
609 gmap_sync_dirty_log_pmd(gmap, bitmap, gaddr, vmaddr);
610 for (i = 0; i < _PAGE_ENTRIES; i++) {
611 if (test_bit(i, bitmap))
612 mark_page_dirty(kvm, cur_gfn + i);
615 if (fatal_signal_pending(current))
621 /* Section: vm related */
622 static void sca_del_vcpu(struct kvm_vcpu *vcpu);
625 * Get (and clear) the dirty memory log for a memory slot.
627 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
628 struct kvm_dirty_log *log)
632 struct kvm_memory_slot *memslot;
635 if (kvm_is_ucontrol(kvm))
638 mutex_lock(&kvm->slots_lock);
641 if (log->slot >= KVM_USER_MEM_SLOTS)
644 r = kvm_get_dirty_log(kvm, log, &is_dirty, &memslot);
648 /* Clear the dirty log */
650 n = kvm_dirty_bitmap_bytes(memslot);
651 memset(memslot->dirty_bitmap, 0, n);
655 mutex_unlock(&kvm->slots_lock);
659 static void icpt_operexc_on_all_vcpus(struct kvm *kvm)
662 struct kvm_vcpu *vcpu;
664 kvm_for_each_vcpu(i, vcpu, kvm) {
665 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC, vcpu);
669 int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap)
677 case KVM_CAP_S390_IRQCHIP:
678 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
679 kvm->arch.use_irqchip = 1;
682 case KVM_CAP_S390_USER_SIGP:
683 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
684 kvm->arch.user_sigp = 1;
687 case KVM_CAP_S390_VECTOR_REGISTERS:
688 mutex_lock(&kvm->lock);
689 if (kvm->created_vcpus) {
691 } else if (MACHINE_HAS_VX) {
692 set_kvm_facility(kvm->arch.model.fac_mask, 129);
693 set_kvm_facility(kvm->arch.model.fac_list, 129);
694 if (test_facility(134)) {
695 set_kvm_facility(kvm->arch.model.fac_mask, 134);
696 set_kvm_facility(kvm->arch.model.fac_list, 134);
698 if (test_facility(135)) {
699 set_kvm_facility(kvm->arch.model.fac_mask, 135);
700 set_kvm_facility(kvm->arch.model.fac_list, 135);
702 if (test_facility(148)) {
703 set_kvm_facility(kvm->arch.model.fac_mask, 148);
704 set_kvm_facility(kvm->arch.model.fac_list, 148);
706 if (test_facility(152)) {
707 set_kvm_facility(kvm->arch.model.fac_mask, 152);
708 set_kvm_facility(kvm->arch.model.fac_list, 152);
713 mutex_unlock(&kvm->lock);
714 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
715 r ? "(not available)" : "(success)");
717 case KVM_CAP_S390_RI:
719 mutex_lock(&kvm->lock);
720 if (kvm->created_vcpus) {
722 } else if (test_facility(64)) {
723 set_kvm_facility(kvm->arch.model.fac_mask, 64);
724 set_kvm_facility(kvm->arch.model.fac_list, 64);
727 mutex_unlock(&kvm->lock);
728 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_RI %s",
729 r ? "(not available)" : "(success)");
731 case KVM_CAP_S390_AIS:
732 mutex_lock(&kvm->lock);
733 if (kvm->created_vcpus) {
736 set_kvm_facility(kvm->arch.model.fac_mask, 72);
737 set_kvm_facility(kvm->arch.model.fac_list, 72);
740 mutex_unlock(&kvm->lock);
741 VM_EVENT(kvm, 3, "ENABLE: AIS %s",
742 r ? "(not available)" : "(success)");
744 case KVM_CAP_S390_GS:
746 mutex_lock(&kvm->lock);
747 if (kvm->created_vcpus) {
749 } else if (test_facility(133)) {
750 set_kvm_facility(kvm->arch.model.fac_mask, 133);
751 set_kvm_facility(kvm->arch.model.fac_list, 133);
754 mutex_unlock(&kvm->lock);
755 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_GS %s",
756 r ? "(not available)" : "(success)");
758 case KVM_CAP_S390_HPAGE_1M:
759 mutex_lock(&kvm->lock);
760 if (kvm->created_vcpus)
762 else if (!hpage || kvm->arch.use_cmma || kvm_is_ucontrol(kvm))
766 down_write(&kvm->mm->mmap_sem);
767 kvm->mm->context.allow_gmap_hpage_1m = 1;
768 up_write(&kvm->mm->mmap_sem);
770 * We might have to create fake 4k page
771 * tables. To avoid that the hardware works on
772 * stale PGSTEs, we emulate these instructions.
774 kvm->arch.use_skf = 0;
775 kvm->arch.use_pfmfi = 0;
777 mutex_unlock(&kvm->lock);
778 VM_EVENT(kvm, 3, "ENABLE: CAP_S390_HPAGE %s",
779 r ? "(not available)" : "(success)");
781 case KVM_CAP_S390_USER_STSI:
782 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
783 kvm->arch.user_stsi = 1;
786 case KVM_CAP_S390_USER_INSTR0:
787 VM_EVENT(kvm, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
788 kvm->arch.user_instr0 = 1;
789 icpt_operexc_on_all_vcpus(kvm);
799 static int kvm_s390_get_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
803 switch (attr->attr) {
804 case KVM_S390_VM_MEM_LIMIT_SIZE:
806 VM_EVENT(kvm, 3, "QUERY: max guest memory: %lu bytes",
807 kvm->arch.mem_limit);
808 if (put_user(kvm->arch.mem_limit, (u64 __user *)attr->addr))
818 static int kvm_s390_set_mem_control(struct kvm *kvm, struct kvm_device_attr *attr)
822 switch (attr->attr) {
823 case KVM_S390_VM_MEM_ENABLE_CMMA:
828 VM_EVENT(kvm, 3, "%s", "ENABLE: CMMA support");
829 mutex_lock(&kvm->lock);
830 if (kvm->created_vcpus)
832 else if (kvm->mm->context.allow_gmap_hpage_1m)
835 kvm->arch.use_cmma = 1;
836 /* Not compatible with cmma. */
837 kvm->arch.use_pfmfi = 0;
840 mutex_unlock(&kvm->lock);
842 case KVM_S390_VM_MEM_CLR_CMMA:
847 if (!kvm->arch.use_cmma)
850 VM_EVENT(kvm, 3, "%s", "RESET: CMMA states");
851 mutex_lock(&kvm->lock);
852 idx = srcu_read_lock(&kvm->srcu);
853 s390_reset_cmma(kvm->arch.gmap->mm);
854 srcu_read_unlock(&kvm->srcu, idx);
855 mutex_unlock(&kvm->lock);
858 case KVM_S390_VM_MEM_LIMIT_SIZE: {
859 unsigned long new_limit;
861 if (kvm_is_ucontrol(kvm))
864 if (get_user(new_limit, (u64 __user *)attr->addr))
867 if (kvm->arch.mem_limit != KVM_S390_NO_MEM_LIMIT &&
868 new_limit > kvm->arch.mem_limit)
874 /* gmap_create takes last usable address */
875 if (new_limit != KVM_S390_NO_MEM_LIMIT)
879 mutex_lock(&kvm->lock);
880 if (!kvm->created_vcpus) {
881 /* gmap_create will round the limit up */
882 struct gmap *new = gmap_create(current->mm, new_limit);
887 gmap_remove(kvm->arch.gmap);
889 kvm->arch.gmap = new;
893 mutex_unlock(&kvm->lock);
894 VM_EVENT(kvm, 3, "SET: max guest address: %lu", new_limit);
895 VM_EVENT(kvm, 3, "New guest asce: 0x%pK",
896 (void *) kvm->arch.gmap->asce);
906 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu);
908 void kvm_s390_vcpu_crypto_reset_all(struct kvm *kvm)
910 struct kvm_vcpu *vcpu;
913 kvm_s390_vcpu_block_all(kvm);
915 kvm_for_each_vcpu(i, vcpu, kvm) {
916 kvm_s390_vcpu_crypto_setup(vcpu);
917 /* recreate the shadow crycb by leaving the VSIE handler */
918 kvm_s390_sync_request(KVM_REQ_VSIE_RESTART, vcpu);
921 kvm_s390_vcpu_unblock_all(kvm);
924 static int kvm_s390_vm_set_crypto(struct kvm *kvm, struct kvm_device_attr *attr)
926 mutex_lock(&kvm->lock);
927 switch (attr->attr) {
928 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
929 if (!test_kvm_facility(kvm, 76)) {
930 mutex_unlock(&kvm->lock);
934 kvm->arch.crypto.crycb->aes_wrapping_key_mask,
935 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
936 kvm->arch.crypto.aes_kw = 1;
937 VM_EVENT(kvm, 3, "%s", "ENABLE: AES keywrapping support");
939 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
940 if (!test_kvm_facility(kvm, 76)) {
941 mutex_unlock(&kvm->lock);
945 kvm->arch.crypto.crycb->dea_wrapping_key_mask,
946 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
947 kvm->arch.crypto.dea_kw = 1;
948 VM_EVENT(kvm, 3, "%s", "ENABLE: DEA keywrapping support");
950 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
951 if (!test_kvm_facility(kvm, 76)) {
952 mutex_unlock(&kvm->lock);
955 kvm->arch.crypto.aes_kw = 0;
956 memset(kvm->arch.crypto.crycb->aes_wrapping_key_mask, 0,
957 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
958 VM_EVENT(kvm, 3, "%s", "DISABLE: AES keywrapping support");
960 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
961 if (!test_kvm_facility(kvm, 76)) {
962 mutex_unlock(&kvm->lock);
965 kvm->arch.crypto.dea_kw = 0;
966 memset(kvm->arch.crypto.crycb->dea_wrapping_key_mask, 0,
967 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
968 VM_EVENT(kvm, 3, "%s", "DISABLE: DEA keywrapping support");
970 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
971 if (!ap_instructions_available()) {
972 mutex_unlock(&kvm->lock);
975 kvm->arch.crypto.apie = 1;
977 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
978 if (!ap_instructions_available()) {
979 mutex_unlock(&kvm->lock);
982 kvm->arch.crypto.apie = 0;
985 mutex_unlock(&kvm->lock);
989 kvm_s390_vcpu_crypto_reset_all(kvm);
990 mutex_unlock(&kvm->lock);
994 static void kvm_s390_sync_request_broadcast(struct kvm *kvm, int req)
997 struct kvm_vcpu *vcpu;
999 kvm_for_each_vcpu(cx, vcpu, kvm)
1000 kvm_s390_sync_request(req, vcpu);
1004 * Must be called with kvm->srcu held to avoid races on memslots, and with
1005 * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
1007 static int kvm_s390_vm_start_migration(struct kvm *kvm)
1009 struct kvm_memory_slot *ms;
1010 struct kvm_memslots *slots;
1011 unsigned long ram_pages = 0;
1014 /* migration mode already enabled */
1015 if (kvm->arch.migration_mode)
1017 slots = kvm_memslots(kvm);
1018 if (!slots || !slots->used_slots)
1021 if (!kvm->arch.use_cmma) {
1022 kvm->arch.migration_mode = 1;
1025 /* mark all the pages in active slots as dirty */
1026 for (slotnr = 0; slotnr < slots->used_slots; slotnr++) {
1027 ms = slots->memslots + slotnr;
1028 if (!ms->dirty_bitmap)
1031 * The second half of the bitmap is only used on x86,
1032 * and would be wasted otherwise, so we put it to good
1033 * use here to keep track of the state of the storage
1036 memset(kvm_second_dirty_bitmap(ms), 0xff, kvm_dirty_bitmap_bytes(ms));
1037 ram_pages += ms->npages;
1039 atomic64_set(&kvm->arch.cmma_dirty_pages, ram_pages);
1040 kvm->arch.migration_mode = 1;
1041 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_START_MIGRATION);
1046 * Must be called with kvm->slots_lock to avoid races with ourselves and
1047 * kvm_s390_vm_start_migration.
1049 static int kvm_s390_vm_stop_migration(struct kvm *kvm)
1051 /* migration mode already disabled */
1052 if (!kvm->arch.migration_mode)
1054 kvm->arch.migration_mode = 0;
1055 if (kvm->arch.use_cmma)
1056 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_STOP_MIGRATION);
1060 static int kvm_s390_vm_set_migration(struct kvm *kvm,
1061 struct kvm_device_attr *attr)
1065 mutex_lock(&kvm->slots_lock);
1066 switch (attr->attr) {
1067 case KVM_S390_VM_MIGRATION_START:
1068 res = kvm_s390_vm_start_migration(kvm);
1070 case KVM_S390_VM_MIGRATION_STOP:
1071 res = kvm_s390_vm_stop_migration(kvm);
1076 mutex_unlock(&kvm->slots_lock);
1081 static int kvm_s390_vm_get_migration(struct kvm *kvm,
1082 struct kvm_device_attr *attr)
1084 u64 mig = kvm->arch.migration_mode;
1086 if (attr->attr != KVM_S390_VM_MIGRATION_STATUS)
1089 if (copy_to_user((void __user *)attr->addr, &mig, sizeof(mig)))
1094 static int kvm_s390_set_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1096 struct kvm_s390_vm_tod_clock gtod;
1098 if (copy_from_user(>od, (void __user *)attr->addr, sizeof(gtod)))
1101 if (!test_kvm_facility(kvm, 139) && gtod.epoch_idx)
1103 kvm_s390_set_tod_clock(kvm, >od);
1105 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
1106 gtod.epoch_idx, gtod.tod);
1111 static int kvm_s390_set_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1115 if (copy_from_user(>od_high, (void __user *)attr->addr,
1121 VM_EVENT(kvm, 3, "SET: TOD extension: 0x%x", gtod_high);
1126 static int kvm_s390_set_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1128 struct kvm_s390_vm_tod_clock gtod = { 0 };
1130 if (copy_from_user(>od.tod, (void __user *)attr->addr,
1134 kvm_s390_set_tod_clock(kvm, >od);
1135 VM_EVENT(kvm, 3, "SET: TOD base: 0x%llx", gtod.tod);
1139 static int kvm_s390_set_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1146 switch (attr->attr) {
1147 case KVM_S390_VM_TOD_EXT:
1148 ret = kvm_s390_set_tod_ext(kvm, attr);
1150 case KVM_S390_VM_TOD_HIGH:
1151 ret = kvm_s390_set_tod_high(kvm, attr);
1153 case KVM_S390_VM_TOD_LOW:
1154 ret = kvm_s390_set_tod_low(kvm, attr);
1163 static void kvm_s390_get_tod_clock(struct kvm *kvm,
1164 struct kvm_s390_vm_tod_clock *gtod)
1166 struct kvm_s390_tod_clock_ext htod;
1170 get_tod_clock_ext((char *)&htod);
1172 gtod->tod = htod.tod + kvm->arch.epoch;
1173 gtod->epoch_idx = 0;
1174 if (test_kvm_facility(kvm, 139)) {
1175 gtod->epoch_idx = htod.epoch_idx + kvm->arch.epdx;
1176 if (gtod->tod < htod.tod)
1177 gtod->epoch_idx += 1;
1183 static int kvm_s390_get_tod_ext(struct kvm *kvm, struct kvm_device_attr *attr)
1185 struct kvm_s390_vm_tod_clock gtod;
1187 memset(>od, 0, sizeof(gtod));
1188 kvm_s390_get_tod_clock(kvm, >od);
1189 if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
1192 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1193 gtod.epoch_idx, gtod.tod);
1197 static int kvm_s390_get_tod_high(struct kvm *kvm, struct kvm_device_attr *attr)
1201 if (copy_to_user((void __user *)attr->addr, >od_high,
1204 VM_EVENT(kvm, 3, "QUERY: TOD extension: 0x%x", gtod_high);
1209 static int kvm_s390_get_tod_low(struct kvm *kvm, struct kvm_device_attr *attr)
1213 gtod = kvm_s390_get_tod_clock_fast(kvm);
1214 if (copy_to_user((void __user *)attr->addr, >od, sizeof(gtod)))
1216 VM_EVENT(kvm, 3, "QUERY: TOD base: 0x%llx", gtod);
1221 static int kvm_s390_get_tod(struct kvm *kvm, struct kvm_device_attr *attr)
1228 switch (attr->attr) {
1229 case KVM_S390_VM_TOD_EXT:
1230 ret = kvm_s390_get_tod_ext(kvm, attr);
1232 case KVM_S390_VM_TOD_HIGH:
1233 ret = kvm_s390_get_tod_high(kvm, attr);
1235 case KVM_S390_VM_TOD_LOW:
1236 ret = kvm_s390_get_tod_low(kvm, attr);
1245 static int kvm_s390_set_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1247 struct kvm_s390_vm_cpu_processor *proc;
1248 u16 lowest_ibc, unblocked_ibc;
1251 mutex_lock(&kvm->lock);
1252 if (kvm->created_vcpus) {
1256 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1261 if (!copy_from_user(proc, (void __user *)attr->addr,
1263 kvm->arch.model.cpuid = proc->cpuid;
1264 lowest_ibc = sclp.ibc >> 16 & 0xfff;
1265 unblocked_ibc = sclp.ibc & 0xfff;
1266 if (lowest_ibc && proc->ibc) {
1267 if (proc->ibc > unblocked_ibc)
1268 kvm->arch.model.ibc = unblocked_ibc;
1269 else if (proc->ibc < lowest_ibc)
1270 kvm->arch.model.ibc = lowest_ibc;
1272 kvm->arch.model.ibc = proc->ibc;
1274 memcpy(kvm->arch.model.fac_list, proc->fac_list,
1275 S390_ARCH_FAC_LIST_SIZE_BYTE);
1276 VM_EVENT(kvm, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1277 kvm->arch.model.ibc,
1278 kvm->arch.model.cpuid);
1279 VM_EVENT(kvm, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1280 kvm->arch.model.fac_list[0],
1281 kvm->arch.model.fac_list[1],
1282 kvm->arch.model.fac_list[2]);
1287 mutex_unlock(&kvm->lock);
1291 static int kvm_s390_set_processor_feat(struct kvm *kvm,
1292 struct kvm_device_attr *attr)
1294 struct kvm_s390_vm_cpu_feat data;
1296 if (copy_from_user(&data, (void __user *)attr->addr, sizeof(data)))
1298 if (!bitmap_subset((unsigned long *) data.feat,
1299 kvm_s390_available_cpu_feat,
1300 KVM_S390_VM_CPU_FEAT_NR_BITS))
1303 mutex_lock(&kvm->lock);
1304 if (kvm->created_vcpus) {
1305 mutex_unlock(&kvm->lock);
1308 bitmap_copy(kvm->arch.cpu_feat, (unsigned long *) data.feat,
1309 KVM_S390_VM_CPU_FEAT_NR_BITS);
1310 mutex_unlock(&kvm->lock);
1311 VM_EVENT(kvm, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1318 static int kvm_s390_set_processor_subfunc(struct kvm *kvm,
1319 struct kvm_device_attr *attr)
1321 mutex_lock(&kvm->lock);
1322 if (kvm->created_vcpus) {
1323 mutex_unlock(&kvm->lock);
1327 if (copy_from_user(&kvm->arch.model.subfuncs, (void __user *)attr->addr,
1328 sizeof(struct kvm_s390_vm_cpu_subfunc))) {
1329 mutex_unlock(&kvm->lock);
1332 mutex_unlock(&kvm->lock);
1334 VM_EVENT(kvm, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1335 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1336 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1337 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1338 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1339 VM_EVENT(kvm, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1340 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1341 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1342 VM_EVENT(kvm, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1343 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1344 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1345 VM_EVENT(kvm, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1346 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1347 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1348 VM_EVENT(kvm, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
1349 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1350 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1351 VM_EVENT(kvm, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1352 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1353 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1354 VM_EVENT(kvm, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1355 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1356 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1357 VM_EVENT(kvm, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1358 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1359 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1360 VM_EVENT(kvm, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1361 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1362 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1363 VM_EVENT(kvm, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1364 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1365 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1366 VM_EVENT(kvm, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1367 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1368 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1369 VM_EVENT(kvm, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1370 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1371 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1372 VM_EVENT(kvm, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1373 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1374 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1375 VM_EVENT(kvm, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1376 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1377 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1378 VM_EVENT(kvm, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1379 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1380 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1381 VM_EVENT(kvm, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1382 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1383 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1384 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1385 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1386 VM_EVENT(kvm, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1387 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1388 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1389 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1390 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1395 static int kvm_s390_set_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1399 switch (attr->attr) {
1400 case KVM_S390_VM_CPU_PROCESSOR:
1401 ret = kvm_s390_set_processor(kvm, attr);
1403 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1404 ret = kvm_s390_set_processor_feat(kvm, attr);
1406 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1407 ret = kvm_s390_set_processor_subfunc(kvm, attr);
1413 static int kvm_s390_get_processor(struct kvm *kvm, struct kvm_device_attr *attr)
1415 struct kvm_s390_vm_cpu_processor *proc;
1418 proc = kzalloc(sizeof(*proc), GFP_KERNEL);
1423 proc->cpuid = kvm->arch.model.cpuid;
1424 proc->ibc = kvm->arch.model.ibc;
1425 memcpy(&proc->fac_list, kvm->arch.model.fac_list,
1426 S390_ARCH_FAC_LIST_SIZE_BYTE);
1427 VM_EVENT(kvm, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1428 kvm->arch.model.ibc,
1429 kvm->arch.model.cpuid);
1430 VM_EVENT(kvm, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1431 kvm->arch.model.fac_list[0],
1432 kvm->arch.model.fac_list[1],
1433 kvm->arch.model.fac_list[2]);
1434 if (copy_to_user((void __user *)attr->addr, proc, sizeof(*proc)))
1441 static int kvm_s390_get_machine(struct kvm *kvm, struct kvm_device_attr *attr)
1443 struct kvm_s390_vm_cpu_machine *mach;
1446 mach = kzalloc(sizeof(*mach), GFP_KERNEL);
1451 get_cpu_id((struct cpuid *) &mach->cpuid);
1452 mach->ibc = sclp.ibc;
1453 memcpy(&mach->fac_mask, kvm->arch.model.fac_mask,
1454 S390_ARCH_FAC_LIST_SIZE_BYTE);
1455 memcpy((unsigned long *)&mach->fac_list, S390_lowcore.stfle_fac_list,
1456 sizeof(S390_lowcore.stfle_fac_list));
1457 VM_EVENT(kvm, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1458 kvm->arch.model.ibc,
1459 kvm->arch.model.cpuid);
1460 VM_EVENT(kvm, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1464 VM_EVENT(kvm, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1468 if (copy_to_user((void __user *)attr->addr, mach, sizeof(*mach)))
1475 static int kvm_s390_get_processor_feat(struct kvm *kvm,
1476 struct kvm_device_attr *attr)
1478 struct kvm_s390_vm_cpu_feat data;
1480 bitmap_copy((unsigned long *) data.feat, kvm->arch.cpu_feat,
1481 KVM_S390_VM_CPU_FEAT_NR_BITS);
1482 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1484 VM_EVENT(kvm, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1491 static int kvm_s390_get_machine_feat(struct kvm *kvm,
1492 struct kvm_device_attr *attr)
1494 struct kvm_s390_vm_cpu_feat data;
1496 bitmap_copy((unsigned long *) data.feat,
1497 kvm_s390_available_cpu_feat,
1498 KVM_S390_VM_CPU_FEAT_NR_BITS);
1499 if (copy_to_user((void __user *)attr->addr, &data, sizeof(data)))
1501 VM_EVENT(kvm, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1508 static int kvm_s390_get_processor_subfunc(struct kvm *kvm,
1509 struct kvm_device_attr *attr)
1511 if (copy_to_user((void __user *)attr->addr, &kvm->arch.model.subfuncs,
1512 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1515 VM_EVENT(kvm, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1516 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[0],
1517 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[1],
1518 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[2],
1519 ((unsigned long *) &kvm->arch.model.subfuncs.plo)[3]);
1520 VM_EVENT(kvm, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1521 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[0],
1522 ((unsigned long *) &kvm->arch.model.subfuncs.ptff)[1]);
1523 VM_EVENT(kvm, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1524 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[0],
1525 ((unsigned long *) &kvm->arch.model.subfuncs.kmac)[1]);
1526 VM_EVENT(kvm, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1527 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[0],
1528 ((unsigned long *) &kvm->arch.model.subfuncs.kmc)[1]);
1529 VM_EVENT(kvm, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
1530 ((unsigned long *) &kvm->arch.model.subfuncs.km)[0],
1531 ((unsigned long *) &kvm->arch.model.subfuncs.km)[1]);
1532 VM_EVENT(kvm, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1533 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[0],
1534 ((unsigned long *) &kvm->arch.model.subfuncs.kimd)[1]);
1535 VM_EVENT(kvm, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1536 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[0],
1537 ((unsigned long *) &kvm->arch.model.subfuncs.klmd)[1]);
1538 VM_EVENT(kvm, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1539 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[0],
1540 ((unsigned long *) &kvm->arch.model.subfuncs.pckmo)[1]);
1541 VM_EVENT(kvm, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1542 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[0],
1543 ((unsigned long *) &kvm->arch.model.subfuncs.kmctr)[1]);
1544 VM_EVENT(kvm, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1545 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[0],
1546 ((unsigned long *) &kvm->arch.model.subfuncs.kmf)[1]);
1547 VM_EVENT(kvm, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1548 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[0],
1549 ((unsigned long *) &kvm->arch.model.subfuncs.kmo)[1]);
1550 VM_EVENT(kvm, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1551 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[0],
1552 ((unsigned long *) &kvm->arch.model.subfuncs.pcc)[1]);
1553 VM_EVENT(kvm, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1554 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[0],
1555 ((unsigned long *) &kvm->arch.model.subfuncs.ppno)[1]);
1556 VM_EVENT(kvm, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1557 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[0],
1558 ((unsigned long *) &kvm->arch.model.subfuncs.kma)[1]);
1559 VM_EVENT(kvm, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1560 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[0],
1561 ((unsigned long *) &kvm->arch.model.subfuncs.kdsa)[1]);
1562 VM_EVENT(kvm, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1563 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[0],
1564 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[1],
1565 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[2],
1566 ((unsigned long *) &kvm->arch.model.subfuncs.sortl)[3]);
1567 VM_EVENT(kvm, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1568 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[0],
1569 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[1],
1570 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[2],
1571 ((unsigned long *) &kvm->arch.model.subfuncs.dfltcc)[3]);
1576 static int kvm_s390_get_machine_subfunc(struct kvm *kvm,
1577 struct kvm_device_attr *attr)
1579 if (copy_to_user((void __user *)attr->addr, &kvm_s390_available_subfunc,
1580 sizeof(struct kvm_s390_vm_cpu_subfunc)))
1583 VM_EVENT(kvm, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1584 ((unsigned long *) &kvm_s390_available_subfunc.plo)[0],
1585 ((unsigned long *) &kvm_s390_available_subfunc.plo)[1],
1586 ((unsigned long *) &kvm_s390_available_subfunc.plo)[2],
1587 ((unsigned long *) &kvm_s390_available_subfunc.plo)[3]);
1588 VM_EVENT(kvm, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
1589 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[0],
1590 ((unsigned long *) &kvm_s390_available_subfunc.ptff)[1]);
1591 VM_EVENT(kvm, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
1592 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[0],
1593 ((unsigned long *) &kvm_s390_available_subfunc.kmac)[1]);
1594 VM_EVENT(kvm, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
1595 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[0],
1596 ((unsigned long *) &kvm_s390_available_subfunc.kmc)[1]);
1597 VM_EVENT(kvm, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
1598 ((unsigned long *) &kvm_s390_available_subfunc.km)[0],
1599 ((unsigned long *) &kvm_s390_available_subfunc.km)[1]);
1600 VM_EVENT(kvm, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
1601 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[0],
1602 ((unsigned long *) &kvm_s390_available_subfunc.kimd)[1]);
1603 VM_EVENT(kvm, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
1604 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[0],
1605 ((unsigned long *) &kvm_s390_available_subfunc.klmd)[1]);
1606 VM_EVENT(kvm, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
1607 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[0],
1608 ((unsigned long *) &kvm_s390_available_subfunc.pckmo)[1]);
1609 VM_EVENT(kvm, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
1610 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[0],
1611 ((unsigned long *) &kvm_s390_available_subfunc.kmctr)[1]);
1612 VM_EVENT(kvm, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
1613 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[0],
1614 ((unsigned long *) &kvm_s390_available_subfunc.kmf)[1]);
1615 VM_EVENT(kvm, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
1616 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[0],
1617 ((unsigned long *) &kvm_s390_available_subfunc.kmo)[1]);
1618 VM_EVENT(kvm, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
1619 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[0],
1620 ((unsigned long *) &kvm_s390_available_subfunc.pcc)[1]);
1621 VM_EVENT(kvm, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
1622 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[0],
1623 ((unsigned long *) &kvm_s390_available_subfunc.ppno)[1]);
1624 VM_EVENT(kvm, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
1625 ((unsigned long *) &kvm_s390_available_subfunc.kma)[0],
1626 ((unsigned long *) &kvm_s390_available_subfunc.kma)[1]);
1627 VM_EVENT(kvm, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx",
1628 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[0],
1629 ((unsigned long *) &kvm_s390_available_subfunc.kdsa)[1]);
1630 VM_EVENT(kvm, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1631 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[0],
1632 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[1],
1633 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[2],
1634 ((unsigned long *) &kvm_s390_available_subfunc.sortl)[3]);
1635 VM_EVENT(kvm, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1636 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[0],
1637 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[1],
1638 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[2],
1639 ((unsigned long *) &kvm_s390_available_subfunc.dfltcc)[3]);
1644 static int kvm_s390_get_cpu_model(struct kvm *kvm, struct kvm_device_attr *attr)
1648 switch (attr->attr) {
1649 case KVM_S390_VM_CPU_PROCESSOR:
1650 ret = kvm_s390_get_processor(kvm, attr);
1652 case KVM_S390_VM_CPU_MACHINE:
1653 ret = kvm_s390_get_machine(kvm, attr);
1655 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1656 ret = kvm_s390_get_processor_feat(kvm, attr);
1658 case KVM_S390_VM_CPU_MACHINE_FEAT:
1659 ret = kvm_s390_get_machine_feat(kvm, attr);
1661 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1662 ret = kvm_s390_get_processor_subfunc(kvm, attr);
1664 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1665 ret = kvm_s390_get_machine_subfunc(kvm, attr);
1671 static int kvm_s390_vm_set_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1675 switch (attr->group) {
1676 case KVM_S390_VM_MEM_CTRL:
1677 ret = kvm_s390_set_mem_control(kvm, attr);
1679 case KVM_S390_VM_TOD:
1680 ret = kvm_s390_set_tod(kvm, attr);
1682 case KVM_S390_VM_CPU_MODEL:
1683 ret = kvm_s390_set_cpu_model(kvm, attr);
1685 case KVM_S390_VM_CRYPTO:
1686 ret = kvm_s390_vm_set_crypto(kvm, attr);
1688 case KVM_S390_VM_MIGRATION:
1689 ret = kvm_s390_vm_set_migration(kvm, attr);
1699 static int kvm_s390_vm_get_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1703 switch (attr->group) {
1704 case KVM_S390_VM_MEM_CTRL:
1705 ret = kvm_s390_get_mem_control(kvm, attr);
1707 case KVM_S390_VM_TOD:
1708 ret = kvm_s390_get_tod(kvm, attr);
1710 case KVM_S390_VM_CPU_MODEL:
1711 ret = kvm_s390_get_cpu_model(kvm, attr);
1713 case KVM_S390_VM_MIGRATION:
1714 ret = kvm_s390_vm_get_migration(kvm, attr);
1724 static int kvm_s390_vm_has_attr(struct kvm *kvm, struct kvm_device_attr *attr)
1728 switch (attr->group) {
1729 case KVM_S390_VM_MEM_CTRL:
1730 switch (attr->attr) {
1731 case KVM_S390_VM_MEM_ENABLE_CMMA:
1732 case KVM_S390_VM_MEM_CLR_CMMA:
1733 ret = sclp.has_cmma ? 0 : -ENXIO;
1735 case KVM_S390_VM_MEM_LIMIT_SIZE:
1743 case KVM_S390_VM_TOD:
1744 switch (attr->attr) {
1745 case KVM_S390_VM_TOD_LOW:
1746 case KVM_S390_VM_TOD_HIGH:
1754 case KVM_S390_VM_CPU_MODEL:
1755 switch (attr->attr) {
1756 case KVM_S390_VM_CPU_PROCESSOR:
1757 case KVM_S390_VM_CPU_MACHINE:
1758 case KVM_S390_VM_CPU_PROCESSOR_FEAT:
1759 case KVM_S390_VM_CPU_MACHINE_FEAT:
1760 case KVM_S390_VM_CPU_MACHINE_SUBFUNC:
1761 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC:
1769 case KVM_S390_VM_CRYPTO:
1770 switch (attr->attr) {
1771 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW:
1772 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW:
1773 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW:
1774 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW:
1777 case KVM_S390_VM_CRYPTO_ENABLE_APIE:
1778 case KVM_S390_VM_CRYPTO_DISABLE_APIE:
1779 ret = ap_instructions_available() ? 0 : -ENXIO;
1786 case KVM_S390_VM_MIGRATION:
1797 static long kvm_s390_get_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1801 int srcu_idx, i, r = 0;
1803 if (args->flags != 0)
1806 /* Is this guest using storage keys? */
1807 if (!mm_uses_skeys(current->mm))
1808 return KVM_S390_GET_SKEYS_NONE;
1810 /* Enforce sane limit on memory allocation */
1811 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1814 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
1818 down_read(¤t->mm->mmap_sem);
1819 srcu_idx = srcu_read_lock(&kvm->srcu);
1820 for (i = 0; i < args->count; i++) {
1821 hva = gfn_to_hva(kvm, args->start_gfn + i);
1822 if (kvm_is_error_hva(hva)) {
1827 r = get_guest_storage_key(current->mm, hva, &keys[i]);
1831 srcu_read_unlock(&kvm->srcu, srcu_idx);
1832 up_read(¤t->mm->mmap_sem);
1835 r = copy_to_user((uint8_t __user *)args->skeydata_addr, keys,
1836 sizeof(uint8_t) * args->count);
1845 static long kvm_s390_set_skeys(struct kvm *kvm, struct kvm_s390_skeys *args)
1849 int srcu_idx, i, r = 0;
1852 if (args->flags != 0)
1855 /* Enforce sane limit on memory allocation */
1856 if (args->count < 1 || args->count > KVM_S390_SKEYS_MAX)
1859 keys = kvmalloc_array(args->count, sizeof(uint8_t), GFP_KERNEL);
1863 r = copy_from_user(keys, (uint8_t __user *)args->skeydata_addr,
1864 sizeof(uint8_t) * args->count);
1870 /* Enable storage key handling for the guest */
1871 r = s390_enable_skey();
1876 down_read(¤t->mm->mmap_sem);
1877 srcu_idx = srcu_read_lock(&kvm->srcu);
1878 while (i < args->count) {
1880 hva = gfn_to_hva(kvm, args->start_gfn + i);
1881 if (kvm_is_error_hva(hva)) {
1886 /* Lowest order bit is reserved */
1887 if (keys[i] & 0x01) {
1892 r = set_guest_storage_key(current->mm, hva, keys[i], 0);
1894 r = fixup_user_fault(current, current->mm, hva,
1895 FAULT_FLAG_WRITE, &unlocked);
1902 srcu_read_unlock(&kvm->srcu, srcu_idx);
1903 up_read(¤t->mm->mmap_sem);
1910 * Base address and length must be sent at the start of each block, therefore
1911 * it's cheaper to send some clean data, as long as it's less than the size of
1914 #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
1915 /* for consistency */
1916 #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
1919 * Similar to gfn_to_memslot, but returns the index of a memslot also when the
1920 * address falls in a hole. In that case the index of one of the memslots
1921 * bordering the hole is returned.
1923 static int gfn_to_memslot_approx(struct kvm_memslots *slots, gfn_t gfn)
1925 int start = 0, end = slots->used_slots;
1926 int slot = atomic_read(&slots->lru_slot);
1927 struct kvm_memory_slot *memslots = slots->memslots;
1929 if (gfn >= memslots[slot].base_gfn &&
1930 gfn < memslots[slot].base_gfn + memslots[slot].npages)
1933 while (start < end) {
1934 slot = start + (end - start) / 2;
1936 if (gfn >= memslots[slot].base_gfn)
1942 if (start >= slots->used_slots)
1943 return slots->used_slots - 1;
1945 if (gfn >= memslots[start].base_gfn &&
1946 gfn < memslots[start].base_gfn + memslots[start].npages) {
1947 atomic_set(&slots->lru_slot, start);
1953 static int kvm_s390_peek_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
1954 u8 *res, unsigned long bufsize)
1956 unsigned long pgstev, hva, cur_gfn = args->start_gfn;
1959 while (args->count < bufsize) {
1960 hva = gfn_to_hva(kvm, cur_gfn);
1962 * We return an error if the first value was invalid, but we
1963 * return successfully if at least one value was copied.
1965 if (kvm_is_error_hva(hva))
1966 return args->count ? 0 : -EFAULT;
1967 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
1969 res[args->count++] = (pgstev >> 24) & 0x43;
1976 static unsigned long kvm_s390_next_dirty_cmma(struct kvm_memslots *slots,
1977 unsigned long cur_gfn)
1979 int slotidx = gfn_to_memslot_approx(slots, cur_gfn);
1980 struct kvm_memory_slot *ms = slots->memslots + slotidx;
1981 unsigned long ofs = cur_gfn - ms->base_gfn;
1983 if (ms->base_gfn + ms->npages <= cur_gfn) {
1985 /* If we are above the highest slot, wrap around */
1987 slotidx = slots->used_slots - 1;
1989 ms = slots->memslots + slotidx;
1992 ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, ofs);
1993 while ((slotidx > 0) && (ofs >= ms->npages)) {
1995 ms = slots->memslots + slotidx;
1996 ofs = find_next_bit(kvm_second_dirty_bitmap(ms), ms->npages, 0);
1998 return ms->base_gfn + ofs;
2001 static int kvm_s390_get_cmma(struct kvm *kvm, struct kvm_s390_cmma_log *args,
2002 u8 *res, unsigned long bufsize)
2004 unsigned long mem_end, cur_gfn, next_gfn, hva, pgstev;
2005 struct kvm_memslots *slots = kvm_memslots(kvm);
2006 struct kvm_memory_slot *ms;
2008 if (unlikely(!slots->used_slots))
2011 cur_gfn = kvm_s390_next_dirty_cmma(slots, args->start_gfn);
2012 ms = gfn_to_memslot(kvm, cur_gfn);
2014 args->start_gfn = cur_gfn;
2017 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2018 mem_end = slots->memslots[0].base_gfn + slots->memslots[0].npages;
2020 while (args->count < bufsize) {
2021 hva = gfn_to_hva(kvm, cur_gfn);
2022 if (kvm_is_error_hva(hva))
2024 /* Decrement only if we actually flipped the bit to 0 */
2025 if (test_and_clear_bit(cur_gfn - ms->base_gfn, kvm_second_dirty_bitmap(ms)))
2026 atomic64_dec(&kvm->arch.cmma_dirty_pages);
2027 if (get_pgste(kvm->mm, hva, &pgstev) < 0)
2029 /* Save the value */
2030 res[args->count++] = (pgstev >> 24) & 0x43;
2031 /* If the next bit is too far away, stop. */
2032 if (next_gfn > cur_gfn + KVM_S390_MAX_BIT_DISTANCE)
2034 /* If we reached the previous "next", find the next one */
2035 if (cur_gfn == next_gfn)
2036 next_gfn = kvm_s390_next_dirty_cmma(slots, cur_gfn + 1);
2037 /* Reached the end of memory or of the buffer, stop */
2038 if ((next_gfn >= mem_end) ||
2039 (next_gfn - args->start_gfn >= bufsize))
2042 /* Reached the end of the current memslot, take the next one. */
2043 if (cur_gfn - ms->base_gfn >= ms->npages) {
2044 ms = gfn_to_memslot(kvm, cur_gfn);
2053 * This function searches for the next page with dirty CMMA attributes, and
2054 * saves the attributes in the buffer up to either the end of the buffer or
2055 * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
2056 * no trailing clean bytes are saved.
2057 * In case no dirty bits were found, or if CMMA was not enabled or used, the
2058 * output buffer will indicate 0 as length.
2060 static int kvm_s390_get_cmma_bits(struct kvm *kvm,
2061 struct kvm_s390_cmma_log *args)
2063 unsigned long bufsize;
2064 int srcu_idx, peek, ret;
2067 if (!kvm->arch.use_cmma)
2069 /* Invalid/unsupported flags were specified */
2070 if (args->flags & ~KVM_S390_CMMA_PEEK)
2072 /* Migration mode query, and we are not doing a migration */
2073 peek = !!(args->flags & KVM_S390_CMMA_PEEK);
2074 if (!peek && !kvm->arch.migration_mode)
2076 /* CMMA is disabled or was not used, or the buffer has length zero */
2077 bufsize = min(args->count, KVM_S390_CMMA_SIZE_MAX);
2078 if (!bufsize || !kvm->mm->context.uses_cmm) {
2079 memset(args, 0, sizeof(*args));
2082 /* We are not peeking, and there are no dirty pages */
2083 if (!peek && !atomic64_read(&kvm->arch.cmma_dirty_pages)) {
2084 memset(args, 0, sizeof(*args));
2088 values = vmalloc(bufsize);
2092 down_read(&kvm->mm->mmap_sem);
2093 srcu_idx = srcu_read_lock(&kvm->srcu);
2095 ret = kvm_s390_peek_cmma(kvm, args, values, bufsize);
2097 ret = kvm_s390_get_cmma(kvm, args, values, bufsize);
2098 srcu_read_unlock(&kvm->srcu, srcu_idx);
2099 up_read(&kvm->mm->mmap_sem);
2101 if (kvm->arch.migration_mode)
2102 args->remaining = atomic64_read(&kvm->arch.cmma_dirty_pages);
2104 args->remaining = 0;
2106 if (copy_to_user((void __user *)args->values, values, args->count))
2114 * This function sets the CMMA attributes for the given pages. If the input
2115 * buffer has zero length, no action is taken, otherwise the attributes are
2116 * set and the mm->context.uses_cmm flag is set.
2118 static int kvm_s390_set_cmma_bits(struct kvm *kvm,
2119 const struct kvm_s390_cmma_log *args)
2121 unsigned long hva, mask, pgstev, i;
2123 int srcu_idx, r = 0;
2127 if (!kvm->arch.use_cmma)
2129 /* invalid/unsupported flags */
2130 if (args->flags != 0)
2132 /* Enforce sane limit on memory allocation */
2133 if (args->count > KVM_S390_CMMA_SIZE_MAX)
2136 if (args->count == 0)
2139 bits = vmalloc(array_size(sizeof(*bits), args->count));
2143 r = copy_from_user(bits, (void __user *)args->values, args->count);
2149 down_read(&kvm->mm->mmap_sem);
2150 srcu_idx = srcu_read_lock(&kvm->srcu);
2151 for (i = 0; i < args->count; i++) {
2152 hva = gfn_to_hva(kvm, args->start_gfn + i);
2153 if (kvm_is_error_hva(hva)) {
2159 pgstev = pgstev << 24;
2160 mask &= _PGSTE_GPS_USAGE_MASK | _PGSTE_GPS_NODAT;
2161 set_pgste_bits(kvm->mm, hva, mask, pgstev);
2163 srcu_read_unlock(&kvm->srcu, srcu_idx);
2164 up_read(&kvm->mm->mmap_sem);
2166 if (!kvm->mm->context.uses_cmm) {
2167 down_write(&kvm->mm->mmap_sem);
2168 kvm->mm->context.uses_cmm = 1;
2169 up_write(&kvm->mm->mmap_sem);
2176 static int kvm_s390_cpus_from_pv(struct kvm *kvm, u16 *rcp, u16 *rrcp)
2178 struct kvm_vcpu *vcpu;
2184 * We ignore failures and try to destroy as many CPUs as possible.
2185 * At the same time we must not free the assigned resources when
2186 * this fails, as the ultravisor has still access to that memory.
2187 * So kvm_s390_pv_destroy_cpu can leave a "wanted" memory leak
2189 * We want to return the first failure rc and rrc, though.
2191 kvm_for_each_vcpu(i, vcpu, kvm) {
2192 mutex_lock(&vcpu->mutex);
2193 if (kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc) && !ret) {
2198 mutex_unlock(&vcpu->mutex);
2203 static int kvm_s390_cpus_to_pv(struct kvm *kvm, u16 *rc, u16 *rrc)
2208 struct kvm_vcpu *vcpu;
2210 kvm_for_each_vcpu(i, vcpu, kvm) {
2211 mutex_lock(&vcpu->mutex);
2212 r = kvm_s390_pv_create_cpu(vcpu, rc, rrc);
2213 mutex_unlock(&vcpu->mutex);
2218 kvm_s390_cpus_from_pv(kvm, &dummy, &dummy);
2222 static int kvm_s390_handle_pv(struct kvm *kvm, struct kvm_pv_cmd *cmd)
2226 void __user *argp = (void __user *)cmd->data;
2229 case KVM_PV_ENABLE: {
2231 if (kvm_s390_pv_is_protected(kvm))
2235 * FMT 4 SIE needs esca. As we never switch back to bsca from
2236 * esca, we need no cleanup in the error cases below
2238 r = sca_switch_to_extended(kvm);
2242 down_write(¤t->mm->mmap_sem);
2243 r = gmap_mark_unmergeable();
2244 up_write(¤t->mm->mmap_sem);
2248 r = kvm_s390_pv_init_vm(kvm, &cmd->rc, &cmd->rrc);
2252 r = kvm_s390_cpus_to_pv(kvm, &cmd->rc, &cmd->rrc);
2254 kvm_s390_pv_deinit_vm(kvm, &dummy, &dummy);
2256 /* we need to block service interrupts from now on */
2257 set_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2260 case KVM_PV_DISABLE: {
2262 if (!kvm_s390_pv_is_protected(kvm))
2265 r = kvm_s390_cpus_from_pv(kvm, &cmd->rc, &cmd->rrc);
2267 * If a CPU could not be destroyed, destroy VM will also fail.
2268 * There is no point in trying to destroy it. Instead return
2269 * the rc and rrc from the first CPU that failed destroying.
2273 r = kvm_s390_pv_deinit_vm(kvm, &cmd->rc, &cmd->rrc);
2275 /* no need to block service interrupts any more */
2276 clear_bit(IRQ_PEND_EXT_SERVICE, &kvm->arch.float_int.masked_irqs);
2279 case KVM_PV_SET_SEC_PARMS: {
2280 struct kvm_s390_pv_sec_parm parms = {};
2284 if (!kvm_s390_pv_is_protected(kvm))
2288 if (copy_from_user(&parms, argp, sizeof(parms)))
2291 /* Currently restricted to 8KB */
2293 if (parms.length > PAGE_SIZE * 2)
2297 hdr = vmalloc(parms.length);
2302 if (!copy_from_user(hdr, (void __user *)parms.origin,
2304 r = kvm_s390_pv_set_sec_parms(kvm, hdr, parms.length,
2305 &cmd->rc, &cmd->rrc);
2310 case KVM_PV_UNPACK: {
2311 struct kvm_s390_pv_unp unp = {};
2314 if (!kvm_s390_pv_is_protected(kvm))
2318 if (copy_from_user(&unp, argp, sizeof(unp)))
2321 r = kvm_s390_pv_unpack(kvm, unp.addr, unp.size, unp.tweak,
2322 &cmd->rc, &cmd->rrc);
2325 case KVM_PV_VERIFY: {
2327 if (!kvm_s390_pv_is_protected(kvm))
2330 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2331 UVC_CMD_VERIFY_IMG, &cmd->rc, &cmd->rrc);
2332 KVM_UV_EVENT(kvm, 3, "PROTVIRT VERIFY: rc %x rrc %x", cmd->rc,
2336 case KVM_PV_PREP_RESET: {
2338 if (!kvm_s390_pv_is_protected(kvm))
2341 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2342 UVC_CMD_PREPARE_RESET, &cmd->rc, &cmd->rrc);
2343 KVM_UV_EVENT(kvm, 3, "PROTVIRT PREP RESET: rc %x rrc %x",
2347 case KVM_PV_UNSHARE_ALL: {
2349 if (!kvm_s390_pv_is_protected(kvm))
2352 r = uv_cmd_nodata(kvm_s390_pv_get_handle(kvm),
2353 UVC_CMD_SET_UNSHARE_ALL, &cmd->rc, &cmd->rrc);
2354 KVM_UV_EVENT(kvm, 3, "PROTVIRT UNSHARE: rc %x rrc %x",
2364 long kvm_arch_vm_ioctl(struct file *filp,
2365 unsigned int ioctl, unsigned long arg)
2367 struct kvm *kvm = filp->private_data;
2368 void __user *argp = (void __user *)arg;
2369 struct kvm_device_attr attr;
2373 case KVM_S390_INTERRUPT: {
2374 struct kvm_s390_interrupt s390int;
2377 if (copy_from_user(&s390int, argp, sizeof(s390int)))
2379 r = kvm_s390_inject_vm(kvm, &s390int);
2382 case KVM_CREATE_IRQCHIP: {
2383 struct kvm_irq_routing_entry routing;
2386 if (kvm->arch.use_irqchip) {
2387 /* Set up dummy routing. */
2388 memset(&routing, 0, sizeof(routing));
2389 r = kvm_set_irq_routing(kvm, &routing, 0, 0);
2393 case KVM_SET_DEVICE_ATTR: {
2395 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2397 r = kvm_s390_vm_set_attr(kvm, &attr);
2400 case KVM_GET_DEVICE_ATTR: {
2402 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2404 r = kvm_s390_vm_get_attr(kvm, &attr);
2407 case KVM_HAS_DEVICE_ATTR: {
2409 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
2411 r = kvm_s390_vm_has_attr(kvm, &attr);
2414 case KVM_S390_GET_SKEYS: {
2415 struct kvm_s390_skeys args;
2418 if (copy_from_user(&args, argp,
2419 sizeof(struct kvm_s390_skeys)))
2421 r = kvm_s390_get_skeys(kvm, &args);
2424 case KVM_S390_SET_SKEYS: {
2425 struct kvm_s390_skeys args;
2428 if (copy_from_user(&args, argp,
2429 sizeof(struct kvm_s390_skeys)))
2431 r = kvm_s390_set_skeys(kvm, &args);
2434 case KVM_S390_GET_CMMA_BITS: {
2435 struct kvm_s390_cmma_log args;
2438 if (copy_from_user(&args, argp, sizeof(args)))
2440 mutex_lock(&kvm->slots_lock);
2441 r = kvm_s390_get_cmma_bits(kvm, &args);
2442 mutex_unlock(&kvm->slots_lock);
2444 r = copy_to_user(argp, &args, sizeof(args));
2450 case KVM_S390_SET_CMMA_BITS: {
2451 struct kvm_s390_cmma_log args;
2454 if (copy_from_user(&args, argp, sizeof(args)))
2456 mutex_lock(&kvm->slots_lock);
2457 r = kvm_s390_set_cmma_bits(kvm, &args);
2458 mutex_unlock(&kvm->slots_lock);
2461 case KVM_S390_PV_COMMAND: {
2462 struct kvm_pv_cmd args;
2464 /* protvirt means user sigp */
2465 kvm->arch.user_cpu_state_ctrl = 1;
2467 if (!is_prot_virt_host()) {
2471 if (copy_from_user(&args, argp, sizeof(args))) {
2479 mutex_lock(&kvm->lock);
2480 r = kvm_s390_handle_pv(kvm, &args);
2481 mutex_unlock(&kvm->lock);
2482 if (copy_to_user(argp, &args, sizeof(args))) {
2495 static int kvm_s390_apxa_installed(void)
2497 struct ap_config_info info;
2499 if (ap_instructions_available()) {
2500 if (ap_qci(&info) == 0)
2508 * The format of the crypto control block (CRYCB) is specified in the 3 low
2509 * order bits of the CRYCB designation (CRYCBD) field as follows:
2510 * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2511 * AP extended addressing (APXA) facility are installed.
2512 * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2513 * Format 2: Both the APXA and MSAX3 facilities are installed
2515 static void kvm_s390_set_crycb_format(struct kvm *kvm)
2517 kvm->arch.crypto.crycbd = (__u32)(unsigned long) kvm->arch.crypto.crycb;
2519 /* Clear the CRYCB format bits - i.e., set format 0 by default */
2520 kvm->arch.crypto.crycbd &= ~(CRYCB_FORMAT_MASK);
2522 /* Check whether MSAX3 is installed */
2523 if (!test_kvm_facility(kvm, 76))
2526 if (kvm_s390_apxa_installed())
2527 kvm->arch.crypto.crycbd |= CRYCB_FORMAT2;
2529 kvm->arch.crypto.crycbd |= CRYCB_FORMAT1;
2532 void kvm_arch_crypto_set_masks(struct kvm *kvm, unsigned long *apm,
2533 unsigned long *aqm, unsigned long *adm)
2535 struct kvm_s390_crypto_cb *crycb = kvm->arch.crypto.crycb;
2537 mutex_lock(&kvm->lock);
2538 kvm_s390_vcpu_block_all(kvm);
2540 switch (kvm->arch.crypto.crycbd & CRYCB_FORMAT_MASK) {
2541 case CRYCB_FORMAT2: /* APCB1 use 256 bits */
2542 memcpy(crycb->apcb1.apm, apm, 32);
2543 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
2544 apm[0], apm[1], apm[2], apm[3]);
2545 memcpy(crycb->apcb1.aqm, aqm, 32);
2546 VM_EVENT(kvm, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
2547 aqm[0], aqm[1], aqm[2], aqm[3]);
2548 memcpy(crycb->apcb1.adm, adm, 32);
2549 VM_EVENT(kvm, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
2550 adm[0], adm[1], adm[2], adm[3]);
2553 case CRYCB_FORMAT0: /* Fall through both use APCB0 */
2554 memcpy(crycb->apcb0.apm, apm, 8);
2555 memcpy(crycb->apcb0.aqm, aqm, 2);
2556 memcpy(crycb->apcb0.adm, adm, 2);
2557 VM_EVENT(kvm, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
2558 apm[0], *((unsigned short *)aqm),
2559 *((unsigned short *)adm));
2561 default: /* Can not happen */
2565 /* recreate the shadow crycb for each vcpu */
2566 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2567 kvm_s390_vcpu_unblock_all(kvm);
2568 mutex_unlock(&kvm->lock);
2570 EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks);
2572 void kvm_arch_crypto_clear_masks(struct kvm *kvm)
2574 mutex_lock(&kvm->lock);
2575 kvm_s390_vcpu_block_all(kvm);
2577 memset(&kvm->arch.crypto.crycb->apcb0, 0,
2578 sizeof(kvm->arch.crypto.crycb->apcb0));
2579 memset(&kvm->arch.crypto.crycb->apcb1, 0,
2580 sizeof(kvm->arch.crypto.crycb->apcb1));
2582 VM_EVENT(kvm, 3, "%s", "CLR CRYCB:");
2583 /* recreate the shadow crycb for each vcpu */
2584 kvm_s390_sync_request_broadcast(kvm, KVM_REQ_VSIE_RESTART);
2585 kvm_s390_vcpu_unblock_all(kvm);
2586 mutex_unlock(&kvm->lock);
2588 EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks);
2590 static u64 kvm_s390_get_initial_cpuid(void)
2595 cpuid.version = 0xff;
2596 return *((u64 *) &cpuid);
2599 static void kvm_s390_crypto_init(struct kvm *kvm)
2601 kvm->arch.crypto.crycb = &kvm->arch.sie_page2->crycb;
2602 kvm_s390_set_crycb_format(kvm);
2604 if (!test_kvm_facility(kvm, 76))
2607 /* Enable AES/DEA protected key functions by default */
2608 kvm->arch.crypto.aes_kw = 1;
2609 kvm->arch.crypto.dea_kw = 1;
2610 get_random_bytes(kvm->arch.crypto.crycb->aes_wrapping_key_mask,
2611 sizeof(kvm->arch.crypto.crycb->aes_wrapping_key_mask));
2612 get_random_bytes(kvm->arch.crypto.crycb->dea_wrapping_key_mask,
2613 sizeof(kvm->arch.crypto.crycb->dea_wrapping_key_mask));
2616 static void sca_dispose(struct kvm *kvm)
2618 if (kvm->arch.use_esca)
2619 free_pages_exact(kvm->arch.sca, sizeof(struct esca_block));
2621 free_page((unsigned long)(kvm->arch.sca));
2622 kvm->arch.sca = NULL;
2625 int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
2627 gfp_t alloc_flags = GFP_KERNEL;
2629 char debug_name[16];
2630 static unsigned long sca_offset;
2633 #ifdef CONFIG_KVM_S390_UCONTROL
2634 if (type & ~KVM_VM_S390_UCONTROL)
2636 if ((type & KVM_VM_S390_UCONTROL) && (!capable(CAP_SYS_ADMIN)))
2643 rc = s390_enable_sie();
2649 if (!sclp.has_64bscao)
2650 alloc_flags |= GFP_DMA;
2651 rwlock_init(&kvm->arch.sca_lock);
2652 /* start with basic SCA */
2653 kvm->arch.sca = (struct bsca_block *) get_zeroed_page(alloc_flags);
2656 mutex_lock(&kvm_lock);
2658 if (sca_offset + sizeof(struct bsca_block) > PAGE_SIZE)
2660 kvm->arch.sca = (struct bsca_block *)
2661 ((char *) kvm->arch.sca + sca_offset);
2662 mutex_unlock(&kvm_lock);
2664 sprintf(debug_name, "kvm-%u", current->pid);
2666 kvm->arch.dbf = debug_register(debug_name, 32, 1, 7 * sizeof(long));
2670 BUILD_BUG_ON(sizeof(struct sie_page2) != 4096);
2671 kvm->arch.sie_page2 =
2672 (struct sie_page2 *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
2673 if (!kvm->arch.sie_page2)
2676 kvm->arch.sie_page2->kvm = kvm;
2677 kvm->arch.model.fac_list = kvm->arch.sie_page2->fac_list;
2679 for (i = 0; i < kvm_s390_fac_size(); i++) {
2680 kvm->arch.model.fac_mask[i] = S390_lowcore.stfle_fac_list[i] &
2681 (kvm_s390_fac_base[i] |
2682 kvm_s390_fac_ext[i]);
2683 kvm->arch.model.fac_list[i] = S390_lowcore.stfle_fac_list[i] &
2684 kvm_s390_fac_base[i];
2686 kvm->arch.model.subfuncs = kvm_s390_available_subfunc;
2688 /* we are always in czam mode - even on pre z14 machines */
2689 set_kvm_facility(kvm->arch.model.fac_mask, 138);
2690 set_kvm_facility(kvm->arch.model.fac_list, 138);
2691 /* we emulate STHYI in kvm */
2692 set_kvm_facility(kvm->arch.model.fac_mask, 74);
2693 set_kvm_facility(kvm->arch.model.fac_list, 74);
2694 if (MACHINE_HAS_TLB_GUEST) {
2695 set_kvm_facility(kvm->arch.model.fac_mask, 147);
2696 set_kvm_facility(kvm->arch.model.fac_list, 147);
2699 if (css_general_characteristics.aiv && test_facility(65))
2700 set_kvm_facility(kvm->arch.model.fac_mask, 65);
2702 kvm->arch.model.cpuid = kvm_s390_get_initial_cpuid();
2703 kvm->arch.model.ibc = sclp.ibc & 0x0fff;
2705 kvm_s390_crypto_init(kvm);
2707 mutex_init(&kvm->arch.float_int.ais_lock);
2708 spin_lock_init(&kvm->arch.float_int.lock);
2709 for (i = 0; i < FIRQ_LIST_COUNT; i++)
2710 INIT_LIST_HEAD(&kvm->arch.float_int.lists[i]);
2711 init_waitqueue_head(&kvm->arch.ipte_wq);
2712 mutex_init(&kvm->arch.ipte_mutex);
2714 debug_register_view(kvm->arch.dbf, &debug_sprintf_view);
2715 VM_EVENT(kvm, 3, "vm created with type %lu", type);
2717 if (type & KVM_VM_S390_UCONTROL) {
2718 kvm->arch.gmap = NULL;
2719 kvm->arch.mem_limit = KVM_S390_NO_MEM_LIMIT;
2721 if (sclp.hamax == U64_MAX)
2722 kvm->arch.mem_limit = TASK_SIZE_MAX;
2724 kvm->arch.mem_limit = min_t(unsigned long, TASK_SIZE_MAX,
2726 kvm->arch.gmap = gmap_create(current->mm, kvm->arch.mem_limit - 1);
2727 if (!kvm->arch.gmap)
2729 kvm->arch.gmap->private = kvm;
2730 kvm->arch.gmap->pfault_enabled = 0;
2733 kvm->arch.use_pfmfi = sclp.has_pfmfi;
2734 kvm->arch.use_skf = sclp.has_skey;
2735 spin_lock_init(&kvm->arch.start_stop_lock);
2736 kvm_s390_vsie_init(kvm);
2738 kvm_s390_gisa_init(kvm);
2739 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm, current->pid);
2743 free_page((unsigned long)kvm->arch.sie_page2);
2744 debug_unregister(kvm->arch.dbf);
2746 KVM_EVENT(3, "creation of vm failed: %d", rc);
2750 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
2754 VCPU_EVENT(vcpu, 3, "%s", "free cpu");
2755 trace_kvm_s390_destroy_vcpu(vcpu->vcpu_id);
2756 kvm_s390_clear_local_irqs(vcpu);
2757 kvm_clear_async_pf_completion_queue(vcpu);
2758 if (!kvm_is_ucontrol(vcpu->kvm))
2761 if (kvm_is_ucontrol(vcpu->kvm))
2762 gmap_remove(vcpu->arch.gmap);
2764 if (vcpu->kvm->arch.use_cmma)
2765 kvm_s390_vcpu_unsetup_cmma(vcpu);
2766 /* We can not hold the vcpu mutex here, we are already dying */
2767 if (kvm_s390_pv_cpu_get_handle(vcpu))
2768 kvm_s390_pv_destroy_cpu(vcpu, &rc, &rrc);
2769 free_page((unsigned long)(vcpu->arch.sie_block));
2772 static void kvm_free_vcpus(struct kvm *kvm)
2775 struct kvm_vcpu *vcpu;
2777 kvm_for_each_vcpu(i, vcpu, kvm)
2778 kvm_vcpu_destroy(vcpu);
2780 mutex_lock(&kvm->lock);
2781 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
2782 kvm->vcpus[i] = NULL;
2784 atomic_set(&kvm->online_vcpus, 0);
2785 mutex_unlock(&kvm->lock);
2788 void kvm_arch_destroy_vm(struct kvm *kvm)
2792 kvm_free_vcpus(kvm);
2794 kvm_s390_gisa_destroy(kvm);
2796 * We are already at the end of life and kvm->lock is not taken.
2797 * This is ok as the file descriptor is closed by now and nobody
2798 * can mess with the pv state. To avoid lockdep_assert_held from
2799 * complaining we do not use kvm_s390_pv_is_protected.
2801 if (kvm_s390_pv_get_handle(kvm))
2802 kvm_s390_pv_deinit_vm(kvm, &rc, &rrc);
2803 debug_unregister(kvm->arch.dbf);
2804 free_page((unsigned long)kvm->arch.sie_page2);
2805 if (!kvm_is_ucontrol(kvm))
2806 gmap_remove(kvm->arch.gmap);
2807 kvm_s390_destroy_adapters(kvm);
2808 kvm_s390_clear_float_irqs(kvm);
2809 kvm_s390_vsie_destroy(kvm);
2810 KVM_EVENT(3, "vm 0x%pK destroyed", kvm);
2813 /* Section: vcpu related */
2814 static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu *vcpu)
2816 vcpu->arch.gmap = gmap_create(current->mm, -1UL);
2817 if (!vcpu->arch.gmap)
2819 vcpu->arch.gmap->private = vcpu->kvm;
2824 static void sca_del_vcpu(struct kvm_vcpu *vcpu)
2826 if (!kvm_s390_use_sca_entries())
2828 read_lock(&vcpu->kvm->arch.sca_lock);
2829 if (vcpu->kvm->arch.use_esca) {
2830 struct esca_block *sca = vcpu->kvm->arch.sca;
2832 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2833 sca->cpu[vcpu->vcpu_id].sda = 0;
2835 struct bsca_block *sca = vcpu->kvm->arch.sca;
2837 clear_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2838 sca->cpu[vcpu->vcpu_id].sda = 0;
2840 read_unlock(&vcpu->kvm->arch.sca_lock);
2843 static void sca_add_vcpu(struct kvm_vcpu *vcpu)
2845 if (!kvm_s390_use_sca_entries()) {
2846 struct bsca_block *sca = vcpu->kvm->arch.sca;
2848 /* we still need the basic sca for the ipte control */
2849 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2850 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2853 read_lock(&vcpu->kvm->arch.sca_lock);
2854 if (vcpu->kvm->arch.use_esca) {
2855 struct esca_block *sca = vcpu->kvm->arch.sca;
2857 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2858 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2859 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca & ~0x3fU;
2860 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2861 set_bit_inv(vcpu->vcpu_id, (unsigned long *) sca->mcn);
2863 struct bsca_block *sca = vcpu->kvm->arch.sca;
2865 sca->cpu[vcpu->vcpu_id].sda = (__u64) vcpu->arch.sie_block;
2866 vcpu->arch.sie_block->scaoh = (__u32)(((__u64)sca) >> 32);
2867 vcpu->arch.sie_block->scaol = (__u32)(__u64)sca;
2868 set_bit_inv(vcpu->vcpu_id, (unsigned long *) &sca->mcn);
2870 read_unlock(&vcpu->kvm->arch.sca_lock);
2873 /* Basic SCA to Extended SCA data copy routines */
2874 static inline void sca_copy_entry(struct esca_entry *d, struct bsca_entry *s)
2877 d->sigp_ctrl.c = s->sigp_ctrl.c;
2878 d->sigp_ctrl.scn = s->sigp_ctrl.scn;
2881 static void sca_copy_b_to_e(struct esca_block *d, struct bsca_block *s)
2885 d->ipte_control = s->ipte_control;
2887 for (i = 0; i < KVM_S390_BSCA_CPU_SLOTS; i++)
2888 sca_copy_entry(&d->cpu[i], &s->cpu[i]);
2891 static int sca_switch_to_extended(struct kvm *kvm)
2893 struct bsca_block *old_sca = kvm->arch.sca;
2894 struct esca_block *new_sca;
2895 struct kvm_vcpu *vcpu;
2896 unsigned int vcpu_idx;
2899 if (kvm->arch.use_esca)
2902 new_sca = alloc_pages_exact(sizeof(*new_sca), GFP_KERNEL|__GFP_ZERO);
2906 scaoh = (u32)((u64)(new_sca) >> 32);
2907 scaol = (u32)(u64)(new_sca) & ~0x3fU;
2909 kvm_s390_vcpu_block_all(kvm);
2910 write_lock(&kvm->arch.sca_lock);
2912 sca_copy_b_to_e(new_sca, old_sca);
2914 kvm_for_each_vcpu(vcpu_idx, vcpu, kvm) {
2915 vcpu->arch.sie_block->scaoh = scaoh;
2916 vcpu->arch.sie_block->scaol = scaol;
2917 vcpu->arch.sie_block->ecb2 |= ECB2_ESCA;
2919 kvm->arch.sca = new_sca;
2920 kvm->arch.use_esca = 1;
2922 write_unlock(&kvm->arch.sca_lock);
2923 kvm_s390_vcpu_unblock_all(kvm);
2925 free_page((unsigned long)old_sca);
2927 VM_EVENT(kvm, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
2928 old_sca, kvm->arch.sca);
2932 static int sca_can_add_vcpu(struct kvm *kvm, unsigned int id)
2936 if (!kvm_s390_use_sca_entries()) {
2937 if (id < KVM_MAX_VCPUS)
2941 if (id < KVM_S390_BSCA_CPU_SLOTS)
2943 if (!sclp.has_esca || !sclp.has_64bscao)
2946 mutex_lock(&kvm->lock);
2947 rc = kvm->arch.use_esca ? 0 : sca_switch_to_extended(kvm);
2948 mutex_unlock(&kvm->lock);
2950 return rc == 0 && id < KVM_S390_ESCA_CPU_SLOTS;
2953 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2954 static void __start_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2956 WARN_ON_ONCE(vcpu->arch.cputm_start != 0);
2957 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2958 vcpu->arch.cputm_start = get_tod_clock_fast();
2959 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2962 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2963 static void __stop_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2965 WARN_ON_ONCE(vcpu->arch.cputm_start == 0);
2966 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
2967 vcpu->arch.sie_block->cputm -= get_tod_clock_fast() - vcpu->arch.cputm_start;
2968 vcpu->arch.cputm_start = 0;
2969 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
2972 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2973 static void __enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2975 WARN_ON_ONCE(vcpu->arch.cputm_enabled);
2976 vcpu->arch.cputm_enabled = true;
2977 __start_cpu_timer_accounting(vcpu);
2980 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2981 static void __disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2983 WARN_ON_ONCE(!vcpu->arch.cputm_enabled);
2984 __stop_cpu_timer_accounting(vcpu);
2985 vcpu->arch.cputm_enabled = false;
2988 static void enable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2990 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2991 __enable_cpu_timer_accounting(vcpu);
2995 static void disable_cpu_timer_accounting(struct kvm_vcpu *vcpu)
2997 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2998 __disable_cpu_timer_accounting(vcpu);
3002 /* set the cpu timer - may only be called from the VCPU thread itself */
3003 void kvm_s390_set_cpu_timer(struct kvm_vcpu *vcpu, __u64 cputm)
3005 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3006 raw_write_seqcount_begin(&vcpu->arch.cputm_seqcount);
3007 if (vcpu->arch.cputm_enabled)
3008 vcpu->arch.cputm_start = get_tod_clock_fast();
3009 vcpu->arch.sie_block->cputm = cputm;
3010 raw_write_seqcount_end(&vcpu->arch.cputm_seqcount);
3014 /* update and get the cpu timer - can also be called from other VCPU threads */
3015 __u64 kvm_s390_get_cpu_timer(struct kvm_vcpu *vcpu)
3020 if (unlikely(!vcpu->arch.cputm_enabled))
3021 return vcpu->arch.sie_block->cputm;
3023 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
3025 seq = raw_read_seqcount(&vcpu->arch.cputm_seqcount);
3027 * If the writer would ever execute a read in the critical
3028 * section, e.g. in irq context, we have a deadlock.
3030 WARN_ON_ONCE((seq & 1) && smp_processor_id() == vcpu->cpu);
3031 value = vcpu->arch.sie_block->cputm;
3032 /* if cputm_start is 0, accounting is being started/stopped */
3033 if (likely(vcpu->arch.cputm_start))
3034 value -= get_tod_clock_fast() - vcpu->arch.cputm_start;
3035 } while (read_seqcount_retry(&vcpu->arch.cputm_seqcount, seq & ~1));
3040 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3043 gmap_enable(vcpu->arch.enabled_gmap);
3044 kvm_s390_set_cpuflags(vcpu, CPUSTAT_RUNNING);
3045 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3046 __start_cpu_timer_accounting(vcpu);
3050 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3053 if (vcpu->arch.cputm_enabled && !is_vcpu_idle(vcpu))
3054 __stop_cpu_timer_accounting(vcpu);
3055 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_RUNNING);
3056 vcpu->arch.enabled_gmap = gmap_get_enabled();
3057 gmap_disable(vcpu->arch.enabled_gmap);
3061 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
3063 mutex_lock(&vcpu->kvm->lock);
3065 vcpu->arch.sie_block->epoch = vcpu->kvm->arch.epoch;
3066 vcpu->arch.sie_block->epdx = vcpu->kvm->arch.epdx;
3068 mutex_unlock(&vcpu->kvm->lock);
3069 if (!kvm_is_ucontrol(vcpu->kvm)) {
3070 vcpu->arch.gmap = vcpu->kvm->arch.gmap;
3073 if (test_kvm_facility(vcpu->kvm, 74) || vcpu->kvm->arch.user_instr0)
3074 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
3075 /* make vcpu_load load the right gmap on the first trigger */
3076 vcpu->arch.enabled_gmap = vcpu->arch.gmap;
3079 static bool kvm_has_pckmo_subfunc(struct kvm *kvm, unsigned long nr)
3081 if (test_bit_inv(nr, (unsigned long *)&kvm->arch.model.subfuncs.pckmo) &&
3082 test_bit_inv(nr, (unsigned long *)&kvm_s390_available_subfunc.pckmo))
3087 static bool kvm_has_pckmo_ecc(struct kvm *kvm)
3089 /* At least one ECC subfunction must be present */
3090 return kvm_has_pckmo_subfunc(kvm, 32) ||
3091 kvm_has_pckmo_subfunc(kvm, 33) ||
3092 kvm_has_pckmo_subfunc(kvm, 34) ||
3093 kvm_has_pckmo_subfunc(kvm, 40) ||
3094 kvm_has_pckmo_subfunc(kvm, 41);
3098 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu *vcpu)
3101 * If the AP instructions are not being interpreted and the MSAX3
3102 * facility is not configured for the guest, there is nothing to set up.
3104 if (!vcpu->kvm->arch.crypto.apie && !test_kvm_facility(vcpu->kvm, 76))
3107 vcpu->arch.sie_block->crycbd = vcpu->kvm->arch.crypto.crycbd;
3108 vcpu->arch.sie_block->ecb3 &= ~(ECB3_AES | ECB3_DEA);
3109 vcpu->arch.sie_block->eca &= ~ECA_APIE;
3110 vcpu->arch.sie_block->ecd &= ~ECD_ECC;
3112 if (vcpu->kvm->arch.crypto.apie)
3113 vcpu->arch.sie_block->eca |= ECA_APIE;
3115 /* Set up protected key support */
3116 if (vcpu->kvm->arch.crypto.aes_kw) {
3117 vcpu->arch.sie_block->ecb3 |= ECB3_AES;
3118 /* ecc is also wrapped with AES key */
3119 if (kvm_has_pckmo_ecc(vcpu->kvm))
3120 vcpu->arch.sie_block->ecd |= ECD_ECC;
3123 if (vcpu->kvm->arch.crypto.dea_kw)
3124 vcpu->arch.sie_block->ecb3 |= ECB3_DEA;
3127 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu *vcpu)
3129 free_page(vcpu->arch.sie_block->cbrlo);
3130 vcpu->arch.sie_block->cbrlo = 0;
3133 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu *vcpu)
3135 vcpu->arch.sie_block->cbrlo = get_zeroed_page(GFP_KERNEL);
3136 if (!vcpu->arch.sie_block->cbrlo)
3141 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu *vcpu)
3143 struct kvm_s390_cpu_model *model = &vcpu->kvm->arch.model;
3145 vcpu->arch.sie_block->ibc = model->ibc;
3146 if (test_kvm_facility(vcpu->kvm, 7))
3147 vcpu->arch.sie_block->fac = (u32)(u64) model->fac_list;
3150 static int kvm_s390_vcpu_setup(struct kvm_vcpu *vcpu)
3155 atomic_set(&vcpu->arch.sie_block->cpuflags, CPUSTAT_ZARCH |
3159 if (test_kvm_facility(vcpu->kvm, 78))
3160 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED2);
3161 else if (test_kvm_facility(vcpu->kvm, 8))
3162 kvm_s390_set_cpuflags(vcpu, CPUSTAT_GED);
3164 kvm_s390_vcpu_setup_model(vcpu);
3166 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
3167 if (MACHINE_HAS_ESOP)
3168 vcpu->arch.sie_block->ecb |= ECB_HOSTPROTINT;
3169 if (test_kvm_facility(vcpu->kvm, 9))
3170 vcpu->arch.sie_block->ecb |= ECB_SRSI;
3171 if (test_kvm_facility(vcpu->kvm, 73))
3172 vcpu->arch.sie_block->ecb |= ECB_TE;
3174 if (test_kvm_facility(vcpu->kvm, 8) && vcpu->kvm->arch.use_pfmfi)
3175 vcpu->arch.sie_block->ecb2 |= ECB2_PFMFI;
3176 if (test_kvm_facility(vcpu->kvm, 130))
3177 vcpu->arch.sie_block->ecb2 |= ECB2_IEP;
3178 vcpu->arch.sie_block->eca = ECA_MVPGI | ECA_PROTEXCI;
3180 vcpu->arch.sie_block->eca |= ECA_CEI;
3182 vcpu->arch.sie_block->eca |= ECA_IB;
3184 vcpu->arch.sie_block->eca |= ECA_SII;
3185 if (sclp.has_sigpif)
3186 vcpu->arch.sie_block->eca |= ECA_SIGPI;
3187 if (test_kvm_facility(vcpu->kvm, 129)) {
3188 vcpu->arch.sie_block->eca |= ECA_VX;
3189 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
3191 if (test_kvm_facility(vcpu->kvm, 139))
3192 vcpu->arch.sie_block->ecd |= ECD_MEF;
3193 if (test_kvm_facility(vcpu->kvm, 156))
3194 vcpu->arch.sie_block->ecd |= ECD_ETOKENF;
3195 if (vcpu->arch.sie_block->gd) {
3196 vcpu->arch.sie_block->eca |= ECA_AIV;
3197 VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3198 vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3200 vcpu->arch.sie_block->sdnxo = ((unsigned long) &vcpu->run->s.regs.sdnx)
3202 vcpu->arch.sie_block->riccbd = (unsigned long) &vcpu->run->s.regs.riccb;
3205 kvm_s390_set_cpuflags(vcpu, CPUSTAT_KSS);
3207 vcpu->arch.sie_block->ictl |= ICTL_ISKE | ICTL_SSKE | ICTL_RRBE;
3209 if (vcpu->kvm->arch.use_cmma) {
3210 rc = kvm_s390_vcpu_setup_cmma(vcpu);
3214 hrtimer_init(&vcpu->arch.ckc_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3215 vcpu->arch.ckc_timer.function = kvm_s390_idle_wakeup;
3217 vcpu->arch.sie_block->hpid = HPID_KVM;
3219 kvm_s390_vcpu_crypto_setup(vcpu);
3221 mutex_lock(&vcpu->kvm->lock);
3222 if (kvm_s390_pv_is_protected(vcpu->kvm)) {
3223 rc = kvm_s390_pv_create_cpu(vcpu, &uvrc, &uvrrc);
3225 kvm_s390_vcpu_unsetup_cmma(vcpu);
3227 mutex_unlock(&vcpu->kvm->lock);
3232 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
3234 if (!kvm_is_ucontrol(kvm) && !sca_can_add_vcpu(kvm, id))
3239 int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
3241 struct sie_page *sie_page;
3244 BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
3245 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
3249 vcpu->arch.sie_block = &sie_page->sie_block;
3250 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
3252 /* the real guest size will always be smaller than msl */
3253 vcpu->arch.sie_block->mso = 0;
3254 vcpu->arch.sie_block->msl = sclp.hamax;
3256 vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
3257 spin_lock_init(&vcpu->arch.local_int.lock);
3258 vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin;
3259 if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
3260 vcpu->arch.sie_block->gd |= GISA_FORMAT1;
3261 seqcount_init(&vcpu->arch.cputm_seqcount);
3263 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3264 kvm_clear_async_pf_completion_queue(vcpu);
3265 vcpu->run->kvm_valid_regs = KVM_SYNC_PREFIX |
3271 kvm_s390_set_prefix(vcpu, 0);
3272 if (test_kvm_facility(vcpu->kvm, 64))
3273 vcpu->run->kvm_valid_regs |= KVM_SYNC_RICCB;
3274 if (test_kvm_facility(vcpu->kvm, 82))
3275 vcpu->run->kvm_valid_regs |= KVM_SYNC_BPBC;
3276 if (test_kvm_facility(vcpu->kvm, 133))
3277 vcpu->run->kvm_valid_regs |= KVM_SYNC_GSCB;
3278 if (test_kvm_facility(vcpu->kvm, 156))
3279 vcpu->run->kvm_valid_regs |= KVM_SYNC_ETOKEN;
3280 /* fprs can be synchronized via vrs, even if the guest has no vx. With
3281 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
3284 vcpu->run->kvm_valid_regs |= KVM_SYNC_VRS;
3286 vcpu->run->kvm_valid_regs |= KVM_SYNC_FPRS;
3288 if (kvm_is_ucontrol(vcpu->kvm)) {
3289 rc = __kvm_ucontrol_vcpu_init(vcpu);
3291 goto out_free_sie_block;
3294 VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3295 vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3296 trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3298 rc = kvm_s390_vcpu_setup(vcpu);
3300 goto out_ucontrol_uninit;
3303 out_ucontrol_uninit:
3304 if (kvm_is_ucontrol(vcpu->kvm))
3305 gmap_remove(vcpu->arch.gmap);
3307 free_page((unsigned long)(vcpu->arch.sie_block));
3311 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
3313 return kvm_s390_vcpu_has_irq(vcpu, 0);
3316 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
3318 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PSTATE);
3321 void kvm_s390_vcpu_block(struct kvm_vcpu *vcpu)
3323 atomic_or(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3327 void kvm_s390_vcpu_unblock(struct kvm_vcpu *vcpu)
3329 atomic_andnot(PROG_BLOCK_SIE, &vcpu->arch.sie_block->prog20);
3332 static void kvm_s390_vcpu_request(struct kvm_vcpu *vcpu)
3334 atomic_or(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3338 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu *vcpu)
3340 return atomic_read(&vcpu->arch.sie_block->prog20) &
3341 (PROG_BLOCK_SIE | PROG_REQUEST);
3344 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu *vcpu)
3346 atomic_andnot(PROG_REQUEST, &vcpu->arch.sie_block->prog20);
3350 * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
3351 * If the CPU is not running (e.g. waiting as idle) the function will
3352 * return immediately. */
3353 void exit_sie(struct kvm_vcpu *vcpu)
3355 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
3356 kvm_s390_vsie_kick(vcpu);
3357 while (vcpu->arch.sie_block->prog0c & PROG_IN_SIE)
3361 /* Kick a guest cpu out of SIE to process a request synchronously */
3362 void kvm_s390_sync_request(int req, struct kvm_vcpu *vcpu)
3364 kvm_make_request(req, vcpu);
3365 kvm_s390_vcpu_request(vcpu);
3368 static void kvm_gmap_notifier(struct gmap *gmap, unsigned long start,
3371 struct kvm *kvm = gmap->private;
3372 struct kvm_vcpu *vcpu;
3373 unsigned long prefix;
3376 if (gmap_is_shadow(gmap))
3378 if (start >= 1UL << 31)
3379 /* We are only interested in prefix pages */
3381 kvm_for_each_vcpu(i, vcpu, kvm) {
3382 /* match against both prefix pages */
3383 prefix = kvm_s390_get_prefix(vcpu);
3384 if (prefix <= end && start <= prefix + 2*PAGE_SIZE - 1) {
3385 VCPU_EVENT(vcpu, 2, "gmap notifier for %lx-%lx",
3387 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD, vcpu);
3392 bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
3394 /* do not poll with more than halt_poll_max_steal percent of steal time */
3395 if (S390_lowcore.avg_steal_timer * 100 / (TICK_USEC << 12) >=
3396 halt_poll_max_steal) {
3397 vcpu->stat.halt_no_poll_steal++;
3403 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
3405 /* kvm common code refers to this, but never calls it */
3410 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu,
3411 struct kvm_one_reg *reg)
3416 case KVM_REG_S390_TODPR:
3417 r = put_user(vcpu->arch.sie_block->todpr,
3418 (u32 __user *)reg->addr);
3420 case KVM_REG_S390_EPOCHDIFF:
3421 r = put_user(vcpu->arch.sie_block->epoch,
3422 (u64 __user *)reg->addr);
3424 case KVM_REG_S390_CPU_TIMER:
3425 r = put_user(kvm_s390_get_cpu_timer(vcpu),
3426 (u64 __user *)reg->addr);
3428 case KVM_REG_S390_CLOCK_COMP:
3429 r = put_user(vcpu->arch.sie_block->ckc,
3430 (u64 __user *)reg->addr);
3432 case KVM_REG_S390_PFTOKEN:
3433 r = put_user(vcpu->arch.pfault_token,
3434 (u64 __user *)reg->addr);
3436 case KVM_REG_S390_PFCOMPARE:
3437 r = put_user(vcpu->arch.pfault_compare,
3438 (u64 __user *)reg->addr);
3440 case KVM_REG_S390_PFSELECT:
3441 r = put_user(vcpu->arch.pfault_select,
3442 (u64 __user *)reg->addr);
3444 case KVM_REG_S390_PP:
3445 r = put_user(vcpu->arch.sie_block->pp,
3446 (u64 __user *)reg->addr);
3448 case KVM_REG_S390_GBEA:
3449 r = put_user(vcpu->arch.sie_block->gbea,
3450 (u64 __user *)reg->addr);
3459 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu,
3460 struct kvm_one_reg *reg)
3466 case KVM_REG_S390_TODPR:
3467 r = get_user(vcpu->arch.sie_block->todpr,
3468 (u32 __user *)reg->addr);
3470 case KVM_REG_S390_EPOCHDIFF:
3471 r = get_user(vcpu->arch.sie_block->epoch,
3472 (u64 __user *)reg->addr);
3474 case KVM_REG_S390_CPU_TIMER:
3475 r = get_user(val, (u64 __user *)reg->addr);
3477 kvm_s390_set_cpu_timer(vcpu, val);
3479 case KVM_REG_S390_CLOCK_COMP:
3480 r = get_user(vcpu->arch.sie_block->ckc,
3481 (u64 __user *)reg->addr);
3483 case KVM_REG_S390_PFTOKEN:
3484 r = get_user(vcpu->arch.pfault_token,
3485 (u64 __user *)reg->addr);
3486 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3487 kvm_clear_async_pf_completion_queue(vcpu);
3489 case KVM_REG_S390_PFCOMPARE:
3490 r = get_user(vcpu->arch.pfault_compare,
3491 (u64 __user *)reg->addr);
3493 case KVM_REG_S390_PFSELECT:
3494 r = get_user(vcpu->arch.pfault_select,
3495 (u64 __user *)reg->addr);
3497 case KVM_REG_S390_PP:
3498 r = get_user(vcpu->arch.sie_block->pp,
3499 (u64 __user *)reg->addr);
3501 case KVM_REG_S390_GBEA:
3502 r = get_user(vcpu->arch.sie_block->gbea,
3503 (u64 __user *)reg->addr);
3512 static void kvm_arch_vcpu_ioctl_normal_reset(struct kvm_vcpu *vcpu)
3514 vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_RI;
3515 vcpu->arch.pfault_token = KVM_S390_PFAULT_TOKEN_INVALID;
3516 memset(vcpu->run->s.regs.riccb, 0, sizeof(vcpu->run->s.regs.riccb));
3518 kvm_clear_async_pf_completion_queue(vcpu);
3519 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm))
3520 kvm_s390_vcpu_stop(vcpu);
3521 kvm_s390_clear_local_irqs(vcpu);
3524 static void kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu *vcpu)
3526 /* Initial reset is a superset of the normal reset */
3527 kvm_arch_vcpu_ioctl_normal_reset(vcpu);
3530 * This equals initial cpu reset in pop, but we don't switch to ESA.
3531 * We do not only reset the internal data, but also ...
3533 vcpu->arch.sie_block->gpsw.mask = 0;
3534 vcpu->arch.sie_block->gpsw.addr = 0;
3535 kvm_s390_set_prefix(vcpu, 0);
3536 kvm_s390_set_cpu_timer(vcpu, 0);
3537 vcpu->arch.sie_block->ckc = 0;
3538 memset(vcpu->arch.sie_block->gcr, 0, sizeof(vcpu->arch.sie_block->gcr));
3539 vcpu->arch.sie_block->gcr[0] = CR0_INITIAL_MASK;
3540 vcpu->arch.sie_block->gcr[14] = CR14_INITIAL_MASK;
3542 /* ... the data in sync regs */
3543 memset(vcpu->run->s.regs.crs, 0, sizeof(vcpu->run->s.regs.crs));
3544 vcpu->run->s.regs.ckc = 0;
3545 vcpu->run->s.regs.crs[0] = CR0_INITIAL_MASK;
3546 vcpu->run->s.regs.crs[14] = CR14_INITIAL_MASK;
3547 vcpu->run->psw_addr = 0;
3548 vcpu->run->psw_mask = 0;
3549 vcpu->run->s.regs.todpr = 0;
3550 vcpu->run->s.regs.cputm = 0;
3551 vcpu->run->s.regs.ckc = 0;
3552 vcpu->run->s.regs.pp = 0;
3553 vcpu->run->s.regs.gbea = 1;
3554 vcpu->run->s.regs.fpc = 0;
3556 * Do not reset these registers in the protected case, as some of
3557 * them are overlayed and they are not accessible in this case
3560 if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
3561 vcpu->arch.sie_block->gbea = 1;
3562 vcpu->arch.sie_block->pp = 0;
3563 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
3564 vcpu->arch.sie_block->todpr = 0;
3568 static void kvm_arch_vcpu_ioctl_clear_reset(struct kvm_vcpu *vcpu)
3570 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
3572 /* Clear reset is a superset of the initial reset */
3573 kvm_arch_vcpu_ioctl_initial_reset(vcpu);
3575 memset(®s->gprs, 0, sizeof(regs->gprs));
3576 memset(®s->vrs, 0, sizeof(regs->vrs));
3577 memset(®s->acrs, 0, sizeof(regs->acrs));
3578 memset(®s->gscb, 0, sizeof(regs->gscb));
3581 regs->etoken_extension = 0;
3584 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3587 memcpy(&vcpu->run->s.regs.gprs, ®s->gprs, sizeof(regs->gprs));
3592 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
3595 memcpy(®s->gprs, &vcpu->run->s.regs.gprs, sizeof(regs->gprs));
3600 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
3601 struct kvm_sregs *sregs)
3605 memcpy(&vcpu->run->s.regs.acrs, &sregs->acrs, sizeof(sregs->acrs));
3606 memcpy(&vcpu->arch.sie_block->gcr, &sregs->crs, sizeof(sregs->crs));
3612 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
3613 struct kvm_sregs *sregs)
3617 memcpy(&sregs->acrs, &vcpu->run->s.regs.acrs, sizeof(sregs->acrs));
3618 memcpy(&sregs->crs, &vcpu->arch.sie_block->gcr, sizeof(sregs->crs));
3624 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3630 if (test_fp_ctl(fpu->fpc)) {
3634 vcpu->run->s.regs.fpc = fpu->fpc;
3636 convert_fp_to_vx((__vector128 *) vcpu->run->s.regs.vrs,
3637 (freg_t *) fpu->fprs);
3639 memcpy(vcpu->run->s.regs.fprs, &fpu->fprs, sizeof(fpu->fprs));
3646 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
3650 /* make sure we have the latest values */
3653 convert_vx_to_fp((freg_t *) fpu->fprs,
3654 (__vector128 *) vcpu->run->s.regs.vrs);
3656 memcpy(fpu->fprs, vcpu->run->s.regs.fprs, sizeof(fpu->fprs));
3657 fpu->fpc = vcpu->run->s.regs.fpc;
3663 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu *vcpu, psw_t psw)
3667 if (!is_vcpu_stopped(vcpu))
3670 vcpu->run->psw_mask = psw.mask;
3671 vcpu->run->psw_addr = psw.addr;
3676 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
3677 struct kvm_translation *tr)
3679 return -EINVAL; /* not implemented yet */
3682 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
3683 KVM_GUESTDBG_USE_HW_BP | \
3684 KVM_GUESTDBG_ENABLE)
3686 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
3687 struct kvm_guest_debug *dbg)
3693 vcpu->guest_debug = 0;
3694 kvm_s390_clear_bp_data(vcpu);
3696 if (dbg->control & ~VALID_GUESTDBG_FLAGS) {
3700 if (!sclp.has_gpere) {
3705 if (dbg->control & KVM_GUESTDBG_ENABLE) {
3706 vcpu->guest_debug = dbg->control;
3707 /* enforce guest PER */
3708 kvm_s390_set_cpuflags(vcpu, CPUSTAT_P);
3710 if (dbg->control & KVM_GUESTDBG_USE_HW_BP)
3711 rc = kvm_s390_import_bp_data(vcpu, dbg);
3713 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
3714 vcpu->arch.guestdbg.last_bp = 0;
3718 vcpu->guest_debug = 0;
3719 kvm_s390_clear_bp_data(vcpu);
3720 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_P);
3728 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
3729 struct kvm_mp_state *mp_state)
3735 /* CHECK_STOP and LOAD are not supported yet */
3736 ret = is_vcpu_stopped(vcpu) ? KVM_MP_STATE_STOPPED :
3737 KVM_MP_STATE_OPERATING;
3743 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
3744 struct kvm_mp_state *mp_state)
3750 /* user space knows about this interface - let it control the state */
3751 vcpu->kvm->arch.user_cpu_state_ctrl = 1;
3753 switch (mp_state->mp_state) {
3754 case KVM_MP_STATE_STOPPED:
3755 rc = kvm_s390_vcpu_stop(vcpu);
3757 case KVM_MP_STATE_OPERATING:
3758 rc = kvm_s390_vcpu_start(vcpu);
3760 case KVM_MP_STATE_LOAD:
3761 if (!kvm_s390_pv_cpu_is_protected(vcpu)) {
3765 rc = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR_LOAD);
3767 case KVM_MP_STATE_CHECK_STOP:
3768 fallthrough; /* CHECK_STOP and LOAD are not supported yet */
3777 static bool ibs_enabled(struct kvm_vcpu *vcpu)
3779 return kvm_s390_test_cpuflags(vcpu, CPUSTAT_IBS);
3782 static int kvm_s390_handle_requests(struct kvm_vcpu *vcpu)
3785 kvm_s390_vcpu_request_handled(vcpu);
3786 if (!kvm_request_pending(vcpu))
3789 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3790 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
3791 * This ensures that the ipte instruction for this request has
3792 * already finished. We might race against a second unmapper that
3793 * wants to set the blocking bit. Lets just retry the request loop.
3795 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) {
3797 rc = gmap_mprotect_notify(vcpu->arch.gmap,
3798 kvm_s390_get_prefix(vcpu),
3799 PAGE_SIZE * 2, PROT_WRITE);
3801 kvm_make_request(KVM_REQ_MMU_RELOAD, vcpu);
3807 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) {
3808 vcpu->arch.sie_block->ihcpu = 0xffff;
3812 if (kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu)) {
3813 if (!ibs_enabled(vcpu)) {
3814 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 1);
3815 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IBS);
3820 if (kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu)) {
3821 if (ibs_enabled(vcpu)) {
3822 trace_kvm_s390_enable_disable_ibs(vcpu->vcpu_id, 0);
3823 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IBS);
3828 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC, vcpu)) {
3829 vcpu->arch.sie_block->ictl |= ICTL_OPEREXC;
3833 if (kvm_check_request(KVM_REQ_START_MIGRATION, vcpu)) {
3835 * Disable CMM virtualization; we will emulate the ESSA
3836 * instruction manually, in order to provide additional
3837 * functionalities needed for live migration.
3839 vcpu->arch.sie_block->ecb2 &= ~ECB2_CMMA;
3843 if (kvm_check_request(KVM_REQ_STOP_MIGRATION, vcpu)) {
3845 * Re-enable CMM virtualization if CMMA is available and
3846 * CMM has been used.
3848 if ((vcpu->kvm->arch.use_cmma) &&
3849 (vcpu->kvm->mm->context.uses_cmm))
3850 vcpu->arch.sie_block->ecb2 |= ECB2_CMMA;
3854 /* nothing to do, just clear the request */
3855 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
3856 /* we left the vsie handler, nothing to do, just clear the request */
3857 kvm_clear_request(KVM_REQ_VSIE_RESTART, vcpu);
3862 void kvm_s390_set_tod_clock(struct kvm *kvm,
3863 const struct kvm_s390_vm_tod_clock *gtod)
3865 struct kvm_vcpu *vcpu;
3866 struct kvm_s390_tod_clock_ext htod;
3869 mutex_lock(&kvm->lock);
3872 get_tod_clock_ext((char *)&htod);
3874 kvm->arch.epoch = gtod->tod - htod.tod;
3876 if (test_kvm_facility(kvm, 139)) {
3877 kvm->arch.epdx = gtod->epoch_idx - htod.epoch_idx;
3878 if (kvm->arch.epoch > gtod->tod)
3879 kvm->arch.epdx -= 1;
3882 kvm_s390_vcpu_block_all(kvm);
3883 kvm_for_each_vcpu(i, vcpu, kvm) {
3884 vcpu->arch.sie_block->epoch = kvm->arch.epoch;
3885 vcpu->arch.sie_block->epdx = kvm->arch.epdx;
3888 kvm_s390_vcpu_unblock_all(kvm);
3890 mutex_unlock(&kvm->lock);
3894 * kvm_arch_fault_in_page - fault-in guest page if necessary
3895 * @vcpu: The corresponding virtual cpu
3896 * @gpa: Guest physical address
3897 * @writable: Whether the page should be writable or not
3899 * Make sure that a guest page has been faulted-in on the host.
3901 * Return: Zero on success, negative error code otherwise.
3903 long kvm_arch_fault_in_page(struct kvm_vcpu *vcpu, gpa_t gpa, int writable)
3905 return gmap_fault(vcpu->arch.gmap, gpa,
3906 writable ? FAULT_FLAG_WRITE : 0);
3909 static void __kvm_inject_pfault_token(struct kvm_vcpu *vcpu, bool start_token,
3910 unsigned long token)
3912 struct kvm_s390_interrupt inti;
3913 struct kvm_s390_irq irq;
3916 irq.u.ext.ext_params2 = token;
3917 irq.type = KVM_S390_INT_PFAULT_INIT;
3918 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu, &irq));
3920 inti.type = KVM_S390_INT_PFAULT_DONE;
3921 inti.parm64 = token;
3922 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu->kvm, &inti));
3926 void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
3927 struct kvm_async_pf *work)
3929 trace_kvm_s390_pfault_init(vcpu, work->arch.pfault_token);
3930 __kvm_inject_pfault_token(vcpu, true, work->arch.pfault_token);
3933 void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
3934 struct kvm_async_pf *work)
3936 trace_kvm_s390_pfault_done(vcpu, work->arch.pfault_token);
3937 __kvm_inject_pfault_token(vcpu, false, work->arch.pfault_token);
3940 void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
3941 struct kvm_async_pf *work)
3943 /* s390 will always inject the page directly */
3946 bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
3949 * s390 will always inject the page directly,
3950 * but we still want check_async_completion to cleanup
3955 static int kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu)
3958 struct kvm_arch_async_pf arch;
3961 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
3963 if ((vcpu->arch.sie_block->gpsw.mask & vcpu->arch.pfault_select) !=
3964 vcpu->arch.pfault_compare)
3966 if (psw_extint_disabled(vcpu))
3968 if (kvm_s390_vcpu_has_irq(vcpu, 0))
3970 if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK))
3972 if (!vcpu->arch.gmap->pfault_enabled)
3975 hva = gfn_to_hva(vcpu->kvm, gpa_to_gfn(current->thread.gmap_addr));
3976 hva += current->thread.gmap_addr & ~PAGE_MASK;
3977 if (read_guest_real(vcpu, vcpu->arch.pfault_token, &arch.pfault_token, 8))
3980 rc = kvm_setup_async_pf(vcpu, current->thread.gmap_addr, hva, &arch);
3984 static int vcpu_pre_run(struct kvm_vcpu *vcpu)
3989 * On s390 notifications for arriving pages will be delivered directly
3990 * to the guest but the house keeping for completed pfaults is
3991 * handled outside the worker.
3993 kvm_check_async_pf_completion(vcpu);
3995 vcpu->arch.sie_block->gg14 = vcpu->run->s.regs.gprs[14];
3996 vcpu->arch.sie_block->gg15 = vcpu->run->s.regs.gprs[15];
4001 if (test_cpu_flag(CIF_MCCK_PENDING))
4004 if (!kvm_is_ucontrol(vcpu->kvm)) {
4005 rc = kvm_s390_deliver_pending_interrupts(vcpu);
4010 rc = kvm_s390_handle_requests(vcpu);
4014 if (guestdbg_enabled(vcpu)) {
4015 kvm_s390_backup_guest_per_regs(vcpu);
4016 kvm_s390_patch_guest_per_regs(vcpu);
4019 clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.gisa_int.kicked_mask);
4021 vcpu->arch.sie_block->icptcode = 0;
4022 cpuflags = atomic_read(&vcpu->arch.sie_block->cpuflags);
4023 VCPU_EVENT(vcpu, 6, "entering sie flags %x", cpuflags);
4024 trace_kvm_s390_sie_enter(vcpu, cpuflags);
4029 static int vcpu_post_run_fault_in_sie(struct kvm_vcpu *vcpu)
4031 struct kvm_s390_pgm_info pgm_info = {
4032 .code = PGM_ADDRESSING,
4037 VCPU_EVENT(vcpu, 3, "%s", "fault in sie instruction");
4038 trace_kvm_s390_sie_fault(vcpu);
4041 * We want to inject an addressing exception, which is defined as a
4042 * suppressing or terminating exception. However, since we came here
4043 * by a DAT access exception, the PSW still points to the faulting
4044 * instruction since DAT exceptions are nullifying. So we've got
4045 * to look up the current opcode to get the length of the instruction
4046 * to be able to forward the PSW.
4048 rc = read_guest_instr(vcpu, vcpu->arch.sie_block->gpsw.addr, &opcode, 1);
4049 ilen = insn_length(opcode);
4053 /* Instruction-Fetching Exceptions - we can't detect the ilen.
4054 * Forward by arbitrary ilc, injection will take care of
4055 * nullification if necessary.
4057 pgm_info = vcpu->arch.pgm;
4060 pgm_info.flags = ilen | KVM_S390_PGM_FLAGS_ILC_VALID;
4061 kvm_s390_forward_psw(vcpu, ilen);
4062 return kvm_s390_inject_prog_irq(vcpu, &pgm_info);
4065 static int vcpu_post_run(struct kvm_vcpu *vcpu, int exit_reason)
4067 struct mcck_volatile_info *mcck_info;
4068 struct sie_page *sie_page;
4070 VCPU_EVENT(vcpu, 6, "exit sie icptcode %d",
4071 vcpu->arch.sie_block->icptcode);
4072 trace_kvm_s390_sie_exit(vcpu, vcpu->arch.sie_block->icptcode);
4074 if (guestdbg_enabled(vcpu))
4075 kvm_s390_restore_guest_per_regs(vcpu);
4077 vcpu->run->s.regs.gprs[14] = vcpu->arch.sie_block->gg14;
4078 vcpu->run->s.regs.gprs[15] = vcpu->arch.sie_block->gg15;
4080 if (exit_reason == -EINTR) {
4081 VCPU_EVENT(vcpu, 3, "%s", "machine check");
4082 sie_page = container_of(vcpu->arch.sie_block,
4083 struct sie_page, sie_block);
4084 mcck_info = &sie_page->mcck_info;
4085 kvm_s390_reinject_machine_check(vcpu, mcck_info);
4089 if (vcpu->arch.sie_block->icptcode > 0) {
4090 int rc = kvm_handle_sie_intercept(vcpu);
4092 if (rc != -EOPNOTSUPP)
4094 vcpu->run->exit_reason = KVM_EXIT_S390_SIEIC;
4095 vcpu->run->s390_sieic.icptcode = vcpu->arch.sie_block->icptcode;
4096 vcpu->run->s390_sieic.ipa = vcpu->arch.sie_block->ipa;
4097 vcpu->run->s390_sieic.ipb = vcpu->arch.sie_block->ipb;
4099 } else if (exit_reason != -EFAULT) {
4100 vcpu->stat.exit_null++;
4102 } else if (kvm_is_ucontrol(vcpu->kvm)) {
4103 vcpu->run->exit_reason = KVM_EXIT_S390_UCONTROL;
4104 vcpu->run->s390_ucontrol.trans_exc_code =
4105 current->thread.gmap_addr;
4106 vcpu->run->s390_ucontrol.pgm_code = 0x10;
4108 } else if (current->thread.gmap_pfault) {
4109 trace_kvm_s390_major_guest_pfault(vcpu);
4110 current->thread.gmap_pfault = 0;
4111 if (kvm_arch_setup_async_pf(vcpu))
4113 return kvm_arch_fault_in_page(vcpu, current->thread.gmap_addr, 1);
4115 return vcpu_post_run_fault_in_sie(vcpu);
4118 #define PSW_INT_MASK (PSW_MASK_EXT | PSW_MASK_IO | PSW_MASK_MCHECK)
4119 static int __vcpu_run(struct kvm_vcpu *vcpu)
4121 int rc, exit_reason;
4122 struct sie_page *sie_page = (struct sie_page *)vcpu->arch.sie_block;
4125 * We try to hold kvm->srcu during most of vcpu_run (except when run-
4126 * ning the guest), so that memslots (and other stuff) are protected
4128 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4131 rc = vcpu_pre_run(vcpu);
4135 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4137 * As PF_VCPU will be used in fault handler, between
4138 * guest_enter and guest_exit should be no uaccess.
4140 local_irq_disable();
4141 guest_enter_irqoff();
4142 __disable_cpu_timer_accounting(vcpu);
4144 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4145 memcpy(sie_page->pv_grregs,
4146 vcpu->run->s.regs.gprs,
4147 sizeof(sie_page->pv_grregs));
4149 exit_reason = sie64a(vcpu->arch.sie_block,
4150 vcpu->run->s.regs.gprs);
4151 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4152 memcpy(vcpu->run->s.regs.gprs,
4153 sie_page->pv_grregs,
4154 sizeof(sie_page->pv_grregs));
4156 * We're not allowed to inject interrupts on intercepts
4157 * that leave the guest state in an "in-between" state
4158 * where the next SIE entry will do a continuation.
4159 * Fence interrupts in our "internal" PSW.
4161 if (vcpu->arch.sie_block->icptcode == ICPT_PV_INSTR ||
4162 vcpu->arch.sie_block->icptcode == ICPT_PV_PREF) {
4163 vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
4166 local_irq_disable();
4167 __enable_cpu_timer_accounting(vcpu);
4168 guest_exit_irqoff();
4170 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4172 rc = vcpu_post_run(vcpu, exit_reason);
4173 } while (!signal_pending(current) && !guestdbg_exit_pending(vcpu) && !rc);
4175 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
4179 static void sync_regs_fmt2(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4181 struct runtime_instr_cb *riccb;
4184 riccb = (struct runtime_instr_cb *) &kvm_run->s.regs.riccb;
4185 gscb = (struct gs_cb *) &kvm_run->s.regs.gscb;
4186 vcpu->arch.sie_block->gpsw.mask = kvm_run->psw_mask;
4187 vcpu->arch.sie_block->gpsw.addr = kvm_run->psw_addr;
4188 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4189 vcpu->arch.sie_block->todpr = kvm_run->s.regs.todpr;
4190 vcpu->arch.sie_block->pp = kvm_run->s.regs.pp;
4191 vcpu->arch.sie_block->gbea = kvm_run->s.regs.gbea;
4193 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PFAULT) {
4194 vcpu->arch.pfault_token = kvm_run->s.regs.pft;
4195 vcpu->arch.pfault_select = kvm_run->s.regs.pfs;
4196 vcpu->arch.pfault_compare = kvm_run->s.regs.pfc;
4197 if (vcpu->arch.pfault_token == KVM_S390_PFAULT_TOKEN_INVALID)
4198 kvm_clear_async_pf_completion_queue(vcpu);
4201 * If userspace sets the riccb (e.g. after migration) to a valid state,
4202 * we should enable RI here instead of doing the lazy enablement.
4204 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_RICCB) &&
4205 test_kvm_facility(vcpu->kvm, 64) &&
4207 !(vcpu->arch.sie_block->ecb3 & ECB3_RI)) {
4208 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: RI (sync_regs)");
4209 vcpu->arch.sie_block->ecb3 |= ECB3_RI;
4212 * If userspace sets the gscb (e.g. after migration) to non-zero,
4213 * we should enable GS here instead of doing the lazy enablement.
4215 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_GSCB) &&
4216 test_kvm_facility(vcpu->kvm, 133) &&
4218 !vcpu->arch.gs_enabled) {
4219 VCPU_EVENT(vcpu, 3, "%s", "ENABLE: GS (sync_regs)");
4220 vcpu->arch.sie_block->ecb |= ECB_GS;
4221 vcpu->arch.sie_block->ecd |= ECD_HOSTREGMGMT;
4222 vcpu->arch.gs_enabled = 1;
4224 if ((kvm_run->kvm_dirty_regs & KVM_SYNC_BPBC) &&
4225 test_kvm_facility(vcpu->kvm, 82)) {
4226 vcpu->arch.sie_block->fpf &= ~FPF_BPBC;
4227 vcpu->arch.sie_block->fpf |= kvm_run->s.regs.bpbc ? FPF_BPBC : 0;
4229 if (MACHINE_HAS_GS) {
4231 __ctl_set_bit(2, 4);
4232 if (current->thread.gs_cb) {
4233 vcpu->arch.host_gscb = current->thread.gs_cb;
4234 save_gs_cb(vcpu->arch.host_gscb);
4236 if (vcpu->arch.gs_enabled) {
4237 current->thread.gs_cb = (struct gs_cb *)
4238 &vcpu->run->s.regs.gscb;
4239 restore_gs_cb(current->thread.gs_cb);
4243 /* SIE will load etoken directly from SDNX and therefore kvm_run */
4246 static void sync_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4248 if (kvm_run->kvm_dirty_regs & KVM_SYNC_PREFIX)
4249 kvm_s390_set_prefix(vcpu, kvm_run->s.regs.prefix);
4250 if (kvm_run->kvm_dirty_regs & KVM_SYNC_CRS) {
4251 memcpy(&vcpu->arch.sie_block->gcr, &kvm_run->s.regs.crs, 128);
4252 /* some control register changes require a tlb flush */
4253 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4255 if (kvm_run->kvm_dirty_regs & KVM_SYNC_ARCH0) {
4256 kvm_s390_set_cpu_timer(vcpu, kvm_run->s.regs.cputm);
4257 vcpu->arch.sie_block->ckc = kvm_run->s.regs.ckc;
4259 save_access_regs(vcpu->arch.host_acrs);
4260 restore_access_regs(vcpu->run->s.regs.acrs);
4261 /* save host (userspace) fprs/vrs */
4263 vcpu->arch.host_fpregs.fpc = current->thread.fpu.fpc;
4264 vcpu->arch.host_fpregs.regs = current->thread.fpu.regs;
4266 current->thread.fpu.regs = vcpu->run->s.regs.vrs;
4268 current->thread.fpu.regs = vcpu->run->s.regs.fprs;
4269 current->thread.fpu.fpc = vcpu->run->s.regs.fpc;
4270 if (test_fp_ctl(current->thread.fpu.fpc))
4271 /* User space provided an invalid FPC, let's clear it */
4272 current->thread.fpu.fpc = 0;
4274 /* Sync fmt2 only data */
4275 if (likely(!kvm_s390_pv_cpu_is_protected(vcpu))) {
4276 sync_regs_fmt2(vcpu, kvm_run);
4279 * In several places we have to modify our internal view to
4280 * not do things that are disallowed by the ultravisor. For
4281 * example we must not inject interrupts after specific exits
4282 * (e.g. 112 prefix page not secure). We do this by turning
4283 * off the machine check, external and I/O interrupt bits
4284 * of our PSW copy. To avoid getting validity intercepts, we
4285 * do only accept the condition code from userspace.
4287 vcpu->arch.sie_block->gpsw.mask &= ~PSW_MASK_CC;
4288 vcpu->arch.sie_block->gpsw.mask |= kvm_run->psw_mask &
4292 kvm_run->kvm_dirty_regs = 0;
4295 static void store_regs_fmt2(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4297 kvm_run->s.regs.todpr = vcpu->arch.sie_block->todpr;
4298 kvm_run->s.regs.pp = vcpu->arch.sie_block->pp;
4299 kvm_run->s.regs.gbea = vcpu->arch.sie_block->gbea;
4300 kvm_run->s.regs.bpbc = (vcpu->arch.sie_block->fpf & FPF_BPBC) == FPF_BPBC;
4301 if (MACHINE_HAS_GS) {
4302 __ctl_set_bit(2, 4);
4303 if (vcpu->arch.gs_enabled)
4304 save_gs_cb(current->thread.gs_cb);
4306 current->thread.gs_cb = vcpu->arch.host_gscb;
4307 restore_gs_cb(vcpu->arch.host_gscb);
4309 if (!vcpu->arch.host_gscb)
4310 __ctl_clear_bit(2, 4);
4311 vcpu->arch.host_gscb = NULL;
4313 /* SIE will save etoken directly into SDNX and therefore kvm_run */
4316 static void store_regs(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4318 kvm_run->psw_mask = vcpu->arch.sie_block->gpsw.mask;
4319 kvm_run->psw_addr = vcpu->arch.sie_block->gpsw.addr;
4320 kvm_run->s.regs.prefix = kvm_s390_get_prefix(vcpu);
4321 memcpy(&kvm_run->s.regs.crs, &vcpu->arch.sie_block->gcr, 128);
4322 kvm_run->s.regs.cputm = kvm_s390_get_cpu_timer(vcpu);
4323 kvm_run->s.regs.ckc = vcpu->arch.sie_block->ckc;
4324 kvm_run->s.regs.pft = vcpu->arch.pfault_token;
4325 kvm_run->s.regs.pfs = vcpu->arch.pfault_select;
4326 kvm_run->s.regs.pfc = vcpu->arch.pfault_compare;
4327 save_access_regs(vcpu->run->s.regs.acrs);
4328 restore_access_regs(vcpu->arch.host_acrs);
4329 /* Save guest register state */
4331 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4332 /* Restore will be done lazily at return */
4333 current->thread.fpu.fpc = vcpu->arch.host_fpregs.fpc;
4334 current->thread.fpu.regs = vcpu->arch.host_fpregs.regs;
4335 if (likely(!kvm_s390_pv_cpu_is_protected(vcpu)))
4336 store_regs_fmt2(vcpu, kvm_run);
4339 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
4343 if (kvm_run->immediate_exit)
4346 if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
4347 kvm_run->kvm_dirty_regs & ~KVM_SYNC_S390_VALID_FIELDS)
4352 if (guestdbg_exit_pending(vcpu)) {
4353 kvm_s390_prepare_debug_exit(vcpu);
4358 kvm_sigset_activate(vcpu);
4361 * no need to check the return value of vcpu_start as it can only have
4362 * an error for protvirt, but protvirt means user cpu state
4364 if (!kvm_s390_user_cpu_state_ctrl(vcpu->kvm)) {
4365 kvm_s390_vcpu_start(vcpu);
4366 } else if (is_vcpu_stopped(vcpu)) {
4367 pr_err_ratelimited("can't run stopped vcpu %d\n",
4373 sync_regs(vcpu, kvm_run);
4374 enable_cpu_timer_accounting(vcpu);
4377 rc = __vcpu_run(vcpu);
4379 if (signal_pending(current) && !rc) {
4380 kvm_run->exit_reason = KVM_EXIT_INTR;
4384 if (guestdbg_exit_pending(vcpu) && !rc) {
4385 kvm_s390_prepare_debug_exit(vcpu);
4389 if (rc == -EREMOTE) {
4390 /* userspace support is needed, kvm_run has been prepared */
4394 disable_cpu_timer_accounting(vcpu);
4395 store_regs(vcpu, kvm_run);
4397 kvm_sigset_deactivate(vcpu);
4399 vcpu->stat.exit_userspace++;
4406 * store status at address
4407 * we use have two special cases:
4408 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
4409 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
4411 int kvm_s390_store_status_unloaded(struct kvm_vcpu *vcpu, unsigned long gpa)
4413 unsigned char archmode = 1;
4414 freg_t fprs[NUM_FPRS];
4419 px = kvm_s390_get_prefix(vcpu);
4420 if (gpa == KVM_S390_STORE_STATUS_NOADDR) {
4421 if (write_guest_abs(vcpu, 163, &archmode, 1))
4424 } else if (gpa == KVM_S390_STORE_STATUS_PREFIXED) {
4425 if (write_guest_real(vcpu, 163, &archmode, 1))
4429 gpa -= __LC_FPREGS_SAVE_AREA;
4431 /* manually convert vector registers if necessary */
4432 if (MACHINE_HAS_VX) {
4433 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
4434 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
4437 rc = write_guest_abs(vcpu, gpa + __LC_FPREGS_SAVE_AREA,
4438 vcpu->run->s.regs.fprs, 128);
4440 rc |= write_guest_abs(vcpu, gpa + __LC_GPREGS_SAVE_AREA,
4441 vcpu->run->s.regs.gprs, 128);
4442 rc |= write_guest_abs(vcpu, gpa + __LC_PSW_SAVE_AREA,
4443 &vcpu->arch.sie_block->gpsw, 16);
4444 rc |= write_guest_abs(vcpu, gpa + __LC_PREFIX_SAVE_AREA,
4446 rc |= write_guest_abs(vcpu, gpa + __LC_FP_CREG_SAVE_AREA,
4447 &vcpu->run->s.regs.fpc, 4);
4448 rc |= write_guest_abs(vcpu, gpa + __LC_TOD_PROGREG_SAVE_AREA,
4449 &vcpu->arch.sie_block->todpr, 4);
4450 cputm = kvm_s390_get_cpu_timer(vcpu);
4451 rc |= write_guest_abs(vcpu, gpa + __LC_CPU_TIMER_SAVE_AREA,
4453 clkcomp = vcpu->arch.sie_block->ckc >> 8;
4454 rc |= write_guest_abs(vcpu, gpa + __LC_CLOCK_COMP_SAVE_AREA,
4456 rc |= write_guest_abs(vcpu, gpa + __LC_AREGS_SAVE_AREA,
4457 &vcpu->run->s.regs.acrs, 64);
4458 rc |= write_guest_abs(vcpu, gpa + __LC_CREGS_SAVE_AREA,
4459 &vcpu->arch.sie_block->gcr, 128);
4460 return rc ? -EFAULT : 0;
4463 int kvm_s390_vcpu_store_status(struct kvm_vcpu *vcpu, unsigned long addr)
4466 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
4467 * switch in the run ioctl. Let's update our copies before we save
4468 * it into the save area
4471 vcpu->run->s.regs.fpc = current->thread.fpu.fpc;
4472 save_access_regs(vcpu->run->s.regs.acrs);
4474 return kvm_s390_store_status_unloaded(vcpu, addr);
4477 static void __disable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
4479 kvm_check_request(KVM_REQ_ENABLE_IBS, vcpu);
4480 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS, vcpu);
4483 static void __disable_ibs_on_all_vcpus(struct kvm *kvm)
4486 struct kvm_vcpu *vcpu;
4488 kvm_for_each_vcpu(i, vcpu, kvm) {
4489 __disable_ibs_on_vcpu(vcpu);
4493 static void __enable_ibs_on_vcpu(struct kvm_vcpu *vcpu)
4497 kvm_check_request(KVM_REQ_DISABLE_IBS, vcpu);
4498 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS, vcpu);
4501 int kvm_s390_vcpu_start(struct kvm_vcpu *vcpu)
4503 int i, online_vcpus, r = 0, started_vcpus = 0;
4505 if (!is_vcpu_stopped(vcpu))
4508 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 1);
4509 /* Only one cpu at a time may enter/leave the STOPPED state. */
4510 spin_lock(&vcpu->kvm->arch.start_stop_lock);
4511 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
4513 /* Let's tell the UV that we want to change into the operating state */
4514 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4515 r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_OPR);
4517 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4522 for (i = 0; i < online_vcpus; i++) {
4523 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i]))
4527 if (started_vcpus == 0) {
4528 /* we're the only active VCPU -> speed it up */
4529 __enable_ibs_on_vcpu(vcpu);
4530 } else if (started_vcpus == 1) {
4532 * As we are starting a second VCPU, we have to disable
4533 * the IBS facility on all VCPUs to remove potentially
4534 * oustanding ENABLE requests.
4536 __disable_ibs_on_all_vcpus(vcpu->kvm);
4539 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_STOPPED);
4541 * The real PSW might have changed due to a RESTART interpreted by the
4542 * ultravisor. We block all interrupts and let the next sie exit
4545 if (kvm_s390_pv_cpu_is_protected(vcpu))
4546 vcpu->arch.sie_block->gpsw.mask &= ~PSW_INT_MASK;
4548 * Another VCPU might have used IBS while we were offline.
4549 * Let's play safe and flush the VCPU at startup.
4551 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
4552 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4556 int kvm_s390_vcpu_stop(struct kvm_vcpu *vcpu)
4558 int i, online_vcpus, r = 0, started_vcpus = 0;
4559 struct kvm_vcpu *started_vcpu = NULL;
4561 if (is_vcpu_stopped(vcpu))
4564 trace_kvm_s390_vcpu_start_stop(vcpu->vcpu_id, 0);
4565 /* Only one cpu at a time may enter/leave the STOPPED state. */
4566 spin_lock(&vcpu->kvm->arch.start_stop_lock);
4567 online_vcpus = atomic_read(&vcpu->kvm->online_vcpus);
4569 /* Let's tell the UV that we want to change into the stopped state */
4570 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4571 r = kvm_s390_pv_set_cpu_state(vcpu, PV_CPU_STATE_STP);
4573 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4578 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
4579 kvm_s390_clear_stop_irq(vcpu);
4581 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOPPED);
4582 __disable_ibs_on_vcpu(vcpu);
4584 for (i = 0; i < online_vcpus; i++) {
4585 if (!is_vcpu_stopped(vcpu->kvm->vcpus[i])) {
4587 started_vcpu = vcpu->kvm->vcpus[i];
4591 if (started_vcpus == 1) {
4593 * As we only have one VCPU left, we want to enable the
4594 * IBS facility for that VCPU to speed it up.
4596 __enable_ibs_on_vcpu(started_vcpu);
4599 spin_unlock(&vcpu->kvm->arch.start_stop_lock);
4603 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4604 struct kvm_enable_cap *cap)
4612 case KVM_CAP_S390_CSS_SUPPORT:
4613 if (!vcpu->kvm->arch.css_support) {
4614 vcpu->kvm->arch.css_support = 1;
4615 VM_EVENT(vcpu->kvm, 3, "%s", "ENABLE: CSS support");
4616 trace_kvm_s390_enable_css(vcpu->kvm);
4627 static long kvm_s390_guest_sida_op(struct kvm_vcpu *vcpu,
4628 struct kvm_s390_mem_op *mop)
4630 void __user *uaddr = (void __user *)mop->buf;
4633 if (mop->flags || !mop->size)
4635 if (mop->size + mop->sida_offset < mop->size)
4637 if (mop->size + mop->sida_offset > sida_size(vcpu->arch.sie_block))
4641 case KVM_S390_MEMOP_SIDA_READ:
4642 if (copy_to_user(uaddr, (void *)(sida_origin(vcpu->arch.sie_block) +
4643 mop->sida_offset), mop->size))
4647 case KVM_S390_MEMOP_SIDA_WRITE:
4648 if (copy_from_user((void *)(sida_origin(vcpu->arch.sie_block) +
4649 mop->sida_offset), uaddr, mop->size))
4655 static long kvm_s390_guest_mem_op(struct kvm_vcpu *vcpu,
4656 struct kvm_s390_mem_op *mop)
4658 void __user *uaddr = (void __user *)mop->buf;
4659 void *tmpbuf = NULL;
4661 const u64 supported_flags = KVM_S390_MEMOP_F_INJECT_EXCEPTION
4662 | KVM_S390_MEMOP_F_CHECK_ONLY;
4664 if (mop->flags & ~supported_flags || mop->ar >= NUM_ACRS || !mop->size)
4667 if (mop->size > MEM_OP_MAX_SIZE)
4670 if (kvm_s390_pv_cpu_is_protected(vcpu))
4673 if (!(mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY)) {
4674 tmpbuf = vmalloc(mop->size);
4680 case KVM_S390_MEMOP_LOGICAL_READ:
4681 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4682 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4683 mop->size, GACC_FETCH);
4686 r = read_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4688 if (copy_to_user(uaddr, tmpbuf, mop->size))
4692 case KVM_S390_MEMOP_LOGICAL_WRITE:
4693 if (mop->flags & KVM_S390_MEMOP_F_CHECK_ONLY) {
4694 r = check_gva_range(vcpu, mop->gaddr, mop->ar,
4695 mop->size, GACC_STORE);
4698 if (copy_from_user(tmpbuf, uaddr, mop->size)) {
4702 r = write_guest(vcpu, mop->gaddr, mop->ar, tmpbuf, mop->size);
4706 if (r > 0 && (mop->flags & KVM_S390_MEMOP_F_INJECT_EXCEPTION) != 0)
4707 kvm_s390_inject_prog_irq(vcpu, &vcpu->arch.pgm);
4713 static long kvm_s390_guest_memsida_op(struct kvm_vcpu *vcpu,
4714 struct kvm_s390_mem_op *mop)
4718 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
4721 case KVM_S390_MEMOP_LOGICAL_READ:
4722 case KVM_S390_MEMOP_LOGICAL_WRITE:
4723 r = kvm_s390_guest_mem_op(vcpu, mop);
4725 case KVM_S390_MEMOP_SIDA_READ:
4726 case KVM_S390_MEMOP_SIDA_WRITE:
4727 /* we are locked against sida going away by the vcpu->mutex */
4728 r = kvm_s390_guest_sida_op(vcpu, mop);
4734 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
4738 long kvm_arch_vcpu_async_ioctl(struct file *filp,
4739 unsigned int ioctl, unsigned long arg)
4741 struct kvm_vcpu *vcpu = filp->private_data;
4742 void __user *argp = (void __user *)arg;
4745 case KVM_S390_IRQ: {
4746 struct kvm_s390_irq s390irq;
4748 if (copy_from_user(&s390irq, argp, sizeof(s390irq)))
4750 return kvm_s390_inject_vcpu(vcpu, &s390irq);
4752 case KVM_S390_INTERRUPT: {
4753 struct kvm_s390_interrupt s390int;
4754 struct kvm_s390_irq s390irq = {};
4756 if (copy_from_user(&s390int, argp, sizeof(s390int)))
4758 if (s390int_to_s390irq(&s390int, &s390irq))
4760 return kvm_s390_inject_vcpu(vcpu, &s390irq);
4763 return -ENOIOCTLCMD;
4766 long kvm_arch_vcpu_ioctl(struct file *filp,
4767 unsigned int ioctl, unsigned long arg)
4769 struct kvm_vcpu *vcpu = filp->private_data;
4770 void __user *argp = (void __user *)arg;
4778 case KVM_S390_STORE_STATUS:
4779 idx = srcu_read_lock(&vcpu->kvm->srcu);
4780 r = kvm_s390_store_status_unloaded(vcpu, arg);
4781 srcu_read_unlock(&vcpu->kvm->srcu, idx);
4783 case KVM_S390_SET_INITIAL_PSW: {
4787 if (copy_from_user(&psw, argp, sizeof(psw)))
4789 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
4792 case KVM_S390_CLEAR_RESET:
4794 kvm_arch_vcpu_ioctl_clear_reset(vcpu);
4795 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4796 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
4797 UVC_CMD_CPU_RESET_CLEAR, &rc, &rrc);
4798 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET CLEAR VCPU: rc %x rrc %x",
4802 case KVM_S390_INITIAL_RESET:
4804 kvm_arch_vcpu_ioctl_initial_reset(vcpu);
4805 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4806 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
4807 UVC_CMD_CPU_RESET_INITIAL,
4809 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET INITIAL VCPU: rc %x rrc %x",
4813 case KVM_S390_NORMAL_RESET:
4815 kvm_arch_vcpu_ioctl_normal_reset(vcpu);
4816 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
4817 r = uv_cmd_nodata(kvm_s390_pv_cpu_get_handle(vcpu),
4818 UVC_CMD_CPU_RESET, &rc, &rrc);
4819 VCPU_EVENT(vcpu, 3, "PROTVIRT RESET NORMAL VCPU: rc %x rrc %x",
4823 case KVM_SET_ONE_REG:
4824 case KVM_GET_ONE_REG: {
4825 struct kvm_one_reg reg;
4827 if (kvm_s390_pv_cpu_is_protected(vcpu))
4830 if (copy_from_user(®, argp, sizeof(reg)))
4832 if (ioctl == KVM_SET_ONE_REG)
4833 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®);
4835 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®);
4838 #ifdef CONFIG_KVM_S390_UCONTROL
4839 case KVM_S390_UCAS_MAP: {
4840 struct kvm_s390_ucas_mapping ucasmap;
4842 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4847 if (!kvm_is_ucontrol(vcpu->kvm)) {
4852 r = gmap_map_segment(vcpu->arch.gmap, ucasmap.user_addr,
4853 ucasmap.vcpu_addr, ucasmap.length);
4856 case KVM_S390_UCAS_UNMAP: {
4857 struct kvm_s390_ucas_mapping ucasmap;
4859 if (copy_from_user(&ucasmap, argp, sizeof(ucasmap))) {
4864 if (!kvm_is_ucontrol(vcpu->kvm)) {
4869 r = gmap_unmap_segment(vcpu->arch.gmap, ucasmap.vcpu_addr,
4874 case KVM_S390_VCPU_FAULT: {
4875 r = gmap_fault(vcpu->arch.gmap, arg, 0);
4878 case KVM_ENABLE_CAP:
4880 struct kvm_enable_cap cap;
4882 if (copy_from_user(&cap, argp, sizeof(cap)))
4884 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4887 case KVM_S390_MEM_OP: {
4888 struct kvm_s390_mem_op mem_op;
4890 if (copy_from_user(&mem_op, argp, sizeof(mem_op)) == 0)
4891 r = kvm_s390_guest_memsida_op(vcpu, &mem_op);
4896 case KVM_S390_SET_IRQ_STATE: {
4897 struct kvm_s390_irq_state irq_state;
4900 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4902 if (irq_state.len > VCPU_IRQS_MAX_BUF ||
4903 irq_state.len == 0 ||
4904 irq_state.len % sizeof(struct kvm_s390_irq) > 0) {
4908 /* do not use irq_state.flags, it will break old QEMUs */
4909 r = kvm_s390_set_irq_state(vcpu,
4910 (void __user *) irq_state.buf,
4914 case KVM_S390_GET_IRQ_STATE: {
4915 struct kvm_s390_irq_state irq_state;
4918 if (copy_from_user(&irq_state, argp, sizeof(irq_state)))
4920 if (irq_state.len == 0) {
4924 /* do not use irq_state.flags, it will break old QEMUs */
4925 r = kvm_s390_get_irq_state(vcpu,
4926 (__u8 __user *) irq_state.buf,
4938 vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
4940 #ifdef CONFIG_KVM_S390_UCONTROL
4941 if ((vmf->pgoff == KVM_S390_SIE_PAGE_OFFSET)
4942 && (kvm_is_ucontrol(vcpu->kvm))) {
4943 vmf->page = virt_to_page(vcpu->arch.sie_block);
4944 get_page(vmf->page);
4948 return VM_FAULT_SIGBUS;
4951 /* Section: memory related */
4952 int kvm_arch_prepare_memory_region(struct kvm *kvm,
4953 struct kvm_memory_slot *memslot,
4954 const struct kvm_userspace_memory_region *mem,
4955 enum kvm_mr_change change)
4957 /* A few sanity checks. We can have memory slots which have to be
4958 located/ended at a segment boundary (1MB). The memory in userland is
4959 ok to be fragmented into various different vmas. It is okay to mmap()
4960 and munmap() stuff in this slot after doing this call at any time */
4962 if (mem->userspace_addr & 0xffffful)
4965 if (mem->memory_size & 0xffffful)
4968 if (mem->guest_phys_addr + mem->memory_size > kvm->arch.mem_limit)
4971 /* When we are protected, we should not change the memory slots */
4972 if (kvm_s390_pv_get_handle(kvm))
4977 void kvm_arch_commit_memory_region(struct kvm *kvm,
4978 const struct kvm_userspace_memory_region *mem,
4979 struct kvm_memory_slot *old,
4980 const struct kvm_memory_slot *new,
4981 enum kvm_mr_change change)
4987 rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
4988 old->npages * PAGE_SIZE);
4991 rc = gmap_unmap_segment(kvm->arch.gmap, old->base_gfn * PAGE_SIZE,
4992 old->npages * PAGE_SIZE);
4997 rc = gmap_map_segment(kvm->arch.gmap, mem->userspace_addr,
4998 mem->guest_phys_addr, mem->memory_size);
5000 case KVM_MR_FLAGS_ONLY:
5003 WARN(1, "Unknown KVM MR CHANGE: %d\n", change);
5006 pr_warn("failed to commit memory region\n");
5010 static inline unsigned long nonhyp_mask(int i)
5012 unsigned int nonhyp_fai = (sclp.hmfai << i * 2) >> 30;
5014 return 0x0000ffffffffffffUL >> (nonhyp_fai << 4);
5017 void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu)
5019 vcpu->valid_wakeup = false;
5022 static int __init kvm_s390_init(void)
5026 if (!sclp.has_sief2) {
5027 pr_info("SIE is not available\n");
5031 if (nested && hpage) {
5032 pr_info("A KVM host that supports nesting cannot back its KVM guests with huge pages\n");
5036 for (i = 0; i < 16; i++)
5037 kvm_s390_fac_base[i] |=
5038 S390_lowcore.stfle_fac_list[i] & nonhyp_mask(i);
5040 return kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
5043 static void __exit kvm_s390_exit(void)
5048 module_init(kvm_s390_init);
5049 module_exit(kvm_s390_exit);
5052 * Enable autoloading of the kvm module.
5053 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
5054 * since x86 takes a different approach.
5056 #include <linux/miscdevice.h>
5057 MODULE_ALIAS_MISCDEV(KVM_MINOR);
5058 MODULE_ALIAS("devname:kvm");