Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * irq.c: API for in kernel interrupt controller
0004  * Copyright (c) 2007, Intel Corporation.
0005  * Copyright 2009 Red Hat, Inc. and/or its affiliates.
0006  *
0007  * Authors:
0008  *   Yaozu (Eddie) Dong <Eddie.dong@intel.com>
0009  */
0010 
0011 #include <linux/export.h>
0012 #include <linux/kvm_host.h>
0013 
0014 #include "irq.h"
0015 #include "i8254.h"
0016 #include "x86.h"
0017 #include "xen.h"
0018 
0019 /*
0020  * check if there are pending timer events
0021  * to be processed.
0022  */
0023 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
0024 {
0025     int r = 0;
0026 
0027     if (lapic_in_kernel(vcpu))
0028         r = apic_has_pending_timer(vcpu);
0029     if (kvm_xen_timer_enabled(vcpu))
0030         r += kvm_xen_has_pending_timer(vcpu);
0031 
0032     return r;
0033 }
0034 EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
0035 
0036 /*
0037  * check if there is a pending userspace external interrupt
0038  */
0039 static int pending_userspace_extint(struct kvm_vcpu *v)
0040 {
0041     return v->arch.pending_external_vector != -1;
0042 }
0043 
0044 /*
0045  * check if there is pending interrupt from
0046  * non-APIC source without intack.
0047  */
0048 int kvm_cpu_has_extint(struct kvm_vcpu *v)
0049 {
0050     /*
0051      * FIXME: interrupt.injected represents an interrupt whose
0052      * side-effects have already been applied (e.g. bit from IRR
0053      * already moved to ISR). Therefore, it is incorrect to rely
0054      * on interrupt.injected to know if there is a pending
0055      * interrupt in the user-mode LAPIC.
0056      * This leads to nVMX/nSVM not be able to distinguish
0057      * if it should exit from L2 to L1 on EXTERNAL_INTERRUPT on
0058      * pending interrupt or should re-inject an injected
0059      * interrupt.
0060      */
0061     if (!lapic_in_kernel(v))
0062         return v->arch.interrupt.injected;
0063 
0064     if (kvm_xen_has_interrupt(v))
0065         return 1;
0066 
0067     if (!kvm_apic_accept_pic_intr(v))
0068         return 0;
0069 
0070     if (irqchip_split(v->kvm))
0071         return pending_userspace_extint(v);
0072     else
0073         return v->kvm->arch.vpic->output;
0074 }
0075 
0076 /*
0077  * check if there is injectable interrupt:
0078  * when virtual interrupt delivery enabled,
0079  * interrupt from apic will handled by hardware,
0080  * we don't need to check it here.
0081  */
0082 int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v)
0083 {
0084     if (kvm_cpu_has_extint(v))
0085         return 1;
0086 
0087     if (!is_guest_mode(v) && kvm_vcpu_apicv_active(v))
0088         return 0;
0089 
0090     return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
0091 }
0092 EXPORT_SYMBOL_GPL(kvm_cpu_has_injectable_intr);
0093 
0094 /*
0095  * check if there is pending interrupt without
0096  * intack.
0097  */
0098 int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
0099 {
0100     if (kvm_cpu_has_extint(v))
0101         return 1;
0102 
0103     return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
0104 }
0105 EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
0106 
0107 /*
0108  * Read pending interrupt(from non-APIC source)
0109  * vector and intack.
0110  */
0111 static int kvm_cpu_get_extint(struct kvm_vcpu *v)
0112 {
0113     if (!kvm_cpu_has_extint(v)) {
0114         WARN_ON(!lapic_in_kernel(v));
0115         return -1;
0116     }
0117 
0118     if (!lapic_in_kernel(v))
0119         return v->arch.interrupt.nr;
0120 
0121     if (kvm_xen_has_interrupt(v))
0122         return v->kvm->arch.xen.upcall_vector;
0123 
0124     if (irqchip_split(v->kvm)) {
0125         int vector = v->arch.pending_external_vector;
0126 
0127         v->arch.pending_external_vector = -1;
0128         return vector;
0129     } else
0130         return kvm_pic_read_irq(v->kvm); /* PIC */
0131 }
0132 
0133 /*
0134  * Read pending interrupt vector and intack.
0135  */
0136 int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
0137 {
0138     int vector = kvm_cpu_get_extint(v);
0139     if (vector != -1)
0140         return vector;          /* PIC */
0141 
0142     return kvm_get_apic_interrupt(v);   /* APIC */
0143 }
0144 EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
0145 
0146 void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
0147 {
0148     if (lapic_in_kernel(vcpu))
0149         kvm_inject_apic_timer_irqs(vcpu);
0150     if (kvm_xen_timer_enabled(vcpu))
0151         kvm_xen_inject_timer_irqs(vcpu);
0152 }
0153 EXPORT_SYMBOL_GPL(kvm_inject_pending_timer_irqs);
0154 
0155 void __kvm_migrate_timers(struct kvm_vcpu *vcpu)
0156 {
0157     __kvm_migrate_apic_timer(vcpu);
0158     __kvm_migrate_pit_timer(vcpu);
0159     static_call_cond(kvm_x86_migrate_timers)(vcpu);
0160 }
0161 
0162 bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
0163 {
0164     bool resample = args->flags & KVM_IRQFD_FLAG_RESAMPLE;
0165 
0166     return resample ? irqchip_kernel(kvm) : irqchip_in_kernel(kvm);
0167 }