blob: caf15c8a8948e76c1bf7cfb5d5a109df6cee9b3e [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;
Jens Freimann6d3da242013-07-03 15:18:35 +0200254 if (!(vcpu->arch.sie_block->gcr[14] &
255 vcpu->kvm->arch.float_int.mchk.cr14))
256 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200257
David Hildenbrand6cddd432014-10-15 16:48:53 +0200258 /*
259 * STOP irqs will never be actively delivered. They are triggered via
260 * intercept requests and cleared when the stop intercept is performed.
261 */
262 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
263
Jens Freimann383d0b02014-07-29 15:11:49 +0200264 return active_mask;
265}
266
Carsten Otteba5c1e92008-03-25 18:47:26 +0100267static void __set_cpu_idle(struct kvm_vcpu *vcpu)
268{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200269 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100270 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
271}
272
273static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
274{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200275 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100276 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
277}
278
279static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
280{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200281 atomic_andnot(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
282 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100283 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100284 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
285
286 if (guestdbg_enabled(vcpu)) {
287 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
288 LCTL_CR10 | LCTL_CR11);
289 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
290 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100291}
292
293static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
294{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200295 atomic_or(flag, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100296}
297
Jens Freimann6d3da242013-07-03 15:18:35 +0200298static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
299{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200300 if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
Jens Freimann6d3da242013-07-03 15:18:35 +0200301 return;
302 else if (psw_ioint_disabled(vcpu))
303 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
304 else
305 vcpu->arch.sie_block->lctl |= LCTL_CR6;
306}
307
Jens Freimann383d0b02014-07-29 15:11:49 +0200308static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
309{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200310 if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200311 return;
312 if (psw_extint_disabled(vcpu))
313 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
314 else
315 vcpu->arch.sie_block->lctl |= LCTL_CR0;
316}
317
318static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
319{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200320 if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200321 return;
322 if (psw_mchk_disabled(vcpu))
323 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
324 else
325 vcpu->arch.sie_block->lctl |= LCTL_CR14;
326}
327
David Hildenbrand6cddd432014-10-15 16:48:53 +0200328static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
329{
330 if (kvm_s390_is_stop_irq_pending(vcpu))
331 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
332}
333
Jens Freimann6d3da242013-07-03 15:18:35 +0200334/* Set interception request for non-deliverable interrupts */
335static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200336{
Jens Freimann6d3da242013-07-03 15:18:35 +0200337 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200338 set_intercept_indicators_ext(vcpu);
339 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200340 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200341}
342
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200343static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100344{
Jens Freimann383d0b02014-07-29 15:11:49 +0200345 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200346 int rc;
347
348 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
349 0, 0);
350
351 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
352 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100353 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200354 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
355 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
356 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
357 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200358 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100359 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200360}
361
362static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
363{
Jens Freimann383d0b02014-07-29 15:11:49 +0200364 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200365 int rc;
366
367 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
368 0, 0);
369
370 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
371 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100372 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200373 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
374 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
375 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
376 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200377 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100378 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200379}
380
Jens Freimann383d0b02014-07-29 15:11:49 +0200381static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200382{
Jens Freimann383d0b02014-07-29 15:11:49 +0200383 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
384 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200385 int rc;
386
Jens Freimann383d0b02014-07-29 15:11:49 +0200387 spin_lock(&li->lock);
388 ext = li->irq.ext;
389 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
390 li->irq.ext.ext_params2 = 0;
391 spin_unlock(&li->lock);
392
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200393 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
394 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200395 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
396 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200397 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200398
399 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
400 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
401 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
402 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
403 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
404 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200405 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100406 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200407}
408
David Hildenbrandd6404de2015-10-14 16:47:36 +0200409static int __write_machine_check(struct kvm_vcpu *vcpu,
410 struct kvm_s390_mchk_info *mchk)
411{
412 unsigned long ext_sa_addr;
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100413 unsigned long lc;
David Hildenbrand0319dae2016-08-03 11:18:57 +0200414 freg_t fprs[NUM_FPRS];
David Hildenbrandff5dc142015-10-14 16:57:56 +0200415 union mci mci;
David Hildenbrandd6404de2015-10-14 16:47:36 +0200416 int rc;
417
David Hildenbrandff5dc142015-10-14 16:57:56 +0200418 mci.val = mchk->mcic;
Christian Borntraeger31d8b8d2016-11-10 14:22:02 +0100419 /* take care of lazy register loading */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200420 save_fpu_regs();
421 save_access_regs(vcpu->run->s.regs.acrs);
Christian Borntraeger80248552017-04-12 12:59:59 +0200422 if (MACHINE_HAS_GS && vcpu->arch.gs_enabled)
423 save_gs_cb(current->thread.gs_cb);
David Hildenbrand0319dae2016-08-03 11:18:57 +0200424
David Hildenbrandd6404de2015-10-14 16:47:36 +0200425 /* Extended save area */
Martin Schwidefsky916cda12016-01-26 14:10:34 +0100426 rc = read_guest_lc(vcpu, __LC_MCESAD, &ext_sa_addr,
427 sizeof(unsigned long));
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100428 /* Only bits 0 through 63-LC are used for address formation */
429 lc = ext_sa_addr & MCESA_LC_MASK;
430 if (test_kvm_facility(vcpu->kvm, 133)) {
431 switch (lc) {
432 case 0:
433 case 10:
434 ext_sa_addr &= ~0x3ffUL;
435 break;
436 case 11:
437 ext_sa_addr &= ~0x7ffUL;
438 break;
439 case 12:
440 ext_sa_addr &= ~0xfffUL;
441 break;
442 default:
443 ext_sa_addr = 0;
444 break;
445 }
446 } else {
447 ext_sa_addr &= ~0x3ffUL;
448 }
449
David Hildenbrandff5dc142015-10-14 16:57:56 +0200450 if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) {
451 if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs,
452 512))
453 mci.vr = 0;
454 } else {
455 mci.vr = 0;
456 }
Fan Zhang4e0b1ab2016-11-29 07:17:55 +0100457 if (!rc && mci.gs && ext_sa_addr && test_kvm_facility(vcpu->kvm, 133)
458 && (lc == 11 || lc == 12)) {
459 if (write_guest_abs(vcpu, ext_sa_addr + 1024,
460 &vcpu->run->s.regs.gscb, 32))
461 mci.gs = 0;
462 } else {
463 mci.gs = 0;
464 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200465
466 /* General interruption information */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200467 rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200468 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
469 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
470 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
471 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
David Hildenbrandff5dc142015-10-14 16:57:56 +0200472 rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200473
474 /* Register-save areas */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200475 if (MACHINE_HAS_VX) {
476 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
477 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128);
478 } else {
479 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA,
480 vcpu->run->s.regs.fprs, 128);
481 }
482 rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA,
483 vcpu->run->s.regs.gprs, 128);
484 rc |= put_guest_lc(vcpu, current->thread.fpu.fpc,
485 (u32 __user *) __LC_FP_CREG_SAVE_AREA);
486 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr,
487 (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA);
488 rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu),
489 (u64 __user *) __LC_CPU_TIMER_SAVE_AREA);
490 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8,
491 (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA);
492 rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA,
493 &vcpu->run->s.regs.acrs, 64);
494 rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA,
495 &vcpu->arch.sie_block->gcr, 128);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200496
497 /* Extended interruption information */
David Hildenbrand8953fb02016-08-03 12:25:08 +0200498 rc |= put_guest_lc(vcpu, mchk->ext_damage_code,
499 (u32 __user *) __LC_EXT_DAMAGE_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200500 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
501 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
502 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
503 sizeof(mchk->fixed_logout));
504 return rc ? -EFAULT : 0;
505}
506
Jens Freimann383d0b02014-07-29 15:11:49 +0200507static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200508{
Jens Freimann6d3da242013-07-03 15:18:35 +0200509 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200510 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200511 struct kvm_s390_mchk_info mchk = {};
Jens Freimann6d3da242013-07-03 15:18:35 +0200512 int deliver = 0;
513 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200514
Jens Freimann6d3da242013-07-03 15:18:35 +0200515 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200516 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200517 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
518 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
519 /*
520 * If there was an exigent machine check pending, then any
521 * repressible machine checks that might have been pending
522 * are indicated along with it, so always clear bits for
523 * repressible and exigent interrupts
524 */
525 mchk = li->irq.mchk;
526 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
527 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
528 memset(&li->irq.mchk, 0, sizeof(mchk));
529 deliver = 1;
530 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200531 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200532 * We indicate floating repressible conditions along with
533 * other pending conditions. Channel Report Pending and Channel
534 * Subsystem damage are the only two and and are indicated by
535 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200536 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200537 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
538 mchk.mcic |= fi->mchk.mcic;
539 mchk.cr14 |= fi->mchk.cr14;
540 memset(&fi->mchk, 0, sizeof(mchk));
541 deliver = 1;
542 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200543 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200544 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200545
Jens Freimann6d3da242013-07-03 15:18:35 +0200546 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200547 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200548 mchk.mcic);
549 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
550 KVM_S390_MCHK,
551 mchk.cr14, mchk.mcic);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200552 rc = __write_machine_check(vcpu, &mchk);
Jens Freimann6d3da242013-07-03 15:18:35 +0200553 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200554 return rc;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200555}
556
557static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
558{
Jens Freimann383d0b02014-07-29 15:11:49 +0200559 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200560 int rc;
561
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200562 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200563 vcpu->stat.deliver_restart_signal++;
564 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
565
566 rc = write_guest_lc(vcpu,
Heiko Carstensc667aea2015-12-31 10:29:00 +0100567 offsetof(struct lowcore, restart_old_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200568 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstensc667aea2015-12-31 10:29:00 +0100569 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200570 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200571 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100572 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200573}
574
Jens Freimann383d0b02014-07-29 15:11:49 +0200575static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200576{
Jens Freimann383d0b02014-07-29 15:11:49 +0200577 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
578 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200579
Jens Freimann383d0b02014-07-29 15:11:49 +0200580 spin_lock(&li->lock);
581 prefix = li->irq.prefix;
582 li->irq.prefix.address = 0;
583 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
584 spin_unlock(&li->lock);
585
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200586 vcpu->stat.deliver_prefix_signal++;
587 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
588 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200589 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200590
Jens Freimann383d0b02014-07-29 15:11:49 +0200591 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200592 return 0;
593}
594
Jens Freimann383d0b02014-07-29 15:11:49 +0200595static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200596{
Jens Freimann383d0b02014-07-29 15:11:49 +0200597 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200598 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200599 int cpu_addr;
600
601 spin_lock(&li->lock);
602 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
603 clear_bit(cpu_addr, li->sigp_emerg_pending);
604 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
605 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
606 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200607
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200608 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200609 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200610 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
611 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200612
613 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
614 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200615 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200616 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
617 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
618 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
619 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100620 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200621}
622
Jens Freimann383d0b02014-07-29 15:11:49 +0200623static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200624{
Jens Freimann383d0b02014-07-29 15:11:49 +0200625 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
626 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200627 int rc;
628
Jens Freimann383d0b02014-07-29 15:11:49 +0200629 spin_lock(&li->lock);
630 extcall = li->irq.extcall;
631 li->irq.extcall.code = 0;
632 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
633 spin_unlock(&li->lock);
634
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200635 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200636 vcpu->stat.deliver_external_call++;
637 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
638 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200639 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200640
641 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
642 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200643 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200644 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
645 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
646 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
647 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100648 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200649}
650
Jens Freimann383d0b02014-07-29 15:11:49 +0200651static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200652{
Jens Freimann383d0b02014-07-29 15:11:49 +0200653 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
654 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100655 int rc = 0, nullifying = false;
David Hildenbrand634790b2015-11-04 16:33:33 +0100656 u16 ilen;
David Hildenbrand87128362014-03-03 10:55:13 +0100657
Jens Freimann383d0b02014-07-29 15:11:49 +0200658 spin_lock(&li->lock);
659 pgm_info = li->irq.pgm;
660 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
661 memset(&li->irq.pgm, 0, sizeof(pgm_info));
662 spin_unlock(&li->lock);
663
David Hildenbrand634790b2015-11-04 16:33:33 +0100664 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100665 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
666 pgm_info.code, ilen);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200667 vcpu->stat.deliver_program_int++;
668 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200669 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200670
Jens Freimann383d0b02014-07-29 15:11:49 +0200671 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100672 case PGM_AFX_TRANSLATION:
673 case PGM_ASX_TRANSLATION:
674 case PGM_EX_TRANSLATION:
675 case PGM_LFX_TRANSLATION:
676 case PGM_LSTE_SEQUENCE:
677 case PGM_LSX_TRANSLATION:
678 case PGM_LX_TRANSLATION:
679 case PGM_PRIMARY_AUTHORITY:
680 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100681 nullifying = true;
682 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100683 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200684 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100685 (u64 *)__LC_TRANS_EXC_CODE);
686 break;
687 case PGM_ALEN_TRANSLATION:
688 case PGM_ALE_SEQUENCE:
689 case PGM_ASTE_INSTANCE:
690 case PGM_ASTE_SEQUENCE:
691 case PGM_ASTE_VALIDITY:
692 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200693 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100694 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100695 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100696 break;
697 case PGM_ASCE_TYPE:
698 case PGM_PAGE_TRANSLATION:
699 case PGM_REGION_FIRST_TRANS:
700 case PGM_REGION_SECOND_TRANS:
701 case PGM_REGION_THIRD_TRANS:
702 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200703 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100704 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200705 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100706 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200707 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100708 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100709 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100710 break;
711 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200712 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200713 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200714 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100715 (u64 *)__LC_MON_CODE);
716 break;
Eric Farman403c8642015-02-02 15:01:06 -0500717 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100718 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200719 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100720 (u32 *)__LC_DATA_EXC_CODE);
721 break;
722 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200723 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100724 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200725 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100726 (u8 *)__LC_EXC_ACCESS_ID);
727 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100728 case PGM_STACK_FULL:
729 case PGM_STACK_EMPTY:
730 case PGM_STACK_SPECIFICATION:
731 case PGM_STACK_TYPE:
732 case PGM_STACK_OPERATION:
733 case PGM_TRACE_TABEL:
734 case PGM_CRYPTO_OPERATION:
735 nullifying = true;
736 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100737 }
738
Jens Freimann383d0b02014-07-29 15:11:49 +0200739 if (pgm_info.code & PGM_PER) {
740 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100741 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200742 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100743 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200744 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100745 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200746 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100747 (u8 *) __LC_PER_ACCESS_ID);
748 }
749
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100750 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100751 kvm_s390_rewind_psw(vcpu, ilen);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100752
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100753 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
754 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100755 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
756 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200757 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100758 (u16 *)__LC_PGM_INT_CODE);
759 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
760 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
761 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
762 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100763 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200764}
David Hildenbrand87128362014-03-03 10:55:13 +0100765
Jens Freimann6d3da242013-07-03 15:18:35 +0200766static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200767{
Jens Freimann6d3da242013-07-03 15:18:35 +0200768 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
769 struct kvm_s390_ext_info ext;
770 int rc = 0;
771
772 spin_lock(&fi->lock);
773 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
774 spin_unlock(&fi->lock);
775 return 0;
776 }
777 ext = fi->srv_signal;
778 memset(&fi->srv_signal, 0, sizeof(ext));
779 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
780 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200781
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200782 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200783 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200784 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200785 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
786 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200787
788 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100789 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200790 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
791 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
792 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
793 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200794 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200795 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200796
Jens Freimann99e20002014-12-01 17:05:39 +0100797 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200798}
799
Jens Freimann6d3da242013-07-03 15:18:35 +0200800static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200801{
Jens Freimann6d3da242013-07-03 15:18:35 +0200802 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
803 struct kvm_s390_interrupt_info *inti;
804 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200805
Jens Freimann6d3da242013-07-03 15:18:35 +0200806 spin_lock(&fi->lock);
807 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
808 struct kvm_s390_interrupt_info,
809 list);
810 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200811 list_del(&inti->list);
812 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
813 }
814 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
815 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
816 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200817
Jens Freimann6d3da242013-07-03 15:18:35 +0200818 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200819 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
820 KVM_S390_INT_PFAULT_DONE, 0,
821 inti->ext.ext_params2);
822 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
823 inti->ext.ext_params2);
824
Jens Freimann6d3da242013-07-03 15:18:35 +0200825 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
826 (u16 *)__LC_EXT_INT_CODE);
827 rc |= put_guest_lc(vcpu, PFAULT_DONE,
828 (u16 *)__LC_EXT_CPU_ADDR);
829 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
830 &vcpu->arch.sie_block->gpsw,
831 sizeof(psw_t));
832 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
833 &vcpu->arch.sie_block->gpsw,
834 sizeof(psw_t));
835 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
836 (u64 *)__LC_EXT_PARAMS2);
837 kfree(inti);
838 }
Jens Freimann99e20002014-12-01 17:05:39 +0100839 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200840}
841
Jens Freimann6d3da242013-07-03 15:18:35 +0200842static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200843{
Jens Freimann6d3da242013-07-03 15:18:35 +0200844 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
845 struct kvm_s390_interrupt_info *inti;
846 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200847
Jens Freimann6d3da242013-07-03 15:18:35 +0200848 spin_lock(&fi->lock);
849 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
850 struct kvm_s390_interrupt_info,
851 list);
852 if (inti) {
853 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200854 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200855 inti->ext.ext_params, inti->ext.ext_params2);
856 vcpu->stat.deliver_virtio_interrupt++;
857 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
858 inti->type,
859 inti->ext.ext_params,
860 inti->ext.ext_params2);
861 list_del(&inti->list);
862 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
863 }
864 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
865 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
866 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200867
Jens Freimann6d3da242013-07-03 15:18:35 +0200868 if (inti) {
869 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
870 (u16 *)__LC_EXT_INT_CODE);
871 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
872 (u16 *)__LC_EXT_CPU_ADDR);
873 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
874 &vcpu->arch.sie_block->gpsw,
875 sizeof(psw_t));
876 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
877 &vcpu->arch.sie_block->gpsw,
878 sizeof(psw_t));
879 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
880 (u32 *)__LC_EXT_PARAMS);
881 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
882 (u64 *)__LC_EXT_PARAMS2);
883 kfree(inti);
884 }
Jens Freimann99e20002014-12-01 17:05:39 +0100885 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200886}
887
888static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200889 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200890{
Jens Freimann6d3da242013-07-03 15:18:35 +0200891 struct list_head *isc_list;
892 struct kvm_s390_float_interrupt *fi;
893 struct kvm_s390_interrupt_info *inti = NULL;
894 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200895
Jens Freimann6d3da242013-07-03 15:18:35 +0200896 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200897
Jens Freimann6d3da242013-07-03 15:18:35 +0200898 spin_lock(&fi->lock);
899 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
900 inti = list_first_entry_or_null(isc_list,
901 struct kvm_s390_interrupt_info,
902 list);
903 if (inti) {
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +0200904 if (inti->type & KVM_S390_INT_IO_AI_MASK)
905 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
906 else
907 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
908 inti->io.subchannel_id >> 8,
909 inti->io.subchannel_id >> 1 & 0x3,
910 inti->io.subchannel_nr);
911
Jens Freimann6d3da242013-07-03 15:18:35 +0200912 vcpu->stat.deliver_io_int++;
913 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
914 inti->type,
915 ((__u32)inti->io.subchannel_id << 16) |
916 inti->io.subchannel_nr,
917 ((__u64)inti->io.io_int_parm << 32) |
918 inti->io.io_int_word);
919 list_del(&inti->list);
920 fi->counters[FIRQ_CNTR_IO] -= 1;
921 }
922 if (list_empty(isc_list))
923 clear_bit(irq_type, &fi->pending_irqs);
924 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100925
Jens Freimann6d3da242013-07-03 15:18:35 +0200926 if (inti) {
927 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
928 (u16 *)__LC_SUBCHANNEL_ID);
929 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
930 (u16 *)__LC_SUBCHANNEL_NR);
931 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
932 (u32 *)__LC_IO_INT_PARM);
933 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
934 (u32 *)__LC_IO_INT_WORD);
935 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
936 &vcpu->arch.sie_block->gpsw,
937 sizeof(psw_t));
938 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
939 &vcpu->arch.sie_block->gpsw,
940 sizeof(psw_t));
941 kfree(inti);
942 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200943
Jens Freimann99e20002014-12-01 17:05:39 +0100944 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200945}
946
947typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
948
949static const deliver_irq_t deliver_irq_funcs[] = {
950 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200951 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200952 [IRQ_PEND_PROG] = __deliver_prog,
953 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
954 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
955 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
956 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
957 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200958 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
959 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200960 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
961 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
962 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200963};
964
David Hildenbrandea5f4962014-10-14 15:29:30 +0200965/* Check whether an external call is pending (deliverable or not) */
966int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100967{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200968 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +0100969
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200970 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200971 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100972
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200973 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100974}
975
David Hildenbrand9a022062014-08-05 17:40:47 +0200976int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100977{
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200978 if (deliverable_irqs(vcpu))
979 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100980
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200981 if (kvm_cpu_has_pending_timer(vcpu))
982 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100983
David Hildenbrandea5f4962014-10-14 15:29:30 +0200984 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200985 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +0200986 !psw_extint_disabled(vcpu) &&
987 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200988 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +0100989
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200990 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
991 return 1;
992 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100993}
994
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300995int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
996{
David Hildenbrandb4aec922014-12-01 15:55:42 +0100997 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300998}
999
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001000static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
1001{
1002 u64 now, cputm, sltime = 0;
1003
1004 if (ckc_interrupts_enabled(vcpu)) {
1005 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1006 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
1007 /* already expired or overflow? */
1008 if (!sltime || vcpu->arch.sie_block->ckc <= now)
1009 return 0;
1010 if (cpu_timer_interrupts_enabled(vcpu)) {
1011 cputm = kvm_s390_get_cpu_timer(vcpu);
1012 /* already expired? */
1013 if (cputm >> 63)
1014 return 0;
1015 return min(sltime, tod_to_ns(cputm));
1016 }
1017 } else if (cpu_timer_interrupts_enabled(vcpu)) {
1018 sltime = kvm_s390_get_cpu_timer(vcpu);
1019 /* already expired? */
1020 if (sltime >> 63)
1021 return 0;
1022 }
1023 return sltime;
1024}
1025
Carsten Otteba5c1e92008-03-25 18:47:26 +01001026int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
1027{
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001028 u64 sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001029
1030 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001031
David Hildenbrand0759d062014-05-13 16:54:32 +02001032 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +02001033 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +02001034 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +02001035
Carsten Otteba5c1e92008-03-25 18:47:26 +01001036 if (psw_interrupts_disabled(vcpu)) {
1037 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001038 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +01001039 }
1040
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001041 if (!ckc_interrupts_enabled(vcpu) &&
1042 !cpu_timer_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001043 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +01001044 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001045 goto no_timer;
1046 }
1047
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001048 sltime = __calculate_sltime(vcpu);
1049 if (!sltime)
David Hildenbrandbda343e2014-12-12 12:26:40 +01001050 return 0;
1051
1052 __set_cpu_idle(vcpu);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001053 hrtimer_start(&vcpu->arch.ckc_timer, sltime, HRTIMER_MODE_REL);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001054 VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001055no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +02001056 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +02001057 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001058 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +02001059 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1060
David Hildenbrand2d00f752014-12-11 10:18:01 +01001061 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001062 return 0;
1063}
1064
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001065void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1066{
Christian Borntraeger3491caf2016-05-13 12:16:35 +02001067 /*
1068 * We cannot move this into the if, as the CPU might be already
1069 * in kvm_vcpu_block without having the waitqueue set (polling)
1070 */
1071 vcpu->valid_wakeup = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001072 if (swait_active(&vcpu->wq)) {
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001073 /*
1074 * The vcpu gave up the cpu voluntarily, mark it as a good
1075 * yield-candidate.
1076 */
1077 vcpu->preempted = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001078 swake_up(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +02001079 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001080 }
David Hildenbrandadbf1692016-05-27 22:03:52 +02001081 /*
1082 * The VCPU might not be sleeping but is executing the VSIE. Let's
1083 * kick it, so it leaves the SIE to process the request.
1084 */
1085 kvm_s390_vsie_kick(vcpu);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001086}
1087
Christian Borntraegerca872302009-05-12 17:21:49 +02001088enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1089{
1090 struct kvm_vcpu *vcpu;
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001091 u64 sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +02001092
1093 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001094 sltime = __calculate_sltime(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001095
David Hildenbrand2d00f752014-12-11 10:18:01 +01001096 /*
1097 * If the monotonic clock runs faster than the tod clock we might be
1098 * woken up too early and have to go back to sleep to avoid deadlocks.
1099 */
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001100 if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
David Hildenbrand2d00f752014-12-11 10:18:01 +01001101 return HRTIMER_RESTART;
1102 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001103 return HRTIMER_NORESTART;
1104}
Carsten Otteba5c1e92008-03-25 18:47:26 +01001105
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001106void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1107{
1108 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001109
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001110 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001111 li->pending_irqs = 0;
1112 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1113 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001114 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +01001115
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001116 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001117}
1118
Christian Borntraeger614aeab2014-08-25 12:27:29 +02001119int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001120{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001121 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001122 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +02001123 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +02001124 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +02001125 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001126
1127 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001128
Jens Freimann383d0b02014-07-29 15:11:49 +02001129 /* pending ckc conditions might have been invalidated */
1130 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +01001131 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +02001132 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1133
David Hildenbrandb4aec922014-12-01 15:55:42 +01001134 /* pending cpu timer conditions might have been invalidated */
1135 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1136 if (cpu_timer_irq_pending(vcpu))
1137 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1138
Jens Freimannffeca0a2015-04-17 10:21:04 +02001139 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001140 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +02001141 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +02001142 if (is_ioirq(irq_type)) {
1143 rc = __deliver_io(vcpu, irq_type);
1144 } else {
1145 func = deliver_irq_funcs[irq_type];
1146 if (!func) {
1147 WARN_ON_ONCE(func == NULL);
1148 clear_bit(irq_type, &li->pending_irqs);
1149 continue;
1150 }
1151 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +02001152 }
Jens Freimannffeca0a2015-04-17 10:21:04 +02001153 }
Jens Freimann383d0b02014-07-29 15:11:49 +02001154
Jens Freimann6d3da242013-07-03 15:18:35 +02001155 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +02001156
1157 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001158}
1159
Jens Freimann383d0b02014-07-29 15:11:49 +02001160static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001161{
1162 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1163
David Hildenbranded2afcf2015-07-20 10:33:03 +02001164 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1165 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1166 irq->u.pgm.code, 0);
1167
David Hildenbrand634790b2015-11-04 16:33:33 +01001168 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1169 /* auto detection if no valid ILC was given */
1170 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1171 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1172 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1173 }
1174
David Hildenbrand238293b2015-05-04 12:38:48 +02001175 if (irq->u.pgm.code == PGM_PER) {
1176 li->irq.pgm.code |= PGM_PER;
David Hildenbrand634790b2015-11-04 16:33:33 +01001177 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001178 /* only modify PER related information */
1179 li->irq.pgm.per_address = irq->u.pgm.per_address;
1180 li->irq.pgm.per_code = irq->u.pgm.per_code;
1181 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1182 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1183 } else if (!(irq->u.pgm.code & PGM_PER)) {
1184 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1185 irq->u.pgm.code;
David Hildenbrand634790b2015-11-04 16:33:33 +01001186 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001187 /* only modify non-PER information */
1188 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1189 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1190 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1191 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1192 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1193 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1194 } else {
1195 li->irq.pgm = irq->u.pgm;
1196 }
Jens Freimann91851242014-12-01 16:43:40 +01001197 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001198 return 0;
1199}
1200
Jens Freimann383d0b02014-07-29 15:11:49 +02001201static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001202{
1203 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1204
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001205 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1206 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001207 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1208 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001209 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001210
1211 li->irq.ext = irq->u.ext;
1212 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001213 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001214 return 0;
1215}
1216
Christian Borntraeger0675d922015-01-15 12:40:42 +01001217static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001218{
1219 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001220 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001221 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001222
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001223 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001224 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001225 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001226 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001227
David Hildenbrandea5f4962014-10-14 15:29:30 +02001228 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001229 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001230 return -EINVAL;
1231
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001232 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001233 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001234
David Hildenbrandb938eace2015-04-30 13:33:59 +02001235 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001236 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001237 *extcall = irq->u.extcall;
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001238 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001239 return 0;
1240}
1241
Jens Freimann383d0b02014-07-29 15:11:49 +02001242static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001243{
1244 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001245 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001246
David Hildenbranded2afcf2015-07-20 10:33:03 +02001247 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001248 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001249 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001250 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001251
David Hildenbranda3a9c592014-10-14 09:44:55 +02001252 if (!is_vcpu_stopped(vcpu))
1253 return -EBUSY;
1254
Jens Freimann383d0b02014-07-29 15:11:49 +02001255 *prefix = irq->u.prefix;
1256 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001257 return 0;
1258}
1259
David Hildenbrand6cddd432014-10-15 16:48:53 +02001260#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001261static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001262{
1263 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001264 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001265 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001266
David Hildenbranded2afcf2015-07-20 10:33:03 +02001267 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001268
David Hildenbrand28225452014-10-15 16:48:16 +02001269 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1270 return -EINVAL;
1271
David Hildenbrand6cddd432014-10-15 16:48:53 +02001272 if (is_vcpu_stopped(vcpu)) {
1273 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1274 rc = kvm_s390_store_status_unloaded(vcpu,
1275 KVM_S390_STORE_STATUS_NOADDR);
1276 return rc;
1277 }
1278
1279 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1280 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001281 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001282 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001283 return 0;
1284}
1285
1286static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001287 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001288{
1289 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1290
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001291 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001292 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001293
1294 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001295 return 0;
1296}
1297
1298static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001299 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001300{
1301 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1302
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001303 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001304 irq->u.emerg.code);
1305 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001306 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001307
David Hildenbrandb85de332015-11-05 09:38:15 +01001308 /* sending vcpu invalid */
1309 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1310 return -EINVAL;
1311
Jens Freimann49538d12014-12-18 15:48:14 +01001312 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001313 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001314 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001315 return 0;
1316}
1317
Jens Freimann383d0b02014-07-29 15:11:49 +02001318static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001319{
1320 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001321 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001322
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001323 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001324 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001325 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001326 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001327
1328 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001329 * Because repressible machine checks can be indicated along with
1330 * exigent machine checks (PoP, Chapter 11, Interruption action)
1331 * we need to combine cr14, mcic and external damage code.
1332 * Failing storage address and the logout area should not be or'ed
1333 * together, we just indicate the last occurrence of the corresponding
1334 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001335 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001336 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001337 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001338 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1339 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1340 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1341 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001342 if (mchk->mcic & MCHK_EX_MASK)
1343 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1344 else if (mchk->mcic & MCHK_REP_MASK)
1345 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001346 return 0;
1347}
1348
Jens Freimann383d0b02014-07-29 15:11:49 +02001349static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001350{
1351 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1352
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001353 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001354 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001355 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001356
1357 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001358 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001359 return 0;
1360}
1361
Jens Freimann383d0b02014-07-29 15:11:49 +02001362static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001363{
1364 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1365
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001366 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001367 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001368 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001369
1370 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001371 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001372 return 0;
1373}
1374
Jens Freimann6d3da242013-07-03 15:18:35 +02001375static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1376 int isc, u32 schid)
1377{
1378 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1379 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1380 struct kvm_s390_interrupt_info *iter;
1381 u16 id = (schid & 0xffff0000U) >> 16;
1382 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001383
Jens Freimann6d3da242013-07-03 15:18:35 +02001384 spin_lock(&fi->lock);
1385 list_for_each_entry(iter, isc_list, list) {
1386 if (schid && (id != iter->io.subchannel_id ||
1387 nr != iter->io.subchannel_nr))
1388 continue;
1389 /* found an appropriate entry */
1390 list_del_init(&iter->list);
1391 fi->counters[FIRQ_CNTR_IO] -= 1;
1392 if (list_empty(isc_list))
1393 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1394 spin_unlock(&fi->lock);
1395 return iter;
1396 }
1397 spin_unlock(&fi->lock);
1398 return NULL;
1399}
1400
1401/*
1402 * Dequeue and return an I/O interrupt matching any of the interruption
1403 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1404 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001405struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001406 u64 isc_mask, u32 schid)
1407{
1408 struct kvm_s390_interrupt_info *inti = NULL;
1409 int isc;
1410
1411 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1412 if (isc_mask & isc_to_isc_bits(isc))
1413 inti = get_io_int(kvm, isc, schid);
1414 }
1415 return inti;
1416}
1417
1418#define SCCB_MASK 0xFFFFFFF8
1419#define SCCB_EVENT_PENDING 0x3
1420
1421static int __inject_service(struct kvm *kvm,
1422 struct kvm_s390_interrupt_info *inti)
1423{
1424 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1425
1426 spin_lock(&fi->lock);
1427 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1428 /*
1429 * Early versions of the QEMU s390 bios will inject several
1430 * service interrupts after another without handling a
1431 * condition code indicating busy.
1432 * We will silently ignore those superfluous sccb values.
1433 * A future version of QEMU will take care of serialization
1434 * of servc requests
1435 */
1436 if (fi->srv_signal.ext_params & SCCB_MASK)
1437 goto out;
1438 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1439 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1440out:
1441 spin_unlock(&fi->lock);
1442 kfree(inti);
1443 return 0;
1444}
1445
1446static int __inject_virtio(struct kvm *kvm,
1447 struct kvm_s390_interrupt_info *inti)
1448{
1449 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1450
1451 spin_lock(&fi->lock);
1452 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1453 spin_unlock(&fi->lock);
1454 return -EBUSY;
1455 }
1456 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1457 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1458 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1459 spin_unlock(&fi->lock);
1460 return 0;
1461}
1462
1463static int __inject_pfault_done(struct kvm *kvm,
1464 struct kvm_s390_interrupt_info *inti)
1465{
1466 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1467
1468 spin_lock(&fi->lock);
1469 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1470 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1471 spin_unlock(&fi->lock);
1472 return -EBUSY;
1473 }
1474 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1475 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1476 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1477 spin_unlock(&fi->lock);
1478 return 0;
1479}
1480
1481#define CR_PENDING_SUBCLASS 28
1482static int __inject_float_mchk(struct kvm *kvm,
1483 struct kvm_s390_interrupt_info *inti)
1484{
1485 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1486
1487 spin_lock(&fi->lock);
1488 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1489 fi->mchk.mcic |= inti->mchk.mcic;
1490 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1491 spin_unlock(&fi->lock);
1492 kfree(inti);
1493 return 0;
1494}
1495
1496static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001497{
1498 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001499 struct list_head *list;
1500 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001501
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001502 fi = &kvm->arch.float_int;
1503 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001504 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1505 spin_unlock(&fi->lock);
1506 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001507 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001508 fi->counters[FIRQ_CNTR_IO] += 1;
1509
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +02001510 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1511 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1512 else
1513 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1514 inti->io.subchannel_id >> 8,
1515 inti->io.subchannel_id >> 1 & 0x3,
1516 inti->io.subchannel_nr);
Jens Freimann6d3da242013-07-03 15:18:35 +02001517 isc = int_word_to_isc(inti->io.io_int_word);
1518 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1519 list_add_tail(&inti->list, list);
1520 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001521 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001522 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001523}
1524
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001525/*
1526 * Find a destination VCPU for a floating irq and kick it.
1527 */
1528static void __floating_irq_kick(struct kvm *kvm, u64 type)
1529{
1530 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1531 struct kvm_s390_local_interrupt *li;
1532 struct kvm_vcpu *dst_vcpu;
1533 int sigcpu, online_vcpus, nr_tries = 0;
1534
1535 online_vcpus = atomic_read(&kvm->online_vcpus);
1536 if (!online_vcpus)
1537 return;
1538
1539 /* find idle VCPUs first, then round robin */
1540 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1541 if (sigcpu == online_vcpus) {
1542 do {
1543 sigcpu = fi->next_rr_cpu;
1544 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1545 /* avoid endless loops if all vcpus are stopped */
1546 if (nr_tries++ >= online_vcpus)
1547 return;
1548 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1549 }
1550 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1551
1552 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1553 li = &dst_vcpu->arch.local_int;
1554 spin_lock(&li->lock);
1555 switch (type) {
1556 case KVM_S390_MCHK:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001557 atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001558 break;
1559 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001560 atomic_or(CPUSTAT_IO_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001561 break;
1562 default:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001563 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001564 break;
1565 }
1566 spin_unlock(&li->lock);
1567 kvm_s390_vcpu_wakeup(dst_vcpu);
1568}
1569
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001570static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001571{
Jens Freimann6d3da242013-07-03 15:18:35 +02001572 u64 type = READ_ONCE(inti->type);
1573 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001574
Jens Freimann6d3da242013-07-03 15:18:35 +02001575 switch (type) {
1576 case KVM_S390_MCHK:
1577 rc = __inject_float_mchk(kvm, inti);
1578 break;
1579 case KVM_S390_INT_VIRTIO:
1580 rc = __inject_virtio(kvm, inti);
1581 break;
1582 case KVM_S390_INT_SERVICE:
1583 rc = __inject_service(kvm, inti);
1584 break;
1585 case KVM_S390_INT_PFAULT_DONE:
1586 rc = __inject_pfault_done(kvm, inti);
1587 break;
1588 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1589 rc = __inject_io(kvm, inti);
1590 break;
1591 default:
1592 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001593 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001594 if (rc)
1595 return rc;
1596
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001597 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001598 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001599}
1600
1601int kvm_s390_inject_vm(struct kvm *kvm,
1602 struct kvm_s390_interrupt *s390int)
1603{
1604 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001605 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001606
1607 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1608 if (!inti)
1609 return -ENOMEM;
1610
1611 inti->type = s390int->type;
1612 switch (inti->type) {
1613 case KVM_S390_INT_VIRTIO:
1614 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1615 s390int->parm, s390int->parm64);
1616 inti->ext.ext_params = s390int->parm;
1617 inti->ext.ext_params2 = s390int->parm64;
1618 break;
1619 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001620 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001621 inti->ext.ext_params = s390int->parm;
1622 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001623 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001624 inti->ext.ext_params2 = s390int->parm64;
1625 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001626 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001627 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001628 s390int->parm64);
1629 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1630 inti->mchk.mcic = s390int->parm64;
1631 break;
1632 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimannc05c4182013-10-07 16:13:45 +02001633 inti->io.subchannel_id = s390int->parm >> 16;
1634 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1635 inti->io.io_int_parm = s390int->parm64 >> 32;
1636 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1637 break;
1638 default:
1639 kfree(inti);
1640 return -EINVAL;
1641 }
1642 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1643 2);
1644
David Hildenbrand428d53b2015-01-16 12:58:09 +01001645 rc = __inject_vm(kvm, inti);
1646 if (rc)
1647 kfree(inti);
1648 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001649}
1650
David Hildenbrand15462e32015-02-04 15:59:11 +01001651int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001652 struct kvm_s390_interrupt_info *inti)
1653{
David Hildenbrand15462e32015-02-04 15:59:11 +01001654 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001655}
1656
Jens Freimann383d0b02014-07-29 15:11:49 +02001657int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1658 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001659{
Jens Freimann383d0b02014-07-29 15:11:49 +02001660 irq->type = s390int->type;
1661 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001662 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001663 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001664 return -EINVAL;
1665 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001666 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001667 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001668 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001669 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001670 case KVM_S390_SIGP_STOP:
1671 irq->u.stop.flags = s390int->parm;
1672 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001673 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001674 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001675 return -EINVAL;
1676 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001677 break;
1678 case KVM_S390_INT_EMERGENCY:
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.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001682 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001683 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001684 irq->u.mchk.mcic = s390int->parm64;
1685 break;
1686 }
1687 return 0;
1688}
1689
David Hildenbrand6cddd432014-10-15 16:48:53 +02001690int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1691{
1692 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1693
1694 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1695}
1696
1697void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1698{
1699 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1700
1701 spin_lock(&li->lock);
1702 li->irq.stop.flags = 0;
1703 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1704 spin_unlock(&li->lock);
1705}
1706
Jens Freimann79e87a12015-03-19 15:12:12 +01001707static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001708{
Jens Freimann383d0b02014-07-29 15:11:49 +02001709 int rc;
1710
Jens Freimann383d0b02014-07-29 15:11:49 +02001711 switch (irq->type) {
1712 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001713 rc = __inject_prog(vcpu, irq);
1714 break;
1715 case KVM_S390_SIGP_SET_PREFIX:
1716 rc = __inject_set_prefix(vcpu, irq);
1717 break;
1718 case KVM_S390_SIGP_STOP:
1719 rc = __inject_sigp_stop(vcpu, irq);
1720 break;
1721 case KVM_S390_RESTART:
1722 rc = __inject_sigp_restart(vcpu, irq);
1723 break;
1724 case KVM_S390_INT_CLOCK_COMP:
1725 rc = __inject_ckc(vcpu);
1726 break;
1727 case KVM_S390_INT_CPU_TIMER:
1728 rc = __inject_cpu_timer(vcpu);
1729 break;
1730 case KVM_S390_INT_EXTERNAL_CALL:
1731 rc = __inject_extcall(vcpu, irq);
1732 break;
1733 case KVM_S390_INT_EMERGENCY:
1734 rc = __inject_sigp_emergency(vcpu, irq);
1735 break;
1736 case KVM_S390_MCHK:
1737 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001738 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001739 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001740 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001741 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001742 case KVM_S390_INT_VIRTIO:
1743 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001744 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001745 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001746 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001747 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001748
1749 return rc;
1750}
1751
1752int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1753{
1754 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1755 int rc;
1756
1757 spin_lock(&li->lock);
1758 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001759 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001760 if (!rc)
1761 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001762 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001763}
Jens Freimannc05c4182013-10-07 16:13:45 +02001764
Jens Freimann6d3da242013-07-03 15:18:35 +02001765static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001766{
Jens Freimann6d3da242013-07-03 15:18:35 +02001767 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001768
Jens Freimann6d3da242013-07-03 15:18:35 +02001769 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001770 list_del(&inti->list);
1771 kfree(inti);
1772 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001773}
1774
Jens Freimann94aa0332015-03-16 12:17:13 +01001775static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1776 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001777{
Jens Freimann94aa0332015-03-16 12:17:13 +01001778 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001779 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001780 case KVM_S390_INT_PFAULT_INIT:
1781 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001782 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001783 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001784 break;
1785 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001786 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001787 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001788 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001789}
1790
Jens Freimann6d3da242013-07-03 15:18:35 +02001791void kvm_s390_clear_float_irqs(struct kvm *kvm)
1792{
1793 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1794 int i;
1795
1796 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001797 fi->pending_irqs = 0;
1798 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1799 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001800 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1801 clear_irq_list(&fi->lists[i]);
1802 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1803 fi->counters[i] = 0;
1804 spin_unlock(&fi->lock);
1805};
1806
Jens Freimann94aa0332015-03-16 12:17:13 +01001807static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001808{
1809 struct kvm_s390_interrupt_info *inti;
1810 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001811 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001812 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001813 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001814 int ret = 0;
1815 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001816 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001817
Jens Freimann94aa0332015-03-16 12:17:13 +01001818 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1819 return -EINVAL;
1820
1821 /*
1822 * We are already using -ENOMEM to signal
1823 * userspace it may retry with a bigger buffer,
1824 * so we need to use something else for this case
1825 */
1826 buf = vzalloc(len);
1827 if (!buf)
1828 return -ENOBUFS;
1829
1830 max_irqs = len / sizeof(struct kvm_s390_irq);
1831
Jens Freimannc05c4182013-10-07 16:13:45 +02001832 fi = &kvm->arch.float_int;
1833 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001834 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1835 list_for_each_entry(inti, &fi->lists[i], list) {
1836 if (n == max_irqs) {
1837 /* signal userspace to try again */
1838 ret = -ENOMEM;
1839 goto out;
1840 }
1841 inti_to_irq(inti, &buf[n]);
1842 n++;
1843 }
1844 }
1845 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001846 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001847 /* signal userspace to try again */
1848 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001849 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001850 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001851 irq = (struct kvm_s390_irq *) &buf[n];
1852 irq->type = KVM_S390_INT_SERVICE;
1853 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001854 n++;
1855 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001856 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1857 if (n == max_irqs) {
1858 /* signal userspace to try again */
1859 ret = -ENOMEM;
1860 goto out;
1861 }
1862 irq = (struct kvm_s390_irq *) &buf[n];
1863 irq->type = KVM_S390_MCHK;
1864 irq->u.mchk = fi->mchk;
1865 n++;
1866}
1867
1868out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001869 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001870 if (!ret && n > 0) {
1871 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1872 ret = -EFAULT;
1873 }
1874 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001875
1876 return ret < 0 ? ret : n;
1877}
1878
1879static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1880{
1881 int r;
1882
1883 switch (attr->group) {
1884 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001885 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001886 attr->attr);
1887 break;
1888 default:
1889 r = -EINVAL;
1890 }
1891
1892 return r;
1893}
1894
1895static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1896 u64 addr)
1897{
1898 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1899 void *target = NULL;
1900 void __user *source;
1901 u64 size;
1902
1903 if (get_user(inti->type, (u64 __user *)addr))
1904 return -EFAULT;
1905
1906 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001907 case KVM_S390_INT_PFAULT_INIT:
1908 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001909 case KVM_S390_INT_VIRTIO:
1910 case KVM_S390_INT_SERVICE:
1911 target = (void *) &inti->ext;
1912 source = &uptr->u.ext;
1913 size = sizeof(inti->ext);
1914 break;
1915 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1916 target = (void *) &inti->io;
1917 source = &uptr->u.io;
1918 size = sizeof(inti->io);
1919 break;
1920 case KVM_S390_MCHK:
1921 target = (void *) &inti->mchk;
1922 source = &uptr->u.mchk;
1923 size = sizeof(inti->mchk);
1924 break;
1925 default:
1926 return -EINVAL;
1927 }
1928
1929 if (copy_from_user(target, source, size))
1930 return -EFAULT;
1931
1932 return 0;
1933}
1934
1935static int enqueue_floating_irq(struct kvm_device *dev,
1936 struct kvm_device_attr *attr)
1937{
1938 struct kvm_s390_interrupt_info *inti = NULL;
1939 int r = 0;
1940 int len = attr->attr;
1941
1942 if (len % sizeof(struct kvm_s390_irq) != 0)
1943 return -EINVAL;
1944 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1945 return -EINVAL;
1946
1947 while (len >= sizeof(struct kvm_s390_irq)) {
1948 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1949 if (!inti)
1950 return -ENOMEM;
1951
1952 r = copy_irq_from_user(inti, attr->addr);
1953 if (r) {
1954 kfree(inti);
1955 return r;
1956 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001957 r = __inject_vm(dev->kvm, inti);
1958 if (r) {
1959 kfree(inti);
1960 return r;
1961 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001962 len -= sizeof(struct kvm_s390_irq);
1963 attr->addr += sizeof(struct kvm_s390_irq);
1964 }
1965
1966 return r;
1967}
1968
Cornelia Huck841b91c2013-07-15 13:36:01 +02001969static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1970{
1971 if (id >= MAX_S390_IO_ADAPTERS)
1972 return NULL;
1973 return kvm->arch.adapters[id];
1974}
1975
1976static int register_io_adapter(struct kvm_device *dev,
1977 struct kvm_device_attr *attr)
1978{
1979 struct s390_io_adapter *adapter;
1980 struct kvm_s390_io_adapter adapter_info;
1981
1982 if (copy_from_user(&adapter_info,
1983 (void __user *)attr->addr, sizeof(adapter_info)))
1984 return -EFAULT;
1985
1986 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1987 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1988 return -EINVAL;
1989
1990 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1991 if (!adapter)
1992 return -ENOMEM;
1993
1994 INIT_LIST_HEAD(&adapter->maps);
1995 init_rwsem(&adapter->maps_lock);
1996 atomic_set(&adapter->nr_maps, 0);
1997 adapter->id = adapter_info.id;
1998 adapter->isc = adapter_info.isc;
1999 adapter->maskable = adapter_info.maskable;
2000 adapter->masked = false;
2001 adapter->swap = adapter_info.swap;
Fei Li08fab502017-01-19 17:02:26 +01002002 adapter->suppressible = (adapter_info.flags) &
2003 KVM_S390_ADAPTER_SUPPRESSIBLE;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002004 dev->kvm->arch.adapters[adapter->id] = adapter;
2005
2006 return 0;
2007}
2008
2009int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
2010{
2011 int ret;
2012 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2013
2014 if (!adapter || !adapter->maskable)
2015 return -EINVAL;
2016 ret = adapter->masked;
2017 adapter->masked = masked;
2018 return ret;
2019}
2020
2021static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
2022{
2023 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2024 struct s390_map_info *map;
2025 int ret;
2026
2027 if (!adapter || !addr)
2028 return -EINVAL;
2029
2030 map = kzalloc(sizeof(*map), GFP_KERNEL);
2031 if (!map) {
2032 ret = -ENOMEM;
2033 goto out;
2034 }
2035 INIT_LIST_HEAD(&map->list);
2036 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02002037 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02002038 if (map->addr == -EFAULT) {
2039 ret = -EFAULT;
2040 goto out;
2041 }
2042 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
2043 if (ret < 0)
2044 goto out;
2045 BUG_ON(ret != 1);
2046 down_write(&adapter->maps_lock);
2047 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2048 list_add_tail(&map->list, &adapter->maps);
2049 ret = 0;
2050 } else {
2051 put_page(map->page);
2052 ret = -EINVAL;
2053 }
2054 up_write(&adapter->maps_lock);
2055out:
2056 if (ret)
2057 kfree(map);
2058 return ret;
2059}
2060
2061static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2062{
2063 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2064 struct s390_map_info *map, *tmp;
2065 int found = 0;
2066
2067 if (!adapter || !addr)
2068 return -EINVAL;
2069
2070 down_write(&adapter->maps_lock);
2071 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2072 if (map->guest_addr == addr) {
2073 found = 1;
2074 atomic_dec(&adapter->nr_maps);
2075 list_del(&map->list);
2076 put_page(map->page);
2077 kfree(map);
2078 break;
2079 }
2080 }
2081 up_write(&adapter->maps_lock);
2082
2083 return found ? 0 : -EINVAL;
2084}
2085
2086void kvm_s390_destroy_adapters(struct kvm *kvm)
2087{
2088 int i;
2089 struct s390_map_info *map, *tmp;
2090
2091 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2092 if (!kvm->arch.adapters[i])
2093 continue;
2094 list_for_each_entry_safe(map, tmp,
2095 &kvm->arch.adapters[i]->maps, list) {
2096 list_del(&map->list);
2097 put_page(map->page);
2098 kfree(map);
2099 }
2100 kfree(kvm->arch.adapters[i]);
2101 }
2102}
2103
2104static int modify_io_adapter(struct kvm_device *dev,
2105 struct kvm_device_attr *attr)
2106{
2107 struct kvm_s390_io_adapter_req req;
2108 struct s390_io_adapter *adapter;
2109 int ret;
2110
2111 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2112 return -EFAULT;
2113
2114 adapter = get_io_adapter(dev->kvm, req.id);
2115 if (!adapter)
2116 return -EINVAL;
2117 switch (req.type) {
2118 case KVM_S390_IO_ADAPTER_MASK:
2119 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2120 if (ret > 0)
2121 ret = 0;
2122 break;
2123 case KVM_S390_IO_ADAPTER_MAP:
2124 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2125 break;
2126 case KVM_S390_IO_ADAPTER_UNMAP:
2127 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2128 break;
2129 default:
2130 ret = -EINVAL;
2131 }
2132
2133 return ret;
2134}
2135
Halil Pasic6d28f782016-01-25 19:10:40 +01002136static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2137
2138{
2139 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2140 u32 schid;
2141
2142 if (attr->flags)
2143 return -EINVAL;
2144 if (attr->attr != sizeof(schid))
2145 return -EINVAL;
2146 if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2147 return -EFAULT;
2148 kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2149 /*
2150 * If userspace is conforming to the architecture, we can have at most
2151 * one pending I/O interrupt per subchannel, so this is effectively a
2152 * clear all.
2153 */
2154 return 0;
2155}
2156
Fei Li51978392017-02-17 17:06:26 +08002157static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
2158{
2159 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2160 struct kvm_s390_ais_req req;
2161 int ret = 0;
2162
2163 if (!fi->ais_enabled)
2164 return -ENOTSUPP;
2165
2166 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2167 return -EFAULT;
2168
2169 if (req.isc > MAX_ISC)
2170 return -EINVAL;
2171
2172 trace_kvm_s390_modify_ais_mode(req.isc,
2173 (fi->simm & AIS_MODE_MASK(req.isc)) ?
2174 (fi->nimm & AIS_MODE_MASK(req.isc)) ?
2175 2 : KVM_S390_AIS_MODE_SINGLE :
2176 KVM_S390_AIS_MODE_ALL, req.mode);
2177
2178 mutex_lock(&fi->ais_lock);
2179 switch (req.mode) {
2180 case KVM_S390_AIS_MODE_ALL:
2181 fi->simm &= ~AIS_MODE_MASK(req.isc);
2182 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2183 break;
2184 case KVM_S390_AIS_MODE_SINGLE:
2185 fi->simm |= AIS_MODE_MASK(req.isc);
2186 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2187 break;
2188 default:
2189 ret = -EINVAL;
2190 }
2191 mutex_unlock(&fi->ais_lock);
2192
2193 return ret;
2194}
2195
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002196static int kvm_s390_inject_airq(struct kvm *kvm,
2197 struct s390_io_adapter *adapter)
2198{
2199 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2200 struct kvm_s390_interrupt s390int = {
2201 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2202 .parm = 0,
2203 .parm64 = (adapter->isc << 27) | 0x80000000,
2204 };
2205 int ret = 0;
2206
2207 if (!fi->ais_enabled || !adapter->suppressible)
2208 return kvm_s390_inject_vm(kvm, &s390int);
2209
2210 mutex_lock(&fi->ais_lock);
2211 if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
2212 trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
2213 goto out;
2214 }
2215
2216 ret = kvm_s390_inject_vm(kvm, &s390int);
2217 if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
2218 fi->nimm |= AIS_MODE_MASK(adapter->isc);
2219 trace_kvm_s390_modify_ais_mode(adapter->isc,
2220 KVM_S390_AIS_MODE_SINGLE, 2);
2221 }
2222out:
2223 mutex_unlock(&fi->ais_lock);
2224 return ret;
2225}
2226
2227static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr)
2228{
2229 unsigned int id = attr->attr;
2230 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2231
2232 if (!adapter)
2233 return -EINVAL;
2234
2235 return kvm_s390_inject_airq(kvm, adapter);
2236}
2237
Jens Freimannc05c4182013-10-07 16:13:45 +02002238static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2239{
2240 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002241 unsigned int i;
2242 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02002243
2244 switch (attr->group) {
2245 case KVM_DEV_FLIC_ENQUEUE:
2246 r = enqueue_floating_irq(dev, attr);
2247 break;
2248 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01002249 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02002250 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002251 case KVM_DEV_FLIC_APF_ENABLE:
2252 dev->kvm->arch.gmap->pfault_enabled = 1;
2253 break;
2254 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2255 dev->kvm->arch.gmap->pfault_enabled = 0;
2256 /*
2257 * Make sure no async faults are in transition when
2258 * clearing the queues. So we don't need to worry
2259 * about late coming workers.
2260 */
2261 synchronize_srcu(&dev->kvm->srcu);
2262 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2263 kvm_clear_async_pf_completion_queue(vcpu);
2264 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002265 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2266 r = register_io_adapter(dev, attr);
2267 break;
2268 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2269 r = modify_io_adapter(dev, attr);
2270 break;
Halil Pasic6d28f782016-01-25 19:10:40 +01002271 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2272 r = clear_io_irq(dev->kvm, attr);
2273 break;
Fei Li51978392017-02-17 17:06:26 +08002274 case KVM_DEV_FLIC_AISM:
2275 r = modify_ais_mode(dev->kvm, attr);
2276 break;
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002277 case KVM_DEV_FLIC_AIRQ_INJECT:
2278 r = flic_inject_airq(dev->kvm, attr);
2279 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002280 default:
2281 r = -EINVAL;
2282 }
2283
2284 return r;
2285}
2286
Halil Pasic4f129852016-02-25 12:44:17 +01002287static int flic_has_attr(struct kvm_device *dev,
2288 struct kvm_device_attr *attr)
2289{
2290 switch (attr->group) {
2291 case KVM_DEV_FLIC_GET_ALL_IRQS:
2292 case KVM_DEV_FLIC_ENQUEUE:
2293 case KVM_DEV_FLIC_CLEAR_IRQS:
2294 case KVM_DEV_FLIC_APF_ENABLE:
2295 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2296 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2297 case KVM_DEV_FLIC_ADAPTER_MODIFY:
Halil Pasic6d28f782016-01-25 19:10:40 +01002298 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
Fei Li51978392017-02-17 17:06:26 +08002299 case KVM_DEV_FLIC_AISM:
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002300 case KVM_DEV_FLIC_AIRQ_INJECT:
Halil Pasic4f129852016-02-25 12:44:17 +01002301 return 0;
2302 }
2303 return -ENXIO;
2304}
2305
Jens Freimannc05c4182013-10-07 16:13:45 +02002306static int flic_create(struct kvm_device *dev, u32 type)
2307{
2308 if (!dev)
2309 return -EINVAL;
2310 if (dev->kvm->arch.flic)
2311 return -EINVAL;
2312 dev->kvm->arch.flic = dev;
2313 return 0;
2314}
2315
2316static void flic_destroy(struct kvm_device *dev)
2317{
2318 dev->kvm->arch.flic = NULL;
2319 kfree(dev);
2320}
2321
2322/* s390 floating irq controller (flic) */
2323struct kvm_device_ops kvm_flic_ops = {
2324 .name = "kvm-flic",
2325 .get_attr = flic_get_attr,
2326 .set_attr = flic_set_attr,
Halil Pasic4f129852016-02-25 12:44:17 +01002327 .has_attr = flic_has_attr,
Jens Freimannc05c4182013-10-07 16:13:45 +02002328 .create = flic_create,
2329 .destroy = flic_destroy,
2330};
Cornelia Huck84223592013-07-15 13:36:01 +02002331
2332static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2333{
2334 unsigned long bit;
2335
2336 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2337
2338 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2339}
2340
2341static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2342 u64 addr)
2343{
2344 struct s390_map_info *map;
2345
2346 if (!adapter)
2347 return NULL;
2348
2349 list_for_each_entry(map, &adapter->maps, list) {
2350 if (map->guest_addr == addr)
2351 return map;
2352 }
2353 return NULL;
2354}
2355
2356static int adapter_indicators_set(struct kvm *kvm,
2357 struct s390_io_adapter *adapter,
2358 struct kvm_s390_adapter_int *adapter_int)
2359{
2360 unsigned long bit;
2361 int summary_set, idx;
2362 struct s390_map_info *info;
2363 void *map;
2364
2365 info = get_map_info(adapter, adapter_int->ind_addr);
2366 if (!info)
2367 return -1;
2368 map = page_address(info->page);
2369 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2370 set_bit(bit, map);
2371 idx = srcu_read_lock(&kvm->srcu);
2372 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2373 set_page_dirty_lock(info->page);
2374 info = get_map_info(adapter, adapter_int->summary_addr);
2375 if (!info) {
2376 srcu_read_unlock(&kvm->srcu, idx);
2377 return -1;
2378 }
2379 map = page_address(info->page);
2380 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2381 adapter->swap);
2382 summary_set = test_and_set_bit(bit, map);
2383 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2384 set_page_dirty_lock(info->page);
2385 srcu_read_unlock(&kvm->srcu, idx);
2386 return summary_set ? 0 : 1;
2387}
2388
2389/*
2390 * < 0 - not injected due to error
2391 * = 0 - coalesced, summary indicator already active
2392 * > 0 - injected interrupt
2393 */
2394static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2395 struct kvm *kvm, int irq_source_id, int level,
2396 bool line_status)
2397{
2398 int ret;
2399 struct s390_io_adapter *adapter;
2400
2401 /* We're only interested in the 0->1 transition. */
2402 if (!level)
2403 return 0;
2404 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2405 if (!adapter)
2406 return -1;
2407 down_read(&adapter->maps_lock);
2408 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2409 up_read(&adapter->maps_lock);
2410 if ((ret > 0) && !adapter->masked) {
Yi Min Zhaoa8920952017-02-20 10:15:01 +08002411 ret = kvm_s390_inject_airq(kvm, adapter);
Cornelia Huck84223592013-07-15 13:36:01 +02002412 if (ret == 0)
2413 ret = 1;
2414 }
2415 return ret;
2416}
2417
Radim Krčmářc63cf532016-07-12 22:09:26 +02002418int kvm_set_routing_entry(struct kvm *kvm,
2419 struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002420 const struct kvm_irq_routing_entry *ue)
2421{
2422 int ret;
2423
2424 switch (ue->type) {
2425 case KVM_IRQ_ROUTING_S390_ADAPTER:
2426 e->set = set_adapter_int;
2427 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2428 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2429 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2430 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2431 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2432 ret = 0;
2433 break;
2434 default:
2435 ret = -EINVAL;
2436 }
2437
2438 return ret;
2439}
2440
2441int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2442 int irq_source_id, int level, bool line_status)
2443{
2444 return -EINVAL;
2445}
Jens Freimann816c7662014-11-24 17:13:46 +01002446
2447int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2448{
2449 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2450 struct kvm_s390_irq *buf;
2451 int r = 0;
2452 int n;
2453
2454 buf = vmalloc(len);
2455 if (!buf)
2456 return -ENOMEM;
2457
2458 if (copy_from_user((void *) buf, irqstate, len)) {
2459 r = -EFAULT;
2460 goto out_free;
2461 }
2462
2463 /*
2464 * Don't allow setting the interrupt state
2465 * when there are already interrupts pending
2466 */
2467 spin_lock(&li->lock);
2468 if (li->pending_irqs) {
2469 r = -EBUSY;
2470 goto out_unlock;
2471 }
2472
2473 for (n = 0; n < len / sizeof(*buf); n++) {
2474 r = do_inject_vcpu(vcpu, &buf[n]);
2475 if (r)
2476 break;
2477 }
2478
2479out_unlock:
2480 spin_unlock(&li->lock);
2481out_free:
2482 vfree(buf);
2483
2484 return r;
2485}
2486
2487static void store_local_irq(struct kvm_s390_local_interrupt *li,
2488 struct kvm_s390_irq *irq,
2489 unsigned long irq_type)
2490{
2491 switch (irq_type) {
2492 case IRQ_PEND_MCHK_EX:
2493 case IRQ_PEND_MCHK_REP:
2494 irq->type = KVM_S390_MCHK;
2495 irq->u.mchk = li->irq.mchk;
2496 break;
2497 case IRQ_PEND_PROG:
2498 irq->type = KVM_S390_PROGRAM_INT;
2499 irq->u.pgm = li->irq.pgm;
2500 break;
2501 case IRQ_PEND_PFAULT_INIT:
2502 irq->type = KVM_S390_INT_PFAULT_INIT;
2503 irq->u.ext = li->irq.ext;
2504 break;
2505 case IRQ_PEND_EXT_EXTERNAL:
2506 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2507 irq->u.extcall = li->irq.extcall;
2508 break;
2509 case IRQ_PEND_EXT_CLOCK_COMP:
2510 irq->type = KVM_S390_INT_CLOCK_COMP;
2511 break;
2512 case IRQ_PEND_EXT_CPU_TIMER:
2513 irq->type = KVM_S390_INT_CPU_TIMER;
2514 break;
2515 case IRQ_PEND_SIGP_STOP:
2516 irq->type = KVM_S390_SIGP_STOP;
2517 irq->u.stop = li->irq.stop;
2518 break;
2519 case IRQ_PEND_RESTART:
2520 irq->type = KVM_S390_RESTART;
2521 break;
2522 case IRQ_PEND_SET_PREFIX:
2523 irq->type = KVM_S390_SIGP_SET_PREFIX;
2524 irq->u.prefix = li->irq.prefix;
2525 break;
2526 }
2527}
2528
2529int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2530{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002531 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002532 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2533 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2534 unsigned long pending_irqs;
2535 struct kvm_s390_irq irq;
2536 unsigned long irq_type;
2537 int cpuaddr;
2538 int n = 0;
2539
2540 spin_lock(&li->lock);
2541 pending_irqs = li->pending_irqs;
2542 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2543 sizeof(sigp_emerg_pending));
2544 spin_unlock(&li->lock);
2545
2546 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2547 memset(&irq, 0, sizeof(irq));
2548 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2549 continue;
2550 if (n + sizeof(irq) > len)
2551 return -ENOBUFS;
2552 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2553 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2554 return -EFAULT;
2555 n += sizeof(irq);
2556 }
2557
2558 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2559 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2560 memset(&irq, 0, sizeof(irq));
2561 if (n + sizeof(irq) > len)
2562 return -ENOBUFS;
2563 irq.type = KVM_S390_INT_EMERGENCY;
2564 irq.u.emerg.code = cpuaddr;
2565 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2566 return -EFAULT;
2567 n += sizeof(irq);
2568 }
2569 }
2570
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002571 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002572 if (n + sizeof(irq) > len)
2573 return -ENOBUFS;
2574 memset(&irq, 0, sizeof(irq));
2575 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002576 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002577 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2578 return -EFAULT;
2579 n += sizeof(irq);
2580 }
2581
2582 return n;
2583}