blob: 036d3757aca921eafde4426f7a3169566d7e2577 [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>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010020#include <asm/asm-offsets.h>
Thomas Huth33b412a2015-02-11 10:38:46 +010021#include <asm/dis.h>
Heiko Carstenscbb870c2010-02-26 22:37:43 +010022#include <asm/uaccess.h>
David Hildenbrandea5f4962014-10-14 15:29:30 +020023#include <asm/sclp.h>
Carsten Otteba5c1e92008-03-25 18:47:26 +010024#include "kvm-s390.h"
25#include "gaccess.h"
Cornelia Huckade38c32012-07-23 17:20:30 +020026#include "trace-s390.h"
Carsten Otteba5c1e92008-03-25 18:47:26 +010027
Cornelia Huckd8346b72012-12-20 15:32:08 +010028#define IOINT_SCHID_MASK 0x0000ffff
29#define IOINT_SSID_MASK 0x00030000
30#define IOINT_CSSID_MASK 0x03fc0000
31#define IOINT_AI_MASK 0x04000000
Jens Freimann44c6ca32014-04-16 13:57:18 +020032#define PFAULT_INIT 0x0600
Jens Freimann60f90a12014-11-10 17:20:07 +010033#define PFAULT_DONE 0x0680
34#define VIRTIO_PARAM 0x0d00
Cornelia Huckd8346b72012-12-20 15:32:08 +010035
36static int is_ioint(u64 type)
37{
38 return ((type & 0xfffe0000u) != 0xfffe0000u);
39}
40
Dominik Dingel3c038e62013-10-07 17:11:48 +020041int psw_extint_disabled(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +010042{
43 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
44}
45
Cornelia Huckd8346b72012-12-20 15:32:08 +010046static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
47{
48 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
49}
50
Cornelia Huck48a3e952012-12-20 15:32:09 +010051static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
52{
53 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
54}
55
Carsten Otteba5c1e92008-03-25 18:47:26 +010056static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
57{
58 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
59 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
60 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
61 return 0;
62 return 1;
63}
64
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010065static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
66{
67 if (psw_extint_disabled(vcpu) ||
68 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
69 return 0;
David Hildenbrandf71d0dc2014-03-18 10:06:14 +010070 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
71 /* No timer interrupts when single stepping */
72 return 0;
David Hildenbrandbb78c5e2014-03-18 10:03:26 +010073 return 1;
74}
75
Cornelia Huck79fd50c2013-02-07 13:20:52 +010076static u64 int_word_to_isc_bits(u32 int_word)
77{
78 u8 isc = (int_word & 0x38000000) >> 27;
79
80 return (0x80 >> isc) << 24;
81}
82
Christian Borntraeger614aeab2014-08-25 12:27:29 +020083static int __must_check __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +020084 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +010085{
86 switch (inti->type) {
Christian Ehrhardt7697e71f2011-10-18 12:27:15 +020087 case KVM_S390_INT_EXTERNAL_CALL:
88 if (psw_extint_disabled(vcpu))
89 return 0;
90 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
91 return 1;
Christian Borntraegerf3460262014-09-03 16:21:32 +020092 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +010093 case KVM_S390_INT_EMERGENCY:
94 if (psw_extint_disabled(vcpu))
95 return 0;
96 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
97 return 1;
98 return 0;
Thomas Huthe029ae52014-03-26 16:11:54 +010099 case KVM_S390_INT_CLOCK_COMP:
100 return ckc_interrupts_enabled(vcpu);
101 case KVM_S390_INT_CPU_TIMER:
102 if (psw_extint_disabled(vcpu))
103 return 0;
104 if (vcpu->arch.sie_block->gcr[0] & 0x400ul)
105 return 1;
106 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100107 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200108 case KVM_S390_INT_PFAULT_INIT:
109 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100110 case KVM_S390_INT_VIRTIO:
111 if (psw_extint_disabled(vcpu))
112 return 0;
113 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
114 return 1;
115 return 0;
116 case KVM_S390_PROGRAM_INT:
117 case KVM_S390_SIGP_STOP:
118 case KVM_S390_SIGP_SET_PREFIX:
119 case KVM_S390_RESTART:
120 return 1;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100121 case KVM_S390_MCHK:
122 if (psw_mchk_disabled(vcpu))
123 return 0;
124 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
125 return 1;
126 return 0;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100127 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
128 if (psw_ioint_disabled(vcpu))
129 return 0;
Cornelia Huck79fd50c2013-02-07 13:20:52 +0100130 if (vcpu->arch.sie_block->gcr[6] &
131 int_word_to_isc_bits(inti->io.io_int_word))
Cornelia Huckd8346b72012-12-20 15:32:08 +0100132 return 1;
133 return 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100134 default:
Cornelia Huckd8346b72012-12-20 15:32:08 +0100135 printk(KERN_WARNING "illegal interrupt type %llx\n",
136 inti->type);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100137 BUG();
138 }
139 return 0;
140}
141
Jens Freimann383d0b02014-07-29 15:11:49 +0200142static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu)
143{
144 return vcpu->arch.local_int.pending_irqs;
145}
146
147static unsigned long deliverable_local_irqs(struct kvm_vcpu *vcpu)
148{
149 unsigned long active_mask = pending_local_irqs(vcpu);
150
151 if (psw_extint_disabled(vcpu))
152 active_mask &= ~IRQ_PEND_EXT_MASK;
153 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
154 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
155 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
156 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
157 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
158 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
159 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
160 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
161 if (psw_mchk_disabled(vcpu))
162 active_mask &= ~IRQ_PEND_MCHK_MASK;
163
David Hildenbrand6cddd432014-10-15 16:48:53 +0200164 /*
165 * STOP irqs will never be actively delivered. They are triggered via
166 * intercept requests and cleared when the stop intercept is performed.
167 */
168 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
169
Jens Freimann383d0b02014-07-29 15:11:49 +0200170 return active_mask;
171}
172
Carsten Otteba5c1e92008-03-25 18:47:26 +0100173static void __set_cpu_idle(struct kvm_vcpu *vcpu)
174{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100175 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
176 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
177}
178
179static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
180{
Carsten Otteba5c1e92008-03-25 18:47:26 +0100181 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
182 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
183}
184
185static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
186{
David Hildenbrand49539192014-02-21 08:59:59 +0100187 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
188 &vcpu->arch.sie_block->cpuflags);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100189 vcpu->arch.sie_block->lctl = 0x0000;
David Hildenbrand27291e22014-01-23 12:26:52 +0100190 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
191
192 if (guestdbg_enabled(vcpu)) {
193 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
194 LCTL_CR10 | LCTL_CR11);
195 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
196 }
Carsten Otteba5c1e92008-03-25 18:47:26 +0100197}
198
199static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
200{
201 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
202}
203
Jens Freimann383d0b02014-07-29 15:11:49 +0200204static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
205{
206 if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK))
207 return;
208 if (psw_extint_disabled(vcpu))
209 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
210 else
211 vcpu->arch.sie_block->lctl |= LCTL_CR0;
212}
213
214static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
215{
216 if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
217 return;
218 if (psw_mchk_disabled(vcpu))
219 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
220 else
221 vcpu->arch.sie_block->lctl |= LCTL_CR14;
222}
223
David Hildenbrand6cddd432014-10-15 16:48:53 +0200224static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
225{
226 if (kvm_s390_is_stop_irq_pending(vcpu))
227 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
228}
229
Jens Freimann383d0b02014-07-29 15:11:49 +0200230/* Set interception request for non-deliverable local interrupts */
231static void set_intercept_indicators_local(struct kvm_vcpu *vcpu)
232{
233 set_intercept_indicators_ext(vcpu);
234 set_intercept_indicators_mchk(vcpu);
David Hildenbrand6cddd432014-10-15 16:48:53 +0200235 set_intercept_indicators_stop(vcpu);
Jens Freimann383d0b02014-07-29 15:11:49 +0200236}
237
Carsten Otteba5c1e92008-03-25 18:47:26 +0100238static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200239 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100240{
241 switch (inti->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100242 case KVM_S390_INT_SERVICE:
Dominik Dingel3c038e62013-10-07 17:11:48 +0200243 case KVM_S390_INT_PFAULT_DONE:
Carsten Otteba5c1e92008-03-25 18:47:26 +0100244 case KVM_S390_INT_VIRTIO:
245 if (psw_extint_disabled(vcpu))
246 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
247 else
248 vcpu->arch.sie_block->lctl |= LCTL_CR0;
249 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100250 case KVM_S390_MCHK:
251 if (psw_mchk_disabled(vcpu))
252 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
253 else
254 vcpu->arch.sie_block->lctl |= LCTL_CR14;
255 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100256 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
257 if (psw_ioint_disabled(vcpu))
258 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
259 else
260 vcpu->arch.sie_block->lctl |= LCTL_CR6;
261 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100262 default:
263 BUG();
264 }
265}
266
Jens Freimann8a2ef712014-07-23 16:36:06 +0200267static u16 get_ilc(struct kvm_vcpu *vcpu)
268{
Jens Freimann8a2ef712014-07-23 16:36:06 +0200269 switch (vcpu->arch.sie_block->icptcode) {
270 case ICPT_INST:
271 case ICPT_INSTPROGI:
272 case ICPT_OPEREXC:
273 case ICPT_PARTEXEC:
274 case ICPT_IOINST:
275 /* last instruction only stored for these icptcodes */
Thomas Huth33b412a2015-02-11 10:38:46 +0100276 return insn_length(vcpu->arch.sie_block->ipa >> 8);
Jens Freimann8a2ef712014-07-23 16:36:06 +0200277 case ICPT_PROGI:
278 return vcpu->arch.sie_block->pgmilc;
279 default:
280 return 0;
281 }
282}
283
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200284static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
David Hildenbrand87128362014-03-03 10:55:13 +0100285{
Jens Freimann383d0b02014-07-29 15:11:49 +0200286 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200287 int rc;
288
289 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
290 0, 0);
291
292 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
293 (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100294 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200295 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
296 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
297 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
298 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200299 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100300 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200301}
302
303static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
304{
Jens Freimann383d0b02014-07-29 15:11:49 +0200305 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200306 int rc;
307
308 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
309 0, 0);
310
311 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
312 (u16 __user *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100313 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200314 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
315 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
316 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
317 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200318 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100319 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200320}
321
Jens Freimann383d0b02014-07-29 15:11:49 +0200322static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200323{
Jens Freimann383d0b02014-07-29 15:11:49 +0200324 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
325 struct kvm_s390_ext_info ext;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200326 int rc;
327
Jens Freimann383d0b02014-07-29 15:11:49 +0200328 spin_lock(&li->lock);
329 ext = li->irq.ext;
330 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
331 li->irq.ext.ext_params2 = 0;
332 spin_unlock(&li->lock);
333
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200334 VCPU_EVENT(vcpu, 4, "interrupt: pfault init parm:%x,parm64:%llx",
Jens Freimann383d0b02014-07-29 15:11:49 +0200335 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200336 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
337 KVM_S390_INT_PFAULT_INIT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200338 0, ext.ext_params2);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200339
340 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
341 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
342 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
343 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
344 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
345 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200346 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100347 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200348}
349
Jens Freimann383d0b02014-07-29 15:11:49 +0200350static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200351{
Jens Freimann383d0b02014-07-29 15:11:49 +0200352 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
353 struct kvm_s390_mchk_info mchk;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200354 int rc;
355
Jens Freimann383d0b02014-07-29 15:11:49 +0200356 spin_lock(&li->lock);
357 mchk = li->irq.mchk;
358 /*
359 * If there was an exigent machine check pending, then any repressible
360 * machine checks that might have been pending are indicated along
361 * with it, so always clear both bits
362 */
363 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
364 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
365 memset(&li->irq.mchk, 0, sizeof(mchk));
366 spin_unlock(&li->lock);
367
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200368 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
Jens Freimann383d0b02014-07-29 15:11:49 +0200369 mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200370 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_MCHK,
Jens Freimann383d0b02014-07-29 15:11:49 +0200371 mchk.cr14, mchk.mcic);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200372
373 rc = kvm_s390_vcpu_store_status(vcpu, KVM_S390_STORE_STATUS_PREFIXED);
Jens Freimann383d0b02014-07-29 15:11:49 +0200374 rc |= put_guest_lc(vcpu, mchk.mcic,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200375 (u64 __user *) __LC_MCCK_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200376 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200377 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
378 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
Jens Freimann383d0b02014-07-29 15:11:49 +0200379 &mchk.fixed_logout, sizeof(mchk.fixed_logout));
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200380 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
381 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
382 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
383 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100384 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200385}
386
387static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
388{
Jens Freimann383d0b02014-07-29 15:11:49 +0200389 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200390 int rc;
391
392 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
393 vcpu->stat.deliver_restart_signal++;
394 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
395
396 rc = write_guest_lc(vcpu,
397 offsetof(struct _lowcore, restart_old_psw),
398 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
399 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
400 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann383d0b02014-07-29 15:11:49 +0200401 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann99e20002014-12-01 17:05:39 +0100402 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200403}
404
Jens Freimann383d0b02014-07-29 15:11:49 +0200405static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200406{
Jens Freimann383d0b02014-07-29 15:11:49 +0200407 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
408 struct kvm_s390_prefix_info prefix;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200409
Jens Freimann383d0b02014-07-29 15:11:49 +0200410 spin_lock(&li->lock);
411 prefix = li->irq.prefix;
412 li->irq.prefix.address = 0;
413 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
414 spin_unlock(&li->lock);
415
416 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200417 vcpu->stat.deliver_prefix_signal++;
418 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
419 KVM_S390_SIGP_SET_PREFIX,
Jens Freimann383d0b02014-07-29 15:11:49 +0200420 prefix.address, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200421
Jens Freimann383d0b02014-07-29 15:11:49 +0200422 kvm_s390_set_prefix(vcpu, prefix.address);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200423 return 0;
424}
425
Jens Freimann383d0b02014-07-29 15:11:49 +0200426static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200427{
Jens Freimann383d0b02014-07-29 15:11:49 +0200428 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200429 int rc;
Jens Freimann383d0b02014-07-29 15:11:49 +0200430 int cpu_addr;
431
432 spin_lock(&li->lock);
433 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
434 clear_bit(cpu_addr, li->sigp_emerg_pending);
435 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
436 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
437 spin_unlock(&li->lock);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200438
439 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
440 vcpu->stat.deliver_emergency_signal++;
Jens Freimann383d0b02014-07-29 15:11:49 +0200441 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
442 cpu_addr, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200443
444 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
445 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200446 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200447 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
448 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
449 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
450 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100451 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200452}
453
Jens Freimann383d0b02014-07-29 15:11:49 +0200454static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200455{
Jens Freimann383d0b02014-07-29 15:11:49 +0200456 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
457 struct kvm_s390_extcall_info extcall;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200458 int rc;
459
Jens Freimann383d0b02014-07-29 15:11:49 +0200460 spin_lock(&li->lock);
461 extcall = li->irq.extcall;
462 li->irq.extcall.code = 0;
463 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
464 spin_unlock(&li->lock);
465
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200466 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
467 vcpu->stat.deliver_external_call++;
468 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
469 KVM_S390_INT_EXTERNAL_CALL,
Jens Freimann383d0b02014-07-29 15:11:49 +0200470 extcall.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200471
472 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
473 (u16 *)__LC_EXT_INT_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200474 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200475 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
476 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
477 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
478 sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100479 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200480}
481
Jens Freimann383d0b02014-07-29 15:11:49 +0200482static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200483{
Jens Freimann383d0b02014-07-29 15:11:49 +0200484 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
485 struct kvm_s390_pgm_info pgm_info;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100486 int rc = 0, nullifying = false;
Jens Freimann8a2ef712014-07-23 16:36:06 +0200487 u16 ilc = get_ilc(vcpu);
David Hildenbrand87128362014-03-03 10:55:13 +0100488
Jens Freimann383d0b02014-07-29 15:11:49 +0200489 spin_lock(&li->lock);
490 pgm_info = li->irq.pgm;
491 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
492 memset(&li->irq.pgm, 0, sizeof(pgm_info));
493 spin_unlock(&li->lock);
494
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200495 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
Jens Freimann383d0b02014-07-29 15:11:49 +0200496 pgm_info.code, ilc);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200497 vcpu->stat.deliver_program_int++;
498 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
Jens Freimann383d0b02014-07-29 15:11:49 +0200499 pgm_info.code, 0);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200500
Jens Freimann383d0b02014-07-29 15:11:49 +0200501 switch (pgm_info.code & ~PGM_PER) {
David Hildenbrand87128362014-03-03 10:55:13 +0100502 case PGM_AFX_TRANSLATION:
503 case PGM_ASX_TRANSLATION:
504 case PGM_EX_TRANSLATION:
505 case PGM_LFX_TRANSLATION:
506 case PGM_LSTE_SEQUENCE:
507 case PGM_LSX_TRANSLATION:
508 case PGM_LX_TRANSLATION:
509 case PGM_PRIMARY_AUTHORITY:
510 case PGM_SECONDARY_AUTHORITY:
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100511 nullifying = true;
512 /* fall through */
David Hildenbrand87128362014-03-03 10:55:13 +0100513 case PGM_SPACE_SWITCH:
Jens Freimann383d0b02014-07-29 15:11:49 +0200514 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100515 (u64 *)__LC_TRANS_EXC_CODE);
516 break;
517 case PGM_ALEN_TRANSLATION:
518 case PGM_ALE_SEQUENCE:
519 case PGM_ASTE_INSTANCE:
520 case PGM_ASTE_SEQUENCE:
521 case PGM_ASTE_VALIDITY:
522 case PGM_EXTENDED_AUTHORITY:
Jens Freimann383d0b02014-07-29 15:11:49 +0200523 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100524 (u8 *)__LC_EXC_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100525 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100526 break;
527 case PGM_ASCE_TYPE:
528 case PGM_PAGE_TRANSLATION:
529 case PGM_REGION_FIRST_TRANS:
530 case PGM_REGION_SECOND_TRANS:
531 case PGM_REGION_THIRD_TRANS:
532 case PGM_SEGMENT_TRANSLATION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200533 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100534 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200535 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100536 (u8 *)__LC_EXC_ACCESS_ID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200537 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100538 (u8 *)__LC_OP_ACCESS_ID);
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100539 nullifying = true;
David Hildenbrand87128362014-03-03 10:55:13 +0100540 break;
541 case PGM_MONITOR:
Jens Freimann383d0b02014-07-29 15:11:49 +0200542 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
Thomas Hutha36c5392014-10-16 14:31:53 +0200543 (u16 *)__LC_MON_CLASS_NR);
Jens Freimann383d0b02014-07-29 15:11:49 +0200544 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100545 (u64 *)__LC_MON_CODE);
546 break;
Eric Farman403c8642015-02-02 15:01:06 -0500547 case PGM_VECTOR_PROCESSING:
David Hildenbrand87128362014-03-03 10:55:13 +0100548 case PGM_DATA:
Jens Freimann383d0b02014-07-29 15:11:49 +0200549 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100550 (u32 *)__LC_DATA_EXC_CODE);
551 break;
552 case PGM_PROTECTION:
Jens Freimann383d0b02014-07-29 15:11:49 +0200553 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100554 (u64 *)__LC_TRANS_EXC_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200555 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100556 (u8 *)__LC_EXC_ACCESS_ID);
557 break;
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100558 case PGM_STACK_FULL:
559 case PGM_STACK_EMPTY:
560 case PGM_STACK_SPECIFICATION:
561 case PGM_STACK_TYPE:
562 case PGM_STACK_OPERATION:
563 case PGM_TRACE_TABEL:
564 case PGM_CRYPTO_OPERATION:
565 nullifying = true;
566 break;
David Hildenbrand87128362014-03-03 10:55:13 +0100567 }
568
Jens Freimann383d0b02014-07-29 15:11:49 +0200569 if (pgm_info.code & PGM_PER) {
570 rc |= put_guest_lc(vcpu, pgm_info.per_code,
David Hildenbrand87128362014-03-03 10:55:13 +0100571 (u8 *) __LC_PER_CODE);
Jens Freimann383d0b02014-07-29 15:11:49 +0200572 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
David Hildenbrand87128362014-03-03 10:55:13 +0100573 (u8 *)__LC_PER_ATMID);
Jens Freimann383d0b02014-07-29 15:11:49 +0200574 rc |= put_guest_lc(vcpu, pgm_info.per_address,
David Hildenbrand87128362014-03-03 10:55:13 +0100575 (u64 *) __LC_PER_ADDRESS);
Jens Freimann383d0b02014-07-29 15:11:49 +0200576 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
David Hildenbrand87128362014-03-03 10:55:13 +0100577 (u8 *) __LC_PER_ACCESS_ID);
578 }
579
Thomas Hutha9a846fd2015-02-05 09:06:56 +0100580 if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
581 kvm_s390_rewind_psw(vcpu, ilc);
582
Jens Freimann8a2ef712014-07-23 16:36:06 +0200583 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
Jens Freimann383d0b02014-07-29 15:11:49 +0200584 rc |= put_guest_lc(vcpu, pgm_info.code,
David Hildenbrand87128362014-03-03 10:55:13 +0100585 (u16 *)__LC_PGM_INT_CODE);
586 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
587 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
588 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
589 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100590 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200591}
David Hildenbrand87128362014-03-03 10:55:13 +0100592
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200593static int __must_check __deliver_service(struct kvm_vcpu *vcpu,
594 struct kvm_s390_interrupt_info *inti)
595{
596 int rc;
597
598 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
599 inti->ext.ext_params);
600 vcpu->stat.deliver_service_signal++;
601 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
602 inti->ext.ext_params, 0);
603
604 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
David Hildenbrand467fc292014-12-01 12:02:44 +0100605 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200606 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
607 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
608 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
609 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
610 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
611 (u32 *)__LC_EXT_PARAMS);
Jens Freimann99e20002014-12-01 17:05:39 +0100612 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200613}
614
615static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu,
616 struct kvm_s390_interrupt_info *inti)
617{
618 int rc;
619
620 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
621 KVM_S390_INT_PFAULT_DONE, 0,
622 inti->ext.ext_params2);
623
624 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *)__LC_EXT_INT_CODE);
625 rc |= put_guest_lc(vcpu, PFAULT_DONE, (u16 *)__LC_EXT_CPU_ADDR);
626 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
627 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
628 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
629 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
630 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
631 (u64 *)__LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100632 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200633}
634
635static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu,
636 struct kvm_s390_interrupt_info *inti)
637{
638 int rc;
639
640 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
641 inti->ext.ext_params, inti->ext.ext_params2);
642 vcpu->stat.deliver_virtio_interrupt++;
643 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
644 inti->ext.ext_params,
645 inti->ext.ext_params2);
646
647 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *)__LC_EXT_INT_CODE);
648 rc |= put_guest_lc(vcpu, VIRTIO_PARAM, (u16 *)__LC_EXT_CPU_ADDR);
649 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
650 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
651 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
652 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
653 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
654 (u32 *)__LC_EXT_PARAMS);
655 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
656 (u64 *)__LC_EXT_PARAMS2);
Jens Freimann99e20002014-12-01 17:05:39 +0100657 return rc ? -EFAULT : 0;
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200658}
659
660static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
661 struct kvm_s390_interrupt_info *inti)
662{
663 int rc;
664
665 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
666 vcpu->stat.deliver_io_int++;
667 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
668 ((__u32)inti->io.subchannel_id << 16) |
669 inti->io.subchannel_nr,
670 ((__u64)inti->io.io_int_parm << 32) |
671 inti->io.io_int_word);
672
673 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
674 (u16 *)__LC_SUBCHANNEL_ID);
675 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
676 (u16 *)__LC_SUBCHANNEL_NR);
677 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
678 (u32 *)__LC_IO_INT_PARM);
679 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
680 (u32 *)__LC_IO_INT_WORD);
681 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
682 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
683 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
684 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100685 return rc ? -EFAULT : 0;
David Hildenbrand87128362014-03-03 10:55:13 +0100686}
687
Jens Freimann383d0b02014-07-29 15:11:49 +0200688static int __must_check __deliver_mchk_floating(struct kvm_vcpu *vcpu,
689 struct kvm_s390_interrupt_info *inti)
690{
691 struct kvm_s390_mchk_info *mchk = &inti->mchk;
692 int rc;
693
694 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
695 mchk->mcic);
696 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_MCHK,
697 mchk->cr14, mchk->mcic);
698
699 rc = kvm_s390_vcpu_store_status(vcpu, KVM_S390_STORE_STATUS_PREFIXED);
700 rc |= put_guest_lc(vcpu, mchk->mcic,
701 (u64 __user *) __LC_MCCK_CODE);
702 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
703 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
704 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
705 &mchk->fixed_logout, sizeof(mchk->fixed_logout));
706 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
707 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
708 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
709 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
Jens Freimann99e20002014-12-01 17:05:39 +0100710 return rc ? -EFAULT : 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200711}
712
713typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
714
715static const deliver_irq_t deliver_irq_funcs[] = {
716 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
717 [IRQ_PEND_PROG] = __deliver_prog,
718 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
719 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
720 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
721 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
722 [IRQ_PEND_RESTART] = __deliver_restart,
Jens Freimann383d0b02014-07-29 15:11:49 +0200723 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
724 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
725};
726
727static int __must_check __deliver_floating_interrupt(struct kvm_vcpu *vcpu,
728 struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100729{
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200730 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100731
732 switch (inti->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100733 case KVM_S390_INT_SERVICE:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200734 rc = __deliver_service(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100735 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +0200736 case KVM_S390_INT_PFAULT_DONE:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200737 rc = __deliver_pfault_done(vcpu, inti);
Dominik Dingel3c038e62013-10-07 17:11:48 +0200738 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100739 case KVM_S390_INT_VIRTIO:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200740 rc = __deliver_virtio(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100741 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +0100742 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +0200743 rc = __deliver_mchk_floating(vcpu, inti);
Cornelia Huck48a3e952012-12-20 15:32:09 +0100744 break;
Cornelia Huckd8346b72012-12-20 15:32:08 +0100745 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Jens Freimann0fb97ab2014-07-29 13:45:21 +0200746 rc = __deliver_io(vcpu, inti);
Cornelia Huckd8346b72012-12-20 15:32:08 +0100747 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100748 default:
749 BUG();
750 }
Jens Freimann79395032014-04-17 10:10:30 +0200751
752 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100753}
754
David Hildenbrandea5f4962014-10-14 15:29:30 +0200755/* Check whether an external call is pending (deliverable or not) */
756int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
David Hildenbrand49539192014-02-21 08:59:59 +0100757{
David Hildenbrandea5f4962014-10-14 15:29:30 +0200758 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
759 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
David Hildenbrand49539192014-02-21 08:59:59 +0100760
David Hildenbrandea5f4962014-10-14 15:29:30 +0200761 if (!sclp_has_sigpif())
762 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
David Hildenbrand49539192014-02-21 08:59:59 +0100763
David Hildenbrandea5f4962014-10-14 15:29:30 +0200764 return (sigp_ctrl & SIGP_CTRL_C) &&
765 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
David Hildenbrand49539192014-02-21 08:59:59 +0100766}
767
David Hildenbrand9a022062014-08-05 17:40:47 +0200768int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100769{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200770 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
771 struct kvm_s390_interrupt_info *inti;
Jens Freimann383d0b02014-07-29 15:11:49 +0200772 int rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100773
Jens Freimann383d0b02014-07-29 15:11:49 +0200774 rc = !!deliverable_local_irqs(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100775
776 if ((!rc) && atomic_read(&fi->active)) {
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200777 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100778 list_for_each_entry(inti, &fi->list, list)
779 if (__interrupt_is_deliverable(vcpu, inti)) {
780 rc = 1;
781 break;
782 }
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200783 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100784 }
785
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100786 if (!rc && kvm_cpu_has_pending_timer(vcpu))
787 rc = 1;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100788
David Hildenbrandea5f4962014-10-14 15:29:30 +0200789 /* external call pending and deliverable */
790 if (!rc && kvm_s390_ext_call_pending(vcpu) &&
791 !psw_extint_disabled(vcpu) &&
792 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
David Hildenbrand49539192014-02-21 08:59:59 +0100793 rc = 1;
794
David Hildenbrand9a022062014-08-05 17:40:47 +0200795 if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
David Hildenbrand6cddd432014-10-15 16:48:53 +0200796 rc = 1;
797
Carsten Otteba5c1e92008-03-25 18:47:26 +0100798 return rc;
799}
800
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300801int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
802{
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100803 if (!(vcpu->arch.sie_block->ckc <
804 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
805 return 0;
806 if (!ckc_interrupts_enabled(vcpu))
807 return 0;
808 return 1;
Marcelo Tosatti3d808402008-04-11 14:53:26 -0300809}
810
Carsten Otteba5c1e92008-03-25 18:47:26 +0100811int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
812{
813 u64 now, sltime;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100814
815 vcpu->stat.exit_wait_state++;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100816
David Hildenbrand0759d062014-05-13 16:54:32 +0200817 /* fast path */
818 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
819 return 0;
Carsten Ottee52b2af2008-05-21 13:37:44 +0200820
Carsten Otteba5c1e92008-03-25 18:47:26 +0100821 if (psw_interrupts_disabled(vcpu)) {
822 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
Heiko Carstensb8e660b2010-02-26 22:37:41 +0100823 return -EOPNOTSUPP; /* disabled wait */
Carsten Otteba5c1e92008-03-25 18:47:26 +0100824 }
825
David Hildenbrandbb78c5e2014-03-18 10:03:26 +0100826 if (!ckc_interrupts_enabled(vcpu)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100827 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
David Hildenbrandbda343e2014-12-12 12:26:40 +0100828 __set_cpu_idle(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100829 goto no_timer;
830 }
831
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200832 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
Heiko Carstensed4f2092013-01-14 16:55:55 +0100833 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
David Hildenbrandbda343e2014-12-12 12:26:40 +0100834
835 /* underflow */
836 if (vcpu->arch.sie_block->ckc < now)
837 return 0;
838
839 __set_cpu_idle(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200840 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
841 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100842no_timer:
Thomas Huth800c1062013-09-12 10:33:45 +0200843 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
David Hildenbrand0759d062014-05-13 16:54:32 +0200844 kvm_vcpu_block(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100845 __unset_cpu_idle(vcpu);
Thomas Huth800c1062013-09-12 10:33:45 +0200846 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
847
David Hildenbrand2d00f752014-12-11 10:18:01 +0100848 hrtimer_cancel(&vcpu->arch.ckc_timer);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100849 return 0;
850}
851
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200852void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
853{
854 if (waitqueue_active(&vcpu->wq)) {
855 /*
856 * The vcpu gave up the cpu voluntarily, mark it as a good
857 * yield-candidate.
858 */
859 vcpu->preempted = true;
860 wake_up_interruptible(&vcpu->wq);
David Hildenbrandce2e4f02014-07-11 10:00:43 +0200861 vcpu->stat.halt_wakeup++;
David Hildenbrand0e9c85a2014-05-16 11:59:46 +0200862 }
863}
864
Christian Borntraegerca872302009-05-12 17:21:49 +0200865enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
866{
867 struct kvm_vcpu *vcpu;
David Hildenbrand2d00f752014-12-11 10:18:01 +0100868 u64 now, sltime;
Christian Borntraegerca872302009-05-12 17:21:49 +0200869
870 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
David Hildenbrand2d00f752014-12-11 10:18:01 +0100871 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
872 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
Christian Borntraegerca872302009-05-12 17:21:49 +0200873
David Hildenbrand2d00f752014-12-11 10:18:01 +0100874 /*
875 * If the monotonic clock runs faster than the tod clock we might be
876 * woken up too early and have to go back to sleep to avoid deadlocks.
877 */
878 if (vcpu->arch.sie_block->ckc > now &&
879 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
880 return HRTIMER_RESTART;
881 kvm_s390_vcpu_wakeup(vcpu);
Christian Borntraegerca872302009-05-12 17:21:49 +0200882 return HRTIMER_NORESTART;
883}
Carsten Otteba5c1e92008-03-25 18:47:26 +0100884
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100885void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
886{
887 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100888
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200889 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200890 li->pending_irqs = 0;
891 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
892 memset(&li->irq, 0, sizeof(li->irq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200893 spin_unlock(&li->lock);
David Hildenbrand49539192014-02-21 08:59:59 +0100894
895 /* clear pending external calls set by sigp interpretation facility */
Jens Freimann383d0b02014-07-29 15:11:49 +0200896 atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags);
David Hildenbrandea5f4962014-10-14 15:29:30 +0200897 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
Jens Freimann2ed10cc2014-02-11 13:48:07 +0100898}
899
Christian Borntraeger614aeab2014-08-25 12:27:29 +0200900int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
Carsten Otteba5c1e92008-03-25 18:47:26 +0100901{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200902 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
903 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
904 struct kvm_s390_interrupt_info *n, *inti = NULL;
Jens Freimann383d0b02014-07-29 15:11:49 +0200905 deliver_irq_t func;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100906 int deliver;
Jens Freimann79395032014-04-17 10:10:30 +0200907 int rc = 0;
Jens Freimann383d0b02014-07-29 15:11:49 +0200908 unsigned long irq_type;
909 unsigned long deliverable_irqs;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100910
911 __reset_intercept_indicators(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100912
Jens Freimann383d0b02014-07-29 15:11:49 +0200913 /* pending ckc conditions might have been invalidated */
914 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
915 if (kvm_cpu_has_pending_timer(vcpu))
916 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
917
918 do {
919 deliverable_irqs = deliverable_local_irqs(vcpu);
920 /* bits are in the order of interrupt priority */
921 irq_type = find_first_bit(&deliverable_irqs, IRQ_PEND_COUNT);
922 if (irq_type == IRQ_PEND_COUNT)
923 break;
924 func = deliver_irq_funcs[irq_type];
925 if (!func) {
926 WARN_ON_ONCE(func == NULL);
927 clear_bit(irq_type, &li->pending_irqs);
928 continue;
929 }
930 rc = func(vcpu);
931 } while (!rc && irq_type != IRQ_PEND_COUNT);
932
933 set_intercept_indicators_local(vcpu);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100934
Jens Freimann79395032014-04-17 10:10:30 +0200935 if (!rc && atomic_read(&fi->active)) {
Carsten Otteba5c1e92008-03-25 18:47:26 +0100936 do {
937 deliver = 0;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200938 spin_lock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100939 list_for_each_entry_safe(inti, n, &fi->list, list) {
940 if (__interrupt_is_deliverable(vcpu, inti)) {
941 list_del(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +0100942 fi->irq_count--;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100943 deliver = 1;
944 break;
945 }
946 __set_intercept_indicator(vcpu, inti);
947 }
948 if (list_empty(&fi->list))
949 atomic_set(&fi->active, 0);
Christian Borntraegerb037a4f2009-05-12 17:21:50 +0200950 spin_unlock(&fi->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100951 if (deliver) {
Jens Freimann383d0b02014-07-29 15:11:49 +0200952 rc = __deliver_floating_interrupt(vcpu, inti);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100953 kfree(inti);
954 }
Jens Freimann79395032014-04-17 10:10:30 +0200955 } while (!rc && deliver);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100956 }
Jens Freimann79395032014-04-17 10:10:30 +0200957
958 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100959}
960
Jens Freimann383d0b02014-07-29 15:11:49 +0200961static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +0200962{
963 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
964
Jens Freimann383d0b02014-07-29 15:11:49 +0200965 li->irq.pgm = irq->u.pgm;
Jens Freimann91851242014-12-01 16:43:40 +0100966 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +0200967 return 0;
968}
969
Carsten Otteba5c1e92008-03-25 18:47:26 +0100970int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
971{
Christian Borntraeger180c12f2008-06-27 15:05:40 +0200972 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200973 struct kvm_s390_irq irq;
Carsten Otteba5c1e92008-03-25 18:47:26 +0100974
975 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
Jens Freimann383d0b02014-07-29 15:11:49 +0200976 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, code,
977 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200978 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200979 irq.u.pgm.code = code;
980 __inject_prog(vcpu, &irq);
Christian Borntraegerd0321a22013-06-12 13:54:55 +0200981 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200982 spin_unlock(&li->lock);
Carsten Otteba5c1e92008-03-25 18:47:26 +0100983 return 0;
984}
985
Jens Freimannbcd84682014-02-11 11:07:05 +0100986int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
987 struct kvm_s390_pgm_info *pgm_info)
988{
989 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +0200990 struct kvm_s390_irq irq;
Jens Freimann0146a7b2014-07-28 15:37:58 +0200991 int rc;
Jens Freimannbcd84682014-02-11 11:07:05 +0100992
Jens Freimannbcd84682014-02-11 11:07:05 +0100993 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
994 pgm_info->code);
995 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
996 pgm_info->code, 0, 1);
David Hildenbrand4ae3c082014-05-16 10:23:53 +0200997 spin_lock(&li->lock);
Jens Freimann383d0b02014-07-29 15:11:49 +0200998 irq.u.pgm = *pgm_info;
999 rc = __inject_prog(vcpu, &irq);
Jens Freimannbcd84682014-02-11 11:07:05 +01001000 BUG_ON(waitqueue_active(li->wq));
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001001 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001002 return rc;
1003}
1004
Jens Freimann383d0b02014-07-29 15:11:49 +02001005static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001006{
1007 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1008
Jens Freimann383d0b02014-07-29 15:11:49 +02001009 VCPU_EVENT(vcpu, 3, "inject: external irq params:%x, params2:%llx",
1010 irq->u.ext.ext_params, irq->u.ext.ext_params2);
1011 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1012 irq->u.ext.ext_params,
1013 irq->u.ext.ext_params2, 2);
1014
1015 li->irq.ext = irq->u.ext;
1016 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001017 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1018 return 0;
1019}
1020
David Hildenbrandea5f4962014-10-14 15:29:30 +02001021static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
1022{
1023 unsigned char new_val, old_val;
1024 uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
1025
1026 new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
1027 old_val = *sigp_ctrl & ~SIGP_CTRL_C;
1028 if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
1029 /* another external call is pending */
1030 return -EBUSY;
1031 }
1032 atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
1033 return 0;
1034}
1035
Christian Borntraeger0675d922015-01-15 12:40:42 +01001036static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001037{
1038 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001039 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
David Hildenbrandea5f4962014-10-14 15:29:30 +02001040 uint16_t src_id = irq->u.extcall.code;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001041
1042 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
David Hildenbrandea5f4962014-10-14 15:29:30 +02001043 src_id);
Jens Freimann383d0b02014-07-29 15:11:49 +02001044 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
David Hildenbrandea5f4962014-10-14 15:29:30 +02001045 src_id, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001046
David Hildenbrandea5f4962014-10-14 15:29:30 +02001047 /* sending vcpu invalid */
1048 if (src_id >= KVM_MAX_VCPUS ||
1049 kvm_get_vcpu(vcpu->kvm, src_id) == NULL)
1050 return -EINVAL;
1051
1052 if (sclp_has_sigpif())
1053 return __inject_extcall_sigpif(vcpu, src_id);
1054
1055 if (!test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1056 return -EBUSY;
Jens Freimann383d0b02014-07-29 15:11:49 +02001057 *extcall = irq->u.extcall;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001058 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1059 return 0;
1060}
1061
Jens Freimann383d0b02014-07-29 15:11:49 +02001062static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001063{
1064 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001065 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001066
1067 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001068 irq->u.prefix.address);
Jens Freimann383d0b02014-07-29 15:11:49 +02001069 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
Jens Freimann556cc0d2014-12-18 15:52:21 +01001070 irq->u.prefix.address, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001071
David Hildenbranda3a9c592014-10-14 09:44:55 +02001072 if (!is_vcpu_stopped(vcpu))
1073 return -EBUSY;
1074
Jens Freimann383d0b02014-07-29 15:11:49 +02001075 *prefix = irq->u.prefix;
1076 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001077 return 0;
1078}
1079
David Hildenbrand6cddd432014-10-15 16:48:53 +02001080#define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
Jens Freimann383d0b02014-07-29 15:11:49 +02001081static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001082{
1083 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
David Hildenbrand28225452014-10-15 16:48:16 +02001084 struct kvm_s390_stop_info *stop = &li->irq.stop;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001085 int rc = 0;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001086
Jens Freimann383d0b02014-07-29 15:11:49 +02001087 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0, 2);
1088
David Hildenbrand28225452014-10-15 16:48:16 +02001089 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1090 return -EINVAL;
1091
David Hildenbrand6cddd432014-10-15 16:48:53 +02001092 if (is_vcpu_stopped(vcpu)) {
1093 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1094 rc = kvm_s390_store_status_unloaded(vcpu,
1095 KVM_S390_STORE_STATUS_NOADDR);
1096 return rc;
1097 }
1098
1099 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1100 return -EBUSY;
David Hildenbrand28225452014-10-15 16:48:16 +02001101 stop->flags = irq->u.stop.flags;
David Hildenbrand6cddd432014-10-15 16:48:53 +02001102 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001103 return 0;
1104}
1105
1106static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001107 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001108{
1109 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1110
Jens Freimann383d0b02014-07-29 15:11:49 +02001111 VCPU_EVENT(vcpu, 3, "inject: restart type %llx", irq->type);
1112 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0, 2);
1113
1114 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001115 return 0;
1116}
1117
1118static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
Jens Freimann383d0b02014-07-29 15:11:49 +02001119 struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001120{
1121 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1122
Jens Freimann383d0b02014-07-29 15:11:49 +02001123 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n",
1124 irq->u.emerg.code);
1125 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
Jens Freimann49538d12014-12-18 15:48:14 +01001126 irq->u.emerg.code, 0, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001127
Jens Freimann49538d12014-12-18 15:48:14 +01001128 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
Jens Freimann383d0b02014-07-29 15:11:49 +02001129 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001130 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1131 return 0;
1132}
1133
Jens Freimann383d0b02014-07-29 15:11:49 +02001134static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001135{
1136 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
Jens Freimann383d0b02014-07-29 15:11:49 +02001137 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
Jens Freimann0146a7b2014-07-28 15:37:58 +02001138
1139 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
Jens Freimann556cc0d2014-12-18 15:52:21 +01001140 irq->u.mchk.mcic);
Jens Freimann383d0b02014-07-29 15:11:49 +02001141 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
Jens Freimann556cc0d2014-12-18 15:52:21 +01001142 irq->u.mchk.mcic, 2);
Jens Freimann383d0b02014-07-29 15:11:49 +02001143
1144 /*
Jens Freimannfc2020c2014-08-13 10:09:04 +02001145 * Because repressible machine checks can be indicated along with
1146 * exigent machine checks (PoP, Chapter 11, Interruption action)
1147 * we need to combine cr14, mcic and external damage code.
1148 * Failing storage address and the logout area should not be or'ed
1149 * together, we just indicate the last occurrence of the corresponding
1150 * machine check
Jens Freimann383d0b02014-07-29 15:11:49 +02001151 */
Jens Freimannfc2020c2014-08-13 10:09:04 +02001152 mchk->cr14 |= irq->u.mchk.cr14;
Jens Freimann383d0b02014-07-29 15:11:49 +02001153 mchk->mcic |= irq->u.mchk.mcic;
Jens Freimannfc2020c2014-08-13 10:09:04 +02001154 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1155 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1156 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1157 sizeof(mchk->fixed_logout));
Jens Freimann383d0b02014-07-29 15:11:49 +02001158 if (mchk->mcic & MCHK_EX_MASK)
1159 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1160 else if (mchk->mcic & MCHK_REP_MASK)
1161 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001162 return 0;
1163}
1164
Jens Freimann383d0b02014-07-29 15:11:49 +02001165static int __inject_ckc(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001166{
1167 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1168
Jens Freimann383d0b02014-07-29 15:11:49 +02001169 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CLOCK_COMP);
1170 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1171 0, 0, 2);
1172
1173 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001174 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1175 return 0;
1176}
1177
Jens Freimann383d0b02014-07-29 15:11:49 +02001178static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
Jens Freimann0146a7b2014-07-28 15:37:58 +02001179{
1180 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1181
Jens Freimann383d0b02014-07-29 15:11:49 +02001182 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CPU_TIMER);
1183 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1184 0, 0, 2);
1185
1186 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001187 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
Jens Freimannbcd84682014-02-11 11:07:05 +01001188 return 0;
1189}
1190
Jens Freimann383d0b02014-07-29 15:11:49 +02001191
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001192struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1193 u64 cr6, u64 schid)
1194{
1195 struct kvm_s390_float_interrupt *fi;
1196 struct kvm_s390_interrupt_info *inti, *iter;
1197
1198 if ((!schid && !cr6) || (schid && cr6))
1199 return NULL;
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001200 fi = &kvm->arch.float_int;
1201 spin_lock(&fi->lock);
1202 inti = NULL;
1203 list_for_each_entry(iter, &fi->list, list) {
1204 if (!is_ioint(iter->type))
1205 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001206 if (cr6 &&
1207 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001208 continue;
1209 if (schid) {
1210 if (((schid & 0x00000000ffff0000) >> 16) !=
1211 iter->io.subchannel_id)
1212 continue;
1213 if ((schid & 0x000000000000ffff) !=
1214 iter->io.subchannel_nr)
1215 continue;
1216 }
1217 inti = iter;
1218 break;
1219 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001220 if (inti) {
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001221 list_del_init(&inti->list);
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001222 fi->irq_count--;
1223 }
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001224 if (list_empty(&fi->list))
1225 atomic_set(&fi->active, 0);
1226 spin_unlock(&fi->lock);
Cornelia Huckfa6b7fe2012-12-20 15:32:12 +01001227 return inti;
1228}
1229
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001230static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001231{
Christian Borntraeger180c12f2008-06-27 15:05:40 +02001232 struct kvm_s390_local_interrupt *li;
1233 struct kvm_s390_float_interrupt *fi;
Jens Freimannc05c4182013-10-07 16:13:45 +02001234 struct kvm_s390_interrupt_info *iter;
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001235 struct kvm_vcpu *dst_vcpu = NULL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001236 int sigcpu;
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001237 int rc = 0;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001238
Carsten Otteba5c1e92008-03-25 18:47:26 +01001239 fi = &kvm->arch.float_int;
Christian Borntraegerb037a4f2009-05-12 17:21:50 +02001240 spin_lock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001241 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
1242 rc = -EINVAL;
1243 goto unlock_fi;
1244 }
1245 fi->irq_count++;
Jens Freimannc05c4182013-10-07 16:13:45 +02001246 if (!is_ioint(inti->type)) {
Cornelia Huckd8346b72012-12-20 15:32:08 +01001247 list_add_tail(&inti->list, &fi->list);
Jens Freimannc05c4182013-10-07 16:13:45 +02001248 } else {
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001249 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
1250
Cornelia Huckd8346b72012-12-20 15:32:08 +01001251 /* Keep I/O interrupts sorted in isc order. */
1252 list_for_each_entry(iter, &fi->list, list) {
1253 if (!is_ioint(iter->type))
1254 continue;
Cornelia Huck79fd50c2013-02-07 13:20:52 +01001255 if (int_word_to_isc_bits(iter->io.io_int_word)
1256 <= isc_bits)
Cornelia Huckd8346b72012-12-20 15:32:08 +01001257 continue;
1258 break;
1259 }
1260 list_add_tail(&inti->list, &iter->list);
1261 }
Carsten Otteba5c1e92008-03-25 18:47:26 +01001262 atomic_set(&fi->active, 1);
David Hildenbrand8e2207c2015-01-15 17:56:18 +01001263 if (atomic_read(&kvm->online_vcpus) == 0)
1264 goto unlock_fi;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001265 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
1266 if (sigcpu == KVM_MAX_VCPUS) {
1267 do {
1268 sigcpu = fi->next_rr_cpu++;
1269 if (sigcpu == KVM_MAX_VCPUS)
1270 sigcpu = fi->next_rr_cpu = 0;
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001271 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
Carsten Otteba5c1e92008-03-25 18:47:26 +01001272 }
Jens Freimann1ee0bc52014-02-25 15:36:45 +01001273 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1274 li = &dst_vcpu->arch.local_int;
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001275 spin_lock(&li->lock);
Christian Borntraegerda00fcb2014-11-21 09:38:12 +01001276 switch (inti->type) {
1277 case KVM_S390_MCHK:
1278 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
1279 break;
1280 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1281 atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags);
1282 break;
1283 default:
1284 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1285 break;
1286 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001287 spin_unlock(&li->lock);
David Hildenbrand0e9c85a2014-05-16 11:59:46 +02001288 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001289unlock_fi:
Christian Borntraegerb037a4f2009-05-12 17:21:50 +02001290 spin_unlock(&fi->lock);
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001291 return rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001292}
1293
1294int kvm_s390_inject_vm(struct kvm *kvm,
1295 struct kvm_s390_interrupt *s390int)
1296{
1297 struct kvm_s390_interrupt_info *inti;
David Hildenbrand428d53b2015-01-16 12:58:09 +01001298 int rc;
Jens Freimannc05c4182013-10-07 16:13:45 +02001299
1300 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1301 if (!inti)
1302 return -ENOMEM;
1303
1304 inti->type = s390int->type;
1305 switch (inti->type) {
1306 case KVM_S390_INT_VIRTIO:
1307 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1308 s390int->parm, s390int->parm64);
1309 inti->ext.ext_params = s390int->parm;
1310 inti->ext.ext_params2 = s390int->parm64;
1311 break;
1312 case KVM_S390_INT_SERVICE:
1313 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
1314 inti->ext.ext_params = s390int->parm;
1315 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001316 case KVM_S390_INT_PFAULT_DONE:
Dominik Dingel3c038e62013-10-07 17:11:48 +02001317 inti->ext.ext_params2 = s390int->parm64;
1318 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001319 case KVM_S390_MCHK:
1320 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
1321 s390int->parm64);
1322 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1323 inti->mchk.mcic = s390int->parm64;
1324 break;
1325 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1326 if (inti->type & IOINT_AI_MASK)
1327 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1328 else
1329 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1330 s390int->type & IOINT_CSSID_MASK,
1331 s390int->type & IOINT_SSID_MASK,
1332 s390int->type & IOINT_SCHID_MASK);
1333 inti->io.subchannel_id = s390int->parm >> 16;
1334 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1335 inti->io.io_int_parm = s390int->parm64 >> 32;
1336 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1337 break;
1338 default:
1339 kfree(inti);
1340 return -EINVAL;
1341 }
1342 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1343 2);
1344
David Hildenbrand428d53b2015-01-16 12:58:09 +01001345 rc = __inject_vm(kvm, inti);
1346 if (rc)
1347 kfree(inti);
1348 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001349}
1350
David Hildenbrand15462e32015-02-04 15:59:11 +01001351int kvm_s390_reinject_io_int(struct kvm *kvm,
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001352 struct kvm_s390_interrupt_info *inti)
1353{
David Hildenbrand15462e32015-02-04 15:59:11 +01001354 return __inject_vm(kvm, inti);
Cornelia Huck2f32d4e2014-01-08 18:07:54 +01001355}
1356
Jens Freimann383d0b02014-07-29 15:11:49 +02001357int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1358 struct kvm_s390_irq *irq)
Carsten Otteba5c1e92008-03-25 18:47:26 +01001359{
Jens Freimann383d0b02014-07-29 15:11:49 +02001360 irq->type = s390int->type;
1361 switch (irq->type) {
Carsten Otteba5c1e92008-03-25 18:47:26 +01001362 case KVM_S390_PROGRAM_INT:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001363 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001364 return -EINVAL;
1365 irq->u.pgm.code = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001366 break;
Christian Borntraegerb7e6e4d2009-01-22 10:29:08 +01001367 case KVM_S390_SIGP_SET_PREFIX:
Jens Freimann383d0b02014-07-29 15:11:49 +02001368 irq->u.prefix.address = s390int->parm;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001369 break;
David Hildenbrand28225452014-10-15 16:48:16 +02001370 case KVM_S390_SIGP_STOP:
1371 irq->u.stop.flags = s390int->parm;
1372 break;
Jason J. Herne82a12732012-10-02 16:25:36 +02001373 case KVM_S390_INT_EXTERNAL_CALL:
Jens Freimann94d1f562015-01-15 14:40:34 +01001374 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001375 return -EINVAL;
1376 irq->u.extcall.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001377 break;
1378 case KVM_S390_INT_EMERGENCY:
Jens Freimann94d1f562015-01-15 14:40:34 +01001379 if (s390int->parm & 0xffff0000)
Jens Freimann383d0b02014-07-29 15:11:49 +02001380 return -EINVAL;
1381 irq->u.emerg.code = s390int->parm;
Jason J. Herne82a12732012-10-02 16:25:36 +02001382 break;
Cornelia Huck48a3e952012-12-20 15:32:09 +01001383 case KVM_S390_MCHK:
Jens Freimann383d0b02014-07-29 15:11:49 +02001384 irq->u.mchk.mcic = s390int->parm64;
1385 break;
1386 }
1387 return 0;
1388}
1389
David Hildenbrand6cddd432014-10-15 16:48:53 +02001390int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1391{
1392 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1393
1394 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1395}
1396
1397void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1398{
1399 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1400
1401 spin_lock(&li->lock);
1402 li->irq.stop.flags = 0;
1403 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1404 spin_unlock(&li->lock);
1405}
1406
Jens Freimann383d0b02014-07-29 15:11:49 +02001407int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1408{
1409 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1410 int rc;
1411
1412 spin_lock(&li->lock);
1413 switch (irq->type) {
1414 case KVM_S390_PROGRAM_INT:
1415 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
1416 irq->u.pgm.code);
1417 rc = __inject_prog(vcpu, irq);
1418 break;
1419 case KVM_S390_SIGP_SET_PREFIX:
1420 rc = __inject_set_prefix(vcpu, irq);
1421 break;
1422 case KVM_S390_SIGP_STOP:
1423 rc = __inject_sigp_stop(vcpu, irq);
1424 break;
1425 case KVM_S390_RESTART:
1426 rc = __inject_sigp_restart(vcpu, irq);
1427 break;
1428 case KVM_S390_INT_CLOCK_COMP:
1429 rc = __inject_ckc(vcpu);
1430 break;
1431 case KVM_S390_INT_CPU_TIMER:
1432 rc = __inject_cpu_timer(vcpu);
1433 break;
1434 case KVM_S390_INT_EXTERNAL_CALL:
1435 rc = __inject_extcall(vcpu, irq);
1436 break;
1437 case KVM_S390_INT_EMERGENCY:
1438 rc = __inject_sigp_emergency(vcpu, irq);
1439 break;
1440 case KVM_S390_MCHK:
1441 rc = __inject_mchk(vcpu, irq);
Cornelia Huck48a3e952012-12-20 15:32:09 +01001442 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001443 case KVM_S390_INT_PFAULT_INIT:
Jens Freimann383d0b02014-07-29 15:11:49 +02001444 rc = __inject_pfault_init(vcpu, irq);
Dominik Dingel3c038e62013-10-07 17:11:48 +02001445 break;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001446 case KVM_S390_INT_VIRTIO:
1447 case KVM_S390_INT_SERVICE:
Cornelia Huckd8346b72012-12-20 15:32:08 +01001448 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
Carsten Otteba5c1e92008-03-25 18:47:26 +01001449 default:
Jens Freimann0146a7b2014-07-28 15:37:58 +02001450 rc = -EINVAL;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001451 }
David Hildenbrand4ae3c082014-05-16 10:23:53 +02001452 spin_unlock(&li->lock);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001453 if (!rc)
1454 kvm_s390_vcpu_wakeup(vcpu);
Jens Freimann0146a7b2014-07-28 15:37:58 +02001455 return rc;
Carsten Otteba5c1e92008-03-25 18:47:26 +01001456}
Jens Freimannc05c4182013-10-07 16:13:45 +02001457
Christian Borntraeger67335e62014-03-25 17:09:08 +01001458void kvm_s390_clear_float_irqs(struct kvm *kvm)
Jens Freimannc05c4182013-10-07 16:13:45 +02001459{
1460 struct kvm_s390_float_interrupt *fi;
1461 struct kvm_s390_interrupt_info *n, *inti = NULL;
1462
Jens Freimannc05c4182013-10-07 16:13:45 +02001463 fi = &kvm->arch.float_int;
1464 spin_lock(&fi->lock);
1465 list_for_each_entry_safe(inti, n, &fi->list, list) {
1466 list_del(&inti->list);
1467 kfree(inti);
1468 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001469 fi->irq_count = 0;
Jens Freimannc05c4182013-10-07 16:13:45 +02001470 atomic_set(&fi->active, 0);
1471 spin_unlock(&fi->lock);
Jens Freimannc05c4182013-10-07 16:13:45 +02001472}
1473
1474static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1475 u8 *addr)
1476{
1477 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1478 struct kvm_s390_irq irq = {0};
1479
1480 irq.type = inti->type;
1481 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001482 case KVM_S390_INT_PFAULT_INIT:
1483 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001484 case KVM_S390_INT_VIRTIO:
1485 case KVM_S390_INT_SERVICE:
1486 irq.u.ext = inti->ext;
1487 break;
1488 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1489 irq.u.io = inti->io;
1490 break;
1491 case KVM_S390_MCHK:
1492 irq.u.mchk = inti->mchk;
1493 break;
1494 default:
1495 return -EINVAL;
1496 }
1497
1498 if (copy_to_user(uptr, &irq, sizeof(irq)))
1499 return -EFAULT;
1500
1501 return 0;
1502}
1503
1504static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1505{
1506 struct kvm_s390_interrupt_info *inti;
1507 struct kvm_s390_float_interrupt *fi;
1508 int ret = 0;
1509 int n = 0;
1510
Jens Freimannc05c4182013-10-07 16:13:45 +02001511 fi = &kvm->arch.float_int;
1512 spin_lock(&fi->lock);
1513
1514 list_for_each_entry(inti, &fi->list, list) {
1515 if (len < sizeof(struct kvm_s390_irq)) {
1516 /* signal userspace to try again */
1517 ret = -ENOMEM;
1518 break;
1519 }
1520 ret = copy_irq_to_user(inti, buf);
1521 if (ret)
1522 break;
1523 buf += sizeof(struct kvm_s390_irq);
1524 len -= sizeof(struct kvm_s390_irq);
1525 n++;
1526 }
1527
1528 spin_unlock(&fi->lock);
Jens Freimannc05c4182013-10-07 16:13:45 +02001529
1530 return ret < 0 ? ret : n;
1531}
1532
1533static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1534{
1535 int r;
1536
1537 switch (attr->group) {
1538 case KVM_DEV_FLIC_GET_ALL_IRQS:
1539 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1540 attr->attr);
1541 break;
1542 default:
1543 r = -EINVAL;
1544 }
1545
1546 return r;
1547}
1548
1549static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1550 u64 addr)
1551{
1552 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1553 void *target = NULL;
1554 void __user *source;
1555 u64 size;
1556
1557 if (get_user(inti->type, (u64 __user *)addr))
1558 return -EFAULT;
1559
1560 switch (inti->type) {
Dominik Dingel3c038e62013-10-07 17:11:48 +02001561 case KVM_S390_INT_PFAULT_INIT:
1562 case KVM_S390_INT_PFAULT_DONE:
Jens Freimannc05c4182013-10-07 16:13:45 +02001563 case KVM_S390_INT_VIRTIO:
1564 case KVM_S390_INT_SERVICE:
1565 target = (void *) &inti->ext;
1566 source = &uptr->u.ext;
1567 size = sizeof(inti->ext);
1568 break;
1569 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1570 target = (void *) &inti->io;
1571 source = &uptr->u.io;
1572 size = sizeof(inti->io);
1573 break;
1574 case KVM_S390_MCHK:
1575 target = (void *) &inti->mchk;
1576 source = &uptr->u.mchk;
1577 size = sizeof(inti->mchk);
1578 break;
1579 default:
1580 return -EINVAL;
1581 }
1582
1583 if (copy_from_user(target, source, size))
1584 return -EFAULT;
1585
1586 return 0;
1587}
1588
1589static int enqueue_floating_irq(struct kvm_device *dev,
1590 struct kvm_device_attr *attr)
1591{
1592 struct kvm_s390_interrupt_info *inti = NULL;
1593 int r = 0;
1594 int len = attr->attr;
1595
1596 if (len % sizeof(struct kvm_s390_irq) != 0)
1597 return -EINVAL;
1598 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1599 return -EINVAL;
1600
1601 while (len >= sizeof(struct kvm_s390_irq)) {
1602 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1603 if (!inti)
1604 return -ENOMEM;
1605
1606 r = copy_irq_from_user(inti, attr->addr);
1607 if (r) {
1608 kfree(inti);
1609 return r;
1610 }
Jens Freimanna91b8eb2014-01-30 08:40:23 +01001611 r = __inject_vm(dev->kvm, inti);
1612 if (r) {
1613 kfree(inti);
1614 return r;
1615 }
Jens Freimannc05c4182013-10-07 16:13:45 +02001616 len -= sizeof(struct kvm_s390_irq);
1617 attr->addr += sizeof(struct kvm_s390_irq);
1618 }
1619
1620 return r;
1621}
1622
Cornelia Huck841b91c2013-07-15 13:36:01 +02001623static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1624{
1625 if (id >= MAX_S390_IO_ADAPTERS)
1626 return NULL;
1627 return kvm->arch.adapters[id];
1628}
1629
1630static int register_io_adapter(struct kvm_device *dev,
1631 struct kvm_device_attr *attr)
1632{
1633 struct s390_io_adapter *adapter;
1634 struct kvm_s390_io_adapter adapter_info;
1635
1636 if (copy_from_user(&adapter_info,
1637 (void __user *)attr->addr, sizeof(adapter_info)))
1638 return -EFAULT;
1639
1640 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1641 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1642 return -EINVAL;
1643
1644 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1645 if (!adapter)
1646 return -ENOMEM;
1647
1648 INIT_LIST_HEAD(&adapter->maps);
1649 init_rwsem(&adapter->maps_lock);
1650 atomic_set(&adapter->nr_maps, 0);
1651 adapter->id = adapter_info.id;
1652 adapter->isc = adapter_info.isc;
1653 adapter->maskable = adapter_info.maskable;
1654 adapter->masked = false;
1655 adapter->swap = adapter_info.swap;
1656 dev->kvm->arch.adapters[adapter->id] = adapter;
1657
1658 return 0;
1659}
1660
1661int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1662{
1663 int ret;
1664 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1665
1666 if (!adapter || !adapter->maskable)
1667 return -EINVAL;
1668 ret = adapter->masked;
1669 adapter->masked = masked;
1670 return ret;
1671}
1672
1673static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1674{
1675 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1676 struct s390_map_info *map;
1677 int ret;
1678
1679 if (!adapter || !addr)
1680 return -EINVAL;
1681
1682 map = kzalloc(sizeof(*map), GFP_KERNEL);
1683 if (!map) {
1684 ret = -ENOMEM;
1685 goto out;
1686 }
1687 INIT_LIST_HEAD(&map->list);
1688 map->guest_addr = addr;
Martin Schwidefsky6e0a0432014-04-29 09:34:41 +02001689 map->addr = gmap_translate(kvm->arch.gmap, addr);
Cornelia Huck841b91c2013-07-15 13:36:01 +02001690 if (map->addr == -EFAULT) {
1691 ret = -EFAULT;
1692 goto out;
1693 }
1694 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1695 if (ret < 0)
1696 goto out;
1697 BUG_ON(ret != 1);
1698 down_write(&adapter->maps_lock);
1699 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1700 list_add_tail(&map->list, &adapter->maps);
1701 ret = 0;
1702 } else {
1703 put_page(map->page);
1704 ret = -EINVAL;
1705 }
1706 up_write(&adapter->maps_lock);
1707out:
1708 if (ret)
1709 kfree(map);
1710 return ret;
1711}
1712
1713static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1714{
1715 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1716 struct s390_map_info *map, *tmp;
1717 int found = 0;
1718
1719 if (!adapter || !addr)
1720 return -EINVAL;
1721
1722 down_write(&adapter->maps_lock);
1723 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1724 if (map->guest_addr == addr) {
1725 found = 1;
1726 atomic_dec(&adapter->nr_maps);
1727 list_del(&map->list);
1728 put_page(map->page);
1729 kfree(map);
1730 break;
1731 }
1732 }
1733 up_write(&adapter->maps_lock);
1734
1735 return found ? 0 : -EINVAL;
1736}
1737
1738void kvm_s390_destroy_adapters(struct kvm *kvm)
1739{
1740 int i;
1741 struct s390_map_info *map, *tmp;
1742
1743 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1744 if (!kvm->arch.adapters[i])
1745 continue;
1746 list_for_each_entry_safe(map, tmp,
1747 &kvm->arch.adapters[i]->maps, list) {
1748 list_del(&map->list);
1749 put_page(map->page);
1750 kfree(map);
1751 }
1752 kfree(kvm->arch.adapters[i]);
1753 }
1754}
1755
1756static int modify_io_adapter(struct kvm_device *dev,
1757 struct kvm_device_attr *attr)
1758{
1759 struct kvm_s390_io_adapter_req req;
1760 struct s390_io_adapter *adapter;
1761 int ret;
1762
1763 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1764 return -EFAULT;
1765
1766 adapter = get_io_adapter(dev->kvm, req.id);
1767 if (!adapter)
1768 return -EINVAL;
1769 switch (req.type) {
1770 case KVM_S390_IO_ADAPTER_MASK:
1771 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1772 if (ret > 0)
1773 ret = 0;
1774 break;
1775 case KVM_S390_IO_ADAPTER_MAP:
1776 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1777 break;
1778 case KVM_S390_IO_ADAPTER_UNMAP:
1779 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1780 break;
1781 default:
1782 ret = -EINVAL;
1783 }
1784
1785 return ret;
1786}
1787
Jens Freimannc05c4182013-10-07 16:13:45 +02001788static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1789{
1790 int r = 0;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001791 unsigned int i;
1792 struct kvm_vcpu *vcpu;
Jens Freimannc05c4182013-10-07 16:13:45 +02001793
1794 switch (attr->group) {
1795 case KVM_DEV_FLIC_ENQUEUE:
1796 r = enqueue_floating_irq(dev, attr);
1797 break;
1798 case KVM_DEV_FLIC_CLEAR_IRQS:
Christian Borntraeger67335e62014-03-25 17:09:08 +01001799 kvm_s390_clear_float_irqs(dev->kvm);
Jens Freimannc05c4182013-10-07 16:13:45 +02001800 break;
Dominik Dingel3c038e62013-10-07 17:11:48 +02001801 case KVM_DEV_FLIC_APF_ENABLE:
1802 dev->kvm->arch.gmap->pfault_enabled = 1;
1803 break;
1804 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1805 dev->kvm->arch.gmap->pfault_enabled = 0;
1806 /*
1807 * Make sure no async faults are in transition when
1808 * clearing the queues. So we don't need to worry
1809 * about late coming workers.
1810 */
1811 synchronize_srcu(&dev->kvm->srcu);
1812 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1813 kvm_clear_async_pf_completion_queue(vcpu);
1814 break;
Cornelia Huck841b91c2013-07-15 13:36:01 +02001815 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1816 r = register_io_adapter(dev, attr);
1817 break;
1818 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1819 r = modify_io_adapter(dev, attr);
1820 break;
Jens Freimannc05c4182013-10-07 16:13:45 +02001821 default:
1822 r = -EINVAL;
1823 }
1824
1825 return r;
1826}
1827
1828static int flic_create(struct kvm_device *dev, u32 type)
1829{
1830 if (!dev)
1831 return -EINVAL;
1832 if (dev->kvm->arch.flic)
1833 return -EINVAL;
1834 dev->kvm->arch.flic = dev;
1835 return 0;
1836}
1837
1838static void flic_destroy(struct kvm_device *dev)
1839{
1840 dev->kvm->arch.flic = NULL;
1841 kfree(dev);
1842}
1843
1844/* s390 floating irq controller (flic) */
1845struct kvm_device_ops kvm_flic_ops = {
1846 .name = "kvm-flic",
1847 .get_attr = flic_get_attr,
1848 .set_attr = flic_set_attr,
1849 .create = flic_create,
1850 .destroy = flic_destroy,
1851};
Cornelia Huck84223592013-07-15 13:36:01 +02001852
1853static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1854{
1855 unsigned long bit;
1856
1857 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1858
1859 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1860}
1861
1862static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1863 u64 addr)
1864{
1865 struct s390_map_info *map;
1866
1867 if (!adapter)
1868 return NULL;
1869
1870 list_for_each_entry(map, &adapter->maps, list) {
1871 if (map->guest_addr == addr)
1872 return map;
1873 }
1874 return NULL;
1875}
1876
1877static int adapter_indicators_set(struct kvm *kvm,
1878 struct s390_io_adapter *adapter,
1879 struct kvm_s390_adapter_int *adapter_int)
1880{
1881 unsigned long bit;
1882 int summary_set, idx;
1883 struct s390_map_info *info;
1884 void *map;
1885
1886 info = get_map_info(adapter, adapter_int->ind_addr);
1887 if (!info)
1888 return -1;
1889 map = page_address(info->page);
1890 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1891 set_bit(bit, map);
1892 idx = srcu_read_lock(&kvm->srcu);
1893 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1894 set_page_dirty_lock(info->page);
1895 info = get_map_info(adapter, adapter_int->summary_addr);
1896 if (!info) {
1897 srcu_read_unlock(&kvm->srcu, idx);
1898 return -1;
1899 }
1900 map = page_address(info->page);
1901 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1902 adapter->swap);
1903 summary_set = test_and_set_bit(bit, map);
1904 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1905 set_page_dirty_lock(info->page);
1906 srcu_read_unlock(&kvm->srcu, idx);
1907 return summary_set ? 0 : 1;
1908}
1909
1910/*
1911 * < 0 - not injected due to error
1912 * = 0 - coalesced, summary indicator already active
1913 * > 0 - injected interrupt
1914 */
1915static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1916 struct kvm *kvm, int irq_source_id, int level,
1917 bool line_status)
1918{
1919 int ret;
1920 struct s390_io_adapter *adapter;
1921
1922 /* We're only interested in the 0->1 transition. */
1923 if (!level)
1924 return 0;
1925 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1926 if (!adapter)
1927 return -1;
1928 down_read(&adapter->maps_lock);
1929 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1930 up_read(&adapter->maps_lock);
1931 if ((ret > 0) && !adapter->masked) {
1932 struct kvm_s390_interrupt s390int = {
1933 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1934 .parm = 0,
1935 .parm64 = (adapter->isc << 27) | 0x80000000,
1936 };
1937 ret = kvm_s390_inject_vm(kvm, &s390int);
1938 if (ret == 0)
1939 ret = 1;
1940 }
1941 return ret;
1942}
1943
Paul Mackerras8ba918d2014-06-30 20:51:10 +10001944int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
Cornelia Huck84223592013-07-15 13:36:01 +02001945 const struct kvm_irq_routing_entry *ue)
1946{
1947 int ret;
1948
1949 switch (ue->type) {
1950 case KVM_IRQ_ROUTING_S390_ADAPTER:
1951 e->set = set_adapter_int;
1952 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1953 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1954 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1955 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1956 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1957 ret = 0;
1958 break;
1959 default:
1960 ret = -EINVAL;
1961 }
1962
1963 return ret;
1964}
1965
1966int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1967 int irq_source_id, int level, bool line_status)
1968{
1969 return -EINVAL;
1970}