blob: 98e4f2b036de3db5f901fc868d408a1b9114e3e0 [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
Sheng Yangd56f5462008-04-25 10:13:16 +080045static int enable_ept;
46module_param(enable_ept, bool, 0);
47
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040048struct vmcs {
49 u32 revision_id;
50 u32 abort;
51 char data[0];
52};
53
54struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100055 struct kvm_vcpu vcpu;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040056 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030057 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020058 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040059 struct kvm_msr_entry *guest_msrs;
60 struct kvm_msr_entry *host_msrs;
61 int nmsrs;
62 int save_nmsrs;
63 int msr_offset_efer;
64#ifdef CONFIG_X86_64
65 int msr_offset_kernel_gs_base;
66#endif
67 struct vmcs *vmcs;
68 struct {
69 int loaded;
70 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020071 int gs_ldt_reload_needed;
72 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030073 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040074 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020075 struct {
76 struct {
77 bool pending;
78 u8 vector;
79 unsigned rip;
80 } irq;
81 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +080082 int vpid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040083};
84
85static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
86{
Rusty Russellfb3f0f52007-07-27 17:16:56 +100087 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040088}
89
Avi Kivity75880a02007-06-20 11:20:04 +030090static int init_rmode_tss(struct kvm *kvm);
91
Avi Kivity6aa8b732006-12-10 02:21:36 -080092static DEFINE_PER_CPU(struct vmcs *, vmxarea);
93static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
94
He, Qingfdef3ad2007-04-30 09:45:24 +030095static struct page *vmx_io_bitmap_a;
96static struct page *vmx_io_bitmap_b;
Sheng Yang25c5f222008-03-28 13:18:56 +080097static struct page *vmx_msr_bitmap;
He, Qingfdef3ad2007-04-30 09:45:24 +030098
Sheng Yang2384d2b2008-01-17 15:14:33 +080099static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
100static DEFINE_SPINLOCK(vmx_vpid_lock);
101
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300102static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800103 int size;
104 int order;
105 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300106 u32 pin_based_exec_ctrl;
107 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800108 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300109 u32 vmexit_ctrl;
110 u32 vmentry_ctrl;
111} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800112
Sheng Yangd56f5462008-04-25 10:13:16 +0800113struct vmx_capability {
114 u32 ept;
115 u32 vpid;
116} vmx_capability;
117
Avi Kivity6aa8b732006-12-10 02:21:36 -0800118#define VMX_SEGMENT_FIELD(seg) \
119 [VCPU_SREG_##seg] = { \
120 .selector = GUEST_##seg##_SELECTOR, \
121 .base = GUEST_##seg##_BASE, \
122 .limit = GUEST_##seg##_LIMIT, \
123 .ar_bytes = GUEST_##seg##_AR_BYTES, \
124 }
125
126static struct kvm_vmx_segment_field {
127 unsigned selector;
128 unsigned base;
129 unsigned limit;
130 unsigned ar_bytes;
131} kvm_vmx_segment_fields[] = {
132 VMX_SEGMENT_FIELD(CS),
133 VMX_SEGMENT_FIELD(DS),
134 VMX_SEGMENT_FIELD(ES),
135 VMX_SEGMENT_FIELD(FS),
136 VMX_SEGMENT_FIELD(GS),
137 VMX_SEGMENT_FIELD(SS),
138 VMX_SEGMENT_FIELD(TR),
139 VMX_SEGMENT_FIELD(LDTR),
140};
141
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300142/*
143 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
144 * away by decrementing the array size.
145 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800146static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800147#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
149#endif
150 MSR_EFER, MSR_K6_STAR,
151};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200152#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800153
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400154static void load_msrs(struct kvm_msr_entry *e, int n)
155{
156 int i;
157
158 for (i = 0; i < n; ++i)
159 wrmsrl(e[i].index, e[i].data);
160}
161
162static void save_msrs(struct kvm_msr_entry *e, int n)
163{
164 int i;
165
166 for (i = 0; i < n; ++i)
167 rdmsrl(e[i].index, e[i].data);
168}
169
Avi Kivity6aa8b732006-12-10 02:21:36 -0800170static inline int is_page_fault(u32 intr_info)
171{
172 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
173 INTR_INFO_VALID_MASK)) ==
174 (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
175}
176
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300177static inline int is_no_device(u32 intr_info)
178{
179 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
180 INTR_INFO_VALID_MASK)) ==
181 (INTR_TYPE_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
182}
183
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500184static inline int is_invalid_opcode(u32 intr_info)
185{
186 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
187 INTR_INFO_VALID_MASK)) ==
188 (INTR_TYPE_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
189}
190
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191static inline int is_external_interrupt(u32 intr_info)
192{
193 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
194 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
195}
196
Sheng Yang25c5f222008-03-28 13:18:56 +0800197static inline int cpu_has_vmx_msr_bitmap(void)
198{
199 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
200}
201
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800202static inline int cpu_has_vmx_tpr_shadow(void)
203{
204 return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
205}
206
207static inline int vm_need_tpr_shadow(struct kvm *kvm)
208{
209 return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
210}
211
Sheng Yangf78e0e22007-10-29 09:40:42 +0800212static inline int cpu_has_secondary_exec_ctrls(void)
213{
214 return (vmcs_config.cpu_based_exec_ctrl &
215 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
216}
217
Avi Kivity774ead32007-12-26 13:57:04 +0200218static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800219{
Avi Kivity4c9fc8e2008-03-24 18:15:14 +0200220 return flexpriority_enabled
221 && (vmcs_config.cpu_based_2nd_exec_ctrl &
222 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800223}
224
Sheng Yangd56f5462008-04-25 10:13:16 +0800225static inline int cpu_has_vmx_invept_individual_addr(void)
226{
227 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT));
228}
229
230static inline int cpu_has_vmx_invept_context(void)
231{
232 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
233}
234
235static inline int cpu_has_vmx_invept_global(void)
236{
237 return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
238}
239
240static inline int cpu_has_vmx_ept(void)
241{
242 return (vmcs_config.cpu_based_2nd_exec_ctrl &
243 SECONDARY_EXEC_ENABLE_EPT);
244}
245
246static inline int vm_need_ept(void)
247{
248 return (cpu_has_vmx_ept() && enable_ept);
249}
250
Sheng Yangf78e0e22007-10-29 09:40:42 +0800251static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
252{
253 return ((cpu_has_vmx_virtualize_apic_accesses()) &&
254 (irqchip_in_kernel(kvm)));
255}
256
Sheng Yang2384d2b2008-01-17 15:14:33 +0800257static inline int cpu_has_vmx_vpid(void)
258{
259 return (vmcs_config.cpu_based_2nd_exec_ctrl &
260 SECONDARY_EXEC_ENABLE_VPID);
261}
262
Rusty Russell8b9cf982007-07-30 16:31:43 +1000263static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800264{
265 int i;
266
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400267 for (i = 0; i < vmx->nmsrs; ++i)
268 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300269 return i;
270 return -1;
271}
272
Sheng Yang2384d2b2008-01-17 15:14:33 +0800273static inline void __invvpid(int ext, u16 vpid, gva_t gva)
274{
275 struct {
276 u64 vpid : 16;
277 u64 rsvd : 48;
278 u64 gva;
279 } operand = { vpid, 0, gva };
280
281 asm volatile (ASM_VMX_INVVPID
282 /* CF==1 or ZF==1 --> rc = -1 */
283 "; ja 1f ; ud2 ; 1:"
284 : : "a"(&operand), "c"(ext) : "cc", "memory");
285}
286
Rusty Russell8b9cf982007-07-30 16:31:43 +1000287static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300288{
289 int i;
290
Rusty Russell8b9cf982007-07-30 16:31:43 +1000291 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300292 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400293 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000294 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800295}
296
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297static void vmcs_clear(struct vmcs *vmcs)
298{
299 u64 phys_addr = __pa(vmcs);
300 u8 error;
301
302 asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0"
303 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
304 : "cc", "memory");
305 if (error)
306 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
307 vmcs, phys_addr);
308}
309
310static void __vcpu_clear(void *arg)
311{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000312 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800313 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800314
Rusty Russell8b9cf982007-07-30 16:31:43 +1000315 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400316 vmcs_clear(vmx->vmcs);
317 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800318 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800319 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800320}
321
Rusty Russell8b9cf982007-07-30 16:31:43 +1000322static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800323{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200324 if (vmx->vcpu.cpu == -1)
325 return;
Avi Kivityf566e092007-09-30 11:02:53 +0200326 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 0, 1);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000327 vmx->launched = 0;
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800328}
329
Sheng Yang2384d2b2008-01-17 15:14:33 +0800330static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
331{
332 if (vmx->vpid == 0)
333 return;
334
335 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
336}
337
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338static unsigned long vmcs_readl(unsigned long field)
339{
340 unsigned long value;
341
342 asm volatile (ASM_VMX_VMREAD_RDX_RAX
343 : "=a"(value) : "d"(field) : "cc");
344 return value;
345}
346
347static u16 vmcs_read16(unsigned long field)
348{
349 return vmcs_readl(field);
350}
351
352static u32 vmcs_read32(unsigned long field)
353{
354 return vmcs_readl(field);
355}
356
357static u64 vmcs_read64(unsigned long field)
358{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800359#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800360 return vmcs_readl(field);
361#else
362 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
363#endif
364}
365
Avi Kivitye52de1b2007-01-05 16:36:56 -0800366static noinline void vmwrite_error(unsigned long field, unsigned long value)
367{
368 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
369 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
370 dump_stack();
371}
372
Avi Kivity6aa8b732006-12-10 02:21:36 -0800373static void vmcs_writel(unsigned long field, unsigned long value)
374{
375 u8 error;
376
377 asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400378 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800379 if (unlikely(error))
380 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800381}
382
383static void vmcs_write16(unsigned long field, u16 value)
384{
385 vmcs_writel(field, value);
386}
387
388static void vmcs_write32(unsigned long field, u32 value)
389{
390 vmcs_writel(field, value);
391}
392
393static void vmcs_write64(unsigned long field, u64 value)
394{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800395#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800396 vmcs_writel(field, value);
397#else
398 vmcs_writel(field, value);
399 asm volatile ("");
400 vmcs_writel(field+1, value >> 32);
401#endif
402}
403
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300404static void vmcs_clear_bits(unsigned long field, u32 mask)
405{
406 vmcs_writel(field, vmcs_readl(field) & ~mask);
407}
408
409static void vmcs_set_bits(unsigned long field, u32 mask)
410{
411 vmcs_writel(field, vmcs_readl(field) | mask);
412}
413
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300414static void update_exception_bitmap(struct kvm_vcpu *vcpu)
415{
416 u32 eb;
417
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500418 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300419 if (!vcpu->fpu_active)
420 eb |= 1u << NM_VECTOR;
421 if (vcpu->guest_debug.enabled)
422 eb |= 1u << 1;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800423 if (vcpu->arch.rmode.active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300424 eb = ~0;
425 vmcs_write32(EXCEPTION_BITMAP, eb);
426}
427
Avi Kivity33ed6322007-05-02 16:54:03 +0300428static void reload_tss(void)
429{
Avi Kivity33ed6322007-05-02 16:54:03 +0300430 /*
431 * VT restores TR but not its size. Useless.
432 */
433 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200434 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300435
436 get_gdt(&gdt);
437 descs = (void *)gdt.base;
438 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
439 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300440}
441
Rusty Russell8b9cf982007-07-30 16:31:43 +1000442static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300443{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400444 int efer_offset = vmx->msr_offset_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300445 u64 host_efer = vmx->host_msrs[efer_offset].data;
446 u64 guest_efer = vmx->guest_msrs[efer_offset].data;
447 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300448
Avi Kivity51c6cf62007-08-29 03:48:05 +0300449 if (efer_offset < 0)
450 return;
451 /*
452 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
453 * outside long mode
454 */
455 ignore_bits = EFER_NX | EFER_SCE;
456#ifdef CONFIG_X86_64
457 ignore_bits |= EFER_LMA | EFER_LME;
458 /* SCE is meaningful only in long mode on Intel */
459 if (guest_efer & EFER_LMA)
460 ignore_bits &= ~(u64)EFER_SCE;
461#endif
462 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
463 return;
464
465 vmx->host_state.guest_efer_loaded = 1;
466 guest_efer &= ~ignore_bits;
467 guest_efer |= host_efer & ignore_bits;
468 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000469 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300470}
471
Avi Kivity51c6cf62007-08-29 03:48:05 +0300472static void reload_host_efer(struct vcpu_vmx *vmx)
473{
474 if (vmx->host_state.guest_efer_loaded) {
475 vmx->host_state.guest_efer_loaded = 0;
476 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
477 }
478}
479
Avi Kivity04d2cc72007-09-10 18:10:54 +0300480static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300481{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300482 struct vcpu_vmx *vmx = to_vmx(vcpu);
483
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400484 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300485 return;
486
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400487 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300488 /*
489 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
490 * allow segment selectors with cpl > 0 or ti == 1.
491 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400492 vmx->host_state.ldt_sel = read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200493 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400494 vmx->host_state.fs_sel = read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200495 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400496 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200497 vmx->host_state.fs_reload_needed = 0;
498 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300499 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200500 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300501 }
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400502 vmx->host_state.gs_sel = read_gs();
503 if (!(vmx->host_state.gs_sel & 7))
504 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300505 else {
506 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200507 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300508 }
509
510#ifdef CONFIG_X86_64
511 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
512 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
513#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400514 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
515 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300516#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300517
518#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400519 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400520 save_msrs(vmx->host_msrs +
521 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400522
Avi Kivity707c0872007-05-02 17:33:43 +0300523#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400524 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300525 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300526}
527
Rusty Russell8b9cf982007-07-30 16:31:43 +1000528static void vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300529{
Avi Kivity15ad7142007-07-11 18:17:21 +0300530 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300531
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400532 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300533 return;
534
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200535 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400536 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200537 if (vmx->host_state.fs_reload_needed)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400538 load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200539 if (vmx->host_state.gs_ldt_reload_needed) {
540 load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300541 /*
542 * If we have to reload gs, we must take care to
543 * preserve our gs base.
544 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300545 local_irq_save(flags);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400546 load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300547#ifdef CONFIG_X86_64
548 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
549#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300550 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300551 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200552 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400553 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
554 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300555 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300556}
557
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558/*
559 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
560 * vcpu mutex is already taken.
561 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300562static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400564 struct vcpu_vmx *vmx = to_vmx(vcpu);
565 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200566 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567
Eddie Donga3d7f852007-09-03 16:15:12 +0300568 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000569 vcpu_clear(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300570 kvm_migrate_apic_timer(vcpu);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800571 vpid_sync_vcpu_all(vmx);
Eddie Donga3d7f852007-09-03 16:15:12 +0300572 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400574 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800575 u8 error;
576
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400577 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800578 asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
579 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
580 : "cc");
581 if (error)
582 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400583 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800584 }
585
586 if (vcpu->cpu != cpu) {
587 struct descriptor_table dt;
588 unsigned long sysenter_esp;
589
590 vcpu->cpu = cpu;
591 /*
592 * Linux uses per-cpu TSS and GDT, so set these when switching
593 * processors.
594 */
595 vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */
596 get_gdt(&dt);
597 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
598
599 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
600 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300601
602 /*
603 * Make sure the time stamp counter is monotonous.
604 */
605 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200606 if (tsc_this < vcpu->arch.host_tsc) {
607 delta = vcpu->arch.host_tsc - tsc_this;
608 new_offset = vmcs_read64(TSC_OFFSET) + delta;
609 vmcs_write64(TSC_OFFSET, new_offset);
610 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612}
613
614static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
615{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000616 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617}
618
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300619static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
620{
621 if (vcpu->fpu_active)
622 return;
623 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000624 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800625 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000626 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300627 update_exception_bitmap(vcpu);
628}
629
630static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
631{
632 if (!vcpu->fpu_active)
633 return;
634 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000635 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300636 update_exception_bitmap(vcpu);
637}
638
Avi Kivity774c47f2007-02-12 00:54:47 -0800639static void vmx_vcpu_decache(struct kvm_vcpu *vcpu)
640{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000641 vcpu_clear(to_vmx(vcpu));
Avi Kivity774c47f2007-02-12 00:54:47 -0800642}
643
Avi Kivity6aa8b732006-12-10 02:21:36 -0800644static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
645{
646 return vmcs_readl(GUEST_RFLAGS);
647}
648
649static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
650{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800651 if (vcpu->arch.rmode.active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100652 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 vmcs_writel(GUEST_RFLAGS, rflags);
654}
655
656static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
657{
658 unsigned long rip;
659 u32 interruptibility;
660
661 rip = vmcs_readl(GUEST_RIP);
662 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
663 vmcs_writel(GUEST_RIP, rip);
664
665 /*
666 * We emulated an instruction, so temporary interrupt blocking
667 * should be removed, if set.
668 */
669 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
670 if (interruptibility & 3)
671 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO,
672 interruptibility & ~3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800673 vcpu->arch.interrupt_window_open = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674}
675
Avi Kivity298101d2007-11-25 13:41:11 +0200676static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
677 bool has_error_code, u32 error_code)
678{
679 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
680 nr | INTR_TYPE_EXCEPTION
Ryan Harper2e113842008-02-11 10:26:38 -0600681 | (has_error_code ? INTR_INFO_DELIVER_CODE_MASK : 0)
Avi Kivity298101d2007-11-25 13:41:11 +0200682 | INTR_INFO_VALID_MASK);
683 if (has_error_code)
684 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
685}
686
687static bool vmx_exception_injected(struct kvm_vcpu *vcpu)
688{
689 struct vcpu_vmx *vmx = to_vmx(vcpu);
690
691 return !(vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
692}
693
Avi Kivity6aa8b732006-12-10 02:21:36 -0800694/*
Eddie Donga75beee2007-05-17 18:55:15 +0300695 * Swap MSR entry in host/guest MSR entry array.
696 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200697#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000698static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300699{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400700 struct kvm_msr_entry tmp;
701
702 tmp = vmx->guest_msrs[to];
703 vmx->guest_msrs[to] = vmx->guest_msrs[from];
704 vmx->guest_msrs[from] = tmp;
705 tmp = vmx->host_msrs[to];
706 vmx->host_msrs[to] = vmx->host_msrs[from];
707 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300708}
Gabriel C54e11fa2007-08-01 16:23:10 +0200709#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300710
711/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300712 * Set up the vmcs to automatically save and restore system
713 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
714 * mode, as fiddling with msrs is very expensive.
715 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000716static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300717{
Eddie Dong2cc51562007-05-21 07:28:09 +0300718 int save_nmsrs;
Avi Kivitye38aea32007-04-19 13:22:48 +0300719
Avi Kivity33f9c502008-02-27 16:06:57 +0200720 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300721 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300722#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000723 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300724 int index;
725
Rusty Russell8b9cf982007-07-30 16:31:43 +1000726 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300727 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000728 move_msr_up(vmx, index, save_nmsrs++);
729 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300730 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000731 move_msr_up(vmx, index, save_nmsrs++);
732 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300733 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000734 move_msr_up(vmx, index, save_nmsrs++);
735 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300736 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000737 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300738 /*
739 * MSR_K6_STAR is only needed on long mode guests, and only
740 * if efer.sce is enabled.
741 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000742 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800743 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000744 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300745 }
Eddie Donga75beee2007-05-17 18:55:15 +0300746#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400747 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300748
Eddie Donga75beee2007-05-17 18:55:15 +0300749#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400750 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000751 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300752#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000753 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivitye38aea32007-04-19 13:22:48 +0300754}
755
756/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 * reads and returns guest's timestamp counter "register"
758 * guest_tsc = host_tsc + tsc_offset -- 21.3
759 */
760static u64 guest_read_tsc(void)
761{
762 u64 host_tsc, tsc_offset;
763
764 rdtscll(host_tsc);
765 tsc_offset = vmcs_read64(TSC_OFFSET);
766 return host_tsc + tsc_offset;
767}
768
769/*
770 * writes 'guest_tsc' into guest's timestamp counter "register"
771 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
772 */
773static void guest_write_tsc(u64 guest_tsc)
774{
775 u64 host_tsc;
776
777 rdtscll(host_tsc);
778 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
779}
780
Avi Kivity6aa8b732006-12-10 02:21:36 -0800781/*
782 * Reads an msr value (of 'msr_index') into 'pdata'.
783 * Returns 0 on success, non-0 otherwise.
784 * Assumes vcpu_load() was already called.
785 */
786static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
787{
788 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400789 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790
791 if (!pdata) {
792 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
793 return -EINVAL;
794 }
795
796 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800797#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800798 case MSR_FS_BASE:
799 data = vmcs_readl(GUEST_FS_BASE);
800 break;
801 case MSR_GS_BASE:
802 data = vmcs_readl(GUEST_GS_BASE);
803 break;
804 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800805 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806#endif
807 case MSR_IA32_TIME_STAMP_COUNTER:
808 data = guest_read_tsc();
809 break;
810 case MSR_IA32_SYSENTER_CS:
811 data = vmcs_read32(GUEST_SYSENTER_CS);
812 break;
813 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200814 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815 break;
816 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200817 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000820 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800821 if (msr) {
822 data = msr->data;
823 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800824 }
Avi Kivity3bab1f52006-12-29 16:49:48 -0800825 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800826 }
827
828 *pdata = data;
829 return 0;
830}
831
832/*
833 * Writes msr value into into the appropriate "register".
834 * Returns 0 on success, non-0 otherwise.
835 * Assumes vcpu_load() was already called.
836 */
837static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
838{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400839 struct vcpu_vmx *vmx = to_vmx(vcpu);
840 struct kvm_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +0300841 int ret = 0;
842
Avi Kivity6aa8b732006-12-10 02:21:36 -0800843 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800844#ifdef CONFIG_X86_64
Avi Kivity3bab1f52006-12-29 16:49:48 -0800845 case MSR_EFER:
Eddie Dong2cc51562007-05-21 07:28:09 +0300846 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300847 if (vmx->host_state.loaded) {
848 reload_host_efer(vmx);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000849 load_transition_efer(vmx);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300850 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300851 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800852 case MSR_FS_BASE:
853 vmcs_writel(GUEST_FS_BASE, data);
854 break;
855 case MSR_GS_BASE:
856 vmcs_writel(GUEST_GS_BASE, data);
857 break;
858#endif
859 case MSR_IA32_SYSENTER_CS:
860 vmcs_write32(GUEST_SYSENTER_CS, data);
861 break;
862 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200863 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864 break;
865 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +0200866 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867 break;
Avi Kivityd27d4ac2007-02-19 14:37:46 +0200868 case MSR_IA32_TIME_STAMP_COUNTER:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869 guest_write_tsc(data);
870 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800871 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +1000872 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800873 if (msr) {
874 msr->data = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400875 if (vmx->host_state.loaded)
876 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity3bab1f52006-12-29 16:49:48 -0800877 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878 }
Eddie Dong2cc51562007-05-21 07:28:09 +0300879 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800880 }
881
Eddie Dong2cc51562007-05-21 07:28:09 +0300882 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800883}
884
885/*
886 * Sync the rsp and rip registers into the vcpu structure. This allows
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800887 * registers to be accessed by indexing vcpu->arch.regs.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888 */
889static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu)
890{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800891 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
892 vcpu->arch.rip = vmcs_readl(GUEST_RIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800893}
894
895/*
896 * Syncs rsp and rip back into the vmcs. Should be called after possible
897 * modification.
898 */
899static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu)
900{
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800901 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
902 vmcs_writel(GUEST_RIP, vcpu->arch.rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800903}
904
905static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg)
906{
907 unsigned long dr7 = 0x400;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908 int old_singlestep;
909
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 old_singlestep = vcpu->guest_debug.singlestep;
911
912 vcpu->guest_debug.enabled = dbg->enabled;
913 if (vcpu->guest_debug.enabled) {
914 int i;
915
916 dr7 |= 0x200; /* exact */
917 for (i = 0; i < 4; ++i) {
918 if (!dbg->breakpoints[i].enabled)
919 continue;
920 vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address;
921 dr7 |= 2 << (i*2); /* global enable */
922 dr7 |= 0 << (i*4+16); /* execution breakpoint */
923 }
924
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925 vcpu->guest_debug.singlestep = dbg->singlestep;
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300926 } else
Avi Kivity6aa8b732006-12-10 02:21:36 -0800927 vcpu->guest_debug.singlestep = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800928
929 if (old_singlestep && !vcpu->guest_debug.singlestep) {
930 unsigned long flags;
931
932 flags = vmcs_readl(GUEST_RFLAGS);
933 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
934 vmcs_writel(GUEST_RFLAGS, flags);
935 }
936
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300937 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938 vmcs_writel(GUEST_DR7, dr7);
939
940 return 0;
941}
942
Eddie Dong2a8067f2007-08-06 16:29:07 +0300943static int vmx_get_irq(struct kvm_vcpu *vcpu)
944{
Avi Kivity1155f762007-11-22 11:30:47 +0200945 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong2a8067f2007-08-06 16:29:07 +0300946 u32 idtv_info_field;
947
Avi Kivity1155f762007-11-22 11:30:47 +0200948 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong2a8067f2007-08-06 16:29:07 +0300949 if (idtv_info_field & INTR_INFO_VALID_MASK) {
950 if (is_external_interrupt(idtv_info_field))
951 return idtv_info_field & VECTORING_INFO_VECTOR_MASK;
952 else
Mike Dayd77c26f2007-10-08 09:02:08 -0400953 printk(KERN_DEBUG "pending exception: not handled yet\n");
Eddie Dong2a8067f2007-08-06 16:29:07 +0300954 }
955 return -1;
956}
957
Avi Kivity6aa8b732006-12-10 02:21:36 -0800958static __init int cpu_has_kvm_support(void)
959{
960 unsigned long ecx = cpuid_ecx(1);
961 return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */
962}
963
964static __init int vmx_disabled_by_bios(void)
965{
966 u64 msr;
967
968 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300969 return (msr & (MSR_IA32_FEATURE_CONTROL_LOCKED |
970 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
971 == MSR_IA32_FEATURE_CONTROL_LOCKED;
972 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973}
974
Avi Kivity774c47f2007-02-12 00:54:47 -0800975static void hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976{
977 int cpu = raw_smp_processor_id();
978 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
979 u64 old;
980
981 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300982 if ((old & (MSR_IA32_FEATURE_CONTROL_LOCKED |
983 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
984 != (MSR_IA32_FEATURE_CONTROL_LOCKED |
985 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +0300987 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
988 MSR_IA32_FEATURE_CONTROL_LOCKED |
989 MSR_IA32_FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +1000990 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr)
992 : "memory", "cc");
993}
994
995static void hardware_disable(void *garbage)
996{
997 asm volatile (ASM_VMX_VMXOFF : : : "cc");
998}
999
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001000static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001001 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002{
1003 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001004 u32 ctl = ctl_min | ctl_opt;
1005
1006 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1007
1008 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1009 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1010
1011 /* Ensure minimum (required) set of control bits are supported. */
1012 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001013 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001014
1015 *result = ctl;
1016 return 0;
1017}
1018
Yang, Sheng002c7f72007-07-31 14:23:01 +03001019static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001020{
1021 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001022 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001023 u32 _pin_based_exec_control = 0;
1024 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001025 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001026 u32 _vmexit_control = 0;
1027 u32 _vmentry_control = 0;
1028
1029 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
1030 opt = 0;
1031 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1032 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001033 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001034
1035 min = CPU_BASED_HLT_EXITING |
1036#ifdef CONFIG_X86_64
1037 CPU_BASED_CR8_LOAD_EXITING |
1038 CPU_BASED_CR8_STORE_EXITING |
1039#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001040 CPU_BASED_CR3_LOAD_EXITING |
1041 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001042 CPU_BASED_USE_IO_BITMAPS |
1043 CPU_BASED_MOV_DR_EXITING |
1044 CPU_BASED_USE_TSC_OFFSETING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001045 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001046 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001047 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001048 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1049 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001050 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001051#ifdef CONFIG_X86_64
1052 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1053 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1054 ~CPU_BASED_CR8_STORE_EXITING;
1055#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001056 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001057 min2 = 0;
1058 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001059 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001060 SECONDARY_EXEC_ENABLE_VPID |
1061 SECONDARY_EXEC_ENABLE_EPT;
1062 if (adjust_vmx_controls(min2, opt2,
1063 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001064 &_cpu_based_2nd_exec_control) < 0)
1065 return -EIO;
1066 }
1067#ifndef CONFIG_X86_64
1068 if (!(_cpu_based_2nd_exec_control &
1069 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1070 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1071#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001072 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
1073 /* CR3 accesses don't need to cause VM Exits when EPT enabled */
1074 min &= ~(CPU_BASED_CR3_LOAD_EXITING |
1075 CPU_BASED_CR3_STORE_EXITING);
1076 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1077 &_cpu_based_exec_control) < 0)
1078 return -EIO;
1079 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1080 vmx_capability.ept, vmx_capability.vpid);
1081 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001082
1083 min = 0;
1084#ifdef CONFIG_X86_64
1085 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1086#endif
1087 opt = 0;
1088 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1089 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001090 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001091
1092 min = opt = 0;
1093 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1094 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001095 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001097 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001098
1099 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1100 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001101 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001102
1103#ifdef CONFIG_X86_64
1104 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1105 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001106 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001107#endif
1108
1109 /* Require Write-Back (WB) memory type for VMCS accesses. */
1110 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001111 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001112
Yang, Sheng002c7f72007-07-31 14:23:01 +03001113 vmcs_conf->size = vmx_msr_high & 0x1fff;
1114 vmcs_conf->order = get_order(vmcs_config.size);
1115 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001116
Yang, Sheng002c7f72007-07-31 14:23:01 +03001117 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1118 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001119 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001120 vmcs_conf->vmexit_ctrl = _vmexit_control;
1121 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001122
1123 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001124}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125
1126static struct vmcs *alloc_vmcs_cpu(int cpu)
1127{
1128 int node = cpu_to_node(cpu);
1129 struct page *pages;
1130 struct vmcs *vmcs;
1131
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001132 pages = alloc_pages_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133 if (!pages)
1134 return NULL;
1135 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001136 memset(vmcs, 0, vmcs_config.size);
1137 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138 return vmcs;
1139}
1140
1141static struct vmcs *alloc_vmcs(void)
1142{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001143 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144}
1145
1146static void free_vmcs(struct vmcs *vmcs)
1147{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001148 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149}
1150
Sam Ravnborg39959582007-06-01 00:47:13 -07001151static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152{
1153 int cpu;
1154
1155 for_each_online_cpu(cpu)
1156 free_vmcs(per_cpu(vmxarea, cpu));
1157}
1158
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159static __init int alloc_kvm_area(void)
1160{
1161 int cpu;
1162
1163 for_each_online_cpu(cpu) {
1164 struct vmcs *vmcs;
1165
1166 vmcs = alloc_vmcs_cpu(cpu);
1167 if (!vmcs) {
1168 free_kvm_area();
1169 return -ENOMEM;
1170 }
1171
1172 per_cpu(vmxarea, cpu) = vmcs;
1173 }
1174 return 0;
1175}
1176
1177static __init int hardware_setup(void)
1178{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001179 if (setup_vmcs_config(&vmcs_config) < 0)
1180 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001181
1182 if (boot_cpu_has(X86_FEATURE_NX))
1183 kvm_enable_efer_bits(EFER_NX);
1184
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 return alloc_kvm_area();
1186}
1187
1188static __exit void hardware_unsetup(void)
1189{
1190 free_kvm_area();
1191}
1192
Avi Kivity6aa8b732006-12-10 02:21:36 -08001193static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1194{
1195 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1196
Avi Kivity6af11b92007-03-19 13:18:10 +02001197 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198 vmcs_write16(sf->selector, save->selector);
1199 vmcs_writel(sf->base, save->base);
1200 vmcs_write32(sf->limit, save->limit);
1201 vmcs_write32(sf->ar_bytes, save->ar);
1202 } else {
1203 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1204 << AR_DPL_SHIFT;
1205 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1206 }
1207}
1208
1209static void enter_pmode(struct kvm_vcpu *vcpu)
1210{
1211 unsigned long flags;
1212
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001213 vcpu->arch.rmode.active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001215 vmcs_writel(GUEST_TR_BASE, vcpu->arch.rmode.tr.base);
1216 vmcs_write32(GUEST_TR_LIMIT, vcpu->arch.rmode.tr.limit);
1217 vmcs_write32(GUEST_TR_AR_BYTES, vcpu->arch.rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218
1219 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001220 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001221 flags |= (vcpu->arch.rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 vmcs_writel(GUEST_RFLAGS, flags);
1223
Rusty Russell66aee912007-07-17 23:34:16 +10001224 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1225 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226
1227 update_exception_bitmap(vcpu);
1228
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001229 fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1230 fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1231 fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1232 fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233
1234 vmcs_write16(GUEST_SS_SELECTOR, 0);
1235 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1236
1237 vmcs_write16(GUEST_CS_SELECTOR,
1238 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1239 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1240}
1241
Mike Dayd77c26f2007-10-08 09:02:08 -04001242static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001244 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001245 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1246 kvm->memslots[0].npages - 3;
1247 return base_gfn << PAGE_SHIFT;
1248 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001249 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250}
1251
1252static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1253{
1254 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1255
1256 save->selector = vmcs_read16(sf->selector);
1257 save->base = vmcs_readl(sf->base);
1258 save->limit = vmcs_read32(sf->limit);
1259 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001260 vmcs_write16(sf->selector, save->base >> 4);
1261 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001262 vmcs_write32(sf->limit, 0xffff);
1263 vmcs_write32(sf->ar_bytes, 0xf3);
1264}
1265
1266static void enter_rmode(struct kvm_vcpu *vcpu)
1267{
1268 unsigned long flags;
1269
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001270 vcpu->arch.rmode.active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001272 vcpu->arch.rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1274
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001275 vcpu->arch.rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1277
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001278 vcpu->arch.rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001279 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1280
1281 flags = vmcs_readl(GUEST_RFLAGS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001282 vcpu->arch.rmode.save_iopl
1283 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001285 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286
1287 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001288 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001289 update_exception_bitmap(vcpu);
1290
1291 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1292 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1293 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1294
1295 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001296 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001297 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1298 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1300
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001301 fix_rmode_seg(VCPU_SREG_ES, &vcpu->arch.rmode.es);
1302 fix_rmode_seg(VCPU_SREG_DS, &vcpu->arch.rmode.ds);
1303 fix_rmode_seg(VCPU_SREG_GS, &vcpu->arch.rmode.gs);
1304 fix_rmode_seg(VCPU_SREG_FS, &vcpu->arch.rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001305
Eddie Dong8668a3c2007-10-10 14:26:45 +08001306 kvm_mmu_reset_context(vcpu);
Avi Kivity75880a02007-06-20 11:20:04 +03001307 init_rmode_tss(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308}
1309
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001310#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001311
1312static void enter_lmode(struct kvm_vcpu *vcpu)
1313{
1314 u32 guest_tr_ar;
1315
1316 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1317 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1318 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001319 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 vmcs_write32(GUEST_TR_AR_BYTES,
1321 (guest_tr_ar & ~AR_TYPE_MASK)
1322 | AR_TYPE_BUSY_64_TSS);
1323 }
1324
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001325 vcpu->arch.shadow_efer |= EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326
Rusty Russell8b9cf982007-07-30 16:31:43 +10001327 find_msr_entry(to_vmx(vcpu), MSR_EFER)->data |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328 vmcs_write32(VM_ENTRY_CONTROLS,
1329 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001330 | VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331}
1332
1333static void exit_lmode(struct kvm_vcpu *vcpu)
1334{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001335 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336
1337 vmcs_write32(VM_ENTRY_CONTROLS,
1338 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001339 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340}
1341
1342#endif
1343
Sheng Yang2384d2b2008-01-17 15:14:33 +08001344static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1345{
1346 vpid_sync_vcpu_all(to_vmx(vcpu));
1347}
1348
Anthony Liguori25c4c272007-04-27 09:29:21 +03001349static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001350{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001351 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1352 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001353}
1354
Avi Kivity6aa8b732006-12-10 02:21:36 -08001355static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1356{
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001357 vmx_fpu_deactivate(vcpu);
1358
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001359 if (vcpu->arch.rmode.active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 enter_pmode(vcpu);
1361
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001362 if (!vcpu->arch.rmode.active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 enter_rmode(vcpu);
1364
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001365#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001366 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001367 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001369 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001370 exit_lmode(vcpu);
1371 }
1372#endif
1373
1374 vmcs_writel(CR0_READ_SHADOW, cr0);
1375 vmcs_writel(GUEST_CR0,
1376 (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001377 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001378
Rusty Russell707d92fa2007-07-17 23:19:08 +10001379 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001380 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381}
1382
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1384{
Sheng Yang2384d2b2008-01-17 15:14:33 +08001385 vmx_flush_tlb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386 vmcs_writel(GUEST_CR3, cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001387 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001388 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389}
1390
1391static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1392{
1393 vmcs_writel(CR4_READ_SHADOW, cr4);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001394 vmcs_writel(GUEST_CR4, cr4 | (vcpu->arch.rmode.active ?
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON));
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001396 vcpu->arch.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397}
1398
Avi Kivity6aa8b732006-12-10 02:21:36 -08001399static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1400{
Rusty Russell8b9cf982007-07-30 16:31:43 +10001401 struct vcpu_vmx *vmx = to_vmx(vcpu);
1402 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001403
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001404 vcpu->arch.shadow_efer = efer;
Joerg Roedel9f62e192008-01-31 14:57:39 +01001405 if (!msr)
1406 return;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407 if (efer & EFER_LMA) {
1408 vmcs_write32(VM_ENTRY_CONTROLS,
1409 vmcs_read32(VM_ENTRY_CONTROLS) |
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001410 VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411 msr->data = efer;
1412
1413 } else {
1414 vmcs_write32(VM_ENTRY_CONTROLS,
1415 vmcs_read32(VM_ENTRY_CONTROLS) &
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001416 ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417
1418 msr->data = efer & ~EFER_LME;
1419 }
Rusty Russell8b9cf982007-07-30 16:31:43 +10001420 setup_msrs(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421}
1422
Avi Kivity6aa8b732006-12-10 02:21:36 -08001423static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1424{
1425 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1426
1427 return vmcs_readl(sf->base);
1428}
1429
1430static void vmx_get_segment(struct kvm_vcpu *vcpu,
1431 struct kvm_segment *var, int seg)
1432{
1433 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1434 u32 ar;
1435
1436 var->base = vmcs_readl(sf->base);
1437 var->limit = vmcs_read32(sf->limit);
1438 var->selector = vmcs_read16(sf->selector);
1439 ar = vmcs_read32(sf->ar_bytes);
1440 if (ar & AR_UNUSABLE_MASK)
1441 ar = 0;
1442 var->type = ar & 15;
1443 var->s = (ar >> 4) & 1;
1444 var->dpl = (ar >> 5) & 3;
1445 var->present = (ar >> 7) & 1;
1446 var->avl = (ar >> 12) & 1;
1447 var->l = (ar >> 13) & 1;
1448 var->db = (ar >> 14) & 1;
1449 var->g = (ar >> 15) & 1;
1450 var->unusable = (ar >> 16) & 1;
1451}
1452
Izik Eidus2e4d2652008-03-24 19:38:34 +02001453static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1454{
1455 struct kvm_segment kvm_seg;
1456
1457 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1458 return 0;
1459
1460 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1461 return 3;
1462
1463 vmx_get_segment(vcpu, &kvm_seg, VCPU_SREG_CS);
1464 return kvm_seg.selector & 3;
1465}
1466
Avi Kivity653e3102007-05-07 10:55:37 +03001467static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469 u32 ar;
1470
Avi Kivity653e3102007-05-07 10:55:37 +03001471 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472 ar = 1 << 16;
1473 else {
1474 ar = var->type & 15;
1475 ar |= (var->s & 1) << 4;
1476 ar |= (var->dpl & 3) << 5;
1477 ar |= (var->present & 1) << 7;
1478 ar |= (var->avl & 1) << 12;
1479 ar |= (var->l & 1) << 13;
1480 ar |= (var->db & 1) << 14;
1481 ar |= (var->g & 1) << 15;
1482 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001483 if (ar == 0) /* a 0 value means unusable */
1484 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001485
1486 return ar;
1487}
1488
1489static void vmx_set_segment(struct kvm_vcpu *vcpu,
1490 struct kvm_segment *var, int seg)
1491{
1492 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1493 u32 ar;
1494
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001495 if (vcpu->arch.rmode.active && seg == VCPU_SREG_TR) {
1496 vcpu->arch.rmode.tr.selector = var->selector;
1497 vcpu->arch.rmode.tr.base = var->base;
1498 vcpu->arch.rmode.tr.limit = var->limit;
1499 vcpu->arch.rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001500 return;
1501 }
1502 vmcs_writel(sf->base, var->base);
1503 vmcs_write32(sf->limit, var->limit);
1504 vmcs_write16(sf->selector, var->selector);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001505 if (vcpu->arch.rmode.active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001506 /*
1507 * Hack real-mode segments into vm86 compatibility.
1508 */
1509 if (var->base == 0xffff0000 && var->selector == 0xf000)
1510 vmcs_writel(sf->base, 0xf0000);
1511 ar = 0xf3;
1512 } else
1513 ar = vmx_segment_access_rights(var);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514 vmcs_write32(sf->ar_bytes, ar);
1515}
1516
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1518{
1519 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1520
1521 *db = (ar >> 14) & 1;
1522 *l = (ar >> 13) & 1;
1523}
1524
1525static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1526{
1527 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1528 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1529}
1530
1531static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1532{
1533 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1534 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1535}
1536
1537static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1538{
1539 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1540 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1541}
1542
1543static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1544{
1545 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1546 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1547}
1548
Mike Dayd77c26f2007-10-08 09:02:08 -04001549static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001551 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02001552 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001553 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02001554 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555
Izik Eidus195aefd2007-10-01 22:14:18 +02001556 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1557 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001558 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001559 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
1560 r = kvm_write_guest_page(kvm, fn++, &data, 0x66, sizeof(u16));
1561 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001562 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001563 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
1564 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001565 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001566 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
1567 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001568 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02001569 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001570 r = kvm_write_guest_page(kvm, fn, &data,
1571 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
1572 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02001573 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001574 goto out;
1575
1576 ret = 1;
1577out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05001578 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579}
1580
Avi Kivity6aa8b732006-12-10 02:21:36 -08001581static void seg_setup(int seg)
1582{
1583 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1584
1585 vmcs_write16(sf->selector, 0);
1586 vmcs_writel(sf->base, 0);
1587 vmcs_write32(sf->limit, 0xffff);
1588 vmcs_write32(sf->ar_bytes, 0x93);
1589}
1590
Sheng Yangf78e0e22007-10-29 09:40:42 +08001591static int alloc_apic_access_page(struct kvm *kvm)
1592{
1593 struct kvm_userspace_memory_region kvm_userspace_mem;
1594 int r = 0;
1595
Izik Eidus72dc67a2008-02-10 18:04:15 +02001596 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001597 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001598 goto out;
1599 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
1600 kvm_userspace_mem.flags = 0;
1601 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
1602 kvm_userspace_mem.memory_size = PAGE_SIZE;
1603 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
1604 if (r)
1605 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02001606
1607 down_read(&current->mm->mmap_sem);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001608 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Izik Eidus72dc67a2008-02-10 18:04:15 +02001609 up_read(&current->mm->mmap_sem);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001610out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02001611 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001612 return r;
1613}
1614
Sheng Yang2384d2b2008-01-17 15:14:33 +08001615static void allocate_vpid(struct vcpu_vmx *vmx)
1616{
1617 int vpid;
1618
1619 vmx->vpid = 0;
1620 if (!enable_vpid || !cpu_has_vmx_vpid())
1621 return;
1622 spin_lock(&vmx_vpid_lock);
1623 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
1624 if (vpid < VMX_NR_VPIDS) {
1625 vmx->vpid = vpid;
1626 __set_bit(vpid, vmx_vpid_bitmap);
1627 }
1628 spin_unlock(&vmx_vpid_lock);
1629}
1630
Sheng Yang25c5f222008-03-28 13:18:56 +08001631void vmx_disable_intercept_for_msr(struct page *msr_bitmap, u32 msr)
1632{
1633 void *va;
1634
1635 if (!cpu_has_vmx_msr_bitmap())
1636 return;
1637
1638 /*
1639 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
1640 * have the write-low and read-high bitmap offsets the wrong way round.
1641 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
1642 */
1643 va = kmap(msr_bitmap);
1644 if (msr <= 0x1fff) {
1645 __clear_bit(msr, va + 0x000); /* read-low */
1646 __clear_bit(msr, va + 0x800); /* write-low */
1647 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1648 msr &= 0x1fff;
1649 __clear_bit(msr, va + 0x400); /* read-high */
1650 __clear_bit(msr, va + 0xc00); /* write-high */
1651 }
1652 kunmap(msr_bitmap);
1653}
1654
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655/*
1656 * Sets up the vmcs for emulated real mode.
1657 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001658static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659{
1660 u32 host_sysenter_cs;
1661 u32 junk;
1662 unsigned long a;
1663 struct descriptor_table dt;
1664 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03001665 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001666 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001667
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668 /* I/O */
He, Qingfdef3ad2007-04-30 09:45:24 +03001669 vmcs_write64(IO_BITMAP_A, page_to_phys(vmx_io_bitmap_a));
1670 vmcs_write64(IO_BITMAP_B, page_to_phys(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001671
Sheng Yang25c5f222008-03-28 13:18:56 +08001672 if (cpu_has_vmx_msr_bitmap())
1673 vmcs_write64(MSR_BITMAP, page_to_phys(vmx_msr_bitmap));
1674
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
1676
Avi Kivity6aa8b732006-12-10 02:21:36 -08001677 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001678 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
1679 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001680
1681 exec_control = vmcs_config.cpu_based_exec_ctrl;
1682 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
1683 exec_control &= ~CPU_BASED_TPR_SHADOW;
1684#ifdef CONFIG_X86_64
1685 exec_control |= CPU_BASED_CR8_STORE_EXITING |
1686 CPU_BASED_CR8_LOAD_EXITING;
1687#endif
1688 }
Sheng Yangd56f5462008-04-25 10:13:16 +08001689 if (!vm_need_ept())
1690 exec_control |= CPU_BASED_CR3_STORE_EXITING |
1691 CPU_BASED_CR3_LOAD_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001692 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693
Sheng Yang83ff3b92007-11-21 14:33:25 +08001694 if (cpu_has_secondary_exec_ctrls()) {
1695 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
1696 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1697 exec_control &=
1698 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08001699 if (vmx->vpid == 0)
1700 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yangd56f5462008-04-25 10:13:16 +08001701 if (!vm_need_ept())
1702 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang83ff3b92007-11-21 14:33:25 +08001703 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
1704 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08001705
Avi Kivityc7addb92007-09-16 18:58:32 +02001706 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
1707 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
1709
1710 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
1711 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
1712 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
1713
1714 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
1715 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1716 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
1717 vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */
1718 vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */
1719 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001720#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001721 rdmsrl(MSR_FS_BASE, a);
1722 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
1723 rdmsrl(MSR_GS_BASE, a);
1724 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
1725#else
1726 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
1727 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
1728#endif
1729
1730 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
1731
1732 get_idt(&dt);
1733 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
1734
Mike Dayd77c26f2007-10-08 09:02:08 -04001735 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03001736 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03001737 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
1738 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
1739 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740
1741 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
1742 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
1743 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
1744 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
1745 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
1746 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
1747
Avi Kivity6aa8b732006-12-10 02:21:36 -08001748 for (i = 0; i < NR_VMX_MSR; ++i) {
1749 u32 index = vmx_msr_index[i];
1750 u32 data_low, data_high;
1751 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001752 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753
1754 if (rdmsr_safe(index, &data_low, &data_high) < 0)
1755 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08001756 if (wrmsr_safe(index, data_low, data_high) < 0)
1757 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001759 vmx->host_msrs[j].index = index;
1760 vmx->host_msrs[j].reserved = 0;
1761 vmx->host_msrs[j].data = data;
1762 vmx->guest_msrs[j] = vmx->host_msrs[j];
1763 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001766 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767
1768 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001769 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
1770
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001771 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
1772 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
1773
Sheng Yangf78e0e22007-10-29 09:40:42 +08001774
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001775 return 0;
1776}
1777
1778static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
1779{
1780 struct vcpu_vmx *vmx = to_vmx(vcpu);
1781 u64 msr;
1782 int ret;
1783
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001784 down_read(&vcpu->kvm->slots_lock);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001785 if (!init_rmode_tss(vmx->vcpu.kvm)) {
1786 ret = -ENOMEM;
1787 goto out;
1788 }
1789
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001790 vmx->vcpu.arch.rmode.active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001791
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001792 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02001793 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001794 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
1795 if (vmx->vcpu.vcpu_id == 0)
1796 msr |= MSR_IA32_APICBASE_BSP;
1797 kvm_set_apic_base(&vmx->vcpu, msr);
1798
1799 fx_init(&vmx->vcpu);
1800
1801 /*
1802 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
1803 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
1804 */
1805 if (vmx->vcpu.vcpu_id == 0) {
1806 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
1807 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
1808 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001809 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
1810 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02001811 }
1812 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
1813 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1814
1815 seg_setup(VCPU_SREG_DS);
1816 seg_setup(VCPU_SREG_ES);
1817 seg_setup(VCPU_SREG_FS);
1818 seg_setup(VCPU_SREG_GS);
1819 seg_setup(VCPU_SREG_SS);
1820
1821 vmcs_write16(GUEST_TR_SELECTOR, 0);
1822 vmcs_writel(GUEST_TR_BASE, 0);
1823 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
1824 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1825
1826 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
1827 vmcs_writel(GUEST_LDTR_BASE, 0);
1828 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
1829 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
1830
1831 vmcs_write32(GUEST_SYSENTER_CS, 0);
1832 vmcs_writel(GUEST_SYSENTER_ESP, 0);
1833 vmcs_writel(GUEST_SYSENTER_EIP, 0);
1834
1835 vmcs_writel(GUEST_RFLAGS, 0x02);
1836 if (vmx->vcpu.vcpu_id == 0)
1837 vmcs_writel(GUEST_RIP, 0xfff0);
1838 else
1839 vmcs_writel(GUEST_RIP, 0);
1840 vmcs_writel(GUEST_RSP, 0);
1841
1842 /* todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 */
1843 vmcs_writel(GUEST_DR7, 0x400);
1844
1845 vmcs_writel(GUEST_GDTR_BASE, 0);
1846 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
1847
1848 vmcs_writel(GUEST_IDTR_BASE, 0);
1849 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
1850
1851 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
1852 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
1853 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
1854
1855 guest_write_tsc(0);
1856
1857 /* Special registers */
1858 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
1859
1860 setup_msrs(vmx);
1861
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
1863
Sheng Yangf78e0e22007-10-29 09:40:42 +08001864 if (cpu_has_vmx_tpr_shadow()) {
1865 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
1866 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
1867 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001868 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08001869 vmcs_write32(TPR_THRESHOLD, 0);
1870 }
1871
1872 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
1873 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001874 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875
Sheng Yang2384d2b2008-01-17 15:14:33 +08001876 if (vmx->vpid != 0)
1877 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
1878
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001879 vmx->vcpu.arch.cr0 = 0x60000010;
1880 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001881 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001882 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10001883 vmx_fpu_activate(&vmx->vcpu);
1884 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885
Sheng Yang2384d2b2008-01-17 15:14:33 +08001886 vpid_sync_vcpu_all(vmx);
1887
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001888 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03001891 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892 return ret;
1893}
1894
Eddie Dong85f455f2007-07-06 12:20:49 +03001895static void vmx_inject_irq(struct kvm_vcpu *vcpu, int irq)
1896{
Avi Kivity9c8cba32007-11-22 11:42:59 +02001897 struct vcpu_vmx *vmx = to_vmx(vcpu);
1898
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04001899 KVMTRACE_1D(INJ_VIRQ, vcpu, (u32)irq, handler);
1900
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001901 if (vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02001902 vmx->rmode.irq.pending = true;
1903 vmx->rmode.irq.vector = irq;
1904 vmx->rmode.irq.rip = vmcs_readl(GUEST_RIP);
Avi Kivity9c5623e2007-11-08 18:19:20 +02001905 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1906 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
1907 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Avi Kivity9c8cba32007-11-22 11:42:59 +02001908 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03001909 return;
1910 }
1911 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
1912 irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
1913}
1914
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915static void kvm_do_inject_irq(struct kvm_vcpu *vcpu)
1916{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001917 int word_index = __ffs(vcpu->arch.irq_summary);
1918 int bit_index = __ffs(vcpu->arch.irq_pending[word_index]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001919 int irq = word_index * BITS_PER_LONG + bit_index;
1920
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001921 clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]);
1922 if (!vcpu->arch.irq_pending[word_index])
1923 clear_bit(word_index, &vcpu->arch.irq_summary);
Eddie Dong85f455f2007-07-06 12:20:49 +03001924 vmx_inject_irq(vcpu, irq);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001925}
1926
Dor Laorc1150d82007-01-05 16:36:24 -08001927
1928static void do_interrupt_requests(struct kvm_vcpu *vcpu,
1929 struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930{
Dor Laorc1150d82007-01-05 16:36:24 -08001931 u32 cpu_based_vm_exec_control;
1932
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001933 vcpu->arch.interrupt_window_open =
Dor Laorc1150d82007-01-05 16:36:24 -08001934 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
1935 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
1936
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001937 if (vcpu->arch.interrupt_window_open &&
1938 vcpu->arch.irq_summary &&
Dor Laorc1150d82007-01-05 16:36:24 -08001939 !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940 /*
Dor Laorc1150d82007-01-05 16:36:24 -08001941 * If interrupts enabled, and not blocked by sti or mov ss. Good.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001942 */
1943 kvm_do_inject_irq(vcpu);
Dor Laorc1150d82007-01-05 16:36:24 -08001944
1945 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001946 if (!vcpu->arch.interrupt_window_open &&
1947 (vcpu->arch.irq_summary || kvm_run->request_interrupt_window))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 /*
1949 * Interrupts blocked. Wait for unblock.
1950 */
Dor Laorc1150d82007-01-05 16:36:24 -08001951 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
1952 else
1953 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
1954 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955}
1956
Izik Eiduscbc94022007-10-25 00:29:55 +02001957static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
1958{
1959 int ret;
1960 struct kvm_userspace_memory_region tss_mem = {
1961 .slot = 8,
1962 .guest_phys_addr = addr,
1963 .memory_size = PAGE_SIZE * 3,
1964 .flags = 0,
1965 };
1966
1967 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
1968 if (ret)
1969 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001970 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02001971 return 0;
1972}
1973
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu)
1975{
1976 struct kvm_guest_debug *dbg = &vcpu->guest_debug;
1977
1978 set_debugreg(dbg->bp[0], 0);
1979 set_debugreg(dbg->bp[1], 1);
1980 set_debugreg(dbg->bp[2], 2);
1981 set_debugreg(dbg->bp[3], 3);
1982
1983 if (dbg->singlestep) {
1984 unsigned long flags;
1985
1986 flags = vmcs_readl(GUEST_RFLAGS);
1987 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1988 vmcs_writel(GUEST_RFLAGS, flags);
1989 }
1990}
1991
1992static int handle_rmode_exception(struct kvm_vcpu *vcpu,
1993 int vec, u32 err_code)
1994{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001995 if (!vcpu->arch.rmode.active)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001996 return 0;
1997
Nitin A Kambleb3f37702007-05-17 15:50:34 +03001998 /*
1999 * Instruction with address size override prefix opcode 0x67
2000 * Cause the #SS fault with 0 error code in VM86 mode.
2001 */
2002 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Laurent Vivier34273182007-09-18 11:27:37 +02002003 if (emulate_instruction(vcpu, NULL, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 return 1;
2005 return 0;
2006}
2007
2008static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2009{
Avi Kivity1155f762007-11-22 11:30:47 +02002010 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011 u32 intr_info, error_code;
2012 unsigned long cr2, rip;
2013 u32 vect_info;
2014 enum emulation_result er;
2015
Avi Kivity1155f762007-11-22 11:30:47 +02002016 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2018
2019 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002020 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002022 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002023
Eddie Dong85f455f2007-07-06 12:20:49 +03002024 if (!irqchip_in_kernel(vcpu->kvm) && is_external_interrupt(vect_info)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002025 int irq = vect_info & VECTORING_INFO_VECTOR_MASK;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002026 set_bit(irq, vcpu->arch.irq_pending);
2027 set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002028 }
2029
Avi Kivity1b6269d2007-10-09 12:12:19 +02002030 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) /* nmi */
2031 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002032
2033 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002034 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002035 return 1;
2036 }
2037
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002038 if (is_invalid_opcode(intr_info)) {
Sheng Yang571008d2008-01-02 14:49:22 +08002039 er = emulate_instruction(vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002040 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002041 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002042 return 1;
2043 }
2044
Avi Kivity6aa8b732006-12-10 02:21:36 -08002045 error_code = 0;
2046 rip = vmcs_readl(GUEST_RIP);
Ryan Harper2e113842008-02-11 10:26:38 -06002047 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2049 if (is_page_fault(intr_info)) {
2050 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002051 KVMTRACE_3D(PAGE_FAULT, vcpu, error_code, (u32)cr2,
2052 (u32)((u64)cr2 >> 32), handler);
Avi Kivity30677142007-10-28 18:48:59 +02002053 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002054 }
2055
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002056 if (vcpu->arch.rmode.active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002057 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002058 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002059 if (vcpu->arch.halt_request) {
2060 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002061 return kvm_emulate_halt(vcpu);
2062 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002063 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002064 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065
Mike Dayd77c26f2007-10-08 09:02:08 -04002066 if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) ==
2067 (INTR_TYPE_EXCEPTION | 1)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2069 return 0;
2070 }
2071 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2072 kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK;
2073 kvm_run->ex.error_code = error_code;
2074 return 0;
2075}
2076
2077static int handle_external_interrupt(struct kvm_vcpu *vcpu,
2078 struct kvm_run *kvm_run)
2079{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002080 ++vcpu->stat.irq_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002081 KVMTRACE_1D(INTR, vcpu, vmcs_read32(VM_EXIT_INTR_INFO), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002082 return 1;
2083}
2084
Avi Kivity988ad742007-02-12 00:54:36 -08002085static int handle_triple_fault(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2086{
2087 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2088 return 0;
2089}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2092{
He, Qingbfdaab02007-09-12 14:18:28 +08002093 unsigned long exit_qualification;
Avi Kivity039576c2007-03-20 12:46:50 +02002094 int size, down, in, string, rep;
2095 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096
Avi Kivity1165f5f2007-04-19 17:27:43 +03002097 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002098 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002099 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002100
2101 if (string) {
Laurent Vivier34273182007-09-18 11:27:37 +02002102 if (emulate_instruction(vcpu,
2103 kvm_run, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002104 return 0;
2105 return 1;
2106 }
2107
2108 size = (exit_qualification & 7) + 1;
2109 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002110 down = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002111 rep = (exit_qualification & 32) != 0;
2112 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002113
Laurent Vivier3090dd72007-08-05 10:43:32 +03002114 return kvm_emulate_pio(vcpu, kvm_run, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002115}
2116
Ingo Molnar102d8322007-02-19 14:37:47 +02002117static void
2118vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2119{
2120 /*
2121 * Patch in the VMCALL instruction:
2122 */
2123 hypercall[0] = 0x0f;
2124 hypercall[1] = 0x01;
2125 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002126}
2127
Avi Kivity6aa8b732006-12-10 02:21:36 -08002128static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2129{
He, Qingbfdaab02007-09-12 14:18:28 +08002130 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131 int cr;
2132 int reg;
2133
He, Qingbfdaab02007-09-12 14:18:28 +08002134 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002135 cr = exit_qualification & 15;
2136 reg = (exit_qualification >> 8) & 15;
2137 switch ((exit_qualification >> 4) & 3) {
2138 case 0: /* mov to cr */
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002139 KVMTRACE_3D(CR_WRITE, vcpu, (u32)cr, (u32)vcpu->arch.regs[reg],
2140 (u32)((u64)vcpu->arch.regs[reg] >> 32), handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002141 switch (cr) {
2142 case 0:
2143 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002144 kvm_set_cr0(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002145 skip_emulated_instruction(vcpu);
2146 return 1;
2147 case 3:
2148 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002149 kvm_set_cr3(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002150 skip_emulated_instruction(vcpu);
2151 return 1;
2152 case 4:
2153 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002154 kvm_set_cr4(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002155 skip_emulated_instruction(vcpu);
2156 return 1;
2157 case 8:
2158 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002159 kvm_set_cr8(vcpu, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160 skip_emulated_instruction(vcpu);
Avi Kivitye5314062007-12-06 16:32:45 +02002161 if (irqchip_in_kernel(vcpu->kvm))
2162 return 1;
Yang, Sheng253abde2007-08-16 13:01:00 +03002163 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2164 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 };
2166 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002167 case 2: /* clts */
2168 vcpu_load_rsp_rip(vcpu);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002169 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002170 vcpu->arch.cr0 &= ~X86_CR0_TS;
2171 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002172 vmx_fpu_activate(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002173 KVMTRACE_0D(CLTS, vcpu, handler);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002174 skip_emulated_instruction(vcpu);
2175 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002176 case 1: /*mov from cr*/
2177 switch (cr) {
2178 case 3:
2179 vcpu_load_rsp_rip(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002180 vcpu->arch.regs[reg] = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002181 vcpu_put_rsp_rip(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002182 KVMTRACE_3D(CR_READ, vcpu, (u32)cr,
2183 (u32)vcpu->arch.regs[reg],
2184 (u32)((u64)vcpu->arch.regs[reg] >> 32),
2185 handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002186 skip_emulated_instruction(vcpu);
2187 return 1;
2188 case 8:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002189 vcpu_load_rsp_rip(vcpu);
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002190 vcpu->arch.regs[reg] = kvm_get_cr8(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 vcpu_put_rsp_rip(vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002192 KVMTRACE_2D(CR_READ, vcpu, (u32)cr,
2193 (u32)vcpu->arch.regs[reg], handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 skip_emulated_instruction(vcpu);
2195 return 1;
2196 }
2197 break;
2198 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002199 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002200
2201 skip_emulated_instruction(vcpu);
2202 return 1;
2203 default:
2204 break;
2205 }
2206 kvm_run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002207 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002208 (int)(exit_qualification >> 4) & 3, cr);
2209 return 0;
2210}
2211
2212static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2213{
He, Qingbfdaab02007-09-12 14:18:28 +08002214 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215 unsigned long val;
2216 int dr, reg;
2217
2218 /*
2219 * FIXME: this code assumes the host is debugging the guest.
2220 * need to deal with guest debugging itself too.
2221 */
He, Qingbfdaab02007-09-12 14:18:28 +08002222 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223 dr = exit_qualification & 7;
2224 reg = (exit_qualification >> 8) & 15;
2225 vcpu_load_rsp_rip(vcpu);
2226 if (exit_qualification & 16) {
2227 /* mov from dr */
2228 switch (dr) {
2229 case 6:
2230 val = 0xffff0ff0;
2231 break;
2232 case 7:
2233 val = 0x400;
2234 break;
2235 default:
2236 val = 0;
2237 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002238 vcpu->arch.regs[reg] = val;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002239 KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240 } else {
2241 /* mov to dr */
2242 }
2243 vcpu_put_rsp_rip(vcpu);
2244 skip_emulated_instruction(vcpu);
2245 return 1;
2246}
2247
2248static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2249{
Avi Kivity06465c52007-02-28 20:46:53 +02002250 kvm_emulate_cpuid(vcpu);
2251 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002252}
2253
2254static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2255{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002256 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257 u64 data;
2258
2259 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002260 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261 return 1;
2262 }
2263
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002264 KVMTRACE_3D(MSR_READ, vcpu, ecx, (u32)data, (u32)(data >> 32),
2265 handler);
2266
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002268 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
2269 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270 skip_emulated_instruction(vcpu);
2271 return 1;
2272}
2273
2274static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2275{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002276 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
2277 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
2278 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002280 KVMTRACE_3D(MSR_WRITE, vcpu, ecx, (u32)data, (u32)(data >> 32),
2281 handler);
2282
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002284 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 return 1;
2286 }
2287
2288 skip_emulated_instruction(vcpu);
2289 return 1;
2290}
2291
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002292static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu,
2293 struct kvm_run *kvm_run)
2294{
2295 return 1;
2296}
2297
Avi Kivity6aa8b732006-12-10 02:21:36 -08002298static int handle_interrupt_window(struct kvm_vcpu *vcpu,
2299 struct kvm_run *kvm_run)
2300{
Eddie Dong85f455f2007-07-06 12:20:49 +03002301 u32 cpu_based_vm_exec_control;
2302
2303 /* clear pending irq */
2304 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2305 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
2306 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002307
2308 KVMTRACE_0D(PEND_INTR, vcpu, handler);
2309
Dor Laorc1150d82007-01-05 16:36:24 -08002310 /*
2311 * If the user space waits to inject interrupts, exit as soon as
2312 * possible
2313 */
2314 if (kvm_run->request_interrupt_window &&
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002315 !vcpu->arch.irq_summary) {
Dor Laorc1150d82007-01-05 16:36:24 -08002316 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Avi Kivity1165f5f2007-04-19 17:27:43 +03002317 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002318 return 0;
2319 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002320 return 1;
2321}
2322
2323static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2324{
2325 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03002326 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327}
2328
Ingo Molnarc21415e2007-02-19 14:37:47 +02002329static int handle_vmcall(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2330{
Dor Laor510043d2007-02-19 18:25:43 +02002331 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002332 kvm_emulate_hypercall(vcpu);
2333 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02002334}
2335
Eddie Donge5edaa02007-11-11 12:28:35 +02002336static int handle_wbinvd(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2337{
2338 skip_emulated_instruction(vcpu);
2339 /* TODO: Add support for VT-d/pass-through device */
2340 return 1;
2341}
2342
Sheng Yangf78e0e22007-10-29 09:40:42 +08002343static int handle_apic_access(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2344{
2345 u64 exit_qualification;
2346 enum emulation_result er;
2347 unsigned long offset;
2348
2349 exit_qualification = vmcs_read64(EXIT_QUALIFICATION);
2350 offset = exit_qualification & 0xffful;
2351
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002352 KVMTRACE_1D(APIC_ACCESS, vcpu, (u32)offset, handler);
2353
Sheng Yangf78e0e22007-10-29 09:40:42 +08002354 er = emulate_instruction(vcpu, kvm_run, 0, 0, 0);
2355
2356 if (er != EMULATE_DONE) {
2357 printk(KERN_ERR
2358 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
2359 offset);
2360 return -ENOTSUPP;
2361 }
2362 return 1;
2363}
2364
Izik Eidus37817f22008-03-24 23:14:53 +02002365static int handle_task_switch(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
2366{
2367 unsigned long exit_qualification;
2368 u16 tss_selector;
2369 int reason;
2370
2371 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
2372
2373 reason = (u32)exit_qualification >> 30;
2374 tss_selector = exit_qualification;
2375
2376 return kvm_task_switch(vcpu, tss_selector, reason);
2377}
2378
Avi Kivity6aa8b732006-12-10 02:21:36 -08002379/*
2380 * The exit handlers return 1 if the exit was handled fully and guest execution
2381 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
2382 * to be done to userspace and return 0.
2383 */
2384static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu,
2385 struct kvm_run *kvm_run) = {
2386 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
2387 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08002388 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 [EXIT_REASON_CR_ACCESS] = handle_cr,
2391 [EXIT_REASON_DR_ACCESS] = handle_dr,
2392 [EXIT_REASON_CPUID] = handle_cpuid,
2393 [EXIT_REASON_MSR_READ] = handle_rdmsr,
2394 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
2395 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
2396 [EXIT_REASON_HLT] = handle_halt,
Ingo Molnarc21415e2007-02-19 14:37:47 +02002397 [EXIT_REASON_VMCALL] = handle_vmcall,
Sheng Yangf78e0e22007-10-29 09:40:42 +08002398 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
2399 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02002400 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02002401 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402};
2403
2404static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04002405 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406
2407/*
2408 * The guest has exited. See if we can fix it or if we need userspace
2409 * assistance.
2410 */
2411static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2412{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002413 u32 exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity29bd8a72007-09-10 17:27:03 +03002414 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1155f762007-11-22 11:30:47 +02002415 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03002416
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002417 KVMTRACE_3D(VMEXIT, vcpu, exit_reason, (u32)vmcs_readl(GUEST_RIP),
2418 (u32)((u64)vmcs_readl(GUEST_RIP) >> 32), entryexit);
2419
Avi Kivity29bd8a72007-09-10 17:27:03 +03002420 if (unlikely(vmx->fail)) {
2421 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2422 kvm_run->fail_entry.hardware_entry_failure_reason
2423 = vmcs_read32(VM_INSTRUCTION_ERROR);
2424 return 0;
2425 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002426
Mike Dayd77c26f2007-10-08 09:02:08 -04002427 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
2428 exit_reason != EXIT_REASON_EXCEPTION_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002429 printk(KERN_WARNING "%s: unexpected, valid vectoring info and "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002430 "exit reason is 0x%x\n", __func__, exit_reason);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002431 if (exit_reason < kvm_vmx_max_exit_handlers
2432 && kvm_vmx_exit_handlers[exit_reason])
2433 return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run);
2434 else {
2435 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
2436 kvm_run->hw.hardware_exit_reason = exit_reason;
2437 }
2438 return 0;
2439}
2440
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002441static void update_tpr_threshold(struct kvm_vcpu *vcpu)
2442{
2443 int max_irr, tpr;
2444
2445 if (!vm_need_tpr_shadow(vcpu->kvm))
2446 return;
2447
2448 if (!kvm_lapic_enabled(vcpu) ||
2449 ((max_irr = kvm_lapic_find_highest_irr(vcpu)) == -1)) {
2450 vmcs_write32(TPR_THRESHOLD, 0);
2451 return;
2452 }
2453
2454 tpr = (kvm_lapic_get_cr8(vcpu) & 0x0f) << 4;
2455 vmcs_write32(TPR_THRESHOLD, (max_irr > tpr) ? tpr >> 4 : max_irr >> 4);
2456}
2457
Eddie Dong85f455f2007-07-06 12:20:49 +03002458static void enable_irq_window(struct kvm_vcpu *vcpu)
2459{
2460 u32 cpu_based_vm_exec_control;
2461
2462 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2463 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2464 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2465}
2466
2467static void vmx_intr_assist(struct kvm_vcpu *vcpu)
2468{
Avi Kivity1155f762007-11-22 11:30:47 +02002469 struct vcpu_vmx *vmx = to_vmx(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03002470 u32 idtv_info_field, intr_info_field;
2471 int has_ext_irq, interrupt_window_open;
Eddie Dong1b9778d2007-09-03 16:56:58 +03002472 int vector;
Eddie Dong85f455f2007-07-06 12:20:49 +03002473
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002474 update_tpr_threshold(vcpu);
2475
Eddie Dong85f455f2007-07-06 12:20:49 +03002476 has_ext_irq = kvm_cpu_has_interrupt(vcpu);
2477 intr_info_field = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD);
Avi Kivity1155f762007-11-22 11:30:47 +02002478 idtv_info_field = vmx->idt_vectoring_info;
Eddie Dong85f455f2007-07-06 12:20:49 +03002479 if (intr_info_field & INTR_INFO_VALID_MASK) {
2480 if (idtv_info_field & INTR_INFO_VALID_MASK) {
2481 /* TODO: fault when IDT_Vectoring */
Ryan Harper9584bf22007-12-13 10:21:10 -06002482 if (printk_ratelimit())
2483 printk(KERN_ERR "Fault when IDT_Vectoring\n");
Eddie Dong85f455f2007-07-06 12:20:49 +03002484 }
2485 if (has_ext_irq)
2486 enable_irq_window(vcpu);
2487 return;
2488 }
2489 if (unlikely(idtv_info_field & INTR_INFO_VALID_MASK)) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002490 if ((idtv_info_field & VECTORING_INFO_TYPE_MASK)
2491 == INTR_TYPE_EXT_INTR
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002492 && vcpu->arch.rmode.active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002493 u8 vect = idtv_info_field & VECTORING_INFO_VECTOR_MASK;
2494
2495 vmx_inject_irq(vcpu, vect);
2496 if (unlikely(has_ext_irq))
2497 enable_irq_window(vcpu);
2498 return;
2499 }
2500
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002501 KVMTRACE_1D(REDELIVER_EVT, vcpu, idtv_info_field, handler);
2502
Eddie Dong85f455f2007-07-06 12:20:49 +03002503 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, idtv_info_field);
2504 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2505 vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
2506
Ryan Harper2e113842008-02-11 10:26:38 -06002507 if (unlikely(idtv_info_field & INTR_INFO_DELIVER_CODE_MASK))
Eddie Dong85f455f2007-07-06 12:20:49 +03002508 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE,
2509 vmcs_read32(IDT_VECTORING_ERROR_CODE));
2510 if (unlikely(has_ext_irq))
2511 enable_irq_window(vcpu);
2512 return;
2513 }
2514 if (!has_ext_irq)
2515 return;
2516 interrupt_window_open =
2517 ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2518 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0);
Eddie Dong1b9778d2007-09-03 16:56:58 +03002519 if (interrupt_window_open) {
2520 vector = kvm_cpu_get_interrupt(vcpu);
2521 vmx_inject_irq(vcpu, vector);
2522 kvm_timer_intr_post(vcpu, vector);
2523 } else
Eddie Dong85f455f2007-07-06 12:20:49 +03002524 enable_irq_window(vcpu);
2525}
2526
Avi Kivity9c8cba32007-11-22 11:42:59 +02002527/*
2528 * Failure to inject an interrupt should give us the information
2529 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
2530 * when fetching the interrupt redirection bitmap in the real-mode
2531 * tss, this doesn't happen. So we do it ourselves.
2532 */
2533static void fixup_rmode_irq(struct vcpu_vmx *vmx)
2534{
2535 vmx->rmode.irq.pending = 0;
2536 if (vmcs_readl(GUEST_RIP) + 1 != vmx->rmode.irq.rip)
2537 return;
2538 vmcs_writel(GUEST_RIP, vmx->rmode.irq.rip);
2539 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
2540 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
2541 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
2542 return;
2543 }
2544 vmx->idt_vectoring_info =
2545 VECTORING_INFO_VALID_MASK
2546 | INTR_TYPE_EXT_INTR
2547 | vmx->rmode.irq.vector;
2548}
2549
Avi Kivity04d2cc72007-09-10 18:10:54 +03002550static void vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002552 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002553 u32 intr_info;
Avi Kivitye6adf282007-04-30 16:07:54 +03002554
2555 /*
2556 * Loading guest fpu may have cleared host cr0.ts
2557 */
2558 vmcs_writel(HOST_CR0, read_cr0());
2559
Mike Dayd77c26f2007-10-08 09:02:08 -04002560 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 /* Store host registers */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002562#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02002563 "push %%rdx; push %%rbp;"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564 "push %%rcx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565#else
Laurent Vivierff593e52007-10-25 14:18:55 +02002566 "push %%edx; push %%ebp;"
2567 "push %%ecx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002568#endif
Laurent Vivierc2036302007-10-25 14:18:52 +02002569 ASM_VMX_VMWRITE_RSP_RDX "\n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002570 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02002571 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002572 /* Load guest registers. Don't clobber flags. */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002573#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002574 "mov %c[cr2](%0), %%rax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002575 "mov %%rax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002576 "mov %c[rax](%0), %%rax \n\t"
2577 "mov %c[rbx](%0), %%rbx \n\t"
2578 "mov %c[rdx](%0), %%rdx \n\t"
2579 "mov %c[rsi](%0), %%rsi \n\t"
2580 "mov %c[rdi](%0), %%rdi \n\t"
2581 "mov %c[rbp](%0), %%rbp \n\t"
2582 "mov %c[r8](%0), %%r8 \n\t"
2583 "mov %c[r9](%0), %%r9 \n\t"
2584 "mov %c[r10](%0), %%r10 \n\t"
2585 "mov %c[r11](%0), %%r11 \n\t"
2586 "mov %c[r12](%0), %%r12 \n\t"
2587 "mov %c[r13](%0), %%r13 \n\t"
2588 "mov %c[r14](%0), %%r14 \n\t"
2589 "mov %c[r15](%0), %%r15 \n\t"
2590 "mov %c[rcx](%0), %%rcx \n\t" /* kills %0 (rcx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002591#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002592 "mov %c[cr2](%0), %%eax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002593 "mov %%eax, %%cr2 \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002594 "mov %c[rax](%0), %%eax \n\t"
2595 "mov %c[rbx](%0), %%ebx \n\t"
2596 "mov %c[rdx](%0), %%edx \n\t"
2597 "mov %c[rsi](%0), %%esi \n\t"
2598 "mov %c[rdi](%0), %%edi \n\t"
2599 "mov %c[rbp](%0), %%ebp \n\t"
2600 "mov %c[rcx](%0), %%ecx \n\t" /* kills %0 (ecx) */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002601#endif
2602 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03002603 "jne .Llaunched \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 ASM_VMX_VMLAUNCH "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03002605 "jmp .Lkvm_vmx_return \n\t"
2606 ".Llaunched: " ASM_VMX_VMRESUME "\n\t"
2607 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608 /* Save guest registers, load host registers, keep flags */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002609#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02002610 "xchg %0, (%%rsp) \n\t"
2611 "mov %%rax, %c[rax](%0) \n\t"
2612 "mov %%rbx, %c[rbx](%0) \n\t"
2613 "pushq (%%rsp); popq %c[rcx](%0) \n\t"
2614 "mov %%rdx, %c[rdx](%0) \n\t"
2615 "mov %%rsi, %c[rsi](%0) \n\t"
2616 "mov %%rdi, %c[rdi](%0) \n\t"
2617 "mov %%rbp, %c[rbp](%0) \n\t"
2618 "mov %%r8, %c[r8](%0) \n\t"
2619 "mov %%r9, %c[r9](%0) \n\t"
2620 "mov %%r10, %c[r10](%0) \n\t"
2621 "mov %%r11, %c[r11](%0) \n\t"
2622 "mov %%r12, %c[r12](%0) \n\t"
2623 "mov %%r13, %c[r13](%0) \n\t"
2624 "mov %%r14, %c[r14](%0) \n\t"
2625 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 "mov %%cr2, %%rax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002627 "mov %%rax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628
Avi Kivitye08aa782007-11-15 18:06:18 +02002629 "pop %%rbp; pop %%rbp; pop %%rdx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630#else
Avi Kivitye08aa782007-11-15 18:06:18 +02002631 "xchg %0, (%%esp) \n\t"
2632 "mov %%eax, %c[rax](%0) \n\t"
2633 "mov %%ebx, %c[rbx](%0) \n\t"
2634 "pushl (%%esp); popl %c[rcx](%0) \n\t"
2635 "mov %%edx, %c[rdx](%0) \n\t"
2636 "mov %%esi, %c[rsi](%0) \n\t"
2637 "mov %%edi, %c[rdi](%0) \n\t"
2638 "mov %%ebp, %c[rbp](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 "mov %%cr2, %%eax \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02002640 "mov %%eax, %c[cr2](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641
Avi Kivitye08aa782007-11-15 18:06:18 +02002642 "pop %%ebp; pop %%ebp; pop %%edx \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08002643#endif
Avi Kivitye08aa782007-11-15 18:06:18 +02002644 "setbe %c[fail](%0) \n\t"
2645 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
2646 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
2647 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002648 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
2649 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
2650 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
2651 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
2652 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
2653 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
2654 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002655#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002656 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
2657 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
2658 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
2659 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
2660 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
2661 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
2662 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
2663 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002665 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02002666 : "cc", "memory"
2667#ifdef CONFIG_X86_64
2668 , "rbx", "rdi", "rsi"
2669 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
Laurent Vivierff593e52007-10-25 14:18:55 +02002670#else
2671 , "ebx", "edi", "rsi"
Laurent Vivierc2036302007-10-25 14:18:52 +02002672#endif
2673 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674
Avi Kivity1155f762007-11-22 11:30:47 +02002675 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02002676 if (vmx->rmode.irq.pending)
2677 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02002678
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002679 vcpu->arch.interrupt_window_open =
Mike Dayd77c26f2007-10-08 09:02:08 -04002680 (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681
Mike Dayd77c26f2007-10-08 09:02:08 -04002682 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03002683 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02002684
2685 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2686
2687 /* We need to handle NMIs before interrupts are enabled */
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002688 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */
2689 KVMTRACE_0D(NMI, vcpu, handler);
Avi Kivity1b6269d2007-10-09 12:12:19 +02002690 asm("int $2");
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002691 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002692}
2693
Avi Kivity6aa8b732006-12-10 02:21:36 -08002694static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
2695{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002696 struct vcpu_vmx *vmx = to_vmx(vcpu);
2697
2698 if (vmx->vmcs) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10002699 on_each_cpu(__vcpu_clear, vmx, 0, 1);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002700 free_vmcs(vmx->vmcs);
2701 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002702 }
2703}
2704
2705static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
2706{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002707 struct vcpu_vmx *vmx = to_vmx(vcpu);
2708
Sheng Yang2384d2b2008-01-17 15:14:33 +08002709 spin_lock(&vmx_vpid_lock);
2710 if (vmx->vpid != 0)
2711 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2712 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002714 kfree(vmx->host_msrs);
2715 kfree(vmx->guest_msrs);
2716 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10002717 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002718}
2719
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002720static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002721{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002722 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10002723 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03002724 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002726 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002727 return ERR_PTR(-ENOMEM);
2728
Sheng Yang2384d2b2008-01-17 15:14:33 +08002729 allocate_vpid(vmx);
2730
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002731 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
2732 if (err)
2733 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002734
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002735 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002736 if (!vmx->guest_msrs) {
2737 err = -ENOMEM;
2738 goto uninit_vcpu;
2739 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08002740
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002741 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
2742 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002743 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002744
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002745 vmx->vmcs = alloc_vmcs();
2746 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002747 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002748
2749 vmcs_clear(vmx->vmcs);
2750
Avi Kivity15ad7142007-07-11 18:17:21 +03002751 cpu = get_cpu();
2752 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002753 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002754 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03002755 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002756 if (err)
2757 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02002758 if (vm_need_virtualize_apic_accesses(kvm))
2759 if (alloc_apic_access_page(kvm) != 0)
2760 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002761
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002762 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08002763
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002764free_vmcs:
2765 free_vmcs(vmx->vmcs);
2766free_msrs:
2767 kfree(vmx->host_msrs);
2768free_guest_msrs:
2769 kfree(vmx->guest_msrs);
2770uninit_vcpu:
2771 kvm_vcpu_uninit(&vmx->vcpu);
2772free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10002773 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10002774 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775}
2776
Yang, Sheng002c7f72007-07-31 14:23:01 +03002777static void __init vmx_check_processor_compat(void *rtn)
2778{
2779 struct vmcs_config vmcs_conf;
2780
2781 *(int *)rtn = 0;
2782 if (setup_vmcs_config(&vmcs_conf) < 0)
2783 *(int *)rtn = -EIO;
2784 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
2785 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
2786 smp_processor_id());
2787 *(int *)rtn = -EIO;
2788 }
2789}
2790
Sheng Yang67253af2008-04-25 10:20:22 +08002791static int get_ept_level(void)
2792{
2793 return VMX_EPT_DEFAULT_GAW + 1;
2794}
2795
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03002796static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797 .cpu_has_kvm_support = cpu_has_kvm_support,
2798 .disabled_by_bios = vmx_disabled_by_bios,
2799 .hardware_setup = hardware_setup,
2800 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03002801 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 .hardware_enable = hardware_enable,
2803 .hardware_disable = hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02002804 .cpu_has_accelerated_tpr = cpu_has_vmx_virtualize_apic_accesses,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002805
2806 .vcpu_create = vmx_create_vcpu,
2807 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002808 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002809
Avi Kivity04d2cc72007-09-10 18:10:54 +03002810 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 .vcpu_load = vmx_vcpu_load,
2812 .vcpu_put = vmx_vcpu_put,
Avi Kivity774c47f2007-02-12 00:54:47 -08002813 .vcpu_decache = vmx_vcpu_decache,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814
2815 .set_guest_debug = set_guest_debug,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002816 .guest_debug_pre = kvm_guest_debug_pre,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 .get_msr = vmx_get_msr,
2818 .set_msr = vmx_set_msr,
2819 .get_segment_base = vmx_get_segment_base,
2820 .get_segment = vmx_get_segment,
2821 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02002822 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03002824 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826 .set_cr3 = vmx_set_cr3,
2827 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 .get_idt = vmx_get_idt,
2830 .set_idt = vmx_set_idt,
2831 .get_gdt = vmx_get_gdt,
2832 .set_gdt = vmx_set_gdt,
2833 .cache_regs = vcpu_load_rsp_rip,
2834 .decache_regs = vcpu_put_rsp_rip,
2835 .get_rflags = vmx_get_rflags,
2836 .set_rflags = vmx_set_rflags,
2837
2838 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002839
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 .run = vmx_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002841 .handle_exit = kvm_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842 .skip_emulated_instruction = skip_emulated_instruction,
Ingo Molnar102d8322007-02-19 14:37:47 +02002843 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03002844 .get_irq = vmx_get_irq,
2845 .set_irq = vmx_inject_irq,
Avi Kivity298101d2007-11-25 13:41:11 +02002846 .queue_exception = vmx_queue_exception,
2847 .exception_injected = vmx_exception_injected,
Avi Kivity04d2cc72007-09-10 18:10:54 +03002848 .inject_pending_irq = vmx_intr_assist,
2849 .inject_pending_vectors = do_interrupt_requests,
Izik Eiduscbc94022007-10-25 00:29:55 +02002850
2851 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08002852 .get_tdp_level = get_ept_level,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853};
2854
2855static int __init vmx_init(void)
2856{
Sheng Yang25c5f222008-03-28 13:18:56 +08002857 void *va;
He, Qingfdef3ad2007-04-30 09:45:24 +03002858 int r;
2859
2860 vmx_io_bitmap_a = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2861 if (!vmx_io_bitmap_a)
2862 return -ENOMEM;
2863
2864 vmx_io_bitmap_b = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2865 if (!vmx_io_bitmap_b) {
2866 r = -ENOMEM;
2867 goto out;
2868 }
2869
Sheng Yang25c5f222008-03-28 13:18:56 +08002870 vmx_msr_bitmap = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2871 if (!vmx_msr_bitmap) {
2872 r = -ENOMEM;
2873 goto out1;
2874 }
2875
He, Qingfdef3ad2007-04-30 09:45:24 +03002876 /*
2877 * Allow direct access to the PC debug port (it is often used for I/O
2878 * delays, but the vmexits simply slow things down).
2879 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002880 va = kmap(vmx_io_bitmap_a);
2881 memset(va, 0xff, PAGE_SIZE);
2882 clear_bit(0x80, va);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002883 kunmap(vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03002884
Sheng Yang25c5f222008-03-28 13:18:56 +08002885 va = kmap(vmx_io_bitmap_b);
2886 memset(va, 0xff, PAGE_SIZE);
Avi Kivitycd0536d2007-05-08 11:34:07 +03002887 kunmap(vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03002888
Sheng Yang25c5f222008-03-28 13:18:56 +08002889 va = kmap(vmx_msr_bitmap);
2890 memset(va, 0xff, PAGE_SIZE);
2891 kunmap(vmx_msr_bitmap);
2892
Sheng Yang2384d2b2008-01-17 15:14:33 +08002893 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
2894
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002895 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03002896 if (r)
Sheng Yang25c5f222008-03-28 13:18:56 +08002897 goto out2;
2898
2899 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_FS_BASE);
2900 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_GS_BASE);
2901 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_CS);
2902 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_ESP);
2903 vmx_disable_intercept_for_msr(vmx_msr_bitmap, MSR_IA32_SYSENTER_EIP);
He, Qingfdef3ad2007-04-30 09:45:24 +03002904
Avi Kivityc7addb92007-09-16 18:58:32 +02002905 if (bypass_guest_pf)
2906 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
2907
He, Qingfdef3ad2007-04-30 09:45:24 +03002908 return 0;
2909
Sheng Yang25c5f222008-03-28 13:18:56 +08002910out2:
2911 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03002912out1:
2913 __free_page(vmx_io_bitmap_b);
2914out:
2915 __free_page(vmx_io_bitmap_a);
2916 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917}
2918
2919static void __exit vmx_exit(void)
2920{
Sheng Yang25c5f222008-03-28 13:18:56 +08002921 __free_page(vmx_msr_bitmap);
He, Qingfdef3ad2007-04-30 09:45:24 +03002922 __free_page(vmx_io_bitmap_b);
2923 __free_page(vmx_io_bitmap_a);
2924
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08002925 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926}
2927
2928module_init(vmx_init)
2929module_exit(vmx_exit)