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