blob: 34171d9008ffafa115e6ad813f0a3b1975424b2b [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
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 Kivity6aa8b732006-12-10 02:21:36 -080020#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020021#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/mm.h>
23#include <linux/highmem.h>
Ingo Molnar07031e12007-01-10 23:15:38 -080024#include <linux/profile.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040025#include <linux/sched.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080027#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
29#include "segment_descriptor.h"
30
Avi Kivity6aa8b732006-12-10 02:21:36 -080031MODULE_AUTHOR("Qumranet");
32MODULE_LICENSE("GPL");
33
34static DEFINE_PER_CPU(struct vmcs *, vmxarea);
35static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
36
He, Qingfdef3ad2007-04-30 09:45:24 +030037static struct page *vmx_io_bitmap_a;
38static struct page *vmx_io_bitmap_b;
39
Avi Kivity05b3e0c2006-12-13 00:33:45 -080040#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080041#define HOST_IS_64 1
42#else
43#define HOST_IS_64 0
44#endif
45
46static struct vmcs_descriptor {
47 int size;
48 int order;
49 u32 revision_id;
50} vmcs_descriptor;
51
52#define VMX_SEGMENT_FIELD(seg) \
53 [VCPU_SREG_##seg] = { \
54 .selector = GUEST_##seg##_SELECTOR, \
55 .base = GUEST_##seg##_BASE, \
56 .limit = GUEST_##seg##_LIMIT, \
57 .ar_bytes = GUEST_##seg##_AR_BYTES, \
58 }
59
60static struct kvm_vmx_segment_field {
61 unsigned selector;
62 unsigned base;
63 unsigned limit;
64 unsigned ar_bytes;
65} kvm_vmx_segment_fields[] = {
66 VMX_SEGMENT_FIELD(CS),
67 VMX_SEGMENT_FIELD(DS),
68 VMX_SEGMENT_FIELD(ES),
69 VMX_SEGMENT_FIELD(FS),
70 VMX_SEGMENT_FIELD(GS),
71 VMX_SEGMENT_FIELD(SS),
72 VMX_SEGMENT_FIELD(TR),
73 VMX_SEGMENT_FIELD(LDTR),
74};
75
Avi Kivity4d56c8a2007-04-19 14:28:44 +030076/*
77 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
78 * away by decrementing the array size.
79 */
Avi Kivity6aa8b732006-12-10 02:21:36 -080080static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -080081#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -080082 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
83#endif
84 MSR_EFER, MSR_K6_STAR,
85};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020086#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -080087
Avi Kivity2345df82007-04-17 15:30:24 +030088#ifdef CONFIG_X86_64
89static unsigned msr_offset_kernel_gs_base;
Avi Kivitye38aea32007-04-19 13:22:48 +030090#define NR_64BIT_MSRS 4
Avi Kivity35cc7f92007-04-19 13:26:39 +030091/*
92 * avoid save/load MSR_SYSCALL_MASK and MSR_LSTAR by std vt
93 * mechanism (cpu bug AA24)
94 */
95#define NR_BAD_MSRS 2
Avi Kivitye38aea32007-04-19 13:22:48 +030096#else
97#define NR_64BIT_MSRS 0
Avi Kivity35cc7f92007-04-19 13:26:39 +030098#define NR_BAD_MSRS 0
Avi Kivity2345df82007-04-17 15:30:24 +030099#endif
100
Avi Kivity6aa8b732006-12-10 02:21:36 -0800101static inline int is_page_fault(u32 intr_info)
102{
103 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
104 INTR_INFO_VALID_MASK)) ==
105 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
106}
107
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300108static inline int is_no_device(u32 intr_info)
109{
110 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
111 INTR_INFO_VALID_MASK)) ==
112 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
113}
114
Avi Kivity6aa8b732006-12-10 02:21:36 -0800115static inline int is_external_interrupt(u32 intr_info)
116{
117 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
118 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
119}
120
Avi Kivity7725f0b2006-12-13 00:34:01 -0800121static struct vmx_msr_entry *find_msr_entry(struct kvm_vcpu *vcpu, u32 msr)
122{
123 int i;
124
125 for (i = 0; i < vcpu->nmsrs; ++i)
126 if (vcpu->guest_msrs[i].index == msr)
127 return &vcpu->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000128 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800129}
130
Avi Kivity6aa8b732006-12-10 02:21:36 -0800131static void vmcs_clear(struct vmcs *vmcs)
132{
133 u64 phys_addr = __pa(vmcs);
134 u8 error;
135
136 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
137 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
138 : "cc", "memory");
139 if (error)
140 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
141 vmcs, phys_addr);
142}
143
144static void __vcpu_clear(void *arg)
145{
146 struct kvm_vcpu *vcpu = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800147 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148
149 if (vcpu->cpu == cpu)
150 vmcs_clear(vcpu->vmcs);
151 if (per_cpu(current_vmcs, cpu) == vcpu->vmcs)
152 per_cpu(current_vmcs, cpu) = NULL;
153}
154
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800155static void vcpu_clear(struct kvm_vcpu *vcpu)
156{
157 if (vcpu->cpu != raw_smp_processor_id() && vcpu->cpu != -1)
158 smp_call_function_single(vcpu->cpu, __vcpu_clear, vcpu, 0, 1);
159 else
160 __vcpu_clear(vcpu);
161 vcpu->launched = 0;
162}
163
Avi Kivity6aa8b732006-12-10 02:21:36 -0800164static unsigned long vmcs_readl(unsigned long field)
165{
166 unsigned long value;
167
168 asm volatile (ASM_VMX_VMREAD_RDX_RAX
169 : "=a"(value) : "d"(field) : "cc");
170 return value;
171}
172
173static u16 vmcs_read16(unsigned long field)
174{
175 return vmcs_readl(field);
176}
177
178static u32 vmcs_read32(unsigned long field)
179{
180 return vmcs_readl(field);
181}
182
183static u64 vmcs_read64(unsigned long field)
184{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800185#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 return vmcs_readl(field);
187#else
188 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
189#endif
190}
191
Avi Kivitye52de1b2007-01-05 16:36:56 -0800192static noinline void vmwrite_error(unsigned long field, unsigned long value)
193{
194 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
195 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
196 dump_stack();
197}
198
Avi Kivity6aa8b732006-12-10 02:21:36 -0800199static void vmcs_writel(unsigned long field, unsigned long value)
200{
201 u8 error;
202
203 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
204 : "=q"(error) : "a"(value), "d"(field) : "cc" );
Avi Kivitye52de1b2007-01-05 16:36:56 -0800205 if (unlikely(error))
206 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207}
208
209static void vmcs_write16(unsigned long field, u16 value)
210{
211 vmcs_writel(field, value);
212}
213
214static void vmcs_write32(unsigned long field, u32 value)
215{
216 vmcs_writel(field, value);
217}
218
219static void vmcs_write64(unsigned long field, u64 value)
220{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800221#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222 vmcs_writel(field, value);
223#else
224 vmcs_writel(field, value);
225 asm volatile ("");
226 vmcs_writel(field+1, value >> 32);
227#endif
228}
229
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300230static void vmcs_clear_bits(unsigned long field, u32 mask)
231{
232 vmcs_writel(field, vmcs_readl(field) & ~mask);
233}
234
235static void vmcs_set_bits(unsigned long field, u32 mask)
236{
237 vmcs_writel(field, vmcs_readl(field) | mask);
238}
239
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300240static void update_exception_bitmap(struct kvm_vcpu *vcpu)
241{
242 u32 eb;
243
244 eb = 1u << PF_VECTOR;
245 if (!vcpu->fpu_active)
246 eb |= 1u << NM_VECTOR;
247 if (vcpu->guest_debug.enabled)
248 eb |= 1u << 1;
249 if (vcpu->rmode.active)
250 eb = ~0;
251 vmcs_write32(EXCEPTION_BITMAP, eb);
252}
253
Avi Kivity33ed6322007-05-02 16:54:03 +0300254static void reload_tss(void)
255{
256#ifndef CONFIG_X86_64
257
258 /*
259 * VT restores TR but not its size. Useless.
260 */
261 struct descriptor_table gdt;
262 struct segment_descriptor *descs;
263
264 get_gdt(&gdt);
265 descs = (void *)gdt.base;
266 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
267 load_TR_desc();
268#endif
269}
270
271static void vmx_save_host_state(struct kvm_vcpu *vcpu)
272{
273 struct vmx_host_state *hs = &vcpu->vmx_host_state;
274
275 if (hs->loaded)
276 return;
277
278 hs->loaded = 1;
279 /*
280 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
281 * allow segment selectors with cpl > 0 or ti == 1.
282 */
283 hs->ldt_sel = read_ldt();
284 hs->fs_gs_ldt_reload_needed = hs->ldt_sel;
285 hs->fs_sel = read_fs();
286 if (!(hs->fs_sel & 7))
287 vmcs_write16(HOST_FS_SELECTOR, hs->fs_sel);
288 else {
289 vmcs_write16(HOST_FS_SELECTOR, 0);
290 hs->fs_gs_ldt_reload_needed = 1;
291 }
292 hs->gs_sel = read_gs();
293 if (!(hs->gs_sel & 7))
294 vmcs_write16(HOST_GS_SELECTOR, hs->gs_sel);
295 else {
296 vmcs_write16(HOST_GS_SELECTOR, 0);
297 hs->fs_gs_ldt_reload_needed = 1;
298 }
299
300#ifdef CONFIG_X86_64
301 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
302 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
303#else
304 vmcs_writel(HOST_FS_BASE, segment_base(hs->fs_sel));
305 vmcs_writel(HOST_GS_BASE, segment_base(hs->gs_sel));
306#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300307
308#ifdef CONFIG_X86_64
309 if (is_long_mode(vcpu)) {
310 save_msrs(vcpu->host_msrs + msr_offset_kernel_gs_base, 1);
311 load_msrs(vcpu->guest_msrs, NR_BAD_MSRS);
312 }
313#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300314}
315
316static void vmx_load_host_state(struct kvm_vcpu *vcpu)
317{
318 struct vmx_host_state *hs = &vcpu->vmx_host_state;
319
320 if (!hs->loaded)
321 return;
322
323 hs->loaded = 0;
324 if (hs->fs_gs_ldt_reload_needed) {
325 load_ldt(hs->ldt_sel);
326 load_fs(hs->fs_sel);
327 /*
328 * If we have to reload gs, we must take care to
329 * preserve our gs base.
330 */
331 local_irq_disable();
332 load_gs(hs->gs_sel);
333#ifdef CONFIG_X86_64
334 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
335#endif
336 local_irq_enable();
337
338 reload_tss();
339 }
340#ifdef CONFIG_X86_64
341 if (is_long_mode(vcpu)) {
342 save_msrs(vcpu->guest_msrs, NR_BAD_MSRS);
343 load_msrs(vcpu->host_msrs, NR_BAD_MSRS);
344 }
345#endif
346}
347
Avi Kivity6aa8b732006-12-10 02:21:36 -0800348/*
349 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
350 * vcpu mutex is already taken.
351 */
Avi Kivitybccf2152007-02-21 18:04:26 +0200352static void vmx_vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353{
354 u64 phys_addr = __pa(vcpu->vmcs);
355 int cpu;
356
357 cpu = get_cpu();
358
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800359 if (vcpu->cpu != cpu)
360 vcpu_clear(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800361
362 if (per_cpu(current_vmcs, cpu) != vcpu->vmcs) {
363 u8 error;
364
365 per_cpu(current_vmcs, cpu) = vcpu->vmcs;
366 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
367 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
368 : "cc");
369 if (error)
370 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
371 vcpu->vmcs, phys_addr);
372 }
373
374 if (vcpu->cpu != cpu) {
375 struct descriptor_table dt;
376 unsigned long sysenter_esp;
377
378 vcpu->cpu = cpu;
379 /*
380 * Linux uses per-cpu TSS and GDT, so set these when switching
381 * processors.
382 */
383 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
384 get_gdt(&dt);
385 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
386
387 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
388 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
389 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800390}
391
392static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
393{
Avi Kivity33ed6322007-05-02 16:54:03 +0300394 vmx_load_host_state(vcpu);
Avi Kivity7702fd12007-06-14 16:27:40 +0300395 kvm_put_guest_fpu(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 put_cpu();
397}
398
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300399static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
400{
401 if (vcpu->fpu_active)
402 return;
403 vcpu->fpu_active = 1;
404 vmcs_clear_bits(GUEST_CR0, CR0_TS_MASK);
405 if (vcpu->cr0 & CR0_TS_MASK)
406 vmcs_set_bits(GUEST_CR0, CR0_TS_MASK);
407 update_exception_bitmap(vcpu);
408}
409
410static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
411{
412 if (!vcpu->fpu_active)
413 return;
414 vcpu->fpu_active = 0;
415 vmcs_set_bits(GUEST_CR0, CR0_TS_MASK);
416 update_exception_bitmap(vcpu);
417}
418
Avi Kivity774c47f2007-02-12 00:54:47 -0800419static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
420{
421 vcpu_clear(vcpu);
422}
423
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
425{
426 return vmcs_readl(GUEST_RFLAGS);
427}
428
429static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
430{
431 vmcs_writel(GUEST_RFLAGS, rflags);
432}
433
434static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
435{
436 unsigned long rip;
437 u32 interruptibility;
438
439 rip = vmcs_readl(GUEST_RIP);
440 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
441 vmcs_writel(GUEST_RIP, rip);
442
443 /*
444 * We emulated an instruction, so temporary interrupt blocking
445 * should be removed, if set.
446 */
447 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
448 if (interruptibility & 3)
449 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
450 interruptibility & ~3);
Dor Laorc1150d82007-01-05 16:36:24 -0800451 vcpu->interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452}
453
454static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code)
455{
456 printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n",
457 vmcs_readl(GUEST_RIP));
458 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
459 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
460 GP_VECTOR |
461 INTR_TYPE_EXCEPTION |
462 INTR_INFO_DELIEVER_CODE_MASK |
463 INTR_INFO_VALID_MASK);
464}
465
466/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300467 * Set up the vmcs to automatically save and restore system
468 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
469 * mode, as fiddling with msrs is very expensive.
470 */
471static void setup_msrs(struct kvm_vcpu *vcpu)
472{
473 int nr_skip, nr_good_msrs;
474
475 if (is_long_mode(vcpu))
476 nr_skip = NR_BAD_MSRS;
477 else
478 nr_skip = NR_64BIT_MSRS;
479 nr_good_msrs = vcpu->nmsrs - nr_skip;
480
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300481 /*
482 * MSR_K6_STAR is only needed on long mode guests, and only
483 * if efer.sce is enabled.
484 */
485 if (find_msr_entry(vcpu, MSR_K6_STAR)) {
486 --nr_good_msrs;
487#ifdef CONFIG_X86_64
488 if (is_long_mode(vcpu) && (vcpu->shadow_efer & EFER_SCE))
489 ++nr_good_msrs;
490#endif
491 }
492
Avi Kivitye38aea32007-04-19 13:22:48 +0300493 vmcs_writel(VM_ENTRY_MSR_LOAD_ADDR,
494 virt_to_phys(vcpu->guest_msrs + nr_skip));
495 vmcs_writel(VM_EXIT_MSR_STORE_ADDR,
496 virt_to_phys(vcpu->guest_msrs + nr_skip));
497 vmcs_writel(VM_EXIT_MSR_LOAD_ADDR,
498 virt_to_phys(vcpu->host_msrs + nr_skip));
499 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, nr_good_msrs); /* 22.2.2 */
500 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, nr_good_msrs); /* 22.2.2 */
501 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, nr_good_msrs); /* 22.2.2 */
502}
503
504/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 * reads and returns guest's timestamp counter "register"
506 * guest_tsc = host_tsc + tsc_offset -- 21.3
507 */
508static u64 guest_read_tsc(void)
509{
510 u64 host_tsc, tsc_offset;
511
512 rdtscll(host_tsc);
513 tsc_offset = vmcs_read64(TSC_OFFSET);
514 return host_tsc + tsc_offset;
515}
516
517/*
518 * writes 'guest_tsc' into guest's timestamp counter "register"
519 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
520 */
521static void guest_write_tsc(u64 guest_tsc)
522{
523 u64 host_tsc;
524
525 rdtscll(host_tsc);
526 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
527}
528
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529/*
530 * Reads an msr value (of 'msr_index') into 'pdata'.
531 * Returns 0 on success, non-0 otherwise.
532 * Assumes vcpu_load() was already called.
533 */
534static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
535{
536 u64 data;
537 struct vmx_msr_entry *msr;
538
539 if (!pdata) {
540 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
541 return -EINVAL;
542 }
543
544 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800545#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800546 case MSR_FS_BASE:
547 data = vmcs_readl(GUEST_FS_BASE);
548 break;
549 case MSR_GS_BASE:
550 data = vmcs_readl(GUEST_GS_BASE);
551 break;
552 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800553 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800554#endif
555 case MSR_IA32_TIME_STAMP_COUNTER:
556 data = guest_read_tsc();
557 break;
558 case MSR_IA32_SYSENTER_CS:
559 data = vmcs_read32(GUEST_SYSENTER_CS);
560 break;
561 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200562 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 break;
564 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200565 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800566 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567 default:
568 msr = find_msr_entry(vcpu, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800569 if (msr) {
570 data = msr->data;
571 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800573 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800574 }
575
576 *pdata = data;
577 return 0;
578}
579
580/*
581 * Writes msr value into into the appropriate "register".
582 * Returns 0 on success, non-0 otherwise.
583 * Assumes vcpu_load() was already called.
584 */
585static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
586{
587 struct vmx_msr_entry *msr;
588 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800589#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800590 case MSR_EFER:
591 return kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 case MSR_FS_BASE:
593 vmcs_writel(GUEST_FS_BASE, data);
594 break;
595 case MSR_GS_BASE:
596 vmcs_writel(GUEST_GS_BASE, data);
597 break;
Avi Kivitye6adf282007-04-30 16:07:54 +0300598 case MSR_LSTAR:
599 case MSR_SYSCALL_MASK:
600 msr = find_msr_entry(vcpu, msr_index);
601 if (msr)
602 msr->data = data;
Avi Kivityeff708b2007-05-06 16:10:01 +0300603 if (vcpu->vmx_host_state.loaded)
604 load_msrs(vcpu->guest_msrs, NR_BAD_MSRS);
Avi Kivitye6adf282007-04-30 16:07:54 +0300605 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800606#endif
607 case MSR_IA32_SYSENTER_CS:
608 vmcs_write32(GUEST_SYSENTER_CS, data);
609 break;
610 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200611 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 break;
613 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200614 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200616 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617 guest_write_tsc(data);
618 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619 default:
620 msr = find_msr_entry(vcpu, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800621 if (msr) {
622 msr->data = data;
623 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800625 return kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800626 msr->data = data;
627 break;
628 }
629
630 return 0;
631}
632
633/*
634 * Sync the rsp and rip registers into the vcpu structure. This allows
635 * registers to be accessed by indexing vcpu->regs.
636 */
637static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
638{
639 vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
640 vcpu->rip = vmcs_readl(GUEST_RIP);
641}
642
643/*
644 * Syncs rsp and rip back into the vmcs. Should be called after possible
645 * modification.
646 */
647static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
648{
649 vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]);
650 vmcs_writel(GUEST_RIP, vcpu->rip);
651}
652
653static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
654{
655 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800656 int old_singlestep;
657
Avi Kivity6aa8b732006-12-10 02:21:36 -0800658 old_singlestep = vcpu->guest_debug.singlestep;
659
660 vcpu->guest_debug.enabled = dbg->enabled;
661 if (vcpu->guest_debug.enabled) {
662 int i;
663
664 dr7 |= 0x200; /* exact */
665 for (i = 0; i < 4; ++i) {
666 if (!dbg->breakpoints[i].enabled)
667 continue;
668 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
669 dr7 |= 2 << (i*2); /* global enable */
670 dr7 |= 0 << (i*4+16); /* execution breakpoint */
671 }
672
Avi Kivity6aa8b732006-12-10 02:21:36 -0800673 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300674 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676
677 if (old_singlestep && !vcpu->guest_debug.singlestep) {
678 unsigned long flags;
679
680 flags = vmcs_readl(GUEST_RFLAGS);
681 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
682 vmcs_writel(GUEST_RFLAGS, flags);
683 }
684
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300685 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800686 vmcs_writel(GUEST_DR7, dr7);
687
688 return 0;
689}
690
691static __init int cpu_has_kvm_support(void)
692{
693 unsigned long ecx = cpuid_ecx(1);
694 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
695}
696
697static __init int vmx_disabled_by_bios(void)
698{
699 u64 msr;
700
701 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
702 return (msr & 5) == 1; /* locked but not enabled */
703}
704
Avi Kivity774c47f2007-02-12 00:54:47 -0800705static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800706{
707 int cpu = raw_smp_processor_id();
708 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
709 u64 old;
710
711 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Avi Kivitybfdc0c22006-12-13 00:34:16 -0800712 if ((old & 5) != 5)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713 /* enable and lock */
714 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | 5);
715 write_cr4(read_cr4() | CR4_VMXE); /* FIXME: not cpu hotplug safe */
716 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
717 : "memory", "cc");
718}
719
720static void hardware_disable(void *garbage)
721{
722 asm volatile (ASM_VMX_VMXOFF : : : "cc");
723}
724
725static __init void setup_vmcs_descriptor(void)
726{
727 u32 vmx_msr_low, vmx_msr_high;
728
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800729 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 vmcs_descriptor.size = vmx_msr_high & 0x1fff;
731 vmcs_descriptor.order = get_order(vmcs_descriptor.size);
732 vmcs_descriptor.revision_id = vmx_msr_low;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -0800733}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
735static struct vmcs *alloc_vmcs_cpu(int cpu)
736{
737 int node = cpu_to_node(cpu);
738 struct page *pages;
739 struct vmcs *vmcs;
740
741 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_descriptor.order);
742 if (!pages)
743 return NULL;
744 vmcs = page_address(pages);
745 memset(vmcs, 0, vmcs_descriptor.size);
746 vmcs->revision_id = vmcs_descriptor.revision_id; /* vmcs revision id */
747 return vmcs;
748}
749
750static struct vmcs *alloc_vmcs(void)
751{
Ingo Molnard3b2c332007-01-05 16:36:23 -0800752 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753}
754
755static void free_vmcs(struct vmcs *vmcs)
756{
757 free_pages((unsigned long)vmcs, vmcs_descriptor.order);
758}
759
Sam Ravnborg39959582007-06-01 00:47:13 -0700760static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761{
762 int cpu;
763
764 for_each_online_cpu(cpu)
765 free_vmcs(per_cpu(vmxarea, cpu));
766}
767
768extern struct vmcs *alloc_vmcs_cpu(int cpu);
769
770static __init int alloc_kvm_area(void)
771{
772 int cpu;
773
774 for_each_online_cpu(cpu) {
775 struct vmcs *vmcs;
776
777 vmcs = alloc_vmcs_cpu(cpu);
778 if (!vmcs) {
779 free_kvm_area();
780 return -ENOMEM;
781 }
782
783 per_cpu(vmxarea, cpu) = vmcs;
784 }
785 return 0;
786}
787
788static __init int hardware_setup(void)
789{
790 setup_vmcs_descriptor();
791 return alloc_kvm_area();
792}
793
794static __exit void hardware_unsetup(void)
795{
796 free_kvm_area();
797}
798
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
800{
801 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
802
Avi Kivity6af11b92007-03-19 13:18:10 +0200803 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804 vmcs_write16(sf->selector, save->selector);
805 vmcs_writel(sf->base, save->base);
806 vmcs_write32(sf->limit, save->limit);
807 vmcs_write32(sf->ar_bytes, save->ar);
808 } else {
809 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
810 << AR_DPL_SHIFT;
811 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
812 }
813}
814
815static void enter_pmode(struct kvm_vcpu *vcpu)
816{
817 unsigned long flags;
818
819 vcpu->rmode.active = 0;
820
821 vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base);
822 vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit);
823 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar);
824
825 flags = vmcs_readl(GUEST_RFLAGS);
826 flags &= ~(IOPL_MASK | X86_EFLAGS_VM);
827 flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT);
828 vmcs_writel(GUEST_RFLAGS, flags);
829
830 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~CR4_VME_MASK) |
831 (vmcs_readl(CR4_READ_SHADOW) & CR4_VME_MASK));
832
833 update_exception_bitmap(vcpu);
834
835 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es);
836 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds);
837 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs);
838 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs);
839
840 vmcs_write16(GUEST_SS_SELECTOR, 0);
841 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
842
843 vmcs_write16(GUEST_CS_SELECTOR,
844 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
845 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
846}
847
848static int rmode_tss_base(struct kvm* kvm)
849{
850 gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3;
851 return base_gfn << PAGE_SHIFT;
852}
853
854static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
855{
856 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
857
858 save->selector = vmcs_read16(sf->selector);
859 save->base = vmcs_readl(sf->base);
860 save->limit = vmcs_read32(sf->limit);
861 save->ar = vmcs_read32(sf->ar_bytes);
862 vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4);
863 vmcs_write32(sf->limit, 0xffff);
864 vmcs_write32(sf->ar_bytes, 0xf3);
865}
866
867static void enter_rmode(struct kvm_vcpu *vcpu)
868{
869 unsigned long flags;
870
871 vcpu->rmode.active = 1;
872
873 vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
874 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
875
876 vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
877 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
878
879 vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
880 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
881
882 flags = vmcs_readl(GUEST_RFLAGS);
883 vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT;
884
885 flags |= IOPL_MASK | X86_EFLAGS_VM;
886
887 vmcs_writel(GUEST_RFLAGS, flags);
888 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | CR4_VME_MASK);
889 update_exception_bitmap(vcpu);
890
891 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
892 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
893 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
894
895 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -0800896 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +0200897 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
898 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
900
901 fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es);
902 fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds);
903 fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs);
904 fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs);
905}
906
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800907#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908
909static void enter_lmode(struct kvm_vcpu *vcpu)
910{
911 u32 guest_tr_ar;
912
913 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
914 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
915 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
916 __FUNCTION__);
917 vmcs_write32(GUEST_TR_AR_BYTES,
918 (guest_tr_ar & ~AR_TYPE_MASK)
919 | AR_TYPE_BUSY_64_TSS);
920 }
921
922 vcpu->shadow_efer |= EFER_LMA;
923
924 find_msr_entry(vcpu, MSR_EFER)->data |= EFER_LMA | EFER_LME;
925 vmcs_write32(VM_ENTRY_CONTROLS,
926 vmcs_read32(VM_ENTRY_CONTROLS)
927 | VM_ENTRY_CONTROLS_IA32E_MASK);
928}
929
930static void exit_lmode(struct kvm_vcpu *vcpu)
931{
932 vcpu->shadow_efer &= ~EFER_LMA;
933
934 vmcs_write32(VM_ENTRY_CONTROLS,
935 vmcs_read32(VM_ENTRY_CONTROLS)
936 & ~VM_ENTRY_CONTROLS_IA32E_MASK);
937}
938
939#endif
940
Anthony Liguori25c4c272007-04-27 09:29:21 +0300941static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -0800942{
Avi Kivity399badf2007-01-05 16:36:38 -0800943 vcpu->cr4 &= KVM_GUEST_CR4_MASK;
944 vcpu->cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
945}
946
Avi Kivity6aa8b732006-12-10 02:21:36 -0800947static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
948{
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300949 vmx_fpu_deactivate(vcpu);
950
Avi Kivity6aa8b732006-12-10 02:21:36 -0800951 if (vcpu->rmode.active && (cr0 & CR0_PE_MASK))
952 enter_pmode(vcpu);
953
954 if (!vcpu->rmode.active && !(cr0 & CR0_PE_MASK))
955 enter_rmode(vcpu);
956
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800957#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958 if (vcpu->shadow_efer & EFER_LME) {
959 if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK))
960 enter_lmode(vcpu);
961 if (is_paging(vcpu) && !(cr0 & CR0_PG_MASK))
962 exit_lmode(vcpu);
963 }
964#endif
965
966 vmcs_writel(CR0_READ_SHADOW, cr0);
967 vmcs_writel(GUEST_CR0,
968 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
969 vcpu->cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300970
971 if (!(cr0 & CR0_TS_MASK) || !(cr0 & CR0_PE_MASK))
972 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973}
974
Avi Kivity6aa8b732006-12-10 02:21:36 -0800975static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
976{
977 vmcs_writel(GUEST_CR3, cr3);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300978 if (vcpu->cr0 & CR0_PE_MASK)
979 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980}
981
982static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
983{
984 vmcs_writel(CR4_READ_SHADOW, cr4);
985 vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ?
986 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
987 vcpu->cr4 = cr4;
988}
989
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800990#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991
992static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
993{
994 struct vmx_msr_entry *msr = find_msr_entry(vcpu, MSR_EFER);
995
996 vcpu->shadow_efer = efer;
997 if (efer & EFER_LMA) {
998 vmcs_write32(VM_ENTRY_CONTROLS,
999 vmcs_read32(VM_ENTRY_CONTROLS) |
1000 VM_ENTRY_CONTROLS_IA32E_MASK);
1001 msr->data = efer;
1002
1003 } else {
1004 vmcs_write32(VM_ENTRY_CONTROLS,
1005 vmcs_read32(VM_ENTRY_CONTROLS) &
1006 ~VM_ENTRY_CONTROLS_IA32E_MASK);
1007
1008 msr->data = efer & ~EFER_LME;
1009 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001010 setup_msrs(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011}
1012
1013#endif
1014
1015static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1016{
1017 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1018
1019 return vmcs_readl(sf->base);
1020}
1021
1022static void vmx_get_segment(struct kvm_vcpu *vcpu,
1023 struct kvm_segment *var, int seg)
1024{
1025 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1026 u32 ar;
1027
1028 var->base = vmcs_readl(sf->base);
1029 var->limit = vmcs_read32(sf->limit);
1030 var->selector = vmcs_read16(sf->selector);
1031 ar = vmcs_read32(sf->ar_bytes);
1032 if (ar & AR_UNUSABLE_MASK)
1033 ar = 0;
1034 var->type = ar & 15;
1035 var->s = (ar >> 4) & 1;
1036 var->dpl = (ar >> 5) & 3;
1037 var->present = (ar >> 7) & 1;
1038 var->avl = (ar >> 12) & 1;
1039 var->l = (ar >> 13) & 1;
1040 var->db = (ar >> 14) & 1;
1041 var->g = (ar >> 15) & 1;
1042 var->unusable = (ar >> 16) & 1;
1043}
1044
Avi Kivity653e3102007-05-07 10:55:37 +03001045static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001046{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 u32 ar;
1048
Avi Kivity653e3102007-05-07 10:55:37 +03001049 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 ar = 1 << 16;
1051 else {
1052 ar = var->type & 15;
1053 ar |= (var->s & 1) << 4;
1054 ar |= (var->dpl & 3) << 5;
1055 ar |= (var->present & 1) << 7;
1056 ar |= (var->avl & 1) << 12;
1057 ar |= (var->l & 1) << 13;
1058 ar |= (var->db & 1) << 14;
1059 ar |= (var->g & 1) << 15;
1060 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001061 if (ar == 0) /* a 0 value means unusable */
1062 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001063
1064 return ar;
1065}
1066
1067static void vmx_set_segment(struct kvm_vcpu *vcpu,
1068 struct kvm_segment *var, int seg)
1069{
1070 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1071 u32 ar;
1072
1073 if (vcpu->rmode.active && seg == VCPU_SREG_TR) {
1074 vcpu->rmode.tr.selector = var->selector;
1075 vcpu->rmode.tr.base = var->base;
1076 vcpu->rmode.tr.limit = var->limit;
1077 vcpu->rmode.tr.ar = vmx_segment_access_rights(var);
1078 return;
1079 }
1080 vmcs_writel(sf->base, var->base);
1081 vmcs_write32(sf->limit, var->limit);
1082 vmcs_write16(sf->selector, var->selector);
1083 if (vcpu->rmode.active && var->s) {
1084 /*
1085 * Hack real-mode segments into vm86 compatibility.
1086 */
1087 if (var->base == 0xffff0000 && var->selector == 0xf000)
1088 vmcs_writel(sf->base, 0xf0000);
1089 ar = 0xf3;
1090 } else
1091 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 vmcs_write32(sf->ar_bytes, ar);
1093}
1094
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1096{
1097 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1098
1099 *db = (ar >> 14) & 1;
1100 *l = (ar >> 13) & 1;
1101}
1102
1103static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1104{
1105 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1106 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1107}
1108
1109static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1110{
1111 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1112 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1113}
1114
1115static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1116{
1117 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1118 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1119}
1120
1121static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1122{
1123 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1124 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1125}
1126
1127static int init_rmode_tss(struct kvm* kvm)
1128{
1129 struct page *p1, *p2, *p3;
1130 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
1131 char *page;
1132
Avi Kivity954bbbc2007-03-30 14:02:32 +03001133 p1 = gfn_to_page(kvm, fn++);
1134 p2 = gfn_to_page(kvm, fn++);
1135 p3 = gfn_to_page(kvm, fn);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136
1137 if (!p1 || !p2 || !p3) {
1138 kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__);
1139 return 0;
1140 }
1141
1142 page = kmap_atomic(p1, KM_USER0);
1143 memset(page, 0, PAGE_SIZE);
1144 *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1145 kunmap_atomic(page, KM_USER0);
1146
1147 page = kmap_atomic(p2, KM_USER0);
1148 memset(page, 0, PAGE_SIZE);
1149 kunmap_atomic(page, KM_USER0);
1150
1151 page = kmap_atomic(p3, KM_USER0);
1152 memset(page, 0, PAGE_SIZE);
1153 *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0;
1154 kunmap_atomic(page, KM_USER0);
1155
1156 return 1;
1157}
1158
1159static void vmcs_write32_fixedbits(u32 msr, u32 vmcs_field, u32 val)
1160{
1161 u32 msr_high, msr_low;
1162
1163 rdmsr(msr, msr_low, msr_high);
1164
1165 val &= msr_high;
1166 val |= msr_low;
1167 vmcs_write32(vmcs_field, val);
1168}
1169
1170static void seg_setup(int seg)
1171{
1172 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1173
1174 vmcs_write16(sf->selector, 0);
1175 vmcs_writel(sf->base, 0);
1176 vmcs_write32(sf->limit, 0xffff);
1177 vmcs_write32(sf->ar_bytes, 0x93);
1178}
1179
1180/*
1181 * Sets up the vmcs for emulated real mode.
1182 */
1183static int vmx_vcpu_setup(struct kvm_vcpu *vcpu)
1184{
1185 u32 host_sysenter_cs;
1186 u32 junk;
1187 unsigned long a;
1188 struct descriptor_table dt;
1189 int i;
1190 int ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 extern asmlinkage void kvm_vmx_return(void);
1192
1193 if (!init_rmode_tss(vcpu->kvm)) {
1194 ret = -ENOMEM;
1195 goto out;
1196 }
1197
1198 memset(vcpu->regs, 0, sizeof(vcpu->regs));
1199 vcpu->regs[VCPU_REGS_RDX] = get_rdx_init_val();
1200 vcpu->cr8 = 0;
1201 vcpu->apic_base = 0xfee00000 |
1202 /*for vcpu 0*/ MSR_IA32_APICBASE_BSP |
1203 MSR_IA32_APICBASE_ENABLE;
1204
1205 fx_init(vcpu);
1206
1207 /*
1208 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1209 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1210 */
1211 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1212 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1213 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1214 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1215
1216 seg_setup(VCPU_SREG_DS);
1217 seg_setup(VCPU_SREG_ES);
1218 seg_setup(VCPU_SREG_FS);
1219 seg_setup(VCPU_SREG_GS);
1220 seg_setup(VCPU_SREG_SS);
1221
1222 vmcs_write16(GUEST_TR_SELECTOR, 0);
1223 vmcs_writel(GUEST_TR_BASE, 0);
1224 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1225 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1226
1227 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1228 vmcs_writel(GUEST_LDTR_BASE, 0);
1229 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1230 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1231
1232 vmcs_write32(GUEST_SYSENTER_CS, 0);
1233 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1234 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1235
1236 vmcs_writel(GUEST_RFLAGS, 0x02);
1237 vmcs_writel(GUEST_RIP, 0xfff0);
1238 vmcs_writel(GUEST_RSP, 0);
1239
Avi Kivity6aa8b732006-12-10 02:21:36 -08001240 //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0
1241 vmcs_writel(GUEST_DR7, 0x400);
1242
1243 vmcs_writel(GUEST_GDTR_BASE, 0);
1244 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1245
1246 vmcs_writel(GUEST_IDTR_BASE, 0);
1247 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1248
1249 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1250 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1251 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1252
1253 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001254 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1255 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001256
1257 guest_write_tsc(0);
1258
1259 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1260
1261 /* Special registers */
1262 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1263
1264 /* Control */
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001265 vmcs_write32_fixedbits(MSR_IA32_VMX_PINBASED_CTLS,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 PIN_BASED_VM_EXEC_CONTROL,
1267 PIN_BASED_EXT_INTR_MASK /* 20.6.1 */
1268 | PIN_BASED_NMI_EXITING /* 20.6.1 */
1269 );
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001270 vmcs_write32_fixedbits(MSR_IA32_VMX_PROCBASED_CTLS,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271 CPU_BASED_VM_EXEC_CONTROL,
1272 CPU_BASED_HLT_EXITING /* 20.6.2 */
1273 | CPU_BASED_CR8_LOAD_EXITING /* 20.6.2 */
1274 | CPU_BASED_CR8_STORE_EXITING /* 20.6.2 */
He, Qingfdef3ad2007-04-30 09:45:24 +03001275 | CPU_BASED_ACTIVATE_IO_BITMAP /* 20.6.2 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 | CPU_BASED_MOV_DR_EXITING
1277 | CPU_BASED_USE_TSC_OFFSETING /* 21.3 */
1278 );
1279
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
1281 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
1282 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1283
1284 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1285 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1286 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1287
1288 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1289 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1290 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1291 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1292 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1293 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001294#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295 rdmsrl(MSR_FS_BASE, a);
1296 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1297 rdmsrl(MSR_GS_BASE, a);
1298 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1299#else
1300 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1301 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1302#endif
1303
1304 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1305
1306 get_idt(&dt);
1307 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1308
1309
1310 vmcs_writel(HOST_RIP, (unsigned long)kvm_vmx_return); /* 22.2.5 */
1311
1312 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1313 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1314 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1315 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1316 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1317 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1318
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319 for (i = 0; i < NR_VMX_MSR; ++i) {
1320 u32 index = vmx_msr_index[i];
1321 u32 data_low, data_high;
1322 u64 data;
1323 int j = vcpu->nmsrs;
1324
1325 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1326 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001327 if (wrmsr_safe(index, data_low, data_high) < 0)
1328 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329 data = data_low | ((u64)data_high << 32);
1330 vcpu->host_msrs[j].index = index;
1331 vcpu->host_msrs[j].reserved = 0;
1332 vcpu->host_msrs[j].data = data;
1333 vcpu->guest_msrs[j] = vcpu->host_msrs[j];
Avi Kivity2345df82007-04-17 15:30:24 +03001334#ifdef CONFIG_X86_64
1335 if (index == MSR_KERNEL_GS_BASE)
1336 msr_offset_kernel_gs_base = j;
1337#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 ++vcpu->nmsrs;
1339 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340
Avi Kivitye38aea32007-04-19 13:22:48 +03001341 setup_msrs(vcpu);
1342
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001343 vmcs_write32_fixedbits(MSR_IA32_VMX_EXIT_CTLS, VM_EXIT_CONTROLS,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344 (HOST_IS_64 << 9)); /* 22.2,1, 20.7.1 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001345
1346 /* 22.2.1, 20.8.1 */
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001347 vmcs_write32_fixedbits(MSR_IA32_VMX_ENTRY_CTLS,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 VM_ENTRY_CONTROLS, 0);
1349 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1350
Michael Riepe3b99ab22006-12-13 00:34:15 -08001351#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352 vmcs_writel(VIRTUAL_APIC_PAGE_ADDR, 0);
1353 vmcs_writel(TPR_THRESHOLD, 0);
Michael Riepe3b99ab22006-12-13 00:34:15 -08001354#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355
Anthony Liguori25c4c272007-04-27 09:29:21 +03001356 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1358
1359 vcpu->cr0 = 0x60000010;
1360 vmx_set_cr0(vcpu, vcpu->cr0); // enter rmode
1361 vmx_set_cr4(vcpu, 0);
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001362#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 vmx_set_efer(vcpu, 0);
1364#endif
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001365 vmx_fpu_activate(vcpu);
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001366 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367
1368 return 0;
1369
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370out:
1371 return ret;
1372}
1373
1374static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq)
1375{
1376 u16 ent[2];
1377 u16 cs;
1378 u16 ip;
1379 unsigned long flags;
1380 unsigned long ss_base = vmcs_readl(GUEST_SS_BASE);
1381 u16 sp = vmcs_readl(GUEST_RSP);
1382 u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT);
1383
Eric Sesterhenn / Snakebyte39649942007-04-09 16:15:05 +02001384 if (sp > ss_limit || sp < 6 ) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n",
1386 __FUNCTION__,
1387 vmcs_readl(GUEST_RSP),
1388 vmcs_readl(GUEST_SS_BASE),
1389 vmcs_read32(GUEST_SS_LIMIT));
1390 return;
1391 }
1392
1393 if (kvm_read_guest(vcpu, irq * sizeof(ent), sizeof(ent), &ent) !=
1394 sizeof(ent)) {
1395 vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__);
1396 return;
1397 }
1398
1399 flags = vmcs_readl(GUEST_RFLAGS);
1400 cs = vmcs_readl(GUEST_CS_BASE) >> 4;
1401 ip = vmcs_readl(GUEST_RIP);
1402
1403
1404 if (kvm_write_guest(vcpu, ss_base + sp - 2, 2, &flags) != 2 ||
1405 kvm_write_guest(vcpu, ss_base + sp - 4, 2, &cs) != 2 ||
1406 kvm_write_guest(vcpu, ss_base + sp - 6, 2, &ip) != 2) {
1407 vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__);
1408 return;
1409 }
1410
1411 vmcs_writel(GUEST_RFLAGS, flags &
1412 ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF));
1413 vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ;
1414 vmcs_writel(GUEST_CS_BASE, ent[1] << 4);
1415 vmcs_writel(GUEST_RIP, ent[0]);
1416 vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6));
1417}
1418
1419static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1420{
1421 int word_index = __ffs(vcpu->irq_summary);
1422 int bit_index = __ffs(vcpu->irq_pending[word_index]);
1423 int irq = word_index * BITS_PER_LONG + bit_index;
1424
1425 clear_bit(bit_index, &vcpu->irq_pending[word_index]);
1426 if (!vcpu->irq_pending[word_index])
1427 clear_bit(word_index, &vcpu->irq_summary);
1428
1429 if (vcpu->rmode.active) {
1430 inject_rmode_irq(vcpu, irq);
1431 return;
1432 }
1433 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1434 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1435}
1436
Dor Laorc1150d82007-01-05 16:36:24 -08001437
1438static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1439 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440{
Dor Laorc1150d82007-01-05 16:36:24 -08001441 u32 cpu_based_vm_exec_control;
1442
1443 vcpu->interrupt_window_open =
1444 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1445 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1446
1447 if (vcpu->interrupt_window_open &&
1448 vcpu->irq_summary &&
1449 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001451 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452 */
1453 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001454
1455 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
1456 if (!vcpu->interrupt_window_open &&
1457 (vcpu->irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458 /*
1459 * Interrupts blocked. Wait for unblock.
1460 */
Dor Laorc1150d82007-01-05 16:36:24 -08001461 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1462 else
1463 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1464 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465}
1466
1467static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1468{
1469 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1470
1471 set_debugreg(dbg->bp[0], 0);
1472 set_debugreg(dbg->bp[1], 1);
1473 set_debugreg(dbg->bp[2], 2);
1474 set_debugreg(dbg->bp[3], 3);
1475
1476 if (dbg->singlestep) {
1477 unsigned long flags;
1478
1479 flags = vmcs_readl(GUEST_RFLAGS);
1480 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1481 vmcs_writel(GUEST_RFLAGS, flags);
1482 }
1483}
1484
1485static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1486 int vec, u32 err_code)
1487{
1488 if (!vcpu->rmode.active)
1489 return 0;
1490
1491 if (vec == GP_VECTOR && err_code == 0)
1492 if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE)
1493 return 1;
1494 return 0;
1495}
1496
1497static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1498{
1499 u32 intr_info, error_code;
1500 unsigned long cr2, rip;
1501 u32 vect_info;
1502 enum emulation_result er;
Avi Kivitye2dec932007-01-05 16:36:54 -08001503 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504
1505 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1506 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1507
1508 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
1509 !is_page_fault(intr_info)) {
1510 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
1511 "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info);
1512 }
1513
1514 if (is_external_interrupt(vect_info)) {
1515 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
1516 set_bit(irq, vcpu->irq_pending);
1517 set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary);
1518 }
1519
1520 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
1521 asm ("int $2");
1522 return 1;
1523 }
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001524
1525 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001526 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001527 return 1;
1528 }
1529
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530 error_code = 0;
1531 rip = vmcs_readl(GUEST_RIP);
1532 if (intr_info & INTR_INFO_DELIEVER_CODE_MASK)
1533 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1534 if (is_page_fault(intr_info)) {
1535 cr2 = vmcs_readl(EXIT_QUALIFICATION);
1536
1537 spin_lock(&vcpu->kvm->lock);
Avi Kivitye2dec932007-01-05 16:36:54 -08001538 r = kvm_mmu_page_fault(vcpu, cr2, error_code);
1539 if (r < 0) {
1540 spin_unlock(&vcpu->kvm->lock);
1541 return r;
1542 }
1543 if (!r) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544 spin_unlock(&vcpu->kvm->lock);
1545 return 1;
1546 }
1547
1548 er = emulate_instruction(vcpu, kvm_run, cr2, error_code);
1549 spin_unlock(&vcpu->kvm->lock);
1550
1551 switch (er) {
1552 case EMULATE_DONE:
1553 return 1;
1554 case EMULATE_DO_MMIO:
Avi Kivity1165f5f2007-04-19 17:27:43 +03001555 ++vcpu->stat.mmio_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556 kvm_run->exit_reason = KVM_EXIT_MMIO;
1557 return 0;
1558 case EMULATE_FAIL:
1559 vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__);
1560 break;
1561 default:
1562 BUG();
1563 }
1564 }
1565
1566 if (vcpu->rmode.active &&
1567 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
1568 error_code))
1569 return 1;
1570
1571 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) {
1572 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1573 return 0;
1574 }
1575 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
1576 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
1577 kvm_run->ex.error_code = error_code;
1578 return 0;
1579}
1580
1581static int handle_external_interrupt(struct kvm_vcpu *vcpu,
1582 struct kvm_run *kvm_run)
1583{
Avi Kivity1165f5f2007-04-19 17:27:43 +03001584 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585 return 1;
1586}
1587
Avi Kivity988ad742007-02-12 00:54:36 -08001588static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1589{
1590 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1591 return 0;
1592}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593
Avi Kivity039576c2007-03-20 12:46:50 +02001594static int get_io_count(struct kvm_vcpu *vcpu, unsigned long *count)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001595{
1596 u64 inst;
1597 gva_t rip;
1598 int countr_size;
1599 int i, n;
1600
1601 if ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_VM)) {
1602 countr_size = 2;
1603 } else {
1604 u32 cs_ar = vmcs_read32(GUEST_CS_AR_BYTES);
1605
1606 countr_size = (cs_ar & AR_L_MASK) ? 8:
1607 (cs_ar & AR_DB_MASK) ? 4: 2;
1608 }
1609
1610 rip = vmcs_readl(GUEST_RIP);
1611 if (countr_size != 8)
1612 rip += vmcs_readl(GUEST_CS_BASE);
1613
1614 n = kvm_read_guest(vcpu, rip, sizeof(inst), &inst);
1615
1616 for (i = 0; i < n; i++) {
1617 switch (((u8*)&inst)[i]) {
1618 case 0xf0:
1619 case 0xf2:
1620 case 0xf3:
1621 case 0x2e:
1622 case 0x36:
1623 case 0x3e:
1624 case 0x26:
1625 case 0x64:
1626 case 0x65:
1627 case 0x66:
1628 break;
1629 case 0x67:
1630 countr_size = (countr_size == 2) ? 4: (countr_size >> 1);
1631 default:
1632 goto done;
1633 }
1634 }
1635 return 0;
1636done:
1637 countr_size *= 8;
1638 *count = vcpu->regs[VCPU_REGS_RCX] & (~0ULL >> (64 - countr_size));
Avi Kivity039576c2007-03-20 12:46:50 +02001639 //printk("cx: %lx\n", vcpu->regs[VCPU_REGS_RCX]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640 return 1;
1641}
1642
1643static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1644{
1645 u64 exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02001646 int size, down, in, string, rep;
1647 unsigned port;
1648 unsigned long count;
1649 gva_t address;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001650
Avi Kivity1165f5f2007-04-19 17:27:43 +03001651 ++vcpu->stat.io_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02001653 in = (exit_qualification & 8) != 0;
1654 size = (exit_qualification & 7) + 1;
1655 string = (exit_qualification & 16) != 0;
1656 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
1657 count = 1;
1658 rep = (exit_qualification & 32) != 0;
1659 port = exit_qualification >> 16;
1660 address = 0;
1661 if (string) {
1662 if (rep && !get_io_count(vcpu, &count))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001663 return 1;
Avi Kivity039576c2007-03-20 12:46:50 +02001664 address = vmcs_readl(GUEST_LINEAR_ADDRESS);
1665 }
1666 return kvm_setup_pio(vcpu, kvm_run, in, size, count, string, down,
1667 address, rep, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668}
1669
Ingo Molnar102d8322007-02-19 14:37:47 +02001670static void
1671vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
1672{
1673 /*
1674 * Patch in the VMCALL instruction:
1675 */
1676 hypercall[0] = 0x0f;
1677 hypercall[1] = 0x01;
1678 hypercall[2] = 0xc1;
1679 hypercall[3] = 0xc3;
1680}
1681
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1683{
1684 u64 exit_qualification;
1685 int cr;
1686 int reg;
1687
1688 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1689 cr = exit_qualification & 15;
1690 reg = (exit_qualification >> 8) & 15;
1691 switch ((exit_qualification >> 4) & 3) {
1692 case 0: /* mov to cr */
1693 switch (cr) {
1694 case 0:
1695 vcpu_load_rsp_rip(vcpu);
1696 set_cr0(vcpu, vcpu->regs[reg]);
1697 skip_emulated_instruction(vcpu);
1698 return 1;
1699 case 3:
1700 vcpu_load_rsp_rip(vcpu);
1701 set_cr3(vcpu, vcpu->regs[reg]);
1702 skip_emulated_instruction(vcpu);
1703 return 1;
1704 case 4:
1705 vcpu_load_rsp_rip(vcpu);
1706 set_cr4(vcpu, vcpu->regs[reg]);
1707 skip_emulated_instruction(vcpu);
1708 return 1;
1709 case 8:
1710 vcpu_load_rsp_rip(vcpu);
1711 set_cr8(vcpu, vcpu->regs[reg]);
1712 skip_emulated_instruction(vcpu);
1713 return 1;
1714 };
1715 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03001716 case 2: /* clts */
1717 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001718 vmx_fpu_deactivate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001719 vcpu->cr0 &= ~CR0_TS_MASK;
1720 vmcs_writel(CR0_READ_SHADOW, vcpu->cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001721 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03001722 skip_emulated_instruction(vcpu);
1723 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001724 case 1: /*mov from cr*/
1725 switch (cr) {
1726 case 3:
1727 vcpu_load_rsp_rip(vcpu);
1728 vcpu->regs[reg] = vcpu->cr3;
1729 vcpu_put_rsp_rip(vcpu);
1730 skip_emulated_instruction(vcpu);
1731 return 1;
1732 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001733 vcpu_load_rsp_rip(vcpu);
1734 vcpu->regs[reg] = vcpu->cr8;
1735 vcpu_put_rsp_rip(vcpu);
1736 skip_emulated_instruction(vcpu);
1737 return 1;
1738 }
1739 break;
1740 case 3: /* lmsw */
1741 lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
1742
1743 skip_emulated_instruction(vcpu);
1744 return 1;
1745 default:
1746 break;
1747 }
1748 kvm_run->exit_reason = 0;
1749 printk(KERN_ERR "kvm: unhandled control register: op %d cr %d\n",
1750 (int)(exit_qualification >> 4) & 3, cr);
1751 return 0;
1752}
1753
1754static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1755{
1756 u64 exit_qualification;
1757 unsigned long val;
1758 int dr, reg;
1759
1760 /*
1761 * FIXME: this code assumes the host is debugging the guest.
1762 * need to deal with guest debugging itself too.
1763 */
1764 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
1765 dr = exit_qualification & 7;
1766 reg = (exit_qualification >> 8) & 15;
1767 vcpu_load_rsp_rip(vcpu);
1768 if (exit_qualification & 16) {
1769 /* mov from dr */
1770 switch (dr) {
1771 case 6:
1772 val = 0xffff0ff0;
1773 break;
1774 case 7:
1775 val = 0x400;
1776 break;
1777 default:
1778 val = 0;
1779 }
1780 vcpu->regs[reg] = val;
1781 } else {
1782 /* mov to dr */
1783 }
1784 vcpu_put_rsp_rip(vcpu);
1785 skip_emulated_instruction(vcpu);
1786 return 1;
1787}
1788
1789static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1790{
Avi Kivity06465c52007-02-28 20:46:53 +02001791 kvm_emulate_cpuid(vcpu);
1792 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793}
1794
1795static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1796{
1797 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1798 u64 data;
1799
1800 if (vmx_get_msr(vcpu, ecx, &data)) {
1801 vmx_inject_gp(vcpu, 0);
1802 return 1;
1803 }
1804
1805 /* FIXME: handling of bits 32:63 of rax, rdx */
1806 vcpu->regs[VCPU_REGS_RAX] = data & -1u;
1807 vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
1808 skip_emulated_instruction(vcpu);
1809 return 1;
1810}
1811
1812static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1813{
1814 u32 ecx = vcpu->regs[VCPU_REGS_RCX];
1815 u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u)
1816 | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32);
1817
1818 if (vmx_set_msr(vcpu, ecx, data) != 0) {
1819 vmx_inject_gp(vcpu, 0);
1820 return 1;
1821 }
1822
1823 skip_emulated_instruction(vcpu);
1824 return 1;
1825}
1826
Dor Laorc1150d82007-01-05 16:36:24 -08001827static void post_kvm_run_save(struct kvm_vcpu *vcpu,
1828 struct kvm_run *kvm_run)
1829{
1830 kvm_run->if_flag = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) != 0;
1831 kvm_run->cr8 = vcpu->cr8;
1832 kvm_run->apic_base = vcpu->apic_base;
1833 kvm_run->ready_for_interrupt_injection = (vcpu->interrupt_window_open &&
1834 vcpu->irq_summary == 0);
1835}
1836
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837static int handle_interrupt_window(struct kvm_vcpu *vcpu,
1838 struct kvm_run *kvm_run)
1839{
Dor Laorc1150d82007-01-05 16:36:24 -08001840 /*
1841 * If the user space waits to inject interrupts, exit as soon as
1842 * possible
1843 */
1844 if (kvm_run->request_interrupt_window &&
Dor Laor022a9302007-01-05 16:37:00 -08001845 !vcpu->irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08001846 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001847 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08001848 return 0;
1849 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 return 1;
1851}
1852
1853static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1854{
1855 skip_emulated_instruction(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001856 if (vcpu->irq_summary)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857 return 1;
1858
1859 kvm_run->exit_reason = KVM_EXIT_HLT;
Avi Kivity1165f5f2007-04-19 17:27:43 +03001860 ++vcpu->stat.halt_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861 return 0;
1862}
1863
Ingo Molnarc21415e2007-02-19 14:37:47 +02001864static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1865{
Dor Laor510043d2007-02-19 18:25:43 +02001866 skip_emulated_instruction(vcpu);
Avi Kivity270fd9b2007-02-19 14:37:47 +02001867 return kvm_hypercall(vcpu, kvm_run);
Ingo Molnarc21415e2007-02-19 14:37:47 +02001868}
1869
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870/*
1871 * The exit handlers return 1 if the exit was handled fully and guest execution
1872 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
1873 * to be done to userspace and return 0.
1874 */
1875static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
1876 struct kvm_run *kvm_run) = {
1877 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
1878 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08001879 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881 [EXIT_REASON_CR_ACCESS] = handle_cr,
1882 [EXIT_REASON_DR_ACCESS] = handle_dr,
1883 [EXIT_REASON_CPUID] = handle_cpuid,
1884 [EXIT_REASON_MSR_READ] = handle_rdmsr,
1885 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
1886 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
1887 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02001888 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889};
1890
1891static const int kvm_vmx_max_exit_handlers =
1892 sizeof(kvm_vmx_exit_handlers) / sizeof(*kvm_vmx_exit_handlers);
1893
1894/*
1895 * The guest has exited. See if we can fix it or if we need userspace
1896 * assistance.
1897 */
1898static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1899{
1900 u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
1901 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
1902
1903 if ( (vectoring_info & VECTORING_INFO_VALID_MASK) &&
1904 exit_reason != EXIT_REASON_EXCEPTION_NMI )
1905 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
1906 "exit reason is 0x%x\n", __FUNCTION__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 if (exit_reason < kvm_vmx_max_exit_handlers
1908 && kvm_vmx_exit_handlers[exit_reason])
1909 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
1910 else {
1911 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
1912 kvm_run->hw.hardware_exit_reason = exit_reason;
1913 }
1914 return 0;
1915}
1916
Dor Laorc1150d82007-01-05 16:36:24 -08001917/*
1918 * Check if userspace requested an interrupt window, and that the
1919 * interrupt window is open.
1920 *
1921 * No need to exit to userspace if we already have an interrupt queued.
1922 */
1923static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
1924 struct kvm_run *kvm_run)
1925{
1926 return (!vcpu->irq_summary &&
1927 kvm_run->request_interrupt_window &&
1928 vcpu->interrupt_window_open &&
1929 (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF));
1930}
1931
Avi Kivity6aa8b732006-12-10 02:21:36 -08001932static int vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1933{
1934 u8 fail;
Avi Kivitye2dec932007-01-05 16:36:54 -08001935 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001936
Avi Kivitye6adf282007-04-30 16:07:54 +03001937preempted:
Avi Kivitycccf7482007-01-22 20:40:39 -08001938 if (!vcpu->mmio_read_completed)
1939 do_interrupt_requests(vcpu, kvm_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940
1941 if (vcpu->guest_debug.enabled)
1942 kvm_guest_debug_pre(vcpu);
1943
Avi Kivitye6adf282007-04-30 16:07:54 +03001944again:
Avi Kivity33ed6322007-05-02 16:54:03 +03001945 vmx_save_host_state(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03001946 kvm_load_guest_fpu(vcpu);
1947
1948 /*
1949 * Loading guest fpu may have cleared host cr0.ts
1950 */
1951 vmcs_writel(HOST_CR0, read_cr0());
1952
Avi Kivity6aa8b732006-12-10 02:21:36 -08001953 asm (
1954 /* Store host registers */
1955 "pushf \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001956#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957 "push %%rax; push %%rbx; push %%rdx;"
1958 "push %%rsi; push %%rdi; push %%rbp;"
1959 "push %%r8; push %%r9; push %%r10; push %%r11;"
1960 "push %%r12; push %%r13; push %%r14; push %%r15;"
1961 "push %%rcx \n\t"
1962 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
1963#else
1964 "pusha; push %%ecx \n\t"
1965 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
1966#endif
1967 /* Check if vmlaunch of vmresume is needed */
1968 "cmp $0, %1 \n\t"
1969 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001970#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971 "mov %c[cr2](%3), %%rax \n\t"
1972 "mov %%rax, %%cr2 \n\t"
1973 "mov %c[rax](%3), %%rax \n\t"
1974 "mov %c[rbx](%3), %%rbx \n\t"
1975 "mov %c[rdx](%3), %%rdx \n\t"
1976 "mov %c[rsi](%3), %%rsi \n\t"
1977 "mov %c[rdi](%3), %%rdi \n\t"
1978 "mov %c[rbp](%3), %%rbp \n\t"
1979 "mov %c[r8](%3), %%r8 \n\t"
1980 "mov %c[r9](%3), %%r9 \n\t"
1981 "mov %c[r10](%3), %%r10 \n\t"
1982 "mov %c[r11](%3), %%r11 \n\t"
1983 "mov %c[r12](%3), %%r12 \n\t"
1984 "mov %c[r13](%3), %%r13 \n\t"
1985 "mov %c[r14](%3), %%r14 \n\t"
1986 "mov %c[r15](%3), %%r15 \n\t"
1987 "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */
1988#else
1989 "mov %c[cr2](%3), %%eax \n\t"
1990 "mov %%eax, %%cr2 \n\t"
1991 "mov %c[rax](%3), %%eax \n\t"
1992 "mov %c[rbx](%3), %%ebx \n\t"
1993 "mov %c[rdx](%3), %%edx \n\t"
1994 "mov %c[rsi](%3), %%esi \n\t"
1995 "mov %c[rdi](%3), %%edi \n\t"
1996 "mov %c[rbp](%3), %%ebp \n\t"
1997 "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */
1998#endif
1999 /* Enter guest mode */
2000 "jne launched \n\t"
2001 ASM_VMX_VMLAUNCH "\n\t"
2002 "jmp kvm_vmx_return \n\t"
2003 "launched: " ASM_VMX_VMRESUME "\n\t"
2004 ".globl kvm_vmx_return \n\t"
2005 "kvm_vmx_return: "
2006 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002007#ifdef CONFIG_X86_64
Ingo Molnar96958232007-02-12 00:54:33 -08002008 "xchg %3, (%%rsp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002009 "mov %%rax, %c[rax](%3) \n\t"
2010 "mov %%rbx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002011 "pushq (%%rsp); popq %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002012 "mov %%rdx, %c[rdx](%3) \n\t"
2013 "mov %%rsi, %c[rsi](%3) \n\t"
2014 "mov %%rdi, %c[rdi](%3) \n\t"
2015 "mov %%rbp, %c[rbp](%3) \n\t"
2016 "mov %%r8, %c[r8](%3) \n\t"
2017 "mov %%r9, %c[r9](%3) \n\t"
2018 "mov %%r10, %c[r10](%3) \n\t"
2019 "mov %%r11, %c[r11](%3) \n\t"
2020 "mov %%r12, %c[r12](%3) \n\t"
2021 "mov %%r13, %c[r13](%3) \n\t"
2022 "mov %%r14, %c[r14](%3) \n\t"
2023 "mov %%r15, %c[r15](%3) \n\t"
2024 "mov %%cr2, %%rax \n\t"
2025 "mov %%rax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002026 "mov (%%rsp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002027
2028 "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;"
2029 "pop %%r11; pop %%r10; pop %%r9; pop %%r8;"
2030 "pop %%rbp; pop %%rdi; pop %%rsi;"
2031 "pop %%rdx; pop %%rbx; pop %%rax \n\t"
2032#else
Ingo Molnar96958232007-02-12 00:54:33 -08002033 "xchg %3, (%%esp) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002034 "mov %%eax, %c[rax](%3) \n\t"
2035 "mov %%ebx, %c[rbx](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002036 "pushl (%%esp); popl %c[rcx](%3) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002037 "mov %%edx, %c[rdx](%3) \n\t"
2038 "mov %%esi, %c[rsi](%3) \n\t"
2039 "mov %%edi, %c[rdi](%3) \n\t"
2040 "mov %%ebp, %c[rbp](%3) \n\t"
2041 "mov %%cr2, %%eax \n\t"
2042 "mov %%eax, %c[cr2](%3) \n\t"
Ingo Molnar96958232007-02-12 00:54:33 -08002043 "mov (%%esp), %3 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002044
2045 "pop %%ecx; popa \n\t"
2046#endif
2047 "setbe %0 \n\t"
2048 "popf \n\t"
Herbert Xue0015482007-01-23 14:10:00 +11002049 : "=q" (fail)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050 : "r"(vcpu->launched), "d"((unsigned long)HOST_RSP),
2051 "c"(vcpu),
2052 [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])),
2053 [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])),
2054 [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])),
2055 [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])),
2056 [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])),
2057 [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])),
2058 [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002059#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002060 [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])),
2061 [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])),
2062 [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])),
2063 [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])),
2064 [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])),
2065 [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])),
2066 [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])),
2067 [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])),
2068#endif
2069 [cr2]"i"(offsetof(struct kvm_vcpu, cr2))
2070 : "cc", "memory" );
2071
Avi Kivity1165f5f2007-04-19 17:27:43 +03002072 ++vcpu->stat.exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073
Dor Laorc1150d82007-01-05 16:36:24 -08002074 vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002075
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076 asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002078 if (unlikely(fail)) {
Avi Kivity8eb7d332007-03-04 14:17:08 +02002079 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2080 kvm_run->fail_entry.hardware_entry_failure_reason
2081 = vmcs_read32(VM_INSTRUCTION_ERROR);
Avi Kivitye2dec932007-01-05 16:36:54 -08002082 r = 0;
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002083 goto out;
2084 }
2085 /*
2086 * Profile KVM exit RIPs:
2087 */
2088 if (unlikely(prof_on == KVM_PROFILING))
2089 profile_hit(KVM_PROFILING, (void *)vmcs_readl(GUEST_RIP));
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +01002090
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002091 vcpu->launched = 1;
2092 r = kvm_handle_exit(kvm_run, vcpu);
2093 if (r > 0) {
2094 /* Give scheduler a change to reschedule. */
2095 if (signal_pending(current)) {
2096 r = -EINTR;
2097 kvm_run->exit_reason = KVM_EXIT_INTR;
2098 ++vcpu->stat.signal_exits;
2099 goto out;
2100 }
Dor Laorc1150d82007-01-05 16:36:24 -08002101
Avi Kivity05e0c8c2007-04-30 16:15:58 +03002102 if (dm_request_for_irq_injection(vcpu, kvm_run)) {
2103 r = -EINTR;
2104 kvm_run->exit_reason = KVM_EXIT_INTR;
2105 ++vcpu->stat.request_irq_exits;
2106 goto out;
2107 }
2108 if (!need_resched()) {
2109 ++vcpu->stat.light_exits;
2110 goto again;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002111 }
2112 }
Dor Laorc1150d82007-01-05 16:36:24 -08002113
Avi Kivitye6adf282007-04-30 16:07:54 +03002114out:
Avi Kivitye6adf282007-04-30 16:07:54 +03002115 if (r > 0) {
2116 kvm_resched(vcpu);
2117 goto preempted;
2118 }
2119
Dor Laorc1150d82007-01-05 16:36:24 -08002120 post_kvm_run_save(vcpu, kvm_run);
Avi Kivitye2dec932007-01-05 16:36:54 -08002121 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002122}
2123
2124static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2125{
2126 vmcs_writel(GUEST_CR3, vmcs_readl(GUEST_CR3));
2127}
2128
2129static void vmx_inject_page_fault(struct kvm_vcpu *vcpu,
2130 unsigned long addr,
2131 u32 err_code)
2132{
2133 u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
2134
Avi Kivity1165f5f2007-04-19 17:27:43 +03002135 ++vcpu->stat.pf_guest;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136
2137 if (is_page_fault(vect_info)) {
2138 printk(KERN_DEBUG "inject_page_fault: "
2139 "double fault 0x%lx @ 0x%lx\n",
2140 addr, vmcs_readl(GUEST_RIP));
2141 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0);
2142 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2143 DF_VECTOR |
2144 INTR_TYPE_EXCEPTION |
2145 INTR_INFO_DELIEVER_CODE_MASK |
2146 INTR_INFO_VALID_MASK);
2147 return;
2148 }
2149 vcpu->cr2 = addr;
2150 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code);
2151 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2152 PF_VECTOR |
2153 INTR_TYPE_EXCEPTION |
2154 INTR_INFO_DELIEVER_CODE_MASK |
2155 INTR_INFO_VALID_MASK);
2156
2157}
2158
2159static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2160{
2161 if (vcpu->vmcs) {
2162 on_each_cpu(__vcpu_clear, vcpu, 0, 1);
2163 free_vmcs(vcpu->vmcs);
2164 vcpu->vmcs = NULL;
2165 }
2166}
2167
2168static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2169{
2170 vmx_free_vmcs(vcpu);
2171}
2172
2173static int vmx_create_vcpu(struct kvm_vcpu *vcpu)
2174{
2175 struct vmcs *vmcs;
2176
Ingo Molnar965b58a2007-01-05 16:36:23 -08002177 vcpu->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2178 if (!vcpu->guest_msrs)
2179 return -ENOMEM;
2180
2181 vcpu->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2182 if (!vcpu->host_msrs)
2183 goto out_free_guest_msrs;
2184
Avi Kivity6aa8b732006-12-10 02:21:36 -08002185 vmcs = alloc_vmcs();
2186 if (!vmcs)
Ingo Molnar965b58a2007-01-05 16:36:23 -08002187 goto out_free_msrs;
2188
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 vmcs_clear(vmcs);
2190 vcpu->vmcs = vmcs;
2191 vcpu->launched = 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002192
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 return 0;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002194
2195out_free_msrs:
2196 kfree(vcpu->host_msrs);
2197 vcpu->host_msrs = NULL;
2198
2199out_free_guest_msrs:
2200 kfree(vcpu->guest_msrs);
2201 vcpu->guest_msrs = NULL;
2202
2203 return -ENOMEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204}
2205
2206static struct kvm_arch_ops vmx_arch_ops = {
2207 .cpu_has_kvm_support = cpu_has_kvm_support,
2208 .disabled_by_bios = vmx_disabled_by_bios,
2209 .hardware_setup = hardware_setup,
2210 .hardware_unsetup = hardware_unsetup,
2211 .hardware_enable = hardware_enable,
2212 .hardware_disable = hardware_disable,
2213
2214 .vcpu_create = vmx_create_vcpu,
2215 .vcpu_free = vmx_free_vcpu,
2216
2217 .vcpu_load = vmx_vcpu_load,
2218 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002219 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220
2221 .set_guest_debug = set_guest_debug,
2222 .get_msr = vmx_get_msr,
2223 .set_msr = vmx_set_msr,
2224 .get_segment_base = vmx_get_segment_base,
2225 .get_segment = vmx_get_segment,
2226 .set_segment = vmx_set_segment,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002227 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002228 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002229 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002230 .set_cr3 = vmx_set_cr3,
2231 .set_cr4 = vmx_set_cr4,
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002232#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002233 .set_efer = vmx_set_efer,
2234#endif
2235 .get_idt = vmx_get_idt,
2236 .set_idt = vmx_set_idt,
2237 .get_gdt = vmx_get_gdt,
2238 .set_gdt = vmx_set_gdt,
2239 .cache_regs = vcpu_load_rsp_rip,
2240 .decache_regs = vcpu_put_rsp_rip,
2241 .get_rflags = vmx_get_rflags,
2242 .set_rflags = vmx_set_rflags,
2243
2244 .tlb_flush = vmx_flush_tlb,
2245 .inject_page_fault = vmx_inject_page_fault,
2246
2247 .inject_gp = vmx_inject_gp,
2248
2249 .run = vmx_vcpu_run,
2250 .skip_emulated_instruction = skip_emulated_instruction,
2251 .vcpu_setup = vmx_vcpu_setup,
Ingo Molnar102d8322007-02-19 14:37:47 +02002252 .patch_hypercall = vmx_patch_hypercall,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002253};
2254
2255static int __init vmx_init(void)
2256{
He, Qingfdef3ad2007-04-30 09:45:24 +03002257 void *iova;
2258 int r;
2259
2260 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2261 if (!vmx_io_bitmap_a)
2262 return -ENOMEM;
2263
2264 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2265 if (!vmx_io_bitmap_b) {
2266 r = -ENOMEM;
2267 goto out;
2268 }
2269
2270 /*
2271 * Allow direct access to the PC debug port (it is often used for I/O
2272 * delays, but the vmexits simply slow things down).
2273 */
2274 iova = kmap(vmx_io_bitmap_a);
2275 memset(iova, 0xff, PAGE_SIZE);
2276 clear_bit(0x80, iova);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002277 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002278
2279 iova = kmap(vmx_io_bitmap_b);
2280 memset(iova, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002281 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002282
2283 r = kvm_init_arch(&vmx_arch_ops, THIS_MODULE);
2284 if (r)
2285 goto out1;
2286
2287 return 0;
2288
2289out1:
2290 __free_page(vmx_io_bitmap_b);
2291out:
2292 __free_page(vmx_io_bitmap_a);
2293 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002294}
2295
2296static void __exit vmx_exit(void)
2297{
He, Qingfdef3ad2007-04-30 09:45:24 +03002298 __free_page(vmx_io_bitmap_b);
2299 __free_page(vmx_io_bitmap_a);
2300
Avi Kivity6aa8b732006-12-10 02:21:36 -08002301 kvm_exit_arch();
2302}
2303
2304module_init(vmx_init)
2305module_exit(vmx_exit)