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