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 | * |
Thomas Huth | 33b412a | 2015-02-11 10:38:46 +0100 | [diff] [blame] | 4 | * Copyright IBM Corp. 2008, 2015 |
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> |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 19 | #include <linux/bitmap.h> |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 20 | #include <linux/vmalloc.h> |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 21 | #include <asm/asm-offsets.h> |
Thomas Huth | 33b412a | 2015-02-11 10:38:46 +0100 | [diff] [blame] | 22 | #include <asm/dis.h> |
Heiko Carstens | cbb870c | 2010-02-26 22:37:43 +0100 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 24 | #include <asm/sclp.h> |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 25 | #include <asm/isc.h> |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 26 | #include "kvm-s390.h" |
| 27 | #include "gaccess.h" |
Cornelia Huck | ade38c3 | 2012-07-23 17:20:30 +0200 | [diff] [blame] | 28 | #include "trace-s390.h" |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 29 | |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 30 | #define IOINT_SCHID_MASK 0x0000ffff |
| 31 | #define IOINT_SSID_MASK 0x00030000 |
| 32 | #define IOINT_CSSID_MASK 0x03fc0000 |
| 33 | #define IOINT_AI_MASK 0x04000000 |
Jens Freimann | 44c6ca3 | 2014-04-16 13:57:18 +0200 | [diff] [blame] | 34 | #define PFAULT_INIT 0x0600 |
Jens Freimann | 60f90a1 | 2014-11-10 17:20:07 +0100 | [diff] [blame] | 35 | #define PFAULT_DONE 0x0680 |
| 36 | #define VIRTIO_PARAM 0x0d00 |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 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 | |
David Hildenbrand | b4aec92 | 2014-12-01 15:55:42 +0100 | [diff] [blame] | 73 | static int ckc_irq_pending(struct kvm_vcpu *vcpu) |
| 74 | { |
| 75 | if (!(vcpu->arch.sie_block->ckc < |
| 76 | get_tod_clock_fast() + vcpu->arch.sie_block->epoch)) |
| 77 | return 0; |
| 78 | return ckc_interrupts_enabled(vcpu); |
| 79 | } |
| 80 | |
| 81 | static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu) |
| 82 | { |
| 83 | return !psw_extint_disabled(vcpu) && |
| 84 | (vcpu->arch.sie_block->gcr[0] & 0x400ul); |
| 85 | } |
| 86 | |
| 87 | static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu) |
| 88 | { |
| 89 | return (vcpu->arch.sie_block->cputm >> 63) && |
| 90 | cpu_timer_interrupts_enabled(vcpu); |
| 91 | } |
| 92 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 93 | static inline int is_ioirq(unsigned long irq_type) |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 94 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 95 | return ((irq_type >= IRQ_PEND_IO_ISC_0) && |
| 96 | (irq_type <= IRQ_PEND_IO_ISC_7)); |
| 97 | } |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 98 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 99 | static uint64_t isc_to_isc_bits(int isc) |
| 100 | { |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 101 | return (0x80 >> isc) << 24; |
| 102 | } |
| 103 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 104 | static inline u8 int_word_to_isc(u32 int_word) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 105 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 106 | return (int_word & 0x38000000) >> 27; |
| 107 | } |
| 108 | |
| 109 | static inline unsigned long pending_floating_irqs(struct kvm_vcpu *vcpu) |
| 110 | { |
| 111 | return vcpu->kvm->arch.float_int.pending_irqs; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 114 | static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu) |
| 115 | { |
| 116 | return vcpu->arch.local_int.pending_irqs; |
| 117 | } |
| 118 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 119 | static unsigned long disable_iscs(struct kvm_vcpu *vcpu, |
| 120 | unsigned long active_mask) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 121 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 122 | int i; |
| 123 | |
| 124 | for (i = 0; i <= MAX_ISC; i++) |
| 125 | if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i))) |
| 126 | active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i)); |
| 127 | |
| 128 | return active_mask; |
| 129 | } |
| 130 | |
| 131 | static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu) |
| 132 | { |
| 133 | unsigned long active_mask; |
| 134 | |
| 135 | active_mask = pending_local_irqs(vcpu); |
| 136 | active_mask |= pending_floating_irqs(vcpu); |
Jens Freimann | ffeca0a | 2015-04-17 10:21:04 +0200 | [diff] [blame] | 137 | if (!active_mask) |
| 138 | return 0; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 139 | |
| 140 | if (psw_extint_disabled(vcpu)) |
| 141 | active_mask &= ~IRQ_PEND_EXT_MASK; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 142 | if (psw_ioint_disabled(vcpu)) |
| 143 | active_mask &= ~IRQ_PEND_IO_MASK; |
| 144 | else |
| 145 | active_mask = disable_iscs(vcpu, active_mask); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 146 | if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul)) |
| 147 | __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask); |
| 148 | if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul)) |
| 149 | __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask); |
| 150 | if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul)) |
| 151 | __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask); |
| 152 | if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul)) |
| 153 | __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 154 | if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul)) |
| 155 | __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 156 | if (psw_mchk_disabled(vcpu)) |
| 157 | active_mask &= ~IRQ_PEND_MCHK_MASK; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 158 | if (!(vcpu->arch.sie_block->gcr[14] & |
| 159 | vcpu->kvm->arch.float_int.mchk.cr14)) |
| 160 | __clear_bit(IRQ_PEND_MCHK_REP, &active_mask); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 161 | |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 162 | /* |
| 163 | * STOP irqs will never be actively delivered. They are triggered via |
| 164 | * intercept requests and cleared when the stop intercept is performed. |
| 165 | */ |
| 166 | __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask); |
| 167 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 168 | return active_mask; |
| 169 | } |
| 170 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 171 | static void __set_cpu_idle(struct kvm_vcpu *vcpu) |
| 172 | { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 173 | atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
| 174 | set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); |
| 175 | } |
| 176 | |
| 177 | static void __unset_cpu_idle(struct kvm_vcpu *vcpu) |
| 178 | { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 179 | atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags); |
| 180 | clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask); |
| 181 | } |
| 182 | |
| 183 | static void __reset_intercept_indicators(struct kvm_vcpu *vcpu) |
| 184 | { |
David Hildenbrand | 4953919 | 2014-02-21 08:59:59 +0100 | [diff] [blame] | 185 | atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT, |
| 186 | &vcpu->arch.sie_block->cpuflags); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 187 | vcpu->arch.sie_block->lctl = 0x0000; |
David Hildenbrand | 27291e2 | 2014-01-23 12:26:52 +0100 | [diff] [blame] | 188 | vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT); |
| 189 | |
| 190 | if (guestdbg_enabled(vcpu)) { |
| 191 | vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 | |
| 192 | LCTL_CR10 | LCTL_CR11); |
| 193 | vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT); |
| 194 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag) |
| 198 | { |
| 199 | atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags); |
| 200 | } |
| 201 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 202 | static void set_intercept_indicators_io(struct kvm_vcpu *vcpu) |
| 203 | { |
| 204 | if (!(pending_floating_irqs(vcpu) & IRQ_PEND_IO_MASK)) |
| 205 | return; |
| 206 | else if (psw_ioint_disabled(vcpu)) |
| 207 | __set_cpuflag(vcpu, CPUSTAT_IO_INT); |
| 208 | else |
| 209 | vcpu->arch.sie_block->lctl |= LCTL_CR6; |
| 210 | } |
| 211 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 212 | static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu) |
| 213 | { |
| 214 | if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK)) |
| 215 | return; |
| 216 | if (psw_extint_disabled(vcpu)) |
| 217 | __set_cpuflag(vcpu, CPUSTAT_EXT_INT); |
| 218 | else |
| 219 | vcpu->arch.sie_block->lctl |= LCTL_CR0; |
| 220 | } |
| 221 | |
| 222 | static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu) |
| 223 | { |
| 224 | if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK)) |
| 225 | return; |
| 226 | if (psw_mchk_disabled(vcpu)) |
| 227 | vcpu->arch.sie_block->ictl |= ICTL_LPSW; |
| 228 | else |
| 229 | vcpu->arch.sie_block->lctl |= LCTL_CR14; |
| 230 | } |
| 231 | |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 232 | static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu) |
| 233 | { |
| 234 | if (kvm_s390_is_stop_irq_pending(vcpu)) |
| 235 | __set_cpuflag(vcpu, CPUSTAT_STOP_INT); |
| 236 | } |
| 237 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 238 | /* Set interception request for non-deliverable interrupts */ |
| 239 | static void set_intercept_indicators(struct kvm_vcpu *vcpu) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 240 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 241 | set_intercept_indicators_io(vcpu); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 242 | set_intercept_indicators_ext(vcpu); |
| 243 | set_intercept_indicators_mchk(vcpu); |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 244 | set_intercept_indicators_stop(vcpu); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Jens Freimann | 8a2ef71 | 2014-07-23 16:36:06 +0200 | [diff] [blame] | 247 | static u16 get_ilc(struct kvm_vcpu *vcpu) |
| 248 | { |
Jens Freimann | 8a2ef71 | 2014-07-23 16:36:06 +0200 | [diff] [blame] | 249 | switch (vcpu->arch.sie_block->icptcode) { |
| 250 | case ICPT_INST: |
| 251 | case ICPT_INSTPROGI: |
| 252 | case ICPT_OPEREXC: |
| 253 | case ICPT_PARTEXEC: |
| 254 | case ICPT_IOINST: |
| 255 | /* last instruction only stored for these icptcodes */ |
Thomas Huth | 33b412a | 2015-02-11 10:38:46 +0100 | [diff] [blame] | 256 | return insn_length(vcpu->arch.sie_block->ipa >> 8); |
Jens Freimann | 8a2ef71 | 2014-07-23 16:36:06 +0200 | [diff] [blame] | 257 | case ICPT_PROGI: |
| 258 | return vcpu->arch.sie_block->pgmilc; |
| 259 | default: |
| 260 | return 0; |
| 261 | } |
| 262 | } |
| 263 | |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 264 | static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu) |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 265 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 266 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 267 | int rc; |
| 268 | |
| 269 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER, |
| 270 | 0, 0); |
| 271 | |
| 272 | rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER, |
| 273 | (u16 *)__LC_EXT_INT_CODE); |
David Hildenbrand | 467fc29 | 2014-12-01 12:02:44 +0100 | [diff] [blame] | 274 | rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 275 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 276 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 277 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 278 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 279 | clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 280 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu) |
| 284 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 285 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 286 | int rc; |
| 287 | |
| 288 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP, |
| 289 | 0, 0); |
| 290 | |
| 291 | rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP, |
| 292 | (u16 __user *)__LC_EXT_INT_CODE); |
David Hildenbrand | 467fc29 | 2014-12-01 12:02:44 +0100 | [diff] [blame] | 293 | rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 294 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 295 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 296 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 297 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 298 | clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 299 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 302 | static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 303 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 304 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 305 | struct kvm_s390_ext_info ext; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 306 | int rc; |
| 307 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 308 | spin_lock(&li->lock); |
| 309 | ext = li->irq.ext; |
| 310 | clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs); |
| 311 | li->irq.ext.ext_params2 = 0; |
| 312 | spin_unlock(&li->lock); |
| 313 | |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 314 | VCPU_EVENT(vcpu, 4, "interrupt: pfault init parm:%x,parm64:%llx", |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 315 | 0, ext.ext_params2); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 316 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
| 317 | KVM_S390_INT_PFAULT_INIT, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 318 | 0, ext.ext_params2); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 319 | |
| 320 | rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE); |
| 321 | rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR); |
| 322 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 323 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 324 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 325 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 326 | rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2); |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 327 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 330 | static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 331 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 332 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 333 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 334 | struct kvm_s390_mchk_info mchk = {}; |
Eric Farman | bc17de7 | 2014-04-14 16:01:09 -0400 | [diff] [blame] | 335 | unsigned long adtl_status_addr; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 336 | int deliver = 0; |
| 337 | int rc = 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 338 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 339 | spin_lock(&fi->lock); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 340 | spin_lock(&li->lock); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 341 | if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) || |
| 342 | test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) { |
| 343 | /* |
| 344 | * If there was an exigent machine check pending, then any |
| 345 | * repressible machine checks that might have been pending |
| 346 | * are indicated along with it, so always clear bits for |
| 347 | * repressible and exigent interrupts |
| 348 | */ |
| 349 | mchk = li->irq.mchk; |
| 350 | clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs); |
| 351 | clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs); |
| 352 | memset(&li->irq.mchk, 0, sizeof(mchk)); |
| 353 | deliver = 1; |
| 354 | } |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 355 | /* |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 356 | * We indicate floating repressible conditions along with |
| 357 | * other pending conditions. Channel Report Pending and Channel |
| 358 | * Subsystem damage are the only two and and are indicated by |
| 359 | * bits in mcic and masked in cr14. |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 360 | */ |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 361 | if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) { |
| 362 | mchk.mcic |= fi->mchk.mcic; |
| 363 | mchk.cr14 |= fi->mchk.cr14; |
| 364 | memset(&fi->mchk, 0, sizeof(mchk)); |
| 365 | deliver = 1; |
| 366 | } |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 367 | spin_unlock(&li->lock); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 368 | spin_unlock(&fi->lock); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 369 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 370 | if (deliver) { |
| 371 | VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx", |
| 372 | mchk.mcic); |
| 373 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
| 374 | KVM_S390_MCHK, |
| 375 | mchk.cr14, mchk.mcic); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 376 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 377 | rc = kvm_s390_vcpu_store_status(vcpu, |
| 378 | KVM_S390_STORE_STATUS_PREFIXED); |
| 379 | rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR, |
| 380 | &adtl_status_addr, |
| 381 | sizeof(unsigned long)); |
| 382 | rc |= kvm_s390_vcpu_store_adtl_status(vcpu, |
| 383 | adtl_status_addr); |
| 384 | rc |= put_guest_lc(vcpu, mchk.mcic, |
| 385 | (u64 __user *) __LC_MCCK_CODE); |
| 386 | rc |= put_guest_lc(vcpu, mchk.failing_storage_address, |
| 387 | (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR); |
| 388 | rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, |
| 389 | &mchk.fixed_logout, |
| 390 | sizeof(mchk.fixed_logout)); |
| 391 | rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW, |
| 392 | &vcpu->arch.sie_block->gpsw, |
| 393 | sizeof(psw_t)); |
| 394 | rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW, |
| 395 | &vcpu->arch.sie_block->gpsw, |
| 396 | sizeof(psw_t)); |
| 397 | } |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 398 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static int __must_check __deliver_restart(struct kvm_vcpu *vcpu) |
| 402 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 403 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 404 | int rc; |
| 405 | |
| 406 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart"); |
| 407 | vcpu->stat.deliver_restart_signal++; |
| 408 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0); |
| 409 | |
| 410 | rc = write_guest_lc(vcpu, |
| 411 | offsetof(struct _lowcore, restart_old_psw), |
| 412 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 413 | rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw), |
| 414 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 415 | clear_bit(IRQ_PEND_RESTART, &li->pending_irqs); |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 416 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 419 | static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 420 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 421 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 422 | struct kvm_s390_prefix_info prefix; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 423 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 424 | spin_lock(&li->lock); |
| 425 | prefix = li->irq.prefix; |
| 426 | li->irq.prefix.address = 0; |
| 427 | clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs); |
| 428 | spin_unlock(&li->lock); |
| 429 | |
| 430 | VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", prefix.address); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 431 | vcpu->stat.deliver_prefix_signal++; |
| 432 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
| 433 | KVM_S390_SIGP_SET_PREFIX, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 434 | prefix.address, 0); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 435 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 436 | kvm_s390_set_prefix(vcpu, prefix.address); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 440 | static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 441 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 442 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 443 | int rc; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 444 | int cpu_addr; |
| 445 | |
| 446 | spin_lock(&li->lock); |
| 447 | cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS); |
| 448 | clear_bit(cpu_addr, li->sigp_emerg_pending); |
| 449 | if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS)) |
| 450 | clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs); |
| 451 | spin_unlock(&li->lock); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 452 | |
| 453 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg"); |
| 454 | vcpu->stat.deliver_emergency_signal++; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 455 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY, |
| 456 | cpu_addr, 0); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 457 | |
| 458 | rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG, |
| 459 | (u16 *)__LC_EXT_INT_CODE); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 460 | rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 461 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 462 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 463 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 464 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 465 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 468 | static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 469 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 470 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 471 | struct kvm_s390_extcall_info extcall; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 472 | int rc; |
| 473 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 474 | spin_lock(&li->lock); |
| 475 | extcall = li->irq.extcall; |
| 476 | li->irq.extcall.code = 0; |
| 477 | clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs); |
| 478 | spin_unlock(&li->lock); |
| 479 | |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 480 | VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call"); |
| 481 | vcpu->stat.deliver_external_call++; |
| 482 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
| 483 | KVM_S390_INT_EXTERNAL_CALL, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 484 | extcall.code, 0); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 485 | |
| 486 | rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL, |
| 487 | (u16 *)__LC_EXT_INT_CODE); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 488 | rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 489 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 490 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 491 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw, |
| 492 | sizeof(psw_t)); |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 493 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 496 | static int __must_check __deliver_prog(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 497 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 498 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 499 | struct kvm_s390_pgm_info pgm_info; |
Thomas Huth | a9a846fd | 2015-02-05 09:06:56 +0100 | [diff] [blame] | 500 | int rc = 0, nullifying = false; |
Jens Freimann | 8a2ef71 | 2014-07-23 16:36:06 +0200 | [diff] [blame] | 501 | u16 ilc = get_ilc(vcpu); |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 502 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 503 | spin_lock(&li->lock); |
| 504 | pgm_info = li->irq.pgm; |
| 505 | clear_bit(IRQ_PEND_PROG, &li->pending_irqs); |
| 506 | memset(&li->irq.pgm, 0, sizeof(pgm_info)); |
| 507 | spin_unlock(&li->lock); |
| 508 | |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 509 | VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x", |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 510 | pgm_info.code, ilc); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 511 | vcpu->stat.deliver_program_int++; |
| 512 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 513 | pgm_info.code, 0); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 514 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 515 | switch (pgm_info.code & ~PGM_PER) { |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 516 | case PGM_AFX_TRANSLATION: |
| 517 | case PGM_ASX_TRANSLATION: |
| 518 | case PGM_EX_TRANSLATION: |
| 519 | case PGM_LFX_TRANSLATION: |
| 520 | case PGM_LSTE_SEQUENCE: |
| 521 | case PGM_LSX_TRANSLATION: |
| 522 | case PGM_LX_TRANSLATION: |
| 523 | case PGM_PRIMARY_AUTHORITY: |
| 524 | case PGM_SECONDARY_AUTHORITY: |
Thomas Huth | a9a846fd | 2015-02-05 09:06:56 +0100 | [diff] [blame] | 525 | nullifying = true; |
| 526 | /* fall through */ |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 527 | case PGM_SPACE_SWITCH: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 528 | rc = put_guest_lc(vcpu, pgm_info.trans_exc_code, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 529 | (u64 *)__LC_TRANS_EXC_CODE); |
| 530 | break; |
| 531 | case PGM_ALEN_TRANSLATION: |
| 532 | case PGM_ALE_SEQUENCE: |
| 533 | case PGM_ASTE_INSTANCE: |
| 534 | case PGM_ASTE_SEQUENCE: |
| 535 | case PGM_ASTE_VALIDITY: |
| 536 | case PGM_EXTENDED_AUTHORITY: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 537 | rc = put_guest_lc(vcpu, pgm_info.exc_access_id, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 538 | (u8 *)__LC_EXC_ACCESS_ID); |
Thomas Huth | a9a846fd | 2015-02-05 09:06:56 +0100 | [diff] [blame] | 539 | nullifying = true; |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 540 | break; |
| 541 | case PGM_ASCE_TYPE: |
| 542 | case PGM_PAGE_TRANSLATION: |
| 543 | case PGM_REGION_FIRST_TRANS: |
| 544 | case PGM_REGION_SECOND_TRANS: |
| 545 | case PGM_REGION_THIRD_TRANS: |
| 546 | case PGM_SEGMENT_TRANSLATION: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 547 | rc = put_guest_lc(vcpu, pgm_info.trans_exc_code, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 548 | (u64 *)__LC_TRANS_EXC_CODE); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 549 | rc |= put_guest_lc(vcpu, pgm_info.exc_access_id, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 550 | (u8 *)__LC_EXC_ACCESS_ID); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 551 | rc |= put_guest_lc(vcpu, pgm_info.op_access_id, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 552 | (u8 *)__LC_OP_ACCESS_ID); |
Thomas Huth | a9a846fd | 2015-02-05 09:06:56 +0100 | [diff] [blame] | 553 | nullifying = true; |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 554 | break; |
| 555 | case PGM_MONITOR: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 556 | rc = put_guest_lc(vcpu, pgm_info.mon_class_nr, |
Thomas Huth | a36c539 | 2014-10-16 14:31:53 +0200 | [diff] [blame] | 557 | (u16 *)__LC_MON_CLASS_NR); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 558 | rc |= put_guest_lc(vcpu, pgm_info.mon_code, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 559 | (u64 *)__LC_MON_CODE); |
| 560 | break; |
Eric Farman | 403c864 | 2015-02-02 15:01:06 -0500 | [diff] [blame] | 561 | case PGM_VECTOR_PROCESSING: |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 562 | case PGM_DATA: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 563 | rc = put_guest_lc(vcpu, pgm_info.data_exc_code, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 564 | (u32 *)__LC_DATA_EXC_CODE); |
| 565 | break; |
| 566 | case PGM_PROTECTION: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 567 | rc = put_guest_lc(vcpu, pgm_info.trans_exc_code, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 568 | (u64 *)__LC_TRANS_EXC_CODE); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 569 | rc |= put_guest_lc(vcpu, pgm_info.exc_access_id, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 570 | (u8 *)__LC_EXC_ACCESS_ID); |
| 571 | break; |
Thomas Huth | a9a846fd | 2015-02-05 09:06:56 +0100 | [diff] [blame] | 572 | case PGM_STACK_FULL: |
| 573 | case PGM_STACK_EMPTY: |
| 574 | case PGM_STACK_SPECIFICATION: |
| 575 | case PGM_STACK_TYPE: |
| 576 | case PGM_STACK_OPERATION: |
| 577 | case PGM_TRACE_TABEL: |
| 578 | case PGM_CRYPTO_OPERATION: |
| 579 | nullifying = true; |
| 580 | break; |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 583 | if (pgm_info.code & PGM_PER) { |
| 584 | rc |= put_guest_lc(vcpu, pgm_info.per_code, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 585 | (u8 *) __LC_PER_CODE); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 586 | rc |= put_guest_lc(vcpu, pgm_info.per_atmid, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 587 | (u8 *)__LC_PER_ATMID); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 588 | rc |= put_guest_lc(vcpu, pgm_info.per_address, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 589 | (u64 *) __LC_PER_ADDRESS); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 590 | rc |= put_guest_lc(vcpu, pgm_info.per_access_id, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 591 | (u8 *) __LC_PER_ACCESS_ID); |
| 592 | } |
| 593 | |
Thomas Huth | a9a846fd | 2015-02-05 09:06:56 +0100 | [diff] [blame] | 594 | if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST) |
| 595 | kvm_s390_rewind_psw(vcpu, ilc); |
| 596 | |
Jens Freimann | 8a2ef71 | 2014-07-23 16:36:06 +0200 | [diff] [blame] | 597 | rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC); |
David Hildenbrand | 2ba4596 | 2015-03-25 13:12:32 +0100 | [diff] [blame] | 598 | rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea, |
| 599 | (u64 *) __LC_LAST_BREAK); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 600 | rc |= put_guest_lc(vcpu, pgm_info.code, |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 601 | (u16 *)__LC_PGM_INT_CODE); |
| 602 | rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW, |
| 603 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 604 | rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW, |
| 605 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 606 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 607 | } |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 608 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 609 | static int __must_check __deliver_service(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 610 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 611 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
| 612 | struct kvm_s390_ext_info ext; |
| 613 | int rc = 0; |
| 614 | |
| 615 | spin_lock(&fi->lock); |
| 616 | if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) { |
| 617 | spin_unlock(&fi->lock); |
| 618 | return 0; |
| 619 | } |
| 620 | ext = fi->srv_signal; |
| 621 | memset(&fi->srv_signal, 0, sizeof(ext)); |
| 622 | clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs); |
| 623 | spin_unlock(&fi->lock); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 624 | |
| 625 | VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x", |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 626 | ext.ext_params); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 627 | vcpu->stat.deliver_service_signal++; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 628 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE, |
| 629 | ext.ext_params, 0); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 630 | |
| 631 | rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE); |
David Hildenbrand | 467fc29 | 2014-12-01 12:02:44 +0100 | [diff] [blame] | 632 | rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 633 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 634 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
| 635 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 636 | &vcpu->arch.sie_block->gpsw, sizeof(psw_t)); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 637 | rc |= put_guest_lc(vcpu, ext.ext_params, |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 638 | (u32 *)__LC_EXT_PARAMS); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 639 | |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 640 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 641 | } |
| 642 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 643 | static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 644 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 645 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
| 646 | struct kvm_s390_interrupt_info *inti; |
| 647 | int rc = 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 648 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 649 | spin_lock(&fi->lock); |
| 650 | inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT], |
| 651 | struct kvm_s390_interrupt_info, |
| 652 | list); |
| 653 | if (inti) { |
| 654 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
| 655 | KVM_S390_INT_PFAULT_DONE, 0, |
| 656 | inti->ext.ext_params2); |
| 657 | list_del(&inti->list); |
| 658 | fi->counters[FIRQ_CNTR_PFAULT] -= 1; |
| 659 | } |
| 660 | if (list_empty(&fi->lists[FIRQ_LIST_PFAULT])) |
| 661 | clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs); |
| 662 | spin_unlock(&fi->lock); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 663 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 664 | if (inti) { |
| 665 | rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, |
| 666 | (u16 *)__LC_EXT_INT_CODE); |
| 667 | rc |= put_guest_lc(vcpu, PFAULT_DONE, |
| 668 | (u16 *)__LC_EXT_CPU_ADDR); |
| 669 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 670 | &vcpu->arch.sie_block->gpsw, |
| 671 | sizeof(psw_t)); |
| 672 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 673 | &vcpu->arch.sie_block->gpsw, |
| 674 | sizeof(psw_t)); |
| 675 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, |
| 676 | (u64 *)__LC_EXT_PARAMS2); |
| 677 | kfree(inti); |
| 678 | } |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 679 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 682 | static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 683 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 684 | struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int; |
| 685 | struct kvm_s390_interrupt_info *inti; |
| 686 | int rc = 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 687 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 688 | spin_lock(&fi->lock); |
| 689 | inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO], |
| 690 | struct kvm_s390_interrupt_info, |
| 691 | list); |
| 692 | if (inti) { |
| 693 | VCPU_EVENT(vcpu, 4, |
| 694 | "interrupt: virtio parm:%x,parm64:%llx", |
| 695 | inti->ext.ext_params, inti->ext.ext_params2); |
| 696 | vcpu->stat.deliver_virtio_interrupt++; |
| 697 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
| 698 | inti->type, |
| 699 | inti->ext.ext_params, |
| 700 | inti->ext.ext_params2); |
| 701 | list_del(&inti->list); |
| 702 | fi->counters[FIRQ_CNTR_VIRTIO] -= 1; |
| 703 | } |
| 704 | if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO])) |
| 705 | clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs); |
| 706 | spin_unlock(&fi->lock); |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 707 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 708 | if (inti) { |
| 709 | rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, |
| 710 | (u16 *)__LC_EXT_INT_CODE); |
| 711 | rc |= put_guest_lc(vcpu, VIRTIO_PARAM, |
| 712 | (u16 *)__LC_EXT_CPU_ADDR); |
| 713 | rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW, |
| 714 | &vcpu->arch.sie_block->gpsw, |
| 715 | sizeof(psw_t)); |
| 716 | rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, |
| 717 | &vcpu->arch.sie_block->gpsw, |
| 718 | sizeof(psw_t)); |
| 719 | rc |= put_guest_lc(vcpu, inti->ext.ext_params, |
| 720 | (u32 *)__LC_EXT_PARAMS); |
| 721 | rc |= put_guest_lc(vcpu, inti->ext.ext_params2, |
| 722 | (u64 *)__LC_EXT_PARAMS2); |
| 723 | kfree(inti); |
| 724 | } |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 725 | return rc ? -EFAULT : 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static int __must_check __deliver_io(struct kvm_vcpu *vcpu, |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 729 | unsigned long irq_type) |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 730 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 731 | struct list_head *isc_list; |
| 732 | struct kvm_s390_float_interrupt *fi; |
| 733 | struct kvm_s390_interrupt_info *inti = NULL; |
| 734 | int rc = 0; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 735 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 736 | fi = &vcpu->kvm->arch.float_int; |
Jens Freimann | 0fb97ab | 2014-07-29 13:45:21 +0200 | [diff] [blame] | 737 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 738 | spin_lock(&fi->lock); |
| 739 | isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0]; |
| 740 | inti = list_first_entry_or_null(isc_list, |
| 741 | struct kvm_s390_interrupt_info, |
| 742 | list); |
| 743 | if (inti) { |
| 744 | VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type); |
| 745 | vcpu->stat.deliver_io_int++; |
| 746 | trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, |
| 747 | inti->type, |
| 748 | ((__u32)inti->io.subchannel_id << 16) | |
| 749 | inti->io.subchannel_nr, |
| 750 | ((__u64)inti->io.io_int_parm << 32) | |
| 751 | inti->io.io_int_word); |
| 752 | list_del(&inti->list); |
| 753 | fi->counters[FIRQ_CNTR_IO] -= 1; |
| 754 | } |
| 755 | if (list_empty(isc_list)) |
| 756 | clear_bit(irq_type, &fi->pending_irqs); |
| 757 | spin_unlock(&fi->lock); |
David Hildenbrand | 8712836 | 2014-03-03 10:55:13 +0100 | [diff] [blame] | 758 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 759 | if (inti) { |
| 760 | rc = put_guest_lc(vcpu, inti->io.subchannel_id, |
| 761 | (u16 *)__LC_SUBCHANNEL_ID); |
| 762 | rc |= put_guest_lc(vcpu, inti->io.subchannel_nr, |
| 763 | (u16 *)__LC_SUBCHANNEL_NR); |
| 764 | rc |= put_guest_lc(vcpu, inti->io.io_int_parm, |
| 765 | (u32 *)__LC_IO_INT_PARM); |
| 766 | rc |= put_guest_lc(vcpu, inti->io.io_int_word, |
| 767 | (u32 *)__LC_IO_INT_WORD); |
| 768 | rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW, |
| 769 | &vcpu->arch.sie_block->gpsw, |
| 770 | sizeof(psw_t)); |
| 771 | rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW, |
| 772 | &vcpu->arch.sie_block->gpsw, |
| 773 | sizeof(psw_t)); |
| 774 | kfree(inti); |
| 775 | } |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 776 | |
Jens Freimann | 99e2000 | 2014-12-01 17:05:39 +0100 | [diff] [blame] | 777 | return rc ? -EFAULT : 0; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu); |
| 781 | |
| 782 | static const deliver_irq_t deliver_irq_funcs[] = { |
| 783 | [IRQ_PEND_MCHK_EX] = __deliver_machine_check, |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 784 | [IRQ_PEND_MCHK_REP] = __deliver_machine_check, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 785 | [IRQ_PEND_PROG] = __deliver_prog, |
| 786 | [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal, |
| 787 | [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call, |
| 788 | [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc, |
| 789 | [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer, |
| 790 | [IRQ_PEND_RESTART] = __deliver_restart, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 791 | [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix, |
| 792 | [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init, |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 793 | [IRQ_PEND_EXT_SERVICE] = __deliver_service, |
| 794 | [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done, |
| 795 | [IRQ_PEND_VIRTIO] = __deliver_virtio, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 796 | }; |
| 797 | |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 798 | /* Check whether an external call is pending (deliverable or not) */ |
| 799 | int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu) |
David Hildenbrand | 4953919 | 2014-02-21 08:59:59 +0100 | [diff] [blame] | 800 | { |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 801 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 802 | uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl; |
David Hildenbrand | 4953919 | 2014-02-21 08:59:59 +0100 | [diff] [blame] | 803 | |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 804 | if (!sclp_has_sigpif()) |
| 805 | return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs); |
David Hildenbrand | 4953919 | 2014-02-21 08:59:59 +0100 | [diff] [blame] | 806 | |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 807 | return (sigp_ctrl & SIGP_CTRL_C) && |
| 808 | (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND); |
David Hildenbrand | 4953919 | 2014-02-21 08:59:59 +0100 | [diff] [blame] | 809 | } |
| 810 | |
David Hildenbrand | 9a02206 | 2014-08-05 17:40:47 +0200 | [diff] [blame] | 811 | int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 812 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 813 | int rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 814 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 815 | rc = !!deliverable_irqs(vcpu); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 816 | |
David Hildenbrand | bb78c5e | 2014-03-18 10:03:26 +0100 | [diff] [blame] | 817 | if (!rc && kvm_cpu_has_pending_timer(vcpu)) |
| 818 | rc = 1; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 819 | |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 820 | /* external call pending and deliverable */ |
| 821 | if (!rc && kvm_s390_ext_call_pending(vcpu) && |
| 822 | !psw_extint_disabled(vcpu) && |
| 823 | (vcpu->arch.sie_block->gcr[0] & 0x2000ul)) |
David Hildenbrand | 4953919 | 2014-02-21 08:59:59 +0100 | [diff] [blame] | 824 | rc = 1; |
| 825 | |
David Hildenbrand | 9a02206 | 2014-08-05 17:40:47 +0200 | [diff] [blame] | 826 | if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu)) |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 827 | rc = 1; |
| 828 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 829 | return rc; |
| 830 | } |
| 831 | |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 832 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 833 | { |
David Hildenbrand | b4aec92 | 2014-12-01 15:55:42 +0100 | [diff] [blame] | 834 | return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu); |
Marcelo Tosatti | 3d80840 | 2008-04-11 14:53:26 -0300 | [diff] [blame] | 835 | } |
| 836 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 837 | int kvm_s390_handle_wait(struct kvm_vcpu *vcpu) |
| 838 | { |
| 839 | u64 now, sltime; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 840 | |
| 841 | vcpu->stat.exit_wait_state++; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 842 | |
David Hildenbrand | 0759d06 | 2014-05-13 16:54:32 +0200 | [diff] [blame] | 843 | /* fast path */ |
| 844 | if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu)) |
| 845 | return 0; |
Carsten Otte | e52b2af | 2008-05-21 13:37:44 +0200 | [diff] [blame] | 846 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 847 | if (psw_interrupts_disabled(vcpu)) { |
| 848 | VCPU_EVENT(vcpu, 3, "%s", "disabled wait"); |
Heiko Carstens | b8e660b | 2010-02-26 22:37:41 +0100 | [diff] [blame] | 849 | return -EOPNOTSUPP; /* disabled wait */ |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 850 | } |
| 851 | |
David Hildenbrand | bb78c5e | 2014-03-18 10:03:26 +0100 | [diff] [blame] | 852 | if (!ckc_interrupts_enabled(vcpu)) { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 853 | VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer"); |
David Hildenbrand | bda343e | 2014-12-12 12:26:40 +0100 | [diff] [blame] | 854 | __set_cpu_idle(vcpu); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 855 | goto no_timer; |
| 856 | } |
| 857 | |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 858 | now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch; |
Heiko Carstens | ed4f209 | 2013-01-14 16:55:55 +0100 | [diff] [blame] | 859 | sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now); |
David Hildenbrand | bda343e | 2014-12-12 12:26:40 +0100 | [diff] [blame] | 860 | |
| 861 | /* underflow */ |
| 862 | if (vcpu->arch.sie_block->ckc < now) |
| 863 | return 0; |
| 864 | |
| 865 | __set_cpu_idle(vcpu); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 866 | hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL); |
| 867 | VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 868 | no_timer: |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 869 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
David Hildenbrand | 0759d06 | 2014-05-13 16:54:32 +0200 | [diff] [blame] | 870 | kvm_vcpu_block(vcpu); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 871 | __unset_cpu_idle(vcpu); |
Thomas Huth | 800c106 | 2013-09-12 10:33:45 +0200 | [diff] [blame] | 872 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 873 | |
David Hildenbrand | 2d00f75 | 2014-12-11 10:18:01 +0100 | [diff] [blame] | 874 | hrtimer_cancel(&vcpu->arch.ckc_timer); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 875 | return 0; |
| 876 | } |
| 877 | |
David Hildenbrand | 0e9c85a | 2014-05-16 11:59:46 +0200 | [diff] [blame] | 878 | void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu) |
| 879 | { |
| 880 | if (waitqueue_active(&vcpu->wq)) { |
| 881 | /* |
| 882 | * The vcpu gave up the cpu voluntarily, mark it as a good |
| 883 | * yield-candidate. |
| 884 | */ |
| 885 | vcpu->preempted = true; |
| 886 | wake_up_interruptible(&vcpu->wq); |
David Hildenbrand | ce2e4f0 | 2014-07-11 10:00:43 +0200 | [diff] [blame] | 887 | vcpu->stat.halt_wakeup++; |
David Hildenbrand | 0e9c85a | 2014-05-16 11:59:46 +0200 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 891 | enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer) |
| 892 | { |
| 893 | struct kvm_vcpu *vcpu; |
David Hildenbrand | 2d00f75 | 2014-12-11 10:18:01 +0100 | [diff] [blame] | 894 | u64 now, sltime; |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 895 | |
| 896 | vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer); |
David Hildenbrand | 2d00f75 | 2014-12-11 10:18:01 +0100 | [diff] [blame] | 897 | now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch; |
| 898 | sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 899 | |
David Hildenbrand | 2d00f75 | 2014-12-11 10:18:01 +0100 | [diff] [blame] | 900 | /* |
| 901 | * If the monotonic clock runs faster than the tod clock we might be |
| 902 | * woken up too early and have to go back to sleep to avoid deadlocks. |
| 903 | */ |
| 904 | if (vcpu->arch.sie_block->ckc > now && |
| 905 | hrtimer_forward_now(timer, ns_to_ktime(sltime))) |
| 906 | return HRTIMER_RESTART; |
| 907 | kvm_s390_vcpu_wakeup(vcpu); |
Christian Borntraeger | ca87230 | 2009-05-12 17:21:49 +0200 | [diff] [blame] | 908 | return HRTIMER_NORESTART; |
| 909 | } |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 910 | |
Jens Freimann | 2ed10cc | 2014-02-11 13:48:07 +0100 | [diff] [blame] | 911 | void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu) |
| 912 | { |
| 913 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 2ed10cc | 2014-02-11 13:48:07 +0100 | [diff] [blame] | 914 | |
David Hildenbrand | 4ae3c08 | 2014-05-16 10:23:53 +0200 | [diff] [blame] | 915 | spin_lock(&li->lock); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 916 | li->pending_irqs = 0; |
| 917 | bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS); |
| 918 | memset(&li->irq, 0, sizeof(li->irq)); |
David Hildenbrand | 4ae3c08 | 2014-05-16 10:23:53 +0200 | [diff] [blame] | 919 | spin_unlock(&li->lock); |
David Hildenbrand | 4953919 | 2014-02-21 08:59:59 +0100 | [diff] [blame] | 920 | |
| 921 | /* clear pending external calls set by sigp interpretation facility */ |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 922 | atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags); |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 923 | vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0; |
Jens Freimann | 2ed10cc | 2014-02-11 13:48:07 +0100 | [diff] [blame] | 924 | } |
| 925 | |
Christian Borntraeger | 614aeab | 2014-08-25 12:27:29 +0200 | [diff] [blame] | 926 | int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 927 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 928 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 929 | deliver_irq_t func; |
Jens Freimann | 7939503 | 2014-04-17 10:10:30 +0200 | [diff] [blame] | 930 | int rc = 0; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 931 | unsigned long irq_type; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 932 | unsigned long irqs; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 933 | |
| 934 | __reset_intercept_indicators(vcpu); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 935 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 936 | /* pending ckc conditions might have been invalidated */ |
| 937 | clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); |
David Hildenbrand | b4aec92 | 2014-12-01 15:55:42 +0100 | [diff] [blame] | 938 | if (ckc_irq_pending(vcpu)) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 939 | set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); |
| 940 | |
David Hildenbrand | b4aec92 | 2014-12-01 15:55:42 +0100 | [diff] [blame] | 941 | /* pending cpu timer conditions might have been invalidated */ |
| 942 | clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); |
| 943 | if (cpu_timer_irq_pending(vcpu)) |
| 944 | set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); |
| 945 | |
Jens Freimann | ffeca0a | 2015-04-17 10:21:04 +0200 | [diff] [blame] | 946 | while ((irqs = deliverable_irqs(vcpu)) && !rc) { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 947 | /* bits are in the order of interrupt priority */ |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 948 | irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 949 | if (is_ioirq(irq_type)) { |
| 950 | rc = __deliver_io(vcpu, irq_type); |
| 951 | } else { |
| 952 | func = deliver_irq_funcs[irq_type]; |
| 953 | if (!func) { |
| 954 | WARN_ON_ONCE(func == NULL); |
| 955 | clear_bit(irq_type, &li->pending_irqs); |
| 956 | continue; |
| 957 | } |
| 958 | rc = func(vcpu); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 959 | } |
Jens Freimann | ffeca0a | 2015-04-17 10:21:04 +0200 | [diff] [blame] | 960 | } |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 961 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 962 | set_intercept_indicators(vcpu); |
Jens Freimann | 7939503 | 2014-04-17 10:10:30 +0200 | [diff] [blame] | 963 | |
| 964 | return rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 965 | } |
| 966 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 967 | static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 968 | { |
| 969 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 970 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 971 | li->irq.pgm = irq->u.pgm; |
Jens Freimann | 9185124 | 2014-12-01 16:43:40 +0100 | [diff] [blame] | 972 | set_bit(IRQ_PEND_PROG, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 976 | int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code) |
| 977 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 978 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 979 | struct kvm_s390_irq irq; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 980 | |
| 981 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 982 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, code, |
| 983 | 0, 1); |
David Hildenbrand | 4ae3c08 | 2014-05-16 10:23:53 +0200 | [diff] [blame] | 984 | spin_lock(&li->lock); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 985 | irq.u.pgm.code = code; |
| 986 | __inject_prog(vcpu, &irq); |
Christian Borntraeger | d0321a2 | 2013-06-12 13:54:55 +0200 | [diff] [blame] | 987 | BUG_ON(waitqueue_active(li->wq)); |
David Hildenbrand | 4ae3c08 | 2014-05-16 10:23:53 +0200 | [diff] [blame] | 988 | spin_unlock(&li->lock); |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
Jens Freimann | bcd8468 | 2014-02-11 11:07:05 +0100 | [diff] [blame] | 992 | int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu, |
| 993 | struct kvm_s390_pgm_info *pgm_info) |
| 994 | { |
| 995 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 996 | struct kvm_s390_irq irq; |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 997 | int rc; |
Jens Freimann | bcd8468 | 2014-02-11 11:07:05 +0100 | [diff] [blame] | 998 | |
Jens Freimann | bcd8468 | 2014-02-11 11:07:05 +0100 | [diff] [blame] | 999 | VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)", |
| 1000 | pgm_info->code); |
| 1001 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, |
| 1002 | pgm_info->code, 0, 1); |
David Hildenbrand | 4ae3c08 | 2014-05-16 10:23:53 +0200 | [diff] [blame] | 1003 | spin_lock(&li->lock); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1004 | irq.u.pgm = *pgm_info; |
| 1005 | rc = __inject_prog(vcpu, &irq); |
Jens Freimann | bcd8468 | 2014-02-11 11:07:05 +0100 | [diff] [blame] | 1006 | BUG_ON(waitqueue_active(li->wq)); |
David Hildenbrand | 4ae3c08 | 2014-05-16 10:23:53 +0200 | [diff] [blame] | 1007 | spin_unlock(&li->lock); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1008 | return rc; |
| 1009 | } |
| 1010 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1011 | static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1012 | { |
| 1013 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1014 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1015 | VCPU_EVENT(vcpu, 3, "inject: external irq params:%x, params2:%llx", |
| 1016 | irq->u.ext.ext_params, irq->u.ext.ext_params2); |
| 1017 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT, |
| 1018 | irq->u.ext.ext_params, |
| 1019 | irq->u.ext.ext_params2, 2); |
| 1020 | |
| 1021 | li->irq.ext = irq->u.ext; |
| 1022 | set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1023 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 1027 | static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id) |
| 1028 | { |
| 1029 | unsigned char new_val, old_val; |
| 1030 | uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl; |
| 1031 | |
| 1032 | new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK); |
| 1033 | old_val = *sigp_ctrl & ~SIGP_CTRL_C; |
| 1034 | if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) { |
| 1035 | /* another external call is pending */ |
| 1036 | return -EBUSY; |
| 1037 | } |
| 1038 | atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags); |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Christian Borntraeger | 0675d92 | 2015-01-15 12:40:42 +0100 | [diff] [blame] | 1042 | static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1043 | { |
| 1044 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1045 | struct kvm_s390_extcall_info *extcall = &li->irq.extcall; |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 1046 | uint16_t src_id = irq->u.extcall.code; |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1047 | |
| 1048 | VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u", |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 1049 | src_id); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1050 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL, |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 1051 | src_id, 0, 2); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1052 | |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 1053 | /* sending vcpu invalid */ |
| 1054 | if (src_id >= KVM_MAX_VCPUS || |
| 1055 | kvm_get_vcpu(vcpu->kvm, src_id) == NULL) |
| 1056 | return -EINVAL; |
| 1057 | |
| 1058 | if (sclp_has_sigpif()) |
| 1059 | return __inject_extcall_sigpif(vcpu, src_id); |
| 1060 | |
David Hildenbrand | b938eace | 2015-04-30 13:33:59 +0200 | [diff] [blame] | 1061 | if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs)) |
David Hildenbrand | ea5f496 | 2014-10-14 15:29:30 +0200 | [diff] [blame] | 1062 | return -EBUSY; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1063 | *extcall = irq->u.extcall; |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1064 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1068 | static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1069 | { |
| 1070 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1071 | struct kvm_s390_prefix_info *prefix = &li->irq.prefix; |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1072 | |
| 1073 | VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)", |
Jens Freimann | 556cc0d | 2014-12-18 15:52:21 +0100 | [diff] [blame] | 1074 | irq->u.prefix.address); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1075 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX, |
Jens Freimann | 556cc0d | 2014-12-18 15:52:21 +0100 | [diff] [blame] | 1076 | irq->u.prefix.address, 0, 2); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1077 | |
David Hildenbrand | a3a9c59 | 2014-10-14 09:44:55 +0200 | [diff] [blame] | 1078 | if (!is_vcpu_stopped(vcpu)) |
| 1079 | return -EBUSY; |
| 1080 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1081 | *prefix = irq->u.prefix; |
| 1082 | set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1083 | return 0; |
| 1084 | } |
| 1085 | |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 1086 | #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1087 | static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1088 | { |
| 1089 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
David Hildenbrand | 2822545 | 2014-10-15 16:48:16 +0200 | [diff] [blame] | 1090 | struct kvm_s390_stop_info *stop = &li->irq.stop; |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 1091 | int rc = 0; |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1092 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1093 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0, 2); |
| 1094 | |
David Hildenbrand | 2822545 | 2014-10-15 16:48:16 +0200 | [diff] [blame] | 1095 | if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS) |
| 1096 | return -EINVAL; |
| 1097 | |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 1098 | if (is_vcpu_stopped(vcpu)) { |
| 1099 | if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS) |
| 1100 | rc = kvm_s390_store_status_unloaded(vcpu, |
| 1101 | KVM_S390_STORE_STATUS_NOADDR); |
| 1102 | return rc; |
| 1103 | } |
| 1104 | |
| 1105 | if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs)) |
| 1106 | return -EBUSY; |
David Hildenbrand | 2822545 | 2014-10-15 16:48:16 +0200 | [diff] [blame] | 1107 | stop->flags = irq->u.stop.flags; |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 1108 | __set_cpuflag(vcpu, CPUSTAT_STOP_INT); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static int __inject_sigp_restart(struct kvm_vcpu *vcpu, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1113 | struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1114 | { |
| 1115 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1116 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1117 | VCPU_EVENT(vcpu, 3, "inject: restart type %llx", irq->type); |
| 1118 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0, 2); |
| 1119 | |
| 1120 | set_bit(IRQ_PEND_RESTART, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | static int __inject_sigp_emergency(struct kvm_vcpu *vcpu, |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1125 | struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1126 | { |
| 1127 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1128 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1129 | VCPU_EVENT(vcpu, 3, "inject: emergency %u\n", |
| 1130 | irq->u.emerg.code); |
| 1131 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY, |
Jens Freimann | 49538d1 | 2014-12-18 15:48:14 +0100 | [diff] [blame] | 1132 | irq->u.emerg.code, 0, 2); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1133 | |
Jens Freimann | 49538d1 | 2014-12-18 15:48:14 +0100 | [diff] [blame] | 1134 | set_bit(irq->u.emerg.code, li->sigp_emerg_pending); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1135 | set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1136 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1140 | static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1141 | { |
| 1142 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1143 | struct kvm_s390_mchk_info *mchk = &li->irq.mchk; |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1144 | |
| 1145 | VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx", |
Jens Freimann | 556cc0d | 2014-12-18 15:52:21 +0100 | [diff] [blame] | 1146 | irq->u.mchk.mcic); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1147 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0, |
Jens Freimann | 556cc0d | 2014-12-18 15:52:21 +0100 | [diff] [blame] | 1148 | irq->u.mchk.mcic, 2); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1149 | |
| 1150 | /* |
Jens Freimann | fc2020c | 2014-08-13 10:09:04 +0200 | [diff] [blame] | 1151 | * Because repressible machine checks can be indicated along with |
| 1152 | * exigent machine checks (PoP, Chapter 11, Interruption action) |
| 1153 | * we need to combine cr14, mcic and external damage code. |
| 1154 | * Failing storage address and the logout area should not be or'ed |
| 1155 | * together, we just indicate the last occurrence of the corresponding |
| 1156 | * machine check |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1157 | */ |
Jens Freimann | fc2020c | 2014-08-13 10:09:04 +0200 | [diff] [blame] | 1158 | mchk->cr14 |= irq->u.mchk.cr14; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1159 | mchk->mcic |= irq->u.mchk.mcic; |
Jens Freimann | fc2020c | 2014-08-13 10:09:04 +0200 | [diff] [blame] | 1160 | mchk->ext_damage_code |= irq->u.mchk.ext_damage_code; |
| 1161 | mchk->failing_storage_address = irq->u.mchk.failing_storage_address; |
| 1162 | memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout, |
| 1163 | sizeof(mchk->fixed_logout)); |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1164 | if (mchk->mcic & MCHK_EX_MASK) |
| 1165 | set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs); |
| 1166 | else if (mchk->mcic & MCHK_REP_MASK) |
| 1167 | set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
| 1170 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1171 | static int __inject_ckc(struct kvm_vcpu *vcpu) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1172 | { |
| 1173 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1174 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1175 | VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CLOCK_COMP); |
| 1176 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP, |
| 1177 | 0, 0, 2); |
| 1178 | |
| 1179 | set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1180 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1184 | static int __inject_cpu_timer(struct kvm_vcpu *vcpu) |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1185 | { |
| 1186 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1187 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1188 | VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CPU_TIMER); |
| 1189 | trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER, |
| 1190 | 0, 0, 2); |
| 1191 | |
| 1192 | set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1193 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
Jens Freimann | bcd8468 | 2014-02-11 11:07:05 +0100 | [diff] [blame] | 1194 | return 0; |
| 1195 | } |
| 1196 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1197 | static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm, |
| 1198 | int isc, u32 schid) |
| 1199 | { |
| 1200 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; |
| 1201 | struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc]; |
| 1202 | struct kvm_s390_interrupt_info *iter; |
| 1203 | u16 id = (schid & 0xffff0000U) >> 16; |
| 1204 | u16 nr = schid & 0x0000ffffU; |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1205 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1206 | spin_lock(&fi->lock); |
| 1207 | list_for_each_entry(iter, isc_list, list) { |
| 1208 | if (schid && (id != iter->io.subchannel_id || |
| 1209 | nr != iter->io.subchannel_nr)) |
| 1210 | continue; |
| 1211 | /* found an appropriate entry */ |
| 1212 | list_del_init(&iter->list); |
| 1213 | fi->counters[FIRQ_CNTR_IO] -= 1; |
| 1214 | if (list_empty(isc_list)) |
| 1215 | clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs); |
| 1216 | spin_unlock(&fi->lock); |
| 1217 | return iter; |
| 1218 | } |
| 1219 | spin_unlock(&fi->lock); |
| 1220 | return NULL; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * Dequeue and return an I/O interrupt matching any of the interruption |
| 1225 | * subclasses as designated by the isc mask in cr6 and the schid (if != 0). |
| 1226 | */ |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 1227 | struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm, |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1228 | u64 isc_mask, u32 schid) |
| 1229 | { |
| 1230 | struct kvm_s390_interrupt_info *inti = NULL; |
| 1231 | int isc; |
| 1232 | |
| 1233 | for (isc = 0; isc <= MAX_ISC && !inti; isc++) { |
| 1234 | if (isc_mask & isc_to_isc_bits(isc)) |
| 1235 | inti = get_io_int(kvm, isc, schid); |
| 1236 | } |
| 1237 | return inti; |
| 1238 | } |
| 1239 | |
| 1240 | #define SCCB_MASK 0xFFFFFFF8 |
| 1241 | #define SCCB_EVENT_PENDING 0x3 |
| 1242 | |
| 1243 | static int __inject_service(struct kvm *kvm, |
| 1244 | struct kvm_s390_interrupt_info *inti) |
| 1245 | { |
| 1246 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; |
| 1247 | |
| 1248 | spin_lock(&fi->lock); |
| 1249 | fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING; |
| 1250 | /* |
| 1251 | * Early versions of the QEMU s390 bios will inject several |
| 1252 | * service interrupts after another without handling a |
| 1253 | * condition code indicating busy. |
| 1254 | * We will silently ignore those superfluous sccb values. |
| 1255 | * A future version of QEMU will take care of serialization |
| 1256 | * of servc requests |
| 1257 | */ |
| 1258 | if (fi->srv_signal.ext_params & SCCB_MASK) |
| 1259 | goto out; |
| 1260 | fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK; |
| 1261 | set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs); |
| 1262 | out: |
| 1263 | spin_unlock(&fi->lock); |
| 1264 | kfree(inti); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | static int __inject_virtio(struct kvm *kvm, |
| 1269 | struct kvm_s390_interrupt_info *inti) |
| 1270 | { |
| 1271 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; |
| 1272 | |
| 1273 | spin_lock(&fi->lock); |
| 1274 | if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) { |
| 1275 | spin_unlock(&fi->lock); |
| 1276 | return -EBUSY; |
| 1277 | } |
| 1278 | fi->counters[FIRQ_CNTR_VIRTIO] += 1; |
| 1279 | list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]); |
| 1280 | set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs); |
| 1281 | spin_unlock(&fi->lock); |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | static int __inject_pfault_done(struct kvm *kvm, |
| 1286 | struct kvm_s390_interrupt_info *inti) |
| 1287 | { |
| 1288 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; |
| 1289 | |
| 1290 | spin_lock(&fi->lock); |
| 1291 | if (fi->counters[FIRQ_CNTR_PFAULT] >= |
| 1292 | (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) { |
| 1293 | spin_unlock(&fi->lock); |
| 1294 | return -EBUSY; |
| 1295 | } |
| 1296 | fi->counters[FIRQ_CNTR_PFAULT] += 1; |
| 1297 | list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]); |
| 1298 | set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs); |
| 1299 | spin_unlock(&fi->lock); |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | #define CR_PENDING_SUBCLASS 28 |
| 1304 | static int __inject_float_mchk(struct kvm *kvm, |
| 1305 | struct kvm_s390_interrupt_info *inti) |
| 1306 | { |
| 1307 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; |
| 1308 | |
| 1309 | spin_lock(&fi->lock); |
| 1310 | fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS); |
| 1311 | fi->mchk.mcic |= inti->mchk.mcic; |
| 1312 | set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs); |
| 1313 | spin_unlock(&fi->lock); |
| 1314 | kfree(inti); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti) |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 1319 | { |
| 1320 | struct kvm_s390_float_interrupt *fi; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1321 | struct list_head *list; |
| 1322 | int isc; |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 1323 | |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 1324 | fi = &kvm->arch.float_int; |
| 1325 | spin_lock(&fi->lock); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1326 | if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) { |
| 1327 | spin_unlock(&fi->lock); |
| 1328 | return -EBUSY; |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 1329 | } |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1330 | fi->counters[FIRQ_CNTR_IO] += 1; |
| 1331 | |
| 1332 | isc = int_word_to_isc(inti->io.io_int_word); |
| 1333 | list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc]; |
| 1334 | list_add_tail(&inti->list, list); |
| 1335 | set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs); |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 1336 | spin_unlock(&fi->lock); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1337 | return 0; |
Cornelia Huck | fa6b7fe | 2012-12-20 15:32:12 +0100 | [diff] [blame] | 1338 | } |
| 1339 | |
David Hildenbrand | 96e0ed2 | 2015-01-14 14:08:38 +0100 | [diff] [blame] | 1340 | /* |
| 1341 | * Find a destination VCPU for a floating irq and kick it. |
| 1342 | */ |
| 1343 | static void __floating_irq_kick(struct kvm *kvm, u64 type) |
| 1344 | { |
| 1345 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; |
| 1346 | struct kvm_s390_local_interrupt *li; |
| 1347 | struct kvm_vcpu *dst_vcpu; |
| 1348 | int sigcpu, online_vcpus, nr_tries = 0; |
| 1349 | |
| 1350 | online_vcpus = atomic_read(&kvm->online_vcpus); |
| 1351 | if (!online_vcpus) |
| 1352 | return; |
| 1353 | |
| 1354 | /* find idle VCPUs first, then round robin */ |
| 1355 | sigcpu = find_first_bit(fi->idle_mask, online_vcpus); |
| 1356 | if (sigcpu == online_vcpus) { |
| 1357 | do { |
| 1358 | sigcpu = fi->next_rr_cpu; |
| 1359 | fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus; |
| 1360 | /* avoid endless loops if all vcpus are stopped */ |
| 1361 | if (nr_tries++ >= online_vcpus) |
| 1362 | return; |
| 1363 | } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu))); |
| 1364 | } |
| 1365 | dst_vcpu = kvm_get_vcpu(kvm, sigcpu); |
| 1366 | |
| 1367 | /* make the VCPU drop out of the SIE, or wake it up if sleeping */ |
| 1368 | li = &dst_vcpu->arch.local_int; |
| 1369 | spin_lock(&li->lock); |
| 1370 | switch (type) { |
| 1371 | case KVM_S390_MCHK: |
| 1372 | atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags); |
| 1373 | break; |
| 1374 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 1375 | atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags); |
| 1376 | break; |
| 1377 | default: |
| 1378 | atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags); |
| 1379 | break; |
| 1380 | } |
| 1381 | spin_unlock(&li->lock); |
| 1382 | kvm_s390_vcpu_wakeup(dst_vcpu); |
| 1383 | } |
| 1384 | |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 1385 | 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] | 1386 | { |
Christian Borntraeger | 180c12f | 2008-06-27 15:05:40 +0200 | [diff] [blame] | 1387 | struct kvm_s390_float_interrupt *fi; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1388 | u64 type = READ_ONCE(inti->type); |
| 1389 | int rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1390 | |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1391 | fi = &kvm->arch.float_int; |
Cornelia Huck | 79fd50c | 2013-02-07 13:20:52 +0100 | [diff] [blame] | 1392 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1393 | switch (type) { |
| 1394 | case KVM_S390_MCHK: |
| 1395 | rc = __inject_float_mchk(kvm, inti); |
| 1396 | break; |
| 1397 | case KVM_S390_INT_VIRTIO: |
| 1398 | rc = __inject_virtio(kvm, inti); |
| 1399 | break; |
| 1400 | case KVM_S390_INT_SERVICE: |
| 1401 | rc = __inject_service(kvm, inti); |
| 1402 | break; |
| 1403 | case KVM_S390_INT_PFAULT_DONE: |
| 1404 | rc = __inject_pfault_done(kvm, inti); |
| 1405 | break; |
| 1406 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 1407 | rc = __inject_io(kvm, inti); |
| 1408 | break; |
| 1409 | default: |
| 1410 | rc = -EINVAL; |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 1411 | } |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1412 | if (rc) |
| 1413 | return rc; |
| 1414 | |
David Hildenbrand | 96e0ed2 | 2015-01-14 14:08:38 +0100 | [diff] [blame] | 1415 | __floating_irq_kick(kvm, type); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1416 | return 0; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | int kvm_s390_inject_vm(struct kvm *kvm, |
| 1420 | struct kvm_s390_interrupt *s390int) |
| 1421 | { |
| 1422 | struct kvm_s390_interrupt_info *inti; |
David Hildenbrand | 428d53b | 2015-01-16 12:58:09 +0100 | [diff] [blame] | 1423 | int rc; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1424 | |
| 1425 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 1426 | if (!inti) |
| 1427 | return -ENOMEM; |
| 1428 | |
| 1429 | inti->type = s390int->type; |
| 1430 | switch (inti->type) { |
| 1431 | case KVM_S390_INT_VIRTIO: |
| 1432 | VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx", |
| 1433 | s390int->parm, s390int->parm64); |
| 1434 | inti->ext.ext_params = s390int->parm; |
| 1435 | inti->ext.ext_params2 = s390int->parm64; |
| 1436 | break; |
| 1437 | case KVM_S390_INT_SERVICE: |
| 1438 | VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm); |
| 1439 | inti->ext.ext_params = s390int->parm; |
| 1440 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1441 | case KVM_S390_INT_PFAULT_DONE: |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1442 | inti->ext.ext_params2 = s390int->parm64; |
| 1443 | break; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1444 | case KVM_S390_MCHK: |
| 1445 | VM_EVENT(kvm, 5, "inject: machine check parm64:%llx", |
| 1446 | s390int->parm64); |
| 1447 | inti->mchk.cr14 = s390int->parm; /* upper bits are not used */ |
| 1448 | inti->mchk.mcic = s390int->parm64; |
| 1449 | break; |
| 1450 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 1451 | if (inti->type & IOINT_AI_MASK) |
| 1452 | VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)"); |
| 1453 | else |
| 1454 | VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x", |
| 1455 | s390int->type & IOINT_CSSID_MASK, |
| 1456 | s390int->type & IOINT_SSID_MASK, |
| 1457 | s390int->type & IOINT_SCHID_MASK); |
| 1458 | inti->io.subchannel_id = s390int->parm >> 16; |
| 1459 | inti->io.subchannel_nr = s390int->parm & 0x0000ffffu; |
| 1460 | inti->io.io_int_parm = s390int->parm64 >> 32; |
| 1461 | inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull; |
| 1462 | break; |
| 1463 | default: |
| 1464 | kfree(inti); |
| 1465 | return -EINVAL; |
| 1466 | } |
| 1467 | trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64, |
| 1468 | 2); |
| 1469 | |
David Hildenbrand | 428d53b | 2015-01-16 12:58:09 +0100 | [diff] [blame] | 1470 | rc = __inject_vm(kvm, inti); |
| 1471 | if (rc) |
| 1472 | kfree(inti); |
| 1473 | return rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
David Hildenbrand | 15462e3 | 2015-02-04 15:59:11 +0100 | [diff] [blame] | 1476 | int kvm_s390_reinject_io_int(struct kvm *kvm, |
Cornelia Huck | 2f32d4e | 2014-01-08 18:07:54 +0100 | [diff] [blame] | 1477 | struct kvm_s390_interrupt_info *inti) |
| 1478 | { |
David Hildenbrand | 15462e3 | 2015-02-04 15:59:11 +0100 | [diff] [blame] | 1479 | return __inject_vm(kvm, inti); |
Cornelia Huck | 2f32d4e | 2014-01-08 18:07:54 +0100 | [diff] [blame] | 1480 | } |
| 1481 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1482 | int s390int_to_s390irq(struct kvm_s390_interrupt *s390int, |
| 1483 | struct kvm_s390_irq *irq) |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1484 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1485 | irq->type = s390int->type; |
| 1486 | switch (irq->type) { |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1487 | case KVM_S390_PROGRAM_INT: |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1488 | if (s390int->parm & 0xffff0000) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1489 | return -EINVAL; |
| 1490 | irq->u.pgm.code = s390int->parm; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1491 | break; |
Christian Borntraeger | b7e6e4d | 2009-01-22 10:29:08 +0100 | [diff] [blame] | 1492 | case KVM_S390_SIGP_SET_PREFIX: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1493 | irq->u.prefix.address = s390int->parm; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1494 | break; |
David Hildenbrand | 2822545 | 2014-10-15 16:48:16 +0200 | [diff] [blame] | 1495 | case KVM_S390_SIGP_STOP: |
| 1496 | irq->u.stop.flags = s390int->parm; |
| 1497 | break; |
Jason J. Herne | 82a1273 | 2012-10-02 16:25:36 +0200 | [diff] [blame] | 1498 | case KVM_S390_INT_EXTERNAL_CALL: |
Jens Freimann | 94d1f56 | 2015-01-15 14:40:34 +0100 | [diff] [blame] | 1499 | if (s390int->parm & 0xffff0000) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1500 | return -EINVAL; |
| 1501 | irq->u.extcall.code = s390int->parm; |
Jason J. Herne | 82a1273 | 2012-10-02 16:25:36 +0200 | [diff] [blame] | 1502 | break; |
| 1503 | case KVM_S390_INT_EMERGENCY: |
Jens Freimann | 94d1f56 | 2015-01-15 14:40:34 +0100 | [diff] [blame] | 1504 | if (s390int->parm & 0xffff0000) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1505 | return -EINVAL; |
| 1506 | irq->u.emerg.code = s390int->parm; |
Jason J. Herne | 82a1273 | 2012-10-02 16:25:36 +0200 | [diff] [blame] | 1507 | break; |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 1508 | case KVM_S390_MCHK: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1509 | irq->u.mchk.mcic = s390int->parm64; |
| 1510 | break; |
| 1511 | } |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
David Hildenbrand | 6cddd43 | 2014-10-15 16:48:53 +0200 | [diff] [blame] | 1515 | int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu) |
| 1516 | { |
| 1517 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1518 | |
| 1519 | return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs); |
| 1520 | } |
| 1521 | |
| 1522 | void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu) |
| 1523 | { |
| 1524 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1525 | |
| 1526 | spin_lock(&li->lock); |
| 1527 | li->irq.stop.flags = 0; |
| 1528 | clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs); |
| 1529 | spin_unlock(&li->lock); |
| 1530 | } |
| 1531 | |
Jens Freimann | 79e87a1 | 2015-03-19 15:12:12 +0100 | [diff] [blame] | 1532 | static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1533 | { |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1534 | int rc; |
| 1535 | |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1536 | switch (irq->type) { |
| 1537 | case KVM_S390_PROGRAM_INT: |
| 1538 | VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)", |
| 1539 | irq->u.pgm.code); |
| 1540 | rc = __inject_prog(vcpu, irq); |
| 1541 | break; |
| 1542 | case KVM_S390_SIGP_SET_PREFIX: |
| 1543 | rc = __inject_set_prefix(vcpu, irq); |
| 1544 | break; |
| 1545 | case KVM_S390_SIGP_STOP: |
| 1546 | rc = __inject_sigp_stop(vcpu, irq); |
| 1547 | break; |
| 1548 | case KVM_S390_RESTART: |
| 1549 | rc = __inject_sigp_restart(vcpu, irq); |
| 1550 | break; |
| 1551 | case KVM_S390_INT_CLOCK_COMP: |
| 1552 | rc = __inject_ckc(vcpu); |
| 1553 | break; |
| 1554 | case KVM_S390_INT_CPU_TIMER: |
| 1555 | rc = __inject_cpu_timer(vcpu); |
| 1556 | break; |
| 1557 | case KVM_S390_INT_EXTERNAL_CALL: |
| 1558 | rc = __inject_extcall(vcpu, irq); |
| 1559 | break; |
| 1560 | case KVM_S390_INT_EMERGENCY: |
| 1561 | rc = __inject_sigp_emergency(vcpu, irq); |
| 1562 | break; |
| 1563 | case KVM_S390_MCHK: |
| 1564 | rc = __inject_mchk(vcpu, irq); |
Cornelia Huck | 48a3e95 | 2012-12-20 15:32:09 +0100 | [diff] [blame] | 1565 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1566 | case KVM_S390_INT_PFAULT_INIT: |
Jens Freimann | 383d0b0 | 2014-07-29 15:11:49 +0200 | [diff] [blame] | 1567 | rc = __inject_pfault_init(vcpu, irq); |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1568 | break; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1569 | case KVM_S390_INT_VIRTIO: |
| 1570 | case KVM_S390_INT_SERVICE: |
Cornelia Huck | d8346b7 | 2012-12-20 15:32:08 +0100 | [diff] [blame] | 1571 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1572 | default: |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1573 | rc = -EINVAL; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1574 | } |
Jens Freimann | 79e87a1 | 2015-03-19 15:12:12 +0100 | [diff] [blame] | 1575 | |
| 1576 | return rc; |
| 1577 | } |
| 1578 | |
| 1579 | int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq) |
| 1580 | { |
| 1581 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 1582 | int rc; |
| 1583 | |
| 1584 | spin_lock(&li->lock); |
| 1585 | rc = do_inject_vcpu(vcpu, irq); |
David Hildenbrand | 4ae3c08 | 2014-05-16 10:23:53 +0200 | [diff] [blame] | 1586 | spin_unlock(&li->lock); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1587 | if (!rc) |
| 1588 | kvm_s390_vcpu_wakeup(vcpu); |
Jens Freimann | 0146a7b | 2014-07-28 15:37:58 +0200 | [diff] [blame] | 1589 | return rc; |
Carsten Otte | ba5c1e9 | 2008-03-25 18:47:26 +0100 | [diff] [blame] | 1590 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1591 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1592 | static inline void clear_irq_list(struct list_head *_list) |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1593 | { |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1594 | struct kvm_s390_interrupt_info *inti, *n; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1595 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1596 | list_for_each_entry_safe(inti, n, _list, list) { |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1597 | list_del(&inti->list); |
| 1598 | kfree(inti); |
| 1599 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1600 | } |
| 1601 | |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1602 | static void inti_to_irq(struct kvm_s390_interrupt_info *inti, |
| 1603 | struct kvm_s390_irq *irq) |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1604 | { |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1605 | irq->type = inti->type; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1606 | switch (inti->type) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1607 | case KVM_S390_INT_PFAULT_INIT: |
| 1608 | case KVM_S390_INT_PFAULT_DONE: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1609 | case KVM_S390_INT_VIRTIO: |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1610 | irq->u.ext = inti->ext; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1611 | break; |
| 1612 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1613 | irq->u.io = inti->io; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1614 | break; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1615 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1616 | } |
| 1617 | |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1618 | void kvm_s390_clear_float_irqs(struct kvm *kvm) |
| 1619 | { |
| 1620 | struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int; |
| 1621 | int i; |
| 1622 | |
| 1623 | spin_lock(&fi->lock); |
Jens Freimann | f2ae45e | 2015-06-22 13:20:12 +0200 | [diff] [blame^] | 1624 | fi->pending_irqs = 0; |
| 1625 | memset(&fi->srv_signal, 0, sizeof(fi->srv_signal)); |
| 1626 | memset(&fi->mchk, 0, sizeof(fi->mchk)); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1627 | for (i = 0; i < FIRQ_LIST_COUNT; i++) |
| 1628 | clear_irq_list(&fi->lists[i]); |
| 1629 | for (i = 0; i < FIRQ_MAX_COUNT; i++) |
| 1630 | fi->counters[i] = 0; |
| 1631 | spin_unlock(&fi->lock); |
| 1632 | }; |
| 1633 | |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1634 | static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len) |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1635 | { |
| 1636 | struct kvm_s390_interrupt_info *inti; |
| 1637 | struct kvm_s390_float_interrupt *fi; |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1638 | struct kvm_s390_irq *buf; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1639 | struct kvm_s390_irq *irq; |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1640 | int max_irqs; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1641 | int ret = 0; |
| 1642 | int n = 0; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1643 | int i; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1644 | |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1645 | if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0) |
| 1646 | return -EINVAL; |
| 1647 | |
| 1648 | /* |
| 1649 | * We are already using -ENOMEM to signal |
| 1650 | * userspace it may retry with a bigger buffer, |
| 1651 | * so we need to use something else for this case |
| 1652 | */ |
| 1653 | buf = vzalloc(len); |
| 1654 | if (!buf) |
| 1655 | return -ENOBUFS; |
| 1656 | |
| 1657 | max_irqs = len / sizeof(struct kvm_s390_irq); |
| 1658 | |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1659 | fi = &kvm->arch.float_int; |
| 1660 | spin_lock(&fi->lock); |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1661 | for (i = 0; i < FIRQ_LIST_COUNT; i++) { |
| 1662 | list_for_each_entry(inti, &fi->lists[i], list) { |
| 1663 | if (n == max_irqs) { |
| 1664 | /* signal userspace to try again */ |
| 1665 | ret = -ENOMEM; |
| 1666 | goto out; |
| 1667 | } |
| 1668 | inti_to_irq(inti, &buf[n]); |
| 1669 | n++; |
| 1670 | } |
| 1671 | } |
| 1672 | if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) { |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1673 | if (n == max_irqs) { |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1674 | /* signal userspace to try again */ |
| 1675 | ret = -ENOMEM; |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1676 | goto out; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1677 | } |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1678 | irq = (struct kvm_s390_irq *) &buf[n]; |
| 1679 | irq->type = KVM_S390_INT_SERVICE; |
| 1680 | irq->u.ext = fi->srv_signal; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1681 | n++; |
| 1682 | } |
Jens Freimann | 6d3da24 | 2013-07-03 15:18:35 +0200 | [diff] [blame] | 1683 | if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) { |
| 1684 | if (n == max_irqs) { |
| 1685 | /* signal userspace to try again */ |
| 1686 | ret = -ENOMEM; |
| 1687 | goto out; |
| 1688 | } |
| 1689 | irq = (struct kvm_s390_irq *) &buf[n]; |
| 1690 | irq->type = KVM_S390_MCHK; |
| 1691 | irq->u.mchk = fi->mchk; |
| 1692 | n++; |
| 1693 | } |
| 1694 | |
| 1695 | out: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1696 | spin_unlock(&fi->lock); |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1697 | if (!ret && n > 0) { |
| 1698 | if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n)) |
| 1699 | ret = -EFAULT; |
| 1700 | } |
| 1701 | vfree(buf); |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1702 | |
| 1703 | return ret < 0 ? ret : n; |
| 1704 | } |
| 1705 | |
| 1706 | static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
| 1707 | { |
| 1708 | int r; |
| 1709 | |
| 1710 | switch (attr->group) { |
| 1711 | case KVM_DEV_FLIC_GET_ALL_IRQS: |
Jens Freimann | 94aa033 | 2015-03-16 12:17:13 +0100 | [diff] [blame] | 1712 | r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr, |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1713 | attr->attr); |
| 1714 | break; |
| 1715 | default: |
| 1716 | r = -EINVAL; |
| 1717 | } |
| 1718 | |
| 1719 | return r; |
| 1720 | } |
| 1721 | |
| 1722 | static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti, |
| 1723 | u64 addr) |
| 1724 | { |
| 1725 | struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr; |
| 1726 | void *target = NULL; |
| 1727 | void __user *source; |
| 1728 | u64 size; |
| 1729 | |
| 1730 | if (get_user(inti->type, (u64 __user *)addr)) |
| 1731 | return -EFAULT; |
| 1732 | |
| 1733 | switch (inti->type) { |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1734 | case KVM_S390_INT_PFAULT_INIT: |
| 1735 | case KVM_S390_INT_PFAULT_DONE: |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1736 | case KVM_S390_INT_VIRTIO: |
| 1737 | case KVM_S390_INT_SERVICE: |
| 1738 | target = (void *) &inti->ext; |
| 1739 | source = &uptr->u.ext; |
| 1740 | size = sizeof(inti->ext); |
| 1741 | break; |
| 1742 | case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX: |
| 1743 | target = (void *) &inti->io; |
| 1744 | source = &uptr->u.io; |
| 1745 | size = sizeof(inti->io); |
| 1746 | break; |
| 1747 | case KVM_S390_MCHK: |
| 1748 | target = (void *) &inti->mchk; |
| 1749 | source = &uptr->u.mchk; |
| 1750 | size = sizeof(inti->mchk); |
| 1751 | break; |
| 1752 | default: |
| 1753 | return -EINVAL; |
| 1754 | } |
| 1755 | |
| 1756 | if (copy_from_user(target, source, size)) |
| 1757 | return -EFAULT; |
| 1758 | |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
| 1762 | static int enqueue_floating_irq(struct kvm_device *dev, |
| 1763 | struct kvm_device_attr *attr) |
| 1764 | { |
| 1765 | struct kvm_s390_interrupt_info *inti = NULL; |
| 1766 | int r = 0; |
| 1767 | int len = attr->attr; |
| 1768 | |
| 1769 | if (len % sizeof(struct kvm_s390_irq) != 0) |
| 1770 | return -EINVAL; |
| 1771 | else if (len > KVM_S390_FLIC_MAX_BUFFER) |
| 1772 | return -EINVAL; |
| 1773 | |
| 1774 | while (len >= sizeof(struct kvm_s390_irq)) { |
| 1775 | inti = kzalloc(sizeof(*inti), GFP_KERNEL); |
| 1776 | if (!inti) |
| 1777 | return -ENOMEM; |
| 1778 | |
| 1779 | r = copy_irq_from_user(inti, attr->addr); |
| 1780 | if (r) { |
| 1781 | kfree(inti); |
| 1782 | return r; |
| 1783 | } |
Jens Freimann | a91b8eb | 2014-01-30 08:40:23 +0100 | [diff] [blame] | 1784 | r = __inject_vm(dev->kvm, inti); |
| 1785 | if (r) { |
| 1786 | kfree(inti); |
| 1787 | return r; |
| 1788 | } |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1789 | len -= sizeof(struct kvm_s390_irq); |
| 1790 | attr->addr += sizeof(struct kvm_s390_irq); |
| 1791 | } |
| 1792 | |
| 1793 | return r; |
| 1794 | } |
| 1795 | |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 1796 | static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id) |
| 1797 | { |
| 1798 | if (id >= MAX_S390_IO_ADAPTERS) |
| 1799 | return NULL; |
| 1800 | return kvm->arch.adapters[id]; |
| 1801 | } |
| 1802 | |
| 1803 | static int register_io_adapter(struct kvm_device *dev, |
| 1804 | struct kvm_device_attr *attr) |
| 1805 | { |
| 1806 | struct s390_io_adapter *adapter; |
| 1807 | struct kvm_s390_io_adapter adapter_info; |
| 1808 | |
| 1809 | if (copy_from_user(&adapter_info, |
| 1810 | (void __user *)attr->addr, sizeof(adapter_info))) |
| 1811 | return -EFAULT; |
| 1812 | |
| 1813 | if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) || |
| 1814 | (dev->kvm->arch.adapters[adapter_info.id] != NULL)) |
| 1815 | return -EINVAL; |
| 1816 | |
| 1817 | adapter = kzalloc(sizeof(*adapter), GFP_KERNEL); |
| 1818 | if (!adapter) |
| 1819 | return -ENOMEM; |
| 1820 | |
| 1821 | INIT_LIST_HEAD(&adapter->maps); |
| 1822 | init_rwsem(&adapter->maps_lock); |
| 1823 | atomic_set(&adapter->nr_maps, 0); |
| 1824 | adapter->id = adapter_info.id; |
| 1825 | adapter->isc = adapter_info.isc; |
| 1826 | adapter->maskable = adapter_info.maskable; |
| 1827 | adapter->masked = false; |
| 1828 | adapter->swap = adapter_info.swap; |
| 1829 | dev->kvm->arch.adapters[adapter->id] = adapter; |
| 1830 | |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
| 1834 | int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked) |
| 1835 | { |
| 1836 | int ret; |
| 1837 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); |
| 1838 | |
| 1839 | if (!adapter || !adapter->maskable) |
| 1840 | return -EINVAL; |
| 1841 | ret = adapter->masked; |
| 1842 | adapter->masked = masked; |
| 1843 | return ret; |
| 1844 | } |
| 1845 | |
| 1846 | static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr) |
| 1847 | { |
| 1848 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); |
| 1849 | struct s390_map_info *map; |
| 1850 | int ret; |
| 1851 | |
| 1852 | if (!adapter || !addr) |
| 1853 | return -EINVAL; |
| 1854 | |
| 1855 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 1856 | if (!map) { |
| 1857 | ret = -ENOMEM; |
| 1858 | goto out; |
| 1859 | } |
| 1860 | INIT_LIST_HEAD(&map->list); |
| 1861 | map->guest_addr = addr; |
Martin Schwidefsky | 6e0a043 | 2014-04-29 09:34:41 +0200 | [diff] [blame] | 1862 | map->addr = gmap_translate(kvm->arch.gmap, addr); |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 1863 | if (map->addr == -EFAULT) { |
| 1864 | ret = -EFAULT; |
| 1865 | goto out; |
| 1866 | } |
| 1867 | ret = get_user_pages_fast(map->addr, 1, 1, &map->page); |
| 1868 | if (ret < 0) |
| 1869 | goto out; |
| 1870 | BUG_ON(ret != 1); |
| 1871 | down_write(&adapter->maps_lock); |
| 1872 | if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) { |
| 1873 | list_add_tail(&map->list, &adapter->maps); |
| 1874 | ret = 0; |
| 1875 | } else { |
| 1876 | put_page(map->page); |
| 1877 | ret = -EINVAL; |
| 1878 | } |
| 1879 | up_write(&adapter->maps_lock); |
| 1880 | out: |
| 1881 | if (ret) |
| 1882 | kfree(map); |
| 1883 | return ret; |
| 1884 | } |
| 1885 | |
| 1886 | static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr) |
| 1887 | { |
| 1888 | struct s390_io_adapter *adapter = get_io_adapter(kvm, id); |
| 1889 | struct s390_map_info *map, *tmp; |
| 1890 | int found = 0; |
| 1891 | |
| 1892 | if (!adapter || !addr) |
| 1893 | return -EINVAL; |
| 1894 | |
| 1895 | down_write(&adapter->maps_lock); |
| 1896 | list_for_each_entry_safe(map, tmp, &adapter->maps, list) { |
| 1897 | if (map->guest_addr == addr) { |
| 1898 | found = 1; |
| 1899 | atomic_dec(&adapter->nr_maps); |
| 1900 | list_del(&map->list); |
| 1901 | put_page(map->page); |
| 1902 | kfree(map); |
| 1903 | break; |
| 1904 | } |
| 1905 | } |
| 1906 | up_write(&adapter->maps_lock); |
| 1907 | |
| 1908 | return found ? 0 : -EINVAL; |
| 1909 | } |
| 1910 | |
| 1911 | void kvm_s390_destroy_adapters(struct kvm *kvm) |
| 1912 | { |
| 1913 | int i; |
| 1914 | struct s390_map_info *map, *tmp; |
| 1915 | |
| 1916 | for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) { |
| 1917 | if (!kvm->arch.adapters[i]) |
| 1918 | continue; |
| 1919 | list_for_each_entry_safe(map, tmp, |
| 1920 | &kvm->arch.adapters[i]->maps, list) { |
| 1921 | list_del(&map->list); |
| 1922 | put_page(map->page); |
| 1923 | kfree(map); |
| 1924 | } |
| 1925 | kfree(kvm->arch.adapters[i]); |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | static int modify_io_adapter(struct kvm_device *dev, |
| 1930 | struct kvm_device_attr *attr) |
| 1931 | { |
| 1932 | struct kvm_s390_io_adapter_req req; |
| 1933 | struct s390_io_adapter *adapter; |
| 1934 | int ret; |
| 1935 | |
| 1936 | if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req))) |
| 1937 | return -EFAULT; |
| 1938 | |
| 1939 | adapter = get_io_adapter(dev->kvm, req.id); |
| 1940 | if (!adapter) |
| 1941 | return -EINVAL; |
| 1942 | switch (req.type) { |
| 1943 | case KVM_S390_IO_ADAPTER_MASK: |
| 1944 | ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask); |
| 1945 | if (ret > 0) |
| 1946 | ret = 0; |
| 1947 | break; |
| 1948 | case KVM_S390_IO_ADAPTER_MAP: |
| 1949 | ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr); |
| 1950 | break; |
| 1951 | case KVM_S390_IO_ADAPTER_UNMAP: |
| 1952 | ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr); |
| 1953 | break; |
| 1954 | default: |
| 1955 | ret = -EINVAL; |
| 1956 | } |
| 1957 | |
| 1958 | return ret; |
| 1959 | } |
| 1960 | |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1961 | static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr) |
| 1962 | { |
| 1963 | int r = 0; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1964 | unsigned int i; |
| 1965 | struct kvm_vcpu *vcpu; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1966 | |
| 1967 | switch (attr->group) { |
| 1968 | case KVM_DEV_FLIC_ENQUEUE: |
| 1969 | r = enqueue_floating_irq(dev, attr); |
| 1970 | break; |
| 1971 | case KVM_DEV_FLIC_CLEAR_IRQS: |
Christian Borntraeger | 67335e6 | 2014-03-25 17:09:08 +0100 | [diff] [blame] | 1972 | kvm_s390_clear_float_irqs(dev->kvm); |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1973 | break; |
Dominik Dingel | 3c038e6 | 2013-10-07 17:11:48 +0200 | [diff] [blame] | 1974 | case KVM_DEV_FLIC_APF_ENABLE: |
| 1975 | dev->kvm->arch.gmap->pfault_enabled = 1; |
| 1976 | break; |
| 1977 | case KVM_DEV_FLIC_APF_DISABLE_WAIT: |
| 1978 | dev->kvm->arch.gmap->pfault_enabled = 0; |
| 1979 | /* |
| 1980 | * Make sure no async faults are in transition when |
| 1981 | * clearing the queues. So we don't need to worry |
| 1982 | * about late coming workers. |
| 1983 | */ |
| 1984 | synchronize_srcu(&dev->kvm->srcu); |
| 1985 | kvm_for_each_vcpu(i, vcpu, dev->kvm) |
| 1986 | kvm_clear_async_pf_completion_queue(vcpu); |
| 1987 | break; |
Cornelia Huck | 841b91c | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 1988 | case KVM_DEV_FLIC_ADAPTER_REGISTER: |
| 1989 | r = register_io_adapter(dev, attr); |
| 1990 | break; |
| 1991 | case KVM_DEV_FLIC_ADAPTER_MODIFY: |
| 1992 | r = modify_io_adapter(dev, attr); |
| 1993 | break; |
Jens Freimann | c05c418 | 2013-10-07 16:13:45 +0200 | [diff] [blame] | 1994 | default: |
| 1995 | r = -EINVAL; |
| 1996 | } |
| 1997 | |
| 1998 | return r; |
| 1999 | } |
| 2000 | |
| 2001 | static int flic_create(struct kvm_device *dev, u32 type) |
| 2002 | { |
| 2003 | if (!dev) |
| 2004 | return -EINVAL; |
| 2005 | if (dev->kvm->arch.flic) |
| 2006 | return -EINVAL; |
| 2007 | dev->kvm->arch.flic = dev; |
| 2008 | return 0; |
| 2009 | } |
| 2010 | |
| 2011 | static void flic_destroy(struct kvm_device *dev) |
| 2012 | { |
| 2013 | dev->kvm->arch.flic = NULL; |
| 2014 | kfree(dev); |
| 2015 | } |
| 2016 | |
| 2017 | /* s390 floating irq controller (flic) */ |
| 2018 | struct kvm_device_ops kvm_flic_ops = { |
| 2019 | .name = "kvm-flic", |
| 2020 | .get_attr = flic_get_attr, |
| 2021 | .set_attr = flic_set_attr, |
| 2022 | .create = flic_create, |
| 2023 | .destroy = flic_destroy, |
| 2024 | }; |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2025 | |
| 2026 | static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap) |
| 2027 | { |
| 2028 | unsigned long bit; |
| 2029 | |
| 2030 | bit = bit_nr + (addr % PAGE_SIZE) * 8; |
| 2031 | |
| 2032 | return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit; |
| 2033 | } |
| 2034 | |
| 2035 | static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter, |
| 2036 | u64 addr) |
| 2037 | { |
| 2038 | struct s390_map_info *map; |
| 2039 | |
| 2040 | if (!adapter) |
| 2041 | return NULL; |
| 2042 | |
| 2043 | list_for_each_entry(map, &adapter->maps, list) { |
| 2044 | if (map->guest_addr == addr) |
| 2045 | return map; |
| 2046 | } |
| 2047 | return NULL; |
| 2048 | } |
| 2049 | |
| 2050 | static int adapter_indicators_set(struct kvm *kvm, |
| 2051 | struct s390_io_adapter *adapter, |
| 2052 | struct kvm_s390_adapter_int *adapter_int) |
| 2053 | { |
| 2054 | unsigned long bit; |
| 2055 | int summary_set, idx; |
| 2056 | struct s390_map_info *info; |
| 2057 | void *map; |
| 2058 | |
| 2059 | info = get_map_info(adapter, adapter_int->ind_addr); |
| 2060 | if (!info) |
| 2061 | return -1; |
| 2062 | map = page_address(info->page); |
| 2063 | bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap); |
| 2064 | set_bit(bit, map); |
| 2065 | idx = srcu_read_lock(&kvm->srcu); |
| 2066 | mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT); |
| 2067 | set_page_dirty_lock(info->page); |
| 2068 | info = get_map_info(adapter, adapter_int->summary_addr); |
| 2069 | if (!info) { |
| 2070 | srcu_read_unlock(&kvm->srcu, idx); |
| 2071 | return -1; |
| 2072 | } |
| 2073 | map = page_address(info->page); |
| 2074 | bit = get_ind_bit(info->addr, adapter_int->summary_offset, |
| 2075 | adapter->swap); |
| 2076 | summary_set = test_and_set_bit(bit, map); |
| 2077 | mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT); |
| 2078 | set_page_dirty_lock(info->page); |
| 2079 | srcu_read_unlock(&kvm->srcu, idx); |
| 2080 | return summary_set ? 0 : 1; |
| 2081 | } |
| 2082 | |
| 2083 | /* |
| 2084 | * < 0 - not injected due to error |
| 2085 | * = 0 - coalesced, summary indicator already active |
| 2086 | * > 0 - injected interrupt |
| 2087 | */ |
| 2088 | static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e, |
| 2089 | struct kvm *kvm, int irq_source_id, int level, |
| 2090 | bool line_status) |
| 2091 | { |
| 2092 | int ret; |
| 2093 | struct s390_io_adapter *adapter; |
| 2094 | |
| 2095 | /* We're only interested in the 0->1 transition. */ |
| 2096 | if (!level) |
| 2097 | return 0; |
| 2098 | adapter = get_io_adapter(kvm, e->adapter.adapter_id); |
| 2099 | if (!adapter) |
| 2100 | return -1; |
| 2101 | down_read(&adapter->maps_lock); |
| 2102 | ret = adapter_indicators_set(kvm, adapter, &e->adapter); |
| 2103 | up_read(&adapter->maps_lock); |
| 2104 | if ((ret > 0) && !adapter->masked) { |
| 2105 | struct kvm_s390_interrupt s390int = { |
| 2106 | .type = KVM_S390_INT_IO(1, 0, 0, 0), |
| 2107 | .parm = 0, |
| 2108 | .parm64 = (adapter->isc << 27) | 0x80000000, |
| 2109 | }; |
| 2110 | ret = kvm_s390_inject_vm(kvm, &s390int); |
| 2111 | if (ret == 0) |
| 2112 | ret = 1; |
| 2113 | } |
| 2114 | return ret; |
| 2115 | } |
| 2116 | |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 2117 | int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e, |
Cornelia Huck | 8422359 | 2013-07-15 13:36:01 +0200 | [diff] [blame] | 2118 | const struct kvm_irq_routing_entry *ue) |
| 2119 | { |
| 2120 | int ret; |
| 2121 | |
| 2122 | switch (ue->type) { |
| 2123 | case KVM_IRQ_ROUTING_S390_ADAPTER: |
| 2124 | e->set = set_adapter_int; |
| 2125 | e->adapter.summary_addr = ue->u.adapter.summary_addr; |
| 2126 | e->adapter.ind_addr = ue->u.adapter.ind_addr; |
| 2127 | e->adapter.summary_offset = ue->u.adapter.summary_offset; |
| 2128 | e->adapter.ind_offset = ue->u.adapter.ind_offset; |
| 2129 | e->adapter.adapter_id = ue->u.adapter.adapter_id; |
| 2130 | ret = 0; |
| 2131 | break; |
| 2132 | default: |
| 2133 | ret = -EINVAL; |
| 2134 | } |
| 2135 | |
| 2136 | return ret; |
| 2137 | } |
| 2138 | |
| 2139 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, |
| 2140 | int irq_source_id, int level, bool line_status) |
| 2141 | { |
| 2142 | return -EINVAL; |
| 2143 | } |
Jens Freimann | 816c766 | 2014-11-24 17:13:46 +0100 | [diff] [blame] | 2144 | |
| 2145 | int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len) |
| 2146 | { |
| 2147 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 2148 | struct kvm_s390_irq *buf; |
| 2149 | int r = 0; |
| 2150 | int n; |
| 2151 | |
| 2152 | buf = vmalloc(len); |
| 2153 | if (!buf) |
| 2154 | return -ENOMEM; |
| 2155 | |
| 2156 | if (copy_from_user((void *) buf, irqstate, len)) { |
| 2157 | r = -EFAULT; |
| 2158 | goto out_free; |
| 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | * Don't allow setting the interrupt state |
| 2163 | * when there are already interrupts pending |
| 2164 | */ |
| 2165 | spin_lock(&li->lock); |
| 2166 | if (li->pending_irqs) { |
| 2167 | r = -EBUSY; |
| 2168 | goto out_unlock; |
| 2169 | } |
| 2170 | |
| 2171 | for (n = 0; n < len / sizeof(*buf); n++) { |
| 2172 | r = do_inject_vcpu(vcpu, &buf[n]); |
| 2173 | if (r) |
| 2174 | break; |
| 2175 | } |
| 2176 | |
| 2177 | out_unlock: |
| 2178 | spin_unlock(&li->lock); |
| 2179 | out_free: |
| 2180 | vfree(buf); |
| 2181 | |
| 2182 | return r; |
| 2183 | } |
| 2184 | |
| 2185 | static void store_local_irq(struct kvm_s390_local_interrupt *li, |
| 2186 | struct kvm_s390_irq *irq, |
| 2187 | unsigned long irq_type) |
| 2188 | { |
| 2189 | switch (irq_type) { |
| 2190 | case IRQ_PEND_MCHK_EX: |
| 2191 | case IRQ_PEND_MCHK_REP: |
| 2192 | irq->type = KVM_S390_MCHK; |
| 2193 | irq->u.mchk = li->irq.mchk; |
| 2194 | break; |
| 2195 | case IRQ_PEND_PROG: |
| 2196 | irq->type = KVM_S390_PROGRAM_INT; |
| 2197 | irq->u.pgm = li->irq.pgm; |
| 2198 | break; |
| 2199 | case IRQ_PEND_PFAULT_INIT: |
| 2200 | irq->type = KVM_S390_INT_PFAULT_INIT; |
| 2201 | irq->u.ext = li->irq.ext; |
| 2202 | break; |
| 2203 | case IRQ_PEND_EXT_EXTERNAL: |
| 2204 | irq->type = KVM_S390_INT_EXTERNAL_CALL; |
| 2205 | irq->u.extcall = li->irq.extcall; |
| 2206 | break; |
| 2207 | case IRQ_PEND_EXT_CLOCK_COMP: |
| 2208 | irq->type = KVM_S390_INT_CLOCK_COMP; |
| 2209 | break; |
| 2210 | case IRQ_PEND_EXT_CPU_TIMER: |
| 2211 | irq->type = KVM_S390_INT_CPU_TIMER; |
| 2212 | break; |
| 2213 | case IRQ_PEND_SIGP_STOP: |
| 2214 | irq->type = KVM_S390_SIGP_STOP; |
| 2215 | irq->u.stop = li->irq.stop; |
| 2216 | break; |
| 2217 | case IRQ_PEND_RESTART: |
| 2218 | irq->type = KVM_S390_RESTART; |
| 2219 | break; |
| 2220 | case IRQ_PEND_SET_PREFIX: |
| 2221 | irq->type = KVM_S390_SIGP_SET_PREFIX; |
| 2222 | irq->u.prefix = li->irq.prefix; |
| 2223 | break; |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len) |
| 2228 | { |
| 2229 | uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl; |
| 2230 | unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)]; |
| 2231 | struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int; |
| 2232 | unsigned long pending_irqs; |
| 2233 | struct kvm_s390_irq irq; |
| 2234 | unsigned long irq_type; |
| 2235 | int cpuaddr; |
| 2236 | int n = 0; |
| 2237 | |
| 2238 | spin_lock(&li->lock); |
| 2239 | pending_irqs = li->pending_irqs; |
| 2240 | memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending, |
| 2241 | sizeof(sigp_emerg_pending)); |
| 2242 | spin_unlock(&li->lock); |
| 2243 | |
| 2244 | for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) { |
| 2245 | memset(&irq, 0, sizeof(irq)); |
| 2246 | if (irq_type == IRQ_PEND_EXT_EMERGENCY) |
| 2247 | continue; |
| 2248 | if (n + sizeof(irq) > len) |
| 2249 | return -ENOBUFS; |
| 2250 | store_local_irq(&vcpu->arch.local_int, &irq, irq_type); |
| 2251 | if (copy_to_user(&buf[n], &irq, sizeof(irq))) |
| 2252 | return -EFAULT; |
| 2253 | n += sizeof(irq); |
| 2254 | } |
| 2255 | |
| 2256 | if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) { |
| 2257 | for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) { |
| 2258 | memset(&irq, 0, sizeof(irq)); |
| 2259 | if (n + sizeof(irq) > len) |
| 2260 | return -ENOBUFS; |
| 2261 | irq.type = KVM_S390_INT_EMERGENCY; |
| 2262 | irq.u.emerg.code = cpuaddr; |
| 2263 | if (copy_to_user(&buf[n], &irq, sizeof(irq))) |
| 2264 | return -EFAULT; |
| 2265 | n += sizeof(irq); |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | if ((sigp_ctrl & SIGP_CTRL_C) && |
| 2270 | (atomic_read(&vcpu->arch.sie_block->cpuflags) & |
| 2271 | CPUSTAT_ECALL_PEND)) { |
| 2272 | if (n + sizeof(irq) > len) |
| 2273 | return -ENOBUFS; |
| 2274 | memset(&irq, 0, sizeof(irq)); |
| 2275 | irq.type = KVM_S390_INT_EXTERNAL_CALL; |
| 2276 | irq.u.extcall.code = sigp_ctrl & SIGP_CTRL_SCN_MASK; |
| 2277 | if (copy_to_user(&buf[n], &irq, sizeof(irq))) |
| 2278 | return -EFAULT; |
| 2279 | n += sizeof(irq); |
| 2280 | } |
| 2281 | |
| 2282 | return n; |
| 2283 | } |