KVM: s390: Forward PSW to next instruction for addressing exceptions
[linux/fpc-iii.git] / arch / s390 / kvm / interrupt.c
blob98a313138f839a7053bdf315c803fe6604bef66c
1 /*
2 * handling kvm guest interrupts
4 * Copyright IBM Corp. 2008,2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License (version 2 only)
8 * as published by the Free Software Foundation.
10 * Author(s): Carsten Otte <cotte@de.ibm.com>
13 #include <linux/interrupt.h>
14 #include <linux/kvm_host.h>
15 #include <linux/hrtimer.h>
16 #include <linux/mmu_context.h>
17 #include <linux/signal.h>
18 #include <linux/slab.h>
19 #include <linux/bitmap.h>
20 #include <asm/asm-offsets.h>
21 #include <asm/uaccess.h>
22 #include <asm/sclp.h>
23 #include "kvm-s390.h"
24 #include "gaccess.h"
25 #include "trace-s390.h"
27 #define IOINT_SCHID_MASK 0x0000ffff
28 #define IOINT_SSID_MASK 0x00030000
29 #define IOINT_CSSID_MASK 0x03fc0000
30 #define IOINT_AI_MASK 0x04000000
31 #define PFAULT_INIT 0x0600
32 #define PFAULT_DONE 0x0680
33 #define VIRTIO_PARAM 0x0d00
35 static int is_ioint(u64 type)
37 return ((type & 0xfffe0000u) != 0xfffe0000u);
40 int psw_extint_disabled(struct kvm_vcpu *vcpu)
42 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
45 static int psw_ioint_disabled(struct kvm_vcpu *vcpu)
47 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO);
50 static int psw_mchk_disabled(struct kvm_vcpu *vcpu)
52 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_MCHECK);
55 static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
57 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
58 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
59 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
60 return 0;
61 return 1;
64 static int ckc_interrupts_enabled(struct kvm_vcpu *vcpu)
66 if (psw_extint_disabled(vcpu) ||
67 !(vcpu->arch.sie_block->gcr[0] & 0x800ul))
68 return 0;
69 if (guestdbg_enabled(vcpu) && guestdbg_sstep_enabled(vcpu))
70 /* No timer interrupts when single stepping */
71 return 0;
72 return 1;
75 static u64 int_word_to_isc_bits(u32 int_word)
77 u8 isc = (int_word & 0x38000000) >> 27;
79 return (0x80 >> isc) << 24;
82 static int __must_check __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
83 struct kvm_s390_interrupt_info *inti)
85 switch (inti->type) {
86 case KVM_S390_INT_EXTERNAL_CALL:
87 if (psw_extint_disabled(vcpu))
88 return 0;
89 if (vcpu->arch.sie_block->gcr[0] & 0x2000ul)
90 return 1;
91 return 0;
92 case KVM_S390_INT_EMERGENCY:
93 if (psw_extint_disabled(vcpu))
94 return 0;
95 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
96 return 1;
97 return 0;
98 case KVM_S390_INT_CLOCK_COMP:
99 return ckc_interrupts_enabled(vcpu);
100 case KVM_S390_INT_CPU_TIMER:
101 if (psw_extint_disabled(vcpu))
102 return 0;
103 if (vcpu->arch.sie_block->gcr[0] & 0x400ul)
104 return 1;
105 return 0;
106 case KVM_S390_INT_SERVICE:
107 case KVM_S390_INT_PFAULT_INIT:
108 case KVM_S390_INT_PFAULT_DONE:
109 case KVM_S390_INT_VIRTIO:
110 if (psw_extint_disabled(vcpu))
111 return 0;
112 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
113 return 1;
114 return 0;
115 case KVM_S390_PROGRAM_INT:
116 case KVM_S390_SIGP_STOP:
117 case KVM_S390_SIGP_SET_PREFIX:
118 case KVM_S390_RESTART:
119 return 1;
120 case KVM_S390_MCHK:
121 if (psw_mchk_disabled(vcpu))
122 return 0;
123 if (vcpu->arch.sie_block->gcr[14] & inti->mchk.cr14)
124 return 1;
125 return 0;
126 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
127 if (psw_ioint_disabled(vcpu))
128 return 0;
129 if (vcpu->arch.sie_block->gcr[6] &
130 int_word_to_isc_bits(inti->io.io_int_word))
131 return 1;
132 return 0;
133 default:
134 printk(KERN_WARNING "illegal interrupt type %llx\n",
135 inti->type);
136 BUG();
138 return 0;
141 static inline unsigned long pending_local_irqs(struct kvm_vcpu *vcpu)
143 return vcpu->arch.local_int.pending_irqs;
146 static unsigned long deliverable_local_irqs(struct kvm_vcpu *vcpu)
148 unsigned long active_mask = pending_local_irqs(vcpu);
150 if (psw_extint_disabled(vcpu))
151 active_mask &= ~IRQ_PEND_EXT_MASK;
152 if (!(vcpu->arch.sie_block->gcr[0] & 0x2000ul))
153 __clear_bit(IRQ_PEND_EXT_EXTERNAL, &active_mask);
154 if (!(vcpu->arch.sie_block->gcr[0] & 0x4000ul))
155 __clear_bit(IRQ_PEND_EXT_EMERGENCY, &active_mask);
156 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
157 __clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &active_mask);
158 if (!(vcpu->arch.sie_block->gcr[0] & 0x400ul))
159 __clear_bit(IRQ_PEND_EXT_CPU_TIMER, &active_mask);
160 if (psw_mchk_disabled(vcpu))
161 active_mask &= ~IRQ_PEND_MCHK_MASK;
164 * STOP irqs will never be actively delivered. They are triggered via
165 * intercept requests and cleared when the stop intercept is performed.
167 __clear_bit(IRQ_PEND_SIGP_STOP, &active_mask);
169 return active_mask;
172 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
174 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
175 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
178 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
180 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
181 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
184 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
186 atomic_clear_mask(CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
187 &vcpu->arch.sie_block->cpuflags);
188 vcpu->arch.sie_block->lctl = 0x0000;
189 vcpu->arch.sie_block->ictl &= ~(ICTL_LPSW | ICTL_STCTL | ICTL_PINT);
191 if (guestdbg_enabled(vcpu)) {
192 vcpu->arch.sie_block->lctl |= (LCTL_CR0 | LCTL_CR9 |
193 LCTL_CR10 | LCTL_CR11);
194 vcpu->arch.sie_block->ictl |= (ICTL_STCTL | ICTL_PINT);
198 static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
200 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
203 static void set_intercept_indicators_ext(struct kvm_vcpu *vcpu)
205 if (!(pending_local_irqs(vcpu) & IRQ_PEND_EXT_MASK))
206 return;
207 if (psw_extint_disabled(vcpu))
208 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
209 else
210 vcpu->arch.sie_block->lctl |= LCTL_CR0;
213 static void set_intercept_indicators_mchk(struct kvm_vcpu *vcpu)
215 if (!(pending_local_irqs(vcpu) & IRQ_PEND_MCHK_MASK))
216 return;
217 if (psw_mchk_disabled(vcpu))
218 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
219 else
220 vcpu->arch.sie_block->lctl |= LCTL_CR14;
223 static void set_intercept_indicators_stop(struct kvm_vcpu *vcpu)
225 if (kvm_s390_is_stop_irq_pending(vcpu))
226 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
229 /* Set interception request for non-deliverable local interrupts */
230 static void set_intercept_indicators_local(struct kvm_vcpu *vcpu)
232 set_intercept_indicators_ext(vcpu);
233 set_intercept_indicators_mchk(vcpu);
234 set_intercept_indicators_stop(vcpu);
237 static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
238 struct kvm_s390_interrupt_info *inti)
240 switch (inti->type) {
241 case KVM_S390_INT_SERVICE:
242 case KVM_S390_INT_PFAULT_DONE:
243 case KVM_S390_INT_VIRTIO:
244 if (psw_extint_disabled(vcpu))
245 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
246 else
247 vcpu->arch.sie_block->lctl |= LCTL_CR0;
248 break;
249 case KVM_S390_MCHK:
250 if (psw_mchk_disabled(vcpu))
251 vcpu->arch.sie_block->ictl |= ICTL_LPSW;
252 else
253 vcpu->arch.sie_block->lctl |= LCTL_CR14;
254 break;
255 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
256 if (psw_ioint_disabled(vcpu))
257 __set_cpuflag(vcpu, CPUSTAT_IO_INT);
258 else
259 vcpu->arch.sie_block->lctl |= LCTL_CR6;
260 break;
261 default:
262 BUG();
266 static u16 get_ilc(struct kvm_vcpu *vcpu)
268 const unsigned short table[] = { 2, 4, 4, 6 };
270 switch (vcpu->arch.sie_block->icptcode) {
271 case ICPT_INST:
272 case ICPT_INSTPROGI:
273 case ICPT_OPEREXC:
274 case ICPT_PARTEXEC:
275 case ICPT_IOINST:
276 /* last instruction only stored for these icptcodes */
277 return table[vcpu->arch.sie_block->ipa >> 14];
278 case ICPT_PROGI:
279 return vcpu->arch.sie_block->pgmilc;
280 default:
281 return 0;
285 static int __must_check __deliver_cpu_timer(struct kvm_vcpu *vcpu)
287 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
288 int rc;
290 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
291 0, 0);
293 rc = put_guest_lc(vcpu, EXT_IRQ_CPU_TIMER,
294 (u16 *)__LC_EXT_INT_CODE);
295 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
296 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
297 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
298 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
299 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
300 clear_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
301 return rc ? -EFAULT : 0;
304 static int __must_check __deliver_ckc(struct kvm_vcpu *vcpu)
306 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
307 int rc;
309 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
310 0, 0);
312 rc = put_guest_lc(vcpu, EXT_IRQ_CLK_COMP,
313 (u16 __user *)__LC_EXT_INT_CODE);
314 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
315 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
316 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
317 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
318 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
319 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
320 return rc ? -EFAULT : 0;
323 static int __must_check __deliver_pfault_init(struct kvm_vcpu *vcpu)
325 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
326 struct kvm_s390_ext_info ext;
327 int rc;
329 spin_lock(&li->lock);
330 ext = li->irq.ext;
331 clear_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
332 li->irq.ext.ext_params2 = 0;
333 spin_unlock(&li->lock);
335 VCPU_EVENT(vcpu, 4, "interrupt: pfault init parm:%x,parm64:%llx",
336 0, ext.ext_params2);
337 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
338 KVM_S390_INT_PFAULT_INIT,
339 0, ext.ext_params2);
341 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *) __LC_EXT_INT_CODE);
342 rc |= put_guest_lc(vcpu, PFAULT_INIT, (u16 *) __LC_EXT_CPU_ADDR);
343 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
344 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
345 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
346 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
347 rc |= put_guest_lc(vcpu, ext.ext_params2, (u64 *) __LC_EXT_PARAMS2);
348 return rc ? -EFAULT : 0;
351 static int __must_check __deliver_machine_check(struct kvm_vcpu *vcpu)
353 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
354 struct kvm_s390_mchk_info mchk;
355 int rc;
357 spin_lock(&li->lock);
358 mchk = li->irq.mchk;
360 * If there was an exigent machine check pending, then any repressible
361 * machine checks that might have been pending are indicated along
362 * with it, so always clear both bits
364 clear_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
365 clear_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
366 memset(&li->irq.mchk, 0, sizeof(mchk));
367 spin_unlock(&li->lock);
369 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
370 mchk.mcic);
371 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_MCHK,
372 mchk.cr14, mchk.mcic);
374 rc = kvm_s390_vcpu_store_status(vcpu, KVM_S390_STORE_STATUS_PREFIXED);
375 rc |= put_guest_lc(vcpu, mchk.mcic,
376 (u64 __user *) __LC_MCCK_CODE);
377 rc |= put_guest_lc(vcpu, mchk.failing_storage_address,
378 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
379 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
380 &mchk.fixed_logout, sizeof(mchk.fixed_logout));
381 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
382 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
383 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
384 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
385 return rc ? -EFAULT : 0;
388 static int __must_check __deliver_restart(struct kvm_vcpu *vcpu)
390 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
391 int rc;
393 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
394 vcpu->stat.deliver_restart_signal++;
395 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0);
397 rc = write_guest_lc(vcpu,
398 offsetof(struct _lowcore, restart_old_psw),
399 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
400 rc |= read_guest_lc(vcpu, offsetof(struct _lowcore, restart_psw),
401 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
402 clear_bit(IRQ_PEND_RESTART, &li->pending_irqs);
403 return rc ? -EFAULT : 0;
406 static int __must_check __deliver_set_prefix(struct kvm_vcpu *vcpu)
408 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
409 struct kvm_s390_prefix_info prefix;
411 spin_lock(&li->lock);
412 prefix = li->irq.prefix;
413 li->irq.prefix.address = 0;
414 clear_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
415 spin_unlock(&li->lock);
417 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x", prefix.address);
418 vcpu->stat.deliver_prefix_signal++;
419 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
420 KVM_S390_SIGP_SET_PREFIX,
421 prefix.address, 0);
423 kvm_s390_set_prefix(vcpu, prefix.address);
424 return 0;
427 static int __must_check __deliver_emergency_signal(struct kvm_vcpu *vcpu)
429 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
430 int rc;
431 int cpu_addr;
433 spin_lock(&li->lock);
434 cpu_addr = find_first_bit(li->sigp_emerg_pending, KVM_MAX_VCPUS);
435 clear_bit(cpu_addr, li->sigp_emerg_pending);
436 if (bitmap_empty(li->sigp_emerg_pending, KVM_MAX_VCPUS))
437 clear_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
438 spin_unlock(&li->lock);
440 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
441 vcpu->stat.deliver_emergency_signal++;
442 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
443 cpu_addr, 0);
445 rc = put_guest_lc(vcpu, EXT_IRQ_EMERGENCY_SIG,
446 (u16 *)__LC_EXT_INT_CODE);
447 rc |= put_guest_lc(vcpu, cpu_addr, (u16 *)__LC_EXT_CPU_ADDR);
448 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
449 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
450 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
451 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
452 return rc ? -EFAULT : 0;
455 static int __must_check __deliver_external_call(struct kvm_vcpu *vcpu)
457 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
458 struct kvm_s390_extcall_info extcall;
459 int rc;
461 spin_lock(&li->lock);
462 extcall = li->irq.extcall;
463 li->irq.extcall.code = 0;
464 clear_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
465 spin_unlock(&li->lock);
467 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp ext call");
468 vcpu->stat.deliver_external_call++;
469 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
470 KVM_S390_INT_EXTERNAL_CALL,
471 extcall.code, 0);
473 rc = put_guest_lc(vcpu, EXT_IRQ_EXTERNAL_CALL,
474 (u16 *)__LC_EXT_INT_CODE);
475 rc |= put_guest_lc(vcpu, extcall.code, (u16 *)__LC_EXT_CPU_ADDR);
476 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
477 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
478 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW, &vcpu->arch.sie_block->gpsw,
479 sizeof(psw_t));
480 return rc ? -EFAULT : 0;
483 static int __must_check __deliver_prog(struct kvm_vcpu *vcpu)
485 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
486 struct kvm_s390_pgm_info pgm_info;
487 int rc = 0, nullifying = false;
488 u16 ilc = get_ilc(vcpu);
490 spin_lock(&li->lock);
491 pgm_info = li->irq.pgm;
492 clear_bit(IRQ_PEND_PROG, &li->pending_irqs);
493 memset(&li->irq.pgm, 0, sizeof(pgm_info));
494 spin_unlock(&li->lock);
496 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
497 pgm_info.code, ilc);
498 vcpu->stat.deliver_program_int++;
499 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
500 pgm_info.code, 0);
502 switch (pgm_info.code & ~PGM_PER) {
503 case PGM_AFX_TRANSLATION:
504 case PGM_ASX_TRANSLATION:
505 case PGM_EX_TRANSLATION:
506 case PGM_LFX_TRANSLATION:
507 case PGM_LSTE_SEQUENCE:
508 case PGM_LSX_TRANSLATION:
509 case PGM_LX_TRANSLATION:
510 case PGM_PRIMARY_AUTHORITY:
511 case PGM_SECONDARY_AUTHORITY:
512 nullifying = true;
513 /* fall through */
514 case PGM_SPACE_SWITCH:
515 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
516 (u64 *)__LC_TRANS_EXC_CODE);
517 break;
518 case PGM_ALEN_TRANSLATION:
519 case PGM_ALE_SEQUENCE:
520 case PGM_ASTE_INSTANCE:
521 case PGM_ASTE_SEQUENCE:
522 case PGM_ASTE_VALIDITY:
523 case PGM_EXTENDED_AUTHORITY:
524 rc = put_guest_lc(vcpu, pgm_info.exc_access_id,
525 (u8 *)__LC_EXC_ACCESS_ID);
526 nullifying = true;
527 break;
528 case PGM_ASCE_TYPE:
529 case PGM_PAGE_TRANSLATION:
530 case PGM_REGION_FIRST_TRANS:
531 case PGM_REGION_SECOND_TRANS:
532 case PGM_REGION_THIRD_TRANS:
533 case PGM_SEGMENT_TRANSLATION:
534 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
535 (u64 *)__LC_TRANS_EXC_CODE);
536 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
537 (u8 *)__LC_EXC_ACCESS_ID);
538 rc |= put_guest_lc(vcpu, pgm_info.op_access_id,
539 (u8 *)__LC_OP_ACCESS_ID);
540 nullifying = true;
541 break;
542 case PGM_MONITOR:
543 rc = put_guest_lc(vcpu, pgm_info.mon_class_nr,
544 (u16 *)__LC_MON_CLASS_NR);
545 rc |= put_guest_lc(vcpu, pgm_info.mon_code,
546 (u64 *)__LC_MON_CODE);
547 break;
548 case PGM_DATA:
549 rc = put_guest_lc(vcpu, pgm_info.data_exc_code,
550 (u32 *)__LC_DATA_EXC_CODE);
551 break;
552 case PGM_PROTECTION:
553 rc = put_guest_lc(vcpu, pgm_info.trans_exc_code,
554 (u64 *)__LC_TRANS_EXC_CODE);
555 rc |= put_guest_lc(vcpu, pgm_info.exc_access_id,
556 (u8 *)__LC_EXC_ACCESS_ID);
557 break;
558 case PGM_STACK_FULL:
559 case PGM_STACK_EMPTY:
560 case PGM_STACK_SPECIFICATION:
561 case PGM_STACK_TYPE:
562 case PGM_STACK_OPERATION:
563 case PGM_TRACE_TABEL:
564 case PGM_CRYPTO_OPERATION:
565 nullifying = true;
566 break;
569 if (pgm_info.code & PGM_PER) {
570 rc |= put_guest_lc(vcpu, pgm_info.per_code,
571 (u8 *) __LC_PER_CODE);
572 rc |= put_guest_lc(vcpu, pgm_info.per_atmid,
573 (u8 *)__LC_PER_ATMID);
574 rc |= put_guest_lc(vcpu, pgm_info.per_address,
575 (u64 *) __LC_PER_ADDRESS);
576 rc |= put_guest_lc(vcpu, pgm_info.per_access_id,
577 (u8 *) __LC_PER_ACCESS_ID);
580 if (nullifying && vcpu->arch.sie_block->icptcode == ICPT_INST)
581 kvm_s390_rewind_psw(vcpu, ilc);
583 rc |= put_guest_lc(vcpu, ilc, (u16 *) __LC_PGM_ILC);
584 rc |= put_guest_lc(vcpu, pgm_info.code,
585 (u16 *)__LC_PGM_INT_CODE);
586 rc |= write_guest_lc(vcpu, __LC_PGM_OLD_PSW,
587 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
588 rc |= read_guest_lc(vcpu, __LC_PGM_NEW_PSW,
589 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
590 return rc ? -EFAULT : 0;
593 static int __must_check __deliver_service(struct kvm_vcpu *vcpu,
594 struct kvm_s390_interrupt_info *inti)
596 int rc;
598 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
599 inti->ext.ext_params);
600 vcpu->stat.deliver_service_signal++;
601 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
602 inti->ext.ext_params, 0);
604 rc = put_guest_lc(vcpu, EXT_IRQ_SERVICE_SIG, (u16 *)__LC_EXT_INT_CODE);
605 rc |= put_guest_lc(vcpu, 0, (u16 *)__LC_EXT_CPU_ADDR);
606 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
607 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
608 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
609 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
610 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
611 (u32 *)__LC_EXT_PARAMS);
612 return rc ? -EFAULT : 0;
615 static int __must_check __deliver_pfault_done(struct kvm_vcpu *vcpu,
616 struct kvm_s390_interrupt_info *inti)
618 int rc;
620 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id,
621 KVM_S390_INT_PFAULT_DONE, 0,
622 inti->ext.ext_params2);
624 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *)__LC_EXT_INT_CODE);
625 rc |= put_guest_lc(vcpu, PFAULT_DONE, (u16 *)__LC_EXT_CPU_ADDR);
626 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
627 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
628 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
629 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
630 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
631 (u64 *)__LC_EXT_PARAMS2);
632 return rc ? -EFAULT : 0;
635 static int __must_check __deliver_virtio(struct kvm_vcpu *vcpu,
636 struct kvm_s390_interrupt_info *inti)
638 int rc;
640 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
641 inti->ext.ext_params, inti->ext.ext_params2);
642 vcpu->stat.deliver_virtio_interrupt++;
643 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
644 inti->ext.ext_params,
645 inti->ext.ext_params2);
647 rc = put_guest_lc(vcpu, EXT_IRQ_CP_SERVICE, (u16 *)__LC_EXT_INT_CODE);
648 rc |= put_guest_lc(vcpu, VIRTIO_PARAM, (u16 *)__LC_EXT_CPU_ADDR);
649 rc |= write_guest_lc(vcpu, __LC_EXT_OLD_PSW,
650 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
651 rc |= read_guest_lc(vcpu, __LC_EXT_NEW_PSW,
652 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
653 rc |= put_guest_lc(vcpu, inti->ext.ext_params,
654 (u32 *)__LC_EXT_PARAMS);
655 rc |= put_guest_lc(vcpu, inti->ext.ext_params2,
656 (u64 *)__LC_EXT_PARAMS2);
657 return rc ? -EFAULT : 0;
660 static int __must_check __deliver_io(struct kvm_vcpu *vcpu,
661 struct kvm_s390_interrupt_info *inti)
663 int rc;
665 VCPU_EVENT(vcpu, 4, "interrupt: I/O %llx", inti->type);
666 vcpu->stat.deliver_io_int++;
667 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, inti->type,
668 ((__u32)inti->io.subchannel_id << 16) |
669 inti->io.subchannel_nr,
670 ((__u64)inti->io.io_int_parm << 32) |
671 inti->io.io_int_word);
673 rc = put_guest_lc(vcpu, inti->io.subchannel_id,
674 (u16 *)__LC_SUBCHANNEL_ID);
675 rc |= put_guest_lc(vcpu, inti->io.subchannel_nr,
676 (u16 *)__LC_SUBCHANNEL_NR);
677 rc |= put_guest_lc(vcpu, inti->io.io_int_parm,
678 (u32 *)__LC_IO_INT_PARM);
679 rc |= put_guest_lc(vcpu, inti->io.io_int_word,
680 (u32 *)__LC_IO_INT_WORD);
681 rc |= write_guest_lc(vcpu, __LC_IO_OLD_PSW,
682 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
683 rc |= read_guest_lc(vcpu, __LC_IO_NEW_PSW,
684 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
685 return rc ? -EFAULT : 0;
688 static int __must_check __deliver_mchk_floating(struct kvm_vcpu *vcpu,
689 struct kvm_s390_interrupt_info *inti)
691 struct kvm_s390_mchk_info *mchk = &inti->mchk;
692 int rc;
694 VCPU_EVENT(vcpu, 4, "interrupt: machine check mcic=%llx",
695 mchk->mcic);
696 trace_kvm_s390_deliver_interrupt(vcpu->vcpu_id, KVM_S390_MCHK,
697 mchk->cr14, mchk->mcic);
699 rc = kvm_s390_vcpu_store_status(vcpu, KVM_S390_STORE_STATUS_PREFIXED);
700 rc |= put_guest_lc(vcpu, mchk->mcic,
701 (u64 __user *) __LC_MCCK_CODE);
702 rc |= put_guest_lc(vcpu, mchk->failing_storage_address,
703 (u64 __user *) __LC_MCCK_FAIL_STOR_ADDR);
704 rc |= write_guest_lc(vcpu, __LC_PSW_SAVE_AREA,
705 &mchk->fixed_logout, sizeof(mchk->fixed_logout));
706 rc |= write_guest_lc(vcpu, __LC_MCK_OLD_PSW,
707 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
708 rc |= read_guest_lc(vcpu, __LC_MCK_NEW_PSW,
709 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
710 return rc ? -EFAULT : 0;
713 typedef int (*deliver_irq_t)(struct kvm_vcpu *vcpu);
715 static const deliver_irq_t deliver_irq_funcs[] = {
716 [IRQ_PEND_MCHK_EX] = __deliver_machine_check,
717 [IRQ_PEND_PROG] = __deliver_prog,
718 [IRQ_PEND_EXT_EMERGENCY] = __deliver_emergency_signal,
719 [IRQ_PEND_EXT_EXTERNAL] = __deliver_external_call,
720 [IRQ_PEND_EXT_CLOCK_COMP] = __deliver_ckc,
721 [IRQ_PEND_EXT_CPU_TIMER] = __deliver_cpu_timer,
722 [IRQ_PEND_RESTART] = __deliver_restart,
723 [IRQ_PEND_SET_PREFIX] = __deliver_set_prefix,
724 [IRQ_PEND_PFAULT_INIT] = __deliver_pfault_init,
727 static int __must_check __deliver_floating_interrupt(struct kvm_vcpu *vcpu,
728 struct kvm_s390_interrupt_info *inti)
730 int rc;
732 switch (inti->type) {
733 case KVM_S390_INT_SERVICE:
734 rc = __deliver_service(vcpu, inti);
735 break;
736 case KVM_S390_INT_PFAULT_DONE:
737 rc = __deliver_pfault_done(vcpu, inti);
738 break;
739 case KVM_S390_INT_VIRTIO:
740 rc = __deliver_virtio(vcpu, inti);
741 break;
742 case KVM_S390_MCHK:
743 rc = __deliver_mchk_floating(vcpu, inti);
744 break;
745 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
746 rc = __deliver_io(vcpu, inti);
747 break;
748 default:
749 BUG();
752 return rc;
755 /* Check whether an external call is pending (deliverable or not) */
756 int kvm_s390_ext_call_pending(struct kvm_vcpu *vcpu)
758 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
759 uint8_t sigp_ctrl = vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
761 if (!sclp_has_sigpif())
762 return test_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs);
764 return (sigp_ctrl & SIGP_CTRL_C) &&
765 (atomic_read(&vcpu->arch.sie_block->cpuflags) & CPUSTAT_ECALL_PEND);
768 int kvm_s390_vcpu_has_irq(struct kvm_vcpu *vcpu, int exclude_stop)
770 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
771 struct kvm_s390_interrupt_info *inti;
772 int rc;
774 rc = !!deliverable_local_irqs(vcpu);
776 if ((!rc) && atomic_read(&fi->active)) {
777 spin_lock(&fi->lock);
778 list_for_each_entry(inti, &fi->list, list)
779 if (__interrupt_is_deliverable(vcpu, inti)) {
780 rc = 1;
781 break;
783 spin_unlock(&fi->lock);
786 if (!rc && kvm_cpu_has_pending_timer(vcpu))
787 rc = 1;
789 /* external call pending and deliverable */
790 if (!rc && kvm_s390_ext_call_pending(vcpu) &&
791 !psw_extint_disabled(vcpu) &&
792 (vcpu->arch.sie_block->gcr[0] & 0x2000ul))
793 rc = 1;
795 if (!rc && !exclude_stop && kvm_s390_is_stop_irq_pending(vcpu))
796 rc = 1;
798 return rc;
801 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
803 if (!(vcpu->arch.sie_block->ckc <
804 get_tod_clock_fast() + vcpu->arch.sie_block->epoch))
805 return 0;
806 if (!ckc_interrupts_enabled(vcpu))
807 return 0;
808 return 1;
811 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
813 u64 now, sltime;
815 vcpu->stat.exit_wait_state++;
817 /* fast path */
818 if (kvm_cpu_has_pending_timer(vcpu) || kvm_arch_vcpu_runnable(vcpu))
819 return 0;
821 if (psw_interrupts_disabled(vcpu)) {
822 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
823 return -EOPNOTSUPP; /* disabled wait */
826 if (!ckc_interrupts_enabled(vcpu)) {
827 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
828 __set_cpu_idle(vcpu);
829 goto no_timer;
832 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
833 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
835 /* underflow */
836 if (vcpu->arch.sie_block->ckc < now)
837 return 0;
839 __set_cpu_idle(vcpu);
840 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
841 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
842 no_timer:
843 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
844 kvm_vcpu_block(vcpu);
845 __unset_cpu_idle(vcpu);
846 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
848 hrtimer_cancel(&vcpu->arch.ckc_timer);
849 return 0;
852 void kvm_s390_vcpu_wakeup(struct kvm_vcpu *vcpu)
854 if (waitqueue_active(&vcpu->wq)) {
856 * The vcpu gave up the cpu voluntarily, mark it as a good
857 * yield-candidate.
859 vcpu->preempted = true;
860 wake_up_interruptible(&vcpu->wq);
861 vcpu->stat.halt_wakeup++;
865 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
867 struct kvm_vcpu *vcpu;
868 u64 now, sltime;
870 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
871 now = get_tod_clock_fast() + vcpu->arch.sie_block->epoch;
872 sltime = tod_to_ns(vcpu->arch.sie_block->ckc - now);
875 * If the monotonic clock runs faster than the tod clock we might be
876 * woken up too early and have to go back to sleep to avoid deadlocks.
878 if (vcpu->arch.sie_block->ckc > now &&
879 hrtimer_forward_now(timer, ns_to_ktime(sltime)))
880 return HRTIMER_RESTART;
881 kvm_s390_vcpu_wakeup(vcpu);
882 return HRTIMER_NORESTART;
885 void kvm_s390_clear_local_irqs(struct kvm_vcpu *vcpu)
887 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
889 spin_lock(&li->lock);
890 li->pending_irqs = 0;
891 bitmap_zero(li->sigp_emerg_pending, KVM_MAX_VCPUS);
892 memset(&li->irq, 0, sizeof(li->irq));
893 spin_unlock(&li->lock);
895 /* clear pending external calls set by sigp interpretation facility */
896 atomic_clear_mask(CPUSTAT_ECALL_PEND, li->cpuflags);
897 vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl = 0;
900 int __must_check kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
902 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
903 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
904 struct kvm_s390_interrupt_info *n, *inti = NULL;
905 deliver_irq_t func;
906 int deliver;
907 int rc = 0;
908 unsigned long irq_type;
909 unsigned long deliverable_irqs;
911 __reset_intercept_indicators(vcpu);
913 /* pending ckc conditions might have been invalidated */
914 clear_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
915 if (kvm_cpu_has_pending_timer(vcpu))
916 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
918 do {
919 deliverable_irqs = deliverable_local_irqs(vcpu);
920 /* bits are in the order of interrupt priority */
921 irq_type = find_first_bit(&deliverable_irqs, IRQ_PEND_COUNT);
922 if (irq_type == IRQ_PEND_COUNT)
923 break;
924 func = deliver_irq_funcs[irq_type];
925 if (!func) {
926 WARN_ON_ONCE(func == NULL);
927 clear_bit(irq_type, &li->pending_irqs);
928 continue;
930 rc = func(vcpu);
931 } while (!rc && irq_type != IRQ_PEND_COUNT);
933 set_intercept_indicators_local(vcpu);
935 if (!rc && atomic_read(&fi->active)) {
936 do {
937 deliver = 0;
938 spin_lock(&fi->lock);
939 list_for_each_entry_safe(inti, n, &fi->list, list) {
940 if (__interrupt_is_deliverable(vcpu, inti)) {
941 list_del(&inti->list);
942 fi->irq_count--;
943 deliver = 1;
944 break;
946 __set_intercept_indicator(vcpu, inti);
948 if (list_empty(&fi->list))
949 atomic_set(&fi->active, 0);
950 spin_unlock(&fi->lock);
951 if (deliver) {
952 rc = __deliver_floating_interrupt(vcpu, inti);
953 kfree(inti);
955 } while (!rc && deliver);
958 return rc;
961 static int __inject_prog(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
963 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
965 li->irq.pgm = irq->u.pgm;
966 set_bit(IRQ_PEND_PROG, &li->pending_irqs);
967 return 0;
970 int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
972 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
973 struct kvm_s390_irq irq;
975 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
976 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT, code,
977 0, 1);
978 spin_lock(&li->lock);
979 irq.u.pgm.code = code;
980 __inject_prog(vcpu, &irq);
981 BUG_ON(waitqueue_active(li->wq));
982 spin_unlock(&li->lock);
983 return 0;
986 int kvm_s390_inject_prog_irq(struct kvm_vcpu *vcpu,
987 struct kvm_s390_pgm_info *pgm_info)
989 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
990 struct kvm_s390_irq irq;
991 int rc;
993 VCPU_EVENT(vcpu, 3, "inject: prog irq %d (from kernel)",
994 pgm_info->code);
995 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_PROGRAM_INT,
996 pgm_info->code, 0, 1);
997 spin_lock(&li->lock);
998 irq.u.pgm = *pgm_info;
999 rc = __inject_prog(vcpu, &irq);
1000 BUG_ON(waitqueue_active(li->wq));
1001 spin_unlock(&li->lock);
1002 return rc;
1005 static int __inject_pfault_init(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1007 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1009 VCPU_EVENT(vcpu, 3, "inject: external irq params:%x, params2:%llx",
1010 irq->u.ext.ext_params, irq->u.ext.ext_params2);
1011 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_PFAULT_INIT,
1012 irq->u.ext.ext_params,
1013 irq->u.ext.ext_params2, 2);
1015 li->irq.ext = irq->u.ext;
1016 set_bit(IRQ_PEND_PFAULT_INIT, &li->pending_irqs);
1017 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1018 return 0;
1021 static int __inject_extcall_sigpif(struct kvm_vcpu *vcpu, uint16_t src_id)
1023 unsigned char new_val, old_val;
1024 uint8_t *sigp_ctrl = &vcpu->kvm->arch.sca->cpu[vcpu->vcpu_id].sigp_ctrl;
1026 new_val = SIGP_CTRL_C | (src_id & SIGP_CTRL_SCN_MASK);
1027 old_val = *sigp_ctrl & ~SIGP_CTRL_C;
1028 if (cmpxchg(sigp_ctrl, old_val, new_val) != old_val) {
1029 /* another external call is pending */
1030 return -EBUSY;
1032 atomic_set_mask(CPUSTAT_ECALL_PEND, &vcpu->arch.sie_block->cpuflags);
1033 return 0;
1036 static int __inject_extcall(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1038 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1039 struct kvm_s390_extcall_info *extcall = &li->irq.extcall;
1040 uint16_t src_id = irq->u.extcall.code;
1042 VCPU_EVENT(vcpu, 3, "inject: external call source-cpu:%u",
1043 src_id);
1044 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EXTERNAL_CALL,
1045 src_id, 0, 2);
1047 /* sending vcpu invalid */
1048 if (src_id >= KVM_MAX_VCPUS ||
1049 kvm_get_vcpu(vcpu->kvm, src_id) == NULL)
1050 return -EINVAL;
1052 if (sclp_has_sigpif())
1053 return __inject_extcall_sigpif(vcpu, src_id);
1055 if (!test_and_set_bit(IRQ_PEND_EXT_EXTERNAL, &li->pending_irqs))
1056 return -EBUSY;
1057 *extcall = irq->u.extcall;
1058 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1059 return 0;
1062 static int __inject_set_prefix(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1064 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1065 struct kvm_s390_prefix_info *prefix = &li->irq.prefix;
1067 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
1068 irq->u.prefix.address);
1069 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_SET_PREFIX,
1070 irq->u.prefix.address, 0, 2);
1072 if (!is_vcpu_stopped(vcpu))
1073 return -EBUSY;
1075 *prefix = irq->u.prefix;
1076 set_bit(IRQ_PEND_SET_PREFIX, &li->pending_irqs);
1077 return 0;
1080 #define KVM_S390_STOP_SUPP_FLAGS (KVM_S390_STOP_FLAG_STORE_STATUS)
1081 static int __inject_sigp_stop(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1083 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1084 struct kvm_s390_stop_info *stop = &li->irq.stop;
1085 int rc = 0;
1087 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_SIGP_STOP, 0, 0, 2);
1089 if (irq->u.stop.flags & ~KVM_S390_STOP_SUPP_FLAGS)
1090 return -EINVAL;
1092 if (is_vcpu_stopped(vcpu)) {
1093 if (irq->u.stop.flags & KVM_S390_STOP_FLAG_STORE_STATUS)
1094 rc = kvm_s390_store_status_unloaded(vcpu,
1095 KVM_S390_STORE_STATUS_NOADDR);
1096 return rc;
1099 if (test_and_set_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs))
1100 return -EBUSY;
1101 stop->flags = irq->u.stop.flags;
1102 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
1103 return 0;
1106 static int __inject_sigp_restart(struct kvm_vcpu *vcpu,
1107 struct kvm_s390_irq *irq)
1109 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1111 VCPU_EVENT(vcpu, 3, "inject: restart type %llx", irq->type);
1112 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_RESTART, 0, 0, 2);
1114 set_bit(IRQ_PEND_RESTART, &li->pending_irqs);
1115 return 0;
1118 static int __inject_sigp_emergency(struct kvm_vcpu *vcpu,
1119 struct kvm_s390_irq *irq)
1121 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1123 VCPU_EVENT(vcpu, 3, "inject: emergency %u\n",
1124 irq->u.emerg.code);
1125 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_EMERGENCY,
1126 irq->u.emerg.code, 0, 2);
1128 set_bit(irq->u.emerg.code, li->sigp_emerg_pending);
1129 set_bit(IRQ_PEND_EXT_EMERGENCY, &li->pending_irqs);
1130 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1131 return 0;
1134 static int __inject_mchk(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1136 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1137 struct kvm_s390_mchk_info *mchk = &li->irq.mchk;
1139 VCPU_EVENT(vcpu, 5, "inject: machine check parm64:%llx",
1140 irq->u.mchk.mcic);
1141 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_MCHK, 0,
1142 irq->u.mchk.mcic, 2);
1145 * Because repressible machine checks can be indicated along with
1146 * exigent machine checks (PoP, Chapter 11, Interruption action)
1147 * we need to combine cr14, mcic and external damage code.
1148 * Failing storage address and the logout area should not be or'ed
1149 * together, we just indicate the last occurrence of the corresponding
1150 * machine check
1152 mchk->cr14 |= irq->u.mchk.cr14;
1153 mchk->mcic |= irq->u.mchk.mcic;
1154 mchk->ext_damage_code |= irq->u.mchk.ext_damage_code;
1155 mchk->failing_storage_address = irq->u.mchk.failing_storage_address;
1156 memcpy(&mchk->fixed_logout, &irq->u.mchk.fixed_logout,
1157 sizeof(mchk->fixed_logout));
1158 if (mchk->mcic & MCHK_EX_MASK)
1159 set_bit(IRQ_PEND_MCHK_EX, &li->pending_irqs);
1160 else if (mchk->mcic & MCHK_REP_MASK)
1161 set_bit(IRQ_PEND_MCHK_REP, &li->pending_irqs);
1162 return 0;
1165 static int __inject_ckc(struct kvm_vcpu *vcpu)
1167 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1169 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CLOCK_COMP);
1170 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CLOCK_COMP,
1171 0, 0, 2);
1173 set_bit(IRQ_PEND_EXT_CLOCK_COMP, &li->pending_irqs);
1174 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1175 return 0;
1178 static int __inject_cpu_timer(struct kvm_vcpu *vcpu)
1180 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1182 VCPU_EVENT(vcpu, 3, "inject: type %x", KVM_S390_INT_CPU_TIMER);
1183 trace_kvm_s390_inject_vcpu(vcpu->vcpu_id, KVM_S390_INT_CPU_TIMER,
1184 0, 0, 2);
1186 set_bit(IRQ_PEND_EXT_CPU_TIMER, &li->pending_irqs);
1187 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1188 return 0;
1192 struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,
1193 u64 cr6, u64 schid)
1195 struct kvm_s390_float_interrupt *fi;
1196 struct kvm_s390_interrupt_info *inti, *iter;
1198 if ((!schid && !cr6) || (schid && cr6))
1199 return NULL;
1200 fi = &kvm->arch.float_int;
1201 spin_lock(&fi->lock);
1202 inti = NULL;
1203 list_for_each_entry(iter, &fi->list, list) {
1204 if (!is_ioint(iter->type))
1205 continue;
1206 if (cr6 &&
1207 ((cr6 & int_word_to_isc_bits(iter->io.io_int_word)) == 0))
1208 continue;
1209 if (schid) {
1210 if (((schid & 0x00000000ffff0000) >> 16) !=
1211 iter->io.subchannel_id)
1212 continue;
1213 if ((schid & 0x000000000000ffff) !=
1214 iter->io.subchannel_nr)
1215 continue;
1217 inti = iter;
1218 break;
1220 if (inti) {
1221 list_del_init(&inti->list);
1222 fi->irq_count--;
1224 if (list_empty(&fi->list))
1225 atomic_set(&fi->active, 0);
1226 spin_unlock(&fi->lock);
1227 return inti;
1230 static int __inject_vm(struct kvm *kvm, struct kvm_s390_interrupt_info *inti)
1232 struct kvm_s390_local_interrupt *li;
1233 struct kvm_s390_float_interrupt *fi;
1234 struct kvm_s390_interrupt_info *iter;
1235 struct kvm_vcpu *dst_vcpu = NULL;
1236 int sigcpu;
1237 int rc = 0;
1239 fi = &kvm->arch.float_int;
1240 spin_lock(&fi->lock);
1241 if (fi->irq_count >= KVM_S390_MAX_FLOAT_IRQS) {
1242 rc = -EINVAL;
1243 goto unlock_fi;
1245 fi->irq_count++;
1246 if (!is_ioint(inti->type)) {
1247 list_add_tail(&inti->list, &fi->list);
1248 } else {
1249 u64 isc_bits = int_word_to_isc_bits(inti->io.io_int_word);
1251 /* Keep I/O interrupts sorted in isc order. */
1252 list_for_each_entry(iter, &fi->list, list) {
1253 if (!is_ioint(iter->type))
1254 continue;
1255 if (int_word_to_isc_bits(iter->io.io_int_word)
1256 <= isc_bits)
1257 continue;
1258 break;
1260 list_add_tail(&inti->list, &iter->list);
1262 atomic_set(&fi->active, 1);
1263 if (atomic_read(&kvm->online_vcpus) == 0)
1264 goto unlock_fi;
1265 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
1266 if (sigcpu == KVM_MAX_VCPUS) {
1267 do {
1268 sigcpu = fi->next_rr_cpu++;
1269 if (sigcpu == KVM_MAX_VCPUS)
1270 sigcpu = fi->next_rr_cpu = 0;
1271 } while (kvm_get_vcpu(kvm, sigcpu) == NULL);
1273 dst_vcpu = kvm_get_vcpu(kvm, sigcpu);
1274 li = &dst_vcpu->arch.local_int;
1275 spin_lock(&li->lock);
1276 switch (inti->type) {
1277 case KVM_S390_MCHK:
1278 atomic_set_mask(CPUSTAT_STOP_INT, li->cpuflags);
1279 break;
1280 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1281 atomic_set_mask(CPUSTAT_IO_INT, li->cpuflags);
1282 break;
1283 default:
1284 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
1285 break;
1287 spin_unlock(&li->lock);
1288 kvm_s390_vcpu_wakeup(kvm_get_vcpu(kvm, sigcpu));
1289 unlock_fi:
1290 spin_unlock(&fi->lock);
1291 return rc;
1294 int kvm_s390_inject_vm(struct kvm *kvm,
1295 struct kvm_s390_interrupt *s390int)
1297 struct kvm_s390_interrupt_info *inti;
1298 int rc;
1300 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1301 if (!inti)
1302 return -ENOMEM;
1304 inti->type = s390int->type;
1305 switch (inti->type) {
1306 case KVM_S390_INT_VIRTIO:
1307 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
1308 s390int->parm, s390int->parm64);
1309 inti->ext.ext_params = s390int->parm;
1310 inti->ext.ext_params2 = s390int->parm64;
1311 break;
1312 case KVM_S390_INT_SERVICE:
1313 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
1314 inti->ext.ext_params = s390int->parm;
1315 break;
1316 case KVM_S390_INT_PFAULT_DONE:
1317 inti->ext.ext_params2 = s390int->parm64;
1318 break;
1319 case KVM_S390_MCHK:
1320 VM_EVENT(kvm, 5, "inject: machine check parm64:%llx",
1321 s390int->parm64);
1322 inti->mchk.cr14 = s390int->parm; /* upper bits are not used */
1323 inti->mchk.mcic = s390int->parm64;
1324 break;
1325 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1326 if (inti->type & IOINT_AI_MASK)
1327 VM_EVENT(kvm, 5, "%s", "inject: I/O (AI)");
1328 else
1329 VM_EVENT(kvm, 5, "inject: I/O css %x ss %x schid %04x",
1330 s390int->type & IOINT_CSSID_MASK,
1331 s390int->type & IOINT_SSID_MASK,
1332 s390int->type & IOINT_SCHID_MASK);
1333 inti->io.subchannel_id = s390int->parm >> 16;
1334 inti->io.subchannel_nr = s390int->parm & 0x0000ffffu;
1335 inti->io.io_int_parm = s390int->parm64 >> 32;
1336 inti->io.io_int_word = s390int->parm64 & 0x00000000ffffffffull;
1337 break;
1338 default:
1339 kfree(inti);
1340 return -EINVAL;
1342 trace_kvm_s390_inject_vm(s390int->type, s390int->parm, s390int->parm64,
1345 rc = __inject_vm(kvm, inti);
1346 if (rc)
1347 kfree(inti);
1348 return rc;
1351 int kvm_s390_reinject_io_int(struct kvm *kvm,
1352 struct kvm_s390_interrupt_info *inti)
1354 return __inject_vm(kvm, inti);
1357 int s390int_to_s390irq(struct kvm_s390_interrupt *s390int,
1358 struct kvm_s390_irq *irq)
1360 irq->type = s390int->type;
1361 switch (irq->type) {
1362 case KVM_S390_PROGRAM_INT:
1363 if (s390int->parm & 0xffff0000)
1364 return -EINVAL;
1365 irq->u.pgm.code = s390int->parm;
1366 break;
1367 case KVM_S390_SIGP_SET_PREFIX:
1368 irq->u.prefix.address = s390int->parm;
1369 break;
1370 case KVM_S390_SIGP_STOP:
1371 irq->u.stop.flags = s390int->parm;
1372 break;
1373 case KVM_S390_INT_EXTERNAL_CALL:
1374 if (s390int->parm & 0xffff0000)
1375 return -EINVAL;
1376 irq->u.extcall.code = s390int->parm;
1377 break;
1378 case KVM_S390_INT_EMERGENCY:
1379 if (s390int->parm & 0xffff0000)
1380 return -EINVAL;
1381 irq->u.emerg.code = s390int->parm;
1382 break;
1383 case KVM_S390_MCHK:
1384 irq->u.mchk.mcic = s390int->parm64;
1385 break;
1387 return 0;
1390 int kvm_s390_is_stop_irq_pending(struct kvm_vcpu *vcpu)
1392 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1394 return test_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1397 void kvm_s390_clear_stop_irq(struct kvm_vcpu *vcpu)
1399 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1401 spin_lock(&li->lock);
1402 li->irq.stop.flags = 0;
1403 clear_bit(IRQ_PEND_SIGP_STOP, &li->pending_irqs);
1404 spin_unlock(&li->lock);
1407 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu, struct kvm_s390_irq *irq)
1409 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
1410 int rc;
1412 spin_lock(&li->lock);
1413 switch (irq->type) {
1414 case KVM_S390_PROGRAM_INT:
1415 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
1416 irq->u.pgm.code);
1417 rc = __inject_prog(vcpu, irq);
1418 break;
1419 case KVM_S390_SIGP_SET_PREFIX:
1420 rc = __inject_set_prefix(vcpu, irq);
1421 break;
1422 case KVM_S390_SIGP_STOP:
1423 rc = __inject_sigp_stop(vcpu, irq);
1424 break;
1425 case KVM_S390_RESTART:
1426 rc = __inject_sigp_restart(vcpu, irq);
1427 break;
1428 case KVM_S390_INT_CLOCK_COMP:
1429 rc = __inject_ckc(vcpu);
1430 break;
1431 case KVM_S390_INT_CPU_TIMER:
1432 rc = __inject_cpu_timer(vcpu);
1433 break;
1434 case KVM_S390_INT_EXTERNAL_CALL:
1435 rc = __inject_extcall(vcpu, irq);
1436 break;
1437 case KVM_S390_INT_EMERGENCY:
1438 rc = __inject_sigp_emergency(vcpu, irq);
1439 break;
1440 case KVM_S390_MCHK:
1441 rc = __inject_mchk(vcpu, irq);
1442 break;
1443 case KVM_S390_INT_PFAULT_INIT:
1444 rc = __inject_pfault_init(vcpu, irq);
1445 break;
1446 case KVM_S390_INT_VIRTIO:
1447 case KVM_S390_INT_SERVICE:
1448 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1449 default:
1450 rc = -EINVAL;
1452 spin_unlock(&li->lock);
1453 if (!rc)
1454 kvm_s390_vcpu_wakeup(vcpu);
1455 return rc;
1458 void kvm_s390_clear_float_irqs(struct kvm *kvm)
1460 struct kvm_s390_float_interrupt *fi;
1461 struct kvm_s390_interrupt_info *n, *inti = NULL;
1463 fi = &kvm->arch.float_int;
1464 spin_lock(&fi->lock);
1465 list_for_each_entry_safe(inti, n, &fi->list, list) {
1466 list_del(&inti->list);
1467 kfree(inti);
1469 fi->irq_count = 0;
1470 atomic_set(&fi->active, 0);
1471 spin_unlock(&fi->lock);
1474 static inline int copy_irq_to_user(struct kvm_s390_interrupt_info *inti,
1475 u8 *addr)
1477 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1478 struct kvm_s390_irq irq = {0};
1480 irq.type = inti->type;
1481 switch (inti->type) {
1482 case KVM_S390_INT_PFAULT_INIT:
1483 case KVM_S390_INT_PFAULT_DONE:
1484 case KVM_S390_INT_VIRTIO:
1485 case KVM_S390_INT_SERVICE:
1486 irq.u.ext = inti->ext;
1487 break;
1488 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1489 irq.u.io = inti->io;
1490 break;
1491 case KVM_S390_MCHK:
1492 irq.u.mchk = inti->mchk;
1493 break;
1494 default:
1495 return -EINVAL;
1498 if (copy_to_user(uptr, &irq, sizeof(irq)))
1499 return -EFAULT;
1501 return 0;
1504 static int get_all_floating_irqs(struct kvm *kvm, __u8 *buf, __u64 len)
1506 struct kvm_s390_interrupt_info *inti;
1507 struct kvm_s390_float_interrupt *fi;
1508 int ret = 0;
1509 int n = 0;
1511 fi = &kvm->arch.float_int;
1512 spin_lock(&fi->lock);
1514 list_for_each_entry(inti, &fi->list, list) {
1515 if (len < sizeof(struct kvm_s390_irq)) {
1516 /* signal userspace to try again */
1517 ret = -ENOMEM;
1518 break;
1520 ret = copy_irq_to_user(inti, buf);
1521 if (ret)
1522 break;
1523 buf += sizeof(struct kvm_s390_irq);
1524 len -= sizeof(struct kvm_s390_irq);
1525 n++;
1528 spin_unlock(&fi->lock);
1530 return ret < 0 ? ret : n;
1533 static int flic_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1535 int r;
1537 switch (attr->group) {
1538 case KVM_DEV_FLIC_GET_ALL_IRQS:
1539 r = get_all_floating_irqs(dev->kvm, (u8 *) attr->addr,
1540 attr->attr);
1541 break;
1542 default:
1543 r = -EINVAL;
1546 return r;
1549 static inline int copy_irq_from_user(struct kvm_s390_interrupt_info *inti,
1550 u64 addr)
1552 struct kvm_s390_irq __user *uptr = (struct kvm_s390_irq __user *) addr;
1553 void *target = NULL;
1554 void __user *source;
1555 u64 size;
1557 if (get_user(inti->type, (u64 __user *)addr))
1558 return -EFAULT;
1560 switch (inti->type) {
1561 case KVM_S390_INT_PFAULT_INIT:
1562 case KVM_S390_INT_PFAULT_DONE:
1563 case KVM_S390_INT_VIRTIO:
1564 case KVM_S390_INT_SERVICE:
1565 target = (void *) &inti->ext;
1566 source = &uptr->u.ext;
1567 size = sizeof(inti->ext);
1568 break;
1569 case KVM_S390_INT_IO_MIN...KVM_S390_INT_IO_MAX:
1570 target = (void *) &inti->io;
1571 source = &uptr->u.io;
1572 size = sizeof(inti->io);
1573 break;
1574 case KVM_S390_MCHK:
1575 target = (void *) &inti->mchk;
1576 source = &uptr->u.mchk;
1577 size = sizeof(inti->mchk);
1578 break;
1579 default:
1580 return -EINVAL;
1583 if (copy_from_user(target, source, size))
1584 return -EFAULT;
1586 return 0;
1589 static int enqueue_floating_irq(struct kvm_device *dev,
1590 struct kvm_device_attr *attr)
1592 struct kvm_s390_interrupt_info *inti = NULL;
1593 int r = 0;
1594 int len = attr->attr;
1596 if (len % sizeof(struct kvm_s390_irq) != 0)
1597 return -EINVAL;
1598 else if (len > KVM_S390_FLIC_MAX_BUFFER)
1599 return -EINVAL;
1601 while (len >= sizeof(struct kvm_s390_irq)) {
1602 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
1603 if (!inti)
1604 return -ENOMEM;
1606 r = copy_irq_from_user(inti, attr->addr);
1607 if (r) {
1608 kfree(inti);
1609 return r;
1611 r = __inject_vm(dev->kvm, inti);
1612 if (r) {
1613 kfree(inti);
1614 return r;
1616 len -= sizeof(struct kvm_s390_irq);
1617 attr->addr += sizeof(struct kvm_s390_irq);
1620 return r;
1623 static struct s390_io_adapter *get_io_adapter(struct kvm *kvm, unsigned int id)
1625 if (id >= MAX_S390_IO_ADAPTERS)
1626 return NULL;
1627 return kvm->arch.adapters[id];
1630 static int register_io_adapter(struct kvm_device *dev,
1631 struct kvm_device_attr *attr)
1633 struct s390_io_adapter *adapter;
1634 struct kvm_s390_io_adapter adapter_info;
1636 if (copy_from_user(&adapter_info,
1637 (void __user *)attr->addr, sizeof(adapter_info)))
1638 return -EFAULT;
1640 if ((adapter_info.id >= MAX_S390_IO_ADAPTERS) ||
1641 (dev->kvm->arch.adapters[adapter_info.id] != NULL))
1642 return -EINVAL;
1644 adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
1645 if (!adapter)
1646 return -ENOMEM;
1648 INIT_LIST_HEAD(&adapter->maps);
1649 init_rwsem(&adapter->maps_lock);
1650 atomic_set(&adapter->nr_maps, 0);
1651 adapter->id = adapter_info.id;
1652 adapter->isc = adapter_info.isc;
1653 adapter->maskable = adapter_info.maskable;
1654 adapter->masked = false;
1655 adapter->swap = adapter_info.swap;
1656 dev->kvm->arch.adapters[adapter->id] = adapter;
1658 return 0;
1661 int kvm_s390_mask_adapter(struct kvm *kvm, unsigned int id, bool masked)
1663 int ret;
1664 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1666 if (!adapter || !adapter->maskable)
1667 return -EINVAL;
1668 ret = adapter->masked;
1669 adapter->masked = masked;
1670 return ret;
1673 static int kvm_s390_adapter_map(struct kvm *kvm, unsigned int id, __u64 addr)
1675 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1676 struct s390_map_info *map;
1677 int ret;
1679 if (!adapter || !addr)
1680 return -EINVAL;
1682 map = kzalloc(sizeof(*map), GFP_KERNEL);
1683 if (!map) {
1684 ret = -ENOMEM;
1685 goto out;
1687 INIT_LIST_HEAD(&map->list);
1688 map->guest_addr = addr;
1689 map->addr = gmap_translate(kvm->arch.gmap, addr);
1690 if (map->addr == -EFAULT) {
1691 ret = -EFAULT;
1692 goto out;
1694 ret = get_user_pages_fast(map->addr, 1, 1, &map->page);
1695 if (ret < 0)
1696 goto out;
1697 BUG_ON(ret != 1);
1698 down_write(&adapter->maps_lock);
1699 if (atomic_inc_return(&adapter->nr_maps) < MAX_S390_ADAPTER_MAPS) {
1700 list_add_tail(&map->list, &adapter->maps);
1701 ret = 0;
1702 } else {
1703 put_page(map->page);
1704 ret = -EINVAL;
1706 up_write(&adapter->maps_lock);
1707 out:
1708 if (ret)
1709 kfree(map);
1710 return ret;
1713 static int kvm_s390_adapter_unmap(struct kvm *kvm, unsigned int id, __u64 addr)
1715 struct s390_io_adapter *adapter = get_io_adapter(kvm, id);
1716 struct s390_map_info *map, *tmp;
1717 int found = 0;
1719 if (!adapter || !addr)
1720 return -EINVAL;
1722 down_write(&adapter->maps_lock);
1723 list_for_each_entry_safe(map, tmp, &adapter->maps, list) {
1724 if (map->guest_addr == addr) {
1725 found = 1;
1726 atomic_dec(&adapter->nr_maps);
1727 list_del(&map->list);
1728 put_page(map->page);
1729 kfree(map);
1730 break;
1733 up_write(&adapter->maps_lock);
1735 return found ? 0 : -EINVAL;
1738 void kvm_s390_destroy_adapters(struct kvm *kvm)
1740 int i;
1741 struct s390_map_info *map, *tmp;
1743 for (i = 0; i < MAX_S390_IO_ADAPTERS; i++) {
1744 if (!kvm->arch.adapters[i])
1745 continue;
1746 list_for_each_entry_safe(map, tmp,
1747 &kvm->arch.adapters[i]->maps, list) {
1748 list_del(&map->list);
1749 put_page(map->page);
1750 kfree(map);
1752 kfree(kvm->arch.adapters[i]);
1756 static int modify_io_adapter(struct kvm_device *dev,
1757 struct kvm_device_attr *attr)
1759 struct kvm_s390_io_adapter_req req;
1760 struct s390_io_adapter *adapter;
1761 int ret;
1763 if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
1764 return -EFAULT;
1766 adapter = get_io_adapter(dev->kvm, req.id);
1767 if (!adapter)
1768 return -EINVAL;
1769 switch (req.type) {
1770 case KVM_S390_IO_ADAPTER_MASK:
1771 ret = kvm_s390_mask_adapter(dev->kvm, req.id, req.mask);
1772 if (ret > 0)
1773 ret = 0;
1774 break;
1775 case KVM_S390_IO_ADAPTER_MAP:
1776 ret = kvm_s390_adapter_map(dev->kvm, req.id, req.addr);
1777 break;
1778 case KVM_S390_IO_ADAPTER_UNMAP:
1779 ret = kvm_s390_adapter_unmap(dev->kvm, req.id, req.addr);
1780 break;
1781 default:
1782 ret = -EINVAL;
1785 return ret;
1788 static int flic_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1790 int r = 0;
1791 unsigned int i;
1792 struct kvm_vcpu *vcpu;
1794 switch (attr->group) {
1795 case KVM_DEV_FLIC_ENQUEUE:
1796 r = enqueue_floating_irq(dev, attr);
1797 break;
1798 case KVM_DEV_FLIC_CLEAR_IRQS:
1799 kvm_s390_clear_float_irqs(dev->kvm);
1800 break;
1801 case KVM_DEV_FLIC_APF_ENABLE:
1802 dev->kvm->arch.gmap->pfault_enabled = 1;
1803 break;
1804 case KVM_DEV_FLIC_APF_DISABLE_WAIT:
1805 dev->kvm->arch.gmap->pfault_enabled = 0;
1807 * Make sure no async faults are in transition when
1808 * clearing the queues. So we don't need to worry
1809 * about late coming workers.
1811 synchronize_srcu(&dev->kvm->srcu);
1812 kvm_for_each_vcpu(i, vcpu, dev->kvm)
1813 kvm_clear_async_pf_completion_queue(vcpu);
1814 break;
1815 case KVM_DEV_FLIC_ADAPTER_REGISTER:
1816 r = register_io_adapter(dev, attr);
1817 break;
1818 case KVM_DEV_FLIC_ADAPTER_MODIFY:
1819 r = modify_io_adapter(dev, attr);
1820 break;
1821 default:
1822 r = -EINVAL;
1825 return r;
1828 static int flic_create(struct kvm_device *dev, u32 type)
1830 if (!dev)
1831 return -EINVAL;
1832 if (dev->kvm->arch.flic)
1833 return -EINVAL;
1834 dev->kvm->arch.flic = dev;
1835 return 0;
1838 static void flic_destroy(struct kvm_device *dev)
1840 dev->kvm->arch.flic = NULL;
1841 kfree(dev);
1844 /* s390 floating irq controller (flic) */
1845 struct kvm_device_ops kvm_flic_ops = {
1846 .name = "kvm-flic",
1847 .get_attr = flic_get_attr,
1848 .set_attr = flic_set_attr,
1849 .create = flic_create,
1850 .destroy = flic_destroy,
1853 static unsigned long get_ind_bit(__u64 addr, unsigned long bit_nr, bool swap)
1855 unsigned long bit;
1857 bit = bit_nr + (addr % PAGE_SIZE) * 8;
1859 return swap ? (bit ^ (BITS_PER_LONG - 1)) : bit;
1862 static struct s390_map_info *get_map_info(struct s390_io_adapter *adapter,
1863 u64 addr)
1865 struct s390_map_info *map;
1867 if (!adapter)
1868 return NULL;
1870 list_for_each_entry(map, &adapter->maps, list) {
1871 if (map->guest_addr == addr)
1872 return map;
1874 return NULL;
1877 static int adapter_indicators_set(struct kvm *kvm,
1878 struct s390_io_adapter *adapter,
1879 struct kvm_s390_adapter_int *adapter_int)
1881 unsigned long bit;
1882 int summary_set, idx;
1883 struct s390_map_info *info;
1884 void *map;
1886 info = get_map_info(adapter, adapter_int->ind_addr);
1887 if (!info)
1888 return -1;
1889 map = page_address(info->page);
1890 bit = get_ind_bit(info->addr, adapter_int->ind_offset, adapter->swap);
1891 set_bit(bit, map);
1892 idx = srcu_read_lock(&kvm->srcu);
1893 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1894 set_page_dirty_lock(info->page);
1895 info = get_map_info(adapter, adapter_int->summary_addr);
1896 if (!info) {
1897 srcu_read_unlock(&kvm->srcu, idx);
1898 return -1;
1900 map = page_address(info->page);
1901 bit = get_ind_bit(info->addr, adapter_int->summary_offset,
1902 adapter->swap);
1903 summary_set = test_and_set_bit(bit, map);
1904 mark_page_dirty(kvm, info->guest_addr >> PAGE_SHIFT);
1905 set_page_dirty_lock(info->page);
1906 srcu_read_unlock(&kvm->srcu, idx);
1907 return summary_set ? 0 : 1;
1911 * < 0 - not injected due to error
1912 * = 0 - coalesced, summary indicator already active
1913 * > 0 - injected interrupt
1915 static int set_adapter_int(struct kvm_kernel_irq_routing_entry *e,
1916 struct kvm *kvm, int irq_source_id, int level,
1917 bool line_status)
1919 int ret;
1920 struct s390_io_adapter *adapter;
1922 /* We're only interested in the 0->1 transition. */
1923 if (!level)
1924 return 0;
1925 adapter = get_io_adapter(kvm, e->adapter.adapter_id);
1926 if (!adapter)
1927 return -1;
1928 down_read(&adapter->maps_lock);
1929 ret = adapter_indicators_set(kvm, adapter, &e->adapter);
1930 up_read(&adapter->maps_lock);
1931 if ((ret > 0) && !adapter->masked) {
1932 struct kvm_s390_interrupt s390int = {
1933 .type = KVM_S390_INT_IO(1, 0, 0, 0),
1934 .parm = 0,
1935 .parm64 = (adapter->isc << 27) | 0x80000000,
1937 ret = kvm_s390_inject_vm(kvm, &s390int);
1938 if (ret == 0)
1939 ret = 1;
1941 return ret;
1944 int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e,
1945 const struct kvm_irq_routing_entry *ue)
1947 int ret;
1949 switch (ue->type) {
1950 case KVM_IRQ_ROUTING_S390_ADAPTER:
1951 e->set = set_adapter_int;
1952 e->adapter.summary_addr = ue->u.adapter.summary_addr;
1953 e->adapter.ind_addr = ue->u.adapter.ind_addr;
1954 e->adapter.summary_offset = ue->u.adapter.summary_offset;
1955 e->adapter.ind_offset = ue->u.adapter.ind_offset;
1956 e->adapter.adapter_id = ue->u.adapter.adapter_id;
1957 ret = 0;
1958 break;
1959 default:
1960 ret = -EINVAL;
1963 return ret;
1966 int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1967 int irq_source_id, int level, bool line_status)
1969 return -EINVAL;