blob: a832ad031cee78f5cc6fe17b93fb315e17fdd50a [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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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) Dvurechenski7d43baf2015-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
Jens Freimann6d3da242013-07-03 15:18:35 +0200216static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
217 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200218{
Jens Freimann6d3da242013-07-03 15:18:35 +0200219 int i;
220
221 for (i = 0; i <= MAX_ISC; i++)
222 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
223 active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
224
225 return active_mask;
226}
227
228static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
229{
230 unsigned long active_mask;
231
David Hildenbrand5f94c582015-09-28 14:27:51 +0200232 active_mask = pending_irqs(vcpu);
Jens Freimannffeca0a2015-04-17 10:21:04 +0200233 if (!active_mask)
234 return 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200235
236 if (psw_extint_disabled(vcpu))
237 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200238 if (psw_ioint_disabled(vcpu))
239 active_mask &= ~IRQ_PEND_IO_MASK;
240 else
241 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200242 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
243 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
244 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
245 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
246 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
247 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
248 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
249 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200250 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
251 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200252 if (psw_mchk_disabled(vcpu))
253 active_mask &= ~IRQ_PEND_MCHK_MASK;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +0200254 /*
255 * Check both floating and local interrupt's cr14 because
256 * bit IRQ_PEND_MCHK_REP could be set in both cases.
257 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200258 if (!(vcpu->arch.sie_block->gcr[14] &
QingFeng Hao4d62fcc2017-06-07 12:03:05 +0200259 (vcpu->kvm->arch.float_int.mchk.cr14 |
260 vcpu->arch.local_int.irq.mchk.cr14)))
Jens Freimann6d3da242013-07-03 15:18:35 +0200261 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200262
David Hildenbrand6cddd432014-10-15 16:48:53 +0200263 /*
264 * STOP irqs will never be actively delivered. They are triggered via
265 * intercept requests and cleared when the stop intercept is performed.
266 */
267 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
268
Jens Freimann383d0b02014-07-29 15:11:49 +0200269 return active_mask;
270}
271
Carsten Otteba5c1e92008-03-25 18:47:26 +0100272static void __set_cpu_idle(struct kvm_vcpu *vcpu)
273{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200274 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100275 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
276}
277
278static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
279{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200280 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100281 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
282}
283
284static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
285{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200286 atomic_andnot(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
287 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100288 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100289 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
290
291 if (guestdbg_enabled(vcpu)) {
292 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
293 LCTL_CR10 | LCTL_CR11);
294 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
295 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100296}
297
298static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
299{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200300 atomic_or(flag, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100301}
302
Jens Freimann6d3da242013-07-03 15:18:35 +0200303static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
304{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200305 if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
Jens Freimann6d3da242013-07-03 15:18:35 +0200306 return;
307 else if (psw_ioint_disabled(vcpu))
308 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
309 else
310 vcpu->arch.sie_block->lctl |= LCTL_CR6;
311}
312
Jens Freimann383d0b02014-07-29 15:11:49 +0200313static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
314{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200315 if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200316 return;
317 if (psw_extint_disabled(vcpu))
318 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
319 else
320 vcpu->arch.sie_block->lctl |= LCTL_CR0;
321}
322
323static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
324{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200325 if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200326 return;
327 if (psw_mchk_disabled(vcpu))
328 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
329 else
330 vcpu->arch.sie_block->lctl |= LCTL_CR14;
331}
332
David Hildenbrand6cddd432014-10-15 16:48:53 +0200333static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
334{
335 if (kvm_s390_is_stop_irq_pending(vcpu))
336 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
337}
338
Jens Freimann6d3da242013-07-03 15:18:35 +0200339/* Set interception request for non-deliverable interrupts */
340static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200341{
Jens Freimann6d3da242013-07-03 15:18:35 +0200342 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200343 set_intercept_indicators_ext(vcpu);
344 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200345 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200346}
347
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200348static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100349{
Jens Freimann383d0b02014-07-29 15:11:49 +0200350 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200351 int rc;
352
353 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
354 0, 0);
355
356 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
357 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100358 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200359 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
360 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
361 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
362 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200363 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100364 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200365}
366
367static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
368{
Jens Freimann383d0b02014-07-29 15:11:49 +0200369 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200370 int rc;
371
372 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
373 0, 0);
374
375 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
376 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100377 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200378 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
379 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
380 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
381 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200382 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100383 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200384}
385
Jens Freimann383d0b02014-07-29 15:11:49 +0200386static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200387{
Jens Freimann383d0b02014-07-29 15:11:49 +0200388 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
389 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200390 int rc;
391
Jens Freimann383d0b02014-07-29 15:11:49 +0200392 spin_lock(&li->lock);
393 ext = li->irq.ext;
394 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
395 li->irq.ext.ext_params2 = 0;
396 spin_unlock(&li->lock);
397
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200398 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
399 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200400 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
401 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200402 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200403
404 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
405 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
406 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
407 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
408 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
409 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200410 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100411 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200412}
413
David Hildenbrandd6404de2015-10-14 16:47:36 +0200414static int __write_machine_check(struct kvm_vcpu *vcpu,
415 struct kvm_s390_mchk_info *mchk)
416{
417 unsigned long ext_sa_addr;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100418 unsigned long lc;
David Hildenbrand0319dae2016-08-03 11:18:57 +0200419 freg_t fprs[NUM_FPRS];
David Hildenbrandff5dc142015-10-14 16:57:56 +0200420 union mci mci;
David Hildenbrandd6404de2015-10-14 16:47:36 +0200421 int rc;
422
David Hildenbrandff5dc142015-10-14 16:57:56 +0200423 mci.val = mchk->mcic;
Christian Borntraeger31d8b8d2016-11-10 14:22:02 +0100424 /* take care of lazy register loading */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200425 save_fpu_regs();
426 save_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger80248552017-04-12 12:59:59 +0200427 if (MACHINE_HAS_GS && vcpu->arch.gs_enabled)
428 save_gs_cb(current->thread.gs_cb);
David Hildenbrand0319dae2016-08-03 11:18:57 +0200429
David Hildenbrandd6404de2015-10-14 16:47:36 +0200430 /* Extended save area */
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100431 rc = read_guest_lc(vcpu, __LC_MCESAD, &ext_sa_addr,
432 sizeof(unsigned long));
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100433 /* Only bits 0 through 63-LC are used for address formation */
434 lc = ext_sa_addr & MCESA_LC_MASK;
435 if (test_kvm_facility(vcpu->kvm, 133)) {
436 switch (lc) {
437 case 0:
438 case 10:
439 ext_sa_addr &= ~0x3ffUL;
440 break;
441 case 11:
442 ext_sa_addr &= ~0x7ffUL;
443 break;
444 case 12:
445 ext_sa_addr &= ~0xfffUL;
446 break;
447 default:
448 ext_sa_addr = 0;
449 break;
450 }
451 } else {
452 ext_sa_addr &= ~0x3ffUL;
453 }
454
David Hildenbrandff5dc142015-10-14 16:57:56 +0200455 if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) {
456 if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs,
457 512))
458 mci.vr = 0;
459 } else {
460 mci.vr = 0;
461 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100462 if (!rc && mci.gs && ext_sa_addr && test_kvm_facility(vcpu->kvm, 133)
463 && (lc == 11 || lc == 12)) {
464 if (write_guest_abs(vcpu, ext_sa_addr + 1024,
465 &vcpu->run->s.regs.gscb, 32))
466 mci.gs = 0;
467 } else {
468 mci.gs = 0;
469 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200470
471 /* General interruption information */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200472 rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200473 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
474 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
475 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
476 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
David Hildenbrandff5dc142015-10-14 16:57:56 +0200477 rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200478
479 /* Register-save areas */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200480 if (MACHINE_HAS_VX) {
481 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
482 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128);
483 } else {
484 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA,
485 vcpu->run->s.regs.fprs, 128);
486 }
487 rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA,
488 vcpu->run->s.regs.gprs, 128);
489 rc |= put_guest_lc(vcpu, current->thread.fpu.fpc,
490 (u32 __user *) __LC_FP_CREG_SAVE_AREA);
491 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr,
492 (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA);
493 rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu),
494 (u64 __user *) __LC_CPU_TIMER_SAVE_AREA);
495 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8,
496 (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA);
497 rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA,
498 &vcpu->run->s.regs.acrs, 64);
499 rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA,
500 &vcpu->arch.sie_block->gcr, 128);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200501
502 /* Extended interruption information */
David Hildenbrand8953fb02016-08-03 12:25:08 +0200503 rc |= put_guest_lc(vcpu, mchk->ext_damage_code,
504 (u32 __user *) __LC_EXT_DAMAGE_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200505 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
506 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
507 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
508 sizeof(mchk->fixed_logout));
509 return rc ? -EFAULT : 0;
510}
511
Jens Freimann383d0b02014-07-29 15:11:49 +0200512static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200513{
Jens Freimann6d3da242013-07-03 15:18:35 +0200514 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200515 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200516 struct kvm_s390_mchk_info mchk = {};
Jens Freimann6d3da242013-07-03 15:18:35 +0200517 int deliver = 0;
518 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200519
Jens Freimann6d3da242013-07-03 15:18:35 +0200520 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200521 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200522 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
523 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
524 /*
525 * If there was an exigent machine check pending, then any
526 * repressible machine checks that might have been pending
527 * are indicated along with it, so always clear bits for
528 * repressible and exigent interrupts
529 */
530 mchk = li->irq.mchk;
531 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
532 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
533 memset(&li->irq.mchk, 0, sizeof(mchk));
534 deliver = 1;
535 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200536 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200537 * We indicate floating repressible conditions along with
538 * other pending conditions. Channel Report Pending and Channel
539 * Subsystem damage are the only two and and are indicated by
540 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200541 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200542 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
543 mchk.mcic |= fi->mchk.mcic;
544 mchk.cr14 |= fi->mchk.cr14;
545 memset(&fi->mchk, 0, sizeof(mchk));
546 deliver = 1;
547 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200548 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200549 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200550
Jens Freimann6d3da242013-07-03 15:18:35 +0200551 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200552 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200553 mchk.mcic);
554 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
555 KVM_S390_MCHK,
556 mchk.cr14, mchk.mcic);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200557 rc = __write_machine_check(vcpu, &mchk);
Jens Freimann6d3da242013-07-03 15:18:35 +0200558 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200559 return rc;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200560}
561
562static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
563{
Jens Freimann383d0b02014-07-29 15:11:49 +0200564 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200565 int rc;
566
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200567 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200568 vcpu->stat.deliver_restart_signal++;
569 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
570
571 rc = write_guest_lc(vcpu,
Heiko Carstensc667aea2015-12-31 10:29:00 +0100572 offsetof(struct lowcore, restart_old_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200573 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstensc667aea2015-12-31 10:29:00 +0100574 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200575 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200576 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100577 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200578}
579
Jens Freimann383d0b02014-07-29 15:11:49 +0200580static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200581{
Jens Freimann383d0b02014-07-29 15:11:49 +0200582 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
583 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200584
Jens Freimann383d0b02014-07-29 15:11:49 +0200585 spin_lock(&li->lock);
586 prefix = li->irq.prefix;
587 li->irq.prefix.address = 0;
588 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
589 spin_unlock(&li->lock);
590
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200591 vcpu->stat.deliver_prefix_signal++;
592 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
593 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200594 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200595
Jens Freimann383d0b02014-07-29 15:11:49 +0200596 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200597 return 0;
598}
599
Jens Freimann383d0b02014-07-29 15:11:49 +0200600static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200601{
Jens Freimann383d0b02014-07-29 15:11:49 +0200602 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200603 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200604 int cpu_addr;
605
606 spin_lock(&li->lock);
607 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
608 clear_bit(cpu_addr, li->sigp_emerg_pending);
609 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
610 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
611 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200612
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200613 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200614 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200615 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
616 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200617
618 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
619 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200620 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200621 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
622 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
623 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
624 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100625 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200626}
627
Jens Freimann383d0b02014-07-29 15:11:49 +0200628static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200629{
Jens Freimann383d0b02014-07-29 15:11:49 +0200630 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
631 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200632 int rc;
633
Jens Freimann383d0b02014-07-29 15:11:49 +0200634 spin_lock(&li->lock);
635 extcall = li->irq.extcall;
636 li->irq.extcall.code = 0;
637 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
638 spin_unlock(&li->lock);
639
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200640 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200641 vcpu->stat.deliver_external_call++;
642 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
643 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200644 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200645
646 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
647 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200648 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200649 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
650 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
651 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
652 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100653 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200654}
655
Jens Freimann383d0b02014-07-29 15:11:49 +0200656static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200657{
Jens Freimann383d0b02014-07-29 15:11:49 +0200658 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
659 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100660 int rc = 0, nullifying = false;
David Hildenbrand634790b2015-11-04 16:33:33 +0100661 u16 ilen;
David Hildenbrand87128362014-03-03 10:55:13 +0100662
Jens Freimann383d0b02014-07-29 15:11:49 +0200663 spin_lock(&li->lock);
664 pgm_info = li->irq.pgm;
665 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
666 memset(&li->irq.pgm, 0, sizeof(pgm_info));
667 spin_unlock(&li->lock);
668
David Hildenbrand634790b2015-11-04 16:33:33 +0100669 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100670 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
671 pgm_info.code, ilen);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200672 vcpu->stat.deliver_program_int++;
673 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200674 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200675
Jens Freimann383d0b02014-07-29 15:11:49 +0200676 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100677 case PGM_AFX_TRANSLATION:
678 case PGM_ASX_TRANSLATION:
679 case PGM_EX_TRANSLATION:
680 case PGM_LFX_TRANSLATION:
681 case PGM_LSTE_SEQUENCE:
682 case PGM_LSX_TRANSLATION:
683 case PGM_LX_TRANSLATION:
684 case PGM_PRIMARY_AUTHORITY:
685 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100686 nullifying = true;
687 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100688 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200689 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100690 (u64 *)__LC_TRANS_EXC_CODE);
691 break;
692 case PGM_ALEN_TRANSLATION:
693 case PGM_ALE_SEQUENCE:
694 case PGM_ASTE_INSTANCE:
695 case PGM_ASTE_SEQUENCE:
696 case PGM_ASTE_VALIDITY:
697 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200698 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100699 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100700 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100701 break;
702 case PGM_ASCE_TYPE:
703 case PGM_PAGE_TRANSLATION:
704 case PGM_REGION_FIRST_TRANS:
705 case PGM_REGION_SECOND_TRANS:
706 case PGM_REGION_THIRD_TRANS:
707 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200708 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100709 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200710 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100711 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200712 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100713 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100714 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100715 break;
716 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200717 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200718 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200719 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100720 (u64 *)__LC_MON_CODE);
721 break;
Eric Farman403c8642015-02-02 15:01:06 -0500722 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100723 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200724 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100725 (u32 *)__LC_DATA_EXC_CODE);
726 break;
727 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200728 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100729 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200730 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100731 (u8 *)__LC_EXC_ACCESS_ID);
732 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100733 case PGM_STACK_FULL:
734 case PGM_STACK_EMPTY:
735 case PGM_STACK_SPECIFICATION:
736 case PGM_STACK_TYPE:
737 case PGM_STACK_OPERATION:
738 case PGM_TRACE_TABEL:
739 case PGM_CRYPTO_OPERATION:
740 nullifying = true;
741 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100742 }
743
Jens Freimann383d0b02014-07-29 15:11:49 +0200744 if (pgm_info.code & PGM_PER) {
745 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100746 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200747 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100748 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200749 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100750 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200751 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100752 (u8 *) __LC_PER_ACCESS_ID);
753 }
754
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100755 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100756 kvm_s390_rewind_psw(vcpu, ilen);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100757
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100758 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
759 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100760 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
761 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200762 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100763 (u16 *)__LC_PGM_INT_CODE);
764 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
765 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
766 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
767 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100768 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200769}
David Hildenbrand87128362014-03-03 10:55:13 +0100770
Jens Freimann6d3da242013-07-03 15:18:35 +0200771static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200772{
Jens Freimann6d3da242013-07-03 15:18:35 +0200773 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
774 struct kvm_s390_ext_info ext;
775 int rc = 0;
776
777 spin_lock(&fi->lock);
778 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
779 spin_unlock(&fi->lock);
780 return 0;
781 }
782 ext = fi->srv_signal;
783 memset(&fi->srv_signal, 0, sizeof(ext));
784 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
785 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200786
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200787 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200788 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200789 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200790 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
791 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200792
793 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100794 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200795 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
796 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
797 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
798 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200799 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200800 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200801
Jens Freimann99e20002014-12-01 17:05:39 +0100802 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200803}
804
Jens Freimann6d3da242013-07-03 15:18:35 +0200805static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200806{
Jens Freimann6d3da242013-07-03 15:18:35 +0200807 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
808 struct kvm_s390_interrupt_info *inti;
809 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200810
Jens Freimann6d3da242013-07-03 15:18:35 +0200811 spin_lock(&fi->lock);
812 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
813 struct kvm_s390_interrupt_info,
814 list);
815 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200816 list_del(&inti->list);
817 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
818 }
819 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
820 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
821 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200822
Jens Freimann6d3da242013-07-03 15:18:35 +0200823 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200824 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
825 KVM_S390_INT_PFAULT_DONE, 0,
826 inti->ext.ext_params2);
827 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
828 inti->ext.ext_params2);
829
Jens Freimann6d3da242013-07-03 15:18:35 +0200830 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
831 (u16 *)__LC_EXT_INT_CODE);
832 rc |= put_guest_lc(vcpu, PFAULT_DONE,
833 (u16 *)__LC_EXT_CPU_ADDR);
834 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
835 &vcpu->arch.sie_block->gpsw,
836 sizeof(psw_t));
837 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
838 &vcpu->arch.sie_block->gpsw,
839 sizeof(psw_t));
840 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
841 (u64 *)__LC_EXT_PARAMS2);
842 kfree(inti);
843 }
Jens Freimann99e20002014-12-01 17:05:39 +0100844 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200845}
846
Jens Freimann6d3da242013-07-03 15:18:35 +0200847static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200848{
Jens Freimann6d3da242013-07-03 15:18:35 +0200849 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
850 struct kvm_s390_interrupt_info *inti;
851 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200852
Jens Freimann6d3da242013-07-03 15:18:35 +0200853 spin_lock(&fi->lock);
854 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
855 struct kvm_s390_interrupt_info,
856 list);
857 if (inti) {
858 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200859 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200860 inti->ext.ext_params, inti->ext.ext_params2);
861 vcpu->stat.deliver_virtio_interrupt++;
862 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
863 inti->type,
864 inti->ext.ext_params,
865 inti->ext.ext_params2);
866 list_del(&inti->list);
867 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
868 }
869 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
870 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
871 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200872
Jens Freimann6d3da242013-07-03 15:18:35 +0200873 if (inti) {
874 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
875 (u16 *)__LC_EXT_INT_CODE);
876 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
877 (u16 *)__LC_EXT_CPU_ADDR);
878 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
879 &vcpu->arch.sie_block->gpsw,
880 sizeof(psw_t));
881 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
882 &vcpu->arch.sie_block->gpsw,
883 sizeof(psw_t));
884 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
885 (u32 *)__LC_EXT_PARAMS);
886 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
887 (u64 *)__LC_EXT_PARAMS2);
888 kfree(inti);
889 }
Jens Freimann99e20002014-12-01 17:05:39 +0100890 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200891}
892
893static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200894 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200895{
Jens Freimann6d3da242013-07-03 15:18:35 +0200896 struct list_head *isc_list;
897 struct kvm_s390_float_interrupt *fi;
898 struct kvm_s390_interrupt_info *inti = NULL;
899 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200900
Jens Freimann6d3da242013-07-03 15:18:35 +0200901 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200902
Jens Freimann6d3da242013-07-03 15:18:35 +0200903 spin_lock(&fi->lock);
904 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
905 inti = list_first_entry_or_null(isc_list,
906 struct kvm_s390_interrupt_info,
907 list);
908 if (inti) {
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +0200909 if (inti->type & KVM_S390_INT_IO_AI_MASK)
910 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
911 else
912 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
913 inti->io.subchannel_id >> 8,
914 inti->io.subchannel_id >> 1 & 0x3,
915 inti->io.subchannel_nr);
916
Jens Freimann6d3da242013-07-03 15:18:35 +0200917 vcpu->stat.deliver_io_int++;
918 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
919 inti->type,
920 ((__u32)inti->io.subchannel_id << 16) |
921 inti->io.subchannel_nr,
922 ((__u64)inti->io.io_int_parm << 32) |
923 inti->io.io_int_word);
924 list_del(&inti->list);
925 fi->counters[FIRQ_CNTR_IO] -= 1;
926 }
927 if (list_empty(isc_list))
928 clear_bit(irq_type, &fi->pending_irqs);
929 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100930
Jens Freimann6d3da242013-07-03 15:18:35 +0200931 if (inti) {
932 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
933 (u16 *)__LC_SUBCHANNEL_ID);
934 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
935 (u16 *)__LC_SUBCHANNEL_NR);
936 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
937 (u32 *)__LC_IO_INT_PARM);
938 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
939 (u32 *)__LC_IO_INT_WORD);
940 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
941 &vcpu->arch.sie_block->gpsw,
942 sizeof(psw_t));
943 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
944 &vcpu->arch.sie_block->gpsw,
945 sizeof(psw_t));
946 kfree(inti);
947 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200948
Jens Freimann99e20002014-12-01 17:05:39 +0100949 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200950}
951
952typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
953
954static const deliver_irq_t deliver_irq_funcs[] = {
955 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200956 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200957 [IRQ_PEND_PROG] = __deliver_prog,
958 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
959 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
960 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
961 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
962 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200963 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
964 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200965 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
966 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
967 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200968};
969
David Hildenbrandea5f4962014-10-14 15:29:30 +0200970/* Check whether an external call is pending (deliverable or not) */
971int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100972{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200973 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +0100974
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200975 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200976 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100977
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200978 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100979}
980
David Hildenbrand9a022062014-08-05 17:40:47 +0200981int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100982{
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200983 if (deliverable_irqs(vcpu))
984 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100985
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200986 if (kvm_cpu_has_pending_timer(vcpu))
987 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100988
David Hildenbrandea5f4962014-10-14 15:29:30 +0200989 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200990 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +0200991 !psw_extint_disabled(vcpu) &&
992 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200993 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +0100994
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200995 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
996 return 1;
997 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100998}
999
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001000int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1001{
David Hildenbrandb4aec922014-12-01 15:55:42 +01001002 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -03001003}
1004
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001005static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
1006{
1007 u64 now, cputm, sltime = 0;
1008
1009 if (ckc_interrupts_enabled(vcpu)) {
1010 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1011 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
1012 /* already expired or overflow? */
1013 if (!sltime || vcpu->arch.sie_block->ckc <= now)
1014 return 0;
1015 if (cpu_timer_interrupts_enabled(vcpu)) {
1016 cputm = kvm_s390_get_cpu_timer(vcpu);
1017 /* already expired? */
1018 if (cputm >> 63)
1019 return 0;
1020 return min(sltime, tod_to_ns(cputm));
1021 }
1022 } else if (cpu_timer_interrupts_enabled(vcpu)) {
1023 sltime = kvm_s390_get_cpu_timer(vcpu);
1024 /* already expired? */
1025 if (sltime >> 63)
1026 return 0;
1027 }
1028 return sltime;
1029}
1030
Carsten Otteba5c1e92008-03-25 18:47:26 +01001031int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
1032{
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001033 u64 sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001034
1035 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001036
David Hildenbrand0759d062014-05-13 16:54:32 +02001037 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +02001038 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +02001039 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +02001040
Carsten Otteba5c1e92008-03-25 18:47:26 +01001041 if (psw_interrupts_disabled(vcpu)) {
1042 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001043 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +01001044 }
1045
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001046 if (!ckc_interrupts_enabled(vcpu) &&
1047 !cpu_timer_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001048 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +01001049 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001050 goto no_timer;
1051 }
1052
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001053 sltime = __calculate_sltime(vcpu);
1054 if (!sltime)
David Hildenbrandbda343e2014-12-12 12:26:40 +01001055 return 0;
1056
1057 __set_cpu_idle(vcpu);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001058 hrtimer_start(&vcpu->arch.ckc_timer, sltime, HRTIMER_MODE_REL);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001059 VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001060no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +02001061 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +02001062 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001063 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +02001064 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1065
David Hildenbrand2d00f752014-12-11 10:18:01 +01001066 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001067 return 0;
1068}
1069
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001070void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1071{
Christian Borntraeger3491caf2016-05-13 12:16:35 +02001072 /*
1073 * We cannot move this into the if, as the CPU might be already
1074 * in kvm_vcpu_block without having the waitqueue set (polling)
1075 */
1076 vcpu->valid_wakeup = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001077 if (swait_active(&vcpu->wq)) {
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001078 /*
1079 * The vcpu gave up the cpu voluntarily, mark it as a good
1080 * yield-candidate.
1081 */
1082 vcpu->preempted = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001083 swake_up(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +02001084 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001085 }
David Hildenbrandadbf1692016-05-27 22:03:52 +02001086 /*
1087 * The VCPU might not be sleeping but is executing the VSIE. Let's
1088 * kick it, so it leaves the SIE to process the request.
1089 */
1090 kvm_s390_vsie_kick(vcpu);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001091}
1092
Christian Borntraegerca872302009-05-12 17:21:49 +02001093enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1094{
1095 struct kvm_vcpu *vcpu;
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001096 u64 sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +02001097
1098 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001099 sltime = __calculate_sltime(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001100
David Hildenbrand2d00f752014-12-11 10:18:01 +01001101 /*
1102 * If the monotonic clock runs faster than the tod clock we might be
1103 * woken up too early and have to go back to sleep to avoid deadlocks.
1104 */
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001105 if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
David Hildenbrand2d00f752014-12-11 10:18:01 +01001106 return HRTIMER_RESTART;
1107 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001108 return HRTIMER_NORESTART;
1109}
Carsten Otteba5c1e92008-03-25 18:47:26 +01001110
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001111void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1112{
1113 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001114
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001115 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001116 li->pending_irqs = 0;
1117 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1118 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001119 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +01001120
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001121 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001122}
1123
Christian Borntraeger614aeab2014-08-25 12:27:29 +02001124int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001125{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001126 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001127 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +02001128 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +02001129 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +02001130 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001131
1132 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001133
Jens Freimann383d0b02014-07-29 15:11:49 +02001134 /* pending ckc conditions might have been invalidated */
1135 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +01001136 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +02001137 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1138
David Hildenbrandb4aec922014-12-01 15:55:42 +01001139 /* pending cpu timer conditions might have been invalidated */
1140 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1141 if (cpu_timer_irq_pending(vcpu))
1142 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1143
Jens Freimannffeca0a2015-04-17 10:21:04 +02001144 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001145 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +02001146 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +02001147 if (is_ioirq(irq_type)) {
1148 rc = __deliver_io(vcpu, irq_type);
1149 } else {
1150 func = deliver_irq_funcs[irq_type];
1151 if (!func) {
1152 WARN_ON_ONCE(func == NULL);
1153 clear_bit(irq_type, &li->pending_irqs);
1154 continue;
1155 }
1156 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +02001157 }
Jens Freimannffeca0a2015-04-17 10:21:04 +02001158 }
Jens Freimann383d0b02014-07-29 15:11:49 +02001159
Jens Freimann6d3da242013-07-03 15:18:35 +02001160 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +02001161
1162 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001163}
1164
Jens Freimann383d0b02014-07-29 15:11:49 +02001165static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001166{
1167 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1168
David Hildenbranded2afcf2015-07-20 10:33:03 +02001169 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1170 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1171 irq->u.pgm.code, 0);
1172
David Hildenbrand634790b2015-11-04 16:33:33 +01001173 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1174 /* auto detection if no valid ILC was given */
1175 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1176 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1177 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1178 }
1179
David Hildenbrand238293b2015-05-04 12:38:48 +02001180 if (irq->u.pgm.code == PGM_PER) {
1181 li->irq.pgm.code |= PGM_PER;
David Hildenbrand634790b2015-11-04 16:33:33 +01001182 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001183 /* only modify PER related information */
1184 li->irq.pgm.per_address = irq->u.pgm.per_address;
1185 li->irq.pgm.per_code = irq->u.pgm.per_code;
1186 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1187 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1188 } else if (!(irq->u.pgm.code & PGM_PER)) {
1189 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1190 irq->u.pgm.code;
David Hildenbrand634790b2015-11-04 16:33:33 +01001191 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001192 /* only modify non-PER information */
1193 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1194 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1195 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1196 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1197 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1198 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1199 } else {
1200 li->irq.pgm = irq->u.pgm;
1201 }
Jens Freimann91851242014-12-01 16:43:40 +01001202 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001203 return 0;
1204}
1205
Jens Freimann383d0b02014-07-29 15:11:49 +02001206static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001207{
1208 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1209
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001210 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1211 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001212 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1213 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001214 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001215
1216 li->irq.ext = irq->u.ext;
1217 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001218 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001219 return 0;
1220}
1221
Christian Borntraeger0675d922015-01-15 12:40:42 +01001222static int __inject_extcall(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;
Jens Freimann383d0b02014-07-29 15:11:49 +02001225 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001226 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001227
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001228 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001229 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001230 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001231 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001232
David Hildenbrandea5f4962014-10-14 15:29:30 +02001233 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001234 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001235 return -EINVAL;
1236
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001237 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001238 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001239
David Hildenbrandb938eace2015-04-30 13:33:59 +02001240 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001241 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001242 *extcall = irq->u.extcall;
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001243 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001244 return 0;
1245}
1246
Jens Freimann383d0b02014-07-29 15:11:49 +02001247static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001248{
1249 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001250 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001251
David Hildenbranded2afcf2015-07-20 10:33:03 +02001252 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001253 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001254 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001255 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001256
David Hildenbranda3a9c592014-10-14 09:44:55 +02001257 if (!is_vcpu_stopped(vcpu))
1258 return -EBUSY;
1259
Jens Freimann383d0b02014-07-29 15:11:49 +02001260 *prefix = irq->u.prefix;
1261 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001262 return 0;
1263}
1264
David Hildenbrand6cddd432014-10-15 16:48:53 +02001265#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001266static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001267{
1268 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001269 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001270 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001271
David Hildenbranded2afcf2015-07-20 10:33:03 +02001272 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001273
David Hildenbrand28225452014-10-15 16:48:16 +02001274 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1275 return -EINVAL;
1276
David Hildenbrand6cddd432014-10-15 16:48:53 +02001277 if (is_vcpu_stopped(vcpu)) {
1278 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1279 rc = kvm_s390_store_status_unloaded(vcpu,
1280 KVM_S390_STORE_STATUS_NOADDR);
1281 return rc;
1282 }
1283
1284 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1285 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001286 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001287 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001288 return 0;
1289}
1290
1291static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001292 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001293{
1294 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1295
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001296 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001297 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001298
1299 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001300 return 0;
1301}
1302
1303static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001304 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001305{
1306 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1307
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001308 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001309 irq->u.emerg.code);
1310 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001311 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001312
David Hildenbrandb85de332015-11-05 09:38:15 +01001313 /* sending vcpu invalid */
1314 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1315 return -EINVAL;
1316
Jens Freimann49538d12014-12-18 15:48:14 +01001317 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001318 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001319 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001320 return 0;
1321}
1322
Jens Freimann383d0b02014-07-29 15:11:49 +02001323static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001324{
1325 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001326 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001327
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001328 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001329 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001330 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001331 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001332
1333 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001334 * Because repressible machine checks can be indicated along with
1335 * exigent machine checks (PoP, Chapter 11, Interruption action)
1336 * we need to combine cr14, mcic and external damage code.
1337 * Failing storage address and the logout area should not be or'ed
1338 * together, we just indicate the last occurrence of the corresponding
1339 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001340 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001341 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001342 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001343 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1344 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1345 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1346 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001347 if (mchk->mcic & MCHK_EX_MASK)
1348 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1349 else if (mchk->mcic & MCHK_REP_MASK)
1350 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001351 return 0;
1352}
1353
Jens Freimann383d0b02014-07-29 15:11:49 +02001354static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001355{
1356 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1357
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001358 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001359 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001360 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001361
1362 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001363 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001364 return 0;
1365}
1366
Jens Freimann383d0b02014-07-29 15:11:49 +02001367static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001368{
1369 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1370
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001371 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001372 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001373 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001374
1375 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001376 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001377 return 0;
1378}
1379
Jens Freimann6d3da242013-07-03 15:18:35 +02001380static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1381 int isc, u32 schid)
1382{
1383 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1384 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1385 struct kvm_s390_interrupt_info *iter;
1386 u16 id = (schid & 0xffff0000U) >> 16;
1387 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001388
Jens Freimann6d3da242013-07-03 15:18:35 +02001389 spin_lock(&fi->lock);
1390 list_for_each_entry(iter, isc_list, list) {
1391 if (schid && (id != iter->io.subchannel_id ||
1392 nr != iter->io.subchannel_nr))
1393 continue;
1394 /* found an appropriate entry */
1395 list_del_init(&iter->list);
1396 fi->counters[FIRQ_CNTR_IO] -= 1;
1397 if (list_empty(isc_list))
1398 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1399 spin_unlock(&fi->lock);
1400 return iter;
1401 }
1402 spin_unlock(&fi->lock);
1403 return NULL;
1404}
1405
1406/*
1407 * Dequeue and return an I/O interrupt matching any of the interruption
1408 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1409 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001410struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001411 u64 isc_mask, u32 schid)
1412{
1413 struct kvm_s390_interrupt_info *inti = NULL;
1414 int isc;
1415
1416 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1417 if (isc_mask & isc_to_isc_bits(isc))
1418 inti = get_io_int(kvm, isc, schid);
1419 }
1420 return inti;
1421}
1422
1423#define SCCB_MASK 0xFFFFFFF8
1424#define SCCB_EVENT_PENDING 0x3
1425
1426static int __inject_service(struct kvm *kvm,
1427 struct kvm_s390_interrupt_info *inti)
1428{
1429 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1430
1431 spin_lock(&fi->lock);
1432 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1433 /*
1434 * Early versions of the QEMU s390 bios will inject several
1435 * service interrupts after another without handling a
1436 * condition code indicating busy.
1437 * We will silently ignore those superfluous sccb values.
1438 * A future version of QEMU will take care of serialization
1439 * of servc requests
1440 */
1441 if (fi->srv_signal.ext_params & SCCB_MASK)
1442 goto out;
1443 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1444 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1445out:
1446 spin_unlock(&fi->lock);
1447 kfree(inti);
1448 return 0;
1449}
1450
1451static int __inject_virtio(struct kvm *kvm,
1452 struct kvm_s390_interrupt_info *inti)
1453{
1454 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1455
1456 spin_lock(&fi->lock);
1457 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1458 spin_unlock(&fi->lock);
1459 return -EBUSY;
1460 }
1461 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1462 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1463 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1464 spin_unlock(&fi->lock);
1465 return 0;
1466}
1467
1468static int __inject_pfault_done(struct kvm *kvm,
1469 struct kvm_s390_interrupt_info *inti)
1470{
1471 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1472
1473 spin_lock(&fi->lock);
1474 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1475 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1476 spin_unlock(&fi->lock);
1477 return -EBUSY;
1478 }
1479 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1480 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1481 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1482 spin_unlock(&fi->lock);
1483 return 0;
1484}
1485
1486#define CR_PENDING_SUBCLASS 28
1487static int __inject_float_mchk(struct kvm *kvm,
1488 struct kvm_s390_interrupt_info *inti)
1489{
1490 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1491
1492 spin_lock(&fi->lock);
1493 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1494 fi->mchk.mcic |= inti->mchk.mcic;
1495 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1496 spin_unlock(&fi->lock);
1497 kfree(inti);
1498 return 0;
1499}
1500
1501static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001502{
1503 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001504 struct list_head *list;
1505 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001506
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001507 fi = &kvm->arch.float_int;
1508 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001509 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1510 spin_unlock(&fi->lock);
1511 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001512 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001513 fi->counters[FIRQ_CNTR_IO] += 1;
1514
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +02001515 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1516 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1517 else
1518 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1519 inti->io.subchannel_id >> 8,
1520 inti->io.subchannel_id >> 1 & 0x3,
1521 inti->io.subchannel_nr);
Jens Freimann6d3da242013-07-03 15:18:35 +02001522 isc = int_word_to_isc(inti->io.io_int_word);
1523 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1524 list_add_tail(&inti->list, list);
1525 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001526 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001527 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001528}
1529
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001530/*
1531 * Find a destination VCPU for a floating irq and kick it.
1532 */
1533static void __floating_irq_kick(struct kvm *kvm, u64 type)
1534{
1535 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1536 struct kvm_s390_local_interrupt *li;
1537 struct kvm_vcpu *dst_vcpu;
1538 int sigcpu, online_vcpus, nr_tries = 0;
1539
1540 online_vcpus = atomic_read(&kvm->online_vcpus);
1541 if (!online_vcpus)
1542 return;
1543
1544 /* find idle VCPUs first, then round robin */
1545 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1546 if (sigcpu == online_vcpus) {
1547 do {
1548 sigcpu = fi->next_rr_cpu;
1549 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1550 /* avoid endless loops if all vcpus are stopped */
1551 if (nr_tries++ >= online_vcpus)
1552 return;
1553 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1554 }
1555 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1556
1557 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1558 li = &dst_vcpu->arch.local_int;
1559 spin_lock(&li->lock);
1560 switch (type) {
1561 case KVM_S390_MCHK:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001562 atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001563 break;
1564 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001565 atomic_or(CPUSTAT_IO_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001566 break;
1567 default:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001568 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001569 break;
1570 }
1571 spin_unlock(&li->lock);
1572 kvm_s390_vcpu_wakeup(dst_vcpu);
1573}
1574
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001575static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001576{
Jens Freimann6d3da242013-07-03 15:18:35 +02001577 u64 type = READ_ONCE(inti->type);
1578 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001579
Jens Freimann6d3da242013-07-03 15:18:35 +02001580 switch (type) {
1581 case KVM_S390_MCHK:
1582 rc = __inject_float_mchk(kvm, inti);
1583 break;
1584 case KVM_S390_INT_VIRTIO:
1585 rc = __inject_virtio(kvm, inti);
1586 break;
1587 case KVM_S390_INT_SERVICE:
1588 rc = __inject_service(kvm, inti);
1589 break;
1590 case KVM_S390_INT_PFAULT_DONE:
1591 rc = __inject_pfault_done(kvm, inti);
1592 break;
1593 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1594 rc = __inject_io(kvm, inti);
1595 break;
1596 default:
1597 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001598 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001599 if (rc)
1600 return rc;
1601
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001602 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001603 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001604}
1605
1606int kvm_s390_inject_vm(struct kvm *kvm,
1607 struct kvm_s390_interrupt *s390int)
1608{
1609 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001610 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001611
1612 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1613 if (!inti)
1614 return -ENOMEM;
1615
1616 inti->type = s390int->type;
1617 switch (inti->type) {
1618 case KVM_S390_INT_VIRTIO:
1619 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1620 s390int->parm, s390int->parm64);
1621 inti->ext.ext_params = s390int->parm;
1622 inti->ext.ext_params2 = s390int->parm64;
1623 break;
1624 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001625 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001626 inti->ext.ext_params = s390int->parm;
1627 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001628 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001629 inti->ext.ext_params2 = s390int->parm64;
1630 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001631 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001632 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001633 s390int->parm64);
1634 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1635 inti->mchk.mcic = s390int->parm64;
1636 break;
1637 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimannc05c4182013-10-07 16:13:45 +02001638 inti->io.subchannel_id = s390int->parm >> 16;
1639 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1640 inti->io.io_int_parm = s390int->parm64 >> 32;
1641 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1642 break;
1643 default:
1644 kfree(inti);
1645 return -EINVAL;
1646 }
1647 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1648 2);
1649
David Hildenbrand428d53b2015-01-16 12:58:09 +01001650 rc = __inject_vm(kvm, inti);
1651 if (rc)
1652 kfree(inti);
1653 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001654}
1655
David Hildenbrand15462e32015-02-04 15:59:11 +01001656int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001657 struct kvm_s390_interrupt_info *inti)
1658{
David Hildenbrand15462e32015-02-04 15:59:11 +01001659 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001660}
1661
Jens Freimann383d0b02014-07-29 15:11:49 +02001662int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1663 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001664{
Jens Freimann383d0b02014-07-29 15:11:49 +02001665 irq->type = s390int->type;
1666 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001667 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001668 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001669 return -EINVAL;
1670 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001671 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001672 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001673 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001674 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001675 case KVM_S390_SIGP_STOP:
1676 irq->u.stop.flags = s390int->parm;
1677 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001678 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001679 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001680 return -EINVAL;
1681 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001682 break;
1683 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001684 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001685 return -EINVAL;
1686 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001687 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001688 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001689 irq->u.mchk.mcic = s390int->parm64;
1690 break;
1691 }
1692 return 0;
1693}
1694
David Hildenbrand6cddd432014-10-15 16:48:53 +02001695int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1696{
1697 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1698
1699 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1700}
1701
1702void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1703{
1704 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1705
1706 spin_lock(&li->lock);
1707 li->irq.stop.flags = 0;
1708 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1709 spin_unlock(&li->lock);
1710}
1711
Jens Freimann79e87a12015-03-19 15:12:12 +01001712static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001713{
Jens Freimann383d0b02014-07-29 15:11:49 +02001714 int rc;
1715
Jens Freimann383d0b02014-07-29 15:11:49 +02001716 switch (irq->type) {
1717 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001718 rc = __inject_prog(vcpu, irq);
1719 break;
1720 case KVM_S390_SIGP_SET_PREFIX:
1721 rc = __inject_set_prefix(vcpu, irq);
1722 break;
1723 case KVM_S390_SIGP_STOP:
1724 rc = __inject_sigp_stop(vcpu, irq);
1725 break;
1726 case KVM_S390_RESTART:
1727 rc = __inject_sigp_restart(vcpu, irq);
1728 break;
1729 case KVM_S390_INT_CLOCK_COMP:
1730 rc = __inject_ckc(vcpu);
1731 break;
1732 case KVM_S390_INT_CPU_TIMER:
1733 rc = __inject_cpu_timer(vcpu);
1734 break;
1735 case KVM_S390_INT_EXTERNAL_CALL:
1736 rc = __inject_extcall(vcpu, irq);
1737 break;
1738 case KVM_S390_INT_EMERGENCY:
1739 rc = __inject_sigp_emergency(vcpu, irq);
1740 break;
1741 case KVM_S390_MCHK:
1742 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001743 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001744 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001745 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001746 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001747 case KVM_S390_INT_VIRTIO:
1748 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001749 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001750 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001751 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001752 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001753
1754 return rc;
1755}
1756
1757int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1758{
1759 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1760 int rc;
1761
1762 spin_lock(&li->lock);
1763 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001764 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001765 if (!rc)
1766 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001767 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001768}
Jens Freimannc05c4182013-10-07 16:13:45 +02001769
Jens Freimann6d3da242013-07-03 15:18:35 +02001770static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001771{
Jens Freimann6d3da242013-07-03 15:18:35 +02001772 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001773
Jens Freimann6d3da242013-07-03 15:18:35 +02001774 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001775 list_del(&inti->list);
1776 kfree(inti);
1777 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001778}
1779
Jens Freimann94aa0332015-03-16 12:17:13 +01001780static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1781 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001782{
Jens Freimann94aa0332015-03-16 12:17:13 +01001783 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001784 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001785 case KVM_S390_INT_PFAULT_INIT:
1786 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001787 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001788 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001789 break;
1790 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001791 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001792 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001793 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001794}
1795
Jens Freimann6d3da242013-07-03 15:18:35 +02001796void kvm_s390_clear_float_irqs(struct kvm *kvm)
1797{
1798 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1799 int i;
1800
1801 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001802 fi->pending_irqs = 0;
1803 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1804 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001805 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1806 clear_irq_list(&fi->lists[i]);
1807 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1808 fi->counters[i] = 0;
1809 spin_unlock(&fi->lock);
1810};
1811
Jens Freimann94aa0332015-03-16 12:17:13 +01001812static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001813{
1814 struct kvm_s390_interrupt_info *inti;
1815 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001816 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001817 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001818 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001819 int ret = 0;
1820 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001821 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001822
Jens Freimann94aa0332015-03-16 12:17:13 +01001823 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1824 return -EINVAL;
1825
1826 /*
1827 * We are already using -ENOMEM to signal
1828 * userspace it may retry with a bigger buffer,
1829 * so we need to use something else for this case
1830 */
1831 buf = vzalloc(len);
1832 if (!buf)
1833 return -ENOBUFS;
1834
1835 max_irqs = len / sizeof(struct kvm_s390_irq);
1836
Jens Freimannc05c4182013-10-07 16:13:45 +02001837 fi = &kvm->arch.float_int;
1838 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001839 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1840 list_for_each_entry(inti, &fi->lists[i], list) {
1841 if (n == max_irqs) {
1842 /* signal userspace to try again */
1843 ret = -ENOMEM;
1844 goto out;
1845 }
1846 inti_to_irq(inti, &buf[n]);
1847 n++;
1848 }
1849 }
1850 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001851 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001852 /* signal userspace to try again */
1853 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001854 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001855 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001856 irq = (struct kvm_s390_irq *) &buf[n];
1857 irq->type = KVM_S390_INT_SERVICE;
1858 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001859 n++;
1860 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001861 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1862 if (n == max_irqs) {
1863 /* signal userspace to try again */
1864 ret = -ENOMEM;
1865 goto out;
1866 }
1867 irq = (struct kvm_s390_irq *) &buf[n];
1868 irq->type = KVM_S390_MCHK;
1869 irq->u.mchk = fi->mchk;
1870 n++;
1871}
1872
1873out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001874 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001875 if (!ret && n > 0) {
1876 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1877 ret = -EFAULT;
1878 }
1879 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001880
1881 return ret < 0 ? ret : n;
1882}
1883
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08001884static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr)
1885{
1886 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1887 struct kvm_s390_ais_all ais;
1888
1889 if (attr->attr < sizeof(ais))
1890 return -EINVAL;
1891
1892 if (!test_kvm_facility(kvm, 72))
1893 return -ENOTSUPP;
1894
1895 mutex_lock(&fi->ais_lock);
1896 ais.simm = fi->simm;
1897 ais.nimm = fi->nimm;
1898 mutex_unlock(&fi->ais_lock);
1899
1900 if (copy_to_user((void __user *)attr->addr, &ais, sizeof(ais)))
1901 return -EFAULT;
1902
1903 return 0;
1904}
1905
Jens Freimannc05c4182013-10-07 16:13:45 +02001906static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1907{
1908 int r;
1909
1910 switch (attr->group) {
1911 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001912 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001913 attr->attr);
1914 break;
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08001915 case KVM_DEV_FLIC_AISM_ALL:
1916 r = flic_ais_mode_get_all(dev->kvm, attr);
1917 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001918 default:
1919 r = -EINVAL;
1920 }
1921
1922 return r;
1923}
1924
1925static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1926 u64 addr)
1927{
1928 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1929 void *target = NULL;
1930 void __user *source;
1931 u64 size;
1932
1933 if (get_user(inti->type, (u64 __user *)addr))
1934 return -EFAULT;
1935
1936 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001937 case KVM_S390_INT_PFAULT_INIT:
1938 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001939 case KVM_S390_INT_VIRTIO:
1940 case KVM_S390_INT_SERVICE:
1941 target = (void *) &inti->ext;
1942 source = &uptr->u.ext;
1943 size = sizeof(inti->ext);
1944 break;
1945 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1946 target = (void *) &inti->io;
1947 source = &uptr->u.io;
1948 size = sizeof(inti->io);
1949 break;
1950 case KVM_S390_MCHK:
1951 target = (void *) &inti->mchk;
1952 source = &uptr->u.mchk;
1953 size = sizeof(inti->mchk);
1954 break;
1955 default:
1956 return -EINVAL;
1957 }
1958
1959 if (copy_from_user(target, source, size))
1960 return -EFAULT;
1961
1962 return 0;
1963}
1964
1965static int enqueue_floating_irq(struct kvm_device *dev,
1966 struct kvm_device_attr *attr)
1967{
1968 struct kvm_s390_interrupt_info *inti = NULL;
1969 int r = 0;
1970 int len = attr->attr;
1971
1972 if (len % sizeof(struct kvm_s390_irq) != 0)
1973 return -EINVAL;
1974 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1975 return -EINVAL;
1976
1977 while (len >= sizeof(struct kvm_s390_irq)) {
1978 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1979 if (!inti)
1980 return -ENOMEM;
1981
1982 r = copy_irq_from_user(inti, attr->addr);
1983 if (r) {
1984 kfree(inti);
1985 return r;
1986 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001987 r = __inject_vm(dev->kvm, inti);
1988 if (r) {
1989 kfree(inti);
1990 return r;
1991 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001992 len -= sizeof(struct kvm_s390_irq);
1993 attr->addr += sizeof(struct kvm_s390_irq);
1994 }
1995
1996 return r;
1997}
1998
Cornelia Huck841b91c2013-07-15 13:36:01 +02001999static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
2000{
2001 if (id >= MAX_S390_IO_ADAPTERS)
2002 return NULL;
2003 return kvm->arch.adapters[id];
2004}
2005
2006static int register_io_adapter(struct kvm_device *dev,
2007 struct kvm_device_attr *attr)
2008{
2009 struct s390_io_adapter *adapter;
2010 struct kvm_s390_io_adapter adapter_info;
2011
2012 if (copy_from_user(&adapter_info,
2013 (void __user *)attr->addr, sizeof(adapter_info)))
2014 return -EFAULT;
2015
2016 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
2017 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
2018 return -EINVAL;
2019
2020 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
2021 if (!adapter)
2022 return -ENOMEM;
2023
2024 INIT_LIST_HEAD(&adapter->maps);
2025 init_rwsem(&adapter->maps_lock);
2026 atomic_set(&adapter->nr_maps, 0);
2027 adapter->id = adapter_info.id;
2028 adapter->isc = adapter_info.isc;
2029 adapter->maskable = adapter_info.maskable;
2030 adapter->masked = false;
2031 adapter->swap = adapter_info.swap;
Fei Li08fab502017-01-19 17:02:26 +01002032 adapter->suppressible = (adapter_info.flags) &
2033 KVM_S390_ADAPTER_SUPPRESSIBLE;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002034 dev->kvm->arch.adapters[adapter->id] = adapter;
2035
2036 return 0;
2037}
2038
2039int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
2040{
2041 int ret;
2042 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2043
2044 if (!adapter || !adapter->maskable)
2045 return -EINVAL;
2046 ret = adapter->masked;
2047 adapter->masked = masked;
2048 return ret;
2049}
2050
2051static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
2052{
2053 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2054 struct s390_map_info *map;
2055 int ret;
2056
2057 if (!adapter || !addr)
2058 return -EINVAL;
2059
2060 map = kzalloc(sizeof(*map), GFP_KERNEL);
2061 if (!map) {
2062 ret = -ENOMEM;
2063 goto out;
2064 }
2065 INIT_LIST_HEAD(&map->list);
2066 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02002067 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02002068 if (map->addr == -EFAULT) {
2069 ret = -EFAULT;
2070 goto out;
2071 }
2072 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
2073 if (ret < 0)
2074 goto out;
2075 BUG_ON(ret != 1);
2076 down_write(&adapter->maps_lock);
2077 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2078 list_add_tail(&map->list, &adapter->maps);
2079 ret = 0;
2080 } else {
2081 put_page(map->page);
2082 ret = -EINVAL;
2083 }
2084 up_write(&adapter->maps_lock);
2085out:
2086 if (ret)
2087 kfree(map);
2088 return ret;
2089}
2090
2091static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2092{
2093 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2094 struct s390_map_info *map, *tmp;
2095 int found = 0;
2096
2097 if (!adapter || !addr)
2098 return -EINVAL;
2099
2100 down_write(&adapter->maps_lock);
2101 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2102 if (map->guest_addr == addr) {
2103 found = 1;
2104 atomic_dec(&adapter->nr_maps);
2105 list_del(&map->list);
2106 put_page(map->page);
2107 kfree(map);
2108 break;
2109 }
2110 }
2111 up_write(&adapter->maps_lock);
2112
2113 return found ? 0 : -EINVAL;
2114}
2115
2116void kvm_s390_destroy_adapters(struct kvm *kvm)
2117{
2118 int i;
2119 struct s390_map_info *map, *tmp;
2120
2121 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2122 if (!kvm->arch.adapters[i])
2123 continue;
2124 list_for_each_entry_safe(map, tmp,
2125 &kvm->arch.adapters[i]->maps, list) {
2126 list_del(&map->list);
2127 put_page(map->page);
2128 kfree(map);
2129 }
2130 kfree(kvm->arch.adapters[i]);
2131 }
2132}
2133
2134static int modify_io_adapter(struct kvm_device *dev,
2135 struct kvm_device_attr *attr)
2136{
2137 struct kvm_s390_io_adapter_req req;
2138 struct s390_io_adapter *adapter;
2139 int ret;
2140
2141 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2142 return -EFAULT;
2143
2144 adapter = get_io_adapter(dev->kvm, req.id);
2145 if (!adapter)
2146 return -EINVAL;
2147 switch (req.type) {
2148 case KVM_S390_IO_ADAPTER_MASK:
2149 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2150 if (ret > 0)
2151 ret = 0;
2152 break;
2153 case KVM_S390_IO_ADAPTER_MAP:
2154 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2155 break;
2156 case KVM_S390_IO_ADAPTER_UNMAP:
2157 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2158 break;
2159 default:
2160 ret = -EINVAL;
2161 }
2162
2163 return ret;
2164}
2165
Halil Pasic6d28f782016-01-25 19:10:40 +01002166static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2167
2168{
2169 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2170 u32 schid;
2171
2172 if (attr->flags)
2173 return -EINVAL;
2174 if (attr->attr != sizeof(schid))
2175 return -EINVAL;
2176 if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2177 return -EFAULT;
2178 kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2179 /*
2180 * If userspace is conforming to the architecture, we can have at most
2181 * one pending I/O interrupt per subchannel, so this is effectively a
2182 * clear all.
2183 */
2184 return 0;
2185}
2186
Fei Li51978392017-02-17 17:06:26 +08002187static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
2188{
2189 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2190 struct kvm_s390_ais_req req;
2191 int ret = 0;
2192
Christian Borntraeger1ba15b22017-05-31 10:18:55 +02002193 if (!test_kvm_facility(kvm, 72))
Fei Li51978392017-02-17 17:06:26 +08002194 return -ENOTSUPP;
2195
2196 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2197 return -EFAULT;
2198
2199 if (req.isc > MAX_ISC)
2200 return -EINVAL;
2201
2202 trace_kvm_s390_modify_ais_mode(req.isc,
2203 (fi->simm & AIS_MODE_MASK(req.isc)) ?
2204 (fi->nimm & AIS_MODE_MASK(req.isc)) ?
2205 2 : KVM_S390_AIS_MODE_SINGLE :
2206 KVM_S390_AIS_MODE_ALL, req.mode);
2207
2208 mutex_lock(&fi->ais_lock);
2209 switch (req.mode) {
2210 case KVM_S390_AIS_MODE_ALL:
2211 fi->simm &= ~AIS_MODE_MASK(req.isc);
2212 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2213 break;
2214 case KVM_S390_AIS_MODE_SINGLE:
2215 fi->simm |= AIS_MODE_MASK(req.isc);
2216 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2217 break;
2218 default:
2219 ret = -EINVAL;
2220 }
2221 mutex_unlock(&fi->ais_lock);
2222
2223 return ret;
2224}
2225
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002226static int kvm_s390_inject_airq(struct kvm *kvm,
2227 struct s390_io_adapter *adapter)
2228{
2229 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2230 struct kvm_s390_interrupt s390int = {
2231 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2232 .parm = 0,
2233 .parm64 = (adapter->isc << 27) | 0x80000000,
2234 };
2235 int ret = 0;
2236
Christian Borntraeger1ba15b22017-05-31 10:18:55 +02002237 if (!test_kvm_facility(kvm, 72) || !adapter->suppressible)
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002238 return kvm_s390_inject_vm(kvm, &s390int);
2239
2240 mutex_lock(&fi->ais_lock);
2241 if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
2242 trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
2243 goto out;
2244 }
2245
2246 ret = kvm_s390_inject_vm(kvm, &s390int);
2247 if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
2248 fi->nimm |= AIS_MODE_MASK(adapter->isc);
2249 trace_kvm_s390_modify_ais_mode(adapter->isc,
2250 KVM_S390_AIS_MODE_SINGLE, 2);
2251 }
2252out:
2253 mutex_unlock(&fi->ais_lock);
2254 return ret;
2255}
2256
2257static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr)
2258{
2259 unsigned int id = attr->attr;
2260 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2261
2262 if (!adapter)
2263 return -EINVAL;
2264
2265 return kvm_s390_inject_airq(kvm, adapter);
2266}
2267
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002268static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr)
2269{
2270 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2271 struct kvm_s390_ais_all ais;
2272
2273 if (!test_kvm_facility(kvm, 72))
2274 return -ENOTSUPP;
2275
2276 if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais)))
2277 return -EFAULT;
2278
2279 mutex_lock(&fi->ais_lock);
2280 fi->simm = ais.simm;
2281 fi->nimm = ais.nimm;
2282 mutex_unlock(&fi->ais_lock);
2283
2284 return 0;
2285}
2286
Jens Freimannc05c4182013-10-07 16:13:45 +02002287static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2288{
2289 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002290 unsigned int i;
2291 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02002292
2293 switch (attr->group) {
2294 case KVM_DEV_FLIC_ENQUEUE:
2295 r = enqueue_floating_irq(dev, attr);
2296 break;
2297 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01002298 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02002299 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002300 case KVM_DEV_FLIC_APF_ENABLE:
2301 dev->kvm->arch.gmap->pfault_enabled = 1;
2302 break;
2303 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2304 dev->kvm->arch.gmap->pfault_enabled = 0;
2305 /*
2306 * Make sure no async faults are in transition when
2307 * clearing the queues. So we don't need to worry
2308 * about late coming workers.
2309 */
2310 synchronize_srcu(&dev->kvm->srcu);
2311 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2312 kvm_clear_async_pf_completion_queue(vcpu);
2313 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002314 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2315 r = register_io_adapter(dev, attr);
2316 break;
2317 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2318 r = modify_io_adapter(dev, attr);
2319 break;
Halil Pasic6d28f782016-01-25 19:10:40 +01002320 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2321 r = clear_io_irq(dev->kvm, attr);
2322 break;
Fei Li51978392017-02-17 17:06:26 +08002323 case KVM_DEV_FLIC_AISM:
2324 r = modify_ais_mode(dev->kvm, attr);
2325 break;
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002326 case KVM_DEV_FLIC_AIRQ_INJECT:
2327 r = flic_inject_airq(dev->kvm, attr);
2328 break;
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002329 case KVM_DEV_FLIC_AISM_ALL:
2330 r = flic_ais_mode_set_all(dev->kvm, attr);
2331 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002332 default:
2333 r = -EINVAL;
2334 }
2335
2336 return r;
2337}
2338
Halil Pasic4f129852016-02-25 12:44:17 +01002339static int flic_has_attr(struct kvm_device *dev,
2340 struct kvm_device_attr *attr)
2341{
2342 switch (attr->group) {
2343 case KVM_DEV_FLIC_GET_ALL_IRQS:
2344 case KVM_DEV_FLIC_ENQUEUE:
2345 case KVM_DEV_FLIC_CLEAR_IRQS:
2346 case KVM_DEV_FLIC_APF_ENABLE:
2347 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2348 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2349 case KVM_DEV_FLIC_ADAPTER_MODIFY:
Halil Pasic6d28f782016-01-25 19:10:40 +01002350 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
Fei Li51978392017-02-17 17:06:26 +08002351 case KVM_DEV_FLIC_AISM:
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002352 case KVM_DEV_FLIC_AIRQ_INJECT:
Yi Min Zhao2c1a48f2017-06-07 16:09:52 +08002353 case KVM_DEV_FLIC_AISM_ALL:
Halil Pasic4f129852016-02-25 12:44:17 +01002354 return 0;
2355 }
2356 return -ENXIO;
2357}
2358
Jens Freimannc05c4182013-10-07 16:13:45 +02002359static int flic_create(struct kvm_device *dev, u32 type)
2360{
2361 if (!dev)
2362 return -EINVAL;
2363 if (dev->kvm->arch.flic)
2364 return -EINVAL;
2365 dev->kvm->arch.flic = dev;
2366 return 0;
2367}
2368
2369static void flic_destroy(struct kvm_device *dev)
2370{
2371 dev->kvm->arch.flic = NULL;
2372 kfree(dev);
2373}
2374
2375/* s390 floating irq controller (flic) */
2376struct kvm_device_ops kvm_flic_ops = {
2377 .name = "kvm-flic",
2378 .get_attr = flic_get_attr,
2379 .set_attr = flic_set_attr,
Halil Pasic4f129852016-02-25 12:44:17 +01002380 .has_attr = flic_has_attr,
Jens Freimannc05c4182013-10-07 16:13:45 +02002381 .create = flic_create,
2382 .destroy = flic_destroy,
2383};
Cornelia Huck84223592013-07-15 13:36:01 +02002384
2385static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2386{
2387 unsigned long bit;
2388
2389 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2390
2391 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2392}
2393
2394static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2395 u64 addr)
2396{
2397 struct s390_map_info *map;
2398
2399 if (!adapter)
2400 return NULL;
2401
2402 list_for_each_entry(map, &adapter->maps, list) {
2403 if (map->guest_addr == addr)
2404 return map;
2405 }
2406 return NULL;
2407}
2408
2409static int adapter_indicators_set(struct kvm *kvm,
2410 struct s390_io_adapter *adapter,
2411 struct kvm_s390_adapter_int *adapter_int)
2412{
2413 unsigned long bit;
2414 int summary_set, idx;
2415 struct s390_map_info *info;
2416 void *map;
2417
2418 info = get_map_info(adapter, adapter_int->ind_addr);
2419 if (!info)
2420 return -1;
2421 map = page_address(info->page);
2422 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2423 set_bit(bit, map);
2424 idx = srcu_read_lock(&kvm->srcu);
2425 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2426 set_page_dirty_lock(info->page);
2427 info = get_map_info(adapter, adapter_int->summary_addr);
2428 if (!info) {
2429 srcu_read_unlock(&kvm->srcu, idx);
2430 return -1;
2431 }
2432 map = page_address(info->page);
2433 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2434 adapter->swap);
2435 summary_set = test_and_set_bit(bit, map);
2436 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2437 set_page_dirty_lock(info->page);
2438 srcu_read_unlock(&kvm->srcu, idx);
2439 return summary_set ? 0 : 1;
2440}
2441
2442/*
2443 * < 0 - not injected due to error
2444 * = 0 - coalesced, summary indicator already active
2445 * > 0 - injected interrupt
2446 */
2447static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2448 struct kvm *kvm, int irq_source_id, int level,
2449 bool line_status)
2450{
2451 int ret;
2452 struct s390_io_adapter *adapter;
2453
2454 /* We're only interested in the 0->1 transition. */
2455 if (!level)
2456 return 0;
2457 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2458 if (!adapter)
2459 return -1;
2460 down_read(&adapter->maps_lock);
2461 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2462 up_read(&adapter->maps_lock);
2463 if ((ret > 0) && !adapter->masked) {
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002464 ret = kvm_s390_inject_airq(kvm, adapter);
Cornelia Huck84223592013-07-15 13:36:01 +02002465 if (ret == 0)
2466 ret = 1;
2467 }
2468 return ret;
2469}
2470
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002471/*
2472 * Inject the machine check to the guest.
2473 */
2474void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
2475 struct mcck_volatile_info *mcck_info)
2476{
2477 struct kvm_s390_interrupt_info inti;
2478 struct kvm_s390_irq irq;
2479 struct kvm_s390_mchk_info *mchk;
2480 union mci mci;
2481 __u64 cr14 = 0; /* upper bits are not used */
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002482 int rc;
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002483
2484 mci.val = mcck_info->mcic;
2485 if (mci.sr)
2486 cr14 |= MCCK_CR14_RECOVERY_SUB_MASK;
2487 if (mci.dg)
2488 cr14 |= MCCK_CR14_DEGRAD_SUB_MASK;
2489 if (mci.w)
2490 cr14 |= MCCK_CR14_WARN_SUB_MASK;
2491
2492 mchk = mci.ck ? &inti.mchk : &irq.u.mchk;
2493 mchk->cr14 = cr14;
2494 mchk->mcic = mcck_info->mcic;
2495 mchk->ext_damage_code = mcck_info->ext_damage_code;
2496 mchk->failing_storage_address = mcck_info->failing_storage_address;
2497 if (mci.ck) {
2498 /* Inject the floating machine check */
2499 inti.type = KVM_S390_MCHK;
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002500 rc = __inject_vm(vcpu->kvm, &inti);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002501 } else {
2502 /* Inject the machine check to specified vcpu */
2503 irq.type = KVM_S390_MCHK;
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002504 rc = kvm_s390_inject_vcpu(vcpu, &irq);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002505 }
David Hildenbrand3dbf0202017-08-30 18:06:01 +02002506 WARN_ON_ONCE(rc);
QingFeng Hao4d62fcc2017-06-07 12:03:05 +02002507}
2508
Radim Krčmářc63cf532016-07-12 22:09:26 +02002509int kvm_set_routing_entry(struct kvm *kvm,
2510 struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002511 const struct kvm_irq_routing_entry *ue)
2512{
2513 int ret;
2514
2515 switch (ue->type) {
2516 case KVM_IRQ_ROUTING_S390_ADAPTER:
2517 e->set = set_adapter_int;
2518 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2519 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2520 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2521 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2522 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2523 ret = 0;
2524 break;
2525 default:
2526 ret = -EINVAL;
2527 }
2528
2529 return ret;
2530}
2531
2532int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2533 int irq_source_id, int level, bool line_status)
2534{
2535 return -EINVAL;
2536}
Jens Freimann816c7662014-11-24 17:13:46 +01002537
2538int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2539{
2540 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2541 struct kvm_s390_irq *buf;
2542 int r = 0;
2543 int n;
2544
2545 buf = vmalloc(len);
2546 if (!buf)
2547 return -ENOMEM;
2548
2549 if (copy_from_user((void *) buf, irqstate, len)) {
2550 r = -EFAULT;
2551 goto out_free;
2552 }
2553
2554 /*
2555 * Don't allow setting the interrupt state
2556 * when there are already interrupts pending
2557 */
2558 spin_lock(&li->lock);
2559 if (li->pending_irqs) {
2560 r = -EBUSY;
2561 goto out_unlock;
2562 }
2563
2564 for (n = 0; n < len / sizeof(*buf); n++) {
2565 r = do_inject_vcpu(vcpu, &buf[n]);
2566 if (r)
2567 break;
2568 }
2569
2570out_unlock:
2571 spin_unlock(&li->lock);
2572out_free:
2573 vfree(buf);
2574
2575 return r;
2576}
2577
2578static void store_local_irq(struct kvm_s390_local_interrupt *li,
2579 struct kvm_s390_irq *irq,
2580 unsigned long irq_type)
2581{
2582 switch (irq_type) {
2583 case IRQ_PEND_MCHK_EX:
2584 case IRQ_PEND_MCHK_REP:
2585 irq->type = KVM_S390_MCHK;
2586 irq->u.mchk = li->irq.mchk;
2587 break;
2588 case IRQ_PEND_PROG:
2589 irq->type = KVM_S390_PROGRAM_INT;
2590 irq->u.pgm = li->irq.pgm;
2591 break;
2592 case IRQ_PEND_PFAULT_INIT:
2593 irq->type = KVM_S390_INT_PFAULT_INIT;
2594 irq->u.ext = li->irq.ext;
2595 break;
2596 case IRQ_PEND_EXT_EXTERNAL:
2597 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2598 irq->u.extcall = li->irq.extcall;
2599 break;
2600 case IRQ_PEND_EXT_CLOCK_COMP:
2601 irq->type = KVM_S390_INT_CLOCK_COMP;
2602 break;
2603 case IRQ_PEND_EXT_CPU_TIMER:
2604 irq->type = KVM_S390_INT_CPU_TIMER;
2605 break;
2606 case IRQ_PEND_SIGP_STOP:
2607 irq->type = KVM_S390_SIGP_STOP;
2608 irq->u.stop = li->irq.stop;
2609 break;
2610 case IRQ_PEND_RESTART:
2611 irq->type = KVM_S390_RESTART;
2612 break;
2613 case IRQ_PEND_SET_PREFIX:
2614 irq->type = KVM_S390_SIGP_SET_PREFIX;
2615 irq->u.prefix = li->irq.prefix;
2616 break;
2617 }
2618}
2619
2620int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2621{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002622 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002623 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2624 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2625 unsigned long pending_irqs;
2626 struct kvm_s390_irq irq;
2627 unsigned long irq_type;
2628 int cpuaddr;
2629 int n = 0;
2630
2631 spin_lock(&li->lock);
2632 pending_irqs = li->pending_irqs;
2633 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2634 sizeof(sigp_emerg_pending));
2635 spin_unlock(&li->lock);
2636
2637 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2638 memset(&irq, 0, sizeof(irq));
2639 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2640 continue;
2641 if (n + sizeof(irq) > len)
2642 return -ENOBUFS;
2643 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2644 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2645 return -EFAULT;
2646 n += sizeof(irq);
2647 }
2648
2649 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2650 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2651 memset(&irq, 0, sizeof(irq));
2652 if (n + sizeof(irq) > len)
2653 return -ENOBUFS;
2654 irq.type = KVM_S390_INT_EMERGENCY;
2655 irq.u.emerg.code = cpuaddr;
2656 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2657 return -EFAULT;
2658 n += sizeof(irq);
2659 }
2660 }
2661
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002662 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002663 if (n + sizeof(irq) > len)
2664 return -ENOBUFS;
2665 memset(&irq, 0, sizeof(irq));
2666 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002667 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002668 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2669 return -EFAULT;
2670 n += sizeof(irq);
2671 }
2672
2673 return n;
2674}