blob: c0ef625b6765331a5619d5765508752688463cbf [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 */
463 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
464 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
465 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
466 sizeof(mchk->fixed_logout));
467 return rc ? -EFAULT : 0;
468}
469
Jens Freimann383d0b02014-07-29 15:11:49 +0200470static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200471{
Jens Freimann6d3da242013-07-03 15:18:35 +0200472 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200473 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200474 struct kvm_s390_mchk_info mchk = {};
Jens Freimann6d3da242013-07-03 15:18:35 +0200475 int deliver = 0;
476 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200477
Jens Freimann6d3da242013-07-03 15:18:35 +0200478 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200479 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200480 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
481 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
482 /*
483 * If there was an exigent machine check pending, then any
484 * repressible machine checks that might have been pending
485 * are indicated along with it, so always clear bits for
486 * repressible and exigent interrupts
487 */
488 mchk = li->irq.mchk;
489 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
490 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
491 memset(&li->irq.mchk, 0, sizeof(mchk));
492 deliver = 1;
493 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200494 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200495 * We indicate floating repressible conditions along with
496 * other pending conditions. Channel Report Pending and Channel
497 * Subsystem damage are the only two and and are indicated by
498 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200499 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200500 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
501 mchk.mcic |= fi->mchk.mcic;
502 mchk.cr14 |= fi->mchk.cr14;
503 memset(&fi->mchk, 0, sizeof(mchk));
504 deliver = 1;
505 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200506 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200507 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200508
Jens Freimann6d3da242013-07-03 15:18:35 +0200509 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200510 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200511 mchk.mcic);
512 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
513 KVM_S390_MCHK,
514 mchk.cr14, mchk.mcic);
David Hildenbrandd6404de2015-10-14 16:47:36 +0200515 rc = __write_machine_check(vcpu, &mchk);
Jens Freimann6d3da242013-07-03 15:18:35 +0200516 }
David Hildenbrandd6404de2015-10-14 16:47:36 +0200517 return rc;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200518}
519
520static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
521{
Jens Freimann383d0b02014-07-29 15:11:49 +0200522 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200523 int rc;
524
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200525 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200526 vcpu->stat.deliver_restart_signal++;
527 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
528
529 rc = write_guest_lc(vcpu,
Heiko Carstensc667aea2015-12-31 10:29:00 +0100530 offsetof(struct lowcore, restart_old_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200531 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Heiko Carstensc667aea2015-12-31 10:29:00 +0100532 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200533 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200534 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100535 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200536}
537
Jens Freimann383d0b02014-07-29 15:11:49 +0200538static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200539{
Jens Freimann383d0b02014-07-29 15:11:49 +0200540 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
541 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200542
Jens Freimann383d0b02014-07-29 15:11:49 +0200543 spin_lock(&li->lock);
544 prefix = li->irq.prefix;
545 li->irq.prefix.address = 0;
546 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
547 spin_unlock(&li->lock);
548
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200549 vcpu->stat.deliver_prefix_signal++;
550 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
551 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200552 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200553
Jens Freimann383d0b02014-07-29 15:11:49 +0200554 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200555 return 0;
556}
557
Jens Freimann383d0b02014-07-29 15:11:49 +0200558static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200559{
Jens Freimann383d0b02014-07-29 15:11:49 +0200560 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200561 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200562 int cpu_addr;
563
564 spin_lock(&li->lock);
565 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
566 clear_bit(cpu_addr, li->sigp_emerg_pending);
567 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
568 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
569 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200570
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200571 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200572 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200573 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
574 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200575
576 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
577 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200578 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200579 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
580 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
581 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
582 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100583 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200584}
585
Jens Freimann383d0b02014-07-29 15:11:49 +0200586static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200587{
Jens Freimann383d0b02014-07-29 15:11:49 +0200588 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
589 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200590 int rc;
591
Jens Freimann383d0b02014-07-29 15:11:49 +0200592 spin_lock(&li->lock);
593 extcall = li->irq.extcall;
594 li->irq.extcall.code = 0;
595 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
596 spin_unlock(&li->lock);
597
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200598 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200599 vcpu->stat.deliver_external_call++;
600 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
601 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200602 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200603
604 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
605 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200606 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200607 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
608 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
609 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
610 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100611 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200612}
613
Jens Freimann383d0b02014-07-29 15:11:49 +0200614static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200615{
Jens Freimann383d0b02014-07-29 15:11:49 +0200616 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
617 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100618 int rc = 0, nullifying = false;
David Hildenbrand634790b2015-11-04 16:33:33 +0100619 u16 ilen;
David Hildenbrand87128362014-03-03 10:55:13 +0100620
Jens Freimann383d0b02014-07-29 15:11:49 +0200621 spin_lock(&li->lock);
622 pgm_info = li->irq.pgm;
623 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
624 memset(&li->irq.pgm, 0, sizeof(pgm_info));
625 spin_unlock(&li->lock);
626
David Hildenbrand634790b2015-11-04 16:33:33 +0100627 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100628 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
629 pgm_info.code, ilen);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200630 vcpu->stat.deliver_program_int++;
631 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200632 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200633
Jens Freimann383d0b02014-07-29 15:11:49 +0200634 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100635 case PGM_AFX_TRANSLATION:
636 case PGM_ASX_TRANSLATION:
637 case PGM_EX_TRANSLATION:
638 case PGM_LFX_TRANSLATION:
639 case PGM_LSTE_SEQUENCE:
640 case PGM_LSX_TRANSLATION:
641 case PGM_LX_TRANSLATION:
642 case PGM_PRIMARY_AUTHORITY:
643 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100644 nullifying = true;
645 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100646 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200647 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100648 (u64 *)__LC_TRANS_EXC_CODE);
649 break;
650 case PGM_ALEN_TRANSLATION:
651 case PGM_ALE_SEQUENCE:
652 case PGM_ASTE_INSTANCE:
653 case PGM_ASTE_SEQUENCE:
654 case PGM_ASTE_VALIDITY:
655 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200656 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100657 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100658 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100659 break;
660 case PGM_ASCE_TYPE:
661 case PGM_PAGE_TRANSLATION:
662 case PGM_REGION_FIRST_TRANS:
663 case PGM_REGION_SECOND_TRANS:
664 case PGM_REGION_THIRD_TRANS:
665 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200666 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100667 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200668 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100669 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200670 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100671 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100672 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100673 break;
674 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200675 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200676 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200677 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100678 (u64 *)__LC_MON_CODE);
679 break;
Eric Farman403c8642015-02-02 15:01:06 -0500680 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100681 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200682 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100683 (u32 *)__LC_DATA_EXC_CODE);
684 break;
685 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200686 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100687 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200688 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100689 (u8 *)__LC_EXC_ACCESS_ID);
690 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100691 case PGM_STACK_FULL:
692 case PGM_STACK_EMPTY:
693 case PGM_STACK_SPECIFICATION:
694 case PGM_STACK_TYPE:
695 case PGM_STACK_OPERATION:
696 case PGM_TRACE_TABEL:
697 case PGM_CRYPTO_OPERATION:
698 nullifying = true;
699 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100700 }
701
Jens Freimann383d0b02014-07-29 15:11:49 +0200702 if (pgm_info.code & PGM_PER) {
703 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100704 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200705 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100706 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200707 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100708 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200709 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100710 (u8 *) __LC_PER_ACCESS_ID);
711 }
712
David Hildenbrandeaa4f412015-11-04 16:46:55 +0100713 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100714 kvm_s390_rewind_psw(vcpu, ilen);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100715
David Hildenbrand0e8bc062015-11-04 13:47:58 +0100716 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
717 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100718 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
719 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200720 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100721 (u16 *)__LC_PGM_INT_CODE);
722 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
723 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
724 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
725 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100726 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200727}
David Hildenbrand87128362014-03-03 10:55:13 +0100728
Jens Freimann6d3da242013-07-03 15:18:35 +0200729static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200730{
Jens Freimann6d3da242013-07-03 15:18:35 +0200731 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
732 struct kvm_s390_ext_info ext;
733 int rc = 0;
734
735 spin_lock(&fi->lock);
736 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
737 spin_unlock(&fi->lock);
738 return 0;
739 }
740 ext = fi->srv_signal;
741 memset(&fi->srv_signal, 0, sizeof(ext));
742 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
743 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200744
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200745 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200746 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200747 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200748 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
749 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200750
751 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100752 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200753 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
754 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
755 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
756 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200757 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200758 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200759
Jens Freimann99e20002014-12-01 17:05:39 +0100760 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200761}
762
Jens Freimann6d3da242013-07-03 15:18:35 +0200763static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200764{
Jens Freimann6d3da242013-07-03 15:18:35 +0200765 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
766 struct kvm_s390_interrupt_info *inti;
767 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200768
Jens Freimann6d3da242013-07-03 15:18:35 +0200769 spin_lock(&fi->lock);
770 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
771 struct kvm_s390_interrupt_info,
772 list);
773 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200774 list_del(&inti->list);
775 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
776 }
777 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
778 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
779 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200780
Jens Freimann6d3da242013-07-03 15:18:35 +0200781 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200782 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
783 KVM_S390_INT_PFAULT_DONE, 0,
784 inti->ext.ext_params2);
785 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
786 inti->ext.ext_params2);
787
Jens Freimann6d3da242013-07-03 15:18:35 +0200788 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
789 (u16 *)__LC_EXT_INT_CODE);
790 rc |= put_guest_lc(vcpu, PFAULT_DONE,
791 (u16 *)__LC_EXT_CPU_ADDR);
792 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
793 &vcpu->arch.sie_block->gpsw,
794 sizeof(psw_t));
795 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
796 &vcpu->arch.sie_block->gpsw,
797 sizeof(psw_t));
798 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
799 (u64 *)__LC_EXT_PARAMS2);
800 kfree(inti);
801 }
Jens Freimann99e20002014-12-01 17:05:39 +0100802 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200803}
804
Jens Freimann6d3da242013-07-03 15:18:35 +0200805static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200806{
Jens Freimann6d3da242013-07-03 15:18:35 +0200807 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
808 struct kvm_s390_interrupt_info *inti;
809 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200810
Jens Freimann6d3da242013-07-03 15:18:35 +0200811 spin_lock(&fi->lock);
812 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
813 struct kvm_s390_interrupt_info,
814 list);
815 if (inti) {
816 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200817 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200818 inti->ext.ext_params, inti->ext.ext_params2);
819 vcpu->stat.deliver_virtio_interrupt++;
820 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
821 inti->type,
822 inti->ext.ext_params,
823 inti->ext.ext_params2);
824 list_del(&inti->list);
825 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
826 }
827 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
828 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
829 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200830
Jens Freimann6d3da242013-07-03 15:18:35 +0200831 if (inti) {
832 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
833 (u16 *)__LC_EXT_INT_CODE);
834 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
835 (u16 *)__LC_EXT_CPU_ADDR);
836 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
837 &vcpu->arch.sie_block->gpsw,
838 sizeof(psw_t));
839 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
840 &vcpu->arch.sie_block->gpsw,
841 sizeof(psw_t));
842 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
843 (u32 *)__LC_EXT_PARAMS);
844 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
845 (u64 *)__LC_EXT_PARAMS2);
846 kfree(inti);
847 }
Jens Freimann99e20002014-12-01 17:05:39 +0100848 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200849}
850
851static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200852 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200853{
Jens Freimann6d3da242013-07-03 15:18:35 +0200854 struct list_head *isc_list;
855 struct kvm_s390_float_interrupt *fi;
856 struct kvm_s390_interrupt_info *inti = NULL;
857 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200858
Jens Freimann6d3da242013-07-03 15:18:35 +0200859 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200860
Jens Freimann6d3da242013-07-03 15:18:35 +0200861 spin_lock(&fi->lock);
862 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
863 inti = list_first_entry_or_null(isc_list,
864 struct kvm_s390_interrupt_info,
865 list);
866 if (inti) {
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +0200867 if (inti->type & KVM_S390_INT_IO_AI_MASK)
868 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
869 else
870 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
871 inti->io.subchannel_id >> 8,
872 inti->io.subchannel_id >> 1 & 0x3,
873 inti->io.subchannel_nr);
874
Jens Freimann6d3da242013-07-03 15:18:35 +0200875 vcpu->stat.deliver_io_int++;
876 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
877 inti->type,
878 ((__u32)inti->io.subchannel_id << 16) |
879 inti->io.subchannel_nr,
880 ((__u64)inti->io.io_int_parm << 32) |
881 inti->io.io_int_word);
882 list_del(&inti->list);
883 fi->counters[FIRQ_CNTR_IO] -= 1;
884 }
885 if (list_empty(isc_list))
886 clear_bit(irq_type, &fi->pending_irqs);
887 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100888
Jens Freimann6d3da242013-07-03 15:18:35 +0200889 if (inti) {
890 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
891 (u16 *)__LC_SUBCHANNEL_ID);
892 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
893 (u16 *)__LC_SUBCHANNEL_NR);
894 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
895 (u32 *)__LC_IO_INT_PARM);
896 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
897 (u32 *)__LC_IO_INT_WORD);
898 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
899 &vcpu->arch.sie_block->gpsw,
900 sizeof(psw_t));
901 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
902 &vcpu->arch.sie_block->gpsw,
903 sizeof(psw_t));
904 kfree(inti);
905 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200906
Jens Freimann99e20002014-12-01 17:05:39 +0100907 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200908}
909
910typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
911
912static const deliver_irq_t deliver_irq_funcs[] = {
913 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200914 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200915 [IRQ_PEND_PROG] = __deliver_prog,
916 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
917 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
918 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
919 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
920 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200921 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
922 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200923 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
924 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
925 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200926};
927
David Hildenbrandea5f4962014-10-14 15:29:30 +0200928/* Check whether an external call is pending (deliverable or not) */
929int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100930{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200931 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +0100932
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200933 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200934 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100935
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200936 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100937}
938
David Hildenbrand9a022062014-08-05 17:40:47 +0200939int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100940{
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200941 if (deliverable_irqs(vcpu))
942 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100943
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200944 if (kvm_cpu_has_pending_timer(vcpu))
945 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100946
David Hildenbrandea5f4962014-10-14 15:29:30 +0200947 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200948 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +0200949 !psw_extint_disabled(vcpu) &&
950 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200951 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +0100952
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200953 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
954 return 1;
955 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100956}
957
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300958int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
959{
David Hildenbrandb4aec922014-12-01 15:55:42 +0100960 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300961}
962
David Hildenbrandb3c17f12016-02-22 14:14:50 +0100963static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
964{
965 u64 now, cputm, sltime = 0;
966
967 if (ckc_interrupts_enabled(vcpu)) {
968 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
969 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
970 /* already expired or overflow? */
971 if (!sltime || vcpu->arch.sie_block->ckc <= now)
972 return 0;
973 if (cpu_timer_interrupts_enabled(vcpu)) {
974 cputm = kvm_s390_get_cpu_timer(vcpu);
975 /* already expired? */
976 if (cputm >> 63)
977 return 0;
978 return min(sltime, tod_to_ns(cputm));
979 }
980 } else if (cpu_timer_interrupts_enabled(vcpu)) {
981 sltime = kvm_s390_get_cpu_timer(vcpu);
982 /* already expired? */
983 if (sltime >> 63)
984 return 0;
985 }
986 return sltime;
987}
988
Carsten Otteba5c1e92008-03-25 18:47:26 +0100989int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
990{
David Hildenbrandb3c17f12016-02-22 14:14:50 +0100991 u64 sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100992
993 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100994
David Hildenbrand0759d062014-05-13 16:54:32 +0200995 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +0200996 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +0200997 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200998
Carsten Otteba5c1e92008-03-25 18:47:26 +0100999 if (psw_interrupts_disabled(vcpu)) {
1000 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +01001001 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +01001002 }
1003
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001004 if (!ckc_interrupts_enabled(vcpu) &&
1005 !cpu_timer_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001006 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +01001007 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001008 goto no_timer;
1009 }
1010
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001011 sltime = __calculate_sltime(vcpu);
1012 if (!sltime)
David Hildenbrandbda343e2014-12-12 12:26:40 +01001013 return 0;
1014
1015 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001016 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001017 VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001018no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +02001019 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +02001020 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001021 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +02001022 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1023
David Hildenbrand2d00f752014-12-11 10:18:01 +01001024 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001025 return 0;
1026}
1027
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001028void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1029{
Christian Borntraeger3491caf2016-05-13 12:16:35 +02001030 /*
1031 * We cannot move this into the if, as the CPU might be already
1032 * in kvm_vcpu_block without having the waitqueue set (polling)
1033 */
1034 vcpu->valid_wakeup = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001035 if (swait_active(&vcpu->wq)) {
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001036 /*
1037 * The vcpu gave up the cpu voluntarily, mark it as a good
1038 * yield-candidate.
1039 */
1040 vcpu->preempted = true;
Marcelo Tosatti85773702016-02-19 09:46:39 +01001041 swake_up(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +02001042 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001043 }
David Hildenbrandadbf1692016-05-27 22:03:52 +02001044 /*
1045 * The VCPU might not be sleeping but is executing the VSIE. Let's
1046 * kick it, so it leaves the SIE to process the request.
1047 */
1048 kvm_s390_vsie_kick(vcpu);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001049}
1050
Christian Borntraegerca872302009-05-12 17:21:49 +02001051enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1052{
1053 struct kvm_vcpu *vcpu;
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001054 u64 sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +02001055
1056 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001057 sltime = __calculate_sltime(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001058
David Hildenbrand2d00f752014-12-11 10:18:01 +01001059 /*
1060 * If the monotonic clock runs faster than the tod clock we might be
1061 * woken up too early and have to go back to sleep to avoid deadlocks.
1062 */
David Hildenbrandb3c17f12016-02-22 14:14:50 +01001063 if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
David Hildenbrand2d00f752014-12-11 10:18:01 +01001064 return HRTIMER_RESTART;
1065 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +02001066 return HRTIMER_NORESTART;
1067}
Carsten Otteba5c1e92008-03-25 18:47:26 +01001068
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001069void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1070{
1071 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001072
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001073 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001074 li->pending_irqs = 0;
1075 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1076 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001077 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +01001078
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001079 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +01001080}
1081
Christian Borntraeger614aeab2014-08-25 12:27:29 +02001082int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001083{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001084 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001085 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +02001086 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +02001087 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +02001088 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001089
1090 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001091
Jens Freimann383d0b02014-07-29 15:11:49 +02001092 /* pending ckc conditions might have been invalidated */
1093 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +01001094 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +02001095 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1096
David Hildenbrandb4aec922014-12-01 15:55:42 +01001097 /* pending cpu timer conditions might have been invalidated */
1098 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1099 if (cpu_timer_irq_pending(vcpu))
1100 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1101
Jens Freimannffeca0a2015-04-17 10:21:04 +02001102 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +02001103 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +02001104 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +02001105 if (is_ioirq(irq_type)) {
1106 rc = __deliver_io(vcpu, irq_type);
1107 } else {
1108 func = deliver_irq_funcs[irq_type];
1109 if (!func) {
1110 WARN_ON_ONCE(func == NULL);
1111 clear_bit(irq_type, &li->pending_irqs);
1112 continue;
1113 }
1114 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +02001115 }
Jens Freimannffeca0a2015-04-17 10:21:04 +02001116 }
Jens Freimann383d0b02014-07-29 15:11:49 +02001117
Jens Freimann6d3da242013-07-03 15:18:35 +02001118 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +02001119
1120 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001121}
1122
Jens Freimann383d0b02014-07-29 15:11:49 +02001123static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001124{
1125 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1126
David Hildenbranded2afcf2015-07-20 10:33:03 +02001127 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1128 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1129 irq->u.pgm.code, 0);
1130
David Hildenbrand634790b2015-11-04 16:33:33 +01001131 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1132 /* auto detection if no valid ILC was given */
1133 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1134 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1135 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1136 }
1137
David Hildenbrand238293b2015-05-04 12:38:48 +02001138 if (irq->u.pgm.code == PGM_PER) {
1139 li->irq.pgm.code |= PGM_PER;
David Hildenbrand634790b2015-11-04 16:33:33 +01001140 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001141 /* only modify PER related information */
1142 li->irq.pgm.per_address = irq->u.pgm.per_address;
1143 li->irq.pgm.per_code = irq->u.pgm.per_code;
1144 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1145 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1146 } else if (!(irq->u.pgm.code & PGM_PER)) {
1147 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1148 irq->u.pgm.code;
David Hildenbrand634790b2015-11-04 16:33:33 +01001149 li->irq.pgm.flags = irq->u.pgm.flags;
David Hildenbrand238293b2015-05-04 12:38:48 +02001150 /* only modify non-PER information */
1151 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1152 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1153 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1154 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1155 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1156 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1157 } else {
1158 li->irq.pgm = irq->u.pgm;
1159 }
Jens Freimann91851242014-12-01 16:43:40 +01001160 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001161 return 0;
1162}
1163
Jens Freimann383d0b02014-07-29 15:11:49 +02001164static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001165{
1166 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1167
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001168 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1169 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001170 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1171 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001172 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001173
1174 li->irq.ext = irq->u.ext;
1175 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001176 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001177 return 0;
1178}
1179
Christian Borntraeger0675d922015-01-15 12:40:42 +01001180static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001181{
1182 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001183 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001184 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001185
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001186 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001187 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001188 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001189 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001190
David Hildenbrandea5f4962014-10-14 15:29:30 +02001191 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001192 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001193 return -EINVAL;
1194
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001195 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001196 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001197
David Hildenbrandb938eace2015-04-30 13:33:59 +02001198 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001199 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001200 *extcall = irq->u.extcall;
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001201 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001202 return 0;
1203}
1204
Jens Freimann383d0b02014-07-29 15:11:49 +02001205static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001206{
1207 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001208 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001209
David Hildenbranded2afcf2015-07-20 10:33:03 +02001210 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001211 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001212 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001213 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001214
David Hildenbranda3a9c592014-10-14 09:44:55 +02001215 if (!is_vcpu_stopped(vcpu))
1216 return -EBUSY;
1217
Jens Freimann383d0b02014-07-29 15:11:49 +02001218 *prefix = irq->u.prefix;
1219 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001220 return 0;
1221}
1222
David Hildenbrand6cddd432014-10-15 16:48:53 +02001223#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001224static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001225{
1226 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001227 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001228 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001229
David Hildenbranded2afcf2015-07-20 10:33:03 +02001230 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001231
David Hildenbrand28225452014-10-15 16:48:16 +02001232 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1233 return -EINVAL;
1234
David Hildenbrand6cddd432014-10-15 16:48:53 +02001235 if (is_vcpu_stopped(vcpu)) {
1236 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1237 rc = kvm_s390_store_status_unloaded(vcpu,
1238 KVM_S390_STORE_STATUS_NOADDR);
1239 return rc;
1240 }
1241
1242 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1243 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001244 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001245 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001246 return 0;
1247}
1248
1249static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001250 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001251{
1252 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1253
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001254 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001255 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001256
1257 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001258 return 0;
1259}
1260
1261static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001262 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001263{
1264 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1265
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001266 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001267 irq->u.emerg.code);
1268 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001269 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001270
David Hildenbrandb85de332015-11-05 09:38:15 +01001271 /* sending vcpu invalid */
1272 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1273 return -EINVAL;
1274
Jens Freimann49538d12014-12-18 15:48:14 +01001275 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001276 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001277 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001278 return 0;
1279}
1280
Jens Freimann383d0b02014-07-29 15:11:49 +02001281static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001282{
1283 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001284 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001285
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001286 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001287 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001288 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001289 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001290
1291 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001292 * Because repressible machine checks can be indicated along with
1293 * exigent machine checks (PoP, Chapter 11, Interruption action)
1294 * we need to combine cr14, mcic and external damage code.
1295 * Failing storage address and the logout area should not be or'ed
1296 * together, we just indicate the last occurrence of the corresponding
1297 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001298 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001299 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001300 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001301 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1302 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1303 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1304 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001305 if (mchk->mcic & MCHK_EX_MASK)
1306 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1307 else if (mchk->mcic & MCHK_REP_MASK)
1308 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001309 return 0;
1310}
1311
Jens Freimann383d0b02014-07-29 15:11:49 +02001312static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001313{
1314 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1315
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001316 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001317 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001318 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001319
1320 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001321 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001322 return 0;
1323}
1324
Jens Freimann383d0b02014-07-29 15:11:49 +02001325static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001326{
1327 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1328
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001329 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001330 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001331 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001332
1333 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001334 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001335 return 0;
1336}
1337
Jens Freimann6d3da242013-07-03 15:18:35 +02001338static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1339 int isc, u32 schid)
1340{
1341 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1342 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1343 struct kvm_s390_interrupt_info *iter;
1344 u16 id = (schid & 0xffff0000U) >> 16;
1345 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001346
Jens Freimann6d3da242013-07-03 15:18:35 +02001347 spin_lock(&fi->lock);
1348 list_for_each_entry(iter, isc_list, list) {
1349 if (schid && (id != iter->io.subchannel_id ||
1350 nr != iter->io.subchannel_nr))
1351 continue;
1352 /* found an appropriate entry */
1353 list_del_init(&iter->list);
1354 fi->counters[FIRQ_CNTR_IO] -= 1;
1355 if (list_empty(isc_list))
1356 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1357 spin_unlock(&fi->lock);
1358 return iter;
1359 }
1360 spin_unlock(&fi->lock);
1361 return NULL;
1362}
1363
1364/*
1365 * Dequeue and return an I/O interrupt matching any of the interruption
1366 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1367 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001368struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001369 u64 isc_mask, u32 schid)
1370{
1371 struct kvm_s390_interrupt_info *inti = NULL;
1372 int isc;
1373
1374 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1375 if (isc_mask & isc_to_isc_bits(isc))
1376 inti = get_io_int(kvm, isc, schid);
1377 }
1378 return inti;
1379}
1380
1381#define SCCB_MASK 0xFFFFFFF8
1382#define SCCB_EVENT_PENDING 0x3
1383
1384static int __inject_service(struct kvm *kvm,
1385 struct kvm_s390_interrupt_info *inti)
1386{
1387 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1388
1389 spin_lock(&fi->lock);
1390 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1391 /*
1392 * Early versions of the QEMU s390 bios will inject several
1393 * service interrupts after another without handling a
1394 * condition code indicating busy.
1395 * We will silently ignore those superfluous sccb values.
1396 * A future version of QEMU will take care of serialization
1397 * of servc requests
1398 */
1399 if (fi->srv_signal.ext_params & SCCB_MASK)
1400 goto out;
1401 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1402 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1403out:
1404 spin_unlock(&fi->lock);
1405 kfree(inti);
1406 return 0;
1407}
1408
1409static int __inject_virtio(struct kvm *kvm,
1410 struct kvm_s390_interrupt_info *inti)
1411{
1412 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1413
1414 spin_lock(&fi->lock);
1415 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1416 spin_unlock(&fi->lock);
1417 return -EBUSY;
1418 }
1419 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1420 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1421 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1422 spin_unlock(&fi->lock);
1423 return 0;
1424}
1425
1426static int __inject_pfault_done(struct kvm *kvm,
1427 struct kvm_s390_interrupt_info *inti)
1428{
1429 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1430
1431 spin_lock(&fi->lock);
1432 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1433 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1434 spin_unlock(&fi->lock);
1435 return -EBUSY;
1436 }
1437 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1438 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1439 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1440 spin_unlock(&fi->lock);
1441 return 0;
1442}
1443
1444#define CR_PENDING_SUBCLASS 28
1445static int __inject_float_mchk(struct kvm *kvm,
1446 struct kvm_s390_interrupt_info *inti)
1447{
1448 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1449
1450 spin_lock(&fi->lock);
1451 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1452 fi->mchk.mcic |= inti->mchk.mcic;
1453 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1454 spin_unlock(&fi->lock);
1455 kfree(inti);
1456 return 0;
1457}
1458
1459static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001460{
1461 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001462 struct list_head *list;
1463 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001464
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001465 fi = &kvm->arch.float_int;
1466 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001467 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1468 spin_unlock(&fi->lock);
1469 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001470 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001471 fi->counters[FIRQ_CNTR_IO] += 1;
1472
Christian Borntraegerdcc98ea2016-06-07 09:37:17 +02001473 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1474 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1475 else
1476 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1477 inti->io.subchannel_id >> 8,
1478 inti->io.subchannel_id >> 1 & 0x3,
1479 inti->io.subchannel_nr);
Jens Freimann6d3da242013-07-03 15:18:35 +02001480 isc = int_word_to_isc(inti->io.io_int_word);
1481 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1482 list_add_tail(&inti->list, list);
1483 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001484 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001485 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001486}
1487
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001488/*
1489 * Find a destination VCPU for a floating irq and kick it.
1490 */
1491static void __floating_irq_kick(struct kvm *kvm, u64 type)
1492{
1493 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1494 struct kvm_s390_local_interrupt *li;
1495 struct kvm_vcpu *dst_vcpu;
1496 int sigcpu, online_vcpus, nr_tries = 0;
1497
1498 online_vcpus = atomic_read(&kvm->online_vcpus);
1499 if (!online_vcpus)
1500 return;
1501
1502 /* find idle VCPUs first, then round robin */
1503 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1504 if (sigcpu == online_vcpus) {
1505 do {
1506 sigcpu = fi->next_rr_cpu;
1507 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1508 /* avoid endless loops if all vcpus are stopped */
1509 if (nr_tries++ >= online_vcpus)
1510 return;
1511 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1512 }
1513 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1514
1515 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1516 li = &dst_vcpu->arch.local_int;
1517 spin_lock(&li->lock);
1518 switch (type) {
1519 case KVM_S390_MCHK:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001520 atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001521 break;
1522 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001523 atomic_or(CPUSTAT_IO_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001524 break;
1525 default:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001526 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001527 break;
1528 }
1529 spin_unlock(&li->lock);
1530 kvm_s390_vcpu_wakeup(dst_vcpu);
1531}
1532
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001533static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001534{
Jens Freimann6d3da242013-07-03 15:18:35 +02001535 u64 type = READ_ONCE(inti->type);
1536 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001537
Jens Freimann6d3da242013-07-03 15:18:35 +02001538 switch (type) {
1539 case KVM_S390_MCHK:
1540 rc = __inject_float_mchk(kvm, inti);
1541 break;
1542 case KVM_S390_INT_VIRTIO:
1543 rc = __inject_virtio(kvm, inti);
1544 break;
1545 case KVM_S390_INT_SERVICE:
1546 rc = __inject_service(kvm, inti);
1547 break;
1548 case KVM_S390_INT_PFAULT_DONE:
1549 rc = __inject_pfault_done(kvm, inti);
1550 break;
1551 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1552 rc = __inject_io(kvm, inti);
1553 break;
1554 default:
1555 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001556 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001557 if (rc)
1558 return rc;
1559
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001560 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001561 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001562}
1563
1564int kvm_s390_inject_vm(struct kvm *kvm,
1565 struct kvm_s390_interrupt *s390int)
1566{
1567 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001568 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001569
1570 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1571 if (!inti)
1572 return -ENOMEM;
1573
1574 inti->type = s390int->type;
1575 switch (inti->type) {
1576 case KVM_S390_INT_VIRTIO:
1577 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1578 s390int->parm, s390int->parm64);
1579 inti->ext.ext_params = s390int->parm;
1580 inti->ext.ext_params2 = s390int->parm64;
1581 break;
1582 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001583 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001584 inti->ext.ext_params = s390int->parm;
1585 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001586 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001587 inti->ext.ext_params2 = s390int->parm64;
1588 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001589 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001590 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001591 s390int->parm64);
1592 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1593 inti->mchk.mcic = s390int->parm64;
1594 break;
1595 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimannc05c4182013-10-07 16:13:45 +02001596 inti->io.subchannel_id = s390int->parm >> 16;
1597 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1598 inti->io.io_int_parm = s390int->parm64 >> 32;
1599 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1600 break;
1601 default:
1602 kfree(inti);
1603 return -EINVAL;
1604 }
1605 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1606 2);
1607
David Hildenbrand428d53b2015-01-16 12:58:09 +01001608 rc = __inject_vm(kvm, inti);
1609 if (rc)
1610 kfree(inti);
1611 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001612}
1613
David Hildenbrand15462e32015-02-04 15:59:11 +01001614int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001615 struct kvm_s390_interrupt_info *inti)
1616{
David Hildenbrand15462e32015-02-04 15:59:11 +01001617 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001618}
1619
Jens Freimann383d0b02014-07-29 15:11:49 +02001620int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1621 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001622{
Jens Freimann383d0b02014-07-29 15:11:49 +02001623 irq->type = s390int->type;
1624 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001625 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001626 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001627 return -EINVAL;
1628 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001629 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001630 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001631 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001632 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001633 case KVM_S390_SIGP_STOP:
1634 irq->u.stop.flags = s390int->parm;
1635 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001636 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001637 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001638 return -EINVAL;
1639 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001640 break;
1641 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001642 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001643 return -EINVAL;
1644 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001645 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001646 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001647 irq->u.mchk.mcic = s390int->parm64;
1648 break;
1649 }
1650 return 0;
1651}
1652
David Hildenbrand6cddd432014-10-15 16:48:53 +02001653int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1654{
1655 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1656
1657 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1658}
1659
1660void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1661{
1662 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1663
1664 spin_lock(&li->lock);
1665 li->irq.stop.flags = 0;
1666 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1667 spin_unlock(&li->lock);
1668}
1669
Jens Freimann79e87a12015-03-19 15:12:12 +01001670static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001671{
Jens Freimann383d0b02014-07-29 15:11:49 +02001672 int rc;
1673
Jens Freimann383d0b02014-07-29 15:11:49 +02001674 switch (irq->type) {
1675 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001676 rc = __inject_prog(vcpu, irq);
1677 break;
1678 case KVM_S390_SIGP_SET_PREFIX:
1679 rc = __inject_set_prefix(vcpu, irq);
1680 break;
1681 case KVM_S390_SIGP_STOP:
1682 rc = __inject_sigp_stop(vcpu, irq);
1683 break;
1684 case KVM_S390_RESTART:
1685 rc = __inject_sigp_restart(vcpu, irq);
1686 break;
1687 case KVM_S390_INT_CLOCK_COMP:
1688 rc = __inject_ckc(vcpu);
1689 break;
1690 case KVM_S390_INT_CPU_TIMER:
1691 rc = __inject_cpu_timer(vcpu);
1692 break;
1693 case KVM_S390_INT_EXTERNAL_CALL:
1694 rc = __inject_extcall(vcpu, irq);
1695 break;
1696 case KVM_S390_INT_EMERGENCY:
1697 rc = __inject_sigp_emergency(vcpu, irq);
1698 break;
1699 case KVM_S390_MCHK:
1700 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001701 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001702 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001703 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001704 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001705 case KVM_S390_INT_VIRTIO:
1706 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001707 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001708 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001709 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001710 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001711
1712 return rc;
1713}
1714
1715int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1716{
1717 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1718 int rc;
1719
1720 spin_lock(&li->lock);
1721 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001722 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001723 if (!rc)
1724 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001725 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001726}
Jens Freimannc05c4182013-10-07 16:13:45 +02001727
Jens Freimann6d3da242013-07-03 15:18:35 +02001728static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001729{
Jens Freimann6d3da242013-07-03 15:18:35 +02001730 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001731
Jens Freimann6d3da242013-07-03 15:18:35 +02001732 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001733 list_del(&inti->list);
1734 kfree(inti);
1735 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001736}
1737
Jens Freimann94aa0332015-03-16 12:17:13 +01001738static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1739 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001740{
Jens Freimann94aa0332015-03-16 12:17:13 +01001741 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001742 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001743 case KVM_S390_INT_PFAULT_INIT:
1744 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001745 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001746 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001747 break;
1748 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001749 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001750 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001751 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001752}
1753
Jens Freimann6d3da242013-07-03 15:18:35 +02001754void kvm_s390_clear_float_irqs(struct kvm *kvm)
1755{
1756 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1757 int i;
1758
1759 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001760 fi->pending_irqs = 0;
1761 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1762 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001763 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1764 clear_irq_list(&fi->lists[i]);
1765 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1766 fi->counters[i] = 0;
1767 spin_unlock(&fi->lock);
1768};
1769
Jens Freimann94aa0332015-03-16 12:17:13 +01001770static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001771{
1772 struct kvm_s390_interrupt_info *inti;
1773 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001774 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001775 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001776 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001777 int ret = 0;
1778 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001779 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001780
Jens Freimann94aa0332015-03-16 12:17:13 +01001781 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1782 return -EINVAL;
1783
1784 /*
1785 * We are already using -ENOMEM to signal
1786 * userspace it may retry with a bigger buffer,
1787 * so we need to use something else for this case
1788 */
1789 buf = vzalloc(len);
1790 if (!buf)
1791 return -ENOBUFS;
1792
1793 max_irqs = len / sizeof(struct kvm_s390_irq);
1794
Jens Freimannc05c4182013-10-07 16:13:45 +02001795 fi = &kvm->arch.float_int;
1796 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001797 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1798 list_for_each_entry(inti, &fi->lists[i], list) {
1799 if (n == max_irqs) {
1800 /* signal userspace to try again */
1801 ret = -ENOMEM;
1802 goto out;
1803 }
1804 inti_to_irq(inti, &buf[n]);
1805 n++;
1806 }
1807 }
1808 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001809 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001810 /* signal userspace to try again */
1811 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001812 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001813 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001814 irq = (struct kvm_s390_irq *) &buf[n];
1815 irq->type = KVM_S390_INT_SERVICE;
1816 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001817 n++;
1818 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001819 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1820 if (n == max_irqs) {
1821 /* signal userspace to try again */
1822 ret = -ENOMEM;
1823 goto out;
1824 }
1825 irq = (struct kvm_s390_irq *) &buf[n];
1826 irq->type = KVM_S390_MCHK;
1827 irq->u.mchk = fi->mchk;
1828 n++;
1829}
1830
1831out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001832 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001833 if (!ret && n > 0) {
1834 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1835 ret = -EFAULT;
1836 }
1837 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001838
1839 return ret < 0 ? ret : n;
1840}
1841
1842static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1843{
1844 int r;
1845
1846 switch (attr->group) {
1847 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001848 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001849 attr->attr);
1850 break;
1851 default:
1852 r = -EINVAL;
1853 }
1854
1855 return r;
1856}
1857
1858static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1859 u64 addr)
1860{
1861 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1862 void *target = NULL;
1863 void __user *source;
1864 u64 size;
1865
1866 if (get_user(inti->type, (u64 __user *)addr))
1867 return -EFAULT;
1868
1869 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001870 case KVM_S390_INT_PFAULT_INIT:
1871 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001872 case KVM_S390_INT_VIRTIO:
1873 case KVM_S390_INT_SERVICE:
1874 target = (void *) &inti->ext;
1875 source = &uptr->u.ext;
1876 size = sizeof(inti->ext);
1877 break;
1878 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1879 target = (void *) &inti->io;
1880 source = &uptr->u.io;
1881 size = sizeof(inti->io);
1882 break;
1883 case KVM_S390_MCHK:
1884 target = (void *) &inti->mchk;
1885 source = &uptr->u.mchk;
1886 size = sizeof(inti->mchk);
1887 break;
1888 default:
1889 return -EINVAL;
1890 }
1891
1892 if (copy_from_user(target, source, size))
1893 return -EFAULT;
1894
1895 return 0;
1896}
1897
1898static int enqueue_floating_irq(struct kvm_device *dev,
1899 struct kvm_device_attr *attr)
1900{
1901 struct kvm_s390_interrupt_info *inti = NULL;
1902 int r = 0;
1903 int len = attr->attr;
1904
1905 if (len % sizeof(struct kvm_s390_irq) != 0)
1906 return -EINVAL;
1907 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1908 return -EINVAL;
1909
1910 while (len >= sizeof(struct kvm_s390_irq)) {
1911 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1912 if (!inti)
1913 return -ENOMEM;
1914
1915 r = copy_irq_from_user(inti, attr->addr);
1916 if (r) {
1917 kfree(inti);
1918 return r;
1919 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001920 r = __inject_vm(dev->kvm, inti);
1921 if (r) {
1922 kfree(inti);
1923 return r;
1924 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001925 len -= sizeof(struct kvm_s390_irq);
1926 attr->addr += sizeof(struct kvm_s390_irq);
1927 }
1928
1929 return r;
1930}
1931
Cornelia Huck841b91c2013-07-15 13:36:01 +02001932static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1933{
1934 if (id >= MAX_S390_IO_ADAPTERS)
1935 return NULL;
1936 return kvm->arch.adapters[id];
1937}
1938
1939static int register_io_adapter(struct kvm_device *dev,
1940 struct kvm_device_attr *attr)
1941{
1942 struct s390_io_adapter *adapter;
1943 struct kvm_s390_io_adapter adapter_info;
1944
1945 if (copy_from_user(&adapter_info,
1946 (void __user *)attr->addr, sizeof(adapter_info)))
1947 return -EFAULT;
1948
1949 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1950 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1951 return -EINVAL;
1952
1953 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1954 if (!adapter)
1955 return -ENOMEM;
1956
1957 INIT_LIST_HEAD(&adapter->maps);
1958 init_rwsem(&adapter->maps_lock);
1959 atomic_set(&adapter->nr_maps, 0);
1960 adapter->id = adapter_info.id;
1961 adapter->isc = adapter_info.isc;
1962 adapter->maskable = adapter_info.maskable;
1963 adapter->masked = false;
1964 adapter->swap = adapter_info.swap;
1965 dev->kvm->arch.adapters[adapter->id] = adapter;
1966
1967 return 0;
1968}
1969
1970int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1971{
1972 int ret;
1973 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1974
1975 if (!adapter || !adapter->maskable)
1976 return -EINVAL;
1977 ret = adapter->masked;
1978 adapter->masked = masked;
1979 return ret;
1980}
1981
1982static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1983{
1984 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1985 struct s390_map_info *map;
1986 int ret;
1987
1988 if (!adapter || !addr)
1989 return -EINVAL;
1990
1991 map = kzalloc(sizeof(*map), GFP_KERNEL);
1992 if (!map) {
1993 ret = -ENOMEM;
1994 goto out;
1995 }
1996 INIT_LIST_HEAD(&map->list);
1997 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02001998 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001999 if (map->addr == -EFAULT) {
2000 ret = -EFAULT;
2001 goto out;
2002 }
2003 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
2004 if (ret < 0)
2005 goto out;
2006 BUG_ON(ret != 1);
2007 down_write(&adapter->maps_lock);
2008 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
2009 list_add_tail(&map->list, &adapter->maps);
2010 ret = 0;
2011 } else {
2012 put_page(map->page);
2013 ret = -EINVAL;
2014 }
2015 up_write(&adapter->maps_lock);
2016out:
2017 if (ret)
2018 kfree(map);
2019 return ret;
2020}
2021
2022static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
2023{
2024 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2025 struct s390_map_info *map, *tmp;
2026 int found = 0;
2027
2028 if (!adapter || !addr)
2029 return -EINVAL;
2030
2031 down_write(&adapter->maps_lock);
2032 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
2033 if (map->guest_addr == addr) {
2034 found = 1;
2035 atomic_dec(&adapter->nr_maps);
2036 list_del(&map->list);
2037 put_page(map->page);
2038 kfree(map);
2039 break;
2040 }
2041 }
2042 up_write(&adapter->maps_lock);
2043
2044 return found ? 0 : -EINVAL;
2045}
2046
2047void kvm_s390_destroy_adapters(struct kvm *kvm)
2048{
2049 int i;
2050 struct s390_map_info *map, *tmp;
2051
2052 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
2053 if (!kvm->arch.adapters[i])
2054 continue;
2055 list_for_each_entry_safe(map, tmp,
2056 &kvm->arch.adapters[i]->maps, list) {
2057 list_del(&map->list);
2058 put_page(map->page);
2059 kfree(map);
2060 }
2061 kfree(kvm->arch.adapters[i]);
2062 }
2063}
2064
2065static int modify_io_adapter(struct kvm_device *dev,
2066 struct kvm_device_attr *attr)
2067{
2068 struct kvm_s390_io_adapter_req req;
2069 struct s390_io_adapter *adapter;
2070 int ret;
2071
2072 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2073 return -EFAULT;
2074
2075 adapter = get_io_adapter(dev->kvm, req.id);
2076 if (!adapter)
2077 return -EINVAL;
2078 switch (req.type) {
2079 case KVM_S390_IO_ADAPTER_MASK:
2080 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2081 if (ret > 0)
2082 ret = 0;
2083 break;
2084 case KVM_S390_IO_ADAPTER_MAP:
2085 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
2086 break;
2087 case KVM_S390_IO_ADAPTER_UNMAP:
2088 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
2089 break;
2090 default:
2091 ret = -EINVAL;
2092 }
2093
2094 return ret;
2095}
2096
Halil Pasic6d28f782016-01-25 19:10:40 +01002097static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2098
2099{
2100 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2101 u32 schid;
2102
2103 if (attr->flags)
2104 return -EINVAL;
2105 if (attr->attr != sizeof(schid))
2106 return -EINVAL;
2107 if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2108 return -EFAULT;
2109 kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2110 /*
2111 * If userspace is conforming to the architecture, we can have at most
2112 * one pending I/O interrupt per subchannel, so this is effectively a
2113 * clear all.
2114 */
2115 return 0;
2116}
2117
Jens Freimannc05c4182013-10-07 16:13:45 +02002118static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2119{
2120 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002121 unsigned int i;
2122 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02002123
2124 switch (attr->group) {
2125 case KVM_DEV_FLIC_ENQUEUE:
2126 r = enqueue_floating_irq(dev, attr);
2127 break;
2128 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01002129 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02002130 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02002131 case KVM_DEV_FLIC_APF_ENABLE:
2132 dev->kvm->arch.gmap->pfault_enabled = 1;
2133 break;
2134 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2135 dev->kvm->arch.gmap->pfault_enabled = 0;
2136 /*
2137 * Make sure no async faults are in transition when
2138 * clearing the queues. So we don't need to worry
2139 * about late coming workers.
2140 */
2141 synchronize_srcu(&dev->kvm->srcu);
2142 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2143 kvm_clear_async_pf_completion_queue(vcpu);
2144 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02002145 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2146 r = register_io_adapter(dev, attr);
2147 break;
2148 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2149 r = modify_io_adapter(dev, attr);
2150 break;
Halil Pasic6d28f782016-01-25 19:10:40 +01002151 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2152 r = clear_io_irq(dev->kvm, attr);
2153 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02002154 default:
2155 r = -EINVAL;
2156 }
2157
2158 return r;
2159}
2160
Halil Pasic4f129852016-02-25 12:44:17 +01002161static int flic_has_attr(struct kvm_device *dev,
2162 struct kvm_device_attr *attr)
2163{
2164 switch (attr->group) {
2165 case KVM_DEV_FLIC_GET_ALL_IRQS:
2166 case KVM_DEV_FLIC_ENQUEUE:
2167 case KVM_DEV_FLIC_CLEAR_IRQS:
2168 case KVM_DEV_FLIC_APF_ENABLE:
2169 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2170 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2171 case KVM_DEV_FLIC_ADAPTER_MODIFY:
Halil Pasic6d28f782016-01-25 19:10:40 +01002172 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
Halil Pasic4f129852016-02-25 12:44:17 +01002173 return 0;
2174 }
2175 return -ENXIO;
2176}
2177
Jens Freimannc05c4182013-10-07 16:13:45 +02002178static int flic_create(struct kvm_device *dev, u32 type)
2179{
2180 if (!dev)
2181 return -EINVAL;
2182 if (dev->kvm->arch.flic)
2183 return -EINVAL;
2184 dev->kvm->arch.flic = dev;
2185 return 0;
2186}
2187
2188static void flic_destroy(struct kvm_device *dev)
2189{
2190 dev->kvm->arch.flic = NULL;
2191 kfree(dev);
2192}
2193
2194/* s390 floating irq controller (flic) */
2195struct kvm_device_ops kvm_flic_ops = {
2196 .name = "kvm-flic",
2197 .get_attr = flic_get_attr,
2198 .set_attr = flic_set_attr,
Halil Pasic4f129852016-02-25 12:44:17 +01002199 .has_attr = flic_has_attr,
Jens Freimannc05c4182013-10-07 16:13:45 +02002200 .create = flic_create,
2201 .destroy = flic_destroy,
2202};
Cornelia Huck84223592013-07-15 13:36:01 +02002203
2204static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2205{
2206 unsigned long bit;
2207
2208 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2209
2210 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2211}
2212
2213static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2214 u64 addr)
2215{
2216 struct s390_map_info *map;
2217
2218 if (!adapter)
2219 return NULL;
2220
2221 list_for_each_entry(map, &adapter->maps, list) {
2222 if (map->guest_addr == addr)
2223 return map;
2224 }
2225 return NULL;
2226}
2227
2228static int adapter_indicators_set(struct kvm *kvm,
2229 struct s390_io_adapter *adapter,
2230 struct kvm_s390_adapter_int *adapter_int)
2231{
2232 unsigned long bit;
2233 int summary_set, idx;
2234 struct s390_map_info *info;
2235 void *map;
2236
2237 info = get_map_info(adapter, adapter_int->ind_addr);
2238 if (!info)
2239 return -1;
2240 map = page_address(info->page);
2241 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2242 set_bit(bit, map);
2243 idx = srcu_read_lock(&kvm->srcu);
2244 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2245 set_page_dirty_lock(info->page);
2246 info = get_map_info(adapter, adapter_int->summary_addr);
2247 if (!info) {
2248 srcu_read_unlock(&kvm->srcu, idx);
2249 return -1;
2250 }
2251 map = page_address(info->page);
2252 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2253 adapter->swap);
2254 summary_set = test_and_set_bit(bit, map);
2255 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2256 set_page_dirty_lock(info->page);
2257 srcu_read_unlock(&kvm->srcu, idx);
2258 return summary_set ? 0 : 1;
2259}
2260
2261/*
2262 * < 0 - not injected due to error
2263 * = 0 - coalesced, summary indicator already active
2264 * > 0 - injected interrupt
2265 */
2266static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2267 struct kvm *kvm, int irq_source_id, int level,
2268 bool line_status)
2269{
2270 int ret;
2271 struct s390_io_adapter *adapter;
2272
2273 /* We're only interested in the 0->1 transition. */
2274 if (!level)
2275 return 0;
2276 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2277 if (!adapter)
2278 return -1;
2279 down_read(&adapter->maps_lock);
2280 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2281 up_read(&adapter->maps_lock);
2282 if ((ret > 0) && !adapter->masked) {
2283 struct kvm_s390_interrupt s390int = {
2284 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2285 .parm = 0,
2286 .parm64 = (adapter->isc << 27) | 0x80000000,
2287 };
2288 ret = kvm_s390_inject_vm(kvm, &s390int);
2289 if (ret == 0)
2290 ret = 1;
2291 }
2292 return ret;
2293}
2294
Radim Krčmářc63cf532016-07-12 22:09:26 +02002295int kvm_set_routing_entry(struct kvm *kvm,
2296 struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002297 const struct kvm_irq_routing_entry *ue)
2298{
2299 int ret;
2300
2301 switch (ue->type) {
2302 case KVM_IRQ_ROUTING_S390_ADAPTER:
2303 e->set = set_adapter_int;
2304 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2305 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2306 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2307 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2308 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2309 ret = 0;
2310 break;
2311 default:
2312 ret = -EINVAL;
2313 }
2314
2315 return ret;
2316}
2317
2318int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2319 int irq_source_id, int level, bool line_status)
2320{
2321 return -EINVAL;
2322}
Jens Freimann816c7662014-11-24 17:13:46 +01002323
2324int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2325{
2326 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2327 struct kvm_s390_irq *buf;
2328 int r = 0;
2329 int n;
2330
2331 buf = vmalloc(len);
2332 if (!buf)
2333 return -ENOMEM;
2334
2335 if (copy_from_user((void *) buf, irqstate, len)) {
2336 r = -EFAULT;
2337 goto out_free;
2338 }
2339
2340 /*
2341 * Don't allow setting the interrupt state
2342 * when there are already interrupts pending
2343 */
2344 spin_lock(&li->lock);
2345 if (li->pending_irqs) {
2346 r = -EBUSY;
2347 goto out_unlock;
2348 }
2349
2350 for (n = 0; n < len / sizeof(*buf); n++) {
2351 r = do_inject_vcpu(vcpu, &buf[n]);
2352 if (r)
2353 break;
2354 }
2355
2356out_unlock:
2357 spin_unlock(&li->lock);
2358out_free:
2359 vfree(buf);
2360
2361 return r;
2362}
2363
2364static void store_local_irq(struct kvm_s390_local_interrupt *li,
2365 struct kvm_s390_irq *irq,
2366 unsigned long irq_type)
2367{
2368 switch (irq_type) {
2369 case IRQ_PEND_MCHK_EX:
2370 case IRQ_PEND_MCHK_REP:
2371 irq->type = KVM_S390_MCHK;
2372 irq->u.mchk = li->irq.mchk;
2373 break;
2374 case IRQ_PEND_PROG:
2375 irq->type = KVM_S390_PROGRAM_INT;
2376 irq->u.pgm = li->irq.pgm;
2377 break;
2378 case IRQ_PEND_PFAULT_INIT:
2379 irq->type = KVM_S390_INT_PFAULT_INIT;
2380 irq->u.ext = li->irq.ext;
2381 break;
2382 case IRQ_PEND_EXT_EXTERNAL:
2383 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2384 irq->u.extcall = li->irq.extcall;
2385 break;
2386 case IRQ_PEND_EXT_CLOCK_COMP:
2387 irq->type = KVM_S390_INT_CLOCK_COMP;
2388 break;
2389 case IRQ_PEND_EXT_CPU_TIMER:
2390 irq->type = KVM_S390_INT_CPU_TIMER;
2391 break;
2392 case IRQ_PEND_SIGP_STOP:
2393 irq->type = KVM_S390_SIGP_STOP;
2394 irq->u.stop = li->irq.stop;
2395 break;
2396 case IRQ_PEND_RESTART:
2397 irq->type = KVM_S390_RESTART;
2398 break;
2399 case IRQ_PEND_SET_PREFIX:
2400 irq->type = KVM_S390_SIGP_SET_PREFIX;
2401 irq->u.prefix = li->irq.prefix;
2402 break;
2403 }
2404}
2405
2406int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2407{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002408 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002409 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2410 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2411 unsigned long pending_irqs;
2412 struct kvm_s390_irq irq;
2413 unsigned long irq_type;
2414 int cpuaddr;
2415 int n = 0;
2416
2417 spin_lock(&li->lock);
2418 pending_irqs = li->pending_irqs;
2419 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2420 sizeof(sigp_emerg_pending));
2421 spin_unlock(&li->lock);
2422
2423 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2424 memset(&irq, 0, sizeof(irq));
2425 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2426 continue;
2427 if (n + sizeof(irq) > len)
2428 return -ENOBUFS;
2429 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2430 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2431 return -EFAULT;
2432 n += sizeof(irq);
2433 }
2434
2435 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2436 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2437 memset(&irq, 0, sizeof(irq));
2438 if (n + sizeof(irq) > len)
2439 return -ENOBUFS;
2440 irq.type = KVM_S390_INT_EMERGENCY;
2441 irq.u.emerg.code = cpuaddr;
2442 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2443 return -EFAULT;
2444 n += sizeof(irq);
2445 }
2446 }
2447
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002448 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002449 if (n + sizeof(irq) > len)
2450 return -ENOBUFS;
2451 memset(&irq, 0, sizeof(irq));
2452 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002453 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002454 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2455 return -EFAULT;
2456 n += sizeof(irq);
2457 }
2458
2459 return n;
2460}