blob: 353e0b78e6679e016b5cfb759abc5bfdc7b9a897 [file] [log] [blame]
Carsten Otteba5c1e92008-03-25 18:47:26 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01003 *
Thomas Huth33b412a2015-02-11 10:38:46 +01004 * Copyright IBM Corp. 2008, 2015
Carsten Otteba5c1e92008-03-25 18:47:26 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 */
12
Christian Borntraegerca872302009-05-12 17:21:49 +020013#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010014#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010015#include <linux/hrtimer.h>
Cornelia Huck84223592013-07-15 13:36:01 +020016#include <linux/mmu_context.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020017#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Jens Freimann383d0b02014-07-29 15:11:49 +020019#include <linux/bitmap.h>
Jens Freimann94aa0332015-03-16 12:17:13 +010020#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010021#include <asm/asm-offsets.h>
Thomas Huth33b412a2015-02-11 10:38:46 +010022#include <asm/dis.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010023#include <asm/uaccess.h>
David Hildenbrandea5f4962014-10-14 15:29:30 +020024#include <asm/sclp.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020025#include <asm/isc.h>
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
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020045 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +020046 if (vcpu->kvm->arch.use_esca) {
47 struct esca_block *sca = vcpu->kvm->arch.sca;
48 union esca_sigp_ctrl sigp_ctrl =
49 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
50
51 c = sigp_ctrl.c;
52 scn = sigp_ctrl.scn;
53 } else {
54 struct bsca_block *sca = vcpu->kvm->arch.sca;
55 union bsca_sigp_ctrl sigp_ctrl =
56 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
57
58 c = sigp_ctrl.c;
59 scn = sigp_ctrl.scn;
60 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020061 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020062
63 if (src_id)
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +020064 *src_id = scn;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020065
David Hildenbrand2c1bb2b2015-09-23 09:45:50 +020066 return c;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020067}
68
69static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
70{
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020071 int expect, rc;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020072
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020073 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +020074 if (vcpu->kvm->arch.use_esca) {
75 struct esca_block *sca = vcpu->kvm->arch.sca;
76 union esca_sigp_ctrl *sigp_ctrl =
77 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
78 union esca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020079
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +020080 new_val.scn = src_id;
81 new_val.c = 1;
82 old_val.c = 0;
83
84 expect = old_val.value;
85 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
86 } else {
87 struct bsca_block *sca = vcpu->kvm->arch.sca;
88 union bsca_sigp_ctrl *sigp_ctrl =
89 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
90 union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
91
92 new_val.scn = src_id;
93 new_val.c = 1;
94 old_val.c = 0;
95
96 expect = old_val.value;
97 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
98 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +020099 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +0200100
101 if (rc != expect) {
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200102 /* another external call is pending */
103 return -EBUSY;
104 }
105 atomic_or(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
106 return 0;
107}
108
109static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
110{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200111 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +0200112 int rc, expect;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200113
114 atomic_andnot(CPUSTAT_ECALL_PEND, li->cpuflags);
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +0200115 read_lock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +0200116 if (vcpu->kvm->arch.use_esca) {
117 struct esca_block *sca = vcpu->kvm->arch.sca;
118 union esca_sigp_ctrl *sigp_ctrl =
119 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
120 union esca_sigp_ctrl old = *sigp_ctrl;
121
122 expect = old.value;
123 rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
124 } else {
125 struct bsca_block *sca = vcpu->kvm->arch.sca;
126 union bsca_sigp_ctrl *sigp_ctrl =
127 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
128 union bsca_sigp_ctrl old = *sigp_ctrl;
129
130 expect = old.value;
131 rc = cmpxchg(&sigp_ctrl->value, old.value, 0);
132 }
Eugene (jno) Dvurechenski5e044312015-04-22 18:08:39 +0200133 read_unlock(&vcpu->kvm->arch.sca_lock);
Eugene (jno) Dvurechenski7d43baf2015-04-22 17:09:44 +0200134 WARN_ON(rc != expect); /* cannot clear? */
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200135}
136
Dominik Dingel3c038e62013-10-07 17:11:48 +0200137int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100138{
139 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
140}
141
Cornelia Huckd8346b72012-12-20 15:32:08 +0100142static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
143{
144 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
145}
146
Cornelia Huck48a3e952012-12-20 15:32:09 +0100147static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
148{
149 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
150}
151
Carsten Otteba5c1e92008-03-25 18:47:26 +0100152static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
153{
David Hildenbrandfee0e0f2015-09-28 13:32:38 +0200154 return psw_extint_disabled(vcpu) &&
155 psw_ioint_disabled(vcpu) &&
156 psw_mchk_disabled(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100157}
158
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100159static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
160{
161 if (psw_extint_disabled(vcpu) ||
162 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
163 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +0100164 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
165 /* No timer interrupts when single stepping */
166 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100167 return 1;
168}
169
David Hildenbrandb4aec922014-12-01 15:55:42 +0100170static int ckc_irq_pending(struct kvm_vcpu *vcpu)
171{
David Hildenbrand60417fc2015-09-29 16:20:36 +0200172 if (vcpu->arch.sie_block->ckc >= kvm_s390_get_tod_clock_fast(vcpu->kvm))
David Hildenbrandb4aec922014-12-01 15:55:42 +0100173 return 0;
174 return ckc_interrupts_enabled(vcpu);
175}
176
177static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
178{
179 return !psw_extint_disabled(vcpu) &&
180 (vcpu->arch.sie_block->gcr[0] & 0x400ul);
181}
182
183static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
184{
David Hildenbrand4287f242016-02-15 09:40:12 +0100185 if (!cpu_timer_interrupts_enabled(vcpu))
186 return 0;
187 return kvm_s390_get_cpu_timer(vcpu) >> 63;
David Hildenbrandb4aec922014-12-01 15:55:42 +0100188}
189
Jens Freimann6d3da242013-07-03 15:18:35 +0200190static inline int is_ioirq(unsigned long irq_type)
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100191{
Jens Freimann6d3da242013-07-03 15:18:35 +0200192 return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
193 (irq_type <= IRQ_PEND_IO_ISC_7));
194}
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100195
Jens Freimann6d3da242013-07-03 15:18:35 +0200196static uint64_t isc_to_isc_bits(int isc)
197{
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100198 return (0x80 >> isc) << 24;
199}
200
Jens Freimann6d3da242013-07-03 15:18:35 +0200201static inline u8 int_word_to_isc(u32 int_word)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100202{
Jens Freimann6d3da242013-07-03 15:18:35 +0200203 return (int_word & 0x38000000) >> 27;
204}
205
David Hildenbrand5f94c582015-09-28 14:27:51 +0200206static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
Jens Freimann6d3da242013-07-03 15:18:35 +0200207{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200208 return vcpu->kvm->arch.float_int.pending_irqs |
209 vcpu->arch.local_int.pending_irqs;
Jens Freimann383d0b02014-07-29 15:11:49 +0200210}
211
Jens Freimann6d3da242013-07-03 15:18:35 +0200212static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
213 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200214{
Jens Freimann6d3da242013-07-03 15:18:35 +0200215 int i;
216
217 for (i = 0; i <= MAX_ISC; i++)
218 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
219 active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
220
221 return active_mask;
222}
223
224static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
225{
226 unsigned long active_mask;
227
David Hildenbrand5f94c582015-09-28 14:27:51 +0200228 active_mask = pending_irqs(vcpu);
Jens Freimannffeca0a2015-04-17 10:21:04 +0200229 if (!active_mask)
230 return 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200231
232 if (psw_extint_disabled(vcpu))
233 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200234 if (psw_ioint_disabled(vcpu))
235 active_mask &= ~IRQ_PEND_IO_MASK;
236 else
237 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200238 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
239 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
240 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
241 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
242 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
243 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
244 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
245 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200246 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
247 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200248 if (psw_mchk_disabled(vcpu))
249 active_mask &= ~IRQ_PEND_MCHK_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200250 if (!(vcpu->arch.sie_block->gcr[14] &
251 vcpu->kvm->arch.float_int.mchk.cr14))
252 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200253
David Hildenbrand6cddd432014-10-15 16:48:53 +0200254 /*
255 * STOP irqs will never be actively delivered. They are triggered via
256 * intercept requests and cleared when the stop intercept is performed.
257 */
258 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
259
Jens Freimann383d0b02014-07-29 15:11:49 +0200260 return active_mask;
261}
262
Carsten Otteba5c1e92008-03-25 18:47:26 +0100263static void __set_cpu_idle(struct kvm_vcpu *vcpu)
264{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200265 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100266 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
267}
268
269static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
270{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200271 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100272 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
273}
274
275static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
276{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200277 atomic_andnot(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
278 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100279 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100280 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
281
282 if (guestdbg_enabled(vcpu)) {
283 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
284 LCTL_CR10 | LCTL_CR11);
285 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
286 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100287}
288
289static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
290{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200291 atomic_or(flag, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100292}
293
Jens Freimann6d3da242013-07-03 15:18:35 +0200294static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
295{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200296 if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
Jens Freimann6d3da242013-07-03 15:18:35 +0200297 return;
298 else if (psw_ioint_disabled(vcpu))
299 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
300 else
301 vcpu->arch.sie_block->lctl |= LCTL_CR6;
302}
303
Jens Freimann383d0b02014-07-29 15:11:49 +0200304static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
305{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200306 if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200307 return;
308 if (psw_extint_disabled(vcpu))
309 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
310 else
311 vcpu->arch.sie_block->lctl |= LCTL_CR0;
312}
313
314static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
315{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200316 if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200317 return;
318 if (psw_mchk_disabled(vcpu))
319 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
320 else
321 vcpu->arch.sie_block->lctl |= LCTL_CR14;
322}
323
David Hildenbrand6cddd432014-10-15 16:48:53 +0200324static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
325{
326 if (kvm_s390_is_stop_irq_pending(vcpu))
327 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
328}
329
Jens Freimann6d3da242013-07-03 15:18:35 +0200330/* Set interception request for non-deliverable interrupts */
331static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200332{
Jens Freimann6d3da242013-07-03 15:18:35 +0200333 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200334 set_intercept_indicators_ext(vcpu);
335 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200336 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200337}
338
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200339static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100340{
Jens Freimann383d0b02014-07-29 15:11:49 +0200341 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200342 int rc;
343
344 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
345 0, 0);
346
347 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
348 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100349 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200350 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
351 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
352 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
353 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200354 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100355 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200356}
357
358static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
359{
Jens Freimann383d0b02014-07-29 15:11:49 +0200360 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200361 int rc;
362
363 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
364 0, 0);
365
366 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
367 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100368 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200369 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
370 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
371 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
372 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200373 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100374 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200375}
376
Jens Freimann383d0b02014-07-29 15:11:49 +0200377static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200378{
Jens Freimann383d0b02014-07-29 15:11:49 +0200379 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
380 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200381 int rc;
382
Jens Freimann383d0b02014-07-29 15:11:49 +0200383 spin_lock(&li->lock);
384 ext = li->irq.ext;
385 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
386 li->irq.ext.ext_params2 = 0;
387 spin_unlock(&li->lock);
388
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200389 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
390 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200391 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
392 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200393 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200394
395 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
396 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
397 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
398 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
399 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
400 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200401 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100402 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200403}
404
David Hildenbrandd6404de2015-10-14 16:47:36 +0200405static int __write_machine_check(struct kvm_vcpu *vcpu,
406 struct kvm_s390_mchk_info *mchk)
407{
408 unsigned long ext_sa_addr;
David Hildenbrand0319dae2016-08-03 11:18:57 +0200409 freg_t fprs[NUM_FPRS];
David Hildenbrandff5dc142015-10-14 16:57:56 +0200410 union mci mci;
David Hildenbrandd6404de2015-10-14 16:47:36 +0200411 int rc;
412
David Hildenbrandff5dc142015-10-14 16:57:56 +0200413 mci.val = mchk->mcic;
David Hildenbrand0319dae2016-08-03 11:18:57 +0200414 /* take care of lazy register loading via vcpu load/put */
415 save_fpu_regs();
416 save_access_regs(vcpu->run->s.regs.acrs);
417
David Hildenbrandd6404de2015-10-14 16:47:36 +0200418 /* Extended save area */
419 rc = read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR, &ext_sa_addr,
420 sizeof(unsigned long));
David Hildenbrandff5dc142015-10-14 16:57:56 +0200421 /* Only bits 0-53 are used for address formation */
422 ext_sa_addr &= ~0x3ffUL;
423 if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) {
424 if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs,
425 512))
426 mci.vr = 0;
427 } else {
428 mci.vr = 0;
429 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200430
431 /* General interruption information */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200432 rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200433 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
434 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
435 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
436 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
David Hildenbrandff5dc142015-10-14 16:57:56 +0200437 rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200438
439 /* Register-save areas */
David Hildenbrand0319dae2016-08-03 11:18:57 +0200440 if (MACHINE_HAS_VX) {
441 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
442 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128);
443 } else {
444 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA,
445 vcpu->run->s.regs.fprs, 128);
446 }
447 rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA,
448 vcpu->run->s.regs.gprs, 128);
449 rc |= put_guest_lc(vcpu, current->thread.fpu.fpc,
450 (u32 __user *) __LC_FP_CREG_SAVE_AREA);
451 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr,
452 (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA);
453 rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu),
454 (u64 __user *) __LC_CPU_TIMER_SAVE_AREA);
455 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8,
456 (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA);
457 rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA,
458 &vcpu->run->s.regs.acrs, 64);
459 rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA,
460 &vcpu->arch.sie_block->gcr, 128);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200461
462 /* Extended interruption information */
David Hildenbrand8953fb02016-08-03 12:25:08 +0200463 rc |= put_guest_lc(vcpu, mchk->ext_damage_code,
464 (u32 __user *) __LC_EXT_DAMAGE_CODE);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200465 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
466 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
467 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
468 sizeof(mchk->fixed_logout));
469 return rc ? -EFAULT : 0;
470}
471
Jens Freimann383d0b02014-07-29 15:11:49 +0200472static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200473{
Jens Freimann6d3da242013-07-03 15:18:35 +0200474 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200475 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200476 struct kvm_s390_mchk_info mchk = {};
Jens Freimann6d3da242013-07-03 15:18:35 +0200477 int deliver = 0;
478 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200479
Jens Freimann6d3da242013-07-03 15:18:35 +0200480 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200481 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200482 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
483 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
484 /*
485 * If there was an exigent machine check pending, then any
486 * repressible machine checks that might have been pending
487 * are indicated along with it, so always clear bits for
488 * repressible and exigent interrupts
489 */
490 mchk = li->irq.mchk;
491 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
492 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
493 memset(&li->irq.mchk, 0, sizeof(mchk));
494 deliver = 1;
495 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200496 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200497 * We indicate floating repressible conditions along with
498 * other pending conditions. Channel Report Pending and Channel
499 * Subsystem damage are the only two and and are indicated by
500 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200501 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200502 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
503 mchk.mcic |= fi->mchk.mcic;
504 mchk.cr14 |= fi->mchk.cr14;
505 memset(&fi->mchk, 0, sizeof(mchk));
506 deliver = 1;
507 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200508 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200509 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200510
Jens Freimann6d3da242013-07-03 15:18:35 +0200511 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200512 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200513 mchk.mcic);
514 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
515 KVM_S390_MCHK,
516 mchk.cr14, mchk.mcic);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200517 rc = __write_machine_check(vcpu, &mchk);
Jens Freimann6d3da242013-07-03 15:18:35 +0200518 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200519 return rc;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200520}
521
522static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
523{
Jens Freimann383d0b02014-07-29 15:11:49 +0200524 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200525 int rc;
526
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200527 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200528 vcpu->stat.deliver_restart_signal++;
529 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
530
531 rc = write_guest_lc(vcpu,
Heiko Carstensc667aea2015-12-31 10:29:00 +0100532 offsetof(struct lowcore, restart_old_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200533 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstensc667aea2015-12-31 10:29:00 +0100534 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200535 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200536 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100537 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200538}
539
Jens Freimann383d0b02014-07-29 15:11:49 +0200540static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200541{
Jens Freimann383d0b02014-07-29 15:11:49 +0200542 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
543 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200544
Jens Freimann383d0b02014-07-29 15:11:49 +0200545 spin_lock(&li->lock);
546 prefix = li->irq.prefix;
547 li->irq.prefix.address = 0;
548 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
549 spin_unlock(&li->lock);
550
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200551 vcpu->stat.deliver_prefix_signal++;
552 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
553 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200554 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200555
Jens Freimann383d0b02014-07-29 15:11:49 +0200556 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200557 return 0;
558}
559
Jens Freimann383d0b02014-07-29 15:11:49 +0200560static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200561{
Jens Freimann383d0b02014-07-29 15:11:49 +0200562 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200563 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200564 int cpu_addr;
565
566 spin_lock(&li->lock);
567 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
568 clear_bit(cpu_addr, li->sigp_emerg_pending);
569 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
570 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
571 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200572
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200573 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200574 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200575 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
576 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200577
578 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
579 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200580 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200581 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
582 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
583 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
584 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100585 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200586}
587
Jens Freimann383d0b02014-07-29 15:11:49 +0200588static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200589{
Jens Freimann383d0b02014-07-29 15:11:49 +0200590 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
591 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200592 int rc;
593
Jens Freimann383d0b02014-07-29 15:11:49 +0200594 spin_lock(&li->lock);
595 extcall = li->irq.extcall;
596 li->irq.extcall.code = 0;
597 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
598 spin_unlock(&li->lock);
599
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200600 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200601 vcpu->stat.deliver_external_call++;
602 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
603 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200604 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200605
606 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
607 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200608 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200609 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
610 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
611 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
612 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100613 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200614}
615
Jens Freimann383d0b02014-07-29 15:11:49 +0200616static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200617{
Jens Freimann383d0b02014-07-29 15:11:49 +0200618 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
619 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100620 int rc = 0, nullifying = false;
David Hildenbrand634790b2015-11-04 16:33:33 +0100621 u16 ilen;
David Hildenbrand87128362014-03-03 10:55:13 +0100622
Jens Freimann383d0b02014-07-29 15:11:49 +0200623 spin_lock(&li->lock);
624 pgm_info = li->irq.pgm;
625 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
626 memset(&li->irq.pgm, 0, sizeof(pgm_info));
627 spin_unlock(&li->lock);
628
David Hildenbrand634790b2015-11-04 16:33:33 +0100629 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100630 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
631 pgm_info.code, ilen);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200632 vcpu->stat.deliver_program_int++;
633 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200634 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200635
Jens Freimann383d0b02014-07-29 15:11:49 +0200636 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100637 case PGM_AFX_TRANSLATION:
638 case PGM_ASX_TRANSLATION:
639 case PGM_EX_TRANSLATION:
640 case PGM_LFX_TRANSLATION:
641 case PGM_LSTE_SEQUENCE:
642 case PGM_LSX_TRANSLATION:
643 case PGM_LX_TRANSLATION:
644 case PGM_PRIMARY_AUTHORITY:
645 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100646 nullifying = true;
647 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100648 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200649 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100650 (u64 *)__LC_TRANS_EXC_CODE);
651 break;
652 case PGM_ALEN_TRANSLATION:
653 case PGM_ALE_SEQUENCE:
654 case PGM_ASTE_INSTANCE:
655 case PGM_ASTE_SEQUENCE:
656 case PGM_ASTE_VALIDITY:
657 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200658 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100659 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100660 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100661 break;
662 case PGM_ASCE_TYPE:
663 case PGM_PAGE_TRANSLATION:
664 case PGM_REGION_FIRST_TRANS:
665 case PGM_REGION_SECOND_TRANS:
666 case PGM_REGION_THIRD_TRANS:
667 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200668 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100669 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200670 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100671 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200672 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100673 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100674 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100675 break;
676 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200677 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200678 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200679 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100680 (u64 *)__LC_MON_CODE);
681 break;
Eric Farman403c8642015-02-02 15:01:06 -0500682 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100683 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200684 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100685 (u32 *)__LC_DATA_EXC_CODE);
686 break;
687 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200688 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100689 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200690 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100691 (u8 *)__LC_EXC_ACCESS_ID);
692 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100693 case PGM_STACK_FULL:
694 case PGM_STACK_EMPTY:
695 case PGM_STACK_SPECIFICATION:
696 case PGM_STACK_TYPE:
697 case PGM_STACK_OPERATION:
698 case PGM_TRACE_TABEL:
699 case PGM_CRYPTO_OPERATION:
700 nullifying = true;
701 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100702 }
703
Jens Freimann383d0b02014-07-29 15:11:49 +0200704 if (pgm_info.code & PGM_PER) {
705 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100706 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200707 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100708 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200709 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100710 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200711 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100712 (u8 *) __LC_PER_ACCESS_ID);
713 }
714
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100715 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100716 kvm_s390_rewind_psw(vcpu, ilen);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100717
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100718 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
719 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100720 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
721 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200722 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100723 (u16 *)__LC_PGM_INT_CODE);
724 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
725 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
726 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
727 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100728 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200729}
David Hildenbrand87128362014-03-03 10:55:13 +0100730
Jens Freimann6d3da242013-07-03 15:18:35 +0200731static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200732{
Jens Freimann6d3da242013-07-03 15:18:35 +0200733 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
734 struct kvm_s390_ext_info ext;
735 int rc = 0;
736
737 spin_lock(&fi->lock);
738 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
739 spin_unlock(&fi->lock);
740 return 0;
741 }
742 ext = fi->srv_signal;
743 memset(&fi->srv_signal, 0, sizeof(ext));
744 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
745 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200746
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200747 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200748 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200749 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200750 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
751 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200752
753 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100754 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200755 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
756 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
757 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
758 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200759 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200760 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200761
Jens Freimann99e20002014-12-01 17:05:39 +0100762 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200763}
764
Jens Freimann6d3da242013-07-03 15:18:35 +0200765static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200766{
Jens Freimann6d3da242013-07-03 15:18:35 +0200767 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
768 struct kvm_s390_interrupt_info *inti;
769 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200770
Jens Freimann6d3da242013-07-03 15:18:35 +0200771 spin_lock(&fi->lock);
772 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
773 struct kvm_s390_interrupt_info,
774 list);
775 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200776 list_del(&inti->list);
777 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
778 }
779 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
780 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
781 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200782
Jens Freimann6d3da242013-07-03 15:18:35 +0200783 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200784 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
785 KVM_S390_INT_PFAULT_DONE, 0,
786 inti->ext.ext_params2);
787 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
788 inti->ext.ext_params2);
789
Jens Freimann6d3da242013-07-03 15:18:35 +0200790 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
791 (u16 *)__LC_EXT_INT_CODE);
792 rc |= put_guest_lc(vcpu, PFAULT_DONE,
793 (u16 *)__LC_EXT_CPU_ADDR);
794 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
795 &vcpu->arch.sie_block->gpsw,
796 sizeof(psw_t));
797 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
798 &vcpu->arch.sie_block->gpsw,
799 sizeof(psw_t));
800 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
801 (u64 *)__LC_EXT_PARAMS2);
802 kfree(inti);
803 }
Jens Freimann99e20002014-12-01 17:05:39 +0100804 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200805}
806
Jens Freimann6d3da242013-07-03 15:18:35 +0200807static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200808{
Jens Freimann6d3da242013-07-03 15:18:35 +0200809 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
810 struct kvm_s390_interrupt_info *inti;
811 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200812
Jens Freimann6d3da242013-07-03 15:18:35 +0200813 spin_lock(&fi->lock);
814 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
815 struct kvm_s390_interrupt_info,
816 list);
817 if (inti) {
818 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200819 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200820 inti->ext.ext_params, inti->ext.ext_params2);
821 vcpu->stat.deliver_virtio_interrupt++;
822 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
823 inti->type,
824 inti->ext.ext_params,
825 inti->ext.ext_params2);
826 list_del(&inti->list);
827 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
828 }
829 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
830 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
831 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200832
Jens Freimann6d3da242013-07-03 15:18:35 +0200833 if (inti) {
834 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
835 (u16 *)__LC_EXT_INT_CODE);
836 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
837 (u16 *)__LC_EXT_CPU_ADDR);
838 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
839 &vcpu->arch.sie_block->gpsw,
840 sizeof(psw_t));
841 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
842 &vcpu->arch.sie_block->gpsw,
843 sizeof(psw_t));
844 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
845 (u32 *)__LC_EXT_PARAMS);
846 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
847 (u64 *)__LC_EXT_PARAMS2);
848 kfree(inti);
849 }
Jens Freimann99e20002014-12-01 17:05:39 +0100850 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200851}
852
853static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200854 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200855{
Jens Freimann6d3da242013-07-03 15:18:35 +0200856 struct list_head *isc_list;
857 struct kvm_s390_float_interrupt *fi;
858 struct kvm_s390_interrupt_info *inti = NULL;
859 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200860
Jens Freimann6d3da242013-07-03 15:18:35 +0200861 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200862
Jens Freimann6d3da242013-07-03 15:18:35 +0200863 spin_lock(&fi->lock);
864 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
865 inti = list_first_entry_or_null(isc_list,
866 struct kvm_s390_interrupt_info,
867 list);
868 if (inti) {
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +0200869 if (inti->type & KVM_S390_INT_IO_AI_MASK)
870 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
871 else
872 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
873 inti->io.subchannel_id >> 8,
874 inti->io.subchannel_id >> 1 & 0x3,
875 inti->io.subchannel_nr);
876
Jens Freimann6d3da242013-07-03 15:18:35 +0200877 vcpu->stat.deliver_io_int++;
878 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
879 inti->type,
880 ((__u32)inti->io.subchannel_id << 16) |
881 inti->io.subchannel_nr,
882 ((__u64)inti->io.io_int_parm << 32) |
883 inti->io.io_int_word);
884 list_del(&inti->list);
885 fi->counters[FIRQ_CNTR_IO] -= 1;
886 }
887 if (list_empty(isc_list))
888 clear_bit(irq_type, &fi->pending_irqs);
889 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100890
Jens Freimann6d3da242013-07-03 15:18:35 +0200891 if (inti) {
892 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
893 (u16 *)__LC_SUBCHANNEL_ID);
894 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
895 (u16 *)__LC_SUBCHANNEL_NR);
896 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
897 (u32 *)__LC_IO_INT_PARM);
898 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
899 (u32 *)__LC_IO_INT_WORD);
900 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
901 &vcpu->arch.sie_block->gpsw,
902 sizeof(psw_t));
903 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
904 &vcpu->arch.sie_block->gpsw,
905 sizeof(psw_t));
906 kfree(inti);
907 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200908
Jens Freimann99e20002014-12-01 17:05:39 +0100909 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200910}
911
912typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
913
914static const deliver_irq_t deliver_irq_funcs[] = {
915 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200916 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200917 [IRQ_PEND_PROG] = __deliver_prog,
918 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
919 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
920 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
921 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
922 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200923 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
924 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200925 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
926 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
927 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200928};
929
David Hildenbrandea5f4962014-10-14 15:29:30 +0200930/* Check whether an external call is pending (deliverable or not) */
931int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100932{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200933 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +0100934
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200935 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200936 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100937
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200938 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100939}
940
David Hildenbrand9a022062014-08-05 17:40:47 +0200941int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100942{
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200943 if (deliverable_irqs(vcpu))
944 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100945
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200946 if (kvm_cpu_has_pending_timer(vcpu))
947 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100948
David Hildenbrandea5f4962014-10-14 15:29:30 +0200949 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200950 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +0200951 !psw_extint_disabled(vcpu) &&
952 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200953 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +0100954
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200955 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
956 return 1;
957 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100958}
959
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300960int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
961{
David Hildenbrandb4aec922014-12-01 15:55:42 +0100962 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300963}
964
David Hildenbrandb3c17f12016-02-22 14:14:50 +0100965static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
966{
967 u64 now, cputm, sltime = 0;
968
969 if (ckc_interrupts_enabled(vcpu)) {
970 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
971 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
972 /* already expired or overflow? */
973 if (!sltime || vcpu->arch.sie_block->ckc <= now)
974 return 0;
975 if (cpu_timer_interrupts_enabled(vcpu)) {
976 cputm = kvm_s390_get_cpu_timer(vcpu);
977 /* already expired? */
978 if (cputm >> 63)
979 return 0;
980 return min(sltime, tod_to_ns(cputm));
981 }
982 } else if (cpu_timer_interrupts_enabled(vcpu)) {
983 sltime = kvm_s390_get_cpu_timer(vcpu);
984 /* already expired? */
985 if (sltime >> 63)
986 return 0;
987 }
988 return sltime;
989}
990
Carsten Otteba5c1e92008-03-25 18:47:26 +0100991int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
992{
David Hildenbrandb3c17f12016-02-22 14:14:50 +0100993 u64 sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100994
995 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100996
David Hildenbrand0759d062014-05-13 16:54:32 +0200997 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +0200998 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +0200999 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +02001000
Carsten Otteba5c1e92008-03-25 18:47:26 +01001001 if (psw_interrupts_disabled(vcpu)) {
1002 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001003 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +01001004 }
1005
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001006 if (!ckc_interrupts_enabled(vcpu) &&
1007 !cpu_timer_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001008 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +01001009 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001010 goto no_timer;
1011 }
1012
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001013 sltime = __calculate_sltime(vcpu);
1014 if (!sltime)
David Hildenbrandbda343e2014-12-12 12:26:40 +01001015 return 0;
1016
1017 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001018 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001019 VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001020no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +02001021 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +02001022 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001023 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +02001024 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1025
David Hildenbrand2d00f752014-12-11 10:18:01 +01001026 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001027 return 0;
1028}
1029
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001030void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1031{
Christian Borntraeger3491caf2016-05-13 12:16:35 +02001032 /*
1033 * We cannot move this into the if, as the CPU might be already
1034 * in kvm_vcpu_block without having the waitqueue set (polling)
1035 */
1036 vcpu->valid_wakeup = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001037 if (swait_active(&vcpu->wq)) {
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001038 /*
1039 * The vcpu gave up the cpu voluntarily, mark it as a good
1040 * yield-candidate.
1041 */
1042 vcpu->preempted = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001043 swake_up(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +02001044 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001045 }
David Hildenbrandadbf1692016-05-27 22:03:52 +02001046 /*
1047 * The VCPU might not be sleeping but is executing the VSIE. Let's
1048 * kick it, so it leaves the SIE to process the request.
1049 */
1050 kvm_s390_vsie_kick(vcpu);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001051}
1052
Christian Borntraegerca872302009-05-12 17:21:49 +02001053enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1054{
1055 struct kvm_vcpu *vcpu;
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001056 u64 sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +02001057
1058 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001059 sltime = __calculate_sltime(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001060
David Hildenbrand2d00f752014-12-11 10:18:01 +01001061 /*
1062 * If the monotonic clock runs faster than the tod clock we might be
1063 * woken up too early and have to go back to sleep to avoid deadlocks.
1064 */
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001065 if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
David Hildenbrand2d00f752014-12-11 10:18:01 +01001066 return HRTIMER_RESTART;
1067 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001068 return HRTIMER_NORESTART;
1069}
Carsten Otteba5c1e92008-03-25 18:47:26 +01001070
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001071void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1072{
1073 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001074
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001075 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001076 li->pending_irqs = 0;
1077 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1078 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001079 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +01001080
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001081 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001082}
1083
Christian Borntraeger614aeab2014-08-25 12:27:29 +02001084int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001085{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001086 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001087 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +02001088 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +02001089 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +02001090 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001091
1092 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001093
Jens Freimann383d0b02014-07-29 15:11:49 +02001094 /* pending ckc conditions might have been invalidated */
1095 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +01001096 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +02001097 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1098
David Hildenbrandb4aec922014-12-01 15:55:42 +01001099 /* pending cpu timer conditions might have been invalidated */
1100 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1101 if (cpu_timer_irq_pending(vcpu))
1102 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1103
Jens Freimannffeca0a2015-04-17 10:21:04 +02001104 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001105 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +02001106 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +02001107 if (is_ioirq(irq_type)) {
1108 rc = __deliver_io(vcpu, irq_type);
1109 } else {
1110 func = deliver_irq_funcs[irq_type];
1111 if (!func) {
1112 WARN_ON_ONCE(func == NULL);
1113 clear_bit(irq_type, &li->pending_irqs);
1114 continue;
1115 }
1116 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +02001117 }
Jens Freimannffeca0a2015-04-17 10:21:04 +02001118 }
Jens Freimann383d0b02014-07-29 15:11:49 +02001119
Jens Freimann6d3da242013-07-03 15:18:35 +02001120 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +02001121
1122 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001123}
1124
Jens Freimann383d0b02014-07-29 15:11:49 +02001125static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001126{
1127 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1128
David Hildenbranded2afcf2015-07-20 10:33:03 +02001129 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1130 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1131 irq->u.pgm.code, 0);
1132
David Hildenbrand634790b2015-11-04 16:33:33 +01001133 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1134 /* auto detection if no valid ILC was given */
1135 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1136 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1137 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1138 }
1139
David Hildenbrand238293b2015-05-04 12:38:48 +02001140 if (irq->u.pgm.code == PGM_PER) {
1141 li->irq.pgm.code |= PGM_PER;
David Hildenbrand634790b2015-11-04 16:33:33 +01001142 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001143 /* only modify PER related information */
1144 li->irq.pgm.per_address = irq->u.pgm.per_address;
1145 li->irq.pgm.per_code = irq->u.pgm.per_code;
1146 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1147 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1148 } else if (!(irq->u.pgm.code & PGM_PER)) {
1149 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1150 irq->u.pgm.code;
David Hildenbrand634790b2015-11-04 16:33:33 +01001151 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001152 /* only modify non-PER information */
1153 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1154 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1155 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1156 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1157 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1158 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1159 } else {
1160 li->irq.pgm = irq->u.pgm;
1161 }
Jens Freimann91851242014-12-01 16:43:40 +01001162 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001163 return 0;
1164}
1165
Jens Freimann383d0b02014-07-29 15:11:49 +02001166static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001167{
1168 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1169
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001170 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1171 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001172 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1173 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001174 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001175
1176 li->irq.ext = irq->u.ext;
1177 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001178 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001179 return 0;
1180}
1181
Christian Borntraeger0675d922015-01-15 12:40:42 +01001182static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001183{
1184 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001185 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001186 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001187
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001188 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001189 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001190 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001191 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001192
David Hildenbrandea5f4962014-10-14 15:29:30 +02001193 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001194 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001195 return -EINVAL;
1196
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001197 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001198 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001199
David Hildenbrandb938eace2015-04-30 13:33:59 +02001200 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001201 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001202 *extcall = irq->u.extcall;
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001203 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001204 return 0;
1205}
1206
Jens Freimann383d0b02014-07-29 15:11:49 +02001207static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001208{
1209 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001210 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001211
David Hildenbranded2afcf2015-07-20 10:33:03 +02001212 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001213 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001214 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001215 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001216
David Hildenbranda3a9c592014-10-14 09:44:55 +02001217 if (!is_vcpu_stopped(vcpu))
1218 return -EBUSY;
1219
Jens Freimann383d0b02014-07-29 15:11:49 +02001220 *prefix = irq->u.prefix;
1221 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001222 return 0;
1223}
1224
David Hildenbrand6cddd432014-10-15 16:48:53 +02001225#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001226static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001227{
1228 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001229 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001230 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001231
David Hildenbranded2afcf2015-07-20 10:33:03 +02001232 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001233
David Hildenbrand28225452014-10-15 16:48:16 +02001234 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1235 return -EINVAL;
1236
David Hildenbrand6cddd432014-10-15 16:48:53 +02001237 if (is_vcpu_stopped(vcpu)) {
1238 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1239 rc = kvm_s390_store_status_unloaded(vcpu,
1240 KVM_S390_STORE_STATUS_NOADDR);
1241 return rc;
1242 }
1243
1244 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1245 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001246 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001247 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001248 return 0;
1249}
1250
1251static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001252 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001253{
1254 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1255
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001256 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001257 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001258
1259 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001260 return 0;
1261}
1262
1263static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001264 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001265{
1266 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1267
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001268 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001269 irq->u.emerg.code);
1270 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001271 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001272
David Hildenbrandb85de332015-11-05 09:38:15 +01001273 /* sending vcpu invalid */
1274 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1275 return -EINVAL;
1276
Jens Freimann49538d12014-12-18 15:48:14 +01001277 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001278 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001279 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001280 return 0;
1281}
1282
Jens Freimann383d0b02014-07-29 15:11:49 +02001283static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001284{
1285 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001286 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001287
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001288 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001289 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001290 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001291 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001292
1293 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001294 * Because repressible machine checks can be indicated along with
1295 * exigent machine checks (PoP, Chapter 11, Interruption action)
1296 * we need to combine cr14, mcic and external damage code.
1297 * Failing storage address and the logout area should not be or'ed
1298 * together, we just indicate the last occurrence of the corresponding
1299 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001300 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001301 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001302 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001303 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1304 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1305 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1306 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001307 if (mchk->mcic & MCHK_EX_MASK)
1308 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1309 else if (mchk->mcic & MCHK_REP_MASK)
1310 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001311 return 0;
1312}
1313
Jens Freimann383d0b02014-07-29 15:11:49 +02001314static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001315{
1316 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1317
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001318 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001319 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001320 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001321
1322 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001323 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001324 return 0;
1325}
1326
Jens Freimann383d0b02014-07-29 15:11:49 +02001327static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001328{
1329 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1330
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001331 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001332 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001333 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001334
1335 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001336 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001337 return 0;
1338}
1339
Jens Freimann6d3da242013-07-03 15:18:35 +02001340static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1341 int isc, u32 schid)
1342{
1343 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1344 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1345 struct kvm_s390_interrupt_info *iter;
1346 u16 id = (schid & 0xffff0000U) >> 16;
1347 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001348
Jens Freimann6d3da242013-07-03 15:18:35 +02001349 spin_lock(&fi->lock);
1350 list_for_each_entry(iter, isc_list, list) {
1351 if (schid && (id != iter->io.subchannel_id ||
1352 nr != iter->io.subchannel_nr))
1353 continue;
1354 /* found an appropriate entry */
1355 list_del_init(&iter->list);
1356 fi->counters[FIRQ_CNTR_IO] -= 1;
1357 if (list_empty(isc_list))
1358 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1359 spin_unlock(&fi->lock);
1360 return iter;
1361 }
1362 spin_unlock(&fi->lock);
1363 return NULL;
1364}
1365
1366/*
1367 * Dequeue and return an I/O interrupt matching any of the interruption
1368 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1369 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001370struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001371 u64 isc_mask, u32 schid)
1372{
1373 struct kvm_s390_interrupt_info *inti = NULL;
1374 int isc;
1375
1376 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1377 if (isc_mask & isc_to_isc_bits(isc))
1378 inti = get_io_int(kvm, isc, schid);
1379 }
1380 return inti;
1381}
1382
1383#define SCCB_MASK 0xFFFFFFF8
1384#define SCCB_EVENT_PENDING 0x3
1385
1386static int __inject_service(struct kvm *kvm,
1387 struct kvm_s390_interrupt_info *inti)
1388{
1389 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1390
1391 spin_lock(&fi->lock);
1392 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1393 /*
1394 * Early versions of the QEMU s390 bios will inject several
1395 * service interrupts after another without handling a
1396 * condition code indicating busy.
1397 * We will silently ignore those superfluous sccb values.
1398 * A future version of QEMU will take care of serialization
1399 * of servc requests
1400 */
1401 if (fi->srv_signal.ext_params & SCCB_MASK)
1402 goto out;
1403 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1404 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1405out:
1406 spin_unlock(&fi->lock);
1407 kfree(inti);
1408 return 0;
1409}
1410
1411static int __inject_virtio(struct kvm *kvm,
1412 struct kvm_s390_interrupt_info *inti)
1413{
1414 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1415
1416 spin_lock(&fi->lock);
1417 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1418 spin_unlock(&fi->lock);
1419 return -EBUSY;
1420 }
1421 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1422 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1423 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1424 spin_unlock(&fi->lock);
1425 return 0;
1426}
1427
1428static int __inject_pfault_done(struct kvm *kvm,
1429 struct kvm_s390_interrupt_info *inti)
1430{
1431 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1432
1433 spin_lock(&fi->lock);
1434 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1435 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1436 spin_unlock(&fi->lock);
1437 return -EBUSY;
1438 }
1439 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1440 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1441 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1442 spin_unlock(&fi->lock);
1443 return 0;
1444}
1445
1446#define CR_PENDING_SUBCLASS 28
1447static int __inject_float_mchk(struct kvm *kvm,
1448 struct kvm_s390_interrupt_info *inti)
1449{
1450 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1451
1452 spin_lock(&fi->lock);
1453 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1454 fi->mchk.mcic |= inti->mchk.mcic;
1455 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1456 spin_unlock(&fi->lock);
1457 kfree(inti);
1458 return 0;
1459}
1460
1461static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001462{
1463 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001464 struct list_head *list;
1465 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001466
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001467 fi = &kvm->arch.float_int;
1468 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001469 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1470 spin_unlock(&fi->lock);
1471 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001472 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001473 fi->counters[FIRQ_CNTR_IO] += 1;
1474
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +02001475 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1476 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1477 else
1478 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1479 inti->io.subchannel_id >> 8,
1480 inti->io.subchannel_id >> 1 & 0x3,
1481 inti->io.subchannel_nr);
Jens Freimann6d3da242013-07-03 15:18:35 +02001482 isc = int_word_to_isc(inti->io.io_int_word);
1483 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1484 list_add_tail(&inti->list, list);
1485 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001486 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001487 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001488}
1489
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001490/*
1491 * Find a destination VCPU for a floating irq and kick it.
1492 */
1493static void __floating_irq_kick(struct kvm *kvm, u64 type)
1494{
1495 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1496 struct kvm_s390_local_interrupt *li;
1497 struct kvm_vcpu *dst_vcpu;
1498 int sigcpu, online_vcpus, nr_tries = 0;
1499
1500 online_vcpus = atomic_read(&kvm->online_vcpus);
1501 if (!online_vcpus)
1502 return;
1503
1504 /* find idle VCPUs first, then round robin */
1505 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1506 if (sigcpu == online_vcpus) {
1507 do {
1508 sigcpu = fi->next_rr_cpu;
1509 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1510 /* avoid endless loops if all vcpus are stopped */
1511 if (nr_tries++ >= online_vcpus)
1512 return;
1513 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1514 }
1515 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1516
1517 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1518 li = &dst_vcpu->arch.local_int;
1519 spin_lock(&li->lock);
1520 switch (type) {
1521 case KVM_S390_MCHK:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001522 atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001523 break;
1524 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001525 atomic_or(CPUSTAT_IO_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001526 break;
1527 default:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001528 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001529 break;
1530 }
1531 spin_unlock(&li->lock);
1532 kvm_s390_vcpu_wakeup(dst_vcpu);
1533}
1534
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001535static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001536{
Jens Freimann6d3da242013-07-03 15:18:35 +02001537 u64 type = READ_ONCE(inti->type);
1538 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001539
Jens Freimann6d3da242013-07-03 15:18:35 +02001540 switch (type) {
1541 case KVM_S390_MCHK:
1542 rc = __inject_float_mchk(kvm, inti);
1543 break;
1544 case KVM_S390_INT_VIRTIO:
1545 rc = __inject_virtio(kvm, inti);
1546 break;
1547 case KVM_S390_INT_SERVICE:
1548 rc = __inject_service(kvm, inti);
1549 break;
1550 case KVM_S390_INT_PFAULT_DONE:
1551 rc = __inject_pfault_done(kvm, inti);
1552 break;
1553 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1554 rc = __inject_io(kvm, inti);
1555 break;
1556 default:
1557 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001558 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001559 if (rc)
1560 return rc;
1561
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001562 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001563 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001564}
1565
1566int kvm_s390_inject_vm(struct kvm *kvm,
1567 struct kvm_s390_interrupt *s390int)
1568{
1569 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001570 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001571
1572 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1573 if (!inti)
1574 return -ENOMEM;
1575
1576 inti->type = s390int->type;
1577 switch (inti->type) {
1578 case KVM_S390_INT_VIRTIO:
1579 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1580 s390int->parm, s390int->parm64);
1581 inti->ext.ext_params = s390int->parm;
1582 inti->ext.ext_params2 = s390int->parm64;
1583 break;
1584 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001585 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001586 inti->ext.ext_params = s390int->parm;
1587 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001588 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001589 inti->ext.ext_params2 = s390int->parm64;
1590 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001591 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001592 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001593 s390int->parm64);
1594 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1595 inti->mchk.mcic = s390int->parm64;
1596 break;
1597 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimannc05c4182013-10-07 16:13:45 +02001598 inti->io.subchannel_id = s390int->parm >> 16;
1599 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1600 inti->io.io_int_parm = s390int->parm64 >> 32;
1601 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1602 break;
1603 default:
1604 kfree(inti);
1605 return -EINVAL;
1606 }
1607 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1608 2);
1609
David Hildenbrand428d53b2015-01-16 12:58:09 +01001610 rc = __inject_vm(kvm, inti);
1611 if (rc)
1612 kfree(inti);
1613 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001614}
1615
David Hildenbrand15462e32015-02-04 15:59:11 +01001616int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001617 struct kvm_s390_interrupt_info *inti)
1618{
David Hildenbrand15462e32015-02-04 15:59:11 +01001619 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001620}
1621
Jens Freimann383d0b02014-07-29 15:11:49 +02001622int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1623 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001624{
Jens Freimann383d0b02014-07-29 15:11:49 +02001625 irq->type = s390int->type;
1626 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001627 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001628 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001629 return -EINVAL;
1630 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001631 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001632 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001633 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001634 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001635 case KVM_S390_SIGP_STOP:
1636 irq->u.stop.flags = s390int->parm;
1637 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001638 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001639 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001640 return -EINVAL;
1641 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001642 break;
1643 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001644 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001645 return -EINVAL;
1646 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001647 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001648 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001649 irq->u.mchk.mcic = s390int->parm64;
1650 break;
1651 }
1652 return 0;
1653}
1654
David Hildenbrand6cddd432014-10-15 16:48:53 +02001655int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1656{
1657 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1658
1659 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1660}
1661
1662void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1663{
1664 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1665
1666 spin_lock(&li->lock);
1667 li->irq.stop.flags = 0;
1668 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1669 spin_unlock(&li->lock);
1670}
1671
Jens Freimann79e87a12015-03-19 15:12:12 +01001672static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001673{
Jens Freimann383d0b02014-07-29 15:11:49 +02001674 int rc;
1675
Jens Freimann383d0b02014-07-29 15:11:49 +02001676 switch (irq->type) {
1677 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001678 rc = __inject_prog(vcpu, irq);
1679 break;
1680 case KVM_S390_SIGP_SET_PREFIX:
1681 rc = __inject_set_prefix(vcpu, irq);
1682 break;
1683 case KVM_S390_SIGP_STOP:
1684 rc = __inject_sigp_stop(vcpu, irq);
1685 break;
1686 case KVM_S390_RESTART:
1687 rc = __inject_sigp_restart(vcpu, irq);
1688 break;
1689 case KVM_S390_INT_CLOCK_COMP:
1690 rc = __inject_ckc(vcpu);
1691 break;
1692 case KVM_S390_INT_CPU_TIMER:
1693 rc = __inject_cpu_timer(vcpu);
1694 break;
1695 case KVM_S390_INT_EXTERNAL_CALL:
1696 rc = __inject_extcall(vcpu, irq);
1697 break;
1698 case KVM_S390_INT_EMERGENCY:
1699 rc = __inject_sigp_emergency(vcpu, irq);
1700 break;
1701 case KVM_S390_MCHK:
1702 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001703 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001704 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001705 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001706 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001707 case KVM_S390_INT_VIRTIO:
1708 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001709 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001710 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001711 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001712 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001713
1714 return rc;
1715}
1716
1717int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1718{
1719 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1720 int rc;
1721
1722 spin_lock(&li->lock);
1723 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001724 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001725 if (!rc)
1726 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001727 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001728}
Jens Freimannc05c4182013-10-07 16:13:45 +02001729
Jens Freimann6d3da242013-07-03 15:18:35 +02001730static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001731{
Jens Freimann6d3da242013-07-03 15:18:35 +02001732 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001733
Jens Freimann6d3da242013-07-03 15:18:35 +02001734 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001735 list_del(&inti->list);
1736 kfree(inti);
1737 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001738}
1739
Jens Freimann94aa0332015-03-16 12:17:13 +01001740static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1741 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001742{
Jens Freimann94aa0332015-03-16 12:17:13 +01001743 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001744 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001745 case KVM_S390_INT_PFAULT_INIT:
1746 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001747 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001748 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001749 break;
1750 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001751 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001752 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001753 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001754}
1755
Jens Freimann6d3da242013-07-03 15:18:35 +02001756void kvm_s390_clear_float_irqs(struct kvm *kvm)
1757{
1758 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1759 int i;
1760
1761 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001762 fi->pending_irqs = 0;
1763 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1764 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001765 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1766 clear_irq_list(&fi->lists[i]);
1767 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1768 fi->counters[i] = 0;
1769 spin_unlock(&fi->lock);
1770};
1771
Jens Freimann94aa0332015-03-16 12:17:13 +01001772static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001773{
1774 struct kvm_s390_interrupt_info *inti;
1775 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001776 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001777 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001778 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001779 int ret = 0;
1780 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001781 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001782
Jens Freimann94aa0332015-03-16 12:17:13 +01001783 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1784 return -EINVAL;
1785
1786 /*
1787 * We are already using -ENOMEM to signal
1788 * userspace it may retry with a bigger buffer,
1789 * so we need to use something else for this case
1790 */
1791 buf = vzalloc(len);
1792 if (!buf)
1793 return -ENOBUFS;
1794
1795 max_irqs = len / sizeof(struct kvm_s390_irq);
1796
Jens Freimannc05c4182013-10-07 16:13:45 +02001797 fi = &kvm->arch.float_int;
1798 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001799 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1800 list_for_each_entry(inti, &fi->lists[i], list) {
1801 if (n == max_irqs) {
1802 /* signal userspace to try again */
1803 ret = -ENOMEM;
1804 goto out;
1805 }
1806 inti_to_irq(inti, &buf[n]);
1807 n++;
1808 }
1809 }
1810 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001811 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001812 /* signal userspace to try again */
1813 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001814 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001815 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001816 irq = (struct kvm_s390_irq *) &buf[n];
1817 irq->type = KVM_S390_INT_SERVICE;
1818 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001819 n++;
1820 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001821 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1822 if (n == max_irqs) {
1823 /* signal userspace to try again */
1824 ret = -ENOMEM;
1825 goto out;
1826 }
1827 irq = (struct kvm_s390_irq *) &buf[n];
1828 irq->type = KVM_S390_MCHK;
1829 irq->u.mchk = fi->mchk;
1830 n++;
1831}
1832
1833out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001834 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001835 if (!ret && n > 0) {
1836 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1837 ret = -EFAULT;
1838 }
1839 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001840
1841 return ret < 0 ? ret : n;
1842}
1843
1844static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1845{
1846 int r;
1847
1848 switch (attr->group) {
1849 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001850 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001851 attr->attr);
1852 break;
1853 default:
1854 r = -EINVAL;
1855 }
1856
1857 return r;
1858}
1859
1860static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1861 u64 addr)
1862{
1863 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1864 void *target = NULL;
1865 void __user *source;
1866 u64 size;
1867
1868 if (get_user(inti->type, (u64 __user *)addr))
1869 return -EFAULT;
1870
1871 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001872 case KVM_S390_INT_PFAULT_INIT:
1873 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001874 case KVM_S390_INT_VIRTIO:
1875 case KVM_S390_INT_SERVICE:
1876 target = (void *) &inti->ext;
1877 source = &uptr->u.ext;
1878 size = sizeof(inti->ext);
1879 break;
1880 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1881 target = (void *) &inti->io;
1882 source = &uptr->u.io;
1883 size = sizeof(inti->io);
1884 break;
1885 case KVM_S390_MCHK:
1886 target = (void *) &inti->mchk;
1887 source = &uptr->u.mchk;
1888 size = sizeof(inti->mchk);
1889 break;
1890 default:
1891 return -EINVAL;
1892 }
1893
1894 if (copy_from_user(target, source, size))
1895 return -EFAULT;
1896
1897 return 0;
1898}
1899
1900static int enqueue_floating_irq(struct kvm_device *dev,
1901 struct kvm_device_attr *attr)
1902{
1903 struct kvm_s390_interrupt_info *inti = NULL;
1904 int r = 0;
1905 int len = attr->attr;
1906
1907 if (len % sizeof(struct kvm_s390_irq) != 0)
1908 return -EINVAL;
1909 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1910 return -EINVAL;
1911
1912 while (len >= sizeof(struct kvm_s390_irq)) {
1913 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1914 if (!inti)
1915 return -ENOMEM;
1916
1917 r = copy_irq_from_user(inti, attr->addr);
1918 if (r) {
1919 kfree(inti);
1920 return r;
1921 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001922 r = __inject_vm(dev->kvm, inti);
1923 if (r) {
1924 kfree(inti);
1925 return r;
1926 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001927 len -= sizeof(struct kvm_s390_irq);
1928 attr->addr += sizeof(struct kvm_s390_irq);
1929 }
1930
1931 return r;
1932}
1933
Cornelia Huck841b91c2013-07-15 13:36:01 +02001934static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1935{
1936 if (id >= MAX_S390_IO_ADAPTERS)
1937 return NULL;
1938 return kvm->arch.adapters[id];
1939}
1940
1941static int register_io_adapter(struct kvm_device *dev,
1942 struct kvm_device_attr *attr)
1943{
1944 struct s390_io_adapter *adapter;
1945 struct kvm_s390_io_adapter adapter_info;
1946
1947 if (copy_from_user(&adapter_info,
1948 (void __user *)attr->addr, sizeof(adapter_info)))
1949 return -EFAULT;
1950
1951 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1952 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1953 return -EINVAL;
1954
1955 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1956 if (!adapter)
1957 return -ENOMEM;
1958
1959 INIT_LIST_HEAD(&adapter->maps);
1960 init_rwsem(&adapter->maps_lock);
1961 atomic_set(&adapter->nr_maps, 0);
1962 adapter->id = adapter_info.id;
1963 adapter->isc = adapter_info.isc;
1964 adapter->maskable = adapter_info.maskable;
1965 adapter->masked = false;
1966 adapter->swap = adapter_info.swap;
1967 dev->kvm->arch.adapters[adapter->id] = adapter;
1968
1969 return 0;
1970}
1971
1972int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1973{
1974 int ret;
1975 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1976
1977 if (!adapter || !adapter->maskable)
1978 return -EINVAL;
1979 ret = adapter->masked;
1980 adapter->masked = masked;
1981 return ret;
1982}
1983
1984static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1985{
1986 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1987 struct s390_map_info *map;
1988 int ret;
1989
1990 if (!adapter || !addr)
1991 return -EINVAL;
1992
1993 map = kzalloc(sizeof(*map), GFP_KERNEL);
1994 if (!map) {
1995 ret = -ENOMEM;
1996 goto out;
1997 }
1998 INIT_LIST_HEAD(&map->list);
1999 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02002000 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02002001 if (map->addr == -EFAULT) {
2002 ret = -EFAULT;
2003 goto out;
2004 }
2005 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
2006 if (ret < 0)
2007 goto out;
2008 BUG_ON(ret != 1);
2009 down_write(&adapter->maps_lock);
2010 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2011 list_add_tail(&map->list, &adapter->maps);
2012 ret = 0;
2013 } else {
2014 put_page(map->page);
2015 ret = -EINVAL;
2016 }
2017 up_write(&adapter->maps_lock);
2018out:
2019 if (ret)
2020 kfree(map);
2021 return ret;
2022}
2023
2024static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2025{
2026 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2027 struct s390_map_info *map, *tmp;
2028 int found = 0;
2029
2030 if (!adapter || !addr)
2031 return -EINVAL;
2032
2033 down_write(&adapter->maps_lock);
2034 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2035 if (map->guest_addr == addr) {
2036 found = 1;
2037 atomic_dec(&adapter->nr_maps);
2038 list_del(&map->list);
2039 put_page(map->page);
2040 kfree(map);
2041 break;
2042 }
2043 }
2044 up_write(&adapter->maps_lock);
2045
2046 return found ? 0 : -EINVAL;
2047}
2048
2049void kvm_s390_destroy_adapters(struct kvm *kvm)
2050{
2051 int i;
2052 struct s390_map_info *map, *tmp;
2053
2054 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2055 if (!kvm->arch.adapters[i])
2056 continue;
2057 list_for_each_entry_safe(map, tmp,
2058 &kvm->arch.adapters[i]->maps, list) {
2059 list_del(&map->list);
2060 put_page(map->page);
2061 kfree(map);
2062 }
2063 kfree(kvm->arch.adapters[i]);
2064 }
2065}
2066
2067static int modify_io_adapter(struct kvm_device *dev,
2068 struct kvm_device_attr *attr)
2069{
2070 struct kvm_s390_io_adapter_req req;
2071 struct s390_io_adapter *adapter;
2072 int ret;
2073
2074 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2075 return -EFAULT;
2076
2077 adapter = get_io_adapter(dev->kvm, req.id);
2078 if (!adapter)
2079 return -EINVAL;
2080 switch (req.type) {
2081 case KVM_S390_IO_ADAPTER_MASK:
2082 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2083 if (ret > 0)
2084 ret = 0;
2085 break;
2086 case KVM_S390_IO_ADAPTER_MAP:
2087 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2088 break;
2089 case KVM_S390_IO_ADAPTER_UNMAP:
2090 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2091 break;
2092 default:
2093 ret = -EINVAL;
2094 }
2095
2096 return ret;
2097}
2098
Halil Pasic6d28f782016-01-25 19:10:40 +01002099static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2100
2101{
2102 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2103 u32 schid;
2104
2105 if (attr->flags)
2106 return -EINVAL;
2107 if (attr->attr != sizeof(schid))
2108 return -EINVAL;
2109 if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2110 return -EFAULT;
2111 kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2112 /*
2113 * If userspace is conforming to the architecture, we can have at most
2114 * one pending I/O interrupt per subchannel, so this is effectively a
2115 * clear all.
2116 */
2117 return 0;
2118}
2119
Jens Freimannc05c4182013-10-07 16:13:45 +02002120static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2121{
2122 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002123 unsigned int i;
2124 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02002125
2126 switch (attr->group) {
2127 case KVM_DEV_FLIC_ENQUEUE:
2128 r = enqueue_floating_irq(dev, attr);
2129 break;
2130 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01002131 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02002132 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002133 case KVM_DEV_FLIC_APF_ENABLE:
2134 dev->kvm->arch.gmap->pfault_enabled = 1;
2135 break;
2136 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2137 dev->kvm->arch.gmap->pfault_enabled = 0;
2138 /*
2139 * Make sure no async faults are in transition when
2140 * clearing the queues. So we don't need to worry
2141 * about late coming workers.
2142 */
2143 synchronize_srcu(&dev->kvm->srcu);
2144 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2145 kvm_clear_async_pf_completion_queue(vcpu);
2146 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002147 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2148 r = register_io_adapter(dev, attr);
2149 break;
2150 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2151 r = modify_io_adapter(dev, attr);
2152 break;
Halil Pasic6d28f782016-01-25 19:10:40 +01002153 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2154 r = clear_io_irq(dev->kvm, attr);
2155 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002156 default:
2157 r = -EINVAL;
2158 }
2159
2160 return r;
2161}
2162
Halil Pasic4f129852016-02-25 12:44:17 +01002163static int flic_has_attr(struct kvm_device *dev,
2164 struct kvm_device_attr *attr)
2165{
2166 switch (attr->group) {
2167 case KVM_DEV_FLIC_GET_ALL_IRQS:
2168 case KVM_DEV_FLIC_ENQUEUE:
2169 case KVM_DEV_FLIC_CLEAR_IRQS:
2170 case KVM_DEV_FLIC_APF_ENABLE:
2171 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2172 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2173 case KVM_DEV_FLIC_ADAPTER_MODIFY:
Halil Pasic6d28f782016-01-25 19:10:40 +01002174 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
Halil Pasic4f129852016-02-25 12:44:17 +01002175 return 0;
2176 }
2177 return -ENXIO;
2178}
2179
Jens Freimannc05c4182013-10-07 16:13:45 +02002180static int flic_create(struct kvm_device *dev, u32 type)
2181{
2182 if (!dev)
2183 return -EINVAL;
2184 if (dev->kvm->arch.flic)
2185 return -EINVAL;
2186 dev->kvm->arch.flic = dev;
2187 return 0;
2188}
2189
2190static void flic_destroy(struct kvm_device *dev)
2191{
2192 dev->kvm->arch.flic = NULL;
2193 kfree(dev);
2194}
2195
2196/* s390 floating irq controller (flic) */
2197struct kvm_device_ops kvm_flic_ops = {
2198 .name = "kvm-flic",
2199 .get_attr = flic_get_attr,
2200 .set_attr = flic_set_attr,
Halil Pasic4f129852016-02-25 12:44:17 +01002201 .has_attr = flic_has_attr,
Jens Freimannc05c4182013-10-07 16:13:45 +02002202 .create = flic_create,
2203 .destroy = flic_destroy,
2204};
Cornelia Huck84223592013-07-15 13:36:01 +02002205
2206static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2207{
2208 unsigned long bit;
2209
2210 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2211
2212 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2213}
2214
2215static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2216 u64 addr)
2217{
2218 struct s390_map_info *map;
2219
2220 if (!adapter)
2221 return NULL;
2222
2223 list_for_each_entry(map, &adapter->maps, list) {
2224 if (map->guest_addr == addr)
2225 return map;
2226 }
2227 return NULL;
2228}
2229
2230static int adapter_indicators_set(struct kvm *kvm,
2231 struct s390_io_adapter *adapter,
2232 struct kvm_s390_adapter_int *adapter_int)
2233{
2234 unsigned long bit;
2235 int summary_set, idx;
2236 struct s390_map_info *info;
2237 void *map;
2238
2239 info = get_map_info(adapter, adapter_int->ind_addr);
2240 if (!info)
2241 return -1;
2242 map = page_address(info->page);
2243 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2244 set_bit(bit, map);
2245 idx = srcu_read_lock(&kvm->srcu);
2246 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2247 set_page_dirty_lock(info->page);
2248 info = get_map_info(adapter, adapter_int->summary_addr);
2249 if (!info) {
2250 srcu_read_unlock(&kvm->srcu, idx);
2251 return -1;
2252 }
2253 map = page_address(info->page);
2254 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2255 adapter->swap);
2256 summary_set = test_and_set_bit(bit, map);
2257 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2258 set_page_dirty_lock(info->page);
2259 srcu_read_unlock(&kvm->srcu, idx);
2260 return summary_set ? 0 : 1;
2261}
2262
2263/*
2264 * < 0 - not injected due to error
2265 * = 0 - coalesced, summary indicator already active
2266 * > 0 - injected interrupt
2267 */
2268static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2269 struct kvm *kvm, int irq_source_id, int level,
2270 bool line_status)
2271{
2272 int ret;
2273 struct s390_io_adapter *adapter;
2274
2275 /* We're only interested in the 0->1 transition. */
2276 if (!level)
2277 return 0;
2278 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2279 if (!adapter)
2280 return -1;
2281 down_read(&adapter->maps_lock);
2282 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2283 up_read(&adapter->maps_lock);
2284 if ((ret > 0) && !adapter->masked) {
2285 struct kvm_s390_interrupt s390int = {
2286 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2287 .parm = 0,
2288 .parm64 = (adapter->isc << 27) | 0x80000000,
2289 };
2290 ret = kvm_s390_inject_vm(kvm, &s390int);
2291 if (ret == 0)
2292 ret = 1;
2293 }
2294 return ret;
2295}
2296
Radim Krčmářc63cf532016-07-12 22:09:26 +02002297int kvm_set_routing_entry(struct kvm *kvm,
2298 struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002299 const struct kvm_irq_routing_entry *ue)
2300{
2301 int ret;
2302
2303 switch (ue->type) {
2304 case KVM_IRQ_ROUTING_S390_ADAPTER:
2305 e->set = set_adapter_int;
2306 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2307 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2308 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2309 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2310 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2311 ret = 0;
2312 break;
2313 default:
2314 ret = -EINVAL;
2315 }
2316
2317 return ret;
2318}
2319
2320int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2321 int irq_source_id, int level, bool line_status)
2322{
2323 return -EINVAL;
2324}
Jens Freimann816c7662014-11-24 17:13:46 +01002325
2326int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2327{
2328 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2329 struct kvm_s390_irq *buf;
2330 int r = 0;
2331 int n;
2332
2333 buf = vmalloc(len);
2334 if (!buf)
2335 return -ENOMEM;
2336
2337 if (copy_from_user((void *) buf, irqstate, len)) {
2338 r = -EFAULT;
2339 goto out_free;
2340 }
2341
2342 /*
2343 * Don't allow setting the interrupt state
2344 * when there are already interrupts pending
2345 */
2346 spin_lock(&li->lock);
2347 if (li->pending_irqs) {
2348 r = -EBUSY;
2349 goto out_unlock;
2350 }
2351
2352 for (n = 0; n < len / sizeof(*buf); n++) {
2353 r = do_inject_vcpu(vcpu, &buf[n]);
2354 if (r)
2355 break;
2356 }
2357
2358out_unlock:
2359 spin_unlock(&li->lock);
2360out_free:
2361 vfree(buf);
2362
2363 return r;
2364}
2365
2366static void store_local_irq(struct kvm_s390_local_interrupt *li,
2367 struct kvm_s390_irq *irq,
2368 unsigned long irq_type)
2369{
2370 switch (irq_type) {
2371 case IRQ_PEND_MCHK_EX:
2372 case IRQ_PEND_MCHK_REP:
2373 irq->type = KVM_S390_MCHK;
2374 irq->u.mchk = li->irq.mchk;
2375 break;
2376 case IRQ_PEND_PROG:
2377 irq->type = KVM_S390_PROGRAM_INT;
2378 irq->u.pgm = li->irq.pgm;
2379 break;
2380 case IRQ_PEND_PFAULT_INIT:
2381 irq->type = KVM_S390_INT_PFAULT_INIT;
2382 irq->u.ext = li->irq.ext;
2383 break;
2384 case IRQ_PEND_EXT_EXTERNAL:
2385 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2386 irq->u.extcall = li->irq.extcall;
2387 break;
2388 case IRQ_PEND_EXT_CLOCK_COMP:
2389 irq->type = KVM_S390_INT_CLOCK_COMP;
2390 break;
2391 case IRQ_PEND_EXT_CPU_TIMER:
2392 irq->type = KVM_S390_INT_CPU_TIMER;
2393 break;
2394 case IRQ_PEND_SIGP_STOP:
2395 irq->type = KVM_S390_SIGP_STOP;
2396 irq->u.stop = li->irq.stop;
2397 break;
2398 case IRQ_PEND_RESTART:
2399 irq->type = KVM_S390_RESTART;
2400 break;
2401 case IRQ_PEND_SET_PREFIX:
2402 irq->type = KVM_S390_SIGP_SET_PREFIX;
2403 irq->u.prefix = li->irq.prefix;
2404 break;
2405 }
2406}
2407
2408int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2409{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002410 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002411 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2412 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2413 unsigned long pending_irqs;
2414 struct kvm_s390_irq irq;
2415 unsigned long irq_type;
2416 int cpuaddr;
2417 int n = 0;
2418
2419 spin_lock(&li->lock);
2420 pending_irqs = li->pending_irqs;
2421 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2422 sizeof(sigp_emerg_pending));
2423 spin_unlock(&li->lock);
2424
2425 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2426 memset(&irq, 0, sizeof(irq));
2427 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2428 continue;
2429 if (n + sizeof(irq) > len)
2430 return -ENOBUFS;
2431 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2432 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2433 return -EFAULT;
2434 n += sizeof(irq);
2435 }
2436
2437 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2438 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2439 memset(&irq, 0, sizeof(irq));
2440 if (n + sizeof(irq) > len)
2441 return -ENOBUFS;
2442 irq.type = KVM_S390_INT_EMERGENCY;
2443 irq.u.emerg.code = cpuaddr;
2444 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2445 return -EFAULT;
2446 n += sizeof(irq);
2447 }
2448 }
2449
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002450 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002451 if (n + sizeof(irq) > len)
2452 return -ENOBUFS;
2453 memset(&irq, 0, sizeof(irq));
2454 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002455 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002456 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2457 return -EFAULT;
2458 n += sizeof(irq);
2459 }
2460
2461 return n;
2462}