blob: 2836b25f63d3b883a33d607c0d45866af41c4270 [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>
Carsten Otteba5c1e92008-03-25 18:47:26 +010026#include "kvm-s390.h"
27#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020028#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010029
Cornelia Huckd8346b72012-12-20 15:32:08 +010030#define IOINT_SCHID_MASK 0x0000ffff
31#define IOINT_SSID_MASK 0x00030000
32#define IOINT_CSSID_MASK 0x03fc0000
33#define IOINT_AI_MASK 0x04000000
Jens Freimann44c6ca32014-04-16 13:57:18 +020034#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010035#define PFAULT_DONE 0x0680
36#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010037
Dominik Dingel3c038e62013-10-07 17:11:48 +020038int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010039{
40 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
41}
42
Cornelia Huckd8346b72012-12-20 15:32:08 +010043static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
44{
45 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
46}
47
Cornelia Huck48a3e952012-12-20 15:32:09 +010048static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
49{
50 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
51}
52
Carsten Otteba5c1e92008-03-25 18:47:26 +010053static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
54{
55 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
56 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
57 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
58 return 0;
59 return 1;
60}
61
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010062static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
63{
64 if (psw_extint_disabled(vcpu) ||
65 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
66 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +010067 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
68 /* No timer interrupts when single stepping */
69 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010070 return 1;
71}
72
David Hildenbrandb4aec922014-12-01 15:55:42 +010073static int ckc_irq_pending(struct kvm_vcpu *vcpu)
74{
75 if (!(vcpu->arch.sie_block->ckc <
76 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
77 return 0;
78 return ckc_interrupts_enabled(vcpu);
79}
80
81static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
82{
83 return !psw_extint_disabled(vcpu) &&
84 (vcpu->arch.sie_block->gcr[0] & 0x400ul);
85}
86
87static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
88{
89 return (vcpu->arch.sie_block->cputm >> 63) &&
90 cpu_timer_interrupts_enabled(vcpu);
91}
92
Jens Freimann6d3da242013-07-03 15:18:35 +020093static inline int is_ioirq(unsigned long irq_type)
Cornelia Huck79fd50c2013-02-07 13:20:52 +010094{
Jens Freimann6d3da242013-07-03 15:18:35 +020095 return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
96 (irq_type <= IRQ_PEND_IO_ISC_7));
97}
Cornelia Huck79fd50c2013-02-07 13:20:52 +010098
Jens Freimann6d3da242013-07-03 15:18:35 +020099static uint64_t isc_to_isc_bits(int isc)
100{
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100101 return (0x80 >> isc) << 24;
102}
103
Jens Freimann6d3da242013-07-03 15:18:35 +0200104static inline u8 int_word_to_isc(u32 int_word)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100105{
Jens Freimann6d3da242013-07-03 15:18:35 +0200106 return (int_word & 0x38000000) >> 27;
107}
108
109static inline unsigned long pending_floating_irqs(struct kvm_vcpu *vcpu)
110{
111 return vcpu->kvm->arch.float_int.pending_irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100112}
113
Jens Freimann383d0b02014-07-29 15:11:49 +0200114static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu)
115{
116 return vcpu->arch.local_int.pending_irqs;
117}
118
Jens Freimann6d3da242013-07-03 15:18:35 +0200119static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
120 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200121{
Jens Freimann6d3da242013-07-03 15:18:35 +0200122 int i;
123
124 for (i = 0; i <= MAX_ISC; i++)
125 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
126 active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
127
128 return active_mask;
129}
130
131static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
132{
133 unsigned long active_mask;
134
135 active_mask = pending_local_irqs(vcpu);
136 active_mask |= pending_floating_irqs(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200137
138 if (psw_extint_disabled(vcpu))
139 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200140 if (psw_ioint_disabled(vcpu))
141 active_mask &= ~IRQ_PEND_IO_MASK;
142 else
143 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200144 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
145 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
146 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
147 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
148 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
149 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
150 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
151 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200152 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
153 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200154 if (psw_mchk_disabled(vcpu))
155 active_mask &= ~IRQ_PEND_MCHK_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200156 if (!(vcpu->arch.sie_block->gcr[14] &
157 vcpu->kvm->arch.float_int.mchk.cr14))
158 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200159
David Hildenbrand6cddd432014-10-15 16:48:53 +0200160 /*
161 * STOP irqs will never be actively delivered. They are triggered via
162 * intercept requests and cleared when the stop intercept is performed.
163 */
164 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
165
Jens Freimann383d0b02014-07-29 15:11:49 +0200166 return active_mask;
167}
168
Carsten Otteba5c1e92008-03-25 18:47:26 +0100169static void __set_cpu_idle(struct kvm_vcpu *vcpu)
170{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100171 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
172 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
173}
174
175static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
176{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100177 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
178 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
179}
180
181static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
182{
David Hildenbrand49539192014-02-21 08:59:59 +0100183 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
184 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100185 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100186 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
187
188 if (guestdbg_enabled(vcpu)) {
189 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
190 LCTL_CR10 | LCTL_CR11);
191 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
192 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100193}
194
195static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
196{
197 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
198}
199
Jens Freimann6d3da242013-07-03 15:18:35 +0200200static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
201{
202 if (!(pending_floating_irqs(vcpu) & IRQ_PEND_IO_MASK))
203 return;
204 else if (psw_ioint_disabled(vcpu))
205 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
206 else
207 vcpu->arch.sie_block->lctl |= LCTL_CR6;
208}
209
Jens Freimann383d0b02014-07-29 15:11:49 +0200210static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
211{
212 if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK))
213 return;
214 if (psw_extint_disabled(vcpu))
215 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
216 else
217 vcpu->arch.sie_block->lctl |= LCTL_CR0;
218}
219
220static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
221{
222 if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
223 return;
224 if (psw_mchk_disabled(vcpu))
225 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
226 else
227 vcpu->arch.sie_block->lctl |= LCTL_CR14;
228}
229
David Hildenbrand6cddd432014-10-15 16:48:53 +0200230static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
231{
232 if (kvm_s390_is_stop_irq_pending(vcpu))
233 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
234}
235
Jens Freimann6d3da242013-07-03 15:18:35 +0200236/* Set interception request for non-deliverable interrupts */
237static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200238{
Jens Freimann6d3da242013-07-03 15:18:35 +0200239 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200240 set_intercept_indicators_ext(vcpu);
241 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200242 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200243}
244
Jens Freimann8a2ef712014-07-23 16:36:06 +0200245static u16 get_ilc(struct kvm_vcpu *vcpu)
246{
Jens Freimann8a2ef712014-07-23 16:36:06 +0200247 switch (vcpu->arch.sie_block->icptcode) {
248 case ICPT_INST:
249 case ICPT_INSTPROGI:
250 case ICPT_OPEREXC:
251 case ICPT_PARTEXEC:
252 case ICPT_IOINST:
253 /* last instruction only stored for these icptcodes */
Thomas Huth33b412a2015-02-11 10:38:46 +0100254 return insn_length(vcpu->arch.sie_block->ipa >> 8);
Jens Freimann8a2ef712014-07-23 16:36:06 +0200255 case ICPT_PROGI:
256 return vcpu->arch.sie_block->pgmilc;
257 default:
258 return 0;
259 }
260}
261
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200262static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100263{
Jens Freimann383d0b02014-07-29 15:11:49 +0200264 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200265 int rc;
266
267 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
268 0, 0);
269
270 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
271 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100272 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200273 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
274 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
275 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
276 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200277 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100278 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200279}
280
281static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
282{
Jens Freimann383d0b02014-07-29 15:11:49 +0200283 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200284 int rc;
285
286 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
287 0, 0);
288
289 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
290 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100291 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200292 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
293 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
294 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
295 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200296 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100297 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200298}
299
Jens Freimann383d0b02014-07-29 15:11:49 +0200300static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200301{
Jens Freimann383d0b02014-07-29 15:11:49 +0200302 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
303 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200304 int rc;
305
Jens Freimann383d0b02014-07-29 15:11:49 +0200306 spin_lock(&li->lock);
307 ext = li->irq.ext;
308 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
309 li->irq.ext.ext_params2 = 0;
310 spin_unlock(&li->lock);
311
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200312 VCPU_EVENT(vcpu, 4, "interrupt: pfault init parm:%x,parm64:%llx",
Jens Freimann383d0b02014-07-29 15:11:49 +0200313 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200314 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
315 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200316 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200317
318 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
319 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
320 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
321 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
322 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
323 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200324 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100325 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200326}
327
Jens Freimann383d0b02014-07-29 15:11:49 +0200328static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200329{
Jens Freimann6d3da242013-07-03 15:18:35 +0200330 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200331 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200332 struct kvm_s390_mchk_info mchk = {};
Eric Farmanbc17de72014-04-14 16:01:09 -0400333 unsigned long adtl_status_addr;
Jens Freimann6d3da242013-07-03 15:18:35 +0200334 int deliver = 0;
335 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200336
Jens Freimann6d3da242013-07-03 15:18:35 +0200337 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200338 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200339 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
340 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
341 /*
342 * If there was an exigent machine check pending, then any
343 * repressible machine checks that might have been pending
344 * are indicated along with it, so always clear bits for
345 * repressible and exigent interrupts
346 */
347 mchk = li->irq.mchk;
348 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
349 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
350 memset(&li->irq.mchk, 0, sizeof(mchk));
351 deliver = 1;
352 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200353 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200354 * We indicate floating repressible conditions along with
355 * other pending conditions. Channel Report Pending and Channel
356 * Subsystem damage are the only two and and are indicated by
357 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200358 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200359 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
360 mchk.mcic |= fi->mchk.mcic;
361 mchk.cr14 |= fi->mchk.cr14;
362 memset(&fi->mchk, 0, sizeof(mchk));
363 deliver = 1;
364 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200365 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200366 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200367
Jens Freimann6d3da242013-07-03 15:18:35 +0200368 if (deliver) {
369 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
370 mchk.mcic);
371 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
372 KVM_S390_MCHK,
373 mchk.cr14, mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200374
Jens Freimann6d3da242013-07-03 15:18:35 +0200375 rc = kvm_s390_vcpu_store_status(vcpu,
376 KVM_S390_STORE_STATUS_PREFIXED);
377 rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR,
378 &adtl_status_addr,
379 sizeof(unsigned long));
380 rc |= kvm_s390_vcpu_store_adtl_status(vcpu,
381 adtl_status_addr);
382 rc |= put_guest_lc(vcpu, mchk.mcic,
383 (u64 __user *) __LC_MCCK_CODE);
384 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
385 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
386 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
387 &mchk.fixed_logout,
388 sizeof(mchk.fixed_logout));
389 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
390 &vcpu->arch.sie_block->gpsw,
391 sizeof(psw_t));
392 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
393 &vcpu->arch.sie_block->gpsw,
394 sizeof(psw_t));
395 }
Jens Freimann99e20002014-12-01 17:05:39 +0100396 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200397}
398
399static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
400{
Jens Freimann383d0b02014-07-29 15:11:49 +0200401 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200402 int rc;
403
404 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
405 vcpu->stat.deliver_restart_signal++;
406 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
407
408 rc = write_guest_lc(vcpu,
409 offsetof(struct _lowcore, restart_old_psw),
410 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
411 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
412 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200413 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100414 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200415}
416
Jens Freimann383d0b02014-07-29 15:11:49 +0200417static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200418{
Jens Freimann383d0b02014-07-29 15:11:49 +0200419 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
420 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200421
Jens Freimann383d0b02014-07-29 15:11:49 +0200422 spin_lock(&li->lock);
423 prefix = li->irq.prefix;
424 li->irq.prefix.address = 0;
425 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
426 spin_unlock(&li->lock);
427
428 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200429 vcpu->stat.deliver_prefix_signal++;
430 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
431 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200432 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200433
Jens Freimann383d0b02014-07-29 15:11:49 +0200434 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200435 return 0;
436}
437
Jens Freimann383d0b02014-07-29 15:11:49 +0200438static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200439{
Jens Freimann383d0b02014-07-29 15:11:49 +0200440 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200441 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200442 int cpu_addr;
443
444 spin_lock(&li->lock);
445 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
446 clear_bit(cpu_addr, li->sigp_emerg_pending);
447 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
448 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
449 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200450
451 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
452 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200453 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
454 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200455
456 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
457 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200458 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200459 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
460 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
461 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
462 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100463 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200464}
465
Jens Freimann383d0b02014-07-29 15:11:49 +0200466static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200467{
Jens Freimann383d0b02014-07-29 15:11:49 +0200468 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
469 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200470 int rc;
471
Jens Freimann383d0b02014-07-29 15:11:49 +0200472 spin_lock(&li->lock);
473 extcall = li->irq.extcall;
474 li->irq.extcall.code = 0;
475 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
476 spin_unlock(&li->lock);
477
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200478 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
479 vcpu->stat.deliver_external_call++;
480 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
481 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200482 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200483
484 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
485 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200486 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200487 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
488 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
489 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
490 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100491 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200492}
493
Jens Freimann383d0b02014-07-29 15:11:49 +0200494static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200495{
Jens Freimann383d0b02014-07-29 15:11:49 +0200496 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
497 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100498 int rc = 0, nullifying = false;
Jens Freimann8a2ef712014-07-23 16:36:06 +0200499 u16 ilc = get_ilc(vcpu);
David Hildenbrand87128362014-03-03 10:55:13 +0100500
Jens Freimann383d0b02014-07-29 15:11:49 +0200501 spin_lock(&li->lock);
502 pgm_info = li->irq.pgm;
503 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
504 memset(&li->irq.pgm, 0, sizeof(pgm_info));
505 spin_unlock(&li->lock);
506
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200507 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
Jens Freimann383d0b02014-07-29 15:11:49 +0200508 pgm_info.code, ilc);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200509 vcpu->stat.deliver_program_int++;
510 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200511 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200512
Jens Freimann383d0b02014-07-29 15:11:49 +0200513 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100514 case PGM_AFX_TRANSLATION:
515 case PGM_ASX_TRANSLATION:
516 case PGM_EX_TRANSLATION:
517 case PGM_LFX_TRANSLATION:
518 case PGM_LSTE_SEQUENCE:
519 case PGM_LSX_TRANSLATION:
520 case PGM_LX_TRANSLATION:
521 case PGM_PRIMARY_AUTHORITY:
522 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100523 nullifying = true;
524 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100525 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200526 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100527 (u64 *)__LC_TRANS_EXC_CODE);
528 break;
529 case PGM_ALEN_TRANSLATION:
530 case PGM_ALE_SEQUENCE:
531 case PGM_ASTE_INSTANCE:
532 case PGM_ASTE_SEQUENCE:
533 case PGM_ASTE_VALIDITY:
534 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200535 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100536 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100537 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100538 break;
539 case PGM_ASCE_TYPE:
540 case PGM_PAGE_TRANSLATION:
541 case PGM_REGION_FIRST_TRANS:
542 case PGM_REGION_SECOND_TRANS:
543 case PGM_REGION_THIRD_TRANS:
544 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200545 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100546 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200547 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100548 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200549 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100550 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100551 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100552 break;
553 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200554 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200555 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200556 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100557 (u64 *)__LC_MON_CODE);
558 break;
Eric Farman403c8642015-02-02 15:01:06 -0500559 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100560 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200561 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100562 (u32 *)__LC_DATA_EXC_CODE);
563 break;
564 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200565 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100566 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200567 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100568 (u8 *)__LC_EXC_ACCESS_ID);
569 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100570 case PGM_STACK_FULL:
571 case PGM_STACK_EMPTY:
572 case PGM_STACK_SPECIFICATION:
573 case PGM_STACK_TYPE:
574 case PGM_STACK_OPERATION:
575 case PGM_TRACE_TABEL:
576 case PGM_CRYPTO_OPERATION:
577 nullifying = true;
578 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100579 }
580
Jens Freimann383d0b02014-07-29 15:11:49 +0200581 if (pgm_info.code & PGM_PER) {
582 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100583 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200584 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100585 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200586 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100587 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200588 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100589 (u8 *) __LC_PER_ACCESS_ID);
590 }
591
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100592 if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
593 kvm_s390_rewind_psw(vcpu, ilc);
594
Jens Freimann8a2ef712014-07-23 16:36:06 +0200595 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100596 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
597 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200598 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100599 (u16 *)__LC_PGM_INT_CODE);
600 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
601 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
602 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
603 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100604 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200605}
David Hildenbrand87128362014-03-03 10:55:13 +0100606
Jens Freimann6d3da242013-07-03 15:18:35 +0200607static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200608{
Jens Freimann6d3da242013-07-03 15:18:35 +0200609 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
610 struct kvm_s390_ext_info ext;
611 int rc = 0;
612
613 spin_lock(&fi->lock);
614 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
615 spin_unlock(&fi->lock);
616 return 0;
617 }
618 ext = fi->srv_signal;
619 memset(&fi->srv_signal, 0, sizeof(ext));
620 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
621 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200622
623 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200624 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200625 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200626 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
627 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200628
629 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100630 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200631 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
632 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
633 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
634 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200635 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200636 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200637
Jens Freimann99e20002014-12-01 17:05:39 +0100638 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200639}
640
Jens Freimann6d3da242013-07-03 15:18:35 +0200641static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200642{
Jens Freimann6d3da242013-07-03 15:18:35 +0200643 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
644 struct kvm_s390_interrupt_info *inti;
645 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200646
Jens Freimann6d3da242013-07-03 15:18:35 +0200647 spin_lock(&fi->lock);
648 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
649 struct kvm_s390_interrupt_info,
650 list);
651 if (inti) {
652 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
653 KVM_S390_INT_PFAULT_DONE, 0,
654 inti->ext.ext_params2);
655 list_del(&inti->list);
656 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
657 }
658 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
659 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
660 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200661
Jens Freimann6d3da242013-07-03 15:18:35 +0200662 if (inti) {
663 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
664 (u16 *)__LC_EXT_INT_CODE);
665 rc |= put_guest_lc(vcpu, PFAULT_DONE,
666 (u16 *)__LC_EXT_CPU_ADDR);
667 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
668 &vcpu->arch.sie_block->gpsw,
669 sizeof(psw_t));
670 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
671 &vcpu->arch.sie_block->gpsw,
672 sizeof(psw_t));
673 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
674 (u64 *)__LC_EXT_PARAMS2);
675 kfree(inti);
676 }
Jens Freimann99e20002014-12-01 17:05:39 +0100677 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200678}
679
Jens Freimann6d3da242013-07-03 15:18:35 +0200680static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200681{
Jens Freimann6d3da242013-07-03 15:18:35 +0200682 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
683 struct kvm_s390_interrupt_info *inti;
684 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200685
Jens Freimann6d3da242013-07-03 15:18:35 +0200686 spin_lock(&fi->lock);
687 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
688 struct kvm_s390_interrupt_info,
689 list);
690 if (inti) {
691 VCPU_EVENT(vcpu, 4,
692 "interrupt: virtio parm:%x,parm64:%llx",
693 inti->ext.ext_params, inti->ext.ext_params2);
694 vcpu->stat.deliver_virtio_interrupt++;
695 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
696 inti->type,
697 inti->ext.ext_params,
698 inti->ext.ext_params2);
699 list_del(&inti->list);
700 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
701 }
702 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
703 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
704 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200705
Jens Freimann6d3da242013-07-03 15:18:35 +0200706 if (inti) {
707 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
708 (u16 *)__LC_EXT_INT_CODE);
709 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
710 (u16 *)__LC_EXT_CPU_ADDR);
711 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
712 &vcpu->arch.sie_block->gpsw,
713 sizeof(psw_t));
714 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
715 &vcpu->arch.sie_block->gpsw,
716 sizeof(psw_t));
717 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
718 (u32 *)__LC_EXT_PARAMS);
719 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
720 (u64 *)__LC_EXT_PARAMS2);
721 kfree(inti);
722 }
Jens Freimann99e20002014-12-01 17:05:39 +0100723 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200724}
725
726static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200727 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200728{
Jens Freimann6d3da242013-07-03 15:18:35 +0200729 struct list_head *isc_list;
730 struct kvm_s390_float_interrupt *fi;
731 struct kvm_s390_interrupt_info *inti = NULL;
732 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200733
Jens Freimann6d3da242013-07-03 15:18:35 +0200734 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200735
Jens Freimann6d3da242013-07-03 15:18:35 +0200736 spin_lock(&fi->lock);
737 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
738 inti = list_first_entry_or_null(isc_list,
739 struct kvm_s390_interrupt_info,
740 list);
741 if (inti) {
742 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
743 vcpu->stat.deliver_io_int++;
744 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
745 inti->type,
746 ((__u32)inti->io.subchannel_id << 16) |
747 inti->io.subchannel_nr,
748 ((__u64)inti->io.io_int_parm << 32) |
749 inti->io.io_int_word);
750 list_del(&inti->list);
751 fi->counters[FIRQ_CNTR_IO] -= 1;
752 }
753 if (list_empty(isc_list))
754 clear_bit(irq_type, &fi->pending_irqs);
755 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100756
Jens Freimann6d3da242013-07-03 15:18:35 +0200757 if (inti) {
758 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
759 (u16 *)__LC_SUBCHANNEL_ID);
760 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
761 (u16 *)__LC_SUBCHANNEL_NR);
762 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
763 (u32 *)__LC_IO_INT_PARM);
764 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
765 (u32 *)__LC_IO_INT_WORD);
766 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
767 &vcpu->arch.sie_block->gpsw,
768 sizeof(psw_t));
769 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
770 &vcpu->arch.sie_block->gpsw,
771 sizeof(psw_t));
772 kfree(inti);
773 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200774
Jens Freimann99e20002014-12-01 17:05:39 +0100775 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200776}
777
778typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
779
780static const deliver_irq_t deliver_irq_funcs[] = {
781 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200782 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200783 [IRQ_PEND_PROG] = __deliver_prog,
784 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
785 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
786 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
787 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
788 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200789 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
790 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200791 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
792 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
793 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200794};
795
David Hildenbrandea5f4962014-10-14 15:29:30 +0200796/* Check whether an external call is pending (deliverable or not) */
797int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100798{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200799 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
800 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
David Hildenbrand49539192014-02-21 08:59:59 +0100801
David Hildenbrandea5f4962014-10-14 15:29:30 +0200802 if (!sclp_has_sigpif())
803 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100804
David Hildenbrandea5f4962014-10-14 15:29:30 +0200805 return (sigp_ctrl & SIGP_CTRL_C) &&
806 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
David Hildenbrand49539192014-02-21 08:59:59 +0100807}
808
David Hildenbrand9a022062014-08-05 17:40:47 +0200809int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100810{
Jens Freimann383d0b02014-07-29 15:11:49 +0200811 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100812
Jens Freimann6d3da242013-07-03 15:18:35 +0200813 rc = !!deliverable_irqs(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100814
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100815 if (!rc && kvm_cpu_has_pending_timer(vcpu))
816 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100817
David Hildenbrandea5f4962014-10-14 15:29:30 +0200818 /* external call pending and deliverable */
819 if (!rc && kvm_s390_ext_call_pending(vcpu) &&
820 !psw_extint_disabled(vcpu) &&
821 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand49539192014-02-21 08:59:59 +0100822 rc = 1;
823
David Hildenbrand9a022062014-08-05 17:40:47 +0200824 if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
David Hildenbrand6cddd432014-10-15 16:48:53 +0200825 rc = 1;
826
Carsten Otteba5c1e92008-03-25 18:47:26 +0100827 return rc;
828}
829
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300830int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
831{
David Hildenbrandb4aec922014-12-01 15:55:42 +0100832 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300833}
834
Carsten Otteba5c1e92008-03-25 18:47:26 +0100835int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
836{
837 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100838
839 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100840
David Hildenbrand0759d062014-05-13 16:54:32 +0200841 /* fast path */
842 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
843 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200844
Carsten Otteba5c1e92008-03-25 18:47:26 +0100845 if (psw_interrupts_disabled(vcpu)) {
846 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100847 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100848 }
849
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100850 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100851 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +0100852 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100853 goto no_timer;
854 }
855
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200856 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100857 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
David Hildenbrandbda343e2014-12-12 12:26:40 +0100858
859 /* underflow */
860 if (vcpu->arch.sie_block->ckc < now)
861 return 0;
862
863 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200864 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
865 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100866no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200867 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200868 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100869 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200870 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
871
David Hildenbrand2d00f752014-12-11 10:18:01 +0100872 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100873 return 0;
874}
875
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200876void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
877{
878 if (waitqueue_active(&vcpu->wq)) {
879 /*
880 * The vcpu gave up the cpu voluntarily, mark it as a good
881 * yield-candidate.
882 */
883 vcpu->preempted = true;
884 wake_up_interruptible(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +0200885 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200886 }
887}
888
Christian Borntraegerca872302009-05-12 17:21:49 +0200889enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
890{
891 struct kvm_vcpu *vcpu;
David Hildenbrand2d00f752014-12-11 10:18:01 +0100892 u64 now, sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +0200893
894 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrand2d00f752014-12-11 10:18:01 +0100895 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
896 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200897
David Hildenbrand2d00f752014-12-11 10:18:01 +0100898 /*
899 * If the monotonic clock runs faster than the tod clock we might be
900 * woken up too early and have to go back to sleep to avoid deadlocks.
901 */
902 if (vcpu->arch.sie_block->ckc > now &&
903 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
904 return HRTIMER_RESTART;
905 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200906 return HRTIMER_NORESTART;
907}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100908
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100909void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
910{
911 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100912
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200913 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200914 li->pending_irqs = 0;
915 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
916 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200917 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100918
919 /* clear pending external calls set by sigp interpretation facility */
Jens Freimann383d0b02014-07-29 15:11:49 +0200920 atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200921 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100922}
923
Christian Borntraeger614aeab2014-08-25 12:27:29 +0200924int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100925{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200926 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200927 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +0200928 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200929 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +0200930 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100931
932 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100933
Jens Freimann383d0b02014-07-29 15:11:49 +0200934 /* pending ckc conditions might have been invalidated */
935 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +0100936 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +0200937 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
938
David Hildenbrandb4aec922014-12-01 15:55:42 +0100939 /* pending cpu timer conditions might have been invalidated */
940 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
941 if (cpu_timer_irq_pending(vcpu))
942 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
943
Jens Freimann383d0b02014-07-29 15:11:49 +0200944 do {
Jens Freimann6d3da242013-07-03 15:18:35 +0200945 irqs = deliverable_irqs(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200946 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +0200947 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann383d0b02014-07-29 15:11:49 +0200948 if (irq_type == IRQ_PEND_COUNT)
949 break;
Jens Freimann6d3da242013-07-03 15:18:35 +0200950 if (is_ioirq(irq_type)) {
951 rc = __deliver_io(vcpu, irq_type);
952 } else {
953 func = deliver_irq_funcs[irq_type];
954 if (!func) {
955 WARN_ON_ONCE(func == NULL);
956 clear_bit(irq_type, &li->pending_irqs);
957 continue;
958 }
959 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200960 }
Jens Freimann6d3da242013-07-03 15:18:35 +0200961 if (rc)
962 break;
963 } while (!rc);
Jens Freimann383d0b02014-07-29 15:11:49 +0200964
Jens Freimann6d3da242013-07-03 15:18:35 +0200965 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +0200966
967 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100968}
969
Jens Freimann383d0b02014-07-29 15:11:49 +0200970static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +0200971{
972 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
973
Jens Freimann383d0b02014-07-29 15:11:49 +0200974 li->irq.pgm = irq->u.pgm;
Jens Freimann91851242014-12-01 16:43:40 +0100975 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +0200976 return 0;
977}
978
Carsten Otteba5c1e92008-03-25 18:47:26 +0100979int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
980{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200981 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200982 struct kvm_s390_irq irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100983
984 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Jens Freimann383d0b02014-07-29 15:11:49 +0200985 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, code,
986 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200987 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200988 irq.u.pgm.code = code;
989 __inject_prog(vcpu, &irq);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200990 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200991 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100992 return 0;
993}
994
Jens Freimannbcd84682014-02-11 11:07:05 +0100995int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
996 struct kvm_s390_pgm_info *pgm_info)
997{
998 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200999 struct kvm_s390_irq irq;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001000 int rc;
Jens Freimannbcd84682014-02-11 11:07:05 +01001001
Jens Freimannbcd84682014-02-11 11:07:05 +01001002 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
1003 pgm_info->code);
1004 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1005 pgm_info->code, 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001006 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001007 irq.u.pgm = *pgm_info;
1008 rc = __inject_prog(vcpu, &irq);
Jens Freimannbcd84682014-02-11 11:07:05 +01001009 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001010 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001011 return rc;
1012}
1013
Jens Freimann383d0b02014-07-29 15:11:49 +02001014static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001015{
1016 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1017
Jens Freimann383d0b02014-07-29 15:11:49 +02001018 VCPU_EVENT(vcpu, 3, "inject: external irq params:%x, params2:%llx",
1019 irq->u.ext.ext_params, irq->u.ext.ext_params2);
1020 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1021 irq->u.ext.ext_params,
1022 irq->u.ext.ext_params2, 2);
1023
1024 li->irq.ext = irq->u.ext;
1025 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001026 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1027 return 0;
1028}
1029
David Hildenbrandea5f4962014-10-14 15:29:30 +02001030static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
1031{
1032 unsigned char new_val, old_val;
1033 uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
1034
1035 new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
1036 old_val = *sigp_ctrl & ~SIGP_CTRL_C;
1037 if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
1038 /* another external call is pending */
1039 return -EBUSY;
1040 }
1041 atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
1042 return 0;
1043}
1044
Christian Borntraeger0675d922015-01-15 12:40:42 +01001045static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001046{
1047 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001048 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001049 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001050
1051 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001052 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001053 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbrandea5f4962014-10-14 15:29:30 +02001054 src_id, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001055
David Hildenbrandea5f4962014-10-14 15:29:30 +02001056 /* sending vcpu invalid */
1057 if (src_id >= KVM_MAX_VCPUS ||
1058 kvm_get_vcpu(vcpu->kvm, src_id) == NULL)
1059 return -EINVAL;
1060
1061 if (sclp_has_sigpif())
1062 return __inject_extcall_sigpif(vcpu, src_id);
1063
David Hildenbrandb938eace2015-04-30 13:33:59 +02001064 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001065 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001066 *extcall = irq->u.extcall;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001067 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1068 return 0;
1069}
1070
Jens Freimann383d0b02014-07-29 15:11:49 +02001071static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001072{
1073 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001074 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001075
1076 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001077 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001078 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
Jens Freimann556cc0d2014-12-18 15:52:21 +01001079 irq->u.prefix.address, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001080
David Hildenbranda3a9c592014-10-14 09:44:55 +02001081 if (!is_vcpu_stopped(vcpu))
1082 return -EBUSY;
1083
Jens Freimann383d0b02014-07-29 15:11:49 +02001084 *prefix = irq->u.prefix;
1085 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001086 return 0;
1087}
1088
David Hildenbrand6cddd432014-10-15 16:48:53 +02001089#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001090static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001091{
1092 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001093 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001094 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001095
Jens Freimann383d0b02014-07-29 15:11:49 +02001096 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0, 2);
1097
David Hildenbrand28225452014-10-15 16:48:16 +02001098 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1099 return -EINVAL;
1100
David Hildenbrand6cddd432014-10-15 16:48:53 +02001101 if (is_vcpu_stopped(vcpu)) {
1102 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1103 rc = kvm_s390_store_status_unloaded(vcpu,
1104 KVM_S390_STORE_STATUS_NOADDR);
1105 return rc;
1106 }
1107
1108 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1109 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001110 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001111 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001112 return 0;
1113}
1114
1115static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001116 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001117{
1118 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1119
Jens Freimann383d0b02014-07-29 15:11:49 +02001120 VCPU_EVENT(vcpu, 3, "inject: restart type %llx", irq->type);
1121 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0, 2);
1122
1123 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001124 return 0;
1125}
1126
1127static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001128 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001129{
1130 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1131
Jens Freimann383d0b02014-07-29 15:11:49 +02001132 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n",
1133 irq->u.emerg.code);
1134 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
Jens Freimann49538d12014-12-18 15:48:14 +01001135 irq->u.emerg.code, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001136
Jens Freimann49538d12014-12-18 15:48:14 +01001137 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001138 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001139 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1140 return 0;
1141}
1142
Jens Freimann383d0b02014-07-29 15:11:49 +02001143static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001144{
1145 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001146 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001147
1148 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001149 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001150 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
Jens Freimann556cc0d2014-12-18 15:52:21 +01001151 irq->u.mchk.mcic, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001152
1153 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001154 * Because repressible machine checks can be indicated along with
1155 * exigent machine checks (PoP, Chapter 11, Interruption action)
1156 * we need to combine cr14, mcic and external damage code.
1157 * Failing storage address and the logout area should not be or'ed
1158 * together, we just indicate the last occurrence of the corresponding
1159 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001160 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001161 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001162 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001163 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1164 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1165 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1166 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001167 if (mchk->mcic & MCHK_EX_MASK)
1168 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1169 else if (mchk->mcic & MCHK_REP_MASK)
1170 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001171 return 0;
1172}
1173
Jens Freimann383d0b02014-07-29 15:11:49 +02001174static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001175{
1176 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1177
Jens Freimann383d0b02014-07-29 15:11:49 +02001178 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CLOCK_COMP);
1179 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1180 0, 0, 2);
1181
1182 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001183 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1184 return 0;
1185}
1186
Jens Freimann383d0b02014-07-29 15:11:49 +02001187static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001188{
1189 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1190
Jens Freimann383d0b02014-07-29 15:11:49 +02001191 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CPU_TIMER);
1192 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1193 0, 0, 2);
1194
1195 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001196 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001197 return 0;
1198}
1199
Jens Freimann6d3da242013-07-03 15:18:35 +02001200static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1201 int isc, u32 schid)
1202{
1203 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1204 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1205 struct kvm_s390_interrupt_info *iter;
1206 u16 id = (schid & 0xffff0000U) >> 16;
1207 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001208
Jens Freimann6d3da242013-07-03 15:18:35 +02001209 spin_lock(&fi->lock);
1210 list_for_each_entry(iter, isc_list, list) {
1211 if (schid && (id != iter->io.subchannel_id ||
1212 nr != iter->io.subchannel_nr))
1213 continue;
1214 /* found an appropriate entry */
1215 list_del_init(&iter->list);
1216 fi->counters[FIRQ_CNTR_IO] -= 1;
1217 if (list_empty(isc_list))
1218 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1219 spin_unlock(&fi->lock);
1220 return iter;
1221 }
1222 spin_unlock(&fi->lock);
1223 return NULL;
1224}
1225
1226/*
1227 * Dequeue and return an I/O interrupt matching any of the interruption
1228 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1229 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001230struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001231 u64 isc_mask, u32 schid)
1232{
1233 struct kvm_s390_interrupt_info *inti = NULL;
1234 int isc;
1235
1236 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1237 if (isc_mask & isc_to_isc_bits(isc))
1238 inti = get_io_int(kvm, isc, schid);
1239 }
1240 return inti;
1241}
1242
1243#define SCCB_MASK 0xFFFFFFF8
1244#define SCCB_EVENT_PENDING 0x3
1245
1246static int __inject_service(struct kvm *kvm,
1247 struct kvm_s390_interrupt_info *inti)
1248{
1249 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1250
1251 spin_lock(&fi->lock);
1252 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1253 /*
1254 * Early versions of the QEMU s390 bios will inject several
1255 * service interrupts after another without handling a
1256 * condition code indicating busy.
1257 * We will silently ignore those superfluous sccb values.
1258 * A future version of QEMU will take care of serialization
1259 * of servc requests
1260 */
1261 if (fi->srv_signal.ext_params & SCCB_MASK)
1262 goto out;
1263 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1264 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1265out:
1266 spin_unlock(&fi->lock);
1267 kfree(inti);
1268 return 0;
1269}
1270
1271static int __inject_virtio(struct kvm *kvm,
1272 struct kvm_s390_interrupt_info *inti)
1273{
1274 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1275
1276 spin_lock(&fi->lock);
1277 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1278 spin_unlock(&fi->lock);
1279 return -EBUSY;
1280 }
1281 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1282 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1283 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1284 spin_unlock(&fi->lock);
1285 return 0;
1286}
1287
1288static int __inject_pfault_done(struct kvm *kvm,
1289 struct kvm_s390_interrupt_info *inti)
1290{
1291 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1292
1293 spin_lock(&fi->lock);
1294 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1295 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1296 spin_unlock(&fi->lock);
1297 return -EBUSY;
1298 }
1299 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1300 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1301 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1302 spin_unlock(&fi->lock);
1303 return 0;
1304}
1305
1306#define CR_PENDING_SUBCLASS 28
1307static int __inject_float_mchk(struct kvm *kvm,
1308 struct kvm_s390_interrupt_info *inti)
1309{
1310 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1311
1312 spin_lock(&fi->lock);
1313 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1314 fi->mchk.mcic |= inti->mchk.mcic;
1315 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1316 spin_unlock(&fi->lock);
1317 kfree(inti);
1318 return 0;
1319}
1320
1321static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001322{
1323 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001324 struct list_head *list;
1325 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001326
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001327 fi = &kvm->arch.float_int;
1328 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001329 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1330 spin_unlock(&fi->lock);
1331 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001332 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001333 fi->counters[FIRQ_CNTR_IO] += 1;
1334
1335 isc = int_word_to_isc(inti->io.io_int_word);
1336 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1337 list_add_tail(&inti->list, list);
1338 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001339 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001340 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001341}
1342
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001343static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001344{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001345 struct kvm_s390_local_interrupt *li;
1346 struct kvm_s390_float_interrupt *fi;
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001347 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001348 int sigcpu;
Jens Freimann6d3da242013-07-03 15:18:35 +02001349 u64 type = READ_ONCE(inti->type);
1350 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001351
Carsten Otteba5c1e92008-03-25 18:47:26 +01001352 fi = &kvm->arch.float_int;
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001353
Jens Freimann6d3da242013-07-03 15:18:35 +02001354 switch (type) {
1355 case KVM_S390_MCHK:
1356 rc = __inject_float_mchk(kvm, inti);
1357 break;
1358 case KVM_S390_INT_VIRTIO:
1359 rc = __inject_virtio(kvm, inti);
1360 break;
1361 case KVM_S390_INT_SERVICE:
1362 rc = __inject_service(kvm, inti);
1363 break;
1364 case KVM_S390_INT_PFAULT_DONE:
1365 rc = __inject_pfault_done(kvm, inti);
1366 break;
1367 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1368 rc = __inject_io(kvm, inti);
1369 break;
1370 default:
1371 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001372 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001373 if (rc)
1374 return rc;
1375
Carsten Otteba5c1e92008-03-25 18:47:26 +01001376 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
1377 if (sigcpu == KVM_MAX_VCPUS) {
1378 do {
1379 sigcpu = fi->next_rr_cpu++;
1380 if (sigcpu == KVM_MAX_VCPUS)
1381 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001382 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001383 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001384 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1385 li = &dst_vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001386 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001387 switch (type) {
Christian Borntraegerda00fcb2014-11-21 09:38:12 +01001388 case KVM_S390_MCHK:
1389 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
1390 break;
1391 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1392 atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags);
1393 break;
1394 default:
1395 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1396 break;
1397 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001398 spin_unlock(&li->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001399 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
Jens Freimann6d3da242013-07-03 15:18:35 +02001400 return 0;
1401
Jens Freimannc05c4182013-10-07 16:13:45 +02001402}
1403
1404int kvm_s390_inject_vm(struct kvm *kvm,
1405 struct kvm_s390_interrupt *s390int)
1406{
1407 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001408 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001409
1410 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1411 if (!inti)
1412 return -ENOMEM;
1413
1414 inti->type = s390int->type;
1415 switch (inti->type) {
1416 case KVM_S390_INT_VIRTIO:
1417 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1418 s390int->parm, s390int->parm64);
1419 inti->ext.ext_params = s390int->parm;
1420 inti->ext.ext_params2 = s390int->parm64;
1421 break;
1422 case KVM_S390_INT_SERVICE:
1423 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
1424 inti->ext.ext_params = s390int->parm;
1425 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001426 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001427 inti->ext.ext_params2 = s390int->parm64;
1428 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001429 case KVM_S390_MCHK:
1430 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
1431 s390int->parm64);
1432 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1433 inti->mchk.mcic = s390int->parm64;
1434 break;
1435 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1436 if (inti->type & IOINT_AI_MASK)
1437 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1438 else
1439 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1440 s390int->type & IOINT_CSSID_MASK,
1441 s390int->type & IOINT_SSID_MASK,
1442 s390int->type & IOINT_SCHID_MASK);
1443 inti->io.subchannel_id = s390int->parm >> 16;
1444 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1445 inti->io.io_int_parm = s390int->parm64 >> 32;
1446 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1447 break;
1448 default:
1449 kfree(inti);
1450 return -EINVAL;
1451 }
1452 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1453 2);
1454
David Hildenbrand428d53b2015-01-16 12:58:09 +01001455 rc = __inject_vm(kvm, inti);
1456 if (rc)
1457 kfree(inti);
1458 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001459}
1460
David Hildenbrand15462e32015-02-04 15:59:11 +01001461int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001462 struct kvm_s390_interrupt_info *inti)
1463{
David Hildenbrand15462e32015-02-04 15:59:11 +01001464 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001465}
1466
Jens Freimann383d0b02014-07-29 15:11:49 +02001467int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1468 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001469{
Jens Freimann383d0b02014-07-29 15:11:49 +02001470 irq->type = s390int->type;
1471 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001472 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001473 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001474 return -EINVAL;
1475 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001476 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001477 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001478 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001479 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001480 case KVM_S390_SIGP_STOP:
1481 irq->u.stop.flags = s390int->parm;
1482 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001483 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001484 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001485 return -EINVAL;
1486 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001487 break;
1488 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001489 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001490 return -EINVAL;
1491 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001492 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001493 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001494 irq->u.mchk.mcic = s390int->parm64;
1495 break;
1496 }
1497 return 0;
1498}
1499
David Hildenbrand6cddd432014-10-15 16:48:53 +02001500int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1501{
1502 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1503
1504 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1505}
1506
1507void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1508{
1509 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1510
1511 spin_lock(&li->lock);
1512 li->irq.stop.flags = 0;
1513 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1514 spin_unlock(&li->lock);
1515}
1516
Jens Freimann79e87a12015-03-19 15:12:12 +01001517static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001518{
Jens Freimann383d0b02014-07-29 15:11:49 +02001519 int rc;
1520
Jens Freimann383d0b02014-07-29 15:11:49 +02001521 switch (irq->type) {
1522 case KVM_S390_PROGRAM_INT:
1523 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
1524 irq->u.pgm.code);
1525 rc = __inject_prog(vcpu, irq);
1526 break;
1527 case KVM_S390_SIGP_SET_PREFIX:
1528 rc = __inject_set_prefix(vcpu, irq);
1529 break;
1530 case KVM_S390_SIGP_STOP:
1531 rc = __inject_sigp_stop(vcpu, irq);
1532 break;
1533 case KVM_S390_RESTART:
1534 rc = __inject_sigp_restart(vcpu, irq);
1535 break;
1536 case KVM_S390_INT_CLOCK_COMP:
1537 rc = __inject_ckc(vcpu);
1538 break;
1539 case KVM_S390_INT_CPU_TIMER:
1540 rc = __inject_cpu_timer(vcpu);
1541 break;
1542 case KVM_S390_INT_EXTERNAL_CALL:
1543 rc = __inject_extcall(vcpu, irq);
1544 break;
1545 case KVM_S390_INT_EMERGENCY:
1546 rc = __inject_sigp_emergency(vcpu, irq);
1547 break;
1548 case KVM_S390_MCHK:
1549 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001550 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001551 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001552 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001553 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001554 case KVM_S390_INT_VIRTIO:
1555 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001556 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001557 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001558 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001559 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001560
1561 return rc;
1562}
1563
1564int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1565{
1566 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1567 int rc;
1568
1569 spin_lock(&li->lock);
1570 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001571 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001572 if (!rc)
1573 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001574 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001575}
Jens Freimannc05c4182013-10-07 16:13:45 +02001576
Jens Freimann6d3da242013-07-03 15:18:35 +02001577static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001578{
Jens Freimann6d3da242013-07-03 15:18:35 +02001579 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001580
Jens Freimann6d3da242013-07-03 15:18:35 +02001581 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001582 list_del(&inti->list);
1583 kfree(inti);
1584 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001585}
1586
Jens Freimann94aa0332015-03-16 12:17:13 +01001587static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1588 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001589{
Jens Freimann94aa0332015-03-16 12:17:13 +01001590 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001591 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001592 case KVM_S390_INT_PFAULT_INIT:
1593 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001594 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001595 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001596 break;
1597 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001598 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001599 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001600 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001601}
1602
Jens Freimann6d3da242013-07-03 15:18:35 +02001603void kvm_s390_clear_float_irqs(struct kvm *kvm)
1604{
1605 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1606 int i;
1607
1608 spin_lock(&fi->lock);
1609 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1610 clear_irq_list(&fi->lists[i]);
1611 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1612 fi->counters[i] = 0;
1613 spin_unlock(&fi->lock);
1614};
1615
Jens Freimann94aa0332015-03-16 12:17:13 +01001616static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001617{
1618 struct kvm_s390_interrupt_info *inti;
1619 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001620 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001621 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001622 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001623 int ret = 0;
1624 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001625 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001626
Jens Freimann94aa0332015-03-16 12:17:13 +01001627 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1628 return -EINVAL;
1629
1630 /*
1631 * We are already using -ENOMEM to signal
1632 * userspace it may retry with a bigger buffer,
1633 * so we need to use something else for this case
1634 */
1635 buf = vzalloc(len);
1636 if (!buf)
1637 return -ENOBUFS;
1638
1639 max_irqs = len / sizeof(struct kvm_s390_irq);
1640
Jens Freimannc05c4182013-10-07 16:13:45 +02001641 fi = &kvm->arch.float_int;
1642 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001643 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1644 list_for_each_entry(inti, &fi->lists[i], list) {
1645 if (n == max_irqs) {
1646 /* signal userspace to try again */
1647 ret = -ENOMEM;
1648 goto out;
1649 }
1650 inti_to_irq(inti, &buf[n]);
1651 n++;
1652 }
1653 }
1654 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001655 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001656 /* signal userspace to try again */
1657 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001658 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001659 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001660 irq = (struct kvm_s390_irq *) &buf[n];
1661 irq->type = KVM_S390_INT_SERVICE;
1662 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001663 n++;
1664 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001665 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1666 if (n == max_irqs) {
1667 /* signal userspace to try again */
1668 ret = -ENOMEM;
1669 goto out;
1670 }
1671 irq = (struct kvm_s390_irq *) &buf[n];
1672 irq->type = KVM_S390_MCHK;
1673 irq->u.mchk = fi->mchk;
1674 n++;
1675}
1676
1677out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001678 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001679 if (!ret && n > 0) {
1680 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1681 ret = -EFAULT;
1682 }
1683 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001684
1685 return ret < 0 ? ret : n;
1686}
1687
1688static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1689{
1690 int r;
1691
1692 switch (attr->group) {
1693 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001694 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001695 attr->attr);
1696 break;
1697 default:
1698 r = -EINVAL;
1699 }
1700
1701 return r;
1702}
1703
1704static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1705 u64 addr)
1706{
1707 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1708 void *target = NULL;
1709 void __user *source;
1710 u64 size;
1711
1712 if (get_user(inti->type, (u64 __user *)addr))
1713 return -EFAULT;
1714
1715 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001716 case KVM_S390_INT_PFAULT_INIT:
1717 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001718 case KVM_S390_INT_VIRTIO:
1719 case KVM_S390_INT_SERVICE:
1720 target = (void *) &inti->ext;
1721 source = &uptr->u.ext;
1722 size = sizeof(inti->ext);
1723 break;
1724 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1725 target = (void *) &inti->io;
1726 source = &uptr->u.io;
1727 size = sizeof(inti->io);
1728 break;
1729 case KVM_S390_MCHK:
1730 target = (void *) &inti->mchk;
1731 source = &uptr->u.mchk;
1732 size = sizeof(inti->mchk);
1733 break;
1734 default:
1735 return -EINVAL;
1736 }
1737
1738 if (copy_from_user(target, source, size))
1739 return -EFAULT;
1740
1741 return 0;
1742}
1743
1744static int enqueue_floating_irq(struct kvm_device *dev,
1745 struct kvm_device_attr *attr)
1746{
1747 struct kvm_s390_interrupt_info *inti = NULL;
1748 int r = 0;
1749 int len = attr->attr;
1750
1751 if (len % sizeof(struct kvm_s390_irq) != 0)
1752 return -EINVAL;
1753 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1754 return -EINVAL;
1755
1756 while (len >= sizeof(struct kvm_s390_irq)) {
1757 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1758 if (!inti)
1759 return -ENOMEM;
1760
1761 r = copy_irq_from_user(inti, attr->addr);
1762 if (r) {
1763 kfree(inti);
1764 return r;
1765 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001766 r = __inject_vm(dev->kvm, inti);
1767 if (r) {
1768 kfree(inti);
1769 return r;
1770 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001771 len -= sizeof(struct kvm_s390_irq);
1772 attr->addr += sizeof(struct kvm_s390_irq);
1773 }
1774
1775 return r;
1776}
1777
Cornelia Huck841b91c2013-07-15 13:36:01 +02001778static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1779{
1780 if (id >= MAX_S390_IO_ADAPTERS)
1781 return NULL;
1782 return kvm->arch.adapters[id];
1783}
1784
1785static int register_io_adapter(struct kvm_device *dev,
1786 struct kvm_device_attr *attr)
1787{
1788 struct s390_io_adapter *adapter;
1789 struct kvm_s390_io_adapter adapter_info;
1790
1791 if (copy_from_user(&adapter_info,
1792 (void __user *)attr->addr, sizeof(adapter_info)))
1793 return -EFAULT;
1794
1795 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1796 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1797 return -EINVAL;
1798
1799 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1800 if (!adapter)
1801 return -ENOMEM;
1802
1803 INIT_LIST_HEAD(&adapter->maps);
1804 init_rwsem(&adapter->maps_lock);
1805 atomic_set(&adapter->nr_maps, 0);
1806 adapter->id = adapter_info.id;
1807 adapter->isc = adapter_info.isc;
1808 adapter->maskable = adapter_info.maskable;
1809 adapter->masked = false;
1810 adapter->swap = adapter_info.swap;
1811 dev->kvm->arch.adapters[adapter->id] = adapter;
1812
1813 return 0;
1814}
1815
1816int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1817{
1818 int ret;
1819 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1820
1821 if (!adapter || !adapter->maskable)
1822 return -EINVAL;
1823 ret = adapter->masked;
1824 adapter->masked = masked;
1825 return ret;
1826}
1827
1828static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1829{
1830 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1831 struct s390_map_info *map;
1832 int ret;
1833
1834 if (!adapter || !addr)
1835 return -EINVAL;
1836
1837 map = kzalloc(sizeof(*map), GFP_KERNEL);
1838 if (!map) {
1839 ret = -ENOMEM;
1840 goto out;
1841 }
1842 INIT_LIST_HEAD(&map->list);
1843 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02001844 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001845 if (map->addr == -EFAULT) {
1846 ret = -EFAULT;
1847 goto out;
1848 }
1849 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1850 if (ret < 0)
1851 goto out;
1852 BUG_ON(ret != 1);
1853 down_write(&adapter->maps_lock);
1854 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1855 list_add_tail(&map->list, &adapter->maps);
1856 ret = 0;
1857 } else {
1858 put_page(map->page);
1859 ret = -EINVAL;
1860 }
1861 up_write(&adapter->maps_lock);
1862out:
1863 if (ret)
1864 kfree(map);
1865 return ret;
1866}
1867
1868static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1869{
1870 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1871 struct s390_map_info *map, *tmp;
1872 int found = 0;
1873
1874 if (!adapter || !addr)
1875 return -EINVAL;
1876
1877 down_write(&adapter->maps_lock);
1878 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1879 if (map->guest_addr == addr) {
1880 found = 1;
1881 atomic_dec(&adapter->nr_maps);
1882 list_del(&map->list);
1883 put_page(map->page);
1884 kfree(map);
1885 break;
1886 }
1887 }
1888 up_write(&adapter->maps_lock);
1889
1890 return found ? 0 : -EINVAL;
1891}
1892
1893void kvm_s390_destroy_adapters(struct kvm *kvm)
1894{
1895 int i;
1896 struct s390_map_info *map, *tmp;
1897
1898 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1899 if (!kvm->arch.adapters[i])
1900 continue;
1901 list_for_each_entry_safe(map, tmp,
1902 &kvm->arch.adapters[i]->maps, list) {
1903 list_del(&map->list);
1904 put_page(map->page);
1905 kfree(map);
1906 }
1907 kfree(kvm->arch.adapters[i]);
1908 }
1909}
1910
1911static int modify_io_adapter(struct kvm_device *dev,
1912 struct kvm_device_attr *attr)
1913{
1914 struct kvm_s390_io_adapter_req req;
1915 struct s390_io_adapter *adapter;
1916 int ret;
1917
1918 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1919 return -EFAULT;
1920
1921 adapter = get_io_adapter(dev->kvm, req.id);
1922 if (!adapter)
1923 return -EINVAL;
1924 switch (req.type) {
1925 case KVM_S390_IO_ADAPTER_MASK:
1926 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1927 if (ret > 0)
1928 ret = 0;
1929 break;
1930 case KVM_S390_IO_ADAPTER_MAP:
1931 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1932 break;
1933 case KVM_S390_IO_ADAPTER_UNMAP:
1934 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1935 break;
1936 default:
1937 ret = -EINVAL;
1938 }
1939
1940 return ret;
1941}
1942
Jens Freimannc05c4182013-10-07 16:13:45 +02001943static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1944{
1945 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001946 unsigned int i;
1947 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001948
1949 switch (attr->group) {
1950 case KVM_DEV_FLIC_ENQUEUE:
1951 r = enqueue_floating_irq(dev, attr);
1952 break;
1953 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01001954 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001955 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001956 case KVM_DEV_FLIC_APF_ENABLE:
1957 dev->kvm->arch.gmap->pfault_enabled = 1;
1958 break;
1959 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1960 dev->kvm->arch.gmap->pfault_enabled = 0;
1961 /*
1962 * Make sure no async faults are in transition when
1963 * clearing the queues. So we don't need to worry
1964 * about late coming workers.
1965 */
1966 synchronize_srcu(&dev->kvm->srcu);
1967 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1968 kvm_clear_async_pf_completion_queue(vcpu);
1969 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001970 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1971 r = register_io_adapter(dev, attr);
1972 break;
1973 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1974 r = modify_io_adapter(dev, attr);
1975 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001976 default:
1977 r = -EINVAL;
1978 }
1979
1980 return r;
1981}
1982
1983static int flic_create(struct kvm_device *dev, u32 type)
1984{
1985 if (!dev)
1986 return -EINVAL;
1987 if (dev->kvm->arch.flic)
1988 return -EINVAL;
1989 dev->kvm->arch.flic = dev;
1990 return 0;
1991}
1992
1993static void flic_destroy(struct kvm_device *dev)
1994{
1995 dev->kvm->arch.flic = NULL;
1996 kfree(dev);
1997}
1998
1999/* s390 floating irq controller (flic) */
2000struct kvm_device_ops kvm_flic_ops = {
2001 .name = "kvm-flic",
2002 .get_attr = flic_get_attr,
2003 .set_attr = flic_set_attr,
2004 .create = flic_create,
2005 .destroy = flic_destroy,
2006};
Cornelia Huck84223592013-07-15 13:36:01 +02002007
2008static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2009{
2010 unsigned long bit;
2011
2012 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2013
2014 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2015}
2016
2017static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2018 u64 addr)
2019{
2020 struct s390_map_info *map;
2021
2022 if (!adapter)
2023 return NULL;
2024
2025 list_for_each_entry(map, &adapter->maps, list) {
2026 if (map->guest_addr == addr)
2027 return map;
2028 }
2029 return NULL;
2030}
2031
2032static int adapter_indicators_set(struct kvm *kvm,
2033 struct s390_io_adapter *adapter,
2034 struct kvm_s390_adapter_int *adapter_int)
2035{
2036 unsigned long bit;
2037 int summary_set, idx;
2038 struct s390_map_info *info;
2039 void *map;
2040
2041 info = get_map_info(adapter, adapter_int->ind_addr);
2042 if (!info)
2043 return -1;
2044 map = page_address(info->page);
2045 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2046 set_bit(bit, map);
2047 idx = srcu_read_lock(&kvm->srcu);
2048 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2049 set_page_dirty_lock(info->page);
2050 info = get_map_info(adapter, adapter_int->summary_addr);
2051 if (!info) {
2052 srcu_read_unlock(&kvm->srcu, idx);
2053 return -1;
2054 }
2055 map = page_address(info->page);
2056 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2057 adapter->swap);
2058 summary_set = test_and_set_bit(bit, map);
2059 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2060 set_page_dirty_lock(info->page);
2061 srcu_read_unlock(&kvm->srcu, idx);
2062 return summary_set ? 0 : 1;
2063}
2064
2065/*
2066 * < 0 - not injected due to error
2067 * = 0 - coalesced, summary indicator already active
2068 * > 0 - injected interrupt
2069 */
2070static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2071 struct kvm *kvm, int irq_source_id, int level,
2072 bool line_status)
2073{
2074 int ret;
2075 struct s390_io_adapter *adapter;
2076
2077 /* We're only interested in the 0->1 transition. */
2078 if (!level)
2079 return 0;
2080 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2081 if (!adapter)
2082 return -1;
2083 down_read(&adapter->maps_lock);
2084 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2085 up_read(&adapter->maps_lock);
2086 if ((ret > 0) && !adapter->masked) {
2087 struct kvm_s390_interrupt s390int = {
2088 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2089 .parm = 0,
2090 .parm64 = (adapter->isc << 27) | 0x80000000,
2091 };
2092 ret = kvm_s390_inject_vm(kvm, &s390int);
2093 if (ret == 0)
2094 ret = 1;
2095 }
2096 return ret;
2097}
2098
Paul Mackerras8ba918d2014-06-30 20:51:10 +10002099int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002100 const struct kvm_irq_routing_entry *ue)
2101{
2102 int ret;
2103
2104 switch (ue->type) {
2105 case KVM_IRQ_ROUTING_S390_ADAPTER:
2106 e->set = set_adapter_int;
2107 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2108 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2109 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2110 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2111 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2112 ret = 0;
2113 break;
2114 default:
2115 ret = -EINVAL;
2116 }
2117
2118 return ret;
2119}
2120
2121int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2122 int irq_source_id, int level, bool line_status)
2123{
2124 return -EINVAL;
2125}
Jens Freimann816c7662014-11-24 17:13:46 +01002126
2127int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2128{
2129 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2130 struct kvm_s390_irq *buf;
2131 int r = 0;
2132 int n;
2133
2134 buf = vmalloc(len);
2135 if (!buf)
2136 return -ENOMEM;
2137
2138 if (copy_from_user((void *) buf, irqstate, len)) {
2139 r = -EFAULT;
2140 goto out_free;
2141 }
2142
2143 /*
2144 * Don't allow setting the interrupt state
2145 * when there are already interrupts pending
2146 */
2147 spin_lock(&li->lock);
2148 if (li->pending_irqs) {
2149 r = -EBUSY;
2150 goto out_unlock;
2151 }
2152
2153 for (n = 0; n < len / sizeof(*buf); n++) {
2154 r = do_inject_vcpu(vcpu, &buf[n]);
2155 if (r)
2156 break;
2157 }
2158
2159out_unlock:
2160 spin_unlock(&li->lock);
2161out_free:
2162 vfree(buf);
2163
2164 return r;
2165}
2166
2167static void store_local_irq(struct kvm_s390_local_interrupt *li,
2168 struct kvm_s390_irq *irq,
2169 unsigned long irq_type)
2170{
2171 switch (irq_type) {
2172 case IRQ_PEND_MCHK_EX:
2173 case IRQ_PEND_MCHK_REP:
2174 irq->type = KVM_S390_MCHK;
2175 irq->u.mchk = li->irq.mchk;
2176 break;
2177 case IRQ_PEND_PROG:
2178 irq->type = KVM_S390_PROGRAM_INT;
2179 irq->u.pgm = li->irq.pgm;
2180 break;
2181 case IRQ_PEND_PFAULT_INIT:
2182 irq->type = KVM_S390_INT_PFAULT_INIT;
2183 irq->u.ext = li->irq.ext;
2184 break;
2185 case IRQ_PEND_EXT_EXTERNAL:
2186 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2187 irq->u.extcall = li->irq.extcall;
2188 break;
2189 case IRQ_PEND_EXT_CLOCK_COMP:
2190 irq->type = KVM_S390_INT_CLOCK_COMP;
2191 break;
2192 case IRQ_PEND_EXT_CPU_TIMER:
2193 irq->type = KVM_S390_INT_CPU_TIMER;
2194 break;
2195 case IRQ_PEND_SIGP_STOP:
2196 irq->type = KVM_S390_SIGP_STOP;
2197 irq->u.stop = li->irq.stop;
2198 break;
2199 case IRQ_PEND_RESTART:
2200 irq->type = KVM_S390_RESTART;
2201 break;
2202 case IRQ_PEND_SET_PREFIX:
2203 irq->type = KVM_S390_SIGP_SET_PREFIX;
2204 irq->u.prefix = li->irq.prefix;
2205 break;
2206 }
2207}
2208
2209int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2210{
2211 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
2212 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2213 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2214 unsigned long pending_irqs;
2215 struct kvm_s390_irq irq;
2216 unsigned long irq_type;
2217 int cpuaddr;
2218 int n = 0;
2219
2220 spin_lock(&li->lock);
2221 pending_irqs = li->pending_irqs;
2222 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2223 sizeof(sigp_emerg_pending));
2224 spin_unlock(&li->lock);
2225
2226 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2227 memset(&irq, 0, sizeof(irq));
2228 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2229 continue;
2230 if (n + sizeof(irq) > len)
2231 return -ENOBUFS;
2232 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2233 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2234 return -EFAULT;
2235 n += sizeof(irq);
2236 }
2237
2238 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2239 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2240 memset(&irq, 0, sizeof(irq));
2241 if (n + sizeof(irq) > len)
2242 return -ENOBUFS;
2243 irq.type = KVM_S390_INT_EMERGENCY;
2244 irq.u.emerg.code = cpuaddr;
2245 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2246 return -EFAULT;
2247 n += sizeof(irq);
2248 }
2249 }
2250
2251 if ((sigp_ctrl & SIGP_CTRL_C) &&
2252 (atomic_read(&vcpu->arch.sie_block->cpuflags) &
2253 CPUSTAT_ECALL_PEND)) {
2254 if (n + sizeof(irq) > len)
2255 return -ENOBUFS;
2256 memset(&irq, 0, sizeof(irq));
2257 irq.type = KVM_S390_INT_EXTERNAL_CALL;
2258 irq.u.extcall.code = sigp_ctrl & SIGP_CTRL_SCN_MASK;
2259 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2260 return -EFAULT;
2261 n += sizeof(irq);
2262 }
2263
2264 return n;
2265}