1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * handling kvm guest interrupts
4 *
5 * Copyright IBM Corp. 2008, 2020
6 *
7 * Author(s): Carsten Otte <cotte@de.ibm.com>
8 */
9
10 #define KMSG_COMPONENT "kvm-s390"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
13 #include <linux/cpufeature.h>
14 #include <linux/interrupt.h>
15 #include <linux/kvm_host.h>
16 #include <linux/hrtimer.h>
17 #include <linux/export.h>
18 #include <linux/mmu_context.h>
19 #include <linux/nospec.h>
20 #include <linux/signal.h>
21 #include <linux/slab.h>
22 #include <linux/bitmap.h>
23 #include <linux/vmalloc.h>
24 #include <asm/access-regs.h>
25 #include <asm/asm-offsets.h>
26 #include <asm/dis.h>
27 #include <linux/uaccess.h>
28 #include <asm/sclp.h>
29 #include <asm/isc.h>
30 #include <asm/gmap.h>
31 #include <asm/nmi.h>
32 #include <asm/airq.h>
33 #include <asm/tpi.h>
34 #include "kvm-s390.h"
35 #include "gaccess.h"
36 #include "trace-s390.h"
37 #include "pci.h"
38
39 #define PFAULT_INIT 0x0600
40 #define PFAULT_DONE 0x0680
41 #define VIRTIO_PARAM 0x0d00
42
43 static struct kvm_s390_gib *gib;
44
45 /* handle external calls via sigp interpretation facility */
sca_ext_call_pending(struct kvm_vcpu * vcpu,int * src_id)46 static int sca_ext_call_pending(struct kvm_vcpu *vcpu, int *src_id)
47 {
48 int c, scn;
49
50 if (!kvm_s390_test_cpuflags(vcpu, CPUSTAT_ECALL_PEND))
51 return 0;
52
53 BUG_ON(!kvm_s390_use_sca_entries());
54 read_lock(&vcpu->kvm->arch.sca_lock);
55 if (vcpu->kvm->arch.use_esca) {
56 struct esca_block *sca = vcpu->kvm->arch.sca;
57 union esca_sigp_ctrl sigp_ctrl =
58 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
59
60 c = sigp_ctrl.c;
61 scn = sigp_ctrl.scn;
62 } else {
63 struct bsca_block *sca = vcpu->kvm->arch.sca;
64 union bsca_sigp_ctrl sigp_ctrl =
65 sca->cpu[vcpu->vcpu_id].sigp_ctrl;
66
67 c = sigp_ctrl.c;
68 scn = sigp_ctrl.scn;
69 }
70 read_unlock(&vcpu->kvm->arch.sca_lock);
71
72 if (src_id)
73 *src_id = scn;
74
75 return c;
76 }
77
sca_inject_ext_call(struct kvm_vcpu * vcpu,int src_id)78 static int sca_inject_ext_call(struct kvm_vcpu *vcpu, int src_id)
79 {
80 int expect, rc;
81
82 BUG_ON(!kvm_s390_use_sca_entries());
83 read_lock(&vcpu->kvm->arch.sca_lock);
84 if (vcpu->kvm->arch.use_esca) {
85 struct esca_block *sca = vcpu->kvm->arch.sca;
86 union esca_sigp_ctrl *sigp_ctrl =
87 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
88 union esca_sigp_ctrl new_val = {0}, old_val;
89
90 old_val = READ_ONCE(*sigp_ctrl);
91 new_val.scn = src_id;
92 new_val.c = 1;
93 old_val.c = 0;
94
95 expect = old_val.value;
96 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
97 } else {
98 struct bsca_block *sca = vcpu->kvm->arch.sca;
99 union bsca_sigp_ctrl *sigp_ctrl =
100 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
101 union bsca_sigp_ctrl new_val = {0}, old_val;
102
103 old_val = READ_ONCE(*sigp_ctrl);
104 new_val.scn = src_id;
105 new_val.c = 1;
106 old_val.c = 0;
107
108 expect = old_val.value;
109 rc = cmpxchg(&sigp_ctrl->value, old_val.value, new_val.value);
110 }
111 read_unlock(&vcpu->kvm->arch.sca_lock);
112
113 if (rc != expect) {
114 /* another external call is pending */
115 return -EBUSY;
116 }
117 kvm_s390_set_cpuflags(vcpu, CPUSTAT_ECALL_PEND);
118 return 0;
119 }
120
sca_clear_ext_call(struct kvm_vcpu * vcpu)121 static void sca_clear_ext_call(struct kvm_vcpu *vcpu)
122 {
123 if (!kvm_s390_use_sca_entries())
124 return;
125 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_ECALL_PEND);
126 read_lock(&vcpu->kvm->arch.sca_lock);
127 if (vcpu->kvm->arch.use_esca) {
128 struct esca_block *sca = vcpu->kvm->arch.sca;
129 union esca_sigp_ctrl *sigp_ctrl =
130 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
131
132 WRITE_ONCE(sigp_ctrl->value, 0);
133 } else {
134 struct bsca_block *sca = vcpu->kvm->arch.sca;
135 union bsca_sigp_ctrl *sigp_ctrl =
136 &(sca->cpu[vcpu->vcpu_id].sigp_ctrl);
137
138 WRITE_ONCE(sigp_ctrl->value, 0);
139 }
140 read_unlock(&vcpu->kvm->arch.sca_lock);
141 }
142
psw_extint_disabled(struct kvm_vcpu * vcpu)143 int psw_extint_disabled(struct kvm_vcpu *vcpu)
144 {
145 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
146 }
147
psw_ioint_disabled(struct kvm_vcpu * vcpu)148 static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
149 {
150 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
151 }
152
psw_mchk_disabled(struct kvm_vcpu * vcpu)153 static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
154 {
155 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
156 }
157
psw_interrupts_disabled(struct kvm_vcpu * vcpu)158 static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
159 {
160 return psw_extint_disabled(vcpu) &&
161 psw_ioint_disabled(vcpu) &&
162 psw_mchk_disabled(vcpu);
163 }
164
ckc_interrupts_enabled(struct kvm_vcpu * vcpu)165 static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
166 {
167 if (psw_extint_disabled(vcpu) ||
168 !(vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SUBMASK))
169 return 0;
170 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
171 /* No timer interrupts when single stepping */
172 return 0;
173 return 1;
174 }
175
ckc_irq_pending(struct kvm_vcpu * vcpu)176 static int ckc_irq_pending(struct kvm_vcpu *vcpu)
177 {
178 const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
179 const u64 ckc = vcpu->arch.sie_block->ckc;
180
181 if (vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SIGN) {
182 if ((s64)ckc >= (s64)now)
183 return 0;
184 } else if (ckc >= now) {
185 return 0;
186 }
187 return ckc_interrupts_enabled(vcpu);
188 }
189
cpu_timer_interrupts_enabled(struct kvm_vcpu * vcpu)190 static int cpu_timer_interrupts_enabled(struct kvm_vcpu *vcpu)
191 {
192 return !psw_extint_disabled(vcpu) &&
193 (vcpu->arch.sie_block->gcr[0] & CR0_CPU_TIMER_SUBMASK);
194 }
195
cpu_timer_irq_pending(struct kvm_vcpu * vcpu)196 static int cpu_timer_irq_pending(struct kvm_vcpu *vcpu)
197 {
198 if (!cpu_timer_interrupts_enabled(vcpu))
199 return 0;
200 return kvm_s390_get_cpu_timer(vcpu) >> 63;
201 }
202
isc_to_isc_bits(int isc)203 static uint64_t isc_to_isc_bits(int isc)
204 {
205 return (0x80 >> isc) << 24;
206 }
207
isc_to_int_word(u8 isc)208 static inline u32 isc_to_int_word(u8 isc)
209 {
210 return ((u32)isc << 27) | 0x80000000;
211 }
212
int_word_to_isc(u32 int_word)213 static inline u8 int_word_to_isc(u32 int_word)
214 {
215 return (int_word & 0x38000000) >> 27;
216 }
217
218 /*
219 * To use atomic bitmap functions, we have to provide a bitmap address
220 * that is u64 aligned. However, the ipm might be u32 aligned.
221 * Therefore, we logically start the bitmap at the very beginning of the
222 * struct and fixup the bit number.
223 */
224 #define IPM_BIT_OFFSET (offsetof(struct kvm_s390_gisa, ipm) * BITS_PER_BYTE)
225
226 /**
227 * gisa_set_iam - change the GISA interruption alert mask
228 *
229 * @gisa: gisa to operate on
230 * @iam: new IAM value to use
231 *
232 * Change the IAM atomically with the next alert address and the IPM
233 * of the GISA if the GISA is not part of the GIB alert list. All three
234 * fields are located in the first long word of the GISA.
235 *
236 * Returns: 0 on success
237 * -EBUSY in case the gisa is part of the alert list
238 */
gisa_set_iam(struct kvm_s390_gisa * gisa,u8 iam)239 static inline int gisa_set_iam(struct kvm_s390_gisa *gisa, u8 iam)
240 {
241 u64 word, _word;
242
243 word = READ_ONCE(gisa->u64.word[0]);
244 do {
245 if ((u64)gisa != word >> 32)
246 return -EBUSY;
247 _word = (word & ~0xffUL) | iam;
248 } while (!try_cmpxchg(&gisa->u64.word[0], &word, _word));
249
250 return 0;
251 }
252
253 /**
254 * gisa_clear_ipm - clear the GISA interruption pending mask
255 *
256 * @gisa: gisa to operate on
257 *
258 * Clear the IPM atomically with the next alert address and the IAM
259 * of the GISA unconditionally. All three fields are located in the
260 * first long word of the GISA.
261 */
gisa_clear_ipm(struct kvm_s390_gisa * gisa)262 static inline void gisa_clear_ipm(struct kvm_s390_gisa *gisa)
263 {
264 u64 word, _word;
265
266 word = READ_ONCE(gisa->u64.word[0]);
267 do {
268 _word = word & ~(0xffUL << 24);
269 } while (!try_cmpxchg(&gisa->u64.word[0], &word, _word));
270 }
271
272 /**
273 * gisa_get_ipm_or_restore_iam - return IPM or restore GISA IAM
274 *
275 * @gi: gisa interrupt struct to work on
276 *
277 * Atomically restores the interruption alert mask if none of the
278 * relevant ISCs are pending and return the IPM.
279 *
280 * Returns: the relevant pending ISCs
281 */
gisa_get_ipm_or_restore_iam(struct kvm_s390_gisa_interrupt * gi)282 static inline u8 gisa_get_ipm_or_restore_iam(struct kvm_s390_gisa_interrupt *gi)
283 {
284 u8 pending_mask, alert_mask;
285 u64 word, _word;
286
287 word = READ_ONCE(gi->origin->u64.word[0]);
288 do {
289 alert_mask = READ_ONCE(gi->alert.mask);
290 pending_mask = (u8)(word >> 24) & alert_mask;
291 if (pending_mask)
292 return pending_mask;
293 _word = (word & ~0xffUL) | alert_mask;
294 } while (!try_cmpxchg(&gi->origin->u64.word[0], &word, _word));
295
296 return 0;
297 }
298
gisa_set_ipm_gisc(struct kvm_s390_gisa * gisa,u32 gisc)299 static inline void gisa_set_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
300 {
301 set_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
302 }
303
gisa_get_ipm(struct kvm_s390_gisa * gisa)304 static inline u8 gisa_get_ipm(struct kvm_s390_gisa *gisa)
305 {
306 return READ_ONCE(gisa->ipm);
307 }
308
gisa_tac_ipm_gisc(struct kvm_s390_gisa * gisa,u32 gisc)309 static inline int gisa_tac_ipm_gisc(struct kvm_s390_gisa *gisa, u32 gisc)
310 {
311 return test_and_clear_bit_inv(IPM_BIT_OFFSET + gisc, (unsigned long *) gisa);
312 }
313
pending_irqs_no_gisa(struct kvm_vcpu * vcpu)314 static inline unsigned long pending_irqs_no_gisa(struct kvm_vcpu *vcpu)
315 {
316 unsigned long pending = vcpu->kvm->arch.float_int.pending_irqs |
317 vcpu->arch.local_int.pending_irqs;
318
319 pending &= ~vcpu->kvm->arch.float_int.masked_irqs;
320 return pending;
321 }
322
pending_irqs(struct kvm_vcpu * vcpu)323 static inline unsigned long pending_irqs(struct kvm_vcpu *vcpu)
324 {
325 struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
326 unsigned long pending_mask;
327
328 pending_mask = pending_irqs_no_gisa(vcpu);
329 if (gi->origin)
330 pending_mask |= gisa_get_ipm(gi->origin) << IRQ_PEND_IO_ISC_7;
331 return pending_mask;
332 }
333
isc_to_irq_type(unsigned long isc)334 static inline int isc_to_irq_type(unsigned long isc)
335 {
336 return IRQ_PEND_IO_ISC_0 - isc;
337 }
338
irq_type_to_isc(unsigned long irq_type)339 static inline int irq_type_to_isc(unsigned long irq_type)
340 {
341 return IRQ_PEND_IO_ISC_0 - irq_type;
342 }
343
disable_iscs(struct kvm_vcpu * vcpu,unsigned long active_mask)344 static unsigned long disable_iscs(struct kvm_vcpu *vcpu,
345 unsigned long active_mask)
346 {
347 int i;
348
349 for (i = 0; i <= MAX_ISC; i++)
350 if (!(vcpu->arch.sie_block->gcr[6] & isc_to_isc_bits(i)))
351 active_mask &= ~(1UL << (isc_to_irq_type(i)));
352
353 return active_mask;
354 }
355
deliverable_irqs(struct kvm_vcpu * vcpu)356 static unsigned long deliverable_irqs(struct kvm_vcpu *vcpu)
357 {
358 unsigned long active_mask;
359
360 active_mask = pending_irqs(vcpu);
361 if (!active_mask)
362 return 0;
363
364 if (psw_extint_disabled(vcpu))
365 active_mask &= ~IRQ_PEND_EXT_MASK;
366 if (psw_ioint_disabled(vcpu))
367 active_mask &= ~IRQ_PEND_IO_MASK;
368 else
369 active_mask = disable_iscs(vcpu, active_mask);
370 if (!(vcpu->arch.sie_block->gcr[0] & CR0_EXTERNAL_CALL_SUBMASK))
371 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
372 if (!(vcpu->arch.sie_block->gcr[0] & CR0_EMERGENCY_SIGNAL_SUBMASK))
373 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
374 if (!(vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SUBMASK))
375 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
376 if (!(vcpu->arch.sie_block->gcr[0] & CR0_CPU_TIMER_SUBMASK))
377 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
378 if (!(vcpu->arch.sie_block->gcr[0] & CR0_SERVICE_SIGNAL_SUBMASK)) {
379 __clear_bit(IRQ_PEND_EXT_SERVICE, &active_mask);
380 __clear_bit(IRQ_PEND_EXT_SERVICE_EV, &active_mask);
381 }
382 if (psw_mchk_disabled(vcpu))
383 active_mask &= ~IRQ_PEND_MCHK_MASK;
384 /* PV guest cpus can have a single interruption injected at a time. */
385 if (kvm_s390_pv_cpu_get_handle(vcpu) &&
386 vcpu->arch.sie_block->iictl != IICTL_CODE_NONE)
387 active_mask &= ~(IRQ_PEND_EXT_II_MASK |
388 IRQ_PEND_IO_MASK |
389 IRQ_PEND_MCHK_MASK);
390 /*
391 * Check both floating and local interrupt's cr14 because
392 * bit IRQ_PEND_MCHK_REP could be set in both cases.
393 */
394 if (!(vcpu->arch.sie_block->gcr[14] &
395 (vcpu->kvm->arch.float_int.mchk.cr14 |
396 vcpu->arch.local_int.irq.mchk.cr14)))
397 __clear_bit(IRQ_PEND_MCHK_REP, &active_mask);
398
399 /*
400 * STOP irqs will never be actively delivered. They are triggered via
401 * intercept requests and cleared when the stop intercept is performed.
402 */
403 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
404
405 return active_mask;
406 }
407
__set_cpu_idle(struct kvm_vcpu * vcpu)408 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
409 {
410 kvm_s390_set_cpuflags(vcpu, CPUSTAT_WAIT);
411 set_bit(vcpu->vcpu_idx, vcpu->kvm->arch.idle_mask);
412 }
413
__unset_cpu_idle(struct kvm_vcpu * vcpu)414 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
415 {
416 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_WAIT);
417 clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.idle_mask);
418 }
419
__reset_intercept_indicators(struct kvm_vcpu * vcpu)420 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
421 {
422 kvm_s390_clear_cpuflags(vcpu, CPUSTAT_IO_INT | CPUSTAT_EXT_INT |
423 CPUSTAT_STOP_INT);
424 vcpu->arch.sie_block->lctl = 0x0000;
425 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
426
427 if (guestdbg_enabled(vcpu)) {
428 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
429 LCTL_CR10 | LCTL_CR11);
430 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
431 }
432 }
433
set_intercept_indicators_io(struct kvm_vcpu * vcpu)434 static void set_intercept_indicators_io(struct kvm_vcpu *vcpu)
435 {
436 if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_IO_MASK))
437 return;
438 if (psw_ioint_disabled(vcpu))
439 kvm_s390_set_cpuflags(vcpu, CPUSTAT_IO_INT);
440 else
441 vcpu->arch.sie_block->lctl |= LCTL_CR6;
442 }
443
set_intercept_indicators_ext(struct kvm_vcpu * vcpu)444 static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
445 {
446 if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_EXT_MASK))
447 return;
448 if (psw_extint_disabled(vcpu))
449 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
450 else
451 vcpu->arch.sie_block->lctl |= LCTL_CR0;
452 }
453
set_intercept_indicators_mchk(struct kvm_vcpu * vcpu)454 static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
455 {
456 if (!(pending_irqs_no_gisa(vcpu) & IRQ_PEND_MCHK_MASK))
457 return;
458 if (psw_mchk_disabled(vcpu))
459 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
460 else
461 vcpu->arch.sie_block->lctl |= LCTL_CR14;
462 }
463
set_intercept_indicators_stop(struct kvm_vcpu * vcpu)464 static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
465 {
466 if (kvm_s390_is_stop_irq_pending(vcpu))
467 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
468 }
469
470 /* Set interception request for non-deliverable interrupts */
set_intercept_indicators(struct kvm_vcpu * vcpu)471 static void set_intercept_indicators(struct kvm_vcpu *vcpu)
472 {
473 set_intercept_indicators_io(vcpu);
474 set_intercept_indicators_ext(vcpu);
475 set_intercept_indicators_mchk(vcpu);
476 set_intercept_indicators_stop(vcpu);
477 }
478
__deliver_cpu_timer(struct kvm_vcpu * vcpu)479 static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
480 {
481 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
482 int rc = 0;
483
484 vcpu->stat.deliver_cputm++;
485 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
486 0, 0);
487 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
488 vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
489 vcpu->arch.sie_block->eic = EXT_IRQ_CPU_TIMER;
490 } else {
491 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
492 (u16 *)__LC_EXT_INT_CODE);
493 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
494 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
495 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
496 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
497 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
498 }
499 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
500 return rc ? -EFAULT : 0;
501 }
502
__deliver_ckc(struct kvm_vcpu * vcpu)503 static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
504 {
505 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
506 int rc = 0;
507
508 vcpu->stat.deliver_ckc++;
509 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
510 0, 0);
511 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
512 vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
513 vcpu->arch.sie_block->eic = EXT_IRQ_CLK_COMP;
514 } else {
515 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
516 (u16 __user *)__LC_EXT_INT_CODE);
517 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
518 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
519 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
520 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
521 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
522 }
523 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
524 return rc ? -EFAULT : 0;
525 }
526
__deliver_pfault_init(struct kvm_vcpu * vcpu)527 static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
528 {
529 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
530 struct kvm_s390_ext_info ext;
531 int rc;
532
533 spin_lock(&li->lock);
534 ext = li->irq.ext;
535 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
536 li->irq.ext.ext_params2 = 0;
537 spin_unlock(&li->lock);
538
539 VCPU_EVENT(vcpu, 4, "deliver: pfault init token 0x%llx",
540 ext.ext_params2);
541 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
542 KVM_S390_INT_PFAULT_INIT,
543 0, ext.ext_params2);
544
545 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
546 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
547 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
548 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
549 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
550 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
551 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
552 return rc ? -EFAULT : 0;
553 }
554
__write_machine_check(struct kvm_vcpu * vcpu,struct kvm_s390_mchk_info * mchk)555 static int __write_machine_check(struct kvm_vcpu *vcpu,
556 struct kvm_s390_mchk_info *mchk)
557 {
558 unsigned long ext_sa_addr;
559 unsigned long lc;
560 freg_t fprs[NUM_FPRS];
561 union mci mci;
562 int rc;
563
564 /*
565 * All other possible payload for a machine check (e.g. the register
566 * contents in the save area) will be handled by the ultravisor, as
567 * the hypervisor does not not have the needed information for
568 * protected guests.
569 */
570 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
571 vcpu->arch.sie_block->iictl = IICTL_CODE_MCHK;
572 vcpu->arch.sie_block->mcic = mchk->mcic;
573 vcpu->arch.sie_block->faddr = mchk->failing_storage_address;
574 vcpu->arch.sie_block->edc = mchk->ext_damage_code;
575 return 0;
576 }
577
578 mci.val = mchk->mcic;
579 /* take care of lazy register loading */
580 kvm_s390_fpu_store(vcpu->run);
581 save_access_regs(vcpu->run->s.regs.acrs);
582 if (cpu_has_gs() && vcpu->arch.gs_enabled)
583 save_gs_cb(current->thread.gs_cb);
584
585 /* Extended save area */
586 rc = read_guest_lc(vcpu, __LC_MCESAD, &ext_sa_addr,
587 sizeof(unsigned long));
588 /* Only bits 0 through 63-LC are used for address formation */
589 lc = ext_sa_addr & MCESA_LC_MASK;
590 if (test_kvm_facility(vcpu->kvm, 133)) {
591 switch (lc) {
592 case 0:
593 case 10:
594 ext_sa_addr &= ~0x3ffUL;
595 break;
596 case 11:
597 ext_sa_addr &= ~0x7ffUL;
598 break;
599 case 12:
600 ext_sa_addr &= ~0xfffUL;
601 break;
602 default:
603 ext_sa_addr = 0;
604 break;
605 }
606 } else {
607 ext_sa_addr &= ~0x3ffUL;
608 }
609
610 if (!rc && mci.vr && ext_sa_addr && test_kvm_facility(vcpu->kvm, 129)) {
611 if (write_guest_abs(vcpu, ext_sa_addr, vcpu->run->s.regs.vrs,
612 512))
613 mci.vr = 0;
614 } else {
615 mci.vr = 0;
616 }
617 if (!rc && mci.gs && ext_sa_addr && test_kvm_facility(vcpu->kvm, 133)
618 && (lc == 11 || lc == 12)) {
619 if (write_guest_abs(vcpu, ext_sa_addr + 1024,
620 &vcpu->run->s.regs.gscb, 32))
621 mci.gs = 0;
622 } else {
623 mci.gs = 0;
624 }
625
626 /* General interruption information */
627 rc |= put_guest_lc(vcpu, 1, (u8 __user *) __LC_AR_MODE_ID);
628 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
629 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
630 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
631 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
632 rc |= put_guest_lc(vcpu, mci.val, (u64 __user *) __LC_MCCK_CODE);
633
634 /* Register-save areas */
635 if (cpu_has_vx()) {
636 convert_vx_to_fp(fprs, (__vector128 *) vcpu->run->s.regs.vrs);
637 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA, fprs, 128);
638 } else {
639 rc |= write_guest_lc(vcpu, __LC_FPREGS_SAVE_AREA,
640 vcpu->run->s.regs.fprs, 128);
641 }
642 rc |= write_guest_lc(vcpu, __LC_GPREGS_SAVE_AREA,
643 vcpu->run->s.regs.gprs, 128);
644 rc |= put_guest_lc(vcpu, vcpu->run->s.regs.fpc,
645 (u32 __user *) __LC_FP_CREG_SAVE_AREA);
646 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->todpr,
647 (u32 __user *) __LC_TOD_PROGREG_SAVE_AREA);
648 rc |= put_guest_lc(vcpu, kvm_s390_get_cpu_timer(vcpu),
649 (u64 __user *) __LC_CPU_TIMER_SAVE_AREA);
650 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->ckc >> 8,
651 (u64 __user *) __LC_CLOCK_COMP_SAVE_AREA);
652 rc |= write_guest_lc(vcpu, __LC_AREGS_SAVE_AREA,
653 &vcpu->run->s.regs.acrs, 64);
654 rc |= write_guest_lc(vcpu, __LC_CREGS_SAVE_AREA,
655 &vcpu->arch.sie_block->gcr, 128);
656
657 /* Extended interruption information */
658 rc |= put_guest_lc(vcpu, mchk->ext_damage_code,
659 (u32 __user *) __LC_EXT_DAMAGE_CODE);
660 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
661 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
662 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA, &mchk->fixed_logout,
663 sizeof(mchk->fixed_logout));
664 return rc ? -EFAULT : 0;
665 }
666
__deliver_machine_check(struct kvm_vcpu * vcpu)667 static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
668 {
669 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
670 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
671 struct kvm_s390_mchk_info mchk = {};
672 int deliver = 0;
673 int rc = 0;
674
675 spin_lock(&fi->lock);
676 spin_lock(&li->lock);
677 if (test_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs) ||
678 test_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs)) {
679 /*
680 * If there was an exigent machine check pending, then any
681 * repressible machine checks that might have been pending
682 * are indicated along with it, so always clear bits for
683 * repressible and exigent interrupts
684 */
685 mchk = li->irq.mchk;
686 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
687 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
688 memset(&li->irq.mchk, 0, sizeof(mchk));
689 deliver = 1;
690 }
691 /*
692 * We indicate floating repressible conditions along with
693 * other pending conditions. Channel Report Pending and Channel
694 * Subsystem damage are the only two and are indicated by
695 * bits in mcic and masked in cr14.
696 */
697 if (test_and_clear_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
698 mchk.mcic |= fi->mchk.mcic;
699 mchk.cr14 |= fi->mchk.cr14;
700 memset(&fi->mchk, 0, sizeof(mchk));
701 deliver = 1;
702 }
703 spin_unlock(&li->lock);
704 spin_unlock(&fi->lock);
705
706 if (deliver) {
707 VCPU_EVENT(vcpu, 3, "deliver: machine check mcic 0x%llx",
708 mchk.mcic);
709 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
710 KVM_S390_MCHK,
711 mchk.cr14, mchk.mcic);
712 vcpu->stat.deliver_machine_check++;
713 rc = __write_machine_check(vcpu, &mchk);
714 }
715 return rc;
716 }
717
__deliver_restart(struct kvm_vcpu * vcpu)718 static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
719 {
720 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
721 int rc = 0;
722
723 VCPU_EVENT(vcpu, 3, "%s", "deliver: cpu restart");
724 vcpu->stat.deliver_restart_signal++;
725 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
726
727 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
728 vcpu->arch.sie_block->iictl = IICTL_CODE_RESTART;
729 } else {
730 rc = write_guest_lc(vcpu,
731 offsetof(struct lowcore, restart_old_psw),
732 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
733 rc |= read_guest_lc(vcpu, offsetof(struct lowcore, restart_psw),
734 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
735 }
736 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
737 return rc ? -EFAULT : 0;
738 }
739
__deliver_set_prefix(struct kvm_vcpu * vcpu)740 static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
741 {
742 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
743 struct kvm_s390_prefix_info prefix;
744
745 spin_lock(&li->lock);
746 prefix = li->irq.prefix;
747 li->irq.prefix.address = 0;
748 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
749 spin_unlock(&li->lock);
750
751 vcpu->stat.deliver_prefix_signal++;
752 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
753 KVM_S390_SIGP_SET_PREFIX,
754 prefix.address, 0);
755
756 kvm_s390_set_prefix(vcpu, prefix.address);
757 return 0;
758 }
759
__deliver_emergency_signal(struct kvm_vcpu * vcpu)760 static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
761 {
762 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
763 int rc;
764 int cpu_addr;
765
766 spin_lock(&li->lock);
767 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
768 clear_bit(cpu_addr, li->sigp_emerg_pending);
769 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
770 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
771 spin_unlock(&li->lock);
772
773 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp emerg");
774 vcpu->stat.deliver_emergency_signal++;
775 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
776 cpu_addr, 0);
777 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
778 vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
779 vcpu->arch.sie_block->eic = EXT_IRQ_EMERGENCY_SIG;
780 vcpu->arch.sie_block->extcpuaddr = cpu_addr;
781 return 0;
782 }
783
784 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
785 (u16 *)__LC_EXT_INT_CODE);
786 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
787 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
788 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
789 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
790 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
791 return rc ? -EFAULT : 0;
792 }
793
__deliver_external_call(struct kvm_vcpu * vcpu)794 static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
795 {
796 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
797 struct kvm_s390_extcall_info extcall;
798 int rc;
799
800 spin_lock(&li->lock);
801 extcall = li->irq.extcall;
802 li->irq.extcall.code = 0;
803 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
804 spin_unlock(&li->lock);
805
806 VCPU_EVENT(vcpu, 4, "%s", "deliver: sigp ext call");
807 vcpu->stat.deliver_external_call++;
808 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
809 KVM_S390_INT_EXTERNAL_CALL,
810 extcall.code, 0);
811 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
812 vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
813 vcpu->arch.sie_block->eic = EXT_IRQ_EXTERNAL_CALL;
814 vcpu->arch.sie_block->extcpuaddr = extcall.code;
815 return 0;
816 }
817
818 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
819 (u16 *)__LC_EXT_INT_CODE);
820 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
821 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
822 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
823 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
824 sizeof(psw_t));
825 return rc ? -EFAULT : 0;
826 }
827
__deliver_prog_pv(struct kvm_vcpu * vcpu,u16 code)828 static int __deliver_prog_pv(struct kvm_vcpu *vcpu, u16 code)
829 {
830 switch (code) {
831 case PGM_SPECIFICATION:
832 vcpu->arch.sie_block->iictl = IICTL_CODE_SPECIFICATION;
833 break;
834 case PGM_OPERAND:
835 vcpu->arch.sie_block->iictl = IICTL_CODE_OPERAND;
836 break;
837 default:
838 return -EINVAL;
839 }
840 return 0;
841 }
842
__deliver_prog(struct kvm_vcpu * vcpu)843 static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
844 {
845 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
846 struct kvm_s390_pgm_info pgm_info;
847 int rc = 0, nullifying = false;
848 u16 ilen;
849
850 spin_lock(&li->lock);
851 pgm_info = li->irq.pgm;
852 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
853 memset(&li->irq.pgm, 0, sizeof(pgm_info));
854 spin_unlock(&li->lock);
855
856 ilen = pgm_info.flags & KVM_S390_PGM_FLAGS_ILC_MASK;
857 VCPU_EVENT(vcpu, 3, "deliver: program irq code 0x%x, ilen:%d",
858 pgm_info.code, ilen);
859 vcpu->stat.deliver_program++;
860 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
861 pgm_info.code, 0);
862
863 /* PER is handled by the ultravisor */
864 if (kvm_s390_pv_cpu_is_protected(vcpu))
865 return __deliver_prog_pv(vcpu, pgm_info.code & ~PGM_PER);
866
867 switch (pgm_info.code & ~PGM_PER) {
868 case PGM_AFX_TRANSLATION:
869 case PGM_ASX_TRANSLATION:
870 case PGM_EX_TRANSLATION:
871 case PGM_LFX_TRANSLATION:
872 case PGM_LSTE_SEQUENCE:
873 case PGM_LSX_TRANSLATION:
874 case PGM_LX_TRANSLATION:
875 case PGM_PRIMARY_AUTHORITY:
876 case PGM_SECONDARY_AUTHORITY:
877 nullifying = true;
878 fallthrough;
879 case PGM_SPACE_SWITCH:
880 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
881 (u64 *)__LC_TRANS_EXC_CODE);
882 break;
883 case PGM_ALEN_TRANSLATION:
884 case PGM_ALE_SEQUENCE:
885 case PGM_ASTE_INSTANCE:
886 case PGM_ASTE_SEQUENCE:
887 case PGM_ASTE_VALIDITY:
888 case PGM_EXTENDED_AUTHORITY:
889 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
890 (u8 *)__LC_EXC_ACCESS_ID);
891 nullifying = true;
892 break;
893 case PGM_ASCE_TYPE:
894 case PGM_PAGE_TRANSLATION:
895 case PGM_REGION_FIRST_TRANS:
896 case PGM_REGION_SECOND_TRANS:
897 case PGM_REGION_THIRD_TRANS:
898 case PGM_SEGMENT_TRANSLATION:
899 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
900 (u64 *)__LC_TRANS_EXC_CODE);
901 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
902 (u8 *)__LC_EXC_ACCESS_ID);
903 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
904 (u8 *)__LC_OP_ACCESS_ID);
905 nullifying = true;
906 break;
907 case PGM_MONITOR:
908 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
909 (u16 *)__LC_MON_CLASS_NR);
910 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
911 (u64 *)__LC_MON_CODE);
912 break;
913 case PGM_VECTOR_PROCESSING:
914 case PGM_DATA:
915 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
916 (u32 *)__LC_DATA_EXC_CODE);
917 break;
918 case PGM_PROTECTION:
919 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
920 (u64 *)__LC_TRANS_EXC_CODE);
921 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
922 (u8 *)__LC_EXC_ACCESS_ID);
923 break;
924 case PGM_STACK_FULL:
925 case PGM_STACK_EMPTY:
926 case PGM_STACK_SPECIFICATION:
927 case PGM_STACK_TYPE:
928 case PGM_STACK_OPERATION:
929 case PGM_TRACE_TABEL:
930 case PGM_CRYPTO_OPERATION:
931 nullifying = true;
932 break;
933 }
934
935 if (pgm_info.code & PGM_PER) {
936 rc |= put_guest_lc(vcpu, pgm_info.per_code,
937 (u8 *) __LC_PER_CODE);
938 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
939 (u8 *)__LC_PER_ATMID);
940 rc |= put_guest_lc(vcpu, pgm_info.per_address,
941 (u64 *) __LC_PER_ADDRESS);
942 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
943 (u8 *) __LC_PER_ACCESS_ID);
944 }
945
946 if (nullifying && !(pgm_info.flags & KVM_S390_PGM_FLAGS_NO_REWIND))
947 kvm_s390_rewind_psw(vcpu, ilen);
948
949 /* bit 1+2 of the target are the ilc, so we can directly use ilen */
950 rc |= put_guest_lc(vcpu, ilen, (u16 *) __LC_PGM_ILC);
951 rc |= put_guest_lc(vcpu, vcpu->arch.sie_block->gbea,
952 (u64 *) __LC_PGM_LAST_BREAK);
953 rc |= put_guest_lc(vcpu, pgm_info.code, (u16 *)__LC_PGM_CODE);
954 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
955 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
956 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
957 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
958 return rc ? -EFAULT : 0;
959 }
960
961 #define SCCB_MASK 0xFFFFFFF8
962 #define SCCB_EVENT_PENDING 0x3
963
write_sclp(struct kvm_vcpu * vcpu,u32 parm)964 static int write_sclp(struct kvm_vcpu *vcpu, u32 parm)
965 {
966 int rc;
967
968 if (kvm_s390_pv_cpu_get_handle(vcpu)) {
969 vcpu->arch.sie_block->iictl = IICTL_CODE_EXT;
970 vcpu->arch.sie_block->eic = EXT_IRQ_SERVICE_SIG;
971 vcpu->arch.sie_block->eiparams = parm;
972 return 0;
973 }
974
975 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
976 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
977 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
978 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
979 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
980 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
981 rc |= put_guest_lc(vcpu, parm,
982 (u32 *)__LC_EXT_PARAMS);
983
984 return rc ? -EFAULT : 0;
985 }
986
__deliver_service(struct kvm_vcpu * vcpu)987 static int __must_check __deliver_service(struct kvm_vcpu *vcpu)
988 {
989 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
990 struct kvm_s390_ext_info ext;
991
992 spin_lock(&fi->lock);
993 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs) ||
994 !(test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs))) {
995 spin_unlock(&fi->lock);
996 return 0;
997 }
998 ext = fi->srv_signal;
999 memset(&fi->srv_signal, 0, sizeof(ext));
1000 clear_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1001 clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1002 if (kvm_s390_pv_cpu_is_protected(vcpu))
1003 set_bit(IRQ_PEND_EXT_SERVICE, &fi->masked_irqs);
1004 spin_unlock(&fi->lock);
1005
1006 VCPU_EVENT(vcpu, 4, "deliver: sclp parameter 0x%x",
1007 ext.ext_params);
1008 vcpu->stat.deliver_service_signal++;
1009 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
1010 ext.ext_params, 0);
1011
1012 return write_sclp(vcpu, ext.ext_params);
1013 }
1014
__deliver_service_ev(struct kvm_vcpu * vcpu)1015 static int __must_check __deliver_service_ev(struct kvm_vcpu *vcpu)
1016 {
1017 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
1018 struct kvm_s390_ext_info ext;
1019
1020 spin_lock(&fi->lock);
1021 if (!(test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs))) {
1022 spin_unlock(&fi->lock);
1023 return 0;
1024 }
1025 ext = fi->srv_signal;
1026 /* only clear the event bits */
1027 fi->srv_signal.ext_params &= ~SCCB_EVENT_PENDING;
1028 clear_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1029 spin_unlock(&fi->lock);
1030
1031 VCPU_EVENT(vcpu, 4, "%s", "deliver: sclp parameter event");
1032 vcpu->stat.deliver_service_signal++;
1033 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_SERVICE,
1034 ext.ext_params, 0);
1035
1036 return write_sclp(vcpu, ext.ext_params & SCCB_EVENT_PENDING);
1037 }
1038
__deliver_pfault_done(struct kvm_vcpu * vcpu)1039 static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu)
1040 {
1041 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
1042 struct kvm_s390_interrupt_info *inti;
1043 int rc = 0;
1044
1045 spin_lock(&fi->lock);
1046 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_PFAULT],
1047 struct kvm_s390_interrupt_info,
1048 list);
1049 if (inti) {
1050 list_del(&inti->list);
1051 fi->counters[FIRQ_CNTR_PFAULT] -= 1;
1052 }
1053 if (list_empty(&fi->lists[FIRQ_LIST_PFAULT]))
1054 clear_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1055 spin_unlock(&fi->lock);
1056
1057 if (inti) {
1058 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1059 KVM_S390_INT_PFAULT_DONE, 0,
1060 inti->ext.ext_params2);
1061 VCPU_EVENT(vcpu, 4, "deliver: pfault done token 0x%llx",
1062 inti->ext.ext_params2);
1063
1064 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
1065 (u16 *)__LC_EXT_INT_CODE);
1066 rc |= put_guest_lc(vcpu, PFAULT_DONE,
1067 (u16 *)__LC_EXT_CPU_ADDR);
1068 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
1069 &vcpu->arch.sie_block->gpsw,
1070 sizeof(psw_t));
1071 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
1072 &vcpu->arch.sie_block->gpsw,
1073 sizeof(psw_t));
1074 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
1075 (u64 *)__LC_EXT_PARAMS2);
1076 kfree(inti);
1077 }
1078 return rc ? -EFAULT : 0;
1079 }
1080
__deliver_virtio(struct kvm_vcpu * vcpu)1081 static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu)
1082 {
1083 struct kvm_s390_float_interrupt *fi = &vcpu->kvm->arch.float_int;
1084 struct kvm_s390_interrupt_info *inti;
1085 int rc = 0;
1086
1087 spin_lock(&fi->lock);
1088 inti = list_first_entry_or_null(&fi->lists[FIRQ_LIST_VIRTIO],
1089 struct kvm_s390_interrupt_info,
1090 list);
1091 if (inti) {
1092 VCPU_EVENT(vcpu, 4,
1093 "deliver: virtio parm: 0x%x,parm64: 0x%llx",
1094 inti->ext.ext_params, inti->ext.ext_params2);
1095 vcpu->stat.deliver_virtio++;
1096 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1097 inti->type,
1098 inti->ext.ext_params,
1099 inti->ext.ext_params2);
1100 list_del(&inti->list);
1101 fi->counters[FIRQ_CNTR_VIRTIO] -= 1;
1102 }
1103 if (list_empty(&fi->lists[FIRQ_LIST_VIRTIO]))
1104 clear_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1105 spin_unlock(&fi->lock);
1106
1107 if (inti) {
1108 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE,
1109 (u16 *)__LC_EXT_INT_CODE);
1110 rc |= put_guest_lc(vcpu, VIRTIO_PARAM,
1111 (u16 *)__LC_EXT_CPU_ADDR);
1112 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
1113 &vcpu->arch.sie_block->gpsw,
1114 sizeof(psw_t));
1115 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
1116 &vcpu->arch.sie_block->gpsw,
1117 sizeof(psw_t));
1118 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
1119 (u32 *)__LC_EXT_PARAMS);
1120 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
1121 (u64 *)__LC_EXT_PARAMS2);
1122 kfree(inti);
1123 }
1124 return rc ? -EFAULT : 0;
1125 }
1126
__do_deliver_io(struct kvm_vcpu * vcpu,struct kvm_s390_io_info * io)1127 static int __do_deliver_io(struct kvm_vcpu *vcpu, struct kvm_s390_io_info *io)
1128 {
1129 int rc;
1130
1131 if (kvm_s390_pv_cpu_is_protected(vcpu)) {
1132 vcpu->arch.sie_block->iictl = IICTL_CODE_IO;
1133 vcpu->arch.sie_block->subchannel_id = io->subchannel_id;
1134 vcpu->arch.sie_block->subchannel_nr = io->subchannel_nr;
1135 vcpu->arch.sie_block->io_int_parm = io->io_int_parm;
1136 vcpu->arch.sie_block->io_int_word = io->io_int_word;
1137 return 0;
1138 }
1139
1140 rc = put_guest_lc(vcpu, io->subchannel_id, (u16 *)__LC_SUBCHANNEL_ID);
1141 rc |= put_guest_lc(vcpu, io->subchannel_nr, (u16 *)__LC_SUBCHANNEL_NR);
1142 rc |= put_guest_lc(vcpu, io->io_int_parm, (u32 *)__LC_IO_INT_PARM);
1143 rc |= put_guest_lc(vcpu, io->io_int_word, (u32 *)__LC_IO_INT_WORD);
1144 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
1145 &vcpu->arch.sie_block->gpsw,
1146 sizeof(psw_t));
1147 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
1148 &vcpu->arch.sie_block->gpsw,
1149 sizeof(psw_t));
1150 return rc ? -EFAULT : 0;
1151 }
1152
__deliver_io(struct kvm_vcpu * vcpu,unsigned long irq_type)1153 static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
1154 unsigned long irq_type)
1155 {
1156 struct list_head *isc_list;
1157 struct kvm_s390_float_interrupt *fi;
1158 struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
1159 struct kvm_s390_interrupt_info *inti = NULL;
1160 struct kvm_s390_io_info io;
1161 u32 isc;
1162 int rc = 0;
1163
1164 fi = &vcpu->kvm->arch.float_int;
1165
1166 spin_lock(&fi->lock);
1167 isc = irq_type_to_isc(irq_type);
1168 isc_list = &fi->lists[isc];
1169 inti = list_first_entry_or_null(isc_list,
1170 struct kvm_s390_interrupt_info,
1171 list);
1172 if (inti) {
1173 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1174 VCPU_EVENT(vcpu, 4, "%s", "deliver: I/O (AI)");
1175 else
1176 VCPU_EVENT(vcpu, 4, "deliver: I/O %x ss %x schid %04x",
1177 inti->io.subchannel_id >> 8,
1178 inti->io.subchannel_id >> 1 & 0x3,
1179 inti->io.subchannel_nr);
1180
1181 vcpu->stat.deliver_io++;
1182 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1183 inti->type,
1184 ((__u32)inti->io.subchannel_id << 16) |
1185 inti->io.subchannel_nr,
1186 ((__u64)inti->io.io_int_parm << 32) |
1187 inti->io.io_int_word);
1188 list_del(&inti->list);
1189 fi->counters[FIRQ_CNTR_IO] -= 1;
1190 }
1191 if (list_empty(isc_list))
1192 clear_bit(irq_type, &fi->pending_irqs);
1193 spin_unlock(&fi->lock);
1194
1195 if (inti) {
1196 rc = __do_deliver_io(vcpu, &(inti->io));
1197 kfree(inti);
1198 goto out;
1199 }
1200
1201 if (gi->origin && gisa_tac_ipm_gisc(gi->origin, isc)) {
1202 /*
1203 * in case an adapter interrupt was not delivered
1204 * in SIE context KVM will handle the delivery
1205 */
1206 VCPU_EVENT(vcpu, 4, "%s isc %u", "deliver: I/O (AI/gisa)", isc);
1207 memset(&io, 0, sizeof(io));
1208 io.io_int_word = isc_to_int_word(isc);
1209 vcpu->stat.deliver_io++;
1210 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
1211 KVM_S390_INT_IO(1, 0, 0, 0),
1212 ((__u32)io.subchannel_id << 16) |
1213 io.subchannel_nr,
1214 ((__u64)io.io_int_parm << 32) |
1215 io.io_int_word);
1216 rc = __do_deliver_io(vcpu, &io);
1217 }
1218 out:
1219 return rc;
1220 }
1221
1222 /* Check whether an external call is pending (deliverable or not) */
kvm_s390_ext_call_pending(struct kvm_vcpu * vcpu)1223 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
1224 {
1225 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1226
1227 if (!sclp.has_sigpif)
1228 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
1229
1230 return sca_ext_call_pending(vcpu, NULL);
1231 }
1232
kvm_s390_vcpu_has_irq(struct kvm_vcpu * vcpu,int exclude_stop)1233 int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
1234 {
1235 if (deliverable_irqs(vcpu))
1236 return 1;
1237
1238 if (kvm_cpu_has_pending_timer(vcpu))
1239 return 1;
1240
1241 /* external call pending and deliverable */
1242 if (kvm_s390_ext_call_pending(vcpu) &&
1243 !psw_extint_disabled(vcpu) &&
1244 (vcpu->arch.sie_block->gcr[0] & CR0_EXTERNAL_CALL_SUBMASK))
1245 return 1;
1246
1247 if (!exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
1248 return 1;
1249 return 0;
1250 }
1251
kvm_cpu_has_pending_timer(struct kvm_vcpu * vcpu)1252 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
1253 {
1254 return ckc_irq_pending(vcpu) || cpu_timer_irq_pending(vcpu);
1255 }
1256
__calculate_sltime(struct kvm_vcpu * vcpu)1257 static u64 __calculate_sltime(struct kvm_vcpu *vcpu)
1258 {
1259 const u64 now = kvm_s390_get_tod_clock_fast(vcpu->kvm);
1260 const u64 ckc = vcpu->arch.sie_block->ckc;
1261 u64 cputm, sltime = 0;
1262
1263 if (ckc_interrupts_enabled(vcpu)) {
1264 if (vcpu->arch.sie_block->gcr[0] & CR0_CLOCK_COMPARATOR_SIGN) {
1265 if ((s64)now < (s64)ckc)
1266 sltime = tod_to_ns((s64)ckc - (s64)now);
1267 } else if (now < ckc) {
1268 sltime = tod_to_ns(ckc - now);
1269 }
1270 /* already expired */
1271 if (!sltime)
1272 return 0;
1273 if (cpu_timer_interrupts_enabled(vcpu)) {
1274 cputm = kvm_s390_get_cpu_timer(vcpu);
1275 /* already expired? */
1276 if (cputm >> 63)
1277 return 0;
1278 return min_t(u64, sltime, tod_to_ns(cputm));
1279 }
1280 } else if (cpu_timer_interrupts_enabled(vcpu)) {
1281 sltime = kvm_s390_get_cpu_timer(vcpu);
1282 /* already expired? */
1283 if (sltime >> 63)
1284 return 0;
1285 }
1286 return sltime;
1287 }
1288
kvm_s390_handle_wait(struct kvm_vcpu * vcpu)1289 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
1290 {
1291 struct kvm_s390_gisa_interrupt *gi = &vcpu->kvm->arch.gisa_int;
1292 u64 sltime;
1293
1294 vcpu->stat.exit_wait_state++;
1295
1296 /* fast path */
1297 if (kvm_arch_vcpu_runnable(vcpu))
1298 return 0;
1299
1300 if (psw_interrupts_disabled(vcpu)) {
1301 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
1302 return -EOPNOTSUPP; /* disabled wait */
1303 }
1304
1305 if (gi->origin &&
1306 (gisa_get_ipm_or_restore_iam(gi) &
1307 vcpu->arch.sie_block->gcr[6] >> 24))
1308 return 0;
1309
1310 if (!ckc_interrupts_enabled(vcpu) &&
1311 !cpu_timer_interrupts_enabled(vcpu)) {
1312 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
1313 __set_cpu_idle(vcpu);
1314 goto no_timer;
1315 }
1316
1317 sltime = __calculate_sltime(vcpu);
1318 if (!sltime)
1319 return 0;
1320
1321 __set_cpu_idle(vcpu);
1322 hrtimer_start(&vcpu->arch.ckc_timer, sltime, HRTIMER_MODE_REL);
1323 VCPU_EVENT(vcpu, 4, "enabled wait: %llu ns", sltime);
1324 no_timer:
1325 kvm_vcpu_srcu_read_unlock(vcpu);
1326 kvm_vcpu_halt(vcpu);
1327 vcpu->valid_wakeup = false;
1328 __unset_cpu_idle(vcpu);
1329 kvm_vcpu_srcu_read_lock(vcpu);
1330
1331 hrtimer_cancel(&vcpu->arch.ckc_timer);
1332 return 0;
1333 }
1334
kvm_s390_vcpu_wakeup(struct kvm_vcpu * vcpu)1335 void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
1336 {
1337 vcpu->valid_wakeup = true;
1338 kvm_vcpu_wake_up(vcpu);
1339
1340 /*
1341 * The VCPU might not be sleeping but rather executing VSIE. Let's
1342 * kick it, so it leaves the SIE to process the request.
1343 */
1344 kvm_s390_vsie_kick(vcpu);
1345 }
1346
kvm_s390_idle_wakeup(struct hrtimer * timer)1347 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
1348 {
1349 struct kvm_vcpu *vcpu;
1350 u64 sltime;
1351
1352 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
1353 sltime = __calculate_sltime(vcpu);
1354
1355 /*
1356 * If the monotonic clock runs faster than the tod clock we might be
1357 * woken up too early and have to go back to sleep to avoid deadlocks.
1358 */
1359 if (sltime && hrtimer_forward_now(timer, ns_to_ktime(sltime)))
1360 return HRTIMER_RESTART;
1361 kvm_s390_vcpu_wakeup(vcpu);
1362 return HRTIMER_NORESTART;
1363 }
1364
kvm_s390_clear_local_irqs(struct kvm_vcpu * vcpu)1365 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
1366 {
1367 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1368
1369 spin_lock(&li->lock);
1370 li->pending_irqs = 0;
1371 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
1372 memset(&li->irq, 0, sizeof(li->irq));
1373 spin_unlock(&li->lock);
1374
1375 sca_clear_ext_call(vcpu);
1376 }
1377
kvm_s390_deliver_pending_interrupts(struct kvm_vcpu * vcpu)1378 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
1379 {
1380 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1381 int rc = 0;
1382 bool delivered = false;
1383 unsigned long irq_type;
1384 unsigned long irqs;
1385
1386 __reset_intercept_indicators(vcpu);
1387
1388 /* pending ckc conditions might have been invalidated */
1389 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1390 if (ckc_irq_pending(vcpu))
1391 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1392
1393 /* pending cpu timer conditions might have been invalidated */
1394 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1395 if (cpu_timer_irq_pending(vcpu))
1396 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1397
1398 while ((irqs = deliverable_irqs(vcpu)) && !rc) {
1399 /* bits are in the reverse order of interrupt priority */
1400 irq_type = find_last_bit(&irqs, IRQ_PEND_COUNT);
1401 switch (irq_type) {
1402 case IRQ_PEND_IO_ISC_0:
1403 case IRQ_PEND_IO_ISC_1:
1404 case IRQ_PEND_IO_ISC_2:
1405 case IRQ_PEND_IO_ISC_3:
1406 case IRQ_PEND_IO_ISC_4:
1407 case IRQ_PEND_IO_ISC_5:
1408 case IRQ_PEND_IO_ISC_6:
1409 case IRQ_PEND_IO_ISC_7:
1410 rc = __deliver_io(vcpu, irq_type);
1411 break;
1412 case IRQ_PEND_MCHK_EX:
1413 case IRQ_PEND_MCHK_REP:
1414 rc = __deliver_machine_check(vcpu);
1415 break;
1416 case IRQ_PEND_PROG:
1417 rc = __deliver_prog(vcpu);
1418 break;
1419 case IRQ_PEND_EXT_EMERGENCY:
1420 rc = __deliver_emergency_signal(vcpu);
1421 break;
1422 case IRQ_PEND_EXT_EXTERNAL:
1423 rc = __deliver_external_call(vcpu);
1424 break;
1425 case IRQ_PEND_EXT_CLOCK_COMP:
1426 rc = __deliver_ckc(vcpu);
1427 break;
1428 case IRQ_PEND_EXT_CPU_TIMER:
1429 rc = __deliver_cpu_timer(vcpu);
1430 break;
1431 case IRQ_PEND_RESTART:
1432 rc = __deliver_restart(vcpu);
1433 break;
1434 case IRQ_PEND_SET_PREFIX:
1435 rc = __deliver_set_prefix(vcpu);
1436 break;
1437 case IRQ_PEND_PFAULT_INIT:
1438 rc = __deliver_pfault_init(vcpu);
1439 break;
1440 case IRQ_PEND_EXT_SERVICE:
1441 rc = __deliver_service(vcpu);
1442 break;
1443 case IRQ_PEND_EXT_SERVICE_EV:
1444 rc = __deliver_service_ev(vcpu);
1445 break;
1446 case IRQ_PEND_PFAULT_DONE:
1447 rc = __deliver_pfault_done(vcpu);
1448 break;
1449 case IRQ_PEND_VIRTIO:
1450 rc = __deliver_virtio(vcpu);
1451 break;
1452 default:
1453 WARN_ONCE(1, "Unknown pending irq type %ld", irq_type);
1454 clear_bit(irq_type, &li->pending_irqs);
1455 }
1456 delivered |= !rc;
1457 }
1458
1459 /*
1460 * We delivered at least one interrupt and modified the PC. Force a
1461 * singlestep event now.
1462 */
1463 if (delivered && guestdbg_sstep_enabled(vcpu)) {
1464 struct kvm_debug_exit_arch *debug_exit = &vcpu->run->debug.arch;
1465
1466 debug_exit->addr = vcpu->arch.sie_block->gpsw.addr;
1467 debug_exit->type = KVM_SINGLESTEP;
1468 vcpu->guest_debug |= KVM_GUESTDBG_EXIT_PENDING;
1469 }
1470
1471 set_intercept_indicators(vcpu);
1472
1473 return rc;
1474 }
1475
__inject_prog(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)1476 static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1477 {
1478 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1479
1480 vcpu->stat.inject_program++;
1481 VCPU_EVENT(vcpu, 3, "inject: program irq code 0x%x", irq->u.pgm.code);
1482 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
1483 irq->u.pgm.code, 0);
1484
1485 if (!(irq->u.pgm.flags & KVM_S390_PGM_FLAGS_ILC_VALID)) {
1486 /* auto detection if no valid ILC was given */
1487 irq->u.pgm.flags &= ~KVM_S390_PGM_FLAGS_ILC_MASK;
1488 irq->u.pgm.flags |= kvm_s390_get_ilen(vcpu);
1489 irq->u.pgm.flags |= KVM_S390_PGM_FLAGS_ILC_VALID;
1490 }
1491
1492 if (irq->u.pgm.code == PGM_PER) {
1493 li->irq.pgm.code |= PGM_PER;
1494 li->irq.pgm.flags = irq->u.pgm.flags;
1495 /* only modify PER related information */
1496 li->irq.pgm.per_address = irq->u.pgm.per_address;
1497 li->irq.pgm.per_code = irq->u.pgm.per_code;
1498 li->irq.pgm.per_atmid = irq->u.pgm.per_atmid;
1499 li->irq.pgm.per_access_id = irq->u.pgm.per_access_id;
1500 } else if (!(irq->u.pgm.code & PGM_PER)) {
1501 li->irq.pgm.code = (li->irq.pgm.code & PGM_PER) |
1502 irq->u.pgm.code;
1503 li->irq.pgm.flags = irq->u.pgm.flags;
1504 /* only modify non-PER information */
1505 li->irq.pgm.trans_exc_code = irq->u.pgm.trans_exc_code;
1506 li->irq.pgm.mon_code = irq->u.pgm.mon_code;
1507 li->irq.pgm.data_exc_code = irq->u.pgm.data_exc_code;
1508 li->irq.pgm.mon_class_nr = irq->u.pgm.mon_class_nr;
1509 li->irq.pgm.exc_access_id = irq->u.pgm.exc_access_id;
1510 li->irq.pgm.op_access_id = irq->u.pgm.op_access_id;
1511 } else {
1512 li->irq.pgm = irq->u.pgm;
1513 }
1514 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
1515 return 0;
1516 }
1517
__inject_pfault_init(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)1518 static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1519 {
1520 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1521
1522 vcpu->stat.inject_pfault_init++;
1523 VCPU_EVENT(vcpu, 4, "inject: pfault init parameter block at 0x%llx",
1524 irq->u.ext.ext_params2);
1525 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1526 irq->u.ext.ext_params,
1527 irq->u.ext.ext_params2);
1528
1529 li->irq.ext = irq->u.ext;
1530 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
1531 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1532 return 0;
1533 }
1534
__inject_extcall(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)1535 static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1536 {
1537 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1538 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
1539 uint16_t src_id = irq->u.extcall.code;
1540
1541 vcpu->stat.inject_external_call++;
1542 VCPU_EVENT(vcpu, 4, "inject: external call source-cpu:%u",
1543 src_id);
1544 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
1545 src_id, 0);
1546
1547 /* sending vcpu invalid */
1548 if (kvm_get_vcpu_by_id(vcpu->kvm, src_id) == NULL)
1549 return -EINVAL;
1550
1551 if (sclp.has_sigpif && !kvm_s390_pv_cpu_get_handle(vcpu))
1552 return sca_inject_ext_call(vcpu, src_id);
1553
1554 if (test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1555 return -EBUSY;
1556 *extcall = irq->u.extcall;
1557 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1558 return 0;
1559 }
1560
__inject_set_prefix(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)1561 static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1562 {
1563 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1564 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
1565
1566 vcpu->stat.inject_set_prefix++;
1567 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x",
1568 irq->u.prefix.address);
1569 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
1570 irq->u.prefix.address, 0);
1571
1572 if (!is_vcpu_stopped(vcpu))
1573 return -EBUSY;
1574
1575 *prefix = irq->u.prefix;
1576 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
1577 return 0;
1578 }
1579
1580 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
__inject_sigp_stop(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)1581 static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1582 {
1583 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1584 struct kvm_s390_stop_info *stop = &li->irq.stop;
1585 int rc = 0;
1586
1587 vcpu->stat.inject_stop_signal++;
1588 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0);
1589
1590 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1591 return -EINVAL;
1592
1593 if (is_vcpu_stopped(vcpu)) {
1594 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1595 rc = kvm_s390_store_status_unloaded(vcpu,
1596 KVM_S390_STORE_STATUS_NOADDR);
1597 return rc;
1598 }
1599
1600 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1601 return -EBUSY;
1602 stop->flags = irq->u.stop.flags;
1603 kvm_s390_set_cpuflags(vcpu, CPUSTAT_STOP_INT);
1604 return 0;
1605 }
1606
__inject_sigp_restart(struct kvm_vcpu * vcpu)1607 static int __inject_sigp_restart(struct kvm_vcpu *vcpu)
1608 {
1609 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1610
1611 vcpu->stat.inject_restart++;
1612 VCPU_EVENT(vcpu, 3, "%s", "inject: restart int");
1613 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
1614
1615 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
1616 return 0;
1617 }
1618
__inject_sigp_emergency(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)1619 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
1620 struct kvm_s390_irq *irq)
1621 {
1622 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1623
1624 vcpu->stat.inject_emergency_signal++;
1625 VCPU_EVENT(vcpu, 4, "inject: emergency from cpu %u",
1626 irq->u.emerg.code);
1627 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
1628 irq->u.emerg.code, 0);
1629
1630 /* sending vcpu invalid */
1631 if (kvm_get_vcpu_by_id(vcpu->kvm, irq->u.emerg.code) == NULL)
1632 return -EINVAL;
1633
1634 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
1635 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
1636 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1637 return 0;
1638 }
1639
__inject_mchk(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)1640 static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1641 {
1642 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1643 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
1644
1645 vcpu->stat.inject_mchk++;
1646 VCPU_EVENT(vcpu, 3, "inject: machine check mcic 0x%llx",
1647 irq->u.mchk.mcic);
1648 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
1649 irq->u.mchk.mcic);
1650
1651 /*
1652 * Because repressible machine checks can be indicated along with
1653 * exigent machine checks (PoP, Chapter 11, Interruption action)
1654 * we need to combine cr14, mcic and external damage code.
1655 * Failing storage address and the logout area should not be or'ed
1656 * together, we just indicate the last occurrence of the corresponding
1657 * machine check
1658 */
1659 mchk->cr14 |= irq->u.mchk.cr14;
1660 mchk->mcic |= irq->u.mchk.mcic;
1661 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1662 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1663 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1664 sizeof(mchk->fixed_logout));
1665 if (mchk->mcic & MCHK_EX_MASK)
1666 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1667 else if (mchk->mcic & MCHK_REP_MASK)
1668 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
1669 return 0;
1670 }
1671
__inject_ckc(struct kvm_vcpu * vcpu)1672 static int __inject_ckc(struct kvm_vcpu *vcpu)
1673 {
1674 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1675
1676 vcpu->stat.inject_ckc++;
1677 VCPU_EVENT(vcpu, 3, "%s", "inject: clock comparator external");
1678 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1679 0, 0);
1680
1681 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1682 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1683 return 0;
1684 }
1685
__inject_cpu_timer(struct kvm_vcpu * vcpu)1686 static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
1687 {
1688 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1689
1690 vcpu->stat.inject_cputm++;
1691 VCPU_EVENT(vcpu, 3, "%s", "inject: cpu timer external");
1692 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1693 0, 0);
1694
1695 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1696 kvm_s390_set_cpuflags(vcpu, CPUSTAT_EXT_INT);
1697 return 0;
1698 }
1699
get_io_int(struct kvm * kvm,int isc,u32 schid)1700 static struct kvm_s390_interrupt_info *get_io_int(struct kvm *kvm,
1701 int isc, u32 schid)
1702 {
1703 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1704 struct list_head *isc_list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1705 struct kvm_s390_interrupt_info *iter;
1706 u16 id = (schid & 0xffff0000U) >> 16;
1707 u16 nr = schid & 0x0000ffffU;
1708
1709 spin_lock(&fi->lock);
1710 list_for_each_entry(iter, isc_list, list) {
1711 if (schid && (id != iter->io.subchannel_id ||
1712 nr != iter->io.subchannel_nr))
1713 continue;
1714 /* found an appropriate entry */
1715 list_del_init(&iter->list);
1716 fi->counters[FIRQ_CNTR_IO] -= 1;
1717 if (list_empty(isc_list))
1718 clear_bit(isc_to_irq_type(isc), &fi->pending_irqs);
1719 spin_unlock(&fi->lock);
1720 return iter;
1721 }
1722 spin_unlock(&fi->lock);
1723 return NULL;
1724 }
1725
get_top_io_int(struct kvm * kvm,u64 isc_mask,u32 schid)1726 static struct kvm_s390_interrupt_info *get_top_io_int(struct kvm *kvm,
1727 u64 isc_mask, u32 schid)
1728 {
1729 struct kvm_s390_interrupt_info *inti = NULL;
1730 int isc;
1731
1732 for (isc = 0; isc <= MAX_ISC && !inti; isc++) {
1733 if (isc_mask & isc_to_isc_bits(isc))
1734 inti = get_io_int(kvm, isc, schid);
1735 }
1736 return inti;
1737 }
1738
get_top_gisa_isc(struct kvm * kvm,u64 isc_mask,u32 schid)1739 static int get_top_gisa_isc(struct kvm *kvm, u64 isc_mask, u32 schid)
1740 {
1741 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1742 unsigned long active_mask;
1743 int isc;
1744
1745 if (schid)
1746 goto out;
1747 if (!gi->origin)
1748 goto out;
1749
1750 active_mask = (isc_mask & gisa_get_ipm(gi->origin) << 24) << 32;
1751 while (active_mask) {
1752 isc = __fls(active_mask) ^ (BITS_PER_LONG - 1);
1753 if (gisa_tac_ipm_gisc(gi->origin, isc))
1754 return isc;
1755 clear_bit_inv(isc, &active_mask);
1756 }
1757 out:
1758 return -EINVAL;
1759 }
1760
1761 /*
1762 * Dequeue and return an I/O interrupt matching any of the interruption
1763 * subclasses as designated by the isc mask in cr6 and the schid (if != 0).
1764 * Take into account the interrupts pending in the interrupt list and in GISA.
1765 *
1766 * Note that for a guest that does not enable I/O interrupts
1767 * but relies on TPI, a flood of classic interrupts may starve
1768 * out adapter interrupts on the same isc. Linux does not do
1769 * that, and it is possible to work around the issue by configuring
1770 * different iscs for classic and adapter interrupts in the guest,
1771 * but we may want to revisit this in the future.
1772 */
kvm_s390_get_io_int(struct kvm * kvm,u64 isc_mask,u32 schid)1773 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1774 u64 isc_mask, u32 schid)
1775 {
1776 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1777 struct kvm_s390_interrupt_info *inti, *tmp_inti;
1778 int isc;
1779
1780 inti = get_top_io_int(kvm, isc_mask, schid);
1781
1782 isc = get_top_gisa_isc(kvm, isc_mask, schid);
1783 if (isc < 0)
1784 /* no AI in GISA */
1785 goto out;
1786
1787 if (!inti)
1788 /* AI in GISA but no classical IO int */
1789 goto gisa_out;
1790
1791 /* both types of interrupts present */
1792 if (int_word_to_isc(inti->io.io_int_word) <= isc) {
1793 /* classical IO int with higher priority */
1794 gisa_set_ipm_gisc(gi->origin, isc);
1795 goto out;
1796 }
1797 gisa_out:
1798 tmp_inti = kzalloc(sizeof(*inti), GFP_KERNEL_ACCOUNT);
1799 if (tmp_inti) {
1800 tmp_inti->type = KVM_S390_INT_IO(1, 0, 0, 0);
1801 tmp_inti->io.io_int_word = isc_to_int_word(isc);
1802 if (inti)
1803 kvm_s390_reinject_io_int(kvm, inti);
1804 inti = tmp_inti;
1805 } else
1806 gisa_set_ipm_gisc(gi->origin, isc);
1807 out:
1808 return inti;
1809 }
1810
__inject_service(struct kvm * kvm,struct kvm_s390_interrupt_info * inti)1811 static int __inject_service(struct kvm *kvm,
1812 struct kvm_s390_interrupt_info *inti)
1813 {
1814 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1815
1816 kvm->stat.inject_service_signal++;
1817 spin_lock(&fi->lock);
1818 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_EVENT_PENDING;
1819
1820 /* We always allow events, track them separately from the sccb ints */
1821 if (fi->srv_signal.ext_params & SCCB_EVENT_PENDING)
1822 set_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs);
1823
1824 /*
1825 * Early versions of the QEMU s390 bios will inject several
1826 * service interrupts after another without handling a
1827 * condition code indicating busy.
1828 * We will silently ignore those superfluous sccb values.
1829 * A future version of QEMU will take care of serialization
1830 * of servc requests
1831 */
1832 if (fi->srv_signal.ext_params & SCCB_MASK)
1833 goto out;
1834 fi->srv_signal.ext_params |= inti->ext.ext_params & SCCB_MASK;
1835 set_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs);
1836 out:
1837 spin_unlock(&fi->lock);
1838 kfree(inti);
1839 return 0;
1840 }
1841
__inject_virtio(struct kvm * kvm,struct kvm_s390_interrupt_info * inti)1842 static int __inject_virtio(struct kvm *kvm,
1843 struct kvm_s390_interrupt_info *inti)
1844 {
1845 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1846
1847 kvm->stat.inject_virtio++;
1848 spin_lock(&fi->lock);
1849 if (fi->counters[FIRQ_CNTR_VIRTIO] >= KVM_S390_MAX_VIRTIO_IRQS) {
1850 spin_unlock(&fi->lock);
1851 return -EBUSY;
1852 }
1853 fi->counters[FIRQ_CNTR_VIRTIO] += 1;
1854 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_VIRTIO]);
1855 set_bit(IRQ_PEND_VIRTIO, &fi->pending_irqs);
1856 spin_unlock(&fi->lock);
1857 return 0;
1858 }
1859
__inject_pfault_done(struct kvm * kvm,struct kvm_s390_interrupt_info * inti)1860 static int __inject_pfault_done(struct kvm *kvm,
1861 struct kvm_s390_interrupt_info *inti)
1862 {
1863 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1864
1865 kvm->stat.inject_pfault_done++;
1866 spin_lock(&fi->lock);
1867 if (fi->counters[FIRQ_CNTR_PFAULT] >=
1868 (ASYNC_PF_PER_VCPU * KVM_MAX_VCPUS)) {
1869 spin_unlock(&fi->lock);
1870 return -EBUSY;
1871 }
1872 fi->counters[FIRQ_CNTR_PFAULT] += 1;
1873 list_add_tail(&inti->list, &fi->lists[FIRQ_LIST_PFAULT]);
1874 set_bit(IRQ_PEND_PFAULT_DONE, &fi->pending_irqs);
1875 spin_unlock(&fi->lock);
1876 return 0;
1877 }
1878
1879 #define CR_PENDING_SUBCLASS 28
__inject_float_mchk(struct kvm * kvm,struct kvm_s390_interrupt_info * inti)1880 static int __inject_float_mchk(struct kvm *kvm,
1881 struct kvm_s390_interrupt_info *inti)
1882 {
1883 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
1884
1885 kvm->stat.inject_float_mchk++;
1886 spin_lock(&fi->lock);
1887 fi->mchk.cr14 |= inti->mchk.cr14 & (1UL << CR_PENDING_SUBCLASS);
1888 fi->mchk.mcic |= inti->mchk.mcic;
1889 set_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs);
1890 spin_unlock(&fi->lock);
1891 kfree(inti);
1892 return 0;
1893 }
1894
__inject_io(struct kvm * kvm,struct kvm_s390_interrupt_info * inti)1895 static int __inject_io(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1896 {
1897 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
1898 struct kvm_s390_float_interrupt *fi;
1899 struct list_head *list;
1900 int isc;
1901
1902 kvm->stat.inject_io++;
1903 isc = int_word_to_isc(inti->io.io_int_word);
1904
1905 /*
1906 * We do not use the lock checking variant as this is just a
1907 * performance optimization and we do not hold the lock here.
1908 * This is ok as the code will pick interrupts from both "lists"
1909 * for delivery.
1910 */
1911 if (gi->origin && inti->type & KVM_S390_INT_IO_AI_MASK) {
1912 VM_EVENT(kvm, 4, "%s isc %1u", "inject: I/O (AI/gisa)", isc);
1913 gisa_set_ipm_gisc(gi->origin, isc);
1914 kfree(inti);
1915 return 0;
1916 }
1917
1918 fi = &kvm->arch.float_int;
1919 spin_lock(&fi->lock);
1920 if (fi->counters[FIRQ_CNTR_IO] >= KVM_S390_MAX_FLOAT_IRQS) {
1921 spin_unlock(&fi->lock);
1922 return -EBUSY;
1923 }
1924 fi->counters[FIRQ_CNTR_IO] += 1;
1925
1926 if (inti->type & KVM_S390_INT_IO_AI_MASK)
1927 VM_EVENT(kvm, 4, "%s", "inject: I/O (AI)");
1928 else
1929 VM_EVENT(kvm, 4, "inject: I/O %x ss %x schid %04x",
1930 inti->io.subchannel_id >> 8,
1931 inti->io.subchannel_id >> 1 & 0x3,
1932 inti->io.subchannel_nr);
1933 list = &fi->lists[FIRQ_LIST_IO_ISC_0 + isc];
1934 list_add_tail(&inti->list, list);
1935 set_bit(isc_to_irq_type(isc), &fi->pending_irqs);
1936 spin_unlock(&fi->lock);
1937 return 0;
1938 }
1939
1940 /*
1941 * Find a destination VCPU for a floating irq and kick it.
1942 */
__floating_irq_kick(struct kvm * kvm,u64 type)1943 static void __floating_irq_kick(struct kvm *kvm, u64 type)
1944 {
1945 struct kvm_vcpu *dst_vcpu;
1946 int sigcpu, online_vcpus, nr_tries = 0;
1947
1948 online_vcpus = atomic_read(&kvm->online_vcpus);
1949 if (!online_vcpus)
1950 return;
1951
1952 /* find idle VCPUs first, then round robin */
1953 sigcpu = find_first_bit(kvm->arch.idle_mask, online_vcpus);
1954 if (sigcpu == online_vcpus) {
1955 do {
1956 sigcpu = kvm->arch.float_int.next_rr_cpu++;
1957 kvm->arch.float_int.next_rr_cpu %= online_vcpus;
1958 /* avoid endless loops if all vcpus are stopped */
1959 if (nr_tries++ >= online_vcpus)
1960 return;
1961 } while (is_vcpu_stopped(kvm_get_vcpu(kvm, sigcpu)));
1962 }
1963 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1964
1965 /* make the VCPU drop out of the SIE, or wake it up if sleeping */
1966 switch (type) {
1967 case KVM_S390_MCHK:
1968 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_STOP_INT);
1969 break;
1970 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1971 if (!(type & KVM_S390_INT_IO_AI_MASK &&
1972 kvm->arch.gisa_int.origin) ||
1973 kvm_s390_pv_cpu_get_handle(dst_vcpu))
1974 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_IO_INT);
1975 break;
1976 default:
1977 kvm_s390_set_cpuflags(dst_vcpu, CPUSTAT_EXT_INT);
1978 break;
1979 }
1980 kvm_s390_vcpu_wakeup(dst_vcpu);
1981 }
1982
__inject_vm(struct kvm * kvm,struct kvm_s390_interrupt_info * inti)1983 static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1984 {
1985 u64 type = READ_ONCE(inti->type);
1986 int rc;
1987
1988 switch (type) {
1989 case KVM_S390_MCHK:
1990 rc = __inject_float_mchk(kvm, inti);
1991 break;
1992 case KVM_S390_INT_VIRTIO:
1993 rc = __inject_virtio(kvm, inti);
1994 break;
1995 case KVM_S390_INT_SERVICE:
1996 rc = __inject_service(kvm, inti);
1997 break;
1998 case KVM_S390_INT_PFAULT_DONE:
1999 rc = __inject_pfault_done(kvm, inti);
2000 break;
2001 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2002 rc = __inject_io(kvm, inti);
2003 break;
2004 default:
2005 rc = -EINVAL;
2006 }
2007 if (rc)
2008 return rc;
2009
2010 __floating_irq_kick(kvm, type);
2011 return 0;
2012 }
2013
kvm_s390_inject_vm(struct kvm * kvm,struct kvm_s390_interrupt * s390int)2014 int kvm_s390_inject_vm(struct kvm *kvm,
2015 struct kvm_s390_interrupt *s390int)
2016 {
2017 struct kvm_s390_interrupt_info *inti;
2018 int rc;
2019
2020 inti = kzalloc(sizeof(*inti), GFP_KERNEL_ACCOUNT);
2021 if (!inti)
2022 return -ENOMEM;
2023
2024 inti->type = s390int->type;
2025 switch (inti->type) {
2026 case KVM_S390_INT_VIRTIO:
2027 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
2028 s390int->parm, s390int->parm64);
2029 inti->ext.ext_params = s390int->parm;
2030 inti->ext.ext_params2 = s390int->parm64;
2031 break;
2032 case KVM_S390_INT_SERVICE:
2033 VM_EVENT(kvm, 4, "inject: sclp parm:%x", s390int->parm);
2034 inti->ext.ext_params = s390int->parm;
2035 break;
2036 case KVM_S390_INT_PFAULT_DONE:
2037 inti->ext.ext_params2 = s390int->parm64;
2038 break;
2039 case KVM_S390_MCHK:
2040 VM_EVENT(kvm, 3, "inject: machine check mcic 0x%llx",
2041 s390int->parm64);
2042 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
2043 inti->mchk.mcic = s390int->parm64;
2044 break;
2045 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2046 inti->io.subchannel_id = s390int->parm >> 16;
2047 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
2048 inti->io.io_int_parm = s390int->parm64 >> 32;
2049 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
2050 break;
2051 default:
2052 kfree(inti);
2053 return -EINVAL;
2054 }
2055 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
2056 2);
2057
2058 rc = __inject_vm(kvm, inti);
2059 if (rc)
2060 kfree(inti);
2061 return rc;
2062 }
2063
kvm_s390_reinject_io_int(struct kvm * kvm,struct kvm_s390_interrupt_info * inti)2064 int kvm_s390_reinject_io_int(struct kvm *kvm,
2065 struct kvm_s390_interrupt_info *inti)
2066 {
2067 return __inject_vm(kvm, inti);
2068 }
2069
s390int_to_s390irq(struct kvm_s390_interrupt * s390int,struct kvm_s390_irq * irq)2070 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
2071 struct kvm_s390_irq *irq)
2072 {
2073 irq->type = s390int->type;
2074 switch (irq->type) {
2075 case KVM_S390_PROGRAM_INT:
2076 if (s390int->parm & 0xffff0000)
2077 return -EINVAL;
2078 irq->u.pgm.code = s390int->parm;
2079 break;
2080 case KVM_S390_SIGP_SET_PREFIX:
2081 irq->u.prefix.address = s390int->parm;
2082 break;
2083 case KVM_S390_SIGP_STOP:
2084 irq->u.stop.flags = s390int->parm;
2085 break;
2086 case KVM_S390_INT_EXTERNAL_CALL:
2087 if (s390int->parm & 0xffff0000)
2088 return -EINVAL;
2089 irq->u.extcall.code = s390int->parm;
2090 break;
2091 case KVM_S390_INT_EMERGENCY:
2092 if (s390int->parm & 0xffff0000)
2093 return -EINVAL;
2094 irq->u.emerg.code = s390int->parm;
2095 break;
2096 case KVM_S390_MCHK:
2097 irq->u.mchk.mcic = s390int->parm64;
2098 break;
2099 case KVM_S390_INT_PFAULT_INIT:
2100 irq->u.ext.ext_params = s390int->parm;
2101 irq->u.ext.ext_params2 = s390int->parm64;
2102 break;
2103 case KVM_S390_RESTART:
2104 case KVM_S390_INT_CLOCK_COMP:
2105 case KVM_S390_INT_CPU_TIMER:
2106 break;
2107 default:
2108 return -EINVAL;
2109 }
2110 return 0;
2111 }
2112
kvm_s390_is_stop_irq_pending(struct kvm_vcpu * vcpu)2113 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
2114 {
2115 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2116
2117 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
2118 }
2119
kvm_s390_is_restart_irq_pending(struct kvm_vcpu * vcpu)2120 int kvm_s390_is_restart_irq_pending(struct kvm_vcpu *vcpu)
2121 {
2122 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2123
2124 return test_bit(IRQ_PEND_RESTART, &li->pending_irqs);
2125 }
2126
kvm_s390_clear_stop_irq(struct kvm_vcpu * vcpu)2127 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
2128 {
2129 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2130
2131 spin_lock(&li->lock);
2132 li->irq.stop.flags = 0;
2133 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
2134 spin_unlock(&li->lock);
2135 }
2136
do_inject_vcpu(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)2137 static int do_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
2138 {
2139 int rc;
2140
2141 switch (irq->type) {
2142 case KVM_S390_PROGRAM_INT:
2143 rc = __inject_prog(vcpu, irq);
2144 break;
2145 case KVM_S390_SIGP_SET_PREFIX:
2146 rc = __inject_set_prefix(vcpu, irq);
2147 break;
2148 case KVM_S390_SIGP_STOP:
2149 rc = __inject_sigp_stop(vcpu, irq);
2150 break;
2151 case KVM_S390_RESTART:
2152 rc = __inject_sigp_restart(vcpu);
2153 break;
2154 case KVM_S390_INT_CLOCK_COMP:
2155 rc = __inject_ckc(vcpu);
2156 break;
2157 case KVM_S390_INT_CPU_TIMER:
2158 rc = __inject_cpu_timer(vcpu);
2159 break;
2160 case KVM_S390_INT_EXTERNAL_CALL:
2161 rc = __inject_extcall(vcpu, irq);
2162 break;
2163 case KVM_S390_INT_EMERGENCY:
2164 rc = __inject_sigp_emergency(vcpu, irq);
2165 break;
2166 case KVM_S390_MCHK:
2167 rc = __inject_mchk(vcpu, irq);
2168 break;
2169 case KVM_S390_INT_PFAULT_INIT:
2170 rc = __inject_pfault_init(vcpu, irq);
2171 break;
2172 case KVM_S390_INT_VIRTIO:
2173 case KVM_S390_INT_SERVICE:
2174 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2175 default:
2176 rc = -EINVAL;
2177 }
2178
2179 return rc;
2180 }
2181
kvm_s390_inject_vcpu(struct kvm_vcpu * vcpu,struct kvm_s390_irq * irq)2182 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
2183 {
2184 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2185 int rc;
2186
2187 spin_lock(&li->lock);
2188 rc = do_inject_vcpu(vcpu, irq);
2189 spin_unlock(&li->lock);
2190 if (!rc)
2191 kvm_s390_vcpu_wakeup(vcpu);
2192 return rc;
2193 }
2194
clear_irq_list(struct list_head * _list)2195 static inline void clear_irq_list(struct list_head *_list)
2196 {
2197 struct kvm_s390_interrupt_info *inti, *n;
2198
2199 list_for_each_entry_safe(inti, n, _list, list) {
2200 list_del(&inti->list);
2201 kfree(inti);
2202 }
2203 }
2204
inti_to_irq(struct kvm_s390_interrupt_info * inti,struct kvm_s390_irq * irq)2205 static void inti_to_irq(struct kvm_s390_interrupt_info *inti,
2206 struct kvm_s390_irq *irq)
2207 {
2208 irq->type = inti->type;
2209 switch (inti->type) {
2210 case KVM_S390_INT_PFAULT_INIT:
2211 case KVM_S390_INT_PFAULT_DONE:
2212 case KVM_S390_INT_VIRTIO:
2213 irq->u.ext = inti->ext;
2214 break;
2215 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2216 irq->u.io = inti->io;
2217 break;
2218 }
2219 }
2220
kvm_s390_clear_float_irqs(struct kvm * kvm)2221 void kvm_s390_clear_float_irqs(struct kvm *kvm)
2222 {
2223 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2224 int i;
2225
2226 mutex_lock(&kvm->lock);
2227 if (!kvm_s390_pv_is_protected(kvm))
2228 fi->masked_irqs = 0;
2229 mutex_unlock(&kvm->lock);
2230 spin_lock(&fi->lock);
2231 fi->pending_irqs = 0;
2232 memset(&fi->srv_signal, 0, sizeof(fi->srv_signal));
2233 memset(&fi->mchk, 0, sizeof(fi->mchk));
2234 for (i = 0; i < FIRQ_LIST_COUNT; i++)
2235 clear_irq_list(&fi->lists[i]);
2236 for (i = 0; i < FIRQ_MAX_COUNT; i++)
2237 fi->counters[i] = 0;
2238 spin_unlock(&fi->lock);
2239 kvm_s390_gisa_clear(kvm);
2240 };
2241
get_all_floating_irqs(struct kvm * kvm,u8 __user * usrbuf,u64 len)2242 static int get_all_floating_irqs(struct kvm *kvm, u8 __user *usrbuf, u64 len)
2243 {
2244 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
2245 struct kvm_s390_interrupt_info *inti;
2246 struct kvm_s390_float_interrupt *fi;
2247 struct kvm_s390_irq *buf;
2248 struct kvm_s390_irq *irq;
2249 int max_irqs;
2250 int ret = 0;
2251 int n = 0;
2252 int i;
2253
2254 if (len > KVM_S390_FLIC_MAX_BUFFER || len == 0)
2255 return -EINVAL;
2256
2257 /*
2258 * We are already using -ENOMEM to signal
2259 * userspace it may retry with a bigger buffer,
2260 * so we need to use something else for this case
2261 */
2262 buf = vzalloc(len);
2263 if (!buf)
2264 return -ENOBUFS;
2265
2266 max_irqs = len / sizeof(struct kvm_s390_irq);
2267
2268 if (gi->origin && gisa_get_ipm(gi->origin)) {
2269 for (i = 0; i <= MAX_ISC; i++) {
2270 if (n == max_irqs) {
2271 /* signal userspace to try again */
2272 ret = -ENOMEM;
2273 goto out_nolock;
2274 }
2275 if (gisa_tac_ipm_gisc(gi->origin, i)) {
2276 irq = (struct kvm_s390_irq *) &buf[n];
2277 irq->type = KVM_S390_INT_IO(1, 0, 0, 0);
2278 irq->u.io.io_int_word = isc_to_int_word(i);
2279 n++;
2280 }
2281 }
2282 }
2283 fi = &kvm->arch.float_int;
2284 spin_lock(&fi->lock);
2285 for (i = 0; i < FIRQ_LIST_COUNT; i++) {
2286 list_for_each_entry(inti, &fi->lists[i], list) {
2287 if (n == max_irqs) {
2288 /* signal userspace to try again */
2289 ret = -ENOMEM;
2290 goto out;
2291 }
2292 inti_to_irq(inti, &buf[n]);
2293 n++;
2294 }
2295 }
2296 if (test_bit(IRQ_PEND_EXT_SERVICE, &fi->pending_irqs) ||
2297 test_bit(IRQ_PEND_EXT_SERVICE_EV, &fi->pending_irqs)) {
2298 if (n == max_irqs) {
2299 /* signal userspace to try again */
2300 ret = -ENOMEM;
2301 goto out;
2302 }
2303 irq = (struct kvm_s390_irq *) &buf[n];
2304 irq->type = KVM_S390_INT_SERVICE;
2305 irq->u.ext = fi->srv_signal;
2306 n++;
2307 }
2308 if (test_bit(IRQ_PEND_MCHK_REP, &fi->pending_irqs)) {
2309 if (n == max_irqs) {
2310 /* signal userspace to try again */
2311 ret = -ENOMEM;
2312 goto out;
2313 }
2314 irq = (struct kvm_s390_irq *) &buf[n];
2315 irq->type = KVM_S390_MCHK;
2316 irq->u.mchk = fi->mchk;
2317 n++;
2318 }
2319
2320 out:
2321 spin_unlock(&fi->lock);
2322 out_nolock:
2323 if (!ret && n > 0) {
2324 if (copy_to_user(usrbuf, buf, sizeof(struct kvm_s390_irq) * n))
2325 ret = -EFAULT;
2326 }
2327 vfree(buf);
2328
2329 return ret < 0 ? ret : n;
2330 }
2331
flic_ais_mode_get_all(struct kvm * kvm,struct kvm_device_attr * attr)2332 static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr)
2333 {
2334 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2335 struct kvm_s390_ais_all ais;
2336
2337 if (attr->attr < sizeof(ais))
2338 return -EINVAL;
2339
2340 if (!test_kvm_facility(kvm, 72))
2341 return -EOPNOTSUPP;
2342
2343 mutex_lock(&fi->ais_lock);
2344 ais.simm = fi->simm;
2345 ais.nimm = fi->nimm;
2346 mutex_unlock(&fi->ais_lock);
2347
2348 if (copy_to_user((void __user *)attr->addr, &ais, sizeof(ais)))
2349 return -EFAULT;
2350
2351 return 0;
2352 }
2353
flic_get_attr(struct kvm_device * dev,struct kvm_device_attr * attr)2354 static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2355 {
2356 int r;
2357
2358 switch (attr->group) {
2359 case KVM_DEV_FLIC_GET_ALL_IRQS:
2360 r = get_all_floating_irqs(dev->kvm, (u8 __user *) attr->addr,
2361 attr->attr);
2362 break;
2363 case KVM_DEV_FLIC_AISM_ALL:
2364 r = flic_ais_mode_get_all(dev->kvm, attr);
2365 break;
2366 default:
2367 r = -EINVAL;
2368 }
2369
2370 return r;
2371 }
2372
copy_irq_from_user(struct kvm_s390_interrupt_info * inti,u64 addr)2373 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
2374 u64 addr)
2375 {
2376 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
2377 void *target = NULL;
2378 void __user *source;
2379 u64 size;
2380
2381 if (get_user(inti->type, (u64 __user *)addr))
2382 return -EFAULT;
2383
2384 switch (inti->type) {
2385 case KVM_S390_INT_PFAULT_INIT:
2386 case KVM_S390_INT_PFAULT_DONE:
2387 case KVM_S390_INT_VIRTIO:
2388 case KVM_S390_INT_SERVICE:
2389 target = (void *) &inti->ext;
2390 source = &uptr->u.ext;
2391 size = sizeof(inti->ext);
2392 break;
2393 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
2394 target = (void *) &inti->io;
2395 source = &uptr->u.io;
2396 size = sizeof(inti->io);
2397 break;
2398 case KVM_S390_MCHK:
2399 target = (void *) &inti->mchk;
2400 source = &uptr->u.mchk;
2401 size = sizeof(inti->mchk);
2402 break;
2403 default:
2404 return -EINVAL;
2405 }
2406
2407 if (copy_from_user(target, source, size))
2408 return -EFAULT;
2409
2410 return 0;
2411 }
2412
enqueue_floating_irq(struct kvm_device * dev,struct kvm_device_attr * attr)2413 static int enqueue_floating_irq(struct kvm_device *dev,
2414 struct kvm_device_attr *attr)
2415 {
2416 struct kvm_s390_interrupt_info *inti = NULL;
2417 int r = 0;
2418 int len = attr->attr;
2419
2420 if (len % sizeof(struct kvm_s390_irq) != 0)
2421 return -EINVAL;
2422 else if (len > KVM_S390_FLIC_MAX_BUFFER)
2423 return -EINVAL;
2424
2425 while (len >= sizeof(struct kvm_s390_irq)) {
2426 inti = kzalloc(sizeof(*inti), GFP_KERNEL_ACCOUNT);
2427 if (!inti)
2428 return -ENOMEM;
2429
2430 r = copy_irq_from_user(inti, attr->addr);
2431 if (r) {
2432 kfree(inti);
2433 return r;
2434 }
2435 r = __inject_vm(dev->kvm, inti);
2436 if (r) {
2437 kfree(inti);
2438 return r;
2439 }
2440 len -= sizeof(struct kvm_s390_irq);
2441 attr->addr += sizeof(struct kvm_s390_irq);
2442 }
2443
2444 return r;
2445 }
2446
get_io_adapter(struct kvm * kvm,unsigned int id)2447 static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
2448 {
2449 if (id >= MAX_S390_IO_ADAPTERS)
2450 return NULL;
2451 id = array_index_nospec(id, MAX_S390_IO_ADAPTERS);
2452 return kvm->arch.adapters[id];
2453 }
2454
register_io_adapter(struct kvm_device * dev,struct kvm_device_attr * attr)2455 static int register_io_adapter(struct kvm_device *dev,
2456 struct kvm_device_attr *attr)
2457 {
2458 struct s390_io_adapter *adapter;
2459 struct kvm_s390_io_adapter adapter_info;
2460
2461 if (copy_from_user(&adapter_info,
2462 (void __user *)attr->addr, sizeof(adapter_info)))
2463 return -EFAULT;
2464
2465 if (adapter_info.id >= MAX_S390_IO_ADAPTERS)
2466 return -EINVAL;
2467
2468 adapter_info.id = array_index_nospec(adapter_info.id,
2469 MAX_S390_IO_ADAPTERS);
2470
2471 if (dev->kvm->arch.adapters[adapter_info.id] != NULL)
2472 return -EINVAL;
2473
2474 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL_ACCOUNT);
2475 if (!adapter)
2476 return -ENOMEM;
2477
2478 adapter->id = adapter_info.id;
2479 adapter->isc = adapter_info.isc;
2480 adapter->maskable = adapter_info.maskable;
2481 adapter->masked = false;
2482 adapter->swap = adapter_info.swap;
2483 adapter->suppressible = (adapter_info.flags) &
2484 KVM_S390_ADAPTER_SUPPRESSIBLE;
2485 dev->kvm->arch.adapters[adapter->id] = adapter;
2486
2487 return 0;
2488 }
2489
kvm_s390_mask_adapter(struct kvm * kvm,unsigned int id,bool masked)2490 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
2491 {
2492 int ret;
2493 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2494
2495 if (!adapter || !adapter->maskable)
2496 return -EINVAL;
2497 ret = adapter->masked;
2498 adapter->masked = masked;
2499 return ret;
2500 }
2501
kvm_s390_destroy_adapters(struct kvm * kvm)2502 void kvm_s390_destroy_adapters(struct kvm *kvm)
2503 {
2504 int i;
2505
2506 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++)
2507 kfree(kvm->arch.adapters[i]);
2508 }
2509
modify_io_adapter(struct kvm_device * dev,struct kvm_device_attr * attr)2510 static int modify_io_adapter(struct kvm_device *dev,
2511 struct kvm_device_attr *attr)
2512 {
2513 struct kvm_s390_io_adapter_req req;
2514 struct s390_io_adapter *adapter;
2515 int ret;
2516
2517 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2518 return -EFAULT;
2519
2520 adapter = get_io_adapter(dev->kvm, req.id);
2521 if (!adapter)
2522 return -EINVAL;
2523 switch (req.type) {
2524 case KVM_S390_IO_ADAPTER_MASK:
2525 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
2526 if (ret > 0)
2527 ret = 0;
2528 break;
2529 /*
2530 * The following operations are no longer needed and therefore no-ops.
2531 * The gpa to hva translation is done when an IRQ route is set up. The
2532 * set_irq code uses get_user_pages_remote() to do the actual write.
2533 */
2534 case KVM_S390_IO_ADAPTER_MAP:
2535 case KVM_S390_IO_ADAPTER_UNMAP:
2536 ret = 0;
2537 break;
2538 default:
2539 ret = -EINVAL;
2540 }
2541
2542 return ret;
2543 }
2544
clear_io_irq(struct kvm * kvm,struct kvm_device_attr * attr)2545 static int clear_io_irq(struct kvm *kvm, struct kvm_device_attr *attr)
2546
2547 {
2548 const u64 isc_mask = 0xffUL << 24; /* all iscs set */
2549 u32 schid;
2550
2551 if (attr->flags)
2552 return -EINVAL;
2553 if (attr->attr != sizeof(schid))
2554 return -EINVAL;
2555 if (copy_from_user(&schid, (void __user *) attr->addr, sizeof(schid)))
2556 return -EFAULT;
2557 if (!schid)
2558 return -EINVAL;
2559 kfree(kvm_s390_get_io_int(kvm, isc_mask, schid));
2560 /*
2561 * If userspace is conforming to the architecture, we can have at most
2562 * one pending I/O interrupt per subchannel, so this is effectively a
2563 * clear all.
2564 */
2565 return 0;
2566 }
2567
modify_ais_mode(struct kvm * kvm,struct kvm_device_attr * attr)2568 static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
2569 {
2570 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2571 struct kvm_s390_ais_req req;
2572 int ret = 0;
2573
2574 if (!test_kvm_facility(kvm, 72))
2575 return -EOPNOTSUPP;
2576
2577 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
2578 return -EFAULT;
2579
2580 if (req.isc > MAX_ISC)
2581 return -EINVAL;
2582
2583 trace_kvm_s390_modify_ais_mode(req.isc,
2584 (fi->simm & AIS_MODE_MASK(req.isc)) ?
2585 (fi->nimm & AIS_MODE_MASK(req.isc)) ?
2586 2 : KVM_S390_AIS_MODE_SINGLE :
2587 KVM_S390_AIS_MODE_ALL, req.mode);
2588
2589 mutex_lock(&fi->ais_lock);
2590 switch (req.mode) {
2591 case KVM_S390_AIS_MODE_ALL:
2592 fi->simm &= ~AIS_MODE_MASK(req.isc);
2593 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2594 break;
2595 case KVM_S390_AIS_MODE_SINGLE:
2596 fi->simm |= AIS_MODE_MASK(req.isc);
2597 fi->nimm &= ~AIS_MODE_MASK(req.isc);
2598 break;
2599 default:
2600 ret = -EINVAL;
2601 }
2602 mutex_unlock(&fi->ais_lock);
2603
2604 return ret;
2605 }
2606
kvm_s390_inject_airq(struct kvm * kvm,struct s390_io_adapter * adapter)2607 static int kvm_s390_inject_airq(struct kvm *kvm,
2608 struct s390_io_adapter *adapter)
2609 {
2610 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2611 struct kvm_s390_interrupt s390int = {
2612 .type = KVM_S390_INT_IO(1, 0, 0, 0),
2613 .parm = 0,
2614 .parm64 = isc_to_int_word(adapter->isc),
2615 };
2616 int ret = 0;
2617
2618 if (!test_kvm_facility(kvm, 72) || !adapter->suppressible)
2619 return kvm_s390_inject_vm(kvm, &s390int);
2620
2621 mutex_lock(&fi->ais_lock);
2622 if (fi->nimm & AIS_MODE_MASK(adapter->isc)) {
2623 trace_kvm_s390_airq_suppressed(adapter->id, adapter->isc);
2624 goto out;
2625 }
2626
2627 ret = kvm_s390_inject_vm(kvm, &s390int);
2628 if (!ret && (fi->simm & AIS_MODE_MASK(adapter->isc))) {
2629 fi->nimm |= AIS_MODE_MASK(adapter->isc);
2630 trace_kvm_s390_modify_ais_mode(adapter->isc,
2631 KVM_S390_AIS_MODE_SINGLE, 2);
2632 }
2633 out:
2634 mutex_unlock(&fi->ais_lock);
2635 return ret;
2636 }
2637
flic_inject_airq(struct kvm * kvm,struct kvm_device_attr * attr)2638 static int flic_inject_airq(struct kvm *kvm, struct kvm_device_attr *attr)
2639 {
2640 unsigned int id = attr->attr;
2641 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
2642
2643 if (!adapter)
2644 return -EINVAL;
2645
2646 return kvm_s390_inject_airq(kvm, adapter);
2647 }
2648
flic_ais_mode_set_all(struct kvm * kvm,struct kvm_device_attr * attr)2649 static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr)
2650 {
2651 struct kvm_s390_float_interrupt *fi = &kvm->arch.float_int;
2652 struct kvm_s390_ais_all ais;
2653
2654 if (!test_kvm_facility(kvm, 72))
2655 return -EOPNOTSUPP;
2656
2657 if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais)))
2658 return -EFAULT;
2659
2660 mutex_lock(&fi->ais_lock);
2661 fi->simm = ais.simm;
2662 fi->nimm = ais.nimm;
2663 mutex_unlock(&fi->ais_lock);
2664
2665 return 0;
2666 }
2667
flic_set_attr(struct kvm_device * dev,struct kvm_device_attr * attr)2668 static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
2669 {
2670 int r = 0;
2671 unsigned long i;
2672 struct kvm_vcpu *vcpu;
2673
2674 switch (attr->group) {
2675 case KVM_DEV_FLIC_ENQUEUE:
2676 r = enqueue_floating_irq(dev, attr);
2677 break;
2678 case KVM_DEV_FLIC_CLEAR_IRQS:
2679 kvm_s390_clear_float_irqs(dev->kvm);
2680 break;
2681 case KVM_DEV_FLIC_APF_ENABLE:
2682 if (kvm_is_ucontrol(dev->kvm))
2683 return -EINVAL;
2684 dev->kvm->arch.gmap->pfault_enabled = 1;
2685 break;
2686 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2687 if (kvm_is_ucontrol(dev->kvm))
2688 return -EINVAL;
2689 dev->kvm->arch.gmap->pfault_enabled = 0;
2690 /*
2691 * Make sure no async faults are in transition when
2692 * clearing the queues. So we don't need to worry
2693 * about late coming workers.
2694 */
2695 synchronize_srcu(&dev->kvm->srcu);
2696 kvm_for_each_vcpu(i, vcpu, dev->kvm)
2697 kvm_clear_async_pf_completion_queue(vcpu);
2698 break;
2699 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2700 r = register_io_adapter(dev, attr);
2701 break;
2702 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2703 r = modify_io_adapter(dev, attr);
2704 break;
2705 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2706 r = clear_io_irq(dev->kvm, attr);
2707 break;
2708 case KVM_DEV_FLIC_AISM:
2709 r = modify_ais_mode(dev->kvm, attr);
2710 break;
2711 case KVM_DEV_FLIC_AIRQ_INJECT:
2712 r = flic_inject_airq(dev->kvm, attr);
2713 break;
2714 case KVM_DEV_FLIC_AISM_ALL:
2715 r = flic_ais_mode_set_all(dev->kvm, attr);
2716 break;
2717 default:
2718 r = -EINVAL;
2719 }
2720
2721 return r;
2722 }
2723
flic_has_attr(struct kvm_device * dev,struct kvm_device_attr * attr)2724 static int flic_has_attr(struct kvm_device *dev,
2725 struct kvm_device_attr *attr)
2726 {
2727 switch (attr->group) {
2728 case KVM_DEV_FLIC_GET_ALL_IRQS:
2729 case KVM_DEV_FLIC_ENQUEUE:
2730 case KVM_DEV_FLIC_CLEAR_IRQS:
2731 case KVM_DEV_FLIC_APF_ENABLE:
2732 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
2733 case KVM_DEV_FLIC_ADAPTER_REGISTER:
2734 case KVM_DEV_FLIC_ADAPTER_MODIFY:
2735 case KVM_DEV_FLIC_CLEAR_IO_IRQ:
2736 case KVM_DEV_FLIC_AISM:
2737 case KVM_DEV_FLIC_AIRQ_INJECT:
2738 case KVM_DEV_FLIC_AISM_ALL:
2739 return 0;
2740 }
2741 return -ENXIO;
2742 }
2743
flic_create(struct kvm_device * dev,u32 type)2744 static int flic_create(struct kvm_device *dev, u32 type)
2745 {
2746 if (!dev)
2747 return -EINVAL;
2748 if (dev->kvm->arch.flic)
2749 return -EINVAL;
2750 dev->kvm->arch.flic = dev;
2751 return 0;
2752 }
2753
flic_destroy(struct kvm_device * dev)2754 static void flic_destroy(struct kvm_device *dev)
2755 {
2756 dev->kvm->arch.flic = NULL;
2757 kfree(dev);
2758 }
2759
2760 /* s390 floating irq controller (flic) */
2761 struct kvm_device_ops kvm_flic_ops = {
2762 .name = "kvm-flic",
2763 .get_attr = flic_get_attr,
2764 .set_attr = flic_set_attr,
2765 .has_attr = flic_has_attr,
2766 .create = flic_create,
2767 .destroy = flic_destroy,
2768 };
2769
get_ind_bit(__u64 addr,unsigned long bit_nr,bool swap)2770 static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
2771 {
2772 unsigned long bit;
2773
2774 bit = bit_nr + (addr % PAGE_SIZE) * 8;
2775
2776 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
2777 }
2778
get_map_page(struct kvm * kvm,u64 uaddr)2779 static struct page *get_map_page(struct kvm *kvm, u64 uaddr)
2780 {
2781 struct page *page = NULL;
2782
2783 mmap_read_lock(kvm->mm);
2784 get_user_pages_remote(kvm->mm, uaddr, 1, FOLL_WRITE,
2785 &page, NULL);
2786 mmap_read_unlock(kvm->mm);
2787 return page;
2788 }
2789
adapter_indicators_set(struct kvm * kvm,struct s390_io_adapter * adapter,struct kvm_s390_adapter_int * adapter_int)2790 static int adapter_indicators_set(struct kvm *kvm,
2791 struct s390_io_adapter *adapter,
2792 struct kvm_s390_adapter_int *adapter_int)
2793 {
2794 unsigned long bit;
2795 int summary_set, idx;
2796 struct page *ind_page, *summary_page;
2797 void *map;
2798
2799 ind_page = get_map_page(kvm, adapter_int->ind_addr);
2800 if (!ind_page)
2801 return -1;
2802 summary_page = get_map_page(kvm, adapter_int->summary_addr);
2803 if (!summary_page) {
2804 put_page(ind_page);
2805 return -1;
2806 }
2807
2808 idx = srcu_read_lock(&kvm->srcu);
2809 map = page_address(ind_page);
2810 bit = get_ind_bit(adapter_int->ind_addr,
2811 adapter_int->ind_offset, adapter->swap);
2812 set_bit(bit, map);
2813 mark_page_dirty(kvm, adapter_int->ind_addr >> PAGE_SHIFT);
2814 set_page_dirty_lock(ind_page);
2815 map = page_address(summary_page);
2816 bit = get_ind_bit(adapter_int->summary_addr,
2817 adapter_int->summary_offset, adapter->swap);
2818 summary_set = test_and_set_bit(bit, map);
2819 mark_page_dirty(kvm, adapter_int->summary_addr >> PAGE_SHIFT);
2820 set_page_dirty_lock(summary_page);
2821 srcu_read_unlock(&kvm->srcu, idx);
2822
2823 put_page(ind_page);
2824 put_page(summary_page);
2825 return summary_set ? 0 : 1;
2826 }
2827
2828 /*
2829 * < 0 - not injected due to error
2830 * = 0 - coalesced, summary indicator already active
2831 * > 0 - injected interrupt
2832 */
set_adapter_int(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)2833 static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
2834 struct kvm *kvm, int irq_source_id, int level,
2835 bool line_status)
2836 {
2837 int ret;
2838 struct s390_io_adapter *adapter;
2839
2840 /* We're only interested in the 0->1 transition. */
2841 if (!level)
2842 return 0;
2843 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
2844 if (!adapter)
2845 return -1;
2846 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
2847 if ((ret > 0) && !adapter->masked) {
2848 ret = kvm_s390_inject_airq(kvm, adapter);
2849 if (ret == 0)
2850 ret = 1;
2851 }
2852 return ret;
2853 }
2854
2855 /*
2856 * Inject the machine check to the guest.
2857 */
kvm_s390_reinject_machine_check(struct kvm_vcpu * vcpu,struct mcck_volatile_info * mcck_info)2858 void kvm_s390_reinject_machine_check(struct kvm_vcpu *vcpu,
2859 struct mcck_volatile_info *mcck_info)
2860 {
2861 struct kvm_s390_interrupt_info inti;
2862 struct kvm_s390_irq irq;
2863 struct kvm_s390_mchk_info *mchk;
2864 union mci mci;
2865 __u64 cr14 = 0; /* upper bits are not used */
2866 int rc;
2867
2868 mci.val = mcck_info->mcic;
2869 if (mci.sr)
2870 cr14 |= CR14_RECOVERY_SUBMASK;
2871 if (mci.dg)
2872 cr14 |= CR14_DEGRADATION_SUBMASK;
2873 if (mci.w)
2874 cr14 |= CR14_WARNING_SUBMASK;
2875
2876 mchk = mci.ck ? &inti.mchk : &irq.u.mchk;
2877 mchk->cr14 = cr14;
2878 mchk->mcic = mcck_info->mcic;
2879 mchk->ext_damage_code = mcck_info->ext_damage_code;
2880 mchk->failing_storage_address = mcck_info->failing_storage_address;
2881 if (mci.ck) {
2882 /* Inject the floating machine check */
2883 inti.type = KVM_S390_MCHK;
2884 rc = __inject_vm(vcpu->kvm, &inti);
2885 } else {
2886 /* Inject the machine check to specified vcpu */
2887 irq.type = KVM_S390_MCHK;
2888 rc = kvm_s390_inject_vcpu(vcpu, &irq);
2889 }
2890 WARN_ON_ONCE(rc);
2891 }
2892
kvm_set_routing_entry(struct kvm * kvm,struct kvm_kernel_irq_routing_entry * e,const struct kvm_irq_routing_entry * ue)2893 int kvm_set_routing_entry(struct kvm *kvm,
2894 struct kvm_kernel_irq_routing_entry *e,
2895 const struct kvm_irq_routing_entry *ue)
2896 {
2897 u64 uaddr_s, uaddr_i;
2898 int idx;
2899
2900 switch (ue->type) {
2901 /* we store the userspace addresses instead of the guest addresses */
2902 case KVM_IRQ_ROUTING_S390_ADAPTER:
2903 if (kvm_is_ucontrol(kvm))
2904 return -EINVAL;
2905 e->set = set_adapter_int;
2906
2907 idx = srcu_read_lock(&kvm->srcu);
2908 uaddr_s = gpa_to_hva(kvm, ue->u.adapter.summary_addr);
2909 uaddr_i = gpa_to_hva(kvm, ue->u.adapter.ind_addr);
2910 srcu_read_unlock(&kvm->srcu, idx);
2911
2912 if (kvm_is_error_hva(uaddr_s) || kvm_is_error_hva(uaddr_i))
2913 return -EFAULT;
2914 e->adapter.summary_addr = uaddr_s;
2915 e->adapter.ind_addr = uaddr_i;
2916 e->adapter.summary_offset = ue->u.adapter.summary_offset;
2917 e->adapter.ind_offset = ue->u.adapter.ind_offset;
2918 e->adapter.adapter_id = ue->u.adapter.adapter_id;
2919 return 0;
2920 default:
2921 return -EINVAL;
2922 }
2923 }
2924
kvm_set_msi(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)2925 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
2926 int irq_source_id, int level, bool line_status)
2927 {
2928 return -EINVAL;
2929 }
2930
kvm_s390_set_irq_state(struct kvm_vcpu * vcpu,void __user * irqstate,int len)2931 int kvm_s390_set_irq_state(struct kvm_vcpu *vcpu, void __user *irqstate, int len)
2932 {
2933 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
2934 struct kvm_s390_irq *buf;
2935 int r = 0;
2936 int n;
2937
2938 buf = vmalloc(len);
2939 if (!buf)
2940 return -ENOMEM;
2941
2942 if (copy_from_user((void *) buf, irqstate, len)) {
2943 r = -EFAULT;
2944 goto out_free;
2945 }
2946
2947 /*
2948 * Don't allow setting the interrupt state
2949 * when there are already interrupts pending
2950 */
2951 spin_lock(&li->lock);
2952 if (li->pending_irqs) {
2953 r = -EBUSY;
2954 goto out_unlock;
2955 }
2956
2957 for (n = 0; n < len / sizeof(*buf); n++) {
2958 r = do_inject_vcpu(vcpu, &buf[n]);
2959 if (r)
2960 break;
2961 }
2962
2963 out_unlock:
2964 spin_unlock(&li->lock);
2965 out_free:
2966 vfree(buf);
2967
2968 return r;
2969 }
2970
store_local_irq(struct kvm_s390_local_interrupt * li,struct kvm_s390_irq * irq,unsigned long irq_type)2971 static void store_local_irq(struct kvm_s390_local_interrupt *li,
2972 struct kvm_s390_irq *irq,
2973 unsigned long irq_type)
2974 {
2975 switch (irq_type) {
2976 case IRQ_PEND_MCHK_EX:
2977 case IRQ_PEND_MCHK_REP:
2978 irq->type = KVM_S390_MCHK;
2979 irq->u.mchk = li->irq.mchk;
2980 break;
2981 case IRQ_PEND_PROG:
2982 irq->type = KVM_S390_PROGRAM_INT;
2983 irq->u.pgm = li->irq.pgm;
2984 break;
2985 case IRQ_PEND_PFAULT_INIT:
2986 irq->type = KVM_S390_INT_PFAULT_INIT;
2987 irq->u.ext = li->irq.ext;
2988 break;
2989 case IRQ_PEND_EXT_EXTERNAL:
2990 irq->type = KVM_S390_INT_EXTERNAL_CALL;
2991 irq->u.extcall = li->irq.extcall;
2992 break;
2993 case IRQ_PEND_EXT_CLOCK_COMP:
2994 irq->type = KVM_S390_INT_CLOCK_COMP;
2995 break;
2996 case IRQ_PEND_EXT_CPU_TIMER:
2997 irq->type = KVM_S390_INT_CPU_TIMER;
2998 break;
2999 case IRQ_PEND_SIGP_STOP:
3000 irq->type = KVM_S390_SIGP_STOP;
3001 irq->u.stop = li->irq.stop;
3002 break;
3003 case IRQ_PEND_RESTART:
3004 irq->type = KVM_S390_RESTART;
3005 break;
3006 case IRQ_PEND_SET_PREFIX:
3007 irq->type = KVM_S390_SIGP_SET_PREFIX;
3008 irq->u.prefix = li->irq.prefix;
3009 break;
3010 }
3011 }
3012
kvm_s390_get_irq_state(struct kvm_vcpu * vcpu,__u8 __user * buf,int len)3013 int kvm_s390_get_irq_state(struct kvm_vcpu *vcpu, __u8 __user *buf, int len)
3014 {
3015 int scn;
3016 DECLARE_BITMAP(sigp_emerg_pending, KVM_MAX_VCPUS);
3017 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
3018 unsigned long pending_irqs;
3019 struct kvm_s390_irq irq;
3020 unsigned long irq_type;
3021 int cpuaddr;
3022 int n = 0;
3023
3024 spin_lock(&li->lock);
3025 pending_irqs = li->pending_irqs;
3026 memcpy(&sigp_emerg_pending, &li->sigp_emerg_pending,
3027 sizeof(sigp_emerg_pending));
3028 spin_unlock(&li->lock);
3029
3030 for_each_set_bit(irq_type, &pending_irqs, IRQ_PEND_COUNT) {
3031 memset(&irq, 0, sizeof(irq));
3032 if (irq_type == IRQ_PEND_EXT_EMERGENCY)
3033 continue;
3034 if (n + sizeof(irq) > len)
3035 return -ENOBUFS;
3036 store_local_irq(&vcpu->arch.local_int, &irq, irq_type);
3037 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
3038 return -EFAULT;
3039 n += sizeof(irq);
3040 }
3041
3042 if (test_bit(IRQ_PEND_EXT_EMERGENCY, &pending_irqs)) {
3043 for_each_set_bit(cpuaddr, sigp_emerg_pending, KVM_MAX_VCPUS) {
3044 memset(&irq, 0, sizeof(irq));
3045 if (n + sizeof(irq) > len)
3046 return -ENOBUFS;
3047 irq.type = KVM_S390_INT_EMERGENCY;
3048 irq.u.emerg.code = cpuaddr;
3049 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
3050 return -EFAULT;
3051 n += sizeof(irq);
3052 }
3053 }
3054
3055 if (sca_ext_call_pending(vcpu, &scn)) {
3056 if (n + sizeof(irq) > len)
3057 return -ENOBUFS;
3058 memset(&irq, 0, sizeof(irq));
3059 irq.type = KVM_S390_INT_EXTERNAL_CALL;
3060 irq.u.extcall.code = scn;
3061 if (copy_to_user(&buf[n], &irq, sizeof(irq)))
3062 return -EFAULT;
3063 n += sizeof(irq);
3064 }
3065
3066 return n;
3067 }
3068
__airqs_kick_single_vcpu(struct kvm * kvm,u8 deliverable_mask)3069 static void __airqs_kick_single_vcpu(struct kvm *kvm, u8 deliverable_mask)
3070 {
3071 int vcpu_idx, online_vcpus = atomic_read(&kvm->online_vcpus);
3072 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3073 struct kvm_vcpu *vcpu;
3074 u8 vcpu_isc_mask;
3075
3076 for_each_set_bit(vcpu_idx, kvm->arch.idle_mask, online_vcpus) {
3077 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
3078 if (psw_ioint_disabled(vcpu))
3079 continue;
3080 vcpu_isc_mask = (u8)(vcpu->arch.sie_block->gcr[6] >> 24);
3081 if (deliverable_mask & vcpu_isc_mask) {
3082 /* lately kicked but not yet running */
3083 if (test_and_set_bit(vcpu_idx, gi->kicked_mask))
3084 return;
3085 kvm_s390_vcpu_wakeup(vcpu);
3086 return;
3087 }
3088 }
3089 }
3090
gisa_vcpu_kicker(struct hrtimer * timer)3091 static enum hrtimer_restart gisa_vcpu_kicker(struct hrtimer *timer)
3092 {
3093 struct kvm_s390_gisa_interrupt *gi =
3094 container_of(timer, struct kvm_s390_gisa_interrupt, timer);
3095 struct kvm *kvm =
3096 container_of(gi->origin, struct sie_page2, gisa)->kvm;
3097 u8 pending_mask;
3098
3099 pending_mask = gisa_get_ipm_or_restore_iam(gi);
3100 if (pending_mask) {
3101 __airqs_kick_single_vcpu(kvm, pending_mask);
3102 hrtimer_forward_now(timer, ns_to_ktime(gi->expires));
3103 return HRTIMER_RESTART;
3104 }
3105
3106 return HRTIMER_NORESTART;
3107 }
3108
3109 #define NULL_GISA_ADDR 0x00000000UL
3110 #define NONE_GISA_ADDR 0x00000001UL
3111 #define GISA_ADDR_MASK 0xfffff000UL
3112
process_gib_alert_list(void)3113 static void process_gib_alert_list(void)
3114 {
3115 struct kvm_s390_gisa_interrupt *gi;
3116 u32 final, gisa_phys, origin = 0UL;
3117 struct kvm_s390_gisa *gisa;
3118 struct kvm *kvm;
3119
3120 do {
3121 /*
3122 * If the NONE_GISA_ADDR is still stored in the alert list
3123 * origin, we will leave the outer loop. No further GISA has
3124 * been added to the alert list by millicode while processing
3125 * the current alert list.
3126 */
3127 final = (origin & NONE_GISA_ADDR);
3128 /*
3129 * Cut off the alert list and store the NONE_GISA_ADDR in the
3130 * alert list origin to avoid further GAL interruptions.
3131 * A new alert list can be build up by millicode in parallel
3132 * for guests not in the yet cut-off alert list. When in the
3133 * final loop, store the NULL_GISA_ADDR instead. This will re-
3134 * enable GAL interruptions on the host again.
3135 */
3136 origin = xchg(&gib->alert_list_origin,
3137 (!final) ? NONE_GISA_ADDR : NULL_GISA_ADDR);
3138 /*
3139 * Loop through the just cut-off alert list and start the
3140 * gisa timers to kick idle vcpus to consume the pending
3141 * interruptions asap.
3142 */
3143 while (origin & GISA_ADDR_MASK) {
3144 gisa_phys = origin;
3145 gisa = phys_to_virt(gisa_phys);
3146 origin = gisa->next_alert;
3147 gisa->next_alert = gisa_phys;
3148 kvm = container_of(gisa, struct sie_page2, gisa)->kvm;
3149 gi = &kvm->arch.gisa_int;
3150 if (hrtimer_active(&gi->timer))
3151 hrtimer_cancel(&gi->timer);
3152 hrtimer_start(&gi->timer, 0, HRTIMER_MODE_REL);
3153 }
3154 } while (!final);
3155
3156 }
3157
kvm_s390_gisa_clear(struct kvm * kvm)3158 void kvm_s390_gisa_clear(struct kvm *kvm)
3159 {
3160 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3161
3162 if (!gi->origin)
3163 return;
3164 gisa_clear_ipm(gi->origin);
3165 VM_EVENT(kvm, 3, "gisa 0x%p cleared", gi->origin);
3166 }
3167
kvm_s390_gisa_init(struct kvm * kvm)3168 void kvm_s390_gisa_init(struct kvm *kvm)
3169 {
3170 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3171
3172 if (!css_general_characteristics.aiv)
3173 return;
3174 gi->origin = &kvm->arch.sie_page2->gisa;
3175 gi->alert.mask = 0;
3176 spin_lock_init(&gi->alert.ref_lock);
3177 gi->expires = 50 * 1000; /* 50 usec */
3178 hrtimer_setup(&gi->timer, gisa_vcpu_kicker, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3179 memset(gi->origin, 0, sizeof(struct kvm_s390_gisa));
3180 gi->origin->next_alert = (u32)virt_to_phys(gi->origin);
3181 VM_EVENT(kvm, 3, "gisa 0x%p initialized", gi->origin);
3182 }
3183
kvm_s390_gisa_enable(struct kvm * kvm)3184 void kvm_s390_gisa_enable(struct kvm *kvm)
3185 {
3186 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3187 struct kvm_vcpu *vcpu;
3188 unsigned long i;
3189 u32 gisa_desc;
3190
3191 if (gi->origin)
3192 return;
3193 kvm_s390_gisa_init(kvm);
3194 gisa_desc = kvm_s390_get_gisa_desc(kvm);
3195 if (!gisa_desc)
3196 return;
3197 kvm_for_each_vcpu(i, vcpu, kvm) {
3198 mutex_lock(&vcpu->mutex);
3199 vcpu->arch.sie_block->gd = gisa_desc;
3200 vcpu->arch.sie_block->eca |= ECA_AIV;
3201 VCPU_EVENT(vcpu, 3, "AIV gisa format-%u enabled for cpu %03u",
3202 vcpu->arch.sie_block->gd & 0x3, vcpu->vcpu_id);
3203 mutex_unlock(&vcpu->mutex);
3204 }
3205 }
3206
kvm_s390_gisa_destroy(struct kvm * kvm)3207 void kvm_s390_gisa_destroy(struct kvm *kvm)
3208 {
3209 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3210 struct kvm_s390_gisa *gisa = gi->origin;
3211
3212 if (!gi->origin)
3213 return;
3214 WARN(gi->alert.mask != 0x00,
3215 "unexpected non zero alert.mask 0x%02x",
3216 gi->alert.mask);
3217 gi->alert.mask = 0x00;
3218 if (gisa_set_iam(gi->origin, gi->alert.mask))
3219 process_gib_alert_list();
3220 hrtimer_cancel(&gi->timer);
3221 gi->origin = NULL;
3222 VM_EVENT(kvm, 3, "gisa 0x%p destroyed", gisa);
3223 }
3224
kvm_s390_gisa_disable(struct kvm * kvm)3225 void kvm_s390_gisa_disable(struct kvm *kvm)
3226 {
3227 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3228 struct kvm_vcpu *vcpu;
3229 unsigned long i;
3230
3231 if (!gi->origin)
3232 return;
3233 kvm_for_each_vcpu(i, vcpu, kvm) {
3234 mutex_lock(&vcpu->mutex);
3235 vcpu->arch.sie_block->eca &= ~ECA_AIV;
3236 vcpu->arch.sie_block->gd = 0U;
3237 mutex_unlock(&vcpu->mutex);
3238 VCPU_EVENT(vcpu, 3, "AIV disabled for cpu %03u", vcpu->vcpu_id);
3239 }
3240 kvm_s390_gisa_destroy(kvm);
3241 }
3242
3243 /**
3244 * kvm_s390_gisc_register - register a guest ISC
3245 *
3246 * @kvm: the kernel vm to work with
3247 * @gisc: the guest interruption sub class to register
3248 *
3249 * The function extends the vm specific alert mask to use.
3250 * The effective IAM mask in the GISA is updated as well
3251 * in case the GISA is not part of the GIB alert list.
3252 * It will be updated latest when the IAM gets restored
3253 * by gisa_get_ipm_or_restore_iam().
3254 *
3255 * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3256 * has registered with the channel subsystem.
3257 * -ENODEV in case the vm uses no GISA
3258 * -ERANGE in case the guest ISC is invalid
3259 */
kvm_s390_gisc_register(struct kvm * kvm,u32 gisc)3260 int kvm_s390_gisc_register(struct kvm *kvm, u32 gisc)
3261 {
3262 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3263
3264 if (!gi->origin)
3265 return -ENODEV;
3266 if (gisc > MAX_ISC)
3267 return -ERANGE;
3268
3269 spin_lock(&gi->alert.ref_lock);
3270 gi->alert.ref_count[gisc]++;
3271 if (gi->alert.ref_count[gisc] == 1) {
3272 gi->alert.mask |= 0x80 >> gisc;
3273 gisa_set_iam(gi->origin, gi->alert.mask);
3274 }
3275 spin_unlock(&gi->alert.ref_lock);
3276
3277 return gib->nisc;
3278 }
3279 EXPORT_SYMBOL_GPL(kvm_s390_gisc_register);
3280
3281 /**
3282 * kvm_s390_gisc_unregister - unregister a guest ISC
3283 *
3284 * @kvm: the kernel vm to work with
3285 * @gisc: the guest interruption sub class to register
3286 *
3287 * The function reduces the vm specific alert mask to use.
3288 * The effective IAM mask in the GISA is updated as well
3289 * in case the GISA is not part of the GIB alert list.
3290 * It will be updated latest when the IAM gets restored
3291 * by gisa_get_ipm_or_restore_iam().
3292 *
3293 * Returns: the nonspecific ISC (NISC) the gib alert mechanism
3294 * has registered with the channel subsystem.
3295 * -ENODEV in case the vm uses no GISA
3296 * -ERANGE in case the guest ISC is invalid
3297 * -EINVAL in case the guest ISC is not registered
3298 */
kvm_s390_gisc_unregister(struct kvm * kvm,u32 gisc)3299 int kvm_s390_gisc_unregister(struct kvm *kvm, u32 gisc)
3300 {
3301 struct kvm_s390_gisa_interrupt *gi = &kvm->arch.gisa_int;
3302 int rc = 0;
3303
3304 if (!gi->origin)
3305 return -ENODEV;
3306 if (gisc > MAX_ISC)
3307 return -ERANGE;
3308
3309 spin_lock(&gi->alert.ref_lock);
3310 if (gi->alert.ref_count[gisc] == 0) {
3311 rc = -EINVAL;
3312 goto out;
3313 }
3314 gi->alert.ref_count[gisc]--;
3315 if (gi->alert.ref_count[gisc] == 0) {
3316 gi->alert.mask &= ~(0x80 >> gisc);
3317 gisa_set_iam(gi->origin, gi->alert.mask);
3318 }
3319 out:
3320 spin_unlock(&gi->alert.ref_lock);
3321
3322 return rc;
3323 }
3324 EXPORT_SYMBOL_GPL(kvm_s390_gisc_unregister);
3325
aen_host_forward(unsigned long si)3326 static void aen_host_forward(unsigned long si)
3327 {
3328 struct kvm_s390_gisa_interrupt *gi;
3329 struct zpci_gaite *gaite;
3330 struct kvm *kvm;
3331
3332 gaite = (struct zpci_gaite *)aift->gait +
3333 (si * sizeof(struct zpci_gaite));
3334 if (gaite->count == 0)
3335 return;
3336 if (gaite->aisb != 0)
3337 set_bit_inv(gaite->aisbo, phys_to_virt(gaite->aisb));
3338
3339 kvm = kvm_s390_pci_si_to_kvm(aift, si);
3340 if (!kvm)
3341 return;
3342 gi = &kvm->arch.gisa_int;
3343
3344 if (!(gi->origin->g1.simm & AIS_MODE_MASK(gaite->gisc)) ||
3345 !(gi->origin->g1.nimm & AIS_MODE_MASK(gaite->gisc))) {
3346 gisa_set_ipm_gisc(gi->origin, gaite->gisc);
3347 if (hrtimer_active(&gi->timer))
3348 hrtimer_cancel(&gi->timer);
3349 hrtimer_start(&gi->timer, 0, HRTIMER_MODE_REL);
3350 kvm->stat.aen_forward++;
3351 }
3352 }
3353
aen_process_gait(u8 isc)3354 static void aen_process_gait(u8 isc)
3355 {
3356 bool found = false, first = true;
3357 union zpci_sic_iib iib = {{0}};
3358 unsigned long si, flags;
3359
3360 spin_lock_irqsave(&aift->gait_lock, flags);
3361
3362 if (!aift->gait) {
3363 spin_unlock_irqrestore(&aift->gait_lock, flags);
3364 return;
3365 }
3366
3367 for (si = 0;;) {
3368 /* Scan adapter summary indicator bit vector */
3369 si = airq_iv_scan(aift->sbv, si, airq_iv_end(aift->sbv));
3370 if (si == -1UL) {
3371 if (first || found) {
3372 /* Re-enable interrupts. */
3373 zpci_set_irq_ctrl(SIC_IRQ_MODE_SINGLE, isc,
3374 &iib);
3375 first = found = false;
3376 } else {
3377 /* Interrupts on and all bits processed */
3378 break;
3379 }
3380 found = false;
3381 si = 0;
3382 /* Scan again after re-enabling interrupts */
3383 continue;
3384 }
3385 found = true;
3386 aen_host_forward(si);
3387 }
3388
3389 spin_unlock_irqrestore(&aift->gait_lock, flags);
3390 }
3391
gib_alert_irq_handler(struct airq_struct * airq,struct tpi_info * tpi_info)3392 static void gib_alert_irq_handler(struct airq_struct *airq,
3393 struct tpi_info *tpi_info)
3394 {
3395 struct tpi_adapter_info *info = (struct tpi_adapter_info *)tpi_info;
3396
3397 inc_irq_stat(IRQIO_GAL);
3398
3399 if ((info->forward || info->error) &&
3400 IS_ENABLED(CONFIG_VFIO_PCI_ZDEV_KVM)) {
3401 aen_process_gait(info->isc);
3402 if (info->aism != 0)
3403 process_gib_alert_list();
3404 } else {
3405 process_gib_alert_list();
3406 }
3407 }
3408
3409 static struct airq_struct gib_alert_irq = {
3410 .handler = gib_alert_irq_handler,
3411 };
3412
kvm_s390_gib_destroy(void)3413 void kvm_s390_gib_destroy(void)
3414 {
3415 if (!gib)
3416 return;
3417 if (kvm_s390_pci_interp_allowed() && aift) {
3418 mutex_lock(&aift->aift_lock);
3419 kvm_s390_pci_aen_exit();
3420 mutex_unlock(&aift->aift_lock);
3421 }
3422 chsc_sgib(0);
3423 unregister_adapter_interrupt(&gib_alert_irq);
3424 free_page((unsigned long)gib);
3425 gib = NULL;
3426 }
3427
kvm_s390_gib_init(u8 nisc)3428 int __init kvm_s390_gib_init(u8 nisc)
3429 {
3430 u32 gib_origin;
3431 int rc = 0;
3432
3433 if (!css_general_characteristics.aiv) {
3434 KVM_EVENT(3, "%s", "gib not initialized, no AIV facility");
3435 goto out;
3436 }
3437
3438 gib = (struct kvm_s390_gib *)get_zeroed_page(GFP_KERNEL_ACCOUNT | GFP_DMA);
3439 if (!gib) {
3440 rc = -ENOMEM;
3441 goto out;
3442 }
3443
3444 gib_alert_irq.isc = nisc;
3445 if (register_adapter_interrupt(&gib_alert_irq)) {
3446 pr_err("Registering the GIB alert interruption handler failed\n");
3447 rc = -EIO;
3448 goto out_free_gib;
3449 }
3450 /* adapter interrupts used for AP (applicable here) don't use the LSI */
3451 *gib_alert_irq.lsi_ptr = 0xff;
3452
3453 gib->nisc = nisc;
3454 gib_origin = virt_to_phys(gib);
3455 if (chsc_sgib(gib_origin)) {
3456 pr_err("Associating the GIB with the AIV facility failed\n");
3457 free_page((unsigned long)gib);
3458 gib = NULL;
3459 rc = -EIO;
3460 goto out_unreg_gal;
3461 }
3462
3463 if (kvm_s390_pci_interp_allowed()) {
3464 if (kvm_s390_pci_aen_init(nisc)) {
3465 pr_err("Initializing AEN for PCI failed\n");
3466 rc = -EIO;
3467 goto out_unreg_gal;
3468 }
3469 }
3470
3471 KVM_EVENT(3, "gib 0x%p (nisc=%d) initialized", gib, gib->nisc);
3472 goto out;
3473
3474 out_unreg_gal:
3475 unregister_adapter_interrupt(&gib_alert_irq);
3476 out_free_gib:
3477 free_page((unsigned long)gib);
3478 gib = NULL;
3479 out:
3480 return rc;
3481 }
3482