Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1 | /* |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 2 | * handling kvm guest interrupts |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright IBM Corp. 2008 |
| 5 | * |
| 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. |
| 9 | * |
| 10 | * Author(s): Carsten Otte <cotte@de.ibm.com> |
| 11 | */ |
| 12 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 14 | #include <linux/kvm_host.h> |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 15 | #include <linux/hrtimer.h> |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 16 | #include <linux/signal.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 18 | #include <asm/asm-offsets.h> |
| 19 | #include <asm/uaccess.h> |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 20 | #include "kvm-s390.h" |
| 21 | #include "gaccess.h" |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 22 | #include "trace-s390.h" |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 23 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 24 | #define IOINT_SCHID_MASK 0x0000ffff |
| 25 | #define IOINT_SSID_MASK 0x00030000 |
| 26 | #define IOINT_CSSID_MASK 0x03fc0000 |
| 27 | #define IOINT_AI_MASK 0x04000000 |
| 28 | |
| 29 | static int is_ioint(u64 type) |
| 30 | { |
| 31 | return ((type & 0xfffe0000u) != 0xfffe0000u); |
| 32 | } |
| 33 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 34 | int psw_extint_disabled(struct kvm_vcpu *vcpu) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 35 | { |
| 36 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT); |
| 37 | } |
| 38 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 39 | static int psw_ioint_disabled(struct kvm_vcpu *vcpu) |
| 40 | { |
| 41 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO); |
| 42 | } |
| 43 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 44 | static int psw_mchk_disabled(struct kvm_vcpu *vcpu) |
| 45 | { |
| 46 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK); |
| 47 | } |
| 48 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 49 | static int psw_interrupts_disabled(struct kvm_vcpu *vcpu) |
| 50 | { |
| 51 | if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) || |
| 52 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) || |
| 53 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT)) |
| 54 | return 0; |
| 55 | return 1; |
| 56 | } |
| 57 | |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 58 | static u64 int_word_to_isc_bits(u32 int_word) |
| 59 | { |
| 60 | u8 isc = (int_word & 0x38000000) >> 27; |
| 61 | |
| 62 | return (0x80 >> isc) << 24; |
| 63 | } |
| 64 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 65 | static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 66 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 67 | { |
| 68 | switch (inti->type) { |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 69 | case KVM_S390_INT_EXTERNAL_CALL: |
| 70 | if (psw_extint_disabled(vcpu)) |
| 71 | return 0; |
| 72 | if (vcpu->arch.sie_block->gcr[0] & 0x2000ul) |
| 73 | return 1; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 74 | case KVM_S390_INT_EMERGENCY: |
| 75 | if (psw_extint_disabled(vcpu)) |
| 76 | return 0; |
| 77 | if (vcpu->arch.sie_block->gcr[0] & 0x4000ul) |
| 78 | return 1; |
| 79 | return 0; |
| 80 | case KVM_S390_INT_SERVICE: |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 81 | case KVM_S390_INT_PFAULT_INIT: |
| 82 | case KVM_S390_INT_PFAULT_DONE: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 83 | case KVM_S390_INT_VIRTIO: |
| 84 | if (psw_extint_disabled(vcpu)) |
| 85 | return 0; |
| 86 | if (vcpu->arch.sie_block->gcr[0] & 0x200ul) |
| 87 | return 1; |
| 88 | return 0; |
| 89 | case KVM_S390_PROGRAM_INT: |
| 90 | case KVM_S390_SIGP_STOP: |
| 91 | case KVM_S390_SIGP_SET_PREFIX: |
| 92 | case KVM_S390_RESTART: |
| 93 | return 1; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 94 | case KVM_S390_MCHK: |
| 95 | if (psw_mchk_disabled(vcpu)) |
| 96 | return 0; |
| 97 | if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14) |
| 98 | return 1; |
| 99 | return 0; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 100 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 101 | if (psw_ioint_disabled(vcpu)) |
| 102 | return 0; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 103 | if (vcpu->arch.sie_block->gcr[6] & |
| 104 | int_word_to_isc_bits(inti->io.io_int_word)) |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 105 | return 1; |
| 106 | return 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 107 | default: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 108 | printk(KERN_WARNING "illegal interrupt type %llx\n", |
| 109 | inti->type); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 110 | BUG(); |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static void __set_cpu_idle(struct kvm_vcpu *vcpu) |
| 116 | { |
| 117 | BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1); |
| 118 | atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
| 119 | set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); |
| 120 | } |
| 121 | |
| 122 | static void __unset_cpu_idle(struct kvm_vcpu *vcpu) |
| 123 | { |
| 124 | BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1); |
| 125 | atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
| 126 | clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); |
| 127 | } |
| 128 | |
| 129 | static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) |
| 130 | { |
| 131 | atomic_clear_mask(CPUSTAT_ECALL_PEND | |
| 132 | CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT, |
| 133 | &vcpu->arch.sie_block->cpuflags); |
| 134 | vcpu->arch.sie_block->lctl = 0x0000; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 135 | vcpu->arch.sie_block->ictl &= ~ICTL_LPSW; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag) |
| 139 | { |
| 140 | atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags); |
| 141 | } |
| 142 | |
| 143 | static void __set_intercept_indicator(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 144 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 145 | { |
| 146 | switch (inti->type) { |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 147 | case KVM_S390_INT_EXTERNAL_CALL: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 148 | case KVM_S390_INT_EMERGENCY: |
| 149 | case KVM_S390_INT_SERVICE: |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 150 | case KVM_S390_INT_PFAULT_INIT: |
| 151 | case KVM_S390_INT_PFAULT_DONE: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 152 | case KVM_S390_INT_VIRTIO: |
| 153 | if (psw_extint_disabled(vcpu)) |
| 154 | __set_cpuflag(vcpu, CPUSTAT_EXT_INT); |
| 155 | else |
| 156 | vcpu->arch.sie_block->lctl |= LCTL_CR0; |
| 157 | break; |
| 158 | case KVM_S390_SIGP_STOP: |
| 159 | __set_cpuflag(vcpu, CPUSTAT_STOP_INT); |
| 160 | break; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 161 | case KVM_S390_MCHK: |
| 162 | if (psw_mchk_disabled(vcpu)) |
| 163 | vcpu->arch.sie_block->ictl |= ICTL_LPSW; |
| 164 | else |
| 165 | vcpu->arch.sie_block->lctl |= LCTL_CR14; |
| 166 | break; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 167 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 168 | if (psw_ioint_disabled(vcpu)) |
| 169 | __set_cpuflag(vcpu, CPUSTAT_IO_INT); |
| 170 | else |
| 171 | vcpu->arch.sie_block->lctl |= LCTL_CR6; |
| 172 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 173 | default: |
| 174 | BUG(); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static void __do_deliver_interrupt(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 179 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 180 | { |
| 181 | const unsigned short table[] = { 2, 4, 4, 6 }; |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 182 | int rc = 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 183 | |
| 184 | switch (inti->type) { |
| 185 | case KVM_S390_INT_EMERGENCY: |
| 186 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg"); |
| 187 | vcpu->stat.deliver_emergency_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 188 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 189 | inti->emerg.code, 0); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 190 | rc = put_guest(vcpu, 0x1201, (u16 __user *)__LC_EXT_INT_CODE); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 191 | rc |= put_guest(vcpu, inti->emerg.code, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 192 | (u16 __user *)__LC_EXT_CPU_ADDR); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 193 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 194 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 195 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 196 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 197 | break; |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 198 | case KVM_S390_INT_EXTERNAL_CALL: |
| 199 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call"); |
| 200 | vcpu->stat.deliver_external_call++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 201 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 202 | inti->extcall.code, 0); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 203 | rc = put_guest(vcpu, 0x1202, (u16 __user *)__LC_EXT_INT_CODE); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 204 | rc |= put_guest(vcpu, inti->extcall.code, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 205 | (u16 __user *)__LC_EXT_CPU_ADDR); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 206 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 207 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 208 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 209 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 210 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 211 | case KVM_S390_INT_SERVICE: |
| 212 | VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x", |
| 213 | inti->ext.ext_params); |
| 214 | vcpu->stat.deliver_service_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 215 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 216 | inti->ext.ext_params, 0); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 217 | rc = put_guest(vcpu, 0x2401, (u16 __user *)__LC_EXT_INT_CODE); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 218 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 219 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 220 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 221 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 222 | rc |= put_guest(vcpu, inti->ext.ext_params, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 223 | (u32 __user *)__LC_EXT_PARAMS); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 224 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 225 | case KVM_S390_INT_PFAULT_INIT: |
| 226 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0, |
| 227 | inti->ext.ext_params2); |
| 228 | rc = put_guest(vcpu, 0x2603, (u16 __user *) __LC_EXT_INT_CODE); |
| 229 | rc |= put_guest(vcpu, 0x0600, (u16 __user *) __LC_EXT_CPU_ADDR); |
| 230 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 231 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 232 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 233 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
| 234 | rc |= put_guest(vcpu, inti->ext.ext_params2, |
| 235 | (u64 __user *) __LC_EXT_PARAMS2); |
| 236 | break; |
| 237 | case KVM_S390_INT_PFAULT_DONE: |
| 238 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0, |
| 239 | inti->ext.ext_params2); |
| 240 | rc = put_guest(vcpu, 0x2603, (u16 __user *) __LC_EXT_INT_CODE); |
| 241 | rc |= put_guest(vcpu, 0x0680, (u16 __user *) __LC_EXT_CPU_ADDR); |
| 242 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 243 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 244 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 245 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
| 246 | rc |= put_guest(vcpu, inti->ext.ext_params2, |
| 247 | (u64 __user *) __LC_EXT_PARAMS2); |
| 248 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 249 | case KVM_S390_INT_VIRTIO: |
Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 250 | VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx", |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 251 | inti->ext.ext_params, inti->ext.ext_params2); |
| 252 | vcpu->stat.deliver_virtio_interrupt++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 253 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 254 | inti->ext.ext_params, |
| 255 | inti->ext.ext_params2); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 256 | rc = put_guest(vcpu, 0x2603, (u16 __user *)__LC_EXT_INT_CODE); |
| 257 | rc |= put_guest(vcpu, 0x0d00, (u16 __user *)__LC_EXT_CPU_ADDR); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 258 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 259 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 260 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 261 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 262 | rc |= put_guest(vcpu, inti->ext.ext_params, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 263 | (u32 __user *)__LC_EXT_PARAMS); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 264 | rc |= put_guest(vcpu, inti->ext.ext_params2, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 265 | (u64 __user *)__LC_EXT_PARAMS2); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 266 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 267 | case KVM_S390_SIGP_STOP: |
| 268 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop"); |
| 269 | vcpu->stat.deliver_stop_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 270 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 271 | 0, 0); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 272 | __set_intercept_indicator(vcpu, inti); |
| 273 | break; |
| 274 | |
| 275 | case KVM_S390_SIGP_SET_PREFIX: |
| 276 | VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", |
| 277 | inti->prefix.address); |
| 278 | vcpu->stat.deliver_prefix_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 279 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 280 | inti->prefix.address, 0); |
Christian Borntraeger | 8d26cf7 | 2012-01-11 11:19:32 +0100 | [diff] [blame] | 281 | kvm_s390_set_prefix(vcpu, inti->prefix.address); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 282 | break; |
| 283 | |
| 284 | case KVM_S390_RESTART: |
| 285 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart"); |
| 286 | vcpu->stat.deliver_restart_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 287 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 288 | 0, 0); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 289 | rc = copy_to_guest(vcpu, |
| 290 | offsetof(struct _lowcore, restart_old_psw), |
| 291 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 292 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 293 | offsetof(struct _lowcore, restart_psw), |
| 294 | sizeof(psw_t)); |
Cornelia Huck | 9e6dabe | 2011-11-17 11:00:41 +0100 | [diff] [blame] | 295 | atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 296 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 297 | case KVM_S390_PROGRAM_INT: |
| 298 | VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x", |
| 299 | inti->pgm.code, |
| 300 | table[vcpu->arch.sie_block->ipa >> 14]); |
| 301 | vcpu->stat.deliver_program_int++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 302 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 303 | inti->pgm.code, 0); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 304 | rc = put_guest(vcpu, inti->pgm.code, (u16 __user *)__LC_PGM_INT_CODE); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 305 | rc |= put_guest(vcpu, table[vcpu->arch.sie_block->ipa >> 14], |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 306 | (u16 __user *)__LC_PGM_ILC); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 307 | rc |= copy_to_guest(vcpu, __LC_PGM_OLD_PSW, |
| 308 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 309 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 310 | __LC_PGM_NEW_PSW, sizeof(psw_t)); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 311 | break; |
| 312 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 313 | case KVM_S390_MCHK: |
| 314 | VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx", |
| 315 | inti->mchk.mcic); |
| 316 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 317 | inti->mchk.cr14, |
| 318 | inti->mchk.mcic); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 319 | rc = kvm_s390_vcpu_store_status(vcpu, |
| 320 | KVM_S390_STORE_STATUS_PREFIXED); |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 321 | rc |= put_guest(vcpu, inti->mchk.mcic, (u64 __user *) __LC_MCCK_CODE); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 322 | rc |= copy_to_guest(vcpu, __LC_MCK_OLD_PSW, |
| 323 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 324 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 325 | __LC_MCK_NEW_PSW, sizeof(psw_t)); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 326 | break; |
| 327 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 328 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 329 | { |
| 330 | __u32 param0 = ((__u32)inti->io.subchannel_id << 16) | |
| 331 | inti->io.subchannel_nr; |
| 332 | __u64 param1 = ((__u64)inti->io.io_int_parm << 32) | |
| 333 | inti->io.io_int_word; |
| 334 | VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type); |
| 335 | vcpu->stat.deliver_io_int++; |
| 336 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 337 | param0, param1); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 338 | rc = put_guest(vcpu, inti->io.subchannel_id, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 339 | (u16 __user *) __LC_SUBCHANNEL_ID); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 340 | rc |= put_guest(vcpu, inti->io.subchannel_nr, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 341 | (u16 __user *) __LC_SUBCHANNEL_NR); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 342 | rc |= put_guest(vcpu, inti->io.io_int_parm, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 343 | (u32 __user *) __LC_IO_INT_PARM); |
Heiko Carstens | 396083a | 2013-03-05 13:14:44 +0100 | [diff] [blame] | 344 | rc |= put_guest(vcpu, inti->io.io_int_word, |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 345 | (u32 __user *) __LC_IO_INT_WORD); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 346 | rc |= copy_to_guest(vcpu, __LC_IO_OLD_PSW, |
| 347 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 348 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 349 | __LC_IO_NEW_PSW, sizeof(psw_t)); |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 350 | break; |
| 351 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 352 | default: |
| 353 | BUG(); |
| 354 | } |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 355 | if (rc) { |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 356 | printk("kvm: The guest lowcore is not mapped during interrupt " |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 357 | "delivery, killing userspace\n"); |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 358 | do_exit(SIGKILL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
| 362 | static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu) |
| 363 | { |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 364 | int rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 365 | |
| 366 | if (psw_extint_disabled(vcpu)) |
| 367 | return 0; |
| 368 | if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul)) |
| 369 | return 0; |
Heiko Carstens | 0a75ca2 | 2013-03-05 13:14:47 +0100 | [diff] [blame] | 370 | rc = put_guest(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 371 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 372 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 373 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 374 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
| 375 | if (rc) { |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 376 | printk("kvm: The guest lowcore is not mapped during interrupt " |
| 377 | "delivery, killing userspace\n"); |
| 378 | do_exit(SIGKILL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 379 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 380 | return 1; |
| 381 | } |
| 382 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 383 | int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 384 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 385 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 386 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 387 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 388 | int rc = 0; |
| 389 | |
| 390 | if (atomic_read(&li->active)) { |
| 391 | spin_lock_bh(&li->lock); |
| 392 | list_for_each_entry(inti, &li->list, list) |
| 393 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 394 | rc = 1; |
| 395 | break; |
| 396 | } |
| 397 | spin_unlock_bh(&li->lock); |
| 398 | } |
| 399 | |
| 400 | if ((!rc) && atomic_read(&fi->active)) { |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 401 | spin_lock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 402 | list_for_each_entry(inti, &fi->list, list) |
| 403 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 404 | rc = 1; |
| 405 | break; |
| 406 | } |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 407 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | if ((!rc) && (vcpu->arch.sie_block->ckc < |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 411 | get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 412 | if ((!psw_extint_disabled(vcpu)) && |
| 413 | (vcpu->arch.sie_block->gcr[0] & 0x800ul)) |
| 414 | rc = 1; |
| 415 | } |
| 416 | |
| 417 | return rc; |
| 418 | } |
| 419 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 420 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 421 | { |
| 422 | return 0; |
| 423 | } |
| 424 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 425 | int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) |
| 426 | { |
| 427 | u64 now, sltime; |
| 428 | DECLARE_WAITQUEUE(wait, current); |
| 429 | |
| 430 | vcpu->stat.exit_wait_state++; |
| 431 | if (kvm_cpu_has_interrupt(vcpu)) |
| 432 | return 0; |
| 433 | |
Carsten Otte | e52b2af | 2008-05-21 13:37:44 +0200 | [diff] [blame] | 434 | __set_cpu_idle(vcpu); |
| 435 | spin_lock_bh(&vcpu->arch.local_int.lock); |
| 436 | vcpu->arch.local_int.timer_due = 0; |
| 437 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
| 438 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 439 | if (psw_interrupts_disabled(vcpu)) { |
| 440 | VCPU_EVENT(vcpu, 3, "%s", "disabled wait"); |
| 441 | __unset_cpu_idle(vcpu); |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 442 | return -EOPNOTSUPP; /* disabled wait */ |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (psw_extint_disabled(vcpu) || |
| 446 | (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) { |
| 447 | VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer"); |
| 448 | goto no_timer; |
| 449 | } |
| 450 | |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 451 | now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 452 | if (vcpu->arch.sie_block->ckc < now) { |
| 453 | __unset_cpu_idle(vcpu); |
| 454 | return 0; |
| 455 | } |
| 456 | |
Heiko Carstens | ed4f209 | 2013-01-14 16:55:55 +0100 | [diff] [blame] | 457 | sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 458 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 459 | hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL); |
| 460 | VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 461 | no_timer: |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 462 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 463 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 464 | spin_lock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 465 | add_wait_queue(&vcpu->wq, &wait); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 466 | while (list_empty(&vcpu->arch.local_int.list) && |
| 467 | list_empty(&vcpu->arch.local_int.float_int->list) && |
| 468 | (!vcpu->arch.local_int.timer_due) && |
| 469 | !signal_pending(current)) { |
| 470 | set_current_state(TASK_INTERRUPTIBLE); |
| 471 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 472 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 473 | schedule(); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 474 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 475 | spin_lock_bh(&vcpu->arch.local_int.lock); |
| 476 | } |
| 477 | __unset_cpu_idle(vcpu); |
| 478 | __set_current_state(TASK_RUNNING); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 479 | remove_wait_queue(&vcpu->wq, &wait); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 480 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 481 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 482 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 483 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 484 | hrtimer_try_to_cancel(&vcpu->arch.ckc_timer); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 488 | void kvm_s390_tasklet(unsigned long parm) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 489 | { |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 490 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 491 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 492 | spin_lock(&vcpu->arch.local_int.lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 493 | vcpu->arch.local_int.timer_due = 1; |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 494 | if (waitqueue_active(&vcpu->wq)) |
| 495 | wake_up_interruptible(&vcpu->wq); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 496 | spin_unlock(&vcpu->arch.local_int.lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 497 | } |
| 498 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 499 | /* |
| 500 | * low level hrtimer wake routine. Because this runs in hardirq context |
| 501 | * we schedule a tasklet to do the real work. |
| 502 | */ |
| 503 | enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer) |
| 504 | { |
| 505 | struct kvm_vcpu *vcpu; |
| 506 | |
| 507 | vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer); |
Michael Mueller | 9cac38d | 2014-02-26 16:14:19 +0100 | [diff] [blame] | 508 | vcpu->preempted = true; |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 509 | tasklet_schedule(&vcpu->arch.tasklet); |
| 510 | |
| 511 | return HRTIMER_NORESTART; |
| 512 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 513 | |
| 514 | void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) |
| 515 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 516 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 517 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 518 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 519 | int deliver; |
| 520 | |
| 521 | __reset_intercept_indicators(vcpu); |
| 522 | if (atomic_read(&li->active)) { |
| 523 | do { |
| 524 | deliver = 0; |
| 525 | spin_lock_bh(&li->lock); |
| 526 | list_for_each_entry_safe(inti, n, &li->list, list) { |
| 527 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 528 | list_del(&inti->list); |
| 529 | deliver = 1; |
| 530 | break; |
| 531 | } |
| 532 | __set_intercept_indicator(vcpu, inti); |
| 533 | } |
| 534 | if (list_empty(&li->list)) |
| 535 | atomic_set(&li->active, 0); |
| 536 | spin_unlock_bh(&li->lock); |
| 537 | if (deliver) { |
| 538 | __do_deliver_interrupt(vcpu, inti); |
| 539 | kfree(inti); |
| 540 | } |
| 541 | } while (deliver); |
| 542 | } |
| 543 | |
| 544 | if ((vcpu->arch.sie_block->ckc < |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 545 | get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 546 | __try_deliver_ckc_interrupt(vcpu); |
| 547 | |
| 548 | if (atomic_read(&fi->active)) { |
| 549 | do { |
| 550 | deliver = 0; |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 551 | spin_lock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 552 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 553 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 554 | list_del(&inti->list); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 555 | fi->irq_count--; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 556 | deliver = 1; |
| 557 | break; |
| 558 | } |
| 559 | __set_intercept_indicator(vcpu, inti); |
| 560 | } |
| 561 | if (list_empty(&fi->list)) |
| 562 | atomic_set(&fi->active, 0); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 563 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 564 | if (deliver) { |
| 565 | __do_deliver_interrupt(vcpu, inti); |
| 566 | kfree(inti); |
| 567 | } |
| 568 | } while (deliver); |
| 569 | } |
| 570 | } |
| 571 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 572 | void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu) |
| 573 | { |
| 574 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 575 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 576 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
| 577 | int deliver; |
| 578 | |
| 579 | __reset_intercept_indicators(vcpu); |
| 580 | if (atomic_read(&li->active)) { |
| 581 | do { |
| 582 | deliver = 0; |
| 583 | spin_lock_bh(&li->lock); |
| 584 | list_for_each_entry_safe(inti, n, &li->list, list) { |
| 585 | if ((inti->type == KVM_S390_MCHK) && |
| 586 | __interrupt_is_deliverable(vcpu, inti)) { |
| 587 | list_del(&inti->list); |
| 588 | deliver = 1; |
| 589 | break; |
| 590 | } |
| 591 | __set_intercept_indicator(vcpu, inti); |
| 592 | } |
| 593 | if (list_empty(&li->list)) |
| 594 | atomic_set(&li->active, 0); |
| 595 | spin_unlock_bh(&li->lock); |
| 596 | if (deliver) { |
| 597 | __do_deliver_interrupt(vcpu, inti); |
| 598 | kfree(inti); |
| 599 | } |
| 600 | } while (deliver); |
| 601 | } |
| 602 | |
| 603 | if (atomic_read(&fi->active)) { |
| 604 | do { |
| 605 | deliver = 0; |
| 606 | spin_lock(&fi->lock); |
| 607 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 608 | if ((inti->type == KVM_S390_MCHK) && |
| 609 | __interrupt_is_deliverable(vcpu, inti)) { |
| 610 | list_del(&inti->list); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 611 | fi->irq_count--; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 612 | deliver = 1; |
| 613 | break; |
| 614 | } |
| 615 | __set_intercept_indicator(vcpu, inti); |
| 616 | } |
| 617 | if (list_empty(&fi->list)) |
| 618 | atomic_set(&fi->active, 0); |
| 619 | spin_unlock(&fi->lock); |
| 620 | if (deliver) { |
| 621 | __do_deliver_interrupt(vcpu, inti); |
| 622 | kfree(inti); |
| 623 | } |
| 624 | } while (deliver); |
| 625 | } |
| 626 | } |
| 627 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 628 | int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code) |
| 629 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 630 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 631 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 632 | |
| 633 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 634 | if (!inti) |
| 635 | return -ENOMEM; |
| 636 | |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 637 | inti->type = KVM_S390_PROGRAM_INT; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 638 | inti->pgm.code = code; |
| 639 | |
| 640 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code); |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 641 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, inti->type, code, 0, 1); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 642 | spin_lock_bh(&li->lock); |
| 643 | list_add(&inti->list, &li->list); |
| 644 | atomic_set(&li->active, 1); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 645 | BUG_ON(waitqueue_active(li->wq)); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 646 | spin_unlock_bh(&li->lock); |
| 647 | return 0; |
| 648 | } |
| 649 | |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 650 | struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm, |
| 651 | u64 cr6, u64 schid) |
| 652 | { |
| 653 | struct kvm_s390_float_interrupt *fi; |
| 654 | struct kvm_s390_interrupt_info *inti, *iter; |
| 655 | |
| 656 | if ((!schid && !cr6) || (schid && cr6)) |
| 657 | return NULL; |
| 658 | mutex_lock(&kvm->lock); |
| 659 | fi = &kvm->arch.float_int; |
| 660 | spin_lock(&fi->lock); |
| 661 | inti = NULL; |
| 662 | list_for_each_entry(iter, &fi->list, list) { |
| 663 | if (!is_ioint(iter->type)) |
| 664 | continue; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 665 | if (cr6 && |
| 666 | ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0)) |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 667 | continue; |
| 668 | if (schid) { |
| 669 | if (((schid & 0x00000000ffff0000) >> 16) != |
| 670 | iter->io.subchannel_id) |
| 671 | continue; |
| 672 | if ((schid & 0x000000000000ffff) != |
| 673 | iter->io.subchannel_nr) |
| 674 | continue; |
| 675 | } |
| 676 | inti = iter; |
| 677 | break; |
| 678 | } |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 679 | if (inti) { |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 680 | list_del_init(&inti->list); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 681 | fi->irq_count--; |
| 682 | } |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 683 | if (list_empty(&fi->list)) |
| 684 | atomic_set(&fi->active, 0); |
| 685 | spin_unlock(&fi->lock); |
| 686 | mutex_unlock(&kvm->lock); |
| 687 | return inti; |
| 688 | } |
| 689 | |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 690 | static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 691 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 692 | struct kvm_s390_local_interrupt *li; |
| 693 | struct kvm_s390_float_interrupt *fi; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 694 | struct kvm_s390_interrupt_info *iter; |
Jens Freimann | 1ee0bc5 | 2014-02-25 15:36:45 +0100 | [diff] [blame^] | 695 | struct kvm_vcpu *dst_vcpu = NULL; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 696 | int sigcpu; |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 697 | int rc = 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 698 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 699 | mutex_lock(&kvm->lock); |
| 700 | fi = &kvm->arch.float_int; |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 701 | spin_lock(&fi->lock); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 702 | if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) { |
| 703 | rc = -EINVAL; |
| 704 | goto unlock_fi; |
| 705 | } |
| 706 | fi->irq_count++; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 707 | if (!is_ioint(inti->type)) { |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 708 | list_add_tail(&inti->list, &fi->list); |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 709 | } else { |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 710 | u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word); |
| 711 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 712 | /* Keep I/O interrupts sorted in isc order. */ |
| 713 | list_for_each_entry(iter, &fi->list, list) { |
| 714 | if (!is_ioint(iter->type)) |
| 715 | continue; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 716 | if (int_word_to_isc_bits(iter->io.io_int_word) |
| 717 | <= isc_bits) |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 718 | continue; |
| 719 | break; |
| 720 | } |
| 721 | list_add_tail(&inti->list, &iter->list); |
| 722 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 723 | atomic_set(&fi->active, 1); |
| 724 | sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS); |
| 725 | if (sigcpu == KVM_MAX_VCPUS) { |
| 726 | do { |
| 727 | sigcpu = fi->next_rr_cpu++; |
| 728 | if (sigcpu == KVM_MAX_VCPUS) |
| 729 | sigcpu = fi->next_rr_cpu = 0; |
Jens Freimann | 1ee0bc5 | 2014-02-25 15:36:45 +0100 | [diff] [blame^] | 730 | } while (kvm_get_vcpu(kvm, sigcpu) == NULL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 731 | } |
Jens Freimann | 1ee0bc5 | 2014-02-25 15:36:45 +0100 | [diff] [blame^] | 732 | dst_vcpu = kvm_get_vcpu(kvm, sigcpu); |
| 733 | li = &dst_vcpu->arch.local_int; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 734 | spin_lock_bh(&li->lock); |
| 735 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 736 | if (waitqueue_active(li->wq)) |
| 737 | wake_up_interruptible(li->wq); |
Michael Mueller | 9cac38d | 2014-02-26 16:14:19 +0100 | [diff] [blame] | 738 | kvm_get_vcpu(kvm, sigcpu)->preempted = true; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 739 | spin_unlock_bh(&li->lock); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 740 | unlock_fi: |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 741 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 742 | mutex_unlock(&kvm->lock); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 743 | return rc; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | int kvm_s390_inject_vm(struct kvm *kvm, |
| 747 | struct kvm_s390_interrupt *s390int) |
| 748 | { |
| 749 | struct kvm_s390_interrupt_info *inti; |
| 750 | |
| 751 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 752 | if (!inti) |
| 753 | return -ENOMEM; |
| 754 | |
| 755 | inti->type = s390int->type; |
| 756 | switch (inti->type) { |
| 757 | case KVM_S390_INT_VIRTIO: |
| 758 | VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx", |
| 759 | s390int->parm, s390int->parm64); |
| 760 | inti->ext.ext_params = s390int->parm; |
| 761 | inti->ext.ext_params2 = s390int->parm64; |
| 762 | break; |
| 763 | case KVM_S390_INT_SERVICE: |
| 764 | VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm); |
| 765 | inti->ext.ext_params = s390int->parm; |
| 766 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 767 | case KVM_S390_INT_PFAULT_DONE: |
| 768 | inti->type = s390int->type; |
| 769 | inti->ext.ext_params2 = s390int->parm64; |
| 770 | break; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 771 | case KVM_S390_MCHK: |
| 772 | VM_EVENT(kvm, 5, "inject: machine check parm64:%llx", |
| 773 | s390int->parm64); |
| 774 | inti->mchk.cr14 = s390int->parm; /* upper bits are not used */ |
| 775 | inti->mchk.mcic = s390int->parm64; |
| 776 | break; |
| 777 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 778 | if (inti->type & IOINT_AI_MASK) |
| 779 | VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)"); |
| 780 | else |
| 781 | VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x", |
| 782 | s390int->type & IOINT_CSSID_MASK, |
| 783 | s390int->type & IOINT_SSID_MASK, |
| 784 | s390int->type & IOINT_SCHID_MASK); |
| 785 | inti->io.subchannel_id = s390int->parm >> 16; |
| 786 | inti->io.subchannel_nr = s390int->parm & 0x0000ffffu; |
| 787 | inti->io.io_int_parm = s390int->parm64 >> 32; |
| 788 | inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull; |
| 789 | break; |
| 790 | default: |
| 791 | kfree(inti); |
| 792 | return -EINVAL; |
| 793 | } |
| 794 | trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64, |
| 795 | 2); |
| 796 | |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 797 | return __inject_vm(kvm, inti); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, |
| 801 | struct kvm_s390_interrupt *s390int) |
| 802 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 803 | struct kvm_s390_local_interrupt *li; |
| 804 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 805 | |
| 806 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 807 | if (!inti) |
| 808 | return -ENOMEM; |
| 809 | |
| 810 | switch (s390int->type) { |
| 811 | case KVM_S390_PROGRAM_INT: |
| 812 | if (s390int->parm & 0xffff0000) { |
| 813 | kfree(inti); |
| 814 | return -EINVAL; |
| 815 | } |
| 816 | inti->type = s390int->type; |
| 817 | inti->pgm.code = s390int->parm; |
| 818 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)", |
| 819 | s390int->parm); |
| 820 | break; |
Christian Borntraeger | b7e6e4d | 2009-01-22 10:29:08 +0100 | [diff] [blame] | 821 | case KVM_S390_SIGP_SET_PREFIX: |
| 822 | inti->prefix.address = s390int->parm; |
| 823 | inti->type = s390int->type; |
| 824 | VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)", |
| 825 | s390int->parm); |
| 826 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 827 | case KVM_S390_SIGP_STOP: |
| 828 | case KVM_S390_RESTART: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 829 | VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type); |
| 830 | inti->type = s390int->type; |
| 831 | break; |
Jason J. Herne | 82a1273 | 2012-10-02 16:25:36 +0200 | [diff] [blame] | 832 | case KVM_S390_INT_EXTERNAL_CALL: |
| 833 | if (s390int->parm & 0xffff0000) { |
| 834 | kfree(inti); |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u", |
| 838 | s390int->parm); |
| 839 | inti->type = s390int->type; |
| 840 | inti->extcall.code = s390int->parm; |
| 841 | break; |
| 842 | case KVM_S390_INT_EMERGENCY: |
| 843 | if (s390int->parm & 0xffff0000) { |
| 844 | kfree(inti); |
| 845 | return -EINVAL; |
| 846 | } |
| 847 | VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm); |
| 848 | inti->type = s390int->type; |
| 849 | inti->emerg.code = s390int->parm; |
| 850 | break; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 851 | case KVM_S390_MCHK: |
| 852 | VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx", |
| 853 | s390int->parm64); |
| 854 | inti->type = s390int->type; |
| 855 | inti->mchk.mcic = s390int->parm64; |
| 856 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 857 | case KVM_S390_INT_PFAULT_INIT: |
| 858 | inti->type = s390int->type; |
| 859 | inti->ext.ext_params2 = s390int->parm64; |
| 860 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 861 | case KVM_S390_INT_VIRTIO: |
| 862 | case KVM_S390_INT_SERVICE: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 863 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 864 | default: |
| 865 | kfree(inti); |
| 866 | return -EINVAL; |
| 867 | } |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 868 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm, |
| 869 | s390int->parm64, 2); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 870 | |
| 871 | mutex_lock(&vcpu->kvm->lock); |
| 872 | li = &vcpu->arch.local_int; |
| 873 | spin_lock_bh(&li->lock); |
| 874 | if (inti->type == KVM_S390_PROGRAM_INT) |
| 875 | list_add(&inti->list, &li->list); |
| 876 | else |
| 877 | list_add_tail(&inti->list, &li->list); |
| 878 | atomic_set(&li->active, 1); |
| 879 | if (inti->type == KVM_S390_SIGP_STOP) |
| 880 | li->action_bits |= ACTION_STOP_ON_STOP; |
| 881 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 882 | if (waitqueue_active(&vcpu->wq)) |
| 883 | wake_up_interruptible(&vcpu->wq); |
Michael Mueller | 9cac38d | 2014-02-26 16:14:19 +0100 | [diff] [blame] | 884 | vcpu->preempted = true; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 885 | spin_unlock_bh(&li->lock); |
| 886 | mutex_unlock(&vcpu->kvm->lock); |
| 887 | return 0; |
| 888 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 889 | |
| 890 | static void clear_floating_interrupts(struct kvm *kvm) |
| 891 | { |
| 892 | struct kvm_s390_float_interrupt *fi; |
| 893 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
| 894 | |
| 895 | mutex_lock(&kvm->lock); |
| 896 | fi = &kvm->arch.float_int; |
| 897 | spin_lock(&fi->lock); |
| 898 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 899 | list_del(&inti->list); |
| 900 | kfree(inti); |
| 901 | } |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 902 | fi->irq_count = 0; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 903 | atomic_set(&fi->active, 0); |
| 904 | spin_unlock(&fi->lock); |
| 905 | mutex_unlock(&kvm->lock); |
| 906 | } |
| 907 | |
| 908 | static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti, |
| 909 | u8 *addr) |
| 910 | { |
| 911 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; |
| 912 | struct kvm_s390_irq irq = {0}; |
| 913 | |
| 914 | irq.type = inti->type; |
| 915 | switch (inti->type) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 916 | case KVM_S390_INT_PFAULT_INIT: |
| 917 | case KVM_S390_INT_PFAULT_DONE: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 918 | case KVM_S390_INT_VIRTIO: |
| 919 | case KVM_S390_INT_SERVICE: |
| 920 | irq.u.ext = inti->ext; |
| 921 | break; |
| 922 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 923 | irq.u.io = inti->io; |
| 924 | break; |
| 925 | case KVM_S390_MCHK: |
| 926 | irq.u.mchk = inti->mchk; |
| 927 | break; |
| 928 | default: |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | |
| 932 | if (copy_to_user(uptr, &irq, sizeof(irq))) |
| 933 | return -EFAULT; |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len) |
| 939 | { |
| 940 | struct kvm_s390_interrupt_info *inti; |
| 941 | struct kvm_s390_float_interrupt *fi; |
| 942 | int ret = 0; |
| 943 | int n = 0; |
| 944 | |
| 945 | mutex_lock(&kvm->lock); |
| 946 | fi = &kvm->arch.float_int; |
| 947 | spin_lock(&fi->lock); |
| 948 | |
| 949 | list_for_each_entry(inti, &fi->list, list) { |
| 950 | if (len < sizeof(struct kvm_s390_irq)) { |
| 951 | /* signal userspace to try again */ |
| 952 | ret = -ENOMEM; |
| 953 | break; |
| 954 | } |
| 955 | ret = copy_irq_to_user(inti, buf); |
| 956 | if (ret) |
| 957 | break; |
| 958 | buf += sizeof(struct kvm_s390_irq); |
| 959 | len -= sizeof(struct kvm_s390_irq); |
| 960 | n++; |
| 961 | } |
| 962 | |
| 963 | spin_unlock(&fi->lock); |
| 964 | mutex_unlock(&kvm->lock); |
| 965 | |
| 966 | return ret < 0 ? ret : n; |
| 967 | } |
| 968 | |
| 969 | static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
| 970 | { |
| 971 | int r; |
| 972 | |
| 973 | switch (attr->group) { |
| 974 | case KVM_DEV_FLIC_GET_ALL_IRQS: |
| 975 | r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr, |
| 976 | attr->attr); |
| 977 | break; |
| 978 | default: |
| 979 | r = -EINVAL; |
| 980 | } |
| 981 | |
| 982 | return r; |
| 983 | } |
| 984 | |
| 985 | static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti, |
| 986 | u64 addr) |
| 987 | { |
| 988 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; |
| 989 | void *target = NULL; |
| 990 | void __user *source; |
| 991 | u64 size; |
| 992 | |
| 993 | if (get_user(inti->type, (u64 __user *)addr)) |
| 994 | return -EFAULT; |
| 995 | |
| 996 | switch (inti->type) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 997 | case KVM_S390_INT_PFAULT_INIT: |
| 998 | case KVM_S390_INT_PFAULT_DONE: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 999 | case KVM_S390_INT_VIRTIO: |
| 1000 | case KVM_S390_INT_SERVICE: |
| 1001 | target = (void *) &inti->ext; |
| 1002 | source = &uptr->u.ext; |
| 1003 | size = sizeof(inti->ext); |
| 1004 | break; |
| 1005 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 1006 | target = (void *) &inti->io; |
| 1007 | source = &uptr->u.io; |
| 1008 | size = sizeof(inti->io); |
| 1009 | break; |
| 1010 | case KVM_S390_MCHK: |
| 1011 | target = (void *) &inti->mchk; |
| 1012 | source = &uptr->u.mchk; |
| 1013 | size = sizeof(inti->mchk); |
| 1014 | break; |
| 1015 | default: |
| 1016 | return -EINVAL; |
| 1017 | } |
| 1018 | |
| 1019 | if (copy_from_user(target, source, size)) |
| 1020 | return -EFAULT; |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | static int enqueue_floating_irq(struct kvm_device *dev, |
| 1026 | struct kvm_device_attr *attr) |
| 1027 | { |
| 1028 | struct kvm_s390_interrupt_info *inti = NULL; |
| 1029 | int r = 0; |
| 1030 | int len = attr->attr; |
| 1031 | |
| 1032 | if (len % sizeof(struct kvm_s390_irq) != 0) |
| 1033 | return -EINVAL; |
| 1034 | else if (len > KVM_S390_FLIC_MAX_BUFFER) |
| 1035 | return -EINVAL; |
| 1036 | |
| 1037 | while (len >= sizeof(struct kvm_s390_irq)) { |
| 1038 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 1039 | if (!inti) |
| 1040 | return -ENOMEM; |
| 1041 | |
| 1042 | r = copy_irq_from_user(inti, attr->addr); |
| 1043 | if (r) { |
| 1044 | kfree(inti); |
| 1045 | return r; |
| 1046 | } |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 1047 | r = __inject_vm(dev->kvm, inti); |
| 1048 | if (r) { |
| 1049 | kfree(inti); |
| 1050 | return r; |
| 1051 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1052 | len -= sizeof(struct kvm_s390_irq); |
| 1053 | attr->addr += sizeof(struct kvm_s390_irq); |
| 1054 | } |
| 1055 | |
| 1056 | return r; |
| 1057 | } |
| 1058 | |
| 1059 | static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
| 1060 | { |
| 1061 | int r = 0; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1062 | unsigned int i; |
| 1063 | struct kvm_vcpu *vcpu; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1064 | |
| 1065 | switch (attr->group) { |
| 1066 | case KVM_DEV_FLIC_ENQUEUE: |
| 1067 | r = enqueue_floating_irq(dev, attr); |
| 1068 | break; |
| 1069 | case KVM_DEV_FLIC_CLEAR_IRQS: |
| 1070 | r = 0; |
| 1071 | clear_floating_interrupts(dev->kvm); |
| 1072 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1073 | case KVM_DEV_FLIC_APF_ENABLE: |
| 1074 | dev->kvm->arch.gmap->pfault_enabled = 1; |
| 1075 | break; |
| 1076 | case KVM_DEV_FLIC_APF_DISABLE_WAIT: |
| 1077 | dev->kvm->arch.gmap->pfault_enabled = 0; |
| 1078 | /* |
| 1079 | * Make sure no async faults are in transition when |
| 1080 | * clearing the queues. So we don't need to worry |
| 1081 | * about late coming workers. |
| 1082 | */ |
| 1083 | synchronize_srcu(&dev->kvm->srcu); |
| 1084 | kvm_for_each_vcpu(i, vcpu, dev->kvm) |
| 1085 | kvm_clear_async_pf_completion_queue(vcpu); |
| 1086 | break; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1087 | default: |
| 1088 | r = -EINVAL; |
| 1089 | } |
| 1090 | |
| 1091 | return r; |
| 1092 | } |
| 1093 | |
| 1094 | static int flic_create(struct kvm_device *dev, u32 type) |
| 1095 | { |
| 1096 | if (!dev) |
| 1097 | return -EINVAL; |
| 1098 | if (dev->kvm->arch.flic) |
| 1099 | return -EINVAL; |
| 1100 | dev->kvm->arch.flic = dev; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | static void flic_destroy(struct kvm_device *dev) |
| 1105 | { |
| 1106 | dev->kvm->arch.flic = NULL; |
| 1107 | kfree(dev); |
| 1108 | } |
| 1109 | |
| 1110 | /* s390 floating irq controller (flic) */ |
| 1111 | struct kvm_device_ops kvm_flic_ops = { |
| 1112 | .name = "kvm-flic", |
| 1113 | .get_attr = flic_get_attr, |
| 1114 | .set_attr = flic_set_attr, |
| 1115 | .create = flic_create, |
| 1116 | .destroy = flic_destroy, |
| 1117 | }; |