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 | |
| 18 | #include "kvm.h" |
| 19 | #include "vmx.h" |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 20 | #include "segment_descriptor.h" |
| 21 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 22 | #include <linux/module.h> |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 23 | #include <linux/kernel.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 24 | #include <linux/mm.h> |
| 25 | #include <linux/highmem.h> |
Ingo Molnar | 07031e1 | 2007-01-10 23:15:38 -0800 | [diff] [blame] | 26 | #include <linux/profile.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 27 | #include <linux/sched.h> |
Avi Kivity | e495606 | 2007-06-28 14:15:57 -0400 | [diff] [blame] | 28 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 29 | #include <asm/io.h> |
Anthony Liguori | 3b3be0d | 2006-12-13 00:33:43 -0800 | [diff] [blame] | 30 | #include <asm/desc.h> |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 31 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 32 | MODULE_AUTHOR("Qumranet"); |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 35 | static int init_rmode_tss(struct kvm *kvm); |
| 36 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 37 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); |
| 38 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); |
| 39 | |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 40 | static struct page *vmx_io_bitmap_a; |
| 41 | static struct page *vmx_io_bitmap_b; |
| 42 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 43 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 44 | #define HOST_IS_64 1 |
| 45 | #else |
| 46 | #define HOST_IS_64 0 |
| 47 | #endif |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 48 | #define EFER_SAVE_RESTORE_BITS ((u64)EFER_SCE) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 49 | |
| 50 | static struct vmcs_descriptor { |
| 51 | int size; |
| 52 | int order; |
| 53 | u32 revision_id; |
| 54 | } vmcs_descriptor; |
| 55 | |
| 56 | #define VMX_SEGMENT_FIELD(seg) \ |
| 57 | [VCPU_SREG_##seg] = { \ |
| 58 | .selector = GUEST_##seg##_SELECTOR, \ |
| 59 | .base = GUEST_##seg##_BASE, \ |
| 60 | .limit = GUEST_##seg##_LIMIT, \ |
| 61 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ |
| 62 | } |
| 63 | |
| 64 | static struct kvm_vmx_segment_field { |
| 65 | unsigned selector; |
| 66 | unsigned base; |
| 67 | unsigned limit; |
| 68 | unsigned ar_bytes; |
| 69 | } kvm_vmx_segment_fields[] = { |
| 70 | VMX_SEGMENT_FIELD(CS), |
| 71 | VMX_SEGMENT_FIELD(DS), |
| 72 | VMX_SEGMENT_FIELD(ES), |
| 73 | VMX_SEGMENT_FIELD(FS), |
| 74 | VMX_SEGMENT_FIELD(GS), |
| 75 | VMX_SEGMENT_FIELD(SS), |
| 76 | VMX_SEGMENT_FIELD(TR), |
| 77 | VMX_SEGMENT_FIELD(LDTR), |
| 78 | }; |
| 79 | |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 80 | /* |
| 81 | * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it |
| 82 | * away by decrementing the array size. |
| 83 | */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 84 | static const u32 vmx_msr_index[] = { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 85 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 86 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE, |
| 87 | #endif |
| 88 | MSR_EFER, MSR_K6_STAR, |
| 89 | }; |
Ahmed S. Darwish | 9d8f549 | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 90 | #define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 91 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 92 | static inline u64 msr_efer_save_restore_bits(struct vmx_msr_entry msr) |
| 93 | { |
| 94 | return (u64)msr.data & EFER_SAVE_RESTORE_BITS; |
| 95 | } |
| 96 | |
| 97 | static inline int msr_efer_need_save_restore(struct kvm_vcpu *vcpu) |
| 98 | { |
| 99 | int efer_offset = vcpu->msr_offset_efer; |
| 100 | return msr_efer_save_restore_bits(vcpu->host_msrs[efer_offset]) != |
| 101 | msr_efer_save_restore_bits(vcpu->guest_msrs[efer_offset]); |
| 102 | } |
| 103 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 104 | static inline int is_page_fault(u32 intr_info) |
| 105 | { |
| 106 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 107 | INTR_INFO_VALID_MASK)) == |
| 108 | (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); |
| 109 | } |
| 110 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 111 | static inline int is_no_device(u32 intr_info) |
| 112 | { |
| 113 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | |
| 114 | INTR_INFO_VALID_MASK)) == |
| 115 | (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK); |
| 116 | } |
| 117 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 118 | static inline int is_external_interrupt(u32 intr_info) |
| 119 | { |
| 120 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) |
| 121 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 122 | } |
| 123 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 124 | static int __find_msr_index(struct kvm_vcpu *vcpu, u32 msr) |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 125 | { |
| 126 | int i; |
| 127 | |
| 128 | for (i = 0; i < vcpu->nmsrs; ++i) |
| 129 | if (vcpu->guest_msrs[i].index == msr) |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 130 | return i; |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | static struct vmx_msr_entry *find_msr_entry(struct kvm_vcpu *vcpu, u32 msr) |
| 135 | { |
| 136 | int i; |
| 137 | |
| 138 | i = __find_msr_index(vcpu, msr); |
| 139 | if (i >= 0) |
| 140 | return &vcpu->guest_msrs[i]; |
Al Viro | 8b6d44c | 2007-02-09 16:38:40 +0000 | [diff] [blame] | 141 | return NULL; |
Avi Kivity | 7725f0b | 2006-12-13 00:34:01 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 144 | static void vmcs_clear(struct vmcs *vmcs) |
| 145 | { |
| 146 | u64 phys_addr = __pa(vmcs); |
| 147 | u8 error; |
| 148 | |
| 149 | asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0" |
| 150 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 151 | : "cc", "memory"); |
| 152 | if (error) |
| 153 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", |
| 154 | vmcs, phys_addr); |
| 155 | } |
| 156 | |
| 157 | static void __vcpu_clear(void *arg) |
| 158 | { |
| 159 | struct kvm_vcpu *vcpu = arg; |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 160 | int cpu = raw_smp_processor_id(); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 161 | |
| 162 | if (vcpu->cpu == cpu) |
| 163 | vmcs_clear(vcpu->vmcs); |
| 164 | if (per_cpu(current_vmcs, cpu) == vcpu->vmcs) |
| 165 | per_cpu(current_vmcs, cpu) = NULL; |
Avi Kivity | 7700270 | 2007-06-13 19:55:28 +0300 | [diff] [blame] | 166 | rdtscll(vcpu->host_tsc); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 169 | static void vcpu_clear(struct kvm_vcpu *vcpu) |
| 170 | { |
| 171 | if (vcpu->cpu != raw_smp_processor_id() && vcpu->cpu != -1) |
| 172 | smp_call_function_single(vcpu->cpu, __vcpu_clear, vcpu, 0, 1); |
| 173 | else |
| 174 | __vcpu_clear(vcpu); |
| 175 | vcpu->launched = 0; |
| 176 | } |
| 177 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 178 | static unsigned long vmcs_readl(unsigned long field) |
| 179 | { |
| 180 | unsigned long value; |
| 181 | |
| 182 | asm volatile (ASM_VMX_VMREAD_RDX_RAX |
| 183 | : "=a"(value) : "d"(field) : "cc"); |
| 184 | return value; |
| 185 | } |
| 186 | |
| 187 | static u16 vmcs_read16(unsigned long field) |
| 188 | { |
| 189 | return vmcs_readl(field); |
| 190 | } |
| 191 | |
| 192 | static u32 vmcs_read32(unsigned long field) |
| 193 | { |
| 194 | return vmcs_readl(field); |
| 195 | } |
| 196 | |
| 197 | static u64 vmcs_read64(unsigned long field) |
| 198 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 199 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 200 | return vmcs_readl(field); |
| 201 | #else |
| 202 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); |
| 203 | #endif |
| 204 | } |
| 205 | |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 206 | static noinline void vmwrite_error(unsigned long field, unsigned long value) |
| 207 | { |
| 208 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", |
| 209 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); |
| 210 | dump_stack(); |
| 211 | } |
| 212 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 213 | static void vmcs_writel(unsigned long field, unsigned long value) |
| 214 | { |
| 215 | u8 error; |
| 216 | |
| 217 | asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0" |
| 218 | : "=q"(error) : "a"(value), "d"(field) : "cc" ); |
Avi Kivity | e52de1b | 2007-01-05 16:36:56 -0800 | [diff] [blame] | 219 | if (unlikely(error)) |
| 220 | vmwrite_error(field, value); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static void vmcs_write16(unsigned long field, u16 value) |
| 224 | { |
| 225 | vmcs_writel(field, value); |
| 226 | } |
| 227 | |
| 228 | static void vmcs_write32(unsigned long field, u32 value) |
| 229 | { |
| 230 | vmcs_writel(field, value); |
| 231 | } |
| 232 | |
| 233 | static void vmcs_write64(unsigned long field, u64 value) |
| 234 | { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 235 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 236 | vmcs_writel(field, value); |
| 237 | #else |
| 238 | vmcs_writel(field, value); |
| 239 | asm volatile (""); |
| 240 | vmcs_writel(field+1, value >> 32); |
| 241 | #endif |
| 242 | } |
| 243 | |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 244 | static void vmcs_clear_bits(unsigned long field, u32 mask) |
| 245 | { |
| 246 | vmcs_writel(field, vmcs_readl(field) & ~mask); |
| 247 | } |
| 248 | |
| 249 | static void vmcs_set_bits(unsigned long field, u32 mask) |
| 250 | { |
| 251 | vmcs_writel(field, vmcs_readl(field) | mask); |
| 252 | } |
| 253 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 254 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) |
| 255 | { |
| 256 | u32 eb; |
| 257 | |
| 258 | eb = 1u << PF_VECTOR; |
| 259 | if (!vcpu->fpu_active) |
| 260 | eb |= 1u << NM_VECTOR; |
| 261 | if (vcpu->guest_debug.enabled) |
| 262 | eb |= 1u << 1; |
| 263 | if (vcpu->rmode.active) |
| 264 | eb = ~0; |
| 265 | vmcs_write32(EXCEPTION_BITMAP, eb); |
| 266 | } |
| 267 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 268 | static void reload_tss(void) |
| 269 | { |
| 270 | #ifndef CONFIG_X86_64 |
| 271 | |
| 272 | /* |
| 273 | * VT restores TR but not its size. Useless. |
| 274 | */ |
| 275 | struct descriptor_table gdt; |
| 276 | struct segment_descriptor *descs; |
| 277 | |
| 278 | get_gdt(&gdt); |
| 279 | descs = (void *)gdt.base; |
| 280 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ |
| 281 | load_TR_desc(); |
| 282 | #endif |
| 283 | } |
| 284 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 285 | static void load_transition_efer(struct kvm_vcpu *vcpu) |
| 286 | { |
| 287 | u64 trans_efer; |
| 288 | int efer_offset = vcpu->msr_offset_efer; |
| 289 | |
| 290 | trans_efer = vcpu->host_msrs[efer_offset].data; |
| 291 | trans_efer &= ~EFER_SAVE_RESTORE_BITS; |
| 292 | trans_efer |= msr_efer_save_restore_bits( |
| 293 | vcpu->guest_msrs[efer_offset]); |
| 294 | wrmsrl(MSR_EFER, trans_efer); |
| 295 | vcpu->stat.efer_reload++; |
| 296 | } |
| 297 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 298 | static void vmx_save_host_state(struct kvm_vcpu *vcpu) |
| 299 | { |
| 300 | struct vmx_host_state *hs = &vcpu->vmx_host_state; |
| 301 | |
| 302 | if (hs->loaded) |
| 303 | return; |
| 304 | |
| 305 | hs->loaded = 1; |
| 306 | /* |
| 307 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not |
| 308 | * allow segment selectors with cpl > 0 or ti == 1. |
| 309 | */ |
| 310 | hs->ldt_sel = read_ldt(); |
| 311 | hs->fs_gs_ldt_reload_needed = hs->ldt_sel; |
| 312 | hs->fs_sel = read_fs(); |
| 313 | if (!(hs->fs_sel & 7)) |
| 314 | vmcs_write16(HOST_FS_SELECTOR, hs->fs_sel); |
| 315 | else { |
| 316 | vmcs_write16(HOST_FS_SELECTOR, 0); |
| 317 | hs->fs_gs_ldt_reload_needed = 1; |
| 318 | } |
| 319 | hs->gs_sel = read_gs(); |
| 320 | if (!(hs->gs_sel & 7)) |
| 321 | vmcs_write16(HOST_GS_SELECTOR, hs->gs_sel); |
| 322 | else { |
| 323 | vmcs_write16(HOST_GS_SELECTOR, 0); |
| 324 | hs->fs_gs_ldt_reload_needed = 1; |
| 325 | } |
| 326 | |
| 327 | #ifdef CONFIG_X86_64 |
| 328 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
| 329 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); |
| 330 | #else |
| 331 | vmcs_writel(HOST_FS_BASE, segment_base(hs->fs_sel)); |
| 332 | vmcs_writel(HOST_GS_BASE, segment_base(hs->gs_sel)); |
| 333 | #endif |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 334 | |
| 335 | #ifdef CONFIG_X86_64 |
| 336 | if (is_long_mode(vcpu)) { |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 337 | save_msrs(vcpu->host_msrs + vcpu->msr_offset_kernel_gs_base, 1); |
Avi Kivity | 707c087 | 2007-05-02 17:33:43 +0300 | [diff] [blame] | 338 | } |
| 339 | #endif |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 340 | load_msrs(vcpu->guest_msrs, vcpu->save_nmsrs); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 341 | if (msr_efer_need_save_restore(vcpu)) |
| 342 | load_transition_efer(vcpu); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static void vmx_load_host_state(struct kvm_vcpu *vcpu) |
| 346 | { |
| 347 | struct vmx_host_state *hs = &vcpu->vmx_host_state; |
| 348 | |
| 349 | if (!hs->loaded) |
| 350 | return; |
| 351 | |
| 352 | hs->loaded = 0; |
| 353 | if (hs->fs_gs_ldt_reload_needed) { |
| 354 | load_ldt(hs->ldt_sel); |
| 355 | load_fs(hs->fs_sel); |
| 356 | /* |
| 357 | * If we have to reload gs, we must take care to |
| 358 | * preserve our gs base. |
| 359 | */ |
| 360 | local_irq_disable(); |
| 361 | load_gs(hs->gs_sel); |
| 362 | #ifdef CONFIG_X86_64 |
| 363 | wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE)); |
| 364 | #endif |
| 365 | local_irq_enable(); |
| 366 | |
| 367 | reload_tss(); |
| 368 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 369 | save_msrs(vcpu->guest_msrs, vcpu->save_nmsrs); |
| 370 | load_msrs(vcpu->host_msrs, vcpu->save_nmsrs); |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 371 | if (msr_efer_need_save_restore(vcpu)) |
| 372 | load_msrs(vcpu->host_msrs + vcpu->msr_offset_efer, 1); |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 373 | } |
| 374 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 375 | /* |
| 376 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes |
| 377 | * vcpu mutex is already taken. |
| 378 | */ |
Avi Kivity | bccf215 | 2007-02-21 18:04:26 +0200 | [diff] [blame] | 379 | static void vmx_vcpu_load(struct kvm_vcpu *vcpu) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 380 | { |
| 381 | u64 phys_addr = __pa(vcpu->vmcs); |
| 382 | int cpu; |
Avi Kivity | 7700270 | 2007-06-13 19:55:28 +0300 | [diff] [blame] | 383 | u64 tsc_this, delta; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 384 | |
| 385 | cpu = get_cpu(); |
| 386 | |
Avi Kivity | 8d0be2b | 2007-02-12 00:54:46 -0800 | [diff] [blame] | 387 | if (vcpu->cpu != cpu) |
| 388 | vcpu_clear(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 389 | |
| 390 | if (per_cpu(current_vmcs, cpu) != vcpu->vmcs) { |
| 391 | u8 error; |
| 392 | |
| 393 | per_cpu(current_vmcs, cpu) = vcpu->vmcs; |
| 394 | asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0" |
| 395 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) |
| 396 | : "cc"); |
| 397 | if (error) |
| 398 | printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n", |
| 399 | vcpu->vmcs, phys_addr); |
| 400 | } |
| 401 | |
| 402 | if (vcpu->cpu != cpu) { |
| 403 | struct descriptor_table dt; |
| 404 | unsigned long sysenter_esp; |
| 405 | |
| 406 | vcpu->cpu = cpu; |
| 407 | /* |
| 408 | * Linux uses per-cpu TSS and GDT, so set these when switching |
| 409 | * processors. |
| 410 | */ |
| 411 | vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */ |
| 412 | get_gdt(&dt); |
| 413 | vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */ |
| 414 | |
| 415 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); |
| 416 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ |
Avi Kivity | 7700270 | 2007-06-13 19:55:28 +0300 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | * Make sure the time stamp counter is monotonous. |
| 420 | */ |
| 421 | rdtscll(tsc_this); |
| 422 | delta = vcpu->host_tsc - tsc_this; |
| 423 | vmcs_write64(TSC_OFFSET, vmcs_read64(TSC_OFFSET) + delta); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 424 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) |
| 428 | { |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 429 | vmx_load_host_state(vcpu); |
Avi Kivity | 7702fd1 | 2007-06-14 16:27:40 +0300 | [diff] [blame] | 430 | kvm_put_guest_fpu(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 431 | put_cpu(); |
| 432 | } |
| 433 | |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 434 | static void vmx_fpu_activate(struct kvm_vcpu *vcpu) |
| 435 | { |
| 436 | if (vcpu->fpu_active) |
| 437 | return; |
| 438 | vcpu->fpu_active = 1; |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 439 | vmcs_clear_bits(GUEST_CR0, X86_CR0_TS); |
| 440 | if (vcpu->cr0 & X86_CR0_TS) |
| 441 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 442 | update_exception_bitmap(vcpu); |
| 443 | } |
| 444 | |
| 445 | static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu) |
| 446 | { |
| 447 | if (!vcpu->fpu_active) |
| 448 | return; |
| 449 | vcpu->fpu_active = 0; |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 450 | vmcs_set_bits(GUEST_CR0, X86_CR0_TS); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 451 | update_exception_bitmap(vcpu); |
| 452 | } |
| 453 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 454 | static void vmx_vcpu_decache(struct kvm_vcpu *vcpu) |
| 455 | { |
| 456 | vcpu_clear(vcpu); |
| 457 | } |
| 458 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 459 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) |
| 460 | { |
| 461 | return vmcs_readl(GUEST_RFLAGS); |
| 462 | } |
| 463 | |
| 464 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
| 465 | { |
| 466 | vmcs_writel(GUEST_RFLAGS, rflags); |
| 467 | } |
| 468 | |
| 469 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
| 470 | { |
| 471 | unsigned long rip; |
| 472 | u32 interruptibility; |
| 473 | |
| 474 | rip = vmcs_readl(GUEST_RIP); |
| 475 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); |
| 476 | vmcs_writel(GUEST_RIP, rip); |
| 477 | |
| 478 | /* |
| 479 | * We emulated an instruction, so temporary interrupt blocking |
| 480 | * should be removed, if set. |
| 481 | */ |
| 482 | interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); |
| 483 | if (interruptibility & 3) |
| 484 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, |
| 485 | interruptibility & ~3); |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 486 | vcpu->interrupt_window_open = 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code) |
| 490 | { |
| 491 | printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n", |
| 492 | vmcs_readl(GUEST_RIP)); |
| 493 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); |
| 494 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 495 | GP_VECTOR | |
| 496 | INTR_TYPE_EXCEPTION | |
| 497 | INTR_INFO_DELIEVER_CODE_MASK | |
| 498 | INTR_INFO_VALID_MASK); |
| 499 | } |
| 500 | |
| 501 | /* |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 502 | * Swap MSR entry in host/guest MSR entry array. |
| 503 | */ |
| 504 | void move_msr_up(struct kvm_vcpu *vcpu, int from, int to) |
| 505 | { |
| 506 | struct vmx_msr_entry tmp; |
| 507 | tmp = vcpu->guest_msrs[to]; |
| 508 | vcpu->guest_msrs[to] = vcpu->guest_msrs[from]; |
| 509 | vcpu->guest_msrs[from] = tmp; |
| 510 | tmp = vcpu->host_msrs[to]; |
| 511 | vcpu->host_msrs[to] = vcpu->host_msrs[from]; |
| 512 | vcpu->host_msrs[from] = tmp; |
| 513 | } |
| 514 | |
| 515 | /* |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 516 | * Set up the vmcs to automatically save and restore system |
| 517 | * msrs. Don't touch the 64-bit msrs if the guest is in legacy |
| 518 | * mode, as fiddling with msrs is very expensive. |
| 519 | */ |
| 520 | static void setup_msrs(struct kvm_vcpu *vcpu) |
| 521 | { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 522 | int save_nmsrs; |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 523 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 524 | save_nmsrs = 0; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 525 | #ifdef CONFIG_X86_64 |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 526 | if (is_long_mode(vcpu)) { |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 527 | int index; |
| 528 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 529 | index = __find_msr_index(vcpu, MSR_SYSCALL_MASK); |
| 530 | if (index >= 0) |
| 531 | move_msr_up(vcpu, index, save_nmsrs++); |
| 532 | index = __find_msr_index(vcpu, MSR_LSTAR); |
| 533 | if (index >= 0) |
| 534 | move_msr_up(vcpu, index, save_nmsrs++); |
| 535 | index = __find_msr_index(vcpu, MSR_CSTAR); |
| 536 | if (index >= 0) |
| 537 | move_msr_up(vcpu, index, save_nmsrs++); |
| 538 | index = __find_msr_index(vcpu, MSR_KERNEL_GS_BASE); |
| 539 | if (index >= 0) |
| 540 | move_msr_up(vcpu, index, save_nmsrs++); |
| 541 | /* |
| 542 | * MSR_K6_STAR is only needed on long mode guests, and only |
| 543 | * if efer.sce is enabled. |
| 544 | */ |
| 545 | index = __find_msr_index(vcpu, MSR_K6_STAR); |
| 546 | if ((index >= 0) && (vcpu->shadow_efer & EFER_SCE)) |
| 547 | move_msr_up(vcpu, index, save_nmsrs++); |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 548 | } |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 549 | #endif |
| 550 | vcpu->save_nmsrs = save_nmsrs; |
Avi Kivity | 4d56c8a | 2007-04-19 14:28:44 +0300 | [diff] [blame] | 551 | |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 552 | #ifdef CONFIG_X86_64 |
| 553 | vcpu->msr_offset_kernel_gs_base = |
| 554 | __find_msr_index(vcpu, MSR_KERNEL_GS_BASE); |
| 555 | #endif |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 556 | vcpu->msr_offset_efer = __find_msr_index(vcpu, MSR_EFER); |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 560 | * reads and returns guest's timestamp counter "register" |
| 561 | * guest_tsc = host_tsc + tsc_offset -- 21.3 |
| 562 | */ |
| 563 | static u64 guest_read_tsc(void) |
| 564 | { |
| 565 | u64 host_tsc, tsc_offset; |
| 566 | |
| 567 | rdtscll(host_tsc); |
| 568 | tsc_offset = vmcs_read64(TSC_OFFSET); |
| 569 | return host_tsc + tsc_offset; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * writes 'guest_tsc' into guest's timestamp counter "register" |
| 574 | * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc |
| 575 | */ |
| 576 | static void guest_write_tsc(u64 guest_tsc) |
| 577 | { |
| 578 | u64 host_tsc; |
| 579 | |
| 580 | rdtscll(host_tsc); |
| 581 | vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc); |
| 582 | } |
| 583 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 584 | /* |
| 585 | * Reads an msr value (of 'msr_index') into 'pdata'. |
| 586 | * Returns 0 on success, non-0 otherwise. |
| 587 | * Assumes vcpu_load() was already called. |
| 588 | */ |
| 589 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) |
| 590 | { |
| 591 | u64 data; |
| 592 | struct vmx_msr_entry *msr; |
| 593 | |
| 594 | if (!pdata) { |
| 595 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); |
| 596 | return -EINVAL; |
| 597 | } |
| 598 | |
| 599 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 600 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 601 | case MSR_FS_BASE: |
| 602 | data = vmcs_readl(GUEST_FS_BASE); |
| 603 | break; |
| 604 | case MSR_GS_BASE: |
| 605 | data = vmcs_readl(GUEST_GS_BASE); |
| 606 | break; |
| 607 | case MSR_EFER: |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 608 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 609 | #endif |
| 610 | case MSR_IA32_TIME_STAMP_COUNTER: |
| 611 | data = guest_read_tsc(); |
| 612 | break; |
| 613 | case MSR_IA32_SYSENTER_CS: |
| 614 | data = vmcs_read32(GUEST_SYSENTER_CS); |
| 615 | break; |
| 616 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 617 | data = vmcs_readl(GUEST_SYSENTER_EIP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 618 | break; |
| 619 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 620 | data = vmcs_readl(GUEST_SYSENTER_ESP); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 621 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 622 | default: |
| 623 | msr = find_msr_entry(vcpu, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 624 | if (msr) { |
| 625 | data = msr->data; |
| 626 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 627 | } |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 628 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | *pdata = data; |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* |
| 636 | * Writes msr value into into the appropriate "register". |
| 637 | * Returns 0 on success, non-0 otherwise. |
| 638 | * Assumes vcpu_load() was already called. |
| 639 | */ |
| 640 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) |
| 641 | { |
| 642 | struct vmx_msr_entry *msr; |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 643 | int ret = 0; |
| 644 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 645 | switch (msr_index) { |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 646 | #ifdef CONFIG_X86_64 |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 647 | case MSR_EFER: |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 648 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
| 649 | if (vcpu->vmx_host_state.loaded) |
| 650 | load_transition_efer(vcpu); |
| 651 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 652 | case MSR_FS_BASE: |
| 653 | vmcs_writel(GUEST_FS_BASE, data); |
| 654 | break; |
| 655 | case MSR_GS_BASE: |
| 656 | vmcs_writel(GUEST_GS_BASE, data); |
| 657 | break; |
| 658 | #endif |
| 659 | case MSR_IA32_SYSENTER_CS: |
| 660 | vmcs_write32(GUEST_SYSENTER_CS, data); |
| 661 | break; |
| 662 | case MSR_IA32_SYSENTER_EIP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 663 | vmcs_writel(GUEST_SYSENTER_EIP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 664 | break; |
| 665 | case MSR_IA32_SYSENTER_ESP: |
Avi Kivity | f5b42c3 | 2007-03-06 12:05:53 +0200 | [diff] [blame] | 666 | vmcs_writel(GUEST_SYSENTER_ESP, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 667 | break; |
Avi Kivity | d27d4ac | 2007-02-19 14:37:46 +0200 | [diff] [blame] | 668 | case MSR_IA32_TIME_STAMP_COUNTER: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 669 | guest_write_tsc(data); |
| 670 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 671 | default: |
| 672 | msr = find_msr_entry(vcpu, msr_index); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 673 | if (msr) { |
| 674 | msr->data = data; |
Eddie Dong | a75beee | 2007-05-17 18:55:15 +0300 | [diff] [blame] | 675 | if (vcpu->vmx_host_state.loaded) |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 676 | load_msrs(vcpu->guest_msrs, vcpu->save_nmsrs); |
Avi Kivity | 3bab1f5 | 2006-12-29 16:49:48 -0800 | [diff] [blame] | 677 | break; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 678 | } |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 679 | ret = kvm_set_msr_common(vcpu, msr_index, data); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 682 | return ret; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Sync the rsp and rip registers into the vcpu structure. This allows |
| 687 | * registers to be accessed by indexing vcpu->regs. |
| 688 | */ |
| 689 | static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu) |
| 690 | { |
| 691 | vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); |
| 692 | vcpu->rip = vmcs_readl(GUEST_RIP); |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Syncs rsp and rip back into the vmcs. Should be called after possible |
| 697 | * modification. |
| 698 | */ |
| 699 | static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu) |
| 700 | { |
| 701 | vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]); |
| 702 | vmcs_writel(GUEST_RIP, vcpu->rip); |
| 703 | } |
| 704 | |
| 705 | static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) |
| 706 | { |
| 707 | unsigned long dr7 = 0x400; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 708 | int old_singlestep; |
| 709 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 710 | old_singlestep = vcpu->guest_debug.singlestep; |
| 711 | |
| 712 | vcpu->guest_debug.enabled = dbg->enabled; |
| 713 | if (vcpu->guest_debug.enabled) { |
| 714 | int i; |
| 715 | |
| 716 | dr7 |= 0x200; /* exact */ |
| 717 | for (i = 0; i < 4; ++i) { |
| 718 | if (!dbg->breakpoints[i].enabled) |
| 719 | continue; |
| 720 | vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address; |
| 721 | dr7 |= 2 << (i*2); /* global enable */ |
| 722 | dr7 |= 0 << (i*4+16); /* execution breakpoint */ |
| 723 | } |
| 724 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 725 | vcpu->guest_debug.singlestep = dbg->singlestep; |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 726 | } else |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 727 | vcpu->guest_debug.singlestep = 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 728 | |
| 729 | if (old_singlestep && !vcpu->guest_debug.singlestep) { |
| 730 | unsigned long flags; |
| 731 | |
| 732 | flags = vmcs_readl(GUEST_RFLAGS); |
| 733 | flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); |
| 734 | vmcs_writel(GUEST_RFLAGS, flags); |
| 735 | } |
| 736 | |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 737 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 738 | vmcs_writel(GUEST_DR7, dr7); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | static __init int cpu_has_kvm_support(void) |
| 744 | { |
| 745 | unsigned long ecx = cpuid_ecx(1); |
| 746 | return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */ |
| 747 | } |
| 748 | |
| 749 | static __init int vmx_disabled_by_bios(void) |
| 750 | { |
| 751 | u64 msr; |
| 752 | |
| 753 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); |
| 754 | return (msr & 5) == 1; /* locked but not enabled */ |
| 755 | } |
| 756 | |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 757 | static void hardware_enable(void *garbage) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 758 | { |
| 759 | int cpu = raw_smp_processor_id(); |
| 760 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); |
| 761 | u64 old; |
| 762 | |
| 763 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); |
Avi Kivity | bfdc0c2 | 2006-12-13 00:34:16 -0800 | [diff] [blame] | 764 | if ((old & 5) != 5) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 765 | /* enable and lock */ |
| 766 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | 5); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 767 | write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 768 | asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr) |
| 769 | : "memory", "cc"); |
| 770 | } |
| 771 | |
| 772 | static void hardware_disable(void *garbage) |
| 773 | { |
| 774 | asm volatile (ASM_VMX_VMXOFF : : : "cc"); |
| 775 | } |
| 776 | |
| 777 | static __init void setup_vmcs_descriptor(void) |
| 778 | { |
| 779 | u32 vmx_msr_low, vmx_msr_high; |
| 780 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 781 | rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 782 | vmcs_descriptor.size = vmx_msr_high & 0x1fff; |
| 783 | vmcs_descriptor.order = get_order(vmcs_descriptor.size); |
| 784 | vmcs_descriptor.revision_id = vmx_msr_low; |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 785 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 786 | |
| 787 | static struct vmcs *alloc_vmcs_cpu(int cpu) |
| 788 | { |
| 789 | int node = cpu_to_node(cpu); |
| 790 | struct page *pages; |
| 791 | struct vmcs *vmcs; |
| 792 | |
| 793 | pages = alloc_pages_node(node, GFP_KERNEL, vmcs_descriptor.order); |
| 794 | if (!pages) |
| 795 | return NULL; |
| 796 | vmcs = page_address(pages); |
| 797 | memset(vmcs, 0, vmcs_descriptor.size); |
| 798 | vmcs->revision_id = vmcs_descriptor.revision_id; /* vmcs revision id */ |
| 799 | return vmcs; |
| 800 | } |
| 801 | |
| 802 | static struct vmcs *alloc_vmcs(void) |
| 803 | { |
Ingo Molnar | d3b2c33 | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 804 | return alloc_vmcs_cpu(raw_smp_processor_id()); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static void free_vmcs(struct vmcs *vmcs) |
| 808 | { |
| 809 | free_pages((unsigned long)vmcs, vmcs_descriptor.order); |
| 810 | } |
| 811 | |
Sam Ravnborg | 3995958 | 2007-06-01 00:47:13 -0700 | [diff] [blame] | 812 | static void free_kvm_area(void) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 813 | { |
| 814 | int cpu; |
| 815 | |
| 816 | for_each_online_cpu(cpu) |
| 817 | free_vmcs(per_cpu(vmxarea, cpu)); |
| 818 | } |
| 819 | |
| 820 | extern struct vmcs *alloc_vmcs_cpu(int cpu); |
| 821 | |
| 822 | static __init int alloc_kvm_area(void) |
| 823 | { |
| 824 | int cpu; |
| 825 | |
| 826 | for_each_online_cpu(cpu) { |
| 827 | struct vmcs *vmcs; |
| 828 | |
| 829 | vmcs = alloc_vmcs_cpu(cpu); |
| 830 | if (!vmcs) { |
| 831 | free_kvm_area(); |
| 832 | return -ENOMEM; |
| 833 | } |
| 834 | |
| 835 | per_cpu(vmxarea, cpu) = vmcs; |
| 836 | } |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | static __init int hardware_setup(void) |
| 841 | { |
| 842 | setup_vmcs_descriptor(); |
| 843 | return alloc_kvm_area(); |
| 844 | } |
| 845 | |
| 846 | static __exit void hardware_unsetup(void) |
| 847 | { |
| 848 | free_kvm_area(); |
| 849 | } |
| 850 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 851 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) |
| 852 | { |
| 853 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 854 | |
Avi Kivity | 6af11b9 | 2007-03-19 13:18:10 +0200 | [diff] [blame] | 855 | if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 856 | vmcs_write16(sf->selector, save->selector); |
| 857 | vmcs_writel(sf->base, save->base); |
| 858 | vmcs_write32(sf->limit, save->limit); |
| 859 | vmcs_write32(sf->ar_bytes, save->ar); |
| 860 | } else { |
| 861 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) |
| 862 | << AR_DPL_SHIFT; |
| 863 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | static void enter_pmode(struct kvm_vcpu *vcpu) |
| 868 | { |
| 869 | unsigned long flags; |
| 870 | |
| 871 | vcpu->rmode.active = 0; |
| 872 | |
| 873 | vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base); |
| 874 | vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit); |
| 875 | vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar); |
| 876 | |
| 877 | flags = vmcs_readl(GUEST_RFLAGS); |
| 878 | flags &= ~(IOPL_MASK | X86_EFLAGS_VM); |
| 879 | flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT); |
| 880 | vmcs_writel(GUEST_RFLAGS, flags); |
| 881 | |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 882 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) | |
| 883 | (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 884 | |
| 885 | update_exception_bitmap(vcpu); |
| 886 | |
| 887 | fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es); |
| 888 | fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds); |
| 889 | fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs); |
| 890 | fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs); |
| 891 | |
| 892 | vmcs_write16(GUEST_SS_SELECTOR, 0); |
| 893 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); |
| 894 | |
| 895 | vmcs_write16(GUEST_CS_SELECTOR, |
| 896 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); |
| 897 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 898 | } |
| 899 | |
| 900 | static int rmode_tss_base(struct kvm* kvm) |
| 901 | { |
| 902 | gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3; |
| 903 | return base_gfn << PAGE_SHIFT; |
| 904 | } |
| 905 | |
| 906 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) |
| 907 | { |
| 908 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 909 | |
| 910 | save->selector = vmcs_read16(sf->selector); |
| 911 | save->base = vmcs_readl(sf->base); |
| 912 | save->limit = vmcs_read32(sf->limit); |
| 913 | save->ar = vmcs_read32(sf->ar_bytes); |
| 914 | vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4); |
| 915 | vmcs_write32(sf->limit, 0xffff); |
| 916 | vmcs_write32(sf->ar_bytes, 0xf3); |
| 917 | } |
| 918 | |
| 919 | static void enter_rmode(struct kvm_vcpu *vcpu) |
| 920 | { |
| 921 | unsigned long flags; |
| 922 | |
| 923 | vcpu->rmode.active = 1; |
| 924 | |
| 925 | vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE); |
| 926 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); |
| 927 | |
| 928 | vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); |
| 929 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); |
| 930 | |
| 931 | vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 932 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 933 | |
| 934 | flags = vmcs_readl(GUEST_RFLAGS); |
| 935 | vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT; |
| 936 | |
| 937 | flags |= IOPL_MASK | X86_EFLAGS_VM; |
| 938 | |
| 939 | vmcs_writel(GUEST_RFLAGS, flags); |
Rusty Russell | 66aee91 | 2007-07-17 23:34:16 +1000 | [diff] [blame] | 940 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 941 | update_exception_bitmap(vcpu); |
| 942 | |
| 943 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); |
| 944 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); |
| 945 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); |
| 946 | |
| 947 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); |
Michael Riepe | abacf8d | 2006-12-22 01:05:45 -0800 | [diff] [blame] | 948 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
Avi Kivity | 8cb5b03 | 2007-03-20 18:40:40 +0200 | [diff] [blame] | 949 | if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000) |
| 950 | vmcs_writel(GUEST_CS_BASE, 0xf0000); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 951 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
| 952 | |
| 953 | fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es); |
| 954 | fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds); |
| 955 | fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs); |
| 956 | fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs); |
Avi Kivity | 75880a0 | 2007-06-20 11:20:04 +0300 | [diff] [blame] | 957 | |
| 958 | init_rmode_tss(vcpu->kvm); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 961 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 962 | |
| 963 | static void enter_lmode(struct kvm_vcpu *vcpu) |
| 964 | { |
| 965 | u32 guest_tr_ar; |
| 966 | |
| 967 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); |
| 968 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { |
| 969 | printk(KERN_DEBUG "%s: tss fixup for long mode. \n", |
| 970 | __FUNCTION__); |
| 971 | vmcs_write32(GUEST_TR_AR_BYTES, |
| 972 | (guest_tr_ar & ~AR_TYPE_MASK) |
| 973 | | AR_TYPE_BUSY_64_TSS); |
| 974 | } |
| 975 | |
| 976 | vcpu->shadow_efer |= EFER_LMA; |
| 977 | |
| 978 | find_msr_entry(vcpu, MSR_EFER)->data |= EFER_LMA | EFER_LME; |
| 979 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 980 | vmcs_read32(VM_ENTRY_CONTROLS) |
| 981 | | VM_ENTRY_CONTROLS_IA32E_MASK); |
| 982 | } |
| 983 | |
| 984 | static void exit_lmode(struct kvm_vcpu *vcpu) |
| 985 | { |
| 986 | vcpu->shadow_efer &= ~EFER_LMA; |
| 987 | |
| 988 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 989 | vmcs_read32(VM_ENTRY_CONTROLS) |
| 990 | & ~VM_ENTRY_CONTROLS_IA32E_MASK); |
| 991 | } |
| 992 | |
| 993 | #endif |
| 994 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 995 | static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 996 | { |
Avi Kivity | 399badf | 2007-01-05 16:36:38 -0800 | [diff] [blame] | 997 | vcpu->cr4 &= KVM_GUEST_CR4_MASK; |
| 998 | vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK; |
| 999 | } |
| 1000 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1001 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
| 1002 | { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1003 | vmx_fpu_deactivate(vcpu); |
| 1004 | |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1005 | if (vcpu->rmode.active && (cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1006 | enter_pmode(vcpu); |
| 1007 | |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1008 | if (!vcpu->rmode.active && !(cr0 & X86_CR0_PE)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1009 | enter_rmode(vcpu); |
| 1010 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1011 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1012 | if (vcpu->shadow_efer & EFER_LME) { |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1013 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1014 | enter_lmode(vcpu); |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1015 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1016 | exit_lmode(vcpu); |
| 1017 | } |
| 1018 | #endif |
| 1019 | |
| 1020 | vmcs_writel(CR0_READ_SHADOW, cr0); |
| 1021 | vmcs_writel(GUEST_CR0, |
| 1022 | (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON); |
| 1023 | vcpu->cr0 = cr0; |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1024 | |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1025 | if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE)) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1026 | vmx_fpu_activate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1027 | } |
| 1028 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1029 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
| 1030 | { |
| 1031 | vmcs_writel(GUEST_CR3, cr3); |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1032 | if (vcpu->cr0 & X86_CR0_PE) |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1033 | vmx_fpu_deactivate(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
| 1037 | { |
| 1038 | vmcs_writel(CR4_READ_SHADOW, cr4); |
| 1039 | vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ? |
| 1040 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON)); |
| 1041 | vcpu->cr4 = cr4; |
| 1042 | } |
| 1043 | |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1044 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1045 | |
| 1046 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
| 1047 | { |
| 1048 | struct vmx_msr_entry *msr = find_msr_entry(vcpu, MSR_EFER); |
| 1049 | |
| 1050 | vcpu->shadow_efer = efer; |
| 1051 | if (efer & EFER_LMA) { |
| 1052 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1053 | vmcs_read32(VM_ENTRY_CONTROLS) | |
| 1054 | VM_ENTRY_CONTROLS_IA32E_MASK); |
| 1055 | msr->data = efer; |
| 1056 | |
| 1057 | } else { |
| 1058 | vmcs_write32(VM_ENTRY_CONTROLS, |
| 1059 | vmcs_read32(VM_ENTRY_CONTROLS) & |
| 1060 | ~VM_ENTRY_CONTROLS_IA32E_MASK); |
| 1061 | |
| 1062 | msr->data = efer & ~EFER_LME; |
| 1063 | } |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1064 | setup_msrs(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | #endif |
| 1068 | |
| 1069 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) |
| 1070 | { |
| 1071 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1072 | |
| 1073 | return vmcs_readl(sf->base); |
| 1074 | } |
| 1075 | |
| 1076 | static void vmx_get_segment(struct kvm_vcpu *vcpu, |
| 1077 | struct kvm_segment *var, int seg) |
| 1078 | { |
| 1079 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1080 | u32 ar; |
| 1081 | |
| 1082 | var->base = vmcs_readl(sf->base); |
| 1083 | var->limit = vmcs_read32(sf->limit); |
| 1084 | var->selector = vmcs_read16(sf->selector); |
| 1085 | ar = vmcs_read32(sf->ar_bytes); |
| 1086 | if (ar & AR_UNUSABLE_MASK) |
| 1087 | ar = 0; |
| 1088 | var->type = ar & 15; |
| 1089 | var->s = (ar >> 4) & 1; |
| 1090 | var->dpl = (ar >> 5) & 3; |
| 1091 | var->present = (ar >> 7) & 1; |
| 1092 | var->avl = (ar >> 12) & 1; |
| 1093 | var->l = (ar >> 13) & 1; |
| 1094 | var->db = (ar >> 14) & 1; |
| 1095 | var->g = (ar >> 15) & 1; |
| 1096 | var->unusable = (ar >> 16) & 1; |
| 1097 | } |
| 1098 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1099 | static u32 vmx_segment_access_rights(struct kvm_segment *var) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1100 | { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1101 | u32 ar; |
| 1102 | |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1103 | if (var->unusable) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1104 | ar = 1 << 16; |
| 1105 | else { |
| 1106 | ar = var->type & 15; |
| 1107 | ar |= (var->s & 1) << 4; |
| 1108 | ar |= (var->dpl & 3) << 5; |
| 1109 | ar |= (var->present & 1) << 7; |
| 1110 | ar |= (var->avl & 1) << 12; |
| 1111 | ar |= (var->l & 1) << 13; |
| 1112 | ar |= (var->db & 1) << 14; |
| 1113 | ar |= (var->g & 1) << 15; |
| 1114 | } |
Uri Lublin | f7fbf1f | 2006-12-13 00:34:00 -0800 | [diff] [blame] | 1115 | if (ar == 0) /* a 0 value means unusable */ |
| 1116 | ar = AR_UNUSABLE_MASK; |
Avi Kivity | 653e310 | 2007-05-07 10:55:37 +0300 | [diff] [blame] | 1117 | |
| 1118 | return ar; |
| 1119 | } |
| 1120 | |
| 1121 | static void vmx_set_segment(struct kvm_vcpu *vcpu, |
| 1122 | struct kvm_segment *var, int seg) |
| 1123 | { |
| 1124 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1125 | u32 ar; |
| 1126 | |
| 1127 | if (vcpu->rmode.active && seg == VCPU_SREG_TR) { |
| 1128 | vcpu->rmode.tr.selector = var->selector; |
| 1129 | vcpu->rmode.tr.base = var->base; |
| 1130 | vcpu->rmode.tr.limit = var->limit; |
| 1131 | vcpu->rmode.tr.ar = vmx_segment_access_rights(var); |
| 1132 | return; |
| 1133 | } |
| 1134 | vmcs_writel(sf->base, var->base); |
| 1135 | vmcs_write32(sf->limit, var->limit); |
| 1136 | vmcs_write16(sf->selector, var->selector); |
| 1137 | if (vcpu->rmode.active && var->s) { |
| 1138 | /* |
| 1139 | * Hack real-mode segments into vm86 compatibility. |
| 1140 | */ |
| 1141 | if (var->base == 0xffff0000 && var->selector == 0xf000) |
| 1142 | vmcs_writel(sf->base, 0xf0000); |
| 1143 | ar = 0xf3; |
| 1144 | } else |
| 1145 | ar = vmx_segment_access_rights(var); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1146 | vmcs_write32(sf->ar_bytes, ar); |
| 1147 | } |
| 1148 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1149 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
| 1150 | { |
| 1151 | u32 ar = vmcs_read32(GUEST_CS_AR_BYTES); |
| 1152 | |
| 1153 | *db = (ar >> 14) & 1; |
| 1154 | *l = (ar >> 13) & 1; |
| 1155 | } |
| 1156 | |
| 1157 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1158 | { |
| 1159 | dt->limit = vmcs_read32(GUEST_IDTR_LIMIT); |
| 1160 | dt->base = vmcs_readl(GUEST_IDTR_BASE); |
| 1161 | } |
| 1162 | |
| 1163 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1164 | { |
| 1165 | vmcs_write32(GUEST_IDTR_LIMIT, dt->limit); |
| 1166 | vmcs_writel(GUEST_IDTR_BASE, dt->base); |
| 1167 | } |
| 1168 | |
| 1169 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1170 | { |
| 1171 | dt->limit = vmcs_read32(GUEST_GDTR_LIMIT); |
| 1172 | dt->base = vmcs_readl(GUEST_GDTR_BASE); |
| 1173 | } |
| 1174 | |
| 1175 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) |
| 1176 | { |
| 1177 | vmcs_write32(GUEST_GDTR_LIMIT, dt->limit); |
| 1178 | vmcs_writel(GUEST_GDTR_BASE, dt->base); |
| 1179 | } |
| 1180 | |
| 1181 | static int init_rmode_tss(struct kvm* kvm) |
| 1182 | { |
| 1183 | struct page *p1, *p2, *p3; |
| 1184 | gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT; |
| 1185 | char *page; |
| 1186 | |
Avi Kivity | 954bbbc2 | 2007-03-30 14:02:32 +0300 | [diff] [blame] | 1187 | p1 = gfn_to_page(kvm, fn++); |
| 1188 | p2 = gfn_to_page(kvm, fn++); |
| 1189 | p3 = gfn_to_page(kvm, fn); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1190 | |
| 1191 | if (!p1 || !p2 || !p3) { |
| 1192 | kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | page = kmap_atomic(p1, KM_USER0); |
Shani Moideen | a3870c4 | 2007-06-11 09:31:33 +0530 | [diff] [blame] | 1197 | clear_page(page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1198 | *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; |
| 1199 | kunmap_atomic(page, KM_USER0); |
| 1200 | |
| 1201 | page = kmap_atomic(p2, KM_USER0); |
Shani Moideen | a3870c4 | 2007-06-11 09:31:33 +0530 | [diff] [blame] | 1202 | clear_page(page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1203 | kunmap_atomic(page, KM_USER0); |
| 1204 | |
| 1205 | page = kmap_atomic(p3, KM_USER0); |
Shani Moideen | a3870c4 | 2007-06-11 09:31:33 +0530 | [diff] [blame] | 1206 | clear_page(page); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1207 | *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0; |
| 1208 | kunmap_atomic(page, KM_USER0); |
| 1209 | |
| 1210 | return 1; |
| 1211 | } |
| 1212 | |
| 1213 | static void vmcs_write32_fixedbits(u32 msr, u32 vmcs_field, u32 val) |
| 1214 | { |
| 1215 | u32 msr_high, msr_low; |
| 1216 | |
| 1217 | rdmsr(msr, msr_low, msr_high); |
| 1218 | |
| 1219 | val &= msr_high; |
| 1220 | val |= msr_low; |
| 1221 | vmcs_write32(vmcs_field, val); |
| 1222 | } |
| 1223 | |
| 1224 | static void seg_setup(int seg) |
| 1225 | { |
| 1226 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; |
| 1227 | |
| 1228 | vmcs_write16(sf->selector, 0); |
| 1229 | vmcs_writel(sf->base, 0); |
| 1230 | vmcs_write32(sf->limit, 0xffff); |
| 1231 | vmcs_write32(sf->ar_bytes, 0x93); |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * Sets up the vmcs for emulated real mode. |
| 1236 | */ |
| 1237 | static int vmx_vcpu_setup(struct kvm_vcpu *vcpu) |
| 1238 | { |
| 1239 | u32 host_sysenter_cs; |
| 1240 | u32 junk; |
| 1241 | unsigned long a; |
| 1242 | struct descriptor_table dt; |
| 1243 | int i; |
| 1244 | int ret = 0; |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 1245 | unsigned long kvm_vmx_return; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1246 | |
| 1247 | if (!init_rmode_tss(vcpu->kvm)) { |
| 1248 | ret = -ENOMEM; |
| 1249 | goto out; |
| 1250 | } |
| 1251 | |
| 1252 | memset(vcpu->regs, 0, sizeof(vcpu->regs)); |
| 1253 | vcpu->regs[VCPU_REGS_RDX] = get_rdx_init_val(); |
| 1254 | vcpu->cr8 = 0; |
Avi Kivity | 94cea1b | 2007-06-13 19:43:19 +0300 | [diff] [blame] | 1255 | vcpu->apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
Qing He | dad3795 | 2007-07-12 12:33:56 +0300 | [diff] [blame] | 1256 | if (vcpu->vcpu_id == 0) |
Avi Kivity | 94cea1b | 2007-06-13 19:43:19 +0300 | [diff] [blame] | 1257 | vcpu->apic_base |= MSR_IA32_APICBASE_BSP; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1258 | |
| 1259 | fx_init(vcpu); |
| 1260 | |
| 1261 | /* |
| 1262 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode |
| 1263 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. |
| 1264 | */ |
| 1265 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); |
| 1266 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); |
| 1267 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
| 1268 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); |
| 1269 | |
| 1270 | seg_setup(VCPU_SREG_DS); |
| 1271 | seg_setup(VCPU_SREG_ES); |
| 1272 | seg_setup(VCPU_SREG_FS); |
| 1273 | seg_setup(VCPU_SREG_GS); |
| 1274 | seg_setup(VCPU_SREG_SS); |
| 1275 | |
| 1276 | vmcs_write16(GUEST_TR_SELECTOR, 0); |
| 1277 | vmcs_writel(GUEST_TR_BASE, 0); |
| 1278 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); |
| 1279 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); |
| 1280 | |
| 1281 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); |
| 1282 | vmcs_writel(GUEST_LDTR_BASE, 0); |
| 1283 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); |
| 1284 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); |
| 1285 | |
| 1286 | vmcs_write32(GUEST_SYSENTER_CS, 0); |
| 1287 | vmcs_writel(GUEST_SYSENTER_ESP, 0); |
| 1288 | vmcs_writel(GUEST_SYSENTER_EIP, 0); |
| 1289 | |
| 1290 | vmcs_writel(GUEST_RFLAGS, 0x02); |
| 1291 | vmcs_writel(GUEST_RIP, 0xfff0); |
| 1292 | vmcs_writel(GUEST_RSP, 0); |
| 1293 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1294 | //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 |
| 1295 | vmcs_writel(GUEST_DR7, 0x400); |
| 1296 | |
| 1297 | vmcs_writel(GUEST_GDTR_BASE, 0); |
| 1298 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); |
| 1299 | |
| 1300 | vmcs_writel(GUEST_IDTR_BASE, 0); |
| 1301 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); |
| 1302 | |
| 1303 | vmcs_write32(GUEST_ACTIVITY_STATE, 0); |
| 1304 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); |
| 1305 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); |
| 1306 | |
| 1307 | /* I/O */ |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1308 | vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a)); |
| 1309 | vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1310 | |
| 1311 | guest_write_tsc(0); |
| 1312 | |
| 1313 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ |
| 1314 | |
| 1315 | /* Special registers */ |
| 1316 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); |
| 1317 | |
| 1318 | /* Control */ |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1319 | vmcs_write32_fixedbits(MSR_IA32_VMX_PINBASED_CTLS, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1320 | PIN_BASED_VM_EXEC_CONTROL, |
| 1321 | PIN_BASED_EXT_INTR_MASK /* 20.6.1 */ |
| 1322 | | PIN_BASED_NMI_EXITING /* 20.6.1 */ |
| 1323 | ); |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1324 | vmcs_write32_fixedbits(MSR_IA32_VMX_PROCBASED_CTLS, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1325 | CPU_BASED_VM_EXEC_CONTROL, |
| 1326 | CPU_BASED_HLT_EXITING /* 20.6.2 */ |
| 1327 | | CPU_BASED_CR8_LOAD_EXITING /* 20.6.2 */ |
| 1328 | | CPU_BASED_CR8_STORE_EXITING /* 20.6.2 */ |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 1329 | | CPU_BASED_ACTIVATE_IO_BITMAP /* 20.6.2 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1330 | | CPU_BASED_MOV_DR_EXITING |
| 1331 | | CPU_BASED_USE_TSC_OFFSETING /* 21.3 */ |
| 1332 | ); |
| 1333 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1334 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); |
| 1335 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); |
| 1336 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ |
| 1337 | |
| 1338 | vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */ |
| 1339 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ |
| 1340 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ |
| 1341 | |
| 1342 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ |
| 1343 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 1344 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
| 1345 | vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */ |
| 1346 | vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */ |
| 1347 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1348 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1349 | rdmsrl(MSR_FS_BASE, a); |
| 1350 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ |
| 1351 | rdmsrl(MSR_GS_BASE, a); |
| 1352 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ |
| 1353 | #else |
| 1354 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ |
| 1355 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ |
| 1356 | #endif |
| 1357 | |
| 1358 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ |
| 1359 | |
| 1360 | get_idt(&dt); |
| 1361 | vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */ |
| 1362 | |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 1363 | asm ("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return)); |
| 1364 | vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */ |
Eddie Dong | 2cc5156 | 2007-05-21 07:28:09 +0300 | [diff] [blame] | 1365 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0); |
| 1366 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0); |
| 1367 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1368 | |
| 1369 | rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk); |
| 1370 | vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs); |
| 1371 | rdmsrl(MSR_IA32_SYSENTER_ESP, a); |
| 1372 | vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */ |
| 1373 | rdmsrl(MSR_IA32_SYSENTER_EIP, a); |
| 1374 | vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */ |
| 1375 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1376 | for (i = 0; i < NR_VMX_MSR; ++i) { |
| 1377 | u32 index = vmx_msr_index[i]; |
| 1378 | u32 data_low, data_high; |
| 1379 | u64 data; |
| 1380 | int j = vcpu->nmsrs; |
| 1381 | |
| 1382 | if (rdmsr_safe(index, &data_low, &data_high) < 0) |
| 1383 | continue; |
Avi Kivity | 432bd6c | 2007-01-31 23:48:13 -0800 | [diff] [blame] | 1384 | if (wrmsr_safe(index, data_low, data_high) < 0) |
| 1385 | continue; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1386 | data = data_low | ((u64)data_high << 32); |
| 1387 | vcpu->host_msrs[j].index = index; |
| 1388 | vcpu->host_msrs[j].reserved = 0; |
| 1389 | vcpu->host_msrs[j].data = data; |
| 1390 | vcpu->guest_msrs[j] = vcpu->host_msrs[j]; |
| 1391 | ++vcpu->nmsrs; |
| 1392 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1393 | |
Avi Kivity | e38aea3 | 2007-04-19 13:22:48 +0300 | [diff] [blame] | 1394 | setup_msrs(vcpu); |
| 1395 | |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1396 | vmcs_write32_fixedbits(MSR_IA32_VMX_EXIT_CTLS, VM_EXIT_CONTROLS, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1397 | (HOST_IS_64 << 9)); /* 22.2,1, 20.7.1 */ |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1398 | |
| 1399 | /* 22.2.1, 20.8.1 */ |
Nguyen Anh Quynh | c68876f | 2006-12-29 16:49:54 -0800 | [diff] [blame] | 1400 | vmcs_write32_fixedbits(MSR_IA32_VMX_ENTRY_CTLS, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1401 | VM_ENTRY_CONTROLS, 0); |
| 1402 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ |
| 1403 | |
Michael Riepe | 3b99ab2 | 2006-12-13 00:34:15 -0800 | [diff] [blame] | 1404 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1405 | vmcs_writel(VIRTUAL_APIC_PAGE_ADDR, 0); |
| 1406 | vmcs_writel(TPR_THRESHOLD, 0); |
Michael Riepe | 3b99ab2 | 2006-12-13 00:34:15 -0800 | [diff] [blame] | 1407 | #endif |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1408 | |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1409 | vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1410 | vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK); |
| 1411 | |
| 1412 | vcpu->cr0 = 0x60000010; |
| 1413 | vmx_set_cr0(vcpu, vcpu->cr0); // enter rmode |
| 1414 | vmx_set_cr4(vcpu, 0); |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 1415 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1416 | vmx_set_efer(vcpu, 0); |
| 1417 | #endif |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1418 | vmx_fpu_activate(vcpu); |
Avi Kivity | abd3f2d | 2007-05-02 17:57:40 +0300 | [diff] [blame] | 1419 | update_exception_bitmap(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1420 | |
| 1421 | return 0; |
| 1422 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1423 | out: |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq) |
| 1428 | { |
| 1429 | u16 ent[2]; |
| 1430 | u16 cs; |
| 1431 | u16 ip; |
| 1432 | unsigned long flags; |
| 1433 | unsigned long ss_base = vmcs_readl(GUEST_SS_BASE); |
| 1434 | u16 sp = vmcs_readl(GUEST_RSP); |
| 1435 | u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT); |
| 1436 | |
Eric Sesterhenn / Snakebyte | 3964994 | 2007-04-09 16:15:05 +0200 | [diff] [blame] | 1437 | if (sp > ss_limit || sp < 6 ) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1438 | vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n", |
| 1439 | __FUNCTION__, |
| 1440 | vmcs_readl(GUEST_RSP), |
| 1441 | vmcs_readl(GUEST_SS_BASE), |
| 1442 | vmcs_read32(GUEST_SS_LIMIT)); |
| 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | if (kvm_read_guest(vcpu, irq * sizeof(ent), sizeof(ent), &ent) != |
| 1447 | sizeof(ent)) { |
| 1448 | vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__); |
| 1449 | return; |
| 1450 | } |
| 1451 | |
| 1452 | flags = vmcs_readl(GUEST_RFLAGS); |
| 1453 | cs = vmcs_readl(GUEST_CS_BASE) >> 4; |
| 1454 | ip = vmcs_readl(GUEST_RIP); |
| 1455 | |
| 1456 | |
| 1457 | if (kvm_write_guest(vcpu, ss_base + sp - 2, 2, &flags) != 2 || |
| 1458 | kvm_write_guest(vcpu, ss_base + sp - 4, 2, &cs) != 2 || |
| 1459 | kvm_write_guest(vcpu, ss_base + sp - 6, 2, &ip) != 2) { |
| 1460 | vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__); |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | vmcs_writel(GUEST_RFLAGS, flags & |
| 1465 | ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF)); |
| 1466 | vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ; |
| 1467 | vmcs_writel(GUEST_CS_BASE, ent[1] << 4); |
| 1468 | vmcs_writel(GUEST_RIP, ent[0]); |
| 1469 | vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6)); |
| 1470 | } |
| 1471 | |
| 1472 | static void kvm_do_inject_irq(struct kvm_vcpu *vcpu) |
| 1473 | { |
| 1474 | int word_index = __ffs(vcpu->irq_summary); |
| 1475 | int bit_index = __ffs(vcpu->irq_pending[word_index]); |
| 1476 | int irq = word_index * BITS_PER_LONG + bit_index; |
| 1477 | |
| 1478 | clear_bit(bit_index, &vcpu->irq_pending[word_index]); |
| 1479 | if (!vcpu->irq_pending[word_index]) |
| 1480 | clear_bit(word_index, &vcpu->irq_summary); |
| 1481 | |
| 1482 | if (vcpu->rmode.active) { |
| 1483 | inject_rmode_irq(vcpu, irq); |
| 1484 | return; |
| 1485 | } |
| 1486 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 1487 | irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); |
| 1488 | } |
| 1489 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1490 | |
| 1491 | static void do_interrupt_requests(struct kvm_vcpu *vcpu, |
| 1492 | struct kvm_run *kvm_run) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1493 | { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1494 | u32 cpu_based_vm_exec_control; |
| 1495 | |
| 1496 | vcpu->interrupt_window_open = |
| 1497 | ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) && |
| 1498 | (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0); |
| 1499 | |
| 1500 | if (vcpu->interrupt_window_open && |
| 1501 | vcpu->irq_summary && |
| 1502 | !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1503 | /* |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1504 | * If interrupts enabled, and not blocked by sti or mov ss. Good. |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1505 | */ |
| 1506 | kvm_do_inject_irq(vcpu); |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1507 | |
| 1508 | cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); |
| 1509 | if (!vcpu->interrupt_window_open && |
| 1510 | (vcpu->irq_summary || kvm_run->request_interrupt_window)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1511 | /* |
| 1512 | * Interrupts blocked. Wait for unblock. |
| 1513 | */ |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1514 | cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING; |
| 1515 | else |
| 1516 | cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING; |
| 1517 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu) |
| 1521 | { |
| 1522 | struct kvm_guest_debug *dbg = &vcpu->guest_debug; |
| 1523 | |
| 1524 | set_debugreg(dbg->bp[0], 0); |
| 1525 | set_debugreg(dbg->bp[1], 1); |
| 1526 | set_debugreg(dbg->bp[2], 2); |
| 1527 | set_debugreg(dbg->bp[3], 3); |
| 1528 | |
| 1529 | if (dbg->singlestep) { |
| 1530 | unsigned long flags; |
| 1531 | |
| 1532 | flags = vmcs_readl(GUEST_RFLAGS); |
| 1533 | flags |= X86_EFLAGS_TF | X86_EFLAGS_RF; |
| 1534 | vmcs_writel(GUEST_RFLAGS, flags); |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, |
| 1539 | int vec, u32 err_code) |
| 1540 | { |
| 1541 | if (!vcpu->rmode.active) |
| 1542 | return 0; |
| 1543 | |
Nitin A Kamble | b3f3770 | 2007-05-17 15:50:34 +0300 | [diff] [blame] | 1544 | /* |
| 1545 | * Instruction with address size override prefix opcode 0x67 |
| 1546 | * Cause the #SS fault with 0 error code in VM86 mode. |
| 1547 | */ |
| 1548 | if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1549 | if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE) |
| 1550 | return 1; |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1555 | { |
| 1556 | u32 intr_info, error_code; |
| 1557 | unsigned long cr2, rip; |
| 1558 | u32 vect_info; |
| 1559 | enum emulation_result er; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 1560 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1561 | |
| 1562 | vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 1563 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); |
| 1564 | |
| 1565 | if ((vect_info & VECTORING_INFO_VALID_MASK) && |
| 1566 | !is_page_fault(intr_info)) { |
| 1567 | printk(KERN_ERR "%s: unexpected, vectoring info 0x%x " |
| 1568 | "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info); |
| 1569 | } |
| 1570 | |
| 1571 | if (is_external_interrupt(vect_info)) { |
| 1572 | int irq = vect_info & VECTORING_INFO_VECTOR_MASK; |
| 1573 | set_bit(irq, vcpu->irq_pending); |
| 1574 | set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary); |
| 1575 | } |
| 1576 | |
| 1577 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */ |
| 1578 | asm ("int $2"); |
| 1579 | return 1; |
| 1580 | } |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1581 | |
| 1582 | if (is_no_device(intr_info)) { |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1583 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1584 | return 1; |
| 1585 | } |
| 1586 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1587 | error_code = 0; |
| 1588 | rip = vmcs_readl(GUEST_RIP); |
| 1589 | if (intr_info & INTR_INFO_DELIEVER_CODE_MASK) |
| 1590 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); |
| 1591 | if (is_page_fault(intr_info)) { |
| 1592 | cr2 = vmcs_readl(EXIT_QUALIFICATION); |
| 1593 | |
| 1594 | spin_lock(&vcpu->kvm->lock); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 1595 | r = kvm_mmu_page_fault(vcpu, cr2, error_code); |
| 1596 | if (r < 0) { |
| 1597 | spin_unlock(&vcpu->kvm->lock); |
| 1598 | return r; |
| 1599 | } |
| 1600 | if (!r) { |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1601 | spin_unlock(&vcpu->kvm->lock); |
| 1602 | return 1; |
| 1603 | } |
| 1604 | |
| 1605 | er = emulate_instruction(vcpu, kvm_run, cr2, error_code); |
| 1606 | spin_unlock(&vcpu->kvm->lock); |
| 1607 | |
| 1608 | switch (er) { |
| 1609 | case EMULATE_DONE: |
| 1610 | return 1; |
| 1611 | case EMULATE_DO_MMIO: |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1612 | ++vcpu->stat.mmio_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1613 | return 0; |
| 1614 | case EMULATE_FAIL: |
| 1615 | vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__); |
| 1616 | break; |
| 1617 | default: |
| 1618 | BUG(); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | if (vcpu->rmode.active && |
| 1623 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 1624 | error_code)) { |
| 1625 | if (vcpu->halt_request) { |
| 1626 | vcpu->halt_request = 0; |
| 1627 | return kvm_emulate_halt(vcpu); |
| 1628 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1629 | return 1; |
Avi Kivity | 72d6e5a | 2007-06-05 16:15:51 +0300 | [diff] [blame] | 1630 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1631 | |
| 1632 | if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) { |
| 1633 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
| 1634 | return 0; |
| 1635 | } |
| 1636 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; |
| 1637 | kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK; |
| 1638 | kvm_run->ex.error_code = error_code; |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | static int handle_external_interrupt(struct kvm_vcpu *vcpu, |
| 1643 | struct kvm_run *kvm_run) |
| 1644 | { |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1645 | ++vcpu->stat.irq_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1646 | return 1; |
| 1647 | } |
| 1648 | |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 1649 | static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1650 | { |
| 1651 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; |
| 1652 | return 0; |
| 1653 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1654 | |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1655 | static int get_io_count(struct kvm_vcpu *vcpu, unsigned long *count) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1656 | { |
| 1657 | u64 inst; |
| 1658 | gva_t rip; |
| 1659 | int countr_size; |
| 1660 | int i, n; |
| 1661 | |
| 1662 | if ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_VM)) { |
| 1663 | countr_size = 2; |
| 1664 | } else { |
| 1665 | u32 cs_ar = vmcs_read32(GUEST_CS_AR_BYTES); |
| 1666 | |
| 1667 | countr_size = (cs_ar & AR_L_MASK) ? 8: |
| 1668 | (cs_ar & AR_DB_MASK) ? 4: 2; |
| 1669 | } |
| 1670 | |
| 1671 | rip = vmcs_readl(GUEST_RIP); |
| 1672 | if (countr_size != 8) |
| 1673 | rip += vmcs_readl(GUEST_CS_BASE); |
| 1674 | |
| 1675 | n = kvm_read_guest(vcpu, rip, sizeof(inst), &inst); |
| 1676 | |
| 1677 | for (i = 0; i < n; i++) { |
| 1678 | switch (((u8*)&inst)[i]) { |
| 1679 | case 0xf0: |
| 1680 | case 0xf2: |
| 1681 | case 0xf3: |
| 1682 | case 0x2e: |
| 1683 | case 0x36: |
| 1684 | case 0x3e: |
| 1685 | case 0x26: |
| 1686 | case 0x64: |
| 1687 | case 0x65: |
| 1688 | case 0x66: |
| 1689 | break; |
| 1690 | case 0x67: |
| 1691 | countr_size = (countr_size == 2) ? 4: (countr_size >> 1); |
| 1692 | default: |
| 1693 | goto done; |
| 1694 | } |
| 1695 | } |
| 1696 | return 0; |
| 1697 | done: |
| 1698 | countr_size *= 8; |
| 1699 | *count = vcpu->regs[VCPU_REGS_RCX] & (~0ULL >> (64 - countr_size)); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1700 | //printk("cx: %lx\n", vcpu->regs[VCPU_REGS_RCX]); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1701 | return 1; |
| 1702 | } |
| 1703 | |
| 1704 | static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1705 | { |
| 1706 | u64 exit_qualification; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1707 | int size, down, in, string, rep; |
| 1708 | unsigned port; |
| 1709 | unsigned long count; |
| 1710 | gva_t address; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1711 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1712 | ++vcpu->stat.io_exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1713 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1714 | in = (exit_qualification & 8) != 0; |
| 1715 | size = (exit_qualification & 7) + 1; |
| 1716 | string = (exit_qualification & 16) != 0; |
| 1717 | down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0; |
| 1718 | count = 1; |
| 1719 | rep = (exit_qualification & 32) != 0; |
| 1720 | port = exit_qualification >> 16; |
| 1721 | address = 0; |
| 1722 | if (string) { |
| 1723 | if (rep && !get_io_count(vcpu, &count)) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1724 | return 1; |
Avi Kivity | 039576c | 2007-03-20 12:46:50 +0200 | [diff] [blame] | 1725 | address = vmcs_readl(GUEST_LINEAR_ADDRESS); |
| 1726 | } |
| 1727 | return kvm_setup_pio(vcpu, kvm_run, in, size, count, string, down, |
| 1728 | address, rep, port); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1729 | } |
| 1730 | |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1731 | static void |
| 1732 | vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) |
| 1733 | { |
| 1734 | /* |
| 1735 | * Patch in the VMCALL instruction: |
| 1736 | */ |
| 1737 | hypercall[0] = 0x0f; |
| 1738 | hypercall[1] = 0x01; |
| 1739 | hypercall[2] = 0xc1; |
| 1740 | hypercall[3] = 0xc3; |
| 1741 | } |
| 1742 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1743 | static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1744 | { |
| 1745 | u64 exit_qualification; |
| 1746 | int cr; |
| 1747 | int reg; |
| 1748 | |
| 1749 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 1750 | cr = exit_qualification & 15; |
| 1751 | reg = (exit_qualification >> 8) & 15; |
| 1752 | switch ((exit_qualification >> 4) & 3) { |
| 1753 | case 0: /* mov to cr */ |
| 1754 | switch (cr) { |
| 1755 | case 0: |
| 1756 | vcpu_load_rsp_rip(vcpu); |
| 1757 | set_cr0(vcpu, vcpu->regs[reg]); |
| 1758 | skip_emulated_instruction(vcpu); |
| 1759 | return 1; |
| 1760 | case 3: |
| 1761 | vcpu_load_rsp_rip(vcpu); |
| 1762 | set_cr3(vcpu, vcpu->regs[reg]); |
| 1763 | skip_emulated_instruction(vcpu); |
| 1764 | return 1; |
| 1765 | case 4: |
| 1766 | vcpu_load_rsp_rip(vcpu); |
| 1767 | set_cr4(vcpu, vcpu->regs[reg]); |
| 1768 | skip_emulated_instruction(vcpu); |
| 1769 | return 1; |
| 1770 | case 8: |
| 1771 | vcpu_load_rsp_rip(vcpu); |
| 1772 | set_cr8(vcpu, vcpu->regs[reg]); |
| 1773 | skip_emulated_instruction(vcpu); |
| 1774 | return 1; |
| 1775 | }; |
| 1776 | break; |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1777 | case 2: /* clts */ |
| 1778 | vcpu_load_rsp_rip(vcpu); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1779 | vmx_fpu_deactivate(vcpu); |
Rusty Russell | 707d92f | 2007-07-17 23:19:08 +1000 | [diff] [blame] | 1780 | vcpu->cr0 &= ~X86_CR0_TS; |
Anthony Liguori | 2ab455c | 2007-04-27 09:29:49 +0300 | [diff] [blame] | 1781 | vmcs_writel(CR0_READ_SHADOW, vcpu->cr0); |
Avi Kivity | 5fd86fc | 2007-05-02 20:40:00 +0300 | [diff] [blame] | 1782 | vmx_fpu_activate(vcpu); |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 1783 | skip_emulated_instruction(vcpu); |
| 1784 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1785 | case 1: /*mov from cr*/ |
| 1786 | switch (cr) { |
| 1787 | case 3: |
| 1788 | vcpu_load_rsp_rip(vcpu); |
| 1789 | vcpu->regs[reg] = vcpu->cr3; |
| 1790 | vcpu_put_rsp_rip(vcpu); |
| 1791 | skip_emulated_instruction(vcpu); |
| 1792 | return 1; |
| 1793 | case 8: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1794 | vcpu_load_rsp_rip(vcpu); |
| 1795 | vcpu->regs[reg] = vcpu->cr8; |
| 1796 | vcpu_put_rsp_rip(vcpu); |
| 1797 | skip_emulated_instruction(vcpu); |
| 1798 | return 1; |
| 1799 | } |
| 1800 | break; |
| 1801 | case 3: /* lmsw */ |
| 1802 | lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f); |
| 1803 | |
| 1804 | skip_emulated_instruction(vcpu); |
| 1805 | return 1; |
| 1806 | default: |
| 1807 | break; |
| 1808 | } |
| 1809 | kvm_run->exit_reason = 0; |
| 1810 | printk(KERN_ERR "kvm: unhandled control register: op %d cr %d\n", |
| 1811 | (int)(exit_qualification >> 4) & 3, cr); |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1816 | { |
| 1817 | u64 exit_qualification; |
| 1818 | unsigned long val; |
| 1819 | int dr, reg; |
| 1820 | |
| 1821 | /* |
| 1822 | * FIXME: this code assumes the host is debugging the guest. |
| 1823 | * need to deal with guest debugging itself too. |
| 1824 | */ |
| 1825 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); |
| 1826 | dr = exit_qualification & 7; |
| 1827 | reg = (exit_qualification >> 8) & 15; |
| 1828 | vcpu_load_rsp_rip(vcpu); |
| 1829 | if (exit_qualification & 16) { |
| 1830 | /* mov from dr */ |
| 1831 | switch (dr) { |
| 1832 | case 6: |
| 1833 | val = 0xffff0ff0; |
| 1834 | break; |
| 1835 | case 7: |
| 1836 | val = 0x400; |
| 1837 | break; |
| 1838 | default: |
| 1839 | val = 0; |
| 1840 | } |
| 1841 | vcpu->regs[reg] = val; |
| 1842 | } else { |
| 1843 | /* mov to dr */ |
| 1844 | } |
| 1845 | vcpu_put_rsp_rip(vcpu); |
| 1846 | skip_emulated_instruction(vcpu); |
| 1847 | return 1; |
| 1848 | } |
| 1849 | |
| 1850 | static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1851 | { |
Avi Kivity | 06465c5 | 2007-02-28 20:46:53 +0200 | [diff] [blame] | 1852 | kvm_emulate_cpuid(vcpu); |
| 1853 | return 1; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1857 | { |
| 1858 | u32 ecx = vcpu->regs[VCPU_REGS_RCX]; |
| 1859 | u64 data; |
| 1860 | |
| 1861 | if (vmx_get_msr(vcpu, ecx, &data)) { |
| 1862 | vmx_inject_gp(vcpu, 0); |
| 1863 | return 1; |
| 1864 | } |
| 1865 | |
| 1866 | /* FIXME: handling of bits 32:63 of rax, rdx */ |
| 1867 | vcpu->regs[VCPU_REGS_RAX] = data & -1u; |
| 1868 | vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u; |
| 1869 | skip_emulated_instruction(vcpu); |
| 1870 | return 1; |
| 1871 | } |
| 1872 | |
| 1873 | static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1874 | { |
| 1875 | u32 ecx = vcpu->regs[VCPU_REGS_RCX]; |
| 1876 | u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u) |
| 1877 | | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32); |
| 1878 | |
| 1879 | if (vmx_set_msr(vcpu, ecx, data) != 0) { |
| 1880 | vmx_inject_gp(vcpu, 0); |
| 1881 | return 1; |
| 1882 | } |
| 1883 | |
| 1884 | skip_emulated_instruction(vcpu); |
| 1885 | return 1; |
| 1886 | } |
| 1887 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1888 | static void post_kvm_run_save(struct kvm_vcpu *vcpu, |
| 1889 | struct kvm_run *kvm_run) |
| 1890 | { |
| 1891 | kvm_run->if_flag = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) != 0; |
| 1892 | kvm_run->cr8 = vcpu->cr8; |
| 1893 | kvm_run->apic_base = vcpu->apic_base; |
| 1894 | kvm_run->ready_for_interrupt_injection = (vcpu->interrupt_window_open && |
| 1895 | vcpu->irq_summary == 0); |
| 1896 | } |
| 1897 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1898 | static int handle_interrupt_window(struct kvm_vcpu *vcpu, |
| 1899 | struct kvm_run *kvm_run) |
| 1900 | { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1901 | /* |
| 1902 | * If the user space waits to inject interrupts, exit as soon as |
| 1903 | * possible |
| 1904 | */ |
| 1905 | if (kvm_run->request_interrupt_window && |
Dor Laor | 022a930 | 2007-01-05 16:37:00 -0800 | [diff] [blame] | 1906 | !vcpu->irq_summary) { |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1907 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 1908 | ++vcpu->stat.irq_window_exits; |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1909 | return 0; |
| 1910 | } |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1911 | return 1; |
| 1912 | } |
| 1913 | |
| 1914 | static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1915 | { |
| 1916 | skip_emulated_instruction(vcpu); |
Avi Kivity | d3bef15 | 2007-06-05 15:53:05 +0300 | [diff] [blame] | 1917 | return kvm_emulate_halt(vcpu); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1918 | } |
| 1919 | |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1920 | static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1921 | { |
Dor Laor | 510043d | 2007-02-19 18:25:43 +0200 | [diff] [blame] | 1922 | skip_emulated_instruction(vcpu); |
Avi Kivity | 270fd9b | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1923 | return kvm_hypercall(vcpu, kvm_run); |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1924 | } |
| 1925 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1926 | /* |
| 1927 | * The exit handlers return 1 if the exit was handled fully and guest execution |
| 1928 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs |
| 1929 | * to be done to userspace and return 0. |
| 1930 | */ |
| 1931 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu, |
| 1932 | struct kvm_run *kvm_run) = { |
| 1933 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, |
| 1934 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, |
Avi Kivity | 988ad74 | 2007-02-12 00:54:36 -0800 | [diff] [blame] | 1935 | [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1936 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1937 | [EXIT_REASON_CR_ACCESS] = handle_cr, |
| 1938 | [EXIT_REASON_DR_ACCESS] = handle_dr, |
| 1939 | [EXIT_REASON_CPUID] = handle_cpuid, |
| 1940 | [EXIT_REASON_MSR_READ] = handle_rdmsr, |
| 1941 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, |
| 1942 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, |
| 1943 | [EXIT_REASON_HLT] = handle_halt, |
Ingo Molnar | c21415e | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 1944 | [EXIT_REASON_VMCALL] = handle_vmcall, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1945 | }; |
| 1946 | |
| 1947 | static const int kvm_vmx_max_exit_handlers = |
Robert P. J. Day | 50a3485 | 2007-06-03 13:35:29 -0400 | [diff] [blame] | 1948 | ARRAY_SIZE(kvm_vmx_exit_handlers); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1949 | |
| 1950 | /* |
| 1951 | * The guest has exited. See if we can fix it or if we need userspace |
| 1952 | * assistance. |
| 1953 | */ |
| 1954 | static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 1955 | { |
| 1956 | u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 1957 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); |
| 1958 | |
| 1959 | if ( (vectoring_info & VECTORING_INFO_VALID_MASK) && |
| 1960 | exit_reason != EXIT_REASON_EXCEPTION_NMI ) |
| 1961 | printk(KERN_WARNING "%s: unexpected, valid vectoring info and " |
| 1962 | "exit reason is 0x%x\n", __FUNCTION__, exit_reason); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1963 | if (exit_reason < kvm_vmx_max_exit_handlers |
| 1964 | && kvm_vmx_exit_handlers[exit_reason]) |
| 1965 | return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run); |
| 1966 | else { |
| 1967 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1968 | kvm_run->hw.hardware_exit_reason = exit_reason; |
| 1969 | } |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 1973 | /* |
| 1974 | * Check if userspace requested an interrupt window, and that the |
| 1975 | * interrupt window is open. |
| 1976 | * |
| 1977 | * No need to exit to userspace if we already have an interrupt queued. |
| 1978 | */ |
| 1979 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu, |
| 1980 | struct kvm_run *kvm_run) |
| 1981 | { |
| 1982 | return (!vcpu->irq_summary && |
| 1983 | kvm_run->request_interrupt_window && |
| 1984 | vcpu->interrupt_window_open && |
| 1985 | (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF)); |
| 1986 | } |
| 1987 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 1988 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) |
| 1989 | { |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 1990 | } |
| 1991 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1992 | static int vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) |
| 1993 | { |
| 1994 | u8 fail; |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 1995 | int r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1996 | |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 1997 | preempted: |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 1998 | if (vcpu->guest_debug.enabled) |
| 1999 | kvm_guest_debug_pre(vcpu); |
| 2000 | |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 2001 | again: |
Shaohua Li | 9ae0448 | 2007-07-23 14:51:32 +0800 | [diff] [blame^] | 2002 | r = kvm_mmu_reload(vcpu); |
| 2003 | if (unlikely(r)) |
| 2004 | goto out; |
| 2005 | |
Gregory Haskins | ff1dc79 | 2007-05-31 14:08:58 -0400 | [diff] [blame] | 2006 | if (!vcpu->mmio_read_completed) |
| 2007 | do_interrupt_requests(vcpu, kvm_run); |
| 2008 | |
Avi Kivity | 33ed632 | 2007-05-02 16:54:03 +0300 | [diff] [blame] | 2009 | vmx_save_host_state(vcpu); |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 2010 | kvm_load_guest_fpu(vcpu); |
| 2011 | |
| 2012 | /* |
| 2013 | * Loading guest fpu may have cleared host cr0.ts |
| 2014 | */ |
| 2015 | vmcs_writel(HOST_CR0, read_cr0()); |
| 2016 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 2017 | local_irq_disable(); |
| 2018 | |
| 2019 | vcpu->guest_mode = 1; |
| 2020 | if (vcpu->requests) |
| 2021 | if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests)) |
| 2022 | vmx_flush_tlb(vcpu); |
| 2023 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2024 | asm ( |
| 2025 | /* Store host registers */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2026 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2027 | "push %%rax; push %%rbx; push %%rdx;" |
| 2028 | "push %%rsi; push %%rdi; push %%rbp;" |
| 2029 | "push %%r8; push %%r9; push %%r10; push %%r11;" |
| 2030 | "push %%r12; push %%r13; push %%r14; push %%r15;" |
| 2031 | "push %%rcx \n\t" |
| 2032 | ASM_VMX_VMWRITE_RSP_RDX "\n\t" |
| 2033 | #else |
| 2034 | "pusha; push %%ecx \n\t" |
| 2035 | ASM_VMX_VMWRITE_RSP_RDX "\n\t" |
| 2036 | #endif |
| 2037 | /* Check if vmlaunch of vmresume is needed */ |
| 2038 | "cmp $0, %1 \n\t" |
| 2039 | /* Load guest registers. Don't clobber flags. */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2040 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2041 | "mov %c[cr2](%3), %%rax \n\t" |
| 2042 | "mov %%rax, %%cr2 \n\t" |
| 2043 | "mov %c[rax](%3), %%rax \n\t" |
| 2044 | "mov %c[rbx](%3), %%rbx \n\t" |
| 2045 | "mov %c[rdx](%3), %%rdx \n\t" |
| 2046 | "mov %c[rsi](%3), %%rsi \n\t" |
| 2047 | "mov %c[rdi](%3), %%rdi \n\t" |
| 2048 | "mov %c[rbp](%3), %%rbp \n\t" |
| 2049 | "mov %c[r8](%3), %%r8 \n\t" |
| 2050 | "mov %c[r9](%3), %%r9 \n\t" |
| 2051 | "mov %c[r10](%3), %%r10 \n\t" |
| 2052 | "mov %c[r11](%3), %%r11 \n\t" |
| 2053 | "mov %c[r12](%3), %%r12 \n\t" |
| 2054 | "mov %c[r13](%3), %%r13 \n\t" |
| 2055 | "mov %c[r14](%3), %%r14 \n\t" |
| 2056 | "mov %c[r15](%3), %%r15 \n\t" |
| 2057 | "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */ |
| 2058 | #else |
| 2059 | "mov %c[cr2](%3), %%eax \n\t" |
| 2060 | "mov %%eax, %%cr2 \n\t" |
| 2061 | "mov %c[rax](%3), %%eax \n\t" |
| 2062 | "mov %c[rbx](%3), %%ebx \n\t" |
| 2063 | "mov %c[rdx](%3), %%edx \n\t" |
| 2064 | "mov %c[rsi](%3), %%esi \n\t" |
| 2065 | "mov %c[rdi](%3), %%edi \n\t" |
| 2066 | "mov %c[rbp](%3), %%ebp \n\t" |
| 2067 | "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */ |
| 2068 | #endif |
| 2069 | /* Enter guest mode */ |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2070 | "jne .Llaunched \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2071 | ASM_VMX_VMLAUNCH "\n\t" |
Avi Kivity | cd2276a | 2007-05-14 20:41:13 +0300 | [diff] [blame] | 2072 | "jmp .Lkvm_vmx_return \n\t" |
| 2073 | ".Llaunched: " ASM_VMX_VMRESUME "\n\t" |
| 2074 | ".Lkvm_vmx_return: " |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2075 | /* Save guest registers, load host registers, keep flags */ |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2076 | #ifdef CONFIG_X86_64 |
Ingo Molnar | 9695823 | 2007-02-12 00:54:33 -0800 | [diff] [blame] | 2077 | "xchg %3, (%%rsp) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2078 | "mov %%rax, %c[rax](%3) \n\t" |
| 2079 | "mov %%rbx, %c[rbx](%3) \n\t" |
Ingo Molnar | 9695823 | 2007-02-12 00:54:33 -0800 | [diff] [blame] | 2080 | "pushq (%%rsp); popq %c[rcx](%3) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2081 | "mov %%rdx, %c[rdx](%3) \n\t" |
| 2082 | "mov %%rsi, %c[rsi](%3) \n\t" |
| 2083 | "mov %%rdi, %c[rdi](%3) \n\t" |
| 2084 | "mov %%rbp, %c[rbp](%3) \n\t" |
| 2085 | "mov %%r8, %c[r8](%3) \n\t" |
| 2086 | "mov %%r9, %c[r9](%3) \n\t" |
| 2087 | "mov %%r10, %c[r10](%3) \n\t" |
| 2088 | "mov %%r11, %c[r11](%3) \n\t" |
| 2089 | "mov %%r12, %c[r12](%3) \n\t" |
| 2090 | "mov %%r13, %c[r13](%3) \n\t" |
| 2091 | "mov %%r14, %c[r14](%3) \n\t" |
| 2092 | "mov %%r15, %c[r15](%3) \n\t" |
| 2093 | "mov %%cr2, %%rax \n\t" |
| 2094 | "mov %%rax, %c[cr2](%3) \n\t" |
Ingo Molnar | 9695823 | 2007-02-12 00:54:33 -0800 | [diff] [blame] | 2095 | "mov (%%rsp), %3 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2096 | |
| 2097 | "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;" |
| 2098 | "pop %%r11; pop %%r10; pop %%r9; pop %%r8;" |
| 2099 | "pop %%rbp; pop %%rdi; pop %%rsi;" |
| 2100 | "pop %%rdx; pop %%rbx; pop %%rax \n\t" |
| 2101 | #else |
Ingo Molnar | 9695823 | 2007-02-12 00:54:33 -0800 | [diff] [blame] | 2102 | "xchg %3, (%%esp) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2103 | "mov %%eax, %c[rax](%3) \n\t" |
| 2104 | "mov %%ebx, %c[rbx](%3) \n\t" |
Ingo Molnar | 9695823 | 2007-02-12 00:54:33 -0800 | [diff] [blame] | 2105 | "pushl (%%esp); popl %c[rcx](%3) \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2106 | "mov %%edx, %c[rdx](%3) \n\t" |
| 2107 | "mov %%esi, %c[rsi](%3) \n\t" |
| 2108 | "mov %%edi, %c[rdi](%3) \n\t" |
| 2109 | "mov %%ebp, %c[rbp](%3) \n\t" |
| 2110 | "mov %%cr2, %%eax \n\t" |
| 2111 | "mov %%eax, %c[cr2](%3) \n\t" |
Ingo Molnar | 9695823 | 2007-02-12 00:54:33 -0800 | [diff] [blame] | 2112 | "mov (%%esp), %3 \n\t" |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2113 | |
| 2114 | "pop %%ecx; popa \n\t" |
| 2115 | #endif |
| 2116 | "setbe %0 \n\t" |
Herbert Xu | e001548 | 2007-01-23 14:10:00 +1100 | [diff] [blame] | 2117 | : "=q" (fail) |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2118 | : "r"(vcpu->launched), "d"((unsigned long)HOST_RSP), |
| 2119 | "c"(vcpu), |
| 2120 | [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])), |
| 2121 | [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])), |
| 2122 | [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])), |
| 2123 | [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])), |
| 2124 | [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])), |
| 2125 | [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])), |
| 2126 | [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])), |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2127 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2128 | [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])), |
| 2129 | [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])), |
| 2130 | [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])), |
| 2131 | [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])), |
| 2132 | [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])), |
| 2133 | [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])), |
| 2134 | [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])), |
| 2135 | [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])), |
| 2136 | #endif |
| 2137 | [cr2]"i"(offsetof(struct kvm_vcpu, cr2)) |
| 2138 | : "cc", "memory" ); |
| 2139 | |
Avi Kivity | d9e368d | 2007-06-07 19:18:30 +0300 | [diff] [blame] | 2140 | vcpu->guest_mode = 0; |
| 2141 | local_irq_enable(); |
| 2142 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2143 | ++vcpu->stat.exits; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2144 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2145 | vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2146 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2147 | asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2148 | |
Avi Kivity | 05e0c8c | 2007-04-30 16:15:58 +0300 | [diff] [blame] | 2149 | if (unlikely(fail)) { |
Avi Kivity | 8eb7d33 | 2007-03-04 14:17:08 +0200 | [diff] [blame] | 2150 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 2151 | kvm_run->fail_entry.hardware_entry_failure_reason |
| 2152 | = vmcs_read32(VM_INSTRUCTION_ERROR); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2153 | r = 0; |
Avi Kivity | 05e0c8c | 2007-04-30 16:15:58 +0300 | [diff] [blame] | 2154 | goto out; |
| 2155 | } |
| 2156 | /* |
| 2157 | * Profile KVM exit RIPs: |
| 2158 | */ |
| 2159 | if (unlikely(prof_on == KVM_PROFILING)) |
| 2160 | profile_hit(KVM_PROFILING, (void *)vmcs_readl(GUEST_RIP)); |
Jeremy Fitzhardinge | 464d1a7 | 2007-02-13 13:26:20 +0100 | [diff] [blame] | 2161 | |
Avi Kivity | 05e0c8c | 2007-04-30 16:15:58 +0300 | [diff] [blame] | 2162 | vcpu->launched = 1; |
| 2163 | r = kvm_handle_exit(kvm_run, vcpu); |
| 2164 | if (r > 0) { |
| 2165 | /* Give scheduler a change to reschedule. */ |
| 2166 | if (signal_pending(current)) { |
| 2167 | r = -EINTR; |
| 2168 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 2169 | ++vcpu->stat.signal_exits; |
| 2170 | goto out; |
| 2171 | } |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2172 | |
Avi Kivity | 05e0c8c | 2007-04-30 16:15:58 +0300 | [diff] [blame] | 2173 | if (dm_request_for_irq_injection(vcpu, kvm_run)) { |
| 2174 | r = -EINTR; |
| 2175 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 2176 | ++vcpu->stat.request_irq_exits; |
| 2177 | goto out; |
| 2178 | } |
| 2179 | if (!need_resched()) { |
| 2180 | ++vcpu->stat.light_exits; |
| 2181 | goto again; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2182 | } |
| 2183 | } |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2184 | |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 2185 | out: |
Avi Kivity | e6adf28 | 2007-04-30 16:07:54 +0300 | [diff] [blame] | 2186 | if (r > 0) { |
| 2187 | kvm_resched(vcpu); |
| 2188 | goto preempted; |
| 2189 | } |
| 2190 | |
Dor Laor | c1150d8 | 2007-01-05 16:36:24 -0800 | [diff] [blame] | 2191 | post_kvm_run_save(vcpu, kvm_run); |
Avi Kivity | e2dec93 | 2007-01-05 16:36:54 -0800 | [diff] [blame] | 2192 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2193 | } |
| 2194 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2195 | static void vmx_inject_page_fault(struct kvm_vcpu *vcpu, |
| 2196 | unsigned long addr, |
| 2197 | u32 err_code) |
| 2198 | { |
| 2199 | u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); |
| 2200 | |
Avi Kivity | 1165f5f | 2007-04-19 17:27:43 +0300 | [diff] [blame] | 2201 | ++vcpu->stat.pf_guest; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2202 | |
| 2203 | if (is_page_fault(vect_info)) { |
| 2204 | printk(KERN_DEBUG "inject_page_fault: " |
| 2205 | "double fault 0x%lx @ 0x%lx\n", |
| 2206 | addr, vmcs_readl(GUEST_RIP)); |
| 2207 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0); |
| 2208 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2209 | DF_VECTOR | |
| 2210 | INTR_TYPE_EXCEPTION | |
| 2211 | INTR_INFO_DELIEVER_CODE_MASK | |
| 2212 | INTR_INFO_VALID_MASK); |
| 2213 | return; |
| 2214 | } |
| 2215 | vcpu->cr2 = addr; |
| 2216 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code); |
| 2217 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, |
| 2218 | PF_VECTOR | |
| 2219 | INTR_TYPE_EXCEPTION | |
| 2220 | INTR_INFO_DELIEVER_CODE_MASK | |
| 2221 | INTR_INFO_VALID_MASK); |
| 2222 | |
| 2223 | } |
| 2224 | |
| 2225 | static void vmx_free_vmcs(struct kvm_vcpu *vcpu) |
| 2226 | { |
| 2227 | if (vcpu->vmcs) { |
| 2228 | on_each_cpu(__vcpu_clear, vcpu, 0, 1); |
| 2229 | free_vmcs(vcpu->vmcs); |
| 2230 | vcpu->vmcs = NULL; |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) |
| 2235 | { |
| 2236 | vmx_free_vmcs(vcpu); |
| 2237 | } |
| 2238 | |
| 2239 | static int vmx_create_vcpu(struct kvm_vcpu *vcpu) |
| 2240 | { |
| 2241 | struct vmcs *vmcs; |
| 2242 | |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2243 | vcpu->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2244 | if (!vcpu->guest_msrs) |
| 2245 | return -ENOMEM; |
| 2246 | |
| 2247 | vcpu->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 2248 | if (!vcpu->host_msrs) |
| 2249 | goto out_free_guest_msrs; |
| 2250 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2251 | vmcs = alloc_vmcs(); |
| 2252 | if (!vmcs) |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2253 | goto out_free_msrs; |
| 2254 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2255 | vmcs_clear(vmcs); |
| 2256 | vcpu->vmcs = vmcs; |
| 2257 | vcpu->launched = 0; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2258 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2259 | return 0; |
Ingo Molnar | 965b58a | 2007-01-05 16:36:23 -0800 | [diff] [blame] | 2260 | |
| 2261 | out_free_msrs: |
| 2262 | kfree(vcpu->host_msrs); |
| 2263 | vcpu->host_msrs = NULL; |
| 2264 | |
| 2265 | out_free_guest_msrs: |
| 2266 | kfree(vcpu->guest_msrs); |
| 2267 | vcpu->guest_msrs = NULL; |
| 2268 | |
| 2269 | return -ENOMEM; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | static struct kvm_arch_ops vmx_arch_ops = { |
| 2273 | .cpu_has_kvm_support = cpu_has_kvm_support, |
| 2274 | .disabled_by_bios = vmx_disabled_by_bios, |
| 2275 | .hardware_setup = hardware_setup, |
| 2276 | .hardware_unsetup = hardware_unsetup, |
| 2277 | .hardware_enable = hardware_enable, |
| 2278 | .hardware_disable = hardware_disable, |
| 2279 | |
| 2280 | .vcpu_create = vmx_create_vcpu, |
| 2281 | .vcpu_free = vmx_free_vcpu, |
| 2282 | |
| 2283 | .vcpu_load = vmx_vcpu_load, |
| 2284 | .vcpu_put = vmx_vcpu_put, |
Avi Kivity | 774c47f | 2007-02-12 00:54:47 -0800 | [diff] [blame] | 2285 | .vcpu_decache = vmx_vcpu_decache, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2286 | |
| 2287 | .set_guest_debug = set_guest_debug, |
| 2288 | .get_msr = vmx_get_msr, |
| 2289 | .set_msr = vmx_set_msr, |
| 2290 | .get_segment_base = vmx_get_segment_base, |
| 2291 | .get_segment = vmx_get_segment, |
| 2292 | .set_segment = vmx_set_segment, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2293 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
Anthony Liguori | 25c4c27 | 2007-04-27 09:29:21 +0300 | [diff] [blame] | 2294 | .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2295 | .set_cr0 = vmx_set_cr0, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2296 | .set_cr3 = vmx_set_cr3, |
| 2297 | .set_cr4 = vmx_set_cr4, |
Avi Kivity | 05b3e0c | 2006-12-13 00:33:45 -0800 | [diff] [blame] | 2298 | #ifdef CONFIG_X86_64 |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2299 | .set_efer = vmx_set_efer, |
| 2300 | #endif |
| 2301 | .get_idt = vmx_get_idt, |
| 2302 | .set_idt = vmx_set_idt, |
| 2303 | .get_gdt = vmx_get_gdt, |
| 2304 | .set_gdt = vmx_set_gdt, |
| 2305 | .cache_regs = vcpu_load_rsp_rip, |
| 2306 | .decache_regs = vcpu_put_rsp_rip, |
| 2307 | .get_rflags = vmx_get_rflags, |
| 2308 | .set_rflags = vmx_set_rflags, |
| 2309 | |
| 2310 | .tlb_flush = vmx_flush_tlb, |
| 2311 | .inject_page_fault = vmx_inject_page_fault, |
| 2312 | |
| 2313 | .inject_gp = vmx_inject_gp, |
| 2314 | |
| 2315 | .run = vmx_vcpu_run, |
| 2316 | .skip_emulated_instruction = skip_emulated_instruction, |
| 2317 | .vcpu_setup = vmx_vcpu_setup, |
Ingo Molnar | 102d832 | 2007-02-19 14:37:47 +0200 | [diff] [blame] | 2318 | .patch_hypercall = vmx_patch_hypercall, |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2319 | }; |
| 2320 | |
| 2321 | static int __init vmx_init(void) |
| 2322 | { |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2323 | void *iova; |
| 2324 | int r; |
| 2325 | |
| 2326 | vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 2327 | if (!vmx_io_bitmap_a) |
| 2328 | return -ENOMEM; |
| 2329 | |
| 2330 | vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM); |
| 2331 | if (!vmx_io_bitmap_b) { |
| 2332 | r = -ENOMEM; |
| 2333 | goto out; |
| 2334 | } |
| 2335 | |
| 2336 | /* |
| 2337 | * Allow direct access to the PC debug port (it is often used for I/O |
| 2338 | * delays, but the vmexits simply slow things down). |
| 2339 | */ |
| 2340 | iova = kmap(vmx_io_bitmap_a); |
| 2341 | memset(iova, 0xff, PAGE_SIZE); |
| 2342 | clear_bit(0x80, iova); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 2343 | kunmap(vmx_io_bitmap_a); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2344 | |
| 2345 | iova = kmap(vmx_io_bitmap_b); |
| 2346 | memset(iova, 0xff, PAGE_SIZE); |
Avi Kivity | cd0536d | 2007-05-08 11:34:07 +0300 | [diff] [blame] | 2347 | kunmap(vmx_io_bitmap_b); |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2348 | |
| 2349 | r = kvm_init_arch(&vmx_arch_ops, THIS_MODULE); |
| 2350 | if (r) |
| 2351 | goto out1; |
| 2352 | |
| 2353 | return 0; |
| 2354 | |
| 2355 | out1: |
| 2356 | __free_page(vmx_io_bitmap_b); |
| 2357 | out: |
| 2358 | __free_page(vmx_io_bitmap_a); |
| 2359 | return r; |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | static void __exit vmx_exit(void) |
| 2363 | { |
He, Qing | fdef3ad | 2007-04-30 09:45:24 +0300 | [diff] [blame] | 2364 | __free_page(vmx_io_bitmap_b); |
| 2365 | __free_page(vmx_io_bitmap_a); |
| 2366 | |
Avi Kivity | 6aa8b73 | 2006-12-10 02:21:36 -0800 | [diff] [blame] | 2367 | kvm_exit_arch(); |
| 2368 | } |
| 2369 | |
| 2370 | module_init(vmx_init) |
| 2371 | module_exit(vmx_exit) |