Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Kernel-based Virtual Machine driver for Linux |
| 3 | * |
| 4 | * This module enables machines with Intel VT-x extensions to run virtual |
| 5 | * machines without emulation or binary translation. |
| 6 | * |
| 7 | * Copyright (C) 2006 Qumranet, Inc. |
| 8 | * |
| 9 | * Authors: |
| 10 | * Avi Kivity <avi@qumranet.com> |
| 11 | * Yaniv Kamay <yaniv@qumranet.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 14 | * the COPYING file in the top-level directory. |
| 15 | * |
| 16 | */ |
| 17 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 18 | #include "irq.h" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 19 | #include "vmx.h" |
Zhang Xiantao | 1d737c8 | 2007-12-14 09:35:10 +0800 | [diff] [blame] | 20 | #include "mmu.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 21 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 22 | #include <linux/kvm_host.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 24 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 25 | #include <linux/mm.h> |
| 26 | #include <linux/highmem.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 28 | #include <linux/moduleparam.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 29 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 30 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 31 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 33 | MODULE_AUTHOR("Qumranet"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 36 | static int bypass_guest_pf = 1; |
| 37 | module_param(bypass_guest_pf, bool, 0); |
| 38 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 39 | static int enable_vpid = 1; |
| 40 | module_param(enable_vpid, bool, 0); |
| 41 | |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 42 | static int flexpriority_enabled = 1; |
| 43 | module_param(flexpriority_enabled, bool, 0); |
| 44 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 45 | struct vmcs { |
| 46 | u32 revision_id; |
| 47 | u32 abort; |
| 48 | char data[0]; |
| 49 | }; |
| 50 | |
| 51 | struct vcpu_vmx { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 52 | struct kvm_vcpu vcpu; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 53 | int launched; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 54 | u8 fail; |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 55 | u32 idt_vectoring_info; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 56 | struct kvm_msr_entry *guest_msrs; |
| 57 | struct kvm_msr_entry *host_msrs; |
| 58 | int nmsrs; |
| 59 | int save_nmsrs; |
| 60 | int msr_offset_efer; |
| 61 | #ifdef CONFIG_X86_64 |
| 62 | int msr_offset_kernel_gs_base; |
| 63 | #endif |
| 64 | struct vmcs *vmcs; |
| 65 | struct { |
| 66 | int loaded; |
| 67 | u16 fs_sel, gs_sel, ldt_sel; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 68 | int gs_ldt_reload_needed; |
| 69 | int fs_reload_needed; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 70 | int guest_efer_loaded; |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 71 | } host_state; |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 72 | struct { |
| 73 | struct { |
| 74 | bool pending; |
| 75 | u8 vector; |
| 76 | unsigned rip; |
| 77 | } irq; |
| 78 | } rmode; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 79 | int vpid; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) |
| 83 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 84 | return container_of(vcpu, struct vcpu_vmx, vcpu); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 87 | static int init_rmode_tss(struct kvm *kvm); |
| 88 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 89 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 90 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
| 91 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 92 | static struct page *vmx_io_bitmap_a; |
| 93 | static struct page *vmx_io_bitmap_b; |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 94 | static struct page *vmx_msr_bitmap; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 95 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 96 | static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 97 | static DEFINE_SPINLOCK(vmx_vpid_lock); |
| 98 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 99 | static struct vmcs_config { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 100 | int size; |
| 101 | int order; |
| 102 | u32 revision_id; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 103 | u32 pin_based_exec_ctrl; |
| 104 | u32 cpu_based_exec_ctrl; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 105 | u32 cpu_based_2nd_exec_ctrl; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 106 | u32 vmexit_ctrl; |
| 107 | u32 vmentry_ctrl; |
| 108 | } vmcs_config; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 109 | |
| 110 | #define VMX_SEGMENT_FIELD(seg) \ |
| 111 | [VCPU_SREG_##seg] = { \ |
| 112 | .selector = GUEST_##seg##_SELECTOR, \ |
| 113 | .base = GUEST_##seg##_BASE, \ |
| 114 | .limit = GUEST_##seg##_LIMIT, \ |
| 115 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 116 | } |
| 117 | |
| 118 | static struct kvm_vmx_segment_field { |
| 119 | unsigned selector; |
| 120 | unsigned base; |
| 121 | unsigned limit; |
| 122 | unsigned ar_bytes; |
| 123 | } kvm_vmx_segment_fields[] = { |
| 124 | VMX_SEGMENT_FIELD(CS), |
| 125 | VMX_SEGMENT_FIELD(DS), |
| 126 | VMX_SEGMENT_FIELD(ES), |
| 127 | VMX_SEGMENT_FIELD(FS), |
| 128 | VMX_SEGMENT_FIELD(GS), |
| 129 | VMX_SEGMENT_FIELD(SS), |
| 130 | VMX_SEGMENT_FIELD(TR), |
| 131 | VMX_SEGMENT_FIELD(LDTR), |
| 132 | }; |
| 133 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 134 | /* |
| 135 | * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it |
| 136 | * away by decrementing the array size. |
| 137 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 138 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 139 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 140 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE, |
| 141 | #endif |
| 142 | MSR_EFER, MSR_K6_STAR, |
| 143 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 144 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 145 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 146 | static void load_msrs(struct kvm_msr_entry *e, int n) |
| 147 | { |
| 148 | int i; |
| 149 | |
| 150 | for (i = 0; i < n; ++i) |
| 151 | wrmsrl(e[i].index, e[i].data); |
| 152 | } |
| 153 | |
| 154 | static void save_msrs(struct kvm_msr_entry *e, int n) |
| 155 | { |
| 156 | int i; |
| 157 | |
| 158 | for (i = 0; i < n; ++i) |
| 159 | rdmsrl(e[i].index, e[i].data); |
| 160 | } |
| 161 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 162 | static inline int is_page_fault(u32 intr_info) |
| 163 | { |
| 164 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 165 | INTR_INFO_VALID_MASK)) == |
| 166 | (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
| 167 | } |
| 168 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 169 | static inline int is_no_device(u32 intr_info) |
| 170 | { |
| 171 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 172 | INTR_INFO_VALID_MASK)) == |
| 173 | (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
| 174 | } |
| 175 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 176 | static inline int is_invalid_opcode(u32 intr_info) |
| 177 | { |
| 178 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 179 | INTR_INFO_VALID_MASK)) == |
| 180 | (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK); |
| 181 | } |
| 182 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 183 | static inline int is_external_interrupt(u32 intr_info) |
| 184 | { |
| 185 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 186 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 187 | } |
| 188 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 189 | static inline int cpu_has_vmx_msr_bitmap(void) |
| 190 | { |
| 191 | return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS); |
| 192 | } |
| 193 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 194 | static inline int cpu_has_vmx_tpr_shadow(void) |
| 195 | { |
| 196 | return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW); |
| 197 | } |
| 198 | |
| 199 | static inline int vm_need_tpr_shadow(struct kvm *kvm) |
| 200 | { |
| 201 | return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm))); |
| 202 | } |
| 203 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 204 | static inline int cpu_has_secondary_exec_ctrls(void) |
| 205 | { |
| 206 | return (vmcs_config.cpu_based_exec_ctrl & |
| 207 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS); |
| 208 | } |
| 209 | |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 210 | static inline bool cpu_has_vmx_virtualize_apic_accesses(void) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 211 | { |
Avi Kivity | 4c9fc8e | 2008-03-24 18:15:14 +0200 | [diff] [blame] | 212 | return flexpriority_enabled |
| 213 | && (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 214 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm) |
| 218 | { |
| 219 | return ((cpu_has_vmx_virtualize_apic_accesses()) && |
| 220 | (irqchip_in_kernel(kvm))); |
| 221 | } |
| 222 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 223 | static inline int cpu_has_vmx_vpid(void) |
| 224 | { |
| 225 | return (vmcs_config.cpu_based_2nd_exec_ctrl & |
| 226 | SECONDARY_EXEC_ENABLE_VPID); |
| 227 | } |
| 228 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 229 | static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 230 | { |
| 231 | int i; |
| 232 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 233 | for (i = 0; i < vmx->nmsrs; ++i) |
| 234 | if (vmx->guest_msrs[i].index == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 235 | return i; |
| 236 | return -1; |
| 237 | } |
| 238 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 239 | static inline void __invvpid(int ext, u16 vpid, gva_t gva) |
| 240 | { |
| 241 | struct { |
| 242 | u64 vpid : 16; |
| 243 | u64 rsvd : 48; |
| 244 | u64 gva; |
| 245 | } operand = { vpid, 0, gva }; |
| 246 | |
| 247 | asm volatile (ASM_VMX_INVVPID |
| 248 | /* CF==1 or ZF==1 --> rc = -1 */ |
| 249 | "; ja 1f ; ud2 ; 1:" |
| 250 | : : "a"(&operand), "c"(ext) : "cc", "memory"); |
| 251 | } |
| 252 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 253 | static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 254 | { |
| 255 | int i; |
| 256 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 257 | i = __find_msr_index(vmx, msr); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 258 | if (i >= 0) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 259 | return &vmx->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 260 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 263 | static void vmcs_clear(struct vmcs *vmcs) |
| 264 | { |
| 265 | u64 phys_addr = __pa(vmcs); |
| 266 | u8 error; |
| 267 | |
| 268 | asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0" |
| 269 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 270 | : "cc", "memory"); |
| 271 | if (error) |
| 272 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 273 | vmcs, phys_addr); |
| 274 | } |
| 275 | |
| 276 | static void __vcpu_clear(void *arg) |
| 277 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 278 | struct vcpu_vmx *vmx = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 279 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 280 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 281 | if (vmx->vcpu.cpu == cpu) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 282 | vmcs_clear(vmx->vmcs); |
| 283 | if (per_cpu(current_vmcs, cpu) == vmx->vmcs) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 284 | per_cpu(current_vmcs, cpu) = NULL; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 285 | rdtscll(vmx->vcpu.arch.host_tsc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 288 | static void vcpu_clear(struct vcpu_vmx *vmx) |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 289 | { |
Avi Kivity | eae5ecb | 2007-09-30 10:50:12 +0200 | [diff] [blame] | 290 | if (vmx->vcpu.cpu == -1) |
| 291 | return; |
Avi Kivity | f566e09 | 2007-09-30 11:02:53 +0200 | [diff] [blame] | 292 | smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 0, 1); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 293 | vmx->launched = 0; |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 296 | static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx) |
| 297 | { |
| 298 | if (vmx->vpid == 0) |
| 299 | return; |
| 300 | |
| 301 | __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0); |
| 302 | } |
| 303 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 304 | static unsigned long vmcs_readl(unsigned long field) |
| 305 | { |
| 306 | unsigned long value; |
| 307 | |
| 308 | asm volatile (ASM_VMX_VMREAD_RDX_RAX |
| 309 | : "=a"(value) : "d"(field) : "cc"); |
| 310 | return value; |
| 311 | } |
| 312 | |
| 313 | static u16 vmcs_read16(unsigned long field) |
| 314 | { |
| 315 | return vmcs_readl(field); |
| 316 | } |
| 317 | |
| 318 | static u32 vmcs_read32(unsigned long field) |
| 319 | { |
| 320 | return vmcs_readl(field); |
| 321 | } |
| 322 | |
| 323 | static u64 vmcs_read64(unsigned long field) |
| 324 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 325 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 326 | return vmcs_readl(field); |
| 327 | #else |
| 328 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 329 | #endif |
| 330 | } |
| 331 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 332 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 333 | { |
| 334 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 335 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 336 | dump_stack(); |
| 337 | } |
| 338 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 339 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 340 | { |
| 341 | u8 error; |
| 342 | |
| 343 | asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0" |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 344 | : "=q"(error) : "a"(value), "d"(field) : "cc"); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 345 | if (unlikely(error)) |
| 346 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static void vmcs_write16(unsigned long field, u16 value) |
| 350 | { |
| 351 | vmcs_writel(field, value); |
| 352 | } |
| 353 | |
| 354 | static void vmcs_write32(unsigned long field, u32 value) |
| 355 | { |
| 356 | vmcs_writel(field, value); |
| 357 | } |
| 358 | |
| 359 | static void vmcs_write64(unsigned long field, u64 value) |
| 360 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 361 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 362 | vmcs_writel(field, value); |
| 363 | #else |
| 364 | vmcs_writel(field, value); |
| 365 | asm volatile (""); |
| 366 | vmcs_writel(field+1, value >> 32); |
| 367 | #endif |
| 368 | } |
| 369 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 370 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 371 | { |
| 372 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 373 | } |
| 374 | |
| 375 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 376 | { |
| 377 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 378 | } |
| 379 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 380 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 381 | { |
| 382 | u32 eb; |
| 383 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 384 | eb = (1u << PF_VECTOR) | (1u << UD_VECTOR); |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 385 | if (!vcpu->fpu_active) |
| 386 | eb |= 1u << NM_VECTOR; |
| 387 | if (vcpu->guest_debug.enabled) |
| 388 | eb |= 1u << 1; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 389 | if (vcpu->arch.rmode.active) |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 390 | eb = ~0; |
| 391 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 392 | } |
| 393 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 394 | static void reload_tss(void) |
| 395 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 396 | /* |
| 397 | * VT restores TR but not its size. Useless. |
| 398 | */ |
| 399 | struct descriptor_table gdt; |
Avi Kivity | a5f6130 | 2008-02-20 17:57:21 +0200 | [diff] [blame] | 400 | struct desc_struct *descs; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 401 | |
| 402 | get_gdt(&gdt); |
| 403 | descs = (void *)gdt.base; |
| 404 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 405 | load_TR_desc(); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 406 | } |
| 407 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 408 | static void load_transition_efer(struct vcpu_vmx *vmx) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 409 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 410 | int efer_offset = vmx->msr_offset_efer; |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 411 | u64 host_efer = vmx->host_msrs[efer_offset].data; |
| 412 | u64 guest_efer = vmx->guest_msrs[efer_offset].data; |
| 413 | u64 ignore_bits; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 414 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 415 | if (efer_offset < 0) |
| 416 | return; |
| 417 | /* |
| 418 | * NX is emulated; LMA and LME handled by hardware; SCE meaninless |
| 419 | * outside long mode |
| 420 | */ |
| 421 | ignore_bits = EFER_NX | EFER_SCE; |
| 422 | #ifdef CONFIG_X86_64 |
| 423 | ignore_bits |= EFER_LMA | EFER_LME; |
| 424 | /* SCE is meaningful only in long mode on Intel */ |
| 425 | if (guest_efer & EFER_LMA) |
| 426 | ignore_bits &= ~(u64)EFER_SCE; |
| 427 | #endif |
| 428 | if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits)) |
| 429 | return; |
| 430 | |
| 431 | vmx->host_state.guest_efer_loaded = 1; |
| 432 | guest_efer &= ~ignore_bits; |
| 433 | guest_efer |= host_efer & ignore_bits; |
| 434 | wrmsrl(MSR_EFER, guest_efer); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 435 | vmx->vcpu.stat.efer_reload++; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 436 | } |
| 437 | |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 438 | static void reload_host_efer(struct vcpu_vmx *vmx) |
| 439 | { |
| 440 | if (vmx->host_state.guest_efer_loaded) { |
| 441 | vmx->host_state.guest_efer_loaded = 0; |
| 442 | load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1); |
| 443 | } |
| 444 | } |
| 445 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 446 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 447 | { |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 448 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 449 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 450 | if (vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 451 | return; |
| 452 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 453 | vmx->host_state.loaded = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 454 | /* |
| 455 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 456 | * allow segment selectors with cpl > 0 or ti == 1. |
| 457 | */ |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 458 | vmx->host_state.ldt_sel = read_ldt(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 459 | vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 460 | vmx->host_state.fs_sel = read_fs(); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 461 | if (!(vmx->host_state.fs_sel & 7)) { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 462 | vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 463 | vmx->host_state.fs_reload_needed = 0; |
| 464 | } else { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 465 | vmcs_write16(HOST_FS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 466 | vmx->host_state.fs_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 467 | } |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 468 | vmx->host_state.gs_sel = read_gs(); |
| 469 | if (!(vmx->host_state.gs_sel & 7)) |
| 470 | vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 471 | else { |
| 472 | vmcs_write16(HOST_GS_SELECTOR, 0); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 473 | vmx->host_state.gs_ldt_reload_needed = 1; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | #ifdef CONFIG_X86_64 |
| 477 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 478 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 479 | #else |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 480 | vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel)); |
| 481 | vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel)); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 482 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 483 | |
| 484 | #ifdef CONFIG_X86_64 |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 485 | if (is_long_mode(&vmx->vcpu)) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 486 | save_msrs(vmx->host_msrs + |
| 487 | vmx->msr_offset_kernel_gs_base, 1); |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 488 | |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 489 | #endif |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 490 | load_msrs(vmx->guest_msrs, vmx->save_nmsrs); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 491 | load_transition_efer(vmx); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 492 | } |
| 493 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 494 | static void vmx_load_host_state(struct vcpu_vmx *vmx) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 495 | { |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 496 | unsigned long flags; |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 497 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 498 | if (!vmx->host_state.loaded) |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 499 | return; |
| 500 | |
Avi Kivity | e1beb1d | 2007-11-18 13:50:24 +0200 | [diff] [blame] | 501 | ++vmx->vcpu.stat.host_state_reload; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 502 | vmx->host_state.loaded = 0; |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 503 | if (vmx->host_state.fs_reload_needed) |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 504 | load_fs(vmx->host_state.fs_sel); |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 505 | if (vmx->host_state.gs_ldt_reload_needed) { |
| 506 | load_ldt(vmx->host_state.ldt_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 507 | /* |
| 508 | * If we have to reload gs, we must take care to |
| 509 | * preserve our gs base. |
| 510 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 511 | local_irq_save(flags); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 512 | load_gs(vmx->host_state.gs_sel); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 513 | #ifdef CONFIG_X86_64 |
| 514 | wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE)); |
| 515 | #endif |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 516 | local_irq_restore(flags); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 517 | } |
Laurent Vivier | 152d3f2 | 2007-08-23 16:33:11 +0200 | [diff] [blame] | 518 | reload_tss(); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 519 | save_msrs(vmx->guest_msrs, vmx->save_nmsrs); |
| 520 | load_msrs(vmx->host_msrs, vmx->save_nmsrs); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 521 | reload_host_efer(vmx); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 522 | } |
| 523 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 524 | /* |
| 525 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 526 | * vcpu mutex is already taken. |
| 527 | */ |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 528 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 529 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 530 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 531 | u64 phys_addr = __pa(vmx->vmcs); |
Avi Kivity | 019960a | 2008-03-04 10:44:51 +0200 | [diff] [blame] | 532 | u64 tsc_this, delta, new_offset; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 533 | |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 534 | if (vcpu->cpu != cpu) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 535 | vcpu_clear(vmx); |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 536 | kvm_migrate_apic_timer(vcpu); |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 537 | vpid_sync_vcpu_all(vmx); |
Eddie Dong | a3d7f85 | 2007-09-03 16:15:12 +0300 | [diff] [blame] | 538 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 539 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 540 | if (per_cpu(current_vmcs, cpu) != vmx->vmcs) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 541 | u8 error; |
| 542 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 543 | per_cpu(current_vmcs, cpu) = vmx->vmcs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 544 | asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0" |
| 545 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 546 | : "cc"); |
| 547 | if (error) |
| 548 | printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n", |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 549 | vmx->vmcs, phys_addr); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (vcpu->cpu != cpu) { |
| 553 | struct descriptor_table dt; |
| 554 | unsigned long sysenter_esp; |
| 555 | |
| 556 | vcpu->cpu = cpu; |
| 557 | /* |
| 558 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 559 | * processors. |
| 560 | */ |
| 561 | vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */ |
| 562 | get_gdt(&dt); |
| 563 | vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */ |
| 564 | |
| 565 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 566 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Avi Kivity | 7700270 | 2007-06-13 19:55:28 +0300 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * Make sure the time stamp counter is monotonous. |
| 570 | */ |
| 571 | rdtscll(tsc_this); |
Avi Kivity | 019960a | 2008-03-04 10:44:51 +0200 | [diff] [blame] | 572 | if (tsc_this < vcpu->arch.host_tsc) { |
| 573 | delta = vcpu->arch.host_tsc - tsc_this; |
| 574 | new_offset = vmcs_read64(TSC_OFFSET) + delta; |
| 575 | vmcs_write64(TSC_OFFSET, new_offset); |
| 576 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 577 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 581 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 582 | vmx_load_host_state(to_vmx(vcpu)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 585 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 586 | { |
| 587 | if (vcpu->fpu_active) |
| 588 | return; |
| 589 | vcpu->fpu_active = 1; |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 590 | vmcs_clear_bits(GUEST_CR0, X86_CR0_TS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 591 | if (vcpu->arch.cr0 & X86_CR0_TS) |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 592 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 593 | update_exception_bitmap(vcpu); |
| 594 | } |
| 595 | |
| 596 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 597 | { |
| 598 | if (!vcpu->fpu_active) |
| 599 | return; |
| 600 | vcpu->fpu_active = 0; |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 601 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 602 | update_exception_bitmap(vcpu); |
| 603 | } |
| 604 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 605 | static void vmx_vcpu_decache(struct kvm_vcpu *vcpu) |
| 606 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 607 | vcpu_clear(to_vmx(vcpu)); |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 610 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 611 | { |
| 612 | return vmcs_readl(GUEST_RFLAGS); |
| 613 | } |
| 614 | |
| 615 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 616 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 617 | if (vcpu->arch.rmode.active) |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 618 | rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 619 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 620 | } |
| 621 | |
| 622 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 623 | { |
| 624 | unsigned long rip; |
| 625 | u32 interruptibility; |
| 626 | |
| 627 | rip = vmcs_readl(GUEST_RIP); |
| 628 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 629 | vmcs_writel(GUEST_RIP, rip); |
| 630 | |
| 631 | /* |
| 632 | * We emulated an instruction, so temporary interrupt blocking |
| 633 | * should be removed, if set. |
| 634 | */ |
| 635 | interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 636 | if (interruptibility & 3) |
| 637 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 638 | interruptibility & ~3); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 639 | vcpu->arch.interrupt_window_open = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 642 | static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
| 643 | bool has_error_code, u32 error_code) |
| 644 | { |
| 645 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 646 | nr | INTR_TYPE_EXCEPTION |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 647 | | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0) |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 648 | | INTR_INFO_VALID_MASK); |
| 649 | if (has_error_code) |
| 650 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
| 651 | } |
| 652 | |
| 653 | static bool vmx_exception_injected(struct kvm_vcpu *vcpu) |
| 654 | { |
| 655 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 656 | |
| 657 | return !(vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK); |
| 658 | } |
| 659 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 660 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 661 | * Swap MSR entry in host/guest MSR entry array. |
| 662 | */ |
Gabriel C | 54e11fa | 2007-08-01 16:23:10 +0200 | [diff] [blame] | 663 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 664 | static void move_msr_up(struct vcpu_vmx *vmx, int from, int to) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 665 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 666 | struct kvm_msr_entry tmp; |
| 667 | |
| 668 | tmp = vmx->guest_msrs[to]; |
| 669 | vmx->guest_msrs[to] = vmx->guest_msrs[from]; |
| 670 | vmx->guest_msrs[from] = tmp; |
| 671 | tmp = vmx->host_msrs[to]; |
| 672 | vmx->host_msrs[to] = vmx->host_msrs[from]; |
| 673 | vmx->host_msrs[from] = tmp; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 674 | } |
Gabriel C | 54e11fa | 2007-08-01 16:23:10 +0200 | [diff] [blame] | 675 | #endif |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 676 | |
| 677 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 678 | * Set up the vmcs to automatically save and restore system |
| 679 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 680 | * mode, as fiddling with msrs is very expensive. |
| 681 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 682 | static void setup_msrs(struct vcpu_vmx *vmx) |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 683 | { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 684 | int save_nmsrs; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 685 | |
Avi Kivity | 33f9c50 | 2008-02-27 16:06:57 +0200 | [diff] [blame] | 686 | vmx_load_host_state(vmx); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 687 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 688 | #ifdef CONFIG_X86_64 |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 689 | if (is_long_mode(&vmx->vcpu)) { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 690 | int index; |
| 691 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 692 | index = __find_msr_index(vmx, MSR_SYSCALL_MASK); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 693 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 694 | move_msr_up(vmx, index, save_nmsrs++); |
| 695 | index = __find_msr_index(vmx, MSR_LSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 696 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 697 | move_msr_up(vmx, index, save_nmsrs++); |
| 698 | index = __find_msr_index(vmx, MSR_CSTAR); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 699 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 700 | move_msr_up(vmx, index, save_nmsrs++); |
| 701 | index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 702 | if (index >= 0) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 703 | move_msr_up(vmx, index, save_nmsrs++); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 704 | /* |
| 705 | * MSR_K6_STAR is only needed on long mode guests, and only |
| 706 | * if efer.sce is enabled. |
| 707 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 708 | index = __find_msr_index(vmx, MSR_K6_STAR); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 709 | if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE)) |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 710 | move_msr_up(vmx, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 711 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 712 | #endif |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 713 | vmx->save_nmsrs = save_nmsrs; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 714 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 715 | #ifdef CONFIG_X86_64 |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 716 | vmx->msr_offset_kernel_gs_base = |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 717 | __find_msr_index(vmx, MSR_KERNEL_GS_BASE); |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 718 | #endif |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 719 | vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 723 | * reads and returns guest's timestamp counter "register" |
| 724 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 725 | */ |
| 726 | static u64 guest_read_tsc(void) |
| 727 | { |
| 728 | u64 host_tsc, tsc_offset; |
| 729 | |
| 730 | rdtscll(host_tsc); |
| 731 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 732 | return host_tsc + tsc_offset; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * writes 'guest_tsc' into guest's timestamp counter "register" |
| 737 | * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc |
| 738 | */ |
| 739 | static void guest_write_tsc(u64 guest_tsc) |
| 740 | { |
| 741 | u64 host_tsc; |
| 742 | |
| 743 | rdtscll(host_tsc); |
| 744 | vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc); |
| 745 | } |
| 746 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 747 | /* |
| 748 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 749 | * Returns 0 on success, non-0 otherwise. |
| 750 | * Assumes vcpu_load() was already called. |
| 751 | */ |
| 752 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 753 | { |
| 754 | u64 data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 755 | struct kvm_msr_entry *msr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 756 | |
| 757 | if (!pdata) { |
| 758 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | |
| 762 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 763 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 764 | case MSR_FS_BASE: |
| 765 | data = vmcs_readl(GUEST_FS_BASE); |
| 766 | break; |
| 767 | case MSR_GS_BASE: |
| 768 | data = vmcs_readl(GUEST_GS_BASE); |
| 769 | break; |
| 770 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 771 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 772 | #endif |
| 773 | case MSR_IA32_TIME_STAMP_COUNTER: |
| 774 | data = guest_read_tsc(); |
| 775 | break; |
| 776 | case MSR_IA32_SYSENTER_CS: |
| 777 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 778 | break; |
| 779 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 780 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 781 | break; |
| 782 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 783 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 784 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 785 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 786 | msr = find_msr_entry(to_vmx(vcpu), msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 787 | if (msr) { |
| 788 | data = msr->data; |
| 789 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 790 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 791 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | *pdata = data; |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Writes msr value into into the appropriate "register". |
| 800 | * Returns 0 on success, non-0 otherwise. |
| 801 | * Assumes vcpu_load() was already called. |
| 802 | */ |
| 803 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 804 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 805 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 806 | struct kvm_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 807 | int ret = 0; |
| 808 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 809 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 810 | #ifdef CONFIG_X86_64 |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 811 | case MSR_EFER: |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 812 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 813 | if (vmx->host_state.loaded) { |
| 814 | reload_host_efer(vmx); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 815 | load_transition_efer(vmx); |
Avi Kivity | 51c6cf6 | 2007-08-29 03:48:05 +0300 | [diff] [blame] | 816 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 817 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 818 | case MSR_FS_BASE: |
| 819 | vmcs_writel(GUEST_FS_BASE, data); |
| 820 | break; |
| 821 | case MSR_GS_BASE: |
| 822 | vmcs_writel(GUEST_GS_BASE, data); |
| 823 | break; |
| 824 | #endif |
| 825 | case MSR_IA32_SYSENTER_CS: |
| 826 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 827 | break; |
| 828 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 829 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 830 | break; |
| 831 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 832 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 833 | break; |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 834 | case MSR_IA32_TIME_STAMP_COUNTER: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 835 | guest_write_tsc(data); |
| 836 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 837 | default: |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 838 | msr = find_msr_entry(vmx, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 839 | if (msr) { |
| 840 | msr->data = data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 841 | if (vmx->host_state.loaded) |
| 842 | load_msrs(vmx->guest_msrs, vmx->save_nmsrs); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 843 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 844 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 845 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 846 | } |
| 847 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 848 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
| 852 | * Sync the rsp and rip registers into the vcpu structure. This allows |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 853 | * registers to be accessed by indexing vcpu->arch.regs. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 854 | */ |
| 855 | static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu) |
| 856 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 857 | vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 858 | vcpu->arch.rip = vmcs_readl(GUEST_RIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* |
| 862 | * Syncs rsp and rip back into the vmcs. Should be called after possible |
| 863 | * modification. |
| 864 | */ |
| 865 | static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu) |
| 866 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 867 | vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]); |
| 868 | vmcs_writel(GUEST_RIP, vcpu->arch.rip); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) |
| 872 | { |
| 873 | unsigned long dr7 = 0x400; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 874 | int old_singlestep; |
| 875 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 876 | old_singlestep = vcpu->guest_debug.singlestep; |
| 877 | |
| 878 | vcpu->guest_debug.enabled = dbg->enabled; |
| 879 | if (vcpu->guest_debug.enabled) { |
| 880 | int i; |
| 881 | |
| 882 | dr7 |= 0x200; /* exact */ |
| 883 | for (i = 0; i < 4; ++i) { |
| 884 | if (!dbg->breakpoints[i].enabled) |
| 885 | continue; |
| 886 | vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address; |
| 887 | dr7 |= 2 << (i*2); /* global enable */ |
| 888 | dr7 |= 0 << (i*4+16); /* execution breakpoint */ |
| 889 | } |
| 890 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 891 | vcpu->guest_debug.singlestep = dbg->singlestep; |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 892 | } else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 893 | vcpu->guest_debug.singlestep = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 894 | |
| 895 | if (old_singlestep && !vcpu->guest_debug.singlestep) { |
| 896 | unsigned long flags; |
| 897 | |
| 898 | flags = vmcs_readl(GUEST_RFLAGS); |
| 899 | flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 900 | vmcs_writel(GUEST_RFLAGS, flags); |
| 901 | } |
| 902 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 903 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 904 | vmcs_writel(GUEST_DR7, dr7); |
| 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 909 | static int vmx_get_irq(struct kvm_vcpu *vcpu) |
| 910 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 911 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 912 | u32 idtv_info_field; |
| 913 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 914 | idtv_info_field = vmx->idt_vectoring_info; |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 915 | if (idtv_info_field & INTR_INFO_VALID_MASK) { |
| 916 | if (is_external_interrupt(idtv_info_field)) |
| 917 | return idtv_info_field & VECTORING_INFO_VECTOR_MASK; |
| 918 | else |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 919 | printk(KERN_DEBUG "pending exception: not handled yet\n"); |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 920 | } |
| 921 | return -1; |
| 922 | } |
| 923 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 924 | static __init int cpu_has_kvm_support(void) |
| 925 | { |
| 926 | unsigned long ecx = cpuid_ecx(1); |
| 927 | return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */ |
| 928 | } |
| 929 | |
| 930 | static __init int vmx_disabled_by_bios(void) |
| 931 | { |
| 932 | u64 msr; |
| 933 | |
| 934 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 935 | return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED | |
| 936 | MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED)) |
| 937 | == MSR_IA32_FEATURE_CONTROL_LOCKED; |
| 938 | /* locked but not enabled */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 939 | } |
| 940 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 941 | static void hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 942 | { |
| 943 | int cpu = raw_smp_processor_id(); |
| 944 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
| 945 | u64 old; |
| 946 | |
| 947 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 948 | if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED | |
| 949 | MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED)) |
| 950 | != (MSR_IA32_FEATURE_CONTROL_LOCKED | |
| 951 | MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 952 | /* enable and lock */ |
Yang, Sheng | 62b3ffb | 2007-07-25 12:17:06 +0300 | [diff] [blame] | 953 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | |
| 954 | MSR_IA32_FEATURE_CONTROL_LOCKED | |
| 955 | MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 956 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 957 | asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr) |
| 958 | : "memory", "cc"); |
| 959 | } |
| 960 | |
| 961 | static void hardware_disable(void *garbage) |
| 962 | { |
| 963 | asm volatile (ASM_VMX_VMXOFF : : : "cc"); |
| 964 | } |
| 965 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 966 | static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt, |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 967 | u32 msr, u32 *result) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 968 | { |
| 969 | u32 vmx_msr_low, vmx_msr_high; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 970 | u32 ctl = ctl_min | ctl_opt; |
| 971 | |
| 972 | rdmsr(msr, vmx_msr_low, vmx_msr_high); |
| 973 | |
| 974 | ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */ |
| 975 | ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */ |
| 976 | |
| 977 | /* Ensure minimum (required) set of control bits are supported. */ |
| 978 | if (ctl_min & ~ctl) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 979 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 980 | |
| 981 | *result = ctl; |
| 982 | return 0; |
| 983 | } |
| 984 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 985 | static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf) |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 986 | { |
| 987 | u32 vmx_msr_low, vmx_msr_high; |
| 988 | u32 min, opt; |
| 989 | u32 _pin_based_exec_control = 0; |
| 990 | u32 _cpu_based_exec_control = 0; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 991 | u32 _cpu_based_2nd_exec_control = 0; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 992 | u32 _vmexit_control = 0; |
| 993 | u32 _vmentry_control = 0; |
| 994 | |
| 995 | min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING; |
| 996 | opt = 0; |
| 997 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS, |
| 998 | &_pin_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 999 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1000 | |
| 1001 | min = CPU_BASED_HLT_EXITING | |
| 1002 | #ifdef CONFIG_X86_64 |
| 1003 | CPU_BASED_CR8_LOAD_EXITING | |
| 1004 | CPU_BASED_CR8_STORE_EXITING | |
| 1005 | #endif |
| 1006 | CPU_BASED_USE_IO_BITMAPS | |
| 1007 | CPU_BASED_MOV_DR_EXITING | |
| 1008 | CPU_BASED_USE_TSC_OFFSETING; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1009 | opt = CPU_BASED_TPR_SHADOW | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 1010 | CPU_BASED_USE_MSR_BITMAPS | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1011 | CPU_BASED_ACTIVATE_SECONDARY_CONTROLS; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1012 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS, |
| 1013 | &_cpu_based_exec_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1014 | return -EIO; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1015 | #ifdef CONFIG_X86_64 |
| 1016 | if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW)) |
| 1017 | _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING & |
| 1018 | ~CPU_BASED_CR8_STORE_EXITING; |
| 1019 | #endif |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1020 | if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) { |
| 1021 | min = 0; |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 1022 | opt = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1023 | SECONDARY_EXEC_WBINVD_EXITING | |
| 1024 | SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1025 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS2, |
| 1026 | &_cpu_based_2nd_exec_control) < 0) |
| 1027 | return -EIO; |
| 1028 | } |
| 1029 | #ifndef CONFIG_X86_64 |
| 1030 | if (!(_cpu_based_2nd_exec_control & |
| 1031 | SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)) |
| 1032 | _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 1033 | #endif |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1034 | |
| 1035 | min = 0; |
| 1036 | #ifdef CONFIG_X86_64 |
| 1037 | min |= VM_EXIT_HOST_ADDR_SPACE_SIZE; |
| 1038 | #endif |
| 1039 | opt = 0; |
| 1040 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS, |
| 1041 | &_vmexit_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1042 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1043 | |
| 1044 | min = opt = 0; |
| 1045 | if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS, |
| 1046 | &_vmentry_control) < 0) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1047 | return -EIO; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1048 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1049 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1050 | |
| 1051 | /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ |
| 1052 | if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1053 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1054 | |
| 1055 | #ifdef CONFIG_X86_64 |
| 1056 | /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ |
| 1057 | if (vmx_msr_high & (1u<<16)) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1058 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1059 | #endif |
| 1060 | |
| 1061 | /* Require Write-Back (WB) memory type for VMCS accesses. */ |
| 1062 | if (((vmx_msr_high >> 18) & 15) != 6) |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1063 | return -EIO; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1064 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1065 | vmcs_conf->size = vmx_msr_high & 0x1fff; |
| 1066 | vmcs_conf->order = get_order(vmcs_config.size); |
| 1067 | vmcs_conf->revision_id = vmx_msr_low; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1068 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1069 | vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; |
| 1070 | vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1071 | vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1072 | vmcs_conf->vmexit_ctrl = _vmexit_control; |
| 1073 | vmcs_conf->vmentry_ctrl = _vmentry_control; |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1074 | |
| 1075 | return 0; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1076 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1077 | |
| 1078 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 1079 | { |
| 1080 | int node = cpu_to_node(cpu); |
| 1081 | struct page *pages; |
| 1082 | struct vmcs *vmcs; |
| 1083 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1084 | pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1085 | if (!pages) |
| 1086 | return NULL; |
| 1087 | vmcs = page_address(pages); |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1088 | memset(vmcs, 0, vmcs_config.size); |
| 1089 | vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1090 | return vmcs; |
| 1091 | } |
| 1092 | |
| 1093 | static struct vmcs *alloc_vmcs(void) |
| 1094 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 1095 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static void free_vmcs(struct vmcs *vmcs) |
| 1099 | { |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1100 | free_pages((unsigned long)vmcs, vmcs_config.order); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 1103 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1104 | { |
| 1105 | int cpu; |
| 1106 | |
| 1107 | for_each_online_cpu(cpu) |
| 1108 | free_vmcs(per_cpu(vmxarea, cpu)); |
| 1109 | } |
| 1110 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1111 | static __init int alloc_kvm_area(void) |
| 1112 | { |
| 1113 | int cpu; |
| 1114 | |
| 1115 | for_each_online_cpu(cpu) { |
| 1116 | struct vmcs *vmcs; |
| 1117 | |
| 1118 | vmcs = alloc_vmcs_cpu(cpu); |
| 1119 | if (!vmcs) { |
| 1120 | free_kvm_area(); |
| 1121 | return -ENOMEM; |
| 1122 | } |
| 1123 | |
| 1124 | per_cpu(vmxarea, cpu) = vmcs; |
| 1125 | } |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | static __init int hardware_setup(void) |
| 1130 | { |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 1131 | if (setup_vmcs_config(&vmcs_config) < 0) |
| 1132 | return -EIO; |
Joerg Roedel | 50a37eb | 2008-01-31 14:57:38 +0100 | [diff] [blame] | 1133 | |
| 1134 | if (boot_cpu_has(X86_FEATURE_NX)) |
| 1135 | kvm_enable_efer_bits(EFER_NX); |
| 1136 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1137 | return alloc_kvm_area(); |
| 1138 | } |
| 1139 | |
| 1140 | static __exit void hardware_unsetup(void) |
| 1141 | { |
| 1142 | free_kvm_area(); |
| 1143 | } |
| 1144 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1145 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 1146 | { |
| 1147 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1148 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 1149 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1150 | vmcs_write16(sf->selector, save->selector); |
| 1151 | vmcs_writel(sf->base, save->base); |
| 1152 | vmcs_write32(sf->limit, save->limit); |
| 1153 | vmcs_write32(sf->ar_bytes, save->ar); |
| 1154 | } else { |
| 1155 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 1156 | << AR_DPL_SHIFT; |
| 1157 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 1162 | { |
| 1163 | unsigned long flags; |
| 1164 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1165 | vcpu->arch.rmode.active = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1166 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1167 | vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base); |
| 1168 | vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit); |
| 1169 | vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1170 | |
| 1171 | flags = vmcs_readl(GUEST_RFLAGS); |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1172 | flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1173 | flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1174 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1175 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1176 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 1177 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1178 | |
| 1179 | update_exception_bitmap(vcpu); |
| 1180 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1181 | fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es); |
| 1182 | fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds); |
| 1183 | fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs); |
| 1184 | fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1185 | |
| 1186 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 1187 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 1188 | |
| 1189 | vmcs_write16(GUEST_CS_SELECTOR, |
| 1190 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 1191 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 1192 | } |
| 1193 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1194 | static gva_t rmode_tss_base(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1195 | { |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1196 | if (!kvm->arch.tss_addr) { |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1197 | gfn_t base_gfn = kvm->memslots[0].base_gfn + |
| 1198 | kvm->memslots[0].npages - 3; |
| 1199 | return base_gfn << PAGE_SHIFT; |
| 1200 | } |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1201 | return kvm->arch.tss_addr; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 1205 | { |
| 1206 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1207 | |
| 1208 | save->selector = vmcs_read16(sf->selector); |
| 1209 | save->base = vmcs_readl(sf->base); |
| 1210 | save->limit = vmcs_read32(sf->limit); |
| 1211 | save->ar = vmcs_read32(sf->ar_bytes); |
Jan Kiszka | 15b00f3 | 2007-11-19 10:21:45 +0100 | [diff] [blame] | 1212 | vmcs_write16(sf->selector, save->base >> 4); |
| 1213 | vmcs_write32(sf->base, save->base & 0xfffff); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1214 | vmcs_write32(sf->limit, 0xffff); |
| 1215 | vmcs_write32(sf->ar_bytes, 0xf3); |
| 1216 | } |
| 1217 | |
| 1218 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 1219 | { |
| 1220 | unsigned long flags; |
| 1221 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1222 | vcpu->arch.rmode.active = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1223 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1224 | vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1225 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 1226 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1227 | vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1228 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 1229 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1230 | vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1231 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 1232 | |
| 1233 | flags = vmcs_readl(GUEST_RFLAGS); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1234 | vcpu->arch.rmode.save_iopl |
| 1235 | = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1236 | |
Glauber de Oliveira Costa | 053de04 | 2008-01-30 13:31:27 +0100 | [diff] [blame] | 1237 | flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1238 | |
| 1239 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 1240 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1241 | update_exception_bitmap(vcpu); |
| 1242 | |
| 1243 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 1244 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 1245 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 1246 | |
| 1247 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 1248 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 1249 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 1250 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1251 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 1252 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1253 | fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es); |
| 1254 | fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds); |
| 1255 | fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs); |
| 1256 | fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1257 | |
Eddie Dong | 8668a3c | 2007-10-10 14:26:45 +0800 | [diff] [blame] | 1258 | kvm_mmu_reset_context(vcpu); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 1259 | init_rmode_tss(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1262 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1263 | |
| 1264 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 1265 | { |
| 1266 | u32 guest_tr_ar; |
| 1267 | |
| 1268 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 1269 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
| 1270 | printk(KERN_DEBUG "%s: tss fixup for long mode. \n", |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1271 | __func__); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1272 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 1273 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 1274 | | AR_TYPE_BUSY_64_TSS); |
| 1275 | } |
| 1276 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1277 | vcpu->arch.shadow_efer |= EFER_LMA; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1278 | |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1279 | find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1280 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1281 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1282 | | VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 1286 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1287 | vcpu->arch.shadow_efer &= ~EFER_LMA; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1288 | |
| 1289 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1290 | vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1291 | & ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | #endif |
| 1295 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1296 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 1297 | { |
| 1298 | vpid_sync_vcpu_all(to_vmx(vcpu)); |
| 1299 | } |
| 1300 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1301 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1302 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1303 | vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK; |
| 1304 | vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK; |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1307 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 1308 | { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1309 | vmx_fpu_deactivate(vcpu); |
| 1310 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1311 | if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1312 | enter_pmode(vcpu); |
| 1313 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1314 | if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1315 | enter_rmode(vcpu); |
| 1316 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1317 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1318 | if (vcpu->arch.shadow_efer & EFER_LME) { |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1319 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1320 | enter_lmode(vcpu); |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1321 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1322 | exit_lmode(vcpu); |
| 1323 | } |
| 1324 | #endif |
| 1325 | |
| 1326 | vmcs_writel(CR0_READ_SHADOW, cr0); |
| 1327 | vmcs_writel(GUEST_CR0, |
| 1328 | (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1329 | vcpu->arch.cr0 = cr0; |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1330 | |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1331 | if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE)) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1332 | vmx_fpu_activate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1335 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 1336 | { |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1337 | vmx_flush_tlb(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1338 | vmcs_writel(GUEST_CR3, cr3); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1339 | if (vcpu->arch.cr0 & X86_CR0_PE) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1340 | vmx_fpu_deactivate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 1344 | { |
| 1345 | vmcs_writel(CR4_READ_SHADOW, cr4); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1346 | vmcs_writel(GUEST_CR4, cr4 | (vcpu->arch.rmode.active ? |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1347 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON)); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1348 | vcpu->arch.cr4 = cr4; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1351 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1352 | { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1353 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1354 | struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1355 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1356 | vcpu->arch.shadow_efer = efer; |
Joerg Roedel | 9f62e19 | 2008-01-31 14:57:39 +0100 | [diff] [blame] | 1357 | if (!msr) |
| 1358 | return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1359 | if (efer & EFER_LMA) { |
| 1360 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1361 | vmcs_read32(VM_ENTRY_CONTROLS) | |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1362 | VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1363 | msr->data = efer; |
| 1364 | |
| 1365 | } else { |
| 1366 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1367 | vmcs_read32(VM_ENTRY_CONTROLS) & |
Li, Xin B | 1e4e6e0 | 2007-08-01 21:49:10 +0300 | [diff] [blame] | 1368 | ~VM_ENTRY_IA32E_MODE); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1369 | |
| 1370 | msr->data = efer & ~EFER_LME; |
| 1371 | } |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1372 | setup_msrs(vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1375 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1376 | { |
| 1377 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1378 | |
| 1379 | return vmcs_readl(sf->base); |
| 1380 | } |
| 1381 | |
| 1382 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1383 | struct kvm_segment *var, int seg) |
| 1384 | { |
| 1385 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1386 | u32 ar; |
| 1387 | |
| 1388 | var->base = vmcs_readl(sf->base); |
| 1389 | var->limit = vmcs_read32(sf->limit); |
| 1390 | var->selector = vmcs_read16(sf->selector); |
| 1391 | ar = vmcs_read32(sf->ar_bytes); |
| 1392 | if (ar & AR_UNUSABLE_MASK) |
| 1393 | ar = 0; |
| 1394 | var->type = ar & 15; |
| 1395 | var->s = (ar >> 4) & 1; |
| 1396 | var->dpl = (ar >> 5) & 3; |
| 1397 | var->present = (ar >> 7) & 1; |
| 1398 | var->avl = (ar >> 12) & 1; |
| 1399 | var->l = (ar >> 13) & 1; |
| 1400 | var->db = (ar >> 14) & 1; |
| 1401 | var->g = (ar >> 15) & 1; |
| 1402 | var->unusable = (ar >> 16) & 1; |
| 1403 | } |
| 1404 | |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 1405 | static int vmx_get_cpl(struct kvm_vcpu *vcpu) |
| 1406 | { |
| 1407 | struct kvm_segment kvm_seg; |
| 1408 | |
| 1409 | if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */ |
| 1410 | return 0; |
| 1411 | |
| 1412 | if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */ |
| 1413 | return 3; |
| 1414 | |
| 1415 | vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS); |
| 1416 | return kvm_seg.selector & 3; |
| 1417 | } |
| 1418 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1419 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1420 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1421 | u32 ar; |
| 1422 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1423 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1424 | ar = 1 << 16; |
| 1425 | else { |
| 1426 | ar = var->type & 15; |
| 1427 | ar |= (var->s & 1) << 4; |
| 1428 | ar |= (var->dpl & 3) << 5; |
| 1429 | ar |= (var->present & 1) << 7; |
| 1430 | ar |= (var->avl & 1) << 12; |
| 1431 | ar |= (var->l & 1) << 13; |
| 1432 | ar |= (var->db & 1) << 14; |
| 1433 | ar |= (var->g & 1) << 15; |
| 1434 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 1435 | if (ar == 0) /* a 0 value means unusable */ |
| 1436 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1437 | |
| 1438 | return ar; |
| 1439 | } |
| 1440 | |
| 1441 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1442 | struct kvm_segment *var, int seg) |
| 1443 | { |
| 1444 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1445 | u32 ar; |
| 1446 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1447 | if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) { |
| 1448 | vcpu->arch.rmode.tr.selector = var->selector; |
| 1449 | vcpu->arch.rmode.tr.base = var->base; |
| 1450 | vcpu->arch.rmode.tr.limit = var->limit; |
| 1451 | vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var); |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1452 | return; |
| 1453 | } |
| 1454 | vmcs_writel(sf->base, var->base); |
| 1455 | vmcs_write32(sf->limit, var->limit); |
| 1456 | vmcs_write16(sf->selector, var->selector); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1457 | if (vcpu->arch.rmode.active && var->s) { |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1458 | /* |
| 1459 | * Hack real-mode segments into vm86 compatibility. |
| 1460 | */ |
| 1461 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 1462 | vmcs_writel(sf->base, 0xf0000); |
| 1463 | ar = 0xf3; |
| 1464 | } else |
| 1465 | ar = vmx_segment_access_rights(var); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1466 | vmcs_write32(sf->ar_bytes, ar); |
| 1467 | } |
| 1468 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1469 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 1470 | { |
| 1471 | u32 ar = vmcs_read32(GUEST_CS_AR_BYTES); |
| 1472 | |
| 1473 | *db = (ar >> 14) & 1; |
| 1474 | *l = (ar >> 13) & 1; |
| 1475 | } |
| 1476 | |
| 1477 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1478 | { |
| 1479 | dt->limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 1480 | dt->base = vmcs_readl(GUEST_IDTR_BASE); |
| 1481 | } |
| 1482 | |
| 1483 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1484 | { |
| 1485 | vmcs_write32(GUEST_IDTR_LIMIT, dt->limit); |
| 1486 | vmcs_writel(GUEST_IDTR_BASE, dt->base); |
| 1487 | } |
| 1488 | |
| 1489 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1490 | { |
| 1491 | dt->limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 1492 | dt->base = vmcs_readl(GUEST_GDTR_BASE); |
| 1493 | } |
| 1494 | |
| 1495 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1496 | { |
| 1497 | vmcs_write32(GUEST_GDTR_LIMIT, dt->limit); |
| 1498 | vmcs_writel(GUEST_GDTR_BASE, dt->base); |
| 1499 | } |
| 1500 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1501 | static int init_rmode_tss(struct kvm *kvm) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1502 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1503 | gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1504 | u16 data = 0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1505 | int ret = 0; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1506 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1507 | |
Marcelo Tosatti | 707a18a | 2008-03-18 17:42:34 -0300 | [diff] [blame] | 1508 | down_read(&kvm->slots_lock); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1509 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 1510 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1511 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1512 | data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
| 1513 | r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16)); |
| 1514 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1515 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1516 | r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE); |
| 1517 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1518 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1519 | r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE); |
| 1520 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1521 | goto out; |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1522 | data = ~0; |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1523 | r = kvm_write_guest_page(kvm, fn, &data, |
| 1524 | RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1, |
| 1525 | sizeof(u8)); |
Izik Eidus | 195aefd | 2007-10-01 22:14:18 +0200 | [diff] [blame] | 1526 | if (r < 0) |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1527 | goto out; |
| 1528 | |
| 1529 | ret = 1; |
| 1530 | out: |
Marcelo Tosatti | 707a18a | 2008-03-18 17:42:34 -0300 | [diff] [blame] | 1531 | up_read(&kvm->slots_lock); |
Marcelo Tosatti | 10589a4 | 2007-12-20 19:18:22 -0500 | [diff] [blame] | 1532 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1533 | } |
| 1534 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1535 | static void seg_setup(int seg) |
| 1536 | { |
| 1537 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1538 | |
| 1539 | vmcs_write16(sf->selector, 0); |
| 1540 | vmcs_writel(sf->base, 0); |
| 1541 | vmcs_write32(sf->limit, 0xffff); |
| 1542 | vmcs_write32(sf->ar_bytes, 0x93); |
| 1543 | } |
| 1544 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1545 | static int alloc_apic_access_page(struct kvm *kvm) |
| 1546 | { |
| 1547 | struct kvm_userspace_memory_region kvm_userspace_mem; |
| 1548 | int r = 0; |
| 1549 | |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1550 | down_write(&kvm->slots_lock); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1551 | if (kvm->arch.apic_access_page) |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1552 | goto out; |
| 1553 | kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT; |
| 1554 | kvm_userspace_mem.flags = 0; |
| 1555 | kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL; |
| 1556 | kvm_userspace_mem.memory_size = PAGE_SIZE; |
| 1557 | r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0); |
| 1558 | if (r) |
| 1559 | goto out; |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1560 | |
| 1561 | down_read(¤t->mm->mmap_sem); |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1562 | kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00); |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1563 | up_read(¤t->mm->mmap_sem); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1564 | out: |
Izik Eidus | 72dc67a | 2008-02-10 18:04:15 +0200 | [diff] [blame] | 1565 | up_write(&kvm->slots_lock); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1566 | return r; |
| 1567 | } |
| 1568 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1569 | static void allocate_vpid(struct vcpu_vmx *vmx) |
| 1570 | { |
| 1571 | int vpid; |
| 1572 | |
| 1573 | vmx->vpid = 0; |
| 1574 | if (!enable_vpid || !cpu_has_vmx_vpid()) |
| 1575 | return; |
| 1576 | spin_lock(&vmx_vpid_lock); |
| 1577 | vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS); |
| 1578 | if (vpid < VMX_NR_VPIDS) { |
| 1579 | vmx->vpid = vpid; |
| 1580 | __set_bit(vpid, vmx_vpid_bitmap); |
| 1581 | } |
| 1582 | spin_unlock(&vmx_vpid_lock); |
| 1583 | } |
| 1584 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 1585 | void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr) |
| 1586 | { |
| 1587 | void *va; |
| 1588 | |
| 1589 | if (!cpu_has_vmx_msr_bitmap()) |
| 1590 | return; |
| 1591 | |
| 1592 | /* |
| 1593 | * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals |
| 1594 | * have the write-low and read-high bitmap offsets the wrong way round. |
| 1595 | * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff. |
| 1596 | */ |
| 1597 | va = kmap(msr_bitmap); |
| 1598 | if (msr <= 0x1fff) { |
| 1599 | __clear_bit(msr, va + 0x000); /* read-low */ |
| 1600 | __clear_bit(msr, va + 0x800); /* write-low */ |
| 1601 | } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) { |
| 1602 | msr &= 0x1fff; |
| 1603 | __clear_bit(msr, va + 0x400); /* read-high */ |
| 1604 | __clear_bit(msr, va + 0xc00); /* write-high */ |
| 1605 | } |
| 1606 | kunmap(msr_bitmap); |
| 1607 | } |
| 1608 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1609 | /* |
| 1610 | * Sets up the vmcs for emulated real mode. |
| 1611 | */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1612 | static int vmx_vcpu_setup(struct vcpu_vmx *vmx) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1613 | { |
| 1614 | u32 host_sysenter_cs; |
| 1615 | u32 junk; |
| 1616 | unsigned long a; |
| 1617 | struct descriptor_table dt; |
| 1618 | int i; |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 1619 | unsigned long kvm_vmx_return; |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1620 | u32 exec_control; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1621 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1622 | /* I/O */ |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1623 | vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a)); |
| 1624 | vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1625 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 1626 | if (cpu_has_vmx_msr_bitmap()) |
| 1627 | vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap)); |
| 1628 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1629 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 1630 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1631 | /* Control */ |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1632 | vmcs_write32(PIN_BASED_VM_EXEC_CONTROL, |
| 1633 | vmcs_config.pin_based_exec_ctrl); |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 1634 | |
| 1635 | exec_control = vmcs_config.cpu_based_exec_ctrl; |
| 1636 | if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) { |
| 1637 | exec_control &= ~CPU_BASED_TPR_SHADOW; |
| 1638 | #ifdef CONFIG_X86_64 |
| 1639 | exec_control |= CPU_BASED_CR8_STORE_EXITING | |
| 1640 | CPU_BASED_CR8_LOAD_EXITING; |
| 1641 | #endif |
| 1642 | } |
| 1643 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1644 | |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 1645 | if (cpu_has_secondary_exec_ctrls()) { |
| 1646 | exec_control = vmcs_config.cpu_based_2nd_exec_ctrl; |
| 1647 | if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 1648 | exec_control &= |
| 1649 | ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES; |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1650 | if (vmx->vpid == 0) |
| 1651 | exec_control &= ~SECONDARY_EXEC_ENABLE_VPID; |
Sheng Yang | 83ff3b9 | 2007-11-21 14:33:25 +0800 | [diff] [blame] | 1652 | vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control); |
| 1653 | } |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1654 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 1655 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf); |
| 1656 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1657 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 1658 | |
| 1659 | vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */ |
| 1660 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 1661 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 1662 | |
| 1663 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
| 1664 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 1665 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 1666 | vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */ |
| 1667 | vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */ |
| 1668 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1669 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1670 | rdmsrl(MSR_FS_BASE, a); |
| 1671 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 1672 | rdmsrl(MSR_GS_BASE, a); |
| 1673 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 1674 | #else |
| 1675 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 1676 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 1677 | #endif |
| 1678 | |
| 1679 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 1680 | |
| 1681 | get_idt(&dt); |
| 1682 | vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */ |
| 1683 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1684 | asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return)); |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 1685 | vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */ |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1686 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 1687 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 1688 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1689 | |
| 1690 | rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk); |
| 1691 | vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs); |
| 1692 | rdmsrl(MSR_IA32_SYSENTER_ESP, a); |
| 1693 | vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */ |
| 1694 | rdmsrl(MSR_IA32_SYSENTER_EIP, a); |
| 1695 | vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */ |
| 1696 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1697 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 1698 | u32 index = vmx_msr_index[i]; |
| 1699 | u32 data_low, data_high; |
| 1700 | u64 data; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1701 | int j = vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1702 | |
| 1703 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 1704 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 1705 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 1706 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1707 | data = data_low | ((u64)data_high << 32); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 1708 | vmx->host_msrs[j].index = index; |
| 1709 | vmx->host_msrs[j].reserved = 0; |
| 1710 | vmx->host_msrs[j].data = data; |
| 1711 | vmx->guest_msrs[j] = vmx->host_msrs[j]; |
| 1712 | ++vmx->nmsrs; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1713 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1714 | |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1715 | vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1716 | |
| 1717 | /* 22.2.1, 20.8.1 */ |
Yang, Sheng | 1c3d14f | 2007-07-29 11:07:42 +0300 | [diff] [blame] | 1718 | vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl); |
| 1719 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 1720 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
| 1721 | vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK); |
| 1722 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1723 | |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 1724 | return 0; |
| 1725 | } |
| 1726 | |
| 1727 | static int vmx_vcpu_reset(struct kvm_vcpu *vcpu) |
| 1728 | { |
| 1729 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1730 | u64 msr; |
| 1731 | int ret; |
| 1732 | |
| 1733 | if (!init_rmode_tss(vmx->vcpu.kvm)) { |
| 1734 | ret = -ENOMEM; |
| 1735 | goto out; |
| 1736 | } |
| 1737 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1738 | vmx->vcpu.arch.rmode.active = 0; |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 1739 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1740 | vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 1741 | kvm_set_cr8(&vmx->vcpu, 0); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 1742 | msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
| 1743 | if (vmx->vcpu.vcpu_id == 0) |
| 1744 | msr |= MSR_IA32_APICBASE_BSP; |
| 1745 | kvm_set_apic_base(&vmx->vcpu, msr); |
| 1746 | |
| 1747 | fx_init(&vmx->vcpu); |
| 1748 | |
| 1749 | /* |
| 1750 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 1751 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 1752 | */ |
| 1753 | if (vmx->vcpu.vcpu_id == 0) { |
| 1754 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 1755 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 1756 | } else { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1757 | vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8); |
| 1758 | vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12); |
Avi Kivity | e00c8cf | 2007-10-21 11:00:39 +0200 | [diff] [blame] | 1759 | } |
| 1760 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
| 1761 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 1762 | |
| 1763 | seg_setup(VCPU_SREG_DS); |
| 1764 | seg_setup(VCPU_SREG_ES); |
| 1765 | seg_setup(VCPU_SREG_FS); |
| 1766 | seg_setup(VCPU_SREG_GS); |
| 1767 | seg_setup(VCPU_SREG_SS); |
| 1768 | |
| 1769 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 1770 | vmcs_writel(GUEST_TR_BASE, 0); |
| 1771 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 1772 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 1773 | |
| 1774 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 1775 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 1776 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 1777 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 1778 | |
| 1779 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 1780 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 1781 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 1782 | |
| 1783 | vmcs_writel(GUEST_RFLAGS, 0x02); |
| 1784 | if (vmx->vcpu.vcpu_id == 0) |
| 1785 | vmcs_writel(GUEST_RIP, 0xfff0); |
| 1786 | else |
| 1787 | vmcs_writel(GUEST_RIP, 0); |
| 1788 | vmcs_writel(GUEST_RSP, 0); |
| 1789 | |
| 1790 | /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */ |
| 1791 | vmcs_writel(GUEST_DR7, 0x400); |
| 1792 | |
| 1793 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 1794 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 1795 | |
| 1796 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 1797 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 1798 | |
| 1799 | vmcs_write32(GUEST_ACTIVITY_STATE, 0); |
| 1800 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 1801 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 1802 | |
| 1803 | guest_write_tsc(0); |
| 1804 | |
| 1805 | /* Special registers */ |
| 1806 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 1807 | |
| 1808 | setup_msrs(vmx); |
| 1809 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1810 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 1811 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1812 | if (cpu_has_vmx_tpr_shadow()) { |
| 1813 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 1814 | if (vm_need_tpr_shadow(vmx->vcpu.kvm)) |
| 1815 | vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1816 | page_to_phys(vmx->vcpu.arch.apic->regs_page)); |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 1817 | vmcs_write32(TPR_THRESHOLD, 0); |
| 1818 | } |
| 1819 | |
| 1820 | if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm)) |
| 1821 | vmcs_write64(APIC_ACCESS_ADDR, |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1822 | page_to_phys(vmx->vcpu.kvm->arch.apic_access_page)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1823 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1824 | if (vmx->vpid != 0) |
| 1825 | vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid); |
| 1826 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1827 | vmx->vcpu.arch.cr0 = 0x60000010; |
| 1828 | vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */ |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1829 | vmx_set_cr4(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1830 | vmx_set_efer(&vmx->vcpu, 0); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 1831 | vmx_fpu_activate(&vmx->vcpu); |
| 1832 | update_exception_bitmap(&vmx->vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1833 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 1834 | vpid_sync_vcpu_all(vmx); |
| 1835 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1836 | return 0; |
| 1837 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1838 | out: |
| 1839 | return ret; |
| 1840 | } |
| 1841 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1842 | static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq) |
| 1843 | { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 1844 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 1845 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1846 | if (vcpu->arch.rmode.active) { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 1847 | vmx->rmode.irq.pending = true; |
| 1848 | vmx->rmode.irq.vector = irq; |
| 1849 | vmx->rmode.irq.rip = vmcs_readl(GUEST_RIP); |
Avi Kivity | 9c5623e | 2007-11-08 18:19:20 +0200 | [diff] [blame] | 1850 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 1851 | irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK); |
| 1852 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1); |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 1853 | vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip - 1); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1854 | return; |
| 1855 | } |
| 1856 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 1857 | irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1858 | } |
| 1859 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1860 | static void kvm_do_inject_irq(struct kvm_vcpu *vcpu) |
| 1861 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1862 | int word_index = __ffs(vcpu->arch.irq_summary); |
| 1863 | int bit_index = __ffs(vcpu->arch.irq_pending[word_index]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1864 | int irq = word_index * BITS_PER_LONG + bit_index; |
| 1865 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1866 | clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]); |
| 1867 | if (!vcpu->arch.irq_pending[word_index]) |
| 1868 | clear_bit(word_index, &vcpu->arch.irq_summary); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1869 | vmx_inject_irq(vcpu, irq); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1872 | |
| 1873 | static void do_interrupt_requests(struct kvm_vcpu *vcpu, |
| 1874 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1875 | { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1876 | u32 cpu_based_vm_exec_control; |
| 1877 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1878 | vcpu->arch.interrupt_window_open = |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1879 | ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 1880 | (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0); |
| 1881 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1882 | if (vcpu->arch.interrupt_window_open && |
| 1883 | vcpu->arch.irq_summary && |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1884 | !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1885 | /* |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1886 | * If interrupts enabled, and not blocked by sti or mov ss. Good. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1887 | */ |
| 1888 | kvm_do_inject_irq(vcpu); |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1889 | |
| 1890 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1891 | if (!vcpu->arch.interrupt_window_open && |
| 1892 | (vcpu->arch.irq_summary || kvm_run->request_interrupt_window)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1893 | /* |
| 1894 | * Interrupts blocked. Wait for unblock. |
| 1895 | */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1896 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 1897 | else |
| 1898 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 1899 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1900 | } |
| 1901 | |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1902 | static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr) |
| 1903 | { |
| 1904 | int ret; |
| 1905 | struct kvm_userspace_memory_region tss_mem = { |
| 1906 | .slot = 8, |
| 1907 | .guest_phys_addr = addr, |
| 1908 | .memory_size = PAGE_SIZE * 3, |
| 1909 | .flags = 0, |
| 1910 | }; |
| 1911 | |
| 1912 | ret = kvm_set_memory_region(kvm, &tss_mem, 0); |
| 1913 | if (ret) |
| 1914 | return ret; |
Zhang Xiantao | bfc6d22 | 2007-12-14 10:20:16 +0800 | [diff] [blame] | 1915 | kvm->arch.tss_addr = addr; |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 1916 | return 0; |
| 1917 | } |
| 1918 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1919 | static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu) |
| 1920 | { |
| 1921 | struct kvm_guest_debug *dbg = &vcpu->guest_debug; |
| 1922 | |
| 1923 | set_debugreg(dbg->bp[0], 0); |
| 1924 | set_debugreg(dbg->bp[1], 1); |
| 1925 | set_debugreg(dbg->bp[2], 2); |
| 1926 | set_debugreg(dbg->bp[3], 3); |
| 1927 | |
| 1928 | if (dbg->singlestep) { |
| 1929 | unsigned long flags; |
| 1930 | |
| 1931 | flags = vmcs_readl(GUEST_RFLAGS); |
| 1932 | flags |= X86_EFLAGS_TF | X86_EFLAGS_RF; |
| 1933 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 1938 | int vec, u32 err_code) |
| 1939 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1940 | if (!vcpu->arch.rmode.active) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1941 | return 0; |
| 1942 | |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 1943 | /* |
| 1944 | * Instruction with address size override prefix opcode 0x67 |
| 1945 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 1946 | */ |
| 1947 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 1948 | if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1949 | return 1; |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
| 1953 | static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1954 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 1955 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1956 | u32 intr_info, error_code; |
| 1957 | unsigned long cr2, rip; |
| 1958 | u32 vect_info; |
| 1959 | enum emulation_result er; |
| 1960 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 1961 | vect_info = vmx->idt_vectoring_info; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1962 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 1963 | |
| 1964 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 1965 | !is_page_fault(intr_info)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1966 | printk(KERN_ERR "%s: unexpected, vectoring info 0x%x " |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 1967 | "intr info 0x%x\n", __func__, vect_info, intr_info); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1968 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 1969 | if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1970 | int irq = vect_info & VECTORING_INFO_VECTOR_MASK; |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1971 | set_bit(irq, vcpu->arch.irq_pending); |
| 1972 | set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1973 | } |
| 1974 | |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 1975 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */ |
| 1976 | return 1; /* already handled by vmx_vcpu_run() */ |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1977 | |
| 1978 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1979 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1980 | return 1; |
| 1981 | } |
| 1982 | |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1983 | if (is_invalid_opcode(intr_info)) { |
Sheng Yang | 571008d | 2008-01-02 14:49:22 +0800 | [diff] [blame] | 1984 | er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1985 | if (er != EMULATE_DONE) |
Avi Kivity | 7ee5d940 | 2007-11-25 15:22:50 +0200 | [diff] [blame] | 1986 | kvm_queue_exception(vcpu, UD_VECTOR); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 1987 | return 1; |
| 1988 | } |
| 1989 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1990 | error_code = 0; |
| 1991 | rip = vmcs_readl(GUEST_RIP); |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 1992 | if (intr_info & INTR_INFO_DELIVER_CODE_MASK) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1993 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 1994 | if (is_page_fault(intr_info)) { |
| 1995 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 3067714 | 2007-10-28 18:48:59 +0200 | [diff] [blame] | 1996 | return kvm_mmu_page_fault(vcpu, cr2, error_code); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1997 | } |
| 1998 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 1999 | if (vcpu->arch.rmode.active && |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2000 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2001 | error_code)) { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2002 | if (vcpu->arch.halt_request) { |
| 2003 | vcpu->arch.halt_request = 0; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2004 | return kvm_emulate_halt(vcpu); |
| 2005 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2006 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 2007 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2008 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2009 | if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == |
| 2010 | (INTR_TYPE_EXCEPTION | 1)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2011 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 2012 | return 0; |
| 2013 | } |
| 2014 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 2015 | kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK; |
| 2016 | kvm_run->ex.error_code = error_code; |
| 2017 | return 0; |
| 2018 | } |
| 2019 | |
| 2020 | static int handle_external_interrupt(struct kvm_vcpu *vcpu, |
| 2021 | struct kvm_run *kvm_run) |
| 2022 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2023 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2024 | return 1; |
| 2025 | } |
| 2026 | |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 2027 | static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2028 | { |
| 2029 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 2030 | return 0; |
| 2031 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2032 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2033 | static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2034 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2035 | unsigned long exit_qualification; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2036 | int size, down, in, string, rep; |
| 2037 | unsigned port; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2038 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2039 | ++vcpu->stat.io_exits; |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2040 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2041 | string = (exit_qualification & 16) != 0; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2042 | |
| 2043 | if (string) { |
Laurent Vivier | 3427318 | 2007-09-18 11:27:37 +0200 | [diff] [blame] | 2044 | if (emulate_instruction(vcpu, |
| 2045 | kvm_run, 0, 0, 0) == EMULATE_DO_MMIO) |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2046 | return 0; |
| 2047 | return 1; |
| 2048 | } |
| 2049 | |
| 2050 | size = (exit_qualification & 7) + 1; |
| 2051 | in = (exit_qualification & 8) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2052 | down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 2053 | rep = (exit_qualification & 32) != 0; |
| 2054 | port = exit_qualification >> 16; |
Laurent Vivier | e70669a | 2007-08-05 10:36:40 +0300 | [diff] [blame] | 2055 | |
Laurent Vivier | 3090dd7 | 2007-08-05 10:43:32 +0300 | [diff] [blame] | 2056 | return kvm_emulate_pio(vcpu, kvm_run, in, size, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2059 | static void |
| 2060 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 2061 | { |
| 2062 | /* |
| 2063 | * Patch in the VMCALL instruction: |
| 2064 | */ |
| 2065 | hypercall[0] = 0x0f; |
| 2066 | hypercall[1] = 0x01; |
| 2067 | hypercall[2] = 0xc1; |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2070 | static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2071 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2072 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2073 | int cr; |
| 2074 | int reg; |
| 2075 | |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2076 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2077 | cr = exit_qualification & 15; |
| 2078 | reg = (exit_qualification >> 8) & 15; |
| 2079 | switch ((exit_qualification >> 4) & 3) { |
| 2080 | case 0: /* mov to cr */ |
| 2081 | switch (cr) { |
| 2082 | case 0: |
| 2083 | vcpu_load_rsp_rip(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2084 | kvm_set_cr0(vcpu, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2085 | skip_emulated_instruction(vcpu); |
| 2086 | return 1; |
| 2087 | case 3: |
| 2088 | vcpu_load_rsp_rip(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2089 | kvm_set_cr3(vcpu, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2090 | skip_emulated_instruction(vcpu); |
| 2091 | return 1; |
| 2092 | case 4: |
| 2093 | vcpu_load_rsp_rip(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2094 | kvm_set_cr4(vcpu, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2095 | skip_emulated_instruction(vcpu); |
| 2096 | return 1; |
| 2097 | case 8: |
| 2098 | vcpu_load_rsp_rip(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2099 | kvm_set_cr8(vcpu, vcpu->arch.regs[reg]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2100 | skip_emulated_instruction(vcpu); |
Avi Kivity | e531406 | 2007-12-06 16:32:45 +0200 | [diff] [blame] | 2101 | if (irqchip_in_kernel(vcpu->kvm)) |
| 2102 | return 1; |
Yang, Sheng | 253abde | 2007-08-16 13:01:00 +0300 | [diff] [blame] | 2103 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
| 2104 | return 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2105 | }; |
| 2106 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2107 | case 2: /* clts */ |
| 2108 | vcpu_load_rsp_rip(vcpu); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2109 | vmx_fpu_deactivate(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2110 | vcpu->arch.cr0 &= ~X86_CR0_TS; |
| 2111 | vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 2112 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2113 | skip_emulated_instruction(vcpu); |
| 2114 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2115 | case 1: /*mov from cr*/ |
| 2116 | switch (cr) { |
| 2117 | case 3: |
| 2118 | vcpu_load_rsp_rip(vcpu); |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2119 | vcpu->arch.regs[reg] = vcpu->arch.cr3; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2120 | vcpu_put_rsp_rip(vcpu); |
| 2121 | skip_emulated_instruction(vcpu); |
| 2122 | return 1; |
| 2123 | case 8: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2124 | vcpu_load_rsp_rip(vcpu); |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2125 | vcpu->arch.regs[reg] = kvm_get_cr8(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2126 | vcpu_put_rsp_rip(vcpu); |
| 2127 | skip_emulated_instruction(vcpu); |
| 2128 | return 1; |
| 2129 | } |
| 2130 | break; |
| 2131 | case 3: /* lmsw */ |
Avi Kivity | 2d3ad1f | 2008-02-24 11:20:43 +0200 | [diff] [blame] | 2132 | kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2133 | |
| 2134 | skip_emulated_instruction(vcpu); |
| 2135 | return 1; |
| 2136 | default: |
| 2137 | break; |
| 2138 | } |
| 2139 | kvm_run->exit_reason = 0; |
Rusty Russell | f024247 | 2007-08-01 10:48:02 +1000 | [diff] [blame] | 2140 | pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n", |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2141 | (int)(exit_qualification >> 4) & 3, cr); |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2146 | { |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2147 | unsigned long exit_qualification; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2148 | unsigned long val; |
| 2149 | int dr, reg; |
| 2150 | |
| 2151 | /* |
| 2152 | * FIXME: this code assumes the host is debugging the guest. |
| 2153 | * need to deal with guest debugging itself too. |
| 2154 | */ |
He, Qing | bfdaab0 | 2007-09-12 14:18:28 +0800 | [diff] [blame] | 2155 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2156 | dr = exit_qualification & 7; |
| 2157 | reg = (exit_qualification >> 8) & 15; |
| 2158 | vcpu_load_rsp_rip(vcpu); |
| 2159 | if (exit_qualification & 16) { |
| 2160 | /* mov from dr */ |
| 2161 | switch (dr) { |
| 2162 | case 6: |
| 2163 | val = 0xffff0ff0; |
| 2164 | break; |
| 2165 | case 7: |
| 2166 | val = 0x400; |
| 2167 | break; |
| 2168 | default: |
| 2169 | val = 0; |
| 2170 | } |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2171 | vcpu->arch.regs[reg] = val; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2172 | } else { |
| 2173 | /* mov to dr */ |
| 2174 | } |
| 2175 | vcpu_put_rsp_rip(vcpu); |
| 2176 | skip_emulated_instruction(vcpu); |
| 2177 | return 1; |
| 2178 | } |
| 2179 | |
| 2180 | static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2181 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 2182 | kvm_emulate_cpuid(vcpu); |
| 2183 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2187 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2188 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2189 | u64 data; |
| 2190 | |
| 2191 | if (vmx_get_msr(vcpu, ecx, &data)) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2192 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2193 | return 1; |
| 2194 | } |
| 2195 | |
| 2196 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2197 | vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u; |
| 2198 | vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2199 | skip_emulated_instruction(vcpu); |
| 2200 | return 1; |
| 2201 | } |
| 2202 | |
| 2203 | static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2204 | { |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2205 | u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX]; |
| 2206 | u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u) |
| 2207 | | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2208 | |
| 2209 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
Avi Kivity | c1a5d4f | 2007-11-25 14:12:03 +0200 | [diff] [blame] | 2210 | kvm_inject_gp(vcpu, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2211 | return 1; |
| 2212 | } |
| 2213 | |
| 2214 | skip_emulated_instruction(vcpu); |
| 2215 | return 1; |
| 2216 | } |
| 2217 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2218 | static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu, |
| 2219 | struct kvm_run *kvm_run) |
| 2220 | { |
| 2221 | return 1; |
| 2222 | } |
| 2223 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2224 | static int handle_interrupt_window(struct kvm_vcpu *vcpu, |
| 2225 | struct kvm_run *kvm_run) |
| 2226 | { |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2227 | u32 cpu_based_vm_exec_control; |
| 2228 | |
| 2229 | /* clear pending irq */ |
| 2230 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2231 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2232 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2233 | /* |
| 2234 | * If the user space waits to inject interrupts, exit as soon as |
| 2235 | * possible |
| 2236 | */ |
| 2237 | if (kvm_run->request_interrupt_window && |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2238 | !vcpu->arch.irq_summary) { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2239 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2240 | ++vcpu->stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2241 | return 0; |
| 2242 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2243 | return 1; |
| 2244 | } |
| 2245 | |
| 2246 | static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2247 | { |
| 2248 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 2249 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2250 | } |
| 2251 | |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2252 | static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2253 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 2254 | skip_emulated_instruction(vcpu); |
Anthony Liguori | 7aa81cc | 2007-09-17 14:57:50 -0500 | [diff] [blame] | 2255 | kvm_emulate_hypercall(vcpu); |
| 2256 | return 1; |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 2259 | static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2260 | { |
| 2261 | skip_emulated_instruction(vcpu); |
| 2262 | /* TODO: Add support for VT-d/pass-through device */ |
| 2263 | return 1; |
| 2264 | } |
| 2265 | |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2266 | static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2267 | { |
| 2268 | u64 exit_qualification; |
| 2269 | enum emulation_result er; |
| 2270 | unsigned long offset; |
| 2271 | |
| 2272 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 2273 | offset = exit_qualification & 0xffful; |
| 2274 | |
| 2275 | er = emulate_instruction(vcpu, kvm_run, 0, 0, 0); |
| 2276 | |
| 2277 | if (er != EMULATE_DONE) { |
| 2278 | printk(KERN_ERR |
| 2279 | "Fail to handle apic access vmexit! Offset is 0x%lx\n", |
| 2280 | offset); |
| 2281 | return -ENOTSUPP; |
| 2282 | } |
| 2283 | return 1; |
| 2284 | } |
| 2285 | |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2286 | static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 2287 | { |
| 2288 | unsigned long exit_qualification; |
| 2289 | u16 tss_selector; |
| 2290 | int reason; |
| 2291 | |
| 2292 | exit_qualification = vmcs_readl(EXIT_QUALIFICATION); |
| 2293 | |
| 2294 | reason = (u32)exit_qualification >> 30; |
| 2295 | tss_selector = exit_qualification; |
| 2296 | |
| 2297 | return kvm_task_switch(vcpu, tss_selector, reason); |
| 2298 | } |
| 2299 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2300 | /* |
| 2301 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 2302 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 2303 | * to be done to userspace and return 0. |
| 2304 | */ |
| 2305 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu, |
| 2306 | struct kvm_run *kvm_run) = { |
| 2307 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 2308 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 2309 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2310 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2311 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 2312 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 2313 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 2314 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 2315 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 2316 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 2317 | [EXIT_REASON_HLT] = handle_halt, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2318 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Sheng Yang | f78e0e2 | 2007-10-29 09:40:42 +0800 | [diff] [blame] | 2319 | [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold, |
| 2320 | [EXIT_REASON_APIC_ACCESS] = handle_apic_access, |
Eddie Dong | e5edaa0 | 2007-11-11 12:28:35 +0200 | [diff] [blame] | 2321 | [EXIT_REASON_WBINVD] = handle_wbinvd, |
Izik Eidus | 37817f2 | 2008-03-24 23:14:53 +0200 | [diff] [blame] | 2322 | [EXIT_REASON_TASK_SWITCH] = handle_task_switch, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2323 | }; |
| 2324 | |
| 2325 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 2326 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2327 | |
| 2328 | /* |
| 2329 | * The guest has exited. See if we can fix it or if we need userspace |
| 2330 | * assistance. |
| 2331 | */ |
| 2332 | static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 2333 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2334 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 2335 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2336 | u32 vectoring_info = vmx->idt_vectoring_info; |
Avi Kivity | 29bd8a7 | 2007-09-10 17:27:03 +0300 | [diff] [blame] | 2337 | |
| 2338 | if (unlikely(vmx->fail)) { |
| 2339 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 2340 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 2341 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
| 2342 | return 0; |
| 2343 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2344 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2345 | if ((vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 2346 | exit_reason != EXIT_REASON_EXCEPTION_NMI) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2347 | printk(KERN_WARNING "%s: unexpected, valid vectoring info and " |
Harvey Harrison | b8688d5 | 2008-03-03 12:59:56 -0800 | [diff] [blame] | 2348 | "exit reason is 0x%x\n", __func__, exit_reason); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2349 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 2350 | && kvm_vmx_exit_handlers[exit_reason]) |
| 2351 | return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run); |
| 2352 | else { |
| 2353 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 2354 | kvm_run->hw.hardware_exit_reason = exit_reason; |
| 2355 | } |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2359 | static void update_tpr_threshold(struct kvm_vcpu *vcpu) |
| 2360 | { |
| 2361 | int max_irr, tpr; |
| 2362 | |
| 2363 | if (!vm_need_tpr_shadow(vcpu->kvm)) |
| 2364 | return; |
| 2365 | |
| 2366 | if (!kvm_lapic_enabled(vcpu) || |
| 2367 | ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) { |
| 2368 | vmcs_write32(TPR_THRESHOLD, 0); |
| 2369 | return; |
| 2370 | } |
| 2371 | |
| 2372 | tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4; |
| 2373 | vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4); |
| 2374 | } |
| 2375 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2376 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
| 2377 | { |
| 2378 | u32 cpu_based_vm_exec_control; |
| 2379 | |
| 2380 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 2381 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 2382 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
| 2383 | } |
| 2384 | |
| 2385 | static void vmx_intr_assist(struct kvm_vcpu *vcpu) |
| 2386 | { |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2387 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2388 | u32 idtv_info_field, intr_info_field; |
| 2389 | int has_ext_irq, interrupt_window_open; |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2390 | int vector; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2391 | |
Yang, Sheng | 6e5d865 | 2007-09-12 18:03:11 +0800 | [diff] [blame] | 2392 | update_tpr_threshold(vcpu); |
| 2393 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2394 | has_ext_irq = kvm_cpu_has_interrupt(vcpu); |
| 2395 | intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD); |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2396 | idtv_info_field = vmx->idt_vectoring_info; |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2397 | if (intr_info_field & INTR_INFO_VALID_MASK) { |
| 2398 | if (idtv_info_field & INTR_INFO_VALID_MASK) { |
| 2399 | /* TODO: fault when IDT_Vectoring */ |
Ryan Harper | 9584bf2 | 2007-12-13 10:21:10 -0600 | [diff] [blame] | 2400 | if (printk_ratelimit()) |
| 2401 | printk(KERN_ERR "Fault when IDT_Vectoring\n"); |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2402 | } |
| 2403 | if (has_ext_irq) |
| 2404 | enable_irq_window(vcpu); |
| 2405 | return; |
| 2406 | } |
| 2407 | if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2408 | if ((idtv_info_field & VECTORING_INFO_TYPE_MASK) |
| 2409 | == INTR_TYPE_EXT_INTR |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2410 | && vcpu->arch.rmode.active) { |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2411 | u8 vect = idtv_info_field & VECTORING_INFO_VECTOR_MASK; |
| 2412 | |
| 2413 | vmx_inject_irq(vcpu, vect); |
| 2414 | if (unlikely(has_ext_irq)) |
| 2415 | enable_irq_window(vcpu); |
| 2416 | return; |
| 2417 | } |
| 2418 | |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2419 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field); |
| 2420 | vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, |
| 2421 | vmcs_read32(VM_EXIT_INSTRUCTION_LEN)); |
| 2422 | |
Ryan Harper | 2e11384 | 2008-02-11 10:26:38 -0600 | [diff] [blame] | 2423 | if (unlikely(idtv_info_field & INTR_INFO_DELIVER_CODE_MASK)) |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2424 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, |
| 2425 | vmcs_read32(IDT_VECTORING_ERROR_CODE)); |
| 2426 | if (unlikely(has_ext_irq)) |
| 2427 | enable_irq_window(vcpu); |
| 2428 | return; |
| 2429 | } |
| 2430 | if (!has_ext_irq) |
| 2431 | return; |
| 2432 | interrupt_window_open = |
| 2433 | ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 2434 | (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0); |
Eddie Dong | 1b9778d | 2007-09-03 16:56:58 +0300 | [diff] [blame] | 2435 | if (interrupt_window_open) { |
| 2436 | vector = kvm_cpu_get_interrupt(vcpu); |
| 2437 | vmx_inject_irq(vcpu, vector); |
| 2438 | kvm_timer_intr_post(vcpu, vector); |
| 2439 | } else |
Eddie Dong | 85f455f | 2007-07-06 12:20:49 +0300 | [diff] [blame] | 2440 | enable_irq_window(vcpu); |
| 2441 | } |
| 2442 | |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2443 | /* |
| 2444 | * Failure to inject an interrupt should give us the information |
| 2445 | * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs |
| 2446 | * when fetching the interrupt redirection bitmap in the real-mode |
| 2447 | * tss, this doesn't happen. So we do it ourselves. |
| 2448 | */ |
| 2449 | static void fixup_rmode_irq(struct vcpu_vmx *vmx) |
| 2450 | { |
| 2451 | vmx->rmode.irq.pending = 0; |
| 2452 | if (vmcs_readl(GUEST_RIP) + 1 != vmx->rmode.irq.rip) |
| 2453 | return; |
| 2454 | vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip); |
| 2455 | if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) { |
| 2456 | vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK; |
| 2457 | vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR; |
| 2458 | return; |
| 2459 | } |
| 2460 | vmx->idt_vectoring_info = |
| 2461 | VECTORING_INFO_VALID_MASK |
| 2462 | | INTR_TYPE_EXT_INTR |
| 2463 | | vmx->rmode.irq.vector; |
| 2464 | } |
| 2465 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2466 | static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2467 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2468 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 2469 | u32 intr_info; |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 2470 | |
| 2471 | /* |
| 2472 | * Loading guest fpu may have cleared host cr0.ts |
| 2473 | */ |
| 2474 | vmcs_writel(HOST_CR0, read_cr0()); |
| 2475 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2476 | asm( |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2477 | /* Store host registers */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2478 | #ifdef CONFIG_X86_64 |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 2479 | "push %%rdx; push %%rbp;" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2480 | "push %%rcx \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2481 | #else |
Laurent Vivier | ff593e5 | 2007-10-25 14:18:55 +0200 | [diff] [blame] | 2482 | "push %%edx; push %%ebp;" |
| 2483 | "push %%ecx \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2484 | #endif |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 2485 | ASM_VMX_VMWRITE_RSP_RDX "\n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2486 | /* Check if vmlaunch of vmresume is needed */ |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2487 | "cmpl $0, %c[launched](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2488 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2489 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2490 | "mov %c[cr2](%0), %%rax \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2491 | "mov %%rax, %%cr2 \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2492 | "mov %c[rax](%0), %%rax \n\t" |
| 2493 | "mov %c[rbx](%0), %%rbx \n\t" |
| 2494 | "mov %c[rdx](%0), %%rdx \n\t" |
| 2495 | "mov %c[rsi](%0), %%rsi \n\t" |
| 2496 | "mov %c[rdi](%0), %%rdi \n\t" |
| 2497 | "mov %c[rbp](%0), %%rbp \n\t" |
| 2498 | "mov %c[r8](%0), %%r8 \n\t" |
| 2499 | "mov %c[r9](%0), %%r9 \n\t" |
| 2500 | "mov %c[r10](%0), %%r10 \n\t" |
| 2501 | "mov %c[r11](%0), %%r11 \n\t" |
| 2502 | "mov %c[r12](%0), %%r12 \n\t" |
| 2503 | "mov %c[r13](%0), %%r13 \n\t" |
| 2504 | "mov %c[r14](%0), %%r14 \n\t" |
| 2505 | "mov %c[r15](%0), %%r15 \n\t" |
| 2506 | "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2507 | #else |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2508 | "mov %c[cr2](%0), %%eax \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2509 | "mov %%eax, %%cr2 \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2510 | "mov %c[rax](%0), %%eax \n\t" |
| 2511 | "mov %c[rbx](%0), %%ebx \n\t" |
| 2512 | "mov %c[rdx](%0), %%edx \n\t" |
| 2513 | "mov %c[rsi](%0), %%esi \n\t" |
| 2514 | "mov %c[rdi](%0), %%edi \n\t" |
| 2515 | "mov %c[rbp](%0), %%ebp \n\t" |
| 2516 | "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2517 | #endif |
| 2518 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2519 | "jne .Llaunched \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2520 | ASM_VMX_VMLAUNCH "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2521 | "jmp .Lkvm_vmx_return \n\t" |
| 2522 | ".Llaunched: " ASM_VMX_VMRESUME "\n\t" |
| 2523 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2524 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2525 | #ifdef CONFIG_X86_64 |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2526 | "xchg %0, (%%rsp) \n\t" |
| 2527 | "mov %%rax, %c[rax](%0) \n\t" |
| 2528 | "mov %%rbx, %c[rbx](%0) \n\t" |
| 2529 | "pushq (%%rsp); popq %c[rcx](%0) \n\t" |
| 2530 | "mov %%rdx, %c[rdx](%0) \n\t" |
| 2531 | "mov %%rsi, %c[rsi](%0) \n\t" |
| 2532 | "mov %%rdi, %c[rdi](%0) \n\t" |
| 2533 | "mov %%rbp, %c[rbp](%0) \n\t" |
| 2534 | "mov %%r8, %c[r8](%0) \n\t" |
| 2535 | "mov %%r9, %c[r9](%0) \n\t" |
| 2536 | "mov %%r10, %c[r10](%0) \n\t" |
| 2537 | "mov %%r11, %c[r11](%0) \n\t" |
| 2538 | "mov %%r12, %c[r12](%0) \n\t" |
| 2539 | "mov %%r13, %c[r13](%0) \n\t" |
| 2540 | "mov %%r14, %c[r14](%0) \n\t" |
| 2541 | "mov %%r15, %c[r15](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2542 | "mov %%cr2, %%rax \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2543 | "mov %%rax, %c[cr2](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2544 | |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2545 | "pop %%rbp; pop %%rbp; pop %%rdx \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2546 | #else |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2547 | "xchg %0, (%%esp) \n\t" |
| 2548 | "mov %%eax, %c[rax](%0) \n\t" |
| 2549 | "mov %%ebx, %c[rbx](%0) \n\t" |
| 2550 | "pushl (%%esp); popl %c[rcx](%0) \n\t" |
| 2551 | "mov %%edx, %c[rdx](%0) \n\t" |
| 2552 | "mov %%esi, %c[rsi](%0) \n\t" |
| 2553 | "mov %%edi, %c[rdi](%0) \n\t" |
| 2554 | "mov %%ebp, %c[rbp](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2555 | "mov %%cr2, %%eax \n\t" |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2556 | "mov %%eax, %c[cr2](%0) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2557 | |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2558 | "pop %%ebp; pop %%ebp; pop %%edx \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2559 | #endif |
Avi Kivity | e08aa78 | 2007-11-15 18:06:18 +0200 | [diff] [blame] | 2560 | "setbe %c[fail](%0) \n\t" |
| 2561 | : : "c"(vmx), "d"((unsigned long)HOST_RSP), |
| 2562 | [launched]"i"(offsetof(struct vcpu_vmx, launched)), |
| 2563 | [fail]"i"(offsetof(struct vcpu_vmx, fail)), |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2564 | [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])), |
| 2565 | [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])), |
| 2566 | [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])), |
| 2567 | [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])), |
| 2568 | [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])), |
| 2569 | [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])), |
| 2570 | [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2571 | #ifdef CONFIG_X86_64 |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2572 | [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])), |
| 2573 | [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])), |
| 2574 | [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])), |
| 2575 | [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])), |
| 2576 | [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])), |
| 2577 | [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])), |
| 2578 | [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])), |
| 2579 | [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])), |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2580 | #endif |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2581 | [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)) |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 2582 | : "cc", "memory" |
| 2583 | #ifdef CONFIG_X86_64 |
| 2584 | , "rbx", "rdi", "rsi" |
| 2585 | , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
Laurent Vivier | ff593e5 | 2007-10-25 14:18:55 +0200 | [diff] [blame] | 2586 | #else |
| 2587 | , "ebx", "edi", "rsi" |
Laurent Vivier | c203630 | 2007-10-25 14:18:52 +0200 | [diff] [blame] | 2588 | #endif |
| 2589 | ); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2590 | |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2591 | vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
Avi Kivity | 9c8cba3 | 2007-11-22 11:42:59 +0200 | [diff] [blame] | 2592 | if (vmx->rmode.irq.pending) |
| 2593 | fixup_rmode_irq(vmx); |
Avi Kivity | 1155f76 | 2007-11-22 11:30:47 +0200 | [diff] [blame] | 2594 | |
Zhang Xiantao | ad312c7 | 2007-12-13 23:50:52 +0800 | [diff] [blame] | 2595 | vcpu->arch.interrupt_window_open = |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2596 | (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2597 | |
Mike Day | d77c26f | 2007-10-08 09:02:08 -0400 | [diff] [blame] | 2598 | asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2599 | vmx->launched = 1; |
Avi Kivity | 1b6269d | 2007-10-09 12:12:19 +0200 | [diff] [blame] | 2600 | |
| 2601 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 2602 | |
| 2603 | /* We need to handle NMIs before interrupts are enabled */ |
| 2604 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */ |
| 2605 | asm("int $2"); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2606 | } |
| 2607 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2608 | static void vmx_free_vmcs(struct kvm_vcpu *vcpu) |
| 2609 | { |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2610 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2611 | |
| 2612 | if (vmx->vmcs) { |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2613 | on_each_cpu(__vcpu_clear, vmx, 0, 1); |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2614 | free_vmcs(vmx->vmcs); |
| 2615 | vmx->vmcs = NULL; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 2620 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2621 | struct vcpu_vmx *vmx = to_vmx(vcpu); |
| 2622 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2623 | spin_lock(&vmx_vpid_lock); |
| 2624 | if (vmx->vpid != 0) |
| 2625 | __clear_bit(vmx->vpid, vmx_vpid_bitmap); |
| 2626 | spin_unlock(&vmx_vpid_lock); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2627 | vmx_free_vmcs(vcpu); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2628 | kfree(vmx->host_msrs); |
| 2629 | kfree(vmx->guest_msrs); |
| 2630 | kvm_vcpu_uninit(vcpu); |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 2631 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2632 | } |
| 2633 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2634 | static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2635 | { |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2636 | int err; |
Rusty Russell | c16f862 | 2007-07-30 21:12:19 +1000 | [diff] [blame] | 2637 | struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2638 | int cpu; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2639 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2640 | if (!vmx) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2641 | return ERR_PTR(-ENOMEM); |
| 2642 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2643 | allocate_vpid(vmx); |
| 2644 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2645 | err = kvm_vcpu_init(&vmx->vcpu, kvm, id); |
| 2646 | if (err) |
| 2647 | goto free_vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2648 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2649 | vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2650 | if (!vmx->guest_msrs) { |
| 2651 | err = -ENOMEM; |
| 2652 | goto uninit_vcpu; |
| 2653 | } |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2654 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2655 | vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2656 | if (!vmx->host_msrs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2657 | goto free_guest_msrs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2658 | |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2659 | vmx->vmcs = alloc_vmcs(); |
| 2660 | if (!vmx->vmcs) |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2661 | goto free_msrs; |
Gregory Haskins | a2fa3e9 | 2007-07-27 08:13:10 -0400 | [diff] [blame] | 2662 | |
| 2663 | vmcs_clear(vmx->vmcs); |
| 2664 | |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2665 | cpu = get_cpu(); |
| 2666 | vmx_vcpu_load(&vmx->vcpu, cpu); |
Rusty Russell | 8b9cf98 | 2007-07-30 16:31:43 +1000 | [diff] [blame] | 2667 | err = vmx_vcpu_setup(vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2668 | vmx_vcpu_put(&vmx->vcpu); |
Avi Kivity | 15ad714 | 2007-07-11 18:17:21 +0300 | [diff] [blame] | 2669 | put_cpu(); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2670 | if (err) |
| 2671 | goto free_vmcs; |
Marcelo Tosatti | 5e4a0b3 | 2008-02-14 21:21:43 -0200 | [diff] [blame] | 2672 | if (vm_need_virtualize_apic_accesses(kvm)) |
| 2673 | if (alloc_apic_access_page(kvm) != 0) |
| 2674 | goto free_vmcs; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2675 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2676 | return &vmx->vcpu; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2677 | |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2678 | free_vmcs: |
| 2679 | free_vmcs(vmx->vmcs); |
| 2680 | free_msrs: |
| 2681 | kfree(vmx->host_msrs); |
| 2682 | free_guest_msrs: |
| 2683 | kfree(vmx->guest_msrs); |
| 2684 | uninit_vcpu: |
| 2685 | kvm_vcpu_uninit(&vmx->vcpu); |
| 2686 | free_vcpu: |
Rusty Russell | a477034 | 2007-08-01 14:46:11 +1000 | [diff] [blame] | 2687 | kmem_cache_free(kvm_vcpu_cache, vmx); |
Rusty Russell | fb3f0f5 | 2007-07-27 17:16:56 +1000 | [diff] [blame] | 2688 | return ERR_PTR(err); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2689 | } |
| 2690 | |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2691 | static void __init vmx_check_processor_compat(void *rtn) |
| 2692 | { |
| 2693 | struct vmcs_config vmcs_conf; |
| 2694 | |
| 2695 | *(int *)rtn = 0; |
| 2696 | if (setup_vmcs_config(&vmcs_conf) < 0) |
| 2697 | *(int *)rtn = -EIO; |
| 2698 | if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) { |
| 2699 | printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n", |
| 2700 | smp_processor_id()); |
| 2701 | *(int *)rtn = -EIO; |
| 2702 | } |
| 2703 | } |
| 2704 | |
Christian Ehrhardt | cbdd1be | 2007-09-09 15:41:59 +0300 | [diff] [blame] | 2705 | static struct kvm_x86_ops vmx_x86_ops = { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2706 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 2707 | .disabled_by_bios = vmx_disabled_by_bios, |
| 2708 | .hardware_setup = hardware_setup, |
| 2709 | .hardware_unsetup = hardware_unsetup, |
Yang, Sheng | 002c7f7 | 2007-07-31 14:23:01 +0300 | [diff] [blame] | 2710 | .check_processor_compatibility = vmx_check_processor_compat, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2711 | .hardware_enable = hardware_enable, |
| 2712 | .hardware_disable = hardware_disable, |
Avi Kivity | 774ead3 | 2007-12-26 13:57:04 +0200 | [diff] [blame] | 2713 | .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2714 | |
| 2715 | .vcpu_create = vmx_create_vcpu, |
| 2716 | .vcpu_free = vmx_free_vcpu, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2717 | .vcpu_reset = vmx_vcpu_reset, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2718 | |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2719 | .prepare_guest_switch = vmx_save_host_state, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2720 | .vcpu_load = vmx_vcpu_load, |
| 2721 | .vcpu_put = vmx_vcpu_put, |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2722 | .vcpu_decache = vmx_vcpu_decache, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2723 | |
| 2724 | .set_guest_debug = set_guest_debug, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2725 | .guest_debug_pre = kvm_guest_debug_pre, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2726 | .get_msr = vmx_get_msr, |
| 2727 | .set_msr = vmx_set_msr, |
| 2728 | .get_segment_base = vmx_get_segment_base, |
| 2729 | .get_segment = vmx_get_segment, |
| 2730 | .set_segment = vmx_set_segment, |
Izik Eidus | 2e4d265 | 2008-03-24 19:38:34 +0200 | [diff] [blame] | 2731 | .get_cpl = vmx_get_cpl, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2732 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2733 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2734 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2735 | .set_cr3 = vmx_set_cr3, |
| 2736 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2737 | .set_efer = vmx_set_efer, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2738 | .get_idt = vmx_get_idt, |
| 2739 | .set_idt = vmx_set_idt, |
| 2740 | .get_gdt = vmx_get_gdt, |
| 2741 | .set_gdt = vmx_set_gdt, |
| 2742 | .cache_regs = vcpu_load_rsp_rip, |
| 2743 | .decache_regs = vcpu_put_rsp_rip, |
| 2744 | .get_rflags = vmx_get_rflags, |
| 2745 | .set_rflags = vmx_set_rflags, |
| 2746 | |
| 2747 | .tlb_flush = vmx_flush_tlb, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2748 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2749 | .run = vmx_vcpu_run, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2750 | .handle_exit = kvm_handle_exit, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2751 | .skip_emulated_instruction = skip_emulated_instruction, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2752 | .patch_hypercall = vmx_patch_hypercall, |
Eddie Dong | 2a8067f | 2007-08-06 16:29:07 +0300 | [diff] [blame] | 2753 | .get_irq = vmx_get_irq, |
| 2754 | .set_irq = vmx_inject_irq, |
Avi Kivity | 298101d | 2007-11-25 13:41:11 +0200 | [diff] [blame] | 2755 | .queue_exception = vmx_queue_exception, |
| 2756 | .exception_injected = vmx_exception_injected, |
Avi Kivity | 04d2cc7 | 2007-09-10 18:10:54 +0300 | [diff] [blame] | 2757 | .inject_pending_irq = vmx_intr_assist, |
| 2758 | .inject_pending_vectors = do_interrupt_requests, |
Izik Eidus | cbc9402 | 2007-10-25 00:29:55 +0200 | [diff] [blame] | 2759 | |
| 2760 | .set_tss_addr = vmx_set_tss_addr, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2761 | }; |
| 2762 | |
| 2763 | static int __init vmx_init(void) |
| 2764 | { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2765 | void *va; |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2766 | int r; |
| 2767 | |
| 2768 | vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 2769 | if (!vmx_io_bitmap_a) |
| 2770 | return -ENOMEM; |
| 2771 | |
| 2772 | vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 2773 | if (!vmx_io_bitmap_b) { |
| 2774 | r = -ENOMEM; |
| 2775 | goto out; |
| 2776 | } |
| 2777 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2778 | vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 2779 | if (!vmx_msr_bitmap) { |
| 2780 | r = -ENOMEM; |
| 2781 | goto out1; |
| 2782 | } |
| 2783 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2784 | /* |
| 2785 | * Allow direct access to the PC debug port (it is often used for I/O |
| 2786 | * delays, but the vmexits simply slow things down). |
| 2787 | */ |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2788 | va = kmap(vmx_io_bitmap_a); |
| 2789 | memset(va, 0xff, PAGE_SIZE); |
| 2790 | clear_bit(0x80, va); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 2791 | kunmap(vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2792 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2793 | va = kmap(vmx_io_bitmap_b); |
| 2794 | memset(va, 0xff, PAGE_SIZE); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 2795 | kunmap(vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2796 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2797 | va = kmap(vmx_msr_bitmap); |
| 2798 | memset(va, 0xff, PAGE_SIZE); |
| 2799 | kunmap(vmx_msr_bitmap); |
| 2800 | |
Sheng Yang | 2384d2b | 2008-01-17 15:14:33 +0800 | [diff] [blame] | 2801 | set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */ |
| 2802 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2803 | r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2804 | if (r) |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2805 | goto out2; |
| 2806 | |
| 2807 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE); |
| 2808 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE); |
| 2809 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS); |
| 2810 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP); |
| 2811 | vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2812 | |
Avi Kivity | c7addb9 | 2007-09-16 18:58:32 +0200 | [diff] [blame] | 2813 | if (bypass_guest_pf) |
| 2814 | kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull); |
| 2815 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2816 | return 0; |
| 2817 | |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2818 | out2: |
| 2819 | __free_page(vmx_msr_bitmap); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2820 | out1: |
| 2821 | __free_page(vmx_io_bitmap_b); |
| 2822 | out: |
| 2823 | __free_page(vmx_io_bitmap_a); |
| 2824 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | static void __exit vmx_exit(void) |
| 2828 | { |
Sheng Yang | 25c5f22 | 2008-03-28 13:18:56 +0800 | [diff] [blame^] | 2829 | __free_page(vmx_msr_bitmap); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2830 | __free_page(vmx_io_bitmap_b); |
| 2831 | __free_page(vmx_io_bitmap_a); |
| 2832 | |
Zhang Xiantao | cb498ea | 2007-11-14 20:39:31 +0800 | [diff] [blame] | 2833 | kvm_exit(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | module_init(vmx_init) |
| 2837 | module_exit(vmx_exit) |