blob: 5098459420705ea53f41c2d05b0a91b688980334 [file] [log] [blame]
Christian Borntraeger5f432382007-10-11 15:34:17 +02001#ifndef __X86_KVM_PARA_H
2#define __X86_KVM_PARA_H
3
4/* This CPUID returns the signature 'KVMKVMKVM' in ebx, ecx, and edx. It
5 * should be used to determine that a VM is running under KVM.
6 */
7#define KVM_CPUID_SIGNATURE 0x40000000
8
9/* This CPUID returns a feature bitmap in eax. Before enabling a particular
10 * paravirtualization, the appropriate feature bit should be checked.
11 */
12#define KVM_CPUID_FEATURES 0x40000001
Marcelo Tosattia28e4f52008-02-22 12:21:36 -050013#define KVM_FEATURE_CLOCKSOURCE 0
14#define KVM_FEATURE_NOP_IO_DELAY 1
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050015#define KVM_FEATURE_MMU_OP 2
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020016
17#define MSR_KVM_WALL_CLOCK 0x11
18#define MSR_KVM_SYSTEM_TIME 0x12
Christian Borntraeger5f432382007-10-11 15:34:17 +020019
Marcelo Tosatti2f333bc2008-02-22 12:21:37 -050020#define KVM_MAX_MMU_OP_BATCH 32
21
22/* Operations for KVM_HC_MMU_OP */
23#define KVM_MMU_OP_WRITE_PTE 1
24#define KVM_MMU_OP_FLUSH_TLB 2
25#define KVM_MMU_OP_RELEASE_PT 3
26
27/* Payload for KVM_HC_MMU_OP */
28struct kvm_mmu_op_header {
29 __u32 op;
30 __u32 pad;
31};
32
33struct kvm_mmu_op_write_pte {
34 struct kvm_mmu_op_header header;
35 __u64 pte_phys;
36 __u64 pte_val;
37};
38
39struct kvm_mmu_op_flush_tlb {
40 struct kvm_mmu_op_header header;
41};
42
43struct kvm_mmu_op_release_pt {
44 struct kvm_mmu_op_header header;
45 __u64 pt_phys;
46};
47
Christian Borntraeger5f432382007-10-11 15:34:17 +020048#ifdef __KERNEL__
49#include <asm/processor.h>
50
Glauber de Oliveira Costa18068522008-02-15 17:52:47 -020051/* xen binary-compatible interface. See xen headers for details */
52struct kvm_vcpu_time_info {
53 uint32_t version;
54 uint32_t pad0;
55 uint64_t tsc_timestamp;
56 uint64_t system_time;
57 uint32_t tsc_to_system_mul;
58 int8_t tsc_shift;
59 int8_t pad[3];
60} __attribute__((__packed__)); /* 32 bytes */
61
62struct kvm_wall_clock {
63 uint32_t wc_version;
64 uint32_t wc_sec;
65 uint32_t wc_nsec;
66} __attribute__((__packed__));
67
68
69extern void kvmclock_init(void);
70
71
Christian Borntraeger5f432382007-10-11 15:34:17 +020072/* This instruction is vmcall. On non-VT architectures, it will generate a
73 * trap that we will then rewrite to the appropriate instruction.
74 */
75#define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
76
77/* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
78 * instruction. The hypervisor may replace it with something else but only the
79 * instructions are guaranteed to be supported.
80 *
81 * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
82 * The hypercall number should be placed in rax and the return value will be
83 * placed in rax. No other registers will be clobbered unless explicited
84 * noted by the particular hypercall.
85 */
86
87static inline long kvm_hypercall0(unsigned int nr)
88{
89 long ret;
90 asm volatile(KVM_HYPERCALL
91 : "=a"(ret)
92 : "a"(nr));
93 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)
101 : "a"(nr), "b"(p1));
102 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)
111 : "a"(nr), "b"(p1), "c"(p2));
112 return ret;
113}
114
115static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
116 unsigned long p2, unsigned long p3)
117{
118 long ret;
119 asm volatile(KVM_HYPERCALL
120 : "=a"(ret)
121 : "a"(nr), "b"(p1), "c"(p2), "d"(p3));
122 return ret;
123}
124
125static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
126 unsigned long p2, unsigned long p3,
127 unsigned long p4)
128{
129 long ret;
130 asm volatile(KVM_HYPERCALL
131 : "=a"(ret)
132 : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4));
133 return ret;
134}
135
136static inline int kvm_para_available(void)
137{
138 unsigned int eax, ebx, ecx, edx;
139 char signature[13];
140
141 cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
142 memcpy(signature + 0, &ebx, 4);
143 memcpy(signature + 4, &ecx, 4);
144 memcpy(signature + 8, &edx, 4);
145 signature[12] = 0;
146
147 if (strcmp(signature, "KVMKVMKVM") == 0)
148 return 1;
149
150 return 0;
151}
152
153static inline unsigned int kvm_arch_para_features(void)
154{
155 return cpuid_eax(KVM_CPUID_FEATURES);
156}
157
158#endif
159
160#endif