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