blob: e3faaaf4301ed8aaf3cf652f8b33389764276f53 [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);
Gleb Natapovca3f1012010-10-14 11:22:49 +020068extern int kvm_register_clock(char *txt);
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020069
70
Christian Borntraeger5f432382007-10-11 15:34:17 +020071/* This instruction is vmcall. On non-VT architectures, it will generate a
72 * trap that we will then rewrite to the appropriate instruction.
73 */
74#define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
75
76/* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
77 * instruction. The hypervisor may replace it with something else but only the
78 * instructions are guaranteed to be supported.
79 *
80 * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
81 * The hypercall number should be placed in rax and the return value will be
82 * placed in rax. No other registers will be clobbered unless explicited
83 * noted by the particular hypercall.
84 */
85
86static inline long kvm_hypercall0(unsigned int nr)
87{
88 long ret;
89 asm volatile(KVM_HYPERCALL
90 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +030091 : "a"(nr)
92 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +020093 return ret;
94}
95
96static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
97{
98 long ret;
99 asm volatile(KVM_HYPERCALL
100 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +0300101 : "a"(nr), "b"(p1)
102 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200103 return ret;
104}
105
106static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
107 unsigned long p2)
108{
109 long ret;
110 asm volatile(KVM_HYPERCALL
111 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +0300112 : "a"(nr), "b"(p1), "c"(p2)
113 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200114 return ret;
115}
116
117static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
118 unsigned long p2, unsigned long p3)
119{
120 long ret;
121 asm volatile(KVM_HYPERCALL
122 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +0300123 : "a"(nr), "b"(p1), "c"(p2), "d"(p3)
124 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200125 return ret;
126}
127
128static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
129 unsigned long p2, unsigned long p3,
130 unsigned long p4)
131{
132 long ret;
133 asm volatile(KVM_HYPERCALL
134 : "=a"(ret)
Anthony Liguorica373932008-07-03 19:02:36 +0300135 : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4)
136 : "memory");
Christian Borntraeger5f432382007-10-11 15:34:17 +0200137 return ret;
138}
139
140static inline int kvm_para_available(void)
141{
142 unsigned int eax, ebx, ecx, edx;
143 char signature[13];
144
145 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
146 memcpy(signature + 0, &ebx, 4);
147 memcpy(signature + 4, &ecx, 4);
148 memcpy(signature + 8, &edx, 4);
149 signature[12] = 0;
150
151 if (strcmp(signature, "KVMKVMKVM") == 0)
152 return 1;
153
154 return 0;
155}
156
157static inline unsigned int kvm_arch_para_features(void)
158{
159 return cpuid_eax(KVM_CPUID_FEATURES);
160}
161
Alexander Grafba492962010-07-29 14:47:56 +0200162#ifdef CONFIG_KVM_GUEST
163void __init kvm_guest_init(void);
164#else
165#define kvm_guest_init() do { } while (0)
Christian Borntraeger5f432382007-10-11 15:34:17 +0200166#endif
167
Alexander Grafba492962010-07-29 14:47:56 +0200168#endif /* __KERNEL__ */
169
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700170#endif /* _ASM_X86_KVM_PARA_H */