KVM: add basic paravirt support
[pandora-kernel.git] / include / asm-x86 / kvm_para.h
1 #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
13 #define KVM_FEATURE_CLOCKSOURCE         0
14 #define KVM_FEATURE_NOP_IO_DELAY        1
15
16 #define MSR_KVM_WALL_CLOCK  0x11
17 #define MSR_KVM_SYSTEM_TIME 0x12
18
19 #ifdef __KERNEL__
20 #include <asm/processor.h>
21
22 /* xen binary-compatible interface. See xen headers for details */
23 struct kvm_vcpu_time_info {
24         uint32_t version;
25         uint32_t pad0;
26         uint64_t tsc_timestamp;
27         uint64_t system_time;
28         uint32_t tsc_to_system_mul;
29         int8_t   tsc_shift;
30         int8_t   pad[3];
31 } __attribute__((__packed__)); /* 32 bytes */
32
33 struct kvm_wall_clock {
34         uint32_t wc_version;
35         uint32_t wc_sec;
36         uint32_t wc_nsec;
37 } __attribute__((__packed__));
38
39
40 extern void kvmclock_init(void);
41
42
43 /* This instruction is vmcall.  On non-VT architectures, it will generate a
44  * trap that we will then rewrite to the appropriate instruction.
45  */
46 #define KVM_HYPERCALL ".byte 0x0f,0x01,0xc1"
47
48 /* For KVM hypercalls, a three-byte sequence of either the vmrun or the vmmrun
49  * instruction.  The hypervisor may replace it with something else but only the
50  * instructions are guaranteed to be supported.
51  *
52  * Up to four arguments may be passed in rbx, rcx, rdx, and rsi respectively.
53  * The hypercall number should be placed in rax and the return value will be
54  * placed in rax.  No other registers will be clobbered unless explicited
55  * noted by the particular hypercall.
56  */
57
58 static inline long kvm_hypercall0(unsigned int nr)
59 {
60         long ret;
61         asm volatile(KVM_HYPERCALL
62                      : "=a"(ret)
63                      : "a"(nr));
64         return ret;
65 }
66
67 static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
68 {
69         long ret;
70         asm volatile(KVM_HYPERCALL
71                      : "=a"(ret)
72                      : "a"(nr), "b"(p1));
73         return ret;
74 }
75
76 static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
77                                   unsigned long p2)
78 {
79         long ret;
80         asm volatile(KVM_HYPERCALL
81                      : "=a"(ret)
82                      : "a"(nr), "b"(p1), "c"(p2));
83         return ret;
84 }
85
86 static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
87                                   unsigned long p2, unsigned long p3)
88 {
89         long ret;
90         asm volatile(KVM_HYPERCALL
91                      : "=a"(ret)
92                      : "a"(nr), "b"(p1), "c"(p2), "d"(p3));
93         return ret;
94 }
95
96 static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
97                                   unsigned long p2, unsigned long p3,
98                                   unsigned long p4)
99 {
100         long ret;
101         asm volatile(KVM_HYPERCALL
102                      : "=a"(ret)
103                      : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4));
104         return ret;
105 }
106
107 static inline int kvm_para_available(void)
108 {
109         unsigned int eax, ebx, ecx, edx;
110         char signature[13];
111
112         cpuid(KVM_CPUID_SIGNATURE, &eax, &ebx, &ecx, &edx);
113         memcpy(signature + 0, &ebx, 4);
114         memcpy(signature + 4, &ecx, 4);
115         memcpy(signature + 8, &edx, 4);
116         signature[12] = 0;
117
118         if (strcmp(signature, "KVMKVMKVM") == 0)
119                 return 1;
120
121         return 0;
122 }
123
124 static inline unsigned int kvm_arch_para_features(void)
125 {
126         return cpuid_eax(KVM_CPUID_FEATURES);
127 }
128
129 #endif
130
131 #endif