blob: 4604e9accc650a94f8203206a98a4217e803d6ae [file] [log] [blame]
Carsten Otteba5c1e92008-03-25 18:47:26 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01003 *
Thomas Huth33b412a2015-02-11 10:38:46 +01004 * Copyright IBM Corp. 2008, 2015
Carsten Otteba5c1e92008-03-25 18:47:26 +01005 *
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 Borntraegerca872302009-05-12 17:21:49 +020013#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010014#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010015#include <linux/hrtimer.h>
Cornelia Huck84223592013-07-15 13:36:01 +020016#include <linux/mmu_context.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020017#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Jens Freimann383d0b02014-07-29 15:11:49 +020019#include <linux/bitmap.h>
Jens Freimann94aa0332015-03-16 12:17:13 +010020#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010021#include <asm/asm-offsets.h>
Thomas Huth33b412a2015-02-11 10:38:46 +010022#include <asm/dis.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010023#include <asm/uaccess.h>
David Hildenbrandea5f4962014-10-14 15:29:30 +020024#include <asm/sclp.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020025#include <asm/isc.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010026#include "kvm-s390.h"
27#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020028#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010029
Cornelia Huckd8346b72012-12-20 15:32:08 +010030#define IOINT_SCHID_MASK 0x0000ffff
31#define IOINT_SSID_MASK 0x00030000
32#define IOINT_CSSID_MASK 0x03fc0000
Jens Freimann44c6ca32014-04-16 13:57:18 +020033#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010034#define PFAULT_DONE 0x0680
35#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010036
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020037/* handle external calls via sigp interpretation facility */
38static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id)
39{
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020040 int c, scn;
41
David Hildenbrand2c1bb2b2015-09-23 09:45:50 +020042 if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND))
43 return 0;
44
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020045 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020046 if (vcpu->kvm->arch.use_esca) {
47 struct esca_block *sca = vcpu->kvm->arch.sca;
48 union esca_sigp_ctrl sigp_ctrl =
49 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
50
51 c = sigp_ctrl.c;
52 scn = sigp_ctrl.scn;
53 } else {
54 struct bsca_block *sca = vcpu->kvm->arch.sca;
55 union bsca_sigp_ctrl sigp_ctrl =
56 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
57
58 c = sigp_ctrl.c;
59 scn = sigp_ctrl.scn;
60 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020061 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020062
63 if (src_id)
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020064 *src_id = scn;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020065
David Hildenbrand2c1bb2b2015-09-23 09:45:50 +020066 return c;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020067}
68
69static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
70{
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020071 int expect, rc;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020072
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020073 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020074 if (vcpu->kvm->arch.use_esca) {
75 struct esca_block *sca = vcpu->kvm->arch.sca;
76 union esca_sigp_ctrl *sigp_ctrl =
77 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
78 union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020079
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020080 new_val.scn = src_id;
81 new_val.c = 1;
82 old_val.c = 0;
83
84 expect = old_val.value;
85 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
86 } else {
87 struct bsca_block *sca = vcpu->kvm->arch.sca;
88 union bsca_sigp_ctrl *sigp_ctrl =
89 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
90 union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
91
92 new_val.scn = src_id;
93 new_val.c = 1;
94 old_val.c = 0;
95
96 expect = old_val.value;
97 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
98 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020099 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +0200100
101 if (rc != expect) {
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200102 /* another external call is pending */
103 return -EBUSY;
104 }
105 atomic_or(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
106 return 0;
107}
108
109static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
110{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200111 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +0200112 int rc, expect;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200113
114 atomic_andnot(CPUSTAT_ECALL_PEND, li->cpuflags);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +0200115 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +0200116 if (vcpu->kvm->arch.use_esca) {
117 struct esca_block *sca = vcpu->kvm->arch.sca;
118 union esca_sigp_ctrl *sigp_ctrl =
119 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
120 union esca_sigp_ctrl old = *sigp_ctrl;
121
122 expect = old.value;
123 rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
124 } else {
125 struct bsca_block *sca = vcpu->kvm->arch.sca;
126 union bsca_sigp_ctrl *sigp_ctrl =
127 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
128 union bsca_sigp_ctrl old = *sigp_ctrl;
129
130 expect = old.value;
131 rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
132 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +0200133 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +0200134 WARN_ON(rc != expect); /* cannot clear? */
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200135}
136
Dominik Dingel3c038e62013-10-07 17:11:48 +0200137int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100138{
139 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
140}
141
Cornelia Huckd8346b72012-12-20 15:32:08 +0100142static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
143{
144 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
145}
146
Cornelia Huck48a3e952012-12-20 15:32:09 +0100147static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
148{
149 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
150}
151
Carsten Otteba5c1e92008-03-25 18:47:26 +0100152static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
153{
David Hildenbrandfee0e0f2015-09-28 13:32:38 +0200154 return psw_extint_disabled(vcpu) &&
155 psw_ioint_disabled(vcpu) &&
156 psw_mchk_disabled(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100157}
158
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100159static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
160{
161 if (psw_extint_disabled(vcpu) ||
162 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
163 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +0100164 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
165 /* No timer interrupts when single stepping */
166 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100167 return 1;
168}
169
David Hildenbrandb4aec922014-12-01 15:55:42 +0100170static int ckc_irq_pending(struct kvm_vcpu *vcpu)
171{
David Hildenbrand60417fc2015-09-29 16:20:36 +0200172 if (vcpu->arch.sie_block->ckc >= kvm_s390_get_tod_clock_fast(vcpu->kvm))
David Hildenbrandb4aec922014-12-01 15:55:42 +0100173 return 0;
174 return ckc_interrupts_enabled(vcpu);
175}
176
177static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
178{
179 return !psw_extint_disabled(vcpu) &&
180 (vcpu->arch.sie_block->gcr[0] & 0x400ul);
181}
182
183static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
184{
David Hildenbrand4287f242016-02-15 09:40:12 +0100185 if (!cpu_timer_interrupts_enabled(vcpu))
186 return 0;
187 return kvm_s390_get_cpu_timer(vcpu) >> 63;
David Hildenbrandb4aec922014-12-01 15:55:42 +0100188}
189
Jens Freimann6d3da242013-07-03 15:18:35 +0200190static inline int is_ioirq(unsigned long irq_type)
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100191{
Jens Freimann6d3da242013-07-03 15:18:35 +0200192 return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
193 (irq_type <= IRQ_PEND_IO_ISC_7));
194}
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100195
Jens Freimann6d3da242013-07-03 15:18:35 +0200196static uint64_t isc_to_isc_bits(int isc)
197{
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100198 return (0x80 >> isc) << 24;
199}
200
Jens Freimann6d3da242013-07-03 15:18:35 +0200201static inline u8 int_word_to_isc(u32 int_word)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100202{
Jens Freimann6d3da242013-07-03 15:18:35 +0200203 return (int_word & 0x38000000) >> 27;
204}
205
David Hildenbrand5f94c582015-09-28 14:27:51 +0200206static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
Jens Freimann6d3da242013-07-03 15:18:35 +0200207{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200208 return vcpu->kvm->arch.float_int.pending_irqs |
209 vcpu->arch.local_int.pending_irqs;
Jens Freimann383d0b02014-07-29 15:11:49 +0200210}
211
Jens Freimann6d3da242013-07-03 15:18:35 +0200212static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
213 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200214{
Jens Freimann6d3da242013-07-03 15:18:35 +0200215 int i;
216
217 for (i = 0; i <= MAX_ISC; i++)
218 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
219 active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
220
221 return active_mask;
222}
223
224static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
225{
226 unsigned long active_mask;
227
David Hildenbrand5f94c582015-09-28 14:27:51 +0200228 active_mask = pending_irqs(vcpu);
Jens Freimannffeca0a2015-04-17 10:21:04 +0200229 if (!active_mask)
230 return 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200231
232 if (psw_extint_disabled(vcpu))
233 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200234 if (psw_ioint_disabled(vcpu))
235 active_mask &= ~IRQ_PEND_IO_MASK;
236 else
237 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200238 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
239 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
240 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
241 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
242 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
243 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
244 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
245 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200246 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
247 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200248 if (psw_mchk_disabled(vcpu))
249 active_mask &= ~IRQ_PEND_MCHK_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200250 if (!(vcpu->arch.sie_block->gcr[14] &
251 vcpu->kvm->arch.float_int.mchk.cr14))
252 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200253
David Hildenbrand6cddd432014-10-15 16:48:53 +0200254 /*
255 * STOP irqs will never be actively delivered. They are triggered via
256 * intercept requests and cleared when the stop intercept is performed.
257 */
258 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
259
Jens Freimann383d0b02014-07-29 15:11:49 +0200260 return active_mask;
261}
262
Carsten Otteba5c1e92008-03-25 18:47:26 +0100263static void __set_cpu_idle(struct kvm_vcpu *vcpu)
264{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200265 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100266 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
267}
268
269static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
270{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200271 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100272 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
273}
274
275static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
276{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200277 atomic_andnot(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
278 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100279 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100280 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
281
282 if (guestdbg_enabled(vcpu)) {
283 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
284 LCTL_CR10 | LCTL_CR11);
285 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
286 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100287}
288
289static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
290{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200291 atomic_or(flag, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100292}
293
Jens Freimann6d3da242013-07-03 15:18:35 +0200294static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
295{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200296 if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
Jens Freimann6d3da242013-07-03 15:18:35 +0200297 return;
298 else if (psw_ioint_disabled(vcpu))
299 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
300 else
301 vcpu->arch.sie_block->lctl |= LCTL_CR6;
302}
303
Jens Freimann383d0b02014-07-29 15:11:49 +0200304static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
305{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200306 if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200307 return;
308 if (psw_extint_disabled(vcpu))
309 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
310 else
311 vcpu->arch.sie_block->lctl |= LCTL_CR0;
312}
313
314static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
315{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200316 if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200317 return;
318 if (psw_mchk_disabled(vcpu))
319 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
320 else
321 vcpu->arch.sie_block->lctl |= LCTL_CR14;
322}
323
David Hildenbrand6cddd432014-10-15 16:48:53 +0200324static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
325{
326 if (kvm_s390_is_stop_irq_pending(vcpu))
327 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
328}
329
Jens Freimann6d3da242013-07-03 15:18:35 +0200330/* Set interception request for non-deliverable interrupts */
331static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200332{
Jens Freimann6d3da242013-07-03 15:18:35 +0200333 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200334 set_intercept_indicators_ext(vcpu);
335 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200336 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200337}
338
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200339static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100340{
Jens Freimann383d0b02014-07-29 15:11:49 +0200341 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200342 int rc;
343
344 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
345 0, 0);
346
347 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
348 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100349 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200350 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
351 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
352 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
353 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200354 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100355 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200356}
357
358static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
359{
Jens Freimann383d0b02014-07-29 15:11:49 +0200360 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200361 int rc;
362
363 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
364 0, 0);
365
366 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
367 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100368 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200369 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
370 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
371 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
372 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200373 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100374 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200375}
376
Jens Freimann383d0b02014-07-29 15:11:49 +0200377static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200378{
Jens Freimann383d0b02014-07-29 15:11:49 +0200379 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
380 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200381 int rc;
382
Jens Freimann383d0b02014-07-29 15:11:49 +0200383 spin_lock(&li->lock);
384 ext = li->irq.ext;
385 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
386 li->irq.ext.ext_params2 = 0;
387 spin_unlock(&li->lock);
388
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200389 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
390 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200391 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
392 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200393 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200394
395 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
396 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
397 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
398 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
399 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
400 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200401 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100402 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200403}
404
Jens Freimann383d0b02014-07-29 15:11:49 +0200405static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200406{
Jens Freimann6d3da242013-07-03 15:18:35 +0200407 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200408 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200409 struct kvm_s390_mchk_info mchk = {};
Eric Farmanbc17de72014-04-14 16:01:09 -0400410 unsigned long adtl_status_addr;
Jens Freimann6d3da242013-07-03 15:18:35 +0200411 int deliver = 0;
412 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200413
Jens Freimann6d3da242013-07-03 15:18:35 +0200414 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200415 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200416 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
417 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
418 /*
419 * If there was an exigent machine check pending, then any
420 * repressible machine checks that might have been pending
421 * are indicated along with it, so always clear bits for
422 * repressible and exigent interrupts
423 */
424 mchk = li->irq.mchk;
425 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
426 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
427 memset(&li->irq.mchk, 0, sizeof(mchk));
428 deliver = 1;
429 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200430 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200431 * We indicate floating repressible conditions along with
432 * other pending conditions. Channel Report Pending and Channel
433 * Subsystem damage are the only two and and are indicated by
434 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200435 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200436 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
437 mchk.mcic |= fi->mchk.mcic;
438 mchk.cr14 |= fi->mchk.cr14;
439 memset(&fi->mchk, 0, sizeof(mchk));
440 deliver = 1;
441 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200442 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200443 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200444
Jens Freimann6d3da242013-07-03 15:18:35 +0200445 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200446 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200447 mchk.mcic);
448 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
449 KVM_S390_MCHK,
450 mchk.cr14, mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200451
Jens Freimann6d3da242013-07-03 15:18:35 +0200452 rc = kvm_s390_vcpu_store_status(vcpu,
453 KVM_S390_STORE_STATUS_PREFIXED);
454 rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR,
455 &adtl_status_addr,
456 sizeof(unsigned long));
457 rc |= kvm_s390_vcpu_store_adtl_status(vcpu,
458 adtl_status_addr);
459 rc |= put_guest_lc(vcpu, mchk.mcic,
460 (u64 __user *) __LC_MCCK_CODE);
461 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
462 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
463 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
464 &mchk.fixed_logout,
465 sizeof(mchk.fixed_logout));
466 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
467 &vcpu->arch.sie_block->gpsw,
468 sizeof(psw_t));
469 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
470 &vcpu->arch.sie_block->gpsw,
471 sizeof(psw_t));
472 }
Jens Freimann99e20002014-12-01 17:05:39 +0100473 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200474}
475
476static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
477{
Jens Freimann383d0b02014-07-29 15:11:49 +0200478 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200479 int rc;
480
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200481 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200482 vcpu->stat.deliver_restart_signal++;
483 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
484
485 rc = write_guest_lc(vcpu,
Heiko Carstensc667aea2015-12-31 10:29:00 +0100486 offsetof(struct lowcore, restart_old_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200487 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstensc667aea2015-12-31 10:29:00 +0100488 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200489 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200490 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100491 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200492}
493
Jens Freimann383d0b02014-07-29 15:11:49 +0200494static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200495{
Jens Freimann383d0b02014-07-29 15:11:49 +0200496 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
497 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200498
Jens Freimann383d0b02014-07-29 15:11:49 +0200499 spin_lock(&li->lock);
500 prefix = li->irq.prefix;
501 li->irq.prefix.address = 0;
502 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
503 spin_unlock(&li->lock);
504
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200505 vcpu->stat.deliver_prefix_signal++;
506 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
507 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200508 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200509
Jens Freimann383d0b02014-07-29 15:11:49 +0200510 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200511 return 0;
512}
513
Jens Freimann383d0b02014-07-29 15:11:49 +0200514static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200515{
Jens Freimann383d0b02014-07-29 15:11:49 +0200516 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200517 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200518 int cpu_addr;
519
520 spin_lock(&li->lock);
521 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
522 clear_bit(cpu_addr, li->sigp_emerg_pending);
523 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
524 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
525 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200526
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200527 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200528 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200529 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
530 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200531
532 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
533 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200534 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200535 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
536 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
537 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
538 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100539 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200540}
541
Jens Freimann383d0b02014-07-29 15:11:49 +0200542static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200543{
Jens Freimann383d0b02014-07-29 15:11:49 +0200544 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
545 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200546 int rc;
547
Jens Freimann383d0b02014-07-29 15:11:49 +0200548 spin_lock(&li->lock);
549 extcall = li->irq.extcall;
550 li->irq.extcall.code = 0;
551 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
552 spin_unlock(&li->lock);
553
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200554 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200555 vcpu->stat.deliver_external_call++;
556 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
557 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200558 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200559
560 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
561 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200562 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200563 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
564 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
565 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
566 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100567 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200568}
569
Jens Freimann383d0b02014-07-29 15:11:49 +0200570static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200571{
Jens Freimann383d0b02014-07-29 15:11:49 +0200572 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
573 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100574 int rc = 0, nullifying = false;
David Hildenbrand634790b2015-11-04 16:33:33 +0100575 u16 ilen;
David Hildenbrand87128362014-03-03 10:55:13 +0100576
Jens Freimann383d0b02014-07-29 15:11:49 +0200577 spin_lock(&li->lock);
578 pgm_info = li->irq.pgm;
579 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
580 memset(&li->irq.pgm, 0, sizeof(pgm_info));
581 spin_unlock(&li->lock);
582
David Hildenbrand634790b2015-11-04 16:33:33 +0100583 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100584 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
585 pgm_info.code, ilen);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200586 vcpu->stat.deliver_program_int++;
587 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200588 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200589
Jens Freimann383d0b02014-07-29 15:11:49 +0200590 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100591 case PGM_AFX_TRANSLATION:
592 case PGM_ASX_TRANSLATION:
593 case PGM_EX_TRANSLATION:
594 case PGM_LFX_TRANSLATION:
595 case PGM_LSTE_SEQUENCE:
596 case PGM_LSX_TRANSLATION:
597 case PGM_LX_TRANSLATION:
598 case PGM_PRIMARY_AUTHORITY:
599 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100600 nullifying = true;
601 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100602 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200603 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100604 (u64 *)__LC_TRANS_EXC_CODE);
605 break;
606 case PGM_ALEN_TRANSLATION:
607 case PGM_ALE_SEQUENCE:
608 case PGM_ASTE_INSTANCE:
609 case PGM_ASTE_SEQUENCE:
610 case PGM_ASTE_VALIDITY:
611 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200612 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100613 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100614 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100615 break;
616 case PGM_ASCE_TYPE:
617 case PGM_PAGE_TRANSLATION:
618 case PGM_REGION_FIRST_TRANS:
619 case PGM_REGION_SECOND_TRANS:
620 case PGM_REGION_THIRD_TRANS:
621 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200622 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100623 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200624 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100625 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200626 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100627 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100628 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100629 break;
630 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200631 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200632 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200633 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100634 (u64 *)__LC_MON_CODE);
635 break;
Eric Farman403c8642015-02-02 15:01:06 -0500636 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100637 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200638 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100639 (u32 *)__LC_DATA_EXC_CODE);
640 break;
641 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200642 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100643 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200644 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100645 (u8 *)__LC_EXC_ACCESS_ID);
646 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100647 case PGM_STACK_FULL:
648 case PGM_STACK_EMPTY:
649 case PGM_STACK_SPECIFICATION:
650 case PGM_STACK_TYPE:
651 case PGM_STACK_OPERATION:
652 case PGM_TRACE_TABEL:
653 case PGM_CRYPTO_OPERATION:
654 nullifying = true;
655 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100656 }
657
Jens Freimann383d0b02014-07-29 15:11:49 +0200658 if (pgm_info.code & PGM_PER) {
659 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100660 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200661 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100662 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200663 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100664 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200665 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100666 (u8 *) __LC_PER_ACCESS_ID);
667 }
668
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100669 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100670 kvm_s390_rewind_psw(vcpu, ilen);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100671
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100672 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
673 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100674 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
675 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200676 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100677 (u16 *)__LC_PGM_INT_CODE);
678 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
679 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
680 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
681 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100682 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200683}
David Hildenbrand87128362014-03-03 10:55:13 +0100684
Jens Freimann6d3da242013-07-03 15:18:35 +0200685static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200686{
Jens Freimann6d3da242013-07-03 15:18:35 +0200687 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
688 struct kvm_s390_ext_info ext;
689 int rc = 0;
690
691 spin_lock(&fi->lock);
692 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
693 spin_unlock(&fi->lock);
694 return 0;
695 }
696 ext = fi->srv_signal;
697 memset(&fi->srv_signal, 0, sizeof(ext));
698 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
699 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200700
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200701 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200702 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200703 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200704 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
705 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200706
707 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100708 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200709 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
710 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
711 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
712 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200713 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200714 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200715
Jens Freimann99e20002014-12-01 17:05:39 +0100716 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200717}
718
Jens Freimann6d3da242013-07-03 15:18:35 +0200719static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200720{
Jens Freimann6d3da242013-07-03 15:18:35 +0200721 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
722 struct kvm_s390_interrupt_info *inti;
723 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200724
Jens Freimann6d3da242013-07-03 15:18:35 +0200725 spin_lock(&fi->lock);
726 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
727 struct kvm_s390_interrupt_info,
728 list);
729 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200730 list_del(&inti->list);
731 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
732 }
733 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
734 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
735 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200736
Jens Freimann6d3da242013-07-03 15:18:35 +0200737 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200738 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
739 KVM_S390_INT_PFAULT_DONE, 0,
740 inti->ext.ext_params2);
741 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
742 inti->ext.ext_params2);
743
Jens Freimann6d3da242013-07-03 15:18:35 +0200744 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
745 (u16 *)__LC_EXT_INT_CODE);
746 rc |= put_guest_lc(vcpu, PFAULT_DONE,
747 (u16 *)__LC_EXT_CPU_ADDR);
748 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
749 &vcpu->arch.sie_block->gpsw,
750 sizeof(psw_t));
751 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
752 &vcpu->arch.sie_block->gpsw,
753 sizeof(psw_t));
754 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
755 (u64 *)__LC_EXT_PARAMS2);
756 kfree(inti);
757 }
Jens Freimann99e20002014-12-01 17:05:39 +0100758 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200759}
760
Jens Freimann6d3da242013-07-03 15:18:35 +0200761static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200762{
Jens Freimann6d3da242013-07-03 15:18:35 +0200763 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
764 struct kvm_s390_interrupt_info *inti;
765 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200766
Jens Freimann6d3da242013-07-03 15:18:35 +0200767 spin_lock(&fi->lock);
768 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
769 struct kvm_s390_interrupt_info,
770 list);
771 if (inti) {
772 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200773 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200774 inti->ext.ext_params, inti->ext.ext_params2);
775 vcpu->stat.deliver_virtio_interrupt++;
776 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
777 inti->type,
778 inti->ext.ext_params,
779 inti->ext.ext_params2);
780 list_del(&inti->list);
781 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
782 }
783 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
784 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
785 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200786
Jens Freimann6d3da242013-07-03 15:18:35 +0200787 if (inti) {
788 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
789 (u16 *)__LC_EXT_INT_CODE);
790 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
791 (u16 *)__LC_EXT_CPU_ADDR);
792 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
793 &vcpu->arch.sie_block->gpsw,
794 sizeof(psw_t));
795 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
796 &vcpu->arch.sie_block->gpsw,
797 sizeof(psw_t));
798 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
799 (u32 *)__LC_EXT_PARAMS);
800 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
801 (u64 *)__LC_EXT_PARAMS2);
802 kfree(inti);
803 }
Jens Freimann99e20002014-12-01 17:05:39 +0100804 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200805}
806
807static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200808 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200809{
Jens Freimann6d3da242013-07-03 15:18:35 +0200810 struct list_head *isc_list;
811 struct kvm_s390_float_interrupt *fi;
812 struct kvm_s390_interrupt_info *inti = NULL;
813 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200814
Jens Freimann6d3da242013-07-03 15:18:35 +0200815 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200816
Jens Freimann6d3da242013-07-03 15:18:35 +0200817 spin_lock(&fi->lock);
818 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
819 inti = list_first_entry_or_null(isc_list,
820 struct kvm_s390_interrupt_info,
821 list);
822 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200823 VCPU_EVENT(vcpu, 4, "deliver: I/O 0x%llx", inti->type);
Jens Freimann6d3da242013-07-03 15:18:35 +0200824 vcpu->stat.deliver_io_int++;
825 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
826 inti->type,
827 ((__u32)inti->io.subchannel_id << 16) |
828 inti->io.subchannel_nr,
829 ((__u64)inti->io.io_int_parm << 32) |
830 inti->io.io_int_word);
831 list_del(&inti->list);
832 fi->counters[FIRQ_CNTR_IO] -= 1;
833 }
834 if (list_empty(isc_list))
835 clear_bit(irq_type, &fi->pending_irqs);
836 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100837
Jens Freimann6d3da242013-07-03 15:18:35 +0200838 if (inti) {
839 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
840 (u16 *)__LC_SUBCHANNEL_ID);
841 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
842 (u16 *)__LC_SUBCHANNEL_NR);
843 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
844 (u32 *)__LC_IO_INT_PARM);
845 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
846 (u32 *)__LC_IO_INT_WORD);
847 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
848 &vcpu->arch.sie_block->gpsw,
849 sizeof(psw_t));
850 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
851 &vcpu->arch.sie_block->gpsw,
852 sizeof(psw_t));
853 kfree(inti);
854 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200855
Jens Freimann99e20002014-12-01 17:05:39 +0100856 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200857}
858
859typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
860
861static const deliver_irq_t deliver_irq_funcs[] = {
862 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200863 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200864 [IRQ_PEND_PROG] = __deliver_prog,
865 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
866 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
867 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
868 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
869 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200870 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
871 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200872 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
873 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
874 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200875};
876
David Hildenbrandea5f4962014-10-14 15:29:30 +0200877/* Check whether an external call is pending (deliverable or not) */
878int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100879{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200880 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +0100881
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200882 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200883 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100884
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200885 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100886}
887
David Hildenbrand9a022062014-08-05 17:40:47 +0200888int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100889{
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200890 if (deliverable_irqs(vcpu))
891 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100892
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200893 if (kvm_cpu_has_pending_timer(vcpu))
894 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100895
David Hildenbrandea5f4962014-10-14 15:29:30 +0200896 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200897 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +0200898 !psw_extint_disabled(vcpu) &&
899 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200900 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +0100901
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200902 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
903 return 1;
904 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100905}
906
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300907int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
908{
David Hildenbrandb4aec922014-12-01 15:55:42 +0100909 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300910}
911
Carsten Otteba5c1e92008-03-25 18:47:26 +0100912int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
913{
914 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100915
916 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100917
David Hildenbrand0759d062014-05-13 16:54:32 +0200918 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +0200919 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +0200920 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200921
Carsten Otteba5c1e92008-03-25 18:47:26 +0100922 if (psw_interrupts_disabled(vcpu)) {
923 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100924 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100925 }
926
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100927 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100928 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +0100929 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100930 goto no_timer;
931 }
932
David Hildenbrand60417fc2015-09-29 16:20:36 +0200933 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
Heiko Carstensed4f2092013-01-14 16:55:55 +0100934 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
David Hildenbrandbda343e2014-12-12 12:26:40 +0100935
936 /* underflow */
937 if (vcpu->arch.sie_block->ckc < now)
938 return 0;
939
940 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200941 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200942 VCPU_EVENT(vcpu, 4, "enabled wait via clock comparator: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100943no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200944 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200945 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100946 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200947 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
948
David Hildenbrand2d00f752014-12-11 10:18:01 +0100949 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100950 return 0;
951}
952
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200953void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
954{
955 if (waitqueue_active(&vcpu->wq)) {
956 /*
957 * The vcpu gave up the cpu voluntarily, mark it as a good
958 * yield-candidate.
959 */
960 vcpu->preempted = true;
961 wake_up_interruptible(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +0200962 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200963 }
964}
965
Christian Borntraegerca872302009-05-12 17:21:49 +0200966enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
967{
968 struct kvm_vcpu *vcpu;
David Hildenbrand2d00f752014-12-11 10:18:01 +0100969 u64 now, sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +0200970
971 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrand60417fc2015-09-29 16:20:36 +0200972 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
David Hildenbrand2d00f752014-12-11 10:18:01 +0100973 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200974
David Hildenbrand2d00f752014-12-11 10:18:01 +0100975 /*
976 * If the monotonic clock runs faster than the tod clock we might be
977 * woken up too early and have to go back to sleep to avoid deadlocks.
978 */
979 if (vcpu->arch.sie_block->ckc > now &&
980 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
981 return HRTIMER_RESTART;
982 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200983 return HRTIMER_NORESTART;
984}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100985
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100986void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
987{
988 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100989
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200990 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200991 li->pending_irqs = 0;
992 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
993 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200994 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100995
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200996 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100997}
998
Christian Borntraeger614aeab2014-08-25 12:27:29 +0200999int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001000{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001001 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001002 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +02001003 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +02001004 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +02001005 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001006
1007 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001008
Jens Freimann383d0b02014-07-29 15:11:49 +02001009 /* pending ckc conditions might have been invalidated */
1010 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +01001011 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +02001012 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1013
David Hildenbrandb4aec922014-12-01 15:55:42 +01001014 /* pending cpu timer conditions might have been invalidated */
1015 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1016 if (cpu_timer_irq_pending(vcpu))
1017 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1018
Jens Freimannffeca0a2015-04-17 10:21:04 +02001019 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001020 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +02001021 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +02001022 if (is_ioirq(irq_type)) {
1023 rc = __deliver_io(vcpu, irq_type);
1024 } else {
1025 func = deliver_irq_funcs[irq_type];
1026 if (!func) {
1027 WARN_ON_ONCE(func == NULL);
1028 clear_bit(irq_type, &li->pending_irqs);
1029 continue;
1030 }
1031 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +02001032 }
Jens Freimannffeca0a2015-04-17 10:21:04 +02001033 }
Jens Freimann383d0b02014-07-29 15:11:49 +02001034
Jens Freimann6d3da242013-07-03 15:18:35 +02001035 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +02001036
1037 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001038}
1039
Jens Freimann383d0b02014-07-29 15:11:49 +02001040static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001041{
1042 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1043
David Hildenbranded2afcf2015-07-20 10:33:03 +02001044 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1045 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1046 irq->u.pgm.code, 0);
1047
David Hildenbrand634790b2015-11-04 16:33:33 +01001048 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1049 /* auto detection if no valid ILC was given */
1050 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1051 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1052 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1053 }
1054
David Hildenbrand238293b2015-05-04 12:38:48 +02001055 if (irq->u.pgm.code == PGM_PER) {
1056 li->irq.pgm.code |= PGM_PER;
David Hildenbrand634790b2015-11-04 16:33:33 +01001057 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001058 /* only modify PER related information */
1059 li->irq.pgm.per_address = irq->u.pgm.per_address;
1060 li->irq.pgm.per_code = irq->u.pgm.per_code;
1061 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1062 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1063 } else if (!(irq->u.pgm.code & PGM_PER)) {
1064 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1065 irq->u.pgm.code;
David Hildenbrand634790b2015-11-04 16:33:33 +01001066 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001067 /* only modify non-PER information */
1068 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1069 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1070 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1071 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1072 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1073 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1074 } else {
1075 li->irq.pgm = irq->u.pgm;
1076 }
Jens Freimann91851242014-12-01 16:43:40 +01001077 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001078 return 0;
1079}
1080
Jens Freimann383d0b02014-07-29 15:11:49 +02001081static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001082{
1083 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1084
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001085 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1086 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001087 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1088 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001089 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001090
1091 li->irq.ext = irq->u.ext;
1092 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001093 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001094 return 0;
1095}
1096
Christian Borntraeger0675d922015-01-15 12:40:42 +01001097static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001098{
1099 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001100 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001101 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001102
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001103 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001104 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001105 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001106 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001107
David Hildenbrandea5f4962014-10-14 15:29:30 +02001108 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001109 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001110 return -EINVAL;
1111
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001112 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001113 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001114
David Hildenbrandb938eace2015-04-30 13:33:59 +02001115 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001116 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001117 *extcall = irq->u.extcall;
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001118 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001119 return 0;
1120}
1121
Jens Freimann383d0b02014-07-29 15:11:49 +02001122static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001123{
1124 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001125 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001126
David Hildenbranded2afcf2015-07-20 10:33:03 +02001127 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001128 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001129 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001130 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001131
David Hildenbranda3a9c592014-10-14 09:44:55 +02001132 if (!is_vcpu_stopped(vcpu))
1133 return -EBUSY;
1134
Jens Freimann383d0b02014-07-29 15:11:49 +02001135 *prefix = irq->u.prefix;
1136 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001137 return 0;
1138}
1139
David Hildenbrand6cddd432014-10-15 16:48:53 +02001140#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001141static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001142{
1143 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001144 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001145 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001146
David Hildenbranded2afcf2015-07-20 10:33:03 +02001147 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001148
David Hildenbrand28225452014-10-15 16:48:16 +02001149 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1150 return -EINVAL;
1151
David Hildenbrand6cddd432014-10-15 16:48:53 +02001152 if (is_vcpu_stopped(vcpu)) {
1153 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1154 rc = kvm_s390_store_status_unloaded(vcpu,
1155 KVM_S390_STORE_STATUS_NOADDR);
1156 return rc;
1157 }
1158
1159 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1160 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001161 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001162 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001163 return 0;
1164}
1165
1166static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001167 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001168{
1169 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1170
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001171 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001172 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001173
1174 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001175 return 0;
1176}
1177
1178static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001179 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001180{
1181 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1182
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001183 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001184 irq->u.emerg.code);
1185 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001186 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001187
David Hildenbrandb85de332015-11-05 09:38:15 +01001188 /* sending vcpu invalid */
1189 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1190 return -EINVAL;
1191
Jens Freimann49538d12014-12-18 15:48:14 +01001192 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001193 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001194 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001195 return 0;
1196}
1197
Jens Freimann383d0b02014-07-29 15:11:49 +02001198static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001199{
1200 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001201 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001202
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001203 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001204 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001205 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001206 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001207
1208 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001209 * Because repressible machine checks can be indicated along with
1210 * exigent machine checks (PoP, Chapter 11, Interruption action)
1211 * we need to combine cr14, mcic and external damage code.
1212 * Failing storage address and the logout area should not be or'ed
1213 * together, we just indicate the last occurrence of the corresponding
1214 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001215 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001216 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001217 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001218 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1219 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1220 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1221 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001222 if (mchk->mcic & MCHK_EX_MASK)
1223 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1224 else if (mchk->mcic & MCHK_REP_MASK)
1225 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001226 return 0;
1227}
1228
Jens Freimann383d0b02014-07-29 15:11:49 +02001229static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001230{
1231 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1232
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001233 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001234 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001235 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001236
1237 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001238 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001239 return 0;
1240}
1241
Jens Freimann383d0b02014-07-29 15:11:49 +02001242static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001243{
1244 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1245
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001246 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001247 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001248 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001249
1250 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001251 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001252 return 0;
1253}
1254
Jens Freimann6d3da242013-07-03 15:18:35 +02001255static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1256 int isc, u32 schid)
1257{
1258 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1259 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1260 struct kvm_s390_interrupt_info *iter;
1261 u16 id = (schid & 0xffff0000U) >> 16;
1262 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001263
Jens Freimann6d3da242013-07-03 15:18:35 +02001264 spin_lock(&fi->lock);
1265 list_for_each_entry(iter, isc_list, list) {
1266 if (schid && (id != iter->io.subchannel_id ||
1267 nr != iter->io.subchannel_nr))
1268 continue;
1269 /* found an appropriate entry */
1270 list_del_init(&iter->list);
1271 fi->counters[FIRQ_CNTR_IO] -= 1;
1272 if (list_empty(isc_list))
1273 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1274 spin_unlock(&fi->lock);
1275 return iter;
1276 }
1277 spin_unlock(&fi->lock);
1278 return NULL;
1279}
1280
1281/*
1282 * Dequeue and return an I/O interrupt matching any of the interruption
1283 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1284 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001285struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001286 u64 isc_mask, u32 schid)
1287{
1288 struct kvm_s390_interrupt_info *inti = NULL;
1289 int isc;
1290
1291 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1292 if (isc_mask & isc_to_isc_bits(isc))
1293 inti = get_io_int(kvm, isc, schid);
1294 }
1295 return inti;
1296}
1297
1298#define SCCB_MASK 0xFFFFFFF8
1299#define SCCB_EVENT_PENDING 0x3
1300
1301static int __inject_service(struct kvm *kvm,
1302 struct kvm_s390_interrupt_info *inti)
1303{
1304 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1305
1306 spin_lock(&fi->lock);
1307 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1308 /*
1309 * Early versions of the QEMU s390 bios will inject several
1310 * service interrupts after another without handling a
1311 * condition code indicating busy.
1312 * We will silently ignore those superfluous sccb values.
1313 * A future version of QEMU will take care of serialization
1314 * of servc requests
1315 */
1316 if (fi->srv_signal.ext_params & SCCB_MASK)
1317 goto out;
1318 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1319 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1320out:
1321 spin_unlock(&fi->lock);
1322 kfree(inti);
1323 return 0;
1324}
1325
1326static int __inject_virtio(struct kvm *kvm,
1327 struct kvm_s390_interrupt_info *inti)
1328{
1329 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1330
1331 spin_lock(&fi->lock);
1332 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1333 spin_unlock(&fi->lock);
1334 return -EBUSY;
1335 }
1336 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1337 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1338 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1339 spin_unlock(&fi->lock);
1340 return 0;
1341}
1342
1343static int __inject_pfault_done(struct kvm *kvm,
1344 struct kvm_s390_interrupt_info *inti)
1345{
1346 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1347
1348 spin_lock(&fi->lock);
1349 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1350 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1351 spin_unlock(&fi->lock);
1352 return -EBUSY;
1353 }
1354 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1355 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1356 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1357 spin_unlock(&fi->lock);
1358 return 0;
1359}
1360
1361#define CR_PENDING_SUBCLASS 28
1362static int __inject_float_mchk(struct kvm *kvm,
1363 struct kvm_s390_interrupt_info *inti)
1364{
1365 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1366
1367 spin_lock(&fi->lock);
1368 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1369 fi->mchk.mcic |= inti->mchk.mcic;
1370 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1371 spin_unlock(&fi->lock);
1372 kfree(inti);
1373 return 0;
1374}
1375
1376static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001377{
1378 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001379 struct list_head *list;
1380 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001381
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001382 fi = &kvm->arch.float_int;
1383 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001384 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1385 spin_unlock(&fi->lock);
1386 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001387 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001388 fi->counters[FIRQ_CNTR_IO] += 1;
1389
1390 isc = int_word_to_isc(inti->io.io_int_word);
1391 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1392 list_add_tail(&inti->list, list);
1393 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001394 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001395 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001396}
1397
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001398/*
1399 * Find a destination VCPU for a floating irq and kick it.
1400 */
1401static void __floating_irq_kick(struct kvm *kvm, u64 type)
1402{
1403 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1404 struct kvm_s390_local_interrupt *li;
1405 struct kvm_vcpu *dst_vcpu;
1406 int sigcpu, online_vcpus, nr_tries = 0;
1407
1408 online_vcpus = atomic_read(&kvm->online_vcpus);
1409 if (!online_vcpus)
1410 return;
1411
1412 /* find idle VCPUs first, then round robin */
1413 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1414 if (sigcpu == online_vcpus) {
1415 do {
1416 sigcpu = fi->next_rr_cpu;
1417 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1418 /* avoid endless loops if all vcpus are stopped */
1419 if (nr_tries++ >= online_vcpus)
1420 return;
1421 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1422 }
1423 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1424
1425 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1426 li = &dst_vcpu->arch.local_int;
1427 spin_lock(&li->lock);
1428 switch (type) {
1429 case KVM_S390_MCHK:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001430 atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001431 break;
1432 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001433 atomic_or(CPUSTAT_IO_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001434 break;
1435 default:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001436 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001437 break;
1438 }
1439 spin_unlock(&li->lock);
1440 kvm_s390_vcpu_wakeup(dst_vcpu);
1441}
1442
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001443static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001444{
Jens Freimann6d3da242013-07-03 15:18:35 +02001445 u64 type = READ_ONCE(inti->type);
1446 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001447
Jens Freimann6d3da242013-07-03 15:18:35 +02001448 switch (type) {
1449 case KVM_S390_MCHK:
1450 rc = __inject_float_mchk(kvm, inti);
1451 break;
1452 case KVM_S390_INT_VIRTIO:
1453 rc = __inject_virtio(kvm, inti);
1454 break;
1455 case KVM_S390_INT_SERVICE:
1456 rc = __inject_service(kvm, inti);
1457 break;
1458 case KVM_S390_INT_PFAULT_DONE:
1459 rc = __inject_pfault_done(kvm, inti);
1460 break;
1461 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1462 rc = __inject_io(kvm, inti);
1463 break;
1464 default:
1465 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001466 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001467 if (rc)
1468 return rc;
1469
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001470 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001471 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001472}
1473
1474int kvm_s390_inject_vm(struct kvm *kvm,
1475 struct kvm_s390_interrupt *s390int)
1476{
1477 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001478 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001479
1480 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1481 if (!inti)
1482 return -ENOMEM;
1483
1484 inti->type = s390int->type;
1485 switch (inti->type) {
1486 case KVM_S390_INT_VIRTIO:
1487 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1488 s390int->parm, s390int->parm64);
1489 inti->ext.ext_params = s390int->parm;
1490 inti->ext.ext_params2 = s390int->parm64;
1491 break;
1492 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001493 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001494 inti->ext.ext_params = s390int->parm;
1495 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001496 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001497 inti->ext.ext_params2 = s390int->parm64;
1498 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001499 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001500 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001501 s390int->parm64);
1502 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1503 inti->mchk.mcic = s390int->parm64;
1504 break;
1505 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
David Hildenbranda37281b2014-11-21 13:45:08 +01001506 if (inti->type & KVM_S390_INT_IO_AI_MASK)
Jens Freimannc05c4182013-10-07 16:13:45 +02001507 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1508 else
1509 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1510 s390int->type & IOINT_CSSID_MASK,
1511 s390int->type & IOINT_SSID_MASK,
1512 s390int->type & IOINT_SCHID_MASK);
1513 inti->io.subchannel_id = s390int->parm >> 16;
1514 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1515 inti->io.io_int_parm = s390int->parm64 >> 32;
1516 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1517 break;
1518 default:
1519 kfree(inti);
1520 return -EINVAL;
1521 }
1522 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1523 2);
1524
David Hildenbrand428d53b2015-01-16 12:58:09 +01001525 rc = __inject_vm(kvm, inti);
1526 if (rc)
1527 kfree(inti);
1528 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001529}
1530
David Hildenbrand15462e32015-02-04 15:59:11 +01001531int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001532 struct kvm_s390_interrupt_info *inti)
1533{
David Hildenbrand15462e32015-02-04 15:59:11 +01001534 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001535}
1536
Jens Freimann383d0b02014-07-29 15:11:49 +02001537int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1538 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001539{
Jens Freimann383d0b02014-07-29 15:11:49 +02001540 irq->type = s390int->type;
1541 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001542 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001543 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001544 return -EINVAL;
1545 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001546 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001547 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001548 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001549 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001550 case KVM_S390_SIGP_STOP:
1551 irq->u.stop.flags = s390int->parm;
1552 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001553 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001554 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001555 return -EINVAL;
1556 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001557 break;
1558 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001559 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001560 return -EINVAL;
1561 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001562 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001563 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001564 irq->u.mchk.mcic = s390int->parm64;
1565 break;
1566 }
1567 return 0;
1568}
1569
David Hildenbrand6cddd432014-10-15 16:48:53 +02001570int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1571{
1572 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1573
1574 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1575}
1576
1577void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1578{
1579 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1580
1581 spin_lock(&li->lock);
1582 li->irq.stop.flags = 0;
1583 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1584 spin_unlock(&li->lock);
1585}
1586
Jens Freimann79e87a12015-03-19 15:12:12 +01001587static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001588{
Jens Freimann383d0b02014-07-29 15:11:49 +02001589 int rc;
1590
Jens Freimann383d0b02014-07-29 15:11:49 +02001591 switch (irq->type) {
1592 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001593 rc = __inject_prog(vcpu, irq);
1594 break;
1595 case KVM_S390_SIGP_SET_PREFIX:
1596 rc = __inject_set_prefix(vcpu, irq);
1597 break;
1598 case KVM_S390_SIGP_STOP:
1599 rc = __inject_sigp_stop(vcpu, irq);
1600 break;
1601 case KVM_S390_RESTART:
1602 rc = __inject_sigp_restart(vcpu, irq);
1603 break;
1604 case KVM_S390_INT_CLOCK_COMP:
1605 rc = __inject_ckc(vcpu);
1606 break;
1607 case KVM_S390_INT_CPU_TIMER:
1608 rc = __inject_cpu_timer(vcpu);
1609 break;
1610 case KVM_S390_INT_EXTERNAL_CALL:
1611 rc = __inject_extcall(vcpu, irq);
1612 break;
1613 case KVM_S390_INT_EMERGENCY:
1614 rc = __inject_sigp_emergency(vcpu, irq);
1615 break;
1616 case KVM_S390_MCHK:
1617 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001618 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001619 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001620 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001621 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001622 case KVM_S390_INT_VIRTIO:
1623 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001624 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001625 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001626 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001627 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001628
1629 return rc;
1630}
1631
1632int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1633{
1634 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1635 int rc;
1636
1637 spin_lock(&li->lock);
1638 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001639 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001640 if (!rc)
1641 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001642 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001643}
Jens Freimannc05c4182013-10-07 16:13:45 +02001644
Jens Freimann6d3da242013-07-03 15:18:35 +02001645static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001646{
Jens Freimann6d3da242013-07-03 15:18:35 +02001647 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001648
Jens Freimann6d3da242013-07-03 15:18:35 +02001649 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001650 list_del(&inti->list);
1651 kfree(inti);
1652 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001653}
1654
Jens Freimann94aa0332015-03-16 12:17:13 +01001655static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1656 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001657{
Jens Freimann94aa0332015-03-16 12:17:13 +01001658 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001659 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001660 case KVM_S390_INT_PFAULT_INIT:
1661 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001662 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001663 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001664 break;
1665 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001666 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001667 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001668 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001669}
1670
Jens Freimann6d3da242013-07-03 15:18:35 +02001671void kvm_s390_clear_float_irqs(struct kvm *kvm)
1672{
1673 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1674 int i;
1675
1676 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001677 fi->pending_irqs = 0;
1678 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1679 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001680 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1681 clear_irq_list(&fi->lists[i]);
1682 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1683 fi->counters[i] = 0;
1684 spin_unlock(&fi->lock);
1685};
1686
Jens Freimann94aa0332015-03-16 12:17:13 +01001687static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001688{
1689 struct kvm_s390_interrupt_info *inti;
1690 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001691 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001692 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001693 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001694 int ret = 0;
1695 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001696 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001697
Jens Freimann94aa0332015-03-16 12:17:13 +01001698 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1699 return -EINVAL;
1700
1701 /*
1702 * We are already using -ENOMEM to signal
1703 * userspace it may retry with a bigger buffer,
1704 * so we need to use something else for this case
1705 */
1706 buf = vzalloc(len);
1707 if (!buf)
1708 return -ENOBUFS;
1709
1710 max_irqs = len / sizeof(struct kvm_s390_irq);
1711
Jens Freimannc05c4182013-10-07 16:13:45 +02001712 fi = &kvm->arch.float_int;
1713 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001714 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1715 list_for_each_entry(inti, &fi->lists[i], list) {
1716 if (n == max_irqs) {
1717 /* signal userspace to try again */
1718 ret = -ENOMEM;
1719 goto out;
1720 }
1721 inti_to_irq(inti, &buf[n]);
1722 n++;
1723 }
1724 }
1725 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001726 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001727 /* signal userspace to try again */
1728 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001729 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001730 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001731 irq = (struct kvm_s390_irq *) &buf[n];
1732 irq->type = KVM_S390_INT_SERVICE;
1733 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001734 n++;
1735 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001736 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1737 if (n == max_irqs) {
1738 /* signal userspace to try again */
1739 ret = -ENOMEM;
1740 goto out;
1741 }
1742 irq = (struct kvm_s390_irq *) &buf[n];
1743 irq->type = KVM_S390_MCHK;
1744 irq->u.mchk = fi->mchk;
1745 n++;
1746}
1747
1748out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001749 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001750 if (!ret && n > 0) {
1751 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1752 ret = -EFAULT;
1753 }
1754 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001755
1756 return ret < 0 ? ret : n;
1757}
1758
1759static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1760{
1761 int r;
1762
1763 switch (attr->group) {
1764 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001765 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001766 attr->attr);
1767 break;
1768 default:
1769 r = -EINVAL;
1770 }
1771
1772 return r;
1773}
1774
1775static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1776 u64 addr)
1777{
1778 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1779 void *target = NULL;
1780 void __user *source;
1781 u64 size;
1782
1783 if (get_user(inti->type, (u64 __user *)addr))
1784 return -EFAULT;
1785
1786 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001787 case KVM_S390_INT_PFAULT_INIT:
1788 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001789 case KVM_S390_INT_VIRTIO:
1790 case KVM_S390_INT_SERVICE:
1791 target = (void *) &inti->ext;
1792 source = &uptr->u.ext;
1793 size = sizeof(inti->ext);
1794 break;
1795 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1796 target = (void *) &inti->io;
1797 source = &uptr->u.io;
1798 size = sizeof(inti->io);
1799 break;
1800 case KVM_S390_MCHK:
1801 target = (void *) &inti->mchk;
1802 source = &uptr->u.mchk;
1803 size = sizeof(inti->mchk);
1804 break;
1805 default:
1806 return -EINVAL;
1807 }
1808
1809 if (copy_from_user(target, source, size))
1810 return -EFAULT;
1811
1812 return 0;
1813}
1814
1815static int enqueue_floating_irq(struct kvm_device *dev,
1816 struct kvm_device_attr *attr)
1817{
1818 struct kvm_s390_interrupt_info *inti = NULL;
1819 int r = 0;
1820 int len = attr->attr;
1821
1822 if (len % sizeof(struct kvm_s390_irq) != 0)
1823 return -EINVAL;
1824 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1825 return -EINVAL;
1826
1827 while (len >= sizeof(struct kvm_s390_irq)) {
1828 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1829 if (!inti)
1830 return -ENOMEM;
1831
1832 r = copy_irq_from_user(inti, attr->addr);
1833 if (r) {
1834 kfree(inti);
1835 return r;
1836 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001837 r = __inject_vm(dev->kvm, inti);
1838 if (r) {
1839 kfree(inti);
1840 return r;
1841 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001842 len -= sizeof(struct kvm_s390_irq);
1843 attr->addr += sizeof(struct kvm_s390_irq);
1844 }
1845
1846 return r;
1847}
1848
Cornelia Huck841b91c2013-07-15 13:36:01 +02001849static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1850{
1851 if (id >= MAX_S390_IO_ADAPTERS)
1852 return NULL;
1853 return kvm->arch.adapters[id];
1854}
1855
1856static int register_io_adapter(struct kvm_device *dev,
1857 struct kvm_device_attr *attr)
1858{
1859 struct s390_io_adapter *adapter;
1860 struct kvm_s390_io_adapter adapter_info;
1861
1862 if (copy_from_user(&adapter_info,
1863 (void __user *)attr->addr, sizeof(adapter_info)))
1864 return -EFAULT;
1865
1866 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1867 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1868 return -EINVAL;
1869
1870 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1871 if (!adapter)
1872 return -ENOMEM;
1873
1874 INIT_LIST_HEAD(&adapter->maps);
1875 init_rwsem(&adapter->maps_lock);
1876 atomic_set(&adapter->nr_maps, 0);
1877 adapter->id = adapter_info.id;
1878 adapter->isc = adapter_info.isc;
1879 adapter->maskable = adapter_info.maskable;
1880 adapter->masked = false;
1881 adapter->swap = adapter_info.swap;
1882 dev->kvm->arch.adapters[adapter->id] = adapter;
1883
1884 return 0;
1885}
1886
1887int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1888{
1889 int ret;
1890 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1891
1892 if (!adapter || !adapter->maskable)
1893 return -EINVAL;
1894 ret = adapter->masked;
1895 adapter->masked = masked;
1896 return ret;
1897}
1898
1899static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1900{
1901 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1902 struct s390_map_info *map;
1903 int ret;
1904
1905 if (!adapter || !addr)
1906 return -EINVAL;
1907
1908 map = kzalloc(sizeof(*map), GFP_KERNEL);
1909 if (!map) {
1910 ret = -ENOMEM;
1911 goto out;
1912 }
1913 INIT_LIST_HEAD(&map->list);
1914 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02001915 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001916 if (map->addr == -EFAULT) {
1917 ret = -EFAULT;
1918 goto out;
1919 }
1920 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1921 if (ret < 0)
1922 goto out;
1923 BUG_ON(ret != 1);
1924 down_write(&adapter->maps_lock);
1925 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1926 list_add_tail(&map->list, &adapter->maps);
1927 ret = 0;
1928 } else {
1929 put_page(map->page);
1930 ret = -EINVAL;
1931 }
1932 up_write(&adapter->maps_lock);
1933out:
1934 if (ret)
1935 kfree(map);
1936 return ret;
1937}
1938
1939static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1940{
1941 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1942 struct s390_map_info *map, *tmp;
1943 int found = 0;
1944
1945 if (!adapter || !addr)
1946 return -EINVAL;
1947
1948 down_write(&adapter->maps_lock);
1949 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1950 if (map->guest_addr == addr) {
1951 found = 1;
1952 atomic_dec(&adapter->nr_maps);
1953 list_del(&map->list);
1954 put_page(map->page);
1955 kfree(map);
1956 break;
1957 }
1958 }
1959 up_write(&adapter->maps_lock);
1960
1961 return found ? 0 : -EINVAL;
1962}
1963
1964void kvm_s390_destroy_adapters(struct kvm *kvm)
1965{
1966 int i;
1967 struct s390_map_info *map, *tmp;
1968
1969 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1970 if (!kvm->arch.adapters[i])
1971 continue;
1972 list_for_each_entry_safe(map, tmp,
1973 &kvm->arch.adapters[i]->maps, list) {
1974 list_del(&map->list);
1975 put_page(map->page);
1976 kfree(map);
1977 }
1978 kfree(kvm->arch.adapters[i]);
1979 }
1980}
1981
1982static int modify_io_adapter(struct kvm_device *dev,
1983 struct kvm_device_attr *attr)
1984{
1985 struct kvm_s390_io_adapter_req req;
1986 struct s390_io_adapter *adapter;
1987 int ret;
1988
1989 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1990 return -EFAULT;
1991
1992 adapter = get_io_adapter(dev->kvm, req.id);
1993 if (!adapter)
1994 return -EINVAL;
1995 switch (req.type) {
1996 case KVM_S390_IO_ADAPTER_MASK:
1997 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1998 if (ret > 0)
1999 ret = 0;
2000 break;
2001 case KVM_S390_IO_ADAPTER_MAP:
2002 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2003 break;
2004 case KVM_S390_IO_ADAPTER_UNMAP:
2005 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2006 break;
2007 default:
2008 ret = -EINVAL;
2009 }
2010
2011 return ret;
2012}
2013
Jens Freimannc05c4182013-10-07 16:13:45 +02002014static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2015{
2016 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002017 unsigned int i;
2018 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02002019
2020 switch (attr->group) {
2021 case KVM_DEV_FLIC_ENQUEUE:
2022 r = enqueue_floating_irq(dev, attr);
2023 break;
2024 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01002025 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02002026 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002027 case KVM_DEV_FLIC_APF_ENABLE:
2028 dev->kvm->arch.gmap->pfault_enabled = 1;
2029 break;
2030 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2031 dev->kvm->arch.gmap->pfault_enabled = 0;
2032 /*
2033 * Make sure no async faults are in transition when
2034 * clearing the queues. So we don't need to worry
2035 * about late coming workers.
2036 */
2037 synchronize_srcu(&dev->kvm->srcu);
2038 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2039 kvm_clear_async_pf_completion_queue(vcpu);
2040 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002041 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2042 r = register_io_adapter(dev, attr);
2043 break;
2044 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2045 r = modify_io_adapter(dev, attr);
2046 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002047 default:
2048 r = -EINVAL;
2049 }
2050
2051 return r;
2052}
2053
2054static int flic_create(struct kvm_device *dev, u32 type)
2055{
2056 if (!dev)
2057 return -EINVAL;
2058 if (dev->kvm->arch.flic)
2059 return -EINVAL;
2060 dev->kvm->arch.flic = dev;
2061 return 0;
2062}
2063
2064static void flic_destroy(struct kvm_device *dev)
2065{
2066 dev->kvm->arch.flic = NULL;
2067 kfree(dev);
2068}
2069
2070/* s390 floating irq controller (flic) */
2071struct kvm_device_ops kvm_flic_ops = {
2072 .name = "kvm-flic",
2073 .get_attr = flic_get_attr,
2074 .set_attr = flic_set_attr,
2075 .create = flic_create,
2076 .destroy = flic_destroy,
2077};
Cornelia Huck84223592013-07-15 13:36:01 +02002078
2079static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2080{
2081 unsigned long bit;
2082
2083 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2084
2085 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2086}
2087
2088static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2089 u64 addr)
2090{
2091 struct s390_map_info *map;
2092
2093 if (!adapter)
2094 return NULL;
2095
2096 list_for_each_entry(map, &adapter->maps, list) {
2097 if (map->guest_addr == addr)
2098 return map;
2099 }
2100 return NULL;
2101}
2102
2103static int adapter_indicators_set(struct kvm *kvm,
2104 struct s390_io_adapter *adapter,
2105 struct kvm_s390_adapter_int *adapter_int)
2106{
2107 unsigned long bit;
2108 int summary_set, idx;
2109 struct s390_map_info *info;
2110 void *map;
2111
2112 info = get_map_info(adapter, adapter_int->ind_addr);
2113 if (!info)
2114 return -1;
2115 map = page_address(info->page);
2116 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2117 set_bit(bit, map);
2118 idx = srcu_read_lock(&kvm->srcu);
2119 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2120 set_page_dirty_lock(info->page);
2121 info = get_map_info(adapter, adapter_int->summary_addr);
2122 if (!info) {
2123 srcu_read_unlock(&kvm->srcu, idx);
2124 return -1;
2125 }
2126 map = page_address(info->page);
2127 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2128 adapter->swap);
2129 summary_set = test_and_set_bit(bit, map);
2130 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2131 set_page_dirty_lock(info->page);
2132 srcu_read_unlock(&kvm->srcu, idx);
2133 return summary_set ? 0 : 1;
2134}
2135
2136/*
2137 * < 0 - not injected due to error
2138 * = 0 - coalesced, summary indicator already active
2139 * > 0 - injected interrupt
2140 */
2141static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2142 struct kvm *kvm, int irq_source_id, int level,
2143 bool line_status)
2144{
2145 int ret;
2146 struct s390_io_adapter *adapter;
2147
2148 /* We're only interested in the 0->1 transition. */
2149 if (!level)
2150 return 0;
2151 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2152 if (!adapter)
2153 return -1;
2154 down_read(&adapter->maps_lock);
2155 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2156 up_read(&adapter->maps_lock);
2157 if ((ret > 0) && !adapter->masked) {
2158 struct kvm_s390_interrupt s390int = {
2159 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2160 .parm = 0,
2161 .parm64 = (adapter->isc << 27) | 0x80000000,
2162 };
2163 ret = kvm_s390_inject_vm(kvm, &s390int);
2164 if (ret == 0)
2165 ret = 1;
2166 }
2167 return ret;
2168}
2169
Paul Mackerras8ba918d2014-06-30 20:51:10 +10002170int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002171 const struct kvm_irq_routing_entry *ue)
2172{
2173 int ret;
2174
2175 switch (ue->type) {
2176 case KVM_IRQ_ROUTING_S390_ADAPTER:
2177 e->set = set_adapter_int;
2178 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2179 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2180 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2181 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2182 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2183 ret = 0;
2184 break;
2185 default:
2186 ret = -EINVAL;
2187 }
2188
2189 return ret;
2190}
2191
2192int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2193 int irq_source_id, int level, bool line_status)
2194{
2195 return -EINVAL;
2196}
Jens Freimann816c7662014-11-24 17:13:46 +01002197
2198int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2199{
2200 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2201 struct kvm_s390_irq *buf;
2202 int r = 0;
2203 int n;
2204
2205 buf = vmalloc(len);
2206 if (!buf)
2207 return -ENOMEM;
2208
2209 if (copy_from_user((void *) buf, irqstate, len)) {
2210 r = -EFAULT;
2211 goto out_free;
2212 }
2213
2214 /*
2215 * Don't allow setting the interrupt state
2216 * when there are already interrupts pending
2217 */
2218 spin_lock(&li->lock);
2219 if (li->pending_irqs) {
2220 r = -EBUSY;
2221 goto out_unlock;
2222 }
2223
2224 for (n = 0; n < len / sizeof(*buf); n++) {
2225 r = do_inject_vcpu(vcpu, &buf[n]);
2226 if (r)
2227 break;
2228 }
2229
2230out_unlock:
2231 spin_unlock(&li->lock);
2232out_free:
2233 vfree(buf);
2234
2235 return r;
2236}
2237
2238static void store_local_irq(struct kvm_s390_local_interrupt *li,
2239 struct kvm_s390_irq *irq,
2240 unsigned long irq_type)
2241{
2242 switch (irq_type) {
2243 case IRQ_PEND_MCHK_EX:
2244 case IRQ_PEND_MCHK_REP:
2245 irq->type = KVM_S390_MCHK;
2246 irq->u.mchk = li->irq.mchk;
2247 break;
2248 case IRQ_PEND_PROG:
2249 irq->type = KVM_S390_PROGRAM_INT;
2250 irq->u.pgm = li->irq.pgm;
2251 break;
2252 case IRQ_PEND_PFAULT_INIT:
2253 irq->type = KVM_S390_INT_PFAULT_INIT;
2254 irq->u.ext = li->irq.ext;
2255 break;
2256 case IRQ_PEND_EXT_EXTERNAL:
2257 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2258 irq->u.extcall = li->irq.extcall;
2259 break;
2260 case IRQ_PEND_EXT_CLOCK_COMP:
2261 irq->type = KVM_S390_INT_CLOCK_COMP;
2262 break;
2263 case IRQ_PEND_EXT_CPU_TIMER:
2264 irq->type = KVM_S390_INT_CPU_TIMER;
2265 break;
2266 case IRQ_PEND_SIGP_STOP:
2267 irq->type = KVM_S390_SIGP_STOP;
2268 irq->u.stop = li->irq.stop;
2269 break;
2270 case IRQ_PEND_RESTART:
2271 irq->type = KVM_S390_RESTART;
2272 break;
2273 case IRQ_PEND_SET_PREFIX:
2274 irq->type = KVM_S390_SIGP_SET_PREFIX;
2275 irq->u.prefix = li->irq.prefix;
2276 break;
2277 }
2278}
2279
2280int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2281{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002282 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002283 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2284 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2285 unsigned long pending_irqs;
2286 struct kvm_s390_irq irq;
2287 unsigned long irq_type;
2288 int cpuaddr;
2289 int n = 0;
2290
2291 spin_lock(&li->lock);
2292 pending_irqs = li->pending_irqs;
2293 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2294 sizeof(sigp_emerg_pending));
2295 spin_unlock(&li->lock);
2296
2297 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2298 memset(&irq, 0, sizeof(irq));
2299 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2300 continue;
2301 if (n + sizeof(irq) > len)
2302 return -ENOBUFS;
2303 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2304 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2305 return -EFAULT;
2306 n += sizeof(irq);
2307 }
2308
2309 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2310 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2311 memset(&irq, 0, sizeof(irq));
2312 if (n + sizeof(irq) > len)
2313 return -ENOBUFS;
2314 irq.type = KVM_S390_INT_EMERGENCY;
2315 irq.u.emerg.code = cpuaddr;
2316 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2317 return -EFAULT;
2318 n += sizeof(irq);
2319 }
2320 }
2321
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002322 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002323 if (n + sizeof(irq) > len)
2324 return -ENOBUFS;
2325 memset(&irq, 0, sizeof(irq));
2326 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002327 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002328 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2329 return -EFAULT;
2330 n += sizeof(irq);
2331 }
2332
2333 return n;
2334}