blob: 1444e4149c4d3e6e86c3fff12dd06c54308b0cd7 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02008 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08009 *
10 * Authors:
11 * Avi Kivity <avi@qumranet.com>
12 * Yaniv Kamay <yaniv@qumranet.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
17 */
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040021
Avi Kivityedf88412007-12-16 11:02:48 +020022#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/mm.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020028#include <linux/moduleparam.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Shane Wangcafd6652010-04-29 12:09:01 -040031#include <linux/tboot.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030032#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030033#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040034
Avi Kivity6aa8b732006-12-10 02:21:36 -080035#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080036#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020037#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020038#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080039#include <asm/mce.h>
Dexuan Cui2acf9232010-06-10 11:27:12 +080040#include <asm/i387.h>
41#include <asm/xcr.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080042
Marcelo Tosatti229456f2009-06-17 09:22:14 -030043#include "trace.h"
44
Avi Kivity4ecac3f2008-05-13 13:23:38 +030045#define __ex(x) __kvm_handle_fault_on_reboot(x)
Avi Kivity5e520e62011-05-15 10:13:12 -040046#define __ex_clear(x, reg) \
47 ____kvm_handle_fault_on_reboot(x, "xor " reg " , " reg)
Avi Kivity4ecac3f2008-05-13 13:23:38 +030048
Avi Kivity6aa8b732006-12-10 02:21:36 -080049MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Avi Kivity4462d212009-03-23 17:53:37 +020052static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020053module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020054
Avi Kivity4462d212009-03-23 17:53:37 +020055static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020056module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080057
Avi Kivity4462d212009-03-23 17:53:37 +020058static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020059module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020060
Avi Kivity4462d212009-03-23 17:53:37 +020061static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020062module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080063
Nitin A Kamble3a624e22009-06-08 11:34:16 -070064static int __read_mostly enable_unrestricted_guest = 1;
65module_param_named(unrestricted_guest,
66 enable_unrestricted_guest, bool, S_IRUGO);
67
Avi Kivity4462d212009-03-23 17:53:37 +020068static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020069module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030070
Dongxiao Xub923e622010-05-11 18:29:45 +080071static int __read_mostly vmm_exclusive = 1;
72module_param(vmm_exclusive, bool, S_IRUGO);
73
Anthony Liguori443381a2010-12-06 10:53:38 -060074static int __read_mostly yield_on_hlt = 1;
75module_param(yield_on_hlt, bool, S_IRUGO);
76
Avi Kivitycdc0e242009-12-06 17:21:14 +020077#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
78 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
79#define KVM_GUEST_CR0_MASK \
80 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
81#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity81231c62010-01-24 16:26:40 +020082 (X86_CR0_WP | X86_CR0_NE)
Avi Kivitycdc0e242009-12-06 17:21:14 +020083#define KVM_VM_CR0_ALWAYS_ON \
84 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +020085#define KVM_CR4_GUEST_OWNED_BITS \
86 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
87 | X86_CR4_OSXMMEXCPT)
88
Avi Kivitycdc0e242009-12-06 17:21:14 +020089#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
90#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
91
Avi Kivity78ac8b42010-04-08 18:19:35 +030092#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
93
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080094/*
95 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
96 * ple_gap: upper bound on the amount of time between two successive
97 * executions of PAUSE in a loop. Also indicate if ple enabled.
Rik van Riel00c25bc2011-01-04 09:51:33 -050098 * According to test, this time is usually smaller than 128 cycles.
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080099 * ple_window: upper bound on the amount of time a guest is allowed to execute
100 * in a PAUSE loop. Tests indicate that most spinlocks are held for
101 * less than 2^12 cycles
102 * Time is measured based on a counter that runs at the same rate as the TSC,
103 * refer SDM volume 3b section 21.6.13 & 22.1.3.
104 */
Rik van Riel00c25bc2011-01-04 09:51:33 -0500105#define KVM_VMX_DEFAULT_PLE_GAP 128
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800106#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
107static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
108module_param(ple_gap, int, S_IRUGO);
109
110static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
111module_param(ple_window, int, S_IRUGO);
112
Avi Kivity61d2ef22010-04-28 16:40:38 +0300113#define NR_AUTOLOAD_MSRS 1
114
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400115struct vmcs {
116 u32 revision_id;
117 u32 abort;
118 char data[0];
119};
120
Nadav Har'Eld462b812011-05-24 15:26:10 +0300121/*
122 * Track a VMCS that may be loaded on a certain CPU. If it is (cpu!=-1), also
123 * remember whether it was VMLAUNCHed, and maintain a linked list of all VMCSs
124 * loaded on this CPU (so we can clear them if the CPU goes down).
125 */
126struct loaded_vmcs {
127 struct vmcs *vmcs;
128 int cpu;
129 int launched;
130 struct list_head loaded_vmcss_on_cpu_link;
131};
132
Avi Kivity26bb0982009-09-07 11:14:12 +0300133struct shared_msr_entry {
134 unsigned index;
135 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200136 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300137};
138
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400139struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000140 struct kvm_vcpu vcpu;
Avi Kivity313dbd42008-07-17 18:04:30 +0300141 unsigned long host_rsp;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300142 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200143 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200144 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300145 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200146 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200147 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300148 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400149 int nmsrs;
150 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400151#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300152 u64 msr_host_kernel_gs_base;
153 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400154#endif
Nadav Har'Eld462b812011-05-24 15:26:10 +0300155 /*
156 * loaded_vmcs points to the VMCS currently used in this vcpu. For a
157 * non-nested (L1) guest, it always points to vmcs01. For a nested
158 * guest (L2), it points to a different VMCS.
159 */
160 struct loaded_vmcs vmcs01;
161 struct loaded_vmcs *loaded_vmcs;
162 bool __launched; /* temporary, used in vmx_vcpu_run */
Avi Kivity61d2ef22010-04-28 16:40:38 +0300163 struct msr_autoload {
164 unsigned nr;
165 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
166 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
167 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400168 struct {
169 int loaded;
170 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200171 int gs_ldt_reload_needed;
172 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400173 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200174 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300175 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300176 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300177 struct kvm_save_segment {
178 u16 selector;
179 unsigned long base;
180 u32 limit;
181 u32 ar;
182 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200183 } rmode;
Avi Kivity2fb92db2011-04-27 19:42:18 +0300184 struct {
185 u32 bitmask; /* 4 bits per segment (1 bit per field) */
186 struct kvm_save_segment seg[8];
187 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800188 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300189 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200190
191 /* Support for vnmi-less CPUs */
192 int soft_vnmi_blocked;
193 ktime_t entry_time;
194 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800195 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800196
197 bool rdtscp_enabled;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400198};
199
Avi Kivity2fb92db2011-04-27 19:42:18 +0300200enum segment_cache_field {
201 SEG_FIELD_SEL = 0,
202 SEG_FIELD_BASE = 1,
203 SEG_FIELD_LIMIT = 2,
204 SEG_FIELD_AR = 3,
205
206 SEG_FIELD_NR = 4
207};
208
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400209static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
210{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000211 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400212}
213
Sheng Yang4e1096d2008-07-06 19:16:51 +0800214static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800215static void kvm_cpu_vmxon(u64 addr);
216static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200217static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200218static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Avi Kivity75880a02007-06-20 11:20:04 +0300219
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220static DEFINE_PER_CPU(struct vmcs *, vmxarea);
221static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Nadav Har'Eld462b812011-05-24 15:26:10 +0300222/*
223 * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
224 * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
225 */
226static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300227static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200229static unsigned long *vmx_io_bitmap_a;
230static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200231static unsigned long *vmx_msr_bitmap_legacy;
232static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300233
Avi Kivity110312c2010-12-21 12:54:20 +0200234static bool cpu_has_load_ia32_efer;
235
Sheng Yang2384d2b2008-01-17 15:14:33 +0800236static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
237static DEFINE_SPINLOCK(vmx_vpid_lock);
238
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300239static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240 int size;
241 int order;
242 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300243 u32 pin_based_exec_ctrl;
244 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800245 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300246 u32 vmexit_ctrl;
247 u32 vmentry_ctrl;
248} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800249
Hannes Ederefff9e52008-11-28 17:02:06 +0100250static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800251 u32 ept;
252 u32 vpid;
253} vmx_capability;
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255#define VMX_SEGMENT_FIELD(seg) \
256 [VCPU_SREG_##seg] = { \
257 .selector = GUEST_##seg##_SELECTOR, \
258 .base = GUEST_##seg##_BASE, \
259 .limit = GUEST_##seg##_LIMIT, \
260 .ar_bytes = GUEST_##seg##_AR_BYTES, \
261 }
262
263static struct kvm_vmx_segment_field {
264 unsigned selector;
265 unsigned base;
266 unsigned limit;
267 unsigned ar_bytes;
268} kvm_vmx_segment_fields[] = {
269 VMX_SEGMENT_FIELD(CS),
270 VMX_SEGMENT_FIELD(DS),
271 VMX_SEGMENT_FIELD(ES),
272 VMX_SEGMENT_FIELD(FS),
273 VMX_SEGMENT_FIELD(GS),
274 VMX_SEGMENT_FIELD(SS),
275 VMX_SEGMENT_FIELD(TR),
276 VMX_SEGMENT_FIELD(LDTR),
277};
278
Avi Kivity26bb0982009-09-07 11:14:12 +0300279static u64 host_efer;
280
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300281static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
282
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300283/*
Brian Gerst8c065852010-07-17 09:03:26 -0400284 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300285 * away by decrementing the array size.
286 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800288#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300289 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400291 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800292};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200293#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800294
Gui Jianfeng31299942010-03-15 17:29:09 +0800295static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296{
297 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
298 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100299 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300}
301
Gui Jianfeng31299942010-03-15 17:29:09 +0800302static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300303{
304 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
305 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100306 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300307}
308
Gui Jianfeng31299942010-03-15 17:29:09 +0800309static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500310{
311 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
312 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100313 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500314}
315
Gui Jianfeng31299942010-03-15 17:29:09 +0800316static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800317{
318 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
319 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
320}
321
Gui Jianfeng31299942010-03-15 17:29:09 +0800322static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800323{
324 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
325 INTR_INFO_VALID_MASK)) ==
326 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
327}
328
Gui Jianfeng31299942010-03-15 17:29:09 +0800329static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800330{
Sheng Yang04547152009-04-01 15:52:31 +0800331 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800332}
333
Gui Jianfeng31299942010-03-15 17:29:09 +0800334static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800335{
Sheng Yang04547152009-04-01 15:52:31 +0800336 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800337}
338
Gui Jianfeng31299942010-03-15 17:29:09 +0800339static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800340{
Sheng Yang04547152009-04-01 15:52:31 +0800341 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800342}
343
Gui Jianfeng31299942010-03-15 17:29:09 +0800344static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800345{
Sheng Yang04547152009-04-01 15:52:31 +0800346 return vmcs_config.cpu_based_exec_ctrl &
347 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800348}
349
Avi Kivity774ead32007-12-26 13:57:04 +0200350static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800351{
Sheng Yang04547152009-04-01 15:52:31 +0800352 return vmcs_config.cpu_based_2nd_exec_ctrl &
353 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
354}
355
356static inline bool cpu_has_vmx_flexpriority(void)
357{
358 return cpu_has_vmx_tpr_shadow() &&
359 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800360}
361
Marcelo Tosattie7997942009-06-11 12:07:40 -0300362static inline bool cpu_has_vmx_ept_execute_only(void)
363{
Gui Jianfeng31299942010-03-15 17:29:09 +0800364 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300365}
366
367static inline bool cpu_has_vmx_eptp_uncacheable(void)
368{
Gui Jianfeng31299942010-03-15 17:29:09 +0800369 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300370}
371
372static inline bool cpu_has_vmx_eptp_writeback(void)
373{
Gui Jianfeng31299942010-03-15 17:29:09 +0800374 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300375}
376
377static inline bool cpu_has_vmx_ept_2m_page(void)
378{
Gui Jianfeng31299942010-03-15 17:29:09 +0800379 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300380}
381
Sheng Yang878403b2010-01-05 19:02:29 +0800382static inline bool cpu_has_vmx_ept_1g_page(void)
383{
Gui Jianfeng31299942010-03-15 17:29:09 +0800384 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800385}
386
Sheng Yang4bc9b982010-06-02 14:05:24 +0800387static inline bool cpu_has_vmx_ept_4levels(void)
388{
389 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
390}
391
Gui Jianfeng31299942010-03-15 17:29:09 +0800392static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800393{
Gui Jianfeng31299942010-03-15 17:29:09 +0800394 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800395}
396
Gui Jianfeng31299942010-03-15 17:29:09 +0800397static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800398{
Gui Jianfeng31299942010-03-15 17:29:09 +0800399 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800400}
401
Gui Jianfeng31299942010-03-15 17:29:09 +0800402static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800403{
Gui Jianfeng31299942010-03-15 17:29:09 +0800404 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800405}
406
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800407static inline bool cpu_has_vmx_invvpid_single(void)
408{
409 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
410}
411
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800412static inline bool cpu_has_vmx_invvpid_global(void)
413{
414 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
415}
416
Gui Jianfeng31299942010-03-15 17:29:09 +0800417static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800418{
Sheng Yang04547152009-04-01 15:52:31 +0800419 return vmcs_config.cpu_based_2nd_exec_ctrl &
420 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800421}
422
Gui Jianfeng31299942010-03-15 17:29:09 +0800423static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700424{
425 return vmcs_config.cpu_based_2nd_exec_ctrl &
426 SECONDARY_EXEC_UNRESTRICTED_GUEST;
427}
428
Gui Jianfeng31299942010-03-15 17:29:09 +0800429static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800430{
431 return vmcs_config.cpu_based_2nd_exec_ctrl &
432 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
433}
434
Gui Jianfeng31299942010-03-15 17:29:09 +0800435static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800436{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800437 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800438}
439
Gui Jianfeng31299942010-03-15 17:29:09 +0800440static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800441{
Sheng Yang04547152009-04-01 15:52:31 +0800442 return vmcs_config.cpu_based_2nd_exec_ctrl &
443 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800444}
445
Gui Jianfeng31299942010-03-15 17:29:09 +0800446static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800447{
448 return vmcs_config.cpu_based_2nd_exec_ctrl &
449 SECONDARY_EXEC_RDTSCP;
450}
451
Gui Jianfeng31299942010-03-15 17:29:09 +0800452static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800453{
454 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
455}
456
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800457static inline bool cpu_has_vmx_wbinvd_exit(void)
458{
459 return vmcs_config.cpu_based_2nd_exec_ctrl &
460 SECONDARY_EXEC_WBINVD_EXITING;
461}
462
Sheng Yang04547152009-04-01 15:52:31 +0800463static inline bool report_flexpriority(void)
464{
465 return flexpriority_enabled;
466}
467
Rusty Russell8b9cf982007-07-30 16:31:43 +1000468static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800469{
470 int i;
471
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400472 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300473 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300474 return i;
475 return -1;
476}
477
Sheng Yang2384d2b2008-01-17 15:14:33 +0800478static inline void __invvpid(int ext, u16 vpid, gva_t gva)
479{
480 struct {
481 u64 vpid : 16;
482 u64 rsvd : 48;
483 u64 gva;
484 } operand = { vpid, 0, gva };
485
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300486 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800487 /* CF==1 or ZF==1 --> rc = -1 */
488 "; ja 1f ; ud2 ; 1:"
489 : : "a"(&operand), "c"(ext) : "cc", "memory");
490}
491
Sheng Yang14394422008-04-28 12:24:45 +0800492static inline void __invept(int ext, u64 eptp, gpa_t gpa)
493{
494 struct {
495 u64 eptp, gpa;
496 } operand = {eptp, gpa};
497
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300498 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800499 /* CF==1 or ZF==1 --> rc = -1 */
500 "; ja 1f ; ud2 ; 1:\n"
501 : : "a" (&operand), "c" (ext) : "cc", "memory");
502}
503
Avi Kivity26bb0982009-09-07 11:14:12 +0300504static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300505{
506 int i;
507
Rusty Russell8b9cf982007-07-30 16:31:43 +1000508 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300509 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400510 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000511 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800512}
513
Avi Kivity6aa8b732006-12-10 02:21:36 -0800514static void vmcs_clear(struct vmcs *vmcs)
515{
516 u64 phys_addr = __pa(vmcs);
517 u8 error;
518
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300519 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200520 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 : "cc", "memory");
522 if (error)
523 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
524 vmcs, phys_addr);
525}
526
Nadav Har'Eld462b812011-05-24 15:26:10 +0300527static inline void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
528{
529 vmcs_clear(loaded_vmcs->vmcs);
530 loaded_vmcs->cpu = -1;
531 loaded_vmcs->launched = 0;
532}
533
Dongxiao Xu7725b892010-05-11 18:29:38 +0800534static void vmcs_load(struct vmcs *vmcs)
535{
536 u64 phys_addr = __pa(vmcs);
537 u8 error;
538
539 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200540 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +0800541 : "cc", "memory");
542 if (error)
543 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
544 vmcs, phys_addr);
545}
546
Nadav Har'Eld462b812011-05-24 15:26:10 +0300547static void __loaded_vmcs_clear(void *arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800548{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300549 struct loaded_vmcs *loaded_vmcs = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800550 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800551
Nadav Har'Eld462b812011-05-24 15:26:10 +0300552 if (loaded_vmcs->cpu != cpu)
553 return; /* vcpu migration can race with cpu offline */
554 if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555 per_cpu(current_vmcs, cpu) = NULL;
Nadav Har'Eld462b812011-05-24 15:26:10 +0300556 list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
557 loaded_vmcs_init(loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558}
559
Nadav Har'Eld462b812011-05-24 15:26:10 +0300560static void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800561{
Nadav Har'Eld462b812011-05-24 15:26:10 +0300562 if (loaded_vmcs->cpu != -1)
563 smp_call_function_single(
564 loaded_vmcs->cpu, __loaded_vmcs_clear, loaded_vmcs, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800565}
566
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800567static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800568{
569 if (vmx->vpid == 0)
570 return;
571
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800572 if (cpu_has_vmx_invvpid_single())
573 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800574}
575
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800576static inline void vpid_sync_vcpu_global(void)
577{
578 if (cpu_has_vmx_invvpid_global())
579 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
580}
581
582static inline void vpid_sync_context(struct vcpu_vmx *vmx)
583{
584 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800585 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800586 else
587 vpid_sync_vcpu_global();
588}
589
Sheng Yang14394422008-04-28 12:24:45 +0800590static inline void ept_sync_global(void)
591{
592 if (cpu_has_vmx_invept_global())
593 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
594}
595
596static inline void ept_sync_context(u64 eptp)
597{
Avi Kivity089d0342009-03-23 18:26:32 +0200598 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800599 if (cpu_has_vmx_invept_context())
600 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
601 else
602 ept_sync_global();
603 }
604}
605
606static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
607{
Avi Kivity089d0342009-03-23 18:26:32 +0200608 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800609 if (cpu_has_vmx_invept_individual_addr())
610 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
611 eptp, gpa);
612 else
613 ept_sync_context(eptp);
614 }
615}
616
Avi Kivity96304212011-05-15 10:13:13 -0400617static __always_inline unsigned long vmcs_readl(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800618{
Avi Kivity5e520e62011-05-15 10:13:12 -0400619 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800620
Avi Kivity5e520e62011-05-15 10:13:12 -0400621 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
622 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623 return value;
624}
625
Avi Kivity96304212011-05-15 10:13:13 -0400626static __always_inline u16 vmcs_read16(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800627{
628 return vmcs_readl(field);
629}
630
Avi Kivity96304212011-05-15 10:13:13 -0400631static __always_inline u32 vmcs_read32(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800632{
633 return vmcs_readl(field);
634}
635
Avi Kivity96304212011-05-15 10:13:13 -0400636static __always_inline u64 vmcs_read64(unsigned long field)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800637{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800638#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800639 return vmcs_readl(field);
640#else
641 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
642#endif
643}
644
Avi Kivitye52de1b2007-01-05 16:36:56 -0800645static noinline void vmwrite_error(unsigned long field, unsigned long value)
646{
647 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
648 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
649 dump_stack();
650}
651
Avi Kivity6aa8b732006-12-10 02:21:36 -0800652static void vmcs_writel(unsigned long field, unsigned long value)
653{
654 u8 error;
655
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300656 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400657 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800658 if (unlikely(error))
659 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660}
661
662static void vmcs_write16(unsigned long field, u16 value)
663{
664 vmcs_writel(field, value);
665}
666
667static void vmcs_write32(unsigned long field, u32 value)
668{
669 vmcs_writel(field, value);
670}
671
672static void vmcs_write64(unsigned long field, u64 value)
673{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800674 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300675#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 asm volatile ("");
677 vmcs_writel(field+1, value >> 32);
678#endif
679}
680
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300681static void vmcs_clear_bits(unsigned long field, u32 mask)
682{
683 vmcs_writel(field, vmcs_readl(field) & ~mask);
684}
685
686static void vmcs_set_bits(unsigned long field, u32 mask)
687{
688 vmcs_writel(field, vmcs_readl(field) | mask);
689}
690
Avi Kivity2fb92db2011-04-27 19:42:18 +0300691static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
692{
693 vmx->segment_cache.bitmask = 0;
694}
695
696static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
697 unsigned field)
698{
699 bool ret;
700 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
701
702 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
703 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
704 vmx->segment_cache.bitmask = 0;
705 }
706 ret = vmx->segment_cache.bitmask & mask;
707 vmx->segment_cache.bitmask |= mask;
708 return ret;
709}
710
711static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
712{
713 u16 *p = &vmx->segment_cache.seg[seg].selector;
714
715 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
716 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
717 return *p;
718}
719
720static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
721{
722 ulong *p = &vmx->segment_cache.seg[seg].base;
723
724 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
725 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
726 return *p;
727}
728
729static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
730{
731 u32 *p = &vmx->segment_cache.seg[seg].limit;
732
733 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
734 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
735 return *p;
736}
737
738static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
739{
740 u32 *p = &vmx->segment_cache.seg[seg].ar;
741
742 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
743 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
744 return *p;
745}
746
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300747static void update_exception_bitmap(struct kvm_vcpu *vcpu)
748{
749 u32 eb;
750
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100751 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
752 (1u << NM_VECTOR) | (1u << DB_VECTOR);
753 if ((vcpu->guest_debug &
754 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
755 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
756 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300757 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300758 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200759 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800760 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200761 if (vcpu->fpu_active)
762 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300763 vmcs_write32(EXCEPTION_BITMAP, eb);
764}
765
Avi Kivity61d2ef22010-04-28 16:40:38 +0300766static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
767{
768 unsigned i;
769 struct msr_autoload *m = &vmx->msr_autoload;
770
Avi Kivity110312c2010-12-21 12:54:20 +0200771 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
772 vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
773 vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
774 return;
775 }
776
Avi Kivity61d2ef22010-04-28 16:40:38 +0300777 for (i = 0; i < m->nr; ++i)
778 if (m->guest[i].index == msr)
779 break;
780
781 if (i == m->nr)
782 return;
783 --m->nr;
784 m->guest[i] = m->guest[m->nr];
785 m->host[i] = m->host[m->nr];
786 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
787 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
788}
789
790static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
791 u64 guest_val, u64 host_val)
792{
793 unsigned i;
794 struct msr_autoload *m = &vmx->msr_autoload;
795
Avi Kivity110312c2010-12-21 12:54:20 +0200796 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
797 vmcs_write64(GUEST_IA32_EFER, guest_val);
798 vmcs_write64(HOST_IA32_EFER, host_val);
799 vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
800 vmcs_set_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
801 return;
802 }
803
Avi Kivity61d2ef22010-04-28 16:40:38 +0300804 for (i = 0; i < m->nr; ++i)
805 if (m->guest[i].index == msr)
806 break;
807
808 if (i == m->nr) {
809 ++m->nr;
810 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
811 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
812 }
813
814 m->guest[i].index = msr;
815 m->guest[i].value = guest_val;
816 m->host[i].index = msr;
817 m->host[i].value = host_val;
818}
819
Avi Kivity33ed6322007-05-02 16:54:03 +0300820static void reload_tss(void)
821{
Avi Kivity33ed6322007-05-02 16:54:03 +0300822 /*
823 * VT restores TR but not its size. Useless.
824 */
Avi Kivityd3591922010-07-26 18:32:39 +0300825 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +0200826 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300827
Avi Kivityd3591922010-07-26 18:32:39 +0300828 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +0300829 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
830 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300831}
832
Avi Kivity92c0d902009-10-29 11:00:16 +0200833static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300834{
Roel Kluin3a34a882009-08-04 02:08:45 -0700835 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300836 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300837
Avi Kivityf6801df2010-01-21 15:31:50 +0200838 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700839
Avi Kivity51c6cf62007-08-29 03:48:05 +0300840 /*
841 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
842 * outside long mode
843 */
844 ignore_bits = EFER_NX | EFER_SCE;
845#ifdef CONFIG_X86_64
846 ignore_bits |= EFER_LMA | EFER_LME;
847 /* SCE is meaningful only in long mode on Intel */
848 if (guest_efer & EFER_LMA)
849 ignore_bits &= ~(u64)EFER_SCE;
850#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300851 guest_efer &= ~ignore_bits;
852 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300853 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200854 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300855
856 clear_atomic_switch_msr(vmx, MSR_EFER);
857 /* On ept, can't emulate nx, and must switch nx atomically */
858 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
859 guest_efer = vmx->vcpu.arch.efer;
860 if (!(guest_efer & EFER_LMA))
861 guest_efer &= ~EFER_LME;
862 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
863 return false;
864 }
865
Avi Kivity26bb0982009-09-07 11:14:12 +0300866 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300867}
868
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200869static unsigned long segment_base(u16 selector)
870{
Avi Kivityd3591922010-07-26 18:32:39 +0300871 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200872 struct desc_struct *d;
873 unsigned long table_base;
874 unsigned long v;
875
876 if (!(selector & ~3))
877 return 0;
878
Avi Kivityd3591922010-07-26 18:32:39 +0300879 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200880
881 if (selector & 4) { /* from ldt */
882 u16 ldt_selector = kvm_read_ldt();
883
884 if (!(ldt_selector & ~3))
885 return 0;
886
887 table_base = segment_base(ldt_selector);
888 }
889 d = (struct desc_struct *)(table_base + (selector & ~7));
890 v = get_desc_base(d);
891#ifdef CONFIG_X86_64
892 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
893 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
894#endif
895 return v;
896}
897
898static inline unsigned long kvm_read_tr_base(void)
899{
900 u16 tr;
901 asm("str %0" : "=g"(tr));
902 return segment_base(tr);
903}
904
Avi Kivity04d2cc72007-09-10 18:10:54 +0300905static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300906{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300907 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300908 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300909
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400910 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300911 return;
912
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400913 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300914 /*
915 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
916 * allow segment selectors with cpl > 0 or ti == 1.
917 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300918 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200919 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +0200920 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200921 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400922 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200923 vmx->host_state.fs_reload_needed = 0;
924 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300925 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200926 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300927 }
Avi Kivity9581d442010-10-19 16:46:55 +0200928 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400929 if (!(vmx->host_state.gs_sel & 7))
930 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300931 else {
932 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200933 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300934 }
935
936#ifdef CONFIG_X86_64
937 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
938 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
939#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400940 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
941 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300942#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300943
944#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +0200945 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
946 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +0300947 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +0300948#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300949 for (i = 0; i < vmx->save_nmsrs; ++i)
950 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200951 vmx->guest_msrs[i].data,
952 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300953}
954
Avi Kivitya9b21b62008-06-24 11:48:49 +0300955static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300956{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400957 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300958 return;
959
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200960 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400961 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +0200962#ifdef CONFIG_X86_64
963 if (is_long_mode(&vmx->vcpu))
964 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
965#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200966 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300967 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300968#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +0200969 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +0200970#else
971 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300972#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300973 }
Avi Kivity0a77fe42010-10-19 18:48:35 +0200974 if (vmx->host_state.fs_reload_needed)
975 loadsegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200976 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300977#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +0200978 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +0300979#endif
Avi Kivity1c11e712010-05-03 16:05:44 +0300980 if (current_thread_info()->status & TS_USEDFPU)
981 clts();
Avi Kivity3444d7d2010-07-26 18:32:38 +0300982 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +0300983}
984
Avi Kivitya9b21b62008-06-24 11:48:49 +0300985static void vmx_load_host_state(struct vcpu_vmx *vmx)
986{
987 preempt_disable();
988 __vmx_load_host_state(vmx);
989 preempt_enable();
990}
991
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992/*
993 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
994 * vcpu mutex is already taken.
995 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300996static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400998 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800999 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001001 if (!vmm_exclusive)
1002 kvm_cpu_vmxon(phys_addr);
Nadav Har'Eld462b812011-05-24 15:26:10 +03001003 else if (vmx->loaded_vmcs->cpu != cpu)
1004 loaded_vmcs_clear(vmx->loaded_vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005
Nadav Har'Eld462b812011-05-24 15:26:10 +03001006 if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
1007 per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
1008 vmcs_load(vmx->loaded_vmcs->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001009 }
1010
Nadav Har'Eld462b812011-05-24 15:26:10 +03001011 if (vmx->loaded_vmcs->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +03001012 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013 unsigned long sysenter_esp;
1014
Avi Kivitya8eeb042010-05-10 12:34:53 +03001015 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001016 local_irq_disable();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001017 list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
1018 &per_cpu(loaded_vmcss_on_cpu, cpu));
Dongxiao Xu92fe13b2010-05-11 18:29:42 +08001019 local_irq_enable();
1020
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021 /*
1022 * Linux uses per-cpu TSS and GDT, so set these when switching
1023 * processors.
1024 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03001025 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +03001026 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001027
1028 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1029 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Nadav Har'Eld462b812011-05-24 15:26:10 +03001030 vmx->loaded_vmcs->cpu = cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001031 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032}
1033
1034static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1035{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001036 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001037 if (!vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001038 __loaded_vmcs_clear(to_vmx(vcpu)->loaded_vmcs);
1039 vcpu->cpu = -1;
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001040 kvm_cpu_vmxoff();
1041 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042}
1043
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001044static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1045{
Avi Kivity81231c62010-01-24 16:26:40 +02001046 ulong cr0;
1047
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001048 if (vcpu->fpu_active)
1049 return;
1050 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001051 cr0 = vmcs_readl(GUEST_CR0);
1052 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1053 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1054 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001055 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001056 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
1057 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001058}
1059
Avi Kivityedcafe32009-12-30 18:07:40 +02001060static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1061
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001062static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1063{
Avi Kivityedcafe32009-12-30 18:07:40 +02001064 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001065 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001066 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001067 vcpu->arch.cr0_guest_owned_bits = 0;
1068 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1069 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001070}
1071
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1073{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001074 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001075
Avi Kivity6de12732011-03-07 12:51:22 +02001076 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1077 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1078 rflags = vmcs_readl(GUEST_RFLAGS);
1079 if (to_vmx(vcpu)->rmode.vm86_active) {
1080 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1081 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1082 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1083 }
1084 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001085 }
Avi Kivity6de12732011-03-07 12:51:22 +02001086 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001087}
1088
1089static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1090{
Avi Kivity6de12732011-03-07 12:51:22 +02001091 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity69c73022011-03-07 15:26:44 +02001092 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6de12732011-03-07 12:51:22 +02001093 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001094 if (to_vmx(vcpu)->rmode.vm86_active) {
1095 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001096 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001097 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098 vmcs_writel(GUEST_RFLAGS, rflags);
1099}
1100
Glauber Costa2809f5d2009-05-12 16:21:05 -04001101static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1102{
1103 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1104 int ret = 0;
1105
1106 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001107 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001108 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001109 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001110
1111 return ret & mask;
1112}
1113
1114static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1115{
1116 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1117 u32 interruptibility = interruptibility_old;
1118
1119 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1120
Jan Kiszka48005f62010-02-19 19:38:07 +01001121 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001122 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001123 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001124 interruptibility |= GUEST_INTR_STATE_STI;
1125
1126 if ((interruptibility != interruptibility_old))
1127 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1128}
1129
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1131{
1132 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001134 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001135 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001136 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137
Glauber Costa2809f5d2009-05-12 16:21:05 -04001138 /* skipping an emulated instruction also counts */
1139 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001140}
1141
Anthony Liguori443381a2010-12-06 10:53:38 -06001142static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1143{
1144 /* Ensure that we clear the HLT state in the VMCS. We don't need to
1145 * explicitly skip the instruction because if the HLT state is set, then
1146 * the instruction is already executing and RIP has already been
1147 * advanced. */
1148 if (!yield_on_hlt &&
1149 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1150 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1151}
1152
Avi Kivity298101d2007-11-25 13:41:11 +02001153static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001154 bool has_error_code, u32 error_code,
1155 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001156{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001157 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001158 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001159
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001160 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001161 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001162 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1163 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001164
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001165 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001166 int inc_eip = 0;
1167 if (kvm_exception_is_soft(nr))
1168 inc_eip = vcpu->arch.event_exit_inst_len;
1169 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001170 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001171 return;
1172 }
1173
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001174 if (kvm_exception_is_soft(nr)) {
1175 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1176 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001177 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1178 } else
1179 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1180
1181 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Anthony Liguori443381a2010-12-06 10:53:38 -06001182 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001183}
1184
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001185static bool vmx_rdtscp_supported(void)
1186{
1187 return cpu_has_vmx_rdtscp();
1188}
1189
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190/*
Eddie Donga75beee2007-05-17 18:55:15 +03001191 * Swap MSR entry in host/guest MSR entry array.
1192 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001193static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001194{
Avi Kivity26bb0982009-09-07 11:14:12 +03001195 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001196
1197 tmp = vmx->guest_msrs[to];
1198 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1199 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001200}
1201
1202/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001203 * Set up the vmcs to automatically save and restore system
1204 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1205 * mode, as fiddling with msrs is very expensive.
1206 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001207static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001208{
Avi Kivity26bb0982009-09-07 11:14:12 +03001209 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +02001210 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +03001211
Avi Kivity33f9c502008-02-27 16:06:57 +02001212 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +03001213 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001214#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001215 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001216 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001217 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001218 move_msr_up(vmx, index, save_nmsrs++);
1219 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001220 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001221 move_msr_up(vmx, index, save_nmsrs++);
1222 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001223 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001224 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001225 index = __find_msr_index(vmx, MSR_TSC_AUX);
1226 if (index >= 0 && vmx->rdtscp_enabled)
1227 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001228 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001229 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001230 * if efer.sce is enabled.
1231 */
Brian Gerst8c065852010-07-17 09:03:26 -04001232 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001233 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001234 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001235 }
Eddie Donga75beee2007-05-17 18:55:15 +03001236#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001237 index = __find_msr_index(vmx, MSR_EFER);
1238 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001239 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001240
Avi Kivity26bb0982009-09-07 11:14:12 +03001241 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001242
1243 if (cpu_has_vmx_msr_bitmap()) {
1244 if (is_long_mode(&vmx->vcpu))
1245 msr_bitmap = vmx_msr_bitmap_longmode;
1246 else
1247 msr_bitmap = vmx_msr_bitmap_legacy;
1248
1249 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1250 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001251}
1252
1253/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001254 * reads and returns guest's timestamp counter "register"
1255 * guest_tsc = host_tsc + tsc_offset -- 21.3
1256 */
1257static u64 guest_read_tsc(void)
1258{
1259 u64 host_tsc, tsc_offset;
1260
1261 rdtscll(host_tsc);
1262 tsc_offset = vmcs_read64(TSC_OFFSET);
1263 return host_tsc + tsc_offset;
1264}
1265
1266/*
Joerg Roedel4051b182011-03-25 09:44:49 +01001267 * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
1268 * ioctl. In this case the call-back should update internal vmx state to make
1269 * the changes effective.
1270 */
1271static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
1272{
1273 /* Nothing to do here */
1274}
1275
1276/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001277 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001279static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001280{
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001281 vmcs_write64(TSC_OFFSET, offset);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282}
1283
Zachary Amsdene48672f2010-08-19 22:07:23 -10001284static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
1285{
1286 u64 offset = vmcs_read64(TSC_OFFSET);
1287 vmcs_write64(TSC_OFFSET, offset + adjustment);
1288}
1289
Joerg Roedel857e4092011-03-25 09:44:50 +01001290static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1291{
1292 return target_tsc - native_read_tsc();
1293}
1294
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295/*
1296 * Reads an msr value (of 'msr_index') into 'pdata'.
1297 * Returns 0 on success, non-0 otherwise.
1298 * Assumes vcpu_load() was already called.
1299 */
1300static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1301{
1302 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001303 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304
1305 if (!pdata) {
1306 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1307 return -EINVAL;
1308 }
1309
1310 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001311#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312 case MSR_FS_BASE:
1313 data = vmcs_readl(GUEST_FS_BASE);
1314 break;
1315 case MSR_GS_BASE:
1316 data = vmcs_readl(GUEST_GS_BASE);
1317 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001318 case MSR_KERNEL_GS_BASE:
1319 vmx_load_host_state(to_vmx(vcpu));
1320 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1321 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001322#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001324 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301325 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326 data = guest_read_tsc();
1327 break;
1328 case MSR_IA32_SYSENTER_CS:
1329 data = vmcs_read32(GUEST_SYSENTER_CS);
1330 break;
1331 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001332 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001333 break;
1334 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001335 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001337 case MSR_TSC_AUX:
1338 if (!to_vmx(vcpu)->rdtscp_enabled)
1339 return 1;
1340 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001342 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001343 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001344 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001345 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001346 data = msr->data;
1347 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001349 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 }
1351
1352 *pdata = data;
1353 return 0;
1354}
1355
1356/*
1357 * Writes msr value into into the appropriate "register".
1358 * Returns 0 on success, non-0 otherwise.
1359 * Assumes vcpu_load() was already called.
1360 */
1361static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1362{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001363 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001364 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001365 int ret = 0;
1366
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001368 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001369 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001370 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001371 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001372#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001374 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375 vmcs_writel(GUEST_FS_BASE, data);
1376 break;
1377 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001378 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379 vmcs_writel(GUEST_GS_BASE, data);
1380 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001381 case MSR_KERNEL_GS_BASE:
1382 vmx_load_host_state(vmx);
1383 vmx->msr_guest_kernel_gs_base = data;
1384 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385#endif
1386 case MSR_IA32_SYSENTER_CS:
1387 vmcs_write32(GUEST_SYSENTER_CS, data);
1388 break;
1389 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001390 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001391 break;
1392 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001393 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301395 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10001396 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001398 case MSR_IA32_CR_PAT:
1399 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1400 vmcs_write64(GUEST_IA32_PAT, data);
1401 vcpu->arch.pat = data;
1402 break;
1403 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001404 ret = kvm_set_msr_common(vcpu, msr_index, data);
1405 break;
1406 case MSR_TSC_AUX:
1407 if (!vmx->rdtscp_enabled)
1408 return 1;
1409 /* Check reserved bit, higher 32 bits should be zero */
1410 if ((data >> 32) != 0)
1411 return 1;
1412 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001414 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001415 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001416 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001417 msr->data = data;
1418 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001419 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001420 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001421 }
1422
Eddie Dong2cc51562007-05-21 07:28:09 +03001423 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424}
1425
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001426static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001427{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001428 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1429 switch (reg) {
1430 case VCPU_REGS_RSP:
1431 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1432 break;
1433 case VCPU_REGS_RIP:
1434 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1435 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001436 case VCPU_EXREG_PDPTR:
1437 if (enable_ept)
1438 ept_save_pdptrs(vcpu);
1439 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001440 default:
1441 break;
1442 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001443}
1444
Jan Kiszka355be0b2009-10-03 00:31:21 +02001445static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001446{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001447 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1448 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1449 else
1450 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1451
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001452 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001453}
1454
1455static __init int cpu_has_kvm_support(void)
1456{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001457 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458}
1459
1460static __init int vmx_disabled_by_bios(void)
1461{
1462 u64 msr;
1463
1464 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001465 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001466 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001467 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1468 && tboot_enabled())
1469 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001470 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001471 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001472 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001473 && !tboot_enabled()) {
1474 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001475 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001476 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001477 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001478 /* launched w/o TXT and VMX disabled */
1479 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1480 && !tboot_enabled())
1481 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001482 }
1483
1484 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485}
1486
Dongxiao Xu7725b892010-05-11 18:29:38 +08001487static void kvm_cpu_vmxon(u64 addr)
1488{
1489 asm volatile (ASM_VMX_VMXON_RAX
1490 : : "a"(&addr), "m"(addr)
1491 : "memory", "cc");
1492}
1493
Alexander Graf10474ae2009-09-15 11:37:46 +02001494static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495{
1496 int cpu = raw_smp_processor_id();
1497 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001498 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001499
Alexander Graf10474ae2009-09-15 11:37:46 +02001500 if (read_cr4() & X86_CR4_VMXE)
1501 return -EBUSY;
1502
Nadav Har'Eld462b812011-05-24 15:26:10 +03001503 INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001505
1506 test_bits = FEATURE_CONTROL_LOCKED;
1507 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1508 if (tboot_enabled())
1509 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1510
1511 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001512 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001513 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1514 }
Rusty Russell66aee912007-07-17 23:34:16 +10001515 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02001516
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001517 if (vmm_exclusive) {
1518 kvm_cpu_vmxon(phys_addr);
1519 ept_sync_global();
1520 }
Alexander Graf10474ae2009-09-15 11:37:46 +02001521
Avi Kivity3444d7d2010-07-26 18:32:38 +03001522 store_gdt(&__get_cpu_var(host_gdt));
1523
Alexander Graf10474ae2009-09-15 11:37:46 +02001524 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001525}
1526
Nadav Har'Eld462b812011-05-24 15:26:10 +03001527static void vmclear_local_loaded_vmcss(void)
Avi Kivity543e4242008-05-13 16:22:47 +03001528{
1529 int cpu = raw_smp_processor_id();
Nadav Har'Eld462b812011-05-24 15:26:10 +03001530 struct loaded_vmcs *v, *n;
Avi Kivity543e4242008-05-13 16:22:47 +03001531
Nadav Har'Eld462b812011-05-24 15:26:10 +03001532 list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
1533 loaded_vmcss_on_cpu_link)
1534 __loaded_vmcs_clear(v);
Avi Kivity543e4242008-05-13 16:22:47 +03001535}
1536
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001537
1538/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1539 * tricks.
1540 */
1541static void kvm_cpu_vmxoff(void)
1542{
1543 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001544}
1545
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546static void hardware_disable(void *garbage)
1547{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001548 if (vmm_exclusive) {
Nadav Har'Eld462b812011-05-24 15:26:10 +03001549 vmclear_local_loaded_vmcss();
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001550 kvm_cpu_vmxoff();
1551 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08001552 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553}
1554
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001555static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001556 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557{
1558 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001559 u32 ctl = ctl_min | ctl_opt;
1560
1561 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1562
1563 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1564 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1565
1566 /* Ensure minimum (required) set of control bits are supported. */
1567 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001568 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001569
1570 *result = ctl;
1571 return 0;
1572}
1573
Avi Kivity110312c2010-12-21 12:54:20 +02001574static __init bool allow_1_setting(u32 msr, u32 ctl)
1575{
1576 u32 vmx_msr_low, vmx_msr_high;
1577
1578 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1579 return vmx_msr_high & ctl;
1580}
1581
Yang, Sheng002c7f72007-07-31 14:23:01 +03001582static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001583{
1584 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001585 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001586 u32 _pin_based_exec_control = 0;
1587 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001588 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001589 u32 _vmexit_control = 0;
1590 u32 _vmentry_control = 0;
1591
1592 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001593 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001594 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1595 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001596 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001597
Anthony Liguori443381a2010-12-06 10:53:38 -06001598 min =
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001599#ifdef CONFIG_X86_64
1600 CPU_BASED_CR8_LOAD_EXITING |
1601 CPU_BASED_CR8_STORE_EXITING |
1602#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001603 CPU_BASED_CR3_LOAD_EXITING |
1604 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001605 CPU_BASED_USE_IO_BITMAPS |
1606 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001607 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001608 CPU_BASED_MWAIT_EXITING |
1609 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001610 CPU_BASED_INVLPG_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001611
1612 if (yield_on_hlt)
1613 min |= CPU_BASED_HLT_EXITING;
1614
Sheng Yangf78e0e22007-10-29 09:40:42 +08001615 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001616 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001617 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001618 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1619 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001620 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001621#ifdef CONFIG_X86_64
1622 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1623 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1624 ~CPU_BASED_CR8_STORE_EXITING;
1625#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001626 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001627 min2 = 0;
1628 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001629 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001630 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001631 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001632 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001633 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1634 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001635 if (adjust_vmx_controls(min2, opt2,
1636 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001637 &_cpu_based_2nd_exec_control) < 0)
1638 return -EIO;
1639 }
1640#ifndef CONFIG_X86_64
1641 if (!(_cpu_based_2nd_exec_control &
1642 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1643 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1644#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001645 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001646 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1647 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001648 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1649 CPU_BASED_CR3_STORE_EXITING |
1650 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001651 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1652 vmx_capability.ept, vmx_capability.vpid);
1653 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001654
1655 min = 0;
1656#ifdef CONFIG_X86_64
1657 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1658#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001659 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001660 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1661 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001662 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001663
Sheng Yang468d4722008-10-09 16:01:55 +08001664 min = 0;
1665 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001666 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1667 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001668 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001669
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001670 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001671
1672 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1673 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001674 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001675
1676#ifdef CONFIG_X86_64
1677 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1678 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001679 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001680#endif
1681
1682 /* Require Write-Back (WB) memory type for VMCS accesses. */
1683 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001684 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001685
Yang, Sheng002c7f72007-07-31 14:23:01 +03001686 vmcs_conf->size = vmx_msr_high & 0x1fff;
1687 vmcs_conf->order = get_order(vmcs_config.size);
1688 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001689
Yang, Sheng002c7f72007-07-31 14:23:01 +03001690 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1691 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001692 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001693 vmcs_conf->vmexit_ctrl = _vmexit_control;
1694 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001695
Avi Kivity110312c2010-12-21 12:54:20 +02001696 cpu_has_load_ia32_efer =
1697 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
1698 VM_ENTRY_LOAD_IA32_EFER)
1699 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
1700 VM_EXIT_LOAD_IA32_EFER);
1701
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001702 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001703}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704
1705static struct vmcs *alloc_vmcs_cpu(int cpu)
1706{
1707 int node = cpu_to_node(cpu);
1708 struct page *pages;
1709 struct vmcs *vmcs;
1710
Mel Gorman6484eb32009-06-16 15:31:54 -07001711 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712 if (!pages)
1713 return NULL;
1714 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001715 memset(vmcs, 0, vmcs_config.size);
1716 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717 return vmcs;
1718}
1719
1720static struct vmcs *alloc_vmcs(void)
1721{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001722 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723}
1724
1725static void free_vmcs(struct vmcs *vmcs)
1726{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001727 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001728}
1729
Nadav Har'Eld462b812011-05-24 15:26:10 +03001730/*
1731 * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
1732 */
1733static void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
1734{
1735 if (!loaded_vmcs->vmcs)
1736 return;
1737 loaded_vmcs_clear(loaded_vmcs);
1738 free_vmcs(loaded_vmcs->vmcs);
1739 loaded_vmcs->vmcs = NULL;
1740}
1741
Sam Ravnborg39959582007-06-01 00:47:13 -07001742static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743{
1744 int cpu;
1745
Zachary Amsden3230bb42009-09-29 11:38:37 -10001746 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001748 per_cpu(vmxarea, cpu) = NULL;
1749 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750}
1751
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752static __init int alloc_kvm_area(void)
1753{
1754 int cpu;
1755
Zachary Amsden3230bb42009-09-29 11:38:37 -10001756 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757 struct vmcs *vmcs;
1758
1759 vmcs = alloc_vmcs_cpu(cpu);
1760 if (!vmcs) {
1761 free_kvm_area();
1762 return -ENOMEM;
1763 }
1764
1765 per_cpu(vmxarea, cpu) = vmcs;
1766 }
1767 return 0;
1768}
1769
1770static __init int hardware_setup(void)
1771{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001772 if (setup_vmcs_config(&vmcs_config) < 0)
1773 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001774
1775 if (boot_cpu_has(X86_FEATURE_NX))
1776 kvm_enable_efer_bits(EFER_NX);
1777
Sheng Yang93ba03c2009-04-01 15:52:32 +08001778 if (!cpu_has_vmx_vpid())
1779 enable_vpid = 0;
1780
Sheng Yang4bc9b982010-06-02 14:05:24 +08001781 if (!cpu_has_vmx_ept() ||
1782 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001783 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001784 enable_unrestricted_guest = 0;
1785 }
1786
1787 if (!cpu_has_vmx_unrestricted_guest())
1788 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001789
1790 if (!cpu_has_vmx_flexpriority())
1791 flexpriority_enabled = 0;
1792
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001793 if (!cpu_has_vmx_tpr_shadow())
1794 kvm_x86_ops->update_cr8_intercept = NULL;
1795
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001796 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1797 kvm_disable_largepages();
1798
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001799 if (!cpu_has_vmx_ple())
1800 ple_gap = 0;
1801
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 return alloc_kvm_area();
1803}
1804
1805static __exit void hardware_unsetup(void)
1806{
1807 free_kvm_area();
1808}
1809
Avi Kivity6aa8b732006-12-10 02:21:36 -08001810static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1811{
1812 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1813
Avi Kivity6af11b92007-03-19 13:18:10 +02001814 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001815 vmcs_write16(sf->selector, save->selector);
1816 vmcs_writel(sf->base, save->base);
1817 vmcs_write32(sf->limit, save->limit);
1818 vmcs_write32(sf->ar_bytes, save->ar);
1819 } else {
1820 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1821 << AR_DPL_SHIFT;
1822 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1823 }
1824}
1825
1826static void enter_pmode(struct kvm_vcpu *vcpu)
1827{
1828 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001829 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001831 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001832 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833
Avi Kivity2fb92db2011-04-27 19:42:18 +03001834 vmx_segment_cache_clear(vmx);
1835
Avi Kivityd0ba64f2011-01-03 14:28:51 +02001836 vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001837 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1838 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1839 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840
1841 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001842 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1843 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844 vmcs_writel(GUEST_RFLAGS, flags);
1845
Rusty Russell66aee912007-07-17 23:34:16 +10001846 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1847 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848
1849 update_exception_bitmap(vcpu);
1850
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001851 if (emulate_invalid_guest_state)
1852 return;
1853
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001854 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1855 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1856 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1857 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858
Avi Kivity2fb92db2011-04-27 19:42:18 +03001859 vmx_segment_cache_clear(vmx);
1860
Avi Kivity6aa8b732006-12-10 02:21:36 -08001861 vmcs_write16(GUEST_SS_SELECTOR, 0);
1862 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1863
1864 vmcs_write16(GUEST_CS_SELECTOR,
1865 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1866 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1867}
1868
Mike Dayd77c26f2007-10-08 09:02:08 -04001869static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001871 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001872 struct kvm_memslots *slots;
1873 gfn_t base_gfn;
1874
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001875 slots = kvm_memslots(kvm);
Avi Kivityf495c6e2010-06-10 17:21:29 +03001876 base_gfn = slots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001877 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02001878 return base_gfn << PAGE_SHIFT;
1879 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001880 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001881}
1882
1883static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1884{
1885 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1886
1887 save->selector = vmcs_read16(sf->selector);
1888 save->base = vmcs_readl(sf->base);
1889 save->limit = vmcs_read32(sf->limit);
1890 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001891 vmcs_write16(sf->selector, save->base >> 4);
Gleb Natapov444e8632010-12-27 17:25:04 +02001892 vmcs_write32(sf->base, save->base & 0xffff0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 vmcs_write32(sf->limit, 0xffff);
1894 vmcs_write32(sf->ar_bytes, 0xf3);
Gleb Natapov444e8632010-12-27 17:25:04 +02001895 if (save->base & 0xf)
1896 printk_once(KERN_WARNING "kvm: segment base is not paragraph"
1897 " aligned when entering protected mode (seg=%d)",
1898 seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899}
1900
1901static void enter_rmode(struct kvm_vcpu *vcpu)
1902{
1903 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001904 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001906 if (enable_unrestricted_guest)
1907 return;
1908
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001909 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001910 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001911
Gleb Natapov776e58e2011-03-13 12:34:27 +02001912 /*
1913 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
1914 * vcpu. Call it here with phys address pointing 16M below 4G.
1915 */
1916 if (!vcpu->kvm->arch.tss_addr) {
1917 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
1918 "called before entering vcpu\n");
1919 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
1920 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
1921 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1922 }
1923
Avi Kivity2fb92db2011-04-27 19:42:18 +03001924 vmx_segment_cache_clear(vmx);
1925
Avi Kivityd0ba64f2011-01-03 14:28:51 +02001926 vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001927 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001928 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1929
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001930 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001931 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1932
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001933 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001934 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1935
1936 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001937 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001938
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001939 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001940
1941 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001942 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943 update_exception_bitmap(vcpu);
1944
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001945 if (emulate_invalid_guest_state)
1946 goto continue_rmode;
1947
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1949 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1950 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1951
1952 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001953 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001954 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1955 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1957
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001958 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1959 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1960 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1961 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001962
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001963continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001964 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001965}
1966
Amit Shah401d10d2009-02-20 22:53:37 +05301967static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1968{
1969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001970 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1971
1972 if (!msr)
1973 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301974
Avi Kivity44ea2b12009-09-06 15:55:37 +03001975 /*
1976 * Force kernel_gs_base reloading before EFER changes, as control
1977 * of this msr depends on is_long_mode().
1978 */
1979 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02001980 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05301981 if (efer & EFER_LMA) {
1982 vmcs_write32(VM_ENTRY_CONTROLS,
1983 vmcs_read32(VM_ENTRY_CONTROLS) |
1984 VM_ENTRY_IA32E_MODE);
1985 msr->data = efer;
1986 } else {
1987 vmcs_write32(VM_ENTRY_CONTROLS,
1988 vmcs_read32(VM_ENTRY_CONTROLS) &
1989 ~VM_ENTRY_IA32E_MODE);
1990
1991 msr->data = efer & ~EFER_LME;
1992 }
1993 setup_msrs(vmx);
1994}
1995
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001996#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001997
1998static void enter_lmode(struct kvm_vcpu *vcpu)
1999{
2000 u32 guest_tr_ar;
2001
Avi Kivity2fb92db2011-04-27 19:42:18 +03002002 vmx_segment_cache_clear(to_vmx(vcpu));
2003
Avi Kivity6aa8b732006-12-10 02:21:36 -08002004 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
2005 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
2006 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002007 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008 vmcs_write32(GUEST_TR_AR_BYTES,
2009 (guest_tr_ar & ~AR_TYPE_MASK)
2010 | AR_TYPE_BUSY_64_TSS);
2011 }
Avi Kivityda38f432010-07-06 11:30:49 +03002012 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002013}
2014
2015static void exit_lmode(struct kvm_vcpu *vcpu)
2016{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002017 vmcs_write32(VM_ENTRY_CONTROLS,
2018 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03002019 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03002020 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002021}
2022
2023#endif
2024
Sheng Yang2384d2b2008-01-17 15:14:33 +08002025static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
2026{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002027 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002028 if (enable_ept) {
2029 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
2030 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08002031 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08002032 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08002033}
2034
Avi Kivitye8467fd2009-12-29 18:43:06 +02002035static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
2036{
2037 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
2038
2039 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
2040 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2041}
2042
Avi Kivityaff48ba2010-12-05 18:56:11 +02002043static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2044{
2045 if (enable_ept && is_paging(vcpu))
2046 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2047 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2048}
2049
Anthony Liguori25c4c272007-04-27 09:29:21 +03002050static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002051{
Avi Kivityfc78f512009-12-07 12:16:48 +02002052 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2053
2054 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2055 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002056}
2057
Sheng Yang14394422008-04-28 12:24:45 +08002058static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2059{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002060 if (!test_bit(VCPU_EXREG_PDPTR,
2061 (unsigned long *)&vcpu->arch.regs_dirty))
2062 return;
2063
Sheng Yang14394422008-04-28 12:24:45 +08002064 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002065 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
2066 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
2067 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
2068 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002069 }
2070}
2071
Avi Kivity8f5d5492009-05-31 18:41:29 +03002072static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
2073{
2074 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002075 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2076 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2077 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2078 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002079 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002080
2081 __set_bit(VCPU_EXREG_PDPTR,
2082 (unsigned long *)&vcpu->arch.regs_avail);
2083 __set_bit(VCPU_EXREG_PDPTR,
2084 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002085}
2086
Sheng Yang14394422008-04-28 12:24:45 +08002087static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
2088
2089static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2090 unsigned long cr0,
2091 struct kvm_vcpu *vcpu)
2092{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002093 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2094 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002095 if (!(cr0 & X86_CR0_PG)) {
2096 /* From paging/starting to nonpaging */
2097 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002098 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002099 (CPU_BASED_CR3_LOAD_EXITING |
2100 CPU_BASED_CR3_STORE_EXITING));
2101 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002102 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002103 } else if (!is_paging(vcpu)) {
2104 /* From nonpaging to paging */
2105 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002106 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002107 ~(CPU_BASED_CR3_LOAD_EXITING |
2108 CPU_BASED_CR3_STORE_EXITING));
2109 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002110 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002111 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002112
2113 if (!(cr0 & X86_CR0_WP))
2114 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002115}
2116
Avi Kivity6aa8b732006-12-10 02:21:36 -08002117static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2118{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002119 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002120 unsigned long hw_cr0;
2121
2122 if (enable_unrestricted_guest)
2123 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
2124 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
2125 else
2126 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002127
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002128 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129 enter_pmode(vcpu);
2130
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002131 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132 enter_rmode(vcpu);
2133
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002134#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002135 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10002136 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10002138 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002139 exit_lmode(vcpu);
2140 }
2141#endif
2142
Avi Kivity089d0342009-03-23 18:26:32 +02002143 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002144 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2145
Avi Kivity02daab22009-12-30 12:40:26 +02002146 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02002147 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02002148
Avi Kivity6aa8b732006-12-10 02:21:36 -08002149 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002150 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002151 vcpu->arch.cr0 = cr0;
Avi Kivity69c73022011-03-07 15:26:44 +02002152 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
Sheng Yang14394422008-04-28 12:24:45 +08002155static u64 construct_eptp(unsigned long root_hpa)
2156{
2157 u64 eptp;
2158
2159 /* TODO write the value reading from MSR */
2160 eptp = VMX_EPT_DEFAULT_MT |
2161 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
2162 eptp |= (root_hpa & PAGE_MASK);
2163
2164 return eptp;
2165}
2166
Avi Kivity6aa8b732006-12-10 02:21:36 -08002167static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
2168{
Sheng Yang14394422008-04-28 12:24:45 +08002169 unsigned long guest_cr3;
2170 u64 eptp;
2171
2172 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002173 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08002174 eptp = construct_eptp(cr3);
2175 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002176 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08002177 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02002178 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002179 }
2180
Sheng Yang2384d2b2008-01-17 15:14:33 +08002181 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002182 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002183}
2184
2185static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
2186{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002187 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08002188 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
2189
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002190 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02002191 if (enable_ept) {
2192 if (!is_paging(vcpu)) {
2193 hw_cr4 &= ~X86_CR4_PAE;
2194 hw_cr4 |= X86_CR4_PSE;
2195 } else if (!(cr4 & X86_CR4_PAE)) {
2196 hw_cr4 &= ~X86_CR4_PAE;
2197 }
2198 }
Sheng Yang14394422008-04-28 12:24:45 +08002199
2200 vmcs_writel(CR4_READ_SHADOW, cr4);
2201 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002202}
2203
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204static void vmx_get_segment(struct kvm_vcpu *vcpu,
2205 struct kvm_segment *var, int seg)
2206{
Avi Kivitya9179492011-01-03 14:28:52 +02002207 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitya9179492011-01-03 14:28:52 +02002208 struct kvm_save_segment *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002209 u32 ar;
2210
Avi Kivitya9179492011-01-03 14:28:52 +02002211 if (vmx->rmode.vm86_active
2212 && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
2213 || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
2214 || seg == VCPU_SREG_GS)
2215 && !emulate_invalid_guest_state) {
2216 switch (seg) {
2217 case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
2218 case VCPU_SREG_ES: save = &vmx->rmode.es; break;
2219 case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
2220 case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
2221 case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
2222 default: BUG();
2223 }
2224 var->selector = save->selector;
2225 var->base = save->base;
2226 var->limit = save->limit;
2227 ar = save->ar;
2228 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002229 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivitya9179492011-01-03 14:28:52 +02002230 goto use_saved_rmode_seg;
2231 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002232 var->base = vmx_read_guest_seg_base(vmx, seg);
2233 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2234 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2235 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002236use_saved_rmode_seg:
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02002237 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002238 ar = 0;
2239 var->type = ar & 15;
2240 var->s = (ar >> 4) & 1;
2241 var->dpl = (ar >> 5) & 3;
2242 var->present = (ar >> 7) & 1;
2243 var->avl = (ar >> 12) & 1;
2244 var->l = (ar >> 13) & 1;
2245 var->db = (ar >> 14) & 1;
2246 var->g = (ar >> 15) & 1;
2247 var->unusable = (ar >> 16) & 1;
2248}
2249
Avi Kivitya9179492011-01-03 14:28:52 +02002250static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2251{
Avi Kivitya9179492011-01-03 14:28:52 +02002252 struct kvm_segment s;
2253
2254 if (to_vmx(vcpu)->rmode.vm86_active) {
2255 vmx_get_segment(vcpu, &s, seg);
2256 return s.base;
2257 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002258 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002259}
2260
Avi Kivity69c73022011-03-07 15:26:44 +02002261static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002262{
Avi Kivity3eeb3282010-01-21 15:31:48 +02002263 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002264 return 0;
2265
Avi Kivityf4c63e52011-03-07 14:54:28 +02002266 if (!is_long_mode(vcpu)
2267 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02002268 return 3;
2269
Avi Kivity2fb92db2011-04-27 19:42:18 +03002270 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02002271}
2272
Avi Kivity69c73022011-03-07 15:26:44 +02002273static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2274{
2275 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
2276 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2277 to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
2278 }
2279 return to_vmx(vcpu)->cpl;
2280}
2281
2282
Avi Kivity653e3102007-05-07 10:55:37 +03002283static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002284{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002285 u32 ar;
2286
Avi Kivity653e3102007-05-07 10:55:37 +03002287 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 ar = 1 << 16;
2289 else {
2290 ar = var->type & 15;
2291 ar |= (var->s & 1) << 4;
2292 ar |= (var->dpl & 3) << 5;
2293 ar |= (var->present & 1) << 7;
2294 ar |= (var->avl & 1) << 12;
2295 ar |= (var->l & 1) << 13;
2296 ar |= (var->db & 1) << 14;
2297 ar |= (var->g & 1) << 15;
2298 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08002299 if (ar == 0) /* a 0 value means unusable */
2300 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03002301
2302 return ar;
2303}
2304
2305static void vmx_set_segment(struct kvm_vcpu *vcpu,
2306 struct kvm_segment *var, int seg)
2307{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002308 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03002309 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2310 u32 ar;
2311
Avi Kivity2fb92db2011-04-27 19:42:18 +03002312 vmx_segment_cache_clear(vmx);
2313
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002314 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
Gleb Natapova8ba6c22011-02-21 12:07:58 +02002315 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002316 vmx->rmode.tr.selector = var->selector;
2317 vmx->rmode.tr.base = var->base;
2318 vmx->rmode.tr.limit = var->limit;
2319 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03002320 return;
2321 }
2322 vmcs_writel(sf->base, var->base);
2323 vmcs_write32(sf->limit, var->limit);
2324 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002325 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03002326 /*
2327 * Hack real-mode segments into vm86 compatibility.
2328 */
2329 if (var->base == 0xffff0000 && var->selector == 0xf000)
2330 vmcs_writel(sf->base, 0xf0000);
2331 ar = 0xf3;
2332 } else
2333 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002334
2335 /*
2336 * Fix the "Accessed" bit in AR field of segment registers for older
2337 * qemu binaries.
2338 * IA32 arch specifies that at the time of processor reset the
2339 * "Accessed" bit in the AR field of segment registers is 1. And qemu
2340 * is setting it to 0 in the usedland code. This causes invalid guest
2341 * state vmexit when "unrestricted guest" mode is turned on.
2342 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2343 * tree. Newer qemu binaries with that qemu fix would not need this
2344 * kvm hack.
2345 */
2346 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
2347 ar |= 0x1; /* Accessed */
2348
Avi Kivity6aa8b732006-12-10 02:21:36 -08002349 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002350 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002351}
2352
Avi Kivity6aa8b732006-12-10 02:21:36 -08002353static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2354{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002355 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002356
2357 *db = (ar >> 14) & 1;
2358 *l = (ar >> 13) & 1;
2359}
2360
Gleb Natapov89a27f42010-02-16 10:51:48 +02002361static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002362{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002363 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2364 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002365}
2366
Gleb Natapov89a27f42010-02-16 10:51:48 +02002367static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002368{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002369 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2370 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002371}
2372
Gleb Natapov89a27f42010-02-16 10:51:48 +02002373static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002375 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2376 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377}
2378
Gleb Natapov89a27f42010-02-16 10:51:48 +02002379static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002381 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2382 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002383}
2384
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002385static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2386{
2387 struct kvm_segment var;
2388 u32 ar;
2389
2390 vmx_get_segment(vcpu, &var, seg);
2391 ar = vmx_segment_access_rights(&var);
2392
2393 if (var.base != (var.selector << 4))
2394 return false;
2395 if (var.limit != 0xffff)
2396 return false;
2397 if (ar != 0xf3)
2398 return false;
2399
2400 return true;
2401}
2402
2403static bool code_segment_valid(struct kvm_vcpu *vcpu)
2404{
2405 struct kvm_segment cs;
2406 unsigned int cs_rpl;
2407
2408 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2409 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2410
Avi Kivity1872a3f2009-01-04 23:26:52 +02002411 if (cs.unusable)
2412 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002413 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2414 return false;
2415 if (!cs.s)
2416 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002417 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002418 if (cs.dpl > cs_rpl)
2419 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002420 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002421 if (cs.dpl != cs_rpl)
2422 return false;
2423 }
2424 if (!cs.present)
2425 return false;
2426
2427 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2428 return true;
2429}
2430
2431static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2432{
2433 struct kvm_segment ss;
2434 unsigned int ss_rpl;
2435
2436 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2437 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2438
Avi Kivity1872a3f2009-01-04 23:26:52 +02002439 if (ss.unusable)
2440 return true;
2441 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002442 return false;
2443 if (!ss.s)
2444 return false;
2445 if (ss.dpl != ss_rpl) /* DPL != RPL */
2446 return false;
2447 if (!ss.present)
2448 return false;
2449
2450 return true;
2451}
2452
2453static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2454{
2455 struct kvm_segment var;
2456 unsigned int rpl;
2457
2458 vmx_get_segment(vcpu, &var, seg);
2459 rpl = var.selector & SELECTOR_RPL_MASK;
2460
Avi Kivity1872a3f2009-01-04 23:26:52 +02002461 if (var.unusable)
2462 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002463 if (!var.s)
2464 return false;
2465 if (!var.present)
2466 return false;
2467 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2468 if (var.dpl < rpl) /* DPL < RPL */
2469 return false;
2470 }
2471
2472 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2473 * rights flags
2474 */
2475 return true;
2476}
2477
2478static bool tr_valid(struct kvm_vcpu *vcpu)
2479{
2480 struct kvm_segment tr;
2481
2482 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2483
Avi Kivity1872a3f2009-01-04 23:26:52 +02002484 if (tr.unusable)
2485 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002486 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2487 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002488 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002489 return false;
2490 if (!tr.present)
2491 return false;
2492
2493 return true;
2494}
2495
2496static bool ldtr_valid(struct kvm_vcpu *vcpu)
2497{
2498 struct kvm_segment ldtr;
2499
2500 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2501
Avi Kivity1872a3f2009-01-04 23:26:52 +02002502 if (ldtr.unusable)
2503 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002504 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2505 return false;
2506 if (ldtr.type != 2)
2507 return false;
2508 if (!ldtr.present)
2509 return false;
2510
2511 return true;
2512}
2513
2514static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2515{
2516 struct kvm_segment cs, ss;
2517
2518 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2519 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2520
2521 return ((cs.selector & SELECTOR_RPL_MASK) ==
2522 (ss.selector & SELECTOR_RPL_MASK));
2523}
2524
2525/*
2526 * Check if guest state is valid. Returns true if valid, false if
2527 * not.
2528 * We assume that registers are always usable
2529 */
2530static bool guest_state_valid(struct kvm_vcpu *vcpu)
2531{
2532 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02002533 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002534 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2535 return false;
2536 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2537 return false;
2538 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2539 return false;
2540 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2541 return false;
2542 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2543 return false;
2544 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2545 return false;
2546 } else {
2547 /* protected mode guest state checks */
2548 if (!cs_ss_rpl_check(vcpu))
2549 return false;
2550 if (!code_segment_valid(vcpu))
2551 return false;
2552 if (!stack_segment_valid(vcpu))
2553 return false;
2554 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2555 return false;
2556 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2557 return false;
2558 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2559 return false;
2560 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2561 return false;
2562 if (!tr_valid(vcpu))
2563 return false;
2564 if (!ldtr_valid(vcpu))
2565 return false;
2566 }
2567 /* TODO:
2568 * - Add checks on RIP
2569 * - Add checks on RFLAGS
2570 */
2571
2572 return true;
2573}
2574
Mike Dayd77c26f2007-10-08 09:02:08 -04002575static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002576{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002577 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02002578 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002579 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002581 idx = srcu_read_lock(&kvm->srcu);
2582 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002583 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2584 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002585 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002586 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002587 r = kvm_write_guest_page(kvm, fn++, &data,
2588 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002589 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002590 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002591 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2592 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002593 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002594 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2595 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002596 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002597 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002598 r = kvm_write_guest_page(kvm, fn, &data,
2599 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2600 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002601 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002602 goto out;
2603
2604 ret = 1;
2605out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002606 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002607 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002608}
2609
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002610static int init_rmode_identity_map(struct kvm *kvm)
2611{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002612 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002613 pfn_t identity_map_pfn;
2614 u32 tmp;
2615
Avi Kivity089d0342009-03-23 18:26:32 +02002616 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002617 return 1;
2618 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2619 printk(KERN_ERR "EPT: identity-mapping pagetable "
2620 "haven't been allocated!\n");
2621 return 0;
2622 }
2623 if (likely(kvm->arch.ept_identity_pagetable_done))
2624 return 1;
2625 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002626 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002627 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002628 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2629 if (r < 0)
2630 goto out;
2631 /* Set up identity-mapping pagetable for EPT in real mode */
2632 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2633 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2634 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2635 r = kvm_write_guest_page(kvm, identity_map_pfn,
2636 &tmp, i * sizeof(tmp), sizeof(tmp));
2637 if (r < 0)
2638 goto out;
2639 }
2640 kvm->arch.ept_identity_pagetable_done = true;
2641 ret = 1;
2642out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002643 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002644 return ret;
2645}
2646
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647static void seg_setup(int seg)
2648{
2649 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002650 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002651
2652 vmcs_write16(sf->selector, 0);
2653 vmcs_writel(sf->base, 0);
2654 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002655 if (enable_unrestricted_guest) {
2656 ar = 0x93;
2657 if (seg == VCPU_SREG_CS)
2658 ar |= 0x08; /* code segment */
2659 } else
2660 ar = 0xf3;
2661
2662 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002663}
2664
Sheng Yangf78e0e22007-10-29 09:40:42 +08002665static int alloc_apic_access_page(struct kvm *kvm)
2666{
2667 struct kvm_userspace_memory_region kvm_userspace_mem;
2668 int r = 0;
2669
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002670 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002671 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002672 goto out;
2673 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2674 kvm_userspace_mem.flags = 0;
2675 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2676 kvm_userspace_mem.memory_size = PAGE_SIZE;
2677 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2678 if (r)
2679 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002680
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002681 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002682out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002683 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002684 return r;
2685}
2686
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002687static int alloc_identity_pagetable(struct kvm *kvm)
2688{
2689 struct kvm_userspace_memory_region kvm_userspace_mem;
2690 int r = 0;
2691
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002692 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002693 if (kvm->arch.ept_identity_pagetable)
2694 goto out;
2695 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2696 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002697 kvm_userspace_mem.guest_phys_addr =
2698 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002699 kvm_userspace_mem.memory_size = PAGE_SIZE;
2700 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2701 if (r)
2702 goto out;
2703
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002704 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002705 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002706out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002707 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002708 return r;
2709}
2710
Sheng Yang2384d2b2008-01-17 15:14:33 +08002711static void allocate_vpid(struct vcpu_vmx *vmx)
2712{
2713 int vpid;
2714
2715 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002716 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002717 return;
2718 spin_lock(&vmx_vpid_lock);
2719 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2720 if (vpid < VMX_NR_VPIDS) {
2721 vmx->vpid = vpid;
2722 __set_bit(vpid, vmx_vpid_bitmap);
2723 }
2724 spin_unlock(&vmx_vpid_lock);
2725}
2726
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08002727static void free_vpid(struct vcpu_vmx *vmx)
2728{
2729 if (!enable_vpid)
2730 return;
2731 spin_lock(&vmx_vpid_lock);
2732 if (vmx->vpid != 0)
2733 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2734 spin_unlock(&vmx_vpid_lock);
2735}
2736
Avi Kivity58972972009-02-24 22:26:47 +02002737static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002738{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002739 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002740
2741 if (!cpu_has_vmx_msr_bitmap())
2742 return;
2743
2744 /*
2745 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2746 * have the write-low and read-high bitmap offsets the wrong way round.
2747 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2748 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002749 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002750 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2751 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002752 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2753 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002754 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2755 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002756 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002757}
2758
Avi Kivity58972972009-02-24 22:26:47 +02002759static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2760{
2761 if (!longmode_only)
2762 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2763 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2764}
2765
Avi Kivity6aa8b732006-12-10 02:21:36 -08002766/*
2767 * Sets up the vmcs for emulated real mode.
2768 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002769static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770{
Sheng Yang468d4722008-10-09 16:01:55 +08002771 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002772 u32 junk;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002773 u64 host_pat;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 unsigned long a;
Gleb Natapov89a27f42010-02-16 10:51:48 +02002775 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002777 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002778 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779
Avi Kivity6aa8b732006-12-10 02:21:36 -08002780 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002781 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2782 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783
Sheng Yang25c5f222008-03-28 13:18:56 +08002784 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002785 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002786
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2788
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002790 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2791 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002792
2793 exec_control = vmcs_config.cpu_based_exec_ctrl;
2794 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2795 exec_control &= ~CPU_BASED_TPR_SHADOW;
2796#ifdef CONFIG_X86_64
2797 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2798 CPU_BASED_CR8_LOAD_EXITING;
2799#endif
2800 }
Avi Kivity089d0342009-03-23 18:26:32 +02002801 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002802 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002803 CPU_BASED_CR3_LOAD_EXITING |
2804 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002805 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002806
Sheng Yang83ff3b92007-11-21 14:33:25 +08002807 if (cpu_has_secondary_exec_ctrls()) {
2808 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2809 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2810 exec_control &=
2811 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002812 if (vmx->vpid == 0)
2813 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002814 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002815 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002816 enable_unrestricted_guest = 0;
2817 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002818 if (!enable_unrestricted_guest)
2819 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002820 if (!ple_gap)
2821 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002822 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2823 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002824
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002825 if (ple_gap) {
2826 vmcs_write32(PLE_GAP, ple_gap);
2827 vmcs_write32(PLE_WINDOW, ple_window);
2828 }
2829
Avi Kivityc7addb92007-09-16 18:58:32 +02002830 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2831 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2833
Avi Kivity1c11e712010-05-03 16:05:44 +03002834 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002835 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2836 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2837
2838 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2839 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2840 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity9581d442010-10-19 16:46:55 +02002841 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
2842 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002843 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002844#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 rdmsrl(MSR_FS_BASE, a);
2846 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2847 rdmsrl(MSR_GS_BASE, a);
2848 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2849#else
2850 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2851 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2852#endif
2853
2854 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2855
Wei Yongjunec687982010-03-05 12:11:48 +08002856 native_store_idt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02002857 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858
Mike Dayd77c26f2007-10-08 09:02:08 -04002859 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002860 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002861 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2862 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002863 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03002864 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002865 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866
2867 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2868 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2869 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2870 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2871 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2872 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2873
Sheng Yang468d4722008-10-09 16:01:55 +08002874 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2875 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2876 host_pat = msr_low | ((u64) msr_high << 32);
2877 vmcs_write64(HOST_IA32_PAT, host_pat);
2878 }
2879 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2880 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2881 host_pat = msr_low | ((u64) msr_high << 32);
2882 /* Write the default value follow host pat */
2883 vmcs_write64(GUEST_IA32_PAT, host_pat);
2884 /* Keep arch.pat sync with GUEST_IA32_PAT */
2885 vmx->vcpu.arch.pat = host_pat;
2886 }
2887
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 for (i = 0; i < NR_VMX_MSR; ++i) {
2889 u32 index = vmx_msr_index[i];
2890 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002891 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002892
2893 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2894 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002895 if (wrmsr_safe(index, data_low, data_high) < 0)
2896 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03002897 vmx->guest_msrs[j].index = i;
2898 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002899 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002900 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002903 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904
2905 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002906 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2907
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002908 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02002909 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02002910 if (enable_ept)
2911 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02002912 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002913
Zachary Amsden99e3e302010-08-19 22:07:17 -10002914 kvm_write_tsc(&vmx->vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002915
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002916 return 0;
2917}
2918
2919static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2920{
2921 struct vcpu_vmx *vmx = to_vmx(vcpu);
2922 u64 msr;
Xiao Guangrong4b9d3a02010-06-08 10:15:51 +08002923 int ret;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002924
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002925 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002926
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002927 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002928
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002929 vmx->soft_vnmi_blocked = 0;
2930
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002931 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002932 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002933 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002934 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002935 msr |= MSR_IA32_APICBASE_BSP;
2936 kvm_set_apic_base(&vmx->vcpu, msr);
2937
Jan Kiszka10ab25c2010-05-25 16:01:50 +02002938 ret = fx_init(&vmx->vcpu);
2939 if (ret != 0)
2940 goto out;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002941
Avi Kivity2fb92db2011-04-27 19:42:18 +03002942 vmx_segment_cache_clear(vmx);
2943
Avi Kivity5706be02008-08-20 15:07:31 +03002944 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002945 /*
2946 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2947 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2948 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002949 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002950 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2951 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2952 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002953 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2954 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002955 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002956
2957 seg_setup(VCPU_SREG_DS);
2958 seg_setup(VCPU_SREG_ES);
2959 seg_setup(VCPU_SREG_FS);
2960 seg_setup(VCPU_SREG_GS);
2961 seg_setup(VCPU_SREG_SS);
2962
2963 vmcs_write16(GUEST_TR_SELECTOR, 0);
2964 vmcs_writel(GUEST_TR_BASE, 0);
2965 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2966 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2967
2968 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2969 vmcs_writel(GUEST_LDTR_BASE, 0);
2970 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2971 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2972
2973 vmcs_write32(GUEST_SYSENTER_CS, 0);
2974 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2975 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2976
2977 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002978 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002979 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002980 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002981 kvm_rip_write(vcpu, 0);
2982 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002983
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002984 vmcs_writel(GUEST_DR7, 0x400);
2985
2986 vmcs_writel(GUEST_GDTR_BASE, 0);
2987 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2988
2989 vmcs_writel(GUEST_IDTR_BASE, 0);
2990 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2991
Anthony Liguori443381a2010-12-06 10:53:38 -06002992 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002993 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2994 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2995
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002996 /* Special registers */
2997 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2998
2999 setup_msrs(vmx);
3000
Avi Kivity6aa8b732006-12-10 02:21:36 -08003001 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
3002
Sheng Yangf78e0e22007-10-29 09:40:42 +08003003 if (cpu_has_vmx_tpr_shadow()) {
3004 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
3005 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
3006 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09003007 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08003008 vmcs_write32(TPR_THRESHOLD, 0);
3009 }
3010
3011 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
3012 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003013 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08003014
Sheng Yang2384d2b2008-01-17 15:14:33 +08003015 if (vmx->vpid != 0)
3016 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
3017
Eduardo Habkostfa400522009-10-24 02:49:58 -02003018 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003019 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10003020 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003021 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003022 vmx_fpu_activate(&vmx->vcpu);
3023 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024
Gui Jianfengb9d762f2010-06-07 10:32:29 +08003025 vpid_sync_context(vmx);
Sheng Yang2384d2b2008-01-17 15:14:33 +08003026
Marcelo Tosatti3200f402008-03-29 20:17:59 -03003027 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003028
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003029 /* HACK: Don't enable emulation on guest boot/reset */
3030 vmx->emulation_required = 0;
3031
Avi Kivity6aa8b732006-12-10 02:21:36 -08003032out:
3033 return ret;
3034}
3035
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003036static void enable_irq_window(struct kvm_vcpu *vcpu)
3037{
3038 u32 cpu_based_vm_exec_control;
3039
3040 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3041 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
3042 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3043}
3044
3045static void enable_nmi_window(struct kvm_vcpu *vcpu)
3046{
3047 u32 cpu_based_vm_exec_control;
3048
3049 if (!cpu_has_virtual_nmis()) {
3050 enable_irq_window(vcpu);
3051 return;
3052 }
3053
Avi Kivity30bd0c42010-11-01 23:20:48 +02003054 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
3055 enable_irq_window(vcpu);
3056 return;
3057 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003058 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3059 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
3060 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3061}
3062
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003063static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003064{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003066 uint32_t intr;
3067 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003068
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003069 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003070
Avi Kivityfa89a812008-09-01 15:57:51 +03003071 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003072 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003073 int inc_eip = 0;
3074 if (vcpu->arch.interrupt.soft)
3075 inc_eip = vcpu->arch.event_exit_inst_len;
3076 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003077 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003078 return;
3079 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003080 intr = irq | INTR_INFO_VALID_MASK;
3081 if (vcpu->arch.interrupt.soft) {
3082 intr |= INTR_TYPE_SOFT_INTR;
3083 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3084 vmx->vcpu.arch.event_exit_inst_len);
3085 } else
3086 intr |= INTR_TYPE_EXT_INTR;
3087 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Anthony Liguori443381a2010-12-06 10:53:38 -06003088 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003089}
3090
Sheng Yangf08864b2008-05-15 18:23:25 +08003091static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3092{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003093 struct vcpu_vmx *vmx = to_vmx(vcpu);
3094
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003095 if (!cpu_has_virtual_nmis()) {
3096 /*
3097 * Tracking the NMI-blocked state in software is built upon
3098 * finding the next open IRQ window. This, in turn, depends on
3099 * well-behaving guests: They have to keep IRQs disabled at
3100 * least as long as the NMI handler runs. Otherwise we may
3101 * cause NMI nesting, maybe breaking the guest. But as this is
3102 * highly unlikely, we can live with the residual risk.
3103 */
3104 vmx->soft_vnmi_blocked = 1;
3105 vmx->vnmi_blocked_time = 0;
3106 }
3107
Jan Kiszka487b3912008-09-26 09:30:56 +02003108 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02003109 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003110 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003111 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003112 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003113 return;
3114 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003115 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3116 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Anthony Liguori443381a2010-12-06 10:53:38 -06003117 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003118}
3119
Gleb Natapovc4282df2009-04-21 17:45:07 +03003120static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02003121{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003122 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03003123 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02003124
Gleb Natapovc4282df2009-04-21 17:45:07 +03003125 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02003126 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
3127 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02003128}
3129
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003130static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
3131{
3132 if (!cpu_has_virtual_nmis())
3133 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02003134 if (to_vmx(vcpu)->nmi_known_unmasked)
3135 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03003136 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003137}
3138
3139static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3140{
3141 struct vcpu_vmx *vmx = to_vmx(vcpu);
3142
3143 if (!cpu_has_virtual_nmis()) {
3144 if (vmx->soft_vnmi_blocked != masked) {
3145 vmx->soft_vnmi_blocked = masked;
3146 vmx->vnmi_blocked_time = 0;
3147 }
3148 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02003149 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003150 if (masked)
3151 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3152 GUEST_INTR_STATE_NMI);
3153 else
3154 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3155 GUEST_INTR_STATE_NMI);
3156 }
3157}
3158
Gleb Natapov78646122009-03-23 12:12:11 +02003159static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
3160{
Gleb Natapovc4282df2009-04-21 17:45:07 +03003161 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
3162 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3163 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02003164}
3165
Izik Eiduscbc94022007-10-25 00:29:55 +02003166static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
3167{
3168 int ret;
3169 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08003170 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02003171 .guest_phys_addr = addr,
3172 .memory_size = PAGE_SIZE * 3,
3173 .flags = 0,
3174 };
3175
3176 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
3177 if (ret)
3178 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003179 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02003180 if (!init_rmode_tss(kvm))
3181 return -ENOMEM;
3182
Izik Eiduscbc94022007-10-25 00:29:55 +02003183 return 0;
3184}
3185
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186static int handle_rmode_exception(struct kvm_vcpu *vcpu,
3187 int vec, u32 err_code)
3188{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03003189 /*
3190 * Instruction with address size override prefix opcode 0x67
3191 * Cause the #SS fault with 0 error code in VM86 mode.
3192 */
3193 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Andre Przywara51d8b662010-12-21 11:12:02 +01003194 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003196 /*
3197 * Forward all other exceptions that are valid in real mode.
3198 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
3199 * the required debugging infrastructure rework.
3200 */
3201 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003202 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003203 if (vcpu->guest_debug &
3204 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
3205 return 0;
3206 kvm_queue_exception(vcpu, vec);
3207 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003208 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003209 /*
3210 * Update instruction length as we may reinject the exception
3211 * from user space while in guest debugging mode.
3212 */
3213 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
3214 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003215 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
3216 return 0;
3217 /* fall through */
3218 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003219 case OF_VECTOR:
3220 case BR_VECTOR:
3221 case UD_VECTOR:
3222 case DF_VECTOR:
3223 case SS_VECTOR:
3224 case GP_VECTOR:
3225 case MF_VECTOR:
3226 kvm_queue_exception(vcpu, vec);
3227 return 1;
3228 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003229 return 0;
3230}
3231
Andi Kleena0861c02009-06-08 17:37:09 +08003232/*
3233 * Trigger machine check on the host. We assume all the MSRs are already set up
3234 * by the CPU and that we still run on the same CPU as the MCE occurred on.
3235 * We pass a fake environment to the machine check handler because we want
3236 * the guest to be always treated like user space, no matter what context
3237 * it used internally.
3238 */
3239static void kvm_machine_check(void)
3240{
3241#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
3242 struct pt_regs regs = {
3243 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
3244 .flags = X86_EFLAGS_IF,
3245 };
3246
3247 do_machine_check(&regs, 0);
3248#endif
3249}
3250
Avi Kivity851ba692009-08-24 11:10:17 +03003251static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08003252{
3253 /* already handled by vcpu_run */
3254 return 1;
3255}
3256
Avi Kivity851ba692009-08-24 11:10:17 +03003257static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258{
Avi Kivity1155f762007-11-22 11:30:47 +02003259 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003260 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003261 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003262 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263 u32 vect_info;
3264 enum emulation_result er;
3265
Avi Kivity1155f762007-11-22 11:30:47 +02003266 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02003267 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003268
Andi Kleena0861c02009-06-08 17:37:09 +08003269 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03003270 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003271
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02003273 !is_page_fault(intr_info)) {
3274 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3275 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
3276 vcpu->run->internal.ndata = 2;
3277 vcpu->run->internal.data[0] = vect_info;
3278 vcpu->run->internal.data[1] = intr_info;
3279 return 0;
3280 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281
Jan Kiszkae4a41882008-09-26 09:30:46 +02003282 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02003283 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003284
3285 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03003286 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003287 return 1;
3288 }
3289
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003290 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01003291 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003292 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02003293 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003294 return 1;
3295 }
3296
Avi Kivity6aa8b732006-12-10 02:21:36 -08003297 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06003298 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003299 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
3300 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08003301 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02003302 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003303 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003304 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003305 trace_kvm_page_fault(cr2, error_code);
3306
Gleb Natapov3298b752009-05-11 13:35:46 +03003307 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03003308 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01003309 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003310 }
3311
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003312 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003314 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003315 if (vcpu->arch.halt_request) {
3316 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003317 return kvm_emulate_halt(vcpu);
3318 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003320 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003322 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003323 switch (ex_no) {
3324 case DB_VECTOR:
3325 dr6 = vmcs_readl(EXIT_QUALIFICATION);
3326 if (!(vcpu->guest_debug &
3327 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
3328 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
3329 kvm_queue_exception(vcpu, DB_VECTOR);
3330 return 1;
3331 }
3332 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
3333 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
3334 /* fall through */
3335 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003336 /*
3337 * Update instruction length as we may reinject #BP from
3338 * user space while in guest debugging mode. Reading it for
3339 * #DB as well causes no harm, it is not used in that case.
3340 */
3341 vmx->vcpu.arch.event_exit_inst_len =
3342 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03003344 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003345 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
3346 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003347 break;
3348 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003349 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
3350 kvm_run->ex.exception = ex_no;
3351 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003352 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354 return 0;
3355}
3356
Avi Kivity851ba692009-08-24 11:10:17 +03003357static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003358{
Avi Kivity1165f5f2007-04-19 17:27:43 +03003359 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003360 return 1;
3361}
3362
Avi Kivity851ba692009-08-24 11:10:17 +03003363static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08003364{
Avi Kivity851ba692009-08-24 11:10:17 +03003365 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08003366 return 0;
3367}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003368
Avi Kivity851ba692009-08-24 11:10:17 +03003369static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003370{
He, Qingbfdaab02007-09-12 14:18:28 +08003371 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01003372 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02003373 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003374
He, Qingbfdaab02007-09-12 14:18:28 +08003375 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02003376 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003377 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003378
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003379 ++vcpu->stat.io_exits;
3380
3381 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01003382 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003383
3384 port = exit_qualification >> 16;
3385 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003386 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003387
3388 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003389}
3390
Ingo Molnar102d8322007-02-19 14:37:47 +02003391static void
3392vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3393{
3394 /*
3395 * Patch in the VMCALL instruction:
3396 */
3397 hypercall[0] = 0x0f;
3398 hypercall[1] = 0x01;
3399 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02003400}
3401
Avi Kivity851ba692009-08-24 11:10:17 +03003402static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003404 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405 int cr;
3406 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03003407 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408
He, Qingbfdaab02007-09-12 14:18:28 +08003409 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003410 cr = exit_qualification & 15;
3411 reg = (exit_qualification >> 8) & 15;
3412 switch ((exit_qualification >> 4) & 3) {
3413 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003414 val = kvm_register_read(vcpu, reg);
3415 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003416 switch (cr) {
3417 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03003418 err = kvm_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003419 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420 return 1;
3421 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03003422 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003423 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424 return 1;
3425 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03003426 err = kvm_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003427 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003429 case 8: {
3430 u8 cr8_prev = kvm_get_cr8(vcpu);
3431 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003432 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003433 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003434 if (irqchip_in_kernel(vcpu->kvm))
3435 return 1;
3436 if (cr8_prev <= cr8)
3437 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03003438 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003439 return 0;
3440 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441 };
3442 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03003443 case 2: /* clts */
Avi Kivityedcafe32009-12-30 18:07:40 +02003444 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02003445 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003446 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02003447 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03003448 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003449 case 1: /*mov from cr*/
3450 switch (cr) {
3451 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003452 val = kvm_read_cr3(vcpu);
3453 kvm_register_write(vcpu, reg, val);
3454 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003455 skip_emulated_instruction(vcpu);
3456 return 1;
3457 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003458 val = kvm_get_cr8(vcpu);
3459 kvm_register_write(vcpu, reg, val);
3460 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 skip_emulated_instruction(vcpu);
3462 return 1;
3463 }
3464 break;
3465 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003466 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003467 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003468 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469
3470 skip_emulated_instruction(vcpu);
3471 return 1;
3472 default:
3473 break;
3474 }
Avi Kivity851ba692009-08-24 11:10:17 +03003475 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003476 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477 (int)(exit_qualification >> 4) & 3, cr);
3478 return 0;
3479}
3480
Avi Kivity851ba692009-08-24 11:10:17 +03003481static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482{
He, Qingbfdaab02007-09-12 14:18:28 +08003483 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003484 int dr, reg;
3485
Jan Kiszkaf2483412010-01-20 18:20:20 +01003486 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03003487 if (!kvm_require_cpl(vcpu, 0))
3488 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003489 dr = vmcs_readl(GUEST_DR7);
3490 if (dr & DR7_GD) {
3491 /*
3492 * As the vm-exit takes precedence over the debug trap, we
3493 * need to emulate the latter, either for the host or the
3494 * guest debugging itself.
3495 */
3496 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003497 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3498 vcpu->run->debug.arch.dr7 = dr;
3499 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003500 vmcs_readl(GUEST_CS_BASE) +
3501 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003502 vcpu->run->debug.arch.exception = DB_VECTOR;
3503 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003504 return 0;
3505 } else {
3506 vcpu->arch.dr7 &= ~DR7_GD;
3507 vcpu->arch.dr6 |= DR6_BD;
3508 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3509 kvm_queue_exception(vcpu, DB_VECTOR);
3510 return 1;
3511 }
3512 }
3513
He, Qingbfdaab02007-09-12 14:18:28 +08003514 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003515 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3516 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3517 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03003518 unsigned long val;
3519 if (!kvm_get_dr(vcpu, dr, &val))
3520 kvm_register_write(vcpu, reg, val);
3521 } else
3522 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003523 skip_emulated_instruction(vcpu);
3524 return 1;
3525}
3526
Gleb Natapov020df072010-04-13 10:05:23 +03003527static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3528{
3529 vmcs_writel(GUEST_DR7, val);
3530}
3531
Avi Kivity851ba692009-08-24 11:10:17 +03003532static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003533{
Avi Kivity06465c52007-02-28 20:46:53 +02003534 kvm_emulate_cpuid(vcpu);
3535 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003536}
3537
Avi Kivity851ba692009-08-24 11:10:17 +03003538static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003539{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003540 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541 u64 data;
3542
3543 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02003544 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003545 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003546 return 1;
3547 }
3548
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003549 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003550
Avi Kivity6aa8b732006-12-10 02:21:36 -08003551 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003552 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3553 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003554 skip_emulated_instruction(vcpu);
3555 return 1;
3556}
3557
Avi Kivity851ba692009-08-24 11:10:17 +03003558static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003559{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003560 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3561 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3562 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563
3564 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02003565 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003566 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567 return 1;
3568 }
3569
Avi Kivity59200272010-01-25 19:47:02 +02003570 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571 skip_emulated_instruction(vcpu);
3572 return 1;
3573}
3574
Avi Kivity851ba692009-08-24 11:10:17 +03003575static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003576{
Avi Kivity3842d132010-07-27 12:30:24 +03003577 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003578 return 1;
3579}
3580
Avi Kivity851ba692009-08-24 11:10:17 +03003581static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003582{
Eddie Dong85f455f2007-07-06 12:20:49 +03003583 u32 cpu_based_vm_exec_control;
3584
3585 /* clear pending irq */
3586 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3587 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3588 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003589
Avi Kivity3842d132010-07-27 12:30:24 +03003590 kvm_make_request(KVM_REQ_EVENT, vcpu);
3591
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003592 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003593
Dor Laorc1150d82007-01-05 16:36:24 -08003594 /*
3595 * If the user space waits to inject interrupts, exit as soon as
3596 * possible
3597 */
Gleb Natapov80618232009-04-21 17:44:56 +03003598 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003599 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003600 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003601 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003602 return 0;
3603 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003604 return 1;
3605}
3606
Avi Kivity851ba692009-08-24 11:10:17 +03003607static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003608{
3609 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003610 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003611}
3612
Avi Kivity851ba692009-08-24 11:10:17 +03003613static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003614{
Dor Laor510043d2007-02-19 18:25:43 +02003615 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003616 kvm_emulate_hypercall(vcpu);
3617 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003618}
3619
Avi Kivity851ba692009-08-24 11:10:17 +03003620static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003621{
3622 kvm_queue_exception(vcpu, UD_VECTOR);
3623 return 1;
3624}
3625
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003626static int handle_invd(struct kvm_vcpu *vcpu)
3627{
Andre Przywara51d8b662010-12-21 11:12:02 +01003628 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003629}
3630
Avi Kivity851ba692009-08-24 11:10:17 +03003631static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003632{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003633 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003634
3635 kvm_mmu_invlpg(vcpu, exit_qualification);
3636 skip_emulated_instruction(vcpu);
3637 return 1;
3638}
3639
Avi Kivity851ba692009-08-24 11:10:17 +03003640static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003641{
3642 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003643 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02003644 return 1;
3645}
3646
Dexuan Cui2acf9232010-06-10 11:27:12 +08003647static int handle_xsetbv(struct kvm_vcpu *vcpu)
3648{
3649 u64 new_bv = kvm_read_edx_eax(vcpu);
3650 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3651
3652 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
3653 skip_emulated_instruction(vcpu);
3654 return 1;
3655}
3656
Avi Kivity851ba692009-08-24 11:10:17 +03003657static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003658{
Andre Przywara51d8b662010-12-21 11:12:02 +01003659 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003660}
3661
Avi Kivity851ba692009-08-24 11:10:17 +03003662static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003663{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003664 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003665 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003666 bool has_error_code = false;
3667 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003668 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003669 int reason, type, idt_v;
3670
3671 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3672 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003673
3674 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3675
3676 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003677 if (reason == TASK_SWITCH_GATE && idt_v) {
3678 switch (type) {
3679 case INTR_TYPE_NMI_INTR:
3680 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02003681 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003682 break;
3683 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003684 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003685 kvm_clear_interrupt_queue(vcpu);
3686 break;
3687 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003688 if (vmx->idt_vectoring_info &
3689 VECTORING_INFO_DELIVER_CODE_MASK) {
3690 has_error_code = true;
3691 error_code =
3692 vmcs_read32(IDT_VECTORING_ERROR_CODE);
3693 }
3694 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003695 case INTR_TYPE_SOFT_EXCEPTION:
3696 kvm_clear_exception_queue(vcpu);
3697 break;
3698 default:
3699 break;
3700 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003701 }
Izik Eidus37817f22008-03-24 23:14:53 +02003702 tss_selector = exit_qualification;
3703
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003704 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3705 type != INTR_TYPE_EXT_INTR &&
3706 type != INTR_TYPE_NMI_INTR))
3707 skip_emulated_instruction(vcpu);
3708
Gleb Natapovacb54512010-04-15 21:03:50 +03003709 if (kvm_task_switch(vcpu, tss_selector, reason,
3710 has_error_code, error_code) == EMULATE_FAIL) {
3711 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3712 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3713 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003714 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03003715 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003716
3717 /* clear all local breakpoint enable flags */
3718 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3719
3720 /*
3721 * TODO: What about debug traps on tss switch?
3722 * Are we supposed to inject them and update dr6?
3723 */
3724
3725 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003726}
3727
Avi Kivity851ba692009-08-24 11:10:17 +03003728static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003729{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003730 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003731 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003732 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003733
Sheng Yangf9c617f2009-03-25 10:08:52 +08003734 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003735
3736 if (exit_qualification & (1 << 6)) {
3737 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003738 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003739 }
3740
3741 gla_validity = (exit_qualification >> 7) & 0x3;
3742 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3743 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3744 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3745 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003746 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003747 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3748 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003749 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3750 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003751 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003752 }
3753
3754 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003755 trace_kvm_page_fault(gpa, exit_qualification);
Andre Przywaradc25e892010-12-21 11:12:07 +01003756 return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003757}
3758
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003759static u64 ept_rsvd_mask(u64 spte, int level)
3760{
3761 int i;
3762 u64 mask = 0;
3763
3764 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3765 mask |= (1ULL << i);
3766
3767 if (level > 2)
3768 /* bits 7:3 reserved */
3769 mask |= 0xf8;
3770 else if (level == 2) {
3771 if (spte & (1ULL << 7))
3772 /* 2MB ref, bits 20:12 reserved */
3773 mask |= 0x1ff000;
3774 else
3775 /* bits 6:3 reserved */
3776 mask |= 0x78;
3777 }
3778
3779 return mask;
3780}
3781
3782static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3783 int level)
3784{
3785 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3786
3787 /* 010b (write-only) */
3788 WARN_ON((spte & 0x7) == 0x2);
3789
3790 /* 110b (write/execute) */
3791 WARN_ON((spte & 0x7) == 0x6);
3792
3793 /* 100b (execute-only) and value not supported by logical processor */
3794 if (!cpu_has_vmx_ept_execute_only())
3795 WARN_ON((spte & 0x7) == 0x4);
3796
3797 /* not 000b */
3798 if ((spte & 0x7)) {
3799 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3800
3801 if (rsvd_bits != 0) {
3802 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3803 __func__, rsvd_bits);
3804 WARN_ON(1);
3805 }
3806
3807 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3808 u64 ept_mem_type = (spte & 0x38) >> 3;
3809
3810 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3811 ept_mem_type == 7) {
3812 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3813 __func__, ept_mem_type);
3814 WARN_ON(1);
3815 }
3816 }
3817 }
3818}
3819
Avi Kivity851ba692009-08-24 11:10:17 +03003820static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003821{
3822 u64 sptes[4];
3823 int nr_sptes, i;
3824 gpa_t gpa;
3825
3826 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3827
3828 printk(KERN_ERR "EPT: Misconfiguration.\n");
3829 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3830
3831 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3832
3833 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3834 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3835
Avi Kivity851ba692009-08-24 11:10:17 +03003836 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3837 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003838
3839 return 0;
3840}
3841
Avi Kivity851ba692009-08-24 11:10:17 +03003842static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003843{
3844 u32 cpu_based_vm_exec_control;
3845
3846 /* clear pending NMI */
3847 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3848 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3849 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3850 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03003851 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003852
3853 return 1;
3854}
3855
Mohammed Gamal80ced182009-09-01 12:48:18 +02003856static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003857{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003858 struct vcpu_vmx *vmx = to_vmx(vcpu);
3859 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003860 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02003861 u32 cpu_exec_ctrl;
3862 bool intr_window_requested;
3863
3864 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3865 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003866
3867 while (!guest_state_valid(vcpu)) {
Avi Kivity49e9d552010-09-19 14:34:08 +02003868 if (intr_window_requested
3869 && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
3870 return handle_interrupt_window(&vmx->vcpu);
3871
Andre Przywara51d8b662010-12-21 11:12:02 +01003872 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003873
Mohammed Gamal80ced182009-09-01 12:48:18 +02003874 if (err == EMULATE_DO_MMIO) {
3875 ret = 0;
3876 goto out;
3877 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003878
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003879 if (err != EMULATE_DONE)
3880 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003881
3882 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003883 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003884 if (need_resched())
3885 schedule();
3886 }
3887
Mohammed Gamal80ced182009-09-01 12:48:18 +02003888 vmx->emulation_required = 0;
3889out:
3890 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003891}
3892
Avi Kivity6aa8b732006-12-10 02:21:36 -08003893/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003894 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3895 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3896 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003897static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003898{
3899 skip_emulated_instruction(vcpu);
3900 kvm_vcpu_on_spin(vcpu);
3901
3902 return 1;
3903}
3904
Sheng Yang59708672009-12-15 13:29:54 +08003905static int handle_invalid_op(struct kvm_vcpu *vcpu)
3906{
3907 kvm_queue_exception(vcpu, UD_VECTOR);
3908 return 1;
3909}
3910
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003911/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003912 * The exit handlers return 1 if the exit was handled fully and guest execution
3913 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3914 * to be done to userspace and return 0.
3915 */
Avi Kivity851ba692009-08-24 11:10:17 +03003916static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3918 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003919 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003920 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922 [EXIT_REASON_CR_ACCESS] = handle_cr,
3923 [EXIT_REASON_DR_ACCESS] = handle_dr,
3924 [EXIT_REASON_CPUID] = handle_cpuid,
3925 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3926 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3927 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3928 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003929 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003930 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003931 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003932 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3933 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3934 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3935 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3936 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3937 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3938 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3939 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3940 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003941 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3942 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003943 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08003944 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02003945 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003946 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003947 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3948 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003949 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003950 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3951 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952};
3953
3954static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003955 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003956
Avi Kivity586f9602010-11-18 13:09:54 +02003957static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3958{
3959 *info1 = vmcs_readl(EXIT_QUALIFICATION);
3960 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
3961}
3962
Avi Kivity6aa8b732006-12-10 02:21:36 -08003963/*
3964 * The guest has exited. See if we can fix it or if we need userspace
3965 * assistance.
3966 */
Avi Kivity851ba692009-08-24 11:10:17 +03003967static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003968{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003969 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003970 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003971 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003972
Avi Kivityaa179112010-11-17 18:44:19 +02003973 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003974
Mohammed Gamal80ced182009-09-01 12:48:18 +02003975 /* If guest state is invalid, start emulating */
3976 if (vmx->emulation_required && emulate_invalid_guest_state)
3977 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003978
Mohammed Gamal51207022010-05-31 22:40:54 +03003979 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
3980 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3981 vcpu->run->fail_entry.hardware_entry_failure_reason
3982 = exit_reason;
3983 return 0;
3984 }
3985
Avi Kivity29bd8a72007-09-10 17:27:03 +03003986 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003987 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3988 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003989 = vmcs_read32(VM_INSTRUCTION_ERROR);
3990 return 0;
3991 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003992
Mike Dayd77c26f2007-10-08 09:02:08 -04003993 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003994 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003995 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3996 exit_reason != EXIT_REASON_TASK_SWITCH))
3997 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3998 "(0x%x) and exit reason is 0x%x\n",
3999 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004000
4001 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03004002 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004003 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004004 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01004005 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004006 /*
4007 * This CPU don't support us in finding the end of an
4008 * NMI-blocked window if the guest runs with IRQs
4009 * disabled. So we pull the trigger after 1 s of
4010 * futile waiting, but inform the user about this.
4011 */
4012 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
4013 "state on VCPU %d after 1 s timeout\n",
4014 __func__, vcpu->vcpu_id);
4015 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004016 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004017 }
4018
Avi Kivity6aa8b732006-12-10 02:21:36 -08004019 if (exit_reason < kvm_vmx_max_exit_handlers
4020 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03004021 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004022 else {
Avi Kivity851ba692009-08-24 11:10:17 +03004023 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
4024 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004025 }
4026 return 0;
4027}
4028
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004029static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004030{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004031 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004032 vmcs_write32(TPR_THRESHOLD, 0);
4033 return;
4034 }
4035
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004036 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08004037}
4038
Avi Kivity51aa01d2010-07-20 14:31:20 +03004039static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03004040{
Avi Kivity00eba012011-03-07 17:24:54 +02004041 u32 exit_intr_info;
4042
4043 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
4044 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
4045 return;
4046
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004047 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02004048 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08004049
4050 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004051 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08004052 kvm_machine_check();
4053
Gleb Natapov20f65982009-05-11 13:35:55 +03004054 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004055 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004056 (exit_intr_info & INTR_INFO_VALID_MASK)) {
4057 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03004058 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004059 kvm_after_handle_nmi(&vmx->vcpu);
4060 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03004061}
Gleb Natapov20f65982009-05-11 13:35:55 +03004062
Avi Kivity51aa01d2010-07-20 14:31:20 +03004063static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
4064{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004065 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03004066 bool unblock_nmi;
4067 u8 vector;
4068 bool idtv_info_valid;
4069
4070 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03004071
Avi Kivitycf393f72008-07-01 16:20:21 +03004072 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02004073 if (vmx->nmi_known_unmasked)
4074 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004075 /*
4076 * Can't use vmx->exit_intr_info since we're not sure what
4077 * the exit reason is.
4078 */
4079 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03004080 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
4081 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
4082 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004083 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03004084 * Re-set bit "block by NMI" before VM entry if vmexit caused by
4085 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004086 * SDM 3: 23.2.2 (September 2008)
4087 * Bit 12 is undefined in any of the following cases:
4088 * If the VM exit sets the valid bit in the IDT-vectoring
4089 * information field.
4090 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03004091 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004092 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
4093 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03004094 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4095 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02004096 else
4097 vmx->nmi_known_unmasked =
4098 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
4099 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004100 } else if (unlikely(vmx->soft_vnmi_blocked))
4101 vmx->vnmi_blocked_time +=
4102 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03004103}
4104
Avi Kivity83422e12010-07-20 14:43:23 +03004105static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
4106 u32 idt_vectoring_info,
4107 int instr_len_field,
4108 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03004109{
Avi Kivity51aa01d2010-07-20 14:31:20 +03004110 u8 vector;
4111 int type;
4112 bool idtv_info_valid;
4113
4114 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03004115
Gleb Natapov37b96e92009-03-30 16:03:13 +03004116 vmx->vcpu.arch.nmi_injected = false;
4117 kvm_clear_exception_queue(&vmx->vcpu);
4118 kvm_clear_interrupt_queue(&vmx->vcpu);
4119
4120 if (!idtv_info_valid)
4121 return;
4122
Avi Kivity3842d132010-07-27 12:30:24 +03004123 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
4124
Avi Kivity668f6122008-07-02 09:28:55 +03004125 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
4126 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004127
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004128 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03004129 case INTR_TYPE_NMI_INTR:
4130 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03004131 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004132 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03004133 * Clear bit "block by NMI" before VM entry if a NMI
4134 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03004135 */
Avi Kivity654f06f2011-03-23 15:02:47 +02004136 vmx_set_nmi_mask(&vmx->vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004137 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004138 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004139 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004140 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004141 /* fall through */
4142 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03004143 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03004144 u32 err = vmcs_read32(error_code_field);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004145 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03004146 } else
4147 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004148 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004149 case INTR_TYPE_SOFT_INTR:
4150 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004151 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004152 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03004153 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004154 kvm_queue_interrupt(&vmx->vcpu, vector,
4155 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004156 break;
4157 default:
4158 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03004159 }
Avi Kivitycf393f72008-07-01 16:20:21 +03004160}
4161
Avi Kivity83422e12010-07-20 14:43:23 +03004162static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
4163{
4164 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
4165 VM_EXIT_INSTRUCTION_LEN,
4166 IDT_VECTORING_ERROR_CODE);
4167}
4168
Avi Kivityb463a6f2010-07-20 15:06:17 +03004169static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
4170{
4171 __vmx_complete_interrupts(to_vmx(vcpu),
4172 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
4173 VM_ENTRY_INSTRUCTION_LEN,
4174 VM_ENTRY_EXCEPTION_ERROR_CODE);
4175
4176 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
4177}
4178
Avi Kivityc8019492008-07-14 14:44:59 +03004179#ifdef CONFIG_X86_64
4180#define R "r"
4181#define Q "q"
4182#else
4183#define R "e"
4184#define Q "l"
4185#endif
4186
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08004187static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004188{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004189 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity104f2262010-11-18 13:12:52 +02004190
4191 /* Record the guest's net vcpu time for enforced NMI injections. */
4192 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
4193 vmx->entry_time = ktime_get();
4194
4195 /* Don't enter VMX if guest state is invalid, let the exit handler
4196 start emulation until we arrive back to a valid state */
4197 if (vmx->emulation_required && emulate_invalid_guest_state)
4198 return;
4199
4200 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
4201 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
4202 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
4203 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
4204
4205 /* When single-stepping over STI and MOV SS, we must clear the
4206 * corresponding interruptibility bits in the guest state. Otherwise
4207 * vmentry fails as it then expects bit 14 (BS) in pending debug
4208 * exceptions being set, but that's not correct for the guest debugging
4209 * case. */
4210 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
4211 vmx_set_interrupt_shadow(vcpu, 0);
4212
Nadav Har'Eld462b812011-05-24 15:26:10 +03004213 vmx->__launched = vmx->loaded_vmcs->launched;
Avi Kivity104f2262010-11-18 13:12:52 +02004214 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08004215 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03004216 "push %%"R"dx; push %%"R"bp;"
Avi Kivity40712fa2011-01-06 18:09:12 +02004217 "push %%"R"cx \n\t" /* placeholder for guest rcx */
Avi Kivityc8019492008-07-14 14:44:59 +03004218 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004219 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
4220 "je 1f \n\t"
4221 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004222 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004223 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03004224 /* Reload cr2 if changed */
4225 "mov %c[cr2](%0), %%"R"ax \n\t"
4226 "mov %%cr2, %%"R"dx \n\t"
4227 "cmp %%"R"ax, %%"R"dx \n\t"
4228 "je 2f \n\t"
4229 "mov %%"R"ax, %%cr2 \n\t"
4230 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004231 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02004232 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004233 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03004234 "mov %c[rax](%0), %%"R"ax \n\t"
4235 "mov %c[rbx](%0), %%"R"bx \n\t"
4236 "mov %c[rdx](%0), %%"R"dx \n\t"
4237 "mov %c[rsi](%0), %%"R"si \n\t"
4238 "mov %c[rdi](%0), %%"R"di \n\t"
4239 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004240#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004241 "mov %c[r8](%0), %%r8 \n\t"
4242 "mov %c[r9](%0), %%r9 \n\t"
4243 "mov %c[r10](%0), %%r10 \n\t"
4244 "mov %c[r11](%0), %%r11 \n\t"
4245 "mov %c[r12](%0), %%r12 \n\t"
4246 "mov %c[r13](%0), %%r13 \n\t"
4247 "mov %c[r14](%0), %%r14 \n\t"
4248 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004249#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004250 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
4251
Avi Kivity6aa8b732006-12-10 02:21:36 -08004252 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03004253 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004254 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004255 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004256 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004257 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004258 /* Save guest registers, load host registers, keep flags */
Avi Kivity40712fa2011-01-06 18:09:12 +02004259 "mov %0, %c[wordsize](%%"R"sp) \n\t"
4260 "pop %0 \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004261 "mov %%"R"ax, %c[rax](%0) \n\t"
4262 "mov %%"R"bx, %c[rbx](%0) \n\t"
Avi Kivity1c696d02011-01-06 18:09:11 +02004263 "pop"Q" %c[rcx](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004264 "mov %%"R"dx, %c[rdx](%0) \n\t"
4265 "mov %%"R"si, %c[rsi](%0) \n\t"
4266 "mov %%"R"di, %c[rdi](%0) \n\t"
4267 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004268#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004269 "mov %%r8, %c[r8](%0) \n\t"
4270 "mov %%r9, %c[r9](%0) \n\t"
4271 "mov %%r10, %c[r10](%0) \n\t"
4272 "mov %%r11, %c[r11](%0) \n\t"
4273 "mov %%r12, %c[r12](%0) \n\t"
4274 "mov %%r13, %c[r13](%0) \n\t"
4275 "mov %%r14, %c[r14](%0) \n\t"
4276 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004277#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004278 "mov %%cr2, %%"R"ax \n\t"
4279 "mov %%"R"ax, %c[cr2](%0) \n\t"
4280
Avi Kivity1c696d02011-01-06 18:09:11 +02004281 "pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02004282 "setbe %c[fail](%0) \n\t"
4283 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
Nadav Har'Eld462b812011-05-24 15:26:10 +03004284 [launched]"i"(offsetof(struct vcpu_vmx, __launched)),
Avi Kivitye08aa782007-11-15 18:06:18 +02004285 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03004286 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004287 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
4288 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
4289 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
4290 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
4291 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
4292 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
4293 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004294#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004295 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
4296 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
4297 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
4298 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
4299 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
4300 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
4301 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
4302 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004303#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02004304 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
4305 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02004306 : "cc", "memory"
Jan Kiszka07d6f552010-09-28 16:37:42 +02004307 , R"ax", R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02004308#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02004309 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
4310#endif
4311 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004312
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004313 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02004314 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02004315 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004316 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03004317 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004318 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004319 vcpu->arch.regs_dirty = 0;
4320
Avi Kivity1155f762007-11-22 11:30:47 +02004321 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
4322
Mike Dayd77c26f2007-10-08 09:02:08 -04004323 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Nadav Har'Eld462b812011-05-24 15:26:10 +03004324 vmx->loaded_vmcs->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02004325
Avi Kivity51aa01d2010-07-20 14:31:20 +03004326 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03004327
4328 vmx_complete_atomic_exit(vmx);
4329 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03004330 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004331}
4332
Avi Kivityc8019492008-07-14 14:44:59 +03004333#undef R
4334#undef Q
4335
Avi Kivity6aa8b732006-12-10 02:21:36 -08004336static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
4337{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004338 struct vcpu_vmx *vmx = to_vmx(vcpu);
4339
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004340 free_vpid(vmx);
Nadav Har'Eld462b812011-05-24 15:26:10 +03004341 free_loaded_vmcs(vmx->loaded_vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004342 kfree(vmx->guest_msrs);
4343 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10004344 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004345}
4346
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004347static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004348{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004349 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10004350 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03004351 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004352
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004353 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004354 return ERR_PTR(-ENOMEM);
4355
Sheng Yang2384d2b2008-01-17 15:14:33 +08004356 allocate_vpid(vmx);
4357
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004358 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
4359 if (err)
4360 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004361
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004362 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004363 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004364 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004365 goto uninit_vcpu;
4366 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08004367
Nadav Har'Eld462b812011-05-24 15:26:10 +03004368 vmx->loaded_vmcs = &vmx->vmcs01;
4369 vmx->loaded_vmcs->vmcs = alloc_vmcs();
4370 if (!vmx->loaded_vmcs->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004371 goto free_msrs;
Nadav Har'Eld462b812011-05-24 15:26:10 +03004372 if (!vmm_exclusive)
4373 kvm_cpu_vmxon(__pa(per_cpu(vmxarea, raw_smp_processor_id())));
4374 loaded_vmcs_init(vmx->loaded_vmcs);
4375 if (!vmm_exclusive)
4376 kvm_cpu_vmxoff();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004377
Avi Kivity15ad7142007-07-11 18:17:21 +03004378 cpu = get_cpu();
4379 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004380 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10004381 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004382 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004383 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004384 if (err)
4385 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004386 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004387 err = alloc_apic_access_page(kvm);
4388 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004389 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004390
Sheng Yangb927a3c2009-07-21 10:42:48 +08004391 if (enable_ept) {
4392 if (!kvm->arch.ept_identity_map_addr)
4393 kvm->arch.ept_identity_map_addr =
4394 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004395 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004396 if (alloc_identity_pagetable(kvm) != 0)
4397 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004398 if (!init_rmode_identity_map(kvm))
4399 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004400 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004401
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004402 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004403
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004404free_vmcs:
Nadav Har'Eld462b812011-05-24 15:26:10 +03004405 free_vmcs(vmx->loaded_vmcs->vmcs);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004406free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004407 kfree(vmx->guest_msrs);
4408uninit_vcpu:
4409 kvm_vcpu_uninit(&vmx->vcpu);
4410free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004411 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10004412 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004413 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004414}
4415
Yang, Sheng002c7f72007-07-31 14:23:01 +03004416static void __init vmx_check_processor_compat(void *rtn)
4417{
4418 struct vmcs_config vmcs_conf;
4419
4420 *(int *)rtn = 0;
4421 if (setup_vmcs_config(&vmcs_conf) < 0)
4422 *(int *)rtn = -EIO;
4423 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
4424 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
4425 smp_processor_id());
4426 *(int *)rtn = -EIO;
4427 }
4428}
4429
Sheng Yang67253af2008-04-25 10:20:22 +08004430static int get_ept_level(void)
4431{
4432 return VMX_EPT_DEFAULT_GAW + 1;
4433}
4434
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004435static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08004436{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004437 u64 ret;
4438
Sheng Yang522c68c2009-04-27 20:35:43 +08004439 /* For VT-d and EPT combination
4440 * 1. MMIO: always map as UC
4441 * 2. EPT with VT-d:
4442 * a. VT-d without snooping control feature: can't guarantee the
4443 * result, try to trust guest.
4444 * b. VT-d with snooping control feature: snooping control feature of
4445 * VT-d engine can guarantee the cache correctness. Just set it
4446 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08004447 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08004448 * consistent with host MTRR
4449 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004450 if (is_mmio)
4451 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08004452 else if (vcpu->kvm->arch.iommu_domain &&
4453 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
4454 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
4455 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004456 else
Sheng Yang522c68c2009-04-27 20:35:43 +08004457 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08004458 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004459
4460 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08004461}
4462
Avi Kivityf4c9e872009-12-28 16:06:35 +02004463#define _ER(x) { EXIT_REASON_##x, #x }
4464
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004465static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02004466 _ER(EXCEPTION_NMI),
4467 _ER(EXTERNAL_INTERRUPT),
4468 _ER(TRIPLE_FAULT),
4469 _ER(PENDING_INTERRUPT),
4470 _ER(NMI_WINDOW),
4471 _ER(TASK_SWITCH),
4472 _ER(CPUID),
4473 _ER(HLT),
4474 _ER(INVLPG),
4475 _ER(RDPMC),
4476 _ER(RDTSC),
4477 _ER(VMCALL),
4478 _ER(VMCLEAR),
4479 _ER(VMLAUNCH),
4480 _ER(VMPTRLD),
4481 _ER(VMPTRST),
4482 _ER(VMREAD),
4483 _ER(VMRESUME),
4484 _ER(VMWRITE),
4485 _ER(VMOFF),
4486 _ER(VMON),
4487 _ER(CR_ACCESS),
4488 _ER(DR_ACCESS),
4489 _ER(IO_INSTRUCTION),
4490 _ER(MSR_READ),
4491 _ER(MSR_WRITE),
4492 _ER(MWAIT_INSTRUCTION),
4493 _ER(MONITOR_INSTRUCTION),
4494 _ER(PAUSE_INSTRUCTION),
4495 _ER(MCE_DURING_VMENTRY),
4496 _ER(TPR_BELOW_THRESHOLD),
4497 _ER(APIC_ACCESS),
4498 _ER(EPT_VIOLATION),
4499 _ER(EPT_MISCONFIG),
4500 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004501 { -1, NULL }
4502};
4503
Avi Kivityf4c9e872009-12-28 16:06:35 +02004504#undef _ER
4505
Sheng Yang17cc3932010-01-05 19:02:27 +08004506static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004507{
Sheng Yang878403b2010-01-05 19:02:29 +08004508 if (enable_ept && !cpu_has_vmx_ept_1g_page())
4509 return PT_DIRECTORY_LEVEL;
4510 else
4511 /* For shadow and EPT supported 1GB page */
4512 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004513}
4514
Sheng Yang0e851882009-12-18 16:48:46 +08004515static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4516{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004517 struct kvm_cpuid_entry2 *best;
4518 struct vcpu_vmx *vmx = to_vmx(vcpu);
4519 u32 exec_control;
4520
4521 vmx->rdtscp_enabled = false;
4522 if (vmx_rdtscp_supported()) {
4523 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4524 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4525 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4526 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4527 vmx->rdtscp_enabled = true;
4528 else {
4529 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4530 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4531 exec_control);
4532 }
4533 }
4534 }
Sheng Yang0e851882009-12-18 16:48:46 +08004535}
4536
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004537static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4538{
4539}
4540
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004541static int vmx_check_intercept(struct kvm_vcpu *vcpu,
4542 struct x86_instruction_info *info,
4543 enum x86_intercept_stage stage)
4544{
4545 return X86EMUL_CONTINUE;
4546}
4547
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004548static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004549 .cpu_has_kvm_support = cpu_has_kvm_support,
4550 .disabled_by_bios = vmx_disabled_by_bios,
4551 .hardware_setup = hardware_setup,
4552 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004553 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004554 .hardware_enable = hardware_enable,
4555 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004556 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004557
4558 .vcpu_create = vmx_create_vcpu,
4559 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004560 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004561
Avi Kivity04d2cc72007-09-10 18:10:54 +03004562 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004563 .vcpu_load = vmx_vcpu_load,
4564 .vcpu_put = vmx_vcpu_put,
4565
4566 .set_guest_debug = set_guest_debug,
4567 .get_msr = vmx_get_msr,
4568 .set_msr = vmx_set_msr,
4569 .get_segment_base = vmx_get_segment_base,
4570 .get_segment = vmx_get_segment,
4571 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004572 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004573 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004574 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004575 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004576 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004577 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004578 .set_cr3 = vmx_set_cr3,
4579 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004580 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004581 .get_idt = vmx_get_idt,
4582 .set_idt = vmx_set_idt,
4583 .get_gdt = vmx_get_gdt,
4584 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004585 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004586 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004587 .get_rflags = vmx_get_rflags,
4588 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02004589 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004590 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004591
4592 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004593
Avi Kivity6aa8b732006-12-10 02:21:36 -08004594 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004595 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004596 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004597 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4598 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004599 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004600 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004601 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004602 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004603 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004604 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004605 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004606 .get_nmi_mask = vmx_get_nmi_mask,
4607 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004608 .enable_nmi_window = enable_nmi_window,
4609 .enable_irq_window = enable_irq_window,
4610 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004611
Izik Eiduscbc94022007-10-25 00:29:55 +02004612 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004613 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004614 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004615
Avi Kivity586f9602010-11-18 13:09:54 +02004616 .get_exit_info = vmx_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004617 .exit_reasons_str = vmx_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02004618
Sheng Yang17cc3932010-01-05 19:02:27 +08004619 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004620
4621 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004622
4623 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004624
4625 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004626
4627 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004628
Joerg Roedel4051b182011-03-25 09:44:49 +01004629 .set_tsc_khz = vmx_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004630 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004631 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004632 .compute_tsc_offset = vmx_compute_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004633
4634 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004635
4636 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004637};
4638
4639static int __init vmx_init(void)
4640{
Avi Kivity26bb0982009-09-07 11:14:12 +03004641 int r, i;
4642
4643 rdmsrl_safe(MSR_EFER, &host_efer);
4644
4645 for (i = 0; i < NR_VMX_MSR; ++i)
4646 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004647
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004648 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004649 if (!vmx_io_bitmap_a)
4650 return -ENOMEM;
4651
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004652 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004653 if (!vmx_io_bitmap_b) {
4654 r = -ENOMEM;
4655 goto out;
4656 }
4657
Avi Kivity58972972009-02-24 22:26:47 +02004658 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4659 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004660 r = -ENOMEM;
4661 goto out1;
4662 }
4663
Avi Kivity58972972009-02-24 22:26:47 +02004664 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4665 if (!vmx_msr_bitmap_longmode) {
4666 r = -ENOMEM;
4667 goto out2;
4668 }
4669
He, Qingfdef3ad2007-04-30 09:45:24 +03004670 /*
4671 * Allow direct access to the PC debug port (it is often used for I/O
4672 * delays, but the vmexits simply slow things down).
4673 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004674 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4675 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004676
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004677 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004678
Avi Kivity58972972009-02-24 22:26:47 +02004679 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4680 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004681
Sheng Yang2384d2b2008-01-17 15:14:33 +08004682 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4683
Avi Kivity0ee75be2010-04-28 15:39:01 +03004684 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
4685 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004686 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004687 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004688
Avi Kivity58972972009-02-24 22:26:47 +02004689 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4690 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4691 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4692 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4693 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4694 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004695
Avi Kivity089d0342009-03-23 18:26:32 +02004696 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004697 bypass_guest_pf = 0;
Sheng Yang534e38b2008-09-08 15:12:30 +08004698 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004699 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004700 kvm_enable_tdp();
4701 } else
4702 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004703
Avi Kivityc7addb92007-09-16 18:58:32 +02004704 if (bypass_guest_pf)
4705 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4706
He, Qingfdef3ad2007-04-30 09:45:24 +03004707 return 0;
4708
Avi Kivity58972972009-02-24 22:26:47 +02004709out3:
4710 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004711out2:
Avi Kivity58972972009-02-24 22:26:47 +02004712 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004713out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004714 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004715out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004716 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004717 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718}
4719
4720static void __exit vmx_exit(void)
4721{
Avi Kivity58972972009-02-24 22:26:47 +02004722 free_page((unsigned long)vmx_msr_bitmap_legacy);
4723 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004724 free_page((unsigned long)vmx_io_bitmap_b);
4725 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004726
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004727 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004728}
4729
4730module_init(vmx_init)
4731module_exit(vmx_exit)