blob: 875b785228f6a78b95c7b7b1f32012494dc3249e [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030030#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030031#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040032
Avi Kivity6aa8b732006-12-10 02:21:36 -080033#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080034#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020035#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020036#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080037#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080038
Marcelo Tosatti229456f2009-06-17 09:22:14 -030039#include "trace.h"
40
Avi Kivity4ecac3f2008-05-13 13:23:38 +030041#define __ex(x) __kvm_handle_fault_on_reboot(x)
42
Avi Kivity6aa8b732006-12-10 02:21:36 -080043MODULE_AUTHOR("Qumranet");
44MODULE_LICENSE("GPL");
45
Avi Kivity4462d212009-03-23 17:53:37 +020046static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020047module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020048
Avi Kivity4462d212009-03-23 17:53:37 +020049static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020050module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080051
Avi Kivity4462d212009-03-23 17:53:37 +020052static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020053module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020054
Avi Kivity4462d212009-03-23 17:53:37 +020055static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020056module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080057
Nitin A Kamble3a624e22009-06-08 11:34:16 -070058static int __read_mostly enable_unrestricted_guest = 1;
59module_param_named(unrestricted_guest,
60 enable_unrestricted_guest, bool, S_IRUGO);
61
Avi Kivity4462d212009-03-23 17:53:37 +020062static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020063module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030064
Avi Kivitycdc0e242009-12-06 17:21:14 +020065#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
66 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
67#define KVM_GUEST_CR0_MASK \
68 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
69#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity81231c62010-01-24 16:26:40 +020070 (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +020071#define KVM_VM_CR0_ALWAYS_ON \
72 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +020073#define KVM_CR4_GUEST_OWNED_BITS \
74 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
75 | X86_CR4_OSXMMEXCPT)
76
Avi Kivitycdc0e242009-12-06 17:21:14 +020077#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
78#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
79
Avi Kivity78ac8b42010-04-08 18:19:35 +030080#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
81
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080082/*
83 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
84 * ple_gap: upper bound on the amount of time between two successive
85 * executions of PAUSE in a loop. Also indicate if ple enabled.
86 * According to test, this time is usually small than 41 cycles.
87 * ple_window: upper bound on the amount of time a guest is allowed to execute
88 * in a PAUSE loop. Tests indicate that most spinlocks are held for
89 * less than 2^12 cycles
90 * Time is measured based on a counter that runs at the same rate as the TSC,
91 * refer SDM volume 3b section 21.6.13 & 22.1.3.
92 */
93#define KVM_VMX_DEFAULT_PLE_GAP 41
94#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
95static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
96module_param(ple_gap, int, S_IRUGO);
97
98static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
99module_param(ple_window, int, S_IRUGO);
100
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400101struct vmcs {
102 u32 revision_id;
103 u32 abort;
104 char data[0];
105};
106
Avi Kivity26bb0982009-09-07 11:14:12 +0300107struct shared_msr_entry {
108 unsigned index;
109 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200110 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300111};
112
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400113struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000114 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +0300115 struct list_head local_vcpus_link;
Avi Kivity313dbd492008-07-17 18:04:30 +0300116 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400117 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300118 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +0200119 u32 idt_vectoring_info;
Avi Kivity26bb0982009-09-07 11:14:12 +0300120 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400121 int nmsrs;
122 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400123#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300124 u64 msr_host_kernel_gs_base;
125 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400126#endif
127 struct vmcs *vmcs;
128 struct {
129 int loaded;
130 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200131 int gs_ldt_reload_needed;
132 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400133 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200134 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300135 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300136 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300137 struct kvm_save_segment {
138 u16 selector;
139 unsigned long base;
140 u32 limit;
141 u32 ar;
142 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200143 struct {
144 bool pending;
145 u8 vector;
146 unsigned rip;
147 } irq;
148 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800149 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300150 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200151
152 /* Support for vnmi-less CPUs */
153 int soft_vnmi_blocked;
154 ktime_t entry_time;
155 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800156 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800157
158 bool rdtscp_enabled;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400159};
160
161static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
162{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000163 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400164}
165
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800166static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800167static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300168
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169static DEFINE_PER_CPU(struct vmcs *, vmxarea);
170static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300171static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800172
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200173static unsigned long *vmx_io_bitmap_a;
174static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200175static unsigned long *vmx_msr_bitmap_legacy;
176static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300177
Sheng Yang2384d2b2008-01-17 15:14:33 +0800178static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
179static DEFINE_SPINLOCK(vmx_vpid_lock);
180
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300181static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800182 int size;
183 int order;
184 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300185 u32 pin_based_exec_ctrl;
186 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800187 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300188 u32 vmexit_ctrl;
189 u32 vmentry_ctrl;
190} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191
Hannes Ederefff9e52008-11-28 17:02:06 +0100192static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800193 u32 ept;
194 u32 vpid;
195} vmx_capability;
196
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197#define VMX_SEGMENT_FIELD(seg) \
198 [VCPU_SREG_##seg] = { \
199 .selector = GUEST_##seg##_SELECTOR, \
200 .base = GUEST_##seg##_BASE, \
201 .limit = GUEST_##seg##_LIMIT, \
202 .ar_bytes = GUEST_##seg##_AR_BYTES, \
203 }
204
205static struct kvm_vmx_segment_field {
206 unsigned selector;
207 unsigned base;
208 unsigned limit;
209 unsigned ar_bytes;
210} kvm_vmx_segment_fields[] = {
211 VMX_SEGMENT_FIELD(CS),
212 VMX_SEGMENT_FIELD(DS),
213 VMX_SEGMENT_FIELD(ES),
214 VMX_SEGMENT_FIELD(FS),
215 VMX_SEGMENT_FIELD(GS),
216 VMX_SEGMENT_FIELD(SS),
217 VMX_SEGMENT_FIELD(TR),
218 VMX_SEGMENT_FIELD(LDTR),
219};
220
Avi Kivity26bb0982009-09-07 11:14:12 +0300221static u64 host_efer;
222
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300223static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
224
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300225/*
226 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
227 * away by decrementing the array size.
228 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800230#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300231 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800232#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800233 MSR_EFER, MSR_TSC_AUX, MSR_K6_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200235#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800236
Gui Jianfeng31299942010-03-15 17:29:09 +0800237static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238{
239 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
240 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100241 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242}
243
Gui Jianfeng31299942010-03-15 17:29:09 +0800244static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300245{
246 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
247 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100248 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300249}
250
Gui Jianfeng31299942010-03-15 17:29:09 +0800251static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500252{
253 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
254 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100255 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500256}
257
Gui Jianfeng31299942010-03-15 17:29:09 +0800258static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800259{
260 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
261 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
262}
263
Gui Jianfeng31299942010-03-15 17:29:09 +0800264static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800265{
266 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
267 INTR_INFO_VALID_MASK)) ==
268 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
269}
270
Gui Jianfeng31299942010-03-15 17:29:09 +0800271static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800272{
Sheng Yang04547152009-04-01 15:52:31 +0800273 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800274}
275
Gui Jianfeng31299942010-03-15 17:29:09 +0800276static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800277{
Sheng Yang04547152009-04-01 15:52:31 +0800278 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800279}
280
Gui Jianfeng31299942010-03-15 17:29:09 +0800281static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800282{
Sheng Yang04547152009-04-01 15:52:31 +0800283 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800284}
285
Gui Jianfeng31299942010-03-15 17:29:09 +0800286static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800287{
Sheng Yang04547152009-04-01 15:52:31 +0800288 return vmcs_config.cpu_based_exec_ctrl &
289 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800290}
291
Avi Kivity774ead32007-12-26 13:57:04 +0200292static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800293{
Sheng Yang04547152009-04-01 15:52:31 +0800294 return vmcs_config.cpu_based_2nd_exec_ctrl &
295 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
296}
297
298static inline bool cpu_has_vmx_flexpriority(void)
299{
300 return cpu_has_vmx_tpr_shadow() &&
301 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800302}
303
Marcelo Tosattie7997942009-06-11 12:07:40 -0300304static inline bool cpu_has_vmx_ept_execute_only(void)
305{
Gui Jianfeng31299942010-03-15 17:29:09 +0800306 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300307}
308
309static inline bool cpu_has_vmx_eptp_uncacheable(void)
310{
Gui Jianfeng31299942010-03-15 17:29:09 +0800311 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300312}
313
314static inline bool cpu_has_vmx_eptp_writeback(void)
315{
Gui Jianfeng31299942010-03-15 17:29:09 +0800316 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300317}
318
319static inline bool cpu_has_vmx_ept_2m_page(void)
320{
Gui Jianfeng31299942010-03-15 17:29:09 +0800321 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300322}
323
Sheng Yang878403b2010-01-05 19:02:29 +0800324static inline bool cpu_has_vmx_ept_1g_page(void)
325{
Gui Jianfeng31299942010-03-15 17:29:09 +0800326 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800327}
328
Gui Jianfeng31299942010-03-15 17:29:09 +0800329static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800330{
Gui Jianfeng31299942010-03-15 17:29:09 +0800331 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800332}
333
Gui Jianfeng31299942010-03-15 17:29:09 +0800334static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800335{
Gui Jianfeng31299942010-03-15 17:29:09 +0800336 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800337}
338
Gui Jianfeng31299942010-03-15 17:29:09 +0800339static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800340{
Gui Jianfeng31299942010-03-15 17:29:09 +0800341 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800342}
343
Gui Jianfeng31299942010-03-15 17:29:09 +0800344static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800345{
Sheng Yang04547152009-04-01 15:52:31 +0800346 return vmcs_config.cpu_based_2nd_exec_ctrl &
347 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800348}
349
Gui Jianfeng31299942010-03-15 17:29:09 +0800350static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700351{
352 return vmcs_config.cpu_based_2nd_exec_ctrl &
353 SECONDARY_EXEC_UNRESTRICTED_GUEST;
354}
355
Gui Jianfeng31299942010-03-15 17:29:09 +0800356static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800357{
358 return vmcs_config.cpu_based_2nd_exec_ctrl &
359 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
360}
361
Gui Jianfeng31299942010-03-15 17:29:09 +0800362static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800363{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800364 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800365}
366
Gui Jianfeng31299942010-03-15 17:29:09 +0800367static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800368{
Sheng Yang04547152009-04-01 15:52:31 +0800369 return vmcs_config.cpu_based_2nd_exec_ctrl &
370 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800371}
372
Gui Jianfeng31299942010-03-15 17:29:09 +0800373static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800374{
375 return vmcs_config.cpu_based_2nd_exec_ctrl &
376 SECONDARY_EXEC_RDTSCP;
377}
378
Gui Jianfeng31299942010-03-15 17:29:09 +0800379static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800380{
381 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
382}
383
Sheng Yang04547152009-04-01 15:52:31 +0800384static inline bool report_flexpriority(void)
385{
386 return flexpriority_enabled;
387}
388
Rusty Russell8b9cf982007-07-30 16:31:43 +1000389static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800390{
391 int i;
392
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400393 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300394 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300395 return i;
396 return -1;
397}
398
Sheng Yang2384d2b2008-01-17 15:14:33 +0800399static inline void __invvpid(int ext, u16 vpid, gva_t gva)
400{
401 struct {
402 u64 vpid : 16;
403 u64 rsvd : 48;
404 u64 gva;
405 } operand = { vpid, 0, gva };
406
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300407 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800408 /* CF==1 or ZF==1 --> rc = -1 */
409 "; ja 1f ; ud2 ; 1:"
410 : : "a"(&operand), "c"(ext) : "cc", "memory");
411}
412
Sheng Yang14394422008-04-28 12:24:45 +0800413static inline void __invept(int ext, u64 eptp, gpa_t gpa)
414{
415 struct {
416 u64 eptp, gpa;
417 } operand = {eptp, gpa};
418
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300419 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800420 /* CF==1 or ZF==1 --> rc = -1 */
421 "; ja 1f ; ud2 ; 1:\n"
422 : : "a" (&operand), "c" (ext) : "cc", "memory");
423}
424
Avi Kivity26bb0982009-09-07 11:14:12 +0300425static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300426{
427 int i;
428
Rusty Russell8b9cf982007-07-30 16:31:43 +1000429 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300430 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400431 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000432 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800433}
434
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435static void vmcs_clear(struct vmcs *vmcs)
436{
437 u64 phys_addr = __pa(vmcs);
438 u8 error;
439
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300440 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
442 : "cc", "memory");
443 if (error)
444 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
445 vmcs, phys_addr);
446}
447
448static void __vcpu_clear(void *arg)
449{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000450 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800451 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452
Rusty Russell8b9cf982007-07-30 16:31:43 +1000453 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400454 vmcs_clear(vmx->vmcs);
455 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800457 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300458 list_del(&vmx->local_vcpus_link);
459 vmx->vcpu.cpu = -1;
460 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461}
462
Rusty Russell8b9cf982007-07-30 16:31:43 +1000463static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800464{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200465 if (vmx->vcpu.cpu == -1)
466 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200467 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800468}
469
Sheng Yang2384d2b2008-01-17 15:14:33 +0800470static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
471{
472 if (vmx->vpid == 0)
473 return;
474
475 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
476}
477
Sheng Yang14394422008-04-28 12:24:45 +0800478static inline void ept_sync_global(void)
479{
480 if (cpu_has_vmx_invept_global())
481 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
482}
483
484static inline void ept_sync_context(u64 eptp)
485{
Avi Kivity089d0342009-03-23 18:26:32 +0200486 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800487 if (cpu_has_vmx_invept_context())
488 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
489 else
490 ept_sync_global();
491 }
492}
493
494static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
495{
Avi Kivity089d0342009-03-23 18:26:32 +0200496 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800497 if (cpu_has_vmx_invept_individual_addr())
498 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
499 eptp, gpa);
500 else
501 ept_sync_context(eptp);
502 }
503}
504
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505static unsigned long vmcs_readl(unsigned long field)
506{
507 unsigned long value;
508
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300509 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510 : "=a"(value) : "d"(field) : "cc");
511 return value;
512}
513
514static u16 vmcs_read16(unsigned long field)
515{
516 return vmcs_readl(field);
517}
518
519static u32 vmcs_read32(unsigned long field)
520{
521 return vmcs_readl(field);
522}
523
524static u64 vmcs_read64(unsigned long field)
525{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800526#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 return vmcs_readl(field);
528#else
529 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
530#endif
531}
532
Avi Kivitye52de1b2007-01-05 16:36:56 -0800533static noinline void vmwrite_error(unsigned long field, unsigned long value)
534{
535 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
536 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
537 dump_stack();
538}
539
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540static void vmcs_writel(unsigned long field, unsigned long value)
541{
542 u8 error;
543
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300544 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400545 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800546 if (unlikely(error))
547 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548}
549
550static void vmcs_write16(unsigned long field, u16 value)
551{
552 vmcs_writel(field, value);
553}
554
555static void vmcs_write32(unsigned long field, u32 value)
556{
557 vmcs_writel(field, value);
558}
559
560static void vmcs_write64(unsigned long field, u64 value)
561{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300563#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564 asm volatile ("");
565 vmcs_writel(field+1, value >> 32);
566#endif
567}
568
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300569static void vmcs_clear_bits(unsigned long field, u32 mask)
570{
571 vmcs_writel(field, vmcs_readl(field) & ~mask);
572}
573
574static void vmcs_set_bits(unsigned long field, u32 mask)
575{
576 vmcs_writel(field, vmcs_readl(field) | mask);
577}
578
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300579static void update_exception_bitmap(struct kvm_vcpu *vcpu)
580{
581 u32 eb;
582
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100583 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
584 (1u << NM_VECTOR) | (1u << DB_VECTOR);
585 if ((vcpu->guest_debug &
586 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
587 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
588 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300589 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300590 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200591 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800592 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200593 if (vcpu->fpu_active)
594 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300595 vmcs_write32(EXCEPTION_BITMAP, eb);
596}
597
Avi Kivity33ed6322007-05-02 16:54:03 +0300598static void reload_tss(void)
599{
Avi Kivity33ed6322007-05-02 16:54:03 +0300600 /*
601 * VT restores TR but not its size. Useless.
602 */
Gleb Natapov89a27f42010-02-16 10:51:48 +0200603 struct desc_ptr gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200604 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300605
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200606 native_store_gdt(&gdt);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200607 descs = (void *)gdt.address;
Avi Kivity33ed6322007-05-02 16:54:03 +0300608 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
609 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300610}
611
Avi Kivity92c0d902009-10-29 11:00:16 +0200612static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300613{
Roel Kluin3a34a882009-08-04 02:08:45 -0700614 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300615 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300616
Avi Kivityf6801df2010-01-21 15:31:50 +0200617 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700618
Avi Kivity51c6cf62007-08-29 03:48:05 +0300619 /*
620 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
621 * outside long mode
622 */
623 ignore_bits = EFER_NX | EFER_SCE;
624#ifdef CONFIG_X86_64
625 ignore_bits |= EFER_LMA | EFER_LME;
626 /* SCE is meaningful only in long mode on Intel */
627 if (guest_efer & EFER_LMA)
628 ignore_bits &= ~(u64)EFER_SCE;
629#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300630 guest_efer &= ~ignore_bits;
631 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300632 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200633 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300634 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300635}
636
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200637static unsigned long segment_base(u16 selector)
638{
639 struct desc_ptr gdt;
640 struct desc_struct *d;
641 unsigned long table_base;
642 unsigned long v;
643
644 if (!(selector & ~3))
645 return 0;
646
647 native_store_gdt(&gdt);
648 table_base = gdt.address;
649
650 if (selector & 4) { /* from ldt */
651 u16 ldt_selector = kvm_read_ldt();
652
653 if (!(ldt_selector & ~3))
654 return 0;
655
656 table_base = segment_base(ldt_selector);
657 }
658 d = (struct desc_struct *)(table_base + (selector & ~7));
659 v = get_desc_base(d);
660#ifdef CONFIG_X86_64
661 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
662 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
663#endif
664 return v;
665}
666
667static inline unsigned long kvm_read_tr_base(void)
668{
669 u16 tr;
670 asm("str %0" : "=g"(tr));
671 return segment_base(tr);
672}
673
Avi Kivity04d2cc72007-09-10 18:10:54 +0300674static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300675{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300676 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300677 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300678
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400679 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300680 return;
681
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400682 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300683 /*
684 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
685 * allow segment selectors with cpl > 0 or ti == 1.
686 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300687 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200688 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300689 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200690 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400691 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200692 vmx->host_state.fs_reload_needed = 0;
693 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300694 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200695 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300696 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300697 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400698 if (!(vmx->host_state.gs_sel & 7))
699 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300700 else {
701 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200702 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300703 }
704
705#ifdef CONFIG_X86_64
706 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
707 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
708#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400709 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
710 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300711#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300712
713#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300714 if (is_long_mode(&vmx->vcpu)) {
715 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
716 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
717 }
Avi Kivity707c0872007-05-02 17:33:43 +0300718#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300719 for (i = 0; i < vmx->save_nmsrs; ++i)
720 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200721 vmx->guest_msrs[i].data,
722 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300723}
724
Avi Kivitya9b21b62008-06-24 11:48:49 +0300725static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300726{
Avi Kivity15ad7142007-07-11 18:17:21 +0300727 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300728
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400729 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300730 return;
731
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200732 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400733 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200734 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300735 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200736 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300737 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300738 /*
739 * If we have to reload gs, we must take care to
740 * preserve our gs base.
741 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300742 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300743 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300744#ifdef CONFIG_X86_64
745 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
746#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300747 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300748 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200749 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300750#ifdef CONFIG_X86_64
751 if (is_long_mode(&vmx->vcpu)) {
752 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
753 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
754 }
755#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300756}
757
Avi Kivitya9b21b62008-06-24 11:48:49 +0300758static void vmx_load_host_state(struct vcpu_vmx *vmx)
759{
760 preempt_disable();
761 __vmx_load_host_state(vmx);
762 preempt_enable();
763}
764
Avi Kivity6aa8b732006-12-10 02:21:36 -0800765/*
766 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
767 * vcpu mutex is already taken.
768 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300769static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400771 struct vcpu_vmx *vmx = to_vmx(vcpu);
772 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200773 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800774
Eddie Donga3d7f852007-09-03 16:15:12 +0300775 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000776 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300777 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300778 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300779 local_irq_disable();
780 list_add(&vmx->local_vcpus_link,
781 &per_cpu(vcpus_on_cpu, cpu));
782 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300783 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400785 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 u8 error;
787
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400788 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300789 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
791 : "cc");
792 if (error)
793 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400794 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 }
796
797 if (vcpu->cpu != cpu) {
Gleb Natapov89a27f42010-02-16 10:51:48 +0200798 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 unsigned long sysenter_esp;
800
801 vcpu->cpu = cpu;
802 /*
803 * Linux uses per-cpu TSS and GDT, so set these when switching
804 * processors.
805 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300806 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200807 native_store_gdt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200808 vmcs_writel(HOST_GDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800809
810 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
811 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300812
813 /*
814 * Make sure the time stamp counter is monotonous.
815 */
816 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200817 if (tsc_this < vcpu->arch.host_tsc) {
818 delta = vcpu->arch.host_tsc - tsc_this;
819 new_offset = vmcs_read64(TSC_OFFSET) + delta;
820 vmcs_write64(TSC_OFFSET, new_offset);
821 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823}
824
825static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
826{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300827 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828}
829
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300830static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
831{
Avi Kivity81231c62010-01-24 16:26:40 +0200832 ulong cr0;
833
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300834 if (vcpu->fpu_active)
835 return;
836 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +0200837 cr0 = vmcs_readl(GUEST_CR0);
838 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
839 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
840 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300841 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +0200842 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
843 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300844}
845
Avi Kivityedcafe32009-12-30 18:07:40 +0200846static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
847
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300848static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
849{
Avi Kivityedcafe32009-12-30 18:07:40 +0200850 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +0200851 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300852 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +0200853 vcpu->arch.cr0_guest_owned_bits = 0;
854 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
855 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300856}
857
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
859{
Avi Kivity78ac8b42010-04-08 18:19:35 +0300860 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +0300861
862 rflags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +0300863 if (to_vmx(vcpu)->rmode.vm86_active) {
864 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
865 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
866 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
867 }
Avi Kivity345dcaa2009-08-12 15:29:37 +0300868 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800869}
870
871static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
872{
Avi Kivity78ac8b42010-04-08 18:19:35 +0300873 if (to_vmx(vcpu)->rmode.vm86_active) {
874 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100875 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300876 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800877 vmcs_writel(GUEST_RFLAGS, rflags);
878}
879
Glauber Costa2809f5d2009-05-12 16:21:05 -0400880static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
881{
882 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
883 int ret = 0;
884
885 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +0100886 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400887 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +0100888 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400889
890 return ret & mask;
891}
892
893static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
894{
895 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
896 u32 interruptibility = interruptibility_old;
897
898 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
899
Jan Kiszka48005f62010-02-19 19:38:07 +0100900 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400901 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +0100902 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400903 interruptibility |= GUEST_INTR_STATE_STI;
904
905 if ((interruptibility != interruptibility_old))
906 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
907}
908
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
910{
911 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300913 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800914 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300915 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800916
Glauber Costa2809f5d2009-05-12 16:21:05 -0400917 /* skipping an emulated instruction also counts */
918 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800919}
920
Avi Kivity298101d2007-11-25 13:41:11 +0200921static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200922 bool has_error_code, u32 error_code,
923 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +0200924{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200925 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100926 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200927
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100928 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200929 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100930 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
931 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200932
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300933 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200934 vmx->rmode.irq.pending = true;
935 vmx->rmode.irq.vector = nr;
936 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300937 if (kvm_exception_is_soft(nr))
938 vmx->rmode.irq.rip +=
939 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100940 intr_info |= INTR_TYPE_SOFT_INTR;
941 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200942 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
943 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
944 return;
945 }
946
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300947 if (kvm_exception_is_soft(nr)) {
948 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
949 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100950 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
951 } else
952 intr_info |= INTR_TYPE_HARD_EXCEPTION;
953
954 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200955}
956
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800957static bool vmx_rdtscp_supported(void)
958{
959 return cpu_has_vmx_rdtscp();
960}
961
Avi Kivity6aa8b732006-12-10 02:21:36 -0800962/*
Eddie Donga75beee2007-05-17 18:55:15 +0300963 * Swap MSR entry in host/guest MSR entry array.
964 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000965static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300966{
Avi Kivity26bb0982009-09-07 11:14:12 +0300967 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968
969 tmp = vmx->guest_msrs[to];
970 vmx->guest_msrs[to] = vmx->guest_msrs[from];
971 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300972}
973
974/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300975 * Set up the vmcs to automatically save and restore system
976 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
977 * mode, as fiddling with msrs is very expensive.
978 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000979static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300980{
Avi Kivity26bb0982009-09-07 11:14:12 +0300981 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +0200982 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300983
Avi Kivity33f9c502008-02-27 16:06:57 +0200984 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300985 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300986#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000987 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000988 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300989 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000990 move_msr_up(vmx, index, save_nmsrs++);
991 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300992 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000993 move_msr_up(vmx, index, save_nmsrs++);
994 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300995 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000996 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800997 index = __find_msr_index(vmx, MSR_TSC_AUX);
998 if (index >= 0 && vmx->rdtscp_enabled)
999 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001000 /*
1001 * MSR_K6_STAR is only needed on long mode guests, and only
1002 * if efer.sce is enabled.
1003 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001004 index = __find_msr_index(vmx, MSR_K6_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001005 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001006 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001007 }
Eddie Donga75beee2007-05-17 18:55:15 +03001008#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001009 index = __find_msr_index(vmx, MSR_EFER);
1010 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001011 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001012
Avi Kivity26bb0982009-09-07 11:14:12 +03001013 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001014
1015 if (cpu_has_vmx_msr_bitmap()) {
1016 if (is_long_mode(&vmx->vcpu))
1017 msr_bitmap = vmx_msr_bitmap_longmode;
1018 else
1019 msr_bitmap = vmx_msr_bitmap_legacy;
1020
1021 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1022 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001023}
1024
1025/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026 * reads and returns guest's timestamp counter "register"
1027 * guest_tsc = host_tsc + tsc_offset -- 21.3
1028 */
1029static u64 guest_read_tsc(void)
1030{
1031 u64 host_tsc, tsc_offset;
1032
1033 rdtscll(host_tsc);
1034 tsc_offset = vmcs_read64(TSC_OFFSET);
1035 return host_tsc + tsc_offset;
1036}
1037
1038/*
1039 * writes 'guest_tsc' into guest's timestamp counter "register"
1040 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
1041 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001042static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001044 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
1045}
1046
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047/*
1048 * Reads an msr value (of 'msr_index') into 'pdata'.
1049 * Returns 0 on success, non-0 otherwise.
1050 * Assumes vcpu_load() was already called.
1051 */
1052static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1053{
1054 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001055 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056
1057 if (!pdata) {
1058 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1059 return -EINVAL;
1060 }
1061
1062 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001063#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064 case MSR_FS_BASE:
1065 data = vmcs_readl(GUEST_FS_BASE);
1066 break;
1067 case MSR_GS_BASE:
1068 data = vmcs_readl(GUEST_GS_BASE);
1069 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001070 case MSR_KERNEL_GS_BASE:
1071 vmx_load_host_state(to_vmx(vcpu));
1072 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1073 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001074#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001076 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301077 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078 data = guest_read_tsc();
1079 break;
1080 case MSR_IA32_SYSENTER_CS:
1081 data = vmcs_read32(GUEST_SYSENTER_CS);
1082 break;
1083 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001084 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001085 break;
1086 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001087 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001088 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001089 case MSR_TSC_AUX:
1090 if (!to_vmx(vcpu)->rdtscp_enabled)
1091 return 1;
1092 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001093 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001094 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001095 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001096 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001097 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001098 data = msr->data;
1099 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001101 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102 }
1103
1104 *pdata = data;
1105 return 0;
1106}
1107
1108/*
1109 * Writes msr value into into the appropriate "register".
1110 * Returns 0 on success, non-0 otherwise.
1111 * Assumes vcpu_load() was already called.
1112 */
1113static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1114{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001115 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001116 struct shared_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001117 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001118 int ret = 0;
1119
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001121 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001122 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001123 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001124 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001125#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126 case MSR_FS_BASE:
1127 vmcs_writel(GUEST_FS_BASE, data);
1128 break;
1129 case MSR_GS_BASE:
1130 vmcs_writel(GUEST_GS_BASE, data);
1131 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001132 case MSR_KERNEL_GS_BASE:
1133 vmx_load_host_state(vmx);
1134 vmx->msr_guest_kernel_gs_base = data;
1135 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136#endif
1137 case MSR_IA32_SYSENTER_CS:
1138 vmcs_write32(GUEST_SYSENTER_CS, data);
1139 break;
1140 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001141 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 break;
1143 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001144 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301146 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001147 rdtscll(host_tsc);
1148 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001150 case MSR_IA32_CR_PAT:
1151 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1152 vmcs_write64(GUEST_IA32_PAT, data);
1153 vcpu->arch.pat = data;
1154 break;
1155 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001156 ret = kvm_set_msr_common(vcpu, msr_index, data);
1157 break;
1158 case MSR_TSC_AUX:
1159 if (!vmx->rdtscp_enabled)
1160 return 1;
1161 /* Check reserved bit, higher 32 bits should be zero */
1162 if ((data >> 32) != 0)
1163 return 1;
1164 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001166 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001167 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001168 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001169 msr->data = data;
1170 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001172 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 }
1174
Eddie Dong2cc51562007-05-21 07:28:09 +03001175 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176}
1177
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001178static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001180 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1181 switch (reg) {
1182 case VCPU_REGS_RSP:
1183 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1184 break;
1185 case VCPU_REGS_RIP:
1186 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1187 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001188 case VCPU_EXREG_PDPTR:
1189 if (enable_ept)
1190 ept_save_pdptrs(vcpu);
1191 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001192 default:
1193 break;
1194 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195}
1196
Jan Kiszka355be0b2009-10-03 00:31:21 +02001197static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001199 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1200 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1201 else
1202 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1203
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001204 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205}
1206
1207static __init int cpu_has_kvm_support(void)
1208{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001209 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001210}
1211
1212static __init int vmx_disabled_by_bios(void)
1213{
1214 u64 msr;
1215
1216 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001217 return (msr & (FEATURE_CONTROL_LOCKED |
1218 FEATURE_CONTROL_VMXON_ENABLED))
1219 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001220 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221}
1222
Alexander Graf10474ae2009-09-15 11:37:46 +02001223static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001224{
1225 int cpu = raw_smp_processor_id();
1226 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1227 u64 old;
1228
Alexander Graf10474ae2009-09-15 11:37:46 +02001229 if (read_cr4() & X86_CR4_VMXE)
1230 return -EBUSY;
1231
Avi Kivity543e4242008-05-13 16:22:47 +03001232 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001233 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001234 if ((old & (FEATURE_CONTROL_LOCKED |
1235 FEATURE_CONTROL_VMXON_ENABLED))
1236 != (FEATURE_CONTROL_LOCKED |
1237 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001239 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001240 FEATURE_CONTROL_LOCKED |
1241 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001242 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001243 asm volatile (ASM_VMX_VMXON_RAX
1244 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001246
1247 ept_sync_global();
1248
1249 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250}
1251
Avi Kivity543e4242008-05-13 16:22:47 +03001252static void vmclear_local_vcpus(void)
1253{
1254 int cpu = raw_smp_processor_id();
1255 struct vcpu_vmx *vmx, *n;
1256
1257 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1258 local_vcpus_link)
1259 __vcpu_clear(vmx);
1260}
1261
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001262
1263/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1264 * tricks.
1265 */
1266static void kvm_cpu_vmxoff(void)
1267{
1268 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1269 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1270}
1271
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272static void hardware_disable(void *garbage)
1273{
Avi Kivity543e4242008-05-13 16:22:47 +03001274 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001275 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276}
1277
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001278static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001279 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280{
1281 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001282 u32 ctl = ctl_min | ctl_opt;
1283
1284 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1285
1286 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1287 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1288
1289 /* Ensure minimum (required) set of control bits are supported. */
1290 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001291 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001292
1293 *result = ctl;
1294 return 0;
1295}
1296
Yang, Sheng002c7f72007-07-31 14:23:01 +03001297static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001298{
1299 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001300 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001301 u32 _pin_based_exec_control = 0;
1302 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001303 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001304 u32 _vmexit_control = 0;
1305 u32 _vmentry_control = 0;
1306
1307 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001308 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001309 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1310 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001311 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001312
1313 min = CPU_BASED_HLT_EXITING |
1314#ifdef CONFIG_X86_64
1315 CPU_BASED_CR8_LOAD_EXITING |
1316 CPU_BASED_CR8_STORE_EXITING |
1317#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001318 CPU_BASED_CR3_LOAD_EXITING |
1319 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001320 CPU_BASED_USE_IO_BITMAPS |
1321 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001322 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001323 CPU_BASED_MWAIT_EXITING |
1324 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001325 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001326 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001327 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001328 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001329 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1330 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001331 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001332#ifdef CONFIG_X86_64
1333 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1334 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1335 ~CPU_BASED_CR8_STORE_EXITING;
1336#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001337 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001338 min2 = 0;
1339 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001340 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001341 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001342 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001343 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001344 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1345 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001346 if (adjust_vmx_controls(min2, opt2,
1347 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001348 &_cpu_based_2nd_exec_control) < 0)
1349 return -EIO;
1350 }
1351#ifndef CONFIG_X86_64
1352 if (!(_cpu_based_2nd_exec_control &
1353 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1354 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1355#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001356 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001357 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1358 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001359 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1360 CPU_BASED_CR3_STORE_EXITING |
1361 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001362 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1363 vmx_capability.ept, vmx_capability.vpid);
1364 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001365
1366 min = 0;
1367#ifdef CONFIG_X86_64
1368 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1369#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001370 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001371 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1372 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001373 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001374
Sheng Yang468d4722008-10-09 16:01:55 +08001375 min = 0;
1376 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001377 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1378 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001379 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001381 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001382
1383 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1384 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001385 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001386
1387#ifdef CONFIG_X86_64
1388 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1389 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001390 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001391#endif
1392
1393 /* Require Write-Back (WB) memory type for VMCS accesses. */
1394 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001395 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001396
Yang, Sheng002c7f72007-07-31 14:23:01 +03001397 vmcs_conf->size = vmx_msr_high & 0x1fff;
1398 vmcs_conf->order = get_order(vmcs_config.size);
1399 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001400
Yang, Sheng002c7f72007-07-31 14:23:01 +03001401 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1402 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001403 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001404 vmcs_conf->vmexit_ctrl = _vmexit_control;
1405 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001406
1407 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001408}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001409
1410static struct vmcs *alloc_vmcs_cpu(int cpu)
1411{
1412 int node = cpu_to_node(cpu);
1413 struct page *pages;
1414 struct vmcs *vmcs;
1415
Mel Gorman6484eb32009-06-16 15:31:54 -07001416 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417 if (!pages)
1418 return NULL;
1419 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001420 memset(vmcs, 0, vmcs_config.size);
1421 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001422 return vmcs;
1423}
1424
1425static struct vmcs *alloc_vmcs(void)
1426{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001427 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428}
1429
1430static void free_vmcs(struct vmcs *vmcs)
1431{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001432 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433}
1434
Sam Ravnborg39959582007-06-01 00:47:13 -07001435static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436{
1437 int cpu;
1438
Zachary Amsden3230bb42009-09-29 11:38:37 -10001439 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001441 per_cpu(vmxarea, cpu) = NULL;
1442 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443}
1444
Avi Kivity6aa8b732006-12-10 02:21:36 -08001445static __init int alloc_kvm_area(void)
1446{
1447 int cpu;
1448
Zachary Amsden3230bb42009-09-29 11:38:37 -10001449 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450 struct vmcs *vmcs;
1451
1452 vmcs = alloc_vmcs_cpu(cpu);
1453 if (!vmcs) {
1454 free_kvm_area();
1455 return -ENOMEM;
1456 }
1457
1458 per_cpu(vmxarea, cpu) = vmcs;
1459 }
1460 return 0;
1461}
1462
1463static __init int hardware_setup(void)
1464{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001465 if (setup_vmcs_config(&vmcs_config) < 0)
1466 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001467
1468 if (boot_cpu_has(X86_FEATURE_NX))
1469 kvm_enable_efer_bits(EFER_NX);
1470
Sheng Yang93ba03c2009-04-01 15:52:32 +08001471 if (!cpu_has_vmx_vpid())
1472 enable_vpid = 0;
1473
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001474 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001475 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001476 enable_unrestricted_guest = 0;
1477 }
1478
1479 if (!cpu_has_vmx_unrestricted_guest())
1480 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001481
1482 if (!cpu_has_vmx_flexpriority())
1483 flexpriority_enabled = 0;
1484
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001485 if (!cpu_has_vmx_tpr_shadow())
1486 kvm_x86_ops->update_cr8_intercept = NULL;
1487
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001488 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1489 kvm_disable_largepages();
1490
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001491 if (!cpu_has_vmx_ple())
1492 ple_gap = 0;
1493
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494 return alloc_kvm_area();
1495}
1496
1497static __exit void hardware_unsetup(void)
1498{
1499 free_kvm_area();
1500}
1501
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1503{
1504 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1505
Avi Kivity6af11b92007-03-19 13:18:10 +02001506 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 vmcs_write16(sf->selector, save->selector);
1508 vmcs_writel(sf->base, save->base);
1509 vmcs_write32(sf->limit, save->limit);
1510 vmcs_write32(sf->ar_bytes, save->ar);
1511 } else {
1512 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1513 << AR_DPL_SHIFT;
1514 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1515 }
1516}
1517
1518static void enter_pmode(struct kvm_vcpu *vcpu)
1519{
1520 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001521 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001523 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001524 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001526 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1527 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1528 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001529
1530 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001531 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1532 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533 vmcs_writel(GUEST_RFLAGS, flags);
1534
Rusty Russell66aee912007-07-17 23:34:16 +10001535 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1536 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537
1538 update_exception_bitmap(vcpu);
1539
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001540 if (emulate_invalid_guest_state)
1541 return;
1542
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001543 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1544 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1545 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1546 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001547
1548 vmcs_write16(GUEST_SS_SELECTOR, 0);
1549 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1550
1551 vmcs_write16(GUEST_CS_SELECTOR,
1552 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1553 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1554}
1555
Mike Dayd77c26f2007-10-08 09:02:08 -04001556static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001558 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001559 struct kvm_memslots *slots;
1560 gfn_t base_gfn;
1561
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001562 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001563 base_gfn = kvm->memslots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001564 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02001565 return base_gfn << PAGE_SHIFT;
1566 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001567 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568}
1569
1570static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1571{
1572 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1573
1574 save->selector = vmcs_read16(sf->selector);
1575 save->base = vmcs_readl(sf->base);
1576 save->limit = vmcs_read32(sf->limit);
1577 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001578 vmcs_write16(sf->selector, save->base >> 4);
1579 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580 vmcs_write32(sf->limit, 0xffff);
1581 vmcs_write32(sf->ar_bytes, 0xf3);
1582}
1583
1584static void enter_rmode(struct kvm_vcpu *vcpu)
1585{
1586 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001587 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001589 if (enable_unrestricted_guest)
1590 return;
1591
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001592 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001593 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001595 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1597
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001598 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001599 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1600
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001601 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1603
1604 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001605 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001607 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608
1609 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001610 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001611 update_exception_bitmap(vcpu);
1612
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001613 if (emulate_invalid_guest_state)
1614 goto continue_rmode;
1615
Avi Kivity6aa8b732006-12-10 02:21:36 -08001616 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1617 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1618 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1619
1620 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001621 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001622 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1623 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1625
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001626 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1627 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1628 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1629 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001630
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001631continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001632 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001633 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634}
1635
Amit Shah401d10d2009-02-20 22:53:37 +05301636static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1637{
1638 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001639 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1640
1641 if (!msr)
1642 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301643
Avi Kivity44ea2b12009-09-06 15:55:37 +03001644 /*
1645 * Force kernel_gs_base reloading before EFER changes, as control
1646 * of this msr depends on is_long_mode().
1647 */
1648 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02001649 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05301650 if (efer & EFER_LMA) {
1651 vmcs_write32(VM_ENTRY_CONTROLS,
1652 vmcs_read32(VM_ENTRY_CONTROLS) |
1653 VM_ENTRY_IA32E_MODE);
1654 msr->data = efer;
1655 } else {
1656 vmcs_write32(VM_ENTRY_CONTROLS,
1657 vmcs_read32(VM_ENTRY_CONTROLS) &
1658 ~VM_ENTRY_IA32E_MODE);
1659
1660 msr->data = efer & ~EFER_LME;
1661 }
1662 setup_msrs(vmx);
1663}
1664
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001665#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001666
1667static void enter_lmode(struct kvm_vcpu *vcpu)
1668{
1669 u32 guest_tr_ar;
1670
1671 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1672 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1673 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001674 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675 vmcs_write32(GUEST_TR_AR_BYTES,
1676 (guest_tr_ar & ~AR_TYPE_MASK)
1677 | AR_TYPE_BUSY_64_TSS);
1678 }
Avi Kivityf6801df2010-01-21 15:31:50 +02001679 vcpu->arch.efer |= EFER_LMA;
1680 vmx_set_efer(vcpu, vcpu->arch.efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681}
1682
1683static void exit_lmode(struct kvm_vcpu *vcpu)
1684{
Avi Kivityf6801df2010-01-21 15:31:50 +02001685 vcpu->arch.efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686
1687 vmcs_write32(VM_ENTRY_CONTROLS,
1688 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001689 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690}
1691
1692#endif
1693
Sheng Yang2384d2b2008-01-17 15:14:33 +08001694static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1695{
1696 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001697 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001698 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001699}
1700
Avi Kivitye8467fd2009-12-29 18:43:06 +02001701static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1702{
1703 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
1704
1705 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
1706 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
1707}
1708
Anthony Liguori25c4c272007-04-27 09:29:21 +03001709static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001710{
Avi Kivityfc78f512009-12-07 12:16:48 +02001711 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
1712
1713 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
1714 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08001715}
1716
Sheng Yang14394422008-04-28 12:24:45 +08001717static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1718{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001719 if (!test_bit(VCPU_EXREG_PDPTR,
1720 (unsigned long *)&vcpu->arch.regs_dirty))
1721 return;
1722
Sheng Yang14394422008-04-28 12:24:45 +08001723 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001724 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1725 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1726 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1727 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1728 }
1729}
1730
Avi Kivity8f5d5492009-05-31 18:41:29 +03001731static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1732{
1733 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1734 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1735 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1736 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1737 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1738 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001739
1740 __set_bit(VCPU_EXREG_PDPTR,
1741 (unsigned long *)&vcpu->arch.regs_avail);
1742 __set_bit(VCPU_EXREG_PDPTR,
1743 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001744}
1745
Sheng Yang14394422008-04-28 12:24:45 +08001746static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1747
1748static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1749 unsigned long cr0,
1750 struct kvm_vcpu *vcpu)
1751{
1752 if (!(cr0 & X86_CR0_PG)) {
1753 /* From paging/starting to nonpaging */
1754 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001755 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001756 (CPU_BASED_CR3_LOAD_EXITING |
1757 CPU_BASED_CR3_STORE_EXITING));
1758 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001759 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001760 } else if (!is_paging(vcpu)) {
1761 /* From nonpaging to paging */
1762 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001763 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001764 ~(CPU_BASED_CR3_LOAD_EXITING |
1765 CPU_BASED_CR3_STORE_EXITING));
1766 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001767 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001768 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001769
1770 if (!(cr0 & X86_CR0_WP))
1771 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001772}
1773
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1775{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001776 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001777 unsigned long hw_cr0;
1778
1779 if (enable_unrestricted_guest)
1780 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1781 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1782 else
1783 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001784
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001785 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001786 enter_pmode(vcpu);
1787
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001788 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789 enter_rmode(vcpu);
1790
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001791#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001792 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001793 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001794 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001795 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001796 exit_lmode(vcpu);
1797 }
1798#endif
1799
Avi Kivity089d0342009-03-23 18:26:32 +02001800 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001801 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1802
Avi Kivity02daab22009-12-30 12:40:26 +02001803 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02001804 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02001805
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001807 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001808 vcpu->arch.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001809}
1810
Sheng Yang14394422008-04-28 12:24:45 +08001811static u64 construct_eptp(unsigned long root_hpa)
1812{
1813 u64 eptp;
1814
1815 /* TODO write the value reading from MSR */
1816 eptp = VMX_EPT_DEFAULT_MT |
1817 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1818 eptp |= (root_hpa & PAGE_MASK);
1819
1820 return eptp;
1821}
1822
Avi Kivity6aa8b732006-12-10 02:21:36 -08001823static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1824{
Sheng Yang14394422008-04-28 12:24:45 +08001825 unsigned long guest_cr3;
1826 u64 eptp;
1827
1828 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001829 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001830 eptp = construct_eptp(cr3);
1831 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001832 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001833 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02001834 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001835 }
1836
Sheng Yang2384d2b2008-01-17 15:14:33 +08001837 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001838 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001839}
1840
1841static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1842{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001843 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001844 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1845
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001846 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02001847 if (enable_ept) {
1848 if (!is_paging(vcpu)) {
1849 hw_cr4 &= ~X86_CR4_PAE;
1850 hw_cr4 |= X86_CR4_PSE;
1851 } else if (!(cr4 & X86_CR4_PAE)) {
1852 hw_cr4 &= ~X86_CR4_PAE;
1853 }
1854 }
Sheng Yang14394422008-04-28 12:24:45 +08001855
1856 vmcs_writel(CR4_READ_SHADOW, cr4);
1857 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858}
1859
Avi Kivity6aa8b732006-12-10 02:21:36 -08001860static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1861{
1862 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1863
1864 return vmcs_readl(sf->base);
1865}
1866
1867static void vmx_get_segment(struct kvm_vcpu *vcpu,
1868 struct kvm_segment *var, int seg)
1869{
1870 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1871 u32 ar;
1872
1873 var->base = vmcs_readl(sf->base);
1874 var->limit = vmcs_read32(sf->limit);
1875 var->selector = vmcs_read16(sf->selector);
1876 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001877 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878 ar = 0;
1879 var->type = ar & 15;
1880 var->s = (ar >> 4) & 1;
1881 var->dpl = (ar >> 5) & 3;
1882 var->present = (ar >> 7) & 1;
1883 var->avl = (ar >> 12) & 1;
1884 var->l = (ar >> 13) & 1;
1885 var->db = (ar >> 14) & 1;
1886 var->g = (ar >> 15) & 1;
1887 var->unusable = (ar >> 16) & 1;
1888}
1889
Izik Eidus2e4d2652008-03-24 19:38:34 +02001890static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1891{
Avi Kivity3eeb3282010-01-21 15:31:48 +02001892 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02001893 return 0;
1894
1895 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1896 return 3;
1897
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001898 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001899}
1900
Avi Kivity653e3102007-05-07 10:55:37 +03001901static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903 u32 ar;
1904
Avi Kivity653e3102007-05-07 10:55:37 +03001905 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 ar = 1 << 16;
1907 else {
1908 ar = var->type & 15;
1909 ar |= (var->s & 1) << 4;
1910 ar |= (var->dpl & 3) << 5;
1911 ar |= (var->present & 1) << 7;
1912 ar |= (var->avl & 1) << 12;
1913 ar |= (var->l & 1) << 13;
1914 ar |= (var->db & 1) << 14;
1915 ar |= (var->g & 1) << 15;
1916 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001917 if (ar == 0) /* a 0 value means unusable */
1918 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001919
1920 return ar;
1921}
1922
1923static void vmx_set_segment(struct kvm_vcpu *vcpu,
1924 struct kvm_segment *var, int seg)
1925{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001926 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001927 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1928 u32 ar;
1929
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001930 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1931 vmx->rmode.tr.selector = var->selector;
1932 vmx->rmode.tr.base = var->base;
1933 vmx->rmode.tr.limit = var->limit;
1934 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001935 return;
1936 }
1937 vmcs_writel(sf->base, var->base);
1938 vmcs_write32(sf->limit, var->limit);
1939 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001940 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001941 /*
1942 * Hack real-mode segments into vm86 compatibility.
1943 */
1944 if (var->base == 0xffff0000 && var->selector == 0xf000)
1945 vmcs_writel(sf->base, 0xf0000);
1946 ar = 0xf3;
1947 } else
1948 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001949
1950 /*
1951 * Fix the "Accessed" bit in AR field of segment registers for older
1952 * qemu binaries.
1953 * IA32 arch specifies that at the time of processor reset the
1954 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1955 * is setting it to 0 in the usedland code. This causes invalid guest
1956 * state vmexit when "unrestricted guest" mode is turned on.
1957 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1958 * tree. Newer qemu binaries with that qemu fix would not need this
1959 * kvm hack.
1960 */
1961 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1962 ar |= 0x1; /* Accessed */
1963
Avi Kivity6aa8b732006-12-10 02:21:36 -08001964 vmcs_write32(sf->ar_bytes, ar);
1965}
1966
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1968{
1969 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1970
1971 *db = (ar >> 14) & 1;
1972 *l = (ar >> 13) & 1;
1973}
1974
Gleb Natapov89a27f42010-02-16 10:51:48 +02001975static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976{
Gleb Natapov89a27f42010-02-16 10:51:48 +02001977 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
1978 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979}
1980
Gleb Natapov89a27f42010-02-16 10:51:48 +02001981static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001982{
Gleb Natapov89a27f42010-02-16 10:51:48 +02001983 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
1984 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985}
1986
Gleb Natapov89a27f42010-02-16 10:51:48 +02001987static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001988{
Gleb Natapov89a27f42010-02-16 10:51:48 +02001989 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
1990 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001991}
1992
Gleb Natapov89a27f42010-02-16 10:51:48 +02001993static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001994{
Gleb Natapov89a27f42010-02-16 10:51:48 +02001995 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
1996 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997}
1998
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001999static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2000{
2001 struct kvm_segment var;
2002 u32 ar;
2003
2004 vmx_get_segment(vcpu, &var, seg);
2005 ar = vmx_segment_access_rights(&var);
2006
2007 if (var.base != (var.selector << 4))
2008 return false;
2009 if (var.limit != 0xffff)
2010 return false;
2011 if (ar != 0xf3)
2012 return false;
2013
2014 return true;
2015}
2016
2017static bool code_segment_valid(struct kvm_vcpu *vcpu)
2018{
2019 struct kvm_segment cs;
2020 unsigned int cs_rpl;
2021
2022 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2023 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2024
Avi Kivity1872a3f2009-01-04 23:26:52 +02002025 if (cs.unusable)
2026 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002027 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2028 return false;
2029 if (!cs.s)
2030 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002031 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002032 if (cs.dpl > cs_rpl)
2033 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002034 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002035 if (cs.dpl != cs_rpl)
2036 return false;
2037 }
2038 if (!cs.present)
2039 return false;
2040
2041 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2042 return true;
2043}
2044
2045static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2046{
2047 struct kvm_segment ss;
2048 unsigned int ss_rpl;
2049
2050 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2051 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2052
Avi Kivity1872a3f2009-01-04 23:26:52 +02002053 if (ss.unusable)
2054 return true;
2055 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002056 return false;
2057 if (!ss.s)
2058 return false;
2059 if (ss.dpl != ss_rpl) /* DPL != RPL */
2060 return false;
2061 if (!ss.present)
2062 return false;
2063
2064 return true;
2065}
2066
2067static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2068{
2069 struct kvm_segment var;
2070 unsigned int rpl;
2071
2072 vmx_get_segment(vcpu, &var, seg);
2073 rpl = var.selector & SELECTOR_RPL_MASK;
2074
Avi Kivity1872a3f2009-01-04 23:26:52 +02002075 if (var.unusable)
2076 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002077 if (!var.s)
2078 return false;
2079 if (!var.present)
2080 return false;
2081 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2082 if (var.dpl < rpl) /* DPL < RPL */
2083 return false;
2084 }
2085
2086 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2087 * rights flags
2088 */
2089 return true;
2090}
2091
2092static bool tr_valid(struct kvm_vcpu *vcpu)
2093{
2094 struct kvm_segment tr;
2095
2096 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2097
Avi Kivity1872a3f2009-01-04 23:26:52 +02002098 if (tr.unusable)
2099 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002100 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2101 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002102 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002103 return false;
2104 if (!tr.present)
2105 return false;
2106
2107 return true;
2108}
2109
2110static bool ldtr_valid(struct kvm_vcpu *vcpu)
2111{
2112 struct kvm_segment ldtr;
2113
2114 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2115
Avi Kivity1872a3f2009-01-04 23:26:52 +02002116 if (ldtr.unusable)
2117 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002118 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2119 return false;
2120 if (ldtr.type != 2)
2121 return false;
2122 if (!ldtr.present)
2123 return false;
2124
2125 return true;
2126}
2127
2128static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2129{
2130 struct kvm_segment cs, ss;
2131
2132 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2133 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2134
2135 return ((cs.selector & SELECTOR_RPL_MASK) ==
2136 (ss.selector & SELECTOR_RPL_MASK));
2137}
2138
2139/*
2140 * Check if guest state is valid. Returns true if valid, false if
2141 * not.
2142 * We assume that registers are always usable
2143 */
2144static bool guest_state_valid(struct kvm_vcpu *vcpu)
2145{
2146 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02002147 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002148 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2149 return false;
2150 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2151 return false;
2152 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2153 return false;
2154 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2155 return false;
2156 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2157 return false;
2158 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2159 return false;
2160 } else {
2161 /* protected mode guest state checks */
2162 if (!cs_ss_rpl_check(vcpu))
2163 return false;
2164 if (!code_segment_valid(vcpu))
2165 return false;
2166 if (!stack_segment_valid(vcpu))
2167 return false;
2168 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2169 return false;
2170 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2171 return false;
2172 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2173 return false;
2174 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2175 return false;
2176 if (!tr_valid(vcpu))
2177 return false;
2178 if (!ldtr_valid(vcpu))
2179 return false;
2180 }
2181 /* TODO:
2182 * - Add checks on RIP
2183 * - Add checks on RFLAGS
2184 */
2185
2186 return true;
2187}
2188
Mike Dayd77c26f2007-10-08 09:02:08 -04002189static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002190{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002191 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002192 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002193 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002194 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002195
Izik Eidus195aefd2007-10-01 22:14:18 +02002196 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2197 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002198 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002199 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002200 r = kvm_write_guest_page(kvm, fn++, &data,
2201 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002202 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002203 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002204 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2205 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002206 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002207 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2208 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002209 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002210 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002211 r = kvm_write_guest_page(kvm, fn, &data,
2212 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2213 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002214 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002215 goto out;
2216
2217 ret = 1;
2218out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002219 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002220}
2221
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002222static int init_rmode_identity_map(struct kvm *kvm)
2223{
2224 int i, r, ret;
2225 pfn_t identity_map_pfn;
2226 u32 tmp;
2227
Avi Kivity089d0342009-03-23 18:26:32 +02002228 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002229 return 1;
2230 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2231 printk(KERN_ERR "EPT: identity-mapping pagetable "
2232 "haven't been allocated!\n");
2233 return 0;
2234 }
2235 if (likely(kvm->arch.ept_identity_pagetable_done))
2236 return 1;
2237 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002238 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002239 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2240 if (r < 0)
2241 goto out;
2242 /* Set up identity-mapping pagetable for EPT in real mode */
2243 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2244 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2245 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2246 r = kvm_write_guest_page(kvm, identity_map_pfn,
2247 &tmp, i * sizeof(tmp), sizeof(tmp));
2248 if (r < 0)
2249 goto out;
2250 }
2251 kvm->arch.ept_identity_pagetable_done = true;
2252 ret = 1;
2253out:
2254 return ret;
2255}
2256
Avi Kivity6aa8b732006-12-10 02:21:36 -08002257static void seg_setup(int seg)
2258{
2259 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002260 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002261
2262 vmcs_write16(sf->selector, 0);
2263 vmcs_writel(sf->base, 0);
2264 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002265 if (enable_unrestricted_guest) {
2266 ar = 0x93;
2267 if (seg == VCPU_SREG_CS)
2268 ar |= 0x08; /* code segment */
2269 } else
2270 ar = 0xf3;
2271
2272 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273}
2274
Sheng Yangf78e0e22007-10-29 09:40:42 +08002275static int alloc_apic_access_page(struct kvm *kvm)
2276{
2277 struct kvm_userspace_memory_region kvm_userspace_mem;
2278 int r = 0;
2279
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002280 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002281 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002282 goto out;
2283 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2284 kvm_userspace_mem.flags = 0;
2285 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2286 kvm_userspace_mem.memory_size = PAGE_SIZE;
2287 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2288 if (r)
2289 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002290
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002291 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002292out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002293 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002294 return r;
2295}
2296
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002297static int alloc_identity_pagetable(struct kvm *kvm)
2298{
2299 struct kvm_userspace_memory_region kvm_userspace_mem;
2300 int r = 0;
2301
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002302 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002303 if (kvm->arch.ept_identity_pagetable)
2304 goto out;
2305 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2306 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002307 kvm_userspace_mem.guest_phys_addr =
2308 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002309 kvm_userspace_mem.memory_size = PAGE_SIZE;
2310 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2311 if (r)
2312 goto out;
2313
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002314 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002315 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002316out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002317 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002318 return r;
2319}
2320
Sheng Yang2384d2b2008-01-17 15:14:33 +08002321static void allocate_vpid(struct vcpu_vmx *vmx)
2322{
2323 int vpid;
2324
2325 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002326 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002327 return;
2328 spin_lock(&vmx_vpid_lock);
2329 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2330 if (vpid < VMX_NR_VPIDS) {
2331 vmx->vpid = vpid;
2332 __set_bit(vpid, vmx_vpid_bitmap);
2333 }
2334 spin_unlock(&vmx_vpid_lock);
2335}
2336
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08002337static void free_vpid(struct vcpu_vmx *vmx)
2338{
2339 if (!enable_vpid)
2340 return;
2341 spin_lock(&vmx_vpid_lock);
2342 if (vmx->vpid != 0)
2343 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2344 spin_unlock(&vmx_vpid_lock);
2345}
2346
Avi Kivity58972972009-02-24 22:26:47 +02002347static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002348{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002349 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002350
2351 if (!cpu_has_vmx_msr_bitmap())
2352 return;
2353
2354 /*
2355 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2356 * have the write-low and read-high bitmap offsets the wrong way round.
2357 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2358 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002359 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002360 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2361 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002362 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2363 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002364 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2365 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002366 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002367}
2368
Avi Kivity58972972009-02-24 22:26:47 +02002369static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2370{
2371 if (!longmode_only)
2372 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2373 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2374}
2375
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376/*
2377 * Sets up the vmcs for emulated real mode.
2378 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002379static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380{
Sheng Yang468d4722008-10-09 16:01:55 +08002381 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002382 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002383 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002384 unsigned long a;
Gleb Natapov89a27f42010-02-16 10:51:48 +02002385 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002387 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002388 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002389
Avi Kivity6aa8b732006-12-10 02:21:36 -08002390 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002391 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2392 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393
Sheng Yang25c5f222008-03-28 13:18:56 +08002394 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002395 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002396
Avi Kivity6aa8b732006-12-10 02:21:36 -08002397 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2398
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002400 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2401 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002402
2403 exec_control = vmcs_config.cpu_based_exec_ctrl;
2404 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2405 exec_control &= ~CPU_BASED_TPR_SHADOW;
2406#ifdef CONFIG_X86_64
2407 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2408 CPU_BASED_CR8_LOAD_EXITING;
2409#endif
2410 }
Avi Kivity089d0342009-03-23 18:26:32 +02002411 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002412 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002413 CPU_BASED_CR3_LOAD_EXITING |
2414 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002415 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002416
Sheng Yang83ff3b92007-11-21 14:33:25 +08002417 if (cpu_has_secondary_exec_ctrls()) {
2418 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2419 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2420 exec_control &=
2421 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002422 if (vmx->vpid == 0)
2423 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002424 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002425 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002426 enable_unrestricted_guest = 0;
2427 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002428 if (!enable_unrestricted_guest)
2429 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002430 if (!ple_gap)
2431 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002432 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2433 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002434
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002435 if (ple_gap) {
2436 vmcs_write32(PLE_GAP, ple_gap);
2437 vmcs_write32(PLE_WINDOW, ple_window);
2438 }
2439
Avi Kivityc7addb92007-09-16 18:58:32 +02002440 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2441 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2443
2444 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2445 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2446 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2447
2448 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2449 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2450 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002451 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2452 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002454#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 rdmsrl(MSR_FS_BASE, a);
2456 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2457 rdmsrl(MSR_GS_BASE, a);
2458 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2459#else
2460 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2461 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2462#endif
2463
2464 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2465
Wei Yongjunec687982010-03-05 12:11:48 +08002466 native_store_idt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02002467 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468
Mike Dayd77c26f2007-10-08 09:02:08 -04002469 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002470 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002471 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2472 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2473 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474
2475 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2476 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2477 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2478 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2479 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2480 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2481
Sheng Yang468d4722008-10-09 16:01:55 +08002482 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2483 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2484 host_pat = msr_low | ((u64) msr_high << 32);
2485 vmcs_write64(HOST_IA32_PAT, host_pat);
2486 }
2487 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2488 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2489 host_pat = msr_low | ((u64) msr_high << 32);
2490 /* Write the default value follow host pat */
2491 vmcs_write64(GUEST_IA32_PAT, host_pat);
2492 /* Keep arch.pat sync with GUEST_IA32_PAT */
2493 vmx->vcpu.arch.pat = host_pat;
2494 }
2495
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496 for (i = 0; i < NR_VMX_MSR; ++i) {
2497 u32 index = vmx_msr_index[i];
2498 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002499 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002500
2501 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2502 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002503 if (wrmsr_safe(index, data_low, data_high) < 0)
2504 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03002505 vmx->guest_msrs[j].index = i;
2506 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002507 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002508 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002509 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002510
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002511 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002512
2513 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002514 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2515
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002516 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02002517 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02002518 if (enable_ept)
2519 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02002520 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002521
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002522 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2523 rdtscll(tsc_this);
2524 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2525 tsc_base = tsc_this;
2526
2527 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002528
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002529 return 0;
2530}
2531
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002532static int init_rmode(struct kvm *kvm)
2533{
2534 if (!init_rmode_tss(kvm))
2535 return 0;
2536 if (!init_rmode_identity_map(kvm))
2537 return 0;
2538 return 1;
2539}
2540
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002541static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2542{
2543 struct vcpu_vmx *vmx = to_vmx(vcpu);
2544 u64 msr;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002545 int ret, idx;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002546
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002547 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002548 idx = srcu_read_lock(&vcpu->kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002549 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002550 ret = -ENOMEM;
2551 goto out;
2552 }
2553
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002554 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002555
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002556 vmx->soft_vnmi_blocked = 0;
2557
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002558 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002559 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002560 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002561 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002562 msr |= MSR_IA32_APICBASE_BSP;
2563 kvm_set_apic_base(&vmx->vcpu, msr);
2564
2565 fx_init(&vmx->vcpu);
2566
Avi Kivity5706be02008-08-20 15:07:31 +03002567 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002568 /*
2569 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2570 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2571 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002572 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002573 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2574 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2575 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002576 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2577 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002578 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002579
2580 seg_setup(VCPU_SREG_DS);
2581 seg_setup(VCPU_SREG_ES);
2582 seg_setup(VCPU_SREG_FS);
2583 seg_setup(VCPU_SREG_GS);
2584 seg_setup(VCPU_SREG_SS);
2585
2586 vmcs_write16(GUEST_TR_SELECTOR, 0);
2587 vmcs_writel(GUEST_TR_BASE, 0);
2588 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2589 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2590
2591 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2592 vmcs_writel(GUEST_LDTR_BASE, 0);
2593 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2594 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2595
2596 vmcs_write32(GUEST_SYSENTER_CS, 0);
2597 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2598 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2599
2600 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002601 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002602 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002603 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002604 kvm_rip_write(vcpu, 0);
2605 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002606
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002607 vmcs_writel(GUEST_DR7, 0x400);
2608
2609 vmcs_writel(GUEST_GDTR_BASE, 0);
2610 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2611
2612 vmcs_writel(GUEST_IDTR_BASE, 0);
2613 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2614
2615 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2616 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2617 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2618
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002619 /* Special registers */
2620 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2621
2622 setup_msrs(vmx);
2623
Avi Kivity6aa8b732006-12-10 02:21:36 -08002624 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2625
Sheng Yangf78e0e22007-10-29 09:40:42 +08002626 if (cpu_has_vmx_tpr_shadow()) {
2627 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2628 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2629 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002630 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002631 vmcs_write32(TPR_THRESHOLD, 0);
2632 }
2633
2634 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2635 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002636 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637
Sheng Yang2384d2b2008-01-17 15:14:33 +08002638 if (vmx->vpid != 0)
2639 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2640
Eduardo Habkostfa400522009-10-24 02:49:58 -02002641 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002642 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002643 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002644 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002645 vmx_fpu_activate(&vmx->vcpu);
2646 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647
Sheng Yang2384d2b2008-01-17 15:14:33 +08002648 vpid_sync_vcpu_all(vmx);
2649
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002650 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002652 /* HACK: Don't enable emulation on guest boot/reset */
2653 vmx->emulation_required = 0;
2654
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655out:
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002656 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 return ret;
2658}
2659
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002660static void enable_irq_window(struct kvm_vcpu *vcpu)
2661{
2662 u32 cpu_based_vm_exec_control;
2663
2664 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2665 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2666 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2667}
2668
2669static void enable_nmi_window(struct kvm_vcpu *vcpu)
2670{
2671 u32 cpu_based_vm_exec_control;
2672
2673 if (!cpu_has_virtual_nmis()) {
2674 enable_irq_window(vcpu);
2675 return;
2676 }
2677
2678 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2679 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2680 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2681}
2682
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002683static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002684{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002685 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002686 uint32_t intr;
2687 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002688
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002689 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002690
Avi Kivityfa89a812008-09-01 15:57:51 +03002691 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002692 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002693 vmx->rmode.irq.pending = true;
2694 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002695 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002696 if (vcpu->arch.interrupt.soft)
2697 vmx->rmode.irq.rip +=
2698 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002699 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2700 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2701 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002702 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002703 return;
2704 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002705 intr = irq | INTR_INFO_VALID_MASK;
2706 if (vcpu->arch.interrupt.soft) {
2707 intr |= INTR_TYPE_SOFT_INTR;
2708 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2709 vmx->vcpu.arch.event_exit_inst_len);
2710 } else
2711 intr |= INTR_TYPE_EXT_INTR;
2712 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002713}
2714
Sheng Yangf08864b2008-05-15 18:23:25 +08002715static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2716{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002717 struct vcpu_vmx *vmx = to_vmx(vcpu);
2718
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002719 if (!cpu_has_virtual_nmis()) {
2720 /*
2721 * Tracking the NMI-blocked state in software is built upon
2722 * finding the next open IRQ window. This, in turn, depends on
2723 * well-behaving guests: They have to keep IRQs disabled at
2724 * least as long as the NMI handler runs. Otherwise we may
2725 * cause NMI nesting, maybe breaking the guest. But as this is
2726 * highly unlikely, we can live with the residual risk.
2727 */
2728 vmx->soft_vnmi_blocked = 1;
2729 vmx->vnmi_blocked_time = 0;
2730 }
2731
Jan Kiszka487b3912008-09-26 09:30:56 +02002732 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002733 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002734 vmx->rmode.irq.pending = true;
2735 vmx->rmode.irq.vector = NMI_VECTOR;
2736 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2737 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2738 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2739 INTR_INFO_VALID_MASK);
2740 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2741 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2742 return;
2743 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002744 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2745 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002746}
2747
Gleb Natapovc4282df2009-04-21 17:45:07 +03002748static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002749{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002750 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002751 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002752
Gleb Natapovc4282df2009-04-21 17:45:07 +03002753 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2754 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2755 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002756}
2757
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002758static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
2759{
2760 if (!cpu_has_virtual_nmis())
2761 return to_vmx(vcpu)->soft_vnmi_blocked;
2762 else
2763 return !!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2764 GUEST_INTR_STATE_NMI);
2765}
2766
2767static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2768{
2769 struct vcpu_vmx *vmx = to_vmx(vcpu);
2770
2771 if (!cpu_has_virtual_nmis()) {
2772 if (vmx->soft_vnmi_blocked != masked) {
2773 vmx->soft_vnmi_blocked = masked;
2774 vmx->vnmi_blocked_time = 0;
2775 }
2776 } else {
2777 if (masked)
2778 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2779 GUEST_INTR_STATE_NMI);
2780 else
2781 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2782 GUEST_INTR_STATE_NMI);
2783 }
2784}
2785
Gleb Natapov78646122009-03-23 12:12:11 +02002786static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2787{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002788 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2789 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2790 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002791}
2792
Izik Eiduscbc94022007-10-25 00:29:55 +02002793static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2794{
2795 int ret;
2796 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002797 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002798 .guest_phys_addr = addr,
2799 .memory_size = PAGE_SIZE * 3,
2800 .flags = 0,
2801 };
2802
2803 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2804 if (ret)
2805 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002806 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002807 return 0;
2808}
2809
Avi Kivity6aa8b732006-12-10 02:21:36 -08002810static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2811 int vec, u32 err_code)
2812{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002813 /*
2814 * Instruction with address size override prefix opcode 0x67
2815 * Cause the #SS fault with 0 error code in VM86 mode.
2816 */
2817 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002818 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002820 /*
2821 * Forward all other exceptions that are valid in real mode.
2822 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2823 * the required debugging infrastructure rework.
2824 */
2825 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002826 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002827 if (vcpu->guest_debug &
2828 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2829 return 0;
2830 kvm_queue_exception(vcpu, vec);
2831 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002832 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01002833 /*
2834 * Update instruction length as we may reinject the exception
2835 * from user space while in guest debugging mode.
2836 */
2837 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
2838 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002839 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2840 return 0;
2841 /* fall through */
2842 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002843 case OF_VECTOR:
2844 case BR_VECTOR:
2845 case UD_VECTOR:
2846 case DF_VECTOR:
2847 case SS_VECTOR:
2848 case GP_VECTOR:
2849 case MF_VECTOR:
2850 kvm_queue_exception(vcpu, vec);
2851 return 1;
2852 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 return 0;
2854}
2855
Andi Kleena0861c02009-06-08 17:37:09 +08002856/*
2857 * Trigger machine check on the host. We assume all the MSRs are already set up
2858 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2859 * We pass a fake environment to the machine check handler because we want
2860 * the guest to be always treated like user space, no matter what context
2861 * it used internally.
2862 */
2863static void kvm_machine_check(void)
2864{
2865#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2866 struct pt_regs regs = {
2867 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2868 .flags = X86_EFLAGS_IF,
2869 };
2870
2871 do_machine_check(&regs, 0);
2872#endif
2873}
2874
Avi Kivity851ba692009-08-24 11:10:17 +03002875static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002876{
2877 /* already handled by vcpu_run */
2878 return 1;
2879}
2880
Avi Kivity851ba692009-08-24 11:10:17 +03002881static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882{
Avi Kivity1155f762007-11-22 11:30:47 +02002883 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002884 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002885 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002886 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 u32 vect_info;
2888 enum emulation_result er;
2889
Avi Kivity1155f762007-11-22 11:30:47 +02002890 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2892
Andi Kleena0861c02009-06-08 17:37:09 +08002893 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002894 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002895
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02002897 !is_page_fault(intr_info)) {
2898 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2899 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
2900 vcpu->run->internal.ndata = 2;
2901 vcpu->run->internal.data[0] = vect_info;
2902 vcpu->run->internal.data[1] = intr_info;
2903 return 0;
2904 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905
Jan Kiszkae4a41882008-09-26 09:30:46 +02002906 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002907 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002908
2909 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002910 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002911 return 1;
2912 }
2913
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002914 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002915 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002916 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002917 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002918 return 1;
2919 }
2920
Avi Kivity6aa8b732006-12-10 02:21:36 -08002921 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002922 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002923 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2925 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002926 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002927 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002928 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002929 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002930 trace_kvm_page_fault(cr2, error_code);
2931
Gleb Natapov3298b752009-05-11 13:35:46 +03002932 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002933 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002934 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935 }
2936
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002937 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002938 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002939 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002940 if (vcpu->arch.halt_request) {
2941 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002942 return kvm_emulate_halt(vcpu);
2943 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002944 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002945 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002947 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002948 switch (ex_no) {
2949 case DB_VECTOR:
2950 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2951 if (!(vcpu->guest_debug &
2952 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2953 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2954 kvm_queue_exception(vcpu, DB_VECTOR);
2955 return 1;
2956 }
2957 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2958 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2959 /* fall through */
2960 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01002961 /*
2962 * Update instruction length as we may reinject #BP from
2963 * user space while in guest debugging mode. Reading it for
2964 * #DB as well causes no harm, it is not used in that case.
2965 */
2966 vmx->vcpu.arch.event_exit_inst_len =
2967 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002969 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2970 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002971 break;
2972 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002973 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2974 kvm_run->ex.exception = ex_no;
2975 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002976 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978 return 0;
2979}
2980
Avi Kivity851ba692009-08-24 11:10:17 +03002981static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002983 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 return 1;
2985}
2986
Avi Kivity851ba692009-08-24 11:10:17 +03002987static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002988{
Avi Kivity851ba692009-08-24 11:10:17 +03002989 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002990 return 0;
2991}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002992
Avi Kivity851ba692009-08-24 11:10:17 +03002993static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994{
He, Qingbfdaab02007-09-12 14:18:28 +08002995 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002996 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002997 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998
He, Qingbfdaab02007-09-12 14:18:28 +08002999 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02003000 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003001 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003002
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003003 ++vcpu->stat.io_exits;
3004
3005 if (string || in)
3006 return !(emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO);
3007
3008 port = exit_qualification >> 16;
3009 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003010 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003011
3012 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003013}
3014
Ingo Molnar102d8322007-02-19 14:37:47 +02003015static void
3016vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3017{
3018 /*
3019 * Patch in the VMCALL instruction:
3020 */
3021 hypercall[0] = 0x0f;
3022 hypercall[1] = 0x01;
3023 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02003024}
3025
Avi Kivity851ba692009-08-24 11:10:17 +03003026static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003028 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003029 int cr;
3030 int reg;
3031
He, Qingbfdaab02007-09-12 14:18:28 +08003032 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033 cr = exit_qualification & 15;
3034 reg = (exit_qualification >> 8) & 15;
3035 switch ((exit_qualification >> 4) & 3) {
3036 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003037 val = kvm_register_read(vcpu, reg);
3038 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 switch (cr) {
3040 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003041 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003042 skip_emulated_instruction(vcpu);
3043 return 1;
3044 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003045 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046 skip_emulated_instruction(vcpu);
3047 return 1;
3048 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003049 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 skip_emulated_instruction(vcpu);
3051 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003052 case 8: {
3053 u8 cr8_prev = kvm_get_cr8(vcpu);
3054 u8 cr8 = kvm_register_read(vcpu, reg);
3055 kvm_set_cr8(vcpu, cr8);
3056 skip_emulated_instruction(vcpu);
3057 if (irqchip_in_kernel(vcpu->kvm))
3058 return 1;
3059 if (cr8_prev <= cr8)
3060 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03003061 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003062 return 0;
3063 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 };
3065 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03003066 case 2: /* clts */
Avi Kivityedcafe32009-12-30 18:07:40 +02003067 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02003068 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003069 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02003070 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03003071 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 case 1: /*mov from cr*/
3073 switch (cr) {
3074 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003075 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003076 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 skip_emulated_instruction(vcpu);
3078 return 1;
3079 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003080 val = kvm_get_cr8(vcpu);
3081 kvm_register_write(vcpu, reg, val);
3082 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003083 skip_emulated_instruction(vcpu);
3084 return 1;
3085 }
3086 break;
3087 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003088 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003089 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003090 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003091
3092 skip_emulated_instruction(vcpu);
3093 return 1;
3094 default:
3095 break;
3096 }
Avi Kivity851ba692009-08-24 11:10:17 +03003097 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003098 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003099 (int)(exit_qualification >> 4) & 3, cr);
3100 return 0;
3101}
3102
Avi Kivity851ba692009-08-24 11:10:17 +03003103static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003104{
He, Qingbfdaab02007-09-12 14:18:28 +08003105 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 int dr, reg;
3107
Jan Kiszkaf2483412010-01-20 18:20:20 +01003108 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03003109 if (!kvm_require_cpl(vcpu, 0))
3110 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003111 dr = vmcs_readl(GUEST_DR7);
3112 if (dr & DR7_GD) {
3113 /*
3114 * As the vm-exit takes precedence over the debug trap, we
3115 * need to emulate the latter, either for the host or the
3116 * guest debugging itself.
3117 */
3118 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003119 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3120 vcpu->run->debug.arch.dr7 = dr;
3121 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003122 vmcs_readl(GUEST_CS_BASE) +
3123 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003124 vcpu->run->debug.arch.exception = DB_VECTOR;
3125 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003126 return 0;
3127 } else {
3128 vcpu->arch.dr7 &= ~DR7_GD;
3129 vcpu->arch.dr6 |= DR6_BD;
3130 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3131 kvm_queue_exception(vcpu, DB_VECTOR);
3132 return 1;
3133 }
3134 }
3135
He, Qingbfdaab02007-09-12 14:18:28 +08003136 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003137 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3138 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3139 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03003140 unsigned long val;
3141 if (!kvm_get_dr(vcpu, dr, &val))
3142 kvm_register_write(vcpu, reg, val);
3143 } else
3144 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145 skip_emulated_instruction(vcpu);
3146 return 1;
3147}
3148
Gleb Natapov020df072010-04-13 10:05:23 +03003149static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3150{
3151 vmcs_writel(GUEST_DR7, val);
3152}
3153
Avi Kivity851ba692009-08-24 11:10:17 +03003154static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003155{
Avi Kivity06465c52007-02-28 20:46:53 +02003156 kvm_emulate_cpuid(vcpu);
3157 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158}
3159
Avi Kivity851ba692009-08-24 11:10:17 +03003160static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003161{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003162 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003163 u64 data;
3164
3165 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02003166 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003167 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168 return 1;
3169 }
3170
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003171 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003172
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003174 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3175 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 skip_emulated_instruction(vcpu);
3177 return 1;
3178}
3179
Avi Kivity851ba692009-08-24 11:10:17 +03003180static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003182 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3183 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3184 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003185
3186 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02003187 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003188 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003189 return 1;
3190 }
3191
Avi Kivity59200272010-01-25 19:47:02 +02003192 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003193 skip_emulated_instruction(vcpu);
3194 return 1;
3195}
3196
Avi Kivity851ba692009-08-24 11:10:17 +03003197static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003198{
3199 return 1;
3200}
3201
Avi Kivity851ba692009-08-24 11:10:17 +03003202static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203{
Eddie Dong85f455f2007-07-06 12:20:49 +03003204 u32 cpu_based_vm_exec_control;
3205
3206 /* clear pending irq */
3207 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3208 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3209 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003210
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003211 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003212
Dor Laorc1150d82007-01-05 16:36:24 -08003213 /*
3214 * If the user space waits to inject interrupts, exit as soon as
3215 * possible
3216 */
Gleb Natapov80618232009-04-21 17:44:56 +03003217 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003218 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003219 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003220 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003221 return 0;
3222 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003223 return 1;
3224}
3225
Avi Kivity851ba692009-08-24 11:10:17 +03003226static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227{
3228 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003229 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230}
3231
Avi Kivity851ba692009-08-24 11:10:17 +03003232static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003233{
Dor Laor510043d2007-02-19 18:25:43 +02003234 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003235 kvm_emulate_hypercall(vcpu);
3236 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003237}
3238
Avi Kivity851ba692009-08-24 11:10:17 +03003239static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003240{
3241 kvm_queue_exception(vcpu, UD_VECTOR);
3242 return 1;
3243}
3244
Avi Kivity851ba692009-08-24 11:10:17 +03003245static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003246{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003247 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003248
3249 kvm_mmu_invlpg(vcpu, exit_qualification);
3250 skip_emulated_instruction(vcpu);
3251 return 1;
3252}
3253
Avi Kivity851ba692009-08-24 11:10:17 +03003254static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003255{
3256 skip_emulated_instruction(vcpu);
3257 /* TODO: Add support for VT-d/pass-through device */
3258 return 1;
3259}
3260
Avi Kivity851ba692009-08-24 11:10:17 +03003261static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003262{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003263 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003264 enum emulation_result er;
3265 unsigned long offset;
3266
Sheng Yangf9c617f2009-03-25 10:08:52 +08003267 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003268 offset = exit_qualification & 0xffful;
3269
Avi Kivity851ba692009-08-24 11:10:17 +03003270 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003271
3272 if (er != EMULATE_DONE) {
3273 printk(KERN_ERR
3274 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3275 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003276 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003277 }
3278 return 1;
3279}
3280
Avi Kivity851ba692009-08-24 11:10:17 +03003281static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003282{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003283 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003284 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003285 bool has_error_code = false;
3286 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003287 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003288 int reason, type, idt_v;
3289
3290 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3291 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003292
3293 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3294
3295 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003296 if (reason == TASK_SWITCH_GATE && idt_v) {
3297 switch (type) {
3298 case INTR_TYPE_NMI_INTR:
3299 vcpu->arch.nmi_injected = false;
3300 if (cpu_has_virtual_nmis())
3301 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3302 GUEST_INTR_STATE_NMI);
3303 break;
3304 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003305 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003306 kvm_clear_interrupt_queue(vcpu);
3307 break;
3308 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003309 if (vmx->idt_vectoring_info &
3310 VECTORING_INFO_DELIVER_CODE_MASK) {
3311 has_error_code = true;
3312 error_code =
3313 vmcs_read32(IDT_VECTORING_ERROR_CODE);
3314 }
3315 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003316 case INTR_TYPE_SOFT_EXCEPTION:
3317 kvm_clear_exception_queue(vcpu);
3318 break;
3319 default:
3320 break;
3321 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003322 }
Izik Eidus37817f22008-03-24 23:14:53 +02003323 tss_selector = exit_qualification;
3324
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003325 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3326 type != INTR_TYPE_EXT_INTR &&
3327 type != INTR_TYPE_NMI_INTR))
3328 skip_emulated_instruction(vcpu);
3329
Gleb Natapovacb54512010-04-15 21:03:50 +03003330 if (kvm_task_switch(vcpu, tss_selector, reason,
3331 has_error_code, error_code) == EMULATE_FAIL) {
3332 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3333 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3334 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003335 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03003336 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003337
3338 /* clear all local breakpoint enable flags */
3339 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3340
3341 /*
3342 * TODO: What about debug traps on tss switch?
3343 * Are we supposed to inject them and update dr6?
3344 */
3345
3346 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003347}
3348
Avi Kivity851ba692009-08-24 11:10:17 +03003349static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003350{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003351 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003352 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003353 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003354
Sheng Yangf9c617f2009-03-25 10:08:52 +08003355 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003356
3357 if (exit_qualification & (1 << 6)) {
3358 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003359 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003360 }
3361
3362 gla_validity = (exit_qualification >> 7) & 0x3;
3363 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3364 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3365 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3366 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003367 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003368 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3369 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003370 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3371 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003372 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003373 }
3374
3375 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003376 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003377 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003378}
3379
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003380static u64 ept_rsvd_mask(u64 spte, int level)
3381{
3382 int i;
3383 u64 mask = 0;
3384
3385 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3386 mask |= (1ULL << i);
3387
3388 if (level > 2)
3389 /* bits 7:3 reserved */
3390 mask |= 0xf8;
3391 else if (level == 2) {
3392 if (spte & (1ULL << 7))
3393 /* 2MB ref, bits 20:12 reserved */
3394 mask |= 0x1ff000;
3395 else
3396 /* bits 6:3 reserved */
3397 mask |= 0x78;
3398 }
3399
3400 return mask;
3401}
3402
3403static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3404 int level)
3405{
3406 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3407
3408 /* 010b (write-only) */
3409 WARN_ON((spte & 0x7) == 0x2);
3410
3411 /* 110b (write/execute) */
3412 WARN_ON((spte & 0x7) == 0x6);
3413
3414 /* 100b (execute-only) and value not supported by logical processor */
3415 if (!cpu_has_vmx_ept_execute_only())
3416 WARN_ON((spte & 0x7) == 0x4);
3417
3418 /* not 000b */
3419 if ((spte & 0x7)) {
3420 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3421
3422 if (rsvd_bits != 0) {
3423 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3424 __func__, rsvd_bits);
3425 WARN_ON(1);
3426 }
3427
3428 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3429 u64 ept_mem_type = (spte & 0x38) >> 3;
3430
3431 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3432 ept_mem_type == 7) {
3433 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3434 __func__, ept_mem_type);
3435 WARN_ON(1);
3436 }
3437 }
3438 }
3439}
3440
Avi Kivity851ba692009-08-24 11:10:17 +03003441static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003442{
3443 u64 sptes[4];
3444 int nr_sptes, i;
3445 gpa_t gpa;
3446
3447 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3448
3449 printk(KERN_ERR "EPT: Misconfiguration.\n");
3450 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3451
3452 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3453
3454 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3455 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3456
Avi Kivity851ba692009-08-24 11:10:17 +03003457 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3458 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003459
3460 return 0;
3461}
3462
Avi Kivity851ba692009-08-24 11:10:17 +03003463static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003464{
3465 u32 cpu_based_vm_exec_control;
3466
3467 /* clear pending NMI */
3468 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3469 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3470 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3471 ++vcpu->stat.nmi_window_exits;
3472
3473 return 1;
3474}
3475
Mohammed Gamal80ced182009-09-01 12:48:18 +02003476static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003477{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003478 struct vcpu_vmx *vmx = to_vmx(vcpu);
3479 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003480 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003481
3482 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003483 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003484
Mohammed Gamal80ced182009-09-01 12:48:18 +02003485 if (err == EMULATE_DO_MMIO) {
3486 ret = 0;
3487 goto out;
3488 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003489
3490 if (err != EMULATE_DONE) {
Mohammed Gamal80ced182009-09-01 12:48:18 +02003491 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3492 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02003493 vcpu->run->internal.ndata = 0;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003494 ret = 0;
3495 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003496 }
3497
3498 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003499 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003500 if (need_resched())
3501 schedule();
3502 }
3503
Mohammed Gamal80ced182009-09-01 12:48:18 +02003504 vmx->emulation_required = 0;
3505out:
3506 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003507}
3508
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003510 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3511 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3512 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003513static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003514{
3515 skip_emulated_instruction(vcpu);
3516 kvm_vcpu_on_spin(vcpu);
3517
3518 return 1;
3519}
3520
Sheng Yang59708672009-12-15 13:29:54 +08003521static int handle_invalid_op(struct kvm_vcpu *vcpu)
3522{
3523 kvm_queue_exception(vcpu, UD_VECTOR);
3524 return 1;
3525}
3526
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003527/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528 * The exit handlers return 1 if the exit was handled fully and guest execution
3529 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3530 * to be done to userspace and return 0.
3531 */
Avi Kivity851ba692009-08-24 11:10:17 +03003532static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3534 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003535 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003536 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003537 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003538 [EXIT_REASON_CR_ACCESS] = handle_cr,
3539 [EXIT_REASON_DR_ACCESS] = handle_dr,
3540 [EXIT_REASON_CPUID] = handle_cpuid,
3541 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3542 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3543 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3544 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003545 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003546 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003547 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3548 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3549 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3550 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3551 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3552 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3553 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3554 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3555 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003556 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3557 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003558 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003559 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003560 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003561 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3562 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003563 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003564 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3565 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003566};
3567
3568static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003569 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003570
3571/*
3572 * The guest has exited. See if we can fix it or if we need userspace
3573 * assistance.
3574 */
Avi Kivity851ba692009-08-24 11:10:17 +03003575static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003576{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003577 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003578 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003579 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003580
Avi Kivity5bfd8b52010-03-11 10:50:44 +02003581 trace_kvm_exit(exit_reason, vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003582
Mohammed Gamal80ced182009-09-01 12:48:18 +02003583 /* If guest state is invalid, start emulating */
3584 if (vmx->emulation_required && emulate_invalid_guest_state)
3585 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003586
Sheng Yang14394422008-04-28 12:24:45 +08003587 /* Access CR3 don't cause VMExit in paging mode, so we need
3588 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003589 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003590 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003591
Avi Kivity29bd8a72007-09-10 17:27:03 +03003592 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003593 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3594 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003595 = vmcs_read32(VM_INSTRUCTION_ERROR);
3596 return 0;
3597 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003598
Mike Dayd77c26f2007-10-08 09:02:08 -04003599 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003600 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003601 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3602 exit_reason != EXIT_REASON_TASK_SWITCH))
3603 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3604 "(0x%x) and exit reason is 0x%x\n",
3605 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003606
3607 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003608 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003609 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003610 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003611 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003612 /*
3613 * This CPU don't support us in finding the end of an
3614 * NMI-blocked window if the guest runs with IRQs
3615 * disabled. So we pull the trigger after 1 s of
3616 * futile waiting, but inform the user about this.
3617 */
3618 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3619 "state on VCPU %d after 1 s timeout\n",
3620 __func__, vcpu->vcpu_id);
3621 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003622 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003623 }
3624
Avi Kivity6aa8b732006-12-10 02:21:36 -08003625 if (exit_reason < kvm_vmx_max_exit_handlers
3626 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003627 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003629 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3630 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 }
3632 return 0;
3633}
3634
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003635static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003636{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003637 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003638 vmcs_write32(TPR_THRESHOLD, 0);
3639 return;
3640 }
3641
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003642 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003643}
3644
Avi Kivitycf393f72008-07-01 16:20:21 +03003645static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3646{
3647 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003648 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003649 bool unblock_nmi;
3650 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003651 int type;
3652 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003653
3654 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003655
Andi Kleena0861c02009-06-08 17:37:09 +08003656 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3657
3658 /* Handle machine checks before interrupts are enabled */
3659 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3660 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3661 && is_machine_check(exit_intr_info)))
3662 kvm_machine_check();
3663
Gleb Natapov20f65982009-05-11 13:35:55 +03003664 /* We need to handle NMIs before interrupts are enabled */
3665 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003666 (exit_intr_info & INTR_INFO_VALID_MASK)) {
3667 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03003668 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003669 kvm_after_handle_nmi(&vmx->vcpu);
3670 }
Gleb Natapov20f65982009-05-11 13:35:55 +03003671
3672 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3673
Avi Kivitycf393f72008-07-01 16:20:21 +03003674 if (cpu_has_virtual_nmis()) {
3675 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3676 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3677 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003678 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003679 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3680 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003681 * SDM 3: 23.2.2 (September 2008)
3682 * Bit 12 is undefined in any of the following cases:
3683 * If the VM exit sets the valid bit in the IDT-vectoring
3684 * information field.
3685 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003686 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003687 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3688 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003689 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3690 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003691 } else if (unlikely(vmx->soft_vnmi_blocked))
3692 vmx->vnmi_blocked_time +=
3693 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003694
Gleb Natapov37b96e92009-03-30 16:03:13 +03003695 vmx->vcpu.arch.nmi_injected = false;
3696 kvm_clear_exception_queue(&vmx->vcpu);
3697 kvm_clear_interrupt_queue(&vmx->vcpu);
3698
3699 if (!idtv_info_valid)
3700 return;
3701
Avi Kivity668f6122008-07-02 09:28:55 +03003702 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3703 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003704
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003705 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003706 case INTR_TYPE_NMI_INTR:
3707 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003708 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003709 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003710 * Clear bit "block by NMI" before VM entry if a NMI
3711 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003712 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003713 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3714 GUEST_INTR_STATE_NMI);
3715 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003716 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003717 vmx->vcpu.arch.event_exit_inst_len =
3718 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3719 /* fall through */
3720 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003721 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003722 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3723 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003724 } else
3725 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003726 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003727 case INTR_TYPE_SOFT_INTR:
3728 vmx->vcpu.arch.event_exit_inst_len =
3729 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3730 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003731 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003732 kvm_queue_interrupt(&vmx->vcpu, vector,
3733 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003734 break;
3735 default:
3736 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003737 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003738}
3739
Avi Kivity9c8cba32007-11-22 11:42:59 +02003740/*
3741 * Failure to inject an interrupt should give us the information
3742 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3743 * when fetching the interrupt redirection bitmap in the real-mode
3744 * tss, this doesn't happen. So we do it ourselves.
3745 */
3746static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3747{
3748 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003749 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003750 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003751 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003752 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3753 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3754 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3755 return;
3756 }
3757 vmx->idt_vectoring_info =
3758 VECTORING_INFO_VALID_MASK
3759 | INTR_TYPE_EXT_INTR
3760 | vmx->rmode.irq.vector;
3761}
3762
Avi Kivityc8019492008-07-14 14:44:59 +03003763#ifdef CONFIG_X86_64
3764#define R "r"
3765#define Q "q"
3766#else
3767#define R "e"
3768#define Q "l"
3769#endif
3770
Avi Kivity851ba692009-08-24 11:10:17 +03003771static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003772{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003773 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003774
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003775 /* Record the guest's net vcpu time for enforced NMI injections. */
3776 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3777 vmx->entry_time = ktime_get();
3778
Mohammed Gamal80ced182009-09-01 12:48:18 +02003779 /* Don't enter VMX if guest state is invalid, let the exit handler
3780 start emulation until we arrive back to a valid state */
3781 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003782 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003783
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003784 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3785 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3786 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3787 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3788
Gleb Natapov787ff732009-05-18 11:44:06 +03003789 /* When single-stepping over STI and MOV SS, we must clear the
3790 * corresponding interruptibility bits in the guest state. Otherwise
3791 * vmentry fails as it then expects bit 14 (BS) in pending debug
3792 * exceptions being set, but that's not correct for the guest debugging
3793 * case. */
3794 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3795 vmx_set_interrupt_shadow(vcpu, 0);
3796
Avi Kivitye6adf282007-04-30 16:07:54 +03003797 /*
3798 * Loading guest fpu may have cleared host cr0.ts
3799 */
3800 vmcs_writel(HOST_CR0, read_cr0());
3801
Mike Dayd77c26f2007-10-08 09:02:08 -04003802 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003804 "push %%"R"dx; push %%"R"bp;"
3805 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003806 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3807 "je 1f \n\t"
3808 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003809 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003810 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003811 /* Reload cr2 if changed */
3812 "mov %c[cr2](%0), %%"R"ax \n\t"
3813 "mov %%cr2, %%"R"dx \n\t"
3814 "cmp %%"R"ax, %%"R"dx \n\t"
3815 "je 2f \n\t"
3816 "mov %%"R"ax, %%cr2 \n\t"
3817 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003819 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003820 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003821 "mov %c[rax](%0), %%"R"ax \n\t"
3822 "mov %c[rbx](%0), %%"R"bx \n\t"
3823 "mov %c[rdx](%0), %%"R"dx \n\t"
3824 "mov %c[rsi](%0), %%"R"si \n\t"
3825 "mov %c[rdi](%0), %%"R"di \n\t"
3826 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003827#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003828 "mov %c[r8](%0), %%r8 \n\t"
3829 "mov %c[r9](%0), %%r9 \n\t"
3830 "mov %c[r10](%0), %%r10 \n\t"
3831 "mov %c[r11](%0), %%r11 \n\t"
3832 "mov %c[r12](%0), %%r12 \n\t"
3833 "mov %c[r13](%0), %%r13 \n\t"
3834 "mov %c[r14](%0), %%r14 \n\t"
3835 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003836#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003837 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3838
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003840 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003841 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003842 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003843 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003844 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003845 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003846 "xchg %0, (%%"R"sp) \n\t"
3847 "mov %%"R"ax, %c[rax](%0) \n\t"
3848 "mov %%"R"bx, %c[rbx](%0) \n\t"
3849 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3850 "mov %%"R"dx, %c[rdx](%0) \n\t"
3851 "mov %%"R"si, %c[rsi](%0) \n\t"
3852 "mov %%"R"di, %c[rdi](%0) \n\t"
3853 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003854#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003855 "mov %%r8, %c[r8](%0) \n\t"
3856 "mov %%r9, %c[r9](%0) \n\t"
3857 "mov %%r10, %c[r10](%0) \n\t"
3858 "mov %%r11, %c[r11](%0) \n\t"
3859 "mov %%r12, %c[r12](%0) \n\t"
3860 "mov %%r13, %c[r13](%0) \n\t"
3861 "mov %%r14, %c[r14](%0) \n\t"
3862 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003863#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003864 "mov %%cr2, %%"R"ax \n\t"
3865 "mov %%"R"ax, %c[cr2](%0) \n\t"
3866
3867 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003868 "setbe %c[fail](%0) \n\t"
3869 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3870 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3871 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03003872 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003873 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3874 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3875 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3876 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3877 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3878 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3879 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003880#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003881 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3882 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3883 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3884 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3885 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3886 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3887 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3888 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003889#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003890 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003891 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003892 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003893#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003894 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3895#endif
3896 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003897
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003898 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3899 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003900 vcpu->arch.regs_dirty = 0;
3901
Avi Kivity1155f762007-11-22 11:30:47 +02003902 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003903 if (vmx->rmode.irq.pending)
3904 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003905
Mike Dayd77c26f2007-10-08 09:02:08 -04003906 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003907 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003908
Avi Kivitycf393f72008-07-01 16:20:21 +03003909 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003910}
3911
Avi Kivityc8019492008-07-14 14:44:59 +03003912#undef R
3913#undef Q
3914
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3916{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003917 struct vcpu_vmx *vmx = to_vmx(vcpu);
3918
3919 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003920 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003921 free_vmcs(vmx->vmcs);
3922 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003923 }
3924}
3925
3926static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3927{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003928 struct vcpu_vmx *vmx = to_vmx(vcpu);
3929
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003930 free_vpid(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003931 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003932 kfree(vmx->guest_msrs);
3933 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003934 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935}
3936
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003937static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003938{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003939 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003940 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003941 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003942
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003943 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003944 return ERR_PTR(-ENOMEM);
3945
Sheng Yang2384d2b2008-01-17 15:14:33 +08003946 allocate_vpid(vmx);
3947
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003948 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3949 if (err)
3950 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003951
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003952 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003953 if (!vmx->guest_msrs) {
3954 err = -ENOMEM;
3955 goto uninit_vcpu;
3956 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003957
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003958 vmx->vmcs = alloc_vmcs();
3959 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003960 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003961
3962 vmcs_clear(vmx->vmcs);
3963
Avi Kivity15ad7142007-07-11 18:17:21 +03003964 cpu = get_cpu();
3965 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003966 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003967 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003968 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003969 if (err)
3970 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003971 if (vm_need_virtualize_apic_accesses(kvm))
3972 if (alloc_apic_access_page(kvm) != 0)
3973 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003974
Sheng Yangb927a3c2009-07-21 10:42:48 +08003975 if (enable_ept) {
3976 if (!kvm->arch.ept_identity_map_addr)
3977 kvm->arch.ept_identity_map_addr =
3978 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003979 if (alloc_identity_pagetable(kvm) != 0)
3980 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003981 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003982
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003983 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003984
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003985free_vmcs:
3986 free_vmcs(vmx->vmcs);
3987free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003988 kfree(vmx->guest_msrs);
3989uninit_vcpu:
3990 kvm_vcpu_uninit(&vmx->vcpu);
3991free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003992 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10003993 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003994 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995}
3996
Yang, Sheng002c7f72007-07-31 14:23:01 +03003997static void __init vmx_check_processor_compat(void *rtn)
3998{
3999 struct vmcs_config vmcs_conf;
4000
4001 *(int *)rtn = 0;
4002 if (setup_vmcs_config(&vmcs_conf) < 0)
4003 *(int *)rtn = -EIO;
4004 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
4005 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
4006 smp_processor_id());
4007 *(int *)rtn = -EIO;
4008 }
4009}
4010
Sheng Yang67253af2008-04-25 10:20:22 +08004011static int get_ept_level(void)
4012{
4013 return VMX_EPT_DEFAULT_GAW + 1;
4014}
4015
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004016static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08004017{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004018 u64 ret;
4019
Sheng Yang522c68c2009-04-27 20:35:43 +08004020 /* For VT-d and EPT combination
4021 * 1. MMIO: always map as UC
4022 * 2. EPT with VT-d:
4023 * a. VT-d without snooping control feature: can't guarantee the
4024 * result, try to trust guest.
4025 * b. VT-d with snooping control feature: snooping control feature of
4026 * VT-d engine can guarantee the cache correctness. Just set it
4027 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08004028 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08004029 * consistent with host MTRR
4030 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004031 if (is_mmio)
4032 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08004033 else if (vcpu->kvm->arch.iommu_domain &&
4034 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
4035 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
4036 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004037 else
Sheng Yang522c68c2009-04-27 20:35:43 +08004038 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08004039 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004040
4041 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08004042}
4043
Avi Kivityf4c9e872009-12-28 16:06:35 +02004044#define _ER(x) { EXIT_REASON_##x, #x }
4045
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004046static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02004047 _ER(EXCEPTION_NMI),
4048 _ER(EXTERNAL_INTERRUPT),
4049 _ER(TRIPLE_FAULT),
4050 _ER(PENDING_INTERRUPT),
4051 _ER(NMI_WINDOW),
4052 _ER(TASK_SWITCH),
4053 _ER(CPUID),
4054 _ER(HLT),
4055 _ER(INVLPG),
4056 _ER(RDPMC),
4057 _ER(RDTSC),
4058 _ER(VMCALL),
4059 _ER(VMCLEAR),
4060 _ER(VMLAUNCH),
4061 _ER(VMPTRLD),
4062 _ER(VMPTRST),
4063 _ER(VMREAD),
4064 _ER(VMRESUME),
4065 _ER(VMWRITE),
4066 _ER(VMOFF),
4067 _ER(VMON),
4068 _ER(CR_ACCESS),
4069 _ER(DR_ACCESS),
4070 _ER(IO_INSTRUCTION),
4071 _ER(MSR_READ),
4072 _ER(MSR_WRITE),
4073 _ER(MWAIT_INSTRUCTION),
4074 _ER(MONITOR_INSTRUCTION),
4075 _ER(PAUSE_INSTRUCTION),
4076 _ER(MCE_DURING_VMENTRY),
4077 _ER(TPR_BELOW_THRESHOLD),
4078 _ER(APIC_ACCESS),
4079 _ER(EPT_VIOLATION),
4080 _ER(EPT_MISCONFIG),
4081 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004082 { -1, NULL }
4083};
4084
Avi Kivityf4c9e872009-12-28 16:06:35 +02004085#undef _ER
4086
Sheng Yang17cc3932010-01-05 19:02:27 +08004087static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004088{
Sheng Yang878403b2010-01-05 19:02:29 +08004089 if (enable_ept && !cpu_has_vmx_ept_1g_page())
4090 return PT_DIRECTORY_LEVEL;
4091 else
4092 /* For shadow and EPT supported 1GB page */
4093 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004094}
4095
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004096static inline u32 bit(int bitno)
4097{
4098 return 1 << (bitno & 31);
4099}
4100
Sheng Yang0e851882009-12-18 16:48:46 +08004101static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4102{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004103 struct kvm_cpuid_entry2 *best;
4104 struct vcpu_vmx *vmx = to_vmx(vcpu);
4105 u32 exec_control;
4106
4107 vmx->rdtscp_enabled = false;
4108 if (vmx_rdtscp_supported()) {
4109 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4110 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4111 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4112 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4113 vmx->rdtscp_enabled = true;
4114 else {
4115 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4116 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4117 exec_control);
4118 }
4119 }
4120 }
Sheng Yang0e851882009-12-18 16:48:46 +08004121}
4122
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004123static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4124{
4125}
4126
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004127static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 .cpu_has_kvm_support = cpu_has_kvm_support,
4129 .disabled_by_bios = vmx_disabled_by_bios,
4130 .hardware_setup = hardware_setup,
4131 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004132 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133 .hardware_enable = hardware_enable,
4134 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004135 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004136
4137 .vcpu_create = vmx_create_vcpu,
4138 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004139 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004140
Avi Kivity04d2cc72007-09-10 18:10:54 +03004141 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004142 .vcpu_load = vmx_vcpu_load,
4143 .vcpu_put = vmx_vcpu_put,
4144
4145 .set_guest_debug = set_guest_debug,
4146 .get_msr = vmx_get_msr,
4147 .set_msr = vmx_set_msr,
4148 .get_segment_base = vmx_get_segment_base,
4149 .get_segment = vmx_get_segment,
4150 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004151 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004152 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004153 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004154 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004155 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004156 .set_cr3 = vmx_set_cr3,
4157 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004158 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004159 .get_idt = vmx_get_idt,
4160 .set_idt = vmx_set_idt,
4161 .get_gdt = vmx_get_gdt,
4162 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004163 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004164 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004165 .get_rflags = vmx_get_rflags,
4166 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02004167 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004168 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004169
4170 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004171
Avi Kivity6aa8b732006-12-10 02:21:36 -08004172 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004173 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004174 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004175 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4176 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004177 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004178 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004179 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004180 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004181 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004182 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004183 .get_nmi_mask = vmx_get_nmi_mask,
4184 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004185 .enable_nmi_window = enable_nmi_window,
4186 .enable_irq_window = enable_irq_window,
4187 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004188
Izik Eiduscbc94022007-10-25 00:29:55 +02004189 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004190 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004191 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004192
4193 .exit_reasons_str = vmx_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08004194 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004195
4196 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004197
4198 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004199
4200 .set_supported_cpuid = vmx_set_supported_cpuid,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004201};
4202
4203static int __init vmx_init(void)
4204{
Avi Kivity26bb0982009-09-07 11:14:12 +03004205 int r, i;
4206
4207 rdmsrl_safe(MSR_EFER, &host_efer);
4208
4209 for (i = 0; i < NR_VMX_MSR; ++i)
4210 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004211
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004212 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004213 if (!vmx_io_bitmap_a)
4214 return -ENOMEM;
4215
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004216 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004217 if (!vmx_io_bitmap_b) {
4218 r = -ENOMEM;
4219 goto out;
4220 }
4221
Avi Kivity58972972009-02-24 22:26:47 +02004222 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4223 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004224 r = -ENOMEM;
4225 goto out1;
4226 }
4227
Avi Kivity58972972009-02-24 22:26:47 +02004228 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4229 if (!vmx_msr_bitmap_longmode) {
4230 r = -ENOMEM;
4231 goto out2;
4232 }
4233
He, Qingfdef3ad2007-04-30 09:45:24 +03004234 /*
4235 * Allow direct access to the PC debug port (it is often used for I/O
4236 * delays, but the vmexits simply slow things down).
4237 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004238 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4239 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004240
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004241 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004242
Avi Kivity58972972009-02-24 22:26:47 +02004243 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4244 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004245
Sheng Yang2384d2b2008-01-17 15:14:33 +08004246 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4247
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004248 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004249 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004250 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004251
Avi Kivity58972972009-02-24 22:26:47 +02004252 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4253 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4254 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4255 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4256 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4257 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004258
Avi Kivity089d0342009-03-23 18:26:32 +02004259 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004260 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004261 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004262 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004263 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004264 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004265 kvm_enable_tdp();
4266 } else
4267 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004268
Avi Kivityc7addb92007-09-16 18:58:32 +02004269 if (bypass_guest_pf)
4270 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4271
He, Qingfdef3ad2007-04-30 09:45:24 +03004272 return 0;
4273
Avi Kivity58972972009-02-24 22:26:47 +02004274out3:
4275 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004276out2:
Avi Kivity58972972009-02-24 22:26:47 +02004277 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004278out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004279 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004280out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004281 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004282 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004283}
4284
4285static void __exit vmx_exit(void)
4286{
Avi Kivity58972972009-02-24 22:26:47 +02004287 free_page((unsigned long)vmx_msr_bitmap_legacy);
4288 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004289 free_page((unsigned long)vmx_io_bitmap_b);
4290 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004291
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004292 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004293}
4294
4295module_init(vmx_init)
4296module_exit(vmx_exit)