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