blob: 97f4265cda3852107590edec2482a925b296d307 [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
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040064struct vmcs {
65 u32 revision_id;
66 u32 abort;
67 char data[0];
68};
69
70struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100071 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030072 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030073 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040074 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +030075 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +020076 u32 idt_vectoring_info;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040077 struct kvm_msr_entry *guest_msrs;
78 struct kvm_msr_entry *host_msrs;
79 int nmsrs;
80 int save_nmsrs;
81 int msr_offset_efer;
82#ifdef CONFIG_X86_64
83 int msr_offset_kernel_gs_base;
84#endif
85 struct vmcs *vmcs;
86 struct {
87 int loaded;
88 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +020089 int gs_ldt_reload_needed;
90 int fs_reload_needed;
Avi Kivity51c6cf62007-08-29 03:48:05 +030091 int guest_efer_loaded;
Mike Dayd77c26f2007-10-08 09:02:08 -040092 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +020093 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +030094 int vm86_active;
95 u8 save_iopl;
96 struct kvm_save_segment {
97 u16 selector;
98 unsigned long base;
99 u32 limit;
100 u32 ar;
101 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200102 struct {
103 bool pending;
104 u8 vector;
105 unsigned rip;
106 } irq;
107 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800108 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300109 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200110
111 /* Support for vnmi-less CPUs */
112 int soft_vnmi_blocked;
113 ktime_t entry_time;
114 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800115 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400116};
117
118static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
119{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000120 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400121}
122
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800123static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800124static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300125
Avi Kivity6aa8b732006-12-10 02:21:36 -0800126static DEFINE_PER_CPU(struct vmcs *, vmxarea);
127static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300128static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800129
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200130static unsigned long *vmx_io_bitmap_a;
131static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200132static unsigned long *vmx_msr_bitmap_legacy;
133static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300134
Sheng Yang2384d2b2008-01-17 15:14:33 +0800135static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
136static DEFINE_SPINLOCK(vmx_vpid_lock);
137
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300138static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800139 int size;
140 int order;
141 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300142 u32 pin_based_exec_ctrl;
143 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800144 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300145 u32 vmexit_ctrl;
146 u32 vmentry_ctrl;
147} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148
Hannes Ederefff9e52008-11-28 17:02:06 +0100149static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800150 u32 ept;
151 u32 vpid;
152} vmx_capability;
153
Avi Kivity6aa8b732006-12-10 02:21:36 -0800154#define VMX_SEGMENT_FIELD(seg) \
155 [VCPU_SREG_##seg] = { \
156 .selector = GUEST_##seg##_SELECTOR, \
157 .base = GUEST_##seg##_BASE, \
158 .limit = GUEST_##seg##_LIMIT, \
159 .ar_bytes = GUEST_##seg##_AR_BYTES, \
160 }
161
162static struct kvm_vmx_segment_field {
163 unsigned selector;
164 unsigned base;
165 unsigned limit;
166 unsigned ar_bytes;
167} kvm_vmx_segment_fields[] = {
168 VMX_SEGMENT_FIELD(CS),
169 VMX_SEGMENT_FIELD(DS),
170 VMX_SEGMENT_FIELD(ES),
171 VMX_SEGMENT_FIELD(FS),
172 VMX_SEGMENT_FIELD(GS),
173 VMX_SEGMENT_FIELD(SS),
174 VMX_SEGMENT_FIELD(TR),
175 VMX_SEGMENT_FIELD(LDTR),
176};
177
Avi Kivity6de4f3ad2009-05-31 22:58:47 +0300178static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
179
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300180/*
181 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
182 * away by decrementing the array size.
183 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800184static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800185#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE,
187#endif
188 MSR_EFER, MSR_K6_STAR,
189};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200190#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800191
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400192static void load_msrs(struct kvm_msr_entry *e, int n)
193{
194 int i;
195
196 for (i = 0; i < n; ++i)
197 wrmsrl(e[i].index, e[i].data);
198}
199
200static void save_msrs(struct kvm_msr_entry *e, int n)
201{
202 int i;
203
204 for (i = 0; i < n; ++i)
205 rdmsrl(e[i].index, e[i].data);
206}
207
Avi Kivity6aa8b732006-12-10 02:21:36 -0800208static inline int is_page_fault(u32 intr_info)
209{
210 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
211 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100212 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213}
214
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300215static inline int is_no_device(u32 intr_info)
216{
217 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
218 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100219 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300220}
221
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500222static inline int is_invalid_opcode(u32 intr_info)
223{
224 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
225 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100226 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500227}
228
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229static inline int is_external_interrupt(u32 intr_info)
230{
231 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
232 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
233}
234
Andi Kleena0861c02009-06-08 17:37:09 +0800235static inline int is_machine_check(u32 intr_info)
236{
237 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
238 INTR_INFO_VALID_MASK)) ==
239 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
240}
241
Sheng Yang25c5f222008-03-28 13:18:56 +0800242static inline int cpu_has_vmx_msr_bitmap(void)
243{
Sheng Yang04547152009-04-01 15:52:31 +0800244 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800245}
246
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800247static inline int cpu_has_vmx_tpr_shadow(void)
248{
Sheng Yang04547152009-04-01 15:52:31 +0800249 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800250}
251
252static inline int vm_need_tpr_shadow(struct kvm *kvm)
253{
Sheng Yang04547152009-04-01 15:52:31 +0800254 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800255}
256
Sheng Yangf78e0e22007-10-29 09:40:42 +0800257static inline int cpu_has_secondary_exec_ctrls(void)
258{
Sheng Yang04547152009-04-01 15:52:31 +0800259 return vmcs_config.cpu_based_exec_ctrl &
260 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800261}
262
Avi Kivity774ead32007-12-26 13:57:04 +0200263static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800264{
Sheng Yang04547152009-04-01 15:52:31 +0800265 return vmcs_config.cpu_based_2nd_exec_ctrl &
266 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
267}
268
269static inline bool cpu_has_vmx_flexpriority(void)
270{
271 return cpu_has_vmx_tpr_shadow() &&
272 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800273}
274
Marcelo Tosattie7997942009-06-11 12:07:40 -0300275static inline bool cpu_has_vmx_ept_execute_only(void)
276{
277 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
278}
279
280static inline bool cpu_has_vmx_eptp_uncacheable(void)
281{
282 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
283}
284
285static inline bool cpu_has_vmx_eptp_writeback(void)
286{
287 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
288}
289
290static inline bool cpu_has_vmx_ept_2m_page(void)
291{
292 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
293}
294
Sheng Yangd56f5462008-04-25 10:13:16 +0800295static inline int cpu_has_vmx_invept_individual_addr(void)
296{
Sheng Yang04547152009-04-01 15:52:31 +0800297 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800298}
299
300static inline int cpu_has_vmx_invept_context(void)
301{
Sheng Yang04547152009-04-01 15:52:31 +0800302 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800303}
304
305static inline int cpu_has_vmx_invept_global(void)
306{
Sheng Yang04547152009-04-01 15:52:31 +0800307 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800308}
309
310static inline int cpu_has_vmx_ept(void)
311{
Sheng Yang04547152009-04-01 15:52:31 +0800312 return vmcs_config.cpu_based_2nd_exec_ctrl &
313 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800314}
315
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700316static inline int cpu_has_vmx_unrestricted_guest(void)
317{
318 return vmcs_config.cpu_based_2nd_exec_ctrl &
319 SECONDARY_EXEC_UNRESTRICTED_GUEST;
320}
321
Sheng Yangf78e0e22007-10-29 09:40:42 +0800322static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
323{
Sheng Yang04547152009-04-01 15:52:31 +0800324 return flexpriority_enabled &&
325 (cpu_has_vmx_virtualize_apic_accesses()) &&
326 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800327}
328
Sheng Yang2384d2b2008-01-17 15:14:33 +0800329static inline int cpu_has_vmx_vpid(void)
330{
Sheng Yang04547152009-04-01 15:52:31 +0800331 return vmcs_config.cpu_based_2nd_exec_ctrl &
332 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800333}
334
Sheng Yangf08864b2008-05-15 18:23:25 +0800335static inline int cpu_has_virtual_nmis(void)
336{
337 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
338}
339
Sheng Yang04547152009-04-01 15:52:31 +0800340static inline bool report_flexpriority(void)
341{
342 return flexpriority_enabled;
343}
344
Rusty Russell8b9cf982007-07-30 16:31:43 +1000345static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800346{
347 int i;
348
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400349 for (i = 0; i < vmx->nmsrs; ++i)
350 if (vmx->guest_msrs[i].index == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300351 return i;
352 return -1;
353}
354
Sheng Yang2384d2b2008-01-17 15:14:33 +0800355static inline void __invvpid(int ext, u16 vpid, gva_t gva)
356{
357 struct {
358 u64 vpid : 16;
359 u64 rsvd : 48;
360 u64 gva;
361 } operand = { vpid, 0, gva };
362
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300363 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800364 /* CF==1 or ZF==1 --> rc = -1 */
365 "; ja 1f ; ud2 ; 1:"
366 : : "a"(&operand), "c"(ext) : "cc", "memory");
367}
368
Sheng Yang14394422008-04-28 12:24:45 +0800369static inline void __invept(int ext, u64 eptp, gpa_t gpa)
370{
371 struct {
372 u64 eptp, gpa;
373 } operand = {eptp, gpa};
374
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300375 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800376 /* CF==1 or ZF==1 --> rc = -1 */
377 "; ja 1f ; ud2 ; 1:\n"
378 : : "a" (&operand), "c" (ext) : "cc", "memory");
379}
380
Rusty Russell8b9cf982007-07-30 16:31:43 +1000381static struct kvm_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300382{
383 int i;
384
Rusty Russell8b9cf982007-07-30 16:31:43 +1000385 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300386 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400387 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000388 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800389}
390
Avi Kivity6aa8b732006-12-10 02:21:36 -0800391static void vmcs_clear(struct vmcs *vmcs)
392{
393 u64 phys_addr = __pa(vmcs);
394 u8 error;
395
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300396 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800397 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
398 : "cc", "memory");
399 if (error)
400 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
401 vmcs, phys_addr);
402}
403
404static void __vcpu_clear(void *arg)
405{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000406 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800407 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408
Rusty Russell8b9cf982007-07-30 16:31:43 +1000409 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400410 vmcs_clear(vmx->vmcs);
411 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800413 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300414 list_del(&vmx->local_vcpus_link);
415 vmx->vcpu.cpu = -1;
416 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800417}
418
Rusty Russell8b9cf982007-07-30 16:31:43 +1000419static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800420{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200421 if (vmx->vcpu.cpu == -1)
422 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200423 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800424}
425
Sheng Yang2384d2b2008-01-17 15:14:33 +0800426static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
427{
428 if (vmx->vpid == 0)
429 return;
430
431 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
432}
433
Sheng Yang14394422008-04-28 12:24:45 +0800434static inline void ept_sync_global(void)
435{
436 if (cpu_has_vmx_invept_global())
437 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
438}
439
440static inline void ept_sync_context(u64 eptp)
441{
Avi Kivity089d0342009-03-23 18:26:32 +0200442 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800443 if (cpu_has_vmx_invept_context())
444 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
445 else
446 ept_sync_global();
447 }
448}
449
450static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
451{
Avi Kivity089d0342009-03-23 18:26:32 +0200452 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800453 if (cpu_has_vmx_invept_individual_addr())
454 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
455 eptp, gpa);
456 else
457 ept_sync_context(eptp);
458 }
459}
460
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461static unsigned long vmcs_readl(unsigned long field)
462{
463 unsigned long value;
464
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300465 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800466 : "=a"(value) : "d"(field) : "cc");
467 return value;
468}
469
470static u16 vmcs_read16(unsigned long field)
471{
472 return vmcs_readl(field);
473}
474
475static u32 vmcs_read32(unsigned long field)
476{
477 return vmcs_readl(field);
478}
479
480static u64 vmcs_read64(unsigned long field)
481{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800482#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 return vmcs_readl(field);
484#else
485 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
486#endif
487}
488
Avi Kivitye52de1b2007-01-05 16:36:56 -0800489static noinline void vmwrite_error(unsigned long field, unsigned long value)
490{
491 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
492 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
493 dump_stack();
494}
495
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496static void vmcs_writel(unsigned long field, unsigned long value)
497{
498 u8 error;
499
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300500 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400501 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800502 if (unlikely(error))
503 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504}
505
506static void vmcs_write16(unsigned long field, u16 value)
507{
508 vmcs_writel(field, value);
509}
510
511static void vmcs_write32(unsigned long field, u32 value)
512{
513 vmcs_writel(field, value);
514}
515
516static void vmcs_write64(unsigned long field, u64 value)
517{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300519#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800520 asm volatile ("");
521 vmcs_writel(field+1, value >> 32);
522#endif
523}
524
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300525static void vmcs_clear_bits(unsigned long field, u32 mask)
526{
527 vmcs_writel(field, vmcs_readl(field) & ~mask);
528}
529
530static void vmcs_set_bits(unsigned long field, u32 mask)
531{
532 vmcs_writel(field, vmcs_readl(field) | mask);
533}
534
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300535static void update_exception_bitmap(struct kvm_vcpu *vcpu)
536{
537 u32 eb;
538
Andi Kleena0861c02009-06-08 17:37:09 +0800539 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300540 if (!vcpu->fpu_active)
541 eb |= 1u << NM_VECTOR;
Avi Kivitye8a48342009-09-01 16:06:25 +0300542 /*
543 * Unconditionally intercept #DB so we can maintain dr6 without
544 * reading it every exit.
545 */
546 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100547 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100548 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
549 eb |= 1u << BP_VECTOR;
550 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300551 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300552 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200553 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800554 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300555 vmcs_write32(EXCEPTION_BITMAP, eb);
556}
557
Avi Kivity33ed6322007-05-02 16:54:03 +0300558static void reload_tss(void)
559{
Avi Kivity33ed6322007-05-02 16:54:03 +0300560 /*
561 * VT restores TR but not its size. Useless.
562 */
563 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200564 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300565
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300566 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300567 descs = (void *)gdt.base;
568 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
569 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300570}
571
Rusty Russell8b9cf982007-07-30 16:31:43 +1000572static void load_transition_efer(struct vcpu_vmx *vmx)
Eddie Dong2cc51562007-05-21 07:28:09 +0300573{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400574 int efer_offset = vmx->msr_offset_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700575 u64 host_efer;
576 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300577 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300578
Avi Kivity51c6cf62007-08-29 03:48:05 +0300579 if (efer_offset < 0)
580 return;
Roel Kluin3a34a882009-08-04 02:08:45 -0700581 host_efer = vmx->host_msrs[efer_offset].data;
582 guest_efer = vmx->guest_msrs[efer_offset].data;
583
Avi Kivity51c6cf62007-08-29 03:48:05 +0300584 /*
585 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
586 * outside long mode
587 */
588 ignore_bits = EFER_NX | EFER_SCE;
589#ifdef CONFIG_X86_64
590 ignore_bits |= EFER_LMA | EFER_LME;
591 /* SCE is meaningful only in long mode on Intel */
592 if (guest_efer & EFER_LMA)
593 ignore_bits &= ~(u64)EFER_SCE;
594#endif
595 if ((guest_efer & ~ignore_bits) == (host_efer & ~ignore_bits))
596 return;
597
598 vmx->host_state.guest_efer_loaded = 1;
599 guest_efer &= ~ignore_bits;
600 guest_efer |= host_efer & ignore_bits;
601 wrmsrl(MSR_EFER, guest_efer);
Rusty Russell8b9cf982007-07-30 16:31:43 +1000602 vmx->vcpu.stat.efer_reload++;
Eddie Dong2cc51562007-05-21 07:28:09 +0300603}
604
Avi Kivity51c6cf62007-08-29 03:48:05 +0300605static void reload_host_efer(struct vcpu_vmx *vmx)
606{
607 if (vmx->host_state.guest_efer_loaded) {
608 vmx->host_state.guest_efer_loaded = 0;
609 load_msrs(vmx->host_msrs + vmx->msr_offset_efer, 1);
610 }
611}
612
Avi Kivity04d2cc72007-09-10 18:10:54 +0300613static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300614{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300615 struct vcpu_vmx *vmx = to_vmx(vcpu);
616
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400617 if (vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300618 return;
619
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400620 vmx->host_state.loaded = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300621 /*
622 * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
623 * allow segment selectors with cpl > 0 or ti == 1.
624 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300625 vmx->host_state.ldt_sel = kvm_read_ldt();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200626 vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300627 vmx->host_state.fs_sel = kvm_read_fs();
Laurent Vivier152d3f22007-08-23 16:33:11 +0200628 if (!(vmx->host_state.fs_sel & 7)) {
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400629 vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200630 vmx->host_state.fs_reload_needed = 0;
631 } else {
Avi Kivity33ed6322007-05-02 16:54:03 +0300632 vmcs_write16(HOST_FS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200633 vmx->host_state.fs_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300634 }
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300635 vmx->host_state.gs_sel = kvm_read_gs();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400636 if (!(vmx->host_state.gs_sel & 7))
637 vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300638 else {
639 vmcs_write16(HOST_GS_SELECTOR, 0);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200640 vmx->host_state.gs_ldt_reload_needed = 1;
Avi Kivity33ed6322007-05-02 16:54:03 +0300641 }
642
643#ifdef CONFIG_X86_64
644 vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
645 vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
646#else
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400647 vmcs_writel(HOST_FS_BASE, segment_base(vmx->host_state.fs_sel));
648 vmcs_writel(HOST_GS_BASE, segment_base(vmx->host_state.gs_sel));
Avi Kivity33ed6322007-05-02 16:54:03 +0300649#endif
Avi Kivity707c0872007-05-02 17:33:43 +0300650
651#ifdef CONFIG_X86_64
Mike Dayd77c26f2007-10-08 09:02:08 -0400652 if (is_long_mode(&vmx->vcpu))
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400653 save_msrs(vmx->host_msrs +
654 vmx->msr_offset_kernel_gs_base, 1);
Mike Dayd77c26f2007-10-08 09:02:08 -0400655
Avi Kivity707c0872007-05-02 17:33:43 +0300656#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400657 load_msrs(vmx->guest_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300658 load_transition_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300659}
660
Avi Kivitya9b21b62008-06-24 11:48:49 +0300661static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300662{
Avi Kivity15ad7142007-07-11 18:17:21 +0300663 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300664
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400665 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300666 return;
667
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200668 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400669 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200670 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300671 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200672 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300673 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300674 /*
675 * If we have to reload gs, we must take care to
676 * preserve our gs base.
677 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300678 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300679 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300680#ifdef CONFIG_X86_64
681 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
682#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300683 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300684 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200685 reload_tss();
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400686 save_msrs(vmx->guest_msrs, vmx->save_nmsrs);
687 load_msrs(vmx->host_msrs, vmx->save_nmsrs);
Avi Kivity51c6cf62007-08-29 03:48:05 +0300688 reload_host_efer(vmx);
Avi Kivity33ed6322007-05-02 16:54:03 +0300689}
690
Avi Kivitya9b21b62008-06-24 11:48:49 +0300691static void vmx_load_host_state(struct vcpu_vmx *vmx)
692{
693 preempt_disable();
694 __vmx_load_host_state(vmx);
695 preempt_enable();
696}
697
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698/*
699 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
700 * vcpu mutex is already taken.
701 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300702static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400704 struct vcpu_vmx *vmx = to_vmx(vcpu);
705 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200706 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800707
Eddie Donga3d7f852007-09-03 16:15:12 +0300708 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000709 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300710 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300711 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300712 local_irq_disable();
713 list_add(&vmx->local_vcpus_link,
714 &per_cpu(vcpus_on_cpu, cpu));
715 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300716 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800717
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400718 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719 u8 error;
720
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400721 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300722 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
724 : "cc");
725 if (error)
726 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400727 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 }
729
730 if (vcpu->cpu != cpu) {
731 struct descriptor_table dt;
732 unsigned long sysenter_esp;
733
734 vcpu->cpu = cpu;
735 /*
736 * Linux uses per-cpu TSS and GDT, so set these when switching
737 * processors.
738 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300739 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
740 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
742
743 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
744 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300745
746 /*
747 * Make sure the time stamp counter is monotonous.
748 */
749 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200750 if (tsc_this < vcpu->arch.host_tsc) {
751 delta = vcpu->arch.host_tsc - tsc_this;
752 new_offset = vmcs_read64(TSC_OFFSET) + delta;
753 vmcs_write64(TSC_OFFSET, new_offset);
754 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756}
757
758static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
759{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300760 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761}
762
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300763static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
764{
765 if (vcpu->fpu_active)
766 return;
767 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000768 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800769 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000770 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300771 update_exception_bitmap(vcpu);
772}
773
774static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
775{
776 if (!vcpu->fpu_active)
777 return;
778 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000779 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300780 update_exception_bitmap(vcpu);
781}
782
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
784{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300785 unsigned long rflags;
786
787 rflags = vmcs_readl(GUEST_RFLAGS);
788 if (to_vmx(vcpu)->rmode.vm86_active)
789 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
790 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791}
792
793static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
794{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300795 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100796 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797 vmcs_writel(GUEST_RFLAGS, rflags);
798}
799
Glauber Costa2809f5d2009-05-12 16:21:05 -0400800static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
801{
802 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
803 int ret = 0;
804
805 if (interruptibility & GUEST_INTR_STATE_STI)
806 ret |= X86_SHADOW_INT_STI;
807 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
808 ret |= X86_SHADOW_INT_MOV_SS;
809
810 return ret & mask;
811}
812
813static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
814{
815 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
816 u32 interruptibility = interruptibility_old;
817
818 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
819
820 if (mask & X86_SHADOW_INT_MOV_SS)
821 interruptibility |= GUEST_INTR_STATE_MOV_SS;
822 if (mask & X86_SHADOW_INT_STI)
823 interruptibility |= GUEST_INTR_STATE_STI;
824
825 if ((interruptibility != interruptibility_old))
826 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
827}
828
Avi Kivity6aa8b732006-12-10 02:21:36 -0800829static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
830{
831 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800832
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300833 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300835 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800836
Glauber Costa2809f5d2009-05-12 16:21:05 -0400837 /* skipping an emulated instruction also counts */
838 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839}
840
Avi Kivity298101d2007-11-25 13:41:11 +0200841static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
842 bool has_error_code, u32 error_code)
843{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200844 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100845 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200846
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100847 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200848 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100849 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
850 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200851
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300852 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200853 vmx->rmode.irq.pending = true;
854 vmx->rmode.irq.vector = nr;
855 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300856 if (kvm_exception_is_soft(nr))
857 vmx->rmode.irq.rip +=
858 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100859 intr_info |= INTR_TYPE_SOFT_INTR;
860 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200861 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
862 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
863 return;
864 }
865
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300866 if (kvm_exception_is_soft(nr)) {
867 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
868 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100869 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
870 } else
871 intr_info |= INTR_TYPE_HARD_EXCEPTION;
872
873 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200874}
875
Avi Kivity6aa8b732006-12-10 02:21:36 -0800876/*
Eddie Donga75beee2007-05-17 18:55:15 +0300877 * Swap MSR entry in host/guest MSR entry array.
878 */
Gabriel C54e11fa2007-08-01 16:23:10 +0200879#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000880static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300881{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400882 struct kvm_msr_entry tmp;
883
884 tmp = vmx->guest_msrs[to];
885 vmx->guest_msrs[to] = vmx->guest_msrs[from];
886 vmx->guest_msrs[from] = tmp;
887 tmp = vmx->host_msrs[to];
888 vmx->host_msrs[to] = vmx->host_msrs[from];
889 vmx->host_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300890}
Gabriel C54e11fa2007-08-01 16:23:10 +0200891#endif
Eddie Donga75beee2007-05-17 18:55:15 +0300892
893/*
Avi Kivitye38aea32007-04-19 13:22:48 +0300894 * Set up the vmcs to automatically save and restore system
895 * msrs. Don't touch the 64-bit msrs if the guest is in legacy
896 * mode, as fiddling with msrs is very expensive.
897 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000898static void setup_msrs(struct vcpu_vmx *vmx)
Avi Kivitye38aea32007-04-19 13:22:48 +0300899{
Eddie Dong2cc51562007-05-21 07:28:09 +0300900 int save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200901 unsigned long *msr_bitmap;
Avi Kivitye38aea32007-04-19 13:22:48 +0300902
Avi Kivity33f9c502008-02-27 16:06:57 +0200903 vmx_load_host_state(vmx);
Eddie Donga75beee2007-05-17 18:55:15 +0300904 save_nmsrs = 0;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300905#ifdef CONFIG_X86_64
Rusty Russell8b9cf982007-07-30 16:31:43 +1000906 if (is_long_mode(&vmx->vcpu)) {
Eddie Dong2cc51562007-05-21 07:28:09 +0300907 int index;
908
Rusty Russell8b9cf982007-07-30 16:31:43 +1000909 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300910 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000911 move_msr_up(vmx, index, save_nmsrs++);
912 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300913 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000914 move_msr_up(vmx, index, save_nmsrs++);
915 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300916 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000917 move_msr_up(vmx, index, save_nmsrs++);
918 index = __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300919 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000920 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300921 /*
922 * MSR_K6_STAR is only needed on long mode guests, and only
923 * if efer.sce is enabled.
924 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000925 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800926 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000927 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300928 }
Eddie Donga75beee2007-05-17 18:55:15 +0300929#endif
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400930 vmx->save_nmsrs = save_nmsrs;
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300931
Eddie Donga75beee2007-05-17 18:55:15 +0300932#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400933 vmx->msr_offset_kernel_gs_base =
Rusty Russell8b9cf982007-07-30 16:31:43 +1000934 __find_msr_index(vmx, MSR_KERNEL_GS_BASE);
Eddie Donga75beee2007-05-17 18:55:15 +0300935#endif
Rusty Russell8b9cf982007-07-30 16:31:43 +1000936 vmx->msr_offset_efer = __find_msr_index(vmx, MSR_EFER);
Avi Kivity58972972009-02-24 22:26:47 +0200937
938 if (cpu_has_vmx_msr_bitmap()) {
939 if (is_long_mode(&vmx->vcpu))
940 msr_bitmap = vmx_msr_bitmap_longmode;
941 else
942 msr_bitmap = vmx_msr_bitmap_legacy;
943
944 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
945 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300946}
947
948/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800949 * reads and returns guest's timestamp counter "register"
950 * guest_tsc = host_tsc + tsc_offset -- 21.3
951 */
952static u64 guest_read_tsc(void)
953{
954 u64 host_tsc, tsc_offset;
955
956 rdtscll(host_tsc);
957 tsc_offset = vmcs_read64(TSC_OFFSET);
958 return host_tsc + tsc_offset;
959}
960
961/*
962 * writes 'guest_tsc' into guest's timestamp counter "register"
963 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
964 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100965static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800967 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
968}
969
Avi Kivity6aa8b732006-12-10 02:21:36 -0800970/*
971 * Reads an msr value (of 'msr_index') into 'pdata'.
972 * Returns 0 on success, non-0 otherwise.
973 * Assumes vcpu_load() was already called.
974 */
975static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
976{
977 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400978 struct kvm_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800979
980 if (!pdata) {
981 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
982 return -EINVAL;
983 }
984
985 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800986#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 case MSR_FS_BASE:
988 data = vmcs_readl(GUEST_FS_BASE);
989 break;
990 case MSR_GS_BASE:
991 data = vmcs_readl(GUEST_GS_BASE);
992 break;
993 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800994 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800995#endif
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530996 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997 data = guest_read_tsc();
998 break;
999 case MSR_IA32_SYSENTER_CS:
1000 data = vmcs_read32(GUEST_SYSENTER_CS);
1001 break;
1002 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001003 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 break;
1005 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001006 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001007 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001008 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001009 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001010 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001011 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001012 data = msr->data;
1013 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001015 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001016 }
1017
1018 *pdata = data;
1019 return 0;
1020}
1021
1022/*
1023 * Writes msr value into into the appropriate "register".
1024 * Returns 0 on success, non-0 otherwise.
1025 * Assumes vcpu_load() was already called.
1026 */
1027static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1028{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001029 struct vcpu_vmx *vmx = to_vmx(vcpu);
1030 struct kvm_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001031 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001032 int ret = 0;
1033
Avi Kivity6aa8b732006-12-10 02:21:36 -08001034 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001035 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001036 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001037 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001038 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001039#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040 case MSR_FS_BASE:
1041 vmcs_writel(GUEST_FS_BASE, data);
1042 break;
1043 case MSR_GS_BASE:
1044 vmcs_writel(GUEST_GS_BASE, data);
1045 break;
1046#endif
1047 case MSR_IA32_SYSENTER_CS:
1048 vmcs_write32(GUEST_SYSENTER_CS, data);
1049 break;
1050 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001051 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001052 break;
1053 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001054 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001055 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301056 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001057 rdtscll(host_tsc);
1058 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001059 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001060 case MSR_IA32_CR_PAT:
1061 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1062 vmcs_write64(GUEST_IA32_PAT, data);
1063 vcpu->arch.pat = data;
1064 break;
1065 }
1066 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001068 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001069 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001070 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001071 msr->data = data;
1072 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001073 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001074 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 }
1076
Eddie Dong2cc51562007-05-21 07:28:09 +03001077 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001078}
1079
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001080static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001081{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001082 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1083 switch (reg) {
1084 case VCPU_REGS_RSP:
1085 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1086 break;
1087 case VCPU_REGS_RIP:
1088 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1089 break;
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001090 case VCPU_EXREG_PDPTR:
1091 if (enable_ept)
1092 ept_save_pdptrs(vcpu);
1093 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001094 default:
1095 break;
1096 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001097}
1098
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001099static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001100{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001101 int old_debug = vcpu->guest_debug;
1102 unsigned long flags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001103
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001104 vcpu->guest_debug = dbg->control;
1105 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
1106 vcpu->guest_debug = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001108 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1109 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1110 else
1111 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1112
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001113 flags = vmcs_readl(GUEST_RFLAGS);
1114 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1115 flags |= X86_EFLAGS_TF | X86_EFLAGS_RF;
1116 else if (old_debug & KVM_GUESTDBG_SINGLESTEP)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001118 vmcs_writel(GUEST_RFLAGS, flags);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001120 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001121
1122 return 0;
1123}
1124
1125static __init int cpu_has_kvm_support(void)
1126{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001127 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128}
1129
1130static __init int vmx_disabled_by_bios(void)
1131{
1132 u64 msr;
1133
1134 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001135 return (msr & (FEATURE_CONTROL_LOCKED |
1136 FEATURE_CONTROL_VMXON_ENABLED))
1137 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001138 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001139}
1140
Alexander Graf10474ae2009-09-15 11:37:46 +02001141static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142{
1143 int cpu = raw_smp_processor_id();
1144 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1145 u64 old;
1146
Alexander Graf10474ae2009-09-15 11:37:46 +02001147 if (read_cr4() & X86_CR4_VMXE)
1148 return -EBUSY;
1149
Avi Kivity543e4242008-05-13 16:22:47 +03001150 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001151 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001152 if ((old & (FEATURE_CONTROL_LOCKED |
1153 FEATURE_CONTROL_VMXON_ENABLED))
1154 != (FEATURE_CONTROL_LOCKED |
1155 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001156 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001157 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001158 FEATURE_CONTROL_LOCKED |
1159 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001160 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001161 asm volatile (ASM_VMX_VMXON_RAX
1162 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001164
1165 ept_sync_global();
1166
1167 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168}
1169
Avi Kivity543e4242008-05-13 16:22:47 +03001170static void vmclear_local_vcpus(void)
1171{
1172 int cpu = raw_smp_processor_id();
1173 struct vcpu_vmx *vmx, *n;
1174
1175 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1176 local_vcpus_link)
1177 __vcpu_clear(vmx);
1178}
1179
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001180
1181/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1182 * tricks.
1183 */
1184static void kvm_cpu_vmxoff(void)
1185{
1186 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1187 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1188}
1189
Avi Kivity6aa8b732006-12-10 02:21:36 -08001190static void hardware_disable(void *garbage)
1191{
Avi Kivity543e4242008-05-13 16:22:47 +03001192 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001193 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194}
1195
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001196static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001197 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198{
1199 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001200 u32 ctl = ctl_min | ctl_opt;
1201
1202 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1203
1204 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1205 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1206
1207 /* Ensure minimum (required) set of control bits are supported. */
1208 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001209 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001210
1211 *result = ctl;
1212 return 0;
1213}
1214
Yang, Sheng002c7f72007-07-31 14:23:01 +03001215static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001216{
1217 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001218 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001219 u32 _pin_based_exec_control = 0;
1220 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001221 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001222 u32 _vmexit_control = 0;
1223 u32 _vmentry_control = 0;
1224
1225 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001226 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001227 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1228 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001229 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001230
1231 min = CPU_BASED_HLT_EXITING |
1232#ifdef CONFIG_X86_64
1233 CPU_BASED_CR8_LOAD_EXITING |
1234 CPU_BASED_CR8_STORE_EXITING |
1235#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001236 CPU_BASED_CR3_LOAD_EXITING |
1237 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001238 CPU_BASED_USE_IO_BITMAPS |
1239 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001240 CPU_BASED_USE_TSC_OFFSETING |
1241 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001242 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001243 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001244 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001245 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1246 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001247 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001248#ifdef CONFIG_X86_64
1249 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1250 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1251 ~CPU_BASED_CR8_STORE_EXITING;
1252#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001253 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001254 min2 = 0;
1255 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001256 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001257 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001258 SECONDARY_EXEC_ENABLE_EPT |
1259 SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yangd56f5462008-04-25 10:13:16 +08001260 if (adjust_vmx_controls(min2, opt2,
1261 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001262 &_cpu_based_2nd_exec_control) < 0)
1263 return -EIO;
1264 }
1265#ifndef CONFIG_X86_64
1266 if (!(_cpu_based_2nd_exec_control &
1267 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1268 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1269#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001270 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001271 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1272 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001273 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1274 CPU_BASED_CR3_STORE_EXITING |
1275 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001276 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1277 vmx_capability.ept, vmx_capability.vpid);
1278 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001279
1280 min = 0;
1281#ifdef CONFIG_X86_64
1282 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1283#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001284 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001285 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1286 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001287 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001288
Sheng Yang468d4722008-10-09 16:01:55 +08001289 min = 0;
1290 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001291 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1292 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001293 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001295 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001296
1297 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1298 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001299 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001300
1301#ifdef CONFIG_X86_64
1302 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1303 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001304 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001305#endif
1306
1307 /* Require Write-Back (WB) memory type for VMCS accesses. */
1308 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001309 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001310
Yang, Sheng002c7f72007-07-31 14:23:01 +03001311 vmcs_conf->size = vmx_msr_high & 0x1fff;
1312 vmcs_conf->order = get_order(vmcs_config.size);
1313 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001314
Yang, Sheng002c7f72007-07-31 14:23:01 +03001315 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1316 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001317 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001318 vmcs_conf->vmexit_ctrl = _vmexit_control;
1319 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001320
1321 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001322}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001323
1324static struct vmcs *alloc_vmcs_cpu(int cpu)
1325{
1326 int node = cpu_to_node(cpu);
1327 struct page *pages;
1328 struct vmcs *vmcs;
1329
Mel Gorman6484eb32009-06-16 15:31:54 -07001330 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331 if (!pages)
1332 return NULL;
1333 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001334 memset(vmcs, 0, vmcs_config.size);
1335 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 return vmcs;
1337}
1338
1339static struct vmcs *alloc_vmcs(void)
1340{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001341 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001342}
1343
1344static void free_vmcs(struct vmcs *vmcs)
1345{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001346 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347}
1348
Sam Ravnborg39959582007-06-01 00:47:13 -07001349static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350{
1351 int cpu;
1352
Zachary Amsden3230bb42009-09-29 11:38:37 -10001353 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001354 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001355 per_cpu(vmxarea, cpu) = NULL;
1356 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001357}
1358
Avi Kivity6aa8b732006-12-10 02:21:36 -08001359static __init int alloc_kvm_area(void)
1360{
1361 int cpu;
1362
Zachary Amsden3230bb42009-09-29 11:38:37 -10001363 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364 struct vmcs *vmcs;
1365
1366 vmcs = alloc_vmcs_cpu(cpu);
1367 if (!vmcs) {
1368 free_kvm_area();
1369 return -ENOMEM;
1370 }
1371
1372 per_cpu(vmxarea, cpu) = vmcs;
1373 }
1374 return 0;
1375}
1376
1377static __init int hardware_setup(void)
1378{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001379 if (setup_vmcs_config(&vmcs_config) < 0)
1380 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001381
1382 if (boot_cpu_has(X86_FEATURE_NX))
1383 kvm_enable_efer_bits(EFER_NX);
1384
Sheng Yang93ba03c2009-04-01 15:52:32 +08001385 if (!cpu_has_vmx_vpid())
1386 enable_vpid = 0;
1387
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001388 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001389 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001390 enable_unrestricted_guest = 0;
1391 }
1392
1393 if (!cpu_has_vmx_unrestricted_guest())
1394 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001395
1396 if (!cpu_has_vmx_flexpriority())
1397 flexpriority_enabled = 0;
1398
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001399 if (!cpu_has_vmx_tpr_shadow())
1400 kvm_x86_ops->update_cr8_intercept = NULL;
1401
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001402 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1403 kvm_disable_largepages();
1404
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405 return alloc_kvm_area();
1406}
1407
1408static __exit void hardware_unsetup(void)
1409{
1410 free_kvm_area();
1411}
1412
Avi Kivity6aa8b732006-12-10 02:21:36 -08001413static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1414{
1415 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1416
Avi Kivity6af11b92007-03-19 13:18:10 +02001417 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001418 vmcs_write16(sf->selector, save->selector);
1419 vmcs_writel(sf->base, save->base);
1420 vmcs_write32(sf->limit, save->limit);
1421 vmcs_write32(sf->ar_bytes, save->ar);
1422 } else {
1423 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1424 << AR_DPL_SHIFT;
1425 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1426 }
1427}
1428
1429static void enter_pmode(struct kvm_vcpu *vcpu)
1430{
1431 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001432 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001433
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001434 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001435 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001437 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1438 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1439 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
1441 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001442 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001443 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001444 vmcs_writel(GUEST_RFLAGS, flags);
1445
Rusty Russell66aee912007-07-17 23:34:16 +10001446 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1447 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001448
1449 update_exception_bitmap(vcpu);
1450
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001451 if (emulate_invalid_guest_state)
1452 return;
1453
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001454 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1455 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1456 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1457 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001458
1459 vmcs_write16(GUEST_SS_SELECTOR, 0);
1460 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1461
1462 vmcs_write16(GUEST_CS_SELECTOR,
1463 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1464 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1465}
1466
Mike Dayd77c26f2007-10-08 09:02:08 -04001467static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001469 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001470 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1471 kvm->memslots[0].npages - 3;
1472 return base_gfn << PAGE_SHIFT;
1473 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001474 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001475}
1476
1477static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1478{
1479 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1480
1481 save->selector = vmcs_read16(sf->selector);
1482 save->base = vmcs_readl(sf->base);
1483 save->limit = vmcs_read32(sf->limit);
1484 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001485 vmcs_write16(sf->selector, save->base >> 4);
1486 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487 vmcs_write32(sf->limit, 0xffff);
1488 vmcs_write32(sf->ar_bytes, 0xf3);
1489}
1490
1491static void enter_rmode(struct kvm_vcpu *vcpu)
1492{
1493 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001494 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001496 if (enable_unrestricted_guest)
1497 return;
1498
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001499 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001500 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001502 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001503 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1504
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001505 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1507
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001508 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1510
1511 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001512 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001513 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001514
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001515 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516
1517 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001518 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001519 update_exception_bitmap(vcpu);
1520
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001521 if (emulate_invalid_guest_state)
1522 goto continue_rmode;
1523
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1525 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1526 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1527
1528 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001529 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001530 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1531 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001532 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1533
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001534 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1535 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1536 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1537 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001538
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001539continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001540 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001541 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542}
1543
Amit Shah401d10d2009-02-20 22:53:37 +05301544static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1545{
1546 struct vcpu_vmx *vmx = to_vmx(vcpu);
1547 struct kvm_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1548
1549 vcpu->arch.shadow_efer = efer;
1550 if (!msr)
1551 return;
1552 if (efer & EFER_LMA) {
1553 vmcs_write32(VM_ENTRY_CONTROLS,
1554 vmcs_read32(VM_ENTRY_CONTROLS) |
1555 VM_ENTRY_IA32E_MODE);
1556 msr->data = efer;
1557 } else {
1558 vmcs_write32(VM_ENTRY_CONTROLS,
1559 vmcs_read32(VM_ENTRY_CONTROLS) &
1560 ~VM_ENTRY_IA32E_MODE);
1561
1562 msr->data = efer & ~EFER_LME;
1563 }
1564 setup_msrs(vmx);
1565}
1566
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001567#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001568
1569static void enter_lmode(struct kvm_vcpu *vcpu)
1570{
1571 u32 guest_tr_ar;
1572
1573 guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
1574 if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) {
1575 printk(KERN_DEBUG "%s: tss fixup for long mode. \n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08001576 __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001577 vmcs_write32(GUEST_TR_AR_BYTES,
1578 (guest_tr_ar & ~AR_TYPE_MASK)
1579 | AR_TYPE_BUSY_64_TSS);
1580 }
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001581 vcpu->arch.shadow_efer |= EFER_LMA;
Amit Shah401d10d2009-02-20 22:53:37 +05301582 vmx_set_efer(vcpu, vcpu->arch.shadow_efer);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583}
1584
1585static void exit_lmode(struct kvm_vcpu *vcpu)
1586{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001587 vcpu->arch.shadow_efer &= ~EFER_LMA;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001588
1589 vmcs_write32(VM_ENTRY_CONTROLS,
1590 vmcs_read32(VM_ENTRY_CONTROLS)
Li, Xin B1e4e6e02007-08-01 21:49:10 +03001591 & ~VM_ENTRY_IA32E_MODE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001592}
1593
1594#endif
1595
Sheng Yang2384d2b2008-01-17 15:14:33 +08001596static void vmx_flush_tlb(struct kvm_vcpu *vcpu)
1597{
1598 vpid_sync_vcpu_all(to_vmx(vcpu));
Avi Kivity089d0342009-03-23 18:26:32 +02001599 if (enable_ept)
Sheng Yang4e1096d2008-07-06 19:16:51 +08001600 ept_sync_context(construct_eptp(vcpu->arch.mmu.root_hpa));
Sheng Yang2384d2b2008-01-17 15:14:33 +08001601}
1602
Anthony Liguori25c4c272007-04-27 09:29:21 +03001603static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001604{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001605 vcpu->arch.cr4 &= KVM_GUEST_CR4_MASK;
1606 vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & ~KVM_GUEST_CR4_MASK;
Avi Kivity399badf2007-01-05 16:36:38 -08001607}
1608
Sheng Yang14394422008-04-28 12:24:45 +08001609static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
1610{
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001611 if (!test_bit(VCPU_EXREG_PDPTR,
1612 (unsigned long *)&vcpu->arch.regs_dirty))
1613 return;
1614
Sheng Yang14394422008-04-28 12:24:45 +08001615 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
Sheng Yang14394422008-04-28 12:24:45 +08001616 vmcs_write64(GUEST_PDPTR0, vcpu->arch.pdptrs[0]);
1617 vmcs_write64(GUEST_PDPTR1, vcpu->arch.pdptrs[1]);
1618 vmcs_write64(GUEST_PDPTR2, vcpu->arch.pdptrs[2]);
1619 vmcs_write64(GUEST_PDPTR3, vcpu->arch.pdptrs[3]);
1620 }
1621}
1622
Avi Kivity8f5d5492009-05-31 18:41:29 +03001623static void ept_save_pdptrs(struct kvm_vcpu *vcpu)
1624{
1625 if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) {
1626 vcpu->arch.pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
1627 vcpu->arch.pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
1628 vcpu->arch.pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
1629 vcpu->arch.pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
1630 }
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03001631
1632 __set_bit(VCPU_EXREG_PDPTR,
1633 (unsigned long *)&vcpu->arch.regs_avail);
1634 __set_bit(VCPU_EXREG_PDPTR,
1635 (unsigned long *)&vcpu->arch.regs_dirty);
Avi Kivity8f5d5492009-05-31 18:41:29 +03001636}
1637
Sheng Yang14394422008-04-28 12:24:45 +08001638static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
1639
1640static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
1641 unsigned long cr0,
1642 struct kvm_vcpu *vcpu)
1643{
1644 if (!(cr0 & X86_CR0_PG)) {
1645 /* From paging/starting to nonpaging */
1646 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001647 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) |
Sheng Yang14394422008-04-28 12:24:45 +08001648 (CPU_BASED_CR3_LOAD_EXITING |
1649 CPU_BASED_CR3_STORE_EXITING));
1650 vcpu->arch.cr0 = cr0;
1651 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001652 } else if (!is_paging(vcpu)) {
1653 /* From nonpaging to paging */
1654 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,
Sheng Yang65267ea2008-06-18 14:43:38 +08001655 vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) &
Sheng Yang14394422008-04-28 12:24:45 +08001656 ~(CPU_BASED_CR3_LOAD_EXITING |
1657 CPU_BASED_CR3_STORE_EXITING));
1658 vcpu->arch.cr0 = cr0;
1659 vmx_set_cr4(vcpu, vcpu->arch.cr4);
Sheng Yang14394422008-04-28 12:24:45 +08001660 }
Sheng Yang95eb84a2009-08-19 09:52:18 +08001661
1662 if (!(cr0 & X86_CR0_WP))
1663 *hw_cr0 &= ~X86_CR0_WP;
Sheng Yang14394422008-04-28 12:24:45 +08001664}
1665
1666static void ept_update_paging_mode_cr4(unsigned long *hw_cr4,
1667 struct kvm_vcpu *vcpu)
1668{
1669 if (!is_paging(vcpu)) {
1670 *hw_cr4 &= ~X86_CR4_PAE;
1671 *hw_cr4 |= X86_CR4_PSE;
1672 } else if (!(vcpu->arch.cr4 & X86_CR4_PAE))
1673 *hw_cr4 &= ~X86_CR4_PAE;
1674}
1675
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1677{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001678 struct vcpu_vmx *vmx = to_vmx(vcpu);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001679 unsigned long hw_cr0;
1680
1681 if (enable_unrestricted_guest)
1682 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST)
1683 | KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
1684 else
1685 hw_cr0 = (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON;
Sheng Yang14394422008-04-28 12:24:45 +08001686
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001687 vmx_fpu_deactivate(vcpu);
1688
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001689 if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690 enter_pmode(vcpu);
1691
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001692 if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693 enter_rmode(vcpu);
1694
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001695#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001696 if (vcpu->arch.shadow_efer & EFER_LME) {
Rusty Russell707d92f2007-07-17 23:19:08 +10001697 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698 enter_lmode(vcpu);
Rusty Russell707d92f2007-07-17 23:19:08 +10001699 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700 exit_lmode(vcpu);
1701 }
1702#endif
1703
Avi Kivity089d0342009-03-23 18:26:32 +02001704 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001705 ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
1706
Avi Kivity6aa8b732006-12-10 02:21:36 -08001707 vmcs_writel(CR0_READ_SHADOW, cr0);
Sheng Yang14394422008-04-28 12:24:45 +08001708 vmcs_writel(GUEST_CR0, hw_cr0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001709 vcpu->arch.cr0 = cr0;
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001710
Rusty Russell707d92f2007-07-17 23:19:08 +10001711 if (!(cr0 & X86_CR0_TS) || !(cr0 & X86_CR0_PE))
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001712 vmx_fpu_activate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001713}
1714
Sheng Yang14394422008-04-28 12:24:45 +08001715static u64 construct_eptp(unsigned long root_hpa)
1716{
1717 u64 eptp;
1718
1719 /* TODO write the value reading from MSR */
1720 eptp = VMX_EPT_DEFAULT_MT |
1721 VMX_EPT_DEFAULT_GAW << VMX_EPT_GAW_EPTP_SHIFT;
1722 eptp |= (root_hpa & PAGE_MASK);
1723
1724 return eptp;
1725}
1726
Avi Kivity6aa8b732006-12-10 02:21:36 -08001727static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1728{
Sheng Yang14394422008-04-28 12:24:45 +08001729 unsigned long guest_cr3;
1730 u64 eptp;
1731
1732 guest_cr3 = cr3;
Avi Kivity089d0342009-03-23 18:26:32 +02001733 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08001734 eptp = construct_eptp(cr3);
1735 vmcs_write64(EPT_POINTER, eptp);
Sheng Yang14394422008-04-28 12:24:45 +08001736 guest_cr3 = is_paging(vcpu) ? vcpu->arch.cr3 :
Sheng Yangb927a3c2009-07-21 10:42:48 +08001737 vcpu->kvm->arch.ept_identity_map_addr;
Sheng Yang14394422008-04-28 12:24:45 +08001738 }
1739
Sheng Yang2384d2b2008-01-17 15:14:33 +08001740 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001741 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001742 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001743 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001744}
1745
1746static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1747{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001748 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001749 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1750
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001751 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001752 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001753 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1754
1755 vmcs_writel(CR4_READ_SHADOW, cr4);
1756 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001757}
1758
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1760{
1761 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1762
1763 return vmcs_readl(sf->base);
1764}
1765
1766static void vmx_get_segment(struct kvm_vcpu *vcpu,
1767 struct kvm_segment *var, int seg)
1768{
1769 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1770 u32 ar;
1771
1772 var->base = vmcs_readl(sf->base);
1773 var->limit = vmcs_read32(sf->limit);
1774 var->selector = vmcs_read16(sf->selector);
1775 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001776 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001777 ar = 0;
1778 var->type = ar & 15;
1779 var->s = (ar >> 4) & 1;
1780 var->dpl = (ar >> 5) & 3;
1781 var->present = (ar >> 7) & 1;
1782 var->avl = (ar >> 12) & 1;
1783 var->l = (ar >> 13) & 1;
1784 var->db = (ar >> 14) & 1;
1785 var->g = (ar >> 15) & 1;
1786 var->unusable = (ar >> 16) & 1;
1787}
1788
Izik Eidus2e4d2652008-03-24 19:38:34 +02001789static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1790{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001791 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1792 return 0;
1793
1794 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1795 return 3;
1796
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001797 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001798}
1799
Avi Kivity653e3102007-05-07 10:55:37 +03001800static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001801{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802 u32 ar;
1803
Avi Kivity653e3102007-05-07 10:55:37 +03001804 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805 ar = 1 << 16;
1806 else {
1807 ar = var->type & 15;
1808 ar |= (var->s & 1) << 4;
1809 ar |= (var->dpl & 3) << 5;
1810 ar |= (var->present & 1) << 7;
1811 ar |= (var->avl & 1) << 12;
1812 ar |= (var->l & 1) << 13;
1813 ar |= (var->db & 1) << 14;
1814 ar |= (var->g & 1) << 15;
1815 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001816 if (ar == 0) /* a 0 value means unusable */
1817 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001818
1819 return ar;
1820}
1821
1822static void vmx_set_segment(struct kvm_vcpu *vcpu,
1823 struct kvm_segment *var, int seg)
1824{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001825 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001826 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1827 u32 ar;
1828
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001829 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1830 vmx->rmode.tr.selector = var->selector;
1831 vmx->rmode.tr.base = var->base;
1832 vmx->rmode.tr.limit = var->limit;
1833 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001834 return;
1835 }
1836 vmcs_writel(sf->base, var->base);
1837 vmcs_write32(sf->limit, var->limit);
1838 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001839 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001840 /*
1841 * Hack real-mode segments into vm86 compatibility.
1842 */
1843 if (var->base == 0xffff0000 && var->selector == 0xf000)
1844 vmcs_writel(sf->base, 0xf0000);
1845 ar = 0xf3;
1846 } else
1847 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001848
1849 /*
1850 * Fix the "Accessed" bit in AR field of segment registers for older
1851 * qemu binaries.
1852 * IA32 arch specifies that at the time of processor reset the
1853 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1854 * is setting it to 0 in the usedland code. This causes invalid guest
1855 * state vmexit when "unrestricted guest" mode is turned on.
1856 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1857 * tree. Newer qemu binaries with that qemu fix would not need this
1858 * kvm hack.
1859 */
1860 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1861 ar |= 0x1; /* Accessed */
1862
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863 vmcs_write32(sf->ar_bytes, ar);
1864}
1865
Avi Kivity6aa8b732006-12-10 02:21:36 -08001866static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1867{
1868 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1869
1870 *db = (ar >> 14) & 1;
1871 *l = (ar >> 13) & 1;
1872}
1873
1874static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1875{
1876 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1877 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1878}
1879
1880static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1881{
1882 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1883 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1884}
1885
1886static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1887{
1888 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1889 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1890}
1891
1892static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1893{
1894 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1895 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1896}
1897
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001898static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1899{
1900 struct kvm_segment var;
1901 u32 ar;
1902
1903 vmx_get_segment(vcpu, &var, seg);
1904 ar = vmx_segment_access_rights(&var);
1905
1906 if (var.base != (var.selector << 4))
1907 return false;
1908 if (var.limit != 0xffff)
1909 return false;
1910 if (ar != 0xf3)
1911 return false;
1912
1913 return true;
1914}
1915
1916static bool code_segment_valid(struct kvm_vcpu *vcpu)
1917{
1918 struct kvm_segment cs;
1919 unsigned int cs_rpl;
1920
1921 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1922 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1923
Avi Kivity1872a3f2009-01-04 23:26:52 +02001924 if (cs.unusable)
1925 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001926 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1927 return false;
1928 if (!cs.s)
1929 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001930 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001931 if (cs.dpl > cs_rpl)
1932 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001933 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001934 if (cs.dpl != cs_rpl)
1935 return false;
1936 }
1937 if (!cs.present)
1938 return false;
1939
1940 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1941 return true;
1942}
1943
1944static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1945{
1946 struct kvm_segment ss;
1947 unsigned int ss_rpl;
1948
1949 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1950 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1951
Avi Kivity1872a3f2009-01-04 23:26:52 +02001952 if (ss.unusable)
1953 return true;
1954 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001955 return false;
1956 if (!ss.s)
1957 return false;
1958 if (ss.dpl != ss_rpl) /* DPL != RPL */
1959 return false;
1960 if (!ss.present)
1961 return false;
1962
1963 return true;
1964}
1965
1966static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1967{
1968 struct kvm_segment var;
1969 unsigned int rpl;
1970
1971 vmx_get_segment(vcpu, &var, seg);
1972 rpl = var.selector & SELECTOR_RPL_MASK;
1973
Avi Kivity1872a3f2009-01-04 23:26:52 +02001974 if (var.unusable)
1975 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001976 if (!var.s)
1977 return false;
1978 if (!var.present)
1979 return false;
1980 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1981 if (var.dpl < rpl) /* DPL < RPL */
1982 return false;
1983 }
1984
1985 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1986 * rights flags
1987 */
1988 return true;
1989}
1990
1991static bool tr_valid(struct kvm_vcpu *vcpu)
1992{
1993 struct kvm_segment tr;
1994
1995 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1996
Avi Kivity1872a3f2009-01-04 23:26:52 +02001997 if (tr.unusable)
1998 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001999 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2000 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002001 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002002 return false;
2003 if (!tr.present)
2004 return false;
2005
2006 return true;
2007}
2008
2009static bool ldtr_valid(struct kvm_vcpu *vcpu)
2010{
2011 struct kvm_segment ldtr;
2012
2013 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2014
Avi Kivity1872a3f2009-01-04 23:26:52 +02002015 if (ldtr.unusable)
2016 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002017 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2018 return false;
2019 if (ldtr.type != 2)
2020 return false;
2021 if (!ldtr.present)
2022 return false;
2023
2024 return true;
2025}
2026
2027static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2028{
2029 struct kvm_segment cs, ss;
2030
2031 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2032 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2033
2034 return ((cs.selector & SELECTOR_RPL_MASK) ==
2035 (ss.selector & SELECTOR_RPL_MASK));
2036}
2037
2038/*
2039 * Check if guest state is valid. Returns true if valid, false if
2040 * not.
2041 * We assume that registers are always usable
2042 */
2043static bool guest_state_valid(struct kvm_vcpu *vcpu)
2044{
2045 /* real mode guest state checks */
2046 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2047 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2048 return false;
2049 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2050 return false;
2051 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2052 return false;
2053 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2054 return false;
2055 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2056 return false;
2057 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2058 return false;
2059 } else {
2060 /* protected mode guest state checks */
2061 if (!cs_ss_rpl_check(vcpu))
2062 return false;
2063 if (!code_segment_valid(vcpu))
2064 return false;
2065 if (!stack_segment_valid(vcpu))
2066 return false;
2067 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2068 return false;
2069 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2070 return false;
2071 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2072 return false;
2073 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2074 return false;
2075 if (!tr_valid(vcpu))
2076 return false;
2077 if (!ldtr_valid(vcpu))
2078 return false;
2079 }
2080 /* TODO:
2081 * - Add checks on RIP
2082 * - Add checks on RFLAGS
2083 */
2084
2085 return true;
2086}
2087
Mike Dayd77c26f2007-10-08 09:02:08 -04002088static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002089{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002091 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002092 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002093 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094
Izik Eidus195aefd2007-10-01 22:14:18 +02002095 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2096 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002097 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002098 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002099 r = kvm_write_guest_page(kvm, fn++, &data,
2100 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002101 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002102 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002103 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2104 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002105 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002106 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2107 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002108 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002109 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002110 r = kvm_write_guest_page(kvm, fn, &data,
2111 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2112 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002113 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002114 goto out;
2115
2116 ret = 1;
2117out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002118 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002119}
2120
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002121static int init_rmode_identity_map(struct kvm *kvm)
2122{
2123 int i, r, ret;
2124 pfn_t identity_map_pfn;
2125 u32 tmp;
2126
Avi Kivity089d0342009-03-23 18:26:32 +02002127 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002128 return 1;
2129 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2130 printk(KERN_ERR "EPT: identity-mapping pagetable "
2131 "haven't been allocated!\n");
2132 return 0;
2133 }
2134 if (likely(kvm->arch.ept_identity_pagetable_done))
2135 return 1;
2136 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002137 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002138 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2139 if (r < 0)
2140 goto out;
2141 /* Set up identity-mapping pagetable for EPT in real mode */
2142 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2143 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2144 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2145 r = kvm_write_guest_page(kvm, identity_map_pfn,
2146 &tmp, i * sizeof(tmp), sizeof(tmp));
2147 if (r < 0)
2148 goto out;
2149 }
2150 kvm->arch.ept_identity_pagetable_done = true;
2151 ret = 1;
2152out:
2153 return ret;
2154}
2155
Avi Kivity6aa8b732006-12-10 02:21:36 -08002156static void seg_setup(int seg)
2157{
2158 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002159 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002160
2161 vmcs_write16(sf->selector, 0);
2162 vmcs_writel(sf->base, 0);
2163 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002164 if (enable_unrestricted_guest) {
2165 ar = 0x93;
2166 if (seg == VCPU_SREG_CS)
2167 ar |= 0x08; /* code segment */
2168 } else
2169 ar = 0xf3;
2170
2171 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172}
2173
Sheng Yangf78e0e22007-10-29 09:40:42 +08002174static int alloc_apic_access_page(struct kvm *kvm)
2175{
2176 struct kvm_userspace_memory_region kvm_userspace_mem;
2177 int r = 0;
2178
Izik Eidus72dc67a2008-02-10 18:04:15 +02002179 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002180 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002181 goto out;
2182 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2183 kvm_userspace_mem.flags = 0;
2184 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2185 kvm_userspace_mem.memory_size = PAGE_SIZE;
2186 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2187 if (r)
2188 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002189
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002190 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002191out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002192 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002193 return r;
2194}
2195
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002196static int alloc_identity_pagetable(struct kvm *kvm)
2197{
2198 struct kvm_userspace_memory_region kvm_userspace_mem;
2199 int r = 0;
2200
2201 down_write(&kvm->slots_lock);
2202 if (kvm->arch.ept_identity_pagetable)
2203 goto out;
2204 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2205 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002206 kvm_userspace_mem.guest_phys_addr =
2207 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002208 kvm_userspace_mem.memory_size = PAGE_SIZE;
2209 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2210 if (r)
2211 goto out;
2212
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002213 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002214 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002215out:
2216 up_write(&kvm->slots_lock);
2217 return r;
2218}
2219
Sheng Yang2384d2b2008-01-17 15:14:33 +08002220static void allocate_vpid(struct vcpu_vmx *vmx)
2221{
2222 int vpid;
2223
2224 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002225 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002226 return;
2227 spin_lock(&vmx_vpid_lock);
2228 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2229 if (vpid < VMX_NR_VPIDS) {
2230 vmx->vpid = vpid;
2231 __set_bit(vpid, vmx_vpid_bitmap);
2232 }
2233 spin_unlock(&vmx_vpid_lock);
2234}
2235
Avi Kivity58972972009-02-24 22:26:47 +02002236static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002237{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002238 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002239
2240 if (!cpu_has_vmx_msr_bitmap())
2241 return;
2242
2243 /*
2244 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2245 * have the write-low and read-high bitmap offsets the wrong way round.
2246 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2247 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002248 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002249 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2250 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002251 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2252 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002253 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2254 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002255 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002256}
2257
Avi Kivity58972972009-02-24 22:26:47 +02002258static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2259{
2260 if (!longmode_only)
2261 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2262 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2263}
2264
Avi Kivity6aa8b732006-12-10 02:21:36 -08002265/*
2266 * Sets up the vmcs for emulated real mode.
2267 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002268static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002269{
Sheng Yang468d4722008-10-09 16:01:55 +08002270 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002271 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002272 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002273 unsigned long a;
2274 struct descriptor_table dt;
2275 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002276 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002277 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002278
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002280 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2281 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002282
Sheng Yang25c5f222008-03-28 13:18:56 +08002283 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002284 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002285
Avi Kivity6aa8b732006-12-10 02:21:36 -08002286 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2287
Avi Kivity6aa8b732006-12-10 02:21:36 -08002288 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002289 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2290 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002291
2292 exec_control = vmcs_config.cpu_based_exec_ctrl;
2293 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2294 exec_control &= ~CPU_BASED_TPR_SHADOW;
2295#ifdef CONFIG_X86_64
2296 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2297 CPU_BASED_CR8_LOAD_EXITING;
2298#endif
2299 }
Avi Kivity089d0342009-03-23 18:26:32 +02002300 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002301 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002302 CPU_BASED_CR3_LOAD_EXITING |
2303 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002304 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002305
Sheng Yang83ff3b92007-11-21 14:33:25 +08002306 if (cpu_has_secondary_exec_ctrls()) {
2307 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2308 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2309 exec_control &=
2310 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002311 if (vmx->vpid == 0)
2312 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Avi Kivity089d0342009-03-23 18:26:32 +02002313 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002314 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002315 if (!enable_unrestricted_guest)
2316 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002317 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2318 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002319
Avi Kivityc7addb92007-09-16 18:58:32 +02002320 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2321 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002322 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2323
2324 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2325 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2326 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2327
2328 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2329 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2330 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002331 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2332 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002333 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002334#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002335 rdmsrl(MSR_FS_BASE, a);
2336 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2337 rdmsrl(MSR_GS_BASE, a);
2338 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2339#else
2340 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2341 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2342#endif
2343
2344 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2345
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002346 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002347 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2348
Mike Dayd77c26f2007-10-08 09:02:08 -04002349 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002350 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002351 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2352 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2353 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002354
2355 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2356 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2357 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2358 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2359 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2360 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2361
Sheng Yang468d4722008-10-09 16:01:55 +08002362 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2363 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2364 host_pat = msr_low | ((u64) msr_high << 32);
2365 vmcs_write64(HOST_IA32_PAT, host_pat);
2366 }
2367 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2368 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2369 host_pat = msr_low | ((u64) msr_high << 32);
2370 /* Write the default value follow host pat */
2371 vmcs_write64(GUEST_IA32_PAT, host_pat);
2372 /* Keep arch.pat sync with GUEST_IA32_PAT */
2373 vmx->vcpu.arch.pat = host_pat;
2374 }
2375
Avi Kivity6aa8b732006-12-10 02:21:36 -08002376 for (i = 0; i < NR_VMX_MSR; ++i) {
2377 u32 index = vmx_msr_index[i];
2378 u32 data_low, data_high;
2379 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002380 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002381
2382 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2383 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002384 if (wrmsr_safe(index, data_low, data_high) < 0)
2385 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 data = data_low | ((u64)data_high << 32);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002387 vmx->host_msrs[j].index = index;
2388 vmx->host_msrs[j].reserved = 0;
2389 vmx->host_msrs[j].data = data;
2390 vmx->guest_msrs[j] = vmx->host_msrs[j];
2391 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002392 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002394 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395
2396 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002397 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2398
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002399 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2400 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2401
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002402 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2403 rdtscll(tsc_this);
2404 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2405 tsc_base = tsc_this;
2406
2407 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002408
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002409 return 0;
2410}
2411
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002412static int init_rmode(struct kvm *kvm)
2413{
2414 if (!init_rmode_tss(kvm))
2415 return 0;
2416 if (!init_rmode_identity_map(kvm))
2417 return 0;
2418 return 1;
2419}
2420
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002421static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2422{
2423 struct vcpu_vmx *vmx = to_vmx(vcpu);
2424 u64 msr;
2425 int ret;
2426
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002427 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002428 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002429 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002430 ret = -ENOMEM;
2431 goto out;
2432 }
2433
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002434 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002435
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002436 vmx->soft_vnmi_blocked = 0;
2437
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002438 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002439 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002440 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002441 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002442 msr |= MSR_IA32_APICBASE_BSP;
2443 kvm_set_apic_base(&vmx->vcpu, msr);
2444
2445 fx_init(&vmx->vcpu);
2446
Avi Kivity5706be02008-08-20 15:07:31 +03002447 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002448 /*
2449 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2450 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2451 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002452 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002453 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2454 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2455 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002456 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2457 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002458 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002459
2460 seg_setup(VCPU_SREG_DS);
2461 seg_setup(VCPU_SREG_ES);
2462 seg_setup(VCPU_SREG_FS);
2463 seg_setup(VCPU_SREG_GS);
2464 seg_setup(VCPU_SREG_SS);
2465
2466 vmcs_write16(GUEST_TR_SELECTOR, 0);
2467 vmcs_writel(GUEST_TR_BASE, 0);
2468 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2469 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2470
2471 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2472 vmcs_writel(GUEST_LDTR_BASE, 0);
2473 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2474 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2475
2476 vmcs_write32(GUEST_SYSENTER_CS, 0);
2477 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2478 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2479
2480 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002481 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002482 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002483 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002484 kvm_rip_write(vcpu, 0);
2485 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002486
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002487 vmcs_writel(GUEST_DR7, 0x400);
2488
2489 vmcs_writel(GUEST_GDTR_BASE, 0);
2490 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2491
2492 vmcs_writel(GUEST_IDTR_BASE, 0);
2493 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2494
2495 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2496 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2497 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2498
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002499 /* Special registers */
2500 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2501
2502 setup_msrs(vmx);
2503
Avi Kivity6aa8b732006-12-10 02:21:36 -08002504 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2505
Sheng Yangf78e0e22007-10-29 09:40:42 +08002506 if (cpu_has_vmx_tpr_shadow()) {
2507 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2508 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2509 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002510 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002511 vmcs_write32(TPR_THRESHOLD, 0);
2512 }
2513
2514 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2515 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002516 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002517
Sheng Yang2384d2b2008-01-17 15:14:33 +08002518 if (vmx->vpid != 0)
2519 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2520
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002521 vmx->vcpu.arch.cr0 = 0x60000010;
2522 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002523 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002524 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002525 vmx_fpu_activate(&vmx->vcpu);
2526 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002527
Sheng Yang2384d2b2008-01-17 15:14:33 +08002528 vpid_sync_vcpu_all(vmx);
2529
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002530 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002531
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002532 /* HACK: Don't enable emulation on guest boot/reset */
2533 vmx->emulation_required = 0;
2534
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002536 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002537 return ret;
2538}
2539
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002540static void enable_irq_window(struct kvm_vcpu *vcpu)
2541{
2542 u32 cpu_based_vm_exec_control;
2543
2544 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2545 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2546 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2547}
2548
2549static void enable_nmi_window(struct kvm_vcpu *vcpu)
2550{
2551 u32 cpu_based_vm_exec_control;
2552
2553 if (!cpu_has_virtual_nmis()) {
2554 enable_irq_window(vcpu);
2555 return;
2556 }
2557
2558 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2559 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2560 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2561}
2562
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002563static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002564{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002565 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002566 uint32_t intr;
2567 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002568
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002569 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002570
Avi Kivityfa89a812008-09-01 15:57:51 +03002571 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002572 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002573 vmx->rmode.irq.pending = true;
2574 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002575 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002576 if (vcpu->arch.interrupt.soft)
2577 vmx->rmode.irq.rip +=
2578 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002579 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2580 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2581 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002582 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002583 return;
2584 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002585 intr = irq | INTR_INFO_VALID_MASK;
2586 if (vcpu->arch.interrupt.soft) {
2587 intr |= INTR_TYPE_SOFT_INTR;
2588 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2589 vmx->vcpu.arch.event_exit_inst_len);
2590 } else
2591 intr |= INTR_TYPE_EXT_INTR;
2592 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002593}
2594
Sheng Yangf08864b2008-05-15 18:23:25 +08002595static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2596{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002597 struct vcpu_vmx *vmx = to_vmx(vcpu);
2598
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002599 if (!cpu_has_virtual_nmis()) {
2600 /*
2601 * Tracking the NMI-blocked state in software is built upon
2602 * finding the next open IRQ window. This, in turn, depends on
2603 * well-behaving guests: They have to keep IRQs disabled at
2604 * least as long as the NMI handler runs. Otherwise we may
2605 * cause NMI nesting, maybe breaking the guest. But as this is
2606 * highly unlikely, we can live with the residual risk.
2607 */
2608 vmx->soft_vnmi_blocked = 1;
2609 vmx->vnmi_blocked_time = 0;
2610 }
2611
Jan Kiszka487b3912008-09-26 09:30:56 +02002612 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002613 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002614 vmx->rmode.irq.pending = true;
2615 vmx->rmode.irq.vector = NMI_VECTOR;
2616 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2617 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2618 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2619 INTR_INFO_VALID_MASK);
2620 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2621 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2622 return;
2623 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002624 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2625 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002626}
2627
Gleb Natapovc4282df2009-04-21 17:45:07 +03002628static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002629{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002630 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002631 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002632
Gleb Natapovc4282df2009-04-21 17:45:07 +03002633 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2634 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2635 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002636}
2637
Gleb Natapov78646122009-03-23 12:12:11 +02002638static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2639{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002640 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2641 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2642 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002643}
2644
Izik Eiduscbc94022007-10-25 00:29:55 +02002645static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2646{
2647 int ret;
2648 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002649 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002650 .guest_phys_addr = addr,
2651 .memory_size = PAGE_SIZE * 3,
2652 .flags = 0,
2653 };
2654
2655 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2656 if (ret)
2657 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002658 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002659 return 0;
2660}
2661
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2663 int vec, u32 err_code)
2664{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002665 /*
2666 * Instruction with address size override prefix opcode 0x67
2667 * Cause the #SS fault with 0 error code in VM86 mode.
2668 */
2669 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002670 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002672 /*
2673 * Forward all other exceptions that are valid in real mode.
2674 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2675 * the required debugging infrastructure rework.
2676 */
2677 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002678 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002679 if (vcpu->guest_debug &
2680 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2681 return 0;
2682 kvm_queue_exception(vcpu, vec);
2683 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002684 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002685 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2686 return 0;
2687 /* fall through */
2688 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002689 case OF_VECTOR:
2690 case BR_VECTOR:
2691 case UD_VECTOR:
2692 case DF_VECTOR:
2693 case SS_VECTOR:
2694 case GP_VECTOR:
2695 case MF_VECTOR:
2696 kvm_queue_exception(vcpu, vec);
2697 return 1;
2698 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699 return 0;
2700}
2701
Andi Kleena0861c02009-06-08 17:37:09 +08002702/*
2703 * Trigger machine check on the host. We assume all the MSRs are already set up
2704 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2705 * We pass a fake environment to the machine check handler because we want
2706 * the guest to be always treated like user space, no matter what context
2707 * it used internally.
2708 */
2709static void kvm_machine_check(void)
2710{
2711#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2712 struct pt_regs regs = {
2713 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2714 .flags = X86_EFLAGS_IF,
2715 };
2716
2717 do_machine_check(&regs, 0);
2718#endif
2719}
2720
Avi Kivity851ba692009-08-24 11:10:17 +03002721static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002722{
2723 /* already handled by vcpu_run */
2724 return 1;
2725}
2726
Avi Kivity851ba692009-08-24 11:10:17 +03002727static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728{
Avi Kivity1155f762007-11-22 11:30:47 +02002729 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002730 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002731 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002732 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 u32 vect_info;
2734 enum emulation_result er;
2735
Avi Kivity1155f762007-11-22 11:30:47 +02002736 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2738
Andi Kleena0861c02009-06-08 17:37:09 +08002739 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002740 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002741
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Mike Dayd77c26f2007-10-08 09:02:08 -04002743 !is_page_fault(intr_info))
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 printk(KERN_ERR "%s: unexpected, vectoring info 0x%x "
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002745 "intr info 0x%x\n", __func__, vect_info, intr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746
Jan Kiszkae4a41882008-09-26 09:30:46 +02002747 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002748 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002749
2750 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002751 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002752 return 1;
2753 }
2754
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002755 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002756 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002757 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002758 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002759 return 1;
2760 }
2761
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002763 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002764 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2766 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002767 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002768 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002769 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002771 trace_kvm_page_fault(cr2, error_code);
2772
Gleb Natapov3298b752009-05-11 13:35:46 +03002773 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002774 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002775 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002776 }
2777
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002778 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002780 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002781 if (vcpu->arch.halt_request) {
2782 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002783 return kvm_emulate_halt(vcpu);
2784 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002786 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002788 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002789 switch (ex_no) {
2790 case DB_VECTOR:
2791 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2792 if (!(vcpu->guest_debug &
2793 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2794 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2795 kvm_queue_exception(vcpu, DB_VECTOR);
2796 return 1;
2797 }
2798 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2799 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2800 /* fall through */
2801 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002803 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2804 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002805 break;
2806 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002807 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2808 kvm_run->ex.exception = ex_no;
2809 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002810 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 return 0;
2813}
2814
Avi Kivity851ba692009-08-24 11:10:17 +03002815static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002816{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002817 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 return 1;
2819}
2820
Avi Kivity851ba692009-08-24 11:10:17 +03002821static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002822{
Avi Kivity851ba692009-08-24 11:10:17 +03002823 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002824 return 0;
2825}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002826
Avi Kivity851ba692009-08-24 11:10:17 +03002827static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002828{
He, Qingbfdaab02007-09-12 14:18:28 +08002829 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002830 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002831 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002832
Avi Kivity1165f5f2007-04-19 17:27:43 +03002833 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002834 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002835 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002836
2837 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002838 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002839 return 0;
2840 return 1;
2841 }
2842
2843 size = (exit_qualification & 7) + 1;
2844 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002845 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002846
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002847 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002848 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002849}
2850
Ingo Molnar102d8322007-02-19 14:37:47 +02002851static void
2852vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2853{
2854 /*
2855 * Patch in the VMCALL instruction:
2856 */
2857 hypercall[0] = 0x0f;
2858 hypercall[1] = 0x01;
2859 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002860}
2861
Avi Kivity851ba692009-08-24 11:10:17 +03002862static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002864 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 int cr;
2866 int reg;
2867
He, Qingbfdaab02007-09-12 14:18:28 +08002868 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 cr = exit_qualification & 15;
2870 reg = (exit_qualification >> 8) & 15;
2871 switch ((exit_qualification >> 4) & 3) {
2872 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002873 val = kvm_register_read(vcpu, reg);
2874 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875 switch (cr) {
2876 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002877 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002878 skip_emulated_instruction(vcpu);
2879 return 1;
2880 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002881 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 skip_emulated_instruction(vcpu);
2883 return 1;
2884 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002885 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 skip_emulated_instruction(vcpu);
2887 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002888 case 8: {
2889 u8 cr8_prev = kvm_get_cr8(vcpu);
2890 u8 cr8 = kvm_register_read(vcpu, reg);
2891 kvm_set_cr8(vcpu, cr8);
2892 skip_emulated_instruction(vcpu);
2893 if (irqchip_in_kernel(vcpu->kvm))
2894 return 1;
2895 if (cr8_prev <= cr8)
2896 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002897 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002898 return 0;
2899 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002900 };
2901 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002902 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002903 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002904 vcpu->arch.cr0 &= ~X86_CR0_TS;
2905 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002906 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002907 skip_emulated_instruction(vcpu);
2908 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002909 case 1: /*mov from cr*/
2910 switch (cr) {
2911 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002912 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002913 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002914 skip_emulated_instruction(vcpu);
2915 return 1;
2916 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002917 val = kvm_get_cr8(vcpu);
2918 kvm_register_write(vcpu, reg, val);
2919 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 skip_emulated_instruction(vcpu);
2921 return 1;
2922 }
2923 break;
2924 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002925 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002926
2927 skip_emulated_instruction(vcpu);
2928 return 1;
2929 default:
2930 break;
2931 }
Avi Kivity851ba692009-08-24 11:10:17 +03002932 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002933 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002934 (int)(exit_qualification >> 4) & 3, cr);
2935 return 0;
2936}
2937
Avi Kivity851ba692009-08-24 11:10:17 +03002938static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939{
He, Qingbfdaab02007-09-12 14:18:28 +08002940 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 unsigned long val;
2942 int dr, reg;
2943
Avi Kivity0a79b002009-09-01 12:03:25 +03002944 if (!kvm_require_cpl(vcpu, 0))
2945 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002946 dr = vmcs_readl(GUEST_DR7);
2947 if (dr & DR7_GD) {
2948 /*
2949 * As the vm-exit takes precedence over the debug trap, we
2950 * need to emulate the latter, either for the host or the
2951 * guest debugging itself.
2952 */
2953 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03002954 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
2955 vcpu->run->debug.arch.dr7 = dr;
2956 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002957 vmcs_readl(GUEST_CS_BASE) +
2958 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03002959 vcpu->run->debug.arch.exception = DB_VECTOR;
2960 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002961 return 0;
2962 } else {
2963 vcpu->arch.dr7 &= ~DR7_GD;
2964 vcpu->arch.dr6 |= DR6_BD;
2965 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
2966 kvm_queue_exception(vcpu, DB_VECTOR);
2967 return 1;
2968 }
2969 }
2970
He, Qingbfdaab02007-09-12 14:18:28 +08002971 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002972 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
2973 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
2974 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002976 case 0 ... 3:
2977 val = vcpu->arch.db[dr];
2978 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002979 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002980 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981 break;
2982 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002983 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002984 break;
2985 default:
2986 val = 0;
2987 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002988 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002990 val = vcpu->arch.regs[reg];
2991 switch (dr) {
2992 case 0 ... 3:
2993 vcpu->arch.db[dr] = val;
2994 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2995 vcpu->arch.eff_db[dr] = val;
2996 break;
2997 case 4 ... 5:
2998 if (vcpu->arch.cr4 & X86_CR4_DE)
2999 kvm_queue_exception(vcpu, UD_VECTOR);
3000 break;
3001 case 6:
3002 if (val & 0xffffffff00000000ULL) {
3003 kvm_queue_exception(vcpu, GP_VECTOR);
3004 break;
3005 }
3006 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3007 break;
3008 case 7:
3009 if (val & 0xffffffff00000000ULL) {
3010 kvm_queue_exception(vcpu, GP_VECTOR);
3011 break;
3012 }
3013 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3014 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3015 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3016 vcpu->arch.switch_db_regs =
3017 (val & DR7_BP_EN_MASK);
3018 }
3019 break;
3020 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003022 skip_emulated_instruction(vcpu);
3023 return 1;
3024}
3025
Avi Kivity851ba692009-08-24 11:10:17 +03003026static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003027{
Avi Kivity06465c52007-02-28 20:46:53 +02003028 kvm_emulate_cpuid(vcpu);
3029 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003030}
3031
Avi Kivity851ba692009-08-24 11:10:17 +03003032static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003033{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003034 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003035 u64 data;
3036
3037 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003038 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003039 return 1;
3040 }
3041
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003042 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003043
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003045 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3046 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003047 skip_emulated_instruction(vcpu);
3048 return 1;
3049}
3050
Avi Kivity851ba692009-08-24 11:10:17 +03003051static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003053 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3054 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3055 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003056
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003057 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003058
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003060 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003061 return 1;
3062 }
3063
3064 skip_emulated_instruction(vcpu);
3065 return 1;
3066}
3067
Avi Kivity851ba692009-08-24 11:10:17 +03003068static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003069{
3070 return 1;
3071}
3072
Avi Kivity851ba692009-08-24 11:10:17 +03003073static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003074{
Eddie Dong85f455f2007-07-06 12:20:49 +03003075 u32 cpu_based_vm_exec_control;
3076
3077 /* clear pending irq */
3078 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3079 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3080 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003081
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003082 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003083
Dor Laorc1150d82007-01-05 16:36:24 -08003084 /*
3085 * If the user space waits to inject interrupts, exit as soon as
3086 * possible
3087 */
Gleb Natapov80618232009-04-21 17:44:56 +03003088 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003089 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003090 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003091 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003092 return 0;
3093 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094 return 1;
3095}
3096
Avi Kivity851ba692009-08-24 11:10:17 +03003097static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098{
3099 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003100 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101}
3102
Avi Kivity851ba692009-08-24 11:10:17 +03003103static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003104{
Dor Laor510043d2007-02-19 18:25:43 +02003105 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003106 kvm_emulate_hypercall(vcpu);
3107 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003108}
3109
Avi Kivity851ba692009-08-24 11:10:17 +03003110static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003111{
3112 kvm_queue_exception(vcpu, UD_VECTOR);
3113 return 1;
3114}
3115
Avi Kivity851ba692009-08-24 11:10:17 +03003116static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003117{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003118 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003119
3120 kvm_mmu_invlpg(vcpu, exit_qualification);
3121 skip_emulated_instruction(vcpu);
3122 return 1;
3123}
3124
Avi Kivity851ba692009-08-24 11:10:17 +03003125static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003126{
3127 skip_emulated_instruction(vcpu);
3128 /* TODO: Add support for VT-d/pass-through device */
3129 return 1;
3130}
3131
Avi Kivity851ba692009-08-24 11:10:17 +03003132static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003133{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003134 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003135 enum emulation_result er;
3136 unsigned long offset;
3137
Sheng Yangf9c617f2009-03-25 10:08:52 +08003138 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003139 offset = exit_qualification & 0xffful;
3140
Avi Kivity851ba692009-08-24 11:10:17 +03003141 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003142
3143 if (er != EMULATE_DONE) {
3144 printk(KERN_ERR
3145 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3146 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003147 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003148 }
3149 return 1;
3150}
3151
Avi Kivity851ba692009-08-24 11:10:17 +03003152static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003153{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003154 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003155 unsigned long exit_qualification;
3156 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003157 int reason, type, idt_v;
3158
3159 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3160 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003161
3162 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3163
3164 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003165 if (reason == TASK_SWITCH_GATE && idt_v) {
3166 switch (type) {
3167 case INTR_TYPE_NMI_INTR:
3168 vcpu->arch.nmi_injected = false;
3169 if (cpu_has_virtual_nmis())
3170 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3171 GUEST_INTR_STATE_NMI);
3172 break;
3173 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003174 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003175 kvm_clear_interrupt_queue(vcpu);
3176 break;
3177 case INTR_TYPE_HARD_EXCEPTION:
3178 case INTR_TYPE_SOFT_EXCEPTION:
3179 kvm_clear_exception_queue(vcpu);
3180 break;
3181 default:
3182 break;
3183 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003184 }
Izik Eidus37817f22008-03-24 23:14:53 +02003185 tss_selector = exit_qualification;
3186
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003187 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3188 type != INTR_TYPE_EXT_INTR &&
3189 type != INTR_TYPE_NMI_INTR))
3190 skip_emulated_instruction(vcpu);
3191
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003192 if (!kvm_task_switch(vcpu, tss_selector, reason))
3193 return 0;
3194
3195 /* clear all local breakpoint enable flags */
3196 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3197
3198 /*
3199 * TODO: What about debug traps on tss switch?
3200 * Are we supposed to inject them and update dr6?
3201 */
3202
3203 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003204}
3205
Avi Kivity851ba692009-08-24 11:10:17 +03003206static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003207{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003208 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003209 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003210 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003211
Sheng Yangf9c617f2009-03-25 10:08:52 +08003212 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003213
3214 if (exit_qualification & (1 << 6)) {
3215 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003216 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003217 }
3218
3219 gla_validity = (exit_qualification >> 7) & 0x3;
3220 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3221 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3222 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3223 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003224 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003225 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3226 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003227 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3228 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003229 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003230 }
3231
3232 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003233 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003234 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003235}
3236
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003237static u64 ept_rsvd_mask(u64 spte, int level)
3238{
3239 int i;
3240 u64 mask = 0;
3241
3242 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3243 mask |= (1ULL << i);
3244
3245 if (level > 2)
3246 /* bits 7:3 reserved */
3247 mask |= 0xf8;
3248 else if (level == 2) {
3249 if (spte & (1ULL << 7))
3250 /* 2MB ref, bits 20:12 reserved */
3251 mask |= 0x1ff000;
3252 else
3253 /* bits 6:3 reserved */
3254 mask |= 0x78;
3255 }
3256
3257 return mask;
3258}
3259
3260static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3261 int level)
3262{
3263 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3264
3265 /* 010b (write-only) */
3266 WARN_ON((spte & 0x7) == 0x2);
3267
3268 /* 110b (write/execute) */
3269 WARN_ON((spte & 0x7) == 0x6);
3270
3271 /* 100b (execute-only) and value not supported by logical processor */
3272 if (!cpu_has_vmx_ept_execute_only())
3273 WARN_ON((spte & 0x7) == 0x4);
3274
3275 /* not 000b */
3276 if ((spte & 0x7)) {
3277 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3278
3279 if (rsvd_bits != 0) {
3280 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3281 __func__, rsvd_bits);
3282 WARN_ON(1);
3283 }
3284
3285 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3286 u64 ept_mem_type = (spte & 0x38) >> 3;
3287
3288 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3289 ept_mem_type == 7) {
3290 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3291 __func__, ept_mem_type);
3292 WARN_ON(1);
3293 }
3294 }
3295 }
3296}
3297
Avi Kivity851ba692009-08-24 11:10:17 +03003298static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003299{
3300 u64 sptes[4];
3301 int nr_sptes, i;
3302 gpa_t gpa;
3303
3304 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3305
3306 printk(KERN_ERR "EPT: Misconfiguration.\n");
3307 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3308
3309 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3310
3311 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3312 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3313
Avi Kivity851ba692009-08-24 11:10:17 +03003314 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3315 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003316
3317 return 0;
3318}
3319
Avi Kivity851ba692009-08-24 11:10:17 +03003320static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003321{
3322 u32 cpu_based_vm_exec_control;
3323
3324 /* clear pending NMI */
3325 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3326 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3327 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3328 ++vcpu->stat.nmi_window_exits;
3329
3330 return 1;
3331}
3332
Mohammed Gamal80ced182009-09-01 12:48:18 +02003333static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003334{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003335 struct vcpu_vmx *vmx = to_vmx(vcpu);
3336 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003337 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003338
3339 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003340 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003341
Mohammed Gamal80ced182009-09-01 12:48:18 +02003342 if (err == EMULATE_DO_MMIO) {
3343 ret = 0;
3344 goto out;
3345 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003346
3347 if (err != EMULATE_DONE) {
3348 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003349 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3350 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
3351 ret = 0;
3352 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003353 }
3354
3355 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003356 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003357 if (need_resched())
3358 schedule();
3359 }
3360
Mohammed Gamal80ced182009-09-01 12:48:18 +02003361 vmx->emulation_required = 0;
3362out:
3363 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003364}
3365
Avi Kivity6aa8b732006-12-10 02:21:36 -08003366/*
3367 * The exit handlers return 1 if the exit was handled fully and guest execution
3368 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3369 * to be done to userspace and return 0.
3370 */
Avi Kivity851ba692009-08-24 11:10:17 +03003371static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3373 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003374 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003375 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003376 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 [EXIT_REASON_CR_ACCESS] = handle_cr,
3378 [EXIT_REASON_DR_ACCESS] = handle_dr,
3379 [EXIT_REASON_CPUID] = handle_cpuid,
3380 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3381 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3382 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3383 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003384 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003385 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003386 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3387 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3388 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3389 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3390 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3391 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3392 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3393 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3394 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003395 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3396 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003397 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003398 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003399 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003400 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3401 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003402};
3403
3404static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003405 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003406
3407/*
3408 * The guest has exited. See if we can fix it or if we need userspace
3409 * assistance.
3410 */
Avi Kivity851ba692009-08-24 11:10:17 +03003411static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003412{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003413 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003414 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003415 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003416
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003417 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003418
Mohammed Gamal80ced182009-09-01 12:48:18 +02003419 /* If guest state is invalid, start emulating */
3420 if (vmx->emulation_required && emulate_invalid_guest_state)
3421 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003422
Sheng Yang14394422008-04-28 12:24:45 +08003423 /* Access CR3 don't cause VMExit in paging mode, so we need
3424 * to sync with guest real CR3. */
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03003425 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003426 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003427
Avi Kivity29bd8a72007-09-10 17:27:03 +03003428 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003429 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3430 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003431 = vmcs_read32(VM_INSTRUCTION_ERROR);
3432 return 0;
3433 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003434
Mike Dayd77c26f2007-10-08 09:02:08 -04003435 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003436 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003437 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3438 exit_reason != EXIT_REASON_TASK_SWITCH))
3439 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3440 "(0x%x) and exit reason is 0x%x\n",
3441 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003442
3443 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003444 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003445 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003446 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003447 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003448 /*
3449 * This CPU don't support us in finding the end of an
3450 * NMI-blocked window if the guest runs with IRQs
3451 * disabled. So we pull the trigger after 1 s of
3452 * futile waiting, but inform the user about this.
3453 */
3454 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3455 "state on VCPU %d after 1 s timeout\n",
3456 __func__, vcpu->vcpu_id);
3457 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003458 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003459 }
3460
Avi Kivity6aa8b732006-12-10 02:21:36 -08003461 if (exit_reason < kvm_vmx_max_exit_handlers
3462 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003463 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003465 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3466 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003467 }
3468 return 0;
3469}
3470
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003471static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003472{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003473 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003474 vmcs_write32(TPR_THRESHOLD, 0);
3475 return;
3476 }
3477
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003478 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003479}
3480
Avi Kivitycf393f72008-07-01 16:20:21 +03003481static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3482{
3483 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003484 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003485 bool unblock_nmi;
3486 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003487 int type;
3488 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003489
3490 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003491
Andi Kleena0861c02009-06-08 17:37:09 +08003492 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3493
3494 /* Handle machine checks before interrupts are enabled */
3495 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3496 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3497 && is_machine_check(exit_intr_info)))
3498 kvm_machine_check();
3499
Gleb Natapov20f65982009-05-11 13:35:55 +03003500 /* We need to handle NMIs before interrupts are enabled */
3501 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003502 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003503 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003504
3505 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3506
Avi Kivitycf393f72008-07-01 16:20:21 +03003507 if (cpu_has_virtual_nmis()) {
3508 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3509 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3510 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003511 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003512 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3513 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003514 * SDM 3: 23.2.2 (September 2008)
3515 * Bit 12 is undefined in any of the following cases:
3516 * If the VM exit sets the valid bit in the IDT-vectoring
3517 * information field.
3518 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003519 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003520 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3521 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003522 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3523 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003524 } else if (unlikely(vmx->soft_vnmi_blocked))
3525 vmx->vnmi_blocked_time +=
3526 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003527
Gleb Natapov37b96e92009-03-30 16:03:13 +03003528 vmx->vcpu.arch.nmi_injected = false;
3529 kvm_clear_exception_queue(&vmx->vcpu);
3530 kvm_clear_interrupt_queue(&vmx->vcpu);
3531
3532 if (!idtv_info_valid)
3533 return;
3534
Avi Kivity668f6122008-07-02 09:28:55 +03003535 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3536 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003537
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003538 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003539 case INTR_TYPE_NMI_INTR:
3540 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003541 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003542 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003543 * Clear bit "block by NMI" before VM entry if a NMI
3544 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003545 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003546 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3547 GUEST_INTR_STATE_NMI);
3548 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003549 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003550 vmx->vcpu.arch.event_exit_inst_len =
3551 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3552 /* fall through */
3553 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003554 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003555 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3556 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003557 } else
3558 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003559 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003560 case INTR_TYPE_SOFT_INTR:
3561 vmx->vcpu.arch.event_exit_inst_len =
3562 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3563 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003564 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003565 kvm_queue_interrupt(&vmx->vcpu, vector,
3566 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003567 break;
3568 default:
3569 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003570 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003571}
3572
Avi Kivity9c8cba32007-11-22 11:42:59 +02003573/*
3574 * Failure to inject an interrupt should give us the information
3575 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3576 * when fetching the interrupt redirection bitmap in the real-mode
3577 * tss, this doesn't happen. So we do it ourselves.
3578 */
3579static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3580{
3581 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003582 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003583 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003584 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003585 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3586 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3587 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3588 return;
3589 }
3590 vmx->idt_vectoring_info =
3591 VECTORING_INFO_VALID_MASK
3592 | INTR_TYPE_EXT_INTR
3593 | vmx->rmode.irq.vector;
3594}
3595
Avi Kivityc8019492008-07-14 14:44:59 +03003596#ifdef CONFIG_X86_64
3597#define R "r"
3598#define Q "q"
3599#else
3600#define R "e"
3601#define Q "l"
3602#endif
3603
Avi Kivity851ba692009-08-24 11:10:17 +03003604static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003605{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003606 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003607
Avi Kivity8f5d5492009-05-31 18:41:29 +03003608 if (enable_ept && is_paging(vcpu)) {
3609 vmcs_writel(GUEST_CR3, vcpu->arch.cr3);
3610 ept_load_pdptrs(vcpu);
3611 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003612 /* Record the guest's net vcpu time for enforced NMI injections. */
3613 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3614 vmx->entry_time = ktime_get();
3615
Mohammed Gamal80ced182009-09-01 12:48:18 +02003616 /* Don't enter VMX if guest state is invalid, let the exit handler
3617 start emulation until we arrive back to a valid state */
3618 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003619 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003620
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003621 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3622 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3623 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3624 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3625
Gleb Natapov787ff732009-05-18 11:44:06 +03003626 /* When single-stepping over STI and MOV SS, we must clear the
3627 * corresponding interruptibility bits in the guest state. Otherwise
3628 * vmentry fails as it then expects bit 14 (BS) in pending debug
3629 * exceptions being set, but that's not correct for the guest debugging
3630 * case. */
3631 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3632 vmx_set_interrupt_shadow(vcpu, 0);
3633
Avi Kivitye6adf282007-04-30 16:07:54 +03003634 /*
3635 * Loading guest fpu may have cleared host cr0.ts
3636 */
3637 vmcs_writel(HOST_CR0, read_cr0());
3638
Avi Kivitye8a48342009-09-01 16:06:25 +03003639 if (vcpu->arch.switch_db_regs)
3640 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003641
Mike Dayd77c26f2007-10-08 09:02:08 -04003642 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003643 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003644 "push %%"R"dx; push %%"R"bp;"
3645 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003646 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3647 "je 1f \n\t"
3648 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003649 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003650 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003651 /* Reload cr2 if changed */
3652 "mov %c[cr2](%0), %%"R"ax \n\t"
3653 "mov %%cr2, %%"R"dx \n\t"
3654 "cmp %%"R"ax, %%"R"dx \n\t"
3655 "je 2f \n\t"
3656 "mov %%"R"ax, %%cr2 \n\t"
3657 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003658 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003659 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003660 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003661 "mov %c[rax](%0), %%"R"ax \n\t"
3662 "mov %c[rbx](%0), %%"R"bx \n\t"
3663 "mov %c[rdx](%0), %%"R"dx \n\t"
3664 "mov %c[rsi](%0), %%"R"si \n\t"
3665 "mov %c[rdi](%0), %%"R"di \n\t"
3666 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003667#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003668 "mov %c[r8](%0), %%r8 \n\t"
3669 "mov %c[r9](%0), %%r9 \n\t"
3670 "mov %c[r10](%0), %%r10 \n\t"
3671 "mov %c[r11](%0), %%r11 \n\t"
3672 "mov %c[r12](%0), %%r12 \n\t"
3673 "mov %c[r13](%0), %%r13 \n\t"
3674 "mov %c[r14](%0), %%r14 \n\t"
3675 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003676#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003677 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3678
Avi Kivity6aa8b732006-12-10 02:21:36 -08003679 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003680 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003681 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003682 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003683 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003684 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003685 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003686 "xchg %0, (%%"R"sp) \n\t"
3687 "mov %%"R"ax, %c[rax](%0) \n\t"
3688 "mov %%"R"bx, %c[rbx](%0) \n\t"
3689 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3690 "mov %%"R"dx, %c[rdx](%0) \n\t"
3691 "mov %%"R"si, %c[rsi](%0) \n\t"
3692 "mov %%"R"di, %c[rdi](%0) \n\t"
3693 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003694#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003695 "mov %%r8, %c[r8](%0) \n\t"
3696 "mov %%r9, %c[r9](%0) \n\t"
3697 "mov %%r10, %c[r10](%0) \n\t"
3698 "mov %%r11, %c[r11](%0) \n\t"
3699 "mov %%r12, %c[r12](%0) \n\t"
3700 "mov %%r13, %c[r13](%0) \n\t"
3701 "mov %%r14, %c[r14](%0) \n\t"
3702 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003704 "mov %%cr2, %%"R"ax \n\t"
3705 "mov %%"R"ax, %c[cr2](%0) \n\t"
3706
3707 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003708 "setbe %c[fail](%0) \n\t"
3709 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3710 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3711 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003712 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003713 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3714 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3715 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3716 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3717 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3718 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3719 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003720#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003721 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3722 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3723 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3724 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3725 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3726 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3727 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3728 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003729#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003730 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003731 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003732 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003733#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003734 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3735#endif
3736 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003737
Avi Kivity6de4f3ad2009-05-31 22:58:47 +03003738 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3739 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003740 vcpu->arch.regs_dirty = 0;
3741
Avi Kivitye8a48342009-09-01 16:06:25 +03003742 if (vcpu->arch.switch_db_regs)
3743 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003744
Avi Kivity1155f762007-11-22 11:30:47 +02003745 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003746 if (vmx->rmode.irq.pending)
3747 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003748
Mike Dayd77c26f2007-10-08 09:02:08 -04003749 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003750 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003751
Avi Kivitycf393f72008-07-01 16:20:21 +03003752 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003753}
3754
Avi Kivityc8019492008-07-14 14:44:59 +03003755#undef R
3756#undef Q
3757
Avi Kivity6aa8b732006-12-10 02:21:36 -08003758static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3759{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003760 struct vcpu_vmx *vmx = to_vmx(vcpu);
3761
3762 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003763 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003764 free_vmcs(vmx->vmcs);
3765 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003766 }
3767}
3768
3769static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3770{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003771 struct vcpu_vmx *vmx = to_vmx(vcpu);
3772
Sheng Yang2384d2b2008-01-17 15:14:33 +08003773 spin_lock(&vmx_vpid_lock);
3774 if (vmx->vpid != 0)
3775 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3776 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003777 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003778 kfree(vmx->host_msrs);
3779 kfree(vmx->guest_msrs);
3780 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003781 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003782}
3783
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003784static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003785{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003786 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003787 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003788 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003789
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003790 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003791 return ERR_PTR(-ENOMEM);
3792
Sheng Yang2384d2b2008-01-17 15:14:33 +08003793 allocate_vpid(vmx);
3794
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003795 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3796 if (err)
3797 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003798
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003799 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003800 if (!vmx->guest_msrs) {
3801 err = -ENOMEM;
3802 goto uninit_vcpu;
3803 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003804
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003805 vmx->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
3806 if (!vmx->host_msrs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003807 goto free_guest_msrs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003808
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003809 vmx->vmcs = alloc_vmcs();
3810 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003811 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003812
3813 vmcs_clear(vmx->vmcs);
3814
Avi Kivity15ad7142007-07-11 18:17:21 +03003815 cpu = get_cpu();
3816 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003817 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003818 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003819 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003820 if (err)
3821 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003822 if (vm_need_virtualize_apic_accesses(kvm))
3823 if (alloc_apic_access_page(kvm) != 0)
3824 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003825
Sheng Yangb927a3c2009-07-21 10:42:48 +08003826 if (enable_ept) {
3827 if (!kvm->arch.ept_identity_map_addr)
3828 kvm->arch.ept_identity_map_addr =
3829 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003830 if (alloc_identity_pagetable(kvm) != 0)
3831 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003832 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003833
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003834 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003835
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003836free_vmcs:
3837 free_vmcs(vmx->vmcs);
3838free_msrs:
3839 kfree(vmx->host_msrs);
3840free_guest_msrs:
3841 kfree(vmx->guest_msrs);
3842uninit_vcpu:
3843 kvm_vcpu_uninit(&vmx->vcpu);
3844free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003845 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003846 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003847}
3848
Yang, Sheng002c7f72007-07-31 14:23:01 +03003849static void __init vmx_check_processor_compat(void *rtn)
3850{
3851 struct vmcs_config vmcs_conf;
3852
3853 *(int *)rtn = 0;
3854 if (setup_vmcs_config(&vmcs_conf) < 0)
3855 *(int *)rtn = -EIO;
3856 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3857 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3858 smp_processor_id());
3859 *(int *)rtn = -EIO;
3860 }
3861}
3862
Sheng Yang67253af2008-04-25 10:20:22 +08003863static int get_ept_level(void)
3864{
3865 return VMX_EPT_DEFAULT_GAW + 1;
3866}
3867
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003868static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003869{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003870 u64 ret;
3871
Sheng Yang522c68c2009-04-27 20:35:43 +08003872 /* For VT-d and EPT combination
3873 * 1. MMIO: always map as UC
3874 * 2. EPT with VT-d:
3875 * a. VT-d without snooping control feature: can't guarantee the
3876 * result, try to trust guest.
3877 * b. VT-d with snooping control feature: snooping control feature of
3878 * VT-d engine can guarantee the cache correctness. Just set it
3879 * to WB to keep consistent with host. So the same as item 3.
3880 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3881 * consistent with host MTRR
3882 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003883 if (is_mmio)
3884 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003885 else if (vcpu->kvm->arch.iommu_domain &&
3886 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3887 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3888 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003889 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003890 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3891 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003892
3893 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003894}
3895
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003896static const struct trace_print_flags vmx_exit_reasons_str[] = {
3897 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3898 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3899 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3900 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3901 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3902 { EXIT_REASON_CR_ACCESS, "cr_access" },
3903 { EXIT_REASON_DR_ACCESS, "dr_access" },
3904 { EXIT_REASON_CPUID, "cpuid" },
3905 { EXIT_REASON_MSR_READ, "rdmsr" },
3906 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3907 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3908 { EXIT_REASON_HLT, "halt" },
3909 { EXIT_REASON_INVLPG, "invlpg" },
3910 { EXIT_REASON_VMCALL, "hypercall" },
3911 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3912 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3913 { EXIT_REASON_WBINVD, "wbinvd" },
3914 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3915 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3916 { -1, NULL }
3917};
3918
Joerg Roedel344f4142009-07-27 16:30:48 +02003919static bool vmx_gb_page_enable(void)
3920{
3921 return false;
3922}
3923
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003924static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925 .cpu_has_kvm_support = cpu_has_kvm_support,
3926 .disabled_by_bios = vmx_disabled_by_bios,
3927 .hardware_setup = hardware_setup,
3928 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003929 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003930 .hardware_enable = hardware_enable,
3931 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003932 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003933
3934 .vcpu_create = vmx_create_vcpu,
3935 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003936 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003937
Avi Kivity04d2cc72007-09-10 18:10:54 +03003938 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003939 .vcpu_load = vmx_vcpu_load,
3940 .vcpu_put = vmx_vcpu_put,
3941
3942 .set_guest_debug = set_guest_debug,
3943 .get_msr = vmx_get_msr,
3944 .set_msr = vmx_set_msr,
3945 .get_segment_base = vmx_get_segment_base,
3946 .get_segment = vmx_get_segment,
3947 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003948 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003950 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003951 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003952 .set_cr3 = vmx_set_cr3,
3953 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003954 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003955 .get_idt = vmx_get_idt,
3956 .set_idt = vmx_set_idt,
3957 .get_gdt = vmx_get_gdt,
3958 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003959 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003960 .get_rflags = vmx_get_rflags,
3961 .set_rflags = vmx_set_rflags,
3962
3963 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003964
Avi Kivity6aa8b732006-12-10 02:21:36 -08003965 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02003966 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003967 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003968 .set_interrupt_shadow = vmx_set_interrupt_shadow,
3969 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003970 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003971 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003972 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003973 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003974 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003975 .nmi_allowed = vmx_nmi_allowed,
3976 .enable_nmi_window = enable_nmi_window,
3977 .enable_irq_window = enable_irq_window,
3978 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003979
Izik Eiduscbc94022007-10-25 00:29:55 +02003980 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003981 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003982 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003983
3984 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02003985 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986};
3987
3988static int __init vmx_init(void)
3989{
He, Qingfdef3ad2007-04-30 09:45:24 +03003990 int r;
3991
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003992 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003993 if (!vmx_io_bitmap_a)
3994 return -ENOMEM;
3995
Avi Kivity3e7c73e2009-02-24 21:46:19 +02003996 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03003997 if (!vmx_io_bitmap_b) {
3998 r = -ENOMEM;
3999 goto out;
4000 }
4001
Avi Kivity58972972009-02-24 22:26:47 +02004002 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4003 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004004 r = -ENOMEM;
4005 goto out1;
4006 }
4007
Avi Kivity58972972009-02-24 22:26:47 +02004008 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4009 if (!vmx_msr_bitmap_longmode) {
4010 r = -ENOMEM;
4011 goto out2;
4012 }
4013
He, Qingfdef3ad2007-04-30 09:45:24 +03004014 /*
4015 * Allow direct access to the PC debug port (it is often used for I/O
4016 * delays, but the vmexits simply slow things down).
4017 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004018 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4019 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004020
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004021 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004022
Avi Kivity58972972009-02-24 22:26:47 +02004023 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4024 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004025
Sheng Yang2384d2b2008-01-17 15:14:33 +08004026 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4027
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004028 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004029 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004030 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004031
Avi Kivity58972972009-02-24 22:26:47 +02004032 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4033 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4034 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4035 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4036 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4037 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004038
Avi Kivity089d0342009-03-23 18:26:32 +02004039 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004040 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004041 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004042 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004043 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004044 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004045 kvm_enable_tdp();
4046 } else
4047 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004048
Avi Kivityc7addb92007-09-16 18:58:32 +02004049 if (bypass_guest_pf)
4050 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4051
He, Qingfdef3ad2007-04-30 09:45:24 +03004052 return 0;
4053
Avi Kivity58972972009-02-24 22:26:47 +02004054out3:
4055 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004056out2:
Avi Kivity58972972009-02-24 22:26:47 +02004057 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004058out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004059 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004060out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004061 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004062 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004063}
4064
4065static void __exit vmx_exit(void)
4066{
Avi Kivity58972972009-02-24 22:26:47 +02004067 free_page((unsigned long)vmx_msr_bitmap_legacy);
4068 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004069 free_page((unsigned long)vmx_io_bitmap_b);
4070 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004071
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004072 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004073}
4074
4075module_init(vmx_init)
4076module_exit(vmx_exit)