blob: 8a8e139650766fd34f74a6a213de6551aa3ccee9 [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
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +080064/*
65 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
66 * ple_gap: upper bound on the amount of time between two successive
67 * executions of PAUSE in a loop. Also indicate if ple enabled.
68 * According to test, this time is usually small than 41 cycles.
69 * ple_window: upper bound on the amount of time a guest is allowed to execute
70 * in a PAUSE loop. Tests indicate that most spinlocks are held for
71 * less than 2^12 cycles
72 * Time is measured based on a counter that runs at the same rate as the TSC,
73 * refer SDM volume 3b section 21.6.13 & 22.1.3.
74 */
75#define KVM_VMX_DEFAULT_PLE_GAP 41
76#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
77static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
78module_param(ple_gap, int, S_IRUGO);
79
80static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
81module_param(ple_window, int, S_IRUGO);
82
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040083struct vmcs {
84 u32 revision_id;
85 u32 abort;
86 char data[0];
87};
88
Avi Kivity26bb0982009-09-07 11:14:12 +030089struct shared_msr_entry {
90 unsigned index;
91 u64 data;
Avi Kivityd5696722009-12-02 12:28:47 +020092 u64 mask;
Avi Kivity26bb0982009-09-07 11:14:12 +030093};
94
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040095struct vcpu_vmx {
Rusty Russellfb3f0f52007-07-27 17:16:56 +100096 struct kvm_vcpu vcpu;
Avi Kivity543e4242008-05-13 16:22:47 +030097 struct list_head local_vcpus_link;
Avi Kivity313dbd42008-07-17 18:04:30 +030098 unsigned long host_rsp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -040099 int launched;
Avi Kivity29bd8a72007-09-10 17:27:03 +0300100 u8 fail;
Avi Kivity1155f762007-11-22 11:30:47 +0200101 u32 idt_vectoring_info;
Avi Kivity26bb0982009-09-07 11:14:12 +0300102 struct shared_msr_entry *guest_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400103 int nmsrs;
104 int save_nmsrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400105#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300106 u64 msr_host_kernel_gs_base;
107 u64 msr_guest_kernel_gs_base;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400108#endif
109 struct vmcs *vmcs;
110 struct {
111 int loaded;
112 u16 fs_sel, gs_sel, ldt_sel;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200113 int gs_ldt_reload_needed;
114 int fs_reload_needed;
Mike Dayd77c26f2007-10-08 09:02:08 -0400115 } host_state;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200116 struct {
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300117 int vm86_active;
118 u8 save_iopl;
119 struct kvm_save_segment {
120 u16 selector;
121 unsigned long base;
122 u32 limit;
123 u32 ar;
124 } tr, es, ds, fs, gs;
Avi Kivity9c8cba32007-11-22 11:42:59 +0200125 struct {
126 bool pending;
127 u8 vector;
128 unsigned rip;
129 } irq;
130 } rmode;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800131 int vpid;
Mohammed Gamal04fa4d32008-08-17 16:39:48 +0300132 bool emulation_required;
Jan Kiszka3b86cd92008-09-26 09:30:57 +0200133
134 /* Support for vnmi-less CPUs */
135 int soft_vnmi_blocked;
136 ktime_t entry_time;
137 s64 vnmi_blocked_time;
Andi Kleena0861c02009-06-08 17:37:09 +0800138 u32 exit_reason;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400139};
140
141static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu)
142{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000143 return container_of(vcpu, struct vcpu_vmx, vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400144}
145
Sheng Yangb7ebfb02008-04-25 21:44:52 +0800146static int init_rmode(struct kvm *kvm);
Sheng Yang4e1096d2008-07-06 19:16:51 +0800147static u64 construct_eptp(unsigned long root_hpa);
Avi Kivity75880a02007-06-20 11:20:04 +0300148
Avi Kivity6aa8b732006-12-10 02:21:36 -0800149static DEFINE_PER_CPU(struct vmcs *, vmxarea);
150static DEFINE_PER_CPU(struct vmcs *, current_vmcs);
Avi Kivity543e4242008-05-13 16:22:47 +0300151static DEFINE_PER_CPU(struct list_head, vcpus_on_cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800152
Avi Kivity3e7c73e2009-02-24 21:46:19 +0200153static unsigned long *vmx_io_bitmap_a;
154static unsigned long *vmx_io_bitmap_b;
Avi Kivity58972972009-02-24 22:26:47 +0200155static unsigned long *vmx_msr_bitmap_legacy;
156static unsigned long *vmx_msr_bitmap_longmode;
He, Qingfdef3ad2007-04-30 09:45:24 +0300157
Sheng Yang2384d2b2008-01-17 15:14:33 +0800158static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
159static DEFINE_SPINLOCK(vmx_vpid_lock);
160
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300161static struct vmcs_config {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800162 int size;
163 int order;
164 u32 revision_id;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300165 u32 pin_based_exec_ctrl;
166 u32 cpu_based_exec_ctrl;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800167 u32 cpu_based_2nd_exec_ctrl;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +0300168 u32 vmexit_ctrl;
169 u32 vmentry_ctrl;
170} vmcs_config;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800171
Hannes Ederefff9e52008-11-28 17:02:06 +0100172static struct vmx_capability {
Sheng Yangd56f5462008-04-25 10:13:16 +0800173 u32 ept;
174 u32 vpid;
175} vmx_capability;
176
Avi Kivity6aa8b732006-12-10 02:21:36 -0800177#define VMX_SEGMENT_FIELD(seg) \
178 [VCPU_SREG_##seg] = { \
179 .selector = GUEST_##seg##_SELECTOR, \
180 .base = GUEST_##seg##_BASE, \
181 .limit = GUEST_##seg##_LIMIT, \
182 .ar_bytes = GUEST_##seg##_AR_BYTES, \
183 }
184
185static struct kvm_vmx_segment_field {
186 unsigned selector;
187 unsigned base;
188 unsigned limit;
189 unsigned ar_bytes;
190} kvm_vmx_segment_fields[] = {
191 VMX_SEGMENT_FIELD(CS),
192 VMX_SEGMENT_FIELD(DS),
193 VMX_SEGMENT_FIELD(ES),
194 VMX_SEGMENT_FIELD(FS),
195 VMX_SEGMENT_FIELD(GS),
196 VMX_SEGMENT_FIELD(SS),
197 VMX_SEGMENT_FIELD(TR),
198 VMX_SEGMENT_FIELD(LDTR),
199};
200
Avi Kivity26bb0982009-09-07 11:14:12 +0300201static u64 host_efer;
202
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300203static void ept_save_pdptrs(struct kvm_vcpu *vcpu);
204
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300205/*
206 * Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
207 * away by decrementing the array size.
208 */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800209static const u32 vmx_msr_index[] = {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800210#ifdef CONFIG_X86_64
Avi Kivity44ea2b12009-09-06 15:55:37 +0300211 MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
Avi Kivity6aa8b732006-12-10 02:21:36 -0800212#endif
213 MSR_EFER, MSR_K6_STAR,
214};
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200215#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217static inline int is_page_fault(u32 intr_info)
218{
219 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
220 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100221 (INTR_TYPE_HARD_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222}
223
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300224static inline int is_no_device(u32 intr_info)
225{
226 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
227 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100228 (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300229}
230
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500231static inline int is_invalid_opcode(u32 intr_info)
232{
233 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
234 INTR_INFO_VALID_MASK)) ==
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100235 (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR | INTR_INFO_VALID_MASK);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500236}
237
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238static inline int is_external_interrupt(u32 intr_info)
239{
240 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK))
241 == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK);
242}
243
Andi Kleena0861c02009-06-08 17:37:09 +0800244static inline int is_machine_check(u32 intr_info)
245{
246 return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
247 INTR_INFO_VALID_MASK)) ==
248 (INTR_TYPE_HARD_EXCEPTION | MC_VECTOR | INTR_INFO_VALID_MASK);
249}
250
Sheng Yang25c5f222008-03-28 13:18:56 +0800251static inline int cpu_has_vmx_msr_bitmap(void)
252{
Sheng Yang04547152009-04-01 15:52:31 +0800253 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS;
Sheng Yang25c5f222008-03-28 13:18:56 +0800254}
255
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800256static inline int cpu_has_vmx_tpr_shadow(void)
257{
Sheng Yang04547152009-04-01 15:52:31 +0800258 return vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW;
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800259}
260
261static inline int vm_need_tpr_shadow(struct kvm *kvm)
262{
Sheng Yang04547152009-04-01 15:52:31 +0800263 return (cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm));
Yang, Sheng6e5d8652007-09-12 18:03:11 +0800264}
265
Sheng Yangf78e0e22007-10-29 09:40:42 +0800266static inline int cpu_has_secondary_exec_ctrls(void)
267{
Sheng Yang04547152009-04-01 15:52:31 +0800268 return vmcs_config.cpu_based_exec_ctrl &
269 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Sheng Yangf78e0e22007-10-29 09:40:42 +0800270}
271
Avi Kivity774ead32007-12-26 13:57:04 +0200272static inline bool cpu_has_vmx_virtualize_apic_accesses(void)
Sheng Yangf78e0e22007-10-29 09:40:42 +0800273{
Sheng Yang04547152009-04-01 15:52:31 +0800274 return vmcs_config.cpu_based_2nd_exec_ctrl &
275 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
276}
277
278static inline bool cpu_has_vmx_flexpriority(void)
279{
280 return cpu_has_vmx_tpr_shadow() &&
281 cpu_has_vmx_virtualize_apic_accesses();
Sheng Yangf78e0e22007-10-29 09:40:42 +0800282}
283
Marcelo Tosattie7997942009-06-11 12:07:40 -0300284static inline bool cpu_has_vmx_ept_execute_only(void)
285{
286 return !!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT);
287}
288
289static inline bool cpu_has_vmx_eptp_uncacheable(void)
290{
291 return !!(vmx_capability.ept & VMX_EPTP_UC_BIT);
292}
293
294static inline bool cpu_has_vmx_eptp_writeback(void)
295{
296 return !!(vmx_capability.ept & VMX_EPTP_WB_BIT);
297}
298
299static inline bool cpu_has_vmx_ept_2m_page(void)
300{
301 return !!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT);
302}
303
Sheng Yangd56f5462008-04-25 10:13:16 +0800304static inline int cpu_has_vmx_invept_individual_addr(void)
305{
Sheng Yang04547152009-04-01 15:52:31 +0800306 return !!(vmx_capability.ept & VMX_EPT_EXTENT_INDIVIDUAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800307}
308
309static inline int cpu_has_vmx_invept_context(void)
310{
Sheng Yang04547152009-04-01 15:52:31 +0800311 return !!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800312}
313
314static inline int cpu_has_vmx_invept_global(void)
315{
Sheng Yang04547152009-04-01 15:52:31 +0800316 return !!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT);
Sheng Yangd56f5462008-04-25 10:13:16 +0800317}
318
319static inline int cpu_has_vmx_ept(void)
320{
Sheng Yang04547152009-04-01 15:52:31 +0800321 return vmcs_config.cpu_based_2nd_exec_ctrl &
322 SECONDARY_EXEC_ENABLE_EPT;
Sheng Yangd56f5462008-04-25 10:13:16 +0800323}
324
Nitin A Kamble3a624e22009-06-08 11:34:16 -0700325static inline int cpu_has_vmx_unrestricted_guest(void)
326{
327 return vmcs_config.cpu_based_2nd_exec_ctrl &
328 SECONDARY_EXEC_UNRESTRICTED_GUEST;
329}
330
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +0800331static inline int cpu_has_vmx_ple(void)
332{
333 return vmcs_config.cpu_based_2nd_exec_ctrl &
334 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
335}
336
Sheng Yangf78e0e22007-10-29 09:40:42 +0800337static inline int vm_need_virtualize_apic_accesses(struct kvm *kvm)
338{
Sheng Yang04547152009-04-01 15:52:31 +0800339 return flexpriority_enabled &&
340 (cpu_has_vmx_virtualize_apic_accesses()) &&
341 (irqchip_in_kernel(kvm));
Sheng Yangf78e0e22007-10-29 09:40:42 +0800342}
343
Sheng Yang2384d2b2008-01-17 15:14:33 +0800344static inline int cpu_has_vmx_vpid(void)
345{
Sheng Yang04547152009-04-01 15:52:31 +0800346 return vmcs_config.cpu_based_2nd_exec_ctrl &
347 SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang2384d2b2008-01-17 15:14:33 +0800348}
349
Sheng Yangf08864b2008-05-15 18:23:25 +0800350static inline int cpu_has_virtual_nmis(void)
351{
352 return vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS;
353}
354
Sheng Yang04547152009-04-01 15:52:31 +0800355static inline bool report_flexpriority(void)
356{
357 return flexpriority_enabled;
358}
359
Rusty Russell8b9cf982007-07-30 16:31:43 +1000360static int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
Avi Kivity7725f0b2006-12-13 00:34:01 -0800361{
362 int i;
363
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400364 for (i = 0; i < vmx->nmsrs; ++i)
Avi Kivity26bb0982009-09-07 11:14:12 +0300365 if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300366 return i;
367 return -1;
368}
369
Sheng Yang2384d2b2008-01-17 15:14:33 +0800370static inline void __invvpid(int ext, u16 vpid, gva_t gva)
371{
372 struct {
373 u64 vpid : 16;
374 u64 rsvd : 48;
375 u64 gva;
376 } operand = { vpid, 0, gva };
377
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300378 asm volatile (__ex(ASM_VMX_INVVPID)
Sheng Yang2384d2b2008-01-17 15:14:33 +0800379 /* CF==1 or ZF==1 --> rc = -1 */
380 "; ja 1f ; ud2 ; 1:"
381 : : "a"(&operand), "c"(ext) : "cc", "memory");
382}
383
Sheng Yang14394422008-04-28 12:24:45 +0800384static inline void __invept(int ext, u64 eptp, gpa_t gpa)
385{
386 struct {
387 u64 eptp, gpa;
388 } operand = {eptp, gpa};
389
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300390 asm volatile (__ex(ASM_VMX_INVEPT)
Sheng Yang14394422008-04-28 12:24:45 +0800391 /* CF==1 or ZF==1 --> rc = -1 */
392 "; ja 1f ; ud2 ; 1:\n"
393 : : "a" (&operand), "c" (ext) : "cc", "memory");
394}
395
Avi Kivity26bb0982009-09-07 11:14:12 +0300396static struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
Eddie Donga75beee2007-05-17 18:55:15 +0300397{
398 int i;
399
Rusty Russell8b9cf982007-07-30 16:31:43 +1000400 i = __find_msr_index(vmx, msr);
Eddie Donga75beee2007-05-17 18:55:15 +0300401 if (i >= 0)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400402 return &vmx->guest_msrs[i];
Al Viro8b6d44c2007-02-09 16:38:40 +0000403 return NULL;
Avi Kivity7725f0b2006-12-13 00:34:01 -0800404}
405
Avi Kivity6aa8b732006-12-10 02:21:36 -0800406static void vmcs_clear(struct vmcs *vmcs)
407{
408 u64 phys_addr = __pa(vmcs);
409 u8 error;
410
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300411 asm volatile (__ex(ASM_VMX_VMCLEAR_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800412 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
413 : "cc", "memory");
414 if (error)
415 printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n",
416 vmcs, phys_addr);
417}
418
419static void __vcpu_clear(void *arg)
420{
Rusty Russell8b9cf982007-07-30 16:31:43 +1000421 struct vcpu_vmx *vmx = arg;
Ingo Molnard3b2c332007-01-05 16:36:23 -0800422 int cpu = raw_smp_processor_id();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423
Rusty Russell8b9cf982007-07-30 16:31:43 +1000424 if (vmx->vcpu.cpu == cpu)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400425 vmcs_clear(vmx->vmcs);
426 if (per_cpu(current_vmcs, cpu) == vmx->vmcs)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 per_cpu(current_vmcs, cpu) = NULL;
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800428 rdtscll(vmx->vcpu.arch.host_tsc);
Avi Kivity543e4242008-05-13 16:22:47 +0300429 list_del(&vmx->local_vcpus_link);
430 vmx->vcpu.cpu = -1;
431 vmx->launched = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800432}
433
Rusty Russell8b9cf982007-07-30 16:31:43 +1000434static void vcpu_clear(struct vcpu_vmx *vmx)
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800435{
Avi Kivityeae5ecb2007-09-30 10:50:12 +0200436 if (vmx->vcpu.cpu == -1)
437 return;
Jens Axboe8691e5a2008-06-06 11:18:06 +0200438 smp_call_function_single(vmx->vcpu.cpu, __vcpu_clear, vmx, 1);
Avi Kivity8d0be2b2007-02-12 00:54:46 -0800439}
440
Sheng Yang2384d2b2008-01-17 15:14:33 +0800441static inline void vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
442{
443 if (vmx->vpid == 0)
444 return;
445
446 __invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
447}
448
Sheng Yang14394422008-04-28 12:24:45 +0800449static inline void ept_sync_global(void)
450{
451 if (cpu_has_vmx_invept_global())
452 __invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
453}
454
455static inline void ept_sync_context(u64 eptp)
456{
Avi Kivity089d0342009-03-23 18:26:32 +0200457 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800458 if (cpu_has_vmx_invept_context())
459 __invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
460 else
461 ept_sync_global();
462 }
463}
464
465static inline void ept_sync_individual_addr(u64 eptp, gpa_t gpa)
466{
Avi Kivity089d0342009-03-23 18:26:32 +0200467 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +0800468 if (cpu_has_vmx_invept_individual_addr())
469 __invept(VMX_EPT_EXTENT_INDIVIDUAL_ADDR,
470 eptp, gpa);
471 else
472 ept_sync_context(eptp);
473 }
474}
475
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476static unsigned long vmcs_readl(unsigned long field)
477{
478 unsigned long value;
479
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300480 asm volatile (__ex(ASM_VMX_VMREAD_RDX_RAX)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 : "=a"(value) : "d"(field) : "cc");
482 return value;
483}
484
485static u16 vmcs_read16(unsigned long field)
486{
487 return vmcs_readl(field);
488}
489
490static u32 vmcs_read32(unsigned long field)
491{
492 return vmcs_readl(field);
493}
494
495static u64 vmcs_read64(unsigned long field)
496{
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800497#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 return vmcs_readl(field);
499#else
500 return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32);
501#endif
502}
503
Avi Kivitye52de1b2007-01-05 16:36:56 -0800504static noinline void vmwrite_error(unsigned long field, unsigned long value)
505{
506 printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n",
507 field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
508 dump_stack();
509}
510
Avi Kivity6aa8b732006-12-10 02:21:36 -0800511static void vmcs_writel(unsigned long field, unsigned long value)
512{
513 u8 error;
514
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300515 asm volatile (__ex(ASM_VMX_VMWRITE_RAX_RDX) "; setna %0"
Mike Dayd77c26f2007-10-08 09:02:08 -0400516 : "=q"(error) : "a"(value), "d"(field) : "cc");
Avi Kivitye52de1b2007-01-05 16:36:56 -0800517 if (unlikely(error))
518 vmwrite_error(field, value);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800519}
520
521static void vmcs_write16(unsigned long field, u16 value)
522{
523 vmcs_writel(field, value);
524}
525
526static void vmcs_write32(unsigned long field, u32 value)
527{
528 vmcs_writel(field, value);
529}
530
531static void vmcs_write64(unsigned long field, u64 value)
532{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800533 vmcs_writel(field, value);
Avi Kivity7682f2d2008-05-12 19:25:43 +0300534#ifndef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800535 asm volatile ("");
536 vmcs_writel(field+1, value >> 32);
537#endif
538}
539
Anthony Liguori2ab455c2007-04-27 09:29:49 +0300540static void vmcs_clear_bits(unsigned long field, u32 mask)
541{
542 vmcs_writel(field, vmcs_readl(field) & ~mask);
543}
544
545static void vmcs_set_bits(unsigned long field, u32 mask)
546{
547 vmcs_writel(field, vmcs_readl(field) | mask);
548}
549
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300550static void update_exception_bitmap(struct kvm_vcpu *vcpu)
551{
552 u32 eb;
553
Andi Kleena0861c02009-06-08 17:37:09 +0800554 eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR);
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300555 if (!vcpu->fpu_active)
556 eb |= 1u << NM_VECTOR;
Avi Kivitye8a48342009-09-01 16:06:25 +0300557 /*
558 * Unconditionally intercept #DB so we can maintain dr6 without
559 * reading it every exit.
560 */
561 eb |= 1u << DB_VECTOR;
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100562 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100563 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
564 eb |= 1u << BP_VECTOR;
565 }
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300566 if (to_vmx(vcpu)->rmode.vm86_active)
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300567 eb = ~0;
Avi Kivity089d0342009-03-23 18:26:32 +0200568 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +0800569 eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
Avi Kivityabd3f2d2007-05-02 17:57:40 +0300570 vmcs_write32(EXCEPTION_BITMAP, eb);
571}
572
Avi Kivity33ed6322007-05-02 16:54:03 +0300573static void reload_tss(void)
574{
Avi Kivity33ed6322007-05-02 16:54:03 +0300575 /*
576 * VT restores TR but not its size. Useless.
577 */
578 struct descriptor_table gdt;
Avi Kivitya5f61302008-02-20 17:57:21 +0200579 struct desc_struct *descs;
Avi Kivity33ed6322007-05-02 16:54:03 +0300580
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300581 kvm_get_gdt(&gdt);
Avi Kivity33ed6322007-05-02 16:54:03 +0300582 descs = (void *)gdt.base;
583 descs[GDT_ENTRY_TSS].type = 9; /* available TSS */
584 load_TR_desc();
Avi Kivity33ed6322007-05-02 16:54:03 +0300585}
586
Avi Kivity92c0d902009-10-29 11:00:16 +0200587static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
Eddie Dong2cc51562007-05-21 07:28:09 +0300588{
Roel Kluin3a34a882009-08-04 02:08:45 -0700589 u64 guest_efer;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300590 u64 ignore_bits;
Eddie Dong2cc51562007-05-21 07:28:09 +0300591
Avi Kivity26bb0982009-09-07 11:14:12 +0300592 guest_efer = vmx->vcpu.arch.shadow_efer;
Roel Kluin3a34a882009-08-04 02:08:45 -0700593
Avi Kivity51c6cf62007-08-29 03:48:05 +0300594 /*
595 * NX is emulated; LMA and LME handled by hardware; SCE meaninless
596 * outside long mode
597 */
598 ignore_bits = EFER_NX | EFER_SCE;
599#ifdef CONFIG_X86_64
600 ignore_bits |= EFER_LMA | EFER_LME;
601 /* SCE is meaningful only in long mode on Intel */
602 if (guest_efer & EFER_LMA)
603 ignore_bits &= ~(u64)EFER_SCE;
604#endif
Avi Kivity51c6cf62007-08-29 03:48:05 +0300605 guest_efer &= ~ignore_bits;
606 guest_efer |= host_efer & ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300607 vmx->guest_msrs[efer_offset].data = guest_efer;
Avi Kivityd5696722009-12-02 12:28:47 +0200608 vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
Avi Kivity26bb0982009-09-07 11:14:12 +0300609 return true;
Avi Kivity51c6cf62007-08-29 03:48:05 +0300610}
611
Avi Kivity04d2cc72007-09-10 18:10:54 +0300612static void vmx_save_host_state(struct kvm_vcpu *vcpu)
Avi Kivity33ed6322007-05-02 16:54:03 +0300613{
Avi Kivity04d2cc72007-09-10 18:10:54 +0300614 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +0300615 int i;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300616
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
Avi Kivity44ea2b12009-09-06 15:55:37 +0300652 if (is_long_mode(&vmx->vcpu)) {
653 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
654 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
655 }
Avi Kivity707c0872007-05-02 17:33:43 +0300656#endif
Avi Kivity26bb0982009-09-07 11:14:12 +0300657 for (i = 0; i < vmx->save_nmsrs; ++i)
658 kvm_set_shared_msr(vmx->guest_msrs[i].index,
Avi Kivityd5696722009-12-02 12:28:47 +0200659 vmx->guest_msrs[i].data,
660 vmx->guest_msrs[i].mask);
Avi Kivity33ed6322007-05-02 16:54:03 +0300661}
662
Avi Kivitya9b21b62008-06-24 11:48:49 +0300663static void __vmx_load_host_state(struct vcpu_vmx *vmx)
Avi Kivity33ed6322007-05-02 16:54:03 +0300664{
Avi Kivity15ad7142007-07-11 18:17:21 +0300665 unsigned long flags;
Avi Kivity33ed6322007-05-02 16:54:03 +0300666
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400667 if (!vmx->host_state.loaded)
Avi Kivity33ed6322007-05-02 16:54:03 +0300668 return;
669
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200670 ++vmx->vcpu.stat.host_state_reload;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400671 vmx->host_state.loaded = 0;
Laurent Vivier152d3f22007-08-23 16:33:11 +0200672 if (vmx->host_state.fs_reload_needed)
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300673 kvm_load_fs(vmx->host_state.fs_sel);
Laurent Vivier152d3f22007-08-23 16:33:11 +0200674 if (vmx->host_state.gs_ldt_reload_needed) {
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300675 kvm_load_ldt(vmx->host_state.ldt_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300676 /*
677 * If we have to reload gs, we must take care to
678 * preserve our gs base.
679 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300680 local_irq_save(flags);
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300681 kvm_load_gs(vmx->host_state.gs_sel);
Avi Kivity33ed6322007-05-02 16:54:03 +0300682#ifdef CONFIG_X86_64
683 wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE));
684#endif
Avi Kivity15ad7142007-07-11 18:17:21 +0300685 local_irq_restore(flags);
Avi Kivity33ed6322007-05-02 16:54:03 +0300686 }
Laurent Vivier152d3f22007-08-23 16:33:11 +0200687 reload_tss();
Avi Kivity44ea2b12009-09-06 15:55:37 +0300688#ifdef CONFIG_X86_64
689 if (is_long_mode(&vmx->vcpu)) {
690 rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
691 wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
692 }
693#endif
Avi Kivity33ed6322007-05-02 16:54:03 +0300694}
695
Avi Kivitya9b21b62008-06-24 11:48:49 +0300696static void vmx_load_host_state(struct vcpu_vmx *vmx)
697{
698 preempt_disable();
699 __vmx_load_host_state(vmx);
700 preempt_enable();
701}
702
Avi Kivity6aa8b732006-12-10 02:21:36 -0800703/*
704 * Switches to specified vcpu, until a matching vcpu_put(), but assumes
705 * vcpu mutex is already taken.
706 */
Avi Kivity15ad7142007-07-11 18:17:21 +0300707static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400709 struct vcpu_vmx *vmx = to_vmx(vcpu);
710 u64 phys_addr = __pa(vmx->vmcs);
Avi Kivity019960a2008-03-04 10:44:51 +0200711 u64 tsc_this, delta, new_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712
Eddie Donga3d7f852007-09-03 16:15:12 +0300713 if (vcpu->cpu != cpu) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000714 vcpu_clear(vmx);
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300715 kvm_migrate_timers(vcpu);
Marcelo Tosattieb5109e2009-10-01 19:16:58 -0300716 set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
Avi Kivity543e4242008-05-13 16:22:47 +0300717 local_irq_disable();
718 list_add(&vmx->local_vcpus_link,
719 &per_cpu(vcpus_on_cpu, cpu));
720 local_irq_enable();
Eddie Donga3d7f852007-09-03 16:15:12 +0300721 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800722
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400723 if (per_cpu(current_vmcs, cpu) != vmx->vmcs) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800724 u8 error;
725
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400726 per_cpu(current_vmcs, cpu) = vmx->vmcs;
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300727 asm volatile (__ex(ASM_VMX_VMPTRLD_RAX) "; setna %0"
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728 : "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
729 : "cc");
730 if (error)
731 printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n",
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400732 vmx->vmcs, phys_addr);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800733 }
734
735 if (vcpu->cpu != cpu) {
736 struct descriptor_table dt;
737 unsigned long sysenter_esp;
738
739 vcpu->cpu = cpu;
740 /*
741 * Linux uses per-cpu TSS and GDT, so set these when switching
742 * processors.
743 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +0300744 vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
745 kvm_get_gdt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800746 vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
747
748 rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
749 vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
Avi Kivity77002702007-06-13 19:55:28 +0300750
751 /*
752 * Make sure the time stamp counter is monotonous.
753 */
754 rdtscll(tsc_this);
Avi Kivity019960a2008-03-04 10:44:51 +0200755 if (tsc_this < vcpu->arch.host_tsc) {
756 delta = vcpu->arch.host_tsc - tsc_this;
757 new_offset = vmcs_read64(TSC_OFFSET) + delta;
758 vmcs_write64(TSC_OFFSET, new_offset);
759 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800761}
762
763static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
764{
Avi Kivitya9b21b62008-06-24 11:48:49 +0300765 __vmx_load_host_state(to_vmx(vcpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766}
767
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300768static void vmx_fpu_activate(struct kvm_vcpu *vcpu)
769{
770 if (vcpu->fpu_active)
771 return;
772 vcpu->fpu_active = 1;
Rusty Russell707d92f2007-07-17 23:19:08 +1000773 vmcs_clear_bits(GUEST_CR0, X86_CR0_TS);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800774 if (vcpu->arch.cr0 & X86_CR0_TS)
Rusty Russell707d92f2007-07-17 23:19:08 +1000775 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300776 update_exception_bitmap(vcpu);
777}
778
779static void vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
780{
781 if (!vcpu->fpu_active)
782 return;
783 vcpu->fpu_active = 0;
Rusty Russell707d92f2007-07-17 23:19:08 +1000784 vmcs_set_bits(GUEST_CR0, X86_CR0_TS);
Avi Kivity5fd86fc2007-05-02 20:40:00 +0300785 update_exception_bitmap(vcpu);
786}
787
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
789{
Avi Kivity345dcaa2009-08-12 15:29:37 +0300790 unsigned long rflags;
791
792 rflags = vmcs_readl(GUEST_RFLAGS);
793 if (to_vmx(vcpu)->rmode.vm86_active)
794 rflags &= ~(unsigned long)(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
795 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796}
797
798static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
799{
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300800 if (to_vmx(vcpu)->rmode.vm86_active)
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +0100801 rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802 vmcs_writel(GUEST_RFLAGS, rflags);
803}
804
Glauber Costa2809f5d2009-05-12 16:21:05 -0400805static u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
806{
807 u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
808 int ret = 0;
809
810 if (interruptibility & GUEST_INTR_STATE_STI)
811 ret |= X86_SHADOW_INT_STI;
812 if (interruptibility & GUEST_INTR_STATE_MOV_SS)
813 ret |= X86_SHADOW_INT_MOV_SS;
814
815 return ret & mask;
816}
817
818static void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
819{
820 u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
821 u32 interruptibility = interruptibility_old;
822
823 interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
824
825 if (mask & X86_SHADOW_INT_MOV_SS)
826 interruptibility |= GUEST_INTR_STATE_MOV_SS;
827 if (mask & X86_SHADOW_INT_STI)
828 interruptibility |= GUEST_INTR_STATE_STI;
829
830 if ((interruptibility != interruptibility_old))
831 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
832}
833
Avi Kivity6aa8b732006-12-10 02:21:36 -0800834static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
835{
836 unsigned long rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300838 rip = kvm_rip_read(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800839 rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300840 kvm_rip_write(vcpu, rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
Glauber Costa2809f5d2009-05-12 16:21:05 -0400842 /* skipping an emulated instruction also counts */
843 vmx_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800844}
845
Avi Kivity298101d2007-11-25 13:41:11 +0200846static void vmx_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
847 bool has_error_code, u32 error_code)
848{
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200849 struct vcpu_vmx *vmx = to_vmx(vcpu);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100850 u32 intr_info = nr | INTR_INFO_VALID_MASK;
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200851
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100852 if (has_error_code) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200853 vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100854 intr_info |= INTR_INFO_DELIVER_CODE_MASK;
855 }
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200856
Avi Kivity7ffd92c2009-06-09 14:10:45 +0300857 if (vmx->rmode.vm86_active) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200858 vmx->rmode.irq.pending = true;
859 vmx->rmode.irq.vector = nr;
860 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +0300861 if (kvm_exception_is_soft(nr))
862 vmx->rmode.irq.rip +=
863 vmx->vcpu.arch.event_exit_inst_len;
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100864 intr_info |= INTR_TYPE_SOFT_INTR;
865 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Jan Kiszka77ab6db2008-07-14 12:28:51 +0200866 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
867 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
868 return;
869 }
870
Gleb Natapov66fd3f72009-05-11 13:35:50 +0300871 if (kvm_exception_is_soft(nr)) {
872 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
873 vmx->vcpu.arch.event_exit_inst_len);
Jan Kiszka8ab2d2e2008-12-15 13:52:10 +0100874 intr_info |= INTR_TYPE_SOFT_EXCEPTION;
875 } else
876 intr_info |= INTR_TYPE_HARD_EXCEPTION;
877
878 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
Avi Kivity298101d2007-11-25 13:41:11 +0200879}
880
Avi Kivity6aa8b732006-12-10 02:21:36 -0800881/*
Eddie Donga75beee2007-05-17 18:55:15 +0300882 * Swap MSR entry in host/guest MSR entry array.
883 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000884static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
Eddie Donga75beee2007-05-17 18:55:15 +0300885{
Avi Kivity26bb0982009-09-07 11:14:12 +0300886 struct shared_msr_entry tmp;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400887
888 tmp = vmx->guest_msrs[to];
889 vmx->guest_msrs[to] = vmx->guest_msrs[from];
890 vmx->guest_msrs[from] = tmp;
Eddie Donga75beee2007-05-17 18:55:15 +0300891}
892
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{
Avi Kivity26bb0982009-09-07 11:14:12 +0300900 int save_nmsrs, index;
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)) {
Rusty Russell8b9cf982007-07-30 16:31:43 +1000907 index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
Eddie Donga75beee2007-05-17 18:55:15 +0300908 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000909 move_msr_up(vmx, index, save_nmsrs++);
910 index = __find_msr_index(vmx, MSR_LSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300911 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000912 move_msr_up(vmx, index, save_nmsrs++);
913 index = __find_msr_index(vmx, MSR_CSTAR);
Eddie Donga75beee2007-05-17 18:55:15 +0300914 if (index >= 0)
Rusty Russell8b9cf982007-07-30 16:31:43 +1000915 move_msr_up(vmx, index, save_nmsrs++);
Eddie Donga75beee2007-05-17 18:55:15 +0300916 /*
917 * MSR_K6_STAR is only needed on long mode guests, and only
918 * if efer.sce is enabled.
919 */
Rusty Russell8b9cf982007-07-30 16:31:43 +1000920 index = __find_msr_index(vmx, MSR_K6_STAR);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800921 if ((index >= 0) && (vmx->vcpu.arch.shadow_efer & EFER_SCE))
Rusty Russell8b9cf982007-07-30 16:31:43 +1000922 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300923 }
Eddie Donga75beee2007-05-17 18:55:15 +0300924#endif
Avi Kivity92c0d902009-10-29 11:00:16 +0200925 index = __find_msr_index(vmx, MSR_EFER);
926 if (index >= 0 && update_transition_efer(vmx, index))
Avi Kivity26bb0982009-09-07 11:14:12 +0300927 move_msr_up(vmx, index, save_nmsrs++);
Avi Kivity4d56c8a2007-04-19 14:28:44 +0300928
Avi Kivity26bb0982009-09-07 11:14:12 +0300929 vmx->save_nmsrs = save_nmsrs;
Avi Kivity58972972009-02-24 22:26:47 +0200930
931 if (cpu_has_vmx_msr_bitmap()) {
932 if (is_long_mode(&vmx->vcpu))
933 msr_bitmap = vmx_msr_bitmap_longmode;
934 else
935 msr_bitmap = vmx_msr_bitmap_legacy;
936
937 vmcs_write64(MSR_BITMAP, __pa(msr_bitmap));
938 }
Avi Kivitye38aea32007-04-19 13:22:48 +0300939}
940
941/*
Avi Kivity6aa8b732006-12-10 02:21:36 -0800942 * reads and returns guest's timestamp counter "register"
943 * guest_tsc = host_tsc + tsc_offset -- 21.3
944 */
945static u64 guest_read_tsc(void)
946{
947 u64 host_tsc, tsc_offset;
948
949 rdtscll(host_tsc);
950 tsc_offset = vmcs_read64(TSC_OFFSET);
951 return host_tsc + tsc_offset;
952}
953
954/*
955 * writes 'guest_tsc' into guest's timestamp counter "register"
956 * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc
957 */
Marcelo Tosatti53f658b2008-12-11 20:45:05 +0100958static void guest_write_tsc(u64 guest_tsc, u64 host_tsc)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959{
Avi Kivity6aa8b732006-12-10 02:21:36 -0800960 vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc);
961}
962
Avi Kivity6aa8b732006-12-10 02:21:36 -0800963/*
964 * Reads an msr value (of 'msr_index') into 'pdata'.
965 * Returns 0 on success, non-0 otherwise.
966 * Assumes vcpu_load() was already called.
967 */
968static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata)
969{
970 u64 data;
Avi Kivity26bb0982009-09-07 11:14:12 +0300971 struct shared_msr_entry *msr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800972
973 if (!pdata) {
974 printk(KERN_ERR "BUG: get_msr called with NULL pdata\n");
975 return -EINVAL;
976 }
977
978 switch (msr_index) {
Avi Kivity05b3e0c2006-12-13 00:33:45 -0800979#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980 case MSR_FS_BASE:
981 data = vmcs_readl(GUEST_FS_BASE);
982 break;
983 case MSR_GS_BASE:
984 data = vmcs_readl(GUEST_GS_BASE);
985 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +0300986 case MSR_KERNEL_GS_BASE:
987 vmx_load_host_state(to_vmx(vcpu));
988 data = to_vmx(vcpu)->msr_guest_kernel_gs_base;
989 break;
Avi Kivity26bb0982009-09-07 11:14:12 +0300990#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 case MSR_EFER:
Avi Kivity3bab1f52006-12-29 16:49:48 -0800992 return kvm_get_msr_common(vcpu, msr_index, pdata);
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +0530993 case MSR_IA32_TSC:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994 data = guest_read_tsc();
995 break;
996 case MSR_IA32_SYSENTER_CS:
997 data = vmcs_read32(GUEST_SYSENTER_CS);
998 break;
999 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001000 data = vmcs_readl(GUEST_SYSENTER_EIP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001 break;
1002 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001003 data = vmcs_readl(GUEST_SYSENTER_ESP);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001004 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001005 default:
Avi Kivity26bb0982009-09-07 11:14:12 +03001006 vmx_load_host_state(to_vmx(vcpu));
Rusty Russell8b9cf982007-07-30 16:31:43 +10001007 msr = find_msr_entry(to_vmx(vcpu), msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001008 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001009 vmx_load_host_state(to_vmx(vcpu));
Avi Kivity3bab1f52006-12-29 16:49:48 -08001010 data = msr->data;
1011 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012 }
Avi Kivity3bab1f52006-12-29 16:49:48 -08001013 return kvm_get_msr_common(vcpu, msr_index, pdata);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001014 }
1015
1016 *pdata = data;
1017 return 0;
1018}
1019
1020/*
1021 * Writes msr value into into the appropriate "register".
1022 * Returns 0 on success, non-0 otherwise.
1023 * Assumes vcpu_load() was already called.
1024 */
1025static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data)
1026{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001027 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001028 struct shared_msr_entry *msr;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001029 u64 host_tsc;
Eddie Dong2cc51562007-05-21 07:28:09 +03001030 int ret = 0;
1031
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032 switch (msr_index) {
Avi Kivity3bab1f52006-12-29 16:49:48 -08001033 case MSR_EFER:
Avi Kivitya9b21b62008-06-24 11:48:49 +03001034 vmx_load_host_state(vmx);
Eddie Dong2cc51562007-05-21 07:28:09 +03001035 ret = kvm_set_msr_common(vcpu, msr_index, data);
Eddie Dong2cc51562007-05-21 07:28:09 +03001036 break;
Avi Kivity16175a72009-03-23 22:13:44 +02001037#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 case MSR_FS_BASE:
1039 vmcs_writel(GUEST_FS_BASE, data);
1040 break;
1041 case MSR_GS_BASE:
1042 vmcs_writel(GUEST_GS_BASE, data);
1043 break;
Avi Kivity44ea2b12009-09-06 15:55:37 +03001044 case MSR_KERNEL_GS_BASE:
1045 vmx_load_host_state(vmx);
1046 vmx->msr_guest_kernel_gs_base = data;
1047 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001048#endif
1049 case MSR_IA32_SYSENTER_CS:
1050 vmcs_write32(GUEST_SYSENTER_CS, data);
1051 break;
1052 case MSR_IA32_SYSENTER_EIP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001053 vmcs_writel(GUEST_SYSENTER_EIP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001054 break;
1055 case MSR_IA32_SYSENTER_ESP:
Avi Kivityf5b42c32007-03-06 12:05:53 +02001056 vmcs_writel(GUEST_SYSENTER_ESP, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 break;
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05301058 case MSR_IA32_TSC:
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01001059 rdtscll(host_tsc);
1060 guest_write_tsc(data, host_tsc);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 break;
Sheng Yang468d4722008-10-09 16:01:55 +08001062 case MSR_IA32_CR_PAT:
1063 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
1064 vmcs_write64(GUEST_IA32_PAT, data);
1065 vcpu->arch.pat = data;
1066 break;
1067 }
1068 /* Otherwise falls through to kvm_set_msr_common */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 default:
Rusty Russell8b9cf982007-07-30 16:31:43 +10001070 msr = find_msr_entry(vmx, msr_index);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001071 if (msr) {
Gleb Natapov542423b2009-08-27 15:07:30 +03001072 vmx_load_host_state(vmx);
Avi Kivity3bab1f52006-12-29 16:49:48 -08001073 msr->data = data;
1074 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001075 }
Eddie Dong2cc51562007-05-21 07:28:09 +03001076 ret = kvm_set_msr_common(vcpu, msr_index, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001077 }
1078
Eddie Dong2cc51562007-05-21 07:28:09 +03001079 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080}
1081
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001082static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001083{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001084 __set_bit(reg, (unsigned long *)&vcpu->arch.regs_avail);
1085 switch (reg) {
1086 case VCPU_REGS_RSP:
1087 vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
1088 break;
1089 case VCPU_REGS_RIP:
1090 vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
1091 break;
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001092 case VCPU_EXREG_PDPTR:
1093 if (enable_ept)
1094 ept_save_pdptrs(vcpu);
1095 break;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001096 default:
1097 break;
1098 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099}
1100
Jan Kiszka355be0b2009-10-03 00:31:21 +02001101static void set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001102{
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001103 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1104 vmcs_writel(GUEST_DR7, dbg->arch.debugreg[7]);
1105 else
1106 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
1107
Avi Kivityabd3f2d2007-05-02 17:57:40 +03001108 update_exception_bitmap(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109}
1110
1111static __init int cpu_has_kvm_support(void)
1112{
Eduardo Habkost6210e372008-11-17 19:03:16 -02001113 return cpu_has_vmx();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114}
1115
1116static __init int vmx_disabled_by_bios(void)
1117{
1118 u64 msr;
1119
1120 rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001121 return (msr & (FEATURE_CONTROL_LOCKED |
1122 FEATURE_CONTROL_VMXON_ENABLED))
1123 == FEATURE_CONTROL_LOCKED;
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001124 /* locked but not enabled */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125}
1126
Alexander Graf10474ae2009-09-15 11:37:46 +02001127static int hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001128{
1129 int cpu = raw_smp_processor_id();
1130 u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
1131 u64 old;
1132
Alexander Graf10474ae2009-09-15 11:37:46 +02001133 if (read_cr4() & X86_CR4_VMXE)
1134 return -EBUSY;
1135
Avi Kivity543e4242008-05-13 16:22:47 +03001136 INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001137 rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
Sheng Yang9ea542f2008-09-11 15:27:49 +08001138 if ((old & (FEATURE_CONTROL_LOCKED |
1139 FEATURE_CONTROL_VMXON_ENABLED))
1140 != (FEATURE_CONTROL_LOCKED |
1141 FEATURE_CONTROL_VMXON_ENABLED))
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142 /* enable and lock */
Yang, Sheng62b3ffb2007-07-25 12:17:06 +03001143 wrmsrl(MSR_IA32_FEATURE_CONTROL, old |
Sheng Yang9ea542f2008-09-11 15:27:49 +08001144 FEATURE_CONTROL_LOCKED |
1145 FEATURE_CONTROL_VMXON_ENABLED);
Rusty Russell66aee912007-07-17 23:34:16 +10001146 write_cr4(read_cr4() | X86_CR4_VMXE); /* FIXME: not cpu hotplug safe */
Avi Kivity4ecac3f2008-05-13 13:23:38 +03001147 asm volatile (ASM_VMX_VMXON_RAX
1148 : : "a"(&phys_addr), "m"(phys_addr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001149 : "memory", "cc");
Alexander Graf10474ae2009-09-15 11:37:46 +02001150
1151 ept_sync_global();
1152
1153 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001154}
1155
Avi Kivity543e4242008-05-13 16:22:47 +03001156static void vmclear_local_vcpus(void)
1157{
1158 int cpu = raw_smp_processor_id();
1159 struct vcpu_vmx *vmx, *n;
1160
1161 list_for_each_entry_safe(vmx, n, &per_cpu(vcpus_on_cpu, cpu),
1162 local_vcpus_link)
1163 __vcpu_clear(vmx);
1164}
1165
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001166
1167/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
1168 * tricks.
1169 */
1170static void kvm_cpu_vmxoff(void)
1171{
1172 asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc");
1173 write_cr4(read_cr4() & ~X86_CR4_VMXE);
1174}
1175
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176static void hardware_disable(void *garbage)
1177{
Avi Kivity543e4242008-05-13 16:22:47 +03001178 vmclear_local_vcpus();
Eduardo Habkost710ff4a2008-11-17 19:03:18 -02001179 kvm_cpu_vmxoff();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001180}
1181
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001182static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
Mike Dayd77c26f2007-10-08 09:02:08 -04001183 u32 msr, u32 *result)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184{
1185 u32 vmx_msr_low, vmx_msr_high;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001186 u32 ctl = ctl_min | ctl_opt;
1187
1188 rdmsr(msr, vmx_msr_low, vmx_msr_high);
1189
1190 ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
1191 ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
1192
1193 /* Ensure minimum (required) set of control bits are supported. */
1194 if (ctl_min & ~ctl)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001195 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001196
1197 *result = ctl;
1198 return 0;
1199}
1200
Yang, Sheng002c7f72007-07-31 14:23:01 +03001201static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf)
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001202{
1203 u32 vmx_msr_low, vmx_msr_high;
Sheng Yangd56f5462008-04-25 10:13:16 +08001204 u32 min, opt, min2, opt2;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001205 u32 _pin_based_exec_control = 0;
1206 u32 _cpu_based_exec_control = 0;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001207 u32 _cpu_based_2nd_exec_control = 0;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001208 u32 _vmexit_control = 0;
1209 u32 _vmentry_control = 0;
1210
1211 min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
Sheng Yangf08864b2008-05-15 18:23:25 +08001212 opt = PIN_BASED_VIRTUAL_NMIS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001213 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
1214 &_pin_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001215 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001216
1217 min = CPU_BASED_HLT_EXITING |
1218#ifdef CONFIG_X86_64
1219 CPU_BASED_CR8_LOAD_EXITING |
1220 CPU_BASED_CR8_STORE_EXITING |
1221#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001222 CPU_BASED_CR3_LOAD_EXITING |
1223 CPU_BASED_CR3_STORE_EXITING |
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001224 CPU_BASED_USE_IO_BITMAPS |
1225 CPU_BASED_MOV_DR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001226 CPU_BASED_USE_TSC_OFFSETING |
Sheng Yang59708672009-12-15 13:29:54 +08001227 CPU_BASED_MWAIT_EXITING |
1228 CPU_BASED_MONITOR_EXITING |
Marcelo Tosattia7052892008-09-23 13:18:35 -03001229 CPU_BASED_INVLPG_EXITING;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001230 opt = CPU_BASED_TPR_SHADOW |
Sheng Yang25c5f222008-03-28 13:18:56 +08001231 CPU_BASED_USE_MSR_BITMAPS |
Sheng Yangf78e0e22007-10-29 09:40:42 +08001232 CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001233 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
1234 &_cpu_based_exec_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001235 return -EIO;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08001236#ifdef CONFIG_X86_64
1237 if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
1238 _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
1239 ~CPU_BASED_CR8_STORE_EXITING;
1240#endif
Sheng Yangf78e0e22007-10-29 09:40:42 +08001241 if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
Sheng Yangd56f5462008-04-25 10:13:16 +08001242 min2 = 0;
1243 opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
Sheng Yang2384d2b2008-01-17 15:14:33 +08001244 SECONDARY_EXEC_WBINVD_EXITING |
Sheng Yangd56f5462008-04-25 10:13:16 +08001245 SECONDARY_EXEC_ENABLE_VPID |
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001246 SECONDARY_EXEC_ENABLE_EPT |
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001247 SECONDARY_EXEC_UNRESTRICTED_GUEST |
1248 SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yangd56f5462008-04-25 10:13:16 +08001249 if (adjust_vmx_controls(min2, opt2,
1250 MSR_IA32_VMX_PROCBASED_CTLS2,
Sheng Yangf78e0e22007-10-29 09:40:42 +08001251 &_cpu_based_2nd_exec_control) < 0)
1252 return -EIO;
1253 }
1254#ifndef CONFIG_X86_64
1255 if (!(_cpu_based_2nd_exec_control &
1256 SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
1257 _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
1258#endif
Sheng Yangd56f5462008-04-25 10:13:16 +08001259 if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
Marcelo Tosattia7052892008-09-23 13:18:35 -03001260 /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
1261 enabled */
Gleb Natapov5fff7d22009-08-27 18:41:30 +03001262 _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
1263 CPU_BASED_CR3_STORE_EXITING |
1264 CPU_BASED_INVLPG_EXITING);
Sheng Yangd56f5462008-04-25 10:13:16 +08001265 rdmsr(MSR_IA32_VMX_EPT_VPID_CAP,
1266 vmx_capability.ept, vmx_capability.vpid);
1267 }
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001268
1269 min = 0;
1270#ifdef CONFIG_X86_64
1271 min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
1272#endif
Sheng Yang468d4722008-10-09 16:01:55 +08001273 opt = VM_EXIT_SAVE_IA32_PAT | VM_EXIT_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001274 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
1275 &_vmexit_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001276 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001277
Sheng Yang468d4722008-10-09 16:01:55 +08001278 min = 0;
1279 opt = VM_ENTRY_LOAD_IA32_PAT;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001280 if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
1281 &_vmentry_control) < 0)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001282 return -EIO;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001284 rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001285
1286 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
1287 if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001288 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001289
1290#ifdef CONFIG_X86_64
1291 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
1292 if (vmx_msr_high & (1u<<16))
Yang, Sheng002c7f72007-07-31 14:23:01 +03001293 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001294#endif
1295
1296 /* Require Write-Back (WB) memory type for VMCS accesses. */
1297 if (((vmx_msr_high >> 18) & 15) != 6)
Yang, Sheng002c7f72007-07-31 14:23:01 +03001298 return -EIO;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001299
Yang, Sheng002c7f72007-07-31 14:23:01 +03001300 vmcs_conf->size = vmx_msr_high & 0x1fff;
1301 vmcs_conf->order = get_order(vmcs_config.size);
1302 vmcs_conf->revision_id = vmx_msr_low;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001303
Yang, Sheng002c7f72007-07-31 14:23:01 +03001304 vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
1305 vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
Sheng Yangf78e0e22007-10-29 09:40:42 +08001306 vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
Yang, Sheng002c7f72007-07-31 14:23:01 +03001307 vmcs_conf->vmexit_ctrl = _vmexit_control;
1308 vmcs_conf->vmentry_ctrl = _vmentry_control;
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001309
1310 return 0;
Nguyen Anh Quynhc68876f2006-12-29 16:49:54 -08001311}
Avi Kivity6aa8b732006-12-10 02:21:36 -08001312
1313static struct vmcs *alloc_vmcs_cpu(int cpu)
1314{
1315 int node = cpu_to_node(cpu);
1316 struct page *pages;
1317 struct vmcs *vmcs;
1318
Mel Gorman6484eb32009-06-16 15:31:54 -07001319 pages = alloc_pages_exact_node(node, GFP_KERNEL, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001320 if (!pages)
1321 return NULL;
1322 vmcs = page_address(pages);
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001323 memset(vmcs, 0, vmcs_config.size);
1324 vmcs->revision_id = vmcs_config.revision_id; /* vmcs revision id */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 return vmcs;
1326}
1327
1328static struct vmcs *alloc_vmcs(void)
1329{
Ingo Molnard3b2c332007-01-05 16:36:23 -08001330 return alloc_vmcs_cpu(raw_smp_processor_id());
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331}
1332
1333static void free_vmcs(struct vmcs *vmcs)
1334{
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03001335 free_pages((unsigned long)vmcs, vmcs_config.order);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336}
1337
Sam Ravnborg39959582007-06-01 00:47:13 -07001338static void free_kvm_area(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339{
1340 int cpu;
1341
Zachary Amsden3230bb42009-09-29 11:38:37 -10001342 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343 free_vmcs(per_cpu(vmxarea, cpu));
Zachary Amsden3230bb42009-09-29 11:38:37 -10001344 per_cpu(vmxarea, cpu) = NULL;
1345 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001346}
1347
Avi Kivity6aa8b732006-12-10 02:21:36 -08001348static __init int alloc_kvm_area(void)
1349{
1350 int cpu;
1351
Zachary Amsden3230bb42009-09-29 11:38:37 -10001352 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353 struct vmcs *vmcs;
1354
1355 vmcs = alloc_vmcs_cpu(cpu);
1356 if (!vmcs) {
1357 free_kvm_area();
1358 return -ENOMEM;
1359 }
1360
1361 per_cpu(vmxarea, cpu) = vmcs;
1362 }
1363 return 0;
1364}
1365
1366static __init int hardware_setup(void)
1367{
Yang, Sheng002c7f72007-07-31 14:23:01 +03001368 if (setup_vmcs_config(&vmcs_config) < 0)
1369 return -EIO;
Joerg Roedel50a37eb2008-01-31 14:57:38 +01001370
1371 if (boot_cpu_has(X86_FEATURE_NX))
1372 kvm_enable_efer_bits(EFER_NX);
1373
Sheng Yang93ba03c2009-04-01 15:52:32 +08001374 if (!cpu_has_vmx_vpid())
1375 enable_vpid = 0;
1376
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001377 if (!cpu_has_vmx_ept()) {
Sheng Yang93ba03c2009-04-01 15:52:32 +08001378 enable_ept = 0;
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001379 enable_unrestricted_guest = 0;
1380 }
1381
1382 if (!cpu_has_vmx_unrestricted_guest())
1383 enable_unrestricted_guest = 0;
Sheng Yang93ba03c2009-04-01 15:52:32 +08001384
1385 if (!cpu_has_vmx_flexpriority())
1386 flexpriority_enabled = 0;
1387
Gleb Natapov95ba8273132009-04-21 17:45:08 +03001388 if (!cpu_has_vmx_tpr_shadow())
1389 kvm_x86_ops->update_cr8_intercept = NULL;
1390
Marcelo Tosatti54dee992009-06-11 12:07:44 -03001391 if (enable_ept && !cpu_has_vmx_ept_2m_page())
1392 kvm_disable_largepages();
1393
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08001394 if (!cpu_has_vmx_ple())
1395 ple_gap = 0;
1396
Avi Kivity6aa8b732006-12-10 02:21:36 -08001397 return alloc_kvm_area();
1398}
1399
1400static __exit void hardware_unsetup(void)
1401{
1402 free_kvm_area();
1403}
1404
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save)
1406{
1407 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1408
Avi Kivity6af11b92007-03-19 13:18:10 +02001409 if (vmcs_readl(sf->base) == save->base && (save->base & AR_S_MASK)) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 vmcs_write16(sf->selector, save->selector);
1411 vmcs_writel(sf->base, save->base);
1412 vmcs_write32(sf->limit, save->limit);
1413 vmcs_write32(sf->ar_bytes, save->ar);
1414 } else {
1415 u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK)
1416 << AR_DPL_SHIFT;
1417 vmcs_write32(sf->ar_bytes, 0x93 | dpl);
1418 }
1419}
1420
1421static void enter_pmode(struct kvm_vcpu *vcpu)
1422{
1423 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001424 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001426 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001427 vmx->rmode.vm86_active = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001429 vmcs_writel(GUEST_TR_BASE, vmx->rmode.tr.base);
1430 vmcs_write32(GUEST_TR_LIMIT, vmx->rmode.tr.limit);
1431 vmcs_write32(GUEST_TR_AR_BYTES, vmx->rmode.tr.ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432
1433 flags = vmcs_readl(GUEST_RFLAGS);
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001434 flags &= ~(X86_EFLAGS_IOPL | X86_EFLAGS_VM);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001435 flags |= (vmx->rmode.save_iopl << IOPL_SHIFT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 vmcs_writel(GUEST_RFLAGS, flags);
1437
Rusty Russell66aee912007-07-17 23:34:16 +10001438 vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
1439 (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001440
1441 update_exception_bitmap(vcpu);
1442
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001443 if (emulate_invalid_guest_state)
1444 return;
1445
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001446 fix_pmode_dataseg(VCPU_SREG_ES, &vmx->rmode.es);
1447 fix_pmode_dataseg(VCPU_SREG_DS, &vmx->rmode.ds);
1448 fix_pmode_dataseg(VCPU_SREG_GS, &vmx->rmode.gs);
1449 fix_pmode_dataseg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001450
1451 vmcs_write16(GUEST_SS_SELECTOR, 0);
1452 vmcs_write32(GUEST_SS_AR_BYTES, 0x93);
1453
1454 vmcs_write16(GUEST_CS_SELECTOR,
1455 vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK);
1456 vmcs_write32(GUEST_CS_AR_BYTES, 0x9b);
1457}
1458
Mike Dayd77c26f2007-10-08 09:02:08 -04001459static gva_t rmode_tss_base(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001460{
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001461 if (!kvm->arch.tss_addr) {
Izik Eiduscbc94022007-10-25 00:29:55 +02001462 gfn_t base_gfn = kvm->memslots[0].base_gfn +
1463 kvm->memslots[0].npages - 3;
1464 return base_gfn << PAGE_SHIFT;
1465 }
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08001466 return kvm->arch.tss_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001467}
1468
1469static void fix_rmode_seg(int seg, struct kvm_save_segment *save)
1470{
1471 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1472
1473 save->selector = vmcs_read16(sf->selector);
1474 save->base = vmcs_readl(sf->base);
1475 save->limit = vmcs_read32(sf->limit);
1476 save->ar = vmcs_read32(sf->ar_bytes);
Jan Kiszka15b00f32007-11-19 10:21:45 +01001477 vmcs_write16(sf->selector, save->base >> 4);
1478 vmcs_write32(sf->base, save->base & 0xfffff);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001479 vmcs_write32(sf->limit, 0xffff);
1480 vmcs_write32(sf->ar_bytes, 0xf3);
1481}
1482
1483static void enter_rmode(struct kvm_vcpu *vcpu)
1484{
1485 unsigned long flags;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001486 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001488 if (enable_unrestricted_guest)
1489 return;
1490
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001491 vmx->emulation_required = 1;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001492 vmx->rmode.vm86_active = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001494 vmx->rmode.tr.base = vmcs_readl(GUEST_TR_BASE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495 vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm));
1496
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001497 vmx->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498 vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
1499
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001500 vmx->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
1502
1503 flags = vmcs_readl(GUEST_RFLAGS);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001504 vmx->rmode.save_iopl
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001505 = (flags & X86_EFLAGS_IOPL) >> IOPL_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001506
Glauber de Oliveira Costa053de042008-01-30 13:31:27 +01001507 flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001508
1509 vmcs_writel(GUEST_RFLAGS, flags);
Rusty Russell66aee912007-07-17 23:34:16 +10001510 vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511 update_exception_bitmap(vcpu);
1512
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001513 if (emulate_invalid_guest_state)
1514 goto continue_rmode;
1515
Avi Kivity6aa8b732006-12-10 02:21:36 -08001516 vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4);
1517 vmcs_write32(GUEST_SS_LIMIT, 0xffff);
1518 vmcs_write32(GUEST_SS_AR_BYTES, 0xf3);
1519
1520 vmcs_write32(GUEST_CS_AR_BYTES, 0xf3);
Michael Riepeabacf8d2006-12-22 01:05:45 -08001521 vmcs_write32(GUEST_CS_LIMIT, 0xffff);
Avi Kivity8cb5b032007-03-20 18:40:40 +02001522 if (vmcs_readl(GUEST_CS_BASE) == 0xffff0000)
1523 vmcs_writel(GUEST_CS_BASE, 0xf0000);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524 vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4);
1525
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001526 fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.es);
1527 fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.ds);
1528 fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.gs);
1529 fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.fs);
Avi Kivity75880a02007-06-20 11:20:04 +03001530
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03001531continue_rmode:
Eddie Dong8668a3c2007-10-10 14:26:45 +08001532 kvm_mmu_reset_context(vcpu);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08001533 init_rmode(vcpu->kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001534}
1535
Amit Shah401d10d2009-02-20 22:53:37 +05301536static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
1537{
1538 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity26bb0982009-09-07 11:14:12 +03001539 struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
1540
1541 if (!msr)
1542 return;
Amit Shah401d10d2009-02-20 22:53:37 +05301543
Avi Kivity44ea2b12009-09-06 15:55:37 +03001544 /*
1545 * Force kernel_gs_base reloading before EFER changes, as control
1546 * of this msr depends on is_long_mode().
1547 */
1548 vmx_load_host_state(to_vmx(vcpu));
Amit Shah401d10d2009-02-20 22:53:37 +05301549 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 Kivity6de4f3a2009-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 Kivity6de4f3a2009-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;
Marcelo Tosatti7c93be42009-10-26 16:48:33 -02001738 ept_load_pdptrs(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001739 }
1740
Sheng Yang2384d2b2008-01-17 15:14:33 +08001741 vmx_flush_tlb(vcpu);
Sheng Yang14394422008-04-28 12:24:45 +08001742 vmcs_writel(GUEST_CR3, guest_cr3);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001743 if (vcpu->arch.cr0 & X86_CR0_PE)
Avi Kivity5fd86fc2007-05-02 20:40:00 +03001744 vmx_fpu_deactivate(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745}
1746
1747static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1748{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001749 unsigned long hw_cr4 = cr4 | (to_vmx(vcpu)->rmode.vm86_active ?
Sheng Yang14394422008-04-28 12:24:45 +08001750 KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON);
1751
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001752 vcpu->arch.cr4 = cr4;
Avi Kivity089d0342009-03-23 18:26:32 +02001753 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08001754 ept_update_paging_mode_cr4(&hw_cr4, vcpu);
1755
1756 vmcs_writel(CR4_READ_SHADOW, cr4);
1757 vmcs_writel(GUEST_CR4, hw_cr4);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758}
1759
Avi Kivity6aa8b732006-12-10 02:21:36 -08001760static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1761{
1762 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1763
1764 return vmcs_readl(sf->base);
1765}
1766
1767static void vmx_get_segment(struct kvm_vcpu *vcpu,
1768 struct kvm_segment *var, int seg)
1769{
1770 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1771 u32 ar;
1772
1773 var->base = vmcs_readl(sf->base);
1774 var->limit = vmcs_read32(sf->limit);
1775 var->selector = vmcs_read16(sf->selector);
1776 ar = vmcs_read32(sf->ar_bytes);
Avi Kivity9fd4a3b2009-01-04 23:43:42 +02001777 if ((ar & AR_UNUSABLE_MASK) && !emulate_invalid_guest_state)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778 ar = 0;
1779 var->type = ar & 15;
1780 var->s = (ar >> 4) & 1;
1781 var->dpl = (ar >> 5) & 3;
1782 var->present = (ar >> 7) & 1;
1783 var->avl = (ar >> 12) & 1;
1784 var->l = (ar >> 13) & 1;
1785 var->db = (ar >> 14) & 1;
1786 var->g = (ar >> 15) & 1;
1787 var->unusable = (ar >> 16) & 1;
1788}
1789
Izik Eidus2e4d2652008-03-24 19:38:34 +02001790static int vmx_get_cpl(struct kvm_vcpu *vcpu)
1791{
Izik Eidus2e4d2652008-03-24 19:38:34 +02001792 if (!(vcpu->arch.cr0 & X86_CR0_PE)) /* if real mode */
1793 return 0;
1794
1795 if (vmx_get_rflags(vcpu) & X86_EFLAGS_VM) /* if virtual 8086 */
1796 return 3;
1797
Avi Kivityeab4b8a2009-08-04 15:02:54 +03001798 return vmcs_read16(GUEST_CS_SELECTOR) & 3;
Izik Eidus2e4d2652008-03-24 19:38:34 +02001799}
1800
Avi Kivity653e3102007-05-07 10:55:37 +03001801static u32 vmx_segment_access_rights(struct kvm_segment *var)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803 u32 ar;
1804
Avi Kivity653e3102007-05-07 10:55:37 +03001805 if (var->unusable)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001806 ar = 1 << 16;
1807 else {
1808 ar = var->type & 15;
1809 ar |= (var->s & 1) << 4;
1810 ar |= (var->dpl & 3) << 5;
1811 ar |= (var->present & 1) << 7;
1812 ar |= (var->avl & 1) << 12;
1813 ar |= (var->l & 1) << 13;
1814 ar |= (var->db & 1) << 14;
1815 ar |= (var->g & 1) << 15;
1816 }
Uri Lublinf7fbf1f2006-12-13 00:34:00 -08001817 if (ar == 0) /* a 0 value means unusable */
1818 ar = AR_UNUSABLE_MASK;
Avi Kivity653e3102007-05-07 10:55:37 +03001819
1820 return ar;
1821}
1822
1823static void vmx_set_segment(struct kvm_vcpu *vcpu,
1824 struct kvm_segment *var, int seg)
1825{
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001826 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity653e3102007-05-07 10:55:37 +03001827 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
1828 u32 ar;
1829
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001830 if (vmx->rmode.vm86_active && seg == VCPU_SREG_TR) {
1831 vmx->rmode.tr.selector = var->selector;
1832 vmx->rmode.tr.base = var->base;
1833 vmx->rmode.tr.limit = var->limit;
1834 vmx->rmode.tr.ar = vmx_segment_access_rights(var);
Avi Kivity653e3102007-05-07 10:55:37 +03001835 return;
1836 }
1837 vmcs_writel(sf->base, var->base);
1838 vmcs_write32(sf->limit, var->limit);
1839 vmcs_write16(sf->selector, var->selector);
Avi Kivity7ffd92c2009-06-09 14:10:45 +03001840 if (vmx->rmode.vm86_active && var->s) {
Avi Kivity653e3102007-05-07 10:55:37 +03001841 /*
1842 * Hack real-mode segments into vm86 compatibility.
1843 */
1844 if (var->base == 0xffff0000 && var->selector == 0xf000)
1845 vmcs_writel(sf->base, 0xf0000);
1846 ar = 0xf3;
1847 } else
1848 ar = vmx_segment_access_rights(var);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07001849
1850 /*
1851 * Fix the "Accessed" bit in AR field of segment registers for older
1852 * qemu binaries.
1853 * IA32 arch specifies that at the time of processor reset the
1854 * "Accessed" bit in the AR field of segment registers is 1. And qemu
1855 * is setting it to 0 in the usedland code. This causes invalid guest
1856 * state vmexit when "unrestricted guest" mode is turned on.
1857 * Fix for this setup issue in cpu_reset is being pushed in the qemu
1858 * tree. Newer qemu binaries with that qemu fix would not need this
1859 * kvm hack.
1860 */
1861 if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
1862 ar |= 0x1; /* Accessed */
1863
Avi Kivity6aa8b732006-12-10 02:21:36 -08001864 vmcs_write32(sf->ar_bytes, ar);
1865}
1866
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
1868{
1869 u32 ar = vmcs_read32(GUEST_CS_AR_BYTES);
1870
1871 *db = (ar >> 14) & 1;
1872 *l = (ar >> 13) & 1;
1873}
1874
1875static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1876{
1877 dt->limit = vmcs_read32(GUEST_IDTR_LIMIT);
1878 dt->base = vmcs_readl(GUEST_IDTR_BASE);
1879}
1880
1881static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1882{
1883 vmcs_write32(GUEST_IDTR_LIMIT, dt->limit);
1884 vmcs_writel(GUEST_IDTR_BASE, dt->base);
1885}
1886
1887static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1888{
1889 dt->limit = vmcs_read32(GUEST_GDTR_LIMIT);
1890 dt->base = vmcs_readl(GUEST_GDTR_BASE);
1891}
1892
1893static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt)
1894{
1895 vmcs_write32(GUEST_GDTR_LIMIT, dt->limit);
1896 vmcs_writel(GUEST_GDTR_BASE, dt->base);
1897}
1898
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001899static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
1900{
1901 struct kvm_segment var;
1902 u32 ar;
1903
1904 vmx_get_segment(vcpu, &var, seg);
1905 ar = vmx_segment_access_rights(&var);
1906
1907 if (var.base != (var.selector << 4))
1908 return false;
1909 if (var.limit != 0xffff)
1910 return false;
1911 if (ar != 0xf3)
1912 return false;
1913
1914 return true;
1915}
1916
1917static bool code_segment_valid(struct kvm_vcpu *vcpu)
1918{
1919 struct kvm_segment cs;
1920 unsigned int cs_rpl;
1921
1922 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
1923 cs_rpl = cs.selector & SELECTOR_RPL_MASK;
1924
Avi Kivity1872a3f2009-01-04 23:26:52 +02001925 if (cs.unusable)
1926 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001927 if (~cs.type & (AR_TYPE_CODE_MASK|AR_TYPE_ACCESSES_MASK))
1928 return false;
1929 if (!cs.s)
1930 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001931 if (cs.type & AR_TYPE_WRITEABLE_MASK) {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001932 if (cs.dpl > cs_rpl)
1933 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02001934 } else {
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001935 if (cs.dpl != cs_rpl)
1936 return false;
1937 }
1938 if (!cs.present)
1939 return false;
1940
1941 /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
1942 return true;
1943}
1944
1945static bool stack_segment_valid(struct kvm_vcpu *vcpu)
1946{
1947 struct kvm_segment ss;
1948 unsigned int ss_rpl;
1949
1950 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
1951 ss_rpl = ss.selector & SELECTOR_RPL_MASK;
1952
Avi Kivity1872a3f2009-01-04 23:26:52 +02001953 if (ss.unusable)
1954 return true;
1955 if (ss.type != 3 && ss.type != 7)
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001956 return false;
1957 if (!ss.s)
1958 return false;
1959 if (ss.dpl != ss_rpl) /* DPL != RPL */
1960 return false;
1961 if (!ss.present)
1962 return false;
1963
1964 return true;
1965}
1966
1967static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
1968{
1969 struct kvm_segment var;
1970 unsigned int rpl;
1971
1972 vmx_get_segment(vcpu, &var, seg);
1973 rpl = var.selector & SELECTOR_RPL_MASK;
1974
Avi Kivity1872a3f2009-01-04 23:26:52 +02001975 if (var.unusable)
1976 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03001977 if (!var.s)
1978 return false;
1979 if (!var.present)
1980 return false;
1981 if (~var.type & (AR_TYPE_CODE_MASK|AR_TYPE_WRITEABLE_MASK)) {
1982 if (var.dpl < rpl) /* DPL < RPL */
1983 return false;
1984 }
1985
1986 /* TODO: Add other members to kvm_segment_field to allow checking for other access
1987 * rights flags
1988 */
1989 return true;
1990}
1991
1992static bool tr_valid(struct kvm_vcpu *vcpu)
1993{
1994 struct kvm_segment tr;
1995
1996 vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
1997
Avi Kivity1872a3f2009-01-04 23:26:52 +02001998 if (tr.unusable)
1999 return false;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002000 if (tr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2001 return false;
Avi Kivity1872a3f2009-01-04 23:26:52 +02002002 if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002003 return false;
2004 if (!tr.present)
2005 return false;
2006
2007 return true;
2008}
2009
2010static bool ldtr_valid(struct kvm_vcpu *vcpu)
2011{
2012 struct kvm_segment ldtr;
2013
2014 vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
2015
Avi Kivity1872a3f2009-01-04 23:26:52 +02002016 if (ldtr.unusable)
2017 return true;
Mohammed Gamal648dfaa2008-08-17 16:38:32 +03002018 if (ldtr.selector & SELECTOR_TI_MASK) /* TI = 1 */
2019 return false;
2020 if (ldtr.type != 2)
2021 return false;
2022 if (!ldtr.present)
2023 return false;
2024
2025 return true;
2026}
2027
2028static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
2029{
2030 struct kvm_segment cs, ss;
2031
2032 vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
2033 vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
2034
2035 return ((cs.selector & SELECTOR_RPL_MASK) ==
2036 (ss.selector & SELECTOR_RPL_MASK));
2037}
2038
2039/*
2040 * Check if guest state is valid. Returns true if valid, false if
2041 * not.
2042 * We assume that registers are always usable
2043 */
2044static bool guest_state_valid(struct kvm_vcpu *vcpu)
2045{
2046 /* real mode guest state checks */
2047 if (!(vcpu->arch.cr0 & X86_CR0_PE)) {
2048 if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
2049 return false;
2050 if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
2051 return false;
2052 if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
2053 return false;
2054 if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
2055 return false;
2056 if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
2057 return false;
2058 if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
2059 return false;
2060 } else {
2061 /* protected mode guest state checks */
2062 if (!cs_ss_rpl_check(vcpu))
2063 return false;
2064 if (!code_segment_valid(vcpu))
2065 return false;
2066 if (!stack_segment_valid(vcpu))
2067 return false;
2068 if (!data_segment_valid(vcpu, VCPU_SREG_DS))
2069 return false;
2070 if (!data_segment_valid(vcpu, VCPU_SREG_ES))
2071 return false;
2072 if (!data_segment_valid(vcpu, VCPU_SREG_FS))
2073 return false;
2074 if (!data_segment_valid(vcpu, VCPU_SREG_GS))
2075 return false;
2076 if (!tr_valid(vcpu))
2077 return false;
2078 if (!ldtr_valid(vcpu))
2079 return false;
2080 }
2081 /* TODO:
2082 * - Add checks on RIP
2083 * - Add checks on RFLAGS
2084 */
2085
2086 return true;
2087}
2088
Mike Dayd77c26f2007-10-08 09:02:08 -04002089static int init_rmode_tss(struct kvm *kvm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002090{
Avi Kivity6aa8b732006-12-10 02:21:36 -08002091 gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002092 u16 data = 0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002093 int ret = 0;
Izik Eidus195aefd2007-10-01 22:14:18 +02002094 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002095
Izik Eidus195aefd2007-10-01 22:14:18 +02002096 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2097 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002098 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002099 data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
Sheng Yang464d17c2008-08-13 14:10:33 +08002100 r = kvm_write_guest_page(kvm, fn++, &data,
2101 TSS_IOPB_BASE_OFFSET, sizeof(u16));
Izik Eidus195aefd2007-10-01 22:14:18 +02002102 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002103 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002104 r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
2105 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002106 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002107 r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
2108 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002109 goto out;
Izik Eidus195aefd2007-10-01 22:14:18 +02002110 data = ~0;
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002111 r = kvm_write_guest_page(kvm, fn, &data,
2112 RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
2113 sizeof(u8));
Izik Eidus195aefd2007-10-01 22:14:18 +02002114 if (r < 0)
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002115 goto out;
2116
2117 ret = 1;
2118out:
Marcelo Tosatti10589a42007-12-20 19:18:22 -05002119 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002120}
2121
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002122static int init_rmode_identity_map(struct kvm *kvm)
2123{
2124 int i, r, ret;
2125 pfn_t identity_map_pfn;
2126 u32 tmp;
2127
Avi Kivity089d0342009-03-23 18:26:32 +02002128 if (!enable_ept)
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002129 return 1;
2130 if (unlikely(!kvm->arch.ept_identity_pagetable)) {
2131 printk(KERN_ERR "EPT: identity-mapping pagetable "
2132 "haven't been allocated!\n");
2133 return 0;
2134 }
2135 if (likely(kvm->arch.ept_identity_pagetable_done))
2136 return 1;
2137 ret = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002138 identity_map_pfn = kvm->arch.ept_identity_map_addr >> PAGE_SHIFT;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002139 r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
2140 if (r < 0)
2141 goto out;
2142 /* Set up identity-mapping pagetable for EPT in real mode */
2143 for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
2144 tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
2145 _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
2146 r = kvm_write_guest_page(kvm, identity_map_pfn,
2147 &tmp, i * sizeof(tmp), sizeof(tmp));
2148 if (r < 0)
2149 goto out;
2150 }
2151 kvm->arch.ept_identity_pagetable_done = true;
2152 ret = 1;
2153out:
2154 return ret;
2155}
2156
Avi Kivity6aa8b732006-12-10 02:21:36 -08002157static void seg_setup(int seg)
2158{
2159 struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002160 unsigned int ar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002161
2162 vmcs_write16(sf->selector, 0);
2163 vmcs_writel(sf->base, 0);
2164 vmcs_write32(sf->limit, 0xffff);
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002165 if (enable_unrestricted_guest) {
2166 ar = 0x93;
2167 if (seg == VCPU_SREG_CS)
2168 ar |= 0x08; /* code segment */
2169 } else
2170 ar = 0xf3;
2171
2172 vmcs_write32(sf->ar_bytes, ar);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002173}
2174
Sheng Yangf78e0e22007-10-29 09:40:42 +08002175static int alloc_apic_access_page(struct kvm *kvm)
2176{
2177 struct kvm_userspace_memory_region kvm_userspace_mem;
2178 int r = 0;
2179
Izik Eidus72dc67a2008-02-10 18:04:15 +02002180 down_write(&kvm->slots_lock);
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002181 if (kvm->arch.apic_access_page)
Sheng Yangf78e0e22007-10-29 09:40:42 +08002182 goto out;
2183 kvm_userspace_mem.slot = APIC_ACCESS_PAGE_PRIVATE_MEMSLOT;
2184 kvm_userspace_mem.flags = 0;
2185 kvm_userspace_mem.guest_phys_addr = 0xfee00000ULL;
2186 kvm_userspace_mem.memory_size = PAGE_SIZE;
2187 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2188 if (r)
2189 goto out;
Izik Eidus72dc67a2008-02-10 18:04:15 +02002190
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002191 kvm->arch.apic_access_page = gfn_to_page(kvm, 0xfee00);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002192out:
Izik Eidus72dc67a2008-02-10 18:04:15 +02002193 up_write(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002194 return r;
2195}
2196
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002197static int alloc_identity_pagetable(struct kvm *kvm)
2198{
2199 struct kvm_userspace_memory_region kvm_userspace_mem;
2200 int r = 0;
2201
2202 down_write(&kvm->slots_lock);
2203 if (kvm->arch.ept_identity_pagetable)
2204 goto out;
2205 kvm_userspace_mem.slot = IDENTITY_PAGETABLE_PRIVATE_MEMSLOT;
2206 kvm_userspace_mem.flags = 0;
Sheng Yangb927a3c2009-07-21 10:42:48 +08002207 kvm_userspace_mem.guest_phys_addr =
2208 kvm->arch.ept_identity_map_addr;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002209 kvm_userspace_mem.memory_size = PAGE_SIZE;
2210 r = __kvm_set_memory_region(kvm, &kvm_userspace_mem, 0);
2211 if (r)
2212 goto out;
2213
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002214 kvm->arch.ept_identity_pagetable = gfn_to_page(kvm,
Sheng Yangb927a3c2009-07-21 10:42:48 +08002215 kvm->arch.ept_identity_map_addr >> PAGE_SHIFT);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002216out:
2217 up_write(&kvm->slots_lock);
2218 return r;
2219}
2220
Sheng Yang2384d2b2008-01-17 15:14:33 +08002221static void allocate_vpid(struct vcpu_vmx *vmx)
2222{
2223 int vpid;
2224
2225 vmx->vpid = 0;
Avi Kivity919818a2009-03-23 18:01:29 +02002226 if (!enable_vpid)
Sheng Yang2384d2b2008-01-17 15:14:33 +08002227 return;
2228 spin_lock(&vmx_vpid_lock);
2229 vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
2230 if (vpid < VMX_NR_VPIDS) {
2231 vmx->vpid = vpid;
2232 __set_bit(vpid, vmx_vpid_bitmap);
2233 }
2234 spin_unlock(&vmx_vpid_lock);
2235}
2236
Avi Kivity58972972009-02-24 22:26:47 +02002237static void __vmx_disable_intercept_for_msr(unsigned long *msr_bitmap, u32 msr)
Sheng Yang25c5f222008-03-28 13:18:56 +08002238{
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002239 int f = sizeof(unsigned long);
Sheng Yang25c5f222008-03-28 13:18:56 +08002240
2241 if (!cpu_has_vmx_msr_bitmap())
2242 return;
2243
2244 /*
2245 * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
2246 * have the write-low and read-high bitmap offsets the wrong way round.
2247 * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
2248 */
Sheng Yang25c5f222008-03-28 13:18:56 +08002249 if (msr <= 0x1fff) {
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002250 __clear_bit(msr, msr_bitmap + 0x000 / f); /* read-low */
2251 __clear_bit(msr, msr_bitmap + 0x800 / f); /* write-low */
Sheng Yang25c5f222008-03-28 13:18:56 +08002252 } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
2253 msr &= 0x1fff;
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002254 __clear_bit(msr, msr_bitmap + 0x400 / f); /* read-high */
2255 __clear_bit(msr, msr_bitmap + 0xc00 / f); /* write-high */
Sheng Yang25c5f222008-03-28 13:18:56 +08002256 }
Sheng Yang25c5f222008-03-28 13:18:56 +08002257}
2258
Avi Kivity58972972009-02-24 22:26:47 +02002259static void vmx_disable_intercept_for_msr(u32 msr, bool longmode_only)
2260{
2261 if (!longmode_only)
2262 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_legacy, msr);
2263 __vmx_disable_intercept_for_msr(vmx_msr_bitmap_longmode, msr);
2264}
2265
Avi Kivity6aa8b732006-12-10 02:21:36 -08002266/*
2267 * Sets up the vmcs for emulated real mode.
2268 */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002269static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270{
Sheng Yang468d4722008-10-09 16:01:55 +08002271 u32 host_sysenter_cs, msr_low, msr_high;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272 u32 junk;
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002273 u64 host_pat, tsc_this, tsc_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002274 unsigned long a;
2275 struct descriptor_table dt;
2276 int i;
Avi Kivitycd2276a2007-05-14 20:41:13 +03002277 unsigned long kvm_vmx_return;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002278 u32 exec_control;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002279
Avi Kivity6aa8b732006-12-10 02:21:36 -08002280 /* I/O */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02002281 vmcs_write64(IO_BITMAP_A, __pa(vmx_io_bitmap_a));
2282 vmcs_write64(IO_BITMAP_B, __pa(vmx_io_bitmap_b));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002283
Sheng Yang25c5f222008-03-28 13:18:56 +08002284 if (cpu_has_vmx_msr_bitmap())
Avi Kivity58972972009-02-24 22:26:47 +02002285 vmcs_write64(MSR_BITMAP, __pa(vmx_msr_bitmap_legacy));
Sheng Yang25c5f222008-03-28 13:18:56 +08002286
Avi Kivity6aa8b732006-12-10 02:21:36 -08002287 vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
2288
Avi Kivity6aa8b732006-12-10 02:21:36 -08002289 /* Control */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002290 vmcs_write32(PIN_BASED_VM_EXEC_CONTROL,
2291 vmcs_config.pin_based_exec_ctrl);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002292
2293 exec_control = vmcs_config.cpu_based_exec_ctrl;
2294 if (!vm_need_tpr_shadow(vmx->vcpu.kvm)) {
2295 exec_control &= ~CPU_BASED_TPR_SHADOW;
2296#ifdef CONFIG_X86_64
2297 exec_control |= CPU_BASED_CR8_STORE_EXITING |
2298 CPU_BASED_CR8_LOAD_EXITING;
2299#endif
2300 }
Avi Kivity089d0342009-03-23 18:26:32 +02002301 if (!enable_ept)
Sheng Yangd56f5462008-04-25 10:13:16 +08002302 exec_control |= CPU_BASED_CR3_STORE_EXITING |
Marcelo Tosatti83dbc832008-10-07 17:01:27 -03002303 CPU_BASED_CR3_LOAD_EXITING |
2304 CPU_BASED_INVLPG_EXITING;
Yang, Sheng6e5d8652007-09-12 18:03:11 +08002305 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, exec_control);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002306
Sheng Yang83ff3b92007-11-21 14:33:25 +08002307 if (cpu_has_secondary_exec_ctrls()) {
2308 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
2309 if (!vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2310 exec_control &=
2311 ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
Sheng Yang2384d2b2008-01-17 15:14:33 +08002312 if (vmx->vpid == 0)
2313 exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
Sheng Yang046d8712009-11-27 16:46:26 +08002314 if (!enable_ept) {
Sheng Yangd56f5462008-04-25 10:13:16 +08002315 exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
Sheng Yang046d8712009-11-27 16:46:26 +08002316 enable_unrestricted_guest = 0;
2317 }
Nitin A Kamble3a624e22009-06-08 11:34:16 -07002318 if (!enable_unrestricted_guest)
2319 exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002320 if (!ple_gap)
2321 exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
Sheng Yang83ff3b92007-11-21 14:33:25 +08002322 vmcs_write32(SECONDARY_VM_EXEC_CONTROL, exec_control);
2323 }
Sheng Yangf78e0e22007-10-29 09:40:42 +08002324
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08002325 if (ple_gap) {
2326 vmcs_write32(PLE_GAP, ple_gap);
2327 vmcs_write32(PLE_WINDOW, ple_window);
2328 }
2329
Avi Kivityc7addb92007-09-16 18:58:32 +02002330 vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, !!bypass_guest_pf);
2331 vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, !!bypass_guest_pf);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002332 vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
2333
2334 vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */
2335 vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */
2336 vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */
2337
2338 vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
2339 vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
2340 vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002341 vmcs_write16(HOST_FS_SELECTOR, kvm_read_fs()); /* 22.2.4 */
2342 vmcs_write16(HOST_GS_SELECTOR, kvm_read_gs()); /* 22.2.4 */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002343 vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
Avi Kivity05b3e0c2006-12-13 00:33:45 -08002344#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002345 rdmsrl(MSR_FS_BASE, a);
2346 vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */
2347 rdmsrl(MSR_GS_BASE, a);
2348 vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */
2349#else
2350 vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
2351 vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
2352#endif
2353
2354 vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
2355
Avi Kivityd6e88ae2008-07-10 16:53:33 +03002356 kvm_get_idt(&dt);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002357 vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */
2358
Mike Dayd77c26f2007-10-08 09:02:08 -04002359 asm("mov $.Lkvm_vmx_return, %0" : "=r"(kvm_vmx_return));
Avi Kivitycd2276a2007-05-14 20:41:13 +03002360 vmcs_writel(HOST_RIP, kvm_vmx_return); /* 22.2.5 */
Eddie Dong2cc51562007-05-21 07:28:09 +03002361 vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
2362 vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
2363 vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002364
2365 rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk);
2366 vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs);
2367 rdmsrl(MSR_IA32_SYSENTER_ESP, a);
2368 vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */
2369 rdmsrl(MSR_IA32_SYSENTER_EIP, a);
2370 vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */
2371
Sheng Yang468d4722008-10-09 16:01:55 +08002372 if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
2373 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2374 host_pat = msr_low | ((u64) msr_high << 32);
2375 vmcs_write64(HOST_IA32_PAT, host_pat);
2376 }
2377 if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2378 rdmsr(MSR_IA32_CR_PAT, msr_low, msr_high);
2379 host_pat = msr_low | ((u64) msr_high << 32);
2380 /* Write the default value follow host pat */
2381 vmcs_write64(GUEST_IA32_PAT, host_pat);
2382 /* Keep arch.pat sync with GUEST_IA32_PAT */
2383 vmx->vcpu.arch.pat = host_pat;
2384 }
2385
Avi Kivity6aa8b732006-12-10 02:21:36 -08002386 for (i = 0; i < NR_VMX_MSR; ++i) {
2387 u32 index = vmx_msr_index[i];
2388 u32 data_low, data_high;
2389 u64 data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002390 int j = vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002391
2392 if (rdmsr_safe(index, &data_low, &data_high) < 0)
2393 continue;
Avi Kivity432bd6c2007-01-31 23:48:13 -08002394 if (wrmsr_safe(index, data_low, data_high) < 0)
2395 continue;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002396 data = data_low | ((u64)data_high << 32);
Avi Kivity26bb0982009-09-07 11:14:12 +03002397 vmx->guest_msrs[j].index = i;
2398 vmx->guest_msrs[j].data = 0;
Avi Kivityd5696722009-12-02 12:28:47 +02002399 vmx->guest_msrs[j].mask = -1ull;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002400 ++vmx->nmsrs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002402
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002403 vmcs_write32(VM_EXIT_CONTROLS, vmcs_config.vmexit_ctrl);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002404
2405 /* 22.2.1, 20.8.1 */
Yang, Sheng1c3d14f2007-07-29 11:07:42 +03002406 vmcs_write32(VM_ENTRY_CONTROLS, vmcs_config.vmentry_ctrl);
2407
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002408 vmcs_writel(CR0_GUEST_HOST_MASK, ~0UL);
2409 vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK);
2410
Marcelo Tosatti53f658b2008-12-11 20:45:05 +01002411 tsc_base = vmx->vcpu.kvm->arch.vm_init_tsc;
2412 rdtscll(tsc_this);
2413 if (tsc_this < vmx->vcpu.kvm->arch.vm_init_tsc)
2414 tsc_base = tsc_this;
2415
2416 guest_write_tsc(0, tsc_base);
Sheng Yangf78e0e22007-10-29 09:40:42 +08002417
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002418 return 0;
2419}
2420
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002421static int init_rmode(struct kvm *kvm)
2422{
2423 if (!init_rmode_tss(kvm))
2424 return 0;
2425 if (!init_rmode_identity_map(kvm))
2426 return 0;
2427 return 1;
2428}
2429
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002430static int vmx_vcpu_reset(struct kvm_vcpu *vcpu)
2431{
2432 struct vcpu_vmx *vmx = to_vmx(vcpu);
2433 u64 msr;
2434 int ret;
2435
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002436 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP));
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002437 down_read(&vcpu->kvm->slots_lock);
Sheng Yangb7ebfb02008-04-25 21:44:52 +08002438 if (!init_rmode(vmx->vcpu.kvm)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002439 ret = -ENOMEM;
2440 goto out;
2441 }
2442
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002443 vmx->rmode.vm86_active = 0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002444
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002445 vmx->soft_vnmi_blocked = 0;
2446
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002447 vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002448 kvm_set_cr8(&vmx->vcpu, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002449 msr = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002450 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002451 msr |= MSR_IA32_APICBASE_BSP;
2452 kvm_set_apic_base(&vmx->vcpu, msr);
2453
2454 fx_init(&vmx->vcpu);
2455
Avi Kivity5706be02008-08-20 15:07:31 +03002456 seg_setup(VCPU_SREG_CS);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002457 /*
2458 * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode
2459 * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh.
2460 */
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002461 if (kvm_vcpu_is_bsp(&vmx->vcpu)) {
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002462 vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
2463 vmcs_writel(GUEST_CS_BASE, 0x000f0000);
2464 } else {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002465 vmcs_write16(GUEST_CS_SELECTOR, vmx->vcpu.arch.sipi_vector << 8);
2466 vmcs_writel(GUEST_CS_BASE, vmx->vcpu.arch.sipi_vector << 12);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002467 }
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002468
2469 seg_setup(VCPU_SREG_DS);
2470 seg_setup(VCPU_SREG_ES);
2471 seg_setup(VCPU_SREG_FS);
2472 seg_setup(VCPU_SREG_GS);
2473 seg_setup(VCPU_SREG_SS);
2474
2475 vmcs_write16(GUEST_TR_SELECTOR, 0);
2476 vmcs_writel(GUEST_TR_BASE, 0);
2477 vmcs_write32(GUEST_TR_LIMIT, 0xffff);
2478 vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
2479
2480 vmcs_write16(GUEST_LDTR_SELECTOR, 0);
2481 vmcs_writel(GUEST_LDTR_BASE, 0);
2482 vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
2483 vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
2484
2485 vmcs_write32(GUEST_SYSENTER_CS, 0);
2486 vmcs_writel(GUEST_SYSENTER_ESP, 0);
2487 vmcs_writel(GUEST_SYSENTER_EIP, 0);
2488
2489 vmcs_writel(GUEST_RFLAGS, 0x02);
Gleb Natapovc5af89b2009-06-09 15:56:26 +03002490 if (kvm_vcpu_is_bsp(&vmx->vcpu))
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002491 kvm_rip_write(vcpu, 0xfff0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002492 else
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002493 kvm_rip_write(vcpu, 0);
2494 kvm_register_write(vcpu, VCPU_REGS_RSP, 0);
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002495
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002496 vmcs_writel(GUEST_DR7, 0x400);
2497
2498 vmcs_writel(GUEST_GDTR_BASE, 0);
2499 vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
2500
2501 vmcs_writel(GUEST_IDTR_BASE, 0);
2502 vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
2503
2504 vmcs_write32(GUEST_ACTIVITY_STATE, 0);
2505 vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
2506 vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0);
2507
Avi Kivitye00c8cf2007-10-21 11:00:39 +02002508 /* Special registers */
2509 vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
2510
2511 setup_msrs(vmx);
2512
Avi Kivity6aa8b732006-12-10 02:21:36 -08002513 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
2514
Sheng Yangf78e0e22007-10-29 09:40:42 +08002515 if (cpu_has_vmx_tpr_shadow()) {
2516 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
2517 if (vm_need_tpr_shadow(vmx->vcpu.kvm))
2518 vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002519 page_to_phys(vmx->vcpu.arch.apic->regs_page));
Sheng Yangf78e0e22007-10-29 09:40:42 +08002520 vmcs_write32(TPR_THRESHOLD, 0);
2521 }
2522
2523 if (vm_need_virtualize_apic_accesses(vmx->vcpu.kvm))
2524 vmcs_write64(APIC_ACCESS_ADDR,
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002525 page_to_phys(vmx->vcpu.kvm->arch.apic_access_page));
Avi Kivity6aa8b732006-12-10 02:21:36 -08002526
Sheng Yang2384d2b2008-01-17 15:14:33 +08002527 if (vmx->vpid != 0)
2528 vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
2529
Eduardo Habkostfa400522009-10-24 02:49:58 -02002530 vmx->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002531 vmx_set_cr0(&vmx->vcpu, vmx->vcpu.arch.cr0); /* enter rmode */
Rusty Russell8b9cf982007-07-30 16:31:43 +10002532 vmx_set_cr4(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002533 vmx_set_efer(&vmx->vcpu, 0);
Rusty Russell8b9cf982007-07-30 16:31:43 +10002534 vmx_fpu_activate(&vmx->vcpu);
2535 update_exception_bitmap(&vmx->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536
Sheng Yang2384d2b2008-01-17 15:14:33 +08002537 vpid_sync_vcpu_all(vmx);
2538
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002539 ret = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002540
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03002541 /* HACK: Don't enable emulation on guest boot/reset */
2542 vmx->emulation_required = 0;
2543
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544out:
Marcelo Tosatti3200f402008-03-29 20:17:59 -03002545 up_read(&vcpu->kvm->slots_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546 return ret;
2547}
2548
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002549static void enable_irq_window(struct kvm_vcpu *vcpu)
2550{
2551 u32 cpu_based_vm_exec_control;
2552
2553 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2554 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
2555 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2556}
2557
2558static void enable_nmi_window(struct kvm_vcpu *vcpu)
2559{
2560 u32 cpu_based_vm_exec_control;
2561
2562 if (!cpu_has_virtual_nmis()) {
2563 enable_irq_window(vcpu);
2564 return;
2565 }
2566
2567 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
2568 cpu_based_vm_exec_control |= CPU_BASED_VIRTUAL_NMI_PENDING;
2569 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
2570}
2571
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002572static void vmx_inject_irq(struct kvm_vcpu *vcpu)
Eddie Dong85f455f2007-07-06 12:20:49 +03002573{
Avi Kivity9c8cba32007-11-22 11:42:59 +02002574 struct vcpu_vmx *vmx = to_vmx(vcpu);
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002575 uint32_t intr;
2576 int irq = vcpu->arch.interrupt.nr;
Avi Kivity9c8cba32007-11-22 11:42:59 +02002577
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002578 trace_kvm_inj_virq(irq);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04002579
Avi Kivityfa89a812008-09-01 15:57:51 +03002580 ++vcpu->stat.irq_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002581 if (vmx->rmode.vm86_active) {
Avi Kivity9c8cba32007-11-22 11:42:59 +02002582 vmx->rmode.irq.pending = true;
2583 vmx->rmode.irq.vector = irq;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002584 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
Gleb Natapovae0bb3e2009-05-19 11:07:10 +03002585 if (vcpu->arch.interrupt.soft)
2586 vmx->rmode.irq.rip +=
2587 vmx->vcpu.arch.event_exit_inst_len;
Avi Kivity9c5623e2007-11-08 18:19:20 +02002588 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2589 irq | INTR_TYPE_SOFT_INTR | INTR_INFO_VALID_MASK);
2590 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002591 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
Eddie Dong85f455f2007-07-06 12:20:49 +03002592 return;
2593 }
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002594 intr = irq | INTR_INFO_VALID_MASK;
2595 if (vcpu->arch.interrupt.soft) {
2596 intr |= INTR_TYPE_SOFT_INTR;
2597 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2598 vmx->vcpu.arch.event_exit_inst_len);
2599 } else
2600 intr |= INTR_TYPE_EXT_INTR;
2601 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
Eddie Dong85f455f2007-07-06 12:20:49 +03002602}
2603
Sheng Yangf08864b2008-05-15 18:23:25 +08002604static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
2605{
Jan Kiszka66a5a342008-09-26 09:30:51 +02002606 struct vcpu_vmx *vmx = to_vmx(vcpu);
2607
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002608 if (!cpu_has_virtual_nmis()) {
2609 /*
2610 * Tracking the NMI-blocked state in software is built upon
2611 * finding the next open IRQ window. This, in turn, depends on
2612 * well-behaving guests: They have to keep IRQs disabled at
2613 * least as long as the NMI handler runs. Otherwise we may
2614 * cause NMI nesting, maybe breaking the guest. But as this is
2615 * highly unlikely, we can live with the residual risk.
2616 */
2617 vmx->soft_vnmi_blocked = 1;
2618 vmx->vnmi_blocked_time = 0;
2619 }
2620
Jan Kiszka487b3912008-09-26 09:30:56 +02002621 ++vcpu->stat.nmi_injections;
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002622 if (vmx->rmode.vm86_active) {
Jan Kiszka66a5a342008-09-26 09:30:51 +02002623 vmx->rmode.irq.pending = true;
2624 vmx->rmode.irq.vector = NMI_VECTOR;
2625 vmx->rmode.irq.rip = kvm_rip_read(vcpu);
2626 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2627 NMI_VECTOR | INTR_TYPE_SOFT_INTR |
2628 INTR_INFO_VALID_MASK);
2629 vmcs_write32(VM_ENTRY_INSTRUCTION_LEN, 1);
2630 kvm_rip_write(vcpu, vmx->rmode.irq.rip - 1);
2631 return;
2632 }
Sheng Yangf08864b2008-05-15 18:23:25 +08002633 vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
2634 INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
Sheng Yangf08864b2008-05-15 18:23:25 +08002635}
2636
Gleb Natapovc4282df2009-04-21 17:45:07 +03002637static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
Jan Kiszka33f089c2008-09-26 09:30:49 +02002638{
Jan Kiszka3b86cd92008-09-26 09:30:57 +02002639 if (!cpu_has_virtual_nmis() && to_vmx(vcpu)->soft_vnmi_blocked)
Gleb Natapovc4282df2009-04-21 17:45:07 +03002640 return 0;
Jan Kiszka33f089c2008-09-26 09:30:49 +02002641
Gleb Natapovc4282df2009-04-21 17:45:07 +03002642 return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2643 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS |
2644 GUEST_INTR_STATE_NMI));
Jan Kiszka33f089c2008-09-26 09:30:49 +02002645}
2646
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002647static bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
2648{
2649 if (!cpu_has_virtual_nmis())
2650 return to_vmx(vcpu)->soft_vnmi_blocked;
2651 else
2652 return !!(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2653 GUEST_INTR_STATE_NMI);
2654}
2655
2656static void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2657{
2658 struct vcpu_vmx *vmx = to_vmx(vcpu);
2659
2660 if (!cpu_has_virtual_nmis()) {
2661 if (vmx->soft_vnmi_blocked != masked) {
2662 vmx->soft_vnmi_blocked = masked;
2663 vmx->vnmi_blocked_time = 0;
2664 }
2665 } else {
2666 if (masked)
2667 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
2668 GUEST_INTR_STATE_NMI);
2669 else
2670 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
2671 GUEST_INTR_STATE_NMI);
2672 }
2673}
2674
Gleb Natapov78646122009-03-23 12:12:11 +02002675static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
2676{
Gleb Natapovc4282df2009-04-21 17:45:07 +03002677 return (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
2678 !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
2679 (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
Gleb Natapov78646122009-03-23 12:12:11 +02002680}
2681
Izik Eiduscbc94022007-10-25 00:29:55 +02002682static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
2683{
2684 int ret;
2685 struct kvm_userspace_memory_region tss_mem = {
Sheng Yang6fe63972008-10-16 17:30:58 +08002686 .slot = TSS_PRIVATE_MEMSLOT,
Izik Eiduscbc94022007-10-25 00:29:55 +02002687 .guest_phys_addr = addr,
2688 .memory_size = PAGE_SIZE * 3,
2689 .flags = 0,
2690 };
2691
2692 ret = kvm_set_memory_region(kvm, &tss_mem, 0);
2693 if (ret)
2694 return ret;
Zhang Xiantaobfc6d222007-12-14 10:20:16 +08002695 kvm->arch.tss_addr = addr;
Izik Eiduscbc94022007-10-25 00:29:55 +02002696 return 0;
2697}
2698
Avi Kivity6aa8b732006-12-10 02:21:36 -08002699static int handle_rmode_exception(struct kvm_vcpu *vcpu,
2700 int vec, u32 err_code)
2701{
Nitin A Kambleb3f37702007-05-17 15:50:34 +03002702 /*
2703 * Instruction with address size override prefix opcode 0x67
2704 * Cause the #SS fault with 0 error code in VM86 mode.
2705 */
2706 if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0)
Avi Kivity851ba692009-08-24 11:10:17 +03002707 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002708 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002709 /*
2710 * Forward all other exceptions that are valid in real mode.
2711 * FIXME: Breaks guest debugging in real mode, needs to be fixed with
2712 * the required debugging infrastructure rework.
2713 */
2714 switch (vec) {
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002715 case DB_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002716 if (vcpu->guest_debug &
2717 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
2718 return 0;
2719 kvm_queue_exception(vcpu, vec);
2720 return 1;
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002721 case BP_VECTOR:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002722 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
2723 return 0;
2724 /* fall through */
2725 case DE_VECTOR:
Jan Kiszka77ab6db2008-07-14 12:28:51 +02002726 case OF_VECTOR:
2727 case BR_VECTOR:
2728 case UD_VECTOR:
2729 case DF_VECTOR:
2730 case SS_VECTOR:
2731 case GP_VECTOR:
2732 case MF_VECTOR:
2733 kvm_queue_exception(vcpu, vec);
2734 return 1;
2735 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 return 0;
2737}
2738
Andi Kleena0861c02009-06-08 17:37:09 +08002739/*
2740 * Trigger machine check on the host. We assume all the MSRs are already set up
2741 * by the CPU and that we still run on the same CPU as the MCE occurred on.
2742 * We pass a fake environment to the machine check handler because we want
2743 * the guest to be always treated like user space, no matter what context
2744 * it used internally.
2745 */
2746static void kvm_machine_check(void)
2747{
2748#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
2749 struct pt_regs regs = {
2750 .cs = 3, /* Fake ring 3 no matter what the guest ran on */
2751 .flags = X86_EFLAGS_IF,
2752 };
2753
2754 do_machine_check(&regs, 0);
2755#endif
2756}
2757
Avi Kivity851ba692009-08-24 11:10:17 +03002758static int handle_machine_check(struct kvm_vcpu *vcpu)
Andi Kleena0861c02009-06-08 17:37:09 +08002759{
2760 /* already handled by vcpu_run */
2761 return 1;
2762}
2763
Avi Kivity851ba692009-08-24 11:10:17 +03002764static int handle_exception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765{
Avi Kivity1155f762007-11-22 11:30:47 +02002766 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002767 struct kvm_run *kvm_run = vcpu->run;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002768 u32 intr_info, ex_no, error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002769 unsigned long cr2, rip, dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002770 u32 vect_info;
2771 enum emulation_result er;
2772
Avi Kivity1155f762007-11-22 11:30:47 +02002773 vect_info = vmx->idt_vectoring_info;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002774 intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
2775
Andi Kleena0861c02009-06-08 17:37:09 +08002776 if (is_machine_check(intr_info))
Avi Kivity851ba692009-08-24 11:10:17 +03002777 return handle_machine_check(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08002778
Avi Kivity6aa8b732006-12-10 02:21:36 -08002779 if ((vect_info & VECTORING_INFO_VALID_MASK) &&
Avi Kivity65ac7262009-11-04 11:59:01 +02002780 !is_page_fault(intr_info)) {
2781 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2782 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
2783 vcpu->run->internal.ndata = 2;
2784 vcpu->run->internal.data[0] = vect_info;
2785 vcpu->run->internal.data[1] = intr_info;
2786 return 0;
2787 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002788
Jan Kiszkae4a41882008-09-26 09:30:46 +02002789 if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR)
Avi Kivity1b6269d2007-10-09 12:12:19 +02002790 return 1; /* already handled by vmx_vcpu_run() */
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002791
2792 if (is_no_device(intr_info)) {
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002793 vmx_fpu_activate(vcpu);
Anthony Liguori2ab455c2007-04-27 09:29:49 +03002794 return 1;
2795 }
2796
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002797 if (is_invalid_opcode(intr_info)) {
Avi Kivity851ba692009-08-24 11:10:17 +03002798 er = emulate_instruction(vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002799 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002800 kvm_queue_exception(vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002801 return 1;
2802 }
2803
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 error_code = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002805 rip = kvm_rip_read(vcpu);
Ryan Harper2e113842008-02-11 10:26:38 -06002806 if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002807 error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
2808 if (is_page_fault(intr_info)) {
Sheng Yang14394422008-04-28 12:24:45 +08002809 /* EPT won't cause page fault directly */
Avi Kivity089d0342009-03-23 18:26:32 +02002810 if (enable_ept)
Sheng Yang14394422008-04-28 12:24:45 +08002811 BUG();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002812 cr2 = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002813 trace_kvm_page_fault(cr2, error_code);
2814
Gleb Natapov3298b752009-05-11 13:35:46 +03002815 if (kvm_event_needs_reinjection(vcpu))
Avi Kivity577bdc42008-07-19 08:57:05 +03002816 kvm_mmu_unprotect_page_virt(vcpu, cr2);
Avi Kivity30677142007-10-28 18:48:59 +02002817 return kvm_mmu_page_fault(vcpu, cr2, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002818 }
2819
Avi Kivity7ffd92c2009-06-09 14:10:45 +03002820 if (vmx->rmode.vm86_active &&
Avi Kivity6aa8b732006-12-10 02:21:36 -08002821 handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK,
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002822 error_code)) {
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002823 if (vcpu->arch.halt_request) {
2824 vcpu->arch.halt_request = 0;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002825 return kvm_emulate_halt(vcpu);
2826 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827 return 1;
Avi Kivity72d6e5a2007-06-05 16:15:51 +03002828 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002830 ex_no = intr_info & INTR_INFO_VECTOR_MASK;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002831 switch (ex_no) {
2832 case DB_VECTOR:
2833 dr6 = vmcs_readl(EXIT_QUALIFICATION);
2834 if (!(vcpu->guest_debug &
2835 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
2836 vcpu->arch.dr6 = dr6 | DR6_FIXED_1;
2837 kvm_queue_exception(vcpu, DB_VECTOR);
2838 return 1;
2839 }
2840 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
2841 kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
2842 /* fall through */
2843 case BP_VECTOR:
Avi Kivity6aa8b732006-12-10 02:21:36 -08002844 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002845 kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
2846 kvm_run->debug.arch.exception = ex_no;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002847 break;
2848 default:
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002849 kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
2850 kvm_run->ex.exception = ex_no;
2851 kvm_run->ex.error_code = error_code;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002852 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002853 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 return 0;
2855}
2856
Avi Kivity851ba692009-08-24 11:10:17 +03002857static int handle_external_interrupt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858{
Avi Kivity1165f5f2007-04-19 17:27:43 +03002859 ++vcpu->stat.irq_exits;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002860 return 1;
2861}
2862
Avi Kivity851ba692009-08-24 11:10:17 +03002863static int handle_triple_fault(struct kvm_vcpu *vcpu)
Avi Kivity988ad742007-02-12 00:54:36 -08002864{
Avi Kivity851ba692009-08-24 11:10:17 +03002865 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
Avi Kivity988ad742007-02-12 00:54:36 -08002866 return 0;
2867}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868
Avi Kivity851ba692009-08-24 11:10:17 +03002869static int handle_io(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870{
He, Qingbfdaab02007-09-12 14:18:28 +08002871 unsigned long exit_qualification;
Jan Kiszka34c33d12009-02-08 13:28:15 +01002872 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002873 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874
Avi Kivity1165f5f2007-04-19 17:27:43 +03002875 ++vcpu->stat.io_exits;
He, Qingbfdaab02007-09-12 14:18:28 +08002876 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity039576c2007-03-20 12:46:50 +02002877 string = (exit_qualification & 16) != 0;
Laurent Viviere70669a2007-08-05 10:36:40 +03002878
2879 if (string) {
Avi Kivity851ba692009-08-24 11:10:17 +03002880 if (emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DO_MMIO)
Laurent Viviere70669a2007-08-05 10:36:40 +03002881 return 0;
2882 return 1;
2883 }
2884
2885 size = (exit_qualification & 7) + 1;
2886 in = (exit_qualification & 8) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002887 port = exit_qualification >> 16;
Laurent Viviere70669a2007-08-05 10:36:40 +03002888
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01002889 skip_emulated_instruction(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002890 return kvm_emulate_pio(vcpu, in, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891}
2892
Ingo Molnar102d8322007-02-19 14:37:47 +02002893static void
2894vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
2895{
2896 /*
2897 * Patch in the VMCALL instruction:
2898 */
2899 hypercall[0] = 0x0f;
2900 hypercall[1] = 0x01;
2901 hypercall[2] = 0xc1;
Ingo Molnar102d8322007-02-19 14:37:47 +02002902}
2903
Avi Kivity851ba692009-08-24 11:10:17 +03002904static int handle_cr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002905{
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002906 unsigned long exit_qualification, val;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002907 int cr;
2908 int reg;
2909
He, Qingbfdaab02007-09-12 14:18:28 +08002910 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002911 cr = exit_qualification & 15;
2912 reg = (exit_qualification >> 8) & 15;
2913 switch ((exit_qualification >> 4) & 3) {
2914 case 0: /* mov to cr */
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002915 val = kvm_register_read(vcpu, reg);
2916 trace_kvm_cr_write(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917 switch (cr) {
2918 case 0:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002919 kvm_set_cr0(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002920 skip_emulated_instruction(vcpu);
2921 return 1;
2922 case 3:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002923 kvm_set_cr3(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002924 skip_emulated_instruction(vcpu);
2925 return 1;
2926 case 4:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002927 kvm_set_cr4(vcpu, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002928 skip_emulated_instruction(vcpu);
2929 return 1;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002930 case 8: {
2931 u8 cr8_prev = kvm_get_cr8(vcpu);
2932 u8 cr8 = kvm_register_read(vcpu, reg);
2933 kvm_set_cr8(vcpu, cr8);
2934 skip_emulated_instruction(vcpu);
2935 if (irqchip_in_kernel(vcpu->kvm))
2936 return 1;
2937 if (cr8_prev <= cr8)
2938 return 1;
Avi Kivity851ba692009-08-24 11:10:17 +03002939 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002940 return 0;
2941 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002942 };
2943 break;
Anthony Liguori25c4c272007-04-27 09:29:21 +03002944 case 2: /* clts */
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002945 vmx_fpu_deactivate(vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002946 vcpu->arch.cr0 &= ~X86_CR0_TS;
2947 vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
Avi Kivity5fd86fc2007-05-02 20:40:00 +03002948 vmx_fpu_activate(vcpu);
Anthony Liguori25c4c272007-04-27 09:29:21 +03002949 skip_emulated_instruction(vcpu);
2950 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951 case 1: /*mov from cr*/
2952 switch (cr) {
2953 case 3:
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002954 kvm_register_write(vcpu, reg, vcpu->arch.cr3);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002955 trace_kvm_cr_read(cr, vcpu->arch.cr3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956 skip_emulated_instruction(vcpu);
2957 return 1;
2958 case 8:
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002959 val = kvm_get_cr8(vcpu);
2960 kvm_register_write(vcpu, reg, val);
2961 trace_kvm_cr_read(cr, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002962 skip_emulated_instruction(vcpu);
2963 return 1;
2964 }
2965 break;
2966 case 3: /* lmsw */
Avi Kivity2d3ad1f2008-02-24 11:20:43 +02002967 kvm_lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968
2969 skip_emulated_instruction(vcpu);
2970 return 1;
2971 default:
2972 break;
2973 }
Avi Kivity851ba692009-08-24 11:10:17 +03002974 vcpu->run->exit_reason = 0;
Rusty Russellf0242472007-08-01 10:48:02 +10002975 pr_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976 (int)(exit_qualification >> 4) & 3, cr);
2977 return 0;
2978}
2979
Avi Kivity851ba692009-08-24 11:10:17 +03002980static int handle_dr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002981{
He, Qingbfdaab02007-09-12 14:18:28 +08002982 unsigned long exit_qualification;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002983 unsigned long val;
2984 int dr, reg;
2985
Avi Kivity0a79b002009-09-01 12:03:25 +03002986 if (!kvm_require_cpl(vcpu, 0))
2987 return 1;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002988 dr = vmcs_readl(GUEST_DR7);
2989 if (dr & DR7_GD) {
2990 /*
2991 * As the vm-exit takes precedence over the debug trap, we
2992 * need to emulate the latter, either for the host or the
2993 * guest debugging itself.
2994 */
2995 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
Avi Kivity851ba692009-08-24 11:10:17 +03002996 vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
2997 vcpu->run->debug.arch.dr7 = dr;
2998 vcpu->run->debug.arch.pc =
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002999 vmcs_readl(GUEST_CS_BASE) +
3000 vmcs_readl(GUEST_RIP);
Avi Kivity851ba692009-08-24 11:10:17 +03003001 vcpu->run->debug.arch.exception = DB_VECTOR;
3002 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003003 return 0;
3004 } else {
3005 vcpu->arch.dr7 &= ~DR7_GD;
3006 vcpu->arch.dr6 |= DR6_BD;
3007 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3008 kvm_queue_exception(vcpu, DB_VECTOR);
3009 return 1;
3010 }
3011 }
3012
He, Qingbfdaab02007-09-12 14:18:28 +08003013 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003014 dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
3015 reg = DEBUG_REG_ACCESS_REG(exit_qualification);
3016 if (exit_qualification & TYPE_MOV_FROM_DR) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003017 switch (dr) {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003018 case 0 ... 3:
3019 val = vcpu->arch.db[dr];
3020 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021 case 6:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003022 val = vcpu->arch.dr6;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003023 break;
3024 case 7:
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003025 val = vcpu->arch.dr7;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003026 break;
3027 default:
3028 val = 0;
3029 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003030 kvm_register_write(vcpu, reg, val);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003031 } else {
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003032 val = vcpu->arch.regs[reg];
3033 switch (dr) {
3034 case 0 ... 3:
3035 vcpu->arch.db[dr] = val;
3036 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
3037 vcpu->arch.eff_db[dr] = val;
3038 break;
3039 case 4 ... 5:
3040 if (vcpu->arch.cr4 & X86_CR4_DE)
3041 kvm_queue_exception(vcpu, UD_VECTOR);
3042 break;
3043 case 6:
3044 if (val & 0xffffffff00000000ULL) {
3045 kvm_queue_exception(vcpu, GP_VECTOR);
3046 break;
3047 }
3048 vcpu->arch.dr6 = (val & DR6_VOLATILE) | DR6_FIXED_1;
3049 break;
3050 case 7:
3051 if (val & 0xffffffff00000000ULL) {
3052 kvm_queue_exception(vcpu, GP_VECTOR);
3053 break;
3054 }
3055 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
3056 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
3057 vmcs_writel(GUEST_DR7, vcpu->arch.dr7);
3058 vcpu->arch.switch_db_regs =
3059 (val & DR7_BP_EN_MASK);
3060 }
3061 break;
3062 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064 skip_emulated_instruction(vcpu);
3065 return 1;
3066}
3067
Avi Kivity851ba692009-08-24 11:10:17 +03003068static int handle_cpuid(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069{
Avi Kivity06465c52007-02-28 20:46:53 +02003070 kvm_emulate_cpuid(vcpu);
3071 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003072}
3073
Avi Kivity851ba692009-08-24 11:10:17 +03003074static int handle_rdmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003076 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08003077 u64 data;
3078
3079 if (vmx_get_msr(vcpu, ecx, &data)) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003080 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003081 return 1;
3082 }
3083
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003084 trace_kvm_msr_read(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003085
Avi Kivity6aa8b732006-12-10 02:21:36 -08003086 /* FIXME: handling of bits 32:63 of rax, rdx */
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003087 vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
3088 vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 skip_emulated_instruction(vcpu);
3090 return 1;
3091}
3092
Avi Kivity851ba692009-08-24 11:10:17 +03003093static int handle_wrmsr(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003094{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003095 u32 ecx = vcpu->arch.regs[VCPU_REGS_RCX];
3096 u64 data = (vcpu->arch.regs[VCPU_REGS_RAX] & -1u)
3097 | ((u64)(vcpu->arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003098
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003099 trace_kvm_msr_write(ecx, data);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003100
Avi Kivity6aa8b732006-12-10 02:21:36 -08003101 if (vmx_set_msr(vcpu, ecx, data) != 0) {
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02003102 kvm_inject_gp(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003103 return 1;
3104 }
3105
3106 skip_emulated_instruction(vcpu);
3107 return 1;
3108}
3109
Avi Kivity851ba692009-08-24 11:10:17 +03003110static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003111{
3112 return 1;
3113}
3114
Avi Kivity851ba692009-08-24 11:10:17 +03003115static int handle_interrupt_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003116{
Eddie Dong85f455f2007-07-06 12:20:49 +03003117 u32 cpu_based_vm_exec_control;
3118
3119 /* clear pending irq */
3120 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3121 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
3122 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003123
Jan Kiszkaa26bf122008-09-26 09:30:45 +02003124 ++vcpu->stat.irq_window_exits;
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003125
Dor Laorc1150d82007-01-05 16:36:24 -08003126 /*
3127 * If the user space waits to inject interrupts, exit as soon as
3128 * possible
3129 */
Gleb Natapov80618232009-04-21 17:44:56 +03003130 if (!irqchip_in_kernel(vcpu->kvm) &&
Avi Kivity851ba692009-08-24 11:10:17 +03003131 vcpu->run->request_interrupt_window &&
Gleb Natapov80618232009-04-21 17:44:56 +03003132 !kvm_cpu_has_interrupt(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003133 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
Dor Laorc1150d82007-01-05 16:36:24 -08003134 return 0;
3135 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003136 return 1;
3137}
3138
Avi Kivity851ba692009-08-24 11:10:17 +03003139static int handle_halt(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003140{
3141 skip_emulated_instruction(vcpu);
Avi Kivityd3bef152007-06-05 15:53:05 +03003142 return kvm_emulate_halt(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003143}
3144
Avi Kivity851ba692009-08-24 11:10:17 +03003145static int handle_vmcall(struct kvm_vcpu *vcpu)
Ingo Molnarc21415e2007-02-19 14:37:47 +02003146{
Dor Laor510043d2007-02-19 18:25:43 +02003147 skip_emulated_instruction(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003148 kvm_emulate_hypercall(vcpu);
3149 return 1;
Ingo Molnarc21415e2007-02-19 14:37:47 +02003150}
3151
Avi Kivity851ba692009-08-24 11:10:17 +03003152static int handle_vmx_insn(struct kvm_vcpu *vcpu)
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003153{
3154 kvm_queue_exception(vcpu, UD_VECTOR);
3155 return 1;
3156}
3157
Avi Kivity851ba692009-08-24 11:10:17 +03003158static int handle_invlpg(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03003159{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003160 unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Marcelo Tosattia7052892008-09-23 13:18:35 -03003161
3162 kvm_mmu_invlpg(vcpu, exit_qualification);
3163 skip_emulated_instruction(vcpu);
3164 return 1;
3165}
3166
Avi Kivity851ba692009-08-24 11:10:17 +03003167static int handle_wbinvd(struct kvm_vcpu *vcpu)
Eddie Donge5edaa02007-11-11 12:28:35 +02003168{
3169 skip_emulated_instruction(vcpu);
3170 /* TODO: Add support for VT-d/pass-through device */
3171 return 1;
3172}
3173
Avi Kivity851ba692009-08-24 11:10:17 +03003174static int handle_apic_access(struct kvm_vcpu *vcpu)
Sheng Yangf78e0e22007-10-29 09:40:42 +08003175{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003176 unsigned long exit_qualification;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003177 enum emulation_result er;
3178 unsigned long offset;
3179
Sheng Yangf9c617f2009-03-25 10:08:52 +08003180 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003181 offset = exit_qualification & 0xffful;
3182
Avi Kivity851ba692009-08-24 11:10:17 +03003183 er = emulate_instruction(vcpu, 0, 0, 0);
Sheng Yangf78e0e22007-10-29 09:40:42 +08003184
3185 if (er != EMULATE_DONE) {
3186 printk(KERN_ERR
3187 "Fail to handle apic access vmexit! Offset is 0x%lx\n",
3188 offset);
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003189 return -ENOEXEC;
Sheng Yangf78e0e22007-10-29 09:40:42 +08003190 }
3191 return 1;
3192}
3193
Avi Kivity851ba692009-08-24 11:10:17 +03003194static int handle_task_switch(struct kvm_vcpu *vcpu)
Izik Eidus37817f22008-03-24 23:14:53 +02003195{
Jan Kiszka60637aa2008-09-26 09:30:47 +02003196 struct vcpu_vmx *vmx = to_vmx(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02003197 unsigned long exit_qualification;
3198 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003199 int reason, type, idt_v;
3200
3201 idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
3202 type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
Izik Eidus37817f22008-03-24 23:14:53 +02003203
3204 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
3205
3206 reason = (u32)exit_qualification >> 30;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003207 if (reason == TASK_SWITCH_GATE && idt_v) {
3208 switch (type) {
3209 case INTR_TYPE_NMI_INTR:
3210 vcpu->arch.nmi_injected = false;
3211 if (cpu_has_virtual_nmis())
3212 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3213 GUEST_INTR_STATE_NMI);
3214 break;
3215 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003216 case INTR_TYPE_SOFT_INTR:
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003217 kvm_clear_interrupt_queue(vcpu);
3218 break;
3219 case INTR_TYPE_HARD_EXCEPTION:
3220 case INTR_TYPE_SOFT_EXCEPTION:
3221 kvm_clear_exception_queue(vcpu);
3222 break;
3223 default:
3224 break;
3225 }
Jan Kiszka60637aa2008-09-26 09:30:47 +02003226 }
Izik Eidus37817f22008-03-24 23:14:53 +02003227 tss_selector = exit_qualification;
3228
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003229 if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
3230 type != INTR_TYPE_EXT_INTR &&
3231 type != INTR_TYPE_NMI_INTR))
3232 skip_emulated_instruction(vcpu);
3233
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003234 if (!kvm_task_switch(vcpu, tss_selector, reason))
3235 return 0;
3236
3237 /* clear all local breakpoint enable flags */
3238 vmcs_writel(GUEST_DR7, vmcs_readl(GUEST_DR7) & ~55);
3239
3240 /*
3241 * TODO: What about debug traps on tss switch?
3242 * Are we supposed to inject them and update dr6?
3243 */
3244
3245 return 1;
Izik Eidus37817f22008-03-24 23:14:53 +02003246}
3247
Avi Kivity851ba692009-08-24 11:10:17 +03003248static int handle_ept_violation(struct kvm_vcpu *vcpu)
Sheng Yang14394422008-04-28 12:24:45 +08003249{
Sheng Yangf9c617f2009-03-25 10:08:52 +08003250 unsigned long exit_qualification;
Sheng Yang14394422008-04-28 12:24:45 +08003251 gpa_t gpa;
Sheng Yang14394422008-04-28 12:24:45 +08003252 int gla_validity;
Sheng Yang14394422008-04-28 12:24:45 +08003253
Sheng Yangf9c617f2009-03-25 10:08:52 +08003254 exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
Sheng Yang14394422008-04-28 12:24:45 +08003255
3256 if (exit_qualification & (1 << 6)) {
3257 printk(KERN_ERR "EPT: GPA exceeds GAW!\n");
Jan Kiszka7f582ab2009-07-22 23:53:01 +02003258 return -EINVAL;
Sheng Yang14394422008-04-28 12:24:45 +08003259 }
3260
3261 gla_validity = (exit_qualification >> 7) & 0x3;
3262 if (gla_validity != 0x3 && gla_validity != 0x1 && gla_validity != 0) {
3263 printk(KERN_ERR "EPT: Handling EPT violation failed!\n");
3264 printk(KERN_ERR "EPT: GPA: 0x%lx, GVA: 0x%lx\n",
3265 (long unsigned int)vmcs_read64(GUEST_PHYSICAL_ADDRESS),
Sheng Yangf9c617f2009-03-25 10:08:52 +08003266 vmcs_readl(GUEST_LINEAR_ADDRESS));
Sheng Yang14394422008-04-28 12:24:45 +08003267 printk(KERN_ERR "EPT: Exit qualification is 0x%lx\n",
3268 (long unsigned int)exit_qualification);
Avi Kivity851ba692009-08-24 11:10:17 +03003269 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3270 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_VIOLATION;
Avi Kivity596ae892009-06-03 14:12:10 +03003271 return 0;
Sheng Yang14394422008-04-28 12:24:45 +08003272 }
3273
3274 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003275 trace_kvm_page_fault(gpa, exit_qualification);
Sheng Yang49cd7d22009-02-11 13:50:40 +08003276 return kvm_mmu_page_fault(vcpu, gpa & PAGE_MASK, 0);
Sheng Yang14394422008-04-28 12:24:45 +08003277}
3278
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003279static u64 ept_rsvd_mask(u64 spte, int level)
3280{
3281 int i;
3282 u64 mask = 0;
3283
3284 for (i = 51; i > boot_cpu_data.x86_phys_bits; i--)
3285 mask |= (1ULL << i);
3286
3287 if (level > 2)
3288 /* bits 7:3 reserved */
3289 mask |= 0xf8;
3290 else if (level == 2) {
3291 if (spte & (1ULL << 7))
3292 /* 2MB ref, bits 20:12 reserved */
3293 mask |= 0x1ff000;
3294 else
3295 /* bits 6:3 reserved */
3296 mask |= 0x78;
3297 }
3298
3299 return mask;
3300}
3301
3302static void ept_misconfig_inspect_spte(struct kvm_vcpu *vcpu, u64 spte,
3303 int level)
3304{
3305 printk(KERN_ERR "%s: spte 0x%llx level %d\n", __func__, spte, level);
3306
3307 /* 010b (write-only) */
3308 WARN_ON((spte & 0x7) == 0x2);
3309
3310 /* 110b (write/execute) */
3311 WARN_ON((spte & 0x7) == 0x6);
3312
3313 /* 100b (execute-only) and value not supported by logical processor */
3314 if (!cpu_has_vmx_ept_execute_only())
3315 WARN_ON((spte & 0x7) == 0x4);
3316
3317 /* not 000b */
3318 if ((spte & 0x7)) {
3319 u64 rsvd_bits = spte & ept_rsvd_mask(spte, level);
3320
3321 if (rsvd_bits != 0) {
3322 printk(KERN_ERR "%s: rsvd_bits = 0x%llx\n",
3323 __func__, rsvd_bits);
3324 WARN_ON(1);
3325 }
3326
3327 if (level == 1 || (level == 2 && (spte & (1ULL << 7)))) {
3328 u64 ept_mem_type = (spte & 0x38) >> 3;
3329
3330 if (ept_mem_type == 2 || ept_mem_type == 3 ||
3331 ept_mem_type == 7) {
3332 printk(KERN_ERR "%s: ept_mem_type=0x%llx\n",
3333 __func__, ept_mem_type);
3334 WARN_ON(1);
3335 }
3336 }
3337 }
3338}
3339
Avi Kivity851ba692009-08-24 11:10:17 +03003340static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003341{
3342 u64 sptes[4];
3343 int nr_sptes, i;
3344 gpa_t gpa;
3345
3346 gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
3347
3348 printk(KERN_ERR "EPT: Misconfiguration.\n");
3349 printk(KERN_ERR "EPT: GPA: 0x%llx\n", gpa);
3350
3351 nr_sptes = kvm_mmu_get_spte_hierarchy(vcpu, gpa, sptes);
3352
3353 for (i = PT64_ROOT_LEVEL; i > PT64_ROOT_LEVEL - nr_sptes; --i)
3354 ept_misconfig_inspect_spte(vcpu, sptes[i-1], i);
3355
Avi Kivity851ba692009-08-24 11:10:17 +03003356 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3357 vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG;
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003358
3359 return 0;
3360}
3361
Avi Kivity851ba692009-08-24 11:10:17 +03003362static int handle_nmi_window(struct kvm_vcpu *vcpu)
Sheng Yangf08864b2008-05-15 18:23:25 +08003363{
3364 u32 cpu_based_vm_exec_control;
3365
3366 /* clear pending NMI */
3367 cpu_based_vm_exec_control = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
3368 cpu_based_vm_exec_control &= ~CPU_BASED_VIRTUAL_NMI_PENDING;
3369 vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, cpu_based_vm_exec_control);
3370 ++vcpu->stat.nmi_window_exits;
3371
3372 return 1;
3373}
3374
Mohammed Gamal80ced182009-09-01 12:48:18 +02003375static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003376{
Avi Kivity8b3079a2009-01-05 12:10:54 +02003377 struct vcpu_vmx *vmx = to_vmx(vcpu);
3378 enum emulation_result err = EMULATE_DONE;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003379 int ret = 1;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003380
3381 while (!guest_state_valid(vcpu)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003382 err = emulate_instruction(vcpu, 0, 0, 0);
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003383
Mohammed Gamal80ced182009-09-01 12:48:18 +02003384 if (err == EMULATE_DO_MMIO) {
3385 ret = 0;
3386 goto out;
3387 }
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003388
3389 if (err != EMULATE_DONE) {
3390 kvm_report_emulation_failure(vcpu, "emulation failure");
Mohammed Gamal80ced182009-09-01 12:48:18 +02003391 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3392 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
Avi Kivitya9c73992009-11-04 11:54:59 +02003393 vcpu->run->internal.ndata = 0;
Mohammed Gamal80ced182009-09-01 12:48:18 +02003394 ret = 0;
3395 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003396 }
3397
3398 if (signal_pending(current))
Mohammed Gamal80ced182009-09-01 12:48:18 +02003399 goto out;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003400 if (need_resched())
3401 schedule();
3402 }
3403
Mohammed Gamal80ced182009-09-01 12:48:18 +02003404 vmx->emulation_required = 0;
3405out:
3406 return ret;
Mohammed Gamalea953ef2008-08-17 16:47:05 +03003407}
3408
Avi Kivity6aa8b732006-12-10 02:21:36 -08003409/*
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003410 * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
3411 * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
3412 */
Marcelo Tosatti9fb41ba2009-10-12 19:37:31 -03003413static int handle_pause(struct kvm_vcpu *vcpu)
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003414{
3415 skip_emulated_instruction(vcpu);
3416 kvm_vcpu_on_spin(vcpu);
3417
3418 return 1;
3419}
3420
Sheng Yang59708672009-12-15 13:29:54 +08003421static int handle_invalid_op(struct kvm_vcpu *vcpu)
3422{
3423 kvm_queue_exception(vcpu, UD_VECTOR);
3424 return 1;
3425}
3426
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003427/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428 * The exit handlers return 1 if the exit was handled fully and guest execution
3429 * may resume. Otherwise they set the kvm_run parameter to indicate what needs
3430 * to be done to userspace and return 0.
3431 */
Avi Kivity851ba692009-08-24 11:10:17 +03003432static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433 [EXIT_REASON_EXCEPTION_NMI] = handle_exception,
3434 [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
Avi Kivity988ad742007-02-12 00:54:36 -08003435 [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
Sheng Yangf08864b2008-05-15 18:23:25 +08003436 [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437 [EXIT_REASON_IO_INSTRUCTION] = handle_io,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 [EXIT_REASON_CR_ACCESS] = handle_cr,
3439 [EXIT_REASON_DR_ACCESS] = handle_dr,
3440 [EXIT_REASON_CPUID] = handle_cpuid,
3441 [EXIT_REASON_MSR_READ] = handle_rdmsr,
3442 [EXIT_REASON_MSR_WRITE] = handle_wrmsr,
3443 [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window,
3444 [EXIT_REASON_HLT] = handle_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003445 [EXIT_REASON_INVLPG] = handle_invlpg,
Ingo Molnarc21415e2007-02-19 14:37:47 +02003446 [EXIT_REASON_VMCALL] = handle_vmcall,
Avi Kivitye3c7cb62009-06-16 14:19:52 +03003447 [EXIT_REASON_VMCLEAR] = handle_vmx_insn,
3448 [EXIT_REASON_VMLAUNCH] = handle_vmx_insn,
3449 [EXIT_REASON_VMPTRLD] = handle_vmx_insn,
3450 [EXIT_REASON_VMPTRST] = handle_vmx_insn,
3451 [EXIT_REASON_VMREAD] = handle_vmx_insn,
3452 [EXIT_REASON_VMRESUME] = handle_vmx_insn,
3453 [EXIT_REASON_VMWRITE] = handle_vmx_insn,
3454 [EXIT_REASON_VMOFF] = handle_vmx_insn,
3455 [EXIT_REASON_VMON] = handle_vmx_insn,
Sheng Yangf78e0e22007-10-29 09:40:42 +08003456 [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
3457 [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
Eddie Donge5edaa02007-11-11 12:28:35 +02003458 [EXIT_REASON_WBINVD] = handle_wbinvd,
Izik Eidus37817f22008-03-24 23:14:53 +02003459 [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
Andi Kleena0861c02009-06-08 17:37:09 +08003460 [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
Marcelo Tosatti68f89402009-06-11 12:07:43 -03003461 [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
3462 [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
Zhai, Edwin4b8d54f2009-10-09 18:03:20 +08003463 [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
Sheng Yang59708672009-12-15 13:29:54 +08003464 [EXIT_REASON_MWAIT_INSTRUCTION] = handle_invalid_op,
3465 [EXIT_REASON_MONITOR_INSTRUCTION] = handle_invalid_op,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003466};
3467
3468static const int kvm_vmx_max_exit_handlers =
Robert P. J. Day50a34852007-06-03 13:35:29 -04003469 ARRAY_SIZE(kvm_vmx_exit_handlers);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003470
3471/*
3472 * The guest has exited. See if we can fix it or if we need userspace
3473 * assistance.
3474 */
Avi Kivity851ba692009-08-24 11:10:17 +03003475static int vmx_handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476{
Avi Kivity29bd8a72007-09-10 17:27:03 +03003477 struct vcpu_vmx *vmx = to_vmx(vcpu);
Andi Kleena0861c02009-06-08 17:37:09 +08003478 u32 exit_reason = vmx->exit_reason;
Avi Kivity1155f762007-11-22 11:30:47 +02003479 u32 vectoring_info = vmx->idt_vectoring_info;
Avi Kivity29bd8a72007-09-10 17:27:03 +03003480
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003481 trace_kvm_exit(exit_reason, kvm_rip_read(vcpu));
Feng (Eric) Liu2714d1d2008-04-10 15:31:10 -04003482
Mohammed Gamal80ced182009-09-01 12:48:18 +02003483 /* If guest state is invalid, start emulating */
3484 if (vmx->emulation_required && emulate_invalid_guest_state)
3485 return handle_invalid_guest_state(vcpu);
Guillaume Thouvenin1d5a4d92008-10-29 09:39:42 +01003486
Sheng Yang14394422008-04-28 12:24:45 +08003487 /* Access CR3 don't cause VMExit in paging mode, so we need
3488 * to sync with guest real CR3. */
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003489 if (enable_ept && is_paging(vcpu))
Sheng Yang14394422008-04-28 12:24:45 +08003490 vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
Sheng Yang14394422008-04-28 12:24:45 +08003491
Avi Kivity29bd8a72007-09-10 17:27:03 +03003492 if (unlikely(vmx->fail)) {
Avi Kivity851ba692009-08-24 11:10:17 +03003493 vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3494 vcpu->run->fail_entry.hardware_entry_failure_reason
Avi Kivity29bd8a72007-09-10 17:27:03 +03003495 = vmcs_read32(VM_INSTRUCTION_ERROR);
3496 return 0;
3497 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003498
Mike Dayd77c26f2007-10-08 09:02:08 -04003499 if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
Sheng Yang14394422008-04-28 12:24:45 +08003500 (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
Jan Kiszka60637aa2008-09-26 09:30:47 +02003501 exit_reason != EXIT_REASON_EPT_VIOLATION &&
3502 exit_reason != EXIT_REASON_TASK_SWITCH))
3503 printk(KERN_WARNING "%s: unexpected, valid vectoring info "
3504 "(0x%x) and exit reason is 0x%x\n",
3505 __func__, vectoring_info, exit_reason);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003506
3507 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked)) {
Gleb Natapovc4282df2009-04-21 17:45:07 +03003508 if (vmx_interrupt_allowed(vcpu)) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003509 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003510 } else if (vmx->vnmi_blocked_time > 1000000000LL &&
Jan Kiszka45312202008-12-11 16:54:54 +01003511 vcpu->arch.nmi_pending) {
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003512 /*
3513 * This CPU don't support us in finding the end of an
3514 * NMI-blocked window if the guest runs with IRQs
3515 * disabled. So we pull the trigger after 1 s of
3516 * futile waiting, but inform the user about this.
3517 */
3518 printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
3519 "state on VCPU %d after 1 s timeout\n",
3520 __func__, vcpu->vcpu_id);
3521 vmx->soft_vnmi_blocked = 0;
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003522 }
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003523 }
3524
Avi Kivity6aa8b732006-12-10 02:21:36 -08003525 if (exit_reason < kvm_vmx_max_exit_handlers
3526 && kvm_vmx_exit_handlers[exit_reason])
Avi Kivity851ba692009-08-24 11:10:17 +03003527 return kvm_vmx_exit_handlers[exit_reason](vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003528 else {
Avi Kivity851ba692009-08-24 11:10:17 +03003529 vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
3530 vcpu->run->hw.hardware_exit_reason = exit_reason;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003531 }
3532 return 0;
3533}
3534
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003535static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003536{
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003537 if (irr == -1 || tpr < irr) {
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003538 vmcs_write32(TPR_THRESHOLD, 0);
3539 return;
3540 }
3541
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003542 vmcs_write32(TPR_THRESHOLD, irr);
Yang, Sheng6e5d8652007-09-12 18:03:11 +08003543}
3544
Avi Kivitycf393f72008-07-01 16:20:21 +03003545static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
3546{
3547 u32 exit_intr_info;
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003548 u32 idt_vectoring_info = vmx->idt_vectoring_info;
Avi Kivitycf393f72008-07-01 16:20:21 +03003549 bool unblock_nmi;
3550 u8 vector;
Avi Kivity668f6122008-07-02 09:28:55 +03003551 int type;
3552 bool idtv_info_valid;
Avi Kivitycf393f72008-07-01 16:20:21 +03003553
3554 exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
Gleb Natapov20f65982009-05-11 13:35:55 +03003555
Andi Kleena0861c02009-06-08 17:37:09 +08003556 vmx->exit_reason = vmcs_read32(VM_EXIT_REASON);
3557
3558 /* Handle machine checks before interrupts are enabled */
3559 if ((vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
3560 || (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI
3561 && is_machine_check(exit_intr_info)))
3562 kvm_machine_check();
3563
Gleb Natapov20f65982009-05-11 13:35:55 +03003564 /* We need to handle NMIs before interrupts are enabled */
3565 if ((exit_intr_info & INTR_INFO_INTR_TYPE_MASK) == INTR_TYPE_NMI_INTR &&
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003566 (exit_intr_info & INTR_INFO_VALID_MASK))
Gleb Natapov20f65982009-05-11 13:35:55 +03003567 asm("int $2");
Gleb Natapov20f65982009-05-11 13:35:55 +03003568
3569 idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
3570
Avi Kivitycf393f72008-07-01 16:20:21 +03003571 if (cpu_has_virtual_nmis()) {
3572 unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
3573 vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
3574 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003575 * SDM 3: 27.7.1.2 (September 2008)
Avi Kivitycf393f72008-07-01 16:20:21 +03003576 * Re-set bit "block by NMI" before VM entry if vmexit caused by
3577 * a guest IRET fault.
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003578 * SDM 3: 23.2.2 (September 2008)
3579 * Bit 12 is undefined in any of the following cases:
3580 * If the VM exit sets the valid bit in the IDT-vectoring
3581 * information field.
3582 * If the VM exit is due to a double fault.
Avi Kivitycf393f72008-07-01 16:20:21 +03003583 */
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003584 if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
3585 vector != DF_VECTOR && !idtv_info_valid)
Avi Kivitycf393f72008-07-01 16:20:21 +03003586 vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
3587 GUEST_INTR_STATE_NMI);
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003588 } else if (unlikely(vmx->soft_vnmi_blocked))
3589 vmx->vnmi_blocked_time +=
3590 ktime_to_ns(ktime_sub(ktime_get(), vmx->entry_time));
Avi Kivity668f6122008-07-02 09:28:55 +03003591
Gleb Natapov37b96e92009-03-30 16:03:13 +03003592 vmx->vcpu.arch.nmi_injected = false;
3593 kvm_clear_exception_queue(&vmx->vcpu);
3594 kvm_clear_interrupt_queue(&vmx->vcpu);
3595
3596 if (!idtv_info_valid)
3597 return;
3598
Avi Kivity668f6122008-07-02 09:28:55 +03003599 vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
3600 type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003601
Gleb Natapov64a7ec02009-03-30 16:03:29 +03003602 switch (type) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003603 case INTR_TYPE_NMI_INTR:
3604 vmx->vcpu.arch.nmi_injected = true;
Avi Kivity668f6122008-07-02 09:28:55 +03003605 /*
Gleb Natapov7b4a25c2009-03-30 16:03:08 +03003606 * SDM 3: 27.7.1.2 (September 2008)
Gleb Natapov37b96e92009-03-30 16:03:13 +03003607 * Clear bit "block by NMI" before VM entry if a NMI
3608 * delivery faulted.
Avi Kivity668f6122008-07-02 09:28:55 +03003609 */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003610 vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
3611 GUEST_INTR_STATE_NMI);
3612 break;
Gleb Natapov37b96e92009-03-30 16:03:13 +03003613 case INTR_TYPE_SOFT_EXCEPTION:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003614 vmx->vcpu.arch.event_exit_inst_len =
3615 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3616 /* fall through */
3617 case INTR_TYPE_HARD_EXCEPTION:
Avi Kivity35920a32008-07-03 14:50:12 +03003618 if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
Gleb Natapov37b96e92009-03-30 16:03:13 +03003619 u32 err = vmcs_read32(IDT_VECTORING_ERROR_CODE);
3620 kvm_queue_exception_e(&vmx->vcpu, vector, err);
Avi Kivity35920a32008-07-03 14:50:12 +03003621 } else
3622 kvm_queue_exception(&vmx->vcpu, vector);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003623 break;
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003624 case INTR_TYPE_SOFT_INTR:
3625 vmx->vcpu.arch.event_exit_inst_len =
3626 vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
3627 /* fall through */
Gleb Natapov37b96e92009-03-30 16:03:13 +03003628 case INTR_TYPE_EXT_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003629 kvm_queue_interrupt(&vmx->vcpu, vector,
3630 type == INTR_TYPE_SOFT_INTR);
Gleb Natapov37b96e92009-03-30 16:03:13 +03003631 break;
3632 default:
3633 break;
Avi Kivityf7d92382008-07-03 16:14:28 +03003634 }
Avi Kivitycf393f72008-07-01 16:20:21 +03003635}
3636
Avi Kivity9c8cba32007-11-22 11:42:59 +02003637/*
3638 * Failure to inject an interrupt should give us the information
3639 * in IDT_VECTORING_INFO_FIELD. However, if the failure occurs
3640 * when fetching the interrupt redirection bitmap in the real-mode
3641 * tss, this doesn't happen. So we do it ourselves.
3642 */
3643static void fixup_rmode_irq(struct vcpu_vmx *vmx)
3644{
3645 vmx->rmode.irq.pending = 0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003646 if (kvm_rip_read(&vmx->vcpu) + 1 != vmx->rmode.irq.rip)
Avi Kivity9c8cba32007-11-22 11:42:59 +02003647 return;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003648 kvm_rip_write(&vmx->vcpu, vmx->rmode.irq.rip);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003649 if (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK) {
3650 vmx->idt_vectoring_info &= ~VECTORING_INFO_TYPE_MASK;
3651 vmx->idt_vectoring_info |= INTR_TYPE_EXT_INTR;
3652 return;
3653 }
3654 vmx->idt_vectoring_info =
3655 VECTORING_INFO_VALID_MASK
3656 | INTR_TYPE_EXT_INTR
3657 | vmx->rmode.irq.vector;
3658}
3659
Avi Kivityc8019492008-07-14 14:44:59 +03003660#ifdef CONFIG_X86_64
3661#define R "r"
3662#define Q "q"
3663#else
3664#define R "e"
3665#define Q "l"
3666#endif
3667
Avi Kivity851ba692009-08-24 11:10:17 +03003668static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003670 struct vcpu_vmx *vmx = to_vmx(vcpu);
Avi Kivitye6adf282007-04-30 16:07:54 +03003671
Jan Kiszka3b86cd92008-09-26 09:30:57 +02003672 /* Record the guest's net vcpu time for enforced NMI injections. */
3673 if (unlikely(!cpu_has_virtual_nmis() && vmx->soft_vnmi_blocked))
3674 vmx->entry_time = ktime_get();
3675
Mohammed Gamal80ced182009-09-01 12:48:18 +02003676 /* Don't enter VMX if guest state is invalid, let the exit handler
3677 start emulation until we arrive back to a valid state */
3678 if (vmx->emulation_required && emulate_invalid_guest_state)
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003679 return;
Mohammed Gamala89a8fb2008-08-17 16:42:16 +03003680
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003681 if (test_bit(VCPU_REGS_RSP, (unsigned long *)&vcpu->arch.regs_dirty))
3682 vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
3683 if (test_bit(VCPU_REGS_RIP, (unsigned long *)&vcpu->arch.regs_dirty))
3684 vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
3685
Gleb Natapov787ff732009-05-18 11:44:06 +03003686 /* When single-stepping over STI and MOV SS, we must clear the
3687 * corresponding interruptibility bits in the guest state. Otherwise
3688 * vmentry fails as it then expects bit 14 (BS) in pending debug
3689 * exceptions being set, but that's not correct for the guest debugging
3690 * case. */
3691 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
3692 vmx_set_interrupt_shadow(vcpu, 0);
3693
Avi Kivitye6adf282007-04-30 16:07:54 +03003694 /*
3695 * Loading guest fpu may have cleared host cr0.ts
3696 */
3697 vmcs_writel(HOST_CR0, read_cr0());
3698
Avi Kivitye8a48342009-09-01 16:06:25 +03003699 if (vcpu->arch.switch_db_regs)
3700 set_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003701
Mike Dayd77c26f2007-10-08 09:02:08 -04003702 asm(
Avi Kivity6aa8b732006-12-10 02:21:36 -08003703 /* Store host registers */
Avi Kivityc8019492008-07-14 14:44:59 +03003704 "push %%"R"dx; push %%"R"bp;"
3705 "push %%"R"cx \n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003706 "cmp %%"R"sp, %c[host_rsp](%0) \n\t"
3707 "je 1f \n\t"
3708 "mov %%"R"sp, %c[host_rsp](%0) \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003709 __ex(ASM_VMX_VMWRITE_RSP_RDX) "\n\t"
Avi Kivity313dbd42008-07-17 18:04:30 +03003710 "1: \n\t"
Avi Kivityd3edefc2009-06-16 12:33:56 +03003711 /* Reload cr2 if changed */
3712 "mov %c[cr2](%0), %%"R"ax \n\t"
3713 "mov %%cr2, %%"R"dx \n\t"
3714 "cmp %%"R"ax, %%"R"dx \n\t"
3715 "je 2f \n\t"
3716 "mov %%"R"ax, %%cr2 \n\t"
3717 "2: \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 /* Check if vmlaunch of vmresume is needed */
Avi Kivitye08aa782007-11-15 18:06:18 +02003719 "cmpl $0, %c[launched](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003720 /* Load guest registers. Don't clobber flags. */
Avi Kivityc8019492008-07-14 14:44:59 +03003721 "mov %c[rax](%0), %%"R"ax \n\t"
3722 "mov %c[rbx](%0), %%"R"bx \n\t"
3723 "mov %c[rdx](%0), %%"R"dx \n\t"
3724 "mov %c[rsi](%0), %%"R"si \n\t"
3725 "mov %c[rdi](%0), %%"R"di \n\t"
3726 "mov %c[rbp](%0), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003727#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003728 "mov %c[r8](%0), %%r8 \n\t"
3729 "mov %c[r9](%0), %%r9 \n\t"
3730 "mov %c[r10](%0), %%r10 \n\t"
3731 "mov %c[r11](%0), %%r11 \n\t"
3732 "mov %c[r12](%0), %%r12 \n\t"
3733 "mov %c[r13](%0), %%r13 \n\t"
3734 "mov %c[r14](%0), %%r14 \n\t"
3735 "mov %c[r15](%0), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003737 "mov %c[rcx](%0), %%"R"cx \n\t" /* kills %0 (ecx) */
3738
Avi Kivity6aa8b732006-12-10 02:21:36 -08003739 /* Enter guest mode */
Avi Kivitycd2276a2007-05-14 20:41:13 +03003740 "jne .Llaunched \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003741 __ex(ASM_VMX_VMLAUNCH) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003742 "jmp .Lkvm_vmx_return \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003743 ".Llaunched: " __ex(ASM_VMX_VMRESUME) "\n\t"
Avi Kivitycd2276a2007-05-14 20:41:13 +03003744 ".Lkvm_vmx_return: "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745 /* Save guest registers, load host registers, keep flags */
Avi Kivityc8019492008-07-14 14:44:59 +03003746 "xchg %0, (%%"R"sp) \n\t"
3747 "mov %%"R"ax, %c[rax](%0) \n\t"
3748 "mov %%"R"bx, %c[rbx](%0) \n\t"
3749 "push"Q" (%%"R"sp); pop"Q" %c[rcx](%0) \n\t"
3750 "mov %%"R"dx, %c[rdx](%0) \n\t"
3751 "mov %%"R"si, %c[rsi](%0) \n\t"
3752 "mov %%"R"di, %c[rdi](%0) \n\t"
3753 "mov %%"R"bp, %c[rbp](%0) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003754#ifdef CONFIG_X86_64
Avi Kivitye08aa782007-11-15 18:06:18 +02003755 "mov %%r8, %c[r8](%0) \n\t"
3756 "mov %%r9, %c[r9](%0) \n\t"
3757 "mov %%r10, %c[r10](%0) \n\t"
3758 "mov %%r11, %c[r11](%0) \n\t"
3759 "mov %%r12, %c[r12](%0) \n\t"
3760 "mov %%r13, %c[r13](%0) \n\t"
3761 "mov %%r14, %c[r14](%0) \n\t"
3762 "mov %%r15, %c[r15](%0) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003763#endif
Avi Kivityc8019492008-07-14 14:44:59 +03003764 "mov %%cr2, %%"R"ax \n\t"
3765 "mov %%"R"ax, %c[cr2](%0) \n\t"
3766
3767 "pop %%"R"bp; pop %%"R"bp; pop %%"R"dx \n\t"
Avi Kivitye08aa782007-11-15 18:06:18 +02003768 "setbe %c[fail](%0) \n\t"
3769 : : "c"(vmx), "d"((unsigned long)HOST_RSP),
3770 [launched]"i"(offsetof(struct vcpu_vmx, launched)),
3771 [fail]"i"(offsetof(struct vcpu_vmx, fail)),
Avi Kivity313dbd42008-07-17 18:04:30 +03003772 [host_rsp]"i"(offsetof(struct vcpu_vmx, host_rsp)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003773 [rax]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RAX])),
3774 [rbx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBX])),
3775 [rcx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RCX])),
3776 [rdx]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDX])),
3777 [rsi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RSI])),
3778 [rdi]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RDI])),
3779 [rbp]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_RBP])),
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003780#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003781 [r8]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R8])),
3782 [r9]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R9])),
3783 [r10]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R10])),
3784 [r11]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R11])),
3785 [r12]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R12])),
3786 [r13]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R13])),
3787 [r14]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R14])),
3788 [r15]"i"(offsetof(struct vcpu_vmx, vcpu.arch.regs[VCPU_REGS_R15])),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003789#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003790 [cr2]"i"(offsetof(struct vcpu_vmx, vcpu.arch.cr2))
Laurent Vivierc2036302007-10-25 14:18:52 +02003791 : "cc", "memory"
Avi Kivityc8019492008-07-14 14:44:59 +03003792 , R"bx", R"di", R"si"
Laurent Vivierc2036302007-10-25 14:18:52 +02003793#ifdef CONFIG_X86_64
Laurent Vivierc2036302007-10-25 14:18:52 +02003794 , "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
3795#endif
3796 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003797
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003798 vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
3799 | (1 << VCPU_EXREG_PDPTR));
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003800 vcpu->arch.regs_dirty = 0;
3801
Avi Kivitye8a48342009-09-01 16:06:25 +03003802 if (vcpu->arch.switch_db_regs)
3803 get_debugreg(vcpu->arch.dr6, 6);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01003804
Avi Kivity1155f762007-11-22 11:30:47 +02003805 vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
Avi Kivity9c8cba32007-11-22 11:42:59 +02003806 if (vmx->rmode.irq.pending)
3807 fixup_rmode_irq(vmx);
Avi Kivity1155f762007-11-22 11:30:47 +02003808
Mike Dayd77c26f2007-10-08 09:02:08 -04003809 asm("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));
Avi Kivity15ad7142007-07-11 18:17:21 +03003810 vmx->launched = 1;
Avi Kivity1b6269d2007-10-09 12:12:19 +02003811
Avi Kivitycf393f72008-07-01 16:20:21 +03003812 vmx_complete_interrupts(vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003813}
3814
Avi Kivityc8019492008-07-14 14:44:59 +03003815#undef R
3816#undef Q
3817
Avi Kivity6aa8b732006-12-10 02:21:36 -08003818static void vmx_free_vmcs(struct kvm_vcpu *vcpu)
3819{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003820 struct vcpu_vmx *vmx = to_vmx(vcpu);
3821
3822 if (vmx->vmcs) {
Avi Kivity543e4242008-05-13 16:22:47 +03003823 vcpu_clear(vmx);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003824 free_vmcs(vmx->vmcs);
3825 vmx->vmcs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826 }
3827}
3828
3829static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
3830{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003831 struct vcpu_vmx *vmx = to_vmx(vcpu);
3832
Sheng Yang2384d2b2008-01-17 15:14:33 +08003833 spin_lock(&vmx_vpid_lock);
3834 if (vmx->vpid != 0)
3835 __clear_bit(vmx->vpid, vmx_vpid_bitmap);
3836 spin_unlock(&vmx_vpid_lock);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003837 vmx_free_vmcs(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003838 kfree(vmx->guest_msrs);
3839 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +10003840 kmem_cache_free(kvm_vcpu_cache, vmx);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003841}
3842
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003843static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003844{
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003845 int err;
Rusty Russellc16f8622007-07-30 21:12:19 +10003846 struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Avi Kivity15ad7142007-07-11 18:17:21 +03003847 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003848
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003849 if (!vmx)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003850 return ERR_PTR(-ENOMEM);
3851
Sheng Yang2384d2b2008-01-17 15:14:33 +08003852 allocate_vpid(vmx);
3853
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003854 err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
3855 if (err)
3856 goto free_vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003857
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003858 vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003859 if (!vmx->guest_msrs) {
3860 err = -ENOMEM;
3861 goto uninit_vcpu;
3862 }
Ingo Molnar965b58a2007-01-05 16:36:23 -08003863
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003864 vmx->vmcs = alloc_vmcs();
3865 if (!vmx->vmcs)
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003866 goto free_msrs;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003867
3868 vmcs_clear(vmx->vmcs);
3869
Avi Kivity15ad7142007-07-11 18:17:21 +03003870 cpu = get_cpu();
3871 vmx_vcpu_load(&vmx->vcpu, cpu);
Rusty Russell8b9cf982007-07-30 16:31:43 +10003872 err = vmx_vcpu_setup(vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003873 vmx_vcpu_put(&vmx->vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03003874 put_cpu();
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003875 if (err)
3876 goto free_vmcs;
Marcelo Tosatti5e4a0b32008-02-14 21:21:43 -02003877 if (vm_need_virtualize_apic_accesses(kvm))
3878 if (alloc_apic_access_page(kvm) != 0)
3879 goto free_vmcs;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003880
Sheng Yangb927a3c2009-07-21 10:42:48 +08003881 if (enable_ept) {
3882 if (!kvm->arch.ept_identity_map_addr)
3883 kvm->arch.ept_identity_map_addr =
3884 VMX_EPT_IDENTITY_PAGETABLE_ADDR;
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003885 if (alloc_identity_pagetable(kvm) != 0)
3886 goto free_vmcs;
Sheng Yangb927a3c2009-07-21 10:42:48 +08003887 }
Sheng Yangb7ebfb02008-04-25 21:44:52 +08003888
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003889 return &vmx->vcpu;
Ingo Molnar965b58a2007-01-05 16:36:23 -08003890
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003891free_vmcs:
3892 free_vmcs(vmx->vmcs);
3893free_msrs:
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003894 kfree(vmx->guest_msrs);
3895uninit_vcpu:
3896 kvm_vcpu_uninit(&vmx->vcpu);
3897free_vcpu:
Rusty Russella4770342007-08-01 14:46:11 +10003898 kmem_cache_free(kvm_vcpu_cache, vmx);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003899 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003900}
3901
Yang, Sheng002c7f72007-07-31 14:23:01 +03003902static void __init vmx_check_processor_compat(void *rtn)
3903{
3904 struct vmcs_config vmcs_conf;
3905
3906 *(int *)rtn = 0;
3907 if (setup_vmcs_config(&vmcs_conf) < 0)
3908 *(int *)rtn = -EIO;
3909 if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
3910 printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
3911 smp_processor_id());
3912 *(int *)rtn = -EIO;
3913 }
3914}
3915
Sheng Yang67253af2008-04-25 10:20:22 +08003916static int get_ept_level(void)
3917{
3918 return VMX_EPT_DEFAULT_GAW + 1;
3919}
3920
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003921static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003922{
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003923 u64 ret;
3924
Sheng Yang522c68c2009-04-27 20:35:43 +08003925 /* For VT-d and EPT combination
3926 * 1. MMIO: always map as UC
3927 * 2. EPT with VT-d:
3928 * a. VT-d without snooping control feature: can't guarantee the
3929 * result, try to trust guest.
3930 * b. VT-d with snooping control feature: snooping control feature of
3931 * VT-d engine can guarantee the cache correctness. Just set it
3932 * to WB to keep consistent with host. So the same as item 3.
3933 * 3. EPT without VT-d: always map as WB and set IGMT=1 to keep
3934 * consistent with host MTRR
3935 */
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003936 if (is_mmio)
3937 ret = MTRR_TYPE_UNCACHABLE << VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang522c68c2009-04-27 20:35:43 +08003938 else if (vcpu->kvm->arch.iommu_domain &&
3939 !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY))
3940 ret = kvm_get_guest_memory_type(vcpu, gfn) <<
3941 VMX_EPT_MT_EPTE_SHIFT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003942 else
Sheng Yang522c68c2009-04-27 20:35:43 +08003943 ret = (MTRR_TYPE_WRBACK << VMX_EPT_MT_EPTE_SHIFT)
3944 | VMX_EPT_IGMT_BIT;
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003945
3946 return ret;
Sheng Yang64d4d522008-10-09 16:01:57 +08003947}
3948
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003949static const struct trace_print_flags vmx_exit_reasons_str[] = {
3950 { EXIT_REASON_EXCEPTION_NMI, "exception" },
3951 { EXIT_REASON_EXTERNAL_INTERRUPT, "ext_irq" },
3952 { EXIT_REASON_TRIPLE_FAULT, "triple_fault" },
3953 { EXIT_REASON_NMI_WINDOW, "nmi_window" },
3954 { EXIT_REASON_IO_INSTRUCTION, "io_instruction" },
3955 { EXIT_REASON_CR_ACCESS, "cr_access" },
3956 { EXIT_REASON_DR_ACCESS, "dr_access" },
3957 { EXIT_REASON_CPUID, "cpuid" },
3958 { EXIT_REASON_MSR_READ, "rdmsr" },
3959 { EXIT_REASON_MSR_WRITE, "wrmsr" },
3960 { EXIT_REASON_PENDING_INTERRUPT, "interrupt_window" },
3961 { EXIT_REASON_HLT, "halt" },
3962 { EXIT_REASON_INVLPG, "invlpg" },
3963 { EXIT_REASON_VMCALL, "hypercall" },
3964 { EXIT_REASON_TPR_BELOW_THRESHOLD, "tpr_below_thres" },
3965 { EXIT_REASON_APIC_ACCESS, "apic_access" },
3966 { EXIT_REASON_WBINVD, "wbinvd" },
3967 { EXIT_REASON_TASK_SWITCH, "task_switch" },
3968 { EXIT_REASON_EPT_VIOLATION, "ept_violation" },
3969 { -1, NULL }
3970};
3971
Joerg Roedel344f4142009-07-27 16:30:48 +02003972static bool vmx_gb_page_enable(void)
3973{
3974 return false;
3975}
3976
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003977static struct kvm_x86_ops vmx_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003978 .cpu_has_kvm_support = cpu_has_kvm_support,
3979 .disabled_by_bios = vmx_disabled_by_bios,
3980 .hardware_setup = hardware_setup,
3981 .hardware_unsetup = hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003982 .check_processor_compatibility = vmx_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983 .hardware_enable = hardware_enable,
3984 .hardware_disable = hardware_disable,
Sheng Yang04547152009-04-01 15:52:31 +08003985 .cpu_has_accelerated_tpr = report_flexpriority,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986
3987 .vcpu_create = vmx_create_vcpu,
3988 .vcpu_free = vmx_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003989 .vcpu_reset = vmx_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990
Avi Kivity04d2cc72007-09-10 18:10:54 +03003991 .prepare_guest_switch = vmx_save_host_state,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003992 .vcpu_load = vmx_vcpu_load,
3993 .vcpu_put = vmx_vcpu_put,
3994
3995 .set_guest_debug = set_guest_debug,
3996 .get_msr = vmx_get_msr,
3997 .set_msr = vmx_set_msr,
3998 .get_segment_base = vmx_get_segment_base,
3999 .get_segment = vmx_get_segment,
4000 .set_segment = vmx_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004001 .get_cpl = vmx_get_cpl,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004002 .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03004003 .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004004 .set_cr0 = vmx_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004005 .set_cr3 = vmx_set_cr3,
4006 .set_cr4 = vmx_set_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004007 .set_efer = vmx_set_efer,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004008 .get_idt = vmx_get_idt,
4009 .set_idt = vmx_set_idt,
4010 .get_gdt = vmx_get_gdt,
4011 .set_gdt = vmx_set_gdt,
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03004012 .cache_reg = vmx_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013 .get_rflags = vmx_get_rflags,
4014 .set_rflags = vmx_set_rflags,
4015
4016 .tlb_flush = vmx_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004017
Avi Kivity6aa8b732006-12-10 02:21:36 -08004018 .run = vmx_vcpu_run,
Avi Kivity6062d012009-03-23 17:35:17 +02004019 .handle_exit = vmx_handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004020 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004021 .set_interrupt_shadow = vmx_set_interrupt_shadow,
4022 .get_interrupt_shadow = vmx_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004023 .patch_hypercall = vmx_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004024 .set_irq = vmx_inject_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004025 .set_nmi = vmx_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004026 .queue_exception = vmx_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02004027 .interrupt_allowed = vmx_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004028 .nmi_allowed = vmx_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004029 .get_nmi_mask = vmx_get_nmi_mask,
4030 .set_nmi_mask = vmx_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004031 .enable_nmi_window = enable_nmi_window,
4032 .enable_irq_window = enable_irq_window,
4033 .update_cr8_intercept = update_cr8_intercept,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004034
Izik Eiduscbc94022007-10-25 00:29:55 +02004035 .set_tss_addr = vmx_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08004036 .get_tdp_level = get_ept_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004037 .get_mt_mask = vmx_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004038
4039 .exit_reasons_str = vmx_exit_reasons_str,
Joerg Roedel344f4142009-07-27 16:30:48 +02004040 .gb_page_enable = vmx_gb_page_enable,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004041};
4042
4043static int __init vmx_init(void)
4044{
Avi Kivity26bb0982009-09-07 11:14:12 +03004045 int r, i;
4046
4047 rdmsrl_safe(MSR_EFER, &host_efer);
4048
4049 for (i = 0; i < NR_VMX_MSR; ++i)
4050 kvm_define_shared_msr(i, vmx_msr_index[i]);
He, Qingfdef3ad2007-04-30 09:45:24 +03004051
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004052 vmx_io_bitmap_a = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004053 if (!vmx_io_bitmap_a)
4054 return -ENOMEM;
4055
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004056 vmx_io_bitmap_b = (unsigned long *)__get_free_page(GFP_KERNEL);
He, Qingfdef3ad2007-04-30 09:45:24 +03004057 if (!vmx_io_bitmap_b) {
4058 r = -ENOMEM;
4059 goto out;
4060 }
4061
Avi Kivity58972972009-02-24 22:26:47 +02004062 vmx_msr_bitmap_legacy = (unsigned long *)__get_free_page(GFP_KERNEL);
4063 if (!vmx_msr_bitmap_legacy) {
Sheng Yang25c5f222008-03-28 13:18:56 +08004064 r = -ENOMEM;
4065 goto out1;
4066 }
4067
Avi Kivity58972972009-02-24 22:26:47 +02004068 vmx_msr_bitmap_longmode = (unsigned long *)__get_free_page(GFP_KERNEL);
4069 if (!vmx_msr_bitmap_longmode) {
4070 r = -ENOMEM;
4071 goto out2;
4072 }
4073
He, Qingfdef3ad2007-04-30 09:45:24 +03004074 /*
4075 * Allow direct access to the PC debug port (it is often used for I/O
4076 * delays, but the vmexits simply slow things down).
4077 */
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004078 memset(vmx_io_bitmap_a, 0xff, PAGE_SIZE);
4079 clear_bit(0x80, vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004080
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004081 memset(vmx_io_bitmap_b, 0xff, PAGE_SIZE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004082
Avi Kivity58972972009-02-24 22:26:47 +02004083 memset(vmx_msr_bitmap_legacy, 0xff, PAGE_SIZE);
4084 memset(vmx_msr_bitmap_longmode, 0xff, PAGE_SIZE);
Sheng Yang25c5f222008-03-28 13:18:56 +08004085
Sheng Yang2384d2b2008-01-17 15:14:33 +08004086 set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
4087
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004088 r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
He, Qingfdef3ad2007-04-30 09:45:24 +03004089 if (r)
Avi Kivity58972972009-02-24 22:26:47 +02004090 goto out3;
Sheng Yang25c5f222008-03-28 13:18:56 +08004091
Avi Kivity58972972009-02-24 22:26:47 +02004092 vmx_disable_intercept_for_msr(MSR_FS_BASE, false);
4093 vmx_disable_intercept_for_msr(MSR_GS_BASE, false);
4094 vmx_disable_intercept_for_msr(MSR_KERNEL_GS_BASE, true);
4095 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_CS, false);
4096 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_ESP, false);
4097 vmx_disable_intercept_for_msr(MSR_IA32_SYSENTER_EIP, false);
He, Qingfdef3ad2007-04-30 09:45:24 +03004098
Avi Kivity089d0342009-03-23 18:26:32 +02004099 if (enable_ept) {
Sheng Yang14394422008-04-28 12:24:45 +08004100 bypass_guest_pf = 0;
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004101 kvm_mmu_set_base_ptes(VMX_EPT_READABLE_MASK |
Sheng Yang2aaf69d2009-01-21 16:52:16 +08004102 VMX_EPT_WRITABLE_MASK);
Sheng Yang534e38b2008-09-08 15:12:30 +08004103 kvm_mmu_set_mask_ptes(0ull, 0ull, 0ull, 0ull,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004104 VMX_EPT_EXECUTABLE_MASK);
Sheng Yang5fdbcb92008-07-16 09:25:40 +08004105 kvm_enable_tdp();
4106 } else
4107 kvm_disable_tdp();
Sheng Yang14394422008-04-28 12:24:45 +08004108
Avi Kivityc7addb92007-09-16 18:58:32 +02004109 if (bypass_guest_pf)
4110 kvm_mmu_set_nonpresent_ptes(~0xffeull, 0ull);
4111
He, Qingfdef3ad2007-04-30 09:45:24 +03004112 return 0;
4113
Avi Kivity58972972009-02-24 22:26:47 +02004114out3:
4115 free_page((unsigned long)vmx_msr_bitmap_longmode);
Sheng Yang25c5f222008-03-28 13:18:56 +08004116out2:
Avi Kivity58972972009-02-24 22:26:47 +02004117 free_page((unsigned long)vmx_msr_bitmap_legacy);
He, Qingfdef3ad2007-04-30 09:45:24 +03004118out1:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004119 free_page((unsigned long)vmx_io_bitmap_b);
He, Qingfdef3ad2007-04-30 09:45:24 +03004120out:
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004121 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004122 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004123}
4124
4125static void __exit vmx_exit(void)
4126{
Avi Kivity58972972009-02-24 22:26:47 +02004127 free_page((unsigned long)vmx_msr_bitmap_legacy);
4128 free_page((unsigned long)vmx_msr_bitmap_longmode);
Avi Kivity3e7c73e2009-02-24 21:46:19 +02004129 free_page((unsigned long)vmx_io_bitmap_b);
4130 free_page((unsigned long)vmx_io_bitmap_a);
He, Qingfdef3ad2007-04-30 09:45:24 +03004131
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004132 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004133}
4134
4135module_init(vmx_init)
4136module_exit(vmx_exit)