blob: 4c3fa0f6746970cef9bb5d26e5808d8bbdabcb1f [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.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.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>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040031#include <linux/tboot.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030032#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030033#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040034
Avi Kivity6aa8b732006-12-10 02:21:36 -080035#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080036#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020037#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020038#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080039#include <asm/mce.h>
Dexuan Cui2acf9232010-06-10 11:27:12 +080040#include <asm/i387.h>
41#include <asm/xcr.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
Marcelo Tosatti229456f2009-06-17 09:22:14 -030043#include "trace.h"
44
Avi Kivity4ecac3f2008-05-13 13:23:38 +030045#define __ex(x) __kvm_handle_fault_on_reboot(x)
46
Avi Kivity6aa8b732006-12-10 02:21:36 -080047MODULE_AUTHOR("Qumranet");
48MODULE_LICENSE("GPL");
49
Avi Kivity4462d212009-03-23 17:53:37 +020050static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020051module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020052
Avi Kivity4462d212009-03-23 17:53:37 +020053static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020054module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080055
Avi Kivity4462d212009-03-23 17:53:37 +020056static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020057module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020058
Avi Kivity4462d212009-03-23 17:53:37 +020059static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020060module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080061
Nitin A Kamble3a624e22009-06-08 11:34:16 -070062static int __read_mostly enable_unrestricted_guest = 1;
63module_param_named(unrestricted_guest,
64 enable_unrestricted_guest, bool, S_IRUGO);
65
Avi Kivity4462d212009-03-23 17:53:37 +020066static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020067module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030068
Dongxiao Xub923e622010-05-11 18:29:45 +080069static int __read_mostly vmm_exclusive = 1;
70module_param(vmm_exclusive, bool, S_IRUGO);
71
Anthony Liguori443381a2010-12-06 10:53:38 -060072static int __read_mostly yield_on_hlt = 1;
73module_param(yield_on_hlt, bool, S_IRUGO);
74
Avi Kivitycdc0e242009-12-06 17:21:14 +020075#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
76 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
77#define KVM_GUEST_CR0_MASK \
78 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
79#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity81231c62010-01-24 16:26:40 +020080 (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +020081#define KVM_VM_CR0_ALWAYS_ON \
82 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +020083#define KVM_CR4_GUEST_OWNED_BITS \
84 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
85 | X86_CR4_OSXMMEXCPT)
86
Avi Kivitycdc0e242009-12-06 17:21:14 +020087#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
88#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
89
Avi Kivity78ac8b42010-04-08 18:19:35 +030090#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
91
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080092/*
93 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
94 * ple_gap: upper bound on the amount of time between two successive
95 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -050096 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080097 * ple_window: upper bound on the amount of time a guest is allowed to execute
98 * in a PAUSE loop. Tests indicate that most spinlocks are held for
99 * less than 2^12 cycles
100 * Time is measured based on a counter that runs at the same rate as the TSC,
101 * refer SDM volume 3b section 21.6.13 & 22.1.3.
102 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500103#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800104#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
105static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
106module_param(ple_gap, int, S_IRUGO);
107
108static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
109module_param(ple_window, int, S_IRUGO);
110
Avi Kivity61d2ef22010-04-28 16:40:38 +0300111#define NR_AUTOLOAD_MSRS 1
112
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400113struct vmcs {
114 u32 revision_id;
115 u32 abort;
116 char data[0];
117};
118
Avi Kivity26bb0982009-09-07 11:14:12 +0300119struct shared_msr_entry {
120 unsigned index;
121 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200122 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300123};
124
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400125struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000126 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +0300127 struct list_head local_vcpus_link;
Avi Kivity313dbd492008-07-17 18:04:30 +0300128 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400129 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300130 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200131 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200132 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300133 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200134 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200135 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300136 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400137 int nmsrs;
138 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400139#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300140 u64 msr_host_kernel_gs_base;
141 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400142#endif
143 struct vmcs *vmcs;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300144 struct msr_autoload {
145 unsigned nr;
146 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
147 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
148 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400149 struct {
150 int loaded;
151 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200152 int gs_ldt_reload_needed;
153 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400154 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200155 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300156 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300157 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300158 struct kvm_save_segment {
159 u16 selector;
160 unsigned long base;
161 u32 limit;
162 u32 ar;
163 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200164 } rmode;
Avi Kivity2fb92db2011-04-27 19:42:18 +0300165 struct {
166 u32 bitmask; /* 4 bits per segment (1 bit per field) */
167 struct kvm_save_segment seg[8];
168 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800169 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300170 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200171
172 /* Support for vnmi-less CPUs */
173 int soft_vnmi_blocked;
174 ktime_t entry_time;
175 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800176 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800177
178 bool rdtscp_enabled;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400179};
180
Avi Kivity2fb92db2011-04-27 19:42:18 +0300181enum segment_cache_field {
182 SEG_FIELD_SEL = 0,
183 SEG_FIELD_BASE = 1,
184 SEG_FIELD_LIMIT = 2,
185 SEG_FIELD_AR = 3,
186
187 SEG_FIELD_NR = 4
188};
189
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400190static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
191{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000192 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400193}
194
Sheng Yang4e1096d2008-07-06 19:16:51 +0800195static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800196static void kvm_cpu_vmxon(u64 addr);
197static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200198static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200199static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Avi Kivity75880a02007-06-20 11:20:04 +0300200
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201static DEFINE_PER_CPU(struct vmcs *, vmxarea);
202static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300203static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300204static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800205
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200206static unsigned long *vmx_io_bitmap_a;
207static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200208static unsigned long *vmx_msr_bitmap_legacy;
209static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300210
Avi Kivity110312c2010-12-21 12:54:20 +0200211static bool cpu_has_load_ia32_efer;
212
Sheng Yang2384d2b2008-01-17 15:14:33 +0800213static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
214static DEFINE_SPINLOCK(vmx_vpid_lock);
215
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300216static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 int size;
218 int order;
219 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300220 u32 pin_based_exec_ctrl;
221 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800222 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300223 u32 vmexit_ctrl;
224 u32 vmentry_ctrl;
225} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226
Hannes Ederefff9e52008-11-28 17:02:06 +0100227static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800228 u32 ept;
229 u32 vpid;
230} vmx_capability;
231
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232#define VMX_SEGMENT_FIELD(seg) \
233 [VCPU_SREG_##seg] = { \
234 .selector = GUEST_##seg##_SELECTOR, \
235 .base = GUEST_##seg##_BASE, \
236 .limit = GUEST_##seg##_LIMIT, \
237 .ar_bytes = GUEST_##seg##_AR_BYTES, \
238 }
239
240static struct kvm_vmx_segment_field {
241 unsigned selector;
242 unsigned base;
243 unsigned limit;
244 unsigned ar_bytes;
245} kvm_vmx_segment_fields[] = {
246 VMX_SEGMENT_FIELD(CS),
247 VMX_SEGMENT_FIELD(DS),
248 VMX_SEGMENT_FIELD(ES),
249 VMX_SEGMENT_FIELD(FS),
250 VMX_SEGMENT_FIELD(GS),
251 VMX_SEGMENT_FIELD(SS),
252 VMX_SEGMENT_FIELD(TR),
253 VMX_SEGMENT_FIELD(LDTR),
254};
255
Avi Kivity26bb0982009-09-07 11:14:12 +0300256static u64 host_efer;
257
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300258static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
259
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300260/*
Brian Gerst8c065852010-07-17 09:03:26 -0400261 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300262 * away by decrementing the array size.
263 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800265#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300266 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400268 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200270#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271
Gui Jianfeng31299942010-03-15 17:29:09 +0800272static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273{
274 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
275 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100276 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277}
278
Gui Jianfeng31299942010-03-15 17:29:09 +0800279static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300280{
281 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
282 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100283 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300284}
285
Gui Jianfeng31299942010-03-15 17:29:09 +0800286static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500287{
288 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
289 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100290 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500291}
292
Gui Jianfeng31299942010-03-15 17:29:09 +0800293static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294{
295 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
296 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
297}
298
Gui Jianfeng31299942010-03-15 17:29:09 +0800299static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800300{
301 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
302 INTR_INFO_VALID_MASK)) ==
303 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
304}
305
Gui Jianfeng31299942010-03-15 17:29:09 +0800306static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800307{
Sheng Yang04547152009-04-01 15:52:31 +0800308 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800309}
310
Gui Jianfeng31299942010-03-15 17:29:09 +0800311static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800312{
Sheng Yang04547152009-04-01 15:52:31 +0800313 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800314}
315
Gui Jianfeng31299942010-03-15 17:29:09 +0800316static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800317{
Sheng Yang04547152009-04-01 15:52:31 +0800318 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800319}
320
Gui Jianfeng31299942010-03-15 17:29:09 +0800321static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800322{
Sheng Yang04547152009-04-01 15:52:31 +0800323 return vmcs_config.cpu_based_exec_ctrl &
324 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800325}
326
Avi Kivity774ead32007-12-26 13:57:04 +0200327static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800328{
Sheng Yang04547152009-04-01 15:52:31 +0800329 return vmcs_config.cpu_based_2nd_exec_ctrl &
330 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
331}
332
333static inline bool cpu_has_vmx_flexpriority(void)
334{
335 return cpu_has_vmx_tpr_shadow() &&
336 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800337}
338
Marcelo Tosattie7997942009-06-11 12:07:40 -0300339static inline bool cpu_has_vmx_ept_execute_only(void)
340{
Gui Jianfeng31299942010-03-15 17:29:09 +0800341 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300342}
343
344static inline bool cpu_has_vmx_eptp_uncacheable(void)
345{
Gui Jianfeng31299942010-03-15 17:29:09 +0800346 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300347}
348
349static inline bool cpu_has_vmx_eptp_writeback(void)
350{
Gui Jianfeng31299942010-03-15 17:29:09 +0800351 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300352}
353
354static inline bool cpu_has_vmx_ept_2m_page(void)
355{
Gui Jianfeng31299942010-03-15 17:29:09 +0800356 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300357}
358
Sheng Yang878403b2010-01-05 19:02:29 +0800359static inline bool cpu_has_vmx_ept_1g_page(void)
360{
Gui Jianfeng31299942010-03-15 17:29:09 +0800361 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800362}
363
Sheng Yang4bc9b982010-06-02 14:05:24 +0800364static inline bool cpu_has_vmx_ept_4levels(void)
365{
366 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
367}
368
Gui Jianfeng31299942010-03-15 17:29:09 +0800369static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800370{
Gui Jianfeng31299942010-03-15 17:29:09 +0800371 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800372}
373
Gui Jianfeng31299942010-03-15 17:29:09 +0800374static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800375{
Gui Jianfeng31299942010-03-15 17:29:09 +0800376 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800377}
378
Gui Jianfeng31299942010-03-15 17:29:09 +0800379static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800380{
Gui Jianfeng31299942010-03-15 17:29:09 +0800381 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800382}
383
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800384static inline bool cpu_has_vmx_invvpid_single(void)
385{
386 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
387}
388
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800389static inline bool cpu_has_vmx_invvpid_global(void)
390{
391 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
392}
393
Gui Jianfeng31299942010-03-15 17:29:09 +0800394static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800395{
Sheng Yang04547152009-04-01 15:52:31 +0800396 return vmcs_config.cpu_based_2nd_exec_ctrl &
397 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800398}
399
Gui Jianfeng31299942010-03-15 17:29:09 +0800400static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700401{
402 return vmcs_config.cpu_based_2nd_exec_ctrl &
403 SECONDARY_EXEC_UNRESTRICTED_GUEST;
404}
405
Gui Jianfeng31299942010-03-15 17:29:09 +0800406static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800407{
408 return vmcs_config.cpu_based_2nd_exec_ctrl &
409 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
410}
411
Gui Jianfeng31299942010-03-15 17:29:09 +0800412static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800413{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800414 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800415}
416
Gui Jianfeng31299942010-03-15 17:29:09 +0800417static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800418{
Sheng Yang04547152009-04-01 15:52:31 +0800419 return vmcs_config.cpu_based_2nd_exec_ctrl &
420 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800421}
422
Gui Jianfeng31299942010-03-15 17:29:09 +0800423static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800424{
425 return vmcs_config.cpu_based_2nd_exec_ctrl &
426 SECONDARY_EXEC_RDTSCP;
427}
428
Gui Jianfeng31299942010-03-15 17:29:09 +0800429static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800430{
431 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
432}
433
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800434static inline bool cpu_has_vmx_wbinvd_exit(void)
435{
436 return vmcs_config.cpu_based_2nd_exec_ctrl &
437 SECONDARY_EXEC_WBINVD_EXITING;
438}
439
Sheng Yang04547152009-04-01 15:52:31 +0800440static inline bool report_flexpriority(void)
441{
442 return flexpriority_enabled;
443}
444
Rusty Russell8b9cf982007-07-30 16:31:43 +1000445static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800446{
447 int i;
448
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400449 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300450 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300451 return i;
452 return -1;
453}
454
Sheng Yang2384d2b2008-01-17 15:14:33 +0800455static inline void __invvpid(int ext, u16 vpid, gva_t gva)
456{
457 struct {
458 u64 vpid : 16;
459 u64 rsvd : 48;
460 u64 gva;
461 } operand = { vpid, 0, gva };
462
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300463 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800464 /* CF==1 or ZF==1 --> rc = -1 */
465 "; ja 1f ; ud2 ; 1:"
466 : : "a"(&operand), "c"(ext) : "cc", "memory");
467}
468
Sheng Yang14394422008-04-28 12:24:45 +0800469static inline void __invept(int ext, u64 eptp, gpa_t gpa)
470{
471 struct {
472 u64 eptp, gpa;
473 } operand = {eptp, gpa};
474
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300475 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800476 /* CF==1 or ZF==1 --> rc = -1 */
477 "; ja 1f ; ud2 ; 1:\n"
478 : : "a" (&operand), "c" (ext) : "cc", "memory");
479}
480
Avi Kivity26bb0982009-09-07 11:14:12 +0300481static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300482{
483 int i;
484
Rusty Russell8b9cf982007-07-30 16:31:43 +1000485 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300486 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400487 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000488 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800489}
490
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491static void vmcs_clear(struct vmcs *vmcs)
492{
493 u64 phys_addr = __pa(vmcs);
494 u8 error;
495
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300496 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200497 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 : "cc", "memory");
499 if (error)
500 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
501 vmcs, phys_addr);
502}
503
Dongxiao Xu7725b892010-05-11 18:29:38 +0800504static void vmcs_load(struct vmcs *vmcs)
505{
506 u64 phys_addr = __pa(vmcs);
507 u8 error;
508
509 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200510 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +0800511 : "cc", "memory");
512 if (error)
513 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
514 vmcs, phys_addr);
515}
516
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517static void __vcpu_clear(void *arg)
518{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000519 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800520 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521
Rusty Russell8b9cf982007-07-30 16:31:43 +1000522 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400523 vmcs_clear(vmx->vmcs);
524 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800525 per_cpu(current_vmcs, cpu) = NULL;
Avi Kivity543e4242008-05-13 16:22:47 +0300526 list_del(&vmx->local_vcpus_link);
527 vmx->vcpu.cpu = -1;
528 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529}
530
Rusty Russell8b9cf982007-07-30 16:31:43 +1000531static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800532{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200533 if (vmx->vcpu.cpu == -1)
534 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200535 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800536}
537
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800538static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800539{
540 if (vmx->vpid == 0)
541 return;
542
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800543 if (cpu_has_vmx_invvpid_single())
544 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800545}
546
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800547static inline void vpid_sync_vcpu_global(void)
548{
549 if (cpu_has_vmx_invvpid_global())
550 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
551}
552
553static inline void vpid_sync_context(struct vcpu_vmx *vmx)
554{
555 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800556 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800557 else
558 vpid_sync_vcpu_global();
559}
560
Sheng Yang14394422008-04-28 12:24:45 +0800561static inline void ept_sync_global(void)
562{
563 if (cpu_has_vmx_invept_global())
564 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
565}
566
567static inline void ept_sync_context(u64 eptp)
568{
Avi Kivity089d0342009-03-23 18:26:32 +0200569 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800570 if (cpu_has_vmx_invept_context())
571 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
572 else
573 ept_sync_global();
574 }
575}
576
577static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
578{
Avi Kivity089d0342009-03-23 18:26:32 +0200579 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800580 if (cpu_has_vmx_invept_individual_addr())
581 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
582 eptp, gpa);
583 else
584 ept_sync_context(eptp);
585 }
586}
587
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588static unsigned long vmcs_readl(unsigned long field)
589{
Avi Kivitya2956732010-12-02 17:55:23 +0200590 unsigned long value = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300592 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivitya2956732010-12-02 17:55:23 +0200593 : "+a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -0800594 return value;
595}
596
597static u16 vmcs_read16(unsigned long field)
598{
599 return vmcs_readl(field);
600}
601
602static u32 vmcs_read32(unsigned long field)
603{
604 return vmcs_readl(field);
605}
606
607static u64 vmcs_read64(unsigned long field)
608{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800609#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800610 return vmcs_readl(field);
611#else
612 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
613#endif
614}
615
Avi Kivitye52de1b2007-01-05 16:36:56 -0800616static noinline void vmwrite_error(unsigned long field, unsigned long value)
617{
618 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
619 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
620 dump_stack();
621}
622
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623static void vmcs_writel(unsigned long field, unsigned long value)
624{
625 u8 error;
626
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300627 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400628 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800629 if (unlikely(error))
630 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631}
632
633static void vmcs_write16(unsigned long field, u16 value)
634{
635 vmcs_writel(field, value);
636}
637
638static void vmcs_write32(unsigned long field, u32 value)
639{
640 vmcs_writel(field, value);
641}
642
643static void vmcs_write64(unsigned long field, u64 value)
644{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300646#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 asm volatile ("");
648 vmcs_writel(field+1, value >> 32);
649#endif
650}
651
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300652static void vmcs_clear_bits(unsigned long field, u32 mask)
653{
654 vmcs_writel(field, vmcs_readl(field) & ~mask);
655}
656
657static void vmcs_set_bits(unsigned long field, u32 mask)
658{
659 vmcs_writel(field, vmcs_readl(field) | mask);
660}
661
Avi Kivity2fb92db2011-04-27 19:42:18 +0300662static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
663{
664 vmx->segment_cache.bitmask = 0;
665}
666
667static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
668 unsigned field)
669{
670 bool ret;
671 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
672
673 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
674 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
675 vmx->segment_cache.bitmask = 0;
676 }
677 ret = vmx->segment_cache.bitmask & mask;
678 vmx->segment_cache.bitmask |= mask;
679 return ret;
680}
681
682static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
683{
684 u16 *p = &vmx->segment_cache.seg[seg].selector;
685
686 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
687 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
688 return *p;
689}
690
691static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
692{
693 ulong *p = &vmx->segment_cache.seg[seg].base;
694
695 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
696 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
697 return *p;
698}
699
700static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
701{
702 u32 *p = &vmx->segment_cache.seg[seg].limit;
703
704 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
705 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
706 return *p;
707}
708
709static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
710{
711 u32 *p = &vmx->segment_cache.seg[seg].ar;
712
713 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
714 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
715 return *p;
716}
717
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300718static void update_exception_bitmap(struct kvm_vcpu *vcpu)
719{
720 u32 eb;
721
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100722 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
723 (1u << NM_VECTOR) | (1u << DB_VECTOR);
724 if ((vcpu->guest_debug &
725 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
726 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
727 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300728 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300729 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200730 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800731 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200732 if (vcpu->fpu_active)
733 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300734 vmcs_write32(EXCEPTION_BITMAP, eb);
735}
736
Avi Kivity61d2ef22010-04-28 16:40:38 +0300737static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
738{
739 unsigned i;
740 struct msr_autoload *m = &vmx->msr_autoload;
741
Avi Kivity110312c2010-12-21 12:54:20 +0200742 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
743 vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
744 vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
745 return;
746 }
747
Avi Kivity61d2ef22010-04-28 16:40:38 +0300748 for (i = 0; i < m->nr; ++i)
749 if (m->guest[i].index == msr)
750 break;
751
752 if (i == m->nr)
753 return;
754 --m->nr;
755 m->guest[i] = m->guest[m->nr];
756 m->host[i] = m->host[m->nr];
757 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
758 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
759}
760
761static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
762 u64 guest_val, u64 host_val)
763{
764 unsigned i;
765 struct msr_autoload *m = &vmx->msr_autoload;
766
Avi Kivity110312c2010-12-21 12:54:20 +0200767 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
768 vmcs_write64(GUEST_IA32_EFER, guest_val);
769 vmcs_write64(HOST_IA32_EFER, host_val);
770 vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
771 vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
772 return;
773 }
774
Avi Kivity61d2ef22010-04-28 16:40:38 +0300775 for (i = 0; i < m->nr; ++i)
776 if (m->guest[i].index == msr)
777 break;
778
779 if (i == m->nr) {
780 ++m->nr;
781 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
782 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
783 }
784
785 m->guest[i].index = msr;
786 m->guest[i].value = guest_val;
787 m->host[i].index = msr;
788 m->host[i].value = host_val;
789}
790
Avi Kivity33ed6322007-05-02 16:54:03 +0300791static void reload_tss(void)
792{
Avi Kivity33ed6322007-05-02 16:54:03 +0300793 /*
794 * VT restores TR but not its size. Useless.
795 */
Avi Kivityd3591922010-07-26 18:32:39 +0300796 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +0200797 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300798
Avi Kivityd3591922010-07-26 18:32:39 +0300799 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +0300800 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
801 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300802}
803
Avi Kivity92c0d902009-10-29 11:00:16 +0200804static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300805{
Roel Kluin3a34a882009-08-04 02:08:45 -0700806 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300807 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300808
Avi Kivityf6801df2010-01-21 15:31:50 +0200809 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700810
Avi Kivity51c6cf62007-08-29 03:48:05 +0300811 /*
812 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
813 * outside long mode
814 */
815 ignore_bits = EFER_NX | EFER_SCE;
816#ifdef CONFIG_X86_64
817 ignore_bits |= EFER_LMA | EFER_LME;
818 /* SCE is meaningful only in long mode on Intel */
819 if (guest_efer & EFER_LMA)
820 ignore_bits &= ~(u64)EFER_SCE;
821#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300822 guest_efer &= ~ignore_bits;
823 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300824 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200825 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300826
827 clear_atomic_switch_msr(vmx, MSR_EFER);
828 /* On ept, can't emulate nx, and must switch nx atomically */
829 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
830 guest_efer = vmx->vcpu.arch.efer;
831 if (!(guest_efer & EFER_LMA))
832 guest_efer &= ~EFER_LME;
833 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
834 return false;
835 }
836
Avi Kivity26bb0982009-09-07 11:14:12 +0300837 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300838}
839
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200840static unsigned long segment_base(u16 selector)
841{
Avi Kivityd3591922010-07-26 18:32:39 +0300842 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200843 struct desc_struct *d;
844 unsigned long table_base;
845 unsigned long v;
846
847 if (!(selector & ~3))
848 return 0;
849
Avi Kivityd3591922010-07-26 18:32:39 +0300850 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200851
852 if (selector & 4) { /* from ldt */
853 u16 ldt_selector = kvm_read_ldt();
854
855 if (!(ldt_selector & ~3))
856 return 0;
857
858 table_base = segment_base(ldt_selector);
859 }
860 d = (struct desc_struct *)(table_base + (selector & ~7));
861 v = get_desc_base(d);
862#ifdef CONFIG_X86_64
863 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
864 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
865#endif
866 return v;
867}
868
869static inline unsigned long kvm_read_tr_base(void)
870{
871 u16 tr;
872 asm("str %0" : "=g"(tr));
873 return segment_base(tr);
874}
875
Avi Kivity04d2cc72007-09-10 18:10:54 +0300876static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300877{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300878 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300879 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300880
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400881 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300882 return;
883
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400884 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300885 /*
886 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
887 * allow segment selectors with cpl > 0 or ti == 1.
888 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300889 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200890 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +0200891 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200892 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400893 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200894 vmx->host_state.fs_reload_needed = 0;
895 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300896 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200897 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300898 }
Avi Kivity9581d442010-10-19 16:46:55 +0200899 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400900 if (!(vmx->host_state.gs_sel & 7))
901 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300902 else {
903 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200904 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300905 }
906
907#ifdef CONFIG_X86_64
908 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
909 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
910#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400911 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
912 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300913#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300914
915#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +0200916 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
917 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +0300918 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +0300919#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300920 for (i = 0; i < vmx->save_nmsrs; ++i)
921 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200922 vmx->guest_msrs[i].data,
923 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300924}
925
Avi Kivitya9b21b62008-06-24 11:48:49 +0300926static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300927{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400928 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300929 return;
930
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200931 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400932 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +0200933#ifdef CONFIG_X86_64
934 if (is_long_mode(&vmx->vcpu))
935 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
936#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200937 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300938 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300939#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +0200940 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +0200941#else
942 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300943#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300944 }
Avi Kivity0a77fe42010-10-19 18:48:35 +0200945 if (vmx->host_state.fs_reload_needed)
946 loadsegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200947 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300948#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +0200949 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +0300950#endif
Avi Kivity1c11e712010-05-03 16:05:44 +0300951 if (current_thread_info()->status & TS_USEDFPU)
952 clts();
Avi Kivity3444d7d2010-07-26 18:32:38 +0300953 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +0300954}
955
Avi Kivitya9b21b62008-06-24 11:48:49 +0300956static void vmx_load_host_state(struct vcpu_vmx *vmx)
957{
958 preempt_disable();
959 __vmx_load_host_state(vmx);
960 preempt_enable();
961}
962
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963/*
964 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
965 * vcpu mutex is already taken.
966 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300967static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800970 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800971
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800972 if (!vmm_exclusive)
973 kvm_cpu_vmxon(phys_addr);
974 else if (vcpu->cpu != cpu)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000975 vcpu_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400977 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400978 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Dongxiao Xu7725b892010-05-11 18:29:38 +0800979 vmcs_load(vmx->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 }
981
982 if (vcpu->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +0300983 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 unsigned long sysenter_esp;
985
Avi Kivitya8eeb042010-05-10 12:34:53 +0300986 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +0800987 local_irq_disable();
988 list_add(&vmx->local_vcpus_link,
989 &per_cpu(vcpus_on_cpu, cpu));
990 local_irq_enable();
991
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992 /*
993 * Linux uses per-cpu TSS and GDT, so set these when switching
994 * processors.
995 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300996 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +0300997 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800998
999 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1000 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
1001 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001002}
1003
1004static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1005{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001006 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001007 if (!vmm_exclusive) {
Dongxiao Xub923e622010-05-11 18:29:45 +08001008 __vcpu_clear(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001009 kvm_cpu_vmxoff();
1010 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011}
1012
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001013static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1014{
Avi Kivity81231c62010-01-24 16:26:40 +02001015 ulong cr0;
1016
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001017 if (vcpu->fpu_active)
1018 return;
1019 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001020 cr0 = vmcs_readl(GUEST_CR0);
1021 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1022 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1023 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001024 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001025 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
1026 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001027}
1028
Avi Kivityedcafe32009-12-30 18:07:40 +02001029static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1030
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001031static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1032{
Avi Kivityedcafe32009-12-30 18:07:40 +02001033 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001034 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001035 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001036 vcpu->arch.cr0_guest_owned_bits = 0;
1037 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1038 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001039}
1040
Avi Kivity6aa8b732006-12-10 02:21:36 -08001041static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1042{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001043 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001044
Avi Kivity6de12732011-03-07 12:51:22 +02001045 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1046 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1047 rflags = vmcs_readl(GUEST_RFLAGS);
1048 if (to_vmx(vcpu)->rmode.vm86_active) {
1049 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1050 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1051 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1052 }
1053 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001054 }
Avi Kivity6de12732011-03-07 12:51:22 +02001055 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056}
1057
1058static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1059{
Avi Kivity6de12732011-03-07 12:51:22 +02001060 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity69c73022011-03-07 15:26:44 +02001061 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6de12732011-03-07 12:51:22 +02001062 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001063 if (to_vmx(vcpu)->rmode.vm86_active) {
1064 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001065 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001066 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 vmcs_writel(GUEST_RFLAGS, rflags);
1068}
1069
Glauber Costa2809f5d2009-05-12 16:21:05 -04001070static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1071{
1072 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1073 int ret = 0;
1074
1075 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001076 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001077 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001078 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001079
1080 return ret & mask;
1081}
1082
1083static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1084{
1085 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1086 u32 interruptibility = interruptibility_old;
1087
1088 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1089
Jan Kiszka48005f62010-02-19 19:38:07 +01001090 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001091 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001092 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001093 interruptibility |= GUEST_INTR_STATE_STI;
1094
1095 if ((interruptibility != interruptibility_old))
1096 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1097}
1098
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1100{
1101 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001103 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001105 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106
Glauber Costa2809f5d2009-05-12 16:21:05 -04001107 /* skipping an emulated instruction also counts */
1108 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109}
1110
Anthony Liguori443381a2010-12-06 10:53:38 -06001111static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1112{
1113 /* Ensure that we clear the HLT state in the VMCS. We don't need to
1114 * explicitly skip the instruction because if the HLT state is set, then
1115 * the instruction is already executing and RIP has already been
1116 * advanced. */
1117 if (!yield_on_hlt &&
1118 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1119 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1120}
1121
Avi Kivity298101d2007-11-25 13:41:11 +02001122static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001123 bool has_error_code, u32 error_code,
1124 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001125{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001126 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001127 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001128
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001129 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001130 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001131 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1132 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001133
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001134 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001135 int inc_eip = 0;
1136 if (kvm_exception_is_soft(nr))
1137 inc_eip = vcpu->arch.event_exit_inst_len;
1138 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001139 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001140 return;
1141 }
1142
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001143 if (kvm_exception_is_soft(nr)) {
1144 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1145 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001146 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1147 } else
1148 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1149
1150 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Anthony Liguori443381a2010-12-06 10:53:38 -06001151 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001152}
1153
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001154static bool vmx_rdtscp_supported(void)
1155{
1156 return cpu_has_vmx_rdtscp();
1157}
1158
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159/*
Eddie Donga75beee2007-05-17 18:55:15 +03001160 * Swap MSR entry in host/guest MSR entry array.
1161 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001162static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001163{
Avi Kivity26bb0982009-09-07 11:14:12 +03001164 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001165
1166 tmp = vmx->guest_msrs[to];
1167 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1168 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001169}
1170
1171/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001172 * Set up the vmcs to automatically save and restore system
1173 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1174 * mode, as fiddling with msrs is very expensive.
1175 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001176static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001177{
Avi Kivity26bb0982009-09-07 11:14:12 +03001178 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +02001179 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +03001180
Avi Kivity33f9c502008-02-27 16:06:57 +02001181 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +03001182 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001183#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001184 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001185 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001186 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001187 move_msr_up(vmx, index, save_nmsrs++);
1188 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001189 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001190 move_msr_up(vmx, index, save_nmsrs++);
1191 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001192 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001193 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001194 index = __find_msr_index(vmx, MSR_TSC_AUX);
1195 if (index >= 0 && vmx->rdtscp_enabled)
1196 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001197 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001198 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001199 * if efer.sce is enabled.
1200 */
Brian Gerst8c065852010-07-17 09:03:26 -04001201 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001202 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001203 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001204 }
Eddie Donga75beee2007-05-17 18:55:15 +03001205#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001206 index = __find_msr_index(vmx, MSR_EFER);
1207 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001208 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001209
Avi Kivity26bb0982009-09-07 11:14:12 +03001210 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001211
1212 if (cpu_has_vmx_msr_bitmap()) {
1213 if (is_long_mode(&vmx->vcpu))
1214 msr_bitmap = vmx_msr_bitmap_longmode;
1215 else
1216 msr_bitmap = vmx_msr_bitmap_legacy;
1217
1218 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1219 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001220}
1221
1222/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223 * reads and returns guest's timestamp counter "register"
1224 * guest_tsc = host_tsc + tsc_offset -- 21.3
1225 */
1226static u64 guest_read_tsc(void)
1227{
1228 u64 host_tsc, tsc_offset;
1229
1230 rdtscll(host_tsc);
1231 tsc_offset = vmcs_read64(TSC_OFFSET);
1232 return host_tsc + tsc_offset;
1233}
1234
1235/*
Joerg Roedel4051b182011-03-25 09:44:49 +01001236 * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
1237 * ioctl. In this case the call-back should update internal vmx state to make
1238 * the changes effective.
1239 */
1240static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
1241{
1242 /* Nothing to do here */
1243}
1244
1245/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001246 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001248static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249{
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001250 vmcs_write64(TSC_OFFSET, offset);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251}
1252
Zachary Amsdene48672f2010-08-19 22:07:23 -10001253static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
1254{
1255 u64 offset = vmcs_read64(TSC_OFFSET);
1256 vmcs_write64(TSC_OFFSET, offset + adjustment);
1257}
1258
Joerg Roedel857e4092011-03-25 09:44:50 +01001259static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1260{
1261 return target_tsc - native_read_tsc();
1262}
1263
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264/*
1265 * Reads an msr value (of 'msr_index') into 'pdata'.
1266 * Returns 0 on success, non-0 otherwise.
1267 * Assumes vcpu_load() was already called.
1268 */
1269static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1270{
1271 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001272 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273
1274 if (!pdata) {
1275 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1276 return -EINVAL;
1277 }
1278
1279 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001280#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281 case MSR_FS_BASE:
1282 data = vmcs_readl(GUEST_FS_BASE);
1283 break;
1284 case MSR_GS_BASE:
1285 data = vmcs_readl(GUEST_GS_BASE);
1286 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001287 case MSR_KERNEL_GS_BASE:
1288 vmx_load_host_state(to_vmx(vcpu));
1289 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1290 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001291#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001293 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301294 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295 data = guest_read_tsc();
1296 break;
1297 case MSR_IA32_SYSENTER_CS:
1298 data = vmcs_read32(GUEST_SYSENTER_CS);
1299 break;
1300 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001301 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 break;
1303 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001304 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001306 case MSR_TSC_AUX:
1307 if (!to_vmx(vcpu)->rdtscp_enabled)
1308 return 1;
1309 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001311 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001312 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001313 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001314 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001315 data = msr->data;
1316 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001317 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001318 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319 }
1320
1321 *pdata = data;
1322 return 0;
1323}
1324
1325/*
1326 * Writes msr value into into the appropriate "register".
1327 * Returns 0 on success, non-0 otherwise.
1328 * Assumes vcpu_load() was already called.
1329 */
1330static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1331{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001332 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001333 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001334 int ret = 0;
1335
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001337 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001338 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001339 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001340 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001341#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001343 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344 vmcs_writel(GUEST_FS_BASE, data);
1345 break;
1346 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001347 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 vmcs_writel(GUEST_GS_BASE, data);
1349 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001350 case MSR_KERNEL_GS_BASE:
1351 vmx_load_host_state(vmx);
1352 vmx->msr_guest_kernel_gs_base = data;
1353 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354#endif
1355 case MSR_IA32_SYSENTER_CS:
1356 vmcs_write32(GUEST_SYSENTER_CS, data);
1357 break;
1358 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001359 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 break;
1361 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001362 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001363 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301364 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10001365 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001366 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001367 case MSR_IA32_CR_PAT:
1368 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1369 vmcs_write64(GUEST_IA32_PAT, data);
1370 vcpu->arch.pat = data;
1371 break;
1372 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001373 ret = kvm_set_msr_common(vcpu, msr_index, data);
1374 break;
1375 case MSR_TSC_AUX:
1376 if (!vmx->rdtscp_enabled)
1377 return 1;
1378 /* Check reserved bit, higher 32 bits should be zero */
1379 if ((data >> 32) != 0)
1380 return 1;
1381 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001382 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001383 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001384 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001385 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001386 msr->data = data;
1387 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001389 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 }
1391
Eddie Dong2cc51562007-05-21 07:28:09 +03001392 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393}
1394
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001395static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001396{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001397 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1398 switch (reg) {
1399 case VCPU_REGS_RSP:
1400 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1401 break;
1402 case VCPU_REGS_RIP:
1403 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1404 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001405 case VCPU_EXREG_PDPTR:
1406 if (enable_ept)
1407 ept_save_pdptrs(vcpu);
1408 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001409 default:
1410 break;
1411 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412}
1413
Jan Kiszka355be0b2009-10-03 00:31:21 +02001414static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001415{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001416 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1417 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1418 else
1419 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1420
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001421 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422}
1423
1424static __init int cpu_has_kvm_support(void)
1425{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001426 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427}
1428
1429static __init int vmx_disabled_by_bios(void)
1430{
1431 u64 msr;
1432
1433 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001434 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001435 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001436 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1437 && tboot_enabled())
1438 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001439 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001440 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001441 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001442 && !tboot_enabled()) {
1443 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001444 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001445 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001446 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001447 /* launched w/o TXT and VMX disabled */
1448 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1449 && !tboot_enabled())
1450 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001451 }
1452
1453 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454}
1455
Dongxiao Xu7725b892010-05-11 18:29:38 +08001456static void kvm_cpu_vmxon(u64 addr)
1457{
1458 asm volatile (ASM_VMX_VMXON_RAX
1459 : : "a"(&addr), "m"(addr)
1460 : "memory", "cc");
1461}
1462
Alexander Graf10474ae2009-09-15 11:37:46 +02001463static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001464{
1465 int cpu = raw_smp_processor_id();
1466 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001467 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468
Alexander Graf10474ae2009-09-15 11:37:46 +02001469 if (read_cr4() & X86_CR4_VMXE)
1470 return -EBUSY;
1471
Avi Kivity543e4242008-05-13 16:22:47 +03001472 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001474
1475 test_bits = FEATURE_CONTROL_LOCKED;
1476 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1477 if (tboot_enabled())
1478 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1479
1480 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001481 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001482 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1483 }
Rusty Russell66aee912007-07-17 23:34:16 +10001484 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02001485
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001486 if (vmm_exclusive) {
1487 kvm_cpu_vmxon(phys_addr);
1488 ept_sync_global();
1489 }
Alexander Graf10474ae2009-09-15 11:37:46 +02001490
Avi Kivity3444d7d2010-07-26 18:32:38 +03001491 store_gdt(&__get_cpu_var(host_gdt));
1492
Alexander Graf10474ae2009-09-15 11:37:46 +02001493 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494}
1495
Avi Kivity543e4242008-05-13 16:22:47 +03001496static void vmclear_local_vcpus(void)
1497{
1498 int cpu = raw_smp_processor_id();
1499 struct vcpu_vmx *vmx, *n;
1500
1501 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1502 local_vcpus_link)
1503 __vcpu_clear(vmx);
1504}
1505
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001506
1507/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1508 * tricks.
1509 */
1510static void kvm_cpu_vmxoff(void)
1511{
1512 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001513}
1514
Avi Kivity6aa8b732006-12-10 02:21:36 -08001515static void hardware_disable(void *garbage)
1516{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001517 if (vmm_exclusive) {
1518 vmclear_local_vcpus();
1519 kvm_cpu_vmxoff();
1520 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08001521 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522}
1523
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001524static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001525 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001526{
1527 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001528 u32 ctl = ctl_min | ctl_opt;
1529
1530 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1531
1532 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1533 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1534
1535 /* Ensure minimum (required) set of control bits are supported. */
1536 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001537 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001538
1539 *result = ctl;
1540 return 0;
1541}
1542
Avi Kivity110312c2010-12-21 12:54:20 +02001543static __init bool allow_1_setting(u32 msr, u32 ctl)
1544{
1545 u32 vmx_msr_low, vmx_msr_high;
1546
1547 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1548 return vmx_msr_high & ctl;
1549}
1550
Yang, Sheng002c7f72007-07-31 14:23:01 +03001551static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001552{
1553 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001554 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001555 u32 _pin_based_exec_control = 0;
1556 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001557 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001558 u32 _vmexit_control = 0;
1559 u32 _vmentry_control = 0;
1560
1561 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001562 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001563 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1564 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001565 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001566
Anthony Liguori443381a2010-12-06 10:53:38 -06001567 min =
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001568#ifdef CONFIG_X86_64
1569 CPU_BASED_CR8_LOAD_EXITING |
1570 CPU_BASED_CR8_STORE_EXITING |
1571#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001572 CPU_BASED_CR3_LOAD_EXITING |
1573 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001574 CPU_BASED_USE_IO_BITMAPS |
1575 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001576 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001577 CPU_BASED_MWAIT_EXITING |
1578 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001579 CPU_BASED_INVLPG_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001580
1581 if (yield_on_hlt)
1582 min |= CPU_BASED_HLT_EXITING;
1583
Sheng Yangf78e0e22007-10-29 09:40:42 +08001584 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001585 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001586 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001587 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1588 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001589 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001590#ifdef CONFIG_X86_64
1591 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1592 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1593 ~CPU_BASED_CR8_STORE_EXITING;
1594#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001595 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001596 min2 = 0;
1597 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001598 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001599 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001600 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001601 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001602 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1603 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001604 if (adjust_vmx_controls(min2, opt2,
1605 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001606 &_cpu_based_2nd_exec_control) < 0)
1607 return -EIO;
1608 }
1609#ifndef CONFIG_X86_64
1610 if (!(_cpu_based_2nd_exec_control &
1611 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1612 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1613#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001614 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001615 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1616 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001617 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1618 CPU_BASED_CR3_STORE_EXITING |
1619 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001620 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1621 vmx_capability.ept, vmx_capability.vpid);
1622 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001623
1624 min = 0;
1625#ifdef CONFIG_X86_64
1626 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1627#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001628 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001629 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1630 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001631 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001632
Sheng Yang468d4722008-10-09 16:01:55 +08001633 min = 0;
1634 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001635 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1636 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001637 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001638
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001639 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001640
1641 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1642 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001643 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001644
1645#ifdef CONFIG_X86_64
1646 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1647 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001648 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001649#endif
1650
1651 /* Require Write-Back (WB) memory type for VMCS accesses. */
1652 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001653 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001654
Yang, Sheng002c7f72007-07-31 14:23:01 +03001655 vmcs_conf->size = vmx_msr_high & 0x1fff;
1656 vmcs_conf->order = get_order(vmcs_config.size);
1657 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001658
Yang, Sheng002c7f72007-07-31 14:23:01 +03001659 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1660 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001661 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001662 vmcs_conf->vmexit_ctrl = _vmexit_control;
1663 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001664
Avi Kivity110312c2010-12-21 12:54:20 +02001665 cpu_has_load_ia32_efer =
1666 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
1667 VM_ENTRY_LOAD_IA32_EFER)
1668 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
1669 VM_EXIT_LOAD_IA32_EFER);
1670
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001671 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001672}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673
1674static struct vmcs *alloc_vmcs_cpu(int cpu)
1675{
1676 int node = cpu_to_node(cpu);
1677 struct page *pages;
1678 struct vmcs *vmcs;
1679
Mel Gorman6484eb32009-06-16 15:31:54 -07001680 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681 if (!pages)
1682 return NULL;
1683 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001684 memset(vmcs, 0, vmcs_config.size);
1685 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686 return vmcs;
1687}
1688
1689static struct vmcs *alloc_vmcs(void)
1690{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001691 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692}
1693
1694static void free_vmcs(struct vmcs *vmcs)
1695{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001696 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697}
1698
Sam Ravnborg39959582007-06-01 00:47:13 -07001699static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700{
1701 int cpu;
1702
Zachary Amsden3230bb42009-09-29 11:38:37 -10001703 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001705 per_cpu(vmxarea, cpu) = NULL;
1706 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707}
1708
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709static __init int alloc_kvm_area(void)
1710{
1711 int cpu;
1712
Zachary Amsden3230bb42009-09-29 11:38:37 -10001713 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714 struct vmcs *vmcs;
1715
1716 vmcs = alloc_vmcs_cpu(cpu);
1717 if (!vmcs) {
1718 free_kvm_area();
1719 return -ENOMEM;
1720 }
1721
1722 per_cpu(vmxarea, cpu) = vmcs;
1723 }
1724 return 0;
1725}
1726
1727static __init int hardware_setup(void)
1728{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001729 if (setup_vmcs_config(&vmcs_config) < 0)
1730 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001731
1732 if (boot_cpu_has(X86_FEATURE_NX))
1733 kvm_enable_efer_bits(EFER_NX);
1734
Sheng Yang93ba03c2009-04-01 15:52:32 +08001735 if (!cpu_has_vmx_vpid())
1736 enable_vpid = 0;
1737
Sheng Yang4bc9b982010-06-02 14:05:24 +08001738 if (!cpu_has_vmx_ept() ||
1739 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001740 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001741 enable_unrestricted_guest = 0;
1742 }
1743
1744 if (!cpu_has_vmx_unrestricted_guest())
1745 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001746
1747 if (!cpu_has_vmx_flexpriority())
1748 flexpriority_enabled = 0;
1749
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001750 if (!cpu_has_vmx_tpr_shadow())
1751 kvm_x86_ops->update_cr8_intercept = NULL;
1752
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001753 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1754 kvm_disable_largepages();
1755
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001756 if (!cpu_has_vmx_ple())
1757 ple_gap = 0;
1758
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 return alloc_kvm_area();
1760}
1761
1762static __exit void hardware_unsetup(void)
1763{
1764 free_kvm_area();
1765}
1766
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1768{
1769 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1770
Avi Kivity6af11b92007-03-19 13:18:10 +02001771 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772 vmcs_write16(sf->selector, save->selector);
1773 vmcs_writel(sf->base, save->base);
1774 vmcs_write32(sf->limit, save->limit);
1775 vmcs_write32(sf->ar_bytes, save->ar);
1776 } else {
1777 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1778 << AR_DPL_SHIFT;
1779 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1780 }
1781}
1782
1783static void enter_pmode(struct kvm_vcpu *vcpu)
1784{
1785 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001786 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001787
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001788 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001789 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790
Avi Kivity2fb92db2011-04-27 19:42:18 +03001791 vmx_segment_cache_clear(vmx);
1792
Avi Kivityd0ba64f2011-01-03 14:28:51 +02001793 vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001794 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1795 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1796 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001797
1798 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001799 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1800 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801 vmcs_writel(GUEST_RFLAGS, flags);
1802
Rusty Russell66aee912007-07-17 23:34:16 +10001803 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1804 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805
1806 update_exception_bitmap(vcpu);
1807
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001808 if (emulate_invalid_guest_state)
1809 return;
1810
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001811 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1812 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1813 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1814 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815
Avi Kivity2fb92db2011-04-27 19:42:18 +03001816 vmx_segment_cache_clear(vmx);
1817
Avi Kivity6aa8b732006-12-10 02:21:36 -08001818 vmcs_write16(GUEST_SS_SELECTOR, 0);
1819 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1820
1821 vmcs_write16(GUEST_CS_SELECTOR,
1822 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1823 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1824}
1825
Mike Dayd77c26f2007-10-08 09:02:08 -04001826static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001828 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001829 struct kvm_memslots *slots;
1830 gfn_t base_gfn;
1831
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001832 slots = kvm_memslots(kvm);
Avi Kivityf495c6e2010-06-10 17:21:29 +03001833 base_gfn = slots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001834 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02001835 return base_gfn << PAGE_SHIFT;
1836 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001837 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001838}
1839
1840static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1841{
1842 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1843
1844 save->selector = vmcs_read16(sf->selector);
1845 save->base = vmcs_readl(sf->base);
1846 save->limit = vmcs_read32(sf->limit);
1847 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001848 vmcs_write16(sf->selector, save->base >> 4);
Gleb Natapov444e8632010-12-27 17:25:04 +02001849 vmcs_write32(sf->base, save->base & 0xffff0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 vmcs_write32(sf->limit, 0xffff);
1851 vmcs_write32(sf->ar_bytes, 0xf3);
Gleb Natapov444e8632010-12-27 17:25:04 +02001852 if (save->base & 0xf)
1853 printk_once(KERN_WARNING "kvm: segment base is not paragraph"
1854 " aligned when entering protected mode (seg=%d)",
1855 seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856}
1857
1858static void enter_rmode(struct kvm_vcpu *vcpu)
1859{
1860 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001861 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001862
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001863 if (enable_unrestricted_guest)
1864 return;
1865
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001866 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001867 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001868
Gleb Natapov776e58e2011-03-13 12:34:27 +02001869 /*
1870 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
1871 * vcpu. Call it here with phys address pointing 16M below 4G.
1872 */
1873 if (!vcpu->kvm->arch.tss_addr) {
1874 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
1875 "called before entering vcpu\n");
1876 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
1877 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
1878 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1879 }
1880
Avi Kivity2fb92db2011-04-27 19:42:18 +03001881 vmx_segment_cache_clear(vmx);
1882
Avi Kivityd0ba64f2011-01-03 14:28:51 +02001883 vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001884 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1886
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001887 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001888 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1889
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001890 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1892
1893 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001894 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001895
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001896 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897
1898 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001899 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001900 update_exception_bitmap(vcpu);
1901
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001902 if (emulate_invalid_guest_state)
1903 goto continue_rmode;
1904
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1906 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1907 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1908
1909 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001910 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001911 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1912 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001913 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1914
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001915 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1916 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1917 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1918 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001919
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001920continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001921 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922}
1923
Amit Shah401d10d2009-02-20 22:53:37 +05301924static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1925{
1926 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001927 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1928
1929 if (!msr)
1930 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301931
Avi Kivity44ea2b12009-09-06 15:55:37 +03001932 /*
1933 * Force kernel_gs_base reloading before EFER changes, as control
1934 * of this msr depends on is_long_mode().
1935 */
1936 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02001937 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05301938 if (efer & EFER_LMA) {
1939 vmcs_write32(VM_ENTRY_CONTROLS,
1940 vmcs_read32(VM_ENTRY_CONTROLS) |
1941 VM_ENTRY_IA32E_MODE);
1942 msr->data = efer;
1943 } else {
1944 vmcs_write32(VM_ENTRY_CONTROLS,
1945 vmcs_read32(VM_ENTRY_CONTROLS) &
1946 ~VM_ENTRY_IA32E_MODE);
1947
1948 msr->data = efer & ~EFER_LME;
1949 }
1950 setup_msrs(vmx);
1951}
1952
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001953#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954
1955static void enter_lmode(struct kvm_vcpu *vcpu)
1956{
1957 u32 guest_tr_ar;
1958
Avi Kivity2fb92db2011-04-27 19:42:18 +03001959 vmx_segment_cache_clear(to_vmx(vcpu));
1960
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1962 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1963 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001964 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965 vmcs_write32(GUEST_TR_AR_BYTES,
1966 (guest_tr_ar & ~AR_TYPE_MASK)
1967 | AR_TYPE_BUSY_64_TSS);
1968 }
Avi Kivityda38f432010-07-06 11:30:49 +03001969 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001970}
1971
1972static void exit_lmode(struct kvm_vcpu *vcpu)
1973{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001974 vmcs_write32(VM_ENTRY_CONTROLS,
1975 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001976 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03001977 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001978}
1979
1980#endif
1981
Sheng Yang2384d2b2008-01-17 15:14:33 +08001982static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1983{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001984 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08001985 if (enable_ept) {
1986 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1987 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08001988 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08001989 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08001990}
1991
Avi Kivitye8467fd2009-12-29 18:43:06 +02001992static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1993{
1994 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
1995
1996 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
1997 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
1998}
1999
Avi Kivityaff48ba2010-12-05 18:56:11 +02002000static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2001{
2002 if (enable_ept && is_paging(vcpu))
2003 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2004 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2005}
2006
Anthony Liguori25c4c272007-04-27 09:29:21 +03002007static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002008{
Avi Kivityfc78f512009-12-07 12:16:48 +02002009 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2010
2011 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2012 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002013}
2014
Sheng Yang14394422008-04-28 12:24:45 +08002015static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2016{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002017 if (!test_bit(VCPU_EXREG_PDPTR,
2018 (unsigned long *)&vcpu->arch.regs_dirty))
2019 return;
2020
Sheng Yang14394422008-04-28 12:24:45 +08002021 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002022 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
2023 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
2024 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
2025 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002026 }
2027}
2028
Avi Kivity8f5d5492009-05-31 18:41:29 +03002029static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
2030{
2031 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002032 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2033 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2034 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2035 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002036 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002037
2038 __set_bit(VCPU_EXREG_PDPTR,
2039 (unsigned long *)&vcpu->arch.regs_avail);
2040 __set_bit(VCPU_EXREG_PDPTR,
2041 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002042}
2043
Sheng Yang14394422008-04-28 12:24:45 +08002044static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
2045
2046static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2047 unsigned long cr0,
2048 struct kvm_vcpu *vcpu)
2049{
Avi Kivityaff48ba2010-12-05 18:56:11 +02002050 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002051 if (!(cr0 & X86_CR0_PG)) {
2052 /* From paging/starting to nonpaging */
2053 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002054 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002055 (CPU_BASED_CR3_LOAD_EXITING |
2056 CPU_BASED_CR3_STORE_EXITING));
2057 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002058 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002059 } else if (!is_paging(vcpu)) {
2060 /* From nonpaging to paging */
2061 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002062 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002063 ~(CPU_BASED_CR3_LOAD_EXITING |
2064 CPU_BASED_CR3_STORE_EXITING));
2065 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002066 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002067 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002068
2069 if (!(cr0 & X86_CR0_WP))
2070 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002071}
2072
Avi Kivity6aa8b732006-12-10 02:21:36 -08002073static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2074{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002075 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002076 unsigned long hw_cr0;
2077
2078 if (enable_unrestricted_guest)
2079 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
2080 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
2081 else
2082 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002083
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002084 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002085 enter_pmode(vcpu);
2086
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002087 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088 enter_rmode(vcpu);
2089
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002090#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002091 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002092 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002093 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10002094 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095 exit_lmode(vcpu);
2096 }
2097#endif
2098
Avi Kivity089d0342009-03-23 18:26:32 +02002099 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002100 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2101
Avi Kivity02daab22009-12-30 12:40:26 +02002102 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02002103 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02002104
Avi Kivity6aa8b732006-12-10 02:21:36 -08002105 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002106 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002107 vcpu->arch.cr0 = cr0;
Avi Kivity69c73022011-03-07 15:26:44 +02002108 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002109}
2110
Sheng Yang14394422008-04-28 12:24:45 +08002111static u64 construct_eptp(unsigned long root_hpa)
2112{
2113 u64 eptp;
2114
2115 /* TODO write the value reading from MSR */
2116 eptp = VMX_EPT_DEFAULT_MT |
2117 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
2118 eptp |= (root_hpa & PAGE_MASK);
2119
2120 return eptp;
2121}
2122
Avi Kivity6aa8b732006-12-10 02:21:36 -08002123static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
2124{
Sheng Yang14394422008-04-28 12:24:45 +08002125 unsigned long guest_cr3;
2126 u64 eptp;
2127
2128 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002129 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08002130 eptp = construct_eptp(cr3);
2131 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002132 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08002133 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02002134 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002135 }
2136
Sheng Yang2384d2b2008-01-17 15:14:33 +08002137 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002138 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139}
2140
2141static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
2142{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002143 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08002144 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
2145
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002146 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02002147 if (enable_ept) {
2148 if (!is_paging(vcpu)) {
2149 hw_cr4 &= ~X86_CR4_PAE;
2150 hw_cr4 |= X86_CR4_PSE;
2151 } else if (!(cr4 & X86_CR4_PAE)) {
2152 hw_cr4 &= ~X86_CR4_PAE;
2153 }
2154 }
Sheng Yang14394422008-04-28 12:24:45 +08002155
2156 vmcs_writel(CR4_READ_SHADOW, cr4);
2157 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002158}
2159
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160static void vmx_get_segment(struct kvm_vcpu *vcpu,
2161 struct kvm_segment *var, int seg)
2162{
Avi Kivitya9179492011-01-03 14:28:52 +02002163 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitya9179492011-01-03 14:28:52 +02002164 struct kvm_save_segment *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002165 u32 ar;
2166
Avi Kivitya9179492011-01-03 14:28:52 +02002167 if (vmx->rmode.vm86_active
2168 && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
2169 || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
2170 || seg == VCPU_SREG_GS)
2171 && !emulate_invalid_guest_state) {
2172 switch (seg) {
2173 case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
2174 case VCPU_SREG_ES: save = &vmx->rmode.es; break;
2175 case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
2176 case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
2177 case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
2178 default: BUG();
2179 }
2180 var->selector = save->selector;
2181 var->base = save->base;
2182 var->limit = save->limit;
2183 ar = save->ar;
2184 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002185 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivitya9179492011-01-03 14:28:52 +02002186 goto use_saved_rmode_seg;
2187 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002188 var->base = vmx_read_guest_seg_base(vmx, seg);
2189 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2190 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2191 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002192use_saved_rmode_seg:
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02002193 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002194 ar = 0;
2195 var->type = ar & 15;
2196 var->s = (ar >> 4) & 1;
2197 var->dpl = (ar >> 5) & 3;
2198 var->present = (ar >> 7) & 1;
2199 var->avl = (ar >> 12) & 1;
2200 var->l = (ar >> 13) & 1;
2201 var->db = (ar >> 14) & 1;
2202 var->g = (ar >> 15) & 1;
2203 var->unusable = (ar >> 16) & 1;
2204}
2205
Avi Kivitya9179492011-01-03 14:28:52 +02002206static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2207{
Avi Kivitya9179492011-01-03 14:28:52 +02002208 struct kvm_segment s;
2209
2210 if (to_vmx(vcpu)->rmode.vm86_active) {
2211 vmx_get_segment(vcpu, &s, seg);
2212 return s.base;
2213 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002214 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002215}
2216
Avi Kivity69c73022011-03-07 15:26:44 +02002217static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002218{
Avi Kivity3eeb3282010-01-21 15:31:48 +02002219 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002220 return 0;
2221
Avi Kivityf4c63e52011-03-07 14:54:28 +02002222 if (!is_long_mode(vcpu)
2223 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02002224 return 3;
2225
Avi Kivity2fb92db2011-04-27 19:42:18 +03002226 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02002227}
2228
Avi Kivity69c73022011-03-07 15:26:44 +02002229static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2230{
2231 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
2232 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2233 to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
2234 }
2235 return to_vmx(vcpu)->cpl;
2236}
2237
2238
Avi Kivity653e3102007-05-07 10:55:37 +03002239static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002240{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002241 u32 ar;
2242
Avi Kivity653e3102007-05-07 10:55:37 +03002243 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 ar = 1 << 16;
2245 else {
2246 ar = var->type & 15;
2247 ar |= (var->s & 1) << 4;
2248 ar |= (var->dpl & 3) << 5;
2249 ar |= (var->present & 1) << 7;
2250 ar |= (var->avl & 1) << 12;
2251 ar |= (var->l & 1) << 13;
2252 ar |= (var->db & 1) << 14;
2253 ar |= (var->g & 1) << 15;
2254 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08002255 if (ar == 0) /* a 0 value means unusable */
2256 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03002257
2258 return ar;
2259}
2260
2261static void vmx_set_segment(struct kvm_vcpu *vcpu,
2262 struct kvm_segment *var, int seg)
2263{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002264 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03002265 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2266 u32 ar;
2267
Avi Kivity2fb92db2011-04-27 19:42:18 +03002268 vmx_segment_cache_clear(vmx);
2269
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002270 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
Gleb Natapova8ba6c22011-02-21 12:07:58 +02002271 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002272 vmx->rmode.tr.selector = var->selector;
2273 vmx->rmode.tr.base = var->base;
2274 vmx->rmode.tr.limit = var->limit;
2275 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03002276 return;
2277 }
2278 vmcs_writel(sf->base, var->base);
2279 vmcs_write32(sf->limit, var->limit);
2280 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002281 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03002282 /*
2283 * Hack real-mode segments into vm86 compatibility.
2284 */
2285 if (var->base == 0xffff0000 && var->selector == 0xf000)
2286 vmcs_writel(sf->base, 0xf0000);
2287 ar = 0xf3;
2288 } else
2289 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002290
2291 /*
2292 * Fix the "Accessed" bit in AR field of segment registers for older
2293 * qemu binaries.
2294 * IA32 arch specifies that at the time of processor reset the
2295 * "Accessed" bit in the AR field of segment registers is 1. And qemu
2296 * is setting it to 0 in the usedland code. This causes invalid guest
2297 * state vmexit when "unrestricted guest" mode is turned on.
2298 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2299 * tree. Newer qemu binaries with that qemu fix would not need this
2300 * kvm hack.
2301 */
2302 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
2303 ar |= 0x1; /* Accessed */
2304
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002306 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002307}
2308
Avi Kivity6aa8b732006-12-10 02:21:36 -08002309static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2310{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002311 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312
2313 *db = (ar >> 14) & 1;
2314 *l = (ar >> 13) & 1;
2315}
2316
Gleb Natapov89a27f42010-02-16 10:51:48 +02002317static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002318{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002319 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2320 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321}
2322
Gleb Natapov89a27f42010-02-16 10:51:48 +02002323static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002325 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2326 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327}
2328
Gleb Natapov89a27f42010-02-16 10:51:48 +02002329static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002331 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2332 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333}
2334
Gleb Natapov89a27f42010-02-16 10:51:48 +02002335static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002337 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2338 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339}
2340
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002341static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2342{
2343 struct kvm_segment var;
2344 u32 ar;
2345
2346 vmx_get_segment(vcpu, &var, seg);
2347 ar = vmx_segment_access_rights(&var);
2348
2349 if (var.base != (var.selector << 4))
2350 return false;
2351 if (var.limit != 0xffff)
2352 return false;
2353 if (ar != 0xf3)
2354 return false;
2355
2356 return true;
2357}
2358
2359static bool code_segment_valid(struct kvm_vcpu *vcpu)
2360{
2361 struct kvm_segment cs;
2362 unsigned int cs_rpl;
2363
2364 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2365 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2366
Avi Kivity1872a3f2009-01-04 23:26:52 +02002367 if (cs.unusable)
2368 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002369 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2370 return false;
2371 if (!cs.s)
2372 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002373 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002374 if (cs.dpl > cs_rpl)
2375 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002376 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002377 if (cs.dpl != cs_rpl)
2378 return false;
2379 }
2380 if (!cs.present)
2381 return false;
2382
2383 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2384 return true;
2385}
2386
2387static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2388{
2389 struct kvm_segment ss;
2390 unsigned int ss_rpl;
2391
2392 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2393 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2394
Avi Kivity1872a3f2009-01-04 23:26:52 +02002395 if (ss.unusable)
2396 return true;
2397 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002398 return false;
2399 if (!ss.s)
2400 return false;
2401 if (ss.dpl != ss_rpl) /* DPL != RPL */
2402 return false;
2403 if (!ss.present)
2404 return false;
2405
2406 return true;
2407}
2408
2409static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2410{
2411 struct kvm_segment var;
2412 unsigned int rpl;
2413
2414 vmx_get_segment(vcpu, &var, seg);
2415 rpl = var.selector & SELECTOR_RPL_MASK;
2416
Avi Kivity1872a3f2009-01-04 23:26:52 +02002417 if (var.unusable)
2418 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002419 if (!var.s)
2420 return false;
2421 if (!var.present)
2422 return false;
2423 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2424 if (var.dpl < rpl) /* DPL < RPL */
2425 return false;
2426 }
2427
2428 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2429 * rights flags
2430 */
2431 return true;
2432}
2433
2434static bool tr_valid(struct kvm_vcpu *vcpu)
2435{
2436 struct kvm_segment tr;
2437
2438 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2439
Avi Kivity1872a3f2009-01-04 23:26:52 +02002440 if (tr.unusable)
2441 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002442 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2443 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002444 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002445 return false;
2446 if (!tr.present)
2447 return false;
2448
2449 return true;
2450}
2451
2452static bool ldtr_valid(struct kvm_vcpu *vcpu)
2453{
2454 struct kvm_segment ldtr;
2455
2456 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2457
Avi Kivity1872a3f2009-01-04 23:26:52 +02002458 if (ldtr.unusable)
2459 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002460 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2461 return false;
2462 if (ldtr.type != 2)
2463 return false;
2464 if (!ldtr.present)
2465 return false;
2466
2467 return true;
2468}
2469
2470static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2471{
2472 struct kvm_segment cs, ss;
2473
2474 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2475 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2476
2477 return ((cs.selector & SELECTOR_RPL_MASK) ==
2478 (ss.selector & SELECTOR_RPL_MASK));
2479}
2480
2481/*
2482 * Check if guest state is valid. Returns true if valid, false if
2483 * not.
2484 * We assume that registers are always usable
2485 */
2486static bool guest_state_valid(struct kvm_vcpu *vcpu)
2487{
2488 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02002489 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002490 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2491 return false;
2492 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2493 return false;
2494 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2495 return false;
2496 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2497 return false;
2498 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2499 return false;
2500 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2501 return false;
2502 } else {
2503 /* protected mode guest state checks */
2504 if (!cs_ss_rpl_check(vcpu))
2505 return false;
2506 if (!code_segment_valid(vcpu))
2507 return false;
2508 if (!stack_segment_valid(vcpu))
2509 return false;
2510 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2511 return false;
2512 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2513 return false;
2514 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2515 return false;
2516 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2517 return false;
2518 if (!tr_valid(vcpu))
2519 return false;
2520 if (!ldtr_valid(vcpu))
2521 return false;
2522 }
2523 /* TODO:
2524 * - Add checks on RIP
2525 * - Add checks on RFLAGS
2526 */
2527
2528 return true;
2529}
2530
Mike Dayd77c26f2007-10-08 09:02:08 -04002531static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002532{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002533 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02002534 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002535 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002537 idx = srcu_read_lock(&kvm->srcu);
2538 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002539 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2540 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002541 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002542 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002543 r = kvm_write_guest_page(kvm, fn++, &data,
2544 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002545 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002546 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002547 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2548 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002549 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002550 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2551 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002552 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002553 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002554 r = kvm_write_guest_page(kvm, fn, &data,
2555 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2556 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002557 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002558 goto out;
2559
2560 ret = 1;
2561out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002562 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002563 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564}
2565
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002566static int init_rmode_identity_map(struct kvm *kvm)
2567{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002568 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002569 pfn_t identity_map_pfn;
2570 u32 tmp;
2571
Avi Kivity089d0342009-03-23 18:26:32 +02002572 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002573 return 1;
2574 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2575 printk(KERN_ERR "EPT: identity-mapping pagetable "
2576 "haven't been allocated!\n");
2577 return 0;
2578 }
2579 if (likely(kvm->arch.ept_identity_pagetable_done))
2580 return 1;
2581 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002582 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002583 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002584 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2585 if (r < 0)
2586 goto out;
2587 /* Set up identity-mapping pagetable for EPT in real mode */
2588 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2589 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2590 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2591 r = kvm_write_guest_page(kvm, identity_map_pfn,
2592 &tmp, i * sizeof(tmp), sizeof(tmp));
2593 if (r < 0)
2594 goto out;
2595 }
2596 kvm->arch.ept_identity_pagetable_done = true;
2597 ret = 1;
2598out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002599 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002600 return ret;
2601}
2602
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603static void seg_setup(int seg)
2604{
2605 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002606 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607
2608 vmcs_write16(sf->selector, 0);
2609 vmcs_writel(sf->base, 0);
2610 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002611 if (enable_unrestricted_guest) {
2612 ar = 0x93;
2613 if (seg == VCPU_SREG_CS)
2614 ar |= 0x08; /* code segment */
2615 } else
2616 ar = 0xf3;
2617
2618 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002619}
2620
Sheng Yangf78e0e22007-10-29 09:40:42 +08002621static int alloc_apic_access_page(struct kvm *kvm)
2622{
2623 struct kvm_userspace_memory_region kvm_userspace_mem;
2624 int r = 0;
2625
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002626 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002627 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002628 goto out;
2629 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2630 kvm_userspace_mem.flags = 0;
2631 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2632 kvm_userspace_mem.memory_size = PAGE_SIZE;
2633 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2634 if (r)
2635 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002636
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002637 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002638out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002639 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002640 return r;
2641}
2642
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002643static int alloc_identity_pagetable(struct kvm *kvm)
2644{
2645 struct kvm_userspace_memory_region kvm_userspace_mem;
2646 int r = 0;
2647
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002648 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002649 if (kvm->arch.ept_identity_pagetable)
2650 goto out;
2651 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2652 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002653 kvm_userspace_mem.guest_phys_addr =
2654 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002655 kvm_userspace_mem.memory_size = PAGE_SIZE;
2656 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2657 if (r)
2658 goto out;
2659
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002660 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002661 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002662out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002663 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002664 return r;
2665}
2666
Sheng Yang2384d2b2008-01-17 15:14:33 +08002667static void allocate_vpid(struct vcpu_vmx *vmx)
2668{
2669 int vpid;
2670
2671 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002672 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002673 return;
2674 spin_lock(&vmx_vpid_lock);
2675 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2676 if (vpid < VMX_NR_VPIDS) {
2677 vmx->vpid = vpid;
2678 __set_bit(vpid, vmx_vpid_bitmap);
2679 }
2680 spin_unlock(&vmx_vpid_lock);
2681}
2682
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08002683static void free_vpid(struct vcpu_vmx *vmx)
2684{
2685 if (!enable_vpid)
2686 return;
2687 spin_lock(&vmx_vpid_lock);
2688 if (vmx->vpid != 0)
2689 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2690 spin_unlock(&vmx_vpid_lock);
2691}
2692
Avi Kivity58972972009-02-24 22:26:47 +02002693static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002694{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002695 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002696
2697 if (!cpu_has_vmx_msr_bitmap())
2698 return;
2699
2700 /*
2701 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2702 * have the write-low and read-high bitmap offsets the wrong way round.
2703 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2704 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002705 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002706 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2707 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002708 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2709 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002710 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2711 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002712 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002713}
2714
Avi Kivity58972972009-02-24 22:26:47 +02002715static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2716{
2717 if (!longmode_only)
2718 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2719 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2720}
2721
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722/*
2723 * Sets up the vmcs for emulated real mode.
2724 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002725static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726{
Sheng Yang468d4722008-10-09 16:01:55 +08002727 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 u32 junk;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002729 u64 host_pat;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 unsigned long a;
Gleb Natapov89a27f42010-02-16 10:51:48 +02002731 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002733 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002734 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002737 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2738 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739
Sheng Yang25c5f222008-03-28 13:18:56 +08002740 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002741 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002742
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2744
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002746 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2747 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002748
2749 exec_control = vmcs_config.cpu_based_exec_ctrl;
2750 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2751 exec_control &= ~CPU_BASED_TPR_SHADOW;
2752#ifdef CONFIG_X86_64
2753 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2754 CPU_BASED_CR8_LOAD_EXITING;
2755#endif
2756 }
Avi Kivity089d0342009-03-23 18:26:32 +02002757 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002758 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002759 CPU_BASED_CR3_LOAD_EXITING |
2760 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002761 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762
Sheng Yang83ff3b92007-11-21 14:33:25 +08002763 if (cpu_has_secondary_exec_ctrls()) {
2764 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2765 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2766 exec_control &=
2767 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002768 if (vmx->vpid == 0)
2769 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002770 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002771 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002772 enable_unrestricted_guest = 0;
2773 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002774 if (!enable_unrestricted_guest)
2775 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002776 if (!ple_gap)
2777 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002778 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2779 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002780
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002781 if (ple_gap) {
2782 vmcs_write32(PLE_GAP, ple_gap);
2783 vmcs_write32(PLE_WINDOW, ple_window);
2784 }
2785
Avi Kivityc7addb92007-09-16 18:58:32 +02002786 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2787 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2789
Avi Kivity1c11e712010-05-03 16:05:44 +03002790 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2792 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2793
2794 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2795 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2796 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity9581d442010-10-19 16:46:55 +02002797 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
2798 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002799 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002800#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002801 rdmsrl(MSR_FS_BASE, a);
2802 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2803 rdmsrl(MSR_GS_BASE, a);
2804 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2805#else
2806 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2807 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2808#endif
2809
2810 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2811
Wei Yongjunec687982010-03-05 12:11:48 +08002812 native_store_idt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02002813 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002814
Mike Dayd77c26f2007-10-08 09:02:08 -04002815 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002816 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002817 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2818 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002819 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03002820 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002821 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822
2823 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2824 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2825 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2826 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2827 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2828 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2829
Sheng Yang468d4722008-10-09 16:01:55 +08002830 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2831 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2832 host_pat = msr_low | ((u64) msr_high << 32);
2833 vmcs_write64(HOST_IA32_PAT, host_pat);
2834 }
2835 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2836 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2837 host_pat = msr_low | ((u64) msr_high << 32);
2838 /* Write the default value follow host pat */
2839 vmcs_write64(GUEST_IA32_PAT, host_pat);
2840 /* Keep arch.pat sync with GUEST_IA32_PAT */
2841 vmx->vcpu.arch.pat = host_pat;
2842 }
2843
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 for (i = 0; i < NR_VMX_MSR; ++i) {
2845 u32 index = vmx_msr_index[i];
2846 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002847 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002848
2849 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2850 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002851 if (wrmsr_safe(index, data_low, data_high) < 0)
2852 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03002853 vmx->guest_msrs[j].index = i;
2854 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002855 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002856 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002859 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860
2861 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002862 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2863
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002864 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02002865 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02002866 if (enable_ept)
2867 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02002868 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002869
Zachary Amsden99e3e302010-08-19 22:07:17 -10002870 kvm_write_tsc(&vmx->vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002871
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002872 return 0;
2873}
2874
2875static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2876{
2877 struct vcpu_vmx *vmx = to_vmx(vcpu);
2878 u64 msr;
Xiao Guangrong4b9d3a02010-06-08 10:15:51 +08002879 int ret;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002880
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002881 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002882
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002883 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002884
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002885 vmx->soft_vnmi_blocked = 0;
2886
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002887 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002888 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002889 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002890 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002891 msr |= MSR_IA32_APICBASE_BSP;
2892 kvm_set_apic_base(&vmx->vcpu, msr);
2893
Jan Kiszka10ab25c2010-05-25 16:01:50 +02002894 ret = fx_init(&vmx->vcpu);
2895 if (ret != 0)
2896 goto out;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002897
Avi Kivity2fb92db2011-04-27 19:42:18 +03002898 vmx_segment_cache_clear(vmx);
2899
Avi Kivity5706be02008-08-20 15:07:31 +03002900 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002901 /*
2902 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2903 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2904 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002905 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002906 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2907 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2908 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002909 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2910 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002911 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002912
2913 seg_setup(VCPU_SREG_DS);
2914 seg_setup(VCPU_SREG_ES);
2915 seg_setup(VCPU_SREG_FS);
2916 seg_setup(VCPU_SREG_GS);
2917 seg_setup(VCPU_SREG_SS);
2918
2919 vmcs_write16(GUEST_TR_SELECTOR, 0);
2920 vmcs_writel(GUEST_TR_BASE, 0);
2921 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2922 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2923
2924 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2925 vmcs_writel(GUEST_LDTR_BASE, 0);
2926 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2927 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2928
2929 vmcs_write32(GUEST_SYSENTER_CS, 0);
2930 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2931 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2932
2933 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002934 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002935 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002936 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002937 kvm_rip_write(vcpu, 0);
2938 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002939
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002940 vmcs_writel(GUEST_DR7, 0x400);
2941
2942 vmcs_writel(GUEST_GDTR_BASE, 0);
2943 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2944
2945 vmcs_writel(GUEST_IDTR_BASE, 0);
2946 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2947
Anthony Liguori443381a2010-12-06 10:53:38 -06002948 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002949 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2950 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2951
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002952 /* Special registers */
2953 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2954
2955 setup_msrs(vmx);
2956
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2958
Sheng Yangf78e0e22007-10-29 09:40:42 +08002959 if (cpu_has_vmx_tpr_shadow()) {
2960 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2961 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2962 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002963 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002964 vmcs_write32(TPR_THRESHOLD, 0);
2965 }
2966
2967 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2968 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002969 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970
Sheng Yang2384d2b2008-01-17 15:14:33 +08002971 if (vmx->vpid != 0)
2972 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2973
Eduardo Habkostfa400522009-10-24 02:49:58 -02002974 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002975 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002976 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002977 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002978 vmx_fpu_activate(&vmx->vcpu);
2979 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002980
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002981 vpid_sync_context(vmx);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002982
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002983 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002985 /* HACK: Don't enable emulation on guest boot/reset */
2986 vmx->emulation_required = 0;
2987
Avi Kivity6aa8b732006-12-10 02:21:36 -08002988out:
2989 return ret;
2990}
2991
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002992static void enable_irq_window(struct kvm_vcpu *vcpu)
2993{
2994 u32 cpu_based_vm_exec_control;
2995
2996 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2997 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2998 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2999}
3000
3001static void enable_nmi_window(struct kvm_vcpu *vcpu)
3002{
3003 u32 cpu_based_vm_exec_control;
3004
3005 if (!cpu_has_virtual_nmis()) {
3006 enable_irq_window(vcpu);
3007 return;
3008 }
3009
Avi Kivity30bd0c42010-11-01 23:20:48 +02003010 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
3011 enable_irq_window(vcpu);
3012 return;
3013 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003014 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3015 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
3016 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3017}
3018
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003019static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003020{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003021 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003022 uint32_t intr;
3023 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003024
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003025 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003026
Avi Kivityfa89a812008-09-01 15:57:51 +03003027 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003028 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003029 int inc_eip = 0;
3030 if (vcpu->arch.interrupt.soft)
3031 inc_eip = vcpu->arch.event_exit_inst_len;
3032 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003033 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003034 return;
3035 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003036 intr = irq | INTR_INFO_VALID_MASK;
3037 if (vcpu->arch.interrupt.soft) {
3038 intr |= INTR_TYPE_SOFT_INTR;
3039 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3040 vmx->vcpu.arch.event_exit_inst_len);
3041 } else
3042 intr |= INTR_TYPE_EXT_INTR;
3043 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Anthony Liguori443381a2010-12-06 10:53:38 -06003044 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003045}
3046
Sheng Yangf08864b2008-05-15 18:23:25 +08003047static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3048{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003049 struct vcpu_vmx *vmx = to_vmx(vcpu);
3050
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003051 if (!cpu_has_virtual_nmis()) {
3052 /*
3053 * Tracking the NMI-blocked state in software is built upon
3054 * finding the next open IRQ window. This, in turn, depends on
3055 * well-behaving guests: They have to keep IRQs disabled at
3056 * least as long as the NMI handler runs. Otherwise we may
3057 * cause NMI nesting, maybe breaking the guest. But as this is
3058 * highly unlikely, we can live with the residual risk.
3059 */
3060 vmx->soft_vnmi_blocked = 1;
3061 vmx->vnmi_blocked_time = 0;
3062 }
3063
Jan Kiszka487b3912008-09-26 09:30:56 +02003064 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02003065 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003066 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003067 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003068 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003069 return;
3070 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003071 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3072 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Anthony Liguori443381a2010-12-06 10:53:38 -06003073 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003074}
3075
Gleb Natapovc4282df2009-04-21 17:45:07 +03003076static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02003077{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003078 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03003079 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02003080
Gleb Natapovc4282df2009-04-21 17:45:07 +03003081 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02003082 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
3083 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02003084}
3085
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003086static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
3087{
3088 if (!cpu_has_virtual_nmis())
3089 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02003090 if (to_vmx(vcpu)->nmi_known_unmasked)
3091 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03003092 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003093}
3094
3095static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3096{
3097 struct vcpu_vmx *vmx = to_vmx(vcpu);
3098
3099 if (!cpu_has_virtual_nmis()) {
3100 if (vmx->soft_vnmi_blocked != masked) {
3101 vmx->soft_vnmi_blocked = masked;
3102 vmx->vnmi_blocked_time = 0;
3103 }
3104 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02003105 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003106 if (masked)
3107 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3108 GUEST_INTR_STATE_NMI);
3109 else
3110 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3111 GUEST_INTR_STATE_NMI);
3112 }
3113}
3114
Gleb Natapov78646122009-03-23 12:12:11 +02003115static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
3116{
Gleb Natapovc4282df2009-04-21 17:45:07 +03003117 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
3118 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3119 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02003120}
3121
Izik Eiduscbc94022007-10-25 00:29:55 +02003122static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
3123{
3124 int ret;
3125 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08003126 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02003127 .guest_phys_addr = addr,
3128 .memory_size = PAGE_SIZE * 3,
3129 .flags = 0,
3130 };
3131
3132 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
3133 if (ret)
3134 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003135 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02003136 if (!init_rmode_tss(kvm))
3137 return -ENOMEM;
3138
Izik Eiduscbc94022007-10-25 00:29:55 +02003139 return 0;
3140}
3141
Avi Kivity6aa8b732006-12-10 02:21:36 -08003142static int handle_rmode_exception(struct kvm_vcpu *vcpu,
3143 int vec, u32 err_code)
3144{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03003145 /*
3146 * Instruction with address size override prefix opcode 0x67
3147 * Cause the #SS fault with 0 error code in VM86 mode.
3148 */
3149 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Andre Przywara51d8b662010-12-21 11:12:02 +01003150 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003151 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003152 /*
3153 * Forward all other exceptions that are valid in real mode.
3154 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
3155 * the required debugging infrastructure rework.
3156 */
3157 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003158 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003159 if (vcpu->guest_debug &
3160 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
3161 return 0;
3162 kvm_queue_exception(vcpu, vec);
3163 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003164 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01003165 /*
3166 * Update instruction length as we may reinject the exception
3167 * from user space while in guest debugging mode.
3168 */
3169 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
3170 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003171 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
3172 return 0;
3173 /* fall through */
3174 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003175 case OF_VECTOR:
3176 case BR_VECTOR:
3177 case UD_VECTOR:
3178 case DF_VECTOR:
3179 case SS_VECTOR:
3180 case GP_VECTOR:
3181 case MF_VECTOR:
3182 kvm_queue_exception(vcpu, vec);
3183 return 1;
3184 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185 return 0;
3186}
3187
Andi Kleena0861c02009-06-08 17:37:09 +08003188/*
3189 * Trigger machine check on the host. We assume all the MSRs are already set up
3190 * by the CPU and that we still run on the same CPU as the MCE occurred on.
3191 * We pass a fake environment to the machine check handler because we want
3192 * the guest to be always treated like user space, no matter what context
3193 * it used internally.
3194 */
3195static void kvm_machine_check(void)
3196{
3197#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
3198 struct pt_regs regs = {
3199 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
3200 .flags = X86_EFLAGS_IF,
3201 };
3202
3203 do_machine_check(&regs, 0);
3204#endif
3205}
3206
Avi Kivity851ba692009-08-24 11:10:17 +03003207static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08003208{
3209 /* already handled by vcpu_run */
3210 return 1;
3211}
3212
Avi Kivity851ba692009-08-24 11:10:17 +03003213static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003214{
Avi Kivity1155f762007-11-22 11:30:47 +02003215 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003216 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003217 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003218 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003219 u32 vect_info;
3220 enum emulation_result er;
3221
Avi Kivity1155f762007-11-22 11:30:47 +02003222 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02003223 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224
Andi Kleena0861c02009-06-08 17:37:09 +08003225 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03003226 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003227
Avi Kivity6aa8b732006-12-10 02:21:36 -08003228 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02003229 !is_page_fault(intr_info)) {
3230 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3231 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
3232 vcpu->run->internal.ndata = 2;
3233 vcpu->run->internal.data[0] = vect_info;
3234 vcpu->run->internal.data[1] = intr_info;
3235 return 0;
3236 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237
Jan Kiszkae4a41882008-09-26 09:30:46 +02003238 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02003239 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003240
3241 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03003242 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003243 return 1;
3244 }
3245
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003246 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01003247 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003248 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02003249 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003250 return 1;
3251 }
3252
Avi Kivity6aa8b732006-12-10 02:21:36 -08003253 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06003254 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
3256 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08003257 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02003258 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003259 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003260 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003261 trace_kvm_page_fault(cr2, error_code);
3262
Gleb Natapov3298b752009-05-11 13:35:46 +03003263 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03003264 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01003265 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003266 }
3267
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003268 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003270 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003271 if (vcpu->arch.halt_request) {
3272 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003273 return kvm_emulate_halt(vcpu);
3274 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003276 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003277
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003278 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003279 switch (ex_no) {
3280 case DB_VECTOR:
3281 dr6 = vmcs_readl(EXIT_QUALIFICATION);
3282 if (!(vcpu->guest_debug &
3283 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
3284 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
3285 kvm_queue_exception(vcpu, DB_VECTOR);
3286 return 1;
3287 }
3288 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
3289 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
3290 /* fall through */
3291 case BP_VECTOR:
Jan Kiszkac573cd222010-02-23 17:47:53 +01003292 /*
3293 * Update instruction length as we may reinject #BP from
3294 * user space while in guest debugging mode. Reading it for
3295 * #DB as well causes no harm, it is not used in that case.
3296 */
3297 vmx->vcpu.arch.event_exit_inst_len =
3298 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03003300 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003301 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
3302 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003303 break;
3304 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003305 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
3306 kvm_run->ex.exception = ex_no;
3307 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003308 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 return 0;
3311}
3312
Avi Kivity851ba692009-08-24 11:10:17 +03003313static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003314{
Avi Kivity1165f5f2007-04-19 17:27:43 +03003315 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003316 return 1;
3317}
3318
Avi Kivity851ba692009-08-24 11:10:17 +03003319static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08003320{
Avi Kivity851ba692009-08-24 11:10:17 +03003321 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08003322 return 0;
3323}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324
Avi Kivity851ba692009-08-24 11:10:17 +03003325static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326{
He, Qingbfdaab02007-09-12 14:18:28 +08003327 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01003328 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02003329 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003330
He, Qingbfdaab02007-09-12 14:18:28 +08003331 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02003332 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003333 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003334
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003335 ++vcpu->stat.io_exits;
3336
3337 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01003338 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003339
3340 port = exit_qualification >> 16;
3341 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003342 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003343
3344 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345}
3346
Ingo Molnar102d8322007-02-19 14:37:47 +02003347static void
3348vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3349{
3350 /*
3351 * Patch in the VMCALL instruction:
3352 */
3353 hypercall[0] = 0x0f;
3354 hypercall[1] = 0x01;
3355 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02003356}
3357
Avi Kivity851ba692009-08-24 11:10:17 +03003358static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003359{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003360 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003361 int cr;
3362 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03003363 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364
He, Qingbfdaab02007-09-12 14:18:28 +08003365 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366 cr = exit_qualification & 15;
3367 reg = (exit_qualification >> 8) & 15;
3368 switch ((exit_qualification >> 4) & 3) {
3369 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003370 val = kvm_register_read(vcpu, reg);
3371 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 switch (cr) {
3373 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03003374 err = kvm_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003375 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003376 return 1;
3377 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03003378 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003379 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380 return 1;
3381 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03003382 err = kvm_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003383 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003385 case 8: {
3386 u8 cr8_prev = kvm_get_cr8(vcpu);
3387 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003388 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003389 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003390 if (irqchip_in_kernel(vcpu->kvm))
3391 return 1;
3392 if (cr8_prev <= cr8)
3393 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03003394 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003395 return 0;
3396 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003397 };
3398 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03003399 case 2: /* clts */
Avi Kivityedcafe32009-12-30 18:07:40 +02003400 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02003401 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003402 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02003403 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03003404 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405 case 1: /*mov from cr*/
3406 switch (cr) {
3407 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003408 val = kvm_read_cr3(vcpu);
3409 kvm_register_write(vcpu, reg, val);
3410 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003411 skip_emulated_instruction(vcpu);
3412 return 1;
3413 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003414 val = kvm_get_cr8(vcpu);
3415 kvm_register_write(vcpu, reg, val);
3416 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003417 skip_emulated_instruction(vcpu);
3418 return 1;
3419 }
3420 break;
3421 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003422 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003423 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003424 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425
3426 skip_emulated_instruction(vcpu);
3427 return 1;
3428 default:
3429 break;
3430 }
Avi Kivity851ba692009-08-24 11:10:17 +03003431 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003432 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 (int)(exit_qualification >> 4) & 3, cr);
3434 return 0;
3435}
3436
Avi Kivity851ba692009-08-24 11:10:17 +03003437static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438{
He, Qingbfdaab02007-09-12 14:18:28 +08003439 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440 int dr, reg;
3441
Jan Kiszkaf2483412010-01-20 18:20:20 +01003442 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03003443 if (!kvm_require_cpl(vcpu, 0))
3444 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003445 dr = vmcs_readl(GUEST_DR7);
3446 if (dr & DR7_GD) {
3447 /*
3448 * As the vm-exit takes precedence over the debug trap, we
3449 * need to emulate the latter, either for the host or the
3450 * guest debugging itself.
3451 */
3452 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003453 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3454 vcpu->run->debug.arch.dr7 = dr;
3455 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003456 vmcs_readl(GUEST_CS_BASE) +
3457 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003458 vcpu->run->debug.arch.exception = DB_VECTOR;
3459 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003460 return 0;
3461 } else {
3462 vcpu->arch.dr7 &= ~DR7_GD;
3463 vcpu->arch.dr6 |= DR6_BD;
3464 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3465 kvm_queue_exception(vcpu, DB_VECTOR);
3466 return 1;
3467 }
3468 }
3469
He, Qingbfdaab02007-09-12 14:18:28 +08003470 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003471 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3472 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3473 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03003474 unsigned long val;
3475 if (!kvm_get_dr(vcpu, dr, &val))
3476 kvm_register_write(vcpu, reg, val);
3477 } else
3478 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003479 skip_emulated_instruction(vcpu);
3480 return 1;
3481}
3482
Gleb Natapov020df072010-04-13 10:05:23 +03003483static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3484{
3485 vmcs_writel(GUEST_DR7, val);
3486}
3487
Avi Kivity851ba692009-08-24 11:10:17 +03003488static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489{
Avi Kivity06465c52007-02-28 20:46:53 +02003490 kvm_emulate_cpuid(vcpu);
3491 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492}
3493
Avi Kivity851ba692009-08-24 11:10:17 +03003494static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003496 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497 u64 data;
3498
3499 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02003500 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003501 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003502 return 1;
3503 }
3504
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003505 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003506
Avi Kivity6aa8b732006-12-10 02:21:36 -08003507 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003508 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3509 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 skip_emulated_instruction(vcpu);
3511 return 1;
3512}
3513
Avi Kivity851ba692009-08-24 11:10:17 +03003514static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003515{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003516 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3517 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3518 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519
3520 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02003521 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003522 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523 return 1;
3524 }
3525
Avi Kivity59200272010-01-25 19:47:02 +02003526 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003527 skip_emulated_instruction(vcpu);
3528 return 1;
3529}
3530
Avi Kivity851ba692009-08-24 11:10:17 +03003531static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003532{
Avi Kivity3842d132010-07-27 12:30:24 +03003533 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003534 return 1;
3535}
3536
Avi Kivity851ba692009-08-24 11:10:17 +03003537static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538{
Eddie Dong85f455f2007-07-06 12:20:49 +03003539 u32 cpu_based_vm_exec_control;
3540
3541 /* clear pending irq */
3542 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3543 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3544 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003545
Avi Kivity3842d132010-07-27 12:30:24 +03003546 kvm_make_request(KVM_REQ_EVENT, vcpu);
3547
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003548 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003549
Dor Laorc1150d82007-01-05 16:36:24 -08003550 /*
3551 * If the user space waits to inject interrupts, exit as soon as
3552 * possible
3553 */
Gleb Natapov80618232009-04-21 17:44:56 +03003554 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003555 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003556 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003557 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003558 return 0;
3559 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003560 return 1;
3561}
3562
Avi Kivity851ba692009-08-24 11:10:17 +03003563static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003564{
3565 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003566 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567}
3568
Avi Kivity851ba692009-08-24 11:10:17 +03003569static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003570{
Dor Laor510043d2007-02-19 18:25:43 +02003571 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003572 kvm_emulate_hypercall(vcpu);
3573 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003574}
3575
Avi Kivity851ba692009-08-24 11:10:17 +03003576static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003577{
3578 kvm_queue_exception(vcpu, UD_VECTOR);
3579 return 1;
3580}
3581
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003582static int handle_invd(struct kvm_vcpu *vcpu)
3583{
Andre Przywara51d8b662010-12-21 11:12:02 +01003584 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003585}
3586
Avi Kivity851ba692009-08-24 11:10:17 +03003587static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003588{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003589 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003590
3591 kvm_mmu_invlpg(vcpu, exit_qualification);
3592 skip_emulated_instruction(vcpu);
3593 return 1;
3594}
3595
Avi Kivity851ba692009-08-24 11:10:17 +03003596static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003597{
3598 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003599 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02003600 return 1;
3601}
3602
Dexuan Cui2acf9232010-06-10 11:27:12 +08003603static int handle_xsetbv(struct kvm_vcpu *vcpu)
3604{
3605 u64 new_bv = kvm_read_edx_eax(vcpu);
3606 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3607
3608 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
3609 skip_emulated_instruction(vcpu);
3610 return 1;
3611}
3612
Avi Kivity851ba692009-08-24 11:10:17 +03003613static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003614{
Andre Przywara51d8b662010-12-21 11:12:02 +01003615 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003616}
3617
Avi Kivity851ba692009-08-24 11:10:17 +03003618static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003619{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003620 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003621 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003622 bool has_error_code = false;
3623 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003624 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003625 int reason, type, idt_v;
3626
3627 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3628 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003629
3630 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3631
3632 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003633 if (reason == TASK_SWITCH_GATE && idt_v) {
3634 switch (type) {
3635 case INTR_TYPE_NMI_INTR:
3636 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02003637 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003638 break;
3639 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003640 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003641 kvm_clear_interrupt_queue(vcpu);
3642 break;
3643 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003644 if (vmx->idt_vectoring_info &
3645 VECTORING_INFO_DELIVER_CODE_MASK) {
3646 has_error_code = true;
3647 error_code =
3648 vmcs_read32(IDT_VECTORING_ERROR_CODE);
3649 }
3650 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003651 case INTR_TYPE_SOFT_EXCEPTION:
3652 kvm_clear_exception_queue(vcpu);
3653 break;
3654 default:
3655 break;
3656 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003657 }
Izik Eidus37817f22008-03-24 23:14:53 +02003658 tss_selector = exit_qualification;
3659
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003660 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3661 type != INTR_TYPE_EXT_INTR &&
3662 type != INTR_TYPE_NMI_INTR))
3663 skip_emulated_instruction(vcpu);
3664
Gleb Natapovacb54512010-04-15 21:03:50 +03003665 if (kvm_task_switch(vcpu, tss_selector, reason,
3666 has_error_code, error_code) == EMULATE_FAIL) {
3667 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3668 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3669 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003670 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03003671 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003672
3673 /* clear all local breakpoint enable flags */
3674 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3675
3676 /*
3677 * TODO: What about debug traps on tss switch?
3678 * Are we supposed to inject them and update dr6?
3679 */
3680
3681 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003682}
3683
Avi Kivity851ba692009-08-24 11:10:17 +03003684static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003685{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003686 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003687 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003688 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003689
Sheng Yangf9c617f2009-03-25 10:08:52 +08003690 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003691
3692 if (exit_qualification & (1 << 6)) {
3693 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003694 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003695 }
3696
3697 gla_validity = (exit_qualification >> 7) & 0x3;
3698 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3699 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3700 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3701 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003702 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003703 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3704 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003705 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3706 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003707 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003708 }
3709
3710 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003711 trace_kvm_page_fault(gpa, exit_qualification);
Andre Przywaradc25e892010-12-21 11:12:07 +01003712 return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003713}
3714
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003715static u64 ept_rsvd_mask(u64 spte, int level)
3716{
3717 int i;
3718 u64 mask = 0;
3719
3720 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3721 mask |= (1ULL << i);
3722
3723 if (level > 2)
3724 /* bits 7:3 reserved */
3725 mask |= 0xf8;
3726 else if (level == 2) {
3727 if (spte & (1ULL << 7))
3728 /* 2MB ref, bits 20:12 reserved */
3729 mask |= 0x1ff000;
3730 else
3731 /* bits 6:3 reserved */
3732 mask |= 0x78;
3733 }
3734
3735 return mask;
3736}
3737
3738static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3739 int level)
3740{
3741 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3742
3743 /* 010b (write-only) */
3744 WARN_ON((spte & 0x7) == 0x2);
3745
3746 /* 110b (write/execute) */
3747 WARN_ON((spte & 0x7) == 0x6);
3748
3749 /* 100b (execute-only) and value not supported by logical processor */
3750 if (!cpu_has_vmx_ept_execute_only())
3751 WARN_ON((spte & 0x7) == 0x4);
3752
3753 /* not 000b */
3754 if ((spte & 0x7)) {
3755 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3756
3757 if (rsvd_bits != 0) {
3758 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3759 __func__, rsvd_bits);
3760 WARN_ON(1);
3761 }
3762
3763 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3764 u64 ept_mem_type = (spte & 0x38) >> 3;
3765
3766 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3767 ept_mem_type == 7) {
3768 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3769 __func__, ept_mem_type);
3770 WARN_ON(1);
3771 }
3772 }
3773 }
3774}
3775
Avi Kivity851ba692009-08-24 11:10:17 +03003776static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003777{
3778 u64 sptes[4];
3779 int nr_sptes, i;
3780 gpa_t gpa;
3781
3782 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3783
3784 printk(KERN_ERR "EPT: Misconfiguration.\n");
3785 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3786
3787 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3788
3789 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3790 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3791
Avi Kivity851ba692009-08-24 11:10:17 +03003792 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3793 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003794
3795 return 0;
3796}
3797
Avi Kivity851ba692009-08-24 11:10:17 +03003798static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003799{
3800 u32 cpu_based_vm_exec_control;
3801
3802 /* clear pending NMI */
3803 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3804 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3805 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3806 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03003807 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003808
3809 return 1;
3810}
3811
Mohammed Gamal80ced182009-09-01 12:48:18 +02003812static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003813{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003814 struct vcpu_vmx *vmx = to_vmx(vcpu);
3815 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003816 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02003817 u32 cpu_exec_ctrl;
3818 bool intr_window_requested;
3819
3820 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3821 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003822
3823 while (!guest_state_valid(vcpu)) {
Avi Kivity49e9d552010-09-19 14:34:08 +02003824 if (intr_window_requested
3825 && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
3826 return handle_interrupt_window(&vmx->vcpu);
3827
Andre Przywara51d8b662010-12-21 11:12:02 +01003828 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003829
Mohammed Gamal80ced182009-09-01 12:48:18 +02003830 if (err == EMULATE_DO_MMIO) {
3831 ret = 0;
3832 goto out;
3833 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003834
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003835 if (err != EMULATE_DONE)
3836 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003837
3838 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003839 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003840 if (need_resched())
3841 schedule();
3842 }
3843
Mohammed Gamal80ced182009-09-01 12:48:18 +02003844 vmx->emulation_required = 0;
3845out:
3846 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003847}
3848
Avi Kivity6aa8b732006-12-10 02:21:36 -08003849/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003850 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3851 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3852 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003853static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003854{
3855 skip_emulated_instruction(vcpu);
3856 kvm_vcpu_on_spin(vcpu);
3857
3858 return 1;
3859}
3860
Sheng Yang59708672009-12-15 13:29:54 +08003861static int handle_invalid_op(struct kvm_vcpu *vcpu)
3862{
3863 kvm_queue_exception(vcpu, UD_VECTOR);
3864 return 1;
3865}
3866
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003867/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868 * The exit handlers return 1 if the exit was handled fully and guest execution
3869 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3870 * to be done to userspace and return 0.
3871 */
Avi Kivity851ba692009-08-24 11:10:17 +03003872static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3874 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003875 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003876 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003878 [EXIT_REASON_CR_ACCESS] = handle_cr,
3879 [EXIT_REASON_DR_ACCESS] = handle_dr,
3880 [EXIT_REASON_CPUID] = handle_cpuid,
3881 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3882 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3883 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3884 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003885 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003886 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003887 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003888 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3889 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3890 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3891 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3892 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3893 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3894 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3895 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3896 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003897 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3898 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003899 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08003900 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02003901 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003902 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003903 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3904 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003905 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003906 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3907 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003908};
3909
3910static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003911 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912
Avi Kivity586f9602010-11-18 13:09:54 +02003913static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3914{
3915 *info1 = vmcs_readl(EXIT_QUALIFICATION);
3916 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
3917}
3918
Avi Kivity6aa8b732006-12-10 02:21:36 -08003919/*
3920 * The guest has exited. See if we can fix it or if we need userspace
3921 * assistance.
3922 */
Avi Kivity851ba692009-08-24 11:10:17 +03003923static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003925 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003926 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003927 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003928
Avi Kivityaa179112010-11-17 18:44:19 +02003929 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003930
Mohammed Gamal80ced182009-09-01 12:48:18 +02003931 /* If guest state is invalid, start emulating */
3932 if (vmx->emulation_required && emulate_invalid_guest_state)
3933 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003934
Mohammed Gamal51207022010-05-31 22:40:54 +03003935 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
3936 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3937 vcpu->run->fail_entry.hardware_entry_failure_reason
3938 = exit_reason;
3939 return 0;
3940 }
3941
Avi Kivity29bd8a72007-09-10 17:27:03 +03003942 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003943 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3944 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003945 = vmcs_read32(VM_INSTRUCTION_ERROR);
3946 return 0;
3947 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003948
Mike Dayd77c26f2007-10-08 09:02:08 -04003949 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003950 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003951 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3952 exit_reason != EXIT_REASON_TASK_SWITCH))
3953 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3954 "(0x%x) and exit reason is 0x%x\n",
3955 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003956
3957 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003958 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003959 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003960 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003961 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003962 /*
3963 * This CPU don't support us in finding the end of an
3964 * NMI-blocked window if the guest runs with IRQs
3965 * disabled. So we pull the trigger after 1 s of
3966 * futile waiting, but inform the user about this.
3967 */
3968 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3969 "state on VCPU %d after 1 s timeout\n",
3970 __func__, vcpu->vcpu_id);
3971 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003972 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003973 }
3974
Avi Kivity6aa8b732006-12-10 02:21:36 -08003975 if (exit_reason < kvm_vmx_max_exit_handlers
3976 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003977 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003979 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3980 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981 }
3982 return 0;
3983}
3984
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003985static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003986{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003987 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003988 vmcs_write32(TPR_THRESHOLD, 0);
3989 return;
3990 }
3991
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003992 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003993}
3994
Avi Kivity51aa01d2010-07-20 14:31:20 +03003995static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03003996{
Avi Kivity00eba012011-03-07 17:24:54 +02003997 u32 exit_intr_info;
3998
3999 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
4000 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
4001 return;
4002
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004003 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02004004 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08004005
4006 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004007 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08004008 kvm_machine_check();
4009
Gleb Natapov20f65982009-05-11 13:35:55 +03004010 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004011 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004012 (exit_intr_info & INTR_INFO_VALID_MASK)) {
4013 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03004014 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004015 kvm_after_handle_nmi(&vmx->vcpu);
4016 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03004017}
Gleb Natapov20f65982009-05-11 13:35:55 +03004018
Avi Kivity51aa01d2010-07-20 14:31:20 +03004019static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
4020{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004021 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03004022 bool unblock_nmi;
4023 u8 vector;
4024 bool idtv_info_valid;
4025
4026 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03004027
Avi Kivitycf393f72008-07-01 16:20:21 +03004028 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02004029 if (vmx->nmi_known_unmasked)
4030 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004031 /*
4032 * Can't use vmx->exit_intr_info since we're not sure what
4033 * the exit reason is.
4034 */
4035 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03004036 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
4037 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
4038 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004039 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03004040 * Re-set bit "block by NMI" before VM entry if vmexit caused by
4041 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004042 * SDM 3: 23.2.2 (September 2008)
4043 * Bit 12 is undefined in any of the following cases:
4044 * If the VM exit sets the valid bit in the IDT-vectoring
4045 * information field.
4046 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03004047 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004048 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
4049 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03004050 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4051 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02004052 else
4053 vmx->nmi_known_unmasked =
4054 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
4055 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004056 } else if (unlikely(vmx->soft_vnmi_blocked))
4057 vmx->vnmi_blocked_time +=
4058 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03004059}
4060
Avi Kivity83422e12010-07-20 14:43:23 +03004061static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
4062 u32 idt_vectoring_info,
4063 int instr_len_field,
4064 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03004065{
Avi Kivity51aa01d2010-07-20 14:31:20 +03004066 u8 vector;
4067 int type;
4068 bool idtv_info_valid;
4069
4070 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03004071
Gleb Natapov37b96e92009-03-30 16:03:13 +03004072 vmx->vcpu.arch.nmi_injected = false;
4073 kvm_clear_exception_queue(&vmx->vcpu);
4074 kvm_clear_interrupt_queue(&vmx->vcpu);
4075
4076 if (!idtv_info_valid)
4077 return;
4078
Avi Kivity3842d132010-07-27 12:30:24 +03004079 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
4080
Avi Kivity668f6122008-07-02 09:28:55 +03004081 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
4082 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004083
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004084 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03004085 case INTR_TYPE_NMI_INTR:
4086 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03004087 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004088 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03004089 * Clear bit "block by NMI" before VM entry if a NMI
4090 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03004091 */
Avi Kivity654f06f2011-03-23 15:02:47 +02004092 vmx_set_nmi_mask(&vmx->vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004093 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004094 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004095 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004096 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004097 /* fall through */
4098 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03004099 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03004100 u32 err = vmcs_read32(error_code_field);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004101 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03004102 } else
4103 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004104 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004105 case INTR_TYPE_SOFT_INTR:
4106 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004107 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004108 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03004109 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004110 kvm_queue_interrupt(&vmx->vcpu, vector,
4111 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004112 break;
4113 default:
4114 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03004115 }
Avi Kivitycf393f72008-07-01 16:20:21 +03004116}
4117
Avi Kivity83422e12010-07-20 14:43:23 +03004118static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
4119{
4120 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
4121 VM_EXIT_INSTRUCTION_LEN,
4122 IDT_VECTORING_ERROR_CODE);
4123}
4124
Avi Kivityb463a6f2010-07-20 15:06:17 +03004125static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
4126{
4127 __vmx_complete_interrupts(to_vmx(vcpu),
4128 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
4129 VM_ENTRY_INSTRUCTION_LEN,
4130 VM_ENTRY_EXCEPTION_ERROR_CODE);
4131
4132 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
4133}
4134
Avi Kivityc8019492008-07-14 14:44:59 +03004135#ifdef CONFIG_X86_64
4136#define R "r"
4137#define Q "q"
4138#else
4139#define R "e"
4140#define Q "l"
4141#endif
4142
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08004143static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004144{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004145 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity104f2262010-11-18 13:12:52 +02004146
4147 /* Record the guest's net vcpu time for enforced NMI injections. */
4148 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
4149 vmx->entry_time = ktime_get();
4150
4151 /* Don't enter VMX if guest state is invalid, let the exit handler
4152 start emulation until we arrive back to a valid state */
4153 if (vmx->emulation_required && emulate_invalid_guest_state)
4154 return;
4155
4156 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
4157 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
4158 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
4159 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
4160
4161 /* When single-stepping over STI and MOV SS, we must clear the
4162 * corresponding interruptibility bits in the guest state. Otherwise
4163 * vmentry fails as it then expects bit 14 (BS) in pending debug
4164 * exceptions being set, but that's not correct for the guest debugging
4165 * case. */
4166 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
4167 vmx_set_interrupt_shadow(vcpu, 0);
4168
4169 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08004170 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03004171 "push %%"R"dx; push %%"R"bp;"
Avi Kivity40712fa2011-01-06 18:09:12 +02004172 "push %%"R"cx \n\t" /* placeholder for guest rcx */
Avi Kivityc8019492008-07-14 14:44:59 +03004173 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03004174 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
4175 "je 1f \n\t"
4176 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004177 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03004178 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03004179 /* Reload cr2 if changed */
4180 "mov %c[cr2](%0), %%"R"ax \n\t"
4181 "mov %%cr2, %%"R"dx \n\t"
4182 "cmp %%"R"ax, %%"R"dx \n\t"
4183 "je 2f \n\t"
4184 "mov %%"R"ax, %%cr2 \n\t"
4185 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004186 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02004187 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03004189 "mov %c[rax](%0), %%"R"ax \n\t"
4190 "mov %c[rbx](%0), %%"R"bx \n\t"
4191 "mov %c[rdx](%0), %%"R"dx \n\t"
4192 "mov %c[rsi](%0), %%"R"si \n\t"
4193 "mov %c[rdi](%0), %%"R"di \n\t"
4194 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004195#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004196 "mov %c[r8](%0), %%r8 \n\t"
4197 "mov %c[r9](%0), %%r9 \n\t"
4198 "mov %c[r10](%0), %%r10 \n\t"
4199 "mov %c[r11](%0), %%r11 \n\t"
4200 "mov %c[r12](%0), %%r12 \n\t"
4201 "mov %c[r13](%0), %%r13 \n\t"
4202 "mov %c[r14](%0), %%r14 \n\t"
4203 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004204#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004205 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
4206
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03004208 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004209 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004210 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004211 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004212 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213 /* Save guest registers, load host registers, keep flags */
Avi Kivity40712fa2011-01-06 18:09:12 +02004214 "mov %0, %c[wordsize](%%"R"sp) \n\t"
4215 "pop %0 \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004216 "mov %%"R"ax, %c[rax](%0) \n\t"
4217 "mov %%"R"bx, %c[rbx](%0) \n\t"
Avi Kivity1c696d02011-01-06 18:09:11 +02004218 "pop"Q" %c[rcx](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004219 "mov %%"R"dx, %c[rdx](%0) \n\t"
4220 "mov %%"R"si, %c[rsi](%0) \n\t"
4221 "mov %%"R"di, %c[rdi](%0) \n\t"
4222 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004223#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004224 "mov %%r8, %c[r8](%0) \n\t"
4225 "mov %%r9, %c[r9](%0) \n\t"
4226 "mov %%r10, %c[r10](%0) \n\t"
4227 "mov %%r11, %c[r11](%0) \n\t"
4228 "mov %%r12, %c[r12](%0) \n\t"
4229 "mov %%r13, %c[r13](%0) \n\t"
4230 "mov %%r14, %c[r14](%0) \n\t"
4231 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004232#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004233 "mov %%cr2, %%"R"ax \n\t"
4234 "mov %%"R"ax, %c[cr2](%0) \n\t"
4235
Avi Kivity1c696d02011-01-06 18:09:11 +02004236 "pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02004237 "setbe %c[fail](%0) \n\t"
4238 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
4239 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
4240 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03004241 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004242 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
4243 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
4244 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
4245 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
4246 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
4247 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
4248 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004249#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004250 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
4251 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
4252 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
4253 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
4254 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
4255 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
4256 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
4257 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02004259 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
4260 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02004261 : "cc", "memory"
Jan Kiszka07d6f552010-09-28 16:37:42 +02004262 , R"ax", R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02004263#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02004264 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
4265#endif
4266 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004267
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004268 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02004269 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02004270 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004271 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03004272 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004273 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004274 vcpu->arch.regs_dirty = 0;
4275
Avi Kivity1155f762007-11-22 11:30:47 +02004276 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
4277
Mike Dayd77c26f2007-10-08 09:02:08 -04004278 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03004279 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02004280
Avi Kivity51aa01d2010-07-20 14:31:20 +03004281 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03004282
4283 vmx_complete_atomic_exit(vmx);
4284 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03004285 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004286}
4287
Avi Kivityc8019492008-07-14 14:44:59 +03004288#undef R
4289#undef Q
4290
Avi Kivity6aa8b732006-12-10 02:21:36 -08004291static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
4292{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004293 struct vcpu_vmx *vmx = to_vmx(vcpu);
4294
4295 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03004296 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004297 free_vmcs(vmx->vmcs);
4298 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004299 }
4300}
4301
4302static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
4303{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004304 struct vcpu_vmx *vmx = to_vmx(vcpu);
4305
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004306 free_vpid(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004307 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004308 kfree(vmx->guest_msrs);
4309 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10004310 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004311}
4312
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08004313static inline void vmcs_init(struct vmcs *vmcs)
4314{
4315 u64 phys_addr = __pa(per_cpu(vmxarea, raw_smp_processor_id()));
4316
4317 if (!vmm_exclusive)
4318 kvm_cpu_vmxon(phys_addr);
4319
4320 vmcs_clear(vmcs);
4321
4322 if (!vmm_exclusive)
4323 kvm_cpu_vmxoff();
4324}
4325
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004326static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004327{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004328 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10004329 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03004330 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004332 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004333 return ERR_PTR(-ENOMEM);
4334
Sheng Yang2384d2b2008-01-17 15:14:33 +08004335 allocate_vpid(vmx);
4336
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004337 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
4338 if (err)
4339 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004340
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004341 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004342 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004343 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004344 goto uninit_vcpu;
4345 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08004346
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004347 vmx->vmcs = alloc_vmcs();
4348 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004349 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004350
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08004351 vmcs_init(vmx->vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004352
Avi Kivity15ad7142007-07-11 18:17:21 +03004353 cpu = get_cpu();
4354 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004355 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10004356 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004357 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004358 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004359 if (err)
4360 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004361 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004362 err = alloc_apic_access_page(kvm);
4363 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004364 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004365
Sheng Yangb927a3c2009-07-21 10:42:48 +08004366 if (enable_ept) {
4367 if (!kvm->arch.ept_identity_map_addr)
4368 kvm->arch.ept_identity_map_addr =
4369 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004370 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004371 if (alloc_identity_pagetable(kvm) != 0)
4372 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004373 if (!init_rmode_identity_map(kvm))
4374 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004375 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004376
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004377 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004378
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004379free_vmcs:
4380 free_vmcs(vmx->vmcs);
4381free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004382 kfree(vmx->guest_msrs);
4383uninit_vcpu:
4384 kvm_vcpu_uninit(&vmx->vcpu);
4385free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004386 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10004387 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004388 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004389}
4390
Yang, Sheng002c7f72007-07-31 14:23:01 +03004391static void __init vmx_check_processor_compat(void *rtn)
4392{
4393 struct vmcs_config vmcs_conf;
4394
4395 *(int *)rtn = 0;
4396 if (setup_vmcs_config(&vmcs_conf) < 0)
4397 *(int *)rtn = -EIO;
4398 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
4399 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
4400 smp_processor_id());
4401 *(int *)rtn = -EIO;
4402 }
4403}
4404
Sheng Yang67253af2008-04-25 10:20:22 +08004405static int get_ept_level(void)
4406{
4407 return VMX_EPT_DEFAULT_GAW + 1;
4408}
4409
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004410static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08004411{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004412 u64 ret;
4413
Sheng Yang522c68c2009-04-27 20:35:43 +08004414 /* For VT-d and EPT combination
4415 * 1. MMIO: always map as UC
4416 * 2. EPT with VT-d:
4417 * a. VT-d without snooping control feature: can't guarantee the
4418 * result, try to trust guest.
4419 * b. VT-d with snooping control feature: snooping control feature of
4420 * VT-d engine can guarantee the cache correctness. Just set it
4421 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08004422 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08004423 * consistent with host MTRR
4424 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004425 if (is_mmio)
4426 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08004427 else if (vcpu->kvm->arch.iommu_domain &&
4428 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
4429 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
4430 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004431 else
Sheng Yang522c68c2009-04-27 20:35:43 +08004432 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08004433 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004434
4435 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08004436}
4437
Avi Kivityf4c9e872009-12-28 16:06:35 +02004438#define _ER(x) { EXIT_REASON_##x, #x }
4439
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004440static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02004441 _ER(EXCEPTION_NMI),
4442 _ER(EXTERNAL_INTERRUPT),
4443 _ER(TRIPLE_FAULT),
4444 _ER(PENDING_INTERRUPT),
4445 _ER(NMI_WINDOW),
4446 _ER(TASK_SWITCH),
4447 _ER(CPUID),
4448 _ER(HLT),
4449 _ER(INVLPG),
4450 _ER(RDPMC),
4451 _ER(RDTSC),
4452 _ER(VMCALL),
4453 _ER(VMCLEAR),
4454 _ER(VMLAUNCH),
4455 _ER(VMPTRLD),
4456 _ER(VMPTRST),
4457 _ER(VMREAD),
4458 _ER(VMRESUME),
4459 _ER(VMWRITE),
4460 _ER(VMOFF),
4461 _ER(VMON),
4462 _ER(CR_ACCESS),
4463 _ER(DR_ACCESS),
4464 _ER(IO_INSTRUCTION),
4465 _ER(MSR_READ),
4466 _ER(MSR_WRITE),
4467 _ER(MWAIT_INSTRUCTION),
4468 _ER(MONITOR_INSTRUCTION),
4469 _ER(PAUSE_INSTRUCTION),
4470 _ER(MCE_DURING_VMENTRY),
4471 _ER(TPR_BELOW_THRESHOLD),
4472 _ER(APIC_ACCESS),
4473 _ER(EPT_VIOLATION),
4474 _ER(EPT_MISCONFIG),
4475 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004476 { -1, NULL }
4477};
4478
Avi Kivityf4c9e872009-12-28 16:06:35 +02004479#undef _ER
4480
Sheng Yang17cc3932010-01-05 19:02:27 +08004481static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004482{
Sheng Yang878403b2010-01-05 19:02:29 +08004483 if (enable_ept && !cpu_has_vmx_ept_1g_page())
4484 return PT_DIRECTORY_LEVEL;
4485 else
4486 /* For shadow and EPT supported 1GB page */
4487 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004488}
4489
Sheng Yang0e851882009-12-18 16:48:46 +08004490static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4491{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004492 struct kvm_cpuid_entry2 *best;
4493 struct vcpu_vmx *vmx = to_vmx(vcpu);
4494 u32 exec_control;
4495
4496 vmx->rdtscp_enabled = false;
4497 if (vmx_rdtscp_supported()) {
4498 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4499 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4500 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4501 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4502 vmx->rdtscp_enabled = true;
4503 else {
4504 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4505 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4506 exec_control);
4507 }
4508 }
4509 }
Sheng Yang0e851882009-12-18 16:48:46 +08004510}
4511
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004512static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4513{
4514}
4515
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004516static int vmx_check_intercept(struct kvm_vcpu *vcpu,
4517 struct x86_instruction_info *info,
4518 enum x86_intercept_stage stage)
4519{
4520 return X86EMUL_CONTINUE;
4521}
4522
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004523static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004524 .cpu_has_kvm_support = cpu_has_kvm_support,
4525 .disabled_by_bios = vmx_disabled_by_bios,
4526 .hardware_setup = hardware_setup,
4527 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004528 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004529 .hardware_enable = hardware_enable,
4530 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004531 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004532
4533 .vcpu_create = vmx_create_vcpu,
4534 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004535 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004536
Avi Kivity04d2cc72007-09-10 18:10:54 +03004537 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004538 .vcpu_load = vmx_vcpu_load,
4539 .vcpu_put = vmx_vcpu_put,
4540
4541 .set_guest_debug = set_guest_debug,
4542 .get_msr = vmx_get_msr,
4543 .set_msr = vmx_set_msr,
4544 .get_segment_base = vmx_get_segment_base,
4545 .get_segment = vmx_get_segment,
4546 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004547 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004548 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004549 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004550 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004551 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004552 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004553 .set_cr3 = vmx_set_cr3,
4554 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004555 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004556 .get_idt = vmx_get_idt,
4557 .set_idt = vmx_set_idt,
4558 .get_gdt = vmx_get_gdt,
4559 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004560 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004561 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004562 .get_rflags = vmx_get_rflags,
4563 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02004564 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004565 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004566
4567 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004568
Avi Kivity6aa8b732006-12-10 02:21:36 -08004569 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004570 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004571 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004572 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4573 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004574 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004575 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004576 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004577 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004578 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004579 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004580 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004581 .get_nmi_mask = vmx_get_nmi_mask,
4582 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004583 .enable_nmi_window = enable_nmi_window,
4584 .enable_irq_window = enable_irq_window,
4585 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004586
Izik Eiduscbc94022007-10-25 00:29:55 +02004587 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004588 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004589 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004590
Avi Kivity586f9602010-11-18 13:09:54 +02004591 .get_exit_info = vmx_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004592 .exit_reasons_str = vmx_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02004593
Sheng Yang17cc3932010-01-05 19:02:27 +08004594 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004595
4596 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004597
4598 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004599
4600 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004601
4602 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004603
Joerg Roedel4051b182011-03-25 09:44:49 +01004604 .set_tsc_khz = vmx_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004605 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004606 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004607 .compute_tsc_offset = vmx_compute_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004608
4609 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004610
4611 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612};
4613
4614static int __init vmx_init(void)
4615{
Avi Kivity26bb0982009-09-07 11:14:12 +03004616 int r, i;
4617
4618 rdmsrl_safe(MSR_EFER, &host_efer);
4619
4620 for (i = 0; i < NR_VMX_MSR; ++i)
4621 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004622
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004623 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004624 if (!vmx_io_bitmap_a)
4625 return -ENOMEM;
4626
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004627 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004628 if (!vmx_io_bitmap_b) {
4629 r = -ENOMEM;
4630 goto out;
4631 }
4632
Avi Kivity58972972009-02-24 22:26:47 +02004633 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4634 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004635 r = -ENOMEM;
4636 goto out1;
4637 }
4638
Avi Kivity58972972009-02-24 22:26:47 +02004639 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4640 if (!vmx_msr_bitmap_longmode) {
4641 r = -ENOMEM;
4642 goto out2;
4643 }
4644
He, Qingfdef3ad2007-04-30 09:45:24 +03004645 /*
4646 * Allow direct access to the PC debug port (it is often used for I/O
4647 * delays, but the vmexits simply slow things down).
4648 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004649 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4650 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004651
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004652 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004653
Avi Kivity58972972009-02-24 22:26:47 +02004654 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4655 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004656
Sheng Yang2384d2b2008-01-17 15:14:33 +08004657 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4658
Avi Kivity0ee75be2010-04-28 15:39:01 +03004659 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
4660 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004661 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004662 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004663
Avi Kivity58972972009-02-24 22:26:47 +02004664 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4665 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4666 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4667 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4668 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4669 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004670
Avi Kivity089d0342009-03-23 18:26:32 +02004671 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004672 bypass_guest_pf = 0;
Sheng Yang534e38b2008-09-08 15:12:30 +08004673 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004674 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004675 kvm_enable_tdp();
4676 } else
4677 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004678
Avi Kivityc7addb92007-09-16 18:58:32 +02004679 if (bypass_guest_pf)
4680 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4681
He, Qingfdef3ad2007-04-30 09:45:24 +03004682 return 0;
4683
Avi Kivity58972972009-02-24 22:26:47 +02004684out3:
4685 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004686out2:
Avi Kivity58972972009-02-24 22:26:47 +02004687 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004688out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004689 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004690out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004691 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004692 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004693}
4694
4695static void __exit vmx_exit(void)
4696{
Avi Kivity58972972009-02-24 22:26:47 +02004697 free_page((unsigned long)vmx_msr_bitmap_legacy);
4698 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004699 free_page((unsigned long)vmx_io_bitmap_b);
4700 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004701
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004702 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004703}
4704
4705module_init(vmx_init)
4706module_exit(vmx_exit)