blob: 74a66f0c00b4a9c2dc3540e2a8c7093e2ce19697 [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 \
69 (X86_CR0_WP | X86_CR0_NE | X86_CR0_TS | X86_CR0_MP)
70#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 Kivity313dbd492008-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 Yangd56f5462008-04-25 10:13:16 +0800321static inline int cpu_has_vmx_invept_individual_addr(void)
322{
Sheng Yang04547152009-04-01 15:52:31 +0800323 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800324}
325
326static inline int cpu_has_vmx_invept_context(void)
327{
Sheng Yang04547152009-04-01 15:52:31 +0800328 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800329}
330
331static inline int cpu_has_vmx_invept_global(void)
332{
Sheng Yang04547152009-04-01 15:52:31 +0800333 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800334}
335
336static inline int cpu_has_vmx_ept(void)
337{
Sheng Yang04547152009-04-01 15:52:31 +0800338 return vmcs_config.cpu_based_2nd_exec_ctrl &
339 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800340}
341
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700342static inline int cpu_has_vmx_unrestricted_guest(void)
343{
344 return vmcs_config.cpu_based_2nd_exec_ctrl &
345 SECONDARY_EXEC_UNRESTRICTED_GUEST;
346}
347
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800348static inline int cpu_has_vmx_ple(void)
349{
350 return vmcs_config.cpu_based_2nd_exec_ctrl &
351 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
352}
353
Sheng Yangf78e0e22007-10-29 09:40:42 +0800354static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
355{
Sheng Yang04547152009-04-01 15:52:31 +0800356 return flexpriority_enabled &&
357 (cpu_has_vmx_virtualize_apic_accesses()) &&
358 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800359}
360
Sheng Yang2384d2b2008-01-17 15:14:33 +0800361static inline int cpu_has_vmx_vpid(void)
362{
Sheng Yang04547152009-04-01 15:52:31 +0800363 return vmcs_config.cpu_based_2nd_exec_ctrl &
364 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800365}
366
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800367static inline int cpu_has_vmx_rdtscp(void)
368{
369 return vmcs_config.cpu_based_2nd_exec_ctrl &
370 SECONDARY_EXEC_RDTSCP;
371}
372
Sheng Yangf08864b2008-05-15 18:23:25 +0800373static inline int cpu_has_virtual_nmis(void)
374{
375 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
376}
377
Sheng Yang04547152009-04-01 15:52:31 +0800378static inline bool report_flexpriority(void)
379{
380 return flexpriority_enabled;
381}
382
Rusty Russell8b9cf982007-07-30 16:31:43 +1000383static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800384{
385 int i;
386
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400387 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300388 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300389 return i;
390 return -1;
391}
392
Sheng Yang2384d2b2008-01-17 15:14:33 +0800393static inline void __invvpid(int ext, u16 vpid, gva_t gva)
394{
395 struct {
396 u64 vpid : 16;
397 u64 rsvd : 48;
398 u64 gva;
399 } operand = { vpid, 0, gva };
400
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300401 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800402 /* CF==1 or ZF==1 --> rc = -1 */
403 "; ja 1f ; ud2 ; 1:"
404 : : "a"(&operand), "c"(ext) : "cc", "memory");
405}
406
Sheng Yang14394422008-04-28 12:24:45 +0800407static inline void __invept(int ext, u64 eptp, gpa_t gpa)
408{
409 struct {
410 u64 eptp, gpa;
411 } operand = {eptp, gpa};
412
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300413 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800414 /* CF==1 or ZF==1 --> rc = -1 */
415 "; ja 1f ; ud2 ; 1:\n"
416 : : "a" (&operand), "c" (ext) : "cc", "memory");
417}
418
Avi Kivity26bb0982009-09-07 11:14:12 +0300419static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300420{
421 int i;
422
Rusty Russell8b9cf982007-07-30 16:31:43 +1000423 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300424 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400425 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000426 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800427}
428
Avi Kivity6aa8b732006-12-10 02:21:36 -0800429static void vmcs_clear(struct vmcs *vmcs)
430{
431 u64 phys_addr = __pa(vmcs);
432 u8 error;
433
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300434 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
436 : "cc", "memory");
437 if (error)
438 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
439 vmcs, phys_addr);
440}
441
442static void __vcpu_clear(void *arg)
443{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000444 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800445 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446
Rusty Russell8b9cf982007-07-30 16:31:43 +1000447 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400448 vmcs_clear(vmx->vmcs);
449 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800451 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300452 list_del(&vmx->local_vcpus_link);
453 vmx->vcpu.cpu = -1;
454 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455}
456
Rusty Russell8b9cf982007-07-30 16:31:43 +1000457static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800458{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200459 if (vmx->vcpu.cpu == -1)
460 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200461 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800462}
463
Sheng Yang2384d2b2008-01-17 15:14:33 +0800464static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
465{
466 if (vmx->vpid == 0)
467 return;
468
469 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
470}
471
Sheng Yang14394422008-04-28 12:24:45 +0800472static inline void ept_sync_global(void)
473{
474 if (cpu_has_vmx_invept_global())
475 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
476}
477
478static inline void ept_sync_context(u64 eptp)
479{
Avi Kivity089d0342009-03-23 18:26:32 +0200480 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800481 if (cpu_has_vmx_invept_context())
482 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
483 else
484 ept_sync_global();
485 }
486}
487
488static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
489{
Avi Kivity089d0342009-03-23 18:26:32 +0200490 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800491 if (cpu_has_vmx_invept_individual_addr())
492 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
493 eptp, gpa);
494 else
495 ept_sync_context(eptp);
496 }
497}
498
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499static unsigned long vmcs_readl(unsigned long field)
500{
501 unsigned long value;
502
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300503 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 : "=a"(value) : "d"(field) : "cc");
505 return value;
506}
507
508static u16 vmcs_read16(unsigned long field)
509{
510 return vmcs_readl(field);
511}
512
513static u32 vmcs_read32(unsigned long field)
514{
515 return vmcs_readl(field);
516}
517
518static u64 vmcs_read64(unsigned long field)
519{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800520#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521 return vmcs_readl(field);
522#else
523 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
524#endif
525}
526
Avi Kivitye52de1b2007-01-05 16:36:56 -0800527static noinline void vmwrite_error(unsigned long field, unsigned long value)
528{
529 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
530 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
531 dump_stack();
532}
533
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534static void vmcs_writel(unsigned long field, unsigned long value)
535{
536 u8 error;
537
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300538 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400539 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800540 if (unlikely(error))
541 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800542}
543
544static void vmcs_write16(unsigned long field, u16 value)
545{
546 vmcs_writel(field, value);
547}
548
549static void vmcs_write32(unsigned long field, u32 value)
550{
551 vmcs_writel(field, value);
552}
553
554static void vmcs_write64(unsigned long field, u64 value)
555{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800556 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300557#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558 asm volatile ("");
559 vmcs_writel(field+1, value >> 32);
560#endif
561}
562
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300563static void vmcs_clear_bits(unsigned long field, u32 mask)
564{
565 vmcs_writel(field, vmcs_readl(field) & ~mask);
566}
567
568static void vmcs_set_bits(unsigned long field, u32 mask)
569{
570 vmcs_writel(field, vmcs_readl(field) | mask);
571}
572
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300573static void update_exception_bitmap(struct kvm_vcpu *vcpu)
574{
575 u32 eb;
576
Andi Kleena0861c02009-06-08 17:37:09 +0800577 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300578 if (!vcpu->fpu_active)
579 eb |= 1u << NM_VECTOR;
Avi Kivitye8a48342009-09-01 16:06:25 +0300580 /*
581 * Unconditionally intercept #DB so we can maintain dr6 without
582 * reading it every exit.
583 */
584 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100585 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100586 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
587 eb |= 1u << BP_VECTOR;
588 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300589 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300590 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200591 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800592 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300593 vmcs_write32(EXCEPTION_BITMAP, eb);
594}
595
Avi Kivity33ed6322007-05-02 16:54:03 +0300596static void reload_tss(void)
597{
Avi Kivity33ed6322007-05-02 16:54:03 +0300598 /*
599 * VT restores TR but not its size. Useless.
600 */
601 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200602 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300603
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300604 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300605 descs = (void *)gdt.base;
606 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
607 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300608}
609
Avi Kivity92c0d902009-10-29 11:00:16 +0200610static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300611{
Roel Kluin3a34a882009-08-04 02:08:45 -0700612 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300613 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300614
Avi Kivity26bb0982009-09-07 11:14:12 +0300615 guest_efer = vmx->vcpu.arch.shadow_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700616
Avi Kivity51c6cf62007-08-29 03:48:05 +0300617 /*
618 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
619 * outside long mode
620 */
621 ignore_bits = EFER_NX | EFER_SCE;
622#ifdef CONFIG_X86_64
623 ignore_bits |= EFER_LMA | EFER_LME;
624 /* SCE is meaningful only in long mode on Intel */
625 if (guest_efer & EFER_LMA)
626 ignore_bits &= ~(u64)EFER_SCE;
627#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300628 guest_efer &= ~ignore_bits;
629 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300630 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200631 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300632 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300633}
634
Avi Kivity04d2cc72007-09-10 18:10:54 +0300635static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300636{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300637 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300638 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300639
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400640 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300641 return;
642
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400643 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300644 /*
645 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
646 * allow segment selectors with cpl > 0 or ti == 1.
647 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300648 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200649 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300650 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200651 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400652 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200653 vmx->host_state.fs_reload_needed = 0;
654 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300655 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200656 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300657 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300658 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400659 if (!(vmx->host_state.gs_sel & 7))
660 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300661 else {
662 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200663 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300664 }
665
666#ifdef CONFIG_X86_64
667 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
668 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
669#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400670 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
671 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300672#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300673
674#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300675 if (is_long_mode(&vmx->vcpu)) {
676 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
677 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
678 }
Avi Kivity707c0872007-05-02 17:33:43 +0300679#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300680 for (i = 0; i < vmx->save_nmsrs; ++i)
681 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200682 vmx->guest_msrs[i].data,
683 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300684}
685
Avi Kivitya9b21b62008-06-24 11:48:49 +0300686static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300687{
Avi Kivity15ad7142007-07-11 18:17:21 +0300688 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300689
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400690 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300691 return;
692
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200693 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400694 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200695 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300696 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200697 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300698 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300699 /*
700 * If we have to reload gs, we must take care to
701 * preserve our gs base.
702 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300703 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300704 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300705#ifdef CONFIG_X86_64
706 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
707#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300708 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300709 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200710 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300711#ifdef CONFIG_X86_64
712 if (is_long_mode(&vmx->vcpu)) {
713 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
714 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
715 }
716#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300717}
718
Avi Kivitya9b21b62008-06-24 11:48:49 +0300719static void vmx_load_host_state(struct vcpu_vmx *vmx)
720{
721 preempt_disable();
722 __vmx_load_host_state(vmx);
723 preempt_enable();
724}
725
Avi Kivity6aa8b732006-12-10 02:21:36 -0800726/*
727 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
728 * vcpu mutex is already taken.
729 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300730static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800731{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400732 struct vcpu_vmx *vmx = to_vmx(vcpu);
733 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200734 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800735
Eddie Donga3d7f852007-09-03 16:15:12 +0300736 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000737 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300738 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300739 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300740 local_irq_disable();
741 list_add(&vmx->local_vcpus_link,
742 &per_cpu(vcpus_on_cpu, cpu));
743 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300744 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800745
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400746 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747 u8 error;
748
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400749 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300750 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
752 : "cc");
753 if (error)
754 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400755 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 }
757
758 if (vcpu->cpu != cpu) {
759 struct descriptor_table dt;
760 unsigned long sysenter_esp;
761
762 vcpu->cpu = cpu;
763 /*
764 * Linux uses per-cpu TSS and GDT, so set these when switching
765 * processors.
766 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300767 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
768 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
770
771 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
772 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300773
774 /*
775 * Make sure the time stamp counter is monotonous.
776 */
777 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200778 if (tsc_this < vcpu->arch.host_tsc) {
779 delta = vcpu->arch.host_tsc - tsc_this;
780 new_offset = vmcs_read64(TSC_OFFSET) + delta;
781 vmcs_write64(TSC_OFFSET, new_offset);
782 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800784}
785
786static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
787{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300788 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789}
790
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300791static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
792{
793 if (vcpu->fpu_active)
794 return;
795 vcpu->fpu_active = 1;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000796 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800797 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92fa2007-07-17 23:19:08 +1000798 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300799 update_exception_bitmap(vcpu);
800}
801
802static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
803{
804 if (!vcpu->fpu_active)
805 return;
806 vcpu->fpu_active = 0;
Rusty Russell707d92fa2007-07-17 23:19:08 +1000807 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300808 update_exception_bitmap(vcpu);
809}
810
Avi Kivity6aa8b732006-12-10 02:21:36 -0800811static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
812{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300813 unsigned long rflags;
814
815 rflags = vmcs_readl(GUEST_RFLAGS);
816 if (to_vmx(vcpu)->rmode.vm86_active)
817 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
818 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800819}
820
821static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
822{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300823 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100824 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800825 vmcs_writel(GUEST_RFLAGS, rflags);
826}
827
Glauber Costa2809f5d2009-05-12 16:21:05 -0400828static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
829{
830 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
831 int ret = 0;
832
833 if (interruptibility & GUEST_INTR_STATE_STI)
834 ret |= X86_SHADOW_INT_STI;
835 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
836 ret |= X86_SHADOW_INT_MOV_SS;
837
838 return ret & mask;
839}
840
841static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
842{
843 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
844 u32 interruptibility = interruptibility_old;
845
846 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
847
848 if (mask & X86_SHADOW_INT_MOV_SS)
849 interruptibility |= GUEST_INTR_STATE_MOV_SS;
850 if (mask & X86_SHADOW_INT_STI)
851 interruptibility |= GUEST_INTR_STATE_STI;
852
853 if ((interruptibility != interruptibility_old))
854 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
855}
856
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
858{
859 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800860
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300861 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300863 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864
Glauber Costa2809f5d2009-05-12 16:21:05 -0400865 /* skipping an emulated instruction also counts */
866 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800867}
868
Avi Kivity298101d2007-11-25 13:41:11 +0200869static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
870 bool has_error_code, u32 error_code)
871{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200872 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100873 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200874
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100875 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200876 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100877 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
878 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200879
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300880 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200881 vmx->rmode.irq.pending = true;
882 vmx->rmode.irq.vector = nr;
883 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300884 if (kvm_exception_is_soft(nr))
885 vmx->rmode.irq.rip +=
886 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100887 intr_info |= INTR_TYPE_SOFT_INTR;
888 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200889 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
890 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
891 return;
892 }
893
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300894 if (kvm_exception_is_soft(nr)) {
895 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
896 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100897 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
898 } else
899 intr_info |= INTR_TYPE_HARD_EXCEPTION;
900
901 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200902}
903
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800904static bool vmx_rdtscp_supported(void)
905{
906 return cpu_has_vmx_rdtscp();
907}
908
Avi Kivity6aa8b732006-12-10 02:21:36 -0800909/*
Eddie Donga75beee2007-05-17 18:55:15 +0300910 * Swap MSR entry in host/guest MSR entry array.
911 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000912static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300913{
Avi Kivity26bb0982009-09-07 11:14:12 +0300914 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400915
916 tmp = vmx->guest_msrs[to];
917 vmx->guest_msrs[to] = vmx->guest_msrs[from];
918 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300919}
920
921/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300922 * Set up the vmcs to automatically save and restore system
923 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
924 * mode, as fiddling with msrs is very expensive.
925 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000926static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300927{
Avi Kivity26bb0982009-09-07 11:14:12 +0300928 int save_nmsrs, index;
Avi Kivity58972972009-02-24 22:26:47 +0200929 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300930
Avi Kivity33f9c502008-02-27 16:06:57 +0200931 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300932 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300933#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000934 if (is_long_mode(&vmx->vcpu)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000935 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300936 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000937 move_msr_up(vmx, index, save_nmsrs++);
938 index = __find_msr_index(vmx, MSR_LSTAR);
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_CSTAR);
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++);
Sheng Yang4e47c7a2009-12-18 16:48:47 +0800944 index = __find_msr_index(vmx, MSR_TSC_AUX);
945 if (index >= 0 && vmx->rdtscp_enabled)
946 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300947 /*
948 * MSR_K6_STAR is only needed on long mode guests, and only
949 * if efer.sce is enabled.
950 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000951 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800952 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000953 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300954 }
Eddie Donga75beee2007-05-17 18:55:15 +0300955#endif
Avi Kivity92c0d902009-10-29 11:00:16 +0200956 index = __find_msr_index(vmx, MSR_EFER);
957 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +0300958 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300959
Avi Kivity26bb0982009-09-07 11:14:12 +0300960 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200961
962 if (cpu_has_vmx_msr_bitmap()) {
963 if (is_long_mode(&vmx->vcpu))
964 msr_bitmap = vmx_msr_bitmap_longmode;
965 else
966 msr_bitmap = vmx_msr_bitmap_legacy;
967
968 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
969 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300970}
971
972/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800973 * reads and returns guest's timestamp counter "register"
974 * guest_tsc = host_tsc + tsc_offset -- 21.3
975 */
976static u64 guest_read_tsc(void)
977{
978 u64 host_tsc, tsc_offset;
979
980 rdtscll(host_tsc);
981 tsc_offset = vmcs_read64(TSC_OFFSET);
982 return host_tsc + tsc_offset;
983}
984
985/*
986 * writes 'guest_tsc' into guest's timestamp counter "register"
987 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
988 */
Marcelo Tosatti53f658b32008-12-11 20:45:05 +0100989static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800990{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
992}
993
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994/*
995 * Reads an msr value (of 'msr_index') into 'pdata'.
996 * Returns 0 on success, non-0 otherwise.
997 * Assumes vcpu_load() was already called.
998 */
999static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
1000{
1001 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +03001002 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003
1004 if (!pdata) {
1005 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
1006 return -EINVAL;
1007 }
1008
1009 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001010#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001011 case MSR_FS_BASE:
1012 data = vmcs_readl(GUEST_FS_BASE);
1013 break;
1014 case MSR_GS_BASE:
1015 data = vmcs_readl(GUEST_GS_BASE);
1016 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001017 case MSR_KERNEL_GS_BASE:
1018 vmx_load_host_state(to_vmx(vcpu));
1019 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
1020 break;
Avi Kivity26bb0982009-09-07 11:14:12 +03001021#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001022 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -08001023 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301024 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -08001025 data = guest_read_tsc();
1026 break;
1027 case MSR_IA32_SYSENTER_CS:
1028 data = vmcs_read32(GUEST_SYSENTER_CS);
1029 break;
1030 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001031 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 break;
1033 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001034 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035 break;
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001036 case MSR_TSC_AUX:
1037 if (!to_vmx(vcpu)->rdtscp_enabled)
1038 return 1;
1039 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001041 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001042 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001043 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001044 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001045 data = msr->data;
1046 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001047 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001048 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001049 }
1050
1051 *pdata = data;
1052 return 0;
1053}
1054
1055/*
1056 * Writes msr value into into the appropriate "register".
1057 * Returns 0 on success, non-0 otherwise.
1058 * Assumes vcpu_load() was already called.
1059 */
1060static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1061{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001062 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001063 struct shared_msr_entry *msr;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001064 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001065 int ret = 0;
1066
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001068 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001069 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001070 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001071 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001072#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 case MSR_FS_BASE:
1074 vmcs_writel(GUEST_FS_BASE, data);
1075 break;
1076 case MSR_GS_BASE:
1077 vmcs_writel(GUEST_GS_BASE, data);
1078 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001079 case MSR_KERNEL_GS_BASE:
1080 vmx_load_host_state(vmx);
1081 vmx->msr_guest_kernel_gs_base = data;
1082 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083#endif
1084 case MSR_IA32_SYSENTER_CS:
1085 vmcs_write32(GUEST_SYSENTER_CS, data);
1086 break;
1087 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001088 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001089 break;
1090 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001091 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001092 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301093 case MSR_IA32_TSC:
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01001094 rdtscll(host_tsc);
1095 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001096 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001097 case MSR_IA32_CR_PAT:
1098 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1099 vmcs_write64(GUEST_IA32_PAT, data);
1100 vcpu->arch.pat = data;
1101 break;
1102 }
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001103 ret = kvm_set_msr_common(vcpu, msr_index, data);
1104 break;
1105 case MSR_TSC_AUX:
1106 if (!vmx->rdtscp_enabled)
1107 return 1;
1108 /* Check reserved bit, higher 32 bits should be zero */
1109 if ((data >> 32) != 0)
1110 return 1;
1111 /* Otherwise falls through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001113 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001114 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001115 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001116 msr->data = data;
1117 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001118 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001119 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001120 }
1121
Eddie Dong2cc51562007-05-21 07:28:09 +03001122 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123}
1124
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001125static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001126{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001127 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1128 switch (reg) {
1129 case VCPU_REGS_RSP:
1130 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1131 break;
1132 case VCPU_REGS_RIP:
1133 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1134 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001135 case VCPU_EXREG_PDPTR:
1136 if (enable_ept)
1137 ept_save_pdptrs(vcpu);
1138 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001139 default:
1140 break;
1141 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142}
1143
Jan Kiszka355be0b2009-10-03 00:31:21 +02001144static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001145{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001146 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1147 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1148 else
1149 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1150
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001151 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152}
1153
1154static __init int cpu_has_kvm_support(void)
1155{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001156 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157}
1158
1159static __init int vmx_disabled_by_bios(void)
1160{
1161 u64 msr;
1162
1163 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001164 return (msr & (FEATURE_CONTROL_LOCKED |
1165 FEATURE_CONTROL_VMXON_ENABLED))
1166 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001167 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168}
1169
Alexander Graf10474ae2009-09-15 11:37:46 +02001170static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171{
1172 int cpu = raw_smp_processor_id();
1173 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1174 u64 old;
1175
Alexander Graf10474ae2009-09-15 11:37:46 +02001176 if (read_cr4() & X86_CR4_VMXE)
1177 return -EBUSY;
1178
Avi Kivity543e4242008-05-13 16:22:47 +03001179 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001181 if ((old & (FEATURE_CONTROL_LOCKED |
1182 FEATURE_CONTROL_VMXON_ENABLED))
1183 != (FEATURE_CONTROL_LOCKED |
1184 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001186 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001187 FEATURE_CONTROL_LOCKED |
1188 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001189 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001190 asm volatile (ASM_VMX_VMXON_RAX
1191 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001193
1194 ept_sync_global();
1195
1196 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001197}
1198
Avi Kivity543e4242008-05-13 16:22:47 +03001199static void vmclear_local_vcpus(void)
1200{
1201 int cpu = raw_smp_processor_id();
1202 struct vcpu_vmx *vmx, *n;
1203
1204 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1205 local_vcpus_link)
1206 __vcpu_clear(vmx);
1207}
1208
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001209
1210/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1211 * tricks.
1212 */
1213static void kvm_cpu_vmxoff(void)
1214{
1215 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1216 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1217}
1218
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219static void hardware_disable(void *garbage)
1220{
Avi Kivity543e4242008-05-13 16:22:47 +03001221 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001222 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001223}
1224
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001225static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001226 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227{
1228 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001229 u32 ctl = ctl_min | ctl_opt;
1230
1231 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1232
1233 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1234 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1235
1236 /* Ensure minimum (required) set of control bits are supported. */
1237 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001238 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001239
1240 *result = ctl;
1241 return 0;
1242}
1243
Yang, Sheng002c7f72007-07-31 14:23:01 +03001244static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001245{
1246 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001247 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001248 u32 _pin_based_exec_control = 0;
1249 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001250 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001251 u32 _vmexit_control = 0;
1252 u32 _vmentry_control = 0;
1253
1254 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001255 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001256 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1257 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001258 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001259
1260 min = CPU_BASED_HLT_EXITING |
1261#ifdef CONFIG_X86_64
1262 CPU_BASED_CR8_LOAD_EXITING |
1263 CPU_BASED_CR8_STORE_EXITING |
1264#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001265 CPU_BASED_CR3_LOAD_EXITING |
1266 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001267 CPU_BASED_USE_IO_BITMAPS |
1268 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001269 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001270 CPU_BASED_MWAIT_EXITING |
1271 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001272 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001273 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001274 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001275 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001276 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1277 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001278 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001279#ifdef CONFIG_X86_64
1280 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1281 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1282 ~CPU_BASED_CR8_STORE_EXITING;
1283#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001284 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001285 min2 = 0;
1286 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001287 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001288 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001289 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001290 SECONDARY_EXEC_UNRESTRICTED_GUEST |
Sheng Yang4e47c7a2009-12-18 16:48:47 +08001291 SECONDARY_EXEC_PAUSE_LOOP_EXITING |
1292 SECONDARY_EXEC_RDTSCP;
Sheng Yangd56f5462008-04-25 10:13:16 +08001293 if (adjust_vmx_controls(min2, opt2,
1294 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001295 &_cpu_based_2nd_exec_control) < 0)
1296 return -EIO;
1297 }
1298#ifndef CONFIG_X86_64
1299 if (!(_cpu_based_2nd_exec_control &
1300 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1301 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1302#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001303 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001304 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1305 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001306 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1307 CPU_BASED_CR3_STORE_EXITING |
1308 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001309 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1310 vmx_capability.ept, vmx_capability.vpid);
1311 }
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001312
1313 min = 0;
1314#ifdef CONFIG_X86_64
1315 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1316#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001317 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001318 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1319 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001320 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001321
Sheng Yang468d4722008-10-09 16:01:55 +08001322 min = 0;
1323 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001324 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1325 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001326 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001327
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001328 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001329
1330 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1331 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001332 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001333
1334#ifdef CONFIG_X86_64
1335 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1336 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001337 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001338#endif
1339
1340 /* Require Write-Back (WB) memory type for VMCS accesses. */
1341 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001342 return -EIO;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001343
Yang, Sheng002c7f72007-07-31 14:23:01 +03001344 vmcs_conf->size = vmx_msr_high & 0x1fff;
1345 vmcs_conf->order = get_order(vmcs_config.size);
1346 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001347
Yang, Sheng002c7f72007-07-31 14:23:01 +03001348 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1349 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001350 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001351 vmcs_conf->vmexit_ctrl = _vmexit_control;
1352 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001353
1354 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001355}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356
1357static struct vmcs *alloc_vmcs_cpu(int cpu)
1358{
1359 int node = cpu_to_node(cpu);
1360 struct page *pages;
1361 struct vmcs *vmcs;
1362
Mel Gorman6484eb32009-06-16 15:31:54 -07001363 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 if (!pages)
1365 return NULL;
1366 vmcs = page_address(pages);
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001367 memset(vmcs, 0, vmcs_config.size);
1368 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369 return vmcs;
1370}
1371
1372static struct vmcs *alloc_vmcs(void)
1373{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001374 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001375}
1376
1377static void free_vmcs(struct vmcs *vmcs)
1378{
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03001379 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380}
1381
Sam Ravnborg39959582007-06-01 00:47:13 -07001382static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383{
1384 int cpu;
1385
Zachary Amsden3230bb42009-09-29 11:38:37 -10001386 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001388 per_cpu(vmxarea, cpu) = NULL;
1389 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390}
1391
Avi Kivity6aa8b732006-12-10 02:21:36 -08001392static __init int alloc_kvm_area(void)
1393{
1394 int cpu;
1395
Zachary Amsden3230bb42009-09-29 11:38:37 -10001396 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 struct vmcs *vmcs;
1398
1399 vmcs = alloc_vmcs_cpu(cpu);
1400 if (!vmcs) {
1401 free_kvm_area();
1402 return -ENOMEM;
1403 }
1404
1405 per_cpu(vmxarea, cpu) = vmcs;
1406 }
1407 return 0;
1408}
1409
1410static __init int hardware_setup(void)
1411{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001412 if (setup_vmcs_config(&vmcs_config) < 0)
1413 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001414
1415 if (boot_cpu_has(X86_FEATURE_NX))
1416 kvm_enable_efer_bits(EFER_NX);
1417
Sheng Yang93ba03c2009-04-01 15:52:32 +08001418 if (!cpu_has_vmx_vpid())
1419 enable_vpid = 0;
1420
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001421 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001422 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001423 enable_unrestricted_guest = 0;
1424 }
1425
1426 if (!cpu_has_vmx_unrestricted_guest())
1427 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001428
1429 if (!cpu_has_vmx_flexpriority())
1430 flexpriority_enabled = 0;
1431
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001432 if (!cpu_has_vmx_tpr_shadow())
1433 kvm_x86_ops->update_cr8_intercept = NULL;
1434
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001435 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1436 kvm_disable_largepages();
1437
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001438 if (!cpu_has_vmx_ple())
1439 ple_gap = 0;
1440
Avi Kivity6aa8b732006-12-10 02:21:36 -08001441 return alloc_kvm_area();
1442}
1443
1444static __exit void hardware_unsetup(void)
1445{
1446 free_kvm_area();
1447}
1448
Avi Kivity6aa8b732006-12-10 02:21:36 -08001449static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1450{
1451 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1452
Avi Kivity6af11b92007-03-19 13:18:10 +02001453 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001454 vmcs_write16(sf->selector, save->selector);
1455 vmcs_writel(sf->base, save->base);
1456 vmcs_write32(sf->limit, save->limit);
1457 vmcs_write32(sf->ar_bytes, save->ar);
1458 } else {
1459 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1460 << AR_DPL_SHIFT;
1461 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1462 }
1463}
1464
1465static void enter_pmode(struct kvm_vcpu *vcpu)
1466{
1467 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001468 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001469
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001470 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001471 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001472
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001473 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1474 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1475 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476
1477 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001478 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001479 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001480 vmcs_writel(GUEST_RFLAGS, flags);
1481
Rusty Russell66aee912007-07-17 23:34:16 +10001482 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1483 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001484
1485 update_exception_bitmap(vcpu);
1486
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001487 if (emulate_invalid_guest_state)
1488 return;
1489
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001490 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1491 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1492 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1493 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001494
1495 vmcs_write16(GUEST_SS_SELECTOR, 0);
1496 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1497
1498 vmcs_write16(GUEST_CS_SELECTOR,
1499 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1500 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1501}
1502
Mike Dayd77c26f2007-10-08 09:02:08 -04001503static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001504{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001505 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001506 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1507 kvm->memslots[0].npages - 3;
1508 return base_gfn << PAGE_SHIFT;
1509 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001510 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511}
1512
1513static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1514{
1515 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1516
1517 save->selector = vmcs_read16(sf->selector);
1518 save->base = vmcs_readl(sf->base);
1519 save->limit = vmcs_read32(sf->limit);
1520 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001521 vmcs_write16(sf->selector, save->base >> 4);
1522 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001523 vmcs_write32(sf->limit, 0xffff);
1524 vmcs_write32(sf->ar_bytes, 0xf3);
1525}
1526
1527static void enter_rmode(struct kvm_vcpu *vcpu)
1528{
1529 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001530 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001531
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001532 if (enable_unrestricted_guest)
1533 return;
1534
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001535 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001536 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001538 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001539 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1540
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001541 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1543
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001544 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1546
1547 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001548 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001549 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001551 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001552
1553 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001554 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001555 update_exception_bitmap(vcpu);
1556
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001557 if (emulate_invalid_guest_state)
1558 goto continue_rmode;
1559
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1561 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1562 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1563
1564 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001565 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001566 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1567 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1569
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001570 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1571 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1572 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1573 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001574
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001575continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001576 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001577 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578}
1579
Amit Shah401d10d2009-02-20 22:53:37 +05301580static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1581{
1582 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001583 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1584
1585 if (!msr)
1586 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301587
Avi Kivity44ea2b12009-09-06 15:55:37 +03001588 /*
1589 * Force kernel_gs_base reloading before EFER changes, as control
1590 * of this msr depends on is_long_mode().
1591 */
1592 vmx_load_host_state(to_vmx(vcpu));
Amit Shah401d10d2009-02-20 22:53:37 +05301593 vcpu->arch.shadow_efer = efer;
1594 if (!msr)
1595 return;
1596 if (efer & EFER_LMA) {
1597 vmcs_write32(VM_ENTRY_CONTROLS,
1598 vmcs_read32(VM_ENTRY_CONTROLS) |
1599 VM_ENTRY_IA32E_MODE);
1600 msr->data = efer;
1601 } else {
1602 vmcs_write32(VM_ENTRY_CONTROLS,
1603 vmcs_read32(VM_ENTRY_CONTROLS) &
1604 ~VM_ENTRY_IA32E_MODE);
1605
1606 msr->data = efer & ~EFER_LME;
1607 }
1608 setup_msrs(vmx);
1609}
1610
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001611#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001612
1613static void enter_lmode(struct kvm_vcpu *vcpu)
1614{
1615 u32 guest_tr_ar;
1616
1617 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1618 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1619 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001620 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621 vmcs_write32(GUEST_TR_AR_BYTES,
1622 (guest_tr_ar & ~AR_TYPE_MASK)
1623 | AR_TYPE_BUSY_64_TSS);
1624 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001625 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301626 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001627}
1628
1629static void exit_lmode(struct kvm_vcpu *vcpu)
1630{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001631 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001632
1633 vmcs_write32(VM_ENTRY_CONTROLS,
1634 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001635 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636}
1637
1638#endif
1639
Sheng Yang2384d2b2008-01-17 15:14:33 +08001640static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1641{
1642 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001643 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001644 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001645}
1646
Anthony Liguori25c4c272007-04-27 09:29:21 +03001647static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001648{
Avi Kivityfc78f512009-12-07 12:16:48 +02001649 ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
1650
1651 vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
1652 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
Avi Kivity399badf2007-01-05 16:36:38 -08001653}
1654
Sheng Yang14394422008-04-28 12:24:45 +08001655static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1656{
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001657 if (!test_bit(VCPU_EXREG_PDPTR,
1658 (unsigned long *)&vcpu->arch.regs_dirty))
1659 return;
1660
Sheng Yang14394422008-04-28 12:24:45 +08001661 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001662 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1663 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1664 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1665 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1666 }
1667}
1668
Avi Kivity8f5d5492009-05-31 18:41:29 +03001669static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1670{
1671 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1672 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1673 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1674 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1675 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1676 }
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001677
1678 __set_bit(VCPU_EXREG_PDPTR,
1679 (unsigned long *)&vcpu->arch.regs_avail);
1680 __set_bit(VCPU_EXREG_PDPTR,
1681 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001682}
1683
Sheng Yang14394422008-04-28 12:24:45 +08001684static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1685
1686static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1687 unsigned long cr0,
1688 struct kvm_vcpu *vcpu)
1689{
1690 if (!(cr0 & X86_CR0_PG)) {
1691 /* From paging/starting to nonpaging */
1692 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001693 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001694 (CPU_BASED_CR3_LOAD_EXITING |
1695 CPU_BASED_CR3_STORE_EXITING));
1696 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001697 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001698 } else if (!is_paging(vcpu)) {
1699 /* From nonpaging to paging */
1700 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001701 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001702 ~(CPU_BASED_CR3_LOAD_EXITING |
1703 CPU_BASED_CR3_STORE_EXITING));
1704 vcpu->arch.cr0 = cr0;
Avi Kivityfc78f512009-12-07 12:16:48 +02001705 vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
Sheng Yang14394422008-04-28 12:24:45 +08001706 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001707
1708 if (!(cr0 & X86_CR0_WP))
1709 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001710}
1711
Avi Kivity6aa8b732006-12-10 02:21:36 -08001712static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1713{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001714 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001715 unsigned long hw_cr0;
1716
1717 if (enable_unrestricted_guest)
1718 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1719 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1720 else
1721 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001722
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001723 vmx_fpu_deactivate(vcpu);
1724
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001725 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726 enter_pmode(vcpu);
1727
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001728 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001729 enter_rmode(vcpu);
1730
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001731#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001732 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001733 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001734 enter_lmode(vcpu);
Rusty Russell707d92fa2007-07-17 23:19:08 +10001735 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001736 exit_lmode(vcpu);
1737 }
1738#endif
1739
Avi Kivity089d0342009-03-23 18:26:32 +02001740 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001741 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1742
Avi Kivity6aa8b732006-12-10 02:21:36 -08001743 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001744 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001745 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001746
Rusty Russell707d92fa2007-07-17 23:19:08 +10001747 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001748 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001749}
1750
Sheng Yang14394422008-04-28 12:24:45 +08001751static u64 construct_eptp(unsigned long root_hpa)
1752{
1753 u64 eptp;
1754
1755 /* TODO write the value reading from MSR */
1756 eptp = VMX_EPT_DEFAULT_MT |
1757 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1758 eptp |= (root_hpa & PAGE_MASK);
1759
1760 return eptp;
1761}
1762
Avi Kivity6aa8b732006-12-10 02:21:36 -08001763static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1764{
Sheng Yang14394422008-04-28 12:24:45 +08001765 unsigned long guest_cr3;
1766 u64 eptp;
1767
1768 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001769 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001770 eptp = construct_eptp(cr3);
1771 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001772 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001773 vcpu->kvm->arch.ept_identity_map_addr;
Marcelo Tosatti7c93be442009-10-26 16:48:33 -02001774 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001775 }
1776
Sheng Yang2384d2b2008-01-17 15:14:33 +08001777 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001778 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001779 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001780 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781}
1782
1783static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1784{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001785 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001786 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1787
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001788 vcpu->arch.cr4 = cr4;
Avi Kivitybc230082009-12-08 12:14:42 +02001789 if (enable_ept) {
1790 if (!is_paging(vcpu)) {
1791 hw_cr4 &= ~X86_CR4_PAE;
1792 hw_cr4 |= X86_CR4_PSE;
1793 } else if (!(cr4 & X86_CR4_PAE)) {
1794 hw_cr4 &= ~X86_CR4_PAE;
1795 }
1796 }
Sheng Yang14394422008-04-28 12:24:45 +08001797
1798 vmcs_writel(CR4_READ_SHADOW, cr4);
1799 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001800}
1801
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1803{
1804 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1805
1806 return vmcs_readl(sf->base);
1807}
1808
1809static void vmx_get_segment(struct kvm_vcpu *vcpu,
1810 struct kvm_segment *var, int seg)
1811{
1812 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1813 u32 ar;
1814
1815 var->base = vmcs_readl(sf->base);
1816 var->limit = vmcs_read32(sf->limit);
1817 var->selector = vmcs_read16(sf->selector);
1818 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001819 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820 ar = 0;
1821 var->type = ar & 15;
1822 var->s = (ar >> 4) & 1;
1823 var->dpl = (ar >> 5) & 3;
1824 var->present = (ar >> 7) & 1;
1825 var->avl = (ar >> 12) & 1;
1826 var->l = (ar >> 13) & 1;
1827 var->db = (ar >> 14) & 1;
1828 var->g = (ar >> 15) & 1;
1829 var->unusable = (ar >> 16) & 1;
1830}
1831
Izik Eidus2e4d2652008-03-24 19:38:34 +02001832static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1833{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001834 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1835 return 0;
1836
1837 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1838 return 3;
1839
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001840 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001841}
1842
Avi Kivity653e3102007-05-07 10:55:37 +03001843static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001845 u32 ar;
1846
Avi Kivity653e3102007-05-07 10:55:37 +03001847 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001848 ar = 1 << 16;
1849 else {
1850 ar = var->type & 15;
1851 ar |= (var->s & 1) << 4;
1852 ar |= (var->dpl & 3) << 5;
1853 ar |= (var->present & 1) << 7;
1854 ar |= (var->avl & 1) << 12;
1855 ar |= (var->l & 1) << 13;
1856 ar |= (var->db & 1) << 14;
1857 ar |= (var->g & 1) << 15;
1858 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001859 if (ar == 0) /* a 0 value means unusable */
1860 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001861
1862 return ar;
1863}
1864
1865static void vmx_set_segment(struct kvm_vcpu *vcpu,
1866 struct kvm_segment *var, int seg)
1867{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001868 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001869 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1870 u32 ar;
1871
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001872 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1873 vmx->rmode.tr.selector = var->selector;
1874 vmx->rmode.tr.base = var->base;
1875 vmx->rmode.tr.limit = var->limit;
1876 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001877 return;
1878 }
1879 vmcs_writel(sf->base, var->base);
1880 vmcs_write32(sf->limit, var->limit);
1881 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001882 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001883 /*
1884 * Hack real-mode segments into vm86 compatibility.
1885 */
1886 if (var->base == 0xffff0000 && var->selector == 0xf000)
1887 vmcs_writel(sf->base, 0xf0000);
1888 ar = 0xf3;
1889 } else
1890 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001891
1892 /*
1893 * Fix the "Accessed" bit in AR field of segment registers for older
1894 * qemu binaries.
1895 * IA32 arch specifies that at the time of processor reset the
1896 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1897 * is setting it to 0 in the usedland code. This causes invalid guest
1898 * state vmexit when "unrestricted guest" mode is turned on.
1899 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1900 * tree. Newer qemu binaries with that qemu fix would not need this
1901 * kvm hack.
1902 */
1903 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1904 ar |= 0x1; /* Accessed */
1905
Avi Kivity6aa8b732006-12-10 02:21:36 -08001906 vmcs_write32(sf->ar_bytes, ar);
1907}
1908
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1910{
1911 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1912
1913 *db = (ar >> 14) & 1;
1914 *l = (ar >> 13) & 1;
1915}
1916
1917static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1918{
1919 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1920 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1921}
1922
1923static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1924{
1925 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1926 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1927}
1928
1929static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1930{
1931 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1932 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1933}
1934
1935static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1936{
1937 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1938 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1939}
1940
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001941static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1942{
1943 struct kvm_segment var;
1944 u32 ar;
1945
1946 vmx_get_segment(vcpu, &var, seg);
1947 ar = vmx_segment_access_rights(&var);
1948
1949 if (var.base != (var.selector << 4))
1950 return false;
1951 if (var.limit != 0xffff)
1952 return false;
1953 if (ar != 0xf3)
1954 return false;
1955
1956 return true;
1957}
1958
1959static bool code_segment_valid(struct kvm_vcpu *vcpu)
1960{
1961 struct kvm_segment cs;
1962 unsigned int cs_rpl;
1963
1964 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1965 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1966
Avi Kivity1872a3f2009-01-04 23:26:52 +02001967 if (cs.unusable)
1968 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001969 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1970 return false;
1971 if (!cs.s)
1972 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001973 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001974 if (cs.dpl > cs_rpl)
1975 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001976 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001977 if (cs.dpl != cs_rpl)
1978 return false;
1979 }
1980 if (!cs.present)
1981 return false;
1982
1983 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1984 return true;
1985}
1986
1987static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1988{
1989 struct kvm_segment ss;
1990 unsigned int ss_rpl;
1991
1992 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1993 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1994
Avi Kivity1872a3f2009-01-04 23:26:52 +02001995 if (ss.unusable)
1996 return true;
1997 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001998 return false;
1999 if (!ss.s)
2000 return false;
2001 if (ss.dpl != ss_rpl) /* DPL != RPL */
2002 return false;
2003 if (!ss.present)
2004 return false;
2005
2006 return true;
2007}
2008
2009static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
2010{
2011 struct kvm_segment var;
2012 unsigned int rpl;
2013
2014 vmx_get_segment(vcpu, &var, seg);
2015 rpl = var.selector & SELECTOR_RPL_MASK;
2016
Avi Kivity1872a3f2009-01-04 23:26:52 +02002017 if (var.unusable)
2018 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002019 if (!var.s)
2020 return false;
2021 if (!var.present)
2022 return false;
2023 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
2024 if (var.dpl < rpl) /* DPL < RPL */
2025 return false;
2026 }
2027
2028 /* TODO: Add other members to kvm_segment_field to allow checking for other access
2029 * rights flags
2030 */
2031 return true;
2032}
2033
2034static bool tr_valid(struct kvm_vcpu *vcpu)
2035{
2036 struct kvm_segment tr;
2037
2038 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
2039
Avi Kivity1872a3f2009-01-04 23:26:52 +02002040 if (tr.unusable)
2041 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002042 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2043 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002044 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002045 return false;
2046 if (!tr.present)
2047 return false;
2048
2049 return true;
2050}
2051
2052static bool ldtr_valid(struct kvm_vcpu *vcpu)
2053{
2054 struct kvm_segment ldtr;
2055
2056 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2057
Avi Kivity1872a3f2009-01-04 23:26:52 +02002058 if (ldtr.unusable)
2059 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002060 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2061 return false;
2062 if (ldtr.type != 2)
2063 return false;
2064 if (!ldtr.present)
2065 return false;
2066
2067 return true;
2068}
2069
2070static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2071{
2072 struct kvm_segment cs, ss;
2073
2074 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2075 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2076
2077 return ((cs.selector & SELECTOR_RPL_MASK) ==
2078 (ss.selector & SELECTOR_RPL_MASK));
2079}
2080
2081/*
2082 * Check if guest state is valid. Returns true if valid, false if
2083 * not.
2084 * We assume that registers are always usable
2085 */
2086static bool guest_state_valid(struct kvm_vcpu *vcpu)
2087{
2088 /* real mode guest state checks */
2089 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2090 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2091 return false;
2092 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2093 return false;
2094 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2095 return false;
2096 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2097 return false;
2098 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2099 return false;
2100 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2101 return false;
2102 } else {
2103 /* protected mode guest state checks */
2104 if (!cs_ss_rpl_check(vcpu))
2105 return false;
2106 if (!code_segment_valid(vcpu))
2107 return false;
2108 if (!stack_segment_valid(vcpu))
2109 return false;
2110 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2111 return false;
2112 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2113 return false;
2114 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2115 return false;
2116 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2117 return false;
2118 if (!tr_valid(vcpu))
2119 return false;
2120 if (!ldtr_valid(vcpu))
2121 return false;
2122 }
2123 /* TODO:
2124 * - Add checks on RIP
2125 * - Add checks on RFLAGS
2126 */
2127
2128 return true;
2129}
2130
Mike Dayd77c26f2007-10-08 09:02:08 -04002131static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002132{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002133 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002134 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002135 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002136 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002137
Izik Eidus195aefd2007-10-01 22:14:18 +02002138 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2139 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002140 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002141 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002142 r = kvm_write_guest_page(kvm, fn++, &data,
2143 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002144 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002145 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002146 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2147 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002148 goto out;
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 = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002153 r = kvm_write_guest_page(kvm, fn, &data,
2154 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2155 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002156 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002157 goto out;
2158
2159 ret = 1;
2160out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002161 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002162}
2163
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002164static int init_rmode_identity_map(struct kvm *kvm)
2165{
2166 int i, r, ret;
2167 pfn_t identity_map_pfn;
2168 u32 tmp;
2169
Avi Kivity089d0342009-03-23 18:26:32 +02002170 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002171 return 1;
2172 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2173 printk(KERN_ERR "EPT: identity-mapping pagetable "
2174 "haven't been allocated!\n");
2175 return 0;
2176 }
2177 if (likely(kvm->arch.ept_identity_pagetable_done))
2178 return 1;
2179 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002180 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002181 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2182 if (r < 0)
2183 goto out;
2184 /* Set up identity-mapping pagetable for EPT in real mode */
2185 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2186 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2187 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2188 r = kvm_write_guest_page(kvm, identity_map_pfn,
2189 &tmp, i * sizeof(tmp), sizeof(tmp));
2190 if (r < 0)
2191 goto out;
2192 }
2193 kvm->arch.ept_identity_pagetable_done = true;
2194 ret = 1;
2195out:
2196 return ret;
2197}
2198
Avi Kivity6aa8b732006-12-10 02:21:36 -08002199static void seg_setup(int seg)
2200{
2201 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002202 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002203
2204 vmcs_write16(sf->selector, 0);
2205 vmcs_writel(sf->base, 0);
2206 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002207 if (enable_unrestricted_guest) {
2208 ar = 0x93;
2209 if (seg == VCPU_SREG_CS)
2210 ar |= 0x08; /* code segment */
2211 } else
2212 ar = 0xf3;
2213
2214 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002215}
2216
Sheng Yangf78e0e22007-10-29 09:40:42 +08002217static int alloc_apic_access_page(struct kvm *kvm)
2218{
2219 struct kvm_userspace_memory_region kvm_userspace_mem;
2220 int r = 0;
2221
Izik Eidus72dc67a2008-02-10 18:04:15 +02002222 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002223 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002224 goto out;
2225 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2226 kvm_userspace_mem.flags = 0;
2227 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2228 kvm_userspace_mem.memory_size = PAGE_SIZE;
2229 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2230 if (r)
2231 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002232
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002233 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002234out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002235 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002236 return r;
2237}
2238
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002239static int alloc_identity_pagetable(struct kvm *kvm)
2240{
2241 struct kvm_userspace_memory_region kvm_userspace_mem;
2242 int r = 0;
2243
2244 down_write(&kvm->slots_lock);
2245 if (kvm->arch.ept_identity_pagetable)
2246 goto out;
2247 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2248 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002249 kvm_userspace_mem.guest_phys_addr =
2250 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002251 kvm_userspace_mem.memory_size = PAGE_SIZE;
2252 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2253 if (r)
2254 goto out;
2255
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002256 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002257 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002258out:
2259 up_write(&kvm->slots_lock);
2260 return r;
2261}
2262
Sheng Yang2384d2b2008-01-17 15:14:33 +08002263static void allocate_vpid(struct vcpu_vmx *vmx)
2264{
2265 int vpid;
2266
2267 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002268 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002269 return;
2270 spin_lock(&vmx_vpid_lock);
2271 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2272 if (vpid < VMX_NR_VPIDS) {
2273 vmx->vpid = vpid;
2274 __set_bit(vpid, vmx_vpid_bitmap);
2275 }
2276 spin_unlock(&vmx_vpid_lock);
2277}
2278
Avi Kivity58972972009-02-24 22:26:47 +02002279static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002280{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002281 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002282
2283 if (!cpu_has_vmx_msr_bitmap())
2284 return;
2285
2286 /*
2287 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2288 * have the write-low and read-high bitmap offsets the wrong way round.
2289 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2290 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002291 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002292 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2293 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002294 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2295 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002296 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2297 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002298 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002299}
2300
Avi Kivity58972972009-02-24 22:26:47 +02002301static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2302{
2303 if (!longmode_only)
2304 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2305 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2306}
2307
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308/*
2309 * Sets up the vmcs for emulated real mode.
2310 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002311static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002312{
Sheng Yang468d4722008-10-09 16:01:55 +08002313 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002314 u32 junk;
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002315 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002316 unsigned long a;
2317 struct descriptor_table dt;
2318 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002319 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002320 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002321
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002323 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2324 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002325
Sheng Yang25c5f222008-03-28 13:18:56 +08002326 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002327 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002328
Avi Kivity6aa8b732006-12-10 02:21:36 -08002329 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2330
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331 /* Control */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002332 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2333 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002334
2335 exec_control = vmcs_config.cpu_based_exec_ctrl;
2336 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2337 exec_control &= ~CPU_BASED_TPR_SHADOW;
2338#ifdef CONFIG_X86_64
2339 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2340 CPU_BASED_CR8_LOAD_EXITING;
2341#endif
2342 }
Avi Kivity089d0342009-03-23 18:26:32 +02002343 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002344 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002345 CPU_BASED_CR3_LOAD_EXITING |
2346 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002347 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002348
Sheng Yang83ff3b92007-11-21 14:33:25 +08002349 if (cpu_has_secondary_exec_ctrls()) {
2350 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2351 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2352 exec_control &=
2353 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002354 if (vmx->vpid == 0)
2355 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002356 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002357 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002358 enable_unrestricted_guest = 0;
2359 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002360 if (!enable_unrestricted_guest)
2361 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002362 if (!ple_gap)
2363 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002364 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2365 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002366
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002367 if (ple_gap) {
2368 vmcs_write32(PLE_GAP, ple_gap);
2369 vmcs_write32(PLE_WINDOW, ple_window);
2370 }
2371
Avi Kivityc7addb92007-09-16 18:58:32 +02002372 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2373 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002374 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2375
2376 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2377 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2378 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2379
2380 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2381 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2382 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002383 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2384 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002385 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002386#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002387 rdmsrl(MSR_FS_BASE, a);
2388 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2389 rdmsrl(MSR_GS_BASE, a);
2390 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2391#else
2392 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2393 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2394#endif
2395
2396 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2397
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002398 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002399 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2400
Mike Dayd77c26f2007-10-08 09:02:08 -04002401 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002402 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002403 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2404 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2405 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002406
2407 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2408 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2409 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2410 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2411 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2412 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2413
Sheng Yang468d4722008-10-09 16:01:55 +08002414 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2415 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2416 host_pat = msr_low | ((u64) msr_high << 32);
2417 vmcs_write64(HOST_IA32_PAT, host_pat);
2418 }
2419 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2420 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2421 host_pat = msr_low | ((u64) msr_high << 32);
2422 /* Write the default value follow host pat */
2423 vmcs_write64(GUEST_IA32_PAT, host_pat);
2424 /* Keep arch.pat sync with GUEST_IA32_PAT */
2425 vmx->vcpu.arch.pat = host_pat;
2426 }
2427
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428 for (i = 0; i < NR_VMX_MSR; ++i) {
2429 u32 index = vmx_msr_index[i];
2430 u32 data_low, data_high;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002431 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432
2433 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2434 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002435 if (wrmsr_safe(index, data_low, data_high) < 0)
2436 continue;
Avi Kivity26bb0982009-09-07 11:14:12 +03002437 vmx->guest_msrs[j].index = i;
2438 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002439 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002440 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002443 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002444
2445 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14fe2007-07-29 11:07:42 +03002446 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2447
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002448 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
Avi Kivity4c386092009-12-07 12:26:18 +02002449 vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
Avi Kivityce03e4f2009-12-07 12:29:14 +02002450 if (enable_ept)
2451 vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
Avi Kivity4c386092009-12-07 12:26:18 +02002452 vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002453
Marcelo Tosatti53f658b32008-12-11 20:45:05 +01002454 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2455 rdtscll(tsc_this);
2456 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2457 tsc_base = tsc_this;
2458
2459 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002460
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002461 return 0;
2462}
2463
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002464static int init_rmode(struct kvm *kvm)
2465{
2466 if (!init_rmode_tss(kvm))
2467 return 0;
2468 if (!init_rmode_identity_map(kvm))
2469 return 0;
2470 return 1;
2471}
2472
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002473static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2474{
2475 struct vcpu_vmx *vmx = to_vmx(vcpu);
2476 u64 msr;
2477 int ret;
2478
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002479 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002480 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002481 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002482 ret = -ENOMEM;
2483 goto out;
2484 }
2485
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002486 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002487
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002488 vmx->soft_vnmi_blocked = 0;
2489
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002490 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002491 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002492 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002493 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002494 msr |= MSR_IA32_APICBASE_BSP;
2495 kvm_set_apic_base(&vmx->vcpu, msr);
2496
2497 fx_init(&vmx->vcpu);
2498
Avi Kivity5706be02008-08-20 15:07:31 +03002499 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002500 /*
2501 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2502 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2503 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002504 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002505 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2506 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2507 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002508 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2509 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002510 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002511
2512 seg_setup(VCPU_SREG_DS);
2513 seg_setup(VCPU_SREG_ES);
2514 seg_setup(VCPU_SREG_FS);
2515 seg_setup(VCPU_SREG_GS);
2516 seg_setup(VCPU_SREG_SS);
2517
2518 vmcs_write16(GUEST_TR_SELECTOR, 0);
2519 vmcs_writel(GUEST_TR_BASE, 0);
2520 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2521 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2522
2523 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2524 vmcs_writel(GUEST_LDTR_BASE, 0);
2525 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2526 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2527
2528 vmcs_write32(GUEST_SYSENTER_CS, 0);
2529 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2530 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2531
2532 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002533 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002534 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002535 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002536 kvm_rip_write(vcpu, 0);
2537 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002538
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002539 vmcs_writel(GUEST_DR7, 0x400);
2540
2541 vmcs_writel(GUEST_GDTR_BASE, 0);
2542 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2543
2544 vmcs_writel(GUEST_IDTR_BASE, 0);
2545 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2546
2547 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2548 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2549 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2550
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002551 /* Special registers */
2552 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2553
2554 setup_msrs(vmx);
2555
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2557
Sheng Yangf78e0e22007-10-29 09:40:42 +08002558 if (cpu_has_vmx_tpr_shadow()) {
2559 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2560 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2561 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002562 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002563 vmcs_write32(TPR_THRESHOLD, 0);
2564 }
2565
2566 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2567 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002568 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569
Sheng Yang2384d2b2008-01-17 15:14:33 +08002570 if (vmx->vpid != 0)
2571 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2572
Eduardo Habkostfa400522009-10-24 02:49:58 -02002573 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002574 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002575 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002576 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002577 vmx_fpu_activate(&vmx->vcpu);
2578 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002579
Sheng Yang2384d2b2008-01-17 15:14:33 +08002580 vpid_sync_vcpu_all(vmx);
2581
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002582 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002583
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002584 /* HACK: Don't enable emulation on guest boot/reset */
2585 vmx->emulation_required = 0;
2586
Avi Kivity6aa8b732006-12-10 02:21:36 -08002587out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002588 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589 return ret;
2590}
2591
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002592static void enable_irq_window(struct kvm_vcpu *vcpu)
2593{
2594 u32 cpu_based_vm_exec_control;
2595
2596 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2597 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2598 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2599}
2600
2601static void enable_nmi_window(struct kvm_vcpu *vcpu)
2602{
2603 u32 cpu_based_vm_exec_control;
2604
2605 if (!cpu_has_virtual_nmis()) {
2606 enable_irq_window(vcpu);
2607 return;
2608 }
2609
2610 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2611 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2612 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2613}
2614
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002615static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002616{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002617 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002618 uint32_t intr;
2619 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002620
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002621 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002622
Avi Kivityfa89a812008-09-01 15:57:51 +03002623 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002624 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002625 vmx->rmode.irq.pending = true;
2626 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002627 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002628 if (vcpu->arch.interrupt.soft)
2629 vmx->rmode.irq.rip +=
2630 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002631 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2632 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2633 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002634 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002635 return;
2636 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002637 intr = irq | INTR_INFO_VALID_MASK;
2638 if (vcpu->arch.interrupt.soft) {
2639 intr |= INTR_TYPE_SOFT_INTR;
2640 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2641 vmx->vcpu.arch.event_exit_inst_len);
2642 } else
2643 intr |= INTR_TYPE_EXT_INTR;
2644 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002645}
2646
Sheng Yangf08864b2008-05-15 18:23:25 +08002647static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2648{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002649 struct vcpu_vmx *vmx = to_vmx(vcpu);
2650
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002651 if (!cpu_has_virtual_nmis()) {
2652 /*
2653 * Tracking the NMI-blocked state in software is built upon
2654 * finding the next open IRQ window. This, in turn, depends on
2655 * well-behaving guests: They have to keep IRQs disabled at
2656 * least as long as the NMI handler runs. Otherwise we may
2657 * cause NMI nesting, maybe breaking the guest. But as this is
2658 * highly unlikely, we can live with the residual risk.
2659 */
2660 vmx->soft_vnmi_blocked = 1;
2661 vmx->vnmi_blocked_time = 0;
2662 }
2663
Jan Kiszka487b3912008-09-26 09:30:56 +02002664 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002665 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002666 vmx->rmode.irq.pending = true;
2667 vmx->rmode.irq.vector = NMI_VECTOR;
2668 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2669 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2670 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2671 INTR_INFO_VALID_MASK);
2672 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2673 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2674 return;
2675 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002676 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2677 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002678}
2679
Gleb Natapovc4282df2009-04-21 17:45:07 +03002680static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002681{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002682 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002683 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002684
Gleb Natapovc4282df2009-04-21 17:45:07 +03002685 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2686 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2687 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002688}
2689
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002690static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
2691{
2692 if (!cpu_has_virtual_nmis())
2693 return to_vmx(vcpu)->soft_vnmi_blocked;
2694 else
2695 return !!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2696 GUEST_INTR_STATE_NMI);
2697}
2698
2699static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2700{
2701 struct vcpu_vmx *vmx = to_vmx(vcpu);
2702
2703 if (!cpu_has_virtual_nmis()) {
2704 if (vmx->soft_vnmi_blocked != masked) {
2705 vmx->soft_vnmi_blocked = masked;
2706 vmx->vnmi_blocked_time = 0;
2707 }
2708 } else {
2709 if (masked)
2710 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2711 GUEST_INTR_STATE_NMI);
2712 else
2713 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2714 GUEST_INTR_STATE_NMI);
2715 }
2716}
2717
Gleb Natapov78646122009-03-23 12:12:11 +02002718static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2719{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002720 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2721 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2722 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002723}
2724
Izik Eiduscbc94022007-10-25 00:29:55 +02002725static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2726{
2727 int ret;
2728 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002729 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002730 .guest_phys_addr = addr,
2731 .memory_size = PAGE_SIZE * 3,
2732 .flags = 0,
2733 };
2734
2735 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2736 if (ret)
2737 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002738 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002739 return 0;
2740}
2741
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2743 int vec, u32 err_code)
2744{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002745 /*
2746 * Instruction with address size override prefix opcode 0x67
2747 * Cause the #SS fault with 0 error code in VM86 mode.
2748 */
2749 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002750 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002752 /*
2753 * Forward all other exceptions that are valid in real mode.
2754 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2755 * the required debugging infrastructure rework.
2756 */
2757 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002758 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002759 if (vcpu->guest_debug &
2760 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2761 return 0;
2762 kvm_queue_exception(vcpu, vec);
2763 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002764 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002765 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2766 return 0;
2767 /* fall through */
2768 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002769 case OF_VECTOR:
2770 case BR_VECTOR:
2771 case UD_VECTOR:
2772 case DF_VECTOR:
2773 case SS_VECTOR:
2774 case GP_VECTOR:
2775 case MF_VECTOR:
2776 kvm_queue_exception(vcpu, vec);
2777 return 1;
2778 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 return 0;
2780}
2781
Andi Kleena0861c02009-06-08 17:37:09 +08002782/*
2783 * Trigger machine check on the host. We assume all the MSRs are already set up
2784 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2785 * We pass a fake environment to the machine check handler because we want
2786 * the guest to be always treated like user space, no matter what context
2787 * it used internally.
2788 */
2789static void kvm_machine_check(void)
2790{
2791#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2792 struct pt_regs regs = {
2793 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2794 .flags = X86_EFLAGS_IF,
2795 };
2796
2797 do_machine_check(&regs, 0);
2798#endif
2799}
2800
Avi Kivity851ba692009-08-24 11:10:17 +03002801static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002802{
2803 /* already handled by vcpu_run */
2804 return 1;
2805}
2806
Avi Kivity851ba692009-08-24 11:10:17 +03002807static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808{
Avi Kivity1155f762007-11-22 11:30:47 +02002809 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002810 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002811 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002812 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813 u32 vect_info;
2814 enum emulation_result er;
2815
Avi Kivity1155f762007-11-22 11:30:47 +02002816 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002817 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2818
Andi Kleena0861c02009-06-08 17:37:09 +08002819 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002820 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002821
Avi Kivity6aa8b732006-12-10 02:21:36 -08002822 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02002823 !is_page_fault(intr_info)) {
2824 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2825 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
2826 vcpu->run->internal.ndata = 2;
2827 vcpu->run->internal.data[0] = vect_info;
2828 vcpu->run->internal.data[1] = intr_info;
2829 return 0;
2830 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831
Jan Kiszkae4a41882008-09-26 09:30:46 +02002832 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002833 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002834
2835 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002836 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002837 return 1;
2838 }
2839
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002840 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002841 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002842 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002843 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002844 return 1;
2845 }
2846
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002848 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002849 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002850 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2851 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002852 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002853 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002854 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002856 trace_kvm_page_fault(cr2, error_code);
2857
Gleb Natapov3298b752009-05-11 13:35:46 +03002858 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002859 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002860 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 }
2862
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002863 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002865 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002866 if (vcpu->arch.halt_request) {
2867 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002868 return kvm_emulate_halt(vcpu);
2869 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002871 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002873 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002874 switch (ex_no) {
2875 case DB_VECTOR:
2876 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2877 if (!(vcpu->guest_debug &
2878 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2879 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2880 kvm_queue_exception(vcpu, DB_VECTOR);
2881 return 1;
2882 }
2883 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2884 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2885 /* fall through */
2886 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002887 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002888 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2889 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002890 break;
2891 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002892 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2893 kvm_run->ex.exception = ex_no;
2894 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002895 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002896 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002897 return 0;
2898}
2899
Avi Kivity851ba692009-08-24 11:10:17 +03002900static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002901{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002902 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002903 return 1;
2904}
2905
Avi Kivity851ba692009-08-24 11:10:17 +03002906static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002907{
Avi Kivity851ba692009-08-24 11:10:17 +03002908 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002909 return 0;
2910}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911
Avi Kivity851ba692009-08-24 11:10:17 +03002912static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002913{
He, Qingbfdaab02007-09-12 14:18:28 +08002914 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002915 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002916 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917
Avi Kivity1165f5f2007-04-19 17:27:43 +03002918 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002919 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002920 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002921
2922 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002923 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002924 return 0;
2925 return 1;
2926 }
2927
2928 size = (exit_qualification & 7) + 1;
2929 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002930 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002931
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002932 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002933 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934}
2935
Ingo Molnar102d8322007-02-19 14:37:47 +02002936static void
2937vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2938{
2939 /*
2940 * Patch in the VMCALL instruction:
2941 */
2942 hypercall[0] = 0x0f;
2943 hypercall[1] = 0x01;
2944 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002945}
2946
Avi Kivity851ba692009-08-24 11:10:17 +03002947static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002949 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002950 int cr;
2951 int reg;
2952
He, Qingbfdaab02007-09-12 14:18:28 +08002953 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 cr = exit_qualification & 15;
2955 reg = (exit_qualification >> 8) & 15;
2956 switch ((exit_qualification >> 4) & 3) {
2957 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002958 val = kvm_register_read(vcpu, reg);
2959 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 switch (cr) {
2961 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002962 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 skip_emulated_instruction(vcpu);
2964 return 1;
2965 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002966 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 skip_emulated_instruction(vcpu);
2968 return 1;
2969 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002970 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002971 skip_emulated_instruction(vcpu);
2972 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002973 case 8: {
2974 u8 cr8_prev = kvm_get_cr8(vcpu);
2975 u8 cr8 = kvm_register_read(vcpu, reg);
2976 kvm_set_cr8(vcpu, cr8);
2977 skip_emulated_instruction(vcpu);
2978 if (irqchip_in_kernel(vcpu->kvm))
2979 return 1;
2980 if (cr8_prev <= cr8)
2981 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002982 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002983 return 0;
2984 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985 };
2986 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002987 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002988 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002989 vcpu->arch.cr0 &= ~X86_CR0_TS;
2990 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002991 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002992 skip_emulated_instruction(vcpu);
2993 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002994 case 1: /*mov from cr*/
2995 switch (cr) {
2996 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002997 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002998 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002999 skip_emulated_instruction(vcpu);
3000 return 1;
3001 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003002 val = kvm_get_cr8(vcpu);
3003 kvm_register_write(vcpu, reg, val);
3004 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003005 skip_emulated_instruction(vcpu);
3006 return 1;
3007 }
3008 break;
3009 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02003010 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003011
3012 skip_emulated_instruction(vcpu);
3013 return 1;
3014 default:
3015 break;
3016 }
Avi Kivity851ba692009-08-24 11:10:17 +03003017 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10003018 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08003019 (int)(exit_qualification >> 4) & 3, cr);
3020 return 0;
3021}
3022
Avi Kivity851ba692009-08-24 11:10:17 +03003023static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003024{
He, Qingbfdaab02007-09-12 14:18:28 +08003025 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 unsigned long val;
3027 int dr, reg;
3028
Avi Kivity0a79b002009-09-01 12:03:25 +03003029 if (!kvm_require_cpl(vcpu, 0))
3030 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003031 dr = vmcs_readl(GUEST_DR7);
3032 if (dr & DR7_GD) {
3033 /*
3034 * As the vm-exit takes precedence over the debug trap, we
3035 * need to emulate the latter, either for the host or the
3036 * guest debugging itself.
3037 */
3038 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03003039 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
3040 vcpu->run->debug.arch.dr7 = dr;
3041 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003042 vmcs_readl(GUEST_CS_BASE) +
3043 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003044 vcpu->run->debug.arch.exception = DB_VECTOR;
3045 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003046 return 0;
3047 } else {
3048 vcpu->arch.dr7 &= ~DR7_GD;
3049 vcpu->arch.dr6 |= DR6_BD;
3050 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3051 kvm_queue_exception(vcpu, DB_VECTOR);
3052 return 1;
3053 }
3054 }
3055
He, Qingbfdaab02007-09-12 14:18:28 +08003056 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003057 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3058 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3059 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003060 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003061 case 0 ... 3:
3062 val = vcpu->arch.db[dr];
3063 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003065 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066 break;
3067 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003068 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069 break;
3070 default:
3071 val = 0;
3072 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003073 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003075 val = vcpu->arch.regs[reg];
3076 switch (dr) {
3077 case 0 ... 3:
3078 vcpu->arch.db[dr] = val;
3079 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
3080 vcpu->arch.eff_db[dr] = val;
3081 break;
3082 case 4 ... 5:
Avi Kivityfc78f512009-12-07 12:16:48 +02003083 if (kvm_read_cr4_bits(vcpu, X86_CR4_DE))
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003084 kvm_queue_exception(vcpu, UD_VECTOR);
3085 break;
3086 case 6:
3087 if (val & 0xffffffff00000000ULL) {
3088 kvm_queue_exception(vcpu, GP_VECTOR);
3089 break;
3090 }
3091 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3092 break;
3093 case 7:
3094 if (val & 0xffffffff00000000ULL) {
3095 kvm_queue_exception(vcpu, GP_VECTOR);
3096 break;
3097 }
3098 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3099 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3100 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3101 vcpu->arch.switch_db_regs =
3102 (val & DR7_BP_EN_MASK);
3103 }
3104 break;
3105 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003107 skip_emulated_instruction(vcpu);
3108 return 1;
3109}
3110
Avi Kivity851ba692009-08-24 11:10:17 +03003111static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112{
Avi Kivity06465c52007-02-28 20:46:53 +02003113 kvm_emulate_cpuid(vcpu);
3114 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003115}
3116
Avi Kivity851ba692009-08-24 11:10:17 +03003117static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003118{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003119 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003120 u64 data;
3121
3122 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003123 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003124 return 1;
3125 }
3126
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003127 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003128
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003130 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3131 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003132 skip_emulated_instruction(vcpu);
3133 return 1;
3134}
3135
Avi Kivity851ba692009-08-24 11:10:17 +03003136static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003137{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003138 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3139 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3140 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003142 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003143
Avi Kivity6aa8b732006-12-10 02:21:36 -08003144 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003145 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003146 return 1;
3147 }
3148
3149 skip_emulated_instruction(vcpu);
3150 return 1;
3151}
3152
Avi Kivity851ba692009-08-24 11:10:17 +03003153static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003154{
3155 return 1;
3156}
3157
Avi Kivity851ba692009-08-24 11:10:17 +03003158static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003159{
Eddie Dong85f455f2007-07-06 12:20:49 +03003160 u32 cpu_based_vm_exec_control;
3161
3162 /* clear pending irq */
3163 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3164 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3165 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003166
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003167 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003168
Dor Laorc1150d82007-01-05 16:36:24 -08003169 /*
3170 * If the user space waits to inject interrupts, exit as soon as
3171 * possible
3172 */
Gleb Natapov80618232009-04-21 17:44:56 +03003173 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003174 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003175 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003176 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003177 return 0;
3178 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179 return 1;
3180}
3181
Avi Kivity851ba692009-08-24 11:10:17 +03003182static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183{
3184 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003185 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003186}
3187
Avi Kivity851ba692009-08-24 11:10:17 +03003188static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003189{
Dor Laor510043d2007-02-19 18:25:43 +02003190 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003191 kvm_emulate_hypercall(vcpu);
3192 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003193}
3194
Avi Kivity851ba692009-08-24 11:10:17 +03003195static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003196{
3197 kvm_queue_exception(vcpu, UD_VECTOR);
3198 return 1;
3199}
3200
Avi Kivity851ba692009-08-24 11:10:17 +03003201static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003202{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003203 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003204
3205 kvm_mmu_invlpg(vcpu, exit_qualification);
3206 skip_emulated_instruction(vcpu);
3207 return 1;
3208}
3209
Avi Kivity851ba692009-08-24 11:10:17 +03003210static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003211{
3212 skip_emulated_instruction(vcpu);
3213 /* TODO: Add support for VT-d/pass-through device */
3214 return 1;
3215}
3216
Avi Kivity851ba692009-08-24 11:10:17 +03003217static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003218{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003219 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003220 enum emulation_result er;
3221 unsigned long offset;
3222
Sheng Yangf9c617f2009-03-25 10:08:52 +08003223 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003224 offset = exit_qualification & 0xffful;
3225
Avi Kivity851ba692009-08-24 11:10:17 +03003226 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003227
3228 if (er != EMULATE_DONE) {
3229 printk(KERN_ERR
3230 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3231 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003232 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003233 }
3234 return 1;
3235}
3236
Avi Kivity851ba692009-08-24 11:10:17 +03003237static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003238{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003239 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003240 unsigned long exit_qualification;
3241 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003242 int reason, type, idt_v;
3243
3244 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3245 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003246
3247 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3248
3249 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003250 if (reason == TASK_SWITCH_GATE && idt_v) {
3251 switch (type) {
3252 case INTR_TYPE_NMI_INTR:
3253 vcpu->arch.nmi_injected = false;
3254 if (cpu_has_virtual_nmis())
3255 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3256 GUEST_INTR_STATE_NMI);
3257 break;
3258 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003259 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003260 kvm_clear_interrupt_queue(vcpu);
3261 break;
3262 case INTR_TYPE_HARD_EXCEPTION:
3263 case INTR_TYPE_SOFT_EXCEPTION:
3264 kvm_clear_exception_queue(vcpu);
3265 break;
3266 default:
3267 break;
3268 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003269 }
Izik Eidus37817f22008-03-24 23:14:53 +02003270 tss_selector = exit_qualification;
3271
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003272 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3273 type != INTR_TYPE_EXT_INTR &&
3274 type != INTR_TYPE_NMI_INTR))
3275 skip_emulated_instruction(vcpu);
3276
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003277 if (!kvm_task_switch(vcpu, tss_selector, reason))
3278 return 0;
3279
3280 /* clear all local breakpoint enable flags */
3281 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3282
3283 /*
3284 * TODO: What about debug traps on tss switch?
3285 * Are we supposed to inject them and update dr6?
3286 */
3287
3288 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003289}
3290
Avi Kivity851ba692009-08-24 11:10:17 +03003291static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003292{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003293 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003294 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003295 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003296
Sheng Yangf9c617f2009-03-25 10:08:52 +08003297 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003298
3299 if (exit_qualification & (1 << 6)) {
3300 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003301 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003302 }
3303
3304 gla_validity = (exit_qualification >> 7) & 0x3;
3305 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3306 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3307 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3308 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003309 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003310 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3311 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003312 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3313 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003314 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003315 }
3316
3317 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003318 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003319 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003320}
3321
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003322static u64 ept_rsvd_mask(u64 spte, int level)
3323{
3324 int i;
3325 u64 mask = 0;
3326
3327 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3328 mask |= (1ULL << i);
3329
3330 if (level > 2)
3331 /* bits 7:3 reserved */
3332 mask |= 0xf8;
3333 else if (level == 2) {
3334 if (spte & (1ULL << 7))
3335 /* 2MB ref, bits 20:12 reserved */
3336 mask |= 0x1ff000;
3337 else
3338 /* bits 6:3 reserved */
3339 mask |= 0x78;
3340 }
3341
3342 return mask;
3343}
3344
3345static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3346 int level)
3347{
3348 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3349
3350 /* 010b (write-only) */
3351 WARN_ON((spte & 0x7) == 0x2);
3352
3353 /* 110b (write/execute) */
3354 WARN_ON((spte & 0x7) == 0x6);
3355
3356 /* 100b (execute-only) and value not supported by logical processor */
3357 if (!cpu_has_vmx_ept_execute_only())
3358 WARN_ON((spte & 0x7) == 0x4);
3359
3360 /* not 000b */
3361 if ((spte & 0x7)) {
3362 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3363
3364 if (rsvd_bits != 0) {
3365 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3366 __func__, rsvd_bits);
3367 WARN_ON(1);
3368 }
3369
3370 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3371 u64 ept_mem_type = (spte & 0x38) >> 3;
3372
3373 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3374 ept_mem_type == 7) {
3375 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3376 __func__, ept_mem_type);
3377 WARN_ON(1);
3378 }
3379 }
3380 }
3381}
3382
Avi Kivity851ba692009-08-24 11:10:17 +03003383static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003384{
3385 u64 sptes[4];
3386 int nr_sptes, i;
3387 gpa_t gpa;
3388
3389 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3390
3391 printk(KERN_ERR "EPT: Misconfiguration.\n");
3392 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3393
3394 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3395
3396 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3397 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3398
Avi Kivity851ba692009-08-24 11:10:17 +03003399 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3400 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003401
3402 return 0;
3403}
3404
Avi Kivity851ba692009-08-24 11:10:17 +03003405static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003406{
3407 u32 cpu_based_vm_exec_control;
3408
3409 /* clear pending NMI */
3410 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3411 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3412 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3413 ++vcpu->stat.nmi_window_exits;
3414
3415 return 1;
3416}
3417
Mohammed Gamal80ced182009-09-01 12:48:18 +02003418static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003419{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003420 struct vcpu_vmx *vmx = to_vmx(vcpu);
3421 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003422 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003423
3424 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003425 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003426
Mohammed Gamal80ced182009-09-01 12:48:18 +02003427 if (err == EMULATE_DO_MMIO) {
3428 ret = 0;
3429 goto out;
3430 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003431
3432 if (err != EMULATE_DONE) {
3433 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003434 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3435 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02003436 vcpu->run->internal.ndata = 0;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003437 ret = 0;
3438 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003439 }
3440
3441 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003442 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003443 if (need_resched())
3444 schedule();
3445 }
3446
Mohammed Gamal80ced182009-09-01 12:48:18 +02003447 vmx->emulation_required = 0;
3448out:
3449 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003450}
3451
Avi Kivity6aa8b732006-12-10 02:21:36 -08003452/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003453 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3454 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3455 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003456static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003457{
3458 skip_emulated_instruction(vcpu);
3459 kvm_vcpu_on_spin(vcpu);
3460
3461 return 1;
3462}
3463
Sheng Yang59708672009-12-15 13:29:54 +08003464static int handle_invalid_op(struct kvm_vcpu *vcpu)
3465{
3466 kvm_queue_exception(vcpu, UD_VECTOR);
3467 return 1;
3468}
3469
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003470/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003471 * The exit handlers return 1 if the exit was handled fully and guest execution
3472 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3473 * to be done to userspace and return 0.
3474 */
Avi Kivity851ba692009-08-24 11:10:17 +03003475static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3477 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003478 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003479 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003480 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003481 [EXIT_REASON_CR_ACCESS] = handle_cr,
3482 [EXIT_REASON_DR_ACCESS] = handle_dr,
3483 [EXIT_REASON_CPUID] = handle_cpuid,
3484 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3485 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3486 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3487 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003488 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003489 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003490 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3491 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3492 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3493 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3494 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3495 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3496 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3497 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3498 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003499 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3500 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003501 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003502 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003503 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003504 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3505 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003506 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003507 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3508 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003509};
3510
3511static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003512 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003513
3514/*
3515 * The guest has exited. See if we can fix it or if we need userspace
3516 * assistance.
3517 */
Avi Kivity851ba692009-08-24 11:10:17 +03003518static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003519{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003520 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003521 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003522 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003523
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003524 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003525
Mohammed Gamal80ced182009-09-01 12:48:18 +02003526 /* If guest state is invalid, start emulating */
3527 if (vmx->emulation_required && emulate_invalid_guest_state)
3528 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003529
Sheng Yang14394422008-04-28 12:24:45 +08003530 /* Access CR3 don't cause VMExit in paging mode, so we need
3531 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003532 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003533 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003534
Avi Kivity29bd8a72007-09-10 17:27:03 +03003535 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003536 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3537 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003538 = vmcs_read32(VM_INSTRUCTION_ERROR);
3539 return 0;
3540 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003541
Mike Dayd77c26f2007-10-08 09:02:08 -04003542 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003543 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003544 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3545 exit_reason != EXIT_REASON_TASK_SWITCH))
3546 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3547 "(0x%x) and exit reason is 0x%x\n",
3548 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003549
3550 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003551 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003552 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003553 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003554 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003555 /*
3556 * This CPU don't support us in finding the end of an
3557 * NMI-blocked window if the guest runs with IRQs
3558 * disabled. So we pull the trigger after 1 s of
3559 * futile waiting, but inform the user about this.
3560 */
3561 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3562 "state on VCPU %d after 1 s timeout\n",
3563 __func__, vcpu->vcpu_id);
3564 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003565 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003566 }
3567
Avi Kivity6aa8b732006-12-10 02:21:36 -08003568 if (exit_reason < kvm_vmx_max_exit_handlers
3569 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003570 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003571 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003572 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3573 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003574 }
3575 return 0;
3576}
3577
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003578static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003579{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003580 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003581 vmcs_write32(TPR_THRESHOLD, 0);
3582 return;
3583 }
3584
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003585 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003586}
3587
Avi Kivitycf393f72008-07-01 16:20:21 +03003588static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3589{
3590 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003591 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003592 bool unblock_nmi;
3593 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003594 int type;
3595 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003596
3597 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003598
Andi Kleena0861c02009-06-08 17:37:09 +08003599 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3600
3601 /* Handle machine checks before interrupts are enabled */
3602 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3603 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3604 && is_machine_check(exit_intr_info)))
3605 kvm_machine_check();
3606
Gleb Natapov20f65982009-05-11 13:35:55 +03003607 /* We need to handle NMIs before interrupts are enabled */
3608 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003609 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003610 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003611
3612 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3613
Avi Kivitycf393f72008-07-01 16:20:21 +03003614 if (cpu_has_virtual_nmis()) {
3615 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3616 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3617 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003618 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003619 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3620 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003621 * SDM 3: 23.2.2 (September 2008)
3622 * Bit 12 is undefined in any of the following cases:
3623 * If the VM exit sets the valid bit in the IDT-vectoring
3624 * information field.
3625 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003626 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003627 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3628 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003629 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3630 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003631 } else if (unlikely(vmx->soft_vnmi_blocked))
3632 vmx->vnmi_blocked_time +=
3633 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003634
Gleb Natapov37b96e92009-03-30 16:03:13 +03003635 vmx->vcpu.arch.nmi_injected = false;
3636 kvm_clear_exception_queue(&vmx->vcpu);
3637 kvm_clear_interrupt_queue(&vmx->vcpu);
3638
3639 if (!idtv_info_valid)
3640 return;
3641
Avi Kivity668f6122008-07-02 09:28:55 +03003642 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3643 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003644
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003645 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003646 case INTR_TYPE_NMI_INTR:
3647 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003648 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003649 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003650 * Clear bit "block by NMI" before VM entry if a NMI
3651 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003652 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003653 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3654 GUEST_INTR_STATE_NMI);
3655 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003656 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003657 vmx->vcpu.arch.event_exit_inst_len =
3658 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3659 /* fall through */
3660 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003661 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003662 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3663 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003664 } else
3665 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003666 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003667 case INTR_TYPE_SOFT_INTR:
3668 vmx->vcpu.arch.event_exit_inst_len =
3669 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3670 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003671 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003672 kvm_queue_interrupt(&vmx->vcpu, vector,
3673 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003674 break;
3675 default:
3676 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003677 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003678}
3679
Avi Kivity9c8cba32007-11-22 11:42:59 +02003680/*
3681 * Failure to inject an interrupt should give us the information
3682 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3683 * when fetching the interrupt redirection bitmap in the real-mode
3684 * tss, this doesn't happen. So we do it ourselves.
3685 */
3686static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3687{
3688 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003689 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003690 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003691 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003692 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3693 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3694 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3695 return;
3696 }
3697 vmx->idt_vectoring_info =
3698 VECTORING_INFO_VALID_MASK
3699 | INTR_TYPE_EXT_INTR
3700 | vmx->rmode.irq.vector;
3701}
3702
Avi Kivityc8019492008-07-14 14:44:59 +03003703#ifdef CONFIG_X86_64
3704#define R "r"
3705#define Q "q"
3706#else
3707#define R "e"
3708#define Q "l"
3709#endif
3710
Avi Kivity851ba692009-08-24 11:10:17 +03003711static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003712{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003713 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003714
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003715 /* Record the guest's net vcpu time for enforced NMI injections. */
3716 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3717 vmx->entry_time = ktime_get();
3718
Mohammed Gamal80ced182009-09-01 12:48:18 +02003719 /* Don't enter VMX if guest state is invalid, let the exit handler
3720 start emulation until we arrive back to a valid state */
3721 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003722 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003723
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003724 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3725 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3726 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3727 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3728
Gleb Natapov787ff732009-05-18 11:44:06 +03003729 /* When single-stepping over STI and MOV SS, we must clear the
3730 * corresponding interruptibility bits in the guest state. Otherwise
3731 * vmentry fails as it then expects bit 14 (BS) in pending debug
3732 * exceptions being set, but that's not correct for the guest debugging
3733 * case. */
3734 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3735 vmx_set_interrupt_shadow(vcpu, 0);
3736
Avi Kivitye6adf282007-04-30 16:07:54 +03003737 /*
3738 * Loading guest fpu may have cleared host cr0.ts
3739 */
3740 vmcs_writel(HOST_CR0, read_cr0());
3741
Avi Kivitye8a48342009-09-01 16:06:25 +03003742 if (vcpu->arch.switch_db_regs)
3743 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003744
Mike Dayd77c26f2007-10-08 09:02:08 -04003745 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003746 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003747 "push %%"R"dx; push %%"R"bp;"
3748 "push %%"R"cx \n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003749 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3750 "je 1f \n\t"
3751 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003752 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd492008-07-17 18:04:30 +03003753 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003754 /* Reload cr2 if changed */
3755 "mov %c[cr2](%0), %%"R"ax \n\t"
3756 "mov %%cr2, %%"R"dx \n\t"
3757 "cmp %%"R"ax, %%"R"dx \n\t"
3758 "je 2f \n\t"
3759 "mov %%"R"ax, %%cr2 \n\t"
3760 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003761 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003762 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003763 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003764 "mov %c[rax](%0), %%"R"ax \n\t"
3765 "mov %c[rbx](%0), %%"R"bx \n\t"
3766 "mov %c[rdx](%0), %%"R"dx \n\t"
3767 "mov %c[rsi](%0), %%"R"si \n\t"
3768 "mov %c[rdi](%0), %%"R"di \n\t"
3769 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003770#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003771 "mov %c[r8](%0), %%r8 \n\t"
3772 "mov %c[r9](%0), %%r9 \n\t"
3773 "mov %c[r10](%0), %%r10 \n\t"
3774 "mov %c[r11](%0), %%r11 \n\t"
3775 "mov %c[r12](%0), %%r12 \n\t"
3776 "mov %c[r13](%0), %%r13 \n\t"
3777 "mov %c[r14](%0), %%r14 \n\t"
3778 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003779#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003780 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3781
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003783 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003784 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003785 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003786 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003787 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003788 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003789 "xchg %0, (%%"R"sp) \n\t"
3790 "mov %%"R"ax, %c[rax](%0) \n\t"
3791 "mov %%"R"bx, %c[rbx](%0) \n\t"
3792 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3793 "mov %%"R"dx, %c[rdx](%0) \n\t"
3794 "mov %%"R"si, %c[rsi](%0) \n\t"
3795 "mov %%"R"di, %c[rdi](%0) \n\t"
3796 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003797#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003798 "mov %%r8, %c[r8](%0) \n\t"
3799 "mov %%r9, %c[r9](%0) \n\t"
3800 "mov %%r10, %c[r10](%0) \n\t"
3801 "mov %%r11, %c[r11](%0) \n\t"
3802 "mov %%r12, %c[r12](%0) \n\t"
3803 "mov %%r13, %c[r13](%0) \n\t"
3804 "mov %%r14, %c[r14](%0) \n\t"
3805 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003806#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003807 "mov %%cr2, %%"R"ax \n\t"
3808 "mov %%"R"ax, %c[cr2](%0) \n\t"
3809
3810 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003811 "setbe %c[fail](%0) \n\t"
3812 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3813 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3814 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd492008-07-17 18:04:30 +03003815 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003816 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3817 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3818 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3819 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3820 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3821 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3822 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003823#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003824 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3825 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3826 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3827 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3828 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3829 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3830 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3831 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003832#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003833 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003834 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003835 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003836#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003837 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3838#endif
3839 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003840
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003841 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3842 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003843 vcpu->arch.regs_dirty = 0;
3844
Avi Kivitye8a48342009-09-01 16:06:25 +03003845 if (vcpu->arch.switch_db_regs)
3846 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003847
Avi Kivity1155f762007-11-22 11:30:47 +02003848 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003849 if (vmx->rmode.irq.pending)
3850 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003851
Mike Dayd77c26f2007-10-08 09:02:08 -04003852 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003853 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003854
Avi Kivitycf393f72008-07-01 16:20:21 +03003855 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003856}
3857
Avi Kivityc8019492008-07-14 14:44:59 +03003858#undef R
3859#undef Q
3860
Avi Kivity6aa8b732006-12-10 02:21:36 -08003861static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3862{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003863 struct vcpu_vmx *vmx = to_vmx(vcpu);
3864
3865 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003866 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003867 free_vmcs(vmx->vmcs);
3868 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003869 }
3870}
3871
3872static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3873{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003874 struct vcpu_vmx *vmx = to_vmx(vcpu);
3875
Sheng Yang2384d2b2008-01-17 15:14:33 +08003876 spin_lock(&vmx_vpid_lock);
3877 if (vmx->vpid != 0)
3878 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3879 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003880 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003881 kfree(vmx->guest_msrs);
3882 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003883 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003884}
3885
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003886static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003887{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003888 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003889 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003890 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003891
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003892 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003893 return ERR_PTR(-ENOMEM);
3894
Sheng Yang2384d2b2008-01-17 15:14:33 +08003895 allocate_vpid(vmx);
3896
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003897 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3898 if (err)
3899 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003900
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003901 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003902 if (!vmx->guest_msrs) {
3903 err = -ENOMEM;
3904 goto uninit_vcpu;
3905 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003906
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003907 vmx->vmcs = alloc_vmcs();
3908 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003909 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003910
3911 vmcs_clear(vmx->vmcs);
3912
Avi Kivity15ad7142007-07-11 18:17:21 +03003913 cpu = get_cpu();
3914 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003915 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003916 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003917 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003918 if (err)
3919 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003920 if (vm_need_virtualize_apic_accesses(kvm))
3921 if (alloc_apic_access_page(kvm) != 0)
3922 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003923
Sheng Yangb927a3c2009-07-21 10:42:48 +08003924 if (enable_ept) {
3925 if (!kvm->arch.ept_identity_map_addr)
3926 kvm->arch.ept_identity_map_addr =
3927 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003928 if (alloc_identity_pagetable(kvm) != 0)
3929 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003930 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003931
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003932 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003933
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003934free_vmcs:
3935 free_vmcs(vmx->vmcs);
3936free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003937 kfree(vmx->guest_msrs);
3938uninit_vcpu:
3939 kvm_vcpu_uninit(&vmx->vcpu);
3940free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003941 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003942 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943}
3944
Yang, Sheng002c7f72007-07-31 14:23:01 +03003945static void __init vmx_check_processor_compat(void *rtn)
3946{
3947 struct vmcs_config vmcs_conf;
3948
3949 *(int *)rtn = 0;
3950 if (setup_vmcs_config(&vmcs_conf) < 0)
3951 *(int *)rtn = -EIO;
3952 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3953 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3954 smp_processor_id());
3955 *(int *)rtn = -EIO;
3956 }
3957}
3958
Sheng Yang67253af2008-04-25 10:20:22 +08003959static int get_ept_level(void)
3960{
3961 return VMX_EPT_DEFAULT_GAW + 1;
3962}
3963
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003964static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003965{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003966 u64 ret;
3967
Sheng Yang522c68c2009-04-27 20:35:43 +08003968 /* For VT-d and EPT combination
3969 * 1. MMIO: always map as UC
3970 * 2. EPT with VT-d:
3971 * a. VT-d without snooping control feature: can't guarantee the
3972 * result, try to trust guest.
3973 * b. VT-d with snooping control feature: snooping control feature of
3974 * VT-d engine can guarantee the cache correctness. Just set it
3975 * to WB to keep consistent with host. So the same as item 3.
3976 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3977 * consistent with host MTRR
3978 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003979 if (is_mmio)
3980 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003981 else if (vcpu->kvm->arch.iommu_domain &&
3982 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3983 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3984 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003985 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003986 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3987 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003988
3989 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003990}
3991
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003992static const struct trace_print_flags vmx_exit_reasons_str[] = {
3993 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3994 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3995 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3996 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3997 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3998 { EXIT_REASON_CR_ACCESS, "cr_access" },
3999 { EXIT_REASON_DR_ACCESS, "dr_access" },
4000 { EXIT_REASON_CPUID, "cpuid" },
4001 { EXIT_REASON_MSR_READ, "rdmsr" },
4002 { EXIT_REASON_MSR_WRITE, "wrmsr" },
4003 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
4004 { EXIT_REASON_HLT, "halt" },
4005 { EXIT_REASON_INVLPG, "invlpg" },
4006 { EXIT_REASON_VMCALL, "hypercall" },
4007 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
4008 { EXIT_REASON_APIC_ACCESS, "apic_access" },
4009 { EXIT_REASON_WBINVD, "wbinvd" },
4010 { EXIT_REASON_TASK_SWITCH, "task_switch" },
4011 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
4012 { -1, NULL }
4013};
4014
Joerg Roedel344f4142009-07-27 16:30:48 +02004015static bool vmx_gb_page_enable(void)
4016{
4017 return false;
4018}
4019
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004020static inline u32 bit(int bitno)
4021{
4022 return 1 << (bitno & 31);
4023}
4024
Sheng Yang0e851882009-12-18 16:48:46 +08004025static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
4026{
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004027 struct kvm_cpuid_entry2 *best;
4028 struct vcpu_vmx *vmx = to_vmx(vcpu);
4029 u32 exec_control;
4030
4031 vmx->rdtscp_enabled = false;
4032 if (vmx_rdtscp_supported()) {
4033 exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
4034 if (exec_control & SECONDARY_EXEC_RDTSCP) {
4035 best = kvm_find_cpuid_entry(vcpu, 0x80000001, 0);
4036 if (best && (best->edx & bit(X86_FEATURE_RDTSCP)))
4037 vmx->rdtscp_enabled = true;
4038 else {
4039 exec_control &= ~SECONDARY_EXEC_RDTSCP;
4040 vmcs_write32(SECONDARY_VM_EXEC_CONTROL,
4041 exec_control);
4042 }
4043 }
4044 }
Sheng Yang0e851882009-12-18 16:48:46 +08004045}
4046
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03004047static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08004048 .cpu_has_kvm_support = cpu_has_kvm_support,
4049 .disabled_by_bios = vmx_disabled_by_bios,
4050 .hardware_setup = hardware_setup,
4051 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03004052 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004053 .hardware_enable = hardware_enable,
4054 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08004055 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004056
4057 .vcpu_create = vmx_create_vcpu,
4058 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004059 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004060
Avi Kivity04d2cc72007-09-10 18:10:54 +03004061 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004062 .vcpu_load = vmx_vcpu_load,
4063 .vcpu_put = vmx_vcpu_put,
4064
4065 .set_guest_debug = set_guest_debug,
4066 .get_msr = vmx_get_msr,
4067 .set_msr = vmx_set_msr,
4068 .get_segment_base = vmx_get_segment_base,
4069 .get_segment = vmx_get_segment,
4070 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004071 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004072 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004073 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004074 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004075 .set_cr3 = vmx_set_cr3,
4076 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004077 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004078 .get_idt = vmx_get_idt,
4079 .set_idt = vmx_set_idt,
4080 .get_gdt = vmx_get_gdt,
4081 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004082 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004083 .get_rflags = vmx_get_rflags,
4084 .set_rflags = vmx_set_rflags,
4085
4086 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004087
Avi Kivity6aa8b732006-12-10 02:21:36 -08004088 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004089 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004090 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004091 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4092 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004093 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004094 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004095 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004096 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004097 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004098 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004099 .get_nmi_mask = vmx_get_nmi_mask,
4100 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004101 .enable_nmi_window = enable_nmi_window,
4102 .enable_irq_window = enable_irq_window,
4103 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004104
Izik Eiduscbc94022007-10-25 00:29:55 +02004105 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004106 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004107 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004108
4109 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02004110 .gb_page_enable = vmx_gb_page_enable,
Sheng Yang0e851882009-12-18 16:48:46 +08004111
4112 .cpuid_update = vmx_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004113
4114 .rdtscp_supported = vmx_rdtscp_supported,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004115};
4116
4117static int __init vmx_init(void)
4118{
Avi Kivity26bb0982009-09-07 11:14:12 +03004119 int r, i;
4120
4121 rdmsrl_safe(MSR_EFER, &host_efer);
4122
4123 for (i = 0; i < NR_VMX_MSR; ++i)
4124 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004125
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004126 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004127 if (!vmx_io_bitmap_a)
4128 return -ENOMEM;
4129
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004130 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004131 if (!vmx_io_bitmap_b) {
4132 r = -ENOMEM;
4133 goto out;
4134 }
4135
Avi Kivity58972972009-02-24 22:26:47 +02004136 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4137 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004138 r = -ENOMEM;
4139 goto out1;
4140 }
4141
Avi Kivity58972972009-02-24 22:26:47 +02004142 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4143 if (!vmx_msr_bitmap_longmode) {
4144 r = -ENOMEM;
4145 goto out2;
4146 }
4147
He, Qingfdef3ad2007-04-30 09:45:24 +03004148 /*
4149 * Allow direct access to the PC debug port (it is often used for I/O
4150 * delays, but the vmexits simply slow things down).
4151 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004152 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4153 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004154
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004155 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004156
Avi Kivity58972972009-02-24 22:26:47 +02004157 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4158 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004159
Sheng Yang2384d2b2008-01-17 15:14:33 +08004160 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4161
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004162 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004163 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004164 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004165
Avi Kivity58972972009-02-24 22:26:47 +02004166 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4167 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4168 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4169 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4170 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4171 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004172
Avi Kivity089d0342009-03-23 18:26:32 +02004173 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004174 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004175 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004176 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004177 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004178 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004179 kvm_enable_tdp();
4180 } else
4181 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004182
Avi Kivityc7addb92007-09-16 18:58:32 +02004183 if (bypass_guest_pf)
4184 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4185
He, Qingfdef3ad2007-04-30 09:45:24 +03004186 return 0;
4187
Avi Kivity58972972009-02-24 22:26:47 +02004188out3:
4189 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004190out2:
Avi Kivity58972972009-02-24 22:26:47 +02004191 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004192out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004193 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004194out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004195 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004196 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004197}
4198
4199static void __exit vmx_exit(void)
4200{
Avi Kivity58972972009-02-24 22:26:47 +02004201 free_page((unsigned long)vmx_msr_bitmap_legacy);
4202 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004203 free_page((unsigned long)vmx_io_bitmap_b);
4204 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004205
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004206 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004207}
4208
4209module_init(vmx_init)
4210module_exit(vmx_exit)