xref: /openbmc/linux/arch/x86/include/asm/kvm_para.h (revision 1fa6ac37)
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 
24 /* The last 8 bits are used to indicate how to interpret the flags field
25  * in pvclock structure. If no bits are set, all flags are ignored.
26  */
27 #define KVM_FEATURE_CLOCKSOURCE_STABLE_BIT	24
28 
29 #define MSR_KVM_WALL_CLOCK  0x11
30 #define MSR_KVM_SYSTEM_TIME 0x12
31 
32 /* Custom MSRs falls in the range 0x4b564d00-0x4b564dff */
33 #define MSR_KVM_WALL_CLOCK_NEW  0x4b564d00
34 #define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01
35 
36 #define KVM_MAX_MMU_OP_BATCH           32
37 
38 /* Operations for KVM_HC_MMU_OP */
39 #define KVM_MMU_OP_WRITE_PTE            1
40 #define KVM_MMU_OP_FLUSH_TLB	        2
41 #define KVM_MMU_OP_RELEASE_PT	        3
42 
43 /* Payload for KVM_HC_MMU_OP */
44 struct kvm_mmu_op_header {
45 	__u32 op;
46 	__u32 pad;
47 };
48 
49 struct kvm_mmu_op_write_pte {
50 	struct kvm_mmu_op_header header;
51 	__u64 pte_phys;
52 	__u64 pte_val;
53 };
54 
55 struct kvm_mmu_op_flush_tlb {
56 	struct kvm_mmu_op_header header;
57 };
58 
59 struct kvm_mmu_op_release_pt {
60 	struct kvm_mmu_op_header header;
61 	__u64 pt_phys;
62 };
63 
64 #ifdef __KERNEL__
65 #include <asm/processor.h>
66 
67 extern void kvmclock_init(void);
68 
69 
70 /* This instruction is vmcall.  On non-VT architectures, it will generate a
71  * trap that we will then rewrite to the appropriate instruction.
72  */
73 #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
74 
75 /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
76  * instruction.  The hypervisor may replace it with something else but only the
77  * instructions are guaranteed to be supported.
78  *
79  * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
80  * The hypercall number should be placed in rax and the return value will be
81  * placed in rax.  No other registers will be clobbered unless explicited
82  * noted by the particular hypercall.
83  */
84 
85 static inline long kvm_hypercall0(unsigned int nr)
86 {
87 	long ret;
88 	asm volatile(KVM_HYPERCALL
89 		     : "=a"(ret)
90 		     : "a"(nr)
91 		     : "memory");
92 	return ret;
93 }
94 
95 static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
96 {
97 	long ret;
98 	asm volatile(KVM_HYPERCALL
99 		     : "=a"(ret)
100 		     : "a"(nr), "b"(p1)
101 		     : "memory");
102 	return ret;
103 }
104 
105 static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
106 				  unsigned long p2)
107 {
108 	long ret;
109 	asm volatile(KVM_HYPERCALL
110 		     : "=a"(ret)
111 		     : "a"(nr), "b"(p1), "c"(p2)
112 		     : "memory");
113 	return ret;
114 }
115 
116 static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
117 				  unsigned long p2, unsigned long p3)
118 {
119 	long ret;
120 	asm volatile(KVM_HYPERCALL
121 		     : "=a"(ret)
122 		     : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
123 		     : "memory");
124 	return ret;
125 }
126 
127 static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
128 				  unsigned long p2, unsigned long p3,
129 				  unsigned long p4)
130 {
131 	long ret;
132 	asm volatile(KVM_HYPERCALL
133 		     : "=a"(ret)
134 		     : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
135 		     : "memory");
136 	return ret;
137 }
138 
139 static inline int kvm_para_available(void)
140 {
141 	unsigned int eax, ebx, ecx, edx;
142 	char signature[13];
143 
144 	cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
145 	memcpy(signature + 0, &ebx, 4);
146 	memcpy(signature + 4, &ecx, 4);
147 	memcpy(signature + 8, &edx, 4);
148 	signature[12] = 0;
149 
150 	if (strcmp(signature, "KVMKVMKVM") == 0)
151 		return 1;
152 
153 	return 0;
154 }
155 
156 static inline unsigned int kvm_arch_para_features(void)
157 {
158 	return cpuid_eax(KVM_CPUID_FEATURES);
159 }
160 
161 #endif
162 
163 #endif /* _ASM_X86_KVM_PARA_H */
164