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