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