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 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 34 | static int psw_extint_disabled(struct kvm_vcpu *vcpu) |
| 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: |
| 81 | if (psw_extint_disabled(vcpu)) |
| 82 | return 0; |
| 83 | if (vcpu->arch.sie_block->gcr[0] & 0x200ul) |
| 84 | return 1; |
| 85 | return 0; |
| 86 | case KVM_S390_INT_VIRTIO: |
| 87 | if (psw_extint_disabled(vcpu)) |
| 88 | return 0; |
| 89 | if (vcpu->arch.sie_block->gcr[0] & 0x200ul) |
| 90 | return 1; |
| 91 | return 0; |
| 92 | case KVM_S390_PROGRAM_INT: |
| 93 | case KVM_S390_SIGP_STOP: |
| 94 | case KVM_S390_SIGP_SET_PREFIX: |
| 95 | case KVM_S390_RESTART: |
| 96 | return 1; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 97 | case KVM_S390_MCHK: |
| 98 | if (psw_mchk_disabled(vcpu)) |
| 99 | return 0; |
| 100 | if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14) |
| 101 | return 1; |
| 102 | return 0; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 103 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 104 | if (psw_ioint_disabled(vcpu)) |
| 105 | return 0; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 106 | if (vcpu->arch.sie_block->gcr[6] & |
| 107 | int_word_to_isc_bits(inti->io.io_int_word)) |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 108 | return 1; |
| 109 | return 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 110 | default: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 111 | printk(KERN_WARNING "illegal interrupt type %llx\n", |
| 112 | inti->type); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 113 | BUG(); |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static void __set_cpu_idle(struct kvm_vcpu *vcpu) |
| 119 | { |
| 120 | BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1); |
| 121 | atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
| 122 | set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); |
| 123 | } |
| 124 | |
| 125 | static void __unset_cpu_idle(struct kvm_vcpu *vcpu) |
| 126 | { |
| 127 | BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1); |
| 128 | atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
| 129 | clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); |
| 130 | } |
| 131 | |
| 132 | static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) |
| 133 | { |
| 134 | atomic_clear_mask(CPUSTAT_ECALL_PEND | |
| 135 | CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT, |
| 136 | &vcpu->arch.sie_block->cpuflags); |
| 137 | vcpu->arch.sie_block->lctl = 0x0000; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 138 | vcpu->arch.sie_block->ictl &= ~ICTL_LPSW; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag) |
| 142 | { |
| 143 | atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags); |
| 144 | } |
| 145 | |
| 146 | static void __set_intercept_indicator(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 147 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 148 | { |
| 149 | switch (inti->type) { |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 150 | case KVM_S390_INT_EXTERNAL_CALL: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 151 | case KVM_S390_INT_EMERGENCY: |
| 152 | case KVM_S390_INT_SERVICE: |
| 153 | case KVM_S390_INT_VIRTIO: |
| 154 | if (psw_extint_disabled(vcpu)) |
| 155 | __set_cpuflag(vcpu, CPUSTAT_EXT_INT); |
| 156 | else |
| 157 | vcpu->arch.sie_block->lctl |= LCTL_CR0; |
| 158 | break; |
| 159 | case KVM_S390_SIGP_STOP: |
| 160 | __set_cpuflag(vcpu, CPUSTAT_STOP_INT); |
| 161 | break; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 162 | case KVM_S390_MCHK: |
| 163 | if (psw_mchk_disabled(vcpu)) |
| 164 | vcpu->arch.sie_block->ictl |= ICTL_LPSW; |
| 165 | else |
| 166 | vcpu->arch.sie_block->lctl |= LCTL_CR14; |
| 167 | break; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 168 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 169 | if (psw_ioint_disabled(vcpu)) |
| 170 | __set_cpuflag(vcpu, CPUSTAT_IO_INT); |
| 171 | else |
| 172 | vcpu->arch.sie_block->lctl |= LCTL_CR6; |
| 173 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 174 | default: |
| 175 | BUG(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static void __do_deliver_interrupt(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 180 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 181 | { |
| 182 | const unsigned short table[] = { 2, 4, 4, 6 }; |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 183 | int rc = 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 184 | |
| 185 | switch (inti->type) { |
| 186 | case KVM_S390_INT_EMERGENCY: |
| 187 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg"); |
| 188 | vcpu->stat.deliver_emergency_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 189 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 190 | inti->emerg.code, 0); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 191 | rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1201); |
| 192 | rc |= put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->emerg.code); |
| 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 | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 203 | rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1202); |
| 204 | rc |= put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, inti->extcall.code); |
| 205 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 206 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 207 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 208 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 209 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 210 | case KVM_S390_INT_SERVICE: |
| 211 | VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x", |
| 212 | inti->ext.ext_params); |
| 213 | vcpu->stat.deliver_service_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 214 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 215 | inti->ext.ext_params, 0); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 216 | rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2401); |
| 217 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 218 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 219 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 220 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
| 221 | rc |= put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 222 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 223 | case KVM_S390_INT_VIRTIO: |
Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 224 | VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx", |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 225 | inti->ext.ext_params, inti->ext.ext_params2); |
| 226 | vcpu->stat.deliver_virtio_interrupt++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 227 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 228 | inti->ext.ext_params, |
| 229 | inti->ext.ext_params2); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 230 | rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2603); |
| 231 | rc |= put_guest_u16(vcpu, __LC_EXT_CPU_ADDR, 0x0d00); |
| 232 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 233 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 234 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 235 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
| 236 | rc |= put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params); |
| 237 | rc |= put_guest_u64(vcpu, __LC_EXT_PARAMS2, |
| 238 | inti->ext.ext_params2); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 239 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 240 | case KVM_S390_SIGP_STOP: |
| 241 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop"); |
| 242 | vcpu->stat.deliver_stop_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 243 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 244 | 0, 0); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 245 | __set_intercept_indicator(vcpu, inti); |
| 246 | break; |
| 247 | |
| 248 | case KVM_S390_SIGP_SET_PREFIX: |
| 249 | VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", |
| 250 | inti->prefix.address); |
| 251 | vcpu->stat.deliver_prefix_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 252 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 253 | inti->prefix.address, 0); |
Christian Borntraeger | 8d26cf7 | 2012-01-11 11:19:32 +0100 | [diff] [blame] | 254 | kvm_s390_set_prefix(vcpu, inti->prefix.address); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 255 | break; |
| 256 | |
| 257 | case KVM_S390_RESTART: |
| 258 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart"); |
| 259 | vcpu->stat.deliver_restart_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 260 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 261 | 0, 0); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 262 | rc = copy_to_guest(vcpu, |
| 263 | offsetof(struct _lowcore, restart_old_psw), |
| 264 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 265 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 266 | offsetof(struct _lowcore, restart_psw), |
| 267 | sizeof(psw_t)); |
Cornelia Huck | 9e6dabe | 2011-11-17 11:00:41 +0100 | [diff] [blame] | 268 | atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 269 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 270 | case KVM_S390_PROGRAM_INT: |
| 271 | VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x", |
| 272 | inti->pgm.code, |
| 273 | table[vcpu->arch.sie_block->ipa >> 14]); |
| 274 | vcpu->stat.deliver_program_int++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 275 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 276 | inti->pgm.code, 0); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 277 | rc = put_guest_u16(vcpu, __LC_PGM_INT_CODE, inti->pgm.code); |
| 278 | rc |= put_guest_u16(vcpu, __LC_PGM_ILC, |
| 279 | table[vcpu->arch.sie_block->ipa >> 14]); |
| 280 | rc |= copy_to_guest(vcpu, __LC_PGM_OLD_PSW, |
| 281 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 282 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 283 | __LC_PGM_NEW_PSW, sizeof(psw_t)); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 284 | break; |
| 285 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 286 | case KVM_S390_MCHK: |
| 287 | VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx", |
| 288 | inti->mchk.mcic); |
| 289 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 290 | inti->mchk.cr14, |
| 291 | inti->mchk.mcic); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 292 | rc = kvm_s390_vcpu_store_status(vcpu, |
| 293 | KVM_S390_STORE_STATUS_PREFIXED); |
| 294 | rc |= put_guest_u64(vcpu, __LC_MCCK_CODE, inti->mchk.mcic); |
| 295 | rc |= copy_to_guest(vcpu, __LC_MCK_OLD_PSW, |
| 296 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 297 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 298 | __LC_MCK_NEW_PSW, sizeof(psw_t)); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 299 | break; |
| 300 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 301 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 302 | { |
| 303 | __u32 param0 = ((__u32)inti->io.subchannel_id << 16) | |
| 304 | inti->io.subchannel_nr; |
| 305 | __u64 param1 = ((__u64)inti->io.io_int_parm << 32) | |
| 306 | inti->io.io_int_word; |
| 307 | VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type); |
| 308 | vcpu->stat.deliver_io_int++; |
| 309 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 310 | param0, param1); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 311 | rc = put_guest_u16(vcpu, __LC_SUBCHANNEL_ID, |
| 312 | inti->io.subchannel_id); |
| 313 | rc |= put_guest_u16(vcpu, __LC_SUBCHANNEL_NR, |
| 314 | inti->io.subchannel_nr); |
| 315 | rc |= put_guest_u32(vcpu, __LC_IO_INT_PARM, |
| 316 | inti->io.io_int_parm); |
| 317 | rc |= put_guest_u32(vcpu, __LC_IO_INT_WORD, |
| 318 | inti->io.io_int_word); |
| 319 | rc |= copy_to_guest(vcpu, __LC_IO_OLD_PSW, |
| 320 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 321 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 322 | __LC_IO_NEW_PSW, sizeof(psw_t)); |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 323 | break; |
| 324 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 325 | default: |
| 326 | BUG(); |
| 327 | } |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 328 | if (rc) { |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 329 | printk("kvm: The guest lowcore is not mapped during interrupt " |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 330 | "delivery, killing userspace\n"); |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 331 | do_exit(SIGKILL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu) |
| 336 | { |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 337 | int rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 338 | |
| 339 | if (psw_extint_disabled(vcpu)) |
| 340 | return 0; |
| 341 | if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul)) |
| 342 | return 0; |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame^] | 343 | rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1004); |
| 344 | rc |= copy_to_guest(vcpu, __LC_EXT_OLD_PSW, |
| 345 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 346 | rc |= copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw, |
| 347 | __LC_EXT_NEW_PSW, sizeof(psw_t)); |
| 348 | if (rc) { |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 349 | printk("kvm: The guest lowcore is not mapped during interrupt " |
| 350 | "delivery, killing userspace\n"); |
| 351 | do_exit(SIGKILL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 352 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 353 | return 1; |
| 354 | } |
| 355 | |
Gleb Natapov | a1b3710 | 2009-07-09 15:33:52 +0300 | [diff] [blame] | 356 | static int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 357 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 358 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 359 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 360 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 361 | int rc = 0; |
| 362 | |
| 363 | if (atomic_read(&li->active)) { |
| 364 | spin_lock_bh(&li->lock); |
| 365 | list_for_each_entry(inti, &li->list, list) |
| 366 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 367 | rc = 1; |
| 368 | break; |
| 369 | } |
| 370 | spin_unlock_bh(&li->lock); |
| 371 | } |
| 372 | |
| 373 | if ((!rc) && atomic_read(&fi->active)) { |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 374 | spin_lock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 375 | list_for_each_entry(inti, &fi->list, list) |
| 376 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 377 | rc = 1; |
| 378 | break; |
| 379 | } |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 380 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | if ((!rc) && (vcpu->arch.sie_block->ckc < |
Heiko Carstens | 1aae056 | 2013-01-30 09:49:40 +0100 | [diff] [blame] | 384 | get_tod_clock() + vcpu->arch.sie_block->epoch)) { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 385 | if ((!psw_extint_disabled(vcpu)) && |
| 386 | (vcpu->arch.sie_block->gcr[0] & 0x800ul)) |
| 387 | rc = 1; |
| 388 | } |
| 389 | |
| 390 | return rc; |
| 391 | } |
| 392 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 393 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 394 | { |
| 395 | return 0; |
| 396 | } |
| 397 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 398 | int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) |
| 399 | { |
| 400 | u64 now, sltime; |
| 401 | DECLARE_WAITQUEUE(wait, current); |
| 402 | |
| 403 | vcpu->stat.exit_wait_state++; |
| 404 | if (kvm_cpu_has_interrupt(vcpu)) |
| 405 | return 0; |
| 406 | |
Carsten Otte | e52b2af | 2008-05-21 13:37:44 +0200 | [diff] [blame] | 407 | __set_cpu_idle(vcpu); |
| 408 | spin_lock_bh(&vcpu->arch.local_int.lock); |
| 409 | vcpu->arch.local_int.timer_due = 0; |
| 410 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
| 411 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 412 | if (psw_interrupts_disabled(vcpu)) { |
| 413 | VCPU_EVENT(vcpu, 3, "%s", "disabled wait"); |
| 414 | __unset_cpu_idle(vcpu); |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 415 | return -EOPNOTSUPP; /* disabled wait */ |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (psw_extint_disabled(vcpu) || |
| 419 | (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) { |
| 420 | VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer"); |
| 421 | goto no_timer; |
| 422 | } |
| 423 | |
Heiko Carstens | 1aae056 | 2013-01-30 09:49:40 +0100 | [diff] [blame] | 424 | now = get_tod_clock() + vcpu->arch.sie_block->epoch; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 425 | if (vcpu->arch.sie_block->ckc < now) { |
| 426 | __unset_cpu_idle(vcpu); |
| 427 | return 0; |
| 428 | } |
| 429 | |
Heiko Carstens | ed4f209 | 2013-01-14 16:55:55 +0100 | [diff] [blame] | 430 | sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 431 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 432 | hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL); |
| 433 | VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 434 | no_timer: |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 435 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 436 | spin_lock_bh(&vcpu->arch.local_int.lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 437 | add_wait_queue(&vcpu->arch.local_int.wq, &wait); |
| 438 | while (list_empty(&vcpu->arch.local_int.list) && |
| 439 | list_empty(&vcpu->arch.local_int.float_int->list) && |
| 440 | (!vcpu->arch.local_int.timer_due) && |
| 441 | !signal_pending(current)) { |
| 442 | set_current_state(TASK_INTERRUPTIBLE); |
| 443 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 444 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 445 | schedule(); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 446 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 447 | spin_lock_bh(&vcpu->arch.local_int.lock); |
| 448 | } |
| 449 | __unset_cpu_idle(vcpu); |
| 450 | __set_current_state(TASK_RUNNING); |
Christian Borntraeger | d3bc2f9 | 2009-07-16 17:17:37 +0200 | [diff] [blame] | 451 | remove_wait_queue(&vcpu->arch.local_int.wq, &wait); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 452 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 453 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 454 | hrtimer_try_to_cancel(&vcpu->arch.ckc_timer); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 458 | void kvm_s390_tasklet(unsigned long parm) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 459 | { |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 460 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 461 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 462 | spin_lock(&vcpu->arch.local_int.lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 463 | vcpu->arch.local_int.timer_due = 1; |
| 464 | if (waitqueue_active(&vcpu->arch.local_int.wq)) |
| 465 | wake_up_interruptible(&vcpu->arch.local_int.wq); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 466 | spin_unlock(&vcpu->arch.local_int.lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 467 | } |
| 468 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 469 | /* |
| 470 | * low level hrtimer wake routine. Because this runs in hardirq context |
| 471 | * we schedule a tasklet to do the real work. |
| 472 | */ |
| 473 | enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer) |
| 474 | { |
| 475 | struct kvm_vcpu *vcpu; |
| 476 | |
| 477 | vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer); |
| 478 | tasklet_schedule(&vcpu->arch.tasklet); |
| 479 | |
| 480 | return HRTIMER_NORESTART; |
| 481 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 482 | |
| 483 | void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) |
| 484 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 485 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 486 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 487 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 488 | int deliver; |
| 489 | |
| 490 | __reset_intercept_indicators(vcpu); |
| 491 | if (atomic_read(&li->active)) { |
| 492 | do { |
| 493 | deliver = 0; |
| 494 | spin_lock_bh(&li->lock); |
| 495 | list_for_each_entry_safe(inti, n, &li->list, list) { |
| 496 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 497 | list_del(&inti->list); |
| 498 | deliver = 1; |
| 499 | break; |
| 500 | } |
| 501 | __set_intercept_indicator(vcpu, inti); |
| 502 | } |
| 503 | if (list_empty(&li->list)) |
| 504 | atomic_set(&li->active, 0); |
| 505 | spin_unlock_bh(&li->lock); |
| 506 | if (deliver) { |
| 507 | __do_deliver_interrupt(vcpu, inti); |
| 508 | kfree(inti); |
| 509 | } |
| 510 | } while (deliver); |
| 511 | } |
| 512 | |
| 513 | if ((vcpu->arch.sie_block->ckc < |
Heiko Carstens | 1aae056 | 2013-01-30 09:49:40 +0100 | [diff] [blame] | 514 | get_tod_clock() + vcpu->arch.sie_block->epoch)) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 515 | __try_deliver_ckc_interrupt(vcpu); |
| 516 | |
| 517 | if (atomic_read(&fi->active)) { |
| 518 | do { |
| 519 | deliver = 0; |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 520 | spin_lock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 521 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 522 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 523 | list_del(&inti->list); |
| 524 | deliver = 1; |
| 525 | break; |
| 526 | } |
| 527 | __set_intercept_indicator(vcpu, inti); |
| 528 | } |
| 529 | if (list_empty(&fi->list)) |
| 530 | atomic_set(&fi->active, 0); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 531 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 532 | if (deliver) { |
| 533 | __do_deliver_interrupt(vcpu, inti); |
| 534 | kfree(inti); |
| 535 | } |
| 536 | } while (deliver); |
| 537 | } |
| 538 | } |
| 539 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 540 | void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu) |
| 541 | { |
| 542 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 543 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 544 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
| 545 | int deliver; |
| 546 | |
| 547 | __reset_intercept_indicators(vcpu); |
| 548 | if (atomic_read(&li->active)) { |
| 549 | do { |
| 550 | deliver = 0; |
| 551 | spin_lock_bh(&li->lock); |
| 552 | list_for_each_entry_safe(inti, n, &li->list, list) { |
| 553 | if ((inti->type == KVM_S390_MCHK) && |
| 554 | __interrupt_is_deliverable(vcpu, inti)) { |
| 555 | list_del(&inti->list); |
| 556 | deliver = 1; |
| 557 | break; |
| 558 | } |
| 559 | __set_intercept_indicator(vcpu, inti); |
| 560 | } |
| 561 | if (list_empty(&li->list)) |
| 562 | atomic_set(&li->active, 0); |
| 563 | spin_unlock_bh(&li->lock); |
| 564 | if (deliver) { |
| 565 | __do_deliver_interrupt(vcpu, inti); |
| 566 | kfree(inti); |
| 567 | } |
| 568 | } while (deliver); |
| 569 | } |
| 570 | |
| 571 | if (atomic_read(&fi->active)) { |
| 572 | do { |
| 573 | deliver = 0; |
| 574 | spin_lock(&fi->lock); |
| 575 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 576 | if ((inti->type == KVM_S390_MCHK) && |
| 577 | __interrupt_is_deliverable(vcpu, inti)) { |
| 578 | list_del(&inti->list); |
| 579 | deliver = 1; |
| 580 | break; |
| 581 | } |
| 582 | __set_intercept_indicator(vcpu, inti); |
| 583 | } |
| 584 | if (list_empty(&fi->list)) |
| 585 | atomic_set(&fi->active, 0); |
| 586 | spin_unlock(&fi->lock); |
| 587 | if (deliver) { |
| 588 | __do_deliver_interrupt(vcpu, inti); |
| 589 | kfree(inti); |
| 590 | } |
| 591 | } while (deliver); |
| 592 | } |
| 593 | } |
| 594 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 595 | int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code) |
| 596 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 597 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 598 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 599 | |
| 600 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 601 | if (!inti) |
| 602 | return -ENOMEM; |
| 603 | |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 604 | inti->type = KVM_S390_PROGRAM_INT; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 605 | inti->pgm.code = code; |
| 606 | |
| 607 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code); |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 608 | 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] | 609 | spin_lock_bh(&li->lock); |
| 610 | list_add(&inti->list, &li->list); |
| 611 | atomic_set(&li->active, 1); |
| 612 | BUG_ON(waitqueue_active(&li->wq)); |
| 613 | spin_unlock_bh(&li->lock); |
| 614 | return 0; |
| 615 | } |
| 616 | |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 617 | struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm, |
| 618 | u64 cr6, u64 schid) |
| 619 | { |
| 620 | struct kvm_s390_float_interrupt *fi; |
| 621 | struct kvm_s390_interrupt_info *inti, *iter; |
| 622 | |
| 623 | if ((!schid && !cr6) || (schid && cr6)) |
| 624 | return NULL; |
| 625 | mutex_lock(&kvm->lock); |
| 626 | fi = &kvm->arch.float_int; |
| 627 | spin_lock(&fi->lock); |
| 628 | inti = NULL; |
| 629 | list_for_each_entry(iter, &fi->list, list) { |
| 630 | if (!is_ioint(iter->type)) |
| 631 | continue; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 632 | if (cr6 && |
| 633 | ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0)) |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 634 | continue; |
| 635 | if (schid) { |
| 636 | if (((schid & 0x00000000ffff0000) >> 16) != |
| 637 | iter->io.subchannel_id) |
| 638 | continue; |
| 639 | if ((schid & 0x000000000000ffff) != |
| 640 | iter->io.subchannel_nr) |
| 641 | continue; |
| 642 | } |
| 643 | inti = iter; |
| 644 | break; |
| 645 | } |
| 646 | if (inti) |
| 647 | list_del_init(&inti->list); |
| 648 | if (list_empty(&fi->list)) |
| 649 | atomic_set(&fi->active, 0); |
| 650 | spin_unlock(&fi->lock); |
| 651 | mutex_unlock(&kvm->lock); |
| 652 | return inti; |
| 653 | } |
| 654 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 655 | int kvm_s390_inject_vm(struct kvm *kvm, |
| 656 | struct kvm_s390_interrupt *s390int) |
| 657 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 658 | struct kvm_s390_local_interrupt *li; |
| 659 | struct kvm_s390_float_interrupt *fi; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 660 | struct kvm_s390_interrupt_info *inti, *iter; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 661 | int sigcpu; |
| 662 | |
| 663 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 664 | if (!inti) |
| 665 | return -ENOMEM; |
| 666 | |
| 667 | switch (s390int->type) { |
| 668 | case KVM_S390_INT_VIRTIO: |
Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 669 | VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx", |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 670 | s390int->parm, s390int->parm64); |
| 671 | inti->type = s390int->type; |
| 672 | inti->ext.ext_params = s390int->parm; |
| 673 | inti->ext.ext_params2 = s390int->parm64; |
| 674 | break; |
| 675 | case KVM_S390_INT_SERVICE: |
| 676 | VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm); |
| 677 | inti->type = s390int->type; |
| 678 | inti->ext.ext_params = s390int->parm; |
| 679 | break; |
| 680 | case KVM_S390_PROGRAM_INT: |
| 681 | case KVM_S390_SIGP_STOP: |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 682 | case KVM_S390_INT_EXTERNAL_CALL: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 683 | case KVM_S390_INT_EMERGENCY: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 684 | kfree(inti); |
| 685 | return -EINVAL; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 686 | case KVM_S390_MCHK: |
| 687 | VM_EVENT(kvm, 5, "inject: machine check parm64:%llx", |
| 688 | s390int->parm64); |
| 689 | inti->type = s390int->type; |
| 690 | inti->mchk.cr14 = s390int->parm; /* upper bits are not used */ |
| 691 | inti->mchk.mcic = s390int->parm64; |
| 692 | break; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 693 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 694 | if (s390int->type & IOINT_AI_MASK) |
| 695 | VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)"); |
| 696 | else |
| 697 | VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x", |
| 698 | s390int->type & IOINT_CSSID_MASK, |
| 699 | s390int->type & IOINT_SSID_MASK, |
| 700 | s390int->type & IOINT_SCHID_MASK); |
| 701 | inti->type = s390int->type; |
| 702 | inti->io.subchannel_id = s390int->parm >> 16; |
| 703 | inti->io.subchannel_nr = s390int->parm & 0x0000ffffu; |
| 704 | inti->io.io_int_parm = s390int->parm64 >> 32; |
| 705 | inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull; |
| 706 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 707 | default: |
| 708 | kfree(inti); |
| 709 | return -EINVAL; |
| 710 | } |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 711 | trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64, |
| 712 | 2); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 713 | |
| 714 | mutex_lock(&kvm->lock); |
| 715 | fi = &kvm->arch.float_int; |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 716 | spin_lock(&fi->lock); |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 717 | if (!is_ioint(inti->type)) |
| 718 | list_add_tail(&inti->list, &fi->list); |
| 719 | else { |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 720 | u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word); |
| 721 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 722 | /* Keep I/O interrupts sorted in isc order. */ |
| 723 | list_for_each_entry(iter, &fi->list, list) { |
| 724 | if (!is_ioint(iter->type)) |
| 725 | continue; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 726 | if (int_word_to_isc_bits(iter->io.io_int_word) |
| 727 | <= isc_bits) |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 728 | continue; |
| 729 | break; |
| 730 | } |
| 731 | list_add_tail(&inti->list, &iter->list); |
| 732 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 733 | atomic_set(&fi->active, 1); |
| 734 | sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS); |
| 735 | if (sigcpu == KVM_MAX_VCPUS) { |
| 736 | do { |
| 737 | sigcpu = fi->next_rr_cpu++; |
| 738 | if (sigcpu == KVM_MAX_VCPUS) |
| 739 | sigcpu = fi->next_rr_cpu = 0; |
| 740 | } while (fi->local_int[sigcpu] == NULL); |
| 741 | } |
| 742 | li = fi->local_int[sigcpu]; |
| 743 | spin_lock_bh(&li->lock); |
| 744 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
| 745 | if (waitqueue_active(&li->wq)) |
| 746 | wake_up_interruptible(&li->wq); |
| 747 | spin_unlock_bh(&li->lock); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 748 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 749 | mutex_unlock(&kvm->lock); |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, |
| 754 | struct kvm_s390_interrupt *s390int) |
| 755 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 756 | struct kvm_s390_local_interrupt *li; |
| 757 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 758 | |
| 759 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 760 | if (!inti) |
| 761 | return -ENOMEM; |
| 762 | |
| 763 | switch (s390int->type) { |
| 764 | case KVM_S390_PROGRAM_INT: |
| 765 | if (s390int->parm & 0xffff0000) { |
| 766 | kfree(inti); |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | inti->type = s390int->type; |
| 770 | inti->pgm.code = s390int->parm; |
| 771 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)", |
| 772 | s390int->parm); |
| 773 | break; |
Christian Borntraeger | b7e6e4d | 2009-01-22 10:29:08 +0100 | [diff] [blame] | 774 | case KVM_S390_SIGP_SET_PREFIX: |
| 775 | inti->prefix.address = s390int->parm; |
| 776 | inti->type = s390int->type; |
| 777 | VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)", |
| 778 | s390int->parm); |
| 779 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 780 | case KVM_S390_SIGP_STOP: |
| 781 | case KVM_S390_RESTART: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 782 | VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type); |
| 783 | inti->type = s390int->type; |
| 784 | break; |
Jason J. Herne | 82a1273 | 2012-10-02 16:25:36 +0200 | [diff] [blame] | 785 | case KVM_S390_INT_EXTERNAL_CALL: |
| 786 | if (s390int->parm & 0xffff0000) { |
| 787 | kfree(inti); |
| 788 | return -EINVAL; |
| 789 | } |
| 790 | VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u", |
| 791 | s390int->parm); |
| 792 | inti->type = s390int->type; |
| 793 | inti->extcall.code = s390int->parm; |
| 794 | break; |
| 795 | case KVM_S390_INT_EMERGENCY: |
| 796 | if (s390int->parm & 0xffff0000) { |
| 797 | kfree(inti); |
| 798 | return -EINVAL; |
| 799 | } |
| 800 | VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm); |
| 801 | inti->type = s390int->type; |
| 802 | inti->emerg.code = s390int->parm; |
| 803 | break; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 804 | case KVM_S390_MCHK: |
| 805 | VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx", |
| 806 | s390int->parm64); |
| 807 | inti->type = s390int->type; |
| 808 | inti->mchk.mcic = s390int->parm64; |
| 809 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 810 | case KVM_S390_INT_VIRTIO: |
| 811 | case KVM_S390_INT_SERVICE: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 812 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 813 | default: |
| 814 | kfree(inti); |
| 815 | return -EINVAL; |
| 816 | } |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 817 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm, |
| 818 | s390int->parm64, 2); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 819 | |
| 820 | mutex_lock(&vcpu->kvm->lock); |
| 821 | li = &vcpu->arch.local_int; |
| 822 | spin_lock_bh(&li->lock); |
| 823 | if (inti->type == KVM_S390_PROGRAM_INT) |
| 824 | list_add(&inti->list, &li->list); |
| 825 | else |
| 826 | list_add_tail(&inti->list, &li->list); |
| 827 | atomic_set(&li->active, 1); |
| 828 | if (inti->type == KVM_S390_SIGP_STOP) |
| 829 | li->action_bits |= ACTION_STOP_ON_STOP; |
| 830 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
| 831 | if (waitqueue_active(&li->wq)) |
| 832 | wake_up_interruptible(&vcpu->arch.local_int.wq); |
| 833 | spin_unlock_bh(&li->lock); |
| 834 | mutex_unlock(&vcpu->kvm->lock); |
| 835 | return 0; |
| 836 | } |