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