2 * interrupt.c - 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 <asm/lowcore.h>
14 #include <asm/uaccess.h>
15 #include <linux/hrtimer.h>
16 #include <linux/interrupt.h>
17 #include <linux/kvm_host.h>
18 #include <linux/signal.h>
22 static int psw_extint_disabled(struct kvm_vcpu *vcpu)
24 return !(vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT);
27 static int psw_interrupts_disabled(struct kvm_vcpu *vcpu)
29 if ((vcpu->arch.sie_block->gpsw.mask & PSW_MASK_PER) ||
30 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_IO) ||
31 (vcpu->arch.sie_block->gpsw.mask & PSW_MASK_EXT))
36 static int __interrupt_is_deliverable(struct kvm_vcpu *vcpu,
37 struct kvm_s390_interrupt_info *inti)
40 case KVM_S390_INT_EMERGENCY:
41 if (psw_extint_disabled(vcpu))
43 if (vcpu->arch.sie_block->gcr[0] & 0x4000ul)
46 case KVM_S390_INT_SERVICE:
47 if (psw_extint_disabled(vcpu))
49 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
52 case KVM_S390_INT_VIRTIO:
53 if (psw_extint_disabled(vcpu))
55 if (vcpu->arch.sie_block->gcr[0] & 0x200ul)
58 case KVM_S390_PROGRAM_INT:
59 case KVM_S390_SIGP_STOP:
60 case KVM_S390_SIGP_SET_PREFIX:
61 case KVM_S390_RESTART:
69 static void __set_cpu_idle(struct kvm_vcpu *vcpu)
71 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
72 atomic_set_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
73 set_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
76 static void __unset_cpu_idle(struct kvm_vcpu *vcpu)
78 BUG_ON(vcpu->vcpu_id > KVM_MAX_VCPUS - 1);
79 atomic_clear_mask(CPUSTAT_WAIT, &vcpu->arch.sie_block->cpuflags);
80 clear_bit(vcpu->vcpu_id, vcpu->arch.local_int.float_int->idle_mask);
83 static void __reset_intercept_indicators(struct kvm_vcpu *vcpu)
85 atomic_clear_mask(CPUSTAT_ECALL_PEND |
86 CPUSTAT_IO_INT | CPUSTAT_EXT_INT | CPUSTAT_STOP_INT,
87 &vcpu->arch.sie_block->cpuflags);
88 vcpu->arch.sie_block->lctl = 0x0000;
91 static void __set_cpuflag(struct kvm_vcpu *vcpu, u32 flag)
93 atomic_set_mask(flag, &vcpu->arch.sie_block->cpuflags);
96 static void __set_intercept_indicator(struct kvm_vcpu *vcpu,
97 struct kvm_s390_interrupt_info *inti)
100 case KVM_S390_INT_EMERGENCY:
101 case KVM_S390_INT_SERVICE:
102 case KVM_S390_INT_VIRTIO:
103 if (psw_extint_disabled(vcpu))
104 __set_cpuflag(vcpu, CPUSTAT_EXT_INT);
106 vcpu->arch.sie_block->lctl |= LCTL_CR0;
108 case KVM_S390_SIGP_STOP:
109 __set_cpuflag(vcpu, CPUSTAT_STOP_INT);
116 static void __do_deliver_interrupt(struct kvm_vcpu *vcpu,
117 struct kvm_s390_interrupt_info *inti)
119 const unsigned short table[] = { 2, 4, 4, 6 };
120 int rc, exception = 0;
122 switch (inti->type) {
123 case KVM_S390_INT_EMERGENCY:
124 VCPU_EVENT(vcpu, 4, "%s", "interrupt: sigp emerg");
125 vcpu->stat.deliver_emergency_signal++;
126 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1201);
130 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
131 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
135 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
136 __LC_EXT_NEW_PSW, sizeof(psw_t));
141 case KVM_S390_INT_SERVICE:
142 VCPU_EVENT(vcpu, 4, "interrupt: sclp parm:%x",
143 inti->ext.ext_params);
144 vcpu->stat.deliver_service_signal++;
145 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2401);
149 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
150 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
154 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
155 __LC_EXT_NEW_PSW, sizeof(psw_t));
159 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
164 case KVM_S390_INT_VIRTIO:
165 VCPU_EVENT(vcpu, 4, "interrupt: virtio parm:%x,parm64:%llx",
166 inti->ext.ext_params, inti->ext.ext_params2);
167 vcpu->stat.deliver_virtio_interrupt++;
168 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x2603);
172 rc = put_guest_u16(vcpu, __LC_CPU_ADDRESS, 0x0d00);
176 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
177 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
181 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
182 __LC_EXT_NEW_PSW, sizeof(psw_t));
186 rc = put_guest_u32(vcpu, __LC_EXT_PARAMS, inti->ext.ext_params);
190 rc = put_guest_u64(vcpu, __LC_PFAULT_INTPARM,
191 inti->ext.ext_params2);
196 case KVM_S390_SIGP_STOP:
197 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu stop");
198 vcpu->stat.deliver_stop_signal++;
199 __set_intercept_indicator(vcpu, inti);
202 case KVM_S390_SIGP_SET_PREFIX:
203 VCPU_EVENT(vcpu, 4, "interrupt: set prefix to %x",
204 inti->prefix.address);
205 vcpu->stat.deliver_prefix_signal++;
206 vcpu->arch.sie_block->prefix = inti->prefix.address;
207 vcpu->arch.sie_block->ihcpu = 0xffff;
210 case KVM_S390_RESTART:
211 VCPU_EVENT(vcpu, 4, "%s", "interrupt: cpu restart");
212 vcpu->stat.deliver_restart_signal++;
213 rc = copy_to_guest(vcpu, offsetof(struct _lowcore,
214 restart_old_psw), &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
218 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
219 offsetof(struct _lowcore, restart_psw), sizeof(psw_t));
224 case KVM_S390_PROGRAM_INT:
225 VCPU_EVENT(vcpu, 4, "interrupt: pgm check code:%x, ilc:%x",
227 table[vcpu->arch.sie_block->ipa >> 14]);
228 vcpu->stat.deliver_program_int++;
229 rc = put_guest_u16(vcpu, __LC_PGM_INT_CODE, inti->pgm.code);
233 rc = put_guest_u16(vcpu, __LC_PGM_ILC,
234 table[vcpu->arch.sie_block->ipa >> 14]);
238 rc = copy_to_guest(vcpu, __LC_PGM_OLD_PSW,
239 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
243 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
244 __LC_PGM_NEW_PSW, sizeof(psw_t));
253 printk("kvm: The guest lowcore is not mapped during interrupt "
254 "delivery, killing userspace\n");
259 static int __try_deliver_ckc_interrupt(struct kvm_vcpu *vcpu)
261 int rc, exception = 0;
263 if (psw_extint_disabled(vcpu))
265 if (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))
267 rc = put_guest_u16(vcpu, __LC_EXT_INT_CODE, 0x1004);
270 rc = copy_to_guest(vcpu, __LC_EXT_OLD_PSW,
271 &vcpu->arch.sie_block->gpsw, sizeof(psw_t));
274 rc = copy_from_guest(vcpu, &vcpu->arch.sie_block->gpsw,
275 __LC_EXT_NEW_PSW, sizeof(psw_t));
279 printk("kvm: The guest lowcore is not mapped during interrupt "
280 "delivery, killing userspace\n");
286 int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu)
288 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
289 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
290 struct kvm_s390_interrupt_info *inti;
293 if (atomic_read(&li->active)) {
294 spin_lock_bh(&li->lock);
295 list_for_each_entry(inti, &li->list, list)
296 if (__interrupt_is_deliverable(vcpu, inti)) {
300 spin_unlock_bh(&li->lock);
303 if ((!rc) && atomic_read(&fi->active)) {
304 spin_lock(&fi->lock);
305 list_for_each_entry(inti, &fi->list, list)
306 if (__interrupt_is_deliverable(vcpu, inti)) {
310 spin_unlock(&fi->lock);
313 if ((!rc) && (vcpu->arch.sie_block->ckc <
314 get_clock() + vcpu->arch.sie_block->epoch)) {
315 if ((!psw_extint_disabled(vcpu)) &&
316 (vcpu->arch.sie_block->gcr[0] & 0x800ul))
323 int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
325 /* do real check here */
329 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
334 int kvm_s390_handle_wait(struct kvm_vcpu *vcpu)
337 DECLARE_WAITQUEUE(wait, current);
339 vcpu->stat.exit_wait_state++;
340 if (kvm_cpu_has_interrupt(vcpu))
343 __set_cpu_idle(vcpu);
344 spin_lock_bh(&vcpu->arch.local_int.lock);
345 vcpu->arch.local_int.timer_due = 0;
346 spin_unlock_bh(&vcpu->arch.local_int.lock);
348 if (psw_interrupts_disabled(vcpu)) {
349 VCPU_EVENT(vcpu, 3, "%s", "disabled wait");
350 __unset_cpu_idle(vcpu);
351 return -ENOTSUPP; /* disabled wait */
354 if (psw_extint_disabled(vcpu) ||
355 (!(vcpu->arch.sie_block->gcr[0] & 0x800ul))) {
356 VCPU_EVENT(vcpu, 3, "%s", "enabled wait w/o timer");
360 now = get_clock() + vcpu->arch.sie_block->epoch;
361 if (vcpu->arch.sie_block->ckc < now) {
362 __unset_cpu_idle(vcpu);
366 sltime = ((vcpu->arch.sie_block->ckc - now)*125)>>9;
368 hrtimer_start(&vcpu->arch.ckc_timer, ktime_set (0, sltime) , HRTIMER_MODE_REL);
369 VCPU_EVENT(vcpu, 5, "enabled wait via clock comparator: %llx ns", sltime);
371 spin_lock(&vcpu->arch.local_int.float_int->lock);
372 spin_lock_bh(&vcpu->arch.local_int.lock);
373 add_wait_queue(&vcpu->arch.local_int.wq, &wait);
374 while (list_empty(&vcpu->arch.local_int.list) &&
375 list_empty(&vcpu->arch.local_int.float_int->list) &&
376 (!vcpu->arch.local_int.timer_due) &&
377 !signal_pending(current)) {
378 set_current_state(TASK_INTERRUPTIBLE);
379 spin_unlock_bh(&vcpu->arch.local_int.lock);
380 spin_unlock(&vcpu->arch.local_int.float_int->lock);
384 spin_lock(&vcpu->arch.local_int.float_int->lock);
385 spin_lock_bh(&vcpu->arch.local_int.lock);
387 __unset_cpu_idle(vcpu);
388 __set_current_state(TASK_RUNNING);
389 remove_wait_queue(&vcpu->wq, &wait);
390 spin_unlock_bh(&vcpu->arch.local_int.lock);
391 spin_unlock(&vcpu->arch.local_int.float_int->lock);
392 hrtimer_try_to_cancel(&vcpu->arch.ckc_timer);
396 void kvm_s390_tasklet(unsigned long parm)
398 struct kvm_vcpu *vcpu = (struct kvm_vcpu *) parm;
400 spin_lock(&vcpu->arch.local_int.lock);
401 vcpu->arch.local_int.timer_due = 1;
402 if (waitqueue_active(&vcpu->arch.local_int.wq))
403 wake_up_interruptible(&vcpu->arch.local_int.wq);
404 spin_unlock(&vcpu->arch.local_int.lock);
408 * low level hrtimer wake routine. Because this runs in hardirq context
409 * we schedule a tasklet to do the real work.
411 enum hrtimer_restart kvm_s390_idle_wakeup(struct hrtimer *timer)
413 struct kvm_vcpu *vcpu;
415 vcpu = container_of(timer, struct kvm_vcpu, arch.ckc_timer);
416 tasklet_schedule(&vcpu->arch.tasklet);
418 return HRTIMER_NORESTART;
421 void kvm_s390_deliver_pending_interrupts(struct kvm_vcpu *vcpu)
423 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
424 struct kvm_s390_float_interrupt *fi = vcpu->arch.local_int.float_int;
425 struct kvm_s390_interrupt_info *n, *inti = NULL;
428 __reset_intercept_indicators(vcpu);
429 if (atomic_read(&li->active)) {
432 spin_lock_bh(&li->lock);
433 list_for_each_entry_safe(inti, n, &li->list, list) {
434 if (__interrupt_is_deliverable(vcpu, inti)) {
435 list_del(&inti->list);
439 __set_intercept_indicator(vcpu, inti);
441 if (list_empty(&li->list))
442 atomic_set(&li->active, 0);
443 spin_unlock_bh(&li->lock);
445 __do_deliver_interrupt(vcpu, inti);
451 if ((vcpu->arch.sie_block->ckc <
452 get_clock() + vcpu->arch.sie_block->epoch))
453 __try_deliver_ckc_interrupt(vcpu);
455 if (atomic_read(&fi->active)) {
458 spin_lock(&fi->lock);
459 list_for_each_entry_safe(inti, n, &fi->list, list) {
460 if (__interrupt_is_deliverable(vcpu, inti)) {
461 list_del(&inti->list);
465 __set_intercept_indicator(vcpu, inti);
467 if (list_empty(&fi->list))
468 atomic_set(&fi->active, 0);
469 spin_unlock(&fi->lock);
471 __do_deliver_interrupt(vcpu, inti);
478 int kvm_s390_inject_program_int(struct kvm_vcpu *vcpu, u16 code)
480 struct kvm_s390_local_interrupt *li = &vcpu->arch.local_int;
481 struct kvm_s390_interrupt_info *inti;
483 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
487 inti->type = KVM_S390_PROGRAM_INT;;
488 inti->pgm.code = code;
490 VCPU_EVENT(vcpu, 3, "inject: program check %d (from kernel)", code);
491 spin_lock_bh(&li->lock);
492 list_add(&inti->list, &li->list);
493 atomic_set(&li->active, 1);
494 BUG_ON(waitqueue_active(&li->wq));
495 spin_unlock_bh(&li->lock);
499 int kvm_s390_inject_vm(struct kvm *kvm,
500 struct kvm_s390_interrupt *s390int)
502 struct kvm_s390_local_interrupt *li;
503 struct kvm_s390_float_interrupt *fi;
504 struct kvm_s390_interrupt_info *inti;
507 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
511 switch (s390int->type) {
512 case KVM_S390_INT_VIRTIO:
513 VM_EVENT(kvm, 5, "inject: virtio parm:%x,parm64:%llx",
514 s390int->parm, s390int->parm64);
515 inti->type = s390int->type;
516 inti->ext.ext_params = s390int->parm;
517 inti->ext.ext_params2 = s390int->parm64;
519 case KVM_S390_INT_SERVICE:
520 VM_EVENT(kvm, 5, "inject: sclp parm:%x", s390int->parm);
521 inti->type = s390int->type;
522 inti->ext.ext_params = s390int->parm;
524 case KVM_S390_PROGRAM_INT:
525 case KVM_S390_SIGP_STOP:
526 case KVM_S390_INT_EMERGENCY:
532 mutex_lock(&kvm->lock);
533 fi = &kvm->arch.float_int;
534 spin_lock(&fi->lock);
535 list_add_tail(&inti->list, &fi->list);
536 atomic_set(&fi->active, 1);
537 sigcpu = find_first_bit(fi->idle_mask, KVM_MAX_VCPUS);
538 if (sigcpu == KVM_MAX_VCPUS) {
540 sigcpu = fi->next_rr_cpu++;
541 if (sigcpu == KVM_MAX_VCPUS)
542 sigcpu = fi->next_rr_cpu = 0;
543 } while (fi->local_int[sigcpu] == NULL);
545 li = fi->local_int[sigcpu];
546 spin_lock_bh(&li->lock);
547 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
548 if (waitqueue_active(&li->wq))
549 wake_up_interruptible(&li->wq);
550 spin_unlock_bh(&li->lock);
551 spin_unlock(&fi->lock);
552 mutex_unlock(&kvm->lock);
556 int kvm_s390_inject_vcpu(struct kvm_vcpu *vcpu,
557 struct kvm_s390_interrupt *s390int)
559 struct kvm_s390_local_interrupt *li;
560 struct kvm_s390_interrupt_info *inti;
562 inti = kzalloc(sizeof(*inti), GFP_KERNEL);
566 switch (s390int->type) {
567 case KVM_S390_PROGRAM_INT:
568 if (s390int->parm & 0xffff0000) {
572 inti->type = s390int->type;
573 inti->pgm.code = s390int->parm;
574 VCPU_EVENT(vcpu, 3, "inject: program check %d (from user)",
577 case KVM_S390_SIGP_SET_PREFIX:
578 inti->prefix.address = s390int->parm;
579 inti->type = s390int->type;
580 VCPU_EVENT(vcpu, 3, "inject: set prefix to %x (from user)",
583 case KVM_S390_SIGP_STOP:
584 case KVM_S390_RESTART:
585 case KVM_S390_INT_EMERGENCY:
586 VCPU_EVENT(vcpu, 3, "inject: type %x", s390int->type);
587 inti->type = s390int->type;
589 case KVM_S390_INT_VIRTIO:
590 case KVM_S390_INT_SERVICE:
596 mutex_lock(&vcpu->kvm->lock);
597 li = &vcpu->arch.local_int;
598 spin_lock_bh(&li->lock);
599 if (inti->type == KVM_S390_PROGRAM_INT)
600 list_add(&inti->list, &li->list);
602 list_add_tail(&inti->list, &li->list);
603 atomic_set(&li->active, 1);
604 if (inti->type == KVM_S390_SIGP_STOP)
605 li->action_bits |= ACTION_STOP_ON_STOP;
606 atomic_set_mask(CPUSTAT_EXT_INT, li->cpuflags);
607 if (waitqueue_active(&li->wq))
608 wake_up_interruptible(&vcpu->arch.local_int.wq);
609 spin_unlock_bh(&li->lock);
610 mutex_unlock(&vcpu->kvm->lock);