blob: dd4c50b82a532038ab78e1cd9d1e61e55f86fd6c [file] [log] [blame]
Greg Kroah-Hartmand809aa22017-11-24 15:00:33 +01001// SPDX-License-Identifier: GPL-2.0
Carsten Otteba5c1e92008-03-25 18:47:26 +01002/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02003 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01004 *
Thomas Huth33b412a2015-02-11 10:38:46 +01005 * Copyright IBM Corp. 2008, 2015
Carsten Otteba5c1e92008-03-25 18:47:26 +01006 *
Carsten Otteba5c1e92008-03-25 18:47:26 +01007 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 */
9
Christian Borntraegerca872302009-05-12 17:21:49 +020010#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010011#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010012#include <linux/hrtimer.h>
Cornelia Huck84223592013-07-15 13:36:01 +020013#include <linux/mmu_context.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020014#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Jens Freimann383d0b02014-07-29 15:11:49 +020016#include <linux/bitmap.h>
Jens Freimann94aa0332015-03-16 12:17:13 +010017#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010018#include <asm/asm-offsets.h>
Thomas Huth33b412a2015-02-11 10:38:46 +010019#include <asm/dis.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
David Hildenbrandea5f4962014-10-14 15:29:30 +020021#include <asm/sclp.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020022#include <asm/isc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010023#include <asm/gmap.h>
David Hildenbrand0319dae2016-08-03 11:18:57 +020024#include <asm/switch_to.h>
David Hildenbrandff5dc142015-10-14 16:57:56 +020025#include <asm/nmi.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
Jens Freimann44c6ca32014-04-16 13:57:18 +020030#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010031#define PFAULT_DONE 0x0680
32#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010033
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020034/* handle external calls via sigp interpretation facility */
35static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id)
36{
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020037 int c, scn;
38
David Hildenbrand8d5fb0d2018-01-23 18:05:31 +010039 if (!kvm_s390_test_cpuflags(vcpu, CPUSTAT_ECALL_PEND))
David Hildenbrand2c1bb2b2015-09-23 09:45:50 +020040 return 0;
41
David Hildenbranda6940672016-08-08 22:39:32 +020042 BUG_ON(!kvm_s390_use_sca_entries());
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020043 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020044 if (vcpu->kvm->arch.use_esca) {
45 struct esca_block *sca = vcpu->kvm->arch.sca;
46 union esca_sigp_ctrl sigp_ctrl =
47 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
48
49 c = sigp_ctrl.c;
50 scn = sigp_ctrl.scn;
51 } else {
52 struct bsca_block *sca = vcpu->kvm->arch.sca;
53 union bsca_sigp_ctrl sigp_ctrl =
54 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
55
56 c = sigp_ctrl.c;
57 scn = sigp_ctrl.scn;
58 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020059 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020060
61 if (src_id)
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020062 *src_id = scn;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020063
David Hildenbrand2c1bb2b2015-09-23 09:45:50 +020064 return c;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020065}
66
67static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
68{
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020069 int expect, rc;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020070
David Hildenbranda6940672016-08-08 22:39:32 +020071 BUG_ON(!kvm_s390_use_sca_entries());
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020072 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020073 if (vcpu->kvm->arch.use_esca) {
74 struct esca_block *sca = vcpu->kvm->arch.sca;
75 union esca_sigp_ctrl *sigp_ctrl =
76 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
77 union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020078
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020079 new_val.scn = src_id;
80 new_val.c = 1;
81 old_val.c = 0;
82
83 expect = old_val.value;
84 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
85 } else {
86 struct bsca_block *sca = vcpu->kvm->arch.sca;
87 union bsca_sigp_ctrl *sigp_ctrl =
88 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
89 union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
90
91 new_val.scn = src_id;
92 new_val.c = 1;
93 old_val.c = 0;
94
95 expect = old_val.value;
96 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
97 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020098 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020099
100 if (rc != expect) {
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200101 /* another external call is pending */
102 return -EBUSY;
103 }
David Hildenbrandef8f4f42018-01-23 18:05:29 +0100104 kvm_s390_set_cpuflags(vcpu, CPUSTAT_ECALL_PEND);
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200105 return 0;
106}
107
108static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
109{
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +0200110 int rc, expect;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200111
David Hildenbranda6940672016-08-08 22:39:32 +0200112 if (!kvm_s390_use_sca_entries())
113 return;
David Hildenbrand9daecfc2018-01-23 18:05:30 +0100114 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_ECALL_PEND);
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{
Michael Muellerc7901a62017-06-29 18:39:27 +0200192 return ((irq_type >= IRQ_PEND_IO_ISC_7) &&
193 (irq_type <= IRQ_PEND_IO_ISC_0));
Jens Freimann6d3da242013-07-03 15:18:35 +0200194}
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
Michael Mueller2496c8e2017-08-31 11:10:28 +0200201static inline u32 isc_to_int_word(u8 isc)
202{
203 return ((u32)isc << 27) | 0x80000000;
204}
205
Jens Freimann6d3da242013-07-03 15:18:35 +0200206static inline u8 int_word_to_isc(u32 int_word)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100207{
Jens Freimann6d3da242013-07-03 15:18:35 +0200208 return (int_word & 0x38000000) >> 27;
209}
210
Michael Muellerd77e6412017-06-12 12:37:57 +0200211/*
212 * To use atomic bitmap functions, we have to provide a bitmap address
213 * that is u64 aligned. However, the ipm might be u32 aligned.
214 * Therefore, we logically start the bitmap at the very beginning of the
215 * struct and fixup the bit number.
216 */
217#define IPM_BIT_OFFSET (offsetof(struct kvm_s390_gisa, ipm) * BITS_PER_BYTE)
218
219static inline void kvm_s390_gisa_set_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
220{
221 set_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
222}
223
224static inline u8 kvm_s390_gisa_get_ipm(struct kvm_s390_gisa *gisa)
225{
226 return READ_ONCE(gisa->ipm);
227}
228
229static inline void kvm_s390_gisa_clear_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
230{
231 clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
232}
233
234static inline int kvm_s390_gisa_tac_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
235{
236 return test_and_clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
237}
238
David Hildenbrand5f94c582015-09-28 14:27:51 +0200239static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
Jens Freimann6d3da242013-07-03 15:18:35 +0200240{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200241 return vcpu->kvm->arch.float_int.pending_irqs |
Michael Muellerd7c5cb02017-06-12 14:15:19 +0200242 vcpu->arch.local_int.pending_irqs |
243 kvm_s390_gisa_get_ipm(vcpu->kvm->arch.gisa) << IRQ_PEND_IO_ISC_7;
Jens Freimann383d0b02014-07-29 15:11:49 +0200244}
245
Michael Muelleree739f42017-07-03 15:32:50 +0200246static inline int isc_to_irq_type(unsigned long isc)
247{
Michael Muellerc7901a62017-06-29 18:39:27 +0200248 return IRQ_PEND_IO_ISC_0 - isc;
Michael Muelleree739f42017-07-03 15:32:50 +0200249}
250
251static inline int irq_type_to_isc(unsigned long irq_type)
252{
Michael Muellerc7901a62017-06-29 18:39:27 +0200253 return IRQ_PEND_IO_ISC_0 - irq_type;
Michael Muelleree739f42017-07-03 15:32:50 +0200254}
255
Jens Freimann6d3da242013-07-03 15:18:35 +0200256static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
257 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200258{
Jens Freimann6d3da242013-07-03 15:18:35 +0200259 int i;
260
261 for (i = 0; i <= MAX_ISC; i++)
262 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
Michael Muelleree739f42017-07-03 15:32:50 +0200263 active_mask &= ~(1UL << (isc_to_irq_type(i)));
Jens Freimann6d3da242013-07-03 15:18:35 +0200264
265 return active_mask;
266}
267
268static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
269{
270 unsigned long active_mask;
271
David Hildenbrand5f94c582015-09-28 14:27:51 +0200272 active_mask = pending_irqs(vcpu);
Jens Freimannffeca0a2015-04-17 10:21:04 +0200273 if (!active_mask)
274 return 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200275
276 if (psw_extint_disabled(vcpu))
277 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200278 if (psw_ioint_disabled(vcpu))
279 active_mask &= ~IRQ_PEND_IO_MASK;
280 else
281 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200282 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
283 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
284 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
285 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
286 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
287 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
288 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
289 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200290 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
291 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200292 if (psw_mchk_disabled(vcpu))
293 active_mask &= ~IRQ_PEND_MCHK_MASK;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +0200294 /*
295 * Check both floating and local interrupt's cr14 because
296 * bit IRQ_PEND_MCHK_REP could be set in both cases.
297 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200298 if (!(vcpu->arch.sie_block->gcr[14] &
QingFeng Hao4d62fcc2017-06-07 12:03:05 +0200299 (vcpu->kvm->arch.float_int.mchk.cr14 |
300 vcpu->arch.local_int.irq.mchk.cr14)))
Jens Freimann6d3da242013-07-03 15:18:35 +0200301 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200302
David Hildenbrand6cddd432014-10-15 16:48:53 +0200303 /*
304 * STOP irqs will never be actively delivered. They are triggered via
305 * intercept requests and cleared when the stop intercept is performed.
306 */
307 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
308
Jens Freimann383d0b02014-07-29 15:11:49 +0200309 return active_mask;
310}
311
Carsten Otteba5c1e92008-03-25 18:47:26 +0100312static void __set_cpu_idle(struct kvm_vcpu *vcpu)
313{
David Hildenbrandef8f4f42018-01-23 18:05:29 +0100314 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
David Hildenbranda9f6c9a2018-01-08 20:37:47 +0100315 set_bit(vcpu->vcpu_id, vcpu->kvm->arch.float_int.idle_mask);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100316}
317
318static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
319{
David Hildenbrand9daecfc2018-01-23 18:05:30 +0100320 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
David Hildenbranda9f6c9a2018-01-08 20:37:47 +0100321 clear_bit(vcpu->vcpu_id, vcpu->kvm->arch.float_int.idle_mask);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100322}
323
324static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
325{
David Hildenbrand9daecfc2018-01-23 18:05:30 +0100326 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IO_INT | CPUSTAT_EXT_INT |
327 CPUSTAT_STOP_INT);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100328 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100329 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
330
331 if (guestdbg_enabled(vcpu)) {
332 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
333 LCTL_CR10 | LCTL_CR11);
334 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
335 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100336}
337
Jens Freimann6d3da242013-07-03 15:18:35 +0200338static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
339{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200340 if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
Jens Freimann6d3da242013-07-03 15:18:35 +0200341 return;
342 else if (psw_ioint_disabled(vcpu))
David Hildenbrand20182242018-01-23 18:05:28 +0100343 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IO_INT);
Jens Freimann6d3da242013-07-03 15:18:35 +0200344 else
345 vcpu->arch.sie_block->lctl |= LCTL_CR6;
346}
347
Jens Freimann383d0b02014-07-29 15:11:49 +0200348static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
349{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200350 if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200351 return;
352 if (psw_extint_disabled(vcpu))
David Hildenbrand20182242018-01-23 18:05:28 +0100353 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
Jens Freimann383d0b02014-07-29 15:11:49 +0200354 else
355 vcpu->arch.sie_block->lctl |= LCTL_CR0;
356}
357
358static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
359{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200360 if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200361 return;
362 if (psw_mchk_disabled(vcpu))
363 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
364 else
365 vcpu->arch.sie_block->lctl |= LCTL_CR14;
366}
367
David Hildenbrand6cddd432014-10-15 16:48:53 +0200368static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
369{
370 if (kvm_s390_is_stop_irq_pending(vcpu))
David Hildenbrand20182242018-01-23 18:05:28 +0100371 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200372}
373
Jens Freimann6d3da242013-07-03 15:18:35 +0200374/* Set interception request for non-deliverable interrupts */
375static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200376{
Jens Freimann6d3da242013-07-03 15:18:35 +0200377 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200378 set_intercept_indicators_ext(vcpu);
379 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200380 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200381}
382
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200383static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100384{
Jens Freimann383d0b02014-07-29 15:11:49 +0200385 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200386 int rc;
387
388 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
389 0, 0);
390
391 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
392 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100393 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200394 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
395 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
396 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
397 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200398 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100399 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200400}
401
402static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
403{
Jens Freimann383d0b02014-07-29 15:11:49 +0200404 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200405 int rc;
406
407 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
408 0, 0);
409
410 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
411 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100412 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200413 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
414 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
415 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
416 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200417 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100418 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200419}
420
Jens Freimann383d0b02014-07-29 15:11:49 +0200421static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200422{
Jens Freimann383d0b02014-07-29 15:11:49 +0200423 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
424 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200425 int rc;
426
Jens Freimann383d0b02014-07-29 15:11:49 +0200427 spin_lock(&li->lock);
428 ext = li->irq.ext;
429 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
430 li->irq.ext.ext_params2 = 0;
431 spin_unlock(&li->lock);
432
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200433 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
434 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200435 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
436 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200437 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200438
439 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
440 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
441 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
442 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
443 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
444 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200445 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100446 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200447}
448
David Hildenbrandd6404de2015-10-14 16:47:36 +0200449static int __write_machine_check(struct kvm_vcpu *vcpu,
450 struct kvm_s390_mchk_info *mchk)
451{
452 unsigned long ext_sa_addr;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100453 unsigned long lc;
David Hildenbrand0319dae2016-08-03 11:18:57 +0200454 freg_t fprs[NUM_FPRS];
David Hildenbrandff5dc142015-10-14 16:57:56 +0200455 union mci mci;
David Hildenbrandd6404de2015-10-14 16:47:36 +0200456 int rc;
457
David Hildenbrandff5dc142015-10-14 16:57:56 +0200458 mci.val = mchk->mcic;
Christian Borntraeger31d8b8d2016-11-10 14:22:02 +0100459 /* take care of lazy register loading */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200460 save_fpu_regs();
461 save_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger80248552017-04-12 12:59:59 +0200462 if (MACHINE_HAS_GS && vcpu->arch.gs_enabled)
463 save_gs_cb(current->thread.gs_cb);
David Hildenbrand0319dae2016-08-03 11:18:57 +0200464
David Hildenbrandd6404de2015-10-14 16:47:36 +0200465 /* Extended save area */
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100466 rc = read_guest_lc(vcpu, __LC_MCESAD, &ext_sa_addr,
467 sizeof(unsigned long));
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100468 /* Only bits 0 through 63-LC are used for address formation */
469 lc = ext_sa_addr & MCESA_LC_MASK;
470 if (test_kvm_facility(vcpu->kvm, 133)) {
471 switch (lc) {
472 case 0:
473 case 10:
474 ext_sa_addr &= ~0x3ffUL;
475 break;
476 case 11:
477 ext_sa_addr &= ~0x7ffUL;
478 break;
479 case 12:
480 ext_sa_addr &= ~0xfffUL;
481 break;
482 default:
483 ext_sa_addr = 0;
484 break;
485 }
486 } else {
487 ext_sa_addr &= ~0x3ffUL;
488 }
489
David Hildenbrandff5dc142015-10-14 16:57:56 +0200490 if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) {
491 if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs,
492 512))
493 mci.vr = 0;
494 } else {
495 mci.vr = 0;
496 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100497 if (!rc && mci.gs && ext_sa_addr && test_kvm_facility(vcpu->kvm, 133)
498 && (lc == 11 || lc == 12)) {
499 if (write_guest_abs(vcpu, ext_sa_addr + 1024,
500 &vcpu->run->s.regs.gscb, 32))
501 mci.gs = 0;
502 } else {
503 mci.gs = 0;
504 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200505
506 /* General interruption information */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200507 rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200508 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
509 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
510 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
511 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
David Hildenbrandff5dc142015-10-14 16:57:56 +0200512 rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200513
514 /* Register-save areas */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200515 if (MACHINE_HAS_VX) {
516 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
517 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128);
518 } else {
519 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA,
520 vcpu->run->s.regs.fprs, 128);
521 }
522 rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA,
523 vcpu->run->s.regs.gprs, 128);
524 rc |= put_guest_lc(vcpu, current->thread.fpu.fpc,
525 (u32 __user *) __LC_FP_CREG_SAVE_AREA);
526 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr,
527 (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA);
528 rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu),
529 (u64 __user *) __LC_CPU_TIMER_SAVE_AREA);
530 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8,
531 (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA);
532 rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA,
533 &vcpu->run->s.regs.acrs, 64);
534 rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA,
535 &vcpu->arch.sie_block->gcr, 128);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200536
537 /* Extended interruption information */
David Hildenbrand8953fb02016-08-03 12:25:08 +0200538 rc |= put_guest_lc(vcpu, mchk->ext_damage_code,
539 (u32 __user *) __LC_EXT_DAMAGE_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200540 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
541 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
542 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
543 sizeof(mchk->fixed_logout));
544 return rc ? -EFAULT : 0;
545}
546
Jens Freimann383d0b02014-07-29 15:11:49 +0200547static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200548{
Jens Freimann6d3da242013-07-03 15:18:35 +0200549 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200550 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200551 struct kvm_s390_mchk_info mchk = {};
Jens Freimann6d3da242013-07-03 15:18:35 +0200552 int deliver = 0;
553 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200554
Jens Freimann6d3da242013-07-03 15:18:35 +0200555 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200556 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200557 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
558 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
559 /*
560 * If there was an exigent machine check pending, then any
561 * repressible machine checks that might have been pending
562 * are indicated along with it, so always clear bits for
563 * repressible and exigent interrupts
564 */
565 mchk = li->irq.mchk;
566 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
567 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
568 memset(&li->irq.mchk, 0, sizeof(mchk));
569 deliver = 1;
570 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200571 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200572 * We indicate floating repressible conditions along with
573 * other pending conditions. Channel Report Pending and Channel
574 * Subsystem damage are the only two and and are indicated by
575 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200576 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200577 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
578 mchk.mcic |= fi->mchk.mcic;
579 mchk.cr14 |= fi->mchk.cr14;
580 memset(&fi->mchk, 0, sizeof(mchk));
581 deliver = 1;
582 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200583 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200584 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200585
Jens Freimann6d3da242013-07-03 15:18:35 +0200586 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200587 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200588 mchk.mcic);
589 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
590 KVM_S390_MCHK,
591 mchk.cr14, mchk.mcic);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200592 rc = __write_machine_check(vcpu, &mchk);
Jens Freimann6d3da242013-07-03 15:18:35 +0200593 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200594 return rc;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200595}
596
597static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
598{
Jens Freimann383d0b02014-07-29 15:11:49 +0200599 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200600 int rc;
601
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200602 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200603 vcpu->stat.deliver_restart_signal++;
604 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
605
606 rc = write_guest_lc(vcpu,
Heiko Carstensc667aea2015-12-31 10:29:00 +0100607 offsetof(struct lowcore, restart_old_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200608 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstensc667aea2015-12-31 10:29:00 +0100609 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200610 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200611 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100612 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200613}
614
Jens Freimann383d0b02014-07-29 15:11:49 +0200615static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200616{
Jens Freimann383d0b02014-07-29 15:11:49 +0200617 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
618 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200619
Jens Freimann383d0b02014-07-29 15:11:49 +0200620 spin_lock(&li->lock);
621 prefix = li->irq.prefix;
622 li->irq.prefix.address = 0;
623 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
624 spin_unlock(&li->lock);
625
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200626 vcpu->stat.deliver_prefix_signal++;
627 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
628 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200629 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200630
Jens Freimann383d0b02014-07-29 15:11:49 +0200631 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200632 return 0;
633}
634
Jens Freimann383d0b02014-07-29 15:11:49 +0200635static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200636{
Jens Freimann383d0b02014-07-29 15:11:49 +0200637 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200638 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200639 int cpu_addr;
640
641 spin_lock(&li->lock);
642 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
643 clear_bit(cpu_addr, li->sigp_emerg_pending);
644 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
645 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
646 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200647
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200648 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200649 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200650 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
651 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200652
653 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
654 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200655 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200656 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
657 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
658 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
659 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100660 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200661}
662
Jens Freimann383d0b02014-07-29 15:11:49 +0200663static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200664{
Jens Freimann383d0b02014-07-29 15:11:49 +0200665 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
666 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200667 int rc;
668
Jens Freimann383d0b02014-07-29 15:11:49 +0200669 spin_lock(&li->lock);
670 extcall = li->irq.extcall;
671 li->irq.extcall.code = 0;
672 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
673 spin_unlock(&li->lock);
674
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200675 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200676 vcpu->stat.deliver_external_call++;
677 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
678 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200679 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200680
681 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
682 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200683 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200684 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
685 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
686 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
687 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100688 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200689}
690
Jens Freimann383d0b02014-07-29 15:11:49 +0200691static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200692{
Jens Freimann383d0b02014-07-29 15:11:49 +0200693 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
694 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100695 int rc = 0, nullifying = false;
David Hildenbrand634790b2015-11-04 16:33:33 +0100696 u16 ilen;
David Hildenbrand87128362014-03-03 10:55:13 +0100697
Jens Freimann383d0b02014-07-29 15:11:49 +0200698 spin_lock(&li->lock);
699 pgm_info = li->irq.pgm;
700 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
701 memset(&li->irq.pgm, 0, sizeof(pgm_info));
702 spin_unlock(&li->lock);
703
David Hildenbrand634790b2015-11-04 16:33:33 +0100704 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100705 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
706 pgm_info.code, ilen);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200707 vcpu->stat.deliver_program_int++;
708 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200709 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200710
Jens Freimann383d0b02014-07-29 15:11:49 +0200711 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100712 case PGM_AFX_TRANSLATION:
713 case PGM_ASX_TRANSLATION:
714 case PGM_EX_TRANSLATION:
715 case PGM_LFX_TRANSLATION:
716 case PGM_LSTE_SEQUENCE:
717 case PGM_LSX_TRANSLATION:
718 case PGM_LX_TRANSLATION:
719 case PGM_PRIMARY_AUTHORITY:
720 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100721 nullifying = true;
722 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100723 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200724 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100725 (u64 *)__LC_TRANS_EXC_CODE);
726 break;
727 case PGM_ALEN_TRANSLATION:
728 case PGM_ALE_SEQUENCE:
729 case PGM_ASTE_INSTANCE:
730 case PGM_ASTE_SEQUENCE:
731 case PGM_ASTE_VALIDITY:
732 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200733 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100734 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100735 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100736 break;
737 case PGM_ASCE_TYPE:
738 case PGM_PAGE_TRANSLATION:
739 case PGM_REGION_FIRST_TRANS:
740 case PGM_REGION_SECOND_TRANS:
741 case PGM_REGION_THIRD_TRANS:
742 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200743 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100744 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200745 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100746 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200747 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100748 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100749 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100750 break;
751 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200752 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200753 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200754 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100755 (u64 *)__LC_MON_CODE);
756 break;
Eric Farman403c8642015-02-02 15:01:06 -0500757 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100758 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200759 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100760 (u32 *)__LC_DATA_EXC_CODE);
761 break;
762 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200763 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100764 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200765 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100766 (u8 *)__LC_EXC_ACCESS_ID);
767 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100768 case PGM_STACK_FULL:
769 case PGM_STACK_EMPTY:
770 case PGM_STACK_SPECIFICATION:
771 case PGM_STACK_TYPE:
772 case PGM_STACK_OPERATION:
773 case PGM_TRACE_TABEL:
774 case PGM_CRYPTO_OPERATION:
775 nullifying = true;
776 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100777 }
778
Jens Freimann383d0b02014-07-29 15:11:49 +0200779 if (pgm_info.code & PGM_PER) {
780 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100781 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200782 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100783 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200784 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100785 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200786 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100787 (u8 *) __LC_PER_ACCESS_ID);
788 }
789
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100790 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100791 kvm_s390_rewind_psw(vcpu, ilen);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100792
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100793 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
794 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100795 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
796 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200797 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100798 (u16 *)__LC_PGM_INT_CODE);
799 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
800 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
801 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
802 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100803 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200804}
David Hildenbrand87128362014-03-03 10:55:13 +0100805
Jens Freimann6d3da242013-07-03 15:18:35 +0200806static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200807{
Jens Freimann6d3da242013-07-03 15:18:35 +0200808 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
809 struct kvm_s390_ext_info ext;
810 int rc = 0;
811
812 spin_lock(&fi->lock);
813 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
814 spin_unlock(&fi->lock);
815 return 0;
816 }
817 ext = fi->srv_signal;
818 memset(&fi->srv_signal, 0, sizeof(ext));
819 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
820 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200821
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200822 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200823 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200824 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200825 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
826 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200827
828 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100829 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200830 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
831 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
832 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
833 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200834 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200835 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200836
Jens Freimann99e20002014-12-01 17:05:39 +0100837 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200838}
839
Jens Freimann6d3da242013-07-03 15:18:35 +0200840static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200841{
Jens Freimann6d3da242013-07-03 15:18:35 +0200842 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
843 struct kvm_s390_interrupt_info *inti;
844 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200845
Jens Freimann6d3da242013-07-03 15:18:35 +0200846 spin_lock(&fi->lock);
847 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
848 struct kvm_s390_interrupt_info,
849 list);
850 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200851 list_del(&inti->list);
852 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
853 }
854 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
855 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
856 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200857
Jens Freimann6d3da242013-07-03 15:18:35 +0200858 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200859 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
860 KVM_S390_INT_PFAULT_DONE, 0,
861 inti->ext.ext_params2);
862 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
863 inti->ext.ext_params2);
864
Jens Freimann6d3da242013-07-03 15:18:35 +0200865 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
866 (u16 *)__LC_EXT_INT_CODE);
867 rc |= put_guest_lc(vcpu, PFAULT_DONE,
868 (u16 *)__LC_EXT_CPU_ADDR);
869 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
870 &vcpu->arch.sie_block->gpsw,
871 sizeof(psw_t));
872 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
873 &vcpu->arch.sie_block->gpsw,
874 sizeof(psw_t));
875 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
876 (u64 *)__LC_EXT_PARAMS2);
877 kfree(inti);
878 }
Jens Freimann99e20002014-12-01 17:05:39 +0100879 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200880}
881
Jens Freimann6d3da242013-07-03 15:18:35 +0200882static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200883{
Jens Freimann6d3da242013-07-03 15:18:35 +0200884 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
885 struct kvm_s390_interrupt_info *inti;
886 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200887
Jens Freimann6d3da242013-07-03 15:18:35 +0200888 spin_lock(&fi->lock);
889 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
890 struct kvm_s390_interrupt_info,
891 list);
892 if (inti) {
893 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200894 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200895 inti->ext.ext_params, inti->ext.ext_params2);
896 vcpu->stat.deliver_virtio_interrupt++;
897 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
898 inti->type,
899 inti->ext.ext_params,
900 inti->ext.ext_params2);
901 list_del(&inti->list);
902 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
903 }
904 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
905 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
906 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200907
Jens Freimann6d3da242013-07-03 15:18:35 +0200908 if (inti) {
909 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
910 (u16 *)__LC_EXT_INT_CODE);
911 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
912 (u16 *)__LC_EXT_CPU_ADDR);
913 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
914 &vcpu->arch.sie_block->gpsw,
915 sizeof(psw_t));
916 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
917 &vcpu->arch.sie_block->gpsw,
918 sizeof(psw_t));
919 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
920 (u32 *)__LC_EXT_PARAMS);
921 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
922 (u64 *)__LC_EXT_PARAMS2);
923 kfree(inti);
924 }
Jens Freimann99e20002014-12-01 17:05:39 +0100925 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200926}
927
Michael Muellerd7c5cb02017-06-12 14:15:19 +0200928static int __do_deliver_io(struct kvm_vcpu *vcpu, struct kvm_s390_io_info *io)
929{
930 int rc;
931
932 rc = put_guest_lc(vcpu, io->subchannel_id, (u16 *)__LC_SUBCHANNEL_ID);
933 rc |= put_guest_lc(vcpu, io->subchannel_nr, (u16 *)__LC_SUBCHANNEL_NR);
934 rc |= put_guest_lc(vcpu, io->io_int_parm, (u32 *)__LC_IO_INT_PARM);
935 rc |= put_guest_lc(vcpu, io->io_int_word, (u32 *)__LC_IO_INT_WORD);
936 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
937 &vcpu->arch.sie_block->gpsw,
938 sizeof(psw_t));
939 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
940 &vcpu->arch.sie_block->gpsw,
941 sizeof(psw_t));
942 return rc ? -EFAULT : 0;
943}
944
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200945static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200946 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200947{
Jens Freimann6d3da242013-07-03 15:18:35 +0200948 struct list_head *isc_list;
949 struct kvm_s390_float_interrupt *fi;
950 struct kvm_s390_interrupt_info *inti = NULL;
Michael Muellerd7c5cb02017-06-12 14:15:19 +0200951 struct kvm_s390_io_info io;
952 u32 isc;
Jens Freimann6d3da242013-07-03 15:18:35 +0200953 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200954
Jens Freimann6d3da242013-07-03 15:18:35 +0200955 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200956
Jens Freimann6d3da242013-07-03 15:18:35 +0200957 spin_lock(&fi->lock);
Michael Muellerd7c5cb02017-06-12 14:15:19 +0200958 isc = irq_type_to_isc(irq_type);
959 isc_list = &fi->lists[isc];
Jens Freimann6d3da242013-07-03 15:18:35 +0200960 inti = list_first_entry_or_null(isc_list,
961 struct kvm_s390_interrupt_info,
962 list);
963 if (inti) {
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +0200964 if (inti->type & KVM_S390_INT_IO_AI_MASK)
965 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
966 else
967 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
968 inti->io.subchannel_id >> 8,
969 inti->io.subchannel_id >> 1 & 0x3,
970 inti->io.subchannel_nr);
971
Jens Freimann6d3da242013-07-03 15:18:35 +0200972 vcpu->stat.deliver_io_int++;
973 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
974 inti->type,
975 ((__u32)inti->io.subchannel_id << 16) |
976 inti->io.subchannel_nr,
977 ((__u64)inti->io.io_int_parm << 32) |
978 inti->io.io_int_word);
979 list_del(&inti->list);
980 fi->counters[FIRQ_CNTR_IO] -= 1;
981 }
982 if (list_empty(isc_list))
983 clear_bit(irq_type, &fi->pending_irqs);
984 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100985
Jens Freimann6d3da242013-07-03 15:18:35 +0200986 if (inti) {
Michael Muellerd7c5cb02017-06-12 14:15:19 +0200987 rc = __do_deliver_io(vcpu, &(inti->io));
Jens Freimann6d3da242013-07-03 15:18:35 +0200988 kfree(inti);
Michael Muellerd7c5cb02017-06-12 14:15:19 +0200989 goto out;
Jens Freimann6d3da242013-07-03 15:18:35 +0200990 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200991
Michael Muellerd7c5cb02017-06-12 14:15:19 +0200992 if (vcpu->kvm->arch.gisa &&
993 kvm_s390_gisa_tac_ipm_gisc(vcpu->kvm->arch.gisa, isc)) {
994 /*
995 * in case an adapter interrupt was not delivered
996 * in SIE context KVM will handle the delivery
997 */
998 VCPU_EVENT(vcpu, 4, "%s isc %u", "deliver: I/O (AI/gisa)", isc);
999 memset(&io, 0, sizeof(io));
Michael Mueller2496c8e2017-08-31 11:10:28 +02001000 io.io_int_word = isc_to_int_word(isc);
Michael Muellerd7c5cb02017-06-12 14:15:19 +02001001 vcpu->stat.deliver_io_int++;
1002 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1003 KVM_S390_INT_IO(1, 0, 0, 0),
1004 ((__u32)io.subchannel_id << 16) |
1005 io.subchannel_nr,
1006 ((__u64)io.io_int_parm << 32) |
1007 io.io_int_word);
1008 rc = __do_deliver_io(vcpu, &io);
1009 }
1010out:
1011 return rc;
Jens Freimann383d0b02014-07-29 15:11:49 +02001012}
1013
1014typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
1015
1016static const deliver_irq_t deliver_irq_funcs[] = {
1017 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +02001018 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +02001019 [IRQ_PEND_PROG] = __deliver_prog,
1020 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
1021 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
1022 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
1023 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
1024 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +02001025 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
1026 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +02001027 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
1028 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
1029 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +02001030};
1031
David Hildenbrandea5f4962014-10-14 15:29:30 +02001032/* Check whether an external call is pending (deliverable or not) */
1033int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +01001034{
David Hildenbrandea5f4962014-10-14 15:29:30 +02001035 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +01001036
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001037 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001038 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +01001039
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001040 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +01001041}
1042
David Hildenbrand9a022062014-08-05 17:40:47 +02001043int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001044{
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001045 if (deliverable_irqs(vcpu))
1046 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001047
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001048 if (kvm_cpu_has_pending_timer(vcpu))
1049 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001050
David Hildenbrandea5f4962014-10-14 15:29:30 +02001051 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001052 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +02001053 !psw_extint_disabled(vcpu) &&
1054 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001055 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +01001056
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001057 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
1058 return 1;
1059 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001060}
1061
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001062int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1063{
David Hildenbrandb4aec922014-12-01 15:55:42 +01001064 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001065}
1066
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001067static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
1068{
1069 u64 now, cputm, sltime = 0;
1070
1071 if (ckc_interrupts_enabled(vcpu)) {
1072 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1073 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
1074 /* already expired or overflow? */
1075 if (!sltime || vcpu->arch.sie_block->ckc <= now)
1076 return 0;
1077 if (cpu_timer_interrupts_enabled(vcpu)) {
1078 cputm = kvm_s390_get_cpu_timer(vcpu);
1079 /* already expired? */
1080 if (cputm >> 63)
1081 return 0;
1082 return min(sltime, tod_to_ns(cputm));
1083 }
1084 } else if (cpu_timer_interrupts_enabled(vcpu)) {
1085 sltime = kvm_s390_get_cpu_timer(vcpu);
1086 /* already expired? */
1087 if (sltime >> 63)
1088 return 0;
1089 }
1090 return sltime;
1091}
1092
Carsten Otteba5c1e92008-03-25 18:47:26 +01001093int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
1094{
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001095 u64 sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001096
1097 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001098
David Hildenbrand0759d062014-05-13 16:54:32 +02001099 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +02001100 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +02001101 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +02001102
Carsten Otteba5c1e92008-03-25 18:47:26 +01001103 if (psw_interrupts_disabled(vcpu)) {
1104 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001105 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +01001106 }
1107
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001108 if (!ckc_interrupts_enabled(vcpu) &&
1109 !cpu_timer_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001110 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +01001111 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001112 goto no_timer;
1113 }
1114
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001115 sltime = __calculate_sltime(vcpu);
1116 if (!sltime)
David Hildenbrandbda343e2014-12-12 12:26:40 +01001117 return 0;
1118
1119 __set_cpu_idle(vcpu);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001120 hrtimer_start(&vcpu->arch.ckc_timer, sltime, HRTIMER_MODE_REL);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001121 VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001122no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +02001123 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +02001124 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001125 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +02001126 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1127
David Hildenbrand2d00f752014-12-11 10:18:01 +01001128 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001129 return 0;
1130}
1131
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001132void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1133{
Christian Borntraeger3491caf2016-05-13 12:16:35 +02001134 /*
1135 * We cannot move this into the if, as the CPU might be already
1136 * in kvm_vcpu_block without having the waitqueue set (polling)
1137 */
1138 vcpu->valid_wakeup = true;
Christian Borntraeger72e1ad42017-09-19 12:34:06 +02001139 /*
1140 * This is mostly to document, that the read in swait_active could
1141 * be moved before other stores, leading to subtle races.
1142 * All current users do not store or use an atomic like update
1143 */
1144 smp_mb__after_atomic();
Marcelo Tosatti85773702016-02-19 09:46:39 +01001145 if (swait_active(&vcpu->wq)) {
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001146 /*
1147 * The vcpu gave up the cpu voluntarily, mark it as a good
1148 * yield-candidate.
1149 */
1150 vcpu->preempted = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001151 swake_up(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +02001152 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001153 }
David Hildenbrandadbf1692016-05-27 22:03:52 +02001154 /*
1155 * The VCPU might not be sleeping but is executing the VSIE. Let's
1156 * kick it, so it leaves the SIE to process the request.
1157 */
1158 kvm_s390_vsie_kick(vcpu);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001159}
1160
Christian Borntraegerca872302009-05-12 17:21:49 +02001161enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1162{
1163 struct kvm_vcpu *vcpu;
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001164 u64 sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +02001165
1166 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001167 sltime = __calculate_sltime(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001168
David Hildenbrand2d00f752014-12-11 10:18:01 +01001169 /*
1170 * If the monotonic clock runs faster than the tod clock we might be
1171 * woken up too early and have to go back to sleep to avoid deadlocks.
1172 */
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001173 if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
David Hildenbrand2d00f752014-12-11 10:18:01 +01001174 return HRTIMER_RESTART;
1175 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001176 return HRTIMER_NORESTART;
1177}
Carsten Otteba5c1e92008-03-25 18:47:26 +01001178
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001179void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1180{
1181 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001182
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001183 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001184 li->pending_irqs = 0;
1185 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1186 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001187 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +01001188
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001189 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001190}
1191
Christian Borntraeger614aeab2014-08-25 12:27:29 +02001192int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001193{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001194 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001195 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +02001196 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +02001197 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +02001198 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001199
1200 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001201
Jens Freimann383d0b02014-07-29 15:11:49 +02001202 /* pending ckc conditions might have been invalidated */
1203 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +01001204 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +02001205 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1206
David Hildenbrandb4aec922014-12-01 15:55:42 +01001207 /* pending cpu timer conditions might have been invalidated */
1208 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1209 if (cpu_timer_irq_pending(vcpu))
1210 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1211
Jens Freimannffeca0a2015-04-17 10:21:04 +02001212 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Michael Muellerc7901a62017-06-29 18:39:27 +02001213 /* bits are in the reverse order of interrupt priority */
1214 irq_type = find_last_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +02001215 if (is_ioirq(irq_type)) {
1216 rc = __deliver_io(vcpu, irq_type);
1217 } else {
1218 func = deliver_irq_funcs[irq_type];
1219 if (!func) {
1220 WARN_ON_ONCE(func == NULL);
1221 clear_bit(irq_type, &li->pending_irqs);
1222 continue;
1223 }
1224 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +02001225 }
Jens Freimannffeca0a2015-04-17 10:21:04 +02001226 }
Jens Freimann383d0b02014-07-29 15:11:49 +02001227
Jens Freimann6d3da242013-07-03 15:18:35 +02001228 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +02001229
1230 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001231}
1232
Jens Freimann383d0b02014-07-29 15:11:49 +02001233static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001234{
1235 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1236
David Hildenbranded2afcf2015-07-20 10:33:03 +02001237 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1238 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1239 irq->u.pgm.code, 0);
1240
David Hildenbrand634790b2015-11-04 16:33:33 +01001241 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1242 /* auto detection if no valid ILC was given */
1243 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1244 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1245 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1246 }
1247
David Hildenbrand238293b2015-05-04 12:38:48 +02001248 if (irq->u.pgm.code == PGM_PER) {
1249 li->irq.pgm.code |= PGM_PER;
David Hildenbrand634790b2015-11-04 16:33:33 +01001250 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001251 /* only modify PER related information */
1252 li->irq.pgm.per_address = irq->u.pgm.per_address;
1253 li->irq.pgm.per_code = irq->u.pgm.per_code;
1254 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1255 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1256 } else if (!(irq->u.pgm.code & PGM_PER)) {
1257 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1258 irq->u.pgm.code;
David Hildenbrand634790b2015-11-04 16:33:33 +01001259 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001260 /* only modify non-PER information */
1261 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1262 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1263 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1264 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1265 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1266 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1267 } else {
1268 li->irq.pgm = irq->u.pgm;
1269 }
Jens Freimann91851242014-12-01 16:43:40 +01001270 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001271 return 0;
1272}
1273
Jens Freimann383d0b02014-07-29 15:11:49 +02001274static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001275{
1276 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1277
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001278 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1279 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001280 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1281 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001282 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001283
1284 li->irq.ext = irq->u.ext;
1285 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
David Hildenbrand20182242018-01-23 18:05:28 +01001286 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001287 return 0;
1288}
1289
Christian Borntraeger0675d922015-01-15 12:40:42 +01001290static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001291{
1292 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001293 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001294 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001295
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001296 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001297 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001298 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001299 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001300
David Hildenbrandea5f4962014-10-14 15:29:30 +02001301 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001302 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001303 return -EINVAL;
1304
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001305 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001306 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001307
David Hildenbrandb938eace2015-04-30 13:33:59 +02001308 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001309 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001310 *extcall = irq->u.extcall;
David Hildenbrand20182242018-01-23 18:05:28 +01001311 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001312 return 0;
1313}
1314
Jens Freimann383d0b02014-07-29 15:11:49 +02001315static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001316{
1317 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001318 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001319
David Hildenbranded2afcf2015-07-20 10:33:03 +02001320 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001321 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001322 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001323 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001324
David Hildenbranda3a9c592014-10-14 09:44:55 +02001325 if (!is_vcpu_stopped(vcpu))
1326 return -EBUSY;
1327
Jens Freimann383d0b02014-07-29 15:11:49 +02001328 *prefix = irq->u.prefix;
1329 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001330 return 0;
1331}
1332
David Hildenbrand6cddd432014-10-15 16:48:53 +02001333#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001334static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001335{
1336 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001337 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001338 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001339
David Hildenbranded2afcf2015-07-20 10:33:03 +02001340 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001341
David Hildenbrand28225452014-10-15 16:48:16 +02001342 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1343 return -EINVAL;
1344
David Hildenbrand6cddd432014-10-15 16:48:53 +02001345 if (is_vcpu_stopped(vcpu)) {
1346 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1347 rc = kvm_s390_store_status_unloaded(vcpu,
1348 KVM_S390_STORE_STATUS_NOADDR);
1349 return rc;
1350 }
1351
1352 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1353 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001354 stop->flags = irq->u.stop.flags;
David Hildenbrand20182242018-01-23 18:05:28 +01001355 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001356 return 0;
1357}
1358
1359static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001360 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001361{
1362 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1363
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001364 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001365 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001366
1367 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001368 return 0;
1369}
1370
1371static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001372 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001373{
1374 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1375
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001376 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001377 irq->u.emerg.code);
1378 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001379 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001380
David Hildenbrandb85de332015-11-05 09:38:15 +01001381 /* sending vcpu invalid */
1382 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1383 return -EINVAL;
1384
Jens Freimann49538d12014-12-18 15:48:14 +01001385 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001386 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
David Hildenbrand20182242018-01-23 18:05:28 +01001387 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001388 return 0;
1389}
1390
Jens Freimann383d0b02014-07-29 15:11:49 +02001391static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001392{
1393 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001394 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001395
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001396 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001397 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001398 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001399 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001400
1401 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001402 * Because repressible machine checks can be indicated along with
1403 * exigent machine checks (PoP, Chapter 11, Interruption action)
1404 * we need to combine cr14, mcic and external damage code.
1405 * Failing storage address and the logout area should not be or'ed
1406 * together, we just indicate the last occurrence of the corresponding
1407 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001408 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001409 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001410 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001411 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1412 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1413 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1414 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001415 if (mchk->mcic & MCHK_EX_MASK)
1416 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1417 else if (mchk->mcic & MCHK_REP_MASK)
1418 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001419 return 0;
1420}
1421
Jens Freimann383d0b02014-07-29 15:11:49 +02001422static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001423{
1424 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1425
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001426 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001427 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001428 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001429
1430 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrand20182242018-01-23 18:05:28 +01001431 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001432 return 0;
1433}
1434
Jens Freimann383d0b02014-07-29 15:11:49 +02001435static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001436{
1437 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1438
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001439 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001440 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001441 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001442
1443 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
David Hildenbrand20182242018-01-23 18:05:28 +01001444 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
Jens Freimannbcd84682014-02-11 11:07:05 +01001445 return 0;
1446}
1447
Jens Freimann6d3da242013-07-03 15:18:35 +02001448static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1449 int isc, u32 schid)
1450{
1451 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1452 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1453 struct kvm_s390_interrupt_info *iter;
1454 u16 id = (schid & 0xffff0000U) >> 16;
1455 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001456
Jens Freimann6d3da242013-07-03 15:18:35 +02001457 spin_lock(&fi->lock);
1458 list_for_each_entry(iter, isc_list, list) {
1459 if (schid && (id != iter->io.subchannel_id ||
1460 nr != iter->io.subchannel_nr))
1461 continue;
1462 /* found an appropriate entry */
1463 list_del_init(&iter->list);
1464 fi->counters[FIRQ_CNTR_IO] -= 1;
1465 if (list_empty(isc_list))
Michael Muelleree739f42017-07-03 15:32:50 +02001466 clear_bit(isc_to_irq_type(isc), &fi->pending_irqs);
Jens Freimann6d3da242013-07-03 15:18:35 +02001467 spin_unlock(&fi->lock);
1468 return iter;
1469 }
1470 spin_unlock(&fi->lock);
1471 return NULL;
1472}
1473
1474/*
1475 * Dequeue and return an I/O interrupt matching any of the interruption
1476 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1477 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001478struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001479 u64 isc_mask, u32 schid)
1480{
1481 struct kvm_s390_interrupt_info *inti = NULL;
1482 int isc;
1483
1484 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1485 if (isc_mask & isc_to_isc_bits(isc))
1486 inti = get_io_int(kvm, isc, schid);
1487 }
1488 return inti;
1489}
1490
1491#define SCCB_MASK 0xFFFFFFF8
1492#define SCCB_EVENT_PENDING 0x3
1493
1494static int __inject_service(struct kvm *kvm,
1495 struct kvm_s390_interrupt_info *inti)
1496{
1497 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1498
1499 spin_lock(&fi->lock);
1500 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1501 /*
1502 * Early versions of the QEMU s390 bios will inject several
1503 * service interrupts after another without handling a
1504 * condition code indicating busy.
1505 * We will silently ignore those superfluous sccb values.
1506 * A future version of QEMU will take care of serialization
1507 * of servc requests
1508 */
1509 if (fi->srv_signal.ext_params & SCCB_MASK)
1510 goto out;
1511 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1512 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1513out:
1514 spin_unlock(&fi->lock);
1515 kfree(inti);
1516 return 0;
1517}
1518
1519static int __inject_virtio(struct kvm *kvm,
1520 struct kvm_s390_interrupt_info *inti)
1521{
1522 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1523
1524 spin_lock(&fi->lock);
1525 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1526 spin_unlock(&fi->lock);
1527 return -EBUSY;
1528 }
1529 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1530 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1531 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1532 spin_unlock(&fi->lock);
1533 return 0;
1534}
1535
1536static int __inject_pfault_done(struct kvm *kvm,
1537 struct kvm_s390_interrupt_info *inti)
1538{
1539 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1540
1541 spin_lock(&fi->lock);
1542 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1543 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1544 spin_unlock(&fi->lock);
1545 return -EBUSY;
1546 }
1547 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1548 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1549 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1550 spin_unlock(&fi->lock);
1551 return 0;
1552}
1553
1554#define CR_PENDING_SUBCLASS 28
1555static int __inject_float_mchk(struct kvm *kvm,
1556 struct kvm_s390_interrupt_info *inti)
1557{
1558 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1559
1560 spin_lock(&fi->lock);
1561 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1562 fi->mchk.mcic |= inti->mchk.mcic;
1563 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1564 spin_unlock(&fi->lock);
1565 kfree(inti);
1566 return 0;
1567}
1568
1569static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001570{
1571 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001572 struct list_head *list;
1573 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001574
Michael Muellerd7c5cb02017-06-12 14:15:19 +02001575 isc = int_word_to_isc(inti->io.io_int_word);
1576
1577 if (kvm->arch.gisa && inti->type & KVM_S390_INT_IO_AI_MASK) {
1578 VM_EVENT(kvm, 4, "%s isc %1u", "inject: I/O (AI/gisa)", isc);
1579 kvm_s390_gisa_set_ipm_gisc(kvm->arch.gisa, isc);
1580 kfree(inti);
1581 return 0;
1582 }
1583
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001584 fi = &kvm->arch.float_int;
1585 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001586 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1587 spin_unlock(&fi->lock);
1588 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001589 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001590 fi->counters[FIRQ_CNTR_IO] += 1;
1591
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +02001592 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1593 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1594 else
1595 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1596 inti->io.subchannel_id >> 8,
1597 inti->io.subchannel_id >> 1 & 0x3,
1598 inti->io.subchannel_nr);
Jens Freimann6d3da242013-07-03 15:18:35 +02001599 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1600 list_add_tail(&inti->list, list);
Michael Muelleree739f42017-07-03 15:32:50 +02001601 set_bit(isc_to_irq_type(isc), &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001602 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001603 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001604}
1605
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001606/*
1607 * Find a destination VCPU for a floating irq and kick it.
1608 */
1609static void __floating_irq_kick(struct kvm *kvm, u64 type)
1610{
1611 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001612 struct kvm_vcpu *dst_vcpu;
1613 int sigcpu, online_vcpus, nr_tries = 0;
1614
1615 online_vcpus = atomic_read(&kvm->online_vcpus);
1616 if (!online_vcpus)
1617 return;
1618
1619 /* find idle VCPUs first, then round robin */
1620 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1621 if (sigcpu == online_vcpus) {
1622 do {
1623 sigcpu = fi->next_rr_cpu;
1624 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1625 /* avoid endless loops if all vcpus are stopped */
1626 if (nr_tries++ >= online_vcpus)
1627 return;
1628 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1629 }
1630 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1631
1632 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001633 switch (type) {
1634 case KVM_S390_MCHK:
David Hildenbrand20182242018-01-23 18:05:28 +01001635 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_STOP_INT);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001636 break;
1637 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
David Hildenbrand20182242018-01-23 18:05:28 +01001638 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_IO_INT);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001639 break;
1640 default:
David Hildenbrand20182242018-01-23 18:05:28 +01001641 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_EXT_INT);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001642 break;
1643 }
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001644 kvm_s390_vcpu_wakeup(dst_vcpu);
1645}
1646
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001647static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001648{
Jens Freimann6d3da242013-07-03 15:18:35 +02001649 u64 type = READ_ONCE(inti->type);
1650 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001651
Jens Freimann6d3da242013-07-03 15:18:35 +02001652 switch (type) {
1653 case KVM_S390_MCHK:
1654 rc = __inject_float_mchk(kvm, inti);
1655 break;
1656 case KVM_S390_INT_VIRTIO:
1657 rc = __inject_virtio(kvm, inti);
1658 break;
1659 case KVM_S390_INT_SERVICE:
1660 rc = __inject_service(kvm, inti);
1661 break;
1662 case KVM_S390_INT_PFAULT_DONE:
1663 rc = __inject_pfault_done(kvm, inti);
1664 break;
1665 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1666 rc = __inject_io(kvm, inti);
1667 break;
1668 default:
1669 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001670 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001671 if (rc)
1672 return rc;
1673
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001674 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001675 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001676}
1677
1678int kvm_s390_inject_vm(struct kvm *kvm,
1679 struct kvm_s390_interrupt *s390int)
1680{
1681 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001682 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001683
1684 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1685 if (!inti)
1686 return -ENOMEM;
1687
1688 inti->type = s390int->type;
1689 switch (inti->type) {
1690 case KVM_S390_INT_VIRTIO:
1691 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1692 s390int->parm, s390int->parm64);
1693 inti->ext.ext_params = s390int->parm;
1694 inti->ext.ext_params2 = s390int->parm64;
1695 break;
1696 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001697 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001698 inti->ext.ext_params = s390int->parm;
1699 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001700 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001701 inti->ext.ext_params2 = s390int->parm64;
1702 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001703 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001704 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001705 s390int->parm64);
1706 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1707 inti->mchk.mcic = s390int->parm64;
1708 break;
1709 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimannc05c4182013-10-07 16:13:45 +02001710 inti->io.subchannel_id = s390int->parm >> 16;
1711 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1712 inti->io.io_int_parm = s390int->parm64 >> 32;
1713 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1714 break;
1715 default:
1716 kfree(inti);
1717 return -EINVAL;
1718 }
1719 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1720 2);
1721
David Hildenbrand428d53b2015-01-16 12:58:09 +01001722 rc = __inject_vm(kvm, inti);
1723 if (rc)
1724 kfree(inti);
1725 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001726}
1727
David Hildenbrand15462e32015-02-04 15:59:11 +01001728int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001729 struct kvm_s390_interrupt_info *inti)
1730{
David Hildenbrand15462e32015-02-04 15:59:11 +01001731 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001732}
1733
Jens Freimann383d0b02014-07-29 15:11:49 +02001734int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1735 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001736{
Jens Freimann383d0b02014-07-29 15:11:49 +02001737 irq->type = s390int->type;
1738 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001739 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001740 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001741 return -EINVAL;
1742 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001743 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001744 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001745 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001746 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001747 case KVM_S390_SIGP_STOP:
1748 irq->u.stop.flags = s390int->parm;
1749 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001750 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001751 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001752 return -EINVAL;
1753 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001754 break;
1755 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001756 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001757 return -EINVAL;
1758 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001759 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001760 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001761 irq->u.mchk.mcic = s390int->parm64;
1762 break;
1763 }
1764 return 0;
1765}
1766
David Hildenbrand6cddd432014-10-15 16:48:53 +02001767int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1768{
1769 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1770
1771 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1772}
1773
1774void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1775{
1776 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1777
1778 spin_lock(&li->lock);
1779 li->irq.stop.flags = 0;
1780 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1781 spin_unlock(&li->lock);
1782}
1783
Jens Freimann79e87a12015-03-19 15:12:12 +01001784static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001785{
Jens Freimann383d0b02014-07-29 15:11:49 +02001786 int rc;
1787
Jens Freimann383d0b02014-07-29 15:11:49 +02001788 switch (irq->type) {
1789 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001790 rc = __inject_prog(vcpu, irq);
1791 break;
1792 case KVM_S390_SIGP_SET_PREFIX:
1793 rc = __inject_set_prefix(vcpu, irq);
1794 break;
1795 case KVM_S390_SIGP_STOP:
1796 rc = __inject_sigp_stop(vcpu, irq);
1797 break;
1798 case KVM_S390_RESTART:
1799 rc = __inject_sigp_restart(vcpu, irq);
1800 break;
1801 case KVM_S390_INT_CLOCK_COMP:
1802 rc = __inject_ckc(vcpu);
1803 break;
1804 case KVM_S390_INT_CPU_TIMER:
1805 rc = __inject_cpu_timer(vcpu);
1806 break;
1807 case KVM_S390_INT_EXTERNAL_CALL:
1808 rc = __inject_extcall(vcpu, irq);
1809 break;
1810 case KVM_S390_INT_EMERGENCY:
1811 rc = __inject_sigp_emergency(vcpu, irq);
1812 break;
1813 case KVM_S390_MCHK:
1814 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001815 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001816 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001817 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001818 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001819 case KVM_S390_INT_VIRTIO:
1820 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001821 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001822 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001823 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001824 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001825
1826 return rc;
1827}
1828
1829int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1830{
1831 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1832 int rc;
1833
1834 spin_lock(&li->lock);
1835 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001836 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001837 if (!rc)
1838 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001839 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001840}
Jens Freimannc05c4182013-10-07 16:13:45 +02001841
Jens Freimann6d3da242013-07-03 15:18:35 +02001842static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001843{
Jens Freimann6d3da242013-07-03 15:18:35 +02001844 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001845
Jens Freimann6d3da242013-07-03 15:18:35 +02001846 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001847 list_del(&inti->list);
1848 kfree(inti);
1849 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001850}
1851
Jens Freimann94aa0332015-03-16 12:17:13 +01001852static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1853 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001854{
Jens Freimann94aa0332015-03-16 12:17:13 +01001855 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001856 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001857 case KVM_S390_INT_PFAULT_INIT:
1858 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001859 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001860 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001861 break;
1862 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001863 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001864 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001865 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001866}
1867
Jens Freimann6d3da242013-07-03 15:18:35 +02001868void kvm_s390_clear_float_irqs(struct kvm *kvm)
1869{
1870 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1871 int i;
1872
1873 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001874 fi->pending_irqs = 0;
1875 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1876 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001877 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1878 clear_irq_list(&fi->lists[i]);
1879 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1880 fi->counters[i] = 0;
1881 spin_unlock(&fi->lock);
Michael Mueller24160af2017-06-14 13:21:32 +02001882 kvm_s390_gisa_clear(kvm);
Jens Freimann6d3da242013-07-03 15:18:35 +02001883};
1884
Jens Freimann94aa0332015-03-16 12:17:13 +01001885static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001886{
1887 struct kvm_s390_interrupt_info *inti;
1888 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001889 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001890 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001891 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001892 int ret = 0;
1893 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001894 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001895
Jens Freimann94aa0332015-03-16 12:17:13 +01001896 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1897 return -EINVAL;
1898
1899 /*
1900 * We are already using -ENOMEM to signal
1901 * userspace it may retry with a bigger buffer,
1902 * so we need to use something else for this case
1903 */
1904 buf = vzalloc(len);
1905 if (!buf)
1906 return -ENOBUFS;
1907
1908 max_irqs = len / sizeof(struct kvm_s390_irq);
1909
Michael Mueller24160af2017-06-14 13:21:32 +02001910 if (kvm->arch.gisa &&
1911 kvm_s390_gisa_get_ipm(kvm->arch.gisa)) {
1912 for (i = 0; i <= MAX_ISC; i++) {
1913 if (n == max_irqs) {
1914 /* signal userspace to try again */
1915 ret = -ENOMEM;
1916 goto out_nolock;
1917 }
1918 if (kvm_s390_gisa_tac_ipm_gisc(kvm->arch.gisa, i)) {
1919 irq = (struct kvm_s390_irq *) &buf[n];
1920 irq->type = KVM_S390_INT_IO(1, 0, 0, 0);
1921 irq->u.io.io_int_word = isc_to_int_word(i);
1922 n++;
1923 }
1924 }
1925 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001926 fi = &kvm->arch.float_int;
1927 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001928 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1929 list_for_each_entry(inti, &fi->lists[i], list) {
1930 if (n == max_irqs) {
1931 /* signal userspace to try again */
1932 ret = -ENOMEM;
1933 goto out;
1934 }
1935 inti_to_irq(inti, &buf[n]);
1936 n++;
1937 }
1938 }
1939 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001940 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001941 /* signal userspace to try again */
1942 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001943 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001944 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001945 irq = (struct kvm_s390_irq *) &buf[n];
1946 irq->type = KVM_S390_INT_SERVICE;
1947 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001948 n++;
1949 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001950 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1951 if (n == max_irqs) {
1952 /* signal userspace to try again */
1953 ret = -ENOMEM;
1954 goto out;
1955 }
1956 irq = (struct kvm_s390_irq *) &buf[n];
1957 irq->type = KVM_S390_MCHK;
1958 irq->u.mchk = fi->mchk;
1959 n++;
1960}
1961
1962out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001963 spin_unlock(&fi->lock);
Michael Mueller24160af2017-06-14 13:21:32 +02001964out_nolock:
Jens Freimann94aa0332015-03-16 12:17:13 +01001965 if (!ret && n > 0) {
1966 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1967 ret = -EFAULT;
1968 }
1969 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001970
1971 return ret < 0 ? ret : n;
1972}
1973
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08001974static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr)
1975{
1976 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1977 struct kvm_s390_ais_all ais;
1978
1979 if (attr->attr < sizeof(ais))
1980 return -EINVAL;
1981
1982 if (!test_kvm_facility(kvm, 72))
1983 return -ENOTSUPP;
1984
1985 mutex_lock(&fi->ais_lock);
1986 ais.simm = fi->simm;
1987 ais.nimm = fi->nimm;
1988 mutex_unlock(&fi->ais_lock);
1989
1990 if (copy_to_user((void __user *)attr->addr, &ais, sizeof(ais)))
1991 return -EFAULT;
1992
1993 return 0;
1994}
1995
Jens Freimannc05c4182013-10-07 16:13:45 +02001996static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1997{
1998 int r;
1999
2000 switch (attr->group) {
2001 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01002002 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02002003 attr->attr);
2004 break;
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002005 case KVM_DEV_FLIC_AISM_ALL:
2006 r = flic_ais_mode_get_all(dev->kvm, attr);
2007 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002008 default:
2009 r = -EINVAL;
2010 }
2011
2012 return r;
2013}
2014
2015static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
2016 u64 addr)
2017{
2018 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
2019 void *target = NULL;
2020 void __user *source;
2021 u64 size;
2022
2023 if (get_user(inti->type, (u64 __user *)addr))
2024 return -EFAULT;
2025
2026 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02002027 case KVM_S390_INT_PFAULT_INIT:
2028 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02002029 case KVM_S390_INT_VIRTIO:
2030 case KVM_S390_INT_SERVICE:
2031 target = (void *) &inti->ext;
2032 source = &uptr->u.ext;
2033 size = sizeof(inti->ext);
2034 break;
2035 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2036 target = (void *) &inti->io;
2037 source = &uptr->u.io;
2038 size = sizeof(inti->io);
2039 break;
2040 case KVM_S390_MCHK:
2041 target = (void *) &inti->mchk;
2042 source = &uptr->u.mchk;
2043 size = sizeof(inti->mchk);
2044 break;
2045 default:
2046 return -EINVAL;
2047 }
2048
2049 if (copy_from_user(target, source, size))
2050 return -EFAULT;
2051
2052 return 0;
2053}
2054
2055static int enqueue_floating_irq(struct kvm_device *dev,
2056 struct kvm_device_attr *attr)
2057{
2058 struct kvm_s390_interrupt_info *inti = NULL;
2059 int r = 0;
2060 int len = attr->attr;
2061
2062 if (len % sizeof(struct kvm_s390_irq) != 0)
2063 return -EINVAL;
2064 else if (len > KVM_S390_FLIC_MAX_BUFFER)
2065 return -EINVAL;
2066
2067 while (len >= sizeof(struct kvm_s390_irq)) {
2068 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
2069 if (!inti)
2070 return -ENOMEM;
2071
2072 r = copy_irq_from_user(inti, attr->addr);
2073 if (r) {
2074 kfree(inti);
2075 return r;
2076 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01002077 r = __inject_vm(dev->kvm, inti);
2078 if (r) {
2079 kfree(inti);
2080 return r;
2081 }
Jens Freimannc05c4182013-10-07 16:13:45 +02002082 len -= sizeof(struct kvm_s390_irq);
2083 attr->addr += sizeof(struct kvm_s390_irq);
2084 }
2085
2086 return r;
2087}
2088
Cornelia Huck841b91c2013-07-15 13:36:01 +02002089static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
2090{
2091 if (id >= MAX_S390_IO_ADAPTERS)
2092 return NULL;
2093 return kvm->arch.adapters[id];
2094}
2095
2096static int register_io_adapter(struct kvm_device *dev,
2097 struct kvm_device_attr *attr)
2098{
2099 struct s390_io_adapter *adapter;
2100 struct kvm_s390_io_adapter adapter_info;
2101
2102 if (copy_from_user(&adapter_info,
2103 (void __user *)attr->addr, sizeof(adapter_info)))
2104 return -EFAULT;
2105
2106 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
2107 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
2108 return -EINVAL;
2109
2110 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
2111 if (!adapter)
2112 return -ENOMEM;
2113
2114 INIT_LIST_HEAD(&adapter->maps);
2115 init_rwsem(&adapter->maps_lock);
2116 atomic_set(&adapter->nr_maps, 0);
2117 adapter->id = adapter_info.id;
2118 adapter->isc = adapter_info.isc;
2119 adapter->maskable = adapter_info.maskable;
2120 adapter->masked = false;
2121 adapter->swap = adapter_info.swap;
Fei Li08fab502017-01-19 17:02:26 +01002122 adapter->suppressible = (adapter_info.flags) &
2123 KVM_S390_ADAPTER_SUPPRESSIBLE;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002124 dev->kvm->arch.adapters[adapter->id] = adapter;
2125
2126 return 0;
2127}
2128
2129int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
2130{
2131 int ret;
2132 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2133
2134 if (!adapter || !adapter->maskable)
2135 return -EINVAL;
2136 ret = adapter->masked;
2137 adapter->masked = masked;
2138 return ret;
2139}
2140
2141static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
2142{
2143 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2144 struct s390_map_info *map;
2145 int ret;
2146
2147 if (!adapter || !addr)
2148 return -EINVAL;
2149
2150 map = kzalloc(sizeof(*map), GFP_KERNEL);
2151 if (!map) {
2152 ret = -ENOMEM;
2153 goto out;
2154 }
2155 INIT_LIST_HEAD(&map->list);
2156 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02002157 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02002158 if (map->addr == -EFAULT) {
2159 ret = -EFAULT;
2160 goto out;
2161 }
2162 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
2163 if (ret < 0)
2164 goto out;
2165 BUG_ON(ret != 1);
2166 down_write(&adapter->maps_lock);
2167 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2168 list_add_tail(&map->list, &adapter->maps);
2169 ret = 0;
2170 } else {
2171 put_page(map->page);
2172 ret = -EINVAL;
2173 }
2174 up_write(&adapter->maps_lock);
2175out:
2176 if (ret)
2177 kfree(map);
2178 return ret;
2179}
2180
2181static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2182{
2183 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2184 struct s390_map_info *map, *tmp;
2185 int found = 0;
2186
2187 if (!adapter || !addr)
2188 return -EINVAL;
2189
2190 down_write(&adapter->maps_lock);
2191 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2192 if (map->guest_addr == addr) {
2193 found = 1;
2194 atomic_dec(&adapter->nr_maps);
2195 list_del(&map->list);
2196 put_page(map->page);
2197 kfree(map);
2198 break;
2199 }
2200 }
2201 up_write(&adapter->maps_lock);
2202
2203 return found ? 0 : -EINVAL;
2204}
2205
2206void kvm_s390_destroy_adapters(struct kvm *kvm)
2207{
2208 int i;
2209 struct s390_map_info *map, *tmp;
2210
2211 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2212 if (!kvm->arch.adapters[i])
2213 continue;
2214 list_for_each_entry_safe(map, tmp,
2215 &kvm->arch.adapters[i]->maps, list) {
2216 list_del(&map->list);
2217 put_page(map->page);
2218 kfree(map);
2219 }
2220 kfree(kvm->arch.adapters[i]);
2221 }
2222}
2223
2224static int modify_io_adapter(struct kvm_device *dev,
2225 struct kvm_device_attr *attr)
2226{
2227 struct kvm_s390_io_adapter_req req;
2228 struct s390_io_adapter *adapter;
2229 int ret;
2230
2231 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2232 return -EFAULT;
2233
2234 adapter = get_io_adapter(dev->kvm, req.id);
2235 if (!adapter)
2236 return -EINVAL;
2237 switch (req.type) {
2238 case KVM_S390_IO_ADAPTER_MASK:
2239 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2240 if (ret > 0)
2241 ret = 0;
2242 break;
2243 case KVM_S390_IO_ADAPTER_MAP:
2244 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2245 break;
2246 case KVM_S390_IO_ADAPTER_UNMAP:
2247 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2248 break;
2249 default:
2250 ret = -EINVAL;
2251 }
2252
2253 return ret;
2254}
2255
Halil Pasic6d28f782016-01-25 19:10:40 +01002256static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2257
2258{
2259 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2260 u32 schid;
2261
2262 if (attr->flags)
2263 return -EINVAL;
2264 if (attr->attr != sizeof(schid))
2265 return -EINVAL;
2266 if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2267 return -EFAULT;
Michael Mueller4dd6f172017-07-06 14:22:20 +02002268 if (!schid)
2269 return -EINVAL;
Halil Pasic6d28f782016-01-25 19:10:40 +01002270 kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2271 /*
2272 * If userspace is conforming to the architecture, we can have at most
2273 * one pending I/O interrupt per subchannel, so this is effectively a
2274 * clear all.
2275 */
2276 return 0;
2277}
2278
Fei Li51978392017-02-17 17:06:26 +08002279static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
2280{
2281 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2282 struct kvm_s390_ais_req req;
2283 int ret = 0;
2284
Christian Borntraeger1ba15b22017-05-31 10:18:55 +02002285 if (!test_kvm_facility(kvm, 72))
Fei Li51978392017-02-17 17:06:26 +08002286 return -ENOTSUPP;
2287
2288 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2289 return -EFAULT;
2290
2291 if (req.isc > MAX_ISC)
2292 return -EINVAL;
2293
2294 trace_kvm_s390_modify_ais_mode(req.isc,
2295 (fi->simm & AIS_MODE_MASK(req.isc)) ?
2296 (fi->nimm & AIS_MODE_MASK(req.isc)) ?
2297 2 : KVM_S390_AIS_MODE_SINGLE :
2298 KVM_S390_AIS_MODE_ALL, req.mode);
2299
2300 mutex_lock(&fi->ais_lock);
2301 switch (req.mode) {
2302 case KVM_S390_AIS_MODE_ALL:
2303 fi->simm &= ~AIS_MODE_MASK(req.isc);
2304 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2305 break;
2306 case KVM_S390_AIS_MODE_SINGLE:
2307 fi->simm |= AIS_MODE_MASK(req.isc);
2308 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2309 break;
2310 default:
2311 ret = -EINVAL;
2312 }
2313 mutex_unlock(&fi->ais_lock);
2314
2315 return ret;
2316}
2317
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002318static int kvm_s390_inject_airq(struct kvm *kvm,
2319 struct s390_io_adapter *adapter)
2320{
2321 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2322 struct kvm_s390_interrupt s390int = {
2323 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2324 .parm = 0,
Michael Mueller2496c8e2017-08-31 11:10:28 +02002325 .parm64 = isc_to_int_word(adapter->isc),
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002326 };
2327 int ret = 0;
2328
Christian Borntraeger1ba15b22017-05-31 10:18:55 +02002329 if (!test_kvm_facility(kvm, 72) || !adapter->suppressible)
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002330 return kvm_s390_inject_vm(kvm, &s390int);
2331
2332 mutex_lock(&fi->ais_lock);
2333 if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
2334 trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
2335 goto out;
2336 }
2337
2338 ret = kvm_s390_inject_vm(kvm, &s390int);
2339 if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
2340 fi->nimm |= AIS_MODE_MASK(adapter->isc);
2341 trace_kvm_s390_modify_ais_mode(adapter->isc,
2342 KVM_S390_AIS_MODE_SINGLE, 2);
2343 }
2344out:
2345 mutex_unlock(&fi->ais_lock);
2346 return ret;
2347}
2348
2349static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr)
2350{
2351 unsigned int id = attr->attr;
2352 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2353
2354 if (!adapter)
2355 return -EINVAL;
2356
2357 return kvm_s390_inject_airq(kvm, adapter);
2358}
2359
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002360static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr)
2361{
2362 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2363 struct kvm_s390_ais_all ais;
2364
2365 if (!test_kvm_facility(kvm, 72))
2366 return -ENOTSUPP;
2367
2368 if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais)))
2369 return -EFAULT;
2370
2371 mutex_lock(&fi->ais_lock);
2372 fi->simm = ais.simm;
2373 fi->nimm = ais.nimm;
2374 mutex_unlock(&fi->ais_lock);
2375
2376 return 0;
2377}
2378
Jens Freimannc05c4182013-10-07 16:13:45 +02002379static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2380{
2381 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002382 unsigned int i;
2383 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02002384
2385 switch (attr->group) {
2386 case KVM_DEV_FLIC_ENQUEUE:
2387 r = enqueue_floating_irq(dev, attr);
2388 break;
2389 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01002390 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02002391 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002392 case KVM_DEV_FLIC_APF_ENABLE:
2393 dev->kvm->arch.gmap->pfault_enabled = 1;
2394 break;
2395 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2396 dev->kvm->arch.gmap->pfault_enabled = 0;
2397 /*
2398 * Make sure no async faults are in transition when
2399 * clearing the queues. So we don't need to worry
2400 * about late coming workers.
2401 */
2402 synchronize_srcu(&dev->kvm->srcu);
2403 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2404 kvm_clear_async_pf_completion_queue(vcpu);
2405 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002406 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2407 r = register_io_adapter(dev, attr);
2408 break;
2409 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2410 r = modify_io_adapter(dev, attr);
2411 break;
Halil Pasic6d28f782016-01-25 19:10:40 +01002412 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2413 r = clear_io_irq(dev->kvm, attr);
2414 break;
Fei Li51978392017-02-17 17:06:26 +08002415 case KVM_DEV_FLIC_AISM:
2416 r = modify_ais_mode(dev->kvm, attr);
2417 break;
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002418 case KVM_DEV_FLIC_AIRQ_INJECT:
2419 r = flic_inject_airq(dev->kvm, attr);
2420 break;
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002421 case KVM_DEV_FLIC_AISM_ALL:
2422 r = flic_ais_mode_set_all(dev->kvm, attr);
2423 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002424 default:
2425 r = -EINVAL;
2426 }
2427
2428 return r;
2429}
2430
Halil Pasic4f129852016-02-25 12:44:17 +01002431static int flic_has_attr(struct kvm_device *dev,
2432 struct kvm_device_attr *attr)
2433{
2434 switch (attr->group) {
2435 case KVM_DEV_FLIC_GET_ALL_IRQS:
2436 case KVM_DEV_FLIC_ENQUEUE:
2437 case KVM_DEV_FLIC_CLEAR_IRQS:
2438 case KVM_DEV_FLIC_APF_ENABLE:
2439 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2440 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2441 case KVM_DEV_FLIC_ADAPTER_MODIFY:
Halil Pasic6d28f782016-01-25 19:10:40 +01002442 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
Fei Li51978392017-02-17 17:06:26 +08002443 case KVM_DEV_FLIC_AISM:
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002444 case KVM_DEV_FLIC_AIRQ_INJECT:
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002445 case KVM_DEV_FLIC_AISM_ALL:
Halil Pasic4f129852016-02-25 12:44:17 +01002446 return 0;
2447 }
2448 return -ENXIO;
2449}
2450
Jens Freimannc05c4182013-10-07 16:13:45 +02002451static int flic_create(struct kvm_device *dev, u32 type)
2452{
2453 if (!dev)
2454 return -EINVAL;
2455 if (dev->kvm->arch.flic)
2456 return -EINVAL;
2457 dev->kvm->arch.flic = dev;
2458 return 0;
2459}
2460
2461static void flic_destroy(struct kvm_device *dev)
2462{
2463 dev->kvm->arch.flic = NULL;
2464 kfree(dev);
2465}
2466
2467/* s390 floating irq controller (flic) */
2468struct kvm_device_ops kvm_flic_ops = {
2469 .name = "kvm-flic",
2470 .get_attr = flic_get_attr,
2471 .set_attr = flic_set_attr,
Halil Pasic4f129852016-02-25 12:44:17 +01002472 .has_attr = flic_has_attr,
Jens Freimannc05c4182013-10-07 16:13:45 +02002473 .create = flic_create,
2474 .destroy = flic_destroy,
2475};
Cornelia Huck84223592013-07-15 13:36:01 +02002476
2477static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2478{
2479 unsigned long bit;
2480
2481 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2482
2483 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2484}
2485
2486static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2487 u64 addr)
2488{
2489 struct s390_map_info *map;
2490
2491 if (!adapter)
2492 return NULL;
2493
2494 list_for_each_entry(map, &adapter->maps, list) {
2495 if (map->guest_addr == addr)
2496 return map;
2497 }
2498 return NULL;
2499}
2500
2501static int adapter_indicators_set(struct kvm *kvm,
2502 struct s390_io_adapter *adapter,
2503 struct kvm_s390_adapter_int *adapter_int)
2504{
2505 unsigned long bit;
2506 int summary_set, idx;
2507 struct s390_map_info *info;
2508 void *map;
2509
2510 info = get_map_info(adapter, adapter_int->ind_addr);
2511 if (!info)
2512 return -1;
2513 map = page_address(info->page);
2514 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2515 set_bit(bit, map);
2516 idx = srcu_read_lock(&kvm->srcu);
2517 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2518 set_page_dirty_lock(info->page);
2519 info = get_map_info(adapter, adapter_int->summary_addr);
2520 if (!info) {
2521 srcu_read_unlock(&kvm->srcu, idx);
2522 return -1;
2523 }
2524 map = page_address(info->page);
2525 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2526 adapter->swap);
2527 summary_set = test_and_set_bit(bit, map);
2528 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2529 set_page_dirty_lock(info->page);
2530 srcu_read_unlock(&kvm->srcu, idx);
2531 return summary_set ? 0 : 1;
2532}
2533
2534/*
2535 * < 0 - not injected due to error
2536 * = 0 - coalesced, summary indicator already active
2537 * > 0 - injected interrupt
2538 */
2539static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2540 struct kvm *kvm, int irq_source_id, int level,
2541 bool line_status)
2542{
2543 int ret;
2544 struct s390_io_adapter *adapter;
2545
2546 /* We're only interested in the 0->1 transition. */
2547 if (!level)
2548 return 0;
2549 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2550 if (!adapter)
2551 return -1;
2552 down_read(&adapter->maps_lock);
2553 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2554 up_read(&adapter->maps_lock);
2555 if ((ret > 0) && !adapter->masked) {
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002556 ret = kvm_s390_inject_airq(kvm, adapter);
Cornelia Huck84223592013-07-15 13:36:01 +02002557 if (ret == 0)
2558 ret = 1;
2559 }
2560 return ret;
2561}
2562
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002563/*
2564 * Inject the machine check to the guest.
2565 */
2566void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
2567 struct mcck_volatile_info *mcck_info)
2568{
2569 struct kvm_s390_interrupt_info inti;
2570 struct kvm_s390_irq irq;
2571 struct kvm_s390_mchk_info *mchk;
2572 union mci mci;
2573 __u64 cr14 = 0; /* upper bits are not used */
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002574 int rc;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002575
2576 mci.val = mcck_info->mcic;
2577 if (mci.sr)
Martin Schwidefskycc654502017-10-12 13:24:46 +02002578 cr14 |= CR14_RECOVERY_SUBMASK;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002579 if (mci.dg)
Martin Schwidefskycc654502017-10-12 13:24:46 +02002580 cr14 |= CR14_DEGRADATION_SUBMASK;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002581 if (mci.w)
Martin Schwidefskycc654502017-10-12 13:24:46 +02002582 cr14 |= CR14_WARNING_SUBMASK;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002583
2584 mchk = mci.ck ? &inti.mchk : &irq.u.mchk;
2585 mchk->cr14 = cr14;
2586 mchk->mcic = mcck_info->mcic;
2587 mchk->ext_damage_code = mcck_info->ext_damage_code;
2588 mchk->failing_storage_address = mcck_info->failing_storage_address;
2589 if (mci.ck) {
2590 /* Inject the floating machine check */
2591 inti.type = KVM_S390_MCHK;
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002592 rc = __inject_vm(vcpu->kvm, &inti);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002593 } else {
2594 /* Inject the machine check to specified vcpu */
2595 irq.type = KVM_S390_MCHK;
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002596 rc = kvm_s390_inject_vcpu(vcpu, &irq);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002597 }
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002598 WARN_ON_ONCE(rc);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002599}
2600
Radim Krčmářc63cf532016-07-12 22:09:26 +02002601int kvm_set_routing_entry(struct kvm *kvm,
2602 struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002603 const struct kvm_irq_routing_entry *ue)
2604{
2605 int ret;
2606
2607 switch (ue->type) {
2608 case KVM_IRQ_ROUTING_S390_ADAPTER:
2609 e->set = set_adapter_int;
2610 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2611 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2612 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2613 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2614 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2615 ret = 0;
2616 break;
2617 default:
2618 ret = -EINVAL;
2619 }
2620
2621 return ret;
2622}
2623
2624int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2625 int irq_source_id, int level, bool line_status)
2626{
2627 return -EINVAL;
2628}
Jens Freimann816c7662014-11-24 17:13:46 +01002629
2630int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2631{
2632 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2633 struct kvm_s390_irq *buf;
2634 int r = 0;
2635 int n;
2636
2637 buf = vmalloc(len);
2638 if (!buf)
2639 return -ENOMEM;
2640
2641 if (copy_from_user((void *) buf, irqstate, len)) {
2642 r = -EFAULT;
2643 goto out_free;
2644 }
2645
2646 /*
2647 * Don't allow setting the interrupt state
2648 * when there are already interrupts pending
2649 */
2650 spin_lock(&li->lock);
2651 if (li->pending_irqs) {
2652 r = -EBUSY;
2653 goto out_unlock;
2654 }
2655
2656 for (n = 0; n < len / sizeof(*buf); n++) {
2657 r = do_inject_vcpu(vcpu, &buf[n]);
2658 if (r)
2659 break;
2660 }
2661
2662out_unlock:
2663 spin_unlock(&li->lock);
2664out_free:
2665 vfree(buf);
2666
2667 return r;
2668}
2669
2670static void store_local_irq(struct kvm_s390_local_interrupt *li,
2671 struct kvm_s390_irq *irq,
2672 unsigned long irq_type)
2673{
2674 switch (irq_type) {
2675 case IRQ_PEND_MCHK_EX:
2676 case IRQ_PEND_MCHK_REP:
2677 irq->type = KVM_S390_MCHK;
2678 irq->u.mchk = li->irq.mchk;
2679 break;
2680 case IRQ_PEND_PROG:
2681 irq->type = KVM_S390_PROGRAM_INT;
2682 irq->u.pgm = li->irq.pgm;
2683 break;
2684 case IRQ_PEND_PFAULT_INIT:
2685 irq->type = KVM_S390_INT_PFAULT_INIT;
2686 irq->u.ext = li->irq.ext;
2687 break;
2688 case IRQ_PEND_EXT_EXTERNAL:
2689 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2690 irq->u.extcall = li->irq.extcall;
2691 break;
2692 case IRQ_PEND_EXT_CLOCK_COMP:
2693 irq->type = KVM_S390_INT_CLOCK_COMP;
2694 break;
2695 case IRQ_PEND_EXT_CPU_TIMER:
2696 irq->type = KVM_S390_INT_CPU_TIMER;
2697 break;
2698 case IRQ_PEND_SIGP_STOP:
2699 irq->type = KVM_S390_SIGP_STOP;
2700 irq->u.stop = li->irq.stop;
2701 break;
2702 case IRQ_PEND_RESTART:
2703 irq->type = KVM_S390_RESTART;
2704 break;
2705 case IRQ_PEND_SET_PREFIX:
2706 irq->type = KVM_S390_SIGP_SET_PREFIX;
2707 irq->u.prefix = li->irq.prefix;
2708 break;
2709 }
2710}
2711
2712int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2713{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002714 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002715 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2716 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2717 unsigned long pending_irqs;
2718 struct kvm_s390_irq irq;
2719 unsigned long irq_type;
2720 int cpuaddr;
2721 int n = 0;
2722
2723 spin_lock(&li->lock);
2724 pending_irqs = li->pending_irqs;
2725 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2726 sizeof(sigp_emerg_pending));
2727 spin_unlock(&li->lock);
2728
2729 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2730 memset(&irq, 0, sizeof(irq));
2731 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2732 continue;
2733 if (n + sizeof(irq) > len)
2734 return -ENOBUFS;
2735 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2736 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2737 return -EFAULT;
2738 n += sizeof(irq);
2739 }
2740
2741 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2742 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2743 memset(&irq, 0, sizeof(irq));
2744 if (n + sizeof(irq) > len)
2745 return -ENOBUFS;
2746 irq.type = KVM_S390_INT_EMERGENCY;
2747 irq.u.emerg.code = cpuaddr;
2748 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2749 return -EFAULT;
2750 n += sizeof(irq);
2751 }
2752 }
2753
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002754 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002755 if (n + sizeof(irq) > len)
2756 return -ENOBUFS;
2757 memset(&irq, 0, sizeof(irq));
2758 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002759 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002760 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2761 return -EFAULT;
2762 n += sizeof(irq);
2763 }
2764
2765 return n;
2766}
Michael Muellerd7c5cb02017-06-12 14:15:19 +02002767
2768void kvm_s390_gisa_clear(struct kvm *kvm)
2769{
2770 if (kvm->arch.gisa) {
2771 memset(kvm->arch.gisa, 0, sizeof(struct kvm_s390_gisa));
2772 kvm->arch.gisa->next_alert = (u32)(u64)kvm->arch.gisa;
2773 VM_EVENT(kvm, 3, "gisa 0x%pK cleared", kvm->arch.gisa);
2774 }
2775}
2776
2777void kvm_s390_gisa_init(struct kvm *kvm)
2778{
2779 /* not implemented yet */
2780}
2781
2782void kvm_s390_gisa_destroy(struct kvm *kvm)
2783{
2784 if (!kvm->arch.gisa)
2785 return;
2786 kvm->arch.gisa = NULL;
2787}