blob: cc0cf5f37450b0e0c908b4d0ae6db3a5d3339d9a [file] [log] [blame]
Joao Martins23200b72018-06-13 09:55:44 -04001// 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
Paolo Bonzinib59b1532021-02-26 04:54:45 -050012#ifdef CONFIG_KVM_XEN
David Woodhouse7d6bbeb2021-02-02 15:48:05 +000013#include <linux/jump_label_ratelimit.h>
14
15extern struct static_key_false_deferred kvm_xen_enabled;
16
David Woodhouse40da8cc2020-12-09 20:08:30 +000017int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu);
David Woodhouse3e324612021-02-02 16:53:25 +000018int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
19int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data);
Joao Martinsa76b9642020-12-03 15:52:25 +000020int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
21int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
Joao Martins23200b72018-06-13 09:55:44 -040022int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data);
David Woodhouse78e98782021-02-02 13:19:35 +000023int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc);
Paolo Bonzini319afe62021-08-04 12:48:41 -040024void kvm_xen_init_vm(struct kvm *kvm);
David Woodhouse7d6bbeb2021-02-02 15:48:05 +000025void kvm_xen_destroy_vm(struct kvm *kvm);
Joao Martins23200b72018-06-13 09:55:44 -040026
David Woodhouse30b5c852021-03-01 12:53:09 +000027static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
28{
29 return static_branch_unlikely(&kvm_xen_enabled.key) &&
30 kvm->arch.xen_hvm_config.msr;
31}
32
Joao Martins23200b72018-06-13 09:55:44 -040033static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
34{
David Woodhouse7d6bbeb2021-02-02 15:48:05 +000035 return static_branch_unlikely(&kvm_xen_enabled.key) &&
36 (kvm->arch.xen_hvm_config.flags &
37 KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL);
Joao Martins23200b72018-06-13 09:55:44 -040038}
39
David Woodhouse40da8cc2020-12-09 20:08:30 +000040static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
41{
42 if (static_branch_unlikely(&kvm_xen_enabled.key) &&
43 vcpu->arch.xen.vcpu_info_set && vcpu->kvm->arch.xen.upcall_vector)
44 return __kvm_xen_has_interrupt(vcpu);
45
46 return 0;
47}
Paolo Bonzinib59b1532021-02-26 04:54:45 -050048#else
49static inline int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
50{
51 return 1;
52}
53
Paolo Bonzini319afe62021-08-04 12:48:41 -040054static inline void kvm_xen_init_vm(struct kvm *kvm)
55{
56}
57
Paolo Bonzinib59b1532021-02-26 04:54:45 -050058static inline void kvm_xen_destroy_vm(struct kvm *kvm)
59{
60}
61
David Woodhouse30b5c852021-03-01 12:53:09 +000062static inline bool kvm_xen_msr_enabled(struct kvm *kvm)
63{
64 return false;
65}
66
Paolo Bonzinib59b1532021-02-26 04:54:45 -050067static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm)
68{
69 return false;
70}
71
72static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu)
73{
74 return 0;
75}
76#endif
77
78int kvm_xen_hypercall(struct kvm_vcpu *vcpu);
David Woodhouse1ea9f2e2020-12-03 18:45:22 +000079
David Woodhouse1ea9f2e2020-12-03 18:45:22 +000080#include <asm/pvclock-abi.h>
81#include <asm/xen/interface.h>
David Woodhouse30b5c852021-03-01 12:53:09 +000082#include <xen/interface/vcpu.h>
David Woodhouse1ea9f2e2020-12-03 18:45:22 +000083
David Woodhouse30b5c852021-03-01 12:53:09 +000084void kvm_xen_update_runstate_guest(struct kvm_vcpu *vcpu, int state);
85
86static inline void kvm_xen_runstate_set_running(struct kvm_vcpu *vcpu)
87{
88 kvm_xen_update_runstate_guest(vcpu, RUNSTATE_running);
89}
90
91static inline void kvm_xen_runstate_set_preempted(struct kvm_vcpu *vcpu)
92{
93 /*
94 * If the vCPU wasn't preempted but took a normal exit for
95 * some reason (hypercalls, I/O, etc.), that is accounted as
96 * still RUNSTATE_running, as the VMM is still operating on
97 * behalf of the vCPU. Only if the VMM does actually block
98 * does it need to enter RUNSTATE_blocked.
99 */
100 if (vcpu->preempted)
101 kvm_xen_update_runstate_guest(vcpu, RUNSTATE_runnable);
102}
103
104/* 32-bit compatibility definitions, also used natively in 32-bit build */
David Woodhouse1ea9f2e2020-12-03 18:45:22 +0000105struct compat_arch_vcpu_info {
106 unsigned int cr2;
107 unsigned int pad[5];
108};
109
110struct compat_vcpu_info {
Sean Christopherson7137b7a2021-02-10 10:26:09 -0800111 uint8_t evtchn_upcall_pending;
112 uint8_t evtchn_upcall_mask;
113 uint16_t pad;
114 uint32_t evtchn_pending_sel;
115 struct compat_arch_vcpu_info arch;
116 struct pvclock_vcpu_time_info time;
David Woodhouse1ea9f2e2020-12-03 18:45:22 +0000117}; /* 64 bytes (x86) */
118
119struct compat_arch_shared_info {
120 unsigned int max_pfn;
121 unsigned int pfn_to_mfn_frame_list_list;
122 unsigned int nmi_reason;
123 unsigned int p2m_cr3;
124 unsigned int p2m_vaddr;
125 unsigned int p2m_generation;
126 uint32_t wc_sec_hi;
127};
128
129struct compat_shared_info {
130 struct compat_vcpu_info vcpu_info[MAX_VIRT_CPUS];
131 uint32_t evtchn_pending[32];
132 uint32_t evtchn_mask[32];
133 struct pvclock_wall_clock wc;
134 struct compat_arch_shared_info arch;
135};
136
David Woodhouse30b5c852021-03-01 12:53:09 +0000137struct compat_vcpu_runstate_info {
138 int state;
139 uint64_t state_entry_time;
140 uint64_t time[4];
141} __attribute__((packed));
142
Joao Martins23200b72018-06-13 09:55:44 -0400143#endif /* __ARCH_X86_KVM_XEN_H__ */