blob: 7d22d6c6734d5505f3c89a3ebfb5c7122f0d79ee [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
Avi Kivity26bb0982009-09-07 11:14:12 +0300121struct shared_msr_entry {
122 unsigned index;
123 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200124 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300125};
126
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400127struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000128 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +0300129 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +0300130 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400131 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300132 u8 fail;
Avi Kivity69c73022011-03-07 15:26:44 +0200133 u8 cpl;
Avi Kivity9d58b932011-03-07 16:52:07 +0200134 bool nmi_known_unmasked;
Avi Kivity51aa01d2010-07-20 14:31:20 +0300135 u32 exit_intr_info;
Avi Kivity1155f762007-11-22 11:30:47 +0200136 u32 idt_vectoring_info;
Avi Kivity6de12732011-03-07 12:51:22 +0200137 ulong rflags;
Avi Kivity26bb0982009-09-07 11:14:12 +0300138 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400139 int nmsrs;
140 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400141#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300142 u64 msr_host_kernel_gs_base;
143 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400144#endif
145 struct vmcs *vmcs;
Avi Kivity61d2ef22010-04-28 16:40:38 +0300146 struct msr_autoload {
147 unsigned nr;
148 struct vmx_msr_entry guest[NR_AUTOLOAD_MSRS];
149 struct vmx_msr_entry host[NR_AUTOLOAD_MSRS];
150 } msr_autoload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400151 struct {
152 int loaded;
153 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200154 int gs_ldt_reload_needed;
155 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400156 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200157 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300158 int vm86_active;
Avi Kivity78ac8b42010-04-08 18:19:35 +0300159 ulong save_rflags;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300160 struct kvm_save_segment {
161 u16 selector;
162 unsigned long base;
163 u32 limit;
164 u32 ar;
165 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200166 } rmode;
Avi Kivity2fb92db2011-04-27 19:42:18 +0300167 struct {
168 u32 bitmask; /* 4 bits per segment (1 bit per field) */
169 struct kvm_save_segment seg[8];
170 } segment_cache;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800171 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300172 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200173
174 /* Support for vnmi-less CPUs */
175 int soft_vnmi_blocked;
176 ktime_t entry_time;
177 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800178 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800179
180 bool rdtscp_enabled;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400181};
182
Avi Kivity2fb92db2011-04-27 19:42:18 +0300183enum segment_cache_field {
184 SEG_FIELD_SEL = 0,
185 SEG_FIELD_BASE = 1,
186 SEG_FIELD_LIMIT = 2,
187 SEG_FIELD_AR = 3,
188
189 SEG_FIELD_NR = 4
190};
191
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400192static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
193{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000194 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400195}
196
Sheng Yang4e1096d2008-07-06 19:16:51 +0800197static u64 construct_eptp(unsigned long root_hpa);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800198static void kvm_cpu_vmxon(u64 addr);
199static void kvm_cpu_vmxoff(void);
Avi Kivityaff48ba2010-12-05 18:56:11 +0200200static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
Gleb Natapov776e58e2011-03-13 12:34:27 +0200201static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr);
Avi Kivity75880a02007-06-20 11:20:04 +0300202
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203static DEFINE_PER_CPU(struct vmcs *, vmxarea);
204static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300205static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity3444d7d2010-07-26 18:32:38 +0300206static DEFINE_PER_CPU(struct desc_ptr, host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200208static unsigned long *vmx_io_bitmap_a;
209static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200210static unsigned long *vmx_msr_bitmap_legacy;
211static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300212
Avi Kivity110312c2010-12-21 12:54:20 +0200213static bool cpu_has_load_ia32_efer;
214
Sheng Yang2384d2b2008-01-17 15:14:33 +0800215static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
216static DEFINE_SPINLOCK(vmx_vpid_lock);
217
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300218static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800219 int size;
220 int order;
221 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300222 u32 pin_based_exec_ctrl;
223 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800224 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300225 u32 vmexit_ctrl;
226 u32 vmentry_ctrl;
227} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800228
Hannes Ederefff9e52008-11-28 17:02:06 +0100229static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800230 u32 ept;
231 u32 vpid;
232} vmx_capability;
233
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234#define VMX_SEGMENT_FIELD(seg) \
235 [VCPU_SREG_##seg] = { \
236 .selector = GUEST_##seg##_SELECTOR, \
237 .base = GUEST_##seg##_BASE, \
238 .limit = GUEST_##seg##_LIMIT, \
239 .ar_bytes = GUEST_##seg##_AR_BYTES, \
240 }
241
242static struct kvm_vmx_segment_field {
243 unsigned selector;
244 unsigned base;
245 unsigned limit;
246 unsigned ar_bytes;
247} kvm_vmx_segment_fields[] = {
248 VMX_SEGMENT_FIELD(CS),
249 VMX_SEGMENT_FIELD(DS),
250 VMX_SEGMENT_FIELD(ES),
251 VMX_SEGMENT_FIELD(FS),
252 VMX_SEGMENT_FIELD(GS),
253 VMX_SEGMENT_FIELD(SS),
254 VMX_SEGMENT_FIELD(TR),
255 VMX_SEGMENT_FIELD(LDTR),
256};
257
Avi Kivity26bb0982009-09-07 11:14:12 +0300258static u64 host_efer;
259
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300260static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
261
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300262/*
Brian Gerst8c065852010-07-17 09:03:26 -0400263 * Keep MSR_STAR at the end, as setup_msrs() will try to optimize it
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300264 * away by decrementing the array size.
265 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800267#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300268 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269#endif
Brian Gerst8c065852010-07-17 09:03:26 -0400270 MSR_EFER, MSR_TSC_AUX, MSR_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800271};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200272#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800273
Gui Jianfeng31299942010-03-15 17:29:09 +0800274static inline bool is_page_fault(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275{
276 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
277 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100278 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279}
280
Gui Jianfeng31299942010-03-15 17:29:09 +0800281static inline bool is_no_device(u32 intr_info)
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300282{
283 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
284 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100285 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300286}
287
Gui Jianfeng31299942010-03-15 17:29:09 +0800288static inline bool is_invalid_opcode(u32 intr_info)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500289{
290 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
291 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100292 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500293}
294
Gui Jianfeng31299942010-03-15 17:29:09 +0800295static inline bool is_external_interrupt(u32 intr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296{
297 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
298 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
299}
300
Gui Jianfeng31299942010-03-15 17:29:09 +0800301static inline bool is_machine_check(u32 intr_info)
Andi Kleena0861c02009-06-08 17:37:09 +0800302{
303 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
304 INTR_INFO_VALID_MASK)) ==
305 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
306}
307
Gui Jianfeng31299942010-03-15 17:29:09 +0800308static inline bool cpu_has_vmx_msr_bitmap(void)
Sheng Yang25c5f222008-03-28 13:18:56 +0800309{
Sheng Yang04547152009-04-01 15:52:31 +0800310 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800311}
312
Gui Jianfeng31299942010-03-15 17:29:09 +0800313static inline bool cpu_has_vmx_tpr_shadow(void)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800314{
Sheng Yang04547152009-04-01 15:52:31 +0800315 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800316}
317
Gui Jianfeng31299942010-03-15 17:29:09 +0800318static inline bool vm_need_tpr_shadow(struct kvm *kvm)
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800319{
Sheng Yang04547152009-04-01 15:52:31 +0800320 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800321}
322
Gui Jianfeng31299942010-03-15 17:29:09 +0800323static inline bool cpu_has_secondary_exec_ctrls(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800324{
Sheng Yang04547152009-04-01 15:52:31 +0800325 return vmcs_config.cpu_based_exec_ctrl &
326 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800327}
328
Avi Kivity774ead32007-12-26 13:57:04 +0200329static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800330{
Sheng Yang04547152009-04-01 15:52:31 +0800331 return vmcs_config.cpu_based_2nd_exec_ctrl &
332 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
333}
334
335static inline bool cpu_has_vmx_flexpriority(void)
336{
337 return cpu_has_vmx_tpr_shadow() &&
338 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800339}
340
Marcelo Tosattie7997942009-06-11 12:07:40 -0300341static inline bool cpu_has_vmx_ept_execute_only(void)
342{
Gui Jianfeng31299942010-03-15 17:29:09 +0800343 return vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300344}
345
346static inline bool cpu_has_vmx_eptp_uncacheable(void)
347{
Gui Jianfeng31299942010-03-15 17:29:09 +0800348 return vmx_capability.ept & VMX_EPTP_UC_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300349}
350
351static inline bool cpu_has_vmx_eptp_writeback(void)
352{
Gui Jianfeng31299942010-03-15 17:29:09 +0800353 return vmx_capability.ept & VMX_EPTP_WB_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300354}
355
356static inline bool cpu_has_vmx_ept_2m_page(void)
357{
Gui Jianfeng31299942010-03-15 17:29:09 +0800358 return vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT;
Marcelo Tosattie7997942009-06-11 12:07:40 -0300359}
360
Sheng Yang878403b2010-01-05 19:02:29 +0800361static inline bool cpu_has_vmx_ept_1g_page(void)
362{
Gui Jianfeng31299942010-03-15 17:29:09 +0800363 return vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT;
Sheng Yang878403b2010-01-05 19:02:29 +0800364}
365
Sheng Yang4bc9b982010-06-02 14:05:24 +0800366static inline bool cpu_has_vmx_ept_4levels(void)
367{
368 return vmx_capability.ept & VMX_EPT_PAGE_WALK_4_BIT;
369}
370
Gui Jianfeng31299942010-03-15 17:29:09 +0800371static inline bool cpu_has_vmx_invept_individual_addr(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800372{
Gui Jianfeng31299942010-03-15 17:29:09 +0800373 return vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800374}
375
Gui Jianfeng31299942010-03-15 17:29:09 +0800376static inline bool cpu_has_vmx_invept_context(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800377{
Gui Jianfeng31299942010-03-15 17:29:09 +0800378 return vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800379}
380
Gui Jianfeng31299942010-03-15 17:29:09 +0800381static inline bool cpu_has_vmx_invept_global(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800382{
Gui Jianfeng31299942010-03-15 17:29:09 +0800383 return vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800384}
385
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800386static inline bool cpu_has_vmx_invvpid_single(void)
387{
388 return vmx_capability.vpid & VMX_VPID_EXTENT_SINGLE_CONTEXT_BIT;
389}
390
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800391static inline bool cpu_has_vmx_invvpid_global(void)
392{
393 return vmx_capability.vpid & VMX_VPID_EXTENT_GLOBAL_CONTEXT_BIT;
394}
395
Gui Jianfeng31299942010-03-15 17:29:09 +0800396static inline bool cpu_has_vmx_ept(void)
Sheng Yangd56f5462008-04-25 10:13:16 +0800397{
Sheng Yang04547152009-04-01 15:52:31 +0800398 return vmcs_config.cpu_based_2nd_exec_ctrl &
399 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800400}
401
Gui Jianfeng31299942010-03-15 17:29:09 +0800402static inline bool cpu_has_vmx_unrestricted_guest(void)
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700403{
404 return vmcs_config.cpu_based_2nd_exec_ctrl &
405 SECONDARY_EXEC_UNRESTRICTED_GUEST;
406}
407
Gui Jianfeng31299942010-03-15 17:29:09 +0800408static inline bool cpu_has_vmx_ple(void)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800409{
410 return vmcs_config.cpu_based_2nd_exec_ctrl &
411 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
412}
413
Gui Jianfeng31299942010-03-15 17:29:09 +0800414static inline bool vm_need_virtualize_apic_accesses(struct kvm *kvm)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800415{
Gui Jianfeng6d3e4352010-01-29 15:36:59 +0800416 return flexpriority_enabled && irqchip_in_kernel(kvm);
Sheng Yangf78e0e22007-10-29 09:40:42 +0800417}
418
Gui Jianfeng31299942010-03-15 17:29:09 +0800419static inline bool cpu_has_vmx_vpid(void)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800420{
Sheng Yang04547152009-04-01 15:52:31 +0800421 return vmcs_config.cpu_based_2nd_exec_ctrl &
422 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800423}
424
Gui Jianfeng31299942010-03-15 17:29:09 +0800425static inline bool cpu_has_vmx_rdtscp(void)
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800426{
427 return vmcs_config.cpu_based_2nd_exec_ctrl &
428 SECONDARY_EXEC_RDTSCP;
429}
430
Gui Jianfeng31299942010-03-15 17:29:09 +0800431static inline bool cpu_has_virtual_nmis(void)
Sheng Yangf08864b2008-05-15 18:23:25 +0800432{
433 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
434}
435
Sheng Yangf5f48ee2010-06-30 12:25:15 +0800436static inline bool cpu_has_vmx_wbinvd_exit(void)
437{
438 return vmcs_config.cpu_based_2nd_exec_ctrl &
439 SECONDARY_EXEC_WBINVD_EXITING;
440}
441
Sheng Yang04547152009-04-01 15:52:31 +0800442static inline bool report_flexpriority(void)
443{
444 return flexpriority_enabled;
445}
446
Rusty Russell8b9cf982007-07-30 16:31:43 +1000447static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800448{
449 int i;
450
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400451 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300452 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300453 return i;
454 return -1;
455}
456
Sheng Yang2384d2b2008-01-17 15:14:33 +0800457static inline void __invvpid(int ext, u16 vpid, gva_t gva)
458{
459 struct {
460 u64 vpid : 16;
461 u64 rsvd : 48;
462 u64 gva;
463 } operand = { vpid, 0, gva };
464
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300465 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800466 /* CF==1 or ZF==1 --> rc = -1 */
467 "; ja 1f ; ud2 ; 1:"
468 : : "a"(&operand), "c"(ext) : "cc", "memory");
469}
470
Sheng Yang14394422008-04-28 12:24:45 +0800471static inline void __invept(int ext, u64 eptp, gpa_t gpa)
472{
473 struct {
474 u64 eptp, gpa;
475 } operand = {eptp, gpa};
476
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300477 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800478 /* CF==1 or ZF==1 --> rc = -1 */
479 "; ja 1f ; ud2 ; 1:\n"
480 : : "a" (&operand), "c" (ext) : "cc", "memory");
481}
482
Avi Kivity26bb0982009-09-07 11:14:12 +0300483static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300484{
485 int i;
486
Rusty Russell8b9cf982007-07-30 16:31:43 +1000487 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300488 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400489 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000490 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800491}
492
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493static void vmcs_clear(struct vmcs *vmcs)
494{
495 u64 phys_addr = __pa(vmcs);
496 u8 error;
497
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300498 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200499 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 : "cc", "memory");
501 if (error)
502 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
503 vmcs, phys_addr);
504}
505
Dongxiao Xu7725b892010-05-11 18:29:38 +0800506static void vmcs_load(struct vmcs *vmcs)
507{
508 u64 phys_addr = __pa(vmcs);
509 u8 error;
510
511 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity16d8f722010-12-21 16:51:50 +0200512 : "=qm"(error) : "a"(&phys_addr), "m"(phys_addr)
Dongxiao Xu7725b892010-05-11 18:29:38 +0800513 : "cc", "memory");
514 if (error)
515 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
516 vmcs, phys_addr);
517}
518
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519static void __vcpu_clear(void *arg)
520{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000521 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800522 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523
Rusty Russell8b9cf982007-07-30 16:31:43 +1000524 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400525 vmcs_clear(vmx->vmcs);
526 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800527 per_cpu(current_vmcs, cpu) = NULL;
Avi Kivity543e4242008-05-13 16:22:47 +0300528 list_del(&vmx->local_vcpus_link);
529 vmx->vcpu.cpu = -1;
530 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531}
532
Rusty Russell8b9cf982007-07-30 16:31:43 +1000533static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800534{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200535 if (vmx->vcpu.cpu == -1)
536 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200537 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800538}
539
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800540static inline void vpid_sync_vcpu_single(struct vcpu_vmx *vmx)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800541{
542 if (vmx->vpid == 0)
543 return;
544
Gui Jianfeng518c8ae2010-06-04 08:51:39 +0800545 if (cpu_has_vmx_invvpid_single())
546 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
Sheng Yang2384d2b2008-01-17 15:14:33 +0800547}
548
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800549static inline void vpid_sync_vcpu_global(void)
550{
551 if (cpu_has_vmx_invvpid_global())
552 __invvpid(VMX_VPID_EXTENT_ALL_CONTEXT, 0, 0);
553}
554
555static inline void vpid_sync_context(struct vcpu_vmx *vmx)
556{
557 if (cpu_has_vmx_invvpid_single())
Gui Jianfeng1760dd42010-06-07 10:33:27 +0800558 vpid_sync_vcpu_single(vmx);
Gui Jianfengb9d762f2010-06-07 10:32:29 +0800559 else
560 vpid_sync_vcpu_global();
561}
562
Sheng Yang14394422008-04-28 12:24:45 +0800563static inline void ept_sync_global(void)
564{
565 if (cpu_has_vmx_invept_global())
566 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
567}
568
569static inline void ept_sync_context(u64 eptp)
570{
Avi Kivity089d0342009-03-23 18:26:32 +0200571 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800572 if (cpu_has_vmx_invept_context())
573 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
574 else
575 ept_sync_global();
576 }
577}
578
579static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
580{
Avi Kivity089d0342009-03-23 18:26:32 +0200581 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800582 if (cpu_has_vmx_invept_individual_addr())
583 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
584 eptp, gpa);
585 else
586 ept_sync_context(eptp);
587 }
588}
589
Avi Kivity6aa8b732006-12-10 02:21:36 -0800590static unsigned long vmcs_readl(unsigned long field)
591{
Avi Kivity5e520e62011-05-15 10:13:12 -0400592 unsigned long value;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593
Avi Kivity5e520e62011-05-15 10:13:12 -0400594 asm volatile (__ex_clear(ASM_VMX_VMREAD_RDX_RAX, "%0")
595 : "=a"(value) : "d"(field) : "cc");
Avi Kivity6aa8b732006-12-10 02:21:36 -0800596 return value;
597}
598
599static u16 vmcs_read16(unsigned long field)
600{
601 return vmcs_readl(field);
602}
603
604static u32 vmcs_read32(unsigned long field)
605{
606 return vmcs_readl(field);
607}
608
609static u64 vmcs_read64(unsigned long field)
610{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800611#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800612 return vmcs_readl(field);
613#else
614 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
615#endif
616}
617
Avi Kivitye52de1b2007-01-05 16:36:56 -0800618static noinline void vmwrite_error(unsigned long field, unsigned long value)
619{
620 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
621 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
622 dump_stack();
623}
624
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625static void vmcs_writel(unsigned long field, unsigned long value)
626{
627 u8 error;
628
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300629 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400630 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800631 if (unlikely(error))
632 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633}
634
635static void vmcs_write16(unsigned long field, u16 value)
636{
637 vmcs_writel(field, value);
638}
639
640static void vmcs_write32(unsigned long field, u32 value)
641{
642 vmcs_writel(field, value);
643}
644
645static void vmcs_write64(unsigned long field, u64 value)
646{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300648#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800649 asm volatile ("");
650 vmcs_writel(field+1, value >> 32);
651#endif
652}
653
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300654static void vmcs_clear_bits(unsigned long field, u32 mask)
655{
656 vmcs_writel(field, vmcs_readl(field) & ~mask);
657}
658
659static void vmcs_set_bits(unsigned long field, u32 mask)
660{
661 vmcs_writel(field, vmcs_readl(field) | mask);
662}
663
Avi Kivity2fb92db2011-04-27 19:42:18 +0300664static void vmx_segment_cache_clear(struct vcpu_vmx *vmx)
665{
666 vmx->segment_cache.bitmask = 0;
667}
668
669static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
670 unsigned field)
671{
672 bool ret;
673 u32 mask = 1 << (seg * SEG_FIELD_NR + field);
674
675 if (!(vmx->vcpu.arch.regs_avail & (1 << VCPU_EXREG_SEGMENTS))) {
676 vmx->vcpu.arch.regs_avail |= (1 << VCPU_EXREG_SEGMENTS);
677 vmx->segment_cache.bitmask = 0;
678 }
679 ret = vmx->segment_cache.bitmask & mask;
680 vmx->segment_cache.bitmask |= mask;
681 return ret;
682}
683
684static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
685{
686 u16 *p = &vmx->segment_cache.seg[seg].selector;
687
688 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
689 *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
690 return *p;
691}
692
693static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
694{
695 ulong *p = &vmx->segment_cache.seg[seg].base;
696
697 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
698 *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
699 return *p;
700}
701
702static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
703{
704 u32 *p = &vmx->segment_cache.seg[seg].limit;
705
706 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
707 *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
708 return *p;
709}
710
711static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
712{
713 u32 *p = &vmx->segment_cache.seg[seg].ar;
714
715 if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
716 *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
717 return *p;
718}
719
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300720static void update_exception_bitmap(struct kvm_vcpu *vcpu)
721{
722 u32 eb;
723
Jan Kiszkafd7373c2010-01-20 18:20:20 +0100724 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
725 (1u << NM_VECTOR) | (1u << DB_VECTOR);
726 if ((vcpu->guest_debug &
727 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
728 (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
729 eb |= 1u << BP_VECTOR;
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300730 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300731 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200732 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800733 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200734 if (vcpu->fpu_active)
735 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300736 vmcs_write32(EXCEPTION_BITMAP, eb);
737}
738
Avi Kivity61d2ef22010-04-28 16:40:38 +0300739static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
740{
741 unsigned i;
742 struct msr_autoload *m = &vmx->msr_autoload;
743
Avi Kivity110312c2010-12-21 12:54:20 +0200744 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
745 vmcs_clear_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
746 vmcs_clear_bits(VM_EXIT_CONTROLS, VM_EXIT_LOAD_IA32_EFER);
747 return;
748 }
749
Avi Kivity61d2ef22010-04-28 16:40:38 +0300750 for (i = 0; i < m->nr; ++i)
751 if (m->guest[i].index == msr)
752 break;
753
754 if (i == m->nr)
755 return;
756 --m->nr;
757 m->guest[i] = m->guest[m->nr];
758 m->host[i] = m->host[m->nr];
759 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
760 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
761}
762
763static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
764 u64 guest_val, u64 host_val)
765{
766 unsigned i;
767 struct msr_autoload *m = &vmx->msr_autoload;
768
Avi Kivity110312c2010-12-21 12:54:20 +0200769 if (msr == MSR_EFER && cpu_has_load_ia32_efer) {
770 vmcs_write64(GUEST_IA32_EFER, guest_val);
771 vmcs_write64(HOST_IA32_EFER, host_val);
772 vmcs_set_bits(VM_ENTRY_CONTROLS, VM_ENTRY_LOAD_IA32_EFER);
773 vmcs_set_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 ++m->nr;
783 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->nr);
784 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->nr);
785 }
786
787 m->guest[i].index = msr;
788 m->guest[i].value = guest_val;
789 m->host[i].index = msr;
790 m->host[i].value = host_val;
791}
792
Avi Kivity33ed6322007-05-02 16:54:03 +0300793static void reload_tss(void)
794{
Avi Kivity33ed6322007-05-02 16:54:03 +0300795 /*
796 * VT restores TR but not its size. Useless.
797 */
Avi Kivityd3591922010-07-26 18:32:39 +0300798 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivitya5f61302008-02-20 17:57:21 +0200799 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300800
Avi Kivityd3591922010-07-26 18:32:39 +0300801 descs = (void *)gdt->address;
Avi Kivity33ed6322007-05-02 16:54:03 +0300802 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
803 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300804}
805
Avi Kivity92c0d902009-10-29 11:00:16 +0200806static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300807{
Roel Kluin3a34a882009-08-04 02:08:45 -0700808 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300809 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300810
Avi Kivityf6801df2010-01-21 15:31:50 +0200811 guest_efer = vmx->vcpu.arch.efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700812
Avi Kivity51c6cf62007-08-29 03:48:05 +0300813 /*
814 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
815 * outside long mode
816 */
817 ignore_bits = EFER_NX | EFER_SCE;
818#ifdef CONFIG_X86_64
819 ignore_bits |= EFER_LMA | EFER_LME;
820 /* SCE is meaningful only in long mode on Intel */
821 if (guest_efer & EFER_LMA)
822 ignore_bits &= ~(u64)EFER_SCE;
823#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300824 guest_efer &= ~ignore_bits;
825 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300826 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200827 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity84ad33e2010-04-28 16:42:29 +0300828
829 clear_atomic_switch_msr(vmx, MSR_EFER);
830 /* On ept, can't emulate nx, and must switch nx atomically */
831 if (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX)) {
832 guest_efer = vmx->vcpu.arch.efer;
833 if (!(guest_efer & EFER_LMA))
834 guest_efer &= ~EFER_LME;
835 add_atomic_switch_msr(vmx, MSR_EFER, guest_efer, host_efer);
836 return false;
837 }
838
Avi Kivity26bb0982009-09-07 11:14:12 +0300839 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300840}
841
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200842static unsigned long segment_base(u16 selector)
843{
Avi Kivityd3591922010-07-26 18:32:39 +0300844 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200845 struct desc_struct *d;
846 unsigned long table_base;
847 unsigned long v;
848
849 if (!(selector & ~3))
850 return 0;
851
Avi Kivityd3591922010-07-26 18:32:39 +0300852 table_base = gdt->address;
Gleb Natapov2d49ec72010-02-25 12:43:09 +0200853
854 if (selector & 4) { /* from ldt */
855 u16 ldt_selector = kvm_read_ldt();
856
857 if (!(ldt_selector & ~3))
858 return 0;
859
860 table_base = segment_base(ldt_selector);
861 }
862 d = (struct desc_struct *)(table_base + (selector & ~7));
863 v = get_desc_base(d);
864#ifdef CONFIG_X86_64
865 if (d->s == 0 && (d->type == 2 || d->type == 9 || d->type == 11))
866 v |= ((unsigned long)((struct ldttss_desc64 *)d)->base3) << 32;
867#endif
868 return v;
869}
870
871static inline unsigned long kvm_read_tr_base(void)
872{
873 u16 tr;
874 asm("str %0" : "=g"(tr));
875 return segment_base(tr);
876}
877
Avi Kivity04d2cc72007-09-10 18:10:54 +0300878static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300879{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300880 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300881 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300882
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400883 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300884 return;
885
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400886 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300887 /*
888 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
889 * allow segment selectors with cpl > 0 or ti == 1.
890 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300891 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200892 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivity9581d442010-10-19 16:46:55 +0200893 savesegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200894 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400895 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200896 vmx->host_state.fs_reload_needed = 0;
897 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300898 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200899 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300900 }
Avi Kivity9581d442010-10-19 16:46:55 +0200901 savesegment(gs, vmx->host_state.gs_sel);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400902 if (!(vmx->host_state.gs_sel & 7))
903 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300904 else {
905 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200906 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300907 }
908
909#ifdef CONFIG_X86_64
910 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
911 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
912#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400913 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
914 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300915#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300916
917#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +0200918 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
919 if (is_long_mode(&vmx->vcpu))
Avi Kivity44ea2b12009-09-06 15:55:37 +0300920 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
Avi Kivity707c0872007-05-02 17:33:43 +0300921#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300922 for (i = 0; i < vmx->save_nmsrs; ++i)
923 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200924 vmx->guest_msrs[i].data,
925 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300926}
927
Avi Kivitya9b21b62008-06-24 11:48:49 +0300928static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300929{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400930 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300931 return;
932
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200933 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400934 vmx->host_state.loaded = 0;
Avi Kivityc8770e72010-11-11 12:37:26 +0200935#ifdef CONFIG_X86_64
936 if (is_long_mode(&vmx->vcpu))
937 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
938#endif
Laurent Vivier152d3f22007-08-23 16:33:11 +0200939 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300940 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300941#ifdef CONFIG_X86_64
Avi Kivity9581d442010-10-19 16:46:55 +0200942 load_gs_index(vmx->host_state.gs_sel);
Avi Kivity9581d442010-10-19 16:46:55 +0200943#else
944 loadsegment(gs, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300945#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300946 }
Avi Kivity0a77fe42010-10-19 18:48:35 +0200947 if (vmx->host_state.fs_reload_needed)
948 loadsegment(fs, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200949 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300950#ifdef CONFIG_X86_64
Avi Kivityc8770e72010-11-11 12:37:26 +0200951 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
Avi Kivity44ea2b12009-09-06 15:55:37 +0300952#endif
Avi Kivity1c11e712010-05-03 16:05:44 +0300953 if (current_thread_info()->status & TS_USEDFPU)
954 clts();
Avi Kivity3444d7d2010-07-26 18:32:38 +0300955 load_gdt(&__get_cpu_var(host_gdt));
Avi Kivity33ed6322007-05-02 16:54:03 +0300956}
957
Avi Kivitya9b21b62008-06-24 11:48:49 +0300958static void vmx_load_host_state(struct vcpu_vmx *vmx)
959{
960 preempt_disable();
961 __vmx_load_host_state(vmx);
962 preempt_enable();
963}
964
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965/*
966 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
967 * vcpu mutex is already taken.
968 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300969static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400971 struct vcpu_vmx *vmx = to_vmx(vcpu);
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800972 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973
Dongxiao Xu4610c9c2010-05-11 18:29:48 +0800974 if (!vmm_exclusive)
975 kvm_cpu_vmxon(phys_addr);
976 else if (vcpu->cpu != cpu)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000977 vcpu_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400979 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400980 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Dongxiao Xu7725b892010-05-11 18:29:38 +0800981 vmcs_load(vmx->vmcs);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982 }
983
984 if (vcpu->cpu != cpu) {
Avi Kivityd3591922010-07-26 18:32:39 +0300985 struct desc_ptr *gdt = &__get_cpu_var(host_gdt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 unsigned long sysenter_esp;
987
Avi Kivitya8eeb042010-05-10 12:34:53 +0300988 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
Dongxiao Xu92fe13b2010-05-11 18:29:42 +0800989 local_irq_disable();
990 list_add(&vmx->local_vcpus_link,
991 &per_cpu(vcpus_on_cpu, cpu));
992 local_irq_enable();
993
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 /*
995 * Linux uses per-cpu TSS and GDT, so set these when switching
996 * processors.
997 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300998 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
Avi Kivityd3591922010-07-26 18:32:39 +0300999 vmcs_writel(HOST_GDTR_BASE, gdt->address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001000
1001 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
1002 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
1003 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004}
1005
1006static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
1007{
Avi Kivitya9b21b62008-06-24 11:48:49 +03001008 __vmx_load_host_state(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001009 if (!vmm_exclusive) {
Dongxiao Xub923e622010-05-11 18:29:45 +08001010 __vcpu_clear(to_vmx(vcpu));
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001011 kvm_cpu_vmxoff();
1012 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013}
1014
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001015static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
1016{
Avi Kivity81231c62010-01-24 16:26:40 +02001017 ulong cr0;
1018
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001019 if (vcpu->fpu_active)
1020 return;
1021 vcpu->fpu_active = 1;
Avi Kivity81231c62010-01-24 16:26:40 +02001022 cr0 = vmcs_readl(GUEST_CR0);
1023 cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
1024 cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
1025 vmcs_writel(GUEST_CR0, cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001026 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001027 vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
1028 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001029}
1030
Avi Kivityedcafe32009-12-30 18:07:40 +02001031static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
1032
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001033static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
1034{
Avi Kivityedcafe32009-12-30 18:07:40 +02001035 vmx_decache_cr0_guest_bits(vcpu);
Avi Kivity81231c62010-01-24 16:26:40 +02001036 vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001037 update_exception_bitmap(vcpu);
Avi Kivityedcafe32009-12-30 18:07:40 +02001038 vcpu->arch.cr0_guest_owned_bits = 0;
1039 vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
1040 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001041}
1042
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
1044{
Avi Kivity78ac8b42010-04-08 18:19:35 +03001045 unsigned long rflags, save_rflags;
Avi Kivity345dcaa2009-08-12 15:29:37 +03001046
Avi Kivity6de12732011-03-07 12:51:22 +02001047 if (!test_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail)) {
1048 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
1049 rflags = vmcs_readl(GUEST_RFLAGS);
1050 if (to_vmx(vcpu)->rmode.vm86_active) {
1051 rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1052 save_rflags = to_vmx(vcpu)->rmode.save_rflags;
1053 rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
1054 }
1055 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001056 }
Avi Kivity6de12732011-03-07 12:51:22 +02001057 return to_vmx(vcpu)->rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001058}
1059
1060static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1061{
Avi Kivity6de12732011-03-07 12:51:22 +02001062 __set_bit(VCPU_EXREG_RFLAGS, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity69c73022011-03-07 15:26:44 +02001063 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6de12732011-03-07 12:51:22 +02001064 to_vmx(vcpu)->rflags = rflags;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001065 if (to_vmx(vcpu)->rmode.vm86_active) {
1066 to_vmx(vcpu)->rmode.save_rflags = rflags;
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001067 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity78ac8b42010-04-08 18:19:35 +03001068 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 vmcs_writel(GUEST_RFLAGS, rflags);
1070}
1071
Glauber Costa2809f5d2009-05-12 16:21:05 -04001072static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1073{
1074 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1075 int ret = 0;
1076
1077 if (interruptibility & GUEST_INTR_STATE_STI)
Jan Kiszka48005f62010-02-19 19:38:07 +01001078 ret |= KVM_X86_SHADOW_INT_STI;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001079 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
Jan Kiszka48005f62010-02-19 19:38:07 +01001080 ret |= KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -04001081
1082 return ret & mask;
1083}
1084
1085static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
1086{
1087 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
1088 u32 interruptibility = interruptibility_old;
1089
1090 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
1091
Jan Kiszka48005f62010-02-19 19:38:07 +01001092 if (mask & KVM_X86_SHADOW_INT_MOV_SS)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001093 interruptibility |= GUEST_INTR_STATE_MOV_SS;
Jan Kiszka48005f62010-02-19 19:38:07 +01001094 else if (mask & KVM_X86_SHADOW_INT_STI)
Glauber Costa2809f5d2009-05-12 16:21:05 -04001095 interruptibility |= GUEST_INTR_STATE_STI;
1096
1097 if ((interruptibility != interruptibility_old))
1098 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
1099}
1100
Avi Kivity6aa8b732006-12-10 02:21:36 -08001101static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
1102{
1103 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001104
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001105 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001106 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001107 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001108
Glauber Costa2809f5d2009-05-12 16:21:05 -04001109 /* skipping an emulated instruction also counts */
1110 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111}
1112
Anthony Liguori443381a2010-12-06 10:53:38 -06001113static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
1114{
1115 /* Ensure that we clear the HLT state in the VMCS. We don't need to
1116 * explicitly skip the instruction because if the HLT state is set, then
1117 * the instruction is already executing and RIP has already been
1118 * advanced. */
1119 if (!yield_on_hlt &&
1120 vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
1121 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
1122}
1123
Avi Kivity298101d2007-11-25 13:41:11 +02001124static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +02001125 bool has_error_code, u32 error_code,
1126 bool reinject)
Avi Kivity298101d2007-11-25 13:41:11 +02001127{
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001128 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001129 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001130
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001131 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001132 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001133 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
1134 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001135
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001136 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05001137 int inc_eip = 0;
1138 if (kvm_exception_is_soft(nr))
1139 inc_eip = vcpu->arch.event_exit_inst_len;
1140 if (kvm_inject_realmode_interrupt(vcpu, nr, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02001141 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka77ab6db2008-07-14 12:28:51 +02001142 return;
1143 }
1144
Gleb Natapov66fd3f72009-05-11 13:35:50 +03001145 if (kvm_exception_is_soft(nr)) {
1146 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
1147 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +01001148 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
1149 } else
1150 intr_info |= INTR_TYPE_HARD_EXCEPTION;
1151
1152 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Anthony Liguori443381a2010-12-06 10:53:38 -06001153 vmx_clear_hlt(vcpu);
Avi Kivity298101d2007-11-25 13:41:11 +02001154}
1155
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001156static bool vmx_rdtscp_supported(void)
1157{
1158 return cpu_has_vmx_rdtscp();
1159}
1160
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161/*
Eddie Donga75beee2007-05-17 18:55:15 +03001162 * Swap MSR entry in host/guest MSR entry array.
1163 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001164static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +03001165{
Avi Kivity26bb0982009-09-07 11:14:12 +03001166 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001167
1168 tmp = vmx->guest_msrs[to];
1169 vmx->guest_msrs[to] = vmx->guest_msrs[from];
1170 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +03001171}
1172
1173/*
Avi Kivitye38aea32007-04-19 13:22:48 +03001174 * Set up the vmcs to automatically save and restore system
1175 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
1176 * mode, as fiddling with msrs is very expensive.
1177 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10001178static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +03001179{
Avi Kivity26bb0982009-09-07 11:14:12 +03001180 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +02001181 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +03001182
Avi Kivity33f9c502008-02-27 16:06:57 +02001183 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +03001184 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001185#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +10001186 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +10001187 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +03001188 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001189 move_msr_up(vmx, index, save_nmsrs++);
1190 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001191 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001192 move_msr_up(vmx, index, save_nmsrs++);
1193 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +03001194 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +10001195 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001196 index = __find_msr_index(vmx, MSR_TSC_AUX);
1197 if (index >= 0 && vmx->rdtscp_enabled)
1198 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +03001199 /*
Brian Gerst8c065852010-07-17 09:03:26 -04001200 * MSR_STAR is only needed on long mode guests, and only
Eddie Donga75beee2007-05-17 18:55:15 +03001201 * if efer.sce is enabled.
1202 */
Brian Gerst8c065852010-07-17 09:03:26 -04001203 index = __find_msr_index(vmx, MSR_STAR);
Avi Kivityf6801df2010-01-21 15:31:50 +02001204 if ((index >= 0) && (vmx->vcpu.arch.efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +10001205 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001206 }
Eddie Donga75beee2007-05-17 18:55:15 +03001207#endif
Avi Kivity92c0d902009-10-29 11:00:16 +02001208 index = __find_msr_index(vmx, MSR_EFER);
1209 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +03001210 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +03001211
Avi Kivity26bb0982009-09-07 11:14:12 +03001212 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +02001213
1214 if (cpu_has_vmx_msr_bitmap()) {
1215 if (is_long_mode(&vmx->vcpu))
1216 msr_bitmap = vmx_msr_bitmap_longmode;
1217 else
1218 msr_bitmap = vmx_msr_bitmap_legacy;
1219
1220 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
1221 }
Avi Kivitye38aea32007-04-19 13:22:48 +03001222}
1223
1224/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 * reads and returns guest's timestamp counter "register"
1226 * guest_tsc = host_tsc + tsc_offset -- 21.3
1227 */
1228static u64 guest_read_tsc(void)
1229{
1230 u64 host_tsc, tsc_offset;
1231
1232 rdtscll(host_tsc);
1233 tsc_offset = vmcs_read64(TSC_OFFSET);
1234 return host_tsc + tsc_offset;
1235}
1236
1237/*
Joerg Roedel4051b182011-03-25 09:44:49 +01001238 * Empty call-back. Needs to be implemented when VMX enables the SET_TSC_KHZ
1239 * ioctl. In this case the call-back should update internal vmx state to make
1240 * the changes effective.
1241 */
1242static void vmx_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
1243{
1244 /* Nothing to do here */
1245}
1246
1247/*
Zachary Amsden99e3e302010-08-19 22:07:17 -10001248 * writes 'offset' into guest's timestamp counter offset register
Avi Kivity6aa8b732006-12-10 02:21:36 -08001249 */
Zachary Amsden99e3e302010-08-19 22:07:17 -10001250static void vmx_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001251{
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001252 vmcs_write64(TSC_OFFSET, offset);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001253}
1254
Zachary Amsdene48672f2010-08-19 22:07:23 -10001255static void vmx_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
1256{
1257 u64 offset = vmcs_read64(TSC_OFFSET);
1258 vmcs_write64(TSC_OFFSET, offset + adjustment);
1259}
1260
Joerg Roedel857e4092011-03-25 09:44:50 +01001261static u64 vmx_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1262{
1263 return target_tsc - native_read_tsc();
1264}
1265
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266/*
1267 * Reads an msr value (of 'msr_index') into 'pdata'.
1268 * Returns 0 on success, non-0 otherwise.
1269 * Assumes vcpu_load() was already called.
1270 */
1271static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1272{
1273 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001274 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001275
1276 if (!pdata) {
1277 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1278 return -EINVAL;
1279 }
1280
1281 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001282#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283 case MSR_FS_BASE:
1284 data = vmcs_readl(GUEST_FS_BASE);
1285 break;
1286 case MSR_GS_BASE:
1287 data = vmcs_readl(GUEST_GS_BASE);
1288 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001289 case MSR_KERNEL_GS_BASE:
1290 vmx_load_host_state(to_vmx(vcpu));
1291 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1292 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001293#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001295 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301296 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297 data = guest_read_tsc();
1298 break;
1299 case MSR_IA32_SYSENTER_CS:
1300 data = vmcs_read32(GUEST_SYSENTER_CS);
1301 break;
1302 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001303 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001304 break;
1305 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001306 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001307 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001308 case MSR_TSC_AUX:
1309 if (!to_vmx(vcpu)->rdtscp_enabled)
1310 return 1;
1311 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001313 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001314 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001315 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001316 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001317 data = msr->data;
1318 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001319 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001320 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321 }
1322
1323 *pdata = data;
1324 return 0;
1325}
1326
1327/*
1328 * Writes msr value into into the appropriate "register".
1329 * Returns 0 on success, non-0 otherwise.
1330 * Assumes vcpu_load() was already called.
1331 */
1332static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1333{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001334 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001335 struct shared_msr_entry *msr;
Eddie Dong2cc51562007-05-21 07:28:09 +03001336 int ret = 0;
1337
Avi Kivity6aa8b732006-12-10 02:21:36 -08001338 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001339 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001340 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001341 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001342 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001343#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344 case MSR_FS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001345 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346 vmcs_writel(GUEST_FS_BASE, data);
1347 break;
1348 case MSR_GS_BASE:
Avi Kivity2fb92db2011-04-27 19:42:18 +03001349 vmx_segment_cache_clear(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350 vmcs_writel(GUEST_GS_BASE, data);
1351 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001352 case MSR_KERNEL_GS_BASE:
1353 vmx_load_host_state(vmx);
1354 vmx->msr_guest_kernel_gs_base = data;
1355 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356#endif
1357 case MSR_IA32_SYSENTER_CS:
1358 vmcs_write32(GUEST_SYSENTER_CS, data);
1359 break;
1360 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001361 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001362 break;
1363 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001364 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001365 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301366 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10001367 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001368 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001369 case MSR_IA32_CR_PAT:
1370 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1371 vmcs_write64(GUEST_IA32_PAT, data);
1372 vcpu->arch.pat = data;
1373 break;
1374 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001375 ret = kvm_set_msr_common(vcpu, msr_index, data);
1376 break;
1377 case MSR_TSC_AUX:
1378 if (!vmx->rdtscp_enabled)
1379 return 1;
1380 /* Check reserved bit, higher 32 bits should be zero */
1381 if ((data >> 32) != 0)
1382 return 1;
1383 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001385 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001386 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001387 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001388 msr->data = data;
1389 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001391 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392 }
1393
Eddie Dong2cc51562007-05-21 07:28:09 +03001394 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395}
1396
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001397static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001399 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1400 switch (reg) {
1401 case VCPU_REGS_RSP:
1402 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1403 break;
1404 case VCPU_REGS_RIP:
1405 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1406 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001407 case VCPU_EXREG_PDPTR:
1408 if (enable_ept)
1409 ept_save_pdptrs(vcpu);
1410 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001411 default:
1412 break;
1413 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001414}
1415
Jan Kiszka355be0b2009-10-03 00:31:21 +02001416static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001417{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001418 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1419 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1420 else
1421 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1422
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001423 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001424}
1425
1426static __init int cpu_has_kvm_support(void)
1427{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001428 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429}
1430
1431static __init int vmx_disabled_by_bios(void)
1432{
1433 u64 msr;
1434
1435 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Shane Wangcafd6652010-04-29 12:09:01 -04001436 if (msr & FEATURE_CONTROL_LOCKED) {
Joseph Cihula23f3e992011-02-08 11:45:56 -08001437 /* launched w/ TXT and VMX disabled */
Shane Wangcafd6652010-04-29 12:09:01 -04001438 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
1439 && tboot_enabled())
1440 return 1;
Joseph Cihula23f3e992011-02-08 11:45:56 -08001441 /* launched w/o TXT and VMX only enabled w/ TXT */
Shane Wangcafd6652010-04-29 12:09:01 -04001442 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
Joseph Cihula23f3e992011-02-08 11:45:56 -08001443 && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
Shane Wangf9335af2010-11-17 11:40:17 +08001444 && !tboot_enabled()) {
1445 printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
Joseph Cihula23f3e992011-02-08 11:45:56 -08001446 "activate TXT before enabling KVM\n");
Shane Wangcafd6652010-04-29 12:09:01 -04001447 return 1;
Shane Wangf9335af2010-11-17 11:40:17 +08001448 }
Joseph Cihula23f3e992011-02-08 11:45:56 -08001449 /* launched w/o TXT and VMX disabled */
1450 if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
1451 && !tboot_enabled())
1452 return 1;
Shane Wangcafd6652010-04-29 12:09:01 -04001453 }
1454
1455 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001456}
1457
Dongxiao Xu7725b892010-05-11 18:29:38 +08001458static void kvm_cpu_vmxon(u64 addr)
1459{
1460 asm volatile (ASM_VMX_VMXON_RAX
1461 : : "a"(&addr), "m"(addr)
1462 : "memory", "cc");
1463}
1464
Alexander Graf10474ae2009-09-15 11:37:46 +02001465static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466{
1467 int cpu = raw_smp_processor_id();
1468 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
Shane Wangcafd6652010-04-29 12:09:01 -04001469 u64 old, test_bits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001470
Alexander Graf10474ae2009-09-15 11:37:46 +02001471 if (read_cr4() & X86_CR4_VMXE)
1472 return -EBUSY;
1473
Avi Kivity543e4242008-05-13 16:22:47 +03001474 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Shane Wangcafd6652010-04-29 12:09:01 -04001476
1477 test_bits = FEATURE_CONTROL_LOCKED;
1478 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
1479 if (tboot_enabled())
1480 test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
1481
1482 if ((old & test_bits) != test_bits) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 /* enable and lock */
Shane Wangcafd6652010-04-29 12:09:01 -04001484 wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
1485 }
Rusty Russell66aee912007-07-17 23:34:16 +10001486 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Alexander Graf10474ae2009-09-15 11:37:46 +02001487
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001488 if (vmm_exclusive) {
1489 kvm_cpu_vmxon(phys_addr);
1490 ept_sync_global();
1491 }
Alexander Graf10474ae2009-09-15 11:37:46 +02001492
Avi Kivity3444d7d2010-07-26 18:32:38 +03001493 store_gdt(&__get_cpu_var(host_gdt));
1494
Alexander Graf10474ae2009-09-15 11:37:46 +02001495 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496}
1497
Avi Kivity543e4242008-05-13 16:22:47 +03001498static void vmclear_local_vcpus(void)
1499{
1500 int cpu = raw_smp_processor_id();
1501 struct vcpu_vmx *vmx, *n;
1502
1503 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1504 local_vcpus_link)
1505 __vcpu_clear(vmx);
1506}
1507
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001508
1509/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1510 * tricks.
1511 */
1512static void kvm_cpu_vmxoff(void)
1513{
1514 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001515}
1516
Avi Kivity6aa8b732006-12-10 02:21:36 -08001517static void hardware_disable(void *garbage)
1518{
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08001519 if (vmm_exclusive) {
1520 vmclear_local_vcpus();
1521 kvm_cpu_vmxoff();
1522 }
Dongxiao Xu7725b892010-05-11 18:29:38 +08001523 write_cr4(read_cr4() & ~X86_CR4_VMXE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524}
1525
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001526static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001527 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528{
1529 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001530 u32 ctl = ctl_min | ctl_opt;
1531
1532 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1533
1534 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1535 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1536
1537 /* Ensure minimum (required) set of control bits are supported. */
1538 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001539 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001540
1541 *result = ctl;
1542 return 0;
1543}
1544
Avi Kivity110312c2010-12-21 12:54:20 +02001545static __init bool allow_1_setting(u32 msr, u32 ctl)
1546{
1547 u32 vmx_msr_low, vmx_msr_high;
1548
1549 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1550 return vmx_msr_high & ctl;
1551}
1552
Yang, Sheng002c7f72007-07-31 14:23:01 +03001553static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001554{
1555 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001556 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001557 u32 _pin_based_exec_control = 0;
1558 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001559 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001560 u32 _vmexit_control = 0;
1561 u32 _vmentry_control = 0;
1562
1563 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001564 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001565 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1566 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001567 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001568
Anthony Liguori443381a2010-12-06 10:53:38 -06001569 min =
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001570#ifdef CONFIG_X86_64
1571 CPU_BASED_CR8_LOAD_EXITING |
1572 CPU_BASED_CR8_STORE_EXITING |
1573#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001574 CPU_BASED_CR3_LOAD_EXITING |
1575 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001576 CPU_BASED_USE_IO_BITMAPS |
1577 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001578 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001579 CPU_BASED_MWAIT_EXITING |
1580 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001581 CPU_BASED_INVLPG_EXITING;
Anthony Liguori443381a2010-12-06 10:53:38 -06001582
1583 if (yield_on_hlt)
1584 min |= CPU_BASED_HLT_EXITING;
1585
Sheng Yangf78e0e22007-10-29 09:40:42 +08001586 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001587 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001588 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001589 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1590 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001591 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001592#ifdef CONFIG_X86_64
1593 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1594 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1595 ~CPU_BASED_CR8_STORE_EXITING;
1596#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001597 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001598 min2 = 0;
1599 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001600 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001601 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001602 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001603 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001604 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1605 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001606 if (adjust_vmx_controls(min2, opt2,
1607 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001608 &_cpu_based_2nd_exec_control) < 0)
1609 return -EIO;
1610 }
1611#ifndef CONFIG_X86_64
1612 if (!(_cpu_based_2nd_exec_control &
1613 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1614 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1615#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001616 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001617 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1618 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001619 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1620 CPU_BASED_CR3_STORE_EXITING |
1621 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001622 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1623 vmx_capability.ept, vmx_capability.vpid);
1624 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001625
1626 min = 0;
1627#ifdef CONFIG_X86_64
1628 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1629#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001630 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001631 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1632 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001633 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001634
Sheng Yang468d4722008-10-09 16:01:55 +08001635 min = 0;
1636 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001637 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1638 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001639 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001641 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001642
1643 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1644 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001645 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001646
1647#ifdef CONFIG_X86_64
1648 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1649 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001650 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001651#endif
1652
1653 /* Require Write-Back (WB) memory type for VMCS accesses. */
1654 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001655 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001656
Yang, Sheng002c7f72007-07-31 14:23:01 +03001657 vmcs_conf->size = vmx_msr_high & 0x1fff;
1658 vmcs_conf->order = get_order(vmcs_config.size);
1659 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001660
Yang, Sheng002c7f72007-07-31 14:23:01 +03001661 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1662 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001663 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001664 vmcs_conf->vmexit_ctrl = _vmexit_control;
1665 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001666
Avi Kivity110312c2010-12-21 12:54:20 +02001667 cpu_has_load_ia32_efer =
1668 allow_1_setting(MSR_IA32_VMX_ENTRY_CTLS,
1669 VM_ENTRY_LOAD_IA32_EFER)
1670 && allow_1_setting(MSR_IA32_VMX_EXIT_CTLS,
1671 VM_EXIT_LOAD_IA32_EFER);
1672
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001673 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001674}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001675
1676static struct vmcs *alloc_vmcs_cpu(int cpu)
1677{
1678 int node = cpu_to_node(cpu);
1679 struct page *pages;
1680 struct vmcs *vmcs;
1681
Mel Gorman6484eb32009-06-16 15:31:54 -07001682 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683 if (!pages)
1684 return NULL;
1685 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001686 memset(vmcs, 0, vmcs_config.size);
1687 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001688 return vmcs;
1689}
1690
1691static struct vmcs *alloc_vmcs(void)
1692{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001693 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001694}
1695
1696static void free_vmcs(struct vmcs *vmcs)
1697{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001698 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699}
1700
Sam Ravnborg39959582007-06-01 00:47:13 -07001701static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702{
1703 int cpu;
1704
Zachary Amsden3230bb42009-09-29 11:38:37 -10001705 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001707 per_cpu(vmxarea, cpu) = NULL;
1708 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709}
1710
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711static __init int alloc_kvm_area(void)
1712{
1713 int cpu;
1714
Zachary Amsden3230bb42009-09-29 11:38:37 -10001715 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716 struct vmcs *vmcs;
1717
1718 vmcs = alloc_vmcs_cpu(cpu);
1719 if (!vmcs) {
1720 free_kvm_area();
1721 return -ENOMEM;
1722 }
1723
1724 per_cpu(vmxarea, cpu) = vmcs;
1725 }
1726 return 0;
1727}
1728
1729static __init int hardware_setup(void)
1730{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001731 if (setup_vmcs_config(&vmcs_config) < 0)
1732 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001733
1734 if (boot_cpu_has(X86_FEATURE_NX))
1735 kvm_enable_efer_bits(EFER_NX);
1736
Sheng Yang93ba03c2009-04-01 15:52:32 +08001737 if (!cpu_has_vmx_vpid())
1738 enable_vpid = 0;
1739
Sheng Yang4bc9b982010-06-02 14:05:24 +08001740 if (!cpu_has_vmx_ept() ||
1741 !cpu_has_vmx_ept_4levels()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001742 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001743 enable_unrestricted_guest = 0;
1744 }
1745
1746 if (!cpu_has_vmx_unrestricted_guest())
1747 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001748
1749 if (!cpu_has_vmx_flexpriority())
1750 flexpriority_enabled = 0;
1751
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001752 if (!cpu_has_vmx_tpr_shadow())
1753 kvm_x86_ops->update_cr8_intercept = NULL;
1754
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001755 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1756 kvm_disable_largepages();
1757
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001758 if (!cpu_has_vmx_ple())
1759 ple_gap = 0;
1760
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761 return alloc_kvm_area();
1762}
1763
1764static __exit void hardware_unsetup(void)
1765{
1766 free_kvm_area();
1767}
1768
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1770{
1771 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1772
Avi Kivity6af11b92007-03-19 13:18:10 +02001773 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774 vmcs_write16(sf->selector, save->selector);
1775 vmcs_writel(sf->base, save->base);
1776 vmcs_write32(sf->limit, save->limit);
1777 vmcs_write32(sf->ar_bytes, save->ar);
1778 } else {
1779 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1780 << AR_DPL_SHIFT;
1781 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1782 }
1783}
1784
1785static void enter_pmode(struct kvm_vcpu *vcpu)
1786{
1787 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001788 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001789
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001790 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001791 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792
Avi Kivity2fb92db2011-04-27 19:42:18 +03001793 vmx_segment_cache_clear(vmx);
1794
Avi Kivityd0ba64f2011-01-03 14:28:51 +02001795 vmcs_write16(GUEST_TR_SELECTOR, vmx->rmode.tr.selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001796 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1797 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1798 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001799
1800 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001801 flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
1802 flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 vmcs_writel(GUEST_RFLAGS, flags);
1804
Rusty Russell66aee912007-07-17 23:34:16 +10001805 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1806 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807
1808 update_exception_bitmap(vcpu);
1809
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001810 if (emulate_invalid_guest_state)
1811 return;
1812
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001813 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1814 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1815 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1816 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001817
Avi Kivity2fb92db2011-04-27 19:42:18 +03001818 vmx_segment_cache_clear(vmx);
1819
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820 vmcs_write16(GUEST_SS_SELECTOR, 0);
1821 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1822
1823 vmcs_write16(GUEST_CS_SELECTOR,
1824 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1825 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1826}
1827
Mike Dayd77c26f2007-10-08 09:02:08 -04001828static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001830 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001831 struct kvm_memslots *slots;
1832 gfn_t base_gfn;
1833
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08001834 slots = kvm_memslots(kvm);
Avi Kivityf495c6e2010-06-10 17:21:29 +03001835 base_gfn = slots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001836 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02001837 return base_gfn << PAGE_SHIFT;
1838 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001839 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001840}
1841
1842static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1843{
1844 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1845
1846 save->selector = vmcs_read16(sf->selector);
1847 save->base = vmcs_readl(sf->base);
1848 save->limit = vmcs_read32(sf->limit);
1849 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001850 vmcs_write16(sf->selector, save->base >> 4);
Gleb Natapov444e8632010-12-27 17:25:04 +02001851 vmcs_write32(sf->base, save->base & 0xffff0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001852 vmcs_write32(sf->limit, 0xffff);
1853 vmcs_write32(sf->ar_bytes, 0xf3);
Gleb Natapov444e8632010-12-27 17:25:04 +02001854 if (save->base & 0xf)
1855 printk_once(KERN_WARNING "kvm: segment base is not paragraph"
1856 " aligned when entering protected mode (seg=%d)",
1857 seg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001858}
1859
1860static void enter_rmode(struct kvm_vcpu *vcpu)
1861{
1862 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001863 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001865 if (enable_unrestricted_guest)
1866 return;
1867
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001868 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001869 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001870
Gleb Natapov776e58e2011-03-13 12:34:27 +02001871 /*
1872 * Very old userspace does not call KVM_SET_TSS_ADDR before entering
1873 * vcpu. Call it here with phys address pointing 16M below 4G.
1874 */
1875 if (!vcpu->kvm->arch.tss_addr) {
1876 printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
1877 "called before entering vcpu\n");
1878 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
1879 vmx_set_tss_addr(vcpu->kvm, 0xfeffd000);
1880 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1881 }
1882
Avi Kivity2fb92db2011-04-27 19:42:18 +03001883 vmx_segment_cache_clear(vmx);
1884
Avi Kivityd0ba64f2011-01-03 14:28:51 +02001885 vmx->rmode.tr.selector = vmcs_read16(GUEST_TR_SELECTOR);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001886 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1888
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001889 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001890 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1891
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001892 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001893 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1894
1895 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity78ac8b42010-04-08 18:19:35 +03001896 vmx->rmode.save_rflags = flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001898 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899
1900 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001901 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902 update_exception_bitmap(vcpu);
1903
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001904 if (emulate_invalid_guest_state)
1905 goto continue_rmode;
1906
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1908 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1909 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1910
1911 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001912 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001913 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1914 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001915 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1916
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001917 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1918 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1919 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1920 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001921
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001922continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001923 kvm_mmu_reset_context(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001924}
1925
Amit Shah401d10d2009-02-20 22:53:37 +05301926static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1927{
1928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001929 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1930
1931 if (!msr)
1932 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301933
Avi Kivity44ea2b12009-09-06 15:55:37 +03001934 /*
1935 * Force kernel_gs_base reloading before EFER changes, as control
1936 * of this msr depends on is_long_mode().
1937 */
1938 vmx_load_host_state(to_vmx(vcpu));
Avi Kivityf6801df2010-01-21 15:31:50 +02001939 vcpu->arch.efer = efer;
Amit Shah401d10d2009-02-20 22:53:37 +05301940 if (efer & EFER_LMA) {
1941 vmcs_write32(VM_ENTRY_CONTROLS,
1942 vmcs_read32(VM_ENTRY_CONTROLS) |
1943 VM_ENTRY_IA32E_MODE);
1944 msr->data = efer;
1945 } else {
1946 vmcs_write32(VM_ENTRY_CONTROLS,
1947 vmcs_read32(VM_ENTRY_CONTROLS) &
1948 ~VM_ENTRY_IA32E_MODE);
1949
1950 msr->data = efer & ~EFER_LME;
1951 }
1952 setup_msrs(vmx);
1953}
1954
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001955#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001956
1957static void enter_lmode(struct kvm_vcpu *vcpu)
1958{
1959 u32 guest_tr_ar;
1960
Avi Kivity2fb92db2011-04-27 19:42:18 +03001961 vmx_segment_cache_clear(to_vmx(vcpu));
1962
Avi Kivity6aa8b732006-12-10 02:21:36 -08001963 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1964 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1965 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001966 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001967 vmcs_write32(GUEST_TR_AR_BYTES,
1968 (guest_tr_ar & ~AR_TYPE_MASK)
1969 | AR_TYPE_BUSY_64_TSS);
1970 }
Avi Kivityda38f432010-07-06 11:30:49 +03001971 vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972}
1973
1974static void exit_lmode(struct kvm_vcpu *vcpu)
1975{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001976 vmcs_write32(VM_ENTRY_CONTROLS,
1977 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001978 & ~VM_ENTRY_IA32E_MODE);
Avi Kivityda38f432010-07-06 11:30:49 +03001979 vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001980}
1981
1982#endif
1983
Sheng Yang2384d2b2008-01-17 15:14:33 +08001984static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1985{
Gui Jianfengb9d762f2010-06-07 10:32:29 +08001986 vpid_sync_context(to_vmx(vcpu));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08001987 if (enable_ept) {
1988 if (!VALID_PAGE(vcpu->arch.mmu.root_hpa))
1989 return;
Sheng Yang4e1096d2008-07-06 19:16:51 +08001990 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Xiao Guangrongdd180b32010-07-03 16:02:42 +08001991 }
Sheng Yang2384d2b2008-01-17 15:14:33 +08001992}
1993
Avi Kivitye8467fd2009-12-29 18:43:06 +02001994static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1995{
1996 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
1997
1998 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
1999 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
2000}
2001
Avi Kivityaff48ba2010-12-05 18:56:11 +02002002static void vmx_decache_cr3(struct kvm_vcpu *vcpu)
2003{
2004 if (enable_ept && is_paging(vcpu))
2005 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
2006 __set_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail);
2007}
2008
Anthony Liguori25c4c272007-04-27 09:29:21 +03002009static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08002010{
Avi Kivityfc78f512009-12-07 12:16:48 +02002011 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
2012
2013 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
2014 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08002015}
2016
Sheng Yang14394422008-04-28 12:24:45 +08002017static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
2018{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002019 if (!test_bit(VCPU_EXREG_PDPTR,
2020 (unsigned long *)&vcpu->arch.regs_dirty))
2021 return;
2022
Sheng Yang14394422008-04-28 12:24:45 +08002023 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002024 vmcs_write64(GUEST_PDPTR0, vcpu->arch.mmu.pdptrs[0]);
2025 vmcs_write64(GUEST_PDPTR1, vcpu->arch.mmu.pdptrs[1]);
2026 vmcs_write64(GUEST_PDPTR2, vcpu->arch.mmu.pdptrs[2]);
2027 vmcs_write64(GUEST_PDPTR3, vcpu->arch.mmu.pdptrs[3]);
Sheng Yang14394422008-04-28 12:24:45 +08002028 }
2029}
2030
Avi Kivity8f5d5492009-05-31 18:41:29 +03002031static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
2032{
2033 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Joerg Roedelff03a072010-09-10 17:30:57 +02002034 vcpu->arch.mmu.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
2035 vcpu->arch.mmu.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
2036 vcpu->arch.mmu.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
2037 vcpu->arch.mmu.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002038 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03002039
2040 __set_bit(VCPU_EXREG_PDPTR,
2041 (unsigned long *)&vcpu->arch.regs_avail);
2042 __set_bit(VCPU_EXREG_PDPTR,
2043 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03002044}
2045
Sheng Yang14394422008-04-28 12:24:45 +08002046static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
2047
2048static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
2049 unsigned long cr0,
2050 struct kvm_vcpu *vcpu)
2051{
Marcelo Tosatti5233dd52011-06-06 14:27:47 -03002052 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
2053 vmx_decache_cr3(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002054 if (!(cr0 & X86_CR0_PG)) {
2055 /* From paging/starting to nonpaging */
2056 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002057 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08002058 (CPU_BASED_CR3_LOAD_EXITING |
2059 CPU_BASED_CR3_STORE_EXITING));
2060 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002061 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002062 } else if (!is_paging(vcpu)) {
2063 /* From nonpaging to paging */
2064 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08002065 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08002066 ~(CPU_BASED_CR3_LOAD_EXITING |
2067 CPU_BASED_CR3_STORE_EXITING));
2068 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02002069 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08002070 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08002071
2072 if (!(cr0 & X86_CR0_WP))
2073 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08002074}
2075
Avi Kivity6aa8b732006-12-10 02:21:36 -08002076static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
2077{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002078 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002079 unsigned long hw_cr0;
2080
2081 if (enable_unrestricted_guest)
2082 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
2083 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
2084 else
2085 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08002086
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002087 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002088 enter_pmode(vcpu);
2089
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002090 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091 enter_rmode(vcpu);
2092
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002093#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02002094 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10002095 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002096 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10002097 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002098 exit_lmode(vcpu);
2099 }
2100#endif
2101
Avi Kivity089d0342009-03-23 18:26:32 +02002102 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002103 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
2104
Avi Kivity02daab22009-12-30 12:40:26 +02002105 if (!vcpu->fpu_active)
Avi Kivity81231c62010-01-24 16:26:40 +02002106 hw_cr0 |= X86_CR0_TS | X86_CR0_MP;
Avi Kivity02daab22009-12-30 12:40:26 +02002107
Avi Kivity6aa8b732006-12-10 02:21:36 -08002108 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08002109 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002110 vcpu->arch.cr0 = cr0;
Avi Kivity69c73022011-03-07 15:26:44 +02002111 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002112}
2113
Sheng Yang14394422008-04-28 12:24:45 +08002114static u64 construct_eptp(unsigned long root_hpa)
2115{
2116 u64 eptp;
2117
2118 /* TODO write the value reading from MSR */
2119 eptp = VMX_EPT_DEFAULT_MT |
2120 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
2121 eptp |= (root_hpa & PAGE_MASK);
2122
2123 return eptp;
2124}
2125
Avi Kivity6aa8b732006-12-10 02:21:36 -08002126static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
2127{
Sheng Yang14394422008-04-28 12:24:45 +08002128 unsigned long guest_cr3;
2129 u64 eptp;
2130
2131 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02002132 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08002133 eptp = construct_eptp(cr3);
2134 vmcs_write64(EPT_POINTER, eptp);
Avi Kivity9f8fe502010-12-05 17:30:00 +02002135 guest_cr3 = is_paging(vcpu) ? kvm_read_cr3(vcpu) :
Sheng Yangb927a3c2009-07-21 10:42:48 +08002136 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02002137 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002138 }
2139
Sheng Yang2384d2b2008-01-17 15:14:33 +08002140 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08002141 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002142}
2143
2144static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
2145{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002146 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08002147 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
2148
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002149 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02002150 if (enable_ept) {
2151 if (!is_paging(vcpu)) {
2152 hw_cr4 &= ~X86_CR4_PAE;
2153 hw_cr4 |= X86_CR4_PSE;
2154 } else if (!(cr4 & X86_CR4_PAE)) {
2155 hw_cr4 &= ~X86_CR4_PAE;
2156 }
2157 }
Sheng Yang14394422008-04-28 12:24:45 +08002158
2159 vmcs_writel(CR4_READ_SHADOW, cr4);
2160 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161}
2162
Avi Kivity6aa8b732006-12-10 02:21:36 -08002163static void vmx_get_segment(struct kvm_vcpu *vcpu,
2164 struct kvm_segment *var, int seg)
2165{
Avi Kivitya9179492011-01-03 14:28:52 +02002166 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitya9179492011-01-03 14:28:52 +02002167 struct kvm_save_segment *save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002168 u32 ar;
2169
Avi Kivitya9179492011-01-03 14:28:52 +02002170 if (vmx->rmode.vm86_active
2171 && (seg == VCPU_SREG_TR || seg == VCPU_SREG_ES
2172 || seg == VCPU_SREG_DS || seg == VCPU_SREG_FS
2173 || seg == VCPU_SREG_GS)
2174 && !emulate_invalid_guest_state) {
2175 switch (seg) {
2176 case VCPU_SREG_TR: save = &vmx->rmode.tr; break;
2177 case VCPU_SREG_ES: save = &vmx->rmode.es; break;
2178 case VCPU_SREG_DS: save = &vmx->rmode.ds; break;
2179 case VCPU_SREG_FS: save = &vmx->rmode.fs; break;
2180 case VCPU_SREG_GS: save = &vmx->rmode.gs; break;
2181 default: BUG();
2182 }
2183 var->selector = save->selector;
2184 var->base = save->base;
2185 var->limit = save->limit;
2186 ar = save->ar;
2187 if (seg == VCPU_SREG_TR
Avi Kivity2fb92db2011-04-27 19:42:18 +03002188 || var->selector == vmx_read_guest_seg_selector(vmx, seg))
Avi Kivitya9179492011-01-03 14:28:52 +02002189 goto use_saved_rmode_seg;
2190 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002191 var->base = vmx_read_guest_seg_base(vmx, seg);
2192 var->limit = vmx_read_guest_seg_limit(vmx, seg);
2193 var->selector = vmx_read_guest_seg_selector(vmx, seg);
2194 ar = vmx_read_guest_seg_ar(vmx, seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002195use_saved_rmode_seg:
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02002196 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002197 ar = 0;
2198 var->type = ar & 15;
2199 var->s = (ar >> 4) & 1;
2200 var->dpl = (ar >> 5) & 3;
2201 var->present = (ar >> 7) & 1;
2202 var->avl = (ar >> 12) & 1;
2203 var->l = (ar >> 13) & 1;
2204 var->db = (ar >> 14) & 1;
2205 var->g = (ar >> 15) & 1;
2206 var->unusable = (ar >> 16) & 1;
2207}
2208
Avi Kivitya9179492011-01-03 14:28:52 +02002209static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
2210{
Avi Kivitya9179492011-01-03 14:28:52 +02002211 struct kvm_segment s;
2212
2213 if (to_vmx(vcpu)->rmode.vm86_active) {
2214 vmx_get_segment(vcpu, &s, seg);
2215 return s.base;
2216 }
Avi Kivity2fb92db2011-04-27 19:42:18 +03002217 return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
Avi Kivitya9179492011-01-03 14:28:52 +02002218}
2219
Avi Kivity69c73022011-03-07 15:26:44 +02002220static int __vmx_get_cpl(struct kvm_vcpu *vcpu)
Izik Eidus2e4d2652008-03-24 19:38:34 +02002221{
Avi Kivity3eeb3282010-01-21 15:31:48 +02002222 if (!is_protmode(vcpu))
Izik Eidus2e4d2652008-03-24 19:38:34 +02002223 return 0;
2224
Avi Kivityf4c63e52011-03-07 14:54:28 +02002225 if (!is_long_mode(vcpu)
2226 && (kvm_get_rflags(vcpu) & X86_EFLAGS_VM)) /* if virtual 8086 */
Izik Eidus2e4d2652008-03-24 19:38:34 +02002227 return 3;
2228
Avi Kivity2fb92db2011-04-27 19:42:18 +03002229 return vmx_read_guest_seg_selector(to_vmx(vcpu), VCPU_SREG_CS) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02002230}
2231
Avi Kivity69c73022011-03-07 15:26:44 +02002232static int vmx_get_cpl(struct kvm_vcpu *vcpu)
2233{
2234 if (!test_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail)) {
2235 __set_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
2236 to_vmx(vcpu)->cpl = __vmx_get_cpl(vcpu);
2237 }
2238 return to_vmx(vcpu)->cpl;
2239}
2240
2241
Avi Kivity653e3102007-05-07 10:55:37 +03002242static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002243{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002244 u32 ar;
2245
Avi Kivity653e3102007-05-07 10:55:37 +03002246 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002247 ar = 1 << 16;
2248 else {
2249 ar = var->type & 15;
2250 ar |= (var->s & 1) << 4;
2251 ar |= (var->dpl & 3) << 5;
2252 ar |= (var->present & 1) << 7;
2253 ar |= (var->avl & 1) << 12;
2254 ar |= (var->l & 1) << 13;
2255 ar |= (var->db & 1) << 14;
2256 ar |= (var->g & 1) << 15;
2257 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08002258 if (ar == 0) /* a 0 value means unusable */
2259 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03002260
2261 return ar;
2262}
2263
2264static void vmx_set_segment(struct kvm_vcpu *vcpu,
2265 struct kvm_segment *var, int seg)
2266{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002267 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03002268 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
2269 u32 ar;
2270
Avi Kivity2fb92db2011-04-27 19:42:18 +03002271 vmx_segment_cache_clear(vmx);
2272
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002273 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
Gleb Natapova8ba6c22011-02-21 12:07:58 +02002274 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002275 vmx->rmode.tr.selector = var->selector;
2276 vmx->rmode.tr.base = var->base;
2277 vmx->rmode.tr.limit = var->limit;
2278 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03002279 return;
2280 }
2281 vmcs_writel(sf->base, var->base);
2282 vmcs_write32(sf->limit, var->limit);
2283 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002284 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03002285 /*
2286 * Hack real-mode segments into vm86 compatibility.
2287 */
2288 if (var->base == 0xffff0000 && var->selector == 0xf000)
2289 vmcs_writel(sf->base, 0xf0000);
2290 ar = 0xf3;
2291 } else
2292 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002293
2294 /*
2295 * Fix the "Accessed" bit in AR field of segment registers for older
2296 * qemu binaries.
2297 * IA32 arch specifies that at the time of processor reset the
2298 * "Accessed" bit in the AR field of segment registers is 1. And qemu
2299 * is setting it to 0 in the usedland code. This causes invalid guest
2300 * state vmexit when "unrestricted guest" mode is turned on.
2301 * Fix for this setup issue in cpu_reset is being pushed in the qemu
2302 * tree. Newer qemu binaries with that qemu fix would not need this
2303 * kvm hack.
2304 */
2305 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
2306 ar |= 0x1; /* Accessed */
2307
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity69c73022011-03-07 15:26:44 +02002309 __clear_bit(VCPU_EXREG_CPL, (ulong *)&vcpu->arch.regs_avail);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002310}
2311
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
2313{
Avi Kivity2fb92db2011-04-27 19:42:18 +03002314 u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002315
2316 *db = (ar >> 14) & 1;
2317 *l = (ar >> 13) & 1;
2318}
2319
Gleb Natapov89a27f42010-02-16 10:51:48 +02002320static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002322 dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
2323 dt->address = vmcs_readl(GUEST_IDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002324}
2325
Gleb Natapov89a27f42010-02-16 10:51:48 +02002326static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002328 vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
2329 vmcs_writel(GUEST_IDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002330}
2331
Gleb Natapov89a27f42010-02-16 10:51:48 +02002332static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002334 dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
2335 dt->address = vmcs_readl(GUEST_GDTR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336}
2337
Gleb Natapov89a27f42010-02-16 10:51:48 +02002338static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002339{
Gleb Natapov89a27f42010-02-16 10:51:48 +02002340 vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
2341 vmcs_writel(GUEST_GDTR_BASE, dt->address);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342}
2343
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002344static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
2345{
2346 struct kvm_segment var;
2347 u32 ar;
2348
2349 vmx_get_segment(vcpu, &var, seg);
2350 ar = vmx_segment_access_rights(&var);
2351
2352 if (var.base != (var.selector << 4))
2353 return false;
2354 if (var.limit != 0xffff)
2355 return false;
2356 if (ar != 0xf3)
2357 return false;
2358
2359 return true;
2360}
2361
2362static bool code_segment_valid(struct kvm_vcpu *vcpu)
2363{
2364 struct kvm_segment cs;
2365 unsigned int cs_rpl;
2366
2367 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2368 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
2369
Avi Kivity1872a3f2009-01-04 23:26:52 +02002370 if (cs.unusable)
2371 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002372 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
2373 return false;
2374 if (!cs.s)
2375 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002376 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002377 if (cs.dpl > cs_rpl)
2378 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002379 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002380 if (cs.dpl != cs_rpl)
2381 return false;
2382 }
2383 if (!cs.present)
2384 return false;
2385
2386 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
2387 return true;
2388}
2389
2390static bool stack_segment_valid(struct kvm_vcpu *vcpu)
2391{
2392 struct kvm_segment ss;
2393 unsigned int ss_rpl;
2394
2395 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2396 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2397
Avi Kivity1872a3f2009-01-04 23:26:52 +02002398 if (ss.unusable)
2399 return true;
2400 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002401 return false;
2402 if (!ss.s)
2403 return false;
2404 if (ss.dpl != ss_rpl) /* DPL != RPL */
2405 return false;
2406 if (!ss.present)
2407 return false;
2408
2409 return true;
2410}
2411
2412static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2413{
2414 struct kvm_segment var;
2415 unsigned int rpl;
2416
2417 vmx_get_segment(vcpu, &var, seg);
2418 rpl = var.selector & SELECTOR_RPL_MASK;
2419
Avi Kivity1872a3f2009-01-04 23:26:52 +02002420 if (var.unusable)
2421 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002422 if (!var.s)
2423 return false;
2424 if (!var.present)
2425 return false;
2426 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2427 if (var.dpl < rpl) /* DPL < RPL */
2428 return false;
2429 }
2430
2431 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2432 * rights flags
2433 */
2434 return true;
2435}
2436
2437static bool tr_valid(struct kvm_vcpu *vcpu)
2438{
2439 struct kvm_segment tr;
2440
2441 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2442
Avi Kivity1872a3f2009-01-04 23:26:52 +02002443 if (tr.unusable)
2444 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002445 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2446 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002447 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002448 return false;
2449 if (!tr.present)
2450 return false;
2451
2452 return true;
2453}
2454
2455static bool ldtr_valid(struct kvm_vcpu *vcpu)
2456{
2457 struct kvm_segment ldtr;
2458
2459 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2460
Avi Kivity1872a3f2009-01-04 23:26:52 +02002461 if (ldtr.unusable)
2462 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002463 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2464 return false;
2465 if (ldtr.type != 2)
2466 return false;
2467 if (!ldtr.present)
2468 return false;
2469
2470 return true;
2471}
2472
2473static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2474{
2475 struct kvm_segment cs, ss;
2476
2477 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2478 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2479
2480 return ((cs.selector & SELECTOR_RPL_MASK) ==
2481 (ss.selector & SELECTOR_RPL_MASK));
2482}
2483
2484/*
2485 * Check if guest state is valid. Returns true if valid, false if
2486 * not.
2487 * We assume that registers are always usable
2488 */
2489static bool guest_state_valid(struct kvm_vcpu *vcpu)
2490{
2491 /* real mode guest state checks */
Avi Kivity3eeb3282010-01-21 15:31:48 +02002492 if (!is_protmode(vcpu)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002493 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2494 return false;
2495 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2496 return false;
2497 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2498 return false;
2499 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2500 return false;
2501 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2502 return false;
2503 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2504 return false;
2505 } else {
2506 /* protected mode guest state checks */
2507 if (!cs_ss_rpl_check(vcpu))
2508 return false;
2509 if (!code_segment_valid(vcpu))
2510 return false;
2511 if (!stack_segment_valid(vcpu))
2512 return false;
2513 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2514 return false;
2515 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2516 return false;
2517 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2518 return false;
2519 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2520 return false;
2521 if (!tr_valid(vcpu))
2522 return false;
2523 if (!ldtr_valid(vcpu))
2524 return false;
2525 }
2526 /* TODO:
2527 * - Add checks on RIP
2528 * - Add checks on RFLAGS
2529 */
2530
2531 return true;
2532}
2533
Mike Dayd77c26f2007-10-08 09:02:08 -04002534static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002536 gfn_t fn;
Izik Eidus195aefd2007-10-01 22:14:18 +02002537 u16 data = 0;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002538 int r, idx, ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002540 idx = srcu_read_lock(&kvm->srcu);
2541 fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002542 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2543 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002544 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002545 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002546 r = kvm_write_guest_page(kvm, fn++, &data,
2547 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002548 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002549 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002550 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2551 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002552 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002553 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2554 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002555 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002556 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002557 r = kvm_write_guest_page(kvm, fn, &data,
2558 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2559 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002560 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002561 goto out;
2562
2563 ret = 1;
2564out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002565 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002566 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567}
2568
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002569static int init_rmode_identity_map(struct kvm *kvm)
2570{
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002571 int i, idx, r, ret;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002572 pfn_t identity_map_pfn;
2573 u32 tmp;
2574
Avi Kivity089d0342009-03-23 18:26:32 +02002575 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002576 return 1;
2577 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2578 printk(KERN_ERR "EPT: identity-mapping pagetable "
2579 "haven't been allocated!\n");
2580 return 0;
2581 }
2582 if (likely(kvm->arch.ept_identity_pagetable_done))
2583 return 1;
2584 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002585 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002586 idx = srcu_read_lock(&kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002587 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2588 if (r < 0)
2589 goto out;
2590 /* Set up identity-mapping pagetable for EPT in real mode */
2591 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2592 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2593 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2594 r = kvm_write_guest_page(kvm, identity_map_pfn,
2595 &tmp, i * sizeof(tmp), sizeof(tmp));
2596 if (r < 0)
2597 goto out;
2598 }
2599 kvm->arch.ept_identity_pagetable_done = true;
2600 ret = 1;
2601out:
Xiao Guangrong40dcaa92011-03-09 15:41:04 +08002602 srcu_read_unlock(&kvm->srcu, idx);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002603 return ret;
2604}
2605
Avi Kivity6aa8b732006-12-10 02:21:36 -08002606static void seg_setup(int seg)
2607{
2608 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002609 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610
2611 vmcs_write16(sf->selector, 0);
2612 vmcs_writel(sf->base, 0);
2613 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002614 if (enable_unrestricted_guest) {
2615 ar = 0x93;
2616 if (seg == VCPU_SREG_CS)
2617 ar |= 0x08; /* code segment */
2618 } else
2619 ar = 0xf3;
2620
2621 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002622}
2623
Sheng Yangf78e0e22007-10-29 09:40:42 +08002624static int alloc_apic_access_page(struct kvm *kvm)
2625{
2626 struct kvm_userspace_memory_region kvm_userspace_mem;
2627 int r = 0;
2628
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002629 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002630 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002631 goto out;
2632 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2633 kvm_userspace_mem.flags = 0;
2634 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2635 kvm_userspace_mem.memory_size = PAGE_SIZE;
2636 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2637 if (r)
2638 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002639
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002640 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002641out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002642 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002643 return r;
2644}
2645
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002646static int alloc_identity_pagetable(struct kvm *kvm)
2647{
2648 struct kvm_userspace_memory_region kvm_userspace_mem;
2649 int r = 0;
2650
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002651 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002652 if (kvm->arch.ept_identity_pagetable)
2653 goto out;
2654 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2655 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002656 kvm_userspace_mem.guest_phys_addr =
2657 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002658 kvm_userspace_mem.memory_size = PAGE_SIZE;
2659 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2660 if (r)
2661 goto out;
2662
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002663 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002664 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002665out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002666 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002667 return r;
2668}
2669
Sheng Yang2384d2b2008-01-17 15:14:33 +08002670static void allocate_vpid(struct vcpu_vmx *vmx)
2671{
2672 int vpid;
2673
2674 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002675 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002676 return;
2677 spin_lock(&vmx_vpid_lock);
2678 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2679 if (vpid < VMX_NR_VPIDS) {
2680 vmx->vpid = vpid;
2681 __set_bit(vpid, vmx_vpid_bitmap);
2682 }
2683 spin_unlock(&vmx_vpid_lock);
2684}
2685
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08002686static void free_vpid(struct vcpu_vmx *vmx)
2687{
2688 if (!enable_vpid)
2689 return;
2690 spin_lock(&vmx_vpid_lock);
2691 if (vmx->vpid != 0)
2692 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
2693 spin_unlock(&vmx_vpid_lock);
2694}
2695
Avi Kivity58972972009-02-24 22:26:47 +02002696static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002697{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002698 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002699
2700 if (!cpu_has_vmx_msr_bitmap())
2701 return;
2702
2703 /*
2704 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2705 * have the write-low and read-high bitmap offsets the wrong way round.
2706 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2707 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002708 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002709 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2710 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002711 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2712 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002713 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2714 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002715 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002716}
2717
Avi Kivity58972972009-02-24 22:26:47 +02002718static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2719{
2720 if (!longmode_only)
2721 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2722 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2723}
2724
Avi Kivity6aa8b732006-12-10 02:21:36 -08002725/*
2726 * Sets up the vmcs for emulated real mode.
2727 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002728static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002729{
Sheng Yang468d4722008-10-09 16:01:55 +08002730 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 u32 junk;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002732 u64 host_pat;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 unsigned long a;
Gleb Natapov89a27f42010-02-16 10:51:48 +02002734 struct desc_ptr dt;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002736 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002737 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002740 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2741 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742
Sheng Yang25c5f222008-03-28 13:18:56 +08002743 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002744 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002745
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2747
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002749 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2750 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002751
2752 exec_control = vmcs_config.cpu_based_exec_ctrl;
2753 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2754 exec_control &= ~CPU_BASED_TPR_SHADOW;
2755#ifdef CONFIG_X86_64
2756 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2757 CPU_BASED_CR8_LOAD_EXITING;
2758#endif
2759 }
Avi Kivity089d0342009-03-23 18:26:32 +02002760 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002761 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002762 CPU_BASED_CR3_LOAD_EXITING |
2763 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002764 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765
Sheng Yang83ff3b92007-11-21 14:33:25 +08002766 if (cpu_has_secondary_exec_ctrls()) {
2767 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2768 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2769 exec_control &=
2770 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002771 if (vmx->vpid == 0)
2772 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002773 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002774 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002775 enable_unrestricted_guest = 0;
2776 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002777 if (!enable_unrestricted_guest)
2778 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002779 if (!ple_gap)
2780 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002781 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2782 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002783
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002784 if (ple_gap) {
2785 vmcs_write32(PLE_GAP, ple_gap);
2786 vmcs_write32(PLE_WINDOW, ple_window);
2787 }
2788
Avi Kivityc7addb92007-09-16 18:58:32 +02002789 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2790 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002791 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2792
Avi Kivity1c11e712010-05-03 16:05:44 +03002793 vmcs_writel(HOST_CR0, read_cr0() | X86_CR0_TS); /* 22.2.3 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002794 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2795 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2796
2797 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2798 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2799 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity9581d442010-10-19 16:46:55 +02002800 vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
2801 vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002803#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 rdmsrl(MSR_FS_BASE, a);
2805 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2806 rdmsrl(MSR_GS_BASE, a);
2807 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2808#else
2809 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2810 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2811#endif
2812
2813 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2814
Wei Yongjunec687982010-03-05 12:11:48 +08002815 native_store_idt(&dt);
Gleb Natapov89a27f42010-02-16 10:51:48 +02002816 vmcs_writel(HOST_IDTR_BASE, dt.address); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817
Mike Dayd77c26f2007-10-08 09:02:08 -04002818 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002819 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002820 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2821 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002822 vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host));
Eddie Dong2cc51562007-05-21 07:28:09 +03002823 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity61d2ef22010-04-28 16:40:38 +03002824 vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002825
2826 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2827 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2828 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2829 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2830 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2831 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2832
Sheng Yang468d4722008-10-09 16:01:55 +08002833 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2834 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2835 host_pat = msr_low | ((u64) msr_high << 32);
2836 vmcs_write64(HOST_IA32_PAT, host_pat);
2837 }
2838 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2839 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2840 host_pat = msr_low | ((u64) msr_high << 32);
2841 /* Write the default value follow host pat */
2842 vmcs_write64(GUEST_IA32_PAT, host_pat);
2843 /* Keep arch.pat sync with GUEST_IA32_PAT */
2844 vmx->vcpu.arch.pat = host_pat;
2845 }
2846
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 for (i = 0; i < NR_VMX_MSR; ++i) {
2848 u32 index = vmx_msr_index[i];
2849 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002850 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002851
2852 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2853 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002854 if (wrmsr_safe(index, data_low, data_high) < 0)
2855 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03002856 vmx->guest_msrs[j].index = i;
2857 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002858 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002859 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002862 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863
2864 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002865 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2866
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002867 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02002868 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02002869 if (enable_ept)
2870 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02002871 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002872
Zachary Amsden99e3e302010-08-19 22:07:17 -10002873 kvm_write_tsc(&vmx->vcpu, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002874
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002875 return 0;
2876}
2877
2878static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2879{
2880 struct vcpu_vmx *vmx = to_vmx(vcpu);
2881 u64 msr;
Xiao Guangrong4b9d3a02010-06-08 10:15:51 +08002882 int ret;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002883
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002884 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002885
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002886 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002887
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002888 vmx->soft_vnmi_blocked = 0;
2889
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002890 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002891 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002892 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002893 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002894 msr |= MSR_IA32_APICBASE_BSP;
2895 kvm_set_apic_base(&vmx->vcpu, msr);
2896
Jan Kiszka10ab25c2010-05-25 16:01:50 +02002897 ret = fx_init(&vmx->vcpu);
2898 if (ret != 0)
2899 goto out;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002900
Avi Kivity2fb92db2011-04-27 19:42:18 +03002901 vmx_segment_cache_clear(vmx);
2902
Avi Kivity5706be02008-08-20 15:07:31 +03002903 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002904 /*
2905 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2906 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2907 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002908 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002909 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2910 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2911 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002912 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2913 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002914 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002915
2916 seg_setup(VCPU_SREG_DS);
2917 seg_setup(VCPU_SREG_ES);
2918 seg_setup(VCPU_SREG_FS);
2919 seg_setup(VCPU_SREG_GS);
2920 seg_setup(VCPU_SREG_SS);
2921
2922 vmcs_write16(GUEST_TR_SELECTOR, 0);
2923 vmcs_writel(GUEST_TR_BASE, 0);
2924 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2925 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2926
2927 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2928 vmcs_writel(GUEST_LDTR_BASE, 0);
2929 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2930 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2931
2932 vmcs_write32(GUEST_SYSENTER_CS, 0);
2933 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2934 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2935
2936 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002937 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002938 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002939 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002940 kvm_rip_write(vcpu, 0);
2941 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002942
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002943 vmcs_writel(GUEST_DR7, 0x400);
2944
2945 vmcs_writel(GUEST_GDTR_BASE, 0);
2946 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2947
2948 vmcs_writel(GUEST_IDTR_BASE, 0);
2949 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2950
Anthony Liguori443381a2010-12-06 10:53:38 -06002951 vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002952 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2953 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2954
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002955 /* Special registers */
2956 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2957
2958 setup_msrs(vmx);
2959
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2961
Sheng Yangf78e0e22007-10-29 09:40:42 +08002962 if (cpu_has_vmx_tpr_shadow()) {
2963 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2964 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2965 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Takuya Yoshikawaafc20182011-03-05 12:40:20 +09002966 __pa(vmx->vcpu.arch.apic->regs));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002967 vmcs_write32(TPR_THRESHOLD, 0);
2968 }
2969
2970 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2971 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002972 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002973
Sheng Yang2384d2b2008-01-17 15:14:33 +08002974 if (vmx->vpid != 0)
2975 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2976
Eduardo Habkostfa400522009-10-24 02:49:58 -02002977 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002978 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002979 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002980 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002981 vmx_fpu_activate(&vmx->vcpu);
2982 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983
Gui Jianfengb9d762f2010-06-07 10:32:29 +08002984 vpid_sync_context(vmx);
Sheng Yang2384d2b2008-01-17 15:14:33 +08002985
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002986 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002988 /* HACK: Don't enable emulation on guest boot/reset */
2989 vmx->emulation_required = 0;
2990
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991out:
2992 return ret;
2993}
2994
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002995static void enable_irq_window(struct kvm_vcpu *vcpu)
2996{
2997 u32 cpu_based_vm_exec_control;
2998
2999 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3000 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
3001 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3002}
3003
3004static void enable_nmi_window(struct kvm_vcpu *vcpu)
3005{
3006 u32 cpu_based_vm_exec_control;
3007
3008 if (!cpu_has_virtual_nmis()) {
3009 enable_irq_window(vcpu);
3010 return;
3011 }
3012
Avi Kivity30bd0c42010-11-01 23:20:48 +02003013 if (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
3014 enable_irq_window(vcpu);
3015 return;
3016 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003017 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3018 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
3019 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3020}
3021
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003022static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03003023{
Avi Kivity9c8cba32007-11-22 11:42:59 +02003024 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003025 uint32_t intr;
3026 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02003027
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003028 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003029
Avi Kivityfa89a812008-09-01 15:57:51 +03003030 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003031 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003032 int inc_eip = 0;
3033 if (vcpu->arch.interrupt.soft)
3034 inc_eip = vcpu->arch.event_exit_inst_len;
3035 if (kvm_inject_realmode_interrupt(vcpu, irq, inc_eip) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003036 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003037 return;
3038 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003039 intr = irq | INTR_INFO_VALID_MASK;
3040 if (vcpu->arch.interrupt.soft) {
3041 intr |= INTR_TYPE_SOFT_INTR;
3042 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
3043 vmx->vcpu.arch.event_exit_inst_len);
3044 } else
3045 intr |= INTR_TYPE_EXT_INTR;
3046 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Anthony Liguori443381a2010-12-06 10:53:38 -06003047 vmx_clear_hlt(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003048}
3049
Sheng Yangf08864b2008-05-15 18:23:25 +08003050static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
3051{
Jan Kiszka66a5a342008-09-26 09:30:51 +02003052 struct vcpu_vmx *vmx = to_vmx(vcpu);
3053
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003054 if (!cpu_has_virtual_nmis()) {
3055 /*
3056 * Tracking the NMI-blocked state in software is built upon
3057 * finding the next open IRQ window. This, in turn, depends on
3058 * well-behaving guests: They have to keep IRQs disabled at
3059 * least as long as the NMI handler runs. Otherwise we may
3060 * cause NMI nesting, maybe breaking the guest. But as this is
3061 * highly unlikely, we can live with the residual risk.
3062 */
3063 vmx->soft_vnmi_blocked = 1;
3064 vmx->vnmi_blocked_time = 0;
3065 }
3066
Jan Kiszka487b3912008-09-26 09:30:56 +02003067 ++vcpu->stat.nmi_injections;
Avi Kivity9d58b932011-03-07 16:52:07 +02003068 vmx->nmi_known_unmasked = false;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003069 if (vmx->rmode.vm86_active) {
Serge E. Hallyn71f98332011-04-13 09:12:54 -05003070 if (kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0) != EMULATE_DONE)
Mohammed Gamala92601b2010-09-19 14:34:07 +02003071 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Jan Kiszka66a5a342008-09-26 09:30:51 +02003072 return;
3073 }
Sheng Yangf08864b2008-05-15 18:23:25 +08003074 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
3075 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Anthony Liguori443381a2010-12-06 10:53:38 -06003076 vmx_clear_hlt(vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003077}
3078
Gleb Natapovc4282df2009-04-21 17:45:07 +03003079static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02003080{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003081 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03003082 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02003083
Gleb Natapovc4282df2009-04-21 17:45:07 +03003084 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
Avi Kivity30bd0c42010-11-01 23:20:48 +02003085 (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
3086 | GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02003087}
3088
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003089static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
3090{
3091 if (!cpu_has_virtual_nmis())
3092 return to_vmx(vcpu)->soft_vnmi_blocked;
Avi Kivity9d58b932011-03-07 16:52:07 +02003093 if (to_vmx(vcpu)->nmi_known_unmasked)
3094 return false;
Avi Kivityc332c832010-05-04 12:24:12 +03003095 return vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003096}
3097
3098static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3099{
3100 struct vcpu_vmx *vmx = to_vmx(vcpu);
3101
3102 if (!cpu_has_virtual_nmis()) {
3103 if (vmx->soft_vnmi_blocked != masked) {
3104 vmx->soft_vnmi_blocked = masked;
3105 vmx->vnmi_blocked_time = 0;
3106 }
3107 } else {
Avi Kivity9d58b932011-03-07 16:52:07 +02003108 vmx->nmi_known_unmasked = !masked;
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003109 if (masked)
3110 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3111 GUEST_INTR_STATE_NMI);
3112 else
3113 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3114 GUEST_INTR_STATE_NMI);
3115 }
3116}
3117
Gleb Natapov78646122009-03-23 12:12:11 +02003118static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
3119{
Gleb Natapovc4282df2009-04-21 17:45:07 +03003120 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
3121 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
3122 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02003123}
3124
Izik Eiduscbc94022007-10-25 00:29:55 +02003125static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
3126{
3127 int ret;
3128 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08003129 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02003130 .guest_phys_addr = addr,
3131 .memory_size = PAGE_SIZE * 3,
3132 .flags = 0,
3133 };
3134
3135 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
3136 if (ret)
3137 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08003138 kvm->arch.tss_addr = addr;
Gleb Natapov93ea5382011-02-21 12:07:59 +02003139 if (!init_rmode_tss(kvm))
3140 return -ENOMEM;
3141
Izik Eiduscbc94022007-10-25 00:29:55 +02003142 return 0;
3143}
3144
Avi Kivity6aa8b732006-12-10 02:21:36 -08003145static int handle_rmode_exception(struct kvm_vcpu *vcpu,
3146 int vec, u32 err_code)
3147{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03003148 /*
3149 * Instruction with address size override prefix opcode 0x67
3150 * Cause the #SS fault with 0 error code in VM86 mode.
3151 */
3152 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Andre Przywara51d8b662010-12-21 11:12:02 +01003153 if (emulate_instruction(vcpu, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003155 /*
3156 * Forward all other exceptions that are valid in real mode.
3157 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
3158 * the required debugging infrastructure rework.
3159 */
3160 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003161 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003162 if (vcpu->guest_debug &
3163 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
3164 return 0;
3165 kvm_queue_exception(vcpu, vec);
3166 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003167 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003168 /*
3169 * Update instruction length as we may reinject the exception
3170 * from user space while in guest debugging mode.
3171 */
3172 to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
3173 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003174 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
3175 return 0;
3176 /* fall through */
3177 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02003178 case OF_VECTOR:
3179 case BR_VECTOR:
3180 case UD_VECTOR:
3181 case DF_VECTOR:
3182 case SS_VECTOR:
3183 case GP_VECTOR:
3184 case MF_VECTOR:
3185 kvm_queue_exception(vcpu, vec);
3186 return 1;
3187 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188 return 0;
3189}
3190
Andi Kleena0861c02009-06-08 17:37:09 +08003191/*
3192 * Trigger machine check on the host. We assume all the MSRs are already set up
3193 * by the CPU and that we still run on the same CPU as the MCE occurred on.
3194 * We pass a fake environment to the machine check handler because we want
3195 * the guest to be always treated like user space, no matter what context
3196 * it used internally.
3197 */
3198static void kvm_machine_check(void)
3199{
3200#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
3201 struct pt_regs regs = {
3202 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
3203 .flags = X86_EFLAGS_IF,
3204 };
3205
3206 do_machine_check(&regs, 0);
3207#endif
3208}
3209
Avi Kivity851ba692009-08-24 11:10:17 +03003210static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08003211{
3212 /* already handled by vcpu_run */
3213 return 1;
3214}
3215
Avi Kivity851ba692009-08-24 11:10:17 +03003216static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003217{
Avi Kivity1155f762007-11-22 11:30:47 +02003218 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003219 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003220 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003221 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 u32 vect_info;
3223 enum emulation_result er;
3224
Avi Kivity1155f762007-11-22 11:30:47 +02003225 vect_info = vmx->idt_vectoring_info;
Avi Kivity88786472011-03-07 17:39:45 +02003226 intr_info = vmx->exit_intr_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003227
Andi Kleena0861c02009-06-08 17:37:09 +08003228 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03003229 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003230
Avi Kivity6aa8b732006-12-10 02:21:36 -08003231 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02003232 !is_page_fault(intr_info)) {
3233 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3234 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
3235 vcpu->run->internal.ndata = 2;
3236 vcpu->run->internal.data[0] = vect_info;
3237 vcpu->run->internal.data[1] = intr_info;
3238 return 0;
3239 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003240
Jan Kiszkae4a41882008-09-26 09:30:46 +02003241 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02003242 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003243
3244 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03003245 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03003246 return 1;
3247 }
3248
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003249 if (is_invalid_opcode(intr_info)) {
Andre Przywara51d8b662010-12-21 11:12:02 +01003250 er = emulate_instruction(vcpu, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003251 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02003252 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003253 return 1;
3254 }
3255
Avi Kivity6aa8b732006-12-10 02:21:36 -08003256 error_code = 0;
Ryan Harper2e113842008-02-11 10:26:38 -06003257 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003258 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
3259 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08003260 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02003261 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08003262 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003263 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003264 trace_kvm_page_fault(cr2, error_code);
3265
Gleb Natapov3298b752009-05-11 13:35:46 +03003266 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03003267 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Andre Przywaradc25e892010-12-21 11:12:07 +01003268 return kvm_mmu_page_fault(vcpu, cr2, error_code, NULL, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 }
3270
Avi Kivity7ffd92c2009-06-09 14:10:45 +03003271 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08003272 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003273 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003274 if (vcpu->arch.halt_request) {
3275 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003276 return kvm_emulate_halt(vcpu);
3277 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003278 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03003279 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003281 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003282 switch (ex_no) {
3283 case DB_VECTOR:
3284 dr6 = vmcs_readl(EXIT_QUALIFICATION);
3285 if (!(vcpu->guest_debug &
3286 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
3287 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
3288 kvm_queue_exception(vcpu, DB_VECTOR);
3289 return 1;
3290 }
3291 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
3292 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
3293 /* fall through */
3294 case BP_VECTOR:
Jan Kiszkac573cd22010-02-23 17:47:53 +01003295 /*
3296 * Update instruction length as we may reinject #BP from
3297 * user space while in guest debugging mode. Reading it for
3298 * #DB as well causes no harm, it is not used in that case.
3299 */
3300 vmx->vcpu.arch.event_exit_inst_len =
3301 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Avi Kivity0a434bb2011-04-28 15:59:33 +03003303 rip = kvm_rip_read(vcpu);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003304 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
3305 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003306 break;
3307 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003308 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
3309 kvm_run->ex.exception = ex_no;
3310 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003311 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003312 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003313 return 0;
3314}
3315
Avi Kivity851ba692009-08-24 11:10:17 +03003316static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003317{
Avi Kivity1165f5f2007-04-19 17:27:43 +03003318 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003319 return 1;
3320}
3321
Avi Kivity851ba692009-08-24 11:10:17 +03003322static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08003323{
Avi Kivity851ba692009-08-24 11:10:17 +03003324 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08003325 return 0;
3326}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003327
Avi Kivity851ba692009-08-24 11:10:17 +03003328static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003329{
He, Qingbfdaab02007-09-12 14:18:28 +08003330 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01003331 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02003332 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003333
He, Qingbfdaab02007-09-12 14:18:28 +08003334 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02003335 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003336 in = (exit_qualification & 8) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03003337
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003338 ++vcpu->stat.io_exits;
3339
3340 if (string || in)
Andre Przywara51d8b662010-12-21 11:12:02 +01003341 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003342
3343 port = exit_qualification >> 16;
3344 size = (exit_qualification & 7) + 1;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01003345 skip_emulated_instruction(vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02003346
3347 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003348}
3349
Ingo Molnar102d8322007-02-19 14:37:47 +02003350static void
3351vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3352{
3353 /*
3354 * Patch in the VMCALL instruction:
3355 */
3356 hypercall[0] = 0x0f;
3357 hypercall[1] = 0x01;
3358 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02003359}
3360
Avi Kivity851ba692009-08-24 11:10:17 +03003361static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003363 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364 int cr;
3365 int reg;
Avi Kivity49a9b072010-06-10 17:02:14 +03003366 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003367
He, Qingbfdaab02007-09-12 14:18:28 +08003368 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003369 cr = exit_qualification & 15;
3370 reg = (exit_qualification >> 8) & 15;
3371 switch ((exit_qualification >> 4) & 3) {
3372 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003373 val = kvm_register_read(vcpu, reg);
3374 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375 switch (cr) {
3376 case 0:
Avi Kivity49a9b072010-06-10 17:02:14 +03003377 err = kvm_set_cr0(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003378 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003379 return 1;
3380 case 3:
Avi Kivity23902182010-06-10 17:02:16 +03003381 err = kvm_set_cr3(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003382 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003383 return 1;
3384 case 4:
Avi Kivitya83b29c2010-06-10 17:02:15 +03003385 err = kvm_set_cr4(vcpu, val);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003386 kvm_complete_insn_gp(vcpu, err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003387 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003388 case 8: {
3389 u8 cr8_prev = kvm_get_cr8(vcpu);
3390 u8 cr8 = kvm_register_read(vcpu, reg);
Andre Przywaraeea1cff2010-12-21 11:12:00 +01003391 err = kvm_set_cr8(vcpu, cr8);
Andre Przywaradb8fcef2010-12-21 11:12:01 +01003392 kvm_complete_insn_gp(vcpu, err);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003393 if (irqchip_in_kernel(vcpu->kvm))
3394 return 1;
3395 if (cr8_prev <= cr8)
3396 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03003397 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03003398 return 0;
3399 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003400 };
3401 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03003402 case 2: /* clts */
Avi Kivityedcafe32009-12-30 18:07:40 +02003403 vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
Avi Kivity4d4ec082009-12-29 18:07:30 +02003404 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003405 skip_emulated_instruction(vcpu);
Avi Kivity6b52d182010-01-21 15:31:47 +02003406 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03003407 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408 case 1: /*mov from cr*/
3409 switch (cr) {
3410 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02003411 val = kvm_read_cr3(vcpu);
3412 kvm_register_write(vcpu, reg, val);
3413 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003414 skip_emulated_instruction(vcpu);
3415 return 1;
3416 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003417 val = kvm_get_cr8(vcpu);
3418 kvm_register_write(vcpu, reg, val);
3419 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003420 skip_emulated_instruction(vcpu);
3421 return 1;
3422 }
3423 break;
3424 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003425 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003426 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003427 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428
3429 skip_emulated_instruction(vcpu);
3430 return 1;
3431 default:
3432 break;
3433 }
Avi Kivity851ba692009-08-24 11:10:17 +03003434 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003435 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436 (int)(exit_qualification >> 4) & 3, cr);
3437 return 0;
3438}
3439
Avi Kivity851ba692009-08-24 11:10:17 +03003440static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003441{
He, Qingbfdaab02007-09-12 14:18:28 +08003442 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443 int dr, reg;
3444
Jan Kiszkaf2483412010-01-20 18:20:20 +01003445 /* Do not handle if the CPL > 0, will trigger GP on re-entry */
Avi Kivity0a79b002009-09-01 12:03:25 +03003446 if (!kvm_require_cpl(vcpu, 0))
3447 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003448 dr = vmcs_readl(GUEST_DR7);
3449 if (dr & DR7_GD) {
3450 /*
3451 * As the vm-exit takes precedence over the debug trap, we
3452 * need to emulate the latter, either for the host or the
3453 * guest debugging itself.
3454 */
3455 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003456 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3457 vcpu->run->debug.arch.dr7 = dr;
3458 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003459 vmcs_readl(GUEST_CS_BASE) +
3460 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003461 vcpu->run->debug.arch.exception = DB_VECTOR;
3462 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003463 return 0;
3464 } else {
3465 vcpu->arch.dr7 &= ~DR7_GD;
3466 vcpu->arch.dr6 |= DR6_BD;
3467 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3468 kvm_queue_exception(vcpu, DB_VECTOR);
3469 return 1;
3470 }
3471 }
3472
He, Qingbfdaab02007-09-12 14:18:28 +08003473 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003474 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3475 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3476 if (exit_qualification & TYPE_MOV_FROM_DR) {
Gleb Natapov020df072010-04-13 10:05:23 +03003477 unsigned long val;
3478 if (!kvm_get_dr(vcpu, dr, &val))
3479 kvm_register_write(vcpu, reg, val);
3480 } else
3481 kvm_set_dr(vcpu, dr, vcpu->arch.regs[reg]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003482 skip_emulated_instruction(vcpu);
3483 return 1;
3484}
3485
Gleb Natapov020df072010-04-13 10:05:23 +03003486static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
3487{
3488 vmcs_writel(GUEST_DR7, val);
3489}
3490
Avi Kivity851ba692009-08-24 11:10:17 +03003491static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492{
Avi Kivity06465c52007-02-28 20:46:53 +02003493 kvm_emulate_cpuid(vcpu);
3494 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003495}
3496
Avi Kivity851ba692009-08-24 11:10:17 +03003497static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003499 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003500 u64 data;
3501
3502 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivity59200272010-01-25 19:47:02 +02003503 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003504 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505 return 1;
3506 }
3507
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003508 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003509
Avi Kivity6aa8b732006-12-10 02:21:36 -08003510 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003511 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3512 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513 skip_emulated_instruction(vcpu);
3514 return 1;
3515}
3516
Avi Kivity851ba692009-08-24 11:10:17 +03003517static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003518{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003519 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3520 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3521 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003522
3523 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivity59200272010-01-25 19:47:02 +02003524 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003525 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003526 return 1;
3527 }
3528
Avi Kivity59200272010-01-25 19:47:02 +02003529 trace_kvm_msr_write(ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003530 skip_emulated_instruction(vcpu);
3531 return 1;
3532}
3533
Avi Kivity851ba692009-08-24 11:10:17 +03003534static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003535{
Avi Kivity3842d132010-07-27 12:30:24 +03003536 kvm_make_request(KVM_REQ_EVENT, vcpu);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003537 return 1;
3538}
3539
Avi Kivity851ba692009-08-24 11:10:17 +03003540static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541{
Eddie Dong85f455f2007-07-06 12:20:49 +03003542 u32 cpu_based_vm_exec_control;
3543
3544 /* clear pending irq */
3545 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3546 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3547 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003548
Avi Kivity3842d132010-07-27 12:30:24 +03003549 kvm_make_request(KVM_REQ_EVENT, vcpu);
3550
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003551 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003552
Dor Laorc1150d82007-01-05 16:36:24 -08003553 /*
3554 * If the user space waits to inject interrupts, exit as soon as
3555 * possible
3556 */
Gleb Natapov80618232009-04-21 17:44:56 +03003557 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003558 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003559 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003560 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003561 return 0;
3562 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003563 return 1;
3564}
3565
Avi Kivity851ba692009-08-24 11:10:17 +03003566static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003567{
3568 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003569 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003570}
3571
Avi Kivity851ba692009-08-24 11:10:17 +03003572static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003573{
Dor Laor510043d2007-02-19 18:25:43 +02003574 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003575 kvm_emulate_hypercall(vcpu);
3576 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003577}
3578
Avi Kivity851ba692009-08-24 11:10:17 +03003579static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003580{
3581 kvm_queue_exception(vcpu, UD_VECTOR);
3582 return 1;
3583}
3584
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003585static int handle_invd(struct kvm_vcpu *vcpu)
3586{
Andre Przywara51d8b662010-12-21 11:12:02 +01003587 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003588}
3589
Avi Kivity851ba692009-08-24 11:10:17 +03003590static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003591{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003592 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003593
3594 kvm_mmu_invlpg(vcpu, exit_qualification);
3595 skip_emulated_instruction(vcpu);
3596 return 1;
3597}
3598
Avi Kivity851ba692009-08-24 11:10:17 +03003599static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003600{
3601 skip_emulated_instruction(vcpu);
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003602 kvm_emulate_wbinvd(vcpu);
Eddie Donge5edaa02007-11-11 12:28:35 +02003603 return 1;
3604}
3605
Dexuan Cui2acf9232010-06-10 11:27:12 +08003606static int handle_xsetbv(struct kvm_vcpu *vcpu)
3607{
3608 u64 new_bv = kvm_read_edx_eax(vcpu);
3609 u32 index = kvm_register_read(vcpu, VCPU_REGS_RCX);
3610
3611 if (kvm_set_xcr(vcpu, index, new_bv) == 0)
3612 skip_emulated_instruction(vcpu);
3613 return 1;
3614}
3615
Avi Kivity851ba692009-08-24 11:10:17 +03003616static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003617{
Andre Przywara51d8b662010-12-21 11:12:02 +01003618 return emulate_instruction(vcpu, 0) == EMULATE_DONE;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003619}
3620
Avi Kivity851ba692009-08-24 11:10:17 +03003621static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003622{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003623 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003624 unsigned long exit_qualification;
Jan Kiszkae269fb22010-04-14 15:51:09 +02003625 bool has_error_code = false;
3626 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02003627 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003628 int reason, type, idt_v;
3629
3630 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3631 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003632
3633 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3634
3635 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003636 if (reason == TASK_SWITCH_GATE && idt_v) {
3637 switch (type) {
3638 case INTR_TYPE_NMI_INTR:
3639 vcpu->arch.nmi_injected = false;
Avi Kivity654f06f2011-03-23 15:02:47 +02003640 vmx_set_nmi_mask(vcpu, true);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003641 break;
3642 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003643 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003644 kvm_clear_interrupt_queue(vcpu);
3645 break;
3646 case INTR_TYPE_HARD_EXCEPTION:
Jan Kiszkae269fb22010-04-14 15:51:09 +02003647 if (vmx->idt_vectoring_info &
3648 VECTORING_INFO_DELIVER_CODE_MASK) {
3649 has_error_code = true;
3650 error_code =
3651 vmcs_read32(IDT_VECTORING_ERROR_CODE);
3652 }
3653 /* fall through */
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003654 case INTR_TYPE_SOFT_EXCEPTION:
3655 kvm_clear_exception_queue(vcpu);
3656 break;
3657 default:
3658 break;
3659 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003660 }
Izik Eidus37817f22008-03-24 23:14:53 +02003661 tss_selector = exit_qualification;
3662
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003663 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3664 type != INTR_TYPE_EXT_INTR &&
3665 type != INTR_TYPE_NMI_INTR))
3666 skip_emulated_instruction(vcpu);
3667
Gleb Natapovacb54512010-04-15 21:03:50 +03003668 if (kvm_task_switch(vcpu, tss_selector, reason,
3669 has_error_code, error_code) == EMULATE_FAIL) {
3670 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3671 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3672 vcpu->run->internal.ndata = 0;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003673 return 0;
Gleb Natapovacb54512010-04-15 21:03:50 +03003674 }
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003675
3676 /* clear all local breakpoint enable flags */
3677 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3678
3679 /*
3680 * TODO: What about debug traps on tss switch?
3681 * Are we supposed to inject them and update dr6?
3682 */
3683
3684 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003685}
3686
Avi Kivity851ba692009-08-24 11:10:17 +03003687static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003688{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003689 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003690 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003691 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003692
Sheng Yangf9c617f2009-03-25 10:08:52 +08003693 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003694
3695 if (exit_qualification & (1 << 6)) {
3696 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003697 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003698 }
3699
3700 gla_validity = (exit_qualification >> 7) & 0x3;
3701 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3702 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3703 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3704 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003705 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003706 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3707 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003708 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3709 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003710 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003711 }
3712
3713 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003714 trace_kvm_page_fault(gpa, exit_qualification);
Andre Przywaradc25e892010-12-21 11:12:07 +01003715 return kvm_mmu_page_fault(vcpu, gpa, exit_qualification & 0x3, NULL, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003716}
3717
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003718static u64 ept_rsvd_mask(u64 spte, int level)
3719{
3720 int i;
3721 u64 mask = 0;
3722
3723 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3724 mask |= (1ULL << i);
3725
3726 if (level > 2)
3727 /* bits 7:3 reserved */
3728 mask |= 0xf8;
3729 else if (level == 2) {
3730 if (spte & (1ULL << 7))
3731 /* 2MB ref, bits 20:12 reserved */
3732 mask |= 0x1ff000;
3733 else
3734 /* bits 6:3 reserved */
3735 mask |= 0x78;
3736 }
3737
3738 return mask;
3739}
3740
3741static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3742 int level)
3743{
3744 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3745
3746 /* 010b (write-only) */
3747 WARN_ON((spte & 0x7) == 0x2);
3748
3749 /* 110b (write/execute) */
3750 WARN_ON((spte & 0x7) == 0x6);
3751
3752 /* 100b (execute-only) and value not supported by logical processor */
3753 if (!cpu_has_vmx_ept_execute_only())
3754 WARN_ON((spte & 0x7) == 0x4);
3755
3756 /* not 000b */
3757 if ((spte & 0x7)) {
3758 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3759
3760 if (rsvd_bits != 0) {
3761 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3762 __func__, rsvd_bits);
3763 WARN_ON(1);
3764 }
3765
3766 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3767 u64 ept_mem_type = (spte & 0x38) >> 3;
3768
3769 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3770 ept_mem_type == 7) {
3771 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3772 __func__, ept_mem_type);
3773 WARN_ON(1);
3774 }
3775 }
3776 }
3777}
3778
Avi Kivity851ba692009-08-24 11:10:17 +03003779static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003780{
3781 u64 sptes[4];
3782 int nr_sptes, i;
3783 gpa_t gpa;
3784
3785 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3786
3787 printk(KERN_ERR "EPT: Misconfiguration.\n");
3788 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3789
3790 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3791
3792 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3793 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3794
Avi Kivity851ba692009-08-24 11:10:17 +03003795 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3796 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003797
3798 return 0;
3799}
3800
Avi Kivity851ba692009-08-24 11:10:17 +03003801static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003802{
3803 u32 cpu_based_vm_exec_control;
3804
3805 /* clear pending NMI */
3806 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3807 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3808 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3809 ++vcpu->stat.nmi_window_exits;
Avi Kivity3842d132010-07-27 12:30:24 +03003810 kvm_make_request(KVM_REQ_EVENT, vcpu);
Sheng Yangf08864b2008-05-15 18:23:25 +08003811
3812 return 1;
3813}
3814
Mohammed Gamal80ced182009-09-01 12:48:18 +02003815static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003816{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003817 struct vcpu_vmx *vmx = to_vmx(vcpu);
3818 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003819 int ret = 1;
Avi Kivity49e9d552010-09-19 14:34:08 +02003820 u32 cpu_exec_ctrl;
3821 bool intr_window_requested;
3822
3823 cpu_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3824 intr_window_requested = cpu_exec_ctrl & CPU_BASED_VIRTUAL_INTR_PENDING;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003825
3826 while (!guest_state_valid(vcpu)) {
Avi Kivity49e9d552010-09-19 14:34:08 +02003827 if (intr_window_requested
3828 && (kvm_get_rflags(&vmx->vcpu) & X86_EFLAGS_IF))
3829 return handle_interrupt_window(&vmx->vcpu);
3830
Andre Przywara51d8b662010-12-21 11:12:02 +01003831 err = emulate_instruction(vcpu, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003832
Mohammed Gamal80ced182009-09-01 12:48:18 +02003833 if (err == EMULATE_DO_MMIO) {
3834 ret = 0;
3835 goto out;
3836 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003837
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03003838 if (err != EMULATE_DONE)
3839 return 0;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003840
3841 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003842 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003843 if (need_resched())
3844 schedule();
3845 }
3846
Mohammed Gamal80ced182009-09-01 12:48:18 +02003847 vmx->emulation_required = 0;
3848out:
3849 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003850}
3851
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003853 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3854 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3855 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003856static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003857{
3858 skip_emulated_instruction(vcpu);
3859 kvm_vcpu_on_spin(vcpu);
3860
3861 return 1;
3862}
3863
Sheng Yang59708672009-12-15 13:29:54 +08003864static int handle_invalid_op(struct kvm_vcpu *vcpu)
3865{
3866 kvm_queue_exception(vcpu, UD_VECTOR);
3867 return 1;
3868}
3869
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003870/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003871 * The exit handlers return 1 if the exit was handled fully and guest execution
3872 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3873 * to be done to userspace and return 0.
3874 */
Avi Kivity851ba692009-08-24 11:10:17 +03003875static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3877 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003878 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003879 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 [EXIT_REASON_CR_ACCESS] = handle_cr,
3882 [EXIT_REASON_DR_ACCESS] = handle_dr,
3883 [EXIT_REASON_CPUID] = handle_cpuid,
3884 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3885 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3886 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3887 [EXIT_REASON_HLT] = handle_halt,
Gleb Natapovec25d5e2010-11-01 15:35:01 +02003888 [EXIT_REASON_INVD] = handle_invd,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003889 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003890 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003891 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3892 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3893 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3894 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3895 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3896 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3897 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3898 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3899 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003900 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3901 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003902 [EXIT_REASON_WBINVD] = handle_wbinvd,
Dexuan Cui2acf9232010-06-10 11:27:12 +08003903 [EXIT_REASON_XSETBV] = handle_xsetbv,
Izik Eidus37817f22008-03-24 23:14:53 +02003904 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003905 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003906 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3907 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003908 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003909 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3910 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911};
3912
3913static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003914 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003915
Avi Kivity586f9602010-11-18 13:09:54 +02003916static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3917{
3918 *info1 = vmcs_readl(EXIT_QUALIFICATION);
3919 *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
3920}
3921
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922/*
3923 * The guest has exited. See if we can fix it or if we need userspace
3924 * assistance.
3925 */
Avi Kivity851ba692009-08-24 11:10:17 +03003926static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003927{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003928 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003929 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003930 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003931
Avi Kivityaa179112010-11-17 18:44:19 +02003932 trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003933
Mohammed Gamal80ced182009-09-01 12:48:18 +02003934 /* If guest state is invalid, start emulating */
3935 if (vmx->emulation_required && emulate_invalid_guest_state)
3936 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003937
Mohammed Gamal51207022010-05-31 22:40:54 +03003938 if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
3939 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3940 vcpu->run->fail_entry.hardware_entry_failure_reason
3941 = exit_reason;
3942 return 0;
3943 }
3944
Avi Kivity29bd8a72007-09-10 17:27:03 +03003945 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003946 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3947 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003948 = vmcs_read32(VM_INSTRUCTION_ERROR);
3949 return 0;
3950 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951
Mike Dayd77c26f2007-10-08 09:02:08 -04003952 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003953 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003954 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3955 exit_reason != EXIT_REASON_TASK_SWITCH))
3956 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3957 "(0x%x) and exit reason is 0x%x\n",
3958 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003959
3960 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003961 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003962 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003963 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003964 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003965 /*
3966 * This CPU don't support us in finding the end of an
3967 * NMI-blocked window if the guest runs with IRQs
3968 * disabled. So we pull the trigger after 1 s of
3969 * futile waiting, but inform the user about this.
3970 */
3971 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3972 "state on VCPU %d after 1 s timeout\n",
3973 __func__, vcpu->vcpu_id);
3974 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003975 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003976 }
3977
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 if (exit_reason < kvm_vmx_max_exit_handlers
3979 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003980 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003981 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003982 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3983 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003984 }
3985 return 0;
3986}
3987
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003988static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003989{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003990 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003991 vmcs_write32(TPR_THRESHOLD, 0);
3992 return;
3993 }
3994
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003995 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003996}
3997
Avi Kivity51aa01d2010-07-20 14:31:20 +03003998static void vmx_complete_atomic_exit(struct vcpu_vmx *vmx)
Avi Kivitycf393f72008-07-01 16:20:21 +03003999{
Avi Kivity00eba012011-03-07 17:24:54 +02004000 u32 exit_intr_info;
4001
4002 if (!(vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY
4003 || vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI))
4004 return;
4005
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004006 vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivity00eba012011-03-07 17:24:54 +02004007 exit_intr_info = vmx->exit_intr_info;
Andi Kleena0861c02009-06-08 17:37:09 +08004008
4009 /* Handle machine checks before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004010 if (is_machine_check(exit_intr_info))
Andi Kleena0861c02009-06-08 17:37:09 +08004011 kvm_machine_check();
4012
Gleb Natapov20f65982009-05-11 13:35:55 +03004013 /* We need to handle NMIs before interrupts are enabled */
Avi Kivity00eba012011-03-07 17:24:54 +02004014 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004015 (exit_intr_info & INTR_INFO_VALID_MASK)) {
4016 kvm_before_handle_nmi(&vmx->vcpu);
Gleb Natapov20f65982009-05-11 13:35:55 +03004017 asm("int $2");
Zhang, Yanminff9d07a2010-04-19 13:32:45 +08004018 kvm_after_handle_nmi(&vmx->vcpu);
4019 }
Avi Kivity51aa01d2010-07-20 14:31:20 +03004020}
Gleb Natapov20f65982009-05-11 13:35:55 +03004021
Avi Kivity51aa01d2010-07-20 14:31:20 +03004022static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
4023{
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004024 u32 exit_intr_info;
Avi Kivity51aa01d2010-07-20 14:31:20 +03004025 bool unblock_nmi;
4026 u8 vector;
4027 bool idtv_info_valid;
4028
4029 idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Gleb Natapov20f65982009-05-11 13:35:55 +03004030
Avi Kivitycf393f72008-07-01 16:20:21 +03004031 if (cpu_has_virtual_nmis()) {
Avi Kivity9d58b932011-03-07 16:52:07 +02004032 if (vmx->nmi_known_unmasked)
4033 return;
Avi Kivityc5ca8e52011-03-07 17:37:37 +02004034 /*
4035 * Can't use vmx->exit_intr_info since we're not sure what
4036 * the exit reason is.
4037 */
4038 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Avi Kivitycf393f72008-07-01 16:20:21 +03004039 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
4040 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
4041 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004042 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03004043 * Re-set bit "block by NMI" before VM entry if vmexit caused by
4044 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004045 * SDM 3: 23.2.2 (September 2008)
4046 * Bit 12 is undefined in any of the following cases:
4047 * If the VM exit sets the valid bit in the IDT-vectoring
4048 * information field.
4049 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03004050 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004051 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
4052 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03004053 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
4054 GUEST_INTR_STATE_NMI);
Avi Kivity9d58b932011-03-07 16:52:07 +02004055 else
4056 vmx->nmi_known_unmasked =
4057 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
4058 & GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02004059 } else if (unlikely(vmx->soft_vnmi_blocked))
4060 vmx->vnmi_blocked_time +=
4061 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity51aa01d2010-07-20 14:31:20 +03004062}
4063
Avi Kivity83422e12010-07-20 14:43:23 +03004064static void __vmx_complete_interrupts(struct vcpu_vmx *vmx,
4065 u32 idt_vectoring_info,
4066 int instr_len_field,
4067 int error_code_field)
Avi Kivity51aa01d2010-07-20 14:31:20 +03004068{
Avi Kivity51aa01d2010-07-20 14:31:20 +03004069 u8 vector;
4070 int type;
4071 bool idtv_info_valid;
4072
4073 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
Avi Kivity668f6122008-07-02 09:28:55 +03004074
Gleb Natapov37b96e92009-03-30 16:03:13 +03004075 vmx->vcpu.arch.nmi_injected = false;
4076 kvm_clear_exception_queue(&vmx->vcpu);
4077 kvm_clear_interrupt_queue(&vmx->vcpu);
4078
4079 if (!idtv_info_valid)
4080 return;
4081
Avi Kivity3842d132010-07-27 12:30:24 +03004082 kvm_make_request(KVM_REQ_EVENT, &vmx->vcpu);
4083
Avi Kivity668f6122008-07-02 09:28:55 +03004084 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
4085 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004086
Gleb Natapov64a7ec02009-03-30 16:03:29 +03004087 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03004088 case INTR_TYPE_NMI_INTR:
4089 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03004090 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03004091 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03004092 * Clear bit "block by NMI" before VM entry if a NMI
4093 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03004094 */
Avi Kivity654f06f2011-03-23 15:02:47 +02004095 vmx_set_nmi_mask(&vmx->vcpu, false);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004096 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03004097 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004098 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004099 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004100 /* fall through */
4101 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03004102 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Avi Kivity83422e12010-07-20 14:43:23 +03004103 u32 err = vmcs_read32(error_code_field);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004104 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03004105 } else
4106 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004107 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004108 case INTR_TYPE_SOFT_INTR:
4109 vmx->vcpu.arch.event_exit_inst_len =
Avi Kivity83422e12010-07-20 14:43:23 +03004110 vmcs_read32(instr_len_field);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004111 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03004112 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03004113 kvm_queue_interrupt(&vmx->vcpu, vector,
4114 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03004115 break;
4116 default:
4117 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03004118 }
Avi Kivitycf393f72008-07-01 16:20:21 +03004119}
4120
Avi Kivity83422e12010-07-20 14:43:23 +03004121static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
4122{
4123 __vmx_complete_interrupts(vmx, vmx->idt_vectoring_info,
4124 VM_EXIT_INSTRUCTION_LEN,
4125 IDT_VECTORING_ERROR_CODE);
4126}
4127
Avi Kivityb463a6f2010-07-20 15:06:17 +03004128static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
4129{
4130 __vmx_complete_interrupts(to_vmx(vcpu),
4131 vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
4132 VM_ENTRY_INSTRUCTION_LEN,
4133 VM_ENTRY_EXCEPTION_ERROR_CODE);
4134
4135 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
4136}
4137
Avi Kivityc8019492008-07-14 14:44:59 +03004138#ifdef CONFIG_X86_64
4139#define R "r"
4140#define Q "q"
4141#else
4142#define R "e"
4143#define Q "l"
4144#endif
4145
Lai Jiangshana3b5ba42011-02-11 14:29:40 +08004146static void __noclone vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004147{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004148 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity104f2262010-11-18 13:12:52 +02004149
4150 /* Record the guest's net vcpu time for enforced NMI injections. */
4151 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
4152 vmx->entry_time = ktime_get();
4153
4154 /* Don't enter VMX if guest state is invalid, let the exit handler
4155 start emulation until we arrive back to a valid state */
4156 if (vmx->emulation_required && emulate_invalid_guest_state)
4157 return;
4158
4159 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
4160 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
4161 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
4162 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
4163
4164 /* When single-stepping over STI and MOV SS, we must clear the
4165 * corresponding interruptibility bits in the guest state. Otherwise
4166 * vmentry fails as it then expects bit 14 (BS) in pending debug
4167 * exceptions being set, but that's not correct for the guest debugging
4168 * case. */
4169 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
4170 vmx_set_interrupt_shadow(vcpu, 0);
4171
4172 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08004173 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03004174 "push %%"R"dx; push %%"R"bp;"
Avi Kivity40712fa2011-01-06 18:09:12 +02004175 "push %%"R"cx \n\t" /* placeholder for guest rcx */
Avi Kivityc8019492008-07-14 14:44:59 +03004176 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004177 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
4178 "je 1f \n\t"
4179 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004180 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03004181 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03004182 /* Reload cr2 if changed */
4183 "mov %c[cr2](%0), %%"R"ax \n\t"
4184 "mov %%cr2, %%"R"dx \n\t"
4185 "cmp %%"R"ax, %%"R"dx \n\t"
4186 "je 2f \n\t"
4187 "mov %%"R"ax, %%cr2 \n\t"
4188 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004189 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02004190 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004191 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03004192 "mov %c[rax](%0), %%"R"ax \n\t"
4193 "mov %c[rbx](%0), %%"R"bx \n\t"
4194 "mov %c[rdx](%0), %%"R"dx \n\t"
4195 "mov %c[rsi](%0), %%"R"si \n\t"
4196 "mov %c[rdi](%0), %%"R"di \n\t"
4197 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004198#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004199 "mov %c[r8](%0), %%r8 \n\t"
4200 "mov %c[r9](%0), %%r9 \n\t"
4201 "mov %c[r10](%0), %%r10 \n\t"
4202 "mov %c[r11](%0), %%r11 \n\t"
4203 "mov %c[r12](%0), %%r12 \n\t"
4204 "mov %c[r13](%0), %%r13 \n\t"
4205 "mov %c[r14](%0), %%r14 \n\t"
4206 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004208 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
4209
Avi Kivity6aa8b732006-12-10 02:21:36 -08004210 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03004211 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004212 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004213 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03004214 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03004215 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08004216 /* Save guest registers, load host registers, keep flags */
Avi Kivity40712fa2011-01-06 18:09:12 +02004217 "mov %0, %c[wordsize](%%"R"sp) \n\t"
4218 "pop %0 \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004219 "mov %%"R"ax, %c[rax](%0) \n\t"
4220 "mov %%"R"bx, %c[rbx](%0) \n\t"
Avi Kivity1c696d02011-01-06 18:09:11 +02004221 "pop"Q" %c[rcx](%0) \n\t"
Avi Kivityc8019492008-07-14 14:44:59 +03004222 "mov %%"R"dx, %c[rdx](%0) \n\t"
4223 "mov %%"R"si, %c[rsi](%0) \n\t"
4224 "mov %%"R"di, %c[rdi](%0) \n\t"
4225 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004226#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02004227 "mov %%r8, %c[r8](%0) \n\t"
4228 "mov %%r9, %c[r9](%0) \n\t"
4229 "mov %%r10, %c[r10](%0) \n\t"
4230 "mov %%r11, %c[r11](%0) \n\t"
4231 "mov %%r12, %c[r12](%0) \n\t"
4232 "mov %%r13, %c[r13](%0) \n\t"
4233 "mov %%r14, %c[r14](%0) \n\t"
4234 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235#endif
Avi Kivityc8019492008-07-14 14:44:59 +03004236 "mov %%cr2, %%"R"ax \n\t"
4237 "mov %%"R"ax, %c[cr2](%0) \n\t"
4238
Avi Kivity1c696d02011-01-06 18:09:11 +02004239 "pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02004240 "setbe %c[fail](%0) \n\t"
4241 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
4242 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
4243 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03004244 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004245 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
4246 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
4247 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
4248 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
4249 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
4250 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
4251 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08004252#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08004253 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
4254 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
4255 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
4256 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
4257 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
4258 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
4259 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
4260 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08004261#endif
Avi Kivity40712fa2011-01-06 18:09:12 +02004262 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2)),
4263 [wordsize]"i"(sizeof(ulong))
Laurent Vivierc2036302007-10-25 14:18:52 +02004264 : "cc", "memory"
Jan Kiszka07d6f552010-09-28 16:37:42 +02004265 , R"ax", R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02004266#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02004267 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
4268#endif
4269 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08004270
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004271 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
Avi Kivity6de12732011-03-07 12:51:22 +02004272 | (1 << VCPU_EXREG_RFLAGS)
Avi Kivity69c73022011-03-07 15:26:44 +02004273 | (1 << VCPU_EXREG_CPL)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004274 | (1 << VCPU_EXREG_PDPTR)
Avi Kivity2fb92db2011-04-27 19:42:18 +03004275 | (1 << VCPU_EXREG_SEGMENTS)
Avi Kivityaff48ba2010-12-05 18:56:11 +02004276 | (1 << VCPU_EXREG_CR3));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004277 vcpu->arch.regs_dirty = 0;
4278
Avi Kivity1155f762007-11-22 11:30:47 +02004279 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
4280
Mike Dayd77c26f2007-10-08 09:02:08 -04004281 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03004282 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02004283
Avi Kivity51aa01d2010-07-20 14:31:20 +03004284 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
Avi Kivity51aa01d2010-07-20 14:31:20 +03004285
4286 vmx_complete_atomic_exit(vmx);
4287 vmx_recover_nmi_blocking(vmx);
Avi Kivitycf393f72008-07-01 16:20:21 +03004288 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004289}
4290
Avi Kivityc8019492008-07-14 14:44:59 +03004291#undef R
4292#undef Q
4293
Avi Kivity6aa8b732006-12-10 02:21:36 -08004294static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
4295{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004296 struct vcpu_vmx *vmx = to_vmx(vcpu);
4297
4298 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03004299 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004300 free_vmcs(vmx->vmcs);
4301 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004302 }
4303}
4304
4305static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
4306{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004307 struct vcpu_vmx *vmx = to_vmx(vcpu);
4308
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004309 free_vpid(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004310 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004311 kfree(vmx->guest_msrs);
4312 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10004313 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004314}
4315
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08004316static inline void vmcs_init(struct vmcs *vmcs)
4317{
4318 u64 phys_addr = __pa(per_cpu(vmxarea, raw_smp_processor_id()));
4319
4320 if (!vmm_exclusive)
4321 kvm_cpu_vmxon(phys_addr);
4322
4323 vmcs_clear(vmcs);
4324
4325 if (!vmm_exclusive)
4326 kvm_cpu_vmxoff();
4327}
4328
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004329static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004330{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004331 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10004332 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03004333 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004334
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004335 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004336 return ERR_PTR(-ENOMEM);
4337
Sheng Yang2384d2b2008-01-17 15:14:33 +08004338 allocate_vpid(vmx);
4339
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004340 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
4341 if (err)
4342 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004343
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004344 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004345 err = -ENOMEM;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004346 if (!vmx->guest_msrs) {
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004347 goto uninit_vcpu;
4348 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08004349
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004350 vmx->vmcs = alloc_vmcs();
4351 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004352 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004353
Dongxiao Xu4610c9c2010-05-11 18:29:48 +08004354 vmcs_init(vmx->vmcs);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04004355
Avi Kivity15ad7142007-07-11 18:17:21 +03004356 cpu = get_cpu();
4357 vmx_vcpu_load(&vmx->vcpu, cpu);
Zachary Amsdene48672f2010-08-19 22:07:23 -10004358 vmx->vcpu.cpu = cpu;
Rusty Russell8b9cf982007-07-30 16:31:43 +10004359 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004360 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004361 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004362 if (err)
4363 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004364 if (vm_need_virtualize_apic_accesses(kvm))
Jan Kiszkabe6d05c2011-04-13 01:27:55 +02004365 err = alloc_apic_access_page(kvm);
4366 if (err)
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02004367 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004368
Sheng Yangb927a3c2009-07-21 10:42:48 +08004369 if (enable_ept) {
4370 if (!kvm->arch.ept_identity_map_addr)
4371 kvm->arch.ept_identity_map_addr =
4372 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004373 err = -ENOMEM;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004374 if (alloc_identity_pagetable(kvm) != 0)
4375 goto free_vmcs;
Gleb Natapov93ea5382011-02-21 12:07:59 +02004376 if (!init_rmode_identity_map(kvm))
4377 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08004378 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08004379
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004380 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08004381
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004382free_vmcs:
4383 free_vmcs(vmx->vmcs);
4384free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004385 kfree(vmx->guest_msrs);
4386uninit_vcpu:
4387 kvm_vcpu_uninit(&vmx->vcpu);
4388free_vcpu:
Lai Jiangshancdbecfc2010-04-17 16:41:47 +08004389 free_vpid(vmx);
Rusty Russella4770342007-08-01 14:46:11 +10004390 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10004391 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392}
4393
Yang, Sheng002c7f72007-07-31 14:23:01 +03004394static void __init vmx_check_processor_compat(void *rtn)
4395{
4396 struct vmcs_config vmcs_conf;
4397
4398 *(int *)rtn = 0;
4399 if (setup_vmcs_config(&vmcs_conf) < 0)
4400 *(int *)rtn = -EIO;
4401 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
4402 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
4403 smp_processor_id());
4404 *(int *)rtn = -EIO;
4405 }
4406}
4407
Sheng Yang67253af2008-04-25 10:20:22 +08004408static int get_ept_level(void)
4409{
4410 return VMX_EPT_DEFAULT_GAW + 1;
4411}
4412
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004413static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08004414{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004415 u64 ret;
4416
Sheng Yang522c68c2009-04-27 20:35:43 +08004417 /* For VT-d and EPT combination
4418 * 1. MMIO: always map as UC
4419 * 2. EPT with VT-d:
4420 * a. VT-d without snooping control feature: can't guarantee the
4421 * result, try to trust guest.
4422 * b. VT-d with snooping control feature: snooping control feature of
4423 * VT-d engine can guarantee the cache correctness. Just set it
4424 * to WB to keep consistent with host. So the same as item 3.
Sheng Yanga19a6d12010-02-09 16:41:53 +08004425 * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
Sheng Yang522c68c2009-04-27 20:35:43 +08004426 * consistent with host MTRR
4427 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004428 if (is_mmio)
4429 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08004430 else if (vcpu->kvm->arch.iommu_domain &&
4431 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
4432 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
4433 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004434 else
Sheng Yang522c68c2009-04-27 20:35:43 +08004435 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
Sheng Yanga19a6d12010-02-09 16:41:53 +08004436 | VMX_EPT_IPAT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004437
4438 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08004439}
4440
Avi Kivityf4c9e872009-12-28 16:06:35 +02004441#define _ER(x) { EXIT_REASON_##x, #x }
4442
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004443static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02004444 _ER(EXCEPTION_NMI),
4445 _ER(EXTERNAL_INTERRUPT),
4446 _ER(TRIPLE_FAULT),
4447 _ER(PENDING_INTERRUPT),
4448 _ER(NMI_WINDOW),
4449 _ER(TASK_SWITCH),
4450 _ER(CPUID),
4451 _ER(HLT),
4452 _ER(INVLPG),
4453 _ER(RDPMC),
4454 _ER(RDTSC),
4455 _ER(VMCALL),
4456 _ER(VMCLEAR),
4457 _ER(VMLAUNCH),
4458 _ER(VMPTRLD),
4459 _ER(VMPTRST),
4460 _ER(VMREAD),
4461 _ER(VMRESUME),
4462 _ER(VMWRITE),
4463 _ER(VMOFF),
4464 _ER(VMON),
4465 _ER(CR_ACCESS),
4466 _ER(DR_ACCESS),
4467 _ER(IO_INSTRUCTION),
4468 _ER(MSR_READ),
4469 _ER(MSR_WRITE),
4470 _ER(MWAIT_INSTRUCTION),
4471 _ER(MONITOR_INSTRUCTION),
4472 _ER(PAUSE_INSTRUCTION),
4473 _ER(MCE_DURING_VMENTRY),
4474 _ER(TPR_BELOW_THRESHOLD),
4475 _ER(APIC_ACCESS),
4476 _ER(EPT_VIOLATION),
4477 _ER(EPT_MISCONFIG),
4478 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004479 { -1, NULL }
4480};
4481
Avi Kivityf4c9e872009-12-28 16:06:35 +02004482#undef _ER
4483
Sheng Yang17cc3932010-01-05 19:02:27 +08004484static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004485{
Sheng Yang878403b2010-01-05 19:02:29 +08004486 if (enable_ept && !cpu_has_vmx_ept_1g_page())
4487 return PT_DIRECTORY_LEVEL;
4488 else
4489 /* For shadow and EPT supported 1GB page */
4490 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004491}
4492
Sheng Yang0e851882009-12-18 16:48:46 +08004493static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4494{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004495 struct kvm_cpuid_entry2 *best;
4496 struct vcpu_vmx *vmx = to_vmx(vcpu);
4497 u32 exec_control;
4498
4499 vmx->rdtscp_enabled = false;
4500 if (vmx_rdtscp_supported()) {
4501 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4502 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4503 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4504 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4505 vmx->rdtscp_enabled = true;
4506 else {
4507 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4508 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4509 exec_control);
4510 }
4511 }
4512 }
Sheng Yang0e851882009-12-18 16:48:46 +08004513}
4514
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004515static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
4516{
4517}
4518
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004519static int vmx_check_intercept(struct kvm_vcpu *vcpu,
4520 struct x86_instruction_info *info,
4521 enum x86_intercept_stage stage)
4522{
4523 return X86EMUL_CONTINUE;
4524}
4525
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004526static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004527 .cpu_has_kvm_support = cpu_has_kvm_support,
4528 .disabled_by_bios = vmx_disabled_by_bios,
4529 .hardware_setup = hardware_setup,
4530 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004531 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004532 .hardware_enable = hardware_enable,
4533 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004534 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004535
4536 .vcpu_create = vmx_create_vcpu,
4537 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004538 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004539
Avi Kivity04d2cc72007-09-10 18:10:54 +03004540 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004541 .vcpu_load = vmx_vcpu_load,
4542 .vcpu_put = vmx_vcpu_put,
4543
4544 .set_guest_debug = set_guest_debug,
4545 .get_msr = vmx_get_msr,
4546 .set_msr = vmx_set_msr,
4547 .get_segment_base = vmx_get_segment_base,
4548 .get_segment = vmx_get_segment,
4549 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004550 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004551 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004552 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Avi Kivityaff48ba2010-12-05 18:56:11 +02004553 .decache_cr3 = vmx_decache_cr3,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004554 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004555 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004556 .set_cr3 = vmx_set_cr3,
4557 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004558 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004559 .get_idt = vmx_get_idt,
4560 .set_idt = vmx_set_idt,
4561 .get_gdt = vmx_get_gdt,
4562 .set_gdt = vmx_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004563 .set_dr7 = vmx_set_dr7,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004564 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004565 .get_rflags = vmx_get_rflags,
4566 .set_rflags = vmx_set_rflags,
Avi Kivityebcbab42010-02-07 11:56:52 +02004567 .fpu_activate = vmx_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02004568 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004569
4570 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004571
Avi Kivity6aa8b732006-12-10 02:21:36 -08004572 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004573 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004574 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004575 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4576 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004577 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004578 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004579 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004580 .queue_exception = vmx_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004581 .cancel_injection = vmx_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004582 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004583 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004584 .get_nmi_mask = vmx_get_nmi_mask,
4585 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004586 .enable_nmi_window = enable_nmi_window,
4587 .enable_irq_window = enable_irq_window,
4588 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004589
Izik Eiduscbc94022007-10-25 00:29:55 +02004590 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004591 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004592 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004593
Avi Kivity586f9602010-11-18 13:09:54 +02004594 .get_exit_info = vmx_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004595 .exit_reasons_str = vmx_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02004596
Sheng Yang17cc3932010-01-05 19:02:27 +08004597 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004598
4599 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004600
4601 .rdtscp_supported = vmx_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02004602
4603 .set_supported_cpuid = vmx_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004604
4605 .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004606
Joerg Roedel4051b182011-03-25 09:44:49 +01004607 .set_tsc_khz = vmx_set_tsc_khz,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004608 .write_tsc_offset = vmx_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10004609 .adjust_tsc_offset = vmx_adjust_tsc_offset,
Joerg Roedel857e4092011-03-25 09:44:50 +01004610 .compute_tsc_offset = vmx_compute_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004611
4612 .set_tdp_cr3 = vmx_set_cr3,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004613
4614 .check_intercept = vmx_check_intercept,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004615};
4616
4617static int __init vmx_init(void)
4618{
Avi Kivity26bb0982009-09-07 11:14:12 +03004619 int r, i;
4620
4621 rdmsrl_safe(MSR_EFER, &host_efer);
4622
4623 for (i = 0; i < NR_VMX_MSR; ++i)
4624 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004625
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004626 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004627 if (!vmx_io_bitmap_a)
4628 return -ENOMEM;
4629
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004630 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004631 if (!vmx_io_bitmap_b) {
4632 r = -ENOMEM;
4633 goto out;
4634 }
4635
Avi Kivity58972972009-02-24 22:26:47 +02004636 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4637 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004638 r = -ENOMEM;
4639 goto out1;
4640 }
4641
Avi Kivity58972972009-02-24 22:26:47 +02004642 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4643 if (!vmx_msr_bitmap_longmode) {
4644 r = -ENOMEM;
4645 goto out2;
4646 }
4647
He, Qingfdef3ad2007-04-30 09:45:24 +03004648 /*
4649 * Allow direct access to the PC debug port (it is often used for I/O
4650 * delays, but the vmexits simply slow things down).
4651 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004652 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4653 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004654
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004655 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004656
Avi Kivity58972972009-02-24 22:26:47 +02004657 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4658 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004659
Sheng Yang2384d2b2008-01-17 15:14:33 +08004660 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4661
Avi Kivity0ee75be2010-04-28 15:39:01 +03004662 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
4663 __alignof__(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004664 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004665 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004666
Avi Kivity58972972009-02-24 22:26:47 +02004667 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4668 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4669 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4670 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4671 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4672 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004673
Avi Kivity089d0342009-03-23 18:26:32 +02004674 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004675 bypass_guest_pf = 0;
Sheng Yang534e38b2008-09-08 15:12:30 +08004676 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004677 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004678 kvm_enable_tdp();
4679 } else
4680 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004681
Avi Kivityc7addb92007-09-16 18:58:32 +02004682 if (bypass_guest_pf)
4683 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4684
He, Qingfdef3ad2007-04-30 09:45:24 +03004685 return 0;
4686
Avi Kivity58972972009-02-24 22:26:47 +02004687out3:
4688 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004689out2:
Avi Kivity58972972009-02-24 22:26:47 +02004690 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004691out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004692 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004693out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004694 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004695 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004696}
4697
4698static void __exit vmx_exit(void)
4699{
Avi Kivity58972972009-02-24 22:26:47 +02004700 free_page((unsigned long)vmx_msr_bitmap_legacy);
4701 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004702 free_page((unsigned long)vmx_io_bitmap_b);
4703 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004704
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004705 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004706}
4707
4708module_init(vmx_init)
4709module_exit(vmx_exit)