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 | * |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2008,2014 |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 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> |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 16 | #include <linux/mmu_context.h> |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 17 | #include <linux/signal.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 19 | #include <asm/asm-offsets.h> |
| 20 | #include <asm/uaccess.h> |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 21 | #include "kvm-s390.h" |
| 22 | #include "gaccess.h" |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 23 | #include "trace-s390.h" |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 24 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 25 | #define IOINT_SCHID_MASK 0x0000ffff |
| 26 | #define IOINT_SSID_MASK 0x00030000 |
| 27 | #define IOINT_CSSID_MASK 0x03fc0000 |
| 28 | #define IOINT_AI_MASK 0x04000000 |
| 29 | |
| 30 | static int is_ioint(u64 type) |
| 31 | { |
| 32 | return ((type & 0xfffe0000u) != 0xfffe0000u); |
| 33 | } |
| 34 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 35 | int psw_extint_disabled(struct kvm_vcpu *vcpu) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 36 | { |
| 37 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT); |
| 38 | } |
| 39 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 40 | static int psw_ioint_disabled(struct kvm_vcpu *vcpu) |
| 41 | { |
| 42 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO); |
| 43 | } |
| 44 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 45 | static int psw_mchk_disabled(struct kvm_vcpu *vcpu) |
| 46 | { |
| 47 | return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK); |
| 48 | } |
| 49 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 50 | static int psw_interrupts_disabled(struct kvm_vcpu *vcpu) |
| 51 | { |
| 52 | if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) || |
| 53 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) || |
| 54 | (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT)) |
| 55 | return 0; |
| 56 | return 1; |
| 57 | } |
| 58 | |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 59 | static u64 int_word_to_isc_bits(u32 int_word) |
| 60 | { |
| 61 | u8 isc = (int_word & 0x38000000) >> 27; |
| 62 | |
| 63 | return (0x80 >> isc) << 24; |
| 64 | } |
| 65 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 66 | static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 67 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 68 | { |
| 69 | switch (inti->type) { |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 70 | case KVM_S390_INT_EXTERNAL_CALL: |
| 71 | if (psw_extint_disabled(vcpu)) |
| 72 | return 0; |
| 73 | if (vcpu->arch.sie_block->gcr[0] & 0x2000ul) |
| 74 | return 1; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 75 | case KVM_S390_INT_EMERGENCY: |
| 76 | if (psw_extint_disabled(vcpu)) |
| 77 | return 0; |
| 78 | if (vcpu->arch.sie_block->gcr[0] & 0x4000ul) |
| 79 | return 1; |
| 80 | return 0; |
| 81 | case KVM_S390_INT_SERVICE: |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 82 | case KVM_S390_INT_PFAULT_INIT: |
| 83 | case KVM_S390_INT_PFAULT_DONE: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 84 | case KVM_S390_INT_VIRTIO: |
| 85 | if (psw_extint_disabled(vcpu)) |
| 86 | return 0; |
| 87 | if (vcpu->arch.sie_block->gcr[0] & 0x200ul) |
| 88 | return 1; |
| 89 | return 0; |
| 90 | case KVM_S390_PROGRAM_INT: |
| 91 | case KVM_S390_SIGP_STOP: |
| 92 | case KVM_S390_SIGP_SET_PREFIX: |
| 93 | case KVM_S390_RESTART: |
| 94 | return 1; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 95 | case KVM_S390_MCHK: |
| 96 | if (psw_mchk_disabled(vcpu)) |
| 97 | return 0; |
| 98 | if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14) |
| 99 | return 1; |
| 100 | return 0; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 101 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 102 | if (psw_ioint_disabled(vcpu)) |
| 103 | return 0; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 104 | if (vcpu->arch.sie_block->gcr[6] & |
| 105 | int_word_to_isc_bits(inti->io.io_int_word)) |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 106 | return 1; |
| 107 | return 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 108 | default: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 109 | printk(KERN_WARNING "illegal interrupt type %llx\n", |
| 110 | inti->type); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 111 | BUG(); |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static void __set_cpu_idle(struct kvm_vcpu *vcpu) |
| 117 | { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 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 | { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 124 | atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
| 125 | clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); |
| 126 | } |
| 127 | |
| 128 | static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) |
| 129 | { |
| 130 | atomic_clear_mask(CPUSTAT_ECALL_PEND | |
| 131 | CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT, |
| 132 | &vcpu->arch.sie_block->cpuflags); |
| 133 | vcpu->arch.sie_block->lctl = 0x0000; |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame^] | 134 | vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT); |
| 135 | |
| 136 | if (guestdbg_enabled(vcpu)) { |
| 137 | vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 | |
| 138 | LCTL_CR10 | LCTL_CR11); |
| 139 | vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT); |
| 140 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag) |
| 144 | { |
| 145 | atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags); |
| 146 | } |
| 147 | |
| 148 | static void __set_intercept_indicator(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 149 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 150 | { |
| 151 | switch (inti->type) { |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 152 | case KVM_S390_INT_EXTERNAL_CALL: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 153 | case KVM_S390_INT_EMERGENCY: |
| 154 | case KVM_S390_INT_SERVICE: |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 155 | case KVM_S390_INT_PFAULT_INIT: |
| 156 | case KVM_S390_INT_PFAULT_DONE: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 157 | case KVM_S390_INT_VIRTIO: |
| 158 | if (psw_extint_disabled(vcpu)) |
| 159 | __set_cpuflag(vcpu, CPUSTAT_EXT_INT); |
| 160 | else |
| 161 | vcpu->arch.sie_block->lctl |= LCTL_CR0; |
| 162 | break; |
| 163 | case KVM_S390_SIGP_STOP: |
| 164 | __set_cpuflag(vcpu, CPUSTAT_STOP_INT); |
| 165 | break; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 166 | case KVM_S390_MCHK: |
| 167 | if (psw_mchk_disabled(vcpu)) |
| 168 | vcpu->arch.sie_block->ictl |= ICTL_LPSW; |
| 169 | else |
| 170 | vcpu->arch.sie_block->lctl |= LCTL_CR14; |
| 171 | break; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 172 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 173 | if (psw_ioint_disabled(vcpu)) |
| 174 | __set_cpuflag(vcpu, CPUSTAT_IO_INT); |
| 175 | else |
| 176 | vcpu->arch.sie_block->lctl |= LCTL_CR6; |
| 177 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 178 | default: |
| 179 | BUG(); |
| 180 | } |
| 181 | } |
| 182 | |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 183 | static int __deliver_prog_irq(struct kvm_vcpu *vcpu, |
| 184 | struct kvm_s390_pgm_info *pgm_info) |
| 185 | { |
| 186 | const unsigned short table[] = { 2, 4, 4, 6 }; |
| 187 | int rc = 0; |
| 188 | |
| 189 | switch (pgm_info->code & ~PGM_PER) { |
| 190 | case PGM_AFX_TRANSLATION: |
| 191 | case PGM_ASX_TRANSLATION: |
| 192 | case PGM_EX_TRANSLATION: |
| 193 | case PGM_LFX_TRANSLATION: |
| 194 | case PGM_LSTE_SEQUENCE: |
| 195 | case PGM_LSX_TRANSLATION: |
| 196 | case PGM_LX_TRANSLATION: |
| 197 | case PGM_PRIMARY_AUTHORITY: |
| 198 | case PGM_SECONDARY_AUTHORITY: |
| 199 | case PGM_SPACE_SWITCH: |
| 200 | rc = put_guest_lc(vcpu, pgm_info->trans_exc_code, |
| 201 | (u64 *)__LC_TRANS_EXC_CODE); |
| 202 | break; |
| 203 | case PGM_ALEN_TRANSLATION: |
| 204 | case PGM_ALE_SEQUENCE: |
| 205 | case PGM_ASTE_INSTANCE: |
| 206 | case PGM_ASTE_SEQUENCE: |
| 207 | case PGM_ASTE_VALIDITY: |
| 208 | case PGM_EXTENDED_AUTHORITY: |
| 209 | rc = put_guest_lc(vcpu, pgm_info->exc_access_id, |
| 210 | (u8 *)__LC_EXC_ACCESS_ID); |
| 211 | break; |
| 212 | case PGM_ASCE_TYPE: |
| 213 | case PGM_PAGE_TRANSLATION: |
| 214 | case PGM_REGION_FIRST_TRANS: |
| 215 | case PGM_REGION_SECOND_TRANS: |
| 216 | case PGM_REGION_THIRD_TRANS: |
| 217 | case PGM_SEGMENT_TRANSLATION: |
| 218 | rc = put_guest_lc(vcpu, pgm_info->trans_exc_code, |
| 219 | (u64 *)__LC_TRANS_EXC_CODE); |
| 220 | rc |= put_guest_lc(vcpu, pgm_info->exc_access_id, |
| 221 | (u8 *)__LC_EXC_ACCESS_ID); |
| 222 | rc |= put_guest_lc(vcpu, pgm_info->op_access_id, |
| 223 | (u8 *)__LC_OP_ACCESS_ID); |
| 224 | break; |
| 225 | case PGM_MONITOR: |
| 226 | rc = put_guest_lc(vcpu, pgm_info->mon_class_nr, |
| 227 | (u64 *)__LC_MON_CLASS_NR); |
| 228 | rc |= put_guest_lc(vcpu, pgm_info->mon_code, |
| 229 | (u64 *)__LC_MON_CODE); |
| 230 | break; |
| 231 | case PGM_DATA: |
| 232 | rc = put_guest_lc(vcpu, pgm_info->data_exc_code, |
| 233 | (u32 *)__LC_DATA_EXC_CODE); |
| 234 | break; |
| 235 | case PGM_PROTECTION: |
| 236 | rc = put_guest_lc(vcpu, pgm_info->trans_exc_code, |
| 237 | (u64 *)__LC_TRANS_EXC_CODE); |
| 238 | rc |= put_guest_lc(vcpu, pgm_info->exc_access_id, |
| 239 | (u8 *)__LC_EXC_ACCESS_ID); |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | if (pgm_info->code & PGM_PER) { |
| 244 | rc |= put_guest_lc(vcpu, pgm_info->per_code, |
| 245 | (u8 *) __LC_PER_CODE); |
| 246 | rc |= put_guest_lc(vcpu, pgm_info->per_atmid, |
| 247 | (u8 *)__LC_PER_ATMID); |
| 248 | rc |= put_guest_lc(vcpu, pgm_info->per_address, |
| 249 | (u64 *) __LC_PER_ADDRESS); |
| 250 | rc |= put_guest_lc(vcpu, pgm_info->per_access_id, |
| 251 | (u8 *) __LC_PER_ACCESS_ID); |
| 252 | } |
| 253 | |
| 254 | switch (vcpu->arch.sie_block->icptcode) { |
| 255 | case ICPT_INST: |
| 256 | case ICPT_INSTPROGI: |
| 257 | case ICPT_OPEREXC: |
| 258 | case ICPT_PARTEXEC: |
| 259 | case ICPT_IOINST: |
| 260 | /* last instruction only stored for these icptcodes */ |
| 261 | rc |= put_guest_lc(vcpu, table[vcpu->arch.sie_block->ipa >> 14], |
| 262 | (u16 *) __LC_PGM_ILC); |
| 263 | break; |
| 264 | case ICPT_PROGI: |
| 265 | rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->pgmilc, |
| 266 | (u16 *) __LC_PGM_ILC); |
| 267 | break; |
| 268 | default: |
| 269 | rc |= put_guest_lc(vcpu, 0, |
| 270 | (u16 *) __LC_PGM_ILC); |
| 271 | } |
| 272 | |
| 273 | rc |= put_guest_lc(vcpu, pgm_info->code, |
| 274 | (u16 *)__LC_PGM_INT_CODE); |
| 275 | rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW, |
| 276 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 277 | rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW, |
| 278 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 279 | |
| 280 | return rc; |
| 281 | } |
| 282 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 283 | static void __do_deliver_interrupt(struct kvm_vcpu *vcpu, |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 284 | struct kvm_s390_interrupt_info *inti) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 285 | { |
| 286 | const unsigned short table[] = { 2, 4, 4, 6 }; |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 287 | int rc = 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 288 | |
| 289 | switch (inti->type) { |
| 290 | case KVM_S390_INT_EMERGENCY: |
| 291 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg"); |
| 292 | vcpu->stat.deliver_emergency_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 293 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 294 | inti->emerg.code, 0); |
Jens Freimann | 1a03b764 | 2014-02-12 14:05:38 +0100 | [diff] [blame] | 295 | rc = put_guest_lc(vcpu, 0x1201, (u16 *)__LC_EXT_INT_CODE); |
| 296 | rc |= put_guest_lc(vcpu, inti->emerg.code, |
| 297 | (u16 *)__LC_EXT_CPU_ADDR); |
| 298 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 299 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 300 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 301 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 302 | break; |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 303 | case KVM_S390_INT_EXTERNAL_CALL: |
| 304 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call"); |
| 305 | vcpu->stat.deliver_external_call++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 306 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 307 | inti->extcall.code, 0); |
Jens Freimann | 1a03b764 | 2014-02-12 14:05:38 +0100 | [diff] [blame] | 308 | rc = put_guest_lc(vcpu, 0x1202, (u16 *)__LC_EXT_INT_CODE); |
| 309 | rc |= put_guest_lc(vcpu, inti->extcall.code, |
| 310 | (u16 *)__LC_EXT_CPU_ADDR); |
| 311 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 312 | &vcpu->arch.sie_block->gpsw, |
| 313 | sizeof(psw_t)); |
| 314 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 315 | &vcpu->arch.sie_block->gpsw, |
| 316 | sizeof(psw_t)); |
Christian Ehrhardt | 7697e71f | 2011-10-18 12:27:15 +0200 | [diff] [blame] | 317 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 318 | case KVM_S390_INT_SERVICE: |
| 319 | VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x", |
| 320 | inti->ext.ext_params); |
| 321 | vcpu->stat.deliver_service_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 322 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 323 | inti->ext.ext_params, 0); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 324 | rc = put_guest_lc(vcpu, 0x2401, (u16 *)__LC_EXT_INT_CODE); |
| 325 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 326 | &vcpu->arch.sie_block->gpsw, |
| 327 | sizeof(psw_t)); |
| 328 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 329 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 330 | rc |= put_guest_lc(vcpu, inti->ext.ext_params, |
| 331 | (u32 *)__LC_EXT_PARAMS); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 332 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 333 | case KVM_S390_INT_PFAULT_INIT: |
| 334 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0, |
| 335 | inti->ext.ext_params2); |
Jens Freimann | 1a03b764 | 2014-02-12 14:05:38 +0100 | [diff] [blame] | 336 | rc = put_guest_lc(vcpu, 0x2603, (u16 *) __LC_EXT_INT_CODE); |
| 337 | rc |= put_guest_lc(vcpu, 0x0600, (u16 *) __LC_EXT_CPU_ADDR); |
| 338 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 339 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 340 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 341 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 1a03b764 | 2014-02-12 14:05:38 +0100 | [diff] [blame] | 342 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, |
| 343 | (u64 *) __LC_EXT_PARAMS2); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 344 | break; |
| 345 | case KVM_S390_INT_PFAULT_DONE: |
| 346 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, 0, |
| 347 | inti->ext.ext_params2); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 348 | rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE); |
| 349 | rc |= put_guest_lc(vcpu, 0x0680, (u16 *)__LC_EXT_CPU_ADDR); |
| 350 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 351 | &vcpu->arch.sie_block->gpsw, |
| 352 | sizeof(psw_t)); |
| 353 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 354 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 355 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, |
| 356 | (u64 *)__LC_EXT_PARAMS2); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 357 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 358 | case KVM_S390_INT_VIRTIO: |
Heiko Carstens | 33e1911 | 2009-01-09 12:14:56 +0100 | [diff] [blame] | 359 | VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx", |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 360 | inti->ext.ext_params, inti->ext.ext_params2); |
| 361 | vcpu->stat.deliver_virtio_interrupt++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 362 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 363 | inti->ext.ext_params, |
| 364 | inti->ext.ext_params2); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 365 | rc = put_guest_lc(vcpu, 0x2603, (u16 *)__LC_EXT_INT_CODE); |
| 366 | rc |= put_guest_lc(vcpu, 0x0d00, (u16 *)__LC_EXT_CPU_ADDR); |
| 367 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 368 | &vcpu->arch.sie_block->gpsw, |
| 369 | sizeof(psw_t)); |
| 370 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 371 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 372 | rc |= put_guest_lc(vcpu, inti->ext.ext_params, |
| 373 | (u32 *)__LC_EXT_PARAMS); |
| 374 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, |
| 375 | (u64 *)__LC_EXT_PARAMS2); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 376 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 377 | case KVM_S390_SIGP_STOP: |
| 378 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop"); |
| 379 | vcpu->stat.deliver_stop_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 380 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 381 | 0, 0); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 382 | __set_intercept_indicator(vcpu, inti); |
| 383 | break; |
| 384 | |
| 385 | case KVM_S390_SIGP_SET_PREFIX: |
| 386 | VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", |
| 387 | inti->prefix.address); |
| 388 | vcpu->stat.deliver_prefix_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 389 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 390 | inti->prefix.address, 0); |
Christian Borntraeger | 8d26cf7 | 2012-01-11 11:19:32 +0100 | [diff] [blame] | 391 | kvm_s390_set_prefix(vcpu, inti->prefix.address); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case KVM_S390_RESTART: |
| 395 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart"); |
| 396 | vcpu->stat.deliver_restart_signal++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 397 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 398 | 0, 0); |
Jens Freimann | 1a03b764 | 2014-02-12 14:05:38 +0100 | [diff] [blame] | 399 | rc = write_guest_lc(vcpu, |
| 400 | offsetof(struct _lowcore, restart_old_psw), |
| 401 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 402 | rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw), |
| 403 | &vcpu->arch.sie_block->gpsw, |
| 404 | sizeof(psw_t)); |
Cornelia Huck | 9e6dabe | 2011-11-17 11:00:41 +0100 | [diff] [blame] | 405 | atomic_clear_mask(CPUSTAT_STOPPED, &vcpu->arch.sie_block->cpuflags); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 406 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 407 | case KVM_S390_PROGRAM_INT: |
| 408 | VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x", |
| 409 | inti->pgm.code, |
| 410 | table[vcpu->arch.sie_block->ipa >> 14]); |
| 411 | vcpu->stat.deliver_program_int++; |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 412 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 413 | inti->pgm.code, 0); |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 414 | rc = __deliver_prog_irq(vcpu, &inti->pgm); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 415 | break; |
| 416 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 417 | case KVM_S390_MCHK: |
| 418 | VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx", |
| 419 | inti->mchk.mcic); |
| 420 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 421 | inti->mchk.cr14, |
| 422 | inti->mchk.mcic); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 423 | rc = kvm_s390_vcpu_store_status(vcpu, |
| 424 | KVM_S390_STORE_STATUS_PREFIXED); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 425 | rc |= put_guest_lc(vcpu, inti->mchk.mcic, (u64 *)__LC_MCCK_CODE); |
| 426 | rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW, |
| 427 | &vcpu->arch.sie_block->gpsw, |
| 428 | sizeof(psw_t)); |
| 429 | rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW, |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 430 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 431 | break; |
| 432 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 433 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 434 | { |
| 435 | __u32 param0 = ((__u32)inti->io.subchannel_id << 16) | |
| 436 | inti->io.subchannel_nr; |
| 437 | __u64 param1 = ((__u64)inti->io.io_int_parm << 32) | |
| 438 | inti->io.io_int_word; |
| 439 | VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type); |
| 440 | vcpu->stat.deliver_io_int++; |
| 441 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type, |
| 442 | param0, param1); |
Heiko Carstens | 7988276 | 2014-01-02 10:59:41 +0100 | [diff] [blame] | 443 | rc = put_guest_lc(vcpu, inti->io.subchannel_id, |
| 444 | (u16 *)__LC_SUBCHANNEL_ID); |
| 445 | rc |= put_guest_lc(vcpu, inti->io.subchannel_nr, |
| 446 | (u16 *)__LC_SUBCHANNEL_NR); |
| 447 | rc |= put_guest_lc(vcpu, inti->io.io_int_parm, |
| 448 | (u32 *)__LC_IO_INT_PARM); |
| 449 | rc |= put_guest_lc(vcpu, inti->io.io_int_word, |
| 450 | (u32 *)__LC_IO_INT_WORD); |
| 451 | rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW, |
| 452 | &vcpu->arch.sie_block->gpsw, |
| 453 | sizeof(psw_t)); |
| 454 | rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW, |
| 455 | &vcpu->arch.sie_block->gpsw, |
| 456 | sizeof(psw_t)); |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 457 | break; |
| 458 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 459 | default: |
| 460 | BUG(); |
| 461 | } |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 462 | if (rc) { |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 463 | printk("kvm: The guest lowcore is not mapped during interrupt " |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 464 | "delivery, killing userspace\n"); |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 465 | do_exit(SIGKILL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu) |
| 470 | { |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 471 | int rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 472 | |
| 473 | if (psw_extint_disabled(vcpu)) |
| 474 | return 0; |
| 475 | if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul)) |
| 476 | return 0; |
Jens Freimann | 1a03b764 | 2014-02-12 14:05:38 +0100 | [diff] [blame] | 477 | rc = put_guest_lc(vcpu, 0x1004, (u16 __user *)__LC_EXT_INT_CODE); |
| 478 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 479 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 480 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 481 | &vcpu->arch.sie_block->gpsw, |
| 482 | sizeof(psw_t)); |
Heiko Carstens | dc5008b | 2013-03-05 13:14:43 +0100 | [diff] [blame] | 483 | if (rc) { |
Christian Borntraeger | 3cd6129 | 2008-07-25 15:51:54 +0200 | [diff] [blame] | 484 | printk("kvm: The guest lowcore is not mapped during interrupt " |
| 485 | "delivery, killing userspace\n"); |
| 486 | do_exit(SIGKILL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 487 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 488 | return 1; |
| 489 | } |
| 490 | |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 491 | int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 492 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 493 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 494 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 495 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 496 | int rc = 0; |
| 497 | |
| 498 | if (atomic_read(&li->active)) { |
| 499 | spin_lock_bh(&li->lock); |
| 500 | list_for_each_entry(inti, &li->list, list) |
| 501 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 502 | rc = 1; |
| 503 | break; |
| 504 | } |
| 505 | spin_unlock_bh(&li->lock); |
| 506 | } |
| 507 | |
| 508 | if ((!rc) && atomic_read(&fi->active)) { |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 509 | spin_lock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 510 | list_for_each_entry(inti, &fi->list, list) |
| 511 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 512 | rc = 1; |
| 513 | break; |
| 514 | } |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 515 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | if ((!rc) && (vcpu->arch.sie_block->ckc < |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 519 | get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 520 | if ((!psw_extint_disabled(vcpu)) && |
| 521 | (vcpu->arch.sie_block->gcr[0] & 0x800ul)) |
| 522 | rc = 1; |
| 523 | } |
| 524 | |
| 525 | return rc; |
| 526 | } |
| 527 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 528 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 529 | { |
| 530 | return 0; |
| 531 | } |
| 532 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 533 | int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) |
| 534 | { |
| 535 | u64 now, sltime; |
| 536 | DECLARE_WAITQUEUE(wait, current); |
| 537 | |
| 538 | vcpu->stat.exit_wait_state++; |
| 539 | if (kvm_cpu_has_interrupt(vcpu)) |
| 540 | return 0; |
| 541 | |
Carsten Otte | e52b2af | 2008-05-21 13:37:44 +0200 | [diff] [blame] | 542 | __set_cpu_idle(vcpu); |
| 543 | spin_lock_bh(&vcpu->arch.local_int.lock); |
| 544 | vcpu->arch.local_int.timer_due = 0; |
| 545 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
| 546 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 547 | if (psw_interrupts_disabled(vcpu)) { |
| 548 | VCPU_EVENT(vcpu, 3, "%s", "disabled wait"); |
| 549 | __unset_cpu_idle(vcpu); |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 550 | return -EOPNOTSUPP; /* disabled wait */ |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | if (psw_extint_disabled(vcpu) || |
| 554 | (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) { |
| 555 | VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer"); |
| 556 | goto no_timer; |
| 557 | } |
| 558 | |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 559 | now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 560 | if (vcpu->arch.sie_block->ckc < now) { |
| 561 | __unset_cpu_idle(vcpu); |
| 562 | return 0; |
| 563 | } |
| 564 | |
Heiko Carstens | ed4f209 | 2013-01-14 16:55:55 +0100 | [diff] [blame] | 565 | sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 566 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 567 | hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL); |
| 568 | VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 569 | no_timer: |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 570 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 571 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 572 | spin_lock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 573 | add_wait_queue(&vcpu->wq, &wait); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 574 | while (list_empty(&vcpu->arch.local_int.list) && |
| 575 | list_empty(&vcpu->arch.local_int.float_int->list) && |
| 576 | (!vcpu->arch.local_int.timer_due) && |
| 577 | !signal_pending(current)) { |
| 578 | set_current_state(TASK_INTERRUPTIBLE); |
| 579 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 580 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 581 | schedule(); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 582 | spin_lock(&vcpu->arch.local_int.float_int->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 583 | spin_lock_bh(&vcpu->arch.local_int.lock); |
| 584 | } |
| 585 | __unset_cpu_idle(vcpu); |
| 586 | __set_current_state(TASK_RUNNING); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 587 | remove_wait_queue(&vcpu->wq, &wait); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 588 | spin_unlock_bh(&vcpu->arch.local_int.lock); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 589 | spin_unlock(&vcpu->arch.local_int.float_int->lock); |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 590 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 591 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 592 | hrtimer_try_to_cancel(&vcpu->arch.ckc_timer); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 596 | void kvm_s390_tasklet(unsigned long parm) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 597 | { |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 598 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 599 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 600 | spin_lock(&vcpu->arch.local_int.lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 601 | vcpu->arch.local_int.timer_due = 1; |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 602 | if (waitqueue_active(&vcpu->wq)) |
| 603 | wake_up_interruptible(&vcpu->wq); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 604 | spin_unlock(&vcpu->arch.local_int.lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 605 | } |
| 606 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 607 | /* |
| 608 | * low level hrtimer wake routine. Because this runs in hardirq context |
| 609 | * we schedule a tasklet to do the real work. |
| 610 | */ |
| 611 | enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer) |
| 612 | { |
| 613 | struct kvm_vcpu *vcpu; |
| 614 | |
| 615 | vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer); |
Michael Mueller | 9cac38d | 2014-02-26 16:14:19 +0100 | [diff] [blame] | 616 | vcpu->preempted = true; |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 617 | tasklet_schedule(&vcpu->arch.tasklet); |
| 618 | |
| 619 | return HRTIMER_NORESTART; |
| 620 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 621 | |
Jens Freimann | 2ed10cc | 2014-02-11 13:48:07 +0100 | [diff] [blame] | 622 | void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu) |
| 623 | { |
| 624 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 625 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
| 626 | |
| 627 | spin_lock_bh(&li->lock); |
| 628 | list_for_each_entry_safe(inti, n, &li->list, list) { |
| 629 | list_del(&inti->list); |
| 630 | kfree(inti); |
| 631 | } |
| 632 | atomic_set(&li->active, 0); |
| 633 | spin_unlock_bh(&li->lock); |
| 634 | } |
| 635 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 636 | void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) |
| 637 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 638 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 639 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 640 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 641 | int deliver; |
| 642 | |
| 643 | __reset_intercept_indicators(vcpu); |
| 644 | if (atomic_read(&li->active)) { |
| 645 | do { |
| 646 | deliver = 0; |
| 647 | spin_lock_bh(&li->lock); |
| 648 | list_for_each_entry_safe(inti, n, &li->list, list) { |
| 649 | if (__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 ((vcpu->arch.sie_block->ckc < |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 667 | get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 668 | __try_deliver_ckc_interrupt(vcpu); |
| 669 | |
| 670 | if (atomic_read(&fi->active)) { |
| 671 | do { |
| 672 | deliver = 0; |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 673 | spin_lock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 674 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 675 | if (__interrupt_is_deliverable(vcpu, inti)) { |
| 676 | list_del(&inti->list); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 677 | fi->irq_count--; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 678 | deliver = 1; |
| 679 | break; |
| 680 | } |
| 681 | __set_intercept_indicator(vcpu, inti); |
| 682 | } |
| 683 | if (list_empty(&fi->list)) |
| 684 | atomic_set(&fi->active, 0); |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 685 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 686 | if (deliver) { |
| 687 | __do_deliver_interrupt(vcpu, inti); |
| 688 | kfree(inti); |
| 689 | } |
| 690 | } while (deliver); |
| 691 | } |
| 692 | } |
| 693 | |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 694 | void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu *vcpu) |
| 695 | { |
| 696 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 697 | struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int; |
| 698 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
| 699 | int deliver; |
| 700 | |
| 701 | __reset_intercept_indicators(vcpu); |
| 702 | if (atomic_read(&li->active)) { |
| 703 | do { |
| 704 | deliver = 0; |
| 705 | spin_lock_bh(&li->lock); |
| 706 | list_for_each_entry_safe(inti, n, &li->list, list) { |
| 707 | if ((inti->type == KVM_S390_MCHK) && |
| 708 | __interrupt_is_deliverable(vcpu, inti)) { |
| 709 | list_del(&inti->list); |
| 710 | deliver = 1; |
| 711 | break; |
| 712 | } |
| 713 | __set_intercept_indicator(vcpu, inti); |
| 714 | } |
| 715 | if (list_empty(&li->list)) |
| 716 | atomic_set(&li->active, 0); |
| 717 | spin_unlock_bh(&li->lock); |
| 718 | if (deliver) { |
| 719 | __do_deliver_interrupt(vcpu, inti); |
| 720 | kfree(inti); |
| 721 | } |
| 722 | } while (deliver); |
| 723 | } |
| 724 | |
| 725 | if (atomic_read(&fi->active)) { |
| 726 | do { |
| 727 | deliver = 0; |
| 728 | spin_lock(&fi->lock); |
| 729 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 730 | if ((inti->type == KVM_S390_MCHK) && |
| 731 | __interrupt_is_deliverable(vcpu, inti)) { |
| 732 | list_del(&inti->list); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 733 | fi->irq_count--; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 734 | deliver = 1; |
| 735 | break; |
| 736 | } |
| 737 | __set_intercept_indicator(vcpu, inti); |
| 738 | } |
| 739 | if (list_empty(&fi->list)) |
| 740 | atomic_set(&fi->active, 0); |
| 741 | spin_unlock(&fi->lock); |
| 742 | if (deliver) { |
| 743 | __do_deliver_interrupt(vcpu, inti); |
| 744 | kfree(inti); |
| 745 | } |
| 746 | } while (deliver); |
| 747 | } |
| 748 | } |
| 749 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 750 | int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code) |
| 751 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 752 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 753 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 754 | |
| 755 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 756 | if (!inti) |
| 757 | return -ENOMEM; |
| 758 | |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 759 | inti->type = KVM_S390_PROGRAM_INT; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 760 | inti->pgm.code = code; |
| 761 | |
| 762 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code); |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 763 | 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] | 764 | spin_lock_bh(&li->lock); |
| 765 | list_add(&inti->list, &li->list); |
| 766 | atomic_set(&li->active, 1); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 767 | BUG_ON(waitqueue_active(li->wq)); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 768 | spin_unlock_bh(&li->lock); |
| 769 | return 0; |
| 770 | } |
| 771 | |
Jens Freimann | bcd8468 | 2014-02-11 11:07:05 +0100 | [diff] [blame] | 772 | int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu, |
| 773 | struct kvm_s390_pgm_info *pgm_info) |
| 774 | { |
| 775 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 776 | struct kvm_s390_interrupt_info *inti; |
| 777 | |
| 778 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 779 | if (!inti) |
| 780 | return -ENOMEM; |
| 781 | |
| 782 | VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)", |
| 783 | pgm_info->code); |
| 784 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, |
| 785 | pgm_info->code, 0, 1); |
| 786 | |
| 787 | inti->type = KVM_S390_PROGRAM_INT; |
| 788 | memcpy(&inti->pgm, pgm_info, sizeof(inti->pgm)); |
| 789 | spin_lock_bh(&li->lock); |
| 790 | list_add(&inti->list, &li->list); |
| 791 | atomic_set(&li->active, 1); |
| 792 | BUG_ON(waitqueue_active(li->wq)); |
| 793 | spin_unlock_bh(&li->lock); |
| 794 | return 0; |
| 795 | } |
| 796 | |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 797 | struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm, |
| 798 | u64 cr6, u64 schid) |
| 799 | { |
| 800 | struct kvm_s390_float_interrupt *fi; |
| 801 | struct kvm_s390_interrupt_info *inti, *iter; |
| 802 | |
| 803 | if ((!schid && !cr6) || (schid && cr6)) |
| 804 | return NULL; |
| 805 | mutex_lock(&kvm->lock); |
| 806 | fi = &kvm->arch.float_int; |
| 807 | spin_lock(&fi->lock); |
| 808 | inti = NULL; |
| 809 | list_for_each_entry(iter, &fi->list, list) { |
| 810 | if (!is_ioint(iter->type)) |
| 811 | continue; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 812 | if (cr6 && |
| 813 | ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0)) |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 814 | continue; |
| 815 | if (schid) { |
| 816 | if (((schid & 0x00000000ffff0000) >> 16) != |
| 817 | iter->io.subchannel_id) |
| 818 | continue; |
| 819 | if ((schid & 0x000000000000ffff) != |
| 820 | iter->io.subchannel_nr) |
| 821 | continue; |
| 822 | } |
| 823 | inti = iter; |
| 824 | break; |
| 825 | } |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 826 | if (inti) { |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 827 | list_del_init(&inti->list); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 828 | fi->irq_count--; |
| 829 | } |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 830 | if (list_empty(&fi->list)) |
| 831 | atomic_set(&fi->active, 0); |
| 832 | spin_unlock(&fi->lock); |
| 833 | mutex_unlock(&kvm->lock); |
| 834 | return inti; |
| 835 | } |
| 836 | |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 837 | 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] | 838 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 839 | struct kvm_s390_local_interrupt *li; |
| 840 | struct kvm_s390_float_interrupt *fi; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 841 | struct kvm_s390_interrupt_info *iter; |
Jens Freimann | 1ee0bc5 | 2014-02-25 15:36:45 +0100 | [diff] [blame] | 842 | struct kvm_vcpu *dst_vcpu = NULL; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 843 | int sigcpu; |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 844 | int rc = 0; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 845 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 846 | mutex_lock(&kvm->lock); |
| 847 | fi = &kvm->arch.float_int; |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 848 | spin_lock(&fi->lock); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 849 | if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) { |
| 850 | rc = -EINVAL; |
| 851 | goto unlock_fi; |
| 852 | } |
| 853 | fi->irq_count++; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 854 | if (!is_ioint(inti->type)) { |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 855 | list_add_tail(&inti->list, &fi->list); |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 856 | } else { |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 857 | u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word); |
| 858 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 859 | /* Keep I/O interrupts sorted in isc order. */ |
| 860 | list_for_each_entry(iter, &fi->list, list) { |
| 861 | if (!is_ioint(iter->type)) |
| 862 | continue; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 863 | if (int_word_to_isc_bits(iter->io.io_int_word) |
| 864 | <= isc_bits) |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 865 | continue; |
| 866 | break; |
| 867 | } |
| 868 | list_add_tail(&inti->list, &iter->list); |
| 869 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 870 | atomic_set(&fi->active, 1); |
| 871 | sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS); |
| 872 | if (sigcpu == KVM_MAX_VCPUS) { |
| 873 | do { |
| 874 | sigcpu = fi->next_rr_cpu++; |
| 875 | if (sigcpu == KVM_MAX_VCPUS) |
| 876 | sigcpu = fi->next_rr_cpu = 0; |
Jens Freimann | 1ee0bc5 | 2014-02-25 15:36:45 +0100 | [diff] [blame] | 877 | } while (kvm_get_vcpu(kvm, sigcpu) == NULL); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 878 | } |
Jens Freimann | 1ee0bc5 | 2014-02-25 15:36:45 +0100 | [diff] [blame] | 879 | dst_vcpu = kvm_get_vcpu(kvm, sigcpu); |
| 880 | li = &dst_vcpu->arch.local_int; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 881 | spin_lock_bh(&li->lock); |
| 882 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 883 | if (waitqueue_active(li->wq)) |
| 884 | wake_up_interruptible(li->wq); |
Michael Mueller | 9cac38d | 2014-02-26 16:14:19 +0100 | [diff] [blame] | 885 | kvm_get_vcpu(kvm, sigcpu)->preempted = true; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 886 | spin_unlock_bh(&li->lock); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 887 | unlock_fi: |
Christian Borntraeger | b037a4f | 2009-05-12 17:21:50 +0200 | [diff] [blame] | 888 | spin_unlock(&fi->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 889 | mutex_unlock(&kvm->lock); |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 890 | return rc; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | int kvm_s390_inject_vm(struct kvm *kvm, |
| 894 | struct kvm_s390_interrupt *s390int) |
| 895 | { |
| 896 | struct kvm_s390_interrupt_info *inti; |
| 897 | |
| 898 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 899 | if (!inti) |
| 900 | return -ENOMEM; |
| 901 | |
| 902 | inti->type = s390int->type; |
| 903 | switch (inti->type) { |
| 904 | case KVM_S390_INT_VIRTIO: |
| 905 | VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx", |
| 906 | s390int->parm, s390int->parm64); |
| 907 | inti->ext.ext_params = s390int->parm; |
| 908 | inti->ext.ext_params2 = s390int->parm64; |
| 909 | break; |
| 910 | case KVM_S390_INT_SERVICE: |
| 911 | VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm); |
| 912 | inti->ext.ext_params = s390int->parm; |
| 913 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 914 | case KVM_S390_INT_PFAULT_DONE: |
| 915 | inti->type = s390int->type; |
| 916 | inti->ext.ext_params2 = s390int->parm64; |
| 917 | break; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 918 | case KVM_S390_MCHK: |
| 919 | VM_EVENT(kvm, 5, "inject: machine check parm64:%llx", |
| 920 | s390int->parm64); |
| 921 | inti->mchk.cr14 = s390int->parm; /* upper bits are not used */ |
| 922 | inti->mchk.mcic = s390int->parm64; |
| 923 | break; |
| 924 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 925 | if (inti->type & IOINT_AI_MASK) |
| 926 | VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)"); |
| 927 | else |
| 928 | VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x", |
| 929 | s390int->type & IOINT_CSSID_MASK, |
| 930 | s390int->type & IOINT_SSID_MASK, |
| 931 | s390int->type & IOINT_SCHID_MASK); |
| 932 | inti->io.subchannel_id = s390int->parm >> 16; |
| 933 | inti->io.subchannel_nr = s390int->parm & 0x0000ffffu; |
| 934 | inti->io.io_int_parm = s390int->parm64 >> 32; |
| 935 | inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull; |
| 936 | break; |
| 937 | default: |
| 938 | kfree(inti); |
| 939 | return -EINVAL; |
| 940 | } |
| 941 | trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64, |
| 942 | 2); |
| 943 | |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 944 | return __inject_vm(kvm, inti); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 945 | } |
| 946 | |
Cornelia Huck | 2f32d4e | 2014-01-08 18:07:54 +0100 | [diff] [blame] | 947 | void kvm_s390_reinject_io_int(struct kvm *kvm, |
| 948 | struct kvm_s390_interrupt_info *inti) |
| 949 | { |
| 950 | __inject_vm(kvm, inti); |
| 951 | } |
| 952 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 953 | int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, |
| 954 | struct kvm_s390_interrupt *s390int) |
| 955 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 956 | struct kvm_s390_local_interrupt *li; |
| 957 | struct kvm_s390_interrupt_info *inti; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 958 | |
| 959 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 960 | if (!inti) |
| 961 | return -ENOMEM; |
| 962 | |
| 963 | switch (s390int->type) { |
| 964 | case KVM_S390_PROGRAM_INT: |
| 965 | if (s390int->parm & 0xffff0000) { |
| 966 | kfree(inti); |
| 967 | return -EINVAL; |
| 968 | } |
| 969 | inti->type = s390int->type; |
| 970 | inti->pgm.code = s390int->parm; |
| 971 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)", |
| 972 | s390int->parm); |
| 973 | break; |
Christian Borntraeger | b7e6e4d | 2009-01-22 10:29:08 +0100 | [diff] [blame] | 974 | case KVM_S390_SIGP_SET_PREFIX: |
| 975 | inti->prefix.address = s390int->parm; |
| 976 | inti->type = s390int->type; |
| 977 | VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)", |
| 978 | s390int->parm); |
| 979 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 980 | case KVM_S390_SIGP_STOP: |
| 981 | case KVM_S390_RESTART: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 982 | VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type); |
| 983 | inti->type = s390int->type; |
| 984 | break; |
Jason J. Herne | 82a1273 | 2012-10-02 16:25:36 +0200 | [diff] [blame] | 985 | case KVM_S390_INT_EXTERNAL_CALL: |
| 986 | if (s390int->parm & 0xffff0000) { |
| 987 | kfree(inti); |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u", |
| 991 | s390int->parm); |
| 992 | inti->type = s390int->type; |
| 993 | inti->extcall.code = s390int->parm; |
| 994 | break; |
| 995 | case KVM_S390_INT_EMERGENCY: |
| 996 | if (s390int->parm & 0xffff0000) { |
| 997 | kfree(inti); |
| 998 | return -EINVAL; |
| 999 | } |
| 1000 | VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", s390int->parm); |
| 1001 | inti->type = s390int->type; |
| 1002 | inti->emerg.code = s390int->parm; |
| 1003 | break; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 1004 | case KVM_S390_MCHK: |
| 1005 | VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx", |
| 1006 | s390int->parm64); |
| 1007 | inti->type = s390int->type; |
| 1008 | inti->mchk.mcic = s390int->parm64; |
| 1009 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1010 | case KVM_S390_INT_PFAULT_INIT: |
| 1011 | inti->type = s390int->type; |
| 1012 | inti->ext.ext_params2 = s390int->parm64; |
| 1013 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1014 | case KVM_S390_INT_VIRTIO: |
| 1015 | case KVM_S390_INT_SERVICE: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 1016 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1017 | default: |
| 1018 | kfree(inti); |
| 1019 | return -EINVAL; |
| 1020 | } |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 1021 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, s390int->type, s390int->parm, |
| 1022 | s390int->parm64, 2); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1023 | |
| 1024 | mutex_lock(&vcpu->kvm->lock); |
| 1025 | li = &vcpu->arch.local_int; |
| 1026 | spin_lock_bh(&li->lock); |
| 1027 | if (inti->type == KVM_S390_PROGRAM_INT) |
| 1028 | list_add(&inti->list, &li->list); |
| 1029 | else |
| 1030 | list_add_tail(&inti->list, &li->list); |
| 1031 | atomic_set(&li->active, 1); |
| 1032 | if (inti->type == KVM_S390_SIGP_STOP) |
| 1033 | li->action_bits |= ACTION_STOP_ON_STOP; |
| 1034 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 1035 | if (waitqueue_active(&vcpu->wq)) |
| 1036 | wake_up_interruptible(&vcpu->wq); |
Michael Mueller | 9cac38d | 2014-02-26 16:14:19 +0100 | [diff] [blame] | 1037 | vcpu->preempted = true; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1038 | spin_unlock_bh(&li->lock); |
| 1039 | mutex_unlock(&vcpu->kvm->lock); |
| 1040 | return 0; |
| 1041 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1042 | |
| 1043 | static void clear_floating_interrupts(struct kvm *kvm) |
| 1044 | { |
| 1045 | struct kvm_s390_float_interrupt *fi; |
| 1046 | struct kvm_s390_interrupt_info *n, *inti = NULL; |
| 1047 | |
| 1048 | mutex_lock(&kvm->lock); |
| 1049 | fi = &kvm->arch.float_int; |
| 1050 | spin_lock(&fi->lock); |
| 1051 | list_for_each_entry_safe(inti, n, &fi->list, list) { |
| 1052 | list_del(&inti->list); |
| 1053 | kfree(inti); |
| 1054 | } |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 1055 | fi->irq_count = 0; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1056 | atomic_set(&fi->active, 0); |
| 1057 | spin_unlock(&fi->lock); |
| 1058 | mutex_unlock(&kvm->lock); |
| 1059 | } |
| 1060 | |
| 1061 | static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti, |
| 1062 | u8 *addr) |
| 1063 | { |
| 1064 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; |
| 1065 | struct kvm_s390_irq irq = {0}; |
| 1066 | |
| 1067 | irq.type = inti->type; |
| 1068 | switch (inti->type) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1069 | case KVM_S390_INT_PFAULT_INIT: |
| 1070 | case KVM_S390_INT_PFAULT_DONE: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1071 | case KVM_S390_INT_VIRTIO: |
| 1072 | case KVM_S390_INT_SERVICE: |
| 1073 | irq.u.ext = inti->ext; |
| 1074 | break; |
| 1075 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 1076 | irq.u.io = inti->io; |
| 1077 | break; |
| 1078 | case KVM_S390_MCHK: |
| 1079 | irq.u.mchk = inti->mchk; |
| 1080 | break; |
| 1081 | default: |
| 1082 | return -EINVAL; |
| 1083 | } |
| 1084 | |
| 1085 | if (copy_to_user(uptr, &irq, sizeof(irq))) |
| 1086 | return -EFAULT; |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len) |
| 1092 | { |
| 1093 | struct kvm_s390_interrupt_info *inti; |
| 1094 | struct kvm_s390_float_interrupt *fi; |
| 1095 | int ret = 0; |
| 1096 | int n = 0; |
| 1097 | |
| 1098 | mutex_lock(&kvm->lock); |
| 1099 | fi = &kvm->arch.float_int; |
| 1100 | spin_lock(&fi->lock); |
| 1101 | |
| 1102 | list_for_each_entry(inti, &fi->list, list) { |
| 1103 | if (len < sizeof(struct kvm_s390_irq)) { |
| 1104 | /* signal userspace to try again */ |
| 1105 | ret = -ENOMEM; |
| 1106 | break; |
| 1107 | } |
| 1108 | ret = copy_irq_to_user(inti, buf); |
| 1109 | if (ret) |
| 1110 | break; |
| 1111 | buf += sizeof(struct kvm_s390_irq); |
| 1112 | len -= sizeof(struct kvm_s390_irq); |
| 1113 | n++; |
| 1114 | } |
| 1115 | |
| 1116 | spin_unlock(&fi->lock); |
| 1117 | mutex_unlock(&kvm->lock); |
| 1118 | |
| 1119 | return ret < 0 ? ret : n; |
| 1120 | } |
| 1121 | |
| 1122 | static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
| 1123 | { |
| 1124 | int r; |
| 1125 | |
| 1126 | switch (attr->group) { |
| 1127 | case KVM_DEV_FLIC_GET_ALL_IRQS: |
| 1128 | r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr, |
| 1129 | attr->attr); |
| 1130 | break; |
| 1131 | default: |
| 1132 | r = -EINVAL; |
| 1133 | } |
| 1134 | |
| 1135 | return r; |
| 1136 | } |
| 1137 | |
| 1138 | static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti, |
| 1139 | u64 addr) |
| 1140 | { |
| 1141 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; |
| 1142 | void *target = NULL; |
| 1143 | void __user *source; |
| 1144 | u64 size; |
| 1145 | |
| 1146 | if (get_user(inti->type, (u64 __user *)addr)) |
| 1147 | return -EFAULT; |
| 1148 | |
| 1149 | switch (inti->type) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1150 | case KVM_S390_INT_PFAULT_INIT: |
| 1151 | case KVM_S390_INT_PFAULT_DONE: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1152 | case KVM_S390_INT_VIRTIO: |
| 1153 | case KVM_S390_INT_SERVICE: |
| 1154 | target = (void *) &inti->ext; |
| 1155 | source = &uptr->u.ext; |
| 1156 | size = sizeof(inti->ext); |
| 1157 | break; |
| 1158 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 1159 | target = (void *) &inti->io; |
| 1160 | source = &uptr->u.io; |
| 1161 | size = sizeof(inti->io); |
| 1162 | break; |
| 1163 | case KVM_S390_MCHK: |
| 1164 | target = (void *) &inti->mchk; |
| 1165 | source = &uptr->u.mchk; |
| 1166 | size = sizeof(inti->mchk); |
| 1167 | break; |
| 1168 | default: |
| 1169 | return -EINVAL; |
| 1170 | } |
| 1171 | |
| 1172 | if (copy_from_user(target, source, size)) |
| 1173 | return -EFAULT; |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
| 1178 | static int enqueue_floating_irq(struct kvm_device *dev, |
| 1179 | struct kvm_device_attr *attr) |
| 1180 | { |
| 1181 | struct kvm_s390_interrupt_info *inti = NULL; |
| 1182 | int r = 0; |
| 1183 | int len = attr->attr; |
| 1184 | |
| 1185 | if (len % sizeof(struct kvm_s390_irq) != 0) |
| 1186 | return -EINVAL; |
| 1187 | else if (len > KVM_S390_FLIC_MAX_BUFFER) |
| 1188 | return -EINVAL; |
| 1189 | |
| 1190 | while (len >= sizeof(struct kvm_s390_irq)) { |
| 1191 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 1192 | if (!inti) |
| 1193 | return -ENOMEM; |
| 1194 | |
| 1195 | r = copy_irq_from_user(inti, attr->addr); |
| 1196 | if (r) { |
| 1197 | kfree(inti); |
| 1198 | return r; |
| 1199 | } |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 1200 | r = __inject_vm(dev->kvm, inti); |
| 1201 | if (r) { |
| 1202 | kfree(inti); |
| 1203 | return r; |
| 1204 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1205 | len -= sizeof(struct kvm_s390_irq); |
| 1206 | attr->addr += sizeof(struct kvm_s390_irq); |
| 1207 | } |
| 1208 | |
| 1209 | return r; |
| 1210 | } |
| 1211 | |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 1212 | static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id) |
| 1213 | { |
| 1214 | if (id >= MAX_S390_IO_ADAPTERS) |
| 1215 | return NULL; |
| 1216 | return kvm->arch.adapters[id]; |
| 1217 | } |
| 1218 | |
| 1219 | static int register_io_adapter(struct kvm_device *dev, |
| 1220 | struct kvm_device_attr *attr) |
| 1221 | { |
| 1222 | struct s390_io_adapter *adapter; |
| 1223 | struct kvm_s390_io_adapter adapter_info; |
| 1224 | |
| 1225 | if (copy_from_user(&adapter_info, |
| 1226 | (void __user *)attr->addr, sizeof(adapter_info))) |
| 1227 | return -EFAULT; |
| 1228 | |
| 1229 | if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) || |
| 1230 | (dev->kvm->arch.adapters[adapter_info.id] != NULL)) |
| 1231 | return -EINVAL; |
| 1232 | |
| 1233 | adapter = kzalloc(sizeof(*adapter), GFP_KERNEL); |
| 1234 | if (!adapter) |
| 1235 | return -ENOMEM; |
| 1236 | |
| 1237 | INIT_LIST_HEAD(&adapter->maps); |
| 1238 | init_rwsem(&adapter->maps_lock); |
| 1239 | atomic_set(&adapter->nr_maps, 0); |
| 1240 | adapter->id = adapter_info.id; |
| 1241 | adapter->isc = adapter_info.isc; |
| 1242 | adapter->maskable = adapter_info.maskable; |
| 1243 | adapter->masked = false; |
| 1244 | adapter->swap = adapter_info.swap; |
| 1245 | dev->kvm->arch.adapters[adapter->id] = adapter; |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked) |
| 1251 | { |
| 1252 | int ret; |
| 1253 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); |
| 1254 | |
| 1255 | if (!adapter || !adapter->maskable) |
| 1256 | return -EINVAL; |
| 1257 | ret = adapter->masked; |
| 1258 | adapter->masked = masked; |
| 1259 | return ret; |
| 1260 | } |
| 1261 | |
| 1262 | static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr) |
| 1263 | { |
| 1264 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); |
| 1265 | struct s390_map_info *map; |
| 1266 | int ret; |
| 1267 | |
| 1268 | if (!adapter || !addr) |
| 1269 | return -EINVAL; |
| 1270 | |
| 1271 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 1272 | if (!map) { |
| 1273 | ret = -ENOMEM; |
| 1274 | goto out; |
| 1275 | } |
| 1276 | INIT_LIST_HEAD(&map->list); |
| 1277 | map->guest_addr = addr; |
| 1278 | map->addr = gmap_translate(addr, kvm->arch.gmap); |
| 1279 | if (map->addr == -EFAULT) { |
| 1280 | ret = -EFAULT; |
| 1281 | goto out; |
| 1282 | } |
| 1283 | ret = get_user_pages_fast(map->addr, 1, 1, &map->page); |
| 1284 | if (ret < 0) |
| 1285 | goto out; |
| 1286 | BUG_ON(ret != 1); |
| 1287 | down_write(&adapter->maps_lock); |
| 1288 | if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) { |
| 1289 | list_add_tail(&map->list, &adapter->maps); |
| 1290 | ret = 0; |
| 1291 | } else { |
| 1292 | put_page(map->page); |
| 1293 | ret = -EINVAL; |
| 1294 | } |
| 1295 | up_write(&adapter->maps_lock); |
| 1296 | out: |
| 1297 | if (ret) |
| 1298 | kfree(map); |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
| 1302 | static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr) |
| 1303 | { |
| 1304 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); |
| 1305 | struct s390_map_info *map, *tmp; |
| 1306 | int found = 0; |
| 1307 | |
| 1308 | if (!adapter || !addr) |
| 1309 | return -EINVAL; |
| 1310 | |
| 1311 | down_write(&adapter->maps_lock); |
| 1312 | list_for_each_entry_safe(map, tmp, &adapter->maps, list) { |
| 1313 | if (map->guest_addr == addr) { |
| 1314 | found = 1; |
| 1315 | atomic_dec(&adapter->nr_maps); |
| 1316 | list_del(&map->list); |
| 1317 | put_page(map->page); |
| 1318 | kfree(map); |
| 1319 | break; |
| 1320 | } |
| 1321 | } |
| 1322 | up_write(&adapter->maps_lock); |
| 1323 | |
| 1324 | return found ? 0 : -EINVAL; |
| 1325 | } |
| 1326 | |
| 1327 | void kvm_s390_destroy_adapters(struct kvm *kvm) |
| 1328 | { |
| 1329 | int i; |
| 1330 | struct s390_map_info *map, *tmp; |
| 1331 | |
| 1332 | for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) { |
| 1333 | if (!kvm->arch.adapters[i]) |
| 1334 | continue; |
| 1335 | list_for_each_entry_safe(map, tmp, |
| 1336 | &kvm->arch.adapters[i]->maps, list) { |
| 1337 | list_del(&map->list); |
| 1338 | put_page(map->page); |
| 1339 | kfree(map); |
| 1340 | } |
| 1341 | kfree(kvm->arch.adapters[i]); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | static int modify_io_adapter(struct kvm_device *dev, |
| 1346 | struct kvm_device_attr *attr) |
| 1347 | { |
| 1348 | struct kvm_s390_io_adapter_req req; |
| 1349 | struct s390_io_adapter *adapter; |
| 1350 | int ret; |
| 1351 | |
| 1352 | if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req))) |
| 1353 | return -EFAULT; |
| 1354 | |
| 1355 | adapter = get_io_adapter(dev->kvm, req.id); |
| 1356 | if (!adapter) |
| 1357 | return -EINVAL; |
| 1358 | switch (req.type) { |
| 1359 | case KVM_S390_IO_ADAPTER_MASK: |
| 1360 | ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask); |
| 1361 | if (ret > 0) |
| 1362 | ret = 0; |
| 1363 | break; |
| 1364 | case KVM_S390_IO_ADAPTER_MAP: |
| 1365 | ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr); |
| 1366 | break; |
| 1367 | case KVM_S390_IO_ADAPTER_UNMAP: |
| 1368 | ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr); |
| 1369 | break; |
| 1370 | default: |
| 1371 | ret = -EINVAL; |
| 1372 | } |
| 1373 | |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1377 | static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
| 1378 | { |
| 1379 | int r = 0; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1380 | unsigned int i; |
| 1381 | struct kvm_vcpu *vcpu; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1382 | |
| 1383 | switch (attr->group) { |
| 1384 | case KVM_DEV_FLIC_ENQUEUE: |
| 1385 | r = enqueue_floating_irq(dev, attr); |
| 1386 | break; |
| 1387 | case KVM_DEV_FLIC_CLEAR_IRQS: |
| 1388 | r = 0; |
| 1389 | clear_floating_interrupts(dev->kvm); |
| 1390 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1391 | case KVM_DEV_FLIC_APF_ENABLE: |
| 1392 | dev->kvm->arch.gmap->pfault_enabled = 1; |
| 1393 | break; |
| 1394 | case KVM_DEV_FLIC_APF_DISABLE_WAIT: |
| 1395 | dev->kvm->arch.gmap->pfault_enabled = 0; |
| 1396 | /* |
| 1397 | * Make sure no async faults are in transition when |
| 1398 | * clearing the queues. So we don't need to worry |
| 1399 | * about late coming workers. |
| 1400 | */ |
| 1401 | synchronize_srcu(&dev->kvm->srcu); |
| 1402 | kvm_for_each_vcpu(i, vcpu, dev->kvm) |
| 1403 | kvm_clear_async_pf_completion_queue(vcpu); |
| 1404 | break; |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 1405 | case KVM_DEV_FLIC_ADAPTER_REGISTER: |
| 1406 | r = register_io_adapter(dev, attr); |
| 1407 | break; |
| 1408 | case KVM_DEV_FLIC_ADAPTER_MODIFY: |
| 1409 | r = modify_io_adapter(dev, attr); |
| 1410 | break; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1411 | default: |
| 1412 | r = -EINVAL; |
| 1413 | } |
| 1414 | |
| 1415 | return r; |
| 1416 | } |
| 1417 | |
| 1418 | static int flic_create(struct kvm_device *dev, u32 type) |
| 1419 | { |
| 1420 | if (!dev) |
| 1421 | return -EINVAL; |
| 1422 | if (dev->kvm->arch.flic) |
| 1423 | return -EINVAL; |
| 1424 | dev->kvm->arch.flic = dev; |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | static void flic_destroy(struct kvm_device *dev) |
| 1429 | { |
| 1430 | dev->kvm->arch.flic = NULL; |
| 1431 | kfree(dev); |
| 1432 | } |
| 1433 | |
| 1434 | /* s390 floating irq controller (flic) */ |
| 1435 | struct kvm_device_ops kvm_flic_ops = { |
| 1436 | .name = "kvm-flic", |
| 1437 | .get_attr = flic_get_attr, |
| 1438 | .set_attr = flic_set_attr, |
| 1439 | .create = flic_create, |
| 1440 | .destroy = flic_destroy, |
| 1441 | }; |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 1442 | |
| 1443 | static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap) |
| 1444 | { |
| 1445 | unsigned long bit; |
| 1446 | |
| 1447 | bit = bit_nr + (addr % PAGE_SIZE) * 8; |
| 1448 | |
| 1449 | return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit; |
| 1450 | } |
| 1451 | |
| 1452 | static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter, |
| 1453 | u64 addr) |
| 1454 | { |
| 1455 | struct s390_map_info *map; |
| 1456 | |
| 1457 | if (!adapter) |
| 1458 | return NULL; |
| 1459 | |
| 1460 | list_for_each_entry(map, &adapter->maps, list) { |
| 1461 | if (map->guest_addr == addr) |
| 1462 | return map; |
| 1463 | } |
| 1464 | return NULL; |
| 1465 | } |
| 1466 | |
| 1467 | static int adapter_indicators_set(struct kvm *kvm, |
| 1468 | struct s390_io_adapter *adapter, |
| 1469 | struct kvm_s390_adapter_int *adapter_int) |
| 1470 | { |
| 1471 | unsigned long bit; |
| 1472 | int summary_set, idx; |
| 1473 | struct s390_map_info *info; |
| 1474 | void *map; |
| 1475 | |
| 1476 | info = get_map_info(adapter, adapter_int->ind_addr); |
| 1477 | if (!info) |
| 1478 | return -1; |
| 1479 | map = page_address(info->page); |
| 1480 | bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap); |
| 1481 | set_bit(bit, map); |
| 1482 | idx = srcu_read_lock(&kvm->srcu); |
| 1483 | mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT); |
| 1484 | set_page_dirty_lock(info->page); |
| 1485 | info = get_map_info(adapter, adapter_int->summary_addr); |
| 1486 | if (!info) { |
| 1487 | srcu_read_unlock(&kvm->srcu, idx); |
| 1488 | return -1; |
| 1489 | } |
| 1490 | map = page_address(info->page); |
| 1491 | bit = get_ind_bit(info->addr, adapter_int->summary_offset, |
| 1492 | adapter->swap); |
| 1493 | summary_set = test_and_set_bit(bit, map); |
| 1494 | mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT); |
| 1495 | set_page_dirty_lock(info->page); |
| 1496 | srcu_read_unlock(&kvm->srcu, idx); |
| 1497 | return summary_set ? 0 : 1; |
| 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * < 0 - not injected due to error |
| 1502 | * = 0 - coalesced, summary indicator already active |
| 1503 | * > 0 - injected interrupt |
| 1504 | */ |
| 1505 | static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e, |
| 1506 | struct kvm *kvm, int irq_source_id, int level, |
| 1507 | bool line_status) |
| 1508 | { |
| 1509 | int ret; |
| 1510 | struct s390_io_adapter *adapter; |
| 1511 | |
| 1512 | /* We're only interested in the 0->1 transition. */ |
| 1513 | if (!level) |
| 1514 | return 0; |
| 1515 | adapter = get_io_adapter(kvm, e->adapter.adapter_id); |
| 1516 | if (!adapter) |
| 1517 | return -1; |
| 1518 | down_read(&adapter->maps_lock); |
| 1519 | ret = adapter_indicators_set(kvm, adapter, &e->adapter); |
| 1520 | up_read(&adapter->maps_lock); |
| 1521 | if ((ret > 0) && !adapter->masked) { |
| 1522 | struct kvm_s390_interrupt s390int = { |
| 1523 | .type = KVM_S390_INT_IO(1, 0, 0, 0), |
| 1524 | .parm = 0, |
| 1525 | .parm64 = (adapter->isc << 27) | 0x80000000, |
| 1526 | }; |
| 1527 | ret = kvm_s390_inject_vm(kvm, &s390int); |
| 1528 | if (ret == 0) |
| 1529 | ret = 1; |
| 1530 | } |
| 1531 | return ret; |
| 1532 | } |
| 1533 | |
| 1534 | int kvm_set_routing_entry(struct kvm_irq_routing_table *rt, |
| 1535 | struct kvm_kernel_irq_routing_entry *e, |
| 1536 | const struct kvm_irq_routing_entry *ue) |
| 1537 | { |
| 1538 | int ret; |
| 1539 | |
| 1540 | switch (ue->type) { |
| 1541 | case KVM_IRQ_ROUTING_S390_ADAPTER: |
| 1542 | e->set = set_adapter_int; |
| 1543 | e->adapter.summary_addr = ue->u.adapter.summary_addr; |
| 1544 | e->adapter.ind_addr = ue->u.adapter.ind_addr; |
| 1545 | e->adapter.summary_offset = ue->u.adapter.summary_offset; |
| 1546 | e->adapter.ind_offset = ue->u.adapter.ind_offset; |
| 1547 | e->adapter.adapter_id = ue->u.adapter.adapter_id; |
| 1548 | ret = 0; |
| 1549 | break; |
| 1550 | default: |
| 1551 | ret = -EINVAL; |
| 1552 | } |
| 1553 | |
| 1554 | return ret; |
| 1555 | } |
| 1556 | |
| 1557 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, |
| 1558 | int irq_source_id, int level, bool line_status) |
| 1559 | { |
| 1560 | return -EINVAL; |
| 1561 | } |