blob: d11be3fb7c80b193520b35d8ce0657a3fd15d60f [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This module enables machines with Intel VT-x extensions to run virtual
5 * machines without emulation or binary translation.
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 *
9 * Authors:
10 * Avi Kivity <avi@qumranet.com>
11 * Yaniv Kamay <yaniv@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040020
Avi Kivityedf88412007-12-16 11:02:48 +020021#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020023#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/mm.h>
25#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Avi Kivityc7addb92007-09-16 18:58:32 +020027#include <linux/moduleparam.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030029#include "kvm_cache_regs.h"
Avi Kivity35920a32008-07-03 14:50:12 +030030#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040031
Avi Kivity6aa8b732006-12-10 02:21:36 -080032#include <asm/io.h>
Anthony Liguori3b3be0d2006-12-13 00:33:43 -080033#include <asm/desc.h>
Eduardo Habkost13673a92008-11-17 19:03:13 -020034#include <asm/vmx.h>
Eduardo Habkost6210e372008-11-17 19:03:16 -020035#include <asm/virtext.h>
Andi Kleena0861c02009-06-08 17:37:09 +080036#include <asm/mce.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080037
Marcelo Tosatti229456f2009-06-17 09:22:14 -030038#include "trace.h"
39
Avi Kivity4ecac3f2008-05-13 13:23:38 +030040#define __ex(x) __kvm_handle_fault_on_reboot(x)
41
Avi Kivity6aa8b732006-12-10 02:21:36 -080042MODULE_AUTHOR("Qumranet");
43MODULE_LICENSE("GPL");
44
Avi Kivity4462d212009-03-23 17:53:37 +020045static int __read_mostly bypass_guest_pf = 1;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020046module_param(bypass_guest_pf, bool, S_IRUGO);
Avi Kivityc7addb92007-09-16 18:58:32 +020047
Avi Kivity4462d212009-03-23 17:53:37 +020048static int __read_mostly enable_vpid = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020049module_param_named(vpid, enable_vpid, bool, 0444);
Sheng Yang2384d2b2008-01-17 15:14:33 +080050
Avi Kivity4462d212009-03-23 17:53:37 +020051static int __read_mostly flexpriority_enabled = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020052module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
Avi Kivity4c9fc8e2008-03-24 18:15:14 +020053
Avi Kivity4462d212009-03-23 17:53:37 +020054static int __read_mostly enable_ept = 1;
Avi Kivity736caef2009-03-23 17:39:48 +020055module_param_named(ept, enable_ept, bool, S_IRUGO);
Sheng Yangd56f5462008-04-25 10:13:16 +080056
Nitin A Kamble3a624e22009-06-08 11:34:16 -070057static int __read_mostly enable_unrestricted_guest = 1;
58module_param_named(unrestricted_guest,
59 enable_unrestricted_guest, bool, S_IRUGO);
60
Avi Kivity4462d212009-03-23 17:53:37 +020061static int __read_mostly emulate_invalid_guest_state = 0;
Avi Kivityc1f8bc02009-03-23 15:41:17 +020062module_param(emulate_invalid_guest_state, bool, S_IRUGO);
Mohammed Gamal04fa4d32008-08-17 16:39:48 +030063
Avi Kivitycdc0e242009-12-06 17:21:14 +020064#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
65 (X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
66#define KVM_GUEST_CR0_MASK \
67 (KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
68#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
Avi Kivity02daab22009-12-30 12:40:26 +020069 (X86_CR0_WP | X86_CR0_NE | X86_CR0_MP)
Avi Kivitycdc0e242009-12-06 17:21:14 +020070#define KVM_VM_CR0_ALWAYS_ON \
71 (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
Avi Kivity4c386092009-12-07 12:26:18 +020072#define KVM_CR4_GUEST_OWNED_BITS \
73 (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
74 | X86_CR4_OSXMMEXCPT)
75
Avi Kivitycdc0e242009-12-06 17:21:14 +020076#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
77#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
78
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080079/*
80 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
81 * ple_gap: upper bound on the amount of time between two successive
82 * executions of PAUSE in a loop. Also indicate if ple enabled.
83 * According to test, this time is usually small than 41 cycles.
84 * ple_window: upper bound on the amount of time a guest is allowed to execute
85 * in a PAUSE loop. Tests indicate that most spinlocks are held for
86 * less than 2^12 cycles
87 * Time is measured based on a counter that runs at the same rate as the TSC,
88 * refer SDM volume 3b section 21.6.13 & 22.1.3.
89 */
90#define KVM_VMX_DEFAULT_PLE_GAP 41
91#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
92static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
93module_param(ple_gap, int, S_IRUGO);
94
95static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
96module_param(ple_window, int, S_IRUGO);
97
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040098struct vmcs {
99 u32 revision_id;
100 u32 abort;
101 char data[0];
102};
103
Avi Kivity26bb0982009-09-07 11:14:12 +0300104struct shared_msr_entry {
105 unsigned index;
106 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +0200107 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +0300108};
109
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400110struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000111 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +0300112 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +0300113 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400114 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300115 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +0200116 u32 idt_vectoring_info;
Avi Kivity26bb0982009-09-07 11:14:12 +0300117 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400118 int nmsrs;
119 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400120#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300121 u64 msr_host_kernel_gs_base;
122 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400123#endif
124 struct vmcs *vmcs;
125 struct {
126 int loaded;
127 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200128 int gs_ldt_reload_needed;
129 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400130 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200131 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300132 int vm86_active;
133 u8 save_iopl;
134 struct kvm_save_segment {
135 u16 selector;
136 unsigned long base;
137 u32 limit;
138 u32 ar;
139 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200140 struct {
141 bool pending;
142 u8 vector;
143 unsigned rip;
144 } irq;
145 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800146 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300147 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200148
149 /* Support for vnmi-less CPUs */
150 int soft_vnmi_blocked;
151 ktime_t entry_time;
152 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800153 u32 exit_reason;
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800154
155 bool rdtscp_enabled;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400156};
157
158static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
159{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000160 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400161}
162
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800163static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800164static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300165
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166static DEFINE_PER_CPU(struct vmcs *, vmxarea);
167static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300168static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200170static unsigned long *vmx_io_bitmap_a;
171static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200172static unsigned long *vmx_msr_bitmap_legacy;
173static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300174
Sheng Yang2384d2b2008-01-17 15:14:33 +0800175static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
176static DEFINE_SPINLOCK(vmx_vpid_lock);
177
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300178static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179 int size;
180 int order;
181 u32 revision_id;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300182 u32 pin_based_exec_ctrl;
183 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800184 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +0300185 u32 vmexit_ctrl;
186 u32 vmentry_ctrl;
187} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800188
Hannes Ederefff9e52008-11-28 17:02:06 +0100189static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800190 u32 ept;
191 u32 vpid;
192} vmx_capability;
193
Avi Kivity6aa8b732006-12-10 02:21:36 -0800194#define VMX_SEGMENT_FIELD(seg) \
195 [VCPU_SREG_##seg] = { \
196 .selector = GUEST_##seg##_SELECTOR, \
197 .base = GUEST_##seg##_BASE, \
198 .limit = GUEST_##seg##_LIMIT, \
199 .ar_bytes = GUEST_##seg##_AR_BYTES, \
200 }
201
202static struct kvm_vmx_segment_field {
203 unsigned selector;
204 unsigned base;
205 unsigned limit;
206 unsigned ar_bytes;
207} kvm_vmx_segment_fields[] = {
208 VMX_SEGMENT_FIELD(CS),
209 VMX_SEGMENT_FIELD(DS),
210 VMX_SEGMENT_FIELD(ES),
211 VMX_SEGMENT_FIELD(FS),
212 VMX_SEGMENT_FIELD(GS),
213 VMX_SEGMENT_FIELD(SS),
214 VMX_SEGMENT_FIELD(TR),
215 VMX_SEGMENT_FIELD(LDTR),
216};
217
Avi Kivity26bb0982009-09-07 11:14:12 +0300218static u64 host_efer;
219
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300220static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
221
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300222/*
223 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
224 * away by decrementing the array size.
225 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800227#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300228 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229#endif
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800230 MSR_EFER, MSR_TSC_AUX, MSR_K6_STAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800231};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200232#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800233
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234static inline int is_page_fault(u32 intr_info)
235{
236 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
237 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100238 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800239}
240
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300241static inline int is_no_device(u32 intr_info)
242{
243 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
244 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100245 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300246}
247
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500248static inline int is_invalid_opcode(u32 intr_info)
249{
250 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
251 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100252 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500253}
254
Avi Kivity6aa8b732006-12-10 02:21:36 -0800255static inline int is_external_interrupt(u32 intr_info)
256{
257 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
258 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
259}
260
Andi Kleena0861c02009-06-08 17:37:09 +0800261static inline int is_machine_check(u32 intr_info)
262{
263 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
264 INTR_INFO_VALID_MASK)) ==
265 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
266}
267
Sheng Yang25c5f222008-03-28 13:18:56 +0800268static inline int cpu_has_vmx_msr_bitmap(void)
269{
Sheng Yang04547152009-04-01 15:52:31 +0800270 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800271}
272
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800273static inline int cpu_has_vmx_tpr_shadow(void)
274{
Sheng Yang04547152009-04-01 15:52:31 +0800275 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800276}
277
278static inline int vm_need_tpr_shadow(struct kvm *kvm)
279{
Sheng Yang04547152009-04-01 15:52:31 +0800280 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800281}
282
Sheng Yangf78e0e22007-10-29 09:40:42 +0800283static inline int cpu_has_secondary_exec_ctrls(void)
284{
Sheng Yang04547152009-04-01 15:52:31 +0800285 return vmcs_config.cpu_based_exec_ctrl &
286 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800287}
288
Avi Kivity774ead32007-12-26 13:57:04 +0200289static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800290{
Sheng Yang04547152009-04-01 15:52:31 +0800291 return vmcs_config.cpu_based_2nd_exec_ctrl &
292 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
293}
294
295static inline bool cpu_has_vmx_flexpriority(void)
296{
297 return cpu_has_vmx_tpr_shadow() &&
298 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800299}
300
Marcelo Tosattie7997942009-06-11 12:07:40 -0300301static inline bool cpu_has_vmx_ept_execute_only(void)
302{
303 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
304}
305
306static inline bool cpu_has_vmx_eptp_uncacheable(void)
307{
308 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
309}
310
311static inline bool cpu_has_vmx_eptp_writeback(void)
312{
313 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
314}
315
316static inline bool cpu_has_vmx_ept_2m_page(void)
317{
318 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
319}
320
Sheng Yang878403b2010-01-05 19:02:29 +0800321static inline bool cpu_has_vmx_ept_1g_page(void)
322{
323 return !!(vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT);
324}
325
Sheng Yangd56f5462008-04-25 10:13:16 +0800326static inline int cpu_has_vmx_invept_individual_addr(void)
327{
Sheng Yang04547152009-04-01 15:52:31 +0800328 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800329}
330
331static inline int cpu_has_vmx_invept_context(void)
332{
Sheng Yang04547152009-04-01 15:52:31 +0800333 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800334}
335
336static inline int cpu_has_vmx_invept_global(void)
337{
Sheng Yang04547152009-04-01 15:52:31 +0800338 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800339}
340
341static inline int cpu_has_vmx_ept(void)
342{
Sheng Yang04547152009-04-01 15:52:31 +0800343 return vmcs_config.cpu_based_2nd_exec_ctrl &
344 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800345}
346
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700347static inline int cpu_has_vmx_unrestricted_guest(void)
348{
349 return vmcs_config.cpu_based_2nd_exec_ctrl &
350 SECONDARY_EXEC_UNRESTRICTED_GUEST;
351}
352
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800353static inline int cpu_has_vmx_ple(void)
354{
355 return vmcs_config.cpu_based_2nd_exec_ctrl &
356 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
357}
358
Sheng Yangf78e0e22007-10-29 09:40:42 +0800359static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
360{
Sheng Yang04547152009-04-01 15:52:31 +0800361 return flexpriority_enabled &&
362 (cpu_has_vmx_virtualize_apic_accesses()) &&
363 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800364}
365
Sheng Yang2384d2b2008-01-17 15:14:33 +0800366static inline int cpu_has_vmx_vpid(void)
367{
Sheng Yang04547152009-04-01 15:52:31 +0800368 return vmcs_config.cpu_based_2nd_exec_ctrl &
369 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800370}
371
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800372static inline int cpu_has_vmx_rdtscp(void)
373{
374 return vmcs_config.cpu_based_2nd_exec_ctrl &
375 SECONDARY_EXEC_RDTSCP;
376}
377
Sheng Yangf08864b2008-05-15 18:23:25 +0800378static inline int cpu_has_virtual_nmis(void)
379{
380 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
381}
382
Sheng Yang04547152009-04-01 15:52:31 +0800383static inline bool report_flexpriority(void)
384{
385 return flexpriority_enabled;
386}
387
Rusty Russell8b9cf982007-07-30 16:31:43 +1000388static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800389{
390 int i;
391
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400392 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300393 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300394 return i;
395 return -1;
396}
397
Sheng Yang2384d2b2008-01-17 15:14:33 +0800398static inline void __invvpid(int ext, u16 vpid, gva_t gva)
399{
400 struct {
401 u64 vpid : 16;
402 u64 rsvd : 48;
403 u64 gva;
404 } operand = { vpid, 0, gva };
405
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300406 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800407 /* CF==1 or ZF==1 --> rc = -1 */
408 "; ja 1f ; ud2 ; 1:"
409 : : "a"(&operand), "c"(ext) : "cc", "memory");
410}
411
Sheng Yang14394422008-04-28 12:24:45 +0800412static inline void __invept(int ext, u64 eptp, gpa_t gpa)
413{
414 struct {
415 u64 eptp, gpa;
416 } operand = {eptp, gpa};
417
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300418 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800419 /* CF==1 or ZF==1 --> rc = -1 */
420 "; ja 1f ; ud2 ; 1:\n"
421 : : "a" (&operand), "c" (ext) : "cc", "memory");
422}
423
Avi Kivity26bb0982009-09-07 11:14:12 +0300424static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300425{
426 int i;
427
Rusty Russell8b9cf982007-07-30 16:31:43 +1000428 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300429 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400430 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000431 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800432}
433
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434static void vmcs_clear(struct vmcs *vmcs)
435{
436 u64 phys_addr = __pa(vmcs);
437 u8 error;
438
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300439 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
441 : "cc", "memory");
442 if (error)
443 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
444 vmcs, phys_addr);
445}
446
447static void __vcpu_clear(void *arg)
448{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000449 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800450 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451
Rusty Russell8b9cf982007-07-30 16:31:43 +1000452 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400453 vmcs_clear(vmx->vmcs);
454 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800456 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300457 list_del(&vmx->local_vcpus_link);
458 vmx->vcpu.cpu = -1;
459 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460}
461
Rusty Russell8b9cf982007-07-30 16:31:43 +1000462static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800463{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200464 if (vmx->vcpu.cpu == -1)
465 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200466 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800467}
468
Sheng Yang2384d2b2008-01-17 15:14:33 +0800469static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
470{
471 if (vmx->vpid == 0)
472 return;
473
474 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
475}
476
Sheng Yang14394422008-04-28 12:24:45 +0800477static inline void ept_sync_global(void)
478{
479 if (cpu_has_vmx_invept_global())
480 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
481}
482
483static inline void ept_sync_context(u64 eptp)
484{
Avi Kivity089d0342009-03-23 18:26:32 +0200485 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800486 if (cpu_has_vmx_invept_context())
487 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
488 else
489 ept_sync_global();
490 }
491}
492
493static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
494{
Avi Kivity089d0342009-03-23 18:26:32 +0200495 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800496 if (cpu_has_vmx_invept_individual_addr())
497 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
498 eptp, gpa);
499 else
500 ept_sync_context(eptp);
501 }
502}
503
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504static unsigned long vmcs_readl(unsigned long field)
505{
506 unsigned long value;
507
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300508 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 : "=a"(value) : "d"(field) : "cc");
510 return value;
511}
512
513static u16 vmcs_read16(unsigned long field)
514{
515 return vmcs_readl(field);
516}
517
518static u32 vmcs_read32(unsigned long field)
519{
520 return vmcs_readl(field);
521}
522
523static u64 vmcs_read64(unsigned long field)
524{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800525#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 return vmcs_readl(field);
527#else
528 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
529#endif
530}
531
Avi Kivitye52de1b2007-01-05 16:36:56 -0800532static noinline void vmwrite_error(unsigned long field, unsigned long value)
533{
534 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
535 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
536 dump_stack();
537}
538
Avi Kivity6aa8b732006-12-10 02:21:36 -0800539static void vmcs_writel(unsigned long field, unsigned long value)
540{
541 u8 error;
542
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300543 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400544 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800545 if (unlikely(error))
546 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547}
548
549static void vmcs_write16(unsigned long field, u16 value)
550{
551 vmcs_writel(field, value);
552}
553
554static void vmcs_write32(unsigned long field, u32 value)
555{
556 vmcs_writel(field, value);
557}
558
559static void vmcs_write64(unsigned long field, u64 value)
560{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300562#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800563 asm volatile ("");
564 vmcs_writel(field+1, value >> 32);
565#endif
566}
567
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300568static void vmcs_clear_bits(unsigned long field, u32 mask)
569{
570 vmcs_writel(field, vmcs_readl(field) & ~mask);
571}
572
573static void vmcs_set_bits(unsigned long field, u32 mask)
574{
575 vmcs_writel(field, vmcs_readl(field) | mask);
576}
577
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300578static void update_exception_bitmap(struct kvm_vcpu *vcpu)
579{
580 u32 eb;
581
Avi Kivity02daab22009-12-30 12:40:26 +0200582 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR)
583 | (1u << NM_VECTOR);
Avi Kivitye8a48342009-09-01 16:06:25 +0300584 /*
585 * Unconditionally intercept #DB so we can maintain dr6 without
586 * reading it every exit.
587 */
588 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100589 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100590 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
591 eb |= 1u << BP_VECTOR;
592 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300593 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300594 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200595 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800596 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivity02daab22009-12-30 12:40:26 +0200597 if (vcpu->fpu_active)
598 eb &= ~(1u << NM_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300599 vmcs_write32(EXCEPTION_BITMAP, eb);
600}
601
Avi Kivity33ed6322007-05-02 16:54:03 +0300602static void reload_tss(void)
603{
Avi Kivity33ed6322007-05-02 16:54:03 +0300604 /*
605 * VT restores TR but not its size. Useless.
606 */
607 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200608 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300609
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300610 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300611 descs = (void *)gdt.base;
612 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
613 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300614}
615
Avi Kivity92c0d902009-10-29 11:00:16 +0200616static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300617{
Roel Kluin3a34a882009-08-04 02:08:45 -0700618 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300619 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300620
Avi Kivity26bb0982009-09-07 11:14:12 +0300621 guest_efer = vmx->vcpu.arch.shadow_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700622
Avi Kivity51c6cf62007-08-29 03:48:05 +0300623 /*
624 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
625 * outside long mode
626 */
627 ignore_bits = EFER_NX | EFER_SCE;
628#ifdef CONFIG_X86_64
629 ignore_bits |= EFER_LMA | EFER_LME;
630 /* SCE is meaningful only in long mode on Intel */
631 if (guest_efer & EFER_LMA)
632 ignore_bits &= ~(u64)EFER_SCE;
633#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300634 guest_efer &= ~ignore_bits;
635 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300636 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200637 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300638 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300639}
640
Avi Kivity04d2cc72007-09-10 18:10:54 +0300641static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300642{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300643 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300644 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300645
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400646 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300647 return;
648
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400649 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300650 /*
651 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
652 * allow segment selectors with cpl > 0 or ti == 1.
653 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300654 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200655 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300656 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200657 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400658 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200659 vmx->host_state.fs_reload_needed = 0;
660 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300661 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200662 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300663 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300664 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400665 if (!(vmx->host_state.gs_sel & 7))
666 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300667 else {
668 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200669 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300670 }
671
672#ifdef CONFIG_X86_64
673 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
674 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
675#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400676 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
677 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300678#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300679
680#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300681 if (is_long_mode(&vmx->vcpu)) {
682 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
683 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
684 }
Avi Kivity707c0872007-05-02 17:33:43 +0300685#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300686 for (i = 0; i < vmx->save_nmsrs; ++i)
687 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200688 vmx->guest_msrs[i].data,
689 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300690}
691
Avi Kivitya9b21b62008-06-24 11:48:49 +0300692static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300693{
Avi Kivity15ad7142007-07-11 18:17:21 +0300694 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300695
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400696 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300697 return;
698
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200699 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400700 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200701 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300702 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200703 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300704 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300705 /*
706 * If we have to reload gs, we must take care to
707 * preserve our gs base.
708 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300709 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300710 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300711#ifdef CONFIG_X86_64
712 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
713#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300714 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300715 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200716 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300717#ifdef CONFIG_X86_64
718 if (is_long_mode(&vmx->vcpu)) {
719 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
720 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
721 }
722#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300723}
724
Avi Kivitya9b21b62008-06-24 11:48:49 +0300725static void vmx_load_host_state(struct vcpu_vmx *vmx)
726{
727 preempt_disable();
728 __vmx_load_host_state(vmx);
729 preempt_enable();
730}
731
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732/*
733 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
734 * vcpu mutex is already taken.
735 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300736static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400738 struct vcpu_vmx *vmx = to_vmx(vcpu);
739 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200740 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741
Eddie Donga3d7f852007-09-03 16:15:12 +0300742 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000743 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300744 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300745 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300746 local_irq_disable();
747 list_add(&vmx->local_vcpus_link,
748 &per_cpu(vcpus_on_cpu, cpu));
749 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300750 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400752 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753 u8 error;
754
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400755 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300756 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
758 : "cc");
759 if (error)
760 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400761 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800762 }
763
764 if (vcpu->cpu != cpu) {
765 struct descriptor_table dt;
766 unsigned long sysenter_esp;
767
768 vcpu->cpu = cpu;
769 /*
770 * Linux uses per-cpu TSS and GDT, so set these when switching
771 * processors.
772 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300773 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
774 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
776
777 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
778 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300779
780 /*
781 * Make sure the time stamp counter is monotonous.
782 */
783 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200784 if (tsc_this < vcpu->arch.host_tsc) {
785 delta = vcpu->arch.host_tsc - tsc_this;
786 new_offset = vmcs_read64(TSC_OFFSET) + delta;
787 vmcs_write64(TSC_OFFSET, new_offset);
788 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790}
791
792static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
793{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300794 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795}
796
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300797static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
798{
799 if (vcpu->fpu_active)
800 return;
801 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000802 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity4d4ec082009-12-29 18:07:30 +0200803 if (kvm_read_cr0_bits(vcpu, X86_CR0_TS))
Rusty Russell707d92fa2007-07-17 23:19:08 +1000804 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300805 update_exception_bitmap(vcpu);
806}
807
808static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
809{
Rusty Russell707d92fa2007-07-17 23:19:08 +1000810 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300811 update_exception_bitmap(vcpu);
812}
813
Avi Kivity6aa8b732006-12-10 02:21:36 -0800814static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
815{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300816 unsigned long rflags;
817
818 rflags = vmcs_readl(GUEST_RFLAGS);
819 if (to_vmx(vcpu)->rmode.vm86_active)
820 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
821 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800822}
823
824static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
825{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300826 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100827 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828 vmcs_writel(GUEST_RFLAGS, rflags);
829}
830
Glauber Costa2809f5d2009-05-12 16:21:05 -0400831static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
832{
833 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
834 int ret = 0;
835
836 if (interruptibility & GUEST_INTR_STATE_STI)
837 ret |= X86_SHADOW_INT_STI;
838 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
839 ret |= X86_SHADOW_INT_MOV_SS;
840
841 return ret & mask;
842}
843
844static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
845{
846 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
847 u32 interruptibility = interruptibility_old;
848
849 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
850
851 if (mask & X86_SHADOW_INT_MOV_SS)
852 interruptibility |= GUEST_INTR_STATE_MOV_SS;
853 if (mask & X86_SHADOW_INT_STI)
854 interruptibility |= GUEST_INTR_STATE_STI;
855
856 if ((interruptibility != interruptibility_old))
857 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
858}
859
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
861{
862 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300864 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800865 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300866 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867
Glauber Costa2809f5d2009-05-12 16:21:05 -0400868 /* skipping an emulated instruction also counts */
869 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800870}
871
Avi Kivity298101d2007-11-25 13:41:11 +0200872static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
873 bool has_error_code, u32 error_code)
874{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200875 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100876 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200877
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100878 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200879 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100880 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
881 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200882
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300883 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200884 vmx->rmode.irq.pending = true;
885 vmx->rmode.irq.vector = nr;
886 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300887 if (kvm_exception_is_soft(nr))
888 vmx->rmode.irq.rip +=
889 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100890 intr_info |= INTR_TYPE_SOFT_INTR;
891 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200892 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
893 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
894 return;
895 }
896
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300897 if (kvm_exception_is_soft(nr)) {
898 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
899 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100900 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
901 } else
902 intr_info |= INTR_TYPE_HARD_EXCEPTION;
903
904 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200905}
906
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800907static bool vmx_rdtscp_supported(void)
908{
909 return cpu_has_vmx_rdtscp();
910}
911
Avi Kivity6aa8b732006-12-10 02:21:36 -0800912/*
Eddie Donga75beee2007-05-17 18:55:15 +0300913 * Swap MSR entry in host/guest MSR entry array.
914 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000915static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300916{
Avi Kivity26bb0982009-09-07 11:14:12 +0300917 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400918
919 tmp = vmx->guest_msrs[to];
920 vmx->guest_msrs[to] = vmx->guest_msrs[from];
921 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300922}
923
924/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300925 * Set up the vmcs to automatically save and restore system
926 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
927 * mode, as fiddling with msrs is very expensive.
928 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000929static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300930{
Avi Kivity26bb0982009-09-07 11:14:12 +0300931 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +0200932 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300933
Avi Kivity33f9c502008-02-27 16:06:57 +0200934 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300935 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300936#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000937 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000938 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300939 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000940 move_msr_up(vmx, index, save_nmsrs++);
941 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300942 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000943 move_msr_up(vmx, index, save_nmsrs++);
944 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300945 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000946 move_msr_up(vmx, index, save_nmsrs++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800947 index = __find_msr_index(vmx, MSR_TSC_AUX);
948 if (index >= 0 && vmx->rdtscp_enabled)
949 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300950 /*
951 * MSR_K6_STAR is only needed on long mode guests, and only
952 * if efer.sce is enabled.
953 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000954 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800955 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000956 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300957 }
Eddie Donga75beee2007-05-17 18:55:15 +0300958#endif
Avi Kivity92c0d902009-10-29 11:00:16 +0200959 index = __find_msr_index(vmx, MSR_EFER);
960 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +0300961 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300962
Avi Kivity26bb0982009-09-07 11:14:12 +0300963 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200964
965 if (cpu_has_vmx_msr_bitmap()) {
966 if (is_long_mode(&vmx->vcpu))
967 msr_bitmap = vmx_msr_bitmap_longmode;
968 else
969 msr_bitmap = vmx_msr_bitmap_legacy;
970
971 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
972 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300973}
974
975/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976 * reads and returns guest's timestamp counter "register"
977 * guest_tsc = host_tsc + tsc_offset -- 21.3
978 */
979static u64 guest_read_tsc(void)
980{
981 u64 host_tsc, tsc_offset;
982
983 rdtscll(host_tsc);
984 tsc_offset = vmcs_read64(TSC_OFFSET);
985 return host_tsc + tsc_offset;
986}
987
988/*
989 * writes 'guest_tsc' into guest's timestamp counter "register"
990 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
991 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100992static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800993{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
995}
996
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997/*
998 * Reads an msr value (of 'msr_index') into 'pdata'.
999 * Returns 0 on success, non-0 otherwise.
1000 * Assumes vcpu_load() was already called.
1001 */
1002static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1003{
1004 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001005 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001006
1007 if (!pdata) {
1008 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1009 return -EINVAL;
1010 }
1011
1012 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001013#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 case MSR_FS_BASE:
1015 data = vmcs_readl(GUEST_FS_BASE);
1016 break;
1017 case MSR_GS_BASE:
1018 data = vmcs_readl(GUEST_GS_BASE);
1019 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001020 case MSR_KERNEL_GS_BASE:
1021 vmx_load_host_state(to_vmx(vcpu));
1022 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1023 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001024#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001026 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301027 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001028 data = guest_read_tsc();
1029 break;
1030 case MSR_IA32_SYSENTER_CS:
1031 data = vmcs_read32(GUEST_SYSENTER_CS);
1032 break;
1033 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001034 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 break;
1036 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001037 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001039 case MSR_TSC_AUX:
1040 if (!to_vmx(vcpu)->rdtscp_enabled)
1041 return 1;
1042 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001043 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001044 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001045 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001046 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001047 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001048 data = msr->data;
1049 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001051 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 }
1053
1054 *pdata = data;
1055 return 0;
1056}
1057
1058/*
1059 * Writes msr value into into the appropriate "register".
1060 * Returns 0 on success, non-0 otherwise.
1061 * Assumes vcpu_load() was already called.
1062 */
1063static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1064{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001065 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001066 struct shared_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001067 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001068 int ret = 0;
1069
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001071 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001072 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001073 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001074 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001075#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001076 case MSR_FS_BASE:
1077 vmcs_writel(GUEST_FS_BASE, data);
1078 break;
1079 case MSR_GS_BASE:
1080 vmcs_writel(GUEST_GS_BASE, data);
1081 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001082 case MSR_KERNEL_GS_BASE:
1083 vmx_load_host_state(vmx);
1084 vmx->msr_guest_kernel_gs_base = data;
1085 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001086#endif
1087 case MSR_IA32_SYSENTER_CS:
1088 vmcs_write32(GUEST_SYSENTER_CS, data);
1089 break;
1090 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001091 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 break;
1093 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001094 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301096 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001097 rdtscll(host_tsc);
1098 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001100 case MSR_IA32_CR_PAT:
1101 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1102 vmcs_write64(GUEST_IA32_PAT, data);
1103 vcpu->arch.pat = data;
1104 break;
1105 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001106 ret = kvm_set_msr_common(vcpu, msr_index, data);
1107 break;
1108 case MSR_TSC_AUX:
1109 if (!vmx->rdtscp_enabled)
1110 return 1;
1111 /* Check reserved bit, higher 32 bits should be zero */
1112 if ((data >> 32) != 0)
1113 return 1;
1114 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001115 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001116 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001117 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001118 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001119 msr->data = data;
1120 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001122 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123 }
1124
Eddie Dong2cc51562007-05-21 07:28:09 +03001125 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126}
1127
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001128static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001129{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001130 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1131 switch (reg) {
1132 case VCPU_REGS_RSP:
1133 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1134 break;
1135 case VCPU_REGS_RIP:
1136 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1137 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001138 case VCPU_EXREG_PDPTR:
1139 if (enable_ept)
1140 ept_save_pdptrs(vcpu);
1141 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001142 default:
1143 break;
1144 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145}
1146
Jan Kiszka355be0b2009-10-03 00:31:21 +02001147static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001148{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001149 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1150 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1151 else
1152 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1153
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001154 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001155}
1156
1157static __init int cpu_has_kvm_support(void)
1158{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001159 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160}
1161
1162static __init int vmx_disabled_by_bios(void)
1163{
1164 u64 msr;
1165
1166 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001167 return (msr & (FEATURE_CONTROL_LOCKED |
1168 FEATURE_CONTROL_VMXON_ENABLED))
1169 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001170 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171}
1172
Alexander Graf10474ae2009-09-15 11:37:46 +02001173static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174{
1175 int cpu = raw_smp_processor_id();
1176 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1177 u64 old;
1178
Alexander Graf10474ae2009-09-15 11:37:46 +02001179 if (read_cr4() & X86_CR4_VMXE)
1180 return -EBUSY;
1181
Avi Kivity543e4242008-05-13 16:22:47 +03001182 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001184 if ((old & (FEATURE_CONTROL_LOCKED |
1185 FEATURE_CONTROL_VMXON_ENABLED))
1186 != (FEATURE_CONTROL_LOCKED |
1187 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001188 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001189 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001190 FEATURE_CONTROL_LOCKED |
1191 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001192 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001193 asm volatile (ASM_VMX_VMXON_RAX
1194 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001195 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001196
1197 ept_sync_global();
1198
1199 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200}
1201
Avi Kivity543e4242008-05-13 16:22:47 +03001202static void vmclear_local_vcpus(void)
1203{
1204 int cpu = raw_smp_processor_id();
1205 struct vcpu_vmx *vmx, *n;
1206
1207 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1208 local_vcpus_link)
1209 __vcpu_clear(vmx);
1210}
1211
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001212
1213/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1214 * tricks.
1215 */
1216static void kvm_cpu_vmxoff(void)
1217{
1218 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1219 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1220}
1221
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222static void hardware_disable(void *garbage)
1223{
Avi Kivity543e4242008-05-13 16:22:47 +03001224 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001225 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001226}
1227
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001228static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001229 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230{
1231 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001232 u32 ctl = ctl_min | ctl_opt;
1233
1234 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1235
1236 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1237 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1238
1239 /* Ensure minimum (required) set of control bits are supported. */
1240 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001241 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001242
1243 *result = ctl;
1244 return 0;
1245}
1246
Yang, Sheng002c7f72007-07-31 14:23:01 +03001247static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001248{
1249 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001250 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001251 u32 _pin_based_exec_control = 0;
1252 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001253 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001254 u32 _vmexit_control = 0;
1255 u32 _vmentry_control = 0;
1256
1257 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001258 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001259 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1260 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001261 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001262
1263 min = CPU_BASED_HLT_EXITING |
1264#ifdef CONFIG_X86_64
1265 CPU_BASED_CR8_LOAD_EXITING |
1266 CPU_BASED_CR8_STORE_EXITING |
1267#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001268 CPU_BASED_CR3_LOAD_EXITING |
1269 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001270 CPU_BASED_USE_IO_BITMAPS |
1271 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001272 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001273 CPU_BASED_MWAIT_EXITING |
1274 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001275 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001276 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001277 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001278 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001279 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1280 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001281 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001282#ifdef CONFIG_X86_64
1283 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1284 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1285 ~CPU_BASED_CR8_STORE_EXITING;
1286#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001287 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001288 min2 = 0;
1289 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001290 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001291 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001292 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001293 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001294 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1295 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001296 if (adjust_vmx_controls(min2, opt2,
1297 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001298 &_cpu_based_2nd_exec_control) < 0)
1299 return -EIO;
1300 }
1301#ifndef CONFIG_X86_64
1302 if (!(_cpu_based_2nd_exec_control &
1303 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1304 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1305#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001306 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001307 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1308 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001309 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1310 CPU_BASED_CR3_STORE_EXITING |
1311 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001312 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1313 vmx_capability.ept, vmx_capability.vpid);
1314 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001315
1316 min = 0;
1317#ifdef CONFIG_X86_64
1318 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1319#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001320 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001321 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1322 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001323 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001324
Sheng Yang468d4722008-10-09 16:01:55 +08001325 min = 0;
1326 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001327 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1328 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001329 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001331 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001332
1333 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1334 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001335 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001336
1337#ifdef CONFIG_X86_64
1338 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1339 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001340 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001341#endif
1342
1343 /* Require Write-Back (WB) memory type for VMCS accesses. */
1344 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001345 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001346
Yang, Sheng002c7f72007-07-31 14:23:01 +03001347 vmcs_conf->size = vmx_msr_high & 0x1fff;
1348 vmcs_conf->order = get_order(vmcs_config.size);
1349 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001350
Yang, Sheng002c7f72007-07-31 14:23:01 +03001351 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1352 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001353 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001354 vmcs_conf->vmexit_ctrl = _vmexit_control;
1355 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001356
1357 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001358}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359
1360static struct vmcs *alloc_vmcs_cpu(int cpu)
1361{
1362 int node = cpu_to_node(cpu);
1363 struct page *pages;
1364 struct vmcs *vmcs;
1365
Mel Gorman6484eb32009-06-16 15:31:54 -07001366 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001367 if (!pages)
1368 return NULL;
1369 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001370 memset(vmcs, 0, vmcs_config.size);
1371 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372 return vmcs;
1373}
1374
1375static struct vmcs *alloc_vmcs(void)
1376{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001377 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378}
1379
1380static void free_vmcs(struct vmcs *vmcs)
1381{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001382 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383}
1384
Sam Ravnborg39959582007-06-01 00:47:13 -07001385static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386{
1387 int cpu;
1388
Zachary Amsden3230bb42009-09-29 11:38:37 -10001389 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001391 per_cpu(vmxarea, cpu) = NULL;
1392 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001393}
1394
Avi Kivity6aa8b732006-12-10 02:21:36 -08001395static __init int alloc_kvm_area(void)
1396{
1397 int cpu;
1398
Zachary Amsden3230bb42009-09-29 11:38:37 -10001399 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400 struct vmcs *vmcs;
1401
1402 vmcs = alloc_vmcs_cpu(cpu);
1403 if (!vmcs) {
1404 free_kvm_area();
1405 return -ENOMEM;
1406 }
1407
1408 per_cpu(vmxarea, cpu) = vmcs;
1409 }
1410 return 0;
1411}
1412
1413static __init int hardware_setup(void)
1414{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001415 if (setup_vmcs_config(&vmcs_config) < 0)
1416 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001417
1418 if (boot_cpu_has(X86_FEATURE_NX))
1419 kvm_enable_efer_bits(EFER_NX);
1420
Sheng Yang93ba03c2009-04-01 15:52:32 +08001421 if (!cpu_has_vmx_vpid())
1422 enable_vpid = 0;
1423
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001424 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001425 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001426 enable_unrestricted_guest = 0;
1427 }
1428
1429 if (!cpu_has_vmx_unrestricted_guest())
1430 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001431
1432 if (!cpu_has_vmx_flexpriority())
1433 flexpriority_enabled = 0;
1434
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001435 if (!cpu_has_vmx_tpr_shadow())
1436 kvm_x86_ops->update_cr8_intercept = NULL;
1437
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001438 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1439 kvm_disable_largepages();
1440
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001441 if (!cpu_has_vmx_ple())
1442 ple_gap = 0;
1443
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 return alloc_kvm_area();
1445}
1446
1447static __exit void hardware_unsetup(void)
1448{
1449 free_kvm_area();
1450}
1451
Avi Kivity6aa8b732006-12-10 02:21:36 -08001452static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1453{
1454 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1455
Avi Kivity6af11b92007-03-19 13:18:10 +02001456 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001457 vmcs_write16(sf->selector, save->selector);
1458 vmcs_writel(sf->base, save->base);
1459 vmcs_write32(sf->limit, save->limit);
1460 vmcs_write32(sf->ar_bytes, save->ar);
1461 } else {
1462 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1463 << AR_DPL_SHIFT;
1464 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1465 }
1466}
1467
1468static void enter_pmode(struct kvm_vcpu *vcpu)
1469{
1470 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001471 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001473 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001474 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001476 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1477 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1478 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479
1480 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001481 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001482 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001483 vmcs_writel(GUEST_RFLAGS, flags);
1484
Rusty Russell66aee912007-07-17 23:34:16 +10001485 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1486 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487
1488 update_exception_bitmap(vcpu);
1489
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001490 if (emulate_invalid_guest_state)
1491 return;
1492
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001493 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1494 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1495 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1496 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001497
1498 vmcs_write16(GUEST_SS_SELECTOR, 0);
1499 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1500
1501 vmcs_write16(GUEST_CS_SELECTOR,
1502 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1503 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1504}
1505
Mike Dayd77c26f2007-10-08 09:02:08 -04001506static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001507{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001508 if (!kvm->arch.tss_addr) {
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001509 struct kvm_memslots *slots;
1510 gfn_t base_gfn;
1511
1512 slots = rcu_dereference(kvm->memslots);
1513 base_gfn = kvm->memslots->memslots[0].base_gfn +
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001514 kvm->memslots->memslots[0].npages - 3;
Izik Eiduscbc94022007-10-25 00:29:55 +02001515 return base_gfn << PAGE_SHIFT;
1516 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001517 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518}
1519
1520static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1521{
1522 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1523
1524 save->selector = vmcs_read16(sf->selector);
1525 save->base = vmcs_readl(sf->base);
1526 save->limit = vmcs_read32(sf->limit);
1527 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001528 vmcs_write16(sf->selector, save->base >> 4);
1529 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001530 vmcs_write32(sf->limit, 0xffff);
1531 vmcs_write32(sf->ar_bytes, 0xf3);
1532}
1533
1534static void enter_rmode(struct kvm_vcpu *vcpu)
1535{
1536 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001537 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001539 if (enable_unrestricted_guest)
1540 return;
1541
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001542 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001543 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001544
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001545 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1547
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001548 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001549 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1550
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001551 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1553
1554 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001555 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001556 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001557
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001558 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559
1560 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001561 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001562 update_exception_bitmap(vcpu);
1563
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001564 if (emulate_invalid_guest_state)
1565 goto continue_rmode;
1566
Avi Kivity6aa8b732006-12-10 02:21:36 -08001567 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1568 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1569 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1570
1571 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001572 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001573 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1574 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001575 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1576
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001577 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1578 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1579 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1580 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001581
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001582continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001583 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001584 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001585}
1586
Amit Shah401d10d2009-02-20 22:53:37 +05301587static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1588{
1589 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001590 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1591
1592 if (!msr)
1593 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301594
Avi Kivity44ea2b12009-09-06 15:55:37 +03001595 /*
1596 * Force kernel_gs_base reloading before EFER changes, as control
1597 * of this msr depends on is_long_mode().
1598 */
1599 vmx_load_host_state(to_vmx(vcpu));
Amit Shah401d10d2009-02-20 22:53:37 +05301600 vcpu->arch.shadow_efer = efer;
1601 if (!msr)
1602 return;
1603 if (efer & EFER_LMA) {
1604 vmcs_write32(VM_ENTRY_CONTROLS,
1605 vmcs_read32(VM_ENTRY_CONTROLS) |
1606 VM_ENTRY_IA32E_MODE);
1607 msr->data = efer;
1608 } else {
1609 vmcs_write32(VM_ENTRY_CONTROLS,
1610 vmcs_read32(VM_ENTRY_CONTROLS) &
1611 ~VM_ENTRY_IA32E_MODE);
1612
1613 msr->data = efer & ~EFER_LME;
1614 }
1615 setup_msrs(vmx);
1616}
1617
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001618#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619
1620static void enter_lmode(struct kvm_vcpu *vcpu)
1621{
1622 u32 guest_tr_ar;
1623
1624 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1625 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1626 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001627 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628 vmcs_write32(GUEST_TR_AR_BYTES,
1629 (guest_tr_ar & ~AR_TYPE_MASK)
1630 | AR_TYPE_BUSY_64_TSS);
1631 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001632 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301633 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001634}
1635
1636static void exit_lmode(struct kvm_vcpu *vcpu)
1637{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001638 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639
1640 vmcs_write32(VM_ENTRY_CONTROLS,
1641 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001642 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001643}
1644
1645#endif
1646
Sheng Yang2384d2b2008-01-17 15:14:33 +08001647static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1648{
1649 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001650 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001651 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001652}
1653
Avi Kivitye8467fd2009-12-29 18:43:06 +02001654static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1655{
1656 ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
1657
1658 vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
1659 vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
1660}
1661
Anthony Liguori25c4c272007-04-27 09:29:21 +03001662static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001663{
Avi Kivityfc78f512009-12-07 12:16:48 +02001664 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
1665
1666 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
1667 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08001668}
1669
Sheng Yang14394422008-04-28 12:24:45 +08001670static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1671{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001672 if (!test_bit(VCPU_EXREG_PDPTR,
1673 (unsigned long *)&vcpu->arch.regs_dirty))
1674 return;
1675
Sheng Yang14394422008-04-28 12:24:45 +08001676 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001677 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1678 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1679 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1680 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1681 }
1682}
1683
Avi Kivity8f5d5492009-05-31 18:41:29 +03001684static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1685{
1686 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1687 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1688 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1689 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1690 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1691 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001692
1693 __set_bit(VCPU_EXREG_PDPTR,
1694 (unsigned long *)&vcpu->arch.regs_avail);
1695 __set_bit(VCPU_EXREG_PDPTR,
1696 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001697}
1698
Sheng Yang14394422008-04-28 12:24:45 +08001699static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1700
1701static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1702 unsigned long cr0,
1703 struct kvm_vcpu *vcpu)
1704{
1705 if (!(cr0 & X86_CR0_PG)) {
1706 /* From paging/starting to nonpaging */
1707 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001708 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001709 (CPU_BASED_CR3_LOAD_EXITING |
1710 CPU_BASED_CR3_STORE_EXITING));
1711 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001712 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001713 } else if (!is_paging(vcpu)) {
1714 /* From nonpaging to paging */
1715 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001716 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001717 ~(CPU_BASED_CR3_LOAD_EXITING |
1718 CPU_BASED_CR3_STORE_EXITING));
1719 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001720 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001721 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001722
1723 if (!(cr0 & X86_CR0_WP))
1724 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001725}
1726
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1728{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001729 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001730 unsigned long hw_cr0;
1731
1732 if (enable_unrestricted_guest)
1733 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1734 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1735 else
1736 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001737
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001738 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001739 enter_pmode(vcpu);
1740
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001741 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001742 enter_rmode(vcpu);
1743
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001744#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001745 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001746 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001748 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749 exit_lmode(vcpu);
1750 }
1751#endif
1752
Avi Kivity089d0342009-03-23 18:26:32 +02001753 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001754 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1755
Avi Kivity02daab22009-12-30 12:40:26 +02001756 if (!vcpu->fpu_active)
1757 hw_cr0 |= X86_CR0_TS;
1758
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001760 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001761 vcpu->arch.cr0 = cr0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762}
1763
Sheng Yang14394422008-04-28 12:24:45 +08001764static u64 construct_eptp(unsigned long root_hpa)
1765{
1766 u64 eptp;
1767
1768 /* TODO write the value reading from MSR */
1769 eptp = VMX_EPT_DEFAULT_MT |
1770 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1771 eptp |= (root_hpa & PAGE_MASK);
1772
1773 return eptp;
1774}
1775
Avi Kivity6aa8b732006-12-10 02:21:36 -08001776static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1777{
Sheng Yang14394422008-04-28 12:24:45 +08001778 unsigned long guest_cr3;
1779 u64 eptp;
1780
1781 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001782 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001783 eptp = construct_eptp(cr3);
1784 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001785 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001786 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02001787 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001788 }
1789
Sheng Yang2384d2b2008-01-17 15:14:33 +08001790 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001791 vmcs_writel(GUEST_CR3, guest_cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792}
1793
1794static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1795{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001796 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001797 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1798
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001799 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02001800 if (enable_ept) {
1801 if (!is_paging(vcpu)) {
1802 hw_cr4 &= ~X86_CR4_PAE;
1803 hw_cr4 |= X86_CR4_PSE;
1804 } else if (!(cr4 & X86_CR4_PAE)) {
1805 hw_cr4 &= ~X86_CR4_PAE;
1806 }
1807 }
Sheng Yang14394422008-04-28 12:24:45 +08001808
1809 vmcs_writel(CR4_READ_SHADOW, cr4);
1810 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001811}
1812
Avi Kivity6aa8b732006-12-10 02:21:36 -08001813static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1814{
1815 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1816
1817 return vmcs_readl(sf->base);
1818}
1819
1820static void vmx_get_segment(struct kvm_vcpu *vcpu,
1821 struct kvm_segment *var, int seg)
1822{
1823 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1824 u32 ar;
1825
1826 var->base = vmcs_readl(sf->base);
1827 var->limit = vmcs_read32(sf->limit);
1828 var->selector = vmcs_read16(sf->selector);
1829 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001830 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001831 ar = 0;
1832 var->type = ar & 15;
1833 var->s = (ar >> 4) & 1;
1834 var->dpl = (ar >> 5) & 3;
1835 var->present = (ar >> 7) & 1;
1836 var->avl = (ar >> 12) & 1;
1837 var->l = (ar >> 13) & 1;
1838 var->db = (ar >> 14) & 1;
1839 var->g = (ar >> 15) & 1;
1840 var->unusable = (ar >> 16) & 1;
1841}
1842
Izik Eidus2e4d2652008-03-24 19:38:34 +02001843static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1844{
Avi Kivity4d4ec082009-12-29 18:07:30 +02001845 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) /* if real mode */
Izik Eidus2e4d2652008-03-24 19:38:34 +02001846 return 0;
1847
1848 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1849 return 3;
1850
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001851 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001852}
1853
Avi Kivity653e3102007-05-07 10:55:37 +03001854static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001855{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001856 u32 ar;
1857
Avi Kivity653e3102007-05-07 10:55:37 +03001858 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859 ar = 1 << 16;
1860 else {
1861 ar = var->type & 15;
1862 ar |= (var->s & 1) << 4;
1863 ar |= (var->dpl & 3) << 5;
1864 ar |= (var->present & 1) << 7;
1865 ar |= (var->avl & 1) << 12;
1866 ar |= (var->l & 1) << 13;
1867 ar |= (var->db & 1) << 14;
1868 ar |= (var->g & 1) << 15;
1869 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001870 if (ar == 0) /* a 0 value means unusable */
1871 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001872
1873 return ar;
1874}
1875
1876static void vmx_set_segment(struct kvm_vcpu *vcpu,
1877 struct kvm_segment *var, int seg)
1878{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001879 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001880 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1881 u32 ar;
1882
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001883 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1884 vmx->rmode.tr.selector = var->selector;
1885 vmx->rmode.tr.base = var->base;
1886 vmx->rmode.tr.limit = var->limit;
1887 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001888 return;
1889 }
1890 vmcs_writel(sf->base, var->base);
1891 vmcs_write32(sf->limit, var->limit);
1892 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001893 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001894 /*
1895 * Hack real-mode segments into vm86 compatibility.
1896 */
1897 if (var->base == 0xffff0000 && var->selector == 0xf000)
1898 vmcs_writel(sf->base, 0xf0000);
1899 ar = 0xf3;
1900 } else
1901 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001902
1903 /*
1904 * Fix the "Accessed" bit in AR field of segment registers for older
1905 * qemu binaries.
1906 * IA32 arch specifies that at the time of processor reset the
1907 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1908 * is setting it to 0 in the usedland code. This causes invalid guest
1909 * state vmexit when "unrestricted guest" mode is turned on.
1910 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1911 * tree. Newer qemu binaries with that qemu fix would not need this
1912 * kvm hack.
1913 */
1914 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1915 ar |= 0x1; /* Accessed */
1916
Avi Kivity6aa8b732006-12-10 02:21:36 -08001917 vmcs_write32(sf->ar_bytes, ar);
1918}
1919
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1921{
1922 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1923
1924 *db = (ar >> 14) & 1;
1925 *l = (ar >> 13) & 1;
1926}
1927
1928static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1929{
1930 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1931 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1932}
1933
1934static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1935{
1936 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1937 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1938}
1939
1940static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1941{
1942 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1943 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1944}
1945
1946static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1947{
1948 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1949 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1950}
1951
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001952static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1953{
1954 struct kvm_segment var;
1955 u32 ar;
1956
1957 vmx_get_segment(vcpu, &var, seg);
1958 ar = vmx_segment_access_rights(&var);
1959
1960 if (var.base != (var.selector << 4))
1961 return false;
1962 if (var.limit != 0xffff)
1963 return false;
1964 if (ar != 0xf3)
1965 return false;
1966
1967 return true;
1968}
1969
1970static bool code_segment_valid(struct kvm_vcpu *vcpu)
1971{
1972 struct kvm_segment cs;
1973 unsigned int cs_rpl;
1974
1975 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1976 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1977
Avi Kivity1872a3f2009-01-04 23:26:52 +02001978 if (cs.unusable)
1979 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001980 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1981 return false;
1982 if (!cs.s)
1983 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001984 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001985 if (cs.dpl > cs_rpl)
1986 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001987 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001988 if (cs.dpl != cs_rpl)
1989 return false;
1990 }
1991 if (!cs.present)
1992 return false;
1993
1994 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1995 return true;
1996}
1997
1998static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1999{
2000 struct kvm_segment ss;
2001 unsigned int ss_rpl;
2002
2003 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2004 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
2005
Avi Kivity1872a3f2009-01-04 23:26:52 +02002006 if (ss.unusable)
2007 return true;
2008 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002009 return false;
2010 if (!ss.s)
2011 return false;
2012 if (ss.dpl != ss_rpl) /* DPL != RPL */
2013 return false;
2014 if (!ss.present)
2015 return false;
2016
2017 return true;
2018}
2019
2020static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2021{
2022 struct kvm_segment var;
2023 unsigned int rpl;
2024
2025 vmx_get_segment(vcpu, &var, seg);
2026 rpl = var.selector & SELECTOR_RPL_MASK;
2027
Avi Kivity1872a3f2009-01-04 23:26:52 +02002028 if (var.unusable)
2029 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002030 if (!var.s)
2031 return false;
2032 if (!var.present)
2033 return false;
2034 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2035 if (var.dpl < rpl) /* DPL < RPL */
2036 return false;
2037 }
2038
2039 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2040 * rights flags
2041 */
2042 return true;
2043}
2044
2045static bool tr_valid(struct kvm_vcpu *vcpu)
2046{
2047 struct kvm_segment tr;
2048
2049 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2050
Avi Kivity1872a3f2009-01-04 23:26:52 +02002051 if (tr.unusable)
2052 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002053 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2054 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002055 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002056 return false;
2057 if (!tr.present)
2058 return false;
2059
2060 return true;
2061}
2062
2063static bool ldtr_valid(struct kvm_vcpu *vcpu)
2064{
2065 struct kvm_segment ldtr;
2066
2067 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2068
Avi Kivity1872a3f2009-01-04 23:26:52 +02002069 if (ldtr.unusable)
2070 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002071 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2072 return false;
2073 if (ldtr.type != 2)
2074 return false;
2075 if (!ldtr.present)
2076 return false;
2077
2078 return true;
2079}
2080
2081static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2082{
2083 struct kvm_segment cs, ss;
2084
2085 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2086 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2087
2088 return ((cs.selector & SELECTOR_RPL_MASK) ==
2089 (ss.selector & SELECTOR_RPL_MASK));
2090}
2091
2092/*
2093 * Check if guest state is valid. Returns true if valid, false if
2094 * not.
2095 * We assume that registers are always usable
2096 */
2097static bool guest_state_valid(struct kvm_vcpu *vcpu)
2098{
2099 /* real mode guest state checks */
Avi Kivity4d4ec082009-12-29 18:07:30 +02002100 if (!kvm_read_cr0_bits(vcpu, X86_CR0_PE)) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002101 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2102 return false;
2103 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2104 return false;
2105 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2106 return false;
2107 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2108 return false;
2109 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2110 return false;
2111 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2112 return false;
2113 } else {
2114 /* protected mode guest state checks */
2115 if (!cs_ss_rpl_check(vcpu))
2116 return false;
2117 if (!code_segment_valid(vcpu))
2118 return false;
2119 if (!stack_segment_valid(vcpu))
2120 return false;
2121 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2122 return false;
2123 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2124 return false;
2125 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2126 return false;
2127 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2128 return false;
2129 if (!tr_valid(vcpu))
2130 return false;
2131 if (!ldtr_valid(vcpu))
2132 return false;
2133 }
2134 /* TODO:
2135 * - Add checks on RIP
2136 * - Add checks on RFLAGS
2137 */
2138
2139 return true;
2140}
2141
Mike Dayd77c26f2007-10-08 09:02:08 -04002142static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002143{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002144 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002145 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002146 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002147 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002148
Izik Eidus195aefd2007-10-01 22:14:18 +02002149 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2150 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002151 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002152 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002153 r = kvm_write_guest_page(kvm, fn++, &data,
2154 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002155 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002156 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002157 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2158 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002159 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002160 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2161 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002162 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002163 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002164 r = kvm_write_guest_page(kvm, fn, &data,
2165 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2166 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002167 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002168 goto out;
2169
2170 ret = 1;
2171out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002172 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173}
2174
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002175static int init_rmode_identity_map(struct kvm *kvm)
2176{
2177 int i, r, ret;
2178 pfn_t identity_map_pfn;
2179 u32 tmp;
2180
Avi Kivity089d0342009-03-23 18:26:32 +02002181 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002182 return 1;
2183 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2184 printk(KERN_ERR "EPT: identity-mapping pagetable "
2185 "haven't been allocated!\n");
2186 return 0;
2187 }
2188 if (likely(kvm->arch.ept_identity_pagetable_done))
2189 return 1;
2190 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002191 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002192 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2193 if (r < 0)
2194 goto out;
2195 /* Set up identity-mapping pagetable for EPT in real mode */
2196 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2197 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2198 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2199 r = kvm_write_guest_page(kvm, identity_map_pfn,
2200 &tmp, i * sizeof(tmp), sizeof(tmp));
2201 if (r < 0)
2202 goto out;
2203 }
2204 kvm->arch.ept_identity_pagetable_done = true;
2205 ret = 1;
2206out:
2207 return ret;
2208}
2209
Avi Kivity6aa8b732006-12-10 02:21:36 -08002210static void seg_setup(int seg)
2211{
2212 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002213 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002214
2215 vmcs_write16(sf->selector, 0);
2216 vmcs_writel(sf->base, 0);
2217 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002218 if (enable_unrestricted_guest) {
2219 ar = 0x93;
2220 if (seg == VCPU_SREG_CS)
2221 ar |= 0x08; /* code segment */
2222 } else
2223 ar = 0xf3;
2224
2225 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002226}
2227
Sheng Yangf78e0e22007-10-29 09:40:42 +08002228static int alloc_apic_access_page(struct kvm *kvm)
2229{
2230 struct kvm_userspace_memory_region kvm_userspace_mem;
2231 int r = 0;
2232
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002233 mutex_lock(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002234 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002235 goto out;
2236 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2237 kvm_userspace_mem.flags = 0;
2238 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2239 kvm_userspace_mem.memory_size = PAGE_SIZE;
2240 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2241 if (r)
2242 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002243
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002244 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002245out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002246 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002247 return r;
2248}
2249
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002250static int alloc_identity_pagetable(struct kvm *kvm)
2251{
2252 struct kvm_userspace_memory_region kvm_userspace_mem;
2253 int r = 0;
2254
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002255 mutex_lock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002256 if (kvm->arch.ept_identity_pagetable)
2257 goto out;
2258 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2259 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002260 kvm_userspace_mem.guest_phys_addr =
2261 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002262 kvm_userspace_mem.memory_size = PAGE_SIZE;
2263 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2264 if (r)
2265 goto out;
2266
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002267 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002268 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002269out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02002270 mutex_unlock(&kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002271 return r;
2272}
2273
Sheng Yang2384d2b2008-01-17 15:14:33 +08002274static void allocate_vpid(struct vcpu_vmx *vmx)
2275{
2276 int vpid;
2277
2278 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002279 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002280 return;
2281 spin_lock(&vmx_vpid_lock);
2282 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2283 if (vpid < VMX_NR_VPIDS) {
2284 vmx->vpid = vpid;
2285 __set_bit(vpid, vmx_vpid_bitmap);
2286 }
2287 spin_unlock(&vmx_vpid_lock);
2288}
2289
Avi Kivity58972972009-02-24 22:26:47 +02002290static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002291{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002292 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002293
2294 if (!cpu_has_vmx_msr_bitmap())
2295 return;
2296
2297 /*
2298 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2299 * have the write-low and read-high bitmap offsets the wrong way round.
2300 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2301 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002302 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002303 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2304 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002305 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2306 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002307 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2308 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002309 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002310}
2311
Avi Kivity58972972009-02-24 22:26:47 +02002312static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2313{
2314 if (!longmode_only)
2315 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2316 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2317}
2318
Avi Kivity6aa8b732006-12-10 02:21:36 -08002319/*
2320 * Sets up the vmcs for emulated real mode.
2321 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002322static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002323{
Sheng Yang468d4722008-10-09 16:01:55 +08002324 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002326 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002327 unsigned long a;
2328 struct descriptor_table dt;
2329 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002330 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002331 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002334 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2335 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002336
Sheng Yang25c5f222008-03-28 13:18:56 +08002337 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002338 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002339
Avi Kivity6aa8b732006-12-10 02:21:36 -08002340 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2341
Avi Kivity6aa8b732006-12-10 02:21:36 -08002342 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002343 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2344 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002345
2346 exec_control = vmcs_config.cpu_based_exec_ctrl;
2347 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2348 exec_control &= ~CPU_BASED_TPR_SHADOW;
2349#ifdef CONFIG_X86_64
2350 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2351 CPU_BASED_CR8_LOAD_EXITING;
2352#endif
2353 }
Avi Kivity089d0342009-03-23 18:26:32 +02002354 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002355 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002356 CPU_BASED_CR3_LOAD_EXITING |
2357 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002358 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002359
Sheng Yang83ff3b92007-11-21 14:33:25 +08002360 if (cpu_has_secondary_exec_ctrls()) {
2361 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2362 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2363 exec_control &=
2364 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002365 if (vmx->vpid == 0)
2366 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002367 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002368 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002369 enable_unrestricted_guest = 0;
2370 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002371 if (!enable_unrestricted_guest)
2372 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002373 if (!ple_gap)
2374 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002375 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2376 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002377
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002378 if (ple_gap) {
2379 vmcs_write32(PLE_GAP, ple_gap);
2380 vmcs_write32(PLE_WINDOW, ple_window);
2381 }
2382
Avi Kivityc7addb92007-09-16 18:58:32 +02002383 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2384 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2386
2387 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2388 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2389 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2390
2391 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2392 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2393 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002394 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2395 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002397#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002398 rdmsrl(MSR_FS_BASE, a);
2399 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2400 rdmsrl(MSR_GS_BASE, a);
2401 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2402#else
2403 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2404 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2405#endif
2406
2407 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2408
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002409 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002410 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2411
Mike Dayd77c26f2007-10-08 09:02:08 -04002412 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002413 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002414 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2415 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2416 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002417
2418 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2419 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2420 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2421 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2422 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2423 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2424
Sheng Yang468d4722008-10-09 16:01:55 +08002425 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2426 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2427 host_pat = msr_low | ((u64) msr_high << 32);
2428 vmcs_write64(HOST_IA32_PAT, host_pat);
2429 }
2430 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2431 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2432 host_pat = msr_low | ((u64) msr_high << 32);
2433 /* Write the default value follow host pat */
2434 vmcs_write64(GUEST_IA32_PAT, host_pat);
2435 /* Keep arch.pat sync with GUEST_IA32_PAT */
2436 vmx->vcpu.arch.pat = host_pat;
2437 }
2438
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 for (i = 0; i < NR_VMX_MSR; ++i) {
2440 u32 index = vmx_msr_index[i];
2441 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002442 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443
2444 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2445 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002446 if (wrmsr_safe(index, data_low, data_high) < 0)
2447 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03002448 vmx->guest_msrs[j].index = i;
2449 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002450 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002451 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002454 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455
2456 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002457 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2458
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002459 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02002460 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02002461 if (enable_ept)
2462 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02002463 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002464
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002465 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2466 rdtscll(tsc_this);
2467 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2468 tsc_base = tsc_this;
2469
2470 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002471
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002472 return 0;
2473}
2474
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002475static int init_rmode(struct kvm *kvm)
2476{
2477 if (!init_rmode_tss(kvm))
2478 return 0;
2479 if (!init_rmode_identity_map(kvm))
2480 return 0;
2481 return 1;
2482}
2483
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002484static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2485{
2486 struct vcpu_vmx *vmx = to_vmx(vcpu);
2487 u64 msr;
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002488 int ret, idx;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002489
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002490 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002491 idx = srcu_read_lock(&vcpu->kvm->srcu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002492 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002493 ret = -ENOMEM;
2494 goto out;
2495 }
2496
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002497 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002498
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002499 vmx->soft_vnmi_blocked = 0;
2500
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002501 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002502 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002503 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002504 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002505 msr |= MSR_IA32_APICBASE_BSP;
2506 kvm_set_apic_base(&vmx->vcpu, msr);
2507
2508 fx_init(&vmx->vcpu);
2509
Avi Kivity5706be02008-08-20 15:07:31 +03002510 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002511 /*
2512 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2513 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2514 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002515 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002516 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2517 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2518 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002519 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2520 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002521 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002522
2523 seg_setup(VCPU_SREG_DS);
2524 seg_setup(VCPU_SREG_ES);
2525 seg_setup(VCPU_SREG_FS);
2526 seg_setup(VCPU_SREG_GS);
2527 seg_setup(VCPU_SREG_SS);
2528
2529 vmcs_write16(GUEST_TR_SELECTOR, 0);
2530 vmcs_writel(GUEST_TR_BASE, 0);
2531 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2532 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2533
2534 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2535 vmcs_writel(GUEST_LDTR_BASE, 0);
2536 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2537 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2538
2539 vmcs_write32(GUEST_SYSENTER_CS, 0);
2540 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2541 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2542
2543 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002544 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002545 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002546 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002547 kvm_rip_write(vcpu, 0);
2548 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002549
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002550 vmcs_writel(GUEST_DR7, 0x400);
2551
2552 vmcs_writel(GUEST_GDTR_BASE, 0);
2553 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2554
2555 vmcs_writel(GUEST_IDTR_BASE, 0);
2556 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2557
2558 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2559 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2560 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2561
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002562 /* Special registers */
2563 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2564
2565 setup_msrs(vmx);
2566
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2568
Sheng Yangf78e0e22007-10-29 09:40:42 +08002569 if (cpu_has_vmx_tpr_shadow()) {
2570 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2571 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2572 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002573 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002574 vmcs_write32(TPR_THRESHOLD, 0);
2575 }
2576
2577 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2578 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002579 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002580
Sheng Yang2384d2b2008-01-17 15:14:33 +08002581 if (vmx->vpid != 0)
2582 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2583
Eduardo Habkostfa400522009-10-24 02:49:58 -02002584 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002585 vmx_set_cr0(&vmx->vcpu, kvm_read_cr0(vcpu)); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002586 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002587 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002588 vmx_fpu_activate(&vmx->vcpu);
2589 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002590
Sheng Yang2384d2b2008-01-17 15:14:33 +08002591 vpid_sync_vcpu_all(vmx);
2592
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002593 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002594
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002595 /* HACK: Don't enable emulation on guest boot/reset */
2596 vmx->emulation_required = 0;
2597
Avi Kivity6aa8b732006-12-10 02:21:36 -08002598out:
Marcelo Tosattif656ce02009-12-23 14:35:25 -02002599 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 return ret;
2601}
2602
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002603static void enable_irq_window(struct kvm_vcpu *vcpu)
2604{
2605 u32 cpu_based_vm_exec_control;
2606
2607 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2608 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2609 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2610}
2611
2612static void enable_nmi_window(struct kvm_vcpu *vcpu)
2613{
2614 u32 cpu_based_vm_exec_control;
2615
2616 if (!cpu_has_virtual_nmis()) {
2617 enable_irq_window(vcpu);
2618 return;
2619 }
2620
2621 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2622 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2623 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2624}
2625
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002626static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002627{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002628 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002629 uint32_t intr;
2630 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002631
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002632 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002633
Avi Kivityfa89a812008-09-01 15:57:51 +03002634 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002635 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002636 vmx->rmode.irq.pending = true;
2637 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002638 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002639 if (vcpu->arch.interrupt.soft)
2640 vmx->rmode.irq.rip +=
2641 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002642 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2643 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2644 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002645 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002646 return;
2647 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002648 intr = irq | INTR_INFO_VALID_MASK;
2649 if (vcpu->arch.interrupt.soft) {
2650 intr |= INTR_TYPE_SOFT_INTR;
2651 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2652 vmx->vcpu.arch.event_exit_inst_len);
2653 } else
2654 intr |= INTR_TYPE_EXT_INTR;
2655 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002656}
2657
Sheng Yangf08864b2008-05-15 18:23:25 +08002658static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2659{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002660 struct vcpu_vmx *vmx = to_vmx(vcpu);
2661
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002662 if (!cpu_has_virtual_nmis()) {
2663 /*
2664 * Tracking the NMI-blocked state in software is built upon
2665 * finding the next open IRQ window. This, in turn, depends on
2666 * well-behaving guests: They have to keep IRQs disabled at
2667 * least as long as the NMI handler runs. Otherwise we may
2668 * cause NMI nesting, maybe breaking the guest. But as this is
2669 * highly unlikely, we can live with the residual risk.
2670 */
2671 vmx->soft_vnmi_blocked = 1;
2672 vmx->vnmi_blocked_time = 0;
2673 }
2674
Jan Kiszka487b3912008-09-26 09:30:56 +02002675 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002676 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002677 vmx->rmode.irq.pending = true;
2678 vmx->rmode.irq.vector = NMI_VECTOR;
2679 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2680 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2681 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2682 INTR_INFO_VALID_MASK);
2683 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2684 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2685 return;
2686 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002687 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2688 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002689}
2690
Gleb Natapovc4282df2009-04-21 17:45:07 +03002691static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002692{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002693 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002694 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002695
Gleb Natapovc4282df2009-04-21 17:45:07 +03002696 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2697 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2698 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002699}
2700
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002701static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
2702{
2703 if (!cpu_has_virtual_nmis())
2704 return to_vmx(vcpu)->soft_vnmi_blocked;
2705 else
2706 return !!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2707 GUEST_INTR_STATE_NMI);
2708}
2709
2710static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2711{
2712 struct vcpu_vmx *vmx = to_vmx(vcpu);
2713
2714 if (!cpu_has_virtual_nmis()) {
2715 if (vmx->soft_vnmi_blocked != masked) {
2716 vmx->soft_vnmi_blocked = masked;
2717 vmx->vnmi_blocked_time = 0;
2718 }
2719 } else {
2720 if (masked)
2721 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2722 GUEST_INTR_STATE_NMI);
2723 else
2724 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2725 GUEST_INTR_STATE_NMI);
2726 }
2727}
2728
Gleb Natapov78646122009-03-23 12:12:11 +02002729static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2730{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002731 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2732 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2733 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002734}
2735
Izik Eiduscbc94022007-10-25 00:29:55 +02002736static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2737{
2738 int ret;
2739 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002740 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002741 .guest_phys_addr = addr,
2742 .memory_size = PAGE_SIZE * 3,
2743 .flags = 0,
2744 };
2745
2746 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2747 if (ret)
2748 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002749 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002750 return 0;
2751}
2752
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2754 int vec, u32 err_code)
2755{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002756 /*
2757 * Instruction with address size override prefix opcode 0x67
2758 * Cause the #SS fault with 0 error code in VM86 mode.
2759 */
2760 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002761 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002763 /*
2764 * Forward all other exceptions that are valid in real mode.
2765 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2766 * the required debugging infrastructure rework.
2767 */
2768 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002769 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002770 if (vcpu->guest_debug &
2771 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2772 return 0;
2773 kvm_queue_exception(vcpu, vec);
2774 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002775 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002776 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2777 return 0;
2778 /* fall through */
2779 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002780 case OF_VECTOR:
2781 case BR_VECTOR:
2782 case UD_VECTOR:
2783 case DF_VECTOR:
2784 case SS_VECTOR:
2785 case GP_VECTOR:
2786 case MF_VECTOR:
2787 kvm_queue_exception(vcpu, vec);
2788 return 1;
2789 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 return 0;
2791}
2792
Andi Kleena0861c02009-06-08 17:37:09 +08002793/*
2794 * Trigger machine check on the host. We assume all the MSRs are already set up
2795 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2796 * We pass a fake environment to the machine check handler because we want
2797 * the guest to be always treated like user space, no matter what context
2798 * it used internally.
2799 */
2800static void kvm_machine_check(void)
2801{
2802#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2803 struct pt_regs regs = {
2804 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2805 .flags = X86_EFLAGS_IF,
2806 };
2807
2808 do_machine_check(&regs, 0);
2809#endif
2810}
2811
Avi Kivity851ba692009-08-24 11:10:17 +03002812static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002813{
2814 /* already handled by vcpu_run */
2815 return 1;
2816}
2817
Avi Kivity851ba692009-08-24 11:10:17 +03002818static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819{
Avi Kivity1155f762007-11-22 11:30:47 +02002820 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002821 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002822 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002823 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002824 u32 vect_info;
2825 enum emulation_result er;
2826
Avi Kivity1155f762007-11-22 11:30:47 +02002827 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2829
Andi Kleena0861c02009-06-08 17:37:09 +08002830 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002831 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002832
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02002834 !is_page_fault(intr_info)) {
2835 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2836 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
2837 vcpu->run->internal.ndata = 2;
2838 vcpu->run->internal.data[0] = vect_info;
2839 vcpu->run->internal.data[1] = intr_info;
2840 return 0;
2841 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002842
Jan Kiszkae4a41882008-09-26 09:30:46 +02002843 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002844 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002845
2846 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002847 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002848 return 1;
2849 }
2850
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002851 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002852 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002853 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002854 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002855 return 1;
2856 }
2857
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002859 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002860 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2862 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002863 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002864 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002865 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002867 trace_kvm_page_fault(cr2, error_code);
2868
Gleb Natapov3298b752009-05-11 13:35:46 +03002869 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002870 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002871 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 }
2873
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002874 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002876 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002877 if (vcpu->arch.halt_request) {
2878 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002879 return kvm_emulate_halt(vcpu);
2880 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002882 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002884 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002885 switch (ex_no) {
2886 case DB_VECTOR:
2887 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2888 if (!(vcpu->guest_debug &
2889 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2890 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2891 kvm_queue_exception(vcpu, DB_VECTOR);
2892 return 1;
2893 }
2894 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2895 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2896 /* fall through */
2897 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002898 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002899 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2900 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002901 break;
2902 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002903 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2904 kvm_run->ex.exception = ex_no;
2905 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002906 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002908 return 0;
2909}
2910
Avi Kivity851ba692009-08-24 11:10:17 +03002911static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002912{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002913 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914 return 1;
2915}
2916
Avi Kivity851ba692009-08-24 11:10:17 +03002917static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002918{
Avi Kivity851ba692009-08-24 11:10:17 +03002919 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002920 return 0;
2921}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002922
Avi Kivity851ba692009-08-24 11:10:17 +03002923static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924{
He, Qingbfdaab02007-09-12 14:18:28 +08002925 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002926 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002927 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928
Avi Kivity1165f5f2007-04-19 17:27:43 +03002929 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002930 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002931 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002932
2933 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002934 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002935 return 0;
2936 return 1;
2937 }
2938
2939 size = (exit_qualification & 7) + 1;
2940 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002941 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002942
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002943 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002944 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002945}
2946
Ingo Molnar102d8322007-02-19 14:37:47 +02002947static void
2948vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2949{
2950 /*
2951 * Patch in the VMCALL instruction:
2952 */
2953 hypercall[0] = 0x0f;
2954 hypercall[1] = 0x01;
2955 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002956}
2957
Avi Kivity851ba692009-08-24 11:10:17 +03002958static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002960 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 int cr;
2962 int reg;
2963
He, Qingbfdaab02007-09-12 14:18:28 +08002964 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 cr = exit_qualification & 15;
2966 reg = (exit_qualification >> 8) & 15;
2967 switch ((exit_qualification >> 4) & 3) {
2968 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002969 val = kvm_register_read(vcpu, reg);
2970 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 switch (cr) {
2972 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002973 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 skip_emulated_instruction(vcpu);
2975 return 1;
2976 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002977 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002978 skip_emulated_instruction(vcpu);
2979 return 1;
2980 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002981 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002982 skip_emulated_instruction(vcpu);
2983 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002984 case 8: {
2985 u8 cr8_prev = kvm_get_cr8(vcpu);
2986 u8 cr8 = kvm_register_read(vcpu, reg);
2987 kvm_set_cr8(vcpu, cr8);
2988 skip_emulated_instruction(vcpu);
2989 if (irqchip_in_kernel(vcpu->kvm))
2990 return 1;
2991 if (cr8_prev <= cr8)
2992 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002993 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002994 return 0;
2995 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996 };
2997 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002998 case 2: /* clts */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002999 vcpu->arch.cr0 &= ~X86_CR0_TS;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003000 vmcs_writel(CR0_READ_SHADOW, kvm_read_cr0(vcpu));
3001 trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
Anthony Liguori25c4c272007-04-27 09:29:21 +03003002 skip_emulated_instruction(vcpu);
3003 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003004 case 1: /*mov from cr*/
3005 switch (cr) {
3006 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003007 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003008 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009 skip_emulated_instruction(vcpu);
3010 return 1;
3011 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003012 val = kvm_get_cr8(vcpu);
3013 kvm_register_write(vcpu, reg, val);
3014 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003015 skip_emulated_instruction(vcpu);
3016 return 1;
3017 }
3018 break;
3019 case 3: /* lmsw */
Avi Kivitya1f83a72009-12-29 17:33:58 +02003020 val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
Avi Kivity4d4ec082009-12-29 18:07:30 +02003021 trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
Avi Kivitya1f83a72009-12-29 17:33:58 +02003022 kvm_lmsw(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023
3024 skip_emulated_instruction(vcpu);
3025 return 1;
3026 default:
3027 break;
3028 }
Avi Kivity851ba692009-08-24 11:10:17 +03003029 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003030 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 (int)(exit_qualification >> 4) & 3, cr);
3032 return 0;
3033}
3034
Avi Kivity851ba692009-08-24 11:10:17 +03003035static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003036{
He, Qingbfdaab02007-09-12 14:18:28 +08003037 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003038 unsigned long val;
3039 int dr, reg;
3040
Avi Kivity0a79b002009-09-01 12:03:25 +03003041 if (!kvm_require_cpl(vcpu, 0))
3042 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003043 dr = vmcs_readl(GUEST_DR7);
3044 if (dr & DR7_GD) {
3045 /*
3046 * As the vm-exit takes precedence over the debug trap, we
3047 * need to emulate the latter, either for the host or the
3048 * guest debugging itself.
3049 */
3050 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003051 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3052 vcpu->run->debug.arch.dr7 = dr;
3053 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003054 vmcs_readl(GUEST_CS_BASE) +
3055 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003056 vcpu->run->debug.arch.exception = DB_VECTOR;
3057 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003058 return 0;
3059 } else {
3060 vcpu->arch.dr7 &= ~DR7_GD;
3061 vcpu->arch.dr6 |= DR6_BD;
3062 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3063 kvm_queue_exception(vcpu, DB_VECTOR);
3064 return 1;
3065 }
3066 }
3067
He, Qingbfdaab02007-09-12 14:18:28 +08003068 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003069 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3070 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3071 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003073 case 0 ... 3:
3074 val = vcpu->arch.db[dr];
3075 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003076 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003077 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003078 break;
3079 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003080 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 break;
3082 default:
3083 val = 0;
3084 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003085 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003087 val = vcpu->arch.regs[reg];
3088 switch (dr) {
3089 case 0 ... 3:
3090 vcpu->arch.db[dr] = val;
3091 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
3092 vcpu->arch.eff_db[dr] = val;
3093 break;
3094 case 4 ... 5:
Avi Kivityfc78f512009-12-07 12:16:48 +02003095 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003096 kvm_queue_exception(vcpu, UD_VECTOR);
3097 break;
3098 case 6:
3099 if (val & 0xffffffff00000000ULL) {
3100 kvm_queue_exception(vcpu, GP_VECTOR);
3101 break;
3102 }
3103 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3104 break;
3105 case 7:
3106 if (val & 0xffffffff00000000ULL) {
3107 kvm_queue_exception(vcpu, GP_VECTOR);
3108 break;
3109 }
3110 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3111 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3112 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3113 vcpu->arch.switch_db_regs =
3114 (val & DR7_BP_EN_MASK);
3115 }
3116 break;
3117 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003119 skip_emulated_instruction(vcpu);
3120 return 1;
3121}
3122
Avi Kivity851ba692009-08-24 11:10:17 +03003123static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124{
Avi Kivity06465c52007-02-28 20:46:53 +02003125 kvm_emulate_cpuid(vcpu);
3126 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003127}
3128
Avi Kivity851ba692009-08-24 11:10:17 +03003129static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003130{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003131 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 u64 data;
3133
3134 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003135 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 return 1;
3137 }
3138
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003139 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003140
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003142 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3143 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144 skip_emulated_instruction(vcpu);
3145 return 1;
3146}
3147
Avi Kivity851ba692009-08-24 11:10:17 +03003148static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003149{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003150 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3151 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3152 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003153
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003154 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003155
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003157 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003158 return 1;
3159 }
3160
3161 skip_emulated_instruction(vcpu);
3162 return 1;
3163}
3164
Avi Kivity851ba692009-08-24 11:10:17 +03003165static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003166{
3167 return 1;
3168}
3169
Avi Kivity851ba692009-08-24 11:10:17 +03003170static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003171{
Eddie Dong85f455f2007-07-06 12:20:49 +03003172 u32 cpu_based_vm_exec_control;
3173
3174 /* clear pending irq */
3175 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3176 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3177 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003178
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003179 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003180
Dor Laorc1150d82007-01-05 16:36:24 -08003181 /*
3182 * If the user space waits to inject interrupts, exit as soon as
3183 * possible
3184 */
Gleb Natapov80618232009-04-21 17:44:56 +03003185 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003186 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003187 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003188 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003189 return 0;
3190 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 return 1;
3192}
3193
Avi Kivity851ba692009-08-24 11:10:17 +03003194static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195{
3196 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003197 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003198}
3199
Avi Kivity851ba692009-08-24 11:10:17 +03003200static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003201{
Dor Laor510043d2007-02-19 18:25:43 +02003202 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003203 kvm_emulate_hypercall(vcpu);
3204 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003205}
3206
Avi Kivity851ba692009-08-24 11:10:17 +03003207static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003208{
3209 kvm_queue_exception(vcpu, UD_VECTOR);
3210 return 1;
3211}
3212
Avi Kivity851ba692009-08-24 11:10:17 +03003213static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003214{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003215 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003216
3217 kvm_mmu_invlpg(vcpu, exit_qualification);
3218 skip_emulated_instruction(vcpu);
3219 return 1;
3220}
3221
Avi Kivity851ba692009-08-24 11:10:17 +03003222static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003223{
3224 skip_emulated_instruction(vcpu);
3225 /* TODO: Add support for VT-d/pass-through device */
3226 return 1;
3227}
3228
Avi Kivity851ba692009-08-24 11:10:17 +03003229static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003230{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003231 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003232 enum emulation_result er;
3233 unsigned long offset;
3234
Sheng Yangf9c617f2009-03-25 10:08:52 +08003235 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003236 offset = exit_qualification & 0xffful;
3237
Avi Kivity851ba692009-08-24 11:10:17 +03003238 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003239
3240 if (er != EMULATE_DONE) {
3241 printk(KERN_ERR
3242 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3243 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003244 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003245 }
3246 return 1;
3247}
3248
Avi Kivity851ba692009-08-24 11:10:17 +03003249static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003250{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003251 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003252 unsigned long exit_qualification;
3253 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003254 int reason, type, idt_v;
3255
3256 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3257 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003258
3259 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3260
3261 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003262 if (reason == TASK_SWITCH_GATE && idt_v) {
3263 switch (type) {
3264 case INTR_TYPE_NMI_INTR:
3265 vcpu->arch.nmi_injected = false;
3266 if (cpu_has_virtual_nmis())
3267 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3268 GUEST_INTR_STATE_NMI);
3269 break;
3270 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003271 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003272 kvm_clear_interrupt_queue(vcpu);
3273 break;
3274 case INTR_TYPE_HARD_EXCEPTION:
3275 case INTR_TYPE_SOFT_EXCEPTION:
3276 kvm_clear_exception_queue(vcpu);
3277 break;
3278 default:
3279 break;
3280 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003281 }
Izik Eidus37817f22008-03-24 23:14:53 +02003282 tss_selector = exit_qualification;
3283
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003284 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3285 type != INTR_TYPE_EXT_INTR &&
3286 type != INTR_TYPE_NMI_INTR))
3287 skip_emulated_instruction(vcpu);
3288
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003289 if (!kvm_task_switch(vcpu, tss_selector, reason))
3290 return 0;
3291
3292 /* clear all local breakpoint enable flags */
3293 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3294
3295 /*
3296 * TODO: What about debug traps on tss switch?
3297 * Are we supposed to inject them and update dr6?
3298 */
3299
3300 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003301}
3302
Avi Kivity851ba692009-08-24 11:10:17 +03003303static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003304{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003305 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003306 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003307 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003308
Sheng Yangf9c617f2009-03-25 10:08:52 +08003309 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003310
3311 if (exit_qualification & (1 << 6)) {
3312 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003313 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003314 }
3315
3316 gla_validity = (exit_qualification >> 7) & 0x3;
3317 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3318 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3319 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3320 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003321 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003322 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3323 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003324 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3325 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003326 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003327 }
3328
3329 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003330 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003331 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003332}
3333
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003334static u64 ept_rsvd_mask(u64 spte, int level)
3335{
3336 int i;
3337 u64 mask = 0;
3338
3339 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3340 mask |= (1ULL << i);
3341
3342 if (level > 2)
3343 /* bits 7:3 reserved */
3344 mask |= 0xf8;
3345 else if (level == 2) {
3346 if (spte & (1ULL << 7))
3347 /* 2MB ref, bits 20:12 reserved */
3348 mask |= 0x1ff000;
3349 else
3350 /* bits 6:3 reserved */
3351 mask |= 0x78;
3352 }
3353
3354 return mask;
3355}
3356
3357static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3358 int level)
3359{
3360 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3361
3362 /* 010b (write-only) */
3363 WARN_ON((spte & 0x7) == 0x2);
3364
3365 /* 110b (write/execute) */
3366 WARN_ON((spte & 0x7) == 0x6);
3367
3368 /* 100b (execute-only) and value not supported by logical processor */
3369 if (!cpu_has_vmx_ept_execute_only())
3370 WARN_ON((spte & 0x7) == 0x4);
3371
3372 /* not 000b */
3373 if ((spte & 0x7)) {
3374 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3375
3376 if (rsvd_bits != 0) {
3377 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3378 __func__, rsvd_bits);
3379 WARN_ON(1);
3380 }
3381
3382 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3383 u64 ept_mem_type = (spte & 0x38) >> 3;
3384
3385 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3386 ept_mem_type == 7) {
3387 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3388 __func__, ept_mem_type);
3389 WARN_ON(1);
3390 }
3391 }
3392 }
3393}
3394
Avi Kivity851ba692009-08-24 11:10:17 +03003395static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003396{
3397 u64 sptes[4];
3398 int nr_sptes, i;
3399 gpa_t gpa;
3400
3401 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3402
3403 printk(KERN_ERR "EPT: Misconfiguration.\n");
3404 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3405
3406 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3407
3408 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3409 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3410
Avi Kivity851ba692009-08-24 11:10:17 +03003411 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3412 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003413
3414 return 0;
3415}
3416
Avi Kivity851ba692009-08-24 11:10:17 +03003417static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003418{
3419 u32 cpu_based_vm_exec_control;
3420
3421 /* clear pending NMI */
3422 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3423 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3424 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3425 ++vcpu->stat.nmi_window_exits;
3426
3427 return 1;
3428}
3429
Mohammed Gamal80ced182009-09-01 12:48:18 +02003430static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003431{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003432 struct vcpu_vmx *vmx = to_vmx(vcpu);
3433 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003434 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003435
3436 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003437 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003438
Mohammed Gamal80ced182009-09-01 12:48:18 +02003439 if (err == EMULATE_DO_MMIO) {
3440 ret = 0;
3441 goto out;
3442 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003443
3444 if (err != EMULATE_DONE) {
3445 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003446 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3447 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02003448 vcpu->run->internal.ndata = 0;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003449 ret = 0;
3450 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003451 }
3452
3453 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003454 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003455 if (need_resched())
3456 schedule();
3457 }
3458
Mohammed Gamal80ced182009-09-01 12:48:18 +02003459 vmx->emulation_required = 0;
3460out:
3461 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003462}
3463
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003465 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3466 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3467 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003468static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003469{
3470 skip_emulated_instruction(vcpu);
3471 kvm_vcpu_on_spin(vcpu);
3472
3473 return 1;
3474}
3475
Sheng Yang59708672009-12-15 13:29:54 +08003476static int handle_invalid_op(struct kvm_vcpu *vcpu)
3477{
3478 kvm_queue_exception(vcpu, UD_VECTOR);
3479 return 1;
3480}
3481
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003482/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003483 * The exit handlers return 1 if the exit was handled fully and guest execution
3484 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3485 * to be done to userspace and return 0.
3486 */
Avi Kivity851ba692009-08-24 11:10:17 +03003487static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003488 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3489 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003490 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003491 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003492 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003493 [EXIT_REASON_CR_ACCESS] = handle_cr,
3494 [EXIT_REASON_DR_ACCESS] = handle_dr,
3495 [EXIT_REASON_CPUID] = handle_cpuid,
3496 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3497 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3498 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3499 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003500 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003501 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003502 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3503 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3504 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3505 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3506 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3507 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3508 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3509 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3510 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003511 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3512 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003513 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003514 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003515 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003516 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3517 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003518 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003519 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3520 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003521};
3522
3523static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003524 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525
3526/*
3527 * The guest has exited. See if we can fix it or if we need userspace
3528 * assistance.
3529 */
Avi Kivity851ba692009-08-24 11:10:17 +03003530static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003532 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003533 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003534 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003535
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003536 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003537
Mohammed Gamal80ced182009-09-01 12:48:18 +02003538 /* If guest state is invalid, start emulating */
3539 if (vmx->emulation_required && emulate_invalid_guest_state)
3540 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003541
Sheng Yang14394422008-04-28 12:24:45 +08003542 /* Access CR3 don't cause VMExit in paging mode, so we need
3543 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003544 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003545 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003546
Avi Kivity29bd8a72007-09-10 17:27:03 +03003547 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003548 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3549 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003550 = vmcs_read32(VM_INSTRUCTION_ERROR);
3551 return 0;
3552 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003553
Mike Dayd77c26f2007-10-08 09:02:08 -04003554 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003555 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003556 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3557 exit_reason != EXIT_REASON_TASK_SWITCH))
3558 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3559 "(0x%x) and exit reason is 0x%x\n",
3560 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003561
3562 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003563 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003564 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003565 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003566 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003567 /*
3568 * This CPU don't support us in finding the end of an
3569 * NMI-blocked window if the guest runs with IRQs
3570 * disabled. So we pull the trigger after 1 s of
3571 * futile waiting, but inform the user about this.
3572 */
3573 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3574 "state on VCPU %d after 1 s timeout\n",
3575 __func__, vcpu->vcpu_id);
3576 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003577 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003578 }
3579
Avi Kivity6aa8b732006-12-10 02:21:36 -08003580 if (exit_reason < kvm_vmx_max_exit_handlers
3581 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003582 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003583 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003584 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3585 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003586 }
3587 return 0;
3588}
3589
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003590static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003591{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003592 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003593 vmcs_write32(TPR_THRESHOLD, 0);
3594 return;
3595 }
3596
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003597 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003598}
3599
Avi Kivitycf393f72008-07-01 16:20:21 +03003600static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3601{
3602 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003603 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003604 bool unblock_nmi;
3605 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003606 int type;
3607 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003608
3609 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003610
Andi Kleena0861c02009-06-08 17:37:09 +08003611 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3612
3613 /* Handle machine checks before interrupts are enabled */
3614 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3615 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3616 && is_machine_check(exit_intr_info)))
3617 kvm_machine_check();
3618
Gleb Natapov20f65982009-05-11 13:35:55 +03003619 /* We need to handle NMIs before interrupts are enabled */
3620 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003621 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003622 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003623
3624 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3625
Avi Kivitycf393f72008-07-01 16:20:21 +03003626 if (cpu_has_virtual_nmis()) {
3627 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3628 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3629 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003630 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003631 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3632 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003633 * SDM 3: 23.2.2 (September 2008)
3634 * Bit 12 is undefined in any of the following cases:
3635 * If the VM exit sets the valid bit in the IDT-vectoring
3636 * information field.
3637 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003638 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003639 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3640 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003641 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3642 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003643 } else if (unlikely(vmx->soft_vnmi_blocked))
3644 vmx->vnmi_blocked_time +=
3645 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003646
Gleb Natapov37b96e92009-03-30 16:03:13 +03003647 vmx->vcpu.arch.nmi_injected = false;
3648 kvm_clear_exception_queue(&vmx->vcpu);
3649 kvm_clear_interrupt_queue(&vmx->vcpu);
3650
3651 if (!idtv_info_valid)
3652 return;
3653
Avi Kivity668f6122008-07-02 09:28:55 +03003654 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3655 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003656
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003657 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003658 case INTR_TYPE_NMI_INTR:
3659 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003660 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003661 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003662 * Clear bit "block by NMI" before VM entry if a NMI
3663 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003664 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003665 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3666 GUEST_INTR_STATE_NMI);
3667 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003668 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003669 vmx->vcpu.arch.event_exit_inst_len =
3670 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3671 /* fall through */
3672 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003673 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003674 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3675 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003676 } else
3677 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003678 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003679 case INTR_TYPE_SOFT_INTR:
3680 vmx->vcpu.arch.event_exit_inst_len =
3681 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3682 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003683 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003684 kvm_queue_interrupt(&vmx->vcpu, vector,
3685 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003686 break;
3687 default:
3688 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003689 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003690}
3691
Avi Kivity9c8cba32007-11-22 11:42:59 +02003692/*
3693 * Failure to inject an interrupt should give us the information
3694 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3695 * when fetching the interrupt redirection bitmap in the real-mode
3696 * tss, this doesn't happen. So we do it ourselves.
3697 */
3698static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3699{
3700 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003701 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003702 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003703 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003704 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3705 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3706 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3707 return;
3708 }
3709 vmx->idt_vectoring_info =
3710 VECTORING_INFO_VALID_MASK
3711 | INTR_TYPE_EXT_INTR
3712 | vmx->rmode.irq.vector;
3713}
3714
Avi Kivityc8019492008-07-14 14:44:59 +03003715#ifdef CONFIG_X86_64
3716#define R "r"
3717#define Q "q"
3718#else
3719#define R "e"
3720#define Q "l"
3721#endif
3722
Avi Kivity851ba692009-08-24 11:10:17 +03003723static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003724{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003725 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003726
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003727 /* Record the guest's net vcpu time for enforced NMI injections. */
3728 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3729 vmx->entry_time = ktime_get();
3730
Mohammed Gamal80ced182009-09-01 12:48:18 +02003731 /* Don't enter VMX if guest state is invalid, let the exit handler
3732 start emulation until we arrive back to a valid state */
3733 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003734 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003735
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003736 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3737 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3738 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3739 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3740
Gleb Natapov787ff732009-05-18 11:44:06 +03003741 /* When single-stepping over STI and MOV SS, we must clear the
3742 * corresponding interruptibility bits in the guest state. Otherwise
3743 * vmentry fails as it then expects bit 14 (BS) in pending debug
3744 * exceptions being set, but that's not correct for the guest debugging
3745 * case. */
3746 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3747 vmx_set_interrupt_shadow(vcpu, 0);
3748
Avi Kivitye6adf282007-04-30 16:07:54 +03003749 /*
3750 * Loading guest fpu may have cleared host cr0.ts
3751 */
3752 vmcs_writel(HOST_CR0, read_cr0());
3753
Avi Kivitye8a48342009-09-01 16:06:25 +03003754 if (vcpu->arch.switch_db_regs)
3755 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003756
Mike Dayd77c26f2007-10-08 09:02:08 -04003757 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003758 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003759 "push %%"R"dx; push %%"R"bp;"
3760 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003761 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3762 "je 1f \n\t"
3763 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003764 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003765 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003766 /* Reload cr2 if changed */
3767 "mov %c[cr2](%0), %%"R"ax \n\t"
3768 "mov %%cr2, %%"R"dx \n\t"
3769 "cmp %%"R"ax, %%"R"dx \n\t"
3770 "je 2f \n\t"
3771 "mov %%"R"ax, %%cr2 \n\t"
3772 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003773 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003774 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003775 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003776 "mov %c[rax](%0), %%"R"ax \n\t"
3777 "mov %c[rbx](%0), %%"R"bx \n\t"
3778 "mov %c[rdx](%0), %%"R"dx \n\t"
3779 "mov %c[rsi](%0), %%"R"si \n\t"
3780 "mov %c[rdi](%0), %%"R"di \n\t"
3781 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003782#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003783 "mov %c[r8](%0), %%r8 \n\t"
3784 "mov %c[r9](%0), %%r9 \n\t"
3785 "mov %c[r10](%0), %%r10 \n\t"
3786 "mov %c[r11](%0), %%r11 \n\t"
3787 "mov %c[r12](%0), %%r12 \n\t"
3788 "mov %c[r13](%0), %%r13 \n\t"
3789 "mov %c[r14](%0), %%r14 \n\t"
3790 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003792 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3793
Avi Kivity6aa8b732006-12-10 02:21:36 -08003794 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003795 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003796 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003797 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003798 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003799 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003800 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003801 "xchg %0, (%%"R"sp) \n\t"
3802 "mov %%"R"ax, %c[rax](%0) \n\t"
3803 "mov %%"R"bx, %c[rbx](%0) \n\t"
3804 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3805 "mov %%"R"dx, %c[rdx](%0) \n\t"
3806 "mov %%"R"si, %c[rsi](%0) \n\t"
3807 "mov %%"R"di, %c[rdi](%0) \n\t"
3808 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003809#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003810 "mov %%r8, %c[r8](%0) \n\t"
3811 "mov %%r9, %c[r9](%0) \n\t"
3812 "mov %%r10, %c[r10](%0) \n\t"
3813 "mov %%r11, %c[r11](%0) \n\t"
3814 "mov %%r12, %c[r12](%0) \n\t"
3815 "mov %%r13, %c[r13](%0) \n\t"
3816 "mov %%r14, %c[r14](%0) \n\t"
3817 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003819 "mov %%cr2, %%"R"ax \n\t"
3820 "mov %%"R"ax, %c[cr2](%0) \n\t"
3821
3822 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003823 "setbe %c[fail](%0) \n\t"
3824 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3825 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3826 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003827 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003828 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3829 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3830 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3831 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3832 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3833 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3834 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003835#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003836 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3837 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3838 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3839 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3840 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3841 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3842 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3843 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003845 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003846 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003847 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003848#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003849 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3850#endif
3851 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003852
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003853 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3854 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003855 vcpu->arch.regs_dirty = 0;
3856
Avi Kivitye8a48342009-09-01 16:06:25 +03003857 if (vcpu->arch.switch_db_regs)
3858 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003859
Avi Kivity1155f762007-11-22 11:30:47 +02003860 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003861 if (vmx->rmode.irq.pending)
3862 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003863
Mike Dayd77c26f2007-10-08 09:02:08 -04003864 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003865 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003866
Avi Kivitycf393f72008-07-01 16:20:21 +03003867 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003868}
3869
Avi Kivityc8019492008-07-14 14:44:59 +03003870#undef R
3871#undef Q
3872
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3874{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003875 struct vcpu_vmx *vmx = to_vmx(vcpu);
3876
3877 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003878 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003879 free_vmcs(vmx->vmcs);
3880 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003881 }
3882}
3883
3884static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3885{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003886 struct vcpu_vmx *vmx = to_vmx(vcpu);
3887
Sheng Yang2384d2b2008-01-17 15:14:33 +08003888 spin_lock(&vmx_vpid_lock);
3889 if (vmx->vpid != 0)
3890 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3891 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003892 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003893 kfree(vmx->guest_msrs);
3894 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003895 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003896}
3897
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003898static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003899{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003900 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003901 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003902 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003903
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003904 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003905 return ERR_PTR(-ENOMEM);
3906
Sheng Yang2384d2b2008-01-17 15:14:33 +08003907 allocate_vpid(vmx);
3908
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003909 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3910 if (err)
3911 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003912
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003913 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003914 if (!vmx->guest_msrs) {
3915 err = -ENOMEM;
3916 goto uninit_vcpu;
3917 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003918
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003919 vmx->vmcs = alloc_vmcs();
3920 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003921 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003922
3923 vmcs_clear(vmx->vmcs);
3924
Avi Kivity15ad7142007-07-11 18:17:21 +03003925 cpu = get_cpu();
3926 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003927 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003928 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003929 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003930 if (err)
3931 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003932 if (vm_need_virtualize_apic_accesses(kvm))
3933 if (alloc_apic_access_page(kvm) != 0)
3934 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003935
Sheng Yangb927a3c2009-07-21 10:42:48 +08003936 if (enable_ept) {
3937 if (!kvm->arch.ept_identity_map_addr)
3938 kvm->arch.ept_identity_map_addr =
3939 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003940 if (alloc_identity_pagetable(kvm) != 0)
3941 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003942 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003943
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003944 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003945
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003946free_vmcs:
3947 free_vmcs(vmx->vmcs);
3948free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003949 kfree(vmx->guest_msrs);
3950uninit_vcpu:
3951 kvm_vcpu_uninit(&vmx->vcpu);
3952free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003953 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003954 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955}
3956
Yang, Sheng002c7f72007-07-31 14:23:01 +03003957static void __init vmx_check_processor_compat(void *rtn)
3958{
3959 struct vmcs_config vmcs_conf;
3960
3961 *(int *)rtn = 0;
3962 if (setup_vmcs_config(&vmcs_conf) < 0)
3963 *(int *)rtn = -EIO;
3964 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3965 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3966 smp_processor_id());
3967 *(int *)rtn = -EIO;
3968 }
3969}
3970
Sheng Yang67253af2008-04-25 10:20:22 +08003971static int get_ept_level(void)
3972{
3973 return VMX_EPT_DEFAULT_GAW + 1;
3974}
3975
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003976static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003977{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003978 u64 ret;
3979
Sheng Yang522c68c2009-04-27 20:35:43 +08003980 /* For VT-d and EPT combination
3981 * 1. MMIO: always map as UC
3982 * 2. EPT with VT-d:
3983 * a. VT-d without snooping control feature: can't guarantee the
3984 * result, try to trust guest.
3985 * b. VT-d with snooping control feature: snooping control feature of
3986 * VT-d engine can guarantee the cache correctness. Just set it
3987 * to WB to keep consistent with host. So the same as item 3.
3988 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3989 * consistent with host MTRR
3990 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003991 if (is_mmio)
3992 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003993 else if (vcpu->kvm->arch.iommu_domain &&
3994 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3995 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3996 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003997 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003998 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3999 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004000
4001 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08004002}
4003
Avi Kivityf4c9e872009-12-28 16:06:35 +02004004#define _ER(x) { EXIT_REASON_##x, #x }
4005
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004006static const struct trace_print_flags vmx_exit_reasons_str[] = {
Avi Kivityf4c9e872009-12-28 16:06:35 +02004007 _ER(EXCEPTION_NMI),
4008 _ER(EXTERNAL_INTERRUPT),
4009 _ER(TRIPLE_FAULT),
4010 _ER(PENDING_INTERRUPT),
4011 _ER(NMI_WINDOW),
4012 _ER(TASK_SWITCH),
4013 _ER(CPUID),
4014 _ER(HLT),
4015 _ER(INVLPG),
4016 _ER(RDPMC),
4017 _ER(RDTSC),
4018 _ER(VMCALL),
4019 _ER(VMCLEAR),
4020 _ER(VMLAUNCH),
4021 _ER(VMPTRLD),
4022 _ER(VMPTRST),
4023 _ER(VMREAD),
4024 _ER(VMRESUME),
4025 _ER(VMWRITE),
4026 _ER(VMOFF),
4027 _ER(VMON),
4028 _ER(CR_ACCESS),
4029 _ER(DR_ACCESS),
4030 _ER(IO_INSTRUCTION),
4031 _ER(MSR_READ),
4032 _ER(MSR_WRITE),
4033 _ER(MWAIT_INSTRUCTION),
4034 _ER(MONITOR_INSTRUCTION),
4035 _ER(PAUSE_INSTRUCTION),
4036 _ER(MCE_DURING_VMENTRY),
4037 _ER(TPR_BELOW_THRESHOLD),
4038 _ER(APIC_ACCESS),
4039 _ER(EPT_VIOLATION),
4040 _ER(EPT_MISCONFIG),
4041 _ER(WBINVD),
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004042 { -1, NULL }
4043};
4044
Avi Kivityf4c9e872009-12-28 16:06:35 +02004045#undef _ER
4046
Sheng Yang17cc3932010-01-05 19:02:27 +08004047static int vmx_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02004048{
Sheng Yang878403b2010-01-05 19:02:29 +08004049 if (enable_ept && !cpu_has_vmx_ept_1g_page())
4050 return PT_DIRECTORY_LEVEL;
4051 else
4052 /* For shadow and EPT supported 1GB page */
4053 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02004054}
4055
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004056static inline u32 bit(int bitno)
4057{
4058 return 1 << (bitno & 31);
4059}
4060
Sheng Yang0e851882009-12-18 16:48:46 +08004061static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4062{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004063 struct kvm_cpuid_entry2 *best;
4064 struct vcpu_vmx *vmx = to_vmx(vcpu);
4065 u32 exec_control;
4066
4067 vmx->rdtscp_enabled = false;
4068 if (vmx_rdtscp_supported()) {
4069 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4070 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4071 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4072 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4073 vmx->rdtscp_enabled = true;
4074 else {
4075 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4076 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4077 exec_control);
4078 }
4079 }
4080 }
Sheng Yang0e851882009-12-18 16:48:46 +08004081}
4082
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004083static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004084 .cpu_has_kvm_support = cpu_has_kvm_support,
4085 .disabled_by_bios = vmx_disabled_by_bios,
4086 .hardware_setup = hardware_setup,
4087 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004088 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004089 .hardware_enable = hardware_enable,
4090 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004091 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004092
4093 .vcpu_create = vmx_create_vcpu,
4094 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004095 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004096
Avi Kivity04d2cc72007-09-10 18:10:54 +03004097 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004098 .vcpu_load = vmx_vcpu_load,
4099 .vcpu_put = vmx_vcpu_put,
4100
4101 .set_guest_debug = set_guest_debug,
4102 .get_msr = vmx_get_msr,
4103 .set_msr = vmx_set_msr,
4104 .get_segment_base = vmx_get_segment_base,
4105 .get_segment = vmx_get_segment,
4106 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004107 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004108 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02004109 .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004110 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004111 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004112 .set_cr3 = vmx_set_cr3,
4113 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004114 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004115 .get_idt = vmx_get_idt,
4116 .set_idt = vmx_set_idt,
4117 .get_gdt = vmx_get_gdt,
4118 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004119 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004120 .get_rflags = vmx_get_rflags,
4121 .set_rflags = vmx_set_rflags,
Avi Kivity02daab22009-12-30 12:40:26 +02004122 .fpu_deactivate = vmx_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004123
4124 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004125
Avi Kivity6aa8b732006-12-10 02:21:36 -08004126 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004127 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004128 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004129 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4130 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004131 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004132 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004133 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004134 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004135 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004136 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004137 .get_nmi_mask = vmx_get_nmi_mask,
4138 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004139 .enable_nmi_window = enable_nmi_window,
4140 .enable_irq_window = enable_irq_window,
4141 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004142
Izik Eiduscbc94022007-10-25 00:29:55 +02004143 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004144 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004145 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004146
4147 .exit_reasons_str = vmx_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08004148 .get_lpage_level = vmx_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08004149
4150 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004151
4152 .rdtscp_supported = vmx_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004153};
4154
4155static int __init vmx_init(void)
4156{
Avi Kivity26bb0982009-09-07 11:14:12 +03004157 int r, i;
4158
4159 rdmsrl_safe(MSR_EFER, &host_efer);
4160
4161 for (i = 0; i < NR_VMX_MSR; ++i)
4162 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004163
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004164 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004165 if (!vmx_io_bitmap_a)
4166 return -ENOMEM;
4167
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004168 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004169 if (!vmx_io_bitmap_b) {
4170 r = -ENOMEM;
4171 goto out;
4172 }
4173
Avi Kivity58972972009-02-24 22:26:47 +02004174 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4175 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004176 r = -ENOMEM;
4177 goto out1;
4178 }
4179
Avi Kivity58972972009-02-24 22:26:47 +02004180 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4181 if (!vmx_msr_bitmap_longmode) {
4182 r = -ENOMEM;
4183 goto out2;
4184 }
4185
He, Qingfdef3ad2007-04-30 09:45:24 +03004186 /*
4187 * Allow direct access to the PC debug port (it is often used for I/O
4188 * delays, but the vmexits simply slow things down).
4189 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004190 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4191 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004192
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004193 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004194
Avi Kivity58972972009-02-24 22:26:47 +02004195 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4196 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004197
Sheng Yang2384d2b2008-01-17 15:14:33 +08004198 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4199
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004200 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004201 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004202 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004203
Avi Kivity58972972009-02-24 22:26:47 +02004204 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4205 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4206 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4207 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4208 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4209 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004210
Avi Kivity089d0342009-03-23 18:26:32 +02004211 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004212 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004213 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004214 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004215 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004216 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004217 kvm_enable_tdp();
4218 } else
4219 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004220
Avi Kivityc7addb92007-09-16 18:58:32 +02004221 if (bypass_guest_pf)
4222 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4223
He, Qingfdef3ad2007-04-30 09:45:24 +03004224 return 0;
4225
Avi Kivity58972972009-02-24 22:26:47 +02004226out3:
4227 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004228out2:
Avi Kivity58972972009-02-24 22:26:47 +02004229 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004230out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004231 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004232out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004233 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004234 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004235}
4236
4237static void __exit vmx_exit(void)
4238{
Avi Kivity58972972009-02-24 22:26:47 +02004239 free_page((unsigned long)vmx_msr_bitmap_legacy);
4240 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004241 free_page((unsigned long)vmx_io_bitmap_b);
4242 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004243
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004244 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004245}
4246
4247module_init(vmx_init)
4248module_exit(vmx_exit)