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