blob: a3da4f3065aa3de50e8e7daeab8c466317d8dea5 [file] [log] [blame]
Carsten Otteba5c1e92008-03-25 18:47:26 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01003 *
Thomas Huth33b412a2015-02-11 10:38:46 +01004 * Copyright IBM Corp. 2008, 2015
Carsten Otteba5c1e92008-03-25 18:47:26 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 */
12
Christian Borntraegerca872302009-05-12 17:21:49 +020013#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010014#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010015#include <linux/hrtimer.h>
Cornelia Huck84223592013-07-15 13:36:01 +020016#include <linux/mmu_context.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020017#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Jens Freimann383d0b02014-07-29 15:11:49 +020019#include <linux/bitmap.h>
Jens Freimann94aa0332015-03-16 12:17:13 +010020#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010021#include <asm/asm-offsets.h>
Thomas Huth33b412a2015-02-11 10:38:46 +010022#include <asm/dis.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
David Hildenbrandea5f4962014-10-14 15:29:30 +020024#include <asm/sclp.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020025#include <asm/isc.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010026#include <asm/gmap.h>
David Hildenbrand0319dae2016-08-03 11:18:57 +020027#include <asm/switch_to.h>
David Hildenbrandff5dc142015-10-14 16:57:56 +020028#include <asm/nmi.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010029#include "kvm-s390.h"
30#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020031#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010032
Jens Freimann44c6ca32014-04-16 13:57:18 +020033#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010034#define PFAULT_DONE 0x0680
35#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010036
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020037/* handle external calls via sigp interpretation facility */
38static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id)
39{
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020040 int c, scn;
41
David Hildenbrand2c1bb2b2015-09-23 09:45:50 +020042 if (!(atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND))
43 return 0;
44
David Hildenbranda6940672016-08-08 22:39:32 +020045 BUG_ON(!kvm_s390_use_sca_entries());
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020046 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020047 if (vcpu->kvm->arch.use_esca) {
48 struct esca_block *sca = vcpu->kvm->arch.sca;
49 union esca_sigp_ctrl sigp_ctrl =
50 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
51
52 c = sigp_ctrl.c;
53 scn = sigp_ctrl.scn;
54 } else {
55 struct bsca_block *sca = vcpu->kvm->arch.sca;
56 union bsca_sigp_ctrl sigp_ctrl =
57 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
58
59 c = sigp_ctrl.c;
60 scn = sigp_ctrl.scn;
61 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020062 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020063
64 if (src_id)
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020065 *src_id = scn;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020066
David Hildenbrand2c1bb2b2015-09-23 09:45:50 +020067 return c;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020068}
69
70static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
71{
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020072 int expect, rc;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020073
David Hildenbranda6940672016-08-08 22:39:32 +020074 BUG_ON(!kvm_s390_use_sca_entries());
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020075 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020076 if (vcpu->kvm->arch.use_esca) {
77 struct esca_block *sca = vcpu->kvm->arch.sca;
78 union esca_sigp_ctrl *sigp_ctrl =
79 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
80 union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020081
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +020082 new_val.scn = src_id;
83 new_val.c = 1;
84 old_val.c = 0;
85
86 expect = old_val.value;
87 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
88 } else {
89 struct bsca_block *sca = vcpu->kvm->arch.sca;
90 union bsca_sigp_ctrl *sigp_ctrl =
91 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
92 union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
93
94 new_val.scn = src_id;
95 new_val.c = 1;
96 old_val.c = 0;
97
98 expect = old_val.value;
99 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
100 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +0200101 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +0200102
103 if (rc != expect) {
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200104 /* another external call is pending */
105 return -EBUSY;
106 }
107 atomic_or(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
108 return 0;
109}
110
111static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
112{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200113 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +0200114 int rc, expect;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200115
David Hildenbranda6940672016-08-08 22:39:32 +0200116 if (!kvm_s390_use_sca_entries())
117 return;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200118 atomic_andnot(CPUSTAT_ECALL_PEND, li->cpuflags);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +0200119 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +0200120 if (vcpu->kvm->arch.use_esca) {
121 struct esca_block *sca = vcpu->kvm->arch.sca;
122 union esca_sigp_ctrl *sigp_ctrl =
123 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
124 union esca_sigp_ctrl old = *sigp_ctrl;
125
126 expect = old.value;
127 rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
128 } else {
129 struct bsca_block *sca = vcpu->kvm->arch.sca;
130 union bsca_sigp_ctrl *sigp_ctrl =
131 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
132 union bsca_sigp_ctrl old = *sigp_ctrl;
133
134 expect = old.value;
135 rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
136 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +0200137 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43bafcf2015-04-22 17:09:44 +0200138 WARN_ON(rc != expect); /* cannot clear? */
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200139}
140
Dominik Dingel3c038e62013-10-07 17:11:48 +0200141int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100142{
143 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
144}
145
Cornelia Huckd8346b72012-12-20 15:32:08 +0100146static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
147{
148 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
149}
150
Cornelia Huck48a3e952012-12-20 15:32:09 +0100151static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
152{
153 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
154}
155
Carsten Otteba5c1e92008-03-25 18:47:26 +0100156static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
157{
David Hildenbrandfee0e0f2015-09-28 13:32:38 +0200158 return psw_extint_disabled(vcpu) &&
159 psw_ioint_disabled(vcpu) &&
160 psw_mchk_disabled(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100161}
162
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100163static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
164{
165 if (psw_extint_disabled(vcpu) ||
166 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
167 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +0100168 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
169 /* No timer interrupts when single stepping */
170 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100171 return 1;
172}
173
David Hildenbrandb4aec922014-12-01 15:55:42 +0100174static int ckc_irq_pending(struct kvm_vcpu *vcpu)
175{
David Hildenbrand60417fc2015-09-29 16:20:36 +0200176 if (vcpu->arch.sie_block->ckc >= kvm_s390_get_tod_clock_fast(vcpu->kvm))
David Hildenbrandb4aec922014-12-01 15:55:42 +0100177 return 0;
178 return ckc_interrupts_enabled(vcpu);
179}
180
181static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
182{
183 return !psw_extint_disabled(vcpu) &&
184 (vcpu->arch.sie_block->gcr[0] & 0x400ul);
185}
186
187static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
188{
David Hildenbrand4287f242016-02-15 09:40:12 +0100189 if (!cpu_timer_interrupts_enabled(vcpu))
190 return 0;
191 return kvm_s390_get_cpu_timer(vcpu) >> 63;
David Hildenbrandb4aec922014-12-01 15:55:42 +0100192}
193
Jens Freimann6d3da242013-07-03 15:18:35 +0200194static inline int is_ioirq(unsigned long irq_type)
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100195{
Jens Freimann6d3da242013-07-03 15:18:35 +0200196 return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
197 (irq_type <= IRQ_PEND_IO_ISC_7));
198}
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100199
Jens Freimann6d3da242013-07-03 15:18:35 +0200200static uint64_t isc_to_isc_bits(int isc)
201{
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100202 return (0x80 >> isc) << 24;
203}
204
Jens Freimann6d3da242013-07-03 15:18:35 +0200205static inline u8 int_word_to_isc(u32 int_word)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100206{
Jens Freimann6d3da242013-07-03 15:18:35 +0200207 return (int_word & 0x38000000) >> 27;
208}
209
David Hildenbrand5f94c582015-09-28 14:27:51 +0200210static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
Jens Freimann6d3da242013-07-03 15:18:35 +0200211{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200212 return vcpu->kvm->arch.float_int.pending_irqs |
213 vcpu->arch.local_int.pending_irqs;
Jens Freimann383d0b02014-07-29 15:11:49 +0200214}
215
Michael Muelleree739f42017-07-03 15:32:50 +0200216static inline int isc_to_irq_type(unsigned long isc)
217{
218 return IRQ_PEND_IO_ISC_0 + isc;
219}
220
221static inline int irq_type_to_isc(unsigned long irq_type)
222{
223 return irq_type - IRQ_PEND_IO_ISC_0;
224}
225
Jens Freimann6d3da242013-07-03 15:18:35 +0200226static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
227 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200228{
Jens Freimann6d3da242013-07-03 15:18:35 +0200229 int i;
230
231 for (i = 0; i <= MAX_ISC; i++)
232 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
Michael Muelleree739f42017-07-03 15:32:50 +0200233 active_mask &= ~(1UL << (isc_to_irq_type(i)));
Jens Freimann6d3da242013-07-03 15:18:35 +0200234
235 return active_mask;
236}
237
238static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
239{
240 unsigned long active_mask;
241
David Hildenbrand5f94c582015-09-28 14:27:51 +0200242 active_mask = pending_irqs(vcpu);
Jens Freimannffeca0a2015-04-17 10:21:04 +0200243 if (!active_mask)
244 return 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200245
246 if (psw_extint_disabled(vcpu))
247 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200248 if (psw_ioint_disabled(vcpu))
249 active_mask &= ~IRQ_PEND_IO_MASK;
250 else
251 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200252 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
253 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
254 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
255 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
256 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
257 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
258 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
259 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200260 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
261 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200262 if (psw_mchk_disabled(vcpu))
263 active_mask &= ~IRQ_PEND_MCHK_MASK;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +0200264 /*
265 * Check both floating and local interrupt's cr14 because
266 * bit IRQ_PEND_MCHK_REP could be set in both cases.
267 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200268 if (!(vcpu->arch.sie_block->gcr[14] &
QingFeng Hao4d62fcc2017-06-07 12:03:05 +0200269 (vcpu->kvm->arch.float_int.mchk.cr14 |
270 vcpu->arch.local_int.irq.mchk.cr14)))
Jens Freimann6d3da242013-07-03 15:18:35 +0200271 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200272
David Hildenbrand6cddd432014-10-15 16:48:53 +0200273 /*
274 * STOP irqs will never be actively delivered. They are triggered via
275 * intercept requests and cleared when the stop intercept is performed.
276 */
277 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
278
Jens Freimann383d0b02014-07-29 15:11:49 +0200279 return active_mask;
280}
281
Carsten Otteba5c1e92008-03-25 18:47:26 +0100282static void __set_cpu_idle(struct kvm_vcpu *vcpu)
283{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200284 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100285 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
286}
287
288static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
289{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200290 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100291 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
292}
293
294static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
295{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200296 atomic_andnot(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
297 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100298 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100299 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
300
301 if (guestdbg_enabled(vcpu)) {
302 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
303 LCTL_CR10 | LCTL_CR11);
304 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
305 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100306}
307
308static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
309{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200310 atomic_or(flag, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100311}
312
Jens Freimann6d3da242013-07-03 15:18:35 +0200313static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
314{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200315 if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
Jens Freimann6d3da242013-07-03 15:18:35 +0200316 return;
317 else if (psw_ioint_disabled(vcpu))
318 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
319 else
320 vcpu->arch.sie_block->lctl |= LCTL_CR6;
321}
322
Jens Freimann383d0b02014-07-29 15:11:49 +0200323static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
324{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200325 if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200326 return;
327 if (psw_extint_disabled(vcpu))
328 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
329 else
330 vcpu->arch.sie_block->lctl |= LCTL_CR0;
331}
332
333static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
334{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200335 if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200336 return;
337 if (psw_mchk_disabled(vcpu))
338 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
339 else
340 vcpu->arch.sie_block->lctl |= LCTL_CR14;
341}
342
David Hildenbrand6cddd432014-10-15 16:48:53 +0200343static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
344{
345 if (kvm_s390_is_stop_irq_pending(vcpu))
346 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
347}
348
Jens Freimann6d3da242013-07-03 15:18:35 +0200349/* Set interception request for non-deliverable interrupts */
350static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200351{
Jens Freimann6d3da242013-07-03 15:18:35 +0200352 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200353 set_intercept_indicators_ext(vcpu);
354 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200355 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200356}
357
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200358static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100359{
Jens Freimann383d0b02014-07-29 15:11:49 +0200360 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200361 int rc;
362
363 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
364 0, 0);
365
366 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
367 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100368 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200369 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
370 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
371 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
372 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200373 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100374 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200375}
376
377static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
378{
Jens Freimann383d0b02014-07-29 15:11:49 +0200379 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200380 int rc;
381
382 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
383 0, 0);
384
385 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
386 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100387 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200388 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
389 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
390 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
391 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200392 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100393 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200394}
395
Jens Freimann383d0b02014-07-29 15:11:49 +0200396static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200397{
Jens Freimann383d0b02014-07-29 15:11:49 +0200398 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
399 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200400 int rc;
401
Jens Freimann383d0b02014-07-29 15:11:49 +0200402 spin_lock(&li->lock);
403 ext = li->irq.ext;
404 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
405 li->irq.ext.ext_params2 = 0;
406 spin_unlock(&li->lock);
407
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200408 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
409 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200410 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
411 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200412 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200413
414 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
415 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
416 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
417 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
418 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
419 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200420 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100421 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200422}
423
David Hildenbrandd6404de2015-10-14 16:47:36 +0200424static int __write_machine_check(struct kvm_vcpu *vcpu,
425 struct kvm_s390_mchk_info *mchk)
426{
427 unsigned long ext_sa_addr;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100428 unsigned long lc;
David Hildenbrand0319dae2016-08-03 11:18:57 +0200429 freg_t fprs[NUM_FPRS];
David Hildenbrandff5dc142015-10-14 16:57:56 +0200430 union mci mci;
David Hildenbrandd6404de2015-10-14 16:47:36 +0200431 int rc;
432
David Hildenbrandff5dc142015-10-14 16:57:56 +0200433 mci.val = mchk->mcic;
Christian Borntraeger31d8b8d2016-11-10 14:22:02 +0100434 /* take care of lazy register loading */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200435 save_fpu_regs();
436 save_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger80248552017-04-12 12:59:59 +0200437 if (MACHINE_HAS_GS && vcpu->arch.gs_enabled)
438 save_gs_cb(current->thread.gs_cb);
David Hildenbrand0319dae2016-08-03 11:18:57 +0200439
David Hildenbrandd6404de2015-10-14 16:47:36 +0200440 /* Extended save area */
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100441 rc = read_guest_lc(vcpu, __LC_MCESAD, &ext_sa_addr,
442 sizeof(unsigned long));
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100443 /* Only bits 0 through 63-LC are used for address formation */
444 lc = ext_sa_addr & MCESA_LC_MASK;
445 if (test_kvm_facility(vcpu->kvm, 133)) {
446 switch (lc) {
447 case 0:
448 case 10:
449 ext_sa_addr &= ~0x3ffUL;
450 break;
451 case 11:
452 ext_sa_addr &= ~0x7ffUL;
453 break;
454 case 12:
455 ext_sa_addr &= ~0xfffUL;
456 break;
457 default:
458 ext_sa_addr = 0;
459 break;
460 }
461 } else {
462 ext_sa_addr &= ~0x3ffUL;
463 }
464
David Hildenbrandff5dc142015-10-14 16:57:56 +0200465 if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) {
466 if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs,
467 512))
468 mci.vr = 0;
469 } else {
470 mci.vr = 0;
471 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100472 if (!rc && mci.gs && ext_sa_addr && test_kvm_facility(vcpu->kvm, 133)
473 && (lc == 11 || lc == 12)) {
474 if (write_guest_abs(vcpu, ext_sa_addr + 1024,
475 &vcpu->run->s.regs.gscb, 32))
476 mci.gs = 0;
477 } else {
478 mci.gs = 0;
479 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200480
481 /* General interruption information */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200482 rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200483 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
484 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
485 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
486 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
David Hildenbrandff5dc142015-10-14 16:57:56 +0200487 rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200488
489 /* Register-save areas */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200490 if (MACHINE_HAS_VX) {
491 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
492 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128);
493 } else {
494 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA,
495 vcpu->run->s.regs.fprs, 128);
496 }
497 rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA,
498 vcpu->run->s.regs.gprs, 128);
499 rc |= put_guest_lc(vcpu, current->thread.fpu.fpc,
500 (u32 __user *) __LC_FP_CREG_SAVE_AREA);
501 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr,
502 (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA);
503 rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu),
504 (u64 __user *) __LC_CPU_TIMER_SAVE_AREA);
505 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8,
506 (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA);
507 rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA,
508 &vcpu->run->s.regs.acrs, 64);
509 rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA,
510 &vcpu->arch.sie_block->gcr, 128);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200511
512 /* Extended interruption information */
David Hildenbrand8953fb02016-08-03 12:25:08 +0200513 rc |= put_guest_lc(vcpu, mchk->ext_damage_code,
514 (u32 __user *) __LC_EXT_DAMAGE_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200515 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
516 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
517 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
518 sizeof(mchk->fixed_logout));
519 return rc ? -EFAULT : 0;
520}
521
Jens Freimann383d0b02014-07-29 15:11:49 +0200522static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200523{
Jens Freimann6d3da242013-07-03 15:18:35 +0200524 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200525 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200526 struct kvm_s390_mchk_info mchk = {};
Jens Freimann6d3da242013-07-03 15:18:35 +0200527 int deliver = 0;
528 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200529
Jens Freimann6d3da242013-07-03 15:18:35 +0200530 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200531 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200532 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
533 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
534 /*
535 * If there was an exigent machine check pending, then any
536 * repressible machine checks that might have been pending
537 * are indicated along with it, so always clear bits for
538 * repressible and exigent interrupts
539 */
540 mchk = li->irq.mchk;
541 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
542 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
543 memset(&li->irq.mchk, 0, sizeof(mchk));
544 deliver = 1;
545 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200546 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200547 * We indicate floating repressible conditions along with
548 * other pending conditions. Channel Report Pending and Channel
549 * Subsystem damage are the only two and and are indicated by
550 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200551 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200552 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
553 mchk.mcic |= fi->mchk.mcic;
554 mchk.cr14 |= fi->mchk.cr14;
555 memset(&fi->mchk, 0, sizeof(mchk));
556 deliver = 1;
557 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200558 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200559 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200560
Jens Freimann6d3da242013-07-03 15:18:35 +0200561 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200562 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200563 mchk.mcic);
564 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
565 KVM_S390_MCHK,
566 mchk.cr14, mchk.mcic);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200567 rc = __write_machine_check(vcpu, &mchk);
Jens Freimann6d3da242013-07-03 15:18:35 +0200568 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200569 return rc;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200570}
571
572static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
573{
Jens Freimann383d0b02014-07-29 15:11:49 +0200574 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200575 int rc;
576
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200577 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200578 vcpu->stat.deliver_restart_signal++;
579 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
580
581 rc = write_guest_lc(vcpu,
Heiko Carstensc667aea2015-12-31 10:29:00 +0100582 offsetof(struct lowcore, restart_old_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200583 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstensc667aea2015-12-31 10:29:00 +0100584 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200585 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200586 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100587 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200588}
589
Jens Freimann383d0b02014-07-29 15:11:49 +0200590static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200591{
Jens Freimann383d0b02014-07-29 15:11:49 +0200592 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
593 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200594
Jens Freimann383d0b02014-07-29 15:11:49 +0200595 spin_lock(&li->lock);
596 prefix = li->irq.prefix;
597 li->irq.prefix.address = 0;
598 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
599 spin_unlock(&li->lock);
600
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200601 vcpu->stat.deliver_prefix_signal++;
602 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
603 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200604 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200605
Jens Freimann383d0b02014-07-29 15:11:49 +0200606 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200607 return 0;
608}
609
Jens Freimann383d0b02014-07-29 15:11:49 +0200610static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200611{
Jens Freimann383d0b02014-07-29 15:11:49 +0200612 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200613 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200614 int cpu_addr;
615
616 spin_lock(&li->lock);
617 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
618 clear_bit(cpu_addr, li->sigp_emerg_pending);
619 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
620 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
621 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200622
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200623 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200624 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200625 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
626 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200627
628 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
629 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200630 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200631 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
632 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
633 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
634 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100635 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200636}
637
Jens Freimann383d0b02014-07-29 15:11:49 +0200638static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200639{
Jens Freimann383d0b02014-07-29 15:11:49 +0200640 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
641 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200642 int rc;
643
Jens Freimann383d0b02014-07-29 15:11:49 +0200644 spin_lock(&li->lock);
645 extcall = li->irq.extcall;
646 li->irq.extcall.code = 0;
647 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
648 spin_unlock(&li->lock);
649
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200650 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200651 vcpu->stat.deliver_external_call++;
652 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
653 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200654 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200655
656 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
657 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200658 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200659 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
660 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
661 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
662 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100663 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200664}
665
Jens Freimann383d0b02014-07-29 15:11:49 +0200666static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200667{
Jens Freimann383d0b02014-07-29 15:11:49 +0200668 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
669 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100670 int rc = 0, nullifying = false;
David Hildenbrand634790b2015-11-04 16:33:33 +0100671 u16 ilen;
David Hildenbrand87128362014-03-03 10:55:13 +0100672
Jens Freimann383d0b02014-07-29 15:11:49 +0200673 spin_lock(&li->lock);
674 pgm_info = li->irq.pgm;
675 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
676 memset(&li->irq.pgm, 0, sizeof(pgm_info));
677 spin_unlock(&li->lock);
678
David Hildenbrand634790b2015-11-04 16:33:33 +0100679 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100680 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
681 pgm_info.code, ilen);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200682 vcpu->stat.deliver_program_int++;
683 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200684 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200685
Jens Freimann383d0b02014-07-29 15:11:49 +0200686 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100687 case PGM_AFX_TRANSLATION:
688 case PGM_ASX_TRANSLATION:
689 case PGM_EX_TRANSLATION:
690 case PGM_LFX_TRANSLATION:
691 case PGM_LSTE_SEQUENCE:
692 case PGM_LSX_TRANSLATION:
693 case PGM_LX_TRANSLATION:
694 case PGM_PRIMARY_AUTHORITY:
695 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100696 nullifying = true;
697 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100698 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200699 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100700 (u64 *)__LC_TRANS_EXC_CODE);
701 break;
702 case PGM_ALEN_TRANSLATION:
703 case PGM_ALE_SEQUENCE:
704 case PGM_ASTE_INSTANCE:
705 case PGM_ASTE_SEQUENCE:
706 case PGM_ASTE_VALIDITY:
707 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200708 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100709 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100710 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100711 break;
712 case PGM_ASCE_TYPE:
713 case PGM_PAGE_TRANSLATION:
714 case PGM_REGION_FIRST_TRANS:
715 case PGM_REGION_SECOND_TRANS:
716 case PGM_REGION_THIRD_TRANS:
717 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200718 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100719 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200720 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100721 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200722 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100723 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100724 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100725 break;
726 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200727 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200728 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200729 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100730 (u64 *)__LC_MON_CODE);
731 break;
Eric Farman403c8642015-02-02 15:01:06 -0500732 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100733 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200734 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100735 (u32 *)__LC_DATA_EXC_CODE);
736 break;
737 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200738 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100739 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200740 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100741 (u8 *)__LC_EXC_ACCESS_ID);
742 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100743 case PGM_STACK_FULL:
744 case PGM_STACK_EMPTY:
745 case PGM_STACK_SPECIFICATION:
746 case PGM_STACK_TYPE:
747 case PGM_STACK_OPERATION:
748 case PGM_TRACE_TABEL:
749 case PGM_CRYPTO_OPERATION:
750 nullifying = true;
751 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100752 }
753
Jens Freimann383d0b02014-07-29 15:11:49 +0200754 if (pgm_info.code & PGM_PER) {
755 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100756 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200757 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100758 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200759 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100760 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200761 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100762 (u8 *) __LC_PER_ACCESS_ID);
763 }
764
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100765 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100766 kvm_s390_rewind_psw(vcpu, ilen);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100767
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100768 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
769 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100770 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
771 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200772 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100773 (u16 *)__LC_PGM_INT_CODE);
774 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
775 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
776 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
777 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100778 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200779}
David Hildenbrand87128362014-03-03 10:55:13 +0100780
Jens Freimann6d3da242013-07-03 15:18:35 +0200781static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200782{
Jens Freimann6d3da242013-07-03 15:18:35 +0200783 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
784 struct kvm_s390_ext_info ext;
785 int rc = 0;
786
787 spin_lock(&fi->lock);
788 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
789 spin_unlock(&fi->lock);
790 return 0;
791 }
792 ext = fi->srv_signal;
793 memset(&fi->srv_signal, 0, sizeof(ext));
794 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
795 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200796
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200797 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200798 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200799 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200800 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
801 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200802
803 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100804 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200805 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
806 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
807 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
808 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200809 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200810 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200811
Jens Freimann99e20002014-12-01 17:05:39 +0100812 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200813}
814
Jens Freimann6d3da242013-07-03 15:18:35 +0200815static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200816{
Jens Freimann6d3da242013-07-03 15:18:35 +0200817 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
818 struct kvm_s390_interrupt_info *inti;
819 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200820
Jens Freimann6d3da242013-07-03 15:18:35 +0200821 spin_lock(&fi->lock);
822 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
823 struct kvm_s390_interrupt_info,
824 list);
825 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200826 list_del(&inti->list);
827 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
828 }
829 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
830 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
831 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200832
Jens Freimann6d3da242013-07-03 15:18:35 +0200833 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200834 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
835 KVM_S390_INT_PFAULT_DONE, 0,
836 inti->ext.ext_params2);
837 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
838 inti->ext.ext_params2);
839
Jens Freimann6d3da242013-07-03 15:18:35 +0200840 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
841 (u16 *)__LC_EXT_INT_CODE);
842 rc |= put_guest_lc(vcpu, PFAULT_DONE,
843 (u16 *)__LC_EXT_CPU_ADDR);
844 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
845 &vcpu->arch.sie_block->gpsw,
846 sizeof(psw_t));
847 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
848 &vcpu->arch.sie_block->gpsw,
849 sizeof(psw_t));
850 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
851 (u64 *)__LC_EXT_PARAMS2);
852 kfree(inti);
853 }
Jens Freimann99e20002014-12-01 17:05:39 +0100854 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200855}
856
Jens Freimann6d3da242013-07-03 15:18:35 +0200857static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200858{
Jens Freimann6d3da242013-07-03 15:18:35 +0200859 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
860 struct kvm_s390_interrupt_info *inti;
861 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200862
Jens Freimann6d3da242013-07-03 15:18:35 +0200863 spin_lock(&fi->lock);
864 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
865 struct kvm_s390_interrupt_info,
866 list);
867 if (inti) {
868 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200869 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200870 inti->ext.ext_params, inti->ext.ext_params2);
871 vcpu->stat.deliver_virtio_interrupt++;
872 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
873 inti->type,
874 inti->ext.ext_params,
875 inti->ext.ext_params2);
876 list_del(&inti->list);
877 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
878 }
879 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
880 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
881 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200882
Jens Freimann6d3da242013-07-03 15:18:35 +0200883 if (inti) {
884 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
885 (u16 *)__LC_EXT_INT_CODE);
886 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
887 (u16 *)__LC_EXT_CPU_ADDR);
888 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
889 &vcpu->arch.sie_block->gpsw,
890 sizeof(psw_t));
891 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
892 &vcpu->arch.sie_block->gpsw,
893 sizeof(psw_t));
894 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
895 (u32 *)__LC_EXT_PARAMS);
896 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
897 (u64 *)__LC_EXT_PARAMS2);
898 kfree(inti);
899 }
Jens Freimann99e20002014-12-01 17:05:39 +0100900 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200901}
902
903static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200904 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200905{
Jens Freimann6d3da242013-07-03 15:18:35 +0200906 struct list_head *isc_list;
907 struct kvm_s390_float_interrupt *fi;
908 struct kvm_s390_interrupt_info *inti = NULL;
909 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200910
Jens Freimann6d3da242013-07-03 15:18:35 +0200911 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200912
Jens Freimann6d3da242013-07-03 15:18:35 +0200913 spin_lock(&fi->lock);
Michael Muelleree739f42017-07-03 15:32:50 +0200914 isc_list = &fi->lists[irq_type_to_isc(irq_type)];
Jens Freimann6d3da242013-07-03 15:18:35 +0200915 inti = list_first_entry_or_null(isc_list,
916 struct kvm_s390_interrupt_info,
917 list);
918 if (inti) {
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +0200919 if (inti->type & KVM_S390_INT_IO_AI_MASK)
920 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
921 else
922 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
923 inti->io.subchannel_id >> 8,
924 inti->io.subchannel_id >> 1 & 0x3,
925 inti->io.subchannel_nr);
926
Jens Freimann6d3da242013-07-03 15:18:35 +0200927 vcpu->stat.deliver_io_int++;
928 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
929 inti->type,
930 ((__u32)inti->io.subchannel_id << 16) |
931 inti->io.subchannel_nr,
932 ((__u64)inti->io.io_int_parm << 32) |
933 inti->io.io_int_word);
934 list_del(&inti->list);
935 fi->counters[FIRQ_CNTR_IO] -= 1;
936 }
937 if (list_empty(isc_list))
938 clear_bit(irq_type, &fi->pending_irqs);
939 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100940
Jens Freimann6d3da242013-07-03 15:18:35 +0200941 if (inti) {
942 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
943 (u16 *)__LC_SUBCHANNEL_ID);
944 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
945 (u16 *)__LC_SUBCHANNEL_NR);
946 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
947 (u32 *)__LC_IO_INT_PARM);
948 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
949 (u32 *)__LC_IO_INT_WORD);
950 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
951 &vcpu->arch.sie_block->gpsw,
952 sizeof(psw_t));
953 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
954 &vcpu->arch.sie_block->gpsw,
955 sizeof(psw_t));
956 kfree(inti);
957 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200958
Jens Freimann99e20002014-12-01 17:05:39 +0100959 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200960}
961
962typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
963
964static const deliver_irq_t deliver_irq_funcs[] = {
965 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200966 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200967 [IRQ_PEND_PROG] = __deliver_prog,
968 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
969 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
970 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
971 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
972 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200973 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
974 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200975 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
976 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
977 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200978};
979
David Hildenbrandea5f4962014-10-14 15:29:30 +0200980/* Check whether an external call is pending (deliverable or not) */
981int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100982{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200983 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +0100984
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200985 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200986 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100987
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200988 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100989}
990
David Hildenbrand9a022062014-08-05 17:40:47 +0200991int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100992{
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200993 if (deliverable_irqs(vcpu))
994 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100995
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200996 if (kvm_cpu_has_pending_timer(vcpu))
997 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100998
David Hildenbrandea5f4962014-10-14 15:29:30 +0200999 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001000 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +02001001 !psw_extint_disabled(vcpu) &&
1002 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001003 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +01001004
David Hildenbrand4d32ad62015-05-06 13:51:29 +02001005 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
1006 return 1;
1007 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001008}
1009
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001010int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1011{
David Hildenbrandb4aec922014-12-01 15:55:42 +01001012 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001013}
1014
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001015static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
1016{
1017 u64 now, cputm, sltime = 0;
1018
1019 if (ckc_interrupts_enabled(vcpu)) {
1020 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1021 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
1022 /* already expired or overflow? */
1023 if (!sltime || vcpu->arch.sie_block->ckc <= now)
1024 return 0;
1025 if (cpu_timer_interrupts_enabled(vcpu)) {
1026 cputm = kvm_s390_get_cpu_timer(vcpu);
1027 /* already expired? */
1028 if (cputm >> 63)
1029 return 0;
1030 return min(sltime, tod_to_ns(cputm));
1031 }
1032 } else if (cpu_timer_interrupts_enabled(vcpu)) {
1033 sltime = kvm_s390_get_cpu_timer(vcpu);
1034 /* already expired? */
1035 if (sltime >> 63)
1036 return 0;
1037 }
1038 return sltime;
1039}
1040
Carsten Otteba5c1e92008-03-25 18:47:26 +01001041int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
1042{
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001043 u64 sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001044
1045 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001046
David Hildenbrand0759d062014-05-13 16:54:32 +02001047 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +02001048 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +02001049 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +02001050
Carsten Otteba5c1e92008-03-25 18:47:26 +01001051 if (psw_interrupts_disabled(vcpu)) {
1052 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001053 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +01001054 }
1055
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001056 if (!ckc_interrupts_enabled(vcpu) &&
1057 !cpu_timer_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001058 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +01001059 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001060 goto no_timer;
1061 }
1062
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001063 sltime = __calculate_sltime(vcpu);
1064 if (!sltime)
David Hildenbrandbda343e2014-12-12 12:26:40 +01001065 return 0;
1066
1067 __set_cpu_idle(vcpu);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001068 hrtimer_start(&vcpu->arch.ckc_timer, sltime, HRTIMER_MODE_REL);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001069 VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001070no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +02001071 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +02001072 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001073 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +02001074 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1075
David Hildenbrand2d00f752014-12-11 10:18:01 +01001076 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001077 return 0;
1078}
1079
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001080void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1081{
Christian Borntraeger3491caf2016-05-13 12:16:35 +02001082 /*
1083 * We cannot move this into the if, as the CPU might be already
1084 * in kvm_vcpu_block without having the waitqueue set (polling)
1085 */
1086 vcpu->valid_wakeup = true;
Christian Borntraeger72e1ad42017-09-19 12:34:06 +02001087 /*
1088 * This is mostly to document, that the read in swait_active could
1089 * be moved before other stores, leading to subtle races.
1090 * All current users do not store or use an atomic like update
1091 */
1092 smp_mb__after_atomic();
Marcelo Tosatti85773702016-02-19 09:46:39 +01001093 if (swait_active(&vcpu->wq)) {
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001094 /*
1095 * The vcpu gave up the cpu voluntarily, mark it as a good
1096 * yield-candidate.
1097 */
1098 vcpu->preempted = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001099 swake_up(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +02001100 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001101 }
David Hildenbrandadbf1692016-05-27 22:03:52 +02001102 /*
1103 * The VCPU might not be sleeping but is executing the VSIE. Let's
1104 * kick it, so it leaves the SIE to process the request.
1105 */
1106 kvm_s390_vsie_kick(vcpu);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001107}
1108
Christian Borntraegerca872302009-05-12 17:21:49 +02001109enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1110{
1111 struct kvm_vcpu *vcpu;
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001112 u64 sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +02001113
1114 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001115 sltime = __calculate_sltime(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001116
David Hildenbrand2d00f752014-12-11 10:18:01 +01001117 /*
1118 * If the monotonic clock runs faster than the tod clock we might be
1119 * woken up too early and have to go back to sleep to avoid deadlocks.
1120 */
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001121 if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
David Hildenbrand2d00f752014-12-11 10:18:01 +01001122 return HRTIMER_RESTART;
1123 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001124 return HRTIMER_NORESTART;
1125}
Carsten Otteba5c1e92008-03-25 18:47:26 +01001126
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001127void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1128{
1129 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001130
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001131 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001132 li->pending_irqs = 0;
1133 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1134 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001135 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +01001136
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001137 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001138}
1139
Christian Borntraeger614aeab2014-08-25 12:27:29 +02001140int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001141{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001142 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001143 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +02001144 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +02001145 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +02001146 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001147
1148 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001149
Jens Freimann383d0b02014-07-29 15:11:49 +02001150 /* pending ckc conditions might have been invalidated */
1151 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +01001152 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +02001153 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1154
David Hildenbrandb4aec922014-12-01 15:55:42 +01001155 /* pending cpu timer conditions might have been invalidated */
1156 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1157 if (cpu_timer_irq_pending(vcpu))
1158 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1159
Jens Freimannffeca0a2015-04-17 10:21:04 +02001160 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001161 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +02001162 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +02001163 if (is_ioirq(irq_type)) {
1164 rc = __deliver_io(vcpu, irq_type);
1165 } else {
1166 func = deliver_irq_funcs[irq_type];
1167 if (!func) {
1168 WARN_ON_ONCE(func == NULL);
1169 clear_bit(irq_type, &li->pending_irqs);
1170 continue;
1171 }
1172 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +02001173 }
Jens Freimannffeca0a2015-04-17 10:21:04 +02001174 }
Jens Freimann383d0b02014-07-29 15:11:49 +02001175
Jens Freimann6d3da242013-07-03 15:18:35 +02001176 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +02001177
1178 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001179}
1180
Jens Freimann383d0b02014-07-29 15:11:49 +02001181static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001182{
1183 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1184
David Hildenbranded2afcf2015-07-20 10:33:03 +02001185 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1186 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1187 irq->u.pgm.code, 0);
1188
David Hildenbrand634790b2015-11-04 16:33:33 +01001189 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1190 /* auto detection if no valid ILC was given */
1191 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1192 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1193 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1194 }
1195
David Hildenbrand238293b2015-05-04 12:38:48 +02001196 if (irq->u.pgm.code == PGM_PER) {
1197 li->irq.pgm.code |= PGM_PER;
David Hildenbrand634790b2015-11-04 16:33:33 +01001198 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001199 /* only modify PER related information */
1200 li->irq.pgm.per_address = irq->u.pgm.per_address;
1201 li->irq.pgm.per_code = irq->u.pgm.per_code;
1202 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1203 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1204 } else if (!(irq->u.pgm.code & PGM_PER)) {
1205 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1206 irq->u.pgm.code;
David Hildenbrand634790b2015-11-04 16:33:33 +01001207 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001208 /* only modify non-PER information */
1209 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1210 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1211 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1212 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1213 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1214 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1215 } else {
1216 li->irq.pgm = irq->u.pgm;
1217 }
Jens Freimann91851242014-12-01 16:43:40 +01001218 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001219 return 0;
1220}
1221
Jens Freimann383d0b02014-07-29 15:11:49 +02001222static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001223{
1224 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1225
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001226 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1227 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001228 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1229 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001230 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001231
1232 li->irq.ext = irq->u.ext;
1233 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001234 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001235 return 0;
1236}
1237
Christian Borntraeger0675d922015-01-15 12:40:42 +01001238static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001239{
1240 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001241 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001242 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001243
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001244 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001245 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001246 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001247 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001248
David Hildenbrandea5f4962014-10-14 15:29:30 +02001249 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001250 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001251 return -EINVAL;
1252
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001253 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001254 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001255
David Hildenbrandb938eace2015-04-30 13:33:59 +02001256 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001257 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001258 *extcall = irq->u.extcall;
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001259 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001260 return 0;
1261}
1262
Jens Freimann383d0b02014-07-29 15:11:49 +02001263static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001264{
1265 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001266 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001267
David Hildenbranded2afcf2015-07-20 10:33:03 +02001268 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001269 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001270 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001271 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001272
David Hildenbranda3a9c592014-10-14 09:44:55 +02001273 if (!is_vcpu_stopped(vcpu))
1274 return -EBUSY;
1275
Jens Freimann383d0b02014-07-29 15:11:49 +02001276 *prefix = irq->u.prefix;
1277 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001278 return 0;
1279}
1280
David Hildenbrand6cddd432014-10-15 16:48:53 +02001281#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001282static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001283{
1284 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001285 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001286 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001287
David Hildenbranded2afcf2015-07-20 10:33:03 +02001288 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001289
David Hildenbrand28225452014-10-15 16:48:16 +02001290 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1291 return -EINVAL;
1292
David Hildenbrand6cddd432014-10-15 16:48:53 +02001293 if (is_vcpu_stopped(vcpu)) {
1294 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1295 rc = kvm_s390_store_status_unloaded(vcpu,
1296 KVM_S390_STORE_STATUS_NOADDR);
1297 return rc;
1298 }
1299
1300 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1301 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001302 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001303 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001304 return 0;
1305}
1306
1307static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001308 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001309{
1310 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1311
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001312 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001313 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001314
1315 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001316 return 0;
1317}
1318
1319static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001320 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001321{
1322 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1323
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001324 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001325 irq->u.emerg.code);
1326 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001327 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001328
David Hildenbrandb85de332015-11-05 09:38:15 +01001329 /* sending vcpu invalid */
1330 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1331 return -EINVAL;
1332
Jens Freimann49538d12014-12-18 15:48:14 +01001333 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001334 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001335 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001336 return 0;
1337}
1338
Jens Freimann383d0b02014-07-29 15:11:49 +02001339static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001340{
1341 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001342 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001343
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001344 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001345 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001346 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001347 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001348
1349 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001350 * Because repressible machine checks can be indicated along with
1351 * exigent machine checks (PoP, Chapter 11, Interruption action)
1352 * we need to combine cr14, mcic and external damage code.
1353 * Failing storage address and the logout area should not be or'ed
1354 * together, we just indicate the last occurrence of the corresponding
1355 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001356 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001357 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001358 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001359 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1360 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1361 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1362 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001363 if (mchk->mcic & MCHK_EX_MASK)
1364 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1365 else if (mchk->mcic & MCHK_REP_MASK)
1366 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001367 return 0;
1368}
1369
Jens Freimann383d0b02014-07-29 15:11:49 +02001370static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001371{
1372 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1373
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001374 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001375 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001376 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001377
1378 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001379 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001380 return 0;
1381}
1382
Jens Freimann383d0b02014-07-29 15:11:49 +02001383static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001384{
1385 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1386
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001387 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001388 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001389 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001390
1391 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001392 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001393 return 0;
1394}
1395
Jens Freimann6d3da242013-07-03 15:18:35 +02001396static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1397 int isc, u32 schid)
1398{
1399 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1400 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1401 struct kvm_s390_interrupt_info *iter;
1402 u16 id = (schid & 0xffff0000U) >> 16;
1403 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001404
Jens Freimann6d3da242013-07-03 15:18:35 +02001405 spin_lock(&fi->lock);
1406 list_for_each_entry(iter, isc_list, list) {
1407 if (schid && (id != iter->io.subchannel_id ||
1408 nr != iter->io.subchannel_nr))
1409 continue;
1410 /* found an appropriate entry */
1411 list_del_init(&iter->list);
1412 fi->counters[FIRQ_CNTR_IO] -= 1;
1413 if (list_empty(isc_list))
Michael Muelleree739f42017-07-03 15:32:50 +02001414 clear_bit(isc_to_irq_type(isc), &fi->pending_irqs);
Jens Freimann6d3da242013-07-03 15:18:35 +02001415 spin_unlock(&fi->lock);
1416 return iter;
1417 }
1418 spin_unlock(&fi->lock);
1419 return NULL;
1420}
1421
1422/*
1423 * Dequeue and return an I/O interrupt matching any of the interruption
1424 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1425 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001426struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001427 u64 isc_mask, u32 schid)
1428{
1429 struct kvm_s390_interrupt_info *inti = NULL;
1430 int isc;
1431
1432 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1433 if (isc_mask & isc_to_isc_bits(isc))
1434 inti = get_io_int(kvm, isc, schid);
1435 }
1436 return inti;
1437}
1438
1439#define SCCB_MASK 0xFFFFFFF8
1440#define SCCB_EVENT_PENDING 0x3
1441
1442static int __inject_service(struct kvm *kvm,
1443 struct kvm_s390_interrupt_info *inti)
1444{
1445 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1446
1447 spin_lock(&fi->lock);
1448 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1449 /*
1450 * Early versions of the QEMU s390 bios will inject several
1451 * service interrupts after another without handling a
1452 * condition code indicating busy.
1453 * We will silently ignore those superfluous sccb values.
1454 * A future version of QEMU will take care of serialization
1455 * of servc requests
1456 */
1457 if (fi->srv_signal.ext_params & SCCB_MASK)
1458 goto out;
1459 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1460 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1461out:
1462 spin_unlock(&fi->lock);
1463 kfree(inti);
1464 return 0;
1465}
1466
1467static int __inject_virtio(struct kvm *kvm,
1468 struct kvm_s390_interrupt_info *inti)
1469{
1470 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1471
1472 spin_lock(&fi->lock);
1473 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1474 spin_unlock(&fi->lock);
1475 return -EBUSY;
1476 }
1477 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1478 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1479 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1480 spin_unlock(&fi->lock);
1481 return 0;
1482}
1483
1484static int __inject_pfault_done(struct kvm *kvm,
1485 struct kvm_s390_interrupt_info *inti)
1486{
1487 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1488
1489 spin_lock(&fi->lock);
1490 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1491 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1492 spin_unlock(&fi->lock);
1493 return -EBUSY;
1494 }
1495 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1496 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1497 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1498 spin_unlock(&fi->lock);
1499 return 0;
1500}
1501
1502#define CR_PENDING_SUBCLASS 28
1503static int __inject_float_mchk(struct kvm *kvm,
1504 struct kvm_s390_interrupt_info *inti)
1505{
1506 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1507
1508 spin_lock(&fi->lock);
1509 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1510 fi->mchk.mcic |= inti->mchk.mcic;
1511 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1512 spin_unlock(&fi->lock);
1513 kfree(inti);
1514 return 0;
1515}
1516
1517static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001518{
1519 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001520 struct list_head *list;
1521 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001522
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001523 fi = &kvm->arch.float_int;
1524 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001525 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1526 spin_unlock(&fi->lock);
1527 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001528 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001529 fi->counters[FIRQ_CNTR_IO] += 1;
1530
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +02001531 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1532 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1533 else
1534 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1535 inti->io.subchannel_id >> 8,
1536 inti->io.subchannel_id >> 1 & 0x3,
1537 inti->io.subchannel_nr);
Jens Freimann6d3da242013-07-03 15:18:35 +02001538 isc = int_word_to_isc(inti->io.io_int_word);
1539 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1540 list_add_tail(&inti->list, list);
Michael Muelleree739f42017-07-03 15:32:50 +02001541 set_bit(isc_to_irq_type(isc), &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001542 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001543 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001544}
1545
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001546/*
1547 * Find a destination VCPU for a floating irq and kick it.
1548 */
1549static void __floating_irq_kick(struct kvm *kvm, u64 type)
1550{
1551 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1552 struct kvm_s390_local_interrupt *li;
1553 struct kvm_vcpu *dst_vcpu;
1554 int sigcpu, online_vcpus, nr_tries = 0;
1555
1556 online_vcpus = atomic_read(&kvm->online_vcpus);
1557 if (!online_vcpus)
1558 return;
1559
1560 /* find idle VCPUs first, then round robin */
1561 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1562 if (sigcpu == online_vcpus) {
1563 do {
1564 sigcpu = fi->next_rr_cpu;
1565 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1566 /* avoid endless loops if all vcpus are stopped */
1567 if (nr_tries++ >= online_vcpus)
1568 return;
1569 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1570 }
1571 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1572
1573 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1574 li = &dst_vcpu->arch.local_int;
1575 spin_lock(&li->lock);
1576 switch (type) {
1577 case KVM_S390_MCHK:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001578 atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001579 break;
1580 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001581 atomic_or(CPUSTAT_IO_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001582 break;
1583 default:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001584 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001585 break;
1586 }
1587 spin_unlock(&li->lock);
1588 kvm_s390_vcpu_wakeup(dst_vcpu);
1589}
1590
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001591static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001592{
Jens Freimann6d3da242013-07-03 15:18:35 +02001593 u64 type = READ_ONCE(inti->type);
1594 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001595
Jens Freimann6d3da242013-07-03 15:18:35 +02001596 switch (type) {
1597 case KVM_S390_MCHK:
1598 rc = __inject_float_mchk(kvm, inti);
1599 break;
1600 case KVM_S390_INT_VIRTIO:
1601 rc = __inject_virtio(kvm, inti);
1602 break;
1603 case KVM_S390_INT_SERVICE:
1604 rc = __inject_service(kvm, inti);
1605 break;
1606 case KVM_S390_INT_PFAULT_DONE:
1607 rc = __inject_pfault_done(kvm, inti);
1608 break;
1609 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1610 rc = __inject_io(kvm, inti);
1611 break;
1612 default:
1613 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001614 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001615 if (rc)
1616 return rc;
1617
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001618 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001619 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001620}
1621
1622int kvm_s390_inject_vm(struct kvm *kvm,
1623 struct kvm_s390_interrupt *s390int)
1624{
1625 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001626 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001627
1628 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1629 if (!inti)
1630 return -ENOMEM;
1631
1632 inti->type = s390int->type;
1633 switch (inti->type) {
1634 case KVM_S390_INT_VIRTIO:
1635 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1636 s390int->parm, s390int->parm64);
1637 inti->ext.ext_params = s390int->parm;
1638 inti->ext.ext_params2 = s390int->parm64;
1639 break;
1640 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001641 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001642 inti->ext.ext_params = s390int->parm;
1643 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001644 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001645 inti->ext.ext_params2 = s390int->parm64;
1646 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001647 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001648 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001649 s390int->parm64);
1650 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1651 inti->mchk.mcic = s390int->parm64;
1652 break;
1653 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimannc05c4182013-10-07 16:13:45 +02001654 inti->io.subchannel_id = s390int->parm >> 16;
1655 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1656 inti->io.io_int_parm = s390int->parm64 >> 32;
1657 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1658 break;
1659 default:
1660 kfree(inti);
1661 return -EINVAL;
1662 }
1663 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1664 2);
1665
David Hildenbrand428d53b2015-01-16 12:58:09 +01001666 rc = __inject_vm(kvm, inti);
1667 if (rc)
1668 kfree(inti);
1669 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001670}
1671
David Hildenbrand15462e32015-02-04 15:59:11 +01001672int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001673 struct kvm_s390_interrupt_info *inti)
1674{
David Hildenbrand15462e32015-02-04 15:59:11 +01001675 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001676}
1677
Jens Freimann383d0b02014-07-29 15:11:49 +02001678int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1679 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001680{
Jens Freimann383d0b02014-07-29 15:11:49 +02001681 irq->type = s390int->type;
1682 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001683 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001684 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001685 return -EINVAL;
1686 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001687 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001688 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001689 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001690 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001691 case KVM_S390_SIGP_STOP:
1692 irq->u.stop.flags = s390int->parm;
1693 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001694 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001695 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001696 return -EINVAL;
1697 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001698 break;
1699 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001700 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001701 return -EINVAL;
1702 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001703 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001704 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001705 irq->u.mchk.mcic = s390int->parm64;
1706 break;
1707 }
1708 return 0;
1709}
1710
David Hildenbrand6cddd432014-10-15 16:48:53 +02001711int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1712{
1713 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1714
1715 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1716}
1717
1718void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1719{
1720 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1721
1722 spin_lock(&li->lock);
1723 li->irq.stop.flags = 0;
1724 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1725 spin_unlock(&li->lock);
1726}
1727
Jens Freimann79e87a12015-03-19 15:12:12 +01001728static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001729{
Jens Freimann383d0b02014-07-29 15:11:49 +02001730 int rc;
1731
Jens Freimann383d0b02014-07-29 15:11:49 +02001732 switch (irq->type) {
1733 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001734 rc = __inject_prog(vcpu, irq);
1735 break;
1736 case KVM_S390_SIGP_SET_PREFIX:
1737 rc = __inject_set_prefix(vcpu, irq);
1738 break;
1739 case KVM_S390_SIGP_STOP:
1740 rc = __inject_sigp_stop(vcpu, irq);
1741 break;
1742 case KVM_S390_RESTART:
1743 rc = __inject_sigp_restart(vcpu, irq);
1744 break;
1745 case KVM_S390_INT_CLOCK_COMP:
1746 rc = __inject_ckc(vcpu);
1747 break;
1748 case KVM_S390_INT_CPU_TIMER:
1749 rc = __inject_cpu_timer(vcpu);
1750 break;
1751 case KVM_S390_INT_EXTERNAL_CALL:
1752 rc = __inject_extcall(vcpu, irq);
1753 break;
1754 case KVM_S390_INT_EMERGENCY:
1755 rc = __inject_sigp_emergency(vcpu, irq);
1756 break;
1757 case KVM_S390_MCHK:
1758 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001759 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001760 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001761 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001762 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001763 case KVM_S390_INT_VIRTIO:
1764 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001765 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001766 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001767 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001768 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001769
1770 return rc;
1771}
1772
1773int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1774{
1775 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1776 int rc;
1777
1778 spin_lock(&li->lock);
1779 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001780 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001781 if (!rc)
1782 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001783 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001784}
Jens Freimannc05c4182013-10-07 16:13:45 +02001785
Jens Freimann6d3da242013-07-03 15:18:35 +02001786static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001787{
Jens Freimann6d3da242013-07-03 15:18:35 +02001788 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001789
Jens Freimann6d3da242013-07-03 15:18:35 +02001790 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001791 list_del(&inti->list);
1792 kfree(inti);
1793 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001794}
1795
Jens Freimann94aa0332015-03-16 12:17:13 +01001796static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1797 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001798{
Jens Freimann94aa0332015-03-16 12:17:13 +01001799 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001800 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001801 case KVM_S390_INT_PFAULT_INIT:
1802 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001803 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001804 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001805 break;
1806 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001807 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001808 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001809 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001810}
1811
Jens Freimann6d3da242013-07-03 15:18:35 +02001812void kvm_s390_clear_float_irqs(struct kvm *kvm)
1813{
1814 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1815 int i;
1816
1817 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001818 fi->pending_irqs = 0;
1819 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1820 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001821 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1822 clear_irq_list(&fi->lists[i]);
1823 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1824 fi->counters[i] = 0;
1825 spin_unlock(&fi->lock);
1826};
1827
Jens Freimann94aa0332015-03-16 12:17:13 +01001828static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001829{
1830 struct kvm_s390_interrupt_info *inti;
1831 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001832 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001833 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001834 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001835 int ret = 0;
1836 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001837 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001838
Jens Freimann94aa0332015-03-16 12:17:13 +01001839 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1840 return -EINVAL;
1841
1842 /*
1843 * We are already using -ENOMEM to signal
1844 * userspace it may retry with a bigger buffer,
1845 * so we need to use something else for this case
1846 */
1847 buf = vzalloc(len);
1848 if (!buf)
1849 return -ENOBUFS;
1850
1851 max_irqs = len / sizeof(struct kvm_s390_irq);
1852
Jens Freimannc05c4182013-10-07 16:13:45 +02001853 fi = &kvm->arch.float_int;
1854 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001855 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1856 list_for_each_entry(inti, &fi->lists[i], list) {
1857 if (n == max_irqs) {
1858 /* signal userspace to try again */
1859 ret = -ENOMEM;
1860 goto out;
1861 }
1862 inti_to_irq(inti, &buf[n]);
1863 n++;
1864 }
1865 }
1866 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001867 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001868 /* signal userspace to try again */
1869 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001870 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001871 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001872 irq = (struct kvm_s390_irq *) &buf[n];
1873 irq->type = KVM_S390_INT_SERVICE;
1874 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001875 n++;
1876 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001877 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1878 if (n == max_irqs) {
1879 /* signal userspace to try again */
1880 ret = -ENOMEM;
1881 goto out;
1882 }
1883 irq = (struct kvm_s390_irq *) &buf[n];
1884 irq->type = KVM_S390_MCHK;
1885 irq->u.mchk = fi->mchk;
1886 n++;
1887}
1888
1889out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001890 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001891 if (!ret && n > 0) {
1892 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1893 ret = -EFAULT;
1894 }
1895 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001896
1897 return ret < 0 ? ret : n;
1898}
1899
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08001900static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr)
1901{
1902 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1903 struct kvm_s390_ais_all ais;
1904
1905 if (attr->attr < sizeof(ais))
1906 return -EINVAL;
1907
1908 if (!test_kvm_facility(kvm, 72))
1909 return -ENOTSUPP;
1910
1911 mutex_lock(&fi->ais_lock);
1912 ais.simm = fi->simm;
1913 ais.nimm = fi->nimm;
1914 mutex_unlock(&fi->ais_lock);
1915
1916 if (copy_to_user((void __user *)attr->addr, &ais, sizeof(ais)))
1917 return -EFAULT;
1918
1919 return 0;
1920}
1921
Jens Freimannc05c4182013-10-07 16:13:45 +02001922static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1923{
1924 int r;
1925
1926 switch (attr->group) {
1927 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001928 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001929 attr->attr);
1930 break;
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08001931 case KVM_DEV_FLIC_AISM_ALL:
1932 r = flic_ais_mode_get_all(dev->kvm, attr);
1933 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001934 default:
1935 r = -EINVAL;
1936 }
1937
1938 return r;
1939}
1940
1941static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1942 u64 addr)
1943{
1944 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1945 void *target = NULL;
1946 void __user *source;
1947 u64 size;
1948
1949 if (get_user(inti->type, (u64 __user *)addr))
1950 return -EFAULT;
1951
1952 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001953 case KVM_S390_INT_PFAULT_INIT:
1954 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001955 case KVM_S390_INT_VIRTIO:
1956 case KVM_S390_INT_SERVICE:
1957 target = (void *) &inti->ext;
1958 source = &uptr->u.ext;
1959 size = sizeof(inti->ext);
1960 break;
1961 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1962 target = (void *) &inti->io;
1963 source = &uptr->u.io;
1964 size = sizeof(inti->io);
1965 break;
1966 case KVM_S390_MCHK:
1967 target = (void *) &inti->mchk;
1968 source = &uptr->u.mchk;
1969 size = sizeof(inti->mchk);
1970 break;
1971 default:
1972 return -EINVAL;
1973 }
1974
1975 if (copy_from_user(target, source, size))
1976 return -EFAULT;
1977
1978 return 0;
1979}
1980
1981static int enqueue_floating_irq(struct kvm_device *dev,
1982 struct kvm_device_attr *attr)
1983{
1984 struct kvm_s390_interrupt_info *inti = NULL;
1985 int r = 0;
1986 int len = attr->attr;
1987
1988 if (len % sizeof(struct kvm_s390_irq) != 0)
1989 return -EINVAL;
1990 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1991 return -EINVAL;
1992
1993 while (len >= sizeof(struct kvm_s390_irq)) {
1994 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1995 if (!inti)
1996 return -ENOMEM;
1997
1998 r = copy_irq_from_user(inti, attr->addr);
1999 if (r) {
2000 kfree(inti);
2001 return r;
2002 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01002003 r = __inject_vm(dev->kvm, inti);
2004 if (r) {
2005 kfree(inti);
2006 return r;
2007 }
Jens Freimannc05c4182013-10-07 16:13:45 +02002008 len -= sizeof(struct kvm_s390_irq);
2009 attr->addr += sizeof(struct kvm_s390_irq);
2010 }
2011
2012 return r;
2013}
2014
Cornelia Huck841b91c2013-07-15 13:36:01 +02002015static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
2016{
2017 if (id >= MAX_S390_IO_ADAPTERS)
2018 return NULL;
2019 return kvm->arch.adapters[id];
2020}
2021
2022static int register_io_adapter(struct kvm_device *dev,
2023 struct kvm_device_attr *attr)
2024{
2025 struct s390_io_adapter *adapter;
2026 struct kvm_s390_io_adapter adapter_info;
2027
2028 if (copy_from_user(&adapter_info,
2029 (void __user *)attr->addr, sizeof(adapter_info)))
2030 return -EFAULT;
2031
2032 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
2033 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
2034 return -EINVAL;
2035
2036 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
2037 if (!adapter)
2038 return -ENOMEM;
2039
2040 INIT_LIST_HEAD(&adapter->maps);
2041 init_rwsem(&adapter->maps_lock);
2042 atomic_set(&adapter->nr_maps, 0);
2043 adapter->id = adapter_info.id;
2044 adapter->isc = adapter_info.isc;
2045 adapter->maskable = adapter_info.maskable;
2046 adapter->masked = false;
2047 adapter->swap = adapter_info.swap;
Fei Li08fab502017-01-19 17:02:26 +01002048 adapter->suppressible = (adapter_info.flags) &
2049 KVM_S390_ADAPTER_SUPPRESSIBLE;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002050 dev->kvm->arch.adapters[adapter->id] = adapter;
2051
2052 return 0;
2053}
2054
2055int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
2056{
2057 int ret;
2058 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2059
2060 if (!adapter || !adapter->maskable)
2061 return -EINVAL;
2062 ret = adapter->masked;
2063 adapter->masked = masked;
2064 return ret;
2065}
2066
2067static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
2068{
2069 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2070 struct s390_map_info *map;
2071 int ret;
2072
2073 if (!adapter || !addr)
2074 return -EINVAL;
2075
2076 map = kzalloc(sizeof(*map), GFP_KERNEL);
2077 if (!map) {
2078 ret = -ENOMEM;
2079 goto out;
2080 }
2081 INIT_LIST_HEAD(&map->list);
2082 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02002083 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02002084 if (map->addr == -EFAULT) {
2085 ret = -EFAULT;
2086 goto out;
2087 }
2088 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
2089 if (ret < 0)
2090 goto out;
2091 BUG_ON(ret != 1);
2092 down_write(&adapter->maps_lock);
2093 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2094 list_add_tail(&map->list, &adapter->maps);
2095 ret = 0;
2096 } else {
2097 put_page(map->page);
2098 ret = -EINVAL;
2099 }
2100 up_write(&adapter->maps_lock);
2101out:
2102 if (ret)
2103 kfree(map);
2104 return ret;
2105}
2106
2107static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2108{
2109 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2110 struct s390_map_info *map, *tmp;
2111 int found = 0;
2112
2113 if (!adapter || !addr)
2114 return -EINVAL;
2115
2116 down_write(&adapter->maps_lock);
2117 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2118 if (map->guest_addr == addr) {
2119 found = 1;
2120 atomic_dec(&adapter->nr_maps);
2121 list_del(&map->list);
2122 put_page(map->page);
2123 kfree(map);
2124 break;
2125 }
2126 }
2127 up_write(&adapter->maps_lock);
2128
2129 return found ? 0 : -EINVAL;
2130}
2131
2132void kvm_s390_destroy_adapters(struct kvm *kvm)
2133{
2134 int i;
2135 struct s390_map_info *map, *tmp;
2136
2137 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2138 if (!kvm->arch.adapters[i])
2139 continue;
2140 list_for_each_entry_safe(map, tmp,
2141 &kvm->arch.adapters[i]->maps, list) {
2142 list_del(&map->list);
2143 put_page(map->page);
2144 kfree(map);
2145 }
2146 kfree(kvm->arch.adapters[i]);
2147 }
2148}
2149
2150static int modify_io_adapter(struct kvm_device *dev,
2151 struct kvm_device_attr *attr)
2152{
2153 struct kvm_s390_io_adapter_req req;
2154 struct s390_io_adapter *adapter;
2155 int ret;
2156
2157 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2158 return -EFAULT;
2159
2160 adapter = get_io_adapter(dev->kvm, req.id);
2161 if (!adapter)
2162 return -EINVAL;
2163 switch (req.type) {
2164 case KVM_S390_IO_ADAPTER_MASK:
2165 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2166 if (ret > 0)
2167 ret = 0;
2168 break;
2169 case KVM_S390_IO_ADAPTER_MAP:
2170 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2171 break;
2172 case KVM_S390_IO_ADAPTER_UNMAP:
2173 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2174 break;
2175 default:
2176 ret = -EINVAL;
2177 }
2178
2179 return ret;
2180}
2181
Halil Pasic6d28f782016-01-25 19:10:40 +01002182static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2183
2184{
2185 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2186 u32 schid;
2187
2188 if (attr->flags)
2189 return -EINVAL;
2190 if (attr->attr != sizeof(schid))
2191 return -EINVAL;
2192 if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2193 return -EFAULT;
2194 kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2195 /*
2196 * If userspace is conforming to the architecture, we can have at most
2197 * one pending I/O interrupt per subchannel, so this is effectively a
2198 * clear all.
2199 */
2200 return 0;
2201}
2202
Fei Li51978392017-02-17 17:06:26 +08002203static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
2204{
2205 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2206 struct kvm_s390_ais_req req;
2207 int ret = 0;
2208
Christian Borntraeger1ba15b22017-05-31 10:18:55 +02002209 if (!test_kvm_facility(kvm, 72))
Fei Li51978392017-02-17 17:06:26 +08002210 return -ENOTSUPP;
2211
2212 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2213 return -EFAULT;
2214
2215 if (req.isc > MAX_ISC)
2216 return -EINVAL;
2217
2218 trace_kvm_s390_modify_ais_mode(req.isc,
2219 (fi->simm & AIS_MODE_MASK(req.isc)) ?
2220 (fi->nimm & AIS_MODE_MASK(req.isc)) ?
2221 2 : KVM_S390_AIS_MODE_SINGLE :
2222 KVM_S390_AIS_MODE_ALL, req.mode);
2223
2224 mutex_lock(&fi->ais_lock);
2225 switch (req.mode) {
2226 case KVM_S390_AIS_MODE_ALL:
2227 fi->simm &= ~AIS_MODE_MASK(req.isc);
2228 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2229 break;
2230 case KVM_S390_AIS_MODE_SINGLE:
2231 fi->simm |= AIS_MODE_MASK(req.isc);
2232 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2233 break;
2234 default:
2235 ret = -EINVAL;
2236 }
2237 mutex_unlock(&fi->ais_lock);
2238
2239 return ret;
2240}
2241
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002242static int kvm_s390_inject_airq(struct kvm *kvm,
2243 struct s390_io_adapter *adapter)
2244{
2245 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2246 struct kvm_s390_interrupt s390int = {
2247 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2248 .parm = 0,
2249 .parm64 = (adapter->isc << 27) | 0x80000000,
2250 };
2251 int ret = 0;
2252
Christian Borntraeger1ba15b22017-05-31 10:18:55 +02002253 if (!test_kvm_facility(kvm, 72) || !adapter->suppressible)
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002254 return kvm_s390_inject_vm(kvm, &s390int);
2255
2256 mutex_lock(&fi->ais_lock);
2257 if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
2258 trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
2259 goto out;
2260 }
2261
2262 ret = kvm_s390_inject_vm(kvm, &s390int);
2263 if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
2264 fi->nimm |= AIS_MODE_MASK(adapter->isc);
2265 trace_kvm_s390_modify_ais_mode(adapter->isc,
2266 KVM_S390_AIS_MODE_SINGLE, 2);
2267 }
2268out:
2269 mutex_unlock(&fi->ais_lock);
2270 return ret;
2271}
2272
2273static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr)
2274{
2275 unsigned int id = attr->attr;
2276 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2277
2278 if (!adapter)
2279 return -EINVAL;
2280
2281 return kvm_s390_inject_airq(kvm, adapter);
2282}
2283
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002284static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr)
2285{
2286 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2287 struct kvm_s390_ais_all ais;
2288
2289 if (!test_kvm_facility(kvm, 72))
2290 return -ENOTSUPP;
2291
2292 if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais)))
2293 return -EFAULT;
2294
2295 mutex_lock(&fi->ais_lock);
2296 fi->simm = ais.simm;
2297 fi->nimm = ais.nimm;
2298 mutex_unlock(&fi->ais_lock);
2299
2300 return 0;
2301}
2302
Jens Freimannc05c4182013-10-07 16:13:45 +02002303static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2304{
2305 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002306 unsigned int i;
2307 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02002308
2309 switch (attr->group) {
2310 case KVM_DEV_FLIC_ENQUEUE:
2311 r = enqueue_floating_irq(dev, attr);
2312 break;
2313 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01002314 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02002315 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002316 case KVM_DEV_FLIC_APF_ENABLE:
2317 dev->kvm->arch.gmap->pfault_enabled = 1;
2318 break;
2319 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2320 dev->kvm->arch.gmap->pfault_enabled = 0;
2321 /*
2322 * Make sure no async faults are in transition when
2323 * clearing the queues. So we don't need to worry
2324 * about late coming workers.
2325 */
2326 synchronize_srcu(&dev->kvm->srcu);
2327 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2328 kvm_clear_async_pf_completion_queue(vcpu);
2329 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002330 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2331 r = register_io_adapter(dev, attr);
2332 break;
2333 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2334 r = modify_io_adapter(dev, attr);
2335 break;
Halil Pasic6d28f782016-01-25 19:10:40 +01002336 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2337 r = clear_io_irq(dev->kvm, attr);
2338 break;
Fei Li51978392017-02-17 17:06:26 +08002339 case KVM_DEV_FLIC_AISM:
2340 r = modify_ais_mode(dev->kvm, attr);
2341 break;
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002342 case KVM_DEV_FLIC_AIRQ_INJECT:
2343 r = flic_inject_airq(dev->kvm, attr);
2344 break;
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002345 case KVM_DEV_FLIC_AISM_ALL:
2346 r = flic_ais_mode_set_all(dev->kvm, attr);
2347 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002348 default:
2349 r = -EINVAL;
2350 }
2351
2352 return r;
2353}
2354
Halil Pasic4f129852016-02-25 12:44:17 +01002355static int flic_has_attr(struct kvm_device *dev,
2356 struct kvm_device_attr *attr)
2357{
2358 switch (attr->group) {
2359 case KVM_DEV_FLIC_GET_ALL_IRQS:
2360 case KVM_DEV_FLIC_ENQUEUE:
2361 case KVM_DEV_FLIC_CLEAR_IRQS:
2362 case KVM_DEV_FLIC_APF_ENABLE:
2363 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2364 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2365 case KVM_DEV_FLIC_ADAPTER_MODIFY:
Halil Pasic6d28f782016-01-25 19:10:40 +01002366 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
Fei Li51978392017-02-17 17:06:26 +08002367 case KVM_DEV_FLIC_AISM:
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002368 case KVM_DEV_FLIC_AIRQ_INJECT:
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002369 case KVM_DEV_FLIC_AISM_ALL:
Halil Pasic4f129852016-02-25 12:44:17 +01002370 return 0;
2371 }
2372 return -ENXIO;
2373}
2374
Jens Freimannc05c4182013-10-07 16:13:45 +02002375static int flic_create(struct kvm_device *dev, u32 type)
2376{
2377 if (!dev)
2378 return -EINVAL;
2379 if (dev->kvm->arch.flic)
2380 return -EINVAL;
2381 dev->kvm->arch.flic = dev;
2382 return 0;
2383}
2384
2385static void flic_destroy(struct kvm_device *dev)
2386{
2387 dev->kvm->arch.flic = NULL;
2388 kfree(dev);
2389}
2390
2391/* s390 floating irq controller (flic) */
2392struct kvm_device_ops kvm_flic_ops = {
2393 .name = "kvm-flic",
2394 .get_attr = flic_get_attr,
2395 .set_attr = flic_set_attr,
Halil Pasic4f129852016-02-25 12:44:17 +01002396 .has_attr = flic_has_attr,
Jens Freimannc05c4182013-10-07 16:13:45 +02002397 .create = flic_create,
2398 .destroy = flic_destroy,
2399};
Cornelia Huck84223592013-07-15 13:36:01 +02002400
2401static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2402{
2403 unsigned long bit;
2404
2405 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2406
2407 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2408}
2409
2410static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2411 u64 addr)
2412{
2413 struct s390_map_info *map;
2414
2415 if (!adapter)
2416 return NULL;
2417
2418 list_for_each_entry(map, &adapter->maps, list) {
2419 if (map->guest_addr == addr)
2420 return map;
2421 }
2422 return NULL;
2423}
2424
2425static int adapter_indicators_set(struct kvm *kvm,
2426 struct s390_io_adapter *adapter,
2427 struct kvm_s390_adapter_int *adapter_int)
2428{
2429 unsigned long bit;
2430 int summary_set, idx;
2431 struct s390_map_info *info;
2432 void *map;
2433
2434 info = get_map_info(adapter, adapter_int->ind_addr);
2435 if (!info)
2436 return -1;
2437 map = page_address(info->page);
2438 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2439 set_bit(bit, map);
2440 idx = srcu_read_lock(&kvm->srcu);
2441 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2442 set_page_dirty_lock(info->page);
2443 info = get_map_info(adapter, adapter_int->summary_addr);
2444 if (!info) {
2445 srcu_read_unlock(&kvm->srcu, idx);
2446 return -1;
2447 }
2448 map = page_address(info->page);
2449 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2450 adapter->swap);
2451 summary_set = test_and_set_bit(bit, map);
2452 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2453 set_page_dirty_lock(info->page);
2454 srcu_read_unlock(&kvm->srcu, idx);
2455 return summary_set ? 0 : 1;
2456}
2457
2458/*
2459 * < 0 - not injected due to error
2460 * = 0 - coalesced, summary indicator already active
2461 * > 0 - injected interrupt
2462 */
2463static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2464 struct kvm *kvm, int irq_source_id, int level,
2465 bool line_status)
2466{
2467 int ret;
2468 struct s390_io_adapter *adapter;
2469
2470 /* We're only interested in the 0->1 transition. */
2471 if (!level)
2472 return 0;
2473 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2474 if (!adapter)
2475 return -1;
2476 down_read(&adapter->maps_lock);
2477 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2478 up_read(&adapter->maps_lock);
2479 if ((ret > 0) && !adapter->masked) {
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002480 ret = kvm_s390_inject_airq(kvm, adapter);
Cornelia Huck84223592013-07-15 13:36:01 +02002481 if (ret == 0)
2482 ret = 1;
2483 }
2484 return ret;
2485}
2486
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002487/*
2488 * Inject the machine check to the guest.
2489 */
2490void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
2491 struct mcck_volatile_info *mcck_info)
2492{
2493 struct kvm_s390_interrupt_info inti;
2494 struct kvm_s390_irq irq;
2495 struct kvm_s390_mchk_info *mchk;
2496 union mci mci;
2497 __u64 cr14 = 0; /* upper bits are not used */
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002498 int rc;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002499
2500 mci.val = mcck_info->mcic;
2501 if (mci.sr)
2502 cr14 |= MCCK_CR14_RECOVERY_SUB_MASK;
2503 if (mci.dg)
2504 cr14 |= MCCK_CR14_DEGRAD_SUB_MASK;
2505 if (mci.w)
2506 cr14 |= MCCK_CR14_WARN_SUB_MASK;
2507
2508 mchk = mci.ck ? &inti.mchk : &irq.u.mchk;
2509 mchk->cr14 = cr14;
2510 mchk->mcic = mcck_info->mcic;
2511 mchk->ext_damage_code = mcck_info->ext_damage_code;
2512 mchk->failing_storage_address = mcck_info->failing_storage_address;
2513 if (mci.ck) {
2514 /* Inject the floating machine check */
2515 inti.type = KVM_S390_MCHK;
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002516 rc = __inject_vm(vcpu->kvm, &inti);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002517 } else {
2518 /* Inject the machine check to specified vcpu */
2519 irq.type = KVM_S390_MCHK;
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002520 rc = kvm_s390_inject_vcpu(vcpu, &irq);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002521 }
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002522 WARN_ON_ONCE(rc);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002523}
2524
Radim Krčmářc63cf532016-07-12 22:09:26 +02002525int kvm_set_routing_entry(struct kvm *kvm,
2526 struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002527 const struct kvm_irq_routing_entry *ue)
2528{
2529 int ret;
2530
2531 switch (ue->type) {
2532 case KVM_IRQ_ROUTING_S390_ADAPTER:
2533 e->set = set_adapter_int;
2534 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2535 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2536 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2537 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2538 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2539 ret = 0;
2540 break;
2541 default:
2542 ret = -EINVAL;
2543 }
2544
2545 return ret;
2546}
2547
2548int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2549 int irq_source_id, int level, bool line_status)
2550{
2551 return -EINVAL;
2552}
Jens Freimann816c7662014-11-24 17:13:46 +01002553
2554int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2555{
2556 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2557 struct kvm_s390_irq *buf;
2558 int r = 0;
2559 int n;
2560
2561 buf = vmalloc(len);
2562 if (!buf)
2563 return -ENOMEM;
2564
2565 if (copy_from_user((void *) buf, irqstate, len)) {
2566 r = -EFAULT;
2567 goto out_free;
2568 }
2569
2570 /*
2571 * Don't allow setting the interrupt state
2572 * when there are already interrupts pending
2573 */
2574 spin_lock(&li->lock);
2575 if (li->pending_irqs) {
2576 r = -EBUSY;
2577 goto out_unlock;
2578 }
2579
2580 for (n = 0; n < len / sizeof(*buf); n++) {
2581 r = do_inject_vcpu(vcpu, &buf[n]);
2582 if (r)
2583 break;
2584 }
2585
2586out_unlock:
2587 spin_unlock(&li->lock);
2588out_free:
2589 vfree(buf);
2590
2591 return r;
2592}
2593
2594static void store_local_irq(struct kvm_s390_local_interrupt *li,
2595 struct kvm_s390_irq *irq,
2596 unsigned long irq_type)
2597{
2598 switch (irq_type) {
2599 case IRQ_PEND_MCHK_EX:
2600 case IRQ_PEND_MCHK_REP:
2601 irq->type = KVM_S390_MCHK;
2602 irq->u.mchk = li->irq.mchk;
2603 break;
2604 case IRQ_PEND_PROG:
2605 irq->type = KVM_S390_PROGRAM_INT;
2606 irq->u.pgm = li->irq.pgm;
2607 break;
2608 case IRQ_PEND_PFAULT_INIT:
2609 irq->type = KVM_S390_INT_PFAULT_INIT;
2610 irq->u.ext = li->irq.ext;
2611 break;
2612 case IRQ_PEND_EXT_EXTERNAL:
2613 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2614 irq->u.extcall = li->irq.extcall;
2615 break;
2616 case IRQ_PEND_EXT_CLOCK_COMP:
2617 irq->type = KVM_S390_INT_CLOCK_COMP;
2618 break;
2619 case IRQ_PEND_EXT_CPU_TIMER:
2620 irq->type = KVM_S390_INT_CPU_TIMER;
2621 break;
2622 case IRQ_PEND_SIGP_STOP:
2623 irq->type = KVM_S390_SIGP_STOP;
2624 irq->u.stop = li->irq.stop;
2625 break;
2626 case IRQ_PEND_RESTART:
2627 irq->type = KVM_S390_RESTART;
2628 break;
2629 case IRQ_PEND_SET_PREFIX:
2630 irq->type = KVM_S390_SIGP_SET_PREFIX;
2631 irq->u.prefix = li->irq.prefix;
2632 break;
2633 }
2634}
2635
2636int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2637{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002638 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002639 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2640 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2641 unsigned long pending_irqs;
2642 struct kvm_s390_irq irq;
2643 unsigned long irq_type;
2644 int cpuaddr;
2645 int n = 0;
2646
2647 spin_lock(&li->lock);
2648 pending_irqs = li->pending_irqs;
2649 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2650 sizeof(sigp_emerg_pending));
2651 spin_unlock(&li->lock);
2652
2653 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2654 memset(&irq, 0, sizeof(irq));
2655 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2656 continue;
2657 if (n + sizeof(irq) > len)
2658 return -ENOBUFS;
2659 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2660 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2661 return -EFAULT;
2662 n += sizeof(irq);
2663 }
2664
2665 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2666 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2667 memset(&irq, 0, sizeof(irq));
2668 if (n + sizeof(irq) > len)
2669 return -ENOBUFS;
2670 irq.type = KVM_S390_INT_EMERGENCY;
2671 irq.u.emerg.code = cpuaddr;
2672 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2673 return -EFAULT;
2674 n += sizeof(irq);
2675 }
2676 }
2677
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002678 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002679 if (n + sizeof(irq) > len)
2680 return -ENOBUFS;
2681 memset(&irq, 0, sizeof(irq));
2682 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002683 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002684 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2685 return -EFAULT;
2686 n += sizeof(irq);
2687 }
2688
2689 return n;
2690}