blob: eb3e9d85e1f1c75fc0db709cc40dd7a226d928cd [file] [log] [blame]
H. Peter Anvin1965aae2008-10-22 22:26:29 -07001#ifndef _ASM_X86_KVM_PARA_H
2#define _ASM_X86_KVM_PARA_H
Christian Borntraeger5f432382007-10-11 15:34:17 +02003
Avi Kivityfa6870c2009-08-16 15:31:33 +03004#include <linux/types.h>
Gleb Natapov55cd8e52010-01-17 15:51:22 +02005#include <asm/hyperv.h>
Avi Kivityfa6870c2009-08-16 15:31:33 +03006
Christian Borntraeger5f432382007-10-11 15:34:17 +02007/* 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
Marcelo Tosattia28e4f52008-02-22 12:21:36 -050016#define KVM_FEATURE_CLOCKSOURCE 0
17#define KVM_FEATURE_NOP_IO_DELAY 1
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050018#define KVM_FEATURE_MMU_OP 2
Glauber Costa0e6ac582010-05-11 12:17:42 -040019/* 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
Gleb Natapov344d9582010-10-14 11:22:50 +020023#define KVM_FEATURE_ASYNC_PF 4
Glauber Costa9ddabbe2011-07-11 15:28:13 -040024#define KVM_FEATURE_STEAL_TIME 5
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +030025#define KVM_FEATURE_PV_EOI 6
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020026
Glauber Costa3a0d7252010-05-11 12:17:45 -040027/* 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
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020032#define MSR_KVM_WALL_CLOCK 0x11
33#define MSR_KVM_SYSTEM_TIME 0x12
Christian Borntraeger5f432382007-10-11 15:34:17 +020034
Glauber Costa4b6b35f2011-07-11 15:28:12 -040035#define KVM_MSR_ENABLED 1
Glauber Costa11c6bff2010-05-11 12:17:41 -040036/* Custom MSRs falls in the range 0x4b564d00-0x4b564dff */
37#define MSR_KVM_WALL_CLOCK_NEW 0x4b564d00
38#define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01
Gleb Natapov344d9582010-10-14 11:22:50 +020039#define MSR_KVM_ASYNC_PF_EN 0x4b564d02
Glauber Costa9ddabbe2011-07-11 15:28:13 -040040#define MSR_KVM_STEAL_TIME 0x4b564d03
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +030041#define MSR_KVM_PV_EOI_EN 0x4b564d04
Glauber Costa9ddabbe2011-07-11 15:28:13 -040042
43struct kvm_steal_time {
44 __u64 steal;
45 __u32 version;
46 __u32 flags;
47 __u32 pad[12];
48};
Glauber Costa11c6bff2010-05-11 12:17:41 -040049
Glauber Costac9aaa892011-07-11 15:28:14 -040050#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
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050054#define KVM_MAX_MMU_OP_BATCH 32
55
Gleb Natapov344d9582010-10-14 11:22:50 +020056#define KVM_ASYNC_PF_ENABLED (1 << 0)
Gleb Natapov6adba522010-10-14 11:22:55 +020057#define KVM_ASYNC_PF_SEND_ALWAYS (1 << 1)
Gleb Natapov344d9582010-10-14 11:22:50 +020058
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050059/* 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 */
65struct kvm_mmu_op_header {
66 __u32 op;
67 __u32 pad;
68};
69
70struct kvm_mmu_op_write_pte {
71 struct kvm_mmu_op_header header;
72 __u64 pte_phys;
73 __u64 pte_val;
74};
75
76struct kvm_mmu_op_flush_tlb {
77 struct kvm_mmu_op_header header;
78};
79
80struct kvm_mmu_op_release_pt {
81 struct kvm_mmu_op_header header;
82 __u64 pt_phys;
83};
84
Gleb Natapov631bc482010-10-14 11:22:52 +020085#define KVM_PV_REASON_PAGE_NOT_PRESENT 1
86#define KVM_PV_REASON_PAGE_READY 2
87
Gleb Natapovfd10cde2010-10-14 11:22:51 +020088struct kvm_vcpu_pv_apf_data {
89 __u32 reason;
90 __u8 pad[60];
91 __u32 enabled;
92};
93
Michael S. Tsirkinab9cf492012-06-24 19:24:34 +030094#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
Christian Borntraeger5f432382007-10-11 15:34:17 +020099#ifdef __KERNEL__
100#include <asm/processor.h>
101
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200102extern void kvmclock_init(void);
Gleb Natapovca3f1012010-10-14 11:22:49 +0200103extern int kvm_register_clock(char *txt);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200104
Marcelo Tosatti90993cd2012-08-16 17:00:19 -0300105#ifdef CONFIG_KVM_GUEST
Eric B Munson3b5d56b2012-03-10 14:37:26 -0500106bool kvm_check_and_clear_guest_paused(void);
107#else
108static inline bool kvm_check_and_clear_guest_paused(void)
109{
110 return false;
111}
Marcelo Tosatti90993cd2012-08-16 17:00:19 -0300112#endif /* CONFIG_KVM_GUEST */
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -0200113
Christian Borntraeger5f432382007-10-11 15:34:17 +0200114/* 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
Raghavendra K Te423ca12012-08-07 13:10:13 +0530119/* For KVM hypercalls, a three-byte sequence of either the vmcall or the vmmcall
Christian Borntraeger5f432382007-10-11 15:34:17 +0200120 * 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
129static inline long kvm_hypercall0(unsigned int nr)
130{
131 long ret;
132 asm volatile(KVM_HYPERCALL
133 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +0300134 : "a"(nr)
135 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200136 return ret;
137}
138
139static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
140{
141 long ret;
142 asm volatile(KVM_HYPERCALL
143 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +0300144 : "a"(nr), "b"(p1)
145 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200146 return ret;
147}
148
149static 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)
Anthony Liguorica373932008-07-03 19:02:36 +0300155 : "a"(nr), "b"(p1), "c"(p2)
156 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200157 return ret;
158}
159
160static 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)
Anthony Liguorica373932008-07-03 19:02:36 +0300166 : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
167 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200168 return ret;
169}
170
171static 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)
Anthony Liguorica373932008-07-03 19:02:36 +0300178 : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
179 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200180 return ret;
181}
182
183static inline int kvm_para_available(void)
184{
185 unsigned int eax, ebx, ecx, edx;
186 char signature[13];
187
Alan Coxc3709e62012-05-14 18:41:09 +0100188 if (boot_cpu_data.cpuid_level < 0)
189 return 0; /* So we don't blow up on old processors */
190
Gleb Natapov9b72d3b2012-04-30 14:45:49 +0300191 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;
Christian Borntraeger5f432382007-10-11 15:34:17 +0200197
Gleb Natapov9b72d3b2012-04-30 14:45:49 +0300198 if (strcmp(signature, "KVMKVMKVM") == 0)
199 return 1;
200 }
Christian Borntraeger5f432382007-10-11 15:34:17 +0200201
202 return 0;
203}
204
205static inline unsigned int kvm_arch_para_features(void)
206{
207 return cpuid_eax(KVM_CPUID_FEATURES);
208}
209
Alexander Grafba492962010-07-29 14:47:56 +0200210#ifdef CONFIG_KVM_GUEST
211void __init kvm_guest_init(void);
Gleb Natapov631bc482010-10-14 11:22:52 +0200212void kvm_async_pf_task_wait(u32 token);
213void kvm_async_pf_task_wake(u32 token);
214u32 kvm_read_and_reset_pf_reason(void);
Glauber Costad910f5c2011-07-11 15:28:19 -0400215extern void kvm_disable_steal_time(void);
Alexander Grafba492962010-07-29 14:47:56 +0200216#else
217#define kvm_guest_init() do { } while (0)
Gleb Natapov631bc482010-10-14 11:22:52 +0200218#define kvm_async_pf_task_wait(T) do {} while(0)
219#define kvm_async_pf_task_wake(T) do {} while(0)
Jan Kiszkad4c90b02010-10-20 18:34:54 +0200220static inline u32 kvm_read_and_reset_pf_reason(void)
Gleb Natapov631bc482010-10-14 11:22:52 +0200221{
222 return 0;
223}
Glauber Costad910f5c2011-07-11 15:28:19 -0400224
225static inline void kvm_disable_steal_time(void)
226{
227 return;
228}
Christian Borntraeger5f432382007-10-11 15:34:17 +0200229#endif
230
Alexander Grafba492962010-07-29 14:47:56 +0200231#endif /* __KERNEL__ */
232
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700233#endif /* _ASM_X86_KVM_PARA_H */