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