irq.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * irq.c: API for in kernel interrupt controller
  4. * Copyright (c) 2007, Intel Corporation.
  5. * Copyright 2009 Red Hat, Inc. and/or its affiliates.
  6. *
  7. * Authors:
  8. * Yaozu (Eddie) Dong <[email protected]>
  9. */
  10. #include <linux/export.h>
  11. #include <linux/kvm_host.h>
  12. #include "irq.h"
  13. #include "i8254.h"
  14. #include "x86.h"
  15. #include "xen.h"
  16. /*
  17. * check if there are pending timer events
  18. * to be processed.
  19. */
  20. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  21. {
  22. int r = 0;
  23. if (lapic_in_kernel(vcpu))
  24. r = apic_has_pending_timer(vcpu);
  25. if (kvm_xen_timer_enabled(vcpu))
  26. r += kvm_xen_has_pending_timer(vcpu);
  27. return r;
  28. }
  29. EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
  30. /*
  31. * check if there is a pending userspace external interrupt
  32. */
  33. static int pending_userspace_extint(struct kvm_vcpu *v)
  34. {
  35. return v->arch.pending_external_vector != -1;
  36. }
  37. /*
  38. * check if there is pending interrupt from
  39. * non-APIC source without intack.
  40. */
  41. int kvm_cpu_has_extint(struct kvm_vcpu *v)
  42. {
  43. /*
  44. * FIXME: interrupt.injected represents an interrupt whose
  45. * side-effects have already been applied (e.g. bit from IRR
  46. * already moved to ISR). Therefore, it is incorrect to rely
  47. * on interrupt.injected to know if there is a pending
  48. * interrupt in the user-mode LAPIC.
  49. * This leads to nVMX/nSVM not be able to distinguish
  50. * if it should exit from L2 to L1 on EXTERNAL_INTERRUPT on
  51. * pending interrupt or should re-inject an injected
  52. * interrupt.
  53. */
  54. if (!lapic_in_kernel(v))
  55. return v->arch.interrupt.injected;
  56. if (kvm_xen_has_interrupt(v))
  57. return 1;
  58. if (!kvm_apic_accept_pic_intr(v))
  59. return 0;
  60. if (irqchip_split(v->kvm))
  61. return pending_userspace_extint(v);
  62. else
  63. return v->kvm->arch.vpic->output;
  64. }
  65. /*
  66. * check if there is injectable interrupt:
  67. * when virtual interrupt delivery enabled,
  68. * interrupt from apic will handled by hardware,
  69. * we don't need to check it here.
  70. */
  71. int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v)
  72. {
  73. if (kvm_cpu_has_extint(v))
  74. return 1;
  75. if (!is_guest_mode(v) && kvm_vcpu_apicv_active(v))
  76. return 0;
  77. return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
  78. }
  79. EXPORT_SYMBOL_GPL(kvm_cpu_has_injectable_intr);
  80. /*
  81. * check if there is pending interrupt without
  82. * intack.
  83. */
  84. int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
  85. {
  86. if (kvm_cpu_has_extint(v))
  87. return 1;
  88. return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
  89. }
  90. EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
  91. /*
  92. * Read pending interrupt(from non-APIC source)
  93. * vector and intack.
  94. */
  95. static int kvm_cpu_get_extint(struct kvm_vcpu *v)
  96. {
  97. if (!kvm_cpu_has_extint(v)) {
  98. WARN_ON(!lapic_in_kernel(v));
  99. return -1;
  100. }
  101. if (!lapic_in_kernel(v))
  102. return v->arch.interrupt.nr;
  103. if (kvm_xen_has_interrupt(v))
  104. return v->kvm->arch.xen.upcall_vector;
  105. if (irqchip_split(v->kvm)) {
  106. int vector = v->arch.pending_external_vector;
  107. v->arch.pending_external_vector = -1;
  108. return vector;
  109. } else
  110. return kvm_pic_read_irq(v->kvm); /* PIC */
  111. }
  112. /*
  113. * Read pending interrupt vector and intack.
  114. */
  115. int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
  116. {
  117. int vector = kvm_cpu_get_extint(v);
  118. if (vector != -1)
  119. return vector; /* PIC */
  120. return kvm_get_apic_interrupt(v); /* APIC */
  121. }
  122. EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
  123. void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
  124. {
  125. if (lapic_in_kernel(vcpu))
  126. kvm_inject_apic_timer_irqs(vcpu);
  127. if (kvm_xen_timer_enabled(vcpu))
  128. kvm_xen_inject_timer_irqs(vcpu);
  129. }
  130. EXPORT_SYMBOL_GPL(kvm_inject_pending_timer_irqs);
  131. void __kvm_migrate_timers(struct kvm_vcpu *vcpu)
  132. {
  133. __kvm_migrate_apic_timer(vcpu);
  134. __kvm_migrate_pit_timer(vcpu);
  135. static_call_cond(kvm_x86_migrate_timers)(vcpu);
  136. }
  137. bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
  138. {
  139. bool resample = args->flags & KVM_IRQFD_FLAG_RESAMPLE;
  140. return resample ? irqchip_kernel(kvm) : irqchip_in_kernel(kvm);
  141. }