xen.h 6.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
 * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * KVM Xen emulation
 */

#ifndef __ARCH_X86_KVM_XEN_H__
#define __ARCH_X86_KVM_XEN_H__

12 13
#include <asm/xen/hypervisor.h>

14
#ifdef CONFIG_KVM_XEN
15 16 17 18
#include <linux/jump_label_ratelimit.h>

extern struct static_key_false_deferred kvm_xen_enabled;

19
int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu);
20
void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu);
21
void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *vcpu);
22 23
int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
24 25
int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
26
int kvm_xen_hvm_evtchn_send(struct kvm *kvm, struct kvm_irq_routing_xen_evtchn *evt);
27
int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data);
28
int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc);
29
void kvm_xen_init_vm(struct kvm *kvm);
30
void kvm_xen_destroy_vm(struct kvm *kvm);
31
void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu);
32
void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu);
33
int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe,
34 35 36 37
			    struct kvm *kvm);
int kvm_xen_setup_evtchn(struct kvm *kvm,
			 struct kvm_kernel_irq_routing_entry *e,
			 const struct kvm_irq_routing_entry *ue);
38
void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu);
39

40 41 42 43 44 45 46 47 48 49 50 51 52
static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
{
	/*
	 * The local APIC is being enabled. If the per-vCPU upcall vector is
	 * set and the vCPU's evtchn_upcall_pending flag is set, inject the
	 * interrupt.
	 */
	if (static_branch_unlikely(&kvm_xen_enabled.key) &&
	    vcpu->arch.xen.vcpu_info_cache.active &&
	    vcpu->arch.xen.upcall_vector && __kvm_xen_has_interrupt(vcpu))
		kvm_xen_inject_vcpu_vector(vcpu);
}

53 54 55 56 57 58
static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
{
	return static_branch_unlikely(&kvm_xen_enabled.key) &&
		kvm->arch.xen_hvm_config.msr;
}

59 60
static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
{
61 62 63
	return static_branch_unlikely(&kvm_xen_enabled.key) &&
		(kvm->arch.xen_hvm_config.flags &
		 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
64 65
}

66 67 68
static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
{
	if (static_branch_unlikely(&kvm_xen_enabled.key) &&
69 70
	    vcpu->arch.xen.vcpu_info_cache.active &&
	    vcpu->kvm->arch.xen.upcall_vector)
71 72 73 74
		return __kvm_xen_has_interrupt(vcpu);

	return 0;
}
75 76 77 78 79 80 81

static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu)
{
	return static_branch_unlikely(&kvm_xen_enabled.key) &&
		vcpu->arch.xen.evtchn_pending_sel;
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95
static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
{
	return !!vcpu->arch.xen.timer_virq;
}

static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu)
{
	if (kvm_xen_hypercall_enabled(vcpu->kvm) && kvm_xen_timer_enabled(vcpu))
		return atomic_read(&vcpu->arch.xen.timer_pending);

	return 0;
}

void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu);
96 97 98 99 100 101
#else
static inline int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
{
	return 1;
}

102 103 104 105
static inline void kvm_xen_init_vm(struct kvm *kvm)
{
}

106 107 108 109
static inline void kvm_xen_destroy_vm(struct kvm *kvm)
{
}

110 111 112 113
static inline void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu)
{
}

114 115 116 117
static inline void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
{
}

118 119 120 121
static inline void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
{
}

122 123 124 125 126
static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
{
	return false;
}

127 128 129 130 131 132 133 134 135
static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
{
	return false;
}

static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
{
	return 0;
}
136 137 138 139 140 141 142 143 144

static inline void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu)
{
}

static inline bool kvm_xen_has_pending_events(struct kvm_vcpu *vcpu)
{
	return false;
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158

static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu)
{
	return 0;
}

static inline void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu)
{
}

static inline bool kvm_xen_timer_enabled(struct kvm_vcpu *vcpu)
{
	return false;
}
159 160 161 162

static inline void kvm_xen_update_tsc_info(struct kvm_vcpu *vcpu)
{
}
163 164 165
#endif

int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
166 167 168

#include <asm/pvclock-abi.h>
#include <asm/xen/interface.h>
169
#include <xen/interface/vcpu.h>
170

171
void kvm_xen_update_runstate(struct kvm_vcpu *vcpu, int state);
172 173 174

static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu)
{
175
	kvm_xen_update_runstate(vcpu, RUNSTATE_running);
176 177 178 179 180 181 182 183 184 185 186
}

static inline void kvm_xen_runstate_set_preempted(struct kvm_vcpu *vcpu)
{
	/*
	 * If the vCPU wasn't preempted but took a normal exit for
	 * some reason (hypercalls, I/O, etc.), that is accounted as
	 * still RUNSTATE_running, as the VMM is still operating on
	 * behalf of the vCPU. Only if the VMM does actually block
	 * does it need to enter RUNSTATE_blocked.
	 */
187 188 189
	if (WARN_ON_ONCE(!vcpu->preempted))
		return;

190
	kvm_xen_update_runstate(vcpu, RUNSTATE_runnable);
191 192 193
}

/* 32-bit compatibility definitions, also used natively in 32-bit build */
194 195 196 197 198 199
struct compat_arch_vcpu_info {
	unsigned int cr2;
	unsigned int pad[5];
};

struct compat_vcpu_info {
200 201 202 203 204 205
	uint8_t evtchn_upcall_pending;
	uint8_t evtchn_upcall_mask;
	uint16_t pad;
	uint32_t evtchn_pending_sel;
	struct compat_arch_vcpu_info arch;
	struct pvclock_vcpu_time_info time;
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
}; /* 64 bytes (x86) */

struct compat_arch_shared_info {
	unsigned int max_pfn;
	unsigned int pfn_to_mfn_frame_list_list;
	unsigned int nmi_reason;
	unsigned int p2m_cr3;
	unsigned int p2m_vaddr;
	unsigned int p2m_generation;
	uint32_t wc_sec_hi;
};

struct compat_shared_info {
	struct compat_vcpu_info vcpu_info[MAX_VIRT_CPUS];
	uint32_t evtchn_pending[32];
	uint32_t evtchn_mask[32];
	struct pvclock_wall_clock wc;
	struct compat_arch_shared_info arch;
};

226 227 228
#define COMPAT_EVTCHN_2L_NR_CHANNELS (8 *				\
				      sizeof_field(struct compat_shared_info, \
						   evtchn_pending))
229 230 231 232 233 234
struct compat_vcpu_runstate_info {
    int state;
    uint64_t state_entry_time;
    uint64_t time[4];
} __attribute__((packed));

235 236 237 238 239 240 241
struct compat_sched_poll {
	/* This is actually a guest virtual address which points to ports. */
	uint32_t ports;
	unsigned int nr_ports;
	uint64_t timeout;
};

242
#endif /* __ARCH_X86_KVM_XEN_H__ */