blob: 6249810b21559fa66bba32c8dfae081cf4d7016f [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
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Avi Kivity6aa8b732006-12-10 02:21:36 -080019#include "vmx.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/mm.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020028#include <linux/moduleparam.h>
Avi Kivitye4956062007-06-28 14:15:57 -040029
Avi Kivity6aa8b732006-12-10 02:21:36 -080030#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080031#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080032
Avi Kivity6aa8b732006-12-10 02:21:36 -080033MODULE_AUTHOR("Qumranet");
34MODULE_LICENSE("GPL");
35
Avi Kivityc7addb92007-09-16 18:58:32 +020036static int bypass_guest_pf = 1;
37module_param(bypass_guest_pf, bool, 0);
38
Sheng Yang2384d2b2008-01-17 15:14:33 +080039static int enable_vpid = 1;
40module_param(enable_vpid, bool, 0);
41
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020042static int flexpriority_enabled = 1;
43module_param(flexpriority_enabled, bool, 0);
44
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040045struct vmcs {
46 u32 revision_id;
47 u32 abort;
48 char data[0];
49};
50
51struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100052 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040053 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030054 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020055 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040056 struct kvm_msr_entry *guest_msrs;
57 struct kvm_msr_entry *host_msrs;
58 int nmsrs;
59 int save_nmsrs;
60 int msr_offset_efer;
61#ifdef CONFIG_X86_64
62 int msr_offset_kernel_gs_base;
63#endif
64 struct vmcs *vmcs;
65 struct {
66 int loaded;
67 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020068 int gs_ldt_reload_needed;
69 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030070 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040071 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020072 struct {
73 struct {
74 bool pending;
75 u8 vector;
76 unsigned rip;
77 } irq;
78 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080079 int vpid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040080};
81
82static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
83{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100084 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040085}
86
Avi Kivity75880a02007-06-20 11:20:04 +030087static int init_rmode_tss(struct kvm *kvm);
88
Avi Kivity6aa8b732006-12-10 02:21:36 -080089static DEFINE_PER_CPU(struct vmcs *, vmxarea);
90static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
91
He, Qingfdef3ad2007-04-30 09:45:24 +030092static struct page *vmx_io_bitmap_a;
93static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +080094static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +030095
Sheng Yang2384d2b2008-01-17 15:14:33 +080096static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
97static DEFINE_SPINLOCK(vmx_vpid_lock);
98
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +030099static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800100 int size;
101 int order;
102 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300103 u32 pin_based_exec_ctrl;
104 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800105 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300106 u32 vmexit_ctrl;
107 u32 vmentry_ctrl;
108} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800109
110#define VMX_SEGMENT_FIELD(seg) \
111 [VCPU_SREG_##seg] = { \
112 .selector = GUEST_##seg##_SELECTOR, \
113 .base = GUEST_##seg##_BASE, \
114 .limit = GUEST_##seg##_LIMIT, \
115 .ar_bytes = GUEST_##seg##_AR_BYTES, \
116 }
117
118static struct kvm_vmx_segment_field {
119 unsigned selector;
120 unsigned base;
121 unsigned limit;
122 unsigned ar_bytes;
123} kvm_vmx_segment_fields[] = {
124 VMX_SEGMENT_FIELD(CS),
125 VMX_SEGMENT_FIELD(DS),
126 VMX_SEGMENT_FIELD(ES),
127 VMX_SEGMENT_FIELD(FS),
128 VMX_SEGMENT_FIELD(GS),
129 VMX_SEGMENT_FIELD(SS),
130 VMX_SEGMENT_FIELD(TR),
131 VMX_SEGMENT_FIELD(LDTR),
132};
133
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300134/*
135 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
136 * away by decrementing the array size.
137 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800138static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800139#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800140 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
141#endif
142 MSR_EFER, MSR_K6_STAR,
143};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200144#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800145
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400146static void load_msrs(struct kvm_msr_entry *e, int n)
147{
148 int i;
149
150 for (i = 0; i < n; ++i)
151 wrmsrl(e[i].index, e[i].data);
152}
153
154static void save_msrs(struct kvm_msr_entry *e, int n)
155{
156 int i;
157
158 for (i = 0; i < n; ++i)
159 rdmsrl(e[i].index, e[i].data);
160}
161
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162static inline int is_page_fault(u32 intr_info)
163{
164 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
165 INTR_INFO_VALID_MASK)) ==
166 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
167}
168
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300169static inline int is_no_device(u32 intr_info)
170{
171 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
172 INTR_INFO_VALID_MASK)) ==
173 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
174}
175
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500176static inline int is_invalid_opcode(u32 intr_info)
177{
178 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
179 INTR_INFO_VALID_MASK)) ==
180 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
181}
182
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183static inline int is_external_interrupt(u32 intr_info)
184{
185 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
186 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
187}
188
Sheng Yang25c5f222008-03-28 13:18:56 +0800189static inline int cpu_has_vmx_msr_bitmap(void)
190{
191 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
192}
193
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800194static inline int cpu_has_vmx_tpr_shadow(void)
195{
196 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
197}
198
199static inline int vm_need_tpr_shadow(struct kvm *kvm)
200{
201 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
202}
203
Sheng Yangf78e0e22007-10-29 09:40:42 +0800204static inline int cpu_has_secondary_exec_ctrls(void)
205{
206 return (vmcs_config.cpu_based_exec_ctrl &
207 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
208}
209
Avi Kivity774ead32007-12-26 13:57:04 +0200210static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800211{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200212 return flexpriority_enabled
213 && (vmcs_config.cpu_based_2nd_exec_ctrl &
214 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800215}
216
217static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
218{
219 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
220 (irqchip_in_kernel(kvm)));
221}
222
Sheng Yang2384d2b2008-01-17 15:14:33 +0800223static inline int cpu_has_vmx_vpid(void)
224{
225 return (vmcs_config.cpu_based_2nd_exec_ctrl &
226 SECONDARY_EXEC_ENABLE_VPID);
227}
228
Rusty Russell8b9cf982007-07-30 16:31:43 +1000229static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800230{
231 int i;
232
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400233 for (i = 0; i < vmx->nmsrs; ++i)
234 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300235 return i;
236 return -1;
237}
238
Sheng Yang2384d2b2008-01-17 15:14:33 +0800239static inline void __invvpid(int ext, u16 vpid, gva_t gva)
240{
241 struct {
242 u64 vpid : 16;
243 u64 rsvd : 48;
244 u64 gva;
245 } operand = { vpid, 0, gva };
246
247 asm volatile (ASM_VMX_INVVPID
248 /* CF==1 or ZF==1 --> rc = -1 */
249 "; ja 1f ; ud2 ; 1:"
250 : : "a"(&operand), "c"(ext) : "cc", "memory");
251}
252
Rusty Russell8b9cf982007-07-30 16:31:43 +1000253static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300254{
255 int i;
256
Rusty Russell8b9cf982007-07-30 16:31:43 +1000257 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300258 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400259 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000260 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800261}
262
Avi Kivity6aa8b732006-12-10 02:21:36 -0800263static void vmcs_clear(struct vmcs *vmcs)
264{
265 u64 phys_addr = __pa(vmcs);
266 u8 error;
267
268 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
269 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
270 : "cc", "memory");
271 if (error)
272 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
273 vmcs, phys_addr);
274}
275
276static void __vcpu_clear(void *arg)
277{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000278 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800279 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800280
Rusty Russell8b9cf982007-07-30 16:31:43 +1000281 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400282 vmcs_clear(vmx->vmcs);
283 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800285 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800286}
287
Rusty Russell8b9cf982007-07-30 16:31:43 +1000288static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800289{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200290 if (vmx->vcpu.cpu == -1)
291 return;
Avi Kivityf566e092007-09-30 11:02:53 +0200292 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 0, 1);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000293 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800294}
295
Sheng Yang2384d2b2008-01-17 15:14:33 +0800296static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
297{
298 if (vmx->vpid == 0)
299 return;
300
301 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
302}
303
Avi Kivity6aa8b732006-12-10 02:21:36 -0800304static unsigned long vmcs_readl(unsigned long field)
305{
306 unsigned long value;
307
308 asm volatile (ASM_VMX_VMREAD_RDX_RAX
309 : "=a"(value) : "d"(field) : "cc");
310 return value;
311}
312
313static u16 vmcs_read16(unsigned long field)
314{
315 return vmcs_readl(field);
316}
317
318static u32 vmcs_read32(unsigned long field)
319{
320 return vmcs_readl(field);
321}
322
323static u64 vmcs_read64(unsigned long field)
324{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800325#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800326 return vmcs_readl(field);
327#else
328 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
329#endif
330}
331
Avi Kivitye52de1b2007-01-05 16:36:56 -0800332static noinline void vmwrite_error(unsigned long field, unsigned long value)
333{
334 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
335 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
336 dump_stack();
337}
338
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339static void vmcs_writel(unsigned long field, unsigned long value)
340{
341 u8 error;
342
343 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400344 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800345 if (unlikely(error))
346 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800347}
348
349static void vmcs_write16(unsigned long field, u16 value)
350{
351 vmcs_writel(field, value);
352}
353
354static void vmcs_write32(unsigned long field, u32 value)
355{
356 vmcs_writel(field, value);
357}
358
359static void vmcs_write64(unsigned long field, u64 value)
360{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800361#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800362 vmcs_writel(field, value);
363#else
364 vmcs_writel(field, value);
365 asm volatile ("");
366 vmcs_writel(field+1, value >> 32);
367#endif
368}
369
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300370static void vmcs_clear_bits(unsigned long field, u32 mask)
371{
372 vmcs_writel(field, vmcs_readl(field) & ~mask);
373}
374
375static void vmcs_set_bits(unsigned long field, u32 mask)
376{
377 vmcs_writel(field, vmcs_readl(field) | mask);
378}
379
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300380static void update_exception_bitmap(struct kvm_vcpu *vcpu)
381{
382 u32 eb;
383
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500384 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300385 if (!vcpu->fpu_active)
386 eb |= 1u << NM_VECTOR;
387 if (vcpu->guest_debug.enabled)
388 eb |= 1u << 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800389 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300390 eb = ~0;
391 vmcs_write32(EXCEPTION_BITMAP, eb);
392}
393
Avi Kivity33ed6322007-05-02 16:54:03 +0300394static void reload_tss(void)
395{
Avi Kivity33ed6322007-05-02 16:54:03 +0300396 /*
397 * VT restores TR but not its size. Useless.
398 */
399 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200400 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300401
402 get_gdt(&gdt);
403 descs = (void *)gdt.base;
404 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
405 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300406}
407
Rusty Russell8b9cf982007-07-30 16:31:43 +1000408static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300409{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400410 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300411 u64 host_efer = vmx->host_msrs[efer_offset].data;
412 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
413 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300414
Avi Kivity51c6cf62007-08-29 03:48:05 +0300415 if (efer_offset < 0)
416 return;
417 /*
418 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
419 * outside long mode
420 */
421 ignore_bits = EFER_NX | EFER_SCE;
422#ifdef CONFIG_X86_64
423 ignore_bits |= EFER_LMA | EFER_LME;
424 /* SCE is meaningful only in long mode on Intel */
425 if (guest_efer & EFER_LMA)
426 ignore_bits &= ~(u64)EFER_SCE;
427#endif
428 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
429 return;
430
431 vmx->host_state.guest_efer_loaded = 1;
432 guest_efer &= ~ignore_bits;
433 guest_efer |= host_efer & ignore_bits;
434 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000435 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300436}
437
Avi Kivity51c6cf62007-08-29 03:48:05 +0300438static void reload_host_efer(struct vcpu_vmx *vmx)
439{
440 if (vmx->host_state.guest_efer_loaded) {
441 vmx->host_state.guest_efer_loaded = 0;
442 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
443 }
444}
445
Avi Kivity04d2cc72007-09-10 18:10:54 +0300446static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300447{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300448 struct vcpu_vmx *vmx = to_vmx(vcpu);
449
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400450 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300451 return;
452
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400453 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300454 /*
455 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
456 * allow segment selectors with cpl > 0 or ti == 1.
457 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400458 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200459 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400460 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200461 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400462 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200463 vmx->host_state.fs_reload_needed = 0;
464 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300465 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200466 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300467 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400468 vmx->host_state.gs_sel = read_gs();
469 if (!(vmx->host_state.gs_sel & 7))
470 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300471 else {
472 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200473 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300474 }
475
476#ifdef CONFIG_X86_64
477 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
478 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
479#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400480 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
481 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300482#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300483
484#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400485 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400486 save_msrs(vmx->host_msrs +
487 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400488
Avi Kivity707c0872007-05-02 17:33:43 +0300489#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400490 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300491 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300492}
493
Rusty Russell8b9cf982007-07-30 16:31:43 +1000494static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300495{
Avi Kivity15ad7142007-07-11 18:17:21 +0300496 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300497
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400498 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300499 return;
500
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200501 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400502 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200503 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400504 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200505 if (vmx->host_state.gs_ldt_reload_needed) {
506 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300507 /*
508 * If we have to reload gs, we must take care to
509 * preserve our gs base.
510 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300511 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400512 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300513#ifdef CONFIG_X86_64
514 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
515#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300516 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300517 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200518 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400519 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
520 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300521 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300522}
523
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524/*
525 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
526 * vcpu mutex is already taken.
527 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300528static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400530 struct vcpu_vmx *vmx = to_vmx(vcpu);
531 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200532 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533
Eddie Donga3d7f852007-09-03 16:15:12 +0300534 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000535 vcpu_clear(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300536 kvm_migrate_apic_timer(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800537 vpid_sync_vcpu_all(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300538 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400540 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541 u8 error;
542
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400543 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800544 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
545 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
546 : "cc");
547 if (error)
548 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400549 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800550 }
551
552 if (vcpu->cpu != cpu) {
553 struct descriptor_table dt;
554 unsigned long sysenter_esp;
555
556 vcpu->cpu = cpu;
557 /*
558 * Linux uses per-cpu TSS and GDT, so set these when switching
559 * processors.
560 */
561 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
562 get_gdt(&dt);
563 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
564
565 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
566 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300567
568 /*
569 * Make sure the time stamp counter is monotonous.
570 */
571 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200572 if (tsc_this < vcpu->arch.host_tsc) {
573 delta = vcpu->arch.host_tsc - tsc_this;
574 new_offset = vmcs_read64(TSC_OFFSET) + delta;
575 vmcs_write64(TSC_OFFSET, new_offset);
576 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578}
579
580static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
581{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000582 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583}
584
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300585static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
586{
587 if (vcpu->fpu_active)
588 return;
589 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000590 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800591 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000592 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300593 update_exception_bitmap(vcpu);
594}
595
596static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
597{
598 if (!vcpu->fpu_active)
599 return;
600 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000601 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300602 update_exception_bitmap(vcpu);
603}
604
Avi Kivity774c47f2007-02-12 00:54:47 -0800605static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
606{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000607 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800608}
609
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
611{
612 return vmcs_readl(GUEST_RFLAGS);
613}
614
615static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
616{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800617 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100618 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619 vmcs_writel(GUEST_RFLAGS, rflags);
620}
621
622static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
623{
624 unsigned long rip;
625 u32 interruptibility;
626
627 rip = vmcs_readl(GUEST_RIP);
628 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
629 vmcs_writel(GUEST_RIP, rip);
630
631 /*
632 * We emulated an instruction, so temporary interrupt blocking
633 * should be removed, if set.
634 */
635 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
636 if (interruptibility & 3)
637 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
638 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800639 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640}
641
Avi Kivity298101d2007-11-25 13:41:11 +0200642static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
643 bool has_error_code, u32 error_code)
644{
645 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
646 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600647 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200648 | INTR_INFO_VALID_MASK);
649 if (has_error_code)
650 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
651}
652
653static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
654{
655 struct vcpu_vmx *vmx = to_vmx(vcpu);
656
657 return !(vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
658}
659
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660/*
Eddie Donga75beee2007-05-17 18:55:15 +0300661 * Swap MSR entry in host/guest MSR entry array.
662 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200663#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000664static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300665{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400666 struct kvm_msr_entry tmp;
667
668 tmp = vmx->guest_msrs[to];
669 vmx->guest_msrs[to] = vmx->guest_msrs[from];
670 vmx->guest_msrs[from] = tmp;
671 tmp = vmx->host_msrs[to];
672 vmx->host_msrs[to] = vmx->host_msrs[from];
673 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300674}
Gabriel C54e11fa2007-08-01 16:23:10 +0200675#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300676
677/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300678 * Set up the vmcs to automatically save and restore system
679 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
680 * mode, as fiddling with msrs is very expensive.
681 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000682static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300683{
Eddie Dong2cc51562007-05-21 07:28:09 +0300684 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300685
Avi Kivity33f9c502008-02-27 16:06:57 +0200686 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300687 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300688#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000689 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300690 int index;
691
Rusty Russell8b9cf982007-07-30 16:31:43 +1000692 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300693 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000694 move_msr_up(vmx, index, save_nmsrs++);
695 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300696 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000697 move_msr_up(vmx, index, save_nmsrs++);
698 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300699 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000700 move_msr_up(vmx, index, save_nmsrs++);
701 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300702 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000703 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300704 /*
705 * MSR_K6_STAR is only needed on long mode guests, and only
706 * if efer.sce is enabled.
707 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000708 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800709 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000710 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300711 }
Eddie Donga75beee2007-05-17 18:55:15 +0300712#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400713 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300714
Eddie Donga75beee2007-05-17 18:55:15 +0300715#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400716 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000717 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300718#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000719 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300720}
721
722/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 * reads and returns guest's timestamp counter "register"
724 * guest_tsc = host_tsc + tsc_offset -- 21.3
725 */
726static u64 guest_read_tsc(void)
727{
728 u64 host_tsc, tsc_offset;
729
730 rdtscll(host_tsc);
731 tsc_offset = vmcs_read64(TSC_OFFSET);
732 return host_tsc + tsc_offset;
733}
734
735/*
736 * writes 'guest_tsc' into guest's timestamp counter "register"
737 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
738 */
739static void guest_write_tsc(u64 guest_tsc)
740{
741 u64 host_tsc;
742
743 rdtscll(host_tsc);
744 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
745}
746
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747/*
748 * Reads an msr value (of 'msr_index') into 'pdata'.
749 * Returns 0 on success, non-0 otherwise.
750 * Assumes vcpu_load() was already called.
751 */
752static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
753{
754 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400755 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756
757 if (!pdata) {
758 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
759 return -EINVAL;
760 }
761
762 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800763#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 case MSR_FS_BASE:
765 data = vmcs_readl(GUEST_FS_BASE);
766 break;
767 case MSR_GS_BASE:
768 data = vmcs_readl(GUEST_GS_BASE);
769 break;
770 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800771 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772#endif
773 case MSR_IA32_TIME_STAMP_COUNTER:
774 data = guest_read_tsc();
775 break;
776 case MSR_IA32_SYSENTER_CS:
777 data = vmcs_read32(GUEST_SYSENTER_CS);
778 break;
779 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200780 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781 break;
782 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200783 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800785 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000786 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800787 if (msr) {
788 data = msr->data;
789 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800791 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800792 }
793
794 *pdata = data;
795 return 0;
796}
797
798/*
799 * Writes msr value into into the appropriate "register".
800 * Returns 0 on success, non-0 otherwise.
801 * Assumes vcpu_load() was already called.
802 */
803static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
804{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400805 struct vcpu_vmx *vmx = to_vmx(vcpu);
806 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300807 int ret = 0;
808
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800810#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800811 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300812 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300813 if (vmx->host_state.loaded) {
814 reload_host_efer(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000815 load_transition_efer(vmx);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300816 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300817 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 case MSR_FS_BASE:
819 vmcs_writel(GUEST_FS_BASE, data);
820 break;
821 case MSR_GS_BASE:
822 vmcs_writel(GUEST_GS_BASE, data);
823 break;
824#endif
825 case MSR_IA32_SYSENTER_CS:
826 vmcs_write32(GUEST_SYSENTER_CS, data);
827 break;
828 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200829 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800830 break;
831 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200832 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800833 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200834 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800835 guest_write_tsc(data);
836 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000838 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800839 if (msr) {
840 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400841 if (vmx->host_state.loaded)
842 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800843 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300845 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800846 }
847
Eddie Dong2cc51562007-05-21 07:28:09 +0300848 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800849}
850
851/*
852 * Sync the rsp and rip registers into the vcpu structure. This allows
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800853 * registers to be accessed by indexing vcpu->arch.regs.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854 */
855static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
856{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800857 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
858 vcpu->arch.rip = vmcs_readl(GUEST_RIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859}
860
861/*
862 * Syncs rsp and rip back into the vmcs. Should be called after possible
863 * modification.
864 */
865static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
866{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800867 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
868 vmcs_writel(GUEST_RIP, vcpu->arch.rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869}
870
871static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
872{
873 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800874 int old_singlestep;
875
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876 old_singlestep = vcpu->guest_debug.singlestep;
877
878 vcpu->guest_debug.enabled = dbg->enabled;
879 if (vcpu->guest_debug.enabled) {
880 int i;
881
882 dr7 |= 0x200; /* exact */
883 for (i = 0; i < 4; ++i) {
884 if (!dbg->breakpoints[i].enabled)
885 continue;
886 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
887 dr7 |= 2 << (i*2); /* global enable */
888 dr7 |= 0 << (i*4+16); /* execution breakpoint */
889 }
890
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300892 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800894
895 if (old_singlestep && !vcpu->guest_debug.singlestep) {
896 unsigned long flags;
897
898 flags = vmcs_readl(GUEST_RFLAGS);
899 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
900 vmcs_writel(GUEST_RFLAGS, flags);
901 }
902
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300903 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800904 vmcs_writel(GUEST_DR7, dr7);
905
906 return 0;
907}
908
Eddie Dong2a8067f2007-08-06 16:29:07 +0300909static int vmx_get_irq(struct kvm_vcpu *vcpu)
910{
Avi Kivity1155f762007-11-22 11:30:47 +0200911 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +0300912 u32 idtv_info_field;
913
Avi Kivity1155f762007-11-22 11:30:47 +0200914 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong2a8067f2007-08-06 16:29:07 +0300915 if (idtv_info_field & INTR_INFO_VALID_MASK) {
916 if (is_external_interrupt(idtv_info_field))
917 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
918 else
Mike Dayd77c26f2007-10-08 09:02:08 -0400919 printk(KERN_DEBUG "pending exception: not handled yet\n");
Eddie Dong2a8067f2007-08-06 16:29:07 +0300920 }
921 return -1;
922}
923
Avi Kivity6aa8b732006-12-10 02:21:36 -0800924static __init int cpu_has_kvm_support(void)
925{
926 unsigned long ecx = cpuid_ecx(1);
927 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
928}
929
930static __init int vmx_disabled_by_bios(void)
931{
932 u64 msr;
933
934 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300935 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
936 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
937 == MSR_IA32_FEATURE_CONTROL_LOCKED;
938 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800939}
940
Avi Kivity774c47f2007-02-12 00:54:47 -0800941static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942{
943 int cpu = raw_smp_processor_id();
944 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
945 u64 old;
946
947 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300948 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
949 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
950 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
951 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300953 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
954 MSR_IA32_FEATURE_CONTROL_LOCKED |
955 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000956 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800957 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
958 : "memory", "cc");
959}
960
961static void hardware_disable(void *garbage)
962{
963 asm volatile (ASM_VMX_VMXOFF : : : "cc");
964}
965
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300966static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -0400967 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968{
969 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300970 u32 ctl = ctl_min | ctl_opt;
971
972 rdmsr(msr, vmx_msr_low, vmx_msr_high);
973
974 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
975 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
976
977 /* Ensure minimum (required) set of control bits are supported. */
978 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300979 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300980
981 *result = ctl;
982 return 0;
983}
984
Yang, Sheng002c7f72007-07-31 14:23:01 +0300985static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300986{
987 u32 vmx_msr_low, vmx_msr_high;
988 u32 min, opt;
989 u32 _pin_based_exec_control = 0;
990 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800991 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300992 u32 _vmexit_control = 0;
993 u32 _vmentry_control = 0;
994
995 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
996 opt = 0;
997 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
998 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +0300999 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001000
1001 min = CPU_BASED_HLT_EXITING |
1002#ifdef CONFIG_X86_64
1003 CPU_BASED_CR8_LOAD_EXITING |
1004 CPU_BASED_CR8_STORE_EXITING |
1005#endif
1006 CPU_BASED_USE_IO_BITMAPS |
1007 CPU_BASED_MOV_DR_EXITING |
1008 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001009 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001010 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001011 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001012 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1013 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001014 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001015#ifdef CONFIG_X86_64
1016 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1017 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1018 ~CPU_BASED_CR8_STORE_EXITING;
1019#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001020 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
1021 min = 0;
Eddie Donge5edaa02007-11-11 12:28:35 +02001022 opt = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001023 SECONDARY_EXEC_WBINVD_EXITING |
1024 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001025 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS2,
1026 &_cpu_based_2nd_exec_control) < 0)
1027 return -EIO;
1028 }
1029#ifndef CONFIG_X86_64
1030 if (!(_cpu_based_2nd_exec_control &
1031 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1032 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1033#endif
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001034
1035 min = 0;
1036#ifdef CONFIG_X86_64
1037 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1038#endif
1039 opt = 0;
1040 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1041 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001042 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001043
1044 min = opt = 0;
1045 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1046 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001047 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001049 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001050
1051 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1052 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001053 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001054
1055#ifdef CONFIG_X86_64
1056 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1057 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001058 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001059#endif
1060
1061 /* Require Write-Back (WB) memory type for VMCS accesses. */
1062 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001063 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001064
Yang, Sheng002c7f72007-07-31 14:23:01 +03001065 vmcs_conf->size = vmx_msr_high & 0x1fff;
1066 vmcs_conf->order = get_order(vmcs_config.size);
1067 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001068
Yang, Sheng002c7f72007-07-31 14:23:01 +03001069 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1070 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001071 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001072 vmcs_conf->vmexit_ctrl = _vmexit_control;
1073 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001074
1075 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001076}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077
1078static struct vmcs *alloc_vmcs_cpu(int cpu)
1079{
1080 int node = cpu_to_node(cpu);
1081 struct page *pages;
1082 struct vmcs *vmcs;
1083
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001084 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 if (!pages)
1086 return NULL;
1087 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001088 memset(vmcs, 0, vmcs_config.size);
1089 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001090 return vmcs;
1091}
1092
1093static struct vmcs *alloc_vmcs(void)
1094{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001095 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096}
1097
1098static void free_vmcs(struct vmcs *vmcs)
1099{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001100 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101}
1102
Sam Ravnborg39959582007-06-01 00:47:13 -07001103static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104{
1105 int cpu;
1106
1107 for_each_online_cpu(cpu)
1108 free_vmcs(per_cpu(vmxarea, cpu));
1109}
1110
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111static __init int alloc_kvm_area(void)
1112{
1113 int cpu;
1114
1115 for_each_online_cpu(cpu) {
1116 struct vmcs *vmcs;
1117
1118 vmcs = alloc_vmcs_cpu(cpu);
1119 if (!vmcs) {
1120 free_kvm_area();
1121 return -ENOMEM;
1122 }
1123
1124 per_cpu(vmxarea, cpu) = vmcs;
1125 }
1126 return 0;
1127}
1128
1129static __init int hardware_setup(void)
1130{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001131 if (setup_vmcs_config(&vmcs_config) < 0)
1132 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001133
1134 if (boot_cpu_has(X86_FEATURE_NX))
1135 kvm_enable_efer_bits(EFER_NX);
1136
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 return alloc_kvm_area();
1138}
1139
1140static __exit void hardware_unsetup(void)
1141{
1142 free_kvm_area();
1143}
1144
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1146{
1147 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1148
Avi Kivity6af11b92007-03-19 13:18:10 +02001149 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150 vmcs_write16(sf->selector, save->selector);
1151 vmcs_writel(sf->base, save->base);
1152 vmcs_write32(sf->limit, save->limit);
1153 vmcs_write32(sf->ar_bytes, save->ar);
1154 } else {
1155 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1156 << AR_DPL_SHIFT;
1157 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1158 }
1159}
1160
1161static void enter_pmode(struct kvm_vcpu *vcpu)
1162{
1163 unsigned long flags;
1164
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001165 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001166
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001167 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1168 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1169 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001170
1171 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001172 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001173 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174 vmcs_writel(GUEST_RFLAGS, flags);
1175
Rusty Russell66aee912007-07-17 23:34:16 +10001176 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1177 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001178
1179 update_exception_bitmap(vcpu);
1180
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001181 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1182 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1183 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1184 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185
1186 vmcs_write16(GUEST_SS_SELECTOR, 0);
1187 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1188
1189 vmcs_write16(GUEST_CS_SELECTOR,
1190 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1191 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1192}
1193
Mike Dayd77c26f2007-10-08 09:02:08 -04001194static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001196 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001197 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1198 kvm->memslots[0].npages - 3;
1199 return base_gfn << PAGE_SHIFT;
1200 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001201 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001202}
1203
1204static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1205{
1206 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1207
1208 save->selector = vmcs_read16(sf->selector);
1209 save->base = vmcs_readl(sf->base);
1210 save->limit = vmcs_read32(sf->limit);
1211 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001212 vmcs_write16(sf->selector, save->base >> 4);
1213 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 vmcs_write32(sf->limit, 0xffff);
1215 vmcs_write32(sf->ar_bytes, 0xf3);
1216}
1217
1218static void enter_rmode(struct kvm_vcpu *vcpu)
1219{
1220 unsigned long flags;
1221
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001222 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001224 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1226
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001227 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001228 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1229
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001230 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1232
1233 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001234 vcpu->arch.rmode.save_iopl
1235 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001237 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238
1239 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001240 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001241 update_exception_bitmap(vcpu);
1242
1243 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1244 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1245 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1246
1247 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001248 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001249 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1250 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1252
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001253 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1254 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1255 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1256 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001257
Eddie Dong8668a3c2007-10-10 14:26:45 +08001258 kvm_mmu_reset_context(vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +03001259 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001260}
1261
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001262#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001263
1264static void enter_lmode(struct kvm_vcpu *vcpu)
1265{
1266 u32 guest_tr_ar;
1267
1268 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1269 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1270 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001271 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 vmcs_write32(GUEST_TR_AR_BYTES,
1273 (guest_tr_ar & ~AR_TYPE_MASK)
1274 | AR_TYPE_BUSY_64_TSS);
1275 }
1276
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001277 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278
Rusty Russell8b9cf982007-07-30 16:31:43 +10001279 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280 vmcs_write32(VM_ENTRY_CONTROLS,
1281 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001282 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283}
1284
1285static void exit_lmode(struct kvm_vcpu *vcpu)
1286{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001287 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001288
1289 vmcs_write32(VM_ENTRY_CONTROLS,
1290 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001291 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292}
1293
1294#endif
1295
Sheng Yang2384d2b2008-01-17 15:14:33 +08001296static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1297{
1298 vpid_sync_vcpu_all(to_vmx(vcpu));
1299}
1300
Anthony Liguori25c4c272007-04-27 09:29:21 +03001301static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001302{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001303 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1304 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001305}
1306
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1308{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001309 vmx_fpu_deactivate(vcpu);
1310
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001311 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312 enter_pmode(vcpu);
1313
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001314 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001315 enter_rmode(vcpu);
1316
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001317#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001318 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001319 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001321 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001322 exit_lmode(vcpu);
1323 }
1324#endif
1325
1326 vmcs_writel(CR0_READ_SHADOW, cr0);
1327 vmcs_writel(GUEST_CR0,
1328 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001329 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001330
Rusty Russell707d92fa2007-07-17 23:19:08 +10001331 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001332 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333}
1334
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1336{
Sheng Yang2384d2b2008-01-17 15:14:33 +08001337 vmx_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 vmcs_writel(GUEST_CR3, cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001339 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001340 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341}
1342
1343static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1344{
1345 vmcs_writel(CR4_READ_SHADOW, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001346 vmcs_writel(GUEST_CR4, cr4 | (vcpu->arch.rmode.active ?
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001348 vcpu->arch.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001349}
1350
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1352{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001353 struct vcpu_vmx *vmx = to_vmx(vcpu);
1354 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001356 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001357 if (!msr)
1358 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359 if (efer & EFER_LMA) {
1360 vmcs_write32(VM_ENTRY_CONTROLS,
1361 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001362 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 msr->data = efer;
1364
1365 } else {
1366 vmcs_write32(VM_ENTRY_CONTROLS,
1367 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001368 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369
1370 msr->data = efer & ~EFER_LME;
1371 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001372 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373}
1374
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1376{
1377 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1378
1379 return vmcs_readl(sf->base);
1380}
1381
1382static void vmx_get_segment(struct kvm_vcpu *vcpu,
1383 struct kvm_segment *var, int seg)
1384{
1385 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1386 u32 ar;
1387
1388 var->base = vmcs_readl(sf->base);
1389 var->limit = vmcs_read32(sf->limit);
1390 var->selector = vmcs_read16(sf->selector);
1391 ar = vmcs_read32(sf->ar_bytes);
1392 if (ar & AR_UNUSABLE_MASK)
1393 ar = 0;
1394 var->type = ar & 15;
1395 var->s = (ar >> 4) & 1;
1396 var->dpl = (ar >> 5) & 3;
1397 var->present = (ar >> 7) & 1;
1398 var->avl = (ar >> 12) & 1;
1399 var->l = (ar >> 13) & 1;
1400 var->db = (ar >> 14) & 1;
1401 var->g = (ar >> 15) & 1;
1402 var->unusable = (ar >> 16) & 1;
1403}
1404
Izik Eidus2e4d2652008-03-24 19:38:34 +02001405static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1406{
1407 struct kvm_segment kvm_seg;
1408
1409 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1410 return 0;
1411
1412 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1413 return 3;
1414
1415 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1416 return kvm_seg.selector & 3;
1417}
1418
Avi Kivity653e3102007-05-07 10:55:37 +03001419static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001420{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 u32 ar;
1422
Avi Kivity653e3102007-05-07 10:55:37 +03001423 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424 ar = 1 << 16;
1425 else {
1426 ar = var->type & 15;
1427 ar |= (var->s & 1) << 4;
1428 ar |= (var->dpl & 3) << 5;
1429 ar |= (var->present & 1) << 7;
1430 ar |= (var->avl & 1) << 12;
1431 ar |= (var->l & 1) << 13;
1432 ar |= (var->db & 1) << 14;
1433 ar |= (var->g & 1) << 15;
1434 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001435 if (ar == 0) /* a 0 value means unusable */
1436 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001437
1438 return ar;
1439}
1440
1441static void vmx_set_segment(struct kvm_vcpu *vcpu,
1442 struct kvm_segment *var, int seg)
1443{
1444 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1445 u32 ar;
1446
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001447 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1448 vcpu->arch.rmode.tr.selector = var->selector;
1449 vcpu->arch.rmode.tr.base = var->base;
1450 vcpu->arch.rmode.tr.limit = var->limit;
1451 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001452 return;
1453 }
1454 vmcs_writel(sf->base, var->base);
1455 vmcs_write32(sf->limit, var->limit);
1456 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001457 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001458 /*
1459 * Hack real-mode segments into vm86 compatibility.
1460 */
1461 if (var->base == 0xffff0000 && var->selector == 0xf000)
1462 vmcs_writel(sf->base, 0xf0000);
1463 ar = 0xf3;
1464 } else
1465 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466 vmcs_write32(sf->ar_bytes, ar);
1467}
1468
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1470{
1471 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1472
1473 *db = (ar >> 14) & 1;
1474 *l = (ar >> 13) & 1;
1475}
1476
1477static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1478{
1479 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1480 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1481}
1482
1483static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1484{
1485 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1486 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1487}
1488
1489static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1490{
1491 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1492 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1493}
1494
1495static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1496{
1497 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1498 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1499}
1500
Mike Dayd77c26f2007-10-08 09:02:08 -04001501static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001504 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001505 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001506 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507
Izik Eidus195aefd2007-10-01 22:14:18 +02001508 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1509 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001510 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001511 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1512 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1513 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001514 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001515 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1516 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001517 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001518 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1519 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001520 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001521 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001522 r = kvm_write_guest_page(kvm, fn, &data,
1523 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1524 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001525 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001526 goto out;
1527
1528 ret = 1;
1529out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001530 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531}
1532
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533static void seg_setup(int seg)
1534{
1535 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1536
1537 vmcs_write16(sf->selector, 0);
1538 vmcs_writel(sf->base, 0);
1539 vmcs_write32(sf->limit, 0xffff);
1540 vmcs_write32(sf->ar_bytes, 0x93);
1541}
1542
Sheng Yangf78e0e22007-10-29 09:40:42 +08001543static int alloc_apic_access_page(struct kvm *kvm)
1544{
1545 struct kvm_userspace_memory_region kvm_userspace_mem;
1546 int r = 0;
1547
Izik Eidus72dc67a2008-02-10 18:04:15 +02001548 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001549 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001550 goto out;
1551 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
1552 kvm_userspace_mem.flags = 0;
1553 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
1554 kvm_userspace_mem.memory_size = PAGE_SIZE;
1555 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1556 if (r)
1557 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001558
1559 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001560 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001561 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001562out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001563 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001564 return r;
1565}
1566
Sheng Yang2384d2b2008-01-17 15:14:33 +08001567static void allocate_vpid(struct vcpu_vmx *vmx)
1568{
1569 int vpid;
1570
1571 vmx->vpid = 0;
1572 if (!enable_vpid || !cpu_has_vmx_vpid())
1573 return;
1574 spin_lock(&vmx_vpid_lock);
1575 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
1576 if (vpid < VMX_NR_VPIDS) {
1577 vmx->vpid = vpid;
1578 __set_bit(vpid, vmx_vpid_bitmap);
1579 }
1580 spin_unlock(&vmx_vpid_lock);
1581}
1582
Sheng Yang25c5f222008-03-28 13:18:56 +08001583void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
1584{
1585 void *va;
1586
1587 if (!cpu_has_vmx_msr_bitmap())
1588 return;
1589
1590 /*
1591 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1592 * have the write-low and read-high bitmap offsets the wrong way round.
1593 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1594 */
1595 va = kmap(msr_bitmap);
1596 if (msr <= 0x1fff) {
1597 __clear_bit(msr, va + 0x000); /* read-low */
1598 __clear_bit(msr, va + 0x800); /* write-low */
1599 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1600 msr &= 0x1fff;
1601 __clear_bit(msr, va + 0x400); /* read-high */
1602 __clear_bit(msr, va + 0xc00); /* write-high */
1603 }
1604 kunmap(msr_bitmap);
1605}
1606
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607/*
1608 * Sets up the vmcs for emulated real mode.
1609 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001610static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611{
1612 u32 host_sysenter_cs;
1613 u32 junk;
1614 unsigned long a;
1615 struct descriptor_table dt;
1616 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001617 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001618 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619
Avi Kivity6aa8b732006-12-10 02:21:36 -08001620 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001621 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1622 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001623
Sheng Yang25c5f222008-03-28 13:18:56 +08001624 if (cpu_has_vmx_msr_bitmap())
1625 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
1626
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1628
Avi Kivity6aa8b732006-12-10 02:21:36 -08001629 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001630 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1631 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001632
1633 exec_control = vmcs_config.cpu_based_exec_ctrl;
1634 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1635 exec_control &= ~CPU_BASED_TPR_SHADOW;
1636#ifdef CONFIG_X86_64
1637 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1638 CPU_BASED_CR8_LOAD_EXITING;
1639#endif
1640 }
1641 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642
Sheng Yang83ff3b92007-11-21 14:33:25 +08001643 if (cpu_has_secondary_exec_ctrls()) {
1644 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
1645 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1646 exec_control &=
1647 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001648 if (vmx->vpid == 0)
1649 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang83ff3b92007-11-21 14:33:25 +08001650 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
1651 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08001652
Avi Kivityc7addb92007-09-16 18:58:32 +02001653 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1654 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1656
1657 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1658 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1659 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1660
1661 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1662 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1663 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1664 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1665 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1666 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001667#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 rdmsrl(MSR_FS_BASE, a);
1669 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1670 rdmsrl(MSR_GS_BASE, a);
1671 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1672#else
1673 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1674 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1675#endif
1676
1677 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1678
1679 get_idt(&dt);
1680 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1681
Mike Dayd77c26f2007-10-08 09:02:08 -04001682 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001683 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001684 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1685 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1686 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687
1688 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1689 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1690 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1691 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1692 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1693 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1694
Avi Kivity6aa8b732006-12-10 02:21:36 -08001695 for (i = 0; i < NR_VMX_MSR; ++i) {
1696 u32 index = vmx_msr_index[i];
1697 u32 data_low, data_high;
1698 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001699 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700
1701 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1702 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001703 if (wrmsr_safe(index, data_low, data_high) < 0)
1704 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001705 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001706 vmx->host_msrs[j].index = index;
1707 vmx->host_msrs[j].reserved = 0;
1708 vmx->host_msrs[j].data = data;
1709 vmx->guest_msrs[j] = vmx->host_msrs[j];
1710 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001713 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714
1715 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001716 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1717
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001718 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
1719 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1720
Sheng Yangf78e0e22007-10-29 09:40:42 +08001721
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001722 return 0;
1723}
1724
1725static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1726{
1727 struct vcpu_vmx *vmx = to_vmx(vcpu);
1728 u64 msr;
1729 int ret;
1730
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001731 down_read(&vcpu->kvm->slots_lock);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001732 if (!init_rmode_tss(vmx->vcpu.kvm)) {
1733 ret = -ENOMEM;
1734 goto out;
1735 }
1736
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001737 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001738
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001739 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001740 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001741 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
1742 if (vmx->vcpu.vcpu_id == 0)
1743 msr |= MSR_IA32_APICBASE_BSP;
1744 kvm_set_apic_base(&vmx->vcpu, msr);
1745
1746 fx_init(&vmx->vcpu);
1747
1748 /*
1749 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1750 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1751 */
1752 if (vmx->vcpu.vcpu_id == 0) {
1753 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1754 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1755 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001756 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
1757 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001758 }
1759 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1760 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1761
1762 seg_setup(VCPU_SREG_DS);
1763 seg_setup(VCPU_SREG_ES);
1764 seg_setup(VCPU_SREG_FS);
1765 seg_setup(VCPU_SREG_GS);
1766 seg_setup(VCPU_SREG_SS);
1767
1768 vmcs_write16(GUEST_TR_SELECTOR, 0);
1769 vmcs_writel(GUEST_TR_BASE, 0);
1770 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1771 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1772
1773 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1774 vmcs_writel(GUEST_LDTR_BASE, 0);
1775 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1776 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1777
1778 vmcs_write32(GUEST_SYSENTER_CS, 0);
1779 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1780 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1781
1782 vmcs_writel(GUEST_RFLAGS, 0x02);
1783 if (vmx->vcpu.vcpu_id == 0)
1784 vmcs_writel(GUEST_RIP, 0xfff0);
1785 else
1786 vmcs_writel(GUEST_RIP, 0);
1787 vmcs_writel(GUEST_RSP, 0);
1788
1789 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
1790 vmcs_writel(GUEST_DR7, 0x400);
1791
1792 vmcs_writel(GUEST_GDTR_BASE, 0);
1793 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1794
1795 vmcs_writel(GUEST_IDTR_BASE, 0);
1796 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1797
1798 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1799 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1800 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1801
1802 guest_write_tsc(0);
1803
1804 /* Special registers */
1805 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1806
1807 setup_msrs(vmx);
1808
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1810
Sheng Yangf78e0e22007-10-29 09:40:42 +08001811 if (cpu_has_vmx_tpr_shadow()) {
1812 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
1813 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
1814 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001815 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08001816 vmcs_write32(TPR_THRESHOLD, 0);
1817 }
1818
1819 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1820 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001821 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001822
Sheng Yang2384d2b2008-01-17 15:14:33 +08001823 if (vmx->vpid != 0)
1824 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
1825
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001826 vmx->vcpu.arch.cr0 = 0x60000010;
1827 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001828 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001829 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001830 vmx_fpu_activate(&vmx->vcpu);
1831 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832
Sheng Yang2384d2b2008-01-17 15:14:33 +08001833 vpid_sync_vcpu_all(vmx);
1834
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001835 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001836
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001838 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839 return ret;
1840}
1841
Eddie Dong85f455f2007-07-06 12:20:49 +03001842static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1843{
Avi Kivity9c8cba32007-11-22 11:42:59 +02001844 struct vcpu_vmx *vmx = to_vmx(vcpu);
1845
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001846 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02001847 vmx->rmode.irq.pending = true;
1848 vmx->rmode.irq.vector = irq;
1849 vmx->rmode.irq.rip = vmcs_readl(GUEST_RIP);
Avi Kivity9c5623e2007-11-08 18:19:20 +02001850 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1851 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
1852 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Avi Kivity9c8cba32007-11-22 11:42:59 +02001853 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03001854 return;
1855 }
1856 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1857 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1858}
1859
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1861{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001862 int word_index = __ffs(vcpu->arch.irq_summary);
1863 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 int irq = word_index * BITS_PER_LONG + bit_index;
1865
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001866 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
1867 if (!vcpu->arch.irq_pending[word_index])
1868 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001869 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870}
1871
Dor Laorc1150d82007-01-05 16:36:24 -08001872
1873static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1874 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875{
Dor Laorc1150d82007-01-05 16:36:24 -08001876 u32 cpu_based_vm_exec_control;
1877
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001878 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001879 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1880 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1881
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001882 if (vcpu->arch.interrupt_window_open &&
1883 vcpu->arch.irq_summary &&
Dor Laorc1150d82007-01-05 16:36:24 -08001884 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001886 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887 */
1888 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001889
1890 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001891 if (!vcpu->arch.interrupt_window_open &&
1892 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 /*
1894 * Interrupts blocked. Wait for unblock.
1895 */
Dor Laorc1150d82007-01-05 16:36:24 -08001896 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1897 else
1898 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1899 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900}
1901
Izik Eiduscbc94022007-10-25 00:29:55 +02001902static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
1903{
1904 int ret;
1905 struct kvm_userspace_memory_region tss_mem = {
1906 .slot = 8,
1907 .guest_phys_addr = addr,
1908 .memory_size = PAGE_SIZE * 3,
1909 .flags = 0,
1910 };
1911
1912 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
1913 if (ret)
1914 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001915 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02001916 return 0;
1917}
1918
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1920{
1921 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1922
1923 set_debugreg(dbg->bp[0], 0);
1924 set_debugreg(dbg->bp[1], 1);
1925 set_debugreg(dbg->bp[2], 2);
1926 set_debugreg(dbg->bp[3], 3);
1927
1928 if (dbg->singlestep) {
1929 unsigned long flags;
1930
1931 flags = vmcs_readl(GUEST_RFLAGS);
1932 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1933 vmcs_writel(GUEST_RFLAGS, flags);
1934 }
1935}
1936
1937static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1938 int vec, u32 err_code)
1939{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001940 if (!vcpu->arch.rmode.active)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941 return 0;
1942
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001943 /*
1944 * Instruction with address size override prefix opcode 0x67
1945 * Cause the #SS fault with 0 error code in VM86 mode.
1946 */
1947 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02001948 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001949 return 1;
1950 return 0;
1951}
1952
1953static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
1954{
Avi Kivity1155f762007-11-22 11:30:47 +02001955 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 u32 intr_info, error_code;
1957 unsigned long cr2, rip;
1958 u32 vect_info;
1959 enum emulation_result er;
1960
Avi Kivity1155f762007-11-22 11:30:47 +02001961 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
1963
1964 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04001965 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001967 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001968
Eddie Dong85f455f2007-07-06 12:20:49 +03001969 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001971 set_bit(irq, vcpu->arch.irq_pending);
1972 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001973 }
1974
Avi Kivity1b6269d2007-10-09 12:12:19 +02001975 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
1976 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001977
1978 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001979 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03001980 return 1;
1981 }
1982
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001983 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08001984 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001985 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001986 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001987 return 1;
1988 }
1989
Avi Kivity6aa8b732006-12-10 02:21:36 -08001990 error_code = 0;
1991 rip = vmcs_readl(GUEST_RIP);
Ryan Harper2e113842008-02-11 10:26:38 -06001992 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001993 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
1994 if (is_page_fault(intr_info)) {
1995 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity30677142007-10-28 18:48:59 +02001996 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997 }
1998
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001999 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002000 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002001 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002002 if (vcpu->arch.halt_request) {
2003 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002004 return kvm_emulate_halt(vcpu);
2005 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002006 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002007 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008
Mike Dayd77c26f2007-10-08 09:02:08 -04002009 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2010 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2012 return 0;
2013 }
2014 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2015 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2016 kvm_run->ex.error_code = error_code;
2017 return 0;
2018}
2019
2020static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2021 struct kvm_run *kvm_run)
2022{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002023 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002024 return 1;
2025}
2026
Avi Kivity988ad742007-02-12 00:54:36 -08002027static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2028{
2029 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2030 return 0;
2031}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002032
Avi Kivity6aa8b732006-12-10 02:21:36 -08002033static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2034{
He, Qingbfdaab02007-09-12 14:18:28 +08002035 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002036 int size, down, in, string, rep;
2037 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002038
Avi Kivity1165f5f2007-04-19 17:27:43 +03002039 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002040 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002041 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002042
2043 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002044 if (emulate_instruction(vcpu,
2045 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002046 return 0;
2047 return 1;
2048 }
2049
2050 size = (exit_qualification & 7) + 1;
2051 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002052 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002053 rep = (exit_qualification & 32) != 0;
2054 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002055
Laurent Vivier3090dd72007-08-05 10:43:32 +03002056 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057}
2058
Ingo Molnar102d8322007-02-19 14:37:47 +02002059static void
2060vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2061{
2062 /*
2063 * Patch in the VMCALL instruction:
2064 */
2065 hypercall[0] = 0x0f;
2066 hypercall[1] = 0x01;
2067 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002068}
2069
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2071{
He, Qingbfdaab02007-09-12 14:18:28 +08002072 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073 int cr;
2074 int reg;
2075
He, Qingbfdaab02007-09-12 14:18:28 +08002076 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077 cr = exit_qualification & 15;
2078 reg = (exit_qualification >> 8) & 15;
2079 switch ((exit_qualification >> 4) & 3) {
2080 case 0: /* mov to cr */
2081 switch (cr) {
2082 case 0:
2083 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002084 kvm_set_cr0(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 skip_emulated_instruction(vcpu);
2086 return 1;
2087 case 3:
2088 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002089 kvm_set_cr3(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 skip_emulated_instruction(vcpu);
2091 return 1;
2092 case 4:
2093 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002094 kvm_set_cr4(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095 skip_emulated_instruction(vcpu);
2096 return 1;
2097 case 8:
2098 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002099 kvm_set_cr8(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002100 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002101 if (irqchip_in_kernel(vcpu->kvm))
2102 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002103 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2104 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105 };
2106 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002107 case 2: /* clts */
2108 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002109 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 vcpu->arch.cr0 &= ~X86_CR0_TS;
2111 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002112 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002113 skip_emulated_instruction(vcpu);
2114 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115 case 1: /*mov from cr*/
2116 switch (cr) {
2117 case 3:
2118 vcpu_load_rsp_rip(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002119 vcpu->arch.regs[reg] = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120 vcpu_put_rsp_rip(vcpu);
2121 skip_emulated_instruction(vcpu);
2122 return 1;
2123 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002124 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002125 vcpu->arch.regs[reg] = kvm_get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126 vcpu_put_rsp_rip(vcpu);
2127 skip_emulated_instruction(vcpu);
2128 return 1;
2129 }
2130 break;
2131 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002132 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133
2134 skip_emulated_instruction(vcpu);
2135 return 1;
2136 default:
2137 break;
2138 }
2139 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002140 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 (int)(exit_qualification >> 4) & 3, cr);
2142 return 0;
2143}
2144
2145static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2146{
He, Qingbfdaab02007-09-12 14:18:28 +08002147 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148 unsigned long val;
2149 int dr, reg;
2150
2151 /*
2152 * FIXME: this code assumes the host is debugging the guest.
2153 * need to deal with guest debugging itself too.
2154 */
He, Qingbfdaab02007-09-12 14:18:28 +08002155 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156 dr = exit_qualification & 7;
2157 reg = (exit_qualification >> 8) & 15;
2158 vcpu_load_rsp_rip(vcpu);
2159 if (exit_qualification & 16) {
2160 /* mov from dr */
2161 switch (dr) {
2162 case 6:
2163 val = 0xffff0ff0;
2164 break;
2165 case 7:
2166 val = 0x400;
2167 break;
2168 default:
2169 val = 0;
2170 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002171 vcpu->arch.regs[reg] = val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172 } else {
2173 /* mov to dr */
2174 }
2175 vcpu_put_rsp_rip(vcpu);
2176 skip_emulated_instruction(vcpu);
2177 return 1;
2178}
2179
2180static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2181{
Avi Kivity06465c52007-02-28 20:46:53 +02002182 kvm_emulate_cpuid(vcpu);
2183 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002184}
2185
2186static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2187{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002188 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 u64 data;
2190
2191 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002192 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002193 return 1;
2194 }
2195
2196 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002197 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2198 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199 skip_emulated_instruction(vcpu);
2200 return 1;
2201}
2202
2203static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2204{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002205 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2206 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2207 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208
2209 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002210 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002211 return 1;
2212 }
2213
2214 skip_emulated_instruction(vcpu);
2215 return 1;
2216}
2217
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002218static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2219 struct kvm_run *kvm_run)
2220{
2221 return 1;
2222}
2223
Avi Kivity6aa8b732006-12-10 02:21:36 -08002224static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2225 struct kvm_run *kvm_run)
2226{
Eddie Dong85f455f2007-07-06 12:20:49 +03002227 u32 cpu_based_vm_exec_control;
2228
2229 /* clear pending irq */
2230 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2231 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2232 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Dor Laorc1150d82007-01-05 16:36:24 -08002233 /*
2234 * If the user space waits to inject interrupts, exit as soon as
2235 * possible
2236 */
2237 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002238 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002239 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002240 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002241 return 0;
2242 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243 return 1;
2244}
2245
2246static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2247{
2248 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002249 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002250}
2251
Ingo Molnarc21415e2007-02-19 14:37:47 +02002252static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2253{
Dor Laor510043d2007-02-19 18:25:43 +02002254 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002255 kvm_emulate_hypercall(vcpu);
2256 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002257}
2258
Eddie Donge5edaa02007-11-11 12:28:35 +02002259static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2260{
2261 skip_emulated_instruction(vcpu);
2262 /* TODO: Add support for VT-d/pass-through device */
2263 return 1;
2264}
2265
Sheng Yangf78e0e22007-10-29 09:40:42 +08002266static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2267{
2268 u64 exit_qualification;
2269 enum emulation_result er;
2270 unsigned long offset;
2271
2272 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2273 offset = exit_qualification & 0xffful;
2274
2275 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2276
2277 if (er != EMULATE_DONE) {
2278 printk(KERN_ERR
2279 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2280 offset);
2281 return -ENOTSUPP;
2282 }
2283 return 1;
2284}
2285
Izik Eidus37817f22008-03-24 23:14:53 +02002286static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2287{
2288 unsigned long exit_qualification;
2289 u16 tss_selector;
2290 int reason;
2291
2292 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2293
2294 reason = (u32)exit_qualification >> 30;
2295 tss_selector = exit_qualification;
2296
2297 return kvm_task_switch(vcpu, tss_selector, reason);
2298}
2299
Avi Kivity6aa8b732006-12-10 02:21:36 -08002300/*
2301 * The exit handlers return 1 if the exit was handled fully and guest execution
2302 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2303 * to be done to userspace and return 0.
2304 */
2305static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2306 struct kvm_run *kvm_run) = {
2307 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2308 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002309 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002311 [EXIT_REASON_CR_ACCESS] = handle_cr,
2312 [EXIT_REASON_DR_ACCESS] = handle_dr,
2313 [EXIT_REASON_CPUID] = handle_cpuid,
2314 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2315 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2316 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2317 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002318 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002319 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2320 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002321 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002322 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323};
2324
2325static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002326 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327
2328/*
2329 * The guest has exited. See if we can fix it or if we need userspace
2330 * assistance.
2331 */
2332static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2333{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002334 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002335 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002336 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002337
2338 if (unlikely(vmx->fail)) {
2339 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2340 kvm_run->fail_entry.hardware_entry_failure_reason
2341 = vmcs_read32(VM_INSTRUCTION_ERROR);
2342 return 0;
2343 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344
Mike Dayd77c26f2007-10-08 09:02:08 -04002345 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
2346 exit_reason != EXIT_REASON_EXCEPTION_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002348 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 if (exit_reason < kvm_vmx_max_exit_handlers
2350 && kvm_vmx_exit_handlers[exit_reason])
2351 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2352 else {
2353 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2354 kvm_run->hw.hardware_exit_reason = exit_reason;
2355 }
2356 return 0;
2357}
2358
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002359static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2360{
2361 int max_irr, tpr;
2362
2363 if (!vm_need_tpr_shadow(vcpu->kvm))
2364 return;
2365
2366 if (!kvm_lapic_enabled(vcpu) ||
2367 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2368 vmcs_write32(TPR_THRESHOLD, 0);
2369 return;
2370 }
2371
2372 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2373 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2374}
2375
Eddie Dong85f455f2007-07-06 12:20:49 +03002376static void enable_irq_window(struct kvm_vcpu *vcpu)
2377{
2378 u32 cpu_based_vm_exec_control;
2379
2380 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2381 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2382 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2383}
2384
2385static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2386{
Avi Kivity1155f762007-11-22 11:30:47 +02002387 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002388 u32 idtv_info_field, intr_info_field;
2389 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002390 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002391
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002392 update_tpr_threshold(vcpu);
2393
Eddie Dong85f455f2007-07-06 12:20:49 +03002394 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2395 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Avi Kivity1155f762007-11-22 11:30:47 +02002396 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong85f455f2007-07-06 12:20:49 +03002397 if (intr_info_field & INTR_INFO_VALID_MASK) {
2398 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2399 /* TODO: fault when IDT_Vectoring */
Ryan Harper9584bf22007-12-13 10:21:10 -06002400 if (printk_ratelimit())
2401 printk(KERN_ERR "Fault when IDT_Vectoring\n");
Eddie Dong85f455f2007-07-06 12:20:49 +03002402 }
2403 if (has_ext_irq)
2404 enable_irq_window(vcpu);
2405 return;
2406 }
2407 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002408 if ((idtv_info_field & VECTORING_INFO_TYPE_MASK)
2409 == INTR_TYPE_EXT_INTR
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002410 && vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002411 u8 vect = idtv_info_field & VECTORING_INFO_VECTOR_MASK;
2412
2413 vmx_inject_irq(vcpu, vect);
2414 if (unlikely(has_ext_irq))
2415 enable_irq_window(vcpu);
2416 return;
2417 }
2418
Eddie Dong85f455f2007-07-06 12:20:49 +03002419 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2420 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2421 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2422
Ryan Harper2e113842008-02-11 10:26:38 -06002423 if (unlikely(idtv_info_field & INTR_INFO_DELIVER_CODE_MASK))
Eddie Dong85f455f2007-07-06 12:20:49 +03002424 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2425 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2426 if (unlikely(has_ext_irq))
2427 enable_irq_window(vcpu);
2428 return;
2429 }
2430 if (!has_ext_irq)
2431 return;
2432 interrupt_window_open =
2433 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2434 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002435 if (interrupt_window_open) {
2436 vector = kvm_cpu_get_interrupt(vcpu);
2437 vmx_inject_irq(vcpu, vector);
2438 kvm_timer_intr_post(vcpu, vector);
2439 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002440 enable_irq_window(vcpu);
2441}
2442
Avi Kivity9c8cba32007-11-22 11:42:59 +02002443/*
2444 * Failure to inject an interrupt should give us the information
2445 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2446 * when fetching the interrupt redirection bitmap in the real-mode
2447 * tss, this doesn't happen. So we do it ourselves.
2448 */
2449static void fixup_rmode_irq(struct vcpu_vmx *vmx)
2450{
2451 vmx->rmode.irq.pending = 0;
2452 if (vmcs_readl(GUEST_RIP) + 1 != vmx->rmode.irq.rip)
2453 return;
2454 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip);
2455 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
2456 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
2457 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
2458 return;
2459 }
2460 vmx->idt_vectoring_info =
2461 VECTORING_INFO_VALID_MASK
2462 | INTR_TYPE_EXT_INTR
2463 | vmx->rmode.irq.vector;
2464}
2465
Avi Kivity04d2cc72007-09-10 18:10:54 +03002466static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002468 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002469 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002470
2471 /*
2472 * Loading guest fpu may have cleared host cr0.ts
2473 */
2474 vmcs_writel(HOST_CR0, read_cr0());
2475
Mike Dayd77c26f2007-10-08 09:02:08 -04002476 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002477 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002478#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02002479 "push %%rdx; push %%rbp;"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 "push %%rcx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002481#else
Laurent Vivierff593e52007-10-25 14:18:55 +02002482 "push %%edx; push %%ebp;"
2483 "push %%ecx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002484#endif
Laurent Vivierc2036302007-10-25 14:18:52 +02002485 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002486 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02002487 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002488 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002489#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002490 "mov %c[cr2](%0), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002491 "mov %%rax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002492 "mov %c[rax](%0), %%rax \n\t"
2493 "mov %c[rbx](%0), %%rbx \n\t"
2494 "mov %c[rdx](%0), %%rdx \n\t"
2495 "mov %c[rsi](%0), %%rsi \n\t"
2496 "mov %c[rdi](%0), %%rdi \n\t"
2497 "mov %c[rbp](%0), %%rbp \n\t"
2498 "mov %c[r8](%0), %%r8 \n\t"
2499 "mov %c[r9](%0), %%r9 \n\t"
2500 "mov %c[r10](%0), %%r10 \n\t"
2501 "mov %c[r11](%0), %%r11 \n\t"
2502 "mov %c[r12](%0), %%r12 \n\t"
2503 "mov %c[r13](%0), %%r13 \n\t"
2504 "mov %c[r14](%0), %%r14 \n\t"
2505 "mov %c[r15](%0), %%r15 \n\t"
2506 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002507#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002508 "mov %c[cr2](%0), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 "mov %%eax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002510 "mov %c[rax](%0), %%eax \n\t"
2511 "mov %c[rbx](%0), %%ebx \n\t"
2512 "mov %c[rdx](%0), %%edx \n\t"
2513 "mov %c[rsi](%0), %%esi \n\t"
2514 "mov %c[rdi](%0), %%edi \n\t"
2515 "mov %c[rbp](%0), %%ebp \n\t"
2516 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517#endif
2518 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002519 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002520 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002521 "jmp .Lkvm_vmx_return \n\t"
2522 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2523 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002525#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002526 "xchg %0, (%%rsp) \n\t"
2527 "mov %%rax, %c[rax](%0) \n\t"
2528 "mov %%rbx, %c[rbx](%0) \n\t"
2529 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
2530 "mov %%rdx, %c[rdx](%0) \n\t"
2531 "mov %%rsi, %c[rsi](%0) \n\t"
2532 "mov %%rdi, %c[rdi](%0) \n\t"
2533 "mov %%rbp, %c[rbp](%0) \n\t"
2534 "mov %%r8, %c[r8](%0) \n\t"
2535 "mov %%r9, %c[r9](%0) \n\t"
2536 "mov %%r10, %c[r10](%0) \n\t"
2537 "mov %%r11, %c[r11](%0) \n\t"
2538 "mov %%r12, %c[r12](%0) \n\t"
2539 "mov %%r13, %c[r13](%0) \n\t"
2540 "mov %%r14, %c[r14](%0) \n\t"
2541 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542 "mov %%cr2, %%rax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002543 "mov %%rax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544
Avi Kivitye08aa782007-11-15 18:06:18 +02002545 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002547 "xchg %0, (%%esp) \n\t"
2548 "mov %%eax, %c[rax](%0) \n\t"
2549 "mov %%ebx, %c[rbx](%0) \n\t"
2550 "pushl (%%esp); popl %c[rcx](%0) \n\t"
2551 "mov %%edx, %c[rdx](%0) \n\t"
2552 "mov %%esi, %c[rsi](%0) \n\t"
2553 "mov %%edi, %c[rdi](%0) \n\t"
2554 "mov %%ebp, %c[rbp](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002555 "mov %%cr2, %%eax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002556 "mov %%eax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002557
Avi Kivitye08aa782007-11-15 18:06:18 +02002558 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559#endif
Avi Kivitye08aa782007-11-15 18:06:18 +02002560 "setbe %c[fail](%0) \n\t"
2561 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
2562 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
2563 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002564 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
2565 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
2566 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
2567 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
2568 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
2569 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
2570 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002571#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002572 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
2573 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
2574 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
2575 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
2576 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
2577 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
2578 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
2579 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002581 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02002582 : "cc", "memory"
2583#ifdef CONFIG_X86_64
2584 , "rbx", "rdi", "rsi"
2585 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Laurent Vivierff593e52007-10-25 14:18:55 +02002586#else
2587 , "ebx", "edi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02002588#endif
2589 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590
Avi Kivity1155f762007-11-22 11:30:47 +02002591 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002592 if (vmx->rmode.irq.pending)
2593 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02002594
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002595 vcpu->arch.interrupt_window_open =
Mike Dayd77c26f2007-10-08 09:02:08 -04002596 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597
Mike Dayd77c26f2007-10-08 09:02:08 -04002598 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002599 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002600
2601 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2602
2603 /* We need to handle NMIs before interrupts are enabled */
2604 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2605 asm("int $2");
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606}
2607
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2609{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002610 struct vcpu_vmx *vmx = to_vmx(vcpu);
2611
2612 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002613 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002614 free_vmcs(vmx->vmcs);
2615 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002616 }
2617}
2618
2619static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2620{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002621 struct vcpu_vmx *vmx = to_vmx(vcpu);
2622
Sheng Yang2384d2b2008-01-17 15:14:33 +08002623 spin_lock(&vmx_vpid_lock);
2624 if (vmx->vpid != 0)
2625 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2626 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002627 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002628 kfree(vmx->host_msrs);
2629 kfree(vmx->guest_msrs);
2630 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002631 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002632}
2633
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002634static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002636 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002637 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002638 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002640 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002641 return ERR_PTR(-ENOMEM);
2642
Sheng Yang2384d2b2008-01-17 15:14:33 +08002643 allocate_vpid(vmx);
2644
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002645 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2646 if (err)
2647 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002648
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002649 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002650 if (!vmx->guest_msrs) {
2651 err = -ENOMEM;
2652 goto uninit_vcpu;
2653 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002654
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002655 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2656 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002657 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002658
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002659 vmx->vmcs = alloc_vmcs();
2660 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002661 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002662
2663 vmcs_clear(vmx->vmcs);
2664
Avi Kivity15ad7142007-07-11 18:17:21 +03002665 cpu = get_cpu();
2666 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002667 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002668 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002669 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002670 if (err)
2671 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02002672 if (vm_need_virtualize_apic_accesses(kvm))
2673 if (alloc_apic_access_page(kvm) != 0)
2674 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002675
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002676 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002677
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002678free_vmcs:
2679 free_vmcs(vmx->vmcs);
2680free_msrs:
2681 kfree(vmx->host_msrs);
2682free_guest_msrs:
2683 kfree(vmx->guest_msrs);
2684uninit_vcpu:
2685 kvm_vcpu_uninit(&vmx->vcpu);
2686free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002687 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002688 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689}
2690
Yang, Sheng002c7f72007-07-31 14:23:01 +03002691static void __init vmx_check_processor_compat(void *rtn)
2692{
2693 struct vmcs_config vmcs_conf;
2694
2695 *(int *)rtn = 0;
2696 if (setup_vmcs_config(&vmcs_conf) < 0)
2697 *(int *)rtn = -EIO;
2698 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2699 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2700 smp_processor_id());
2701 *(int *)rtn = -EIO;
2702 }
2703}
2704
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002705static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002706 .cpu_has_kvm_support = cpu_has_kvm_support,
2707 .disabled_by_bios = vmx_disabled_by_bios,
2708 .hardware_setup = hardware_setup,
2709 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002710 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002711 .hardware_enable = hardware_enable,
2712 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02002713 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002714
2715 .vcpu_create = vmx_create_vcpu,
2716 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002717 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718
Avi Kivity04d2cc72007-09-10 18:10:54 +03002719 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 .vcpu_load = vmx_vcpu_load,
2721 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002722 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723
2724 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002725 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 .get_msr = vmx_get_msr,
2727 .set_msr = vmx_set_msr,
2728 .get_segment_base = vmx_get_segment_base,
2729 .get_segment = vmx_get_segment,
2730 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02002731 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002733 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 .set_cr3 = vmx_set_cr3,
2736 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 .get_idt = vmx_get_idt,
2739 .set_idt = vmx_set_idt,
2740 .get_gdt = vmx_get_gdt,
2741 .set_gdt = vmx_set_gdt,
2742 .cache_regs = vcpu_load_rsp_rip,
2743 .decache_regs = vcpu_put_rsp_rip,
2744 .get_rflags = vmx_get_rflags,
2745 .set_rflags = vmx_set_rflags,
2746
2747 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748
Avi Kivity6aa8b732006-12-10 02:21:36 -08002749 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002750 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002752 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002753 .get_irq = vmx_get_irq,
2754 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02002755 .queue_exception = vmx_queue_exception,
2756 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002757 .inject_pending_irq = vmx_intr_assist,
2758 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02002759
2760 .set_tss_addr = vmx_set_tss_addr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002761};
2762
2763static int __init vmx_init(void)
2764{
Sheng Yang25c5f222008-03-28 13:18:56 +08002765 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03002766 int r;
2767
2768 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2769 if (!vmx_io_bitmap_a)
2770 return -ENOMEM;
2771
2772 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2773 if (!vmx_io_bitmap_b) {
2774 r = -ENOMEM;
2775 goto out;
2776 }
2777
Sheng Yang25c5f222008-03-28 13:18:56 +08002778 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2779 if (!vmx_msr_bitmap) {
2780 r = -ENOMEM;
2781 goto out1;
2782 }
2783
He, Qingfdef3ad2007-04-30 09:45:24 +03002784 /*
2785 * Allow direct access to the PC debug port (it is often used for I/O
2786 * delays, but the vmexits simply slow things down).
2787 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002788 va = kmap(vmx_io_bitmap_a);
2789 memset(va, 0xff, PAGE_SIZE);
2790 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002791 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002792
Sheng Yang25c5f222008-03-28 13:18:56 +08002793 va = kmap(vmx_io_bitmap_b);
2794 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002795 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002796
Sheng Yang25c5f222008-03-28 13:18:56 +08002797 va = kmap(vmx_msr_bitmap);
2798 memset(va, 0xff, PAGE_SIZE);
2799 kunmap(vmx_msr_bitmap);
2800
Sheng Yang2384d2b2008-01-17 15:14:33 +08002801 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
2802
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002803 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002804 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08002805 goto out2;
2806
2807 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
2808 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
2809 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
2810 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
2811 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03002812
Avi Kivityc7addb92007-09-16 18:58:32 +02002813 if (bypass_guest_pf)
2814 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
2815
He, Qingfdef3ad2007-04-30 09:45:24 +03002816 return 0;
2817
Sheng Yang25c5f222008-03-28 13:18:56 +08002818out2:
2819 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03002820out1:
2821 __free_page(vmx_io_bitmap_b);
2822out:
2823 __free_page(vmx_io_bitmap_a);
2824 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825}
2826
2827static void __exit vmx_exit(void)
2828{
Sheng Yang25c5f222008-03-28 13:18:56 +08002829 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03002830 __free_page(vmx_io_bitmap_b);
2831 __free_page(vmx_io_bitmap_a);
2832
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002833 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002834}
2835
2836module_init(vmx_init)
2837module_exit(vmx_exit)