1 // SPDX-License-Identifier: GPL-2.0
3 * hosting IBM Z kernel virtual machines (s390x)
5 * Copyright IBM Corp. 2008, 2018
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 * Christian Borntraeger <borntraeger@de.ibm.com>
9 * Heiko Carstens <heiko.carstens@de.ibm.com>
10 * Christian Ehrhardt <ehrhardt@de.ibm.com>
11 * Jason J. Herne <jjherne@us.ibm.com>
14 #include <linux/compiler.h>
15 #include <linux/err.h>
17 #include <linux/hrtimer.h>
18 #include <linux/init.h>
19 #include <linux/kvm.h>
20 #include <linux/kvm_host.h>
21 #include <linux/mman.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/random.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/vmalloc.h>
28 #include <linux/bitmap.h>
29 #include <linux/sched/signal.h>
30 #include <linux/string.h>
32 #include <asm/asm-offsets.h>
33 #include <asm/lowcore.h>
35 #include <asm/pgtable.h>
38 #include <asm/switch_to.h>
41 #include <asm/cpacf.h>
42 #include <asm/timex.h>
46 #define KMSG_COMPONENT "kvm-s390"
48 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
50 #define CREATE_TRACE_POINTS
52 #include "trace-s390.h"
54 #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
56 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
57 (KVM_MAX_VCPUS + LOCAL_IRQS))
59 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
61 struct kvm_stats_debugfs_item debugfs_entries
[] = {
62 { "userspace_handled", VCPU_STAT(exit_userspace
) },
63 { "exit_null", VCPU_STAT(exit_null
) },
64 { "exit_validity", VCPU_STAT(exit_validity
) },
65 { "exit_stop_request", VCPU_STAT(exit_stop_request
) },
66 { "exit_external_request", VCPU_STAT(exit_external_request
) },
67 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt
) },
68 { "exit_instruction", VCPU_STAT(exit_instruction
) },
69 { "exit_pei", VCPU_STAT(exit_pei
) },
70 { "exit_program_interruption", VCPU_STAT(exit_program_interruption
) },
71 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program
) },
72 { "exit_operation_exception", VCPU_STAT(exit_operation_exception
) },
73 { "halt_successful_poll", VCPU_STAT(halt_successful_poll
) },
74 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll
) },
75 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid
) },
76 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
77 { "instruction_lctlg", VCPU_STAT(instruction_lctlg
) },
78 { "instruction_lctl", VCPU_STAT(instruction_lctl
) },
79 { "instruction_stctl", VCPU_STAT(instruction_stctl
) },
80 { "instruction_stctg", VCPU_STAT(instruction_stctg
) },
81 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal
) },
82 { "deliver_external_call", VCPU_STAT(deliver_external_call
) },
83 { "deliver_service_signal", VCPU_STAT(deliver_service_signal
) },
84 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt
) },
85 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal
) },
86 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal
) },
87 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal
) },
88 { "deliver_program_interruption", VCPU_STAT(deliver_program_int
) },
89 { "exit_wait_state", VCPU_STAT(exit_wait_state
) },
90 { "instruction_epsw", VCPU_STAT(instruction_epsw
) },
91 { "instruction_gs", VCPU_STAT(instruction_gs
) },
92 { "instruction_io_other", VCPU_STAT(instruction_io_other
) },
93 { "instruction_lpsw", VCPU_STAT(instruction_lpsw
) },
94 { "instruction_lpswe", VCPU_STAT(instruction_lpswe
) },
95 { "instruction_pfmf", VCPU_STAT(instruction_pfmf
) },
96 { "instruction_ptff", VCPU_STAT(instruction_ptff
) },
97 { "instruction_stidp", VCPU_STAT(instruction_stidp
) },
98 { "instruction_sck", VCPU_STAT(instruction_sck
) },
99 { "instruction_sckpf", VCPU_STAT(instruction_sckpf
) },
100 { "instruction_spx", VCPU_STAT(instruction_spx
) },
101 { "instruction_stpx", VCPU_STAT(instruction_stpx
) },
102 { "instruction_stap", VCPU_STAT(instruction_stap
) },
103 { "instruction_iske", VCPU_STAT(instruction_iske
) },
104 { "instruction_ri", VCPU_STAT(instruction_ri
) },
105 { "instruction_rrbe", VCPU_STAT(instruction_rrbe
) },
106 { "instruction_sske", VCPU_STAT(instruction_sske
) },
107 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock
) },
108 { "instruction_essa", VCPU_STAT(instruction_essa
) },
109 { "instruction_stsi", VCPU_STAT(instruction_stsi
) },
110 { "instruction_stfl", VCPU_STAT(instruction_stfl
) },
111 { "instruction_tb", VCPU_STAT(instruction_tb
) },
112 { "instruction_tpi", VCPU_STAT(instruction_tpi
) },
113 { "instruction_tprot", VCPU_STAT(instruction_tprot
) },
114 { "instruction_tsch", VCPU_STAT(instruction_tsch
) },
115 { "instruction_sthyi", VCPU_STAT(instruction_sthyi
) },
116 { "instruction_sie", VCPU_STAT(instruction_sie
) },
117 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense
) },
118 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running
) },
119 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call
) },
120 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency
) },
121 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency
) },
122 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start
) },
123 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop
) },
124 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status
) },
125 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status
) },
126 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status
) },
127 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch
) },
128 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix
) },
129 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart
) },
130 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset
) },
131 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset
) },
132 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown
) },
133 { "instruction_diag_10", VCPU_STAT(diagnose_10
) },
134 { "instruction_diag_44", VCPU_STAT(diagnose_44
) },
135 { "instruction_diag_9c", VCPU_STAT(diagnose_9c
) },
136 { "instruction_diag_258", VCPU_STAT(diagnose_258
) },
137 { "instruction_diag_308", VCPU_STAT(diagnose_308
) },
138 { "instruction_diag_500", VCPU_STAT(diagnose_500
) },
139 { "instruction_diag_other", VCPU_STAT(diagnose_other
) },
143 struct kvm_s390_tod_clock_ext
{
149 /* allow nested virtualization in KVM (if enabled by user space) */
151 module_param(nested
, int, S_IRUGO
);
152 MODULE_PARM_DESC(nested
, "Nested virtualization support");
154 /* upper facilities limit for kvm */
155 unsigned long kvm_s390_fac_list_mask
[16] = { FACILITIES_KVM
};
157 unsigned long kvm_s390_fac_list_mask_size(void)
159 BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask
) > S390_ARCH_FAC_MASK_SIZE_U64
);
160 return ARRAY_SIZE(kvm_s390_fac_list_mask
);
163 /* available cpu features supported by kvm */
164 static DECLARE_BITMAP(kvm_s390_available_cpu_feat
, KVM_S390_VM_CPU_FEAT_NR_BITS
);
165 /* available subfunctions indicated via query / "test bit" */
166 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc
;
168 static struct gmap_notifier gmap_notifier
;
169 static struct gmap_notifier vsie_gmap_notifier
;
170 debug_info_t
*kvm_s390_dbf
;
172 /* Section: not file related */
173 int kvm_arch_hardware_enable(void)
175 /* every s390 is virtualization enabled ;-) */
179 static void kvm_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
183 * This callback is executed during stop_machine(). All CPUs are therefore
184 * temporarily stopped. In order not to change guest behavior, we have to
185 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
186 * so a CPU won't be stopped while calculating with the epoch.
188 static int kvm_clock_sync(struct notifier_block
*notifier
, unsigned long val
,
192 struct kvm_vcpu
*vcpu
;
194 unsigned long long *delta
= v
;
196 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
197 kvm
->arch
.epoch
-= *delta
;
198 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
199 vcpu
->arch
.sie_block
->epoch
-= *delta
;
200 if (vcpu
->arch
.cputm_enabled
)
201 vcpu
->arch
.cputm_start
+= *delta
;
202 if (vcpu
->arch
.vsie_block
)
203 vcpu
->arch
.vsie_block
->epoch
-= *delta
;
209 static struct notifier_block kvm_clock_notifier
= {
210 .notifier_call
= kvm_clock_sync
,
213 int kvm_arch_hardware_setup(void)
215 gmap_notifier
.notifier_call
= kvm_gmap_notifier
;
216 gmap_register_pte_notifier(&gmap_notifier
);
217 vsie_gmap_notifier
.notifier_call
= kvm_s390_vsie_gmap_notifier
;
218 gmap_register_pte_notifier(&vsie_gmap_notifier
);
219 atomic_notifier_chain_register(&s390_epoch_delta_notifier
,
220 &kvm_clock_notifier
);
224 void kvm_arch_hardware_unsetup(void)
226 gmap_unregister_pte_notifier(&gmap_notifier
);
227 gmap_unregister_pte_notifier(&vsie_gmap_notifier
);
228 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier
,
229 &kvm_clock_notifier
);
232 static void allow_cpu_feat(unsigned long nr
)
234 set_bit_inv(nr
, kvm_s390_available_cpu_feat
);
237 static inline int plo_test_bit(unsigned char nr
)
239 register unsigned long r0
asm("0") = (unsigned long) nr
| 0x100;
243 /* Parameter registers are ignored for "test bit" */
253 static void kvm_s390_cpu_feat_init(void)
257 for (i
= 0; i
< 256; ++i
) {
259 kvm_s390_available_subfunc
.plo
[i
>> 3] |= 0x80 >> (i
& 7);
262 if (test_facility(28)) /* TOD-clock steering */
263 ptff(kvm_s390_available_subfunc
.ptff
,
264 sizeof(kvm_s390_available_subfunc
.ptff
),
267 if (test_facility(17)) { /* MSA */
268 __cpacf_query(CPACF_KMAC
, (cpacf_mask_t
*)
269 kvm_s390_available_subfunc
.kmac
);
270 __cpacf_query(CPACF_KMC
, (cpacf_mask_t
*)
271 kvm_s390_available_subfunc
.kmc
);
272 __cpacf_query(CPACF_KM
, (cpacf_mask_t
*)
273 kvm_s390_available_subfunc
.km
);
274 __cpacf_query(CPACF_KIMD
, (cpacf_mask_t
*)
275 kvm_s390_available_subfunc
.kimd
);
276 __cpacf_query(CPACF_KLMD
, (cpacf_mask_t
*)
277 kvm_s390_available_subfunc
.klmd
);
279 if (test_facility(76)) /* MSA3 */
280 __cpacf_query(CPACF_PCKMO
, (cpacf_mask_t
*)
281 kvm_s390_available_subfunc
.pckmo
);
282 if (test_facility(77)) { /* MSA4 */
283 __cpacf_query(CPACF_KMCTR
, (cpacf_mask_t
*)
284 kvm_s390_available_subfunc
.kmctr
);
285 __cpacf_query(CPACF_KMF
, (cpacf_mask_t
*)
286 kvm_s390_available_subfunc
.kmf
);
287 __cpacf_query(CPACF_KMO
, (cpacf_mask_t
*)
288 kvm_s390_available_subfunc
.kmo
);
289 __cpacf_query(CPACF_PCC
, (cpacf_mask_t
*)
290 kvm_s390_available_subfunc
.pcc
);
292 if (test_facility(57)) /* MSA5 */
293 __cpacf_query(CPACF_PRNO
, (cpacf_mask_t
*)
294 kvm_s390_available_subfunc
.ppno
);
296 if (test_facility(146)) /* MSA8 */
297 __cpacf_query(CPACF_KMA
, (cpacf_mask_t
*)
298 kvm_s390_available_subfunc
.kma
);
300 if (MACHINE_HAS_ESOP
)
301 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP
);
303 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
304 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
306 if (!sclp
.has_sief2
|| !MACHINE_HAS_ESOP
|| !sclp
.has_64bscao
||
307 !test_facility(3) || !nested
)
309 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2
);
310 if (sclp
.has_64bscao
)
311 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO
);
313 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF
);
315 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE
);
317 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS
);
319 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB
);
321 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI
);
323 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS
);
325 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS
);
327 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
328 * all skey handling functions read/set the skey from the PGSTE
329 * instead of the real storage key.
331 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
332 * pages being detected as preserved although they are resident.
334 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
335 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
337 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
338 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
339 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
341 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
342 * cannot easily shadow the SCA because of the ipte lock.
346 int kvm_arch_init(void *opaque
)
348 kvm_s390_dbf
= debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
352 if (debug_register_view(kvm_s390_dbf
, &debug_sprintf_view
)) {
353 debug_unregister(kvm_s390_dbf
);
357 kvm_s390_cpu_feat_init();
359 /* Register floating interrupt controller interface. */
360 return kvm_register_device_ops(&kvm_flic_ops
, KVM_DEV_TYPE_FLIC
);
363 void kvm_arch_exit(void)
365 debug_unregister(kvm_s390_dbf
);
368 /* Section: device related */
369 long kvm_arch_dev_ioctl(struct file
*filp
,
370 unsigned int ioctl
, unsigned long arg
)
372 if (ioctl
== KVM_S390_ENABLE_SIE
)
373 return s390_enable_sie();
377 int kvm_vm_ioctl_check_extension(struct kvm
*kvm
, long ext
)
382 case KVM_CAP_S390_PSW
:
383 case KVM_CAP_S390_GMAP
:
384 case KVM_CAP_SYNC_MMU
:
385 #ifdef CONFIG_KVM_S390_UCONTROL
386 case KVM_CAP_S390_UCONTROL
:
388 case KVM_CAP_ASYNC_PF
:
389 case KVM_CAP_SYNC_REGS
:
390 case KVM_CAP_ONE_REG
:
391 case KVM_CAP_ENABLE_CAP
:
392 case KVM_CAP_S390_CSS_SUPPORT
:
393 case KVM_CAP_IOEVENTFD
:
394 case KVM_CAP_DEVICE_CTRL
:
395 case KVM_CAP_ENABLE_CAP_VM
:
396 case KVM_CAP_S390_IRQCHIP
:
397 case KVM_CAP_VM_ATTRIBUTES
:
398 case KVM_CAP_MP_STATE
:
399 case KVM_CAP_IMMEDIATE_EXIT
:
400 case KVM_CAP_S390_INJECT_IRQ
:
401 case KVM_CAP_S390_USER_SIGP
:
402 case KVM_CAP_S390_USER_STSI
:
403 case KVM_CAP_S390_SKEYS
:
404 case KVM_CAP_S390_IRQ_STATE
:
405 case KVM_CAP_S390_USER_INSTR0
:
406 case KVM_CAP_S390_CMMA_MIGRATION
:
407 case KVM_CAP_S390_AIS
:
408 case KVM_CAP_S390_AIS_MIGRATION
:
411 case KVM_CAP_S390_MEM_OP
:
414 case KVM_CAP_NR_VCPUS
:
415 case KVM_CAP_MAX_VCPUS
:
416 r
= KVM_S390_BSCA_CPU_SLOTS
;
417 if (!kvm_s390_use_sca_entries())
419 else if (sclp
.has_esca
&& sclp
.has_64bscao
)
420 r
= KVM_S390_ESCA_CPU_SLOTS
;
422 case KVM_CAP_NR_MEMSLOTS
:
423 r
= KVM_USER_MEM_SLOTS
;
425 case KVM_CAP_S390_COW
:
426 r
= MACHINE_HAS_ESOP
;
428 case KVM_CAP_S390_VECTOR_REGISTERS
:
431 case KVM_CAP_S390_RI
:
432 r
= test_facility(64);
434 case KVM_CAP_S390_GS
:
435 r
= test_facility(133);
437 case KVM_CAP_S390_BPB
:
438 r
= test_facility(82);
446 static void kvm_s390_sync_dirty_log(struct kvm
*kvm
,
447 struct kvm_memory_slot
*memslot
)
449 gfn_t cur_gfn
, last_gfn
;
450 unsigned long address
;
451 struct gmap
*gmap
= kvm
->arch
.gmap
;
453 /* Loop over all guest pages */
454 last_gfn
= memslot
->base_gfn
+ memslot
->npages
;
455 for (cur_gfn
= memslot
->base_gfn
; cur_gfn
<= last_gfn
; cur_gfn
++) {
456 address
= gfn_to_hva_memslot(memslot
, cur_gfn
);
458 if (test_and_clear_guest_dirty(gmap
->mm
, address
))
459 mark_page_dirty(kvm
, cur_gfn
);
460 if (fatal_signal_pending(current
))
466 /* Section: vm related */
467 static void sca_del_vcpu(struct kvm_vcpu
*vcpu
);
470 * Get (and clear) the dirty memory log for a memory slot.
472 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
473 struct kvm_dirty_log
*log
)
477 struct kvm_memslots
*slots
;
478 struct kvm_memory_slot
*memslot
;
481 if (kvm_is_ucontrol(kvm
))
484 mutex_lock(&kvm
->slots_lock
);
487 if (log
->slot
>= KVM_USER_MEM_SLOTS
)
490 slots
= kvm_memslots(kvm
);
491 memslot
= id_to_memslot(slots
, log
->slot
);
493 if (!memslot
->dirty_bitmap
)
496 kvm_s390_sync_dirty_log(kvm
, memslot
);
497 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
501 /* Clear the dirty log */
503 n
= kvm_dirty_bitmap_bytes(memslot
);
504 memset(memslot
->dirty_bitmap
, 0, n
);
508 mutex_unlock(&kvm
->slots_lock
);
512 static void icpt_operexc_on_all_vcpus(struct kvm
*kvm
)
515 struct kvm_vcpu
*vcpu
;
517 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
518 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC
, vcpu
);
522 static int kvm_vm_ioctl_enable_cap(struct kvm
*kvm
, struct kvm_enable_cap
*cap
)
530 case KVM_CAP_S390_IRQCHIP
:
531 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
532 kvm
->arch
.use_irqchip
= 1;
535 case KVM_CAP_S390_USER_SIGP
:
536 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
537 kvm
->arch
.user_sigp
= 1;
540 case KVM_CAP_S390_VECTOR_REGISTERS
:
541 mutex_lock(&kvm
->lock
);
542 if (kvm
->created_vcpus
) {
544 } else if (MACHINE_HAS_VX
) {
545 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 129);
546 set_kvm_facility(kvm
->arch
.model
.fac_list
, 129);
547 if (test_facility(134)) {
548 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 134);
549 set_kvm_facility(kvm
->arch
.model
.fac_list
, 134);
551 if (test_facility(135)) {
552 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 135);
553 set_kvm_facility(kvm
->arch
.model
.fac_list
, 135);
558 mutex_unlock(&kvm
->lock
);
559 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
560 r
? "(not available)" : "(success)");
562 case KVM_CAP_S390_RI
:
564 mutex_lock(&kvm
->lock
);
565 if (kvm
->created_vcpus
) {
567 } else if (test_facility(64)) {
568 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 64);
569 set_kvm_facility(kvm
->arch
.model
.fac_list
, 64);
572 mutex_unlock(&kvm
->lock
);
573 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_RI %s",
574 r
? "(not available)" : "(success)");
576 case KVM_CAP_S390_AIS
:
577 mutex_lock(&kvm
->lock
);
578 if (kvm
->created_vcpus
) {
581 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 72);
582 set_kvm_facility(kvm
->arch
.model
.fac_list
, 72);
585 mutex_unlock(&kvm
->lock
);
586 VM_EVENT(kvm
, 3, "ENABLE: AIS %s",
587 r
? "(not available)" : "(success)");
589 case KVM_CAP_S390_GS
:
591 mutex_lock(&kvm
->lock
);
592 if (kvm
->created_vcpus
) {
594 } else if (test_facility(133)) {
595 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 133);
596 set_kvm_facility(kvm
->arch
.model
.fac_list
, 133);
599 mutex_unlock(&kvm
->lock
);
600 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_GS %s",
601 r
? "(not available)" : "(success)");
603 case KVM_CAP_S390_USER_STSI
:
604 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
605 kvm
->arch
.user_stsi
= 1;
608 case KVM_CAP_S390_USER_INSTR0
:
609 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
610 kvm
->arch
.user_instr0
= 1;
611 icpt_operexc_on_all_vcpus(kvm
);
621 static int kvm_s390_get_mem_control(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
625 switch (attr
->attr
) {
626 case KVM_S390_VM_MEM_LIMIT_SIZE
:
628 VM_EVENT(kvm
, 3, "QUERY: max guest memory: %lu bytes",
629 kvm
->arch
.mem_limit
);
630 if (put_user(kvm
->arch
.mem_limit
, (u64 __user
*)attr
->addr
))
640 static int kvm_s390_set_mem_control(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
644 switch (attr
->attr
) {
645 case KVM_S390_VM_MEM_ENABLE_CMMA
:
651 VM_EVENT(kvm
, 3, "%s", "ENABLE: CMMA support");
652 mutex_lock(&kvm
->lock
);
653 if (!kvm
->created_vcpus
) {
654 kvm
->arch
.use_cmma
= 1;
657 mutex_unlock(&kvm
->lock
);
659 case KVM_S390_VM_MEM_CLR_CMMA
:
664 if (!kvm
->arch
.use_cmma
)
667 VM_EVENT(kvm
, 3, "%s", "RESET: CMMA states");
668 mutex_lock(&kvm
->lock
);
669 idx
= srcu_read_lock(&kvm
->srcu
);
670 s390_reset_cmma(kvm
->arch
.gmap
->mm
);
671 srcu_read_unlock(&kvm
->srcu
, idx
);
672 mutex_unlock(&kvm
->lock
);
675 case KVM_S390_VM_MEM_LIMIT_SIZE
: {
676 unsigned long new_limit
;
678 if (kvm_is_ucontrol(kvm
))
681 if (get_user(new_limit
, (u64 __user
*)attr
->addr
))
684 if (kvm
->arch
.mem_limit
!= KVM_S390_NO_MEM_LIMIT
&&
685 new_limit
> kvm
->arch
.mem_limit
)
691 /* gmap_create takes last usable address */
692 if (new_limit
!= KVM_S390_NO_MEM_LIMIT
)
696 mutex_lock(&kvm
->lock
);
697 if (!kvm
->created_vcpus
) {
698 /* gmap_create will round the limit up */
699 struct gmap
*new = gmap_create(current
->mm
, new_limit
);
704 gmap_remove(kvm
->arch
.gmap
);
706 kvm
->arch
.gmap
= new;
710 mutex_unlock(&kvm
->lock
);
711 VM_EVENT(kvm
, 3, "SET: max guest address: %lu", new_limit
);
712 VM_EVENT(kvm
, 3, "New guest asce: 0x%pK",
713 (void *) kvm
->arch
.gmap
->asce
);
723 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu
*vcpu
);
725 static int kvm_s390_vm_set_crypto(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
727 struct kvm_vcpu
*vcpu
;
730 if (!test_kvm_facility(kvm
, 76))
733 mutex_lock(&kvm
->lock
);
734 switch (attr
->attr
) {
735 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW
:
737 kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
,
738 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
739 kvm
->arch
.crypto
.aes_kw
= 1;
740 VM_EVENT(kvm
, 3, "%s", "ENABLE: AES keywrapping support");
742 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
:
744 kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
,
745 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
746 kvm
->arch
.crypto
.dea_kw
= 1;
747 VM_EVENT(kvm
, 3, "%s", "ENABLE: DEA keywrapping support");
749 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW
:
750 kvm
->arch
.crypto
.aes_kw
= 0;
751 memset(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
, 0,
752 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
753 VM_EVENT(kvm
, 3, "%s", "DISABLE: AES keywrapping support");
755 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
:
756 kvm
->arch
.crypto
.dea_kw
= 0;
757 memset(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
, 0,
758 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
759 VM_EVENT(kvm
, 3, "%s", "DISABLE: DEA keywrapping support");
762 mutex_unlock(&kvm
->lock
);
766 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
767 kvm_s390_vcpu_crypto_setup(vcpu
);
770 mutex_unlock(&kvm
->lock
);
774 static void kvm_s390_sync_request_broadcast(struct kvm
*kvm
, int req
)
777 struct kvm_vcpu
*vcpu
;
779 kvm_for_each_vcpu(cx
, vcpu
, kvm
)
780 kvm_s390_sync_request(req
, vcpu
);
784 * Must be called with kvm->srcu held to avoid races on memslots, and with
785 * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
787 static int kvm_s390_vm_start_migration(struct kvm
*kvm
)
789 struct kvm_s390_migration_state
*mgs
;
790 struct kvm_memory_slot
*ms
;
791 /* should be the only one */
792 struct kvm_memslots
*slots
;
793 unsigned long ram_pages
;
796 /* migration mode already enabled */
797 if (kvm
->arch
.migration_state
)
800 slots
= kvm_memslots(kvm
);
801 if (!slots
|| !slots
->used_slots
)
804 mgs
= kzalloc(sizeof(*mgs
), GFP_KERNEL
);
807 kvm
->arch
.migration_state
= mgs
;
809 if (kvm
->arch
.use_cmma
) {
811 * Get the first slot. They are reverse sorted by base_gfn, so
812 * the first slot is also the one at the end of the address
813 * space. We have verified above that at least one slot is
816 ms
= slots
->memslots
;
817 /* round up so we only use full longs */
818 ram_pages
= roundup(ms
->base_gfn
+ ms
->npages
, BITS_PER_LONG
);
819 /* allocate enough bytes to store all the bits */
820 mgs
->pgste_bitmap
= vmalloc(ram_pages
/ 8);
821 if (!mgs
->pgste_bitmap
) {
823 kvm
->arch
.migration_state
= NULL
;
827 mgs
->bitmap_size
= ram_pages
;
828 atomic64_set(&mgs
->dirty_pages
, ram_pages
);
829 /* mark all the pages in active slots as dirty */
830 for (slotnr
= 0; slotnr
< slots
->used_slots
; slotnr
++) {
831 ms
= slots
->memslots
+ slotnr
;
832 bitmap_set(mgs
->pgste_bitmap
, ms
->base_gfn
, ms
->npages
);
835 kvm_s390_sync_request_broadcast(kvm
, KVM_REQ_START_MIGRATION
);
841 * Must be called with kvm->slots_lock to avoid races with ourselves and
842 * kvm_s390_vm_start_migration.
844 static int kvm_s390_vm_stop_migration(struct kvm
*kvm
)
846 struct kvm_s390_migration_state
*mgs
;
848 /* migration mode already disabled */
849 if (!kvm
->arch
.migration_state
)
851 mgs
= kvm
->arch
.migration_state
;
852 kvm
->arch
.migration_state
= NULL
;
854 if (kvm
->arch
.use_cmma
) {
855 kvm_s390_sync_request_broadcast(kvm
, KVM_REQ_STOP_MIGRATION
);
856 /* We have to wait for the essa emulation to finish */
857 synchronize_srcu(&kvm
->srcu
);
858 vfree(mgs
->pgste_bitmap
);
864 static int kvm_s390_vm_set_migration(struct kvm
*kvm
,
865 struct kvm_device_attr
*attr
)
869 mutex_lock(&kvm
->slots_lock
);
870 switch (attr
->attr
) {
871 case KVM_S390_VM_MIGRATION_START
:
872 res
= kvm_s390_vm_start_migration(kvm
);
874 case KVM_S390_VM_MIGRATION_STOP
:
875 res
= kvm_s390_vm_stop_migration(kvm
);
880 mutex_unlock(&kvm
->slots_lock
);
885 static int kvm_s390_vm_get_migration(struct kvm
*kvm
,
886 struct kvm_device_attr
*attr
)
888 u64 mig
= (kvm
->arch
.migration_state
!= NULL
);
890 if (attr
->attr
!= KVM_S390_VM_MIGRATION_STATUS
)
893 if (copy_to_user((void __user
*)attr
->addr
, &mig
, sizeof(mig
)))
898 static int kvm_s390_set_tod_ext(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
900 struct kvm_s390_vm_tod_clock gtod
;
902 if (copy_from_user(>od
, (void __user
*)attr
->addr
, sizeof(gtod
)))
905 if (test_kvm_facility(kvm
, 139))
906 kvm_s390_set_tod_clock_ext(kvm
, >od
);
907 else if (gtod
.epoch_idx
== 0)
908 kvm_s390_set_tod_clock(kvm
, gtod
.tod
);
912 VM_EVENT(kvm
, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
913 gtod
.epoch_idx
, gtod
.tod
);
918 static int kvm_s390_set_tod_high(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
922 if (copy_from_user(>od_high
, (void __user
*)attr
->addr
,
928 VM_EVENT(kvm
, 3, "SET: TOD extension: 0x%x", gtod_high
);
933 static int kvm_s390_set_tod_low(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
937 if (copy_from_user(>od
, (void __user
*)attr
->addr
, sizeof(gtod
)))
940 kvm_s390_set_tod_clock(kvm
, gtod
);
941 VM_EVENT(kvm
, 3, "SET: TOD base: 0x%llx", gtod
);
945 static int kvm_s390_set_tod(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
952 switch (attr
->attr
) {
953 case KVM_S390_VM_TOD_EXT
:
954 ret
= kvm_s390_set_tod_ext(kvm
, attr
);
956 case KVM_S390_VM_TOD_HIGH
:
957 ret
= kvm_s390_set_tod_high(kvm
, attr
);
959 case KVM_S390_VM_TOD_LOW
:
960 ret
= kvm_s390_set_tod_low(kvm
, attr
);
969 static void kvm_s390_get_tod_clock_ext(struct kvm
*kvm
,
970 struct kvm_s390_vm_tod_clock
*gtod
)
972 struct kvm_s390_tod_clock_ext htod
;
976 get_tod_clock_ext((char *)&htod
);
978 gtod
->tod
= htod
.tod
+ kvm
->arch
.epoch
;
979 gtod
->epoch_idx
= htod
.epoch_idx
+ kvm
->arch
.epdx
;
981 if (gtod
->tod
< htod
.tod
)
982 gtod
->epoch_idx
+= 1;
987 static int kvm_s390_get_tod_ext(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
989 struct kvm_s390_vm_tod_clock gtod
;
991 memset(>od
, 0, sizeof(gtod
));
993 if (test_kvm_facility(kvm
, 139))
994 kvm_s390_get_tod_clock_ext(kvm
, >od
);
996 gtod
.tod
= kvm_s390_get_tod_clock_fast(kvm
);
998 if (copy_to_user((void __user
*)attr
->addr
, >od
, sizeof(gtod
)))
1001 VM_EVENT(kvm
, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1002 gtod
.epoch_idx
, gtod
.tod
);
1006 static int kvm_s390_get_tod_high(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1010 if (copy_to_user((void __user
*)attr
->addr
, >od_high
,
1013 VM_EVENT(kvm
, 3, "QUERY: TOD extension: 0x%x", gtod_high
);
1018 static int kvm_s390_get_tod_low(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1022 gtod
= kvm_s390_get_tod_clock_fast(kvm
);
1023 if (copy_to_user((void __user
*)attr
->addr
, >od
, sizeof(gtod
)))
1025 VM_EVENT(kvm
, 3, "QUERY: TOD base: 0x%llx", gtod
);
1030 static int kvm_s390_get_tod(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1037 switch (attr
->attr
) {
1038 case KVM_S390_VM_TOD_EXT
:
1039 ret
= kvm_s390_get_tod_ext(kvm
, attr
);
1041 case KVM_S390_VM_TOD_HIGH
:
1042 ret
= kvm_s390_get_tod_high(kvm
, attr
);
1044 case KVM_S390_VM_TOD_LOW
:
1045 ret
= kvm_s390_get_tod_low(kvm
, attr
);
1054 static int kvm_s390_set_processor(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1056 struct kvm_s390_vm_cpu_processor
*proc
;
1057 u16 lowest_ibc
, unblocked_ibc
;
1060 mutex_lock(&kvm
->lock
);
1061 if (kvm
->created_vcpus
) {
1065 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
1070 if (!copy_from_user(proc
, (void __user
*)attr
->addr
,
1072 kvm
->arch
.model
.cpuid
= proc
->cpuid
;
1073 lowest_ibc
= sclp
.ibc
>> 16 & 0xfff;
1074 unblocked_ibc
= sclp
.ibc
& 0xfff;
1075 if (lowest_ibc
&& proc
->ibc
) {
1076 if (proc
->ibc
> unblocked_ibc
)
1077 kvm
->arch
.model
.ibc
= unblocked_ibc
;
1078 else if (proc
->ibc
< lowest_ibc
)
1079 kvm
->arch
.model
.ibc
= lowest_ibc
;
1081 kvm
->arch
.model
.ibc
= proc
->ibc
;
1083 memcpy(kvm
->arch
.model
.fac_list
, proc
->fac_list
,
1084 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1085 VM_EVENT(kvm
, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1086 kvm
->arch
.model
.ibc
,
1087 kvm
->arch
.model
.cpuid
);
1088 VM_EVENT(kvm
, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1089 kvm
->arch
.model
.fac_list
[0],
1090 kvm
->arch
.model
.fac_list
[1],
1091 kvm
->arch
.model
.fac_list
[2]);
1096 mutex_unlock(&kvm
->lock
);
1100 static int kvm_s390_set_processor_feat(struct kvm
*kvm
,
1101 struct kvm_device_attr
*attr
)
1103 struct kvm_s390_vm_cpu_feat data
;
1105 if (copy_from_user(&data
, (void __user
*)attr
->addr
, sizeof(data
)))
1107 if (!bitmap_subset((unsigned long *) data
.feat
,
1108 kvm_s390_available_cpu_feat
,
1109 KVM_S390_VM_CPU_FEAT_NR_BITS
))
1112 mutex_lock(&kvm
->lock
);
1113 if (kvm
->created_vcpus
) {
1114 mutex_unlock(&kvm
->lock
);
1117 bitmap_copy(kvm
->arch
.cpu_feat
, (unsigned long *) data
.feat
,
1118 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1119 mutex_unlock(&kvm
->lock
);
1120 VM_EVENT(kvm
, 3, "SET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1127 static int kvm_s390_set_processor_subfunc(struct kvm
*kvm
,
1128 struct kvm_device_attr
*attr
)
1131 * Once supported by kernel + hw, we have to store the subfunctions
1132 * in kvm->arch and remember that user space configured them.
1137 static int kvm_s390_set_cpu_model(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1141 switch (attr
->attr
) {
1142 case KVM_S390_VM_CPU_PROCESSOR
:
1143 ret
= kvm_s390_set_processor(kvm
, attr
);
1145 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1146 ret
= kvm_s390_set_processor_feat(kvm
, attr
);
1148 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1149 ret
= kvm_s390_set_processor_subfunc(kvm
, attr
);
1155 static int kvm_s390_get_processor(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1157 struct kvm_s390_vm_cpu_processor
*proc
;
1160 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
1165 proc
->cpuid
= kvm
->arch
.model
.cpuid
;
1166 proc
->ibc
= kvm
->arch
.model
.ibc
;
1167 memcpy(&proc
->fac_list
, kvm
->arch
.model
.fac_list
,
1168 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1169 VM_EVENT(kvm
, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1170 kvm
->arch
.model
.ibc
,
1171 kvm
->arch
.model
.cpuid
);
1172 VM_EVENT(kvm
, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1173 kvm
->arch
.model
.fac_list
[0],
1174 kvm
->arch
.model
.fac_list
[1],
1175 kvm
->arch
.model
.fac_list
[2]);
1176 if (copy_to_user((void __user
*)attr
->addr
, proc
, sizeof(*proc
)))
1183 static int kvm_s390_get_machine(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1185 struct kvm_s390_vm_cpu_machine
*mach
;
1188 mach
= kzalloc(sizeof(*mach
), GFP_KERNEL
);
1193 get_cpu_id((struct cpuid
*) &mach
->cpuid
);
1194 mach
->ibc
= sclp
.ibc
;
1195 memcpy(&mach
->fac_mask
, kvm
->arch
.model
.fac_mask
,
1196 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1197 memcpy((unsigned long *)&mach
->fac_list
, S390_lowcore
.stfle_fac_list
,
1198 sizeof(S390_lowcore
.stfle_fac_list
));
1199 VM_EVENT(kvm
, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1200 kvm
->arch
.model
.ibc
,
1201 kvm
->arch
.model
.cpuid
);
1202 VM_EVENT(kvm
, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1206 VM_EVENT(kvm
, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1210 if (copy_to_user((void __user
*)attr
->addr
, mach
, sizeof(*mach
)))
1217 static int kvm_s390_get_processor_feat(struct kvm
*kvm
,
1218 struct kvm_device_attr
*attr
)
1220 struct kvm_s390_vm_cpu_feat data
;
1222 bitmap_copy((unsigned long *) data
.feat
, kvm
->arch
.cpu_feat
,
1223 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1224 if (copy_to_user((void __user
*)attr
->addr
, &data
, sizeof(data
)))
1226 VM_EVENT(kvm
, 3, "GET: guest feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1233 static int kvm_s390_get_machine_feat(struct kvm
*kvm
,
1234 struct kvm_device_attr
*attr
)
1236 struct kvm_s390_vm_cpu_feat data
;
1238 bitmap_copy((unsigned long *) data
.feat
,
1239 kvm_s390_available_cpu_feat
,
1240 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1241 if (copy_to_user((void __user
*)attr
->addr
, &data
, sizeof(data
)))
1243 VM_EVENT(kvm
, 3, "GET: host feat: 0x%16.16llx.0x%16.16llx.0x%16.16llx",
1250 static int kvm_s390_get_processor_subfunc(struct kvm
*kvm
,
1251 struct kvm_device_attr
*attr
)
1254 * Once we can actually configure subfunctions (kernel + hw support),
1255 * we have to check if they were already set by user space, if so copy
1256 * them from kvm->arch.
1261 static int kvm_s390_get_machine_subfunc(struct kvm
*kvm
,
1262 struct kvm_device_attr
*attr
)
1264 if (copy_to_user((void __user
*)attr
->addr
, &kvm_s390_available_subfunc
,
1265 sizeof(struct kvm_s390_vm_cpu_subfunc
)))
1269 static int kvm_s390_get_cpu_model(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1273 switch (attr
->attr
) {
1274 case KVM_S390_VM_CPU_PROCESSOR
:
1275 ret
= kvm_s390_get_processor(kvm
, attr
);
1277 case KVM_S390_VM_CPU_MACHINE
:
1278 ret
= kvm_s390_get_machine(kvm
, attr
);
1280 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1281 ret
= kvm_s390_get_processor_feat(kvm
, attr
);
1283 case KVM_S390_VM_CPU_MACHINE_FEAT
:
1284 ret
= kvm_s390_get_machine_feat(kvm
, attr
);
1286 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1287 ret
= kvm_s390_get_processor_subfunc(kvm
, attr
);
1289 case KVM_S390_VM_CPU_MACHINE_SUBFUNC
:
1290 ret
= kvm_s390_get_machine_subfunc(kvm
, attr
);
1296 static int kvm_s390_vm_set_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1300 switch (attr
->group
) {
1301 case KVM_S390_VM_MEM_CTRL
:
1302 ret
= kvm_s390_set_mem_control(kvm
, attr
);
1304 case KVM_S390_VM_TOD
:
1305 ret
= kvm_s390_set_tod(kvm
, attr
);
1307 case KVM_S390_VM_CPU_MODEL
:
1308 ret
= kvm_s390_set_cpu_model(kvm
, attr
);
1310 case KVM_S390_VM_CRYPTO
:
1311 ret
= kvm_s390_vm_set_crypto(kvm
, attr
);
1313 case KVM_S390_VM_MIGRATION
:
1314 ret
= kvm_s390_vm_set_migration(kvm
, attr
);
1324 static int kvm_s390_vm_get_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1328 switch (attr
->group
) {
1329 case KVM_S390_VM_MEM_CTRL
:
1330 ret
= kvm_s390_get_mem_control(kvm
, attr
);
1332 case KVM_S390_VM_TOD
:
1333 ret
= kvm_s390_get_tod(kvm
, attr
);
1335 case KVM_S390_VM_CPU_MODEL
:
1336 ret
= kvm_s390_get_cpu_model(kvm
, attr
);
1338 case KVM_S390_VM_MIGRATION
:
1339 ret
= kvm_s390_vm_get_migration(kvm
, attr
);
1349 static int kvm_s390_vm_has_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1353 switch (attr
->group
) {
1354 case KVM_S390_VM_MEM_CTRL
:
1355 switch (attr
->attr
) {
1356 case KVM_S390_VM_MEM_ENABLE_CMMA
:
1357 case KVM_S390_VM_MEM_CLR_CMMA
:
1358 ret
= sclp
.has_cmma
? 0 : -ENXIO
;
1360 case KVM_S390_VM_MEM_LIMIT_SIZE
:
1368 case KVM_S390_VM_TOD
:
1369 switch (attr
->attr
) {
1370 case KVM_S390_VM_TOD_LOW
:
1371 case KVM_S390_VM_TOD_HIGH
:
1379 case KVM_S390_VM_CPU_MODEL
:
1380 switch (attr
->attr
) {
1381 case KVM_S390_VM_CPU_PROCESSOR
:
1382 case KVM_S390_VM_CPU_MACHINE
:
1383 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1384 case KVM_S390_VM_CPU_MACHINE_FEAT
:
1385 case KVM_S390_VM_CPU_MACHINE_SUBFUNC
:
1388 /* configuring subfunctions is not supported yet */
1389 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1395 case KVM_S390_VM_CRYPTO
:
1396 switch (attr
->attr
) {
1397 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW
:
1398 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
:
1399 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW
:
1400 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
:
1408 case KVM_S390_VM_MIGRATION
:
1419 static long kvm_s390_get_skeys(struct kvm
*kvm
, struct kvm_s390_skeys
*args
)
1423 int srcu_idx
, i
, r
= 0;
1425 if (args
->flags
!= 0)
1428 /* Is this guest using storage keys? */
1429 if (!mm_use_skey(current
->mm
))
1430 return KVM_S390_GET_SKEYS_NONE
;
1432 /* Enforce sane limit on memory allocation */
1433 if (args
->count
< 1 || args
->count
> KVM_S390_SKEYS_MAX
)
1436 keys
= kvmalloc_array(args
->count
, sizeof(uint8_t), GFP_KERNEL
);
1440 down_read(¤t
->mm
->mmap_sem
);
1441 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1442 for (i
= 0; i
< args
->count
; i
++) {
1443 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1444 if (kvm_is_error_hva(hva
)) {
1449 r
= get_guest_storage_key(current
->mm
, hva
, &keys
[i
]);
1453 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1454 up_read(¤t
->mm
->mmap_sem
);
1457 r
= copy_to_user((uint8_t __user
*)args
->skeydata_addr
, keys
,
1458 sizeof(uint8_t) * args
->count
);
1467 static long kvm_s390_set_skeys(struct kvm
*kvm
, struct kvm_s390_skeys
*args
)
1471 int srcu_idx
, i
, r
= 0;
1473 if (args
->flags
!= 0)
1476 /* Enforce sane limit on memory allocation */
1477 if (args
->count
< 1 || args
->count
> KVM_S390_SKEYS_MAX
)
1480 keys
= kvmalloc_array(args
->count
, sizeof(uint8_t), GFP_KERNEL
);
1484 r
= copy_from_user(keys
, (uint8_t __user
*)args
->skeydata_addr
,
1485 sizeof(uint8_t) * args
->count
);
1491 /* Enable storage key handling for the guest */
1492 r
= s390_enable_skey();
1496 down_read(¤t
->mm
->mmap_sem
);
1497 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1498 for (i
= 0; i
< args
->count
; i
++) {
1499 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1500 if (kvm_is_error_hva(hva
)) {
1505 /* Lowest order bit is reserved */
1506 if (keys
[i
] & 0x01) {
1511 r
= set_guest_storage_key(current
->mm
, hva
, keys
[i
], 0);
1515 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1516 up_read(¤t
->mm
->mmap_sem
);
1523 * Base address and length must be sent at the start of each block, therefore
1524 * it's cheaper to send some clean data, as long as it's less than the size of
1527 #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
1528 /* for consistency */
1529 #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
1532 * This function searches for the next page with dirty CMMA attributes, and
1533 * saves the attributes in the buffer up to either the end of the buffer or
1534 * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
1535 * no trailing clean bytes are saved.
1536 * In case no dirty bits were found, or if CMMA was not enabled or used, the
1537 * output buffer will indicate 0 as length.
1539 static int kvm_s390_get_cmma_bits(struct kvm
*kvm
,
1540 struct kvm_s390_cmma_log
*args
)
1542 struct kvm_s390_migration_state
*s
= kvm
->arch
.migration_state
;
1543 unsigned long bufsize
, hva
, pgstev
, i
, next
, cur
;
1544 int srcu_idx
, peek
, r
= 0, rr
;
1547 cur
= args
->start_gfn
;
1548 i
= next
= pgstev
= 0;
1550 if (unlikely(!kvm
->arch
.use_cmma
))
1552 /* Invalid/unsupported flags were specified */
1553 if (args
->flags
& ~KVM_S390_CMMA_PEEK
)
1555 /* Migration mode query, and we are not doing a migration */
1556 peek
= !!(args
->flags
& KVM_S390_CMMA_PEEK
);
1559 /* CMMA is disabled or was not used, or the buffer has length zero */
1560 bufsize
= min(args
->count
, KVM_S390_CMMA_SIZE_MAX
);
1561 if (!bufsize
|| !kvm
->mm
->context
.use_cmma
) {
1562 memset(args
, 0, sizeof(*args
));
1567 /* We are not peeking, and there are no dirty pages */
1568 if (!atomic64_read(&s
->dirty_pages
)) {
1569 memset(args
, 0, sizeof(*args
));
1572 cur
= find_next_bit(s
->pgste_bitmap
, s
->bitmap_size
,
1574 if (cur
>= s
->bitmap_size
) /* nothing found, loop back */
1575 cur
= find_next_bit(s
->pgste_bitmap
, s
->bitmap_size
, 0);
1576 if (cur
>= s
->bitmap_size
) { /* again! (very unlikely) */
1577 memset(args
, 0, sizeof(*args
));
1580 next
= find_next_bit(s
->pgste_bitmap
, s
->bitmap_size
, cur
+ 1);
1583 res
= vmalloc(bufsize
);
1587 args
->start_gfn
= cur
;
1589 down_read(&kvm
->mm
->mmap_sem
);
1590 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1591 while (i
< bufsize
) {
1592 hva
= gfn_to_hva(kvm
, cur
);
1593 if (kvm_is_error_hva(hva
)) {
1597 /* decrement only if we actually flipped the bit to 0 */
1598 if (!peek
&& test_and_clear_bit(cur
, s
->pgste_bitmap
))
1599 atomic64_dec(&s
->dirty_pages
);
1600 r
= get_pgste(kvm
->mm
, hva
, &pgstev
);
1603 /* save the value */
1604 res
[i
++] = (pgstev
>> 24) & 0x43;
1606 * if the next bit is too far away, stop.
1607 * if we reached the previous "next", find the next one
1610 if (next
> cur
+ KVM_S390_MAX_BIT_DISTANCE
)
1613 next
= find_next_bit(s
->pgste_bitmap
,
1614 s
->bitmap_size
, cur
+ 1);
1615 /* reached the end of the bitmap or of the buffer, stop */
1616 if ((next
>= s
->bitmap_size
) ||
1617 (next
>= args
->start_gfn
+ bufsize
))
1622 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1623 up_read(&kvm
->mm
->mmap_sem
);
1625 args
->remaining
= s
? atomic64_read(&s
->dirty_pages
) : 0;
1627 rr
= copy_to_user((void __user
*)args
->values
, res
, args
->count
);
1636 * This function sets the CMMA attributes for the given pages. If the input
1637 * buffer has zero length, no action is taken, otherwise the attributes are
1638 * set and the mm->context.use_cmma flag is set.
1640 static int kvm_s390_set_cmma_bits(struct kvm
*kvm
,
1641 const struct kvm_s390_cmma_log
*args
)
1643 unsigned long hva
, mask
, pgstev
, i
;
1645 int srcu_idx
, r
= 0;
1649 if (!kvm
->arch
.use_cmma
)
1651 /* invalid/unsupported flags */
1652 if (args
->flags
!= 0)
1654 /* Enforce sane limit on memory allocation */
1655 if (args
->count
> KVM_S390_CMMA_SIZE_MAX
)
1658 if (args
->count
== 0)
1661 bits
= vmalloc(sizeof(*bits
) * args
->count
);
1665 r
= copy_from_user(bits
, (void __user
*)args
->values
, args
->count
);
1671 down_read(&kvm
->mm
->mmap_sem
);
1672 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1673 for (i
= 0; i
< args
->count
; i
++) {
1674 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1675 if (kvm_is_error_hva(hva
)) {
1681 pgstev
= pgstev
<< 24;
1682 mask
&= _PGSTE_GPS_USAGE_MASK
| _PGSTE_GPS_NODAT
;
1683 set_pgste_bits(kvm
->mm
, hva
, mask
, pgstev
);
1685 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1686 up_read(&kvm
->mm
->mmap_sem
);
1688 if (!kvm
->mm
->context
.use_cmma
) {
1689 down_write(&kvm
->mm
->mmap_sem
);
1690 kvm
->mm
->context
.use_cmma
= 1;
1691 up_write(&kvm
->mm
->mmap_sem
);
1698 long kvm_arch_vm_ioctl(struct file
*filp
,
1699 unsigned int ioctl
, unsigned long arg
)
1701 struct kvm
*kvm
= filp
->private_data
;
1702 void __user
*argp
= (void __user
*)arg
;
1703 struct kvm_device_attr attr
;
1707 case KVM_S390_INTERRUPT
: {
1708 struct kvm_s390_interrupt s390int
;
1711 if (copy_from_user(&s390int
, argp
, sizeof(s390int
)))
1713 r
= kvm_s390_inject_vm(kvm
, &s390int
);
1716 case KVM_ENABLE_CAP
: {
1717 struct kvm_enable_cap cap
;
1719 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
1721 r
= kvm_vm_ioctl_enable_cap(kvm
, &cap
);
1724 case KVM_CREATE_IRQCHIP
: {
1725 struct kvm_irq_routing_entry routing
;
1728 if (kvm
->arch
.use_irqchip
) {
1729 /* Set up dummy routing. */
1730 memset(&routing
, 0, sizeof(routing
));
1731 r
= kvm_set_irq_routing(kvm
, &routing
, 0, 0);
1735 case KVM_SET_DEVICE_ATTR
: {
1737 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
1739 r
= kvm_s390_vm_set_attr(kvm
, &attr
);
1742 case KVM_GET_DEVICE_ATTR
: {
1744 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
1746 r
= kvm_s390_vm_get_attr(kvm
, &attr
);
1749 case KVM_HAS_DEVICE_ATTR
: {
1751 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
1753 r
= kvm_s390_vm_has_attr(kvm
, &attr
);
1756 case KVM_S390_GET_SKEYS
: {
1757 struct kvm_s390_skeys args
;
1760 if (copy_from_user(&args
, argp
,
1761 sizeof(struct kvm_s390_skeys
)))
1763 r
= kvm_s390_get_skeys(kvm
, &args
);
1766 case KVM_S390_SET_SKEYS
: {
1767 struct kvm_s390_skeys args
;
1770 if (copy_from_user(&args
, argp
,
1771 sizeof(struct kvm_s390_skeys
)))
1773 r
= kvm_s390_set_skeys(kvm
, &args
);
1776 case KVM_S390_GET_CMMA_BITS
: {
1777 struct kvm_s390_cmma_log args
;
1780 if (copy_from_user(&args
, argp
, sizeof(args
)))
1782 mutex_lock(&kvm
->slots_lock
);
1783 r
= kvm_s390_get_cmma_bits(kvm
, &args
);
1784 mutex_unlock(&kvm
->slots_lock
);
1786 r
= copy_to_user(argp
, &args
, sizeof(args
));
1792 case KVM_S390_SET_CMMA_BITS
: {
1793 struct kvm_s390_cmma_log args
;
1796 if (copy_from_user(&args
, argp
, sizeof(args
)))
1798 mutex_lock(&kvm
->slots_lock
);
1799 r
= kvm_s390_set_cmma_bits(kvm
, &args
);
1800 mutex_unlock(&kvm
->slots_lock
);
1810 static int kvm_s390_query_ap_config(u8
*config
)
1812 u32 fcn_code
= 0x04000000UL
;
1815 memset(config
, 0, 128);
1819 ".long 0xb2af0000\n" /* PQAP(QCI) */
1825 : "r" (fcn_code
), "r" (config
)
1826 : "cc", "0", "2", "memory"
1832 static int kvm_s390_apxa_installed(void)
1837 if (test_facility(12)) {
1838 cc
= kvm_s390_query_ap_config(config
);
1841 pr_err("PQAP(QCI) failed with cc=%d", cc
);
1843 return config
[0] & 0x40;
1849 static void kvm_s390_set_crycb_format(struct kvm
*kvm
)
1851 kvm
->arch
.crypto
.crycbd
= (__u32
)(unsigned long) kvm
->arch
.crypto
.crycb
;
1853 if (kvm_s390_apxa_installed())
1854 kvm
->arch
.crypto
.crycbd
|= CRYCB_FORMAT2
;
1856 kvm
->arch
.crypto
.crycbd
|= CRYCB_FORMAT1
;
1859 static u64
kvm_s390_get_initial_cpuid(void)
1864 cpuid
.version
= 0xff;
1865 return *((u64
*) &cpuid
);
1868 static void kvm_s390_crypto_init(struct kvm
*kvm
)
1870 if (!test_kvm_facility(kvm
, 76))
1873 kvm
->arch
.crypto
.crycb
= &kvm
->arch
.sie_page2
->crycb
;
1874 kvm_s390_set_crycb_format(kvm
);
1876 /* Enable AES/DEA protected key functions by default */
1877 kvm
->arch
.crypto
.aes_kw
= 1;
1878 kvm
->arch
.crypto
.dea_kw
= 1;
1879 get_random_bytes(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
,
1880 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
1881 get_random_bytes(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
,
1882 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
1885 static void sca_dispose(struct kvm
*kvm
)
1887 if (kvm
->arch
.use_esca
)
1888 free_pages_exact(kvm
->arch
.sca
, sizeof(struct esca_block
));
1890 free_page((unsigned long)(kvm
->arch
.sca
));
1891 kvm
->arch
.sca
= NULL
;
1894 int kvm_arch_init_vm(struct kvm
*kvm
, unsigned long type
)
1896 gfp_t alloc_flags
= GFP_KERNEL
;
1898 char debug_name
[16];
1899 static unsigned long sca_offset
;
1902 #ifdef CONFIG_KVM_S390_UCONTROL
1903 if (type
& ~KVM_VM_S390_UCONTROL
)
1905 if ((type
& KVM_VM_S390_UCONTROL
) && (!capable(CAP_SYS_ADMIN
)))
1912 rc
= s390_enable_sie();
1918 kvm
->arch
.use_esca
= 0; /* start with basic SCA */
1919 if (!sclp
.has_64bscao
)
1920 alloc_flags
|= GFP_DMA
;
1921 rwlock_init(&kvm
->arch
.sca_lock
);
1922 kvm
->arch
.sca
= (struct bsca_block
*) get_zeroed_page(alloc_flags
);
1925 spin_lock(&kvm_lock
);
1927 if (sca_offset
+ sizeof(struct bsca_block
) > PAGE_SIZE
)
1929 kvm
->arch
.sca
= (struct bsca_block
*)
1930 ((char *) kvm
->arch
.sca
+ sca_offset
);
1931 spin_unlock(&kvm_lock
);
1933 sprintf(debug_name
, "kvm-%u", current
->pid
);
1935 kvm
->arch
.dbf
= debug_register(debug_name
, 32, 1, 7 * sizeof(long));
1939 BUILD_BUG_ON(sizeof(struct sie_page2
) != 4096);
1940 kvm
->arch
.sie_page2
=
1941 (struct sie_page2
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
1942 if (!kvm
->arch
.sie_page2
)
1945 /* Populate the facility mask initially. */
1946 memcpy(kvm
->arch
.model
.fac_mask
, S390_lowcore
.stfle_fac_list
,
1947 sizeof(S390_lowcore
.stfle_fac_list
));
1948 for (i
= 0; i
< S390_ARCH_FAC_LIST_SIZE_U64
; i
++) {
1949 if (i
< kvm_s390_fac_list_mask_size())
1950 kvm
->arch
.model
.fac_mask
[i
] &= kvm_s390_fac_list_mask
[i
];
1952 kvm
->arch
.model
.fac_mask
[i
] = 0UL;
1955 /* Populate the facility list initially. */
1956 kvm
->arch
.model
.fac_list
= kvm
->arch
.sie_page2
->fac_list
;
1957 memcpy(kvm
->arch
.model
.fac_list
, kvm
->arch
.model
.fac_mask
,
1958 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1960 /* we are always in czam mode - even on pre z14 machines */
1961 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 138);
1962 set_kvm_facility(kvm
->arch
.model
.fac_list
, 138);
1963 /* we emulate STHYI in kvm */
1964 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 74);
1965 set_kvm_facility(kvm
->arch
.model
.fac_list
, 74);
1966 if (MACHINE_HAS_TLB_GUEST
) {
1967 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 147);
1968 set_kvm_facility(kvm
->arch
.model
.fac_list
, 147);
1971 kvm
->arch
.model
.cpuid
= kvm_s390_get_initial_cpuid();
1972 kvm
->arch
.model
.ibc
= sclp
.ibc
& 0x0fff;
1974 kvm_s390_crypto_init(kvm
);
1976 mutex_init(&kvm
->arch
.float_int
.ais_lock
);
1977 kvm
->arch
.float_int
.simm
= 0;
1978 kvm
->arch
.float_int
.nimm
= 0;
1979 spin_lock_init(&kvm
->arch
.float_int
.lock
);
1980 for (i
= 0; i
< FIRQ_LIST_COUNT
; i
++)
1981 INIT_LIST_HEAD(&kvm
->arch
.float_int
.lists
[i
]);
1982 init_waitqueue_head(&kvm
->arch
.ipte_wq
);
1983 mutex_init(&kvm
->arch
.ipte_mutex
);
1985 debug_register_view(kvm
->arch
.dbf
, &debug_sprintf_view
);
1986 VM_EVENT(kvm
, 3, "vm created with type %lu", type
);
1988 if (type
& KVM_VM_S390_UCONTROL
) {
1989 kvm
->arch
.gmap
= NULL
;
1990 kvm
->arch
.mem_limit
= KVM_S390_NO_MEM_LIMIT
;
1992 if (sclp
.hamax
== U64_MAX
)
1993 kvm
->arch
.mem_limit
= TASK_SIZE_MAX
;
1995 kvm
->arch
.mem_limit
= min_t(unsigned long, TASK_SIZE_MAX
,
1997 kvm
->arch
.gmap
= gmap_create(current
->mm
, kvm
->arch
.mem_limit
- 1);
1998 if (!kvm
->arch
.gmap
)
2000 kvm
->arch
.gmap
->private = kvm
;
2001 kvm
->arch
.gmap
->pfault_enabled
= 0;
2004 kvm
->arch
.css_support
= 0;
2005 kvm
->arch
.use_irqchip
= 0;
2006 kvm
->arch
.epoch
= 0;
2008 spin_lock_init(&kvm
->arch
.start_stop_lock
);
2009 kvm_s390_vsie_init(kvm
);
2010 kvm_s390_gisa_init(kvm
);
2011 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm
, current
->pid
);
2015 free_page((unsigned long)kvm
->arch
.sie_page2
);
2016 debug_unregister(kvm
->arch
.dbf
);
2018 KVM_EVENT(3, "creation of vm failed: %d", rc
);
2022 bool kvm_arch_has_vcpu_debugfs(void)
2027 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu
*vcpu
)
2032 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
2034 VCPU_EVENT(vcpu
, 3, "%s", "free cpu");
2035 trace_kvm_s390_destroy_vcpu(vcpu
->vcpu_id
);
2036 kvm_s390_clear_local_irqs(vcpu
);
2037 kvm_clear_async_pf_completion_queue(vcpu
);
2038 if (!kvm_is_ucontrol(vcpu
->kvm
))
2041 if (kvm_is_ucontrol(vcpu
->kvm
))
2042 gmap_remove(vcpu
->arch
.gmap
);
2044 if (vcpu
->kvm
->arch
.use_cmma
)
2045 kvm_s390_vcpu_unsetup_cmma(vcpu
);
2046 free_page((unsigned long)(vcpu
->arch
.sie_block
));
2048 kvm_vcpu_uninit(vcpu
);
2049 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
2052 static void kvm_free_vcpus(struct kvm
*kvm
)
2055 struct kvm_vcpu
*vcpu
;
2057 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2058 kvm_arch_vcpu_destroy(vcpu
);
2060 mutex_lock(&kvm
->lock
);
2061 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
2062 kvm
->vcpus
[i
] = NULL
;
2064 atomic_set(&kvm
->online_vcpus
, 0);
2065 mutex_unlock(&kvm
->lock
);
2068 void kvm_arch_destroy_vm(struct kvm
*kvm
)
2070 kvm_free_vcpus(kvm
);
2072 debug_unregister(kvm
->arch
.dbf
);
2073 kvm_s390_gisa_destroy(kvm
);
2074 free_page((unsigned long)kvm
->arch
.sie_page2
);
2075 if (!kvm_is_ucontrol(kvm
))
2076 gmap_remove(kvm
->arch
.gmap
);
2077 kvm_s390_destroy_adapters(kvm
);
2078 kvm_s390_clear_float_irqs(kvm
);
2079 kvm_s390_vsie_destroy(kvm
);
2080 if (kvm
->arch
.migration_state
) {
2081 vfree(kvm
->arch
.migration_state
->pgste_bitmap
);
2082 kfree(kvm
->arch
.migration_state
);
2084 KVM_EVENT(3, "vm 0x%pK destroyed", kvm
);
2087 /* Section: vcpu related */
2088 static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu
*vcpu
)
2090 vcpu
->arch
.gmap
= gmap_create(current
->mm
, -1UL);
2091 if (!vcpu
->arch
.gmap
)
2093 vcpu
->arch
.gmap
->private = vcpu
->kvm
;
2098 static void sca_del_vcpu(struct kvm_vcpu
*vcpu
)
2100 if (!kvm_s390_use_sca_entries())
2102 read_lock(&vcpu
->kvm
->arch
.sca_lock
);
2103 if (vcpu
->kvm
->arch
.use_esca
) {
2104 struct esca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2106 clear_bit_inv(vcpu
->vcpu_id
, (unsigned long *) sca
->mcn
);
2107 sca
->cpu
[vcpu
->vcpu_id
].sda
= 0;
2109 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2111 clear_bit_inv(vcpu
->vcpu_id
, (unsigned long *) &sca
->mcn
);
2112 sca
->cpu
[vcpu
->vcpu_id
].sda
= 0;
2114 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
2117 static void sca_add_vcpu(struct kvm_vcpu
*vcpu
)
2119 if (!kvm_s390_use_sca_entries()) {
2120 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2122 /* we still need the basic sca for the ipte control */
2123 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
2124 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
;
2126 read_lock(&vcpu
->kvm
->arch
.sca_lock
);
2127 if (vcpu
->kvm
->arch
.use_esca
) {
2128 struct esca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2130 sca
->cpu
[vcpu
->vcpu_id
].sda
= (__u64
) vcpu
->arch
.sie_block
;
2131 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
2132 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
& ~0x3fU
;
2133 vcpu
->arch
.sie_block
->ecb2
|= ECB2_ESCA
;
2134 set_bit_inv(vcpu
->vcpu_id
, (unsigned long *) sca
->mcn
);
2136 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2138 sca
->cpu
[vcpu
->vcpu_id
].sda
= (__u64
) vcpu
->arch
.sie_block
;
2139 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
2140 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
;
2141 set_bit_inv(vcpu
->vcpu_id
, (unsigned long *) &sca
->mcn
);
2143 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
2146 /* Basic SCA to Extended SCA data copy routines */
2147 static inline void sca_copy_entry(struct esca_entry
*d
, struct bsca_entry
*s
)
2150 d
->sigp_ctrl
.c
= s
->sigp_ctrl
.c
;
2151 d
->sigp_ctrl
.scn
= s
->sigp_ctrl
.scn
;
2154 static void sca_copy_b_to_e(struct esca_block
*d
, struct bsca_block
*s
)
2158 d
->ipte_control
= s
->ipte_control
;
2160 for (i
= 0; i
< KVM_S390_BSCA_CPU_SLOTS
; i
++)
2161 sca_copy_entry(&d
->cpu
[i
], &s
->cpu
[i
]);
2164 static int sca_switch_to_extended(struct kvm
*kvm
)
2166 struct bsca_block
*old_sca
= kvm
->arch
.sca
;
2167 struct esca_block
*new_sca
;
2168 struct kvm_vcpu
*vcpu
;
2169 unsigned int vcpu_idx
;
2172 new_sca
= alloc_pages_exact(sizeof(*new_sca
), GFP_KERNEL
|__GFP_ZERO
);
2176 scaoh
= (u32
)((u64
)(new_sca
) >> 32);
2177 scaol
= (u32
)(u64
)(new_sca
) & ~0x3fU
;
2179 kvm_s390_vcpu_block_all(kvm
);
2180 write_lock(&kvm
->arch
.sca_lock
);
2182 sca_copy_b_to_e(new_sca
, old_sca
);
2184 kvm_for_each_vcpu(vcpu_idx
, vcpu
, kvm
) {
2185 vcpu
->arch
.sie_block
->scaoh
= scaoh
;
2186 vcpu
->arch
.sie_block
->scaol
= scaol
;
2187 vcpu
->arch
.sie_block
->ecb2
|= ECB2_ESCA
;
2189 kvm
->arch
.sca
= new_sca
;
2190 kvm
->arch
.use_esca
= 1;
2192 write_unlock(&kvm
->arch
.sca_lock
);
2193 kvm_s390_vcpu_unblock_all(kvm
);
2195 free_page((unsigned long)old_sca
);
2197 VM_EVENT(kvm
, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
2198 old_sca
, kvm
->arch
.sca
);
2202 static int sca_can_add_vcpu(struct kvm
*kvm
, unsigned int id
)
2206 if (!kvm_s390_use_sca_entries()) {
2207 if (id
< KVM_MAX_VCPUS
)
2211 if (id
< KVM_S390_BSCA_CPU_SLOTS
)
2213 if (!sclp
.has_esca
|| !sclp
.has_64bscao
)
2216 mutex_lock(&kvm
->lock
);
2217 rc
= kvm
->arch
.use_esca
? 0 : sca_switch_to_extended(kvm
);
2218 mutex_unlock(&kvm
->lock
);
2220 return rc
== 0 && id
< KVM_S390_ESCA_CPU_SLOTS
;
2223 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
2225 vcpu
->arch
.pfault_token
= KVM_S390_PFAULT_TOKEN_INVALID
;
2226 kvm_clear_async_pf_completion_queue(vcpu
);
2227 vcpu
->run
->kvm_valid_regs
= KVM_SYNC_PREFIX
|
2233 kvm_s390_set_prefix(vcpu
, 0);
2234 if (test_kvm_facility(vcpu
->kvm
, 64))
2235 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_RICCB
;
2236 if (test_kvm_facility(vcpu
->kvm
, 82))
2237 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_BPBC
;
2238 if (test_kvm_facility(vcpu
->kvm
, 133))
2239 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_GSCB
;
2240 /* fprs can be synchronized via vrs, even if the guest has no vx. With
2241 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
2244 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_VRS
;
2246 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_FPRS
;
2248 if (kvm_is_ucontrol(vcpu
->kvm
))
2249 return __kvm_ucontrol_vcpu_init(vcpu
);
2254 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2255 static void __start_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2257 WARN_ON_ONCE(vcpu
->arch
.cputm_start
!= 0);
2258 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
2259 vcpu
->arch
.cputm_start
= get_tod_clock_fast();
2260 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
2263 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2264 static void __stop_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2266 WARN_ON_ONCE(vcpu
->arch
.cputm_start
== 0);
2267 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
2268 vcpu
->arch
.sie_block
->cputm
-= get_tod_clock_fast() - vcpu
->arch
.cputm_start
;
2269 vcpu
->arch
.cputm_start
= 0;
2270 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
2273 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2274 static void __enable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2276 WARN_ON_ONCE(vcpu
->arch
.cputm_enabled
);
2277 vcpu
->arch
.cputm_enabled
= true;
2278 __start_cpu_timer_accounting(vcpu
);
2281 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2282 static void __disable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2284 WARN_ON_ONCE(!vcpu
->arch
.cputm_enabled
);
2285 __stop_cpu_timer_accounting(vcpu
);
2286 vcpu
->arch
.cputm_enabled
= false;
2289 static void enable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2291 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2292 __enable_cpu_timer_accounting(vcpu
);
2296 static void disable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2298 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2299 __disable_cpu_timer_accounting(vcpu
);
2303 /* set the cpu timer - may only be called from the VCPU thread itself */
2304 void kvm_s390_set_cpu_timer(struct kvm_vcpu
*vcpu
, __u64 cputm
)
2306 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2307 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
2308 if (vcpu
->arch
.cputm_enabled
)
2309 vcpu
->arch
.cputm_start
= get_tod_clock_fast();
2310 vcpu
->arch
.sie_block
->cputm
= cputm
;
2311 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
2315 /* update and get the cpu timer - can also be called from other VCPU threads */
2316 __u64
kvm_s390_get_cpu_timer(struct kvm_vcpu
*vcpu
)
2321 if (unlikely(!vcpu
->arch
.cputm_enabled
))
2322 return vcpu
->arch
.sie_block
->cputm
;
2324 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2326 seq
= raw_read_seqcount(&vcpu
->arch
.cputm_seqcount
);
2328 * If the writer would ever execute a read in the critical
2329 * section, e.g. in irq context, we have a deadlock.
2331 WARN_ON_ONCE((seq
& 1) && smp_processor_id() == vcpu
->cpu
);
2332 value
= vcpu
->arch
.sie_block
->cputm
;
2333 /* if cputm_start is 0, accounting is being started/stopped */
2334 if (likely(vcpu
->arch
.cputm_start
))
2335 value
-= get_tod_clock_fast() - vcpu
->arch
.cputm_start
;
2336 } while (read_seqcount_retry(&vcpu
->arch
.cputm_seqcount
, seq
& ~1));
2341 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
2344 gmap_enable(vcpu
->arch
.enabled_gmap
);
2345 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_RUNNING
);
2346 if (vcpu
->arch
.cputm_enabled
&& !is_vcpu_idle(vcpu
))
2347 __start_cpu_timer_accounting(vcpu
);
2351 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
2354 if (vcpu
->arch
.cputm_enabled
&& !is_vcpu_idle(vcpu
))
2355 __stop_cpu_timer_accounting(vcpu
);
2356 kvm_s390_clear_cpuflags(vcpu
, CPUSTAT_RUNNING
);
2357 vcpu
->arch
.enabled_gmap
= gmap_get_enabled();
2358 gmap_disable(vcpu
->arch
.enabled_gmap
);
2362 static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu
*vcpu
)
2364 /* this equals initial cpu reset in pop, but we don't switch to ESA */
2365 vcpu
->arch
.sie_block
->gpsw
.mask
= 0UL;
2366 vcpu
->arch
.sie_block
->gpsw
.addr
= 0UL;
2367 kvm_s390_set_prefix(vcpu
, 0);
2368 kvm_s390_set_cpu_timer(vcpu
, 0);
2369 vcpu
->arch
.sie_block
->ckc
= 0UL;
2370 vcpu
->arch
.sie_block
->todpr
= 0;
2371 memset(vcpu
->arch
.sie_block
->gcr
, 0, 16 * sizeof(__u64
));
2372 vcpu
->arch
.sie_block
->gcr
[0] = 0xE0UL
;
2373 vcpu
->arch
.sie_block
->gcr
[14] = 0xC2000000UL
;
2374 /* make sure the new fpc will be lazily loaded */
2376 current
->thread
.fpu
.fpc
= 0;
2377 vcpu
->arch
.sie_block
->gbea
= 1;
2378 vcpu
->arch
.sie_block
->pp
= 0;
2379 vcpu
->arch
.sie_block
->fpf
&= ~FPF_BPBC
;
2380 vcpu
->arch
.pfault_token
= KVM_S390_PFAULT_TOKEN_INVALID
;
2381 kvm_clear_async_pf_completion_queue(vcpu
);
2382 if (!kvm_s390_user_cpu_state_ctrl(vcpu
->kvm
))
2383 kvm_s390_vcpu_stop(vcpu
);
2384 kvm_s390_clear_local_irqs(vcpu
);
2387 void kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
2389 mutex_lock(&vcpu
->kvm
->lock
);
2391 vcpu
->arch
.sie_block
->epoch
= vcpu
->kvm
->arch
.epoch
;
2393 mutex_unlock(&vcpu
->kvm
->lock
);
2394 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
2395 vcpu
->arch
.gmap
= vcpu
->kvm
->arch
.gmap
;
2398 if (test_kvm_facility(vcpu
->kvm
, 74) || vcpu
->kvm
->arch
.user_instr0
)
2399 vcpu
->arch
.sie_block
->ictl
|= ICTL_OPEREXC
;
2400 /* make vcpu_load load the right gmap on the first trigger */
2401 vcpu
->arch
.enabled_gmap
= vcpu
->arch
.gmap
;
2404 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu
*vcpu
)
2406 if (!test_kvm_facility(vcpu
->kvm
, 76))
2409 vcpu
->arch
.sie_block
->ecb3
&= ~(ECB3_AES
| ECB3_DEA
);
2411 if (vcpu
->kvm
->arch
.crypto
.aes_kw
)
2412 vcpu
->arch
.sie_block
->ecb3
|= ECB3_AES
;
2413 if (vcpu
->kvm
->arch
.crypto
.dea_kw
)
2414 vcpu
->arch
.sie_block
->ecb3
|= ECB3_DEA
;
2416 vcpu
->arch
.sie_block
->crycbd
= vcpu
->kvm
->arch
.crypto
.crycbd
;
2419 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu
*vcpu
)
2421 free_page(vcpu
->arch
.sie_block
->cbrlo
);
2422 vcpu
->arch
.sie_block
->cbrlo
= 0;
2425 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu
*vcpu
)
2427 vcpu
->arch
.sie_block
->cbrlo
= get_zeroed_page(GFP_KERNEL
);
2428 if (!vcpu
->arch
.sie_block
->cbrlo
)
2431 vcpu
->arch
.sie_block
->ecb2
&= ~ECB2_PFMFI
;
2435 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu
*vcpu
)
2437 struct kvm_s390_cpu_model
*model
= &vcpu
->kvm
->arch
.model
;
2439 vcpu
->arch
.sie_block
->ibc
= model
->ibc
;
2440 if (test_kvm_facility(vcpu
->kvm
, 7))
2441 vcpu
->arch
.sie_block
->fac
= (u32
)(u64
) model
->fac_list
;
2444 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
2448 atomic_set(&vcpu
->arch
.sie_block
->cpuflags
, CPUSTAT_ZARCH
|
2452 if (test_kvm_facility(vcpu
->kvm
, 78))
2453 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_GED2
);
2454 else if (test_kvm_facility(vcpu
->kvm
, 8))
2455 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_GED
);
2457 kvm_s390_vcpu_setup_model(vcpu
);
2459 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2460 if (MACHINE_HAS_ESOP
)
2461 vcpu
->arch
.sie_block
->ecb
|= ECB_HOSTPROTINT
;
2462 if (test_kvm_facility(vcpu
->kvm
, 9))
2463 vcpu
->arch
.sie_block
->ecb
|= ECB_SRSI
;
2464 if (test_kvm_facility(vcpu
->kvm
, 73))
2465 vcpu
->arch
.sie_block
->ecb
|= ECB_TE
;
2467 if (test_kvm_facility(vcpu
->kvm
, 8) && sclp
.has_pfmfi
)
2468 vcpu
->arch
.sie_block
->ecb2
|= ECB2_PFMFI
;
2469 if (test_kvm_facility(vcpu
->kvm
, 130))
2470 vcpu
->arch
.sie_block
->ecb2
|= ECB2_IEP
;
2471 vcpu
->arch
.sie_block
->eca
= ECA_MVPGI
| ECA_PROTEXCI
;
2473 vcpu
->arch
.sie_block
->eca
|= ECA_CEI
;
2475 vcpu
->arch
.sie_block
->eca
|= ECA_IB
;
2477 vcpu
->arch
.sie_block
->eca
|= ECA_SII
;
2478 if (sclp
.has_sigpif
)
2479 vcpu
->arch
.sie_block
->eca
|= ECA_SIGPI
;
2480 if (test_kvm_facility(vcpu
->kvm
, 129)) {
2481 vcpu
->arch
.sie_block
->eca
|= ECA_VX
;
2482 vcpu
->arch
.sie_block
->ecd
|= ECD_HOSTREGMGMT
;
2484 if (test_kvm_facility(vcpu
->kvm
, 139))
2485 vcpu
->arch
.sie_block
->ecd
|= ECD_MEF
;
2487 if (vcpu
->arch
.sie_block
->gd
) {
2488 vcpu
->arch
.sie_block
->eca
|= ECA_AIV
;
2489 VCPU_EVENT(vcpu
, 3, "AIV gisa format-%u enabled for cpu %03u",
2490 vcpu
->arch
.sie_block
->gd
& 0x3, vcpu
->vcpu_id
);
2492 vcpu
->arch
.sie_block
->sdnxo
= ((unsigned long) &vcpu
->run
->s
.regs
.sdnx
)
2494 vcpu
->arch
.sie_block
->riccbd
= (unsigned long) &vcpu
->run
->s
.regs
.riccb
;
2497 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_KSS
);
2499 vcpu
->arch
.sie_block
->ictl
|= ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
;
2501 if (vcpu
->kvm
->arch
.use_cmma
) {
2502 rc
= kvm_s390_vcpu_setup_cmma(vcpu
);
2506 hrtimer_init(&vcpu
->arch
.ckc_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2507 vcpu
->arch
.ckc_timer
.function
= kvm_s390_idle_wakeup
;
2509 kvm_s390_vcpu_crypto_setup(vcpu
);
2514 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
2517 struct kvm_vcpu
*vcpu
;
2518 struct sie_page
*sie_page
;
2521 if (!kvm_is_ucontrol(kvm
) && !sca_can_add_vcpu(kvm
, id
))
2526 vcpu
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
2530 BUILD_BUG_ON(sizeof(struct sie_page
) != 4096);
2531 sie_page
= (struct sie_page
*) get_zeroed_page(GFP_KERNEL
);
2535 vcpu
->arch
.sie_block
= &sie_page
->sie_block
;
2536 vcpu
->arch
.sie_block
->itdba
= (unsigned long) &sie_page
->itdb
;
2538 /* the real guest size will always be smaller than msl */
2539 vcpu
->arch
.sie_block
->mso
= 0;
2540 vcpu
->arch
.sie_block
->msl
= sclp
.hamax
;
2542 vcpu
->arch
.sie_block
->icpua
= id
;
2543 spin_lock_init(&vcpu
->arch
.local_int
.lock
);
2544 vcpu
->arch
.sie_block
->gd
= (u32
)(u64
)kvm
->arch
.gisa
;
2545 if (vcpu
->arch
.sie_block
->gd
&& sclp
.has_gisaf
)
2546 vcpu
->arch
.sie_block
->gd
|= GISA_FORMAT1
;
2547 seqcount_init(&vcpu
->arch
.cputm_seqcount
);
2549 rc
= kvm_vcpu_init(vcpu
, kvm
, id
);
2551 goto out_free_sie_block
;
2552 VM_EVENT(kvm
, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id
, vcpu
,
2553 vcpu
->arch
.sie_block
);
2554 trace_kvm_s390_create_vcpu(id
, vcpu
, vcpu
->arch
.sie_block
);
2558 free_page((unsigned long)(vcpu
->arch
.sie_block
));
2560 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
2565 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
2567 return kvm_s390_vcpu_has_irq(vcpu
, 0);
2570 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu
*vcpu
)
2572 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
);
2575 void kvm_s390_vcpu_block(struct kvm_vcpu
*vcpu
)
2577 atomic_or(PROG_BLOCK_SIE
, &vcpu
->arch
.sie_block
->prog20
);
2581 void kvm_s390_vcpu_unblock(struct kvm_vcpu
*vcpu
)
2583 atomic_andnot(PROG_BLOCK_SIE
, &vcpu
->arch
.sie_block
->prog20
);
2586 static void kvm_s390_vcpu_request(struct kvm_vcpu
*vcpu
)
2588 atomic_or(PROG_REQUEST
, &vcpu
->arch
.sie_block
->prog20
);
2592 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu
*vcpu
)
2594 atomic_andnot(PROG_REQUEST
, &vcpu
->arch
.sie_block
->prog20
);
2598 * Kick a guest cpu out of SIE and wait until SIE is not running.
2599 * If the CPU is not running (e.g. waiting as idle) the function will
2600 * return immediately. */
2601 void exit_sie(struct kvm_vcpu
*vcpu
)
2603 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_STOP_INT
);
2604 while (vcpu
->arch
.sie_block
->prog0c
& PROG_IN_SIE
)
2608 /* Kick a guest cpu out of SIE to process a request synchronously */
2609 void kvm_s390_sync_request(int req
, struct kvm_vcpu
*vcpu
)
2611 kvm_make_request(req
, vcpu
);
2612 kvm_s390_vcpu_request(vcpu
);
2615 static void kvm_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
2618 struct kvm
*kvm
= gmap
->private;
2619 struct kvm_vcpu
*vcpu
;
2620 unsigned long prefix
;
2623 if (gmap_is_shadow(gmap
))
2625 if (start
>= 1UL << 31)
2626 /* We are only interested in prefix pages */
2628 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
2629 /* match against both prefix pages */
2630 prefix
= kvm_s390_get_prefix(vcpu
);
2631 if (prefix
<= end
&& start
<= prefix
+ 2*PAGE_SIZE
- 1) {
2632 VCPU_EVENT(vcpu
, 2, "gmap notifier for %lx-%lx",
2634 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD
, vcpu
);
2639 int kvm_arch_vcpu_should_kick(struct kvm_vcpu
*vcpu
)
2641 /* kvm common code refers to this, but never calls it */
2646 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu
*vcpu
,
2647 struct kvm_one_reg
*reg
)
2652 case KVM_REG_S390_TODPR
:
2653 r
= put_user(vcpu
->arch
.sie_block
->todpr
,
2654 (u32 __user
*)reg
->addr
);
2656 case KVM_REG_S390_EPOCHDIFF
:
2657 r
= put_user(vcpu
->arch
.sie_block
->epoch
,
2658 (u64 __user
*)reg
->addr
);
2660 case KVM_REG_S390_CPU_TIMER
:
2661 r
= put_user(kvm_s390_get_cpu_timer(vcpu
),
2662 (u64 __user
*)reg
->addr
);
2664 case KVM_REG_S390_CLOCK_COMP
:
2665 r
= put_user(vcpu
->arch
.sie_block
->ckc
,
2666 (u64 __user
*)reg
->addr
);
2668 case KVM_REG_S390_PFTOKEN
:
2669 r
= put_user(vcpu
->arch
.pfault_token
,
2670 (u64 __user
*)reg
->addr
);
2672 case KVM_REG_S390_PFCOMPARE
:
2673 r
= put_user(vcpu
->arch
.pfault_compare
,
2674 (u64 __user
*)reg
->addr
);
2676 case KVM_REG_S390_PFSELECT
:
2677 r
= put_user(vcpu
->arch
.pfault_select
,
2678 (u64 __user
*)reg
->addr
);
2680 case KVM_REG_S390_PP
:
2681 r
= put_user(vcpu
->arch
.sie_block
->pp
,
2682 (u64 __user
*)reg
->addr
);
2684 case KVM_REG_S390_GBEA
:
2685 r
= put_user(vcpu
->arch
.sie_block
->gbea
,
2686 (u64 __user
*)reg
->addr
);
2695 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu
*vcpu
,
2696 struct kvm_one_reg
*reg
)
2702 case KVM_REG_S390_TODPR
:
2703 r
= get_user(vcpu
->arch
.sie_block
->todpr
,
2704 (u32 __user
*)reg
->addr
);
2706 case KVM_REG_S390_EPOCHDIFF
:
2707 r
= get_user(vcpu
->arch
.sie_block
->epoch
,
2708 (u64 __user
*)reg
->addr
);
2710 case KVM_REG_S390_CPU_TIMER
:
2711 r
= get_user(val
, (u64 __user
*)reg
->addr
);
2713 kvm_s390_set_cpu_timer(vcpu
, val
);
2715 case KVM_REG_S390_CLOCK_COMP
:
2716 r
= get_user(vcpu
->arch
.sie_block
->ckc
,
2717 (u64 __user
*)reg
->addr
);
2719 case KVM_REG_S390_PFTOKEN
:
2720 r
= get_user(vcpu
->arch
.pfault_token
,
2721 (u64 __user
*)reg
->addr
);
2722 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
2723 kvm_clear_async_pf_completion_queue(vcpu
);
2725 case KVM_REG_S390_PFCOMPARE
:
2726 r
= get_user(vcpu
->arch
.pfault_compare
,
2727 (u64 __user
*)reg
->addr
);
2729 case KVM_REG_S390_PFSELECT
:
2730 r
= get_user(vcpu
->arch
.pfault_select
,
2731 (u64 __user
*)reg
->addr
);
2733 case KVM_REG_S390_PP
:
2734 r
= get_user(vcpu
->arch
.sie_block
->pp
,
2735 (u64 __user
*)reg
->addr
);
2737 case KVM_REG_S390_GBEA
:
2738 r
= get_user(vcpu
->arch
.sie_block
->gbea
,
2739 (u64 __user
*)reg
->addr
);
2748 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu
*vcpu
)
2750 kvm_s390_vcpu_initial_reset(vcpu
);
2754 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2757 memcpy(&vcpu
->run
->s
.regs
.gprs
, ®s
->gprs
, sizeof(regs
->gprs
));
2762 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2765 memcpy(®s
->gprs
, &vcpu
->run
->s
.regs
.gprs
, sizeof(regs
->gprs
));
2770 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2771 struct kvm_sregs
*sregs
)
2775 memcpy(&vcpu
->run
->s
.regs
.acrs
, &sregs
->acrs
, sizeof(sregs
->acrs
));
2776 memcpy(&vcpu
->arch
.sie_block
->gcr
, &sregs
->crs
, sizeof(sregs
->crs
));
2782 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2783 struct kvm_sregs
*sregs
)
2787 memcpy(&sregs
->acrs
, &vcpu
->run
->s
.regs
.acrs
, sizeof(sregs
->acrs
));
2788 memcpy(&sregs
->crs
, &vcpu
->arch
.sie_block
->gcr
, sizeof(sregs
->crs
));
2794 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2800 if (test_fp_ctl(fpu
->fpc
)) {
2804 vcpu
->run
->s
.regs
.fpc
= fpu
->fpc
;
2806 convert_fp_to_vx((__vector128
*) vcpu
->run
->s
.regs
.vrs
,
2807 (freg_t
*) fpu
->fprs
);
2809 memcpy(vcpu
->run
->s
.regs
.fprs
, &fpu
->fprs
, sizeof(fpu
->fprs
));
2816 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2820 /* make sure we have the latest values */
2823 convert_vx_to_fp((freg_t
*) fpu
->fprs
,
2824 (__vector128
*) vcpu
->run
->s
.regs
.vrs
);
2826 memcpy(fpu
->fprs
, vcpu
->run
->s
.regs
.fprs
, sizeof(fpu
->fprs
));
2827 fpu
->fpc
= vcpu
->run
->s
.regs
.fpc
;
2833 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu
*vcpu
, psw_t psw
)
2837 if (!is_vcpu_stopped(vcpu
))
2840 vcpu
->run
->psw_mask
= psw
.mask
;
2841 vcpu
->run
->psw_addr
= psw
.addr
;
2846 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2847 struct kvm_translation
*tr
)
2849 return -EINVAL
; /* not implemented yet */
2852 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
2853 KVM_GUESTDBG_USE_HW_BP | \
2854 KVM_GUESTDBG_ENABLE)
2856 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
2857 struct kvm_guest_debug
*dbg
)
2863 vcpu
->guest_debug
= 0;
2864 kvm_s390_clear_bp_data(vcpu
);
2866 if (dbg
->control
& ~VALID_GUESTDBG_FLAGS
) {
2870 if (!sclp
.has_gpere
) {
2875 if (dbg
->control
& KVM_GUESTDBG_ENABLE
) {
2876 vcpu
->guest_debug
= dbg
->control
;
2877 /* enforce guest PER */
2878 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_P
);
2880 if (dbg
->control
& KVM_GUESTDBG_USE_HW_BP
)
2881 rc
= kvm_s390_import_bp_data(vcpu
, dbg
);
2883 kvm_s390_clear_cpuflags(vcpu
, CPUSTAT_P
);
2884 vcpu
->arch
.guestdbg
.last_bp
= 0;
2888 vcpu
->guest_debug
= 0;
2889 kvm_s390_clear_bp_data(vcpu
);
2890 kvm_s390_clear_cpuflags(vcpu
, CPUSTAT_P
);
2898 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
2899 struct kvm_mp_state
*mp_state
)
2905 /* CHECK_STOP and LOAD are not supported yet */
2906 ret
= is_vcpu_stopped(vcpu
) ? KVM_MP_STATE_STOPPED
:
2907 KVM_MP_STATE_OPERATING
;
2913 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
2914 struct kvm_mp_state
*mp_state
)
2920 /* user space knows about this interface - let it control the state */
2921 vcpu
->kvm
->arch
.user_cpu_state_ctrl
= 1;
2923 switch (mp_state
->mp_state
) {
2924 case KVM_MP_STATE_STOPPED
:
2925 kvm_s390_vcpu_stop(vcpu
);
2927 case KVM_MP_STATE_OPERATING
:
2928 kvm_s390_vcpu_start(vcpu
);
2930 case KVM_MP_STATE_LOAD
:
2931 case KVM_MP_STATE_CHECK_STOP
:
2932 /* fall through - CHECK_STOP and LOAD are not supported yet */
2941 static bool ibs_enabled(struct kvm_vcpu
*vcpu
)
2943 return kvm_s390_test_cpuflags(vcpu
, CPUSTAT_IBS
);
2946 static int kvm_s390_handle_requests(struct kvm_vcpu
*vcpu
)
2949 kvm_s390_vcpu_request_handled(vcpu
);
2950 if (!kvm_request_pending(vcpu
))
2953 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2954 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
2955 * This ensures that the ipte instruction for this request has
2956 * already finished. We might race against a second unmapper that
2957 * wants to set the blocking bit. Lets just retry the request loop.
2959 if (kvm_check_request(KVM_REQ_MMU_RELOAD
, vcpu
)) {
2961 rc
= gmap_mprotect_notify(vcpu
->arch
.gmap
,
2962 kvm_s390_get_prefix(vcpu
),
2963 PAGE_SIZE
* 2, PROT_WRITE
);
2965 kvm_make_request(KVM_REQ_MMU_RELOAD
, vcpu
);
2971 if (kvm_check_request(KVM_REQ_TLB_FLUSH
, vcpu
)) {
2972 vcpu
->arch
.sie_block
->ihcpu
= 0xffff;
2976 if (kvm_check_request(KVM_REQ_ENABLE_IBS
, vcpu
)) {
2977 if (!ibs_enabled(vcpu
)) {
2978 trace_kvm_s390_enable_disable_ibs(vcpu
->vcpu_id
, 1);
2979 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_IBS
);
2984 if (kvm_check_request(KVM_REQ_DISABLE_IBS
, vcpu
)) {
2985 if (ibs_enabled(vcpu
)) {
2986 trace_kvm_s390_enable_disable_ibs(vcpu
->vcpu_id
, 0);
2987 kvm_s390_clear_cpuflags(vcpu
, CPUSTAT_IBS
);
2992 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC
, vcpu
)) {
2993 vcpu
->arch
.sie_block
->ictl
|= ICTL_OPEREXC
;
2997 if (kvm_check_request(KVM_REQ_START_MIGRATION
, vcpu
)) {
2999 * Disable CMMA virtualization; we will emulate the ESSA
3000 * instruction manually, in order to provide additional
3001 * functionalities needed for live migration.
3003 vcpu
->arch
.sie_block
->ecb2
&= ~ECB2_CMMA
;
3007 if (kvm_check_request(KVM_REQ_STOP_MIGRATION
, vcpu
)) {
3009 * Re-enable CMMA virtualization if CMMA is available and
3012 if ((vcpu
->kvm
->arch
.use_cmma
) &&
3013 (vcpu
->kvm
->mm
->context
.use_cmma
))
3014 vcpu
->arch
.sie_block
->ecb2
|= ECB2_CMMA
;
3018 /* nothing to do, just clear the request */
3019 kvm_clear_request(KVM_REQ_UNHALT
, vcpu
);
3024 void kvm_s390_set_tod_clock_ext(struct kvm
*kvm
,
3025 const struct kvm_s390_vm_tod_clock
*gtod
)
3027 struct kvm_vcpu
*vcpu
;
3028 struct kvm_s390_tod_clock_ext htod
;
3031 mutex_lock(&kvm
->lock
);
3034 get_tod_clock_ext((char *)&htod
);
3036 kvm
->arch
.epoch
= gtod
->tod
- htod
.tod
;
3037 kvm
->arch
.epdx
= gtod
->epoch_idx
- htod
.epoch_idx
;
3039 if (kvm
->arch
.epoch
> gtod
->tod
)
3040 kvm
->arch
.epdx
-= 1;
3042 kvm_s390_vcpu_block_all(kvm
);
3043 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
3044 vcpu
->arch
.sie_block
->epoch
= kvm
->arch
.epoch
;
3045 vcpu
->arch
.sie_block
->epdx
= kvm
->arch
.epdx
;
3048 kvm_s390_vcpu_unblock_all(kvm
);
3050 mutex_unlock(&kvm
->lock
);
3053 void kvm_s390_set_tod_clock(struct kvm
*kvm
, u64 tod
)
3055 struct kvm_vcpu
*vcpu
;
3058 mutex_lock(&kvm
->lock
);
3060 kvm
->arch
.epoch
= tod
- get_tod_clock();
3061 kvm_s390_vcpu_block_all(kvm
);
3062 kvm_for_each_vcpu(i
, vcpu
, kvm
)
3063 vcpu
->arch
.sie_block
->epoch
= kvm
->arch
.epoch
;
3064 kvm_s390_vcpu_unblock_all(kvm
);
3066 mutex_unlock(&kvm
->lock
);
3070 * kvm_arch_fault_in_page - fault-in guest page if necessary
3071 * @vcpu: The corresponding virtual cpu
3072 * @gpa: Guest physical address
3073 * @writable: Whether the page should be writable or not
3075 * Make sure that a guest page has been faulted-in on the host.
3077 * Return: Zero on success, negative error code otherwise.
3079 long kvm_arch_fault_in_page(struct kvm_vcpu
*vcpu
, gpa_t gpa
, int writable
)
3081 return gmap_fault(vcpu
->arch
.gmap
, gpa
,
3082 writable
? FAULT_FLAG_WRITE
: 0);
3085 static void __kvm_inject_pfault_token(struct kvm_vcpu
*vcpu
, bool start_token
,
3086 unsigned long token
)
3088 struct kvm_s390_interrupt inti
;
3089 struct kvm_s390_irq irq
;
3092 irq
.u
.ext
.ext_params2
= token
;
3093 irq
.type
= KVM_S390_INT_PFAULT_INIT
;
3094 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu
, &irq
));
3096 inti
.type
= KVM_S390_INT_PFAULT_DONE
;
3097 inti
.parm64
= token
;
3098 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu
->kvm
, &inti
));
3102 void kvm_arch_async_page_not_present(struct kvm_vcpu
*vcpu
,
3103 struct kvm_async_pf
*work
)
3105 trace_kvm_s390_pfault_init(vcpu
, work
->arch
.pfault_token
);
3106 __kvm_inject_pfault_token(vcpu
, true, work
->arch
.pfault_token
);
3109 void kvm_arch_async_page_present(struct kvm_vcpu
*vcpu
,
3110 struct kvm_async_pf
*work
)
3112 trace_kvm_s390_pfault_done(vcpu
, work
->arch
.pfault_token
);
3113 __kvm_inject_pfault_token(vcpu
, false, work
->arch
.pfault_token
);
3116 void kvm_arch_async_page_ready(struct kvm_vcpu
*vcpu
,
3117 struct kvm_async_pf
*work
)
3119 /* s390 will always inject the page directly */
3122 bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu
*vcpu
)
3125 * s390 will always inject the page directly,
3126 * but we still want check_async_completion to cleanup
3131 static int kvm_arch_setup_async_pf(struct kvm_vcpu
*vcpu
)
3134 struct kvm_arch_async_pf arch
;
3137 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
3139 if ((vcpu
->arch
.sie_block
->gpsw
.mask
& vcpu
->arch
.pfault_select
) !=
3140 vcpu
->arch
.pfault_compare
)
3142 if (psw_extint_disabled(vcpu
))
3144 if (kvm_s390_vcpu_has_irq(vcpu
, 0))
3146 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x200ul
))
3148 if (!vcpu
->arch
.gmap
->pfault_enabled
)
3151 hva
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(current
->thread
.gmap_addr
));
3152 hva
+= current
->thread
.gmap_addr
& ~PAGE_MASK
;
3153 if (read_guest_real(vcpu
, vcpu
->arch
.pfault_token
, &arch
.pfault_token
, 8))
3156 rc
= kvm_setup_async_pf(vcpu
, current
->thread
.gmap_addr
, hva
, &arch
);
3160 static int vcpu_pre_run(struct kvm_vcpu
*vcpu
)
3165 * On s390 notifications for arriving pages will be delivered directly
3166 * to the guest but the house keeping for completed pfaults is
3167 * handled outside the worker.
3169 kvm_check_async_pf_completion(vcpu
);
3171 vcpu
->arch
.sie_block
->gg14
= vcpu
->run
->s
.regs
.gprs
[14];
3172 vcpu
->arch
.sie_block
->gg15
= vcpu
->run
->s
.regs
.gprs
[15];
3177 if (test_cpu_flag(CIF_MCCK_PENDING
))
3180 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
3181 rc
= kvm_s390_deliver_pending_interrupts(vcpu
);
3186 rc
= kvm_s390_handle_requests(vcpu
);
3190 if (guestdbg_enabled(vcpu
)) {
3191 kvm_s390_backup_guest_per_regs(vcpu
);
3192 kvm_s390_patch_guest_per_regs(vcpu
);
3195 vcpu
->arch
.sie_block
->icptcode
= 0;
3196 cpuflags
= atomic_read(&vcpu
->arch
.sie_block
->cpuflags
);
3197 VCPU_EVENT(vcpu
, 6, "entering sie flags %x", cpuflags
);
3198 trace_kvm_s390_sie_enter(vcpu
, cpuflags
);
3203 static int vcpu_post_run_fault_in_sie(struct kvm_vcpu
*vcpu
)
3205 struct kvm_s390_pgm_info pgm_info
= {
3206 .code
= PGM_ADDRESSING
,
3211 VCPU_EVENT(vcpu
, 3, "%s", "fault in sie instruction");
3212 trace_kvm_s390_sie_fault(vcpu
);
3215 * We want to inject an addressing exception, which is defined as a
3216 * suppressing or terminating exception. However, since we came here
3217 * by a DAT access exception, the PSW still points to the faulting
3218 * instruction since DAT exceptions are nullifying. So we've got
3219 * to look up the current opcode to get the length of the instruction
3220 * to be able to forward the PSW.
3222 rc
= read_guest_instr(vcpu
, vcpu
->arch
.sie_block
->gpsw
.addr
, &opcode
, 1);
3223 ilen
= insn_length(opcode
);
3227 /* Instruction-Fetching Exceptions - we can't detect the ilen.
3228 * Forward by arbitrary ilc, injection will take care of
3229 * nullification if necessary.
3231 pgm_info
= vcpu
->arch
.pgm
;
3234 pgm_info
.flags
= ilen
| KVM_S390_PGM_FLAGS_ILC_VALID
;
3235 kvm_s390_forward_psw(vcpu
, ilen
);
3236 return kvm_s390_inject_prog_irq(vcpu
, &pgm_info
);
3239 static int vcpu_post_run(struct kvm_vcpu
*vcpu
, int exit_reason
)
3241 struct mcck_volatile_info
*mcck_info
;
3242 struct sie_page
*sie_page
;
3244 VCPU_EVENT(vcpu
, 6, "exit sie icptcode %d",
3245 vcpu
->arch
.sie_block
->icptcode
);
3246 trace_kvm_s390_sie_exit(vcpu
, vcpu
->arch
.sie_block
->icptcode
);
3248 if (guestdbg_enabled(vcpu
))
3249 kvm_s390_restore_guest_per_regs(vcpu
);
3251 vcpu
->run
->s
.regs
.gprs
[14] = vcpu
->arch
.sie_block
->gg14
;
3252 vcpu
->run
->s
.regs
.gprs
[15] = vcpu
->arch
.sie_block
->gg15
;
3254 if (exit_reason
== -EINTR
) {
3255 VCPU_EVENT(vcpu
, 3, "%s", "machine check");
3256 sie_page
= container_of(vcpu
->arch
.sie_block
,
3257 struct sie_page
, sie_block
);
3258 mcck_info
= &sie_page
->mcck_info
;
3259 kvm_s390_reinject_machine_check(vcpu
, mcck_info
);
3263 if (vcpu
->arch
.sie_block
->icptcode
> 0) {
3264 int rc
= kvm_handle_sie_intercept(vcpu
);
3266 if (rc
!= -EOPNOTSUPP
)
3268 vcpu
->run
->exit_reason
= KVM_EXIT_S390_SIEIC
;
3269 vcpu
->run
->s390_sieic
.icptcode
= vcpu
->arch
.sie_block
->icptcode
;
3270 vcpu
->run
->s390_sieic
.ipa
= vcpu
->arch
.sie_block
->ipa
;
3271 vcpu
->run
->s390_sieic
.ipb
= vcpu
->arch
.sie_block
->ipb
;
3273 } else if (exit_reason
!= -EFAULT
) {
3274 vcpu
->stat
.exit_null
++;
3276 } else if (kvm_is_ucontrol(vcpu
->kvm
)) {
3277 vcpu
->run
->exit_reason
= KVM_EXIT_S390_UCONTROL
;
3278 vcpu
->run
->s390_ucontrol
.trans_exc_code
=
3279 current
->thread
.gmap_addr
;
3280 vcpu
->run
->s390_ucontrol
.pgm_code
= 0x10;
3282 } else if (current
->thread
.gmap_pfault
) {
3283 trace_kvm_s390_major_guest_pfault(vcpu
);
3284 current
->thread
.gmap_pfault
= 0;
3285 if (kvm_arch_setup_async_pf(vcpu
))
3287 return kvm_arch_fault_in_page(vcpu
, current
->thread
.gmap_addr
, 1);
3289 return vcpu_post_run_fault_in_sie(vcpu
);
3292 static int __vcpu_run(struct kvm_vcpu
*vcpu
)
3294 int rc
, exit_reason
;
3297 * We try to hold kvm->srcu during most of vcpu_run (except when run-
3298 * ning the guest), so that memslots (and other stuff) are protected
3300 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3303 rc
= vcpu_pre_run(vcpu
);
3307 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
3309 * As PF_VCPU will be used in fault handler, between
3310 * guest_enter and guest_exit should be no uaccess.
3312 local_irq_disable();
3313 guest_enter_irqoff();
3314 __disable_cpu_timer_accounting(vcpu
);
3316 exit_reason
= sie64a(vcpu
->arch
.sie_block
,
3317 vcpu
->run
->s
.regs
.gprs
);
3318 local_irq_disable();
3319 __enable_cpu_timer_accounting(vcpu
);
3320 guest_exit_irqoff();
3322 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3324 rc
= vcpu_post_run(vcpu
, exit_reason
);
3325 } while (!signal_pending(current
) && !guestdbg_exit_pending(vcpu
) && !rc
);
3327 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
3331 static void sync_regs(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3333 struct runtime_instr_cb
*riccb
;
3336 riccb
= (struct runtime_instr_cb
*) &kvm_run
->s
.regs
.riccb
;
3337 gscb
= (struct gs_cb
*) &kvm_run
->s
.regs
.gscb
;
3338 vcpu
->arch
.sie_block
->gpsw
.mask
= kvm_run
->psw_mask
;
3339 vcpu
->arch
.sie_block
->gpsw
.addr
= kvm_run
->psw_addr
;
3340 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_PREFIX
)
3341 kvm_s390_set_prefix(vcpu
, kvm_run
->s
.regs
.prefix
);
3342 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_CRS
) {
3343 memcpy(&vcpu
->arch
.sie_block
->gcr
, &kvm_run
->s
.regs
.crs
, 128);
3344 /* some control register changes require a tlb flush */
3345 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
3347 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_ARCH0
) {
3348 kvm_s390_set_cpu_timer(vcpu
, kvm_run
->s
.regs
.cputm
);
3349 vcpu
->arch
.sie_block
->ckc
= kvm_run
->s
.regs
.ckc
;
3350 vcpu
->arch
.sie_block
->todpr
= kvm_run
->s
.regs
.todpr
;
3351 vcpu
->arch
.sie_block
->pp
= kvm_run
->s
.regs
.pp
;
3352 vcpu
->arch
.sie_block
->gbea
= kvm_run
->s
.regs
.gbea
;
3354 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_PFAULT
) {
3355 vcpu
->arch
.pfault_token
= kvm_run
->s
.regs
.pft
;
3356 vcpu
->arch
.pfault_select
= kvm_run
->s
.regs
.pfs
;
3357 vcpu
->arch
.pfault_compare
= kvm_run
->s
.regs
.pfc
;
3358 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
3359 kvm_clear_async_pf_completion_queue(vcpu
);
3362 * If userspace sets the riccb (e.g. after migration) to a valid state,
3363 * we should enable RI here instead of doing the lazy enablement.
3365 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_RICCB
) &&
3366 test_kvm_facility(vcpu
->kvm
, 64) &&
3368 !(vcpu
->arch
.sie_block
->ecb3
& ECB3_RI
)) {
3369 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: RI (sync_regs)");
3370 vcpu
->arch
.sie_block
->ecb3
|= ECB3_RI
;
3373 * If userspace sets the gscb (e.g. after migration) to non-zero,
3374 * we should enable GS here instead of doing the lazy enablement.
3376 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_GSCB
) &&
3377 test_kvm_facility(vcpu
->kvm
, 133) &&
3379 !vcpu
->arch
.gs_enabled
) {
3380 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: GS (sync_regs)");
3381 vcpu
->arch
.sie_block
->ecb
|= ECB_GS
;
3382 vcpu
->arch
.sie_block
->ecd
|= ECD_HOSTREGMGMT
;
3383 vcpu
->arch
.gs_enabled
= 1;
3385 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_BPBC
) &&
3386 test_kvm_facility(vcpu
->kvm
, 82)) {
3387 vcpu
->arch
.sie_block
->fpf
&= ~FPF_BPBC
;
3388 vcpu
->arch
.sie_block
->fpf
|= kvm_run
->s
.regs
.bpbc
? FPF_BPBC
: 0;
3390 save_access_regs(vcpu
->arch
.host_acrs
);
3391 restore_access_regs(vcpu
->run
->s
.regs
.acrs
);
3392 /* save host (userspace) fprs/vrs */
3394 vcpu
->arch
.host_fpregs
.fpc
= current
->thread
.fpu
.fpc
;
3395 vcpu
->arch
.host_fpregs
.regs
= current
->thread
.fpu
.regs
;
3397 current
->thread
.fpu
.regs
= vcpu
->run
->s
.regs
.vrs
;
3399 current
->thread
.fpu
.regs
= vcpu
->run
->s
.regs
.fprs
;
3400 current
->thread
.fpu
.fpc
= vcpu
->run
->s
.regs
.fpc
;
3401 if (test_fp_ctl(current
->thread
.fpu
.fpc
))
3402 /* User space provided an invalid FPC, let's clear it */
3403 current
->thread
.fpu
.fpc
= 0;
3404 if (MACHINE_HAS_GS
) {
3406 __ctl_set_bit(2, 4);
3407 if (current
->thread
.gs_cb
) {
3408 vcpu
->arch
.host_gscb
= current
->thread
.gs_cb
;
3409 save_gs_cb(vcpu
->arch
.host_gscb
);
3411 if (vcpu
->arch
.gs_enabled
) {
3412 current
->thread
.gs_cb
= (struct gs_cb
*)
3413 &vcpu
->run
->s
.regs
.gscb
;
3414 restore_gs_cb(current
->thread
.gs_cb
);
3419 kvm_run
->kvm_dirty_regs
= 0;
3422 static void store_regs(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3424 kvm_run
->psw_mask
= vcpu
->arch
.sie_block
->gpsw
.mask
;
3425 kvm_run
->psw_addr
= vcpu
->arch
.sie_block
->gpsw
.addr
;
3426 kvm_run
->s
.regs
.prefix
= kvm_s390_get_prefix(vcpu
);
3427 memcpy(&kvm_run
->s
.regs
.crs
, &vcpu
->arch
.sie_block
->gcr
, 128);
3428 kvm_run
->s
.regs
.cputm
= kvm_s390_get_cpu_timer(vcpu
);
3429 kvm_run
->s
.regs
.ckc
= vcpu
->arch
.sie_block
->ckc
;
3430 kvm_run
->s
.regs
.todpr
= vcpu
->arch
.sie_block
->todpr
;
3431 kvm_run
->s
.regs
.pp
= vcpu
->arch
.sie_block
->pp
;
3432 kvm_run
->s
.regs
.gbea
= vcpu
->arch
.sie_block
->gbea
;
3433 kvm_run
->s
.regs
.pft
= vcpu
->arch
.pfault_token
;
3434 kvm_run
->s
.regs
.pfs
= vcpu
->arch
.pfault_select
;
3435 kvm_run
->s
.regs
.pfc
= vcpu
->arch
.pfault_compare
;
3436 kvm_run
->s
.regs
.bpbc
= (vcpu
->arch
.sie_block
->fpf
& FPF_BPBC
) == FPF_BPBC
;
3437 save_access_regs(vcpu
->run
->s
.regs
.acrs
);
3438 restore_access_regs(vcpu
->arch
.host_acrs
);
3439 /* Save guest register state */
3441 vcpu
->run
->s
.regs
.fpc
= current
->thread
.fpu
.fpc
;
3442 /* Restore will be done lazily at return */
3443 current
->thread
.fpu
.fpc
= vcpu
->arch
.host_fpregs
.fpc
;
3444 current
->thread
.fpu
.regs
= vcpu
->arch
.host_fpregs
.regs
;
3445 if (MACHINE_HAS_GS
) {
3446 __ctl_set_bit(2, 4);
3447 if (vcpu
->arch
.gs_enabled
)
3448 save_gs_cb(current
->thread
.gs_cb
);
3450 current
->thread
.gs_cb
= vcpu
->arch
.host_gscb
;
3451 restore_gs_cb(vcpu
->arch
.host_gscb
);
3453 if (!vcpu
->arch
.host_gscb
)
3454 __ctl_clear_bit(2, 4);
3455 vcpu
->arch
.host_gscb
= NULL
;
3460 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3464 if (kvm_run
->immediate_exit
)
3469 if (guestdbg_exit_pending(vcpu
)) {
3470 kvm_s390_prepare_debug_exit(vcpu
);
3475 kvm_sigset_activate(vcpu
);
3477 if (!kvm_s390_user_cpu_state_ctrl(vcpu
->kvm
)) {
3478 kvm_s390_vcpu_start(vcpu
);
3479 } else if (is_vcpu_stopped(vcpu
)) {
3480 pr_err_ratelimited("can't run stopped vcpu %d\n",
3486 sync_regs(vcpu
, kvm_run
);
3487 enable_cpu_timer_accounting(vcpu
);
3490 rc
= __vcpu_run(vcpu
);
3492 if (signal_pending(current
) && !rc
) {
3493 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
3497 if (guestdbg_exit_pending(vcpu
) && !rc
) {
3498 kvm_s390_prepare_debug_exit(vcpu
);
3502 if (rc
== -EREMOTE
) {
3503 /* userspace support is needed, kvm_run has been prepared */
3507 disable_cpu_timer_accounting(vcpu
);
3508 store_regs(vcpu
, kvm_run
);
3510 kvm_sigset_deactivate(vcpu
);
3512 vcpu
->stat
.exit_userspace
++;
3519 * store status at address
3520 * we use have two special cases:
3521 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3522 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3524 int kvm_s390_store_status_unloaded(struct kvm_vcpu
*vcpu
, unsigned long gpa
)
3526 unsigned char archmode
= 1;
3527 freg_t fprs
[NUM_FPRS
];
3532 px
= kvm_s390_get_prefix(vcpu
);
3533 if (gpa
== KVM_S390_STORE_STATUS_NOADDR
) {
3534 if (write_guest_abs(vcpu
, 163, &archmode
, 1))
3537 } else if (gpa
== KVM_S390_STORE_STATUS_PREFIXED
) {
3538 if (write_guest_real(vcpu
, 163, &archmode
, 1))
3542 gpa
-= __LC_FPREGS_SAVE_AREA
;
3544 /* manually convert vector registers if necessary */
3545 if (MACHINE_HAS_VX
) {
3546 convert_vx_to_fp(fprs
, (__vector128
*) vcpu
->run
->s
.regs
.vrs
);
3547 rc
= write_guest_abs(vcpu
, gpa
+ __LC_FPREGS_SAVE_AREA
,
3550 rc
= write_guest_abs(vcpu
, gpa
+ __LC_FPREGS_SAVE_AREA
,
3551 vcpu
->run
->s
.regs
.fprs
, 128);
3553 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_GPREGS_SAVE_AREA
,
3554 vcpu
->run
->s
.regs
.gprs
, 128);
3555 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_PSW_SAVE_AREA
,
3556 &vcpu
->arch
.sie_block
->gpsw
, 16);
3557 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_PREFIX_SAVE_AREA
,
3559 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_FP_CREG_SAVE_AREA
,
3560 &vcpu
->run
->s
.regs
.fpc
, 4);
3561 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_TOD_PROGREG_SAVE_AREA
,
3562 &vcpu
->arch
.sie_block
->todpr
, 4);
3563 cputm
= kvm_s390_get_cpu_timer(vcpu
);
3564 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CPU_TIMER_SAVE_AREA
,
3566 clkcomp
= vcpu
->arch
.sie_block
->ckc
>> 8;
3567 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CLOCK_COMP_SAVE_AREA
,
3569 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_AREGS_SAVE_AREA
,
3570 &vcpu
->run
->s
.regs
.acrs
, 64);
3571 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CREGS_SAVE_AREA
,
3572 &vcpu
->arch
.sie_block
->gcr
, 128);
3573 return rc
? -EFAULT
: 0;
3576 int kvm_s390_vcpu_store_status(struct kvm_vcpu
*vcpu
, unsigned long addr
)
3579 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
3580 * switch in the run ioctl. Let's update our copies before we save
3581 * it into the save area
3584 vcpu
->run
->s
.regs
.fpc
= current
->thread
.fpu
.fpc
;
3585 save_access_regs(vcpu
->run
->s
.regs
.acrs
);
3587 return kvm_s390_store_status_unloaded(vcpu
, addr
);
3590 static void __disable_ibs_on_vcpu(struct kvm_vcpu
*vcpu
)
3592 kvm_check_request(KVM_REQ_ENABLE_IBS
, vcpu
);
3593 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS
, vcpu
);
3596 static void __disable_ibs_on_all_vcpus(struct kvm
*kvm
)
3599 struct kvm_vcpu
*vcpu
;
3601 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
3602 __disable_ibs_on_vcpu(vcpu
);
3606 static void __enable_ibs_on_vcpu(struct kvm_vcpu
*vcpu
)
3610 kvm_check_request(KVM_REQ_DISABLE_IBS
, vcpu
);
3611 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS
, vcpu
);
3614 void kvm_s390_vcpu_start(struct kvm_vcpu
*vcpu
)
3616 int i
, online_vcpus
, started_vcpus
= 0;
3618 if (!is_vcpu_stopped(vcpu
))
3621 trace_kvm_s390_vcpu_start_stop(vcpu
->vcpu_id
, 1);
3622 /* Only one cpu at a time may enter/leave the STOPPED state. */
3623 spin_lock(&vcpu
->kvm
->arch
.start_stop_lock
);
3624 online_vcpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
3626 for (i
= 0; i
< online_vcpus
; i
++) {
3627 if (!is_vcpu_stopped(vcpu
->kvm
->vcpus
[i
]))
3631 if (started_vcpus
== 0) {
3632 /* we're the only active VCPU -> speed it up */
3633 __enable_ibs_on_vcpu(vcpu
);
3634 } else if (started_vcpus
== 1) {
3636 * As we are starting a second VCPU, we have to disable
3637 * the IBS facility on all VCPUs to remove potentially
3638 * oustanding ENABLE requests.
3640 __disable_ibs_on_all_vcpus(vcpu
->kvm
);
3643 kvm_s390_clear_cpuflags(vcpu
, CPUSTAT_STOPPED
);
3645 * Another VCPU might have used IBS while we were offline.
3646 * Let's play safe and flush the VCPU at startup.
3648 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
3649 spin_unlock(&vcpu
->kvm
->arch
.start_stop_lock
);
3653 void kvm_s390_vcpu_stop(struct kvm_vcpu
*vcpu
)
3655 int i
, online_vcpus
, started_vcpus
= 0;
3656 struct kvm_vcpu
*started_vcpu
= NULL
;
3658 if (is_vcpu_stopped(vcpu
))
3661 trace_kvm_s390_vcpu_start_stop(vcpu
->vcpu_id
, 0);
3662 /* Only one cpu at a time may enter/leave the STOPPED state. */
3663 spin_lock(&vcpu
->kvm
->arch
.start_stop_lock
);
3664 online_vcpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
3666 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
3667 kvm_s390_clear_stop_irq(vcpu
);
3669 kvm_s390_set_cpuflags(vcpu
, CPUSTAT_STOPPED
);
3670 __disable_ibs_on_vcpu(vcpu
);
3672 for (i
= 0; i
< online_vcpus
; i
++) {
3673 if (!is_vcpu_stopped(vcpu
->kvm
->vcpus
[i
])) {
3675 started_vcpu
= vcpu
->kvm
->vcpus
[i
];
3679 if (started_vcpus
== 1) {
3681 * As we only have one VCPU left, we want to enable the
3682 * IBS facility for that VCPU to speed it up.
3684 __enable_ibs_on_vcpu(started_vcpu
);
3687 spin_unlock(&vcpu
->kvm
->arch
.start_stop_lock
);
3691 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu
*vcpu
,
3692 struct kvm_enable_cap
*cap
)
3700 case KVM_CAP_S390_CSS_SUPPORT
:
3701 if (!vcpu
->kvm
->arch
.css_support
) {
3702 vcpu
->kvm
->arch
.css_support
= 1;
3703 VM_EVENT(vcpu
->kvm
, 3, "%s", "ENABLE: CSS support");
3704 trace_kvm_s390_enable_css(vcpu
->kvm
);
3715 static long kvm_s390_guest_mem_op(struct kvm_vcpu
*vcpu
,
3716 struct kvm_s390_mem_op
*mop
)
3718 void __user
*uaddr
= (void __user
*)mop
->buf
;
3719 void *tmpbuf
= NULL
;
3721 const u64 supported_flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
3722 | KVM_S390_MEMOP_F_CHECK_ONLY
;
3724 if (mop
->flags
& ~supported_flags
)
3727 if (mop
->size
> MEM_OP_MAX_SIZE
)
3730 if (!(mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
)) {
3731 tmpbuf
= vmalloc(mop
->size
);
3736 srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3739 case KVM_S390_MEMOP_LOGICAL_READ
:
3740 if (mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
) {
3741 r
= check_gva_range(vcpu
, mop
->gaddr
, mop
->ar
,
3742 mop
->size
, GACC_FETCH
);
3745 r
= read_guest(vcpu
, mop
->gaddr
, mop
->ar
, tmpbuf
, mop
->size
);
3747 if (copy_to_user(uaddr
, tmpbuf
, mop
->size
))
3751 case KVM_S390_MEMOP_LOGICAL_WRITE
:
3752 if (mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
) {
3753 r
= check_gva_range(vcpu
, mop
->gaddr
, mop
->ar
,
3754 mop
->size
, GACC_STORE
);
3757 if (copy_from_user(tmpbuf
, uaddr
, mop
->size
)) {
3761 r
= write_guest(vcpu
, mop
->gaddr
, mop
->ar
, tmpbuf
, mop
->size
);
3767 srcu_read_unlock(&vcpu
->kvm
->srcu
, srcu_idx
);
3769 if (r
> 0 && (mop
->flags
& KVM_S390_MEMOP_F_INJECT_EXCEPTION
) != 0)
3770 kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
3776 long kvm_arch_vcpu_async_ioctl(struct file
*filp
,
3777 unsigned int ioctl
, unsigned long arg
)
3779 struct kvm_vcpu
*vcpu
= filp
->private_data
;
3780 void __user
*argp
= (void __user
*)arg
;
3783 case KVM_S390_IRQ
: {
3784 struct kvm_s390_irq s390irq
;
3786 if (copy_from_user(&s390irq
, argp
, sizeof(s390irq
)))
3788 return kvm_s390_inject_vcpu(vcpu
, &s390irq
);
3790 case KVM_S390_INTERRUPT
: {
3791 struct kvm_s390_interrupt s390int
;
3792 struct kvm_s390_irq s390irq
;
3794 if (copy_from_user(&s390int
, argp
, sizeof(s390int
)))
3796 if (s390int_to_s390irq(&s390int
, &s390irq
))
3798 return kvm_s390_inject_vcpu(vcpu
, &s390irq
);
3801 return -ENOIOCTLCMD
;
3804 long kvm_arch_vcpu_ioctl(struct file
*filp
,
3805 unsigned int ioctl
, unsigned long arg
)
3807 struct kvm_vcpu
*vcpu
= filp
->private_data
;
3808 void __user
*argp
= (void __user
*)arg
;
3815 case KVM_S390_STORE_STATUS
:
3816 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3817 r
= kvm_s390_vcpu_store_status(vcpu
, arg
);
3818 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
3820 case KVM_S390_SET_INITIAL_PSW
: {
3824 if (copy_from_user(&psw
, argp
, sizeof(psw
)))
3826 r
= kvm_arch_vcpu_ioctl_set_initial_psw(vcpu
, psw
);
3829 case KVM_S390_INITIAL_RESET
:
3830 r
= kvm_arch_vcpu_ioctl_initial_reset(vcpu
);
3832 case KVM_SET_ONE_REG
:
3833 case KVM_GET_ONE_REG
: {
3834 struct kvm_one_reg reg
;
3836 if (copy_from_user(®
, argp
, sizeof(reg
)))
3838 if (ioctl
== KVM_SET_ONE_REG
)
3839 r
= kvm_arch_vcpu_ioctl_set_one_reg(vcpu
, ®
);
3841 r
= kvm_arch_vcpu_ioctl_get_one_reg(vcpu
, ®
);
3844 #ifdef CONFIG_KVM_S390_UCONTROL
3845 case KVM_S390_UCAS_MAP
: {
3846 struct kvm_s390_ucas_mapping ucasmap
;
3848 if (copy_from_user(&ucasmap
, argp
, sizeof(ucasmap
))) {
3853 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
3858 r
= gmap_map_segment(vcpu
->arch
.gmap
, ucasmap
.user_addr
,
3859 ucasmap
.vcpu_addr
, ucasmap
.length
);
3862 case KVM_S390_UCAS_UNMAP
: {
3863 struct kvm_s390_ucas_mapping ucasmap
;
3865 if (copy_from_user(&ucasmap
, argp
, sizeof(ucasmap
))) {
3870 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
3875 r
= gmap_unmap_segment(vcpu
->arch
.gmap
, ucasmap
.vcpu_addr
,
3880 case KVM_S390_VCPU_FAULT
: {
3881 r
= gmap_fault(vcpu
->arch
.gmap
, arg
, 0);
3884 case KVM_ENABLE_CAP
:
3886 struct kvm_enable_cap cap
;
3888 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
3890 r
= kvm_vcpu_ioctl_enable_cap(vcpu
, &cap
);
3893 case KVM_S390_MEM_OP
: {
3894 struct kvm_s390_mem_op mem_op
;
3896 if (copy_from_user(&mem_op
, argp
, sizeof(mem_op
)) == 0)
3897 r
= kvm_s390_guest_mem_op(vcpu
, &mem_op
);
3902 case KVM_S390_SET_IRQ_STATE
: {
3903 struct kvm_s390_irq_state irq_state
;
3906 if (copy_from_user(&irq_state
, argp
, sizeof(irq_state
)))
3908 if (irq_state
.len
> VCPU_IRQS_MAX_BUF
||
3909 irq_state
.len
== 0 ||
3910 irq_state
.len
% sizeof(struct kvm_s390_irq
) > 0) {
3914 /* do not use irq_state.flags, it will break old QEMUs */
3915 r
= kvm_s390_set_irq_state(vcpu
,
3916 (void __user
*) irq_state
.buf
,
3920 case KVM_S390_GET_IRQ_STATE
: {
3921 struct kvm_s390_irq_state irq_state
;
3924 if (copy_from_user(&irq_state
, argp
, sizeof(irq_state
)))
3926 if (irq_state
.len
== 0) {
3930 /* do not use irq_state.flags, it will break old QEMUs */
3931 r
= kvm_s390_get_irq_state(vcpu
,
3932 (__u8 __user
*) irq_state
.buf
,
3944 int kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
3946 #ifdef CONFIG_KVM_S390_UCONTROL
3947 if ((vmf
->pgoff
== KVM_S390_SIE_PAGE_OFFSET
)
3948 && (kvm_is_ucontrol(vcpu
->kvm
))) {
3949 vmf
->page
= virt_to_page(vcpu
->arch
.sie_block
);
3950 get_page(vmf
->page
);
3954 return VM_FAULT_SIGBUS
;
3957 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
3958 unsigned long npages
)
3963 /* Section: memory related */
3964 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
3965 struct kvm_memory_slot
*memslot
,
3966 const struct kvm_userspace_memory_region
*mem
,
3967 enum kvm_mr_change change
)
3969 /* A few sanity checks. We can have memory slots which have to be
3970 located/ended at a segment boundary (1MB). The memory in userland is
3971 ok to be fragmented into various different vmas. It is okay to mmap()
3972 and munmap() stuff in this slot after doing this call at any time */
3974 if (mem
->userspace_addr
& 0xffffful
)
3977 if (mem
->memory_size
& 0xffffful
)
3980 if (mem
->guest_phys_addr
+ mem
->memory_size
> kvm
->arch
.mem_limit
)
3986 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
3987 const struct kvm_userspace_memory_region
*mem
,
3988 const struct kvm_memory_slot
*old
,
3989 const struct kvm_memory_slot
*new,
3990 enum kvm_mr_change change
)
3994 /* If the basics of the memslot do not change, we do not want
3995 * to update the gmap. Every update causes several unnecessary
3996 * segment translation exceptions. This is usually handled just
3997 * fine by the normal fault handler + gmap, but it will also
3998 * cause faults on the prefix page of running guest CPUs.
4000 if (old
->userspace_addr
== mem
->userspace_addr
&&
4001 old
->base_gfn
* PAGE_SIZE
== mem
->guest_phys_addr
&&
4002 old
->npages
* PAGE_SIZE
== mem
->memory_size
)
4005 rc
= gmap_map_segment(kvm
->arch
.gmap
, mem
->userspace_addr
,
4006 mem
->guest_phys_addr
, mem
->memory_size
);
4008 pr_warn("failed to commit memory region\n");
4012 static inline unsigned long nonhyp_mask(int i
)
4014 unsigned int nonhyp_fai
= (sclp
.hmfai
<< i
* 2) >> 30;
4016 return 0x0000ffffffffffffUL
>> (nonhyp_fai
<< 4);
4019 void kvm_arch_vcpu_block_finish(struct kvm_vcpu
*vcpu
)
4021 vcpu
->valid_wakeup
= false;
4024 static int __init
kvm_s390_init(void)
4028 if (!sclp
.has_sief2
) {
4029 pr_info("SIE not available\n");
4033 for (i
= 0; i
< 16; i
++)
4034 kvm_s390_fac_list_mask
[i
] |=
4035 S390_lowcore
.stfle_fac_list
[i
] & nonhyp_mask(i
);
4037 return kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
4040 static void __exit
kvm_s390_exit(void)
4045 module_init(kvm_s390_init
);
4046 module_exit(kvm_s390_exit
);
4049 * Enable autoloading of the kvm module.
4050 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
4051 * since x86 takes a different approach.
4053 #include <linux/miscdevice.h>
4054 MODULE_ALIAS_MISCDEV(KVM_MINOR
);
4055 MODULE_ALIAS("devname:kvm");