2 * handling kvm guest interrupts
4 * Copyright IBM Corp. 2008
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/signal.h>
17 #include <linux/slab.h>
18 #include <asm/asm-offsets.h>
19 #include <asm/uaccess.h>
22 #include "trace-s390.h"
24 #define IOINT_SCHID_MASK 0x0000ffff
25 #define IOINT_SSID_MASK 0x00030000
26 #define IOINT_CSSID_MASK 0x03fc0000
27 #define IOINT_AI_MASK 0x04000000
29 static int is_ioint(u64 type
)
31 return ((type
& 0xfffe0000u
) != 0xfffe0000u
);
34 static int psw_extint_disabled(struct kvm_vcpu
*vcpu
)
36 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_EXT
);
39 static int psw_ioint_disabled(struct kvm_vcpu
*vcpu
)
41 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_IO
);
44 static int psw_mchk_disabled(struct kvm_vcpu
*vcpu
)
46 return !(vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_MCHECK
);
49 static int psw_interrupts_disabled(struct kvm_vcpu
*vcpu
)
51 if ((vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_PER
) ||
52 (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_IO
) ||
53 (vcpu
->arch
.sie_block
->gpsw
.mask
& PSW_MASK_EXT
))
58 static u64
int_word_to_isc_bits(u32 int_word
)
60 u8 isc
= (int_word
& 0x38000000) >> 27;
62 return (0x80 >> isc
) << 24;
65 static int __interrupt_is_deliverable(struct kvm_vcpu
*vcpu
,
66 struct kvm_s390_interrupt_info
*inti
)
69 case KVM_S390_INT_EXTERNAL_CALL
:
70 if (psw_extint_disabled(vcpu
))
72 if (vcpu
->arch
.sie_block
->gcr
[0] & 0x2000ul
)
74 case KVM_S390_INT_EMERGENCY
:
75 if (psw_extint_disabled(vcpu
))
77 if (vcpu
->arch
.sie_block
->gcr
[0] & 0x4000ul
)
80 case KVM_S390_INT_SERVICE
:
81 if (psw_extint_disabled(vcpu
))
83 if (vcpu
->arch
.sie_block
->gcr
[0] & 0x200ul
)
86 case KVM_S390_INT_VIRTIO
:
87 if (psw_extint_disabled(vcpu
))
89 if (vcpu
->arch
.sie_block
->gcr
[0] & 0x200ul
)
92 case KVM_S390_PROGRAM_INT
:
93 case KVM_S390_SIGP_STOP
:
94 case KVM_S390_SIGP_SET_PREFIX
:
95 case KVM_S390_RESTART
:
98 if (psw_mchk_disabled(vcpu
))
100 if (vcpu
->arch
.sie_block
->gcr
[14] & inti
->mchk
.cr14
)
103 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
104 if (psw_ioint_disabled(vcpu
))
106 if (vcpu
->arch
.sie_block
->gcr
[6] &
107 int_word_to_isc_bits(inti
->io
.io_int_word
))
111 printk(KERN_WARNING
"illegal interrupt type %llx\n",
118 static void __set_cpu_idle(struct kvm_vcpu
*vcpu
)
120 BUG_ON(vcpu
->vcpu_id
> KVM_MAX_VCPUS
- 1);
121 atomic_set_mask(CPUSTAT_WAIT
, &vcpu
->arch
.sie_block
->cpuflags
);
122 set_bit(vcpu
->vcpu_id
, vcpu
->arch
.local_int
.float_int
->idle_mask
);
125 static void __unset_cpu_idle(struct kvm_vcpu
*vcpu
)
127 BUG_ON(vcpu
->vcpu_id
> KVM_MAX_VCPUS
- 1);
128 atomic_clear_mask(CPUSTAT_WAIT
, &vcpu
->arch
.sie_block
->cpuflags
);
129 clear_bit(vcpu
->vcpu_id
, vcpu
->arch
.local_int
.float_int
->idle_mask
);
132 static void __reset_intercept_indicators(struct kvm_vcpu
*vcpu
)
134 atomic_clear_mask(CPUSTAT_ECALL_PEND
|
135 CPUSTAT_IO_INT
| CPUSTAT_EXT_INT
| CPUSTAT_STOP_INT
,
136 &vcpu
->arch
.sie_block
->cpuflags
);
137 vcpu
->arch
.sie_block
->lctl
= 0x0000;
138 vcpu
->arch
.sie_block
->ictl
&= ~ICTL_LPSW
;
141 static void __set_cpuflag(struct kvm_vcpu
*vcpu
, u32 flag
)
143 atomic_set_mask(flag
, &vcpu
->arch
.sie_block
->cpuflags
);
146 static void __set_intercept_indicator(struct kvm_vcpu
*vcpu
,
147 struct kvm_s390_interrupt_info
*inti
)
149 switch (inti
->type
) {
150 case KVM_S390_INT_EXTERNAL_CALL
:
151 case KVM_S390_INT_EMERGENCY
:
152 case KVM_S390_INT_SERVICE
:
153 case KVM_S390_INT_VIRTIO
:
154 if (psw_extint_disabled(vcpu
))
155 __set_cpuflag(vcpu
, CPUSTAT_EXT_INT
);
157 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR0
;
159 case KVM_S390_SIGP_STOP
:
160 __set_cpuflag(vcpu
, CPUSTAT_STOP_INT
);
163 if (psw_mchk_disabled(vcpu
))
164 vcpu
->arch
.sie_block
->ictl
|= ICTL_LPSW
;
166 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR14
;
168 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
169 if (psw_ioint_disabled(vcpu
))
170 __set_cpuflag(vcpu
, CPUSTAT_IO_INT
);
172 vcpu
->arch
.sie_block
->lctl
|= LCTL_CR6
;
179 static void __do_deliver_interrupt(struct kvm_vcpu
*vcpu
,
180 struct kvm_s390_interrupt_info
*inti
)
182 const unsigned short table
[] = { 2, 4, 4, 6 };
183 int rc
, exception
= 0;
185 switch (inti
->type
) {
186 case KVM_S390_INT_EMERGENCY
:
187 VCPU_EVENT(vcpu
, 4, "%s", "interrupt: sigp emerg");
188 vcpu
->stat
.deliver_emergency_signal
++;
189 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
190 inti
->emerg
.code
, 0);
191 rc
= put_guest_u16(vcpu
, __LC_EXT_INT_CODE
, 0x1201);
195 rc
= put_guest_u16(vcpu
, __LC_EXT_CPU_ADDR
, inti
->emerg
.code
);
199 rc
= copy_to_guest(vcpu
, __LC_EXT_OLD_PSW
,
200 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
204 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
205 __LC_EXT_NEW_PSW
, sizeof(psw_t
));
210 case KVM_S390_INT_EXTERNAL_CALL
:
211 VCPU_EVENT(vcpu
, 4, "%s", "interrupt: sigp ext call");
212 vcpu
->stat
.deliver_external_call
++;
213 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
214 inti
->extcall
.code
, 0);
215 rc
= put_guest_u16(vcpu
, __LC_EXT_INT_CODE
, 0x1202);
219 rc
= put_guest_u16(vcpu
, __LC_EXT_CPU_ADDR
, inti
->extcall
.code
);
223 rc
= copy_to_guest(vcpu
, __LC_EXT_OLD_PSW
,
224 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
228 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
229 __LC_EXT_NEW_PSW
, sizeof(psw_t
));
234 case KVM_S390_INT_SERVICE
:
235 VCPU_EVENT(vcpu
, 4, "interrupt: sclp parm:%x",
236 inti
->ext
.ext_params
);
237 vcpu
->stat
.deliver_service_signal
++;
238 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
239 inti
->ext
.ext_params
, 0);
240 rc
= put_guest_u16(vcpu
, __LC_EXT_INT_CODE
, 0x2401);
244 rc
= copy_to_guest(vcpu
, __LC_EXT_OLD_PSW
,
245 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
249 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
250 __LC_EXT_NEW_PSW
, sizeof(psw_t
));
254 rc
= put_guest_u32(vcpu
, __LC_EXT_PARAMS
, inti
->ext
.ext_params
);
259 case KVM_S390_INT_VIRTIO
:
260 VCPU_EVENT(vcpu
, 4, "interrupt: virtio parm:%x,parm64:%llx",
261 inti
->ext
.ext_params
, inti
->ext
.ext_params2
);
262 vcpu
->stat
.deliver_virtio_interrupt
++;
263 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
264 inti
->ext
.ext_params
,
265 inti
->ext
.ext_params2
);
266 rc
= put_guest_u16(vcpu
, __LC_EXT_INT_CODE
, 0x2603);
270 rc
= put_guest_u16(vcpu
, __LC_EXT_CPU_ADDR
, 0x0d00);
274 rc
= copy_to_guest(vcpu
, __LC_EXT_OLD_PSW
,
275 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
279 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
280 __LC_EXT_NEW_PSW
, sizeof(psw_t
));
284 rc
= put_guest_u32(vcpu
, __LC_EXT_PARAMS
, inti
->ext
.ext_params
);
288 rc
= put_guest_u64(vcpu
, __LC_EXT_PARAMS2
,
289 inti
->ext
.ext_params2
);
294 case KVM_S390_SIGP_STOP
:
295 VCPU_EVENT(vcpu
, 4, "%s", "interrupt: cpu stop");
296 vcpu
->stat
.deliver_stop_signal
++;
297 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
299 __set_intercept_indicator(vcpu
, inti
);
302 case KVM_S390_SIGP_SET_PREFIX
:
303 VCPU_EVENT(vcpu
, 4, "interrupt: set prefix to %x",
304 inti
->prefix
.address
);
305 vcpu
->stat
.deliver_prefix_signal
++;
306 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
307 inti
->prefix
.address
, 0);
308 kvm_s390_set_prefix(vcpu
, inti
->prefix
.address
);
311 case KVM_S390_RESTART
:
312 VCPU_EVENT(vcpu
, 4, "%s", "interrupt: cpu restart");
313 vcpu
->stat
.deliver_restart_signal
++;
314 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
316 rc
= copy_to_guest(vcpu
, offsetof(struct _lowcore
,
317 restart_old_psw
), &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
321 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
322 offsetof(struct _lowcore
, restart_psw
), sizeof(psw_t
));
325 atomic_clear_mask(CPUSTAT_STOPPED
, &vcpu
->arch
.sie_block
->cpuflags
);
328 case KVM_S390_PROGRAM_INT
:
329 VCPU_EVENT(vcpu
, 4, "interrupt: pgm check code:%x, ilc:%x",
331 table
[vcpu
->arch
.sie_block
->ipa
>> 14]);
332 vcpu
->stat
.deliver_program_int
++;
333 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
335 rc
= put_guest_u16(vcpu
, __LC_PGM_INT_CODE
, inti
->pgm
.code
);
339 rc
= put_guest_u16(vcpu
, __LC_PGM_ILC
,
340 table
[vcpu
->arch
.sie_block
->ipa
>> 14]);
344 rc
= copy_to_guest(vcpu
, __LC_PGM_OLD_PSW
,
345 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
349 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
350 __LC_PGM_NEW_PSW
, sizeof(psw_t
));
356 VCPU_EVENT(vcpu
, 4, "interrupt: machine check mcic=%llx",
358 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
361 rc
= kvm_s390_vcpu_store_status(vcpu
,
362 KVM_S390_STORE_STATUS_PREFIXED
);
366 rc
= put_guest_u64(vcpu
, __LC_MCCK_CODE
, inti
->mchk
.mcic
);
370 rc
= copy_to_guest(vcpu
, __LC_MCK_OLD_PSW
,
371 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
375 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
376 __LC_MCK_NEW_PSW
, sizeof(psw_t
));
381 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
383 __u32 param0
= ((__u32
)inti
->io
.subchannel_id
<< 16) |
384 inti
->io
.subchannel_nr
;
385 __u64 param1
= ((__u64
)inti
->io
.io_int_parm
<< 32) |
386 inti
->io
.io_int_word
;
387 VCPU_EVENT(vcpu
, 4, "interrupt: I/O %llx", inti
->type
);
388 vcpu
->stat
.deliver_io_int
++;
389 trace_kvm_s390_deliver_interrupt(vcpu
->vcpu_id
, inti
->type
,
391 rc
= put_guest_u16(vcpu
, __LC_SUBCHANNEL_ID
,
392 inti
->io
.subchannel_id
);
396 rc
= put_guest_u16(vcpu
, __LC_SUBCHANNEL_NR
,
397 inti
->io
.subchannel_nr
);
401 rc
= put_guest_u32(vcpu
, __LC_IO_INT_PARM
,
402 inti
->io
.io_int_parm
);
406 rc
= put_guest_u32(vcpu
, __LC_IO_INT_WORD
,
407 inti
->io
.io_int_word
);
411 rc
= copy_to_guest(vcpu
, __LC_IO_OLD_PSW
,
412 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
416 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
417 __LC_IO_NEW_PSW
, sizeof(psw_t
));
426 printk("kvm: The guest lowcore is not mapped during interrupt "
427 "delivery, killing userspace\n");
432 static int __try_deliver_ckc_interrupt(struct kvm_vcpu
*vcpu
)
434 int rc
, exception
= 0;
436 if (psw_extint_disabled(vcpu
))
438 if (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x800ul
))
440 rc
= put_guest_u16(vcpu
, __LC_EXT_INT_CODE
, 0x1004);
443 rc
= copy_to_guest(vcpu
, __LC_EXT_OLD_PSW
,
444 &vcpu
->arch
.sie_block
->gpsw
, sizeof(psw_t
));
447 rc
= copy_from_guest(vcpu
, &vcpu
->arch
.sie_block
->gpsw
,
448 __LC_EXT_NEW_PSW
, sizeof(psw_t
));
452 printk("kvm: The guest lowcore is not mapped during interrupt "
453 "delivery, killing userspace\n");
459 static int kvm_cpu_has_interrupt(struct kvm_vcpu
*vcpu
)
461 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
462 struct kvm_s390_float_interrupt
*fi
= vcpu
->arch
.local_int
.float_int
;
463 struct kvm_s390_interrupt_info
*inti
;
466 if (atomic_read(&li
->active
)) {
467 spin_lock_bh(&li
->lock
);
468 list_for_each_entry(inti
, &li
->list
, list
)
469 if (__interrupt_is_deliverable(vcpu
, inti
)) {
473 spin_unlock_bh(&li
->lock
);
476 if ((!rc
) && atomic_read(&fi
->active
)) {
477 spin_lock(&fi
->lock
);
478 list_for_each_entry(inti
, &fi
->list
, list
)
479 if (__interrupt_is_deliverable(vcpu
, inti
)) {
483 spin_unlock(&fi
->lock
);
486 if ((!rc
) && (vcpu
->arch
.sie_block
->ckc
<
487 get_tod_clock() + vcpu
->arch
.sie_block
->epoch
)) {
488 if ((!psw_extint_disabled(vcpu
)) &&
489 (vcpu
->arch
.sie_block
->gcr
[0] & 0x800ul
))
496 int kvm_cpu_has_pending_timer(struct kvm_vcpu
*vcpu
)
501 int kvm_s390_handle_wait(struct kvm_vcpu
*vcpu
)
504 DECLARE_WAITQUEUE(wait
, current
);
506 vcpu
->stat
.exit_wait_state
++;
507 if (kvm_cpu_has_interrupt(vcpu
))
510 __set_cpu_idle(vcpu
);
511 spin_lock_bh(&vcpu
->arch
.local_int
.lock
);
512 vcpu
->arch
.local_int
.timer_due
= 0;
513 spin_unlock_bh(&vcpu
->arch
.local_int
.lock
);
515 if (psw_interrupts_disabled(vcpu
)) {
516 VCPU_EVENT(vcpu
, 3, "%s", "disabled wait");
517 __unset_cpu_idle(vcpu
);
518 return -EOPNOTSUPP
; /* disabled wait */
521 if (psw_extint_disabled(vcpu
) ||
522 (!(vcpu
->arch
.sie_block
->gcr
[0] & 0x800ul
))) {
523 VCPU_EVENT(vcpu
, 3, "%s", "enabled wait w/o timer");
527 now
= get_tod_clock() + vcpu
->arch
.sie_block
->epoch
;
528 if (vcpu
->arch
.sie_block
->ckc
< now
) {
529 __unset_cpu_idle(vcpu
);
533 sltime
= tod_to_ns(vcpu
->arch
.sie_block
->ckc
- now
);
535 hrtimer_start(&vcpu
->arch
.ckc_timer
, ktime_set (0, sltime
) , HRTIMER_MODE_REL
);
536 VCPU_EVENT(vcpu
, 5, "enabled wait via clock comparator: %llx ns", sltime
);
538 spin_lock(&vcpu
->arch
.local_int
.float_int
->lock
);
539 spin_lock_bh(&vcpu
->arch
.local_int
.lock
);
540 add_wait_queue(&vcpu
->arch
.local_int
.wq
, &wait
);
541 while (list_empty(&vcpu
->arch
.local_int
.list
) &&
542 list_empty(&vcpu
->arch
.local_int
.float_int
->list
) &&
543 (!vcpu
->arch
.local_int
.timer_due
) &&
544 !signal_pending(current
)) {
545 set_current_state(TASK_INTERRUPTIBLE
);
546 spin_unlock_bh(&vcpu
->arch
.local_int
.lock
);
547 spin_unlock(&vcpu
->arch
.local_int
.float_int
->lock
);
549 spin_lock(&vcpu
->arch
.local_int
.float_int
->lock
);
550 spin_lock_bh(&vcpu
->arch
.local_int
.lock
);
552 __unset_cpu_idle(vcpu
);
553 __set_current_state(TASK_RUNNING
);
554 remove_wait_queue(&vcpu
->arch
.local_int
.wq
, &wait
);
555 spin_unlock_bh(&vcpu
->arch
.local_int
.lock
);
556 spin_unlock(&vcpu
->arch
.local_int
.float_int
->lock
);
557 hrtimer_try_to_cancel(&vcpu
->arch
.ckc_timer
);
561 void kvm_s390_tasklet(unsigned long parm
)
563 struct kvm_vcpu
*vcpu
= (struct kvm_vcpu
*) parm
;
565 spin_lock(&vcpu
->arch
.local_int
.lock
);
566 vcpu
->arch
.local_int
.timer_due
= 1;
567 if (waitqueue_active(&vcpu
->arch
.local_int
.wq
))
568 wake_up_interruptible(&vcpu
->arch
.local_int
.wq
);
569 spin_unlock(&vcpu
->arch
.local_int
.lock
);
573 * low level hrtimer wake routine. Because this runs in hardirq context
574 * we schedule a tasklet to do the real work.
576 enum hrtimer_restart
kvm_s390_idle_wakeup(struct hrtimer
*timer
)
578 struct kvm_vcpu
*vcpu
;
580 vcpu
= container_of(timer
, struct kvm_vcpu
, arch
.ckc_timer
);
581 tasklet_schedule(&vcpu
->arch
.tasklet
);
583 return HRTIMER_NORESTART
;
586 void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu
*vcpu
)
588 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
589 struct kvm_s390_float_interrupt
*fi
= vcpu
->arch
.local_int
.float_int
;
590 struct kvm_s390_interrupt_info
*n
, *inti
= NULL
;
593 __reset_intercept_indicators(vcpu
);
594 if (atomic_read(&li
->active
)) {
597 spin_lock_bh(&li
->lock
);
598 list_for_each_entry_safe(inti
, n
, &li
->list
, list
) {
599 if (__interrupt_is_deliverable(vcpu
, inti
)) {
600 list_del(&inti
->list
);
604 __set_intercept_indicator(vcpu
, inti
);
606 if (list_empty(&li
->list
))
607 atomic_set(&li
->active
, 0);
608 spin_unlock_bh(&li
->lock
);
610 __do_deliver_interrupt(vcpu
, inti
);
616 if ((vcpu
->arch
.sie_block
->ckc
<
617 get_tod_clock() + vcpu
->arch
.sie_block
->epoch
))
618 __try_deliver_ckc_interrupt(vcpu
);
620 if (atomic_read(&fi
->active
)) {
623 spin_lock(&fi
->lock
);
624 list_for_each_entry_safe(inti
, n
, &fi
->list
, list
) {
625 if (__interrupt_is_deliverable(vcpu
, inti
)) {
626 list_del(&inti
->list
);
630 __set_intercept_indicator(vcpu
, inti
);
632 if (list_empty(&fi
->list
))
633 atomic_set(&fi
->active
, 0);
634 spin_unlock(&fi
->lock
);
636 __do_deliver_interrupt(vcpu
, inti
);
643 void kvm_s390_deliver_pending_machine_checks(struct kvm_vcpu
*vcpu
)
645 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
646 struct kvm_s390_float_interrupt
*fi
= vcpu
->arch
.local_int
.float_int
;
647 struct kvm_s390_interrupt_info
*n
, *inti
= NULL
;
650 __reset_intercept_indicators(vcpu
);
651 if (atomic_read(&li
->active
)) {
654 spin_lock_bh(&li
->lock
);
655 list_for_each_entry_safe(inti
, n
, &li
->list
, list
) {
656 if ((inti
->type
== KVM_S390_MCHK
) &&
657 __interrupt_is_deliverable(vcpu
, inti
)) {
658 list_del(&inti
->list
);
662 __set_intercept_indicator(vcpu
, inti
);
664 if (list_empty(&li
->list
))
665 atomic_set(&li
->active
, 0);
666 spin_unlock_bh(&li
->lock
);
668 __do_deliver_interrupt(vcpu
, inti
);
674 if (atomic_read(&fi
->active
)) {
677 spin_lock(&fi
->lock
);
678 list_for_each_entry_safe(inti
, n
, &fi
->list
, list
) {
679 if ((inti
->type
== KVM_S390_MCHK
) &&
680 __interrupt_is_deliverable(vcpu
, inti
)) {
681 list_del(&inti
->list
);
685 __set_intercept_indicator(vcpu
, inti
);
687 if (list_empty(&fi
->list
))
688 atomic_set(&fi
->active
, 0);
689 spin_unlock(&fi
->lock
);
691 __do_deliver_interrupt(vcpu
, inti
);
698 int kvm_s390_inject_program_int(struct kvm_vcpu
*vcpu
, u16 code
)
700 struct kvm_s390_local_interrupt
*li
= &vcpu
->arch
.local_int
;
701 struct kvm_s390_interrupt_info
*inti
;
703 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
707 inti
->type
= KVM_S390_PROGRAM_INT
;
708 inti
->pgm
.code
= code
;
710 VCPU_EVENT(vcpu
, 3, "inject: program check %d (from kernel)", code
);
711 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, inti
->type
, code
, 0, 1);
712 spin_lock_bh(&li
->lock
);
713 list_add(&inti
->list
, &li
->list
);
714 atomic_set(&li
->active
, 1);
715 BUG_ON(waitqueue_active(&li
->wq
));
716 spin_unlock_bh(&li
->lock
);
720 struct kvm_s390_interrupt_info
*kvm_s390_get_io_int(struct kvm
*kvm
,
723 struct kvm_s390_float_interrupt
*fi
;
724 struct kvm_s390_interrupt_info
*inti
, *iter
;
726 if ((!schid
&& !cr6
) || (schid
&& cr6
))
728 mutex_lock(&kvm
->lock
);
729 fi
= &kvm
->arch
.float_int
;
730 spin_lock(&fi
->lock
);
732 list_for_each_entry(iter
, &fi
->list
, list
) {
733 if (!is_ioint(iter
->type
))
736 ((cr6
& int_word_to_isc_bits(iter
->io
.io_int_word
)) == 0))
739 if (((schid
& 0x00000000ffff0000) >> 16) !=
740 iter
->io
.subchannel_id
)
742 if ((schid
& 0x000000000000ffff) !=
743 iter
->io
.subchannel_nr
)
750 list_del_init(&inti
->list
);
751 if (list_empty(&fi
->list
))
752 atomic_set(&fi
->active
, 0);
753 spin_unlock(&fi
->lock
);
754 mutex_unlock(&kvm
->lock
);
758 int kvm_s390_inject_vm(struct kvm
*kvm
,
759 struct kvm_s390_interrupt
*s390int
)
761 struct kvm_s390_local_interrupt
*li
;
762 struct kvm_s390_float_interrupt
*fi
;
763 struct kvm_s390_interrupt_info
*inti
, *iter
;
766 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
770 switch (s390int
->type
) {
771 case KVM_S390_INT_VIRTIO
:
772 VM_EVENT(kvm
, 5, "inject: virtio parm:%x,parm64:%llx",
773 s390int
->parm
, s390int
->parm64
);
774 inti
->type
= s390int
->type
;
775 inti
->ext
.ext_params
= s390int
->parm
;
776 inti
->ext
.ext_params2
= s390int
->parm64
;
778 case KVM_S390_INT_SERVICE
:
779 VM_EVENT(kvm
, 5, "inject: sclp parm:%x", s390int
->parm
);
780 inti
->type
= s390int
->type
;
781 inti
->ext
.ext_params
= s390int
->parm
;
783 case KVM_S390_PROGRAM_INT
:
784 case KVM_S390_SIGP_STOP
:
785 case KVM_S390_INT_EXTERNAL_CALL
:
786 case KVM_S390_INT_EMERGENCY
:
790 VM_EVENT(kvm
, 5, "inject: machine check parm64:%llx",
792 inti
->type
= s390int
->type
;
793 inti
->mchk
.cr14
= s390int
->parm
; /* upper bits are not used */
794 inti
->mchk
.mcic
= s390int
->parm64
;
796 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
797 if (s390int
->type
& IOINT_AI_MASK
)
798 VM_EVENT(kvm
, 5, "%s", "inject: I/O (AI)");
800 VM_EVENT(kvm
, 5, "inject: I/O css %x ss %x schid %04x",
801 s390int
->type
& IOINT_CSSID_MASK
,
802 s390int
->type
& IOINT_SSID_MASK
,
803 s390int
->type
& IOINT_SCHID_MASK
);
804 inti
->type
= s390int
->type
;
805 inti
->io
.subchannel_id
= s390int
->parm
>> 16;
806 inti
->io
.subchannel_nr
= s390int
->parm
& 0x0000ffffu
;
807 inti
->io
.io_int_parm
= s390int
->parm64
>> 32;
808 inti
->io
.io_int_word
= s390int
->parm64
& 0x00000000ffffffffull
;
814 trace_kvm_s390_inject_vm(s390int
->type
, s390int
->parm
, s390int
->parm64
,
817 mutex_lock(&kvm
->lock
);
818 fi
= &kvm
->arch
.float_int
;
819 spin_lock(&fi
->lock
);
820 if (!is_ioint(inti
->type
))
821 list_add_tail(&inti
->list
, &fi
->list
);
823 u64 isc_bits
= int_word_to_isc_bits(inti
->io
.io_int_word
);
825 /* Keep I/O interrupts sorted in isc order. */
826 list_for_each_entry(iter
, &fi
->list
, list
) {
827 if (!is_ioint(iter
->type
))
829 if (int_word_to_isc_bits(iter
->io
.io_int_word
)
834 list_add_tail(&inti
->list
, &iter
->list
);
836 atomic_set(&fi
->active
, 1);
837 sigcpu
= find_first_bit(fi
->idle_mask
, KVM_MAX_VCPUS
);
838 if (sigcpu
== KVM_MAX_VCPUS
) {
840 sigcpu
= fi
->next_rr_cpu
++;
841 if (sigcpu
== KVM_MAX_VCPUS
)
842 sigcpu
= fi
->next_rr_cpu
= 0;
843 } while (fi
->local_int
[sigcpu
] == NULL
);
845 li
= fi
->local_int
[sigcpu
];
846 spin_lock_bh(&li
->lock
);
847 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
848 if (waitqueue_active(&li
->wq
))
849 wake_up_interruptible(&li
->wq
);
850 spin_unlock_bh(&li
->lock
);
851 spin_unlock(&fi
->lock
);
852 mutex_unlock(&kvm
->lock
);
856 int kvm_s390_inject_vcpu(struct kvm_vcpu
*vcpu
,
857 struct kvm_s390_interrupt
*s390int
)
859 struct kvm_s390_local_interrupt
*li
;
860 struct kvm_s390_interrupt_info
*inti
;
862 inti
= kzalloc(sizeof(*inti
), GFP_KERNEL
);
866 switch (s390int
->type
) {
867 case KVM_S390_PROGRAM_INT
:
868 if (s390int
->parm
& 0xffff0000) {
872 inti
->type
= s390int
->type
;
873 inti
->pgm
.code
= s390int
->parm
;
874 VCPU_EVENT(vcpu
, 3, "inject: program check %d (from user)",
877 case KVM_S390_SIGP_SET_PREFIX
:
878 inti
->prefix
.address
= s390int
->parm
;
879 inti
->type
= s390int
->type
;
880 VCPU_EVENT(vcpu
, 3, "inject: set prefix to %x (from user)",
883 case KVM_S390_SIGP_STOP
:
884 case KVM_S390_RESTART
:
885 VCPU_EVENT(vcpu
, 3, "inject: type %x", s390int
->type
);
886 inti
->type
= s390int
->type
;
888 case KVM_S390_INT_EXTERNAL_CALL
:
889 if (s390int
->parm
& 0xffff0000) {
893 VCPU_EVENT(vcpu
, 3, "inject: external call source-cpu:%u",
895 inti
->type
= s390int
->type
;
896 inti
->extcall
.code
= s390int
->parm
;
898 case KVM_S390_INT_EMERGENCY
:
899 if (s390int
->parm
& 0xffff0000) {
903 VCPU_EVENT(vcpu
, 3, "inject: emergency %u\n", s390int
->parm
);
904 inti
->type
= s390int
->type
;
905 inti
->emerg
.code
= s390int
->parm
;
908 VCPU_EVENT(vcpu
, 5, "inject: machine check parm64:%llx",
910 inti
->type
= s390int
->type
;
911 inti
->mchk
.mcic
= s390int
->parm64
;
913 case KVM_S390_INT_VIRTIO
:
914 case KVM_S390_INT_SERVICE
:
915 case KVM_S390_INT_IO_MIN
...KVM_S390_INT_IO_MAX
:
920 trace_kvm_s390_inject_vcpu(vcpu
->vcpu_id
, s390int
->type
, s390int
->parm
,
923 mutex_lock(&vcpu
->kvm
->lock
);
924 li
= &vcpu
->arch
.local_int
;
925 spin_lock_bh(&li
->lock
);
926 if (inti
->type
== KVM_S390_PROGRAM_INT
)
927 list_add(&inti
->list
, &li
->list
);
929 list_add_tail(&inti
->list
, &li
->list
);
930 atomic_set(&li
->active
, 1);
931 if (inti
->type
== KVM_S390_SIGP_STOP
)
932 li
->action_bits
|= ACTION_STOP_ON_STOP
;
933 atomic_set_mask(CPUSTAT_EXT_INT
, li
->cpuflags
);
934 if (waitqueue_active(&li
->wq
))
935 wake_up_interruptible(&vcpu
->arch
.local_int
.wq
);
936 spin_unlock_bh(&li
->lock
);
937 mutex_unlock(&vcpu
->kvm
->lock
);