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>
47 #define KMSG_COMPONENT "kvm-s390"
49 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
51 #define CREATE_TRACE_POINTS
53 #include "trace-s390.h"
55 #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
57 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
58 (KVM_MAX_VCPUS + LOCAL_IRQS))
60 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
62 struct kvm_stats_debugfs_item debugfs_entries
[] = {
63 { "userspace_handled", VCPU_STAT(exit_userspace
) },
64 { "exit_null", VCPU_STAT(exit_null
) },
65 { "exit_validity", VCPU_STAT(exit_validity
) },
66 { "exit_stop_request", VCPU_STAT(exit_stop_request
) },
67 { "exit_external_request", VCPU_STAT(exit_external_request
) },
68 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt
) },
69 { "exit_instruction", VCPU_STAT(exit_instruction
) },
70 { "exit_pei", VCPU_STAT(exit_pei
) },
71 { "exit_program_interruption", VCPU_STAT(exit_program_interruption
) },
72 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program
) },
73 { "exit_operation_exception", VCPU_STAT(exit_operation_exception
) },
74 { "halt_successful_poll", VCPU_STAT(halt_successful_poll
) },
75 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll
) },
76 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid
) },
77 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
78 { "instruction_lctlg", VCPU_STAT(instruction_lctlg
) },
79 { "instruction_lctl", VCPU_STAT(instruction_lctl
) },
80 { "instruction_stctl", VCPU_STAT(instruction_stctl
) },
81 { "instruction_stctg", VCPU_STAT(instruction_stctg
) },
82 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal
) },
83 { "deliver_external_call", VCPU_STAT(deliver_external_call
) },
84 { "deliver_service_signal", VCPU_STAT(deliver_service_signal
) },
85 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt
) },
86 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal
) },
87 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal
) },
88 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal
) },
89 { "deliver_program_interruption", VCPU_STAT(deliver_program_int
) },
90 { "exit_wait_state", VCPU_STAT(exit_wait_state
) },
91 { "instruction_epsw", VCPU_STAT(instruction_epsw
) },
92 { "instruction_gs", VCPU_STAT(instruction_gs
) },
93 { "instruction_io_other", VCPU_STAT(instruction_io_other
) },
94 { "instruction_lpsw", VCPU_STAT(instruction_lpsw
) },
95 { "instruction_lpswe", VCPU_STAT(instruction_lpswe
) },
96 { "instruction_pfmf", VCPU_STAT(instruction_pfmf
) },
97 { "instruction_ptff", VCPU_STAT(instruction_ptff
) },
98 { "instruction_stidp", VCPU_STAT(instruction_stidp
) },
99 { "instruction_sck", VCPU_STAT(instruction_sck
) },
100 { "instruction_sckpf", VCPU_STAT(instruction_sckpf
) },
101 { "instruction_spx", VCPU_STAT(instruction_spx
) },
102 { "instruction_stpx", VCPU_STAT(instruction_stpx
) },
103 { "instruction_stap", VCPU_STAT(instruction_stap
) },
104 { "instruction_iske", VCPU_STAT(instruction_iske
) },
105 { "instruction_ri", VCPU_STAT(instruction_ri
) },
106 { "instruction_rrbe", VCPU_STAT(instruction_rrbe
) },
107 { "instruction_sske", VCPU_STAT(instruction_sske
) },
108 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock
) },
109 { "instruction_essa", VCPU_STAT(instruction_essa
) },
110 { "instruction_stsi", VCPU_STAT(instruction_stsi
) },
111 { "instruction_stfl", VCPU_STAT(instruction_stfl
) },
112 { "instruction_tb", VCPU_STAT(instruction_tb
) },
113 { "instruction_tpi", VCPU_STAT(instruction_tpi
) },
114 { "instruction_tprot", VCPU_STAT(instruction_tprot
) },
115 { "instruction_tsch", VCPU_STAT(instruction_tsch
) },
116 { "instruction_sthyi", VCPU_STAT(instruction_sthyi
) },
117 { "instruction_sie", VCPU_STAT(instruction_sie
) },
118 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense
) },
119 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running
) },
120 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call
) },
121 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency
) },
122 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency
) },
123 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start
) },
124 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop
) },
125 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status
) },
126 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status
) },
127 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status
) },
128 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch
) },
129 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix
) },
130 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart
) },
131 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset
) },
132 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset
) },
133 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown
) },
134 { "instruction_diag_10", VCPU_STAT(diagnose_10
) },
135 { "instruction_diag_44", VCPU_STAT(diagnose_44
) },
136 { "instruction_diag_9c", VCPU_STAT(diagnose_9c
) },
137 { "instruction_diag_258", VCPU_STAT(diagnose_258
) },
138 { "instruction_diag_308", VCPU_STAT(diagnose_308
) },
139 { "instruction_diag_500", VCPU_STAT(diagnose_500
) },
140 { "instruction_diag_other", VCPU_STAT(diagnose_other
) },
144 struct kvm_s390_tod_clock_ext
{
150 /* allow nested virtualization in KVM (if enabled by user space) */
152 module_param(nested
, int, S_IRUGO
);
153 MODULE_PARM_DESC(nested
, "Nested virtualization support");
157 * For now we handle at most 16 double words as this is what the s390 base
158 * kernel handles and stores in the prefix page. If we ever need to go beyond
159 * this, this requires changes to code, but the external uapi can stay.
161 #define SIZE_INTERNAL 16
164 * Base feature mask that defines default mask for facilities. Consists of the
165 * defines in FACILITIES_KVM and the non-hypervisor managed bits.
167 static unsigned long kvm_s390_fac_base
[SIZE_INTERNAL
] = { FACILITIES_KVM
};
169 * Extended feature mask. Consists of the defines in FACILITIES_KVM_CPUMODEL
170 * and defines the facilities that can be enabled via a cpu model.
172 static unsigned long kvm_s390_fac_ext
[SIZE_INTERNAL
] = { FACILITIES_KVM_CPUMODEL
};
174 static unsigned long kvm_s390_fac_size(void)
176 BUILD_BUG_ON(SIZE_INTERNAL
> S390_ARCH_FAC_MASK_SIZE_U64
);
177 BUILD_BUG_ON(SIZE_INTERNAL
> S390_ARCH_FAC_LIST_SIZE_U64
);
178 BUILD_BUG_ON(SIZE_INTERNAL
* sizeof(unsigned long) >
179 sizeof(S390_lowcore
.stfle_fac_list
));
181 return SIZE_INTERNAL
;
184 /* available cpu features supported by kvm */
185 static DECLARE_BITMAP(kvm_s390_available_cpu_feat
, KVM_S390_VM_CPU_FEAT_NR_BITS
);
186 /* available subfunctions indicated via query / "test bit" */
187 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc
;
189 static struct gmap_notifier gmap_notifier
;
190 static struct gmap_notifier vsie_gmap_notifier
;
191 debug_info_t
*kvm_s390_dbf
;
193 /* Section: not file related */
194 int kvm_arch_hardware_enable(void)
196 /* every s390 is virtualization enabled ;-) */
200 static void kvm_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
203 static void kvm_clock_sync_scb(struct kvm_s390_sie_block
*scb
, u64 delta
)
208 * The TOD jumps by delta, we have to compensate this by adding
209 * -delta to the epoch.
213 /* sign-extension - we're adding to signed values below */
218 if (scb
->ecd
& ECD_MEF
) {
219 scb
->epdx
+= delta_idx
;
220 if (scb
->epoch
< delta
)
226 * This callback is executed during stop_machine(). All CPUs are therefore
227 * temporarily stopped. In order not to change guest behavior, we have to
228 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
229 * so a CPU won't be stopped while calculating with the epoch.
231 static int kvm_clock_sync(struct notifier_block
*notifier
, unsigned long val
,
235 struct kvm_vcpu
*vcpu
;
237 unsigned long long *delta
= v
;
239 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
240 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
241 kvm_clock_sync_scb(vcpu
->arch
.sie_block
, *delta
);
243 kvm
->arch
.epoch
= vcpu
->arch
.sie_block
->epoch
;
244 kvm
->arch
.epdx
= vcpu
->arch
.sie_block
->epdx
;
246 if (vcpu
->arch
.cputm_enabled
)
247 vcpu
->arch
.cputm_start
+= *delta
;
248 if (vcpu
->arch
.vsie_block
)
249 kvm_clock_sync_scb(vcpu
->arch
.vsie_block
,
256 static struct notifier_block kvm_clock_notifier
= {
257 .notifier_call
= kvm_clock_sync
,
260 int kvm_arch_hardware_setup(void)
262 gmap_notifier
.notifier_call
= kvm_gmap_notifier
;
263 gmap_register_pte_notifier(&gmap_notifier
);
264 vsie_gmap_notifier
.notifier_call
= kvm_s390_vsie_gmap_notifier
;
265 gmap_register_pte_notifier(&vsie_gmap_notifier
);
266 atomic_notifier_chain_register(&s390_epoch_delta_notifier
,
267 &kvm_clock_notifier
);
271 void kvm_arch_hardware_unsetup(void)
273 gmap_unregister_pte_notifier(&gmap_notifier
);
274 gmap_unregister_pte_notifier(&vsie_gmap_notifier
);
275 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier
,
276 &kvm_clock_notifier
);
279 static void allow_cpu_feat(unsigned long nr
)
281 set_bit_inv(nr
, kvm_s390_available_cpu_feat
);
284 static inline int plo_test_bit(unsigned char nr
)
286 register unsigned long r0
asm("0") = (unsigned long) nr
| 0x100;
290 /* Parameter registers are ignored for "test bit" */
300 static inline void __insn32_query(unsigned int opcode
, u8 query
[32])
302 register unsigned long r0
asm("0") = 0; /* query function */
303 register unsigned long r1
asm("1") = (unsigned long) query
;
306 /* Parameter regs are ignored */
307 " .insn rrf,%[opc] << 16,2,4,6,0\n"
309 : "d" (r0
), "a" (r1
), [opc
] "i" (opcode
)
313 #define INSN_SORTL 0xb938
314 #define INSN_DFLTCC 0xb939
316 static void kvm_s390_cpu_feat_init(void)
320 for (i
= 0; i
< 256; ++i
) {
322 kvm_s390_available_subfunc
.plo
[i
>> 3] |= 0x80 >> (i
& 7);
325 if (test_facility(28)) /* TOD-clock steering */
326 ptff(kvm_s390_available_subfunc
.ptff
,
327 sizeof(kvm_s390_available_subfunc
.ptff
),
330 if (test_facility(17)) { /* MSA */
331 __cpacf_query(CPACF_KMAC
, (cpacf_mask_t
*)
332 kvm_s390_available_subfunc
.kmac
);
333 __cpacf_query(CPACF_KMC
, (cpacf_mask_t
*)
334 kvm_s390_available_subfunc
.kmc
);
335 __cpacf_query(CPACF_KM
, (cpacf_mask_t
*)
336 kvm_s390_available_subfunc
.km
);
337 __cpacf_query(CPACF_KIMD
, (cpacf_mask_t
*)
338 kvm_s390_available_subfunc
.kimd
);
339 __cpacf_query(CPACF_KLMD
, (cpacf_mask_t
*)
340 kvm_s390_available_subfunc
.klmd
);
342 if (test_facility(76)) /* MSA3 */
343 __cpacf_query(CPACF_PCKMO
, (cpacf_mask_t
*)
344 kvm_s390_available_subfunc
.pckmo
);
345 if (test_facility(77)) { /* MSA4 */
346 __cpacf_query(CPACF_KMCTR
, (cpacf_mask_t
*)
347 kvm_s390_available_subfunc
.kmctr
);
348 __cpacf_query(CPACF_KMF
, (cpacf_mask_t
*)
349 kvm_s390_available_subfunc
.kmf
);
350 __cpacf_query(CPACF_KMO
, (cpacf_mask_t
*)
351 kvm_s390_available_subfunc
.kmo
);
352 __cpacf_query(CPACF_PCC
, (cpacf_mask_t
*)
353 kvm_s390_available_subfunc
.pcc
);
355 if (test_facility(57)) /* MSA5 */
356 __cpacf_query(CPACF_PRNO
, (cpacf_mask_t
*)
357 kvm_s390_available_subfunc
.ppno
);
359 if (test_facility(146)) /* MSA8 */
360 __cpacf_query(CPACF_KMA
, (cpacf_mask_t
*)
361 kvm_s390_available_subfunc
.kma
);
363 if (test_facility(155)) /* MSA9 */
364 __cpacf_query(CPACF_KDSA
, (cpacf_mask_t
*)
365 kvm_s390_available_subfunc
.kdsa
);
367 if (test_facility(150)) /* SORTL */
368 __insn32_query(INSN_SORTL
, kvm_s390_available_subfunc
.sortl
);
370 if (test_facility(151)) /* DFLTCC */
371 __insn32_query(INSN_DFLTCC
, kvm_s390_available_subfunc
.dfltcc
);
373 if (MACHINE_HAS_ESOP
)
374 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP
);
376 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
377 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
379 if (!sclp
.has_sief2
|| !MACHINE_HAS_ESOP
|| !sclp
.has_64bscao
||
380 !test_facility(3) || !nested
)
382 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2
);
383 if (sclp
.has_64bscao
)
384 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO
);
386 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF
);
388 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE
);
390 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS
);
392 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB
);
394 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI
);
396 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS
);
398 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS
);
400 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
401 * all skey handling functions read/set the skey from the PGSTE
402 * instead of the real storage key.
404 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
405 * pages being detected as preserved although they are resident.
407 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
408 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
410 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
411 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
412 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
414 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
415 * cannot easily shadow the SCA because of the ipte lock.
419 int kvm_arch_init(void *opaque
)
421 kvm_s390_dbf
= debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
425 if (debug_register_view(kvm_s390_dbf
, &debug_sprintf_view
)) {
426 debug_unregister(kvm_s390_dbf
);
430 kvm_s390_cpu_feat_init();
432 /* Register floating interrupt controller interface. */
433 return kvm_register_device_ops(&kvm_flic_ops
, KVM_DEV_TYPE_FLIC
);
436 void kvm_arch_exit(void)
438 debug_unregister(kvm_s390_dbf
);
441 /* Section: device related */
442 long kvm_arch_dev_ioctl(struct file
*filp
,
443 unsigned int ioctl
, unsigned long arg
)
445 if (ioctl
== KVM_S390_ENABLE_SIE
)
446 return s390_enable_sie();
450 int kvm_vm_ioctl_check_extension(struct kvm
*kvm
, long ext
)
455 case KVM_CAP_S390_PSW
:
456 case KVM_CAP_S390_GMAP
:
457 case KVM_CAP_SYNC_MMU
:
458 #ifdef CONFIG_KVM_S390_UCONTROL
459 case KVM_CAP_S390_UCONTROL
:
461 case KVM_CAP_ASYNC_PF
:
462 case KVM_CAP_SYNC_REGS
:
463 case KVM_CAP_ONE_REG
:
464 case KVM_CAP_ENABLE_CAP
:
465 case KVM_CAP_S390_CSS_SUPPORT
:
466 case KVM_CAP_IOEVENTFD
:
467 case KVM_CAP_DEVICE_CTRL
:
468 case KVM_CAP_ENABLE_CAP_VM
:
469 case KVM_CAP_S390_IRQCHIP
:
470 case KVM_CAP_VM_ATTRIBUTES
:
471 case KVM_CAP_MP_STATE
:
472 case KVM_CAP_IMMEDIATE_EXIT
:
473 case KVM_CAP_S390_INJECT_IRQ
:
474 case KVM_CAP_S390_USER_SIGP
:
475 case KVM_CAP_S390_USER_STSI
:
476 case KVM_CAP_S390_SKEYS
:
477 case KVM_CAP_S390_IRQ_STATE
:
478 case KVM_CAP_S390_USER_INSTR0
:
479 case KVM_CAP_S390_CMMA_MIGRATION
:
480 case KVM_CAP_S390_AIS
:
481 case KVM_CAP_S390_AIS_MIGRATION
:
484 case KVM_CAP_S390_MEM_OP
:
487 case KVM_CAP_NR_VCPUS
:
488 case KVM_CAP_MAX_VCPUS
:
489 r
= KVM_S390_BSCA_CPU_SLOTS
;
490 if (!kvm_s390_use_sca_entries())
492 else if (sclp
.has_esca
&& sclp
.has_64bscao
)
493 r
= KVM_S390_ESCA_CPU_SLOTS
;
495 case KVM_CAP_NR_MEMSLOTS
:
496 r
= KVM_USER_MEM_SLOTS
;
498 case KVM_CAP_S390_COW
:
499 r
= MACHINE_HAS_ESOP
;
501 case KVM_CAP_S390_VECTOR_REGISTERS
:
504 case KVM_CAP_S390_RI
:
505 r
= test_facility(64);
507 case KVM_CAP_S390_GS
:
508 r
= test_facility(133);
510 case KVM_CAP_S390_BPB
:
511 r
= test_facility(82);
519 static void kvm_s390_sync_dirty_log(struct kvm
*kvm
,
520 struct kvm_memory_slot
*memslot
)
522 gfn_t cur_gfn
, last_gfn
;
523 unsigned long address
;
524 struct gmap
*gmap
= kvm
->arch
.gmap
;
526 /* Loop over all guest pages */
527 last_gfn
= memslot
->base_gfn
+ memslot
->npages
;
528 for (cur_gfn
= memslot
->base_gfn
; cur_gfn
<= last_gfn
; cur_gfn
++) {
529 address
= gfn_to_hva_memslot(memslot
, cur_gfn
);
531 if (test_and_clear_guest_dirty(gmap
->mm
, address
))
532 mark_page_dirty(kvm
, cur_gfn
);
533 if (fatal_signal_pending(current
))
539 /* Section: vm related */
540 static void sca_del_vcpu(struct kvm_vcpu
*vcpu
);
543 * Get (and clear) the dirty memory log for a memory slot.
545 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
546 struct kvm_dirty_log
*log
)
550 struct kvm_memslots
*slots
;
551 struct kvm_memory_slot
*memslot
;
554 if (kvm_is_ucontrol(kvm
))
557 mutex_lock(&kvm
->slots_lock
);
560 if (log
->slot
>= KVM_USER_MEM_SLOTS
)
563 slots
= kvm_memslots(kvm
);
564 memslot
= id_to_memslot(slots
, log
->slot
);
566 if (!memslot
->dirty_bitmap
)
569 kvm_s390_sync_dirty_log(kvm
, memslot
);
570 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
574 /* Clear the dirty log */
576 n
= kvm_dirty_bitmap_bytes(memslot
);
577 memset(memslot
->dirty_bitmap
, 0, n
);
581 mutex_unlock(&kvm
->slots_lock
);
585 static void icpt_operexc_on_all_vcpus(struct kvm
*kvm
)
588 struct kvm_vcpu
*vcpu
;
590 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
591 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC
, vcpu
);
595 static int kvm_vm_ioctl_enable_cap(struct kvm
*kvm
, struct kvm_enable_cap
*cap
)
603 case KVM_CAP_S390_IRQCHIP
:
604 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
605 kvm
->arch
.use_irqchip
= 1;
608 case KVM_CAP_S390_USER_SIGP
:
609 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
610 kvm
->arch
.user_sigp
= 1;
613 case KVM_CAP_S390_VECTOR_REGISTERS
:
614 mutex_lock(&kvm
->lock
);
615 if (kvm
->created_vcpus
) {
617 } else if (MACHINE_HAS_VX
) {
618 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 129);
619 set_kvm_facility(kvm
->arch
.model
.fac_list
, 129);
620 if (test_facility(134)) {
621 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 134);
622 set_kvm_facility(kvm
->arch
.model
.fac_list
, 134);
624 if (test_facility(135)) {
625 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 135);
626 set_kvm_facility(kvm
->arch
.model
.fac_list
, 135);
628 if (test_facility(148)) {
629 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 148);
630 set_kvm_facility(kvm
->arch
.model
.fac_list
, 148);
632 if (test_facility(152)) {
633 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 152);
634 set_kvm_facility(kvm
->arch
.model
.fac_list
, 152);
639 mutex_unlock(&kvm
->lock
);
640 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
641 r
? "(not available)" : "(success)");
643 case KVM_CAP_S390_RI
:
645 mutex_lock(&kvm
->lock
);
646 if (kvm
->created_vcpus
) {
648 } else if (test_facility(64)) {
649 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 64);
650 set_kvm_facility(kvm
->arch
.model
.fac_list
, 64);
653 mutex_unlock(&kvm
->lock
);
654 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_RI %s",
655 r
? "(not available)" : "(success)");
657 case KVM_CAP_S390_AIS
:
658 mutex_lock(&kvm
->lock
);
659 if (kvm
->created_vcpus
) {
662 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 72);
663 set_kvm_facility(kvm
->arch
.model
.fac_list
, 72);
666 mutex_unlock(&kvm
->lock
);
667 VM_EVENT(kvm
, 3, "ENABLE: AIS %s",
668 r
? "(not available)" : "(success)");
670 case KVM_CAP_S390_GS
:
672 mutex_lock(&kvm
->lock
);
673 if (kvm
->created_vcpus
) {
675 } else if (test_facility(133)) {
676 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 133);
677 set_kvm_facility(kvm
->arch
.model
.fac_list
, 133);
680 mutex_unlock(&kvm
->lock
);
681 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_GS %s",
682 r
? "(not available)" : "(success)");
684 case KVM_CAP_S390_USER_STSI
:
685 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
686 kvm
->arch
.user_stsi
= 1;
689 case KVM_CAP_S390_USER_INSTR0
:
690 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
691 kvm
->arch
.user_instr0
= 1;
692 icpt_operexc_on_all_vcpus(kvm
);
702 static int kvm_s390_get_mem_control(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
706 switch (attr
->attr
) {
707 case KVM_S390_VM_MEM_LIMIT_SIZE
:
709 VM_EVENT(kvm
, 3, "QUERY: max guest memory: %lu bytes",
710 kvm
->arch
.mem_limit
);
711 if (put_user(kvm
->arch
.mem_limit
, (u64 __user
*)attr
->addr
))
721 static int kvm_s390_set_mem_control(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
725 switch (attr
->attr
) {
726 case KVM_S390_VM_MEM_ENABLE_CMMA
:
732 VM_EVENT(kvm
, 3, "%s", "ENABLE: CMMA support");
733 mutex_lock(&kvm
->lock
);
734 if (!kvm
->created_vcpus
) {
735 kvm
->arch
.use_cmma
= 1;
738 mutex_unlock(&kvm
->lock
);
740 case KVM_S390_VM_MEM_CLR_CMMA
:
745 if (!kvm
->arch
.use_cmma
)
748 VM_EVENT(kvm
, 3, "%s", "RESET: CMMA states");
749 mutex_lock(&kvm
->lock
);
750 idx
= srcu_read_lock(&kvm
->srcu
);
751 s390_reset_cmma(kvm
->arch
.gmap
->mm
);
752 srcu_read_unlock(&kvm
->srcu
, idx
);
753 mutex_unlock(&kvm
->lock
);
756 case KVM_S390_VM_MEM_LIMIT_SIZE
: {
757 unsigned long new_limit
;
759 if (kvm_is_ucontrol(kvm
))
762 if (get_user(new_limit
, (u64 __user
*)attr
->addr
))
765 if (kvm
->arch
.mem_limit
!= KVM_S390_NO_MEM_LIMIT
&&
766 new_limit
> kvm
->arch
.mem_limit
)
772 /* gmap_create takes last usable address */
773 if (new_limit
!= KVM_S390_NO_MEM_LIMIT
)
777 mutex_lock(&kvm
->lock
);
778 if (!kvm
->created_vcpus
) {
779 /* gmap_create will round the limit up */
780 struct gmap
*new = gmap_create(current
->mm
, new_limit
);
785 gmap_remove(kvm
->arch
.gmap
);
787 kvm
->arch
.gmap
= new;
791 mutex_unlock(&kvm
->lock
);
792 VM_EVENT(kvm
, 3, "SET: max guest address: %lu", new_limit
);
793 VM_EVENT(kvm
, 3, "New guest asce: 0x%pK",
794 (void *) kvm
->arch
.gmap
->asce
);
804 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu
*vcpu
);
806 void kvm_s390_vcpu_crypto_reset_all(struct kvm
*kvm
)
808 struct kvm_vcpu
*vcpu
;
811 kvm_s390_vcpu_block_all(kvm
);
813 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
814 kvm_s390_vcpu_crypto_setup(vcpu
);
815 /* recreate the shadow crycb by leaving the VSIE handler */
816 kvm_s390_sync_request(KVM_REQ_VSIE_RESTART
, vcpu
);
819 kvm_s390_vcpu_unblock_all(kvm
);
822 static int kvm_s390_vm_set_crypto(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
824 mutex_lock(&kvm
->lock
);
825 switch (attr
->attr
) {
826 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW
:
827 if (!test_kvm_facility(kvm
, 76)) {
828 mutex_unlock(&kvm
->lock
);
832 kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
,
833 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
834 kvm
->arch
.crypto
.aes_kw
= 1;
835 VM_EVENT(kvm
, 3, "%s", "ENABLE: AES keywrapping support");
837 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
:
838 if (!test_kvm_facility(kvm
, 76)) {
839 mutex_unlock(&kvm
->lock
);
843 kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
,
844 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
845 kvm
->arch
.crypto
.dea_kw
= 1;
846 VM_EVENT(kvm
, 3, "%s", "ENABLE: DEA keywrapping support");
848 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW
:
849 if (!test_kvm_facility(kvm
, 76)) {
850 mutex_unlock(&kvm
->lock
);
853 kvm
->arch
.crypto
.aes_kw
= 0;
854 memset(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
, 0,
855 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
856 VM_EVENT(kvm
, 3, "%s", "DISABLE: AES keywrapping support");
858 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
:
859 if (!test_kvm_facility(kvm
, 76)) {
860 mutex_unlock(&kvm
->lock
);
863 kvm
->arch
.crypto
.dea_kw
= 0;
864 memset(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
, 0,
865 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
866 VM_EVENT(kvm
, 3, "%s", "DISABLE: DEA keywrapping support");
868 case KVM_S390_VM_CRYPTO_ENABLE_APIE
:
869 if (!ap_instructions_available()) {
870 mutex_unlock(&kvm
->lock
);
873 kvm
->arch
.crypto
.apie
= 1;
875 case KVM_S390_VM_CRYPTO_DISABLE_APIE
:
876 if (!ap_instructions_available()) {
877 mutex_unlock(&kvm
->lock
);
880 kvm
->arch
.crypto
.apie
= 0;
883 mutex_unlock(&kvm
->lock
);
887 kvm_s390_vcpu_crypto_reset_all(kvm
);
888 mutex_unlock(&kvm
->lock
);
892 static void kvm_s390_sync_request_broadcast(struct kvm
*kvm
, int req
)
895 struct kvm_vcpu
*vcpu
;
897 kvm_for_each_vcpu(cx
, vcpu
, kvm
)
898 kvm_s390_sync_request(req
, vcpu
);
902 * Must be called with kvm->srcu held to avoid races on memslots, and with
903 * kvm->slots_lock to avoid races with ourselves and kvm_s390_vm_stop_migration.
905 static int kvm_s390_vm_start_migration(struct kvm
*kvm
)
907 struct kvm_s390_migration_state
*mgs
;
908 struct kvm_memory_slot
*ms
;
909 /* should be the only one */
910 struct kvm_memslots
*slots
;
911 unsigned long ram_pages
;
914 /* migration mode already enabled */
915 if (kvm
->arch
.migration_state
)
918 slots
= kvm_memslots(kvm
);
919 if (!slots
|| !slots
->used_slots
)
922 mgs
= kzalloc(sizeof(*mgs
), GFP_KERNEL
);
925 kvm
->arch
.migration_state
= mgs
;
927 if (kvm
->arch
.use_cmma
) {
929 * Get the first slot. They are reverse sorted by base_gfn, so
930 * the first slot is also the one at the end of the address
931 * space. We have verified above that at least one slot is
934 ms
= slots
->memslots
;
935 /* round up so we only use full longs */
936 ram_pages
= roundup(ms
->base_gfn
+ ms
->npages
, BITS_PER_LONG
);
937 /* allocate enough bytes to store all the bits */
938 mgs
->pgste_bitmap
= vmalloc(ram_pages
/ 8);
939 if (!mgs
->pgste_bitmap
) {
941 kvm
->arch
.migration_state
= NULL
;
945 mgs
->bitmap_size
= ram_pages
;
946 atomic64_set(&mgs
->dirty_pages
, ram_pages
);
947 /* mark all the pages in active slots as dirty */
948 for (slotnr
= 0; slotnr
< slots
->used_slots
; slotnr
++) {
949 ms
= slots
->memslots
+ slotnr
;
950 bitmap_set(mgs
->pgste_bitmap
, ms
->base_gfn
, ms
->npages
);
953 kvm_s390_sync_request_broadcast(kvm
, KVM_REQ_START_MIGRATION
);
959 * Must be called with kvm->slots_lock to avoid races with ourselves and
960 * kvm_s390_vm_start_migration.
962 static int kvm_s390_vm_stop_migration(struct kvm
*kvm
)
964 struct kvm_s390_migration_state
*mgs
;
966 /* migration mode already disabled */
967 if (!kvm
->arch
.migration_state
)
969 mgs
= kvm
->arch
.migration_state
;
970 kvm
->arch
.migration_state
= NULL
;
972 if (kvm
->arch
.use_cmma
) {
973 kvm_s390_sync_request_broadcast(kvm
, KVM_REQ_STOP_MIGRATION
);
974 /* We have to wait for the essa emulation to finish */
975 synchronize_srcu(&kvm
->srcu
);
976 vfree(mgs
->pgste_bitmap
);
982 static int kvm_s390_vm_set_migration(struct kvm
*kvm
,
983 struct kvm_device_attr
*attr
)
987 mutex_lock(&kvm
->slots_lock
);
988 switch (attr
->attr
) {
989 case KVM_S390_VM_MIGRATION_START
:
990 res
= kvm_s390_vm_start_migration(kvm
);
992 case KVM_S390_VM_MIGRATION_STOP
:
993 res
= kvm_s390_vm_stop_migration(kvm
);
998 mutex_unlock(&kvm
->slots_lock
);
1003 static int kvm_s390_vm_get_migration(struct kvm
*kvm
,
1004 struct kvm_device_attr
*attr
)
1006 u64 mig
= (kvm
->arch
.migration_state
!= NULL
);
1008 if (attr
->attr
!= KVM_S390_VM_MIGRATION_STATUS
)
1011 if (copy_to_user((void __user
*)attr
->addr
, &mig
, sizeof(mig
)))
1016 static int kvm_s390_set_tod_ext(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1018 struct kvm_s390_vm_tod_clock gtod
;
1020 if (copy_from_user(>od
, (void __user
*)attr
->addr
, sizeof(gtod
)))
1023 if (!test_kvm_facility(kvm
, 139) && gtod
.epoch_idx
)
1025 kvm_s390_set_tod_clock(kvm
, >od
);
1027 VM_EVENT(kvm
, 3, "SET: TOD extension: 0x%x, TOD base: 0x%llx",
1028 gtod
.epoch_idx
, gtod
.tod
);
1033 static int kvm_s390_set_tod_high(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1037 if (copy_from_user(>od_high
, (void __user
*)attr
->addr
,
1043 VM_EVENT(kvm
, 3, "SET: TOD extension: 0x%x", gtod_high
);
1048 static int kvm_s390_set_tod_low(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1050 struct kvm_s390_vm_tod_clock gtod
= { 0 };
1052 if (copy_from_user(>od
.tod
, (void __user
*)attr
->addr
,
1056 kvm_s390_set_tod_clock(kvm
, >od
);
1057 VM_EVENT(kvm
, 3, "SET: TOD base: 0x%llx", gtod
.tod
);
1061 static int kvm_s390_set_tod(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1068 switch (attr
->attr
) {
1069 case KVM_S390_VM_TOD_EXT
:
1070 ret
= kvm_s390_set_tod_ext(kvm
, attr
);
1072 case KVM_S390_VM_TOD_HIGH
:
1073 ret
= kvm_s390_set_tod_high(kvm
, attr
);
1075 case KVM_S390_VM_TOD_LOW
:
1076 ret
= kvm_s390_set_tod_low(kvm
, attr
);
1085 static void kvm_s390_get_tod_clock_ext(struct kvm
*kvm
,
1086 struct kvm_s390_vm_tod_clock
*gtod
)
1088 struct kvm_s390_tod_clock_ext htod
;
1092 get_tod_clock_ext((char *)&htod
);
1094 gtod
->tod
= htod
.tod
+ kvm
->arch
.epoch
;
1095 gtod
->epoch_idx
= htod
.epoch_idx
+ kvm
->arch
.epdx
;
1097 if (gtod
->tod
< htod
.tod
)
1098 gtod
->epoch_idx
+= 1;
1103 static int kvm_s390_get_tod_ext(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1105 struct kvm_s390_vm_tod_clock gtod
;
1107 memset(>od
, 0, sizeof(gtod
));
1109 if (test_kvm_facility(kvm
, 139))
1110 kvm_s390_get_tod_clock_ext(kvm
, >od
);
1112 gtod
.tod
= kvm_s390_get_tod_clock_fast(kvm
);
1114 if (copy_to_user((void __user
*)attr
->addr
, >od
, sizeof(gtod
)))
1117 VM_EVENT(kvm
, 3, "QUERY: TOD extension: 0x%x, TOD base: 0x%llx",
1118 gtod
.epoch_idx
, gtod
.tod
);
1122 static int kvm_s390_get_tod_high(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1126 if (copy_to_user((void __user
*)attr
->addr
, >od_high
,
1129 VM_EVENT(kvm
, 3, "QUERY: TOD extension: 0x%x", gtod_high
);
1134 static int kvm_s390_get_tod_low(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1138 gtod
= kvm_s390_get_tod_clock_fast(kvm
);
1139 if (copy_to_user((void __user
*)attr
->addr
, >od
, sizeof(gtod
)))
1141 VM_EVENT(kvm
, 3, "QUERY: TOD base: 0x%llx", gtod
);
1146 static int kvm_s390_get_tod(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1153 switch (attr
->attr
) {
1154 case KVM_S390_VM_TOD_EXT
:
1155 ret
= kvm_s390_get_tod_ext(kvm
, attr
);
1157 case KVM_S390_VM_TOD_HIGH
:
1158 ret
= kvm_s390_get_tod_high(kvm
, attr
);
1160 case KVM_S390_VM_TOD_LOW
:
1161 ret
= kvm_s390_get_tod_low(kvm
, attr
);
1170 static int kvm_s390_set_processor(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1172 struct kvm_s390_vm_cpu_processor
*proc
;
1173 u16 lowest_ibc
, unblocked_ibc
;
1176 mutex_lock(&kvm
->lock
);
1177 if (kvm
->created_vcpus
) {
1181 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
1186 if (!copy_from_user(proc
, (void __user
*)attr
->addr
,
1188 kvm
->arch
.model
.cpuid
= proc
->cpuid
;
1189 lowest_ibc
= sclp
.ibc
>> 16 & 0xfff;
1190 unblocked_ibc
= sclp
.ibc
& 0xfff;
1191 if (lowest_ibc
&& proc
->ibc
) {
1192 if (proc
->ibc
> unblocked_ibc
)
1193 kvm
->arch
.model
.ibc
= unblocked_ibc
;
1194 else if (proc
->ibc
< lowest_ibc
)
1195 kvm
->arch
.model
.ibc
= lowest_ibc
;
1197 kvm
->arch
.model
.ibc
= proc
->ibc
;
1199 memcpy(kvm
->arch
.model
.fac_list
, proc
->fac_list
,
1200 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1201 VM_EVENT(kvm
, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1202 kvm
->arch
.model
.ibc
,
1203 kvm
->arch
.model
.cpuid
);
1204 VM_EVENT(kvm
, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1205 kvm
->arch
.model
.fac_list
[0],
1206 kvm
->arch
.model
.fac_list
[1],
1207 kvm
->arch
.model
.fac_list
[2]);
1212 mutex_unlock(&kvm
->lock
);
1216 static int kvm_s390_set_processor_feat(struct kvm
*kvm
,
1217 struct kvm_device_attr
*attr
)
1219 struct kvm_s390_vm_cpu_feat data
;
1222 if (copy_from_user(&data
, (void __user
*)attr
->addr
, sizeof(data
)))
1224 if (!bitmap_subset((unsigned long *) data
.feat
,
1225 kvm_s390_available_cpu_feat
,
1226 KVM_S390_VM_CPU_FEAT_NR_BITS
))
1229 mutex_lock(&kvm
->lock
);
1230 if (!kvm
->created_vcpus
) {
1231 bitmap_copy(kvm
->arch
.cpu_feat
, (unsigned long *) data
.feat
,
1232 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1235 mutex_unlock(&kvm
->lock
);
1239 static int kvm_s390_set_processor_subfunc(struct kvm
*kvm
,
1240 struct kvm_device_attr
*attr
)
1242 mutex_lock(&kvm
->lock
);
1243 if (kvm
->created_vcpus
) {
1244 mutex_unlock(&kvm
->lock
);
1248 if (copy_from_user(&kvm
->arch
.model
.subfuncs
, (void __user
*)attr
->addr
,
1249 sizeof(struct kvm_s390_vm_cpu_subfunc
))) {
1250 mutex_unlock(&kvm
->lock
);
1253 mutex_unlock(&kvm
->lock
);
1255 VM_EVENT(kvm
, 3, "SET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1256 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[0],
1257 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[1],
1258 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[2],
1259 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[3]);
1260 VM_EVENT(kvm
, 3, "SET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1261 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ptff
)[0],
1262 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ptff
)[1]);
1263 VM_EVENT(kvm
, 3, "SET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1264 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmac
)[0],
1265 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmac
)[1]);
1266 VM_EVENT(kvm
, 3, "SET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1267 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmc
)[0],
1268 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmc
)[1]);
1269 VM_EVENT(kvm
, 3, "SET: guest KM subfunc 0x%16.16lx.%16.16lx",
1270 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.km
)[0],
1271 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.km
)[1]);
1272 VM_EVENT(kvm
, 3, "SET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1273 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kimd
)[0],
1274 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kimd
)[1]);
1275 VM_EVENT(kvm
, 3, "SET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1276 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.klmd
)[0],
1277 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.klmd
)[1]);
1278 VM_EVENT(kvm
, 3, "SET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1279 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pckmo
)[0],
1280 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pckmo
)[1]);
1281 VM_EVENT(kvm
, 3, "SET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1282 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmctr
)[0],
1283 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmctr
)[1]);
1284 VM_EVENT(kvm
, 3, "SET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1285 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmf
)[0],
1286 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmf
)[1]);
1287 VM_EVENT(kvm
, 3, "SET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1288 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmo
)[0],
1289 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmo
)[1]);
1290 VM_EVENT(kvm
, 3, "SET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1291 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pcc
)[0],
1292 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pcc
)[1]);
1293 VM_EVENT(kvm
, 3, "SET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1294 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ppno
)[0],
1295 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ppno
)[1]);
1296 VM_EVENT(kvm
, 3, "SET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1297 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kma
)[0],
1298 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kma
)[1]);
1299 VM_EVENT(kvm
, 3, "SET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1300 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kdsa
)[0],
1301 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kdsa
)[1]);
1302 VM_EVENT(kvm
, 3, "SET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1303 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[0],
1304 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[1],
1305 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[2],
1306 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[3]);
1307 VM_EVENT(kvm
, 3, "SET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1308 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[0],
1309 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[1],
1310 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[2],
1311 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[3]);
1316 static int kvm_s390_set_cpu_model(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1320 switch (attr
->attr
) {
1321 case KVM_S390_VM_CPU_PROCESSOR
:
1322 ret
= kvm_s390_set_processor(kvm
, attr
);
1324 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1325 ret
= kvm_s390_set_processor_feat(kvm
, attr
);
1327 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1328 ret
= kvm_s390_set_processor_subfunc(kvm
, attr
);
1334 static int kvm_s390_get_processor(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1336 struct kvm_s390_vm_cpu_processor
*proc
;
1339 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
1344 proc
->cpuid
= kvm
->arch
.model
.cpuid
;
1345 proc
->ibc
= kvm
->arch
.model
.ibc
;
1346 memcpy(&proc
->fac_list
, kvm
->arch
.model
.fac_list
,
1347 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1348 VM_EVENT(kvm
, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
1349 kvm
->arch
.model
.ibc
,
1350 kvm
->arch
.model
.cpuid
);
1351 VM_EVENT(kvm
, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1352 kvm
->arch
.model
.fac_list
[0],
1353 kvm
->arch
.model
.fac_list
[1],
1354 kvm
->arch
.model
.fac_list
[2]);
1355 if (copy_to_user((void __user
*)attr
->addr
, proc
, sizeof(*proc
)))
1362 static int kvm_s390_get_machine(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1364 struct kvm_s390_vm_cpu_machine
*mach
;
1367 mach
= kzalloc(sizeof(*mach
), GFP_KERNEL
);
1372 get_cpu_id((struct cpuid
*) &mach
->cpuid
);
1373 mach
->ibc
= sclp
.ibc
;
1374 memcpy(&mach
->fac_mask
, kvm
->arch
.model
.fac_mask
,
1375 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1376 memcpy((unsigned long *)&mach
->fac_list
, S390_lowcore
.stfle_fac_list
,
1377 sizeof(S390_lowcore
.stfle_fac_list
));
1378 VM_EVENT(kvm
, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
1379 kvm
->arch
.model
.ibc
,
1380 kvm
->arch
.model
.cpuid
);
1381 VM_EVENT(kvm
, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
1385 VM_EVENT(kvm
, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
1389 if (copy_to_user((void __user
*)attr
->addr
, mach
, sizeof(*mach
)))
1396 static int kvm_s390_get_processor_feat(struct kvm
*kvm
,
1397 struct kvm_device_attr
*attr
)
1399 struct kvm_s390_vm_cpu_feat data
;
1401 bitmap_copy((unsigned long *) data
.feat
, kvm
->arch
.cpu_feat
,
1402 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1403 if (copy_to_user((void __user
*)attr
->addr
, &data
, sizeof(data
)))
1408 static int kvm_s390_get_machine_feat(struct kvm
*kvm
,
1409 struct kvm_device_attr
*attr
)
1411 struct kvm_s390_vm_cpu_feat data
;
1413 bitmap_copy((unsigned long *) data
.feat
,
1414 kvm_s390_available_cpu_feat
,
1415 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1416 if (copy_to_user((void __user
*)attr
->addr
, &data
, sizeof(data
)))
1421 static int kvm_s390_get_processor_subfunc(struct kvm
*kvm
,
1422 struct kvm_device_attr
*attr
)
1424 if (copy_to_user((void __user
*)attr
->addr
, &kvm
->arch
.model
.subfuncs
,
1425 sizeof(struct kvm_s390_vm_cpu_subfunc
)))
1428 VM_EVENT(kvm
, 3, "GET: guest PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1429 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[0],
1430 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[1],
1431 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[2],
1432 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.plo
)[3]);
1433 VM_EVENT(kvm
, 3, "GET: guest PTFF subfunc 0x%16.16lx.%16.16lx",
1434 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ptff
)[0],
1435 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ptff
)[1]);
1436 VM_EVENT(kvm
, 3, "GET: guest KMAC subfunc 0x%16.16lx.%16.16lx",
1437 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmac
)[0],
1438 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmac
)[1]);
1439 VM_EVENT(kvm
, 3, "GET: guest KMC subfunc 0x%16.16lx.%16.16lx",
1440 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmc
)[0],
1441 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmc
)[1]);
1442 VM_EVENT(kvm
, 3, "GET: guest KM subfunc 0x%16.16lx.%16.16lx",
1443 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.km
)[0],
1444 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.km
)[1]);
1445 VM_EVENT(kvm
, 3, "GET: guest KIMD subfunc 0x%16.16lx.%16.16lx",
1446 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kimd
)[0],
1447 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kimd
)[1]);
1448 VM_EVENT(kvm
, 3, "GET: guest KLMD subfunc 0x%16.16lx.%16.16lx",
1449 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.klmd
)[0],
1450 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.klmd
)[1]);
1451 VM_EVENT(kvm
, 3, "GET: guest PCKMO subfunc 0x%16.16lx.%16.16lx",
1452 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pckmo
)[0],
1453 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pckmo
)[1]);
1454 VM_EVENT(kvm
, 3, "GET: guest KMCTR subfunc 0x%16.16lx.%16.16lx",
1455 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmctr
)[0],
1456 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmctr
)[1]);
1457 VM_EVENT(kvm
, 3, "GET: guest KMF subfunc 0x%16.16lx.%16.16lx",
1458 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmf
)[0],
1459 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmf
)[1]);
1460 VM_EVENT(kvm
, 3, "GET: guest KMO subfunc 0x%16.16lx.%16.16lx",
1461 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmo
)[0],
1462 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kmo
)[1]);
1463 VM_EVENT(kvm
, 3, "GET: guest PCC subfunc 0x%16.16lx.%16.16lx",
1464 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pcc
)[0],
1465 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.pcc
)[1]);
1466 VM_EVENT(kvm
, 3, "GET: guest PPNO subfunc 0x%16.16lx.%16.16lx",
1467 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ppno
)[0],
1468 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.ppno
)[1]);
1469 VM_EVENT(kvm
, 3, "GET: guest KMA subfunc 0x%16.16lx.%16.16lx",
1470 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kma
)[0],
1471 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kma
)[1]);
1472 VM_EVENT(kvm
, 3, "GET: guest KDSA subfunc 0x%16.16lx.%16.16lx",
1473 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kdsa
)[0],
1474 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.kdsa
)[1]);
1475 VM_EVENT(kvm
, 3, "GET: guest SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1476 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[0],
1477 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[1],
1478 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[2],
1479 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.sortl
)[3]);
1480 VM_EVENT(kvm
, 3, "GET: guest DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1481 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[0],
1482 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[1],
1483 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[2],
1484 ((unsigned long *) &kvm
->arch
.model
.subfuncs
.dfltcc
)[3]);
1489 static int kvm_s390_get_machine_subfunc(struct kvm
*kvm
,
1490 struct kvm_device_attr
*attr
)
1492 if (copy_to_user((void __user
*)attr
->addr
, &kvm_s390_available_subfunc
,
1493 sizeof(struct kvm_s390_vm_cpu_subfunc
)))
1496 VM_EVENT(kvm
, 3, "GET: host PLO subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1497 ((unsigned long *) &kvm_s390_available_subfunc
.plo
)[0],
1498 ((unsigned long *) &kvm_s390_available_subfunc
.plo
)[1],
1499 ((unsigned long *) &kvm_s390_available_subfunc
.plo
)[2],
1500 ((unsigned long *) &kvm_s390_available_subfunc
.plo
)[3]);
1501 VM_EVENT(kvm
, 3, "GET: host PTFF subfunc 0x%16.16lx.%16.16lx",
1502 ((unsigned long *) &kvm_s390_available_subfunc
.ptff
)[0],
1503 ((unsigned long *) &kvm_s390_available_subfunc
.ptff
)[1]);
1504 VM_EVENT(kvm
, 3, "GET: host KMAC subfunc 0x%16.16lx.%16.16lx",
1505 ((unsigned long *) &kvm_s390_available_subfunc
.kmac
)[0],
1506 ((unsigned long *) &kvm_s390_available_subfunc
.kmac
)[1]);
1507 VM_EVENT(kvm
, 3, "GET: host KMC subfunc 0x%16.16lx.%16.16lx",
1508 ((unsigned long *) &kvm_s390_available_subfunc
.kmc
)[0],
1509 ((unsigned long *) &kvm_s390_available_subfunc
.kmc
)[1]);
1510 VM_EVENT(kvm
, 3, "GET: host KM subfunc 0x%16.16lx.%16.16lx",
1511 ((unsigned long *) &kvm_s390_available_subfunc
.km
)[0],
1512 ((unsigned long *) &kvm_s390_available_subfunc
.km
)[1]);
1513 VM_EVENT(kvm
, 3, "GET: host KIMD subfunc 0x%16.16lx.%16.16lx",
1514 ((unsigned long *) &kvm_s390_available_subfunc
.kimd
)[0],
1515 ((unsigned long *) &kvm_s390_available_subfunc
.kimd
)[1]);
1516 VM_EVENT(kvm
, 3, "GET: host KLMD subfunc 0x%16.16lx.%16.16lx",
1517 ((unsigned long *) &kvm_s390_available_subfunc
.klmd
)[0],
1518 ((unsigned long *) &kvm_s390_available_subfunc
.klmd
)[1]);
1519 VM_EVENT(kvm
, 3, "GET: host PCKMO subfunc 0x%16.16lx.%16.16lx",
1520 ((unsigned long *) &kvm_s390_available_subfunc
.pckmo
)[0],
1521 ((unsigned long *) &kvm_s390_available_subfunc
.pckmo
)[1]);
1522 VM_EVENT(kvm
, 3, "GET: host KMCTR subfunc 0x%16.16lx.%16.16lx",
1523 ((unsigned long *) &kvm_s390_available_subfunc
.kmctr
)[0],
1524 ((unsigned long *) &kvm_s390_available_subfunc
.kmctr
)[1]);
1525 VM_EVENT(kvm
, 3, "GET: host KMF subfunc 0x%16.16lx.%16.16lx",
1526 ((unsigned long *) &kvm_s390_available_subfunc
.kmf
)[0],
1527 ((unsigned long *) &kvm_s390_available_subfunc
.kmf
)[1]);
1528 VM_EVENT(kvm
, 3, "GET: host KMO subfunc 0x%16.16lx.%16.16lx",
1529 ((unsigned long *) &kvm_s390_available_subfunc
.kmo
)[0],
1530 ((unsigned long *) &kvm_s390_available_subfunc
.kmo
)[1]);
1531 VM_EVENT(kvm
, 3, "GET: host PCC subfunc 0x%16.16lx.%16.16lx",
1532 ((unsigned long *) &kvm_s390_available_subfunc
.pcc
)[0],
1533 ((unsigned long *) &kvm_s390_available_subfunc
.pcc
)[1]);
1534 VM_EVENT(kvm
, 3, "GET: host PPNO subfunc 0x%16.16lx.%16.16lx",
1535 ((unsigned long *) &kvm_s390_available_subfunc
.ppno
)[0],
1536 ((unsigned long *) &kvm_s390_available_subfunc
.ppno
)[1]);
1537 VM_EVENT(kvm
, 3, "GET: host KMA subfunc 0x%16.16lx.%16.16lx",
1538 ((unsigned long *) &kvm_s390_available_subfunc
.kma
)[0],
1539 ((unsigned long *) &kvm_s390_available_subfunc
.kma
)[1]);
1540 VM_EVENT(kvm
, 3, "GET: host KDSA subfunc 0x%16.16lx.%16.16lx",
1541 ((unsigned long *) &kvm_s390_available_subfunc
.kdsa
)[0],
1542 ((unsigned long *) &kvm_s390_available_subfunc
.kdsa
)[1]);
1543 VM_EVENT(kvm
, 3, "GET: host SORTL subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1544 ((unsigned long *) &kvm_s390_available_subfunc
.sortl
)[0],
1545 ((unsigned long *) &kvm_s390_available_subfunc
.sortl
)[1],
1546 ((unsigned long *) &kvm_s390_available_subfunc
.sortl
)[2],
1547 ((unsigned long *) &kvm_s390_available_subfunc
.sortl
)[3]);
1548 VM_EVENT(kvm
, 3, "GET: host DFLTCC subfunc 0x%16.16lx.%16.16lx.%16.16lx.%16.16lx",
1549 ((unsigned long *) &kvm_s390_available_subfunc
.dfltcc
)[0],
1550 ((unsigned long *) &kvm_s390_available_subfunc
.dfltcc
)[1],
1551 ((unsigned long *) &kvm_s390_available_subfunc
.dfltcc
)[2],
1552 ((unsigned long *) &kvm_s390_available_subfunc
.dfltcc
)[3]);
1557 static int kvm_s390_get_cpu_model(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1561 switch (attr
->attr
) {
1562 case KVM_S390_VM_CPU_PROCESSOR
:
1563 ret
= kvm_s390_get_processor(kvm
, attr
);
1565 case KVM_S390_VM_CPU_MACHINE
:
1566 ret
= kvm_s390_get_machine(kvm
, attr
);
1568 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1569 ret
= kvm_s390_get_processor_feat(kvm
, attr
);
1571 case KVM_S390_VM_CPU_MACHINE_FEAT
:
1572 ret
= kvm_s390_get_machine_feat(kvm
, attr
);
1574 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1575 ret
= kvm_s390_get_processor_subfunc(kvm
, attr
);
1577 case KVM_S390_VM_CPU_MACHINE_SUBFUNC
:
1578 ret
= kvm_s390_get_machine_subfunc(kvm
, attr
);
1584 static int kvm_s390_vm_set_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1588 switch (attr
->group
) {
1589 case KVM_S390_VM_MEM_CTRL
:
1590 ret
= kvm_s390_set_mem_control(kvm
, attr
);
1592 case KVM_S390_VM_TOD
:
1593 ret
= kvm_s390_set_tod(kvm
, attr
);
1595 case KVM_S390_VM_CPU_MODEL
:
1596 ret
= kvm_s390_set_cpu_model(kvm
, attr
);
1598 case KVM_S390_VM_CRYPTO
:
1599 ret
= kvm_s390_vm_set_crypto(kvm
, attr
);
1601 case KVM_S390_VM_MIGRATION
:
1602 ret
= kvm_s390_vm_set_migration(kvm
, attr
);
1612 static int kvm_s390_vm_get_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1616 switch (attr
->group
) {
1617 case KVM_S390_VM_MEM_CTRL
:
1618 ret
= kvm_s390_get_mem_control(kvm
, attr
);
1620 case KVM_S390_VM_TOD
:
1621 ret
= kvm_s390_get_tod(kvm
, attr
);
1623 case KVM_S390_VM_CPU_MODEL
:
1624 ret
= kvm_s390_get_cpu_model(kvm
, attr
);
1626 case KVM_S390_VM_MIGRATION
:
1627 ret
= kvm_s390_vm_get_migration(kvm
, attr
);
1637 static int kvm_s390_vm_has_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1641 switch (attr
->group
) {
1642 case KVM_S390_VM_MEM_CTRL
:
1643 switch (attr
->attr
) {
1644 case KVM_S390_VM_MEM_ENABLE_CMMA
:
1645 case KVM_S390_VM_MEM_CLR_CMMA
:
1646 ret
= sclp
.has_cmma
? 0 : -ENXIO
;
1648 case KVM_S390_VM_MEM_LIMIT_SIZE
:
1656 case KVM_S390_VM_TOD
:
1657 switch (attr
->attr
) {
1658 case KVM_S390_VM_TOD_LOW
:
1659 case KVM_S390_VM_TOD_HIGH
:
1667 case KVM_S390_VM_CPU_MODEL
:
1668 switch (attr
->attr
) {
1669 case KVM_S390_VM_CPU_PROCESSOR
:
1670 case KVM_S390_VM_CPU_MACHINE
:
1671 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1672 case KVM_S390_VM_CPU_MACHINE_FEAT
:
1673 case KVM_S390_VM_CPU_MACHINE_SUBFUNC
:
1674 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1682 case KVM_S390_VM_CRYPTO
:
1683 switch (attr
->attr
) {
1684 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW
:
1685 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
:
1686 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW
:
1687 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
:
1690 case KVM_S390_VM_CRYPTO_ENABLE_APIE
:
1691 case KVM_S390_VM_CRYPTO_DISABLE_APIE
:
1692 ret
= ap_instructions_available() ? 0 : -ENXIO
;
1699 case KVM_S390_VM_MIGRATION
:
1710 static long kvm_s390_get_skeys(struct kvm
*kvm
, struct kvm_s390_skeys
*args
)
1714 int srcu_idx
, i
, r
= 0;
1716 if (args
->flags
!= 0)
1719 /* Is this guest using storage keys? */
1720 if (!mm_use_skey(current
->mm
))
1721 return KVM_S390_GET_SKEYS_NONE
;
1723 /* Enforce sane limit on memory allocation */
1724 if (args
->count
< 1 || args
->count
> KVM_S390_SKEYS_MAX
)
1727 keys
= kvmalloc_array(args
->count
, sizeof(uint8_t), GFP_KERNEL
);
1731 down_read(¤t
->mm
->mmap_sem
);
1732 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1733 for (i
= 0; i
< args
->count
; i
++) {
1734 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1735 if (kvm_is_error_hva(hva
)) {
1740 r
= get_guest_storage_key(current
->mm
, hva
, &keys
[i
]);
1744 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1745 up_read(¤t
->mm
->mmap_sem
);
1748 r
= copy_to_user((uint8_t __user
*)args
->skeydata_addr
, keys
,
1749 sizeof(uint8_t) * args
->count
);
1758 static long kvm_s390_set_skeys(struct kvm
*kvm
, struct kvm_s390_skeys
*args
)
1762 int srcu_idx
, i
, r
= 0;
1764 if (args
->flags
!= 0)
1767 /* Enforce sane limit on memory allocation */
1768 if (args
->count
< 1 || args
->count
> KVM_S390_SKEYS_MAX
)
1771 keys
= kvmalloc_array(args
->count
, sizeof(uint8_t), GFP_KERNEL
);
1775 r
= copy_from_user(keys
, (uint8_t __user
*)args
->skeydata_addr
,
1776 sizeof(uint8_t) * args
->count
);
1782 /* Enable storage key handling for the guest */
1783 r
= s390_enable_skey();
1787 down_read(¤t
->mm
->mmap_sem
);
1788 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1789 for (i
= 0; i
< args
->count
; i
++) {
1790 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1791 if (kvm_is_error_hva(hva
)) {
1796 /* Lowest order bit is reserved */
1797 if (keys
[i
] & 0x01) {
1802 r
= set_guest_storage_key(current
->mm
, hva
, keys
[i
], 0);
1806 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1807 up_read(¤t
->mm
->mmap_sem
);
1814 * Base address and length must be sent at the start of each block, therefore
1815 * it's cheaper to send some clean data, as long as it's less than the size of
1818 #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *))
1819 /* for consistency */
1820 #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX)
1823 * This function searches for the next page with dirty CMMA attributes, and
1824 * saves the attributes in the buffer up to either the end of the buffer or
1825 * until a block of at least KVM_S390_MAX_BIT_DISTANCE clean bits is found;
1826 * no trailing clean bytes are saved.
1827 * In case no dirty bits were found, or if CMMA was not enabled or used, the
1828 * output buffer will indicate 0 as length.
1830 static int kvm_s390_get_cmma_bits(struct kvm
*kvm
,
1831 struct kvm_s390_cmma_log
*args
)
1833 struct kvm_s390_migration_state
*s
= kvm
->arch
.migration_state
;
1834 unsigned long bufsize
, hva
, pgstev
, i
, next
, cur
;
1835 int srcu_idx
, peek
, r
= 0, rr
;
1838 cur
= args
->start_gfn
;
1839 i
= next
= pgstev
= 0;
1841 if (unlikely(!kvm
->arch
.use_cmma
))
1843 /* Invalid/unsupported flags were specified */
1844 if (args
->flags
& ~KVM_S390_CMMA_PEEK
)
1846 /* Migration mode query, and we are not doing a migration */
1847 peek
= !!(args
->flags
& KVM_S390_CMMA_PEEK
);
1850 /* CMMA is disabled or was not used, or the buffer has length zero */
1851 bufsize
= min(args
->count
, KVM_S390_CMMA_SIZE_MAX
);
1852 if (!bufsize
|| !kvm
->mm
->context
.use_cmma
) {
1853 memset(args
, 0, sizeof(*args
));
1858 /* We are not peeking, and there are no dirty pages */
1859 if (!atomic64_read(&s
->dirty_pages
)) {
1860 memset(args
, 0, sizeof(*args
));
1863 cur
= find_next_bit(s
->pgste_bitmap
, s
->bitmap_size
,
1865 if (cur
>= s
->bitmap_size
) /* nothing found, loop back */
1866 cur
= find_next_bit(s
->pgste_bitmap
, s
->bitmap_size
, 0);
1867 if (cur
>= s
->bitmap_size
) { /* again! (very unlikely) */
1868 memset(args
, 0, sizeof(*args
));
1871 next
= find_next_bit(s
->pgste_bitmap
, s
->bitmap_size
, cur
+ 1);
1874 res
= vmalloc(bufsize
);
1878 args
->start_gfn
= cur
;
1880 down_read(&kvm
->mm
->mmap_sem
);
1881 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1882 while (i
< bufsize
) {
1883 hva
= gfn_to_hva(kvm
, cur
);
1884 if (kvm_is_error_hva(hva
)) {
1888 /* decrement only if we actually flipped the bit to 0 */
1889 if (!peek
&& test_and_clear_bit(cur
, s
->pgste_bitmap
))
1890 atomic64_dec(&s
->dirty_pages
);
1891 r
= get_pgste(kvm
->mm
, hva
, &pgstev
);
1894 /* save the value */
1895 res
[i
++] = (pgstev
>> 24) & 0x43;
1897 * if the next bit is too far away, stop.
1898 * if we reached the previous "next", find the next one
1901 if (next
> cur
+ KVM_S390_MAX_BIT_DISTANCE
)
1904 next
= find_next_bit(s
->pgste_bitmap
,
1905 s
->bitmap_size
, cur
+ 1);
1906 /* reached the end of the bitmap or of the buffer, stop */
1907 if ((next
>= s
->bitmap_size
) ||
1908 (next
>= args
->start_gfn
+ bufsize
))
1913 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1914 up_read(&kvm
->mm
->mmap_sem
);
1916 args
->remaining
= s
? atomic64_read(&s
->dirty_pages
) : 0;
1918 rr
= copy_to_user((void __user
*)args
->values
, res
, args
->count
);
1927 * This function sets the CMMA attributes for the given pages. If the input
1928 * buffer has zero length, no action is taken, otherwise the attributes are
1929 * set and the mm->context.use_cmma flag is set.
1931 static int kvm_s390_set_cmma_bits(struct kvm
*kvm
,
1932 const struct kvm_s390_cmma_log
*args
)
1934 unsigned long hva
, mask
, pgstev
, i
;
1936 int srcu_idx
, r
= 0;
1940 if (!kvm
->arch
.use_cmma
)
1942 /* invalid/unsupported flags */
1943 if (args
->flags
!= 0)
1945 /* Enforce sane limit on memory allocation */
1946 if (args
->count
> KVM_S390_CMMA_SIZE_MAX
)
1949 if (args
->count
== 0)
1952 bits
= vmalloc(sizeof(*bits
) * args
->count
);
1956 r
= copy_from_user(bits
, (void __user
*)args
->values
, args
->count
);
1962 down_read(&kvm
->mm
->mmap_sem
);
1963 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1964 for (i
= 0; i
< args
->count
; i
++) {
1965 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1966 if (kvm_is_error_hva(hva
)) {
1972 pgstev
= pgstev
<< 24;
1973 mask
&= _PGSTE_GPS_USAGE_MASK
| _PGSTE_GPS_NODAT
;
1974 set_pgste_bits(kvm
->mm
, hva
, mask
, pgstev
);
1976 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1977 up_read(&kvm
->mm
->mmap_sem
);
1979 if (!kvm
->mm
->context
.use_cmma
) {
1980 down_write(&kvm
->mm
->mmap_sem
);
1981 kvm
->mm
->context
.use_cmma
= 1;
1982 up_write(&kvm
->mm
->mmap_sem
);
1989 long kvm_arch_vm_ioctl(struct file
*filp
,
1990 unsigned int ioctl
, unsigned long arg
)
1992 struct kvm
*kvm
= filp
->private_data
;
1993 void __user
*argp
= (void __user
*)arg
;
1994 struct kvm_device_attr attr
;
1998 case KVM_S390_INTERRUPT
: {
1999 struct kvm_s390_interrupt s390int
;
2002 if (copy_from_user(&s390int
, argp
, sizeof(s390int
)))
2004 r
= kvm_s390_inject_vm(kvm
, &s390int
);
2007 case KVM_ENABLE_CAP
: {
2008 struct kvm_enable_cap cap
;
2010 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
2012 r
= kvm_vm_ioctl_enable_cap(kvm
, &cap
);
2015 case KVM_CREATE_IRQCHIP
: {
2016 struct kvm_irq_routing_entry routing
;
2019 if (kvm
->arch
.use_irqchip
) {
2020 /* Set up dummy routing. */
2021 memset(&routing
, 0, sizeof(routing
));
2022 r
= kvm_set_irq_routing(kvm
, &routing
, 0, 0);
2026 case KVM_SET_DEVICE_ATTR
: {
2028 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
2030 r
= kvm_s390_vm_set_attr(kvm
, &attr
);
2033 case KVM_GET_DEVICE_ATTR
: {
2035 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
2037 r
= kvm_s390_vm_get_attr(kvm
, &attr
);
2040 case KVM_HAS_DEVICE_ATTR
: {
2042 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
2044 r
= kvm_s390_vm_has_attr(kvm
, &attr
);
2047 case KVM_S390_GET_SKEYS
: {
2048 struct kvm_s390_skeys args
;
2051 if (copy_from_user(&args
, argp
,
2052 sizeof(struct kvm_s390_skeys
)))
2054 r
= kvm_s390_get_skeys(kvm
, &args
);
2057 case KVM_S390_SET_SKEYS
: {
2058 struct kvm_s390_skeys args
;
2061 if (copy_from_user(&args
, argp
,
2062 sizeof(struct kvm_s390_skeys
)))
2064 r
= kvm_s390_set_skeys(kvm
, &args
);
2067 case KVM_S390_GET_CMMA_BITS
: {
2068 struct kvm_s390_cmma_log args
;
2071 if (copy_from_user(&args
, argp
, sizeof(args
)))
2073 mutex_lock(&kvm
->slots_lock
);
2074 r
= kvm_s390_get_cmma_bits(kvm
, &args
);
2075 mutex_unlock(&kvm
->slots_lock
);
2077 r
= copy_to_user(argp
, &args
, sizeof(args
));
2083 case KVM_S390_SET_CMMA_BITS
: {
2084 struct kvm_s390_cmma_log args
;
2087 if (copy_from_user(&args
, argp
, sizeof(args
)))
2089 mutex_lock(&kvm
->slots_lock
);
2090 r
= kvm_s390_set_cmma_bits(kvm
, &args
);
2091 mutex_unlock(&kvm
->slots_lock
);
2101 static int kvm_s390_apxa_installed(void)
2103 struct ap_config_info info
;
2105 if (ap_instructions_available()) {
2106 if (ap_qci(&info
) == 0)
2114 * The format of the crypto control block (CRYCB) is specified in the 3 low
2115 * order bits of the CRYCB designation (CRYCBD) field as follows:
2116 * Format 0: Neither the message security assist extension 3 (MSAX3) nor the
2117 * AP extended addressing (APXA) facility are installed.
2118 * Format 1: The APXA facility is not installed but the MSAX3 facility is.
2119 * Format 2: Both the APXA and MSAX3 facilities are installed
2121 static void kvm_s390_set_crycb_format(struct kvm
*kvm
)
2123 kvm
->arch
.crypto
.crycbd
= (__u32
)(unsigned long) kvm
->arch
.crypto
.crycb
;
2125 /* Clear the CRYCB format bits - i.e., set format 0 by default */
2126 kvm
->arch
.crypto
.crycbd
&= ~(CRYCB_FORMAT_MASK
);
2128 /* Check whether MSAX3 is installed */
2129 if (!test_kvm_facility(kvm
, 76))
2132 if (kvm_s390_apxa_installed())
2133 kvm
->arch
.crypto
.crycbd
|= CRYCB_FORMAT2
;
2135 kvm
->arch
.crypto
.crycbd
|= CRYCB_FORMAT1
;
2138 void kvm_arch_crypto_set_masks(struct kvm
*kvm
, unsigned long *apm
,
2139 unsigned long *aqm
, unsigned long *adm
)
2141 struct kvm_s390_crypto_cb
*crycb
= kvm
->arch
.crypto
.crycb
;
2143 mutex_lock(&kvm
->lock
);
2144 kvm_s390_vcpu_block_all(kvm
);
2146 switch (kvm
->arch
.crypto
.crycbd
& CRYCB_FORMAT_MASK
) {
2147 case CRYCB_FORMAT2
: /* APCB1 use 256 bits */
2148 memcpy(crycb
->apcb1
.apm
, apm
, 32);
2149 VM_EVENT(kvm
, 3, "SET CRYCB: apm %016lx %016lx %016lx %016lx",
2150 apm
[0], apm
[1], apm
[2], apm
[3]);
2151 memcpy(crycb
->apcb1
.aqm
, aqm
, 32);
2152 VM_EVENT(kvm
, 3, "SET CRYCB: aqm %016lx %016lx %016lx %016lx",
2153 aqm
[0], aqm
[1], aqm
[2], aqm
[3]);
2154 memcpy(crycb
->apcb1
.adm
, adm
, 32);
2155 VM_EVENT(kvm
, 3, "SET CRYCB: adm %016lx %016lx %016lx %016lx",
2156 adm
[0], adm
[1], adm
[2], adm
[3]);
2159 case CRYCB_FORMAT0
: /* Fall through both use APCB0 */
2160 memcpy(crycb
->apcb0
.apm
, apm
, 8);
2161 memcpy(crycb
->apcb0
.aqm
, aqm
, 2);
2162 memcpy(crycb
->apcb0
.adm
, adm
, 2);
2163 VM_EVENT(kvm
, 3, "SET CRYCB: apm %016lx aqm %04x adm %04x",
2164 apm
[0], *((unsigned short *)aqm
),
2165 *((unsigned short *)adm
));
2167 default: /* Can not happen */
2171 /* recreate the shadow crycb for each vcpu */
2172 kvm_s390_sync_request_broadcast(kvm
, KVM_REQ_VSIE_RESTART
);
2173 kvm_s390_vcpu_unblock_all(kvm
);
2174 mutex_unlock(&kvm
->lock
);
2176 EXPORT_SYMBOL_GPL(kvm_arch_crypto_set_masks
);
2178 void kvm_arch_crypto_clear_masks(struct kvm
*kvm
)
2180 mutex_lock(&kvm
->lock
);
2181 kvm_s390_vcpu_block_all(kvm
);
2183 memset(&kvm
->arch
.crypto
.crycb
->apcb0
, 0,
2184 sizeof(kvm
->arch
.crypto
.crycb
->apcb0
));
2185 memset(&kvm
->arch
.crypto
.crycb
->apcb1
, 0,
2186 sizeof(kvm
->arch
.crypto
.crycb
->apcb1
));
2188 VM_EVENT(kvm
, 3, "%s", "CLR CRYCB:");
2189 /* recreate the shadow crycb for each vcpu */
2190 kvm_s390_sync_request_broadcast(kvm
, KVM_REQ_VSIE_RESTART
);
2191 kvm_s390_vcpu_unblock_all(kvm
);
2192 mutex_unlock(&kvm
->lock
);
2194 EXPORT_SYMBOL_GPL(kvm_arch_crypto_clear_masks
);
2196 static u64
kvm_s390_get_initial_cpuid(void)
2201 cpuid
.version
= 0xff;
2202 return *((u64
*) &cpuid
);
2205 static void kvm_s390_crypto_init(struct kvm
*kvm
)
2207 kvm
->arch
.crypto
.crycb
= &kvm
->arch
.sie_page2
->crycb
;
2208 kvm_s390_set_crycb_format(kvm
);
2210 if (!test_kvm_facility(kvm
, 76))
2213 /* Enable AES/DEA protected key functions by default */
2214 kvm
->arch
.crypto
.aes_kw
= 1;
2215 kvm
->arch
.crypto
.dea_kw
= 1;
2216 get_random_bytes(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
,
2217 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
2218 get_random_bytes(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
,
2219 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
2222 static void sca_dispose(struct kvm
*kvm
)
2224 if (kvm
->arch
.use_esca
)
2225 free_pages_exact(kvm
->arch
.sca
, sizeof(struct esca_block
));
2227 free_page((unsigned long)(kvm
->arch
.sca
));
2228 kvm
->arch
.sca
= NULL
;
2231 int kvm_arch_init_vm(struct kvm
*kvm
, unsigned long type
)
2233 gfp_t alloc_flags
= GFP_KERNEL
;
2235 char debug_name
[16];
2236 static unsigned long sca_offset
;
2239 #ifdef CONFIG_KVM_S390_UCONTROL
2240 if (type
& ~KVM_VM_S390_UCONTROL
)
2242 if ((type
& KVM_VM_S390_UCONTROL
) && (!capable(CAP_SYS_ADMIN
)))
2249 rc
= s390_enable_sie();
2255 kvm
->arch
.use_esca
= 0; /* start with basic SCA */
2256 if (!sclp
.has_64bscao
)
2257 alloc_flags
|= GFP_DMA
;
2258 rwlock_init(&kvm
->arch
.sca_lock
);
2259 kvm
->arch
.sca
= (struct bsca_block
*) get_zeroed_page(alloc_flags
);
2262 spin_lock(&kvm_lock
);
2264 if (sca_offset
+ sizeof(struct bsca_block
) > PAGE_SIZE
)
2266 kvm
->arch
.sca
= (struct bsca_block
*)
2267 ((char *) kvm
->arch
.sca
+ sca_offset
);
2268 spin_unlock(&kvm_lock
);
2270 sprintf(debug_name
, "kvm-%u", current
->pid
);
2272 kvm
->arch
.dbf
= debug_register(debug_name
, 32, 1, 7 * sizeof(long));
2276 kvm
->arch
.sie_page2
=
2277 (struct sie_page2
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
2278 if (!kvm
->arch
.sie_page2
)
2281 kvm
->arch
.model
.fac_list
= kvm
->arch
.sie_page2
->fac_list
;
2283 for (i
= 0; i
< kvm_s390_fac_size(); i
++) {
2284 kvm
->arch
.model
.fac_mask
[i
] = S390_lowcore
.stfle_fac_list
[i
] &
2285 (kvm_s390_fac_base
[i
] |
2286 kvm_s390_fac_ext
[i
]);
2287 kvm
->arch
.model
.fac_list
[i
] = S390_lowcore
.stfle_fac_list
[i
] &
2288 kvm_s390_fac_base
[i
];
2290 kvm
->arch
.model
.subfuncs
= kvm_s390_available_subfunc
;
2292 /* we are always in czam mode - even on pre z14 machines */
2293 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 138);
2294 set_kvm_facility(kvm
->arch
.model
.fac_list
, 138);
2295 /* we emulate STHYI in kvm */
2296 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 74);
2297 set_kvm_facility(kvm
->arch
.model
.fac_list
, 74);
2298 if (MACHINE_HAS_TLB_GUEST
) {
2299 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 147);
2300 set_kvm_facility(kvm
->arch
.model
.fac_list
, 147);
2303 kvm
->arch
.model
.cpuid
= kvm_s390_get_initial_cpuid();
2304 kvm
->arch
.model
.ibc
= sclp
.ibc
& 0x0fff;
2306 kvm_s390_crypto_init(kvm
);
2308 mutex_init(&kvm
->arch
.float_int
.ais_lock
);
2309 kvm
->arch
.float_int
.simm
= 0;
2310 kvm
->arch
.float_int
.nimm
= 0;
2311 spin_lock_init(&kvm
->arch
.float_int
.lock
);
2312 for (i
= 0; i
< FIRQ_LIST_COUNT
; i
++)
2313 INIT_LIST_HEAD(&kvm
->arch
.float_int
.lists
[i
]);
2314 init_waitqueue_head(&kvm
->arch
.ipte_wq
);
2315 mutex_init(&kvm
->arch
.ipte_mutex
);
2317 debug_register_view(kvm
->arch
.dbf
, &debug_sprintf_view
);
2318 VM_EVENT(kvm
, 3, "vm created with type %lu", type
);
2320 if (type
& KVM_VM_S390_UCONTROL
) {
2321 kvm
->arch
.gmap
= NULL
;
2322 kvm
->arch
.mem_limit
= KVM_S390_NO_MEM_LIMIT
;
2324 if (sclp
.hamax
== U64_MAX
)
2325 kvm
->arch
.mem_limit
= TASK_SIZE_MAX
;
2327 kvm
->arch
.mem_limit
= min_t(unsigned long, TASK_SIZE_MAX
,
2329 kvm
->arch
.gmap
= gmap_create(current
->mm
, kvm
->arch
.mem_limit
- 1);
2330 if (!kvm
->arch
.gmap
)
2332 kvm
->arch
.gmap
->private = kvm
;
2333 kvm
->arch
.gmap
->pfault_enabled
= 0;
2336 kvm
->arch
.css_support
= 0;
2337 kvm
->arch
.use_irqchip
= 0;
2338 kvm
->arch
.epoch
= 0;
2340 spin_lock_init(&kvm
->arch
.start_stop_lock
);
2341 kvm_s390_vsie_init(kvm
);
2342 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm
, current
->pid
);
2346 free_page((unsigned long)kvm
->arch
.sie_page2
);
2347 debug_unregister(kvm
->arch
.dbf
);
2349 KVM_EVENT(3, "creation of vm failed: %d", rc
);
2353 bool kvm_arch_has_vcpu_debugfs(void)
2358 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu
*vcpu
)
2363 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
2365 VCPU_EVENT(vcpu
, 3, "%s", "free cpu");
2366 trace_kvm_s390_destroy_vcpu(vcpu
->vcpu_id
);
2367 kvm_s390_clear_local_irqs(vcpu
);
2368 kvm_clear_async_pf_completion_queue(vcpu
);
2369 if (!kvm_is_ucontrol(vcpu
->kvm
))
2372 if (kvm_is_ucontrol(vcpu
->kvm
))
2373 gmap_remove(vcpu
->arch
.gmap
);
2375 if (vcpu
->kvm
->arch
.use_cmma
)
2376 kvm_s390_vcpu_unsetup_cmma(vcpu
);
2377 free_page((unsigned long)(vcpu
->arch
.sie_block
));
2379 kvm_vcpu_uninit(vcpu
);
2380 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
2383 static void kvm_free_vcpus(struct kvm
*kvm
)
2386 struct kvm_vcpu
*vcpu
;
2388 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2389 kvm_arch_vcpu_destroy(vcpu
);
2391 mutex_lock(&kvm
->lock
);
2392 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
2393 kvm
->vcpus
[i
] = NULL
;
2395 atomic_set(&kvm
->online_vcpus
, 0);
2396 mutex_unlock(&kvm
->lock
);
2399 void kvm_arch_destroy_vm(struct kvm
*kvm
)
2401 kvm_free_vcpus(kvm
);
2403 debug_unregister(kvm
->arch
.dbf
);
2404 free_page((unsigned long)kvm
->arch
.sie_page2
);
2405 if (!kvm_is_ucontrol(kvm
))
2406 gmap_remove(kvm
->arch
.gmap
);
2407 kvm_s390_destroy_adapters(kvm
);
2408 kvm_s390_clear_float_irqs(kvm
);
2409 kvm_s390_vsie_destroy(kvm
);
2410 if (kvm
->arch
.migration_state
) {
2411 vfree(kvm
->arch
.migration_state
->pgste_bitmap
);
2412 kfree(kvm
->arch
.migration_state
);
2414 KVM_EVENT(3, "vm 0x%pK destroyed", kvm
);
2417 /* Section: vcpu related */
2418 static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu
*vcpu
)
2420 vcpu
->arch
.gmap
= gmap_create(current
->mm
, -1UL);
2421 if (!vcpu
->arch
.gmap
)
2423 vcpu
->arch
.gmap
->private = vcpu
->kvm
;
2428 static void sca_del_vcpu(struct kvm_vcpu
*vcpu
)
2430 if (!kvm_s390_use_sca_entries())
2432 read_lock(&vcpu
->kvm
->arch
.sca_lock
);
2433 if (vcpu
->kvm
->arch
.use_esca
) {
2434 struct esca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2436 clear_bit_inv(vcpu
->vcpu_id
, (unsigned long *) sca
->mcn
);
2437 sca
->cpu
[vcpu
->vcpu_id
].sda
= 0;
2439 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2441 clear_bit_inv(vcpu
->vcpu_id
, (unsigned long *) &sca
->mcn
);
2442 sca
->cpu
[vcpu
->vcpu_id
].sda
= 0;
2444 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
2447 static void sca_add_vcpu(struct kvm_vcpu
*vcpu
)
2449 if (!kvm_s390_use_sca_entries()) {
2450 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2452 /* we still need the basic sca for the ipte control */
2453 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
2454 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
;
2457 read_lock(&vcpu
->kvm
->arch
.sca_lock
);
2458 if (vcpu
->kvm
->arch
.use_esca
) {
2459 struct esca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2461 sca
->cpu
[vcpu
->vcpu_id
].sda
= (__u64
) vcpu
->arch
.sie_block
;
2462 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
2463 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
& ~0x3fU
;
2464 vcpu
->arch
.sie_block
->ecb2
|= ECB2_ESCA
;
2465 set_bit_inv(vcpu
->vcpu_id
, (unsigned long *) sca
->mcn
);
2467 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
2469 sca
->cpu
[vcpu
->vcpu_id
].sda
= (__u64
) vcpu
->arch
.sie_block
;
2470 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
2471 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
;
2472 set_bit_inv(vcpu
->vcpu_id
, (unsigned long *) &sca
->mcn
);
2474 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
2477 /* Basic SCA to Extended SCA data copy routines */
2478 static inline void sca_copy_entry(struct esca_entry
*d
, struct bsca_entry
*s
)
2481 d
->sigp_ctrl
.c
= s
->sigp_ctrl
.c
;
2482 d
->sigp_ctrl
.scn
= s
->sigp_ctrl
.scn
;
2485 static void sca_copy_b_to_e(struct esca_block
*d
, struct bsca_block
*s
)
2489 d
->ipte_control
= s
->ipte_control
;
2491 for (i
= 0; i
< KVM_S390_BSCA_CPU_SLOTS
; i
++)
2492 sca_copy_entry(&d
->cpu
[i
], &s
->cpu
[i
]);
2495 static int sca_switch_to_extended(struct kvm
*kvm
)
2497 struct bsca_block
*old_sca
= kvm
->arch
.sca
;
2498 struct esca_block
*new_sca
;
2499 struct kvm_vcpu
*vcpu
;
2500 unsigned int vcpu_idx
;
2503 new_sca
= alloc_pages_exact(sizeof(*new_sca
), GFP_KERNEL
|__GFP_ZERO
);
2507 scaoh
= (u32
)((u64
)(new_sca
) >> 32);
2508 scaol
= (u32
)(u64
)(new_sca
) & ~0x3fU
;
2510 kvm_s390_vcpu_block_all(kvm
);
2511 write_lock(&kvm
->arch
.sca_lock
);
2513 sca_copy_b_to_e(new_sca
, old_sca
);
2515 kvm_for_each_vcpu(vcpu_idx
, vcpu
, kvm
) {
2516 vcpu
->arch
.sie_block
->scaoh
= scaoh
;
2517 vcpu
->arch
.sie_block
->scaol
= scaol
;
2518 vcpu
->arch
.sie_block
->ecb2
|= ECB2_ESCA
;
2520 kvm
->arch
.sca
= new_sca
;
2521 kvm
->arch
.use_esca
= 1;
2523 write_unlock(&kvm
->arch
.sca_lock
);
2524 kvm_s390_vcpu_unblock_all(kvm
);
2526 free_page((unsigned long)old_sca
);
2528 VM_EVENT(kvm
, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
2529 old_sca
, kvm
->arch
.sca
);
2533 static int sca_can_add_vcpu(struct kvm
*kvm
, unsigned int id
)
2537 if (!kvm_s390_use_sca_entries()) {
2538 if (id
< KVM_MAX_VCPUS
)
2542 if (id
< KVM_S390_BSCA_CPU_SLOTS
)
2544 if (!sclp
.has_esca
|| !sclp
.has_64bscao
)
2547 mutex_lock(&kvm
->lock
);
2548 rc
= kvm
->arch
.use_esca
? 0 : sca_switch_to_extended(kvm
);
2549 mutex_unlock(&kvm
->lock
);
2551 return rc
== 0 && id
< KVM_S390_ESCA_CPU_SLOTS
;
2554 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
2556 vcpu
->arch
.pfault_token
= KVM_S390_PFAULT_TOKEN_INVALID
;
2557 kvm_clear_async_pf_completion_queue(vcpu
);
2558 vcpu
->run
->kvm_valid_regs
= KVM_SYNC_PREFIX
|
2564 kvm_s390_set_prefix(vcpu
, 0);
2565 if (test_kvm_facility(vcpu
->kvm
, 64))
2566 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_RICCB
;
2567 if (test_kvm_facility(vcpu
->kvm
, 82))
2568 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_BPBC
;
2569 if (test_kvm_facility(vcpu
->kvm
, 133))
2570 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_GSCB
;
2571 if (test_kvm_facility(vcpu
->kvm
, 156))
2572 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_ETOKEN
;
2573 /* fprs can be synchronized via vrs, even if the guest has no vx. With
2574 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
2577 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_VRS
;
2579 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_FPRS
;
2581 if (kvm_is_ucontrol(vcpu
->kvm
))
2582 return __kvm_ucontrol_vcpu_init(vcpu
);
2587 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2588 static void __start_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2590 WARN_ON_ONCE(vcpu
->arch
.cputm_start
!= 0);
2591 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
2592 vcpu
->arch
.cputm_start
= get_tod_clock_fast();
2593 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
2596 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2597 static void __stop_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2599 WARN_ON_ONCE(vcpu
->arch
.cputm_start
== 0);
2600 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
2601 vcpu
->arch
.sie_block
->cputm
-= get_tod_clock_fast() - vcpu
->arch
.cputm_start
;
2602 vcpu
->arch
.cputm_start
= 0;
2603 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
2606 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2607 static void __enable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2609 WARN_ON_ONCE(vcpu
->arch
.cputm_enabled
);
2610 vcpu
->arch
.cputm_enabled
= true;
2611 __start_cpu_timer_accounting(vcpu
);
2614 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
2615 static void __disable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2617 WARN_ON_ONCE(!vcpu
->arch
.cputm_enabled
);
2618 __stop_cpu_timer_accounting(vcpu
);
2619 vcpu
->arch
.cputm_enabled
= false;
2622 static void enable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2624 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2625 __enable_cpu_timer_accounting(vcpu
);
2629 static void disable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
2631 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2632 __disable_cpu_timer_accounting(vcpu
);
2636 /* set the cpu timer - may only be called from the VCPU thread itself */
2637 void kvm_s390_set_cpu_timer(struct kvm_vcpu
*vcpu
, __u64 cputm
)
2639 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2640 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
2641 if (vcpu
->arch
.cputm_enabled
)
2642 vcpu
->arch
.cputm_start
= get_tod_clock_fast();
2643 vcpu
->arch
.sie_block
->cputm
= cputm
;
2644 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
2648 /* update and get the cpu timer - can also be called from other VCPU threads */
2649 __u64
kvm_s390_get_cpu_timer(struct kvm_vcpu
*vcpu
)
2654 if (unlikely(!vcpu
->arch
.cputm_enabled
))
2655 return vcpu
->arch
.sie_block
->cputm
;
2657 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
2659 seq
= raw_read_seqcount(&vcpu
->arch
.cputm_seqcount
);
2661 * If the writer would ever execute a read in the critical
2662 * section, e.g. in irq context, we have a deadlock.
2664 WARN_ON_ONCE((seq
& 1) && smp_processor_id() == vcpu
->cpu
);
2665 value
= vcpu
->arch
.sie_block
->cputm
;
2666 /* if cputm_start is 0, accounting is being started/stopped */
2667 if (likely(vcpu
->arch
.cputm_start
))
2668 value
-= get_tod_clock_fast() - vcpu
->arch
.cputm_start
;
2669 } while (read_seqcount_retry(&vcpu
->arch
.cputm_seqcount
, seq
& ~1));
2674 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
2677 gmap_enable(vcpu
->arch
.enabled_gmap
);
2678 atomic_or(CPUSTAT_RUNNING
, &vcpu
->arch
.sie_block
->cpuflags
);
2679 if (vcpu
->arch
.cputm_enabled
&& !is_vcpu_idle(vcpu
))
2680 __start_cpu_timer_accounting(vcpu
);
2684 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
2687 if (vcpu
->arch
.cputm_enabled
&& !is_vcpu_idle(vcpu
))
2688 __stop_cpu_timer_accounting(vcpu
);
2689 atomic_andnot(CPUSTAT_RUNNING
, &vcpu
->arch
.sie_block
->cpuflags
);
2690 vcpu
->arch
.enabled_gmap
= gmap_get_enabled();
2691 gmap_disable(vcpu
->arch
.enabled_gmap
);
2695 static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu
*vcpu
)
2697 /* this equals initial cpu reset in pop, but we don't switch to ESA */
2698 vcpu
->arch
.sie_block
->gpsw
.mask
= 0UL;
2699 vcpu
->arch
.sie_block
->gpsw
.addr
= 0UL;
2700 kvm_s390_set_prefix(vcpu
, 0);
2701 kvm_s390_set_cpu_timer(vcpu
, 0);
2702 vcpu
->arch
.sie_block
->ckc
= 0UL;
2703 vcpu
->arch
.sie_block
->todpr
= 0;
2704 memset(vcpu
->arch
.sie_block
->gcr
, 0, 16 * sizeof(__u64
));
2705 vcpu
->arch
.sie_block
->gcr
[0] = 0xE0UL
;
2706 vcpu
->arch
.sie_block
->gcr
[14] = 0xC2000000UL
;
2707 /* make sure the new fpc will be lazily loaded */
2709 current
->thread
.fpu
.fpc
= 0;
2710 vcpu
->arch
.sie_block
->gbea
= 1;
2711 vcpu
->arch
.sie_block
->pp
= 0;
2712 vcpu
->arch
.sie_block
->fpf
&= ~FPF_BPBC
;
2713 vcpu
->arch
.pfault_token
= KVM_S390_PFAULT_TOKEN_INVALID
;
2714 kvm_clear_async_pf_completion_queue(vcpu
);
2715 if (!kvm_s390_user_cpu_state_ctrl(vcpu
->kvm
))
2716 kvm_s390_vcpu_stop(vcpu
);
2717 kvm_s390_clear_local_irqs(vcpu
);
2720 void kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
2722 mutex_lock(&vcpu
->kvm
->lock
);
2724 vcpu
->arch
.sie_block
->epoch
= vcpu
->kvm
->arch
.epoch
;
2725 vcpu
->arch
.sie_block
->epdx
= vcpu
->kvm
->arch
.epdx
;
2727 mutex_unlock(&vcpu
->kvm
->lock
);
2728 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
2729 vcpu
->arch
.gmap
= vcpu
->kvm
->arch
.gmap
;
2732 if (test_kvm_facility(vcpu
->kvm
, 74) || vcpu
->kvm
->arch
.user_instr0
)
2733 vcpu
->arch
.sie_block
->ictl
|= ICTL_OPEREXC
;
2734 /* make vcpu_load load the right gmap on the first trigger */
2735 vcpu
->arch
.enabled_gmap
= vcpu
->arch
.gmap
;
2738 static bool kvm_has_pckmo_subfunc(struct kvm
*kvm
, unsigned long nr
)
2740 if (test_bit_inv(nr
, (unsigned long *)&kvm
->arch
.model
.subfuncs
.pckmo
) &&
2741 test_bit_inv(nr
, (unsigned long *)&kvm_s390_available_subfunc
.pckmo
))
2746 static bool kvm_has_pckmo_ecc(struct kvm
*kvm
)
2748 /* At least one ECC subfunction must be present */
2749 return kvm_has_pckmo_subfunc(kvm
, 32) ||
2750 kvm_has_pckmo_subfunc(kvm
, 33) ||
2751 kvm_has_pckmo_subfunc(kvm
, 34) ||
2752 kvm_has_pckmo_subfunc(kvm
, 40) ||
2753 kvm_has_pckmo_subfunc(kvm
, 41);
2757 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu
*vcpu
)
2760 * If the AP instructions are not being interpreted and the MSAX3
2761 * facility is not configured for the guest, there is nothing to set up.
2763 if (!vcpu
->kvm
->arch
.crypto
.apie
&& !test_kvm_facility(vcpu
->kvm
, 76))
2766 vcpu
->arch
.sie_block
->crycbd
= vcpu
->kvm
->arch
.crypto
.crycbd
;
2767 vcpu
->arch
.sie_block
->ecb3
&= ~(ECB3_AES
| ECB3_DEA
);
2768 vcpu
->arch
.sie_block
->eca
&= ~ECA_APIE
;
2769 vcpu
->arch
.sie_block
->ecd
&= ~ECD_ECC
;
2771 if (vcpu
->kvm
->arch
.crypto
.apie
)
2772 vcpu
->arch
.sie_block
->eca
|= ECA_APIE
;
2774 /* Set up protected key support */
2775 if (vcpu
->kvm
->arch
.crypto
.aes_kw
) {
2776 vcpu
->arch
.sie_block
->ecb3
|= ECB3_AES
;
2777 /* ecc is also wrapped with AES key */
2778 if (kvm_has_pckmo_ecc(vcpu
->kvm
))
2779 vcpu
->arch
.sie_block
->ecd
|= ECD_ECC
;
2782 if (vcpu
->kvm
->arch
.crypto
.dea_kw
)
2783 vcpu
->arch
.sie_block
->ecb3
|= ECB3_DEA
;
2786 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu
*vcpu
)
2788 free_page(vcpu
->arch
.sie_block
->cbrlo
);
2789 vcpu
->arch
.sie_block
->cbrlo
= 0;
2792 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu
*vcpu
)
2794 vcpu
->arch
.sie_block
->cbrlo
= get_zeroed_page(GFP_KERNEL
);
2795 if (!vcpu
->arch
.sie_block
->cbrlo
)
2798 vcpu
->arch
.sie_block
->ecb2
&= ~ECB2_PFMFI
;
2802 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu
*vcpu
)
2804 struct kvm_s390_cpu_model
*model
= &vcpu
->kvm
->arch
.model
;
2806 vcpu
->arch
.sie_block
->ibc
= model
->ibc
;
2807 if (test_kvm_facility(vcpu
->kvm
, 7))
2808 vcpu
->arch
.sie_block
->fac
= (u32
)(u64
) model
->fac_list
;
2811 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
2815 atomic_set(&vcpu
->arch
.sie_block
->cpuflags
, CPUSTAT_ZARCH
|
2819 if (test_kvm_facility(vcpu
->kvm
, 78))
2820 atomic_or(CPUSTAT_GED2
, &vcpu
->arch
.sie_block
->cpuflags
);
2821 else if (test_kvm_facility(vcpu
->kvm
, 8))
2822 atomic_or(CPUSTAT_GED
, &vcpu
->arch
.sie_block
->cpuflags
);
2824 kvm_s390_vcpu_setup_model(vcpu
);
2826 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2827 if (MACHINE_HAS_ESOP
)
2828 vcpu
->arch
.sie_block
->ecb
|= ECB_HOSTPROTINT
;
2829 if (test_kvm_facility(vcpu
->kvm
, 9))
2830 vcpu
->arch
.sie_block
->ecb
|= ECB_SRSI
;
2831 if (test_kvm_facility(vcpu
->kvm
, 73))
2832 vcpu
->arch
.sie_block
->ecb
|= ECB_TE
;
2834 if (test_kvm_facility(vcpu
->kvm
, 8) && sclp
.has_pfmfi
)
2835 vcpu
->arch
.sie_block
->ecb2
|= ECB2_PFMFI
;
2836 if (test_kvm_facility(vcpu
->kvm
, 130))
2837 vcpu
->arch
.sie_block
->ecb2
|= ECB2_IEP
;
2838 vcpu
->arch
.sie_block
->eca
= ECA_MVPGI
| ECA_PROTEXCI
;
2840 vcpu
->arch
.sie_block
->eca
|= ECA_CEI
;
2842 vcpu
->arch
.sie_block
->eca
|= ECA_IB
;
2844 vcpu
->arch
.sie_block
->eca
|= ECA_SII
;
2845 if (sclp
.has_sigpif
)
2846 vcpu
->arch
.sie_block
->eca
|= ECA_SIGPI
;
2847 if (test_kvm_facility(vcpu
->kvm
, 129)) {
2848 vcpu
->arch
.sie_block
->eca
|= ECA_VX
;
2849 vcpu
->arch
.sie_block
->ecd
|= ECD_HOSTREGMGMT
;
2851 if (test_kvm_facility(vcpu
->kvm
, 139))
2852 vcpu
->arch
.sie_block
->ecd
|= ECD_MEF
;
2853 if (test_kvm_facility(vcpu
->kvm
, 156))
2854 vcpu
->arch
.sie_block
->ecd
|= ECD_ETOKENF
;
2855 vcpu
->arch
.sie_block
->sdnxo
= ((unsigned long) &vcpu
->run
->s
.regs
.sdnx
)
2857 vcpu
->arch
.sie_block
->riccbd
= (unsigned long) &vcpu
->run
->s
.regs
.riccb
;
2860 atomic_or(CPUSTAT_KSS
, &vcpu
->arch
.sie_block
->cpuflags
);
2862 vcpu
->arch
.sie_block
->ictl
|= ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
;
2864 if (vcpu
->kvm
->arch
.use_cmma
) {
2865 rc
= kvm_s390_vcpu_setup_cmma(vcpu
);
2869 hrtimer_init(&vcpu
->arch
.ckc_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2870 vcpu
->arch
.ckc_timer
.function
= kvm_s390_idle_wakeup
;
2872 kvm_s390_vcpu_crypto_setup(vcpu
);
2877 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
2880 struct kvm_vcpu
*vcpu
;
2881 struct sie_page
*sie_page
;
2884 if (!kvm_is_ucontrol(kvm
) && !sca_can_add_vcpu(kvm
, id
))
2889 vcpu
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
2893 BUILD_BUG_ON(sizeof(struct sie_page
) != 4096);
2894 sie_page
= (struct sie_page
*) get_zeroed_page(GFP_KERNEL
);
2898 vcpu
->arch
.sie_block
= &sie_page
->sie_block
;
2899 vcpu
->arch
.sie_block
->itdba
= (unsigned long) &sie_page
->itdb
;
2901 /* the real guest size will always be smaller than msl */
2902 vcpu
->arch
.sie_block
->mso
= 0;
2903 vcpu
->arch
.sie_block
->msl
= sclp
.hamax
;
2905 vcpu
->arch
.sie_block
->icpua
= id
;
2906 spin_lock_init(&vcpu
->arch
.local_int
.lock
);
2907 vcpu
->arch
.local_int
.float_int
= &kvm
->arch
.float_int
;
2908 vcpu
->arch
.local_int
.wq
= &vcpu
->wq
;
2909 vcpu
->arch
.local_int
.cpuflags
= &vcpu
->arch
.sie_block
->cpuflags
;
2910 seqcount_init(&vcpu
->arch
.cputm_seqcount
);
2912 rc
= kvm_vcpu_init(vcpu
, kvm
, id
);
2914 goto out_free_sie_block
;
2915 VM_EVENT(kvm
, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id
, vcpu
,
2916 vcpu
->arch
.sie_block
);
2917 trace_kvm_s390_create_vcpu(id
, vcpu
, vcpu
->arch
.sie_block
);
2921 free_page((unsigned long)(vcpu
->arch
.sie_block
));
2923 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
2928 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
2930 return kvm_s390_vcpu_has_irq(vcpu
, 0);
2933 bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu
*vcpu
)
2935 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PSTATE
);
2938 void kvm_s390_vcpu_block(struct kvm_vcpu
*vcpu
)
2940 atomic_or(PROG_BLOCK_SIE
, &vcpu
->arch
.sie_block
->prog20
);
2944 void kvm_s390_vcpu_unblock(struct kvm_vcpu
*vcpu
)
2946 atomic_andnot(PROG_BLOCK_SIE
, &vcpu
->arch
.sie_block
->prog20
);
2949 static void kvm_s390_vcpu_request(struct kvm_vcpu
*vcpu
)
2951 atomic_or(PROG_REQUEST
, &vcpu
->arch
.sie_block
->prog20
);
2955 bool kvm_s390_vcpu_sie_inhibited(struct kvm_vcpu
*vcpu
)
2957 return atomic_read(&vcpu
->arch
.sie_block
->prog20
) &
2958 (PROG_BLOCK_SIE
| PROG_REQUEST
);
2961 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu
*vcpu
)
2963 atomic_andnot(PROG_REQUEST
, &vcpu
->arch
.sie_block
->prog20
);
2967 * Kick a guest cpu out of (v)SIE and wait until (v)SIE is not running.
2968 * If the CPU is not running (e.g. waiting as idle) the function will
2969 * return immediately. */
2970 void exit_sie(struct kvm_vcpu
*vcpu
)
2972 atomic_or(CPUSTAT_STOP_INT
, &vcpu
->arch
.sie_block
->cpuflags
);
2973 kvm_s390_vsie_kick(vcpu
);
2974 while (vcpu
->arch
.sie_block
->prog0c
& PROG_IN_SIE
)
2978 /* Kick a guest cpu out of SIE to process a request synchronously */
2979 void kvm_s390_sync_request(int req
, struct kvm_vcpu
*vcpu
)
2981 kvm_make_request(req
, vcpu
);
2982 kvm_s390_vcpu_request(vcpu
);
2985 static void kvm_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
2988 struct kvm
*kvm
= gmap
->private;
2989 struct kvm_vcpu
*vcpu
;
2990 unsigned long prefix
;
2993 if (gmap_is_shadow(gmap
))
2995 if (start
>= 1UL << 31)
2996 /* We are only interested in prefix pages */
2998 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
2999 /* match against both prefix pages */
3000 prefix
= kvm_s390_get_prefix(vcpu
);
3001 if (prefix
<= end
&& start
<= prefix
+ 2*PAGE_SIZE
- 1) {
3002 VCPU_EVENT(vcpu
, 2, "gmap notifier for %lx-%lx",
3004 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD
, vcpu
);
3009 int kvm_arch_vcpu_should_kick(struct kvm_vcpu
*vcpu
)
3011 /* kvm common code refers to this, but never calls it */
3016 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu
*vcpu
,
3017 struct kvm_one_reg
*reg
)
3022 case KVM_REG_S390_TODPR
:
3023 r
= put_user(vcpu
->arch
.sie_block
->todpr
,
3024 (u32 __user
*)reg
->addr
);
3026 case KVM_REG_S390_EPOCHDIFF
:
3027 r
= put_user(vcpu
->arch
.sie_block
->epoch
,
3028 (u64 __user
*)reg
->addr
);
3030 case KVM_REG_S390_CPU_TIMER
:
3031 r
= put_user(kvm_s390_get_cpu_timer(vcpu
),
3032 (u64 __user
*)reg
->addr
);
3034 case KVM_REG_S390_CLOCK_COMP
:
3035 r
= put_user(vcpu
->arch
.sie_block
->ckc
,
3036 (u64 __user
*)reg
->addr
);
3038 case KVM_REG_S390_PFTOKEN
:
3039 r
= put_user(vcpu
->arch
.pfault_token
,
3040 (u64 __user
*)reg
->addr
);
3042 case KVM_REG_S390_PFCOMPARE
:
3043 r
= put_user(vcpu
->arch
.pfault_compare
,
3044 (u64 __user
*)reg
->addr
);
3046 case KVM_REG_S390_PFSELECT
:
3047 r
= put_user(vcpu
->arch
.pfault_select
,
3048 (u64 __user
*)reg
->addr
);
3050 case KVM_REG_S390_PP
:
3051 r
= put_user(vcpu
->arch
.sie_block
->pp
,
3052 (u64 __user
*)reg
->addr
);
3054 case KVM_REG_S390_GBEA
:
3055 r
= put_user(vcpu
->arch
.sie_block
->gbea
,
3056 (u64 __user
*)reg
->addr
);
3065 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu
*vcpu
,
3066 struct kvm_one_reg
*reg
)
3072 case KVM_REG_S390_TODPR
:
3073 r
= get_user(vcpu
->arch
.sie_block
->todpr
,
3074 (u32 __user
*)reg
->addr
);
3076 case KVM_REG_S390_EPOCHDIFF
:
3077 r
= get_user(vcpu
->arch
.sie_block
->epoch
,
3078 (u64 __user
*)reg
->addr
);
3080 case KVM_REG_S390_CPU_TIMER
:
3081 r
= get_user(val
, (u64 __user
*)reg
->addr
);
3083 kvm_s390_set_cpu_timer(vcpu
, val
);
3085 case KVM_REG_S390_CLOCK_COMP
:
3086 r
= get_user(vcpu
->arch
.sie_block
->ckc
,
3087 (u64 __user
*)reg
->addr
);
3089 case KVM_REG_S390_PFTOKEN
:
3090 r
= get_user(vcpu
->arch
.pfault_token
,
3091 (u64 __user
*)reg
->addr
);
3092 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
3093 kvm_clear_async_pf_completion_queue(vcpu
);
3095 case KVM_REG_S390_PFCOMPARE
:
3096 r
= get_user(vcpu
->arch
.pfault_compare
,
3097 (u64 __user
*)reg
->addr
);
3099 case KVM_REG_S390_PFSELECT
:
3100 r
= get_user(vcpu
->arch
.pfault_select
,
3101 (u64 __user
*)reg
->addr
);
3103 case KVM_REG_S390_PP
:
3104 r
= get_user(vcpu
->arch
.sie_block
->pp
,
3105 (u64 __user
*)reg
->addr
);
3107 case KVM_REG_S390_GBEA
:
3108 r
= get_user(vcpu
->arch
.sie_block
->gbea
,
3109 (u64 __user
*)reg
->addr
);
3118 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu
*vcpu
)
3120 kvm_s390_vcpu_initial_reset(vcpu
);
3124 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3126 memcpy(&vcpu
->run
->s
.regs
.gprs
, ®s
->gprs
, sizeof(regs
->gprs
));
3130 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
3132 memcpy(®s
->gprs
, &vcpu
->run
->s
.regs
.gprs
, sizeof(regs
->gprs
));
3136 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
3137 struct kvm_sregs
*sregs
)
3139 memcpy(&vcpu
->run
->s
.regs
.acrs
, &sregs
->acrs
, sizeof(sregs
->acrs
));
3140 memcpy(&vcpu
->arch
.sie_block
->gcr
, &sregs
->crs
, sizeof(sregs
->crs
));
3144 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
3145 struct kvm_sregs
*sregs
)
3147 memcpy(&sregs
->acrs
, &vcpu
->run
->s
.regs
.acrs
, sizeof(sregs
->acrs
));
3148 memcpy(&sregs
->crs
, &vcpu
->arch
.sie_block
->gcr
, sizeof(sregs
->crs
));
3152 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3154 if (test_fp_ctl(fpu
->fpc
))
3156 vcpu
->run
->s
.regs
.fpc
= fpu
->fpc
;
3158 convert_fp_to_vx((__vector128
*) vcpu
->run
->s
.regs
.vrs
,
3159 (freg_t
*) fpu
->fprs
);
3161 memcpy(vcpu
->run
->s
.regs
.fprs
, &fpu
->fprs
, sizeof(fpu
->fprs
));
3165 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
3167 /* make sure we have the latest values */
3170 convert_vx_to_fp((freg_t
*) fpu
->fprs
,
3171 (__vector128
*) vcpu
->run
->s
.regs
.vrs
);
3173 memcpy(fpu
->fprs
, vcpu
->run
->s
.regs
.fprs
, sizeof(fpu
->fprs
));
3174 fpu
->fpc
= vcpu
->run
->s
.regs
.fpc
;
3178 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu
*vcpu
, psw_t psw
)
3182 if (!is_vcpu_stopped(vcpu
))
3185 vcpu
->run
->psw_mask
= psw
.mask
;
3186 vcpu
->run
->psw_addr
= psw
.addr
;
3191 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
3192 struct kvm_translation
*tr
)
3194 return -EINVAL
; /* not implemented yet */
3197 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
3198 KVM_GUESTDBG_USE_HW_BP | \
3199 KVM_GUESTDBG_ENABLE)
3201 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
3202 struct kvm_guest_debug
*dbg
)
3206 vcpu
->guest_debug
= 0;
3207 kvm_s390_clear_bp_data(vcpu
);
3209 if (dbg
->control
& ~VALID_GUESTDBG_FLAGS
)
3211 if (!sclp
.has_gpere
)
3214 if (dbg
->control
& KVM_GUESTDBG_ENABLE
) {
3215 vcpu
->guest_debug
= dbg
->control
;
3216 /* enforce guest PER */
3217 atomic_or(CPUSTAT_P
, &vcpu
->arch
.sie_block
->cpuflags
);
3219 if (dbg
->control
& KVM_GUESTDBG_USE_HW_BP
)
3220 rc
= kvm_s390_import_bp_data(vcpu
, dbg
);
3222 atomic_andnot(CPUSTAT_P
, &vcpu
->arch
.sie_block
->cpuflags
);
3223 vcpu
->arch
.guestdbg
.last_bp
= 0;
3227 vcpu
->guest_debug
= 0;
3228 kvm_s390_clear_bp_data(vcpu
);
3229 atomic_andnot(CPUSTAT_P
, &vcpu
->arch
.sie_block
->cpuflags
);
3235 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
3236 struct kvm_mp_state
*mp_state
)
3238 /* CHECK_STOP and LOAD are not supported yet */
3239 return is_vcpu_stopped(vcpu
) ? KVM_MP_STATE_STOPPED
:
3240 KVM_MP_STATE_OPERATING
;
3243 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
3244 struct kvm_mp_state
*mp_state
)
3248 /* user space knows about this interface - let it control the state */
3249 vcpu
->kvm
->arch
.user_cpu_state_ctrl
= 1;
3251 switch (mp_state
->mp_state
) {
3252 case KVM_MP_STATE_STOPPED
:
3253 kvm_s390_vcpu_stop(vcpu
);
3255 case KVM_MP_STATE_OPERATING
:
3256 kvm_s390_vcpu_start(vcpu
);
3258 case KVM_MP_STATE_LOAD
:
3259 case KVM_MP_STATE_CHECK_STOP
:
3260 /* fall through - CHECK_STOP and LOAD are not supported yet */
3268 static bool ibs_enabled(struct kvm_vcpu
*vcpu
)
3270 return atomic_read(&vcpu
->arch
.sie_block
->cpuflags
) & CPUSTAT_IBS
;
3273 static int kvm_s390_handle_requests(struct kvm_vcpu
*vcpu
)
3276 kvm_s390_vcpu_request_handled(vcpu
);
3277 if (!kvm_request_pending(vcpu
))
3280 * We use MMU_RELOAD just to re-arm the ipte notifier for the
3281 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
3282 * This ensures that the ipte instruction for this request has
3283 * already finished. We might race against a second unmapper that
3284 * wants to set the blocking bit. Lets just retry the request loop.
3286 if (kvm_check_request(KVM_REQ_MMU_RELOAD
, vcpu
)) {
3288 rc
= gmap_mprotect_notify(vcpu
->arch
.gmap
,
3289 kvm_s390_get_prefix(vcpu
),
3290 PAGE_SIZE
* 2, PROT_WRITE
);
3292 kvm_make_request(KVM_REQ_MMU_RELOAD
, vcpu
);
3298 if (kvm_check_request(KVM_REQ_TLB_FLUSH
, vcpu
)) {
3299 vcpu
->arch
.sie_block
->ihcpu
= 0xffff;
3303 if (kvm_check_request(KVM_REQ_ENABLE_IBS
, vcpu
)) {
3304 if (!ibs_enabled(vcpu
)) {
3305 trace_kvm_s390_enable_disable_ibs(vcpu
->vcpu_id
, 1);
3306 atomic_or(CPUSTAT_IBS
,
3307 &vcpu
->arch
.sie_block
->cpuflags
);
3312 if (kvm_check_request(KVM_REQ_DISABLE_IBS
, vcpu
)) {
3313 if (ibs_enabled(vcpu
)) {
3314 trace_kvm_s390_enable_disable_ibs(vcpu
->vcpu_id
, 0);
3315 atomic_andnot(CPUSTAT_IBS
,
3316 &vcpu
->arch
.sie_block
->cpuflags
);
3321 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC
, vcpu
)) {
3322 vcpu
->arch
.sie_block
->ictl
|= ICTL_OPEREXC
;
3326 if (kvm_check_request(KVM_REQ_START_MIGRATION
, vcpu
)) {
3328 * Disable CMMA virtualization; we will emulate the ESSA
3329 * instruction manually, in order to provide additional
3330 * functionalities needed for live migration.
3332 vcpu
->arch
.sie_block
->ecb2
&= ~ECB2_CMMA
;
3336 if (kvm_check_request(KVM_REQ_STOP_MIGRATION
, vcpu
)) {
3338 * Re-enable CMMA virtualization if CMMA is available and
3341 if ((vcpu
->kvm
->arch
.use_cmma
) &&
3342 (vcpu
->kvm
->mm
->context
.use_cmma
))
3343 vcpu
->arch
.sie_block
->ecb2
|= ECB2_CMMA
;
3347 /* nothing to do, just clear the request */
3348 kvm_clear_request(KVM_REQ_UNHALT
, vcpu
);
3349 /* we left the vsie handler, nothing to do, just clear the request */
3350 kvm_clear_request(KVM_REQ_VSIE_RESTART
, vcpu
);
3355 void kvm_s390_set_tod_clock(struct kvm
*kvm
,
3356 const struct kvm_s390_vm_tod_clock
*gtod
)
3358 struct kvm_vcpu
*vcpu
;
3359 struct kvm_s390_tod_clock_ext htod
;
3362 mutex_lock(&kvm
->lock
);
3365 get_tod_clock_ext((char *)&htod
);
3367 kvm
->arch
.epoch
= gtod
->tod
- htod
.tod
;
3369 if (test_kvm_facility(kvm
, 139)) {
3370 kvm
->arch
.epdx
= gtod
->epoch_idx
- htod
.epoch_idx
;
3371 if (kvm
->arch
.epoch
> gtod
->tod
)
3372 kvm
->arch
.epdx
-= 1;
3375 kvm_s390_vcpu_block_all(kvm
);
3376 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
3377 vcpu
->arch
.sie_block
->epoch
= kvm
->arch
.epoch
;
3378 vcpu
->arch
.sie_block
->epdx
= kvm
->arch
.epdx
;
3381 kvm_s390_vcpu_unblock_all(kvm
);
3383 mutex_unlock(&kvm
->lock
);
3387 * kvm_arch_fault_in_page - fault-in guest page if necessary
3388 * @vcpu: The corresponding virtual cpu
3389 * @gpa: Guest physical address
3390 * @writable: Whether the page should be writable or not
3392 * Make sure that a guest page has been faulted-in on the host.
3394 * Return: Zero on success, negative error code otherwise.
3396 long kvm_arch_fault_in_page(struct kvm_vcpu
*vcpu
, gpa_t gpa
, int writable
)
3398 return gmap_fault(vcpu
->arch
.gmap
, gpa
,
3399 writable
? FAULT_FLAG_WRITE
: 0);
3402 static void __kvm_inject_pfault_token(struct kvm_vcpu
*vcpu
, bool start_token
,
3403 unsigned long token
)
3405 struct kvm_s390_interrupt inti
;
3406 struct kvm_s390_irq irq
;
3409 irq
.u
.ext
.ext_params2
= token
;
3410 irq
.type
= KVM_S390_INT_PFAULT_INIT
;
3411 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu
, &irq
));
3413 inti
.type
= KVM_S390_INT_PFAULT_DONE
;
3414 inti
.parm64
= token
;
3415 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu
->kvm
, &inti
));
3419 void kvm_arch_async_page_not_present(struct kvm_vcpu
*vcpu
,
3420 struct kvm_async_pf
*work
)
3422 trace_kvm_s390_pfault_init(vcpu
, work
->arch
.pfault_token
);
3423 __kvm_inject_pfault_token(vcpu
, true, work
->arch
.pfault_token
);
3426 void kvm_arch_async_page_present(struct kvm_vcpu
*vcpu
,
3427 struct kvm_async_pf
*work
)
3429 trace_kvm_s390_pfault_done(vcpu
, work
->arch
.pfault_token
);
3430 __kvm_inject_pfault_token(vcpu
, false, work
->arch
.pfault_token
);
3433 void kvm_arch_async_page_ready(struct kvm_vcpu
*vcpu
,
3434 struct kvm_async_pf
*work
)
3436 /* s390 will always inject the page directly */
3439 bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu
*vcpu
)
3442 * s390 will always inject the page directly,
3443 * but we still want check_async_completion to cleanup
3448 static int kvm_arch_setup_async_pf(struct kvm_vcpu
*vcpu
)
3451 struct kvm_arch_async_pf arch
;
3454 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
3456 if ((vcpu
->arch
.sie_block
->gpsw
.mask
& vcpu
->arch
.pfault_select
) !=
3457 vcpu
->arch
.pfault_compare
)
3459 if (psw_extint_disabled(vcpu
))
3461 if (kvm_s390_vcpu_has_irq(vcpu
, 0))
3463 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x200ul
))
3465 if (!vcpu
->arch
.gmap
->pfault_enabled
)
3468 hva
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(current
->thread
.gmap_addr
));
3469 hva
+= current
->thread
.gmap_addr
& ~PAGE_MASK
;
3470 if (read_guest_real(vcpu
, vcpu
->arch
.pfault_token
, &arch
.pfault_token
, 8))
3473 rc
= kvm_setup_async_pf(vcpu
, current
->thread
.gmap_addr
, hva
, &arch
);
3477 static int vcpu_pre_run(struct kvm_vcpu
*vcpu
)
3482 * On s390 notifications for arriving pages will be delivered directly
3483 * to the guest but the house keeping for completed pfaults is
3484 * handled outside the worker.
3486 kvm_check_async_pf_completion(vcpu
);
3488 vcpu
->arch
.sie_block
->gg14
= vcpu
->run
->s
.regs
.gprs
[14];
3489 vcpu
->arch
.sie_block
->gg15
= vcpu
->run
->s
.regs
.gprs
[15];
3494 if (test_cpu_flag(CIF_MCCK_PENDING
))
3497 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
3498 rc
= kvm_s390_deliver_pending_interrupts(vcpu
);
3503 rc
= kvm_s390_handle_requests(vcpu
);
3507 if (guestdbg_enabled(vcpu
)) {
3508 kvm_s390_backup_guest_per_regs(vcpu
);
3509 kvm_s390_patch_guest_per_regs(vcpu
);
3512 vcpu
->arch
.sie_block
->icptcode
= 0;
3513 cpuflags
= atomic_read(&vcpu
->arch
.sie_block
->cpuflags
);
3514 VCPU_EVENT(vcpu
, 6, "entering sie flags %x", cpuflags
);
3515 trace_kvm_s390_sie_enter(vcpu
, cpuflags
);
3520 static int vcpu_post_run_fault_in_sie(struct kvm_vcpu
*vcpu
)
3522 struct kvm_s390_pgm_info pgm_info
= {
3523 .code
= PGM_ADDRESSING
,
3528 VCPU_EVENT(vcpu
, 3, "%s", "fault in sie instruction");
3529 trace_kvm_s390_sie_fault(vcpu
);
3532 * We want to inject an addressing exception, which is defined as a
3533 * suppressing or terminating exception. However, since we came here
3534 * by a DAT access exception, the PSW still points to the faulting
3535 * instruction since DAT exceptions are nullifying. So we've got
3536 * to look up the current opcode to get the length of the instruction
3537 * to be able to forward the PSW.
3539 rc
= read_guest_instr(vcpu
, vcpu
->arch
.sie_block
->gpsw
.addr
, &opcode
, 1);
3540 ilen
= insn_length(opcode
);
3544 /* Instruction-Fetching Exceptions - we can't detect the ilen.
3545 * Forward by arbitrary ilc, injection will take care of
3546 * nullification if necessary.
3548 pgm_info
= vcpu
->arch
.pgm
;
3551 pgm_info
.flags
= ilen
| KVM_S390_PGM_FLAGS_ILC_VALID
;
3552 kvm_s390_forward_psw(vcpu
, ilen
);
3553 return kvm_s390_inject_prog_irq(vcpu
, &pgm_info
);
3556 static int vcpu_post_run(struct kvm_vcpu
*vcpu
, int exit_reason
)
3558 struct mcck_volatile_info
*mcck_info
;
3559 struct sie_page
*sie_page
;
3561 VCPU_EVENT(vcpu
, 6, "exit sie icptcode %d",
3562 vcpu
->arch
.sie_block
->icptcode
);
3563 trace_kvm_s390_sie_exit(vcpu
, vcpu
->arch
.sie_block
->icptcode
);
3565 if (guestdbg_enabled(vcpu
))
3566 kvm_s390_restore_guest_per_regs(vcpu
);
3568 vcpu
->run
->s
.regs
.gprs
[14] = vcpu
->arch
.sie_block
->gg14
;
3569 vcpu
->run
->s
.regs
.gprs
[15] = vcpu
->arch
.sie_block
->gg15
;
3571 if (exit_reason
== -EINTR
) {
3572 VCPU_EVENT(vcpu
, 3, "%s", "machine check");
3573 sie_page
= container_of(vcpu
->arch
.sie_block
,
3574 struct sie_page
, sie_block
);
3575 mcck_info
= &sie_page
->mcck_info
;
3576 kvm_s390_reinject_machine_check(vcpu
, mcck_info
);
3580 if (vcpu
->arch
.sie_block
->icptcode
> 0) {
3581 int rc
= kvm_handle_sie_intercept(vcpu
);
3583 if (rc
!= -EOPNOTSUPP
)
3585 vcpu
->run
->exit_reason
= KVM_EXIT_S390_SIEIC
;
3586 vcpu
->run
->s390_sieic
.icptcode
= vcpu
->arch
.sie_block
->icptcode
;
3587 vcpu
->run
->s390_sieic
.ipa
= vcpu
->arch
.sie_block
->ipa
;
3588 vcpu
->run
->s390_sieic
.ipb
= vcpu
->arch
.sie_block
->ipb
;
3590 } else if (exit_reason
!= -EFAULT
) {
3591 vcpu
->stat
.exit_null
++;
3593 } else if (kvm_is_ucontrol(vcpu
->kvm
)) {
3594 vcpu
->run
->exit_reason
= KVM_EXIT_S390_UCONTROL
;
3595 vcpu
->run
->s390_ucontrol
.trans_exc_code
=
3596 current
->thread
.gmap_addr
;
3597 vcpu
->run
->s390_ucontrol
.pgm_code
= 0x10;
3599 } else if (current
->thread
.gmap_pfault
) {
3600 trace_kvm_s390_major_guest_pfault(vcpu
);
3601 current
->thread
.gmap_pfault
= 0;
3602 if (kvm_arch_setup_async_pf(vcpu
))
3604 return kvm_arch_fault_in_page(vcpu
, current
->thread
.gmap_addr
, 1);
3606 return vcpu_post_run_fault_in_sie(vcpu
);
3609 static int __vcpu_run(struct kvm_vcpu
*vcpu
)
3611 int rc
, exit_reason
;
3614 * We try to hold kvm->srcu during most of vcpu_run (except when run-
3615 * ning the guest), so that memslots (and other stuff) are protected
3617 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3620 rc
= vcpu_pre_run(vcpu
);
3624 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
3626 * As PF_VCPU will be used in fault handler, between
3627 * guest_enter and guest_exit should be no uaccess.
3629 local_irq_disable();
3630 guest_enter_irqoff();
3631 __disable_cpu_timer_accounting(vcpu
);
3633 exit_reason
= sie64a(vcpu
->arch
.sie_block
,
3634 vcpu
->run
->s
.regs
.gprs
);
3635 local_irq_disable();
3636 __enable_cpu_timer_accounting(vcpu
);
3637 guest_exit_irqoff();
3639 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3641 rc
= vcpu_post_run(vcpu
, exit_reason
);
3642 } while (!signal_pending(current
) && !guestdbg_exit_pending(vcpu
) && !rc
);
3644 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
3648 static void sync_regs(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3650 struct runtime_instr_cb
*riccb
;
3653 riccb
= (struct runtime_instr_cb
*) &kvm_run
->s
.regs
.riccb
;
3654 gscb
= (struct gs_cb
*) &kvm_run
->s
.regs
.gscb
;
3655 vcpu
->arch
.sie_block
->gpsw
.mask
= kvm_run
->psw_mask
;
3656 vcpu
->arch
.sie_block
->gpsw
.addr
= kvm_run
->psw_addr
;
3657 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_PREFIX
)
3658 kvm_s390_set_prefix(vcpu
, kvm_run
->s
.regs
.prefix
);
3659 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_CRS
) {
3660 memcpy(&vcpu
->arch
.sie_block
->gcr
, &kvm_run
->s
.regs
.crs
, 128);
3661 /* some control register changes require a tlb flush */
3662 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
3664 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_ARCH0
) {
3665 kvm_s390_set_cpu_timer(vcpu
, kvm_run
->s
.regs
.cputm
);
3666 vcpu
->arch
.sie_block
->ckc
= kvm_run
->s
.regs
.ckc
;
3667 vcpu
->arch
.sie_block
->todpr
= kvm_run
->s
.regs
.todpr
;
3668 vcpu
->arch
.sie_block
->pp
= kvm_run
->s
.regs
.pp
;
3669 vcpu
->arch
.sie_block
->gbea
= kvm_run
->s
.regs
.gbea
;
3671 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_PFAULT
) {
3672 vcpu
->arch
.pfault_token
= kvm_run
->s
.regs
.pft
;
3673 vcpu
->arch
.pfault_select
= kvm_run
->s
.regs
.pfs
;
3674 vcpu
->arch
.pfault_compare
= kvm_run
->s
.regs
.pfc
;
3675 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
3676 kvm_clear_async_pf_completion_queue(vcpu
);
3679 * If userspace sets the riccb (e.g. after migration) to a valid state,
3680 * we should enable RI here instead of doing the lazy enablement.
3682 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_RICCB
) &&
3683 test_kvm_facility(vcpu
->kvm
, 64) &&
3685 !(vcpu
->arch
.sie_block
->ecb3
& ECB3_RI
)) {
3686 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: RI (sync_regs)");
3687 vcpu
->arch
.sie_block
->ecb3
|= ECB3_RI
;
3690 * If userspace sets the gscb (e.g. after migration) to non-zero,
3691 * we should enable GS here instead of doing the lazy enablement.
3693 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_GSCB
) &&
3694 test_kvm_facility(vcpu
->kvm
, 133) &&
3696 !vcpu
->arch
.gs_enabled
) {
3697 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: GS (sync_regs)");
3698 vcpu
->arch
.sie_block
->ecb
|= ECB_GS
;
3699 vcpu
->arch
.sie_block
->ecd
|= ECD_HOSTREGMGMT
;
3700 vcpu
->arch
.gs_enabled
= 1;
3702 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_BPBC
) &&
3703 test_kvm_facility(vcpu
->kvm
, 82)) {
3704 vcpu
->arch
.sie_block
->fpf
&= ~FPF_BPBC
;
3705 vcpu
->arch
.sie_block
->fpf
|= kvm_run
->s
.regs
.bpbc
? FPF_BPBC
: 0;
3707 save_access_regs(vcpu
->arch
.host_acrs
);
3708 restore_access_regs(vcpu
->run
->s
.regs
.acrs
);
3709 /* save host (userspace) fprs/vrs */
3711 vcpu
->arch
.host_fpregs
.fpc
= current
->thread
.fpu
.fpc
;
3712 vcpu
->arch
.host_fpregs
.regs
= current
->thread
.fpu
.regs
;
3714 current
->thread
.fpu
.regs
= vcpu
->run
->s
.regs
.vrs
;
3716 current
->thread
.fpu
.regs
= vcpu
->run
->s
.regs
.fprs
;
3717 current
->thread
.fpu
.fpc
= vcpu
->run
->s
.regs
.fpc
;
3718 if (test_fp_ctl(current
->thread
.fpu
.fpc
))
3719 /* User space provided an invalid FPC, let's clear it */
3720 current
->thread
.fpu
.fpc
= 0;
3721 if (MACHINE_HAS_GS
) {
3723 __ctl_set_bit(2, 4);
3724 if (current
->thread
.gs_cb
) {
3725 vcpu
->arch
.host_gscb
= current
->thread
.gs_cb
;
3726 save_gs_cb(vcpu
->arch
.host_gscb
);
3728 if (vcpu
->arch
.gs_enabled
) {
3729 current
->thread
.gs_cb
= (struct gs_cb
*)
3730 &vcpu
->run
->s
.regs
.gscb
;
3731 restore_gs_cb(current
->thread
.gs_cb
);
3735 /* SIE will load etoken directly from SDNX and therefore kvm_run */
3737 kvm_run
->kvm_dirty_regs
= 0;
3740 static void store_regs(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3742 kvm_run
->psw_mask
= vcpu
->arch
.sie_block
->gpsw
.mask
;
3743 kvm_run
->psw_addr
= vcpu
->arch
.sie_block
->gpsw
.addr
;
3744 kvm_run
->s
.regs
.prefix
= kvm_s390_get_prefix(vcpu
);
3745 memcpy(&kvm_run
->s
.regs
.crs
, &vcpu
->arch
.sie_block
->gcr
, 128);
3746 kvm_run
->s
.regs
.cputm
= kvm_s390_get_cpu_timer(vcpu
);
3747 kvm_run
->s
.regs
.ckc
= vcpu
->arch
.sie_block
->ckc
;
3748 kvm_run
->s
.regs
.todpr
= vcpu
->arch
.sie_block
->todpr
;
3749 kvm_run
->s
.regs
.pp
= vcpu
->arch
.sie_block
->pp
;
3750 kvm_run
->s
.regs
.gbea
= vcpu
->arch
.sie_block
->gbea
;
3751 kvm_run
->s
.regs
.pft
= vcpu
->arch
.pfault_token
;
3752 kvm_run
->s
.regs
.pfs
= vcpu
->arch
.pfault_select
;
3753 kvm_run
->s
.regs
.pfc
= vcpu
->arch
.pfault_compare
;
3754 kvm_run
->s
.regs
.bpbc
= (vcpu
->arch
.sie_block
->fpf
& FPF_BPBC
) == FPF_BPBC
;
3755 save_access_regs(vcpu
->run
->s
.regs
.acrs
);
3756 restore_access_regs(vcpu
->arch
.host_acrs
);
3757 /* Save guest register state */
3759 vcpu
->run
->s
.regs
.fpc
= current
->thread
.fpu
.fpc
;
3760 /* Restore will be done lazily at return */
3761 current
->thread
.fpu
.fpc
= vcpu
->arch
.host_fpregs
.fpc
;
3762 current
->thread
.fpu
.regs
= vcpu
->arch
.host_fpregs
.regs
;
3763 if (MACHINE_HAS_GS
) {
3764 __ctl_set_bit(2, 4);
3765 if (vcpu
->arch
.gs_enabled
)
3766 save_gs_cb(current
->thread
.gs_cb
);
3768 current
->thread
.gs_cb
= vcpu
->arch
.host_gscb
;
3769 restore_gs_cb(vcpu
->arch
.host_gscb
);
3771 if (!vcpu
->arch
.host_gscb
)
3772 __ctl_clear_bit(2, 4);
3773 vcpu
->arch
.host_gscb
= NULL
;
3775 /* SIE will save etoken directly into SDNX and therefore kvm_run */
3778 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
3782 if (kvm_run
->immediate_exit
)
3785 if (guestdbg_exit_pending(vcpu
)) {
3786 kvm_s390_prepare_debug_exit(vcpu
);
3790 kvm_sigset_activate(vcpu
);
3792 if (!kvm_s390_user_cpu_state_ctrl(vcpu
->kvm
)) {
3793 kvm_s390_vcpu_start(vcpu
);
3794 } else if (is_vcpu_stopped(vcpu
)) {
3795 pr_err_ratelimited("can't run stopped vcpu %d\n",
3800 sync_regs(vcpu
, kvm_run
);
3801 enable_cpu_timer_accounting(vcpu
);
3804 rc
= __vcpu_run(vcpu
);
3806 if (signal_pending(current
) && !rc
) {
3807 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
3811 if (guestdbg_exit_pending(vcpu
) && !rc
) {
3812 kvm_s390_prepare_debug_exit(vcpu
);
3816 if (rc
== -EREMOTE
) {
3817 /* userspace support is needed, kvm_run has been prepared */
3821 disable_cpu_timer_accounting(vcpu
);
3822 store_regs(vcpu
, kvm_run
);
3824 kvm_sigset_deactivate(vcpu
);
3826 vcpu
->stat
.exit_userspace
++;
3831 * store status at address
3832 * we use have two special cases:
3833 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
3834 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
3836 int kvm_s390_store_status_unloaded(struct kvm_vcpu
*vcpu
, unsigned long gpa
)
3838 unsigned char archmode
= 1;
3839 freg_t fprs
[NUM_FPRS
];
3844 px
= kvm_s390_get_prefix(vcpu
);
3845 if (gpa
== KVM_S390_STORE_STATUS_NOADDR
) {
3846 if (write_guest_abs(vcpu
, 163, &archmode
, 1))
3849 } else if (gpa
== KVM_S390_STORE_STATUS_PREFIXED
) {
3850 if (write_guest_real(vcpu
, 163, &archmode
, 1))
3854 gpa
-= __LC_FPREGS_SAVE_AREA
;
3856 /* manually convert vector registers if necessary */
3857 if (MACHINE_HAS_VX
) {
3858 convert_vx_to_fp(fprs
, (__vector128
*) vcpu
->run
->s
.regs
.vrs
);
3859 rc
= write_guest_abs(vcpu
, gpa
+ __LC_FPREGS_SAVE_AREA
,
3862 rc
= write_guest_abs(vcpu
, gpa
+ __LC_FPREGS_SAVE_AREA
,
3863 vcpu
->run
->s
.regs
.fprs
, 128);
3865 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_GPREGS_SAVE_AREA
,
3866 vcpu
->run
->s
.regs
.gprs
, 128);
3867 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_PSW_SAVE_AREA
,
3868 &vcpu
->arch
.sie_block
->gpsw
, 16);
3869 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_PREFIX_SAVE_AREA
,
3871 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_FP_CREG_SAVE_AREA
,
3872 &vcpu
->run
->s
.regs
.fpc
, 4);
3873 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_TOD_PROGREG_SAVE_AREA
,
3874 &vcpu
->arch
.sie_block
->todpr
, 4);
3875 cputm
= kvm_s390_get_cpu_timer(vcpu
);
3876 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CPU_TIMER_SAVE_AREA
,
3878 clkcomp
= vcpu
->arch
.sie_block
->ckc
>> 8;
3879 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CLOCK_COMP_SAVE_AREA
,
3881 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_AREGS_SAVE_AREA
,
3882 &vcpu
->run
->s
.regs
.acrs
, 64);
3883 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CREGS_SAVE_AREA
,
3884 &vcpu
->arch
.sie_block
->gcr
, 128);
3885 return rc
? -EFAULT
: 0;
3888 int kvm_s390_vcpu_store_status(struct kvm_vcpu
*vcpu
, unsigned long addr
)
3891 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
3892 * switch in the run ioctl. Let's update our copies before we save
3893 * it into the save area
3896 vcpu
->run
->s
.regs
.fpc
= current
->thread
.fpu
.fpc
;
3897 save_access_regs(vcpu
->run
->s
.regs
.acrs
);
3899 return kvm_s390_store_status_unloaded(vcpu
, addr
);
3902 static void __disable_ibs_on_vcpu(struct kvm_vcpu
*vcpu
)
3904 kvm_check_request(KVM_REQ_ENABLE_IBS
, vcpu
);
3905 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS
, vcpu
);
3908 static void __disable_ibs_on_all_vcpus(struct kvm
*kvm
)
3911 struct kvm_vcpu
*vcpu
;
3913 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
3914 __disable_ibs_on_vcpu(vcpu
);
3918 static void __enable_ibs_on_vcpu(struct kvm_vcpu
*vcpu
)
3922 kvm_check_request(KVM_REQ_DISABLE_IBS
, vcpu
);
3923 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS
, vcpu
);
3926 void kvm_s390_vcpu_start(struct kvm_vcpu
*vcpu
)
3928 int i
, online_vcpus
, started_vcpus
= 0;
3930 if (!is_vcpu_stopped(vcpu
))
3933 trace_kvm_s390_vcpu_start_stop(vcpu
->vcpu_id
, 1);
3934 /* Only one cpu at a time may enter/leave the STOPPED state. */
3935 spin_lock(&vcpu
->kvm
->arch
.start_stop_lock
);
3936 online_vcpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
3938 for (i
= 0; i
< online_vcpus
; i
++) {
3939 if (!is_vcpu_stopped(vcpu
->kvm
->vcpus
[i
]))
3943 if (started_vcpus
== 0) {
3944 /* we're the only active VCPU -> speed it up */
3945 __enable_ibs_on_vcpu(vcpu
);
3946 } else if (started_vcpus
== 1) {
3948 * As we are starting a second VCPU, we have to disable
3949 * the IBS facility on all VCPUs to remove potentially
3950 * oustanding ENABLE requests.
3952 __disable_ibs_on_all_vcpus(vcpu
->kvm
);
3955 atomic_andnot(CPUSTAT_STOPPED
, &vcpu
->arch
.sie_block
->cpuflags
);
3957 * Another VCPU might have used IBS while we were offline.
3958 * Let's play safe and flush the VCPU at startup.
3960 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
3961 spin_unlock(&vcpu
->kvm
->arch
.start_stop_lock
);
3965 void kvm_s390_vcpu_stop(struct kvm_vcpu
*vcpu
)
3967 int i
, online_vcpus
, started_vcpus
= 0;
3968 struct kvm_vcpu
*started_vcpu
= NULL
;
3970 if (is_vcpu_stopped(vcpu
))
3973 trace_kvm_s390_vcpu_start_stop(vcpu
->vcpu_id
, 0);
3974 /* Only one cpu at a time may enter/leave the STOPPED state. */
3975 spin_lock(&vcpu
->kvm
->arch
.start_stop_lock
);
3976 online_vcpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
3978 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
3979 kvm_s390_clear_stop_irq(vcpu
);
3981 atomic_or(CPUSTAT_STOPPED
, &vcpu
->arch
.sie_block
->cpuflags
);
3982 __disable_ibs_on_vcpu(vcpu
);
3984 for (i
= 0; i
< online_vcpus
; i
++) {
3985 if (!is_vcpu_stopped(vcpu
->kvm
->vcpus
[i
])) {
3987 started_vcpu
= vcpu
->kvm
->vcpus
[i
];
3991 if (started_vcpus
== 1) {
3993 * As we only have one VCPU left, we want to enable the
3994 * IBS facility for that VCPU to speed it up.
3996 __enable_ibs_on_vcpu(started_vcpu
);
3999 spin_unlock(&vcpu
->kvm
->arch
.start_stop_lock
);
4003 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu
*vcpu
,
4004 struct kvm_enable_cap
*cap
)
4012 case KVM_CAP_S390_CSS_SUPPORT
:
4013 if (!vcpu
->kvm
->arch
.css_support
) {
4014 vcpu
->kvm
->arch
.css_support
= 1;
4015 VM_EVENT(vcpu
->kvm
, 3, "%s", "ENABLE: CSS support");
4016 trace_kvm_s390_enable_css(vcpu
->kvm
);
4027 static long kvm_s390_guest_mem_op(struct kvm_vcpu
*vcpu
,
4028 struct kvm_s390_mem_op
*mop
)
4030 void __user
*uaddr
= (void __user
*)mop
->buf
;
4031 void *tmpbuf
= NULL
;
4033 const u64 supported_flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
4034 | KVM_S390_MEMOP_F_CHECK_ONLY
;
4036 if (mop
->flags
& ~supported_flags
)
4039 if (mop
->size
> MEM_OP_MAX_SIZE
)
4042 if (!(mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
)) {
4043 tmpbuf
= vmalloc(mop
->size
);
4048 srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4051 case KVM_S390_MEMOP_LOGICAL_READ
:
4052 if (mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
) {
4053 r
= check_gva_range(vcpu
, mop
->gaddr
, mop
->ar
,
4054 mop
->size
, GACC_FETCH
);
4057 r
= read_guest(vcpu
, mop
->gaddr
, mop
->ar
, tmpbuf
, mop
->size
);
4059 if (copy_to_user(uaddr
, tmpbuf
, mop
->size
))
4063 case KVM_S390_MEMOP_LOGICAL_WRITE
:
4064 if (mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
) {
4065 r
= check_gva_range(vcpu
, mop
->gaddr
, mop
->ar
,
4066 mop
->size
, GACC_STORE
);
4069 if (copy_from_user(tmpbuf
, uaddr
, mop
->size
)) {
4073 r
= write_guest(vcpu
, mop
->gaddr
, mop
->ar
, tmpbuf
, mop
->size
);
4079 srcu_read_unlock(&vcpu
->kvm
->srcu
, srcu_idx
);
4081 if (r
> 0 && (mop
->flags
& KVM_S390_MEMOP_F_INJECT_EXCEPTION
) != 0)
4082 kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
4088 long kvm_arch_vcpu_ioctl(struct file
*filp
,
4089 unsigned int ioctl
, unsigned long arg
)
4091 struct kvm_vcpu
*vcpu
= filp
->private_data
;
4092 void __user
*argp
= (void __user
*)arg
;
4097 case KVM_S390_IRQ
: {
4098 struct kvm_s390_irq s390irq
;
4101 if (copy_from_user(&s390irq
, argp
, sizeof(s390irq
)))
4103 r
= kvm_s390_inject_vcpu(vcpu
, &s390irq
);
4106 case KVM_S390_INTERRUPT
: {
4107 struct kvm_s390_interrupt s390int
;
4108 struct kvm_s390_irq s390irq
;
4111 if (copy_from_user(&s390int
, argp
, sizeof(s390int
)))
4113 if (s390int_to_s390irq(&s390int
, &s390irq
))
4115 r
= kvm_s390_inject_vcpu(vcpu
, &s390irq
);
4118 case KVM_S390_STORE_STATUS
:
4119 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
4120 r
= kvm_s390_vcpu_store_status(vcpu
, arg
);
4121 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
4123 case KVM_S390_SET_INITIAL_PSW
: {
4127 if (copy_from_user(&psw
, argp
, sizeof(psw
)))
4129 r
= kvm_arch_vcpu_ioctl_set_initial_psw(vcpu
, psw
);
4132 case KVM_S390_INITIAL_RESET
:
4133 r
= kvm_arch_vcpu_ioctl_initial_reset(vcpu
);
4135 case KVM_SET_ONE_REG
:
4136 case KVM_GET_ONE_REG
: {
4137 struct kvm_one_reg reg
;
4139 if (copy_from_user(®
, argp
, sizeof(reg
)))
4141 if (ioctl
== KVM_SET_ONE_REG
)
4142 r
= kvm_arch_vcpu_ioctl_set_one_reg(vcpu
, ®
);
4144 r
= kvm_arch_vcpu_ioctl_get_one_reg(vcpu
, ®
);
4147 #ifdef CONFIG_KVM_S390_UCONTROL
4148 case KVM_S390_UCAS_MAP
: {
4149 struct kvm_s390_ucas_mapping ucasmap
;
4151 if (copy_from_user(&ucasmap
, argp
, sizeof(ucasmap
))) {
4156 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
4161 r
= gmap_map_segment(vcpu
->arch
.gmap
, ucasmap
.user_addr
,
4162 ucasmap
.vcpu_addr
, ucasmap
.length
);
4165 case KVM_S390_UCAS_UNMAP
: {
4166 struct kvm_s390_ucas_mapping ucasmap
;
4168 if (copy_from_user(&ucasmap
, argp
, sizeof(ucasmap
))) {
4173 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
4178 r
= gmap_unmap_segment(vcpu
->arch
.gmap
, ucasmap
.vcpu_addr
,
4183 case KVM_S390_VCPU_FAULT
: {
4184 r
= gmap_fault(vcpu
->arch
.gmap
, arg
, 0);
4187 case KVM_ENABLE_CAP
:
4189 struct kvm_enable_cap cap
;
4191 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
4193 r
= kvm_vcpu_ioctl_enable_cap(vcpu
, &cap
);
4196 case KVM_S390_MEM_OP
: {
4197 struct kvm_s390_mem_op mem_op
;
4199 if (copy_from_user(&mem_op
, argp
, sizeof(mem_op
)) == 0)
4200 r
= kvm_s390_guest_mem_op(vcpu
, &mem_op
);
4205 case KVM_S390_SET_IRQ_STATE
: {
4206 struct kvm_s390_irq_state irq_state
;
4209 if (copy_from_user(&irq_state
, argp
, sizeof(irq_state
)))
4211 if (irq_state
.len
> VCPU_IRQS_MAX_BUF
||
4212 irq_state
.len
== 0 ||
4213 irq_state
.len
% sizeof(struct kvm_s390_irq
) > 0) {
4217 /* do not use irq_state.flags, it will break old QEMUs */
4218 r
= kvm_s390_set_irq_state(vcpu
,
4219 (void __user
*) irq_state
.buf
,
4223 case KVM_S390_GET_IRQ_STATE
: {
4224 struct kvm_s390_irq_state irq_state
;
4227 if (copy_from_user(&irq_state
, argp
, sizeof(irq_state
)))
4229 if (irq_state
.len
== 0) {
4233 /* do not use irq_state.flags, it will break old QEMUs */
4234 r
= kvm_s390_get_irq_state(vcpu
,
4235 (__u8 __user
*) irq_state
.buf
,
4245 int kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
4247 #ifdef CONFIG_KVM_S390_UCONTROL
4248 if ((vmf
->pgoff
== KVM_S390_SIE_PAGE_OFFSET
)
4249 && (kvm_is_ucontrol(vcpu
->kvm
))) {
4250 vmf
->page
= virt_to_page(vcpu
->arch
.sie_block
);
4251 get_page(vmf
->page
);
4255 return VM_FAULT_SIGBUS
;
4258 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
4259 unsigned long npages
)
4264 /* Section: memory related */
4265 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
4266 struct kvm_memory_slot
*memslot
,
4267 const struct kvm_userspace_memory_region
*mem
,
4268 enum kvm_mr_change change
)
4270 /* A few sanity checks. We can have memory slots which have to be
4271 located/ended at a segment boundary (1MB). The memory in userland is
4272 ok to be fragmented into various different vmas. It is okay to mmap()
4273 and munmap() stuff in this slot after doing this call at any time */
4275 if (mem
->userspace_addr
& 0xffffful
)
4278 if (mem
->memory_size
& 0xffffful
)
4281 if (mem
->guest_phys_addr
+ mem
->memory_size
> kvm
->arch
.mem_limit
)
4287 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
4288 const struct kvm_userspace_memory_region
*mem
,
4289 const struct kvm_memory_slot
*old
,
4290 const struct kvm_memory_slot
*new,
4291 enum kvm_mr_change change
)
4295 /* If the basics of the memslot do not change, we do not want
4296 * to update the gmap. Every update causes several unnecessary
4297 * segment translation exceptions. This is usually handled just
4298 * fine by the normal fault handler + gmap, but it will also
4299 * cause faults on the prefix page of running guest CPUs.
4301 if (old
->userspace_addr
== mem
->userspace_addr
&&
4302 old
->base_gfn
* PAGE_SIZE
== mem
->guest_phys_addr
&&
4303 old
->npages
* PAGE_SIZE
== mem
->memory_size
)
4306 rc
= gmap_map_segment(kvm
->arch
.gmap
, mem
->userspace_addr
,
4307 mem
->guest_phys_addr
, mem
->memory_size
);
4309 pr_warn("failed to commit memory region\n");
4313 static inline unsigned long nonhyp_mask(int i
)
4315 unsigned int nonhyp_fai
= (sclp
.hmfai
<< i
* 2) >> 30;
4317 return 0x0000ffffffffffffUL
>> (nonhyp_fai
<< 4);
4320 void kvm_arch_vcpu_block_finish(struct kvm_vcpu
*vcpu
)
4322 vcpu
->valid_wakeup
= false;
4325 static int __init
kvm_s390_init(void)
4329 if (!sclp
.has_sief2
) {
4330 pr_info("SIE not available\n");
4334 for (i
= 0; i
< 16; i
++)
4335 kvm_s390_fac_base
[i
] |=
4336 S390_lowcore
.stfle_fac_list
[i
] & nonhyp_mask(i
);
4338 return kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
4341 static void __exit
kvm_s390_exit(void)
4346 module_init(kvm_s390_init
);
4347 module_exit(kvm_s390_exit
);
4350 * Enable autoloading of the kvm module.
4351 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
4352 * since x86 takes a different approach.
4354 #include <linux/miscdevice.h>
4355 MODULE_ALIAS_MISCDEV(KVM_MINOR
);
4356 MODULE_ALIAS("devname:kvm");