Mention branches and keyring.
[releases.git] / x86 / kvm / xen.h
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
4  * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5  *
6  * KVM Xen emulation
7  */
8
9 #ifndef __ARCH_X86_KVM_XEN_H__
10 #define __ARCH_X86_KVM_XEN_H__
11
12 #ifdef CONFIG_KVM_XEN
13 #include <linux/jump_label_ratelimit.h>
14
15 extern struct static_key_false_deferred kvm_xen_enabled;
16
17 int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu);
18 void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu);
19 void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *vcpu);
20 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
21 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
22 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
23 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
24 int kvm_xen_hvm_evtchn_send(struct kvm *kvm, struct kvm_irq_routing_xen_evtchn *evt);
25 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data);
26 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc);
27 void kvm_xen_init_vm(struct kvm *kvm);
28 void kvm_xen_destroy_vm(struct kvm *kvm);
29 void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu);
30 void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu);
31 int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe,
32                             struct kvm *kvm);
33 int kvm_xen_setup_evtchn(struct kvm *kvm,
34                          struct kvm_kernel_irq_routing_entry *e,
35                          const struct kvm_irq_routing_entry *ue);
36
37 static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
38 {
39         /*
40          * The local APIC is being enabled. If the per-vCPU upcall vector is
41          * set and the vCPU's evtchn_upcall_pending flag is set, inject the
42          * interrupt.
43          */
44         if (static_branch_unlikely(&kvm_xen_enabled.key) &&
45             vcpu->arch.xen.vcpu_info_cache.active &&
46             vcpu->arch.xen.upcall_vector && __kvm_xen_has_interrupt(vcpu))
47                 kvm_xen_inject_vcpu_vector(vcpu);
48 }
49
50 static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
51 {
52         return static_branch_unlikely(&kvm_xen_enabled.key) &&
53                 kvm->arch.xen_hvm_config.msr;
54 }
55
56 static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
57 {
58         return static_branch_unlikely(&kvm_xen_enabled.key) &&
59                 (kvm->arch.xen_hvm_config.flags &
60                  KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
61 }
62
63 static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
64 {
65         if (static_branch_unlikely(&kvm_xen_enabled.key) &&
66             vcpu->arch.xen.vcpu_info_cache.active &&
67             vcpu->kvm->arch.xen.upcall_vector)
68                 return __kvm_xen_has_interrupt(vcpu);
69
70         return 0;
71 }
72
73 static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu)
74 {
75         return static_branch_unlikely(&kvm_xen_enabled.key) &&
76                 vcpu->arch.xen.evtchn_pending_sel;
77 }
78
79 static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
80 {
81         return !!vcpu->arch.xen.timer_virq;
82 }
83
84 static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu)
85 {
86         if (kvm_xen_hypercall_enabled(vcpu->kvm) && kvm_xen_timer_enabled(vcpu))
87                 return atomic_read(&vcpu->arch.xen.timer_pending);
88
89         return 0;
90 }
91
92 void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu);
93 #else
94 static inline int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
95 {
96         return 1;
97 }
98
99 static inline void kvm_xen_init_vm(struct kvm *kvm)
100 {
101 }
102
103 static inline void kvm_xen_destroy_vm(struct kvm *kvm)
104 {
105 }
106
107 static inline void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu)
108 {
109 }
110
111 static inline void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
112 {
113 }
114
115 static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
116 {
117 }
118
119 static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
120 {
121         return false;
122 }
123
124 static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
125 {
126         return false;
127 }
128
129 static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
130 {
131         return 0;
132 }
133
134 static inline void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu)
135 {
136 }
137
138 static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu)
139 {
140         return false;
141 }
142
143 static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu)
144 {
145         return 0;
146 }
147
148 static inline void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu)
149 {
150 }
151
152 static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
153 {
154         return false;
155 }
156 #endif
157
158 int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
159
160 #include <asm/pvclock-abi.h>
161 #include <asm/xen/interface.h>
162 #include <xen/interface/vcpu.h>
163
164 void kvm_xen_update_runstate_guest(struct kvm_vcpu *vcpu, int state);
165
166 static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu)
167 {
168         kvm_xen_update_runstate_guest(vcpu, RUNSTATE_running);
169 }
170
171 static inline void kvm_xen_runstate_set_preempted(struct kvm_vcpu *vcpu)
172 {
173         /*
174          * If the vCPU wasn't preempted but took a normal exit for
175          * some reason (hypercalls, I/O, etc.), that is accounted as
176          * still RUNSTATE_running, as the VMM is still operating on
177          * behalf of the vCPU. Only if the VMM does actually block
178          * does it need to enter RUNSTATE_blocked.
179          */
180         if (WARN_ON_ONCE(!vcpu->preempted))
181                 return;
182
183         kvm_xen_update_runstate_guest(vcpu, RUNSTATE_runnable);
184 }
185
186 /* 32-bit compatibility definitions, also used natively in 32-bit build */
187 struct compat_arch_vcpu_info {
188         unsigned int cr2;
189         unsigned int pad[5];
190 };
191
192 struct compat_vcpu_info {
193         uint8_t evtchn_upcall_pending;
194         uint8_t evtchn_upcall_mask;
195         uint16_t pad;
196         uint32_t evtchn_pending_sel;
197         struct compat_arch_vcpu_info arch;
198         struct pvclock_vcpu_time_info time;
199 }; /* 64 bytes (x86) */
200
201 struct compat_arch_shared_info {
202         unsigned int max_pfn;
203         unsigned int pfn_to_mfn_frame_list_list;
204         unsigned int nmi_reason;
205         unsigned int p2m_cr3;
206         unsigned int p2m_vaddr;
207         unsigned int p2m_generation;
208         uint32_t wc_sec_hi;
209 };
210
211 struct compat_shared_info {
212         struct compat_vcpu_info vcpu_info[MAX_VIRT_CPUS];
213         uint32_t evtchn_pending[32];
214         uint32_t evtchn_mask[32];
215         struct pvclock_wall_clock wc;
216         struct compat_arch_shared_info arch;
217 };
218
219 #define COMPAT_EVTCHN_2L_NR_CHANNELS (8 *                               \
220                                       sizeof_field(struct compat_shared_info, \
221                                                    evtchn_pending))
222 struct compat_vcpu_runstate_info {
223     int state;
224     uint64_t state_entry_time;
225     uint64_t time[4];
226 } __attribute__((packed));
227
228 #endif /* __ARCH_X86_KVM_XEN_H__ */