blob: 9529bff042626f0eeb5e640a3a85413fa1199641 [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>
Shane Wangcafd6652010-04-29 12:09:01 -040030#include <linux/tboot.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030031#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030032#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040033
Avi Kivity6aa8b732006-12-10 02:21:36 -080034#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080035#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020036#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020037#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080038#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080039
Marcelo Tosatti229456f2009-06-17 09:22:14 -030040#include "trace.h"
41
Avi Kivity4ecac3f2008-05-13 13:23:38 +030042#define __ex(x) __kvm_handle_fault_on_reboot(x)
43
Avi Kivity6aa8b732006-12-10 02:21:36 -080044MODULE_AUTHOR("Qumranet");
45MODULE_LICENSE("GPL");
46
Avi Kivity4462d212009-03-23 17:53:37 +020047static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020048module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020049
Avi Kivity4462d212009-03-23 17:53:37 +020050static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020051module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080052
Avi Kivity4462d212009-03-23 17:53:37 +020053static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020054module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020055
Avi Kivity4462d212009-03-23 17:53:37 +020056static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020057module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080058
Nitin A Kamble3a624e22009-06-08 11:34:16 -070059static int __read_mostly enable_unrestricted_guest = 1;
60module_param_named(unrestricted_guest,
61 enable_unrestricted_guest, bool, S_IRUGO);
62
Avi Kivity4462d212009-03-23 17:53:37 +020063static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020064module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030065
Dongxiao Xub923e622010-05-11 18:29:45 +080066static int __read_mostly vmm_exclusive = 1;
67module_param(vmm_exclusive, bool, S_IRUGO);
68
Avi Kivitycdc0e242009-12-06 17:21:14 +020069#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
70 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
71#define KVM_GUEST_CR0_MASK \
72 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
73#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity81231c62010-01-24 16:26:40 +020074 (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +020075#define KVM_VM_CR0_ALWAYS_ON \
76 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +020077#define KVM_CR4_GUEST_OWNED_BITS \
78 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
79 | X86_CR4_OSXMMEXCPT)
80
Avi Kivitycdc0e242009-12-06 17:21:14 +020081#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
82#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
83
Avi Kivity78ac8b42010-04-08 18:19:35 +030084#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
85
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080086/*
87 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
88 * ple_gap: upper bound on the amount of time between two successive
89 * executions of PAUSE in a loop. Also indicate if ple enabled.
90 * According to test, this time is usually small than 41 cycles.
91 * ple_window: upper bound on the amount of time a guest is allowed to execute
92 * in a PAUSE loop. Tests indicate that most spinlocks are held for
93 * less than 2^12 cycles
94 * Time is measured based on a counter that runs at the same rate as the TSC,
95 * refer SDM volume 3b section 21.6.13 & 22.1.3.
96 */
97#define KVM_VMX_DEFAULT_PLE_GAP 41
98#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
99static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
100module_param(ple_gap, int, S_IRUGO);
101
102static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
103module_param(ple_window, int, S_IRUGO);
104
Avi Kivity61d2ef22010-04-28 16:40:38 +0300105#define NR_AUTOLOAD_MSRS 1
106
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400107struct vmcs {
108 u32 revision_id;
109 u32 abort;
110 char data[0];
111};
112
Avi Kivity26bb0982009-09-07 11:14:12 +0300113struct shared_msr_entry {
114 unsigned index;
115 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200116 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300117};
118
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400119struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000120 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +0300121 struct list_head local_vcpus_link;
Avi Kivity313dbd492008-07-17 18:04:30 +0300122 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400123 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300124 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +0200125 u32 idt_vectoring_info;
Avi Kivity26bb0982009-09-07 11:14:12 +0300126 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400127 int nmsrs;
128 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400129#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300130 u64 msr_host_kernel_gs_base;
131 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400132#endif
133 struct vmcs *vmcs;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300134 struct msr_autoload {
135 unsigned nr;
136 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
137 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
138 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400139 struct {
140 int loaded;
141 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200142 int gs_ldt_reload_needed;
143 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400144 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200145 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300146 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300147 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300148 struct kvm_save_segment {
149 u16 selector;
150 unsigned long base;
151 u32 limit;
152 u32 ar;
153 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200154 struct {
155 bool pending;
156 u8 vector;
157 unsigned rip;
158 } irq;
159 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800160 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300161 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200162
163 /* Support for vnmi-less CPUs */
164 int soft_vnmi_blocked;
165 ktime_t entry_time;
166 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800167 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800168
169 bool rdtscp_enabled;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400170};
171
172static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
173{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000174 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400175}
176
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800177static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800178static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300179
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180static DEFINE_PER_CPU(struct vmcs *, vmxarea);
181static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300182static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200184static unsigned long *vmx_io_bitmap_a;
185static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200186static unsigned long *vmx_msr_bitmap_legacy;
187static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300188
Sheng Yang2384d2b2008-01-17 15:14:33 +0800189static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
190static DEFINE_SPINLOCK(vmx_vpid_lock);
191
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300192static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193 int size;
194 int order;
195 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300196 u32 pin_based_exec_ctrl;
197 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800198 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300199 u32 vmexit_ctrl;
200 u32 vmentry_ctrl;
201} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202
Hannes Ederefff9e52008-11-28 17:02:06 +0100203static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800204 u32 ept;
205 u32 vpid;
206} vmx_capability;
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208#define VMX_SEGMENT_FIELD(seg) \
209 [VCPU_SREG_##seg] = { \
210 .selector = GUEST_##seg##_SELECTOR, \
211 .base = GUEST_##seg##_BASE, \
212 .limit = GUEST_##seg##_LIMIT, \
213 .ar_bytes = GUEST_##seg##_AR_BYTES, \
214 }
215
216static struct kvm_vmx_segment_field {
217 unsigned selector;
218 unsigned base;
219 unsigned limit;
220 unsigned ar_bytes;
221} kvm_vmx_segment_fields[] = {
222 VMX_SEGMENT_FIELD(CS),
223 VMX_SEGMENT_FIELD(DS),
224 VMX_SEGMENT_FIELD(ES),
225 VMX_SEGMENT_FIELD(FS),
226 VMX_SEGMENT_FIELD(GS),
227 VMX_SEGMENT_FIELD(SS),
228 VMX_SEGMENT_FIELD(TR),
229 VMX_SEGMENT_FIELD(LDTR),
230};
231
Avi Kivity26bb0982009-09-07 11:14:12 +0300232static u64 host_efer;
233
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300234static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
235
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300236/*
237 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
238 * away by decrementing the array size.
239 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800241#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300242 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800244 MSR_EFER, MSR_TSC_AUX, MSR_K6_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800245};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200246#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247
Gui Jianfeng31299942010-03-15 17:29:09 +0800248static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249{
250 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
251 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100252 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800253}
254
Gui Jianfeng31299942010-03-15 17:29:09 +0800255static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300256{
257 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
258 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100259 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300260}
261
Gui Jianfeng31299942010-03-15 17:29:09 +0800262static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500263{
264 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
265 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100266 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500267}
268
Gui Jianfeng31299942010-03-15 17:29:09 +0800269static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270{
271 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
272 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
273}
274
Gui Jianfeng31299942010-03-15 17:29:09 +0800275static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800276{
277 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
278 INTR_INFO_VALID_MASK)) ==
279 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
280}
281
Gui Jianfeng31299942010-03-15 17:29:09 +0800282static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800283{
Sheng Yang04547152009-04-01 15:52:31 +0800284 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800285}
286
Gui Jianfeng31299942010-03-15 17:29:09 +0800287static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800288{
Sheng Yang04547152009-04-01 15:52:31 +0800289 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800290}
291
Gui Jianfeng31299942010-03-15 17:29:09 +0800292static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800293{
Sheng Yang04547152009-04-01 15:52:31 +0800294 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800295}
296
Gui Jianfeng31299942010-03-15 17:29:09 +0800297static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800298{
Sheng Yang04547152009-04-01 15:52:31 +0800299 return vmcs_config.cpu_based_exec_ctrl &
300 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800301}
302
Avi Kivity774ead32007-12-26 13:57:04 +0200303static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800304{
Sheng Yang04547152009-04-01 15:52:31 +0800305 return vmcs_config.cpu_based_2nd_exec_ctrl &
306 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
307}
308
309static inline bool cpu_has_vmx_flexpriority(void)
310{
311 return cpu_has_vmx_tpr_shadow() &&
312 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800313}
314
Marcelo Tosattie7997942009-06-11 12:07:40 -0300315static inline bool cpu_has_vmx_ept_execute_only(void)
316{
Gui Jianfeng31299942010-03-15 17:29:09 +0800317 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300318}
319
320static inline bool cpu_has_vmx_eptp_uncacheable(void)
321{
Gui Jianfeng31299942010-03-15 17:29:09 +0800322 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300323}
324
325static inline bool cpu_has_vmx_eptp_writeback(void)
326{
Gui Jianfeng31299942010-03-15 17:29:09 +0800327 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300328}
329
330static inline bool cpu_has_vmx_ept_2m_page(void)
331{
Gui Jianfeng31299942010-03-15 17:29:09 +0800332 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300333}
334
Sheng Yang878403b2010-01-05 19:02:29 +0800335static inline bool cpu_has_vmx_ept_1g_page(void)
336{
Gui Jianfeng31299942010-03-15 17:29:09 +0800337 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800338}
339
Gui Jianfeng31299942010-03-15 17:29:09 +0800340static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800341{
Gui Jianfeng31299942010-03-15 17:29:09 +0800342 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800343}
344
Gui Jianfeng31299942010-03-15 17:29:09 +0800345static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800346{
Gui Jianfeng31299942010-03-15 17:29:09 +0800347 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800348}
349
Gui Jianfeng31299942010-03-15 17:29:09 +0800350static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800351{
Gui Jianfeng31299942010-03-15 17:29:09 +0800352 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800353}
354
Gui Jianfeng31299942010-03-15 17:29:09 +0800355static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800356{
Sheng Yang04547152009-04-01 15:52:31 +0800357 return vmcs_config.cpu_based_2nd_exec_ctrl &
358 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800359}
360
Gui Jianfeng31299942010-03-15 17:29:09 +0800361static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700362{
363 return vmcs_config.cpu_based_2nd_exec_ctrl &
364 SECONDARY_EXEC_UNRESTRICTED_GUEST;
365}
366
Gui Jianfeng31299942010-03-15 17:29:09 +0800367static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800368{
369 return vmcs_config.cpu_based_2nd_exec_ctrl &
370 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
371}
372
Gui Jianfeng31299942010-03-15 17:29:09 +0800373static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800374{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800375 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800376}
377
Gui Jianfeng31299942010-03-15 17:29:09 +0800378static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800379{
Sheng Yang04547152009-04-01 15:52:31 +0800380 return vmcs_config.cpu_based_2nd_exec_ctrl &
381 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800382}
383
Gui Jianfeng31299942010-03-15 17:29:09 +0800384static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800385{
386 return vmcs_config.cpu_based_2nd_exec_ctrl &
387 SECONDARY_EXEC_RDTSCP;
388}
389
Gui Jianfeng31299942010-03-15 17:29:09 +0800390static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800391{
392 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
393}
394
Sheng Yang04547152009-04-01 15:52:31 +0800395static inline bool report_flexpriority(void)
396{
397 return flexpriority_enabled;
398}
399
Rusty Russell8b9cf982007-07-30 16:31:43 +1000400static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800401{
402 int i;
403
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400404 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300405 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300406 return i;
407 return -1;
408}
409
Sheng Yang2384d2b2008-01-17 15:14:33 +0800410static inline void __invvpid(int ext, u16 vpid, gva_t gva)
411{
412 struct {
413 u64 vpid : 16;
414 u64 rsvd : 48;
415 u64 gva;
416 } operand = { vpid, 0, gva };
417
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300418 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800419 /* CF==1 or ZF==1 --> rc = -1 */
420 "; ja 1f ; ud2 ; 1:"
421 : : "a"(&operand), "c"(ext) : "cc", "memory");
422}
423
Sheng Yang14394422008-04-28 12:24:45 +0800424static inline void __invept(int ext, u64 eptp, gpa_t gpa)
425{
426 struct {
427 u64 eptp, gpa;
428 } operand = {eptp, gpa};
429
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300430 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800431 /* CF==1 or ZF==1 --> rc = -1 */
432 "; ja 1f ; ud2 ; 1:\n"
433 : : "a" (&operand), "c" (ext) : "cc", "memory");
434}
435
Avi Kivity26bb0982009-09-07 11:14:12 +0300436static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300437{
438 int i;
439
Rusty Russell8b9cf982007-07-30 16:31:43 +1000440 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300441 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400442 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000443 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800444}
445
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446static void vmcs_clear(struct vmcs *vmcs)
447{
448 u64 phys_addr = __pa(vmcs);
449 u8 error;
450
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300451 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
453 : "cc", "memory");
454 if (error)
455 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
456 vmcs, phys_addr);
457}
458
Dongxiao Xu7725b892010-05-11 18:29:38 +0800459static void vmcs_load(struct vmcs *vmcs)
460{
461 u64 phys_addr = __pa(vmcs);
462 u8 error;
463
464 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
465 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
466 : "cc", "memory");
467 if (error)
468 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
469 vmcs, phys_addr);
470}
471
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472static void __vcpu_clear(void *arg)
473{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000474 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800475 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476
Rusty Russell8b9cf982007-07-30 16:31:43 +1000477 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400478 vmcs_clear(vmx->vmcs);
479 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800481 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300482 list_del(&vmx->local_vcpus_link);
483 vmx->vcpu.cpu = -1;
484 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485}
486
Rusty Russell8b9cf982007-07-30 16:31:43 +1000487static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800488{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200489 if (vmx->vcpu.cpu == -1)
490 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200491 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800492}
493
Sheng Yang2384d2b2008-01-17 15:14:33 +0800494static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
495{
496 if (vmx->vpid == 0)
497 return;
498
499 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
500}
501
Sheng Yang14394422008-04-28 12:24:45 +0800502static inline void ept_sync_global(void)
503{
504 if (cpu_has_vmx_invept_global())
505 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
506}
507
508static inline void ept_sync_context(u64 eptp)
509{
Avi Kivity089d0342009-03-23 18:26:32 +0200510 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800511 if (cpu_has_vmx_invept_context())
512 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
513 else
514 ept_sync_global();
515 }
516}
517
518static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
519{
Avi Kivity089d0342009-03-23 18:26:32 +0200520 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800521 if (cpu_has_vmx_invept_individual_addr())
522 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
523 eptp, gpa);
524 else
525 ept_sync_context(eptp);
526 }
527}
528
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529static unsigned long vmcs_readl(unsigned long field)
530{
531 unsigned long value;
532
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300533 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534 : "=a"(value) : "d"(field) : "cc");
535 return value;
536}
537
538static u16 vmcs_read16(unsigned long field)
539{
540 return vmcs_readl(field);
541}
542
543static u32 vmcs_read32(unsigned long field)
544{
545 return vmcs_readl(field);
546}
547
548static u64 vmcs_read64(unsigned long field)
549{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800550#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551 return vmcs_readl(field);
552#else
553 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
554#endif
555}
556
Avi Kivitye52de1b2007-01-05 16:36:56 -0800557static noinline void vmwrite_error(unsigned long field, unsigned long value)
558{
559 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
560 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
561 dump_stack();
562}
563
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564static void vmcs_writel(unsigned long field, unsigned long value)
565{
566 u8 error;
567
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300568 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400569 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800570 if (unlikely(error))
571 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800572}
573
574static void vmcs_write16(unsigned long field, u16 value)
575{
576 vmcs_writel(field, value);
577}
578
579static void vmcs_write32(unsigned long field, u32 value)
580{
581 vmcs_writel(field, value);
582}
583
584static void vmcs_write64(unsigned long field, u64 value)
585{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300587#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800588 asm volatile ("");
589 vmcs_writel(field+1, value >> 32);
590#endif
591}
592
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300593static void vmcs_clear_bits(unsigned long field, u32 mask)
594{
595 vmcs_writel(field, vmcs_readl(field) & ~mask);
596}
597
598static void vmcs_set_bits(unsigned long field, u32 mask)
599{
600 vmcs_writel(field, vmcs_readl(field) | mask);
601}
602
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300603static void update_exception_bitmap(struct kvm_vcpu *vcpu)
604{
605 u32 eb;
606
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100607 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
608 (1u << NM_VECTOR) | (1u << DB_VECTOR);
609 if ((vcpu->guest_debug &
610 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
611 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
612 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300613 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300614 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200615 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800616 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200617 if (vcpu->fpu_active)
618 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300619 vmcs_write32(EXCEPTION_BITMAP, eb);
620}
621
Avi Kivity61d2ef22010-04-28 16:40:38 +0300622static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
623{
624 unsigned i;
625 struct msr_autoload *m = &vmx->msr_autoload;
626
627 for (i = 0; i < m->nr; ++i)
628 if (m->guest[i].index == msr)
629 break;
630
631 if (i == m->nr)
632 return;
633 --m->nr;
634 m->guest[i] = m->guest[m->nr];
635 m->host[i] = m->host[m->nr];
636 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
637 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
638}
639
640static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
641 u64 guest_val, u64 host_val)
642{
643 unsigned i;
644 struct msr_autoload *m = &vmx->msr_autoload;
645
646 for (i = 0; i < m->nr; ++i)
647 if (m->guest[i].index == msr)
648 break;
649
650 if (i == m->nr) {
651 ++m->nr;
652 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
653 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
654 }
655
656 m->guest[i].index = msr;
657 m->guest[i].value = guest_val;
658 m->host[i].index = msr;
659 m->host[i].value = host_val;
660}
661
Avi Kivity33ed6322007-05-02 16:54:03 +0300662static void reload_tss(void)
663{
Avi Kivity33ed6322007-05-02 16:54:03 +0300664 /*
665 * VT restores TR but not its size. Useless.
666 */
Gleb Natapov89a27f42010-02-16 10:51:48 +0200667 struct desc_ptr gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200668 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300669
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200670 native_store_gdt(&gdt);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200671 descs = (void *)gdt.address;
Avi Kivity33ed6322007-05-02 16:54:03 +0300672 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
673 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300674}
675
Avi Kivity92c0d902009-10-29 11:00:16 +0200676static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300677{
Roel Kluin3a34a882009-08-04 02:08:45 -0700678 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300679 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300680
Avi Kivityf6801df2010-01-21 15:31:50 +0200681 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700682
Avi Kivity51c6cf62007-08-29 03:48:05 +0300683 /*
684 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
685 * outside long mode
686 */
687 ignore_bits = EFER_NX | EFER_SCE;
688#ifdef CONFIG_X86_64
689 ignore_bits |= EFER_LMA | EFER_LME;
690 /* SCE is meaningful only in long mode on Intel */
691 if (guest_efer & EFER_LMA)
692 ignore_bits &= ~(u64)EFER_SCE;
693#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300694 guest_efer &= ~ignore_bits;
695 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300696 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200697 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300698
699 clear_atomic_switch_msr(vmx, MSR_EFER);
700 /* On ept, can't emulate nx, and must switch nx atomically */
701 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
702 guest_efer = vmx->vcpu.arch.efer;
703 if (!(guest_efer & EFER_LMA))
704 guest_efer &= ~EFER_LME;
705 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
706 return false;
707 }
708
Avi Kivity26bb0982009-09-07 11:14:12 +0300709 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300710}
711
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200712static unsigned long segment_base(u16 selector)
713{
714 struct desc_ptr gdt;
715 struct desc_struct *d;
716 unsigned long table_base;
717 unsigned long v;
718
719 if (!(selector & ~3))
720 return 0;
721
722 native_store_gdt(&gdt);
723 table_base = gdt.address;
724
725 if (selector & 4) { /* from ldt */
726 u16 ldt_selector = kvm_read_ldt();
727
728 if (!(ldt_selector & ~3))
729 return 0;
730
731 table_base = segment_base(ldt_selector);
732 }
733 d = (struct desc_struct *)(table_base + (selector & ~7));
734 v = get_desc_base(d);
735#ifdef CONFIG_X86_64
736 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
737 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
738#endif
739 return v;
740}
741
742static inline unsigned long kvm_read_tr_base(void)
743{
744 u16 tr;
745 asm("str %0" : "=g"(tr));
746 return segment_base(tr);
747}
748
Avi Kivity04d2cc72007-09-10 18:10:54 +0300749static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300750{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300751 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300752 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300753
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400754 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300755 return;
756
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400757 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300758 /*
759 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
760 * allow segment selectors with cpl > 0 or ti == 1.
761 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300762 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200763 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300764 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200765 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400766 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200767 vmx->host_state.fs_reload_needed = 0;
768 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300769 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200770 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300771 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300772 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400773 if (!(vmx->host_state.gs_sel & 7))
774 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300775 else {
776 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200777 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300778 }
779
780#ifdef CONFIG_X86_64
781 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
782 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
783#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400784 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
785 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300786#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300787
788#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300789 if (is_long_mode(&vmx->vcpu)) {
790 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
791 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
792 }
Avi Kivity707c0872007-05-02 17:33:43 +0300793#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300794 for (i = 0; i < vmx->save_nmsrs; ++i)
795 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200796 vmx->guest_msrs[i].data,
797 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300798}
799
Avi Kivitya9b21b62008-06-24 11:48:49 +0300800static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300801{
Avi Kivity15ad7142007-07-11 18:17:21 +0300802 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300803
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400804 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300805 return;
806
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200807 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400808 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200809 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300810 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200811 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300812 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300813 /*
814 * If we have to reload gs, we must take care to
815 * preserve our gs base.
816 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300817 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300818 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300819#ifdef CONFIG_X86_64
820 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
821#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300822 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300823 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200824 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300825#ifdef CONFIG_X86_64
826 if (is_long_mode(&vmx->vcpu)) {
827 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
828 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
829 }
830#endif
Avi Kivity1c11e712010-05-03 16:05:44 +0300831 if (current_thread_info()->status & TS_USEDFPU)
832 clts();
Avi Kivity33ed6322007-05-02 16:54:03 +0300833}
834
Avi Kivitya9b21b62008-06-24 11:48:49 +0300835static void vmx_load_host_state(struct vcpu_vmx *vmx)
836{
837 preempt_disable();
838 __vmx_load_host_state(vmx);
839 preempt_enable();
840}
841
Avi Kivity6aa8b732006-12-10 02:21:36 -0800842/*
843 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
844 * vcpu mutex is already taken.
845 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300846static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800847{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400848 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity019960a2008-03-04 10:44:51 +0200849 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850
Dongxiao Xub923e622010-05-11 18:29:45 +0800851 if (vmm_exclusive && vcpu->cpu != cpu)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000852 vcpu_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800853
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400854 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400855 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Dongxiao Xu7725b892010-05-11 18:29:38 +0800856 vmcs_load(vmx->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857 }
858
859 if (vcpu->cpu != cpu) {
Gleb Natapov89a27f42010-02-16 10:51:48 +0200860 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800861 unsigned long sysenter_esp;
862
Dongxiao Xu92fe13b2010-05-11 18:29:42 +0800863 kvm_migrate_timers(vcpu);
864 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
865 local_irq_disable();
866 list_add(&vmx->local_vcpus_link,
867 &per_cpu(vcpus_on_cpu, cpu));
868 local_irq_enable();
869
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870 vcpu->cpu = cpu;
871 /*
872 * Linux uses per-cpu TSS and GDT, so set these when switching
873 * processors.
874 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300875 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200876 native_store_gdt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200877 vmcs_writel(HOST_GDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800878
879 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
880 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300881
882 /*
883 * Make sure the time stamp counter is monotonous.
884 */
885 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200886 if (tsc_this < vcpu->arch.host_tsc) {
887 delta = vcpu->arch.host_tsc - tsc_this;
888 new_offset = vmcs_read64(TSC_OFFSET) + delta;
889 vmcs_write64(TSC_OFFSET, new_offset);
890 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800892}
893
894static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
895{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300896 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xub923e622010-05-11 18:29:45 +0800897 if (!vmm_exclusive)
898 __vcpu_clear(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800899}
900
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300901static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
902{
Avi Kivity81231c62010-01-24 16:26:40 +0200903 ulong cr0;
904
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300905 if (vcpu->fpu_active)
906 return;
907 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +0200908 cr0 = vmcs_readl(GUEST_CR0);
909 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
910 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
911 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300912 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +0200913 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
914 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300915}
916
Avi Kivityedcafe32009-12-30 18:07:40 +0200917static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
918
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300919static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
920{
Avi Kivityedcafe32009-12-30 18:07:40 +0200921 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +0200922 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300923 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +0200924 vcpu->arch.cr0_guest_owned_bits = 0;
925 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
926 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300927}
928
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
930{
Avi Kivity78ac8b42010-04-08 18:19:35 +0300931 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +0300932
933 rflags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +0300934 if (to_vmx(vcpu)->rmode.vm86_active) {
935 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
936 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
937 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
938 }
Avi Kivity345dcaa2009-08-12 15:29:37 +0300939 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940}
941
942static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
943{
Avi Kivity78ac8b42010-04-08 18:19:35 +0300944 if (to_vmx(vcpu)->rmode.vm86_active) {
945 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100946 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300947 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800948 vmcs_writel(GUEST_RFLAGS, rflags);
949}
950
Glauber Costa2809f5d2009-05-12 16:21:05 -0400951static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
952{
953 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
954 int ret = 0;
955
956 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +0100957 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400958 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +0100959 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400960
961 return ret & mask;
962}
963
964static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
965{
966 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
967 u32 interruptibility = interruptibility_old;
968
969 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
970
Jan Kiszka48005f62010-02-19 19:38:07 +0100971 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400972 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +0100973 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400974 interruptibility |= GUEST_INTR_STATE_STI;
975
976 if ((interruptibility != interruptibility_old))
977 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
978}
979
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
981{
982 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800983
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300984 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800985 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300986 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987
Glauber Costa2809f5d2009-05-12 16:21:05 -0400988 /* skipping an emulated instruction also counts */
989 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990}
991
Avi Kivity298101d2007-11-25 13:41:11 +0200992static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200993 bool has_error_code, u32 error_code,
994 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +0200995{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200996 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100997 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200998
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100999 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001000 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001001 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1002 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001003
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001004 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001005 vmx->rmode.irq.pending = true;
1006 vmx->rmode.irq.vector = nr;
1007 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03001008 if (kvm_exception_is_soft(nr))
1009 vmx->rmode.irq.rip +=
1010 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001011 intr_info |= INTR_TYPE_SOFT_INTR;
1012 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001013 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
1014 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
1015 return;
1016 }
1017
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001018 if (kvm_exception_is_soft(nr)) {
1019 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1020 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001021 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1022 } else
1023 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1024
1025 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +02001026}
1027
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001028static bool vmx_rdtscp_supported(void)
1029{
1030 return cpu_has_vmx_rdtscp();
1031}
1032
Avi Kivity6aa8b732006-12-10 02:21:36 -08001033/*
Eddie Donga75beee2007-05-17 18:55:15 +03001034 * Swap MSR entry in host/guest MSR entry array.
1035 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001036static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001037{
Avi Kivity26bb0982009-09-07 11:14:12 +03001038 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001039
1040 tmp = vmx->guest_msrs[to];
1041 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1042 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001043}
1044
1045/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001046 * Set up the vmcs to automatically save and restore system
1047 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1048 * mode, as fiddling with msrs is very expensive.
1049 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001050static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001051{
Avi Kivity26bb0982009-09-07 11:14:12 +03001052 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +02001053 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +03001054
Avi Kivity33f9c502008-02-27 16:06:57 +02001055 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +03001056 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001057#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001058 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001059 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001060 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001061 move_msr_up(vmx, index, save_nmsrs++);
1062 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001063 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001064 move_msr_up(vmx, index, save_nmsrs++);
1065 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001066 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001067 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001068 index = __find_msr_index(vmx, MSR_TSC_AUX);
1069 if (index >= 0 && vmx->rdtscp_enabled)
1070 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001071 /*
1072 * MSR_K6_STAR is only needed on long mode guests, and only
1073 * if efer.sce is enabled.
1074 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001075 index = __find_msr_index(vmx, MSR_K6_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001076 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001077 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001078 }
Eddie Donga75beee2007-05-17 18:55:15 +03001079#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001080 index = __find_msr_index(vmx, MSR_EFER);
1081 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001082 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001083
Avi Kivity26bb0982009-09-07 11:14:12 +03001084 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001085
1086 if (cpu_has_vmx_msr_bitmap()) {
1087 if (is_long_mode(&vmx->vcpu))
1088 msr_bitmap = vmx_msr_bitmap_longmode;
1089 else
1090 msr_bitmap = vmx_msr_bitmap_legacy;
1091
1092 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1093 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001094}
1095
1096/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097 * reads and returns guest's timestamp counter "register"
1098 * guest_tsc = host_tsc + tsc_offset -- 21.3
1099 */
1100static u64 guest_read_tsc(void)
1101{
1102 u64 host_tsc, tsc_offset;
1103
1104 rdtscll(host_tsc);
1105 tsc_offset = vmcs_read64(TSC_OFFSET);
1106 return host_tsc + tsc_offset;
1107}
1108
1109/*
1110 * writes 'guest_tsc' into guest's timestamp counter "register"
1111 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
1112 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001113static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
1116}
1117
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118/*
1119 * Reads an msr value (of 'msr_index') into 'pdata'.
1120 * Returns 0 on success, non-0 otherwise.
1121 * Assumes vcpu_load() was already called.
1122 */
1123static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1124{
1125 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001126 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127
1128 if (!pdata) {
1129 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1130 return -EINVAL;
1131 }
1132
1133 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001134#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 case MSR_FS_BASE:
1136 data = vmcs_readl(GUEST_FS_BASE);
1137 break;
1138 case MSR_GS_BASE:
1139 data = vmcs_readl(GUEST_GS_BASE);
1140 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001141 case MSR_KERNEL_GS_BASE:
1142 vmx_load_host_state(to_vmx(vcpu));
1143 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1144 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001145#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001146 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001147 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301148 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 data = guest_read_tsc();
1150 break;
1151 case MSR_IA32_SYSENTER_CS:
1152 data = vmcs_read32(GUEST_SYSENTER_CS);
1153 break;
1154 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001155 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 break;
1157 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001158 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001159 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001160 case MSR_TSC_AUX:
1161 if (!to_vmx(vcpu)->rdtscp_enabled)
1162 return 1;
1163 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001164 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001165 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001166 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001167 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001168 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001169 data = msr->data;
1170 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001172 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173 }
1174
1175 *pdata = data;
1176 return 0;
1177}
1178
1179/*
1180 * Writes msr value into into the appropriate "register".
1181 * Returns 0 on success, non-0 otherwise.
1182 * Assumes vcpu_load() was already called.
1183 */
1184static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1185{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001186 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001187 struct shared_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001188 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001189 int ret = 0;
1190
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001192 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001193 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001194 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001195 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001196#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197 case MSR_FS_BASE:
1198 vmcs_writel(GUEST_FS_BASE, data);
1199 break;
1200 case MSR_GS_BASE:
1201 vmcs_writel(GUEST_GS_BASE, data);
1202 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001203 case MSR_KERNEL_GS_BASE:
1204 vmx_load_host_state(vmx);
1205 vmx->msr_guest_kernel_gs_base = data;
1206 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001207#endif
1208 case MSR_IA32_SYSENTER_CS:
1209 vmcs_write32(GUEST_SYSENTER_CS, data);
1210 break;
1211 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001212 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213 break;
1214 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001215 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301217 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001218 rdtscll(host_tsc);
1219 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001221 case MSR_IA32_CR_PAT:
1222 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1223 vmcs_write64(GUEST_IA32_PAT, data);
1224 vcpu->arch.pat = data;
1225 break;
1226 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001227 ret = kvm_set_msr_common(vcpu, msr_index, data);
1228 break;
1229 case MSR_TSC_AUX:
1230 if (!vmx->rdtscp_enabled)
1231 return 1;
1232 /* Check reserved bit, higher 32 bits should be zero */
1233 if ((data >> 32) != 0)
1234 return 1;
1235 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001236 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001237 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001238 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001239 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001240 msr->data = data;
1241 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001242 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001243 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244 }
1245
Eddie Dong2cc51562007-05-21 07:28:09 +03001246 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001247}
1248
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001249static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001250{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001251 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1252 switch (reg) {
1253 case VCPU_REGS_RSP:
1254 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1255 break;
1256 case VCPU_REGS_RIP:
1257 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1258 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001259 case VCPU_EXREG_PDPTR:
1260 if (enable_ept)
1261 ept_save_pdptrs(vcpu);
1262 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001263 default:
1264 break;
1265 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266}
1267
Jan Kiszka355be0b2009-10-03 00:31:21 +02001268static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001269{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001270 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1271 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1272 else
1273 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1274
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001275 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001276}
1277
1278static __init int cpu_has_kvm_support(void)
1279{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001280 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001281}
1282
1283static __init int vmx_disabled_by_bios(void)
1284{
1285 u64 msr;
1286
1287 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001288 if (msr & FEATURE_CONTROL_LOCKED) {
1289 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1290 && tboot_enabled())
1291 return 1;
1292 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1293 && !tboot_enabled())
1294 return 1;
1295 }
1296
1297 return 0;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001298 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299}
1300
Dongxiao Xu7725b892010-05-11 18:29:38 +08001301static void kvm_cpu_vmxon(u64 addr)
1302{
1303 asm volatile (ASM_VMX_VMXON_RAX
1304 : : "a"(&addr), "m"(addr)
1305 : "memory", "cc");
1306}
1307
Alexander Graf10474ae2009-09-15 11:37:46 +02001308static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001309{
1310 int cpu = raw_smp_processor_id();
1311 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001312 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001313
Alexander Graf10474ae2009-09-15 11:37:46 +02001314 if (read_cr4() & X86_CR4_VMXE)
1315 return -EBUSY;
1316
Avi Kivity543e4242008-05-13 16:22:47 +03001317 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001318 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001319
1320 test_bits = FEATURE_CONTROL_LOCKED;
1321 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1322 if (tboot_enabled())
1323 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1324
1325 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001327 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1328 }
Rusty Russell66aee912007-07-17 23:34:16 +10001329 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Dongxiao Xu7725b892010-05-11 18:29:38 +08001330 kvm_cpu_vmxon(phys_addr);
Alexander Graf10474ae2009-09-15 11:37:46 +02001331
1332 ept_sync_global();
1333
1334 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001335}
1336
Avi Kivity543e4242008-05-13 16:22:47 +03001337static void vmclear_local_vcpus(void)
1338{
1339 int cpu = raw_smp_processor_id();
1340 struct vcpu_vmx *vmx, *n;
1341
1342 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1343 local_vcpus_link)
1344 __vcpu_clear(vmx);
1345}
1346
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001347
1348/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1349 * tricks.
1350 */
1351static void kvm_cpu_vmxoff(void)
1352{
1353 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001354}
1355
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356static void hardware_disable(void *garbage)
1357{
Avi Kivity543e4242008-05-13 16:22:47 +03001358 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001359 kvm_cpu_vmxoff();
Dongxiao Xu7725b892010-05-11 18:29:38 +08001360 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361}
1362
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001363static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001364 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365{
1366 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001367 u32 ctl = ctl_min | ctl_opt;
1368
1369 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1370
1371 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1372 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1373
1374 /* Ensure minimum (required) set of control bits are supported. */
1375 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001376 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001377
1378 *result = ctl;
1379 return 0;
1380}
1381
Yang, Sheng002c7f72007-07-31 14:23:01 +03001382static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001383{
1384 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001385 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001386 u32 _pin_based_exec_control = 0;
1387 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001388 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001389 u32 _vmexit_control = 0;
1390 u32 _vmentry_control = 0;
1391
1392 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001393 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001394 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1395 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001396 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001397
1398 min = CPU_BASED_HLT_EXITING |
1399#ifdef CONFIG_X86_64
1400 CPU_BASED_CR8_LOAD_EXITING |
1401 CPU_BASED_CR8_STORE_EXITING |
1402#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001403 CPU_BASED_CR3_LOAD_EXITING |
1404 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001405 CPU_BASED_USE_IO_BITMAPS |
1406 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001407 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001408 CPU_BASED_MWAIT_EXITING |
1409 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001410 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001411 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001412 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001413 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001414 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1415 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001416 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001417#ifdef CONFIG_X86_64
1418 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1419 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1420 ~CPU_BASED_CR8_STORE_EXITING;
1421#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001422 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001423 min2 = 0;
1424 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001425 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001426 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001427 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001428 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001429 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1430 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001431 if (adjust_vmx_controls(min2, opt2,
1432 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001433 &_cpu_based_2nd_exec_control) < 0)
1434 return -EIO;
1435 }
1436#ifndef CONFIG_X86_64
1437 if (!(_cpu_based_2nd_exec_control &
1438 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1439 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1440#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001441 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001442 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1443 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001444 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1445 CPU_BASED_CR3_STORE_EXITING |
1446 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001447 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1448 vmx_capability.ept, vmx_capability.vpid);
1449 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001450
1451 min = 0;
1452#ifdef CONFIG_X86_64
1453 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1454#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001455 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001456 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1457 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001458 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001459
Sheng Yang468d4722008-10-09 16:01:55 +08001460 min = 0;
1461 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001462 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1463 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001464 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001465
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001466 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001467
1468 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1469 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001470 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001471
1472#ifdef CONFIG_X86_64
1473 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1474 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001475 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001476#endif
1477
1478 /* Require Write-Back (WB) memory type for VMCS accesses. */
1479 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001480 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001481
Yang, Sheng002c7f72007-07-31 14:23:01 +03001482 vmcs_conf->size = vmx_msr_high & 0x1fff;
1483 vmcs_conf->order = get_order(vmcs_config.size);
1484 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001485
Yang, Sheng002c7f72007-07-31 14:23:01 +03001486 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1487 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001488 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001489 vmcs_conf->vmexit_ctrl = _vmexit_control;
1490 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001491
1492 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001493}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494
1495static struct vmcs *alloc_vmcs_cpu(int cpu)
1496{
1497 int node = cpu_to_node(cpu);
1498 struct page *pages;
1499 struct vmcs *vmcs;
1500
Mel Gorman6484eb32009-06-16 15:31:54 -07001501 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001502 if (!pages)
1503 return NULL;
1504 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001505 memset(vmcs, 0, vmcs_config.size);
1506 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507 return vmcs;
1508}
1509
1510static struct vmcs *alloc_vmcs(void)
1511{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001512 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513}
1514
1515static void free_vmcs(struct vmcs *vmcs)
1516{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001517 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518}
1519
Sam Ravnborg39959582007-06-01 00:47:13 -07001520static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001521{
1522 int cpu;
1523
Zachary Amsden3230bb42009-09-29 11:38:37 -10001524 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001526 per_cpu(vmxarea, cpu) = NULL;
1527 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528}
1529
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530static __init int alloc_kvm_area(void)
1531{
1532 int cpu;
1533
Zachary Amsden3230bb42009-09-29 11:38:37 -10001534 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001535 struct vmcs *vmcs;
1536
1537 vmcs = alloc_vmcs_cpu(cpu);
1538 if (!vmcs) {
1539 free_kvm_area();
1540 return -ENOMEM;
1541 }
1542
1543 per_cpu(vmxarea, cpu) = vmcs;
1544 }
1545 return 0;
1546}
1547
1548static __init int hardware_setup(void)
1549{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001550 if (setup_vmcs_config(&vmcs_config) < 0)
1551 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001552
1553 if (boot_cpu_has(X86_FEATURE_NX))
1554 kvm_enable_efer_bits(EFER_NX);
1555
Sheng Yang93ba03c2009-04-01 15:52:32 +08001556 if (!cpu_has_vmx_vpid())
1557 enable_vpid = 0;
1558
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001559 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001560 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001561 enable_unrestricted_guest = 0;
1562 }
1563
1564 if (!cpu_has_vmx_unrestricted_guest())
1565 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001566
1567 if (!cpu_has_vmx_flexpriority())
1568 flexpriority_enabled = 0;
1569
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001570 if (!cpu_has_vmx_tpr_shadow())
1571 kvm_x86_ops->update_cr8_intercept = NULL;
1572
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001573 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1574 kvm_disable_largepages();
1575
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001576 if (!cpu_has_vmx_ple())
1577 ple_gap = 0;
1578
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 return alloc_kvm_area();
1580}
1581
1582static __exit void hardware_unsetup(void)
1583{
1584 free_kvm_area();
1585}
1586
Avi Kivity6aa8b732006-12-10 02:21:36 -08001587static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1588{
1589 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1590
Avi Kivity6af11b92007-03-19 13:18:10 +02001591 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592 vmcs_write16(sf->selector, save->selector);
1593 vmcs_writel(sf->base, save->base);
1594 vmcs_write32(sf->limit, save->limit);
1595 vmcs_write32(sf->ar_bytes, save->ar);
1596 } else {
1597 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1598 << AR_DPL_SHIFT;
1599 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1600 }
1601}
1602
1603static void enter_pmode(struct kvm_vcpu *vcpu)
1604{
1605 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001606 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001607
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001608 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001609 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001611 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1612 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1613 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001614
1615 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001616 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1617 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001618 vmcs_writel(GUEST_RFLAGS, flags);
1619
Rusty Russell66aee912007-07-17 23:34:16 +10001620 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1621 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001622
1623 update_exception_bitmap(vcpu);
1624
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001625 if (emulate_invalid_guest_state)
1626 return;
1627
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001628 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1629 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1630 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1631 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632
1633 vmcs_write16(GUEST_SS_SELECTOR, 0);
1634 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1635
1636 vmcs_write16(GUEST_CS_SELECTOR,
1637 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1638 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1639}
1640
Mike Dayd77c26f2007-10-08 09:02:08 -04001641static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001643 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001644 struct kvm_memslots *slots;
1645 gfn_t base_gfn;
1646
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001647 slots = kvm_memslots(kvm);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001648 base_gfn = kvm->memslots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001649 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02001650 return base_gfn << PAGE_SHIFT;
1651 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001652 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653}
1654
1655static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1656{
1657 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1658
1659 save->selector = vmcs_read16(sf->selector);
1660 save->base = vmcs_readl(sf->base);
1661 save->limit = vmcs_read32(sf->limit);
1662 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001663 vmcs_write16(sf->selector, save->base >> 4);
1664 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001665 vmcs_write32(sf->limit, 0xffff);
1666 vmcs_write32(sf->ar_bytes, 0xf3);
1667}
1668
1669static void enter_rmode(struct kvm_vcpu *vcpu)
1670{
1671 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001672 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001674 if (enable_unrestricted_guest)
1675 return;
1676
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001677 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001678 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001679
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001680 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1682
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001683 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001684 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1685
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001686 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001687 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1688
1689 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001690 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001691
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001692 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693
1694 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001695 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001696 update_exception_bitmap(vcpu);
1697
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001698 if (emulate_invalid_guest_state)
1699 goto continue_rmode;
1700
Avi Kivity6aa8b732006-12-10 02:21:36 -08001701 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1702 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1703 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1704
1705 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001706 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001707 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1708 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1710
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001711 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1712 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1713 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1714 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001715
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001716continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001717 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001718 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001719}
1720
Amit Shah401d10d2009-02-20 22:53:37 +05301721static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1722{
1723 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001724 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1725
1726 if (!msr)
1727 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301728
Avi Kivity44ea2b12009-09-06 15:55:37 +03001729 /*
1730 * Force kernel_gs_base reloading before EFER changes, as control
1731 * of this msr depends on is_long_mode().
1732 */
1733 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02001734 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05301735 if (efer & EFER_LMA) {
1736 vmcs_write32(VM_ENTRY_CONTROLS,
1737 vmcs_read32(VM_ENTRY_CONTROLS) |
1738 VM_ENTRY_IA32E_MODE);
1739 msr->data = efer;
1740 } else {
1741 vmcs_write32(VM_ENTRY_CONTROLS,
1742 vmcs_read32(VM_ENTRY_CONTROLS) &
1743 ~VM_ENTRY_IA32E_MODE);
1744
1745 msr->data = efer & ~EFER_LME;
1746 }
1747 setup_msrs(vmx);
1748}
1749
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001750#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001751
1752static void enter_lmode(struct kvm_vcpu *vcpu)
1753{
1754 u32 guest_tr_ar;
1755
1756 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1757 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1758 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001759 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760 vmcs_write32(GUEST_TR_AR_BYTES,
1761 (guest_tr_ar & ~AR_TYPE_MASK)
1762 | AR_TYPE_BUSY_64_TSS);
1763 }
Avi Kivityda38f432010-07-06 11:30:49 +03001764 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001765}
1766
1767static void exit_lmode(struct kvm_vcpu *vcpu)
1768{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769 vmcs_write32(VM_ENTRY_CONTROLS,
1770 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001771 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03001772 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001773}
1774
1775#endif
1776
Sheng Yang2384d2b2008-01-17 15:14:33 +08001777static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1778{
1779 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001780 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001781 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001782}
1783
Avi Kivitye8467fd2009-12-29 18:43:06 +02001784static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1785{
1786 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
1787
1788 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
1789 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
1790}
1791
Anthony Liguori25c4c272007-04-27 09:29:21 +03001792static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001793{
Avi Kivityfc78f512009-12-07 12:16:48 +02001794 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
1795
1796 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
1797 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08001798}
1799
Sheng Yang14394422008-04-28 12:24:45 +08001800static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1801{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001802 if (!test_bit(VCPU_EXREG_PDPTR,
1803 (unsigned long *)&vcpu->arch.regs_dirty))
1804 return;
1805
Sheng Yang14394422008-04-28 12:24:45 +08001806 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001807 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1808 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1809 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1810 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1811 }
1812}
1813
Avi Kivity8f5d5492009-05-31 18:41:29 +03001814static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1815{
1816 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1817 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1818 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1819 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1820 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1821 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001822
1823 __set_bit(VCPU_EXREG_PDPTR,
1824 (unsigned long *)&vcpu->arch.regs_avail);
1825 __set_bit(VCPU_EXREG_PDPTR,
1826 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001827}
1828
Sheng Yang14394422008-04-28 12:24:45 +08001829static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1830
1831static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1832 unsigned long cr0,
1833 struct kvm_vcpu *vcpu)
1834{
1835 if (!(cr0 & X86_CR0_PG)) {
1836 /* From paging/starting to nonpaging */
1837 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001838 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001839 (CPU_BASED_CR3_LOAD_EXITING |
1840 CPU_BASED_CR3_STORE_EXITING));
1841 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001842 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001843 } else if (!is_paging(vcpu)) {
1844 /* From nonpaging to paging */
1845 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001846 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001847 ~(CPU_BASED_CR3_LOAD_EXITING |
1848 CPU_BASED_CR3_STORE_EXITING));
1849 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001850 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001851 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001852
1853 if (!(cr0 & X86_CR0_WP))
1854 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001855}
1856
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1858{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001859 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001860 unsigned long hw_cr0;
1861
1862 if (enable_unrestricted_guest)
1863 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1864 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1865 else
1866 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001867
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001868 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001869 enter_pmode(vcpu);
1870
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001871 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001872 enter_rmode(vcpu);
1873
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001874#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001875 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001876 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001877 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001878 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001879 exit_lmode(vcpu);
1880 }
1881#endif
1882
Avi Kivity089d0342009-03-23 18:26:32 +02001883 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001884 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1885
Avi Kivity02daab22009-12-30 12:40:26 +02001886 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02001887 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02001888
Avi Kivity6aa8b732006-12-10 02:21:36 -08001889 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001890 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001891 vcpu->arch.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001892}
1893
Sheng Yang14394422008-04-28 12:24:45 +08001894static u64 construct_eptp(unsigned long root_hpa)
1895{
1896 u64 eptp;
1897
1898 /* TODO write the value reading from MSR */
1899 eptp = VMX_EPT_DEFAULT_MT |
1900 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1901 eptp |= (root_hpa & PAGE_MASK);
1902
1903 return eptp;
1904}
1905
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1907{
Sheng Yang14394422008-04-28 12:24:45 +08001908 unsigned long guest_cr3;
1909 u64 eptp;
1910
1911 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001912 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001913 eptp = construct_eptp(cr3);
1914 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001915 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001916 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02001917 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001918 }
1919
Sheng Yang2384d2b2008-01-17 15:14:33 +08001920 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001921 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001922}
1923
1924static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1925{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001926 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001927 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1928
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001929 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02001930 if (enable_ept) {
1931 if (!is_paging(vcpu)) {
1932 hw_cr4 &= ~X86_CR4_PAE;
1933 hw_cr4 |= X86_CR4_PSE;
1934 } else if (!(cr4 & X86_CR4_PAE)) {
1935 hw_cr4 &= ~X86_CR4_PAE;
1936 }
1937 }
Sheng Yang14394422008-04-28 12:24:45 +08001938
1939 vmcs_writel(CR4_READ_SHADOW, cr4);
1940 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001941}
1942
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1944{
1945 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1946
1947 return vmcs_readl(sf->base);
1948}
1949
1950static void vmx_get_segment(struct kvm_vcpu *vcpu,
1951 struct kvm_segment *var, int seg)
1952{
1953 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1954 u32 ar;
1955
1956 var->base = vmcs_readl(sf->base);
1957 var->limit = vmcs_read32(sf->limit);
1958 var->selector = vmcs_read16(sf->selector);
1959 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001960 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961 ar = 0;
1962 var->type = ar & 15;
1963 var->s = (ar >> 4) & 1;
1964 var->dpl = (ar >> 5) & 3;
1965 var->present = (ar >> 7) & 1;
1966 var->avl = (ar >> 12) & 1;
1967 var->l = (ar >> 13) & 1;
1968 var->db = (ar >> 14) & 1;
1969 var->g = (ar >> 15) & 1;
1970 var->unusable = (ar >> 16) & 1;
1971}
1972
Izik Eidus2e4d2652008-03-24 19:38:34 +02001973static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1974{
Avi Kivity3eeb3282010-01-21 15:31:48 +02001975 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02001976 return 0;
1977
1978 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1979 return 3;
1980
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001981 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001982}
1983
Avi Kivity653e3102007-05-07 10:55:37 +03001984static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001985{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001986 u32 ar;
1987
Avi Kivity653e3102007-05-07 10:55:37 +03001988 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001989 ar = 1 << 16;
1990 else {
1991 ar = var->type & 15;
1992 ar |= (var->s & 1) << 4;
1993 ar |= (var->dpl & 3) << 5;
1994 ar |= (var->present & 1) << 7;
1995 ar |= (var->avl & 1) << 12;
1996 ar |= (var->l & 1) << 13;
1997 ar |= (var->db & 1) << 14;
1998 ar |= (var->g & 1) << 15;
1999 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08002000 if (ar == 0) /* a 0 value means unusable */
2001 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03002002
2003 return ar;
2004}
2005
2006static void vmx_set_segment(struct kvm_vcpu *vcpu,
2007 struct kvm_segment *var, int seg)
2008{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002009 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03002010 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2011 u32 ar;
2012
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002013 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
2014 vmx->rmode.tr.selector = var->selector;
2015 vmx->rmode.tr.base = var->base;
2016 vmx->rmode.tr.limit = var->limit;
2017 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03002018 return;
2019 }
2020 vmcs_writel(sf->base, var->base);
2021 vmcs_write32(sf->limit, var->limit);
2022 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002023 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03002024 /*
2025 * Hack real-mode segments into vm86 compatibility.
2026 */
2027 if (var->base == 0xffff0000 && var->selector == 0xf000)
2028 vmcs_writel(sf->base, 0xf0000);
2029 ar = 0xf3;
2030 } else
2031 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002032
2033 /*
2034 * Fix the "Accessed" bit in AR field of segment registers for older
2035 * qemu binaries.
2036 * IA32 arch specifies that at the time of processor reset the
2037 * "Accessed" bit in the AR field of segment registers is 1. And qemu
2038 * is setting it to 0 in the usedland code. This causes invalid guest
2039 * state vmexit when "unrestricted guest" mode is turned on.
2040 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2041 * tree. Newer qemu binaries with that qemu fix would not need this
2042 * kvm hack.
2043 */
2044 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
2045 ar |= 0x1; /* Accessed */
2046
Avi Kivity6aa8b732006-12-10 02:21:36 -08002047 vmcs_write32(sf->ar_bytes, ar);
2048}
2049
Avi Kivity6aa8b732006-12-10 02:21:36 -08002050static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2051{
2052 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
2053
2054 *db = (ar >> 14) & 1;
2055 *l = (ar >> 13) & 1;
2056}
2057
Gleb Natapov89a27f42010-02-16 10:51:48 +02002058static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002059{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002060 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2061 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002062}
2063
Gleb Natapov89a27f42010-02-16 10:51:48 +02002064static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002065{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002066 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2067 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002068}
2069
Gleb Natapov89a27f42010-02-16 10:51:48 +02002070static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002071{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002072 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2073 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002074}
2075
Gleb Natapov89a27f42010-02-16 10:51:48 +02002076static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002078 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2079 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002080}
2081
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002082static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2083{
2084 struct kvm_segment var;
2085 u32 ar;
2086
2087 vmx_get_segment(vcpu, &var, seg);
2088 ar = vmx_segment_access_rights(&var);
2089
2090 if (var.base != (var.selector << 4))
2091 return false;
2092 if (var.limit != 0xffff)
2093 return false;
2094 if (ar != 0xf3)
2095 return false;
2096
2097 return true;
2098}
2099
2100static bool code_segment_valid(struct kvm_vcpu *vcpu)
2101{
2102 struct kvm_segment cs;
2103 unsigned int cs_rpl;
2104
2105 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2106 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2107
Avi Kivity1872a3f2009-01-04 23:26:52 +02002108 if (cs.unusable)
2109 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002110 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2111 return false;
2112 if (!cs.s)
2113 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002114 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002115 if (cs.dpl > cs_rpl)
2116 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002117 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002118 if (cs.dpl != cs_rpl)
2119 return false;
2120 }
2121 if (!cs.present)
2122 return false;
2123
2124 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2125 return true;
2126}
2127
2128static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2129{
2130 struct kvm_segment ss;
2131 unsigned int ss_rpl;
2132
2133 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2134 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2135
Avi Kivity1872a3f2009-01-04 23:26:52 +02002136 if (ss.unusable)
2137 return true;
2138 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002139 return false;
2140 if (!ss.s)
2141 return false;
2142 if (ss.dpl != ss_rpl) /* DPL != RPL */
2143 return false;
2144 if (!ss.present)
2145 return false;
2146
2147 return true;
2148}
2149
2150static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2151{
2152 struct kvm_segment var;
2153 unsigned int rpl;
2154
2155 vmx_get_segment(vcpu, &var, seg);
2156 rpl = var.selector & SELECTOR_RPL_MASK;
2157
Avi Kivity1872a3f2009-01-04 23:26:52 +02002158 if (var.unusable)
2159 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002160 if (!var.s)
2161 return false;
2162 if (!var.present)
2163 return false;
2164 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2165 if (var.dpl < rpl) /* DPL < RPL */
2166 return false;
2167 }
2168
2169 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2170 * rights flags
2171 */
2172 return true;
2173}
2174
2175static bool tr_valid(struct kvm_vcpu *vcpu)
2176{
2177 struct kvm_segment tr;
2178
2179 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2180
Avi Kivity1872a3f2009-01-04 23:26:52 +02002181 if (tr.unusable)
2182 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002183 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2184 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002185 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002186 return false;
2187 if (!tr.present)
2188 return false;
2189
2190 return true;
2191}
2192
2193static bool ldtr_valid(struct kvm_vcpu *vcpu)
2194{
2195 struct kvm_segment ldtr;
2196
2197 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2198
Avi Kivity1872a3f2009-01-04 23:26:52 +02002199 if (ldtr.unusable)
2200 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002201 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2202 return false;
2203 if (ldtr.type != 2)
2204 return false;
2205 if (!ldtr.present)
2206 return false;
2207
2208 return true;
2209}
2210
2211static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2212{
2213 struct kvm_segment cs, ss;
2214
2215 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2216 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2217
2218 return ((cs.selector & SELECTOR_RPL_MASK) ==
2219 (ss.selector & SELECTOR_RPL_MASK));
2220}
2221
2222/*
2223 * Check if guest state is valid. Returns true if valid, false if
2224 * not.
2225 * We assume that registers are always usable
2226 */
2227static bool guest_state_valid(struct kvm_vcpu *vcpu)
2228{
2229 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02002230 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002231 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2232 return false;
2233 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2234 return false;
2235 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2236 return false;
2237 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2238 return false;
2239 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2240 return false;
2241 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2242 return false;
2243 } else {
2244 /* protected mode guest state checks */
2245 if (!cs_ss_rpl_check(vcpu))
2246 return false;
2247 if (!code_segment_valid(vcpu))
2248 return false;
2249 if (!stack_segment_valid(vcpu))
2250 return false;
2251 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2252 return false;
2253 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2254 return false;
2255 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2256 return false;
2257 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2258 return false;
2259 if (!tr_valid(vcpu))
2260 return false;
2261 if (!ldtr_valid(vcpu))
2262 return false;
2263 }
2264 /* TODO:
2265 * - Add checks on RIP
2266 * - Add checks on RFLAGS
2267 */
2268
2269 return true;
2270}
2271
Mike Dayd77c26f2007-10-08 09:02:08 -04002272static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002275 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002276 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002277 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278
Izik Eidus195aefd2007-10-01 22:14:18 +02002279 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2280 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002281 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002282 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002283 r = kvm_write_guest_page(kvm, fn++, &data,
2284 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002285 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002286 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002287 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2288 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002289 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002290 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2291 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002292 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002293 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002294 r = kvm_write_guest_page(kvm, fn, &data,
2295 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2296 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002297 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002298 goto out;
2299
2300 ret = 1;
2301out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002302 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002303}
2304
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002305static int init_rmode_identity_map(struct kvm *kvm)
2306{
2307 int i, r, ret;
2308 pfn_t identity_map_pfn;
2309 u32 tmp;
2310
Avi Kivity089d0342009-03-23 18:26:32 +02002311 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002312 return 1;
2313 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2314 printk(KERN_ERR "EPT: identity-mapping pagetable "
2315 "haven't been allocated!\n");
2316 return 0;
2317 }
2318 if (likely(kvm->arch.ept_identity_pagetable_done))
2319 return 1;
2320 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002321 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002322 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2323 if (r < 0)
2324 goto out;
2325 /* Set up identity-mapping pagetable for EPT in real mode */
2326 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2327 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2328 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2329 r = kvm_write_guest_page(kvm, identity_map_pfn,
2330 &tmp, i * sizeof(tmp), sizeof(tmp));
2331 if (r < 0)
2332 goto out;
2333 }
2334 kvm->arch.ept_identity_pagetable_done = true;
2335 ret = 1;
2336out:
2337 return ret;
2338}
2339
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340static void seg_setup(int seg)
2341{
2342 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002343 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002344
2345 vmcs_write16(sf->selector, 0);
2346 vmcs_writel(sf->base, 0);
2347 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002348 if (enable_unrestricted_guest) {
2349 ar = 0x93;
2350 if (seg == VCPU_SREG_CS)
2351 ar |= 0x08; /* code segment */
2352 } else
2353 ar = 0xf3;
2354
2355 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356}
2357
Sheng Yangf78e0e22007-10-29 09:40:42 +08002358static int alloc_apic_access_page(struct kvm *kvm)
2359{
2360 struct kvm_userspace_memory_region kvm_userspace_mem;
2361 int r = 0;
2362
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002363 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002364 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002365 goto out;
2366 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2367 kvm_userspace_mem.flags = 0;
2368 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2369 kvm_userspace_mem.memory_size = PAGE_SIZE;
2370 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2371 if (r)
2372 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002373
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002374 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002375out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002376 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002377 return r;
2378}
2379
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002380static int alloc_identity_pagetable(struct kvm *kvm)
2381{
2382 struct kvm_userspace_memory_region kvm_userspace_mem;
2383 int r = 0;
2384
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002385 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002386 if (kvm->arch.ept_identity_pagetable)
2387 goto out;
2388 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2389 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002390 kvm_userspace_mem.guest_phys_addr =
2391 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002392 kvm_userspace_mem.memory_size = PAGE_SIZE;
2393 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2394 if (r)
2395 goto out;
2396
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002397 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002398 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002399out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002400 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002401 return r;
2402}
2403
Sheng Yang2384d2b2008-01-17 15:14:33 +08002404static void allocate_vpid(struct vcpu_vmx *vmx)
2405{
2406 int vpid;
2407
2408 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002409 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002410 return;
2411 spin_lock(&vmx_vpid_lock);
2412 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2413 if (vpid < VMX_NR_VPIDS) {
2414 vmx->vpid = vpid;
2415 __set_bit(vpid, vmx_vpid_bitmap);
2416 }
2417 spin_unlock(&vmx_vpid_lock);
2418}
2419
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08002420static void free_vpid(struct vcpu_vmx *vmx)
2421{
2422 if (!enable_vpid)
2423 return;
2424 spin_lock(&vmx_vpid_lock);
2425 if (vmx->vpid != 0)
2426 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2427 spin_unlock(&vmx_vpid_lock);
2428}
2429
Avi Kivity58972972009-02-24 22:26:47 +02002430static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002431{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002432 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002433
2434 if (!cpu_has_vmx_msr_bitmap())
2435 return;
2436
2437 /*
2438 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2439 * have the write-low and read-high bitmap offsets the wrong way round.
2440 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2441 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002442 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002443 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2444 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002445 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2446 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002447 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2448 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002449 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002450}
2451
Avi Kivity58972972009-02-24 22:26:47 +02002452static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2453{
2454 if (!longmode_only)
2455 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2456 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2457}
2458
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459/*
2460 * Sets up the vmcs for emulated real mode.
2461 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002462static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002463{
Sheng Yang468d4722008-10-09 16:01:55 +08002464 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002466 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467 unsigned long a;
Gleb Natapov89a27f42010-02-16 10:51:48 +02002468 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002470 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002471 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002472
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002474 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2475 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476
Sheng Yang25c5f222008-03-28 13:18:56 +08002477 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002478 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002479
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2481
Avi Kivity6aa8b732006-12-10 02:21:36 -08002482 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002483 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2484 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002485
2486 exec_control = vmcs_config.cpu_based_exec_ctrl;
2487 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2488 exec_control &= ~CPU_BASED_TPR_SHADOW;
2489#ifdef CONFIG_X86_64
2490 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2491 CPU_BASED_CR8_LOAD_EXITING;
2492#endif
2493 }
Avi Kivity089d0342009-03-23 18:26:32 +02002494 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002495 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002496 CPU_BASED_CR3_LOAD_EXITING |
2497 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002498 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002499
Sheng Yang83ff3b92007-11-21 14:33:25 +08002500 if (cpu_has_secondary_exec_ctrls()) {
2501 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2502 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2503 exec_control &=
2504 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002505 if (vmx->vpid == 0)
2506 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002507 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002508 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002509 enable_unrestricted_guest = 0;
2510 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002511 if (!enable_unrestricted_guest)
2512 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002513 if (!ple_gap)
2514 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002515 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2516 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002517
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002518 if (ple_gap) {
2519 vmcs_write32(PLE_GAP, ple_gap);
2520 vmcs_write32(PLE_WINDOW, ple_window);
2521 }
2522
Avi Kivityc7addb92007-09-16 18:58:32 +02002523 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2524 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002525 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2526
Avi Kivity1c11e712010-05-03 16:05:44 +03002527 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2529 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2530
2531 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2532 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2533 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002534 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2535 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002537#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 rdmsrl(MSR_FS_BASE, a);
2539 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2540 rdmsrl(MSR_GS_BASE, a);
2541 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2542#else
2543 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2544 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2545#endif
2546
2547 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2548
Wei Yongjunec687982010-03-05 12:11:48 +08002549 native_store_idt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02002550 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551
Mike Dayd77c26f2007-10-08 09:02:08 -04002552 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002553 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002554 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2555 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002556 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03002557 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002558 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002559
2560 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2561 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2562 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2563 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2564 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2565 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2566
Sheng Yang468d4722008-10-09 16:01:55 +08002567 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2568 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2569 host_pat = msr_low | ((u64) msr_high << 32);
2570 vmcs_write64(HOST_IA32_PAT, host_pat);
2571 }
2572 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2573 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2574 host_pat = msr_low | ((u64) msr_high << 32);
2575 /* Write the default value follow host pat */
2576 vmcs_write64(GUEST_IA32_PAT, host_pat);
2577 /* Keep arch.pat sync with GUEST_IA32_PAT */
2578 vmx->vcpu.arch.pat = host_pat;
2579 }
2580
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 for (i = 0; i < NR_VMX_MSR; ++i) {
2582 u32 index = vmx_msr_index[i];
2583 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002584 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002585
2586 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2587 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002588 if (wrmsr_safe(index, data_low, data_high) < 0)
2589 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03002590 vmx->guest_msrs[j].index = i;
2591 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002592 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002593 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002595
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002596 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002597
2598 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002599 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2600
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002601 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02002602 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02002603 if (enable_ept)
2604 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02002605 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002606
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002607 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2608 rdtscll(tsc_this);
2609 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2610 tsc_base = tsc_this;
2611
2612 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002613
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002614 return 0;
2615}
2616
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002617static int init_rmode(struct kvm *kvm)
2618{
2619 if (!init_rmode_tss(kvm))
2620 return 0;
2621 if (!init_rmode_identity_map(kvm))
2622 return 0;
2623 return 1;
2624}
2625
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002626static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2627{
2628 struct vcpu_vmx *vmx = to_vmx(vcpu);
2629 u64 msr;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002630 int ret, idx;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002631
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002632 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002633 idx = srcu_read_lock(&vcpu->kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002634 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002635 ret = -ENOMEM;
2636 goto out;
2637 }
2638
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002639 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002640
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002641 vmx->soft_vnmi_blocked = 0;
2642
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002643 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002644 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002645 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002646 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002647 msr |= MSR_IA32_APICBASE_BSP;
2648 kvm_set_apic_base(&vmx->vcpu, msr);
2649
2650 fx_init(&vmx->vcpu);
2651
Avi Kivity5706be02008-08-20 15:07:31 +03002652 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002653 /*
2654 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2655 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2656 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002657 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002658 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2659 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2660 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002661 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2662 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002663 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002664
2665 seg_setup(VCPU_SREG_DS);
2666 seg_setup(VCPU_SREG_ES);
2667 seg_setup(VCPU_SREG_FS);
2668 seg_setup(VCPU_SREG_GS);
2669 seg_setup(VCPU_SREG_SS);
2670
2671 vmcs_write16(GUEST_TR_SELECTOR, 0);
2672 vmcs_writel(GUEST_TR_BASE, 0);
2673 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2674 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2675
2676 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2677 vmcs_writel(GUEST_LDTR_BASE, 0);
2678 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2679 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2680
2681 vmcs_write32(GUEST_SYSENTER_CS, 0);
2682 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2683 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2684
2685 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002686 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002687 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002688 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002689 kvm_rip_write(vcpu, 0);
2690 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002691
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002692 vmcs_writel(GUEST_DR7, 0x400);
2693
2694 vmcs_writel(GUEST_GDTR_BASE, 0);
2695 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2696
2697 vmcs_writel(GUEST_IDTR_BASE, 0);
2698 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2699
2700 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2701 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2702 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2703
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002704 /* Special registers */
2705 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2706
2707 setup_msrs(vmx);
2708
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2710
Sheng Yangf78e0e22007-10-29 09:40:42 +08002711 if (cpu_has_vmx_tpr_shadow()) {
2712 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2713 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2714 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002715 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002716 vmcs_write32(TPR_THRESHOLD, 0);
2717 }
2718
2719 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2720 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002721 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722
Sheng Yang2384d2b2008-01-17 15:14:33 +08002723 if (vmx->vpid != 0)
2724 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2725
Eduardo Habkostfa400522009-10-24 02:49:58 -02002726 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002727 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002728 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002729 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002730 vmx_fpu_activate(&vmx->vcpu);
2731 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002732
Sheng Yang2384d2b2008-01-17 15:14:33 +08002733 vpid_sync_vcpu_all(vmx);
2734
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002735 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002737 /* HACK: Don't enable emulation on guest boot/reset */
2738 vmx->emulation_required = 0;
2739
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740out:
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002741 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 return ret;
2743}
2744
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002745static void enable_irq_window(struct kvm_vcpu *vcpu)
2746{
2747 u32 cpu_based_vm_exec_control;
2748
2749 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2750 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2751 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2752}
2753
2754static void enable_nmi_window(struct kvm_vcpu *vcpu)
2755{
2756 u32 cpu_based_vm_exec_control;
2757
2758 if (!cpu_has_virtual_nmis()) {
2759 enable_irq_window(vcpu);
2760 return;
2761 }
2762
2763 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2764 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2765 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2766}
2767
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002768static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002769{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002770 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002771 uint32_t intr;
2772 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002773
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002774 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002775
Avi Kivityfa89a812008-09-01 15:57:51 +03002776 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002777 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002778 vmx->rmode.irq.pending = true;
2779 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002780 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002781 if (vcpu->arch.interrupt.soft)
2782 vmx->rmode.irq.rip +=
2783 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002784 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2785 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2786 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002787 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002788 return;
2789 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002790 intr = irq | INTR_INFO_VALID_MASK;
2791 if (vcpu->arch.interrupt.soft) {
2792 intr |= INTR_TYPE_SOFT_INTR;
2793 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2794 vmx->vcpu.arch.event_exit_inst_len);
2795 } else
2796 intr |= INTR_TYPE_EXT_INTR;
2797 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002798}
2799
Sheng Yangf08864b2008-05-15 18:23:25 +08002800static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2801{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002802 struct vcpu_vmx *vmx = to_vmx(vcpu);
2803
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002804 if (!cpu_has_virtual_nmis()) {
2805 /*
2806 * Tracking the NMI-blocked state in software is built upon
2807 * finding the next open IRQ window. This, in turn, depends on
2808 * well-behaving guests: They have to keep IRQs disabled at
2809 * least as long as the NMI handler runs. Otherwise we may
2810 * cause NMI nesting, maybe breaking the guest. But as this is
2811 * highly unlikely, we can live with the residual risk.
2812 */
2813 vmx->soft_vnmi_blocked = 1;
2814 vmx->vnmi_blocked_time = 0;
2815 }
2816
Jan Kiszka487b3912008-09-26 09:30:56 +02002817 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002818 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002819 vmx->rmode.irq.pending = true;
2820 vmx->rmode.irq.vector = NMI_VECTOR;
2821 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2822 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2823 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2824 INTR_INFO_VALID_MASK);
2825 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2826 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2827 return;
2828 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002829 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2830 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002831}
2832
Gleb Natapovc4282df2009-04-21 17:45:07 +03002833static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002834{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002835 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002836 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002837
Gleb Natapovc4282df2009-04-21 17:45:07 +03002838 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Jan Kiszkaf8c5fae2010-05-11 15:16:46 +02002839 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002840}
2841
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002842static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
2843{
2844 if (!cpu_has_virtual_nmis())
2845 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivityc332c832010-05-04 12:24:12 +03002846 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002847}
2848
2849static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2850{
2851 struct vcpu_vmx *vmx = to_vmx(vcpu);
2852
2853 if (!cpu_has_virtual_nmis()) {
2854 if (vmx->soft_vnmi_blocked != masked) {
2855 vmx->soft_vnmi_blocked = masked;
2856 vmx->vnmi_blocked_time = 0;
2857 }
2858 } else {
2859 if (masked)
2860 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2861 GUEST_INTR_STATE_NMI);
2862 else
2863 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2864 GUEST_INTR_STATE_NMI);
2865 }
2866}
2867
Gleb Natapov78646122009-03-23 12:12:11 +02002868static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2869{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002870 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2871 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2872 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002873}
2874
Izik Eiduscbc94022007-10-25 00:29:55 +02002875static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2876{
2877 int ret;
2878 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002879 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002880 .guest_phys_addr = addr,
2881 .memory_size = PAGE_SIZE * 3,
2882 .flags = 0,
2883 };
2884
2885 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2886 if (ret)
2887 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002888 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002889 return 0;
2890}
2891
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2893 int vec, u32 err_code)
2894{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002895 /*
2896 * Instruction with address size override prefix opcode 0x67
2897 * Cause the #SS fault with 0 error code in VM86 mode.
2898 */
2899 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002900 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002902 /*
2903 * Forward all other exceptions that are valid in real mode.
2904 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2905 * the required debugging infrastructure rework.
2906 */
2907 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002908 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002909 if (vcpu->guest_debug &
2910 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2911 return 0;
2912 kvm_queue_exception(vcpu, vec);
2913 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002914 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01002915 /*
2916 * Update instruction length as we may reinject the exception
2917 * from user space while in guest debugging mode.
2918 */
2919 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
2920 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002921 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2922 return 0;
2923 /* fall through */
2924 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002925 case OF_VECTOR:
2926 case BR_VECTOR:
2927 case UD_VECTOR:
2928 case DF_VECTOR:
2929 case SS_VECTOR:
2930 case GP_VECTOR:
2931 case MF_VECTOR:
2932 kvm_queue_exception(vcpu, vec);
2933 return 1;
2934 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002935 return 0;
2936}
2937
Andi Kleena0861c02009-06-08 17:37:09 +08002938/*
2939 * Trigger machine check on the host. We assume all the MSRs are already set up
2940 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2941 * We pass a fake environment to the machine check handler because we want
2942 * the guest to be always treated like user space, no matter what context
2943 * it used internally.
2944 */
2945static void kvm_machine_check(void)
2946{
2947#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2948 struct pt_regs regs = {
2949 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2950 .flags = X86_EFLAGS_IF,
2951 };
2952
2953 do_machine_check(&regs, 0);
2954#endif
2955}
2956
Avi Kivity851ba692009-08-24 11:10:17 +03002957static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002958{
2959 /* already handled by vcpu_run */
2960 return 1;
2961}
2962
Avi Kivity851ba692009-08-24 11:10:17 +03002963static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964{
Avi Kivity1155f762007-11-22 11:30:47 +02002965 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002966 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002967 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002968 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 u32 vect_info;
2970 enum emulation_result er;
2971
Avi Kivity1155f762007-11-22 11:30:47 +02002972 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2974
Andi Kleena0861c02009-06-08 17:37:09 +08002975 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002976 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002977
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02002979 !is_page_fault(intr_info)) {
2980 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2981 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
2982 vcpu->run->internal.ndata = 2;
2983 vcpu->run->internal.data[0] = vect_info;
2984 vcpu->run->internal.data[1] = intr_info;
2985 return 0;
2986 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987
Jan Kiszkae4a41882008-09-26 09:30:46 +02002988 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002989 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002990
2991 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002992 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002993 return 1;
2994 }
2995
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002996 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002997 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002998 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002999 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003000 return 1;
3001 }
3002
Avi Kivity6aa8b732006-12-10 02:21:36 -08003003 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003004 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06003005 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003006 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
3007 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08003008 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02003009 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003010 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003012 trace_kvm_page_fault(cr2, error_code);
3013
Gleb Natapov3298b752009-05-11 13:35:46 +03003014 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03003015 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02003016 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 }
3018
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003019 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08003020 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003021 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003022 if (vcpu->arch.halt_request) {
3023 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003024 return kvm_emulate_halt(vcpu);
3025 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003027 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003029 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003030 switch (ex_no) {
3031 case DB_VECTOR:
3032 dr6 = vmcs_readl(EXIT_QUALIFICATION);
3033 if (!(vcpu->guest_debug &
3034 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
3035 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
3036 kvm_queue_exception(vcpu, DB_VECTOR);
3037 return 1;
3038 }
3039 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
3040 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
3041 /* fall through */
3042 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003043 /*
3044 * Update instruction length as we may reinject #BP from
3045 * user space while in guest debugging mode. Reading it for
3046 * #DB as well causes no harm, it is not used in that case.
3047 */
3048 vmx->vcpu.arch.event_exit_inst_len =
3049 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003051 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
3052 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003053 break;
3054 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003055 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
3056 kvm_run->ex.exception = ex_no;
3057 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003058 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 return 0;
3061}
3062
Avi Kivity851ba692009-08-24 11:10:17 +03003063static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064{
Avi Kivity1165f5f2007-04-19 17:27:43 +03003065 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 return 1;
3067}
3068
Avi Kivity851ba692009-08-24 11:10:17 +03003069static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08003070{
Avi Kivity851ba692009-08-24 11:10:17 +03003071 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08003072 return 0;
3073}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074
Avi Kivity851ba692009-08-24 11:10:17 +03003075static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076{
He, Qingbfdaab02007-09-12 14:18:28 +08003077 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01003078 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02003079 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080
He, Qingbfdaab02007-09-12 14:18:28 +08003081 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02003082 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003083 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003084
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003085 ++vcpu->stat.io_exits;
3086
3087 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003088 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003089
3090 port = exit_qualification >> 16;
3091 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003092 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003093
3094 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095}
3096
Ingo Molnar102d8322007-02-19 14:37:47 +02003097static void
3098vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3099{
3100 /*
3101 * Patch in the VMCALL instruction:
3102 */
3103 hypercall[0] = 0x0f;
3104 hypercall[1] = 0x01;
3105 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02003106}
3107
Avi Kivity851ba692009-08-24 11:10:17 +03003108static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003109{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003110 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003111 int cr;
3112 int reg;
3113
He, Qingbfdaab02007-09-12 14:18:28 +08003114 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115 cr = exit_qualification & 15;
3116 reg = (exit_qualification >> 8) & 15;
3117 switch ((exit_qualification >> 4) & 3) {
3118 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003119 val = kvm_register_read(vcpu, reg);
3120 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003121 switch (cr) {
3122 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003123 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 skip_emulated_instruction(vcpu);
3125 return 1;
3126 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003127 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003128 skip_emulated_instruction(vcpu);
3129 return 1;
3130 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003131 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 skip_emulated_instruction(vcpu);
3133 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003134 case 8: {
3135 u8 cr8_prev = kvm_get_cr8(vcpu);
3136 u8 cr8 = kvm_register_read(vcpu, reg);
3137 kvm_set_cr8(vcpu, cr8);
3138 skip_emulated_instruction(vcpu);
3139 if (irqchip_in_kernel(vcpu->kvm))
3140 return 1;
3141 if (cr8_prev <= cr8)
3142 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03003143 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003144 return 0;
3145 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146 };
3147 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03003148 case 2: /* clts */
Avi Kivityedcafe32009-12-30 18:07:40 +02003149 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02003150 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003151 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02003152 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03003153 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 case 1: /*mov from cr*/
3155 switch (cr) {
3156 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003157 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003158 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159 skip_emulated_instruction(vcpu);
3160 return 1;
3161 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003162 val = kvm_get_cr8(vcpu);
3163 kvm_register_write(vcpu, reg, val);
3164 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003165 skip_emulated_instruction(vcpu);
3166 return 1;
3167 }
3168 break;
3169 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003170 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003171 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003172 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173
3174 skip_emulated_instruction(vcpu);
3175 return 1;
3176 default:
3177 break;
3178 }
Avi Kivity851ba692009-08-24 11:10:17 +03003179 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003180 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003181 (int)(exit_qualification >> 4) & 3, cr);
3182 return 0;
3183}
3184
Avi Kivity851ba692009-08-24 11:10:17 +03003185static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186{
He, Qingbfdaab02007-09-12 14:18:28 +08003187 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188 int dr, reg;
3189
Jan Kiszkaf2483412010-01-20 18:20:20 +01003190 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03003191 if (!kvm_require_cpl(vcpu, 0))
3192 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003193 dr = vmcs_readl(GUEST_DR7);
3194 if (dr & DR7_GD) {
3195 /*
3196 * As the vm-exit takes precedence over the debug trap, we
3197 * need to emulate the latter, either for the host or the
3198 * guest debugging itself.
3199 */
3200 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003201 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3202 vcpu->run->debug.arch.dr7 = dr;
3203 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003204 vmcs_readl(GUEST_CS_BASE) +
3205 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003206 vcpu->run->debug.arch.exception = DB_VECTOR;
3207 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003208 return 0;
3209 } else {
3210 vcpu->arch.dr7 &= ~DR7_GD;
3211 vcpu->arch.dr6 |= DR6_BD;
3212 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3213 kvm_queue_exception(vcpu, DB_VECTOR);
3214 return 1;
3215 }
3216 }
3217
He, Qingbfdaab02007-09-12 14:18:28 +08003218 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003219 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3220 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3221 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03003222 unsigned long val;
3223 if (!kvm_get_dr(vcpu, dr, &val))
3224 kvm_register_write(vcpu, reg, val);
3225 } else
3226 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227 skip_emulated_instruction(vcpu);
3228 return 1;
3229}
3230
Gleb Natapov020df072010-04-13 10:05:23 +03003231static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3232{
3233 vmcs_writel(GUEST_DR7, val);
3234}
3235
Avi Kivity851ba692009-08-24 11:10:17 +03003236static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237{
Avi Kivity06465c52007-02-28 20:46:53 +02003238 kvm_emulate_cpuid(vcpu);
3239 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240}
3241
Avi Kivity851ba692009-08-24 11:10:17 +03003242static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003244 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003245 u64 data;
3246
3247 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02003248 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003249 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 return 1;
3251 }
3252
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003253 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003254
Avi Kivity6aa8b732006-12-10 02:21:36 -08003255 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003256 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3257 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 skip_emulated_instruction(vcpu);
3259 return 1;
3260}
3261
Avi Kivity851ba692009-08-24 11:10:17 +03003262static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003264 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3265 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3266 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003267
3268 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02003269 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003270 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003271 return 1;
3272 }
3273
Avi Kivity59200272010-01-25 19:47:02 +02003274 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003275 skip_emulated_instruction(vcpu);
3276 return 1;
3277}
3278
Avi Kivity851ba692009-08-24 11:10:17 +03003279static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003280{
3281 return 1;
3282}
3283
Avi Kivity851ba692009-08-24 11:10:17 +03003284static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285{
Eddie Dong85f455f2007-07-06 12:20:49 +03003286 u32 cpu_based_vm_exec_control;
3287
3288 /* clear pending irq */
3289 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3290 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3291 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003292
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003293 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003294
Dor Laorc1150d82007-01-05 16:36:24 -08003295 /*
3296 * If the user space waits to inject interrupts, exit as soon as
3297 * possible
3298 */
Gleb Natapov80618232009-04-21 17:44:56 +03003299 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003300 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003301 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003302 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003303 return 0;
3304 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003305 return 1;
3306}
3307
Avi Kivity851ba692009-08-24 11:10:17 +03003308static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003309{
3310 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003311 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312}
3313
Avi Kivity851ba692009-08-24 11:10:17 +03003314static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003315{
Dor Laor510043d2007-02-19 18:25:43 +02003316 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003317 kvm_emulate_hypercall(vcpu);
3318 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003319}
3320
Avi Kivity851ba692009-08-24 11:10:17 +03003321static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003322{
3323 kvm_queue_exception(vcpu, UD_VECTOR);
3324 return 1;
3325}
3326
Avi Kivity851ba692009-08-24 11:10:17 +03003327static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003328{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003329 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003330
3331 kvm_mmu_invlpg(vcpu, exit_qualification);
3332 skip_emulated_instruction(vcpu);
3333 return 1;
3334}
3335
Avi Kivity851ba692009-08-24 11:10:17 +03003336static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003337{
3338 skip_emulated_instruction(vcpu);
3339 /* TODO: Add support for VT-d/pass-through device */
3340 return 1;
3341}
3342
Avi Kivity851ba692009-08-24 11:10:17 +03003343static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003344{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003345 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003346}
3347
Avi Kivity851ba692009-08-24 11:10:17 +03003348static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003349{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003350 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003351 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003352 bool has_error_code = false;
3353 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003354 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003355 int reason, type, idt_v;
3356
3357 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3358 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003359
3360 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3361
3362 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003363 if (reason == TASK_SWITCH_GATE && idt_v) {
3364 switch (type) {
3365 case INTR_TYPE_NMI_INTR:
3366 vcpu->arch.nmi_injected = false;
3367 if (cpu_has_virtual_nmis())
3368 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3369 GUEST_INTR_STATE_NMI);
3370 break;
3371 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003372 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003373 kvm_clear_interrupt_queue(vcpu);
3374 break;
3375 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003376 if (vmx->idt_vectoring_info &
3377 VECTORING_INFO_DELIVER_CODE_MASK) {
3378 has_error_code = true;
3379 error_code =
3380 vmcs_read32(IDT_VECTORING_ERROR_CODE);
3381 }
3382 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003383 case INTR_TYPE_SOFT_EXCEPTION:
3384 kvm_clear_exception_queue(vcpu);
3385 break;
3386 default:
3387 break;
3388 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003389 }
Izik Eidus37817f22008-03-24 23:14:53 +02003390 tss_selector = exit_qualification;
3391
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003392 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3393 type != INTR_TYPE_EXT_INTR &&
3394 type != INTR_TYPE_NMI_INTR))
3395 skip_emulated_instruction(vcpu);
3396
Gleb Natapovacb54512010-04-15 21:03:50 +03003397 if (kvm_task_switch(vcpu, tss_selector, reason,
3398 has_error_code, error_code) == EMULATE_FAIL) {
3399 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3400 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3401 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003402 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03003403 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003404
3405 /* clear all local breakpoint enable flags */
3406 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3407
3408 /*
3409 * TODO: What about debug traps on tss switch?
3410 * Are we supposed to inject them and update dr6?
3411 */
3412
3413 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003414}
3415
Avi Kivity851ba692009-08-24 11:10:17 +03003416static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003417{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003418 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003419 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003420 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003421
Sheng Yangf9c617f2009-03-25 10:08:52 +08003422 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003423
3424 if (exit_qualification & (1 << 6)) {
3425 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003426 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003427 }
3428
3429 gla_validity = (exit_qualification >> 7) & 0x3;
3430 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3431 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3432 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3433 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003434 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003435 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3436 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003437 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3438 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003439 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003440 }
3441
3442 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003443 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003444 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003445}
3446
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003447static u64 ept_rsvd_mask(u64 spte, int level)
3448{
3449 int i;
3450 u64 mask = 0;
3451
3452 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3453 mask |= (1ULL << i);
3454
3455 if (level > 2)
3456 /* bits 7:3 reserved */
3457 mask |= 0xf8;
3458 else if (level == 2) {
3459 if (spte & (1ULL << 7))
3460 /* 2MB ref, bits 20:12 reserved */
3461 mask |= 0x1ff000;
3462 else
3463 /* bits 6:3 reserved */
3464 mask |= 0x78;
3465 }
3466
3467 return mask;
3468}
3469
3470static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3471 int level)
3472{
3473 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3474
3475 /* 010b (write-only) */
3476 WARN_ON((spte & 0x7) == 0x2);
3477
3478 /* 110b (write/execute) */
3479 WARN_ON((spte & 0x7) == 0x6);
3480
3481 /* 100b (execute-only) and value not supported by logical processor */
3482 if (!cpu_has_vmx_ept_execute_only())
3483 WARN_ON((spte & 0x7) == 0x4);
3484
3485 /* not 000b */
3486 if ((spte & 0x7)) {
3487 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3488
3489 if (rsvd_bits != 0) {
3490 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3491 __func__, rsvd_bits);
3492 WARN_ON(1);
3493 }
3494
3495 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3496 u64 ept_mem_type = (spte & 0x38) >> 3;
3497
3498 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3499 ept_mem_type == 7) {
3500 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3501 __func__, ept_mem_type);
3502 WARN_ON(1);
3503 }
3504 }
3505 }
3506}
3507
Avi Kivity851ba692009-08-24 11:10:17 +03003508static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003509{
3510 u64 sptes[4];
3511 int nr_sptes, i;
3512 gpa_t gpa;
3513
3514 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3515
3516 printk(KERN_ERR "EPT: Misconfiguration.\n");
3517 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3518
3519 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3520
3521 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3522 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3523
Avi Kivity851ba692009-08-24 11:10:17 +03003524 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3525 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003526
3527 return 0;
3528}
3529
Avi Kivity851ba692009-08-24 11:10:17 +03003530static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003531{
3532 u32 cpu_based_vm_exec_control;
3533
3534 /* clear pending NMI */
3535 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3536 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3537 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3538 ++vcpu->stat.nmi_window_exits;
3539
3540 return 1;
3541}
3542
Mohammed Gamal80ced182009-09-01 12:48:18 +02003543static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003544{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003545 struct vcpu_vmx *vmx = to_vmx(vcpu);
3546 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003547 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003548
3549 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003550 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003551
Mohammed Gamal80ced182009-09-01 12:48:18 +02003552 if (err == EMULATE_DO_MMIO) {
3553 ret = 0;
3554 goto out;
3555 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003556
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003557 if (err != EMULATE_DONE)
3558 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003559
3560 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003561 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003562 if (need_resched())
3563 schedule();
3564 }
3565
Mohammed Gamal80ced182009-09-01 12:48:18 +02003566 vmx->emulation_required = 0;
3567out:
3568 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003569}
3570
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003572 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3573 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3574 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003575static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003576{
3577 skip_emulated_instruction(vcpu);
3578 kvm_vcpu_on_spin(vcpu);
3579
3580 return 1;
3581}
3582
Sheng Yang59708672009-12-15 13:29:54 +08003583static int handle_invalid_op(struct kvm_vcpu *vcpu)
3584{
3585 kvm_queue_exception(vcpu, UD_VECTOR);
3586 return 1;
3587}
3588
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003589/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003590 * The exit handlers return 1 if the exit was handled fully and guest execution
3591 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3592 * to be done to userspace and return 0.
3593 */
Avi Kivity851ba692009-08-24 11:10:17 +03003594static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003595 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3596 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003597 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003598 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003599 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003600 [EXIT_REASON_CR_ACCESS] = handle_cr,
3601 [EXIT_REASON_DR_ACCESS] = handle_dr,
3602 [EXIT_REASON_CPUID] = handle_cpuid,
3603 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3604 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3605 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3606 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003607 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003608 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003609 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3610 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3611 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3612 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3613 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3614 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3615 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3616 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3617 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003618 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3619 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003620 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003621 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003622 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003623 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3624 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003625 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003626 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3627 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628};
3629
3630static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003631 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003632
3633/*
3634 * The guest has exited. See if we can fix it or if we need userspace
3635 * assistance.
3636 */
Avi Kivity851ba692009-08-24 11:10:17 +03003637static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003639 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003640 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003641 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003642
Avi Kivity5bfd8b52010-03-11 10:50:44 +02003643 trace_kvm_exit(exit_reason, vcpu);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003644
Mohammed Gamal80ced182009-09-01 12:48:18 +02003645 /* If guest state is invalid, start emulating */
3646 if (vmx->emulation_required && emulate_invalid_guest_state)
3647 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003648
Sheng Yang14394422008-04-28 12:24:45 +08003649 /* Access CR3 don't cause VMExit in paging mode, so we need
3650 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003651 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003652 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003653
Avi Kivity29bd8a72007-09-10 17:27:03 +03003654 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003655 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3656 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003657 = vmcs_read32(VM_INSTRUCTION_ERROR);
3658 return 0;
3659 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660
Mike Dayd77c26f2007-10-08 09:02:08 -04003661 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003662 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003663 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3664 exit_reason != EXIT_REASON_TASK_SWITCH))
3665 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3666 "(0x%x) and exit reason is 0x%x\n",
3667 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003668
3669 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003670 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003671 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003672 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003673 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003674 /*
3675 * This CPU don't support us in finding the end of an
3676 * NMI-blocked window if the guest runs with IRQs
3677 * disabled. So we pull the trigger after 1 s of
3678 * futile waiting, but inform the user about this.
3679 */
3680 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3681 "state on VCPU %d after 1 s timeout\n",
3682 __func__, vcpu->vcpu_id);
3683 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003684 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003685 }
3686
Avi Kivity6aa8b732006-12-10 02:21:36 -08003687 if (exit_reason < kvm_vmx_max_exit_handlers
3688 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003689 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003690 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003691 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3692 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003693 }
3694 return 0;
3695}
3696
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003697static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003698{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003699 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003700 vmcs_write32(TPR_THRESHOLD, 0);
3701 return;
3702 }
3703
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003704 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003705}
3706
Avi Kivitycf393f72008-07-01 16:20:21 +03003707static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3708{
3709 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003710 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003711 bool unblock_nmi;
3712 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003713 int type;
3714 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003715
3716 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003717
Andi Kleena0861c02009-06-08 17:37:09 +08003718 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3719
3720 /* Handle machine checks before interrupts are enabled */
3721 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3722 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3723 && is_machine_check(exit_intr_info)))
3724 kvm_machine_check();
3725
Gleb Natapov20f65982009-05-11 13:35:55 +03003726 /* We need to handle NMIs before interrupts are enabled */
3727 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003728 (exit_intr_info & INTR_INFO_VALID_MASK)) {
3729 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03003730 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08003731 kvm_after_handle_nmi(&vmx->vcpu);
3732 }
Gleb Natapov20f65982009-05-11 13:35:55 +03003733
3734 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3735
Avi Kivitycf393f72008-07-01 16:20:21 +03003736 if (cpu_has_virtual_nmis()) {
3737 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3738 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3739 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003740 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003741 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3742 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003743 * SDM 3: 23.2.2 (September 2008)
3744 * Bit 12 is undefined in any of the following cases:
3745 * If the VM exit sets the valid bit in the IDT-vectoring
3746 * information field.
3747 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003748 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003749 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3750 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003751 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3752 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003753 } else if (unlikely(vmx->soft_vnmi_blocked))
3754 vmx->vnmi_blocked_time +=
3755 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003756
Gleb Natapov37b96e92009-03-30 16:03:13 +03003757 vmx->vcpu.arch.nmi_injected = false;
3758 kvm_clear_exception_queue(&vmx->vcpu);
3759 kvm_clear_interrupt_queue(&vmx->vcpu);
3760
3761 if (!idtv_info_valid)
3762 return;
3763
Avi Kivity668f6122008-07-02 09:28:55 +03003764 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3765 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003766
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003767 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003768 case INTR_TYPE_NMI_INTR:
3769 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003770 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003771 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003772 * Clear bit "block by NMI" before VM entry if a NMI
3773 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003774 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003775 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3776 GUEST_INTR_STATE_NMI);
3777 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003778 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003779 vmx->vcpu.arch.event_exit_inst_len =
3780 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3781 /* fall through */
3782 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003783 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003784 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3785 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003786 } else
3787 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003788 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003789 case INTR_TYPE_SOFT_INTR:
3790 vmx->vcpu.arch.event_exit_inst_len =
3791 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3792 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003793 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003794 kvm_queue_interrupt(&vmx->vcpu, vector,
3795 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003796 break;
3797 default:
3798 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003799 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003800}
3801
Avi Kivity9c8cba32007-11-22 11:42:59 +02003802/*
3803 * Failure to inject an interrupt should give us the information
3804 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3805 * when fetching the interrupt redirection bitmap in the real-mode
3806 * tss, this doesn't happen. So we do it ourselves.
3807 */
3808static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3809{
3810 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003811 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003812 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003813 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003814 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3815 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3816 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3817 return;
3818 }
3819 vmx->idt_vectoring_info =
3820 VECTORING_INFO_VALID_MASK
3821 | INTR_TYPE_EXT_INTR
3822 | vmx->rmode.irq.vector;
3823}
3824
Avi Kivityc8019492008-07-14 14:44:59 +03003825#ifdef CONFIG_X86_64
3826#define R "r"
3827#define Q "q"
3828#else
3829#define R "e"
3830#define Q "l"
3831#endif
3832
Avi Kivity851ba692009-08-24 11:10:17 +03003833static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003834{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003835 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003836
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003837 /* Record the guest's net vcpu time for enforced NMI injections. */
3838 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3839 vmx->entry_time = ktime_get();
3840
Mohammed Gamal80ced182009-09-01 12:48:18 +02003841 /* Don't enter VMX if guest state is invalid, let the exit handler
3842 start emulation until we arrive back to a valid state */
3843 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003844 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003845
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003846 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3847 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3848 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3849 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3850
Gleb Natapov787ff732009-05-18 11:44:06 +03003851 /* When single-stepping over STI and MOV SS, we must clear the
3852 * corresponding interruptibility bits in the guest state. Otherwise
3853 * vmentry fails as it then expects bit 14 (BS) in pending debug
3854 * exceptions being set, but that's not correct for the guest debugging
3855 * case. */
3856 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3857 vmx_set_interrupt_shadow(vcpu, 0);
3858
Mike Dayd77c26f2007-10-08 09:02:08 -04003859 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003860 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003861 "push %%"R"dx; push %%"R"bp;"
3862 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003863 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3864 "je 1f \n\t"
3865 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003866 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003867 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003868 /* Reload cr2 if changed */
3869 "mov %c[cr2](%0), %%"R"ax \n\t"
3870 "mov %%cr2, %%"R"dx \n\t"
3871 "cmp %%"R"ax, %%"R"dx \n\t"
3872 "je 2f \n\t"
3873 "mov %%"R"ax, %%cr2 \n\t"
3874 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003875 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003876 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003877 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003878 "mov %c[rax](%0), %%"R"ax \n\t"
3879 "mov %c[rbx](%0), %%"R"bx \n\t"
3880 "mov %c[rdx](%0), %%"R"dx \n\t"
3881 "mov %c[rsi](%0), %%"R"si \n\t"
3882 "mov %c[rdi](%0), %%"R"di \n\t"
3883 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003884#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003885 "mov %c[r8](%0), %%r8 \n\t"
3886 "mov %c[r9](%0), %%r9 \n\t"
3887 "mov %c[r10](%0), %%r10 \n\t"
3888 "mov %c[r11](%0), %%r11 \n\t"
3889 "mov %c[r12](%0), %%r12 \n\t"
3890 "mov %c[r13](%0), %%r13 \n\t"
3891 "mov %c[r14](%0), %%r14 \n\t"
3892 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003894 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3895
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003897 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003898 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003899 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003900 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003901 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003902 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003903 "xchg %0, (%%"R"sp) \n\t"
3904 "mov %%"R"ax, %c[rax](%0) \n\t"
3905 "mov %%"R"bx, %c[rbx](%0) \n\t"
3906 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3907 "mov %%"R"dx, %c[rdx](%0) \n\t"
3908 "mov %%"R"si, %c[rsi](%0) \n\t"
3909 "mov %%"R"di, %c[rdi](%0) \n\t"
3910 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003911#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003912 "mov %%r8, %c[r8](%0) \n\t"
3913 "mov %%r9, %c[r9](%0) \n\t"
3914 "mov %%r10, %c[r10](%0) \n\t"
3915 "mov %%r11, %c[r11](%0) \n\t"
3916 "mov %%r12, %c[r12](%0) \n\t"
3917 "mov %%r13, %c[r13](%0) \n\t"
3918 "mov %%r14, %c[r14](%0) \n\t"
3919 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003921 "mov %%cr2, %%"R"ax \n\t"
3922 "mov %%"R"ax, %c[cr2](%0) \n\t"
3923
3924 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003925 "setbe %c[fail](%0) \n\t"
3926 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3927 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3928 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03003929 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003930 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3931 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3932 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3933 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3934 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3935 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3936 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003937#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003938 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3939 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3940 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3941 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3942 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3943 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3944 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3945 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003946#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003947 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003948 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003949 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003950#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003951 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3952#endif
3953 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003955 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3956 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003957 vcpu->arch.regs_dirty = 0;
3958
Avi Kivity1155f762007-11-22 11:30:47 +02003959 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003960 if (vmx->rmode.irq.pending)
3961 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003962
Mike Dayd77c26f2007-10-08 09:02:08 -04003963 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003964 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003965
Avi Kivitycf393f72008-07-01 16:20:21 +03003966 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003967}
3968
Avi Kivityc8019492008-07-14 14:44:59 +03003969#undef R
3970#undef Q
3971
Avi Kivity6aa8b732006-12-10 02:21:36 -08003972static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3973{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003974 struct vcpu_vmx *vmx = to_vmx(vcpu);
3975
3976 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003977 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003978 free_vmcs(vmx->vmcs);
3979 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003980 }
3981}
3982
3983static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3984{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003985 struct vcpu_vmx *vmx = to_vmx(vcpu);
3986
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08003987 free_vpid(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003988 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003989 kfree(vmx->guest_msrs);
3990 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003991 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003992}
3993
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003994static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003995{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003996 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003997 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003998 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003999
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004000 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004001 return ERR_PTR(-ENOMEM);
4002
Sheng Yang2384d2b2008-01-17 15:14:33 +08004003 allocate_vpid(vmx);
4004
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004005 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
4006 if (err)
4007 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004008
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004009 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004010 if (!vmx->guest_msrs) {
4011 err = -ENOMEM;
4012 goto uninit_vcpu;
4013 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08004014
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004015 vmx->vmcs = alloc_vmcs();
4016 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004017 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004018
4019 vmcs_clear(vmx->vmcs);
4020
Avi Kivity15ad7142007-07-11 18:17:21 +03004021 cpu = get_cpu();
4022 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10004023 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004024 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004025 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004026 if (err)
4027 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004028 if (vm_need_virtualize_apic_accesses(kvm))
4029 if (alloc_apic_access_page(kvm) != 0)
4030 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004031
Sheng Yangb927a3c2009-07-21 10:42:48 +08004032 if (enable_ept) {
4033 if (!kvm->arch.ept_identity_map_addr)
4034 kvm->arch.ept_identity_map_addr =
4035 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004036 if (alloc_identity_pagetable(kvm) != 0)
4037 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004038 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004039
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004040 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004041
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004042free_vmcs:
4043 free_vmcs(vmx->vmcs);
4044free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004045 kfree(vmx->guest_msrs);
4046uninit_vcpu:
4047 kvm_vcpu_uninit(&vmx->vcpu);
4048free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004049 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10004050 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004051 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004052}
4053
Yang, Sheng002c7f72007-07-31 14:23:01 +03004054static void __init vmx_check_processor_compat(void *rtn)
4055{
4056 struct vmcs_config vmcs_conf;
4057
4058 *(int *)rtn = 0;
4059 if (setup_vmcs_config(&vmcs_conf) < 0)
4060 *(int *)rtn = -EIO;
4061 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
4062 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
4063 smp_processor_id());
4064 *(int *)rtn = -EIO;
4065 }
4066}
4067
Sheng Yang67253af2008-04-25 10:20:22 +08004068static int get_ept_level(void)
4069{
4070 return VMX_EPT_DEFAULT_GAW + 1;
4071}
4072
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004073static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08004074{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004075 u64 ret;
4076
Sheng Yang522c68c2009-04-27 20:35:43 +08004077 /* For VT-d and EPT combination
4078 * 1. MMIO: always map as UC
4079 * 2. EPT with VT-d:
4080 * a. VT-d without snooping control feature: can't guarantee the
4081 * result, try to trust guest.
4082 * b. VT-d with snooping control feature: snooping control feature of
4083 * VT-d engine can guarantee the cache correctness. Just set it
4084 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08004085 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08004086 * consistent with host MTRR
4087 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004088 if (is_mmio)
4089 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08004090 else if (vcpu->kvm->arch.iommu_domain &&
4091 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
4092 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
4093 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004094 else
Sheng Yang522c68c2009-04-27 20:35:43 +08004095 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08004096 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004097
4098 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08004099}
4100
Avi Kivityf4c9e872009-12-28 16:06:35 +02004101#define _ER(x) { EXIT_REASON_##x, #x }
4102
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004103static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02004104 _ER(EXCEPTION_NMI),
4105 _ER(EXTERNAL_INTERRUPT),
4106 _ER(TRIPLE_FAULT),
4107 _ER(PENDING_INTERRUPT),
4108 _ER(NMI_WINDOW),
4109 _ER(TASK_SWITCH),
4110 _ER(CPUID),
4111 _ER(HLT),
4112 _ER(INVLPG),
4113 _ER(RDPMC),
4114 _ER(RDTSC),
4115 _ER(VMCALL),
4116 _ER(VMCLEAR),
4117 _ER(VMLAUNCH),
4118 _ER(VMPTRLD),
4119 _ER(VMPTRST),
4120 _ER(VMREAD),
4121 _ER(VMRESUME),
4122 _ER(VMWRITE),
4123 _ER(VMOFF),
4124 _ER(VMON),
4125 _ER(CR_ACCESS),
4126 _ER(DR_ACCESS),
4127 _ER(IO_INSTRUCTION),
4128 _ER(MSR_READ),
4129 _ER(MSR_WRITE),
4130 _ER(MWAIT_INSTRUCTION),
4131 _ER(MONITOR_INSTRUCTION),
4132 _ER(PAUSE_INSTRUCTION),
4133 _ER(MCE_DURING_VMENTRY),
4134 _ER(TPR_BELOW_THRESHOLD),
4135 _ER(APIC_ACCESS),
4136 _ER(EPT_VIOLATION),
4137 _ER(EPT_MISCONFIG),
4138 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004139 { -1, NULL }
4140};
4141
Avi Kivityf4c9e872009-12-28 16:06:35 +02004142#undef _ER
4143
Sheng Yang17cc3932010-01-05 19:02:27 +08004144static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004145{
Sheng Yang878403b2010-01-05 19:02:29 +08004146 if (enable_ept && !cpu_has_vmx_ept_1g_page())
4147 return PT_DIRECTORY_LEVEL;
4148 else
4149 /* For shadow and EPT supported 1GB page */
4150 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004151}
4152
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004153static inline u32 bit(int bitno)
4154{
4155 return 1 << (bitno & 31);
4156}
4157
Sheng Yang0e851882009-12-18 16:48:46 +08004158static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4159{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004160 struct kvm_cpuid_entry2 *best;
4161 struct vcpu_vmx *vmx = to_vmx(vcpu);
4162 u32 exec_control;
4163
4164 vmx->rdtscp_enabled = false;
4165 if (vmx_rdtscp_supported()) {
4166 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4167 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4168 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4169 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4170 vmx->rdtscp_enabled = true;
4171 else {
4172 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4173 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4174 exec_control);
4175 }
4176 }
4177 }
Sheng Yang0e851882009-12-18 16:48:46 +08004178}
4179
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004180static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4181{
4182}
4183
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004184static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004185 .cpu_has_kvm_support = cpu_has_kvm_support,
4186 .disabled_by_bios = vmx_disabled_by_bios,
4187 .hardware_setup = hardware_setup,
4188 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004189 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004190 .hardware_enable = hardware_enable,
4191 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004192 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004193
4194 .vcpu_create = vmx_create_vcpu,
4195 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004196 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197
Avi Kivity04d2cc72007-09-10 18:10:54 +03004198 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004199 .vcpu_load = vmx_vcpu_load,
4200 .vcpu_put = vmx_vcpu_put,
4201
4202 .set_guest_debug = set_guest_debug,
4203 .get_msr = vmx_get_msr,
4204 .set_msr = vmx_set_msr,
4205 .get_segment_base = vmx_get_segment_base,
4206 .get_segment = vmx_get_segment,
4207 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004208 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004209 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004210 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004211 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004212 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004213 .set_cr3 = vmx_set_cr3,
4214 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216 .get_idt = vmx_get_idt,
4217 .set_idt = vmx_set_idt,
4218 .get_gdt = vmx_get_gdt,
4219 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004220 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004221 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004222 .get_rflags = vmx_get_rflags,
4223 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02004224 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004225 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004226
4227 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004228
Avi Kivity6aa8b732006-12-10 02:21:36 -08004229 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004230 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004231 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004232 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4233 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004234 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004235 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004236 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004237 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004238 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004239 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004240 .get_nmi_mask = vmx_get_nmi_mask,
4241 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004242 .enable_nmi_window = enable_nmi_window,
4243 .enable_irq_window = enable_irq_window,
4244 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004245
Izik Eiduscbc94022007-10-25 00:29:55 +02004246 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004247 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004248 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004249
4250 .exit_reasons_str = vmx_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08004251 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004252
4253 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004254
4255 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004256
4257 .set_supported_cpuid = vmx_set_supported_cpuid,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258};
4259
4260static int __init vmx_init(void)
4261{
Avi Kivity26bb0982009-09-07 11:14:12 +03004262 int r, i;
4263
4264 rdmsrl_safe(MSR_EFER, &host_efer);
4265
4266 for (i = 0; i < NR_VMX_MSR; ++i)
4267 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004268
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004269 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004270 if (!vmx_io_bitmap_a)
4271 return -ENOMEM;
4272
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004273 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004274 if (!vmx_io_bitmap_b) {
4275 r = -ENOMEM;
4276 goto out;
4277 }
4278
Avi Kivity58972972009-02-24 22:26:47 +02004279 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4280 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004281 r = -ENOMEM;
4282 goto out1;
4283 }
4284
Avi Kivity58972972009-02-24 22:26:47 +02004285 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4286 if (!vmx_msr_bitmap_longmode) {
4287 r = -ENOMEM;
4288 goto out2;
4289 }
4290
He, Qingfdef3ad2007-04-30 09:45:24 +03004291 /*
4292 * Allow direct access to the PC debug port (it is often used for I/O
4293 * delays, but the vmexits simply slow things down).
4294 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004295 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4296 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004297
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004298 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004299
Avi Kivity58972972009-02-24 22:26:47 +02004300 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4301 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004302
Sheng Yang2384d2b2008-01-17 15:14:33 +08004303 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4304
Avi Kivity0ee75be2010-04-28 15:39:01 +03004305 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
4306 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004307 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004308 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004309
Avi Kivity58972972009-02-24 22:26:47 +02004310 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4311 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4312 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4313 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4314 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4315 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004316
Avi Kivity089d0342009-03-23 18:26:32 +02004317 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004318 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004319 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004320 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004321 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004322 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004323 kvm_enable_tdp();
4324 } else
4325 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004326
Avi Kivityc7addb92007-09-16 18:58:32 +02004327 if (bypass_guest_pf)
4328 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4329
He, Qingfdef3ad2007-04-30 09:45:24 +03004330 return 0;
4331
Avi Kivity58972972009-02-24 22:26:47 +02004332out3:
4333 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004334out2:
Avi Kivity58972972009-02-24 22:26:47 +02004335 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004336out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004337 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004338out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004339 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004340 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004341}
4342
4343static void __exit vmx_exit(void)
4344{
Avi Kivity58972972009-02-24 22:26:47 +02004345 free_page((unsigned long)vmx_msr_bitmap_legacy);
4346 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004347 free_page((unsigned long)vmx_io_bitmap_b);
4348 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004349
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004350 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004351}
4352
4353module_init(vmx_init)
4354module_exit(vmx_exit)