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