blob: aa221a48cc7c12060126617605d88d8e19c6a562 [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
Jens Freimann44c6ca32014-04-16 13:57:18 +020033#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010034#define PFAULT_DONE 0x0680
35#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010036
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020037/* handle external calls via sigp interpretation facility */
38static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id)
39{
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020040 struct bsca_block *sca = vcpu->kvm->arch.sca;
41 union bsca_sigp_ctrl sigp_ctrl = sca->cpu[vcpu->vcpu_id].sigp_ctrl;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020042
43 if (src_id)
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020044 *src_id = sigp_ctrl.scn;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020045
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020046 return sigp_ctrl.c &&
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020047 atomic_read(&vcpu->arch.sie_block->cpuflags) &
48 CPUSTAT_ECALL_PEND;
49}
50
51static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
52{
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020053 int expect, rc;
54 struct bsca_block *sca = vcpu->kvm->arch.sca;
55 union bsca_sigp_ctrl *sigp_ctrl = &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
56 union bsca_sigp_ctrl new_val = {0}, old_val = *sigp_ctrl;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020057
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020058 new_val.scn = src_id;
59 new_val.c = 1;
60 old_val.c = 0;
61
62 expect = old_val.value;
63 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
64
65 if (rc != expect) {
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020066 /* another external call is pending */
67 return -EBUSY;
68 }
69 atomic_or(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
70 return 0;
71}
72
73static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
74{
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020075 struct bsca_block *sca = vcpu->kvm->arch.sca;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020076 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020077 union bsca_sigp_ctrl *sigp_ctrl = &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020078
79 atomic_andnot(CPUSTAT_ECALL_PEND, li->cpuflags);
Eugene (jno) Dvurechenskibc784cc2015-04-23 16:09:06 +020080 sigp_ctrl->value = 0;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +020081}
82
Dominik Dingel3c038e62013-10-07 17:11:48 +020083int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010084{
85 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
86}
87
Cornelia Huckd8346b72012-12-20 15:32:08 +010088static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
89{
90 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
91}
92
Cornelia Huck48a3e952012-12-20 15:32:09 +010093static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
94{
95 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
96}
97
Carsten Otteba5c1e92008-03-25 18:47:26 +010098static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
99{
David Hildenbrandfee0e0fd2015-09-28 13:32:38 +0200100 return psw_extint_disabled(vcpu) &&
101 psw_ioint_disabled(vcpu) &&
102 psw_mchk_disabled(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100103}
104
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100105static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
106{
107 if (psw_extint_disabled(vcpu) ||
108 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
109 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +0100110 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
111 /* No timer interrupts when single stepping */
112 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100113 return 1;
114}
115
David Hildenbrandb4aec922014-12-01 15:55:42 +0100116static int ckc_irq_pending(struct kvm_vcpu *vcpu)
117{
David Hildenbrand60417fc2015-09-29 16:20:36 +0200118 if (vcpu->arch.sie_block->ckc >= kvm_s390_get_tod_clock_fast(vcpu->kvm))
David Hildenbrandb4aec922014-12-01 15:55:42 +0100119 return 0;
120 return ckc_interrupts_enabled(vcpu);
121}
122
123static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
124{
125 return !psw_extint_disabled(vcpu) &&
126 (vcpu->arch.sie_block->gcr[0] & 0x400ul);
127}
128
129static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
130{
131 return (vcpu->arch.sie_block->cputm >> 63) &&
132 cpu_timer_interrupts_enabled(vcpu);
133}
134
Jens Freimann6d3da242013-07-03 15:18:35 +0200135static inline int is_ioirq(unsigned long irq_type)
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100136{
Jens Freimann6d3da242013-07-03 15:18:35 +0200137 return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
138 (irq_type <= IRQ_PEND_IO_ISC_7));
139}
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100140
Jens Freimann6d3da242013-07-03 15:18:35 +0200141static uint64_t isc_to_isc_bits(int isc)
142{
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100143 return (0x80 >> isc) << 24;
144}
145
Jens Freimann6d3da242013-07-03 15:18:35 +0200146static inline u8 int_word_to_isc(u32 int_word)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100147{
Jens Freimann6d3da242013-07-03 15:18:35 +0200148 return (int_word & 0x38000000) >> 27;
149}
150
David Hildenbrand5f94c582015-09-28 14:27:51 +0200151static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
Jens Freimann6d3da242013-07-03 15:18:35 +0200152{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200153 return vcpu->kvm->arch.float_int.pending_irqs |
154 vcpu->arch.local_int.pending_irqs;
Jens Freimann383d0b02014-07-29 15:11:49 +0200155}
156
Jens Freimann6d3da242013-07-03 15:18:35 +0200157static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
158 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200159{
Jens Freimann6d3da242013-07-03 15:18:35 +0200160 int i;
161
162 for (i = 0; i <= MAX_ISC; i++)
163 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
164 active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
165
166 return active_mask;
167}
168
169static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
170{
171 unsigned long active_mask;
172
David Hildenbrand5f94c582015-09-28 14:27:51 +0200173 active_mask = pending_irqs(vcpu);
Jens Freimannffeca0a2015-04-17 10:21:04 +0200174 if (!active_mask)
175 return 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200176
177 if (psw_extint_disabled(vcpu))
178 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200179 if (psw_ioint_disabled(vcpu))
180 active_mask &= ~IRQ_PEND_IO_MASK;
181 else
182 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200183 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
184 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
185 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
186 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
187 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
188 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
189 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
190 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200191 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
192 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200193 if (psw_mchk_disabled(vcpu))
194 active_mask &= ~IRQ_PEND_MCHK_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200195 if (!(vcpu->arch.sie_block->gcr[14] &
196 vcpu->kvm->arch.float_int.mchk.cr14))
197 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200198
David Hildenbrand6cddd432014-10-15 16:48:53 +0200199 /*
200 * STOP irqs will never be actively delivered. They are triggered via
201 * intercept requests and cleared when the stop intercept is performed.
202 */
203 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
204
Jens Freimann383d0b02014-07-29 15:11:49 +0200205 return active_mask;
206}
207
Carsten Otteba5c1e92008-03-25 18:47:26 +0100208static void __set_cpu_idle(struct kvm_vcpu *vcpu)
209{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200210 atomic_or(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100211 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
212}
213
214static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
215{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200216 atomic_andnot(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100217 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
218}
219
220static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
221{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200222 atomic_andnot(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
223 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100224 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100225 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
226
227 if (guestdbg_enabled(vcpu)) {
228 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
229 LCTL_CR10 | LCTL_CR11);
230 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
231 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100232}
233
234static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
235{
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200236 atomic_or(flag, &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100237}
238
Jens Freimann6d3da242013-07-03 15:18:35 +0200239static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
240{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200241 if (!(pending_irqs(vcpu) & IRQ_PEND_IO_MASK))
Jens Freimann6d3da242013-07-03 15:18:35 +0200242 return;
243 else if (psw_ioint_disabled(vcpu))
244 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
245 else
246 vcpu->arch.sie_block->lctl |= LCTL_CR6;
247}
248
Jens Freimann383d0b02014-07-29 15:11:49 +0200249static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
250{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200251 if (!(pending_irqs(vcpu) & IRQ_PEND_EXT_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200252 return;
253 if (psw_extint_disabled(vcpu))
254 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
255 else
256 vcpu->arch.sie_block->lctl |= LCTL_CR0;
257}
258
259static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
260{
David Hildenbrand5f94c582015-09-28 14:27:51 +0200261 if (!(pending_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
Jens Freimann383d0b02014-07-29 15:11:49 +0200262 return;
263 if (psw_mchk_disabled(vcpu))
264 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
265 else
266 vcpu->arch.sie_block->lctl |= LCTL_CR14;
267}
268
David Hildenbrand6cddd432014-10-15 16:48:53 +0200269static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
270{
271 if (kvm_s390_is_stop_irq_pending(vcpu))
272 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
273}
274
Jens Freimann6d3da242013-07-03 15:18:35 +0200275/* Set interception request for non-deliverable interrupts */
276static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200277{
Jens Freimann6d3da242013-07-03 15:18:35 +0200278 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200279 set_intercept_indicators_ext(vcpu);
280 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200281 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200282}
283
Jens Freimann8a2ef712014-07-23 16:36:06 +0200284static u16 get_ilc(struct kvm_vcpu *vcpu)
285{
Jens Freimann8a2ef712014-07-23 16:36:06 +0200286 switch (vcpu->arch.sie_block->icptcode) {
287 case ICPT_INST:
288 case ICPT_INSTPROGI:
289 case ICPT_OPEREXC:
290 case ICPT_PARTEXEC:
291 case ICPT_IOINST:
292 /* last instruction only stored for these icptcodes */
Thomas Huth33b412a2015-02-11 10:38:46 +0100293 return insn_length(vcpu->arch.sie_block->ipa >> 8);
Jens Freimann8a2ef712014-07-23 16:36:06 +0200294 case ICPT_PROGI:
295 return vcpu->arch.sie_block->pgmilc;
296 default:
297 return 0;
298 }
299}
300
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200301static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100302{
Jens Freimann383d0b02014-07-29 15:11:49 +0200303 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200304 int rc;
305
306 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
307 0, 0);
308
309 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
310 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100311 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200312 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
313 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
314 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
315 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200316 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100317 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200318}
319
320static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
321{
Jens Freimann383d0b02014-07-29 15:11:49 +0200322 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200323 int rc;
324
325 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
326 0, 0);
327
328 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
329 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100330 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200331 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
332 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
333 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
334 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200335 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100336 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200337}
338
Jens Freimann383d0b02014-07-29 15:11:49 +0200339static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200340{
Jens Freimann383d0b02014-07-29 15:11:49 +0200341 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
342 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200343 int rc;
344
Jens Freimann383d0b02014-07-29 15:11:49 +0200345 spin_lock(&li->lock);
346 ext = li->irq.ext;
347 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
348 li->irq.ext.ext_params2 = 0;
349 spin_unlock(&li->lock);
350
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200351 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
352 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200353 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
354 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200355 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200356
357 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
358 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
359 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
360 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
361 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
362 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200363 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100364 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200365}
366
Jens Freimann383d0b02014-07-29 15:11:49 +0200367static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200368{
Jens Freimann6d3da242013-07-03 15:18:35 +0200369 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200370 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200371 struct kvm_s390_mchk_info mchk = {};
Eric Farmanbc17de72014-04-14 16:01:09 -0400372 unsigned long adtl_status_addr;
Jens Freimann6d3da242013-07-03 15:18:35 +0200373 int deliver = 0;
374 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200375
Jens Freimann6d3da242013-07-03 15:18:35 +0200376 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200377 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200378 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
379 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
380 /*
381 * If there was an exigent machine check pending, then any
382 * repressible machine checks that might have been pending
383 * are indicated along with it, so always clear bits for
384 * repressible and exigent interrupts
385 */
386 mchk = li->irq.mchk;
387 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
388 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
389 memset(&li->irq.mchk, 0, sizeof(mchk));
390 deliver = 1;
391 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200392 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200393 * We indicate floating repressible conditions along with
394 * other pending conditions. Channel Report Pending and Channel
395 * Subsystem damage are the only two and and are indicated by
396 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200397 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200398 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
399 mchk.mcic |= fi->mchk.mcic;
400 mchk.cr14 |= fi->mchk.cr14;
401 memset(&fi->mchk, 0, sizeof(mchk));
402 deliver = 1;
403 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200404 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200405 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200406
Jens Freimann6d3da242013-07-03 15:18:35 +0200407 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200408 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200409 mchk.mcic);
410 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
411 KVM_S390_MCHK,
412 mchk.cr14, mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200413
Jens Freimann6d3da242013-07-03 15:18:35 +0200414 rc = kvm_s390_vcpu_store_status(vcpu,
415 KVM_S390_STORE_STATUS_PREFIXED);
416 rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR,
417 &adtl_status_addr,
418 sizeof(unsigned long));
419 rc |= kvm_s390_vcpu_store_adtl_status(vcpu,
420 adtl_status_addr);
421 rc |= put_guest_lc(vcpu, mchk.mcic,
422 (u64 __user *) __LC_MCCK_CODE);
423 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
424 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
425 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
426 &mchk.fixed_logout,
427 sizeof(mchk.fixed_logout));
428 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
429 &vcpu->arch.sie_block->gpsw,
430 sizeof(psw_t));
431 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
432 &vcpu->arch.sie_block->gpsw,
433 sizeof(psw_t));
434 }
Jens Freimann99e20002014-12-01 17:05:39 +0100435 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200436}
437
438static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
439{
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;
442
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200443 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200444 vcpu->stat.deliver_restart_signal++;
445 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
446
447 rc = write_guest_lc(vcpu,
448 offsetof(struct _lowcore, restart_old_psw),
449 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
450 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
451 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200452 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100453 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200454}
455
Jens Freimann383d0b02014-07-29 15:11:49 +0200456static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200457{
Jens Freimann383d0b02014-07-29 15:11:49 +0200458 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
459 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200460
Jens Freimann383d0b02014-07-29 15:11:49 +0200461 spin_lock(&li->lock);
462 prefix = li->irq.prefix;
463 li->irq.prefix.address = 0;
464 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
465 spin_unlock(&li->lock);
466
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200467 vcpu->stat.deliver_prefix_signal++;
468 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
469 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200470 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200471
Jens Freimann383d0b02014-07-29 15:11:49 +0200472 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200473 return 0;
474}
475
Jens Freimann383d0b02014-07-29 15:11:49 +0200476static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200477{
Jens Freimann383d0b02014-07-29 15:11:49 +0200478 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200479 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200480 int cpu_addr;
481
482 spin_lock(&li->lock);
483 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
484 clear_bit(cpu_addr, li->sigp_emerg_pending);
485 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
486 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
487 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200488
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200489 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200490 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200491 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
492 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200493
494 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
495 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200496 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200497 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
498 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
499 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
500 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100501 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200502}
503
Jens Freimann383d0b02014-07-29 15:11:49 +0200504static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200505{
Jens Freimann383d0b02014-07-29 15:11:49 +0200506 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
507 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200508 int rc;
509
Jens Freimann383d0b02014-07-29 15:11:49 +0200510 spin_lock(&li->lock);
511 extcall = li->irq.extcall;
512 li->irq.extcall.code = 0;
513 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
514 spin_unlock(&li->lock);
515
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200516 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200517 vcpu->stat.deliver_external_call++;
518 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
519 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200520 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200521
522 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
523 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200524 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200525 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
526 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
527 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
528 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100529 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200530}
531
Jens Freimann383d0b02014-07-29 15:11:49 +0200532static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200533{
Jens Freimann383d0b02014-07-29 15:11:49 +0200534 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
535 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100536 int rc = 0, nullifying = false;
Jens Freimann8a2ef712014-07-23 16:36:06 +0200537 u16 ilc = get_ilc(vcpu);
David Hildenbrand87128362014-03-03 10:55:13 +0100538
Jens Freimann383d0b02014-07-29 15:11:49 +0200539 spin_lock(&li->lock);
540 pgm_info = li->irq.pgm;
541 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
542 memset(&li->irq.pgm, 0, sizeof(pgm_info));
543 spin_unlock(&li->lock);
544
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200545 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilc:%d",
Jens Freimann383d0b02014-07-29 15:11:49 +0200546 pgm_info.code, ilc);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200547 vcpu->stat.deliver_program_int++;
548 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200549 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200550
Jens Freimann383d0b02014-07-29 15:11:49 +0200551 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100552 case PGM_AFX_TRANSLATION:
553 case PGM_ASX_TRANSLATION:
554 case PGM_EX_TRANSLATION:
555 case PGM_LFX_TRANSLATION:
556 case PGM_LSTE_SEQUENCE:
557 case PGM_LSX_TRANSLATION:
558 case PGM_LX_TRANSLATION:
559 case PGM_PRIMARY_AUTHORITY:
560 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100561 nullifying = true;
562 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100563 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200564 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100565 (u64 *)__LC_TRANS_EXC_CODE);
566 break;
567 case PGM_ALEN_TRANSLATION:
568 case PGM_ALE_SEQUENCE:
569 case PGM_ASTE_INSTANCE:
570 case PGM_ASTE_SEQUENCE:
571 case PGM_ASTE_VALIDITY:
572 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200573 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100574 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100575 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100576 break;
577 case PGM_ASCE_TYPE:
578 case PGM_PAGE_TRANSLATION:
579 case PGM_REGION_FIRST_TRANS:
580 case PGM_REGION_SECOND_TRANS:
581 case PGM_REGION_THIRD_TRANS:
582 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200583 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100584 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200585 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100586 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200587 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100588 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100589 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100590 break;
591 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200592 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200593 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200594 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100595 (u64 *)__LC_MON_CODE);
596 break;
Eric Farman403c8642015-02-02 15:01:06 -0500597 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100598 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200599 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100600 (u32 *)__LC_DATA_EXC_CODE);
601 break;
602 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200603 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100604 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200605 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100606 (u8 *)__LC_EXC_ACCESS_ID);
607 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100608 case PGM_STACK_FULL:
609 case PGM_STACK_EMPTY:
610 case PGM_STACK_SPECIFICATION:
611 case PGM_STACK_TYPE:
612 case PGM_STACK_OPERATION:
613 case PGM_TRACE_TABEL:
614 case PGM_CRYPTO_OPERATION:
615 nullifying = true;
616 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100617 }
618
Jens Freimann383d0b02014-07-29 15:11:49 +0200619 if (pgm_info.code & PGM_PER) {
620 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100621 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200622 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100623 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200624 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100625 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200626 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100627 (u8 *) __LC_PER_ACCESS_ID);
628 }
629
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100630 if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
631 kvm_s390_rewind_psw(vcpu, ilc);
632
Jens Freimann8a2ef712014-07-23 16:36:06 +0200633 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100634 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
635 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200636 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100637 (u16 *)__LC_PGM_INT_CODE);
638 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
639 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
640 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
641 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100642 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200643}
David Hildenbrand87128362014-03-03 10:55:13 +0100644
Jens Freimann6d3da242013-07-03 15:18:35 +0200645static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200646{
Jens Freimann6d3da242013-07-03 15:18:35 +0200647 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
648 struct kvm_s390_ext_info ext;
649 int rc = 0;
650
651 spin_lock(&fi->lock);
652 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
653 spin_unlock(&fi->lock);
654 return 0;
655 }
656 ext = fi->srv_signal;
657 memset(&fi->srv_signal, 0, sizeof(ext));
658 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
659 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200660
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200661 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200662 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200663 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200664 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
665 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200666
667 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100668 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200669 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
670 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
671 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
672 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200673 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200674 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200675
Jens Freimann99e20002014-12-01 17:05:39 +0100676 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200677}
678
Jens Freimann6d3da242013-07-03 15:18:35 +0200679static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200680{
Jens Freimann6d3da242013-07-03 15:18:35 +0200681 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
682 struct kvm_s390_interrupt_info *inti;
683 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200684
Jens Freimann6d3da242013-07-03 15:18:35 +0200685 spin_lock(&fi->lock);
686 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
687 struct kvm_s390_interrupt_info,
688 list);
689 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200690 list_del(&inti->list);
691 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
692 }
693 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
694 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
695 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200696
Jens Freimann6d3da242013-07-03 15:18:35 +0200697 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200698 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
699 KVM_S390_INT_PFAULT_DONE, 0,
700 inti->ext.ext_params2);
701 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
702 inti->ext.ext_params2);
703
Jens Freimann6d3da242013-07-03 15:18:35 +0200704 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
705 (u16 *)__LC_EXT_INT_CODE);
706 rc |= put_guest_lc(vcpu, PFAULT_DONE,
707 (u16 *)__LC_EXT_CPU_ADDR);
708 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
709 &vcpu->arch.sie_block->gpsw,
710 sizeof(psw_t));
711 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
712 &vcpu->arch.sie_block->gpsw,
713 sizeof(psw_t));
714 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
715 (u64 *)__LC_EXT_PARAMS2);
716 kfree(inti);
717 }
Jens Freimann99e20002014-12-01 17:05:39 +0100718 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200719}
720
Jens Freimann6d3da242013-07-03 15:18:35 +0200721static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200722{
Jens Freimann6d3da242013-07-03 15:18:35 +0200723 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
724 struct kvm_s390_interrupt_info *inti;
725 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200726
Jens Freimann6d3da242013-07-03 15:18:35 +0200727 spin_lock(&fi->lock);
728 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
729 struct kvm_s390_interrupt_info,
730 list);
731 if (inti) {
732 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200733 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200734 inti->ext.ext_params, inti->ext.ext_params2);
735 vcpu->stat.deliver_virtio_interrupt++;
736 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
737 inti->type,
738 inti->ext.ext_params,
739 inti->ext.ext_params2);
740 list_del(&inti->list);
741 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
742 }
743 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
744 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
745 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200746
Jens Freimann6d3da242013-07-03 15:18:35 +0200747 if (inti) {
748 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
749 (u16 *)__LC_EXT_INT_CODE);
750 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
751 (u16 *)__LC_EXT_CPU_ADDR);
752 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
753 &vcpu->arch.sie_block->gpsw,
754 sizeof(psw_t));
755 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
756 &vcpu->arch.sie_block->gpsw,
757 sizeof(psw_t));
758 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
759 (u32 *)__LC_EXT_PARAMS);
760 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
761 (u64 *)__LC_EXT_PARAMS2);
762 kfree(inti);
763 }
Jens Freimann99e20002014-12-01 17:05:39 +0100764 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200765}
766
767static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200768 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200769{
Jens Freimann6d3da242013-07-03 15:18:35 +0200770 struct list_head *isc_list;
771 struct kvm_s390_float_interrupt *fi;
772 struct kvm_s390_interrupt_info *inti = NULL;
773 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200774
Jens Freimann6d3da242013-07-03 15:18:35 +0200775 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200776
Jens Freimann6d3da242013-07-03 15:18:35 +0200777 spin_lock(&fi->lock);
778 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
779 inti = list_first_entry_or_null(isc_list,
780 struct kvm_s390_interrupt_info,
781 list);
782 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200783 VCPU_EVENT(vcpu, 4, "deliver: I/O 0x%llx", inti->type);
Jens Freimann6d3da242013-07-03 15:18:35 +0200784 vcpu->stat.deliver_io_int++;
785 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
786 inti->type,
787 ((__u32)inti->io.subchannel_id << 16) |
788 inti->io.subchannel_nr,
789 ((__u64)inti->io.io_int_parm << 32) |
790 inti->io.io_int_word);
791 list_del(&inti->list);
792 fi->counters[FIRQ_CNTR_IO] -= 1;
793 }
794 if (list_empty(isc_list))
795 clear_bit(irq_type, &fi->pending_irqs);
796 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100797
Jens Freimann6d3da242013-07-03 15:18:35 +0200798 if (inti) {
799 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
800 (u16 *)__LC_SUBCHANNEL_ID);
801 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
802 (u16 *)__LC_SUBCHANNEL_NR);
803 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
804 (u32 *)__LC_IO_INT_PARM);
805 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
806 (u32 *)__LC_IO_INT_WORD);
807 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
808 &vcpu->arch.sie_block->gpsw,
809 sizeof(psw_t));
810 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
811 &vcpu->arch.sie_block->gpsw,
812 sizeof(psw_t));
813 kfree(inti);
814 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200815
Jens Freimann99e20002014-12-01 17:05:39 +0100816 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200817}
818
819typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
820
821static const deliver_irq_t deliver_irq_funcs[] = {
822 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200823 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200824 [IRQ_PEND_PROG] = __deliver_prog,
825 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
826 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
827 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
828 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
829 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200830 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
831 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200832 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
833 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
834 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200835};
836
David Hildenbrandea5f4962014-10-14 15:29:30 +0200837/* Check whether an external call is pending (deliverable or not) */
838int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100839{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200840 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand49539192014-02-21 08:59:59 +0100841
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200842 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200843 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100844
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200845 return sca_ext_call_pending(vcpu, NULL);
David Hildenbrand49539192014-02-21 08:59:59 +0100846}
847
David Hildenbrand9a022062014-08-05 17:40:47 +0200848int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100849{
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200850 if (deliverable_irqs(vcpu))
851 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100852
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200853 if (kvm_cpu_has_pending_timer(vcpu))
854 return 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100855
David Hildenbrandea5f4962014-10-14 15:29:30 +0200856 /* external call pending and deliverable */
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200857 if (kvm_s390_ext_call_pending(vcpu) &&
David Hildenbrandea5f4962014-10-14 15:29:30 +0200858 !psw_extint_disabled(vcpu) &&
859 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200860 return 1;
David Hildenbrand49539192014-02-21 08:59:59 +0100861
David Hildenbrand4d32ad62015-05-06 13:51:29 +0200862 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
863 return 1;
864 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100865}
866
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300867int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
868{
David Hildenbrandb4aec922014-12-01 15:55:42 +0100869 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300870}
871
Carsten Otteba5c1e92008-03-25 18:47:26 +0100872int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
873{
874 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100875
876 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100877
David Hildenbrand0759d062014-05-13 16:54:32 +0200878 /* fast path */
David Hildenbrand118b8622015-09-23 12:25:15 +0200879 if (kvm_arch_vcpu_runnable(vcpu))
David Hildenbrand0759d062014-05-13 16:54:32 +0200880 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200881
Carsten Otteba5c1e92008-03-25 18:47:26 +0100882 if (psw_interrupts_disabled(vcpu)) {
883 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100884 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100885 }
886
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100887 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100888 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +0100889 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100890 goto no_timer;
891 }
892
David Hildenbrand60417fc2015-09-29 16:20:36 +0200893 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
Heiko Carstensed4f2092013-01-14 16:55:55 +0100894 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
David Hildenbrandbda343e2014-12-12 12:26:40 +0100895
896 /* underflow */
897 if (vcpu->arch.sie_block->ckc < now)
898 return 0;
899
900 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200901 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200902 VCPU_EVENT(vcpu, 4, "enabled wait via clock comparator: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100903no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200904 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200905 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100906 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200907 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
908
David Hildenbrand2d00f752014-12-11 10:18:01 +0100909 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100910 return 0;
911}
912
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200913void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
914{
915 if (waitqueue_active(&vcpu->wq)) {
916 /*
917 * The vcpu gave up the cpu voluntarily, mark it as a good
918 * yield-candidate.
919 */
920 vcpu->preempted = true;
921 wake_up_interruptible(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +0200922 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200923 }
924}
925
Christian Borntraegerca872302009-05-12 17:21:49 +0200926enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
927{
928 struct kvm_vcpu *vcpu;
David Hildenbrand2d00f752014-12-11 10:18:01 +0100929 u64 now, sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +0200930
931 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrand60417fc2015-09-29 16:20:36 +0200932 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
David Hildenbrand2d00f752014-12-11 10:18:01 +0100933 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200934
David Hildenbrand2d00f752014-12-11 10:18:01 +0100935 /*
936 * If the monotonic clock runs faster than the tod clock we might be
937 * woken up too early and have to go back to sleep to avoid deadlocks.
938 */
939 if (vcpu->arch.sie_block->ckc > now &&
940 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
941 return HRTIMER_RESTART;
942 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200943 return HRTIMER_NORESTART;
944}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100945
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100946void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
947{
948 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100949
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200950 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200951 li->pending_irqs = 0;
952 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
953 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200954 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100955
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +0200956 sca_clear_ext_call(vcpu);
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100957}
958
Christian Borntraeger614aeab2014-08-25 12:27:29 +0200959int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100960{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200961 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200962 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +0200963 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200964 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +0200965 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100966
967 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100968
Jens Freimann383d0b02014-07-29 15:11:49 +0200969 /* pending ckc conditions might have been invalidated */
970 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +0100971 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +0200972 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
973
David Hildenbrandb4aec922014-12-01 15:55:42 +0100974 /* pending cpu timer conditions might have been invalidated */
975 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
976 if (cpu_timer_irq_pending(vcpu))
977 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
978
Jens Freimannffeca0a2015-04-17 10:21:04 +0200979 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +0200980 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +0200981 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +0200982 if (is_ioirq(irq_type)) {
983 rc = __deliver_io(vcpu, irq_type);
984 } else {
985 func = deliver_irq_funcs[irq_type];
986 if (!func) {
987 WARN_ON_ONCE(func == NULL);
988 clear_bit(irq_type, &li->pending_irqs);
989 continue;
990 }
991 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200992 }
Jens Freimannffeca0a2015-04-17 10:21:04 +0200993 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200994
Jens Freimann6d3da242013-07-03 15:18:35 +0200995 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +0200996
997 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100998}
999
Jens Freimann383d0b02014-07-29 15:11:49 +02001000static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001001{
1002 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1003
David Hildenbranded2afcf2015-07-20 10:33:03 +02001004 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1005 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1006 irq->u.pgm.code, 0);
1007
David Hildenbrand238293b2015-05-04 12:38:48 +02001008 if (irq->u.pgm.code == PGM_PER) {
1009 li->irq.pgm.code |= PGM_PER;
1010 /* only modify PER related information */
1011 li->irq.pgm.per_address = irq->u.pgm.per_address;
1012 li->irq.pgm.per_code = irq->u.pgm.per_code;
1013 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1014 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1015 } else if (!(irq->u.pgm.code & PGM_PER)) {
1016 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1017 irq->u.pgm.code;
1018 /* only modify non-PER information */
1019 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1020 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1021 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1022 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1023 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1024 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1025 } else {
1026 li->irq.pgm = irq->u.pgm;
1027 }
Jens Freimann91851242014-12-01 16:43:40 +01001028 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001029 return 0;
1030}
1031
Jens Freimann383d0b02014-07-29 15:11:49 +02001032static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001033{
1034 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1035
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001036 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1037 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001038 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1039 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001040 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001041
1042 li->irq.ext = irq->u.ext;
1043 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001044 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001045 return 0;
1046}
1047
Christian Borntraeger0675d922015-01-15 12:40:42 +01001048static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001049{
1050 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001051 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001052 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001053
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001054 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001055 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001056 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001057 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001058
David Hildenbrandea5f4962014-10-14 15:29:30 +02001059 /* sending vcpu invalid */
David Hildenbrand152e9f62015-11-05 09:06:06 +01001060 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001061 return -EINVAL;
1062
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001063 if (sclp.has_sigpif)
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02001064 return sca_inject_ext_call(vcpu, src_id);
David Hildenbrandea5f4962014-10-14 15:29:30 +02001065
David Hildenbrandb938eace2015-04-30 13:33:59 +02001066 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001067 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001068 *extcall = irq->u.extcall;
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001069 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001070 return 0;
1071}
1072
Jens Freimann383d0b02014-07-29 15:11:49 +02001073static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001074{
1075 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001076 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001077
David Hildenbranded2afcf2015-07-20 10:33:03 +02001078 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001079 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001080 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001081 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001082
David Hildenbranda3a9c592014-10-14 09:44:55 +02001083 if (!is_vcpu_stopped(vcpu))
1084 return -EBUSY;
1085
Jens Freimann383d0b02014-07-29 15:11:49 +02001086 *prefix = irq->u.prefix;
1087 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001088 return 0;
1089}
1090
David Hildenbrand6cddd432014-10-15 16:48:53 +02001091#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001092static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001093{
1094 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001095 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001096 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001097
David Hildenbranded2afcf2015-07-20 10:33:03 +02001098 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001099
David Hildenbrand28225452014-10-15 16:48:16 +02001100 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1101 return -EINVAL;
1102
David Hildenbrand6cddd432014-10-15 16:48:53 +02001103 if (is_vcpu_stopped(vcpu)) {
1104 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1105 rc = kvm_s390_store_status_unloaded(vcpu,
1106 KVM_S390_STORE_STATUS_NOADDR);
1107 return rc;
1108 }
1109
1110 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1111 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001112 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001113 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001114 return 0;
1115}
1116
1117static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001118 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001119{
1120 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1121
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001122 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001123 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001124
1125 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001126 return 0;
1127}
1128
1129static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001130 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001131{
1132 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1133
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001134 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001135 irq->u.emerg.code);
1136 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001137 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001138
David Hildenbrandb85de332015-11-05 09:38:15 +01001139 /* sending vcpu invalid */
1140 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1141 return -EINVAL;
1142
Jens Freimann49538d12014-12-18 15:48:14 +01001143 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001144 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001145 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001146 return 0;
1147}
1148
Jens Freimann383d0b02014-07-29 15:11:49 +02001149static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001150{
1151 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001152 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001153
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001154 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001155 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001156 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001157 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001158
1159 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001160 * Because repressible machine checks can be indicated along with
1161 * exigent machine checks (PoP, Chapter 11, Interruption action)
1162 * we need to combine cr14, mcic and external damage code.
1163 * Failing storage address and the logout area should not be or'ed
1164 * together, we just indicate the last occurrence of the corresponding
1165 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001166 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001167 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001168 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001169 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1170 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1171 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1172 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001173 if (mchk->mcic & MCHK_EX_MASK)
1174 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1175 else if (mchk->mcic & MCHK_REP_MASK)
1176 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001177 return 0;
1178}
1179
Jens Freimann383d0b02014-07-29 15:11:49 +02001180static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001181{
1182 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1183
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001184 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001185 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001186 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001187
1188 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001189 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001190 return 0;
1191}
1192
Jens Freimann383d0b02014-07-29 15:11:49 +02001193static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001194{
1195 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1196
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001197 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001198 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001199 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001200
1201 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001202 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001203 return 0;
1204}
1205
Jens Freimann6d3da242013-07-03 15:18:35 +02001206static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1207 int isc, u32 schid)
1208{
1209 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1210 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1211 struct kvm_s390_interrupt_info *iter;
1212 u16 id = (schid & 0xffff0000U) >> 16;
1213 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001214
Jens Freimann6d3da242013-07-03 15:18:35 +02001215 spin_lock(&fi->lock);
1216 list_for_each_entry(iter, isc_list, list) {
1217 if (schid && (id != iter->io.subchannel_id ||
1218 nr != iter->io.subchannel_nr))
1219 continue;
1220 /* found an appropriate entry */
1221 list_del_init(&iter->list);
1222 fi->counters[FIRQ_CNTR_IO] -= 1;
1223 if (list_empty(isc_list))
1224 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1225 spin_unlock(&fi->lock);
1226 return iter;
1227 }
1228 spin_unlock(&fi->lock);
1229 return NULL;
1230}
1231
1232/*
1233 * Dequeue and return an I/O interrupt matching any of the interruption
1234 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1235 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001236struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001237 u64 isc_mask, u32 schid)
1238{
1239 struct kvm_s390_interrupt_info *inti = NULL;
1240 int isc;
1241
1242 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1243 if (isc_mask & isc_to_isc_bits(isc))
1244 inti = get_io_int(kvm, isc, schid);
1245 }
1246 return inti;
1247}
1248
1249#define SCCB_MASK 0xFFFFFFF8
1250#define SCCB_EVENT_PENDING 0x3
1251
1252static int __inject_service(struct kvm *kvm,
1253 struct kvm_s390_interrupt_info *inti)
1254{
1255 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1256
1257 spin_lock(&fi->lock);
1258 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1259 /*
1260 * Early versions of the QEMU s390 bios will inject several
1261 * service interrupts after another without handling a
1262 * condition code indicating busy.
1263 * We will silently ignore those superfluous sccb values.
1264 * A future version of QEMU will take care of serialization
1265 * of servc requests
1266 */
1267 if (fi->srv_signal.ext_params & SCCB_MASK)
1268 goto out;
1269 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1270 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1271out:
1272 spin_unlock(&fi->lock);
1273 kfree(inti);
1274 return 0;
1275}
1276
1277static int __inject_virtio(struct kvm *kvm,
1278 struct kvm_s390_interrupt_info *inti)
1279{
1280 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1281
1282 spin_lock(&fi->lock);
1283 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1284 spin_unlock(&fi->lock);
1285 return -EBUSY;
1286 }
1287 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1288 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1289 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1290 spin_unlock(&fi->lock);
1291 return 0;
1292}
1293
1294static int __inject_pfault_done(struct kvm *kvm,
1295 struct kvm_s390_interrupt_info *inti)
1296{
1297 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1298
1299 spin_lock(&fi->lock);
1300 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1301 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1302 spin_unlock(&fi->lock);
1303 return -EBUSY;
1304 }
1305 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1306 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1307 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1308 spin_unlock(&fi->lock);
1309 return 0;
1310}
1311
1312#define CR_PENDING_SUBCLASS 28
1313static int __inject_float_mchk(struct kvm *kvm,
1314 struct kvm_s390_interrupt_info *inti)
1315{
1316 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1317
1318 spin_lock(&fi->lock);
1319 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1320 fi->mchk.mcic |= inti->mchk.mcic;
1321 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1322 spin_unlock(&fi->lock);
1323 kfree(inti);
1324 return 0;
1325}
1326
1327static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001328{
1329 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001330 struct list_head *list;
1331 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001332
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001333 fi = &kvm->arch.float_int;
1334 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001335 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1336 spin_unlock(&fi->lock);
1337 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001338 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001339 fi->counters[FIRQ_CNTR_IO] += 1;
1340
1341 isc = int_word_to_isc(inti->io.io_int_word);
1342 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1343 list_add_tail(&inti->list, list);
1344 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001345 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001346 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001347}
1348
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001349/*
1350 * Find a destination VCPU for a floating irq and kick it.
1351 */
1352static void __floating_irq_kick(struct kvm *kvm, u64 type)
1353{
1354 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1355 struct kvm_s390_local_interrupt *li;
1356 struct kvm_vcpu *dst_vcpu;
1357 int sigcpu, online_vcpus, nr_tries = 0;
1358
1359 online_vcpus = atomic_read(&kvm->online_vcpus);
1360 if (!online_vcpus)
1361 return;
1362
1363 /* find idle VCPUs first, then round robin */
1364 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1365 if (sigcpu == online_vcpus) {
1366 do {
1367 sigcpu = fi->next_rr_cpu;
1368 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1369 /* avoid endless loops if all vcpus are stopped */
1370 if (nr_tries++ >= online_vcpus)
1371 return;
1372 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1373 }
1374 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1375
1376 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1377 li = &dst_vcpu->arch.local_int;
1378 spin_lock(&li->lock);
1379 switch (type) {
1380 case KVM_S390_MCHK:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001381 atomic_or(CPUSTAT_STOP_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001382 break;
1383 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001384 atomic_or(CPUSTAT_IO_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001385 break;
1386 default:
Peter Zijlstra805de8f42015-04-24 01:12:32 +02001387 atomic_or(CPUSTAT_EXT_INT, li->cpuflags);
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001388 break;
1389 }
1390 spin_unlock(&li->lock);
1391 kvm_s390_vcpu_wakeup(dst_vcpu);
1392}
1393
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001394static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001395{
Jens Freimann6d3da242013-07-03 15:18:35 +02001396 u64 type = READ_ONCE(inti->type);
1397 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001398
Jens Freimann6d3da242013-07-03 15:18:35 +02001399 switch (type) {
1400 case KVM_S390_MCHK:
1401 rc = __inject_float_mchk(kvm, inti);
1402 break;
1403 case KVM_S390_INT_VIRTIO:
1404 rc = __inject_virtio(kvm, inti);
1405 break;
1406 case KVM_S390_INT_SERVICE:
1407 rc = __inject_service(kvm, inti);
1408 break;
1409 case KVM_S390_INT_PFAULT_DONE:
1410 rc = __inject_pfault_done(kvm, inti);
1411 break;
1412 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1413 rc = __inject_io(kvm, inti);
1414 break;
1415 default:
1416 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001417 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001418 if (rc)
1419 return rc;
1420
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001421 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001422 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001423}
1424
1425int kvm_s390_inject_vm(struct kvm *kvm,
1426 struct kvm_s390_interrupt *s390int)
1427{
1428 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001429 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001430
1431 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1432 if (!inti)
1433 return -ENOMEM;
1434
1435 inti->type = s390int->type;
1436 switch (inti->type) {
1437 case KVM_S390_INT_VIRTIO:
1438 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1439 s390int->parm, s390int->parm64);
1440 inti->ext.ext_params = s390int->parm;
1441 inti->ext.ext_params2 = s390int->parm64;
1442 break;
1443 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001444 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001445 inti->ext.ext_params = s390int->parm;
1446 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001447 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001448 inti->ext.ext_params2 = s390int->parm64;
1449 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001450 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001451 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001452 s390int->parm64);
1453 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1454 inti->mchk.mcic = s390int->parm64;
1455 break;
1456 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
David Hildenbranda37281b2014-11-21 13:45:08 +01001457 if (inti->type & KVM_S390_INT_IO_AI_MASK)
Jens Freimannc05c4182013-10-07 16:13:45 +02001458 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1459 else
1460 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1461 s390int->type & IOINT_CSSID_MASK,
1462 s390int->type & IOINT_SSID_MASK,
1463 s390int->type & IOINT_SCHID_MASK);
1464 inti->io.subchannel_id = s390int->parm >> 16;
1465 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1466 inti->io.io_int_parm = s390int->parm64 >> 32;
1467 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1468 break;
1469 default:
1470 kfree(inti);
1471 return -EINVAL;
1472 }
1473 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1474 2);
1475
David Hildenbrand428d53b2015-01-16 12:58:09 +01001476 rc = __inject_vm(kvm, inti);
1477 if (rc)
1478 kfree(inti);
1479 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001480}
1481
David Hildenbrand15462e32015-02-04 15:59:11 +01001482int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001483 struct kvm_s390_interrupt_info *inti)
1484{
David Hildenbrand15462e32015-02-04 15:59:11 +01001485 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001486}
1487
Jens Freimann383d0b02014-07-29 15:11:49 +02001488int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1489 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001490{
Jens Freimann383d0b02014-07-29 15:11:49 +02001491 irq->type = s390int->type;
1492 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001493 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001494 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001495 return -EINVAL;
1496 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001497 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001498 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001499 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001500 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001501 case KVM_S390_SIGP_STOP:
1502 irq->u.stop.flags = s390int->parm;
1503 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001504 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001505 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001506 return -EINVAL;
1507 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001508 break;
1509 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001510 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001511 return -EINVAL;
1512 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001513 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001514 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001515 irq->u.mchk.mcic = s390int->parm64;
1516 break;
1517 }
1518 return 0;
1519}
1520
David Hildenbrand6cddd432014-10-15 16:48:53 +02001521int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1522{
1523 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1524
1525 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1526}
1527
1528void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1529{
1530 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1531
1532 spin_lock(&li->lock);
1533 li->irq.stop.flags = 0;
1534 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1535 spin_unlock(&li->lock);
1536}
1537
Jens Freimann79e87a12015-03-19 15:12:12 +01001538static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001539{
Jens Freimann383d0b02014-07-29 15:11:49 +02001540 int rc;
1541
Jens Freimann383d0b02014-07-29 15:11:49 +02001542 switch (irq->type) {
1543 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001544 rc = __inject_prog(vcpu, irq);
1545 break;
1546 case KVM_S390_SIGP_SET_PREFIX:
1547 rc = __inject_set_prefix(vcpu, irq);
1548 break;
1549 case KVM_S390_SIGP_STOP:
1550 rc = __inject_sigp_stop(vcpu, irq);
1551 break;
1552 case KVM_S390_RESTART:
1553 rc = __inject_sigp_restart(vcpu, irq);
1554 break;
1555 case KVM_S390_INT_CLOCK_COMP:
1556 rc = __inject_ckc(vcpu);
1557 break;
1558 case KVM_S390_INT_CPU_TIMER:
1559 rc = __inject_cpu_timer(vcpu);
1560 break;
1561 case KVM_S390_INT_EXTERNAL_CALL:
1562 rc = __inject_extcall(vcpu, irq);
1563 break;
1564 case KVM_S390_INT_EMERGENCY:
1565 rc = __inject_sigp_emergency(vcpu, irq);
1566 break;
1567 case KVM_S390_MCHK:
1568 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001569 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001570 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001571 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001572 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001573 case KVM_S390_INT_VIRTIO:
1574 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001575 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001576 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001577 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001578 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001579
1580 return rc;
1581}
1582
1583int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1584{
1585 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1586 int rc;
1587
1588 spin_lock(&li->lock);
1589 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001590 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001591 if (!rc)
1592 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001593 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001594}
Jens Freimannc05c4182013-10-07 16:13:45 +02001595
Jens Freimann6d3da242013-07-03 15:18:35 +02001596static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001597{
Jens Freimann6d3da242013-07-03 15:18:35 +02001598 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001599
Jens Freimann6d3da242013-07-03 15:18:35 +02001600 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001601 list_del(&inti->list);
1602 kfree(inti);
1603 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001604}
1605
Jens Freimann94aa0332015-03-16 12:17:13 +01001606static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1607 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001608{
Jens Freimann94aa0332015-03-16 12:17:13 +01001609 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001610 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001611 case KVM_S390_INT_PFAULT_INIT:
1612 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001613 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001614 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001615 break;
1616 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001617 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001618 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001619 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001620}
1621
Jens Freimann6d3da242013-07-03 15:18:35 +02001622void kvm_s390_clear_float_irqs(struct kvm *kvm)
1623{
1624 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1625 int i;
1626
1627 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001628 fi->pending_irqs = 0;
1629 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1630 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001631 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1632 clear_irq_list(&fi->lists[i]);
1633 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1634 fi->counters[i] = 0;
1635 spin_unlock(&fi->lock);
1636};
1637
Jens Freimann94aa0332015-03-16 12:17:13 +01001638static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001639{
1640 struct kvm_s390_interrupt_info *inti;
1641 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001642 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001643 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001644 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001645 int ret = 0;
1646 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001647 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001648
Jens Freimann94aa0332015-03-16 12:17:13 +01001649 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1650 return -EINVAL;
1651
1652 /*
1653 * We are already using -ENOMEM to signal
1654 * userspace it may retry with a bigger buffer,
1655 * so we need to use something else for this case
1656 */
1657 buf = vzalloc(len);
1658 if (!buf)
1659 return -ENOBUFS;
1660
1661 max_irqs = len / sizeof(struct kvm_s390_irq);
1662
Jens Freimannc05c4182013-10-07 16:13:45 +02001663 fi = &kvm->arch.float_int;
1664 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001665 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1666 list_for_each_entry(inti, &fi->lists[i], list) {
1667 if (n == max_irqs) {
1668 /* signal userspace to try again */
1669 ret = -ENOMEM;
1670 goto out;
1671 }
1672 inti_to_irq(inti, &buf[n]);
1673 n++;
1674 }
1675 }
1676 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001677 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001678 /* signal userspace to try again */
1679 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001680 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001681 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001682 irq = (struct kvm_s390_irq *) &buf[n];
1683 irq->type = KVM_S390_INT_SERVICE;
1684 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001685 n++;
1686 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001687 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1688 if (n == max_irqs) {
1689 /* signal userspace to try again */
1690 ret = -ENOMEM;
1691 goto out;
1692 }
1693 irq = (struct kvm_s390_irq *) &buf[n];
1694 irq->type = KVM_S390_MCHK;
1695 irq->u.mchk = fi->mchk;
1696 n++;
1697}
1698
1699out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001700 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001701 if (!ret && n > 0) {
1702 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1703 ret = -EFAULT;
1704 }
1705 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001706
1707 return ret < 0 ? ret : n;
1708}
1709
1710static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1711{
1712 int r;
1713
1714 switch (attr->group) {
1715 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001716 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001717 attr->attr);
1718 break;
1719 default:
1720 r = -EINVAL;
1721 }
1722
1723 return r;
1724}
1725
1726static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1727 u64 addr)
1728{
1729 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1730 void *target = NULL;
1731 void __user *source;
1732 u64 size;
1733
1734 if (get_user(inti->type, (u64 __user *)addr))
1735 return -EFAULT;
1736
1737 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001738 case KVM_S390_INT_PFAULT_INIT:
1739 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001740 case KVM_S390_INT_VIRTIO:
1741 case KVM_S390_INT_SERVICE:
1742 target = (void *) &inti->ext;
1743 source = &uptr->u.ext;
1744 size = sizeof(inti->ext);
1745 break;
1746 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1747 target = (void *) &inti->io;
1748 source = &uptr->u.io;
1749 size = sizeof(inti->io);
1750 break;
1751 case KVM_S390_MCHK:
1752 target = (void *) &inti->mchk;
1753 source = &uptr->u.mchk;
1754 size = sizeof(inti->mchk);
1755 break;
1756 default:
1757 return -EINVAL;
1758 }
1759
1760 if (copy_from_user(target, source, size))
1761 return -EFAULT;
1762
1763 return 0;
1764}
1765
1766static int enqueue_floating_irq(struct kvm_device *dev,
1767 struct kvm_device_attr *attr)
1768{
1769 struct kvm_s390_interrupt_info *inti = NULL;
1770 int r = 0;
1771 int len = attr->attr;
1772
1773 if (len % sizeof(struct kvm_s390_irq) != 0)
1774 return -EINVAL;
1775 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1776 return -EINVAL;
1777
1778 while (len >= sizeof(struct kvm_s390_irq)) {
1779 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1780 if (!inti)
1781 return -ENOMEM;
1782
1783 r = copy_irq_from_user(inti, attr->addr);
1784 if (r) {
1785 kfree(inti);
1786 return r;
1787 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001788 r = __inject_vm(dev->kvm, inti);
1789 if (r) {
1790 kfree(inti);
1791 return r;
1792 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001793 len -= sizeof(struct kvm_s390_irq);
1794 attr->addr += sizeof(struct kvm_s390_irq);
1795 }
1796
1797 return r;
1798}
1799
Cornelia Huck841b91c2013-07-15 13:36:01 +02001800static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1801{
1802 if (id >= MAX_S390_IO_ADAPTERS)
1803 return NULL;
1804 return kvm->arch.adapters[id];
1805}
1806
1807static int register_io_adapter(struct kvm_device *dev,
1808 struct kvm_device_attr *attr)
1809{
1810 struct s390_io_adapter *adapter;
1811 struct kvm_s390_io_adapter adapter_info;
1812
1813 if (copy_from_user(&adapter_info,
1814 (void __user *)attr->addr, sizeof(adapter_info)))
1815 return -EFAULT;
1816
1817 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1818 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1819 return -EINVAL;
1820
1821 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1822 if (!adapter)
1823 return -ENOMEM;
1824
1825 INIT_LIST_HEAD(&adapter->maps);
1826 init_rwsem(&adapter->maps_lock);
1827 atomic_set(&adapter->nr_maps, 0);
1828 adapter->id = adapter_info.id;
1829 adapter->isc = adapter_info.isc;
1830 adapter->maskable = adapter_info.maskable;
1831 adapter->masked = false;
1832 adapter->swap = adapter_info.swap;
1833 dev->kvm->arch.adapters[adapter->id] = adapter;
1834
1835 return 0;
1836}
1837
1838int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1839{
1840 int ret;
1841 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1842
1843 if (!adapter || !adapter->maskable)
1844 return -EINVAL;
1845 ret = adapter->masked;
1846 adapter->masked = masked;
1847 return ret;
1848}
1849
1850static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1851{
1852 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1853 struct s390_map_info *map;
1854 int ret;
1855
1856 if (!adapter || !addr)
1857 return -EINVAL;
1858
1859 map = kzalloc(sizeof(*map), GFP_KERNEL);
1860 if (!map) {
1861 ret = -ENOMEM;
1862 goto out;
1863 }
1864 INIT_LIST_HEAD(&map->list);
1865 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02001866 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001867 if (map->addr == -EFAULT) {
1868 ret = -EFAULT;
1869 goto out;
1870 }
1871 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1872 if (ret < 0)
1873 goto out;
1874 BUG_ON(ret != 1);
1875 down_write(&adapter->maps_lock);
1876 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1877 list_add_tail(&map->list, &adapter->maps);
1878 ret = 0;
1879 } else {
1880 put_page(map->page);
1881 ret = -EINVAL;
1882 }
1883 up_write(&adapter->maps_lock);
1884out:
1885 if (ret)
1886 kfree(map);
1887 return ret;
1888}
1889
1890static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1891{
1892 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1893 struct s390_map_info *map, *tmp;
1894 int found = 0;
1895
1896 if (!adapter || !addr)
1897 return -EINVAL;
1898
1899 down_write(&adapter->maps_lock);
1900 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1901 if (map->guest_addr == addr) {
1902 found = 1;
1903 atomic_dec(&adapter->nr_maps);
1904 list_del(&map->list);
1905 put_page(map->page);
1906 kfree(map);
1907 break;
1908 }
1909 }
1910 up_write(&adapter->maps_lock);
1911
1912 return found ? 0 : -EINVAL;
1913}
1914
1915void kvm_s390_destroy_adapters(struct kvm *kvm)
1916{
1917 int i;
1918 struct s390_map_info *map, *tmp;
1919
1920 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1921 if (!kvm->arch.adapters[i])
1922 continue;
1923 list_for_each_entry_safe(map, tmp,
1924 &kvm->arch.adapters[i]->maps, list) {
1925 list_del(&map->list);
1926 put_page(map->page);
1927 kfree(map);
1928 }
1929 kfree(kvm->arch.adapters[i]);
1930 }
1931}
1932
1933static int modify_io_adapter(struct kvm_device *dev,
1934 struct kvm_device_attr *attr)
1935{
1936 struct kvm_s390_io_adapter_req req;
1937 struct s390_io_adapter *adapter;
1938 int ret;
1939
1940 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1941 return -EFAULT;
1942
1943 adapter = get_io_adapter(dev->kvm, req.id);
1944 if (!adapter)
1945 return -EINVAL;
1946 switch (req.type) {
1947 case KVM_S390_IO_ADAPTER_MASK:
1948 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1949 if (ret > 0)
1950 ret = 0;
1951 break;
1952 case KVM_S390_IO_ADAPTER_MAP:
1953 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1954 break;
1955 case KVM_S390_IO_ADAPTER_UNMAP:
1956 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1957 break;
1958 default:
1959 ret = -EINVAL;
1960 }
1961
1962 return ret;
1963}
1964
Jens Freimannc05c4182013-10-07 16:13:45 +02001965static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1966{
1967 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001968 unsigned int i;
1969 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001970
1971 switch (attr->group) {
1972 case KVM_DEV_FLIC_ENQUEUE:
1973 r = enqueue_floating_irq(dev, attr);
1974 break;
1975 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01001976 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001977 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001978 case KVM_DEV_FLIC_APF_ENABLE:
1979 dev->kvm->arch.gmap->pfault_enabled = 1;
1980 break;
1981 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1982 dev->kvm->arch.gmap->pfault_enabled = 0;
1983 /*
1984 * Make sure no async faults are in transition when
1985 * clearing the queues. So we don't need to worry
1986 * about late coming workers.
1987 */
1988 synchronize_srcu(&dev->kvm->srcu);
1989 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1990 kvm_clear_async_pf_completion_queue(vcpu);
1991 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001992 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1993 r = register_io_adapter(dev, attr);
1994 break;
1995 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1996 r = modify_io_adapter(dev, attr);
1997 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001998 default:
1999 r = -EINVAL;
2000 }
2001
2002 return r;
2003}
2004
2005static int flic_create(struct kvm_device *dev, u32 type)
2006{
2007 if (!dev)
2008 return -EINVAL;
2009 if (dev->kvm->arch.flic)
2010 return -EINVAL;
2011 dev->kvm->arch.flic = dev;
2012 return 0;
2013}
2014
2015static void flic_destroy(struct kvm_device *dev)
2016{
2017 dev->kvm->arch.flic = NULL;
2018 kfree(dev);
2019}
2020
2021/* s390 floating irq controller (flic) */
2022struct kvm_device_ops kvm_flic_ops = {
2023 .name = "kvm-flic",
2024 .get_attr = flic_get_attr,
2025 .set_attr = flic_set_attr,
2026 .create = flic_create,
2027 .destroy = flic_destroy,
2028};
Cornelia Huck84223592013-07-15 13:36:01 +02002029
2030static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2031{
2032 unsigned long bit;
2033
2034 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2035
2036 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2037}
2038
2039static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2040 u64 addr)
2041{
2042 struct s390_map_info *map;
2043
2044 if (!adapter)
2045 return NULL;
2046
2047 list_for_each_entry(map, &adapter->maps, list) {
2048 if (map->guest_addr == addr)
2049 return map;
2050 }
2051 return NULL;
2052}
2053
2054static int adapter_indicators_set(struct kvm *kvm,
2055 struct s390_io_adapter *adapter,
2056 struct kvm_s390_adapter_int *adapter_int)
2057{
2058 unsigned long bit;
2059 int summary_set, idx;
2060 struct s390_map_info *info;
2061 void *map;
2062
2063 info = get_map_info(adapter, adapter_int->ind_addr);
2064 if (!info)
2065 return -1;
2066 map = page_address(info->page);
2067 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2068 set_bit(bit, map);
2069 idx = srcu_read_lock(&kvm->srcu);
2070 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2071 set_page_dirty_lock(info->page);
2072 info = get_map_info(adapter, adapter_int->summary_addr);
2073 if (!info) {
2074 srcu_read_unlock(&kvm->srcu, idx);
2075 return -1;
2076 }
2077 map = page_address(info->page);
2078 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2079 adapter->swap);
2080 summary_set = test_and_set_bit(bit, map);
2081 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2082 set_page_dirty_lock(info->page);
2083 srcu_read_unlock(&kvm->srcu, idx);
2084 return summary_set ? 0 : 1;
2085}
2086
2087/*
2088 * < 0 - not injected due to error
2089 * = 0 - coalesced, summary indicator already active
2090 * > 0 - injected interrupt
2091 */
2092static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2093 struct kvm *kvm, int irq_source_id, int level,
2094 bool line_status)
2095{
2096 int ret;
2097 struct s390_io_adapter *adapter;
2098
2099 /* We're only interested in the 0->1 transition. */
2100 if (!level)
2101 return 0;
2102 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2103 if (!adapter)
2104 return -1;
2105 down_read(&adapter->maps_lock);
2106 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2107 up_read(&adapter->maps_lock);
2108 if ((ret > 0) && !adapter->masked) {
2109 struct kvm_s390_interrupt s390int = {
2110 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2111 .parm = 0,
2112 .parm64 = (adapter->isc << 27) | 0x80000000,
2113 };
2114 ret = kvm_s390_inject_vm(kvm, &s390int);
2115 if (ret == 0)
2116 ret = 1;
2117 }
2118 return ret;
2119}
2120
Paul Mackerras8ba918d2014-06-30 20:51:10 +10002121int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002122 const struct kvm_irq_routing_entry *ue)
2123{
2124 int ret;
2125
2126 switch (ue->type) {
2127 case KVM_IRQ_ROUTING_S390_ADAPTER:
2128 e->set = set_adapter_int;
2129 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2130 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2131 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2132 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2133 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2134 ret = 0;
2135 break;
2136 default:
2137 ret = -EINVAL;
2138 }
2139
2140 return ret;
2141}
2142
2143int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2144 int irq_source_id, int level, bool line_status)
2145{
2146 return -EINVAL;
2147}
Jens Freimann816c7662014-11-24 17:13:46 +01002148
2149int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2150{
2151 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2152 struct kvm_s390_irq *buf;
2153 int r = 0;
2154 int n;
2155
2156 buf = vmalloc(len);
2157 if (!buf)
2158 return -ENOMEM;
2159
2160 if (copy_from_user((void *) buf, irqstate, len)) {
2161 r = -EFAULT;
2162 goto out_free;
2163 }
2164
2165 /*
2166 * Don't allow setting the interrupt state
2167 * when there are already interrupts pending
2168 */
2169 spin_lock(&li->lock);
2170 if (li->pending_irqs) {
2171 r = -EBUSY;
2172 goto out_unlock;
2173 }
2174
2175 for (n = 0; n < len / sizeof(*buf); n++) {
2176 r = do_inject_vcpu(vcpu, &buf[n]);
2177 if (r)
2178 break;
2179 }
2180
2181out_unlock:
2182 spin_unlock(&li->lock);
2183out_free:
2184 vfree(buf);
2185
2186 return r;
2187}
2188
2189static void store_local_irq(struct kvm_s390_local_interrupt *li,
2190 struct kvm_s390_irq *irq,
2191 unsigned long irq_type)
2192{
2193 switch (irq_type) {
2194 case IRQ_PEND_MCHK_EX:
2195 case IRQ_PEND_MCHK_REP:
2196 irq->type = KVM_S390_MCHK;
2197 irq->u.mchk = li->irq.mchk;
2198 break;
2199 case IRQ_PEND_PROG:
2200 irq->type = KVM_S390_PROGRAM_INT;
2201 irq->u.pgm = li->irq.pgm;
2202 break;
2203 case IRQ_PEND_PFAULT_INIT:
2204 irq->type = KVM_S390_INT_PFAULT_INIT;
2205 irq->u.ext = li->irq.ext;
2206 break;
2207 case IRQ_PEND_EXT_EXTERNAL:
2208 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2209 irq->u.extcall = li->irq.extcall;
2210 break;
2211 case IRQ_PEND_EXT_CLOCK_COMP:
2212 irq->type = KVM_S390_INT_CLOCK_COMP;
2213 break;
2214 case IRQ_PEND_EXT_CPU_TIMER:
2215 irq->type = KVM_S390_INT_CPU_TIMER;
2216 break;
2217 case IRQ_PEND_SIGP_STOP:
2218 irq->type = KVM_S390_SIGP_STOP;
2219 irq->u.stop = li->irq.stop;
2220 break;
2221 case IRQ_PEND_RESTART:
2222 irq->type = KVM_S390_RESTART;
2223 break;
2224 case IRQ_PEND_SET_PREFIX:
2225 irq->type = KVM_S390_SIGP_SET_PREFIX;
2226 irq->u.prefix = li->irq.prefix;
2227 break;
2228 }
2229}
2230
2231int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2232{
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002233 int scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002234 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2235 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2236 unsigned long pending_irqs;
2237 struct kvm_s390_irq irq;
2238 unsigned long irq_type;
2239 int cpuaddr;
2240 int n = 0;
2241
2242 spin_lock(&li->lock);
2243 pending_irqs = li->pending_irqs;
2244 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2245 sizeof(sigp_emerg_pending));
2246 spin_unlock(&li->lock);
2247
2248 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2249 memset(&irq, 0, sizeof(irq));
2250 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2251 continue;
2252 if (n + sizeof(irq) > len)
2253 return -ENOBUFS;
2254 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2255 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2256 return -EFAULT;
2257 n += sizeof(irq);
2258 }
2259
2260 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2261 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2262 memset(&irq, 0, sizeof(irq));
2263 if (n + sizeof(irq) > len)
2264 return -ENOBUFS;
2265 irq.type = KVM_S390_INT_EMERGENCY;
2266 irq.u.emerg.code = cpuaddr;
2267 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2268 return -EFAULT;
2269 n += sizeof(irq);
2270 }
2271 }
2272
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002273 if (sca_ext_call_pending(vcpu, &scn)) {
Jens Freimann816c7662014-11-24 17:13:46 +01002274 if (n + sizeof(irq) > len)
2275 return -ENOBUFS;
2276 memset(&irq, 0, sizeof(irq));
2277 irq.type = KVM_S390_INT_EXTERNAL_CALL;
Eugene (jno) Dvurechenskia5bd7642015-04-21 15:10:10 +02002278 irq.u.extcall.code = scn;
Jens Freimann816c7662014-11-24 17:13:46 +01002279 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2280 return -EFAULT;
2281 n += sizeof(irq);
2282 }
2283
2284 return n;
2285}