blob: 6c98fb61d154a4e9fd573abc7fe03ece471b6519 [file] [log] [blame]
Carsten Otteba5c1e92008-03-25 18:47:26 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * handling kvm guest interrupts
Carsten Otteba5c1e92008-03-25 18:47:26 +01003 *
Thomas Huth33b412a2015-02-11 10:38:46 +01004 * Copyright IBM Corp. 2008, 2015
Carsten Otteba5c1e92008-03-25 18:47:26 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
9 *
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
11 */
12
Christian Borntraegerca872302009-05-12 17:21:49 +020013#include <linux/interrupt.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010014#include <linux/kvm_host.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010015#include <linux/hrtimer.h>
Cornelia Huck84223592013-07-15 13:36:01 +020016#include <linux/mmu_context.h>
Christian Borntraeger3cd61292008-07-25 15:51:54 +020017#include <linux/signal.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Jens Freimann383d0b02014-07-29 15:11:49 +020019#include <linux/bitmap.h>
Jens Freimann94aa0332015-03-16 12:17:13 +010020#include <linux/vmalloc.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010021#include <asm/asm-offsets.h>
Thomas Huth33b412a2015-02-11 10:38:46 +010022#include <asm/dis.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010023#include <asm/uaccess.h>
David Hildenbrandea5f4962014-10-14 15:29:30 +020024#include <asm/sclp.h>
Jens Freimann6d3da242013-07-03 15:18:35 +020025#include <asm/isc.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010026#include "kvm-s390.h"
27#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020028#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010029
Cornelia Huckd8346b72012-12-20 15:32:08 +010030#define IOINT_SCHID_MASK 0x0000ffff
31#define IOINT_SSID_MASK 0x00030000
32#define IOINT_CSSID_MASK 0x03fc0000
33#define IOINT_AI_MASK 0x04000000
Jens Freimann44c6ca32014-04-16 13:57:18 +020034#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010035#define PFAULT_DONE 0x0680
36#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010037
Dominik Dingel3c038e62013-10-07 17:11:48 +020038int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010039{
40 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
41}
42
Cornelia Huckd8346b72012-12-20 15:32:08 +010043static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
44{
45 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
46}
47
Cornelia Huck48a3e952012-12-20 15:32:09 +010048static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
49{
50 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
51}
52
Carsten Otteba5c1e92008-03-25 18:47:26 +010053static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
54{
55 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
56 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
57 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
58 return 0;
59 return 1;
60}
61
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010062static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
63{
64 if (psw_extint_disabled(vcpu) ||
65 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
66 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +010067 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
68 /* No timer interrupts when single stepping */
69 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010070 return 1;
71}
72
David Hildenbrandb4aec922014-12-01 15:55:42 +010073static int ckc_irq_pending(struct kvm_vcpu *vcpu)
74{
75 if (!(vcpu->arch.sie_block->ckc <
76 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
77 return 0;
78 return ckc_interrupts_enabled(vcpu);
79}
80
81static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
82{
83 return !psw_extint_disabled(vcpu) &&
84 (vcpu->arch.sie_block->gcr[0] & 0x400ul);
85}
86
87static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
88{
89 return (vcpu->arch.sie_block->cputm >> 63) &&
90 cpu_timer_interrupts_enabled(vcpu);
91}
92
Jens Freimann6d3da242013-07-03 15:18:35 +020093static inline int is_ioirq(unsigned long irq_type)
Cornelia Huck79fd50c2013-02-07 13:20:52 +010094{
Jens Freimann6d3da242013-07-03 15:18:35 +020095 return ((irq_type >= IRQ_PEND_IO_ISC_0) &&
96 (irq_type <= IRQ_PEND_IO_ISC_7));
97}
Cornelia Huck79fd50c2013-02-07 13:20:52 +010098
Jens Freimann6d3da242013-07-03 15:18:35 +020099static uint64_t isc_to_isc_bits(int isc)
100{
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100101 return (0x80 >> isc) << 24;
102}
103
Jens Freimann6d3da242013-07-03 15:18:35 +0200104static inline u8 int_word_to_isc(u32 int_word)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100105{
Jens Freimann6d3da242013-07-03 15:18:35 +0200106 return (int_word & 0x38000000) >> 27;
107}
108
109static inline unsigned long pending_floating_irqs(struct kvm_vcpu *vcpu)
110{
111 return vcpu->kvm->arch.float_int.pending_irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100112}
113
Jens Freimann383d0b02014-07-29 15:11:49 +0200114static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu)
115{
116 return vcpu->arch.local_int.pending_irqs;
117}
118
Jens Freimann6d3da242013-07-03 15:18:35 +0200119static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
120 unsigned long active_mask)
Jens Freimann383d0b02014-07-29 15:11:49 +0200121{
Jens Freimann6d3da242013-07-03 15:18:35 +0200122 int i;
123
124 for (i = 0; i <= MAX_ISC; i++)
125 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
126 active_mask &= ~(1UL << (IRQ_PEND_IO_ISC_0 + i));
127
128 return active_mask;
129}
130
131static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
132{
133 unsigned long active_mask;
134
135 active_mask = pending_local_irqs(vcpu);
136 active_mask |= pending_floating_irqs(vcpu);
Jens Freimannffeca0a2015-04-17 10:21:04 +0200137 if (!active_mask)
138 return 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200139
140 if (psw_extint_disabled(vcpu))
141 active_mask &= ~IRQ_PEND_EXT_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200142 if (psw_ioint_disabled(vcpu))
143 active_mask &= ~IRQ_PEND_IO_MASK;
144 else
145 active_mask = disable_iscs(vcpu, active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200146 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
147 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
148 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
149 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
150 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
151 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
152 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
153 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
Jens Freimann6d3da242013-07-03 15:18:35 +0200154 if (!(vcpu->arch.sie_block->gcr[0] & 0x200ul))
155 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200156 if (psw_mchk_disabled(vcpu))
157 active_mask &= ~IRQ_PEND_MCHK_MASK;
Jens Freimann6d3da242013-07-03 15:18:35 +0200158 if (!(vcpu->arch.sie_block->gcr[14] &
159 vcpu->kvm->arch.float_int.mchk.cr14))
160 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
Jens Freimann383d0b02014-07-29 15:11:49 +0200161
David Hildenbrand6cddd432014-10-15 16:48:53 +0200162 /*
163 * STOP irqs will never be actively delivered. They are triggered via
164 * intercept requests and cleared when the stop intercept is performed.
165 */
166 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
167
Jens Freimann383d0b02014-07-29 15:11:49 +0200168 return active_mask;
169}
170
Carsten Otteba5c1e92008-03-25 18:47:26 +0100171static void __set_cpu_idle(struct kvm_vcpu *vcpu)
172{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100173 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
174 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
175}
176
177static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
178{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100179 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
180 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
181}
182
183static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
184{
David Hildenbrand49539192014-02-21 08:59:59 +0100185 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
186 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100187 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100188 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
189
190 if (guestdbg_enabled(vcpu)) {
191 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
192 LCTL_CR10 | LCTL_CR11);
193 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
194 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100195}
196
197static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
198{
199 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
200}
201
Jens Freimann6d3da242013-07-03 15:18:35 +0200202static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
203{
204 if (!(pending_floating_irqs(vcpu) & IRQ_PEND_IO_MASK))
205 return;
206 else if (psw_ioint_disabled(vcpu))
207 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
208 else
209 vcpu->arch.sie_block->lctl |= LCTL_CR6;
210}
211
Jens Freimann383d0b02014-07-29 15:11:49 +0200212static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
213{
214 if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK))
215 return;
216 if (psw_extint_disabled(vcpu))
217 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
218 else
219 vcpu->arch.sie_block->lctl |= LCTL_CR0;
220}
221
222static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
223{
224 if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
225 return;
226 if (psw_mchk_disabled(vcpu))
227 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
228 else
229 vcpu->arch.sie_block->lctl |= LCTL_CR14;
230}
231
David Hildenbrand6cddd432014-10-15 16:48:53 +0200232static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
233{
234 if (kvm_s390_is_stop_irq_pending(vcpu))
235 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
236}
237
Jens Freimann6d3da242013-07-03 15:18:35 +0200238/* Set interception request for non-deliverable interrupts */
239static void set_intercept_indicators(struct kvm_vcpu *vcpu)
Jens Freimann383d0b02014-07-29 15:11:49 +0200240{
Jens Freimann6d3da242013-07-03 15:18:35 +0200241 set_intercept_indicators_io(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200242 set_intercept_indicators_ext(vcpu);
243 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200244 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200245}
246
Jens Freimann8a2ef712014-07-23 16:36:06 +0200247static u16 get_ilc(struct kvm_vcpu *vcpu)
248{
Jens Freimann8a2ef712014-07-23 16:36:06 +0200249 switch (vcpu->arch.sie_block->icptcode) {
250 case ICPT_INST:
251 case ICPT_INSTPROGI:
252 case ICPT_OPEREXC:
253 case ICPT_PARTEXEC:
254 case ICPT_IOINST:
255 /* last instruction only stored for these icptcodes */
Thomas Huth33b412a2015-02-11 10:38:46 +0100256 return insn_length(vcpu->arch.sie_block->ipa >> 8);
Jens Freimann8a2ef712014-07-23 16:36:06 +0200257 case ICPT_PROGI:
258 return vcpu->arch.sie_block->pgmilc;
259 default:
260 return 0;
261 }
262}
263
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200264static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100265{
Jens Freimann383d0b02014-07-29 15:11:49 +0200266 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200267 int rc;
268
269 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
270 0, 0);
271
272 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
273 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100274 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200275 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
276 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
277 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
278 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200279 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100280 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200281}
282
283static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
284{
Jens Freimann383d0b02014-07-29 15:11:49 +0200285 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200286 int rc;
287
288 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
289 0, 0);
290
291 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
292 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100293 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200294 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
295 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
296 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
297 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200298 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100299 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200300}
301
Jens Freimann383d0b02014-07-29 15:11:49 +0200302static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200303{
Jens Freimann383d0b02014-07-29 15:11:49 +0200304 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
305 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200306 int rc;
307
Jens Freimann383d0b02014-07-29 15:11:49 +0200308 spin_lock(&li->lock);
309 ext = li->irq.ext;
310 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
311 li->irq.ext.ext_params2 = 0;
312 spin_unlock(&li->lock);
313
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200314 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
315 ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200316 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
317 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200318 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200319
320 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
321 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
322 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
323 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
324 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
325 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200326 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100327 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200328}
329
Jens Freimann383d0b02014-07-29 15:11:49 +0200330static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200331{
Jens Freimann6d3da242013-07-03 15:18:35 +0200332 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200333 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann6d3da242013-07-03 15:18:35 +0200334 struct kvm_s390_mchk_info mchk = {};
Eric Farmanbc17de72014-04-14 16:01:09 -0400335 unsigned long adtl_status_addr;
Jens Freimann6d3da242013-07-03 15:18:35 +0200336 int deliver = 0;
337 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200338
Jens Freimann6d3da242013-07-03 15:18:35 +0200339 spin_lock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200340 spin_lock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200341 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
342 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
343 /*
344 * If there was an exigent machine check pending, then any
345 * repressible machine checks that might have been pending
346 * are indicated along with it, so always clear bits for
347 * repressible and exigent interrupts
348 */
349 mchk = li->irq.mchk;
350 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
351 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
352 memset(&li->irq.mchk, 0, sizeof(mchk));
353 deliver = 1;
354 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200355 /*
Jens Freimann6d3da242013-07-03 15:18:35 +0200356 * We indicate floating repressible conditions along with
357 * other pending conditions. Channel Report Pending and Channel
358 * Subsystem damage are the only two and and are indicated by
359 * bits in mcic and masked in cr14.
Jens Freimann383d0b02014-07-29 15:11:49 +0200360 */
Jens Freimann6d3da242013-07-03 15:18:35 +0200361 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
362 mchk.mcic |= fi->mchk.mcic;
363 mchk.cr14 |= fi->mchk.cr14;
364 memset(&fi->mchk, 0, sizeof(mchk));
365 deliver = 1;
366 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200367 spin_unlock(&li->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +0200368 spin_unlock(&fi->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200369
Jens Freimann6d3da242013-07-03 15:18:35 +0200370 if (deliver) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200371 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200372 mchk.mcic);
373 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
374 KVM_S390_MCHK,
375 mchk.cr14, mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200376
Jens Freimann6d3da242013-07-03 15:18:35 +0200377 rc = kvm_s390_vcpu_store_status(vcpu,
378 KVM_S390_STORE_STATUS_PREFIXED);
379 rc |= read_guest_lc(vcpu, __LC_VX_SAVE_AREA_ADDR,
380 &adtl_status_addr,
381 sizeof(unsigned long));
382 rc |= kvm_s390_vcpu_store_adtl_status(vcpu,
383 adtl_status_addr);
384 rc |= put_guest_lc(vcpu, mchk.mcic,
385 (u64 __user *) __LC_MCCK_CODE);
386 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
387 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
388 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
389 &mchk.fixed_logout,
390 sizeof(mchk.fixed_logout));
391 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
392 &vcpu->arch.sie_block->gpsw,
393 sizeof(psw_t));
394 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
395 &vcpu->arch.sie_block->gpsw,
396 sizeof(psw_t));
397 }
Jens Freimann99e20002014-12-01 17:05:39 +0100398 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200399}
400
401static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
402{
Jens Freimann383d0b02014-07-29 15:11:49 +0200403 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200404 int rc;
405
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200406 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200407 vcpu->stat.deliver_restart_signal++;
408 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
409
410 rc = write_guest_lc(vcpu,
411 offsetof(struct _lowcore, restart_old_psw),
412 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
413 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
414 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200415 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100416 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200417}
418
Jens Freimann383d0b02014-07-29 15:11:49 +0200419static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200420{
Jens Freimann383d0b02014-07-29 15:11:49 +0200421 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
422 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200423
Jens Freimann383d0b02014-07-29 15:11:49 +0200424 spin_lock(&li->lock);
425 prefix = li->irq.prefix;
426 li->irq.prefix.address = 0;
427 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
428 spin_unlock(&li->lock);
429
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200430 vcpu->stat.deliver_prefix_signal++;
431 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
432 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200433 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200434
Jens Freimann383d0b02014-07-29 15:11:49 +0200435 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200436 return 0;
437}
438
Jens Freimann383d0b02014-07-29 15:11:49 +0200439static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200440{
Jens Freimann383d0b02014-07-29 15:11:49 +0200441 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200442 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200443 int cpu_addr;
444
445 spin_lock(&li->lock);
446 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
447 clear_bit(cpu_addr, li->sigp_emerg_pending);
448 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
449 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
450 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200451
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200452 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200453 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200454 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
455 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200456
457 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
458 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200459 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200460 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
461 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
462 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
463 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100464 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200465}
466
Jens Freimann383d0b02014-07-29 15:11:49 +0200467static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200468{
Jens Freimann383d0b02014-07-29 15:11:49 +0200469 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
470 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200471 int rc;
472
Jens Freimann383d0b02014-07-29 15:11:49 +0200473 spin_lock(&li->lock);
474 extcall = li->irq.extcall;
475 li->irq.extcall.code = 0;
476 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
477 spin_unlock(&li->lock);
478
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200479 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200480 vcpu->stat.deliver_external_call++;
481 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
482 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200483 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200484
485 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
486 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200487 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200488 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
489 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
490 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
491 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100492 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200493}
494
Jens Freimann383d0b02014-07-29 15:11:49 +0200495static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200496{
Jens Freimann383d0b02014-07-29 15:11:49 +0200497 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
498 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100499 int rc = 0, nullifying = false;
Jens Freimann8a2ef712014-07-23 16:36:06 +0200500 u16 ilc = get_ilc(vcpu);
David Hildenbrand87128362014-03-03 10:55:13 +0100501
Jens Freimann383d0b02014-07-29 15:11:49 +0200502 spin_lock(&li->lock);
503 pgm_info = li->irq.pgm;
504 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
505 memset(&li->irq.pgm, 0, sizeof(pgm_info));
506 spin_unlock(&li->lock);
507
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200508 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilc:%d",
Jens Freimann383d0b02014-07-29 15:11:49 +0200509 pgm_info.code, ilc);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200510 vcpu->stat.deliver_program_int++;
511 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200512 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200513
Jens Freimann383d0b02014-07-29 15:11:49 +0200514 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100515 case PGM_AFX_TRANSLATION:
516 case PGM_ASX_TRANSLATION:
517 case PGM_EX_TRANSLATION:
518 case PGM_LFX_TRANSLATION:
519 case PGM_LSTE_SEQUENCE:
520 case PGM_LSX_TRANSLATION:
521 case PGM_LX_TRANSLATION:
522 case PGM_PRIMARY_AUTHORITY:
523 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100524 nullifying = true;
525 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100526 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200527 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100528 (u64 *)__LC_TRANS_EXC_CODE);
529 break;
530 case PGM_ALEN_TRANSLATION:
531 case PGM_ALE_SEQUENCE:
532 case PGM_ASTE_INSTANCE:
533 case PGM_ASTE_SEQUENCE:
534 case PGM_ASTE_VALIDITY:
535 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200536 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100537 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100538 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100539 break;
540 case PGM_ASCE_TYPE:
541 case PGM_PAGE_TRANSLATION:
542 case PGM_REGION_FIRST_TRANS:
543 case PGM_REGION_SECOND_TRANS:
544 case PGM_REGION_THIRD_TRANS:
545 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200546 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100547 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200548 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100549 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200550 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100551 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100552 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100553 break;
554 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200555 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200556 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200557 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100558 (u64 *)__LC_MON_CODE);
559 break;
Eric Farman403c8642015-02-02 15:01:06 -0500560 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100561 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200562 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100563 (u32 *)__LC_DATA_EXC_CODE);
564 break;
565 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200566 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100567 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200568 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100569 (u8 *)__LC_EXC_ACCESS_ID);
570 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100571 case PGM_STACK_FULL:
572 case PGM_STACK_EMPTY:
573 case PGM_STACK_SPECIFICATION:
574 case PGM_STACK_TYPE:
575 case PGM_STACK_OPERATION:
576 case PGM_TRACE_TABEL:
577 case PGM_CRYPTO_OPERATION:
578 nullifying = true;
579 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100580 }
581
Jens Freimann383d0b02014-07-29 15:11:49 +0200582 if (pgm_info.code & PGM_PER) {
583 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100584 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200585 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100586 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200587 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100588 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200589 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100590 (u8 *) __LC_PER_ACCESS_ID);
591 }
592
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100593 if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
594 kvm_s390_rewind_psw(vcpu, ilc);
595
Jens Freimann8a2ef712014-07-23 16:36:06 +0200596 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
David Hildenbrand2ba45962015-03-25 13:12:32 +0100597 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
598 (u64 *) __LC_LAST_BREAK);
Jens Freimann383d0b02014-07-29 15:11:49 +0200599 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100600 (u16 *)__LC_PGM_INT_CODE);
601 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
602 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
603 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
604 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100605 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200606}
David Hildenbrand87128362014-03-03 10:55:13 +0100607
Jens Freimann6d3da242013-07-03 15:18:35 +0200608static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200609{
Jens Freimann6d3da242013-07-03 15:18:35 +0200610 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
611 struct kvm_s390_ext_info ext;
612 int rc = 0;
613
614 spin_lock(&fi->lock);
615 if (!(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
616 spin_unlock(&fi->lock);
617 return 0;
618 }
619 ext = fi->srv_signal;
620 memset(&fi->srv_signal, 0, sizeof(ext));
621 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
622 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200623
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200624 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
Jens Freimann6d3da242013-07-03 15:18:35 +0200625 ext.ext_params);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200626 vcpu->stat.deliver_service_signal++;
Jens Freimann6d3da242013-07-03 15:18:35 +0200627 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
628 ext.ext_params, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200629
630 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100631 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200632 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
633 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
634 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
635 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann6d3da242013-07-03 15:18:35 +0200636 rc |= put_guest_lc(vcpu, ext.ext_params,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200637 (u32 *)__LC_EXT_PARAMS);
Jens Freimann6d3da242013-07-03 15:18:35 +0200638
Jens Freimann99e20002014-12-01 17:05:39 +0100639 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200640}
641
Jens Freimann6d3da242013-07-03 15:18:35 +0200642static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200643{
Jens Freimann6d3da242013-07-03 15:18:35 +0200644 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
645 struct kvm_s390_interrupt_info *inti;
646 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200647
Jens Freimann6d3da242013-07-03 15:18:35 +0200648 spin_lock(&fi->lock);
649 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
650 struct kvm_s390_interrupt_info,
651 list);
652 if (inti) {
Jens Freimann6d3da242013-07-03 15:18:35 +0200653 list_del(&inti->list);
654 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
655 }
656 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
657 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
658 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200659
Jens Freimann6d3da242013-07-03 15:18:35 +0200660 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200661 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
662 KVM_S390_INT_PFAULT_DONE, 0,
663 inti->ext.ext_params2);
664 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
665 inti->ext.ext_params2);
666
Jens Freimann6d3da242013-07-03 15:18:35 +0200667 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
668 (u16 *)__LC_EXT_INT_CODE);
669 rc |= put_guest_lc(vcpu, PFAULT_DONE,
670 (u16 *)__LC_EXT_CPU_ADDR);
671 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
672 &vcpu->arch.sie_block->gpsw,
673 sizeof(psw_t));
674 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
675 &vcpu->arch.sie_block->gpsw,
676 sizeof(psw_t));
677 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
678 (u64 *)__LC_EXT_PARAMS2);
679 kfree(inti);
680 }
Jens Freimann99e20002014-12-01 17:05:39 +0100681 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200682}
683
Jens Freimann6d3da242013-07-03 15:18:35 +0200684static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200685{
Jens Freimann6d3da242013-07-03 15:18:35 +0200686 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
687 struct kvm_s390_interrupt_info *inti;
688 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200689
Jens Freimann6d3da242013-07-03 15:18:35 +0200690 spin_lock(&fi->lock);
691 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
692 struct kvm_s390_interrupt_info,
693 list);
694 if (inti) {
695 VCPU_EVENT(vcpu, 4,
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200696 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
Jens Freimann6d3da242013-07-03 15:18:35 +0200697 inti->ext.ext_params, inti->ext.ext_params2);
698 vcpu->stat.deliver_virtio_interrupt++;
699 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
700 inti->type,
701 inti->ext.ext_params,
702 inti->ext.ext_params2);
703 list_del(&inti->list);
704 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
705 }
706 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
707 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
708 spin_unlock(&fi->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200709
Jens Freimann6d3da242013-07-03 15:18:35 +0200710 if (inti) {
711 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
712 (u16 *)__LC_EXT_INT_CODE);
713 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
714 (u16 *)__LC_EXT_CPU_ADDR);
715 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
716 &vcpu->arch.sie_block->gpsw,
717 sizeof(psw_t));
718 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
719 &vcpu->arch.sie_block->gpsw,
720 sizeof(psw_t));
721 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
722 (u32 *)__LC_EXT_PARAMS);
723 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
724 (u64 *)__LC_EXT_PARAMS2);
725 kfree(inti);
726 }
Jens Freimann99e20002014-12-01 17:05:39 +0100727 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200728}
729
730static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
Jens Freimann6d3da242013-07-03 15:18:35 +0200731 unsigned long irq_type)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200732{
Jens Freimann6d3da242013-07-03 15:18:35 +0200733 struct list_head *isc_list;
734 struct kvm_s390_float_interrupt *fi;
735 struct kvm_s390_interrupt_info *inti = NULL;
736 int rc = 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200737
Jens Freimann6d3da242013-07-03 15:18:35 +0200738 fi = &vcpu->kvm->arch.float_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200739
Jens Freimann6d3da242013-07-03 15:18:35 +0200740 spin_lock(&fi->lock);
741 isc_list = &fi->lists[irq_type - IRQ_PEND_IO_ISC_0];
742 inti = list_first_entry_or_null(isc_list,
743 struct kvm_s390_interrupt_info,
744 list);
745 if (inti) {
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200746 VCPU_EVENT(vcpu, 4, "deliver: I/O 0x%llx", inti->type);
Jens Freimann6d3da242013-07-03 15:18:35 +0200747 vcpu->stat.deliver_io_int++;
748 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
749 inti->type,
750 ((__u32)inti->io.subchannel_id << 16) |
751 inti->io.subchannel_nr,
752 ((__u64)inti->io.io_int_parm << 32) |
753 inti->io.io_int_word);
754 list_del(&inti->list);
755 fi->counters[FIRQ_CNTR_IO] -= 1;
756 }
757 if (list_empty(isc_list))
758 clear_bit(irq_type, &fi->pending_irqs);
759 spin_unlock(&fi->lock);
David Hildenbrand87128362014-03-03 10:55:13 +0100760
Jens Freimann6d3da242013-07-03 15:18:35 +0200761 if (inti) {
762 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
763 (u16 *)__LC_SUBCHANNEL_ID);
764 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
765 (u16 *)__LC_SUBCHANNEL_NR);
766 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
767 (u32 *)__LC_IO_INT_PARM);
768 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
769 (u32 *)__LC_IO_INT_WORD);
770 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
771 &vcpu->arch.sie_block->gpsw,
772 sizeof(psw_t));
773 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
774 &vcpu->arch.sie_block->gpsw,
775 sizeof(psw_t));
776 kfree(inti);
777 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200778
Jens Freimann99e20002014-12-01 17:05:39 +0100779 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200780}
781
782typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
783
784static const deliver_irq_t deliver_irq_funcs[] = {
785 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
Jens Freimann6d3da242013-07-03 15:18:35 +0200786 [IRQ_PEND_MCHK_REP] = __deliver_machine_check,
Jens Freimann383d0b02014-07-29 15:11:49 +0200787 [IRQ_PEND_PROG] = __deliver_prog,
788 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
789 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
790 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
791 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
792 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200793 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
794 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
Jens Freimann6d3da242013-07-03 15:18:35 +0200795 [IRQ_PEND_EXT_SERVICE] = __deliver_service,
796 [IRQ_PEND_PFAULT_DONE] = __deliver_pfault_done,
797 [IRQ_PEND_VIRTIO] = __deliver_virtio,
Jens Freimann383d0b02014-07-29 15:11:49 +0200798};
799
David Hildenbrandea5f4962014-10-14 15:29:30 +0200800/* Check whether an external call is pending (deliverable or not) */
801int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100802{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200803 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
804 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
David Hildenbrand49539192014-02-21 08:59:59 +0100805
David Hildenbrand37c5f6c2015-05-06 13:18:59 +0200806 if (!sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +0200807 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100808
David Hildenbrandea5f4962014-10-14 15:29:30 +0200809 return (sigp_ctrl & SIGP_CTRL_C) &&
810 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
David Hildenbrand49539192014-02-21 08:59:59 +0100811}
812
David Hildenbrand9a022062014-08-05 17:40:47 +0200813int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100814{
Jens Freimann383d0b02014-07-29 15:11:49 +0200815 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100816
Jens Freimann6d3da242013-07-03 15:18:35 +0200817 rc = !!deliverable_irqs(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100818
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100819 if (!rc && kvm_cpu_has_pending_timer(vcpu))
820 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100821
David Hildenbrandea5f4962014-10-14 15:29:30 +0200822 /* external call pending and deliverable */
823 if (!rc && kvm_s390_ext_call_pending(vcpu) &&
824 !psw_extint_disabled(vcpu) &&
825 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand49539192014-02-21 08:59:59 +0100826 rc = 1;
827
David Hildenbrand9a022062014-08-05 17:40:47 +0200828 if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
David Hildenbrand6cddd432014-10-15 16:48:53 +0200829 rc = 1;
830
Carsten Otteba5c1e92008-03-25 18:47:26 +0100831 return rc;
832}
833
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300834int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
835{
David Hildenbrandb4aec922014-12-01 15:55:42 +0100836 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300837}
838
Carsten Otteba5c1e92008-03-25 18:47:26 +0100839int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
840{
841 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100842
843 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100844
David Hildenbrand0759d062014-05-13 16:54:32 +0200845 /* fast path */
846 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
847 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200848
Carsten Otteba5c1e92008-03-25 18:47:26 +0100849 if (psw_interrupts_disabled(vcpu)) {
850 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100851 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100852 }
853
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100854 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100855 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +0100856 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100857 goto no_timer;
858 }
859
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200860 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100861 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
David Hildenbrandbda343e2014-12-12 12:26:40 +0100862
863 /* underflow */
864 if (vcpu->arch.sie_block->ckc < now)
865 return 0;
866
867 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200868 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
Christian Borntraeger3f24ba12015-07-09 14:08:18 +0200869 VCPU_EVENT(vcpu, 4, "enabled wait via clock comparator: %llu ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100870no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200871 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200872 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100873 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200874 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
875
David Hildenbrand2d00f752014-12-11 10:18:01 +0100876 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100877 return 0;
878}
879
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200880void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
881{
882 if (waitqueue_active(&vcpu->wq)) {
883 /*
884 * The vcpu gave up the cpu voluntarily, mark it as a good
885 * yield-candidate.
886 */
887 vcpu->preempted = true;
888 wake_up_interruptible(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +0200889 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200890 }
891}
892
Christian Borntraegerca872302009-05-12 17:21:49 +0200893enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
894{
895 struct kvm_vcpu *vcpu;
David Hildenbrand2d00f752014-12-11 10:18:01 +0100896 u64 now, sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +0200897
898 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrand2d00f752014-12-11 10:18:01 +0100899 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
900 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200901
David Hildenbrand2d00f752014-12-11 10:18:01 +0100902 /*
903 * If the monotonic clock runs faster than the tod clock we might be
904 * woken up too early and have to go back to sleep to avoid deadlocks.
905 */
906 if (vcpu->arch.sie_block->ckc > now &&
907 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
908 return HRTIMER_RESTART;
909 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200910 return HRTIMER_NORESTART;
911}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100912
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100913void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
914{
915 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100916
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200917 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200918 li->pending_irqs = 0;
919 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
920 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200921 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100922
923 /* clear pending external calls set by sigp interpretation facility */
Jens Freimann383d0b02014-07-29 15:11:49 +0200924 atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200925 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100926}
927
Christian Borntraeger614aeab2014-08-25 12:27:29 +0200928int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100929{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200930 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200931 deliver_irq_t func;
Jens Freimann79395032014-04-17 10:10:30 +0200932 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200933 unsigned long irq_type;
Jens Freimann6d3da242013-07-03 15:18:35 +0200934 unsigned long irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100935
936 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100937
Jens Freimann383d0b02014-07-29 15:11:49 +0200938 /* pending ckc conditions might have been invalidated */
939 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
David Hildenbrandb4aec922014-12-01 15:55:42 +0100940 if (ckc_irq_pending(vcpu))
Jens Freimann383d0b02014-07-29 15:11:49 +0200941 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
942
David Hildenbrandb4aec922014-12-01 15:55:42 +0100943 /* pending cpu timer conditions might have been invalidated */
944 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
945 if (cpu_timer_irq_pending(vcpu))
946 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
947
Jens Freimannffeca0a2015-04-17 10:21:04 +0200948 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
Jens Freimann383d0b02014-07-29 15:11:49 +0200949 /* bits are in the order of interrupt priority */
Jens Freimann6d3da242013-07-03 15:18:35 +0200950 irq_type = find_first_bit(&irqs, IRQ_PEND_COUNT);
Jens Freimann6d3da242013-07-03 15:18:35 +0200951 if (is_ioirq(irq_type)) {
952 rc = __deliver_io(vcpu, irq_type);
953 } else {
954 func = deliver_irq_funcs[irq_type];
955 if (!func) {
956 WARN_ON_ONCE(func == NULL);
957 clear_bit(irq_type, &li->pending_irqs);
958 continue;
959 }
960 rc = func(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200961 }
Jens Freimannffeca0a2015-04-17 10:21:04 +0200962 }
Jens Freimann383d0b02014-07-29 15:11:49 +0200963
Jens Freimann6d3da242013-07-03 15:18:35 +0200964 set_intercept_indicators(vcpu);
Jens Freimann79395032014-04-17 10:10:30 +0200965
966 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100967}
968
Jens Freimann383d0b02014-07-29 15:11:49 +0200969static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +0200970{
971 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
972
David Hildenbranded2afcf2015-07-20 10:33:03 +0200973 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
974 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
975 irq->u.pgm.code, 0);
976
Jens Freimann383d0b02014-07-29 15:11:49 +0200977 li->irq.pgm = irq->u.pgm;
Jens Freimann91851242014-12-01 16:43:40 +0100978 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +0200979 return 0;
980}
981
Carsten Otteba5c1e92008-03-25 18:47:26 +0100982int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
983{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200984 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200985 struct kvm_s390_irq irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100986
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200987 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200988 irq.u.pgm.code = code;
989 __inject_prog(vcpu, &irq);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200990 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200991 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100992 return 0;
993}
994
Jens Freimannbcd84682014-02-11 11:07:05 +0100995int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
996 struct kvm_s390_pgm_info *pgm_info)
997{
998 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200999 struct kvm_s390_irq irq;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001000 int rc;
Jens Freimannbcd84682014-02-11 11:07:05 +01001001
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001002 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +02001003 irq.u.pgm = *pgm_info;
1004 rc = __inject_prog(vcpu, &irq);
Jens Freimannbcd84682014-02-11 11:07:05 +01001005 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001006 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001007 return rc;
1008}
1009
Jens Freimann383d0b02014-07-29 15:11:49 +02001010static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001011{
1012 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1013
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001014 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1015 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001016 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1017 irq->u.ext.ext_params,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001018 irq->u.ext.ext_params2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001019
1020 li->irq.ext = irq->u.ext;
1021 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001022 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1023 return 0;
1024}
1025
David Hildenbrandea5f4962014-10-14 15:29:30 +02001026static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
1027{
1028 unsigned char new_val, old_val;
1029 uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
1030
1031 new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
1032 old_val = *sigp_ctrl & ~SIGP_CTRL_C;
1033 if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
1034 /* another external call is pending */
1035 return -EBUSY;
1036 }
1037 atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
1038 return 0;
1039}
1040
Christian Borntraeger0675d922015-01-15 12:40:42 +01001041static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001042{
1043 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001044 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001045 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001046
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001047 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001048 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001049 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001050 src_id, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001051
David Hildenbrandea5f4962014-10-14 15:29:30 +02001052 /* sending vcpu invalid */
1053 if (src_id >= KVM_MAX_VCPUS ||
1054 kvm_get_vcpu(vcpu->kvm, src_id) == NULL)
1055 return -EINVAL;
1056
David Hildenbrand37c5f6c2015-05-06 13:18:59 +02001057 if (sclp.has_sigpif)
David Hildenbrandea5f4962014-10-14 15:29:30 +02001058 return __inject_extcall_sigpif(vcpu, src_id);
1059
David Hildenbrandb938eace2015-04-30 13:33:59 +02001060 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
David Hildenbrandea5f4962014-10-14 15:29:30 +02001061 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001062 *extcall = irq->u.extcall;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001063 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1064 return 0;
1065}
1066
Jens Freimann383d0b02014-07-29 15:11:49 +02001067static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001068{
1069 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001070 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001071
David Hildenbranded2afcf2015-07-20 10:33:03 +02001072 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001073 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001074 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001075 irq->u.prefix.address, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001076
David Hildenbranda3a9c592014-10-14 09:44:55 +02001077 if (!is_vcpu_stopped(vcpu))
1078 return -EBUSY;
1079
Jens Freimann383d0b02014-07-29 15:11:49 +02001080 *prefix = irq->u.prefix;
1081 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001082 return 0;
1083}
1084
David Hildenbrand6cddd432014-10-15 16:48:53 +02001085#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001086static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001087{
1088 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001089 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001090 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001091
David Hildenbranded2afcf2015-07-20 10:33:03 +02001092 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001093
David Hildenbrand28225452014-10-15 16:48:16 +02001094 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1095 return -EINVAL;
1096
David Hildenbrand6cddd432014-10-15 16:48:53 +02001097 if (is_vcpu_stopped(vcpu)) {
1098 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1099 rc = kvm_s390_store_status_unloaded(vcpu,
1100 KVM_S390_STORE_STATUS_NOADDR);
1101 return rc;
1102 }
1103
1104 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1105 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001106 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001107 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001108 return 0;
1109}
1110
1111static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001112 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001113{
1114 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1115
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001116 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
David Hildenbranded2afcf2015-07-20 10:33:03 +02001117 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001118
1119 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001120 return 0;
1121}
1122
1123static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001124 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001125{
1126 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1127
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001128 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
Jens Freimann383d0b02014-07-29 15:11:49 +02001129 irq->u.emerg.code);
1130 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001131 irq->u.emerg.code, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001132
Jens Freimann49538d12014-12-18 15:48:14 +01001133 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001134 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001135 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1136 return 0;
1137}
1138
Jens Freimann383d0b02014-07-29 15:11:49 +02001139static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001140{
1141 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001142 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001143
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001144 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001145 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001146 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001147 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001148
1149 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001150 * Because repressible machine checks can be indicated along with
1151 * exigent machine checks (PoP, Chapter 11, Interruption action)
1152 * we need to combine cr14, mcic and external damage code.
1153 * Failing storage address and the logout area should not be or'ed
1154 * together, we just indicate the last occurrence of the corresponding
1155 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001156 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001157 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001158 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001159 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1160 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1161 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1162 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001163 if (mchk->mcic & MCHK_EX_MASK)
1164 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1165 else if (mchk->mcic & MCHK_REP_MASK)
1166 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001167 return 0;
1168}
1169
Jens Freimann383d0b02014-07-29 15:11:49 +02001170static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001171{
1172 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1173
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001174 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001175 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001176 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001177
1178 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001179 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1180 return 0;
1181}
1182
Jens Freimann383d0b02014-07-29 15:11:49 +02001183static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001184{
1185 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1186
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001187 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
Jens Freimann383d0b02014-07-29 15:11:49 +02001188 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
David Hildenbranded2afcf2015-07-20 10:33:03 +02001189 0, 0);
Jens Freimann383d0b02014-07-29 15:11:49 +02001190
1191 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001192 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001193 return 0;
1194}
1195
Jens Freimann6d3da242013-07-03 15:18:35 +02001196static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1197 int isc, u32 schid)
1198{
1199 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1200 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1201 struct kvm_s390_interrupt_info *iter;
1202 u16 id = (schid & 0xffff0000U) >> 16;
1203 u16 nr = schid & 0x0000ffffU;
Jens Freimann383d0b02014-07-29 15:11:49 +02001204
Jens Freimann6d3da242013-07-03 15:18:35 +02001205 spin_lock(&fi->lock);
1206 list_for_each_entry(iter, isc_list, list) {
1207 if (schid && (id != iter->io.subchannel_id ||
1208 nr != iter->io.subchannel_nr))
1209 continue;
1210 /* found an appropriate entry */
1211 list_del_init(&iter->list);
1212 fi->counters[FIRQ_CNTR_IO] -= 1;
1213 if (list_empty(isc_list))
1214 clear_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
1215 spin_unlock(&fi->lock);
1216 return iter;
1217 }
1218 spin_unlock(&fi->lock);
1219 return NULL;
1220}
1221
1222/*
1223 * Dequeue and return an I/O interrupt matching any of the interruption
1224 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1225 */
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001226struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
Jens Freimann6d3da242013-07-03 15:18:35 +02001227 u64 isc_mask, u32 schid)
1228{
1229 struct kvm_s390_interrupt_info *inti = NULL;
1230 int isc;
1231
1232 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1233 if (isc_mask & isc_to_isc_bits(isc))
1234 inti = get_io_int(kvm, isc, schid);
1235 }
1236 return inti;
1237}
1238
1239#define SCCB_MASK 0xFFFFFFF8
1240#define SCCB_EVENT_PENDING 0x3
1241
1242static int __inject_service(struct kvm *kvm,
1243 struct kvm_s390_interrupt_info *inti)
1244{
1245 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1246
1247 spin_lock(&fi->lock);
1248 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1249 /*
1250 * Early versions of the QEMU s390 bios will inject several
1251 * service interrupts after another without handling a
1252 * condition code indicating busy.
1253 * We will silently ignore those superfluous sccb values.
1254 * A future version of QEMU will take care of serialization
1255 * of servc requests
1256 */
1257 if (fi->srv_signal.ext_params & SCCB_MASK)
1258 goto out;
1259 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1260 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1261out:
1262 spin_unlock(&fi->lock);
1263 kfree(inti);
1264 return 0;
1265}
1266
1267static int __inject_virtio(struct kvm *kvm,
1268 struct kvm_s390_interrupt_info *inti)
1269{
1270 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1271
1272 spin_lock(&fi->lock);
1273 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1274 spin_unlock(&fi->lock);
1275 return -EBUSY;
1276 }
1277 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1278 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1279 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1280 spin_unlock(&fi->lock);
1281 return 0;
1282}
1283
1284static int __inject_pfault_done(struct kvm *kvm,
1285 struct kvm_s390_interrupt_info *inti)
1286{
1287 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1288
1289 spin_lock(&fi->lock);
1290 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1291 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1292 spin_unlock(&fi->lock);
1293 return -EBUSY;
1294 }
1295 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1296 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1297 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1298 spin_unlock(&fi->lock);
1299 return 0;
1300}
1301
1302#define CR_PENDING_SUBCLASS 28
1303static int __inject_float_mchk(struct kvm *kvm,
1304 struct kvm_s390_interrupt_info *inti)
1305{
1306 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1307
1308 spin_lock(&fi->lock);
1309 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1310 fi->mchk.mcic |= inti->mchk.mcic;
1311 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1312 spin_unlock(&fi->lock);
1313 kfree(inti);
1314 return 0;
1315}
1316
1317static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001318{
1319 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001320 struct list_head *list;
1321 int isc;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001322
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001323 fi = &kvm->arch.float_int;
1324 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001325 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1326 spin_unlock(&fi->lock);
1327 return -EBUSY;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001328 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001329 fi->counters[FIRQ_CNTR_IO] += 1;
1330
1331 isc = int_word_to_isc(inti->io.io_int_word);
1332 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1333 list_add_tail(&inti->list, list);
1334 set_bit(IRQ_PEND_IO_ISC_0 + isc, &fi->pending_irqs);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001335 spin_unlock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001336 return 0;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001337}
1338
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001339/*
1340 * Find a destination VCPU for a floating irq and kick it.
1341 */
1342static void __floating_irq_kick(struct kvm *kvm, u64 type)
1343{
1344 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1345 struct kvm_s390_local_interrupt *li;
1346 struct kvm_vcpu *dst_vcpu;
1347 int sigcpu, online_vcpus, nr_tries = 0;
1348
1349 online_vcpus = atomic_read(&kvm->online_vcpus);
1350 if (!online_vcpus)
1351 return;
1352
1353 /* find idle VCPUs first, then round robin */
1354 sigcpu = find_first_bit(fi->idle_mask, online_vcpus);
1355 if (sigcpu == online_vcpus) {
1356 do {
1357 sigcpu = fi->next_rr_cpu;
1358 fi->next_rr_cpu = (fi->next_rr_cpu + 1) % online_vcpus;
1359 /* avoid endless loops if all vcpus are stopped */
1360 if (nr_tries++ >= online_vcpus)
1361 return;
1362 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1363 }
1364 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1365
1366 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1367 li = &dst_vcpu->arch.local_int;
1368 spin_lock(&li->lock);
1369 switch (type) {
1370 case KVM_S390_MCHK:
1371 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
1372 break;
1373 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1374 atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags);
1375 break;
1376 default:
1377 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1378 break;
1379 }
1380 spin_unlock(&li->lock);
1381 kvm_s390_vcpu_wakeup(dst_vcpu);
1382}
1383
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001384static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001385{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001386 struct kvm_s390_float_interrupt *fi;
Jens Freimann6d3da242013-07-03 15:18:35 +02001387 u64 type = READ_ONCE(inti->type);
1388 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001389
Carsten Otteba5c1e92008-03-25 18:47:26 +01001390 fi = &kvm->arch.float_int;
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001391
Jens Freimann6d3da242013-07-03 15:18:35 +02001392 switch (type) {
1393 case KVM_S390_MCHK:
1394 rc = __inject_float_mchk(kvm, inti);
1395 break;
1396 case KVM_S390_INT_VIRTIO:
1397 rc = __inject_virtio(kvm, inti);
1398 break;
1399 case KVM_S390_INT_SERVICE:
1400 rc = __inject_service(kvm, inti);
1401 break;
1402 case KVM_S390_INT_PFAULT_DONE:
1403 rc = __inject_pfault_done(kvm, inti);
1404 break;
1405 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1406 rc = __inject_io(kvm, inti);
1407 break;
1408 default:
1409 rc = -EINVAL;
Cornelia Huckd8346b72012-12-20 15:32:08 +01001410 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001411 if (rc)
1412 return rc;
1413
David Hildenbrand96e0ed22015-01-14 14:08:38 +01001414 __floating_irq_kick(kvm, type);
Jens Freimann6d3da242013-07-03 15:18:35 +02001415 return 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001416}
1417
1418int kvm_s390_inject_vm(struct kvm *kvm,
1419 struct kvm_s390_interrupt *s390int)
1420{
1421 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001422 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001423
1424 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1425 if (!inti)
1426 return -ENOMEM;
1427
1428 inti->type = s390int->type;
1429 switch (inti->type) {
1430 case KVM_S390_INT_VIRTIO:
1431 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1432 s390int->parm, s390int->parm64);
1433 inti->ext.ext_params = s390int->parm;
1434 inti->ext.ext_params2 = s390int->parm64;
1435 break;
1436 case KVM_S390_INT_SERVICE:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001437 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001438 inti->ext.ext_params = s390int->parm;
1439 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001440 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001441 inti->ext.ext_params2 = s390int->parm64;
1442 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001443 case KVM_S390_MCHK:
Christian Borntraeger3f24ba12015-07-09 14:08:18 +02001444 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
Jens Freimannc05c4182013-10-07 16:13:45 +02001445 s390int->parm64);
1446 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1447 inti->mchk.mcic = s390int->parm64;
1448 break;
1449 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1450 if (inti->type & IOINT_AI_MASK)
1451 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1452 else
1453 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1454 s390int->type & IOINT_CSSID_MASK,
1455 s390int->type & IOINT_SSID_MASK,
1456 s390int->type & IOINT_SCHID_MASK);
1457 inti->io.subchannel_id = s390int->parm >> 16;
1458 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1459 inti->io.io_int_parm = s390int->parm64 >> 32;
1460 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1461 break;
1462 default:
1463 kfree(inti);
1464 return -EINVAL;
1465 }
1466 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1467 2);
1468
David Hildenbrand428d53b2015-01-16 12:58:09 +01001469 rc = __inject_vm(kvm, inti);
1470 if (rc)
1471 kfree(inti);
1472 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001473}
1474
David Hildenbrand15462e32015-02-04 15:59:11 +01001475int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001476 struct kvm_s390_interrupt_info *inti)
1477{
David Hildenbrand15462e32015-02-04 15:59:11 +01001478 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001479}
1480
Jens Freimann383d0b02014-07-29 15:11:49 +02001481int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1482 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001483{
Jens Freimann383d0b02014-07-29 15:11:49 +02001484 irq->type = s390int->type;
1485 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001486 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001487 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001488 return -EINVAL;
1489 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001490 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001491 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001492 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001493 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001494 case KVM_S390_SIGP_STOP:
1495 irq->u.stop.flags = s390int->parm;
1496 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001497 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001498 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001499 return -EINVAL;
1500 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001501 break;
1502 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001503 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001504 return -EINVAL;
1505 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001506 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001507 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001508 irq->u.mchk.mcic = s390int->parm64;
1509 break;
1510 }
1511 return 0;
1512}
1513
David Hildenbrand6cddd432014-10-15 16:48:53 +02001514int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1515{
1516 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1517
1518 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1519}
1520
1521void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1522{
1523 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1524
1525 spin_lock(&li->lock);
1526 li->irq.stop.flags = 0;
1527 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1528 spin_unlock(&li->lock);
1529}
1530
Jens Freimann79e87a12015-03-19 15:12:12 +01001531static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann383d0b02014-07-29 15:11:49 +02001532{
Jens Freimann383d0b02014-07-29 15:11:49 +02001533 int rc;
1534
Jens Freimann383d0b02014-07-29 15:11:49 +02001535 switch (irq->type) {
1536 case KVM_S390_PROGRAM_INT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001537 rc = __inject_prog(vcpu, irq);
1538 break;
1539 case KVM_S390_SIGP_SET_PREFIX:
1540 rc = __inject_set_prefix(vcpu, irq);
1541 break;
1542 case KVM_S390_SIGP_STOP:
1543 rc = __inject_sigp_stop(vcpu, irq);
1544 break;
1545 case KVM_S390_RESTART:
1546 rc = __inject_sigp_restart(vcpu, irq);
1547 break;
1548 case KVM_S390_INT_CLOCK_COMP:
1549 rc = __inject_ckc(vcpu);
1550 break;
1551 case KVM_S390_INT_CPU_TIMER:
1552 rc = __inject_cpu_timer(vcpu);
1553 break;
1554 case KVM_S390_INT_EXTERNAL_CALL:
1555 rc = __inject_extcall(vcpu, irq);
1556 break;
1557 case KVM_S390_INT_EMERGENCY:
1558 rc = __inject_sigp_emergency(vcpu, irq);
1559 break;
1560 case KVM_S390_MCHK:
1561 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001562 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001563 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001564 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001565 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001566 case KVM_S390_INT_VIRTIO:
1567 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001568 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001569 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001570 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001571 }
Jens Freimann79e87a12015-03-19 15:12:12 +01001572
1573 return rc;
1574}
1575
1576int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1577{
1578 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1579 int rc;
1580
1581 spin_lock(&li->lock);
1582 rc = do_inject_vcpu(vcpu, irq);
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001583 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001584 if (!rc)
1585 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001586 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001587}
Jens Freimannc05c4182013-10-07 16:13:45 +02001588
Jens Freimann6d3da242013-07-03 15:18:35 +02001589static inline void clear_irq_list(struct list_head *_list)
Jens Freimannc05c4182013-10-07 16:13:45 +02001590{
Jens Freimann6d3da242013-07-03 15:18:35 +02001591 struct kvm_s390_interrupt_info *inti, *n;
Jens Freimannc05c4182013-10-07 16:13:45 +02001592
Jens Freimann6d3da242013-07-03 15:18:35 +02001593 list_for_each_entry_safe(inti, n, _list, list) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001594 list_del(&inti->list);
1595 kfree(inti);
1596 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001597}
1598
Jens Freimann94aa0332015-03-16 12:17:13 +01001599static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
1600 struct kvm_s390_irq *irq)
Jens Freimannc05c4182013-10-07 16:13:45 +02001601{
Jens Freimann94aa0332015-03-16 12:17:13 +01001602 irq->type = inti->type;
Jens Freimannc05c4182013-10-07 16:13:45 +02001603 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001604 case KVM_S390_INT_PFAULT_INIT:
1605 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001606 case KVM_S390_INT_VIRTIO:
Jens Freimann94aa0332015-03-16 12:17:13 +01001607 irq->u.ext = inti->ext;
Jens Freimannc05c4182013-10-07 16:13:45 +02001608 break;
1609 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann94aa0332015-03-16 12:17:13 +01001610 irq->u.io = inti->io;
Jens Freimannc05c4182013-10-07 16:13:45 +02001611 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001612 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001613}
1614
Jens Freimann6d3da242013-07-03 15:18:35 +02001615void kvm_s390_clear_float_irqs(struct kvm *kvm)
1616{
1617 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1618 int i;
1619
1620 spin_lock(&fi->lock);
Jens Freimannf2ae45e2015-06-22 13:20:12 +02001621 fi->pending_irqs = 0;
1622 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
1623 memset(&fi->mchk, 0, sizeof(fi->mchk));
Jens Freimann6d3da242013-07-03 15:18:35 +02001624 for (i = 0; i < FIRQ_LIST_COUNT; i++)
1625 clear_irq_list(&fi->lists[i]);
1626 for (i = 0; i < FIRQ_MAX_COUNT; i++)
1627 fi->counters[i] = 0;
1628 spin_unlock(&fi->lock);
1629};
1630
Jens Freimann94aa0332015-03-16 12:17:13 +01001631static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
Jens Freimannc05c4182013-10-07 16:13:45 +02001632{
1633 struct kvm_s390_interrupt_info *inti;
1634 struct kvm_s390_float_interrupt *fi;
Jens Freimann94aa0332015-03-16 12:17:13 +01001635 struct kvm_s390_irq *buf;
Jens Freimann6d3da242013-07-03 15:18:35 +02001636 struct kvm_s390_irq *irq;
Jens Freimann94aa0332015-03-16 12:17:13 +01001637 int max_irqs;
Jens Freimannc05c4182013-10-07 16:13:45 +02001638 int ret = 0;
1639 int n = 0;
Jens Freimann6d3da242013-07-03 15:18:35 +02001640 int i;
Jens Freimannc05c4182013-10-07 16:13:45 +02001641
Jens Freimann94aa0332015-03-16 12:17:13 +01001642 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
1643 return -EINVAL;
1644
1645 /*
1646 * We are already using -ENOMEM to signal
1647 * userspace it may retry with a bigger buffer,
1648 * so we need to use something else for this case
1649 */
1650 buf = vzalloc(len);
1651 if (!buf)
1652 return -ENOBUFS;
1653
1654 max_irqs = len / sizeof(struct kvm_s390_irq);
1655
Jens Freimannc05c4182013-10-07 16:13:45 +02001656 fi = &kvm->arch.float_int;
1657 spin_lock(&fi->lock);
Jens Freimann6d3da242013-07-03 15:18:35 +02001658 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
1659 list_for_each_entry(inti, &fi->lists[i], list) {
1660 if (n == max_irqs) {
1661 /* signal userspace to try again */
1662 ret = -ENOMEM;
1663 goto out;
1664 }
1665 inti_to_irq(inti, &buf[n]);
1666 n++;
1667 }
1668 }
1669 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs)) {
Jens Freimann94aa0332015-03-16 12:17:13 +01001670 if (n == max_irqs) {
Jens Freimannc05c4182013-10-07 16:13:45 +02001671 /* signal userspace to try again */
1672 ret = -ENOMEM;
Jens Freimann6d3da242013-07-03 15:18:35 +02001673 goto out;
Jens Freimannc05c4182013-10-07 16:13:45 +02001674 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001675 irq = (struct kvm_s390_irq *) &buf[n];
1676 irq->type = KVM_S390_INT_SERVICE;
1677 irq->u.ext = fi->srv_signal;
Jens Freimannc05c4182013-10-07 16:13:45 +02001678 n++;
1679 }
Jens Freimann6d3da242013-07-03 15:18:35 +02001680 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
1681 if (n == max_irqs) {
1682 /* signal userspace to try again */
1683 ret = -ENOMEM;
1684 goto out;
1685 }
1686 irq = (struct kvm_s390_irq *) &buf[n];
1687 irq->type = KVM_S390_MCHK;
1688 irq->u.mchk = fi->mchk;
1689 n++;
1690}
1691
1692out:
Jens Freimannc05c4182013-10-07 16:13:45 +02001693 spin_unlock(&fi->lock);
Jens Freimann94aa0332015-03-16 12:17:13 +01001694 if (!ret && n > 0) {
1695 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
1696 ret = -EFAULT;
1697 }
1698 vfree(buf);
Jens Freimannc05c4182013-10-07 16:13:45 +02001699
1700 return ret < 0 ? ret : n;
1701}
1702
1703static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1704{
1705 int r;
1706
1707 switch (attr->group) {
1708 case KVM_DEV_FLIC_GET_ALL_IRQS:
Jens Freimann94aa0332015-03-16 12:17:13 +01001709 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
Jens Freimannc05c4182013-10-07 16:13:45 +02001710 attr->attr);
1711 break;
1712 default:
1713 r = -EINVAL;
1714 }
1715
1716 return r;
1717}
1718
1719static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1720 u64 addr)
1721{
1722 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1723 void *target = NULL;
1724 void __user *source;
1725 u64 size;
1726
1727 if (get_user(inti->type, (u64 __user *)addr))
1728 return -EFAULT;
1729
1730 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001731 case KVM_S390_INT_PFAULT_INIT:
1732 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001733 case KVM_S390_INT_VIRTIO:
1734 case KVM_S390_INT_SERVICE:
1735 target = (void *) &inti->ext;
1736 source = &uptr->u.ext;
1737 size = sizeof(inti->ext);
1738 break;
1739 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1740 target = (void *) &inti->io;
1741 source = &uptr->u.io;
1742 size = sizeof(inti->io);
1743 break;
1744 case KVM_S390_MCHK:
1745 target = (void *) &inti->mchk;
1746 source = &uptr->u.mchk;
1747 size = sizeof(inti->mchk);
1748 break;
1749 default:
1750 return -EINVAL;
1751 }
1752
1753 if (copy_from_user(target, source, size))
1754 return -EFAULT;
1755
1756 return 0;
1757}
1758
1759static int enqueue_floating_irq(struct kvm_device *dev,
1760 struct kvm_device_attr *attr)
1761{
1762 struct kvm_s390_interrupt_info *inti = NULL;
1763 int r = 0;
1764 int len = attr->attr;
1765
1766 if (len % sizeof(struct kvm_s390_irq) != 0)
1767 return -EINVAL;
1768 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1769 return -EINVAL;
1770
1771 while (len >= sizeof(struct kvm_s390_irq)) {
1772 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1773 if (!inti)
1774 return -ENOMEM;
1775
1776 r = copy_irq_from_user(inti, attr->addr);
1777 if (r) {
1778 kfree(inti);
1779 return r;
1780 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001781 r = __inject_vm(dev->kvm, inti);
1782 if (r) {
1783 kfree(inti);
1784 return r;
1785 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001786 len -= sizeof(struct kvm_s390_irq);
1787 attr->addr += sizeof(struct kvm_s390_irq);
1788 }
1789
1790 return r;
1791}
1792
Cornelia Huck841b91c2013-07-15 13:36:01 +02001793static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1794{
1795 if (id >= MAX_S390_IO_ADAPTERS)
1796 return NULL;
1797 return kvm->arch.adapters[id];
1798}
1799
1800static int register_io_adapter(struct kvm_device *dev,
1801 struct kvm_device_attr *attr)
1802{
1803 struct s390_io_adapter *adapter;
1804 struct kvm_s390_io_adapter adapter_info;
1805
1806 if (copy_from_user(&adapter_info,
1807 (void __user *)attr->addr, sizeof(adapter_info)))
1808 return -EFAULT;
1809
1810 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1811 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1812 return -EINVAL;
1813
1814 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1815 if (!adapter)
1816 return -ENOMEM;
1817
1818 INIT_LIST_HEAD(&adapter->maps);
1819 init_rwsem(&adapter->maps_lock);
1820 atomic_set(&adapter->nr_maps, 0);
1821 adapter->id = adapter_info.id;
1822 adapter->isc = adapter_info.isc;
1823 adapter->maskable = adapter_info.maskable;
1824 adapter->masked = false;
1825 adapter->swap = adapter_info.swap;
1826 dev->kvm->arch.adapters[adapter->id] = adapter;
1827
1828 return 0;
1829}
1830
1831int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1832{
1833 int ret;
1834 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1835
1836 if (!adapter || !adapter->maskable)
1837 return -EINVAL;
1838 ret = adapter->masked;
1839 adapter->masked = masked;
1840 return ret;
1841}
1842
1843static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1844{
1845 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1846 struct s390_map_info *map;
1847 int ret;
1848
1849 if (!adapter || !addr)
1850 return -EINVAL;
1851
1852 map = kzalloc(sizeof(*map), GFP_KERNEL);
1853 if (!map) {
1854 ret = -ENOMEM;
1855 goto out;
1856 }
1857 INIT_LIST_HEAD(&map->list);
1858 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02001859 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001860 if (map->addr == -EFAULT) {
1861 ret = -EFAULT;
1862 goto out;
1863 }
1864 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1865 if (ret < 0)
1866 goto out;
1867 BUG_ON(ret != 1);
1868 down_write(&adapter->maps_lock);
1869 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1870 list_add_tail(&map->list, &adapter->maps);
1871 ret = 0;
1872 } else {
1873 put_page(map->page);
1874 ret = -EINVAL;
1875 }
1876 up_write(&adapter->maps_lock);
1877out:
1878 if (ret)
1879 kfree(map);
1880 return ret;
1881}
1882
1883static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1884{
1885 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1886 struct s390_map_info *map, *tmp;
1887 int found = 0;
1888
1889 if (!adapter || !addr)
1890 return -EINVAL;
1891
1892 down_write(&adapter->maps_lock);
1893 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1894 if (map->guest_addr == addr) {
1895 found = 1;
1896 atomic_dec(&adapter->nr_maps);
1897 list_del(&map->list);
1898 put_page(map->page);
1899 kfree(map);
1900 break;
1901 }
1902 }
1903 up_write(&adapter->maps_lock);
1904
1905 return found ? 0 : -EINVAL;
1906}
1907
1908void kvm_s390_destroy_adapters(struct kvm *kvm)
1909{
1910 int i;
1911 struct s390_map_info *map, *tmp;
1912
1913 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1914 if (!kvm->arch.adapters[i])
1915 continue;
1916 list_for_each_entry_safe(map, tmp,
1917 &kvm->arch.adapters[i]->maps, list) {
1918 list_del(&map->list);
1919 put_page(map->page);
1920 kfree(map);
1921 }
1922 kfree(kvm->arch.adapters[i]);
1923 }
1924}
1925
1926static int modify_io_adapter(struct kvm_device *dev,
1927 struct kvm_device_attr *attr)
1928{
1929 struct kvm_s390_io_adapter_req req;
1930 struct s390_io_adapter *adapter;
1931 int ret;
1932
1933 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1934 return -EFAULT;
1935
1936 adapter = get_io_adapter(dev->kvm, req.id);
1937 if (!adapter)
1938 return -EINVAL;
1939 switch (req.type) {
1940 case KVM_S390_IO_ADAPTER_MASK:
1941 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1942 if (ret > 0)
1943 ret = 0;
1944 break;
1945 case KVM_S390_IO_ADAPTER_MAP:
1946 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1947 break;
1948 case KVM_S390_IO_ADAPTER_UNMAP:
1949 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1950 break;
1951 default:
1952 ret = -EINVAL;
1953 }
1954
1955 return ret;
1956}
1957
Jens Freimannc05c4182013-10-07 16:13:45 +02001958static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1959{
1960 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001961 unsigned int i;
1962 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001963
1964 switch (attr->group) {
1965 case KVM_DEV_FLIC_ENQUEUE:
1966 r = enqueue_floating_irq(dev, attr);
1967 break;
1968 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01001969 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001970 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001971 case KVM_DEV_FLIC_APF_ENABLE:
1972 dev->kvm->arch.gmap->pfault_enabled = 1;
1973 break;
1974 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1975 dev->kvm->arch.gmap->pfault_enabled = 0;
1976 /*
1977 * Make sure no async faults are in transition when
1978 * clearing the queues. So we don't need to worry
1979 * about late coming workers.
1980 */
1981 synchronize_srcu(&dev->kvm->srcu);
1982 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1983 kvm_clear_async_pf_completion_queue(vcpu);
1984 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001985 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1986 r = register_io_adapter(dev, attr);
1987 break;
1988 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1989 r = modify_io_adapter(dev, attr);
1990 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001991 default:
1992 r = -EINVAL;
1993 }
1994
1995 return r;
1996}
1997
1998static int flic_create(struct kvm_device *dev, u32 type)
1999{
2000 if (!dev)
2001 return -EINVAL;
2002 if (dev->kvm->arch.flic)
2003 return -EINVAL;
2004 dev->kvm->arch.flic = dev;
2005 return 0;
2006}
2007
2008static void flic_destroy(struct kvm_device *dev)
2009{
2010 dev->kvm->arch.flic = NULL;
2011 kfree(dev);
2012}
2013
2014/* s390 floating irq controller (flic) */
2015struct kvm_device_ops kvm_flic_ops = {
2016 .name = "kvm-flic",
2017 .get_attr = flic_get_attr,
2018 .set_attr = flic_set_attr,
2019 .create = flic_create,
2020 .destroy = flic_destroy,
2021};
Cornelia Huck84223592013-07-15 13:36:01 +02002022
2023static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2024{
2025 unsigned long bit;
2026
2027 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2028
2029 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2030}
2031
2032static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
2033 u64 addr)
2034{
2035 struct s390_map_info *map;
2036
2037 if (!adapter)
2038 return NULL;
2039
2040 list_for_each_entry(map, &adapter->maps, list) {
2041 if (map->guest_addr == addr)
2042 return map;
2043 }
2044 return NULL;
2045}
2046
2047static int adapter_indicators_set(struct kvm *kvm,
2048 struct s390_io_adapter *adapter,
2049 struct kvm_s390_adapter_int *adapter_int)
2050{
2051 unsigned long bit;
2052 int summary_set, idx;
2053 struct s390_map_info *info;
2054 void *map;
2055
2056 info = get_map_info(adapter, adapter_int->ind_addr);
2057 if (!info)
2058 return -1;
2059 map = page_address(info->page);
2060 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
2061 set_bit(bit, map);
2062 idx = srcu_read_lock(&kvm->srcu);
2063 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2064 set_page_dirty_lock(info->page);
2065 info = get_map_info(adapter, adapter_int->summary_addr);
2066 if (!info) {
2067 srcu_read_unlock(&kvm->srcu, idx);
2068 return -1;
2069 }
2070 map = page_address(info->page);
2071 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
2072 adapter->swap);
2073 summary_set = test_and_set_bit(bit, map);
2074 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
2075 set_page_dirty_lock(info->page);
2076 srcu_read_unlock(&kvm->srcu, idx);
2077 return summary_set ? 0 : 1;
2078}
2079
2080/*
2081 * < 0 - not injected due to error
2082 * = 0 - coalesced, summary indicator already active
2083 * > 0 - injected interrupt
2084 */
2085static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2086 struct kvm *kvm, int irq_source_id, int level,
2087 bool line_status)
2088{
2089 int ret;
2090 struct s390_io_adapter *adapter;
2091
2092 /* We're only interested in the 0->1 transition. */
2093 if (!level)
2094 return 0;
2095 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2096 if (!adapter)
2097 return -1;
2098 down_read(&adapter->maps_lock);
2099 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2100 up_read(&adapter->maps_lock);
2101 if ((ret > 0) && !adapter->masked) {
2102 struct kvm_s390_interrupt s390int = {
2103 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2104 .parm = 0,
2105 .parm64 = (adapter->isc << 27) | 0x80000000,
2106 };
2107 ret = kvm_s390_inject_vm(kvm, &s390int);
2108 if (ret == 0)
2109 ret = 1;
2110 }
2111 return ret;
2112}
2113
Paul Mackerras8ba918d2014-06-30 20:51:10 +10002114int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02002115 const struct kvm_irq_routing_entry *ue)
2116{
2117 int ret;
2118
2119 switch (ue->type) {
2120 case KVM_IRQ_ROUTING_S390_ADAPTER:
2121 e->set = set_adapter_int;
2122 e->adapter.summary_addr = ue->u.adapter.summary_addr;
2123 e->adapter.ind_addr = ue->u.adapter.ind_addr;
2124 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2125 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2126 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2127 ret = 0;
2128 break;
2129 default:
2130 ret = -EINVAL;
2131 }
2132
2133 return ret;
2134}
2135
2136int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2137 int irq_source_id, int level, bool line_status)
2138{
2139 return -EINVAL;
2140}
Jens Freimann816c7662014-11-24 17:13:46 +01002141
2142int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2143{
2144 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2145 struct kvm_s390_irq *buf;
2146 int r = 0;
2147 int n;
2148
2149 buf = vmalloc(len);
2150 if (!buf)
2151 return -ENOMEM;
2152
2153 if (copy_from_user((void *) buf, irqstate, len)) {
2154 r = -EFAULT;
2155 goto out_free;
2156 }
2157
2158 /*
2159 * Don't allow setting the interrupt state
2160 * when there are already interrupts pending
2161 */
2162 spin_lock(&li->lock);
2163 if (li->pending_irqs) {
2164 r = -EBUSY;
2165 goto out_unlock;
2166 }
2167
2168 for (n = 0; n < len / sizeof(*buf); n++) {
2169 r = do_inject_vcpu(vcpu, &buf[n]);
2170 if (r)
2171 break;
2172 }
2173
2174out_unlock:
2175 spin_unlock(&li->lock);
2176out_free:
2177 vfree(buf);
2178
2179 return r;
2180}
2181
2182static void store_local_irq(struct kvm_s390_local_interrupt *li,
2183 struct kvm_s390_irq *irq,
2184 unsigned long irq_type)
2185{
2186 switch (irq_type) {
2187 case IRQ_PEND_MCHK_EX:
2188 case IRQ_PEND_MCHK_REP:
2189 irq->type = KVM_S390_MCHK;
2190 irq->u.mchk = li->irq.mchk;
2191 break;
2192 case IRQ_PEND_PROG:
2193 irq->type = KVM_S390_PROGRAM_INT;
2194 irq->u.pgm = li->irq.pgm;
2195 break;
2196 case IRQ_PEND_PFAULT_INIT:
2197 irq->type = KVM_S390_INT_PFAULT_INIT;
2198 irq->u.ext = li->irq.ext;
2199 break;
2200 case IRQ_PEND_EXT_EXTERNAL:
2201 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2202 irq->u.extcall = li->irq.extcall;
2203 break;
2204 case IRQ_PEND_EXT_CLOCK_COMP:
2205 irq->type = KVM_S390_INT_CLOCK_COMP;
2206 break;
2207 case IRQ_PEND_EXT_CPU_TIMER:
2208 irq->type = KVM_S390_INT_CPU_TIMER;
2209 break;
2210 case IRQ_PEND_SIGP_STOP:
2211 irq->type = KVM_S390_SIGP_STOP;
2212 irq->u.stop = li->irq.stop;
2213 break;
2214 case IRQ_PEND_RESTART:
2215 irq->type = KVM_S390_RESTART;
2216 break;
2217 case IRQ_PEND_SET_PREFIX:
2218 irq->type = KVM_S390_SIGP_SET_PREFIX;
2219 irq->u.prefix = li->irq.prefix;
2220 break;
2221 }
2222}
2223
2224int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
2225{
2226 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
2227 unsigned long sigp_emerg_pending[BITS_TO_LONGS(KVM_MAX_VCPUS)];
2228 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2229 unsigned long pending_irqs;
2230 struct kvm_s390_irq irq;
2231 unsigned long irq_type;
2232 int cpuaddr;
2233 int n = 0;
2234
2235 spin_lock(&li->lock);
2236 pending_irqs = li->pending_irqs;
2237 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
2238 sizeof(sigp_emerg_pending));
2239 spin_unlock(&li->lock);
2240
2241 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
2242 memset(&irq, 0, sizeof(irq));
2243 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
2244 continue;
2245 if (n + sizeof(irq) > len)
2246 return -ENOBUFS;
2247 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
2248 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2249 return -EFAULT;
2250 n += sizeof(irq);
2251 }
2252
2253 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
2254 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
2255 memset(&irq, 0, sizeof(irq));
2256 if (n + sizeof(irq) > len)
2257 return -ENOBUFS;
2258 irq.type = KVM_S390_INT_EMERGENCY;
2259 irq.u.emerg.code = cpuaddr;
2260 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2261 return -EFAULT;
2262 n += sizeof(irq);
2263 }
2264 }
2265
2266 if ((sigp_ctrl & SIGP_CTRL_C) &&
2267 (atomic_read(&vcpu->arch.sie_block->cpuflags) &
2268 CPUSTAT_ECALL_PEND)) {
2269 if (n + sizeof(irq) > len)
2270 return -ENOBUFS;
2271 memset(&irq, 0, sizeof(irq));
2272 irq.type = KVM_S390_INT_EXTERNAL_CALL;
2273 irq.u.extcall.code = sigp_ctrl & SIGP_CTRL_SCN_MASK;
2274 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
2275 return -EFAULT;
2276 n += sizeof(irq);
2277 }
2278
2279 return n;
2280}