blob: 7b562b6184bcf6d1b4b640c676ed6d4f3f0bae2e [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
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020023
Glauber Costa3a0d7252010-05-11 12:17:45 -040024/* 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
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020029#define MSR_KVM_WALL_CLOCK 0x11
30#define MSR_KVM_SYSTEM_TIME 0x12
Christian Borntraeger5f432382007-10-11 15:34:17 +020031
Glauber Costa11c6bff2010-05-11 12:17:41 -040032/* 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
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050036#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 */
44struct kvm_mmu_op_header {
45 __u32 op;
46 __u32 pad;
47};
48
49struct kvm_mmu_op_write_pte {
50 struct kvm_mmu_op_header header;
51 __u64 pte_phys;
52 __u64 pte_val;
53};
54
55struct kvm_mmu_op_flush_tlb {
56 struct kvm_mmu_op_header header;
57};
58
59struct kvm_mmu_op_release_pt {
60 struct kvm_mmu_op_header header;
61 __u64 pt_phys;
62};
63
Christian Borntraeger5f432382007-10-11 15:34:17 +020064#ifdef __KERNEL__
65#include <asm/processor.h>
66
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020067extern void kvmclock_init(void);
68
69
Christian Borntraeger5f432382007-10-11 15:34:17 +020070/* 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
85static inline long kvm_hypercall0(unsigned int nr)
86{
87 long ret;
88 asm volatile(KVM_HYPERCALL
89 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +030090 : "a"(nr)
91 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +020092 return ret;
93}
94
95static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
96{
97 long ret;
98 asm volatile(KVM_HYPERCALL
99 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +0300100 : "a"(nr), "b"(p1)
101 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200102 return ret;
103}
104
105static 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)
Anthony Liguorica373932008-07-03 19:02:36 +0300111 : "a"(nr), "b"(p1), "c"(p2)
112 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200113 return ret;
114}
115
116static 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)
Anthony Liguorica373932008-07-03 19:02:36 +0300122 : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
123 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200124 return ret;
125}
126
127static 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)
Anthony Liguorica373932008-07-03 19:02:36 +0300134 : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
135 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200136 return ret;
137}
138
139static 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
156static inline unsigned int kvm_arch_para_features(void)
157{
158 return cpuid_eax(KVM_CPUID_FEATURES);
159}
160
Alexander Grafba492962010-07-29 14:47:56 +0200161#ifdef CONFIG_KVM_GUEST
162void __init kvm_guest_init(void);
163#else
164#define kvm_guest_init() do { } while (0)
Christian Borntraeger5f432382007-10-11 15:34:17 +0200165#endif
166
Alexander Grafba492962010-07-29 14:47:56 +0200167#endif /* __KERNEL__ */
168
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700169#endif /* _ASM_X86_KVM_PARA_H */