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