xref: /openbmc/linux/arch/x86/include/asm/kvm_para.h (revision 7e035230)
1 #ifndef _ASM_X86_KVM_PARA_H
2 #define _ASM_X86_KVM_PARA_H
3 
4 #include <linux/types.h>
5 #include <asm/hyperv.h>
6 
7 /* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx.  It
8  * should be used to determine that a VM is running under KVM.
9  */
10 #define KVM_CPUID_SIGNATURE	0x40000000
11 
12 /* This CPUID returns a feature bitmap in eax.  Before enabling a particular
13  * paravirtualization, the appropriate feature bit should be checked.
14  */
15 #define KVM_CPUID_FEATURES	0x40000001
16 #define KVM_FEATURE_CLOCKSOURCE		0
17 #define KVM_FEATURE_NOP_IO_DELAY	1
18 #define KVM_FEATURE_MMU_OP		2
19 /* This indicates that the new set of kvmclock msrs
20  * are available. The use of 0x11 and 0x12 is deprecated
21  */
22 #define KVM_FEATURE_CLOCKSOURCE2        3
23 #define KVM_FEATURE_ASYNC_PF		4
24 #define KVM_FEATURE_STEAL_TIME		5
25 #define KVM_FEATURE_PV_EOI		6
26 
27 /* The last 8 bits are used to indicate how to interpret the flags field
28  * in pvclock structure. If no bits are set, all flags are ignored.
29  */
30 #define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT	24
31 
32 #define MSR_KVM_WALL_CLOCK  0x11
33 #define MSR_KVM_SYSTEM_TIME 0x12
34 
35 #define KVM_MSR_ENABLED 1
36 /* Custom MSRs falls in the range 0x4b564d00-0x4b564dff */
37 #define MSR_KVM_WALL_CLOCK_NEW  0x4b564d00
38 #define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01
39 #define MSR_KVM_ASYNC_PF_EN 0x4b564d02
40 #define MSR_KVM_STEAL_TIME  0x4b564d03
41 #define MSR_KVM_PV_EOI_EN      0x4b564d04
42 
43 struct kvm_steal_time {
44 	__u64 steal;
45 	__u32 version;
46 	__u32 flags;
47 	__u32 pad[12];
48 };
49 
50 #define KVM_STEAL_ALIGNMENT_BITS 5
51 #define KVM_STEAL_VALID_BITS ((-1ULL << (KVM_STEAL_ALIGNMENT_BITS + 1)))
52 #define KVM_STEAL_RESERVED_MASK (((1 << KVM_STEAL_ALIGNMENT_BITS) - 1 ) << 1)
53 
54 #define KVM_MAX_MMU_OP_BATCH           32
55 
56 #define KVM_ASYNC_PF_ENABLED			(1 << 0)
57 #define KVM_ASYNC_PF_SEND_ALWAYS		(1 << 1)
58 
59 /* Operations for KVM_HC_MMU_OP */
60 #define KVM_MMU_OP_WRITE_PTE            1
61 #define KVM_MMU_OP_FLUSH_TLB	        2
62 #define KVM_MMU_OP_RELEASE_PT	        3
63 
64 /* Payload for KVM_HC_MMU_OP */
65 struct kvm_mmu_op_header {
66 	__u32 op;
67 	__u32 pad;
68 };
69 
70 struct kvm_mmu_op_write_pte {
71 	struct kvm_mmu_op_header header;
72 	__u64 pte_phys;
73 	__u64 pte_val;
74 };
75 
76 struct kvm_mmu_op_flush_tlb {
77 	struct kvm_mmu_op_header header;
78 };
79 
80 struct kvm_mmu_op_release_pt {
81 	struct kvm_mmu_op_header header;
82 	__u64 pt_phys;
83 };
84 
85 #define KVM_PV_REASON_PAGE_NOT_PRESENT 1
86 #define KVM_PV_REASON_PAGE_READY 2
87 
88 struct kvm_vcpu_pv_apf_data {
89 	__u32 reason;
90 	__u8 pad[60];
91 	__u32 enabled;
92 };
93 
94 #define KVM_PV_EOI_BIT 0
95 #define KVM_PV_EOI_MASK (0x1 << KVM_PV_EOI_BIT)
96 #define KVM_PV_EOI_ENABLED KVM_PV_EOI_MASK
97 #define KVM_PV_EOI_DISABLED 0x0
98 
99 #ifdef __KERNEL__
100 #include <asm/processor.h>
101 
102 extern void kvmclock_init(void);
103 extern int kvm_register_clock(char *txt);
104 
105 #ifdef CONFIG_KVM_CLOCK
106 bool kvm_check_and_clear_guest_paused(void);
107 #else
108 static inline bool kvm_check_and_clear_guest_paused(void)
109 {
110 	return false;
111 }
112 #endif /* CONFIG_KVMCLOCK */
113 
114 /* This instruction is vmcall.  On non-VT architectures, it will generate a
115  * trap that we will then rewrite to the appropriate instruction.
116  */
117 #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
118 
119 /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
120  * instruction.  The hypervisor may replace it with something else but only the
121  * instructions are guaranteed to be supported.
122  *
123  * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
124  * The hypercall number should be placed in rax and the return value will be
125  * placed in rax.  No other registers will be clobbered unless explicited
126  * noted by the particular hypercall.
127  */
128 
129 static inline long kvm_hypercall0(unsigned int nr)
130 {
131 	long ret;
132 	asm volatile(KVM_HYPERCALL
133 		     : "=a"(ret)
134 		     : "a"(nr)
135 		     : "memory");
136 	return ret;
137 }
138 
139 static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
140 {
141 	long ret;
142 	asm volatile(KVM_HYPERCALL
143 		     : "=a"(ret)
144 		     : "a"(nr), "b"(p1)
145 		     : "memory");
146 	return ret;
147 }
148 
149 static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
150 				  unsigned long p2)
151 {
152 	long ret;
153 	asm volatile(KVM_HYPERCALL
154 		     : "=a"(ret)
155 		     : "a"(nr), "b"(p1), "c"(p2)
156 		     : "memory");
157 	return ret;
158 }
159 
160 static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
161 				  unsigned long p2, unsigned long p3)
162 {
163 	long ret;
164 	asm volatile(KVM_HYPERCALL
165 		     : "=a"(ret)
166 		     : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
167 		     : "memory");
168 	return ret;
169 }
170 
171 static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
172 				  unsigned long p2, unsigned long p3,
173 				  unsigned long p4)
174 {
175 	long ret;
176 	asm volatile(KVM_HYPERCALL
177 		     : "=a"(ret)
178 		     : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
179 		     : "memory");
180 	return ret;
181 }
182 
183 static inline int kvm_para_available(void)
184 {
185 	unsigned int eax, ebx, ecx, edx;
186 	char signature[13];
187 
188 	if (boot_cpu_data.cpuid_level < 0)
189 		return 0;	/* So we don't blow up on old processors */
190 
191 	if (cpu_has_hypervisor) {
192 		cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
193 		memcpy(signature + 0, &ebx, 4);
194 		memcpy(signature + 4, &ecx, 4);
195 		memcpy(signature + 8, &edx, 4);
196 		signature[12] = 0;
197 
198 		if (strcmp(signature, "KVMKVMKVM") == 0)
199 			return 1;
200 	}
201 
202 	return 0;
203 }
204 
205 static inline unsigned int kvm_arch_para_features(void)
206 {
207 	return cpuid_eax(KVM_CPUID_FEATURES);
208 }
209 
210 #ifdef CONFIG_KVM_GUEST
211 void __init kvm_guest_init(void);
212 void kvm_async_pf_task_wait(u32 token);
213 void kvm_async_pf_task_wake(u32 token);
214 u32 kvm_read_and_reset_pf_reason(void);
215 extern void kvm_disable_steal_time(void);
216 #else
217 #define kvm_guest_init() do { } while (0)
218 #define kvm_async_pf_task_wait(T) do {} while(0)
219 #define kvm_async_pf_task_wake(T) do {} while(0)
220 static inline u32 kvm_read_and_reset_pf_reason(void)
221 {
222 	return 0;
223 }
224 
225 static inline void kvm_disable_steal_time(void)
226 {
227 	return;
228 }
229 #endif
230 
231 #endif /* __KERNEL__ */
232 
233 #endif /* _ASM_X86_KVM_PARA_H */
234