H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_KVM_PARA_H |
| 2 | #define _ASM_X86_KVM_PARA_H |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 3 | |
Avi Kivity | fa6870c | 2009-08-16 15:31:33 +0300 | [diff] [blame] | 4 | #include <linux/types.h> |
Gleb Natapov | 55cd8e5 | 2010-01-17 15:51:22 +0200 | [diff] [blame] | 5 | #include <asm/hyperv.h> |
Avi Kivity | fa6870c | 2009-08-16 15:31:33 +0300 | [diff] [blame] | 6 | |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 7 | /* 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 Tosatti | a28e4f5 | 2008-02-22 12:21:36 -0500 | [diff] [blame] | 16 | #define KVM_FEATURE_CLOCKSOURCE 0 |
| 17 | #define KVM_FEATURE_NOP_IO_DELAY 1 |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 18 | #define KVM_FEATURE_MMU_OP 2 |
Glauber Costa | 0e6ac58 | 2010-05-11 12:17:42 -0400 | [diff] [blame] | 19 | /* 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 Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 23 | #define KVM_FEATURE_ASYNC_PF 4 |
Glauber Costa | 9ddabbe | 2011-07-11 15:28:13 -0400 | [diff] [blame] | 24 | #define KVM_FEATURE_STEAL_TIME 5 |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 25 | #define KVM_FEATURE_PV_EOI 6 |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 26 | |
Glauber Costa | 3a0d725 | 2010-05-11 12:17:45 -0400 | [diff] [blame] | 27 | /* 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 Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 32 | #define MSR_KVM_WALL_CLOCK 0x11 |
| 33 | #define MSR_KVM_SYSTEM_TIME 0x12 |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 34 | |
Glauber Costa | 4b6b35f | 2011-07-11 15:28:12 -0400 | [diff] [blame] | 35 | #define KVM_MSR_ENABLED 1 |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 36 | /* 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 Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 39 | #define MSR_KVM_ASYNC_PF_EN 0x4b564d02 |
Glauber Costa | 9ddabbe | 2011-07-11 15:28:13 -0400 | [diff] [blame] | 40 | #define MSR_KVM_STEAL_TIME 0x4b564d03 |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 41 | #define MSR_KVM_PV_EOI_EN 0x4b564d04 |
Glauber Costa | 9ddabbe | 2011-07-11 15:28:13 -0400 | [diff] [blame] | 42 | |
| 43 | struct kvm_steal_time { |
| 44 | __u64 steal; |
| 45 | __u32 version; |
| 46 | __u32 flags; |
| 47 | __u32 pad[12]; |
| 48 | }; |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 49 | |
Glauber Costa | c9aaa89 | 2011-07-11 15:28:14 -0400 | [diff] [blame] | 50 | #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 Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 54 | #define KVM_MAX_MMU_OP_BATCH 32 |
| 55 | |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 56 | #define KVM_ASYNC_PF_ENABLED (1 << 0) |
Gleb Natapov | 6adba52 | 2010-10-14 11:22:55 +0200 | [diff] [blame] | 57 | #define KVM_ASYNC_PF_SEND_ALWAYS (1 << 1) |
Gleb Natapov | 344d958 | 2010-10-14 11:22:50 +0200 | [diff] [blame] | 58 | |
Marcelo Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 59 | /* 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 */ |
| 65 | struct kvm_mmu_op_header { |
| 66 | __u32 op; |
| 67 | __u32 pad; |
| 68 | }; |
| 69 | |
| 70 | struct kvm_mmu_op_write_pte { |
| 71 | struct kvm_mmu_op_header header; |
| 72 | __u64 pte_phys; |
| 73 | __u64 pte_val; |
| 74 | }; |
| 75 | |
| 76 | struct kvm_mmu_op_flush_tlb { |
| 77 | struct kvm_mmu_op_header header; |
| 78 | }; |
| 79 | |
| 80 | struct kvm_mmu_op_release_pt { |
| 81 | struct kvm_mmu_op_header header; |
| 82 | __u64 pt_phys; |
| 83 | }; |
| 84 | |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 85 | #define KVM_PV_REASON_PAGE_NOT_PRESENT 1 |
| 86 | #define KVM_PV_REASON_PAGE_READY 2 |
| 87 | |
Gleb Natapov | fd10cde | 2010-10-14 11:22:51 +0200 | [diff] [blame] | 88 | struct kvm_vcpu_pv_apf_data { |
| 89 | __u32 reason; |
| 90 | __u8 pad[60]; |
| 91 | __u32 enabled; |
| 92 | }; |
| 93 | |
Michael S. Tsirkin | ab9cf49 | 2012-06-24 19:24:34 +0300 | [diff] [blame] | 94 | #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 Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 99 | #ifdef __KERNEL__ |
| 100 | #include <asm/processor.h> |
| 101 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 102 | extern void kvmclock_init(void); |
Gleb Natapov | ca3f101 | 2010-10-14 11:22:49 +0200 | [diff] [blame] | 103 | extern int kvm_register_clock(char *txt); |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 104 | |
Marcelo Tosatti | 90993cd | 2012-08-16 17:00:19 -0300 | [diff] [blame] | 105 | #ifdef CONFIG_KVM_GUEST |
Eric B Munson | 3b5d56b | 2012-03-10 14:37:26 -0500 | [diff] [blame] | 106 | bool kvm_check_and_clear_guest_paused(void); |
| 107 | #else |
| 108 | static inline bool kvm_check_and_clear_guest_paused(void) |
| 109 | { |
| 110 | return false; |
| 111 | } |
Marcelo Tosatti | 90993cd | 2012-08-16 17:00:19 -0300 | [diff] [blame] | 112 | #endif /* CONFIG_KVM_GUEST */ |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 113 | |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 114 | /* 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 T | e423ca1 | 2012-08-07 13:10:13 +0530 | [diff] [blame] | 119 | /* For KVM hypercalls, a three-byte sequence of either the vmcall or the vmmcall |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 120 | * 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 | |
| 129 | static inline long kvm_hypercall0(unsigned int nr) |
| 130 | { |
| 131 | long ret; |
| 132 | asm volatile(KVM_HYPERCALL |
| 133 | : "=a"(ret) |
Anthony Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 134 | : "a"(nr) |
| 135 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static inline long kvm_hypercall1(unsigned int nr, unsigned long p1) |
| 140 | { |
| 141 | long ret; |
| 142 | asm volatile(KVM_HYPERCALL |
| 143 | : "=a"(ret) |
Anthony Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 144 | : "a"(nr), "b"(p1) |
| 145 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static 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 Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 155 | : "a"(nr), "b"(p1), "c"(p2) |
| 156 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | static 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 Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 166 | : "a"(nr), "b"(p1), "c"(p2), "d"(p3) |
| 167 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static 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 Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 178 | : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4) |
| 179 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | static inline int kvm_para_available(void) |
| 184 | { |
| 185 | unsigned int eax, ebx, ecx, edx; |
| 186 | char signature[13]; |
| 187 | |
Alan Cox | c3709e6 | 2012-05-14 18:41:09 +0100 | [diff] [blame] | 188 | if (boot_cpu_data.cpuid_level < 0) |
| 189 | return 0; /* So we don't blow up on old processors */ |
| 190 | |
Gleb Natapov | 9b72d3b | 2012-04-30 14:45:49 +0300 | [diff] [blame] | 191 | 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 Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 197 | |
Gleb Natapov | 9b72d3b | 2012-04-30 14:45:49 +0300 | [diff] [blame] | 198 | if (strcmp(signature, "KVMKVMKVM") == 0) |
| 199 | return 1; |
| 200 | } |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static inline unsigned int kvm_arch_para_features(void) |
| 206 | { |
| 207 | return cpuid_eax(KVM_CPUID_FEATURES); |
| 208 | } |
| 209 | |
Alexander Graf | ba49296 | 2010-07-29 14:47:56 +0200 | [diff] [blame] | 210 | #ifdef CONFIG_KVM_GUEST |
| 211 | void __init kvm_guest_init(void); |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 212 | void kvm_async_pf_task_wait(u32 token); |
| 213 | void kvm_async_pf_task_wake(u32 token); |
| 214 | u32 kvm_read_and_reset_pf_reason(void); |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 215 | extern void kvm_disable_steal_time(void); |
Alexander Graf | ba49296 | 2010-07-29 14:47:56 +0200 | [diff] [blame] | 216 | #else |
| 217 | #define kvm_guest_init() do { } while (0) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 218 | #define kvm_async_pf_task_wait(T) do {} while(0) |
| 219 | #define kvm_async_pf_task_wake(T) do {} while(0) |
Jan Kiszka | d4c90b0 | 2010-10-20 18:34:54 +0200 | [diff] [blame] | 220 | static inline u32 kvm_read_and_reset_pf_reason(void) |
Gleb Natapov | 631bc48 | 2010-10-14 11:22:52 +0200 | [diff] [blame] | 221 | { |
| 222 | return 0; |
| 223 | } |
Glauber Costa | d910f5c | 2011-07-11 15:28:19 -0400 | [diff] [blame] | 224 | |
| 225 | static inline void kvm_disable_steal_time(void) |
| 226 | { |
| 227 | return; |
| 228 | } |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 229 | #endif |
| 230 | |
Alexander Graf | ba49296 | 2010-07-29 14:47:56 +0200 | [diff] [blame] | 231 | #endif /* __KERNEL__ */ |
| 232 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 233 | #endif /* _ASM_X86_KVM_PARA_H */ |