16 #include <linux/compiler.h>
19 #include <linux/hrtimer.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
26 #include <asm/asm-offsets.h>
28 #include <asm/pgtable.h>
30 #include <asm/switch_to.h>
35 #define CREATE_TRACE_POINTS
39 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
42 {
"userspace_handled",
VCPU_STAT(exit_userspace) },
44 {
"exit_validity",
VCPU_STAT(exit_validity) },
45 {
"exit_stop_request",
VCPU_STAT(exit_stop_request) },
46 {
"exit_external_request",
VCPU_STAT(exit_external_request) },
47 {
"exit_external_interrupt",
VCPU_STAT(exit_external_interrupt) },
48 {
"exit_instruction",
VCPU_STAT(exit_instruction) },
49 {
"exit_program_interruption",
VCPU_STAT(exit_program_interruption) },
50 {
"exit_instr_and_program_int",
VCPU_STAT(exit_instr_and_program) },
51 {
"instruction_lctlg",
VCPU_STAT(instruction_lctlg) },
52 {
"instruction_lctl",
VCPU_STAT(instruction_lctl) },
53 {
"deliver_emergency_signal",
VCPU_STAT(deliver_emergency_signal) },
54 {
"deliver_external_call",
VCPU_STAT(deliver_external_call) },
55 {
"deliver_service_signal",
VCPU_STAT(deliver_service_signal) },
56 {
"deliver_virtio_interrupt",
VCPU_STAT(deliver_virtio_interrupt) },
57 {
"deliver_stop_signal",
VCPU_STAT(deliver_stop_signal) },
58 {
"deliver_prefix_signal",
VCPU_STAT(deliver_prefix_signal) },
59 {
"deliver_restart_signal",
VCPU_STAT(deliver_restart_signal) },
60 {
"deliver_program_interruption",
VCPU_STAT(deliver_program_int) },
61 {
"exit_wait_state",
VCPU_STAT(exit_wait_state) },
62 {
"instruction_stidp",
VCPU_STAT(instruction_stidp) },
63 {
"instruction_spx",
VCPU_STAT(instruction_spx) },
64 {
"instruction_stpx",
VCPU_STAT(instruction_stpx) },
65 {
"instruction_stap",
VCPU_STAT(instruction_stap) },
66 {
"instruction_storage_key",
VCPU_STAT(instruction_storage_key) },
67 {
"instruction_stsch",
VCPU_STAT(instruction_stsch) },
68 {
"instruction_chsc",
VCPU_STAT(instruction_chsc) },
69 {
"instruction_stsi",
VCPU_STAT(instruction_stsi) },
70 {
"instruction_stfl",
VCPU_STAT(instruction_stfl) },
71 {
"instruction_tprot",
VCPU_STAT(instruction_tprot) },
72 {
"instruction_sigp_sense",
VCPU_STAT(instruction_sigp_sense) },
73 {
"instruction_sigp_sense_running",
VCPU_STAT(instruction_sigp_sense_running) },
74 {
"instruction_sigp_external_call",
VCPU_STAT(instruction_sigp_external_call) },
75 {
"instruction_sigp_emergency",
VCPU_STAT(instruction_sigp_emergency) },
76 {
"instruction_sigp_stop",
VCPU_STAT(instruction_sigp_stop) },
77 {
"instruction_sigp_set_arch",
VCPU_STAT(instruction_sigp_arch) },
78 {
"instruction_sigp_set_prefix",
VCPU_STAT(instruction_sigp_prefix) },
79 {
"instruction_sigp_restart",
VCPU_STAT(instruction_sigp_restart) },
80 {
"diagnose_10",
VCPU_STAT(diagnose_10) },
81 {
"diagnose_44",
VCPU_STAT(diagnose_44) },
82 {
"diagnose_9c",
VCPU_STAT(diagnose_9c) },
123 unsigned int ioctl,
unsigned long arg)
138 #ifdef CONFIG_KVM_S390_UCONTROL
169 unsigned int ioctl,
unsigned long arg)
198 #ifdef CONFIG_KVM_S390_UCONTROL
225 INIT_LIST_HEAD(&kvm->
arch.float_int.list);
228 VM_EVENT(kvm, 3,
"%s",
"vm created");
230 if (type & KVM_VM_S390_UCONTROL) {
249 trace_kvm_s390_destroy_vcpu(vcpu->
vcpu_id);
250 if (!kvm_is_ucontrol(vcpu->
kvm)) {
252 (
unsigned long *) &vcpu->
kvm->arch.sca->mcn);
253 if (vcpu->
kvm->arch.sca->cpu[vcpu->
vcpu_id].sda ==
255 vcpu->
kvm->arch.sca->cpu[vcpu->
vcpu_id].sda = 0;
259 if (kvm_is_ucontrol(vcpu->
kvm))
267 static void kvm_free_vcpus(
struct kvm *
kvm)
277 kvm->vcpus[i] =
NULL;
292 if (!kvm_is_ucontrol(kvm))
299 if (kvm_is_ucontrol(vcpu->
kvm)) {
301 if (!vcpu->
arch.gmap)
306 vcpu->
arch.gmap = vcpu->
kvm->arch.gmap;
321 save_fp_regs(&vcpu->
arch.host_fpregs);
322 save_access_regs(vcpu->
arch.host_acrs);
324 restore_fp_regs(&vcpu->
arch.guest_fpregs);
325 restore_access_regs(vcpu->
run->s.regs.acrs);
334 save_fp_regs(&vcpu->
arch.guest_fpregs);
335 save_access_regs(vcpu->
run->s.regs.acrs);
336 restore_fp_regs(&vcpu->
arch.host_fpregs);
337 restore_access_regs(vcpu->
arch.host_acrs);
340 static void kvm_s390_vcpu_initial_reset(
struct kvm_vcpu *vcpu)
343 vcpu->
arch.sie_block->gpsw.mask = 0
UL;
344 vcpu->
arch.sie_block->gpsw.addr = 0
UL;
345 kvm_s390_set_prefix(vcpu, 0);
346 vcpu->
arch.sie_block->cputm = 0
UL;
347 vcpu->
arch.sie_block->ckc = 0
UL;
348 vcpu->
arch.sie_block->todpr = 0;
350 vcpu->
arch.sie_block->gcr[0] = 0xE0
UL;
351 vcpu->
arch.sie_block->gcr[14] = 0xC2000000
UL;
352 vcpu->
arch.guest_fpregs.fpc = 0;
353 asm volatile(
"lfpc %0" : :
"Q" (vcpu->
arch.guest_fpregs.fpc));
354 vcpu->
arch.sie_block->gbea = 1;
363 vcpu->
arch.sie_block->ecb = 6;
364 vcpu->
arch.sie_block->eca = 0xC1002001
U;
368 (
unsigned long) vcpu);
370 get_cpu_id(&vcpu->arch.cpu_id);
371 vcpu->arch.cpu_id.version = 0xff;
393 if (!vcpu->
arch.sie_block)
396 vcpu->
arch.sie_block->icpua =
id;
397 if (!kvm_is_ucontrol(kvm)) {
398 if (!kvm->
arch.sca) {
402 if (!kvm->
arch.sca->cpu[
id].sda)
403 kvm->
arch.sca->cpu[
id].sda =
405 vcpu->
arch.sie_block->scaoh =
408 set_bit(63 -
id, (
unsigned long *) &kvm->
arch.sca->mcn);
412 INIT_LIST_HEAD(&vcpu->
arch.local_int.list);
413 vcpu->
arch.local_int.float_int = &kvm->
arch.float_int;
414 spin_lock(&kvm->
arch.float_int.lock);
415 kvm->
arch.float_int.local_int[
id] = &vcpu->
arch.local_int;
417 vcpu->
arch.local_int.cpuflags = &vcpu->
arch.sie_block->cpuflags;
418 spin_unlock(&kvm->
arch.float_int.lock);
422 goto out_free_sie_block;
423 VM_EVENT(kvm, 3,
"create cpu %d at %p, sie block at %p",
id, vcpu,
424 vcpu->
arch.sie_block);
425 trace_kvm_s390_create_vcpu(
id, vcpu, vcpu->
arch.sie_block);
450 static int kvm_arch_vcpu_ioctl_get_one_reg(
struct kvm_vcpu *vcpu,
479 static int kvm_arch_vcpu_ioctl_set_one_reg(
struct kvm_vcpu *vcpu,
508 static int kvm_arch_vcpu_ioctl_initial_reset(
struct kvm_vcpu *vcpu)
510 kvm_s390_vcpu_initial_reset(vcpu);
531 restore_access_regs(vcpu->
run->s.regs.acrs);
547 restore_fp_regs(&vcpu->
arch.guest_fpregs);
554 fpu->
fpc = vcpu->
arch.guest_fpregs.fpc;
558 static int kvm_arch_vcpu_ioctl_set_initial_psw(
struct kvm_vcpu *vcpu, psw_t psw)
565 vcpu->
run->psw_mask = psw.mask;
566 vcpu->
run->psw_addr = psw.addr;
595 static int __vcpu_run(
struct kvm_vcpu *vcpu)
599 memcpy(&vcpu->
arch.sie_block->gg14, &vcpu->
run->s.regs.gprs[14], 16);
607 if (!kvm_is_ucontrol(vcpu->
kvm))
610 vcpu->
arch.sie_block->icptcode = 0;
616 trace_kvm_s390_sie_enter(vcpu,
620 if (kvm_is_ucontrol(vcpu->
kvm)) {
623 VCPU_EVENT(vcpu, 3,
"%s",
"fault in sie instruction");
624 trace_kvm_s390_sie_fault(vcpu);
630 vcpu->
arch.sie_block->icptcode);
631 trace_kvm_s390_sie_exit(vcpu, vcpu->
arch.sie_block->icptcode);
636 memcpy(&vcpu->
run->s.regs.gprs[14], &vcpu->
arch.sie_block->gg14, 16);
664 vcpu->
arch.sie_block->gpsw.mask = kvm_run->psw_mask;
665 vcpu->
arch.sie_block->gpsw.addr = kvm_run->psw_addr;
668 kvm_s390_set_prefix(vcpu, kvm_run->
s.
regs.prefix);
673 kvm_s390_set_prefix(vcpu, kvm_run->
s.
regs.prefix);
679 rc = __vcpu_run(vcpu);
682 if (kvm_is_ucontrol(vcpu->
kvm))
686 }
while (!signal_pending(
current) && !rc);
691 if (signal_pending(
current) && !rc) {
696 #ifdef CONFIG_KVM_S390_UCONTROL
721 kvm_run->psw_mask = vcpu->
arch.sie_block->gpsw.mask;
722 kvm_run->psw_addr = vcpu->
arch.sie_block->gpsw.addr;
723 kvm_run->
s.
regs.prefix = vcpu->
arch.sie_block->prefix;
729 vcpu->
stat.exit_userspace++;
733 static int __guestcopy(
struct kvm_vcpu *vcpu,
u64 guestdest,
void *
from,
737 return copy_to_guest(vcpu, guestdest, from, n);
739 return copy_to_guest_absolute(vcpu, guestdest, from, n);
750 unsigned char archmode = 1;
754 if (copy_to_guest_absolute(vcpu, 163ul, &archmode, 1))
756 addr = SAVE_AREA_BASE;
759 if (copy_to_guest(vcpu, 163ul, &archmode, 1))
761 addr = SAVE_AREA_BASE;
767 vcpu->
arch.guest_fpregs.fprs, 128, prefix))
771 vcpu->
run->s.regs.gprs, 128, prefix))
775 &vcpu->
arch.sie_block->gpsw, 16, prefix))
779 &vcpu->
arch.sie_block->prefix, 4, prefix))
782 if (__guestcopy(vcpu,
784 &vcpu->
arch.guest_fpregs.fpc, 4, prefix))
788 &vcpu->
arch.sie_block->todpr, 4, prefix))
792 &vcpu->
arch.sie_block->cputm, 8, prefix))
796 &vcpu->
arch.sie_block->ckc, 8, prefix))
800 &vcpu->
run->s.regs.acrs, 64, prefix))
803 if (__guestcopy(vcpu,
805 &vcpu->
arch.sie_block->gcr, 128, prefix))
811 unsigned int ioctl,
unsigned long arg)
836 r = kvm_arch_vcpu_ioctl_set_initial_psw(vcpu, psw);
840 r = kvm_arch_vcpu_ioctl_initial_reset(vcpu);
849 r = kvm_arch_vcpu_ioctl_set_one_reg(vcpu, ®);
851 r = kvm_arch_vcpu_ioctl_get_one_reg(vcpu, ®);
854 #ifdef CONFIG_KVM_S390_UCONTROL
863 if (!kvm_is_ucontrol(vcpu->
kvm)) {
880 if (!kvm_is_ucontrol(vcpu->
kvm)) {
904 #ifdef CONFIG_KVM_S390_UCONTROL
906 && (kvm_is_ucontrol(vcpu->
kvm))) {
912 return VM_FAULT_SIGBUS;
981 static int __init kvm_s390_init(
void)
1004 static void __exit kvm_s390_exit(
void)