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 |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 23 | |
Glauber Costa | 3a0d725 | 2010-05-11 12:17:45 -0400 | [diff] [blame] | 24 | /* 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 Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 29 | #define MSR_KVM_WALL_CLOCK 0x11 |
| 30 | #define MSR_KVM_SYSTEM_TIME 0x12 |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 31 | |
Glauber Costa | 11c6bff | 2010-05-11 12:17:41 -0400 | [diff] [blame] | 32 | /* 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 Tosatti | 2f333bc | 2008-02-22 12:21:37 -0500 | [diff] [blame] | 36 | #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 */ |
| 44 | struct kvm_mmu_op_header { |
| 45 | __u32 op; |
| 46 | __u32 pad; |
| 47 | }; |
| 48 | |
| 49 | struct kvm_mmu_op_write_pte { |
| 50 | struct kvm_mmu_op_header header; |
| 51 | __u64 pte_phys; |
| 52 | __u64 pte_val; |
| 53 | }; |
| 54 | |
| 55 | struct kvm_mmu_op_flush_tlb { |
| 56 | struct kvm_mmu_op_header header; |
| 57 | }; |
| 58 | |
| 59 | struct kvm_mmu_op_release_pt { |
| 60 | struct kvm_mmu_op_header header; |
| 61 | __u64 pt_phys; |
| 62 | }; |
| 63 | |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 64 | #ifdef __KERNEL__ |
| 65 | #include <asm/processor.h> |
| 66 | |
Glauber de Oliveira Costa | 1806852 | 2008-02-15 17:52:47 -0200 | [diff] [blame] | 67 | extern void kvmclock_init(void); |
| 68 | |
| 69 | |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 70 | /* 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 | |
| 85 | static inline long kvm_hypercall0(unsigned int nr) |
| 86 | { |
| 87 | long ret; |
| 88 | asm volatile(KVM_HYPERCALL |
| 89 | : "=a"(ret) |
Anthony Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 90 | : "a"(nr) |
| 91 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | static inline long kvm_hypercall1(unsigned int nr, unsigned long p1) |
| 96 | { |
| 97 | long ret; |
| 98 | asm volatile(KVM_HYPERCALL |
| 99 | : "=a"(ret) |
Anthony Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 100 | : "a"(nr), "b"(p1) |
| 101 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | static 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 Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 111 | : "a"(nr), "b"(p1), "c"(p2) |
| 112 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static 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 Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 122 | : "a"(nr), "b"(p1), "c"(p2), "d"(p3) |
| 123 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static 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 Liguori | ca37393 | 2008-07-03 19:02:36 +0300 | [diff] [blame] | 134 | : "a"(nr), "b"(p1), "c"(p2), "d"(p3), "S"(p4) |
| 135 | : "memory"); |
Christian Borntraeger | 5f43238 | 2007-10-11 15:34:17 +0200 | [diff] [blame] | 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static 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 | |
| 156 | static inline unsigned int kvm_arch_para_features(void) |
| 157 | { |
| 158 | return cpuid_eax(KVM_CPUID_FEATURES); |
| 159 | } |
| 160 | |
| 161 | #endif |
| 162 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 163 | #endif /* _ASM_X86_KVM_PARA_H */ |