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