2 * hosting zSeries kernel virtual machines
4 * Copyright IBM Corp. 2008, 2009
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 * Christian Borntraeger <borntraeger@de.ibm.com>
12 * Heiko Carstens <heiko.carstens@de.ibm.com>
13 * Christian Ehrhardt <ehrhardt@de.ibm.com>
14 * Jason J. Herne <jjherne@us.ibm.com>
17 #include <linux/compiler.h>
18 #include <linux/err.h>
20 #include <linux/hrtimer.h>
21 #include <linux/init.h>
22 #include <linux/kvm.h>
23 #include <linux/kvm_host.h>
24 #include <linux/mman.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/random.h>
28 #include <linux/slab.h>
29 #include <linux/timer.h>
30 #include <linux/vmalloc.h>
31 #include <linux/bitmap.h>
32 #include <linux/sched/signal.h>
34 #include <asm/asm-offsets.h>
35 #include <asm/lowcore.h>
37 #include <asm/pgtable.h>
40 #include <asm/switch_to.h>
43 #include <asm/cpacf.h>
44 #include <asm/timex.h>
48 #define KMSG_COMPONENT "kvm-s390"
50 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
52 #define CREATE_TRACE_POINTS
54 #include "trace-s390.h"
56 #define MEM_OP_MAX_SIZE 65536 /* Maximum transfer size for KVM_S390_MEM_OP */
58 #define VCPU_IRQS_MAX_BUF (sizeof(struct kvm_s390_irq) * \
59 (KVM_MAX_VCPUS + LOCAL_IRQS))
61 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
63 struct kvm_stats_debugfs_item debugfs_entries
[] = {
64 { "userspace_handled", VCPU_STAT(exit_userspace
) },
65 { "exit_null", VCPU_STAT(exit_null
) },
66 { "exit_validity", VCPU_STAT(exit_validity
) },
67 { "exit_stop_request", VCPU_STAT(exit_stop_request
) },
68 { "exit_external_request", VCPU_STAT(exit_external_request
) },
69 { "exit_external_interrupt", VCPU_STAT(exit_external_interrupt
) },
70 { "exit_instruction", VCPU_STAT(exit_instruction
) },
71 { "exit_pei", VCPU_STAT(exit_pei
) },
72 { "exit_program_interruption", VCPU_STAT(exit_program_interruption
) },
73 { "exit_instr_and_program_int", VCPU_STAT(exit_instr_and_program
) },
74 { "exit_operation_exception", VCPU_STAT(exit_operation_exception
) },
75 { "halt_successful_poll", VCPU_STAT(halt_successful_poll
) },
76 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll
) },
77 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid
) },
78 { "halt_wakeup", VCPU_STAT(halt_wakeup
) },
79 { "instruction_lctlg", VCPU_STAT(instruction_lctlg
) },
80 { "instruction_lctl", VCPU_STAT(instruction_lctl
) },
81 { "instruction_stctl", VCPU_STAT(instruction_stctl
) },
82 { "instruction_stctg", VCPU_STAT(instruction_stctg
) },
83 { "deliver_emergency_signal", VCPU_STAT(deliver_emergency_signal
) },
84 { "deliver_external_call", VCPU_STAT(deliver_external_call
) },
85 { "deliver_service_signal", VCPU_STAT(deliver_service_signal
) },
86 { "deliver_virtio_interrupt", VCPU_STAT(deliver_virtio_interrupt
) },
87 { "deliver_stop_signal", VCPU_STAT(deliver_stop_signal
) },
88 { "deliver_prefix_signal", VCPU_STAT(deliver_prefix_signal
) },
89 { "deliver_restart_signal", VCPU_STAT(deliver_restart_signal
) },
90 { "deliver_program_interruption", VCPU_STAT(deliver_program_int
) },
91 { "exit_wait_state", VCPU_STAT(exit_wait_state
) },
92 { "instruction_pfmf", VCPU_STAT(instruction_pfmf
) },
93 { "instruction_stidp", VCPU_STAT(instruction_stidp
) },
94 { "instruction_spx", VCPU_STAT(instruction_spx
) },
95 { "instruction_stpx", VCPU_STAT(instruction_stpx
) },
96 { "instruction_stap", VCPU_STAT(instruction_stap
) },
97 { "instruction_storage_key", VCPU_STAT(instruction_storage_key
) },
98 { "instruction_ipte_interlock", VCPU_STAT(instruction_ipte_interlock
) },
99 { "instruction_stsch", VCPU_STAT(instruction_stsch
) },
100 { "instruction_chsc", VCPU_STAT(instruction_chsc
) },
101 { "instruction_essa", VCPU_STAT(instruction_essa
) },
102 { "instruction_stsi", VCPU_STAT(instruction_stsi
) },
103 { "instruction_stfl", VCPU_STAT(instruction_stfl
) },
104 { "instruction_tprot", VCPU_STAT(instruction_tprot
) },
105 { "instruction_sthyi", VCPU_STAT(instruction_sthyi
) },
106 { "instruction_sie", VCPU_STAT(instruction_sie
) },
107 { "instruction_sigp_sense", VCPU_STAT(instruction_sigp_sense
) },
108 { "instruction_sigp_sense_running", VCPU_STAT(instruction_sigp_sense_running
) },
109 { "instruction_sigp_external_call", VCPU_STAT(instruction_sigp_external_call
) },
110 { "instruction_sigp_emergency", VCPU_STAT(instruction_sigp_emergency
) },
111 { "instruction_sigp_cond_emergency", VCPU_STAT(instruction_sigp_cond_emergency
) },
112 { "instruction_sigp_start", VCPU_STAT(instruction_sigp_start
) },
113 { "instruction_sigp_stop", VCPU_STAT(instruction_sigp_stop
) },
114 { "instruction_sigp_stop_store_status", VCPU_STAT(instruction_sigp_stop_store_status
) },
115 { "instruction_sigp_store_status", VCPU_STAT(instruction_sigp_store_status
) },
116 { "instruction_sigp_store_adtl_status", VCPU_STAT(instruction_sigp_store_adtl_status
) },
117 { "instruction_sigp_set_arch", VCPU_STAT(instruction_sigp_arch
) },
118 { "instruction_sigp_set_prefix", VCPU_STAT(instruction_sigp_prefix
) },
119 { "instruction_sigp_restart", VCPU_STAT(instruction_sigp_restart
) },
120 { "instruction_sigp_cpu_reset", VCPU_STAT(instruction_sigp_cpu_reset
) },
121 { "instruction_sigp_init_cpu_reset", VCPU_STAT(instruction_sigp_init_cpu_reset
) },
122 { "instruction_sigp_unknown", VCPU_STAT(instruction_sigp_unknown
) },
123 { "diagnose_10", VCPU_STAT(diagnose_10
) },
124 { "diagnose_44", VCPU_STAT(diagnose_44
) },
125 { "diagnose_9c", VCPU_STAT(diagnose_9c
) },
126 { "diagnose_258", VCPU_STAT(diagnose_258
) },
127 { "diagnose_308", VCPU_STAT(diagnose_308
) },
128 { "diagnose_500", VCPU_STAT(diagnose_500
) },
132 /* allow nested virtualization in KVM (if enabled by user space) */
134 module_param(nested
, int, S_IRUGO
);
135 MODULE_PARM_DESC(nested
, "Nested virtualization support");
137 /* upper facilities limit for kvm */
138 unsigned long kvm_s390_fac_list_mask
[16] = { FACILITIES_KVM
};
140 unsigned long kvm_s390_fac_list_mask_size(void)
142 BUILD_BUG_ON(ARRAY_SIZE(kvm_s390_fac_list_mask
) > S390_ARCH_FAC_MASK_SIZE_U64
);
143 return ARRAY_SIZE(kvm_s390_fac_list_mask
);
146 /* available cpu features supported by kvm */
147 static DECLARE_BITMAP(kvm_s390_available_cpu_feat
, KVM_S390_VM_CPU_FEAT_NR_BITS
);
148 /* available subfunctions indicated via query / "test bit" */
149 static struct kvm_s390_vm_cpu_subfunc kvm_s390_available_subfunc
;
151 static struct gmap_notifier gmap_notifier
;
152 static struct gmap_notifier vsie_gmap_notifier
;
153 debug_info_t
*kvm_s390_dbf
;
155 /* Section: not file related */
156 int kvm_arch_hardware_enable(void)
158 /* every s390 is virtualization enabled ;-) */
162 static void kvm_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
166 * This callback is executed during stop_machine(). All CPUs are therefore
167 * temporarily stopped. In order not to change guest behavior, we have to
168 * disable preemption whenever we touch the epoch of kvm and the VCPUs,
169 * so a CPU won't be stopped while calculating with the epoch.
171 static int kvm_clock_sync(struct notifier_block
*notifier
, unsigned long val
,
175 struct kvm_vcpu
*vcpu
;
177 unsigned long long *delta
= v
;
179 list_for_each_entry(kvm
, &vm_list
, vm_list
) {
180 kvm
->arch
.epoch
-= *delta
;
181 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
182 vcpu
->arch
.sie_block
->epoch
-= *delta
;
183 if (vcpu
->arch
.cputm_enabled
)
184 vcpu
->arch
.cputm_start
+= *delta
;
185 if (vcpu
->arch
.vsie_block
)
186 vcpu
->arch
.vsie_block
->epoch
-= *delta
;
192 static struct notifier_block kvm_clock_notifier
= {
193 .notifier_call
= kvm_clock_sync
,
196 int kvm_arch_hardware_setup(void)
198 gmap_notifier
.notifier_call
= kvm_gmap_notifier
;
199 gmap_register_pte_notifier(&gmap_notifier
);
200 vsie_gmap_notifier
.notifier_call
= kvm_s390_vsie_gmap_notifier
;
201 gmap_register_pte_notifier(&vsie_gmap_notifier
);
202 atomic_notifier_chain_register(&s390_epoch_delta_notifier
,
203 &kvm_clock_notifier
);
207 void kvm_arch_hardware_unsetup(void)
209 gmap_unregister_pte_notifier(&gmap_notifier
);
210 gmap_unregister_pte_notifier(&vsie_gmap_notifier
);
211 atomic_notifier_chain_unregister(&s390_epoch_delta_notifier
,
212 &kvm_clock_notifier
);
215 static void allow_cpu_feat(unsigned long nr
)
217 set_bit_inv(nr
, kvm_s390_available_cpu_feat
);
220 static inline int plo_test_bit(unsigned char nr
)
222 register unsigned long r0
asm("0") = (unsigned long) nr
| 0x100;
226 /* Parameter registers are ignored for "test bit" */
236 static void kvm_s390_cpu_feat_init(void)
240 for (i
= 0; i
< 256; ++i
) {
242 kvm_s390_available_subfunc
.plo
[i
>> 3] |= 0x80 >> (i
& 7);
245 if (test_facility(28)) /* TOD-clock steering */
246 ptff(kvm_s390_available_subfunc
.ptff
,
247 sizeof(kvm_s390_available_subfunc
.ptff
),
250 if (test_facility(17)) { /* MSA */
251 __cpacf_query(CPACF_KMAC
, (cpacf_mask_t
*)
252 kvm_s390_available_subfunc
.kmac
);
253 __cpacf_query(CPACF_KMC
, (cpacf_mask_t
*)
254 kvm_s390_available_subfunc
.kmc
);
255 __cpacf_query(CPACF_KM
, (cpacf_mask_t
*)
256 kvm_s390_available_subfunc
.km
);
257 __cpacf_query(CPACF_KIMD
, (cpacf_mask_t
*)
258 kvm_s390_available_subfunc
.kimd
);
259 __cpacf_query(CPACF_KLMD
, (cpacf_mask_t
*)
260 kvm_s390_available_subfunc
.klmd
);
262 if (test_facility(76)) /* MSA3 */
263 __cpacf_query(CPACF_PCKMO
, (cpacf_mask_t
*)
264 kvm_s390_available_subfunc
.pckmo
);
265 if (test_facility(77)) { /* MSA4 */
266 __cpacf_query(CPACF_KMCTR
, (cpacf_mask_t
*)
267 kvm_s390_available_subfunc
.kmctr
);
268 __cpacf_query(CPACF_KMF
, (cpacf_mask_t
*)
269 kvm_s390_available_subfunc
.kmf
);
270 __cpacf_query(CPACF_KMO
, (cpacf_mask_t
*)
271 kvm_s390_available_subfunc
.kmo
);
272 __cpacf_query(CPACF_PCC
, (cpacf_mask_t
*)
273 kvm_s390_available_subfunc
.pcc
);
275 if (test_facility(57)) /* MSA5 */
276 __cpacf_query(CPACF_PPNO
, (cpacf_mask_t
*)
277 kvm_s390_available_subfunc
.ppno
);
279 if (MACHINE_HAS_ESOP
)
280 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_ESOP
);
282 * We need SIE support, ESOP (PROT_READ protection for gmap_shadow),
283 * 64bit SCAO (SCA passthrough) and IDTE (for gmap_shadow unshadowing).
285 if (!sclp
.has_sief2
|| !MACHINE_HAS_ESOP
|| !sclp
.has_64bscao
||
286 !test_facility(3) || !nested
)
288 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIEF2
);
289 if (sclp
.has_64bscao
)
290 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_64BSCAO
);
292 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_SIIF
);
294 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GPERE
);
296 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_GSLS
);
298 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IB
);
300 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_CEI
);
302 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_IBS
);
304 allow_cpu_feat(KVM_S390_VM_CPU_FEAT_KSS
);
306 * KVM_S390_VM_CPU_FEAT_SKEY: Wrong shadow of PTE.I bits will make
307 * all skey handling functions read/set the skey from the PGSTE
308 * instead of the real storage key.
310 * KVM_S390_VM_CPU_FEAT_CMMA: Wrong shadow of PTE.I bits will make
311 * pages being detected as preserved although they are resident.
313 * KVM_S390_VM_CPU_FEAT_PFMFI: Wrong shadow of PTE.I bits will
314 * have the same effect as for KVM_S390_VM_CPU_FEAT_SKEY.
316 * For KVM_S390_VM_CPU_FEAT_SKEY, KVM_S390_VM_CPU_FEAT_CMMA and
317 * KVM_S390_VM_CPU_FEAT_PFMFI, all PTE.I and PGSTE bits have to be
318 * correctly shadowed. We can do that for the PGSTE but not for PTE.I.
320 * KVM_S390_VM_CPU_FEAT_SIGPIF: Wrong SCB addresses in the SCA. We
321 * cannot easily shadow the SCA because of the ipte lock.
325 int kvm_arch_init(void *opaque
)
327 kvm_s390_dbf
= debug_register("kvm-trace", 32, 1, 7 * sizeof(long));
331 if (debug_register_view(kvm_s390_dbf
, &debug_sprintf_view
)) {
332 debug_unregister(kvm_s390_dbf
);
336 kvm_s390_cpu_feat_init();
338 /* Register floating interrupt controller interface. */
339 return kvm_register_device_ops(&kvm_flic_ops
, KVM_DEV_TYPE_FLIC
);
342 void kvm_arch_exit(void)
344 debug_unregister(kvm_s390_dbf
);
347 /* Section: device related */
348 long kvm_arch_dev_ioctl(struct file
*filp
,
349 unsigned int ioctl
, unsigned long arg
)
351 if (ioctl
== KVM_S390_ENABLE_SIE
)
352 return s390_enable_sie();
356 int kvm_vm_ioctl_check_extension(struct kvm
*kvm
, long ext
)
361 case KVM_CAP_S390_PSW
:
362 case KVM_CAP_S390_GMAP
:
363 case KVM_CAP_SYNC_MMU
:
364 #ifdef CONFIG_KVM_S390_UCONTROL
365 case KVM_CAP_S390_UCONTROL
:
367 case KVM_CAP_ASYNC_PF
:
368 case KVM_CAP_SYNC_REGS
:
369 case KVM_CAP_ONE_REG
:
370 case KVM_CAP_ENABLE_CAP
:
371 case KVM_CAP_S390_CSS_SUPPORT
:
372 case KVM_CAP_IOEVENTFD
:
373 case KVM_CAP_DEVICE_CTRL
:
374 case KVM_CAP_ENABLE_CAP_VM
:
375 case KVM_CAP_S390_IRQCHIP
:
376 case KVM_CAP_VM_ATTRIBUTES
:
377 case KVM_CAP_MP_STATE
:
378 case KVM_CAP_IMMEDIATE_EXIT
:
379 case KVM_CAP_S390_INJECT_IRQ
:
380 case KVM_CAP_S390_USER_SIGP
:
381 case KVM_CAP_S390_USER_STSI
:
382 case KVM_CAP_S390_SKEYS
:
383 case KVM_CAP_S390_IRQ_STATE
:
384 case KVM_CAP_S390_USER_INSTR0
:
385 case KVM_CAP_S390_AIS
:
388 case KVM_CAP_S390_MEM_OP
:
391 case KVM_CAP_NR_VCPUS
:
392 case KVM_CAP_MAX_VCPUS
:
393 r
= KVM_S390_BSCA_CPU_SLOTS
;
394 if (!kvm_s390_use_sca_entries())
396 else if (sclp
.has_esca
&& sclp
.has_64bscao
)
397 r
= KVM_S390_ESCA_CPU_SLOTS
;
399 case KVM_CAP_NR_MEMSLOTS
:
400 r
= KVM_USER_MEM_SLOTS
;
402 case KVM_CAP_S390_COW
:
403 r
= MACHINE_HAS_ESOP
;
405 case KVM_CAP_S390_VECTOR_REGISTERS
:
408 case KVM_CAP_S390_RI
:
409 r
= test_facility(64);
411 case KVM_CAP_S390_GS
:
412 r
= test_facility(133);
420 static void kvm_s390_sync_dirty_log(struct kvm
*kvm
,
421 struct kvm_memory_slot
*memslot
)
423 gfn_t cur_gfn
, last_gfn
;
424 unsigned long address
;
425 struct gmap
*gmap
= kvm
->arch
.gmap
;
427 /* Loop over all guest pages */
428 last_gfn
= memslot
->base_gfn
+ memslot
->npages
;
429 for (cur_gfn
= memslot
->base_gfn
; cur_gfn
<= last_gfn
; cur_gfn
++) {
430 address
= gfn_to_hva_memslot(memslot
, cur_gfn
);
432 if (test_and_clear_guest_dirty(gmap
->mm
, address
))
433 mark_page_dirty(kvm
, cur_gfn
);
434 if (fatal_signal_pending(current
))
440 /* Section: vm related */
441 static void sca_del_vcpu(struct kvm_vcpu
*vcpu
);
444 * Get (and clear) the dirty memory log for a memory slot.
446 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
,
447 struct kvm_dirty_log
*log
)
451 struct kvm_memslots
*slots
;
452 struct kvm_memory_slot
*memslot
;
455 if (kvm_is_ucontrol(kvm
))
458 mutex_lock(&kvm
->slots_lock
);
461 if (log
->slot
>= KVM_USER_MEM_SLOTS
)
464 slots
= kvm_memslots(kvm
);
465 memslot
= id_to_memslot(slots
, log
->slot
);
467 if (!memslot
->dirty_bitmap
)
470 kvm_s390_sync_dirty_log(kvm
, memslot
);
471 r
= kvm_get_dirty_log(kvm
, log
, &is_dirty
);
475 /* Clear the dirty log */
477 n
= kvm_dirty_bitmap_bytes(memslot
);
478 memset(memslot
->dirty_bitmap
, 0, n
);
482 mutex_unlock(&kvm
->slots_lock
);
486 static void icpt_operexc_on_all_vcpus(struct kvm
*kvm
)
489 struct kvm_vcpu
*vcpu
;
491 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
492 kvm_s390_sync_request(KVM_REQ_ICPT_OPEREXC
, vcpu
);
496 static int kvm_vm_ioctl_enable_cap(struct kvm
*kvm
, struct kvm_enable_cap
*cap
)
504 case KVM_CAP_S390_IRQCHIP
:
505 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_IRQCHIP");
506 kvm
->arch
.use_irqchip
= 1;
509 case KVM_CAP_S390_USER_SIGP
:
510 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_SIGP");
511 kvm
->arch
.user_sigp
= 1;
514 case KVM_CAP_S390_VECTOR_REGISTERS
:
515 mutex_lock(&kvm
->lock
);
516 if (kvm
->created_vcpus
) {
518 } else if (MACHINE_HAS_VX
) {
519 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 129);
520 set_kvm_facility(kvm
->arch
.model
.fac_list
, 129);
521 if (test_facility(134)) {
522 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 134);
523 set_kvm_facility(kvm
->arch
.model
.fac_list
, 134);
525 if (test_facility(135)) {
526 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 135);
527 set_kvm_facility(kvm
->arch
.model
.fac_list
, 135);
532 mutex_unlock(&kvm
->lock
);
533 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_VECTOR_REGISTERS %s",
534 r
? "(not available)" : "(success)");
536 case KVM_CAP_S390_RI
:
538 mutex_lock(&kvm
->lock
);
539 if (kvm
->created_vcpus
) {
541 } else if (test_facility(64)) {
542 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 64);
543 set_kvm_facility(kvm
->arch
.model
.fac_list
, 64);
546 mutex_unlock(&kvm
->lock
);
547 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_RI %s",
548 r
? "(not available)" : "(success)");
550 case KVM_CAP_S390_AIS
:
551 mutex_lock(&kvm
->lock
);
552 if (kvm
->created_vcpus
) {
555 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 72);
556 set_kvm_facility(kvm
->arch
.model
.fac_list
, 72);
557 kvm
->arch
.float_int
.ais_enabled
= 1;
560 mutex_unlock(&kvm
->lock
);
561 VM_EVENT(kvm
, 3, "ENABLE: AIS %s",
562 r
? "(not available)" : "(success)");
564 case KVM_CAP_S390_GS
:
566 mutex_lock(&kvm
->lock
);
567 if (atomic_read(&kvm
->online_vcpus
)) {
569 } else if (test_facility(133)) {
570 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 133);
571 set_kvm_facility(kvm
->arch
.model
.fac_list
, 133);
574 mutex_unlock(&kvm
->lock
);
575 VM_EVENT(kvm
, 3, "ENABLE: CAP_S390_GS %s",
576 r
? "(not available)" : "(success)");
578 case KVM_CAP_S390_USER_STSI
:
579 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_STSI");
580 kvm
->arch
.user_stsi
= 1;
583 case KVM_CAP_S390_USER_INSTR0
:
584 VM_EVENT(kvm
, 3, "%s", "ENABLE: CAP_S390_USER_INSTR0");
585 kvm
->arch
.user_instr0
= 1;
586 icpt_operexc_on_all_vcpus(kvm
);
596 static int kvm_s390_get_mem_control(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
600 switch (attr
->attr
) {
601 case KVM_S390_VM_MEM_LIMIT_SIZE
:
603 VM_EVENT(kvm
, 3, "QUERY: max guest memory: %lu bytes",
604 kvm
->arch
.mem_limit
);
605 if (put_user(kvm
->arch
.mem_limit
, (u64 __user
*)attr
->addr
))
615 static int kvm_s390_set_mem_control(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
619 switch (attr
->attr
) {
620 case KVM_S390_VM_MEM_ENABLE_CMMA
:
626 VM_EVENT(kvm
, 3, "%s", "ENABLE: CMMA support");
627 mutex_lock(&kvm
->lock
);
628 if (!kvm
->created_vcpus
) {
629 kvm
->arch
.use_cmma
= 1;
632 mutex_unlock(&kvm
->lock
);
634 case KVM_S390_VM_MEM_CLR_CMMA
:
639 if (!kvm
->arch
.use_cmma
)
642 VM_EVENT(kvm
, 3, "%s", "RESET: CMMA states");
643 mutex_lock(&kvm
->lock
);
644 idx
= srcu_read_lock(&kvm
->srcu
);
645 s390_reset_cmma(kvm
->arch
.gmap
->mm
);
646 srcu_read_unlock(&kvm
->srcu
, idx
);
647 mutex_unlock(&kvm
->lock
);
650 case KVM_S390_VM_MEM_LIMIT_SIZE
: {
651 unsigned long new_limit
;
653 if (kvm_is_ucontrol(kvm
))
656 if (get_user(new_limit
, (u64 __user
*)attr
->addr
))
659 if (kvm
->arch
.mem_limit
!= KVM_S390_NO_MEM_LIMIT
&&
660 new_limit
> kvm
->arch
.mem_limit
)
666 /* gmap_create takes last usable address */
667 if (new_limit
!= KVM_S390_NO_MEM_LIMIT
)
671 mutex_lock(&kvm
->lock
);
672 if (!kvm
->created_vcpus
) {
673 /* gmap_create will round the limit up */
674 struct gmap
*new = gmap_create(current
->mm
, new_limit
);
679 gmap_remove(kvm
->arch
.gmap
);
681 kvm
->arch
.gmap
= new;
685 mutex_unlock(&kvm
->lock
);
686 VM_EVENT(kvm
, 3, "SET: max guest address: %lu", new_limit
);
687 VM_EVENT(kvm
, 3, "New guest asce: 0x%pK",
688 (void *) kvm
->arch
.gmap
->asce
);
698 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu
*vcpu
);
700 static int kvm_s390_vm_set_crypto(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
702 struct kvm_vcpu
*vcpu
;
705 if (!test_kvm_facility(kvm
, 76))
708 mutex_lock(&kvm
->lock
);
709 switch (attr
->attr
) {
710 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW
:
712 kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
,
713 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
714 kvm
->arch
.crypto
.aes_kw
= 1;
715 VM_EVENT(kvm
, 3, "%s", "ENABLE: AES keywrapping support");
717 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
:
719 kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
,
720 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
721 kvm
->arch
.crypto
.dea_kw
= 1;
722 VM_EVENT(kvm
, 3, "%s", "ENABLE: DEA keywrapping support");
724 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW
:
725 kvm
->arch
.crypto
.aes_kw
= 0;
726 memset(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
, 0,
727 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
728 VM_EVENT(kvm
, 3, "%s", "DISABLE: AES keywrapping support");
730 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
:
731 kvm
->arch
.crypto
.dea_kw
= 0;
732 memset(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
, 0,
733 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
734 VM_EVENT(kvm
, 3, "%s", "DISABLE: DEA keywrapping support");
737 mutex_unlock(&kvm
->lock
);
741 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
742 kvm_s390_vcpu_crypto_setup(vcpu
);
745 mutex_unlock(&kvm
->lock
);
749 static int kvm_s390_set_tod_high(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
753 if (copy_from_user(>od_high
, (void __user
*)attr
->addr
,
759 VM_EVENT(kvm
, 3, "SET: TOD extension: 0x%x", gtod_high
);
764 static int kvm_s390_set_tod_low(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
768 if (copy_from_user(>od
, (void __user
*)attr
->addr
, sizeof(gtod
)))
771 kvm_s390_set_tod_clock(kvm
, gtod
);
772 VM_EVENT(kvm
, 3, "SET: TOD base: 0x%llx", gtod
);
776 static int kvm_s390_set_tod(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
783 switch (attr
->attr
) {
784 case KVM_S390_VM_TOD_HIGH
:
785 ret
= kvm_s390_set_tod_high(kvm
, attr
);
787 case KVM_S390_VM_TOD_LOW
:
788 ret
= kvm_s390_set_tod_low(kvm
, attr
);
797 static int kvm_s390_get_tod_high(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
801 if (copy_to_user((void __user
*)attr
->addr
, >od_high
,
804 VM_EVENT(kvm
, 3, "QUERY: TOD extension: 0x%x", gtod_high
);
809 static int kvm_s390_get_tod_low(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
813 gtod
= kvm_s390_get_tod_clock_fast(kvm
);
814 if (copy_to_user((void __user
*)attr
->addr
, >od
, sizeof(gtod
)))
816 VM_EVENT(kvm
, 3, "QUERY: TOD base: 0x%llx", gtod
);
821 static int kvm_s390_get_tod(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
828 switch (attr
->attr
) {
829 case KVM_S390_VM_TOD_HIGH
:
830 ret
= kvm_s390_get_tod_high(kvm
, attr
);
832 case KVM_S390_VM_TOD_LOW
:
833 ret
= kvm_s390_get_tod_low(kvm
, attr
);
842 static int kvm_s390_set_processor(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
844 struct kvm_s390_vm_cpu_processor
*proc
;
845 u16 lowest_ibc
, unblocked_ibc
;
848 mutex_lock(&kvm
->lock
);
849 if (kvm
->created_vcpus
) {
853 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
858 if (!copy_from_user(proc
, (void __user
*)attr
->addr
,
860 kvm
->arch
.model
.cpuid
= proc
->cpuid
;
861 lowest_ibc
= sclp
.ibc
>> 16 & 0xfff;
862 unblocked_ibc
= sclp
.ibc
& 0xfff;
863 if (lowest_ibc
&& proc
->ibc
) {
864 if (proc
->ibc
> unblocked_ibc
)
865 kvm
->arch
.model
.ibc
= unblocked_ibc
;
866 else if (proc
->ibc
< lowest_ibc
)
867 kvm
->arch
.model
.ibc
= lowest_ibc
;
869 kvm
->arch
.model
.ibc
= proc
->ibc
;
871 memcpy(kvm
->arch
.model
.fac_list
, proc
->fac_list
,
872 S390_ARCH_FAC_LIST_SIZE_BYTE
);
873 VM_EVENT(kvm
, 3, "SET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
875 kvm
->arch
.model
.cpuid
);
876 VM_EVENT(kvm
, 3, "SET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
877 kvm
->arch
.model
.fac_list
[0],
878 kvm
->arch
.model
.fac_list
[1],
879 kvm
->arch
.model
.fac_list
[2]);
884 mutex_unlock(&kvm
->lock
);
888 static int kvm_s390_set_processor_feat(struct kvm
*kvm
,
889 struct kvm_device_attr
*attr
)
891 struct kvm_s390_vm_cpu_feat data
;
894 if (copy_from_user(&data
, (void __user
*)attr
->addr
, sizeof(data
)))
896 if (!bitmap_subset((unsigned long *) data
.feat
,
897 kvm_s390_available_cpu_feat
,
898 KVM_S390_VM_CPU_FEAT_NR_BITS
))
901 mutex_lock(&kvm
->lock
);
902 if (!atomic_read(&kvm
->online_vcpus
)) {
903 bitmap_copy(kvm
->arch
.cpu_feat
, (unsigned long *) data
.feat
,
904 KVM_S390_VM_CPU_FEAT_NR_BITS
);
907 mutex_unlock(&kvm
->lock
);
911 static int kvm_s390_set_processor_subfunc(struct kvm
*kvm
,
912 struct kvm_device_attr
*attr
)
915 * Once supported by kernel + hw, we have to store the subfunctions
916 * in kvm->arch and remember that user space configured them.
921 static int kvm_s390_set_cpu_model(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
925 switch (attr
->attr
) {
926 case KVM_S390_VM_CPU_PROCESSOR
:
927 ret
= kvm_s390_set_processor(kvm
, attr
);
929 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
930 ret
= kvm_s390_set_processor_feat(kvm
, attr
);
932 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
933 ret
= kvm_s390_set_processor_subfunc(kvm
, attr
);
939 static int kvm_s390_get_processor(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
941 struct kvm_s390_vm_cpu_processor
*proc
;
944 proc
= kzalloc(sizeof(*proc
), GFP_KERNEL
);
949 proc
->cpuid
= kvm
->arch
.model
.cpuid
;
950 proc
->ibc
= kvm
->arch
.model
.ibc
;
951 memcpy(&proc
->fac_list
, kvm
->arch
.model
.fac_list
,
952 S390_ARCH_FAC_LIST_SIZE_BYTE
);
953 VM_EVENT(kvm
, 3, "GET: guest ibc: 0x%4.4x, guest cpuid: 0x%16.16llx",
955 kvm
->arch
.model
.cpuid
);
956 VM_EVENT(kvm
, 3, "GET: guest faclist: 0x%16.16llx.%16.16llx.%16.16llx",
957 kvm
->arch
.model
.fac_list
[0],
958 kvm
->arch
.model
.fac_list
[1],
959 kvm
->arch
.model
.fac_list
[2]);
960 if (copy_to_user((void __user
*)attr
->addr
, proc
, sizeof(*proc
)))
967 static int kvm_s390_get_machine(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
969 struct kvm_s390_vm_cpu_machine
*mach
;
972 mach
= kzalloc(sizeof(*mach
), GFP_KERNEL
);
977 get_cpu_id((struct cpuid
*) &mach
->cpuid
);
978 mach
->ibc
= sclp
.ibc
;
979 memcpy(&mach
->fac_mask
, kvm
->arch
.model
.fac_mask
,
980 S390_ARCH_FAC_LIST_SIZE_BYTE
);
981 memcpy((unsigned long *)&mach
->fac_list
, S390_lowcore
.stfle_fac_list
,
982 sizeof(S390_lowcore
.stfle_fac_list
));
983 VM_EVENT(kvm
, 3, "GET: host ibc: 0x%4.4x, host cpuid: 0x%16.16llx",
985 kvm
->arch
.model
.cpuid
);
986 VM_EVENT(kvm
, 3, "GET: host facmask: 0x%16.16llx.%16.16llx.%16.16llx",
990 VM_EVENT(kvm
, 3, "GET: host faclist: 0x%16.16llx.%16.16llx.%16.16llx",
994 if (copy_to_user((void __user
*)attr
->addr
, mach
, sizeof(*mach
)))
1001 static int kvm_s390_get_processor_feat(struct kvm
*kvm
,
1002 struct kvm_device_attr
*attr
)
1004 struct kvm_s390_vm_cpu_feat data
;
1006 bitmap_copy((unsigned long *) data
.feat
, kvm
->arch
.cpu_feat
,
1007 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1008 if (copy_to_user((void __user
*)attr
->addr
, &data
, sizeof(data
)))
1013 static int kvm_s390_get_machine_feat(struct kvm
*kvm
,
1014 struct kvm_device_attr
*attr
)
1016 struct kvm_s390_vm_cpu_feat data
;
1018 bitmap_copy((unsigned long *) data
.feat
,
1019 kvm_s390_available_cpu_feat
,
1020 KVM_S390_VM_CPU_FEAT_NR_BITS
);
1021 if (copy_to_user((void __user
*)attr
->addr
, &data
, sizeof(data
)))
1026 static int kvm_s390_get_processor_subfunc(struct kvm
*kvm
,
1027 struct kvm_device_attr
*attr
)
1030 * Once we can actually configure subfunctions (kernel + hw support),
1031 * we have to check if they were already set by user space, if so copy
1032 * them from kvm->arch.
1037 static int kvm_s390_get_machine_subfunc(struct kvm
*kvm
,
1038 struct kvm_device_attr
*attr
)
1040 if (copy_to_user((void __user
*)attr
->addr
, &kvm_s390_available_subfunc
,
1041 sizeof(struct kvm_s390_vm_cpu_subfunc
)))
1045 static int kvm_s390_get_cpu_model(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1049 switch (attr
->attr
) {
1050 case KVM_S390_VM_CPU_PROCESSOR
:
1051 ret
= kvm_s390_get_processor(kvm
, attr
);
1053 case KVM_S390_VM_CPU_MACHINE
:
1054 ret
= kvm_s390_get_machine(kvm
, attr
);
1056 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1057 ret
= kvm_s390_get_processor_feat(kvm
, attr
);
1059 case KVM_S390_VM_CPU_MACHINE_FEAT
:
1060 ret
= kvm_s390_get_machine_feat(kvm
, attr
);
1062 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1063 ret
= kvm_s390_get_processor_subfunc(kvm
, attr
);
1065 case KVM_S390_VM_CPU_MACHINE_SUBFUNC
:
1066 ret
= kvm_s390_get_machine_subfunc(kvm
, attr
);
1072 static int kvm_s390_vm_set_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1076 switch (attr
->group
) {
1077 case KVM_S390_VM_MEM_CTRL
:
1078 ret
= kvm_s390_set_mem_control(kvm
, attr
);
1080 case KVM_S390_VM_TOD
:
1081 ret
= kvm_s390_set_tod(kvm
, attr
);
1083 case KVM_S390_VM_CPU_MODEL
:
1084 ret
= kvm_s390_set_cpu_model(kvm
, attr
);
1086 case KVM_S390_VM_CRYPTO
:
1087 ret
= kvm_s390_vm_set_crypto(kvm
, attr
);
1097 static int kvm_s390_vm_get_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1101 switch (attr
->group
) {
1102 case KVM_S390_VM_MEM_CTRL
:
1103 ret
= kvm_s390_get_mem_control(kvm
, attr
);
1105 case KVM_S390_VM_TOD
:
1106 ret
= kvm_s390_get_tod(kvm
, attr
);
1108 case KVM_S390_VM_CPU_MODEL
:
1109 ret
= kvm_s390_get_cpu_model(kvm
, attr
);
1119 static int kvm_s390_vm_has_attr(struct kvm
*kvm
, struct kvm_device_attr
*attr
)
1123 switch (attr
->group
) {
1124 case KVM_S390_VM_MEM_CTRL
:
1125 switch (attr
->attr
) {
1126 case KVM_S390_VM_MEM_ENABLE_CMMA
:
1127 case KVM_S390_VM_MEM_CLR_CMMA
:
1128 ret
= sclp
.has_cmma
? 0 : -ENXIO
;
1130 case KVM_S390_VM_MEM_LIMIT_SIZE
:
1138 case KVM_S390_VM_TOD
:
1139 switch (attr
->attr
) {
1140 case KVM_S390_VM_TOD_LOW
:
1141 case KVM_S390_VM_TOD_HIGH
:
1149 case KVM_S390_VM_CPU_MODEL
:
1150 switch (attr
->attr
) {
1151 case KVM_S390_VM_CPU_PROCESSOR
:
1152 case KVM_S390_VM_CPU_MACHINE
:
1153 case KVM_S390_VM_CPU_PROCESSOR_FEAT
:
1154 case KVM_S390_VM_CPU_MACHINE_FEAT
:
1155 case KVM_S390_VM_CPU_MACHINE_SUBFUNC
:
1158 /* configuring subfunctions is not supported yet */
1159 case KVM_S390_VM_CPU_PROCESSOR_SUBFUNC
:
1165 case KVM_S390_VM_CRYPTO
:
1166 switch (attr
->attr
) {
1167 case KVM_S390_VM_CRYPTO_ENABLE_AES_KW
:
1168 case KVM_S390_VM_CRYPTO_ENABLE_DEA_KW
:
1169 case KVM_S390_VM_CRYPTO_DISABLE_AES_KW
:
1170 case KVM_S390_VM_CRYPTO_DISABLE_DEA_KW
:
1186 static long kvm_s390_get_skeys(struct kvm
*kvm
, struct kvm_s390_skeys
*args
)
1192 if (args
->flags
!= 0)
1195 /* Is this guest using storage keys? */
1196 if (!mm_use_skey(current
->mm
))
1197 return KVM_S390_GET_SKEYS_NONE
;
1199 /* Enforce sane limit on memory allocation */
1200 if (args
->count
< 1 || args
->count
> KVM_S390_SKEYS_MAX
)
1203 keys
= kmalloc_array(args
->count
, sizeof(uint8_t),
1204 GFP_KERNEL
| __GFP_NOWARN
);
1206 keys
= vmalloc(sizeof(uint8_t) * args
->count
);
1210 down_read(¤t
->mm
->mmap_sem
);
1211 for (i
= 0; i
< args
->count
; i
++) {
1212 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1213 if (kvm_is_error_hva(hva
)) {
1218 r
= get_guest_storage_key(current
->mm
, hva
, &keys
[i
]);
1222 up_read(¤t
->mm
->mmap_sem
);
1225 r
= copy_to_user((uint8_t __user
*)args
->skeydata_addr
, keys
,
1226 sizeof(uint8_t) * args
->count
);
1235 static long kvm_s390_set_skeys(struct kvm
*kvm
, struct kvm_s390_skeys
*args
)
1241 if (args
->flags
!= 0)
1244 /* Enforce sane limit on memory allocation */
1245 if (args
->count
< 1 || args
->count
> KVM_S390_SKEYS_MAX
)
1248 keys
= kmalloc_array(args
->count
, sizeof(uint8_t),
1249 GFP_KERNEL
| __GFP_NOWARN
);
1251 keys
= vmalloc(sizeof(uint8_t) * args
->count
);
1255 r
= copy_from_user(keys
, (uint8_t __user
*)args
->skeydata_addr
,
1256 sizeof(uint8_t) * args
->count
);
1262 /* Enable storage key handling for the guest */
1263 r
= s390_enable_skey();
1267 down_read(¤t
->mm
->mmap_sem
);
1268 for (i
= 0; i
< args
->count
; i
++) {
1269 hva
= gfn_to_hva(kvm
, args
->start_gfn
+ i
);
1270 if (kvm_is_error_hva(hva
)) {
1275 /* Lowest order bit is reserved */
1276 if (keys
[i
] & 0x01) {
1281 r
= set_guest_storage_key(current
->mm
, hva
, keys
[i
], 0);
1285 up_read(¤t
->mm
->mmap_sem
);
1291 long kvm_arch_vm_ioctl(struct file
*filp
,
1292 unsigned int ioctl
, unsigned long arg
)
1294 struct kvm
*kvm
= filp
->private_data
;
1295 void __user
*argp
= (void __user
*)arg
;
1296 struct kvm_device_attr attr
;
1300 case KVM_S390_INTERRUPT
: {
1301 struct kvm_s390_interrupt s390int
;
1304 if (copy_from_user(&s390int
, argp
, sizeof(s390int
)))
1306 r
= kvm_s390_inject_vm(kvm
, &s390int
);
1309 case KVM_ENABLE_CAP
: {
1310 struct kvm_enable_cap cap
;
1312 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
1314 r
= kvm_vm_ioctl_enable_cap(kvm
, &cap
);
1317 case KVM_CREATE_IRQCHIP
: {
1318 struct kvm_irq_routing_entry routing
;
1321 if (kvm
->arch
.use_irqchip
) {
1322 /* Set up dummy routing. */
1323 memset(&routing
, 0, sizeof(routing
));
1324 r
= kvm_set_irq_routing(kvm
, &routing
, 0, 0);
1328 case KVM_SET_DEVICE_ATTR
: {
1330 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
1332 r
= kvm_s390_vm_set_attr(kvm
, &attr
);
1335 case KVM_GET_DEVICE_ATTR
: {
1337 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
1339 r
= kvm_s390_vm_get_attr(kvm
, &attr
);
1342 case KVM_HAS_DEVICE_ATTR
: {
1344 if (copy_from_user(&attr
, (void __user
*)arg
, sizeof(attr
)))
1346 r
= kvm_s390_vm_has_attr(kvm
, &attr
);
1349 case KVM_S390_GET_SKEYS
: {
1350 struct kvm_s390_skeys args
;
1353 if (copy_from_user(&args
, argp
,
1354 sizeof(struct kvm_s390_skeys
)))
1356 r
= kvm_s390_get_skeys(kvm
, &args
);
1359 case KVM_S390_SET_SKEYS
: {
1360 struct kvm_s390_skeys args
;
1363 if (copy_from_user(&args
, argp
,
1364 sizeof(struct kvm_s390_skeys
)))
1366 r
= kvm_s390_set_skeys(kvm
, &args
);
1376 static int kvm_s390_query_ap_config(u8
*config
)
1378 u32 fcn_code
= 0x04000000UL
;
1381 memset(config
, 0, 128);
1385 ".long 0xb2af0000\n" /* PQAP(QCI) */
1391 : "r" (fcn_code
), "r" (config
)
1392 : "cc", "0", "2", "memory"
1398 static int kvm_s390_apxa_installed(void)
1403 if (test_facility(12)) {
1404 cc
= kvm_s390_query_ap_config(config
);
1407 pr_err("PQAP(QCI) failed with cc=%d", cc
);
1409 return config
[0] & 0x40;
1415 static void kvm_s390_set_crycb_format(struct kvm
*kvm
)
1417 kvm
->arch
.crypto
.crycbd
= (__u32
)(unsigned long) kvm
->arch
.crypto
.crycb
;
1419 if (kvm_s390_apxa_installed())
1420 kvm
->arch
.crypto
.crycbd
|= CRYCB_FORMAT2
;
1422 kvm
->arch
.crypto
.crycbd
|= CRYCB_FORMAT1
;
1425 static u64
kvm_s390_get_initial_cpuid(void)
1430 cpuid
.version
= 0xff;
1431 return *((u64
*) &cpuid
);
1434 static void kvm_s390_crypto_init(struct kvm
*kvm
)
1436 if (!test_kvm_facility(kvm
, 76))
1439 kvm
->arch
.crypto
.crycb
= &kvm
->arch
.sie_page2
->crycb
;
1440 kvm_s390_set_crycb_format(kvm
);
1442 /* Enable AES/DEA protected key functions by default */
1443 kvm
->arch
.crypto
.aes_kw
= 1;
1444 kvm
->arch
.crypto
.dea_kw
= 1;
1445 get_random_bytes(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
,
1446 sizeof(kvm
->arch
.crypto
.crycb
->aes_wrapping_key_mask
));
1447 get_random_bytes(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
,
1448 sizeof(kvm
->arch
.crypto
.crycb
->dea_wrapping_key_mask
));
1451 static void sca_dispose(struct kvm
*kvm
)
1453 if (kvm
->arch
.use_esca
)
1454 free_pages_exact(kvm
->arch
.sca
, sizeof(struct esca_block
));
1456 free_page((unsigned long)(kvm
->arch
.sca
));
1457 kvm
->arch
.sca
= NULL
;
1460 int kvm_arch_init_vm(struct kvm
*kvm
, unsigned long type
)
1462 gfp_t alloc_flags
= GFP_KERNEL
;
1464 char debug_name
[16];
1465 static unsigned long sca_offset
;
1468 #ifdef CONFIG_KVM_S390_UCONTROL
1469 if (type
& ~KVM_VM_S390_UCONTROL
)
1471 if ((type
& KVM_VM_S390_UCONTROL
) && (!capable(CAP_SYS_ADMIN
)))
1478 rc
= s390_enable_sie();
1484 ratelimit_state_init(&kvm
->arch
.sthyi_limit
, 5 * HZ
, 500);
1486 kvm
->arch
.use_esca
= 0; /* start with basic SCA */
1487 if (!sclp
.has_64bscao
)
1488 alloc_flags
|= GFP_DMA
;
1489 rwlock_init(&kvm
->arch
.sca_lock
);
1490 kvm
->arch
.sca
= (struct bsca_block
*) get_zeroed_page(alloc_flags
);
1493 spin_lock(&kvm_lock
);
1495 if (sca_offset
+ sizeof(struct bsca_block
) > PAGE_SIZE
)
1497 kvm
->arch
.sca
= (struct bsca_block
*)
1498 ((char *) kvm
->arch
.sca
+ sca_offset
);
1499 spin_unlock(&kvm_lock
);
1501 sprintf(debug_name
, "kvm-%u", current
->pid
);
1503 kvm
->arch
.dbf
= debug_register(debug_name
, 32, 1, 7 * sizeof(long));
1507 kvm
->arch
.sie_page2
=
1508 (struct sie_page2
*) get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
1509 if (!kvm
->arch
.sie_page2
)
1512 /* Populate the facility mask initially. */
1513 memcpy(kvm
->arch
.model
.fac_mask
, S390_lowcore
.stfle_fac_list
,
1514 sizeof(S390_lowcore
.stfle_fac_list
));
1515 for (i
= 0; i
< S390_ARCH_FAC_LIST_SIZE_U64
; i
++) {
1516 if (i
< kvm_s390_fac_list_mask_size())
1517 kvm
->arch
.model
.fac_mask
[i
] &= kvm_s390_fac_list_mask
[i
];
1519 kvm
->arch
.model
.fac_mask
[i
] = 0UL;
1522 /* Populate the facility list initially. */
1523 kvm
->arch
.model
.fac_list
= kvm
->arch
.sie_page2
->fac_list
;
1524 memcpy(kvm
->arch
.model
.fac_list
, kvm
->arch
.model
.fac_mask
,
1525 S390_ARCH_FAC_LIST_SIZE_BYTE
);
1527 set_kvm_facility(kvm
->arch
.model
.fac_mask
, 74);
1528 set_kvm_facility(kvm
->arch
.model
.fac_list
, 74);
1530 kvm
->arch
.model
.cpuid
= kvm_s390_get_initial_cpuid();
1531 kvm
->arch
.model
.ibc
= sclp
.ibc
& 0x0fff;
1533 kvm_s390_crypto_init(kvm
);
1535 mutex_init(&kvm
->arch
.float_int
.ais_lock
);
1536 kvm
->arch
.float_int
.simm
= 0;
1537 kvm
->arch
.float_int
.nimm
= 0;
1538 kvm
->arch
.float_int
.ais_enabled
= 0;
1539 spin_lock_init(&kvm
->arch
.float_int
.lock
);
1540 for (i
= 0; i
< FIRQ_LIST_COUNT
; i
++)
1541 INIT_LIST_HEAD(&kvm
->arch
.float_int
.lists
[i
]);
1542 init_waitqueue_head(&kvm
->arch
.ipte_wq
);
1543 mutex_init(&kvm
->arch
.ipte_mutex
);
1545 debug_register_view(kvm
->arch
.dbf
, &debug_sprintf_view
);
1546 VM_EVENT(kvm
, 3, "vm created with type %lu", type
);
1548 if (type
& KVM_VM_S390_UCONTROL
) {
1549 kvm
->arch
.gmap
= NULL
;
1550 kvm
->arch
.mem_limit
= KVM_S390_NO_MEM_LIMIT
;
1552 if (sclp
.hamax
== U64_MAX
)
1553 kvm
->arch
.mem_limit
= TASK_MAX_SIZE
;
1555 kvm
->arch
.mem_limit
= min_t(unsigned long, TASK_MAX_SIZE
,
1557 kvm
->arch
.gmap
= gmap_create(current
->mm
, kvm
->arch
.mem_limit
- 1);
1558 if (!kvm
->arch
.gmap
)
1560 kvm
->arch
.gmap
->private = kvm
;
1561 kvm
->arch
.gmap
->pfault_enabled
= 0;
1564 kvm
->arch
.css_support
= 0;
1565 kvm
->arch
.use_irqchip
= 0;
1566 kvm
->arch
.epoch
= 0;
1568 spin_lock_init(&kvm
->arch
.start_stop_lock
);
1569 kvm_s390_vsie_init(kvm
);
1570 KVM_EVENT(3, "vm 0x%pK created by pid %u", kvm
, current
->pid
);
1574 free_page((unsigned long)kvm
->arch
.sie_page2
);
1575 debug_unregister(kvm
->arch
.dbf
);
1577 KVM_EVENT(3, "creation of vm failed: %d", rc
);
1581 bool kvm_arch_has_vcpu_debugfs(void)
1586 int kvm_arch_create_vcpu_debugfs(struct kvm_vcpu
*vcpu
)
1591 void kvm_arch_vcpu_destroy(struct kvm_vcpu
*vcpu
)
1593 VCPU_EVENT(vcpu
, 3, "%s", "free cpu");
1594 trace_kvm_s390_destroy_vcpu(vcpu
->vcpu_id
);
1595 kvm_s390_clear_local_irqs(vcpu
);
1596 kvm_clear_async_pf_completion_queue(vcpu
);
1597 if (!kvm_is_ucontrol(vcpu
->kvm
))
1600 if (kvm_is_ucontrol(vcpu
->kvm
))
1601 gmap_remove(vcpu
->arch
.gmap
);
1603 if (vcpu
->kvm
->arch
.use_cmma
)
1604 kvm_s390_vcpu_unsetup_cmma(vcpu
);
1605 free_page((unsigned long)(vcpu
->arch
.sie_block
));
1607 kvm_vcpu_uninit(vcpu
);
1608 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
1611 static void kvm_free_vcpus(struct kvm
*kvm
)
1614 struct kvm_vcpu
*vcpu
;
1616 kvm_for_each_vcpu(i
, vcpu
, kvm
)
1617 kvm_arch_vcpu_destroy(vcpu
);
1619 mutex_lock(&kvm
->lock
);
1620 for (i
= 0; i
< atomic_read(&kvm
->online_vcpus
); i
++)
1621 kvm
->vcpus
[i
] = NULL
;
1623 atomic_set(&kvm
->online_vcpus
, 0);
1624 mutex_unlock(&kvm
->lock
);
1627 void kvm_arch_destroy_vm(struct kvm
*kvm
)
1629 kvm_free_vcpus(kvm
);
1631 debug_unregister(kvm
->arch
.dbf
);
1632 free_page((unsigned long)kvm
->arch
.sie_page2
);
1633 if (!kvm_is_ucontrol(kvm
))
1634 gmap_remove(kvm
->arch
.gmap
);
1635 kvm_s390_destroy_adapters(kvm
);
1636 kvm_s390_clear_float_irqs(kvm
);
1637 kvm_s390_vsie_destroy(kvm
);
1638 KVM_EVENT(3, "vm 0x%pK destroyed", kvm
);
1641 /* Section: vcpu related */
1642 static int __kvm_ucontrol_vcpu_init(struct kvm_vcpu
*vcpu
)
1644 vcpu
->arch
.gmap
= gmap_create(current
->mm
, -1UL);
1645 if (!vcpu
->arch
.gmap
)
1647 vcpu
->arch
.gmap
->private = vcpu
->kvm
;
1652 static void sca_del_vcpu(struct kvm_vcpu
*vcpu
)
1654 if (!kvm_s390_use_sca_entries())
1656 read_lock(&vcpu
->kvm
->arch
.sca_lock
);
1657 if (vcpu
->kvm
->arch
.use_esca
) {
1658 struct esca_block
*sca
= vcpu
->kvm
->arch
.sca
;
1660 clear_bit_inv(vcpu
->vcpu_id
, (unsigned long *) sca
->mcn
);
1661 sca
->cpu
[vcpu
->vcpu_id
].sda
= 0;
1663 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
1665 clear_bit_inv(vcpu
->vcpu_id
, (unsigned long *) &sca
->mcn
);
1666 sca
->cpu
[vcpu
->vcpu_id
].sda
= 0;
1668 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
1671 static void sca_add_vcpu(struct kvm_vcpu
*vcpu
)
1673 if (!kvm_s390_use_sca_entries()) {
1674 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
1676 /* we still need the basic sca for the ipte control */
1677 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
1678 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
;
1680 read_lock(&vcpu
->kvm
->arch
.sca_lock
);
1681 if (vcpu
->kvm
->arch
.use_esca
) {
1682 struct esca_block
*sca
= vcpu
->kvm
->arch
.sca
;
1684 sca
->cpu
[vcpu
->vcpu_id
].sda
= (__u64
) vcpu
->arch
.sie_block
;
1685 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
1686 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
& ~0x3fU
;
1687 vcpu
->arch
.sie_block
->ecb2
|= ECB2_ESCA
;
1688 set_bit_inv(vcpu
->vcpu_id
, (unsigned long *) sca
->mcn
);
1690 struct bsca_block
*sca
= vcpu
->kvm
->arch
.sca
;
1692 sca
->cpu
[vcpu
->vcpu_id
].sda
= (__u64
) vcpu
->arch
.sie_block
;
1693 vcpu
->arch
.sie_block
->scaoh
= (__u32
)(((__u64
)sca
) >> 32);
1694 vcpu
->arch
.sie_block
->scaol
= (__u32
)(__u64
)sca
;
1695 set_bit_inv(vcpu
->vcpu_id
, (unsigned long *) &sca
->mcn
);
1697 read_unlock(&vcpu
->kvm
->arch
.sca_lock
);
1700 /* Basic SCA to Extended SCA data copy routines */
1701 static inline void sca_copy_entry(struct esca_entry
*d
, struct bsca_entry
*s
)
1704 d
->sigp_ctrl
.c
= s
->sigp_ctrl
.c
;
1705 d
->sigp_ctrl
.scn
= s
->sigp_ctrl
.scn
;
1708 static void sca_copy_b_to_e(struct esca_block
*d
, struct bsca_block
*s
)
1712 d
->ipte_control
= s
->ipte_control
;
1714 for (i
= 0; i
< KVM_S390_BSCA_CPU_SLOTS
; i
++)
1715 sca_copy_entry(&d
->cpu
[i
], &s
->cpu
[i
]);
1718 static int sca_switch_to_extended(struct kvm
*kvm
)
1720 struct bsca_block
*old_sca
= kvm
->arch
.sca
;
1721 struct esca_block
*new_sca
;
1722 struct kvm_vcpu
*vcpu
;
1723 unsigned int vcpu_idx
;
1726 new_sca
= alloc_pages_exact(sizeof(*new_sca
), GFP_KERNEL
|__GFP_ZERO
);
1730 scaoh
= (u32
)((u64
)(new_sca
) >> 32);
1731 scaol
= (u32
)(u64
)(new_sca
) & ~0x3fU
;
1733 kvm_s390_vcpu_block_all(kvm
);
1734 write_lock(&kvm
->arch
.sca_lock
);
1736 sca_copy_b_to_e(new_sca
, old_sca
);
1738 kvm_for_each_vcpu(vcpu_idx
, vcpu
, kvm
) {
1739 vcpu
->arch
.sie_block
->scaoh
= scaoh
;
1740 vcpu
->arch
.sie_block
->scaol
= scaol
;
1741 vcpu
->arch
.sie_block
->ecb2
|= ECB2_ESCA
;
1743 kvm
->arch
.sca
= new_sca
;
1744 kvm
->arch
.use_esca
= 1;
1746 write_unlock(&kvm
->arch
.sca_lock
);
1747 kvm_s390_vcpu_unblock_all(kvm
);
1749 free_page((unsigned long)old_sca
);
1751 VM_EVENT(kvm
, 2, "Switched to ESCA (0x%pK -> 0x%pK)",
1752 old_sca
, kvm
->arch
.sca
);
1756 static int sca_can_add_vcpu(struct kvm
*kvm
, unsigned int id
)
1760 if (!kvm_s390_use_sca_entries()) {
1761 if (id
< KVM_MAX_VCPUS
)
1765 if (id
< KVM_S390_BSCA_CPU_SLOTS
)
1767 if (!sclp
.has_esca
|| !sclp
.has_64bscao
)
1770 mutex_lock(&kvm
->lock
);
1771 rc
= kvm
->arch
.use_esca
? 0 : sca_switch_to_extended(kvm
);
1772 mutex_unlock(&kvm
->lock
);
1774 return rc
== 0 && id
< KVM_S390_ESCA_CPU_SLOTS
;
1777 int kvm_arch_vcpu_init(struct kvm_vcpu
*vcpu
)
1779 vcpu
->arch
.pfault_token
= KVM_S390_PFAULT_TOKEN_INVALID
;
1780 kvm_clear_async_pf_completion_queue(vcpu
);
1781 vcpu
->run
->kvm_valid_regs
= KVM_SYNC_PREFIX
|
1787 kvm_s390_set_prefix(vcpu
, 0);
1788 if (test_kvm_facility(vcpu
->kvm
, 64))
1789 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_RICCB
;
1790 if (test_kvm_facility(vcpu
->kvm
, 133))
1791 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_GSCB
;
1792 /* fprs can be synchronized via vrs, even if the guest has no vx. With
1793 * MACHINE_HAS_VX, (load|store)_fpu_regs() will work with vrs format.
1796 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_VRS
;
1798 vcpu
->run
->kvm_valid_regs
|= KVM_SYNC_FPRS
;
1800 if (kvm_is_ucontrol(vcpu
->kvm
))
1801 return __kvm_ucontrol_vcpu_init(vcpu
);
1806 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1807 static void __start_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
1809 WARN_ON_ONCE(vcpu
->arch
.cputm_start
!= 0);
1810 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
1811 vcpu
->arch
.cputm_start
= get_tod_clock_fast();
1812 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
1815 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1816 static void __stop_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
1818 WARN_ON_ONCE(vcpu
->arch
.cputm_start
== 0);
1819 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
1820 vcpu
->arch
.sie_block
->cputm
-= get_tod_clock_fast() - vcpu
->arch
.cputm_start
;
1821 vcpu
->arch
.cputm_start
= 0;
1822 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
1825 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1826 static void __enable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
1828 WARN_ON_ONCE(vcpu
->arch
.cputm_enabled
);
1829 vcpu
->arch
.cputm_enabled
= true;
1830 __start_cpu_timer_accounting(vcpu
);
1833 /* needs disabled preemption to protect from TOD sync and vcpu_load/put */
1834 static void __disable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
1836 WARN_ON_ONCE(!vcpu
->arch
.cputm_enabled
);
1837 __stop_cpu_timer_accounting(vcpu
);
1838 vcpu
->arch
.cputm_enabled
= false;
1841 static void enable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
1843 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1844 __enable_cpu_timer_accounting(vcpu
);
1848 static void disable_cpu_timer_accounting(struct kvm_vcpu
*vcpu
)
1850 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1851 __disable_cpu_timer_accounting(vcpu
);
1855 /* set the cpu timer - may only be called from the VCPU thread itself */
1856 void kvm_s390_set_cpu_timer(struct kvm_vcpu
*vcpu
, __u64 cputm
)
1858 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1859 raw_write_seqcount_begin(&vcpu
->arch
.cputm_seqcount
);
1860 if (vcpu
->arch
.cputm_enabled
)
1861 vcpu
->arch
.cputm_start
= get_tod_clock_fast();
1862 vcpu
->arch
.sie_block
->cputm
= cputm
;
1863 raw_write_seqcount_end(&vcpu
->arch
.cputm_seqcount
);
1867 /* update and get the cpu timer - can also be called from other VCPU threads */
1868 __u64
kvm_s390_get_cpu_timer(struct kvm_vcpu
*vcpu
)
1873 if (unlikely(!vcpu
->arch
.cputm_enabled
))
1874 return vcpu
->arch
.sie_block
->cputm
;
1876 preempt_disable(); /* protect from TOD sync and vcpu_load/put */
1878 seq
= raw_read_seqcount(&vcpu
->arch
.cputm_seqcount
);
1880 * If the writer would ever execute a read in the critical
1881 * section, e.g. in irq context, we have a deadlock.
1883 WARN_ON_ONCE((seq
& 1) && smp_processor_id() == vcpu
->cpu
);
1884 value
= vcpu
->arch
.sie_block
->cputm
;
1885 /* if cputm_start is 0, accounting is being started/stopped */
1886 if (likely(vcpu
->arch
.cputm_start
))
1887 value
-= get_tod_clock_fast() - vcpu
->arch
.cputm_start
;
1888 } while (read_seqcount_retry(&vcpu
->arch
.cputm_seqcount
, seq
& ~1));
1893 void kvm_arch_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
1896 gmap_enable(vcpu
->arch
.enabled_gmap
);
1897 atomic_or(CPUSTAT_RUNNING
, &vcpu
->arch
.sie_block
->cpuflags
);
1898 if (vcpu
->arch
.cputm_enabled
&& !is_vcpu_idle(vcpu
))
1899 __start_cpu_timer_accounting(vcpu
);
1903 void kvm_arch_vcpu_put(struct kvm_vcpu
*vcpu
)
1906 if (vcpu
->arch
.cputm_enabled
&& !is_vcpu_idle(vcpu
))
1907 __stop_cpu_timer_accounting(vcpu
);
1908 atomic_andnot(CPUSTAT_RUNNING
, &vcpu
->arch
.sie_block
->cpuflags
);
1909 vcpu
->arch
.enabled_gmap
= gmap_get_enabled();
1910 gmap_disable(vcpu
->arch
.enabled_gmap
);
1914 static void kvm_s390_vcpu_initial_reset(struct kvm_vcpu
*vcpu
)
1916 /* this equals initial cpu reset in pop, but we don't switch to ESA */
1917 vcpu
->arch
.sie_block
->gpsw
.mask
= 0UL;
1918 vcpu
->arch
.sie_block
->gpsw
.addr
= 0UL;
1919 kvm_s390_set_prefix(vcpu
, 0);
1920 kvm_s390_set_cpu_timer(vcpu
, 0);
1921 vcpu
->arch
.sie_block
->ckc
= 0UL;
1922 vcpu
->arch
.sie_block
->todpr
= 0;
1923 memset(vcpu
->arch
.sie_block
->gcr
, 0, 16 * sizeof(__u64
));
1924 vcpu
->arch
.sie_block
->gcr
[0] = 0xE0UL
;
1925 vcpu
->arch
.sie_block
->gcr
[14] = 0xC2000000UL
;
1926 /* make sure the new fpc will be lazily loaded */
1928 current
->thread
.fpu
.fpc
= 0;
1929 vcpu
->arch
.sie_block
->gbea
= 1;
1930 vcpu
->arch
.sie_block
->pp
= 0;
1931 vcpu
->arch
.pfault_token
= KVM_S390_PFAULT_TOKEN_INVALID
;
1932 kvm_clear_async_pf_completion_queue(vcpu
);
1933 if (!kvm_s390_user_cpu_state_ctrl(vcpu
->kvm
))
1934 kvm_s390_vcpu_stop(vcpu
);
1935 kvm_s390_clear_local_irqs(vcpu
);
1938 void kvm_arch_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
1940 mutex_lock(&vcpu
->kvm
->lock
);
1942 vcpu
->arch
.sie_block
->epoch
= vcpu
->kvm
->arch
.epoch
;
1944 mutex_unlock(&vcpu
->kvm
->lock
);
1945 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
1946 vcpu
->arch
.gmap
= vcpu
->kvm
->arch
.gmap
;
1949 if (test_kvm_facility(vcpu
->kvm
, 74) || vcpu
->kvm
->arch
.user_instr0
)
1950 vcpu
->arch
.sie_block
->ictl
|= ICTL_OPEREXC
;
1951 /* make vcpu_load load the right gmap on the first trigger */
1952 vcpu
->arch
.enabled_gmap
= vcpu
->arch
.gmap
;
1955 static void kvm_s390_vcpu_crypto_setup(struct kvm_vcpu
*vcpu
)
1957 if (!test_kvm_facility(vcpu
->kvm
, 76))
1960 vcpu
->arch
.sie_block
->ecb3
&= ~(ECB3_AES
| ECB3_DEA
);
1962 if (vcpu
->kvm
->arch
.crypto
.aes_kw
)
1963 vcpu
->arch
.sie_block
->ecb3
|= ECB3_AES
;
1964 if (vcpu
->kvm
->arch
.crypto
.dea_kw
)
1965 vcpu
->arch
.sie_block
->ecb3
|= ECB3_DEA
;
1967 vcpu
->arch
.sie_block
->crycbd
= vcpu
->kvm
->arch
.crypto
.crycbd
;
1970 void kvm_s390_vcpu_unsetup_cmma(struct kvm_vcpu
*vcpu
)
1972 free_page(vcpu
->arch
.sie_block
->cbrlo
);
1973 vcpu
->arch
.sie_block
->cbrlo
= 0;
1976 int kvm_s390_vcpu_setup_cmma(struct kvm_vcpu
*vcpu
)
1978 vcpu
->arch
.sie_block
->cbrlo
= get_zeroed_page(GFP_KERNEL
);
1979 if (!vcpu
->arch
.sie_block
->cbrlo
)
1982 vcpu
->arch
.sie_block
->ecb2
|= ECB2_CMMA
;
1983 vcpu
->arch
.sie_block
->ecb2
&= ~ECB2_PFMFI
;
1987 static void kvm_s390_vcpu_setup_model(struct kvm_vcpu
*vcpu
)
1989 struct kvm_s390_cpu_model
*model
= &vcpu
->kvm
->arch
.model
;
1991 vcpu
->arch
.sie_block
->ibc
= model
->ibc
;
1992 if (test_kvm_facility(vcpu
->kvm
, 7))
1993 vcpu
->arch
.sie_block
->fac
= (u32
)(u64
) model
->fac_list
;
1996 int kvm_arch_vcpu_setup(struct kvm_vcpu
*vcpu
)
2000 atomic_set(&vcpu
->arch
.sie_block
->cpuflags
, CPUSTAT_ZARCH
|
2004 if (test_kvm_facility(vcpu
->kvm
, 78))
2005 atomic_or(CPUSTAT_GED2
, &vcpu
->arch
.sie_block
->cpuflags
);
2006 else if (test_kvm_facility(vcpu
->kvm
, 8))
2007 atomic_or(CPUSTAT_GED
, &vcpu
->arch
.sie_block
->cpuflags
);
2009 kvm_s390_vcpu_setup_model(vcpu
);
2011 /* pgste_set_pte has special handling for !MACHINE_HAS_ESOP */
2012 if (MACHINE_HAS_ESOP
)
2013 vcpu
->arch
.sie_block
->ecb
|= ECB_HOSTPROTINT
;
2014 if (test_kvm_facility(vcpu
->kvm
, 9))
2015 vcpu
->arch
.sie_block
->ecb
|= ECB_SRSI
;
2016 if (test_kvm_facility(vcpu
->kvm
, 73))
2017 vcpu
->arch
.sie_block
->ecb
|= ECB_TE
;
2019 if (test_kvm_facility(vcpu
->kvm
, 8) && sclp
.has_pfmfi
)
2020 vcpu
->arch
.sie_block
->ecb2
|= ECB2_PFMFI
;
2021 if (test_kvm_facility(vcpu
->kvm
, 130))
2022 vcpu
->arch
.sie_block
->ecb2
|= ECB2_IEP
;
2023 vcpu
->arch
.sie_block
->eca
= ECA_MVPGI
| ECA_PROTEXCI
;
2025 vcpu
->arch
.sie_block
->eca
|= ECA_CEI
;
2027 vcpu
->arch
.sie_block
->eca
|= ECA_IB
;
2029 vcpu
->arch
.sie_block
->eca
|= ECA_SII
;
2030 if (sclp
.has_sigpif
)
2031 vcpu
->arch
.sie_block
->eca
|= ECA_SIGPI
;
2032 if (test_kvm_facility(vcpu
->kvm
, 129)) {
2033 vcpu
->arch
.sie_block
->eca
|= ECA_VX
;
2034 vcpu
->arch
.sie_block
->ecd
|= ECD_HOSTREGMGMT
;
2036 vcpu
->arch
.sie_block
->sdnxo
= ((unsigned long) &vcpu
->run
->s
.regs
.sdnx
)
2038 vcpu
->arch
.sie_block
->riccbd
= (unsigned long) &vcpu
->run
->s
.regs
.riccb
;
2041 atomic_or(CPUSTAT_KSS
, &vcpu
->arch
.sie_block
->cpuflags
);
2043 vcpu
->arch
.sie_block
->ictl
|= ICTL_ISKE
| ICTL_SSKE
| ICTL_RRBE
;
2045 if (vcpu
->kvm
->arch
.use_cmma
) {
2046 rc
= kvm_s390_vcpu_setup_cmma(vcpu
);
2050 hrtimer_init(&vcpu
->arch
.ckc_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2051 vcpu
->arch
.ckc_timer
.function
= kvm_s390_idle_wakeup
;
2053 kvm_s390_vcpu_crypto_setup(vcpu
);
2058 struct kvm_vcpu
*kvm_arch_vcpu_create(struct kvm
*kvm
,
2061 struct kvm_vcpu
*vcpu
;
2062 struct sie_page
*sie_page
;
2065 if (!kvm_is_ucontrol(kvm
) && !sca_can_add_vcpu(kvm
, id
))
2070 vcpu
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
2074 sie_page
= (struct sie_page
*) get_zeroed_page(GFP_KERNEL
);
2078 vcpu
->arch
.sie_block
= &sie_page
->sie_block
;
2079 vcpu
->arch
.sie_block
->itdba
= (unsigned long) &sie_page
->itdb
;
2081 /* the real guest size will always be smaller than msl */
2082 vcpu
->arch
.sie_block
->mso
= 0;
2083 vcpu
->arch
.sie_block
->msl
= sclp
.hamax
;
2085 vcpu
->arch
.sie_block
->icpua
= id
;
2086 spin_lock_init(&vcpu
->arch
.local_int
.lock
);
2087 vcpu
->arch
.local_int
.float_int
= &kvm
->arch
.float_int
;
2088 vcpu
->arch
.local_int
.wq
= &vcpu
->wq
;
2089 vcpu
->arch
.local_int
.cpuflags
= &vcpu
->arch
.sie_block
->cpuflags
;
2090 seqcount_init(&vcpu
->arch
.cputm_seqcount
);
2092 rc
= kvm_vcpu_init(vcpu
, kvm
, id
);
2094 goto out_free_sie_block
;
2095 VM_EVENT(kvm
, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id
, vcpu
,
2096 vcpu
->arch
.sie_block
);
2097 trace_kvm_s390_create_vcpu(id
, vcpu
, vcpu
->arch
.sie_block
);
2101 free_page((unsigned long)(vcpu
->arch
.sie_block
));
2103 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
2108 int kvm_arch_vcpu_runnable(struct kvm_vcpu
*vcpu
)
2110 return kvm_s390_vcpu_has_irq(vcpu
, 0);
2113 void kvm_s390_vcpu_block(struct kvm_vcpu
*vcpu
)
2115 atomic_or(PROG_BLOCK_SIE
, &vcpu
->arch
.sie_block
->prog20
);
2119 void kvm_s390_vcpu_unblock(struct kvm_vcpu
*vcpu
)
2121 atomic_andnot(PROG_BLOCK_SIE
, &vcpu
->arch
.sie_block
->prog20
);
2124 static void kvm_s390_vcpu_request(struct kvm_vcpu
*vcpu
)
2126 atomic_or(PROG_REQUEST
, &vcpu
->arch
.sie_block
->prog20
);
2130 static void kvm_s390_vcpu_request_handled(struct kvm_vcpu
*vcpu
)
2132 atomic_andnot(PROG_REQUEST
, &vcpu
->arch
.sie_block
->prog20
);
2136 * Kick a guest cpu out of SIE and wait until SIE is not running.
2137 * If the CPU is not running (e.g. waiting as idle) the function will
2138 * return immediately. */
2139 void exit_sie(struct kvm_vcpu
*vcpu
)
2141 atomic_or(CPUSTAT_STOP_INT
, &vcpu
->arch
.sie_block
->cpuflags
);
2142 while (vcpu
->arch
.sie_block
->prog0c
& PROG_IN_SIE
)
2146 /* Kick a guest cpu out of SIE to process a request synchronously */
2147 void kvm_s390_sync_request(int req
, struct kvm_vcpu
*vcpu
)
2149 kvm_make_request(req
, vcpu
);
2150 kvm_s390_vcpu_request(vcpu
);
2153 static void kvm_gmap_notifier(struct gmap
*gmap
, unsigned long start
,
2156 struct kvm
*kvm
= gmap
->private;
2157 struct kvm_vcpu
*vcpu
;
2158 unsigned long prefix
;
2161 if (gmap_is_shadow(gmap
))
2163 if (start
>= 1UL << 31)
2164 /* We are only interested in prefix pages */
2166 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
2167 /* match against both prefix pages */
2168 prefix
= kvm_s390_get_prefix(vcpu
);
2169 if (prefix
<= end
&& start
<= prefix
+ 2*PAGE_SIZE
- 1) {
2170 VCPU_EVENT(vcpu
, 2, "gmap notifier for %lx-%lx",
2172 kvm_s390_sync_request(KVM_REQ_MMU_RELOAD
, vcpu
);
2177 int kvm_arch_vcpu_should_kick(struct kvm_vcpu
*vcpu
)
2179 /* kvm common code refers to this, but never calls it */
2184 static int kvm_arch_vcpu_ioctl_get_one_reg(struct kvm_vcpu
*vcpu
,
2185 struct kvm_one_reg
*reg
)
2190 case KVM_REG_S390_TODPR
:
2191 r
= put_user(vcpu
->arch
.sie_block
->todpr
,
2192 (u32 __user
*)reg
->addr
);
2194 case KVM_REG_S390_EPOCHDIFF
:
2195 r
= put_user(vcpu
->arch
.sie_block
->epoch
,
2196 (u64 __user
*)reg
->addr
);
2198 case KVM_REG_S390_CPU_TIMER
:
2199 r
= put_user(kvm_s390_get_cpu_timer(vcpu
),
2200 (u64 __user
*)reg
->addr
);
2202 case KVM_REG_S390_CLOCK_COMP
:
2203 r
= put_user(vcpu
->arch
.sie_block
->ckc
,
2204 (u64 __user
*)reg
->addr
);
2206 case KVM_REG_S390_PFTOKEN
:
2207 r
= put_user(vcpu
->arch
.pfault_token
,
2208 (u64 __user
*)reg
->addr
);
2210 case KVM_REG_S390_PFCOMPARE
:
2211 r
= put_user(vcpu
->arch
.pfault_compare
,
2212 (u64 __user
*)reg
->addr
);
2214 case KVM_REG_S390_PFSELECT
:
2215 r
= put_user(vcpu
->arch
.pfault_select
,
2216 (u64 __user
*)reg
->addr
);
2218 case KVM_REG_S390_PP
:
2219 r
= put_user(vcpu
->arch
.sie_block
->pp
,
2220 (u64 __user
*)reg
->addr
);
2222 case KVM_REG_S390_GBEA
:
2223 r
= put_user(vcpu
->arch
.sie_block
->gbea
,
2224 (u64 __user
*)reg
->addr
);
2233 static int kvm_arch_vcpu_ioctl_set_one_reg(struct kvm_vcpu
*vcpu
,
2234 struct kvm_one_reg
*reg
)
2240 case KVM_REG_S390_TODPR
:
2241 r
= get_user(vcpu
->arch
.sie_block
->todpr
,
2242 (u32 __user
*)reg
->addr
);
2244 case KVM_REG_S390_EPOCHDIFF
:
2245 r
= get_user(vcpu
->arch
.sie_block
->epoch
,
2246 (u64 __user
*)reg
->addr
);
2248 case KVM_REG_S390_CPU_TIMER
:
2249 r
= get_user(val
, (u64 __user
*)reg
->addr
);
2251 kvm_s390_set_cpu_timer(vcpu
, val
);
2253 case KVM_REG_S390_CLOCK_COMP
:
2254 r
= get_user(vcpu
->arch
.sie_block
->ckc
,
2255 (u64 __user
*)reg
->addr
);
2257 case KVM_REG_S390_PFTOKEN
:
2258 r
= get_user(vcpu
->arch
.pfault_token
,
2259 (u64 __user
*)reg
->addr
);
2260 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
2261 kvm_clear_async_pf_completion_queue(vcpu
);
2263 case KVM_REG_S390_PFCOMPARE
:
2264 r
= get_user(vcpu
->arch
.pfault_compare
,
2265 (u64 __user
*)reg
->addr
);
2267 case KVM_REG_S390_PFSELECT
:
2268 r
= get_user(vcpu
->arch
.pfault_select
,
2269 (u64 __user
*)reg
->addr
);
2271 case KVM_REG_S390_PP
:
2272 r
= get_user(vcpu
->arch
.sie_block
->pp
,
2273 (u64 __user
*)reg
->addr
);
2275 case KVM_REG_S390_GBEA
:
2276 r
= get_user(vcpu
->arch
.sie_block
->gbea
,
2277 (u64 __user
*)reg
->addr
);
2286 static int kvm_arch_vcpu_ioctl_initial_reset(struct kvm_vcpu
*vcpu
)
2288 kvm_s390_vcpu_initial_reset(vcpu
);
2292 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2294 memcpy(&vcpu
->run
->s
.regs
.gprs
, ®s
->gprs
, sizeof(regs
->gprs
));
2298 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu
*vcpu
, struct kvm_regs
*regs
)
2300 memcpy(®s
->gprs
, &vcpu
->run
->s
.regs
.gprs
, sizeof(regs
->gprs
));
2304 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
2305 struct kvm_sregs
*sregs
)
2307 memcpy(&vcpu
->run
->s
.regs
.acrs
, &sregs
->acrs
, sizeof(sregs
->acrs
));
2308 memcpy(&vcpu
->arch
.sie_block
->gcr
, &sregs
->crs
, sizeof(sregs
->crs
));
2312 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
2313 struct kvm_sregs
*sregs
)
2315 memcpy(&sregs
->acrs
, &vcpu
->run
->s
.regs
.acrs
, sizeof(sregs
->acrs
));
2316 memcpy(&sregs
->crs
, &vcpu
->arch
.sie_block
->gcr
, sizeof(sregs
->crs
));
2320 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2322 if (test_fp_ctl(fpu
->fpc
))
2324 vcpu
->run
->s
.regs
.fpc
= fpu
->fpc
;
2326 convert_fp_to_vx((__vector128
*) vcpu
->run
->s
.regs
.vrs
,
2327 (freg_t
*) fpu
->fprs
);
2329 memcpy(vcpu
->run
->s
.regs
.fprs
, &fpu
->fprs
, sizeof(fpu
->fprs
));
2333 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu
*vcpu
, struct kvm_fpu
*fpu
)
2335 /* make sure we have the latest values */
2338 convert_vx_to_fp((freg_t
*) fpu
->fprs
,
2339 (__vector128
*) vcpu
->run
->s
.regs
.vrs
);
2341 memcpy(fpu
->fprs
, vcpu
->run
->s
.regs
.fprs
, sizeof(fpu
->fprs
));
2342 fpu
->fpc
= vcpu
->run
->s
.regs
.fpc
;
2346 static int kvm_arch_vcpu_ioctl_set_initial_psw(struct kvm_vcpu
*vcpu
, psw_t psw
)
2350 if (!is_vcpu_stopped(vcpu
))
2353 vcpu
->run
->psw_mask
= psw
.mask
;
2354 vcpu
->run
->psw_addr
= psw
.addr
;
2359 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu
*vcpu
,
2360 struct kvm_translation
*tr
)
2362 return -EINVAL
; /* not implemented yet */
2365 #define VALID_GUESTDBG_FLAGS (KVM_GUESTDBG_SINGLESTEP | \
2366 KVM_GUESTDBG_USE_HW_BP | \
2367 KVM_GUESTDBG_ENABLE)
2369 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu
*vcpu
,
2370 struct kvm_guest_debug
*dbg
)
2374 vcpu
->guest_debug
= 0;
2375 kvm_s390_clear_bp_data(vcpu
);
2377 if (dbg
->control
& ~VALID_GUESTDBG_FLAGS
)
2379 if (!sclp
.has_gpere
)
2382 if (dbg
->control
& KVM_GUESTDBG_ENABLE
) {
2383 vcpu
->guest_debug
= dbg
->control
;
2384 /* enforce guest PER */
2385 atomic_or(CPUSTAT_P
, &vcpu
->arch
.sie_block
->cpuflags
);
2387 if (dbg
->control
& KVM_GUESTDBG_USE_HW_BP
)
2388 rc
= kvm_s390_import_bp_data(vcpu
, dbg
);
2390 atomic_andnot(CPUSTAT_P
, &vcpu
->arch
.sie_block
->cpuflags
);
2391 vcpu
->arch
.guestdbg
.last_bp
= 0;
2395 vcpu
->guest_debug
= 0;
2396 kvm_s390_clear_bp_data(vcpu
);
2397 atomic_andnot(CPUSTAT_P
, &vcpu
->arch
.sie_block
->cpuflags
);
2403 int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu
*vcpu
,
2404 struct kvm_mp_state
*mp_state
)
2406 /* CHECK_STOP and LOAD are not supported yet */
2407 return is_vcpu_stopped(vcpu
) ? KVM_MP_STATE_STOPPED
:
2408 KVM_MP_STATE_OPERATING
;
2411 int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu
*vcpu
,
2412 struct kvm_mp_state
*mp_state
)
2416 /* user space knows about this interface - let it control the state */
2417 vcpu
->kvm
->arch
.user_cpu_state_ctrl
= 1;
2419 switch (mp_state
->mp_state
) {
2420 case KVM_MP_STATE_STOPPED
:
2421 kvm_s390_vcpu_stop(vcpu
);
2423 case KVM_MP_STATE_OPERATING
:
2424 kvm_s390_vcpu_start(vcpu
);
2426 case KVM_MP_STATE_LOAD
:
2427 case KVM_MP_STATE_CHECK_STOP
:
2428 /* fall through - CHECK_STOP and LOAD are not supported yet */
2436 static bool ibs_enabled(struct kvm_vcpu
*vcpu
)
2438 return atomic_read(&vcpu
->arch
.sie_block
->cpuflags
) & CPUSTAT_IBS
;
2441 static int kvm_s390_handle_requests(struct kvm_vcpu
*vcpu
)
2444 kvm_s390_vcpu_request_handled(vcpu
);
2445 if (!vcpu
->requests
)
2448 * We use MMU_RELOAD just to re-arm the ipte notifier for the
2449 * guest prefix page. gmap_mprotect_notify will wait on the ptl lock.
2450 * This ensures that the ipte instruction for this request has
2451 * already finished. We might race against a second unmapper that
2452 * wants to set the blocking bit. Lets just retry the request loop.
2454 if (kvm_check_request(KVM_REQ_MMU_RELOAD
, vcpu
)) {
2456 rc
= gmap_mprotect_notify(vcpu
->arch
.gmap
,
2457 kvm_s390_get_prefix(vcpu
),
2458 PAGE_SIZE
* 2, PROT_WRITE
);
2460 kvm_make_request(KVM_REQ_MMU_RELOAD
, vcpu
);
2466 if (kvm_check_request(KVM_REQ_TLB_FLUSH
, vcpu
)) {
2467 vcpu
->arch
.sie_block
->ihcpu
= 0xffff;
2471 if (kvm_check_request(KVM_REQ_ENABLE_IBS
, vcpu
)) {
2472 if (!ibs_enabled(vcpu
)) {
2473 trace_kvm_s390_enable_disable_ibs(vcpu
->vcpu_id
, 1);
2474 atomic_or(CPUSTAT_IBS
,
2475 &vcpu
->arch
.sie_block
->cpuflags
);
2480 if (kvm_check_request(KVM_REQ_DISABLE_IBS
, vcpu
)) {
2481 if (ibs_enabled(vcpu
)) {
2482 trace_kvm_s390_enable_disable_ibs(vcpu
->vcpu_id
, 0);
2483 atomic_andnot(CPUSTAT_IBS
,
2484 &vcpu
->arch
.sie_block
->cpuflags
);
2489 if (kvm_check_request(KVM_REQ_ICPT_OPEREXC
, vcpu
)) {
2490 vcpu
->arch
.sie_block
->ictl
|= ICTL_OPEREXC
;
2494 /* nothing to do, just clear the request */
2495 clear_bit(KVM_REQ_UNHALT
, &vcpu
->requests
);
2500 void kvm_s390_set_tod_clock(struct kvm
*kvm
, u64 tod
)
2502 struct kvm_vcpu
*vcpu
;
2505 mutex_lock(&kvm
->lock
);
2507 kvm
->arch
.epoch
= tod
- get_tod_clock();
2508 kvm_s390_vcpu_block_all(kvm
);
2509 kvm_for_each_vcpu(i
, vcpu
, kvm
)
2510 vcpu
->arch
.sie_block
->epoch
= kvm
->arch
.epoch
;
2511 kvm_s390_vcpu_unblock_all(kvm
);
2513 mutex_unlock(&kvm
->lock
);
2517 * kvm_arch_fault_in_page - fault-in guest page if necessary
2518 * @vcpu: The corresponding virtual cpu
2519 * @gpa: Guest physical address
2520 * @writable: Whether the page should be writable or not
2522 * Make sure that a guest page has been faulted-in on the host.
2524 * Return: Zero on success, negative error code otherwise.
2526 long kvm_arch_fault_in_page(struct kvm_vcpu
*vcpu
, gpa_t gpa
, int writable
)
2528 return gmap_fault(vcpu
->arch
.gmap
, gpa
,
2529 writable
? FAULT_FLAG_WRITE
: 0);
2532 static void __kvm_inject_pfault_token(struct kvm_vcpu
*vcpu
, bool start_token
,
2533 unsigned long token
)
2535 struct kvm_s390_interrupt inti
;
2536 struct kvm_s390_irq irq
;
2539 irq
.u
.ext
.ext_params2
= token
;
2540 irq
.type
= KVM_S390_INT_PFAULT_INIT
;
2541 WARN_ON_ONCE(kvm_s390_inject_vcpu(vcpu
, &irq
));
2543 inti
.type
= KVM_S390_INT_PFAULT_DONE
;
2544 inti
.parm64
= token
;
2545 WARN_ON_ONCE(kvm_s390_inject_vm(vcpu
->kvm
, &inti
));
2549 void kvm_arch_async_page_not_present(struct kvm_vcpu
*vcpu
,
2550 struct kvm_async_pf
*work
)
2552 trace_kvm_s390_pfault_init(vcpu
, work
->arch
.pfault_token
);
2553 __kvm_inject_pfault_token(vcpu
, true, work
->arch
.pfault_token
);
2556 void kvm_arch_async_page_present(struct kvm_vcpu
*vcpu
,
2557 struct kvm_async_pf
*work
)
2559 trace_kvm_s390_pfault_done(vcpu
, work
->arch
.pfault_token
);
2560 __kvm_inject_pfault_token(vcpu
, false, work
->arch
.pfault_token
);
2563 void kvm_arch_async_page_ready(struct kvm_vcpu
*vcpu
,
2564 struct kvm_async_pf
*work
)
2566 /* s390 will always inject the page directly */
2569 bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu
*vcpu
)
2572 * s390 will always inject the page directly,
2573 * but we still want check_async_completion to cleanup
2578 static int kvm_arch_setup_async_pf(struct kvm_vcpu
*vcpu
)
2581 struct kvm_arch_async_pf arch
;
2584 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
2586 if ((vcpu
->arch
.sie_block
->gpsw
.mask
& vcpu
->arch
.pfault_select
) !=
2587 vcpu
->arch
.pfault_compare
)
2589 if (psw_extint_disabled(vcpu
))
2591 if (kvm_s390_vcpu_has_irq(vcpu
, 0))
2593 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x200ul
))
2595 if (!vcpu
->arch
.gmap
->pfault_enabled
)
2598 hva
= gfn_to_hva(vcpu
->kvm
, gpa_to_gfn(current
->thread
.gmap_addr
));
2599 hva
+= current
->thread
.gmap_addr
& ~PAGE_MASK
;
2600 if (read_guest_real(vcpu
, vcpu
->arch
.pfault_token
, &arch
.pfault_token
, 8))
2603 rc
= kvm_setup_async_pf(vcpu
, current
->thread
.gmap_addr
, hva
, &arch
);
2607 static int vcpu_pre_run(struct kvm_vcpu
*vcpu
)
2612 * On s390 notifications for arriving pages will be delivered directly
2613 * to the guest but the house keeping for completed pfaults is
2614 * handled outside the worker.
2616 kvm_check_async_pf_completion(vcpu
);
2618 vcpu
->arch
.sie_block
->gg14
= vcpu
->run
->s
.regs
.gprs
[14];
2619 vcpu
->arch
.sie_block
->gg15
= vcpu
->run
->s
.regs
.gprs
[15];
2624 if (test_cpu_flag(CIF_MCCK_PENDING
))
2627 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
2628 rc
= kvm_s390_deliver_pending_interrupts(vcpu
);
2633 rc
= kvm_s390_handle_requests(vcpu
);
2637 if (guestdbg_enabled(vcpu
)) {
2638 kvm_s390_backup_guest_per_regs(vcpu
);
2639 kvm_s390_patch_guest_per_regs(vcpu
);
2642 vcpu
->arch
.sie_block
->icptcode
= 0;
2643 cpuflags
= atomic_read(&vcpu
->arch
.sie_block
->cpuflags
);
2644 VCPU_EVENT(vcpu
, 6, "entering sie flags %x", cpuflags
);
2645 trace_kvm_s390_sie_enter(vcpu
, cpuflags
);
2650 static int vcpu_post_run_fault_in_sie(struct kvm_vcpu
*vcpu
)
2652 struct kvm_s390_pgm_info pgm_info
= {
2653 .code
= PGM_ADDRESSING
,
2658 VCPU_EVENT(vcpu
, 3, "%s", "fault in sie instruction");
2659 trace_kvm_s390_sie_fault(vcpu
);
2662 * We want to inject an addressing exception, which is defined as a
2663 * suppressing or terminating exception. However, since we came here
2664 * by a DAT access exception, the PSW still points to the faulting
2665 * instruction since DAT exceptions are nullifying. So we've got
2666 * to look up the current opcode to get the length of the instruction
2667 * to be able to forward the PSW.
2669 rc
= read_guest_instr(vcpu
, vcpu
->arch
.sie_block
->gpsw
.addr
, &opcode
, 1);
2670 ilen
= insn_length(opcode
);
2674 /* Instruction-Fetching Exceptions - we can't detect the ilen.
2675 * Forward by arbitrary ilc, injection will take care of
2676 * nullification if necessary.
2678 pgm_info
= vcpu
->arch
.pgm
;
2681 pgm_info
.flags
= ilen
| KVM_S390_PGM_FLAGS_ILC_VALID
;
2682 kvm_s390_forward_psw(vcpu
, ilen
);
2683 return kvm_s390_inject_prog_irq(vcpu
, &pgm_info
);
2686 static int vcpu_post_run(struct kvm_vcpu
*vcpu
, int exit_reason
)
2688 VCPU_EVENT(vcpu
, 6, "exit sie icptcode %d",
2689 vcpu
->arch
.sie_block
->icptcode
);
2690 trace_kvm_s390_sie_exit(vcpu
, vcpu
->arch
.sie_block
->icptcode
);
2692 if (guestdbg_enabled(vcpu
))
2693 kvm_s390_restore_guest_per_regs(vcpu
);
2695 vcpu
->run
->s
.regs
.gprs
[14] = vcpu
->arch
.sie_block
->gg14
;
2696 vcpu
->run
->s
.regs
.gprs
[15] = vcpu
->arch
.sie_block
->gg15
;
2698 if (vcpu
->arch
.sie_block
->icptcode
> 0) {
2699 int rc
= kvm_handle_sie_intercept(vcpu
);
2701 if (rc
!= -EOPNOTSUPP
)
2703 vcpu
->run
->exit_reason
= KVM_EXIT_S390_SIEIC
;
2704 vcpu
->run
->s390_sieic
.icptcode
= vcpu
->arch
.sie_block
->icptcode
;
2705 vcpu
->run
->s390_sieic
.ipa
= vcpu
->arch
.sie_block
->ipa
;
2706 vcpu
->run
->s390_sieic
.ipb
= vcpu
->arch
.sie_block
->ipb
;
2708 } else if (exit_reason
!= -EFAULT
) {
2709 vcpu
->stat
.exit_null
++;
2711 } else if (kvm_is_ucontrol(vcpu
->kvm
)) {
2712 vcpu
->run
->exit_reason
= KVM_EXIT_S390_UCONTROL
;
2713 vcpu
->run
->s390_ucontrol
.trans_exc_code
=
2714 current
->thread
.gmap_addr
;
2715 vcpu
->run
->s390_ucontrol
.pgm_code
= 0x10;
2717 } else if (current
->thread
.gmap_pfault
) {
2718 trace_kvm_s390_major_guest_pfault(vcpu
);
2719 current
->thread
.gmap_pfault
= 0;
2720 if (kvm_arch_setup_async_pf(vcpu
))
2722 return kvm_arch_fault_in_page(vcpu
, current
->thread
.gmap_addr
, 1);
2724 return vcpu_post_run_fault_in_sie(vcpu
);
2727 static int __vcpu_run(struct kvm_vcpu
*vcpu
)
2729 int rc
, exit_reason
;
2732 * We try to hold kvm->srcu during most of vcpu_run (except when run-
2733 * ning the guest), so that memslots (and other stuff) are protected
2735 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
2738 rc
= vcpu_pre_run(vcpu
);
2742 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
2744 * As PF_VCPU will be used in fault handler, between
2745 * guest_enter and guest_exit should be no uaccess.
2747 local_irq_disable();
2748 guest_enter_irqoff();
2749 __disable_cpu_timer_accounting(vcpu
);
2751 exit_reason
= sie64a(vcpu
->arch
.sie_block
,
2752 vcpu
->run
->s
.regs
.gprs
);
2753 local_irq_disable();
2754 __enable_cpu_timer_accounting(vcpu
);
2755 guest_exit_irqoff();
2757 vcpu
->srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
2759 rc
= vcpu_post_run(vcpu
, exit_reason
);
2760 } while (!signal_pending(current
) && !guestdbg_exit_pending(vcpu
) && !rc
);
2762 srcu_read_unlock(&vcpu
->kvm
->srcu
, vcpu
->srcu_idx
);
2766 static void sync_regs(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2768 struct runtime_instr_cb
*riccb
;
2771 riccb
= (struct runtime_instr_cb
*) &kvm_run
->s
.regs
.riccb
;
2772 gscb
= (struct gs_cb
*) &kvm_run
->s
.regs
.gscb
;
2773 vcpu
->arch
.sie_block
->gpsw
.mask
= kvm_run
->psw_mask
;
2774 vcpu
->arch
.sie_block
->gpsw
.addr
= kvm_run
->psw_addr
;
2775 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_PREFIX
)
2776 kvm_s390_set_prefix(vcpu
, kvm_run
->s
.regs
.prefix
);
2777 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_CRS
) {
2778 memcpy(&vcpu
->arch
.sie_block
->gcr
, &kvm_run
->s
.regs
.crs
, 128);
2779 /* some control register changes require a tlb flush */
2780 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
2782 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_ARCH0
) {
2783 kvm_s390_set_cpu_timer(vcpu
, kvm_run
->s
.regs
.cputm
);
2784 vcpu
->arch
.sie_block
->ckc
= kvm_run
->s
.regs
.ckc
;
2785 vcpu
->arch
.sie_block
->todpr
= kvm_run
->s
.regs
.todpr
;
2786 vcpu
->arch
.sie_block
->pp
= kvm_run
->s
.regs
.pp
;
2787 vcpu
->arch
.sie_block
->gbea
= kvm_run
->s
.regs
.gbea
;
2789 if (kvm_run
->kvm_dirty_regs
& KVM_SYNC_PFAULT
) {
2790 vcpu
->arch
.pfault_token
= kvm_run
->s
.regs
.pft
;
2791 vcpu
->arch
.pfault_select
= kvm_run
->s
.regs
.pfs
;
2792 vcpu
->arch
.pfault_compare
= kvm_run
->s
.regs
.pfc
;
2793 if (vcpu
->arch
.pfault_token
== KVM_S390_PFAULT_TOKEN_INVALID
)
2794 kvm_clear_async_pf_completion_queue(vcpu
);
2797 * If userspace sets the riccb (e.g. after migration) to a valid state,
2798 * we should enable RI here instead of doing the lazy enablement.
2800 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_RICCB
) &&
2801 test_kvm_facility(vcpu
->kvm
, 64) &&
2803 !(vcpu
->arch
.sie_block
->ecb3
& ECB3_RI
)) {
2804 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: RI (sync_regs)");
2805 vcpu
->arch
.sie_block
->ecb3
|= ECB3_RI
;
2808 * If userspace sets the gscb (e.g. after migration) to non-zero,
2809 * we should enable GS here instead of doing the lazy enablement.
2811 if ((kvm_run
->kvm_dirty_regs
& KVM_SYNC_GSCB
) &&
2812 test_kvm_facility(vcpu
->kvm
, 133) &&
2814 !vcpu
->arch
.gs_enabled
) {
2815 VCPU_EVENT(vcpu
, 3, "%s", "ENABLE: GS (sync_regs)");
2816 vcpu
->arch
.sie_block
->ecb
|= ECB_GS
;
2817 vcpu
->arch
.sie_block
->ecd
|= ECD_HOSTREGMGMT
;
2818 vcpu
->arch
.gs_enabled
= 1;
2820 save_access_regs(vcpu
->arch
.host_acrs
);
2821 restore_access_regs(vcpu
->run
->s
.regs
.acrs
);
2822 /* save host (userspace) fprs/vrs */
2824 vcpu
->arch
.host_fpregs
.fpc
= current
->thread
.fpu
.fpc
;
2825 vcpu
->arch
.host_fpregs
.regs
= current
->thread
.fpu
.regs
;
2827 current
->thread
.fpu
.regs
= vcpu
->run
->s
.regs
.vrs
;
2829 current
->thread
.fpu
.regs
= vcpu
->run
->s
.regs
.fprs
;
2830 current
->thread
.fpu
.fpc
= vcpu
->run
->s
.regs
.fpc
;
2831 if (test_fp_ctl(current
->thread
.fpu
.fpc
))
2832 /* User space provided an invalid FPC, let's clear it */
2833 current
->thread
.fpu
.fpc
= 0;
2834 if (MACHINE_HAS_GS
) {
2836 __ctl_set_bit(2, 4);
2837 if (current
->thread
.gs_cb
) {
2838 vcpu
->arch
.host_gscb
= current
->thread
.gs_cb
;
2839 save_gs_cb(vcpu
->arch
.host_gscb
);
2841 if (vcpu
->arch
.gs_enabled
) {
2842 current
->thread
.gs_cb
= (struct gs_cb
*)
2843 &vcpu
->run
->s
.regs
.gscb
;
2844 restore_gs_cb(current
->thread
.gs_cb
);
2849 kvm_run
->kvm_dirty_regs
= 0;
2852 static void store_regs(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2854 kvm_run
->psw_mask
= vcpu
->arch
.sie_block
->gpsw
.mask
;
2855 kvm_run
->psw_addr
= vcpu
->arch
.sie_block
->gpsw
.addr
;
2856 kvm_run
->s
.regs
.prefix
= kvm_s390_get_prefix(vcpu
);
2857 memcpy(&kvm_run
->s
.regs
.crs
, &vcpu
->arch
.sie_block
->gcr
, 128);
2858 kvm_run
->s
.regs
.cputm
= kvm_s390_get_cpu_timer(vcpu
);
2859 kvm_run
->s
.regs
.ckc
= vcpu
->arch
.sie_block
->ckc
;
2860 kvm_run
->s
.regs
.todpr
= vcpu
->arch
.sie_block
->todpr
;
2861 kvm_run
->s
.regs
.pp
= vcpu
->arch
.sie_block
->pp
;
2862 kvm_run
->s
.regs
.gbea
= vcpu
->arch
.sie_block
->gbea
;
2863 kvm_run
->s
.regs
.pft
= vcpu
->arch
.pfault_token
;
2864 kvm_run
->s
.regs
.pfs
= vcpu
->arch
.pfault_select
;
2865 kvm_run
->s
.regs
.pfc
= vcpu
->arch
.pfault_compare
;
2866 save_access_regs(vcpu
->run
->s
.regs
.acrs
);
2867 restore_access_regs(vcpu
->arch
.host_acrs
);
2868 /* Save guest register state */
2870 vcpu
->run
->s
.regs
.fpc
= current
->thread
.fpu
.fpc
;
2871 /* Restore will be done lazily at return */
2872 current
->thread
.fpu
.fpc
= vcpu
->arch
.host_fpregs
.fpc
;
2873 current
->thread
.fpu
.regs
= vcpu
->arch
.host_fpregs
.regs
;
2874 if (MACHINE_HAS_GS
) {
2875 __ctl_set_bit(2, 4);
2876 if (vcpu
->arch
.gs_enabled
)
2877 save_gs_cb(current
->thread
.gs_cb
);
2879 current
->thread
.gs_cb
= vcpu
->arch
.host_gscb
;
2880 restore_gs_cb(vcpu
->arch
.host_gscb
);
2882 if (!vcpu
->arch
.host_gscb
)
2883 __ctl_clear_bit(2, 4);
2884 vcpu
->arch
.host_gscb
= NULL
;
2889 int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu
*vcpu
, struct kvm_run
*kvm_run
)
2894 if (kvm_run
->immediate_exit
)
2897 if (guestdbg_exit_pending(vcpu
)) {
2898 kvm_s390_prepare_debug_exit(vcpu
);
2902 if (vcpu
->sigset_active
)
2903 sigprocmask(SIG_SETMASK
, &vcpu
->sigset
, &sigsaved
);
2905 if (!kvm_s390_user_cpu_state_ctrl(vcpu
->kvm
)) {
2906 kvm_s390_vcpu_start(vcpu
);
2907 } else if (is_vcpu_stopped(vcpu
)) {
2908 pr_err_ratelimited("can't run stopped vcpu %d\n",
2913 sync_regs(vcpu
, kvm_run
);
2914 enable_cpu_timer_accounting(vcpu
);
2917 rc
= __vcpu_run(vcpu
);
2919 if (signal_pending(current
) && !rc
) {
2920 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
2924 if (guestdbg_exit_pending(vcpu
) && !rc
) {
2925 kvm_s390_prepare_debug_exit(vcpu
);
2929 if (rc
== -EREMOTE
) {
2930 /* userspace support is needed, kvm_run has been prepared */
2934 disable_cpu_timer_accounting(vcpu
);
2935 store_regs(vcpu
, kvm_run
);
2937 if (vcpu
->sigset_active
)
2938 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2940 vcpu
->stat
.exit_userspace
++;
2945 * store status at address
2946 * we use have two special cases:
2947 * KVM_S390_STORE_STATUS_NOADDR: -> 0x1200 on 64 bit
2948 * KVM_S390_STORE_STATUS_PREFIXED: -> prefix
2950 int kvm_s390_store_status_unloaded(struct kvm_vcpu
*vcpu
, unsigned long gpa
)
2952 unsigned char archmode
= 1;
2953 freg_t fprs
[NUM_FPRS
];
2958 px
= kvm_s390_get_prefix(vcpu
);
2959 if (gpa
== KVM_S390_STORE_STATUS_NOADDR
) {
2960 if (write_guest_abs(vcpu
, 163, &archmode
, 1))
2963 } else if (gpa
== KVM_S390_STORE_STATUS_PREFIXED
) {
2964 if (write_guest_real(vcpu
, 163, &archmode
, 1))
2968 gpa
-= __LC_FPREGS_SAVE_AREA
;
2970 /* manually convert vector registers if necessary */
2971 if (MACHINE_HAS_VX
) {
2972 convert_vx_to_fp(fprs
, (__vector128
*) vcpu
->run
->s
.regs
.vrs
);
2973 rc
= write_guest_abs(vcpu
, gpa
+ __LC_FPREGS_SAVE_AREA
,
2976 rc
= write_guest_abs(vcpu
, gpa
+ __LC_FPREGS_SAVE_AREA
,
2977 vcpu
->run
->s
.regs
.fprs
, 128);
2979 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_GPREGS_SAVE_AREA
,
2980 vcpu
->run
->s
.regs
.gprs
, 128);
2981 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_PSW_SAVE_AREA
,
2982 &vcpu
->arch
.sie_block
->gpsw
, 16);
2983 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_PREFIX_SAVE_AREA
,
2985 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_FP_CREG_SAVE_AREA
,
2986 &vcpu
->run
->s
.regs
.fpc
, 4);
2987 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_TOD_PROGREG_SAVE_AREA
,
2988 &vcpu
->arch
.sie_block
->todpr
, 4);
2989 cputm
= kvm_s390_get_cpu_timer(vcpu
);
2990 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CPU_TIMER_SAVE_AREA
,
2992 clkcomp
= vcpu
->arch
.sie_block
->ckc
>> 8;
2993 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CLOCK_COMP_SAVE_AREA
,
2995 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_AREGS_SAVE_AREA
,
2996 &vcpu
->run
->s
.regs
.acrs
, 64);
2997 rc
|= write_guest_abs(vcpu
, gpa
+ __LC_CREGS_SAVE_AREA
,
2998 &vcpu
->arch
.sie_block
->gcr
, 128);
2999 return rc
? -EFAULT
: 0;
3002 int kvm_s390_vcpu_store_status(struct kvm_vcpu
*vcpu
, unsigned long addr
)
3005 * The guest FPRS and ACRS are in the host FPRS/ACRS due to the lazy
3006 * switch in the run ioctl. Let's update our copies before we save
3007 * it into the save area
3010 vcpu
->run
->s
.regs
.fpc
= current
->thread
.fpu
.fpc
;
3011 save_access_regs(vcpu
->run
->s
.regs
.acrs
);
3013 return kvm_s390_store_status_unloaded(vcpu
, addr
);
3016 static void __disable_ibs_on_vcpu(struct kvm_vcpu
*vcpu
)
3018 kvm_check_request(KVM_REQ_ENABLE_IBS
, vcpu
);
3019 kvm_s390_sync_request(KVM_REQ_DISABLE_IBS
, vcpu
);
3022 static void __disable_ibs_on_all_vcpus(struct kvm
*kvm
)
3025 struct kvm_vcpu
*vcpu
;
3027 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
3028 __disable_ibs_on_vcpu(vcpu
);
3032 static void __enable_ibs_on_vcpu(struct kvm_vcpu
*vcpu
)
3036 kvm_check_request(KVM_REQ_DISABLE_IBS
, vcpu
);
3037 kvm_s390_sync_request(KVM_REQ_ENABLE_IBS
, vcpu
);
3040 void kvm_s390_vcpu_start(struct kvm_vcpu
*vcpu
)
3042 int i
, online_vcpus
, started_vcpus
= 0;
3044 if (!is_vcpu_stopped(vcpu
))
3047 trace_kvm_s390_vcpu_start_stop(vcpu
->vcpu_id
, 1);
3048 /* Only one cpu at a time may enter/leave the STOPPED state. */
3049 spin_lock(&vcpu
->kvm
->arch
.start_stop_lock
);
3050 online_vcpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
3052 for (i
= 0; i
< online_vcpus
; i
++) {
3053 if (!is_vcpu_stopped(vcpu
->kvm
->vcpus
[i
]))
3057 if (started_vcpus
== 0) {
3058 /* we're the only active VCPU -> speed it up */
3059 __enable_ibs_on_vcpu(vcpu
);
3060 } else if (started_vcpus
== 1) {
3062 * As we are starting a second VCPU, we have to disable
3063 * the IBS facility on all VCPUs to remove potentially
3064 * oustanding ENABLE requests.
3066 __disable_ibs_on_all_vcpus(vcpu
->kvm
);
3069 atomic_andnot(CPUSTAT_STOPPED
, &vcpu
->arch
.sie_block
->cpuflags
);
3071 * Another VCPU might have used IBS while we were offline.
3072 * Let's play safe and flush the VCPU at startup.
3074 kvm_make_request(KVM_REQ_TLB_FLUSH
, vcpu
);
3075 spin_unlock(&vcpu
->kvm
->arch
.start_stop_lock
);
3079 void kvm_s390_vcpu_stop(struct kvm_vcpu
*vcpu
)
3081 int i
, online_vcpus
, started_vcpus
= 0;
3082 struct kvm_vcpu
*started_vcpu
= NULL
;
3084 if (is_vcpu_stopped(vcpu
))
3087 trace_kvm_s390_vcpu_start_stop(vcpu
->vcpu_id
, 0);
3088 /* Only one cpu at a time may enter/leave the STOPPED state. */
3089 spin_lock(&vcpu
->kvm
->arch
.start_stop_lock
);
3090 online_vcpus
= atomic_read(&vcpu
->kvm
->online_vcpus
);
3092 /* SIGP STOP and SIGP STOP AND STORE STATUS has been fully processed */
3093 kvm_s390_clear_stop_irq(vcpu
);
3095 atomic_or(CPUSTAT_STOPPED
, &vcpu
->arch
.sie_block
->cpuflags
);
3096 __disable_ibs_on_vcpu(vcpu
);
3098 for (i
= 0; i
< online_vcpus
; i
++) {
3099 if (!is_vcpu_stopped(vcpu
->kvm
->vcpus
[i
])) {
3101 started_vcpu
= vcpu
->kvm
->vcpus
[i
];
3105 if (started_vcpus
== 1) {
3107 * As we only have one VCPU left, we want to enable the
3108 * IBS facility for that VCPU to speed it up.
3110 __enable_ibs_on_vcpu(started_vcpu
);
3113 spin_unlock(&vcpu
->kvm
->arch
.start_stop_lock
);
3117 static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu
*vcpu
,
3118 struct kvm_enable_cap
*cap
)
3126 case KVM_CAP_S390_CSS_SUPPORT
:
3127 if (!vcpu
->kvm
->arch
.css_support
) {
3128 vcpu
->kvm
->arch
.css_support
= 1;
3129 VM_EVENT(vcpu
->kvm
, 3, "%s", "ENABLE: CSS support");
3130 trace_kvm_s390_enable_css(vcpu
->kvm
);
3141 static long kvm_s390_guest_mem_op(struct kvm_vcpu
*vcpu
,
3142 struct kvm_s390_mem_op
*mop
)
3144 void __user
*uaddr
= (void __user
*)mop
->buf
;
3145 void *tmpbuf
= NULL
;
3147 const u64 supported_flags
= KVM_S390_MEMOP_F_INJECT_EXCEPTION
3148 | KVM_S390_MEMOP_F_CHECK_ONLY
;
3150 if (mop
->flags
& ~supported_flags
)
3153 if (mop
->size
> MEM_OP_MAX_SIZE
)
3156 if (!(mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
)) {
3157 tmpbuf
= vmalloc(mop
->size
);
3162 srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3165 case KVM_S390_MEMOP_LOGICAL_READ
:
3166 if (mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
) {
3167 r
= check_gva_range(vcpu
, mop
->gaddr
, mop
->ar
,
3168 mop
->size
, GACC_FETCH
);
3171 r
= read_guest(vcpu
, mop
->gaddr
, mop
->ar
, tmpbuf
, mop
->size
);
3173 if (copy_to_user(uaddr
, tmpbuf
, mop
->size
))
3177 case KVM_S390_MEMOP_LOGICAL_WRITE
:
3178 if (mop
->flags
& KVM_S390_MEMOP_F_CHECK_ONLY
) {
3179 r
= check_gva_range(vcpu
, mop
->gaddr
, mop
->ar
,
3180 mop
->size
, GACC_STORE
);
3183 if (copy_from_user(tmpbuf
, uaddr
, mop
->size
)) {
3187 r
= write_guest(vcpu
, mop
->gaddr
, mop
->ar
, tmpbuf
, mop
->size
);
3193 srcu_read_unlock(&vcpu
->kvm
->srcu
, srcu_idx
);
3195 if (r
> 0 && (mop
->flags
& KVM_S390_MEMOP_F_INJECT_EXCEPTION
) != 0)
3196 kvm_s390_inject_prog_irq(vcpu
, &vcpu
->arch
.pgm
);
3202 long kvm_arch_vcpu_ioctl(struct file
*filp
,
3203 unsigned int ioctl
, unsigned long arg
)
3205 struct kvm_vcpu
*vcpu
= filp
->private_data
;
3206 void __user
*argp
= (void __user
*)arg
;
3211 case KVM_S390_IRQ
: {
3212 struct kvm_s390_irq s390irq
;
3215 if (copy_from_user(&s390irq
, argp
, sizeof(s390irq
)))
3217 r
= kvm_s390_inject_vcpu(vcpu
, &s390irq
);
3220 case KVM_S390_INTERRUPT
: {
3221 struct kvm_s390_interrupt s390int
;
3222 struct kvm_s390_irq s390irq
;
3225 if (copy_from_user(&s390int
, argp
, sizeof(s390int
)))
3227 if (s390int_to_s390irq(&s390int
, &s390irq
))
3229 r
= kvm_s390_inject_vcpu(vcpu
, &s390irq
);
3232 case KVM_S390_STORE_STATUS
:
3233 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
3234 r
= kvm_s390_vcpu_store_status(vcpu
, arg
);
3235 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
3237 case KVM_S390_SET_INITIAL_PSW
: {
3241 if (copy_from_user(&psw
, argp
, sizeof(psw
)))
3243 r
= kvm_arch_vcpu_ioctl_set_initial_psw(vcpu
, psw
);
3246 case KVM_S390_INITIAL_RESET
:
3247 r
= kvm_arch_vcpu_ioctl_initial_reset(vcpu
);
3249 case KVM_SET_ONE_REG
:
3250 case KVM_GET_ONE_REG
: {
3251 struct kvm_one_reg reg
;
3253 if (copy_from_user(®
, argp
, sizeof(reg
)))
3255 if (ioctl
== KVM_SET_ONE_REG
)
3256 r
= kvm_arch_vcpu_ioctl_set_one_reg(vcpu
, ®
);
3258 r
= kvm_arch_vcpu_ioctl_get_one_reg(vcpu
, ®
);
3261 #ifdef CONFIG_KVM_S390_UCONTROL
3262 case KVM_S390_UCAS_MAP
: {
3263 struct kvm_s390_ucas_mapping ucasmap
;
3265 if (copy_from_user(&ucasmap
, argp
, sizeof(ucasmap
))) {
3270 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
3275 r
= gmap_map_segment(vcpu
->arch
.gmap
, ucasmap
.user_addr
,
3276 ucasmap
.vcpu_addr
, ucasmap
.length
);
3279 case KVM_S390_UCAS_UNMAP
: {
3280 struct kvm_s390_ucas_mapping ucasmap
;
3282 if (copy_from_user(&ucasmap
, argp
, sizeof(ucasmap
))) {
3287 if (!kvm_is_ucontrol(vcpu
->kvm
)) {
3292 r
= gmap_unmap_segment(vcpu
->arch
.gmap
, ucasmap
.vcpu_addr
,
3297 case KVM_S390_VCPU_FAULT
: {
3298 r
= gmap_fault(vcpu
->arch
.gmap
, arg
, 0);
3301 case KVM_ENABLE_CAP
:
3303 struct kvm_enable_cap cap
;
3305 if (copy_from_user(&cap
, argp
, sizeof(cap
)))
3307 r
= kvm_vcpu_ioctl_enable_cap(vcpu
, &cap
);
3310 case KVM_S390_MEM_OP
: {
3311 struct kvm_s390_mem_op mem_op
;
3313 if (copy_from_user(&mem_op
, argp
, sizeof(mem_op
)) == 0)
3314 r
= kvm_s390_guest_mem_op(vcpu
, &mem_op
);
3319 case KVM_S390_SET_IRQ_STATE
: {
3320 struct kvm_s390_irq_state irq_state
;
3323 if (copy_from_user(&irq_state
, argp
, sizeof(irq_state
)))
3325 if (irq_state
.len
> VCPU_IRQS_MAX_BUF
||
3326 irq_state
.len
== 0 ||
3327 irq_state
.len
% sizeof(struct kvm_s390_irq
) > 0) {
3331 r
= kvm_s390_set_irq_state(vcpu
,
3332 (void __user
*) irq_state
.buf
,
3336 case KVM_S390_GET_IRQ_STATE
: {
3337 struct kvm_s390_irq_state irq_state
;
3340 if (copy_from_user(&irq_state
, argp
, sizeof(irq_state
)))
3342 if (irq_state
.len
== 0) {
3346 r
= kvm_s390_get_irq_state(vcpu
,
3347 (__u8 __user
*) irq_state
.buf
,
3357 int kvm_arch_vcpu_fault(struct kvm_vcpu
*vcpu
, struct vm_fault
*vmf
)
3359 #ifdef CONFIG_KVM_S390_UCONTROL
3360 if ((vmf
->pgoff
== KVM_S390_SIE_PAGE_OFFSET
)
3361 && (kvm_is_ucontrol(vcpu
->kvm
))) {
3362 vmf
->page
= virt_to_page(vcpu
->arch
.sie_block
);
3363 get_page(vmf
->page
);
3367 return VM_FAULT_SIGBUS
;
3370 int kvm_arch_create_memslot(struct kvm
*kvm
, struct kvm_memory_slot
*slot
,
3371 unsigned long npages
)
3376 /* Section: memory related */
3377 int kvm_arch_prepare_memory_region(struct kvm
*kvm
,
3378 struct kvm_memory_slot
*memslot
,
3379 const struct kvm_userspace_memory_region
*mem
,
3380 enum kvm_mr_change change
)
3382 /* A few sanity checks. We can have memory slots which have to be
3383 located/ended at a segment boundary (1MB). The memory in userland is
3384 ok to be fragmented into various different vmas. It is okay to mmap()
3385 and munmap() stuff in this slot after doing this call at any time */
3387 if (mem
->userspace_addr
& 0xffffful
)
3390 if (mem
->memory_size
& 0xffffful
)
3393 if (mem
->guest_phys_addr
+ mem
->memory_size
> kvm
->arch
.mem_limit
)
3399 void kvm_arch_commit_memory_region(struct kvm
*kvm
,
3400 const struct kvm_userspace_memory_region
*mem
,
3401 const struct kvm_memory_slot
*old
,
3402 const struct kvm_memory_slot
*new,
3403 enum kvm_mr_change change
)
3407 /* If the basics of the memslot do not change, we do not want
3408 * to update the gmap. Every update causes several unnecessary
3409 * segment translation exceptions. This is usually handled just
3410 * fine by the normal fault handler + gmap, but it will also
3411 * cause faults on the prefix page of running guest CPUs.
3413 if (old
->userspace_addr
== mem
->userspace_addr
&&
3414 old
->base_gfn
* PAGE_SIZE
== mem
->guest_phys_addr
&&
3415 old
->npages
* PAGE_SIZE
== mem
->memory_size
)
3418 rc
= gmap_map_segment(kvm
->arch
.gmap
, mem
->userspace_addr
,
3419 mem
->guest_phys_addr
, mem
->memory_size
);
3421 pr_warn("failed to commit memory region\n");
3425 static inline unsigned long nonhyp_mask(int i
)
3427 unsigned int nonhyp_fai
= (sclp
.hmfai
<< i
* 2) >> 30;
3429 return 0x0000ffffffffffffUL
>> (nonhyp_fai
<< 4);
3432 void kvm_arch_vcpu_block_finish(struct kvm_vcpu
*vcpu
)
3434 vcpu
->valid_wakeup
= false;
3437 static int __init
kvm_s390_init(void)
3441 if (!sclp
.has_sief2
) {
3442 pr_info("SIE not available\n");
3446 for (i
= 0; i
< 16; i
++)
3447 kvm_s390_fac_list_mask
[i
] |=
3448 S390_lowcore
.stfle_fac_list
[i
] & nonhyp_mask(i
);
3450 return kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
3453 static void __exit
kvm_s390_exit(void)
3458 module_init(kvm_s390_init
);
3459 module_exit(kvm_s390_exit
);
3462 * Enable autoloading of the kvm module.
3463 * Note that we add the module alias here instead of virt/kvm/kvm_main.c
3464 * since x86 takes a different approach.
3466 #include <linux/miscdevice.h>
3467 MODULE_ALIAS_MISCDEV(KVM_MINOR
);
3468 MODULE_ALIAS("devname:kvm");